↑ Contents

Lean sources, part 30 of 50 (lib/TensorSq3.lean – ym/CBM10.lean)

lib/TensorSq3.lean

lean_all/lib/TensorSq3.lean

/-
  lib/TensorSq3.lean — T46 shared library, cycle 592 (G310): M⊗M = ∧²M ⊕ Sym²M for 3×3 (local factors).

  Any commutative ring, M any 3×3, MM = fl(M⊗M) (9×9). Integer unimodular Q = [e_ab − e_ba (a < b), e_ab (a ≤ b)] (checked by script), P = Q⁻¹:
    PMQ is 3 | 6 block upper triangular, blocks = Ext2Three.ext2 M, Sym2Three.sym2 M (equal entrywise)
    tensor_sq3   det(I − x(M⊗M)) = det(I − x∧²M) · det(I − x Sym²M)     (BlockSim.block_sim_euler)
  (Local GL3 version of L(π × π) = L(Sym²π) L(∧²π), without a diagonalization hypothesis.)
  Lean 4 core only, no axioms.
-/
import lib.Ext2ThreeInv

namespace LibTensorSq3
open DetChain LibRing LibSum LibSemi LibMat LibDet LibEulerSum LibKron LibKronDetGen

variable {R : Type} (K : CRing R) (M : Nat → Nat → R)


def Pt : Nat → Nat → R := fun i j =>
  (cond (Nat.beq i 0) (cond (Nat.beq j 0) K.zero (cond (Nat.beq j 1) K.zero (cond (Nat.beq j 2) K.zero (cond (Nat.beq j 3) (K.neg K.one) (cond (Nat.beq j 4) K.zero (cond (Nat.beq j 5) K.zero (cond (Nat.beq j 6) K.zero (cond (Nat.beq j 7) K.zero K.zero)))))))) (cond (Nat.beq i 1) (cond (Nat.beq j 0) K.zero (cond (Nat.beq j 1) K.zero (cond (Nat.beq j 2) K.zero (cond (Nat.beq j 3) K.zero (cond (Nat.b  -- (+1860 chars; full line in the Zenodo source)

def Qt : Nat → Nat → R := fun i j =>
  (cond (Nat.beq i 0) (cond (Nat.beq j 0) K.zero (cond (Nat.beq j 1) K.zero (cond (Nat.beq j 2) K.zero (cond (Nat.beq j 3) K.one (cond (Nat.beq j 4) K.zero (cond (Nat.beq j 5) K.zero (cond (Nat.beq j 6) K.zero (cond (Nat.beq j 7) K.zero K.zero)))))))) (cond (Nat.beq i 1) (cond (Nat.beq j 0) K.one (cond (Nat.beq j 1) K.zero (cond (Nat.beq j 2) K.zero (cond (Nat.beq j 3) K.zero (cond (Nat.beq j 4) K  -- (+1860 chars; full line in the Zenodo source)

def MM : Nat → Nat → R := fl 3 (kron K M M)

theorem pq_0_0 : mmul (toSemi K) 9 (Pt K) (Qt K) 0 0 = idM K 0 0 := by
  show (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.one) (K.add (K.mul K.zero K.zero) (K.add (K.mul (K.neg K.one) (K.neg K.one)) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) K.zero))))))))) = K.one; lib_ring

theorem pq_0_1 : mmul (toSemi K) 9 (Pt K) (Qt K) 0 1 = idM K 0 1 := by
  show (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.one) (K.add (K.mul (K.neg K.one) K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero (K.neg K.one)) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) K.zero))))))))) = K.zero; lib_ring

theorem pq_0_2 : mmul (toSemi K) 9 (Pt K) (Qt K) 0 2 = idM K 0 2 := by
  show (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul (K.neg K.one) K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.one) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero (K.neg K.one)) (K.add (K.mul K.zero K.zero) K.zero))))))))) = K.zero; lib_ring

theorem pq_0_3 : mmul (toSemi K) 9 (Pt K) (Qt K) 0 3 = idM K 0 3 := by
  show (K.add (K.mul K.zero K.one) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul (K.neg K.one) K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) K.zero))))))))) = K.zero; lib_ring

theorem pq_0_4 : mmul (toSemi K) 9 (Pt K) (Qt K) 0 4 = idM K 0 4 := by
  show (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.one) (K.add (K.mul K.zero K.zero) (K.add (K.mul (K.neg K.one) K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) K.zero))))))))) = K.zero; lib_ring

theorem pq_0_5 : mmul (toSemi K) 9 (Pt K) (Qt K) 0 5 = idM K 0 5 := by
  show (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.one) (K.add (K.mul (K.neg K.one) K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) K.zero))))))))) = K.zero; lib_ring

theorem pq_0_6 : mmul (toSemi K) 9 (Pt K) (Qt K) 0 6 = idM K 0 6 := by
  show (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul (K.neg K.one) K.zero) (K.add (K.mul K.zero K.one) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) K.zero))))))))) = K.zero; lib_ring

theorem pq_0_7 : mmul (toSemi K) 9 (Pt K) (Qt K) 0 7 = idM K 0 7 := by
  show (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul (K.neg K.one) K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.one) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) K.zero))))))))) = K.zero; lib_ring

theorem pq_0_8 : mmul (toSemi K) 9 (Pt K) (Qt K) 0 8 = idM K 0 8 := by
  show (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul (K.neg K.one) K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.one) K.zero))))))))) = K.zero; lib_ring

theorem pq_1_0 : mmul (toSemi K) 9 (Pt K) (Qt K) 1 0 = idM K 1 0 := by
  show (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.one) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero (K.neg K.one)) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul (K.neg K.one) K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) K.zero))))))))) = K.zero; lib_ring

theorem pq_1_1 : mmul (toSemi K) 9 (Pt K) (Qt K) 1 1 = idM K 1 1 := by
  show (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.one) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul (K.neg K.one) (K.neg K.one)) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) K.zero))))))))) = K.one; lib_ring

theorem pq_1_2 : mmul (toSemi K) 9 (Pt K) (Qt K) 1 2 = idM K 1 2 := by
  show (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.one) (K.add (K.mul (K.neg K.one) K.zero) (K.add (K.mul K.zero (K.neg K.one)) (K.add (K.mul K.zero K.zero) K.zero))))))))) = K.zero; lib_ring

theorem pq_1_3 : mmul (toSemi K) 9 (Pt K) (Qt K) 1 3 = idM K 1 3 := by
  show (K.add (K.mul K.zero K.one) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul (K.neg K.one) K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) K.zero))))))))) = K.zero; lib_ring

theorem pq_1_4 : mmul (toSemi K) 9 (Pt K) (Qt K) 1 4 = idM K 1 4 := by
  show (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.one) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul (K.neg K.one) K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) K.zero))))))))) = K.zero; lib_ring

theorem pq_1_5 : mmul (toSemi K) 9 (Pt K) (Qt K) 1 5 = idM K 1 5 := by
  show (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.one) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul (K.neg K.one) K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) K.zero))))))))) = K.zero; lib_ring

theorem pq_1_6 : mmul (toSemi K) 9 (Pt K) (Qt K) 1 6 = idM K 1 6 := by
  show (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.one) (K.add (K.mul K.zero K.zero) (K.add (K.mul (K.neg K.one) K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) K.zero))))))))) = K.zero; lib_ring

theorem pq_1_7 : mmul (toSemi K) 9 (Pt K) (Qt K) 1 7 = idM K 1 7 := by
  show (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.one) (K.add (K.mul (K.neg K.one) K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) K.zero))))))))) = K.zero; lib_ring

theorem pq_1_8 : mmul (toSemi K) 9 (Pt K) (Qt K) 1 8 = idM K 1 8 := by
  show (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul (K.neg K.one) K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.one) K.zero))))))))) = K.zero; lib_ring

theorem pq_2_0 : mmul (toSemi K) 9 (Pt K) (Qt K) 2 0 = idM K 2 0 := by
  show (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.one) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero (K.neg K.one)) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul (K.neg K.one) K.zero) (K.add (K.mul K.zero K.zero) K.zero))))))))) = K.zero; lib_ring

theorem pq_2_1 : mmul (toSemi K) 9 (Pt K) (Qt K) 2 1 = idM K 2 1 := by
  show (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.one) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero (K.neg K.one)) (K.add (K.mul (K.neg K.one) K.zero) (K.add (K.mul K.zero K.zero) K.zero))))))))) = K.zero; lib_ring

theorem pq_2_2 : mmul (toSemi K) 9 (Pt K) (Qt K) 2 2 = idM K 2 2 := by
  show (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.one) (K.add (K.mul K.zero K.zero) (K.add (K.mul (K.neg K.one) (K.neg K.one)) (K.add (K.mul K.zero K.zero) K.zero))))))))) = K.one; lib_ring

theorem pq_2_3 : mmul (toSemi K) 9 (Pt K) (Qt K) 2 3 = idM K 2 3 := by
  show (K.add (K.mul K.zero K.one) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul (K.neg K.one) K.zero) (K.add (K.mul K.zero K.zero) K.zero))))))))) = K.zero; lib_ring

theorem pq_2_4 : mmul (toSemi K) 9 (Pt K) (Qt K) 2 4 = idM K 2 4 := by
  show (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.one) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul (K.neg K.one) K.zero) (K.add (K.mul K.zero K.zero) K.zero))))))))) = K.zero; lib_ring

theorem pq_2_5 : mmul (toSemi K) 9 (Pt K) (Qt K) 2 5 = idM K 2 5 := by
  show (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.one) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul (K.neg K.one) K.zero) (K.add (K.mul K.zero K.zero) K.zero))))))))) = K.zero; lib_ring

theorem pq_2_6 : mmul (toSemi K) 9 (Pt K) (Qt K) 2 6 = idM K 2 6 := by
  show (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.one) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul (K.neg K.one) K.zero) (K.add (K.mul K.zero K.zero) K.zero))))))))) = K.zero; lib_ring

theorem pq_2_7 : mmul (toSemi K) 9 (Pt K) (Qt K) 2 7 = idM K 2 7 := by
  show (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.one) (K.add (K.mul K.zero K.zero) (K.add (K.mul (K.neg K.one) K.zero) (K.add (K.mul K.zero K.zero) K.zero))))))))) = K.zero; lib_ring

theorem pq_2_8 : mmul (toSemi K) 9 (Pt K) (Qt K) 2 8 = idM K 2 8 := by
  show (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul (K.neg K.one) K.zero) (K.add (K.mul K.zero K.one) K.zero))))))))) = K.zero; lib_ring

theorem pq_3_0 : mmul (toSemi K) 9 (Pt K) (Qt K) 3 0 = idM K 3 0 := by
  show (K.add (K.mul K.one K.zero) (K.add (K.mul K.zero K.one) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero (K.neg K.one)) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) K.zero))))))))) = K.zero; lib_ring

theorem pq_3_1 : mmul (toSemi K) 9 (Pt K) (Qt K) 3 1 = idM K 3 1 := by
  show (K.add (K.mul K.one K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.one) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero (K.neg K.one)) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) K.zero))))))))) = K.zero; lib_ring

theorem pq_3_2 : mmul (toSemi K) 9 (Pt K) (Qt K) 3 2 = idM K 3 2 := by
  show (K.add (K.mul K.one K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.one) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero (K.neg K.one)) (K.add (K.mul K.zero K.zero) K.zero))))))))) = K.zero; lib_ring

theorem pq_3_3 : mmul (toSemi K) 9 (Pt K) (Qt K) 3 3 = idM K 3 3 := by
  show (K.add (K.mul K.one K.one) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) K.zero))))))))) = K.one; lib_ring

theorem pq_3_4 : mmul (toSemi K) 9 (Pt K) (Qt K) 3 4 = idM K 3 4 := by
  show (K.add (K.mul K.one K.zero) (K.add (K.mul K.zero K.one) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) K.zero))))))))) = K.zero; lib_ring

theorem pq_3_5 : mmul (toSemi K) 9 (Pt K) (Qt K) 3 5 = idM K 3 5 := by
  show (K.add (K.mul K.one K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.one) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) K.zero))))))))) = K.zero; lib_ring

theorem pq_3_6 : mmul (toSemi K) 9 (Pt K) (Qt K) 3 6 = idM K 3 6 := by
  show (K.add (K.mul K.one K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.one) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) K.zero))))))))) = K.zero; lib_ring

theorem pq_3_7 : mmul (toSemi K) 9 (Pt K) (Qt K) 3 7 = idM K 3 7 := by
  show (K.add (K.mul K.one K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.one) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) K.zero))))))))) = K.zero; lib_ring

theorem pq_3_8 : mmul (toSemi K) 9 (Pt K) (Qt K) 3 8 = idM K 3 8 := by
  show (K.add (K.mul K.one K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.one) K.zero))))))))) = K.zero; lib_ring

theorem pq_4_0 : mmul (toSemi K) 9 (Pt K) (Qt K) 4 0 = idM K 4 0 := by
  show (K.add (K.mul K.zero K.zero) (K.add (K.mul K.one K.one) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.one (K.neg K.one)) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) K.zero))))))))) = K.zero; lib_ring

theorem pq_4_1 : mmul (toSemi K) 9 (Pt K) (Qt K) 4 1 = idM K 4 1 := by
  show (K.add (K.mul K.zero K.zero) (K.add (K.mul K.one K.zero) (K.add (K.mul K.zero K.one) (K.add (K.mul K.one K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero (K.neg K.one)) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) K.zero))))))))) = K.zero; lib_ring

theorem pq_4_2 : mmul (toSemi K) 9 (Pt K) (Qt K) 4 2 = idM K 4 2 := by
  show (K.add (K.mul K.zero K.zero) (K.add (K.mul K.one K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.one K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.one) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero (K.neg K.one)) (K.add (K.mul K.zero K.zero) K.zero))))))))) = K.zero; lib_ring

theorem pq_4_3 : mmul (toSemi K) 9 (Pt K) (Qt K) 4 3 = idM K 4 3 := by
  show (K.add (K.mul K.zero K.one) (K.add (K.mul K.one K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.one K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) K.zero))))))))) = K.zero; lib_ring

theorem pq_4_4 : mmul (toSemi K) 9 (Pt K) (Qt K) 4 4 = idM K 4 4 := by
  show (K.add (K.mul K.zero K.zero) (K.add (K.mul K.one K.one) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.one K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) K.zero))))))))) = K.one; lib_ring

theorem pq_4_5 : mmul (toSemi K) 9 (Pt K) (Qt K) 4 5 = idM K 4 5 := by
  show (K.add (K.mul K.zero K.zero) (K.add (K.mul K.one K.zero) (K.add (K.mul K.zero K.one) (K.add (K.mul K.one K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) K.zero))))))))) = K.zero; lib_ring

theorem pq_4_6 : mmul (toSemi K) 9 (Pt K) (Qt K) 4 6 = idM K 4 6 := by
  show (K.add (K.mul K.zero K.zero) (K.add (K.mul K.one K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.one K.zero) (K.add (K.mul K.zero K.one) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) K.zero))))))))) = K.zero; lib_ring

theorem pq_4_7 : mmul (toSemi K) 9 (Pt K) (Qt K) 4 7 = idM K 4 7 := by
  show (K.add (K.mul K.zero K.zero) (K.add (K.mul K.one K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.one K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.one) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) K.zero))))))))) = K.zero; lib_ring

theorem pq_4_8 : mmul (toSemi K) 9 (Pt K) (Qt K) 4 8 = idM K 4 8 := by
  show (K.add (K.mul K.zero K.zero) (K.add (K.mul K.one K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.one K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.one) K.zero))))))))) = K.zero; lib_ring

theorem pq_5_0 : mmul (toSemi K) 9 (Pt K) (Qt K) 5 0 = idM K 5 0 := by
  show (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.one) (K.add (K.mul K.one K.zero) (K.add (K.mul K.zero (K.neg K.one)) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.one K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) K.zero))))))))) = K.zero; lib_ring

theorem pq_5_1 : mmul (toSemi K) 9 (Pt K) (Qt K) 5 1 = idM K 5 1 := by
  show (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.one K.one) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.one (K.neg K.one)) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) K.zero))))))))) = K.zero; lib_ring

theorem pq_5_2 : mmul (toSemi K) 9 (Pt K) (Qt K) 5 2 = idM K 5 2 := by
  show (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.one K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.one) (K.add (K.mul K.one K.zero) (K.add (K.mul K.zero (K.neg K.one)) (K.add (K.mul K.zero K.zero) K.zero))))))))) = K.zero; lib_ring

theorem pq_5_3 : mmul (toSemi K) 9 (Pt K) (Qt K) 5 3 = idM K 5 3 := by
  show (K.add (K.mul K.zero K.one) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.one K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.one K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) K.zero))))))))) = K.zero; lib_ring

theorem pq_5_4 : mmul (toSemi K) 9 (Pt K) (Qt K) 5 4 = idM K 5 4 := by
  show (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.one) (K.add (K.mul K.one K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.one K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) K.zero))))))))) = K.zero; lib_ring

theorem pq_5_5 : mmul (toSemi K) 9 (Pt K) (Qt K) 5 5 = idM K 5 5 := by
  show (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.one K.one) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.one K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) K.zero))))))))) = K.one; lib_ring

theorem pq_5_6 : mmul (toSemi K) 9 (Pt K) (Qt K) 5 6 = idM K 5 6 := by
  show (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.one K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.one) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.one K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) K.zero))))))))) = K.zero; lib_ring

theorem pq_5_7 : mmul (toSemi K) 9 (Pt K) (Qt K) 5 7 = idM K 5 7 := by
  show (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.one K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.one) (K.add (K.mul K.one K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) K.zero))))))))) = K.zero; lib_ring

theorem pq_5_8 : mmul (toSemi K) 9 (Pt K) (Qt K) 5 8 = idM K 5 8 := by
  show (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.one K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.one K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.one) K.zero))))))))) = K.zero; lib_ring

theorem pq_6_0 : mmul (toSemi K) 9 (Pt K) (Qt K) 6 0 = idM K 6 0 := by
  show (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.one) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero (K.neg K.one)) (K.add (K.mul K.one K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) K.zero))))))))) = K.zero; lib_ring

theorem pq_6_1 : mmul (toSemi K) 9 (Pt K) (Qt K) 6 1 = idM K 6 1 := by
  show (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.one) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.one K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero (K.neg K.one)) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) K.zero))))))))) = K.zero; lib_ring

theorem pq_6_2 : mmul (toSemi K) 9 (Pt K) (Qt K) 6 2 = idM K 6 2 := by
  show (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.one K.zero) (K.add (K.mul K.zero K.one) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero (K.neg K.one)) (K.add (K.mul K.zero K.zero) K.zero))))))))) = K.zero; lib_ring

theorem pq_6_3 : mmul (toSemi K) 9 (Pt K) (Qt K) 6 3 = idM K 6 3 := by
  show (K.add (K.mul K.zero K.one) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.one K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) K.zero))))))))) = K.zero; lib_ring

theorem pq_6_4 : mmul (toSemi K) 9 (Pt K) (Qt K) 6 4 = idM K 6 4 := by
  show (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.one) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.one K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) K.zero))))))))) = K.zero; lib_ring

theorem pq_6_5 : mmul (toSemi K) 9 (Pt K) (Qt K) 6 5 = idM K 6 5 := by
  show (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.one) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.one K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) K.zero))))))))) = K.zero; lib_ring

theorem pq_6_6 : mmul (toSemi K) 9 (Pt K) (Qt K) 6 6 = idM K 6 6 := by
  show (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.one K.one) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) K.zero))))))))) = K.one; lib_ring

theorem pq_6_7 : mmul (toSemi K) 9 (Pt K) (Qt K) 6 7 = idM K 6 7 := by
  show (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.one K.zero) (K.add (K.mul K.zero K.one) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) K.zero))))))))) = K.zero; lib_ring

theorem pq_6_8 : mmul (toSemi K) 9 (Pt K) (Qt K) 6 8 = idM K 6 8 := by
  show (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.one K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.one) K.zero))))))))) = K.zero; lib_ring

theorem pq_7_0 : mmul (toSemi K) 9 (Pt K) (Qt K) 7 0 = idM K 7 0 := by
  show (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.one) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero (K.neg K.one)) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.one K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.one K.zero) (K.add (K.mul K.zero K.zero) K.zero))))))))) = K.zero; lib_ring

theorem pq_7_1 : mmul (toSemi K) 9 (Pt K) (Qt K) 7 1 = idM K 7 1 := by
  show (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.one) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.one K.zero) (K.add (K.mul K.zero (K.neg K.one)) (K.add (K.mul K.one K.zero) (K.add (K.mul K.zero K.zero) K.zero))))))))) = K.zero; lib_ring

theorem pq_7_2 : mmul (toSemi K) 9 (Pt K) (Qt K) 7 2 = idM K 7 2 := by
  show (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.one K.one) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.one (K.neg K.one)) (K.add (K.mul K.zero K.zero) K.zero))))))))) = K.zero; lib_ring

theorem pq_7_3 : mmul (toSemi K) 9 (Pt K) (Qt K) 7 3 = idM K 7 3 := by
  show (K.add (K.mul K.zero K.one) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.one K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.one K.zero) (K.add (K.mul K.zero K.zero) K.zero))))))))) = K.zero; lib_ring

theorem pq_7_4 : mmul (toSemi K) 9 (Pt K) (Qt K) 7 4 = idM K 7 4 := by
  show (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.one) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.one K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.one K.zero) (K.add (K.mul K.zero K.zero) K.zero))))))))) = K.zero; lib_ring

theorem pq_7_5 : mmul (toSemi K) 9 (Pt K) (Qt K) 7 5 = idM K 7 5 := by
  show (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.one) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.one K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.one K.zero) (K.add (K.mul K.zero K.zero) K.zero))))))))) = K.zero; lib_ring

theorem pq_7_6 : mmul (toSemi K) 9 (Pt K) (Qt K) 7 6 = idM K 7 6 := by
  show (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.one) (K.add (K.mul K.one K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.one K.zero) (K.add (K.mul K.zero K.zero) K.zero))))))))) = K.zero; lib_ring

theorem pq_7_7 : mmul (toSemi K) 9 (Pt K) (Qt K) 7 7 = idM K 7 7 := by
  show (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.one K.one) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.one K.zero) (K.add (K.mul K.zero K.zero) K.zero))))))))) = K.one; lib_ring

theorem pq_7_8 : mmul (toSemi K) 9 (Pt K) (Qt K) 7 8 = idM K 7 8 := by
  show (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.one K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.one K.zero) (K.add (K.mul K.zero K.one) K.zero))))))))) = K.zero; lib_ring

theorem pq_8_0 : mmul (toSemi K) 9 (Pt K) (Qt K) 8 0 = idM K 8 0 := by
  show (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.one) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero (K.neg K.one)) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.one K.zero) K.zero))))))))) = K.zero; lib_ring

theorem pq_8_1 : mmul (toSemi K) 9 (Pt K) (Qt K) 8 1 = idM K 8 1 := by
  show (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.one) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero (K.neg K.one)) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.one K.zero) K.zero))))))))) = K.zero; lib_ring

theorem pq_8_2 : mmul (toSemi K) 9 (Pt K) (Qt K) 8 2 = idM K 8 2 := by
  show (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.one) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero (K.neg K.one)) (K.add (K.mul K.one K.zero) K.zero))))))))) = K.zero; lib_ring

theorem pq_8_3 : mmul (toSemi K) 9 (Pt K) (Qt K) 8 3 = idM K 8 3 := by
  show (K.add (K.mul K.zero K.one) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.one K.zero) K.zero))))))))) = K.zero; lib_ring

theorem pq_8_4 : mmul (toSemi K) 9 (Pt K) (Qt K) 8 4 = idM K 8 4 := by
  show (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.one) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.one K.zero) K.zero))))))))) = K.zero; lib_ring

theorem pq_8_5 : mmul (toSemi K) 9 (Pt K) (Qt K) 8 5 = idM K 8 5 := by
  show (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.one) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.one K.zero) K.zero))))))))) = K.zero; lib_ring

theorem pq_8_6 : mmul (toSemi K) 9 (Pt K) (Qt K) 8 6 = idM K 8 6 := by
  show (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.one) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.one K.zero) K.zero))))))))) = K.zero; lib_ring

theorem pq_8_7 : mmul (toSemi K) 9 (Pt K) (Qt K) 8 7 = idM K 8 7 := by
  show (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.one) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.one K.zero) K.zero))))))))) = K.zero; lib_ring

theorem pq_8_8 : mmul (toSemi K) 9 (Pt K) (Qt K) 8 8 = idM K 8 8 := by
  show (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.one K.one) K.zero))))))))) = K.one; lib_ring

theorem pq (i j : Nat) (hi : i < 3 + 6) (hj : j < 3 + 6) : mmul (toSemi K) (3 + 6) (Pt K) (Qt K) i j = idM K i j := by
  rcases LibSym22.nine i hi with rfl | rfl | rfl | rfl | rfl | rfl | rfl | rfl | rfl <;> rcases LibSym22.nine j hj with rfl | rfl | rfl | rfl | rfl | rfl | rfl | rfl | rfl
  · exact pq_0_0 K
  · exact pq_0_1 K
  · exact pq_0_2 K
  · exact pq_0_3 K
  · exact pq_0_4 K
  · exact pq_0_5 K
  · exact pq_0_6 K
  · exact pq_0_7 K
  · exact pq_0_8 K
  · exact pq_1_0 K
  · exact pq_1_1 K
  · exact pq_1_2 K
  · exact pq_1_3 K
  · exact pq_1_4 K
  · exact pq_1_5 K
  · exact pq_1_6 K
  · exact pq_1_7 K
  · exact pq_1_8 K
  · exact pq_2_0 K
  · exact pq_2_1 K
  · exact pq_2_2 K
  · exact pq_2_3 K
  · exact pq_2_4 K
  · exact pq_2_5 K
  · exact pq_2_6 K
  · exact pq_2_7 K
  · exact pq_2_8 K
  · exact pq_3_0 K
  · exact pq_3_1 K
  · exact pq_3_2 K
  · exact pq_3_3 K
  · exact pq_3_4 K
  · exact pq_3_5 K
  · exact pq_3_6 K
  · exact pq_3_7 K
  · exact pq_3_8 K
  · exact pq_4_0 K
  · exact pq_4_1 K
  · exact pq_4_2 K
  · exact pq_4_3 K
  · exact pq_4_4 K
  · exact pq_4_5 K
  · exact pq_4_6 K
  · exact pq_4_7 K
  · exact pq_4_8 K
  · exact pq_5_0 K
  · exact pq_5_1 K
  · exact pq_5_2 K
  · exact pq_5_3 K
  · exact pq_5_4 K
  · exact pq_5_5 K
  · exact pq_5_6 K
  · exact pq_5_7 K
  · exact pq_5_8 K
  · exact pq_6_0 K
  · exact pq_6_1 K
  · exact pq_6_2 K
  · exact pq_6_3 K
  · exact pq_6_4 K
  · exact pq_6_5 K
  · exact pq_6_6 K
  · exact pq_6_7 K
  · exact pq_6_8 K
  · exact pq_7_0 K
  · exact pq_7_1 K
  · exact pq_7_2 K
  · exact pq_7_3 K
  · exact pq_7_4 K
  · exact pq_7_5 K
  · exact pq_7_6 K
  · exact pq_7_7 K
  · exact pq_7_8 K
  · exact pq_8_0 K
  · exact pq_8_1 K
  · exact pq_8_2 K
  · exact pq_8_3 K
  · exact pq_8_4 K
  · exact pq_8_5 K
  · exact pq_8_6 K
  · exact pq_8_7 K
  · exact pq_8_8 K

theorem z_0_0 : mmul (toSemi K) 9 (mmul (toSemi K) 9 (Pt K) (MM K M)) (Qt K) 3 0 = K.zero := by
  have h : mmul (toSemi K) 9 (mmul (toSemi K) 9 (Pt K) (MM K M)) (Qt K) 3 0 = (K.add (K.mul K.one (MM K M 0 1)) (K.mul (K.neg K.one) (MM K M 0 3))) := by
    show (K.add (K.mul (K.add (K.mul K.one (MM K M 0 0)) (K.add (K.mul K.zero (MM K M 1 0)) (K.add (K.mul K.zero (MM K M 2 0)) (K.add (K.mul K.zero (MM K M 3 0)) (K.add (K.mul K.zero (MM K M 4 0)) (K.add (K.mul K.zero (MM K M 5 0)) (K.add (K.mul K.zero (MM K M 6 0)) (K.add (K.mul K.zero (MM K M 7 0)) (K.add (K.mul K.zero (MM K M 8 0)) K.zero))))))))) K.zero) (K.add (K.mul (K.add (K.mul K.one (MM K  -- (+2802 chars; full line in the Zenodo source)
    lib_ring
  rw [h]
  show (K.add (K.mul K.one (K.mul (M 0 0) (M 0 1))) (K.mul (K.neg K.one) (K.mul (M 0 1) (M 0 0)))) = K.zero
  lib_ring

theorem z_0_1 : mmul (toSemi K) 9 (mmul (toSemi K) 9 (Pt K) (MM K M)) (Qt K) 3 1 = K.zero := by
  have h : mmul (toSemi K) 9 (mmul (toSemi K) 9 (Pt K) (MM K M)) (Qt K) 3 1 = (K.add (K.mul K.one (MM K M 0 2)) (K.mul (K.neg K.one) (MM K M 0 6))) := by
    show (K.add (K.mul (K.add (K.mul K.one (MM K M 0 0)) (K.add (K.mul K.zero (MM K M 1 0)) (K.add (K.mul K.zero (MM K M 2 0)) (K.add (K.mul K.zero (MM K M 3 0)) (K.add (K.mul K.zero (MM K M 4 0)) (K.add (K.mul K.zero (MM K M 5 0)) (K.add (K.mul K.zero (MM K M 6 0)) (K.add (K.mul K.zero (MM K M 7 0)) (K.add (K.mul K.zero (MM K M 8 0)) K.zero))))))))) K.zero) (K.add (K.mul (K.add (K.mul K.one (MM K  -- (+2802 chars; full line in the Zenodo source)
    lib_ring
  rw [h]
  show (K.add (K.mul K.one (K.mul (M 0 0) (M 0 2))) (K.mul (K.neg K.one) (K.mul (M 0 2) (M 0 0)))) = K.zero
  lib_ring

theorem z_0_2 : mmul (toSemi K) 9 (mmul (toSemi K) 9 (Pt K) (MM K M)) (Qt K) 3 2 = K.zero := by
  have h : mmul (toSemi K) 9 (mmul (toSemi K) 9 (Pt K) (MM K M)) (Qt K) 3 2 = (K.add (K.mul K.one (MM K M 0 5)) (K.mul (K.neg K.one) (MM K M 0 7))) := by
    show (K.add (K.mul (K.add (K.mul K.one (MM K M 0 0)) (K.add (K.mul K.zero (MM K M 1 0)) (K.add (K.mul K.zero (MM K M 2 0)) (K.add (K.mul K.zero (MM K M 3 0)) (K.add (K.mul K.zero (MM K M 4 0)) (K.add (K.mul K.zero (MM K M 5 0)) (K.add (K.mul K.zero (MM K M 6 0)) (K.add (K.mul K.zero (MM K M 7 0)) (K.add (K.mul K.zero (MM K M 8 0)) K.zero))))))))) K.zero) (K.add (K.mul (K.add (K.mul K.one (MM K  -- (+2802 chars; full line in the Zenodo source)
    lib_ring
  rw [h]
  show (K.add (K.mul K.one (K.mul (M 0 1) (M 0 2))) (K.mul (K.neg K.one) (K.mul (M 0 2) (M 0 1)))) = K.zero
  lib_ring

theorem z_1_0 : mmul (toSemi K) 9 (mmul (toSemi K) 9 (Pt K) (MM K M)) (Qt K) 4 0 = K.zero := by
  have h : mmul (toSemi K) 9 (mmul (toSemi K) 9 (Pt K) (MM K M)) (Qt K) 4 0 = (K.add (K.add (K.add (K.mul K.one (MM K M 1 1)) (K.mul (K.neg K.one) (MM K M 1 3))) (K.mul K.one (MM K M 3 1))) (K.mul (K.neg K.one) (MM K M 3 3))) := by
    show (K.add (K.mul (K.add (K.mul K.zero (MM K M 0 0)) (K.add (K.mul K.one (MM K M 1 0)) (K.add (K.mul K.zero (MM K M 2 0)) (K.add (K.mul K.one (MM K M 3 0)) (K.add (K.mul K.zero (MM K M 4 0)) (K.add (K.mul K.zero (MM K M 5 0)) (K.add (K.mul K.zero (MM K M 6 0)) (K.add (K.mul K.zero (MM K M 7 0)) (K.add (K.mul K.zero (MM K M 8 0)) K.zero))))))))) K.zero) (K.add (K.mul (K.add (K.mul K.zero (MM K  -- (+2793 chars; full line in the Zenodo source)
    lib_ring
  rw [h]
  show (K.add (K.add (K.add (K.mul K.one (K.mul (M 0 0) (M 1 1))) (K.mul (K.neg K.one) (K.mul (M 0 1) (M 1 0)))) (K.mul K.one (K.mul (M 1 0) (M 0 1)))) (K.mul (K.neg K.one) (K.mul (M 1 1) (M 0 0)))) = K.zero
  lib_ring

theorem z_1_1 : mmul (toSemi K) 9 (mmul (toSemi K) 9 (Pt K) (MM K M)) (Qt K) 4 1 = K.zero := by
  have h : mmul (toSemi K) 9 (mmul (toSemi K) 9 (Pt K) (MM K M)) (Qt K) 4 1 = (K.add (K.add (K.add (K.mul K.one (MM K M 1 2)) (K.mul (K.neg K.one) (MM K M 1 6))) (K.mul K.one (MM K M 3 2))) (K.mul (K.neg K.one) (MM K M 3 6))) := by
    show (K.add (K.mul (K.add (K.mul K.zero (MM K M 0 0)) (K.add (K.mul K.one (MM K M 1 0)) (K.add (K.mul K.zero (MM K M 2 0)) (K.add (K.mul K.one (MM K M 3 0)) (K.add (K.mul K.zero (MM K M 4 0)) (K.add (K.mul K.zero (MM K M 5 0)) (K.add (K.mul K.zero (MM K M 6 0)) (K.add (K.mul K.zero (MM K M 7 0)) (K.add (K.mul K.zero (MM K M 8 0)) K.zero))))))))) K.zero) (K.add (K.mul (K.add (K.mul K.zero (MM K  -- (+2793 chars; full line in the Zenodo source)
    lib_ring
  rw [h]
  show (K.add (K.add (K.add (K.mul K.one (K.mul (M 0 0) (M 1 2))) (K.mul (K.neg K.one) (K.mul (M 0 2) (M 1 0)))) (K.mul K.one (K.mul (M 1 0) (M 0 2)))) (K.mul (K.neg K.one) (K.mul (M 1 2) (M 0 0)))) = K.zero
  lib_ring

theorem z_1_2 : mmul (toSemi K) 9 (mmul (toSemi K) 9 (Pt K) (MM K M)) (Qt K) 4 2 = K.zero := by
  have h : mmul (toSemi K) 9 (mmul (toSemi K) 9 (Pt K) (MM K M)) (Qt K) 4 2 = (K.add (K.add (K.add (K.mul K.one (MM K M 1 5)) (K.mul (K.neg K.one) (MM K M 1 7))) (K.mul K.one (MM K M 3 5))) (K.mul (K.neg K.one) (MM K M 3 7))) := by
    show (K.add (K.mul (K.add (K.mul K.zero (MM K M 0 0)) (K.add (K.mul K.one (MM K M 1 0)) (K.add (K.mul K.zero (MM K M 2 0)) (K.add (K.mul K.one (MM K M 3 0)) (K.add (K.mul K.zero (MM K M 4 0)) (K.add (K.mul K.zero (MM K M 5 0)) (K.add (K.mul K.zero (MM K M 6 0)) (K.add (K.mul K.zero (MM K M 7 0)) (K.add (K.mul K.zero (MM K M 8 0)) K.zero))))))))) K.zero) (K.add (K.mul (K.add (K.mul K.zero (MM K  -- (+2793 chars; full line in the Zenodo source)
    lib_ring
  rw [h]
  show (K.add (K.add (K.add (K.mul K.one (K.mul (M 0 1) (M 1 2))) (K.mul (K.neg K.one) (K.mul (M 0 2) (M 1 1)))) (K.mul K.one (K.mul (M 1 1) (M 0 2)))) (K.mul (K.neg K.one) (K.mul (M 1 2) (M 0 1)))) = K.zero
  lib_ring

theorem z_2_0 : mmul (toSemi K) 9 (mmul (toSemi K) 9 (Pt K) (MM K M)) (Qt K) 5 0 = K.zero := by
  have h : mmul (toSemi K) 9 (mmul (toSemi K) 9 (Pt K) (MM K M)) (Qt K) 5 0 = (K.add (K.add (K.add (K.mul K.one (MM K M 2 1)) (K.mul (K.neg K.one) (MM K M 2 3))) (K.mul K.one (MM K M 6 1))) (K.mul (K.neg K.one) (MM K M 6 3))) := by
    show (K.add (K.mul (K.add (K.mul K.zero (MM K M 0 0)) (K.add (K.mul K.zero (MM K M 1 0)) (K.add (K.mul K.one (MM K M 2 0)) (K.add (K.mul K.zero (MM K M 3 0)) (K.add (K.mul K.zero (MM K M 4 0)) (K.add (K.mul K.zero (MM K M 5 0)) (K.add (K.mul K.one (MM K M 6 0)) (K.add (K.mul K.zero (MM K M 7 0)) (K.add (K.mul K.zero (MM K M 8 0)) K.zero))))))))) K.zero) (K.add (K.mul (K.add (K.mul K.zero (MM K  -- (+2793 chars; full line in the Zenodo source)
    lib_ring
  rw [h]
  show (K.add (K.add (K.add (K.mul K.one (K.mul (M 0 0) (M 2 1))) (K.mul (K.neg K.one) (K.mul (M 0 1) (M 2 0)))) (K.mul K.one (K.mul (M 2 0) (M 0 1)))) (K.mul (K.neg K.one) (K.mul (M 2 1) (M 0 0)))) = K.zero
  lib_ring

theorem z_2_1 : mmul (toSemi K) 9 (mmul (toSemi K) 9 (Pt K) (MM K M)) (Qt K) 5 1 = K.zero := by
  have h : mmul (toSemi K) 9 (mmul (toSemi K) 9 (Pt K) (MM K M)) (Qt K) 5 1 = (K.add (K.add (K.add (K.mul K.one (MM K M 2 2)) (K.mul (K.neg K.one) (MM K M 2 6))) (K.mul K.one (MM K M 6 2))) (K.mul (K.neg K.one) (MM K M 6 6))) := by
    show (K.add (K.mul (K.add (K.mul K.zero (MM K M 0 0)) (K.add (K.mul K.zero (MM K M 1 0)) (K.add (K.mul K.one (MM K M 2 0)) (K.add (K.mul K.zero (MM K M 3 0)) (K.add (K.mul K.zero (MM K M 4 0)) (K.add (K.mul K.zero (MM K M 5 0)) (K.add (K.mul K.one (MM K M 6 0)) (K.add (K.mul K.zero (MM K M 7 0)) (K.add (K.mul K.zero (MM K M 8 0)) K.zero))))))))) K.zero) (K.add (K.mul (K.add (K.mul K.zero (MM K  -- (+2793 chars; full line in the Zenodo source)
    lib_ring
  rw [h]
  show (K.add (K.add (K.add (K.mul K.one (K.mul (M 0 0) (M 2 2))) (K.mul (K.neg K.one) (K.mul (M 0 2) (M 2 0)))) (K.mul K.one (K.mul (M 2 0) (M 0 2)))) (K.mul (K.neg K.one) (K.mul (M 2 2) (M 0 0)))) = K.zero
  lib_ring

theorem z_2_2 : mmul (toSemi K) 9 (mmul (toSemi K) 9 (Pt K) (MM K M)) (Qt K) 5 2 = K.zero := by
  have h : mmul (toSemi K) 9 (mmul (toSemi K) 9 (Pt K) (MM K M)) (Qt K) 5 2 = (K.add (K.add (K.add (K.mul K.one (MM K M 2 5)) (K.mul (K.neg K.one) (MM K M 2 7))) (K.mul K.one (MM K M 6 5))) (K.mul (K.neg K.one) (MM K M 6 7))) := by
    show (K.add (K.mul (K.add (K.mul K.zero (MM K M 0 0)) (K.add (K.mul K.zero (MM K M 1 0)) (K.add (K.mul K.one (MM K M 2 0)) (K.add (K.mul K.zero (MM K M 3 0)) (K.add (K.mul K.zero (MM K M 4 0)) (K.add (K.mul K.zero (MM K M 5 0)) (K.add (K.mul K.one (MM K M 6 0)) (K.add (K.mul K.zero (MM K M 7 0)) (K.add (K.mul K.zero (MM K M 8 0)) K.zero))))))))) K.zero) (K.add (K.mul (K.add (K.mul K.zero (MM K  -- (+2793 chars; full line in the Zenodo source)
    lib_ring
  rw [h]
  show (K.add (K.add (K.add (K.mul K.one (K.mul (M 0 1) (M 2 2))) (K.mul (K.neg K.one) (K.mul (M 0 2) (M 2 1)))) (K.mul K.one (K.mul (M 2 1) (M 0 2)))) (K.mul (K.neg K.one) (K.mul (M 2 2) (M 0 1)))) = K.zero
  lib_ring

theorem z_3_0 : mmul (toSemi K) 9 (mmul (toSemi K) 9 (Pt K) (MM K M)) (Qt K) 6 0 = K.zero := by
  have h : mmul (toSemi K) 9 (mmul (toSemi K) 9 (Pt K) (MM K M)) (Qt K) 6 0 = (K.add (K.mul K.one (MM K M 4 1)) (K.mul (K.neg K.one) (MM K M 4 3))) := by
    show (K.add (K.mul (K.add (K.mul K.zero (MM K M 0 0)) (K.add (K.mul K.zero (MM K M 1 0)) (K.add (K.mul K.zero (MM K M 2 0)) (K.add (K.mul K.zero (MM K M 3 0)) (K.add (K.mul K.one (MM K M 4 0)) (K.add (K.mul K.zero (MM K M 5 0)) (K.add (K.mul K.zero (MM K M 6 0)) (K.add (K.mul K.zero (MM K M 7 0)) (K.add (K.mul K.zero (MM K M 8 0)) K.zero))))))))) K.zero) (K.add (K.mul (K.add (K.mul K.zero (MM   -- (+2802 chars; full line in the Zenodo source)
    lib_ring
  rw [h]
  show (K.add (K.mul K.one (K.mul (M 1 0) (M 1 1))) (K.mul (K.neg K.one) (K.mul (M 1 1) (M 1 0)))) = K.zero
  lib_ring

theorem z_3_1 : mmul (toSemi K) 9 (mmul (toSemi K) 9 (Pt K) (MM K M)) (Qt K) 6 1 = K.zero := by
  have h : mmul (toSemi K) 9 (mmul (toSemi K) 9 (Pt K) (MM K M)) (Qt K) 6 1 = (K.add (K.mul K.one (MM K M 4 2)) (K.mul (K.neg K.one) (MM K M 4 6))) := by
    show (K.add (K.mul (K.add (K.mul K.zero (MM K M 0 0)) (K.add (K.mul K.zero (MM K M 1 0)) (K.add (K.mul K.zero (MM K M 2 0)) (K.add (K.mul K.zero (MM K M 3 0)) (K.add (K.mul K.one (MM K M 4 0)) (K.add (K.mul K.zero (MM K M 5 0)) (K.add (K.mul K.zero (MM K M 6 0)) (K.add (K.mul K.zero (MM K M 7 0)) (K.add (K.mul K.zero (MM K M 8 0)) K.zero))))))))) K.zero) (K.add (K.mul (K.add (K.mul K.zero (MM   -- (+2802 chars; full line in the Zenodo source)
    lib_ring
  rw [h]
  show (K.add (K.mul K.one (K.mul (M 1 0) (M 1 2))) (K.mul (K.neg K.one) (K.mul (M 1 2) (M 1 0)))) = K.zero
  lib_ring

theorem z_3_2 : mmul (toSemi K) 9 (mmul (toSemi K) 9 (Pt K) (MM K M)) (Qt K) 6 2 = K.zero := by
  have h : mmul (toSemi K) 9 (mmul (toSemi K) 9 (Pt K) (MM K M)) (Qt K) 6 2 = (K.add (K.mul K.one (MM K M 4 5)) (K.mul (K.neg K.one) (MM K M 4 7))) := by
    show (K.add (K.mul (K.add (K.mul K.zero (MM K M 0 0)) (K.add (K.mul K.zero (MM K M 1 0)) (K.add (K.mul K.zero (MM K M 2 0)) (K.add (K.mul K.zero (MM K M 3 0)) (K.add (K.mul K.one (MM K M 4 0)) (K.add (K.mul K.zero (MM K M 5 0)) (K.add (K.mul K.zero (MM K M 6 0)) (K.add (K.mul K.zero (MM K M 7 0)) (K.add (K.mul K.zero (MM K M 8 0)) K.zero))))))))) K.zero) (K.add (K.mul (K.add (K.mul K.zero (MM   -- (+2802 chars; full line in the Zenodo source)
    lib_ring
  rw [h]
  show (K.add (K.mul K.one (K.mul (M 1 1) (M 1 2))) (K.mul (K.neg K.one) (K.mul (M 1 2) (M 1 1)))) = K.zero
  lib_ring

theorem z_4_0 : mmul (toSemi K) 9 (mmul (toSemi K) 9 (Pt K) (MM K M)) (Qt K) 7 0 = K.zero := by
  have h : mmul (toSemi K) 9 (mmul (toSemi K) 9 (Pt K) (MM K M)) (Qt K) 7 0 = (K.add (K.add (K.add (K.mul K.one (MM K M 5 1)) (K.mul (K.neg K.one) (MM K M 5 3))) (K.mul K.one (MM K M 7 1))) (K.mul (K.neg K.one) (MM K M 7 3))) := by
    show (K.add (K.mul (K.add (K.mul K.zero (MM K M 0 0)) (K.add (K.mul K.zero (MM K M 1 0)) (K.add (K.mul K.zero (MM K M 2 0)) (K.add (K.mul K.zero (MM K M 3 0)) (K.add (K.mul K.zero (MM K M 4 0)) (K.add (K.mul K.one (MM K M 5 0)) (K.add (K.mul K.zero (MM K M 6 0)) (K.add (K.mul K.one (MM K M 7 0)) (K.add (K.mul K.zero (MM K M 8 0)) K.zero))))))))) K.zero) (K.add (K.mul (K.add (K.mul K.zero (MM K  -- (+2793 chars; full line in the Zenodo source)
    lib_ring
  rw [h]
  show (K.add (K.add (K.add (K.mul K.one (K.mul (M 1 0) (M 2 1))) (K.mul (K.neg K.one) (K.mul (M 1 1) (M 2 0)))) (K.mul K.one (K.mul (M 2 0) (M 1 1)))) (K.mul (K.neg K.one) (K.mul (M 2 1) (M 1 0)))) = K.zero
  lib_ring

theorem z_4_1 : mmul (toSemi K) 9 (mmul (toSemi K) 9 (Pt K) (MM K M)) (Qt K) 7 1 = K.zero := by
  have h : mmul (toSemi K) 9 (mmul (toSemi K) 9 (Pt K) (MM K M)) (Qt K) 7 1 = (K.add (K.add (K.add (K.mul K.one (MM K M 5 2)) (K.mul (K.neg K.one) (MM K M 5 6))) (K.mul K.one (MM K M 7 2))) (K.mul (K.neg K.one) (MM K M 7 6))) := by
    show (K.add (K.mul (K.add (K.mul K.zero (MM K M 0 0)) (K.add (K.mul K.zero (MM K M 1 0)) (K.add (K.mul K.zero (MM K M 2 0)) (K.add (K.mul K.zero (MM K M 3 0)) (K.add (K.mul K.zero (MM K M 4 0)) (K.add (K.mul K.one (MM K M 5 0)) (K.add (K.mul K.zero (MM K M 6 0)) (K.add (K.mul K.one (MM K M 7 0)) (K.add (K.mul K.zero (MM K M 8 0)) K.zero))))))))) K.zero) (K.add (K.mul (K.add (K.mul K.zero (MM K  -- (+2793 chars; full line in the Zenodo source)
    lib_ring
  rw [h]
  show (K.add (K.add (K.add (K.mul K.one (K.mul (M 1 0) (M 2 2))) (K.mul (K.neg K.one) (K.mul (M 1 2) (M 2 0)))) (K.mul K.one (K.mul (M 2 0) (M 1 2)))) (K.mul (K.neg K.one) (K.mul (M 2 2) (M 1 0)))) = K.zero
  lib_ring

theorem z_4_2 : mmul (toSemi K) 9 (mmul (toSemi K) 9 (Pt K) (MM K M)) (Qt K) 7 2 = K.zero := by
  have h : mmul (toSemi K) 9 (mmul (toSemi K) 9 (Pt K) (MM K M)) (Qt K) 7 2 = (K.add (K.add (K.add (K.mul K.one (MM K M 5 5)) (K.mul (K.neg K.one) (MM K M 5 7))) (K.mul K.one (MM K M 7 5))) (K.mul (K.neg K.one) (MM K M 7 7))) := by
    show (K.add (K.mul (K.add (K.mul K.zero (MM K M 0 0)) (K.add (K.mul K.zero (MM K M 1 0)) (K.add (K.mul K.zero (MM K M 2 0)) (K.add (K.mul K.zero (MM K M 3 0)) (K.add (K.mul K.zero (MM K M 4 0)) (K.add (K.mul K.one (MM K M 5 0)) (K.add (K.mul K.zero (MM K M 6 0)) (K.add (K.mul K.one (MM K M 7 0)) (K.add (K.mul K.zero (MM K M 8 0)) K.zero))))))))) K.zero) (K.add (K.mul (K.add (K.mul K.zero (MM K  -- (+2793 chars; full line in the Zenodo source)
    lib_ring
  rw [h]
  show (K.add (K.add (K.add (K.mul K.one (K.mul (M 1 1) (M 2 2))) (K.mul (K.neg K.one) (K.mul (M 1 2) (M 2 1)))) (K.mul K.one (K.mul (M 2 1) (M 1 2)))) (K.mul (K.neg K.one) (K.mul (M 2 2) (M 1 1)))) = K.zero
  lib_ring

theorem z_5_0 : mmul (toSemi K) 9 (mmul (toSemi K) 9 (Pt K) (MM K M)) (Qt K) 8 0 = K.zero := by
  have h : mmul (toSemi K) 9 (mmul (toSemi K) 9 (Pt K) (MM K M)) (Qt K) 8 0 = (K.add (K.mul K.one (MM K M 8 1)) (K.mul (K.neg K.one) (MM K M 8 3))) := by
    show (K.add (K.mul (K.add (K.mul K.zero (MM K M 0 0)) (K.add (K.mul K.zero (MM K M 1 0)) (K.add (K.mul K.zero (MM K M 2 0)) (K.add (K.mul K.zero (MM K M 3 0)) (K.add (K.mul K.zero (MM K M 4 0)) (K.add (K.mul K.zero (MM K M 5 0)) (K.add (K.mul K.zero (MM K M 6 0)) (K.add (K.mul K.zero (MM K M 7 0)) (K.add (K.mul K.one (MM K M 8 0)) K.zero))))))))) K.zero) (K.add (K.mul (K.add (K.mul K.zero (MM   -- (+2802 chars; full line in the Zenodo source)
    lib_ring
  rw [h]
  show (K.add (K.mul K.one (K.mul (M 2 0) (M 2 1))) (K.mul (K.neg K.one) (K.mul (M 2 1) (M 2 0)))) = K.zero
  lib_ring

theorem z_5_1 : mmul (toSemi K) 9 (mmul (toSemi K) 9 (Pt K) (MM K M)) (Qt K) 8 1 = K.zero := by
  have h : mmul (toSemi K) 9 (mmul (toSemi K) 9 (Pt K) (MM K M)) (Qt K) 8 1 = (K.add (K.mul K.one (MM K M 8 2)) (K.mul (K.neg K.one) (MM K M 8 6))) := by
    show (K.add (K.mul (K.add (K.mul K.zero (MM K M 0 0)) (K.add (K.mul K.zero (MM K M 1 0)) (K.add (K.mul K.zero (MM K M 2 0)) (K.add (K.mul K.zero (MM K M 3 0)) (K.add (K.mul K.zero (MM K M 4 0)) (K.add (K.mul K.zero (MM K M 5 0)) (K.add (K.mul K.zero (MM K M 6 0)) (K.add (K.mul K.zero (MM K M 7 0)) (K.add (K.mul K.one (MM K M 8 0)) K.zero))))))))) K.zero) (K.add (K.mul (K.add (K.mul K.zero (MM   -- (+2802 chars; full line in the Zenodo source)
    lib_ring
  rw [h]
  show (K.add (K.mul K.one (K.mul (M 2 0) (M 2 2))) (K.mul (K.neg K.one) (K.mul (M 2 2) (M 2 0)))) = K.zero
  lib_ring

theorem z_5_2 : mmul (toSemi K) 9 (mmul (toSemi K) 9 (Pt K) (MM K M)) (Qt K) 8 2 = K.zero := by
  have h : mmul (toSemi K) 9 (mmul (toSemi K) 9 (Pt K) (MM K M)) (Qt K) 8 2 = (K.add (K.mul K.one (MM K M 8 5)) (K.mul (K.neg K.one) (MM K M 8 7))) := by
    show (K.add (K.mul (K.add (K.mul K.zero (MM K M 0 0)) (K.add (K.mul K.zero (MM K M 1 0)) (K.add (K.mul K.zero (MM K M 2 0)) (K.add (K.mul K.zero (MM K M 3 0)) (K.add (K.mul K.zero (MM K M 4 0)) (K.add (K.mul K.zero (MM K M 5 0)) (K.add (K.mul K.zero (MM K M 6 0)) (K.add (K.mul K.zero (MM K M 7 0)) (K.add (K.mul K.one (MM K M 8 0)) K.zero))))))))) K.zero) (K.add (K.mul (K.add (K.mul K.zero (MM   -- (+2802 chars; full line in the Zenodo source)
    lib_ring
  rw [h]
  show (K.add (K.mul K.one (K.mul (M 2 1) (M 2 2))) (K.mul (K.neg K.one) (K.mul (M 2 2) (M 2 1)))) = K.zero
  lib_ring

theorem x_0_0 : mmul (toSemi K) 9 (mmul (toSemi K) 9 (Pt K) (MM K M)) (Qt K) 0 0 = LibExt2Three.ext2 K M 0 0 := by
  have h : mmul (toSemi K) 9 (mmul (toSemi K) 9 (Pt K) (MM K M)) (Qt K) 0 0 = (K.add (K.mul (K.neg K.one) (MM K M 3 1)) (K.mul K.one (MM K M 3 3))) := by
    show (K.add (K.mul (K.add (K.mul K.zero (MM K M 0 0)) (K.add (K.mul K.zero (MM K M 1 0)) (K.add (K.mul K.zero (MM K M 2 0)) (K.add (K.mul (K.neg K.one) (MM K M 3 0)) (K.add (K.mul K.zero (MM K M 4 0)) (K.add (K.mul K.zero (MM K M 5 0)) (K.add (K.mul K.zero (MM K M 6 0)) (K.add (K.mul K.zero (MM K M 7 0)) (K.add (K.mul K.zero (MM K M 8 0)) K.zero))))))))) K.zero) (K.add (K.mul (K.add (K.mul K.z  -- (+2874 chars; full line in the Zenodo source)
    lib_ring
  rw [h]
  show (K.add (K.mul (K.neg K.one) (K.mul (M 1 0) (M 0 1))) (K.mul K.one (K.mul (M 1 1) (M 0 0)))) = (K.add (K.mul (M 0 0) (M 1 1)) (K.neg (K.mul (M 0 1) (M 1 0))))
  lib_ring

theorem x_0_1 : mmul (toSemi K) 9 (mmul (toSemi K) 9 (Pt K) (MM K M)) (Qt K) 0 1 = LibExt2Three.ext2 K M 0 1 := by
  have h : mmul (toSemi K) 9 (mmul (toSemi K) 9 (Pt K) (MM K M)) (Qt K) 0 1 = (K.add (K.mul (K.neg K.one) (MM K M 3 2)) (K.mul K.one (MM K M 3 6))) := by
    show (K.add (K.mul (K.add (K.mul K.zero (MM K M 0 0)) (K.add (K.mul K.zero (MM K M 1 0)) (K.add (K.mul K.zero (MM K M 2 0)) (K.add (K.mul (K.neg K.one) (MM K M 3 0)) (K.add (K.mul K.zero (MM K M 4 0)) (K.add (K.mul K.zero (MM K M 5 0)) (K.add (K.mul K.zero (MM K M 6 0)) (K.add (K.mul K.zero (MM K M 7 0)) (K.add (K.mul K.zero (MM K M 8 0)) K.zero))))))))) K.zero) (K.add (K.mul (K.add (K.mul K.z  -- (+2874 chars; full line in the Zenodo source)
    lib_ring
  rw [h]
  show (K.add (K.mul (K.neg K.one) (K.mul (M 1 0) (M 0 2))) (K.mul K.one (K.mul (M 1 2) (M 0 0)))) = (K.add (K.mul (M 0 0) (M 1 2)) (K.neg (K.mul (M 0 2) (M 1 0))))
  lib_ring

theorem x_0_2 : mmul (toSemi K) 9 (mmul (toSemi K) 9 (Pt K) (MM K M)) (Qt K) 0 2 = LibExt2Three.ext2 K M 0 2 := by
  have h : mmul (toSemi K) 9 (mmul (toSemi K) 9 (Pt K) (MM K M)) (Qt K) 0 2 = (K.add (K.mul (K.neg K.one) (MM K M 3 5)) (K.mul K.one (MM K M 3 7))) := by
    show (K.add (K.mul (K.add (K.mul K.zero (MM K M 0 0)) (K.add (K.mul K.zero (MM K M 1 0)) (K.add (K.mul K.zero (MM K M 2 0)) (K.add (K.mul (K.neg K.one) (MM K M 3 0)) (K.add (K.mul K.zero (MM K M 4 0)) (K.add (K.mul K.zero (MM K M 5 0)) (K.add (K.mul K.zero (MM K M 6 0)) (K.add (K.mul K.zero (MM K M 7 0)) (K.add (K.mul K.zero (MM K M 8 0)) K.zero))))))))) K.zero) (K.add (K.mul (K.add (K.mul K.z  -- (+2874 chars; full line in the Zenodo source)
    lib_ring
  rw [h]
  show (K.add (K.mul (K.neg K.one) (K.mul (M 1 1) (M 0 2))) (K.mul K.one (K.mul (M 1 2) (M 0 1)))) = (K.add (K.mul (M 0 1) (M 1 2)) (K.neg (K.mul (M 0 2) (M 1 1))))
  lib_ring

theorem x_1_0 : mmul (toSemi K) 9 (mmul (toSemi K) 9 (Pt K) (MM K M)) (Qt K) 1 0 = LibExt2Three.ext2 K M 1 0 := by
  have h : mmul (toSemi K) 9 (mmul (toSemi K) 9 (Pt K) (MM K M)) (Qt K) 1 0 = (K.add (K.mul (K.neg K.one) (MM K M 6 1)) (K.mul K.one (MM K M 6 3))) := by
    show (K.add (K.mul (K.add (K.mul K.zero (MM K M 0 0)) (K.add (K.mul K.zero (MM K M 1 0)) (K.add (K.mul K.zero (MM K M 2 0)) (K.add (K.mul K.zero (MM K M 3 0)) (K.add (K.mul K.zero (MM K M 4 0)) (K.add (K.mul K.zero (MM K M 5 0)) (K.add (K.mul (K.neg K.one) (MM K M 6 0)) (K.add (K.mul K.zero (MM K M 7 0)) (K.add (K.mul K.zero (MM K M 8 0)) K.zero))))))))) K.zero) (K.add (K.mul (K.add (K.mul K.z  -- (+2874 chars; full line in the Zenodo source)
    lib_ring
  rw [h]
  show (K.add (K.mul (K.neg K.one) (K.mul (M 2 0) (M 0 1))) (K.mul K.one (K.mul (M 2 1) (M 0 0)))) = (K.add (K.mul (M 0 0) (M 2 1)) (K.neg (K.mul (M 0 1) (M 2 0))))
  lib_ring

theorem x_1_1 : mmul (toSemi K) 9 (mmul (toSemi K) 9 (Pt K) (MM K M)) (Qt K) 1 1 = LibExt2Three.ext2 K M 1 1 := by
  have h : mmul (toSemi K) 9 (mmul (toSemi K) 9 (Pt K) (MM K M)) (Qt K) 1 1 = (K.add (K.mul (K.neg K.one) (MM K M 6 2)) (K.mul K.one (MM K M 6 6))) := by
    show (K.add (K.mul (K.add (K.mul K.zero (MM K M 0 0)) (K.add (K.mul K.zero (MM K M 1 0)) (K.add (K.mul K.zero (MM K M 2 0)) (K.add (K.mul K.zero (MM K M 3 0)) (K.add (K.mul K.zero (MM K M 4 0)) (K.add (K.mul K.zero (MM K M 5 0)) (K.add (K.mul (K.neg K.one) (MM K M 6 0)) (K.add (K.mul K.zero (MM K M 7 0)) (K.add (K.mul K.zero (MM K M 8 0)) K.zero))))))))) K.zero) (K.add (K.mul (K.add (K.mul K.z  -- (+2874 chars; full line in the Zenodo source)
    lib_ring
  rw [h]
  show (K.add (K.mul (K.neg K.one) (K.mul (M 2 0) (M 0 2))) (K.mul K.one (K.mul (M 2 2) (M 0 0)))) = (K.add (K.mul (M 0 0) (M 2 2)) (K.neg (K.mul (M 0 2) (M 2 0))))
  lib_ring

theorem x_1_2 : mmul (toSemi K) 9 (mmul (toSemi K) 9 (Pt K) (MM K M)) (Qt K) 1 2 = LibExt2Three.ext2 K M 1 2 := by
  have h : mmul (toSemi K) 9 (mmul (toSemi K) 9 (Pt K) (MM K M)) (Qt K) 1 2 = (K.add (K.mul (K.neg K.one) (MM K M 6 5)) (K.mul K.one (MM K M 6 7))) := by
    show (K.add (K.mul (K.add (K.mul K.zero (MM K M 0 0)) (K.add (K.mul K.zero (MM K M 1 0)) (K.add (K.mul K.zero (MM K M 2 0)) (K.add (K.mul K.zero (MM K M 3 0)) (K.add (K.mul K.zero (MM K M 4 0)) (K.add (K.mul K.zero (MM K M 5 0)) (K.add (K.mul (K.neg K.one) (MM K M 6 0)) (K.add (K.mul K.zero (MM K M 7 0)) (K.add (K.mul K.zero (MM K M 8 0)) K.zero))))))))) K.zero) (K.add (K.mul (K.add (K.mul K.z  -- (+2874 chars; full line in the Zenodo source)
    lib_ring
  rw [h]
  show (K.add (K.mul (K.neg K.one) (K.mul (M 2 1) (M 0 2))) (K.mul K.one (K.mul (M 2 2) (M 0 1)))) = (K.add (K.mul (M 0 1) (M 2 2)) (K.neg (K.mul (M 0 2) (M 2 1))))
  lib_ring

theorem x_2_0 : mmul (toSemi K) 9 (mmul (toSemi K) 9 (Pt K) (MM K M)) (Qt K) 2 0 = LibExt2Three.ext2 K M 2 0 := by
  have h : mmul (toSemi K) 9 (mmul (toSemi K) 9 (Pt K) (MM K M)) (Qt K) 2 0 = (K.add (K.mul (K.neg K.one) (MM K M 7 1)) (K.mul K.one (MM K M 7 3))) := by
    show (K.add (K.mul (K.add (K.mul K.zero (MM K M 0 0)) (K.add (K.mul K.zero (MM K M 1 0)) (K.add (K.mul K.zero (MM K M 2 0)) (K.add (K.mul K.zero (MM K M 3 0)) (K.add (K.mul K.zero (MM K M 4 0)) (K.add (K.mul K.zero (MM K M 5 0)) (K.add (K.mul K.zero (MM K M 6 0)) (K.add (K.mul (K.neg K.one) (MM K M 7 0)) (K.add (K.mul K.zero (MM K M 8 0)) K.zero))))))))) K.zero) (K.add (K.mul (K.add (K.mul K.z  -- (+2874 chars; full line in the Zenodo source)
    lib_ring
  rw [h]
  show (K.add (K.mul (K.neg K.one) (K.mul (M 2 0) (M 1 1))) (K.mul K.one (K.mul (M 2 1) (M 1 0)))) = (K.add (K.mul (M 1 0) (M 2 1)) (K.neg (K.mul (M 1 1) (M 2 0))))
  lib_ring

theorem x_2_1 : mmul (toSemi K) 9 (mmul (toSemi K) 9 (Pt K) (MM K M)) (Qt K) 2 1 = LibExt2Three.ext2 K M 2 1 := by
  have h : mmul (toSemi K) 9 (mmul (toSemi K) 9 (Pt K) (MM K M)) (Qt K) 2 1 = (K.add (K.mul (K.neg K.one) (MM K M 7 2)) (K.mul K.one (MM K M 7 6))) := by
    show (K.add (K.mul (K.add (K.mul K.zero (MM K M 0 0)) (K.add (K.mul K.zero (MM K M 1 0)) (K.add (K.mul K.zero (MM K M 2 0)) (K.add (K.mul K.zero (MM K M 3 0)) (K.add (K.mul K.zero (MM K M 4 0)) (K.add (K.mul K.zero (MM K M 5 0)) (K.add (K.mul K.zero (MM K M 6 0)) (K.add (K.mul (K.neg K.one) (MM K M 7 0)) (K.add (K.mul K.zero (MM K M 8 0)) K.zero))))))))) K.zero) (K.add (K.mul (K.add (K.mul K.z  -- (+2874 chars; full line in the Zenodo source)
    lib_ring
  rw [h]
  show (K.add (K.mul (K.neg K.one) (K.mul (M 2 0) (M 1 2))) (K.mul K.one (K.mul (M 2 2) (M 1 0)))) = (K.add (K.mul (M 1 0) (M 2 2)) (K.neg (K.mul (M 1 2) (M 2 0))))
  lib_ring

theorem x_2_2 : mmul (toSemi K) 9 (mmul (toSemi K) 9 (Pt K) (MM K M)) (Qt K) 2 2 = LibExt2Three.ext2 K M 2 2 := by
  have h : mmul (toSemi K) 9 (mmul (toSemi K) 9 (Pt K) (MM K M)) (Qt K) 2 2 = (K.add (K.mul (K.neg K.one) (MM K M 7 5)) (K.mul K.one (MM K M 7 7))) := by
    show (K.add (K.mul (K.add (K.mul K.zero (MM K M 0 0)) (K.add (K.mul K.zero (MM K M 1 0)) (K.add (K.mul K.zero (MM K M 2 0)) (K.add (K.mul K.zero (MM K M 3 0)) (K.add (K.mul K.zero (MM K M 4 0)) (K.add (K.mul K.zero (MM K M 5 0)) (K.add (K.mul K.zero (MM K M 6 0)) (K.add (K.mul (K.neg K.one) (MM K M 7 0)) (K.add (K.mul K.zero (MM K M 8 0)) K.zero))))))))) K.zero) (K.add (K.mul (K.add (K.mul K.z  -- (+2874 chars; full line in the Zenodo source)
    lib_ring
  rw [h]
  show (K.add (K.mul (K.neg K.one) (K.mul (M 2 1) (M 1 2))) (K.mul K.one (K.mul (M 2 2) (M 1 1)))) = (K.add (K.mul (M 1 1) (M 2 2)) (K.neg (K.mul (M 1 2) (M 2 1))))
  lib_ring

theorem y_0_0 : mmul (toSemi K) 9 (mmul (toSemi K) 9 (Pt K) (MM K M)) (Qt K) 3 3 = LibSym2Three.sym2 K M 0 0 := by
  have h : mmul (toSemi K) 9 (mmul (toSemi K) 9 (Pt K) (MM K M)) (Qt K) 3 3 = (K.mul K.one (MM K M 0 0)) := by
    show (K.add (K.mul (K.add (K.mul K.one (MM K M 0 0)) (K.add (K.mul K.zero (MM K M 1 0)) (K.add (K.mul K.zero (MM K M 2 0)) (K.add (K.mul K.zero (MM K M 3 0)) (K.add (K.mul K.zero (MM K M 4 0)) (K.add (K.mul K.zero (MM K M 5 0)) (K.add (K.mul K.zero (MM K M 6 0)) (K.add (K.mul K.zero (MM K M 7 0)) (K.add (K.mul K.zero (MM K M 8 0)) K.zero))))))))) K.one) (K.add (K.mul (K.add (K.mul K.one (MM K   -- (+2795 chars; full line in the Zenodo source)
    lib_ring
  rw [h]
  show (K.mul K.one (K.mul (M 0 0) (M 0 0))) = (K.mul (M 0 0) (M 0 0))
  lib_ring

theorem y_0_1 : mmul (toSemi K) 9 (mmul (toSemi K) 9 (Pt K) (MM K M)) (Qt K) 3 4 = LibSym2Three.sym2 K M 0 1 := by
  have h : mmul (toSemi K) 9 (mmul (toSemi K) 9 (Pt K) (MM K M)) (Qt K) 3 4 = (K.mul K.one (MM K M 0 1)) := by
    show (K.add (K.mul (K.add (K.mul K.one (MM K M 0 0)) (K.add (K.mul K.zero (MM K M 1 0)) (K.add (K.mul K.zero (MM K M 2 0)) (K.add (K.mul K.zero (MM K M 3 0)) (K.add (K.mul K.zero (MM K M 4 0)) (K.add (K.mul K.zero (MM K M 5 0)) (K.add (K.mul K.zero (MM K M 6 0)) (K.add (K.mul K.zero (MM K M 7 0)) (K.add (K.mul K.zero (MM K M 8 0)) K.zero))))))))) K.zero) (K.add (K.mul (K.add (K.mul K.one (MM K  -- (+2795 chars; full line in the Zenodo source)
    lib_ring
  rw [h]
  show (K.mul K.one (K.mul (M 0 0) (M 0 1))) = (K.mul (M 0 0) (M 0 1))
  lib_ring

theorem y_0_2 : mmul (toSemi K) 9 (mmul (toSemi K) 9 (Pt K) (MM K M)) (Qt K) 3 5 = LibSym2Three.sym2 K M 0 2 := by
  have h : mmul (toSemi K) 9 (mmul (toSemi K) 9 (Pt K) (MM K M)) (Qt K) 3 5 = (K.mul K.one (MM K M 0 2)) := by
    show (K.add (K.mul (K.add (K.mul K.one (MM K M 0 0)) (K.add (K.mul K.zero (MM K M 1 0)) (K.add (K.mul K.zero (MM K M 2 0)) (K.add (K.mul K.zero (MM K M 3 0)) (K.add (K.mul K.zero (MM K M 4 0)) (K.add (K.mul K.zero (MM K M 5 0)) (K.add (K.mul K.zero (MM K M 6 0)) (K.add (K.mul K.zero (MM K M 7 0)) (K.add (K.mul K.zero (MM K M 8 0)) K.zero))))))))) K.zero) (K.add (K.mul (K.add (K.mul K.one (MM K  -- (+2795 chars; full line in the Zenodo source)
    lib_ring
  rw [h]
  show (K.mul K.one (K.mul (M 0 0) (M 0 2))) = (K.mul (M 0 0) (M 0 2))
  lib_ring

theorem y_0_3 : mmul (toSemi K) 9 (mmul (toSemi K) 9 (Pt K) (MM K M)) (Qt K) 3 6 = LibSym2Three.sym2 K M 0 3 := by
  have h : mmul (toSemi K) 9 (mmul (toSemi K) 9 (Pt K) (MM K M)) (Qt K) 3 6 = (K.mul K.one (MM K M 0 4)) := by
    show (K.add (K.mul (K.add (K.mul K.one (MM K M 0 0)) (K.add (K.mul K.zero (MM K M 1 0)) (K.add (K.mul K.zero (MM K M 2 0)) (K.add (K.mul K.zero (MM K M 3 0)) (K.add (K.mul K.zero (MM K M 4 0)) (K.add (K.mul K.zero (MM K M 5 0)) (K.add (K.mul K.zero (MM K M 6 0)) (K.add (K.mul K.zero (MM K M 7 0)) (K.add (K.mul K.zero (MM K M 8 0)) K.zero))))))))) K.zero) (K.add (K.mul (K.add (K.mul K.one (MM K  -- (+2795 chars; full line in the Zenodo source)
    lib_ring
  rw [h]
  show (K.mul K.one (K.mul (M 0 1) (M 0 1))) = (K.mul (M 0 1) (M 0 1))
  lib_ring

theorem y_0_4 : mmul (toSemi K) 9 (mmul (toSemi K) 9 (Pt K) (MM K M)) (Qt K) 3 7 = LibSym2Three.sym2 K M 0 4 := by
  have h : mmul (toSemi K) 9 (mmul (toSemi K) 9 (Pt K) (MM K M)) (Qt K) 3 7 = (K.mul K.one (MM K M 0 5)) := by
    show (K.add (K.mul (K.add (K.mul K.one (MM K M 0 0)) (K.add (K.mul K.zero (MM K M 1 0)) (K.add (K.mul K.zero (MM K M 2 0)) (K.add (K.mul K.zero (MM K M 3 0)) (K.add (K.mul K.zero (MM K M 4 0)) (K.add (K.mul K.zero (MM K M 5 0)) (K.add (K.mul K.zero (MM K M 6 0)) (K.add (K.mul K.zero (MM K M 7 0)) (K.add (K.mul K.zero (MM K M 8 0)) K.zero))))))))) K.zero) (K.add (K.mul (K.add (K.mul K.one (MM K  -- (+2795 chars; full line in the Zenodo source)
    lib_ring
  rw [h]
  show (K.mul K.one (K.mul (M 0 1) (M 0 2))) = (K.mul (M 0 1) (M 0 2))
  lib_ring

theorem y_0_5 : mmul (toSemi K) 9 (mmul (toSemi K) 9 (Pt K) (MM K M)) (Qt K) 3 8 = LibSym2Three.sym2 K M 0 5 := by
  have h : mmul (toSemi K) 9 (mmul (toSemi K) 9 (Pt K) (MM K M)) (Qt K) 3 8 = (K.mul K.one (MM K M 0 8)) := by
    show (K.add (K.mul (K.add (K.mul K.one (MM K M 0 0)) (K.add (K.mul K.zero (MM K M 1 0)) (K.add (K.mul K.zero (MM K M 2 0)) (K.add (K.mul K.zero (MM K M 3 0)) (K.add (K.mul K.zero (MM K M 4 0)) (K.add (K.mul K.zero (MM K M 5 0)) (K.add (K.mul K.zero (MM K M 6 0)) (K.add (K.mul K.zero (MM K M 7 0)) (K.add (K.mul K.zero (MM K M 8 0)) K.zero))))))))) K.zero) (K.add (K.mul (K.add (K.mul K.one (MM K  -- (+2795 chars; full line in the Zenodo source)
    lib_ring
  rw [h]
  show (K.mul K.one (K.mul (M 0 2) (M 0 2))) = (K.mul (M 0 2) (M 0 2))
  lib_ring

theorem y_1_0 : mmul (toSemi K) 9 (mmul (toSemi K) 9 (Pt K) (MM K M)) (Qt K) 4 3 = LibSym2Three.sym2 K M 1 0 := by
  have h : mmul (toSemi K) 9 (mmul (toSemi K) 9 (Pt K) (MM K M)) (Qt K) 4 3 = (K.add (K.mul K.one (MM K M 1 0)) (K.mul K.one (MM K M 3 0))) := by
    show (K.add (K.mul (K.add (K.mul K.zero (MM K M 0 0)) (K.add (K.mul K.one (MM K M 1 0)) (K.add (K.mul K.zero (MM K M 2 0)) (K.add (K.mul K.one (MM K M 3 0)) (K.add (K.mul K.zero (MM K M 4 0)) (K.add (K.mul K.zero (MM K M 5 0)) (K.add (K.mul K.zero (MM K M 6 0)) (K.add (K.mul K.zero (MM K M 7 0)) (K.add (K.mul K.zero (MM K M 8 0)) K.zero))))))))) K.one) (K.add (K.mul (K.add (K.mul K.zero (MM K   -- (+2786 chars; full line in the Zenodo source)
    lib_ring
  rw [h]
  show (K.add (K.mul K.one (K.mul (M 0 0) (M 1 0))) (K.mul K.one (K.mul (M 1 0) (M 0 0)))) = (K.add (K.mul (M 0 0) (M 1 0)) (K.mul (M 1 0) (M 0 0)))
  lib_ring

theorem y_1_1 : mmul (toSemi K) 9 (mmul (toSemi K) 9 (Pt K) (MM K M)) (Qt K) 4 4 = LibSym2Three.sym2 K M 1 1 := by
  have h : mmul (toSemi K) 9 (mmul (toSemi K) 9 (Pt K) (MM K M)) (Qt K) 4 4 = (K.add (K.mul K.one (MM K M 1 1)) (K.mul K.one (MM K M 3 1))) := by
    show (K.add (K.mul (K.add (K.mul K.zero (MM K M 0 0)) (K.add (K.mul K.one (MM K M 1 0)) (K.add (K.mul K.zero (MM K M 2 0)) (K.add (K.mul K.one (MM K M 3 0)) (K.add (K.mul K.zero (MM K M 4 0)) (K.add (K.mul K.zero (MM K M 5 0)) (K.add (K.mul K.zero (MM K M 6 0)) (K.add (K.mul K.zero (MM K M 7 0)) (K.add (K.mul K.zero (MM K M 8 0)) K.zero))))))))) K.zero) (K.add (K.mul (K.add (K.mul K.zero (MM K  -- (+2786 chars; full line in the Zenodo source)
    lib_ring
  rw [h]
  show (K.add (K.mul K.one (K.mul (M 0 0) (M 1 1))) (K.mul K.one (K.mul (M 1 0) (M 0 1)))) = (K.add (K.mul (M 0 0) (M 1 1)) (K.mul (M 1 0) (M 0 1)))
  lib_ring

theorem y_1_2 : mmul (toSemi K) 9 (mmul (toSemi K) 9 (Pt K) (MM K M)) (Qt K) 4 5 = LibSym2Three.sym2 K M 1 2 := by
  have h : mmul (toSemi K) 9 (mmul (toSemi K) 9 (Pt K) (MM K M)) (Qt K) 4 5 = (K.add (K.mul K.one (MM K M 1 2)) (K.mul K.one (MM K M 3 2))) := by
    show (K.add (K.mul (K.add (K.mul K.zero (MM K M 0 0)) (K.add (K.mul K.one (MM K M 1 0)) (K.add (K.mul K.zero (MM K M 2 0)) (K.add (K.mul K.one (MM K M 3 0)) (K.add (K.mul K.zero (MM K M 4 0)) (K.add (K.mul K.zero (MM K M 5 0)) (K.add (K.mul K.zero (MM K M 6 0)) (K.add (K.mul K.zero (MM K M 7 0)) (K.add (K.mul K.zero (MM K M 8 0)) K.zero))))))))) K.zero) (K.add (K.mul (K.add (K.mul K.zero (MM K  -- (+2786 chars; full line in the Zenodo source)
    lib_ring
  rw [h]
  show (K.add (K.mul K.one (K.mul (M 0 0) (M 1 2))) (K.mul K.one (K.mul (M 1 0) (M 0 2)))) = (K.add (K.mul (M 0 0) (M 1 2)) (K.mul (M 1 0) (M 0 2)))
  lib_ring

theorem y_1_3 : mmul (toSemi K) 9 (mmul (toSemi K) 9 (Pt K) (MM K M)) (Qt K) 4 6 = LibSym2Three.sym2 K M 1 3 := by
  have h : mmul (toSemi K) 9 (mmul (toSemi K) 9 (Pt K) (MM K M)) (Qt K) 4 6 = (K.add (K.mul K.one (MM K M 1 4)) (K.mul K.one (MM K M 3 4))) := by
    show (K.add (K.mul (K.add (K.mul K.zero (MM K M 0 0)) (K.add (K.mul K.one (MM K M 1 0)) (K.add (K.mul K.zero (MM K M 2 0)) (K.add (K.mul K.one (MM K M 3 0)) (K.add (K.mul K.zero (MM K M 4 0)) (K.add (K.mul K.zero (MM K M 5 0)) (K.add (K.mul K.zero (MM K M 6 0)) (K.add (K.mul K.zero (MM K M 7 0)) (K.add (K.mul K.zero (MM K M 8 0)) K.zero))))))))) K.zero) (K.add (K.mul (K.add (K.mul K.zero (MM K  -- (+2786 chars; full line in the Zenodo source)
    lib_ring
  rw [h]
  show (K.add (K.mul K.one (K.mul (M 0 1) (M 1 1))) (K.mul K.one (K.mul (M 1 1) (M 0 1)))) = (K.add (K.mul (M 0 1) (M 1 1)) (K.mul (M 1 1) (M 0 1)))
  lib_ring

theorem y_1_4 : mmul (toSemi K) 9 (mmul (toSemi K) 9 (Pt K) (MM K M)) (Qt K) 4 7 = LibSym2Three.sym2 K M 1 4 := by
  have h : mmul (toSemi K) 9 (mmul (toSemi K) 9 (Pt K) (MM K M)) (Qt K) 4 7 = (K.add (K.mul K.one (MM K M 1 5)) (K.mul K.one (MM K M 3 5))) := by
    show (K.add (K.mul (K.add (K.mul K.zero (MM K M 0 0)) (K.add (K.mul K.one (MM K M 1 0)) (K.add (K.mul K.zero (MM K M 2 0)) (K.add (K.mul K.one (MM K M 3 0)) (K.add (K.mul K.zero (MM K M 4 0)) (K.add (K.mul K.zero (MM K M 5 0)) (K.add (K.mul K.zero (MM K M 6 0)) (K.add (K.mul K.zero (MM K M 7 0)) (K.add (K.mul K.zero (MM K M 8 0)) K.zero))))))))) K.zero) (K.add (K.mul (K.add (K.mul K.zero (MM K  -- (+2786 chars; full line in the Zenodo source)
    lib_ring
  rw [h]
  show (K.add (K.mul K.one (K.mul (M 0 1) (M 1 2))) (K.mul K.one (K.mul (M 1 1) (M 0 2)))) = (K.add (K.mul (M 0 1) (M 1 2)) (K.mul (M 1 1) (M 0 2)))
  lib_ring

theorem y_1_5 : mmul (toSemi K) 9 (mmul (toSemi K) 9 (Pt K) (MM K M)) (Qt K) 4 8 = LibSym2Three.sym2 K M 1 5 := by
  have h : mmul (toSemi K) 9 (mmul (toSemi K) 9 (Pt K) (MM K M)) (Qt K) 4 8 = (K.add (K.mul K.one (MM K M 1 8)) (K.mul K.one (MM K M 3 8))) := by
    show (K.add (K.mul (K.add (K.mul K.zero (MM K M 0 0)) (K.add (K.mul K.one (MM K M 1 0)) (K.add (K.mul K.zero (MM K M 2 0)) (K.add (K.mul K.one (MM K M 3 0)) (K.add (K.mul K.zero (MM K M 4 0)) (K.add (K.mul K.zero (MM K M 5 0)) (K.add (K.mul K.zero (MM K M 6 0)) (K.add (K.mul K.zero (MM K M 7 0)) (K.add (K.mul K.zero (MM K M 8 0)) K.zero))))))))) K.zero) (K.add (K.mul (K.add (K.mul K.zero (MM K  -- (+2786 chars; full line in the Zenodo source)
    lib_ring
  rw [h]
  show (K.add (K.mul K.one (K.mul (M 0 2) (M 1 2))) (K.mul K.one (K.mul (M 1 2) (M 0 2)))) = (K.add (K.mul (M 0 2) (M 1 2)) (K.mul (M 1 2) (M 0 2)))
  lib_ring

theorem y_2_0 : mmul (toSemi K) 9 (mmul (toSemi K) 9 (Pt K) (MM K M)) (Qt K) 5 3 = LibSym2Three.sym2 K M 2 0 := by
  have h : mmul (toSemi K) 9 (mmul (toSemi K) 9 (Pt K) (MM K M)) (Qt K) 5 3 = (K.add (K.mul K.one (MM K M 2 0)) (K.mul K.one (MM K M 6 0))) := by
    show (K.add (K.mul (K.add (K.mul K.zero (MM K M 0 0)) (K.add (K.mul K.zero (MM K M 1 0)) (K.add (K.mul K.one (MM K M 2 0)) (K.add (K.mul K.zero (MM K M 3 0)) (K.add (K.mul K.zero (MM K M 4 0)) (K.add (K.mul K.zero (MM K M 5 0)) (K.add (K.mul K.one (MM K M 6 0)) (K.add (K.mul K.zero (MM K M 7 0)) (K.add (K.mul K.zero (MM K M 8 0)) K.zero))))))))) K.one) (K.add (K.mul (K.add (K.mul K.zero (MM K   -- (+2786 chars; full line in the Zenodo source)
    lib_ring
  rw [h]
  show (K.add (K.mul K.one (K.mul (M 0 0) (M 2 0))) (K.mul K.one (K.mul (M 2 0) (M 0 0)))) = (K.add (K.mul (M 0 0) (M 2 0)) (K.mul (M 2 0) (M 0 0)))
  lib_ring

theorem y_2_1 : mmul (toSemi K) 9 (mmul (toSemi K) 9 (Pt K) (MM K M)) (Qt K) 5 4 = LibSym2Three.sym2 K M 2 1 := by
  have h : mmul (toSemi K) 9 (mmul (toSemi K) 9 (Pt K) (MM K M)) (Qt K) 5 4 = (K.add (K.mul K.one (MM K M 2 1)) (K.mul K.one (MM K M 6 1))) := by
    show (K.add (K.mul (K.add (K.mul K.zero (MM K M 0 0)) (K.add (K.mul K.zero (MM K M 1 0)) (K.add (K.mul K.one (MM K M 2 0)) (K.add (K.mul K.zero (MM K M 3 0)) (K.add (K.mul K.zero (MM K M 4 0)) (K.add (K.mul K.zero (MM K M 5 0)) (K.add (K.mul K.one (MM K M 6 0)) (K.add (K.mul K.zero (MM K M 7 0)) (K.add (K.mul K.zero (MM K M 8 0)) K.zero))))))))) K.zero) (K.add (K.mul (K.add (K.mul K.zero (MM K  -- (+2786 chars; full line in the Zenodo source)
    lib_ring
  rw [h]
  show (K.add (K.mul K.one (K.mul (M 0 0) (M 2 1))) (K.mul K.one (K.mul (M 2 0) (M 0 1)))) = (K.add (K.mul (M 0 0) (M 2 1)) (K.mul (M 2 0) (M 0 1)))
  lib_ring

theorem y_2_2 : mmul (toSemi K) 9 (mmul (toSemi K) 9 (Pt K) (MM K M)) (Qt K) 5 5 = LibSym2Three.sym2 K M 2 2 := by
  have h : mmul (toSemi K) 9 (mmul (toSemi K) 9 (Pt K) (MM K M)) (Qt K) 5 5 = (K.add (K.mul K.one (MM K M 2 2)) (K.mul K.one (MM K M 6 2))) := by
    show (K.add (K.mul (K.add (K.mul K.zero (MM K M 0 0)) (K.add (K.mul K.zero (MM K M 1 0)) (K.add (K.mul K.one (MM K M 2 0)) (K.add (K.mul K.zero (MM K M 3 0)) (K.add (K.mul K.zero (MM K M 4 0)) (K.add (K.mul K.zero (MM K M 5 0)) (K.add (K.mul K.one (MM K M 6 0)) (K.add (K.mul K.zero (MM K M 7 0)) (K.add (K.mul K.zero (MM K M 8 0)) K.zero))))))))) K.zero) (K.add (K.mul (K.add (K.mul K.zero (MM K  -- (+2786 chars; full line in the Zenodo source)
    lib_ring
  rw [h]
  show (K.add (K.mul K.one (K.mul (M 0 0) (M 2 2))) (K.mul K.one (K.mul (M 2 0) (M 0 2)))) = (K.add (K.mul (M 0 0) (M 2 2)) (K.mul (M 2 0) (M 0 2)))
  lib_ring

theorem y_2_3 : mmul (toSemi K) 9 (mmul (toSemi K) 9 (Pt K) (MM K M)) (Qt K) 5 6 = LibSym2Three.sym2 K M 2 3 := by
  have h : mmul (toSemi K) 9 (mmul (toSemi K) 9 (Pt K) (MM K M)) (Qt K) 5 6 = (K.add (K.mul K.one (MM K M 2 4)) (K.mul K.one (MM K M 6 4))) := by
    show (K.add (K.mul (K.add (K.mul K.zero (MM K M 0 0)) (K.add (K.mul K.zero (MM K M 1 0)) (K.add (K.mul K.one (MM K M 2 0)) (K.add (K.mul K.zero (MM K M 3 0)) (K.add (K.mul K.zero (MM K M 4 0)) (K.add (K.mul K.zero (MM K M 5 0)) (K.add (K.mul K.one (MM K M 6 0)) (K.add (K.mul K.zero (MM K M 7 0)) (K.add (K.mul K.zero (MM K M 8 0)) K.zero))))))))) K.zero) (K.add (K.mul (K.add (K.mul K.zero (MM K  -- (+2786 chars; full line in the Zenodo source)
    lib_ring
  rw [h]
  show (K.add (K.mul K.one (K.mul (M 0 1) (M 2 1))) (K.mul K.one (K.mul (M 2 1) (M 0 1)))) = (K.add (K.mul (M 0 1) (M 2 1)) (K.mul (M 2 1) (M 0 1)))
  lib_ring

theorem y_2_4 : mmul (toSemi K) 9 (mmul (toSemi K) 9 (Pt K) (MM K M)) (Qt K) 5 7 = LibSym2Three.sym2 K M 2 4 := by
  have h : mmul (toSemi K) 9 (mmul (toSemi K) 9 (Pt K) (MM K M)) (Qt K) 5 7 = (K.add (K.mul K.one (MM K M 2 5)) (K.mul K.one (MM K M 6 5))) := by
    show (K.add (K.mul (K.add (K.mul K.zero (MM K M 0 0)) (K.add (K.mul K.zero (MM K M 1 0)) (K.add (K.mul K.one (MM K M 2 0)) (K.add (K.mul K.zero (MM K M 3 0)) (K.add (K.mul K.zero (MM K M 4 0)) (K.add (K.mul K.zero (MM K M 5 0)) (K.add (K.mul K.one (MM K M 6 0)) (K.add (K.mul K.zero (MM K M 7 0)) (K.add (K.mul K.zero (MM K M 8 0)) K.zero))))))))) K.zero) (K.add (K.mul (K.add (K.mul K.zero (MM K  -- (+2786 chars; full line in the Zenodo source)
    lib_ring
  rw [h]
  show (K.add (K.mul K.one (K.mul (M 0 1) (M 2 2))) (K.mul K.one (K.mul (M 2 1) (M 0 2)))) = (K.add (K.mul (M 0 1) (M 2 2)) (K.mul (M 2 1) (M 0 2)))
  lib_ring

theorem y_2_5 : mmul (toSemi K) 9 (mmul (toSemi K) 9 (Pt K) (MM K M)) (Qt K) 5 8 = LibSym2Three.sym2 K M 2 5 := by
  have h : mmul (toSemi K) 9 (mmul (toSemi K) 9 (Pt K) (MM K M)) (Qt K) 5 8 = (K.add (K.mul K.one (MM K M 2 8)) (K.mul K.one (MM K M 6 8))) := by
    show (K.add (K.mul (K.add (K.mul K.zero (MM K M 0 0)) (K.add (K.mul K.zero (MM K M 1 0)) (K.add (K.mul K.one (MM K M 2 0)) (K.add (K.mul K.zero (MM K M 3 0)) (K.add (K.mul K.zero (MM K M 4 0)) (K.add (K.mul K.zero (MM K M 5 0)) (K.add (K.mul K.one (MM K M 6 0)) (K.add (K.mul K.zero (MM K M 7 0)) (K.add (K.mul K.zero (MM K M 8 0)) K.zero))))))))) K.zero) (K.add (K.mul (K.add (K.mul K.zero (MM K  -- (+2786 chars; full line in the Zenodo source)
    lib_ring
  rw [h]
  show (K.add (K.mul K.one (K.mul (M 0 2) (M 2 2))) (K.mul K.one (K.mul (M 2 2) (M 0 2)))) = (K.add (K.mul (M 0 2) (M 2 2)) (K.mul (M 2 2) (M 0 2)))
  lib_ring

theorem y_3_0 : mmul (toSemi K) 9 (mmul (toSemi K) 9 (Pt K) (MM K M)) (Qt K) 6 3 = LibSym2Three.sym2 K M 3 0 := by
  have h : mmul (toSemi K) 9 (mmul (toSemi K) 9 (Pt K) (MM K M)) (Qt K) 6 3 = (K.mul K.one (MM K M 4 0)) := by
    show (K.add (K.mul (K.add (K.mul K.zero (MM K M 0 0)) (K.add (K.mul K.zero (MM K M 1 0)) (K.add (K.mul K.zero (MM K M 2 0)) (K.add (K.mul K.zero (MM K M 3 0)) (K.add (K.mul K.one (MM K M 4 0)) (K.add (K.mul K.zero (MM K M 5 0)) (K.add (K.mul K.zero (MM K M 6 0)) (K.add (K.mul K.zero (MM K M 7 0)) (K.add (K.mul K.zero (MM K M 8 0)) K.zero))))))))) K.one) (K.add (K.mul (K.add (K.mul K.zero (MM K  -- (+2795 chars; full line in the Zenodo source)
    lib_ring
  rw [h]
  show (K.mul K.one (K.mul (M 1 0) (M 1 0))) = (K.mul (M 1 0) (M 1 0))
  lib_ring

theorem y_3_1 : mmul (toSemi K) 9 (mmul (toSemi K) 9 (Pt K) (MM K M)) (Qt K) 6 4 = LibSym2Three.sym2 K M 3 1 := by
  have h : mmul (toSemi K) 9 (mmul (toSemi K) 9 (Pt K) (MM K M)) (Qt K) 6 4 = (K.mul K.one (MM K M 4 1)) := by
    show (K.add (K.mul (K.add (K.mul K.zero (MM K M 0 0)) (K.add (K.mul K.zero (MM K M 1 0)) (K.add (K.mul K.zero (MM K M 2 0)) (K.add (K.mul K.zero (MM K M 3 0)) (K.add (K.mul K.one (MM K M 4 0)) (K.add (K.mul K.zero (MM K M 5 0)) (K.add (K.mul K.zero (MM K M 6 0)) (K.add (K.mul K.zero (MM K M 7 0)) (K.add (K.mul K.zero (MM K M 8 0)) K.zero))))))))) K.zero) (K.add (K.mul (K.add (K.mul K.zero (MM   -- (+2795 chars; full line in the Zenodo source)
    lib_ring
  rw [h]
  show (K.mul K.one (K.mul (M 1 0) (M 1 1))) = (K.mul (M 1 0) (M 1 1))
  lib_ring

theorem y_3_2 : mmul (toSemi K) 9 (mmul (toSemi K) 9 (Pt K) (MM K M)) (Qt K) 6 5 = LibSym2Three.sym2 K M 3 2 := by
  have h : mmul (toSemi K) 9 (mmul (toSemi K) 9 (Pt K) (MM K M)) (Qt K) 6 5 = (K.mul K.one (MM K M 4 2)) := by
    show (K.add (K.mul (K.add (K.mul K.zero (MM K M 0 0)) (K.add (K.mul K.zero (MM K M 1 0)) (K.add (K.mul K.zero (MM K M 2 0)) (K.add (K.mul K.zero (MM K M 3 0)) (K.add (K.mul K.one (MM K M 4 0)) (K.add (K.mul K.zero (MM K M 5 0)) (K.add (K.mul K.zero (MM K M 6 0)) (K.add (K.mul K.zero (MM K M 7 0)) (K.add (K.mul K.zero (MM K M 8 0)) K.zero))))))))) K.zero) (K.add (K.mul (K.add (K.mul K.zero (MM   -- (+2795 chars; full line in the Zenodo source)
    lib_ring
  rw [h]
  show (K.mul K.one (K.mul (M 1 0) (M 1 2))) = (K.mul (M 1 0) (M 1 2))
  lib_ring

theorem y_3_3 : mmul (toSemi K) 9 (mmul (toSemi K) 9 (Pt K) (MM K M)) (Qt K) 6 6 = LibSym2Three.sym2 K M 3 3 := by
  have h : mmul (toSemi K) 9 (mmul (toSemi K) 9 (Pt K) (MM K M)) (Qt K) 6 6 = (K.mul K.one (MM K M 4 4)) := by
    show (K.add (K.mul (K.add (K.mul K.zero (MM K M 0 0)) (K.add (K.mul K.zero (MM K M 1 0)) (K.add (K.mul K.zero (MM K M 2 0)) (K.add (K.mul K.zero (MM K M 3 0)) (K.add (K.mul K.one (MM K M 4 0)) (K.add (K.mul K.zero (MM K M 5 0)) (K.add (K.mul K.zero (MM K M 6 0)) (K.add (K.mul K.zero (MM K M 7 0)) (K.add (K.mul K.zero (MM K M 8 0)) K.zero))))))))) K.zero) (K.add (K.mul (K.add (K.mul K.zero (MM   -- (+2795 chars; full line in the Zenodo source)
    lib_ring
  rw [h]
  show (K.mul K.one (K.mul (M 1 1) (M 1 1))) = (K.mul (M 1 1) (M 1 1))
  lib_ring

theorem y_3_4 : mmul (toSemi K) 9 (mmul (toSemi K) 9 (Pt K) (MM K M)) (Qt K) 6 7 = LibSym2Three.sym2 K M 3 4 := by
  have h : mmul (toSemi K) 9 (mmul (toSemi K) 9 (Pt K) (MM K M)) (Qt K) 6 7 = (K.mul K.one (MM K M 4 5)) := by
    show (K.add (K.mul (K.add (K.mul K.zero (MM K M 0 0)) (K.add (K.mul K.zero (MM K M 1 0)) (K.add (K.mul K.zero (MM K M 2 0)) (K.add (K.mul K.zero (MM K M 3 0)) (K.add (K.mul K.one (MM K M 4 0)) (K.add (K.mul K.zero (MM K M 5 0)) (K.add (K.mul K.zero (MM K M 6 0)) (K.add (K.mul K.zero (MM K M 7 0)) (K.add (K.mul K.zero (MM K M 8 0)) K.zero))))))))) K.zero) (K.add (K.mul (K.add (K.mul K.zero (MM   -- (+2795 chars; full line in the Zenodo source)
    lib_ring
  rw [h]
  show (K.mul K.one (K.mul (M 1 1) (M 1 2))) = (K.mul (M 1 1) (M 1 2))
  lib_ring

theorem y_3_5 : mmul (toSemi K) 9 (mmul (toSemi K) 9 (Pt K) (MM K M)) (Qt K) 6 8 = LibSym2Three.sym2 K M 3 5 := by
  have h : mmul (toSemi K) 9 (mmul (toSemi K) 9 (Pt K) (MM K M)) (Qt K) 6 8 = (K.mul K.one (MM K M 4 8)) := by
    show (K.add (K.mul (K.add (K.mul K.zero (MM K M 0 0)) (K.add (K.mul K.zero (MM K M 1 0)) (K.add (K.mul K.zero (MM K M 2 0)) (K.add (K.mul K.zero (MM K M 3 0)) (K.add (K.mul K.one (MM K M 4 0)) (K.add (K.mul K.zero (MM K M 5 0)) (K.add (K.mul K.zero (MM K M 6 0)) (K.add (K.mul K.zero (MM K M 7 0)) (K.add (K.mul K.zero (MM K M 8 0)) K.zero))))))))) K.zero) (K.add (K.mul (K.add (K.mul K.zero (MM   -- (+2795 chars; full line in the Zenodo source)
    lib_ring
  rw [h]
  show (K.mul K.one (K.mul (M 1 2) (M 1 2))) = (K.mul (M 1 2) (M 1 2))
  lib_ring

theorem y_4_0 : mmul (toSemi K) 9 (mmul (toSemi K) 9 (Pt K) (MM K M)) (Qt K) 7 3 = LibSym2Three.sym2 K M 4 0 := by
  have h : mmul (toSemi K) 9 (mmul (toSemi K) 9 (Pt K) (MM K M)) (Qt K) 7 3 = (K.add (K.mul K.one (MM K M 5 0)) (K.mul K.one (MM K M 7 0))) := by
    show (K.add (K.mul (K.add (K.mul K.zero (MM K M 0 0)) (K.add (K.mul K.zero (MM K M 1 0)) (K.add (K.mul K.zero (MM K M 2 0)) (K.add (K.mul K.zero (MM K M 3 0)) (K.add (K.mul K.zero (MM K M 4 0)) (K.add (K.mul K.one (MM K M 5 0)) (K.add (K.mul K.zero (MM K M 6 0)) (K.add (K.mul K.one (MM K M 7 0)) (K.add (K.mul K.zero (MM K M 8 0)) K.zero))))))))) K.one) (K.add (K.mul (K.add (K.mul K.zero (MM K   -- (+2786 chars; full line in the Zenodo source)
    lib_ring
  rw [h]
  show (K.add (K.mul K.one (K.mul (M 1 0) (M 2 0))) (K.mul K.one (K.mul (M 2 0) (M 1 0)))) = (K.add (K.mul (M 1 0) (M 2 0)) (K.mul (M 2 0) (M 1 0)))
  lib_ring

theorem y_4_1 : mmul (toSemi K) 9 (mmul (toSemi K) 9 (Pt K) (MM K M)) (Qt K) 7 4 = LibSym2Three.sym2 K M 4 1 := by
  have h : mmul (toSemi K) 9 (mmul (toSemi K) 9 (Pt K) (MM K M)) (Qt K) 7 4 = (K.add (K.mul K.one (MM K M 5 1)) (K.mul K.one (MM K M 7 1))) := by
    show (K.add (K.mul (K.add (K.mul K.zero (MM K M 0 0)) (K.add (K.mul K.zero (MM K M 1 0)) (K.add (K.mul K.zero (MM K M 2 0)) (K.add (K.mul K.zero (MM K M 3 0)) (K.add (K.mul K.zero (MM K M 4 0)) (K.add (K.mul K.one (MM K M 5 0)) (K.add (K.mul K.zero (MM K M 6 0)) (K.add (K.mul K.one (MM K M 7 0)) (K.add (K.mul K.zero (MM K M 8 0)) K.zero))))))))) K.zero) (K.add (K.mul (K.add (K.mul K.zero (MM K  -- (+2786 chars; full line in the Zenodo source)
    lib_ring
  rw [h]
  show (K.add (K.mul K.one (K.mul (M 1 0) (M 2 1))) (K.mul K.one (K.mul (M 2 0) (M 1 1)))) = (K.add (K.mul (M 1 0) (M 2 1)) (K.mul (M 2 0) (M 1 1)))
  lib_ring

theorem y_4_2 : mmul (toSemi K) 9 (mmul (toSemi K) 9 (Pt K) (MM K M)) (Qt K) 7 5 = LibSym2Three.sym2 K M 4 2 := by
  have h : mmul (toSemi K) 9 (mmul (toSemi K) 9 (Pt K) (MM K M)) (Qt K) 7 5 = (K.add (K.mul K.one (MM K M 5 2)) (K.mul K.one (MM K M 7 2))) := by
    show (K.add (K.mul (K.add (K.mul K.zero (MM K M 0 0)) (K.add (K.mul K.zero (MM K M 1 0)) (K.add (K.mul K.zero (MM K M 2 0)) (K.add (K.mul K.zero (MM K M 3 0)) (K.add (K.mul K.zero (MM K M 4 0)) (K.add (K.mul K.one (MM K M 5 0)) (K.add (K.mul K.zero (MM K M 6 0)) (K.add (K.mul K.one (MM K M 7 0)) (K.add (K.mul K.zero (MM K M 8 0)) K.zero))))))))) K.zero) (K.add (K.mul (K.add (K.mul K.zero (MM K  -- (+2786 chars; full line in the Zenodo source)
    lib_ring
  rw [h]
  show (K.add (K.mul K.one (K.mul (M 1 0) (M 2 2))) (K.mul K.one (K.mul (M 2 0) (M 1 2)))) = (K.add (K.mul (M 1 0) (M 2 2)) (K.mul (M 2 0) (M 1 2)))
  lib_ring

theorem y_4_3 : mmul (toSemi K) 9 (mmul (toSemi K) 9 (Pt K) (MM K M)) (Qt K) 7 6 = LibSym2Three.sym2 K M 4 3 := by
  have h : mmul (toSemi K) 9 (mmul (toSemi K) 9 (Pt K) (MM K M)) (Qt K) 7 6 = (K.add (K.mul K.one (MM K M 5 4)) (K.mul K.one (MM K M 7 4))) := by
    show (K.add (K.mul (K.add (K.mul K.zero (MM K M 0 0)) (K.add (K.mul K.zero (MM K M 1 0)) (K.add (K.mul K.zero (MM K M 2 0)) (K.add (K.mul K.zero (MM K M 3 0)) (K.add (K.mul K.zero (MM K M 4 0)) (K.add (K.mul K.one (MM K M 5 0)) (K.add (K.mul K.zero (MM K M 6 0)) (K.add (K.mul K.one (MM K M 7 0)) (K.add (K.mul K.zero (MM K M 8 0)) K.zero))))))))) K.zero) (K.add (K.mul (K.add (K.mul K.zero (MM K  -- (+2786 chars; full line in the Zenodo source)
    lib_ring
  rw [h]
  show (K.add (K.mul K.one (K.mul (M 1 1) (M 2 1))) (K.mul K.one (K.mul (M 2 1) (M 1 1)))) = (K.add (K.mul (M 1 1) (M 2 1)) (K.mul (M 2 1) (M 1 1)))
  lib_ring

theorem y_4_4 : mmul (toSemi K) 9 (mmul (toSemi K) 9 (Pt K) (MM K M)) (Qt K) 7 7 = LibSym2Three.sym2 K M 4 4 := by
  have h : mmul (toSemi K) 9 (mmul (toSemi K) 9 (Pt K) (MM K M)) (Qt K) 7 7 = (K.add (K.mul K.one (MM K M 5 5)) (K.mul K.one (MM K M 7 5))) := by
    show (K.add (K.mul (K.add (K.mul K.zero (MM K M 0 0)) (K.add (K.mul K.zero (MM K M 1 0)) (K.add (K.mul K.zero (MM K M 2 0)) (K.add (K.mul K.zero (MM K M 3 0)) (K.add (K.mul K.zero (MM K M 4 0)) (K.add (K.mul K.one (MM K M 5 0)) (K.add (K.mul K.zero (MM K M 6 0)) (K.add (K.mul K.one (MM K M 7 0)) (K.add (K.mul K.zero (MM K M 8 0)) K.zero))))))))) K.zero) (K.add (K.mul (K.add (K.mul K.zero (MM K  -- (+2786 chars; full line in the Zenodo source)
    lib_ring
  rw [h]
  show (K.add (K.mul K.one (K.mul (M 1 1) (M 2 2))) (K.mul K.one (K.mul (M 2 1) (M 1 2)))) = (K.add (K.mul (M 1 1) (M 2 2)) (K.mul (M 2 1) (M 1 2)))
  lib_ring

theorem y_4_5 : mmul (toSemi K) 9 (mmul (toSemi K) 9 (Pt K) (MM K M)) (Qt K) 7 8 = LibSym2Three.sym2 K M 4 5 := by
  have h : mmul (toSemi K) 9 (mmul (toSemi K) 9 (Pt K) (MM K M)) (Qt K) 7 8 = (K.add (K.mul K.one (MM K M 5 8)) (K.mul K.one (MM K M 7 8))) := by
    show (K.add (K.mul (K.add (K.mul K.zero (MM K M 0 0)) (K.add (K.mul K.zero (MM K M 1 0)) (K.add (K.mul K.zero (MM K M 2 0)) (K.add (K.mul K.zero (MM K M 3 0)) (K.add (K.mul K.zero (MM K M 4 0)) (K.add (K.mul K.one (MM K M 5 0)) (K.add (K.mul K.zero (MM K M 6 0)) (K.add (K.mul K.one (MM K M 7 0)) (K.add (K.mul K.zero (MM K M 8 0)) K.zero))))))))) K.zero) (K.add (K.mul (K.add (K.mul K.zero (MM K  -- (+2786 chars; full line in the Zenodo source)
    lib_ring
  rw [h]
  show (K.add (K.mul K.one (K.mul (M 1 2) (M 2 2))) (K.mul K.one (K.mul (M 2 2) (M 1 2)))) = (K.add (K.mul (M 1 2) (M 2 2)) (K.mul (M 2 2) (M 1 2)))
  lib_ring

theorem y_5_0 : mmul (toSemi K) 9 (mmul (toSemi K) 9 (Pt K) (MM K M)) (Qt K) 8 3 = LibSym2Three.sym2 K M 5 0 := by
  have h : mmul (toSemi K) 9 (mmul (toSemi K) 9 (Pt K) (MM K M)) (Qt K) 8 3 = (K.mul K.one (MM K M 8 0)) := by
    show (K.add (K.mul (K.add (K.mul K.zero (MM K M 0 0)) (K.add (K.mul K.zero (MM K M 1 0)) (K.add (K.mul K.zero (MM K M 2 0)) (K.add (K.mul K.zero (MM K M 3 0)) (K.add (K.mul K.zero (MM K M 4 0)) (K.add (K.mul K.zero (MM K M 5 0)) (K.add (K.mul K.zero (MM K M 6 0)) (K.add (K.mul K.zero (MM K M 7 0)) (K.add (K.mul K.one (MM K M 8 0)) K.zero))))))))) K.one) (K.add (K.mul (K.add (K.mul K.zero (MM K  -- (+2795 chars; full line in the Zenodo source)
    lib_ring
  rw [h]
  show (K.mul K.one (K.mul (M 2 0) (M 2 0))) = (K.mul (M 2 0) (M 2 0))
  lib_ring

theorem y_5_1 : mmul (toSemi K) 9 (mmul (toSemi K) 9 (Pt K) (MM K M)) (Qt K) 8 4 = LibSym2Three.sym2 K M 5 1 := by
  have h : mmul (toSemi K) 9 (mmul (toSemi K) 9 (Pt K) (MM K M)) (Qt K) 8 4 = (K.mul K.one (MM K M 8 1)) := by
    show (K.add (K.mul (K.add (K.mul K.zero (MM K M 0 0)) (K.add (K.mul K.zero (MM K M 1 0)) (K.add (K.mul K.zero (MM K M 2 0)) (K.add (K.mul K.zero (MM K M 3 0)) (K.add (K.mul K.zero (MM K M 4 0)) (K.add (K.mul K.zero (MM K M 5 0)) (K.add (K.mul K.zero (MM K M 6 0)) (K.add (K.mul K.zero (MM K M 7 0)) (K.add (K.mul K.one (MM K M 8 0)) K.zero))))))))) K.zero) (K.add (K.mul (K.add (K.mul K.zero (MM   -- (+2795 chars; full line in the Zenodo source)
    lib_ring
  rw [h]
  show (K.mul K.one (K.mul (M 2 0) (M 2 1))) = (K.mul (M 2 0) (M 2 1))
  lib_ring

theorem y_5_2 : mmul (toSemi K) 9 (mmul (toSemi K) 9 (Pt K) (MM K M)) (Qt K) 8 5 = LibSym2Three.sym2 K M 5 2 := by
  have h : mmul (toSemi K) 9 (mmul (toSemi K) 9 (Pt K) (MM K M)) (Qt K) 8 5 = (K.mul K.one (MM K M 8 2)) := by
    show (K.add (K.mul (K.add (K.mul K.zero (MM K M 0 0)) (K.add (K.mul K.zero (MM K M 1 0)) (K.add (K.mul K.zero (MM K M 2 0)) (K.add (K.mul K.zero (MM K M 3 0)) (K.add (K.mul K.zero (MM K M 4 0)) (K.add (K.mul K.zero (MM K M 5 0)) (K.add (K.mul K.zero (MM K M 6 0)) (K.add (K.mul K.zero (MM K M 7 0)) (K.add (K.mul K.one (MM K M 8 0)) K.zero))))))))) K.zero) (K.add (K.mul (K.add (K.mul K.zero (MM   -- (+2795 chars; full line in the Zenodo source)
    lib_ring
  rw [h]
  show (K.mul K.one (K.mul (M 2 0) (M 2 2))) = (K.mul (M 2 0) (M 2 2))
  lib_ring

theorem y_5_3 : mmul (toSemi K) 9 (mmul (toSemi K) 9 (Pt K) (MM K M)) (Qt K) 8 6 = LibSym2Three.sym2 K M 5 3 := by
  have h : mmul (toSemi K) 9 (mmul (toSemi K) 9 (Pt K) (MM K M)) (Qt K) 8 6 = (K.mul K.one (MM K M 8 4)) := by
    show (K.add (K.mul (K.add (K.mul K.zero (MM K M 0 0)) (K.add (K.mul K.zero (MM K M 1 0)) (K.add (K.mul K.zero (MM K M 2 0)) (K.add (K.mul K.zero (MM K M 3 0)) (K.add (K.mul K.zero (MM K M 4 0)) (K.add (K.mul K.zero (MM K M 5 0)) (K.add (K.mul K.zero (MM K M 6 0)) (K.add (K.mul K.zero (MM K M 7 0)) (K.add (K.mul K.one (MM K M 8 0)) K.zero))))))))) K.zero) (K.add (K.mul (K.add (K.mul K.zero (MM   -- (+2795 chars; full line in the Zenodo source)
    lib_ring
  rw [h]
  show (K.mul K.one (K.mul (M 2 1) (M 2 1))) = (K.mul (M 2 1) (M 2 1))
  lib_ring

theorem y_5_4 : mmul (toSemi K) 9 (mmul (toSemi K) 9 (Pt K) (MM K M)) (Qt K) 8 7 = LibSym2Three.sym2 K M 5 4 := by
  have h : mmul (toSemi K) 9 (mmul (toSemi K) 9 (Pt K) (MM K M)) (Qt K) 8 7 = (K.mul K.one (MM K M 8 5)) := by
    show (K.add (K.mul (K.add (K.mul K.zero (MM K M 0 0)) (K.add (K.mul K.zero (MM K M 1 0)) (K.add (K.mul K.zero (MM K M 2 0)) (K.add (K.mul K.zero (MM K M 3 0)) (K.add (K.mul K.zero (MM K M 4 0)) (K.add (K.mul K.zero (MM K M 5 0)) (K.add (K.mul K.zero (MM K M 6 0)) (K.add (K.mul K.zero (MM K M 7 0)) (K.add (K.mul K.one (MM K M 8 0)) K.zero))))))))) K.zero) (K.add (K.mul (K.add (K.mul K.zero (MM   -- (+2795 chars; full line in the Zenodo source)
    lib_ring
  rw [h]
  show (K.mul K.one (K.mul (M 2 1) (M 2 2))) = (K.mul (M 2 1) (M 2 2))
  lib_ring

theorem y_5_5 : mmul (toSemi K) 9 (mmul (toSemi K) 9 (Pt K) (MM K M)) (Qt K) 8 8 = LibSym2Three.sym2 K M 5 5 := by
  have h : mmul (toSemi K) 9 (mmul (toSemi K) 9 (Pt K) (MM K M)) (Qt K) 8 8 = (K.mul K.one (MM K M 8 8)) := by
    show (K.add (K.mul (K.add (K.mul K.zero (MM K M 0 0)) (K.add (K.mul K.zero (MM K M 1 0)) (K.add (K.mul K.zero (MM K M 2 0)) (K.add (K.mul K.zero (MM K M 3 0)) (K.add (K.mul K.zero (MM K M 4 0)) (K.add (K.mul K.zero (MM K M 5 0)) (K.add (K.mul K.zero (MM K M 6 0)) (K.add (K.mul K.zero (MM K M 7 0)) (K.add (K.mul K.one (MM K M 8 0)) K.zero))))))))) K.zero) (K.add (K.mul (K.add (K.mul K.zero (MM   -- (+2795 chars; full line in the Zenodo source)
    lib_ring
  rw [h]
  show (K.mul K.one (K.mul (M 2 2) (M 2 2))) = (K.mul (M 2 2) (M 2 2))
  lib_ring

theorem low (s j : Nat) (hs : s < 6) (hj : j < 3) : mmul (toSemi K) (3 + 6) (mmul (toSemi K) (3 + 6) (Pt K) (MM K M)) (Qt K) (3 + s) j = K.zero := by
  rcases LibCGGen2.six s hs with rfl | rfl | rfl | rfl | rfl | rfl <;> rcases LibSym2.three j hj with rfl | rfl | rfl
  · exact z_0_0 K M
  · exact z_0_1 K M
  · exact z_0_2 K M
  · exact z_1_0 K M
  · exact z_1_1 K M
  · exact z_1_2 K M
  · exact z_2_0 K M
  · exact z_2_1 K M
  · exact z_2_2 K M
  · exact z_3_0 K M
  · exact z_3_1 K M
  · exact z_3_2 K M
  · exact z_4_0 K M
  · exact z_4_1 K M
  · exact z_4_2 K M
  · exact z_5_0 K M
  · exact z_5_1 K M
  · exact z_5_2 K M

theorem hx (i j : Nat) (hi : i < 3) (hj : j < 3) : mmul (toSemi K) (3 + 6) (mmul (toSemi K) (3 + 6) (Pt K) (MM K M)) (Qt K) i j = LibExt2Three.ext2 K M i j := by
  rcases LibSym2.three i hi with rfl | rfl | rfl <;> rcases LibSym2.three j hj with rfl | rfl | rfl
  · exact x_0_0 K M
  · exact x_0_1 K M
  · exact x_0_2 K M
  · exact x_1_0 K M
  · exact x_1_1 K M
  · exact x_1_2 K M
  · exact x_2_0 K M
  · exact x_2_1 K M
  · exact x_2_2 K M

theorem hy (i j : Nat) (hi : i < 6) (hj : j < 6) : mmul (toSemi K) (3 + 6) (mmul (toSemi K) (3 + 6) (Pt K) (MM K M)) (Qt K) (3 + i) (3 + j) = LibSym2Three.sym2 K M i j := by
  rcases LibCGGen2.six i hi with rfl | rfl | rfl | rfl | rfl | rfl <;> rcases LibCGGen2.six j hj with rfl | rfl | rfl | rfl | rfl | rfl
  · exact y_0_0 K M
  · exact y_0_1 K M
  · exact y_0_2 K M
  · exact y_0_3 K M
  · exact y_0_4 K M
  · exact y_0_5 K M
  · exact y_1_0 K M
  · exact y_1_1 K M
  · exact y_1_2 K M
  · exact y_1_3 K M
  · exact y_1_4 K M
  · exact y_1_5 K M
  · exact y_2_0 K M
  · exact y_2_1 K M
  · exact y_2_2 K M
  · exact y_2_3 K M
  · exact y_2_4 K M
  · exact y_2_5 K M
  · exact y_3_0 K M
  · exact y_3_1 K M
  · exact y_3_2 K M
  · exact y_3_3 K M
  · exact y_3_4 K M
  · exact y_3_5 K M
  · exact y_4_0 K M
  · exact y_4_1 K M
  · exact y_4_2 K M
  · exact y_4_3 K M
  · exact y_4_4 K M
  · exact y_4_5 K M
  · exact y_5_0 K M
  · exact y_5_1 K M
  · exact y_5_2 K M
  · exact y_5_3 K M
  · exact y_5_4 K M
  · exact y_5_5 K M

/-- **L(M⊗M) = L(∧²M) L(Sym²M)** (any 3×3). -/
theorem tensor_sq3 (x : R) :
    det K (3 * 3) (imx K (fl 3 (kron K M M)) x) = K.mul (det K 3 (imx K (LibExt2Three.ext2 K M) x)) (det K 6 (imx K (LibSym2Three.sym2 K M) x)) :=
  LibBlockSim.block_sim_euler K 3 6 (MM K M) (Pt K) (Qt K) (LibExt2Three.ext2 K M) (LibSym2Three.sym2 K M) (pq K) (low K M) (hx K M) (hy K M) x

end LibTensorSq3

#print axioms LibTensorSq3.tensor_sq3

lib/TensorSq3Psum.lean

lean_all/lib/TensorSq3Psum.lean

/-
  lib/TensorSq3Psum.lean — T46 shared library, cycle 596 (G315): traces of powers of the 3×3 tensor square.

  Any commutative ring, M any 3×3:
    qp            QP = I (P, Q of TensorSq3)
    tensor_sq3_psum   tr(M⊗M)^k = tr(∧²M)^k + tr(Sym²M)^k          (BlockSim.block_sim_psum)
  Lean 4 core only, no axioms.
-/
import lib.FunctorLin

namespace LibTensorSq3Psum
open DetChain LibRing LibSum LibSemi LibMat LibDet LibKron LibKronDetGen LibTensorSq3

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

theorem qp_0_0 : mmul (toSemi K) 9 (Qt K) (Pt K) 0 0 = idM K 0 0 := by
  show (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.one K.one) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) K.zero))))))))) = K.one; lib_ring
theorem qp_0_1 : mmul (toSemi K) 9 (Qt K) (Pt K) 0 1 = idM K 0 1 := by
  show (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.one K.zero) (K.add (K.mul K.zero K.one) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) K.zero))))))))) = K.zero; lib_ring
theorem qp_0_2 : mmul (toSemi K) 9 (Qt K) (Pt K) 0 2 = idM K 0 2 := by
  show (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.one K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.one) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) K.zero))))))))) = K.zero; lib_ring
theorem qp_0_3 : mmul (toSemi K) 9 (Qt K) (Pt K) 0 3 = idM K 0 3 := by
  show (K.add (K.mul K.zero (K.neg K.one)) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.one K.zero) (K.add (K.mul K.zero K.one) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) K.zero))))))))) = K.zero; lib_ring
theorem qp_0_4 : mmul (toSemi K) 9 (Qt K) (Pt K) 0 4 = idM K 0 4 := by
  show (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.one K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.one) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) K.zero))))))))) = K.zero; lib_ring
theorem qp_0_5 : mmul (toSemi K) 9 (Qt K) (Pt K) 0 5 = idM K 0 5 := by
  show (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.one K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.one) (K.add (K.mul K.zero K.zero) K.zero))))))))) = K.zero; lib_ring
theorem qp_0_6 : mmul (toSemi K) 9 (Qt K) (Pt K) 0 6 = idM K 0 6 := by
  show (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero (K.neg K.one)) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.one K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.one) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) K.zero))))))))) = K.zero; lib_ring
theorem qp_0_7 : mmul (toSemi K) 9 (Qt K) (Pt K) 0 7 = idM K 0 7 := by
  show (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero (K.neg K.one)) (K.add (K.mul K.one K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.one) (K.add (K.mul K.zero K.zero) K.zero))))))))) = K.zero; lib_ring
theorem qp_0_8 : mmul (toSemi K) 9 (Qt K) (Pt K) 0 8 = idM K 0 8 := by
  show (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.one K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.one) K.zero))))))))) = K.zero; lib_ring
theorem qp_1_0 : mmul (toSemi K) 9 (Qt K) (Pt K) 1 0 = idM K 1 0 := by
  show (K.add (K.mul K.one K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.one) (K.add (K.mul K.one K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) K.zero))))))))) = K.zero; lib_ring
theorem qp_1_1 : mmul (toSemi K) 9 (Qt K) (Pt K) 1 1 = idM K 1 1 := by
  show (K.add (K.mul K.one K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.one K.one) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) K.zero))))))))) = K.one; lib_ring
theorem qp_1_2 : mmul (toSemi K) 9 (Qt K) (Pt K) 1 2 = idM K 1 2 := by
  show (K.add (K.mul K.one K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.one K.zero) (K.add (K.mul K.zero K.one) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) K.zero))))))))) = K.zero; lib_ring
theorem qp_1_3 : mmul (toSemi K) 9 (Qt K) (Pt K) 1 3 = idM K 1 3 := by
  show (K.add (K.mul K.one (K.neg K.one)) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.one K.one) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) K.zero))))))))) = K.zero; lib_ring
theorem qp_1_4 : mmul (toSemi K) 9 (Qt K) (Pt K) 1 4 = idM K 1 4 := by
  show (K.add (K.mul K.one K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.one K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.one) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) K.zero))))))))) = K.zero; lib_ring
theorem qp_1_5 : mmul (toSemi K) 9 (Qt K) (Pt K) 1 5 = idM K 1 5 := by
  show (K.add (K.mul K.one K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.one K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.one) (K.add (K.mul K.zero K.zero) K.zero))))))))) = K.zero; lib_ring
theorem qp_1_6 : mmul (toSemi K) 9 (Qt K) (Pt K) 1 6 = idM K 1 6 := by
  show (K.add (K.mul K.one K.zero) (K.add (K.mul K.zero (K.neg K.one)) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.one K.zero) (K.add (K.mul K.zero K.one) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) K.zero))))))))) = K.zero; lib_ring
theorem qp_1_7 : mmul (toSemi K) 9 (Qt K) (Pt K) 1 7 = idM K 1 7 := by
  show (K.add (K.mul K.one K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero (K.neg K.one)) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.one K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.one) (K.add (K.mul K.zero K.zero) K.zero))))))))) = K.zero; lib_ring
theorem qp_1_8 : mmul (toSemi K) 9 (Qt K) (Pt K) 1 8 = idM K 1 8 := by
  show (K.add (K.mul K.one K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.one K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.one) K.zero))))))))) = K.zero; lib_ring
theorem qp_2_0 : mmul (toSemi K) 9 (Qt K) (Pt K) 2 0 = idM K 2 0 := by
  show (K.add (K.mul K.zero K.zero) (K.add (K.mul K.one K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.one) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.one K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) K.zero))))))))) = K.zero; lib_ring
theorem qp_2_1 : mmul (toSemi K) 9 (Qt K) (Pt K) 2 1 = idM K 2 1 := by
  show (K.add (K.mul K.zero K.zero) (K.add (K.mul K.one K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.one) (K.add (K.mul K.one K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) K.zero))))))))) = K.zero; lib_ring
theorem qp_2_2 : mmul (toSemi K) 9 (Qt K) (Pt K) 2 2 = idM K 2 2 := by
  show (K.add (K.mul K.zero K.zero) (K.add (K.mul K.one K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.one K.one) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) K.zero))))))))) = K.one; lib_ring
theorem qp_2_3 : mmul (toSemi K) 9 (Qt K) (Pt K) 2 3 = idM K 2 3 := by
  show (K.add (K.mul K.zero (K.neg K.one)) (K.add (K.mul K.one K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.one) (K.add (K.mul K.one K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) K.zero))))))))) = K.zero; lib_ring
theorem qp_2_4 : mmul (toSemi K) 9 (Qt K) (Pt K) 2 4 = idM K 2 4 := by
  show (K.add (K.mul K.zero K.zero) (K.add (K.mul K.one K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.one K.zero) (K.add (K.mul K.zero K.one) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) K.zero))))))))) = K.zero; lib_ring
theorem qp_2_5 : mmul (toSemi K) 9 (Qt K) (Pt K) 2 5 = idM K 2 5 := by
  show (K.add (K.mul K.zero K.zero) (K.add (K.mul K.one K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.one K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.one) (K.add (K.mul K.zero K.zero) K.zero))))))))) = K.zero; lib_ring
theorem qp_2_6 : mmul (toSemi K) 9 (Qt K) (Pt K) 2 6 = idM K 2 6 := by
  show (K.add (K.mul K.zero K.zero) (K.add (K.mul K.one (K.neg K.one)) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.one K.one) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) K.zero))))))))) = K.zero; lib_ring
theorem qp_2_7 : mmul (toSemi K) 9 (Qt K) (Pt K) 2 7 = idM K 2 7 := by
  show (K.add (K.mul K.zero K.zero) (K.add (K.mul K.one K.zero) (K.add (K.mul K.zero (K.neg K.one)) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.one K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.one) (K.add (K.mul K.zero K.zero) K.zero))))))))) = K.zero; lib_ring
theorem qp_2_8 : mmul (toSemi K) 9 (Qt K) (Pt K) 2 8 = idM K 2 8 := by
  show (K.add (K.mul K.zero K.zero) (K.add (K.mul K.one K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.one K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.one) K.zero))))))))) = K.zero; lib_ring
theorem qp_3_0 : mmul (toSemi K) 9 (Qt K) (Pt K) 3 0 = idM K 3 0 := by
  show (K.add (K.mul (K.neg K.one) K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.one) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) K.zero))))))))) = K.zero; lib_ring
theorem qp_3_1 : mmul (toSemi K) 9 (Qt K) (Pt K) 3 1 = idM K 3 1 := by
  show (K.add (K.mul (K.neg K.one) K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.one) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) K.zero))))))))) = K.zero; lib_ring
theorem qp_3_2 : mmul (toSemi K) 9 (Qt K) (Pt K) 3 2 = idM K 3 2 := by
  show (K.add (K.mul (K.neg K.one) K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.one) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) K.zero))))))))) = K.zero; lib_ring
theorem qp_3_3 : mmul (toSemi K) 9 (Qt K) (Pt K) 3 3 = idM K 3 3 := by
  show (K.add (K.mul (K.neg K.one) (K.neg K.one)) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.one) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) K.zero))))))))) = K.one; lib_ring
theorem qp_3_4 : mmul (toSemi K) 9 (Qt K) (Pt K) 3 4 = idM K 3 4 := by
  show (K.add (K.mul (K.neg K.one) K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.one) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) K.zero))))))))) = K.zero; lib_ring
theorem qp_3_5 : mmul (toSemi K) 9 (Qt K) (Pt K) 3 5 = idM K 3 5 := by
  show (K.add (K.mul (K.neg K.one) K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.one) (K.add (K.mul K.zero K.zero) K.zero))))))))) = K.zero; lib_ring
theorem qp_3_6 : mmul (toSemi K) 9 (Qt K) (Pt K) 3 6 = idM K 3 6 := by
  show (K.add (K.mul (K.neg K.one) K.zero) (K.add (K.mul K.zero (K.neg K.one)) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.one) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) K.zero))))))))) = K.zero; lib_ring
theorem qp_3_7 : mmul (toSemi K) 9 (Qt K) (Pt K) 3 7 = idM K 3 7 := by
  show (K.add (K.mul (K.neg K.one) K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero (K.neg K.one)) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.one) (K.add (K.mul K.zero K.zero) K.zero))))))))) = K.zero; lib_ring
theorem qp_3_8 : mmul (toSemi K) 9 (Qt K) (Pt K) 3 8 = idM K 3 8 := by
  show (K.add (K.mul (K.neg K.one) K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.one) K.zero))))))))) = K.zero; lib_ring
theorem qp_4_0 : mmul (toSemi K) 9 (Qt K) (Pt K) 4 0 = idM K 4 0 := by
  show (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.one) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.one K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) K.zero))))))))) = K.zero; lib_ring
theorem qp_4_1 : mmul (toSemi K) 9 (Qt K) (Pt K) 4 1 = idM K 4 1 := by
  show (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.one) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.one K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) K.zero))))))))) = K.zero; lib_ring
theorem qp_4_2 : mmul (toSemi K) 9 (Qt K) (Pt K) 4 2 = idM K 4 2 := by
  show (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.one) (K.add (K.mul K.one K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) K.zero))))))))) = K.zero; lib_ring
theorem qp_4_3 : mmul (toSemi K) 9 (Qt K) (Pt K) 4 3 = idM K 4 3 := by
  show (K.add (K.mul K.zero (K.neg K.one)) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.one) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.one K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) K.zero))))))))) = K.zero; lib_ring
theorem qp_4_4 : mmul (toSemi K) 9 (Qt K) (Pt K) 4 4 = idM K 4 4 := by
  show (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.one K.one) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) K.zero))))))))) = K.one; lib_ring
theorem qp_4_5 : mmul (toSemi K) 9 (Qt K) (Pt K) 4 5 = idM K 4 5 := by
  show (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.one K.zero) (K.add (K.mul K.zero K.one) (K.add (K.mul K.zero K.zero) K.zero))))))))) = K.zero; lib_ring
theorem qp_4_6 : mmul (toSemi K) 9 (Qt K) (Pt K) 4 6 = idM K 4 6 := by
  show (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero (K.neg K.one)) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.one) (K.add (K.mul K.one K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) K.zero))))))))) = K.zero; lib_ring
theorem qp_4_7 : mmul (toSemi K) 9 (Qt K) (Pt K) 4 7 = idM K 4 7 := by
  show (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero (K.neg K.one)) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.one K.zero) (K.add (K.mul K.zero K.one) (K.add (K.mul K.zero K.zero) K.zero))))))))) = K.zero; lib_ring
theorem qp_4_8 : mmul (toSemi K) 9 (Qt K) (Pt K) 4 8 = idM K 4 8 := by
  show (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.one K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.one) K.zero))))))))) = K.zero; lib_ring
theorem qp_5_0 : mmul (toSemi K) 9 (Qt K) (Pt K) 5 0 = idM K 5 0 := by
  show (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.one K.zero) (K.add (K.mul K.zero K.one) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.one K.zero) (K.add (K.mul K.zero K.zero) K.zero))))))))) = K.zero; lib_ring
theorem qp_5_1 : mmul (toSemi K) 9 (Qt K) (Pt K) 5 1 = idM K 5 1 := by
  show (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.one K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.one) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.one K.zero) (K.add (K.mul K.zero K.zero) K.zero))))))))) = K.zero; lib_ring
theorem qp_5_2 : mmul (toSemi K) 9 (Qt K) (Pt K) 5 2 = idM K 5 2 := by
  show (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.one K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.one) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.one K.zero) (K.add (K.mul K.zero K.zero) K.zero))))))))) = K.zero; lib_ring
theorem qp_5_3 : mmul (toSemi K) 9 (Qt K) (Pt K) 5 3 = idM K 5 3 := by
  show (K.add (K.mul K.zero (K.neg K.one)) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.one K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.one) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.one K.zero) (K.add (K.mul K.zero K.zero) K.zero))))))))) = K.zero; lib_ring
theorem qp_5_4 : mmul (toSemi K) 9 (Qt K) (Pt K) 5 4 = idM K 5 4 := by
  show (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.one K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.one) (K.add (K.mul K.one K.zero) (K.add (K.mul K.zero K.zero) K.zero))))))))) = K.zero; lib_ring
theorem qp_5_5 : mmul (toSemi K) 9 (Qt K) (Pt K) 5 5 = idM K 5 5 := by
  show (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.one K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.one K.one) (K.add (K.mul K.zero K.zero) K.zero))))))))) = K.one; lib_ring
theorem qp_5_6 : mmul (toSemi K) 9 (Qt K) (Pt K) 5 6 = idM K 5 6 := by
  show (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero (K.neg K.one)) (K.add (K.mul K.one K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.one) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.one K.zero) (K.add (K.mul K.zero K.zero) K.zero))))))))) = K.zero; lib_ring
theorem qp_5_7 : mmul (toSemi K) 9 (Qt K) (Pt K) 5 7 = idM K 5 7 := by
  show (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.one (K.neg K.one)) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.one K.one) (K.add (K.mul K.zero K.zero) K.zero))))))))) = K.zero; lib_ring
theorem qp_5_8 : mmul (toSemi K) 9 (Qt K) (Pt K) 5 8 = idM K 5 8 := by
  show (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.one K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.one K.zero) (K.add (K.mul K.zero K.one) K.zero))))))))) = K.zero; lib_ring
theorem qp_6_0 : mmul (toSemi K) 9 (Qt K) (Pt K) 6 0 = idM K 6 0 := by
  show (K.add (K.mul K.zero K.zero) (K.add (K.mul (K.neg K.one) K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.one) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) K.zero))))))))) = K.zero; lib_ring
theorem qp_6_1 : mmul (toSemi K) 9 (Qt K) (Pt K) 6 1 = idM K 6 1 := by
  show (K.add (K.mul K.zero K.zero) (K.add (K.mul (K.neg K.one) K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.one) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) K.zero))))))))) = K.zero; lib_ring
theorem qp_6_2 : mmul (toSemi K) 9 (Qt K) (Pt K) 6 2 = idM K 6 2 := by
  show (K.add (K.mul K.zero K.zero) (K.add (K.mul (K.neg K.one) K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.one) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) K.zero))))))))) = K.zero; lib_ring
theorem qp_6_3 : mmul (toSemi K) 9 (Qt K) (Pt K) 6 3 = idM K 6 3 := by
  show (K.add (K.mul K.zero (K.neg K.one)) (K.add (K.mul (K.neg K.one) K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.one) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) K.zero))))))))) = K.zero; lib_ring
theorem qp_6_4 : mmul (toSemi K) 9 (Qt K) (Pt K) 6 4 = idM K 6 4 := by
  show (K.add (K.mul K.zero K.zero) (K.add (K.mul (K.neg K.one) K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.one) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) K.zero))))))))) = K.zero; lib_ring
theorem qp_6_5 : mmul (toSemi K) 9 (Qt K) (Pt K) 6 5 = idM K 6 5 := by
  show (K.add (K.mul K.zero K.zero) (K.add (K.mul (K.neg K.one) K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.one) (K.add (K.mul K.zero K.zero) K.zero))))))))) = K.zero; lib_ring
theorem qp_6_6 : mmul (toSemi K) 9 (Qt K) (Pt K) 6 6 = idM K 6 6 := by
  show (K.add (K.mul K.zero K.zero) (K.add (K.mul (K.neg K.one) (K.neg K.one)) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.one) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) K.zero))))))))) = K.one; lib_ring
theorem qp_6_7 : mmul (toSemi K) 9 (Qt K) (Pt K) 6 7 = idM K 6 7 := by
  show (K.add (K.mul K.zero K.zero) (K.add (K.mul (K.neg K.one) K.zero) (K.add (K.mul K.zero (K.neg K.one)) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.one) (K.add (K.mul K.zero K.zero) K.zero))))))))) = K.zero; lib_ring
theorem qp_6_8 : mmul (toSemi K) 9 (Qt K) (Pt K) 6 8 = idM K 6 8 := by
  show (K.add (K.mul K.zero K.zero) (K.add (K.mul (K.neg K.one) K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.one) K.zero))))))))) = K.zero; lib_ring
theorem qp_7_0 : mmul (toSemi K) 9 (Qt K) (Pt K) 7 0 = idM K 7 0 := by
  show (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul (K.neg K.one) K.zero) (K.add (K.mul K.zero K.one) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) K.zero))))))))) = K.zero; lib_ring
theorem qp_7_1 : mmul (toSemi K) 9 (Qt K) (Pt K) 7 1 = idM K 7 1 := by
  show (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul (K.neg K.one) K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.one) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) K.zero))))))))) = K.zero; lib_ring
theorem qp_7_2 : mmul (toSemi K) 9 (Qt K) (Pt K) 7 2 = idM K 7 2 := by
  show (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul (K.neg K.one) K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.one) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) K.zero))))))))) = K.zero; lib_ring
theorem qp_7_3 : mmul (toSemi K) 9 (Qt K) (Pt K) 7 3 = idM K 7 3 := by
  show (K.add (K.mul K.zero (K.neg K.one)) (K.add (K.mul K.zero K.zero) (K.add (K.mul (K.neg K.one) K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.one) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) K.zero))))))))) = K.zero; lib_ring
theorem qp_7_4 : mmul (toSemi K) 9 (Qt K) (Pt K) 7 4 = idM K 7 4 := by
  show (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul (K.neg K.one) K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.one) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) K.zero))))))))) = K.zero; lib_ring
theorem qp_7_5 : mmul (toSemi K) 9 (Qt K) (Pt K) 7 5 = idM K 7 5 := by
  show (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul (K.neg K.one) K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.one) (K.add (K.mul K.zero K.zero) K.zero))))))))) = K.zero; lib_ring
theorem qp_7_6 : mmul (toSemi K) 9 (Qt K) (Pt K) 7 6 = idM K 7 6 := by
  show (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero (K.neg K.one)) (K.add (K.mul (K.neg K.one) K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.one) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) K.zero))))))))) = K.zero; lib_ring
theorem qp_7_7 : mmul (toSemi K) 9 (Qt K) (Pt K) 7 7 = idM K 7 7 := by
  show (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul (K.neg K.one) (K.neg K.one)) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.one) (K.add (K.mul K.zero K.zero) K.zero))))))))) = K.one; lib_ring
theorem qp_7_8 : mmul (toSemi K) 9 (Qt K) (Pt K) 7 8 = idM K 7 8 := by
  show (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul (K.neg K.one) K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.one) K.zero))))))))) = K.zero; lib_ring
theorem qp_8_0 : mmul (toSemi K) 9 (Qt K) (Pt K) 8 0 = idM K 8 0 := by
  show (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.one) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.one K.zero) K.zero))))))))) = K.zero; lib_ring
theorem qp_8_1 : mmul (toSemi K) 9 (Qt K) (Pt K) 8 1 = idM K 8 1 := by
  show (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.one) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.one K.zero) K.zero))))))))) = K.zero; lib_ring
theorem qp_8_2 : mmul (toSemi K) 9 (Qt K) (Pt K) 8 2 = idM K 8 2 := by
  show (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.one) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.one K.zero) K.zero))))))))) = K.zero; lib_ring
theorem qp_8_3 : mmul (toSemi K) 9 (Qt K) (Pt K) 8 3 = idM K 8 3 := by
  show (K.add (K.mul K.zero (K.neg K.one)) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.one) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.one K.zero) K.zero))))))))) = K.zero; lib_ring
theorem qp_8_4 : mmul (toSemi K) 9 (Qt K) (Pt K) 8 4 = idM K 8 4 := by
  show (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.one) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.one K.zero) K.zero))))))))) = K.zero; lib_ring
theorem qp_8_5 : mmul (toSemi K) 9 (Qt K) (Pt K) 8 5 = idM K 8 5 := by
  show (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.one) (K.add (K.mul K.one K.zero) K.zero))))))))) = K.zero; lib_ring
theorem qp_8_6 : mmul (toSemi K) 9 (Qt K) (Pt K) 8 6 = idM K 8 6 := by
  show (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero (K.neg K.one)) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.one) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.one K.zero) K.zero))))))))) = K.zero; lib_ring
theorem qp_8_7 : mmul (toSemi K) 9 (Qt K) (Pt K) 8 7 = idM K 8 7 := by
  show (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero (K.neg K.one)) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.one) (K.add (K.mul K.one K.zero) K.zero))))))))) = K.zero; lib_ring
theorem qp_8_8 : mmul (toSemi K) 9 (Qt K) (Pt K) 8 8 = idM K 8 8 := by
  show (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.zero K.zero) (K.add (K.mul K.one K.one) K.zero))))))))) = K.one; lib_ring

theorem qp (i j : Nat) (hi : i < 3 + 6) (hj : j < 3 + 6) : mmul (toSemi K) (3 + 6) (Qt K) (Pt K) i j = idM K i j := by
  rcases LibSym22.nine i hi with rfl | rfl | rfl | rfl | rfl | rfl | rfl | rfl | rfl <;> rcases LibSym22.nine j hj with rfl | rfl | rfl | rfl | rfl | rfl | rfl | rfl | rfl
  · exact qp_0_0 K
  · exact qp_0_1 K
  · exact qp_0_2 K
  · exact qp_0_3 K
  · exact qp_0_4 K
  · exact qp_0_5 K
  · exact qp_0_6 K
  · exact qp_0_7 K
  · exact qp_0_8 K
  · exact qp_1_0 K
  · exact qp_1_1 K
  · exact qp_1_2 K
  · exact qp_1_3 K
  · exact qp_1_4 K
  · exact qp_1_5 K
  · exact qp_1_6 K
  · exact qp_1_7 K
  · exact qp_1_8 K
  · exact qp_2_0 K
  · exact qp_2_1 K
  · exact qp_2_2 K
  · exact qp_2_3 K
  · exact qp_2_4 K
  · exact qp_2_5 K
  · exact qp_2_6 K
  · exact qp_2_7 K
  · exact qp_2_8 K
  · exact qp_3_0 K
  · exact qp_3_1 K
  · exact qp_3_2 K
  · exact qp_3_3 K
  · exact qp_3_4 K
  · exact qp_3_5 K
  · exact qp_3_6 K
  · exact qp_3_7 K
  · exact qp_3_8 K
  · exact qp_4_0 K
  · exact qp_4_1 K
  · exact qp_4_2 K
  · exact qp_4_3 K
  · exact qp_4_4 K
  · exact qp_4_5 K
  · exact qp_4_6 K
  · exact qp_4_7 K
  · exact qp_4_8 K
  · exact qp_5_0 K
  · exact qp_5_1 K
  · exact qp_5_2 K
  · exact qp_5_3 K
  · exact qp_5_4 K
  · exact qp_5_5 K
  · exact qp_5_6 K
  · exact qp_5_7 K
  · exact qp_5_8 K
  · exact qp_6_0 K
  · exact qp_6_1 K
  · exact qp_6_2 K
  · exact qp_6_3 K
  · exact qp_6_4 K
  · exact qp_6_5 K
  · exact qp_6_6 K
  · exact qp_6_7 K
  · exact qp_6_8 K
  · exact qp_7_0 K
  · exact qp_7_1 K
  · exact qp_7_2 K
  · exact qp_7_3 K
  · exact qp_7_4 K
  · exact qp_7_5 K
  · exact qp_7_6 K
  · exact qp_7_7 K
  · exact qp_7_8 K
  · exact qp_8_0 K
  · exact qp_8_1 K
  · exact qp_8_2 K
  · exact qp_8_3 K
  · exact qp_8_4 K
  · exact qp_8_5 K
  · exact qp_8_6 K
  · exact qp_8_7 K
  · exact qp_8_8 K

/-- **tr(M⊗M)^k = tr(∧²M)^k + tr(Sym²M)^k.** -/
theorem tensor_sq3_psum (M : Nat → Nat → R) (k : Nat) :
    trace (toSemi K) (3 + 6) (mpow (toSemi K) (3 + 6) (MM K M) k)
      = K.add (trace (toSemi K) 3 (mpow (toSemi K) 3 (LibExt2Three.ext2 K M) k)) (trace (toSemi K) 6 (mpow (toSemi K) 6 (LibSym2Three.sym2 K M) k)) :=
  LibBlockSim.block_sim_psum K 3 6 (MM K M) (Pt K) (Qt K) (LibExt2Three.ext2 K M) (LibSym2Three.sym2 K M) (pq K) (low K M) (hx K M) (hy K M) (qp K) k

end LibTensorSq3Psum

#print axioms LibTensorSq3Psum.qp
#print axioms LibTensorSq3Psum.tensor_sq3_psum

lib/TensorSq4Lin.lean

lean_all/lib/TensorSq4Lin.lean

/-
  lib/TensorSq4Lin.lean — T46 shared library, cycle 605 (G332): agreement of the x coefficient for GL4 ⊗².

  any commutative ring, M any 4×4:
    sym2_lin4      ∃ Q, det(I − x Sym²M) = 1 − x(c₁² − c₂) + x²Q
    prod_lin4      ∃ Q, det(I − x∧²M)·det(I − x Sym²M) = 1 − x c₁² + x²Q    (same x coefficient as L(M⊗M) = 1 − x(tr M)² + …)
  Lean 4 core only, no axioms.
-/
import lib.Sym2Four

namespace LibTensorSq4Lin
open DetChain LibRing LibSum LibSemi LibMat LibDet LibEulerSum

variable {R : Type} (K : CRing R) (M : Nat → Nat → R) (x : R)

theorem sym2_lin4 : ∃ Q, det K 10 (imx K (LibSym2Four.sym2 K M) x) = K.add (K.add K.one (K.neg (K.mul x (K.add (K.mul (K.add (K.add (K.add (M 0 0) (M 1 1)) (M 2 2)) (M 3 3)) (K.add (K.add (K.add (M 0 0) (M 1 1)) (M 2 2)) (M 3 3))) (K.neg (K.add (K.add (K.add (K.add (K.add (K.add (K.mul (M 0 0) (M 1 1)) (K.neg (K.mul (M 0 1) (M 1 0)))) (K.add (K.mul (M 0 0) (M 2 2)) (K.neg (K.mul (M 0 2) (M 2 0))))  -- (+291 chars; full line in the Zenodo source)
  LibFunctorLin.gen_lin K x 10 (LibSym2Four.sym2 K M) _ (LibSym2Four.tr_sym2 K M)

theorem prod_lin4 : ∃ Q, K.mul (det K 6 (imx K (LibExt2Four.ext2 K M) x)) (det K 10 (imx K (LibSym2Four.sym2 K M) x))
    = K.add (K.add K.one (K.neg (K.mul x (K.mul (K.add (K.add (K.add (M 0 0) (M 1 1)) (M 2 2)) (M 3 3)) (K.add (K.add (K.add (M 0 0) (M 1 1)) (M 2 2)) (M 3 3)))))) (K.mul (K.mul x x) Q) := by
  obtain ⟨Q1, h1⟩ := LibFunctorLin.ext2_lin4 K M x
  obtain ⟨Q2, h2⟩ := sym2_lin4 K M x
  rw [h1, h2]
  generalize (K.add (K.add (K.add (M 0 0) (M 1 1)) (M 2 2)) (M 3 3)) = a; generalize (K.add (K.add (K.add (K.add (K.add (K.add (K.mul (M 0 0) (M 1 1)) (K.neg (K.mul (M 0 1) (M 1 0)))) (K.add (K.mul (M 0 0) (M 2 2)) (K.neg (K.mul (M 0 2) (M 2 0))))) (K.add (K.mul (M 0 0) (M 3 3)) (K.neg (K.mul (M 0 3) (M 3 0))))) (K.add (K.mul (M 1 1) (M 2 2)) (K.neg (K.mul (M 1 2) (M 2 1))))) (K.add (K.mul (M 1 1)  -- (+112 chars; full line in the Zenodo source)
  exact ⟨K.add (K.add (K.add Q1 Q2) (K.mul b (K.add (K.mul a a) (K.neg b)))) (K.add (K.add (K.neg (K.mul x (K.mul b Q2))) (K.neg (K.mul x (K.mul (K.add (K.mul a a) (K.neg b)) Q1)))) (K.mul (K.mul x x) (K.mul Q1 Q2))), by lib_ring⟩

end LibTensorSq4Lin

#print axioms LibTensorSq4Lin.sym2_lin4
#print axioms LibTensorSq4Lin.prod_lin4

lib/TernaryCount.lean

lean_all/lib/TernaryCount.lean

/-
  lib/TernaryCount.lean — T46 shared library, cycle 132: fast computation of representation numbers of ternary quadratic forms (= naive computation, proved).

    w z                  1 (z = 0), 2 (z > 0)   (sign weight)
    innerN c m r         Σ_{z<r} [c z² = m] w z            (naive)
    innerF c m r         check only z = isqrt (m / c)        (fast; m / c is the verified LibVal.qr)
    inner_eq             c > 0 ⇒ innerN = innerF
    Test: innerN = innerF at a few values by decide
  (Replacing the inner sum of totality2 bsd/Tunnell's cnt n b c (triple sum, linear isqrt; exceeds heartbeats on Mac) gives O(r³) → O(r² log).)

  Lean 4 core only, proofs by hand, no axioms.
-/
import lib.ISqrt
import lib.Valuation
import lib.Burnside

namespace LibTern
open LibMod LibVal LibISqrt

def w (z : Nat) : Nat := cond (Nat.beq z 0) 1 2

def innerN (c m : Nat) : Nat → Nat
  | 0 => 0
  | r + 1 => innerN c m r + cond (Nat.beq (c * (r * r)) m) (w r) 0

def innerF (c m r : Nat) : Nat :=
  cond (Nat.beq (qr c m).2 0)
    (cond (Nat.beq (isqrt (qr c m).1 * isqrt (qr c m).1) (qr c m).1)
      (cond (Nat.blt (isqrt (qr c m).1) r) (w (isqrt (qr c m).1)) 0) 0) 0

theorem sq_inj {a b : Nat} (h : a * a = b * b) : a = b := by
  cases Nat.lt_or_ge a b with
  | inl hl => exact absurd h (fun e => Nat.lt_irrefl _ (e ▸ Nat.mul_lt_mul_of_le_of_lt (Nat.le_of_lt hl) hl (Nat.lt_of_le_of_lt (Nat.zero_le a) hl)))
  | inr hg =>
    cases Nat.lt_or_eq_of_le hg with
    | inr he => exact he.symm
    | inl hl => exact absurd h.symm (fun e => Nat.lt_irrefl _ (e ▸ Nat.mul_lt_mul_of_le_of_lt (Nat.le_of_lt hl) hl (Nat.lt_of_le_of_lt (Nat.zero_le b) hl)))

/-- There is at most one z with c z² = m, and if it is out of range it is not in the sum. -/
theorem innerN_none (c m : Nat) (hc : 0 < c) : ∀ r, (∀ z, z < r → c * (z * z) ≠ m) → innerN c m r = 0
  | 0, _ => rfl
  | r + 1, h => by
    show innerN c m r + cond (Nat.beq (c * (r * r)) m) (w r) 0 = 0
    rw [innerN_none c m hc r (fun z hz => h z (Nat.lt_succ_of_lt hz))]
    cases hb : Nat.beq (c * (r * r)) m
    · rfl
    · exact absurd (LibMod.beq_true hb) (h r (Nat.lt_succ_self r))

theorem innerN_one (c m s : Nat) (hc : 0 < c) (hs : c * (s * s) = m) : ∀ r,
    innerN c m r = cond (Nat.blt s r) (w s) 0
  | 0 => by
    show 0 = cond (Nat.blt s 0) (w s) 0
    cases hb : Nat.blt s 0
    · rfl
    · exact absurd (Nat.le_of_ble_eq_true hb) (Nat.not_succ_le_zero s)
  | r + 1 => by
    show innerN c m r + cond (Nat.beq (c * (r * r)) m) (w r) 0 = cond (Nat.blt s (r + 1)) (w s) 0
    rw [innerN_one c m s hc hs r]
    cases Nat.lt_or_ge s r with
    | inl hl =>
      have h1 : Nat.blt s r = true := Nat.ble_eq_true_of_le hl
      have h2 : Nat.blt s (r + 1) = true := Nat.ble_eq_true_of_le (Nat.lt_succ_of_lt hl)
      have h3 : Nat.beq (c * (r * r)) m = false := by
        cases hb : Nat.beq (c * (r * r)) m
        · rfl
        · exfalso
          have e := LibMod.beq_true hb
          rw [← hs] at e
          have := sq_inj (LibBurnside.nat_cancel hc (r * r) (s * s) (by rw [show r * r * c = c * (r * r) by lib_nat, e]; lib_nat))
          subst this; exact Nat.lt_irrefl _ hl
      rw [h1, h2, h3]; rfl
    | inr hg =>
      have h1 : Nat.blt s r = false := by
        cases hb : Nat.blt s r
        · rfl
        · exact absurd (Nat.lt_of_lt_of_le (Nat.le_of_ble_eq_true hb) hg) (Nat.lt_irrefl _)
      rw [h1]
      cases Nat.lt_or_eq_of_le hg with
      | inr he =>
        rw [← he]
        have h2 : Nat.blt r (r + 1) = true := Nat.ble_eq_true_of_le (Nat.le_refl _)
        have h3 : Nat.beq (c * (r * r)) m = true := by rw [he, hs]; exact LibMod.beq_self m
        rw [h2, h3]; show 0 + w r = w r; lib_nat
      | inl hl =>
        have h2 : Nat.blt s (r + 1) = false := by
          cases hb : Nat.blt s (r + 1)
          · rfl
          · exact absurd (Nat.lt_of_lt_of_le (Nat.le_of_ble_eq_true hb) hl) (Nat.lt_irrefl _)
        have h3 : Nat.beq (c * (r * r)) m = false := by
          cases hb : Nat.beq (c * (r * r)) m
          · rfl
          · exfalso
            have e := LibMod.beq_true hb
            rw [← hs] at e
            have := sq_inj (LibBurnside.nat_cancel hc (r * r) (s * s) (by rw [show r * r * c = c * (r * r) by lib_nat, e]; lib_nat))
            subst this; exact Nat.lt_irrefl _ hl
        rw [h2, h3]; rfl

/-- **Fast inner sum = naive inner sum.** -/
theorem inner_eq (c m r : Nat) (hc : 0 < c) : innerN c m r = innerF c m r := by
  have ⟨e1, e2⟩ := qr_spec hc m
  unfold innerF
  rw [e1, e2]
  have hs := spec c m
  cases h0 : Nat.beq (md c m) 0
  · -- c ∤ m
    apply innerN_none c m hc r
    intro z _ hz
    have := md_zero_of_dvd hc ⟨z * z, hz.symm⟩
    rw [this] at h0; exact Bool.noConfusion h0
  · have hm0 : md c m = 0 := LibMod.beq_true h0
    rw [hm0] at hs
    generalize dv c m = q at hs ⊢
    cases h1 : Nat.beq (isqrt q * isqrt q) q
    · apply innerN_none c m hc r
      intro z _ hz
      have hq : z * z = q := LibBurnside.nat_cancel hc _ _ (by rw [show z * z * c = c * (z * z) by lib_nat, hz, hs]; lib_nat)
      have hiz : isqrt q = z := isqrt_unique (by rw [hq]; exact Nat.le_refl _)
        (by rw [← hq]; exact Nat.lt_of_lt_of_le (Nat.lt_succ_self _) (by lib_nat_le))
      rw [hiz, hq, LibMod.beq_self] at h1; exact Bool.noConfusion h1
    · have hsq := LibMod.beq_true h1
      exact innerN_one c m (isqrt q) hc (by rw [hsq, hs]; lib_nat) r

example : innerN 3 48 10 = innerF 3 48 10 := by decide
example : innerN 2 50 6 = innerF 2 50 6 := by decide

end LibTern

#print axioms LibTern.innerN_one
#print axioms LibTern.inner_eq

lib/TernaryCount2.lean

lean_all/lib/TernaryCount2.lean

/-
  lib/TernaryCount2.lean — T46 shared library, cycle 133: representation numbers of ternary quadratic forms — triple sum = double sum (proof).

    sR r f             Σ_{i<r} f i
    cntN n b c r       Σ_{x,y,z<r} [x² + b y² + c z² = n] w x w y w z      (simple, form of totality2 Tunnell.cnt)
    cntF n b c r       Σ_{x,y<r} (x² + b y² ≤ n ? w x w y · innerF c (n − x² − b y²) r : 0)
    cnt_eq             c > 0 ⇒ cntN = cntF
  Lean 4 core only, proofs by hand, no axioms.
-/
import lib.TernaryCount
import lib.NatSub

namespace LibTern2
open LibMod LibTern LibNatSub

def sR (r : Nat) (f : Nat → Nat) : Nat :=
  match r with
  | 0 => 0
  | r + 1 => sR r f + f r

theorem sR_ext (f g : Nat → Nat) (h : ∀ i, f i = g i) : ∀ r, sR r f = sR r g
  | 0 => rfl
  | r + 1 => by show sR r f + f r = sR r g + g r; rw [sR_ext f g h r, h r]

theorem sR_mul (k : Nat) (f : Nat → Nat) : ∀ r, sR r (fun i => k * f i) = k * sR r f
  | 0 => by show 0 = k * 0; lib_nat
  | r + 1 => by show sR r (fun i => k * f i) + k * f r = k * (sR r f + f r); rw [sR_mul k f r]; lib_nat

def ind (b : Bool) : Nat := cond b 1 0

def cntN (n b c r : Nat) : Nat :=
  sR r (fun x => sR r (fun y => sR r (fun z =>
    ind (Nat.beq (x * x + b * (y * y) + c * (z * z)) n) * (w x * w y * w z))))

def cntF (n b c r : Nat) : Nat :=
  sR r (fun x => sR r (fun y =>
    cond (Nat.ble (x * x + b * (y * y)) n) (w x * w y * innerF c (n - (x * x + b * (y * y))) r) 0))

/-- Sum over z: weighted sum for A + c z² = n. -/
theorem zsum (A c n : Nat) : ∀ r, sR r (fun z => ind (Nat.beq (A + c * (z * z)) n) * w z)
    = cond (Nat.ble A n) (innerN c (n - A) r) 0
  | 0 => by show 0 = cond (Nat.ble A n) 0 0; cases Nat.ble A n <;> rfl
  | r + 1 => by
    show sR r _ + ind (Nat.beq (A + c * (r * r)) n) * w r = cond (Nat.ble A n) (innerN c (n - A) r + cond (Nat.beq (c * (r * r)) (n - A)) (w r) 0) 0
    rw [zsum A c n r]
    cases hA : Nat.ble A n
    · -- A > n: A + c r² ≠ n
      have hb : Nat.beq (A + c * (r * r)) n = false := by
        cases hb : Nat.beq (A + c * (r * r)) n
        · rfl
        · exfalso
          have e := LibMod.beq_true hb
          have : A ≤ n := by rw [← e]; exact Nat.le_add_right _ _
          rw [Nat.ble_eq_true_of_le this] at hA; exact Bool.noConfusion hA
      rw [hb]; show 0 + 0 * w r = 0; lib_nat
    · have hle : A ≤ n := Nat.le_of_ble_eq_true hA
      show innerN c (n - A) r + ind (Nat.beq (A + c * (r * r)) n) * w r
        = innerN c (n - A) r + cond (Nat.beq (c * (r * r)) (n - A)) (w r) 0
      cases h1 : Nat.beq (c * (r * r)) (n - A)
      · have h2 : Nat.beq (A + c * (r * r)) n = false := by
          cases hb : Nat.beq (A + c * (r * r)) n
          · rfl
          · exfalso
            have e := LibMod.beq_true hb
            have : c * (r * r) = n - A := by rw [← e, add_sub_l]
            rw [this, LibMod.beq_self] at h1; exact Bool.noConfusion h1
        rw [h2]; show _ + 0 * w r = _ + 0; lib_nat
      · have e := LibMod.beq_true h1
        have h2 : Nat.beq (A + c * (r * r)) n = true := by
          rw [e, add_sub_of_le hle]; exact LibMod.beq_self n
        rw [h2]; show _ + 1 * w r = _ + w r; lib_nat

/-- **Triple sum = double sum.** -/
theorem cnt_eq (n b c r : Nat) (hc : 0 < c) : cntN n b c r = cntF n b c r := by
  unfold cntN cntF
  apply sR_ext; intro x
  apply sR_ext; intro y
  rw [sR_ext _ (fun z => (w x * w y) * (ind (Nat.beq (x * x + b * (y * y) + c * (z * z)) n) * w z))
    (fun z => by lib_nat), sR_mul, zsum, ← inner_eq c _ r hc]
  cases Nat.ble (x * x + b * (y * y)) n
  · show w x * w y * 0 = 0; lib_nat
  · rfl

example : cntN 41 2 8 7 = cntF 41 2 8 7 := by decide

end LibTern2

#print axioms LibTern2.zsum
#print axioms LibTern2.cnt_eq

lib/TernaryFF.lean

lean_all/lib/TernaryFF.lean

/-
  lib/TernaryFF.lean — T46 shared library, cycle 258: number of solutions of a ternary quadratic form ≡ 0 (mod p).

  finite field data (LibFFSum), q − 1 = 2h, a, b ≠ 0 (b·ib = 1), all c, d:
    ternary_zero   Σ_z #{(x, y) : a x² + b y² = d − c z²} = 0,  i.e. #{a x² + b y² + c z² = d} ≡ 0 (mod p)
  (quadratic-form version of Chevalley–Warning (degree 2 < 3 variables); TwoSqFF.form_count is a constant independent of z, so adding it q times gives 0.)
  Lean 4 core only, no axioms.
-/
import lib.TwoSqFF

namespace LibTernaryFF
open DetChain LibRing LibSum LibSemi LibBinom LibFFSum LibDist LibFFPoly LibPoly LibLegSum

variable {R : Type} [DecidableEq R] (K : CRing R)
variable (hK : NoZD K) (h10 : K.one ≠ K.zero) (els : List R) (hd : Dist els) (hall : ∀ x, x ∈ els) (n : Nat)
  (hlen : els.length = n + 2) (hfer : ∀ x, x ≠ K.zero → pw K x (n + 1) = K.one) (hq : castN K (n + 2) = K.zero)

omit [DecidableEq R] in
theorem sum_const_zero {A : Type} (c : R) (l : List A) (hl : sum (toSemi K) (fun _ => K.one) l = K.zero) :
    sum (toSemi K) (fun _ => c) l = K.zero := by
  rw [show sum (toSemi K) (fun _ => c) l = K.mul c (sum (toSemi K) (fun _ => K.one) l) by
    rw [LibLegHasse.mulS]; apply sum_ext (toSemi K); intro _; lib_ring, hl]; lib_ring

include hK h10 hd hall hlen hfer hq in
/-- **#{a x² + b y² + c z² = d} ≡ 0 (mod p).** -/
theorem ternary_zero (h : Nat) (hn : n + 1 = 2 * h) (a b c d ib : R) (ha : a ≠ K.zero) (hib : K.mul b ib = K.one) :
    sum (toSemi K) (fun z => sum (toSemi K) (fun x => sum (toSemi K)
      (fun y => indK (toSemi K) (decide (K.add (K.mul a (K.mul x x)) (K.mul b (K.mul y y)) = sub K d (K.mul c (K.mul z z))))) els) els) els
      = K.zero := by
  rw [sum_ext (toSemi K) _ _ (fun z => LibTwoSqFF.form_count K hK h10 els hd hall n hlen hfer hq h hn a b _ ib ha hib)]
  exact sum_const_zero K _ els (powsum_zero_k K els n hlen hq)

end LibTernaryFF

#print axioms LibTernaryFF.sum_const_zero
#print axioms LibTernaryFF.ternary_zero

lib/TernaryZ.lean

lean_all/lib/TernaryZ.lean

/-
  lib/TernaryZ.lean — T46 shared library, cycle 358 (G32): exact number of zeros of a diagonal ternary quadratic form.

  Finite-field data, q − 1 = n + 1 = 2h, integer χ, a, b, c ≠ 0, a⁻¹ = a^n:
    binary_count   d ≠ 0 ⇒ #{(x, y) : a x² + b y² = d} = q − χ(−a⁻¹b);  d = 0 ⇒ q + (q − 1)χ(−a⁻¹b)
    ternary_count  #{(x, y, z) : a x² + b y² + c z² = 0} = q²
  (Sum over y of the number of solutions of x² = a⁻¹(d − b y²) (cntSq). QuadPolyChi.quad_poly_ne/zero.)
  Lean 4 core only, no axioms.
-/
import lib.CycChar
import lib.QuadPolyChi

namespace LibTernaryZ
open DetChain LibRing LibSum LibSemi LibFFSum LibDist LibPoly LibLegSum LibEulerFF LibQuadCharFF LibQuadCharMul

variable {R : Type} [DecidableEq R] (K : CRing R)
variable (hK : NoZD K) (h10 : K.one ≠ K.zero) (els : List R) (hd : Dist els) (hall : ∀ x, x ∈ els) (n : Nat)
  (hlen : els.length = n + 2) (hfer : ∀ x, x ≠ K.zero → pw K x (n + 1) = K.one) (hq : castN K (n + 2) = K.zero)
  (h : Nat) (hn : n + 1 = 2 * h)

include hfer in
theorem inv_mul (a : R) (ha : a ≠ K.zero) : K.mul a (pw K a n) = K.one := by
  rw [← hfer a ha]; show K.mul a (pw K a n) = K.mul (pw K a n) a; exact K.mul_comm _ _

include hK h10 hd hall hlen hfer hq hn in
/-- Binary: Σ_y χ(a⁻¹(d − b y²)). -/
theorem binary_sum (a b d : R) (ha : a ≠ K.zero) (hb : b ≠ K.zero) :
    sum (toSemi LibInt.intR) (fun y => chi K h (K.mul (pw K a n) (K.add d (K.neg (K.mul b (K.mul y y)))))) els
      = cond (decide (d = K.zero)) (Int.mul (chi K h (K.neg (K.mul (pw K a n) b))) (Int.ofNat (n + 1)))
          (Int.neg (chi K h (K.neg (K.mul (pw K a n) b)))) := by
  let ai := pw K a n
  let bi := pw K b n
  have hbb : K.mul b bi = K.one := inv_mul K n hfer b hb
  have hf : ∀ E, E = K.neg (K.mul d bi) → ∀ y, K.mul ai (K.add d (K.neg (K.mul b (K.mul y y))))
      = K.mul (K.neg (K.mul ai b)) (K.add (K.mul (K.add y K.zero) (K.add y K.zero)) E) := by
    intro E hE y; rw [hE]
    calc K.mul ai (K.add d (K.neg (K.mul b (K.mul y y))))
        = K.add (K.mul ai (K.mul d K.one)) (K.neg (K.mul ai (K.mul b (K.mul y y)))) := by lib_ring
      _ = K.add (K.mul ai (K.mul d (K.mul b bi))) (K.neg (K.mul ai (K.mul b (K.mul y y)))) := by rw [hbb]
      _ = _ := by lib_ring
  by_cases hd0 : d = K.zero
  · rw [decide_eq_true hd0]
    have hE : K.neg (K.mul d bi) = K.zero := by rw [hd0]; lib_ring
    exact LibQuadPolyChi.quad_poly_zero K hK h10 els hd hall n hlen hfer h hn (K.neg (K.mul ai b)) K.zero _
      (fun y => by rw [hf K.zero hE.symm y])
  · rw [decide_eq_false hd0]
    have hE : K.neg (K.mul d bi) ≠ K.zero := fun e => hd0 (by
      calc d = K.neg (K.mul (K.neg (K.mul d bi)) b) := by rw [show K.neg (K.mul (K.neg (K.mul d bi)) b) = K.mul d (K.mul b bi) by lib_ring, hbb]; lib_ring
        _ = K.zero := by rw [e]; lib_ring)
    exact LibQuadPolyChi.quad_poly_ne K hK h10 els hd hall n hlen hfer hq h hn (K.neg (K.mul ai b)) K.zero _ hE _
      (fun y => hf _ rfl y)

include hK h10 hd hall hlen hfer hq hn in
/-- **#{(x, y, z) : a x² + b y² + c z² = 0} = q².** -/
theorem ternary_count (a b c : R) (ha : a ≠ K.zero) (hb : b ≠ K.zero) (hc : c ≠ K.zero) :
    Int.ofNat (sum natS (fun z => sum natS (fun y =>
        cntSq K els (K.mul (pw K a n) (K.add (K.neg (K.mul c (K.mul z z))) (K.neg (K.mul b (K.mul y y)))))) els) els)
      = Int.mul (Int.ofNat (n + 2)) (Int.ofNat (n + 2)) := by
  rw [cast_sum]
  let A := chi K h (K.neg (K.mul (pw K a n) b))
  have pt : ∀ z, Int.ofNat (sum natS (fun y =>
        cntSq K els (K.mul (pw K a n) (K.add (K.neg (K.mul c (K.mul z z))) (K.neg (K.mul b (K.mul y y)))))) els)
      = Int.add (Int.add (Int.ofNat (n + 2)) (Int.neg A))
          (Int.mul (indK (toSemi LibInt.intR) (decide (z = K.zero))) (Int.mul A (Int.ofNat (n + 2)))) := by
    intro z
    rw [point_count_Z K hK h10 els hd hall n hlen hfer hq h hn
      (fun y => K.mul (pw K a n) (K.add (K.neg (K.mul c (K.mul z z))) (K.neg (K.mul b (K.mul y y)))))]
    rw [binary_sum K hK h10 els hd hall n hlen hfer hq h hn a b _ ha hb]
    by_cases hz : z = K.zero
    · rw [decide_eq_true (show K.neg (K.mul c (K.mul z z)) = K.zero by rw [hz]; lib_ring), decide_eq_true hz]
      show Int.ofNat (n + 2) + A * Int.ofNat (n + 1) = Int.ofNat (n + 2) + -A + 1 * (A * Int.ofNat (n + 2))
      rw [show Int.ofNat (n + 2) = Int.ofNat (n + 1) + 1 from rfl]; lib_int
    · have hd0 : K.neg (K.mul c (K.mul z z)) ≠ K.zero := fun e => by
        have : K.mul c (K.mul z z) = K.zero := by rw [show K.mul c (K.mul z z) = K.neg (K.neg (K.mul c (K.mul z z))) by lib_ring, e]; lib_ring
        cases hK _ _ this with
        | inl h1 => exact hc h1
        | inr h1 => cases hK _ _ h1 with
          | inl h2 => exact hz h2
          | inr h2 => exact hz h2
      rw [decide_eq_false hd0, decide_eq_false hz]
      show Int.ofNat (n + 2) + -A = Int.ofNat (n + 2) + -A + 0 * (A * Int.ofNat (n + 2)); lib_int
  rw [sum_ext (toSemi LibInt.intR) _ _ pt, LibConicZ.sadd]
  have s1 : sum (toSemi LibInt.intR) (fun _ => Int.add (Int.ofNat (n + 2)) (Int.neg A)) els
      = Int.mul (Int.add (Int.ofNat (n + 2)) (Int.neg A)) (Int.ofNat (n + 2)) := by
    generalize Int.add (Int.ofNat (n + 2)) (Int.neg A) = t
    calc sum (toSemi LibInt.intR) (fun _ => t) els = sum (toSemi LibInt.intR) (fun _ => Int.mul t 1) els :=
          sum_ext (toSemi LibInt.intR) _ _ (fun _ => by show t = t * 1; lib_int) _
      _ = Int.mul t (sum (toSemi LibInt.intR) (fun _ => (1 : Int)) els) := (mul_sum (toSemi LibInt.intR) t _ els).symm
      _ = Int.mul t (Int.ofNat (n + 2)) := by rw [LibQuadPolyChi.sum_one_int, hlen]
  have s2 : sum (toSemi LibInt.intR) (fun z => Int.mul (indK (toSemi LibInt.intR) (decide (z = K.zero))) (Int.mul A (Int.ofNat (n + 2)))) els
      = Int.mul A (Int.ofNat (n + 2)) :=
    pickK (toSemi LibInt.intR) els K.zero (LibCharOrth.onehot_of_dist LibInt.intR K.zero els hd (hall _)) (fun _ => Int.mul A (Int.ofNat (n + 2)))
  rw [s1, s2]
  generalize A = t
  show (Int.ofNat (n + 2) + -t) * Int.ofNat (n + 2) + t * Int.ofNat (n + 2) = Int.ofNat (n + 2) * Int.ofNat (n + 2)
  lib_int

end LibTernaryZ

#print axioms LibTernaryZ.binary_sum
#print axioms LibTernaryZ.ternary_count

lib/Theta11.lean

lean_all/lib/Theta11.lean

/-
  lib/Theta11.lean — T46 shared library (G1520e): the theta series of x² + xy + 3y² is an Eisenstein series.

    chi11_cases    χ₋₁₁(n) ∈ {1, −1, 0}, and χ₋₁₁(n) = 0 ⇒ 11 ∣ n
    odd_prime_k    p prime, ¬ p ∣ 2 ⇒ p = k m + 1 for some m      (p = 2(m+1) + 1)
    prime_data     every prime is split (χ = 1, p = N(π)), inert (χ = −1) or ramified (p = 11 = N(−1, 2))
    RH_one         RH 2 1 = 1
    theta11_half   n > 0 ⇒ RH(n) = Σ_{d ∣ n} χ₋₁₁(d)
    theta11        n > 0 ⇒ #{(x, y) ∈ ℤ² : x² + xy + 3y² = n} = 2 Σ_{d ∣ n} χ₋₁₁(d)
  Lean 4 core only, proofs by hand, no axioms.
-/
import lib.QR11
import lib.NormPiF

namespace LibTheta11
open LibMod LibGcd LibIntBox LibNormPiA LibNormPiB LibNormPiF LibChi11

def chkCases : Bool := allB 11 (fun r => ieq (F11 r) 1 || ieq (F11 r) (-1) || (ieq (F11 r) 0 && Nat.beq r 0))

theorem chkCases_true : chkCases = true := by decide

theorem ieq_eq {a b : Int} (h : ieq a b = true) : a = b := by
  cases e : Int.decEq a b with
  | isTrue h' => exact h'
  | isFalse h' => rw [ieq_false h'] at h; exact Bool.noConfusion h

theorem or3 {a b c : Bool} (h : (a || b || c) = true) : a = true ∨ b = true ∨ c = true := by
  cases a <;> cases b <;> cases c <;> first | exact Or.inl rfl | exact Or.inr (Or.inl rfl) | exact Or.inr (Or.inr rfl) | exact Bool.noConfusion h

theorem chi11_cases (n : Nat) : chi11 n = 1 ∨ chi11 n = -1 ∨ (chi11 n = 0 ∧ 11 ∣ n) := by
  have hr : md 11 n < 11 := md_lt (by decide) n
  have t := allB_spec 11 _ chkCases_true (md 11 n) hr
  rcases or3 t with h | h | h
  · exact Or.inl (ieq_eq h)
  · exact Or.inr (Or.inl (ieq_eq h))
  · obtain ⟨h1, h2⟩ := LibTwoSqPiQ.and_split h
    exact Or.inr (Or.inr ⟨ieq_eq h1, dvd_of_md_zero (Nat.eq_of_beq_eq_true h2)⟩)

theorem odd_prime_k {p : Nat} (hP : LibPrimePow.Prime p) (h2 : ¬ p ∣ 2) : ∃ m, p = LibEulerCrit.k m + 1 := by
  have s := spec 2 p
  have hr := md_lt (n := 2) (by decide) p
  generalize dv 2 p = q at s
  generalize md 2 p = r at s hr
  match r, hr with
  | 0, _ =>
    have hd : 2 ∣ p := ⟨q, by rw [s]; lib_nat⟩
    have p2 : LibPrimePow.Prime 2 := LibPrimeB.prime_of_isPrimeB (by decide)
    cases p2.2 p (by
      cases hP.2 2 hd with
      | inl e => exact absurd e (by decide)
      | inr e => rw [← e]; exact ⟨1, rfl⟩) with
    | inl e => exact absurd (e ▸ hP.1) (Nat.lt_irrefl 1)
    | inr e => exact absurd ⟨1, by rw [e]⟩ h2
  | 1, _ =>
    match q, s with
    | 0, s => exact absurd (s ▸ hP.1 : 1 < 2 * 0 + 1) (Nat.lt_irrefl 1)
    | m + 1, s => exact ⟨m, s⟩
  | r + 2, hr => exact absurd (Nat.lt_of_succ_lt_succ (Nat.lt_of_succ_lt_succ hr)) (Nat.not_lt_zero r)

theorem p11_eq {p : Nat} (hP : LibPrimePow.Prime p) (h : 11 ∣ p) : p = 11 := by
  have p11 : LibPrimePow.Prime 11 := LibPrimeB.prime_of_isPrimeB (by decide)
  cases hP.2 11 h with
  | inl e => exact absurd e (by decide)
  | inr e => exact e.symm

theorem ndvd11 {p : Nat} (hP : LibPrimePow.Prime p) (hne : p ≠ 11) : ¬ p ∣ 11 := fun hd => by
  have p11 : LibPrimePow.Prime 11 := LibPrimeB.prime_of_isPrimeB (by decide)
  cases p11.2 p hd with
  | inl e => exact absurd (e ▸ hP.1) (Nat.lt_irrefl 1)
  | inr e => exact hne e

theorem n11_2 : ¬ 11 ∣ 2 := fun hd => by
  have p2 : LibPrimePow.Prime 2 := LibPrimeB.prime_of_isPrimeB (by decide)
  cases p2.2 11 hd with
  | inl e => exact absurd e (by decide)
  | inr e => exact absurd e (by decide)

theorem prime_data (p : Nat) (hP : LibPrimePow.Prime p) :
    (chi11 p = 1 ∧ (∃ u v : Int, Nf (2 + 1) u v = (p : Int)) ∧ ¬ p ∣ 4 * 2 + 3)
      ∨ (chi11 p = -1 ∧ ∀ x y : Int, IDv (p : Int) (Nf (2 + 1) x y) → IDv (p : Int) x ∧ IDv (p : Int) y)
      ∨ (chi11 p = 0 ∧ (∃ u v : Int, Nf (2 + 1) u v = (p : Int)) ∧ ¬ p ∣ 2 ∧ p ∣ 4 * 2 + 3) := by
  rcases chi11_cases p with hc | hc | ⟨hc, hd⟩
  · have hne : p ≠ 11 := fun e => by rw [e] at hc; exact absurd hc (by decide)
    have h2 : ¬ p ∣ 2 := fun hd2 => by
      have p2 : LibPrimePow.Prime 2 := LibPrimeB.prime_of_isPrimeB (by decide)
      cases p2.2 p hd2 with
      | inl e => exact absurd (e ▸ hP.1) (Nat.lt_irrefl 1)
      | inr e => rw [e] at hc; exact absurd hc (by decide)
    obtain ⟨m, hm⟩ := odd_prime_k hP h2
    have hp1 : LibZp.Prime1 (LibEulerCrit.k m) := LibPrimeB.prime1_of_prime (hm ▸ hP)
    obtain ⟨t, ht⟩ := LibQR11.root11 m hp1 (hm ▸ hne) (hm ▸ hc)
    rw [← hm] at ht
    exact Or.inl ⟨hc, LibSplit11.pi_of_root11 hP h2 t ht, ndvd11 hP hne⟩
  · exact Or.inr (Or.inl ⟨hc, inert11 hP hc⟩)
  · have e := p11_eq hP hd
    subst e
    exact Or.inr (Or.inr ⟨hc, ⟨-1, 2, rfl⟩, n11_2, ⟨1, rfl⟩⟩)

set_option maxRecDepth 100000 in
theorem RH_one : RH 2 1 = 1 := by decide

theorem theta11_half {n : Nat} (hn : 0 < n) :
    (RH 2 n : Int) = LibEisGen.e LibInt.intR chi11 (LibDivFun.one1 LibInt.intR) n :=
  theta_eis 2 chi11 chi11_cmult prime_data RH_one hn

theorem theta11 {n : Nat} (hn : 0 < n) :
    (RZ 2 n : Int) = 2 * LibEisGen.e LibInt.intR chi11 (LibDivFun.one1 LibInt.intR) n := by
  rw [RZ_eq_RH 2 hn, LibTwoSqJacobi.cast_add, theta11_half hn]; lib_int

end LibTheta11

#print axioms LibTheta11.chi11_cases
#print axioms LibTheta11.odd_prime_k
#print axioms LibTheta11.n11_2
#print axioms LibTheta11.ndvd11
#print axioms LibTheta11.p11_eq
#print axioms LibTheta11.prime_data
#print axioms LibTheta11.RH_one
#print axioms LibTheta11.theta11_half
#print axioms LibTheta11.theta11

lib/Theta15.lean

lean_all/lib/Theta15.lean

/-
  lib/Theta15.lean — T46 shared library (G1557d): the genus theta series of discriminant −15 (class number 2,
  odd discriminant with an xy term), an instance of ThetaGenus2:

      r₁(n) + r₂(n) = 2 Σ_{d ∣ n} χ₋₁₅(d),   r₁(n) − r₂(n) = 2 Σ_{d ∣ n} χ₋₃(d) χ₅(n/d),
      r₁ = #{x² + xy + 4y² = n},  r₂ = #{2x² + xy + 2y² = n},  n > 0.

    data15         the LocalData at every prime (split: Pair15A / Pair15B / Pair15Two at p = 2, inert: Root15,
                   ramified 3 and 5: Ram15)
    theta15        r₁ + r₂ = 2E(χ₋₁₅, 1)
    theta15D       r₁ − r₂ = 2E(χ₋₃, χ₅)
    r1_formula15   2r₁ = 2E(χ₋₁₅, 1) + 2E(χ₋₃, χ₅)
  Lean 4 core only, proofs by hand, no axioms.
-/
import lib.ThetaGenus2
import lib.Ram15
import lib.Pair15Two
import lib.Theta24D
import lib.Theta40

namespace LibTheta15
open LibMod LibGcd LibIntBox LibNormPiA LibNormPiC LibChi11 LibFormCount LibFormRel LibSplit15 LibComp15 LibRoot15
  LibPair15A LibPair15B LibPair15Two LibRam15 LibThetaGenus2

/-- genus-character values on the residues mod 15 -/
def chkG : Bool := allB 15 (fun r =>
  (!(inL2 r 1 4) || (ieq (LibTheta24D.F3 (md 3 r) * LibTheta20D.F5 (md 5 r)) 1 && ieq (LibTheta24D.F3 (md 3 r) + LibTheta20D.F5 (md 5 r)) 2)) &&
  (!(inL2 r 2 8) || (ieq (LibTheta24D.F3 (md 3 r) * LibTheta20D.F5 (md 5 r)) 1 && ieq (LibTheta24D.F3 (md 3 r) + LibTheta20D.F5 (md 5 r)) (-2))) &&
  (!(ieq (F15 r) (-1)) || (ieq (LibTheta24D.F3 (md 3 r) * LibTheta20D.F5 (md 5 r)) (-1) && ieq (LibTheta24D.F3 (md 3 r) + LibTheta20D.F5 (md 5 r)) 0)) &&
  (ieq (F15 r) 1 || ieq (F15 r) (-1) || (ieq (F15 r) 0 && (Nat.beq (md 3 r) 0 || Nat.beq (md 5 r) 0))))

set_option maxRecDepth 100000 in
theorem chkG_true : chkG = true := by decide

theorem vals (p : Nat) : LibTheta24D.chim3 p = LibTheta24D.F3 (md 3 (md 15 p)) ∧ LibTheta20D.chi5 p = LibTheta20D.F5 (md 5 (md 15 p)) := by
  unfold LibTheta24D.chim3 LibTheta20D.chi5
  rw [md3_of_md15, md5_of_md15]; exact ⟨rfl, rfl⟩

theorem prime_eq {p q : Nat} (hP : LibPrimePow.Prime p) (hQ : LibPrimePow.Prime q) (d : q ∣ p) : p = q := by
  cases hP.2 q d with
  | inl e => exact absurd (e ▸ hQ.1) (Nat.lt_irrefl 1)
  | inr e => exact e.symm

theorem ndvd_small {p q : Nat} (hP : LibPrimePow.Prime p) (hQ : LibPrimePow.Prime q) (hq : ¬ q ∣ p) : ¬ p ∣ q := fun hd => by
  cases hQ.2 p hd with
  | inl e => exact absurd (e ▸ hP.1) (Nat.lt_irrefl 1)
  | inr e => exact hq (by rw [← e]; exact ⟨1, by lib_nat⟩)

theorem dec2 (p : Nat) : 2 ∣ p ∨ ¬ 2 ∣ p := by
  cases e : Nat.beq (md 2 p) 0
  · exact Or.inr (fun hd => by rw [md_zero_of_dvd (by decide) hd] at e; exact Bool.noConfusion e)
  · exact Or.inl (dvd_of_md_zero (Nat.eq_of_beq_eq_true e))

/-- **Local data at every prime** for (x² + xy + 4y², 2x² + xy + 2y²). -/
theorem data15 (p : Nat) (hP : LibPrimePow.Prime p) : LocalData f1 f2 chi15 LibTheta24D.chim3 LibTheta20D.chi5 p := by
  obtain ⟨tA, tB, tC, tD⟩ := LibTheta40.and4 (allB_spec _ _ chkG_true (md 15 p) (md_lt (by decide) p))
  obtain ⟨v3, v5⟩ := vals p
  rcases LibTheta11.or3 tD with h1 | hm1 | h0
  · have hc : chi15 p = 1 := eq_of_ieq h1
    have h35 := prime_to_15 (n := p) (by rw [hc]; decide)
    have h3' := ndvd_small hP LibSplit15.P3 h35.1
    have h5' := ndvd_small hP LibSplit15.P5 h35.2
    obtain ⟨u, v, h | h⟩ := split15 hP hc
    · have h2 : ¬ 2 ∣ p := fun hd => by
        have e := prime_eq hP LibRoot15.P2 hd
        subst e
        exact not_both h (show f2 1 0 = ((2 : Nat) : Int) from LibPair15Two.hp) h35.1 h35.2
      have h2' := LibRoot20.ndvd2 hP h2
      have hv := LibTheta40.two_vals (LibTheta40.imp tA (gen1 h h35.1 h35.2))
      exact LocalData.split11 ⟨pairA11 h hP.1 hP.2 h3' h5'⟩ ⟨pairA22 h hP.1 hP.2 h2' h3' h5'⟩ hc
        (by rw [v3, v5]; exact hv.1) (by rw [v3, v5]; exact hv.2)
    · have hv := LibTheta40.two_vals (LibTheta40.imp tB (gen2 h h35.1 h35.2))
      cases dec2 p with
      | inl hd =>
        have e := prime_eq hP LibRoot15.P2 hd
        subst e
        exact LocalData.split12 ⟨pairT12⟩ ⟨pairT21⟩ hc (by decide) (by decide)
      | inr h2 =>
        have h2' := LibRoot20.ndvd2 hP h2
        exact LocalData.split12 ⟨pairB12 h hP.1 hP.2 h2' h3' h5'⟩ ⟨pairB21 h hP.1 hP.2 h2' h3' h5'⟩ hc
          (by rw [v3, v5]; exact hv.1) (by rw [v3, v5]; exact hv.2)
  · have hc : chi15 p = -1 := eq_of_ieq hm1
    have hv := LibTheta40.two_vals' (LibTheta40.imp tC hm1)
    exact LocalData.inert (inert15_f1 hP hc) (inert15_f2 hP hc) hc (by rw [v3, v5]; exact hv.1) (by rw [v3, v5]; exact hv.2)
  · obtain ⟨_, hd⟩ := LibTwoSqPiQ.and_split h0
    rcases LibGenus20.or2' hd with e | e
    · have e3 := prime_eq hP LibSplit15.P3 (dvd_of_md_zero (by rw [md3_of_md15]; exact Nat.eq_of_beq_eq_true e))
      subst e3
      exact LocalData.ramSwap (fun n => ram3 n) (by decide) (by decide) (by decide)
    · have e5 := prime_eq hP LibSplit15.P5 (dvd_of_md_zero (by rw [md5_of_md15]; exact Nat.eq_of_beq_eq_true e))
      subst e5
      exact LocalData.ramSwap (fun n => ram5 n) (by decide) (by decide) (by decide)

theorem RF1_one : RF f1 1 = 2 := by decide
theorem RF2_one : RF f2 1 = 0 := by decide

/-- **Genus theta series of discriminant −15.** n > 0 ⇒ #{x² + xy + 4y² = n} + #{2x² + xy + 2y² = n} = 2Σ_{d∣n} χ₋₁₅(d). -/
theorem theta15 {n : Nat} (hn : 0 < n) :
    LibThetaGenus2.R f1 f2 n = 2 * LibEisGen.e LibInt.intR chi15 (LibDivFun.one1 LibInt.intR) n :=
  theta_sum f1_bound f2_bound f1_s f2_s chi15_cmult RF1_one RF2_one data15 hn

/-- **The genus difference.** n > 0 ⇒ #{x² + xy + 4y² = n} − #{2x² + xy + 2y² = n} = 2Σ_{d∣n} χ₋₃(d)χ₅(n/d). -/
theorem theta15D {n : Nat} (hn : 0 < n) :
    LibThetaGenus2.D f1 f2 n = 2 * LibEisGen.e LibInt.intR LibTheta24D.chim3 LibTheta20D.chi5 n :=
  theta_diff f1_bound f2_bound f1_s f2_s LibTheta24D.chim3_cmult LibTheta20D.chi5_cmult RF1_one RF2_one data15 hn

/-- **x² + xy + 4y².** n > 0 ⇒ 2#{x² + xy + 4y² = n} = 2Σχ₋₁₅(d) + 2Σχ₋₃(d)χ₅(n/d). -/
theorem r1_formula15 {n : Nat} (hn : 0 < n) :
    2 * (RF f1 n : Int) = 2 * LibEisGen.e LibInt.intR chi15 (LibDivFun.one1 LibInt.intR) n
      + 2 * LibEisGen.e LibInt.intR LibTheta24D.chim3 LibTheta20D.chi5 n :=
  r1_formula f1_bound f2_bound f1_s f2_s chi15_cmult LibTheta24D.chim3_cmult LibTheta20D.chi5_cmult RF1_one RF2_one data15 hn

end LibTheta15

#print axioms LibTheta15.chkG_true
#print axioms LibTheta15.data15
#print axioms LibTheta15.theta15
#print axioms LibTheta15.theta15D
#print axioms LibTheta15.r1_formula15

lib/Theta20.lean

lean_all/lib/Theta20.lean

/-
  lib/Theta20.lean — T46 shared library (G1547e2): the genus theta series of discriminant −20 (class number 2)
  is an Eisenstein series:
      r₁(n) + r₂(n) = 2 Σ_{d ∣ n} χ₋₂₀(d),   r₁ = #{x² + 5y² = n},  r₂ = #{2x² + 2xy + 3y² = n},  n > 0.

    R n            r₁(n) + r₂(n) (as Int)
    eig_of         a(pn) = (c+1)a(n) (p ∤ n) and a(p²m) + c·a(m) = (c+1)a(pm)  ⇒  T_p a = (c + 1) a   (c = χ(p))
    split_R        χ(p) = 1:  p = f₁ (Pair20A) or p = f₂ (Pair20B)
    inert_R        χ(p) = −1: Root20.inert20_f1/f2 + FormRam.inert_*
    ram5_R, ram2_R χ(p) = 0:  p = 5 (√−5 ·) and p = 2 (I = (2, 1 + √−5), swaps the two forms)
    R_one          R 1 = 2
    theta20        n > 0 ⇒ R n = 2 · Σ_{d ∣ n} χ₋₂₀(d)
  Lean 4 core only, proofs by hand, no axioms.
-/
import lib.Pair20B
import lib.FormRam
import lib.HeckeScale

namespace LibTheta20
open LibMod LibGcd LibIntBox LibNormPiA LibNormPiC LibComp20 LibFormCount LibFormRel LibFormRam LibPair20A LibPair20B LibRoot20 LibChi11

def R (n : Nat) : Int := (RF f1 n : Int) + (RF f2 n : Int)

theorem eig_of (a : Nat → Int) {p : Nat} (hp0 : 0 < p) (c : Int)
    (h1 : ∀ n, ¬ p ∣ n → a (p * n) = (c + 1) * a n)
    (h2 : ∀ m, a (p * (p * m)) + c * a m = (c + 1) * a (p * m)) (n : Nat) :
    LibHeckeOp.hk LibInt.intR p (LibInt.intR.mul c (LibDivFun.one1 LibInt.intR p)) a n
      = LibInt.intR.mul (LibInt.intR.add c (LibDivFun.one1 LibInt.intR p)) (a n) := by
  unfold LibHeckeOp.hk
  cases eb : Nat.beq (md p n) 0
  · have hn : ¬ p ∣ n := fun hd => by
      rw [md_zero_of_dvd hp0 hd, LibMod.beq_self] at eb; exact Bool.noConfusion eb
    show a (p * n) + 0 = (c + 1) * a n
    rw [h1 n hn]; lib_int
  · have sn : n = p * dv p n := by
      have s := spec p n
      rw [Nat.eq_of_beq_eq_true eb, Nat.add_zero] at s; exact s
    show a (p * n) + c * 1 * a (dv p n) = (c + 1) * a n
    generalize dv p n = m at sn ⊢
    subst sn
    have e := h2 m
    generalize a (p * (p * m)) = X at e ⊢
    generalize a m = Y at e ⊢
    generalize a (p * m) = Z at e ⊢
    rw [show X = (c + 1) * Z - c * Y by rw [← e]; lib_int]; lib_int

theorem cast2 (x y : Nat) : ((x + y : Nat) : Int) = (x : Int) + (y : Int) := rfl

section Split
variable {p : Nat} (hP : LibPrimePow.Prime p) (h2 : ¬ p ∣ 2) (h5 : ¬ p ∣ 5)

include hP h2 h5 in
theorem split_R {u v : Int} (huv : f1 u v = (p : Int) ∨ f2 u v = (p : Int)) :
    (∀ n, ¬ p ∣ n → R (p * n) = (1 + 1) * R n) ∧ (∀ m, R (p * (p * m)) + 1 * R m = (1 + 1) * R (p * m)) := by
  rcases huv with h | h
  · let P11 := pairA11 h hP.1 hP.2 h2 h5
    let P22 := pairA22 h hP.1 hP.2 h2 h5
    refine ⟨fun n hn => ?_, fun m => ?_⟩
    · unfold R; rw [split_ndvd P11 hn, split_ndvd P22 hn, cast2, cast2]; lib_int
    · have e1 := congrArg (fun z : Nat => (z : Int)) (split_dvd P11 m)
      have e2 := congrArg (fun z : Nat => (z : Int)) (split_dvd P22 m)
      simp only [cast2] at e1 e2
      unfold R
      generalize (RF f1 (p * (p * m)) : Int) = A1 at e1 ⊢
      generalize (RF f1 m : Int) = B1 at e1 ⊢
      generalize (RF f1 (p * m) : Int) = C1 at e1 ⊢
      generalize (RF f2 (p * (p * m)) : Int) = A2 at e2 ⊢
      generalize (RF f2 m : Int) = B2 at e2 ⊢
      generalize (RF f2 (p * m) : Int) = C2 at e2 ⊢
      rw [show A1 = C1 + C1 - B1 by rw [← e1]; lib_int, show A2 = C2 + C2 - B2 by rw [← e2]; lib_int]; lib_int
  · let P12 := pairB12 h hP.1 hP.2 h2 h5
    let P21 := pairB21 h hP.1 hP.2 h2 h5
    refine ⟨fun n hn => ?_, fun m => ?_⟩
    · unfold R; rw [split_ndvd P12 hn, split_ndvd P21 hn, cast2, cast2]; lib_int
    · have e1 := congrArg (fun z : Nat => (z : Int)) (split_dvd P12 m)
      have e2 := congrArg (fun z : Nat => (z : Int)) (split_dvd P21 m)
      simp only [cast2] at e1 e2
      unfold R
      generalize (RF f1 (p * (p * m)) : Int) = A1 at e1 ⊢
      generalize (RF f1 m : Int) = B1 at e1 ⊢
      generalize (RF f1 (p * m) : Int) = C1 at e1 e2 ⊢
      generalize (RF f2 (p * (p * m)) : Int) = A2 at e2 ⊢
      generalize (RF f2 m : Int) = B2 at e2 ⊢
      generalize (RF f2 (p * m) : Int) = C2 at e1 e2 ⊢
      rw [show A1 = C2 + C2 - B1 by rw [← e1]; lib_int, show A2 = C1 + C1 - B2 by rw [← e2]; lib_int]; lib_int

end Split

theorem inert_R {p : Nat} (hP : LibPrimePow.Prime p) (hc : chi20 p = -1) :
    (∀ n, ¬ p ∣ n → R (p * n) = (-1 + 1) * R n) ∧ (∀ m, R (p * (p * m)) + -1 * R m = (-1 + 1) * R (p * m)) := by
  have hp0 : 0 < p := Nat.lt_trans Nat.zero_lt_one hP.1
  refine ⟨fun n hn => ?_, fun m => ?_⟩
  · unfold R
    rw [inert_zero hp0 f1_s (inert20_f1 hP hc) hn, inert_zero hp0 f2_s (inert20_f2 hP hc) hn]
    generalize (RF f1 n : Int) = X; generalize (RF f2 n : Int) = Y
    show (0 : Int) + 0 = (-1 + 1) * (X + Y); lib_int
  · unfold R
    rw [inert_scale hp0 f1_s f1_bound (inert20_f1 hP hc) m, inert_scale hp0 f2_s f2_bound (inert20_f2 hP hc) m]
    generalize (RF f1 m : Int) = X; generalize (RF f2 m : Int) = Y
    generalize (RF f1 (p * m) : Int) = Z; generalize (RF f2 (p * m) : Int) = W; lib_int

theorem five_P : LibPrimePow.Prime 5 := LibPrimeB.prime_of_isPrimeB (by decide)
theorem two_P : LibPrimePow.Prime 2 := LibPrimeB.prime_of_isPrimeB (by decide)
theorem three_P : LibPrimePow.Prime 3 := LibPrimeB.prime_of_isPrimeB (by decide)
theorem n5_2 : ¬ 5 ∣ 2 := fun hd => by
  cases two_P.2 5 hd with
  | inl e => exact absurd e (by decide)
  | inr e => exact absurd e (by decide)
theorem n2_3 : ¬ 2 ∣ 3 := fun hd => by
  cases three_P.2 2 hd with
  | inl e => exact absurd e (by decide)
  | inr e => exact absurd e (by decide)

/-- p = 5: RF f₁ (5n) = RF f₁ n and RF f₂ (5n) = RF f₂ n -/
theorem ram5 (n : Nat) : RF f1 (5 * n) = RF f1 n ∧ RF f2 (5 * n) = RF f2 n := by
  refine ⟨ram_gen f1 f1 (A11 0 1) (B11 0 1) (p := 5) (by decide) n f1_bound f1_bound
    (fun x y => by unfold A11 f1; dsimp only; rw [LibGenus20.c5]; lib_int)
    (fun x y => by unfold B11 f1; dsimp only; rw [LibGenus20.c5]; lib_int)
    (fun x y => by unfold A11 B11; dsimp only; rw [LibGenus20.c5]; lib_int)
    (fun x y => by unfold A11 B11; dsimp only; rw [LibGenus20.c5]; lib_int)
    (fun x y => by unfold A11 B11; dsimp only; rw [LibGenus20.c5]; lib_int)
    (fun x y => by unfold A11 B11; dsimp only; rw [LibGenus20.c5]; lib_int)
    (fun k u v => by unfold A11; dsimp only; lib_int) (fun k u v => by unfold A11; dsimp only; lib_int) f1_s
    (fun x y hF => ?_),
    ram_gen f2 f2 (A22 0 1) (B22 0 1) (p := 5) (by decide) n f2_bound f2_bound
    (fun x y => by unfold A22 f2; dsimp only; rw [LibGenus20.c5]; lib_int)
    (fun x y => by unfold B22 f2; dsimp only; rw [LibGenus20.c5]; lib_int)
    (fun x y => by unfold A22 B22; dsimp only; rw [LibGenus20.c5]; lib_int)
    (fun x y => by unfold A22 B22; dsimp only; rw [LibGenus20.c5]; lib_int)
    (fun x y => by unfold A22 B22; dsimp only; rw [LibGenus20.c5]; lib_int)
    (fun x y => by unfold A22 B22; dsimp only; rw [LibGenus20.c5]; lib_int)
    (fun k u v => by unfold A22; dsimp only; lib_int) (fun k u v => by unfold A22; dsimp only; lib_int) f2_s
    (fun x y hF => ?_)⟩
  · have hx2 : IDv ((5 : Nat) : Int) (x * x) := idv_eq (idv_lin hF (idv_self ((5 : Nat) : Int)) 1 (-(y * y)))
      (by unfold f1; rw [LibGenus20.c5]; lib_int)
    have hx := (euclid_int five_P.2 hx2).elim id id
    show (dB 5 (x * 0 + 5 * (y * 1)) && dB 5 (y * 0 - x * 1)) = true
    rw [dB_t (by decide) (⟨y, by rw [LibGenus20.c5]; lib_int⟩ : IDv ((5 : Nat) : Int) (x * 0 + 5 * (y * 1))),
      dB_t (by decide) (idv_eq (LibNormPiC.idv_mul_l (-1) hx) (by lib_int))]; rfl
  · have hs : IDv ((5 : Nat) : Int) (2 * ((x + 3 * y) * (x + 3 * y))) := idv_eq (idv_lin hF (idv_self ((5 : Nat) : Int)) 1 (2 * (x * y) + 3 * (y * y)))
      (by unfold f2; rw [LibGenus20.c5]; lib_int)
    have h3 : IDv ((5 : Nat) : Int) (x + 3 * y) := by
      cases euclid_int five_P.2 hs with
      | inl h => exact absurd (idv_natAbs h) n5_2
      | inr h => exact (euclid_int five_P.2 h).elim id id
    show (dB 5 (0 * x + 1 * x + 3 * (1 * y)) && dB 5 (0 * y - 2 * (1 * x) - 1 * y)) = true
    rw [dB_t (by decide) (idv_eq h3 (by lib_int)),
      dB_t (by decide) (idv_eq (idv_lin h3 (idv_self ((5 : Nat) : Int)) (-2) y) (by rw [LibGenus20.c5]; lib_int))]; rfl

/-- p = 2: RF f₁ (2n) = RF f₂ n and RF f₂ (2n) = RF f₁ n (the class of I = (2, 1 + √−5) swaps the forms) -/
theorem ram2 (n : Nat) : RF f1 (2 * n) = RF f2 n ∧ RF f2 (2 * n) = RF f1 n := by
  refine ⟨ram_gen f1 f2 (AB12 1 0) (BB12 1 0) (p := 2) (by decide) n f1_bound f2_bound
    (fun x y => by unfold AB12 f1 f2; dsimp only; rw [LibGenus20.c2]; lib_int)
    (fun x y => by unfold BB12 f1 f2; dsimp only; rw [LibGenus20.c2]; lib_int)
    (fun x y => by unfold AB12 BB12; dsimp only; rw [LibGenus20.c2]; lib_int)
    (fun x y => by unfold AB12 BB12; dsimp only; rw [LibGenus20.c2]; lib_int)
    (fun x y => by unfold AB12 BB12; dsimp only; rw [LibGenus20.c2]; lib_int)
    (fun x y => by unfold AB12 BB12; dsimp only; rw [LibGenus20.c2]; lib_int)
    (fun k u v => by unfold AB12; dsimp only; lib_int) (fun k u v => by unfold AB12; dsimp only; lib_int) f2_s
    (fun x y hF => ?_),
    ram_gen f2 f1 (AB21 1 0) (BB21 1 0) (p := 2) (by decide) n f2_bound f1_bound
    (fun x y => by unfold AB21 f1 f2; dsimp only; rw [LibGenus20.c2]; lib_int)
    (fun x y => by unfold BB21 f1 f2; dsimp only; rw [LibGenus20.c2]; lib_int)
    (fun x y => by unfold AB21 BB21; dsimp only; rw [LibGenus20.c2]; lib_int)
    (fun x y => by unfold AB21 BB21; dsimp only; rw [LibGenus20.c2]; lib_int)
    (fun x y => by unfold AB21 BB21; dsimp only; rw [LibGenus20.c2]; lib_int)
    (fun x y => by unfold AB21 BB21; dsimp only; rw [LibGenus20.c2]; lib_int)
    (fun k u v => by unfold AB21; dsimp only; lib_int) (fun k u v => by unfold AB21; dsimp only; lib_int) f1_s
    (fun x y hF => ?_)⟩
  · have hs : IDv ((2 : Nat) : Int) ((x + y) * (x + y)) := idv_eq (idv_lin hF (idv_self ((2 : Nat) : Int)) 1 (x * y - 2 * (y * y)))
      (by unfold f1; rw [LibGenus20.c2]; lib_int)
    have h3 := (euclid_int two_P.2 hs).elim id id
    show (dB 2 (x * 1 + y * 1 + 3 * (y * 0)) && dB 2 (x * 0 - 2 * (y * 1) - y * 0)) = true
    rw [dB_t (by decide) (idv_eq h3 (by lib_int)),
      dB_t (by decide) (⟨-y, by rw [LibGenus20.c2]; lib_int⟩ : IDv ((2 : Nat) : Int) (x * 0 - 2 * (y * 1) - y * 0))]; rfl
  · have hs : IDv ((2 : Nat) : Int) (3 * (y * y)) := idv_eq (idv_lin hF (idv_self ((2 : Nat) : Int)) 1 (-(x * x + x * y)))
      (by unfold f2; rw [LibGenus20.c2]; lib_int)
    have hy : IDv ((2 : Nat) : Int) y := by
      cases euclid_int two_P.2 hs with
      | inl h => exact absurd (idv_natAbs h) n2_3
      | inr h => exact (euclid_int two_P.2 h).elim id id
    show (dB 2 (2 * (x * 1) + x * 0 + y * 1 + 3 * (y * 0)) && dB 2 (y * 1 - x * 0)) = true
    rw [dB_t (by decide) (idv_eq (idv_lin hy (idv_self ((2 : Nat) : Int)) 1 x) (by rw [LibGenus20.c2]; lib_int)),
      dB_t (by decide) (idv_eq hy (by lib_int))]; rfl

def chkC : Bool := allB 20 (fun r => ieq (F20 r) 1 || ieq (F20 r) (-1) || (ieq (F20 r) 0 && (Nat.beq (md 2 r) 0 || Nat.beq (md 5 r) 0)))

set_option maxRecDepth 100000 in
theorem chkC_true : chkC = true := by decide

theorem chi20_cases (p : Nat) : chi20 p = 1 ∨ chi20 p = -1 ∨ (chi20 p = 0 ∧ (2 ∣ p ∨ 5 ∣ p)) := by
  have t := allB_spec _ _ chkC_true (md 20 p) (md_lt (by decide) p)
  rcases LibTheta11.or3 t with h | h | h
  · exact Or.inl (LibTheta11.ieq_eq h)
  · exact Or.inr (Or.inl (LibTheta11.ieq_eq h))
  · obtain ⟨h1, h2⟩ := LibTwoSqPiQ.and_split h
    refine Or.inr (Or.inr ⟨LibTheta11.ieq_eq h1, ?_⟩)
    rcases LibGenus20.or2' h2 with e | e
    · exact Or.inl (dvd_of_md_zero (by rw [LibGenus20.md2_of_md20]; exact Nat.eq_of_beq_eq_true e))
    · exact Or.inr (dvd_of_md_zero (by rw [LibGenus20.md5_of_md20]; exact Nat.eq_of_beq_eq_true e))

theorem eig20 (p : Nat) (hP : LibPrimePow.Prime p) (n : Nat) :
    LibHeckeOp.hk LibInt.intR p (LibInt.intR.mul (chi20 p) (LibDivFun.one1 LibInt.intR p)) R n
      = LibInt.intR.mul (LibInt.intR.add (chi20 p) (LibDivFun.one1 LibInt.intR p)) (R n) := by
  have hp0 : 0 < p := Nat.lt_trans Nat.zero_lt_one hP.1
  rcases chi20_cases p with hc | hc | ⟨hc, hd⟩
  · have h25 := prime_to_10 (n := p) (by rw [hc]; decide)
    obtain ⟨u, v, huv⟩ := split20 hP hc
    have s := split_R hP (ndvd2 hP h25.1) (fun hd => h25.2 (by
      have p5 := five_P
      cases p5.2 p hd with
      | inl e => exact absurd (e ▸ hP.1) (Nat.lt_irrefl 1)
      | inr e => rw [e]; exact ⟨1, rfl⟩)) huv
    rw [hc]; exact eig_of R hp0 1 s.1 s.2 n
  · have s := inert_R hP hc
    rw [hc]; exact eig_of R hp0 (-1) s.1 s.2 n
  · rw [hc]
    have e : p = 2 ∨ p = 5 := by
      rcases hd with d | d
      · cases two_P.2 p (by cases hP.2 2 d with
          | inl e => exact absurd e (by decide)
          | inr e => rw [← e]; exact ⟨1, rfl⟩) with
        | inl e => exact absurd (e ▸ hP.1) (Nat.lt_irrefl 1)
        | inr e => exact Or.inl e
      · cases five_P.2 p (by cases hP.2 5 d with
          | inl e => exact absurd e (by decide)
          | inr e => rw [← e]; exact ⟨1, rfl⟩) with
        | inl e => exact absurd (e ▸ hP.1) (Nat.lt_irrefl 1)
        | inr e => exact Or.inr e
    rcases e with e | e <;> subst e
    · refine eig_of R hp0 0 (fun n _ => ?_) (fun m => ?_) n
      · unfold R; rw [(ram2 n).1, (ram2 n).2]; generalize (RF f1 n : Int) = X; generalize (RF f2 n : Int) = Y; lib_int
      · unfold R; rw [(ram2 (2 * m)).1, (ram2 (2 * m)).2]
        generalize (RF f1 (2 * m) : Int) = X; generalize (RF f2 (2 * m) : Int) = Y
        generalize (RF f1 m : Int) = Z; generalize (RF f2 m : Int) = W; lib_int
    · refine eig_of R hp0 0 (fun n _ => ?_) (fun m => ?_) n
      · unfold R; rw [(ram5 n).1, (ram5 n).2]; generalize (RF f1 n : Int) = X; generalize (RF f2 n : Int) = Y; lib_int
      · unfold R; rw [(ram5 (5 * m)).1, (ram5 (5 * m)).2]
        generalize (RF f1 (5 * m) : Int) = X; generalize (RF f2 (5 * m) : Int) = Y
        generalize (RF f1 m : Int) = Z; generalize (RF f2 m : Int) = W; lib_int

theorem RF1_one : RF f1 1 = 2 := by decide
theorem RF2_one : RF f2 1 = 0 := by decide

theorem R_one : R 1 = 2 := by unfold R; rw [RF1_one, RF2_one]; rfl

/-- **Genus theta series of discriminant −20.** n > 0 ⇒ #{x² + 5y² = n} + #{2x² + 2xy + 3y² = n} = 2Σ_{d∣n} χ₋₂₀(d). -/
theorem theta20 {n : Nat} (hn : 0 < n) : R n = 2 * LibEisGen.e LibInt.intR chi20 (LibDivFun.one1 LibInt.intR) n := by
  have h := LibHeckeScale.eis_scale LibInt.intR chi20 (LibDivFun.one1 LibInt.intR) chi20_cmult
    (LibEisBridge.one1_cmult LibInt.intR) R eig20 hn
  rw [h, R_one]; rfl

end LibTheta20

#print axioms LibTheta20.split_R
#print axioms LibTheta20.inert_R
#print axioms LibTheta20.ram5
#print axioms LibTheta20.ram2
#print axioms LibTheta20.chi20_cases
#print axioms LibTheta20.eig20
#print axioms LibTheta20.R_one
#print axioms LibTheta20.theta20

lib/Theta20D.lean

lean_all/lib/Theta20D.lean

/-
  lib/Theta20D.lean — T46 shared library (G1550–G1551, discriminant −20): the difference of the two genus theta series
  is the Eisenstein series E(χ₋₄, χ₅), and the exact formula for x² + 5y².

    chim4, chi5    χ₋₄ (mod 4) and χ₅ = (·/5) (mod 5), completely multiplicative
    eig_of2        a(pn) = s·a(n) (p ∤ n), a(p²m) + c·a(m) = s·a(pm)  ⇒  T_p a = s a  (Hecke with c = χ₁χ₂(p))
    D n            r₁(n) − r₂(n)
    eigD           T_p D = (χ₋₄(p) + χ₅(p)) D  for every prime p
    thetaD         n > 0 ⇒ r₁(n) − r₂(n) = 2 Σ_{d ∣ n} χ₋₄(d) χ₅(n/d)
    r1_formula     n > 0 ⇒ 2 r₁(n) = 2 Σ_{d∣n} χ₋₂₀(d) + 2 Σ_{d∣n} χ₋₄(d) χ₅(n/d)
  Lean 4 core only, proofs by hand, no axioms.
-/
import lib.Theta20

namespace LibTheta20D
open LibMod LibGcd LibIntBox LibNormPiA LibNormPiC LibComp20 LibFormCount LibFormRel LibFormRam LibPair20A LibPair20B
  LibRoot20 LibChi11 LibTheta20

def F4i : Nat → Int
  | 0 => 0 | 1 => 1 | 2 => 0 | 3 => -1 | _ + 4 => 0
def F5 : Nat → Int
  | 0 => 0 | 1 => 1 | 2 => -1 | 3 => -1 | 4 => 1 | _ + 5 => 0

def chim4 (n : Nat) : Int := F4i (md 4 n)
def chi5 (n : Nat) : Int := F5 (md 5 n)

def chkM4 : Bool := allB 4 (fun u => allB 4 (fun v => ieq (F4i (md 4 (u * v))) (F4i u * F4i v)))
def chkM5 : Bool := allB 5 (fun u => allB 5 (fun v => ieq (F5 (md 5 (u * v))) (F5 u * F5 v)))
theorem chkM4_true : chkM4 = true := by decide
theorem chkM5_true : chkM5 = true := by decide

theorem chim4_cmult : LibDirTwist.CMult LibInt.intR chim4 := ⟨rfl, fun x y => by
  unfold chim4
  rw [md_mul (n := 4) (by decide) x y]
  exact eq_of_ieq (allB_spec _ _ (allB_spec _ _ chkM4_true _ (md_lt (n := 4) (by decide) x)) _ (md_lt (n := 4) (by decide) y))⟩

theorem chi5_cmult : LibDirTwist.CMult LibInt.intR chi5 := ⟨rfl, fun x y => by
  unfold chi5
  rw [md_mul (n := 5) (by decide) x y]
  exact eq_of_ieq (allB_spec _ _ (allB_spec _ _ chkM5_true _ (md_lt (n := 5) (by decide) x)) _ (md_lt (n := 5) (by decide) y))⟩

theorem vals {p r : Nat} (h : md 20 p = r) : chim4 p = F4i (md 4 r) ∧ chi5 p = F5 (md 5 r) := by
  unfold chim4 chi5
  rw [md4_of_md20, LibGenus20.md5_of_md20, h]; exact ⟨rfl, rfl⟩

theorem eig_of2 (a : Nat → Int) {p : Nat} (hp0 : 0 < p) (c s : Int)
    (h1 : ∀ n, ¬ p ∣ n → a (p * n) = s * a n)
    (h2 : ∀ m, a (p * (p * m)) + c * a m = s * a (p * m)) (n : Nat) :
    LibHeckeOp.hk LibInt.intR p c a n = s * a n := by
  unfold LibHeckeOp.hk
  cases eb : Nat.beq (md p n) 0
  · have hn : ¬ p ∣ n := fun hd => by
      rw [md_zero_of_dvd hp0 hd, LibMod.beq_self] at eb; exact Bool.noConfusion eb
    show a (p * n) + 0 = s * a n
    rw [h1 n hn]; lib_int
  · have sn : n = p * dv p n := by
      have s' := spec p n
      rw [Nat.eq_of_beq_eq_true eb, Nat.add_zero] at s'; exact s'
    show a (p * n) + c * a (dv p n) = s * a n
    generalize dv p n = m at sn ⊢
    subst sn
    have e := h2 m
    generalize a (p * (p * m)) = X at e ⊢
    generalize a m = Y at e ⊢
    generalize a (p * m) = Z at e ⊢
    rw [show X = s * Z - c * Y by rw [← e]; lib_int]; lib_int

def D (n : Nat) : Int := (RF f1 n : Int) - (RF f2 n : Int)

def chkI : Bool := allB 20 (fun r => !(ieq (F20 r) (-1)) || (ieq (F4i (md 4 r) * F5 (md 5 r)) (-1) && ieq (F4i (md 4 r) + F5 (md 5 r)) 0))
set_option maxRecDepth 100000 in
theorem chkI_true : chkI = true := by decide

theorem eigD (p : Nat) (hP : LibPrimePow.Prime p) (n : Nat) :
    LibHeckeOp.hk LibInt.intR p (LibInt.intR.mul (chim4 p) (chi5 p)) D n
      = LibInt.intR.mul (LibInt.intR.add (chim4 p) (chi5 p)) (D n) := by
  have hp0 : 0 < p := Nat.lt_trans Nat.zero_lt_one hP.1
  show LibHeckeOp.hk LibInt.intR p (chim4 p * chi5 p) D n = (chim4 p + chi5 p) * D n
  rcases chi20_cases p with hc | hc | ⟨hc, hd⟩
  · have h25 := prime_to_10 (n := p) (by rw [hc]; decide)
    have h5' : ¬ p ∣ 5 := fun hd => h25.2 (by
      cases five_P.2 p hd with
      | inl e => exact absurd (e ▸ hP.1) (Nat.lt_irrefl 1)
      | inr e => rw [e]; exact ⟨1, rfl⟩)
    have h2' := ndvd2 hP h25.1
    obtain ⟨u, v, h | h⟩ := split20 hP hc
    · have hv : chim4 p * chi5 p = 1 ∧ chim4 p + chi5 p = 2 := by
        rcases LibGenus20.gen1 h h25.1 h25.2 with e | e <;> (obtain ⟨a1, a2⟩ := vals e; rw [a1, a2]; exact ⟨rfl, rfl⟩)
      rw [hv.1, hv.2]
      let P11 := pairA11 h hP.1 hP.2 h2' h5'
      let P22 := pairA22 h hP.1 hP.2 h2' h5'
      refine eig_of2 D hp0 1 2 (fun n hn => ?_) (fun m => ?_) n
      · unfold D; rw [split_ndvd P11 hn, split_ndvd P22 hn, cast2, cast2]; lib_int
      · have e1 := congrArg (fun z : Nat => (z : Int)) (split_dvd P11 m)
        have e2 := congrArg (fun z : Nat => (z : Int)) (split_dvd P22 m)
        simp only [cast2] at e1 e2
        unfold D
        generalize (RF f1 (p * (p * m)) : Int) = A1 at e1 ⊢
        generalize (RF f1 m : Int) = B1 at e1 ⊢
        generalize (RF f1 (p * m) : Int) = C1 at e1 ⊢
        generalize (RF f2 (p * (p * m)) : Int) = A2 at e2 ⊢
        generalize (RF f2 m : Int) = B2 at e2 ⊢
        generalize (RF f2 (p * m) : Int) = C2 at e2 ⊢
        rw [show A1 = C1 + C1 - B1 by rw [← e1]; lib_int, show A2 = C2 + C2 - B2 by rw [← e2]; lib_int]; lib_int
    · have hv : chim4 p * chi5 p = 1 ∧ chim4 p + chi5 p = -2 := by
        rcases LibGenus20.gen2 h h25.1 h25.2 with e | e <;> (obtain ⟨a1, a2⟩ := vals e; rw [a1, a2]; exact ⟨rfl, rfl⟩)
      rw [hv.1, hv.2]
      let P12 := pairB12 h hP.1 hP.2 h2' h5'
      let P21 := pairB21 h hP.1 hP.2 h2' h5'
      refine eig_of2 D hp0 1 (-2) (fun n hn => ?_) (fun m => ?_) n
      · unfold D; rw [split_ndvd P12 hn, split_ndvd P21 hn, cast2, cast2]; lib_int
      · have e1 := congrArg (fun z : Nat => (z : Int)) (split_dvd P12 m)
        have e2 := congrArg (fun z : Nat => (z : Int)) (split_dvd P21 m)
        simp only [cast2] at e1 e2
        unfold D
        generalize (RF f1 (p * (p * m)) : Int) = A1 at e1 ⊢
        generalize (RF f1 m : Int) = B1 at e1 ⊢
        generalize (RF f1 (p * m) : Int) = C1 at e1 e2 ⊢
        generalize (RF f2 (p * (p * m)) : Int) = A2 at e2 ⊢
        generalize (RF f2 m : Int) = B2 at e2 ⊢
        generalize (RF f2 (p * m) : Int) = C2 at e1 e2 ⊢
        rw [show A1 = C2 + C2 - B1 by rw [← e1]; lib_int, show A2 = C1 + C1 - B2 by rw [← e2]; lib_int]; lib_int
  · have t := allB_spec _ _ chkI_true (md 20 p) (md_lt (by decide) p)
    have hi : ieq (F20 (md 20 p)) (-1) = true := by
      have : F20 (md 20 p) = -1 := hc
      rw [this]; exact ieq_self (-1)
    rw [hi] at t
    obtain ⟨t1, t2⟩ := LibTwoSqPiQ.and_split t
    have hv1 : chim4 p * chi5 p = -1 := by
      unfold chim4 chi5; rw [md4_of_md20, LibGenus20.md5_of_md20]; exact LibTheta11.ieq_eq t1
    have hv2 : chim4 p + chi5 p = 0 := by
      unfold chim4 chi5; rw [md4_of_md20, LibGenus20.md5_of_md20]; exact LibTheta11.ieq_eq t2
    rw [hv1, hv2]
    refine eig_of2 D hp0 (-1) 0 (fun n hn => ?_) (fun m => ?_) n
    · unfold D
      rw [inert_zero hp0 f1_s (inert20_f1 hP hc) hn, inert_zero hp0 f2_s (inert20_f2 hP hc) hn]
      generalize (RF f1 n : Int) = X; generalize (RF f2 n : Int) = Y
      show (0 : Int) - 0 = 0 * (X - Y); lib_int
    · unfold D
      rw [inert_scale hp0 f1_s f1_bound (inert20_f1 hP hc) m, inert_scale hp0 f2_s f2_bound (inert20_f2 hP hc) m]
      generalize (RF f1 m : Int) = X; generalize (RF f2 m : Int) = Y
      generalize (RF f1 (p * m) : Int) = Z; generalize (RF f2 (p * m) : Int) = W; lib_int
  · have e : p = 2 ∨ p = 5 := by
      rcases hd with d | d
      · cases two_P.2 p (by cases hP.2 2 d with
          | inl e => exact absurd e (by decide)
          | inr e => rw [← e]; exact ⟨1, rfl⟩) with
        | inl e => exact absurd (e ▸ hP.1) (Nat.lt_irrefl 1)
        | inr e => exact Or.inl e
      · cases five_P.2 p (by cases hP.2 5 d with
          | inl e => exact absurd e (by decide)
          | inr e => rw [← e]; exact ⟨1, rfl⟩) with
        | inl e => exact absurd (e ▸ hP.1) (Nat.lt_irrefl 1)
        | inr e => exact Or.inr e
    rcases e with e | e <;> subst e
    · show LibHeckeOp.hk LibInt.intR 2 (0 * -1) D n = (0 + -1) * D n
      refine eig_of2 D hp0 (0 * -1) (0 + -1) (fun n _ => ?_) (fun m => ?_) n
      · unfold D; rw [(ram2 n).1, (ram2 n).2]; generalize (RF f1 n : Int) = X; generalize (RF f2 n : Int) = Y; lib_int
      · unfold D; rw [(ram2 (2 * m)).1, (ram2 (2 * m)).2]
        generalize (RF f1 (2 * m) : Int) = X; generalize (RF f2 (2 * m) : Int) = Y
        generalize (RF f1 m : Int) = Z; generalize (RF f2 m : Int) = W; lib_int
    · show LibHeckeOp.hk LibInt.intR 5 (1 * 0) D n = (1 + 0) * D n
      refine eig_of2 D hp0 (1 * 0) (1 + 0) (fun n _ => ?_) (fun m => ?_) n
      · unfold D; rw [(ram5 n).1, (ram5 n).2]; generalize (RF f1 n : Int) = X; generalize (RF f2 n : Int) = Y; lib_int
      · unfold D; rw [(ram5 (5 * m)).1, (ram5 (5 * m)).2]
        generalize (RF f1 (5 * m) : Int) = X; generalize (RF f2 (5 * m) : Int) = Y
        generalize (RF f1 m : Int) = Z; generalize (RF f2 m : Int) = W; lib_int

theorem D_one : D 1 = 2 := by unfold D; rw [RF1_one, RF2_one]; rfl

/-- **The difference is E(χ₋₄, χ₅).** n > 0 ⇒ r₁(n) − r₂(n) = 2 Σ_{d∣n} χ₋₄(d) χ₅(n/d). -/
theorem thetaD {n : Nat} (hn : 0 < n) : D n = 2 * LibEisGen.e LibInt.intR chim4 chi5 n := by
  have h := LibHeckeScale.eis_scale LibInt.intR chim4 chi5 chim4_cmult chi5_cmult D eigD hn
  rw [h, D_one]; rfl

/-- **x² + 5y².** n > 0 ⇒ 2 r₁(n) = 2 Σ χ₋₂₀(d) + 2 Σ χ₋₄(d) χ₅(n/d). -/
theorem r1_formula {n : Nat} (hn : 0 < n) :
    2 * (RF f1 n : Int) = 2 * LibEisGen.e LibInt.intR chi20 (LibDivFun.one1 LibInt.intR) n
      + 2 * LibEisGen.e LibInt.intR chim4 chi5 n := by
  rw [← theta20 hn, ← thetaD hn]; unfold R D; lib_int

end LibTheta20D

#print axioms LibTheta20D.chim4_cmult
#print axioms LibTheta20D.chi5_cmult
#print axioms LibTheta20D.eigD
#print axioms LibTheta20D.thetaD
#print axioms LibTheta20D.r1_formula

lib/Theta23.lean

lean_all/lib/Theta23.lean

/-
  lib/Theta23.lean — T46 shared library (G1578b, G1579): discriminant −23, class number 3,
  f₀ = x² + xy + 6y², f₁ = 2x² + xy + 3y² (r₁ = r(2x² − xy + 3y²)), χ = (·/23):

      r₀(n) + 2 r₁(n) = 2 Σ_{d ∣ n} χ(d)                                  (n > 0)
      D = r₀ − r₁ is a Hecke eigenform:  2·(T_p D)(n) = D(p)·D(n) for every prime p
                                         (D/2 = η(z)η(23z), the weight-one cusp form of level 23)

    ram23_0, ram23_1   r(23n) = r(n)   (multiplication by √−23 = −1 + 2ω)
    legB_f1            p = f₁(x, y), 23 ∤ p ⇒ p is a square mod 23      (8p = (4x + y)² + 23y²)
    no_root23          χ(p) = −1 ⇒ p ∤ t² + t + 6
    inert23_f0/f1      χ(p) = −1, p ∣ f(x, y) ⇒ p ∣ x, p ∣ y
    data23             LocalData3 at every prime (Pair23A/B/Small, the roots from QRRoot.root_q, Split23)
    theta23            r₀ + 2r₁ = 2E(χ, 1)
    cusp23             2·hk p χ(p) D n = D(p)·D(n)
  Lean 4 core only, proofs by hand, no axioms.
-/
import lib.ThetaGenus3
import lib.Pair23Small
import lib.ThetaQ

namespace LibTheta23
open LibMod LibGcd LibIntBox LibNormPiA LibNormPiC LibFormCount LibFormRel LibFormRel3 LibFormRam LibChi11
  LibSplit23 LibPair23A LibPair23B LibPair23Small LibThetaGenus3

def chi23 (n : Nat) : Int := LibThetaQ.chiQ 23 n

theorem P23' : LibZp.Prime1 (LibEulerCrit.k 10) := LibPrimeB.prime1_of_prime (LibPrimeB.prime_of_isPrimeB (by decide))
theorem P23 : LibPrimePow.Prime 23 := LibPrimeB.prime_of_isPrimeB (by decide)

theorem chi23_cmult : LibDirTwist.CMult LibInt.intR chi23 := LibThetaQ.chiQ_cmult 10 P23'

/-! ### The ramified prime 23 -/

def A0r (x y : Int) : Int × Int := (-x - 12 * y, 2 * x + y)
def B0r (x y : Int) : Int × Int := (x + 12 * y, -(2 * x) - y)
def A1r (x y : Int) : Int × Int := (-x - 6 * y, 4 * x + y)
def B1r (x y : Int) : Int × Int := (x + 6 * y, -(4 * x) - y)

theorem c23 : ((23 : Nat) : Int) = 23 := rfl

theorem ram23_0 (n : Nat) : RF f0 (23 * n) = RF f0 n :=
  ram_gen f0 f0 A0r B0r (p := 23) (by decide) n f0_bound f0_bound
    (fun x y => by unfold A0r; dsimp only; rw [c23]; unfold f0; lib_int)
    (fun x y => by unfold B0r; dsimp only; rw [c23]; unfold f0; lib_int)
    (fun x y => by unfold A0r B0r; dsimp only; rw [c23]; lib_int)
    (fun x y => by unfold A0r B0r; dsimp only; rw [c23]; lib_int)
    (fun x y => by unfold A0r B0r; dsimp only; rw [c23]; lib_int)
    (fun x y => by unfold A0r B0r; dsimp only; rw [c23]; lib_int)
    (fun k a b => by unfold A0r; dsimp only; lib_int)
    (fun k a b => by unfold A0r; dsimp only; lib_int)
    f0_s
    (fun x y h => by
      have h1 : IDv ((23 : Nat) : Int) ((2 * x + y) * (2 * x + y)) :=
        idv_eq (idv_lin h (idv_self ((23 : Nat) : Int)) 4 (-(y * y))) (by rw [c23]; unfold f0; lib_int)
      have h2 := (euclid_int P23.2 h1).elim id id
      show (dB 23 (x + 12 * y) && dB 23 (-(2 * x) - y)) = true
      rw [LibPair23A.dB_t (by decide) (idv_eq (idv_lin h2 (idv_self ((23 : Nat) : Int)) 12 (-x)) (by rw [c23]; lib_int)),
        LibPair23A.dB_t (by decide) (idv_eq (idv_mul_l (-1) h2) (by lib_int))]; rfl)

theorem ram23_1 (n : Nat) : RF f1 (23 * n) = RF f1 n :=
  ram_gen f1 f1 A1r B1r (p := 23) (by decide) n f1_bound f1_bound
    (fun x y => by unfold A1r; dsimp only; rw [c23]; unfold f1; lib_int)
    (fun x y => by unfold B1r; dsimp only; rw [c23]; unfold f1; lib_int)
    (fun x y => by unfold A1r B1r; dsimp only; rw [c23]; lib_int)
    (fun x y => by unfold A1r B1r; dsimp only; rw [c23]; lib_int)
    (fun x y => by unfold A1r B1r; dsimp only; rw [c23]; lib_int)
    (fun x y => by unfold A1r B1r; dsimp only; rw [c23]; lib_int)
    (fun k a b => by unfold A1r; dsimp only; lib_int)
    (fun k a b => by unfold A1r; dsimp only; lib_int)
    f1_s
    (fun x y h => by
      have h1 : IDv ((23 : Nat) : Int) ((4 * x + y) * (4 * x + y)) :=
        idv_eq (idv_lin h (idv_self ((23 : Nat) : Int)) 8 (-(y * y))) (by rw [c23]; unfold f1; lib_int)
      have h2 := (euclid_int P23.2 h1).elim id id
      show (dB 23 (x + 6 * y) && dB 23 (-(4 * x) - y)) = true
      rw [LibPair23A.dB_t (by decide) (idv_eq (idv_lin h2 (idv_self ((23 : Nat) : Int)) 6 (-x)) (by rw [c23]; lib_int)),
        LibPair23A.dB_t (by decide) (idv_eq (idv_mul_l (-1) h2) (by lib_int))]; rfl)

/-! ### Split and inert primes -/

set_option maxRecDepth 100000 in
theorem legB_f1 {x y : Int} {p : Nat} (h : f1 x y = (p : Int)) (hp0 : Nat.beq (md 23 p) 0 = false) :
    QRChain.legB 23 p = true := by
  have h8 : Nat.beq (md 23 8) 0 = false := rfl
  have hm := LibLegMul.legB_mul 10 P23' (LibThetaQ.mk_ne_of 10 h8) (LibThetaQ.mk_ne_of 10 hp0)
  have hsq : IDv ((23 : Nat) : Int) (((8 * p : Nat) : Int) - (((4 * x + y).natAbs * (4 * x + y).natAbs : Nat) : Int)) := by
    refine ⟨y * y, ?_⟩
    rw [← sq_natAbs]
    show 8 * (p : Int) - (4 * x + y) * (4 * x + y) = _
    rw [← h]; unfold f1; rw [c23]; lib_int
  have e := md_eq_of_idv (by decide) hsq
  have hl8p : QRChain.legB 23 (8 * p) = true := by
    rw [LibThetaQ.legB_res 22, e, ← LibThetaQ.legB_res 22]; exact LibThetaQ.legB_sq _ _
  have hl8 : QRChain.legB 23 8 = true := by decide
  have hm' : QRChain.legB 23 (8 * p) = (QRChain.legB 23 8 == QRChain.legB 23 p) := hm
  rw [hm', hl8] at hl8p
  cases hb : QRChain.legB 23 p
  · rw [hb] at hl8p; exact Bool.noConfusion hl8p
  · rfl

theorem f0_nf (x y : Int) : f0 x y = Nf (5 + 1) x y := by unfold f0 Nf; rfl

theorem chi_of_rep {p : Nat} (_hP : LibPrimePow.Prime p) (h23 : Nat.beq (md 23 p) 0 = false)
    (h : ∃ u v : Int, f0 u v = (p : Int) ∨ f1 u v = (p : Int)) : chi23 p = 1 := by
  obtain ⟨u, v, h | h⟩ := h
  · have hl := LibThetaQ.norm_legB 10 P23' 5 rfl (by rw [← f0_nf]; exact h) h23
    unfold chi23 LibThetaQ.chiQ; rw [h23, hl]; rfl
  · have hl := legB_f1 h h23
    unfold chi23 LibThetaQ.chiQ; rw [h23, hl]; rfl

theorem no_root23 {p : Nat} (hP : LibPrimePow.Prime p) (hc : chi23 p = -1) :
    ∀ t : Nat, ¬ IDv (p : Int) ((t : Int) * t + t + 6) := fun t ht => by
  rcases LibThetaQ.chiQ_cases 10 p with ⟨h1, _, _⟩ | ⟨_, hne, _⟩ | ⟨h0, _⟩
  · have : chi23 p = 1 := h1
    rw [this] at hc; exact absurd hc (by decide)
  · have := chi_of_rep hP hne (rep_of_root23 hP t ht)
    rw [this] at hc; exact absurd hc (by decide)
  · have : chi23 p = 0 := h0
    rw [this] at hc; exact absurd hc (by decide)

theorem inert23_f0 {p : Nat} (hP : LibPrimePow.Prime p) (hc : chi23 p = -1) :
    ∀ x y : Int, IDv (p : Int) (f0 x y) → IDv (p : Int) x ∧ IDv (p : Int) y := by
  intro x y hN
  have hp0 : 0 < p := Nat.lt_trans Nat.zero_lt_one hP.1
  cases (show IDv (p : Int) y ∨ ¬ IDv (p : Int) y from by
    cases e : dB p y
    · exact Or.inr (fun h => by rw [dB_of hp0 h] at e; exact Bool.noConfusion e)
    · exact Or.inl (dB_true e)) with
  | inl hy =>
    have hx2 : IDv (p : Int) (x * x) := idv_eq (idv_lin hN hy 1 (-(x + 6 * y))) (by unfold f0; lib_int)
    exact ⟨(euclid_int hP.2 hx2).elim id id, hy⟩
  | inr hy =>
    exfalso
    obtain ⟨w, hw⟩ := inv_mod hP.2 hy
    have ht : IDv (p : Int) ((w * x) * (w * x) + w * x + 6) :=
      idv_eq (idv_lin (idv_mul_l (w * w) hN) hw 1 (-(w * x + 6 * (1 + w * y)))) (by unfold f0; lib_int)
    obtain ⟨tn, htn⟩ := res_nat hp0 (w * x)
    have ht' : IDv (p : Int) ((tn : Int) * tn + tn + 6) :=
      idv_eq (idv_lin ht htn 1 ((tn : Int) + w * x + 1)) (by lib_int)
    exact no_root23 hP hc tn ht'

theorem odd_of_chi {p : Nat} (hP : LibPrimePow.Prime p) (hc : chi23 p = -1) : ¬ IDv (p : Int) 2 := fun h => by
  cases LibSplit23.P2.2 p (idv_natAbs h) with
  | inl e => rw [e] at hP; exact Nat.lt_irrefl 1 hP.1
  | inr e => rw [e] at hc; exact absurd hc (by decide)

theorem inert23_f1 {p : Nat} (hP : LibPrimePow.Prime p) (hc : chi23 p = -1) :
    ∀ x y : Int, IDv (p : Int) (f1 x y) → IDv (p : Int) x ∧ IDv (p : Int) y := by
  intro x y hN
  obtain ⟨h2x, hy⟩ := inert23_f0 hP hc (2 * x) y (idv_eq (idv_mul_l 2 hN) (by unfold f0 f1; lib_int))
  exact ⟨(euclid_int hP.2 h2x).resolve_left (odd_of_chi hP hc), hy⟩

/-! ### Local data -/

theorem ne23 {p : Nat} (hP : LibPrimePow.Prime p) (hne : Nat.beq (md 23 p) 0 = false) : ¬ IDv (p : Int) 23 := fun h => by
  cases P23.2 p (idv_natAbs h) with
  | inl e => rw [e] at hP; exact Nat.lt_irrefl 1 hP.1
  | inr e => rw [e] at hne; exact Bool.noConfusion hne

theorem ne6 {p : Nat} (hP : LibPrimePow.Prime p) (h2 : p ≠ 2) (h3 : p ≠ 3) : ¬ IDv (p : Int) 6 := fun h => by
  cases euclid_int hP.2 (show IDv (p : Int) (2 * 3) from h) with
  | inl h' =>
    cases LibSplit23.P2.2 p (idv_natAbs h') with
    | inl e => rw [e] at hP; exact Nat.lt_irrefl 1 hP.1
    | inr e => exact h2 e
  | inr h' =>
    cases LibSplit23.P3.2 p (idv_natAbs h') with
    | inl e => rw [e] at hP; exact Nat.lt_irrefl 1 hP.1
    | inr e => exact h3 e

/-- **Local data at every prime** for (x² + xy + 6y², 2x² + xy + 3y²). -/
theorem data23 (p : Nat) (hP : LibPrimePow.Prime p) : LocalData3 f0 f1 chi23 p := by
  rcases LibThetaQ.chiQ_cases 10 p with ⟨h1, hne, hl⟩ | ⟨h1, _, _⟩ | ⟨h0, hd⟩
  · have hc : chi23 p = 1 := h1
    cases Nat.decEq p 2 with
    | isTrue e2 => subst e2; exact LocalData3.splitC ⟨pair01_2⟩ ⟨trip_2⟩ hc
    | isFalse n2 =>
      cases Nat.decEq p 3 with
      | isTrue e3 => subst e3; exact LocalData3.splitC ⟨pair01_3⟩ ⟨trip_3⟩ hc
      | isFalse n3 =>
        have hodd : ¬ p ∣ 2 := fun hd2 => by
          cases LibSplit23.P2.2 p hd2 with
          | inl e => rw [e] at hP; exact Nat.lt_irrefl 1 hP.1
          | inr e => exact n2 e
        obtain ⟨m, hm⟩ := LibTheta11.odd_prime_k hP hodd
        have hp1 : LibZp.Prime1 (LibEulerCrit.k m) := LibPrimeB.prime1_of_prime (hm ▸ hP)
        have hPr : FTAChain.Pr ((2 * 5 + 1) + (2 * 5 + 1) + 1) := LibPrimeB.Pr_of_prime P23
        have hpq : LibEulerCrit.k m + 1 ≠ (2 * 5 + 1) + (2 * 5 + 1) + 1 := fun e => by
          rw [← hm] at e; rw [e] at hne; exact Bool.noConfusion hne
        obtain ⟨t, ht⟩ := LibQRRoot.root_q m 5 hp1 hPr hpq (by rw [← hm]; exact hl)
        rw [← hm] at ht
        have h23 := ne23 hP hne
        obtain ⟨u, v, h | h⟩ := rep_of_root23 hP t ht
        · exact LocalData3.splitP ⟨pair00 h hP.1 hP.2 h23⟩ ⟨pair11 h hP.1 hP.2 h23⟩ hc
        · exact LocalData3.splitC ⟨pair01 h hP.1 hP.2 (ne6 hP n2 n3) h23⟩ ⟨trip h hP.1 hP.2 (ne6 hP n2 n3) h23⟩ hc
  · exact LocalData3.inert (inert23_f0 hP h1) (inert23_f1 hP h1) h1
  · have e : p = 23 := by
      cases hP.2 23 hd with
      | inl e => exact absurd e (by decide)
      | inr e => exact e.symm
    subst e
    exact LocalData3.ram (fun n => ⟨ram23_0 n, ram23_1 n⟩) h0

theorem RF0_one : RF f0 1 = 2 := by decide
theorem RF1_one : RF f1 1 = 0 := by decide

/-- **Theta series of discriminant −23.** n > 0 ⇒ r₀(n) + 2r₁(n) = 2Σ_{d∣n} (d/23). -/
theorem theta23 {n : Nat} (hn : 0 < n) :
    R f0 f1 n = 2 * LibEisGen.e LibInt.intR chi23 (LibDivFun.one1 LibInt.intR) n :=
  theta_sum3 f0_bound f1_bound f0_s f1_s chi23_cmult RF0_one RF1_one data23 hn

/-- **The weight-one cusp form of level 23 is a Hecke eigenform.** D = r₀ − r₁: 2·(T_p D)(n) = D(p)·D(n). -/
theorem cusp23 {p : Nat} (hP : LibPrimePow.Prime p) (n : Nat) :
    2 * LibHeckeOp.hk LibInt.intR p (chi23 p) (D f0 f1) n = D f0 f1 p * D f0 f1 n :=
  cusp_eig f0_bound f1_bound f0_s f1_s RF0_one RF1_one data23 hP n

end LibTheta23

#print axioms LibTheta23.ram23_0
#print axioms LibTheta23.ram23_1
#print axioms LibTheta23.legB_f1
#print axioms LibTheta23.inert23_f1
#print axioms LibTheta23.data23
#print axioms LibTheta23.theta23
#print axioms LibTheta23.cusp23

lib/Theta24.lean

lean_all/lib/Theta24.lean

/-
  lib/Theta24.lean — T46 shared library (G1552e): the genus theta series of discriminant −24 (class number 2)
  is an Eisenstein series:
      r₁(n) + r₂(n) = 2 Σ_{d ∣ n} χ₋₂₄(d),   r₁ = #{x² + 6y² = n},  r₂ = #{2x² + 3y² = n},  n > 0.

    R n            r₁(n) + r₂(n) (as Int)
    split_R        χ(p) = 1:  p = g₁ (Pair24A) or p = g₂ (Pair24B)
    inert_R        χ(p) = −1: Root24.inert24_g1/g2 + FormRam.inert_*
    ram2, ram3     χ(p) = 0:  p = 2 (I = (2, √−6)) and p = 3 ((3, √−6)), both swap the two forms
    theta24        n > 0 ⇒ R n = 2 · Σ_{d ∣ n} χ₋₂₄(d)
  Lean 4 core only, proofs by hand, no axioms.
-/
import lib.Pair24B
import lib.FormRam
import lib.HeckeScale

namespace LibTheta24
open LibMod LibGcd LibIntBox LibNormPiA LibNormPiC LibComp24 LibFormCount LibFormRel LibFormRam LibPair24A LibPair24B LibRoot24 LibChi11

def R (n : Nat) : Int := (RF g1 n : Int) + (RF g2 n : Int)

theorem eig_of (a : Nat → Int) {p : Nat} (hp0 : 0 < p) (c : Int)
    (h1 : ∀ n, ¬ p ∣ n → a (p * n) = (c + 1) * a n)
    (h2 : ∀ m, a (p * (p * m)) + c * a m = (c + 1) * a (p * m)) (n : Nat) :
    LibHeckeOp.hk LibInt.intR p (LibInt.intR.mul c (LibDivFun.one1 LibInt.intR p)) a n
      = LibInt.intR.mul (LibInt.intR.add c (LibDivFun.one1 LibInt.intR p)) (a n) := by
  unfold LibHeckeOp.hk
  cases eb : Nat.beq (md p n) 0
  · have hn : ¬ p ∣ n := fun hd => by
      rw [md_zero_of_dvd hp0 hd, LibMod.beq_self] at eb; exact Bool.noConfusion eb
    show a (p * n) + 0 = (c + 1) * a n
    rw [h1 n hn]; lib_int
  · have sn : n = p * dv p n := by
      have s := spec p n
      rw [Nat.eq_of_beq_eq_true eb, Nat.add_zero] at s; exact s
    show a (p * n) + c * 1 * a (dv p n) = (c + 1) * a n
    generalize dv p n = m at sn ⊢
    subst sn
    have e := h2 m
    generalize a (p * (p * m)) = X at e ⊢
    generalize a m = Y at e ⊢
    generalize a (p * m) = Z at e ⊢
    rw [show X = (c + 1) * Z - c * Y by rw [← e]; lib_int]; lib_int

theorem cast2 (x y : Nat) : ((x + y : Nat) : Int) = (x : Int) + (y : Int) := rfl

section Split
variable {p : Nat} (hP : LibPrimePow.Prime p) (h2 : ¬ p ∣ 2) (h3 : ¬ p ∣ 3)

include hP h2 h3 in
theorem split_R {u v : Int} (huv : g1 u v = (p : Int) ∨ g2 u v = (p : Int)) :
    (∀ n, ¬ p ∣ n → R (p * n) = (1 + 1) * R n) ∧ (∀ m, R (p * (p * m)) + 1 * R m = (1 + 1) * R (p * m)) := by
  rcases huv with h | h
  · let P11 := pairA11 h hP.1 hP.2 h2 h3
    let P22 := pairA22 h hP.1 hP.2 h2 h3
    refine ⟨fun n hn => ?_, fun m => ?_⟩
    · unfold R; rw [split_ndvd P11 hn, split_ndvd P22 hn, cast2, cast2]; lib_int
    · have e1 := congrArg (fun z : Nat => (z : Int)) (split_dvd P11 m)
      have e2 := congrArg (fun z : Nat => (z : Int)) (split_dvd P22 m)
      simp only [cast2] at e1 e2
      unfold R
      generalize (RF g1 (p * (p * m)) : Int) = A1 at e1 ⊢
      generalize (RF g1 m : Int) = B1 at e1 ⊢
      generalize (RF g1 (p * m) : Int) = C1 at e1 ⊢
      generalize (RF g2 (p * (p * m)) : Int) = A2 at e2 ⊢
      generalize (RF g2 m : Int) = B2 at e2 ⊢
      generalize (RF g2 (p * m) : Int) = C2 at e2 ⊢
      rw [show A1 = C1 + C1 - B1 by rw [← e1]; lib_int, show A2 = C2 + C2 - B2 by rw [← e2]; lib_int]; lib_int
  · let P12 := pairB12 h hP.1 hP.2 h2 h3
    let P21 := pairB21 h hP.1 hP.2 h2 h3
    refine ⟨fun n hn => ?_, fun m => ?_⟩
    · unfold R; rw [split_ndvd P12 hn, split_ndvd P21 hn, cast2, cast2]; lib_int
    · have e1 := congrArg (fun z : Nat => (z : Int)) (split_dvd P12 m)
      have e2 := congrArg (fun z : Nat => (z : Int)) (split_dvd P21 m)
      simp only [cast2] at e1 e2
      unfold R
      generalize (RF g1 (p * (p * m)) : Int) = A1 at e1 ⊢
      generalize (RF g1 m : Int) = B1 at e1 ⊢
      generalize (RF g1 (p * m) : Int) = C1 at e1 e2 ⊢
      generalize (RF g2 (p * (p * m)) : Int) = A2 at e2 ⊢
      generalize (RF g2 m : Int) = B2 at e2 ⊢
      generalize (RF g2 (p * m) : Int) = C2 at e1 e2 ⊢
      rw [show A1 = C2 + C2 - B1 by rw [← e1]; lib_int, show A2 = C1 + C1 - B2 by rw [← e2]; lib_int]; lib_int

end Split

theorem inert_R {p : Nat} (hP : LibPrimePow.Prime p) (hc : chi24 p = -1) :
    (∀ n, ¬ p ∣ n → R (p * n) = (-1 + 1) * R n) ∧ (∀ m, R (p * (p * m)) + -1 * R m = (-1 + 1) * R (p * m)) := by
  have hp0 : 0 < p := Nat.lt_trans Nat.zero_lt_one hP.1
  refine ⟨fun n hn => ?_, fun m => ?_⟩
  · unfold R
    rw [inert_zero hp0 g1_s (inert24_g1 hP hc) hn, inert_zero hp0 g2_s (inert24_g2 hP hc) hn]
    generalize (RF g1 n : Int) = X; generalize (RF g2 n : Int) = Y
    show (0 : Int) + 0 = (-1 + 1) * (X + Y); lib_int
  · unfold R
    rw [inert_scale hp0 g1_s g1_bound (inert24_g1 hP hc) m, inert_scale hp0 g2_s g2_bound (inert24_g2 hP hc) m]
    generalize (RF g1 m : Int) = X; generalize (RF g2 m : Int) = Y
    generalize (RF g1 (p * m) : Int) = Z; generalize (RF g2 (p * m) : Int) = W; lib_int

theorem two_P : LibPrimePow.Prime 2 := LibPrimeB.prime_of_isPrimeB (by decide)
theorem three_P : LibPrimePow.Prime 3 := LibPrimeB.prime_of_isPrimeB (by decide)
theorem n2_3 : ¬ 2 ∣ 3 := fun hd => by
  cases three_P.2 2 hd with
  | inl e => exact absurd e (by decide)
  | inr e => exact absurd e (by decide)
theorem n3_2 : ¬ 3 ∣ 2 := fun hd => by
  cases two_P.2 3 hd with
  | inl e => exact absurd e (by decide)
  | inr e => exact absurd e (by decide)

/-- p = 2: RF g₁ (2n) = RF g₂ n and RF g₂ (2n) = RF g₁ n (I = (2, √−6) swaps the forms) -/
theorem ram2 (n : Nat) : RF g1 (2 * n) = RF g2 n ∧ RF g2 (2 * n) = RF g1 n := by
  refine ⟨ram_gen g1 g2 (AB12 1 0) (BB12 1 0) (p := 2) (by decide) n g1_bound g2_bound
    (fun x y => by unfold AB12 g1 g2; dsimp only; rw [LibGenus20.c2]; lib_int)
    (fun x y => by unfold BB12 g1 g2; dsimp only; rw [LibGenus20.c2]; lib_int)
    (fun x y => by unfold AB12 BB12; dsimp only; rw [LibGenus20.c2]; lib_int)
    (fun x y => by unfold AB12 BB12; dsimp only; rw [LibGenus20.c2]; lib_int)
    (fun x y => by unfold AB12 BB12; dsimp only; rw [LibGenus20.c2]; lib_int)
    (fun x y => by unfold AB12 BB12; dsimp only; rw [LibGenus20.c2]; lib_int)
    (fun k u v => by unfold AB12; dsimp only; lib_int) (fun k u v => by unfold AB12; dsimp only; lib_int) g2_s
    (fun x y hF => ?_),
    ram_gen g2 g1 (AB21 1 0) (BB21 1 0) (p := 2) (by decide) n g2_bound g1_bound
    (fun x y => by unfold AB21 g1 g2; dsimp only; rw [LibGenus20.c2]; lib_int)
    (fun x y => by unfold BB21 g1 g2; dsimp only; rw [LibGenus20.c2]; lib_int)
    (fun x y => by unfold AB21 BB21; dsimp only; rw [LibGenus20.c2]; lib_int)
    (fun x y => by unfold AB21 BB21; dsimp only; rw [LibGenus20.c2]; lib_int)
    (fun x y => by unfold AB21 BB21; dsimp only; rw [LibGenus20.c2]; lib_int)
    (fun x y => by unfold AB21 BB21; dsimp only; rw [LibGenus20.c2]; lib_int)
    (fun k u v => by unfold AB21; dsimp only; lib_int) (fun k u v => by unfold AB21; dsimp only; lib_int) g1_s
    (fun x y hF => ?_)⟩
  · have hs : IDv ((2 : Nat) : Int) (x * x) := idv_eq (idv_lin hF (idv_self ((2 : Nat) : Int)) 1 (-(3 * (y * y))))
      (by unfold g1; rw [LibGenus20.c2]; lib_int)
    have hx := (euclid_int two_P.2 hs).elim id id
    show (dB 2 (1 * x + 3 * (0 * y)) && dB 2 (2 * (1 * y) - 0 * x)) = true
    rw [dB_t (by decide) (idv_eq hx (by lib_int)),
      dB_t (by decide) (⟨y, by rw [LibGenus20.c2]; lib_int⟩ : IDv ((2 : Nat) : Int) (2 * (1 * y) - 0 * x))]; rfl
  · have hs : IDv ((2 : Nat) : Int) (3 * (y * y)) := idv_eq (idv_lin hF (idv_self ((2 : Nat) : Int)) 1 (-(x * x)))
      (by unfold g2; rw [LibGenus20.c2]; lib_int)
    have hy : IDv ((2 : Nat) : Int) y := by
      cases euclid_int two_P.2 hs with
      | inl h => exact absurd (idv_natAbs h) n2_3
      | inr h => exact (euclid_int two_P.2 h).elim id id
    show (dB 2 (2 * (1 * x) + 3 * (0 * y)) && dB 2 (1 * y - 0 * x)) = true
    rw [dB_t (by decide) (⟨x, by rw [LibGenus20.c2]; lib_int⟩ : IDv ((2 : Nat) : Int) (2 * (1 * x) + 3 * (0 * y))),
      dB_t (by decide) (idv_eq hy (by lib_int))]; rfl

/-- p = 3: RF g₁ (3n) = RF g₂ n and RF g₂ (3n) = RF g₁ n ((3, √−6) = (√−6)I⁻¹ swaps the forms) -/
theorem ram3 (n : Nat) : RF g1 (3 * n) = RF g2 n ∧ RF g2 (3 * n) = RF g1 n := by
  refine ⟨ram_gen g1 g2 (AB12 0 1) (BB12 0 1) (p := 3) (by decide) n g1_bound g2_bound
    (fun x y => by unfold AB12 g1 g2; dsimp only; rw [LibGenus20.c3]; lib_int)
    (fun x y => by unfold BB12 g1 g2; dsimp only; rw [LibGenus20.c3]; lib_int)
    (fun x y => by unfold AB12 BB12; dsimp only; rw [LibGenus20.c3]; lib_int)
    (fun x y => by unfold AB12 BB12; dsimp only; rw [LibGenus20.c3]; lib_int)
    (fun x y => by unfold AB12 BB12; dsimp only; rw [LibGenus20.c3]; lib_int)
    (fun x y => by unfold AB12 BB12; dsimp only; rw [LibGenus20.c3]; lib_int)
    (fun k u v => by unfold AB12; dsimp only; lib_int) (fun k u v => by unfold AB12; dsimp only; lib_int) g2_s
    (fun x y hF => ?_),
    ram_gen g2 g1 (AB21 0 1) (BB21 0 1) (p := 3) (by decide) n g2_bound g1_bound
    (fun x y => by unfold AB21 g1 g2; dsimp only; rw [LibGenus20.c3]; lib_int)
    (fun x y => by unfold BB21 g1 g2; dsimp only; rw [LibGenus20.c3]; lib_int)
    (fun x y => by unfold AB21 BB21; dsimp only; rw [LibGenus20.c3]; lib_int)
    (fun x y => by unfold AB21 BB21; dsimp only; rw [LibGenus20.c3]; lib_int)
    (fun x y => by unfold AB21 BB21; dsimp only; rw [LibGenus20.c3]; lib_int)
    (fun x y => by unfold AB21 BB21; dsimp only; rw [LibGenus20.c3]; lib_int)
    (fun k u v => by unfold AB21; dsimp only; lib_int) (fun k u v => by unfold AB21; dsimp only; lib_int) g1_s
    (fun x y hF => ?_)⟩
  · have hs : IDv ((3 : Nat) : Int) (x * x) := idv_eq (idv_lin hF (idv_self ((3 : Nat) : Int)) 1 (-(2 * (y * y))))
      (by unfold g1; rw [LibGenus20.c3]; lib_int)
    have hx := (euclid_int three_P.2 hs).elim id id
    show (dB 3 (0 * x + 3 * (1 * y)) && dB 3 (2 * (0 * y) - 1 * x)) = true
    rw [dB_t (by decide) (⟨y, by rw [LibGenus20.c3]; lib_int⟩ : IDv ((3 : Nat) : Int) (0 * x + 3 * (1 * y))),
      dB_t (by decide) (idv_eq (LibNormPiC.idv_mul_l (-1) hx) (by lib_int))]; rfl
  · have hs : IDv ((3 : Nat) : Int) (2 * (x * x)) := idv_eq (idv_lin hF (idv_self ((3 : Nat) : Int)) 1 (-(y * y)))
      (by unfold g2; rw [LibGenus20.c3]; lib_int)
    have hx : IDv ((3 : Nat) : Int) x := by
      cases euclid_int three_P.2 hs with
      | inl h => exact absurd (idv_natAbs h) n3_2
      | inr h => exact (euclid_int three_P.2 h).elim id id
    show (dB 3 (2 * (0 * x) + 3 * (1 * y)) && dB 3 (0 * y - 1 * x)) = true
    rw [dB_t (by decide) (⟨y, by rw [LibGenus20.c3]; lib_int⟩ : IDv ((3 : Nat) : Int) (2 * (0 * x) + 3 * (1 * y))),
      dB_t (by decide) (idv_eq (LibNormPiC.idv_mul_l (-1) hx) (by lib_int))]; rfl

def chkC : Bool := allB 24 (fun r => ieq (F24 r) 1 || ieq (F24 r) (-1) || (ieq (F24 r) 0 && (Nat.beq (md 2 r) 0 || Nat.beq (md 3 r) 0)))

set_option maxRecDepth 100000 in
theorem chkC_true : chkC = true := by decide

theorem chi24_cases (p : Nat) : chi24 p = 1 ∨ chi24 p = -1 ∨ (chi24 p = 0 ∧ (2 ∣ p ∨ 3 ∣ p)) := by
  have t := allB_spec _ _ chkC_true (md 24 p) (md_lt (by decide) p)
  rcases LibTheta11.or3 t with h | h | h
  · exact Or.inl (LibTheta11.ieq_eq h)
  · exact Or.inr (Or.inl (LibTheta11.ieq_eq h))
  · obtain ⟨h1, h2⟩ := LibTwoSqPiQ.and_split h
    refine Or.inr (Or.inr ⟨LibTheta11.ieq_eq h1, ?_⟩)
    rcases LibGenus20.or2' h2 with e | e
    · exact Or.inl (dvd_of_md_zero (by rw [md2_of_md24]; exact Nat.eq_of_beq_eq_true e))
    · exact Or.inr (dvd_of_md_zero (by rw [md3_of_md24]; exact Nat.eq_of_beq_eq_true e))

theorem prime_eq {p q : Nat} (hP : LibPrimePow.Prime p) (hQ : LibPrimePow.Prime q) (d : q ∣ p) : p = q := by
  cases hP.2 q d with
  | inl e => exact absurd (e ▸ hQ.1) (Nat.lt_irrefl 1)
  | inr e => exact e.symm

theorem not_dvd_of {p q : Nat} (hP : LibPrimePow.Prime p) (hQ : LibPrimePow.Prime q) (h : ¬ q ∣ p) : ¬ p ∣ q := fun hd => by
  cases hQ.2 p hd with
  | inl e => exact absurd (e ▸ hP.1) (Nat.lt_irrefl 1)
  | inr e => exact h (by rw [e]; exact ⟨1, (Nat.mul_one q).symm⟩)

theorem swap_eig {p : Nat} (hp0 : 0 < p) (hs : ∀ n, RF g1 (p * n) = RF g2 n ∧ RF g2 (p * n) = RF g1 n) (n : Nat) :
    LibHeckeOp.hk LibInt.intR p (LibInt.intR.mul 0 (LibDivFun.one1 LibInt.intR p)) R n
      = LibInt.intR.mul (LibInt.intR.add 0 (LibDivFun.one1 LibInt.intR p)) (R n) := by
  refine eig_of R hp0 0 (fun n _ => ?_) (fun m => ?_) n
  · unfold R; rw [(hs n).1, (hs n).2]; generalize (RF g1 n : Int) = X; generalize (RF g2 n : Int) = Y; lib_int
  · unfold R; rw [(hs (p * m)).1, (hs (p * m)).2]
    generalize (RF g1 (p * m) : Int) = X; generalize (RF g2 (p * m) : Int) = Y
    generalize (RF g1 m : Int) = Z; generalize (RF g2 m : Int) = W; lib_int

theorem eig24 (p : Nat) (hP : LibPrimePow.Prime p) (n : Nat) :
    LibHeckeOp.hk LibInt.intR p (LibInt.intR.mul (chi24 p) (LibDivFun.one1 LibInt.intR p)) R n
      = LibInt.intR.mul (LibInt.intR.add (chi24 p) (LibDivFun.one1 LibInt.intR p)) (R n) := by
  have hp0 : 0 < p := Nat.lt_trans Nat.zero_lt_one hP.1
  rcases chi24_cases p with hc | hc | ⟨hc, hd⟩
  · have h23 := prime_to_6 (n := p) (by rw [hc]; decide)
    obtain ⟨u, v, huv⟩ := split24 hP hc
    have s := split_R hP (not_dvd_of hP two_P h23.1) (not_dvd_of hP three_P h23.2) huv
    rw [hc]; exact eig_of R hp0 1 s.1 s.2 n
  · have s := inert_R hP hc
    rw [hc]; exact eig_of R hp0 (-1) s.1 s.2 n
  · rw [hc]
    rcases hd with d | d
    · have e := prime_eq hP two_P d
      subst e; exact swap_eig hp0 ram2 n
    · have e := prime_eq hP three_P d
      subst e; exact swap_eig hp0 ram3 n

theorem RF1_one : RF g1 1 = 2 := by decide
theorem RF2_one : RF g2 1 = 0 := by decide

theorem R_one : R 1 = 2 := by unfold R; rw [RF1_one, RF2_one]; rfl

/-- **Genus theta series of discriminant −24.** n > 0 ⇒ #{x² + 6y² = n} + #{2x² + 3y² = n} = 2Σ_{d∣n} χ₋₂₄(d). -/
theorem theta24 {n : Nat} (hn : 0 < n) : R n = 2 * LibEisGen.e LibInt.intR chi24 (LibDivFun.one1 LibInt.intR) n := by
  have h := LibHeckeScale.eis_scale LibInt.intR chi24 (LibDivFun.one1 LibInt.intR) chi24_cmult
    (LibEisBridge.one1_cmult LibInt.intR) R eig24 hn
  rw [h, R_one]; rfl

end LibTheta24

#print axioms LibTheta24.split_R
#print axioms LibTheta24.inert_R
#print axioms LibTheta24.ram2
#print axioms LibTheta24.ram3
#print axioms LibTheta24.chi24_cases
#print axioms LibTheta24.eig24
#print axioms LibTheta24.R_one
#print axioms LibTheta24.theta24

lib/Theta24D.lean

lean_all/lib/Theta24D.lean

/-
  lib/Theta24D.lean — T46 shared library (G1555, discriminant −24): the difference of the two genus theta series
  is the Eisenstein series E(χ₋₃, χ₈), and the exact formula for x² + 6y².

    chim3, chi8    χ₋₃ (mod 3) and χ₈ (mod 8: 1 at ±1, −1 at ±3), completely multiplicative
    D n            r₁(n) − r₂(n)
    eigD           T_p D = (χ₋₃(p) + χ₈(p)) D  for every prime p   (p = 2, 3 swap the forms: D(pn) = −D(n))
    thetaD         n > 0 ⇒ r₁(n) − r₂(n) = 2 Σ_{d ∣ n} χ₋₃(d) χ₈(n/d)
    r1_formula     n > 0 ⇒ 2 r₁(n) = 2 Σ_{d∣n} χ₋₂₄(d) + 2 Σ_{d∣n} χ₋₃(d) χ₈(n/d)
  Lean 4 core only, proofs by hand, no axioms.
-/
import lib.Theta24
import lib.Theta20D

namespace LibTheta24D
open LibMod LibGcd LibIntBox LibNormPiA LibNormPiC LibComp24 LibFormCount LibFormRel LibFormRam LibPair24A LibPair24B
  LibRoot24 LibChi11 LibTheta24

def F3 : Nat → Int
  | 0 => 0 | 1 => 1 | 2 => -1 | _ + 3 => 0
def F8 : Nat → Int
  | 0 => 0 | 1 => 1 | 2 => 0 | 3 => -1 | 4 => 0 | 5 => -1 | 6 => 0 | 7 => 1 | _ + 8 => 0

def chim3 (n : Nat) : Int := F3 (md 3 n)
def chi8 (n : Nat) : Int := F8 (md 8 n)

def chkM3 : Bool := allB 3 (fun u => allB 3 (fun v => ieq (F3 (md 3 (u * v))) (F3 u * F3 v)))
def chkM8 : Bool := allB 8 (fun u => allB 8 (fun v => ieq (F8 (md 8 (u * v))) (F8 u * F8 v)))
theorem chkM3_true : chkM3 = true := by decide
set_option maxRecDepth 100000 in
theorem chkM8_true : chkM8 = true := by decide

theorem chim3_cmult : LibDirTwist.CMult LibInt.intR chim3 := ⟨rfl, fun x y => by
  unfold chim3
  rw [md_mul (n := 3) (by decide) x y]
  exact eq_of_ieq (allB_spec _ _ (allB_spec _ _ chkM3_true _ (md_lt (n := 3) (by decide) x)) _ (md_lt (n := 3) (by decide) y))⟩

theorem chi8_cmult : LibDirTwist.CMult LibInt.intR chi8 := ⟨rfl, fun x y => by
  unfold chi8
  rw [md_mul (n := 8) (by decide) x y]
  exact eq_of_ieq (allB_spec _ _ (allB_spec _ _ chkM8_true _ (md_lt (n := 8) (by decide) x)) _ (md_lt (n := 8) (by decide) y))⟩

theorem vals {p r : Nat} (h : md 24 p = r) : chim3 p = F3 (md 3 r) ∧ chi8 p = F8 (md 8 r) := by
  unfold chim3 chi8
  rw [md3_of_md24, md8_of_md24, h]; exact ⟨rfl, rfl⟩

def D (n : Nat) : Int := (RF g1 n : Int) - (RF g2 n : Int)

theorem swapD {p : Nat} (hs : ∀ n, RF g1 (p * n) = RF g2 n ∧ RF g2 (p * n) = RF g1 n) (n : Nat) :
    D (p * n) = (-1 + 0) * D n := by
  unfold D; rw [(hs n).1, (hs n).2]; generalize (RF g1 n : Int) = X; generalize (RF g2 n : Int) = Y; lib_int

theorem swapD2 {p : Nat} (hs : ∀ n, RF g1 (p * n) = RF g2 n ∧ RF g2 (p * n) = RF g1 n) (m : Nat) :
    D (p * (p * m)) + 0 * D m = (-1 + 0) * D (p * m) := by
  unfold D; rw [(hs (p * m)).1, (hs (p * m)).2]
  generalize (RF g1 (p * m) : Int) = X; generalize (RF g2 (p * m) : Int) = Y
  generalize (RF g1 m : Int) = Z; generalize (RF g2 m : Int) = W; lib_int

def chkI : Bool := allB 24 (fun r => !(ieq (F24 r) (-1)) || (ieq (F3 (md 3 r) * F8 (md 8 r)) (-1) && ieq (F3 (md 3 r) + F8 (md 8 r)) 0))
set_option maxRecDepth 100000 in
theorem chkI_true : chkI = true := by decide

theorem eigD (p : Nat) (hP : LibPrimePow.Prime p) (n : Nat) :
    LibHeckeOp.hk LibInt.intR p (LibInt.intR.mul (chim3 p) (chi8 p)) D n
      = LibInt.intR.mul (LibInt.intR.add (chim3 p) (chi8 p)) (D n) := by
  have hp0 : 0 < p := Nat.lt_trans Nat.zero_lt_one hP.1
  show LibHeckeOp.hk LibInt.intR p (chim3 p * chi8 p) D n = (chim3 p + chi8 p) * D n
  rcases chi24_cases p with hc | hc | ⟨hc, hd⟩
  · have h23 := prime_to_6 (n := p) (by rw [hc]; decide)
    have h3' := LibTheta24.not_dvd_of hP LibTheta24.three_P h23.2
    have h2' := LibTheta24.not_dvd_of hP LibTheta24.two_P h23.1
    obtain ⟨u, v, h | h⟩ := split24 hP hc
    · have hv : chim3 p * chi8 p = 1 ∧ chim3 p + chi8 p = 2 := by
        rcases gen1 h h23.1 h23.2 with e | e <;> (obtain ⟨a1, a2⟩ := vals e; rw [a1, a2]; exact ⟨rfl, rfl⟩)
      rw [hv.1, hv.2]
      let P11 := pairA11 h hP.1 hP.2 h2' h3'
      let P22 := pairA22 h hP.1 hP.2 h2' h3'
      refine LibTheta20D.eig_of2 D hp0 1 2 (fun n hn => ?_) (fun m => ?_) n
      · unfold D; rw [split_ndvd P11 hn, split_ndvd P22 hn, cast2, cast2]; lib_int
      · have e1 := congrArg (fun z : Nat => (z : Int)) (split_dvd P11 m)
        have e2 := congrArg (fun z : Nat => (z : Int)) (split_dvd P22 m)
        simp only [cast2] at e1 e2
        unfold D
        generalize (RF g1 (p * (p * m)) : Int) = A1 at e1 ⊢
        generalize (RF g1 m : Int) = B1 at e1 ⊢
        generalize (RF g1 (p * m) : Int) = C1 at e1 ⊢
        generalize (RF g2 (p * (p * m)) : Int) = A2 at e2 ⊢
        generalize (RF g2 m : Int) = B2 at e2 ⊢
        generalize (RF g2 (p * m) : Int) = C2 at e2 ⊢
        rw [show A1 = C1 + C1 - B1 by rw [← e1]; lib_int, show A2 = C2 + C2 - B2 by rw [← e2]; lib_int]; lib_int
    · have hv : chim3 p * chi8 p = 1 ∧ chim3 p + chi8 p = -2 := by
        rcases gen2 h h23.1 h23.2 with e | e <;> (obtain ⟨a1, a2⟩ := vals e; rw [a1, a2]; exact ⟨rfl, rfl⟩)
      rw [hv.1, hv.2]
      let P12 := pairB12 h hP.1 hP.2 h2' h3'
      let P21 := pairB21 h hP.1 hP.2 h2' h3'
      refine LibTheta20D.eig_of2 D hp0 1 (-2) (fun n hn => ?_) (fun m => ?_) n
      · unfold D; rw [split_ndvd P12 hn, split_ndvd P21 hn, cast2, cast2]; lib_int
      · have e1 := congrArg (fun z : Nat => (z : Int)) (split_dvd P12 m)
        have e2 := congrArg (fun z : Nat => (z : Int)) (split_dvd P21 m)
        simp only [cast2] at e1 e2
        unfold D
        generalize (RF g1 (p * (p * m)) : Int) = A1 at e1 ⊢
        generalize (RF g1 m : Int) = B1 at e1 ⊢
        generalize (RF g1 (p * m) : Int) = C1 at e1 e2 ⊢
        generalize (RF g2 (p * (p * m)) : Int) = A2 at e2 ⊢
        generalize (RF g2 m : Int) = B2 at e2 ⊢
        generalize (RF g2 (p * m) : Int) = C2 at e1 e2 ⊢
        rw [show A1 = C2 + C2 - B1 by rw [← e1]; lib_int, show A2 = C1 + C1 - B2 by rw [← e2]; lib_int]; lib_int
  · have t := allB_spec _ _ chkI_true (md 24 p) (md_lt (by decide) p)
    have hi : ieq (F24 (md 24 p)) (-1) = true := by
      have : F24 (md 24 p) = -1 := hc
      rw [this]; exact ieq_self (-1)
    rw [hi] at t
    obtain ⟨t1, t2⟩ := LibTwoSqPiQ.and_split t
    have hv1 : chim3 p * chi8 p = -1 := by
      unfold chim3 chi8; rw [md3_of_md24, md8_of_md24]; exact LibTheta11.ieq_eq t1
    have hv2 : chim3 p + chi8 p = 0 := by
      unfold chim3 chi8; rw [md3_of_md24, md8_of_md24]; exact LibTheta11.ieq_eq t2
    rw [hv1, hv2]
    refine LibTheta20D.eig_of2 D hp0 (-1) 0 (fun n hn => ?_) (fun m => ?_) n
    · unfold D
      rw [inert_zero hp0 g1_s (inert24_g1 hP hc) hn, inert_zero hp0 g2_s (inert24_g2 hP hc) hn]
      generalize (RF g1 n : Int) = X; generalize (RF g2 n : Int) = Y
      show (0 : Int) - 0 = 0 * (X - Y); lib_int
    · unfold D
      rw [inert_scale hp0 g1_s g1_bound (inert24_g1 hP hc) m, inert_scale hp0 g2_s g2_bound (inert24_g2 hP hc) m]
      generalize (RF g1 m : Int) = X; generalize (RF g2 m : Int) = Y
      generalize (RF g1 (p * m) : Int) = Z; generalize (RF g2 (p * m) : Int) = W; lib_int
  · rcases hd with d | d
    · have e := LibTheta24.prime_eq hP LibTheta24.two_P d
      subst e
      show LibHeckeOp.hk LibInt.intR 2 (-1 * 0) D n = (-1 + 0) * D n
      exact LibTheta20D.eig_of2 D hp0 (-1 * 0) (-1 + 0) (fun n _ => swapD (p := 2) ram2 n) (fun m => swapD2 (p := 2) ram2 m) n
    · have e := LibTheta24.prime_eq hP LibTheta24.three_P d
      subst e
      show LibHeckeOp.hk LibInt.intR 3 (0 * -1) D n = (0 + -1) * D n
      rw [show (0 * -1 : Int) = -1 * 0 from rfl, show (0 + -1 : Int) = -1 + 0 from rfl]
      exact LibTheta20D.eig_of2 D hp0 (-1 * 0) (-1 + 0) (fun n _ => swapD (p := 3) ram3 n) (fun m => swapD2 (p := 3) ram3 m) n

theorem D_one : D 1 = 2 := by unfold D; rw [RF1_one, RF2_one]; rfl

/-- **The difference is E(χ₋₃, χ₈).** n > 0 ⇒ r₁(n) − r₂(n) = 2 Σ_{d∣n} χ₋₃(d) χ₈(n/d). -/
theorem thetaD {n : Nat} (hn : 0 < n) : D n = 2 * LibEisGen.e LibInt.intR chim3 chi8 n := by
  have h := LibHeckeScale.eis_scale LibInt.intR chim3 chi8 chim3_cmult chi8_cmult D eigD hn
  rw [h, D_one]; rfl

/-- **x² + 6y².** n > 0 ⇒ 2 r₁(n) = 2 Σ χ₋₂₄(d) + 2 Σ χ₋₃(d) χ₈(n/d). -/
theorem r1_formula {n : Nat} (hn : 0 < n) :
    2 * (RF g1 n : Int) = 2 * LibEisGen.e LibInt.intR chi24 (LibDivFun.one1 LibInt.intR) n
      + 2 * LibEisGen.e LibInt.intR chim3 chi8 n := by
  rw [← theta24 hn, ← thetaD hn]; unfold R D; lib_int

end LibTheta24D

#print axioms LibTheta24D.chim3_cmult
#print axioms LibTheta24D.chi8_cmult
#print axioms LibTheta24D.eigD
#print axioms LibTheta24D.thetaD
#print axioms LibTheta24D.r1_formula

lib/Theta2Half.lean

lean_all/lib/Theta2Half.lean

/-
  lib/Theta2Half.lean — T46 shared library (G936): the two half products on the θ₂ side (any commutative ring).

    half_even1    Π_{n<N}(1 + q^{2n+2}) = Σ_{a≤N} q^{a²+a}[N; a]_{q²}
    half_even0    Π_{n<N}(1 + q^{2n})   = Σ_{b≤N} q^{2tri b}[N; b]_{q²}       (2tri b = b² − b)
  (Their product is the Σ_{k∈ℤ} q^{k²+k} side — the first step toward the product formula for θ₂(Q) = Σ_{d odd} Q^{d²}.) Lean 4 core only, proofs by hand, no axioms.
-/
import lib.MacMahon

namespace LibTheta2Half
open DetChain LibRing LibSum LibSemi LibQBinom LibJTPFinite LibMacMahon

variable {S : Type} (T : CRing S)

theorem half_even1 (q : S) (N : Nat) :
    prodF T (fun n => T.add T.one (pw T q (n + n + 2))) N
      = sum (toSemi T) (fun a => T.mul (T.mul (pw T q (a * a + a)) (gb T (T.mul q q) N a)) T.one) (rng (N + 1)) := by
  have e : ∀ n, T.add T.one (pw T q (n + n + 2)) = T.add T.one (T.mul (pw T (T.mul q q) n) (T.mul q q)) := by
    intro n
    rw [pw_sq T q n]
    show T.add T.one (T.mul (T.mul (pw T q (n + n)) q) q) = _
    lib_ring
  rw [prodF_ext T _ _ e N]
  have e2 := (P_eq_prodF T (T.mul q q) N (T.mul q q)).symm
  rw [e2, rothe]
  refine sum_ext (toSemi T) _ _ (fun a => ?_) _
  rw [pw_sq T q (tri a), pw_sq T q a]
  have ex : tri a + tri a + (a + a) = a * a + a := by
    rw [show tri a + tri a + (a + a) = (tri a + tri a + a) + a by lib_nat, tri_sq a]
  rw [← ex, pw_add' T q (tri a + tri a) (a + a)]; lib_ring

theorem half_even0 (q : S) (N : Nat) :
    prodF T (fun n => T.add T.one (pw T q (n + n))) N
      = sum (toSemi T) (fun b => T.mul (pw T q (tri b + tri b)) (gb T (T.mul q q) N b)) (rng (N + 1)) := by
  have e : ∀ n, T.add T.one (pw T q (n + n)) = T.add T.one (T.mul (pw T (T.mul q q) n) T.one) := by
    intro n; rw [pw_sq T q n]; lib_ring
  rw [prodF_ext T _ _ e N]
  have e2 := (P_eq_prodF T (T.mul q q) N T.one).symm
  rw [e2, rothe]
  refine sum_ext (toSemi T) _ _ (fun b => ?_) _
  rw [pw_sq T q (tri b), pw_one' T b]; lib_ring

end LibTheta2Half

#print axioms LibTheta2Half.half_even1
#print axioms LibTheta2Half.half_even0

lib/Theta2LogDeriv.lean

lean_all/lib/Theta2LogDeriv.lean

/-
  lib/Theta2LogDeriv.lean — T46 shared library (G941): closed form of the log-derivative of the θ₂ product formula (truncated ring, Euler derivative δ = q∂_q).

    theta2_logderiv   in R[q]/(q^{R′+1}) (R′ + K₀ ≤ 2r₀+1, 2r₀+1 < K₀(K₀+1)), with inverses v_i(1 − q^{2i+2}) = 1, u_n(1 + q^{2n+2}) = 1, w_n(1 + q^{2n}) = 1:
      Σ_{k≤K₀} (k²+k) q^{k²+k} + Σ_{k<K₀} 2tri(k+1) q^{2tri(k+1)}
        = θ₂ · ( Σ_{i<2N} δ(1 − q^{2i+2})·v_i + Σ_{n<N} (2n+2) q^{2n+2} u_n + Σ_{n<N} (2n) q^{2n} w_n )
    θ₂ = (q²;q²)_{2N} Π_{n<N}(1+q^{2n+2})(1+q^{2n}).   (the odd side of B_p in agent3 G44d: Σ d² Q^{d²}, d odd.)
  Lean 4 core only, proofs by hand, no axioms.
-/
import lib.Theta2Ring
import lib.TPLogDeriv

namespace LibTheta2LogDeriv
open DetChain LibRing LibSum LibSemi LibQBinom LibJTPFinite LibTPoly LibTPChar LibQPoch LibTPDeriv LibTPLogDeriv LibTheta2Ring

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

theorem dlt_one_add_Xpow (N m : Nat) :
    dlt K N ((tpR K N).add (tpR K N).one (pw (tpR K N) (X K N) m)) = (tpR K N).mul (iota K N (castN K m)) (pw (tpR K N) (X K N) m) := by
  rw [dlt_add, dlt_one, dlt_Xpow]; lib_ring

theorem theta2_logderiv (r0 K0 Rp : Nat) (hK2 : r0 + r0 + 1 < tri K0 + tri K0 + (K0 + K0)) (hR : Rp + K0 ≤ r0 + r0 + 1)
    (v u w : Nat → TP R Rp)
    (hv : ∀ i, (tpR K Rp).mul (sub (tpR K Rp) (tpR K Rp).one (pw (tpR K Rp) ((tpR K Rp).mul (X K Rp) (X K Rp)) (i + 1))) (v i) = (tpR K Rp).one)
    (hu : ∀ n, (tpR K Rp).mul ((tpR K Rp).add (tpR K Rp).one (pw (tpR K Rp) (X K Rp) (n + n + 2))) (u n) = (tpR K Rp).one)
    (hw : ∀ n, (tpR K Rp).mul ((tpR K Rp).add (tpR K Rp).one (pw (tpR K Rp) (X K Rp) (n + n))) (w n) = (tpR K Rp).one) :
    (tpR K Rp).add (sum (toSemi (tpR K Rp)) (fun k => (tpR K Rp).mul (iota K Rp (castN K (k * k + k))) (pw (tpR K Rp) (X K Rp) (k * k + k))) (rng (K0 + 1)))
        (sum (toSemi (tpR K Rp)) (fun k => (tpR K Rp).mul (iota K Rp (castN K (tri (k + 1) + tri (k + 1)))) (pw (tpR K Rp) (X K Rp) (tri (k + 1) + tri (k + 1)))) (rng K0))
      = (tpR K Rp).mul
          ((tpR K Rp).mul (qp (tpR K Rp) ((tpR K Rp).mul (X K Rp) (X K Rp)) ((K0 + r0) + (K0 + r0)))
            ((tpR K Rp).mul (prodF (tpR K Rp) (fun n => (tpR K Rp).add (tpR K Rp).one (pw (tpR K Rp) (X K Rp) (n + n + 2))) (K0 + r0))
              (prodF (tpR K Rp) (fun n => (tpR K Rp).add (tpR K Rp).one (pw (tpR K Rp) (X K Rp) (n + n))) (K0 + r0))))
          ((tpR K Rp).add (sum (toSemi (tpR K Rp)) (fun i => (tpR K Rp).mul (dlt K Rp (sub (tpR K Rp) (tpR K Rp).one (pw (tpR K Rp) ((tpR K Rp).mul (X K Rp) (X K Rp)) (i + 1)))) (v i)) (rng ((K0 + r0) + (K0 + r0))))
            ((tpR K Rp).add (sum (toSemi (tpR K Rp)) (fun n => (tpR K Rp).mul ((tpR K Rp).mul (iota K Rp (castN K (n + n + 2))) (pw (tpR K Rp) (X K Rp) (n + n + 2))) (u n)) (rng (K0 + r0)))
              (sum (toSemi (tpR K Rp)) (fun n => (tpR K Rp).mul ((tpR K Rp).mul (iota K Rp (castN K (n + n))) (pw (tpR K Rp) (X K Rp) (n + n))) (w n)) (rng (K0 + r0))))) := by
  have h := congrArg (dlt K Rp) (theta2_ring K r0 K0 Rp hK2 hR)
  rw [dlt_add, dlt_sum, dlt_sum] at h
  rw [sum_ext (toSemi (tpR K Rp)) _ _ (fun k => dlt_Xpow K Rp (k * k + k)), sum_ext (toSemi (tpR K Rp)) _ _ (fun k => dlt_Xpow K Rp (tri (k + 1) + tri (k + 1)))] at h
  rw [← h]
  unfold qp
  rw [dlt_mul, dlt_mul, dlt_prodF K Rp _ v hv, dlt_prodF K Rp _ u hu, dlt_prodF K Rp _ w hw]
  rw [sum_ext (toSemi (tpR K Rp)) (fun i => (tpR K Rp).mul (dlt K Rp ((tpR K Rp).add (tpR K Rp).one (pw (tpR K Rp) (X K Rp) (i + i + 2)))) (u i)) _
      (fun n => by rw [dlt_one_add_Xpow]),
    sum_ext (toSemi (tpR K Rp)) (fun i => (tpR K Rp).mul (dlt K Rp ((tpR K Rp).add (tpR K Rp).one (pw (tpR K Rp) (X K Rp) (i + i)))) (w i)) _
      (fun n => by rw [dlt_one_add_Xpow])]
  generalize prodF (tpR K Rp) (fun i => sub (tpR K Rp) (tpR K Rp).one (pw (tpR K Rp) ((tpR K Rp).mul (X K Rp) (X K Rp)) (i + 1))) ((K0 + r0) + (K0 + r0)) = P1
  generalize prodF (tpR K Rp) (fun n => (tpR K Rp).add (tpR K Rp).one (pw (tpR K Rp) (X K Rp) (n + n + 2))) (K0 + r0) = P2
  generalize prodF (tpR K Rp) (fun n => (tpR K Rp).add (tpR K Rp).one (pw (tpR K Rp) (X K Rp) (n + n))) (K0 + r0) = P3
  lib_ring

end LibTheta2LogDeriv

#print axioms LibTheta2LogDeriv.dlt_one_add_Xpow
#print axioms LibTheta2LogDeriv.theta2_logderiv

lib/Theta2LogSubst.lean

lean_all/lib/Theta2LogSubst.lean

/-
  lib/Theta2LogSubst.lean — T46 shared library (G942–G943): substituted version of the closed form of the θ₂ logarithmic derivative, and δ(X·A).

    dlt_X_mul          δ(X·A) = X·A + X·δA                       (the Q∂_Q side of Q·θ₂(Q⁴))
    theta2_log_subst   theta2_logderiv transported by ev_Y where Y^{R′+1} = 0 (e.g. Y = Q⁴)
  Lean 4 core only, proofs by hand, no axioms.
-/
import lib.Theta2LogDeriv
import lib.ThetaSubst

namespace LibTheta2LogSubst
open DetChain LibRing LibSum LibSemi LibQBinom LibJTPFinite LibTPoly LibTPChar LibQPoch LibTPDeriv LibTPEval LibThetaSubst
  LibTheta2LogDeriv

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

theorem dlt_X_mul (N : Nat) (A : TP R N) :
    dlt K N ((tpR K N).mul (X K N) A) = (tpR K N).add ((tpR K N).mul (X K N) A) ((tpR K N).mul (X K N) (dlt K N A)) := by
  have hX : X K N = pw (tpR K N) (X K N) 1 := by show _ = (tpR K N).mul (tpR K N).one (X K N); lib_ring
  rw [dlt_mul, hX, dlt_Xpow]
  show (tpR K N).add ((tpR K N).mul ((tpR K N).mul (iota K N (K.add K.one K.zero)) _) A) _ = _
  rw [show K.add K.one K.zero = K.one by lib_ring, iota_one]; lib_ring

theorem theta2_log_subst (r0 K0 Rp M'' : Nat) (hK2 : r0 + r0 + 1 < tri K0 + tri K0 + (K0 + K0)) (hR : Rp + K0 ≤ r0 + r0 + 1)
    (v u w : Nat → TP R Rp)
    (hv : ∀ i, (tpR K Rp).mul (sub (tpR K Rp) (tpR K Rp).one (pw (tpR K Rp) ((tpR K Rp).mul (X K Rp) (X K Rp)) (i + 1))) (v i) = (tpR K Rp).one)
    (hu : ∀ n, (tpR K Rp).mul ((tpR K Rp).add (tpR K Rp).one (pw (tpR K Rp) (X K Rp) (n + n + 2))) (u n) = (tpR K Rp).one)
    (hw : ∀ n, (tpR K Rp).mul ((tpR K Rp).add (tpR K Rp).one (pw (tpR K Rp) (X K Rp) (n + n))) (w n) = (tpR K Rp).one)
    (Y : TP R M'') (hY : pw (tpR K M'') Y (Rp + 1) = (tpR K M'').zero) (hR1 : 1 ≤ Rp) :
    (tpR K M'').add (sum (toSemi (tpR K M'')) (fun k => (tpR K M'').mul (iota K M'' (castN K (k * k + k))) (pw (tpR K M'') Y (k * k + k))) (rng (K0 + 1)))
        (sum (toSemi (tpR K M'')) (fun k => (tpR K M'').mul (iota K M'' (castN K (tri (k + 1) + tri (k + 1)))) (pw (tpR K M'') Y (tri (k + 1) + tri (k + 1)))) (rng K0))
      = (tpR K M'').mul
          ((tpR K M'').mul (qp (tpR K M'') ((tpR K M'').mul Y Y) ((K0 + r0) + (K0 + r0)))
            ((tpR K M'').mul (prodF (tpR K M'') (fun n => (tpR K M'').add (tpR K M'').one (pw (tpR K M'') Y (n + n + 2))) (K0 + r0))
              (prodF (tpR K M'') (fun n => (tpR K M'').add (tpR K M'').one (pw (tpR K M'') Y (n + n))) (K0 + r0))))
          ((tpR K M'').add (sum (toSemi (tpR K M'')) (fun i => (tpR K M'').mul (ev K Rp M'' Y (dlt K Rp (sub (tpR K Rp) (tpR K Rp).one (pw (tpR K Rp) ((tpR K Rp).mul (X K Rp) (X K Rp)) (i + 1))))) (ev K Rp M'' Y (v i))) (rng ((K0 + r0) + (K0 + r0))))
            ((tpR K M'').add (sum (toSemi (tpR K M'')) (fun n => (tpR K M'').mul ((tpR K M'').mul (iota K M'' (castN K (n + n + 2))) (pw (tpR K M'') Y (n + n + 2))) (ev K Rp M'' Y (u n))) (rng (K0 + r0)))
              (sum (toSemi (tpR K M'')) (fun n => (tpR K M'').mul ((tpR K M'').mul (iota K M'' (castN K (n + n))) (pw (tpR K M'') Y (n + n))) (ev K Rp M'' Y (w n))) (rng (K0 + r0))))) := by
  have h := congrArg (ev K Rp M'' Y) (theta2_logderiv K r0 K0 Rp hK2 hR v u w hv hu hw)
  have hX := ev_X K Rp M'' Y hY hR1
  unfold qp at h ⊢
  rw [ev_add, ev_sum, ev_sum, ev_mul K _ M'' Y hY, ev_mul K _ M'' Y hY, ev_mul K _ M'' Y hY, ev_prodF K _ M'' Y hY,
    ev_prodF K _ M'' Y hY, ev_prodF K _ M'' Y hY, ev_add, ev_add, ev_sum, ev_sum, ev_sum] at h
  have l1 : ∀ m, ev K Rp M'' Y ((tpR K Rp).mul (iota K Rp (castN K m)) (pw (tpR K Rp) (X K Rp) m))
      = (tpR K M'').mul (iota K M'' (castN K m)) (pw (tpR K M'') Y m) := by
    intro m; rw [ev_iota_mul, ev_pw K _ M'' Y hY, hX]
  have f0 : ∀ i, ev K Rp M'' Y (sub (tpR K Rp) (tpR K Rp).one (pw (tpR K Rp) ((tpR K Rp).mul (X K Rp) (X K Rp)) (i + 1)))
      = sub (tpR K M'') (tpR K M'').one (pw (tpR K M'') ((tpR K M'').mul Y Y) (i + 1)) := by
    intro i
    show ev K _ M'' Y ((tpR K _).add _ ((tpR K _).neg _)) = (tpR K M'').add _ ((tpR K M'').neg _)
    rw [ev_add, ev_one, ev_neg, ev_pw K _ M'' Y hY, ev_mul K _ M'' Y hY, hX]
  have f1 : ∀ m, ev K Rp M'' Y ((tpR K Rp).add (tpR K Rp).one (pw (tpR K Rp) (X K Rp) m)) = (tpR K M'').add (tpR K M'').one (pw (tpR K M'') Y m) := by
    intro m; rw [ev_add, ev_one, ev_pw K _ M'' Y hY, hX]
  have s0 : ∀ i, ev K Rp M'' Y ((tpR K Rp).mul (dlt K Rp (sub (tpR K Rp) (tpR K Rp).one (pw (tpR K Rp) ((tpR K Rp).mul (X K Rp) (X K Rp)) (i + 1)))) (v i))
      = (tpR K M'').mul (ev K Rp M'' Y (dlt K Rp (sub (tpR K Rp) (tpR K Rp).one (pw (tpR K Rp) ((tpR K Rp).mul (X K Rp) (X K Rp)) (i + 1))))) (ev K Rp M'' Y (v i)) :=
    fun i => ev_mul K _ M'' Y hY _ _
  have s1 : ∀ (m : Nat) (z : TP R Rp), ev K Rp M'' Y ((tpR K Rp).mul ((tpR K Rp).mul (iota K Rp (castN K m)) (pw (tpR K Rp) (X K Rp) m)) z)
      = (tpR K M'').mul ((tpR K M'').mul (iota K M'' (castN K m)) (pw (tpR K M'') Y m)) (ev K Rp M'' Y z) := by
    intro m z; rw [ev_mul K _ M'' Y hY, l1]
  rw [sum_ext (toSemi (tpR K M'')) _ _ (fun k => l1 (k * k + k)), sum_ext (toSemi (tpR K M'')) _ _ (fun k => l1 (tri (k + 1) + tri (k + 1))),
    prodF_ext (tpR K M'') _ _ f0, prodF_ext (tpR K M'') _ _ (fun n => f1 (n + n + 2)), prodF_ext (tpR K M'') _ _ (fun n => f1 (n + n)),
    sum_ext (toSemi (tpR K M'')) _ _ s0, sum_ext (toSemi (tpR K M'')) _ _ (fun n => s1 (n + n + 2) (u n)),
    sum_ext (toSemi (tpR K M'')) _ _ (fun n => s1 (n + n) (w n))] at h
  exact h

end LibTheta2LogSubst

#print axioms LibTheta2LogSubst.dlt_X_mul
#print axioms LibTheta2LogSubst.theta2_log_subst

lib/Theta2Mom4.lean

lean_all/lib/Theta2Mom4.lean

/-
  lib/Theta2Mom4.lean — T46 shared library (G962): closed form of the fourth moment Σ (k²+k)² q^{k²+k} of θ₂ (truncated version).

    theta2_mom4   (under the hypotheses of Theta2LogDeriv) Σ_{k≤K₀} (k²+k)² q^{k²+k} + Σ_{k<K₀} (2tri(k+1))² q^{2tri(k+1)} = Θ₂·(L₂² + δL₂)
  Lean 4 core only, proofs by hand, no axioms.
-/
import lib.Theta2LogDeriv
import lib.TPDeriv2

namespace LibTheta2Mom4
open DetChain LibRing LibSum LibSemi LibQBinom LibJTPFinite LibTPoly LibTPChar LibQPoch LibTPDeriv LibTPLogDeriv
  LibTheta2Ring LibTheta2LogDeriv LibTPDeriv2

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

theorem theta2_mom4 (r0 K0 Rp : Nat) (hK2 : r0 + r0 + 1 < tri K0 + tri K0 + (K0 + K0)) (hR : Rp + K0 ≤ r0 + r0 + 1)
    (v u w : Nat → TP R Rp)
    (hv : ∀ i, (tpR K Rp).mul (sub (tpR K Rp) (tpR K Rp).one (pw (tpR K Rp) ((tpR K Rp).mul (X K Rp) (X K Rp)) (i + 1))) (v i) = (tpR K Rp).one)
    (hu : ∀ n, (tpR K Rp).mul ((tpR K Rp).add (tpR K Rp).one (pw (tpR K Rp) (X K Rp) (n + n + 2))) (u n) = (tpR K Rp).one)
    (hw : ∀ n, (tpR K Rp).mul ((tpR K Rp).add (tpR K Rp).one (pw (tpR K Rp) (X K Rp) (n + n))) (w n) = (tpR K Rp).one) :
    let T := tpR K Rp
    let Θ := T.mul (qp T (T.mul (X K Rp) (X K Rp)) ((K0 + r0) + (K0 + r0)))
      (T.mul (prodF T (fun n => T.add T.one (pw T (X K Rp) (n + n + 2))) (K0 + r0)) (prodF T (fun n => T.add T.one (pw T (X K Rp) (n + n))) (K0 + r0)))
    let L := T.add (sum (toSemi T) (fun i => T.mul (dlt K Rp (sub T T.one (pw T (T.mul (X K Rp) (X K Rp)) (i + 1)))) (v i)) (rng ((K0 + r0) + (K0 + r0))))
      (T.add (sum (toSemi T) (fun n => T.mul (T.mul (iota K Rp (castN K (n + n + 2))) (pw T (X K Rp) (n + n + 2))) (u n)) (rng (K0 + r0)))
        (sum (toSemi T) (fun n => T.mul (T.mul (iota K Rp (castN K (n + n))) (pw T (X K Rp) (n + n))) (w n)) (rng (K0 + r0))))
    T.add (sum (toSemi T) (fun k => T.mul (iota K Rp (K.mul (castN K (k * k + k)) (castN K (k * k + k)))) (pw T (X K Rp) (k * k + k))) (rng (K0 + 1)))
        (sum (toSemi T) (fun k => T.mul (iota K Rp (K.mul (castN K (tri (k + 1) + tri (k + 1))) (castN K (tri (k + 1) + tri (k + 1))))) (pw T (X K Rp) (tri (k + 1) + tri (k + 1)))) (rng K0))
      = T.mul Θ (T.add (T.mul L L) (dlt K Rp L)) := by
  intro T Θ L
  have hθ := theta2_ring K r0 K0 Rp hK2 hR
  have hlog := theta2_logderiv K r0 K0 Rp hK2 hR v u w hv hu hw
  have hd1 : dlt K Rp Θ = T.mul Θ L := by
    show dlt K Rp (T.mul _ _) = _
    rw [hθ, dlt_add, dlt_sum, dlt_sum, sum_ext (toSemi T) _ _ (fun k => dlt_Xpow K Rp (k * k + k)),
      sum_ext (toSemi T) _ _ (fun k => dlt_Xpow K Rp (tri (k + 1) + tri (k + 1)))]
    exact hlog
  have h2 := dlt2_of_log K Rp Θ L hd1
  rw [← h2, hd1, ← hlog, dlt_add, dlt_sum, dlt_sum]
  have t : ∀ (m : Nat), dlt K Rp (T.mul (iota K Rp (castN K m)) (pw T (X K Rp) m))
      = T.mul (iota K Rp (K.mul (castN K m) (castN K m))) (pw T (X K Rp) m) := by
    intro m; rw [dlt_const_mul, dlt_Xpow, iota_mul]; lib_ring
  rw [sum_ext (toSemi T) _ _ (fun k => t (k * k + k)), sum_ext (toSemi T) _ _ (fun k => t (tri (k + 1) + tri (k + 1)))]

end LibTheta2Mom4

#print axioms LibTheta2Mom4.theta2_mom4

lib/Theta2Prod.lean

lean_all/lib/Theta2Prod.lean

/-
  lib/Theta2Prod.lean — T46 shared library (G937): diagonal expansion of the θ₂-side product (any commutative ring).

    corr2 N j     Σ_{a≤N} q^{a²+a+2tri(a+j)}[N; a+j][N; a]        (= q^{−j}·corr_{N,j}, subtraction-free form)
    corr2_shift   q^j·corr2_{N,j} = corr_{N,j}
    prod_theta2   Π_{n<N}(1+q^{2n+2})·Π_{n<N}(1+q^{2n}) = Σ_{k≤N} q^k corr_{N,k} + Σ_{k≤N} corr2_{N,k+1}
  Lean 4 core only, proofs by hand, no axioms.
-/
import lib.Theta2Half
import lib.JTPTrunc
import lib.DiagSum

namespace LibTheta2Prod
open DetChain LibRing LibSum LibSemi LibQBinom LibJTPFinite LibMacMahon LibJTPTrunc LibDiagSum LibTheta2Half

variable {S : Type} (T : CRing S)

def corr2 (q : S) (N j : Nat) : S :=
  sum (toSemi T) (fun a => T.mul (pw T q (a * a + a + (tri (a + j) + tri (a + j))))
    (T.mul (gb T (T.mul q q) N (a + j)) (gb T (T.mul q q) N a))) (rng (N + 1))

theorem exp_shift (a j : Nat) : j + (a * a + a + (tri (a + j) + tri (a + j))) = (a + j) * (a + j) + a * a := by
  have h := tri_sq (a + j)
  rw [← h]; lib_nat

theorem corr2_shift (q : S) (N j : Nat) : T.mul (pw T q j) (corr2 T q N j) = corr T q N j := by
  unfold corr2 corr
  refine (mul_sum (toSemi T) _ _ _).trans (sum_ext (toSemi T) _ _ (fun a => ?_) _)
  show T.mul (pw T q j) (T.mul (pw T q (a * a + a + (tri (a + j) + tri (a + j)))) _) = _
  rw [← T.mul_assoc, ← pw_add', exp_shift]

theorem exp_diag1 (b k : Nat) : (b + k) * (b + k) + (b + k) + (tri b + tri b) = k + ((b + k) * (b + k) + b * b) := by
  have h := tri_sq b
  rw [← h]; lib_nat

/-- **Diagonal expansion of the θ₂-side product.** -/
theorem prod_theta2 (q : S) (N : Nat) :
    T.mul (prodF T (fun n => T.add T.one (pw T q (n + n + 2))) N) (prodF T (fun n => T.add T.one (pw T q (n + n))) N)
      = T.add (sum (toSemi T) (fun k => T.mul (pw T q k) (corr T q N k)) (rng (N + 1)))
          (sum (toSemi T) (fun k => corr2 T q N (k + 1)) (rng (N + 1))) := by
  rw [half_even1, half_even0]
  refine (sum_mul_sum (toSemi T) _ _ _ _).trans ?_
  let F : Nat → Nat → S := fun a b => T.mul (T.mul (T.mul (pw T q (a * a + a)) (gb T (T.mul q q) N a)) T.one)
    (T.mul (pw T q (tri b + tri b)) (gb T (T.mul q q) N b))
  have hA : ∀ a b, N + 1 ≤ a → F a b = T.zero := by
    intro a b h; show T.mul (T.mul (T.mul _ (gb T (T.mul q q) N a)) _) _ = _; rw [gb_above T _ N a h]; lib_ring
  have hB : ∀ a b, N + 1 ≤ b → F a b = T.zero := by
    intro a b h; show T.mul _ (T.mul _ (gb T (T.mul q q) N b)) = _; rw [gb_above T _ N b h]; lib_ring
  refine (diag_split T F (N + 1) hA hB).trans ?_
  have h1 : sum (toSemi T) (fun k => sum (toSemi T) (fun b => F (b + k) b) (rng (N + 1))) (rng (N + 1))
      = sum (toSemi T) (fun k => T.mul (pw T q k) (corr T q N k)) (rng (N + 1)) := by
    refine sum_ext (toSemi T) _ _ (fun k => ?_) _
    unfold corr
    refine Eq.trans ?_ (mul_sum (toSemi T) _ _ _).symm
    refine sum_ext (toSemi T) _ _ (fun b => ?_) _
    show T.mul (T.mul (T.mul (pw T q ((b + k) * (b + k) + (b + k))) (gb T (T.mul q q) N (b + k))) T.one)
        (T.mul (pw T q (tri b + tri b)) (gb T (T.mul q q) N b))
      = T.mul (pw T q k) (T.mul (pw T q ((b + k) * (b + k) + b * b)) (T.mul (gb T (T.mul q q) N (b + k)) (gb T (T.mul q q) N b)))
    have e : T.mul (pw T q ((b + k) * (b + k) + (b + k))) (pw T q (tri b + tri b)) = T.mul (pw T q k) (pw T q ((b + k) * (b + k) + b * b)) := by
      rw [← pw_add', ← pw_add', exp_diag1]
    generalize gb T (T.mul q q) N (b + k) = g1
    generalize gb T (T.mul q q) N b = g2
    have e2 : T.mul (T.mul (T.mul (pw T q ((b + k) * (b + k) + (b + k))) g1) T.one) (T.mul (pw T q (tri b + tri b)) g2)
        = T.mul (T.mul (pw T q ((b + k) * (b + k) + (b + k))) (pw T q (tri b + tri b))) (T.mul g1 g2) := by lib_ring
    rw [e2, e]; lib_ring
  have h2 : sum (toSemi T) (fun k => sum (toSemi T) (fun a => F a (a + k + 1)) (rng (N + 1))) (rng (N + 1))
      = sum (toSemi T) (fun k => corr2 T q N (k + 1)) (rng (N + 1)) := by
    refine sum_ext (toSemi T) _ _ (fun k => ?_) _
    unfold corr2
    refine sum_ext (toSemi T) _ _ (fun a => ?_) _
    show T.mul (T.mul (T.mul (pw T q (a * a + a)) (gb T (T.mul q q) N a)) T.one)
        (T.mul (pw T q (tri (a + (k + 1)) + tri (a + (k + 1)))) (gb T (T.mul q q) N (a + (k + 1))))
      = T.mul (pw T q (a * a + a + (tri (a + (k + 1)) + tri (a + (k + 1))))) (T.mul (gb T (T.mul q q) N (a + (k + 1))) (gb T (T.mul q q) N a))
    rw [pw_add' T q (a * a + a)]; lib_ring
  rw [h1, h2]

end LibTheta2Prod

#print axioms LibTheta2Prod.corr2_shift
#print axioms LibTheta2Prod.prod_theta2

lib/Theta2Ring.lean

lean_all/lib/Theta2Ring.lean

/-
  lib/Theta2Ring.lean — T46 shared library (G940a): the θ₂ product formula as a ring identity in a small truncated ring.

    trunc_prodF    truncation map and finite products
    theta2_ring    if R′ + K₀ ≤ 2r₀+1, 2r₀+1 < K₀(K₀+1), N = K₀ + r₀, then in R[q]/(q^{R′+1})
                   (q²;q²)_{2N}·Π_{n<N}(1+q^{2n+2})(1+q^{2n}) = Σ_{k≤K₀} q^{k²+k} + Σ_{k<K₀} q^{2tri(k+1)}
  (A form to which the substitution q ↦ Q⁴ (TPEval) can be applied.) Lean 4 core only, proofs by hand, no axioms.
-/
import lib.Theta2TP
import lib.TPTrunc

namespace LibTheta2Ring
open DetChain LibRing LibSum LibSemi LibQBinom LibJTPFinite LibTPoly LibTPChar LibQPoch LibTPTrunc LibJTPSum LibTheta2TP

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

theorem trunc_prodF (M M' : Nat) (hM : M' ≤ M) (f : Nat → TP R M) : ∀ n,
    trunc K M M' (prodF (tpR K M) f n) = prodF (tpR K M') (fun i => trunc K M M' (f i)) n
  | 0 => trunc_one K M M' hM
  | n + 1 => by
    show trunc K M M' ((tpR K M).mul (prodF (tpR K M) f n) (f n)) = (tpR K M').mul _ _
    rw [trunc_mul K M M' hM, trunc_prodF M M' hM f n]

/-- **θ₂ product formula (ring identity).** -/
theorem theta2_ring (r0 K0 R' : Nat) (hK2 : r0 + r0 + 1 < tri K0 + tri K0 + (K0 + K0)) (hR : R' + K0 ≤ r0 + r0 + 1) :
    (tpR K R').mul (qp (tpR K R') ((tpR K R').mul (X K R') (X K R')) ((K0 + r0) + (K0 + r0)))
        ((tpR K R').mul (prodF (tpR K R') (fun n => (tpR K R').add (tpR K R').one (pw (tpR K R') (X K R') (n + n + 2))) (K0 + r0))
          (prodF (tpR K R') (fun n => (tpR K R').add (tpR K R').one (pw (tpR K R') (X K R') (n + n))) (K0 + r0)))
      = (tpR K R').add (sum (toSemi (tpR K R')) (fun k => pw (tpR K R') (X K R') (k * k + k)) (rng (K0 + 1)))
          (sum (toSemi (tpR K R')) (fun k => pw (tpR K R') (X K R') (tri (k + 1) + tri (k + 1))) (rng K0)) := by
  have hM : R' ≤ r0 + r0 + 1 := Nat.le_trans (Nat.le_add_right R' K0) hR
  have h := trunc_eq_of_coeff K (r0 + r0 + 1) R' _ _ (fun d hd => theta2_tp K r0 K0 hK2 d (Nat.le_trans (Nat.add_le_add_right hd K0) hR))
  rw [trunc_mul K _ _ hM, trunc_mul K _ _ hM, trunc_qp K _ _ hM, trunc_mul K _ _ hM, trunc_X K _ _ hM,
    trunc_prodF K _ _ hM, trunc_prodF K _ _ hM, trunc_add K _ _ hM, trunc_sum K _ _ hM, trunc_sum K _ _ hM] at h
  have f1 : ∀ n, trunc K (r0 + r0 + 1) R' ((tpR K (r0 + r0 + 1)).add (tpR K (r0 + r0 + 1)).one (pw (tpR K (r0 + r0 + 1)) (X K (r0 + r0 + 1)) (n + n + 2)))
      = (tpR K R').add (tpR K R').one (pw (tpR K R') (X K R') (n + n + 2)) := by
    intro n; rw [trunc_add K _ _ hM, trunc_one K _ _ hM, trunc_pw K _ _ hM, trunc_X K _ _ hM]
  have f2 : ∀ n, trunc K (r0 + r0 + 1) R' ((tpR K (r0 + r0 + 1)).add (tpR K (r0 + r0 + 1)).one (pw (tpR K (r0 + r0 + 1)) (X K (r0 + r0 + 1)) (n + n)))
      = (tpR K R').add (tpR K R').one (pw (tpR K R') (X K R') (n + n)) := by
    intro n; rw [trunc_add K _ _ hM, trunc_one K _ _ hM, trunc_pw K _ _ hM, trunc_X K _ _ hM]
  have g1 : ∀ k, trunc K (r0 + r0 + 1) R' (pw (tpR K (r0 + r0 + 1)) (X K (r0 + r0 + 1)) (k * k + k)) = pw (tpR K R') (X K R') (k * k + k) := by
    intro k; rw [trunc_pw K _ _ hM, trunc_X K _ _ hM]
  have g2 : ∀ k, trunc K (r0 + r0 + 1) R' (pw (tpR K (r0 + r0 + 1)) (X K (r0 + r0 + 1)) (tri (k + 1) + tri (k + 1))) = pw (tpR K R') (X K R') (tri (k + 1) + tri (k + 1)) := by
    intro k; rw [trunc_pw K _ _ hM, trunc_X K _ _ hM]
  rw [prodF_ext (tpR K R') _ _ f1, prodF_ext (tpR K R') _ _ f2, sum_ext (toSemi (tpR K R')) _ _ g1, sum_ext (toSemi (tpR K R')) _ _ g2] at h
  exact h

end LibTheta2Ring

#print axioms LibTheta2Ring.trunc_prodF
#print axioms LibTheta2Ring.theta2_ring

lib/Theta2Subst.lean

lean_all/lib/Theta2Subst.lean

/-
  lib/Theta2Subst.lean — T46 shared library (G940b): substitution version of the θ₂ product formula.

    theta2_subst   if Y^{R′+1} = 0 (e.g. Y = Q⁴, 4(R′+1) > M″) then in R[Q]/(Q^{M″+1})
                   (Y²;Y²)_{2N}·Π_{n<N}(1+Y^{2n+2})(1+Y^{2n}) = Σ_{k≤K₀} Y^{k²+k} + Σ_{k<K₀} Y^{2tri(k+1)}
  Multiplying Y = Q⁴ by Q gives the Σ_{d odd} Q^{d²} = 2Σ_k Q^{(2k+1)²} side (agent3 G44d odd A_p). Lean 4 core only, proofs by hand, no axioms.
-/
import lib.Theta2Ring
import lib.ThetaSubst

namespace LibTheta2Subst
open DetChain LibRing LibSum LibSemi LibQBinom LibJTPFinite LibTPoly LibTPChar LibQPoch LibTPEval LibThetaSubst LibTheta2Ring

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

theorem theta2_subst (r0 K0 R' M'' : Nat) (hK2 : r0 + r0 + 1 < tri K0 + tri K0 + (K0 + K0)) (hR : R' + K0 ≤ r0 + r0 + 1)
    (Y : TP R M'') (hY : pw (tpR K M'') Y (R' + 1) = (tpR K M'').zero) (hR1 : 1 ≤ R') :
    (tpR K M'').mul (qp (tpR K M'') ((tpR K M'').mul Y Y) ((K0 + r0) + (K0 + r0)))
        ((tpR K M'').mul (prodF (tpR K M'') (fun n => (tpR K M'').add (tpR K M'').one (pw (tpR K M'') Y (n + n + 2))) (K0 + r0))
          (prodF (tpR K M'') (fun n => (tpR K M'').add (tpR K M'').one (pw (tpR K M'') Y (n + n))) (K0 + r0)))
      = (tpR K M'').add (sum (toSemi (tpR K M'')) (fun k => pw (tpR K M'') Y (k * k + k)) (rng (K0 + 1)))
          (sum (toSemi (tpR K M'')) (fun k => pw (tpR K M'') Y (tri (k + 1) + tri (k + 1))) (rng K0)) := by
  have h := congrArg (ev K R' M'' Y) (theta2_ring K r0 K0 R' hK2 hR)
  have hX := ev_X K R' M'' Y hY hR1
  unfold qp at h ⊢
  rw [ev_mul K _ M'' Y hY, ev_mul K _ M'' Y hY, ev_prodF K _ M'' Y hY, ev_prodF K _ M'' Y hY, ev_prodF K _ M'' Y hY,
    ev_add, ev_sum, ev_sum] at h
  have f0 : ∀ i, ev K R' M'' Y (sub (tpR K R') (tpR K R').one (pw (tpR K R') ((tpR K R').mul (X K R') (X K R')) (i + 1)))
      = sub (tpR K M'') (tpR K M'').one (pw (tpR K M'') ((tpR K M'').mul Y Y) (i + 1)) := by
    intro i
    show ev K _ M'' Y ((tpR K _).add _ ((tpR K _).neg _)) = (tpR K M'').add _ ((tpR K M'').neg _)
    rw [ev_add, ev_one, ev_neg, ev_pw K _ M'' Y hY, ev_mul K _ M'' Y hY, hX]
  have f1 : ∀ n, ev K R' M'' Y ((tpR K R').add (tpR K R').one (pw (tpR K R') (X K R') (n + n + 2)))
      = (tpR K M'').add (tpR K M'').one (pw (tpR K M'') Y (n + n + 2)) := by
    intro n; rw [ev_add, ev_one, ev_pw K _ M'' Y hY, hX]
  have f2 : ∀ n, ev K R' M'' Y ((tpR K R').add (tpR K R').one (pw (tpR K R') (X K R') (n + n)))
      = (tpR K M'').add (tpR K M'').one (pw (tpR K M'') Y (n + n)) := by
    intro n; rw [ev_add, ev_one, ev_pw K _ M'' Y hY, hX]
  have g1 : ∀ k, ev K R' M'' Y (pw (tpR K R') (X K R') (k * k + k)) = pw (tpR K M'') Y (k * k + k) := by
    intro k; rw [ev_pw K _ M'' Y hY, hX]
  have g2 : ∀ k, ev K R' M'' Y (pw (tpR K R') (X K R') (tri (k + 1) + tri (k + 1))) = pw (tpR K M'') Y (tri (k + 1) + tri (k + 1)) := by
    intro k; rw [ev_pw K _ M'' Y hY, hX]
  rw [prodF_ext (tpR K M'') _ _ f0, prodF_ext (tpR K M'') _ _ f1, prodF_ext (tpR K M'') _ _ f2,
    sum_ext (toSemi (tpR K M'')) _ _ g1, sum_ext (toSemi (tpR K M'')) _ _ g2] at h
  exact h

end LibTheta2Subst

#print axioms LibTheta2Subst.theta2_subst

lib/Theta2TP.lean

lean_all/lib/Theta2TP.lean

/-
  lib/Theta2TP.lean — T46 shared library (G938–G939): truncated version of the θ₂ product formula (coefficient form).

  R[q]/(q^{2r₀+2}) with q = X, N = K₀ + r₀, 2r₀+1 < K₀(K₀+1):
    corr2_fixed    j ≤ K₀, d + j ≤ 2r₀+1 ⇒ [q^d] (q²;q²)_{2N}·corr2_{N,j} = [q^d] q^{2tri j}
    corr2_high     2r₀+1 < 2tri j ⇒ corr2_{N,j} = 0
    theta2_tp      d + K₀ ≤ 2r₀+1 ⇒ [q^d] (q²;q²)_{2N}·Π_{n<N}(1+q^{2n+2})(1+q^{2n})
                                  = [q^d] (Σ_{k≤K₀} q^{k²+k} + Σ_{k<K₀} q^{2tri(k+1)})      (= 2Σ_{k≥0} q^{k(k+1)})
  i.e. (q²;q²)_∞ Π_{n≥1}(1+q^{2n})(1+q^{2n−2}) = Σ_{k∈ℤ} q^{k(k+1)} (θ₂ form; with q = Q⁴, Σ_{d odd} Q^{d²} = Q·Σ Q^{4k(k+1)} — agent3 G44d).
  Lean 4 core only, proofs by hand, no axioms.
-/
import lib.Theta2Prod
import lib.JTPThetaTP
import lib.TPShift
import lib.NatLe

namespace LibTheta2TP
open DetChain LibRing LibSum LibSemi LibQBinom LibJTPFinite LibMacMahon LibJTPTrunc LibJTPSum LibJTPThetaTP LibTheta2Prod
  LibTPoly LibTPChar LibTPShift LibQPoch

variable {R : Type} (K : CRing R) (r0 : Nat)

theorem corr2_fixed (K0 j : Nat) (hj : j ≤ K0) (d : Nat) (hd : d + j ≤ r0 + r0 + 1) :
    coeff K (r0 + r0 + 1) ((tpR K (r0 + r0 + 1)).mul (qp (tpR K (r0 + r0 + 1)) ((tpR K (r0 + r0 + 1)).mul (X K (r0 + r0 + 1)) (X K (r0 + r0 + 1))) ((K0 + r0) + (K0 + r0)))
        (corr2 (tpR K (r0 + r0 + 1)) (X K (r0 + r0 + 1)) (K0 + r0) j)) d
      = coeff K (r0 + r0 + 1) (pw (tpR K (r0 + r0 + 1)) (X K (r0 + r0 + 1)) (tri j + tri j)) d := by
  have h := jtp_fixed K K0 r0 j hj
  rw [← corr2_shift] at h
  apply cancel_Xpow K (r0 + r0 + 1) _ _ j _ d hd
  rw [← pw_add' (tpR K (r0 + r0 + 1)), show tri j + tri j + j = j * j from tri_sq j, ← h]
  generalize qp _ _ _ = P
  generalize corr2 _ _ _ _ = Q
  lib_ring

theorem corr2_high (N j : Nat) (hj : r0 + r0 + 1 < tri j + tri j) : corr2 (tpR K (r0 + r0 + 1)) (X K (r0 + r0 + 1)) N j = (tpR K (r0 + r0 + 1)).zero := by
  unfold corr2
  rw [sum_ext (toSemi (tpR K (r0 + r0 + 1))) _ (fun _ => (tpR K (r0 + r0 + 1)).zero) (fun a => by
    rw [LibQPoch.X_pow_high K (r0 + r0 + 1) _ (Nat.lt_of_lt_of_le hj (by
      have h1 : tri j ≤ tri (a + j) := by
        rw [Nat.add_comm a j, tri_add j a]; exact Nat.le_trans (Nat.le_add_right _ _) (Nat.le_add_right _ _)
      exact Nat.le_trans (Nat.add_le_add h1 h1) (Nat.le_add_left _ _)))]
    lib_ring)]
  exact sum_zero (toSemi (tpR K (r0 + r0 + 1))) _

theorem tri_mono (K0 j : Nat) (h : K0 + 1 ≤ j) : tri K0 + tri K0 + (K0 + K0) ≤ tri j + tri j := by
  obtain ⟨e, he⟩ := exists_add h
  rw [he, tri_add (K0 + 1) e]
  show tri K0 + tri K0 + (K0 + K0) ≤ tri K0 + K0 + tri e + (K0 + 1) * e + (tri K0 + K0 + tri e + (K0 + 1) * e)
  lib_nat_le

/-- **Truncated θ₂ product formula (coefficient form).** -/
theorem theta2_tp (K0 : Nat) (hK2 : r0 + r0 + 1 < tri K0 + tri K0 + (K0 + K0)) (d : Nat) (hd : d + K0 ≤ r0 + r0 + 1) :
    coeff K (r0 + r0 + 1) ((tpR K (r0 + r0 + 1)).mul (qp (tpR K (r0 + r0 + 1)) ((tpR K (r0 + r0 + 1)).mul (X K (r0 + r0 + 1)) (X K (r0 + r0 + 1))) ((K0 + r0) + (K0 + r0)))
        ((tpR K (r0 + r0 + 1)).mul (prodF (tpR K (r0 + r0 + 1)) (fun n => (tpR K (r0 + r0 + 1)).add (tpR K (r0 + r0 + 1)).one (pw (tpR K (r0 + r0 + 1)) (X K (r0 + r0 + 1)) (n + n + 2))) (K0 + r0))
          (prodF (tpR K (r0 + r0 + 1)) (fun n => (tpR K (r0 + r0 + 1)).add (tpR K (r0 + r0 + 1)).one (pw (tpR K (r0 + r0 + 1)) (X K (r0 + r0 + 1)) (n + n))) (K0 + r0)))) d
      = coeff K (r0 + r0 + 1) ((tpR K (r0 + r0 + 1)).add (sum (toSemi (tpR K (r0 + r0 + 1))) (fun k => pw (tpR K (r0 + r0 + 1)) (X K (r0 + r0 + 1)) (k * k + k)) (rng (K0 + 1)))
          (sum (toSemi (tpR K (r0 + r0 + 1))) (fun k => pw (tpR K (r0 + r0 + 1)) (X K (r0 + r0 + 1)) (tri (k + 1) + tri (k + 1))) (rng K0))) d := by
  have hdM : d ≤ r0 + r0 + 1 := Nat.le_trans (Nat.le_add_right d K0) hd
  rw [prod_theta2, (tpR K (r0 + r0 + 1)).distrib, coeff_add K _ _ _ d hdM, coeff_add K _ _ _ d hdM]
  -- first sum: ring identity
  have hk1 : ∀ k, K0 + 1 ≤ k → corr (tpR K (r0 + r0 + 1)) (X K (r0 + r0 + 1)) (K0 + r0) k = (tpR K (r0 + r0 + 1)).zero := by
    intro k hk
    apply corr_high K r0
    have h2 := tri_mono K0 k hk
    have h3 : tri k + tri k + k = k * k := tri_sq k
    calc r0 + r0 + 1 < tri K0 + tri K0 + (K0 + K0) := hK2
      _ ≤ tri k + tri k := h2
      _ ≤ k * k := by rw [← h3]; exact Nat.le_add_right _ _
  have p1 : (tpR K (r0 + r0 + 1)).mul (qp (tpR K (r0 + r0 + 1)) ((tpR K (r0 + r0 + 1)).mul (X K (r0 + r0 + 1)) (X K (r0 + r0 + 1))) ((K0 + r0) + (K0 + r0))) (sum (toSemi (tpR K (r0 + r0 + 1))) (fun k => (tpR K (r0 + r0 + 1)).mul (pw (tpR K (r0 + r0 + 1)) (X K (r0 + r0 + 1)) k) (corr (tpR K (r0 + r0 + 1)) (X K (r0 + r0 + 1)) (K0 + r0) k)) (rng (K0 + r0 + 1)))
      = sum (toSemi (tpR K (r0 + r0 + 1))) (fun k => pw (tpR K (r0 + r0 + 1)) (X K (r0 + r0 + 1)) (k * k + k)) (rng (K0 + 1)) := by
    refine (mul_sum (toSemi (tpR K (r0 + r0 + 1))) _ _ _).trans ?_
    rw [show K0 + r0 + 1 = (K0 + 1) + r0 by lib_nat, sum_rng_add]
    rw [sum_ext (toSemi (tpR K (r0 + r0 + 1))) (fun j => (toSemi (tpR K (r0 + r0 + 1))).mul (qp (tpR K (r0 + r0 + 1)) ((tpR K (r0 + r0 + 1)).mul (X K (r0 + r0 + 1)) (X K (r0 + r0 + 1))) ((K0 + r0) + (K0 + r0))) ((tpR K (r0 + r0 + 1)).mul (pw (tpR K (r0 + r0 + 1)) (X K (r0 + r0 + 1)) (K0 + 1 + j)) (corr (tpR K (r0 + r0 + 1)) (X K (r0 + r0 + 1)) (K0 + r0) (K0 + 1 + j))))
      (fun _ => (tpR K (r0 + r0 + 1)).zero) (fun j => by show (tpR K (r0 + r0 + 1)).mul _ _ = _; rw [hk1 _ (Nat.le_add_right _ _)]; lib_ring)]
    rw [show sum (toSemi (tpR K (r0 + r0 + 1))) (fun _ => (tpR K (r0 + r0 + 1)).zero) (rng r0) = (tpR K (r0 + r0 + 1)).zero from sum_zero (toSemi (tpR K (r0 + r0 + 1))) _]
    refine ((toSemi (tpR K (r0 + r0 + 1))).add_comm _ _ |>.trans ((toSemi (tpR K (r0 + r0 + 1))).zero_add _)).trans ?_
    refine sum_ext_mem (toSemi (tpR K (r0 + r0 + 1))) _ _ _ (fun k hk => ?_)
    have h := jtp_fixed K K0 r0 k (Nat.le_of_lt_succ (LibBinom.mem_rng hk))
    show (tpR K (r0 + r0 + 1)).mul _ ((tpR K (r0 + r0 + 1)).mul (pw (tpR K (r0 + r0 + 1)) (X K (r0 + r0 + 1)) k) _) = _
    rw [pw_add' (tpR K (r0 + r0 + 1)) (X K (r0 + r0 + 1)) (k * k) k, ← h]
    generalize qp _ _ _ = P
    generalize corr _ _ _ _ = Qc
    lib_ring
  rw [p1]
  refine congrArg (K.add _) ?_
  -- second sum: coefficientwise
  have hm : (tpR K (r0 + r0 + 1)).mul (qp (tpR K (r0 + r0 + 1)) ((tpR K (r0 + r0 + 1)).mul (X K (r0 + r0 + 1)) (X K (r0 + r0 + 1))) ((K0 + r0) + (K0 + r0))) (sum (toSemi (tpR K (r0 + r0 + 1))) (fun k => corr2 (tpR K (r0 + r0 + 1)) (X K (r0 + r0 + 1)) (K0 + r0) (k + 1)) (rng (K0 + r0 + 1)))
      = sum (toSemi (tpR K (r0 + r0 + 1))) (fun k => (tpR K (r0 + r0 + 1)).mul (qp (tpR K (r0 + r0 + 1)) ((tpR K (r0 + r0 + 1)).mul (X K (r0 + r0 + 1)) (X K (r0 + r0 + 1))) ((K0 + r0) + (K0 + r0))) (corr2 (tpR K (r0 + r0 + 1)) (X K (r0 + r0 + 1)) (K0 + r0) (k + 1))) (rng (K0 + r0 + 1)) :=
    mul_sum (toSemi (tpR K (r0 + r0 + 1))) _ _ _
  rw [hm]
  rw [coeff_sum K (r0 + r0 + 1) _ d hdM, coeff_sum K (r0 + r0 + 1) _ d hdM]
  rw [show K0 + r0 + 1 = K0 + (r0 + 1) by lib_nat, sum_rng_add (toSemi K)]
  have hz : sum (toSemi K) (fun j => coeff K (r0 + r0 + 1) ((tpR K (r0 + r0 + 1)).mul (qp (tpR K (r0 + r0 + 1)) ((tpR K (r0 + r0 + 1)).mul (X K (r0 + r0 + 1)) (X K (r0 + r0 + 1))) ((K0 + r0) + (K0 + r0))) (corr2 (tpR K (r0 + r0 + 1)) (X K (r0 + r0 + 1)) (K0 + r0) (K0 + j + 1))) d) (rng (r0 + 1)) = K.zero := by
    rw [sum_ext (toSemi K) _ (fun _ => K.zero) (fun j => by
      show coeff K _ ((tpR K (r0 + r0 + 1)).mul _ (corr2 (tpR K (r0 + r0 + 1)) (X K (r0 + r0 + 1)) (K0 + r0) (K0 + j + 1))) d = K.zero
      rw [corr2_high K r0 _ (K0 + j + 1) (Nat.lt_of_lt_of_le hK2 (tri_mono K0 (K0 + j + 1) (by
        show K0 + 1 ≤ K0 + j + 1; exact Nat.succ_le_succ (Nat.le_add_right K0 j))))]
      rw [show (tpR K (r0 + r0 + 1)).mul (qp (tpR K (r0 + r0 + 1)) ((tpR K (r0 + r0 + 1)).mul (X K (r0 + r0 + 1)) (X K (r0 + r0 + 1))) ((K0 + r0) + (K0 + r0))) (tpR K (r0 + r0 + 1)).zero = (tpR K (r0 + r0 + 1)).zero by lib_ring]
      exact coeff_zero K _ d hdM)]
    exact sum_zero (toSemi K) _
  rw [hz]
  show K.add _ K.zero = _
  rw [show ∀ z : R, K.add z K.zero = z from fun z => by lib_ring]
  refine sum_ext_mem (toSemi K) _ _ _ (fun k hk => ?_)
  exact corr2_fixed K r0 K0 (k + 1) (LibBinom.mem_rng hk) d (Nat.le_trans (Nat.add_le_add_left (LibBinom.mem_rng hk) d) hd)

end LibTheta2TP

#print axioms LibTheta2TP.corr2_fixed
#print axioms LibTheta2TP.corr2_high
#print axioms LibTheta2TP.theta2_tp

lib/Theta3.lean

lean_all/lib/Theta3.lean

/-
  lib/Theta3.lean — T46 shared library (G1519c): the theta series of x² + xy + y² is an Eisenstein series.

    leg3_of_chi    χ₋₃(p) = 1 ⇒ p is a square mod 3
    leg_p3         p = 2(m+1)+1 prime ≠ 3, χ₋₃(p) = 1 ⇒ (3/p) = (−1/p)          (quadratic reciprocity)
    root3          … ⇒ ∃ t, p ∣ t² + t + 1
    chi3_cases     χ₋₃(n) ∈ {1, −1, 0}, χ₋₃(n) = 0 ⇒ 3 ∣ n
    prime_data     every prime is split (χ = 1), inert (χ = −1) or ramified (p = 3 = N(1, 1))
    RH_one         RH 0 1 = 3      (six units)
    theta3_half    n > 0 ⇒ RH(n) = 3 Σ_{d ∣ n} χ₋₃(d)                           (HeckeScale.eis_scale)
    theta3         n > 0 ⇒ #{(x, y) ∈ ℤ² : x² + xy + y² = n} = 6 Σ_{d ∣ n} χ₋₃(d)
  Lean 4 core only, proofs by hand, no axioms.
-/
import lib.Split3
import lib.Theta11
import lib.HeckeScale

namespace LibTheta3
open LibMod LibGcd LibEulerCrit LibZp LibIntBox LibNormPiA LibNormPiB LibNormPiF LibChi11 LibSplit3

def chkLeg : Bool := allB 3 (fun r => !(ieq (F3 r) 1) || QRChain.legB 3 r)

theorem chkLeg_true : chkLeg = true := by decide

theorem leg3_of_chi {p : Nat} (hc : chi3 p = 1) : QRChain.legB 3 p = true := by
  have hr : md 3 p < 3 := md_lt (by decide) p
  have hrem : FTAChain.rem 3 p = FTAChain.rem 3 (md 3 p) := by
    rw [show (3 : Nat) = 2 + 1 from rfl, LibFTAB.rem_eq_md, LibFTAB.rem_eq_md, md_md (Nat.succ_pos 2)]
  have e : QRChain.legB 3 p = QRChain.legB 3 (md 3 p) := LibQR11.sqS_congr 3 p (md 3 p) hrem 3
  rw [e]
  have t := allB_spec 3 _ chkLeg_true (md 3 p) hr
  have hi : ieq (F3 (md 3 p)) 1 = true := by
    have : F3 (md 3 p) = 1 := hc
    rw [this]; exact ieq_self 1
  rw [hi] at t
  exact t

variable (m : Nat)

theorem leg_p3 (hp : Prime1 (k m)) (hne : k m + 1 ≠ 3) (hc : chi3 (k m + 1) = 1) :
    QRChain.legB (k m + 1) 3 = !QRChain.par (m + 1) := by
  have ek : k m + 1 = (m + 1) + (m + 1) + 1 := by show 2 * (m + 1) + 1 = _; lib_nat
  have hP : FTAChain.Pr ((m + 1) + (m + 1) + 1) := by
    rw [← ek]; exact LibLLNum.pr_of_prime1 (k m) (Nat.succ_le_succ (Nat.zero_le _)) hp
  have hQ : FTAChain.Pr (1 + 1 + 1) := LibFTAB.pr_of_isPrimeB (by decide)
  have hne' : m + 1 ≠ 1 := fun e => hne (by rw [ek, e])
  have q := QRChain.qr_law (m + 1) 1 hP hQ hne'
  have h3 : QRChain.legB (1 + 1 + 1) ((m + 1) + (m + 1) + 1) = true := by
    rw [← ek]; exact leg3_of_chi hc
  rw [h3, show (m + 1) * 1 = m + 1 by lib_nat] at q
  have q' : QRChain.bx true (QRChain.legB ((m + 1) + (m + 1) + 1) (1 + 1 + 1)) = QRChain.par (m + 1) := by
    cases hb : QRChain.legB ((m + 1) + (m + 1) + 1) (1 + 1 + 1) <;> rw [hb] at q <;> exact q
  rw [LibQR11.bx_true] at q'
  rw [ek, ← q', LibQR11.not_not']

theorem mk3_ne (hne : k m + 1 ≠ 3) : mk (k m) 3 ≠ (znR (k m)).zero := fun e => by
  have h1 := congrArg Subtype.val e
  have h2 : md (k m + 1) 0 = 0 := md_of_lt (pos (k m)) (pos (k m))
  have h0 : md (k m + 1) 3 = 0 := by rw [← h2]; exact h1
  have hd : k m + 1 ∣ 3 := dvd_of_md_zero h0
  have p3 : LibPrimePow.Prime 3 := LibPrimeB.prime_of_isPrimeB (by decide)
  cases p3.2 _ hd with
  | inl e1 => exact Nat.noConfusion (show 2 * (m + 1) = 0 from Nat.succ.inj e1)
  | inr e1 => exact hne e1

theorem sq_neg3 (hp : Prime1 (k m)) (hne : k m + 1 ≠ 3) (hc : chi3 (k m + 1) = 1) :
    ∃ x : Nat, IDv ((k m + 1 : Nat) : Int) ((x : Int) * x + 3) := by
  have hl := LibLegMul.legB_mul m hp (LibLegNeg.mk_ne m) (mk3_ne m hne)
  rw [LibLegNeg.legB_neg_one m hp, leg_p3 m hp hne hc] at hl
  have ht : QRChain.legB (k m + 1) (k m * 3) = true := by
    rw [hl]; cases QRChain.par (m + 1) <;> rfl
  obtain ⟨x, hx⟩ := (QRChain.legB_iff (k m) (k m * 3)).1 ht
  rw [LibFTAB.rem_eq_md, LibFTAB.rem_eq_md] at hx
  obtain ⟨c, hc'⟩ := LibThue.idv_of_md hx
  refine ⟨x, c + 3, ?_⟩
  have e1 : ((k m * 3 : Nat) : Int) = ((k m : Nat) : Int) * 3 := rfl
  have e2 : ((k m + 1 : Nat) : Int) = ((k m : Nat) : Int) + 1 := rfl
  have e3 : ((x * x : Nat) : Int) = (x : Int) * x := rfl
  rw [e1, e3] at hc'
  rw [e2] at hc' ⊢
  have : (x : Int) * x = ((k m : Nat) : Int) * 3 + (((k m : Nat) : Int) + 1) * c := by
    rw [← hc']; lib_int
  rw [this]; lib_int

theorem root3 (hp : Prime1 (k m)) (hne : k m + 1 ≠ 3) (hc : chi3 (k m + 1) = 1) :
    ∃ t : Nat, IDv ((k m + 1 : Nat) : Int) ((t : Int) * t + t + 1) := by
  obtain ⟨x, c, hx⟩ := sq_neg3 m hp hne hc
  have hodd : ∃ j, k m + 1 = 2 * j + 1 := ⟨m + 1, rfl⟩
  have h1 : k m + 1 ≠ 1 := fun e => Nat.noConfusion (show 2 * (m + 1) = 0 from Nat.succ.inj e)
  have h2 : k m + 1 ≠ 2 := fun e => Nat.noConfusion (show 2 * m + 1 = 0 from Nat.succ.inj (Nat.succ.inj (by
    show 2 * (m + 1) + 1 = 2; exact e)))
  have hN : ∃ T : Int, IDv ((k m + 1 : Nat) : Int) (T * T + T + 1) := by
    obtain ⟨T, hT | hT⟩ := LibQR11.odd_rep (k m + 1) hodd x
    · refine ⟨T, LibQR11.four_dvd hp h1 h2 (d := c) ?_⟩
      have : 4 * (T * T + T + 1) = (2 * T + 1) * (2 * T + 1) + 3 := by lib_int
      rw [this, hT]; exact hx
    · refine ⟨T, LibQR11.four_dvd hp h1 h2 (d := c + 2 * x + ((k m + 1 : Nat) : Int)) ?_⟩
      have : 4 * (T * T + T + 1) = (2 * T + 1) * (2 * T + 1) + 3 := by lib_int
      rw [this, hT]
      have e : (x + ((k m + 1 : Nat) : Int)) * (x + ((k m + 1 : Nat) : Int)) + 3
          = (x * x + 3) + ((k m + 1 : Nat) : Int) * (2 * x + ((k m + 1 : Nat) : Int)) := by lib_int
      rw [e, hx]; lib_int
  obtain ⟨T, a, ha⟩ := hN
  obtain ⟨tn, b, hb⟩ := res_nat (pos (k m)) T
  refine ⟨tn, a + b * ((tn : Int) + T + 1), ?_⟩
  have e : (tn : Int) * tn + tn + 1 = (T * T + T + 1) + ((tn : Int) - T) * ((tn : Int) + T + 1) := by lib_int
  rw [e, ha, hb]; lib_int

def chkCases : Bool := allB 3 (fun r => ieq (F3 r) 1 || ieq (F3 r) (-1) || (ieq (F3 r) 0 && Nat.beq r 0))

theorem chkCases_true : chkCases = true := by decide

theorem chi3_cases (n : Nat) : chi3 n = 1 ∨ chi3 n = -1 ∨ (chi3 n = 0 ∧ 3 ∣ n) := by
  have hr : md 3 n < 3 := md_lt (by decide) n
  have t := allB_spec 3 _ chkCases_true (md 3 n) hr
  rcases LibTheta11.or3 t with h | h | h
  · exact Or.inl (ieq_eq h)
  · exact Or.inr (Or.inl (ieq_eq h))
  · obtain ⟨h1, h2⟩ := LibTwoSqPiQ.and_split h
    exact Or.inr (Or.inr ⟨ieq_eq h1, dvd_of_md_zero (Nat.eq_of_beq_eq_true h2)⟩)

theorem p3_eq {p : Nat} (hP : LibPrimePow.Prime p) (h : 3 ∣ p) : p = 3 := by
  cases hP.2 3 h with
  | inl e => exact absurd e (by decide)
  | inr e => exact e.symm

theorem ndvd3 {p : Nat} (hP : LibPrimePow.Prime p) (hne : p ≠ 3) : ¬ p ∣ 3 := fun hd => by
  have p3 : LibPrimePow.Prime 3 := LibPrimeB.prime_of_isPrimeB (by decide)
  cases p3.2 p hd with
  | inl e => exact absurd (e ▸ hP.1) (Nat.lt_irrefl 1)
  | inr e => exact hne e

theorem n3_2 : ¬ 3 ∣ 2 := fun hd => by
  have p2 : LibPrimePow.Prime 2 := LibPrimeB.prime_of_isPrimeB (by decide)
  cases p2.2 3 hd with
  | inl e => exact absurd e (by decide)
  | inr e => exact absurd e (by decide)

theorem prime_data (p : Nat) (hP : LibPrimePow.Prime p) :
    (chi3 p = 1 ∧ (∃ u v : Int, Nf (0 + 1) u v = (p : Int)) ∧ ¬ p ∣ 4 * 0 + 3)
      ∨ (chi3 p = -1 ∧ ∀ x y : Int, IDv (p : Int) (Nf (0 + 1) x y) → IDv (p : Int) x ∧ IDv (p : Int) y)
      ∨ (chi3 p = 0 ∧ (∃ u v : Int, Nf (0 + 1) u v = (p : Int)) ∧ ¬ p ∣ 2 ∧ p ∣ 4 * 0 + 3) := by
  rcases chi3_cases p with hc | hc | ⟨hc, hd⟩
  · have hne : p ≠ 3 := fun e => by rw [e] at hc; exact absurd hc (by decide)
    have h2 : ¬ p ∣ 2 := fun hd2 => by
      have p2 : LibPrimePow.Prime 2 := LibPrimeB.prime_of_isPrimeB (by decide)
      cases p2.2 p hd2 with
      | inl e => exact absurd (e ▸ hP.1) (Nat.lt_irrefl 1)
      | inr e => rw [e] at hc; exact absurd hc (by decide)
    obtain ⟨m, hm⟩ := LibTheta11.odd_prime_k hP h2
    have hp1 : LibZp.Prime1 (LibEulerCrit.k m) := LibPrimeB.prime1_of_prime (hm ▸ hP)
    obtain ⟨t, ht⟩ := root3 m hp1 (hm ▸ hne) (hm ▸ hc)
    rw [← hm] at ht
    exact Or.inl ⟨hc, pi_of_root3 hP h2 t ht, ndvd3 hP hne⟩
  · exact Or.inr (Or.inl ⟨hc, inert3 hP hc⟩)
  · have e := p3_eq hP hd
    subst e
    exact Or.inr (Or.inr ⟨hc, ⟨1, 1, rfl⟩, n3_2, ⟨1, rfl⟩⟩)

theorem RH_one : RH 0 1 = 3 := by decide

theorem theta3_half {n : Nat} (hn : 0 < n) :
    (RH 0 n : Int) = 3 * LibEisGen.e LibInt.intR chi3 (LibDivFun.one1 LibInt.intR) n := by
  have h := LibHeckeScale.eis_scale LibInt.intR chi3 (LibDivFun.one1 LibInt.intR) chi3_cmult
    (LibEisBridge.one1_cmult LibInt.intR) (fun n => (RH 0 n : Int)) (RH_eig 0 chi3 prime_data) hn
  have h1 : ((RH 0 1 : Nat) : Int) = 3 := by rw [RH_one]; rfl
  rw [h1] at h
  exact h

theorem theta3 {n : Nat} (hn : 0 < n) :
    (RZ 0 n : Int) = 6 * LibEisGen.e LibInt.intR chi3 (LibDivFun.one1 LibInt.intR) n := by
  rw [RZ_eq_RH 0 hn, LibTwoSqJacobi.cast_add, theta3_half hn]; lib_int

end LibTheta3

#print axioms LibTheta3.leg3_of_chi
#print axioms LibTheta3.leg_p3
#print axioms LibTheta3.mk3_ne
#print axioms LibTheta3.sq_neg3
#print axioms LibTheta3.root3
#print axioms LibTheta3.chi3_cases
#print axioms LibTheta3.prime_data
#print axioms LibTheta3.RH_one
#print axioms LibTheta3.theta3_half
#print axioms LibTheta3.theta3

lib/Theta31.lean

lean_all/lib/Theta31.lean

/-
  lib/Theta31.lean — T46 shared library (G1583f): discriminant −31, class number 3,
  f₀ = x² + xy + 8y², f₁ = 2x² + xy + 4y², χ = (·/31):

      r₀(n) + 2 r₁(n) = 2 Σ_{d ∣ n} χ(d)   (n > 0),    2·(T_p D)(n) = D(p)·D(n)   (D = r₀ − r₁, every prime p)

  The same steps as Theta23 (√−31 = −1 + 2ω for the ramified prime; 8p = (4x + y)² + 31y² for f₁).
  Lean 4 core only, proofs by hand, no axioms.
-/
import lib.ThetaGenus3
import lib.Split31
import lib.ThetaQ

namespace LibTheta31
open LibMod LibGcd LibIntBox LibNormPiA LibNormPiC LibFormCount LibFormRel LibFormRel3 LibFormRam LibChi11
  LibSplit31 LibPair31A LibPair31B LibPair31Small LibThetaGenus3

def chi31 (n : Nat) : Int := LibThetaQ.chiQ 31 n

theorem P31' : LibZp.Prime1 (LibEulerCrit.k 14) := LibPrimeB.prime1_of_prime (LibPrimeB.prime_of_isPrimeB (by decide))
theorem P31 : LibPrimePow.Prime 31 := LibPrimeB.prime_of_isPrimeB (by decide)

theorem chi31_cmult : LibDirTwist.CMult LibInt.intR chi31 := LibThetaQ.chiQ_cmult 14 P31'

/-! ### The ramified prime 31 -/

def A0r (x y : Int) : Int × Int := (-x - 16 * y, 2 * x + y)
def B0r (x y : Int) : Int × Int := (x + 16 * y, -(2 * x) - y)
def A1r (x y : Int) : Int × Int := (-x - 8 * y, 4 * x + y)
def B1r (x y : Int) : Int × Int := (x + 8 * y, -(4 * x) - y)

theorem c31 : ((31 : Nat) : Int) = 31 := rfl

theorem ram31_0 (n : Nat) : RF f0 (31 * n) = RF f0 n :=
  ram_gen f0 f0 A0r B0r (p := 31) (by decide) n f0_bound f0_bound
    (fun x y => by unfold A0r; dsimp only; rw [c31]; unfold f0; lib_int)
    (fun x y => by unfold B0r; dsimp only; rw [c31]; unfold f0; lib_int)
    (fun x y => by unfold A0r B0r; dsimp only; rw [c31]; lib_int)
    (fun x y => by unfold A0r B0r; dsimp only; rw [c31]; lib_int)
    (fun x y => by unfold A0r B0r; dsimp only; rw [c31]; lib_int)
    (fun x y => by unfold A0r B0r; dsimp only; rw [c31]; lib_int)
    (fun k a b => by unfold A0r; dsimp only; lib_int)
    (fun k a b => by unfold A0r; dsimp only; lib_int)
    f0_s
    (fun x y h => by
      have h1 : IDv ((31 : Nat) : Int) ((2 * x + y) * (2 * x + y)) :=
        idv_eq (idv_lin h (idv_self ((31 : Nat) : Int)) 4 (-(y * y))) (by rw [c31]; unfold f0; lib_int)
      have h2 := (euclid_int P31.2 h1).elim id id
      show (dB 31 (x + 16 * y) && dB 31 (-(2 * x) - y)) = true
      rw [LibPair31A.dB_t (by decide) (idv_eq (idv_lin h2 (idv_self ((31 : Nat) : Int)) 16 (-x)) (by rw [c31]; lib_int)),
        LibPair31A.dB_t (by decide) (idv_eq (idv_mul_l (-1) h2) (by lib_int))]; rfl)

theorem ram31_1 (n : Nat) : RF f1 (31 * n) = RF f1 n :=
  ram_gen f1 f1 A1r B1r (p := 31) (by decide) n f1_bound f1_bound
    (fun x y => by unfold A1r; dsimp only; rw [c31]; unfold f1; lib_int)
    (fun x y => by unfold B1r; dsimp only; rw [c31]; unfold f1; lib_int)
    (fun x y => by unfold A1r B1r; dsimp only; rw [c31]; lib_int)
    (fun x y => by unfold A1r B1r; dsimp only; rw [c31]; lib_int)
    (fun x y => by unfold A1r B1r; dsimp only; rw [c31]; lib_int)
    (fun x y => by unfold A1r B1r; dsimp only; rw [c31]; lib_int)
    (fun k a b => by unfold A1r; dsimp only; lib_int)
    (fun k a b => by unfold A1r; dsimp only; lib_int)
    f1_s
    (fun x y h => by
      have h1 : IDv ((31 : Nat) : Int) ((4 * x + y) * (4 * x + y)) :=
        idv_eq (idv_lin h (idv_self ((31 : Nat) : Int)) 8 (-(y * y))) (by rw [c31]; unfold f1; lib_int)
      have h2 := (euclid_int P31.2 h1).elim id id
      show (dB 31 (x + 8 * y) && dB 31 (-(4 * x) - y)) = true
      rw [LibPair31A.dB_t (by decide) (idv_eq (idv_lin h2 (idv_self ((31 : Nat) : Int)) 8 (-x)) (by rw [c31]; lib_int)),
        LibPair31A.dB_t (by decide) (idv_eq (idv_mul_l (-1) h2) (by lib_int))]; rfl)

/-! ### Split and inert primes -/

set_option maxRecDepth 100000 in
theorem legB_f1 {x y : Int} {p : Nat} (h : f1 x y = (p : Int)) (hp0 : Nat.beq (md 31 p) 0 = false) :
    QRChain.legB 31 p = true := by
  have h8 : Nat.beq (md 31 8) 0 = false := rfl
  have hm := LibLegMul.legB_mul 14 P31' (LibThetaQ.mk_ne_of 14 h8) (LibThetaQ.mk_ne_of 14 hp0)
  have hsq : IDv ((31 : Nat) : Int) (((8 * p : Nat) : Int) - (((4 * x + y).natAbs * (4 * x + y).natAbs : Nat) : Int)) := by
    refine ⟨y * y, ?_⟩
    rw [← sq_natAbs]
    show 8 * (p : Int) - (4 * x + y) * (4 * x + y) = _
    rw [← h]; unfold f1; rw [c31]; lib_int
  have e := md_eq_of_idv (by decide) hsq
  have hl8p : QRChain.legB 31 (8 * p) = true := by
    rw [LibThetaQ.legB_res 30, e, ← LibThetaQ.legB_res 30]; exact LibThetaQ.legB_sq _ _
  have hl8 : QRChain.legB 31 8 = true := by decide
  have hm' : QRChain.legB 31 (8 * p) = (QRChain.legB 31 8 == QRChain.legB 31 p) := hm
  rw [hm', hl8] at hl8p
  cases hb : QRChain.legB 31 p
  · rw [hb] at hl8p; exact Bool.noConfusion hl8p
  · rfl

theorem f0_nf (x y : Int) : f0 x y = Nf (7 + 1) x y := by unfold f0 Nf; rfl

theorem chi_of_rep {p : Nat} (_hP : LibPrimePow.Prime p) (h31 : Nat.beq (md 31 p) 0 = false)
    (h : ∃ u v : Int, f0 u v = (p : Int) ∨ f1 u v = (p : Int)) : chi31 p = 1 := by
  obtain ⟨u, v, h | h⟩ := h
  · have hl := LibThetaQ.norm_legB 14 P31' 7 rfl (by rw [← f0_nf]; exact h) h31
    unfold chi31 LibThetaQ.chiQ; rw [h31, hl]; rfl
  · have hl := legB_f1 h h31
    unfold chi31 LibThetaQ.chiQ; rw [h31, hl]; rfl

theorem no_root31 {p : Nat} (hP : LibPrimePow.Prime p) (hc : chi31 p = -1) :
    ∀ t : Nat, ¬ IDv (p : Int) ((t : Int) * t + t + 8) := fun t ht => by
  rcases LibThetaQ.chiQ_cases 14 p with ⟨h1, _, _⟩ | ⟨_, hne, _⟩ | ⟨h0, _⟩
  · have : chi31 p = 1 := h1
    rw [this] at hc; exact absurd hc (by decide)
  · have := chi_of_rep hP hne (rep_of_root31 hP t ht)
    rw [this] at hc; exact absurd hc (by decide)
  · have : chi31 p = 0 := h0
    rw [this] at hc; exact absurd hc (by decide)

theorem inert31_f0 {p : Nat} (hP : LibPrimePow.Prime p) (hc : chi31 p = -1) :
    ∀ x y : Int, IDv (p : Int) (f0 x y) → IDv (p : Int) x ∧ IDv (p : Int) y := by
  intro x y hN
  have hp0 : 0 < p := Nat.lt_trans Nat.zero_lt_one hP.1
  cases (show IDv (p : Int) y ∨ ¬ IDv (p : Int) y from by
    cases e : dB p y
    · exact Or.inr (fun h => by rw [dB_of hp0 h] at e; exact Bool.noConfusion e)
    · exact Or.inl (dB_true e)) with
  | inl hy =>
    have hx2 : IDv (p : Int) (x * x) := idv_eq (idv_lin hN hy 1 (-(x + 8 * y))) (by unfold f0; lib_int)
    exact ⟨(euclid_int hP.2 hx2).elim id id, hy⟩
  | inr hy =>
    exfalso
    obtain ⟨w, hw⟩ := inv_mod hP.2 hy
    have ht : IDv (p : Int) ((w * x) * (w * x) + w * x + 8) :=
      idv_eq (idv_lin (idv_mul_l (w * w) hN) hw 1 (-(w * x + 8 * (1 + w * y)))) (by unfold f0; lib_int)
    obtain ⟨tn, htn⟩ := res_nat hp0 (w * x)
    have ht' : IDv (p : Int) ((tn : Int) * tn + tn + 8) :=
      idv_eq (idv_lin ht htn 1 ((tn : Int) + w * x + 1)) (by lib_int)
    exact no_root31 hP hc tn ht'

set_option maxRecDepth 100000 in
theorem odd_of_chi {p : Nat} (hP : LibPrimePow.Prime p) (hc : chi31 p = -1) : ¬ IDv (p : Int) 2 := fun h => by
  cases LibSplit23.P2.2 p (idv_natAbs h) with
  | inl e => rw [e] at hP; exact Nat.lt_irrefl 1 hP.1
  | inr e => rw [e] at hc; exact absurd hc (by decide)

theorem inert31_f1 {p : Nat} (hP : LibPrimePow.Prime p) (hc : chi31 p = -1) :
    ∀ x y : Int, IDv (p : Int) (f1 x y) → IDv (p : Int) x ∧ IDv (p : Int) y := by
  intro x y hN
  obtain ⟨h2x, hy⟩ := inert31_f0 hP hc (2 * x) y (idv_eq (idv_mul_l 2 hN) (by unfold f0 f1; lib_int))
  exact ⟨(euclid_int hP.2 h2x).resolve_left (odd_of_chi hP hc), hy⟩

/-! ### Local data -/

theorem ne31 {p : Nat} (hP : LibPrimePow.Prime p) (hne : Nat.beq (md 31 p) 0 = false) : ¬ IDv (p : Int) 31 := fun h => by
  cases P31.2 p (idv_natAbs h) with
  | inl e => rw [e] at hP; exact Nat.lt_irrefl 1 hP.1
  | inr e => rw [e] at hne; exact Bool.noConfusion hne

theorem ne6 {p : Nat} (hP : LibPrimePow.Prime p) (h2 : p ≠ 2) (h3 : p ≠ 3) : ¬ IDv (p : Int) 6 := fun h => by
  cases euclid_int hP.2 (show IDv (p : Int) (2 * 3) from h) with
  | inl h' =>
    cases LibSplit23.P2.2 p (idv_natAbs h') with
    | inl e => rw [e] at hP; exact Nat.lt_irrefl 1 hP.1
    | inr e => exact h2 e
  | inr h' =>
    cases LibSplit23.P3.2 p (idv_natAbs h') with
    | inl e => rw [e] at hP; exact Nat.lt_irrefl 1 hP.1
    | inr e => exact h3 e

/-- **Local data at every prime** for (x² + xy + 6y², 2x² + xy + 3y²). -/
theorem data31 (p : Nat) (hP : LibPrimePow.Prime p) : LocalData3 f0 f1 chi31 p := by
  rcases LibThetaQ.chiQ_cases 14 p with ⟨h1, hne, hl⟩ | ⟨h1, _, _⟩ | ⟨h0, hd⟩
  · have hc : chi31 p = 1 := h1
    cases Nat.decEq p 2 with
    | isTrue e2 => subst e2; exact LocalData3.splitC ⟨pair01_2⟩ ⟨trip_2⟩ hc
    | isFalse n2 =>
      have hodd : ¬ p ∣ 2 := fun hd2 => by
        cases LibSplit23.P2.2 p hd2 with
        | inl e => rw [e] at hP; exact Nat.lt_irrefl 1 hP.1
        | inr e => exact n2 e
      have h2 : ¬ IDv (p : Int) 2 := fun h => hodd (idv_natAbs h)
      obtain ⟨m, hm⟩ := LibTheta11.odd_prime_k hP hodd
      have hp1 : LibZp.Prime1 (LibEulerCrit.k m) := LibPrimeB.prime1_of_prime (hm ▸ hP)
      have hPr : FTAChain.Pr ((2 * 7 + 1) + (2 * 7 + 1) + 1) := LibPrimeB.Pr_of_prime P31
      have hpq : LibEulerCrit.k m + 1 ≠ (2 * 7 + 1) + (2 * 7 + 1) + 1 := fun e => by
        rw [← hm] at e; rw [e] at hne; exact Bool.noConfusion hne
      obtain ⟨t, ht⟩ := LibQRRoot.root_q m 7 hp1 hPr hpq (by rw [← hm]; exact hl)
      rw [← hm] at ht
      have h31 := ne31 hP hne
      obtain ⟨u, v, h | h⟩ := rep_of_root31 hP t ht
      · exact LocalData3.splitP ⟨pair00 h hP.1 hP.2 h31⟩ ⟨pair11 h hP.1 hP.2 h31⟩ hc
      · exact LocalData3.splitC ⟨pair01 h hP.1 hP.2 h2 h31⟩ ⟨trip h hP.1 hP.2 h2 h31⟩ hc
  · exact LocalData3.inert (inert31_f0 hP h1) (inert31_f1 hP h1) h1
  · have e : p = 31 := by
      cases hP.2 31 hd with
      | inl e => exact absurd e (by decide)
      | inr e => exact e.symm
    subst e
    exact LocalData3.ram (fun n => ⟨ram31_0 n, ram31_1 n⟩) h0

theorem RF0_one : RF f0 1 = 2 := by decide
theorem RF1_one : RF f1 1 = 0 := by decide

/-- **Theta series of discriminant −31.** n > 0 ⇒ r₀(n) + 2r₁(n) = 2Σ_{d∣n} (d/31). -/
theorem theta31 {n : Nat} (hn : 0 < n) :
    R f0 f1 n = 2 * LibEisGen.e LibInt.intR chi31 (LibDivFun.one1 LibInt.intR) n :=
  theta_sum3 f0_bound f1_bound f0_s f1_s chi31_cmult RF0_one RF1_one data31 hn

/-- **The weight-one cusp form of level 31 is a Hecke eigenform.** D = r₀ − r₁: 2·(T_p D)(n) = D(p)·D(n). -/
theorem cusp31 {p : Nat} (hP : LibPrimePow.Prime p) (n : Nat) :
    2 * LibHeckeOp.hk LibInt.intR p (chi31 p) (D f0 f1) n = D f0 f1 p * D f0 f1 n :=
  cusp_eig f0_bound f1_bound f0_s f1_s RF0_one RF1_one data31 hP n

end LibTheta31

#print axioms LibTheta31.ram31_0
#print axioms LibTheta31.ram31_1
#print axioms LibTheta31.legB_f1
#print axioms LibTheta31.inert31_f1
#print axioms LibTheta31.data31
#print axioms LibTheta31.theta31
#print axioms LibTheta31.cusp31

lib/Theta35.lean

lean_all/lib/Theta35.lean

/-
  lib/Theta35.lean — T46 shared library (G1563c): the genus theta series of discriminant −35 (class number 2),
  the q = 3 instance of the PairSym family f₁ = x² + xy + q²y², f₂ = qx² + xy + qy²:

      r₁(n) + r₂(n) = 2 Σ_{d ∣ n} χ₋₃₅(d),   r₁(n) − r₂(n) = 2 Σ_{d ∣ n} χ₋₇(d) χ₅(n/d),
      r₁ = #{x² + xy + 9y² = n},  r₂ = #{3x² + xy + 3y² = n},  n > 0.

    data35         the LocalData at every prime (split: PairSymA / PairSymB, split prime 3: PairSymQ,
                   inert: Root35, ramified 5 = 2q − 1 and 7 = 2q + 1: RamSym)
    theta35        r₁ + r₂ = 2E(χ₋₃₅, 1)
    theta35D       r₁ − r₂ = 2E(χ₋₇, χ₅)
    r1_formula35   2r₁ = 2E(χ₋₃₅, 1) + 2E(χ₋₇, χ₅)
  Lean 4 core only, proofs by hand, no axioms.
-/
import lib.ThetaGenus2
import lib.Root35
import lib.PairSymQ
import lib.Theta15

namespace LibTheta35
open LibMod LibGcd LibIntBox LibNormPiA LibNormPiC LibChi11 LibFormCount LibFormRel LibPairSymA LibPairSymB LibPairSymQ
  LibRamSym LibSplit35 LibComp35 LibRoot35 LibThetaGenus2

/-- residue table mod 35: inert residues have ψ₁ + ψ₂ = 0, and χ₋₃₅ ∈ {1, −1} or the residue is divisible by 5 or 7 -/
def chkG : Bool := allB 35 (fun r =>
  (!(ieq (F35 r) (-1)) || ieq (LibSplit7.F7 (md 7 r) + LibTheta20D.F5 (md 5 r)) 0) &&
  (ieq (F35 r) 1 || ieq (F35 r) (-1) || (ieq (F35 r) 0 && (Nat.beq (md 5 r) 0 || Nat.beq (md 7 r) 0))))

set_option maxRecDepth 100000 in
theorem chkG_true : chkG = true := by decide

theorem vals (p : Nat) : LibSplit7.chi7 p = LibSplit7.F7 (md 7 (md 35 p)) ∧ LibTheta20D.chi5 p = LibTheta20D.F5 (md 5 (md 35 p)) := by
  unfold LibSplit7.chi7 LibTheta20D.chi5
  rw [md7_of_md35, md5_of_md35]; exact ⟨rfl, rfl⟩

theorem dec3 (p : Nat) : 3 ∣ p ∨ ¬ 3 ∣ p := by
  cases e : Nat.beq (md 3 p) 0
  · exact Or.inr (fun hd => by rw [md_zero_of_dvd (by decide) hd] at e; exact Bool.noConfusion e)
  · exact Or.inl (dvd_of_md_zero (Nat.eq_of_beq_eq_true e))

theorem nidv {p q : Nat} (hP : LibPrimePow.Prime p) (hQ : LibPrimePow.Prime q) (hq : ¬ q ∣ p) :
    ¬ IDv (p : Int) ((q : Nat) : Int) := fun h => LibTheta15.ndvd_small hP hQ hq (idv_natAbs h)

theorem f1q_bound2 : ∀ (m : Nat) (x y : Int), f1q (Qc 2) x y = (m : Int) → x.natAbs ≤ m ∧ y.natAbs ≤ m := f1q_bound 2
theorem f2q_bound2 : ∀ (m : Nat) (x y : Int), f2q (Qc 2) x y = (m : Int) → x.natAbs ≤ m ∧ y.natAbs ≤ m := f2q_bound 2
theorem f1q_s2 : ∀ k a b : Int, f1q (Qc 2) (k * a) (k * b) = k * k * f1q (Qc 2) a b := f1q_s (Qc 2)
theorem f2q_s2 : ∀ k a b : Int, f2q (Qc 2) (k * a) (k * b) = k * k * f2q (Qc 2) a b := f2q_s (Qc 2)

/-- **Local data at every prime** for (x² + xy + 9y², 3x² + xy + 3y²). -/
theorem data35 (p : Nat) (hP : LibPrimePow.Prime p) :
    LocalData (f1q (Qc 2)) (f2q (Qc 2)) chi35 LibSplit7.chi7 LibTheta20D.chi5 p := by
  obtain ⟨tA, tB⟩ := LibTwoSqPiQ.and_split (allB_spec _ _ chkG_true (md 35 p) (md_lt (by decide) p))
  obtain ⟨v7, v5⟩ := vals p
  rcases LibTheta11.or3 tB with h1 | hm1 | h0
  · have hc : chi35 p = 1 := eq_of_ieq h1
    have h57 := prime_to_35 (n := p) (by rw [hc]; decide)
    cases dec3 p with
    | inl hd =>
      have e := LibTheta15.prime_eq hP P3 hd
      subst e
      exact LocalData.split12 ⟨pairQ12 (qN := 2) P3⟩ ⟨pairQ21 (qN := 2) P3⟩ hc (by decide) (by decide)
    | inr h3 =>
      have hq : ¬ IDv (p : Int) (Qc 2) := nidv hP P3 h3
      have hm : ¬ IDv (p : Int) (2 * Qc 2 - 1) := nidv hP P5 h57.1
      have hpl : ¬ IDv (p : Int) (2 * Qc 2 + 1) := nidv hP P7 h57.2
      obtain ⟨u, v, h | h⟩ := split35 hP hc
      · obtain ⟨e7, e5⟩ := gen1 h h57.1 h57.2
        exact LocalData.split11 ⟨pairA11 h hP.1 hP.2 hm hpl⟩ ⟨pairA22 h hP.1 hP.2 hq hm hpl⟩ hc
          (by rw [e7, e5]; decide) (by rw [e7, e5]; decide)
      · obtain ⟨e7, e5⟩ := gen2 h h57.1 h57.2
        exact LocalData.split12 ⟨pairB12 h hP.1 hP.2 hq hm hpl⟩ ⟨pairB21 h hP.1 hP.2 hq hm hpl⟩ hc
          (by rw [e7, e5]; decide) (by rw [e7, e5]; decide)
  · have hc : chi35 p = -1 := eq_of_ieq hm1
    have hs := eq_of_ieq (LibTheta40.imp tA hm1)
    have hprod : LibSplit7.chi7 p * LibTheta20D.chi5 p = -1 := by rw [← chi35_eq]; exact hc
    exact LocalData.inert (inert35_f1 hP hc) (inert35_f2 hP hc) hc hprod (by rw [v7, v5]; exact hs)
  · obtain ⟨_, hd⟩ := LibTwoSqPiQ.and_split h0
    rcases LibGenus20.or2' hd with e | e
    · have e5 := LibTheta15.prime_eq hP P5 (dvd_of_md_zero (by rw [md5_of_md35]; exact Nat.eq_of_beq_eq_true e))
      subst e5
      exact LocalData.ramSwap (fun n => ram_m (qN := 2) (L := 5) rfl P5 n) (by decide) (by decide) (by decide)
    · have e7 := LibTheta15.prime_eq hP P7 (dvd_of_md_zero (by rw [md7_of_md35]; exact Nat.eq_of_beq_eq_true e))
      subst e7
      exact LocalData.ramSwap (fun n => ram_p (qN := 2) (L := 7) rfl P7 n) (by decide) (by decide) (by decide)

theorem RF1_one : RF (f1q (Qc 2)) 1 = 2 := by decide
theorem RF2_one : RF (f2q (Qc 2)) 1 = 0 := by decide

/-- **Genus theta series of discriminant −35.** n > 0 ⇒ #{x² + xy + 9y² = n} + #{3x² + xy + 3y² = n} = 2Σ_{d∣n} χ₋₃₅(d). -/
theorem theta35 {n : Nat} (hn : 0 < n) :
    LibThetaGenus2.R (f1q (Qc 2)) (f2q (Qc 2)) n = 2 * LibEisGen.e LibInt.intR chi35 (LibDivFun.one1 LibInt.intR) n :=
  theta_sum f1q_bound2 f2q_bound2 f1q_s2 f2q_s2 chi35_cmult RF1_one RF2_one data35 hn

/-- **The genus difference.** n > 0 ⇒ #{x² + xy + 9y² = n} − #{3x² + xy + 3y² = n} = 2Σ_{d∣n} χ₋₇(d)χ₅(n/d). -/
theorem theta35D {n : Nat} (hn : 0 < n) :
    LibThetaGenus2.D (f1q (Qc 2)) (f2q (Qc 2)) n = 2 * LibEisGen.e LibInt.intR LibSplit7.chi7 LibTheta20D.chi5 n :=
  theta_diff f1q_bound2 f2q_bound2 f1q_s2 f2q_s2 LibSplit7.chi7_cmult LibTheta20D.chi5_cmult RF1_one RF2_one data35 hn

/-- **x² + xy + 9y².** n > 0 ⇒ 2#{x² + xy + 9y² = n} = 2Σχ₋₃₅(d) + 2Σχ₋₇(d)χ₅(n/d). -/
theorem r1_formula35 {n : Nat} (hn : 0 < n) :
    2 * (RF (f1q (Qc 2)) n : Int) = 2 * LibEisGen.e LibInt.intR chi35 (LibDivFun.one1 LibInt.intR) n
      + 2 * LibEisGen.e LibInt.intR LibSplit7.chi7 LibTheta20D.chi5 n :=
  r1_formula f1q_bound2 f2q_bound2 f1q_s2 f2q_s2 chi35_cmult LibSplit7.chi7_cmult LibTheta20D.chi5_cmult RF1_one RF2_one data35 hn

end LibTheta35

#print axioms LibTheta35.chkG_true
#print axioms LibTheta35.data35
#print axioms LibTheta35.theta35
#print axioms LibTheta35.theta35D
#print axioms LibTheta35.r1_formula35

lib/Theta4.lean

lean_all/lib/Theta4.lean

/-
  lib/Theta4.lean — T46 shared library (G961): closed form of the fourth moment Σ k⁴cᵏq^{k²} of θ(c) (truncated version).

    theta4   (under the hypotheses of ThetaLogDeriv) Σ_{k≤K₀} k⁴cᵏq^{k²} + Σ_{k<K₀} (k+1)⁴c^{k+1}q^{(k+1)²} = θ·(L·L + δL)
             θ = (q²;q²)_{2N}Π(1 + cq^{2n+1})², L = Σ δ(1 − q^{2i+2})v_i + Σ (2n+1)c q^{2n+1}(u_n + u_n)   (δθ = θL)
  (Variance side of the heat kernel.) Lean 4 core only, proofs by hand, no axioms.
-/
import lib.ThetaLogDeriv
import lib.TPDeriv2

namespace LibTheta4
open DetChain LibRing LibSum LibSemi LibQBinom LibJTPFinite LibTPoly LibTPChar LibQPoch LibJTPThetaTP LibTPDeriv LibTPLogDeriv
  LibThetaLogDeriv LibTPDeriv2 LibQVander

variable {R : Type} (K : CRing R) (r0 : Nat)

theorem theta4 (K0 : Nat) (hK : r0 + r0 + 1 < (K0 + 1) * (K0 + 1)) (c0 : R) (hc0 : K.mul c0 c0 = K.one)
    (v u : Nat → TP R (r0 + r0 + 1))
    (hv : ∀ i, (tpR K (r0 + r0 + 1)).mul (sub (tpR K (r0 + r0 + 1)) (tpR K (r0 + r0 + 1)).one (pw (tpR K (r0 + r0 + 1)) ((tpR K (r0 + r0 + 1)).mul (X K (r0 + r0 + 1)) (X K (r0 + r0 + 1))) (i + 1))) (v i) = (tpR K (r0 + r0 + 1)).one)
    (hu : ∀ n, (tpR K (r0 + r0 + 1)).mul ((tpR K (r0 + r0 + 1)).add (tpR K (r0 + r0 + 1)).one ((tpR K (r0 + r0 + 1)).mul (pw (tpR K (r0 + r0 + 1)) (X K (r0 + r0 + 1)) (n + n + 1)) (iota K (r0 + r0 + 1) c0))) (u n) = (tpR K (r0 + r0 + 1)).one) :
    let T := tpR K (r0 + r0 + 1)
    let Θ := T.mul (qp T (T.mul (X K (r0 + r0 + 1)) (X K (r0 + r0 + 1))) ((K0 + r0) + (K0 + r0)))
      (prodF T (fun n => T.mul (T.add T.one (T.mul (pw T (X K (r0 + r0 + 1)) (n + n + 1)) (iota K (r0 + r0 + 1) c0))) (T.add T.one (T.mul (pw T (X K (r0 + r0 + 1)) (n + n + 1)) (iota K (r0 + r0 + 1) c0)))) (K0 + r0))
    let L := T.add (sum (toSemi T) (fun i => T.mul (dlt K (r0 + r0 + 1) (sub T T.one (pw T (T.mul (X K (r0 + r0 + 1)) (X K (r0 + r0 + 1))) (i + 1)))) (v i)) (rng ((K0 + r0) + (K0 + r0))))
      (sum (toSemi T) (fun n => T.mul (T.mul (iota K (r0 + r0 + 1) (K.mul (castN K (n + n + 1)) c0)) (pw T (X K (r0 + r0 + 1)) (n + n + 1))) (T.add (u n) (u n))) (rng (K0 + r0)))
    T.add (sum (toSemi T) (fun k => T.mul (iota K (r0 + r0 + 1) (K.mul (castN K (k * k)) (K.mul (castN K (k * k)) (pw K c0 k)))) (pw T (X K (r0 + r0 + 1)) (k * k))) (rng (K0 + 1)))
        (sum (toSemi T) (fun k => T.mul (iota K (r0 + r0 + 1) (K.mul (castN K ((k + 1) * (k + 1))) (K.mul (castN K ((k + 1) * (k + 1))) (pw K c0 (k + 1))))) (pw T (X K (r0 + r0 + 1)) ((k + 1) * (k + 1)))) (rng K0))
      = T.mul Θ (T.add (T.mul L L) (dlt K (r0 + r0 + 1) L)) := by
  intro T Θ L
  have hc : T.mul (iota K (r0 + r0 + 1) c0) (iota K (r0 + r0 + 1) c0) = T.one := by rw [← iota_mul, hc0]; rfl
  have hθ := theta_c K r0 K0 hK (iota K (r0 + r0 + 1) c0) hc
  have hlog := theta_logderiv K r0 K0 hK c0 hc0 v u hv hu
  -- δΘ = left sum = ΘL
  have hd1 : dlt K (r0 + r0 + 1) Θ = T.mul Θ L := by
    show dlt K (r0 + r0 + 1) (T.mul _ _) = _
    rw [hθ, dlt_add, dlt_sum, dlt_sum, sum_ext (toSemi T) _ _ (fun k => dlt_cXpow K r0 c0 k (k * k)),
      sum_ext (toSemi T) _ _ (fun k => dlt_cXpow K r0 c0 (k + 1) ((k + 1) * (k + 1)))]
    exact hlog
  have h2 := dlt2_of_log K (r0 + r0 + 1) Θ L hd1
  rw [← h2, hd1, ← hlog, dlt_add, dlt_sum, dlt_sum]
  have t : ∀ (m : Nat) (a : R), dlt K (r0 + r0 + 1) (T.mul (iota K (r0 + r0 + 1) a) (pw T (X K (r0 + r0 + 1)) m))
      = T.mul (iota K (r0 + r0 + 1) (K.mul (castN K m) a)) (pw T (X K (r0 + r0 + 1)) m) := by
    intro m a; rw [dlt_const_mul, dlt_Xpow, iota_mul]; lib_ring
  rw [sum_ext (toSemi T) _ _ (fun k => t (k * k) _), sum_ext (toSemi T) _ _ (fun k => t ((k + 1) * (k + 1)) _)]

end LibTheta4

#print axioms LibTheta4.theta4

lib/Theta40.lean

lean_all/lib/Theta40.lean

/-
  lib/Theta40.lean — T46 shared library (G1559e): the genus theta series of discriminant −40 (class number 2),
  the first instance of the general framework ThetaGenus2 with the generic packages PairDiag/RamDiag (k = 5):

      r₁(n) + r₂(n) = 2 Σ_{d ∣ n} χ₋₄₀(d),   r₁(n) − r₂(n) = 2 Σ_{d ∣ n} χ₋₈(d) χ₅(n/d),
      r₁ = #{x² + 10y² = n},  r₂ = #{2x² + 5y² = n},  n > 0.

    data40         the LocalData at every prime (split: Pair packages, inert: Root40, ramified 2 and 5: RamDiag)
    theta40        r₁ + r₂ = 2E(χ₋₄₀, 1)
    theta40D       r₁ − r₂ = 2E(χ₋₈, χ₅)
    r1_formula40   2r₁ = 2E(χ₋₄₀, 1) + 2E(χ₋₈, χ₅)
  Lean 4 core only, proofs by hand, no axioms.
-/
import lib.ThetaGenus2
import lib.RamDiag
import lib.Root40
import lib.Theta8

namespace LibTheta40
open LibMod LibGcd LibIntBox LibNormPiA LibNormPiC LibChi11 LibFormCount LibFormRel LibPairDiag LibComp40 LibRoot40
  LibThetaGenus2

theorem hb1 : ∀ (m : Nat) x y, gd1 5 x y = (m : Int) → x.natAbs ≤ m ∧ y.natAbs ≤ m := gd1_bound 4
theorem hb2 : ∀ (m : Nat) x y, gd2 5 x y = (m : Int) → x.natAbs ≤ m ∧ y.natAbs ≤ m := gd2_bound 4

/-- genus-character values on the residues mod 40 -/
def chkG : Bool := allB 40 (fun r =>
  (!(inL r 1 9 11 19) || (ieq (LibTheta8.F8 (md 8 r) * LibTheta20D.F5 (md 5 r)) 1 && ieq (LibTheta8.F8 (md 8 r) + LibTheta20D.F5 (md 5 r)) 2)) &&
  (!(inL r 7 13 23 37) || (ieq (LibTheta8.F8 (md 8 r) * LibTheta20D.F5 (md 5 r)) 1 && ieq (LibTheta8.F8 (md 8 r) + LibTheta20D.F5 (md 5 r)) (-2))) &&
  (!(ieq (F40 r) (-1)) || (ieq (LibTheta8.F8 (md 8 r) * LibTheta20D.F5 (md 5 r)) (-1) && ieq (LibTheta8.F8 (md 8 r) + LibTheta20D.F5 (md 5 r)) 0)) &&
  (ieq (F40 r) 1 || ieq (F40 r) (-1) || (ieq (F40 r) 0 && (Nat.beq (md 2 r) 0 || Nat.beq (md 5 r) 0))))

set_option maxRecDepth 100000 in
theorem chkG_true : chkG = true := by decide

theorem vals (p : Nat) : LibTheta8.chi8 p = LibTheta8.F8 (md 8 (md 40 p)) ∧ LibTheta20D.chi5 p = LibTheta20D.F5 (md 5 (md 40 p)) := by
  unfold LibTheta8.chi8 LibTheta20D.chi5
  rw [md8_of_md40, md5_of_md40]; exact ⟨rfl, rfl⟩

theorem and4 {a b c d : Bool} (h : (a && b && c && d) = true) : a = true ∧ b = true ∧ c = true ∧ d = true := by
  cases a <;> cases b <;> cases c <;> cases d <;> first | exact ⟨rfl, rfl, rfl, rfl⟩ | exact Bool.noConfusion h

theorem imp {a b : Bool} (h : (!a || b) = true) (ha : a = true) : b = true := by
  rw [ha] at h; exact h

theorem two_vals {x y : Int} {s : Int} (h : (ieq x 1 && ieq y s) = true) : x = 1 ∧ y = s := by
  obtain ⟨h1, h2⟩ := LibTwoSqPiQ.and_split h
  exact ⟨eq_of_ieq h1, eq_of_ieq h2⟩

theorem two_vals' {x y : Int} {s : Int} (h : (ieq x (-1) && ieq y s) = true) : x = -1 ∧ y = s := by
  obtain ⟨h1, h2⟩ := LibTwoSqPiQ.and_split h
  exact ⟨eq_of_ieq h1, eq_of_ieq h2⟩

theorem P2 : LibPrimePow.Prime 2 := LibPrimeB.prime_of_isPrimeB (by decide)
theorem P5 : LibPrimePow.Prime 5 := LibPrimeB.prime_of_isPrimeB (by decide)

theorem prime_eq {p q : Nat} (hP : LibPrimePow.Prime p) (hQ : LibPrimePow.Prime q) (d : q ∣ p) : p = q := by
  cases hP.2 q d with
  | inl e => exact absurd (e ▸ hQ.1) (Nat.lt_irrefl 1)
  | inr e => exact e.symm

theorem ndvd5 {p : Nat} (hP : LibPrimePow.Prime p) (h5 : ¬ 5 ∣ p) : ¬ IDv (p : Int) 5 := fun h => by
  have hd : p ∣ 5 := idv_natAbs h
  cases P5.2 p hd with
  | inl e => exact absurd (e ▸ hP.1) (Nat.lt_irrefl 1)
  | inr e => exact h5 (by rw [e]; exact ⟨1, rfl⟩)

theorem n2_5 : ¬ 2 ∣ 4 + 1 := fun hd => by
  cases P5.2 2 hd with
  | inl e => exact absurd e (by decide)
  | inr e => exact absurd e (by decide)
theorem n5_2 : ¬ 4 + 1 ∣ 2 := fun hd => by
  cases P2.2 5 hd with
  | inl e => exact absurd e (by decide)
  | inr e => exact absurd e (by decide)

/-- **Local data at every prime** for (x² + 10y², 2x² + 5y²). -/
theorem data40 (p : Nat) (hP : LibPrimePow.Prime p) : LocalData (gd1 5) (gd2 5) chi40 LibTheta8.chi8 LibTheta20D.chi5 p := by
  obtain ⟨tA, tB, tC, tD⟩ := and4 (allB_spec _ _ chkG_true (md 40 p) (md_lt (by decide) p))
  obtain ⟨v8, v5⟩ := vals p
  rcases LibTheta11.or3 tD with h1 | hm1 | h0
  · have hc : chi40 p = 1 := eq_of_ieq h1
    have h25 := prime_to_10 (n := p) (by rw [hc]; decide)
    have h2' := LibRoot20.ndvd2 hP h25.1
    have h5' := ndvd5 hP h25.2
    obtain ⟨u, v, h | h⟩ := split40 hP hc
    · have hv := two_vals (imp tA (gen1 h h25.1 h25.2))
      exact LocalData.split11 ⟨pairA11 hb1 h hP.1 hP.2 h2' h5'⟩ ⟨pairA22 hb2 h hP.1 hP.2 h2' h5'⟩ hc
        (by rw [v8, v5]; exact hv.1) (by rw [v8, v5]; exact hv.2)
    · have hv := two_vals (imp tB (gen2 h h25.1 h25.2))
      exact LocalData.split12 ⟨pairB12 hb1 hb2 h hP.1 hP.2 h2' h5'⟩ ⟨pairB21 hb1 hb2 h hP.1 hP.2 h2' h5'⟩ hc
        (by rw [v8, v5]; exact hv.1) (by rw [v8, v5]; exact hv.2)
  · have hc : chi40 p = -1 := eq_of_ieq hm1
    have hv := two_vals' (imp tC hm1)
    exact LocalData.inert (inert40_g1 hP hc) (inert40_g2 hP hc) hc (by rw [v8, v5]; exact hv.1) (by rw [v8, v5]; exact hv.2)
  · obtain ⟨_, hd⟩ := LibTwoSqPiQ.and_split h0
    rcases LibGenus20.or2' hd with e | e
    · have e2 := prime_eq hP P2 (dvd_of_md_zero (by rw [md2_of_md40]; exact Nat.eq_of_beq_eq_true e))
      subst e2
      exact LocalData.ramSwap (fun n => LibRamDiag.ram2_diag (kN := 4) n2_5 n) (by decide) (by decide) (by decide)
    · have e5 := prime_eq hP P5 (dvd_of_md_zero (by rw [md5_of_md40]; exact Nat.eq_of_beq_eq_true e))
      subst e5
      exact LocalData.ramSwap (fun n => LibRamDiag.ramk_diag (kN := 4) P5 n5_2 n) (by decide) (by decide) (by decide)

theorem RF1_one : RF (gd1 5) 1 = 2 := by decide
theorem RF2_one : RF (gd2 5) 1 = 0 := by decide

/-- **Genus theta series of discriminant −40.** n > 0 ⇒ #{x² + 10y² = n} + #{2x² + 5y² = n} = 2Σ_{d∣n} χ₋₄₀(d). -/
theorem theta40 {n : Nat} (hn : 0 < n) :
    LibThetaGenus2.R (gd1 5) (gd2 5) n = 2 * LibEisGen.e LibInt.intR chi40 (LibDivFun.one1 LibInt.intR) n :=
  theta_sum hb1 hb2 (gd1_s 5) (gd2_s 5) chi40_cmult RF1_one RF2_one data40 hn

/-- **The genus difference.** n > 0 ⇒ #{x² + 10y² = n} − #{2x² + 5y² = n} = 2Σ_{d∣n} χ₋₈(d)χ₅(n/d). -/
theorem theta40D {n : Nat} (hn : 0 < n) :
    LibThetaGenus2.D (gd1 5) (gd2 5) n = 2 * LibEisGen.e LibInt.intR LibTheta8.chi8 LibTheta20D.chi5 n :=
  theta_diff hb1 hb2 (gd1_s 5) (gd2_s 5) LibTheta8.chi8_cmult LibTheta20D.chi5_cmult RF1_one RF2_one data40 hn

/-- **x² + 10y².** n > 0 ⇒ 2#{x² + 10y² = n} = 2Σχ₋₄₀(d) + 2Σχ₋₈(d)χ₅(n/d). -/
theorem r1_formula40 {n : Nat} (hn : 0 < n) :
    2 * (RF (gd1 5) n : Int) = 2 * LibEisGen.e LibInt.intR chi40 (LibDivFun.one1 LibInt.intR) n
      + 2 * LibEisGen.e LibInt.intR LibTheta8.chi8 LibTheta20D.chi5 n :=
  r1_formula hb1 hb2 (gd1_s 5) (gd2_s 5) chi40_cmult LibTheta8.chi8_cmult LibTheta20D.chi5_cmult RF1_one RF2_one data40 hn

end LibTheta40

#print axioms LibTheta40.data40
#print axioms LibTheta40.theta40
#print axioms LibTheta40.theta40D
#print axioms LibTheta40.r1_formula40

lib/Theta59.lean

lean_all/lib/Theta59.lean

/-
  lib/Theta59.lean — T46 shared library (G1588d): discriminant −59, class number 3 (2 inert),
  f₀ = x² + xy + 15y², f₁ = 3x² + xy + 5y², χ = (·/59):

      r₀(n) + 2 r₁(n) = 2 Σ_{d ∣ n} χ(d)   (n > 0),    2·(T_p D)(n) = D(p)·D(n)   (D = r₀ − r₁, every prime p)

  Packages: LibPairQd (Q, d) = (3, 5) and LibPair59; √−59 = −1 + 2ω; 12p = (6x + y)² + 59y² for f₁.
  Lean 4 core only, proofs by hand, no axioms.
-/
import lib.ThetaGenus3
import lib.Split59
import lib.ThetaQ
import lib.Split23

namespace LibTheta59
open LibMod LibGcd LibIntBox LibNormPiA LibNormPiC LibFormCount LibFormRel LibFormRel3 LibFormRam LibChi11
  LibSplit59 LibPair59 LibThetaGenus3

def chi59 (n : Nat) : Int := LibThetaQ.chiQ 59 n

theorem P59' : LibZp.Prime1 (LibEulerCrit.k 28) := LibPrimeB.prime1_of_prime (LibPrimeB.prime_of_isPrimeB (by decide))
theorem P59 : LibPrimePow.Prime 59 := LibPrimeB.prime_of_isPrimeB (by decide)

theorem chi59_cmult : LibDirTwist.CMult LibInt.intR chi59 := LibThetaQ.chiQ_cmult 28 P59'

/-! ### The ramified prime 59 -/

def A0r (x y : Int) : Int × Int := (-x - 30 * y, 2 * x + y)
def B0r (x y : Int) : Int × Int := (x + 30 * y, -(2 * x) - y)
def A1r (x y : Int) : Int × Int := (-x - 10 * y, 6 * x + y)
def B1r (x y : Int) : Int × Int := (x + 10 * y, -(6 * x) - y)

theorem c59 : ((59 : Nat) : Int) = 59 := rfl

set_option maxHeartbeats 2000000 in
theorem ram59_0 (n : Nat) : RF f0 (59 * n) = RF f0 n :=
  ram_gen f0 f0 A0r B0r (p := 59) (by decide) n (LibPairQd.f0_bound 2 4) (LibPairQd.f0_bound 2 4)
    (fun x y => by unfold A0r; dsimp only; rw [c59]; unfold f0; lib_int)
    (fun x y => by unfold B0r; dsimp only; rw [c59]; unfold f0; lib_int)
    (fun x y => by unfold A0r B0r; dsimp only; rw [c59]; lib_int)
    (fun x y => by unfold A0r B0r; dsimp only; rw [c59]; lib_int)
    (fun x y => by unfold A0r B0r; dsimp only; rw [c59]; lib_int)
    (fun x y => by unfold A0r B0r; dsimp only; rw [c59]; lib_int)
    (fun k a b => by unfold A0r; dsimp only; lib_int)
    (fun k a b => by unfold A0r; dsimp only; lib_int)
    (LibPairQd.f0_s 2 4)
    (fun x y h => by
      have h1 : IDv ((59 : Nat) : Int) ((2 * x + y) * (2 * x + y)) :=
        idv_eq (idv_lin h (idv_self ((59 : Nat) : Int)) 4 (-(y * y))) (by rw [c59]; unfold f0; lib_int)
      have h2 := (euclid_int P59.2 h1).elim id id
      show (dB 59 (x + 30 * y) && dB 59 (-(2 * x) - y)) = true
      rw [LibPairQd.dB_t (by decide) (idv_eq (idv_lin h2 (idv_self ((59 : Nat) : Int)) 30 (-x)) (by rw [c59]; lib_int)),
        LibPairQd.dB_t (by decide) (idv_eq (idv_mul_l (-1) h2) (by lib_int))]; rfl)

theorem f1_bound (m : Nat) (x y : Int) (h : f1 x y = (m : Int)) : x.natAbs ≤ m ∧ y.natAbs ≤ m := by
  have i1 : ((2 * m : Nat) : Int) = ((5 * (x.natAbs * x.natAbs) + 9 * (y.natAbs * y.natAbs) + (x + y).natAbs * (x + y).natAbs : Nat) : Int) := by
    show 2 * (m : Int) = 5 * ((x.natAbs * x.natAbs : Nat) : Int) + 9 * ((y.natAbs * y.natAbs : Nat) : Int) + ((x + y).natAbs * (x + y).natAbs : Nat)
    rw [← sq_natAbs, ← sq_natAbs, ← sq_natAbs, ← h]; unfold f1; lib_int
  have n1 := Int.ofNat.inj i1
  refine ⟨LibPairSymA.key2 ?_, LibPairSymA.key2 ?_⟩
  · rw [n1]; exact Nat.le_trans (Nat.le_trans (Nat.le_mul_of_pos_left _ (by decide)) (Nat.le_add_right _ _)) (Nat.le_add_right _ _)
  · rw [n1]; exact Nat.le_trans (Nat.le_trans (Nat.le_mul_of_pos_left _ (by decide)) (Nat.le_add_left _ _)) (Nat.le_add_right _ _)

theorem f1_s (k a b : Int) : f1 (k * a) (k * b) = k * k * f1 a b := by unfold f1; lib_int

set_option maxHeartbeats 2000000 in
theorem ram59_1 (n : Nat) : RF f1 (59 * n) = RF f1 n :=
  ram_gen f1 f1 A1r B1r (p := 59) (by decide) n f1_bound f1_bound
    (fun x y => by unfold A1r; dsimp only; rw [c59]; unfold f1; lib_int)
    (fun x y => by unfold B1r; dsimp only; rw [c59]; unfold f1; lib_int)
    (fun x y => by unfold A1r B1r; dsimp only; rw [c59]; lib_int)
    (fun x y => by unfold A1r B1r; dsimp only; rw [c59]; lib_int)
    (fun x y => by unfold A1r B1r; dsimp only; rw [c59]; lib_int)
    (fun x y => by unfold A1r B1r; dsimp only; rw [c59]; lib_int)
    (fun k a b => by unfold A1r; dsimp only; lib_int)
    (fun k a b => by unfold A1r; dsimp only; lib_int)
    f1_s
    (fun x y h => by
      have h1 : IDv ((59 : Nat) : Int) ((6 * x + y) * (6 * x + y)) :=
        idv_eq (idv_lin h (idv_self ((59 : Nat) : Int)) 12 (-(y * y))) (by rw [c59]; unfold f1; lib_int)
      have h2 := (euclid_int P59.2 h1).elim id id
      show (dB 59 (x + 10 * y) && dB 59 (-(6 * x) - y)) = true
      rw [LibPairQd.dB_t (by decide) (idv_eq (idv_lin h2 (idv_self ((59 : Nat) : Int)) 10 (-x)) (by rw [c59]; lib_int)),
        LibPairQd.dB_t (by decide) (idv_eq (idv_mul_l (-1) h2) (by lib_int))]; rfl)

/-! ### Split and inert primes -/

set_option maxRecDepth 100000 in
theorem legB_f1 {x y : Int} {p : Nat} (h : f1 x y = (p : Int)) (hp0 : Nat.beq (md 59 p) 0 = false) :
    QRChain.legB 59 p = true := by
  have h8 : Nat.beq (md 59 12) 0 = false := rfl
  have hm := LibLegMul.legB_mul 28 P59' (LibThetaQ.mk_ne_of 28 h8) (LibThetaQ.mk_ne_of 28 hp0)
  have hsq : IDv ((59 : Nat) : Int) (((12 * p : Nat) : Int) - (((6 * x + y).natAbs * (6 * x + y).natAbs : Nat) : Int)) := by
    refine ⟨y * y, ?_⟩
    rw [← sq_natAbs]
    show 12 * (p : Int) - (6 * x + y) * (6 * x + y) = _
    rw [← h]; unfold f1; rw [c59]; lib_int
  have e := md_eq_of_idv (by decide) hsq
  have hl8p : QRChain.legB 59 (12 * p) = true := by
    rw [LibThetaQ.legB_res 58, e, ← LibThetaQ.legB_res 58]; exact LibThetaQ.legB_sq _ _
  have hl8 : QRChain.legB 59 12 = true := by decide
  have hm' : QRChain.legB 59 (12 * p) = (QRChain.legB 59 12 == QRChain.legB 59 p) := hm
  rw [hm', hl8] at hl8p
  cases hb : QRChain.legB 59 p
  · rw [hb] at hl8p; exact Bool.noConfusion hl8p
  · rfl

theorem f0_nf (x y : Int) : f0 x y = Nf (14 + 1) x y := by unfold f0 Nf; rfl

theorem chi_of_rep {p : Nat} (_hP : LibPrimePow.Prime p) (h59 : Nat.beq (md 59 p) 0 = false)
    (h : ∃ u v : Int, f0 u v = (p : Int) ∨ f1 u v = (p : Int)) : chi59 p = 1 := by
  obtain ⟨u, v, h | h⟩ := h
  · have hl := LibThetaQ.norm_legB 28 P59' 14 rfl (by rw [← f0_nf]; exact h) h59
    unfold chi59 LibThetaQ.chiQ; rw [h59, hl]; rfl
  · have hl := legB_f1 h h59
    unfold chi59 LibThetaQ.chiQ; rw [h59, hl]; rfl

theorem no_root59 {p : Nat} (hP : LibPrimePow.Prime p) (hc : chi59 p = -1) :
    ∀ t : Nat, ¬ IDv (p : Int) ((t : Int) * t + t + 15) := fun t ht => by
  rcases LibThetaQ.chiQ_cases 28 p with ⟨h1, _, _⟩ | ⟨_, hne, _⟩ | ⟨h0, _⟩
  · have : chi59 p = 1 := h1
    rw [this] at hc; exact absurd hc (by decide)
  · have := chi_of_rep hP hne (rep_of_root59 hP t ht)
    rw [this] at hc; exact absurd hc (by decide)
  · have : chi59 p = 0 := h0
    rw [this] at hc; exact absurd hc (by decide)

theorem inert59_f0 {p : Nat} (hP : LibPrimePow.Prime p) (hc : chi59 p = -1) :
    ∀ x y : Int, IDv (p : Int) (f0 x y) → IDv (p : Int) x ∧ IDv (p : Int) y := by
  intro x y hN
  have hp0 : 0 < p := Nat.lt_trans Nat.zero_lt_one hP.1
  cases (show IDv (p : Int) y ∨ ¬ IDv (p : Int) y from by
    cases e : dB p y
    · exact Or.inr (fun h => by rw [dB_of hp0 h] at e; exact Bool.noConfusion e)
    · exact Or.inl (dB_true e)) with
  | inl hy =>
    have hx2 : IDv (p : Int) (x * x) := idv_eq (idv_lin hN hy 1 (-(x + 15 * y))) (by unfold f0; lib_int)
    exact ⟨(euclid_int hP.2 hx2).elim id id, hy⟩
  | inr hy =>
    exfalso
    obtain ⟨w, hw⟩ := inv_mod hP.2 hy
    have ht : IDv (p : Int) ((w * x) * (w * x) + w * x + 15) :=
      idv_eq (idv_lin (idv_mul_l (w * w) hN) hw 1 (-(w * x + 15 * (1 + w * y)))) (by unfold f0; lib_int)
    obtain ⟨tn, htn⟩ := res_nat hp0 (w * x)
    have ht' : IDv (p : Int) ((tn : Int) * tn + tn + 15) :=
      idv_eq (idv_lin ht htn 1 ((tn : Int) + w * x + 1)) (by lib_int)
    exact no_root59 hP hc tn ht'

set_option maxRecDepth 100000 in
theorem odd_of_chi {p : Nat} (hP : LibPrimePow.Prime p) (hc : chi59 p = -1) : ¬ IDv (p : Int) 3 := fun h => by
  cases LibPair59.P3.2 p (idv_natAbs h) with
  | inl e => rw [e] at hP; exact Nat.lt_irrefl 1 hP.1
  | inr e => rw [e] at hc; exact absurd hc (by decide)

theorem inert59_f1 {p : Nat} (hP : LibPrimePow.Prime p) (hc : chi59 p = -1) :
    ∀ x y : Int, IDv (p : Int) (f1 x y) → IDv (p : Int) x ∧ IDv (p : Int) y := by
  intro x y hN
  obtain ⟨h2x, hy⟩ := inert59_f0 hP hc (3 * x) y (idv_eq (idv_mul_l 3 hN) (by unfold f0 f1; lib_int))
  exact ⟨(euclid_int hP.2 h2x).resolve_left (odd_of_chi hP hc), hy⟩

/-! ### Local data -/

theorem ne59 {p : Nat} (hP : LibPrimePow.Prime p) (hne : Nat.beq (md 59 p) 0 = false) : ¬ IDv (p : Int) 59 := fun h => by
  cases P59.2 p (idv_natAbs h) with
  | inl e => rw [e] at hP; exact Nat.lt_irrefl 1 hP.1
  | inr e => rw [e] at hne; exact Bool.noConfusion hne

theorem ne6 {p : Nat} (hP : LibPrimePow.Prime p) (h2 : p ≠ 2) (h3 : p ≠ 3) : ¬ IDv (p : Int) 6 := fun h => by
  cases euclid_int hP.2 (show IDv (p : Int) (2 * 3) from h) with
  | inl h' =>
    cases LibSplit23.P2.2 p (idv_natAbs h') with
    | inl e => rw [e] at hP; exact Nat.lt_irrefl 1 hP.1
    | inr e => exact h2 e
  | inr h' =>
    cases LibSplit23.P3.2 p (idv_natAbs h') with
    | inl e => rw [e] at hP; exact Nat.lt_irrefl 1 hP.1
    | inr e => exact h3 e

set_option maxHeartbeats 2000000 in
set_option maxRecDepth 100000 in
/-- **Local data at every prime** for (x² + xy + 15y², 3x² + xy + 5y²). -/
theorem data59 (p : Nat) (hP : LibPrimePow.Prime p) : LocalData3 f0 f1 chi59 p := by
  rcases LibThetaQ.chiQ_cases 28 p with ⟨h1, hne, hl⟩ | ⟨h1, _, _⟩ | ⟨h0, hd⟩
  · have hc : chi59 p = 1 := h1
    cases Nat.decEq p 3 with
    | isTrue e3 => subst e3; exact LocalData3.splitC ⟨pair01_3⟩ ⟨trip_3⟩ hc
    | isFalse n3 =>
      cases Nat.decEq p 5 with
      | isTrue e5 => subst e5; exact LocalData3.splitC ⟨pair01_5⟩ ⟨trip_5⟩ hc
      | isFalse n5 =>
        have hodd : ¬ p ∣ 2 := fun hd2 => by
          cases LibSplit23.P2.2 p hd2 with
          | inl e => rw [e] at hP; exact Nat.lt_irrefl 1 hP.1
          | inr e => rw [e] at hc; exact absurd hc (by decide)
        have h3 : ¬ IDv (p : Int) (LibPairQd.Qi 2) := fun h => by
          cases LibPair59.P3.2 p (idv_natAbs (show IDv (p : Int) ((3 : Nat) : Int) from h)) with
          | inl e => rw [e] at hP; exact Nat.lt_irrefl 1 hP.1
          | inr e => exact n3 e
        have h5 : ¬ IDv (p : Int) (LibPairQd.Di 4) := fun h => by
          cases LibPair59.P5.2 p (idv_natAbs (show IDv (p : Int) ((5 : Nat) : Int) from h)) with
          | inl e => rw [e] at hP; exact Nat.lt_irrefl 1 hP.1
          | inr e => exact n5 e
        have h15 : ¬ IDv (p : Int) (LibPairQd.Qi 2 * LibPairQd.Di 4) := fun h => by
          cases euclid_int hP.2 h with
          | inl h' => exact h3 h'
          | inr h' => exact h5 h'
        have h59 : ¬ IDv (p : Int) (4 * (LibPairQd.Qi 2 * LibPairQd.Di 4) - 1) := fun h => ne59 hP hne (show IDv (p : Int) 59 from h)
        obtain ⟨m, hm⟩ := LibTheta11.odd_prime_k hP hodd
        have hp1 : LibZp.Prime1 (LibEulerCrit.k m) := LibPrimeB.prime1_of_prime (hm ▸ hP)
        have hPr : FTAChain.Pr ((2 * 14 + 1) + (2 * 14 + 1) + 1) := LibPrimeB.Pr_of_prime P59
        have hpq : LibEulerCrit.k m + 1 ≠ (2 * 14 + 1) + (2 * 14 + 1) + 1 := fun e => by
          rw [← hm] at e; rw [e] at hne; exact Bool.noConfusion hne
        obtain ⟨t, ht⟩ := LibQRRoot.root_q m 14 hp1 hPr hpq (by rw [← hm]; exact hl)
        rw [← hm] at ht
        obtain ⟨u, v, h | h⟩ := rep_of_root59 hP t ht
        · exact LocalData3.splitP ⟨LibPairQd.pair00 2 4 (h.trans rfl) hP.1 hP.2 h15 h59⟩ ⟨LibPairQd.pair11 2 4 (h.trans rfl) hP.1 hP.2 h15 h59⟩ hc
        · exact LocalData3.splitC ⟨LibPairQd.pair01 2 4 (h.trans rfl) hP.1 hP.2 h3 h5 h59⟩ ⟨LibPair59.trip (h.trans rfl) hP.1 hP.2 h3 h5 h59⟩ hc
  · exact LocalData3.inert (inert59_f0 hP h1) (inert59_f1 hP h1) h1
  · have e : p = 59 := by
      cases hP.2 59 hd with
      | inl e => exact absurd e (by decide)
      | inr e => exact e.symm
    subst e
    exact LocalData3.ram (fun n => ⟨ram59_0 n, ram59_1 n⟩) h0

theorem RF0_one : RF f0 1 = 2 := by decide
theorem RF1_one : RF f1 1 = 0 := by decide

/-- **Theta series of discriminant −59.** n > 0 ⇒ r₀(n) + 2r₁(n) = 2Σ_{d∣n} (d/59). -/
theorem theta59 {n : Nat} (hn : 0 < n) :
    R f0 f1 n = 2 * LibEisGen.e LibInt.intR chi59 (LibDivFun.one1 LibInt.intR) n :=
  theta_sum3 (LibPairQd.f0_bound 2 4) (LibPairQd.fQ_bound 2 4) (LibPairQd.f0_s 2 4) (LibPairQd.fQ_s 2 4) chi59_cmult RF0_one RF1_one data59 hn

/-- **The weight-one cusp form of level 59 is a Hecke eigenform.** D = r₀ − r₁: 2·(T_p D)(n) = D(p)·D(n). -/
theorem cusp59 {p : Nat} (hP : LibPrimePow.Prime p) (n : Nat) :
    2 * LibHeckeOp.hk LibInt.intR p (chi59 p) (D f0 f1) n = D f0 f1 p * D f0 f1 n :=
  cusp_eig (LibPairQd.f0_bound 2 4) (LibPairQd.fQ_bound 2 4) (LibPairQd.f0_s 2 4) (LibPairQd.fQ_s 2 4) RF0_one RF1_one data59 hP n

end LibTheta59

#print axioms LibTheta59.ram59_0
#print axioms LibTheta59.ram59_1
#print axioms LibTheta59.legB_f1
#print axioms LibTheta59.inert59_f1
#print axioms LibTheta59.data59
#print axioms LibTheta59.theta59
#print axioms LibTheta59.cusp59

lib/Theta7.lean

lean_all/lib/Theta7.lean

/-
  lib/Theta7.lean — T46 shared library (G1522b): the theta series of x² + xy + 2y² is an Eisenstein series.

    leg7_of_chi    χ₋₇(p) = 1 ⇒ p is a square mod 7
    leg_p7         p = 2(m+1)+1 prime ≠ 7, χ₋₇(p) = 1 ⇒ (7/p) = (−1/p)          (quadratic reciprocity)
    root7          … ⇒ ∃ t, p ∣ t² + t + 2
    chi7_cases     χ₋₇(n) ∈ {1, −1, 0}, χ₋₇(n) = 0 ⇒ 7 ∣ n
    prime_data     2 = N(0, 1) and odd χ = 1 primes split, χ = −1 primes inert, 7 = N(1, −2) ramified
    theta7_half    n > 0 ⇒ RH(n) = Σ_{d ∣ n} χ₋₇(d)
    theta7         n > 0 ⇒ #{(x, y) ∈ ℤ² : x² + xy + 2y² = n} = 2 Σ_{d ∣ n} χ₋₇(d)
  Lean 4 core only, proofs by hand, no axioms.
-/
import lib.Split7
import lib.Theta3

namespace LibTheta7
open LibMod LibGcd LibEulerCrit LibZp LibIntBox LibNormPiA LibNormPiB LibNormPiF LibChi11 LibSplit7

theorem par_three (n : Nat) : QRChain.par (3 * n) = QRChain.par n := by
  rw [show 3 * n = (n + n) + n by lib_nat, QRChain.par_add, LibQR5.par_double]
  rfl

def chkLeg : Bool := allB 7 (fun r => !(ieq (F7 r) 1) || QRChain.legB 7 r)

theorem chkLeg_true : chkLeg = true := by decide

theorem leg7_of_chi {p : Nat} (hc : chi7 p = 1) : QRChain.legB 7 p = true := by
  have hr : md 7 p < 7 := md_lt (by decide) p
  have hrem : FTAChain.rem 7 p = FTAChain.rem 7 (md 7 p) := by
    rw [show (7 : Nat) = 6 + 1 from rfl, LibFTAB.rem_eq_md, LibFTAB.rem_eq_md, md_md (Nat.succ_pos 6)]
  have e : QRChain.legB 7 p = QRChain.legB 7 (md 7 p) := LibQR11.sqS_congr 7 p (md 7 p) hrem 7
  rw [e]
  have t := allB_spec 7 _ chkLeg_true (md 7 p) hr
  have hi : ieq (F7 (md 7 p)) 1 = true := by
    have : F7 (md 7 p) = 1 := hc
    rw [this]; exact ieq_self 1
  rw [hi] at t
  exact t

variable (m : Nat)

theorem leg_p7 (hp : Prime1 (k m)) (hne : k m + 1 ≠ 7) (hc : chi7 (k m + 1) = 1) :
    QRChain.legB (k m + 1) 7 = !QRChain.par (m + 1) := by
  have ek : k m + 1 = (m + 1) + (m + 1) + 1 := by show 2 * (m + 1) + 1 = _; lib_nat
  have hP : FTAChain.Pr ((m + 1) + (m + 1) + 1) := by
    rw [← ek]; exact LibLLNum.pr_of_prime1 (k m) (Nat.succ_le_succ (Nat.zero_le _)) hp
  have hQ : FTAChain.Pr (3 + 3 + 1) := LibFTAB.pr_of_isPrimeB (by decide)
  have hne' : m + 1 ≠ 3 := fun e => hne (by rw [ek, e])
  have q := QRChain.qr_law (m + 1) 3 hP hQ hne'
  have h7 : QRChain.legB (3 + 3 + 1) ((m + 1) + (m + 1) + 1) = true := by
    rw [← ek]; exact leg7_of_chi hc
  rw [h7, show (m + 1) * 3 = 3 * (m + 1) by lib_nat, par_three] at q
  have q' : QRChain.bx true (QRChain.legB ((m + 1) + (m + 1) + 1) (3 + 3 + 1)) = QRChain.par (m + 1) := by
    cases hb : QRChain.legB ((m + 1) + (m + 1) + 1) (3 + 3 + 1) <;> rw [hb] at q <;> exact q
  rw [LibQR11.bx_true] at q'
  rw [ek, ← q', LibQR11.not_not']

theorem mk7_ne (hne : k m + 1 ≠ 7) : mk (k m) 7 ≠ (znR (k m)).zero := fun e => by
  have h1 := congrArg Subtype.val e
  have h2 : md (k m + 1) 0 = 0 := md_of_lt (pos (k m)) (pos (k m))
  have h0 : md (k m + 1) 7 = 0 := by rw [← h2]; exact h1
  have hd : k m + 1 ∣ 7 := dvd_of_md_zero h0
  have p3 : LibPrimePow.Prime 7 := LibPrimeB.prime_of_isPrimeB (by decide)
  cases p3.2 _ hd with
  | inl e1 => exact Nat.noConfusion (show 2 * (m + 1) = 0 from Nat.succ.inj e1)
  | inr e1 => exact hne e1

theorem sq_neg7 (hp : Prime1 (k m)) (hne : k m + 1 ≠ 7) (hc : chi7 (k m + 1) = 1) :
    ∃ x : Nat, IDv ((k m + 1 : Nat) : Int) ((x : Int) * x + 7) := by
  have hl := LibLegMul.legB_mul m hp (LibLegNeg.mk_ne m) (mk7_ne m hne)
  rw [LibLegNeg.legB_neg_one m hp, leg_p7 m hp hne hc] at hl
  have ht : QRChain.legB (k m + 1) (k m * 7) = true := by
    rw [hl]; cases QRChain.par (m + 1) <;> rfl
  obtain ⟨x, hx⟩ := (QRChain.legB_iff (k m) (k m * 7)).1 ht
  rw [LibFTAB.rem_eq_md, LibFTAB.rem_eq_md] at hx
  obtain ⟨c, hc'⟩ := LibThue.idv_of_md hx
  refine ⟨x, c + 7, ?_⟩
  have e1 : ((k m * 7 : Nat) : Int) = ((k m : Nat) : Int) * 7 := rfl
  have e2 : ((k m + 1 : Nat) : Int) = ((k m : Nat) : Int) + 1 := rfl
  have e3 : ((x * x : Nat) : Int) = (x : Int) * x := rfl
  rw [e1, e3] at hc'
  rw [e2] at hc' ⊢
  have : (x : Int) * x = ((k m : Nat) : Int) * 7 + (((k m : Nat) : Int) + 1) * c := by
    rw [← hc']; lib_int
  rw [this]; lib_int

theorem root7 (hp : Prime1 (k m)) (hne : k m + 1 ≠ 7) (hc : chi7 (k m + 1) = 1) :
    ∃ t : Nat, IDv ((k m + 1 : Nat) : Int) ((t : Int) * t + t + 2) := by
  obtain ⟨x, c, hx⟩ := sq_neg7 m hp hne hc
  have hodd : ∃ j, k m + 1 = 2 * j + 1 := ⟨m + 1, rfl⟩
  have h1 : k m + 1 ≠ 1 := fun e => Nat.noConfusion (show 2 * (m + 1) = 0 from Nat.succ.inj e)
  have h2 : k m + 1 ≠ 2 := fun e => Nat.noConfusion (show 2 * m + 1 = 0 from Nat.succ.inj (Nat.succ.inj (by
    show 2 * (m + 1) + 1 = 2; exact e)))
  have hN : ∃ T : Int, IDv ((k m + 1 : Nat) : Int) (T * T + T + 2) := by
    obtain ⟨T, hT | hT⟩ := LibQR11.odd_rep (k m + 1) hodd x
    · refine ⟨T, LibQR11.four_dvd hp h1 h2 (d := c) ?_⟩
      have : 4 * (T * T + T + 2) = (2 * T + 1) * (2 * T + 1) + 7 := by lib_int
      rw [this, hT]; exact hx
    · refine ⟨T, LibQR11.four_dvd hp h1 h2 (d := c + 2 * x + ((k m + 1 : Nat) : Int)) ?_⟩
      have : 4 * (T * T + T + 2) = (2 * T + 1) * (2 * T + 1) + 7 := by lib_int
      rw [this, hT]
      have e : (x + ((k m + 1 : Nat) : Int)) * (x + ((k m + 1 : Nat) : Int)) + 7
          = (x * x + 7) + ((k m + 1 : Nat) : Int) * (2 * x + ((k m + 1 : Nat) : Int)) := by lib_int
      rw [e, hx]; lib_int
  obtain ⟨T, a, ha⟩ := hN
  obtain ⟨tn, b, hb⟩ := res_nat (pos (k m)) T
  refine ⟨tn, a + b * ((tn : Int) + T + 1), ?_⟩
  have e : (tn : Int) * tn + tn + 2 = (T * T + T + 2) + ((tn : Int) - T) * ((tn : Int) + T + 1) := by lib_int
  rw [e, ha, hb]; lib_int

def chkCases : Bool := allB 7 (fun r => ieq (F7 r) 1 || ieq (F7 r) (-1) || (ieq (F7 r) 0 && Nat.beq r 0))

theorem chkCases_true : chkCases = true := by decide

theorem chi7_cases (n : Nat) : chi7 n = 1 ∨ chi7 n = -1 ∨ (chi7 n = 0 ∧ 7 ∣ n) := by
  have hr : md 7 n < 7 := md_lt (by decide) n
  have t := allB_spec 7 _ chkCases_true (md 7 n) hr
  rcases LibTheta11.or3 t with h | h | h
  · exact Or.inl (LibSplit3.ieq_eq h)
  · exact Or.inr (Or.inl (LibSplit3.ieq_eq h))
  · obtain ⟨h1, h2⟩ := LibTwoSqPiQ.and_split h
    exact Or.inr (Or.inr ⟨LibSplit3.ieq_eq h1, dvd_of_md_zero (Nat.eq_of_beq_eq_true h2)⟩)

theorem prime_eq {q p : Nat} (hq : 1 < q) (hP : LibPrimePow.Prime p) (h : q ∣ p) : p = q := by
  cases hP.2 q h with
  | inl e => exact absurd (e ▸ hq) (Nat.lt_irrefl 1)
  | inr e => exact e.symm

theorem n7_2 : ¬ 7 ∣ 2 := fun hd => by
  have p2 : LibPrimePow.Prime 2 := LibPrimeB.prime_of_isPrimeB (by decide)
  cases p2.2 7 hd with
  | inl e => exact absurd e (by decide)
  | inr e => exact absurd e (by decide)

theorem prime_data (p : Nat) (hP : LibPrimePow.Prime p) :
    (chi7 p = 1 ∧ (∃ u v : Int, Nf (1 + 1) u v = (p : Int)) ∧ ¬ p ∣ 4 * 1 + 3)
      ∨ (chi7 p = -1 ∧ ∀ x y : Int, IDv (p : Int) (Nf (1 + 1) x y) → IDv (p : Int) x ∧ IDv (p : Int) y)
      ∨ (chi7 p = 0 ∧ (∃ u v : Int, Nf (1 + 1) u v = (p : Int)) ∧ ¬ p ∣ 2 ∧ p ∣ 4 * 1 + 3) := by
  have p7 : LibPrimePow.Prime 7 := LibPrimeB.prime_of_isPrimeB (by decide)
  rcases chi7_cases p with hc | hc | ⟨hc, hd⟩
  · have hne : p ≠ 7 := fun e => by rw [e] at hc; exact absurd hc (by decide)
    have n7 : ¬ p ∣ 4 * 1 + 3 := fun h7 => by
      cases p7.2 p h7 with
      | inl e => exact absurd (e ▸ hP.1) (Nat.lt_irrefl 1)
      | inr e => exact hne e
    refine Or.inl ⟨hc, ?_, n7⟩
    cases (show p = 2 ∨ ¬ p ∣ 2 from by
      cases e : Nat.beq p 2
      · exact Or.inr (fun hd2 => by
          have p2 : LibPrimePow.Prime 2 := LibPrimeB.prime_of_isPrimeB (by decide)
          cases p2.2 p hd2 with
          | inl e1 => exact absurd (e1 ▸ hP.1) (Nat.lt_irrefl 1)
          | inr e1 => rw [e1] at e; exact Bool.noConfusion e)
      · exact Or.inl (Nat.eq_of_beq_eq_true e)) with
    | inl h2 => subst h2; exact ⟨0, 1, rfl⟩
    | inr h2 =>
      have h3 : ¬ p ∣ 3 := fun hd3 => by
        have p3 : LibPrimePow.Prime 3 := LibPrimeB.prime_of_isPrimeB (by decide)
        cases p3.2 p hd3 with
        | inl e => exact absurd (e ▸ hP.1) (Nat.lt_irrefl 1)
        | inr e => rw [e] at hc; exact absurd hc (by decide)
      obtain ⟨m, hm⟩ := LibTheta11.odd_prime_k hP h2
      have hp1 : LibZp.Prime1 (LibEulerCrit.k m) := LibPrimeB.prime1_of_prime (hm ▸ hP)
      obtain ⟨t, ht⟩ := root7 m hp1 (hm ▸ hne) (hm ▸ hc)
      rw [← hm] at ht
      exact pi_of_root7 hP h3 t ht
  · exact Or.inr (Or.inl ⟨hc, inert7 hP hc⟩)
  · have e := prime_eq (q := 7) (by decide) hP hd
    subst e
    exact Or.inr (Or.inr ⟨hc, ⟨1, -2, rfl⟩, n7_2, ⟨1, rfl⟩⟩)

set_option maxRecDepth 100000 in
theorem RH_one : RH 1 1 = 1 := by decide

theorem theta7_half {n : Nat} (hn : 0 < n) :
    (RH 1 n : Int) = LibEisGen.e LibInt.intR chi7 (LibDivFun.one1 LibInt.intR) n :=
  theta_eis 1 chi7 chi7_cmult prime_data RH_one hn

theorem theta7 {n : Nat} (hn : 0 < n) :
    (RZ 1 n : Int) = 2 * LibEisGen.e LibInt.intR chi7 (LibDivFun.one1 LibInt.intR) n := by
  rw [RZ_eq_RH 1 hn, LibTwoSqJacobi.cast_add, theta7_half hn]; lib_int

end LibTheta7

#print axioms LibTheta7.par_three
#print axioms LibTheta7.leg7_of_chi
#print axioms LibTheta7.leg_p7
#print axioms LibTheta7.mk7_ne
#print axioms LibTheta7.sq_neg7
#print axioms LibTheta7.root7
#print axioms LibTheta7.chi7_cases
#print axioms LibTheta7.prime_data
#print axioms LibTheta7.RH_one
#print axioms LibTheta7.theta7_half
#print axioms LibTheta7.theta7

lib/Theta8.lean

lean_all/lib/Theta8.lean

/-
  lib/Theta8.lean — T46 shared library (G1536b, theta series of x² + 2y²): the theta series of discriminant −8
  is an Eisenstein series.

    chi8           χ₋₈(n) = 1, 1, −1, −1 for n ≡ 1, 3, 5, 7 (mod 8), 0 for n even
    chi8_cmult     χ₋₈ is completely multiplicative
    norm8          p = x² + 2y² ⇒ χ₋₈(p) ≠ −1
    inert8         χ₋₈(p) = −1 ⇒ p ∣ x² + 2y² forces p ∣ x, p ∣ y      (no root of t² + 2, Split2.inert2)
    split8         χ₋₈(p) = 1 ⇒ p = x² + 2y²                           (Root2.root2, Split2.pi_of_root2)
    prime_data     every prime is split, inert, or ramified (p = 2 = N(0, 1))
    theta8_half    n > 0 ⇒ RH(n) = Σ_{d ∣ n} χ₋₈(d)
    theta8         n > 0 ⇒ #{(x, y) ∈ ℤ² : x² + 2y² = n} = 2 Σ_{d ∣ n} χ₋₈(d)
  Lean 4 core only, proofs by hand, no axioms.
-/
import lib.Root2
import lib.Split2
import lib.NormP0F
import lib.Theta11

namespace LibTheta8
open LibMod LibGcd LibIntBox LibNormPiA LibNormP0B LibNormP0F LibChi11 LibSplit2

def F8 : Nat → Int
  | 0 => 0
  | 1 => 1
  | 2 => 0
  | 3 => 1
  | 4 => 0
  | 5 => -1
  | 6 => 0
  | 7 => -1
  | _ + 8 => 0

def chi8 (n : Nat) : Int := F8 (md 8 n)

def chkMul : Bool := allB 8 (fun u => allB 8 (fun v => ieq (F8 (md 8 (u * v))) (F8 u * F8 v)))

theorem chkMul_true : chkMul = true := by decide

theorem F8_mul {u v : Nat} (hu : u < 8) (hv : v < 8) : F8 (md 8 (u * v)) = F8 u * F8 v :=
  eq_of_ieq (allB_spec _ _ (allB_spec _ _ chkMul_true u hu) v hv)

theorem chi8_mul (x y : Nat) : chi8 (x * y) = chi8 x * chi8 y := by
  unfold chi8
  rw [md_mul (n := 8) (by decide) x y]
  exact F8_mul (md_lt (n := 8) (by decide) x) (md_lt (n := 8) (by decide) y)

theorem chi8_cmult : LibDirTwist.CMult LibInt.intR chi8 := ⟨rfl, chi8_mul⟩

def chkCases : Bool := allB 8 (fun r => ieq (F8 r) 1 || ieq (F8 r) (-1) || (ieq (F8 r) 0 && Nat.beq (md 2 r) 0))

theorem chkCases_true : chkCases = true := by decide

theorem chi8_cases (n : Nat) : chi8 n = 1 ∨ chi8 n = -1 ∨ (chi8 n = 0 ∧ 2 ∣ n) := by
  have hr : md 8 n < 8 := md_lt (by decide) n
  have t := allB_spec 8 _ chkCases_true (md 8 n) hr
  rcases LibTheta11.or3 t with h | h | h
  · exact Or.inl (LibTheta11.ieq_eq h)
  · exact Or.inr (Or.inl (LibTheta11.ieq_eq h))
  · obtain ⟨h1, h2⟩ := LibTwoSqPiQ.and_split h
    refine Or.inr (Or.inr ⟨LibTheta11.ieq_eq h1, ?_⟩)
    have s := spec 8 n
    generalize dv 8 n = q at s
    generalize md 8 n = r at s h2
    obtain ⟨c, hc⟩ := dvd_of_md_zero (n := 2) (Nat.eq_of_beq_eq_true h2)
    exact ⟨4 * q + c, by rw [s, hc]; lib_nat⟩

def chkNorm : Bool := allB 8 (fun r => allB 8 (fun s => !(ieq (F8 (md 8 (r * r + 2 * (s * s)))) (-1))))

theorem chkNorm_true : chkNorm = true := by decide

theorem norm8 {x y : Int} {p : Nat} (h : N2 x y = (p : Int)) : chi8 p ≠ -1 := by
  have hp : p = x.natAbs * x.natAbs + 2 * (y.natAbs * y.natAbs) := by
    have hi : (p : Int) = ((x.natAbs * x.natAbs + 2 * (y.natAbs * y.natAbs) : Nat) : Int) := by
      rw [← h]; unfold N2
      rw [sq_natAbs x, sq_natAbs y]; rfl
    exact Int.ofNat.inj hi
  have sx := spec 8 x.natAbs
  have sy := spec 8 y.natAbs
  have rx := md_lt (n := 8) (by decide) x.natAbs
  have ry := md_lt (n := 8) (by decide) y.natAbs
  generalize x.natAbs = X at hp sx rx
  generalize y.natAbs = Y at hp sy ry
  generalize dv 8 X = a at sx
  generalize dv 8 Y = b at sy
  generalize md 8 X = r at sx rx
  generalize md 8 Y = s at sy ry
  have e : p = (r * r + 2 * (s * s)) + 8 * (8 * a * a + 2 * a * r + 2 * (8 * b * b + 2 * b * s)) := by
    rw [hp, sx, sy]; lib_nat
  unfold chi8
  rw [e, md_add_mul (by decide)]
  have hs := allB_spec _ _ (allB_spec _ _ chkNorm_true r rx) s ry
  intro hm
  rw [hm, ieq_self] at hs
  exact Bool.noConfusion hs

theorem inert8 {p : Nat} (hP : LibPrimePow.Prime p) (hc : chi8 p = -1) :
    ∀ x y : Int, IDv (p : Int) (N0 (1 + 1) x y) → IDv (p : Int) x ∧ IDv (p : Int) y := by
  have hp0 : 0 < p := Nat.lt_trans Nat.zero_lt_one hP.1
  have hnr : ∀ t : Int, ¬ IDv (p : Int) (t * t + 2) := fun t ⟨b, hb⟩ => by
    obtain ⟨tn, a, ha⟩ := res_nat hp0 t
    have ht : IDv (p : Int) ((tn : Int) * tn + 2) := ⟨b + a * ((tn : Int) + t), by
      have e : (tn : Int) * tn + 2 = (t * t + 2) + ((tn : Int) - t) * ((tn : Int) + t) := by lib_int
      rw [e, hb, ha]; lib_int⟩
    obtain ⟨u, v, huv⟩ := pi_of_root2 hP tn ht
    exact norm8 huv hc
  exact inert2 hP hnr

theorem ndvd8 {p : Nat} (hP : LibPrimePow.Prime p) (h2 : ¬ p ∣ 2) : ¬ p ∣ 4 * 2 := fun hd => by
  have hd' : p ∣ 2 * (2 * 2) := hd
  cases euclid_prime hP.2 hd' with
  | inl a => exact h2 a
  | inr a => cases euclid_prime hP.2 a with
    | inl b => exact h2 b
    | inr b => exact h2 b

/-- p = 2h + 1 with h = m + 1, χ₋₈(p) = 1 ⇒ h ≡ 0, 1 (mod 4). -/
theorem h_mod4 (m : Nat) (hc : chi8 (LibEulerCrit.k m + 1) = 1) :
    (∃ b, m + 1 = 4 * b) ∨ (∃ b, m + 1 = 4 * b + 1) := by
  have s := spec 4 (m + 1)
  have hr := md_lt (n := 4) (by decide) (m + 1)
  generalize dv 4 (m + 1) = q at s
  generalize md 4 (m + 1) = r at s hr
  have e : LibEulerCrit.k m + 1 = (2 * r + 1) + 8 * q := by
    show 2 * (m + 1) + 1 = _; rw [s]; lib_nat
  unfold chi8 at hc
  rw [e, md_add_mul (by decide)] at hc
  match r, hr with
  | 0, _ => exact Or.inl ⟨q, by rw [s]; try lib_nat⟩
  | 1, _ => exact Or.inr ⟨q, by rw [s]; try lib_nat⟩
  | 2, _ => exact absurd hc (by decide)
  | 3, _ => exact absurd hc (by decide)
  | r + 4, hr => exact absurd (Nat.lt_of_succ_lt_succ (Nat.lt_of_succ_lt_succ (Nat.lt_of_succ_lt_succ (Nat.lt_of_succ_lt_succ hr)))) (Nat.not_lt_zero r)

theorem split8 {p : Nat} (hP : LibPrimePow.Prime p) (hc : chi8 p = 1) :
    (∃ u v : Int, N0 (1 + 1) u v = (p : Int)) ∧ ¬ p ∣ 4 * 2 := by
  have h2 : ¬ p ∣ 2 := fun hd2 => by
    have p2 : LibPrimePow.Prime 2 := LibPrimeB.prime_of_isPrimeB (by decide)
    cases p2.2 p hd2 with
    | inl e => exact absurd (e ▸ hP.1) (Nat.lt_irrefl 1)
    | inr e => rw [e] at hc; exact absurd hc (by decide)
  obtain ⟨m, hm⟩ := LibTheta11.odd_prime_k hP h2
  have hp1 : LibZp.Prime1 (LibEulerCrit.k m) := LibPrimeB.prime1_of_prime (hm ▸ hP)
  obtain ⟨t, ht⟩ := LibRoot2.root2 m hp1 (h_mod4 m (hm ▸ hc))
  rw [← hm] at ht
  obtain ⟨u, v, huv⟩ := pi_of_root2 hP t ht
  exact ⟨⟨u, v, huv⟩, ndvd8 hP h2⟩

theorem prime_data (p : Nat) (hP : LibPrimePow.Prime p) :
    (chi8 p = 1 ∧ (∃ u v : Int, N0 (1 + 1) u v = (p : Int)) ∧ ¬ p ∣ 4 * (1 + 1))
      ∨ (chi8 p = -1 ∧ ∀ x y : Int, IDv (p : Int) (N0 (1 + 1) x y) → IDv (p : Int) x ∧ IDv (p : Int) y)
      ∨ (chi8 p = 0 ∧ ∃ u v : Int, N0 (1 + 1) u v = (p : Int) ∧
          ∀ x y : Int, IDv (p : Int) (N0 (1 + 1) x y) → piB (1 + 1) u v p x y = true) := by
  rcases chi8_cases p with hc | hc | ⟨hc, hd⟩
  · exact Or.inl ⟨hc, split8 hP hc⟩
  · exact Or.inr (Or.inl ⟨hc, inert8 hP hc⟩)
  · have p2 : LibPrimePow.Prime 2 := LibPrimeB.prime_of_isPrimeB (by decide)
    have e : p = 2 := by
      cases hP.2 2 hd with
      | inl e => exact absurd e (by decide)
      | inr e => exact e.symm
    subst e
    exact Or.inr (Or.inr ⟨hc, 0, 1, rfl, LibNormP0E.ram_dvd2⟩)

set_option maxRecDepth 100000 in
theorem RH_one : RH 1 1 = 1 := by decide

theorem theta8_half {n : Nat} (hn : 0 < n) :
    (RH 1 n : Int) = LibEisGen.e LibInt.intR chi8 (LibDivFun.one1 LibInt.intR) n :=
  theta_eis 1 chi8 chi8_cmult prime_data RH_one hn

/-- **Theta series of x² + 2y².** n > 0 ⇒ #{x² + 2y² = n} = 2 Σ_{d ∣ n} χ₋₈(d). -/
theorem theta8 {n : Nat} (hn : 0 < n) :
    (RZ 1 n : Int) = 2 * LibEisGen.e LibInt.intR chi8 (LibDivFun.one1 LibInt.intR) n := by
  rw [RZ_eq_RH 1 hn, LibTwoSqJacobi.cast_add, theta8_half hn]; lib_int

end LibTheta8

#print axioms LibTheta8.chi8_cmult
#print axioms LibTheta8.chi8_cases
#print axioms LibTheta8.norm8
#print axioms LibTheta8.inert8
#print axioms LibTheta8.split8
#print axioms LibTheta8.prime_data
#print axioms LibTheta8.RH_one
#print axioms LibTheta8.theta8

lib/Theta88.lean

lean_all/lib/Theta88.lean

/-
  lib/Theta88.lean — T46 shared library (G1574c): the genus theta series of discriminant −88 (class number 2):

      r₁(n) + r₂(n) = 2 Σ_{d ∣ n} χ₋₈₈(d),   r₁(n) − r₂(n) = 2 Σ_{d ∣ n} χ₈(d) χ₋₁₁(n/d),
      r₁ = #{x² + 22y² = n},  r₂ = #{2x² + 11y² = n},  n > 0.

    data88         the LocalData at every prime (split: PairDiag, inert: Root88, ramified 2 and 11: RamDiag)
    theta88        r₁ + r₂ = 2E(χ₋₈₈, 1)
    theta88D       r₁ − r₂ = 2E(χ₈, χ₋₁₁)
    r1_formula88   2r₁ = 2E(χ₋₈₈, 1) + 2E(χ₈, χ₋₁₁)
  Lean 4 core only, proofs by hand, no axioms.
-/
import lib.ThetaGenus2
import lib.RamDiag
import lib.Root88
import lib.Theta40

namespace LibTheta88
open LibMod LibGcd LibIntBox LibNormPiA LibNormPiC LibChi11 LibFormCount LibFormRel LibPairDiag LibRoot88
  LibThetaGenus2

theorem hb1 : ∀ (m : Nat) x y, gd1 11 x y = (m : Int) → x.natAbs ≤ m ∧ y.natAbs ≤ m := gd1_bound 10
theorem hb2 : ∀ (m : Nat) x y, gd2 11 x y = (m : Int) → x.natAbs ≤ m ∧ y.natAbs ≤ m := gd2_bound 10

/-- residues mod 88: χ₋₈₈ = −1 forces ψ₁ + ψ₂ = 0, and χ₋₈₈ ∈ {1, −1} unless 2 or 11 divides -/
def chkG : Bool := allB 88 (fun r =>
  (!(ieq (LibTheta24D.F8 (md 8 r) * LibChi11.F11 (md 11 r)) (-1)) || ieq (LibTheta24D.F8 (md 8 r) + LibChi11.F11 (md 11 r)) 0) &&
  (ieq (LibTheta24D.F8 (md 8 r) * LibChi11.F11 (md 11 r)) 1 || ieq (LibTheta24D.F8 (md 8 r) * LibChi11.F11 (md 11 r)) (-1) ||
    (Nat.beq (md 2 r) 0 || Nat.beq (md 11 r) 0)))

set_option maxRecDepth 100000 in
theorem chkG_true : chkG = true := by decide

theorem vals (p : Nat) : LibTheta24D.chi8 p = LibTheta24D.F8 (md 8 (md 88 p)) ∧ LibChi11.chi11 p = LibChi11.F11 (md 11 (md 88 p)) := by
  unfold LibTheta24D.chi8 LibChi11.chi11
  rw [← LibRoot15.md_of_md_mul 8 11 p (by decide), ← LibRoot15.md_of_md_mul 11 8 p (by decide)]; exact ⟨rfl, rfl⟩

theorem nidv {p q : Nat} (hP : LibPrimePow.Prime p) (hQ : LibPrimePow.Prime q) (hq : ¬ q ∣ p) :
    ¬ IDv (p : Int) ((q : Nat) : Int) := fun h => LibTheta15.ndvd_small hP hQ hq (idv_natAbs h)

theorem n2_11 : ¬ 2 ∣ 10 + 1 := LibSplit88.ndm (q := 2) (n := 11) (by decide) (by decide)
theorem n11_2 : ¬ 10 + 1 ∣ 2 := LibSplit88.ndm (q := 11) (n := 2) (by decide) (by decide)

/-- **Local data at every prime** for (x² + 22y², 2x² + 11y²). -/
theorem data88 (p : Nat) (hP : LibPrimePow.Prime p) :
    LocalData (gd1 11) (gd2 11) chi88 LibTheta24D.chi8 LibChi11.chi11 p := by
  obtain ⟨tA, tB⟩ := LibTwoSqPiQ.and_split (allB_spec _ _ chkG_true (md 88 p) (md_lt (by decide) p))
  obtain ⟨v8, v11⟩ := vals p
  have ec : chi88 p = LibTheta24D.F8 (md 8 (md 88 p)) * LibChi11.F11 (md 11 (md 88 p)) := by
    unfold chi88; rw [v8, v11]
  rcases LibTheta11.or3 tB with h1 | hm1 | h0
  · have hc : chi88 p = 1 := by rw [ec]; exact eq_of_ieq h1
    have h211 := prime_to_22 (n := p) (by rw [hc]; decide)
    have h2' := LibRoot20.ndvd2 hP h211.1
    have h11' : ¬ IDv (p : Int) 11 := nidv hP LibSplit88.P11 h211.2
    obtain ⟨u, v, h | h⟩ := split88 hP hc
    · obtain ⟨e8, e11⟩ := gen1 h h211.1 h211.2
      exact LocalData.split11 ⟨pairA11 hb1 h hP.1 hP.2 h2' h11'⟩ ⟨pairA22 hb2 h hP.1 hP.2 h2' h11'⟩ hc
        (by rw [e8, e11]; decide) (by rw [e8, e11]; decide)
    · obtain ⟨e8, e11⟩ := gen2 h h211.1 h211.2
      exact LocalData.split12 ⟨pairB12 hb1 hb2 h hP.1 hP.2 h2' h11'⟩ ⟨pairB21 hb1 hb2 h hP.1 hP.2 h2' h11'⟩ hc
        (by rw [e8, e11]; decide) (by rw [e8, e11]; decide)
  · have hc : chi88 p = -1 := by rw [ec]; exact eq_of_ieq hm1
    have hs := eq_of_ieq (LibTheta40.imp tA hm1)
    exact LocalData.inert (inert88_g1 hP hc) (inert88_g2 hP hc) hc hc (by rw [v8, v11]; exact hs)
  · rcases LibGenus20.or2' h0 with e | e
    · have e2 := LibTheta40.prime_eq hP LibTheta40.P2 (dvd_of_md_zero (by rw [LibRoot15.md_of_md_mul 2 44 p (by decide)]; exact Nat.eq_of_beq_eq_true e))
      subst e2
      exact LocalData.ramSwap (fun n => LibRamDiag.ram2_diag (kN := 10) n2_11 n) (by decide) (by decide) (by decide)
    · have e11 := LibTheta40.prime_eq hP LibSplit88.P11 (dvd_of_md_zero (by rw [LibRoot15.md_of_md_mul 11 8 p (by decide)]; exact Nat.eq_of_beq_eq_true e))
      subst e11
      exact LocalData.ramSwap (fun n => LibRamDiag.ramk_diag (kN := 10) LibSplit88.P11 n11_2 n) (by decide) (by decide) (by decide)

theorem RF1_one : RF (gd1 11) 1 = 2 := by decide
theorem RF2_one : RF (gd2 11) 1 = 0 := by decide

/-- **Genus theta series of discriminant −88.** n > 0 ⇒ #{x² + 22y² = n} + #{2x² + 11y² = n} = 2Σ_{d∣n} χ₋₈₈(d). -/
theorem theta88 {n : Nat} (hn : 0 < n) :
    LibThetaGenus2.R (gd1 11) (gd2 11) n = 2 * LibEisGen.e LibInt.intR chi88 (LibDivFun.one1 LibInt.intR) n :=
  theta_sum hb1 hb2 (gd1_s 11) (gd2_s 11) chi88_cmult RF1_one RF2_one data88 hn

/-- **The genus difference.** n > 0 ⇒ #{x² + 22y² = n} − #{2x² + 11y² = n} = 2Σ_{d∣n} χ₈(d)χ₋₁₁(n/d). -/
theorem theta88D {n : Nat} (hn : 0 < n) :
    LibThetaGenus2.D (gd1 11) (gd2 11) n = 2 * LibEisGen.e LibInt.intR LibTheta24D.chi8 LibChi11.chi11 n :=
  theta_diff hb1 hb2 (gd1_s 11) (gd2_s 11) LibTheta24D.chi8_cmult LibChi11.chi11_cmult RF1_one RF2_one data88 hn

/-- **x² + 22y².** n > 0 ⇒ 2#{x² + 22y² = n} = 2Σχ₋₈₈(d) + 2Σχ₈(d)χ₋₁₁(n/d). -/
theorem r1_formula88 {n : Nat} (hn : 0 < n) :
    2 * (RF (gd1 11) n : Int) = 2 * LibEisGen.e LibInt.intR chi88 (LibDivFun.one1 LibInt.intR) n
      + 2 * LibEisGen.e LibInt.intR LibTheta24D.chi8 LibChi11.chi11 n :=
  r1_formula hb1 hb2 (gd1_s 11) (gd2_s 11) chi88_cmult LibTheta24D.chi8_cmult LibChi11.chi11_cmult RF1_one RF2_one data88 hn

end LibTheta88

#print axioms LibTheta88.chkG_true
#print axioms LibTheta88.data88
#print axioms LibTheta88.theta88
#print axioms LibTheta88.theta88D
#print axioms LibTheta88.r1_formula88

lib/ThetaClassOne.lean

lean_all/lib/ThetaClassOne.lean

/-
  lib/ThetaClassOne.lean — T46 shared library (G1525c): theta series of the forms of class number one, discriminants
  −19, −43, −67, −163, as Eisenstein series.

    sqF, legB_eq_sqF   the Legendre test QRChain.legB with % (fast in the kernel)
    norm_one           c ≥ 2, x² + xy + c·y² = 1 ⇒ y = 0, x = ±1
    RH_one             c ≥ 2 ⇒ RH(1) = 1                         (count through (x, y) ↦ (x, y) onto the box [−1, 1]²)
    theta19, theta43, theta67, theta163
                       n > 0 ⇒ #{(x, y) ∈ ℤ² : x² + xy + ((q+1)/4)·y² = n} = 2 Σ_{d ∣ n} (d/q)
  (ThetaQ.thetaQ with the small-prime data from one Boolean table, ClassOneData.dataB.)
  Lean 4 core only, proofs by hand, no axioms.
-/
import lib.ClassOneData
import lib.ThetaQ

namespace LibThetaClassOne
open LibMod LibGcd LibIntBox LibNormPiA LibNormPiB LibNormPiF LibChi11 LibClassOne LibClassOneData LibThetaQ LibEulerCrit LibZp LibSplit11

def sqF (m a : Nat) : Nat → Bool
  | 0 => Nat.beq ((0 * 0) % m) (a % m)
  | n + 1 => sqF m a n || Nat.beq (((n + 1) * (n + 1)) % m) (a % m)

theorem sqS_eq_sqF (M a : Nat) : ∀ n, QRChain.sqS (M + 1) a n = sqF (M + 1) a n
  | 0 => by
    show Nat.beq (FTAChain.rem (M + 1) (0 * 0)) (FTAChain.rem (M + 1) a) = Nat.beq ((0 * 0) % (M + 1)) (a % (M + 1))
    rw [LibFTAB.rem_eq_md, LibFTAB.rem_eq_md, LibNatMod.mod_eq_md (Nat.succ_pos M), LibNatMod.mod_eq_md (Nat.succ_pos M)]
  | n + 1 => by
    show (QRChain.sqS (M + 1) a n || Nat.beq (FTAChain.rem (M + 1) ((n + 1) * (n + 1))) (FTAChain.rem (M + 1) a))
      = (sqF (M + 1) a n || Nat.beq (((n + 1) * (n + 1)) % (M + 1)) (a % (M + 1)))
    rw [sqS_eq_sqF M a n, LibFTAB.rem_eq_md, LibFTAB.rem_eq_md, LibNatMod.mod_eq_md (Nat.succ_pos M),
      LibNatMod.mod_eq_md (Nat.succ_pos M)]

theorem legB_eq_sqF (M a : Nat) : QRChain.legB (M + 1) a = sqF (M + 1) a (M + 1) := sqS_eq_sqF M a (M + 1)

theorem chi2_ne (M : Nat) (h : sqF (k M + 1) 2 (k M + 1) = false) : chiQ (k M + 1) 2 ≠ 1 := by
  unfold chiQ
  rw [legB_eq_sqF, h]
  cases Nat.beq (md (k M + 1) 2) 0 <;> decide

theorem norm_one (c'' : Nat) {x y : Int} (h : Nf (c'' + 2) x y = 1) : y = 0 ∧ (x = 1 ∨ x = -1) := by
  have i4 : 4 * Nf (c'' + 2) x y = (((2 * x + y).natAbs * (2 * x + y).natAbs + (4 * c'' + 7) * (y.natAbs * y.natAbs) : Nat) : Int) := by
    show _ = (((2 * x + y).natAbs * (2 * x + y).natAbs : Nat) : Int) + (4 * (c'' : Int) + 7) * ((y.natAbs * y.natAbs : Nat) : Int)
    rw [← sq_natAbs, ← sq_natAbs]; unfold Nf; show 4 * (x * x + x * y + ((c'' : Int) + 2) * (y * y)) = _; lib_int
  rw [h] at i4
  have n4 : 4 = (2 * x + y).natAbs * (2 * x + y).natAbs + (4 * c'' + 7) * (y.natAbs * y.natAbs) := Int.ofNat.inj i4
  have hy : y.natAbs * y.natAbs = 0 := by
    cases e : y.natAbs * y.natAbs with
    | zero => rfl
    | succ w =>
      rw [e] at n4
      have : 7 ≤ 4 := by
        rw [n4, show (2 * x + y).natAbs * (2 * x + y).natAbs + (4 * c'' + 7) * (w + 1)
          = ((2 * x + y).natAbs * (2 * x + y).natAbs + (4 * c'' + 7) * w + 4 * c'') + 7 by lib_nat]
        exact Nat.le_add_left 7 _
      exact absurd this (by decide)
  have y0 := natAbs_zero (LibTwoSqUniq.zero_of_sq _ hy)
  refine ⟨y0, ?_⟩
  rw [y0] at h
  have hx : x * x = 1 := by rw [← h]; unfold Nf; lib_int
  rw [sq_natAbs] at hx
  have hx' : x.natAbs * x.natAbs = 1 := Int.ofNat.inj hx
  cases x with
  | ofNat n =>
    match n, hx' with
    | 0, h0 => exact absurd h0 (by decide)
    | 1, _ => exact Or.inl rfl
    | n + 2, h2 => exfalso; exact absurd h2 (by
        show (n + 2) * (n + 2) ≠ 1
        rw [show (n + 2) * (n + 2) = (n * n + 4 * n + 2) + 1 + 1 by lib_nat]
        exact fun e => Nat.noConfusion (Nat.succ.inj e))
  | negSucc n =>
    match n, hx' with
    | 0, _ => exact Or.inr rfl
    | n + 1, h2 => exfalso; exact absurd h2 (by
        show (n + 2) * (n + 2) ≠ 1
        rw [show (n + 2) * (n + 2) = (n * n + 4 * n + 2) + 1 + 1 by lib_nat]
        exact fun e => Nat.noConfusion (Nat.succ.inj e))

def oneQ (x y : Int) : Bool := ieq x 1 && ieq y 0

theorem cnt_oneQ : CntZ oneQ 1 = 1 := by decide

theorem RH_one (c'' : Nat) : RH (c'' + 1) 1 = 1 := by
  unfold RH
  rw [← cnt_oneQ]
  refine count_bij_Z _ _ _ _ (fun x y => (x, y)) (fun x y => (x, y)) (fun x y _ _ hP => ?_) (fun x y _ _ hQ => ?_)
  · obtain ⟨hp, hN⟩ := LibTwoSqPiQ.and_split hP
    have hN' : Nf (c'' + 2) x y = 1 := eq_of_ieq hN
    obtain ⟨y0, hx | hx⟩ := norm_one c'' hN'
    · subst y0; subst hx; exact ⟨Nat.le_refl 1, Nat.zero_le 1, rfl, rfl⟩
    · subst y0; subst hx; exact absurd hp (by decide)
  · obtain ⟨hx, hy⟩ := LibTwoSqPiQ.and_split hQ
    have hx' := eq_of_ieq hx
    have hy' := eq_of_ieq hy
    subst hx'; subst hy'
    refine ⟨Nat.succ_le_succ (Nat.zero_le _), Nat.zero_le _, ?_, rfl⟩
    show (hpos 1 0 && ieq (Nf (c'' + 1 + 1) 1 0) ((1 : Nat) : Int)) = true
    have : Nf (c'' + 1 + 1) 1 0 = ((1 : Nat) : Int) := by unfold Nf; show (1 : Int) * 1 + 1 * 0 + _ * (0 * 0) = 1; lib_int
    rw [this, ieq_self]; rfl

theorem data19 : dataB 7 5 = true := by decide
theorem data43 : dataB 13 11 = true := by decide
theorem data67 : dataB 19 17 = true := by decide
theorem data163 : dataB 43 41 = true := by decide

theorem prime19 : Prime1 (LibEulerCrit.k 8) := LibPrimeTest.prime1_of (by decide)
theorem prime43 : Prime1 (LibEulerCrit.k 20) := LibPrimeTest.prime1_of (by decide)
theorem prime67 : Prime1 (LibEulerCrit.k 32) := LibPrimeTest.prime1_of (by decide)
theorem prime163 : Prime1 (LibEulerCrit.k 80) := LibPrimeTest.prime1_of (by decide)

theorem theta19 {n : Nat} (hn : 0 < n) :
    (RZ 4 n : Int) = 2 * LibEisGen.e LibInt.intR (chiQ 19) (LibDivFun.one1 LibInt.intR) n :=
  thetaQ 8 prime19 4 rfl (data_of_dataB data19) (chi2_ne 8 (by decide)) (RH_one 3) hn

theorem theta43 {n : Nat} (hn : 0 < n) :
    (RZ 10 n : Int) = 2 * LibEisGen.e LibInt.intR (chiQ 43) (LibDivFun.one1 LibInt.intR) n :=
  thetaQ 20 prime43 10 rfl (data_of_dataB data43) (chi2_ne 20 (by decide)) (RH_one 9) hn

theorem theta67 {n : Nat} (hn : 0 < n) :
    (RZ 16 n : Int) = 2 * LibEisGen.e LibInt.intR (chiQ 67) (LibDivFun.one1 LibInt.intR) n :=
  thetaQ 32 prime67 16 rfl (data_of_dataB data67) (chi2_ne 32 (by decide)) (RH_one 15) hn

theorem theta163 {n : Nat} (hn : 0 < n) :
    (RZ 40 n : Int) = 2 * LibEisGen.e LibInt.intR (chiQ 163) (LibDivFun.one1 LibInt.intR) n :=
  thetaQ 80 prime163 40 rfl (data_of_dataB data163) (chi2_ne 80 (by decide)) (RH_one 39) hn

end LibThetaClassOne

#print axioms LibThetaClassOne.legB_eq_sqF
#print axioms LibThetaClassOne.chi2_ne
#print axioms LibThetaClassOne.norm_one
#print axioms LibThetaClassOne.RH_one
#print axioms LibThetaClassOne.theta19
#print axioms LibThetaClassOne.theta43
#print axioms LibThetaClassOne.theta67
#print axioms LibThetaClassOne.theta163

lib/ThetaD0.lean

lean_all/lib/ThetaD0.lean

/-
  lib/ThetaD0.lean — T46 shared library (G1539–G1540, agent3 T45 6f-A′-1): a quantitative lower bound for the odd
  Gaussian sum and the split of agent3's D_0 into the odd sum and the alternating moment.

    S_M(x)      Σ_{i<M} 2x^{k²}(1 − k²(1 − x²)), k = 2i + 1                     (LibThetaOddTel.ssum oddT)
    A_K(x)      Σ_{k=1}^{K} (−1)^k k² x^{k²}                                    (altA)
    D0 x M K    Σ_{k=2}^{K+1} (−1)^k 2k² x^{k²+3} + Σ_{i=1}^{M} 2x^{k²+1}(1 − k²(1 − x²))
    oddsum_lower1   0 ≤ x ≤ 1 ⇒ 2x³(1 − x) + 2(M+1) x^{(2M+2)²} ≤ S_{M+1}(x)
    D0_eq       D0 x M K = x·S_{M+1}(x) + 2x³ A_{K+1}(x)                  (any commutative ring)
    D0_lower    0 ≤ x ≤ 1 ⇒ 2x⁴(1 − x) + 2x³ A_{K+1}(x) ≤ D0 x M K
  So D_0 ≥ 0 reduces to the alternating moment: A_{K+1}(x) ≥ −x(1 − x).
  Lean 4 core only, proofs by hand, no axioms.
-/
import lib.ThetaQPos

namespace LibThetaD0
open DetChain LibRing YMChain LibThetaOddTel LibThetaQPos

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

/-- (−1)^k k² x^{k²} for k = j + 1. -/
def altT (x : R) (j : Nat) : R :=
  K.mul (pw K (K.neg K.one) (j + 1)) (K.mul (K.mul (castN K (j + 1)) (castN K (j + 1))) (pw K x ((j + 1) * (j + 1))))

/-- A_K(x) = Σ_{k=1}^{K} (−1)^k k² x^{k²}. -/
def altA (x : R) (n : Nat) : R := ssum K (altT K x) n

/-- D_0 with the two brackets of 6f-A′-1 (k = j + 2 in the first, k = 2i + 3 in the second). -/
def D0 (x : R) (M Kk : Nat) : R :=
  K.add (ssum K (fun j => K.mul (K.add K.one K.one) (K.mul (altT K x (j + 1)) (K.mul x (K.mul x x)))) Kk)
    (ssum K (fun i => K.mul x (oddT K x (i + 1))) M)

theorem ssum_succ' (f : Nat → R) : ∀ n, ssum K f (n + 1) = K.add (f 0) (ssum K (fun i => f (i + 1)) n)
  | 0 => by show K.add K.zero (f 0) = K.add (f 0) K.zero; lib_ring
  | n + 1 => by
    show K.add (ssum K f (n + 1)) (f (n + 1)) = K.add (f 0) (K.add (ssum K (fun i => f (i + 1)) n) (f (n + 1)))
    rw [ssum_succ' f n]; lib_ring

theorem ssum_mul (c : R) (f : Nat → R) : ∀ n, ssum K (fun i => K.mul c (f i)) n = K.mul c (ssum K f n)
  | 0 => by show K.zero = K.mul c K.zero; lib_ring
  | n + 1 => by
    show K.add (ssum K (fun i => K.mul c (f i)) n) (K.mul c (f n)) = K.mul c (K.add (ssum K f n) (f n))
    rw [ssum_mul c f n]; lib_ring

theorem ssum_mul_r (c : R) (f : Nat → R) : ∀ n, ssum K (fun i => K.mul (f i) c) n = K.mul (ssum K f n) c
  | 0 => by show K.zero = K.mul K.zero c; lib_ring
  | n + 1 => by
    show K.add (ssum K (fun i => K.mul (f i) c) n) (K.mul (f n) c) = K.mul (K.add (ssum K f n) (f n)) c
    rw [ssum_mul_r c f n]; lib_ring

theorem oddT_zero (x : R) : oddT K x 0 = K.mul (K.add K.one K.one) (K.mul x (K.mul x x)) := by
  unfold oddT
  show K.mul (K.add K.one K.one) (K.mul (pw K x 1)
    (sub K K.one (K.mul (K.mul (K.add (K.add K.zero K.zero) K.one) (K.add (K.add K.zero K.zero) K.one)) (sub K K.one (K.mul x x))))) = _
  show K.mul (K.add K.one K.one) (K.mul (K.mul K.one x)
    (sub K K.one (K.mul (K.mul (K.add (K.add K.zero K.zero) K.one) (K.add (K.add K.zero K.zero) K.one)) (sub K K.one (K.mul x x))))) = _
  unfold sub; lib_ring

theorem altT_zero (x : R) : altT K x 0 = K.neg x := by
  unfold altT
  show K.mul (K.mul K.one (K.neg K.one)) (K.mul (K.mul (K.add K.one K.zero) (K.add K.one K.zero)) (K.mul K.one x)) = _
  lib_ring

/-- **D_0 split.** D0 x M K = x·S_{M+1}(x) + 2x³A_{K+1}(x). -/
theorem D0_eq (x : R) (M Kk : Nat) :
    D0 K x M Kk
      = K.add (K.mul x (ssum K (oddT K x) (M + 1))) (K.mul (K.mul (K.add K.one K.one) (K.mul x (K.mul x x))) (altA K x (Kk + 1))) := by
  unfold D0 altA
  rw [ssum_succ' K (oddT K x) M, ssum_succ' K (altT K x) Kk, oddT_zero, altT_zero,
    ssum_mul K x (fun i => oddT K x (i + 1)),
    ssum_mul K (K.add K.one K.one) (fun j => K.mul (altT K x (j + 1)) (K.mul x (K.mul x x))),
    ssum_mul_r K (K.mul x (K.mul x x)) (fun j => altT K x (j + 1))]
  generalize ssum K (fun i => oddT K x (i + 1)) M = S
  generalize ssum K (fun j => altT K x (j + 1)) Kk = A
  lib_ring

section Ord
variable (O : ORing K)

theorem ssum_ge0 (f : Nat → R) (hf : ∀ i, O.le K.zero (f i)) : ∀ n, O.le (f 0) (ssum K f (n + 1))
  | 0 => by
    show O.le (f 0) (K.add K.zero (f 0))
    have e : K.add K.zero (f 0) = f 0 := by lib_ring
    rw [e]; exact O.le_refl _
  | n + 1 => by
    show O.le (f 0) (K.add (ssum K f (n + 1)) (f (n + 1)))
    have h := O.add_le_add _ _ _ _ (ssum_ge0 f hf n) (hf (n + 1))
    have e : K.add (f 0) K.zero = f 0 := by lib_ring
    rw [e] at h; exact h

/-- **Quantitative lower bound.** 0 ≤ x ≤ 1 ⇒ 2x³(1 − x) + 2(M+1)x^{(2M+2)²} ≤ S_{M+1}(x). -/
theorem oddsum_lower1 (h10 : K.one ≠ K.zero) (x : R) (hx0 : O.le K.zero x) (hx1 : O.le x K.one) (M : Nat) :
    O.le (K.add (K.mul (K.mul (K.add K.one K.one) (K.mul x (K.mul x x))) (sub K K.one x))
        (K.mul (K.add (castN K (M + 1)) (castN K (M + 1))) (pw K x ((2 * (M + 1)) * (2 * (M + 1))))))
      (ssum K (oddT K x) (M + 1)) := by
  rw [tel K x (M + 1)]
  have s := ssum_ge0 K O (fun i => K.mul (pw K x ((2 * i) * (2 * i))) (qpoly K x i)) (q_nonneg K O h10 x hx0 hx1) M
  have e0 : K.mul (pw K x ((2 * 0) * (2 * 0))) (qpoly K x 0)
      = K.mul (K.mul (K.add K.one K.one) (K.mul x (K.mul x x))) (sub K K.one x) := by
    unfold qpoly
    show K.mul K.one (K.add (K.add (K.add (K.add K.zero K.zero) (K.neg (K.mul (K.mul (K.mul (K.add (K.add K.one K.one) (K.add K.one K.one)) (K.add K.one K.one))
      (K.mul K.zero (K.add K.zero K.one))) (pw K x 1)))) (K.mul (K.mul (K.add K.one K.one) (K.mul (K.add (K.add K.zero K.zero) K.one) (K.add (K.add K.zero K.zero) K.one))) (K.mul (K.mul (K.mul K.one x) x) x)))
      (K.neg (K.mul (K.add (K.add K.zero K.zero) (K.add K.one K.one)) (K.mul (K.mul (K.mul (K.mul K.one x) x) x) x)))) = _
    unfold sub; lib_ring
  rw [e0] at s
  exact O.add_le_add _ _ _ _ s (O.le_refl _)

/-- **D_0 lower bound.** 0 ≤ x ≤ 1 ⇒ 2x⁴(1 − x) + 2x³A_{K+1}(x) ≤ D0 x M K. -/
theorem D0_lower (h10 : K.one ≠ K.zero) (x : R) (hx0 : O.le K.zero x) (hx1 : O.le x K.one) (M Kk : Nat) :
    O.le (K.add (K.mul (K.mul (K.add K.one K.one) (K.mul x (K.mul x (K.mul x x)))) (sub K K.one x))
        (K.mul (K.mul (K.add K.one K.one) (K.mul x (K.mul x x))) (altA K x (Kk + 1))))
      (D0 K x M Kk) := by
  rw [D0_eq K x M Kk]
  have h := oddsum_lower1 K O h10 x hx0 hx1 M
  have hb : O.le K.zero (K.mul (K.add (castN K (M + 1)) (castN K (M + 1))) (pw K x ((2 * (M + 1)) * (2 * (M + 1))))) :=
    O.mul_nonneg _ _ (add_nn K O (cast_nn K O _) (cast_nn K O _)) (LibChebPow.pw_nonneg K O x hx0 _)
  have h' := O.le_trans _ _ _ (by
      have t := O.add_le_add _ _ _ _ (O.le_refl (K.mul (K.mul (K.add K.one K.one) (K.mul x (K.mul x x))) (sub K K.one x))) hb
      have e : K.add (K.mul (K.mul (K.add K.one K.one) (K.mul x (K.mul x x))) (sub K K.one x)) K.zero
          = K.mul (K.mul (K.add K.one K.one) (K.mul x (K.mul x x))) (sub K K.one x) := by lib_ring
      rw [e] at t; exact t) h
  have hx := O.mul_le_mul_right _ _ _ h' hx0
  have t := O.add_le_add _ _ _ _ hx (O.le_refl (K.mul (K.mul (K.add K.one K.one) (K.mul x (K.mul x x))) (altA K x (Kk + 1))))
  have e1 : K.mul (K.mul (K.mul (K.add K.one K.one) (K.mul x (K.mul x x))) (sub K K.one x)) x
      = K.mul (K.mul (K.add K.one K.one) (K.mul x (K.mul x (K.mul x x)))) (sub K K.one x) := by lib_ring
  have e2 : K.mul (ssum K (oddT K x) (M + 1)) x = K.mul x (ssum K (oddT K x) (M + 1)) := K.mul_comm _ _
  rw [e1, e2] at t; exact t

end Ord

end LibThetaD0

#print axioms LibThetaD0.D0_eq
#print axioms LibThetaD0.oddsum_lower1
#print axioms LibThetaD0.D0_lower

lib/ThetaFullTel.lean

lean_all/lib/ThetaFullTel.lean

/-
  lib/ThetaFullTel.lean — T46 shared library (G1545, agent3 T45 6f-A′-1, full-ℤ form): every pointwise polynomial
  Q_k is nonnegative, and the full Gaussian variance sum telescopes to a nonnegative remainder plus 1 − x.

    qk x j        Q_{j+1}(x) = j − 2(j² + 2j)x^{2j+1} + 2(j+1)²x^{2j+3} − (j+2)x^{4j+4}   (k = j + 1)
    fT x j        2 x^{k²}(1 − k²(1 − x²)),  k = j + 1
    tel_full      Σ_{j<N} fT x j = Σ_{j<N} x^{j²} Q_{j+1}(x) + (N+1)x^{(N+1)²} + N x^{N²} − x      (any ring)
    upg           0 ≤ x³Q, Q + x³P = c, 1 ≤ c  ⇒  0 ≤ Q                    (removes the x³ of `core`)
    q2, q4, q6    0 ≤ x³ Q_k for k = 2, 4, 2m + 6   (core + certificates; odd k: ThetaQPos.q3, q5)
    qk_nonneg     0 ≤ x ≤ 1 ⇒ 0 ≤ Q_k(x) for every k ≥ 1
    fullsum_lower 0 ≤ x ≤ 1 ⇒ (N+1)x^{(N+1)²} + N x^{N²} − x ≤ Σ_{k=1}^{N} 2x^{k²}(1 − k²(1 − x²)),
                  i.e. Σ_{|c| ≤ N} x^{c²}(1 − c²(1 − x²)) ≥ 1 − x + (N+1)x^{(N+1)²} + N x^{N²}
  Lean 4 core only, proofs by hand, no axioms.
-/
import lib.ThetaQPos

namespace LibThetaFullTel
open DetChain LibRing YMChain LibBinomTrunc3 LibThetaOddTel LibThetaQPos

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

def qk (x : R) (j : Nat) : R :=
  K.add (K.add (K.add (castN K j)
    (K.neg (K.mul (K.mul (K.add K.one K.one) (K.add (K.mul (castN K j) (castN K j)) (K.add (castN K j) (castN K j)))) (pw K x (2 * j + 1)))))
    (K.mul (K.mul (K.add K.one K.one) (K.mul (K.add (castN K j) K.one) (K.add (castN K j) K.one))) (pw K x (2 * j + 3))))
    (K.neg (K.mul (K.add (castN K j) (K.add K.one K.one)) (pw K x (4 * j + 4))))

def fT (x : R) (j : Nat) : R :=
  K.mul (K.add K.one K.one) (K.mul (pw K x ((j + 1) * (j + 1)))
    (sub K K.one (K.mul (K.mul (castN K (j + 1)) (castN K (j + 1))) (sub K K.one (K.mul x x)))))

theorem e_a (j : Nat) : (j + 1) * (j + 1) = j * j + (2 * j + 1) := by lib_nat
theorem e_b (j : Nat) : (j + 1 + 1) * (j + 1 + 1) = j * j + ((2 * j + 1) + (2 * j + 1) + 2) := by lib_nat
theorem e_c (j : Nat) : 4 * j + 4 = (2 * j + 1) + (2 * j + 1) + 2 := by lib_nat

theorem stepF (x : R) (j : Nat) :
    K.add (fT K x j) (sub K (K.add (K.mul (castN K (j + 1)) (pw K x ((j + 1) * (j + 1)))) (K.mul (castN K j) (pw K x (j * j)))) x)
      = K.add (K.mul (pw K x (j * j)) (qk K x j))
          (sub K (K.add (K.mul (castN K (j + 1 + 1)) (pw K x ((j + 1 + 1) * (j + 1 + 1)))) (K.mul (castN K (j + 1)) (pw K x ((j + 1) * (j + 1))))) x) := by
  unfold fT qk
  rw [e_b, e_a, e_c, LibChebPow.pw_add K x (j * j) (2 * j + 1),
    LibChebPow.pw_add K x (j * j) ((2 * j + 1) + (2 * j + 1) + 2),
    show pw K x ((2 * j + 1) + (2 * j + 1) + 2) = K.mul (K.mul (pw K x ((2 * j + 1) + (2 * j + 1))) x) x from rfl,
    LibChebPow.pw_add K x (2 * j + 1) (2 * j + 1),
    show pw K x (2 * j + 3) = K.mul (K.mul (pw K x (2 * j + 1)) x) x from rfl,
    show castN K (j + 1 + 1) = K.add K.one (K.add K.one (castN K j)) from rfl,
    show castN K (j + 1) = K.add K.one (castN K j) from rfl]
  unfold sub; lib_ring

/-- **Full telescoping.** Σ_{j<N} fT x j = Σ_{j<N} x^{j²}Q_{j+1}(x) + (N+1)x^{(N+1)²} + N x^{N²} − x. -/
theorem tel_full (x : R) : ∀ N, ssum K (fT K x) N
    = K.add (ssum K (fun j => K.mul (pw K x (j * j)) (qk K x j)) N)
        (sub K (K.add (K.mul (castN K (N + 1)) (pw K x ((N + 1) * (N + 1)))) (K.mul (castN K N) (pw K x (N * N)))) x)
  | 0 => by
    show K.zero = K.add K.zero (sub K (K.add (K.mul (K.add K.one K.zero) (K.mul K.one x)) (K.mul K.zero K.one)) x)
    unfold sub; lib_ring
  | N + 1 => by
    show K.add (ssum K (fT K x) N) (fT K x N) = K.add (K.add (ssum K (fun j => K.mul (pw K x (j * j)) (qk K x j)) N)
        (K.mul (pw K x (N * N)) (qk K x N))) _
    rw [tel_full x N]
    have e1 : K.add (K.add (ssum K (fun j => K.mul (pw K x (j * j)) (qk K x j)) N)
          (sub K (K.add (K.mul (castN K (N + 1)) (pw K x ((N + 1) * (N + 1)))) (K.mul (castN K N) (pw K x (N * N)))) x)) (fT K x N)
        = K.add (ssum K (fun j => K.mul (pw K x (j * j)) (qk K x j)) N)
          (K.add (fT K x N) (sub K (K.add (K.mul (castN K (N + 1)) (pw K x ((N + 1) * (N + 1)))) (K.mul (castN K N) (pw K x (N * N)))) x)) := by
      unfold sub; lib_ring
    rw [e1, stepF K x N]
    unfold sub; lib_ring

/-! ### even k: certificates -/

def eB (cm : R) : R := (K.add (castN K 4320) (K.mul cm (K.add (castN K 3168) (K.mul cm (castN K 576)))))
def eC (cm : R) : R := (K.add (castN K 71280) (K.mul cm (K.add (castN K 109872) (K.mul cm (K.add (castN K 63264) (K.mul cm (K.add (castN K 16128) (K.mul cm (castN K 1536)))))))))
def eE (cm : R) : R := (K.add (castN K 1866240) (K.mul cm (K.add (castN K 4271616) (K.mul cm (K.add (castN K 3207168) (K.mul cm (K.add (castN K 995328) (K.mul cm (castN K 110592)))))))))
def e4 (cm : R) : R := (K.add (castN K 823680) (K.mul cm (K.add (castN K 1465872) (K.mul cm (K.add (castN K 1041360) (K.mul cm (K.add (castN K 369120) (K.mul cm (K.add (castN K 65280) (K.mul cm (castN K 4608)))))))))))
def e5 (cm : R) : R := (K.add (castN K 5524200) (K.mul cm (K.add (castN K 12064560) (K.mul cm (K.add (castN K 10950432) (K.mul cm (K.add (castN K 5285952) (K.mul cm (K.add (castN K 1430784) (K.mul cm (K.add (castN K 205824) (K.mul cm (castN K 12288)))))))))))))
def e6 (cm : R) : R := (K.add (castN K 12632400) (K.mul cm (K.add (castN K 31635120) (K.mul cm (K.add (castN K 33762944) (K.mul cm (K.add (castN K 19894592) (K.mul cm (K.add (castN K 6984704) (K.mul cm (K.add (castN K 1459712) (K.mul cm (K.add (castN K 167936) (K.mul cm (castN K 8192)))))))))))))))
def e7 (cm : R) : R := (K.add (castN K 13068000) (K.mul cm (K.add (castN K 35349600) (K.mul cm (K.add (castN K 40930240) (K.mul cm (K.add (castN K 26296192) (K.mul cm (K.add (castN K 10124032) (K.mul cm (K.add (castN K 2335744) (K.mul cm (K.add (castN K 299008) (K.mul cm (castN K 16384)))))))))))))))
def e8 (cm : R) : R := (K.add (castN K 4900500) (K.mul cm (K.add (castN K 13800600) (K.mul cm (K.add (castN K 16640240) (K.mul cm (K.add (castN K 11136032) (K.mul cm (K.add (castN K 4467200) (K.mul cm (K.add (castN K 1074176) (K.mul cm (K.add (castN K 143360) (K.mul cm (castN K 8192)))))))))))))))

def nn6 (cm : R) : R := K.add (K.mul (castN K 4) cm) (castN K 11)
def DD6 (cm x v : R) : R :=
  K.add (K.add (K.add (K.mul (castN K 6) (K.mul x (K.mul x x)))
    (K.mul (K.mul (castN K 6) (nn6 K cm)) (K.mul (K.mul x x) v)))
    (K.mul (K.mul (castN K 3) (K.mul (nn6 K cm) (K.add (K.mul (castN K 4) cm) (castN K 10)))) (K.mul x (K.mul v v))))
    (K.mul (K.mul (nn6 K cm) (K.mul (K.add (K.mul (castN K 4) cm) (castN K 10)) (K.add (K.mul (castN K 4) cm) (castN K 9)))) (K.mul v (K.mul v v)))
def al6 (cm : R) : R := K.add (K.mul (castN K 2) cm) (castN K 5)
def kk6 (cm : R) : R := K.add (K.mul (castN K 2) cm) (castN K 6)
def be6 (cm x : R) : R := sub K (K.mul (castN K 2) (sub K (K.mul (kk6 K cm) (kk6 K cm)) K.one)) (K.mul (K.mul (castN K 2) (K.mul (kk6 K cm) (kk6 K cm))) (K.mul x x))
def ga6 (cm x : R) : R := K.mul (K.add (K.mul (castN K 2) cm) (castN K 7)) (K.mul x x)

theorem cert6 (cm x : R) :
    K.mul (K.mul (castN K 4) (eC K cm))
      (sub K (sub K (K.mul (al6 K cm) (K.mul (DD6 K cm x (sub K K.one x)) (DD6 K cm x (sub K K.one x))))
        (K.mul (be6 K cm x) (K.mul (K.mul (castN K 6) (K.mul x (K.mul x x))) (DD6 K cm x (sub K K.one x)))))
        (K.mul (ga6 K cm x) (K.mul (K.mul (castN K 6) (K.mul x (K.mul x x))) (K.mul (castN K 6) (K.mul x (K.mul x x))))))
    = K.add (K.mul (K.mul (pw K x 5) (sub K K.one x))
        (K.add (K.mul (sub K (K.mul (K.mul (castN K 2) (eC K cm)) (sub K K.one x)) (K.mul (eB K cm) x))
                      (sub K (K.mul (K.mul (castN K 2) (eC K cm)) (sub K K.one x)) (K.mul (eB K cm) x)))
               (K.mul (eE K cm) (K.mul x x))))
      (K.mul (K.mul (castN K 4) (eC K cm))
        (K.add (K.add (K.add (K.add (K.mul (e4 K cm) (K.mul (pw K x 4) (pw K (sub K K.one x) 4)))
          (K.mul (e5 K cm) (K.mul (pw K x 3) (pw K (sub K K.one x) 5))))
          (K.mul (e6 K cm) (K.mul (pw K x 2) (pw K (sub K K.one x) 6))))
          (K.mul (e7 K cm) (K.mul x (pw K (sub K K.one x) 7))))
          (K.mul (e8 K cm) (pw K (sub K K.one x) 8)))) := by
  unfold eC eB eE e4 e5 e6 e7 e8 DD6 nn6 al6 be6 ga6 kk6 sub
  lib_ringz

theorem castN_4m8 (m : Nat) : castN K (4 * m + 8) = K.add (K.mul (castN K 4) (castN K m)) (castN K 8) := by
  rw [LibRingZ.castN_add', LibRingZ.castN_mul']

theorem D6_DD6 (m : Nat) (x v : R) : D6 K (4 * m + 8) x v = DD6 K (castN K m) x v := by
  unfold D6 DD6 nn6 six three
  rw [show castN K (4 * m + 8 + 3) = K.add K.one (K.add K.one (K.add K.one (castN K (4 * m + 8)))) from rfl,
    show castN K (4 * m + 8 + 2) = K.add K.one (K.add K.one (castN K (4 * m + 8))) from rfl,
    show castN K (4 * m + 8 + 1) = K.add K.one (castN K (4 * m + 8)) from rfl, castN_4m8]
  lib_ringz

/-- k = 4 (M = 4, N = 7): 64·hq = x⁴v[1152x(2x − 25v)² + 72v(8x − 65v)² + 5501880v³] + 64 Σ_{j≥5} h_j x^{8−j}v^j. -/
theorem cert4 (x : R) :
    K.mul (castN K 64) (hqE K 4 (castN K 3) (sub K (castN K 30) (K.mul (castN K 32) (K.mul x x))) (K.mul (castN K 5) (K.mul x x)) x)
    = K.add (K.mul (K.mul (pw K x 4) (sub K K.one x))
        (K.add (K.add (K.mul (K.mul (castN K 1152) x) (K.mul (sub K (K.mul (castN K 2) x) (K.mul (castN K 25) (sub K K.one x)))
                                                       (sub K (K.mul (castN K 2) x) (K.mul (castN K 25) (sub K K.one x)))))
          (K.mul (K.mul (castN K 72) (sub K K.one x)) (K.mul (sub K (K.mul (castN K 8) x) (K.mul (castN K 65) (sub K K.one x)))
                                                       (sub K (K.mul (castN K 8) x) (K.mul (castN K 65) (sub K K.one x))))))
          (K.mul (castN K 5501880) (pw K (sub K K.one x) 3))))
      (K.mul (castN K 64)
        (K.add (K.add (K.add (K.mul (castN K 361368) (K.mul (pw K x 3) (pw K (sub K K.one x) 5)))
          (K.mul (castN K 550368) (K.mul (pw K x 2) (pw K (sub K K.one x) 6))))
          (K.mul (castN K 423360) (K.mul x (pw K (sub K K.one x) 7))))
          (K.mul (castN K 132300) (pw K (sub K K.one x) 8)))) := by
  unfold hqE D6 six three
  rw [show castN K (4 + 3) = castN K 7 from rfl, show castN K (4 + 2) = castN K 6 from rfl, show castN K (4 + 1) = castN K 5 from rfl]
  unfold sub
  lib_ringz

/-- k = 2 (M = 0, N = 3): 64·hq = x⁴v[1152x(2x − 5v)² + 72v(8x − 17v)² + 71352v³] + 64 Σ_{j≥5} h_j x^{8−j}v^j. -/
theorem cert2 (x : R) :
    K.mul (castN K 64) (hqE K 0 (castN K 1) (sub K (castN K 6) (K.mul (castN K 8) (K.mul x x))) (K.mul (castN K 3) (K.mul x x)) x)
    = K.add (K.mul (K.mul (pw K x 4) (sub K K.one x))
        (K.add (K.add (K.mul (K.mul (castN K 1152) x) (K.mul (sub K (K.mul (castN K 2) x) (K.mul (castN K 5) (sub K K.one x)))
                                                       (sub K (K.mul (castN K 2) x) (K.mul (castN K 5) (sub K K.one x)))))
          (K.mul (K.mul (castN K 72) (sub K K.one x)) (K.mul (sub K (K.mul (castN K 8) x) (K.mul (castN K 17) (sub K K.one x)))
                                                       (sub K (K.mul (castN K 8) x) (K.mul (castN K 17) (sub K K.one x))))))
          (K.mul (castN K 71352) (pw K (sub K K.one x) 3))))
      (K.mul (castN K 64)
        (K.add (K.add (K.add (K.mul (castN K 1800) (K.mul (pw K x 3) (pw K (sub K K.one x) 5)))
          (K.mul (castN K 1008) (K.mul (pw K x 2) (pw K (sub K K.one x) 6))))
          (K.mul (castN K 288) (K.mul x (pw K (sub K K.one x) 7))))
          (K.mul (castN K 36) (pw K (sub K K.one x) 8)))) := by
  unfold hqE D6 six three
  rw [show castN K (0 + 3) = castN K 3 from rfl, show castN K (0 + 2) = castN K 2 from rfl, show castN K (0 + 1) = castN K 1 from rfl]
  unfold sub
  lib_ringz

section Ord
variable (O : ORing K)

theorem upg (h10 : K.one ≠ K.zero) (x Q P c : R) (hx0 : O.le K.zero x) (h : O.le K.zero (K.mul (K.mul x (K.mul x x)) Q))
    (hQ : K.add Q (K.mul (K.mul x (K.mul x x)) P) = c) (hc : O.le K.one c) : O.le K.zero Q := by
  cases O.le_total K.zero Q with
  | inl h' => exact h'
  | inr h' =>
    have hx3 : O.le K.zero (K.mul x (K.mul x x)) := mul_nn K O hx0 (mul_nn K O hx0 hx0)
    have hne : K.mul x (K.mul x x) ≠ K.zero := fun e => by
      rw [e] at hQ
      have e2 : K.add Q (K.mul K.zero P) = Q := by lib_ring
      rw [e2] at hQ
      rw [hQ] at h'
      exact h10 (O.le_antisymm _ _ (O.le_trans _ _ _ hc h') (LibLCHeat.zero_le_one K O))
    apply O.pos_cancel _ _ _ hx3 hne
    have e : K.mul K.zero (K.mul x (K.mul x x)) = K.zero := by lib_ring
    rw [e, K.mul_comm]; exact h

theorem q6 (h10 : K.one ≠ K.zero) (m : Nat) (x : R) (hx0 : O.le K.zero x) (hx1 : O.le x K.one) :
    O.le K.zero (K.mul (K.mul x (K.mul x x)) (qk K x (2 * m + 5))) := by
  have hv := sub_nn K O x hx1
  have hcm := cast_nn K O m
  have hq : O.le K.zero (hqE K (4 * m + 8) (al6 K (castN K m)) (be6 K (castN K m) x) (ga6 K (castN K m) x) x) := by
    have hc : O.le K.one (K.mul (castN K 4) (eC K (castN K m))) := by
      have e : K.mul (castN K 4) (eC K (castN K m)) = K.add K.one (K.add (castN K 285119)
          (K.mul (castN K 4) (K.mul (castN K m) (K.add (castN K 109872) (K.mul (castN K m) (K.add (castN K 63264) (K.mul (castN K m) (K.add (castN K 16128) (K.mul (castN K m) (castN K 1536)))))))))) := by
        unfold eC; lib_ringz
      rw [e]
      apply one_le_add K O
      repeat' (first | exact hcm | exact cast_nn K O _ | apply add_nn K O | apply mul_nn K O)
    apply cancel_r K O h10 _ _ hc
    have e : K.mul (hqE K (4 * m + 8) (al6 K (castN K m)) (be6 K (castN K m) x) (ga6 K (castN K m) x) x)
          (K.mul (castN K 4) (eC K (castN K m)))
        = K.mul (K.mul (castN K 4) (eC K (castN K m)))
          (sub K (sub K (K.mul (al6 K (castN K m)) (K.mul (DD6 K (castN K m) x (sub K K.one x)) (DD6 K (castN K m) x (sub K K.one x))))
            (K.mul (be6 K (castN K m) x) (K.mul (K.mul (castN K 6) (K.mul x (K.mul x x))) (DD6 K (castN K m) x (sub K K.one x)))))
            (K.mul (ga6 K (castN K m) x) (K.mul (K.mul (castN K 6) (K.mul x (K.mul x x))) (K.mul (castN K 6) (K.mul x (K.mul x x)))))) := by
      unfold hqE
      rw [D6_DD6]
      unfold six
      lib_ringz
    rw [e, cert6]
    repeat' (first | exact hx0 | exact hv | exact hcm | exact cast_nn K O _ | exact pw_nn' K O x hx0 _ | exact pw_nn' K O _ hv _ | exact sq_nn K O _ | apply add_nn K O | apply mul_nn K O)
  have hα : O.le K.zero (al6 K (castN K m)) := by
    unfold al6; repeat' (first | exact hcm | exact cast_nn K O _ | apply add_nn K O | apply mul_nn K O)
  have hγ : O.le K.zero (ga6 K (castN K m) x) := by
    unfold ga6; repeat' (first | exact hx0 | exact hcm | exact cast_nn K O _ | apply add_nn K O | apply mul_nn K O)
  have c := core K O h10 (4 * m + 8) _ _ _ x hα hγ hx0 hx1 hq
  have e : K.mul (K.mul x (K.mul x x)) (qk K x (2 * m + 5))
      = K.mul (K.mul x (K.mul x x)) (sub K (sub K (al6 K (castN K m)) (K.mul (be6 K (castN K m) x) (pw K x (4 * m + 8 + 3))))
          (K.mul (ga6 K (castN K m) x) (K.mul (pw K x (4 * m + 8 + 3)) (pw K x (4 * m + 8 + 3))))) := by
    unfold qk al6 be6 ga6 kk6
    rw [show 2 * (2 * m + 5) + 1 = 4 * m + 8 + 3 by lib_nat, show 2 * (2 * m + 5) + 3 = (4 * m + 8 + 3) + 2 by lib_nat,
      show 4 * (2 * m + 5) + 4 = ((4 * m + 8 + 3) + (4 * m + 8 + 3)) + 2 by lib_nat,
      show pw K x ((4 * m + 8 + 3) + 2) = K.mul (K.mul (pw K x (4 * m + 8 + 3)) x) x from rfl,
      show pw K x ((4 * m + 8 + 3) + (4 * m + 8 + 3) + 2) = K.mul (K.mul (pw K x ((4 * m + 8 + 3) + (4 * m + 8 + 3))) x) x from rfl,
      LibChebPow.pw_add K x (4 * m + 8 + 3) (4 * m + 8 + 3),
      show castN K (2 * m + 5) = K.add (K.mul (castN K 2) (castN K m)) (castN K 5) by rw [LibRingZ.castN_add', LibRingZ.castN_mul']]
    unfold sub
    lib_ringz
  rw [e]; exact c

theorem q4 (h10 : K.one ≠ K.zero) (x : R) (hx0 : O.le K.zero x) (hx1 : O.le x K.one) :
    O.le K.zero (K.mul (K.mul x (K.mul x x)) (qk K x 3)) := by
  have hv := sub_nn K O x hx1
  have hq : O.le K.zero (hqE K 4 (castN K 3) (sub K (castN K 30) (K.mul (castN K 32) (K.mul x x))) (K.mul (castN K 5) (K.mul x x)) x) := by
    apply cancel_r K O h10 _ (castN K 64) (one_le_cast K O 63)
    rw [K.mul_comm, cert4]
    repeat' (first | exact hx0 | exact hv | exact cast_nn K O _ | exact pw_nn' K O x hx0 _ | exact pw_nn' K O _ hv _ | exact sq_nn K O _ | apply add_nn K O | apply mul_nn K O)
  have c := core K O h10 4 _ _ _ x (cast_nn K O 3) (mul_nn K O (cast_nn K O 5) (mul_nn K O hx0 hx0)) hx0 hx1 hq
  have e : K.mul (K.mul x (K.mul x x)) (qk K x 3)
      = K.mul (K.mul x (K.mul x x)) (sub K (sub K (castN K 3) (K.mul (sub K (castN K 30) (K.mul (castN K 32) (K.mul x x))) (pw K x (4 + 3))))
          (K.mul (K.mul (castN K 5) (K.mul x x)) (K.mul (pw K x (4 + 3)) (pw K x (4 + 3))))) := by
    unfold qk
    rw [show pw K x (2 * 3 + 3) = K.mul (K.mul (pw K x 7) x) x from rfl,
      show pw K x (4 * 3 + 4) = K.mul (K.mul (pw K x (7 + 7)) x) x from rfl,
      LibChebPow.pw_add K x 7 7]
    show K.mul (K.mul x (K.mul x x)) (K.add (K.add (K.add (castN K 3) (K.neg (K.mul (K.mul (K.add K.one K.one) (K.add (K.mul (castN K 3) (castN K 3)) (K.add (castN K 3) (castN K 3)))) (pw K x 7))))
      (K.mul (K.mul (K.add K.one K.one) (K.mul (K.add (castN K 3) K.one) (K.add (castN K 3) K.one))) (K.mul (K.mul (pw K x 7) x) x)))
      (K.neg (K.mul (K.add (castN K 3) (K.add K.one K.one)) (K.mul (K.mul (K.mul (pw K x 7) (pw K x 7)) x) x)))) = _
    unfold sub
    lib_ringz
  rw [e]; exact c

theorem q2 (h10 : K.one ≠ K.zero) (x : R) (hx0 : O.le K.zero x) (hx1 : O.le x K.one) :
    O.le K.zero (K.mul (K.mul x (K.mul x x)) (qk K x 1)) := by
  have hv := sub_nn K O x hx1
  have hq : O.le K.zero (hqE K 0 (castN K 1) (sub K (castN K 6) (K.mul (castN K 8) (K.mul x x))) (K.mul (castN K 3) (K.mul x x)) x) := by
    apply cancel_r K O h10 _ (castN K 64) (one_le_cast K O 63)
    rw [K.mul_comm, cert2]
    repeat' (first | exact hx0 | exact hv | exact cast_nn K O _ | exact pw_nn' K O x hx0 _ | exact pw_nn' K O _ hv _ | exact sq_nn K O _ | apply add_nn K O | apply mul_nn K O)
  have c := core K O h10 0 _ _ _ x (cast_nn K O 1) (mul_nn K O (cast_nn K O 3) (mul_nn K O hx0 hx0)) hx0 hx1 hq
  have e : K.mul (K.mul x (K.mul x x)) (qk K x 1)
      = K.mul (K.mul x (K.mul x x)) (sub K (sub K (castN K 1) (K.mul (sub K (castN K 6) (K.mul (castN K 8) (K.mul x x))) (pw K x (0 + 3))))
          (K.mul (K.mul (castN K 3) (K.mul x x)) (K.mul (pw K x (0 + 3)) (pw K x (0 + 3))))) := by
    unfold qk
    rw [show pw K x (2 * 1 + 3) = K.mul (K.mul (pw K x 3) x) x from rfl,
      show pw K x (4 * 1 + 4) = K.mul (K.mul (pw K x (3 + 3)) x) x from rfl,
      LibChebPow.pw_add K x 3 3]
    show K.mul (K.mul x (K.mul x x)) (K.add (K.add (K.add (castN K 1) (K.neg (K.mul (K.mul (K.add K.one K.one) (K.add (K.mul (castN K 1) (castN K 1)) (K.add (castN K 1) (castN K 1)))) (pw K x 3))))
      (K.mul (K.mul (K.add K.one K.one) (K.mul (K.add (castN K 1) K.one) (K.add (castN K 1) K.one))) (K.mul (K.mul (pw K x 3) x) x)))
      (K.neg (K.mul (K.add (castN K 1) (K.add K.one K.one)) (K.mul (K.mul (K.mul (pw K x 3) (pw K x 3)) x) x)))) = _
    unfold sub
    lib_ringz
  rw [e]; exact c

theorem qk_odd (x : R) (i : Nat) : qk K x (2 * i) = qpoly K x i := by
  unfold qk qpoly
  rw [show 2 * (2 * i) + 1 = 4 * i + 1 by lib_nat, show 2 * (2 * i) + 3 = 4 * i + 3 by lib_nat,
    show 4 * (2 * i) + 4 = 8 * i + 4 by lib_nat,
    show castN K (2 * i) = K.add (castN K i) (castN K i) by rw [show 2 * i = i + i by lib_nat, LibRingZ.castN_add']]
  lib_ringz

theorem par2 : ∀ j : Nat, (∃ i, j = 2 * i) ∨ (∃ i, j = 2 * i + 1)
  | 0 => Or.inl ⟨0, rfl⟩
  | j + 1 => by
    rcases par2 j with ⟨i, hi⟩ | ⟨i, hi⟩
    · exact Or.inr ⟨i, by rw [hi]⟩
    · exact Or.inl ⟨i + 1, by rw [hi]; lib_nat⟩

/-- 0 ≤ x³Q_k for every k = j + 1 ≥ 2. -/
theorem qk3 (h10 : K.one ≠ K.zero) (x : R) (hx0 : O.le K.zero x) (hx1 : O.le x K.one) (j : Nat) :
    O.le K.zero (K.mul (K.mul x (K.mul x x)) (qk K x (j + 1))) := by
  rcases par2 (j + 1) with ⟨i, hi⟩ | ⟨i, hi⟩
  · rw [hi, qk_odd]
    match i, hi with
    | 0, hi => exact Nat.noConfusion hi
    | 1, _ => exact q3 K O h10 x hx0 hx1
    | m + 2, _ => exact q5 K O h10 m x hx0 hx1
  · rw [hi]
    match i with
    | 0 => exact q2 K O h10 x hx0 hx1
    | 1 => exact q4 K O h10 x hx0 hx1
    | m + 2 => rw [show 2 * (m + 2) + 1 = 2 * m + 5 by lib_nat]; exact q6 K O h10 m x hx0 hx1

/-- **Every Q_k is nonnegative on [0, 1].** -/
theorem qk_nonneg (h10 : K.one ≠ K.zero) (x : R) (hx0 : O.le K.zero x) (hx1 : O.le x K.one) :
    ∀ j, O.le K.zero (qk K x j)
  | 0 => by
    have e : qk K x 0 = K.mul (K.mul (castN K 2) (K.mul x (K.mul x x))) (sub K K.one x) := by
      unfold qk
      rw [show pw K x (2 * 0 + 1) = K.mul K.one x from rfl,
        show pw K x (2 * 0 + 3) = K.mul (K.mul (K.mul K.one x) x) x from rfl,
        show pw K x (4 * 0 + 4) = K.mul (K.mul (K.mul (K.mul K.one x) x) x) x from rfl,
        show castN K 0 = K.zero from rfl]
      unfold sub; lib_ringz
    rw [e]
    exact mul_nn K O (mul_nn K O (cast_nn K O 2) (mul_nn K O hx0 (mul_nn K O hx0 hx0))) (sub_nn K O x hx1)
  | j + 1 => by
    apply upg K O h10 x (qk K x (j + 1))
      (K.add (K.mul (K.mul (K.add K.one K.one) (K.add (K.mul (castN K (j + 1)) (castN K (j + 1))) (K.add (castN K (j + 1)) (castN K (j + 1))))) (pw K x (2 * j)))
        (K.add (K.neg (K.mul (K.mul (K.add K.one K.one) (K.mul (K.add (castN K (j + 1)) K.one) (K.add (castN K (j + 1)) K.one))) (pw K x (2 * j + 2))))
          (K.mul (K.add (castN K (j + 1)) (K.add K.one K.one)) (pw K x (4 * j + 5)))))
      (castN K (j + 1)) hx0 (qk3 K O h10 x hx0 hx1 j) _ (one_le_cast K O j)
    unfold qk
    rw [show 2 * (j + 1) + 1 = 2 * j + 3 by lib_nat, show 2 * (j + 1) + 3 = (2 * j + 2) + 3 by lib_nat,
      show 4 * (j + 1) + 4 = (4 * j + 5) + 3 by lib_nat,
      show pw K x (2 * j + 3) = K.mul (K.mul (K.mul (pw K x (2 * j)) x) x) x from rfl,
      show pw K x ((2 * j + 2) + 3) = K.mul (K.mul (K.mul (pw K x (2 * j + 2)) x) x) x from rfl,
      show pw K x ((4 * j + 5) + 3) = K.mul (K.mul (K.mul (pw K x (4 * j + 5)) x) x) x from rfl]
    lib_ring

/-- **Full Gaussian variance sum.** 0 ≤ x ≤ 1 ⇒ (N+1)x^{(N+1)²} + N x^{N²} − x ≤ Σ_{k=1}^{N} 2x^{k²}(1 − k²(1 − x²)). -/
theorem fullsum_lower (h10 : K.one ≠ K.zero) (x : R) (hx0 : O.le K.zero x) (hx1 : O.le x K.one) (N : Nat) :
    O.le (sub K (K.add (K.mul (castN K (N + 1)) (pw K x ((N + 1) * (N + 1)))) (K.mul (castN K N) (pw K x (N * N)))) x)
      (ssum K (fT K x) N) := by
  rw [tel_full K x N]
  have s := ssum_nn K O (fun j => K.mul (pw K x (j * j)) (qk K x j)) N
    (fun j _ => mul_nn K O (pw_nn' K O x hx0 _) (qk_nonneg K O h10 x hx0 hx1 j)) N (Nat.le_refl N)
  have h := O.add_le_add _ _ _ _ s (O.le_refl (sub K (K.add (K.mul (castN K (N + 1)) (pw K x ((N + 1) * (N + 1)))) (K.mul (castN K N) (pw K x (N * N)))) x))
  have e : K.add K.zero (sub K (K.add (K.mul (castN K (N + 1)) (pw K x ((N + 1) * (N + 1)))) (K.mul (castN K N) (pw K x (N * N)))) x)
      = sub K (K.add (K.mul (castN K (N + 1)) (pw K x ((N + 1) * (N + 1)))) (K.mul (castN K N) (pw K x (N * N)))) x := by lib_ring
  rw [e] at h; exact h

end Ord

end LibThetaFullTel

#print axioms LibThetaFullTel.tel_full
#print axioms LibThetaFullTel.cert6
#print axioms LibThetaFullTel.qk_nonneg
#print axioms LibThetaFullTel.fullsum_lower

lib/ThetaGenus2.lean

lean_all/lib/ThetaGenus2.lean

/-
  lib/ThetaGenus2.lean — T46 shared library (G1558): the genus-2 theta framework.  For two binary forms F₁, F₂
  (a class group of order 2 with F₁ principal) and characters χ (the genus field character, = χ_D) and ψ₁, ψ₂
  (the genus characters, ψ₁ψ₂ = χ), local data at every prime p gives

      r₁(n) + r₂(n) = 2 Σ_{d ∣ n} χ(d),      r₁(n) − r₂(n) = 2 Σ_{d ∣ n} ψ₁(d) ψ₂(n/d)      (n > 0).

    LocalData       (Prop) per prime: split11 (FPair F₁F₁, F₂F₂), split12 (FPair F₁F₂, F₂F₁), inert, ramSame, ramSwap
    eigR, eigD      T_p (r₁ ± r₂) = eigenvalue · (r₁ ± r₂)
    theta_sum       r₁ + r₂ = 2E(χ, 1)
    theta_diff      r₁ − r₂ = 2E(ψ₁, ψ₂)
    r1_formula      2r₁ = 2E(χ, 1) + 2E(ψ₁, ψ₂)
  Instances: Theta20/Theta20D (−20) and Theta24/Theta24D (−24) are this pattern written out.
  Lean 4 core only, proofs by hand, no axioms.
-/
import lib.Theta20D

namespace LibThetaGenus2
open LibMod LibGcd LibIntBox LibNormPiA LibNormPiC LibFormCount LibFormRel LibFormRam

variable (F1 F2 : Int → Int → Int) (chi ψ1 ψ2 : Nat → Int)

/-- The local behaviour of the pair (F₁, F₂) at a prime p, with the character values it forces. -/
inductive LocalData (p : Nat) : Prop where
  | split11 (P11 : Nonempty (FPair F1 F1 p)) (P22 : Nonempty (FPair F2 F2 p)) (hc : chi p = 1) (hm : ψ1 p * ψ2 p = 1) (hs : ψ1 p + ψ2 p = 2)
  | split12 (P12 : Nonempty (FPair F1 F2 p)) (P21 : Nonempty (FPair F2 F1 p)) (hc : chi p = 1) (hm : ψ1 p * ψ2 p = 1) (hs : ψ1 p + ψ2 p = -2)
  | inert (h1 : ∀ x y : Int, IDv (p : Int) (F1 x y) → IDv (p : Int) x ∧ IDv (p : Int) y)
      (h2 : ∀ x y : Int, IDv (p : Int) (F2 x y) → IDv (p : Int) x ∧ IDv (p : Int) y)
      (hc : chi p = -1) (hm : ψ1 p * ψ2 p = -1) (hs : ψ1 p + ψ2 p = 0)
  | ramSame (hr : ∀ n, RF F1 (p * n) = RF F1 n ∧ RF F2 (p * n) = RF F2 n)
      (hc : chi p = 0) (hm : ψ1 p * ψ2 p = 0) (hs : ψ1 p + ψ2 p = 1)
  | ramSwap (hr : ∀ n, RF F1 (p * n) = RF F2 n ∧ RF F2 (p * n) = RF F1 n)
      (hc : chi p = 0) (hm : ψ1 p * ψ2 p = 0) (hs : ψ1 p + ψ2 p = -1)

def R (n : Nat) : Int := (RF F1 n : Int) + (RF F2 n : Int)
def D (n : Nat) : Int := (RF F1 n : Int) - (RF F2 n : Int)

variable {F1 F2} {chi ψ1 ψ2}

theorem split_same {p : Nat} (P11 : FPair F1 F1 p) (P22 : FPair F2 F2 p) :
    (∀ n, ¬ p ∣ n → RF F1 (p * n) = RF F1 n + RF F1 n ∧ RF F2 (p * n) = RF F2 n + RF F2 n) ∧
    (∀ m, (RF F1 (p * (p * m)) : Int) = RF F1 (p * m) + RF F1 (p * m) - RF F1 m ∧
          (RF F2 (p * (p * m)) : Int) = RF F2 (p * m) + RF F2 (p * m) - RF F2 m) := by
  refine ⟨fun n hn => ⟨split_ndvd P11 hn, split_ndvd P22 hn⟩, fun m => ⟨?_, ?_⟩⟩
  · have e1 := congrArg (fun z : Nat => (z : Int)) (split_dvd P11 m)
    simp only [LibTheta20.cast2] at e1
    rw [← e1]; lib_int
  · have e2 := congrArg (fun z : Nat => (z : Int)) (split_dvd P22 m)
    simp only [LibTheta20.cast2] at e2
    rw [← e2]; lib_int

theorem split_cross {p : Nat} (P12 : FPair F1 F2 p) (P21 : FPair F2 F1 p) :
    (∀ n, ¬ p ∣ n → RF F1 (p * n) = RF F2 n + RF F2 n ∧ RF F2 (p * n) = RF F1 n + RF F1 n) ∧
    (∀ m, (RF F1 (p * (p * m)) : Int) = RF F2 (p * m) + RF F2 (p * m) - RF F1 m ∧
          (RF F2 (p * (p * m)) : Int) = RF F1 (p * m) + RF F1 (p * m) - RF F2 m) := by
  refine ⟨fun n hn => ⟨split_ndvd P12 hn, split_ndvd P21 hn⟩, fun m => ⟨?_, ?_⟩⟩
  · have e1 := congrArg (fun z : Nat => (z : Int)) (split_dvd P12 m)
    simp only [LibTheta20.cast2] at e1
    rw [← e1]; lib_int
  · have e2 := congrArg (fun z : Nat => (z : Int)) (split_dvd P21 m)
    simp only [LibTheta20.cast2] at e2
    rw [← e2]; lib_int

section Eig
variable (hb1 : ∀ (m : Nat) x y, F1 x y = (m : Int) → x.natAbs ≤ m ∧ y.natAbs ≤ m)
  (hb2 : ∀ (m : Nat) x y, F2 x y = (m : Int) → x.natAbs ≤ m ∧ y.natAbs ≤ m)
  (hs1 : ∀ k a b, F1 (k * a) (k * b) = k * k * F1 a b) (hs2 : ∀ k a b, F2 (k * a) (k * b) = k * k * F2 a b)

include hb1 hb2 hs1 hs2 in
theorem eigR {p : Nat} (hP : LibPrimePow.Prime p) (L : LocalData F1 F2 chi ψ1 ψ2 p) (n : Nat) :
    LibHeckeOp.hk LibInt.intR p (LibInt.intR.mul (chi p) (LibDivFun.one1 LibInt.intR p)) (R F1 F2) n
      = LibInt.intR.mul (LibInt.intR.add (chi p) (LibDivFun.one1 LibInt.intR p)) (R F1 F2 n) := by
  have hp0 : 0 < p := Nat.lt_trans Nat.zero_lt_one hP.1
  cases L with
  | split11 Q11 Q22 hc _ _ =>
    obtain ⟨P11⟩ := Q11; obtain ⟨P22⟩ := Q22
    rw [hc]
    obtain ⟨s1, s2⟩ := split_same P11 P22
    refine LibTheta20.eig_of (R F1 F2) hp0 1 (fun n hn => ?_) (fun m => ?_) n
    · unfold R; rw [(s1 n hn).1, (s1 n hn).2, LibTheta20.cast2, LibTheta20.cast2]; lib_int
    · unfold R; rw [(s2 m).1, (s2 m).2]; lib_int
  | split12 Q12 Q21 hc _ _ =>
    obtain ⟨P12⟩ := Q12; obtain ⟨P21⟩ := Q21
    rw [hc]
    obtain ⟨s1, s2⟩ := split_cross P12 P21
    refine LibTheta20.eig_of (R F1 F2) hp0 1 (fun n hn => ?_) (fun m => ?_) n
    · unfold R; rw [(s1 n hn).1, (s1 n hn).2, LibTheta20.cast2, LibTheta20.cast2]; lib_int
    · unfold R; rw [(s2 m).1, (s2 m).2]; lib_int
  | inert h1 h2 hc _ _ =>
    rw [hc]
    refine LibTheta20.eig_of (R F1 F2) hp0 (-1) (fun n hn => ?_) (fun m => ?_) n
    · unfold R
      rw [inert_zero hp0 hs1 h1 hn, inert_zero hp0 hs2 h2 hn]
      generalize (RF F1 n : Int) = X; generalize (RF F2 n : Int) = Y
      show (0 : Int) + 0 = (-1 + 1) * (X + Y); lib_int
    · unfold R
      rw [inert_scale hp0 hs1 hb1 h1 m, inert_scale hp0 hs2 hb2 h2 m]
      generalize (RF F1 m : Int) = X; generalize (RF F2 m : Int) = Y
      generalize (RF F1 (p * m) : Int) = Z; generalize (RF F2 (p * m) : Int) = W; lib_int
  | ramSame hr hc _ _ =>
    rw [hc]
    refine LibTheta20.eig_of (R F1 F2) hp0 0 (fun n _ => ?_) (fun m => ?_) n
    · unfold R; rw [(hr n).1, (hr n).2]; generalize (RF F1 n : Int) = X; generalize (RF F2 n : Int) = Y; lib_int
    · unfold R; rw [(hr (p * m)).1, (hr (p * m)).2]
      generalize (RF F1 (p * m) : Int) = X; generalize (RF F2 (p * m) : Int) = Y
      generalize (RF F1 m : Int) = Z; generalize (RF F2 m : Int) = W; lib_int
  | ramSwap hr hc _ _ =>
    rw [hc]
    refine LibTheta20.eig_of (R F1 F2) hp0 0 (fun n _ => ?_) (fun m => ?_) n
    · unfold R; rw [(hr n).1, (hr n).2]; generalize (RF F1 n : Int) = X; generalize (RF F2 n : Int) = Y; lib_int
    · unfold R; rw [(hr (p * m)).1, (hr (p * m)).2]
      generalize (RF F1 (p * m) : Int) = X; generalize (RF F2 (p * m) : Int) = Y
      generalize (RF F1 m : Int) = Z; generalize (RF F2 m : Int) = W; lib_int

include hb1 hb2 hs1 hs2 in
theorem eigD {p : Nat} (hP : LibPrimePow.Prime p) (L : LocalData F1 F2 chi ψ1 ψ2 p) (n : Nat) :
    LibHeckeOp.hk LibInt.intR p (LibInt.intR.mul (ψ1 p) (ψ2 p)) (D F1 F2) n
      = LibInt.intR.mul (LibInt.intR.add (ψ1 p) (ψ2 p)) (D F1 F2 n) := by
  have hp0 : 0 < p := Nat.lt_trans Nat.zero_lt_one hP.1
  show LibHeckeOp.hk LibInt.intR p (ψ1 p * ψ2 p) (D F1 F2) n = (ψ1 p + ψ2 p) * D F1 F2 n
  cases L with
  | split11 Q11 Q22 _ hm hs =>
    obtain ⟨P11⟩ := Q11; obtain ⟨P22⟩ := Q22
    rw [hm, hs]
    obtain ⟨s1, s2⟩ := split_same P11 P22
    refine LibTheta20D.eig_of2 (D F1 F2) hp0 1 2 (fun n hn => ?_) (fun m => ?_) n
    · unfold D; rw [(s1 n hn).1, (s1 n hn).2, LibTheta20.cast2, LibTheta20.cast2]; lib_int
    · unfold D; rw [(s2 m).1, (s2 m).2]; lib_int
  | split12 Q12 Q21 _ hm hs =>
    obtain ⟨P12⟩ := Q12; obtain ⟨P21⟩ := Q21
    rw [hm, hs]
    obtain ⟨s1, s2⟩ := split_cross P12 P21
    refine LibTheta20D.eig_of2 (D F1 F2) hp0 1 (-2) (fun n hn => ?_) (fun m => ?_) n
    · unfold D; rw [(s1 n hn).1, (s1 n hn).2, LibTheta20.cast2, LibTheta20.cast2]; lib_int
    · unfold D; rw [(s2 m).1, (s2 m).2]; lib_int
  | inert h1 h2 _ hm hs =>
    rw [hm, hs]
    refine LibTheta20D.eig_of2 (D F1 F2) hp0 (-1) 0 (fun n hn => ?_) (fun m => ?_) n
    · unfold D
      rw [inert_zero hp0 hs1 h1 hn, inert_zero hp0 hs2 h2 hn]
      generalize (RF F1 n : Int) = X; generalize (RF F2 n : Int) = Y
      show (0 : Int) - 0 = 0 * (X - Y); lib_int
    · unfold D
      rw [inert_scale hp0 hs1 hb1 h1 m, inert_scale hp0 hs2 hb2 h2 m]
      generalize (RF F1 m : Int) = X; generalize (RF F2 m : Int) = Y
      generalize (RF F1 (p * m) : Int) = Z; generalize (RF F2 (p * m) : Int) = W; lib_int
  | ramSame hr _ hm hs =>
    rw [hm, hs]
    refine LibTheta20D.eig_of2 (D F1 F2) hp0 0 1 (fun n _ => ?_) (fun m => ?_) n
    · unfold D; rw [(hr n).1, (hr n).2]; generalize (RF F1 n : Int) = X; generalize (RF F2 n : Int) = Y; lib_int
    · unfold D; rw [(hr (p * m)).1, (hr (p * m)).2]
      generalize (RF F1 (p * m) : Int) = X; generalize (RF F2 (p * m) : Int) = Y
      generalize (RF F1 m : Int) = Z; generalize (RF F2 m : Int) = W; lib_int
  | ramSwap hr _ hm hs =>
    rw [hm, hs]
    refine LibTheta20D.eig_of2 (D F1 F2) hp0 0 (-1) (fun n _ => ?_) (fun m => ?_) n
    · unfold D; rw [(hr n).1, (hr n).2]; generalize (RF F1 n : Int) = X; generalize (RF F2 n : Int) = Y; lib_int
    · unfold D; rw [(hr (p * m)).1, (hr (p * m)).2]
      generalize (RF F1 (p * m) : Int) = X; generalize (RF F2 (p * m) : Int) = Y
      generalize (RF F1 m : Int) = Z; generalize (RF F2 m : Int) = W; lib_int

variable (hχ : LibDirTwist.CMult LibInt.intR chi) (hψ1 : LibDirTwist.CMult LibInt.intR ψ1)
  (hψ2 : LibDirTwist.CMult LibInt.intR ψ2) (h1 : RF F1 1 = 2) (h2 : RF F2 1 = 0)
  (data : ∀ p, LibPrimePow.Prime p → LocalData F1 F2 chi ψ1 ψ2 p)

include hb1 hb2 hs1 hs2 hχ h1 h2 data in
/-- **Genus sum.** n > 0 ⇒ r₁(n) + r₂(n) = 2 Σ_{d∣n} χ(d). -/
theorem theta_sum {n : Nat} (hn : 0 < n) :
    R F1 F2 n = 2 * LibEisGen.e LibInt.intR chi (LibDivFun.one1 LibInt.intR) n := by
  have h := LibHeckeScale.eis_scale LibInt.intR chi (LibDivFun.one1 LibInt.intR) hχ
    (LibEisBridge.one1_cmult LibInt.intR) (R F1 F2) (fun p hP n => eigR hb1 hb2 hs1 hs2 hP (data p hP) n) hn
  rw [h]; unfold R; rw [h1, h2]; rfl

include hb1 hb2 hs1 hs2 hψ1 hψ2 h1 h2 data in
/-- **Genus difference.** n > 0 ⇒ r₁(n) − r₂(n) = 2 Σ_{d∣n} ψ₁(d) ψ₂(n/d). -/
theorem theta_diff {n : Nat} (hn : 0 < n) :
    D F1 F2 n = 2 * LibEisGen.e LibInt.intR ψ1 ψ2 n := by
  have h := LibHeckeScale.eis_scale LibInt.intR ψ1 ψ2 hψ1 hψ2 (D F1 F2) (fun p hP n => eigD hb1 hb2 hs1 hs2 hP (data p hP) n) hn
  rw [h]; unfold D; rw [h1, h2]; rfl

include hb1 hb2 hs1 hs2 hχ hψ1 hψ2 h1 h2 data in
/-- **The principal form.** n > 0 ⇒ 2 r₁(n) = 2 Σ χ(d) + 2 Σ ψ₁(d) ψ₂(n/d). -/
theorem r1_formula {n : Nat} (hn : 0 < n) :
    2 * (RF F1 n : Int) = 2 * LibEisGen.e LibInt.intR chi (LibDivFun.one1 LibInt.intR) n
      + 2 * LibEisGen.e LibInt.intR ψ1 ψ2 n := by
  rw [← theta_sum hb1 hb2 hs1 hs2 hχ h1 h2 data hn, ← theta_diff hb1 hb2 hs1 hs2 hψ1 hψ2 h1 h2 data hn]
  unfold R D; lib_int

end Eig

end LibThetaGenus2

#print axioms LibThetaGenus2.eigR
#print axioms LibThetaGenus2.eigD
#print axioms LibThetaGenus2.theta_sum
#print axioms LibThetaGenus2.theta_diff
#print axioms LibThetaGenus2.r1_formula

lib/ThetaGenus3.lean

lean_all/lib/ThetaGenus3.lean

/-
  lib/ThetaGenus3.lean — T46 shared library (G1578a): the theta framework for a class group of order 3, {1, c, c²},
  with F₀ the principal form and F₁ a form of the class c (the class c² has the same representation numbers).
  Local data at every prime gives

      R = r₀ + 2r₁ :  T_p R = (χ(p) + 1)·R,     so R(n) = 2 Σ_{d ∣ n} χ(d)          (n > 0)
      D = r₀ − r₁  :  T_p D = λ_p·D  (λ_p = 2, −1, 0, 1: p principal, p of class c, inert, ramified),
                      so 2·(T_p D)(n) = D(p)·D(n)  —  D is a Hecke eigenform (a cusp form of weight 1, D(1) = 2).

    LocalData3     splitP (FPair F₀F₀, F₁F₁), splitC (FPair F₀F₁, FTri F₁F₀F₁), inert, ram
    eigR, eigD     the eigen relations
    theta_sum3     R(n) = 2E(χ, 1)
    cusp_eig       2·hk p χ(p) D n = D(p)·D(n)
  Lean 4 core only, proofs by hand, no axioms.
-/
import lib.ThetaGenus2
import lib.FormRel3

namespace LibThetaGenus3
open LibMod LibGcd LibIntBox LibNormPiA LibNormPiC LibFormCount LibFormRel LibFormRel3 LibFormRam

variable (F0 F1 : Int → Int → Int) (chi : Nat → Int)

/-- The local behaviour at a prime p of the class group {1, c, c²}. -/
inductive LocalData3 (p : Nat) : Prop where
  | splitP (P00 : Nonempty (FPair F0 F0 p)) (P11 : Nonempty (FPair F1 F1 p)) (hc : chi p = 1)
  | splitC (P01 : Nonempty (FPair F0 F1 p)) (T : Nonempty (FTri F1 F0 F1 p)) (hc : chi p = 1)
  | inert (h0 : ∀ x y : Int, IDv (p : Int) (F0 x y) → IDv (p : Int) x ∧ IDv (p : Int) y)
      (h1 : ∀ x y : Int, IDv (p : Int) (F1 x y) → IDv (p : Int) x ∧ IDv (p : Int) y) (hc : chi p = -1)
  | ram (hr : ∀ n, RF F0 (p * n) = RF F0 n ∧ RF F1 (p * n) = RF F1 n) (hc : chi p = 0)

def R (n : Nat) : Int := (RF F0 n : Int) + 2 * (RF F1 n : Int)
def D (n : Nat) : Int := (RF F0 n : Int) - (RF F1 n : Int)

variable {F0 F1} {chi}

theorem cI (a b : Nat) : ((a + b : Nat) : Int) = (a : Int) + (b : Int) := rfl

section Eig
variable (hb0 : ∀ (m : Nat) x y, F0 x y = (m : Int) → x.natAbs ≤ m ∧ y.natAbs ≤ m)
  (hb1 : ∀ (m : Nat) x y, F1 x y = (m : Int) → x.natAbs ≤ m ∧ y.natAbs ≤ m)
  (hs0 : ∀ k a b, F0 (k * a) (k * b) = k * k * F0 a b) (hs1 : ∀ k a b, F1 (k * a) (k * b) = k * k * F1 a b)

include hb0 hb1 hs0 hs1 in
theorem eigR {p : Nat} (hP : LibPrimePow.Prime p) (L : LocalData3 F0 F1 chi p) (n : Nat) :
    LibHeckeOp.hk LibInt.intR p (LibInt.intR.mul (chi p) (LibDivFun.one1 LibInt.intR p)) (R F0 F1) n
      = LibInt.intR.mul (LibInt.intR.add (chi p) (LibDivFun.one1 LibInt.intR p)) (R F0 F1 n) := by
  have hp0 : 0 < p := Nat.lt_trans Nat.zero_lt_one hP.1
  cases L with
  | splitP Q00 Q11 hc =>
    obtain ⟨P00⟩ := Q00; obtain ⟨P11⟩ := Q11
    rw [hc]
    refine LibTheta20.eig_of (R F0 F1) hp0 1 (fun n hn => ?_) (fun m => ?_) n
    · unfold R; rw [LibFormRel.split_ndvd P00 hn, LibFormRel.split_ndvd P11 hn, cI, cI]; lib_int
    · have e0 := congrArg (fun z : Nat => (z : Int)) (LibFormRel.split_dvd P00 m)
      have e1 := congrArg (fun z : Nat => (z : Int)) (LibFormRel.split_dvd P11 m)
      simp only [cI] at e0 e1
      unfold R
      generalize (RF F0 (p * (p * m)) : Int) = A at e0 ⊢; generalize (RF F0 m : Int) = B at e0 ⊢
      generalize (RF F0 (p * m) : Int) = C at e0 ⊢; generalize (RF F1 (p * (p * m)) : Int) = A' at e1 ⊢
      generalize (RF F1 m : Int) = B' at e1 ⊢; generalize (RF F1 (p * m) : Int) = C' at e1 ⊢
      have hA : A = C + C - B := by rw [show A = (A + B) - B by lib_int, e0]
      have hA' : A' = C' + C' - B' := by rw [show A' = (A' + B') - B' by lib_int, e1]
      rw [hA, hA']; lib_int
  | splitC Q01 QT hc =>
    obtain ⟨P01⟩ := Q01; obtain ⟨T⟩ := QT
    rw [hc]
    refine LibTheta20.eig_of (R F0 F1) hp0 1 (fun n hn => ?_) (fun m => ?_) n
    · unfold R; rw [LibFormRel.split_ndvd P01 hn, LibFormRel3.split_ndvd T hn, cI, cI]; lib_int
    · have e0 := congrArg (fun z : Nat => (z : Int)) (LibFormRel.split_dvd P01 m)
      have e1 := congrArg (fun z : Nat => (z : Int)) (LibFormRel3.split_dvd T m)
      simp only [cI] at e0 e1
      unfold R
      generalize (RF F0 (p * (p * m)) : Int) = A at e0 ⊢; generalize (RF F0 m : Int) = B at e0 ⊢
      generalize (RF F0 (p * m) : Int) = C at e0 e1 ⊢; generalize (RF F1 (p * (p * m)) : Int) = A' at e1 ⊢
      generalize (RF F1 m : Int) = B' at e1 ⊢; generalize (RF F1 (p * m) : Int) = C' at e0 e1 ⊢
      have hA : A = C' + C' - B := by rw [show A = (A + B) - B by lib_int, e0]
      have hA' : A' = C + C' - B' := by rw [show A' = (A' + B') - B' by lib_int, e1]
      rw [hA, hA']; lib_int
  | inert h0 h1 hc =>
    rw [hc]
    refine LibTheta20.eig_of (R F0 F1) hp0 (-1) (fun n hn => ?_) (fun m => ?_) n
    · unfold R
      rw [inert_zero hp0 hs0 h0 hn, inert_zero hp0 hs1 h1 hn]
      generalize (RF F0 n : Int) = X; generalize (RF F1 n : Int) = Y
      show (0 : Int) + 2 * 0 = (-1 + 1) * (X + 2 * Y); lib_int
    · unfold R
      rw [inert_scale hp0 hs0 hb0 h0 m, inert_scale hp0 hs1 hb1 h1 m]
      generalize (RF F0 m : Int) = X; generalize (RF F1 m : Int) = Y
      generalize (RF F0 (p * m) : Int) = Z; generalize (RF F1 (p * m) : Int) = W; lib_int
  | ram hr hc =>
    rw [hc]
    refine LibTheta20.eig_of (R F0 F1) hp0 0 (fun n _ => ?_) (fun m => ?_) n
    · unfold R; rw [(hr n).1, (hr n).2]; generalize (RF F0 n : Int) = X; generalize (RF F1 n : Int) = Y; lib_int
    · unfold R; rw [(hr (p * m)).1, (hr (p * m)).2]
      generalize (RF F0 (p * m) : Int) = X; generalize (RF F1 (p * m) : Int) = Y
      generalize (RF F0 m : Int) = Z; generalize (RF F1 m : Int) = W; lib_int

include hb0 hb1 hs0 hs1 in
theorem eigD {p : Nat} (hP : LibPrimePow.Prime p) (L : LocalData3 F0 F1 chi p) :
    ∃ s : Int, ∀ n, LibHeckeOp.hk LibInt.intR p (chi p) (D F0 F1) n = s * D F0 F1 n := by
  have hp0 : 0 < p := Nat.lt_trans Nat.zero_lt_one hP.1
  cases L with
  | splitP Q00 Q11 hc =>
    obtain ⟨P00⟩ := Q00; obtain ⟨P11⟩ := Q11
    refine ⟨2, fun n => ?_⟩
    rw [hc]
    refine LibTheta20D.eig_of2 (D F0 F1) hp0 1 2 (fun n hn => ?_) (fun m => ?_) n
    · unfold D; rw [LibFormRel.split_ndvd P00 hn, LibFormRel.split_ndvd P11 hn, cI, cI]; lib_int
    · have e0 := congrArg (fun z : Nat => (z : Int)) (LibFormRel.split_dvd P00 m)
      have e1 := congrArg (fun z : Nat => (z : Int)) (LibFormRel.split_dvd P11 m)
      simp only [cI] at e0 e1
      unfold D
      generalize (RF F0 (p * (p * m)) : Int) = A at e0 ⊢; generalize (RF F0 m : Int) = B at e0 ⊢
      generalize (RF F0 (p * m) : Int) = C at e0 ⊢; generalize (RF F1 (p * (p * m)) : Int) = A' at e1 ⊢
      generalize (RF F1 m : Int) = B' at e1 ⊢; generalize (RF F1 (p * m) : Int) = C' at e1 ⊢
      have hA : A = C + C - B := by rw [show A = (A + B) - B by lib_int, e0]
      have hA' : A' = C' + C' - B' := by rw [show A' = (A' + B') - B' by lib_int, e1]
      rw [hA, hA']; lib_int
  | splitC Q01 QT hc =>
    obtain ⟨P01⟩ := Q01; obtain ⟨T⟩ := QT
    refine ⟨-1, fun n => ?_⟩
    rw [hc]
    refine LibTheta20D.eig_of2 (D F0 F1) hp0 1 (-1) (fun n hn => ?_) (fun m => ?_) n
    · unfold D; rw [LibFormRel.split_ndvd P01 hn, LibFormRel3.split_ndvd T hn, cI, cI]; lib_int
    · have e0 := congrArg (fun z : Nat => (z : Int)) (LibFormRel.split_dvd P01 m)
      have e1 := congrArg (fun z : Nat => (z : Int)) (LibFormRel3.split_dvd T m)
      simp only [cI] at e0 e1
      unfold D
      generalize (RF F0 (p * (p * m)) : Int) = A at e0 ⊢; generalize (RF F0 m : Int) = B at e0 ⊢
      generalize (RF F0 (p * m) : Int) = C at e0 e1 ⊢; generalize (RF F1 (p * (p * m)) : Int) = A' at e1 ⊢
      generalize (RF F1 m : Int) = B' at e1 ⊢; generalize (RF F1 (p * m) : Int) = C' at e0 e1 ⊢
      have hA : A = C' + C' - B := by rw [show A = (A + B) - B by lib_int, e0]
      have hA' : A' = C + C' - B' := by rw [show A' = (A' + B') - B' by lib_int, e1]
      rw [hA, hA']; lib_int
  | inert h0 h1 hc =>
    refine ⟨0, fun n => ?_⟩
    rw [hc]
    refine LibTheta20D.eig_of2 (D F0 F1) hp0 (-1) 0 (fun n hn => ?_) (fun m => ?_) n
    · unfold D
      rw [inert_zero hp0 hs0 h0 hn, inert_zero hp0 hs1 h1 hn]
      generalize (RF F0 n : Int) = X; generalize (RF F1 n : Int) = Y
      show (0 : Int) - 0 = 0 * (X - Y); lib_int
    · unfold D
      rw [inert_scale hp0 hs0 hb0 h0 m, inert_scale hp0 hs1 hb1 h1 m]
      generalize (RF F0 m : Int) = X; generalize (RF F1 m : Int) = Y
      generalize (RF F0 (p * m) : Int) = Z; generalize (RF F1 (p * m) : Int) = W; lib_int
  | ram hr hc =>
    refine ⟨1, fun n => ?_⟩
    rw [hc]
    refine LibTheta20D.eig_of2 (D F0 F1) hp0 0 1 (fun n _ => ?_) (fun m => ?_) n
    · unfold D; rw [(hr n).1, (hr n).2]; generalize (RF F0 n : Int) = X; generalize (RF F1 n : Int) = Y; lib_int
    · unfold D; rw [(hr (p * m)).1, (hr (p * m)).2]
      generalize (RF F0 (p * m) : Int) = X; generalize (RF F1 (p * m) : Int) = Y
      generalize (RF F0 m : Int) = Z; generalize (RF F1 m : Int) = W; lib_int

variable (hχ : LibDirTwist.CMult LibInt.intR chi) (h1 : RF F0 1 = 2) (h2 : RF F1 1 = 0)
  (data : ∀ p, LibPrimePow.Prime p → LocalData3 F0 F1 chi p)

include hb0 hb1 hs0 hs1 hχ h1 h2 data in
/-- **Class sum.** n > 0 ⇒ r₀(n) + 2r₁(n) = 2 Σ_{d∣n} χ(d). -/
theorem theta_sum3 {n : Nat} (hn : 0 < n) :
    R F0 F1 n = 2 * LibEisGen.e LibInt.intR chi (LibDivFun.one1 LibInt.intR) n := by
  have h := LibHeckeScale.eis_scale LibInt.intR chi (LibDivFun.one1 LibInt.intR) hχ
    (LibEisBridge.one1_cmult LibInt.intR) (R F0 F1) (fun p hP n => eigR hb0 hb1 hs0 hs1 hP (data p hP) n) hn
  rw [h]; unfold R; rw [h1, h2]; rfl

include hb0 hb1 hs0 hs1 h1 h2 data in
/-- **The cusp form is a Hecke eigenform.** 2·(T_p D)(n) = D(p)·D(n) for every prime p. -/
theorem cusp_eig {p : Nat} (hP : LibPrimePow.Prime p) (n : Nat) :
    2 * LibHeckeOp.hk LibInt.intR p (chi p) (D F0 F1) n = D F0 F1 p * D F0 F1 n := by
  obtain ⟨s, hs⟩ := eigD hb0 hb1 hs0 hs1 hP (data p hP)
  have hp1 : D F0 F1 p = s * 2 := by
    have e := hs 1
    have hk1 : LibHeckeOp.hk LibInt.intR p (chi p) (D F0 F1) 1 = D F0 F1 p := by
      unfold LibHeckeOp.hk
      have hm : md p 1 = 1 := md_of_lt (Nat.lt_trans Nat.zero_lt_one hP.1) hP.1
      rw [hm, Nat.mul_one]
      show D F0 F1 p + 0 = D F0 F1 p
      generalize D F0 F1 p = z; lib_int
    rw [hk1] at e
    rw [e]; unfold D; rw [h1, h2]; rfl
  rw [hs n, hp1]; lib_int

end Eig

end LibThetaGenus3

#print axioms LibThetaGenus3.eigR
#print axioms LibThetaGenus3.eigD
#print axioms LibThetaGenus3.theta_sum3
#print axioms LibThetaGenus3.cusp_eig

lib/ThetaLogDeriv.lean

lean_all/lib/ThetaLogDeriv.lean

/-
  lib/ThetaLogDeriv.lean — T46 shared library (G925d): closed form of Σ k² cᵏ q^{k²} (q∂_q log θ), truncated version. agent3 request (2).

  R[q]/(q^{2r₀+2}) with q = X, N = K₀ + r₀, (K₀+1)² > 2r₀+1, c = ι(c₀), c₀² = 1, inverses v_i·(1 − q^{2i+2}) = 1, u_n·(1 + c q^{2n+1}) = 1:
    theta_logderiv
      Σ_{k≤K₀} k² cᵏ q^{k²} + Σ_{k<K₀} (k+1)² c^{k+1} q^{(k+1)²}                      (= 2Σ_{k≥1} k² cᵏ q^{k²} = q∂_q θ(c))
        = θ · ( Σ_{i<2N} −(2i+2) q^{2i+2} v_i  +  Σ_{n<N} (2n+1) c q^{2n+1} (u_n + u_n) )
    θ = (q²;q²)_{2N} Π_{n<N}(1 + c q^{2n+1})² (= 1 + 2Σ cᵏ q^{k²}, JTPThetaTP).
  Inverses via TPInv.tinv (constant term 1). Lean 4 core only, proofs by hand, no axioms.
-/
import lib.JTPThetaTP
import lib.TPLogDeriv
import lib.QVander

namespace LibThetaLogDeriv
open DetChain LibRing LibSum LibSemi LibQBinom LibJTPFinite LibTPoly LibTPChar LibMacMahon LibQPoch LibJTPTrunc LibJTPSum
  LibJTPTheta LibJTPThetaTP LibTPDeriv LibTPLogDeriv LibQVander

variable {R : Type} (K : CRing R) (r0 : Nat)


theorem dlt_cXpow (c0 : R) (j m : Nat) :
    dlt K (r0 + r0 + 1) ((tpR K (r0 + r0 + 1)).mul (pw (tpR K (r0 + r0 + 1)) (iota K (r0 + r0 + 1) c0) j) (pw (tpR K (r0 + r0 + 1)) (X K (r0 + r0 + 1)) m)) = (tpR K (r0 + r0 + 1)).mul ((iota K (r0 + r0 + 1)) (K.mul (castN K m) (pw K c0 j))) (pw (tpR K (r0 + r0 + 1)) (X K (r0 + r0 + 1)) m) := by
  rw [← iota_pw K (r0 + r0 + 1) c0 j, dlt_const_mul, dlt_Xpow, iota_mul]; lib_ring

/-- **Closed form of the logarithmic derivative of θ.** -/
theorem theta_logderiv (K0 : Nat) (hK : r0 + r0 + 1 < (K0 + 1) * (K0 + 1)) (c0 : R) (hc0 : K.mul c0 c0 = K.one)
    (v u : Nat → TP R (r0 + r0 + 1))
    (hv : ∀ i, (tpR K (r0 + r0 + 1)).mul (sub (tpR K (r0 + r0 + 1)) (tpR K (r0 + r0 + 1)).one (pw (tpR K (r0 + r0 + 1)) ((tpR K (r0 + r0 + 1)).mul (X K (r0 + r0 + 1)) (X K (r0 + r0 + 1))) (i + 1))) (v i) = (tpR K (r0 + r0 + 1)).one)
    (hu : ∀ n, (tpR K (r0 + r0 + 1)).mul ((tpR K (r0 + r0 + 1)).add (tpR K (r0 + r0 + 1)).one ((tpR K (r0 + r0 + 1)).mul (pw (tpR K (r0 + r0 + 1)) (X K (r0 + r0 + 1)) (n + n + 1)) (iota K (r0 + r0 + 1) c0))) (u n) = (tpR K (r0 + r0 + 1)).one) :
    (tpR K (r0 + r0 + 1)).add (sum (toSemi (tpR K (r0 + r0 + 1))) (fun k => (tpR K (r0 + r0 + 1)).mul ((iota K (r0 + r0 + 1)) (K.mul (castN K (k * k)) (pw K c0 k))) (pw (tpR K (r0 + r0 + 1)) (X K (r0 + r0 + 1)) (k * k))) (rng (K0 + 1)))
        (sum (toSemi (tpR K (r0 + r0 + 1))) (fun k => (tpR K (r0 + r0 + 1)).mul ((iota K (r0 + r0 + 1)) (K.mul (castN K ((k + 1) * (k + 1))) (pw K c0 (k + 1)))) (pw (tpR K (r0 + r0 + 1)) (X K (r0 + r0 + 1)) ((k + 1) * (k + 1)))) (rng K0))
      = (tpR K (r0 + r0 + 1)).mul ((tpR K (r0 + r0 + 1)).mul (qp (tpR K (r0 + r0 + 1)) ((tpR K (r0 + r0 + 1)).mul (X K (r0 + r0 + 1)) (X K (r0 + r0 + 1))) ((K0 + r0) + (K0 + r0)))
            (prodF (tpR K (r0 + r0 + 1)) (fun n => (tpR K (r0 + r0 + 1)).mul ((tpR K (r0 + r0 + 1)).add (tpR K (r0 + r0 + 1)).one ((tpR K (r0 + r0 + 1)).mul (pw (tpR K (r0 + r0 + 1)) (X K (r0 + r0 + 1)) (n + n + 1)) (iota K (r0 + r0 + 1) c0))) ((tpR K (r0 + r0 + 1)).add (tpR K (r0 + r0 + 1)).one ((tpR K (r0 + r0 + 1)).mul (pw (tpR K (r0 + r0 + 1)) (X K (r0 + r0 + 1)) (n + n + 1)) (iota K (r0 + r0   -- (+23 chars; full line in the Zenodo source)
          ((tpR K (r0 + r0 + 1)).add (sum (toSemi (tpR K (r0 + r0 + 1))) (fun i => (tpR K (r0 + r0 + 1)).mul (dlt K (r0 + r0 + 1) (sub (tpR K (r0 + r0 + 1)) (tpR K (r0 + r0 + 1)).one (pw (tpR K (r0 + r0 + 1)) ((tpR K (r0 + r0 + 1)).mul (X K (r0 + r0 + 1)) (X K (r0 + r0 + 1))) (i + 1)))) (v i)) (rng ((K0 + r0) + (K0 + r0))))
            (sum (toSemi (tpR K (r0 + r0 + 1))) (fun n => (tpR K (r0 + r0 + 1)).mul ((tpR K (r0 + r0 + 1)).mul ((iota K (r0 + r0 + 1)) (K.mul (castN K (n + n + 1)) c0)) (pw (tpR K (r0 + r0 + 1)) (X K (r0 + r0 + 1)) (n + n + 1))) ((tpR K (r0 + r0 + 1)).add (u n) (u n))) (rng (K0 + r0)))) := by
  have hc : (tpR K (r0 + r0 + 1)).mul (iota K (r0 + r0 + 1) c0) (iota K (r0 + r0 + 1) c0) = (tpR K (r0 + r0 + 1)).one := by rw [← iota_mul, hc0]; rfl
  have hθ := theta_c K r0 K0 hK (iota K (r0 + r0 + 1) c0) hc
  have hd := congrArg (dlt K (r0 + r0 + 1)) hθ
  -- left: δ(Σ cᵏ X^{k²})
  rw [dlt_add, dlt_sum, dlt_sum] at hd
  rw [sum_ext (toSemi (tpR K (r0 + r0 + 1))) _ _ (fun k => dlt_cXpow K r0 c0 k (k * k)), sum_ext (toSemi (tpR K (r0 + r0 + 1))) _ _ (fun k => dlt_cXpow K r0 c0 (k + 1) ((k + 1) * (k + 1)))] at hd
  rw [← hd]
  -- right: δ(qp · Pc)
  unfold qp
  rw [dlt_mul]
  rw [dlt_prodF K (r0 + r0 + 1) _ v hv]
  have huu : ∀ n, (tpR K (r0 + r0 + 1)).mul ((tpR K (r0 + r0 + 1)).mul ((tpR K (r0 + r0 + 1)).add (tpR K (r0 + r0 + 1)).one ((tpR K (r0 + r0 + 1)).mul (pw (tpR K (r0 + r0 + 1)) (X K (r0 + r0 + 1)) (n + n + 1)) (iota K (r0 + r0 + 1) c0))) ((tpR K (r0 + r0 + 1)).add (tpR K (r0 + r0 + 1)).one ((tpR K (r0 + r0 + 1)).mul (pw (tpR K (r0 + r0 + 1)) (X K (r0 + r0 + 1)) (n + n + 1)) (iota K (r0 + r0 + 1) c  -- (+5 chars; full line in the Zenodo source)
      ((tpR K (r0 + r0 + 1)).mul (u n) (u n)) = (tpR K (r0 + r0 + 1)).one := by
    intro n
    have h := hu n
    generalize (tpR K (r0 + r0 + 1)).add (tpR K (r0 + r0 + 1)).one ((tpR K (r0 + r0 + 1)).mul (pw (tpR K (r0 + r0 + 1)) (X K (r0 + r0 + 1)) (n + n + 1)) (iota K (r0 + r0 + 1) c0)) = a at h ⊢
    generalize u n = b at h ⊢
    have e : (tpR K (r0 + r0 + 1)).mul ((tpR K (r0 + r0 + 1)).mul a a) ((tpR K (r0 + r0 + 1)).mul b b) = (tpR K (r0 + r0 + 1)).mul ((tpR K (r0 + r0 + 1)).mul a b) ((tpR K (r0 + r0 + 1)).mul a b) := by lib_ring
    rw [e, h]; lib_ring
  rw [dlt_prodF K (r0 + r0 + 1) _ (fun n => (tpR K (r0 + r0 + 1)).mul (u n) (u n)) huu]
  -- term of the second sum: δ((1+a)(1+a))·u² = (2n+1)c X^{2n+1}·(u + u)
  have hterm : ∀ n, (tpR K (r0 + r0 + 1)).mul (dlt K (r0 + r0 + 1) ((tpR K (r0 + r0 + 1)).mul ((tpR K (r0 + r0 + 1)).add (tpR K (r0 + r0 + 1)).one ((tpR K (r0 + r0 + 1)).mul (pw (tpR K (r0 + r0 + 1)) (X K (r0 + r0 + 1)) (n + n + 1)) (iota K (r0 + r0 + 1) c0))) ((tpR K (r0 + r0 + 1)).add (tpR K (r0 + r0 + 1)).one ((tpR K (r0 + r0 + 1)).mul (pw (tpR K (r0 + r0 + 1)) (X K (r0 + r0 + 1)) (n + n + 1))   -- (+29 chars; full line in the Zenodo source)
      ((tpR K (r0 + r0 + 1)).mul (u n) (u n)) = (tpR K (r0 + r0 + 1)).mul ((tpR K (r0 + r0 + 1)).mul ((iota K (r0 + r0 + 1)) (K.mul (castN K (n + n + 1)) c0)) (pw (tpR K (r0 + r0 + 1)) (X K (r0 + r0 + 1)) (n + n + 1))) ((tpR K (r0 + r0 + 1)).add (u n) (u n)) := by
    intro n
    have da : dlt K (r0 + r0 + 1) ((tpR K (r0 + r0 + 1)).add (tpR K (r0 + r0 + 1)).one ((tpR K (r0 + r0 + 1)).mul (pw (tpR K (r0 + r0 + 1)) (X K (r0 + r0 + 1)) (n + n + 1)) (iota K (r0 + r0 + 1) c0)))
        = (tpR K (r0 + r0 + 1)).mul ((iota K (r0 + r0 + 1)) (K.mul (castN K (n + n + 1)) c0)) (pw (tpR K (r0 + r0 + 1)) (X K (r0 + r0 + 1)) (n + n + 1)) := by
      rw [dlt_add, dlt_one, (tpR K (r0 + r0 + 1)).mul_comm (pw (tpR K (r0 + r0 + 1)) (X K (r0 + r0 + 1)) _) (iota K (r0 + r0 + 1) c0), dlt_const_mul, dlt_Xpow, iota_mul]; lib_ring
    rw [dlt_mul, da]
    have h := hu n
    generalize (tpR K (r0 + r0 + 1)).add (tpR K (r0 + r0 + 1)).one ((tpR K (r0 + r0 + 1)).mul (pw (tpR K (r0 + r0 + 1)) (X K (r0 + r0 + 1)) (n + n + 1)) (iota K (r0 + r0 + 1) c0)) = a at h ⊢
    generalize (tpR K (r0 + r0 + 1)).mul ((iota K (r0 + r0 + 1)) (K.mul (castN K (n + n + 1)) c0)) (pw (tpR K (r0 + r0 + 1)) (X K (r0 + r0 + 1)) (n + n + 1)) = d
    generalize u n = b at h ⊢
    have e : (tpR K (r0 + r0 + 1)).mul ((tpR K (r0 + r0 + 1)).add ((tpR K (r0 + r0 + 1)).mul d a) ((tpR K (r0 + r0 + 1)).mul a d)) ((tpR K (r0 + r0 + 1)).mul b b) = (tpR K (r0 + r0 + 1)).mul ((tpR K (r0 + r0 + 1)).mul d ((tpR K (r0 + r0 + 1)).add b b)) ((tpR K (r0 + r0 + 1)).mul a b) := by lib_ring
    rw [e, h]; lib_ring
  rw [sum_ext (toSemi (tpR K (r0 + r0 + 1))) _ _ hterm]
  generalize prodF (tpR K (r0 + r0 + 1)) (fun i => sub (tpR K (r0 + r0 + 1)) (tpR K (r0 + r0 + 1)).one (pw (tpR K (r0 + r0 + 1)) ((tpR K (r0 + r0 + 1)).mul (X K (r0 + r0 + 1)) (X K (r0 + r0 + 1))) (i + 1))) ((K0 + r0) + (K0 + r0)) = P
  lib_ring

end LibThetaLogDeriv

#print axioms LibThetaLogDeriv.dlt_cXpow
#print axioms LibThetaLogDeriv.theta_logderiv

lib/ThetaLogSubst.lean

lean_all/lib/ThetaLogSubst.lean

/-
  lib/ThetaLogSubst.lean — T46 shared library (G929): substituted version of the closed form of the θ log-derivative (Y^{2r₀+2} = 0, e.g. Y = X⁴).

    ev_inv            uv = 1 ⇒ ev(u)·ev(v) = 1
    theta_log_subst   Σ_{k≤K₀} k²cᵏY^{k²} + Σ_{k<K₀} (k+1)²c^{k+1}Y^{(k+1)²}
                        = θ(c;Y)·(Σ_{i<2N} ev(δ(1 − q^{2i+2}))·ev(v_i) + Σ_{n<N} (2n+1)c Y^{2n+1}·(ev u_n + ev u_n))
  (Y = X⁴: Σ k² cᵏ Q^{4k²}, the even side of B_p in agent3 G44d.) Lean 4 core only, proofs by hand, no axioms.
-/
import lib.ThetaLogDeriv
import lib.ThetaSubst

namespace LibThetaLogSubst
open DetChain LibRing LibSum LibSemi LibTPoly LibTPChar LibQBinom LibJTPFinite LibQPoch LibQVander LibTPEval LibThetaSubst
  LibThetaLogDeriv LibTPDeriv

variable {R : Type} (K : CRing R) (r0 M' : Nat) (Y : TP R M') (hY : pw (tpR K M') Y (r0 + r0 + 1 + 1) = (tpR K M').zero)

include hY in
theorem ev_inv (u v : TP R (r0 + r0 + 1)) (h : (tpR K (r0 + r0 + 1)).mul u v = (tpR K (r0 + r0 + 1)).one) :
    (tpR K M').mul (ev K (r0 + r0 + 1) M' Y u) (ev K (r0 + r0 + 1) M' Y v) = (tpR K M').one := by
  rw [← ev_mul K _ M' Y hY, h, ev_one]

include hY in
/-- **Substituted version of the log-derivative closed form.** -/
theorem theta_log_subst (K0 : Nat) (hK : r0 + r0 + 1 < (K0 + 1) * (K0 + 1)) (c0 : R) (hc0 : K.mul c0 c0 = K.one)
    (v u : Nat → TP R (r0 + r0 + 1))
    (hv : ∀ i, (tpR K (r0 + r0 + 1)).mul (sub (tpR K (r0 + r0 + 1)) (tpR K (r0 + r0 + 1)).one (pw (tpR K (r0 + r0 + 1)) ((tpR K (r0 + r0 + 1)).mul (X K (r0 + r0 + 1)) (X K (r0 + r0 + 1))) (i + 1))) (v i) = (tpR K (r0 + r0 + 1)).one)
    (hu : ∀ n, (tpR K (r0 + r0 + 1)).mul ((tpR K (r0 + r0 + 1)).add (tpR K (r0 + r0 + 1)).one ((tpR K (r0 + r0 + 1)).mul (pw (tpR K (r0 + r0 + 1)) (X K (r0 + r0 + 1)) (n + n + 1)) (iota K (r0 + r0 + 1) c0))) (u n) = (tpR K (r0 + r0 + 1)).one) :
    (tpR K M').add (sum (toSemi (tpR K M')) (fun k => (tpR K M').mul (iota K M' (K.mul (castN K (k * k)) (pw K c0 k))) (pw (tpR K M') Y (k * k))) (rng (K0 + 1)))
        (sum (toSemi (tpR K M')) (fun k => (tpR K M').mul (iota K M' (K.mul (castN K ((k + 1) * (k + 1))) (pw K c0 (k + 1)))) (pw (tpR K M') Y ((k + 1) * (k + 1)))) (rng K0))
      = (tpR K M').mul
          ((tpR K M').mul (qp (tpR K M') ((tpR K M').mul Y Y) ((K0 + r0) + (K0 + r0)))
            (prodF (tpR K M') (fun n => (tpR K M').mul ((tpR K M').add (tpR K M').one ((tpR K M').mul (pw (tpR K M') Y (n + n + 1)) (iota K M' c0)))
              ((tpR K M').add (tpR K M').one ((tpR K M').mul (pw (tpR K M') Y (n + n + 1)) (iota K M' c0)))) (K0 + r0)))
          ((tpR K M').add
            (sum (toSemi (tpR K M')) (fun i => (tpR K M').mul (ev K (r0 + r0 + 1) M' Y (dlt K (r0 + r0 + 1) (sub (tpR K (r0 + r0 + 1)) (tpR K (r0 + r0 + 1)).one (pw (tpR K (r0 + r0 + 1)) ((tpR K (r0 + r0 + 1)).mul (X K (r0 + r0 + 1)) (X K (r0 + r0 + 1))) (i + 1))))) (ev K (r0 + r0 + 1) M' Y (v i))) (rng ((K0 + r0) + (K0 + r0))))
            (sum (toSemi (tpR K M')) (fun n => (tpR K M').mul ((tpR K M').mul (iota K M' (K.mul (castN K (n + n + 1)) c0)) (pw (tpR K M') Y (n + n + 1)))
              ((tpR K M').add (ev K (r0 + r0 + 1) M' Y (u n)) (ev K (r0 + r0 + 1) M' Y (u n)))) (rng (K0 + r0)))) := by
  have h := congrArg (ev K (r0 + r0 + 1) M' Y) (theta_logderiv K r0 K0 hK c0 hc0 v u hv hu)
  have hX := ev_X K (r0 + r0 + 1) M' Y hY (Nat.succ_le_succ (Nat.zero_le _))
  unfold qp at h ⊢
  rw [ev_add, ev_sum, ev_sum, ev_mul K _ M' Y hY, ev_mul K _ M' Y hY, ev_prodF K _ M' Y hY, ev_prodF K _ M' Y hY, ev_add, ev_sum, ev_sum] at h
  have l1 : ∀ k, ev K (r0 + r0 + 1) M' Y ((tpR K (r0 + r0 + 1)).mul (iota K (r0 + r0 + 1) (K.mul (castN K (k * k)) (pw K c0 k))) (pw (tpR K (r0 + r0 + 1)) (X K (r0 + r0 + 1)) (k * k)))
      = (tpR K M').mul (iota K M' (K.mul (castN K (k * k)) (pw K c0 k))) (pw (tpR K M') Y (k * k)) := by
    intro k; rw [ev_iota_mul, ev_pw K _ M' Y hY, hX]
  have l2 : ∀ k, ev K (r0 + r0 + 1) M' Y ((tpR K (r0 + r0 + 1)).mul (iota K (r0 + r0 + 1) (K.mul (castN K ((k + 1) * (k + 1))) (pw K c0 (k + 1)))) (pw (tpR K (r0 + r0 + 1)) (X K (r0 + r0 + 1)) ((k + 1) * (k + 1))))
      = (tpR K M').mul (iota K M' (K.mul (castN K ((k + 1) * (k + 1))) (pw K c0 (k + 1)))) (pw (tpR K M') Y ((k + 1) * (k + 1))) := fun k => l1 (k + 1)
  have f1 : ∀ i, ev K (r0 + r0 + 1) M' Y (sub (tpR K (r0 + r0 + 1)) (tpR K (r0 + r0 + 1)).one
        (pw (tpR K (r0 + r0 + 1)) ((tpR K (r0 + r0 + 1)).mul (X K (r0 + r0 + 1)) (X K (r0 + r0 + 1))) (i + 1)))
      = sub (tpR K M') (tpR K M').one (pw (tpR K M') ((tpR K M').mul Y Y) (i + 1)) := by
    intro i
    show ev K _ M' Y ((tpR K _).add _ ((tpR K _).neg _)) = (tpR K M').add _ ((tpR K M').neg _)
    rw [ev_add, ev_one, ev_neg, ev_pw K _ M' Y hY, ev_mul K _ M' Y hY, hX]
  have f2 : ∀ n, ev K (r0 + r0 + 1) M' Y ((tpR K (r0 + r0 + 1)).mul
        ((tpR K (r0 + r0 + 1)).add (tpR K (r0 + r0 + 1)).one ((tpR K (r0 + r0 + 1)).mul (pw (tpR K (r0 + r0 + 1)) (X K (r0 + r0 + 1)) (n + n + 1)) (iota K (r0 + r0 + 1) c0)))
        ((tpR K (r0 + r0 + 1)).add (tpR K (r0 + r0 + 1)).one ((tpR K (r0 + r0 + 1)).mul (pw (tpR K (r0 + r0 + 1)) (X K (r0 + r0 + 1)) (n + n + 1)) (iota K (r0 + r0 + 1) c0))))
      = (tpR K M').mul
        ((tpR K M').add (tpR K M').one ((tpR K M').mul (pw (tpR K M') Y (n + n + 1)) (iota K M' c0)))
        ((tpR K M').add (tpR K M').one ((tpR K M').mul (pw (tpR K M') Y (n + n + 1)) (iota K M' c0))) := by
    intro n
    rw [ev_mul K _ M' Y hY, ev_add, ev_one, ev_mul K _ M' Y hY, ev_pw K _ M' Y hY, hX, ev_iota]
  have s1 : ∀ i, ev K (r0 + r0 + 1) M' Y ((tpR K (r0 + r0 + 1)).mul (dlt K (r0 + r0 + 1) (sub (tpR K (r0 + r0 + 1)) (tpR K (r0 + r0 + 1)).one (pw (tpR K (r0 + r0 + 1)) ((tpR K (r0 + r0 + 1)).mul (X K (r0 + r0 + 1)) (X K (r0 + r0 + 1))) (i + 1)))) (v i))
      = (tpR K M').mul (ev K (r0 + r0 + 1) M' Y (dlt K (r0 + r0 + 1) (sub (tpR K (r0 + r0 + 1)) (tpR K (r0 + r0 + 1)).one (pw (tpR K (r0 + r0 + 1)) ((tpR K (r0 + r0 + 1)).mul (X K (r0 + r0 + 1)) (X K (r0 + r0 + 1))) (i + 1))))) (ev K (r0 + r0 + 1) M' Y (v i)) :=
    fun i => ev_mul K _ M' Y hY _ _
  have s2 : ∀ n, ev K (r0 + r0 + 1) M' Y ((tpR K (r0 + r0 + 1)).mul ((tpR K (r0 + r0 + 1)).mul (iota K (r0 + r0 + 1) (K.mul (castN K (n + n + 1)) c0)) (pw (tpR K (r0 + r0 + 1)) (X K (r0 + r0 + 1)) (n + n + 1)))
        ((tpR K (r0 + r0 + 1)).add (u n) (u n)))
      = (tpR K M').mul ((tpR K M').mul (iota K M' (K.mul (castN K (n + n + 1)) c0)) (pw (tpR K M') Y (n + n + 1)))
        ((tpR K M').add (ev K (r0 + r0 + 1) M' Y (u n)) (ev K (r0 + r0 + 1) M' Y (u n))) := by
    intro n
    rw [ev_mul K _ M' Y hY, ev_iota_mul, ev_pw K _ M' Y hY, hX, ev_add]
  rw [sum_ext (toSemi (tpR K M')) _ _ l1, sum_ext (toSemi (tpR K M')) _ _ l2, prodF_ext (tpR K M') _ _ f1, prodF_ext (tpR K M') _ _ f2,
    sum_ext (toSemi (tpR K M')) _ _ s1, sum_ext (toSemi (tpR K M')) _ _ s2] at h
  exact h

end LibThetaLogSubst

#print axioms LibThetaLogSubst.ev_inv
#print axioms LibThetaLogSubst.theta_log_subst

lib/ThetaOddTel.lean

lean_all/lib/ThetaOddTel.lean

/-
  lib/ThetaOddTel.lean — T46 shared library (agent3 request, T45 6f-A′-1): a discrete Euler–Maclaurin
  telescoping for the odd Gaussian sum  E_M(x) = Σ_{k odd < 2M} x^{k²} (1 − k²(1 − x²)).

  Continuous model: d/dt (t e^{−at²}) = e^{−at²}(1 − 2at²), so ∫ e^{−at²}(1 − 2at²) = 0.
  Discrete version, with the central difference of g(j) = j x^{j²} over the odd points k = 2i + 1:
      2 x^{k²}(1 − k²(1 − x²)) = x^{(k−1)²} Q_k(x) + [ (k+1) x^{(k+1)²} − (k−1) x^{(k−1)²} ],
      Q_k(x) = (k − 1) − 2(k² − 1) x^{2k−1} + 2k² x^{2k+1} − (k + 1) x^{4k}      (Q_k(1) = 0, Q_k'(1) = −2).
  Summing, the brackets telescope:

    oddT x i      2 x^{(2i+1)²} (1 − (2i+1)²(1 − x²))
    qpoly x i     Q_{2i+1}(x), written with m = castN i
    tel           Σ_{i<M} oddT x i = Σ_{i<M} x^{(2i)²} Q_{2i+1}(x) + 2M · x^{(2M)²}       (any commutative ring)
    tel_nonneg    0 ≤ x and Q_{2i+1}(x) ≥ 0 for i < M  ⇒  2M x^{(2M)²} ≤ Σ_{i<M} oddT x i   (ordered ring)

  (Q_k ≥ 0 on [0, 1] holds numerically for all odd k ≤ 401; it is the remaining pointwise lemma.)
  Lean 4 core only, proofs by hand, no axioms.
-/
import lib.ChebPow

namespace LibThetaOddTel
open DetChain LibRing YMChain

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

def ssum (f : Nat → R) : Nat → R
  | 0 => K.zero
  | n + 1 => K.add (ssum f n) (f n)

/-- 2 x^{k²}(1 − k²(1 − x²)) with k = 2i + 1. -/
def oddT (x : R) (i : Nat) : R :=
  K.mul (K.add K.one K.one) (K.mul (pw K x ((2 * i + 1) * (2 * i + 1)))
    (sub K K.one (K.mul (K.mul (K.add (K.add (castN K i) (castN K i)) K.one) (K.add (K.add (castN K i) (castN K i)) K.one))
      (sub K K.one (K.mul x x)))))

/-- Q_k(x) for k = 2i + 1: 2m − 8m(m+1) x^{4i+1} + 2(2m+1)² x^{4i+3} − (2m+2) x^{8i+4}, m = i. -/
def qpoly (x : R) (i : Nat) : R :=
  K.add (K.add (K.add (K.add (castN K i) (castN K i))
    (K.neg (K.mul (K.mul (K.mul (K.add (K.add K.one K.one) (K.add K.one K.one)) (K.add K.one K.one))
      (K.mul (castN K i) (K.add (castN K i) K.one))) (pw K x (4 * i + 1)))))
    (K.mul (K.mul (K.add K.one K.one) (K.mul (K.add (K.add (castN K i) (castN K i)) K.one) (K.add (K.add (castN K i) (castN K i)) K.one)))
      (pw K x (4 * i + 3))))
    (K.neg (K.mul (K.add (K.add (castN K i) (castN K i)) (K.add K.one K.one)) (pw K x (8 * i + 4))))

theorem e_odd (i : Nat) : (2 * i + 1) * (2 * i + 1) = (2 * i) * (2 * i) + (4 * i + 1) := by lib_nat
theorem e_next (i : Nat) : (2 * (i + 1)) * (2 * (i + 1)) = (2 * i) * (2 * i) + (4 * i + 4 * i + 4) := by lib_nat
theorem e_8 (i : Nat) : 8 * i + 4 = (4 * i + 4 * i) + 4 := by lib_nat

theorem step (x : R) (i : Nat) :
    K.add (oddT K x i) (K.mul (K.add (castN K i) (castN K i)) (pw K x ((2 * i) * (2 * i))))
      = K.add (K.mul (pw K x ((2 * i) * (2 * i))) (qpoly K x i))
          (K.mul (K.add (castN K (i + 1)) (castN K (i + 1))) (pw K x ((2 * (i + 1)) * (2 * (i + 1))))) := by
  unfold oddT qpoly
  rw [e_odd, e_next, e_8, LibChebPow.pw_add K x ((2 * i) * (2 * i)) (4 * i + 1),
    LibChebPow.pw_add K x ((2 * i) * (2 * i)) (4 * i + 4 * i + 4),
    show pw K x (4 * i + 1) = K.mul (pw K x (4 * i)) x from rfl,
    show pw K x (4 * i + 3) = K.mul (K.mul (K.mul (pw K x (4 * i)) x) x) x from rfl,
    show pw K x (4 * i + 4 * i + 4) = K.mul (K.mul (K.mul (K.mul (pw K x (4 * i + 4 * i)) x) x) x) x from rfl,
    LibChebPow.pw_add K x (4 * i) (4 * i),
    show castN K (i + 1) = K.add K.one (castN K i) from rfl]
  unfold sub
  lib_ring

/-- **Odd Gaussian telescoping (exact).** Σ_{i<M} 2x^{k²}(1 − k²(1−x²)) = Σ_{i<M} x^{(2i)²} Q_{2i+1}(x) + 2M x^{(2M)²}. -/
theorem tel (x : R) : ∀ M, ssum K (oddT K x) M
    = K.add (ssum K (fun i => K.mul (pw K x ((2 * i) * (2 * i))) (qpoly K x i)) M)
        (K.mul (K.add (castN K M) (castN K M)) (pw K x ((2 * M) * (2 * M))))
  | 0 => by
    show K.zero = K.add K.zero (K.mul (K.add K.zero K.zero) (pw K x 0))
    show K.zero = K.add K.zero (K.mul (K.add K.zero K.zero) K.one)
    lib_ring
  | M + 1 => by
    show K.add (ssum K (oddT K x) M) (oddT K x M) = K.add (K.add (ssum K (fun i => K.mul (pw K x ((2 * i) * (2 * i))) (qpoly K x i)) M)
        (K.mul (pw K x ((2 * M) * (2 * M))) (qpoly K x M)))
        (K.mul (K.add (castN K (M + 1)) (castN K (M + 1))) (pw K x ((2 * (M + 1)) * (2 * (M + 1)))))
    rw [tel x M]
    have e1 : K.add (K.add (ssum K (fun i => K.mul (pw K x ((2 * i) * (2 * i))) (qpoly K x i)) M)
          (K.mul (K.add (castN K M) (castN K M)) (pw K x ((2 * M) * (2 * M))))) (oddT K x M)
        = K.add (ssum K (fun i => K.mul (pw K x ((2 * i) * (2 * i))) (qpoly K x i)) M)
          (K.add (oddT K x M) (K.mul (K.add (castN K M) (castN K M)) (pw K x ((2 * M) * (2 * M))))) := by lib_ring
    rw [e1, step K x M]
    lib_ring

section Ord
variable (O : ORing K)

theorem ssum_nn (f : Nat → R) (M : Nat) (hf : ∀ i, i < M → O.le K.zero (f i)) : ∀ n, n ≤ M → O.le K.zero (ssum K f n)
  | 0, _ => O.le_refl _
  | n + 1, hn => by
    show O.le K.zero (K.add (ssum K f n) (f n))
    have a := ssum_nn f M hf n (Nat.le_of_succ_le hn)
    have b := hf n hn
    have h := O.add_le_add _ _ _ _ (O.le_refl (ssum K f n)) b
    have e1 : K.add (ssum K f n) K.zero = ssum K f n := by lib_ring
    rw [e1] at h
    exact O.le_trans _ _ _ a h

/-- **Lower bound.** If x ≥ 0 and every Q_{2i+1}(x) ≥ 0 (i < M), then Σ_{i<M} oddT x i ≥ 2M x^{(2M)²} ≥ 0. -/
theorem tel_nonneg (x : R) (hx : O.le K.zero x) (M : Nat)
    (hq : ∀ i, i < M → O.le K.zero (qpoly K x i)) :
    O.le (K.mul (K.add (castN K M) (castN K M)) (pw K x ((2 * M) * (2 * M)))) (ssum K (oddT K x) M) := by
  rw [tel K x M]
  have s := ssum_nn K O (fun i => K.mul (pw K x ((2 * i) * (2 * i))) (qpoly K x i)) M
    (fun i hi => O.mul_nonneg _ _ (LibChebPow.pw_nonneg K O x hx _) (hq i hi)) M (Nat.le_refl M)
  have h := O.add_le_add _ _ _ _ (O.le_refl (K.mul (K.add (castN K M) (castN K M)) (pw K x ((2 * M) * (2 * M))))) s
  have e1 : K.add (K.mul (K.add (castN K M) (castN K M)) (pw K x ((2 * M) * (2 * M)))) K.zero
      = K.mul (K.add (castN K M) (castN K M)) (pw K x ((2 * M) * (2 * M))) := by lib_ring
  have e2 : K.add (K.mul (K.add (castN K M) (castN K M)) (pw K x ((2 * M) * (2 * M))))
        (ssum K (fun i => K.mul (pw K x ((2 * i) * (2 * i))) (qpoly K x i)) M)
      = K.add (ssum K (fun i => K.mul (pw K x ((2 * i) * (2 * i))) (qpoly K x i)) M)
        (K.mul (K.add (castN K M) (castN K M)) (pw K x ((2 * M) * (2 * M)))) := by lib_ring
  rw [e1, e2] at h; exact h

end Ord

end LibThetaOddTel

#print axioms LibThetaOddTel.tel
#print axioms LibThetaOddTel.tel_nonneg

lib/ThetaQ.lean

lean_all/lib/ThetaQ.lean

/-
  lib/ThetaQ.lean — T46 shared library (G1525a): theta series of x² + xy + (h+1)y² for a prime q = 4h + 3,
  from finite data at the primes below h + 3 (class number one).

  Q = k (2h) + 1 = 4h + 3 prime (LibEulerCrit.k m = 2(m+1)), χ(n) = (n/Q) (Legendre symbol, 0 on multiples of Q):
    legB_res       legB Q n = legB Q (n mod Q)
    chiQ_one, chiQ_mul, chiQ_cmult    χ is completely multiplicative
    norm_legB      p = N(x, y), Q ∤ p ⇒ p is a square mod Q                (4N = (2x + y)² + Q y²)
    inertQ         χ(p) = −1 ⇒ p ∣ N(x, y) ⇒ p ∣ x, p ∣ y                 (ClassOne.norm_of_root + norm_legB)
    prime_dataQ    split (χ = 1: QRRoot.root_q + ClassOne.norm_of_root), inert, ramified (Q = N(−1, 2))
    thetaQ_half    n > 0 ⇒ RH(n) = RH(1) · Σ_{d ∣ n} χ(d)                  (HeckeScale.eis_scale)
  Hypotheses left to each Q: primality of Q, the small-prime data (ClassOne.Data, by tables), χ(2) ≠ 1, RH(1).
  Lean 4 core only, proofs by hand, no axioms.
-/
import lib.ClassOne
import lib.QRRoot
import lib.Theta3

namespace LibThetaQ
open LibMod LibGcd LibEulerCrit LibZp LibIntBox LibNormPiA LibNormPiB LibNormPiF LibChi11 LibClassOne

theorem legB_res (M n : Nat) : QRChain.legB (M + 1) n = QRChain.legB (M + 1) (md (M + 1) n) := by
  have hrem : FTAChain.rem (M + 1) n = FTAChain.rem (M + 1) (md (M + 1) n) := by
    rw [LibFTAB.rem_eq_md, LibFTAB.rem_eq_md, md_md (Nat.succ_pos M)]
  exact LibQR11.sqS_congr (M + 1) n (md (M + 1) n) hrem (M + 1)

theorem legB_sq (M x : Nat) : QRChain.legB (M + 1) (x * x) = true :=
  (QRChain.legB_iff M (x * x)).2 ⟨x, rfl⟩

def chiQ (Q n : Nat) : Int := cond (Nat.beq (md Q n) 0) 0 (cond (QRChain.legB Q n) 1 (-1))

variable (M : Nat)

theorem mk_ne_of {a : Nat} (h : Nat.beq (md (k M + 1) a) 0 = false) : mk (k M) a ≠ (znR (k M)).zero := fun e => by
  have h1 := congrArg Subtype.val e
  have h2 : md (k M + 1) 0 = 0 := md_of_lt (pos (k M)) (pos (k M))
  have h0 : md (k M + 1) a = 0 := by rw [← h2]; exact h1
  rw [h0] at h; exact Bool.noConfusion h

theorem md_mul_zero_l {a : Nat} (b : Nat) (h : md (k M + 1) a = 0) : md (k M + 1) (a * b) = 0 := by
  rw [md_mul (pos (k M)), h, Nat.zero_mul]; exact md_of_lt (pos (k M)) (pos (k M))

theorem md_mul_zero_r (a : Nat) {b : Nat} (h : md (k M + 1) b = 0) : md (k M + 1) (a * b) = 0 := by
  rw [md_mul (pos (k M)), h, Nat.mul_zero]; exact md_of_lt (pos (k M)) (pos (k M))

theorem md_mul_ne (hp : Prime1 (k M)) {a b : Nat} (ha : Nat.beq (md (k M + 1) a) 0 = false)
    (hb : Nat.beq (md (k M + 1) b) 0 = false) : Nat.beq (md (k M + 1) (a * b)) 0 = false := by
  cases e : Nat.beq (md (k M + 1) (a * b)) 0
  · rfl
  · exfalso
    have hd : k M + 1 ∣ a * b := dvd_of_md_zero (Nat.eq_of_beq_eq_true e)
    cases euclid_prime hp hd with
    | inl h => rw [md_zero_of_dvd (pos (k M)) h] at ha; exact Bool.noConfusion ha
    | inr h => rw [md_zero_of_dvd (pos (k M)) h] at hb; exact Bool.noConfusion hb

theorem chiQ_mul (hp : Prime1 (k M)) (x y : Nat) : chiQ (k M + 1) (x * y) = chiQ (k M + 1) x * chiQ (k M + 1) y := by
  unfold chiQ
  cases ex : Nat.beq (md (k M + 1) x) 0
  · cases ey : Nat.beq (md (k M + 1) y) 0
    · rw [md_mul_ne M hp ex ey, LibLegMul.legB_mul M hp (mk_ne_of M ex) (mk_ne_of M ey)]
      cases QRChain.legB (k M + 1) x <;> cases QRChain.legB (k M + 1) y <;> rfl
    · rw [md_mul_zero_r M x (Nat.eq_of_beq_eq_true ey)]
      show (0 : Int) = _ * 0; lib_int
  · rw [md_mul_zero_l M y (Nat.eq_of_beq_eq_true ex)]
    show (0 : Int) = 0 * _; lib_int

theorem chiQ_one : chiQ (k M + 1) 1 = 1 := by
  unfold chiQ
  have h1 : md (k M + 1) 1 = 1 := md_of_lt (pos (k M)) (Nat.succ_lt_succ (Nat.succ_pos _))
  rw [h1, show (1 : Nat) = 1 * 1 from rfl, legB_sq]; rfl

theorem chiQ_cmult (hp : Prime1 (k M)) : LibDirTwist.CMult LibInt.intR (chiQ (k M + 1)) :=
  ⟨chiQ_one M, chiQ_mul M hp⟩

theorem chiQ_cases (n : Nat) :
    (chiQ (k M + 1) n = 1 ∧ Nat.beq (md (k M + 1) n) 0 = false ∧ QRChain.legB (k M + 1) n = true)
    ∨ (chiQ (k M + 1) n = -1 ∧ Nat.beq (md (k M + 1) n) 0 = false ∧ QRChain.legB (k M + 1) n = false)
    ∨ (chiQ (k M + 1) n = 0 ∧ k M + 1 ∣ n) := by
  unfold chiQ
  cases e : Nat.beq (md (k M + 1) n) 0
  · cases l : QRChain.legB (k M + 1) n
    · exact Or.inr (Or.inl ⟨rfl, rfl, rfl⟩)
    · exact Or.inl ⟨rfl, rfl, rfl⟩
  · exact Or.inr (Or.inr ⟨rfl, dvd_of_md_zero (Nat.eq_of_beq_eq_true e)⟩)

theorem norm_legB (hp : Prime1 (k M)) (c' : Nat) (hc : 4 * c' + 3 = k M + 1) {x y : Int} {p : Nat}
    (h : Nf (c' + 1) x y = (p : Int)) (hp0 : Nat.beq (md (k M + 1) p) 0 = false) :
    QRChain.legB (k M + 1) p = true := by
  have h4 : Nat.beq (md (k M + 1) 4) 0 = false := by
    cases e : Nat.beq (md (k M + 1) 4) 0
    · rfl
    · exfalso
      have hd : k M + 1 ∣ 2 * 2 := dvd_of_md_zero (Nat.eq_of_beq_eq_true e)
      have hd2 : k M + 1 ∣ 2 := by cases euclid_prime hp hd with | inl a => exact a | inr a => exact a
      have hle : k M + 1 ≤ 2 := LibPrimePow.le_of_dvd (by decide) hd2
      have h3 : 3 ≤ k M + 1 := Nat.succ_le_succ (show 2 ≤ 2 * (M + 1) from
        Nat.mul_le_mul_left 2 (Nat.succ_le_succ (Nat.zero_le M)))
      exact Nat.not_succ_le_self 2 (Nat.le_trans h3 hle)
  have hm := LibLegMul.legB_mul M hp (mk_ne_of M h4) (mk_ne_of M hp0)
  have hsq : IDv ((k M + 1 : Nat) : Int) (((4 * p : Nat) : Int) - (((2 * x + y).natAbs * (2 * x + y).natAbs : Nat) : Int)) := by
    refine ⟨y * y, ?_⟩
    rw [← sq_natAbs]
    show 4 * (p : Int) - (2 * x + y) * (2 * x + y) = _
    rw [← h, ← hc]; unfold Nf
    show 4 * (x * x + x * y + ((c' : Int) + 1) * (y * y)) - (2 * x + y) * (2 * x + y) = (4 * (c' : Int) + 3) * (y * y)
    lib_int
  have e := md_eq_of_idv (pos (k M)) hsq
  have hl4p : QRChain.legB (k M + 1) (4 * p) = true := by
    rw [legB_res, e, ← legB_res]; exact legB_sq _ _
  have hl4 : QRChain.legB (k M + 1) 4 = true := legB_sq _ 2
  rw [hm, hl4] at hl4p
  cases hb : QRChain.legB (k M + 1) p
  · rw [hb] at hl4p; exact Bool.noConfusion hl4p
  · rfl

theorem inertQ (hp : Prime1 (k M)) (c' : Nat) (hc : 4 * c' + 3 = k M + 1)
    (hdata : ∀ q, LibPrimePow.Prime q → q < c' + 3 → Data (c' + 1) q)
    {p : Nat} (hP : LibPrimePow.Prime p) (hχ : chiQ (k M + 1) p = -1) :
    ∀ x y : Int, IDv (p : Int) (Nf (c' + 1) x y) → IDv (p : Int) x ∧ IDv (p : Int) y := by
  intro x y hN
  have hp0 : 0 < p := Nat.lt_trans Nat.zero_lt_one hP.1
  cases (show IDv (p : Int) y ∨ ¬ IDv (p : Int) y from by
    cases e : dB p y
    · exact Or.inr (fun h => by rw [dB_of hp0 h] at e; exact Bool.noConfusion e)
    · exact Or.inl (dB_true e)) with
  | inl hy =>
    have hx2 : IDv (p : Int) (x * x) := LibNormPiC.idv_eq (idv_lin hN hy 1 (-(x + ((c' : Int) + 1) * y)))
      (by unfold Nf; show _ = 1 * (x * x + x * y + ((c' : Int) + 1) * (y * y)) + _; lib_int)
    exact ⟨(euclid_int hP.2 hx2).elim id id, hy⟩
  | inr hy =>
    exfalso
    obtain ⟨w, hw⟩ := inv_mod hP.2 hy
    have ht : IDv (p : Int) ((w * x) * (w * x) + w * x + ((c' + 1 : Nat) : Int)) :=
      LibNormPiC.idv_eq (idv_lin (LibNormPiC.idv_mul_l (w * w) hN) hw 1 (-(w * x + ((c' : Int) + 1) * (1 + w * y))))
        (by unfold Nf; show _ = 1 * (w * w * (x * x + x * y + ((c' : Int) + 1) * (y * y))) + _
            show (w * x) * (w * x) + w * x + ((c' : Int) + 1) = _; lib_int)
    obtain ⟨tn, htn⟩ := res_nat hp0 (w * x)
    have ht' : IDv (p : Int) ((tn : Int) * tn + tn + ((c' + 1 : Nat) : Int)) :=
      LibNormPiC.idv_eq (idv_lin ht htn 1 ((tn : Int) + w * x + 1)) (by lib_int)
    obtain ⟨u, v, huv⟩ := norm_of_root c' hdata hP tn ht'
    rcases chiQ_cases M p with ⟨h1, _, _⟩ | ⟨_, hne, hl⟩ | ⟨h0, _⟩
    · rw [h1] at hχ; exact absurd hχ (by decide)
    · rw [norm_legB M hp c' hc huv hne] at hl; exact Bool.noConfusion hl
    · rw [h0] at hχ; exact absurd hχ (by decide)

theorem prime_dataQ (hp : Prime1 (k M)) (c' : Nat) (hc : 4 * c' + 3 = k M + 1)
    (hdata : ∀ q, LibPrimePow.Prime q → q < c' + 3 → Data (c' + 1) q)
    (h2 : chiQ (k M + 1) 2 ≠ 1) (p : Nat) (hP : LibPrimePow.Prime p) :
    (chiQ (k M + 1) p = 1 ∧ (∃ u v : Int, Nf (c' + 1) u v = (p : Int)) ∧ ¬ p ∣ 4 * c' + 3)
      ∨ (chiQ (k M + 1) p = -1 ∧ ∀ x y : Int, IDv (p : Int) (Nf (c' + 1) x y) → IDv (p : Int) x ∧ IDv (p : Int) y)
      ∨ (chiQ (k M + 1) p = 0 ∧ (∃ u v : Int, Nf (c' + 1) u v = (p : Int)) ∧ ¬ p ∣ 2 ∧ p ∣ 4 * c' + 3) := by
  have hQ : LibPrimePow.Prime (k M + 1) := LibPrimeB.prime_of_prime1 (Nat.succ_le_succ (Nat.zero_le _)) hp
  rcases chiQ_cases M p with ⟨h1, hne, hl⟩ | ⟨h1, _, _⟩ | ⟨h0, hd⟩
  · have hpq : p ≠ k M + 1 := fun e => by
      rw [e, md_zero_of_dvd (pos (k M)) ⟨1, by lib_nat⟩] at hne; exact Bool.noConfusion hne
    have hodd : ¬ p ∣ 2 := fun hd2 => by
      have p2 : LibPrimePow.Prime 2 := LibPrimeB.prime_of_isPrimeB (by decide)
      cases p2.2 p hd2 with
      | inl e => exact absurd (e ▸ hP.1) (Nat.lt_irrefl 1)
      | inr e => rw [e] at h1; exact h2 h1
    have hndQ : ¬ p ∣ 4 * c' + 3 := fun hd => by
      rw [hc] at hd
      cases hQ.2 p hd with
      | inl e => exact absurd (e ▸ hP.1) (Nat.lt_irrefl 1)
      | inr e => exact hpq e
    obtain ⟨m, hm⟩ := LibTheta11.odd_prime_k hP hodd
    have hp1 : Prime1 (k m) := LibPrimeB.prime1_of_prime (hm ▸ hP)
    -- write Q = (2c'+1) + (2c'+1) + 1 for QRRoot
    have eQ : k M + 1 = (2 * c' + 1) + (2 * c' + 1) + 1 := by rw [← hc]; lib_nat
    have hPr : FTAChain.Pr ((2 * c' + 1) + (2 * c' + 1) + 1) := by
      rw [← eQ]; exact LibPrimeB.Pr_of_prime hQ
    have hleg : QRChain.legB ((2 * c' + 1) + (2 * c' + 1) + 1) (k m + 1) = true := by
      rw [← eQ, ← hm]; exact hl
    obtain ⟨t, ht⟩ := LibQRRoot.root_q m c' hp1 hPr (by rw [← eQ, ← hm]; exact hpq) hleg
    rw [← hm] at ht
    exact Or.inl ⟨h1, norm_of_root c' hdata hP t ht, hndQ⟩
  · exact Or.inr (Or.inl ⟨h1, inertQ M hp c' hc hdata hP h1⟩)
  · have e : p = k M + 1 := by
      cases hP.2 (k M + 1) hd with
      | inl e => exact absurd e (fun h' => Nat.noConfusion (show 2 * (M + 1) = 0 from Nat.succ.inj h'))
      | inr e => exact e.symm
    refine Or.inr (Or.inr ⟨h0, ⟨-1, 2, ?_⟩, ?_, by rw [hc, e]; exact ⟨1, by lib_nat⟩⟩)
    · rw [e, ← hc]; unfold Nf
      show (-1) * (-1) + (-1) * 2 + ((c' : Int) + 1) * (2 * 2) = 4 * (c' : Int) + 3; lib_int
    · intro hd2
      have := LibPrimePow.le_of_dvd (by decide) hd2
      rw [e] at this
      exact Nat.lt_irrefl 2 (Nat.lt_of_lt_of_le (by show 2 < 2 * (M + 1) + 1; exact Nat.succ_lt_succ (by
        show 1 < 2 * M + 2; exact Nat.succ_lt_succ (Nat.zero_lt_succ _))) this)

theorem thetaQ_half (hp : Prime1 (k M)) (c' : Nat) (hc : 4 * c' + 3 = k M + 1)
    (hdata : ∀ q, LibPrimePow.Prime q → q < c' + 3 → Data (c' + 1) q)
    (h2 : chiQ (k M + 1) 2 ≠ 1) {n : Nat} (hn : 0 < n) :
    (RH c' n : Int) = (RH c' 1 : Int) * LibEisGen.e LibInt.intR (chiQ (k M + 1)) (LibDivFun.one1 LibInt.intR) n :=
  LibHeckeScale.eis_scale LibInt.intR (chiQ (k M + 1)) (LibDivFun.one1 LibInt.intR) (chiQ_cmult M hp)
    (LibEisBridge.one1_cmult LibInt.intR) (fun n => (RH c' n : Int))
    (RH_eig c' (chiQ (k M + 1)) (prime_dataQ M hp c' hc hdata h2)) hn

theorem thetaQ (hp : Prime1 (k M)) (c' : Nat) (hc : 4 * c' + 3 = k M + 1)
    (hdata : ∀ q, LibPrimePow.Prime q → q < c' + 3 → Data (c' + 1) q)
    (h2 : chiQ (k M + 1) 2 ≠ 1) (h1 : RH c' 1 = 1) {n : Nat} (hn : 0 < n) :
    (RZ c' n : Int) = 2 * LibEisGen.e LibInt.intR (chiQ (k M + 1)) (LibDivFun.one1 LibInt.intR) n := by
  rw [RZ_eq_RH c' hn, LibTwoSqJacobi.cast_add, thetaQ_half M hp c' hc hdata h2 hn, h1]
  show (1 : Int) * _ + 1 * _ = _; lib_int

end LibThetaQ

#print axioms LibThetaQ.legB_res
#print axioms LibThetaQ.chiQ_mul
#print axioms LibThetaQ.chiQ_cmult
#print axioms LibThetaQ.chiQ_cases
#print axioms LibThetaQ.norm_legB
#print axioms LibThetaQ.inertQ
#print axioms LibThetaQ.prime_dataQ
#print axioms LibThetaQ.thetaQ_half
#print axioms LibThetaQ.thetaQ

lib/ThetaQPos.lean

lean_all/lib/ThetaQPos.lean

/-
  lib/ThetaQPos.lean — T46 shared library (G1538, agent3 T45 6f-A′-1): the pointwise polynomials of
  lib/ThetaOddTel are nonnegative, hence the odd Gaussian sum is nonnegative.

    Q_k(x) = (k − 1) − 2(k² − 1)x^{2k−1} + 2k²x^{2k+1} − (k + 1)x^{4k}        (k = 2i + 1, `LibThetaOddTel.qpoly`)
    D6 M x v       6x³ + 6N x²v + 3N(N−1) x v² + N(N−1)(N−2) v³,  N = M + 3   (tr3 = x^M · D6)
    core           0 ≤ x ≤ 1, α, γ ≥ 0, and αD6² − β·6x³·D6 − γ(6x³)² ≥ 0 (v = 1 − x)
                   ⇒ 0 ≤ x³ (α − βw − γw²),  w = x^{M+3}
                   (w·D6 ≤ 6x³ by the truncated binomial, then the concave transfer, then D6 ≥ 6)
    cert5          k = 2m + 5: 4C·(αD6² − βXD6 − γX²) = x⁵v[(2Cv − Bx)² + Ex²] + 4C Σ_{j=4}^{8} h_j x^{8−j}v^j,
                   B, C, E, h_j polynomials in m with nonnegative integer coefficients
    cert3          k = 3: 64·(…) = x⁴v[1152x(2x − 13v)² + 72v(8x − 49v)² + 956088v³] + 64 Σ_{j≥5} h_j x^{8−j}v^j
    q_nonneg       0 ≤ x ≤ 1 ⇒ 0 ≤ x^{(2i)²} Q_{2i+1}(x) for every i
    oddsum_lower   0 ≤ x ≤ 1 ⇒ 2M x^{(2M)²} ≤ Σ_{i<M} 2x^{k²}(1 − k²(1 − x²))   (k = 2i + 1; in particular E_M(x) ≥ 0)
  Lean 4 core only, proofs by hand, no axioms.
-/
import lib.ThetaOddTel
import lib.BinomTrunc3
import lib.RingZ

namespace LibThetaQPos
open DetChain LibRing YMChain LibBinomTrunc3 LibThetaOddTel

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

def D6 (M : Nat) (x v : R) : R :=
  K.add (K.add (K.add (K.mul (six K) (K.mul x (K.mul x x)))
    (K.mul (K.mul (six K) (castN K (M + 3))) (K.mul (K.mul x x) v)))
    (K.mul (K.mul (three K) (K.mul (castN K (M + 3)) (castN K (M + 2)))) (K.mul x (K.mul v v))))
    (K.mul (K.mul (castN K (M + 3)) (K.mul (castN K (M + 2)) (castN K (M + 1)))) (K.mul v (K.mul v v)))

/-- the hypothesis of `core`: αD² − β X D − γ X², X = 6x³, D = D6 M x (1 − x). -/
def hqE (M : Nat) (α β γ x : R) : R :=
  sub K (sub K (K.mul α (K.mul (D6 K M x (sub K K.one x)) (D6 K M x (sub K K.one x))))
      (K.mul β (K.mul (K.mul (six K) (K.mul x (K.mul x x))) (D6 K M x (sub K K.one x)))))
    (K.mul γ (K.mul (K.mul (six K) (K.mul x (K.mul x x))) (K.mul (six K) (K.mul x (K.mul x x)))))

theorem tr3_D6 (x v : R) (M : Nat) : tr3 K x v M = K.mul (pw K x M) (D6 K M x v) := by
  unfold tr3 D6
  try rw [show pw K x (M + 3) = K.mul (K.mul (K.mul (pw K x M) x) x) x from rfl]
  try rw [show pw K x (M + 2) = K.mul (K.mul (pw K x M) x) x from rfl]
  try rw [show pw K x (M + 1) = K.mul (pw K x M) x from rfl]
  lib_ring

theorem D6_eq (M : Nat) (x : R) : D6 K M x (sub K K.one x)
    = K.add (six K) (K.add (K.add (K.mul (K.mul (six K) (castN K M)) (K.mul (K.mul x x) (sub K K.one x)))
        (K.mul (K.mul (three K) (K.add (K.mul (castN K M) (castN K M)) (K.mul (castN K 5) (castN K M)))) (K.mul x (K.mul (sub K K.one x) (sub K K.one x)))))
        (K.mul (K.add (K.add (K.mul (castN K M) (K.mul (castN K M) (castN K M))) (K.mul (castN K 6) (K.mul (castN K M) (castN K M))))
          (K.mul (castN K 11) (castN K M))) (K.mul (sub K K.one x) (K.mul (sub K K.one x) (sub K K.one x))))) := by
  unfold D6 six three
  rw [show castN K (M + 3) = K.add K.one (K.add K.one (K.add K.one (castN K M))) from rfl,
    show castN K (M + 2) = K.add K.one (K.add K.one (castN K M)) from rfl,
    show castN K (M + 1) = K.add K.one (castN K M) from rfl]
  unfold sub
  lib_ringz

theorem pw_one' : ∀ n, pw K K.one n = K.one
  | 0 => rfl
  | n + 1 => by show K.mul (pw K K.one n) K.one = K.one; rw [pw_one' n]; lib_ring

section Ord
variable (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
  have e : K.add K.zero K.zero = K.zero := by lib_ring
  rw [e] at h; exact h

theorem mul_nn {a b : R} (ha : O.le K.zero a) (hb : O.le K.zero b) : O.le K.zero (K.mul a b) := O.mul_nonneg _ _ ha hb

theorem cast_nn (n : Nat) : O.le K.zero (castN K n) := LibLCHeat.castN_nonneg K O n

theorem one_le_add (a : R) (ha : O.le K.zero a) : O.le K.one (K.add K.one a) := by
  have h := O.add_le_add _ _ _ _ (O.le_refl K.one) ha
  have e : K.add K.one K.zero = K.one := by lib_ring
  rw [e] at h; exact h

theorem ne_zero_of_one_le (h10 : K.one ≠ K.zero) (c : R) (h : O.le K.one c) : c ≠ K.zero := by
  intro h0
  rw [h0] at h
  exact h10 (O.le_antisymm _ _ h (LibLCHeat.zero_le_one K O))

theorem one_le_six : O.le K.one (six K) := by
  have e : six K = K.add K.one (K.add (K.add (K.add K.one K.one) (K.add K.one K.one)) K.one) := by unfold six; lib_ring
  rw [e]
  exact one_le_add K O _ (add_nn K O (add_nn K O (add_nn K O (LibLCHeat.zero_le_one K O) (LibLCHeat.zero_le_one K O))
    (add_nn K O (LibLCHeat.zero_le_one K O) (LibLCHeat.zero_le_one K O))) (LibLCHeat.zero_le_one K O))

theorem sub_nn (x : R) (hx1 : O.le x K.one) : O.le K.zero (sub K K.one x) := by
  have h := O.add_le_add _ _ _ _ hx1 (O.le_refl (K.neg x))
  have e : K.add x (K.neg x) = K.zero := by lib_ring
  rw [e] at h; exact h

/-- cancel a factor c ≥ 1 on the right: 0 ≤ a·c ⇒ 0 ≤ a. -/
theorem cancel_r (h10 : K.one ≠ K.zero) (a c : R) (hc : O.le K.one c) (h : O.le K.zero (K.mul a c)) : O.le K.zero a := by
  have hc0 : O.le K.zero c := O.le_trans _ _ _ (LibLCHeat.zero_le_one K O) hc
  apply O.pos_cancel _ _ c hc0 (ne_zero_of_one_le K O h10 c hc)
  have e : K.mul K.zero c = K.zero := by lib_ring
  rw [e]; exact h

theorem D6_ge (M : Nat) (x : R) (hx0 : O.le K.zero x) (hx1 : O.le x K.one) : O.le (six K) (D6 K M x (sub K K.one x)) := by
  rw [D6_eq K M x]
  have hv := sub_nn K O x hx1
  have hs : O.le K.zero (six K) := O.le_trans _ _ _ (LibLCHeat.zero_le_one K O) (one_le_six K O)
  have hr : O.le K.zero (K.add (K.add (K.mul (K.mul (six K) (castN K M)) (K.mul (K.mul x x) (sub K K.one x)))
        (K.mul (K.mul (three K) (K.add (K.mul (castN K M) (castN K M)) (K.mul (castN K 5) (castN K M)))) (K.mul x (K.mul (sub K K.one x) (sub K K.one x)))))
        (K.mul (K.add (K.add (K.mul (castN K M) (K.mul (castN K M) (castN K M))) (K.mul (castN K 6) (K.mul (castN K M) (castN K M))))
          (K.mul (castN K 11) (castN K M))) (K.mul (sub K K.one x) (K.mul (sub K K.one x) (sub K K.one x))))) := by
    have h3 : O.le K.zero (three K) := by
      unfold three; exact add_nn K O (add_nn K O (LibLCHeat.zero_le_one K O) (LibLCHeat.zero_le_one K O)) (LibLCHeat.zero_le_one K O)
    repeat' (first | exact hx0 | exact hv | exact hs | exact h3 | exact cast_nn K O _ | apply add_nn K O | apply mul_nn K O)
  have h := O.add_le_add _ _ _ _ (O.le_refl (six K)) hr
  have e : K.add (six K) K.zero = six K := by lib_ring
  rw [e] at h; exact h

/-- **Core transfer.** -/
theorem core (h10 : K.one ≠ K.zero) (M : Nat) (α β γ x : R) (hα : O.le K.zero α) (hγ : O.le K.zero γ)
    (hx0 : O.le K.zero x) (hx1 : O.le x K.one) (hq : O.le K.zero (hqE K M α β γ x)) :
    O.le K.zero (K.mul (K.mul x (K.mul x x))
      (sub K (sub K α (K.mul β (pw K x (M + 3)))) (K.mul γ (K.mul (pw K x (M + 3)) (pw K x (M + 3)))))) := by
  have hv := sub_nn K O x hx1
  have ht := tr3_le K O x (sub K K.one x) hx0 hv M
  have e1 : K.add x (sub K K.one x) = K.one := by unfold sub; lib_ring
  rw [e1, pw_one', tr3_D6] at ht
  have hx3 : O.le K.zero (K.mul x (K.mul x x)) := mul_nn K O hx0 (mul_nn K O hx0 hx0)
  have h1 := O.mul_le_mul_right _ _ _ ht hx3
  have e2 : K.mul (K.mul (pw K x M) (D6 K M x (sub K K.one x))) (K.mul x (K.mul x x))
      = K.mul (pw K x (M + 3)) (D6 K M x (sub K K.one x)) := by
    show _ = K.mul (K.mul (K.mul (K.mul (pw K x M) x) x) x) (D6 K M x (sub K K.one x)); lib_ring
  have e3 : K.mul (K.mul (six K) K.one) (K.mul x (K.mul x x)) = K.mul (six K) (K.mul x (K.mul x x)) := by lib_ring
  rw [e2, e3] at h1
  have hD6 := D6_ge K O M x hx0 hx1
  have hs : O.le K.zero (six K) := O.le_trans _ _ _ (LibLCHeat.zero_le_one K O) (one_le_six K O)
  have c := conc_nonneg K O α β γ (pw K x (M + 3)) (D6 K M x (sub K K.one x)) (K.mul (six K) (K.mul x (K.mul x x)))
    hα hγ (LibChebPow.pw_nonneg K O x hx0 _) (O.le_trans _ _ _ hs hD6) (mul_nn K O hs hx3) h1 hq
  have e4 : K.mul (K.mul (D6 K M x (sub K K.one x)) (K.mul (six K) (K.mul x (K.mul x x))))
        (sub K (sub K α (K.mul β (pw K x (M + 3)))) (K.mul γ (K.mul (pw K x (M + 3)) (pw K x (M + 3)))))
      = K.mul (K.mul (K.mul (K.mul x (K.mul x x))
          (sub K (sub K α (K.mul β (pw K x (M + 3)))) (K.mul γ (K.mul (pw K x (M + 3)) (pw K x (M + 3)))))) (six K))
          (D6 K M x (sub K K.one x)) := by lib_ring
  rw [e4] at c
  have c1 := cancel_r K O h10 _ _ (O.le_trans _ _ _ (one_le_six K O) hD6) c
  exact cancel_r K O h10 _ _ (one_le_six K O) c1

end Ord

/-! ### k = 2m + 5 -/

def cB (cm : R) : R := (K.add (castN K 2880) (K.mul cm (K.add (castN K 2592) (K.mul cm (castN K 576)))))
def cC (cm : R) : R := (K.add (castN K 30240) (K.mul cm (K.add (castN K 57936) (K.mul cm (K.add (castN K 41376) (K.mul cm (K.add (castN K 13056) (K.mul cm (castN K 1536)))))))))
def cE (cm : R) : R := (K.add (castN K 414720) (K.mul cm (K.add (castN K 1755648) (K.mul cm (K.add (castN K 1880064) (K.mul cm (K.add (castN K 774144) (K.mul cm (castN K 110592)))))))))
def h4 (cm : R) : R := (K.add (castN K 308880) (K.mul cm (K.add (castN K 670152) (K.mul cm (K.add (castN K 579840) (K.mul cm (K.add (castN K 250080) (K.mul cm (K.add (castN K 53760) (K.mul cm (castN K 4608)))))))))))
def h5 (cm : R) : R := (K.add (castN K 1651968) (K.mul cm (K.add (castN K 4425216) (K.mul cm (K.add (castN K 4921920) (K.mul cm (K.add (castN K 2908224) (K.mul cm (K.add (castN K 962304) (K.mul cm (K.add (castN K 168960) (K.mul cm (castN K 12288)))))))))))))
def h6 (cm : R) : R := (K.add (castN K 3162240) (K.mul cm (K.add (castN K 9726336) (K.mul cm (K.add (castN K 12725536) (K.mul cm (K.add (castN K 9172544) (K.mul cm (K.add (castN K 3929344) (K.mul cm (K.add (castN K 998912) (K.mul cm (K.add (castN K 139264) (K.mul cm (castN K 8192)))))))))))))))
def h7 (cm : R) : R := (K.add (castN K 2903040) (K.mul cm (K.add (castN K 9755136) (K.mul cm (K.add (castN K 14021888) (K.mul cm (K.add (castN K 11175808) (K.mul cm (K.add (castN K 5334272) (K.mul cm (K.add (castN K 1524736) (K.mul cm (K.add (castN K 241664) (K.mul cm (castN K 16384)))))))))))))))
def h8 (cm : R) : R := (K.add (castN K 1016064) (K.mul cm (K.add (castN K 3588480) (K.mul cm (K.add (castN K 5423296) (K.mul cm (K.add (castN K 4546592) (K.mul cm (K.add (castN K 2283520) (K.mul cm (K.add (castN K 687104) (K.mul cm (K.add (castN K 114688) (K.mul cm (castN K 8192)))))))))))))))

def nn (cm : R) : R := K.add (K.mul (castN K 4) cm) (castN K 9)
def DD (cm x v : R) : R :=
  K.add (K.add (K.add (K.mul (castN K 6) (K.mul x (K.mul x x)))
    (K.mul (K.mul (castN K 6) (nn K cm)) (K.mul (K.mul x x) v)))
    (K.mul (K.mul (castN K 3) (K.mul (nn K cm) (K.add (K.mul (castN K 4) cm) (castN K 8)))) (K.mul x (K.mul v v))))
    (K.mul (K.mul (nn K cm) (K.mul (K.add (K.mul (castN K 4) cm) (castN K 8)) (K.add (K.mul (castN K 4) cm) (castN K 7)))) (K.mul v (K.mul v v)))

def al5 (cm : R) : R := K.add (K.mul (castN K 2) cm) (castN K 4)
def kk5 (cm : R) : R := K.add (K.mul (castN K 2) cm) (castN K 5)
def be5 (cm x : R) : R := sub K (K.mul (castN K 2) (sub K (K.mul (kk5 K cm) (kk5 K cm)) K.one)) (K.mul (K.mul (castN K 2) (K.mul (kk5 K cm) (kk5 K cm))) (K.mul x x))
def ga5 (cm x : R) : R := K.mul (K.add (K.mul (castN K 2) cm) (castN K 6)) (K.mul x x)

theorem cert5 (cm x : R) :
    K.mul (K.mul (castN K 4) (cC K cm))
      (sub K (sub K (K.mul (al5 K cm) (K.mul (DD K cm x (sub K K.one x)) (DD K cm x (sub K K.one x))))
        (K.mul (be5 K cm x) (K.mul (K.mul (castN K 6) (K.mul x (K.mul x x))) (DD K cm x (sub K K.one x)))))
        (K.mul (ga5 K cm x) (K.mul (K.mul (castN K 6) (K.mul x (K.mul x x))) (K.mul (castN K 6) (K.mul x (K.mul x x))))))
    = K.add (K.mul (K.mul (pw K x 5) (sub K K.one x))
        (K.add (K.mul (sub K (K.mul (K.mul (castN K 2) (cC K cm)) (sub K K.one x)) (K.mul (cB K cm) x))
                      (sub K (K.mul (K.mul (castN K 2) (cC K cm)) (sub K K.one x)) (K.mul (cB K cm) x)))
               (K.mul (cE K cm) (K.mul x x))))
      (K.mul (K.mul (castN K 4) (cC K cm))
        (K.add (K.add (K.add (K.add (K.mul (h4 K cm) (K.mul (pw K x 4) (pw K (sub K K.one x) 4)))
          (K.mul (h5 K cm) (K.mul (pw K x 3) (pw K (sub K K.one x) 5))))
          (K.mul (h6 K cm) (K.mul (pw K x 2) (pw K (sub K K.one x) 6))))
          (K.mul (h7 K cm) (K.mul x (pw K (sub K K.one x) 7))))
          (K.mul (h8 K cm) (pw K (sub K K.one x) 8)))) := by
  unfold cC cB cE h4 h5 h6 h7 h8 DD nn al5 be5 ga5 kk5 sub
  lib_ringz

theorem castN_4m6 (m : Nat) : castN K (4 * m + 6) = K.add (K.mul (castN K 4) (castN K m)) (castN K 6) := by
  rw [LibRingZ.castN_add', LibRingZ.castN_mul']

theorem D6_DD (m : Nat) (x v : R) : D6 K (4 * m + 6) x v = DD K (castN K m) x v := by
  unfold D6 DD nn six three
  rw [show castN K (4 * m + 6 + 3) = K.add K.one (K.add K.one (K.add K.one (castN K (4 * m + 6)))) from rfl,
    show castN K (4 * m + 6 + 2) = K.add K.one (K.add K.one (castN K (4 * m + 6))) from rfl,
    show castN K (4 * m + 6 + 1) = K.add K.one (castN K (4 * m + 6)) from rfl, castN_4m6]
  lib_ringz

/-! ### k = 3 -/

theorem cert3 (x : R) :
    K.mul (castN K 64) (hqE K 2 (castN K 2) (sub K (castN K 16) (K.mul (castN K 18) (K.mul x x))) (K.mul (castN K 4) (K.mul x x)) x)
    = K.add (K.mul (K.mul (pw K x 4) (sub K K.one x))
        (K.add (K.add (K.mul (K.mul (castN K 1152) x) (K.mul (sub K (K.mul (castN K 2) x) (K.mul (castN K 13) (sub K K.one x)))
                                                       (sub K (K.mul (castN K 2) x) (K.mul (castN K 13) (sub K K.one x)))))
          (K.mul (K.mul (castN K 72) (sub K K.one x)) (K.mul (sub K (K.mul (castN K 8) x) (K.mul (castN K 49) (sub K K.one x)))
                                                       (sub K (K.mul (castN K 8) x) (K.mul (castN K 49) (sub K K.one x))))))
          (K.mul (castN K 956088) (pw K (sub K K.one x) 3))))
      (K.mul (castN K 64)
        (K.add (K.add (K.add (K.mul (castN K 46080) (K.mul (pw K x 3) (pw K (sub K K.one x) 5)))
          (K.mul (castN K 50400) (K.mul (pw K x 2) (pw K (sub K K.one x) 6))))
          (K.mul (castN K 28800) (K.mul x (pw K (sub K K.one x) 7))))
          (K.mul (castN K 7200) (pw K (sub K K.one x) 8)))) := by
  unfold hqE D6 six three
  rw [show castN K (2 + 3) = castN K 5 from rfl, show castN K (2 + 2) = castN K 4 from rfl, show castN K (2 + 1) = castN K 3 from rfl]
  unfold sub
  lib_ringz

section Ord2
variable (O : ORing K)

theorem one_le_cast (n : Nat) : O.le K.one (castN K (n + 1)) := one_le_add K O _ (cast_nn K O n)

theorem pw_nn' (x : R) (hx : O.le K.zero x) (n : Nat) : O.le K.zero (pw K x n) := LibChebPow.pw_nonneg K O x hx n

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 mul_nn K O h h
  | inr h =>
    have hn : O.le K.zero (K.neg a) := by
      have h2 := O.add_le_add _ _ _ _ h (O.le_refl (K.neg a))
      have e1 : K.add a (K.neg a) = K.zero := by lib_ring
      have e2 : K.add K.zero (K.neg a) = K.neg a := by lib_ring
      rw [e1, e2] at h2; exact h2
    have e : K.mul (K.neg a) (K.neg a) = K.mul a a := by lib_ring
    rw [← e]; exact mul_nn K O hn hn

/-- k = 2m + 5: 0 ≤ x³ Q_k(x). -/
theorem q5 (h10 : K.one ≠ K.zero) (m : Nat) (x : R) (hx0 : O.le K.zero x) (hx1 : O.le x K.one) :
    O.le K.zero (K.mul (K.mul x (K.mul x x)) (qpoly K x (m + 2))) := by
  have hv := sub_nn K O x hx1
  have hcm := cast_nn K O m
  have hq : O.le K.zero (hqE K (4 * m + 6) (al5 K (castN K m)) (be5 K (castN K m) x) (ga5 K (castN K m) x) x) := by
    have hc : O.le K.one (K.mul (castN K 4) (cC K (castN K m))) := by
      have e : K.mul (castN K 4) (cC K (castN K m)) = K.add K.one (K.add (castN K 120959)
          (K.mul (castN K 4) (K.mul (castN K m) (K.add (castN K 57936) (K.mul (castN K m) (K.add (castN K 41376)
            (K.mul (castN K m) (K.add (castN K 13056) (K.mul (castN K m) (castN K 1536)))))))))) := by
        unfold cC; lib_ringz
      rw [e]
      apply one_le_add K O
      repeat' (first | exact hcm | exact cast_nn K O _ | apply add_nn K O | apply mul_nn K O)
    apply cancel_r K O h10 _ _ hc
    have e : K.mul (hqE K (4 * m + 6) (al5 K (castN K m)) (be5 K (castN K m) x) (ga5 K (castN K m) x) x)
          (K.mul (castN K 4) (cC K (castN K m)))
        = K.mul (K.mul (castN K 4) (cC K (castN K m)))
          (sub K (sub K (K.mul (al5 K (castN K m)) (K.mul (DD K (castN K m) x (sub K K.one x)) (DD K (castN K m) x (sub K K.one x))))
            (K.mul (be5 K (castN K m) x) (K.mul (K.mul (castN K 6) (K.mul x (K.mul x x))) (DD K (castN K m) x (sub K K.one x)))))
            (K.mul (ga5 K (castN K m) x) (K.mul (K.mul (castN K 6) (K.mul x (K.mul x x))) (K.mul (castN K 6) (K.mul x (K.mul x x)))))) := by
      unfold hqE
      rw [D6_DD]
      unfold six
      lib_ringz
    rw [e, cert5]
    repeat' (first | exact hx0 | exact hv | exact hcm | exact cast_nn K O _ | exact pw_nn' K O x hx0 _ | exact pw_nn' K O _ hv _ | exact sq_nn K O _ | apply add_nn K O | apply mul_nn K O)
  have hα : O.le K.zero (al5 K (castN K m)) := by
    unfold al5; repeat' (first | exact hcm | exact cast_nn K O _ | apply add_nn K O | apply mul_nn K O)
  have hγ : O.le K.zero (ga5 K (castN K m) x) := by
    unfold ga5; repeat' (first | exact hx0 | exact hcm | exact cast_nn K O _ | apply add_nn K O | apply mul_nn K O)
  have c := core K O h10 (4 * m + 6) _ _ _ x hα hγ hx0 hx1 hq
  have e : K.mul (K.mul x (K.mul x x)) (qpoly K x (m + 2))
      = K.mul (K.mul x (K.mul x x)) (sub K (sub K (al5 K (castN K m)) (K.mul (be5 K (castN K m) x) (pw K x (4 * m + 6 + 3))))
          (K.mul (ga5 K (castN K m) x) (K.mul (pw K x (4 * m + 6 + 3)) (pw K x (4 * m + 6 + 3))))) := by
    unfold qpoly al5 be5 ga5 kk5
    rw [show 4 * (m + 2) + 1 = 4 * m + 6 + 3 by lib_nat, show 4 * (m + 2) + 3 = (4 * m + 6 + 3) + 2 by lib_nat,
      show 8 * (m + 2) + 4 = ((4 * m + 6 + 3) + (4 * m + 6 + 3)) + 2 by lib_nat,
      show pw K x ((4 * m + 6 + 3) + 2) = K.mul (K.mul (pw K x (4 * m + 6 + 3)) x) x from rfl,
      show pw K x ((4 * m + 6 + 3) + (4 * m + 6 + 3) + 2) = K.mul (K.mul (pw K x ((4 * m + 6 + 3) + (4 * m + 6 + 3))) x) x from rfl,
      LibChebPow.pw_add K x (4 * m + 6 + 3) (4 * m + 6 + 3),
      show castN K (m + 2) = K.add K.one (K.add K.one (castN K m)) from rfl]
    unfold sub
    lib_ringz
  rw [e]; exact c

/-- k = 3: 0 ≤ x³ Q_3(x). -/
theorem q3 (h10 : K.one ≠ K.zero) (x : R) (hx0 : O.le K.zero x) (hx1 : O.le x K.one) :
    O.le K.zero (K.mul (K.mul x (K.mul x x)) (qpoly K x 1)) := by
  have hv := sub_nn K O x hx1
  have hq : O.le K.zero (hqE K 2 (castN K 2) (sub K (castN K 16) (K.mul (castN K 18) (K.mul x x))) (K.mul (castN K 4) (K.mul x x)) x) := by
    apply cancel_r K O h10 _ (castN K 64) (one_le_cast K O 63)
    rw [K.mul_comm, cert3]
    repeat' (first | exact hx0 | exact hv | exact cast_nn K O _ | exact pw_nn' K O x hx0 _ | exact pw_nn' K O _ hv _ | exact sq_nn K O _ | apply add_nn K O | apply mul_nn K O)
  have c := core K O h10 2 _ _ _ x (cast_nn K O 2) (mul_nn K O (cast_nn K O 4) (mul_nn K O hx0 hx0)) hx0 hx1 hq
  have e : K.mul (K.mul x (K.mul x x)) (qpoly K x 1)
      = K.mul (K.mul x (K.mul x x)) (sub K (sub K (castN K 2) (K.mul (sub K (castN K 16) (K.mul (castN K 18) (K.mul x x))) (pw K x (2 + 3))))
          (K.mul (K.mul (castN K 4) (K.mul x x)) (K.mul (pw K x (2 + 3)) (pw K x (2 + 3))))) := by
    unfold qpoly
    show K.mul (K.mul x (K.mul x x)) (K.add (K.add (K.add (K.add (castN K 1) (castN K 1)) (K.neg (K.mul (K.mul (K.mul (K.add (K.add K.one K.one) (K.add K.one K.one)) (K.add K.one K.one))
      (K.mul (castN K 1) (K.add (castN K 1) K.one))) (pw K x 5)))) (K.mul (K.mul (K.add K.one K.one) (K.mul (K.add (K.add (castN K 1) (castN K 1)) K.one) (K.add (K.add (castN K 1) (castN K 1)) K.one))) (pw K x 7)))
      (K.neg (K.mul (K.add (K.add (castN K 1) (castN K 1)) (K.add K.one K.one)) (pw K x 12)))) = _
    rw [show pw K x (2 + 3) = pw K x 5 from rfl, show pw K x 12 = K.mul (pw K x 5) (K.mul (pw K x 5) (K.mul x x)) by
      rw [show (12 : Nat) = 5 + 7 from rfl, LibChebPow.pw_add, show pw K x 7 = K.mul (K.mul (pw K x 5) x) x from rfl]; lib_ring,
      show pw K x 7 = K.mul (K.mul (pw K x 5) x) x from rfl]
    unfold sub
    lib_ringz
  rw [e]; exact c

/-- **Pointwise positivity.** 0 ≤ x ≤ 1 ⇒ 0 ≤ x^{(2i)²} Q_{2i+1}(x). -/
theorem q_nonneg (h10 : K.one ≠ K.zero) (x : R) (hx0 : O.le K.zero x) (hx1 : O.le x K.one) :
    ∀ i, O.le K.zero (K.mul (pw K x ((2 * i) * (2 * i))) (qpoly K x i))
  | 0 => by
    have e : K.mul (pw K x ((2 * 0) * (2 * 0))) (qpoly K x 0)
        = K.mul (K.mul (castN K 2) (K.mul x (K.mul x x))) (sub K K.one x) := by
      unfold qpoly
      show K.mul K.one (K.add (K.add (K.add (K.add K.zero K.zero) (K.neg (K.mul (K.mul (K.mul (K.add (K.add K.one K.one) (K.add K.one K.one)) (K.add K.one K.one))
        (K.mul K.zero (K.add K.zero K.one))) (pw K x 1)))) (K.mul (K.mul (K.add K.one K.one) (K.mul (K.add (K.add K.zero K.zero) K.one) (K.add (K.add K.zero K.zero) K.one))) (K.mul (K.mul (K.mul K.one x) x) x)))
        (K.neg (K.mul (K.add (K.add K.zero K.zero) (K.add K.one K.one)) (K.mul (K.mul (K.mul (K.mul K.one x) x) x) x)))) = _
      unfold sub; lib_ringz
    rw [e]
    exact mul_nn K O (mul_nn K O (cast_nn K O 2) (mul_nn K O hx0 (mul_nn K O hx0 hx0))) (sub_nn K O x hx1)
  | 1 => by
    have e : K.mul (pw K x ((2 * 1) * (2 * 1))) (qpoly K x 1) = K.mul x (K.mul (K.mul x (K.mul x x)) (qpoly K x 1)) := by
      show K.mul (K.mul (K.mul (K.mul (K.mul K.one x) x) x) x) (qpoly K x 1) = _; lib_ring
    rw [e]; exact mul_nn K O hx0 (q3 K O h10 x hx0 hx1)
  | m + 2 => by
    have e : K.mul (pw K x ((2 * (m + 2)) * (2 * (m + 2)))) (qpoly K x (m + 2))
        = K.mul (pw K x (4 * m * m + 16 * m + 13)) (K.mul (K.mul x (K.mul x x)) (qpoly K x (m + 2))) := by
      rw [show (2 * (m + 2)) * (2 * (m + 2)) = (4 * m * m + 16 * m + 13) + 3 by lib_nat, LibChebPow.pw_add,
        show pw K x 3 = K.mul (K.mul (K.mul K.one x) x) x from rfl]
      lib_ring
    rw [e]; exact mul_nn K O (pw_nn' K O x hx0 _) (q5 K O h10 m x hx0 hx1)

/-- **Odd Gaussian sum is nonnegative.** 0 ≤ x ≤ 1 ⇒ 2M x^{(2M)²} ≤ Σ_{i<M} 2x^{k²}(1 − k²(1 − x²)), k = 2i + 1. -/
theorem oddsum_lower (h10 : K.one ≠ K.zero) (x : R) (hx0 : O.le K.zero x) (hx1 : O.le x K.one) (M : Nat) :
    O.le (K.mul (K.add (castN K M) (castN K M)) (pw K x ((2 * M) * (2 * M)))) (ssum K (oddT K x) M) := by
  rw [tel K x M]
  have s := ssum_nn K O (fun i => K.mul (pw K x ((2 * i) * (2 * i))) (qpoly K x i)) M
    (fun i _ => q_nonneg K O h10 x hx0 hx1 i) M (Nat.le_refl M)
  have h := O.add_le_add _ _ _ _ (O.le_refl (K.mul (K.add (castN K M) (castN K M)) (pw K x ((2 * M) * (2 * M))))) s
  have e1 : K.add (K.mul (K.add (castN K M) (castN K M)) (pw K x ((2 * M) * (2 * M)))) K.zero
      = K.mul (K.add (castN K M) (castN K M)) (pw K x ((2 * M) * (2 * M))) := by lib_ring
  have e2 : K.add (K.mul (K.add (castN K M) (castN K M)) (pw K x ((2 * M) * (2 * M))))
        (ssum K (fun i => K.mul (pw K x ((2 * i) * (2 * i))) (qpoly K x i)) M)
      = K.add (ssum K (fun i => K.mul (pw K x ((2 * i) * (2 * i))) (qpoly K x i)) M)
        (K.mul (K.add (castN K M) (castN K M)) (pw K x ((2 * M) * (2 * M)))) := by lib_ring
  rw [e1, e2] at h; exact h

end Ord2

end LibThetaQPos

#print axioms LibThetaQPos.cert5
#print axioms LibThetaQPos.cert3
#print axioms LibThetaQPos.q_nonneg
#print axioms LibThetaQPos.oddsum_lower

lib/ThetaSubst.lean

lean_all/lib/ThetaSubst.lean

/-
  lib/ThetaSubst.lean — T46 shared library (G928): transporting the theta product formula along q ↦ Y (Y^{2r₀+2} = 0, e.g. Y = X⁴).

    ev_one, ev_X, ev_neg, ev_pw, ev_prodF, ev_qp    evaluation and 1, X, sign, powers, products
    theta_subst   at Y (Y^{2r₀+2} = 0) in R[X]/(X^{M′+1})
                  (Y²;Y²)_{2N}·Π_{n<N}(1 + c Y^{2n+1})² = Σ_{k≤K₀} cᵏ Y^{k²} + Σ_{k<K₀} c^{k+1} Y^{(k+1)²}    (c = ι(c₀), c₀² = 1)
  For Y = X⁴, θ(c; Q⁴) = Σ cᵏ Q^{4k²} (Σ Q^{d²} over even d = 2k) — the A_p side of agent3 G44d. Lean 4 core only, proofs by hand, no axioms.
-/
import lib.JTPThetaTP
import lib.TPEval

namespace LibThetaSubst
open DetChain LibRing LibSum LibSemi LibTPoly LibTPChar LibQBinom LibJTPFinite LibQPoch LibQVander LibTPEval LibJTPThetaTP LibNewton LibCvAlg

section Basic
variable {R : Type} (K : CRing R) (M M' : Nat) (Y : TP R M') (hY : pw (tpR K M') Y (M + 1) = (tpR K M').zero)

theorem ev_iota (c : R) : ev K M M' Y (iota K M c) = iota K M' c := by
  unfold ev
  rw [sum_head (tpR K M') _ M]
  have hz : sum (toSemi (tpR K M')) (fun j => (tpR K M').mul (iota K M' (coeff K M (iota K M c) (j + 1))) (pw (tpR K M') Y (j + 1))) (rng M)
      = (tpR K M').zero := by
    rw [sum_ext_mem (toSemi (tpR K M')) _ (fun _ => (tpR K M').zero) _ (fun j hj => by
      show (tpR K M').mul (iota K M' (coeff K M (build M (dl K c)) (j + 1))) _ = _
      rw [coeff_build K M _ (j + 1) (LibBinom.mem_rng hj)]
      show (tpR K M').mul (iota K M' K.zero) _ = _
      rw [iota_zero]; lib_ring)]
    exact sum_zero (toSemi (tpR K M')) _
  rw [hz]
  show (tpR K M').add ((tpR K M').mul (iota K M' (coeff K M (build M (dl K c)) 0)) (tpR K M').one) (tpR K M').zero = _
  rw [coeff_build K M _ 0 (Nat.zero_le M)]
  show (tpR K M').add ((tpR K M').mul (iota K M' c) (tpR K M').one) (tpR K M').zero = _
  lib_ring

theorem ev_one : ev K M M' Y (tpR K M).one = (tpR K M').one := ev_iota K M M' Y K.one

include hY in
theorem ev_X (hM : 1 ≤ M) : ev K M M' Y (X K M) = Y := by
  have h := ev_X_mul K M M' Y hY (tpR K M).one
  rw [(tpR K M).mul_comm, (tpR K M).one_mul, ev_one] at h
  rw [h]; lib_ring

include hY in
theorem ev_pw (A : TP R M) : ∀ n, ev K M M' Y (pw (tpR K M) A n) = pw (tpR K M') (ev K M M' Y A) n
  | 0 => ev_one K M M' Y
  | n + 1 => by
    show ev K M M' Y ((tpR K M).mul (pw (tpR K M) A n) A) = (tpR K M').mul _ _
    rw [ev_mul K M M' Y hY, ev_pw A n]

theorem ev_neg (A : TP R M) : ev K M M' Y ((tpR K M).neg A) = (tpR K M').neg (ev K M M' Y A) := by
  have e : (tpR K M).neg A = (tpR K M).mul (iota K M (K.neg K.one)) A := by
    rw [iota_neg, iota_one]; lib_ring
  rw [e, ev_iota_mul, iota_neg, iota_one]; lib_ring

include hY in
theorem ev_prodF (f : Nat → TP R M) : ∀ n, ev K M M' Y (prodF (tpR K M) f n) = prodF (tpR K M') (fun i => ev K M M' Y (f i)) n
  | 0 => ev_one K M M' Y
  | n + 1 => by
    show ev K M M' Y ((tpR K M).mul (prodF (tpR K M) f n) (f n)) = (tpR K M').mul _ _
    rw [ev_mul K M M' Y hY, ev_prodF f n]

end Basic

section Main
variable {R : Type} (K : CRing R) (r0 M' : Nat) (Y : TP R M') (hY : pw (tpR K M') Y (r0 + r0 + 1 + 1) = (tpR K M').zero)

include hY in
/-- **Substitution version of the theta product formula.** -/
theorem theta_subst (K0 : Nat) (hK : r0 + r0 + 1 < (K0 + 1) * (K0 + 1)) (c0 : R) (hc0 : K.mul c0 c0 = K.one) :
    (tpR K M').mul (qp (tpR K M') ((tpR K M').mul Y Y) ((K0 + r0) + (K0 + r0)))
      (prodF (tpR K M') (fun n => (tpR K M').mul
        ((tpR K M').add (tpR K M').one ((tpR K M').mul (pw (tpR K M') Y (n + n + 1)) (iota K M' c0)))
        ((tpR K M').add (tpR K M').one ((tpR K M').mul (pw (tpR K M') Y (n + n + 1)) (iota K M' c0)))) (K0 + r0))
    = (tpR K M').add
        (sum (toSemi (tpR K M')) (fun k => (tpR K M').mul (pw (tpR K M') (iota K M' c0) k) (pw (tpR K M') Y (k * k))) (rng (K0 + 1)))
        (sum (toSemi (tpR K M')) (fun k => (tpR K M').mul (pw (tpR K M') (iota K M' c0) (k + 1)) (pw (tpR K M') Y ((k + 1) * (k + 1)))) (rng K0)) := by
  have hc : (tpR K (r0 + r0 + 1)).mul (iota K (r0 + r0 + 1) c0) (iota K (r0 + r0 + 1) c0) = (tpR K (r0 + r0 + 1)).one := by
    rw [← iota_mul, hc0]; rfl
  have h := congrArg (ev K (r0 + r0 + 1) M' Y) (theta_c K r0 K0 hK (iota K (r0 + r0 + 1) c0) hc)
  have hX := ev_X K (r0 + r0 + 1) M' Y hY (Nat.succ_le_succ (Nat.zero_le _))
  unfold qp at h ⊢
  rw [ev_mul K _ M' Y hY, ev_prodF K _ M' Y hY, ev_prodF K _ M' Y hY, ev_add, ev_sum, ev_sum] at h
  -- factor by factor and term by term
  have f1 : ∀ i, ev K (r0 + r0 + 1) M' Y (sub (tpR K (r0 + r0 + 1)) (tpR K (r0 + r0 + 1)).one
        (pw (tpR K (r0 + r0 + 1)) ((tpR K (r0 + r0 + 1)).mul (X K (r0 + r0 + 1)) (X K (r0 + r0 + 1))) (i + 1)))
      = sub (tpR K M') (tpR K M').one (pw (tpR K M') ((tpR K M').mul Y Y) (i + 1)) := by
    intro i
    show ev K _ M' Y ((tpR K _).add _ ((tpR K _).neg _)) = (tpR K M').add _ ((tpR K M').neg _)
    rw [ev_add, ev_one, ev_neg, ev_pw K _ M' Y hY, ev_mul K _ M' Y hY, hX]
  have f2 : ∀ n, ev K (r0 + r0 + 1) M' Y ((tpR K (r0 + r0 + 1)).mul
        ((tpR K (r0 + r0 + 1)).add (tpR K (r0 + r0 + 1)).one ((tpR K (r0 + r0 + 1)).mul (pw (tpR K (r0 + r0 + 1)) (X K (r0 + r0 + 1)) (n + n + 1)) (iota K (r0 + r0 + 1) c0)))
        ((tpR K (r0 + r0 + 1)).add (tpR K (r0 + r0 + 1)).one ((tpR K (r0 + r0 + 1)).mul (pw (tpR K (r0 + r0 + 1)) (X K (r0 + r0 + 1)) (n + n + 1)) (iota K (r0 + r0 + 1) c0))))
      = (tpR K M').mul
        ((tpR K M').add (tpR K M').one ((tpR K M').mul (pw (tpR K M') Y (n + n + 1)) (iota K M' c0)))
        ((tpR K M').add (tpR K M').one ((tpR K M').mul (pw (tpR K M') Y (n + n + 1)) (iota K M' c0))) := by
    intro n
    rw [ev_mul K _ M' Y hY, ev_add, ev_one, ev_mul K _ M' Y hY, ev_pw K _ M' Y hY, hX, ev_iota]
  have t1 : ∀ k, ev K (r0 + r0 + 1) M' Y ((tpR K (r0 + r0 + 1)).mul (pw (tpR K (r0 + r0 + 1)) (iota K (r0 + r0 + 1) c0) k) (pw (tpR K (r0 + r0 + 1)) (X K (r0 + r0 + 1)) (k * k)))
      = (tpR K M').mul (pw (tpR K M') (iota K M' c0) k) (pw (tpR K M') Y (k * k)) := by
    intro k
    rw [ev_mul K _ M' Y hY, ev_pw K _ M' Y hY, ev_pw K _ M' Y hY, hX, ev_iota]
  have t2 : ∀ k, ev K (r0 + r0 + 1) M' Y ((tpR K (r0 + r0 + 1)).mul (pw (tpR K (r0 + r0 + 1)) (iota K (r0 + r0 + 1) c0) (k + 1)) (pw (tpR K (r0 + r0 + 1)) (X K (r0 + r0 + 1)) ((k + 1) * (k + 1))))
      = (tpR K M').mul (pw (tpR K M') (iota K M' c0) (k + 1)) (pw (tpR K M') Y ((k + 1) * (k + 1))) := fun k => t1 (k + 1)
  rw [prodF_ext (tpR K M') _ _ f1, prodF_ext (tpR K M') _ _ f2, sum_ext (toSemi (tpR K M')) _ _ t1, sum_ext (toSemi (tpR K M')) _ _ t2] at h
  exact h

end Main

end LibThetaSubst

#print axioms LibThetaSubst.ev_iota
#print axioms LibThetaSubst.ev_X
#print axioms LibThetaSubst.ev_pw
#print axioms LibThetaSubst.ev_neg
#print axioms LibThetaSubst.ev_prodF
#print axioms LibThetaSubst.theta_subst

lib/ThetaZW.lean

lean_all/lib/ThetaZW.lean

/-
  lib/ThetaZW.lean — T46 shared library (G1087a): Jacobi triple product at z and w = z⁻¹ (any zw = 1), finite and truncated.

    prod_zw     any ring, zw = 1: Π_{n<N}(1 + q^{2n+1}z)(1 + q^{2n+1}w) = Σ_{k≤N} zᵏ corr_{N,k} + Σ_{k≤N} w^{k+1} corr_{N,k+1}
    theta_zw    in R[q]/(q^{2r₀+2}), N = K₀ + r₀, (K₀+1)² > 2r₀+1, z w = 1 (z, w any elements, e.g. z = −c·X^b with c a unit constant):
                (q²;q²)_{2N}·Π_{n<N}(1 + q^{2n+1}z)(1 + q^{2n+1}w) = Σ_{k≤K₀} zᵏ q^{k²} + Σ_{k<K₀} w^{k+1} q^{(k+1)²}
  Generalizes JTPTheta.prod_c / JTPThetaTP.theta_c (z = w = c, c² = 1); the first triple product of the quintuple product (G1087).
  Lean 4 core only, proofs by hand, no axioms.
-/
import lib.JTPThetaTP

namespace LibThetaZW
open DetChain LibRing LibSum LibSemi LibQBinom LibJTPFinite LibTPoly LibTPChar LibMacMahon LibQPoch LibTPTrunc LibJTPTrunc LibJTPSum LibDiagSum LibJTPTheta LibJTPThetaTP

section Any
variable {S : Type} (T : CRing S)

theorem term_zw (q z w : S) (hzw : T.mul z w = T.one) (N b k : Nat) :
    T.mul (T.mul (T.mul (pw T q ((b + k) * (b + k))) (gb T (T.mul q q) N (b + k))) (pw T z (b + k)))
      (T.mul (T.mul (pw T q (b * b)) (gb T (T.mul q q) N b)) (pw T w b))
    = T.mul (pw T z k) (T.mul (pw T q ((b + k) * (b + k) + b * b)) (T.mul (gb T (T.mul q q) N (b + k)) (gb T (T.mul q q) N b))) := by
  have hcc : T.mul (pw T z b) (pw T w b) = T.one := by rw [← pw_mul', hzw, pw_one']
  rw [pw_add' T z b k, pw_add' T q ((b + k) * (b + k)) (b * b)]
  generalize pw T q ((b + k) * (b + k)) = A
  generalize pw T q (b * b) = B
  generalize gb T (T.mul q q) N (b + k) = g1
  generalize gb T (T.mul q q) N b = g2
  generalize pw T z k = ck
  have e : T.mul (T.mul (T.mul A g1) (T.mul (pw T z b) ck)) (T.mul (T.mul B g2) (pw T w b))
      = T.mul (T.mul (pw T z b) (pw T w b)) (T.mul ck (T.mul (T.mul A B) (T.mul g1 g2))) := by lib_ring
  rw [e, hcc]; lib_ring

/-- **z, w specialization (zw = 1).** -/
theorem prod_zw (q z w : S) (hzw : T.mul z w = T.one) (N : Nat) :
    prodF T (fun n => T.mul (T.add T.one (T.mul (pw T q (n + n + 1)) z)) (T.add T.one (T.mul (pw T q (n + n + 1)) w))) N
      = T.add (sum (toSemi T) (fun k => T.mul (pw T z k) (corr T q N k)) (rng (N + 1)))
          (sum (toSemi T) (fun k => T.mul (pw T w (k + 1)) (corr T q N (k + 1))) (rng (N + 1))) := by
  rw [jtp_two]
  let F : Nat → Nat → S := fun a b => T.mul (T.mul (T.mul (pw T q (a * a)) (gb T (T.mul q q) N a)) (pw T z a))
    (T.mul (T.mul (pw T q (b * b)) (gb T (T.mul q q) N b)) (pw T w b))
  have hA : ∀ a b, N + 1 ≤ a → F a b = T.zero := by
    intro a b h
    show T.mul (T.mul (T.mul _ (gb T (T.mul q q) N a)) _) _ = _
    rw [gb_above T _ N a h]; lib_ring
  have hB : ∀ a b, N + 1 ≤ b → F a b = T.zero := by
    intro a b h
    show T.mul _ (T.mul (T.mul _ (gb T (T.mul q q) N b)) _) = _
    rw [gb_above T _ N b h]; lib_ring
  have hd := diag_split T F (N + 1) hA hB
  refine hd.trans ?_
  have h1 : sum (toSemi T) (fun k => sum (toSemi T) (fun b => F (b + k) b) (rng (N + 1))) (rng (N + 1))
      = sum (toSemi T) (fun k => T.mul (pw T z k) (corr T q N k)) (rng (N + 1)) := by
    refine sum_ext (toSemi T) _ _ (fun k => ?_) _
    unfold corr
    refine Eq.trans ?_ (mul_sum (toSemi T) _ _ _).symm
    exact sum_ext (toSemi T) _ _ (fun b => term_zw T q z w hzw N b k) _
  have h2 : sum (toSemi T) (fun k => sum (toSemi T) (fun a => F a (a + k + 1)) (rng (N + 1))) (rng (N + 1))
      = sum (toSemi T) (fun k => T.mul (pw T w (k + 1)) (corr T q N (k + 1))) (rng (N + 1)) := by
    refine sum_ext (toSemi T) _ _ (fun k => ?_) _
    unfold corr
    refine Eq.trans ?_ (mul_sum (toSemi T) _ _ _).symm
    refine sum_ext (toSemi T) _ _ (fun a => ?_) _
    exact (T.mul_comm _ _).trans (term_zw T q w z ((T.mul_comm w z).trans hzw) N a (k + 1))
  rw [h1, h2]

end Any

section Trunc
variable {R : Type} (K : CRing R) (r0 : Nat)

/-- **Jacobi triple product at z, w = z⁻¹ (truncated version).** -/
theorem theta_zw (K0 : Nat) (hK : r0 + r0 + 1 < (K0 + 1) * (K0 + 1)) (z w : TP R (r0 + r0 + 1))
    (hzw : (tpR K (r0 + r0 + 1)).mul z w = (tpR K (r0 + r0 + 1)).one) :
    (tpR K (r0 + r0 + 1)).mul (qp (tpR K (r0 + r0 + 1)) ((tpR K (r0 + r0 + 1)).mul (X K (r0 + r0 + 1)) (X K (r0 + r0 + 1))) ((K0 + r0) + (K0 + r0)))
      (prodF (tpR K (r0 + r0 + 1)) (fun n => (tpR K (r0 + r0 + 1)).mul
        ((tpR K (r0 + r0 + 1)).add (tpR K (r0 + r0 + 1)).one ((tpR K (r0 + r0 + 1)).mul (pw (tpR K (r0 + r0 + 1)) (X K (r0 + r0 + 1)) (n + n + 1)) z))
        ((tpR K (r0 + r0 + 1)).add (tpR K (r0 + r0 + 1)).one ((tpR K (r0 + r0 + 1)).mul (pw (tpR K (r0 + r0 + 1)) (X K (r0 + r0 + 1)) (n + n + 1)) w))) (K0 + r0))
    = (tpR K (r0 + r0 + 1)).add
        (sum (toSemi (tpR K (r0 + r0 + 1))) (fun k => (tpR K (r0 + r0 + 1)).mul (pw (tpR K (r0 + r0 + 1)) z k) (pw (tpR K (r0 + r0 + 1)) (X K (r0 + r0 + 1)) (k * k))) (rng (K0 + 1)))
        (sum (toSemi (tpR K (r0 + r0 + 1))) (fun k => (tpR K (r0 + r0 + 1)).mul (pw (tpR K (r0 + r0 + 1)) w (k + 1)) (pw (tpR K (r0 + r0 + 1)) (X K (r0 + r0 + 1)) ((k + 1) * (k + 1)))) (rng K0)) := by
  rw [prod_zw (tpR K (r0 + r0 + 1)) (X K (r0 + r0 + 1)) z w hzw (K0 + r0)]
  have hz : ∀ j, corr (tpR K (r0 + r0 + 1)) (X K (r0 + r0 + 1)) (K0 + r0) (K0 + 1 + j) = (tpR K (r0 + r0 + 1)).zero :=
    fun j => corr_high K r0 _ _ (Nat.lt_of_lt_of_le hK (sq_mono K0 j))
  -- first sum: k < K₀+1 and the rest (0)
  have e1 : sum (toSemi (tpR K (r0 + r0 + 1))) (fun k => (tpR K (r0 + r0 + 1)).mul (pw (tpR K (r0 + r0 + 1)) z k) (corr (tpR K (r0 + r0 + 1)) (X K (r0 + r0 + 1)) (K0 + r0) k)) (rng (K0 + r0 + 1))
      = sum (toSemi (tpR K (r0 + r0 + 1))) (fun k => (tpR K (r0 + r0 + 1)).mul (pw (tpR K (r0 + r0 + 1)) z k) (corr (tpR K (r0 + r0 + 1)) (X K (r0 + r0 + 1)) (K0 + r0) k)) (rng (K0 + 1)) := by
    rw [show K0 + r0 + 1 = (K0 + 1) + r0 by lib_nat, sum_rng_add]
    rw [sum_ext (toSemi (tpR K (r0 + r0 + 1)))
      (fun j => (tpR K (r0 + r0 + 1)).mul (pw (tpR K (r0 + r0 + 1)) z (K0 + 1 + j)) (corr (tpR K (r0 + r0 + 1)) (X K (r0 + r0 + 1)) (K0 + r0) (K0 + 1 + j)))
      (fun _ => (tpR K (r0 + r0 + 1)).zero) (fun j => by rw [hz]; lib_ring)]
    exact (congrArg ((toSemi (tpR K (r0 + r0 + 1))).add _) (sum_zero (toSemi (tpR K (r0 + r0 + 1))) _)).trans (((tpR K (r0 + r0 + 1)).add_comm _ _).trans ((tpR K (r0 + r0 + 1)).zero_add _))
  -- second sum: k < K₀ and the rest (0)
  have e2 : sum (toSemi (tpR K (r0 + r0 + 1))) (fun k => (tpR K (r0 + r0 + 1)).mul (pw (tpR K (r0 + r0 + 1)) w (k + 1)) (corr (tpR K (r0 + r0 + 1)) (X K (r0 + r0 + 1)) (K0 + r0) (k + 1))) (rng (K0 + r0 + 1))
      = sum (toSemi (tpR K (r0 + r0 + 1))) (fun k => (tpR K (r0 + r0 + 1)).mul (pw (tpR K (r0 + r0 + 1)) w (k + 1)) (corr (tpR K (r0 + r0 + 1)) (X K (r0 + r0 + 1)) (K0 + r0) (k + 1))) (rng K0) := by
    rw [show K0 + r0 + 1 = K0 + (r0 + 1) by lib_nat, sum_rng_add]
    rw [sum_ext (toSemi (tpR K (r0 + r0 + 1)))
      (fun j => (tpR K (r0 + r0 + 1)).mul (pw (tpR K (r0 + r0 + 1)) w (K0 + j + 1)) (corr (tpR K (r0 + r0 + 1)) (X K (r0 + r0 + 1)) (K0 + r0) (K0 + j + 1)))
      (fun _ => (tpR K (r0 + r0 + 1)).zero) (fun j => by
      rw [show K0 + j + 1 = K0 + 1 + j by lib_nat, hz]; lib_ring)]
    exact (congrArg ((toSemi (tpR K (r0 + r0 + 1))).add _) (sum_zero (toSemi (tpR K (r0 + r0 + 1))) _)).trans (((tpR K (r0 + r0 + 1)).add_comm _ _).trans ((tpR K (r0 + r0 + 1)).zero_add _))
  rw [e1, e2, (tpR K (r0 + r0 + 1)).distrib]
  refine congrArg (fun z => (tpR K (r0 + r0 + 1)).add z _) (jtp_weighted K K0 r0 (fun k => pw (tpR K (r0 + r0 + 1)) z k)).symm |>.trans ?_
  refine congrArg ((tpR K (r0 + r0 + 1)).add _) ?_
  refine Eq.trans (mul_sum (toSemi (tpR K (r0 + r0 + 1))) _ _ _) ?_
  refine sum_ext_mem (toSemi (tpR K (r0 + r0 + 1))) _ _ _ (fun k hk => ?_)
  have h := jtp_fixed K K0 r0 (k + 1) (LibBinom.mem_rng hk)
  rw [← h]
  show (tpR K (r0 + r0 + 1)).mul _ ((tpR K (r0 + r0 + 1)).mul _ _) = _
  generalize qp _ _ _ = P
  generalize corr _ _ _ _ = Q
  lib_ring

end Trunc

end LibThetaZW

#print axioms LibThetaZW.prod_zw
#print axioms LibThetaZW.theta_zw

lib/Thue.lean

lean_all/lib/Thue.lean

/-
  lib/Thue.lean — T46 shared library (G1520b): Thue's lemma over ℤ.

    idv_of_md       A ≡ B (mod P)  ⇒  P ∣ A − B   (Int)
    natAbs_sub_le   i, i' ≤ s ⇒ |i − i'| ≤ s
    int_eq_of_sub   x − y = 0 ⇒ x = y
    thue            P > 0, any t:  ∃ (a, b) ≠ (0, 0), |a|, |b| ≤ ⌊√P⌋, P ∣ a − t·b
  (Pigeonhole on (i, j) ↦ i − t·j mod P over [0, ⌊√P⌋]², (⌊√P⌋ + 1)² > P.)
  Lean 4 core only, proofs by hand, no axioms.
-/
import lib.NormPiF

namespace LibThue
open LibMod LibGcd LibNormPiA

theorem idv_of_md {P A B : Nat} (h : md P A = md P B) : IDv (P : Int) ((A : Int) - (B : Int)) := by
  have sA := spec P A
  have sB := spec P B
  generalize dv P A = qa at sA
  generalize dv P B = qb at sB
  rw [h] at sA
  generalize md P B = r at sA sB
  refine ⟨(qa : Int) - (qb : Int), ?_⟩
  rw [sA, sB]
  show (P : Int) * (qa : Int) + (r : Int) - ((P : Int) * (qb : Int) + (r : Int)) = _
  lib_int

theorem natAbs_sub_le {i i' s : Nat} (hi : i ≤ s) (hi' : i' ≤ s) : ((i : Int) - (i' : Int)).natAbs ≤ s := by
  cases Nat.lt_or_ge i i' with
  | inl hlt =>
    obtain ⟨d, hd⟩ := LibHeckeRel.ex_add (Nat.le_of_lt hlt)
    have e : (i : Int) - (i' : Int) = -(d : Int) := by
      rw [hd]; show (i : Int) - ((d : Int) + (i : Int)) = _; lib_int
    rw [e, LibNormPiF.natAbs_neg']
    exact Nat.le_trans (by rw [hd]; exact Nat.le_add_right d i) hi'
  | inr hge =>
    obtain ⟨d, hd⟩ := LibHeckeRel.ex_add hge
    have e : (i : Int) - (i' : Int) = (d : Int) := by
      rw [hd]; show ((d : Int) + (i' : Int)) - (i' : Int) = _; lib_int
    rw [e]
    exact Nat.le_trans (by rw [hd]; exact Nat.le_add_right d i') hi

theorem int_eq_of_sub {x y : Int} (h : x - y = 0) : x = y := by
  have e : x = (x - y) + y := by lib_int
  rw [h] at e; rw [e]; lib_int

theorem thue (P t : Nat) (hP : 0 < P) :
    ∃ a b : Int, (a ≠ 0 ∨ b ≠ 0) ∧ a.natAbs ≤ LibISqrt.isqrt P ∧ b.natAbs ≤ LibISqrt.isqrt P
      ∧ IDv (P : Int) (a - (t : Int) * b) := by
  have hs := LibISqrt.isqrt_spec P
  generalize LibISqrt.isqrt P = s at hs ⊢
  have hL : 0 < s + 1 := Nat.zero_lt_succ s
  obtain ⟨k, k', hk, hk', hkk, hfe⟩ := LibPigeonhole.collide ((s + 1) * (s + 1)) P
    (fun k => md P (dv (s + 1) k + t * (P - 1) * md (s + 1) k)) (fun k _ => md_lt hP _) hs.2
  have di := LibIntBox.dv_lt hk
  have di' := LibIntBox.dv_lt hk'
  have mj := md_lt hL k
  have mj' := md_lt hL k'
  generalize hi : dv (s + 1) k = i at di hfe
  generalize hi' : dv (s + 1) k' = i' at di' hfe
  generalize hj : md (s + 1) k = j at mj hfe
  generalize hj' : md (s + 1) k' = j' at mj' hfe
  have hD := idv_of_md hfe
  obtain ⟨q, hq⟩ := LibHeckeRel.ex_add hP
  have hPq : P - 1 = q := by rw [hq]; exact LibR2Quad.sub_of_add q 1
  rw [hPq] at hD
  refine ⟨(i : Int) - (i' : Int), (j : Int) - (j' : Int), ?_, natAbs_sub_le (Nat.le_of_lt_succ di) (Nat.le_of_lt_succ di'),
    natAbs_sub_le (Nat.le_of_lt_succ mj) (Nat.le_of_lt_succ mj'), ?_⟩
  · cases e1 : LibIntBox.ieq ((i : Int) - (i' : Int)) 0
    · exact Or.inl (fun h => by rw [h, LibIntBox.ieq_self] at e1; exact Bool.noConfusion e1)
    · cases e2 : LibIntBox.ieq ((j : Int) - (j' : Int)) 0
      · exact Or.inr (fun h => by rw [h, LibIntBox.ieq_self] at e2; exact Bool.noConfusion e2)
      · exfalso
        have ii : i = i' := Int.ofNat.inj (int_eq_of_sub (LibIntBox.eq_of_ieq e1))
        have jj : j = j' := Int.ofNat.inj (int_eq_of_sub (LibIntBox.eq_of_ieq e2))
        have sk := spec (s + 1) k
        have sk' := spec (s + 1) k'
        rw [hi, hj] at sk
        rw [hi', hj', ← ii, ← jj] at sk'
        exact hkk (sk.trans sk'.symm)
  · refine LibNormPiC.idv_eq (idv_lin hD (LibNormPiC.idv_self (P : Int)) 1 (-((t : Int) * ((j : Int) - (j' : Int))))) ?_
    have hPI : (P : Int) = (q : Int) + 1 := by rw [hq]; rfl
    show _ = 1 * (((i + t * q * j : Nat) : Int) - ((i' + t * q * j' : Nat) : Int)) + _
    show _ = 1 * (((i : Int) + (t : Int) * (q : Int) * (j : Int)) - ((i' : Int) + (t : Int) * (q : Int) * (j' : Int))) + _
    rw [hPI]; lib_int

end LibThue

#print axioms LibThue.idv_of_md
#print axioms LibThue.natAbs_sub_le
#print axioms LibThue.int_eq_of_sub
#print axioms LibThue.thue

lib/ToeplitzLC.lean

lean_all/lib/ToeplitzLC.lean

/-
  lib/ToeplitzLC.lean — T46 shared library (G976): log-concavity of a convolution through 2×2 Cauchy–Binet.

    tz f r j           Toeplitz entry: f (r − j) if j ≤ r, else 0
    tz_mm              Σ_{j<n} tz a (s+m) j · tz b j s = (b ⋆ a)_m      (n ≥ s+m+1)
    conv_lc            c = b ⋆ a, n ≥ k+3:
                       c_{k+1}² − c_k c_{k+2} = Σ_{j,j′<n} [j<j′] · det₂(T_a)_{(k+1,k+2),(j,j′)} · det₂(T_b)_{(j,j′),(0,1)}
  So nonnegative 2×2 minors of the Toeplitz matrices of a and b (TP2, i.e. log-concavity without internal zeros)
  give log-concavity of the convolution (with LibCauchyBinet2.tp2_mul).
  Lean 4 core only, proofs by hand, no axioms.
-/
import lib.CauchyBinet2
import lib.MacMahon

namespace LibToeplitzLC
open DetChain LibRing LibSum LibSemi LibNewton LibCauchyBinet2 LibMacMahon LibQBinom

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

def tz (f : Nat → R) (r j : Nat) : R := cond (decide (j ≤ r)) (f (r - j)) K.zero

theorem tz_mm (a b : Nat → R) (s m e : Nat) :
    sum (toSemi K) (fun j => K.mul (tz K a (s + m) j) (tz K b j s)) (rng (s + (m + 1) + e)) = cv K b a m := by
  rw [sum_rng_add (toSemi K), sum_rng_add (toSemi K)]
  have h1 : sum (toSemi K) (fun j => K.mul (tz K a (s + m) j) (tz K b j s)) (rng s) = K.zero := by
    rw [sum_ext_mem (toSemi K) _ (fun _ => K.zero) _ (fun j hj => by
      have hjs : ¬ s ≤ j := Nat.not_le_of_gt (LibBinom.mem_rng hj)
      unfold tz; rw [decide_eq_false hjs]; show K.mul _ K.zero = K.zero; lib_ring)]
    exact sum_zero (toSemi K) _
  have h3 : sum (toSemi K) (fun j => K.mul (tz K a (s + m) (s + (m + 1) + j)) (tz K b (s + (m + 1) + j) s)) (rng e) = K.zero := by
    rw [sum_ext (toSemi K) _ (fun _ => K.zero) (fun j => by
      have hj : ¬ s + (m + 1) + j ≤ s + m := by
        intro h; exact Nat.lt_irrefl (s + m) (Nat.lt_of_lt_of_le (Nat.lt_of_lt_of_le (Nat.lt_succ_self (s + m)) (Nat.le_add_right _ j)) h)
      unfold tz; rw [decide_eq_false hj]; show K.mul K.zero _ = K.zero; lib_ring)]
    exact sum_zero (toSemi K) _
  rw [h1, h3]
  have h2 : sum (toSemi K) (fun t => K.mul (tz K a (s + m) (s + t)) (tz K b (s + t) s)) (rng (m + 1))
      = sum (toSemi K) (fun t => K.mul (b t) (a (m - t))) (rng (m + 1)) := by
    refine sum_ext_mem (toSemi K) _ _ _ (fun t ht => ?_)
    have htm : t ≤ m := Nat.le_of_lt_succ (LibBinom.mem_rng ht)
    have hle1 : s + t ≤ s + m := Nat.add_le_add_left htm s
    have hle2 : s ≤ s + t := Nat.le_add_right s t
    unfold tz
    rw [decide_eq_true hle1, decide_eq_true hle2]
    show K.mul (a (s + m - (s + t))) (b (s + t - s)) = _
    obtain ⟨u, rfl⟩ := exists_add htm
    rw [sub_left' s t, show s + (t + u) - (s + t) = u from by rw [← Nat.add_assoc, sub_left'], sub_left' t u]
    exact K.mul_comm _ _
  show K.add (K.add K.zero _) K.zero = _
  rw [h2, cv_as_sum']
  generalize sum (toSemi K) (fun t => K.mul (b t) (a (m - t))) (rng (m + 1)) = X
  lib_ring

/-- **Log-concavity defect of a convolution as a Cauchy–Binet sum.** -/
theorem conv_lc (a b : Nat → R) (k e : Nat) :
    sub K (K.mul (cv K b a (k + 1)) (cv K b a (k + 1))) (K.mul (cv K b a k) (cv K b a (k + 2)))
      = sum (toSemi K) (fun j => sum (toSemi K) (fun j' => K.mul (lt K j j')
          (K.mul (det2A K (tz K a) (k + 1) (k + 2) j j') (det2B K (tz K b) j j' 0 1))) (rng (k + 3 + e))) (rng (k + 3 + e)) := by
  rw [← cauchy_binet2 K (k + 3 + e) (tz K a) (tz K b) (k + 1) (k + 2) 0 1]
  unfold mm
  have m10 := tz_mm K a b 0 (k + 1) (e + 1)
  have m21 := tz_mm K a b 1 (k + 1) e
  have m11 := tz_mm K a b 1 k (e + 1)
  have m20 := tz_mm K a b 0 (k + 2) e
  rw [show 0 + (k + 1 + 1) + (e + 1) = k + 3 + e by lib_nat, show 0 + (k + 1) = k + 1 by lib_nat] at m10
  rw [show 1 + (k + 1 + 1) + e = k + 3 + e by lib_nat, show 1 + (k + 1) = k + 2 by lib_nat] at m21
  rw [show 1 + (k + 1) + (e + 1) = k + 3 + e by lib_nat, show 1 + k = k + 1 by lib_nat] at m11
  rw [show 0 + (k + 2 + 1) + e = k + 3 + e by lib_nat, show 0 + (k + 2) = k + 2 by lib_nat] at m20
  rw [m10, m21, m11, m20]

end LibToeplitzLC

#print axioms LibToeplitzLC.tz_mm
#print axioms LibToeplitzLC.conv_lc

lib/TorsCons.lean

lean_all/lib/TorsCons.lean

/-
  lib/TorsCons.lean — T46 shared library, cycle 692 (G546–G550): closed form of 2-torsion translations and agreement with the Z/4 × Z/2 points.

  y² = x(x + a²)(x + b²), P = (ab, ab(a+b)):
    tauTa_P   w'(ab + a²) = 1 ⇒ τ_{T_a}(P) = (−ab, ab(b − a))        (form of TwoTorsAB.tauA_on, α = a², β = b²)
    tauTb_P   w''(ab + b²) = 1 ⇒ τ_{T_b}(P) = (−ab, ab(a − b))
    tau0_P    w·ab = 1 ⇒ τ₀(P) = (ab, −ab(a + b)) = −P
    tau0_Ta   w·(−a²) = 1 ⇒ τ₀(T_a) = (−b², 0) = T_b
    tau_neg   τ_T(x, −y) = (x', −y')
    tauA_inv  zα(α − β) = 1 ⇒ τ_{T_α} is an involution
  Lean 4 core only, no axioms.
-/
import lib.Z4Z2Tors

namespace LibTorsCons
open DetChain LibRing

variable {R : Type} (K : CRing R) (a b : R)

theorem tauTa_P (w' : R) (hw : K.mul w' (K.add (K.mul a b) (K.mul a a)) = K.one) :
    sub K (K.mul (K.mul (K.mul a a) (sub K (K.mul a a) (K.mul b b))) w') (K.mul a a) = K.neg (K.mul a b) ∧
    K.neg (K.mul (K.mul (K.mul (K.mul a a) (sub K (K.mul a a) (K.mul b b))) (K.mul (K.mul a b) (K.add a b))) (K.mul w' w'))
      = K.mul (K.mul a b) (sub K b a) := by
  refine ⟨?_, ?_⟩
  · lib_lc K [hw] [K.mul a (sub K a b)]
  · lib_lc K [hw] [K.neg (K.mul (K.mul (K.mul a b) (sub K a b)) (K.add K.one (K.mul w' (K.add (K.mul a b) (K.mul a a)))))]

theorem tauTb_P (w' : R) (hw : K.mul w' (K.add (K.mul a b) (K.mul b b)) = K.one) :
    sub K (K.mul (K.mul (K.mul b b) (sub K (K.mul b b) (K.mul a a))) w') (K.mul b b) = K.neg (K.mul a b) ∧
    K.neg (K.mul (K.mul (K.mul (K.mul b b) (sub K (K.mul b b) (K.mul a a))) (K.mul (K.mul a b) (K.add a b))) (K.mul w' w'))
      = K.mul (K.mul a b) (sub K a b) := by
  refine ⟨?_, ?_⟩
  · lib_lc K [hw] [K.mul b (sub K b a)]
  · lib_lc K [hw] [K.neg (K.mul (K.mul (K.mul a b) (sub K b a)) (K.add K.one (K.mul w' (K.add (K.mul a b) (K.mul b b)))))]

theorem tau0_P (w : R) (hw : K.mul w (K.mul a b) = K.one) :
    K.mul (K.mul (K.mul a a) (K.mul b b)) w = K.mul a b ∧
    K.neg (K.mul (K.mul (K.mul (K.mul a a) (K.mul b b)) (K.mul (K.mul a b) (K.add a b))) (K.mul w w))
      = K.neg (K.mul (K.mul a b) (K.add a b)) := by
  refine ⟨?_, ?_⟩
  · lib_lc K [hw] [K.mul a b]
  · lib_lc K [hw] [K.neg (K.mul (K.mul (K.mul a b) (K.add a b)) (K.add K.one (K.mul w (K.mul a b))))]

theorem tau0_Ta (w : R) (hw : K.mul w (K.neg (K.mul a a)) = K.one) :
    K.mul (K.mul (K.mul a a) (K.mul b b)) w = K.neg (K.mul b b) ∧
    K.neg (K.mul (K.mul (K.mul (K.mul a a) (K.mul b b)) K.zero) (K.mul w w)) = K.zero := by
  refine ⟨?_, by lib_ring⟩
  lib_lc K [hw] [K.neg (K.mul b b)]

theorem tau_neg (v y w : R) :
    K.neg (K.mul (K.mul v (K.neg y)) (K.mul w w)) = K.neg (K.neg (K.mul (K.mul v y) (K.mul w w))) := by lib_ring

/-- **τ_{T_α} is an involution** (original coordinates). -/
theorem tauA_inv (α β x y w' z : R) (hw : K.mul w' (K.add x α) = K.one) (hz : K.mul z (K.mul α (sub K α β)) = K.one) :
    sub K (K.mul (K.mul α (sub K α β)) (K.mul z (K.add x α))) α = x ∧
    K.neg (K.mul (K.mul (K.mul α (sub K α β)) (K.neg (K.mul (K.mul (K.mul α (sub K α β)) y) (K.mul w' w'))))
      (K.mul (K.mul z (K.add x α)) (K.mul z (K.add x α)))) = y := by
  obtain ⟨h1, h2⟩ := LibTwoTors.tauT_inv K (K.mul α (sub K α β)) (K.add x α) y w' z hw hz
  refine ⟨?_, h2⟩
  rw [h1]; lib_ring

end LibTorsCons

#print axioms LibTorsCons.tauTa_P
#print axioms LibTorsCons.tauTb_P
#print axioms LibTorsCons.tau0_P
#print axioms LibTorsCons.tau0_Ta
#print axioms LibTorsCons.tauA_inv

lib/Totient.lean

lean_all/lib/Totient.lean

/-
  lib/Totient.lean — T46 shared library, cycle 184: Euler φ (computable) and its value at primes.

    phi n             #{1 ≤ k ≤ n : gcd(k, n) = 1}   (LibFastGcd.fgcd)
    phi_prime         p prime (LibPrimePow.Prime) ⇒ phi p = p − 1   (form: phi (m+1) = m, m+1 prime)
    Test: phi 36 = 12, phi 97 = 96 (decide)
  Lean 4 core only, no axioms.
-/
import lib.FastGcd
import lib.PrimePow
import lib.BoolNat

namespace LibPhi
open LibFastGcd LibPrimePow

def cntUp (n : Nat) : Nat → Nat
  | 0 => 0
  | k + 1 => cntUp n k + cond (Nat.beq (fgcd (k + 1) n) 1) 1 0

def phi (n : Nat) : Nat := cntUp n n

/-- For a prime p, 1 ≤ k < p is coprime to p. -/
theorem gcd_one {p k : Nat} (hp : Prime p) (hk1 : 1 ≤ k) (hk : k < p) : fgcd k p = 1 := by
  have hp0 : 0 < p := Nat.lt_trans (Nat.lt_succ_self 0) hp.1
  cases hp.2 _ (fgcd_dvd_right k p) with
  | inl h => exact h
  | inr h =>
    exfalso
    have := fgcd_dvd_left k p
    rw [h] at this
    exact Nat.lt_irrefl _ (Nat.lt_of_le_of_lt (LibOrd.le_of_dvd' hk1 this) hk)

theorem cnt_below {p : Nat} (hp : Prime p) : ∀ k, k < p → cntUp p k = k
  | 0, _ => rfl
  | k + 1, hk => by
    show cntUp p k + cond (Nat.beq (fgcd (k + 1) p) 1) 1 0 = k + 1
    rw [cnt_below hp k (Nat.lt_trans (Nat.lt_succ_self k) hk), gcd_one hp (Nat.succ_le_succ (Nat.zero_le k)) hk]
    rfl

/-- **φ(p) = p − 1.** -/
theorem phi_prime {m : Nat} (hp : Prime (m + 1)) : phi (m + 1) = m := by
  show cntUp (m + 1) m + cond (Nat.beq (fgcd (m + 1) (m + 1)) 1) 1 0 = m
  rw [cnt_below hp m (Nat.lt_succ_self m)]
  have hg : fgcd (m + 1) (m + 1) = m + 1 := by
    have hp0 : 0 < m + 1 := Nat.succ_le_succ (Nat.zero_le m)
    apply Nat.le_antisymm (LibOrd.le_of_dvd' hp0 (fgcd_dvd_left _ _))
    exact LibOrd.le_of_dvd' (fgcd_pos hp0) (dvd_fgcd (LibGcd.dvd_refl' _) (LibGcd.dvd_refl' _))
  rw [hg]
  have : Nat.beq (m + 1) 1 = false := LibBoolNat.ne_beq (fun e => Nat.lt_irrefl _ (e ▸ hp.1))
  rw [this]; rfl

example : phi 36 = 12 := by decide
example : phi 97 = 96 := by decide

end LibPhi

#print axioms LibPhi.gcd_one
#print axioms LibPhi.phi_prime

lib/Tr4.lean

lean_all/lib/Tr4.lean

/-
  lib/Tr4.lean — T46 shared library, cycle 645 (G410a): block recursion for tr M⁴.

  Any commutative ring, M (n+1)² = [[a, b], [c, N]], s = b·c, w₁ = bNc, w₂ = bN²c, NN = N·N:
    tr4_succ   tr((MM)(MM)) = a⁴ + 4a²s + 2s² + 4a w₁ + 4w₂ + tr(NN·NN)
  Lean 4 core only, no axioms.
-/
import lib.AdjDual4

namespace LibTr4
open DetChain LibRing LibSum LibSemi LibMat LibLegSum LibCharQuad LibNewtonGen3

variable {R : Type} (K : CRing R) (n : Nat) (M : Nat → Nat → R)

def w2 : R := sum (toSemi K) (fun m => K.mul (bN K n M m) (Nc K n M m)) (rng n)

abbrev NN : Nat → Nat → R := mmul (toSemi K) n (sh M) (sh M)

/-- Σ_m b_m (NN·c)_m = w₂. -/
theorem bNNc : sum (toSemi K) (fun m => K.mul (M 0 (m + 1)) (sum (toSemi K) (fun i => K.mul (NN K n M m i) (M (i + 1) 0)) (rng n))) (rng n)
    = w2 K n M := by
  unfold w2 bN Nc
  -- (NN c)_m = Σ_l N_ml (Nc)_l
  have hin : ∀ m, sum (toSemi K) (fun i => K.mul (NN K n M m i) (M (i + 1) 0)) (rng n)
      = sum (toSemi K) (fun l => K.mul (M (m + 1) (l + 1)) (sum (toSemi K) (fun r => K.mul (M (l + 1) (r + 1)) (M (r + 1) 0)) (rng n))) (rng n) := by
    intro m
    show sum (toSemi K) (fun i => K.mul (sum (toSemi K) (fun l => K.mul (M (m + 1) (l + 1)) (M (l + 1) (i + 1))) (rng n)) (M (i + 1) 0)) (rng n) = _
    refine Eq.trans (sum_ext (toSemi K) _ (fun i => sum (toSemi K) (fun l => K.mul (M (m + 1) (l + 1)) (K.mul (M (l + 1) (i + 1)) (M (i + 1) 0))) (rng n)) (fun i => by
      refine Eq.trans (LibSum.sum_mul (toSemi K) _ _ _) ?_
      exact sum_ext (toSemi K) _ _ (fun l => by
        show K.mul (K.mul (M (m + 1) (l + 1)) (M (l + 1) (i + 1))) (M (i + 1) 0) = _
        generalize M (m + 1) (l + 1) = x; generalize M (l + 1) (i + 1) = y; generalize M (i + 1) 0 = z; lib_ring) _) _) ?_
    rw [sum_swap (toSemi K)]
    exact sum_ext (toSemi K) _ _ (fun l => (mul_sum (toSemi K) _ _ _).symm) _
  rw [sum_ext (toSemi K) _ _ (fun m => congrArg (K.mul (M 0 (m + 1))) (hin m))]
  refine Eq.trans (sum_ext (toSemi K) _ (fun m => sum (toSemi K) (fun l => K.mul (K.mul (M 0 (m + 1)) (M (m + 1) (l + 1)))
      (sum (toSemi K) (fun r => K.mul (M (l + 1) (r + 1)) (M (r + 1) 0)) (rng n))) (rng n)) (fun m => by
    refine Eq.trans (mul_sum (toSemi K) _ _ _) ?_
    exact sum_ext (toSemi K) _ _ (fun l => by
      generalize sum (toSemi K) (fun r => K.mul (M (l + 1) (r + 1)) (M (r + 1) 0)) (rng n) = S
      show K.mul (M 0 (m + 1)) (K.mul (M (m + 1) (l + 1)) S) = _
      generalize M 0 (m + 1) = x; generalize M (m + 1) (l + 1) = y; lib_ring) _) _) ?_
  rw [sum_swap (toSemi K)]
  exact sum_ext (toSemi K) _ _ (fun l => (LibSum.sum_mul (toSemi K) _ _ _).symm) _

theorem sum4 (f1 f2 f3 f4 : Nat → R) (l : List Nat) :
    sum (toSemi K) (fun x => K.add (K.add (K.add (f1 x) (f2 x)) (f3 x)) (f4 x)) l
      = K.add (K.add (K.add (sum (toSemi K) f1 l) (sum (toSemi K) f2 l)) (sum (toSemi K) f3 l)) (sum (toSemi K) f4 l) := by
  refine (LibSum.sum_add (toSemi K) _ _ _).trans ?_
  refine congr (congrArg K.add ((LibSum.sum_add (toSemi K) _ _ _).trans (congr (congrArg K.add (LibSum.sum_add (toSemi K) _ _ _)) rfl))) rfl

theorem X1v : sum (toSemi K) (fun m => K.mul (K.add (K.mul (M 0 0) (M 0 (m + 1))) (bN K n M m)) (K.add (K.mul (M (m + 1) 0) (M 0 0)) (Nc K n M m))) (rng n)
    = K.add (K.add (K.mul (K.mul (M 0 0) (M 0 0)) (sbc K n M)) (K.mul (K.add (M 0 0) (M 0 0)) (bNc K n M))) (w2 K n M) := by
  refine Eq.trans (sum_ext (toSemi K) _ (fun m => K.add (K.add (K.add (K.mul (K.mul (M 0 0) (M 0 0)) (K.mul (M 0 (m + 1)) (M (m + 1) 0)))
      (K.mul (M 0 0) (K.mul (M 0 (m + 1)) (Nc K n M m)))) (K.mul (M 0 0) (K.mul (bN K n M m) (M (m + 1) 0)))) (K.mul (bN K n M m) (Nc K n M m))) (fun m => by
    generalize bN K n M m = p; generalize Nc K n M m = q; generalize M 0 0 = a; generalize M 0 (m + 1) = b; generalize M (m + 1) 0 = c; lib_ring) _) ?_
  rw [sum4]
  have a1 : sum (toSemi K) (fun m => K.mul (K.mul (M 0 0) (M 0 0)) (K.mul (M 0 (m + 1)) (M (m + 1) 0))) (rng n) = K.mul (K.mul (M 0 0) (M 0 0)) (sbc K n M) :=
    (mul_sum (toSemi K) _ _ _).symm
  have a2 : sum (toSemi K) (fun m => K.mul (M 0 0) (K.mul (M 0 (m + 1)) (Nc K n M m))) (rng n) = K.mul (M 0 0) (bNc K n M) :=
    (mul_sum (toSemi K) _ _ _).symm
  have a3 : sum (toSemi K) (fun m => K.mul (M 0 0) (K.mul (bN K n M m) (M (m + 1) 0))) (rng n) = K.mul (M 0 0) (bNc K n M) :=
    (mul_sum (toSemi K) _ _ _).symm.trans (congrArg (K.mul (M 0 0)) (swap1 K n M))
  rw [a1, a2, a3]
  show K.add (K.add (K.add (K.mul _ (sbc K n M)) (K.mul _ (bNc K n M))) (K.mul _ (bNc K n M))) (w2 K n M) = _
  generalize sbc K n M = s; generalize bNc K n M = w; generalize w2 K n M = v; generalize M 0 0 = a; lib_ring

theorem X2v : sum (toSemi K) (fun i => K.mul (K.add (K.mul (M (i + 1) 0) (M 0 0)) (Nc K n M i)) (K.add (K.mul (M 0 0) (M 0 (i + 1))) (bN K n M i))) (rng n)
    = K.add (K.add (K.mul (K.mul (M 0 0) (M 0 0)) (sbc K n M)) (K.mul (K.add (M 0 0) (M 0 0)) (bNc K n M))) (w2 K n M) := by
  rw [← X1v]
  exact sum_ext (toSemi K) _ _ (fun i => K.mul_comm _ _) _

theorem X3v : sum (toSemi K) (fun i => sum (toSemi K) (fun m => K.mul (K.add (K.mul (M (i + 1) 0) (M 0 (m + 1))) (NN K n M i m))
      (K.add (K.mul (M (m + 1) 0) (M 0 (i + 1))) (NN K n M m i))) (rng n)) (rng n)
    = K.add (K.add (K.mul (sbc K n M) (sbc K n M)) (K.add (w2 K n M) (w2 K n M)))
        (trace (toSemi K) n (mmul (toSemi K) n (NN K n M) (NN K n M))) := by
  refine Eq.trans (sum_ext (toSemi K) _ (fun i => sum (toSemi K) (fun m => K.add (K.add (K.add
      (K.mul (K.mul (M (i + 1) 0) (M 0 (i + 1))) (K.mul (M 0 (m + 1)) (M (m + 1) 0)))
      (K.mul (M 0 (m + 1)) (K.mul (NN K n M m i) (M (i + 1) 0))))
      (K.mul (M 0 (i + 1)) (K.mul (NN K n M i m) (M (m + 1) 0))))
      (K.mul (NN K n M i m) (NN K n M m i))) (rng n)) (fun i => sum_ext (toSemi K) _ _ (fun m => by
    generalize NN K n M i m = p; generalize NN K n M m i = q; generalize M (i + 1) 0 = ci; generalize M 0 (m + 1) = bm
    generalize M (m + 1) 0 = cm; generalize M 0 (i + 1) = bi; lib_ring) _) _) ?_
  rw [sum_ext (toSemi K) _ _ (fun i => sum4 K _ _ _ _ _), sum4]
  have t1 : sum (toSemi K) (fun i => sum (toSemi K) (fun m => K.mul (K.mul (M (i + 1) 0) (M 0 (i + 1))) (K.mul (M 0 (m + 1)) (M (m + 1) 0))) (rng n)) (rng n)
      = K.mul (sbc K n M) (sbc K n M) := by
    refine (LibSum.sum_mul_sum (toSemi K) (fun i => K.mul (M (i + 1) 0) (M 0 (i + 1))) (fun m => K.mul (M 0 (m + 1)) (M (m + 1) 0)) (rng n) (rng n)).symm.trans ?_
    rw [sbc_comm]; rfl
  have t2 : sum (toSemi K) (fun i => sum (toSemi K) (fun m => K.mul (M 0 (m + 1)) (K.mul (NN K n M m i) (M (i + 1) 0))) (rng n)) (rng n)
      = w2 K n M := by
    rw [sum_swap (toSemi K), ← bNNc]
    exact sum_ext (toSemi K) _ _ (fun m => (mul_sum (toSemi K) _ _ _).symm) _
  have t3 : sum (toSemi K) (fun i => sum (toSemi K) (fun m => K.mul (M 0 (i + 1)) (K.mul (NN K n M i m) (M (m + 1) 0))) (rng n)) (rng n)
      = w2 K n M := by
    rw [← bNNc]
    exact sum_ext (toSemi K) _ _ (fun i => (mul_sum (toSemi K) _ _ _).symm) _
  rw [t1, t2, t3]
  show K.add (K.add (K.add _ _) _) (trace (toSemi K) n (mmul (toSemi K) n (NN K n M) (NN K n M))) = _
  generalize sbc K n M = s; generalize w2 K n M = v
  generalize trace (toSemi K) n (mmul (toSemi K) n (NN K n M) (NN K n M)) = T4
  lib_ring

/-- **tr(P·P) = a⁴ + 4a²s + 2s² + 4a w₁ + 4w₂ + tr(NN·NN)** (P = MM). -/
theorem tr4_succ :
    trace (toSemi K) (n + 1) (mmul (toSemi K) (n + 1) (P K n M) (P K n M))
      = K.add (K.add (K.add (K.add (K.add (K.mul (K.mul (M 0 0) (M 0 0)) (K.mul (M 0 0) (M 0 0)))
          (K.mul (K.add (K.add (K.add K.one K.one) K.one) K.one) (K.mul (K.mul (M 0 0) (M 0 0)) (sbc K n M))))
          (K.mul (K.add K.one K.one) (K.mul (sbc K n M) (sbc K n M))))
          (K.mul (K.add (K.add (K.add K.one K.one) K.one) K.one) (K.mul (M 0 0) (bNc K n M))))
          (K.mul (K.add (K.add (K.add K.one K.one) K.one) K.one) (w2 K n M)))
        (trace (toSemi K) n (mmul (toSemi K) n (NN K n M) (NN K n M))) := by
  show sum (toSemi K) (fun i => sum (toSemi K) (fun m => K.mul (P K n M i m) (P K n M m i)) (rng (n + 1))) (rng (n + 1)) = _
  rw [LibDetKernel.sum_split0 K n, LibDetKernel.sum_split0 K n (fun m => K.mul (P K n M 0 m) (P K n M m 0))]
  have hrow : ∀ i, sum (toSemi K) (fun m => K.mul (P K n M (i + 1) m) (P K n M m (i + 1))) (rng (n + 1))
      = K.add (K.mul (K.add (K.mul (M (i + 1) 0) (M 0 0)) (Nc K n M i)) (K.add (K.mul (M 0 0) (M 0 (i + 1))) (bN K n M i)))
          (sum (toSemi K) (fun m => K.mul (K.add (K.mul (M (i + 1) 0) (M 0 (m + 1))) (NN K n M i m))
            (K.add (K.mul (M (m + 1) 0) (M 0 (i + 1))) (NN K n M m i))) (rng n)) := by
    intro i
    rw [LibDetKernel.sum_split0 K n, pp_i0, pp_0m]
    exact congrArg (K.add _) (sum_ext (toSemi K) _ _ (fun m => by rw [pp_im, pp_im]) _)
  rw [sum_ext (toSemi K) _ _ hrow]
  refine Eq.trans (congrArg (K.add _) (LibSum.sum_add (toSemi K) _ _ _)) ?_
  rw [X2v, X3v, pp_00]
  have h0 : sum (toSemi K) (fun m => K.mul (P K n M 0 (m + 1)) (P K n M (m + 1) 0)) (rng n)
      = sum (toSemi K) (fun m => K.mul (K.add (K.mul (M 0 0) (M 0 (m + 1))) (bN K n M m)) (K.add (K.mul (M (m + 1) 0) (M 0 0)) (Nc K n M m))) (rng n) :=
    sum_ext (toSemi K) _ _ (fun m => by rw [pp_0m, pp_i0]) _
  rw [h0, X1v]
  generalize sbc K n M = s; generalize bNc K n M = w; generalize w2 K n M = v; generalize M 0 0 = a
  generalize trace (toSemi K) n (mmul (toSemi K) n (NN K n M) (NN K n M)) = T4
  show K.add (K.add _ _) (K.add _ _) = _
  lib_ring

end LibTr4

#print axioms LibTr4.bNNc
#print axioms LibTr4.tr4_succ

lib/Trace2.lean

lean_all/lib/Trace2.lean

/-
  lib/Trace2.lean — T46 shared library, cycle 426 (G92): traces of powers of an arbitrary 2×2 matrix.

  Any commutative ring, A 2×2, t = tr A, d = det A:
    tr_step     tr(P A A) = t·tr(P A) − d·tr P             (any P; trace form of A² = tA − dI)
    tr_pow      tr A^k = s_k(t, d)                         (Psum2.s: s_0 = 2, s_1 = t, s_{k+2} = t s_{k+1} − d s_k)
  (expand entries, then lib_ring — 2×2 Cayley–Hamilton is a polynomial identity.)
  Lean 4 core only, no axioms.
-/
import lib.ContBinet

namespace LibTrace2
open DetChain LibRing LibSum LibSemi LibMat LibLegSum

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

theorem tr_step (P A : Nat → Nat → R) :
    trace (toSemi K) 2 (mmul (toSemi K) 2 (mmul (toSemi K) 2 P A) A)
      = K.add (K.mul (K.add (A 0 0) (A 1 1)) (trace (toSemi K) 2 (mmul (toSemi K) 2 P A)))
          (K.neg (K.mul (K.add (K.mul (A 0 0) (A 1 1)) (K.neg (K.mul (A 0 1) (A 1 0)))) (trace (toSemi K) 2 P))) := by
  show K.add (K.add (K.mul (K.add (K.mul (P 0 0) (A 0 0)) (K.add (K.mul (P 0 1) (A 1 0)) K.zero)) (A 0 0)) (K.add (K.mul (K.add (K.mul (P 0 0) (A 0 1)) (K.add (K.mul (P 0 1) (A 1 1)) K.zero)) (A 1 0)) K.zero)) (K.add (K.add (K.mul (K.add (K.mul (P 1 0) (A 0 0)) (K.add (K.mul (P 1 1) (A 1 0)) K.zero)) (A 0 1)) (K.add (K.mul (K.add (K.mul (P 1 0) (A 0 1)) (K.add (K.mul (P 1 1) (A 1 1)) K.zero)) (A 1  -- (+345 chars; full line in the Zenodo source)
  lib_ring

theorem tr_pow_aux (A : Nat → Nat → R) : ∀ k,
    trace (toSemi K) 2 (mpow (toSemi K) 2 A k)
      = LibPsum2.s K (K.add (A 0 0) (A 1 1)) (K.add (K.mul (A 0 0) (A 1 1)) (K.neg (K.mul (A 0 1) (A 1 0)))) k
    ∧ trace (toSemi K) 2 (mpow (toSemi K) 2 A (k + 1))
      = LibPsum2.s K (K.add (A 0 0) (A 1 1)) (K.add (K.mul (A 0 0) (A 1 1)) (K.neg (K.mul (A 0 1) (A 1 0)))) (k + 1)
  | 0 => by
    refine ⟨?_, ?_⟩
    · show K.add (indK (toSemi K) (decide (0 = 0))) (K.add (indK (toSemi K) (decide (1 = 1))) K.zero) = K.add K.one K.one
      rw [decide_eq_true rfl, decide_eq_true rfl]; show K.add K.one (K.add K.one K.zero) = _; lib_ring
    · show K.add (K.add (K.mul (indK (toSemi K) (decide (0 = 0))) (A 0 0)) (K.add (K.mul (indK (toSemi K) (decide (0 = 1))) (A 1 0)) K.zero))
        (K.add (K.add (K.mul (indK (toSemi K) (decide (1 = 0))) (A 0 1)) (K.add (K.mul (indK (toSemi K) (decide (1 = 1))) (A 1 1)) K.zero)) K.zero)
        = K.add (A 0 0) (A 1 1)
      rw [decide_eq_true (rfl : (0 : Nat) = 0), decide_eq_true (rfl : (1 : Nat) = 1),
        decide_eq_false (fun h => Nat.noConfusion h : (0 : Nat) ≠ 1), decide_eq_false (fun h => Nat.noConfusion h : (1 : Nat) ≠ 0)]
      show K.add (K.add (K.mul K.one (A 0 0)) (K.add (K.mul K.zero (A 1 0)) K.zero))
        (K.add (K.add (K.mul K.zero (A 0 1)) (K.add (K.mul K.one (A 1 1)) K.zero)) K.zero) = _
      lib_ring
  | k + 1 => by
    have ⟨i0, i1⟩ := tr_pow_aux A k
    refine ⟨i1, ?_⟩
    show trace (toSemi K) 2 (mmul (toSemi K) 2 (mmul (toSemi K) 2 (mpow (toSemi K) 2 A k) A) A) = _
    refine Eq.trans (tr_step K (mpow (toSemi K) 2 A k) A) ?_
    show K.add (K.mul _ (trace (toSemi K) 2 (mpow (toSemi K) 2 A (k + 1)))) (K.neg (K.mul _ (trace (toSemi K) 2 (mpow (toSemi K) 2 A k)))) = _
    rw [i0, i1]; rfl

/-- **tr A^k = s_k(tr A, det A).** -/
theorem tr_pow (A : Nat → Nat → R) (k : Nat) :
    trace (toSemi K) 2 (mpow (toSemi K) 2 A k)
      = LibPsum2.s K (K.add (A 0 0) (A 1 1)) (K.add (K.mul (A 0 0) (A 1 1)) (K.neg (K.mul (A 0 1) (A 1 0)))) k :=
  (tr_pow_aux K A k).1

end LibTrace2

#print axioms LibTrace2.tr_step
#print axioms LibTrace2.tr_pow

lib/TraceDet.lean

lean_all/lib/TraceDet.lean

/-
  lib/TraceDet.lean — T46 shared library, cycle 715 (G596): traces determine the characteristic polynomial coefficients (triangular, any size).

    cv_congr_le   f = f', g = g' for i ≤ k ⇒ cv f g k = cv f' g' k
    c_zero_any    c₀(M) = 1
    trace_det     T (n×n), T' (n'×n') upper triangular, tr T^i = tr T'^i for i = 1…N, 1…N invertible ⇒ c_j(T) = c_j(T') for j ≤ N
  Lean 4 core only, no axioms.
-/
import lib.RSNewton

namespace LibTraceDet
open DetChain LibRing LibNewtonTriAll

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

theorem cv_congr_le : ∀ (k : Nat) (f f' g g' : Nat → R), (∀ i, i ≤ k → f i = f' i) → (∀ i, i ≤ k → g i = g' i) →
    LibNewton.cv K f g k = LibNewton.cv K f' g' k
  | 0, f, f', g, g', hf, hg => by
    show K.mul (f 0) (g 0) = K.mul (f' 0) (g' 0); rw [hf 0 (Nat.le_refl 0), hg 0 (Nat.le_refl 0)]
  | k + 1, f, f', g, g', hf, hg => by
    show K.add (K.mul (f 0) (g (k + 1))) (LibNewton.cv K (fun i => f (i + 1)) g k)
      = K.add (K.mul (f' 0) (g' (k + 1))) (LibNewton.cv K (fun i => f' (i + 1)) g' k)
    rw [hf 0 (Nat.zero_le _), hg (k + 1) (Nat.le_refl _),
      cv_congr_le k _ _ g g' (fun i hi => hf (i + 1) (Nat.succ_le_succ hi)) (fun i hi => hg i (Nat.le_succ_of_le hi))]

theorem c_zero_any (n : Nat) (M : Nat → Nat → R) : LibCharPolyC.c K 0 n M = K.one := by
  rw [LibCoefMinors.c_minors, LibDegSubsets.SG_zero_deg K n _ (fun f g h => by unfold LibPrincMinor.PM; rw [LibPrincMinor.selL_congr n f g (fun i _ => h i)])]
  unfold LibPrincMinor.PM
  rw [LibEsymRec.sel_none]; rfl

/-- **Traces determine the coefficients.** -/
theorem trace_det (n n' N : Nat) (T T' : Nat → Nat → R) (hT : ∀ s t, t < s → T s t = K.zero) (hT' : ∀ s t, t < s → T' s t = K.zero)
    (htr : ∀ i, 1 ≤ i → i ≤ N → LibMat.trace (LibSum.toSemi K) n (LibMat.mpow (LibSum.toSemi K) n T i)
      = LibMat.trace (LibSum.toSemi K) n' (LibMat.mpow (LibSum.toSemi K) n' T' i))
    (hinv : ∀ j, 1 ≤ j → j ≤ N → ∃ u, K.mul (castN K j) u = K.one) :
    ∀ j, j ≤ N → ∀ l, l ≤ j → LibCharPolyC.c K l n T = LibCharPolyC.c K l n' T' := by
  intro j
  induction j with
  | zero => intro _ l hl; rw [Nat.le_antisymm hl (Nat.zero_le _), c_zero_any, c_zero_any]
  | succ j ih =>
    intro hjN l hl
    cases Nat.lt_or_ge l (j + 1) with
    | inl h => exact ih (Nat.le_of_succ_le hjN) l (Nat.le_of_lt_succ h)
    | inr h =>
      have hl' : l = j + 1 := Nat.le_antisymm hl h
      subst hl'
      have e1 := newton_tri K (j + 1) n T hT
      have e2 := newton_tri K (j + 1) n' T' hT'
      have hcv : LibNewton.cv K (qT K n T) (fun l => LibCharPolyC.c K l n T) (j + 1)
          = LibNewton.cv K (qT K n' T') (fun l => LibCharPolyC.c K l n' T') (j + 1) := by
        show K.add (K.mul K.zero _) (LibNewton.cv K (fun i => qT K n T (i + 1)) _ j)
          = K.add (K.mul K.zero _) (LibNewton.cv K (fun i => qT K n' T' (i + 1)) _ j)
        rw [cv_congr_le K j _ (fun i => qT K n' T' (i + 1)) _ (fun l => LibCharPolyC.c K l n' T')
          (fun i hi => by show K.mul _ _ = K.mul _ _; rw [htr (i + 1) (Nat.succ_le_succ (Nat.zero_le i)) (Nat.le_trans (Nat.succ_le_succ hi) hjN)])
          (fun i hi => ih (Nat.le_of_succ_le hjN) i hi)]
        generalize LibCharPolyC.c K (j + 1) n T = a; generalize LibCharPolyC.c K (j + 1) n' T' = b
        generalize LibNewton.cv K (fun i => qT K n' T' (i + 1)) (fun l => LibCharPolyC.c K l n' T') j = x; lib_ring
      obtain ⟨u, hu⟩ := hinv (j + 1) (Nat.succ_le_succ (Nat.zero_le j)) hjN
      have e : K.mul (castN K (j + 1)) (LibCharPolyC.c K (j + 1) n T) = K.mul (castN K (j + 1)) (LibCharPolyC.c K (j + 1) n' T') := by
        rw [← e1, ← e2, hcv]
      calc LibCharPolyC.c K (j + 1) n T = K.mul (K.mul (castN K (j + 1)) u) (LibCharPolyC.c K (j + 1) n T) := by rw [hu]; lib_ring
        _ = K.mul u (K.mul (castN K (j + 1)) (LibCharPolyC.c K (j + 1) n T)) := by lib_ring
        _ = K.mul u (K.mul (castN K (j + 1)) (LibCharPolyC.c K (j + 1) n' T')) := by rw [e]
        _ = K.mul (K.mul (castN K (j + 1)) u) (LibCharPolyC.c K (j + 1) n' T') := by lib_ring
        _ = LibCharPolyC.c K (j + 1) n' T' := by rw [hu]; lib_ring

end LibTraceDet

#print axioms LibTraceDet.cv_congr_le
#print axioms LibTraceDet.trace_det

lib/TraceDetApp.lean

lean_all/lib/TraceDetApp.lean

/-
  lib/TraceDetApp.lean — T46 shared library, cycle 716 (G597, G598): RS and block versions of determination by traces.

    rs_trace_det    A (n×n), B (m×m) upper triangular, T' upper triangular, tr T'^i = tr A^i · tr B^i (i ≤ N), 1…N invertible ⇒ c_j(A⊗B) = c_j(T') (j ≤ N)
    dsum_trace_det  tr T'^i = tr A^i + tr B^i ⇒ c_j(A⊕B) = c_j(T')
  Lean 4 core only, no axioms.
-/
import lib.TraceDet
import lib.KronPowFlat

namespace LibTraceDetApp
open DetChain LibRing

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

theorem rs_trace_det (n' m' n'' N : Nat) (A B T' : Nat → Nat → R)
    (hA : ∀ s t, t < s → A s t = K.zero) (hB : ∀ s t, t < s → B s t = K.zero) (hT' : ∀ s t, t < s → T' s t = K.zero)
    (htr : ∀ i, 1 ≤ i → i ≤ N → K.mul (LibMat.trace (LibSum.toSemi K) (n' + 1) (LibMat.mpow (LibSum.toSemi K) (n' + 1) A i))
        (LibMat.trace (LibSum.toSemi K) (m' + 1) (LibMat.mpow (LibSum.toSemi K) (m' + 1) B i))
      = LibMat.trace (LibSum.toSemi K) n'' (LibMat.mpow (LibSum.toSemi K) n'' T' i))
    (hinv : ∀ j, 1 ≤ j → j ≤ N → ∃ u, K.mul (castN K j) u = K.one) (j : Nat) (hj : j ≤ N) :
    LibCharPolyC.c K j ((m' + 1) * (n' + 1)) (LibKronDetGen.fl (m' + 1) (LibKron.kron K A B)) = LibCharPolyC.c K j n'' T' :=
  LibTraceDet.trace_det K _ n'' N _ T' (LibKronCoef.kron_upper K m' A B hA hB) hT'
    (fun i h1 h2 => (LibKronPowFlat.tr_fl_pow K n' m' A B i).trans (htr i h1 h2)) hinv j hj j (Nat.le_refl j)

theorem dsum_trace_det (n m n'' N : Nat) (A B T' : Nat → Nat → R)
    (hA : ∀ s t, t < s → A s t = K.zero) (hB : ∀ s t, t < s → B s t = K.zero) (hT' : ∀ s t, t < s → T' s t = K.zero)
    (htr : ∀ i, 1 ≤ i → i ≤ N → K.add (LibMat.trace (LibSum.toSemi K) n (LibMat.mpow (LibSum.toSemi K) n A i))
        (LibMat.trace (LibSum.toSemi K) m (LibMat.mpow (LibSum.toSemi K) m B i))
      = LibMat.trace (LibSum.toSemi K) n'' (LibMat.mpow (LibSum.toSemi K) n'' T' i))
    (hinv : ∀ j, 1 ≤ j → j ≤ N → ∃ u, K.mul (castN K j) u = K.one) (j : Nat) (hj : j ≤ N) :
    LibCharPolyC.c K j (n + m) (LibEulerSum.dsum K A B n) = LibCharPolyC.c K j n'' T' :=
  LibTraceDet.trace_det K _ n'' N _ T' (LibDsumCoefAll.dsum_upper K n A B hA hB) hT'
    (fun i h1 h2 => (LibDsumPow.tr_dsum_pow K A B n m i).trans (htr i h1 h2)) hinv j hj j (Nat.le_refl j)

end LibTraceDetApp

#print axioms LibTraceDetApp.rs_trace_det
#print axioms LibTraceDetApp.dsum_trace_det

lib/TracePow34.lean

lean_all/lib/TracePow34.lean

/-
  lib/TracePow34.lean — T46 shared library, cycle 508 (G173, G174): traces of powers of 3×3 and 4×4 matrices (Newton).

  Any commutative ring, M 3×3 / 4×4, c₂ = sum of principal 2×2 minors (CharPoly34):
    mpow2 / mpow3   M² = MM, M³ = (MM)M      (entrywise, row < n)
    tr_sq3          tr M² = tr(M)² − 2c₂                     (3×3)
    tr_cube3        tr M³ = tr(M)³ − 3 tr(M) c₂ + 3 det M     (3×3, Newton k = 3)
    tr_sq4          tr M² = tr(M)² − 2c₂                     (4×4)
  (Entries expanded, then lib_ring; formulas generated by script.)
  Lean 4 core only, no axioms.
-/
import lib.CharPoly34

namespace LibTracePow34
open DetChain LibRing LibSum LibSemi LibMat LibLegSum LibDet LibDet3

variable {R : Type} (K : CRing R) (M : Nat → Nat → R)

theorem mpow2 (n : Nat) (i j : Nat) (hi : i < n) : mpow (toSemi K) n M 2 i j = mmul (toSemi K) n M M i j := by
  show mmul (toSemi K) n (mmul (toSemi K) n (ident (toSemi K)) M) M i j = _
  exact LibAdj.mmul_congr_l K n _ M M i j (fun l _ => mmul_ident_l (toSemi K) n M hi l)

theorem mpow3 (n : Nat) (i j : Nat) (hi : i < n) :
    mpow (toSemi K) n M 3 i j = mmul (toSemi K) n (mmul (toSemi K) n M M) M i j := by
  show mmul (toSemi K) n (mpow (toSemi K) n M 2) M i j = _
  exact LibAdj.mmul_congr_l K n _ (mmul (toSemi K) n M M) M i j (fun l _ => mpow2 K M n i l hi)

theorem trace_congr (n : Nat) (X Y : Nat → Nat → R) (h : ∀ i, i < n → X i i = Y i i) :
    trace (toSemi K) n X = trace (toSemi K) n Y :=
  sum_ext_mem (toSemi K) _ _ (rng n) (fun i hi => h i (LibBinom.mem_rng hi))

theorem tr_sq3 : trace (toSemi K) 3 (mpow (toSemi K) 3 M 2)
    = K.add (K.mul (K.add (K.add (M 0 0) (M 1 1)) (M 2 2)) (K.add (K.add (M 0 0) (M 1 1)) (M 2 2))) (K.neg (K.mul (K.add K.one K.one) (K.add (K.add (K.add (K.mul (M 0 0) (M 1 1)) (K.neg (K.mul (M 0 1) (M 1 0)))) (K.add (K.mul (M 0 0) (M 2 2)) (K.neg (K.mul (M 0 2) (M 2 0))))) (K.add (K.mul (M 1 1) (M 2 2)) (K.neg (K.mul (M 1 2) (M 2 1))))))) := by
  rw [trace_congr K 3 _ (mmul (toSemi K) 3 M M) (fun i hi => mpow2 K M 3 i i hi)]
  show (K.add (K.add (K.mul (M 0 0) (M 0 0)) (K.add (K.mul (M 0 1) (M 1 0)) (K.add (K.mul (M 0 2) (M 2 0)) K.zero))) (K.add (K.add (K.mul (M 1 0) (M 0 1)) (K.add (K.mul (M 1 1) (M 1 1)) (K.add (K.mul (M 1 2) (M 2 1)) K.zero))) (K.add (K.add (K.mul (M 2 0) (M 0 2)) (K.add (K.mul (M 2 1) (M 1 2)) (K.add (K.mul (M 2 2) (M 2 2)) K.zero))) K.zero))) = _
  lib_ring

theorem tr_sq4 : trace (toSemi K) 4 (mpow (toSemi K) 4 M 2)
    = K.add (K.mul (K.add (K.add (K.add (M 0 0) (M 1 1)) (M 2 2)) (M 3 3)) (K.add (K.add (K.add (M 0 0) (M 1 1)) (M 2 2)) (M 3 3))) (K.neg (K.mul (K.add K.one K.one) (K.add (K.add (K.add (K.add (K.add (K.add (K.mul (M 0 0) (M 1 1)) (K.neg (K.mul (M 0 1) (M 1 0)))) (K.add (K.mul (M 0 0) (M 2 2)) (K.neg (K.mul (M 0 2) (M 2 0))))) (K.add (K.mul (M 0 0) (M 3 3)) (K.neg (K.mul (M 0 3) (M 3 0))))) (K.ad  -- (+197 chars; full line in the Zenodo source)
  rw [trace_congr K 4 _ (mmul (toSemi K) 4 M M) (fun i hi => mpow2 K M 4 i i hi)]
  show (K.add (K.add (K.mul (M 0 0) (M 0 0)) (K.add (K.mul (M 0 1) (M 1 0)) (K.add (K.mul (M 0 2) (M 2 0)) (K.add (K.mul (M 0 3) (M 3 0)) K.zero)))) (K.add (K.add (K.mul (M 1 0) (M 0 1)) (K.add (K.mul (M 1 1) (M 1 1)) (K.add (K.mul (M 1 2) (M 2 1)) (K.add (K.mul (M 1 3) (M 3 1)) K.zero)))) (K.add (K.add (K.mul (M 2 0) (M 0 2)) (K.add (K.mul (M 2 1) (M 1 2)) (K.add (K.mul (M 2 2) (M 2 2)) (K.add (K  -- (+189 chars; full line in the Zenodo source)
  lib_ring

theorem tr_cube3 : trace (toSemi K) 3 (mpow (toSemi K) 3 M 3)
    = K.add (K.add (K.mul (K.mul (K.add (K.add (M 0 0) (M 1 1)) (M 2 2)) (K.add (K.add (M 0 0) (M 1 1)) (M 2 2))) (K.add (K.add (M 0 0) (M 1 1)) (M 2 2))) (K.neg (K.mul (K.add K.one (K.add K.one K.one)) (K.mul (K.add (K.add (M 0 0) (M 1 1)) (M 2 2)) (K.add (K.add (K.add (K.mul (M 0 0) (M 1 1)) (K.neg (K.mul (M 0 1) (M 1 0)))) (K.add (K.mul (M 0 0) (M 2 2)) (K.neg (K.mul (M 0 2) (M 2 0))))) (K.add   -- (+397 chars; full line in the Zenodo source)
  rw [trace_congr K 3 _ (mmul (toSemi K) 3 (mmul (toSemi K) 3 M M) M) (fun i hi => mpow3 K M 3 i i hi)]
  show (K.add (K.add (K.mul (K.add (K.mul (M 0 0) (M 0 0)) (K.add (K.mul (M 0 1) (M 1 0)) (K.add (K.mul (M 0 2) (M 2 0)) K.zero))) (M 0 0)) (K.add (K.mul (K.add (K.mul (M 0 0) (M 0 1)) (K.add (K.mul (M 0 1) (M 1 1)) (K.add (K.mul (M 0 2) (M 2 1)) K.zero))) (M 1 0)) (K.add (K.mul (K.add (K.mul (M 0 0) (M 0 2)) (K.add (K.mul (M 0 1) (M 1 2)) (K.add (K.mul (M 0 2) (M 2 2)) K.zero))) (M 2 0)) K.zero))  -- (+805 chars; full line in the Zenodo source)
  lib_ring

end LibTracePow34

#print axioms LibTracePow34.tr_sq3
#print axioms LibTracePow34.tr_cube3
#print axioms LibTracePow34.tr_sq4

lib/TracePow4c.lean

lean_all/lib/TracePow4c.lean

/-
  lib/TracePow4c.lean — T46 shared library, cycle 519 (G186): 4×4 Newton k = 3.

  Any commutative ring, M 4×4, c₁ = tr M, c₂ = sum of principal 2×2 minors, c₃ = sum of principal 3×3 minors (coefficients of CharPoly34):
    tr_cube4    tr M³ = c₁³ − 3c₁c₂ + 3c₃
  (M³ entrywise as (MM)M (TracePow34.mpow3), expanded, then lib_ring; formulas generated by script.)
  Lean 4 core only, no axioms.
-/
import lib.KronBlock

namespace LibTracePow4c
open DetChain LibRing LibSum LibSemi LibMat LibLegSum LibDet LibTracePow34

variable {R : Type} (K : CRing R) (M : Nat → Nat → R)

/-- **tr M³ = c₁³ − 3c₁c₂ + 3c₃** (4×4). -/
theorem tr_cube4 : trace (toSemi K) 4 (mpow (toSemi K) 4 M 3)
    = K.add (K.add (K.mul (K.mul (K.add (K.add (K.add (M 0 0) (M 1 1)) (M 2 2)) (M 3 3)) (K.add (K.add (K.add (M 0 0) (M 1 1)) (M 2 2)) (M 3 3))) (K.add (K.add (K.add (M 0 0) (M 1 1)) (M 2 2)) (M 3 3))) (K.neg (K.mul (K.add K.one (K.add K.one K.one)) (K.mul (K.add (K.add (K.add (M 0 0) (M 1 1)) (M 2 2)) (M 3 3)) (K.add (K.add (K.add (K.add (K.add (K.add (K.mul (M 0 0) (M 1 1)) (K.neg (K.mul (M 0 1  -- (+1565 chars; full line in the Zenodo source)
  rw [trace_congr K 4 _ (mmul (toSemi K) 4 (mmul (toSemi K) 4 M M) M) (fun i hi => mpow3 K M 4 i i hi)]
  show (K.add (K.add (K.mul (K.add (K.mul (M 0 0) (M 0 0)) (K.add (K.mul (M 0 1) (M 1 0)) (K.add (K.mul (M 0 2) (M 2 0)) (K.add (K.mul (M 0 3) (M 3 0)) K.zero)))) (M 0 0)) (K.add (K.mul (K.add (K.mul (M 0 0) (M 0 1)) (K.add (K.mul (M 0 1) (M 1 1)) (K.add (K.mul (M 0 2) (M 2 1)) (K.add (K.mul (M 0 3) (M 3 1)) K.zero)))) (M 1 0)) (K.add (K.mul (K.add (K.mul (M 0 0) (M 0 2)) (K.add (K.mul (M 0 1) (M   -- (+2221 chars; full line in the Zenodo source)
  lib_ring

end LibTracePow4c

#print axioms LibTracePow4c.tr_cube4

lib/TracePow4d.lean

lean_all/lib/TracePow4d.lean

/-
  lib/TracePow4d.lean — T46 shared library, cycle 522 (G191): 4×4 Newton for k = 4.

  Any commutative ring, M 4×4, c₁…c₄ = sums of principal minors (c₄ = det M, coefficients from CharPoly34):
    tr_pow4_4    tr M⁴ = c₁ tr M³ − c₂ tr M² + c₃ tr M − 4c₄
  (Entries expanded, then lib_ring; formula generated by script. Large expression, so checked on Kamiak.)
  Lean 4 core only, no axioms.
-/
import lib.Sym2Tri

namespace LibTracePow4d
open DetChain LibRing LibSum LibSemi LibMat LibLegSum LibDet LibTracePow34

variable {R : Type} (K : CRing R) (M : Nat → Nat → R)

/-- **tr M⁴ = c₁ tr M³ − c₂ tr M² + c₃ tr M − 4 det M** (4×4). -/
theorem tr_pow4_4 : trace (toSemi K) 4 (mpow (toSemi K) 4 M 4)
    = K.add (K.add (K.add (K.mul (K.add (K.add (K.add (M 0 0) (M 1 1)) (M 2 2)) (M 3 3)) (trace (toSemi K) 4 (mpow (toSemi K) 4 M 3)))
        (K.neg (K.mul (K.add (K.add (K.add (K.add (K.add (K.add (K.mul (M 0 0) (M 1 1)) (K.neg (K.mul (M 0 1) (M 1 0)))) (K.add (K.mul (M 0 0) (M 2 2)) (K.neg (K.mul (M 0 2) (M 2 0))))) (K.add (K.mul (M 0 0) (M 3 3)) (K.neg (K.mul (M 0 3) (M 3 0))))) (K.add (K.mul (M 1 1) (M 2 2)) (K.neg (K.mul (M 1 2) (M 2 1))))) (K.add (K.mul (M 1 1) (M 3 3)) (K.neg (K.mul (M 1 3) (M 3 1))))) (K.add (K.mul (M 2   -- (+1301 chars; full line in the Zenodo source)
        (K.neg (K.mul (K.add (K.add K.one K.one) (K.add K.one K.one)) (K.add (K.add (K.add (K.add K.zero (K.mul (K.mul K.one (M 0 0)) (K.add (K.add (K.add (K.mul (K.mul (M 1 1) (M 2 2)) (M 3 3)) (K.mul (K.mul (M 1 2) (M 2 3)) (M 3 1))) (K.mul (K.mul (M 1 3) (M 2 1)) (M 3 2))) (K.neg (K.add (K.add (K.mul (K.mul (M 1 3) (M 2 2)) (M 3 1)) (K.mul (K.mul (M 1 1) (M 2 3)) (M 3 2))) (K.mul (K.mul (M 1 2)  -- (+1120 chars; full line in the Zenodo source)
  rw [trace_congr K 4 _ _ (fun i hi => LibCayley3c.mpow4 K M 4 i i hi),
    trace_congr K 4 (mpow (toSemi K) 4 M 3) _ (fun i hi => mpow3 K M 4 i i hi),
    trace_congr K 4 (mpow (toSemi K) 4 M 2) _ (fun i hi => mpow2 K M 4 i i hi)]
  show (K.add (K.add (K.mul (K.add (K.mul (K.add (K.mul (M 0 0) (M 0 0)) (K.add (K.mul (M 0 1) (M 1 0)) (K.add (K.mul (M 0 2) (M 2 0)) (K.add (K.mul (M 0 3) (M 3 0)) K.zero)))) (M 0 0)) (K.add (K.mul (K.add (K.mul (M 0 0) (M 0 1)) (K.add (K.mul (M 0 1) (M 1 1)) (K.add (K.mul (M 0 2) (M 2 1)) (K.add (K.mul (M 0 3) (M 3 1)) K.zero)))) (M 1 0)) (K.add (K.mul (K.add (K.mul (M 0 0) (M 0 2)) (K.add (K.m  -- (+16825 chars; full line in the Zenodo source)
  lib_ring

end LibTracePow4d

#print axioms LibTracePow4d.tr_pow4_4

lib/TracePowSim.lean

lean_all/lib/TracePowSim.lean

/-
  lib/TracePowSim.lean — T46 shared library, cycle 497 (G159): traces of powers of a diagonalizable matrix.

  Any commutative ring, n×n, PQ = I, QP = I (within range):
    tr_conj      tr(P M Q) = tr M
    pow_conj     (PAQ)^k = P A^k Q                                  (entrywise)
    dg_pow       (diag α)^k = diag(α^k)                             (entrywise)
    tr_dg        tr diag β = Σ β_i
    tr_pow_diag  PAQ = diag α ⇒ tr A^k = Σ_{i<n} α_i^k
  Lean 4 core only, no axioms.
-/
import lib.DsumSim

namespace LibTracePowSim
open DetChain LibRing LibSum LibSemi LibMat LibLegSum LibDet LibCircDet

variable {R : Type} (K : CRing R) (n : Nat)

theorem mmul_assoc' (A B C : Nat → Nat → R) (i j : Nat) :
    mmul (toSemi K) n (mmul (toSemi K) n A B) C i j = mmul (toSemi K) n A (mmul (toSemi K) n B C) i j :=
  mmul_assoc (toSemi K) n A B C i j

theorem tr_conj (P M Q : Nat → Nat → R) (hQP : ∀ i j, i < n → j < n → mmul (toSemi K) n Q P i j = idM K i j) :
    trace (toSemi K) n (mmul (toSemi K) n (mmul (toSemi K) n P M) Q) = trace (toSemi K) n M := by
  have e1 : trace (toSemi K) n (mmul (toSemi K) n (mmul (toSemi K) n P M) Q)
      = trace (toSemi K) n (mmul (toSemi K) n P (mmul (toSemi K) n M Q)) :=
    sum_ext (toSemi K) _ _ (fun i => mmul_assoc' K n P M Q i i) _
  rw [e1]
  refine Eq.trans (trace_comm (toSemi K) n P (mmul (toSemi K) n M Q)) ?_
  apply sum_ext_mem (toSemi K); intro i hi
  show mmul (toSemi K) n (mmul (toSemi K) n M Q) P i i = M i i
  rw [mmul_assoc' K n M Q P i i]
  refine Eq.trans (LibAdj.mmul_congr_r K n M _ (idM K) i i (fun l hl => hQP l i hl (LibBinom.mem_rng hi))) ?_
  exact mmul_ident_r (toSemi K) n M i (LibBinom.mem_rng hi)

theorem pow_conj (P A Q : Nat → Nat → R) (hPQ : ∀ i j, i < n → j < n → mmul (toSemi K) n P Q i j = idM K i j)
    (hQP : ∀ i j, i < n → j < n → mmul (toSemi K) n Q P i j = idM K i j) :
    ∀ k i j, i < n → j < n →
      mpow (toSemi K) n (mmul (toSemi K) n (mmul (toSemi K) n P A) Q) k i j
        = mmul (toSemi K) n (mmul (toSemi K) n P (mpow (toSemi K) n A k)) Q i j
  | 0, i, j, hi, hj => by
    show idM K i j = _
    rw [← hPQ i j hi hj]
    apply LibAdj.mmul_congr_l K n; intro l hl
    exact (mmul_ident_r (toSemi K) n P i hl).symm
  | k + 1, i, j, hi, hj => by
    show mmul (toSemi K) n (mpow (toSemi K) n (mmul (toSemi K) n (mmul (toSemi K) n P A) Q) k) (mmul (toSemi K) n (mmul (toSemi K) n P A) Q) i j
      = mmul (toSemi K) n (mmul (toSemi K) n P (mmul (toSemi K) n (mpow (toSemi K) n A k) A)) Q i j
    have hq : ∀ l, l < n → mmul (toSemi K) n Q (mmul (toSemi K) n (mmul (toSemi K) n P A) Q) l j
        = mmul (toSemi K) n A Q l j := by
      intro l hl
      refine Eq.trans (mmul_assoc' K n Q (mmul (toSemi K) n P A) Q l j).symm ?_
      apply LibAdj.mmul_congr_l K n; intro c hc
      refine Eq.trans (mmul_assoc' K n Q P A l c).symm ?_
      refine Eq.trans (LibAdj.mmul_congr_l K n _ (idM K) A l c (fun d hd => hQP l d hl hd)) ?_
      exact mmul_ident_l (toSemi K) n A hl c
    refine Eq.trans (LibAdj.mmul_congr_l K n _ (mmul (toSemi K) n (mmul (toSemi K) n P (mpow (toSemi K) n A k)) Q) _ i j
      (fun l hl => pow_conj P A Q hPQ hQP k i l hi hl)) ?_
    refine Eq.trans (mmul_assoc' K n _ Q (mmul (toSemi K) n (mmul (toSemi K) n P A) Q) i j) ?_
    refine Eq.trans (LibAdj.mmul_congr_r K n _ _ (mmul (toSemi K) n A Q) i j hq) ?_
    refine Eq.trans (mmul_assoc' K n (mmul (toSemi K) n P (mpow (toSemi K) n A k)) A Q i j).symm ?_
    apply LibAdj.mmul_congr_l K n; intro l _
    exact mmul_assoc' K n P (mpow (toSemi K) n A k) A i l

theorem dg_pow (α : Nat → R) : ∀ k i j, i < n → j < n → mpow (toSemi K) n (dg K α) k i j = dg K (fun i => pw K (α i) k) i j
  | 0, i, j, _, _ => by show idM K i j = K.mul K.one (idM K i j); lib_ring
  | k + 1, i, j, hi, hj => by
    show sum (toSemi K) (fun l => K.mul (mpow (toSemi K) n (dg K α) k i l) (K.mul (α j) (idM K l j))) (rng n)
      = K.mul (K.mul (pw K (α j) k) (α j)) (idM K i j)
    rw [sum_ext_mem (toSemi K) _ (fun l => K.mul (indK (toSemi K) (decide (l = j))) (K.mul (mpow (toSemi K) n (dg K α) k i l) (α j))) (rng n)
      (fun l hl => by
        show _ = K.mul (indK (toSemi K) (decide (l = j))) _
        show K.mul _ (K.mul (α j) (indK (toSemi K) (decide (l = j)))) = _; lib_ring)]
    rw [LibDetKernel.pick_rng K n j hj, dg_pow α k i j hi hj]
    show K.mul (K.mul (pw K (α j) k) (idM K i j)) (α j) = _; lib_ring

theorem mpow_congr (M M' : Nat → Nat → R) (h : ∀ i j, i < n → j < n → M i j = M' i j) :
    ∀ k i j, i < n → j < n → mpow (toSemi K) n M k i j = mpow (toSemi K) n M' k i j
  | 0, _, _, _, _ => rfl
  | k + 1, i, j, hi, hj => by
    show mmul (toSemi K) n (mpow (toSemi K) n M k) M i j = mmul (toSemi K) n (mpow (toSemi K) n M' k) M' i j
    refine Eq.trans (LibAdj.mmul_congr_l K n _ (mpow (toSemi K) n M' k) M i j (fun l hl => mpow_congr M M' h k i l hi hl)) ?_
    exact LibAdj.mmul_congr_r K n _ M M' i j (fun l hl => h l j hl hj)

theorem tr_dg (β : Nat → R) : trace (toSemi K) n (dg K β) = sum (toSemi K) β (rng n) :=
  sum_ext (toSemi K) _ _ (fun i => by
    show K.mul (β i) (indK (toSemi K) (decide (i = i))) = β i
    rw [decide_eq_true rfl]; show K.mul (β i) K.one = β i; lib_ring) _

/-- **PAQ = diag α ⇒ tr A^k = Σ α_i^k.** -/
theorem tr_pow_diag (P A Q : Nat → Nat → R) (α : Nat → R)
    (hPQ : ∀ i j, i < n → j < n → mmul (toSemi K) n P Q i j = idM K i j)
    (hQP : ∀ i j, i < n → j < n → mmul (toSemi K) n Q P i j = idM K i j)
    (hD : ∀ i j, i < n → j < n → mmul (toSemi K) n (mmul (toSemi K) n P A) Q i j = dg K α i j) (k : Nat) :
    trace (toSemi K) n (mpow (toSemi K) n A k) = sum (toSemi K) (fun i => pw K (α i) k) (rng n) := by
  rw [← tr_conj K n P (mpow (toSemi K) n A k) Q hQP]
  have e : trace (toSemi K) n (mmul (toSemi K) n (mmul (toSemi K) n P (mpow (toSemi K) n A k)) Q)
      = trace (toSemi K) n (mpow (toSemi K) n (mmul (toSemi K) n (mmul (toSemi K) n P A) Q) k) :=
    sum_ext_mem (toSemi K) _ _ _ (fun i hi =>
      (pow_conj K n P A Q hPQ hQP k i i (LibBinom.mem_rng hi) (LibBinom.mem_rng hi)).symm)
  rw [e]
  have e2 : trace (toSemi K) n (mpow (toSemi K) n (mmul (toSemi K) n (mmul (toSemi K) n P A) Q) k)
      = trace (toSemi K) n (mpow (toSemi K) n (dg K α) k) :=
    sum_ext_mem (toSemi K) _ _ _ (fun i hi =>
      mpow_congr K n _ (dg K α) hD k i i (LibBinom.mem_rng hi) (LibBinom.mem_rng hi))
  rw [e2]
  have e3 : trace (toSemi K) n (mpow (toSemi K) n (dg K α) k) = trace (toSemi K) n (dg K (fun i => pw K (α i) k)) :=
    sum_ext_mem (toSemi K) _ _ _ (fun i hi => dg_pow K n α k i i (LibBinom.mem_rng hi) (LibBinom.mem_rng hi))
  rw [e3, tr_dg]

end LibTracePowSim

#print axioms LibTracePowSim.tr_conj
#print axioms LibTracePowSim.pow_conj
#print axioms LibTracePowSim.dg_pow
#print axioms LibTracePowSim.tr_pow_diag

lib/TransferDet.lean

lean_all/lib/TransferDet.lean

/-
  lib/TransferDet.lean — T46 shared library, cycle 442 (G110): determinant of a product of transfer matrices.

  any commutative ring:
    tm a g j        [[a_j, −g_j], [1, 0]]          (one step of the continuant, Continuant.transfer)
    tprod a g k     tm 0 · tm 1 · … · tm (k−1)   (tprod 0 = I)
    det_tm          det tm j = g_j
    det_tprod       det (tprod k) = Π_{j<k} g_j   (ContCassini.pr)
  (matrix version of the Cassini identity (ContCassini): determinant of the product = product of the determinants.)
  Lean 4 core only, no axioms.
-/
import lib.HeckeU
import lib.DetMulSmall

namespace LibTransferDet
open DetChain LibRing LibSum LibSemi LibMat LibLegSum LibDet

variable {R : Type} (K : CRing R) (a g : Nat → R)

def tm (j : Nat) : Nat → Nat → R := fun i l =>
  cond (Nat.beq i 0) (cond (Nat.beq l 0) (a j) (K.neg (g j))) (cond (Nat.beq l 0) K.one K.zero)

def tprod : Nat → Nat → Nat → R
  | 0 => idM K
  | k + 1 => mmul (toSemi K) 2 (tprod k) (tm K a g k)

theorem det_tm (j : Nat) : det K 2 (tm K a g j) = g j := by
  rw [det_two]
  show K.add (K.mul (a j) K.zero) (K.neg (K.mul (K.neg (g j)) K.one)) = g j
  lib_ring

/-- **det Π tm_j = Π g_j.** -/
theorem det_tprod : ∀ k, det K 2 (tprod K a g k) = LibContCassini.pr K g k
  | 0 => det_ident K 2
  | k + 1 => by
    show det K 2 (mmul (toSemi K) 2 (tprod K a g k) (tm K a g k)) = K.mul (LibContCassini.pr K g k) (g k)
    rw [LibDetMulSmall.det2_mul, det_tprod k, det_tm]

end LibTransferDet

#print axioms LibTransferDet.det_tm
#print axioms LibTransferDet.det_tprod

lib/Tri2Satake.lean

lean_all/lib/Tri2Satake.lean

/-
  lib/Tri2Satake.lean — T46 shared library, cycle 537 (G215, G216): Hecke/Satake form of an upper-triangular 2×2 (no diagonalizability hypothesis).

  Any commutative ring, A 2×2, A₁₀ = 0, a = A₀₀, d = A₁₁:
    det2_tri    det formula a·d − A₀₁A₁₀ = a·d
    s_tri       s_k(tr A, det A) = a^k + d^k           (Trace2.tr_pow + TriEuler.tri_psum)
    u_tri       u_k(tr A, det A) = h_k(a, d)            (HeckeDiag.u_hS)
  (PsumDiag2·HeckeDiag are the diagonalizable versions; in Borel form the same conclusion holds without similarity.)
  Lean 4 core only, no axioms.
-/
import lib.CGTri

namespace LibTri2Satake
open DetChain LibRing LibSum LibSemi LibMat LibLegSum LibDet LibPow2 LibTridiagConst

variable {R : Type} (K : CRing R) (A : Nat → Nat → R) (h10 : A 1 0 = K.zero)

include h10 in
theorem det2_tri : K.add (K.mul (A 0 0) (A 1 1)) (K.neg (K.mul (A 0 1) (A 1 0))) = K.mul (A 0 0) (A 1 1) := by
  rw [h10]; lib_ring

include h10 in
/-- **s_k(tr A, det A) = a^k + d^k.** -/
theorem s_tri (k : Nat) :
    LibPsum2.s K (K.add (A 0 0) (A 1 1)) (K.add (K.mul (A 0 0) (A 1 1)) (K.neg (K.mul (A 0 1) (A 1 0)))) k
      = K.add (pw K (A 0 0) k) (pw K (A 1 1) k) := by
  rw [← LibTrace2.tr_pow, LibTriEuler.tri_psum K A k 2 (LibTriUnip.tri2 K A h10), sum_rng_succ, sum_rng_succ]
  show K.add (K.add K.zero (pw K (A 0 0) k)) (pw K (A 1 1) k) = _
  generalize pw K (A 0 0) k = p; generalize pw K (A 1 1) k = q
  lib_ring

include h10 in
/-- **u_k(tr A, det A) = h_k(a, d).** -/
theorem u_tri (k : Nat) :
    (uw K (K.add (A 0 0) (A 1 1)) (K.add (K.mul (A 0 0) (A 1 1)) (K.neg (K.mul (A 0 1) (A 1 0)))) k).1 = hS K (A 0 0) (A 1 1) k := by
  rw [det2_tri K A h10]; exact (LibHeckeDiag.u_hS K (A 0 0) (A 1 1) k).1

end LibTri2Satake

#print axioms LibTri2Satake.s_tri
#print axioms LibTri2Satake.u_tri

lib/TriBCRS.lean

lean_all/lib/TriBCRS.lean

/-
  lib/TriBCRS.lean — T46 shared library, cycle 533 (G204, G207): upper triangular GL3 base change coefficients, x coefficient of Rankin–Selberg.

  Any commutative ring:
    bc3_tri      T 3×3 upper triangular: det(I − xT²) = 1 − (e₁² − 2e₂)x + (e₂² − 2e₁e₃)x² − e₃²x³   (Borel version of agent1 bc3_poly)
    rs_x_coeff   T, U upper triangular: x coefficient cf of L(T⊗U) = −(Σ T_ii)(Σ U_jj)                    (KronTri.fl_tri + cf_one + TriDet)
  Lean 4 core only, no axioms.
-/
import lib.BaseChangeTri

namespace LibTriBCRS
open DetChain LibRing LibSum LibSemi LibMat LibLegSum LibDet LibCircDet LibEulerSum LibKron LibKronDetGen LibTriCoeff

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

/-- **GL3 upper triangular base change: Π(1 − t_i²x) in terms of e_k.** -/
theorem bc3_tri (T : Nat → Nat → R) (hT : ∀ i j, j < i → i < 3 → T i j = K.zero) (x : R) :
    det K 3 (imx K (mpow (toSemi K) 3 T 2) x) = K.add (K.add (K.add K.one (K.neg (K.mul (K.add (K.mul (LibNewtonTri4.esym4 K (fun i => T i i) 3).1 (LibNewtonTri4.esym4 K (fun i => T i i) 3).1) (K.neg (K.mul (K.add K.one K.one) (LibNewtonTri4.esym4 K (fun i => T i i) 3).2.1))) x))) (K.mul (K.add (K.mul (LibNewtonTri4.esym4 K (fun i => T i i) 3).2.1 (LibNewtonTri4.esym4 K (fun i => T i i) 3).2.1) (K  -- (+300 chars; full line in the Zenodo source)
  rw [LibBaseChangeTri.bc_tri K 3 T hT 2 x]
  show K.mul (K.add K.one (K.neg (K.mul (K.mul (K.mul K.one (T 0 0)) (T 0 0)) x))) (K.mul (K.add K.one (K.neg (K.mul (K.mul (K.mul K.one (T 1 1)) (T 1 1)) x))) (K.mul (K.add K.one (K.neg (K.mul (K.mul (K.mul K.one (T 2 2)) (T 2 2)) x))) K.one)) = K.add (K.add (K.add K.one (K.neg (K.mul (K.add (K.mul (K.add (K.add (K.add K.zero (T 0 0)) (T 1 1)) (T 2 2)) (K.add (K.add (K.add K.zero (T 0 0)) (T 1 1)  -- (+1306 chars; full line in the Zenodo source)
  lib_ring

/-- **x coefficient of L(T⊗U) = −tr T tr U** (upper triangular). -/
theorem rs_x_coeff (n' m' : Nat) (T U : Nat → Nat → R)
    (hT : ∀ i j, j < i → i < n' + 1 → T i j = K.zero) (hU : ∀ i j, j < i → i < m' + 1 → U i j = K.zero) :
    cf K (fun p => fl (m' + 1) (kron K T U) p p) ((n' + 1) * (m' + 1)) 1
      = K.neg (K.mul (sum (toSemi K) (fun i => pw K (T i i) 1) (rng (n' + 1))) (sum (toSemi K) (fun j => pw K (U j j) 1) (rng (m' + 1)))) := by
  rw [LibBaseChangeTri.cf_one, ← LibNewtonTri.ps_sum,
    ← LibTriEuler.tri_psum K (fl (m' + 1) (kron K T U)) 1 _ (LibKronTri.fl_tri K (n' + 1) m' T U hT hU),
    Nat.mul_comm (n' + 1) (m' + 1), LibTriDet.kron_tri_psum K n' m' T U hT hU 1]

end LibTriBCRS

#print axioms LibTriBCRS.bc3_tri
#print axioms LibTriBCRS.rs_x_coeff

lib/TriCH.lean

lean_all/lib/TriCH.lean

/-
  lib/TriCH.lean — T46 shared library, cycle 717 (G601–G604): Cayley–Hamilton for triangular matrices (product form, any n).

    Bk A k        A − A_kk I
    Pk A k        Π_{i<k} (A − A_ii I)   (multiplied on the right)
    Pk_cols       A upper triangular ⇒ the first k columns of P_k are 0
    tri_ch        P_n = 0   (triangular Cayley–Hamilton)
    unip_ch       all diagonal entries λ ⇒ (A − λI)^n = 0
    nil_pow       strictly upper triangular N ⇒ N^n = 0
  Lean 4 core only, no axioms.
-/
import lib.TraceDetApp

namespace LibTriCH
open DetChain LibRing LibSum LibMat

variable {R : Type} (K : CRing R) (n : Nat)

def Bk (A : Nat → Nat → R) (k : Nat) : Nat → Nat → R := fun i j => K.add (A i j) (K.neg (K.mul (A k k) (LibDet.idM K i j)))

def Pk (A : Nat → Nat → R) : Nat → Nat → Nat → R
  | 0 => LibDet.idM K
  | k + 1 => mmul (toSemi K) n (Pk A k) (Bk K A k)

theorem sum_zero_of (f : Nat → R) (h : ∀ l, f l = K.zero) (l : List Nat) : sum (toSemi K) f l = K.zero :=
  (sum_ext (toSemi K) f (fun _ => K.zero) h l).trans (sum_zero (toSemi K) l)

/-- **The first k columns of P_k are 0.** -/
theorem Pk_cols (A : Nat → Nat → R) (hA : ∀ s t, t < s → A s t = K.zero) :
    ∀ k, ∀ i j, j < k → Pk K n A k i j = K.zero
  | 0, _, _, h => absurd h (Nat.not_lt_zero _)
  | k + 1, i, j, hj => by
    show sum (toSemi K) (fun l => K.mul (Pk K n A k i l) (Bk K A k l j)) (rng n) = K.zero
    apply sum_zero_of; intro l
    cases Nat.lt_or_ge l k with
    | inl hl => rw [Pk_cols A hA k i l hl]; generalize Bk K A k l j = b; lib_ring
    | inr hl =>
      have hjk : j ≤ k := Nat.le_of_lt_succ hj
      cases Nat.lt_or_ge j l with
      | inl hjl =>
        have hB : Bk K A k l j = K.zero := by
          show K.add (A l j) (K.neg (K.mul (A k k) (LibDet.idM K l j))) = K.zero
          rw [hA l j hjl, LibPrincMinor.idM_ne K (fun e => Nat.lt_irrefl j (by rw [e] at hjl; exact hjl))]; lib_ring
        rw [hB]; generalize Pk K n A k i l = p; lib_ring
      | inr hlj =>
        have e1 : l = k := Nat.le_antisymm (Nat.le_trans hlj hjk) hl
        have e2 : j = k := Nat.le_antisymm hjk (e1 ▸ hlj)
        have hB : Bk K A k l j = K.zero := by
          rw [e1, e2]
          show K.add (A k k) (K.neg (K.mul (A k k) (LibDet.idM K k k))) = K.zero
          rw [LibPrincMinor.idM_eq]; generalize A k k = a; lib_ring
        rw [hB]; generalize Pk K n A k i l = p; lib_ring

/-- **Triangular Cayley–Hamilton: Π_{i<n} (A − A_ii I) = 0.** -/
theorem tri_ch (A : Nat → Nat → R) (hA : ∀ s t, t < s → A s t = K.zero) (i j : Nat) (hj : j < n) : Pk K n A n i j = K.zero :=
  Pk_cols K n A hA n i j hj

/-- If all diagonal entries are λ then P_k = (A − λI)^k. -/
theorem Pk_pow (A : Nat → Nat → R) (l : R) (hd : ∀ k, A k k = l) :
    ∀ k i j, Pk K n A k i j = mpow (toSemi K) n (fun a b => K.add (A a b) (K.neg (K.mul l (LibDet.idM K a b)))) k i j
  | 0, _, _ => rfl
  | k + 1, i, j => by
    show sum (toSemi K) (fun m => K.mul (Pk K n A k i m) (Bk K A k m j)) (rng n)
      = sum (toSemi K) (fun m => K.mul (mpow (toSemi K) n _ k i m) (K.add (A m j) (K.neg (K.mul l (LibDet.idM K m j))))) (rng n)
    apply sum_ext (toSemi K); intro m
    rw [Pk_pow A l hd k i m]
    show _ = K.mul _ (K.add (A m j) (K.neg (K.mul l (LibDet.idM K m j))))
    rw [show Bk K A k m j = K.add (A m j) (K.neg (K.mul (A k k) (LibDet.idM K m j))) from rfl, hd k]

/-- **(A − λI)^n = 0** (upper triangular, all diagonal entries λ). -/
theorem unip_ch (A : Nat → Nat → R) (hA : ∀ s t, t < s → A s t = K.zero) (l : R) (hd : ∀ k, A k k = l) (i j : Nat) (hj : j < n) :
    mpow (toSemi K) n (fun a b => K.add (A a b) (K.neg (K.mul l (LibDet.idM K a b)))) n i j = K.zero := by
  rw [← Pk_pow K n A l hd n i j]; exact tri_ch K n A hA i j hj

/-- **Strictly upper triangular N ⇒ N^n = 0.** -/
theorem nil_pow (N : Nat → Nat → R) (hN : ∀ s t, t ≤ s → N s t = K.zero) (i j : Nat) (hj : j < n) :
    mpow (toSemi K) n N n i j = K.zero := by
  have e := unip_ch K n N (fun s t h => hN s t (Nat.le_of_lt h)) K.zero (fun k => hN k k (Nat.le_refl k)) i j hj
  rw [← e]
  have : ∀ k a b, mpow (toSemi K) n N k a b = mpow (toSemi K) n (fun a b => K.add (N a b) (K.neg (K.mul K.zero (LibDet.idM K a b)))) k a b := by
    intro k; induction k with
    | zero => intro a b; rfl
    | succ k ih =>
      intro a b
      show sum (toSemi K) (fun m => K.mul (mpow (toSemi K) n N k a m) (N m b)) (rng n) = sum (toSemi K) (fun m => K.mul (mpow (toSemi K) n _ k a m) (K.add (N m b) (K.neg (K.mul K.zero (LibDet.idM K m b))))) (rng n)
      apply sum_ext (toSemi K); intro m
      rw [ih a m]; generalize N m b = x; generalize LibDet.idM K m b = y; generalize mpow (toSemi K) n _ k a m = z; lib_ring
  exact this n i j

end LibTriCH

#print axioms LibTriCH.tri_ch
#print axioms LibTriCH.unip_ch
#print axioms LibTriCH.nil_pow

lib/TriCoefAll.lean

lean_all/lib/TriCoefAll.lean

/-
  lib/TriCoefAll.lean — T46 shared library, cycle 705 (G574, G575): characteristic polynomial coefficients of triangular matrices = elementary symmetric functions of the diagonal (any k).

    prodL d l     Π_{s ∈ l} d s
    esym n k d    e_k(d₀, …, d_{n−1}) = Σ_{|S|=k} Π_{s∈S} d_s
    sel_sorted    selL n f is strictly increasing
    c_lower       lower triangular (s < t ⇒ M s t = 0) ⇒ c_k(M) = e_k(diagonal)
    c_upper       upper triangular (t < s ⇒ M s t = 0) ⇒ c_k(M) = e_k(diagonal)
    c_diag        diagonal matrix (both) ⇒ same
  Lean 4 core only, no axioms.
-/
import lib.CoefMinorsCor
import lib.DetUpper

namespace LibTriCoefAll
open DetChain LibRing LibDet LibPrincMinor LibDegSubsets LibCoefMinors

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

def prodL (d : Nat → R) : List Nat → R
  | [] => K.one
  | a :: l => K.mul (d a) (prodL d l)

def esym (n k : Nat) (d : Nat → R) : R := SG K n k (fun f => prodL K d (selL n f))

theorem diag_sel (M : Nat → Nat → R) : ∀ L : List Nat,
    diagProd K (fun i j => M (nthD L i) (nthD L j)) (lenL L) = prodL K (fun s => M s s) L
  | [] => rfl
  | a :: l => by
    show K.mul (M a a) (diagProd K (fun i j => M (nthD l i) (nthD l j)) (lenL l)) = K.mul (M a a) (prodL K _ l)
    rw [diag_sel M l]

theorem nth_all (P : Nat → Prop) : ∀ (l : List Nat) (i : Nat), AllL P l → i < lenL l → P (nthD l i)
  | [], _, _, h => absurd h (Nat.not_lt_zero _)
  | _ :: _, 0, ⟨h, _⟩, _ => h
  | _ :: l, i + 1, ⟨_, h⟩, hi => nth_all P l i h (Nat.lt_of_succ_lt_succ hi)

def Sorted (L : List Nat) : Prop := ∀ i j, i < j → j < lenL L → nthD L i < nthD L j

theorem sorted_app (l : List Nat) (a : Nat) (hs : Sorted l) (ha : AllL (fun x => x < a) l) : Sorted (appL l [a]) := by
  intro i j hij hj
  rw [len_app1] at hj
  rw [nth_app_lt l a i (Nat.lt_of_lt_of_le hij (Nat.le_of_lt_succ hj))]
  cases Nat.lt_or_ge j (lenL l) with
  | inl h => rw [nth_app_lt l a j h]; exact hs i j hij h
  | inr h =>
    have : j = lenL l := Nat.le_antisymm (Nat.le_of_lt_succ hj) h
    subst this
    rw [nth_app_eq]
    exact nth_all _ l i ha hij

theorem sel_sorted : ∀ (n : Nat) (f : Nat → Bool), Sorted (selL n f)
  | 0, _ => fun _ _ _ h => absurd h (Nat.not_lt_zero _)
  | n + 1, f => by
    show Sorted (cond (f n) _ _)
    cases f n with
    | true => exact sorted_app _ n (sel_sorted n f) (selL_lt n f)
    | false => exact sel_sorted n f

/-- **Lower triangular: c_k = e_k(diagonal).** -/
theorem c_lower (k n : Nat) (M : Nat → Nat → R) (h : ∀ s t, s < t → M s t = K.zero) :
    LibCharPolyC.c K k n M = esym K n k (fun s => M s s) := by
  rw [c_minors]
  apply SG_congr; intro f
  unfold PM
  rw [det_lower K _ _ (fun i j hij hj => h _ _ (sel_sorted n f i j hij hj)), diag_sel]

/-- **Upper triangular: c_k = e_k(diagonal).** -/
theorem c_upper (k n : Nat) (M : Nat → Nat → R) (h : ∀ s t, t < s → M s t = K.zero) :
    LibCharPolyC.c K k n M = esym K n k (fun s => M s s) := by
  rw [c_minors]
  apply SG_congr; intro f
  unfold PM
  rw [LibDetUpper.det_upper K _ _ (fun i j hji hi => h _ _ (sel_sorted n f j i hji hi)), diag_sel]

/-- **Diagonal matrix: c_k = e_k(d).** -/
theorem c_diag (k n : Nat) (M : Nat → Nat → R) (h : ∀ s t, s ≠ t → M s t = K.zero) :
    LibCharPolyC.c K k n M = esym K n k (fun s => M s s) :=
  c_lower K k n M (fun s t hst => h s t (Nat.ne_of_lt hst))

end LibTriCoefAll

#print axioms LibTriCoefAll.sel_sorted
#print axioms LibTriCoefAll.c_lower
#print axioms LibTriCoefAll.c_upper
#print axioms LibTriCoefAll.c_diag

lib/TriCoeff.lean

lean_all/lib/TriCoeff.lean

/-
  lib/TriCoeff.lean — T46 shared library, cycle 531 (G198): all coefficients of an upper triangular n×n local factor.

  Any commutative ring, t : Nat → R:
    cf n k        x^k coefficient of Π_{i<n}(1 − t_i x) (cf (n+1) (k+1) = cf n (k+1) − t_n cf n k)
    ev f N x      Σ_{k<N} f k x^k (own recursion)
    cf_top        k > n ⇒ cf n k = 0
    ev_mul        g 0 = f 0, g (k+1) = f (k+1) − t f k ⇒ ev g (N+1) = ev f (N+1) − t x ev f N
    prod_ev       Π_{i<n}(1 − t_i x) = Σ_{k≤n} cf n k x^k
    tri_coeff     T upper triangular ⇒ det(I − xT) = Σ_{k≤n} cf n k x^k   (t_i = T_ii; cf n k = (−1)^k e_k)
  Lean 4 core only, no axioms.
-/
import lib.Dsum22

namespace LibTriCoeff
open DetChain LibRing LibSum LibSemi LibMat LibLegSum LibDet LibCircDet LibEulerSum LibTriEuler

variable {R : Type} (K : CRing R) (t : Nat → R)

def cf : Nat → Nat → R
  | 0, 0 => K.one
  | 0, _ + 1 => K.zero
  | _ + 1, 0 => K.one
  | n + 1, k + 1 => K.add (cf n (k + 1)) (K.neg (K.mul (t n) (cf n k)))

def ev (f : Nat → R) (x : R) : Nat → R
  | 0 => K.zero
  | N + 1 => K.add (ev f x N) (K.mul (f N) (pw K x N))

theorem cf_zero : ∀ n, cf K t n 0 = K.one
  | 0 => rfl
  | _ + 1 => rfl

theorem cf_top : ∀ n k, n < k → cf K t n k = K.zero
  | 0, 0, h => absurd h (Nat.lt_irrefl 0)
  | 0, _ + 1, _ => rfl
  | _ + 1, 0, h => absurd h (Nat.not_lt_zero _)
  | n + 1, k + 1, h => by
    show K.add (cf K t n (k + 1)) (K.neg (K.mul (t n) (cf K t n k))) = K.zero
    rw [cf_top n (k + 1) (Nat.lt_trans (Nat.lt_of_succ_lt_succ h) (Nat.lt_succ_self k)),
      cf_top n k (Nat.lt_of_succ_lt_succ h)]
    lib_ring

theorem ev_mul (f g : Nat → R) (s x : R) (h0 : g 0 = f 0) (hs : ∀ k, g (k + 1) = K.add (f (k + 1)) (K.neg (K.mul s (f k)))) :
    ∀ N, ev K g x (N + 1) = K.add (ev K f x (N + 1)) (K.neg (K.mul (K.mul s x) (ev K f x N)))
  | 0 => by
    show K.add K.zero (K.mul (g 0) K.one) = K.add (K.add K.zero (K.mul (f 0) K.one)) (K.neg (K.mul (K.mul s x) K.zero))
    rw [h0]; lib_ring
  | N + 1 => by
    show K.add (ev K g x (N + 1)) (K.mul (g (N + 1)) (K.mul (pw K x N) x))
      = K.add (K.add (K.add (ev K f x N) (K.mul (f N) (pw K x N))) (K.mul (f (N + 1)) (K.mul (pw K x N) x)))
          (K.neg (K.mul (K.mul s x) (K.add (ev K f x N) (K.mul (f N) (pw K x N)))))
    rw [ev_mul f g s x h0 hs N, hs N]
    show K.add (K.add (K.add (ev K f x N) (K.mul (f N) (pw K x N))) (K.neg (K.mul (K.mul s x) (ev K f x N))))
        (K.mul (K.add (f (N + 1)) (K.neg (K.mul s (f N)))) (K.mul (pw K x N) x)) = _
    generalize ev K f x N = E; generalize pw K x N = P
    lib_ring

/-- **Π_{i<n}(1 − t_i x) = Σ_{k≤n} cf n k x^k.** -/
theorem prod_ev (x : R) : ∀ n, prodN K (fun i => K.add K.one (K.neg (K.mul (t i) x))) n = ev K (cf K t n) x (n + 1)
  | 0 => by show K.one = K.add K.zero (K.mul K.one K.one); lib_ring
  | n + 1 => by
    rw [prodN_last, prod_ev x n,
      ev_mul K (cf K t n) (cf K t (n + 1)) (t n) x (by rw [cf_zero, cf_zero]) (fun k => rfl) (n + 1)]
    show _ = K.add (K.add (ev K (cf K t n) x (n + 1)) (K.mul (cf K t n (n + 1)) (pw K x (n + 1)))) _
    rw [cf_top K t n (n + 1) (Nat.lt_succ_self n)]
    generalize ev K (cf K t n) x (n + 1) = E; generalize pw K x (n + 1) = P
    lib_ring

/-- **T upper triangular: det(I − xT) = Σ_{k≤n} cf n k x^k.** -/
theorem tri_coeff (n : Nat) (T : Nat → Nat → R) (x : R) (hT : ∀ i j, j < i → i < n → T i j = K.zero) :
    det K n (imx K T x) = ev K (cf K (fun i => T i i) n) x (n + 1) := by
  rw [tri_euler K T x n hT]; exact prod_ev K (fun i => T i i) x n

end LibTriCoeff

#print axioms LibTriCoeff.cf_top
#print axioms LibTriCoeff.ev_mul
#print axioms LibTriCoeff.prod_ev
#print axioms LibTriCoeff.tri_coeff

lib/TriDet.lean

lean_all/lib/TriDet.lean

/-
  lib/TriDet.lean — T46 shared library, cycle 525 (G193, G195): determinants of upper-triangular matrices, traces of tensor powers.

  Any commutative ring, T, U upper triangular:
    det_tri        det T = Π_{i<n} T_ii                                (induction via DetBlock)
    prodN_mul      Π (f i · g i) = Π f · Π g
    det_tri_mul    det(TU) = Π T_ii · Π U_ii                          (TU is upper triangular by TriMul)
    kron_tri_psum  tr(fl(T⊗U))^k = (Σ T_ii^k)(Σ U_jj^k)                (KronPowFlat + TriEuler.tri_psum)
  Lean 4 core only, no axioms.
-/
import lib.NewtonTri

namespace LibTriDet
open DetChain LibRing LibSum LibSemi LibMat LibLegSum LibDet LibCircDet LibEulerSum LibKron LibKronDetGen LibTriEuler

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

/-- **det T = Π T_ii** (upper triangular). -/
theorem det_tri (T : Nat → Nat → R) : ∀ n, (∀ i j, j < i → i < n → T i j = K.zero) →
    det K n T = prodN K (fun i => T i i) n
  | 0, _ => rfl
  | n + 1, hT => by
    rw [LibDetBlock.det_block K 1 n T (fun i j hi hi' hj => hT i j (Nat.lt_of_lt_of_le hj hi) hi'),
      det_tri T n (fun i j hji hi => hT i j hji (Nat.lt_succ_of_lt hi)), prodN_last, LibDet.det_one]
    rfl

theorem prodN_mul (f g : Nat → R) : ∀ n, prodN K (fun i => K.mul (f i) (g i)) n = K.mul (prodN K f n) (prodN K g n)
  | 0 => by show K.one = K.mul K.one K.one; lib_ring
  | n + 1 => by
    rw [prodN_last, prodN_mul f g n, prodN_last K n f, prodN_last K n g]
    generalize prodN K f n = a; generalize prodN K g n = b
    lib_ring

/-- **det(TU) = Π T_ii Π U_ii** (upper triangular). -/
theorem det_tri_mul (n : Nat) (T U : Nat → Nat → R)
    (hT : ∀ i j, j < i → i < n → T i j = K.zero) (hU : ∀ i j, j < i → i < n → U i j = K.zero) :
    det K n (mmul (toSemi K) n T U) = K.mul (prodN K (fun i => T i i) n) (prodN K (fun i => U i i) n) := by
  rw [det_tri K _ n (fun i j hji hi => LibTriMul.tri_mul_lower K n T U hT hU i j hji hi), ← prodN_mul]
  exact LibKronTri.prodN_ext K n _ _ (fun i hi => LibTriMul.tri_mul_diag K n T U hT hU i hi)

/-- **tr(T⊗U)^k = (Σ T_ii^k)(Σ U_jj^k)** (upper triangular). -/
theorem kron_tri_psum (n' m' : Nat) (T U : Nat → Nat → R)
    (hT : ∀ i j, j < i → i < n' + 1 → T i j = K.zero) (hU : ∀ i j, j < i → i < m' + 1 → U i j = K.zero) (k : Nat) :
    trace (toSemi K) ((m' + 1) * (n' + 1)) (mpow (toSemi K) ((m' + 1) * (n' + 1)) (fl (m' + 1) (kron K T U)) k)
      = K.mul (sum (toSemi K) (fun i => pw K (T i i) k) (rng (n' + 1))) (sum (toSemi K) (fun j => pw K (U j j) k) (rng (m' + 1))) := by
  rw [LibKronPowFlat.tr_fl_pow, tri_psum K T k (n' + 1) hT, tri_psum K U k (m' + 1) hU]

end LibTriDet

#print axioms LibTriDet.det_tri
#print axioms LibTriDet.det_tri_mul
#print axioms LibTriDet.kron_tri_psum

lib/TriE2.lean

lean_all/lib/TriE2.lean

/-
  lib/TriE2.lean — T46 shared library, cycle 622 (G389): x² coefficient of block upper-triangular matrices.

  Any commutative ring, M (n+m)×(n+m) with zero lower-left block (n ≤ i, j < n ⇒ M i j = 0), shf n M i j = M (n+i) (n+j):
    tr_split   tr_{n+m} M = tr_n M + tr_m (shf n M)
    tri_e2     e₂(M) = e₂(upper left) + tr(upper left)·tr(lower right) + e₂(lower right)     (upper-right block arbitrary)
  Lean 4 core only, no axioms.
-/
import lib.CycDet

namespace LibTriE2
open DetChain LibRing LibSum LibSemi LibMat LibLegSum LibCharQuad LibDsumPow LibE2Dsum

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

def shf (n : Nat) (M : Nat → Nat → R) : Nat → Nat → R := fun i j => M (n + i) (n + j)

theorem tr_split (n m : Nat) (M : Nat → Nat → R) :
    trace (toSemi K) (n + m) M = K.add (trace (toSemi K) n M) (trace (toSemi K) m (shf n M)) :=
  split_nm K n m _

/-- **e₂(block upper triangular) = e₂(A) + trA·trC + e₂(C).** -/
theorem tri_e2 (m : Nat) : ∀ (n : Nat) (M : Nat → Nat → R), (∀ i j, n ≤ i → j < n → M i j = K.zero) →
    e2 K (n + m) M = K.add (K.add (e2 K n M) (K.mul (trace (toSemi K) n M) (trace (toSemi K) m (shf n M)))) (e2 K m (shf n M))
  | 0, M, _ => by
    rw [Nat.zero_add]
    refine Eq.trans (e2_ext K m _ (shf 0 M) (fun i j _ _ => by show M i j = M (0 + i) (0 + j); rw [Nat.zero_add, Nat.zero_add])) ?_
    show _ = K.add (K.add K.zero (K.mul K.zero _)) _
    lib_ring
  | n + 1, M, hz => by
    rw [Nat.succ_add]
    show K.add (K.add (e2 K (n + m) (sh M)) (K.mul (M 0 0) (trace (toSemi K) (n + m) (sh M))))
        (K.neg (sum (toSemi K) (fun t => K.mul (M 0 (t + 1)) (M (t + 1) 0)) (rng (n + m))))
      = K.add (K.add (K.add (K.add (e2 K n (sh M)) (K.mul (M 0 0) (trace (toSemi K) n (sh M))))
          (K.neg (sum (toSemi K) (fun t => K.mul (M 0 (t + 1)) (M (t + 1) 0)) (rng n))))
          (K.mul (trace (toSemi K) (n + 1) M) (trace (toSemi K) m (shf (n + 1) M)))) (e2 K m (shf (n + 1) M))
    have hz' : ∀ i j, n ≤ i → j < n → sh M i j = K.zero := fun i j hi hj => hz _ _ (Nat.succ_le_succ hi) (Nat.succ_lt_succ hj)
    have hs : ∀ i j, i < m → j < m → shf n (sh M) i j = shf (n + 1) M i j := fun i j _ _ => by
      show M (n + i + 1) (n + j + 1) = M (n + 1 + i) (n + 1 + j); rw [Nat.succ_add, Nat.succ_add]
    rw [tri_e2 m n (sh M) hz', tr_split K n m (sh M), e2_ext K m _ _ hs, tr_ext K m _ _ hs, split_nm]
    have h0 : sum (toSemi K) (fun t => K.mul (M 0 (n + t + 1)) (M (n + t + 1) 0)) (rng m) = K.zero := by
      refine Eq.trans (sum_ext (toSemi K) _ (fun _ => K.zero) (fun t => by
        rw [hz (n + t + 1) 0 (Nat.succ_le_succ (Nat.le_add_right n t)) (Nat.succ_pos n)]
        show K.mul _ K.zero = K.zero; generalize M 0 (n + t + 1) = u; lib_ring) _) ?_
      exact sum_zero (toSemi K) _
    rw [h0, show trace (toSemi K) (n + 1) M = K.add (M 0 0) (trace (toSemi K) n (sh M)) from LibDetKernel.sum_split0 K n _]
    generalize e2 K n (sh M) = E; generalize trace (toSemi K) n (sh M) = T
    generalize trace (toSemi K) m (shf (n + 1) M) = b; generalize e2 K m (shf (n + 1) M) = f
    generalize sum (toSemi K) (fun t => K.mul (M 0 (t + 1)) (M (t + 1) 0)) (rng n) = S; generalize M 0 0 = a
    lib_ring

end LibTriE2

#print axioms LibTriE2.tri_e2

lib/TriE3.lean

lean_all/lib/TriE3.lean

/-
  lib/TriE3.lean — T46 shared library, cycle 623 (G390): x³ coefficient of block upper-triangular matrices.

  Any commutative ring, M (n+m)², lower-left block 0:
    tri_e3   e3(M) = e3(A) + e2(A)·trC + trA·e2(C) + e3(C)     (A = upper-left n², C = shf n M)
  Lean 4 core only, no axioms.
-/
import lib.TriE2

namespace LibTriE3
open DetChain LibRing LibSum LibSemi LibMat LibLegSum LibCharQuad LibDsumPow LibE2Dsum LibE3Rec LibE3Dsum LibTriE2

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

theorem inner_split (n m : Nat) (M : Nat → Nat → R) (hz : ∀ i j, n + 1 ≤ i → j < n + 1 → M i j = K.zero) (t : Nat) :
    sum (toSemi K) (fun r => K.mul (M (t + 1) (r + 1)) (M (r + 1) 0)) (rng (n + m))
      = sum (toSemi K) (fun r => K.mul (M (t + 1) (r + 1)) (M (r + 1) 0)) (rng n) := by
  rw [split_nm]
  have h0 : sum (toSemi K) (fun r => K.mul (M (t + 1) (n + r + 1)) (M (n + r + 1) 0)) (rng m) = K.zero := by
    refine Eq.trans (sum_ext (toSemi K) _ (fun _ => K.zero) (fun r => by
      rw [hz (n + r + 1) 0 (Nat.succ_le_succ (Nat.le_add_right n r)) (Nat.succ_pos n)]
      show K.mul _ K.zero = K.zero; generalize M (t + 1) (n + r + 1) = u; lib_ring) _) ?_
    exact sum_zero (toSemi K) _
  rw [h0]; generalize sum (toSemi K) _ (rng n) = S; lib_ring

theorem outer_split (n m : Nat) (M : Nat → Nat → R) (hz : ∀ i j, n + 1 ≤ i → j < n + 1 → M i j = K.zero) (X : R) :
    sum (toSemi K) (fun t => K.mul (M 0 (t + 1))
      (K.add (K.mul (M (t + 1) 0) X) (K.neg (sum (toSemi K) (fun r => K.mul (M (t + 1) (r + 1)) (M (r + 1) 0)) (rng (n + m)))))) (rng (n + m))
      = sum (toSemi K) (fun t => K.mul (M 0 (t + 1))
          (K.add (K.mul (M (t + 1) 0) X) (K.neg (sum (toSemi K) (fun r => K.mul (M (t + 1) (r + 1)) (M (r + 1) 0)) (rng n))))) (rng n) := by
  rw [split_nm]
  have h0 : sum (toSemi K) (fun t => K.mul (M 0 (n + t + 1))
      (K.add (K.mul (M (n + t + 1) 0) X) (K.neg (sum (toSemi K) (fun r => K.mul (M (n + t + 1) (r + 1)) (M (r + 1) 0)) (rng (n + m)))))) (rng m) = K.zero := by
    refine Eq.trans (sum_ext (toSemi K) _ (fun _ => K.zero) (fun t => by
      rw [inner_split K n m M hz]
      have hin : sum (toSemi K) (fun r => K.mul (M (n + t + 1) (r + 1)) (M (r + 1) 0)) (rng n) = K.zero := by
        refine Eq.trans (sum_ext_mem (toSemi K) _ (fun _ => K.zero) _ (fun r hr => by
          rw [hz (n + t + 1) (r + 1) (Nat.succ_le_succ (Nat.le_add_right n t)) (Nat.succ_lt_succ (LibBinom.mem_rng hr))]
          exact DetChain.zero_mul' K _)) ?_
        exact sum_zero (toSemi K) _
      rw [hin, hz (n + t + 1) 0 (Nat.succ_le_succ (Nat.le_add_right n t)) (Nat.succ_pos n)]
      generalize M 0 (n + t + 1) = u; lib_ring) _) ?_
    exact sum_zero (toSemi K) _
  rw [h0]
  refine Eq.trans (congrArg (fun z => K.add z K.zero) (sum_ext (toSemi K) _ _ (fun t => by rw [inner_split K n m M hz]) _)) ?_
  generalize sum (toSemi K) _ (rng n) = S; lib_ring

/-- **e3(block upper triangular) = e3A + e2A·trC + trA·e2C + e3C.** -/
theorem tri_e3 (m : Nat) : ∀ (n : Nat) (M : Nat → Nat → R), (∀ i j, n ≤ i → j < n → M i j = K.zero) →
    e3 K (n + m) M
      = K.add (K.add (K.add (e3 K n M) (K.mul (e2 K n M) (trace (toSemi K) m (shf n M)))) (K.mul (trace (toSemi K) n M) (e2 K m (shf n M)))) (e3 K m (shf n M))
  | 0, M, _ => by
    rw [Nat.zero_add]
    refine Eq.trans (e3_ext K m _ (shf 0 M) (fun i j _ _ => by show M i j = M (0 + i) (0 + j); rw [Nat.zero_add, Nat.zero_add])) ?_
    show _ = K.add (K.add (K.add K.zero (K.mul K.zero _)) (K.mul K.zero _)) _
    lib_ring
  | n + 1, M, hz => by
    rw [Nat.succ_add]
    show K.add (K.add (e3 K (n + m) (sh M)) (K.mul (M 0 0) (e2 K (n + m) (sh M))))
        (K.neg (sum (toSemi K) (fun t => K.mul (M 0 (t + 1))
          (K.add (K.mul (M (t + 1) 0) (trace (toSemi K) (n + m) (sh M)))
            (K.neg (sum (toSemi K) (fun r => K.mul (M (t + 1) (r + 1)) (M (r + 1) 0)) (rng (n + m)))))) (rng (n + m))))
      = K.add (K.add (K.add (K.add (K.add (e3 K n (sh M)) (K.mul (M 0 0) (e2 K n (sh M))))
          (K.neg (sum (toSemi K) (fun t => K.mul (M 0 (t + 1))
            (K.add (K.mul (M (t + 1) 0) (trace (toSemi K) n (sh M)))
              (K.neg (sum (toSemi K) (fun r => K.mul (M (t + 1) (r + 1)) (M (r + 1) 0)) (rng n))))) (rng n))))
          (K.mul (K.add (K.add (e2 K n (sh M)) (K.mul (M 0 0) (trace (toSemi K) n (sh M))))
            (K.neg (sum (toSemi K) (fun t => K.mul (M 0 (t + 1)) (M (t + 1) 0)) (rng n)))) (trace (toSemi K) m (shf (n + 1) M))))
          (K.mul (trace (toSemi K) (n + 1) M) (e2 K m (shf (n + 1) M)))) (e3 K m (shf (n + 1) M))
    have hz' : ∀ i j, n ≤ i → j < n → sh M i j = K.zero := fun i j hi hj => hz _ _ (Nat.succ_le_succ hi) (Nat.succ_lt_succ hj)
    have hs : ∀ i j, i < m → j < m → shf n (sh M) i j = shf (n + 1) M i j := fun i j _ _ => by
      show M (n + i + 1) (n + j + 1) = M (n + 1 + i) (n + 1 + j); rw [Nat.succ_add, Nat.succ_add]
    rw [tri_e3 m n (sh M) hz', tri_e2 K m n (sh M) hz', tr_split K n m (sh M), e3_ext K m _ _ hs, e2_ext K m _ _ hs, tr_ext K m _ _ hs,
      outer_split K n m M hz, split_X K M n,
      show trace (toSemi K) (n + 1) M = K.add (M 0 0) (trace (toSemi K) n (sh M)) from LibDetKernel.sum_split0 K n _]
    generalize sum (toSemi K) (fun t => K.mul (M 0 (t + 1))
        (K.add (K.mul (M (t + 1) 0) (trace (toSemi K) n (sh M))) (K.neg (sum (toSemi K) (fun r => K.mul (M (t + 1) (r + 1)) (M (r + 1) 0)) (rng n))))) (rng n) = Y
    generalize sum (toSemi K) (fun t => K.mul (M 0 (t + 1)) (M (t + 1) 0)) (rng n) = s
    generalize e3 K n (sh M) = E3; generalize e2 K n (sh M) = E2; generalize trace (toSemi K) n (sh M) = T
    generalize trace (toSemi K) m (shf (n + 1) M) = b; generalize e2 K m (shf (n + 1) M) = f; generalize e3 K m (shf (n + 1) M) = g
    generalize M 0 0 = a
    lib_ring

end LibTriE3

#print axioms LibTriE3.tri_e3

lib/TriE4.lean

lean_all/lib/TriE4.lean

/-
  lib/TriE4.lean — T46 shared library, cycle 633 (G411 = G408): x⁴ coefficient of block upper-triangular matrices.

  Any commutative ring, M (n+m)², lower-left block 0, C = shf n M:
    tq_lo, tq_mid, tq_hi, tq_low   block cases of q (0 if t is a row of the lower block)
    tri_e4   e4(M) = e4A + e3A·trC + e2A·e2C + trA·e3C + e4C
  Lean 4 core only, no axioms.
-/
import lib.E4Five

namespace LibTriE4
open DetChain LibRing LibSum LibSemi LibMat LibLegSum LibCharQuad LibDsumPow LibE2Dsum LibE3Rec LibE3Dsum LibE4Rec LibPermRows LibE2Minors LibTriE2 LibTriE3 LibE4Dsum

variable {R : Type} (K : CRing R) (n : Nat) (M : Nat → Nat → R) (hz : ∀ i j, n + 1 ≤ i → j < n + 1 → M i j = K.zero)

include hz in
theorem tq_mid (t r s : Nat) (hr : r < n) :
    q K M t r (n + s)
      = K.mul (M (n + s + 1) (n + s + 1)) (K.add (K.mul (M (t + 1) 0) (M (r + 1) (r + 1))) (K.neg (K.mul (M (t + 1) (r + 1)) (M (r + 1) 0)))) := by
  unfold q d3; dsimp only
  rw [hz (n + s + 1) 0 (Nat.succ_le_succ (Nat.le_add_right n s)) (Nat.succ_pos n),
    hz (n + s + 1) (r + 1) (Nat.succ_le_succ (Nat.le_add_right n s)) (Nat.succ_lt_succ hr)]
  generalize M (t + 1) 0 = a0; generalize M (t + 1) (r + 1) = a1; generalize M (t + 1) (n + s + 1) = a2
  generalize M (r + 1) 0 = b0; generalize M (r + 1) (r + 1) = b1; generalize M (r + 1) (n + s + 1) = b2
  generalize M (n + s + 1) (n + s + 1) = c2; lib_ring

include hz in
theorem tq_hi (t r s : Nat) :
    q K M t (n + r) (n + s) = K.mul (M (t + 1) 0) (m2 K (shf (n + 1) M) r s) := by
  unfold q d3 m2 shf; dsimp only
  rw [hz (n + r + 1) 0 (Nat.succ_le_succ (Nat.le_add_right n r)) (Nat.succ_pos n),
    hz (n + s + 1) 0 (Nat.succ_le_succ (Nat.le_add_right n s)) (Nat.succ_pos n),
    show n + 1 + r = n + r + 1 from Nat.succ_add n r, show n + 1 + s = n + s + 1 from Nat.succ_add n s]
  generalize M (t + 1) 0 = a; generalize M (t + 1) (n + r + 1) = a1; generalize M (t + 1) (n + s + 1) = a2
  generalize M (n + r + 1) (n + r + 1) = x; generalize M (n + r + 1) (n + s + 1) = y
  generalize M (n + s + 1) (n + r + 1) = z; generalize M (n + s + 1) (n + s + 1) = w; lib_ring

include hz in
/-- q = 0 if t is a row of the lower block. -/
theorem tq_low (t r s : Nat) (hrs : r < s) : q K M (n + t) r s = K.zero := by
  unfold q d3; dsimp only
  have hu0 : M (n + t + 1) 0 = K.zero := hz _ 0 (Nat.succ_le_succ (Nat.le_add_right n t)) (Nat.succ_pos n)
  rw [hu0]
  cases Nat.lt_or_ge r n with
  | inl hr =>
    rw [hz (n + t + 1) (r + 1) (Nat.succ_le_succ (Nat.le_add_right n t)) (Nat.succ_lt_succ hr)]
    cases Nat.lt_or_ge s n with
    | inl hs =>
      rw [hz (n + t + 1) (s + 1) (Nat.succ_le_succ (Nat.le_add_right n t)) (Nat.succ_lt_succ hs)]
      generalize M (r + 1) 0 = b0; generalize M (r + 1) (r + 1) = b1; generalize M (r + 1) (s + 1) = b2
      generalize M (s + 1) 0 = c0; generalize M (s + 1) (r + 1) = c1; generalize M (s + 1) (s + 1) = c2; lib_ring
    | inr hs =>
      rw [hz (s + 1) 0 (Nat.le_succ_of_le hs |> fun h => Nat.succ_le_succ hs) (Nat.succ_pos n),
        hz (s + 1) (r + 1) (Nat.succ_le_succ hs) (Nat.succ_lt_succ hr)]
      generalize M (r + 1) 0 = b0; generalize M (r + 1) (r + 1) = b1; generalize M (r + 1) (s + 1) = b2
      generalize M (n + t + 1) (s + 1) = u2; generalize M (s + 1) (s + 1) = c2; lib_ring
  | inr hr =>
    have hs : n ≤ s := Nat.le_trans hr (Nat.le_of_lt hrs)
    rw [hz (r + 1) 0 (Nat.succ_le_succ hr) (Nat.succ_pos n), hz (s + 1) 0 (Nat.succ_le_succ hs) (Nat.succ_pos n)]
    generalize M (n + t + 1) (r + 1) = u1; generalize M (n + t + 1) (s + 1) = u2
    generalize M (r + 1) (r + 1) = b1; generalize M (r + 1) (s + 1) = b2
    generalize M (s + 1) (r + 1) = c1; generalize M (s + 1) (s + 1) = c2; lib_ring

include hz in
theorem Q3_tri (m t : Nat) (ht : t < n) :
    Q3 K M (n + m) t
      = K.add (K.add (Q3 K M n t)
          (K.mul (trace (toSemi K) m (shf (n + 1) M)) (K.add (K.mul (M (t + 1) 0) (trace (toSemi K) n (sh M)))
            (K.neg (sum (toSemi K) (fun r => K.mul (M (t + 1) (r + 1)) (M (r + 1) 0)) (rng n))))))
          (K.mul (M (t + 1) 0) (e2 K m (shf (n + 1) M))) := by
  unfold Q3
  rw [split_nm]
  have htrC : trace (toSemi K) m (shf (n + 1) M) = sum (toSemi K) (fun s => M (n + s + 1) (n + s + 1)) (rng m) :=
    sum_ext (toSemi K) _ _ (fun s => by show M (n + 1 + s) (n + 1 + s) = _; rw [Nat.succ_add]) _
  have hlo_part : sum (toSemi K) (fun r => sum (toSemi K) (fun s => K.mul (indK (toSemi K) (decide (r < s))) (q K M t r s)) (rng (n + m))) (rng n)
      = K.add (sum (toSemi K) (fun r => sum (toSemi K) (fun s => K.mul (indK (toSemi K) (decide (r < s))) (q K M t r s)) (rng n)) (rng n))
          (K.mul (trace (toSemi K) m (shf (n + 1) M)) (K.add (K.mul (M (t + 1) 0) (trace (toSemi K) n (sh M)))
            (K.neg (sum (toSemi K) (fun r => K.mul (M (t + 1) (r + 1)) (M (r + 1) 0)) (rng n))))) := by
    rw [htrC]
    refine Eq.trans (sum_ext_mem (toSemi K) _ (fun r => K.add (sum (toSemi K) (fun s => K.mul (indK (toSemi K) (decide (r < s))) (q K M t r s)) (rng n))
        (K.mul (sum (toSemi K) (fun s => M (n + s + 1) (n + s + 1)) (rng m)) (K.add (K.mul (M (t + 1) 0) (M (r + 1) (r + 1))) (K.neg (K.mul (M (t + 1) (r + 1)) (M (r + 1) 0)))))) _ (fun r hr => by
      have hr' := LibBinom.mem_rng hr
      rw [split_nm]
      refine congr (congrArg K.add rfl) ?_
      refine Eq.trans (sum_ext (toSemi K) _ (fun s => K.mul (M (n + s + 1) (n + s + 1)) (K.add (K.mul (M (t + 1) 0) (M (r + 1) (r + 1))) (K.neg (K.mul (M (t + 1) (r + 1)) (M (r + 1) 0))))) (fun s => by
        rw [tq_mid K n M hz t r s hr', decide_eq_true (Nat.lt_of_lt_of_le hr' (Nat.le_add_right n s))]
        show K.mul K.one _ = _; generalize K.mul (M (n + s + 1) (n + s + 1)) _ = z; lib_ring) _) ?_
      exact (LibSum.sum_mul (toSemi K) _ (fun s => M (n + s + 1) (n + s + 1)) _).symm)) ?_
    refine (LibSum.sum_add (toSemi K) _ _ _).trans (congr (congrArg K.add rfl) ?_)
    refine Eq.trans (mul_sum (toSemi K) _ _ _).symm (congrArg (K.mul _) ?_)
    refine (LibSum.sum_add (toSemi K) _ _ _).trans ?_
    rw [LibSum.sum_neg K]
    exact congr (congrArg K.add (mul_sum (toSemi K) _ _ _).symm) rfl
  have hhi_part : sum (toSemi K) (fun r => sum (toSemi K) (fun s => K.mul (indK (toSemi K) (decide (n + r < s))) (q K M t (n + r) s)) (rng (n + m))) (rng m)
      = K.mul (M (t + 1) 0) (e2 K m (shf (n + 1) M)) := by
    rw [e2_minors K m _]; unfold S2m
    refine Eq.trans ?_ (mul_sum (toSemi K) _ _ _).symm
    apply sum_ext (toSemi K); intro r
    rw [split_nm]
    have hz0 : sum (toSemi K) (fun s => K.mul (indK (toSemi K) (decide (n + r < s))) (q K M t (n + r) s)) (rng n) = K.zero := by
      refine Eq.trans (sum_ext_mem (toSemi K) _ (fun _ => K.zero) _ (fun s hs => by
        rw [decide_eq_false (fun h => Nat.lt_irrefl _ (Nat.lt_of_lt_of_le (Nat.lt_of_le_of_lt (Nat.le_add_right n r) h) (Nat.le_of_lt (LibBinom.mem_rng hs))))]
        exact DetChain.zero_mul' K _)) ?_
      exact sum_zero (toSemi K) _
    rw [hz0]
    refine Eq.trans (K.zero_add _) ?_
    refine Eq.trans ?_ (mul_sum (toSemi K) _ _ _).symm
    apply sum_ext (toSemi K); intro s
    rw [tq_hi K n M hz t r s, LibGS.decide_congr (p := n + r < n + s) (q := r < s) (add_lt_l n r s) (add_lt_r n r s)]
    show _ = K.mul (M (t + 1) 0) (K.mul _ _)
    generalize indK (toSemi K) (decide (r < s)) = d; generalize m2 K (shf (n + 1) M) r s = x; generalize M (t + 1) 0 = a; lib_ring
  rw [hlo_part, hhi_part]

include hz in
theorem Q3_low (m t : Nat) : Q3 K M (n + m) (n + t) = K.zero := by
  unfold Q3
  refine Eq.trans (sum_ext (toSemi K) _ (fun _ => K.zero) (fun r => by
    refine Eq.trans (sum_ext (toSemi K) _ (fun _ => K.zero) (fun s => by
      cases Nat.lt_or_ge r s with
      | inl h => rw [tq_low K n M hz t r s h]; show K.mul _ K.zero = K.zero; generalize indK (toSemi K) (decide (r < s)) = d; lib_ring
      | inr h => rw [decide_eq_false (Nat.not_lt_of_le h)]; exact DetChain.zero_mul' K _) _) ?_
    exact sum_zero (toSemi K) _) _) ?_
  exact sum_zero (toSemi K) _

/-- **e4(block upper-triangular) = e4A + e3A·trC + e2A·e2C + trA·e3C + e4C.** -/
theorem tri_e4 (m : Nat) : ∀ (n : Nat) (M : Nat → Nat → R), (∀ i j, n ≤ i → j < n → M i j = K.zero) →
    e4 K (n + m) M
      = K.add (K.add (K.add (K.add (e4 K n M) (K.mul (e3 K n M) (trace (toSemi K) m (shf n M)))) (K.mul (e2 K n M) (e2 K m (shf n M))))
          (K.mul (trace (toSemi K) n M) (e3 K m (shf n M)))) (e4 K m (shf n M))
  | 0, M, _ => by
    rw [Nat.zero_add]
    refine Eq.trans (e4_ext K m _ (shf 0 M) (fun i j _ _ => by show M i j = M (0 + i) (0 + j); rw [Nat.zero_add, Nat.zero_add])) ?_
    show _ = K.add (K.add (K.add (K.add K.zero (K.mul K.zero _)) (K.mul K.zero _)) (K.mul K.zero _)) _
    lib_ring
  | n + 1, M, hz => by
    rw [Nat.succ_add]
    show K.add (K.add (e4 K (n + m) (sh M)) (K.mul (M 0 0) (e3 K (n + m) (sh M))))
        (K.neg (sum (toSemi K) (fun t => K.mul (M 0 (t + 1)) (Q3 K M (n + m) t)) (rng (n + m))))
      = K.add (K.add (K.add (K.add (K.add (K.add (e4 K n (sh M)) (K.mul (M 0 0) (e3 K n (sh M))))
            (K.neg (sum (toSemi K) (fun t => K.mul (M 0 (t + 1)) (Q3 K M n t)) (rng n))))
          (K.mul (K.add (K.add (e3 K n (sh M)) (K.mul (M 0 0) (e2 K n (sh M))))
            (K.neg (sum (toSemi K) (fun t => K.mul (M 0 (t + 1))
              (K.add (K.mul (M (t + 1) 0) (trace (toSemi K) n (sh M)))
                (K.neg (sum (toSemi K) (fun r => K.mul (M (t + 1) (r + 1)) (M (r + 1) 0)) (rng n))))) (rng n)))) (trace (toSemi K) m (shf (n + 1) M))))
          (K.mul (K.add (K.add (e2 K n (sh M)) (K.mul (M 0 0) (trace (toSemi K) n (sh M))))
            (K.neg (sum (toSemi K) (fun t => K.mul (M 0 (t + 1)) (M (t + 1) 0)) (rng n)))) (e2 K m (shf (n + 1) M))))
          (K.mul (trace (toSemi K) (n + 1) M) (e3 K m (shf (n + 1) M)))) (e4 K m (shf (n + 1) M))
    have hz' : ∀ i j, n ≤ i → j < n → sh M i j = K.zero := fun i j hi hj => hz _ _ (Nat.succ_le_succ hi) (Nat.succ_lt_succ hj)
    have hs : ∀ i j, i < m → j < m → shf n (sh M) i j = shf (n + 1) M i j := fun i j _ _ => by
      show M (n + i + 1) (n + j + 1) = M (n + 1 + i) (n + 1 + j); rw [Nat.succ_add, Nat.succ_add]
    rw [tri_e4 m n (sh M) hz', tri_e3 K m n (sh M) hz', e4_ext K m _ _ hs, e3_ext K m _ _ hs, e2_ext K m _ _ hs, tr_ext K m _ _ hs,
      split_nm, show trace (toSemi K) (n + 1) M = K.add (M 0 0) (trace (toSemi K) n (sh M)) from LibDetKernel.sum_split0 K n _]
    have hz0 : sum (toSemi K) (fun t => K.mul (M 0 (n + t + 1)) (Q3 K M (n + m) (n + t))) (rng m) = K.zero := by
      refine Eq.trans (sum_ext (toSemi K) _ (fun _ => K.zero) (fun t => by
        rw [Q3_low K n M hz m t]; show K.mul _ K.zero = K.zero; generalize M 0 (n + t + 1) = u; lib_ring) _) ?_
      exact sum_zero (toSemi K) _
    rw [hz0]
    have hlo : sum (toSemi K) (fun t => K.mul (M 0 (t + 1)) (Q3 K M (n + m) t)) (rng n)
        = K.add (K.add (sum (toSemi K) (fun t => K.mul (M 0 (t + 1)) (Q3 K M n t)) (rng n))
            (K.mul (trace (toSemi K) m (shf (n + 1) M)) (sum (toSemi K) (fun t => K.mul (M 0 (t + 1))
              (K.add (K.mul (M (t + 1) 0) (trace (toSemi K) n (sh M)))
                (K.neg (sum (toSemi K) (fun r => K.mul (M (t + 1) (r + 1)) (M (r + 1) 0)) (rng n))))) (rng n))))
            (K.mul (e2 K m (shf (n + 1) M)) (sum (toSemi K) (fun t => K.mul (M 0 (t + 1)) (M (t + 1) 0)) (rng n))) := by
      refine Eq.trans (sum_ext_mem (toSemi K) _ (fun t => K.add (K.add (K.mul (M 0 (t + 1)) (Q3 K M n t))
          (K.mul (trace (toSemi K) m (shf (n + 1) M)) (K.mul (M 0 (t + 1)) (K.add (K.mul (M (t + 1) 0) (trace (toSemi K) n (sh M)))
                (K.neg (sum (toSemi K) (fun r => K.mul (M (t + 1) (r + 1)) (M (r + 1) 0)) (rng n)))))))
          (K.mul (e2 K m (shf (n + 1) M)) (K.mul (M 0 (t + 1)) (M (t + 1) 0)))) _ (fun t ht => by
        have ht' := LibBinom.mem_rng ht
        rw [Q3_tri K n M hz m t ht']
        generalize Q3 K M n t = Qv; generalize trace (toSemi K) m (shf (n + 1) M) = b; generalize e2 K m (shf (n + 1) M) = f
        generalize K.add (K.mul (M (t + 1) 0) (trace (toSemi K) n (sh M))) (K.neg (sum (toSemi K) (fun r => K.mul (M (t + 1) (r + 1)) (M (r + 1) 0)) (rng n))) = Yv
        generalize M 0 (t + 1) = bb; generalize M (t + 1) 0 = cc
        lib_ring)) ?_
      refine (LibSum.sum_add (toSemi K) _ _ _).trans ?_
      refine congr (congrArg K.add ((LibSum.sum_add (toSemi K) _ _ _).trans (congr (congrArg K.add rfl) (mul_sum (toSemi K) _ _ _).symm))) ?_
      exact (mul_sum (toSemi K) _ _ _).symm
    rw [hlo]
    generalize sum (toSemi K) (fun t => K.mul (M 0 (t + 1)) (Q3 K M n t)) (rng n) = SQ
    generalize sum (toSemi K) (fun t => K.mul (M 0 (t + 1))
              (K.add (K.mul (M (t + 1) 0) (trace (toSemi K) n (sh M)))
                (K.neg (sum (toSemi K) (fun r => K.mul (M (t + 1) (r + 1)) (M (r + 1) 0)) (rng n))))) (rng n) = Y
    generalize sum (toSemi K) (fun t => K.mul (M 0 (t + 1)) (M (t + 1) 0)) (rng n) = s
    generalize e4 K n (sh M) = E4; generalize e3 K n (sh M) = E3; generalize e2 K n (sh M) = E2; generalize trace (toSemi K) n (sh M) = T
    generalize trace (toSemi K) m (shf (n + 1) M) = b; generalize e2 K m (shf (n + 1) M) = f; generalize e3 K m (shf (n + 1) M) = g
    generalize e4 K m (shf (n + 1) M) = hh; generalize M 0 0 = a
    show K.add (K.add _ _) (K.neg (K.add (K.add (K.add SQ (K.mul b Y)) (K.mul f s)) K.zero)) = _
    lib_ring

end LibTriE4

#print axioms LibTriE4.tq_mid
#print axioms LibTriE4.tq_hi
#print axioms LibTriE4.tq_low
#print axioms LibTriE4.Q3_tri
#print axioms LibTriE4.Q3_low
#print axioms LibTriE4.tri_e4

lib/TriE5.lean

lean_all/lib/TriE5.lean

/-
  lib/TriE5.lean — T46 shared library, cycle 658 (G467a): block cases of q4 in a block upper-triangular matrix.

  M (n+1+m)², lower-left block 0 (n+1 ≤ i, j < n+1 ⇒ M i j = 0), C = shf (n+1) M:
    tq4_mid1  q4(t, r, s, n+u) = M_{n+u+1,n+u+1} · q(t, r, s)
    tq4_mid2  q4(t, r, n+s, n+u) = m2(C; s, u) · (M_{t+1,0}M_{r+1,r+1} − M_{t+1,r+1}M_{r+1,0})
    tq4_hi3   q4(t, n+r, n+s, n+u) = M_{t+1,0} · m3(C; r, s, u)
    tq4_low   r < s < u ⇒ q4(n+t, r, s, u) = 0
  Lean 4 core only, no axioms.
-/
import lib.E5Minors

namespace LibTriE5
open DetChain LibRing LibSum LibSemi LibMat LibLegSum LibCharQuad LibE4Rec LibPermRows LibPermRows4 LibE5Rec LibE2Minors LibE3Minors LibTriE2

variable {R : Type} (K : CRing R) (n : Nat) (M : Nat → Nat → R) (hz : ∀ i j, n + 1 ≤ i → j < n + 1 → M i j = K.zero)

include hz in
theorem tq4_mid1 (t r s u : Nat) (hr : r < n) (hs : s < n) :
    q4 K M t r s (n + u) = K.mul (M (n + u + 1) (n + u + 1)) (q K M t r s) := by
  unfold q4 d4 q d3; dsimp only
  (try rw [hz (n + u + 1) 0 (Nat.succ_le_succ (Nat.le_add_right n u)) (Nat.succ_pos n)])
  (try rw [hz (n + u + 1) (r + 1) (Nat.succ_le_succ (Nat.le_add_right n u)) (Nat.succ_lt_succ hr)])
  (try rw [hz (n + u + 1) (s + 1) (Nat.succ_le_succ (Nat.le_add_right n u)) (Nat.succ_lt_succ hs)])
  generalize M (n + u + 1) (n + u + 1) = z; lib_ring

include hz in
theorem tq4_mid2 (t r s u : Nat) (hr : r < n) :
    q4 K M t r (n + s) (n + u)
      = K.mul (m2 K (shf (n + 1) M) s u) (K.add (K.mul (M (t + 1) 0) (M (r + 1) (r + 1))) (K.neg (K.mul (M (t + 1) (r + 1)) (M (r + 1) 0)))) := by
  unfold q4 d4 m2 shf d3; dsimp only
  rw [show n + 1 + s = n + s + 1 from Nat.succ_add n s, show n + 1 + u = n + u + 1 from Nat.succ_add n u]
  (try rw [hz (n + s + 1) 0 (Nat.succ_le_succ (Nat.le_add_right n s)) (Nat.succ_pos n)])
  (try rw [hz (n + s + 1) (r + 1) (Nat.succ_le_succ (Nat.le_add_right n s)) (Nat.succ_lt_succ hr)])
  (try rw [hz (n + u + 1) 0 (Nat.succ_le_succ (Nat.le_add_right n u)) (Nat.succ_pos n)])
  (try rw [hz (n + u + 1) (r + 1) (Nat.succ_le_succ (Nat.le_add_right n u)) (Nat.succ_lt_succ hr)])
  lib_ring

include hz in
theorem tq4_hi3 (t r s u : Nat) :
    q4 K M t (n + r) (n + s) (n + u) = K.mul (M (t + 1) 0) (m3 K (shf (n + 1) M) r s u) := by
  unfold q4 d4 m3 shf d3; dsimp only
  rw [show n + 1 + r = n + r + 1 from Nat.succ_add n r, show n + 1 + s = n + s + 1 from Nat.succ_add n s, show n + 1 + u = n + u + 1 from Nat.succ_add n u]
  (try rw [hz (n + r + 1) 0 (Nat.succ_le_succ (Nat.le_add_right n r)) (Nat.succ_pos n)])
  (try rw [hz (n + s + 1) 0 (Nat.succ_le_succ (Nat.le_add_right n s)) (Nat.succ_pos n)])
  (try rw [hz (n + u + 1) 0 (Nat.succ_le_succ (Nat.le_add_right n u)) (Nat.succ_pos n)])
  lib_ring

include hz in
theorem tq4_low (t r s u : Nat) (hrs : r < s) (hsu : s < u) : q4 K M (n + t) r s u = K.zero := by
  cases Nat.lt_or_ge u n with
  | inl hu =>
    have hs : s < n := Nat.lt_trans hsu hu
    have hr : r < n := Nat.lt_trans hrs hs
    unfold q4 d4 d3; dsimp only
    (try rw [hz (n + t + 1) 0 (Nat.succ_le_succ (Nat.le_add_right n t)) (Nat.succ_pos n)])
    (try rw [hz (n + t + 1) (r + 1) (Nat.succ_le_succ (Nat.le_add_right n t)) (Nat.succ_lt_succ hr)])
    (try rw [hz (n + t + 1) (s + 1) (Nat.succ_le_succ (Nat.le_add_right n t)) (Nat.succ_lt_succ hs)])
    (try rw [hz (n + t + 1) (u + 1) (Nat.succ_le_succ (Nat.le_add_right n t)) (Nat.succ_lt_succ hu)])
    lib_ring
  | inr hu =>
    obtain ⟨u', rfl⟩ := LibNatSub.le_ex hu
    cases Nat.lt_or_ge s n with
    | inl hs =>
      have hr : r < n := Nat.lt_trans hrs hs
      unfold q4 d4 d3; dsimp only
      (try rw [hz (n + t + 1) 0 (Nat.succ_le_succ (Nat.le_add_right n t)) (Nat.succ_pos n)])
      (try rw [hz (n + t + 1) (r + 1) (Nat.succ_le_succ (Nat.le_add_right n t)) (Nat.succ_lt_succ hr)])
      (try rw [hz (n + t + 1) (s + 1) (Nat.succ_le_succ (Nat.le_add_right n t)) (Nat.succ_lt_succ hs)])
      (try rw [hz (n + u' + 1) 0 (Nat.succ_le_succ (Nat.le_add_right n u')) (Nat.succ_pos n)])
      (try rw [hz (n + u' + 1) (r + 1) (Nat.succ_le_succ (Nat.le_add_right n u')) (Nat.succ_lt_succ hr)])
      (try rw [hz (n + u' + 1) (s + 1) (Nat.succ_le_succ (Nat.le_add_right n u')) (Nat.succ_lt_succ hs)])
      lib_ring
    | inr hs =>
      obtain ⟨s', rfl⟩ := LibNatSub.le_ex hs
      cases Nat.lt_or_ge r n with
      | inl hr =>
        unfold q4 d4 d3; dsimp only
        (try rw [hz (n + t + 1) 0 (Nat.succ_le_succ (Nat.le_add_right n t)) (Nat.succ_pos n)])
        (try rw [hz (n + t + 1) (r + 1) (Nat.succ_le_succ (Nat.le_add_right n t)) (Nat.succ_lt_succ hr)])
        (try rw [hz (n + s' + 1) 0 (Nat.succ_le_succ (Nat.le_add_right n s')) (Nat.succ_pos n)])
        (try rw [hz (n + s' + 1) (r + 1) (Nat.succ_le_succ (Nat.le_add_right n s')) (Nat.succ_lt_succ hr)])
        (try rw [hz (n + u' + 1) 0 (Nat.succ_le_succ (Nat.le_add_right n u')) (Nat.succ_pos n)])
        (try rw [hz (n + u' + 1) (r + 1) (Nat.succ_le_succ (Nat.le_add_right n u')) (Nat.succ_lt_succ hr)])
        lib_ring
      | inr hr =>
        obtain ⟨r', rfl⟩ := LibNatSub.le_ex hr
        unfold q4 d4 d3; dsimp only
        (try rw [hz (n + t + 1) 0 (Nat.succ_le_succ (Nat.le_add_right n t)) (Nat.succ_pos n)])
        (try rw [hz (n + r' + 1) 0 (Nat.succ_le_succ (Nat.le_add_right n r')) (Nat.succ_pos n)])
        (try rw [hz (n + s' + 1) 0 (Nat.succ_le_succ (Nat.le_add_right n s')) (Nat.succ_pos n)])
        (try rw [hz (n + u' + 1) 0 (Nat.succ_le_succ (Nat.le_add_right n u')) (Nat.succ_pos n)])
        lib_ring

end LibTriE5

#print axioms LibTriE5.tq4_mid1
#print axioms LibTriE5.tq4_mid2
#print axioms LibTriE5.tq4_hi3
#print axioms LibTriE5.tq4_low

lib/TriE5Main.lean

lean_all/lib/TriE5Main.lean

/-
  lib/TriE5Main.lean — T46 shared library, cycle 658 (G467b): Q4 splitting for block upper triangular matrices.

    Q4_tri  t < n: Q4(M, t) = Q4(A, t) + trC·Q3(A, t) + e₂C·(…) + M_{t+1,0} e₃C
  Lean 4 core only, no axioms.
-/
import lib.TriE5

namespace LibTriE5Main
open DetChain LibRing LibSum LibSemi LibMat LibLegSum LibEulerSum LibE4Rec LibPermRows LibPermRows4 LibE5Rec LibE4Dsum LibE2Minors LibE3Minors LibD3Rows LibE5Dsum LibTriE2

variable {R : Type} (K : CRing R) (n : Nat) (M : Nat → Nat → R) (hz : ∀ i j, n + 1 ≤ i → j < n + 1 → M i j = K.zero)

include hz in
theorem Q4_tri (m t : Nat) (ht : t < n) :
    Q4 K M (n + m) t
      = K.add (K.add (K.add (Q4 K M n t) (K.mul (LibMat.trace (toSemi K) m (shf (n + 1) M)) (Q3 K M n t)))
          (K.mul (LibCharQuad.e2 K m (shf (n + 1) M)) (K.add (K.mul (M (t + 1) 0) (LibMat.trace (toSemi K) n (LibCharQuad.sh M)))
            (K.neg (sum (toSemi K) (fun r => K.mul (M (t + 1) (r + 1)) (M (r + 1) 0)) (rng n))))))
          (K.mul (M (t + 1) 0) (LibE3Rec.e3 K m (shf (n + 1) M))) := by
  have htrC : LibMat.trace (toSemi K) m (shf (n + 1) M) = sum (toSemi K) (fun s => M (n + s + 1) (n + s + 1)) (rng m) :=
    sum_ext (toSemi K) _ _ (fun s => by show M (n + 1 + s) (n + 1 + s) = _; rw [Nat.succ_add]) _
  unfold Q4
  rw [LibDsumPow.split_nm]
  -- the r < n part
  have zmem : ∀ (l : List Nat) (f : Nat → R), (∀ x, x ∈ l → f x = K.zero) → sum (toSemi K) f l = K.zero :=
    fun l f h => (sum_ext_mem (toSemi K) _ (fun _ => K.zero) l h).trans (sum_zero (toSemi K) _)
  have hlo : sum (toSemi K) (fun r => sum (toSemi K) (fun s => sum (toSemi K) (fun u => K.mul (I K r s) (K.mul (I K s u)
        (q4 K M t r s u))) (rng (n + m))) (rng (n + m))) (rng n)
      = K.add (K.add (sum (toSemi K) (fun r => sum (toSemi K) (fun s => sum (toSemi K) (fun u => K.mul (I K r s) (K.mul (I K s u) (q4 K M t r s u))) (rng n)) (rng n)) (rng n))
          (K.mul (LibMat.trace (toSemi K) m (shf (n + 1) M)) (Q3 K M n t)))
          (K.mul (LibCharQuad.e2 K m (shf (n + 1) M)) (K.add (K.mul (M (t + 1) 0) (LibMat.trace (toSemi K) n (LibCharQuad.sh M)))
            (K.neg (sum (toSemi K) (fun r => K.mul (M (t + 1) (r + 1)) (M (r + 1) 0)) (rng n))))) := by
    -- pointwise: three pieces at each r
    refine Eq.trans (sum_ext_mem (toSemi K) _ (fun r => K.add (K.add
        (sum (toSemi K) (fun s => sum (toSemi K) (fun u => K.mul (I K r s) (K.mul (I K s u) (q4 K M t r s u))) (rng n)) (rng n))
        (K.mul (LibMat.trace (toSemi K) m (shf (n + 1) M)) (sum (toSemi K) (fun s => K.mul (indK (toSemi K) (decide (r < s))) (q K M t r s)) (rng n))))
        (K.mul (LibCharQuad.e2 K m (shf (n + 1) M)) (K.add (K.mul (M (t + 1) 0) (M (r + 1) (r + 1))) (K.neg (K.mul (M (t + 1) (r + 1)) (M (r + 1) 0)))))) _ (fun r hr => by
      have hr' := LibBinom.mem_rng hr
      rw [LibDsumPow.split_nm]
      -- s < n
      have hs1 : sum (toSemi K) (fun s => sum (toSemi K) (fun u => K.mul (I K r s) (K.mul (I K s u) (q4 K M t r s u))) (rng (n + m))) (rng n)
          = K.add (sum (toSemi K) (fun s => sum (toSemi K) (fun u => K.mul (I K r s) (K.mul (I K s u) (q4 K M t r s u))) (rng n)) (rng n))
              (K.mul (LibMat.trace (toSemi K) m (shf (n + 1) M)) (sum (toSemi K) (fun s => K.mul (indK (toSemi K) (decide (r < s))) (q K M t r s)) (rng n))) := by
        refine Eq.trans (sum_ext_mem (toSemi K) _ (fun s => K.add (sum (toSemi K) (fun u => K.mul (I K r s) (K.mul (I K s u) (q4 K M t r s u))) (rng n))
            (K.mul (LibMat.trace (toSemi K) m (shf (n + 1) M)) (K.mul (indK (toSemi K) (decide (r < s))) (q K M t r s)))) _ (fun s hs => by
          have hs' := LibBinom.mem_rng hs
          rw [LibDsumPow.split_nm]
          refine congr (congrArg K.add (sum_ext_mem (toSemi K) _ _ _ (fun u hu => by rfl))) ?_
          refine Eq.trans (sum_ext (toSemi K) _ (fun u => K.mul (M (n + u + 1) (n + u + 1)) (K.mul (indK (toSemi K) (decide (r < s))) (q K M t r s))) (fun u => by
            rw [I_mixed K n s u hs', LibTriE5.tq4_mid1 K n M hz t r s u hr' hs']
            show K.mul (indK (toSemi K) (decide (r < s))) _ = _
            generalize indK (toSemi K) (decide (r < s)) = d; generalize q K M t r s = x; generalize M (n + u + 1) (n + u + 1) = y; show K.mul d (K.mul K.one (K.mul y x)) = _; lib_ring) _) ?_
          exact (LibSum.sum_mul (toSemi K) _ (fun u => M (n + u + 1) (n + u + 1)) _).symm.trans (congrArg (fun z => K.mul z _) htrC.symm))) ?_
        refine (LibSum.sum_add (toSemi K) _ _ _).trans (congr (congrArg K.add rfl) (mul_sum (toSemi K) _ _ _).symm)
      -- s = n + s'
      have hs2 : sum (toSemi K) (fun s => sum (toSemi K) (fun u => K.mul (I K r (n + s)) (K.mul (I K (n + s) u) (q4 K M t r (n + s) u))) (rng (n + m))) (rng m)
          = K.mul (LibCharQuad.e2 K m (shf (n + 1) M)) (K.add (K.mul (M (t + 1) 0) (M (r + 1) (r + 1))) (K.neg (K.mul (M (t + 1) (r + 1)) (M (r + 1) 0)))) := by
        rw [LibE2Minors.e2_minors K m (shf (n + 1) M)]; unfold S2m
        refine Eq.trans ?_ (LibSum.sum_mul (toSemi K) _ _ _).symm
        apply sum_ext (toSemi K); intro s
        rw [LibDsumPow.split_nm, zmem _ _ (fun u hu => by
          rw [I_back K n s u (LibBinom.mem_rng hu)]; generalize I K r (n + s) = d; generalize q4 K _ t r (n + s) u = q'; show K.mul d (K.mul K.zero q') = K.zero; lib_ring)]
        refine Eq.trans (K.zero_add _) ?_
        refine Eq.trans ?_ (LibSum.sum_mul (toSemi K) _ _ _).symm
        apply sum_ext (toSemi K); intro u
        rw [I_mixed K n r s hr', I_hh, LibTriE5.tq4_mid2 K n M hz t r s u hr']
        unfold I
        generalize indK (toSemi K) (decide (s < u)) = d; generalize m2 K (shf (n + 1) M) s u = x
        generalize K.add (K.mul (M (t + 1) 0) (M (r + 1) (r + 1))) (K.neg (K.mul (M (t + 1) (r + 1)) (M (r + 1) 0))) = y
        show K.mul K.one (K.mul d (K.mul x y)) = K.mul (K.mul d x) y; lib_ring
      rw [hs1, hs2])) ?_
    refine (LibSum.sum_add (toSemi K) _ _ _).trans ?_
    refine congr (congrArg K.add ((LibSum.sum_add (toSemi K) _ _ _).trans (congr (congrArg K.add rfl) (mul_sum (toSemi K) _ _ _).symm))) ?_
    refine Eq.trans (mul_sum (toSemi K) _ _ _).symm (congrArg (K.mul _) ?_)
    refine (LibSum.sum_add (toSemi K) _ _ _).trans ?_
    rw [LibSum.sum_neg K]
    exact congr (congrArg K.add (mul_sum (toSemi K) _ _ _).symm) rfl
  have hhi : sum (toSemi K) (fun r => sum (toSemi K) (fun s => sum (toSemi K) (fun u => K.mul (I K (n + r) s) (K.mul (I K s u)
        (q4 K M t (n + r) s u))) (rng (n + m))) (rng (n + m))) (rng m)
      = K.mul (M (t + 1) 0) (LibE3Rec.e3 K m (shf (n + 1) M)) := by
    rw [LibE3Minors.e3_minors K m (shf (n + 1) M)]; unfold S3m
    refine Eq.trans ?_ (mul_sum (toSemi K) _ _ _).symm
    apply sum_ext (toSemi K); intro r
    rw [LibDsumPow.split_nm, zmem _ _ (fun s hs => by
      apply zmem; intro u _
      rw [I_back K n r s (LibBinom.mem_rng hs)]; exact DetChain.zero_mul' K _)]
    refine Eq.trans (K.zero_add _) ?_
    refine Eq.trans ?_ (mul_sum (toSemi K) _ _ _).symm
    apply sum_ext (toSemi K); intro s
    rw [LibDsumPow.split_nm, zmem _ _ (fun u hu => by
      rw [I_back K n s u (LibBinom.mem_rng hu)]; generalize I K (n + r) (n + s) = d; generalize q4 K _ t (n + r) (n + s) u = q'; show K.mul d (K.mul K.zero q') = K.zero; lib_ring)]
    refine Eq.trans (K.zero_add _) ?_
    refine Eq.trans ?_ (mul_sum (toSemi K) _ _ _).symm
    apply sum_ext (toSemi K); intro u
    rw [I_hh, I_hh, LibTriE5.tq4_hi3 K n M hz t r s u]
    generalize I K r s = d1; generalize I K s u = d2; generalize m3 K (shf (n + 1) M) r s u = x; generalize M (t + 1) 0 = a
    show _ = K.mul a (K.mul d1 (K.mul d2 x)); lib_ring
  rw [hlo, hhi]

include hz in
theorem Q4_low (m t : Nat) : Q4 K M (n + m) (n + t) = K.zero := by
  unfold Q4
  refine Eq.trans (sum_ext (toSemi K) _ (fun _ => K.zero) (fun r => ?_) _) (sum_zero (toSemi K) _)
  refine Eq.trans (sum_ext (toSemi K) _ (fun _ => K.zero) (fun s => ?_) _) (sum_zero (toSemi K) _)
  refine Eq.trans (sum_ext (toSemi K) _ (fun _ => K.zero) (fun u => ?_) _) (sum_zero (toSemi K) _)
  cases Nat.lt_or_ge r s with
  | inr h => rw [LibE5Rec.I_ge K r s h]; exact DetChain.zero_mul' K _
  | inl hrs =>
    cases Nat.lt_or_ge s u with
    | inr h => rw [LibE5Rec.I_ge K s u h]; show K.mul _ (K.mul K.zero _) = K.zero; generalize I K r s = a; generalize q4 K M (n + t) r s u = q'; lib_ring
    | inl hsu => rw [LibTriE5.tq4_low K n M hz t r s u hrs hsu]; generalize I K r s = a; generalize I K s u = b; show K.mul a (K.mul b K.zero) = K.zero; lib_ring

/-- **e5(block upper triangular) = Σ_{a+b=5} e_a(A) e_b(C).** -/
theorem tri_e5 (m : Nat) : ∀ (n : Nat) (M : Nat → Nat → R), (∀ i j, n ≤ i → j < n → M i j = K.zero) →
    e5 K (n + m) M
      = K.add (K.add (K.add (K.add (K.add (e5 K n M) (K.mul (e4 K n M) (trace (toSemi K) m (shf n M)))) (K.mul (LibE3Rec.e3 K n M) (LibCharQuad.e2 K m (shf n M))))
          (K.mul (LibCharQuad.e2 K n M) (LibE3Rec.e3 K m (shf n M)))) (K.mul (trace (toSemi K) n M) (e4 K m (shf n M)))) (e5 K m (shf n M))
  | 0, M, _ => by
    rw [Nat.zero_add]
    refine Eq.trans (LibE5DsumMain.e5_ext K m _ (shf 0 M) (fun i j _ _ => by show M i j = M (0 + i) (0 + j); rw [Nat.zero_add, Nat.zero_add])) ?_
    show _ = K.add (K.add (K.add (K.add (K.add K.zero (K.mul K.zero _)) (K.mul K.zero _)) (K.mul K.zero _)) (K.mul K.zero _)) _
    lib_ring
  | n + 1, M, hz => by
    rw [Nat.succ_add]
    show K.add (K.add (e5 K (n + m) (LibCharQuad.sh M)) (K.mul (M 0 0) (e4 K (n + m) (LibCharQuad.sh M))))
        (K.neg (sum (toSemi K) (fun t => K.mul (M 0 (t + 1)) (Q4 K M (n + m) t)) (rng (n + m))))
      = K.add (K.add (K.add (K.add (K.add (K.add (K.add (e5 K n (LibCharQuad.sh M)) (K.mul (M 0 0) (e4 K n (LibCharQuad.sh M))))
            (K.neg (sum (toSemi K) (fun t => K.mul (M 0 (t + 1)) (Q4 K M n t)) (rng n))))
          (K.mul (K.add (K.add (e4 K n (LibCharQuad.sh M)) (K.mul (M 0 0) (LibE3Rec.e3 K n (LibCharQuad.sh M))))
            (K.neg (sum (toSemi K) (fun t => K.mul (M 0 (t + 1)) (Q3 K M n t)) (rng n)))) (trace (toSemi K) m (shf (n + 1) M))))
          (K.mul (K.add (K.add (LibE3Rec.e3 K n (LibCharQuad.sh M)) (K.mul (M 0 0) (LibCharQuad.e2 K n (LibCharQuad.sh M))))
            (K.neg (sum (toSemi K) (fun t => K.mul (M 0 (t + 1))
              (K.add (K.mul (M (t + 1) 0) (trace (toSemi K) n (LibCharQuad.sh M)))
                (K.neg (sum (toSemi K) (fun r => K.mul (M (t + 1) (r + 1)) (M (r + 1) 0)) (rng n))))) (rng n)))) (LibCharQuad.e2 K m (shf (n + 1) M))))
          (K.mul (K.add (K.add (LibCharQuad.e2 K n (LibCharQuad.sh M)) (K.mul (M 0 0) (trace (toSemi K) n (LibCharQuad.sh M))))
            (K.neg (sum (toSemi K) (fun t => K.mul (M 0 (t + 1)) (M (t + 1) 0)) (rng n)))) (LibE3Rec.e3 K m (shf (n + 1) M))))
          (K.mul (trace (toSemi K) (n + 1) M) (e4 K m (shf (n + 1) M)))) (e5 K m (shf (n + 1) M))
    have hz' : ∀ i j, n ≤ i → j < n → LibCharQuad.sh M i j = K.zero := fun i j hi hj => hz _ _ (Nat.succ_le_succ hi) (Nat.succ_lt_succ hj)
    have hs : ∀ i j, i < m → j < m → shf n (LibCharQuad.sh M) i j = shf (n + 1) M i j := fun i j _ _ => by
      show M (n + i + 1) (n + j + 1) = M (n + 1 + i) (n + 1 + j); rw [Nat.succ_add, Nat.succ_add]
    rw [tri_e5 m n (LibCharQuad.sh M) hz', LibTriE4.tri_e4 K m n (LibCharQuad.sh M) hz', LibE5DsumMain.e5_ext K m _ _ hs, e4_ext K m _ _ hs,
      LibE3Dsum.e3_ext K m _ _ hs, LibE2Dsum.e2_ext K m _ _ hs, LibE2Dsum.tr_ext K m _ _ hs,
      LibDsumPow.split_nm, show trace (toSemi K) (n + 1) M = K.add (M 0 0) (trace (toSemi K) n (LibCharQuad.sh M)) from LibDetKernel.sum_split0 K n _]
    have hz0 : sum (toSemi K) (fun t => K.mul (M 0 (n + t + 1)) (Q4 K M (n + m) (n + t))) (rng m) = K.zero := by
      refine Eq.trans (sum_ext (toSemi K) _ (fun _ => K.zero) (fun t => by
        rw [Q4_low K n M hz m t]; show K.mul _ K.zero = K.zero; generalize M 0 (n + t + 1) = u; lib_ring) _) ?_
      exact sum_zero (toSemi K) _
    rw [hz0]
    have hlo : sum (toSemi K) (fun t => K.mul (M 0 (t + 1)) (Q4 K M (n + m) t)) (rng n)
        = K.add (K.add (K.add (sum (toSemi K) (fun t => K.mul (M 0 (t + 1)) (Q4 K M n t)) (rng n))
            (K.mul (trace (toSemi K) m (shf (n + 1) M)) (sum (toSemi K) (fun t => K.mul (M 0 (t + 1)) (Q3 K M n t)) (rng n))))
            (K.mul (LibCharQuad.e2 K m (shf (n + 1) M)) (sum (toSemi K) (fun t => K.mul (M 0 (t + 1))
              (K.add (K.mul (M (t + 1) 0) (trace (toSemi K) n (LibCharQuad.sh M)))
                (K.neg (sum (toSemi K) (fun r => K.mul (M (t + 1) (r + 1)) (M (r + 1) 0)) (rng n))))) (rng n))))
            (K.mul (LibE3Rec.e3 K m (shf (n + 1) M)) (sum (toSemi K) (fun t => K.mul (M 0 (t + 1)) (M (t + 1) 0)) (rng n))) := by
      refine Eq.trans (sum_ext_mem (toSemi K) _ (fun t => K.add (K.add (K.add (K.mul (M 0 (t + 1)) (Q4 K M n t))
          (K.mul (trace (toSemi K) m (shf (n + 1) M)) (K.mul (M 0 (t + 1)) (Q3 K M n t))))
          (K.mul (LibCharQuad.e2 K m (shf (n + 1) M)) (K.mul (M 0 (t + 1)) (K.add (K.mul (M (t + 1) 0) (trace (toSemi K) n (LibCharQuad.sh M)))
                (K.neg (sum (toSemi K) (fun r => K.mul (M (t + 1) (r + 1)) (M (r + 1) 0)) (rng n)))))))
          (K.mul (LibE3Rec.e3 K m (shf (n + 1) M)) (K.mul (M 0 (t + 1)) (M (t + 1) 0)))) _ (fun t ht => by
        have ht' := LibBinom.mem_rng ht
        rw [Q4_tri K n M hz m t ht']
        generalize Q4 K M n t = Qv; generalize Q3 K M n t = Pv; generalize trace (toSemi K) m (shf (n + 1) M) = b
        generalize LibCharQuad.e2 K m (shf (n + 1) M) = f; generalize LibE3Rec.e3 K m (shf (n + 1) M) = g
        generalize K.add (K.mul (M (t + 1) 0) (trace (toSemi K) n (LibCharQuad.sh M))) (K.neg (sum (toSemi K) (fun r => K.mul (M (t + 1) (r + 1)) (M (r + 1) 0)) (rng n))) = Yv
        generalize M 0 (t + 1) = bb; generalize M (t + 1) 0 = cc
        lib_ring)) ?_
      refine (LibSum.sum_add (toSemi K) _ _ _).trans ?_
      refine congr (congrArg K.add ((LibSum.sum_add (toSemi K) _ _ _).trans (congr (congrArg K.add
        ((LibSum.sum_add (toSemi K) _ _ _).trans (congr (congrArg K.add rfl) (mul_sum (toSemi K) _ _ _).symm))) (mul_sum (toSemi K) _ _ _).symm))) ?_
      exact (mul_sum (toSemi K) _ _ _).symm
    rw [hlo]
    generalize sum (toSemi K) (fun t => K.mul (M 0 (t + 1)) (Q4 K M n t)) (rng n) = S4'
    generalize sum (toSemi K) (fun t => K.mul (M 0 (t + 1)) (Q3 K M n t)) (rng n) = S3
    generalize sum (toSemi K) (fun t => K.mul (M 0 (t + 1))
              (K.add (K.mul (M (t + 1) 0) (trace (toSemi K) n (LibCharQuad.sh M)))
                (K.neg (sum (toSemi K) (fun r => K.mul (M (t + 1) (r + 1)) (M (r + 1) 0)) (rng n))))) (rng n) = Y
    generalize sum (toSemi K) (fun t => K.mul (M 0 (t + 1)) (M (t + 1) 0)) (rng n) = s
    generalize e5 K n (LibCharQuad.sh M) = E5; generalize e4 K n (LibCharQuad.sh M) = E4; generalize LibE3Rec.e3 K n (LibCharQuad.sh M) = E3
    generalize LibCharQuad.e2 K n (LibCharQuad.sh M) = E2; generalize trace (toSemi K) n (LibCharQuad.sh M) = T
    generalize trace (toSemi K) m (shf (n + 1) M) = b; generalize LibCharQuad.e2 K m (shf (n + 1) M) = f; generalize LibE3Rec.e3 K m (shf (n + 1) M) = g
    generalize e4 K m (shf (n + 1) M) = hh; generalize e5 K m (shf (n + 1) M) = kk
    generalize M 0 0 = a
    show K.add (K.add _ _) (K.neg (K.add (K.add (K.add (K.add S4' (K.mul b S3)) (K.mul f Y)) (K.mul g s)) K.zero)) = _
    lib_ring

end LibTriE5Main

#print axioms LibTriE5Main.Q4_tri
#print axioms LibTriE5Main.tri_e5

lib/TriEuler.lean

lean_all/lib/TriEuler.lean

/-
  lib/TriEuler.lean — T46 shared library, cycle 517 (G183, G185): local factors of upper-triangular matrices.

  Any commutative ring, T n×n, T_{ij} = 0 (j < i < n):
    prodN_last   Π_{i<N+1} f i = (Π_{i<N} f i) · f N
    tri_euler    det(I − xT) = Π_{i<n}(1 − T_{ii} x)
    mpow_one     power of a 1×1 matrix = power of its entry
    tri_psum     tr T^k = Σ_{i<n} T_{ii}^k                                  (G185, BlockPow induction)
  (Induction via BlockEuler with blocks n + 1 = n ⊕ 1. Borel form with Satake parameters on the diagonal — local factors without semisimplification.)
  Lean 4 core only, no axioms.
-/
import lib.BlockPow

namespace LibTriEuler
open DetChain LibRing LibSum LibSemi LibMat LibLegSum LibDet LibCircDet LibEulerSum

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

theorem prodN_last : ∀ (N : Nat) (f : Nat → R), prodN K f (N + 1) = K.mul (prodN K f N) (f N)
  | 0, f => by show K.mul (f 0) K.one = K.mul K.one (f 0); lib_ring
  | N + 1, f => by
    show K.mul (f 0) (prodN K (fun k => f (k + 1)) (N + 1)) = K.mul (K.mul (f 0) (prodN K (fun k => f (k + 1)) N)) (f (N + 1))
    rw [prodN_last N (fun k => f (k + 1))]; lib_ring

/-- **det(I − xT) = Π(1 − T_ii x)** (upper triangular). -/
theorem tri_euler (T : Nat → Nat → R) (x : R) : ∀ n, (∀ i j, j < i → i < n → T i j = K.zero) →
    det K n (imx K T x) = prodN K (fun i => K.add K.one (K.neg (K.mul (T i i) x))) n
  | 0, _ => rfl
  | n + 1, hT => by
    rw [LibBlockEuler.block_euler K n 1 T x (fun i j hi hi' hj =>
        hT i j (Nat.lt_of_lt_of_le hj hi) hi'),
      tri_euler T x n (fun i j hji hi => hT i j hji (Nat.lt_succ_of_lt hi)), prodN_last, LibDet.det_one]
    show K.mul _ (K.add (indK (toSemi K) (decide (0 = 0))) (K.neg (K.mul x (T (n + 0) (n + 0))))) = K.mul _ (K.add K.one (K.neg (K.mul (T n n) x)))
    rw [decide_eq_true rfl]
    show K.mul _ (K.add K.one (K.neg (K.mul x (T n n)))) = _
    lib_ring

theorem mpow_one (B : Nat → Nat → R) : ∀ k, mpow (toSemi K) 1 B k 0 0 = pw K (B 0 0) k
  | 0 => rfl
  | k + 1 => by
    show K.add (K.mul (mpow (toSemi K) 1 B k 0 0) (B 0 0)) K.zero = K.mul (pw K (B 0 0) k) (B 0 0)
    rw [mpow_one B k]; lib_ring

/-- **tr T^k = Σ T_ii^k** (upper triangular). -/
theorem tri_psum (T : Nat → Nat → R) (k : Nat) : ∀ n, (∀ i j, j < i → i < n → T i j = K.zero) →
    trace (toSemi K) n (mpow (toSemi K) n T k) = sum (toSemi K) (fun i => pw K (T i i) k) (rng n)
  | 0, _ => rfl
  | n + 1, hT => by
    rw [LibBlockPow.tr_block_pow K n 1 T (fun s j hs hj => hT (n + s) j (Nat.lt_of_lt_of_le hj (Nat.le_add_right n s))
        (Nat.add_lt_add_left hs n)),
      tri_psum T k n (fun i j hji hi => hT i j hji (Nat.lt_succ_of_lt hi)), sum_rng_succ]
    show K.add _ (K.add (mpow (toSemi K) 1 (fun i j => T (n + i) (n + j)) k 0 0) K.zero) = _
    rw [mpow_one]
    show K.add (sum (toSemi K) (fun i => pw K (T i i) k) (rng n)) (K.add (pw K (T n n) k) K.zero)
      = K.add (sum (toSemi K) (fun i => pw K (T i i) k) (rng n)) (pw K (T n n) k)
    generalize sum (toSemi K) (fun i => pw K (T i i) k) (rng n) = S
    generalize pw K (T n n) k = a
    lib_ring

end LibTriEuler

#print axioms LibTriEuler.prodN_last
#print axioms LibTriEuler.tri_euler
#print axioms LibTriEuler.tri_psum

lib/TriGenRS.lean

lean_all/lib/TriGenRS.lean

/-
  lib/TriGenRS.lean — T46 shared library, cycle 543 (G223, G227): RS local factor of upper triangular × general matrix.

  Any commutative ring, T (n×n) upper triangular, U (m×m, m = m'+1) any matrix:
    dm_small      p < m ⇒ dm m p = (0, p)
    tri_gen_rs    L(T⊗U) = Π_{a<n} L(T_aa·U)                         (induction via KronBlock.kron_block_euler)
    tri_gen_rs2   T 2×2 upper triangular, U 2×2: L(T⊗U) = (1 − a·tr U x + a²det U x²)(1 − d·tr U x + d²det U x²)
  (If π is of Borel form, then for any σ, L(π×σ) = Π L(σ ⊗ α_a); no semisimplicity hypothesis on the right factor.)
  Lean 4 core only, no axioms.
-/
import lib.SymScale

namespace LibTriGenRS
open DetChain LibRing LibSum LibSemi LibMat LibLegSum LibDet LibCircDet LibEulerSum LibKron LibKronDetGen LibDivModOwn LibTriEuler

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

theorem dm_small (m' p : Nat) (hp : p < m' + 1) : dm (m' + 1) p = (0, p) := by
  have e := dm_spec m' 0 p hp
  rw [Nat.zero_mul, Nat.zero_add] at e; exact e

/-- **L(T⊗U) = Π_a L(T_aa·U)** (T upper triangular, U arbitrary). -/
theorem tri_gen_rs (m' : Nat) (T U : Nat → Nat → R) (x : R) : ∀ n, (∀ i j, j < i → i < n → T i j = K.zero) →
    det K (n * (m' + 1)) (imx K (fl (m' + 1) (kron K T U)) x)
      = prodN K (fun a => det K (m' + 1) (imx K (fun i j => K.mul (T a a) (U i j)) x)) n
  | 0, _ => by rw [Nat.zero_mul]; rfl
  | n + 1, hT => by
    rw [LibKronBlock.kron_block_euler K n 1 m' T U x (fun i j hi hi' hj => hT i j (Nat.lt_of_lt_of_le hj hi) hi'),
      tri_gen_rs m' T U x n (fun i j hji hi => hT i j hji (Nat.lt_succ_of_lt hi)), prodN_last, Nat.one_mul]
    refine congrArg (K.mul _) ?_
    exact det_ext K (m' + 1) _ _ (fun p q hp hq => by
      show K.add (idM K p q) (K.neg (K.mul x (K.mul (T (n + (dm (m' + 1) p).1) (n + (dm (m' + 1) q).1)) (U (dm (m' + 1) p).2 (dm (m' + 1) q).2))))
        = K.add (idM K p q) (K.neg (K.mul x (K.mul (T n n) (U p q))))
      rw [dm_small m' p hp, dm_small m' q hq]; rfl)

/-- **T 2×2 upper triangular, U 2×2: L(T⊗U) = (1 − a tr U x + a² det U x²)(1 − d tr U x + d² det U x²).** -/
theorem tri_gen_rs2 (T U : Nat → Nat → R) (h10 : T 1 0 = K.zero) (x : R) :
    det K (2 * 2) (imx K (fl 2 (kron K T U)) x)
      = K.mul (K.add (K.add K.one (K.neg (K.mul (K.mul (T 0 0) (K.add (U 0 0) (U 1 1))) x)))
            (K.mul (K.mul (K.mul (T 0 0) (T 0 0)) (K.add (K.mul (U 0 0) (U 1 1)) (K.neg (K.mul (U 0 1) (U 1 0))))) (K.mul x x)))
          (K.add (K.add K.one (K.neg (K.mul (K.mul (T 1 1) (K.add (U 0 0) (U 1 1))) x)))
            (K.mul (K.mul (K.mul (T 1 1) (T 1 1)) (K.add (K.mul (U 0 0) (U 1 1)) (K.neg (K.mul (U 0 1) (U 1 0))))) (K.mul x x))) := by
  rw [tri_gen_rs K 1 T U x 2 (LibTriUnip.tri2 K T h10)]
  show K.mul (det K 2 (imx K (fun i j => K.mul (T 0 0) (U i j)) x))
    (K.mul (det K 2 (imx K (fun i j => K.mul (T 1 1) (U i j)) x)) K.one) = _
  rw [LibGL2Inv.euler2, LibGL2Inv.euler2, LibGL2Inv.tr2, LibGL2Inv.tr2, LibDet.det_two, LibDet.det_two]
  unfold det2; lib_ring

end LibTriGenRS

#print axioms LibTriGenRS.dm_small
#print axioms LibTriGenRS.tri_gen_rs
#print axioms LibTriGenRS.tri_gen_rs2

lib/TriKronDet.lean

lean_all/lib/TriKronDet.lean

/-
  lib/TriKronDet.lean — T46 shared library, cycle 535 (G210, G211): determinant of upper triangular tensors, upper triangular Sym² coefficients.

  Any commutative ring:
    prodN_const      Π_{i<m} c = c^m
    prodN_pw         Π_a (f a)^m = (Π_a f a)^m
    kron_tri_det     T (n×n), U (m×m) upper triangular: det(fl(T⊗U)) = (det T)^m (det U)^n
    sym2_tri_c1      A₁₀ = 0: x coefficient of L(Sym²A) = −(a² + ad + d²)
    sym2_tri_c3      A₁₀ = 0: x³ coefficient of L(Sym²A) = (−a²)(−ad)(−d²)
  Lean 4 core only, no axioms.
-/
import lib.TriUnip

namespace LibTriKronDet
open DetChain LibRing LibSum LibSemi LibMat LibLegSum LibDet LibCircDet LibEulerSum LibKron LibKronDetGen LibDivModOwn
  LibTriEuler LibTriCoeff LibSym2

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

theorem prodN_const (c : R) : ∀ m, prodN K (fun _ => c) m = pw K c m
  | 0 => rfl
  | m + 1 => by rw [prodN_last, prodN_const c m]; rfl

theorem prodN_pw (f : Nat → R) (m : Nat) : ∀ n, prodN K (fun a => pw K (f a) m) n = pw K (prodN K f n) m
  | 0 => by
    show K.one = pw K K.one m
    exact (LibTriUnip.prodN_one K m).symm.trans (prodN_const K K.one m)
  | n + 1 => by
    rw [prodN_last, prodN_pw f m n, prodN_last K n f, LibCentralBinom2.pw_mul]

/-- **det(T⊗U) = (det T)^m (det U)^n** (upper triangular). -/
theorem kron_tri_det (n m' : Nat) (T U : Nat → Nat → R)
    (hT : ∀ i j, j < i → i < n → T i j = K.zero) (hU : ∀ i j, j < i → i < m' + 1 → U i j = K.zero) :
    det K (n * (m' + 1)) (fl (m' + 1) (kron K T U))
      = K.mul (pw K (det K n T) (m' + 1)) (pw K (det K (m' + 1) U) n) := by
  rw [LibTriDet.det_tri K _ _ (LibKronTri.fl_tri K n m' T U hT hU), LibKronTri.prodN_flat,
    LibTriDet.det_tri K T n hT, LibTriDet.det_tri K U (m' + 1) hU]
  have e : prodN K (fun a => prodN K (fun b => fl (m' + 1) (kron K T U) (a * (m' + 1) + b) (a * (m' + 1) + b)) (m' + 1)) n
      = prodN K (fun a => K.mul (pw K (T a a) (m' + 1)) (prodN K (fun b => U b b) (m' + 1))) n :=
    LibKronTri.prodN_ext K n _ _ (fun a _ => by
      refine Eq.trans (LibKronTri.prodN_ext K (m' + 1) _ (fun b => K.mul (T a a) (U b b)) (fun b hb => by
        show K.mul (T (dm (m' + 1) (a * (m' + 1) + b)).1 (dm (m' + 1) (a * (m' + 1) + b)).1)
          (U (dm (m' + 1) (a * (m' + 1) + b)).2 (dm (m' + 1) (a * (m' + 1) + b)).2) = _
        rw [dm_spec m' a b hb])) ?_
      rw [LibTriDet.prodN_mul, prodN_const])
  rw [e, LibTriDet.prodN_mul, prodN_pw, prodN_const]

variable (A : Nat → Nat → R) (h10 : A 1 0 = K.zero)

include h10 in
/-- **x coefficient of L(Sym²A) = −(a² + ad + d²).** -/
theorem sym2_tri_c1 :
    cf K (fun i => s2 K A i i) 3 1 = K.neg (K.add (K.add (K.mul (A 0 0) (A 0 0)) (K.mul (A 0 0) (A 1 1))) (K.mul (A 1 1) (A 1 1))) := by
  rw [LibBaseChangeTri.cf_one]
  show K.neg (K.add (K.add (K.add K.zero (K.mul K.one (K.mul (A 0 0) (A 0 0))))
      (K.mul K.one (K.add (K.mul (A 0 0) (A 1 1)) (K.mul (A 0 1) (A 1 0))))) (K.mul K.one (K.mul (A 1 1) (A 1 1)))) = _
  rw [h10]; lib_ring

include h10 in
/-- **x³ coefficient of L(Sym²A) = (−a²)(−ad)(−d²).** -/
theorem sym2_tri_c3 :
    cf K (fun i => s2 K A i i) 3 3
      = K.mul (K.mul (K.neg (K.mul (A 0 0) (A 0 0))) (K.neg (K.mul (A 0 0) (A 1 1)))) (K.neg (K.mul (A 1 1) (A 1 1))) := by
  rw [LibBaseChangeTri.cf_diag]
  show K.mul (K.neg (K.mul (A 0 0) (A 0 0))) (K.mul (K.neg (K.add (K.mul (A 0 0) (A 1 1)) (K.mul (A 0 1) (A 1 0))))
      (K.mul (K.neg (K.mul (A 1 1) (A 1 1))) K.one)) = _
  rw [h10]; lib_ring

end LibTriKronDet

#print axioms LibTriKronDet.kron_tri_det
#print axioms LibTriKronDet.sym2_tri_c1
#print axioms LibTriKronDet.sym2_tri_c3

lib/TriMul.lean

lean_all/lib/TriMul.lean

/-
  lib/TriMul.lean — T46 shared library, cycle 520 (G189): products of upper triangular matrices (closure of the Borel subgroup).

  Any commutative ring, T, U n×n upper triangular (T_{ij} = 0, j < i < n):
    tri_mul_lower   (TU)_{ij} = 0  (j < i < n)
    tri_mul_diag    (TU)_{ii} = T_ii U_ii  (i < n)
  (In each term of the sum, T_{il} = 0 if l < i and U_{lj} = 0 if l > j; the diagonal via LegHasse.sum_single.)
  Lean 4 core only, no axioms.
-/
import lib.TracePow4c

namespace LibTriMul
open DetChain LibRing LibSum LibSemi LibMat LibLegSum LibDet

variable {R : Type} (K : CRing R) (n : Nat) (T U : Nat → Nat → R)
  (hT : ∀ i j, j < i → i < n → T i j = K.zero) (hU : ∀ i j, j < i → i < n → U i j = K.zero)

include hT hU in
theorem tri_mul_lower (i j : Nat) (hji : j < i) (hi : i < n) : mmul (toSemi K) n T U i j = K.zero := by
  show sum (toSemi K) (fun l => K.mul (T i l) (U l j)) (rng n) = K.zero
  refine (sum_ext_mem (toSemi K) _ (fun _ => K.zero) (rng n) (fun l hl => ?_)).trans (sum_zero (toSemi K) _)
  cases Nat.decLt l i with
  | isTrue h => show K.mul (T i l) (U l j) = K.zero; rw [hT i l h hi]; lib_ring
  | isFalse h =>
    show K.mul (T i l) (U l j) = K.zero
    rw [hU l j (Nat.lt_of_lt_of_le hji (Nat.le_of_not_lt h)) (LibBinom.mem_rng hl)]; lib_ring

include hT hU in
theorem tri_mul_diag (i : Nat) (hi : i < n) : mmul (toSemi K) n T U i i = K.mul (T i i) (U i i) := by
  show sum (toSemi K) (fun l => K.mul (T i l) (U l i)) (rng n) = _
  refine LibLegHasse.sum_single K (fun l => K.mul (T i l) (U l i)) i n hi (fun l hl hne => ?_)
  cases Nat.decLt l i with
  | isTrue h => show K.mul (T i l) (U l i) = K.zero; rw [hT i l h hi]; lib_ring
  | isFalse h =>
    show K.mul (T i l) (U l i) = K.zero
    rw [hU l i (Nat.lt_of_le_of_ne (Nat.le_of_not_lt h) (fun e => hne e.symm)) hl]; lib_ring

end LibTriMul

#print axioms LibTriMul.tri_mul_lower
#print axioms LibTriMul.tri_mul_diag

lib/TriPow.lean

lean_all/lib/TriPow.lean

/-
  lib/TriPow.lean — T46 shared library, cycle 528 (G201, G202): powers of upper triangular matrices, power traces of upper triangular Sym².

  Any commutative ring:
    tri_pow        T upper triangular ⇒ T^k upper triangular, (T^k)_ii = T_ii^k                 (TriMul induction)
    sym2_tri_psum  A₁₀ = 0 ⇒ tr(Sym²A)^k = a^{2k}…, i.e. (a²)^k + (ad)^k + (d²)^k   (Sym2Tri + TriEuler.tri_psum)
  Lean 4 core only, no axioms.
-/
import lib.NewtonTri4

namespace LibTriPow
open DetChain LibRing LibSum LibSemi LibMat LibLegSum LibDet LibEulerSum LibSym2

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

/-- **T^k is upper triangular, diagonal = T_ii^k.** -/
theorem tri_pow (n : Nat) (T : Nat → Nat → R) (hT : ∀ i j, j < i → i < n → T i j = K.zero) : ∀ k,
    (∀ i j, j < i → i < n → mpow (toSemi K) n T k i j = K.zero)
    ∧ (∀ i, i < n → mpow (toSemi K) n T k i i = pw K (T i i) k)
  | 0 => by
    refine ⟨fun i j hji _ => ?_, fun i _ => ?_⟩
    · show indK (toSemi K) (decide (i = j)) = K.zero
      rw [decide_eq_false (fun (e : i = j) => by subst e; exact Nat.lt_irrefl _ hji)]; rfl
    · show indK (toSemi K) (decide (i = i)) = K.one
      rw [decide_eq_true rfl]; rfl
  | k + 1 => by
    have ⟨h1, h2⟩ := tri_pow n T hT k
    refine ⟨fun i j hji hi => LibTriMul.tri_mul_lower K n _ T h1 hT i j hji hi, fun i hi => ?_⟩
    show mmul (toSemi K) n (mpow (toSemi K) n T k) T i i = K.mul (pw K (T i i) k) (T i i)
    rw [LibTriMul.tri_mul_diag K n _ T h1 hT i hi, h2 i hi]

/-- **tr(Sym²A)^k = (a²)^k + (ad)^k + (d²)^k** (A₁₀ = 0). -/
theorem sym2_tri_psum (A : Nat → Nat → R) (h10 : A 1 0 = K.zero) (k : Nat) :
    trace (toSemi K) 3 (mpow (toSemi K) 3 (s2 K A) k)
      = K.add (K.add (pw K (K.mul (A 0 0) (A 0 0)) k) (pw K (K.mul (A 0 0) (A 1 1)) k)) (pw K (K.mul (A 1 1) (A 1 1)) k) := by
  rw [LibTriEuler.tri_psum K (s2 K A) k 3 (LibSym2Tri.s2_tri K A h10)]
  show K.add (pw K (K.mul (A 0 0) (A 0 0)) k) (K.add (pw K (K.add (K.mul (A 0 0) (A 1 1)) (K.mul (A 0 1) (A 1 0))) k)
      (K.add (pw K (K.mul (A 1 1) (A 1 1)) k) K.zero)) = _
  rw [h10, show K.add (K.mul (A 0 0) (A 1 1)) (K.mul (A 0 1) K.zero) = K.mul (A 0 0) (A 1 1) by lib_ring]
  generalize pw K (K.mul (A 0 0) (A 0 0)) k = p
  generalize pw K (K.mul (A 0 0) (A 1 1)) k = q
  generalize pw K (K.mul (A 1 1) (A 1 1)) k = r
  lib_ring

end LibTriPow

#print axioms LibTriPow.tri_pow
#print axioms LibTriPow.sym2_tri_psum

lib/TriSymRS.lean

lean_all/lib/TriSymRS.lean

/-
  lib/TriSymRS.lean — T46 shared library, cycle 546 (G234, G235, G237): Sym³ × general, trace of upper triangular × general, Sym² × Sym².

  any commutative ring:
    sym3_gen_rs    A 2×2 upper triangular, U arbitrary: L(Sym³A ⊗ U) = L(a³U) L(a²dU) L(ad²U) L(d³U)
    tri_gen_psum   T upper triangular, U arbitrary: tr(fl(T⊗U))^k = (Σ T_aa^k) · tr U^k
    sym2_sym2_tri  A, B 2×2 upper triangular: L(Sym²A ⊗ Sym²B) = Π_aΠ_b (1 − (Sym²A)_aa (Sym²B)_bb x)
  Lean 4 core only, no axioms.
-/
import lib.BlockRS

namespace LibTriSymRS
open DetChain LibRing LibSum LibSemi LibMat LibLegSum LibDet LibCircDet LibEulerSum LibKron LibKronDetGen LibSym2 LibSym3

variable {R : Type} (K : CRing R) (A : Nat → Nat → R) (h10 : A 1 0 = K.zero)

include h10 in
theorem s3d0 : s3 K A 0 0 = (K.mul (K.mul (A 0 0) (A 0 0)) (A 0 0)) := by
  show (K.mul K.one (K.mul (K.mul K.one (K.mul (K.mul (A 0 0) (A 0 0)) (A 0 0))) (K.mul K.one K.one))) = _
  first | (rw [h10]; lib_ring) | lib_ring

include h10 in
theorem s3d1 : s3 K A 1 1 = (K.mul (K.mul (A 0 0) (A 0 0)) (A 1 1)) := by
  show (K.add (K.mul K.one (K.mul (K.mul K.one (K.mul (A 0 0) (A 0 0))) (K.mul (A 1 1) K.one))) (K.mul (K.add K.one K.one) (K.mul (K.mul (A 1 0) (A 0 0)) (K.mul K.one (A 0 1))))) = _
  first | (rw [h10]; lib_ring) | lib_ring

include h10 in
theorem s3d2 : s3 K A 2 2 = (K.mul (K.mul (A 0 0) (A 1 1)) (A 1 1)) := by
  show (K.add (K.mul K.one (K.mul (K.mul K.one (A 0 0)) (K.mul (K.mul (A 1 1) (A 1 1)) K.one))) (K.mul (K.add K.one K.one) (K.mul (K.mul (A 1 0) K.one) (K.mul (A 1 1) (A 0 1))))) = _
  first | (rw [h10]; lib_ring) | lib_ring

include h10 in
theorem s3d3 : s3 K A 3 3 = (K.mul (K.mul (A 1 1) (A 1 1)) (A 1 1)) := by
  show (K.mul K.one (K.mul (K.mul K.one K.one) (K.mul (K.mul (K.mul (A 1 1) (A 1 1)) (A 1 1)) K.one))) = _
  first | (rw [h10]; lib_ring) | lib_ring

include h10 in
/-- **L(Sym³A ⊗ U) = Π_j L(a^(3−j) d^j U)** (A upper triangular, U arbitrary). -/
theorem sym3_gen_rs (m' : Nat) (U : Nat → Nat → R) (x : R) :
    det K (4 * (m' + 1)) (imx K (fl (m' + 1) (kron K (s3 K A) U)) x) = K.mul (K.mul (K.mul (det K (m' + 1) (imx K (fun i j => K.mul (K.mul (K.mul (A 0 0) (A 0 0)) (A 0 0)) (U i j)) x)) (det K (m' + 1) (imx K (fun i j => K.mul (K.mul (K.mul (A 0 0) (A 0 0)) (A 1 1)) (U i j)) x))) (det K (m' + 1) (imx K (fun i j => K.mul (K.mul (K.mul (A 0 0) (A 1 1)) (A 1 1)) (U i j)) x))) (det K (m' + 1) (imx K (f  -- (+74 chars; full line in the Zenodo source)
  rw [LibTriGenRS.tri_gen_rs K m' (s3 K A) U x 4 (LibSym3Tri.s3_tri K A h10)]
  show K.mul (det K (m' + 1) (imx K (fun i j => K.mul (s3 K A 0 0) (U i j)) x)) (K.mul (det K (m' + 1) (imx K (fun i j => K.mul (s3 K A 1 1) (U i j)) x)) (K.mul (det K (m' + 1) (imx K (fun i j => K.mul (s3 K A 2 2) (U i j)) x)) (K.mul (det K (m' + 1) (imx K (fun i j => K.mul (s3 K A 3 3) (U i j)) x)) K.one))) = _
  rw [s3d0 K A h10, s3d1 K A h10, s3d2 K A h10, s3d3 K A h10]
  generalize (det K (m' + 1) (imx K (fun i j => K.mul (K.mul (K.mul (A 0 0) (A 0 0)) (A 0 0)) (U i j)) x)) = P0
  generalize (det K (m' + 1) (imx K (fun i j => K.mul (K.mul (K.mul (A 0 0) (A 0 0)) (A 1 1)) (U i j)) x)) = P1
  generalize (det K (m' + 1) (imx K (fun i j => K.mul (K.mul (K.mul (A 0 0) (A 1 1)) (A 1 1)) (U i j)) x)) = P2
  generalize (det K (m' + 1) (imx K (fun i j => K.mul (K.mul (K.mul (A 1 1) (A 1 1)) (A 1 1)) (U i j)) x)) = P3
  lib_ring

/-- **tr(T⊗U)^k = (Σ T_aa^k) tr U^k** (T upper triangular, U arbitrary). -/
theorem tri_gen_psum (n' m' : Nat) (T U : Nat → Nat → R) (hT : ∀ i j, j < i → i < n' + 1 → T i j = K.zero) (k : Nat) :
    trace (toSemi K) ((m' + 1) * (n' + 1)) (mpow (toSemi K) ((m' + 1) * (n' + 1)) (fl (m' + 1) (kron K T U)) k)
      = K.mul (sum (toSemi K) (fun i => pw K (T i i) k) (rng (n' + 1))) (trace (toSemi K) (m' + 1) (mpow (toSemi K) (m' + 1) U k)) := by
  rw [LibKronPowFlat.tr_fl_pow, LibTriEuler.tri_psum K T k (n' + 1) hT]

include h10 in
/-- **L(Sym²A ⊗ Sym²B) = Π_aΠ_b(1 − s_aa(A) s_bb(B) x)** (A, B upper triangular). -/
theorem sym2_sym2_tri (B : Nat → Nat → R) (h10' : B 1 0 = K.zero) (x : R) :
    det K (3 * 3) (imx K (fl 3 (kron K (s2 K A) (s2 K B))) x)
      = prodN K (fun a => prodN K (fun b => K.add K.one (K.neg (K.mul (K.mul (s2 K A a a) (s2 K B b b)) x))) 3) 3 :=
  LibKronTri.kron_tri_euler K 3 2 (s2 K A) (s2 K B) x (LibSym2Tri.s2_tri K A h10) (LibSym2Tri.s2_tri K B h10')

end LibTriSymRS

#print axioms LibTriSymRS.sym3_gen_rs
#print axioms LibTriSymRS.tri_gen_psum
#print axioms LibTriSymRS.sym2_sym2_tri

lib/TriUnip.lean

lean_all/lib/TriUnip.lean

/-
  lib/TriUnip.lean — T46 shared library, cycle 534 (G208, G209, G212): nilpotent/unipotent parts and GL2 upper triangular base change.

  Any commutative ring:
    prodN_one        Π_{i<n} 1 = 1
    nil_euler        N upper triangular with zero diagonal ⇒ det(I − xN) = 1
    nil_psum         same N: tr N^(k+1) = 0
    jordan_euler     D diagonal, N upper triangular with zero diagonal ⇒ det(I − x(D + N)) = det(I − xD)   (local-factor version of the Jordan decomposition)
    bc2_tri / bc2_tri3   T 2×2, T₁₀ = 0: det(I − xT²) = 1 − (t² − 2d)x + d²x²,  det(I − xT³) = 1 − (t³ − 3td)x + d³x²
  Lean 4 core only, no axioms.
-/
import lib.TriBCRS

namespace LibTriUnip
open DetChain LibRing LibSum LibSemi LibMat LibLegSum LibDet LibCircDet LibEulerSum LibTriEuler

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

theorem prodN_one : ∀ n, prodN K (fun _ => K.one) n = K.one
  | 0 => rfl
  | n + 1 => by show K.mul K.one (prodN K (fun _ => K.one) n) = K.one; rw [prodN_one n]; lib_ring

variable (n : Nat) (N : Nat → Nat → R) (hN : ∀ i j, j < i → i < n → N i j = K.zero) (hd : ∀ i, i < n → N i i = K.zero)

include hN hd in
/-- **det(I − xN) = 1** (N nilpotent upper triangular). -/
theorem nil_euler (x : R) : det K n (imx K N x) = K.one := by
  rw [tri_euler K N x n hN]
  refine Eq.trans ?_ (prodN_one K n)
  exact LibKronTri.prodN_ext K n _ (fun _ => K.one) (fun i hi => by
    show K.add K.one (K.neg (K.mul (N i i) x)) = K.one
    rw [hd i hi]; lib_ring)

include hN hd in
/-- **tr N^(k+1) = 0.** -/
theorem nil_psum (k : Nat) : trace (toSemi K) n (mpow (toSemi K) n N (k + 1)) = K.zero := by
  rw [tri_psum K N (k + 1) n hN]
  refine (sum_ext_mem (toSemi K) _ (fun _ => K.zero) (rng n) (fun i hi => ?_)).trans (sum_zero (toSemi K) _)
  show K.mul (pw K (N i i) k) (N i i) = K.zero
  rw [hd i (LibBinom.mem_rng hi)]; lib_ring

include hN hd in
/-- **L(D + N) = L(D)** (D diagonal, N nilpotent upper triangular). -/
theorem jordan_euler (D : Nat → Nat → R) (hD : ∀ i j, i ≠ j → D i j = K.zero) (x : R) :
    det K n (imx K (fun i j => K.add (D i j) (N i j)) x) = det K n (imx K D x) := by
  have hDt : ∀ i j, j < i → i < n → D i j = K.zero := fun i j hji _ => hD i j (fun e => by subst e; exact Nat.lt_irrefl _ hji)
  rw [tri_euler K _ x n (fun i j hji hi => by show K.add (D i j) (N i j) = K.zero; rw [hDt i j hji hi, hN i j hji hi]; lib_ring),
    tri_euler K D x n hDt]
  exact LibKronTri.prodN_ext K n _ _ (fun i hi => by
    show K.add K.one (K.neg (K.mul (K.add (D i i) (N i i)) x)) = _
    rw [hd i hi]; show _ = K.add K.one (K.neg (K.mul (D i i) x)); lib_ring)

variable (T : Nat → Nat → R) (h10 : T 1 0 = K.zero)

theorem tri2 (h10 : T 1 0 = K.zero) : ∀ i j, j < i → i < 2 → T i j = K.zero := by
  intro i j hji hi
  rcases LibSym2.three i (Nat.lt_trans hi (by decide)) with rfl | rfl | rfl
  · exact absurd hji (Nat.not_lt_zero _)
  · rcases LibSym2.three j (Nat.lt_trans hji (by decide)) with rfl | rfl | rfl
    · exact h10
    · exact absurd hji (Nat.lt_irrefl _)
    · exact absurd hji (by decide)
  · exact absurd hi (by decide)

include h10 in
/-- **det(I − xT²) = 1 − (t² − 2d)x + d²x²** (T₁₀ = 0). -/
theorem bc2_tri (x : R) :
    det K 2 (imx K (mpow (toSemi K) 2 T 2) x)
      = K.add (K.add K.one (K.neg (K.mul (K.add (K.mul (K.add (T 0 0) (T 1 1)) (K.add (T 0 0) (T 1 1)))
          (K.neg (K.mul (K.add K.one K.one) (K.mul (T 0 0) (T 1 1))))) x)))
          (K.mul (K.mul (K.mul (T 0 0) (T 1 1)) (K.mul (T 0 0) (T 1 1))) (K.mul x x)) := by
  rw [LibBaseChangeTri.bc_tri K 2 T (tri2 K T h10) 2 x]
  show K.mul (K.add K.one (K.neg (K.mul (K.mul (K.mul K.one (T 0 0)) (T 0 0)) x)))
    (K.mul (K.add K.one (K.neg (K.mul (K.mul (K.mul K.one (T 1 1)) (T 1 1)) x))) K.one) = _
  lib_ring

include h10 in
/-- **det(I − xT³) = 1 − (t³ − 3td)x + d³x²** (T₁₀ = 0). -/
theorem bc2_tri3 (x : R) :
    det K 2 (imx K (mpow (toSemi K) 2 T 3) x)
      = K.add (K.add K.one (K.neg (K.mul (K.add (K.mul (K.mul (K.add (T 0 0) (T 1 1)) (K.add (T 0 0) (T 1 1))) (K.add (T 0 0) (T 1 1)))
          (K.neg (K.mul (K.add K.one (K.add K.one K.one)) (K.mul (K.add (T 0 0) (T 1 1)) (K.mul (T 0 0) (T 1 1)))))) x)))
          (K.mul (K.mul (K.mul (K.mul (T 0 0) (T 1 1)) (K.mul (T 0 0) (T 1 1))) (K.mul (T 0 0) (T 1 1))) (K.mul x x)) := by
  rw [LibBaseChangeTri.bc_tri K 2 T (tri2 K T h10) 3 x]
  show K.mul (K.add K.one (K.neg (K.mul (K.mul (K.mul (K.mul K.one (T 0 0)) (T 0 0)) (T 0 0)) x)))
    (K.mul (K.add K.one (K.neg (K.mul (K.mul (K.mul (K.mul K.one (T 1 1)) (T 1 1)) (T 1 1)) x))) K.one) = _
  lib_ring

end LibTriUnip

#print axioms LibTriUnip.nil_euler
#print axioms LibTriUnip.nil_psum
#print axioms LibTriUnip.jordan_euler
#print axioms LibTriUnip.bc2_tri
#print axioms LibTriUnip.bc2_tri3

lib/Tridiag.lean

lean_all/lib/Tridiag.lean

/-
  lib/Tridiag.lean — T46 shared library, cycle 375 (G50): determinant recurrence for tridiagonal matrices.

    tri a b c      T_ii = a_i, T_{i,i+1} = b_i, T_{i+1,i} = c_i, all else 0
    det_tri        det T_{n+2}(a, b, c) = a₀ det T_{n+1}(a', b', c') − b₀c₀ det T_n(a'', b'', c'')
                   (f' = f(· + 1), f'' = f(· + 2); recurrence for continued-fraction denominators / discrete Schrödinger transfer matrices)
  (First-row expansion: the column-1 minor has first column with only c₀ → det_trans then det_row0_single.)
  Lean 4 core only, no axioms.
-/
import lib.CompanionEig
import lib.DetCol

namespace LibTridiag
open DetChain LibRing LibSum LibSemi LibMat LibLegSum LibDet

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

def tri (a b c : Nat → R) : Nat → Nat → R := fun i j =>
  cond (Nat.beq i j) (a i) (cond (Nat.beq (i + 1) j) (b i) (cond (Nat.beq i (j + 1)) (c j) K.zero))

/-- **det T_{n+2} = a₀ det T' − b₀ c₀ det T''.** -/
theorem det_tri (n : Nat) (a b c : Nat → R) :
    det K (n + 2) (tri K a b c)
      = K.add (K.mul (a 0) (det K (n + 1) (tri K (fun i => a (i + 1)) (fun i => b (i + 1)) (fun i => c (i + 1)))))
          (K.neg (K.mul (K.mul (b 0) (c 0))
            (det K n (tri K (fun i => a (i + 2)) (fun i => b (i + 2)) (fun i => c (i + 2)))))) := by
  rw [LibDetUpper.det_split0 K (n + 1) (tri K a b c), LibDetKernel.sum_split0 K n]
  -- column 0
  have h0 : det K (n + 1) (minor (tri K a b c) 0)
      = det K (n + 1) (tri K (fun i => a (i + 1)) (fun i => b (i + 1)) (fun i => c (i + 1))) :=
    det_ext K (n + 1) _ _ (fun i k _ _ => rfl)
  -- columns from 2 on are 0
  have hz : sum (toSemi K) (fun k => K.mul (K.mul (sg K (1 + (k + 1))) (tri K a b c 0 (1 + (k + 1))))
      (det K (n + 1) (minor (tri K a b c) (1 + (k + 1))))) (rng n) = K.zero := by
    rw [sum_ext (toSemi K) _ (fun _ => K.zero) (fun k => by
      rw [show 1 + (k + 1) = k + 2 by lib_nat]
      show K.mul (K.mul (sg K (k + 2)) (cond (Nat.beq 0 (k + 2)) (a 0) (cond (Nat.beq 1 (k + 2)) (b 0)
        (cond (Nat.beq 0 (k + 2 + 1)) (c (k + 2)) K.zero)))) _ = K.zero
      show K.mul (K.mul (sg K (k + 2)) K.zero) _ = K.zero
      lib_ring)]
    exact sum_zero (toSemi K) _
  -- column 1
  have h1 : det K (n + 1) (minor (tri K a b c) (1 + 0))
      = K.mul (c 0) (det K n (tri K (fun i => a (i + 2)) (fun i => b (i + 2)) (fun i => c (i + 2)))) := by
    rw [← LibDetCol.tr K (minor (tri K a b c) (1 + 0)) (n + 1)]
    rw [LibDetKernel.det_row0_single K n (fun i j => minor (tri K a b c) (1 + 0) j i) 0 (Nat.zero_lt_succ n) (fun j _ hj0 => by
      cases j with
      | zero => exact absurd rfl hj0
      | succ j' => rfl)]
    have hm : det K n (minor (fun i j => minor (tri K a b c) (1 + 0) j i) 0)
        = det K n (fun i j => tri K (fun i => a (i + 2)) (fun i => b (i + 2)) (fun i => c (i + 2)) j i) :=
      det_ext K n _ _ (fun i k _ _ => rfl)
    rw [hm, LibDetCol.tr]
    show K.mul (K.mul K.one (c 0)) _ = _; lib_ring
  rw [h0, hz, h1]
  show K.add (K.mul (a 0) _) (K.add (K.mul (K.mul (K.mul K.one (K.neg K.one)) (b 0)) (K.mul (c 0) _)) K.zero) = _
  lib_ring

end LibTridiag

#print axioms LibTridiag.det_tri

lib/TridiagConst.lean

lean_all/lib/TridiagConst.lean

/-
  lib/TridiagConst.lean — T46 shared library, cycle 376 (G51): determinant of constant tridiagonal matrices.

  Diagonal x + y, upper b, lower c, bc = xy:
    hS n          h_n(x, y) = Σ_{k ≤ n} x^k y^{n−k}   (hS 0 = 1, hS (n+1) = x^{n+1} + y·hS n)
    det_const     det T_n = h_n(x, y)
    det_two       x = y = 1 (diagonal 2, bc = 1) ⇒ det T_n = n + 1
  (Two-step induction via Tridiag.det_tri; determinant form of the discrete Laplacian / Chebyshev U_n.)
  Lean 4 core only, no axioms.
-/
import lib.Tridiag

namespace LibTridiagConst
open DetChain LibRing LibSum LibSemi LibMat LibLegSum LibDet LibTridiag

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

/-- Complete homogeneous polynomial h_n(x, y). -/
def hS (x y : R) : Nat → R
  | 0 => K.one
  | n + 1 => K.add (pw K x (n + 1)) (K.mul y (hS x y n))

theorem hS_rec (x y : R) (n : Nat) :
    hS K x y (n + 2) = K.add (K.mul (K.add x y) (hS K x y (n + 1))) (K.neg (K.mul (K.mul x y) (hS K x y n))) := by
  show K.add (pw K x (n + 2)) (K.mul y (K.add (pw K x (n + 1)) (K.mul y (hS K x y n))))
    = K.add (K.mul (K.add x y) (K.add (pw K x (n + 1)) (K.mul y (hS K x y n)))) (K.neg (K.mul (K.mul x y) (hS K x y n)))
  show K.add (K.mul (pw K x (n + 1)) x) _ = _
  generalize pw K x (n + 1) = P
  generalize hS K x y n = H
  lib_ring

/-- **bc = xy ⇒ det T_n = h_n(x, y).** -/
theorem det_const (x y b c : R) (hbc : K.mul b c = K.mul x y) :
    ∀ n, det K n (tri K (fun _ => K.add x y) (fun _ => b) (fun _ => c)) = hS K x y n
      ∧ det K (n + 1) (tri K (fun _ => K.add x y) (fun _ => b) (fun _ => c)) = hS K x y (n + 1)
  | 0 => by
    refine ⟨rfl, ?_⟩
    show K.add (K.mul (K.mul K.one (K.add x y)) K.one) K.zero = K.add (K.mul K.one x) (K.mul y K.one)
    lib_ring
  | n + 1 => by
    have ⟨ih1, ih2⟩ := det_const x y b c hbc n
    refine ⟨ih2, ?_⟩
    rw [det_tri K n]
    show K.add (K.mul (K.add x y) (det K (n + 1) (tri K (fun _ => K.add x y) (fun _ => b) (fun _ => c))))
      (K.neg (K.mul (K.mul b c) (det K n (tri K (fun _ => K.add x y) (fun _ => b) (fun _ => c))))) = _
    rw [ih1, ih2, hbc, hS_rec]

theorem hS_one : ∀ n, hS K K.one K.one n = castN K (n + 1)
  | 0 => by show K.one = K.add K.one K.zero; lib_ring
  | n + 1 => by
    show K.add (pw K K.one (n + 1)) (K.mul K.one (hS K K.one K.one n)) = K.add K.one (castN K (n + 1))
    rw [hS_one n, LibFinFourier.pw_one]; lib_ring

/-- **Diagonal 2, bc = 1 ⇒ det T_n = n + 1.** -/
theorem det_two (b c : R) (hbc : K.mul b c = K.one) (n : Nat) :
    det K n (tri K (fun _ => K.add K.one K.one) (fun _ => b) (fun _ => c)) = castN K (n + 1) := by
  rw [(det_const K K.one K.one b c (by rw [hbc]; lib_ring) n).1, hS_one]

end LibTridiagConst

#print axioms LibTridiagConst.hS_rec
#print axioms LibTridiagConst.det_const
#print axioms LibTridiagConst.det_two

lib/TridiagEig.lean

lean_all/lib/TridiagEig.lean

/-
  lib/TridiagEig.lean — T46 shared library, cycle 395 (G69): eigenvectors of constant tridiagonal matrices.

  T = tri (diagonal s, superdiagonal 1, subdiagonal xy), size m, v_k = h_k(x, y) (TridiagConst.hS):
    pick_out    t ≥ m ⇒ Σ_{k<m} [k = t] f k = 0
    split3      T_jk v_k = [k = j] s v_k + [k = j+1] v_k + [k+1 = j] xy v_k
    tri_eig     h_m(x, y) = 0, j < m ⇒ (T v)_j = (s + x + y) v_j
  (h_{j+2} = (x+y) h_{j+1} − xy h_j is the eigen-equation; boundary conditions h_{−1} = 0 (not used in the formula) and h_m = 0.
   x = e^{iθ}, y = e^{−iθ}: algebraic version of the sine eigenvectors of the discrete Laplacian.)
  Lean 4 core only, no axioms.
-/
import lib.DiagConic

namespace LibTridiagEig
open DetChain LibRing LibSum LibSemi LibMat LibLegSum LibDet LibTridiag LibTridiagConst

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

theorem pick_out (m t : Nat) (ht : m ≤ t) (f : Nat → R) :
    sum (toSemi K) (fun k => K.mul (indK (toSemi K) (decide (k = t))) (f k)) (rng m) = K.zero := by
  rw [sum_ext_mem (toSemi K) _ (fun _ => K.zero) (rng m) (fun k hk => by
    rw [decide_eq_false (fun (e : k = t) => by subst e; exact Nat.lt_irrefl _ (Nat.lt_of_lt_of_le (LibBinom.mem_rng hk) ht))]
    show K.mul K.zero (f k) = K.zero; lib_ring)]
  exact sum_zero (toSemi K) _

theorem pick_le (m t : Nat) (f : Nat → R) (hf : m ≤ t → f t = K.zero) :
    sum (toSemi K) (fun k => K.mul (indK (toSemi K) (decide (k = t))) (f k)) (rng m) = f t := by
  cases Nat.decLt t m with
  | isTrue h => exact LibDetKernel.pick_rng K m t h f
  | isFalse h => rw [pick_out K m t (Nat.le_of_not_lt h), hf (Nat.le_of_not_lt h)]

theorem split3 (s x y : R) (v : Nat → R) (j k : Nat) :
    K.mul (tri K (fun _ => s) (fun _ => K.one) (fun _ => K.mul x y) j k) (v k)
      = K.add (K.add (K.mul (indK (toSemi K) (decide (k = j))) (K.mul s (v k)))
          (K.mul (indK (toSemi K) (decide (k = j + 1))) (v k)))
          (K.mul (indK (toSemi K) (decide (k + 1 = j))) (K.mul (K.mul x y) (v k))) := by
  show K.mul (cond (Nat.beq j k) s (cond (Nat.beq (j + 1) k) K.one (cond (Nat.beq j (k + 1)) (K.mul x y) K.zero))) (v k) = _
  cases Nat.decEq k j with
  | isTrue e =>
    subst e
    rw [LibMod.beq_self, decide_eq_true rfl, decide_eq_false (Nat.ne_of_lt (Nat.lt_succ_self k)),
      decide_eq_false (Nat.ne_of_gt (Nat.lt_succ_self k))]
    show K.mul s (v k) = K.add (K.add (K.mul K.one (K.mul s (v k))) (K.mul K.zero (v k))) (K.mul K.zero (K.mul (K.mul x y) (v k)))
    lib_ring
  | isFalse e =>
    rw [LibDetAlt.beq_ne (fun h => e h.symm), decide_eq_false e]
    cases Nat.decEq k (j + 1) with
    | isTrue e2 =>
      subst e2
      rw [LibMod.beq_self, decide_eq_true rfl, decide_eq_false (Nat.ne_of_gt (Nat.lt_trans (Nat.lt_succ_self j) (Nat.lt_succ_self (j + 1))))]
      show K.mul K.one (v (j + 1)) = K.add (K.add (K.mul K.zero (K.mul s (v (j + 1)))) (K.mul K.one (v (j + 1))))
        (K.mul K.zero (K.mul (K.mul x y) (v (j + 1))))
      lib_ring
    | isFalse e2 =>
      rw [LibDetAlt.beq_ne (fun h => e2 h.symm), decide_eq_false e2]
      cases Nat.decEq (k + 1) j with
      | isTrue e3 =>
        subst e3
        rw [LibMod.beq_self, decide_eq_true rfl]
        show K.mul (K.mul x y) (v k) = K.add (K.add (K.mul K.zero (K.mul s (v k))) (K.mul K.zero (v k)))
          (K.mul K.one (K.mul (K.mul x y) (v k)))
        lib_ring
      | isFalse e3 =>
        rw [LibDetAlt.beq_ne (fun h => e3 h.symm), decide_eq_false e3]
        show K.mul K.zero (v k) = K.add (K.add (K.mul K.zero (K.mul s (v k))) (K.mul K.zero (v k)))
          (K.mul K.zero (K.mul (K.mul x y) (v k)))
        lib_ring

/-- **h_m(x, y) = 0 ⇒ T (h_k)_k = (s + x + y)(h_k)_k.** -/
theorem tri_eig (s x y : R) (m : Nat) (hm : hS K x y m = K.zero) (j : Nat) (hj : j < m) :
    sum (toSemi K) (fun k => K.mul (tri K (fun _ => s) (fun _ => K.one) (fun _ => K.mul x y) j k) (hS K x y k)) (rng m)
      = K.mul (K.add s (K.add x y)) (hS K x y j) := by
  rw [sum_ext (toSemi K) _ _ (split3 K s x y (hS K x y) j)]
  refine Eq.trans (sum_add (toSemi K) _ _ _) ?_
  have e1 := sum_add (toSemi K) (fun k => K.mul (indK (toSemi K) (decide (k = j))) (K.mul s (hS K x y k)))
    (fun k => K.mul (indK (toSemi K) (decide (k = j + 1))) (hS K x y k)) (rng m)
  show K.add (sum (toSemi K) (fun k => (toSemi K).add _ _) (rng m)) _ = _
  rw [e1]
  show K.add (K.add _ _) _ = _
  rw [LibDetKernel.pick_rng K m j hj (fun k => K.mul s (hS K x y k)),
    pick_le K m (j + 1) (hS K x y) (fun h => by
      rw [show j + 1 = m from Nat.le_antisymm hj h]; exact hm)]
  cases j with
  | zero =>
    rw [sum_ext (toSemi K) _ (fun _ => K.zero) (fun k => by
      rw [decide_eq_false (fun (h : k + 1 = 0) => Nat.noConfusion h)]; show K.mul K.zero _ = K.zero; lib_ring)]
    rw [show sum (toSemi K) (fun _ => K.zero) (rng m) = K.zero from sum_zero (toSemi K) _]
    show K.add (K.add (K.mul s K.one) (K.add (K.mul K.one x) (K.mul y K.one))) K.zero = K.mul (K.add s (K.add x y)) K.one
    lib_ring
  | succ j' =>
    rw [sum_ext (toSemi K) _ (fun k => K.mul (indK (toSemi K) (decide (k = j'))) (K.mul (K.mul x y) (hS K x y k)))
      (fun k => by
        rw [LibGS.decide_congr (p := k + 1 = j' + 1) (q := k = j') Nat.succ.inj (fun h => h ▸ rfl)])]
    rw [LibDetKernel.pick_rng K m j' (Nat.lt_trans (Nat.lt_succ_self j') hj) _, hS_rec]
    generalize hS K x y (j' + 1) = P
    generalize hS K x y j' = Q
    lib_ring

end LibTridiagEig

#print axioms LibTridiagEig.pick_out
#print axioms LibTridiagEig.split3
#print axioms LibTridiagEig.tri_eig

lib/TwistLocal.lean

lean_all/lib/TwistLocal.lean

/-
  lib/TwistLocal.lean — T46 shared library, cycle 539 (G219, G220, G221): unramified twists and traces of upper-triangular products.

  Any commutative ring, c : R (value of an unramified character):
    twist_euler      any M: det(I − x(cM)) = det(I − (cx)M)
    twist_tri        T upper triangular: det(I − x(cT)) = Π(1 − c t_i x)
    twist_kron       L((cT)⊗U)(x) = L(T⊗U)(cx)  (flattened tensor, general)
    tri_mul_psum     T, U upper triangular: tr (TU)^k = Σ (T_ii U_ii)^k
  Lean 4 core only, no axioms.
-/
import lib.NewtonTri5

namespace LibTwistLocal
open DetChain LibRing LibSum LibSemi LibMat LibLegSum LibDet LibCircDet LibEulerSum LibKron LibKronDetGen LibTriEuler LibDivModOwn

variable {R : Type} (K : CRing R) (c : R)

/-- **det(I − x(cM)) = det(I − (cx)M).** -/
theorem twist_euler (n : Nat) (M : Nat → Nat → R) (x : R) :
    det K n (imx K (fun i j => K.mul c (M i j)) x) = det K n (imx K M (K.mul c x)) :=
  det_ext K n _ _ (fun i j _ _ => by
    show K.add (idM K i j) (K.neg (K.mul x (K.mul c (M i j)))) = K.add (idM K i j) (K.neg (K.mul (K.mul c x) (M i j)))
    generalize idM K i j = I; lib_ring)

/-- **L(cT) = Π(1 − c t_i x)** (T upper triangular). -/
theorem twist_tri (n : Nat) (T : Nat → Nat → R) (hT : ∀ i j, j < i → i < n → T i j = K.zero) (x : R) :
    det K n (imx K (fun i j => K.mul c (T i j)) x) = prodN K (fun i => K.add K.one (K.neg (K.mul (K.mul c (T i i)) x))) n := by
  rw [twist_euler, tri_euler K T _ n hT]
  exact LibKronTri.prodN_ext K n _ _ (fun i _ => by
    show K.add K.one (K.neg (K.mul (T i i) (K.mul c x))) = K.add K.one (K.neg (K.mul (K.mul c (T i i)) x)); lib_ring)

/-- **L((cT)⊗U)(x) = L(T⊗U)(cx).** -/
theorem twist_kron (N m : Nat) (T U : Nat → Nat → R) (x : R) :
    det K N (imx K (fl m (kron K (fun i j => K.mul c (T i j)) U)) x) = det K N (imx K (fl m (kron K T U)) (K.mul c x)) := by
  rw [← twist_euler]
  exact det_ext K N _ _ (fun p q _ _ => by
    show K.add (idM K p q) (K.neg (K.mul x (K.mul (K.mul c (T (dm m p).1 (dm m q).1)) (U (dm m p).2 (dm m q).2))))
      = K.add (idM K p q) (K.neg (K.mul x (K.mul c (K.mul (T (dm m p).1 (dm m q).1) (U (dm m p).2 (dm m q).2)))))
    generalize idM K p q = I; lib_ring)

/-- **tr (TU)^k = Σ (T_ii U_ii)^k** (upper triangular). -/
theorem tri_mul_psum (n : Nat) (T U : Nat → Nat → R)
    (hT : ∀ i j, j < i → i < n → T i j = K.zero) (hU : ∀ i j, j < i → i < n → U i j = K.zero) (k : Nat) :
    trace (toSemi K) n (mpow (toSemi K) n (mmul (toSemi K) n T U) k)
      = sum (toSemi K) (fun i => pw K (K.mul (T i i) (U i i)) k) (rng n) := by
  rw [tri_psum K _ k n (fun i j hji hi => LibTriMul.tri_mul_lower K n T U hT hU i j hji hi)]
  exact sum_ext_mem (toSemi K) _ _ (rng n) (fun i hi => by
    show pw K (mmul (toSemi K) n T U i i) k = _
    rw [LibTriMul.tri_mul_diag K n T U hT hU i (LibBinom.mem_rng hi)])

end LibTwistLocal

#print axioms LibTwistLocal.twist_euler
#print axioms LibTwistLocal.twist_tri
#print axioms LibTwistLocal.twist_kron
#print axioms LibTwistLocal.tri_mul_psum

lib/TwistProd.lean

lean_all/lib/TwistProd.lean

/-
  lib/TwistProd.lean — T46 shared library: the sign-twisted Euler product.

    pw_add2     x^(a+b) = x^a x^b
    pw_sq       (x²)^n = x^(n+n)
    twist_odd   (1 − (−x)^(2k+1))(1 − x^(2k+1)) = 1 − (x²)^(2k+1)
    twist_fin   (−x;−x)_{2T} · (x;x)_{2T} · (x⁴;x⁴)_T = (x²;x²)_T² · (x²;x²)_{2T}        (any commutative ring)
  Infinite form: (−x;−x)(x;x)(x⁴;x⁴) = (x²;x²)³, so q(−q⁴;−q⁴)²(q⁸;q⁸)² = η(8τ)⁸/(η(4τ)²η(16τ)²) (64a = 32a twisted).
  Lean 4 core only, proofs by hand, no axioms.
-/
import lib.EtaQuot

namespace LibTwistProd
open DetChain LibRing LibQBinom LibJTPFinite LibQPoch LibEtaQuot

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

theorem pw_add2 (x : R) (a : Nat) : ∀ b, pw K x (a + b) = K.mul (pw K x a) (pw K x b)
  | 0 => by show pw K x a = K.mul (pw K x a) K.one; lib_ring
  | b + 1 => by
    show K.mul (pw K x (a + b)) x = K.mul (pw K x a) (K.mul (pw K x b) x)
    rw [pw_add2 x a b]; lib_ring

theorem pw_sq (x : R) : ∀ n, pw K (pw K x 2) n = pw K x (n + n)
  | 0 => rfl
  | n + 1 => by
    show K.mul (pw K (pw K x 2) n) (pw K x 2) = pw K x (n + 1 + (n + 1))
    rw [pw_sq x n, show n + 1 + (n + 1) = (n + n) + 2 from Nat.succ_add n (n + 1) ▸ rfl, pw_add2 K x (n + n) 2]

theorem neg_sq (x : R) : pw K (K.neg x) 2 = pw K x 2 := by
  show K.mul (K.mul K.one (K.neg x)) (K.neg x) = K.mul (K.mul K.one x) x; lib_ring

theorem twist_odd (x : R) (j : Nat) :
    K.mul (sub K K.one (pw K (K.neg x) (j + j + 1))) (sub K K.one (pw K x (j + j + 1)))
      = sub K K.one (pw K (pw K x 2) (j + j + 1)) := by
  have e1 : pw K (K.neg x) (j + j + 1) = K.mul (pw K (pw K x 2) j) (K.neg x) := by
    show K.mul (pw K (K.neg x) (j + j)) (K.neg x) = _
    rw [← pw_sq K (K.neg x) j, neg_sq]
  have e2 : pw K x (j + j + 1) = K.mul (pw K (pw K x 2) j) x := by
    show K.mul (pw K x (j + j)) x = _
    rw [← pw_sq K x j]
  have e3 : pw K (pw K x 2) (j + j + 1) = K.mul (pw K (pw K x 2) (j + j)) (pw K x 2) := rfl
  have e4 : pw K (pw K x 2) (j + j) = K.mul (pw K (pw K x 2) j) (pw K (pw K x 2) j) := pw_add2 K _ j j
  rw [e1, e2, e3, e4]
  show _ = sub K K.one (K.mul (K.mul (pw K (pw K x 2) j) (pw K (pw K x 2) j)) (K.mul (K.mul K.one x) x))
  lib_ring

/-- **(−x;−x)_{2T}(x;x)_{2T}(x⁴;x⁴)_T = (x²;x²)_T²(x²;x²)_{2T}.** -/
theorem twist_fin (x : R) (T : Nat) :
    K.mul (K.mul (qp K (K.neg x) (T + T)) (qp K x (T + T))) (qp K (pw K x 4) T)
      = K.mul (K.mul (qp K (pw K x 2) T) (qp K (pw K x 2) T)) (qp K (pw K x 2) (T + T)) := by
  have h4 : pw K x 4 = pw K (pw K x 2) 2 := (pw_sq K x 2).symm
  have hodd : K.mul (oddP K (K.neg x) T) (oddP K x T) = oddP K (pw K x 2) T := by
    unfold oddP
    rw [← prodF_mul K]
    exact prodF_ext K _ _ (fun j => twist_odd K x j) T
  rw [even_split K (K.neg x) T, even_split K x T, even_split K (pw K x 2) T, neg_sq, h4, ← hodd]
  lib_ring

end LibTwistProd

#print axioms LibTwistProd.pw_sq
#print axioms LibTwistProd.twist_odd
#print axioms LibTwistProd.twist_fin

lib/TwoPointVar.lean

lean_all/lib/TwoPointVar.lean

/-
  lib/TwoPointVar.lean — T46 shared library: the pair-square sum of two and three points (any commutative ring).

    pairSq_two     pairSq [a, b] = (a − b)²
    pairSq_three   pairSq [a, b, c] = (a − b)² + (a − c)² + (b − c)²
  Lean 4 core only, proofs by hand, no axioms.
-/
import lib.PsumVar

namespace LibTwoPointVar
open DetChain LibRing LibPsumVar

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

theorem pairSq_two (a b : R) : pairSq K [a, b] = K.mul (sub K a b) (sub K a b) := by
  show K.add (K.add (K.mul (sub K a b) (sub K a b)) K.zero) (K.add K.zero K.zero) = K.mul (sub K a b) (sub K a b)
  lib_ring

theorem pairSq_three (a b c : R) : pairSq K [a, b, c] =
    K.add (K.add (K.mul (sub K a b) (sub K a b)) (K.mul (sub K a c) (sub K a c))) (K.mul (sub K b c) (sub K b c)) := by
  show K.add (K.add (K.mul (sub K a b) (sub K a b)) (K.add (K.mul (sub K a c) (sub K a c)) K.zero))
      (K.add (K.add (K.mul (sub K b c) (sub K b c)) K.zero) (K.add K.zero K.zero)) =
    K.add (K.add (K.mul (sub K a b) (sub K a b)) (K.mul (sub K a c) (sub K a c))) (K.mul (sub K b c) (sub K b c))
  lib_ring

end LibTwoPointVar

#print axioms LibTwoPointVar.pairSq_two
#print axioms LibTwoPointVar.pairSq_three

lib/TwoRow.lean

lean_all/lib/TwoRow.lean

/-
  lib/TwoRow.lean — T46 shared library, cycle 616 (G374a): sign of row swaps and the row structure of the shifted identity matrix.

  Any commutative ring:
    sr_i, sr_o        setRow entries
    swap_rows         det(A with row i x, row r y) = −det(A with row i y, row r x)   (i ≠ r, any row positions)
    cr i r            the column such that row r (r ≠ i) of the shifted identity Z_i = minor I (i+1) is e_{cr}: r + 1 if r < i, r if r > i
    rowZ              r ≠ i ⇒ Z_i r c = I (cr i r) c
  Lean 4 core only, no axioms.
-/
import lib.CharThree

namespace LibTwoRow
open DetChain LibRing LibSum LibSemi LibMat LibLegSum LibDet LibDetDep LibCharLin

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

theorem sr_i (A : Nat → Nat → R) (k : Nat) (v : Nat → R) (j : Nat) : setRow A k v k j = v j := by
  show cond (Nat.beq k k) (v j) (A k j) = v j; rw [LibDqRows.beq_self]; rfl

theorem sr_o (A : Nat → Nat → R) (k : Nat) (v : Nat → R) (r j : Nat) (h : r ≠ k) : setRow A k v r j = A r j := by
  show cond (Nat.beq r k) (v j) (A r j) = A r j; rw [LibDetAlt.beq_ne h]; rfl

def S2 (A : Nat → Nat → R) (i r : Nat) (x y : Nat → R) : Nat → Nat → R := setRow (setRow A i x) r y

theorem S2_i (A : Nat → Nat → R) (i r : Nat) (x y : Nat → R) (h : i ≠ r) (j : Nat) : S2 A i r x y i j = x j := by
  unfold S2; rw [sr_o _ _ _ _ _ h, sr_i]
theorem S2_r (A : Nat → Nat → R) (i r : Nat) (x y : Nat → R) (j : Nat) : S2 A i r x y r j = y j := by
  unfold S2; rw [sr_i]
theorem S2_o (A : Nat → Nat → R) (i r : Nat) (x y : Nat → R) (ρ j : Nat) (h1 : ρ ≠ i) (h2 : ρ ≠ r) : S2 A i r x y ρ j = A ρ j := by
  unfold S2; rw [sr_o _ _ _ _ _ h2, sr_o _ _ _ _ _ h1]

/-- Addition in row i. -/
theorem add_i (n : Nat) (A : Nat → Nat → R) (i r : Nat) (hi : i < n) (hir : i ≠ r) (x x' y : Nat → R) :
    det K n (S2 A i r (fun j => K.add (x j) (x' j)) y) = K.add (det K n (S2 A i r x y)) (det K n (S2 A i r x' y)) := by
  apply LibDetRow.det_row_add K n _ _ _ i hi
  · intro j; rw [S2_i _ _ _ _ _ hir, S2_i _ _ _ _ _ hir, S2_i _ _ _ _ _ hir]
  · intro ρ j hρ
    cases Nat.decEq ρ r with
    | isTrue e => subst e; rw [S2_r, S2_r]
    | isFalse e => rw [S2_o _ _ _ _ _ _ _ hρ e, S2_o _ _ _ _ _ _ _ hρ e]
  · intro ρ j hρ
    cases Nat.decEq ρ r with
    | isTrue e => subst e; rw [S2_r, S2_r]
    | isFalse e => rw [S2_o _ _ _ _ _ _ _ hρ e, S2_o _ _ _ _ _ _ _ hρ e]

/-- Addition in row r. -/
theorem add_r (n : Nat) (A : Nat → Nat → R) (i r : Nat) (hr : r < n) (hir : i ≠ r) (x y y' : Nat → R) :
    det K n (S2 A i r x (fun j => K.add (y j) (y' j))) = K.add (det K n (S2 A i r x y)) (det K n (S2 A i r x y')) := by
  apply LibDetRow.det_row_add K n _ _ _ r hr
  · intro j; rw [S2_r, S2_r, S2_r]
  · intro ρ j hρ
    cases Nat.decEq ρ i with
    | isTrue e => subst e; rw [S2_i _ _ _ _ _ hir, S2_i _ _ _ _ _ hir]
    | isFalse e => rw [S2_o _ _ _ _ _ _ _ e hρ, S2_o _ _ _ _ _ _ _ e hρ]
  · intro ρ j hρ
    cases Nat.decEq ρ i with
    | isTrue e => subst e; rw [S2_i _ _ _ _ _ hir, S2_i _ _ _ _ _ hir]
    | isFalse e => rw [S2_o _ _ _ _ _ _ _ e hρ, S2_o _ _ _ _ _ _ _ e hρ]

theorem same (n : Nat) (A : Nat → Nat → R) (i r : Nat) (hi : i < n) (hr : r < n) (hir : i ≠ r) (x : Nat → R) :
    det K n (S2 A i r x x) = K.zero :=
  LibDetRowOp.det_eq_rows' K n _ i r hir hi hr (fun j => by rw [S2_i _ _ _ _ _ hir, S2_r])

/-- **Row swap: det(…x…y…) = −det(…y…x…).** -/
theorem swap_rows (n : Nat) (A : Nat → Nat → R) (i r : Nat) (hi : i < n) (hr : r < n) (hir : i ≠ r) (x y : Nat → R) :
    det K n (S2 A i r x y) = K.neg (det K n (S2 A i r y x)) := by
  have h0 := same K n A i r hi hr hir (fun j => K.add (x j) (y j))
  rw [add_i K n A i r hi hir, add_r K n A i r hr hir, add_r K n A i r hr hir,
    same K n A i r hi hr hir x, same K n A i r hi hr hir y] at h0
  generalize det K n (S2 A i r x y) = a at h0 ⊢
  generalize det K n (S2 A i r y x) = b at h0 ⊢
  have e : a = K.add (K.add (K.add K.zero a) (K.add b K.zero)) (K.neg b) := by lib_ring
  rw [e, h0]; lib_ring

def cr (i r : Nat) : Nat := cond (Nat.blt r i) (r + 1) r

theorem bt {a b : Nat} (h : Nat.blt a b = true) : a < b := Nat.le_of_ble_eq_true h
theorem bf {a b : Nat} (h : Nat.blt a b = false) : b ≤ a := by
  cases Nat.lt_or_ge a b with
  | inl hl => rw [LibDetAlt.blt_true hl] at h; exact Bool.noConfusion h
  | inr hg => exact hg

theorem key (i ρ c : Nat) (h : ρ ≠ i) : (ρ + 1 = cond (Nat.blt c (i + 1)) c (c + 1)) ↔ (cr i ρ = c) := by
  unfold cr
  cases hb : Nat.blt c (i + 1) <;> cases hr : Nat.blt ρ i
  · exact ⟨fun e => Nat.succ.inj e, fun e => by show ρ + 1 = c + 1; rw [show ρ = c from e]⟩
  · have h1 := bf hb; have h2 := bt hr
    exact ⟨fun e => by
        have e' : ρ = c := Nat.succ.inj e
        rw [e'] at h2; exact absurd (Nat.lt_trans h2 (Nat.lt_succ_self i)) (Nat.not_lt_of_le h1),
      fun e => by
        have e' : ρ + 1 = c := e
        rw [← e'] at h1; exact absurd (Nat.le_of_succ_le_succ h1) (Nat.not_le_of_lt h2)⟩
  · have h1 := bt hb; have h2 := bf hr
    have hl : i < ρ := Nat.lt_of_le_of_ne h2 (fun e => h e.symm)
    exact ⟨fun e => by
        have e' : ρ + 1 = c := e
        rw [← e'] at h1; exact absurd (Nat.lt_of_succ_lt_succ h1) (Nat.not_lt_of_le (Nat.le_of_lt hl)),
      fun e => by
        have e' : ρ = c := e
        rw [← e'] at h1; exact absurd (Nat.le_of_lt_succ h1) (Nat.not_le_of_lt hl)⟩
  · exact ⟨fun e => e, fun e => e⟩

theorem rowZ (i ρ c : Nat) (h : ρ ≠ i) : minor (idM K) (i + 1) ρ c = idM K (cr i ρ) c := by
  show indK (toSemi K) (decide (ρ + 1 = cond (Nat.blt c (i + 1)) c (c + 1))) = indK (toSemi K) (decide (cr i ρ = c))
  rw [LibGS.decide_congr (key i ρ c h).1 (key i ρ c h).2]

end LibTwoRow

#print axioms LibTwoRow.swap_rows
#print axioms LibTwoRow.rowZ

lib/TwoRowDet.lean

lean_all/lib/TwoRowDet.lean

/-
  lib/TwoRowDet.lean — T46 shared library, cycle 616 (G374b): determinant of a shifted identity matrix with two rows replaced.

  Any commutative ring, Z_i = minor I (i+1) (n×n), i, r < n, r ≠ i, c = cr i r:
    row_expand   det(setRow M k w) = Σ_{c<n} w_c det(setRow M k e_c)
    two_row      det(Z_i with row i := v, row r := w) = (−1)^i (v₀ w_c − v_c w₀)
  Lean 4 core only, no axioms.
-/
import lib.TwoRow

namespace LibTwoRowDet
open DetChain LibRing LibSum LibSemi LibMat LibLegSum LibDet LibDetDep LibCharLin LibTwoRow

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

theorem comb_id (w : Nat → R) (n j : Nat) (hj : j < n) : comb K (idM K) w n j = w j := by
  show sum (toSemi K) (fun c => K.mul (w c) (idM K c j)) (rng n) = w j
  refine Eq.trans (sum_ext (toSemi K) _ (fun c => K.mul (indK (toSemi K) (decide (c = j))) (w c)) (fun c => by
    show K.mul (w c) (indK (toSemi K) (decide (c = j))) = _; generalize indK (toSemi K) (decide (c = j)) = d; lib_ring) _) ?_
  exact LibDetKernel.pick_rng K n j hj w

theorem row_expand (n : Nat) (M : Nat → Nat → R) (k : Nat) (hk : k < n) (w : Nat → R) :
    det K n (setRow M k w) = sum (toSemi K) (fun c => K.mul (w c) (det K n (setRow M k (idM K c)))) (rng n) := by
  rw [← LibDetExpand.det_setRow_lin K n M (idM K) k hk w n]
  exact det_ext K n _ _ (fun ρ j _ hj => by
    cases Nat.decEq ρ k with
    | isTrue e => subst e; rw [sr_i, sr_i, comb_id K w n j hj]
    | isFalse e => rw [sr_o _ _ _ _ _ e, sr_o _ _ _ _ _ e])

def Z (i : Nat) : Nat → Nat → R := minor (idM K) (i + 1)

theorem cr_lt (n i r : Nat) (hi : i < n) (hr : r < n) : cr i r < n := by
  unfold cr; cases hb : Nat.blt r i
  · exact hr
  · exact Nat.lt_of_le_of_lt (bt hb) hi

theorem cr_ne0 (i r : Nat) (h : r ≠ i) : cr i r ≠ 0 := by
  unfold cr; cases hb : Nat.blt r i
  · intro e; have := bf hb; rw [show r = 0 from e] at this h
    exact h (Nat.le_antisymm this (Nat.zero_le i)).symm
  · intro e; exact Nat.noConfusion e

variable (n i r : Nat) (hi : i < n) (hr : r < n) (hir : r ≠ i)

include hi hr hir in
theorem G_cr (x : Nat → R) : det K n (S2 (Z K i) i r x (idM K (cr i r))) = K.mul (sg K i) (x 0) := by
  rw [← LibShiftDet.shift_det K i n x hi]
  exact det_ext K n _ _ (fun ρ j _ _ => by
    cases Nat.decEq ρ r with
    | isTrue e => subst e; rw [S2_r, sr_o _ _ _ _ _ hir]; exact (rowZ K i ρ j hir).symm
    | isFalse e =>
      cases Nat.decEq ρ i with
      | isTrue e' => subst e'; rw [S2_i _ _ _ _ _ (fun h => hir h.symm), sr_i]
      | isFalse e' => rw [S2_o _ _ _ _ _ _ _ e' e, sr_o _ _ _ _ _ e']; rfl)

include hi hr hir in
theorem G_other (x : Nat → R) (k : Nat) (hk : k < n) (h0 : k ≠ 0) (hc : k ≠ cr i r) :
    det K n (S2 (Z K i) i r x (idM K k)) = K.zero := by
  cases k with
  | zero => exact absurd rfl h0
  | succ k' =>
    let ρ := cond (Nat.blt k' i) k' (k' + 1)
    have hcr : cr i ρ = k' + 1 := by
      show cond (Nat.blt (cond (Nat.blt k' i) k' (k' + 1)) i) (cond (Nat.blt k' i) k' (k' + 1) + 1) (cond (Nat.blt k' i) k' (k' + 1)) = k' + 1
      cases hb : Nat.blt k' i
      · show cond (Nat.blt (k' + 1) i) (k' + 1 + 1) (k' + 1) = k' + 1
        rw [LibDetAlt.blt_false (Nat.le_trans (bf hb) (Nat.le_succ k'))]; rfl
      · show cond (Nat.blt k' i) (k' + 1) k' = k' + 1; rw [hb]; rfl
    have hρi : ρ ≠ i := by
      show cond (Nat.blt k' i) k' (k' + 1) ≠ i
      cases hb : Nat.blt k' i
      · intro e; have := bf hb; rw [← show k' + 1 = i from e] at this; exact Nat.not_succ_le_self k' this
      · intro e; have := bt hb; rw [show k' = i from e] at this; exact Nat.lt_irrefl _ this
    have hρr : ρ ≠ r := fun e => hc (by rw [← e]; exact hcr.symm)
    have hρn : ρ < n := by
      show cond (Nat.blt k' i) k' (k' + 1) < n
      cases Nat.blt k' i
      · exact hk
      · exact Nat.lt_trans (Nat.lt_succ_self k') hk
    exact LibDetRowOp.det_eq_rows' K n _ ρ r hρr hρn hr (fun j => by
      rw [S2_o _ _ _ _ _ _ _ hρi hρr, S2_r]
      show minor (idM K) (i + 1) ρ j = _
      rw [rowZ K i ρ j hρi, hcr])

include hi hr hir in
theorem G_e0 : det K n (S2 (Z K i) i r (idM K 0) (idM K 0)) = K.zero :=
  same K n _ i r hi hr (fun h => hir h.symm) _

include hi hr hir in
theorem col_pick (x : Nat → R) (H : Nat → R) (hcr : H (cr i r) = sg K i)
    (h0 : H 0 = K.zero) (ho : ∀ k, k < n → k ≠ 0 → k ≠ cr i r → H k = K.zero) :
    sum (toSemi K) (fun c => K.mul (x c) (H c)) (rng n) = K.mul (sg K i) (x (cr i r)) := by
  refine Eq.trans (sum_ext_mem (toSemi K) _ (fun c => K.mul (indK (toSemi K) (decide (c = cr i r))) (K.mul (sg K i) (x c))) _ (fun c hc => by
    have hc' := LibBinom.mem_rng hc
    cases Nat.decEq c (cr i r) with
    | isTrue e => rw [decide_eq_true e, e, hcr]; show _ = K.mul K.one _; generalize sg K i = s; lib_ring
    | isFalse e =>
      rw [decide_eq_false e]
      have hz : H c = K.zero := by
        cases Nat.decEq c 0 with
        | isTrue e0 => rw [e0]; exact h0
        | isFalse e0 => exact ho c hc' e0 e
      rw [hz]; show _ = K.mul K.zero _; lib_ring)) ?_
  exact LibDetKernel.pick_rng K n (cr i r) (cr_lt n i r hi hr) (fun c => K.mul (sg K i) (x c))

include hi hr hir in
theorem G_zero (x : Nat → R) :
    det K n (S2 (Z K i) i r x (idM K 0)) = K.neg (K.mul (sg K i) (x (cr i r))) := by
  rw [swap_rows K n _ i r hi hr (fun h => hir h.symm)]
  show K.neg (det K n (setRow (setRow (Z K i) i (idM K 0)) r x)) = _
  rw [row_expand K n _ r hr x]
  refine congrArg K.neg (col_pick K n i r hi hr hir x _ ?_ ?_ ?_)
  · show det K n (S2 (Z K i) i r (idM K 0) (idM K (cr i r))) = sg K i
    rw [G_cr K n i r hi hr hir]; show K.mul (sg K i) K.one = _; generalize sg K i = s; lib_ring
  · exact G_e0 K n i r hi hr hir
  · intro k hk h0 hc; exact G_other K n i r hi hr hir _ k hk h0 hc

include hi hr hir in
/-- **det(Z_i with row i := v, row r := w) = (−1)^i (v₀ w_c − v_c w₀).** -/
theorem two_row (v w : Nat → R) :
    det K n (S2 (Z K i) i r v w) = K.mul (sg K i) (K.add (K.mul (v 0) (w (cr i r))) (K.neg (K.mul (v (cr i r)) (w 0)))) := by
  show det K n (setRow (setRow (Z K i) i v) r w) = _
  rw [row_expand K n _ r hr w]
  have hpt : ∀ c, c ∈ rng n → K.mul (w c) (det K n (setRow (setRow (Z K i) i v) r (idM K c)))
      = K.add (K.mul (indK (toSemi K) (decide (c = cr i r))) (K.mul (K.mul (sg K i) (v 0)) (w c)))
          (K.mul (indK (toSemi K) (decide (c = 0))) (K.mul (K.neg (K.mul (sg K i) (v (cr i r)))) (w c))) := by
    intro c hc
    have hc' := LibBinom.mem_rng hc
    show K.mul (w c) (det K n (S2 (Z K i) i r v (idM K c))) = _
    cases Nat.decEq c (cr i r) with
    | isTrue e =>
      rw [decide_eq_true e, decide_eq_false (fun e0 => cr_ne0 i r hir (e ▸ e0)), e, G_cr K n i r hi hr hir]
      show _ = K.add (K.mul K.one _) (K.mul K.zero _); generalize sg K i = s; lib_ring
    | isFalse e =>
      rw [decide_eq_false e]
      cases Nat.decEq c 0 with
      | isTrue e0 =>
        rw [decide_eq_true e0, e0, G_zero K n i r hi hr hir]
        show _ = K.add (K.mul K.zero _) (K.mul K.one _); generalize sg K i = s; lib_ring
      | isFalse e0 =>
        rw [decide_eq_false e0, G_other K n i r hi hr hir v c hc' e0 e]
        show _ = K.add (K.mul K.zero _) (K.mul K.zero _); lib_ring
  rw [sum_ext_mem (toSemi K) _ _ _ hpt]
  refine Eq.trans (LibSum.sum_add (toSemi K) _ _ _) ?_
  rw [LibDetKernel.pick_rng K n (cr i r) (cr_lt n i r hi hr) (fun c => K.mul (K.mul (sg K i) (v 0)) (w c)),
    LibDetKernel.pick_rng K n 0 (Nat.lt_of_le_of_lt (Nat.zero_le i) hi) (fun c => K.mul (K.neg (K.mul (sg K i) (v (cr i r)))) (w c))]
  show K.add _ _ = _; generalize sg K i = s; lib_ring

end LibTwoRowDet

#print axioms LibTwoRowDet.two_row

lib/TwoSqCount.lean

lean_all/lib/TwoSqCount.lean

/-
  lib/TwoSqCount.lean — T46 shared library (G1513, ladder to Jacobi's two-square formula): the quarter count Q(n) = r₂(n)/4.

    cntB n a B     #{b < B : a² + b² = n}
    Q n            #{(a, b) : 1 ≤ a ≤ n, 0 ≤ b ≤ n, a² + b² = n}   (= r₂(n)/4 for n > 0: one point per rotation orbit)
    Q_zero, Q_one, Q_two, Q_three, Q_five    Q(0) = 0, Q(1) = 1, Q(2) = 1, Q(3) = 0, Q(5) = 2
    cntB_le        cntB n a B ≤ B
  Target (G1515): Q(n) = Σ_{d∣n} χ₋₄(d) for n > 0 (LibChi4, LibEisGen).
  Lean 4 core only, proofs by hand, no axioms.
-/
import lib.Chi4

namespace LibTwoSqCount

def cntB (n a : Nat) : Nat → Nat
  | 0 => 0
  | b + 1 => cntB n a b + cond (Nat.beq (a * a + b * b) n) 1 0

def cntA (n : Nat) : Nat → Nat
  | 0 => 0
  | a + 1 => cntA n a + cntB n (a + 1) (n + 1)

def Q (n : Nat) : Nat := cntA n n

theorem Q_zero : Q 0 = 0 := rfl
theorem Q_one : Q 1 = 1 := rfl
theorem Q_two : Q 2 = 1 := rfl
theorem Q_three : Q 3 = 0 := rfl
theorem Q_five : Q 5 = 2 := rfl

theorem cntB_le (n a : Nat) : ∀ B, cntB n a B ≤ B
  | 0 => Nat.le_refl 0
  | b + 1 => by
    show cntB n a b + cond (Nat.beq (a * a + b * b) n) 1 0 ≤ b + 1
    cases Nat.beq (a * a + b * b) n
    · exact Nat.le_trans (cntB_le n a b) (Nat.le_succ b)
    · exact Nat.succ_le_succ (cntB_le n a b)

/-- #{d ∣ n, d ≤ D : d ≡ r (mod 4)} -/
def cntD (n r : Nat) : Nat → Nat
  | 0 => 0
  | d + 1 => cntD n r d + cond (Nat.beq (LibMod.md (d + 1) n) 0 && Nat.beq (LibMod.md 4 (d + 1)) r) 1 0

/-- Jacobi's formula checked numerically: Q(n) + #{d ∣ n : d ≡ 3} = #{d ∣ n : d ≡ 1}. -/
def chk : Nat → Bool
  | 0 => true
  | n + 1 => chk n && Nat.beq (Q (n + 1) + cntD (n + 1) 3 (n + 1)) (cntD (n + 1) 1 (n + 1))

theorem jacobi_upto_60 : chk 60 = true := by decide

theorem sq4 : ∀ u v, u < 4 → v < 4 → LibMod.md 4 (LibMod.md 4 (u * u) + LibMod.md 4 (v * v)) ≠ 3
  | 0, 0, _, _ => by decide
  | 0, 1, _, _ => by decide
  | 0, 2, _, _ => by decide
  | 0, 3, _, _ => by decide
  | 1, 0, _, _ => by decide
  | 1, 1, _, _ => by decide
  | 1, 2, _, _ => by decide
  | 1, 3, _, _ => by decide
  | 2, 0, _, _ => by decide
  | 2, 1, _, _ => by decide
  | 2, 2, _, _ => by decide
  | 2, 3, _, _ => by decide
  | 3, 0, _, _ => by decide
  | 3, 1, _, _ => by decide
  | 3, 2, _, _ => by decide
  | 3, 3, _, _ => by decide
  | u + 4, _, h, _ => absurd h (fun h' => Nat.lt_irrefl 4 (Nat.lt_of_le_of_lt (Nat.le_add_left 4 u) h'))
  | _, v + 4, _, h => absurd h (fun h' => Nat.lt_irrefl 4 (Nat.lt_of_le_of_lt (Nat.le_add_left 4 v) h'))

theorem sumsq_ne3 (a b : Nat) : LibMod.md 4 (a * a + b * b) ≠ 3 := by
  have h4 : (0 : Nat) < 4 := by decide
  rw [LibMod.md_add (n := 4) h4, LibMod.md_mul (n := 4) h4 a a, LibMod.md_mul (n := 4) h4 b b]
  have := sq4 (LibMod.md 4 a) (LibMod.md 4 b) (LibMod.md_lt h4 a) (LibMod.md_lt h4 b)
  rw [LibMod.md_mul (n := 4) h4 (LibMod.md 4 a), LibMod.md_mul (n := 4) h4 (LibMod.md 4 b)] at this
  rw [LibMod.md_md (n := 4) h4, LibMod.md_md (n := 4) h4] at this
  exact this

theorem cntB_three {n : Nat} (hn : LibMod.md 4 n = 3) (a : Nat) : ∀ B, cntB n a B = 0
  | 0 => rfl
  | b + 1 => by
    show cntB n a b + cond (Nat.beq (a * a + b * b) n) 1 0 = 0
    rw [cntB_three hn a b]
    cases h : Nat.beq (a * a + b * b) n
    · rfl
    · exact absurd (hn ▸ congrArg (LibMod.md 4) (Nat.eq_of_beq_eq_true h)) (sumsq_ne3 a b)

theorem cntA_three {n : Nat} (hn : LibMod.md 4 n = 3) : ∀ A, cntA n A = 0
  | 0 => rfl
  | a + 1 => by
    show cntA n a + cntB n (a + 1) (n + 1) = 0
    rw [cntA_three hn a, cntB_three hn (a + 1) (n + 1)]

/-- n ≡ 3 (mod 4) is never a sum of two squares. -/
theorem Q_three_mod4 {n : Nat} (hn : LibMod.md 4 n = 3) : Q n = 0 := cntA_three hn n

end LibTwoSqCount

#print axioms LibTwoSqCount.Q_zero
#print axioms LibTwoSqCount.Q_one
#print axioms LibTwoSqCount.Q_two
#print axioms LibTwoSqCount.Q_three
#print axioms LibTwoSqCount.Q_five
#print axioms LibTwoSqCount.cntB_le
#print axioms LibTwoSqCount.jacobi_upto_60
#print axioms LibTwoSqCount.sumsq_ne3
#print axioms LibTwoSqCount.Q_three_mod4

lib/TwoSqFF.lean

lean_all/lib/TwoSqFF.lean

/-
  lib/TwoSqFF.lean — T46 shared library, cycle 257: number of solutions of a binary quadratic form (mod p).

  finite field data (LibFFSum), q − 1 = n + 1 = 2h, a, b ≠ 0, b·ib = 1:
    form_count   #{(x, y) : a x² + b y² = c} ≡ −(−a·ib)^h          (all c; for c ≠ 0 actually q − χ(−ab))
    two_squares  #{(x, y) : x² + y² = c} ≡ −(−1)^h
  (SqrtCountFF.sqrt_count + QuadSumFF.quad_sum.)
  Lean 4 core only, no axioms.
-/
import lib.QuadSumFF

namespace LibTwoSqFF
open DetChain LibRing LibSum LibSemi LibBinom LibFFSum LibDist LibFFPoly LibPoly LibLegSum LibLegHasse

variable {R : Type} [DecidableEq R] (K : CRing R)
variable (hK : NoZD K) (h10 : K.one ≠ K.zero) (els : List R) (hd : Dist els) (hall : ∀ x, x ∈ els) (n : Nat)
  (hlen : els.length = n + 2) (hfer : ∀ x, x ≠ K.zero → pw K x (n + 1) = K.one) (hq : castN K (n + 2) = K.zero)

omit [DecidableEq R] in
theorem solve_y (a b c ib x y : R) (hib : K.mul b ib = K.one) :
    (K.add (K.mul a (K.mul x x)) (K.mul b (K.mul y y)) = c) ↔ (K.mul y y = K.mul (sub K c (K.mul a (K.mul x x))) ib) := by
  constructor
  · intro e
    rw [← e]
    have : K.mul (sub K (K.add (K.mul a (K.mul x x)) (K.mul b (K.mul y y))) (K.mul a (K.mul x x))) ib
        = K.mul (K.mul y y) (K.mul b ib) := by unfold sub; lib_ring
    rw [this, hib]; lib_ring
  · intro e
    have : K.add (K.mul a (K.mul x x)) (K.mul b (K.mul y y))
        = K.add c (K.mul (sub K c (K.mul a (K.mul x x))) (sub K (K.mul b ib) K.one)) := by
      rw [e]; unfold sub; lib_ring
    rw [this, hib]; unfold sub; lib_ring

include hK h10 hd hall hlen hfer hq in
/-- **#{a x² + b y² = c} ≡ −(−a·ib)^h.** -/
theorem form_count (h : Nat) (hn : n + 1 = 2 * h) (a b c ib : R) (ha : a ≠ K.zero) (hib : K.mul b ib = K.one) :
    sum (toSemi K) (fun x => sum (toSemi K)
      (fun y => indK (toSemi K) (decide (K.add (K.mul a (K.mul x x)) (K.mul b (K.mul y y)) = c))) els) els
      = K.neg (pw K (K.mul (K.neg a) ib) h) := by
  have hib0 : ib ≠ K.zero := fun e => h10 (by rw [← hib, e]; lib_ring)
  rw [sum_ext (toSemi K) _ (fun x => sum (toSemi K)
      (fun y => indK (toSemi K) (decide (K.mul y y = K.mul (sub K c (K.mul a (K.mul x x))) ib))) els) (fun x =>
    sum_ext (toSemi K) _ _ (fun y => by
      by_cases e : K.add (K.mul a (K.mul x x)) (K.mul b (K.mul y y)) = c
      · rw [decide_eq_true e, decide_eq_true ((solve_y K a b c ib x y hib).1 e)]
      · rw [decide_eq_false e, decide_eq_false (fun e' => e ((solve_y K a b c ib x y hib).2 e'))]) els)]
  rw [sum_ext (toSemi K) _ _ (fun x => LibSqrtFF.sqrt_count K hK h10 els hd hall n hlen hfer hq h hn _), LibSqrtFF.sum_addK]
  rw [show sum (toSemi K) (fun _ => K.one) els = K.zero from powsum_zero_k K els n hlen hq]
  rw [sum_ext (toSemi K) _ (fun x => pw K (K.add (K.add (K.mul (K.mul (K.neg a) ib) (K.mul x x)) (K.mul K.zero x)) (K.mul c ib)) h)
    (fun x => by congr 1; unfold sub; lib_ring)]
  have ha' : K.mul (K.neg a) ib ≠ K.zero := fun e => by
    cases hK _ _ e with
    | inl e1 => exact ha (by have : a = K.neg (K.neg a) := by lib_ring
                             rw [this, e1]; lib_ring)
    | inr e2 => exact hib0 e2
  rw [LibQuadSumFF.quad_sum K hK h10 els hd hall n hlen hfer hq h hn _ _ _ ha']
  lib_ring

include hK h10 hd hall hlen hfer hq in
/-- **#{x² + y² = c} ≡ −(−1)^h.** -/
theorem two_squares (h : Nat) (hn : n + 1 = 2 * h) (c : R) :
    sum (toSemi K) (fun x => sum (toSemi K)
      (fun y => indK (toSemi K) (decide (K.add (K.mul K.one (K.mul x x)) (K.mul K.one (K.mul y y)) = c))) els) els
      = K.neg (pw K (K.neg K.one) h) := by
  rw [form_count K hK h10 els hd hall n hlen hfer hq h hn K.one K.one c K.one h10 (by lib_ring),
    show K.mul (K.neg K.one) K.one = K.neg K.one by lib_ring]

end LibTwoSqFF

#print axioms LibTwoSqFF.solve_y
#print axioms LibTwoSqFF.form_count
#print axioms LibTwoSqFF.two_squares

lib/TwoSqJacobi.lean

lean_all/lib/TwoSqJacobi.lean

/-
  lib/TwoSqJacobi.lean — T46 shared library (G1515, Jacobi's two-square formula).

    prime_cases    a prime p is 2, 4j + 5 or 4k + 3
    eig            a(n) = Q(n) is a T_p-eigenvector with factor χ₋₄(p) and eigenvalue 1 + χ₋₄(p) at every prime p
                   (p = 2: Q(2n) = Q(n);  p ≡ 1: Q(pn) + [p∣n]Q(n/p) = 2Q(n);  p ≡ 3: Q(pn) = [p∣n]Q(n/p))
    jacobi_Q       Q(n) = Σ_{d ∣ n} χ₋₄(d)  for n > 0   (so r₂(n) = 4 Σ_{d ∣ n} χ₋₄(d))
  Proof: Q(1) = 1 and the eigen relations determine a normalized eigen-sequence (LibEisGen.eis_unique2).
  Lean 4 core only, proofs by hand, no axioms.
-/
import lib.TwoSqPiQ

namespace LibTwoSqJacobi
open LibMod LibTwoSqCount LibPrimePow LibInt LibChi4 LibHeckeOp

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

theorem prime_cases {p : Nat} (hp : Prime p) : p = 2 ∨ (∃ j, p = 4 * j + 5) ∨ (∃ k, p = 4 * k + 2 + 1) := by
  have s := spec 4 p
  have hr := md_lt (n := 4) (by decide) p
  generalize dv 4 p = q at s
  generalize md 4 p = r at s hr
  match r, hr with
  | 0, _ =>
    left
    cases hp.2 2 ⟨q + q, by rw [s]; lib_nat⟩ with
    | inl h => exact absurd h two_ne_one
    | inr h => exact h.symm
  | 2, _ =>
    left
    cases hp.2 2 ⟨q + q + 1, by rw [s]; lib_nat⟩ with
    | inl h => exact absurd h two_ne_one
    | inr h => exact h.symm
  | 1, _ =>
    cases q with
    | zero => rw [s] at hp; exact absurd hp.1 (Nat.lt_irrefl 1)
    | succ j => exact Or.inr (Or.inl ⟨j, s.trans (by lib_nat)⟩)
  | 3, _ => exact Or.inr (Or.inr ⟨q, s.trans (by lib_nat)⟩)
  | r + 4, h => exact absurd h (fun h' => Nat.lt_irrefl 4 (Nat.lt_of_le_of_lt (Nat.le_add_left 4 r) h'))

def a (n : Nat) : Int := (Q n : Int)

theorem cast_add (x y : Nat) : ((x + y : Nat) : Int) = (x : Int) + (y : Int) := rfl

theorem eig : ∀ p, Prime p → ∀ n,
    hk intR p (intR.mul (chi4 intR p) (LibDivFun.one1 intR p)) a n
      = intR.mul (intR.add (chi4 intR p) (LibDivFun.one1 intR p)) (a n) := by
  intro p hp n
  rcases prime_cases hp with h | ⟨j, h⟩ | ⟨k, h⟩
  · subst h
    have c : chi4 intR 2 = 0 := by
      show F4 intR (md 4 2) = 0; rw [md_of_lt (n := 4) (by decide) (by decide)]; rfl
    unfold hk; rw [c]
    have hq : Q (2 * n) = Q n := by rw [show 2 * n = n + n by lib_nat]; exact LibTwoSqP2.Q_two n
    cases Nat.beq (md 2 n) 0
    · show (Q (2 * n) : Int) + 0 = (0 + 1) * (Q n : Int); rw [hq]; lib_int
    · show (Q (2 * n) : Int) + 0 * 1 * (Q (dv 2 n) : Int) = (0 + 1) * (Q n : Int); rw [hq]; lib_int
  · subst h
    have e : LibEulerCrit.k (2 * j + 1) + 1 = 4 * j + 5 := by show 2 * (2 * j + 1 + 1) + 1 = 4 * j + 5; lib_nat
    have hP1 : LibZp.Prime1 (LibEulerCrit.k (2 * j + 1)) := fun d hd => by
      rw [e] at hd ⊢; exact hp.2 d hd
    obtain ⟨u, v, huv⟩ := LibFermatTwoSq.fermat_two_sq j hP1
    rw [e] at huv
    have hu := LibTwoSqUniq.pos_of_prime_sq hp huv
    have hv := LibTwoSqUniq.pos_of_prime_sq hp (show v * v + u * u = 4 * j + 5 by rw [← huv]; lib_nat)
    have hp0 : 0 < 4 * j + 5 := Nat.zero_lt_succ _
    have h2 : ¬ 4 * j + 5 ∣ 2 := fun hd =>
      Nat.ne_of_lt (Nat.lt_of_lt_of_le (by decide : 2 < 5) (Nat.le_add_left 5 (4 * j)))
        (LibTwoSqUniq.eq_of_dvd_le hd (by decide) (Nat.le_trans (by decide : 2 ≤ 5) (Nat.le_add_left 5 (4 * j))))
    have c : chi4 intR (4 * j + 5) = 1 := by
      show F4 intR (md 4 (4 * j + 5)) = 1
      rw [(md_unique (n := 4) (a := 4 * j + 5) (q := j + 1) (r := 1) (by decide) (by lib_nat)).1]; rfl
    unfold hk; rw [c]
    cases eb : Nat.beq (md (4 * j + 5) n) 0
    · have hn : ¬ 4 * j + 5 ∣ n := fun hd => by
        rw [md_zero_of_dvd hp0 hd, LibMod.beq_self] at eb; exact Bool.noConfusion eb
      show (Q ((4 * j + 5) * n) : Int) + 0 = (1 + 1) * (Q n : Int)
      rw [LibTwoSqPiQ.Q_pi_ndvd huv hp.2 h2 hu hv hn, cast_add]; lib_int
    · show (Q ((4 * j + 5) * n) : Int) + 1 * 1 * (Q (dv (4 * j + 5) n) : Int) = (1 + 1) * (Q n : Int)
      have sn := spec (4 * j + 5) n
      rw [Nat.eq_of_beq_eq_true eb, Nat.add_zero] at sn
      generalize dv (4 * j + 5) n = m at sn ⊢
      subst sn
      have hI : (Q ((4 * j + 5) * ((4 * j + 5) * m)) : Int) + (Q m : Int)
          = (Q ((4 * j + 5) * m) : Int) + (Q ((4 * j + 5) * m) : Int) :=
        congrArg (fun x : Nat => (x : Int)) (LibTwoSqPiQ.Q_pi_dvd huv hp.2 h2 hu hv m)
      have hX : (Q ((4 * j + 5) * ((4 * j + 5) * m)) : Int)
          = (Q ((4 * j + 5) * m) : Int) + (Q ((4 * j + 5) * m) : Int) - (Q m : Int) := by rw [← hI]; lib_int
      rw [hX]; lib_int
  · subst h
    have hp1 : LibZp.Prime1 (4 * k + 2) := hp.2
    have hp0 : 0 < 4 * k + 2 + 1 := Nat.zero_lt_succ _
    have c : chi4 intR (4 * k + 2 + 1) = -1 := by
      show F4 intR (md 4 (4 * k + 2 + 1)) = -1
      rw [(md_unique (n := 4) (a := 4 * k + 2 + 1) (q := k) (r := 3) (by decide) (by lib_nat)).1]; rfl
    unfold hk; rw [c]
    cases eb : Nat.beq (md (4 * k + 2 + 1) n) 0
    · have hn : ¬ 4 * k + 2 + 1 ∣ n := fun hd => by
        rw [md_zero_of_dvd hp0 hd, LibMod.beq_self] at eb; exact Bool.noConfusion eb
      show (Q ((4 * k + 2 + 1) * n) : Int) + 0 = (-1 + 1) * (Q n : Int)
      rw [LibTwoSqP3.Q_p3_zero k hp1 hn]
      show (0 : Int) + 0 = (-1 + 1) * (Q n : Int)
      generalize (Q n : Int) = X; lib_int
    · show (Q ((4 * k + 2 + 1) * n) : Int) + -1 * 1 * (Q (dv (4 * k + 2 + 1) n) : Int) = (-1 + 1) * (Q n : Int)
      have sn := spec (4 * k + 2 + 1) n
      rw [Nat.eq_of_beq_eq_true eb, Nat.add_zero] at sn
      generalize dv (4 * k + 2 + 1) n = m at sn ⊢
      subst sn
      rw [show (4 * k + 2 + 1) * ((4 * k + 2 + 1) * m) = (4 * k + 2 + 1) * (4 * k + 2 + 1) * m by lib_nat,
        LibTwoSqP3.Q_p3_scale k hp1 m]
      generalize (Q m : Int) = X; generalize (Q ((4 * k + 2 + 1) * m) : Int) = Y
      lib_int

/-- **Jacobi's two-square theorem** (quarter form): Q(n) = Σ_{d ∣ n} χ₋₄(d) for n > 0. -/
theorem jacobi_Q {n : Nat} (hn : 0 < n) : a n = LibEisGen.e intR (chi4 intR) (LibDivFun.one1 intR) n :=
  LibEisGen.eis_unique2 intR (chi4 intR) (LibDivFun.one1 intR) (chi4_cmult intR) (LibEisBridge.one1_cmult intR) a eig rfl hn

end LibTwoSqJacobi

#print axioms LibTwoSqJacobi.two_ne_one
#print axioms LibTwoSqJacobi.prime_cases
#print axioms LibTwoSqJacobi.cast_add
#print axioms LibTwoSqJacobi.eig
#print axioms LibTwoSqJacobi.jacobi_Q

lib/TwoSqP1.lean

lean_all/lib/TwoSqP1.lean

/-
  lib/TwoSqP1.lean — T46 shared library (G1514c, Jacobi's two-square formula): Q(p) = 2 for primes p ≡ 1 (mod 4).

    S1_ind_ge, S1_ind_lt   Σ_{b<B} [b = t] = 0 (B ≤ t), = 1 (t < B)
    S1_add                 Σ (g + h) = Σ g + Σ h
    inner_one, inner_zero  #{b : a² + b² = n} = [a = u] counts for a fixed a
    Q_p1                   p = 4j + 5 prime ⇒ Q(p) = 2   (the two ordered representations (u, v), (v, u), u ≠ v)
  Lean 4 core only, proofs by hand, no axioms.
-/
import lib.FermatTwoSq
import lib.TwoSqP3

namespace LibTwoSqP1
open LibMod LibSumScale LibTwoSqCount LibTwoSqP3

theorem S1_ind_ge (t : Nat) : ∀ B, B ≤ t → S1 (fun b => cond (Nat.beq b t) 1 0) B = 0
  | 0, _ => rfl
  | B + 1, h => by
    show S1 _ B + cond (Nat.beq B t) 1 0 = 0
    rw [S1_ind_ge t B (Nat.le_of_succ_le h), LibTwoSqP3.beq_false_of_ne (Nat.ne_of_lt h)]
    rfl

theorem S1_ind_lt (t : Nat) : ∀ B, t < B → S1 (fun b => cond (Nat.beq b t) 1 0) B = 1
  | 0, h => absurd h (Nat.not_lt_zero t)
  | B + 1, h => by
    show S1 _ B + cond (Nat.beq B t) 1 0 = 1
    cases Nat.lt_or_ge t B with
    | inl hl => rw [S1_ind_lt t B hl, LibTwoSqP3.beq_false_of_ne (Nat.ne_of_gt hl)]; rfl
    | inr hg =>
      have e : t = B := Nat.le_antisymm (Nat.le_of_lt_succ h) hg
      subst e
      rw [S1_ind_ge t t (Nat.le_refl t), LibMod.beq_self]
      rfl

theorem S1_add (g h : Nat → Nat) : ∀ B, S1 (fun a => g a + h a) B = S1 g B + S1 h B
  | 0 => rfl
  | B + 1 => by
    show S1 _ B + (g B + h B) = (S1 g B + g B) + (S1 h B + h B)
    rw [S1_add g h B]; lib_nat

/-- for fixed a, if a² + b² = n holds exactly for b = t, the inner count is 1 (t < B) -/
theorem inner_one {n a t B : Nat} (h : ∀ b, a * a + b * b = n → b = t) (ht : a * a + t * t = n) (htB : t < B) :
    inner n a B = 1 := by
  unfold inner
  rw [S1_ext _ (fun b => cond (Nat.beq b t) 1 0) B (fun b _ => by
    show cond (Nat.beq (a * a + b * b) n) 1 0 = cond (Nat.beq b t) 1 0
    cases e : Nat.beq (a * a + b * b) n
    · cases e2 : Nat.beq b t
      · rfl
      · rw [Nat.eq_of_beq_eq_true e2, ht, LibMod.beq_self] at e; exact Bool.noConfusion e
    · rw [h b (Nat.eq_of_beq_eq_true e), LibMod.beq_self])]
  exact S1_ind_lt t B htB

theorem Q_p1 (j : Nat) (hp : LibZp.Prime1 (LibEulerCrit.k (2 * j + 1))) : Q (LibEulerCrit.k (2 * j + 1) + 1) = 2 := by
  generalize hPdef : LibEulerCrit.k (2 * j + 1) + 1 = P
  have hpr : LibPrimePow.Prime P := by rw [← hPdef]; exact ⟨Nat.succ_lt_succ (Nat.zero_lt_succ _), hp⟩
  obtain ⟨u, v, huv⟩ := LibFermatTwoSq.fermat_two_sq j hp
  rw [hPdef] at huv
  have u0 := LibTwoSqUniq.pos_of_prime_sq hpr huv
  have v0 := LibTwoSqUniq.pos_of_prime_sq hpr (show v * v + u * u = P by rw [← huv]; lib_nat)
  have hodd : ∀ w, P ≠ w + w := fun w e => by
    rw [← hPdef] at e
    have h2 : md 2 (LibEulerCrit.k (2 * j + 1) + 1) = 1 :=
      (md_unique (n := 2) (a := LibEulerCrit.k (2 * j + 1) + 1) (q := 2 * j + 1 + 1) (r := 1) (by decide) rfl).1
    rw [e, show w + w = 2 * w + 0 by lib_nat, (md_unique (n := 2) (a := 2 * w + 0) (q := w) (r := 0) (by decide) rfl).1] at h2
    exact Nat.noConfusion h2
  have huv_ne : u ≠ v := fun e => by subst e; exact hodd (u * u) huv.symm
  have hle : ∀ x, x * x ≤ P → x < P + 1 := fun x hx => Nat.lt_succ_of_le (Nat.le_trans
    (by cases x with | zero => exact Nat.zero_le _ | succ x' => exact Nat.le_mul_of_pos_left _ (Nat.zero_lt_succ x')) hx)
  have hvP : v < P + 1 := hle v (by rw [← huv]; exact Nat.le_add_left _ _)
  have huP : u < P + 1 := hle u (by rw [← huv]; exact Nat.le_add_right _ _)
  -- the summand of Q is [a = u] + [a = v]
  rw [Q_Qb]
  unfold Qb
  rw [S1_ext _ (fun a => cond (Nat.beq a u) 1 0 + cond (Nat.beq a v) 1 0) (P + 1) (fun a _ => by
    show cond (Nat.beq a 0) 0 (inner P a (P + 1)) = cond (Nat.beq a u) 1 0 + cond (Nat.beq a v) 1 0
    cases ea : Nat.beq a u
    · cases eb : Nat.beq a v
      · -- a is neither u nor v: no b works (a = 0 is excluded by the cond)
        cases e0 : Nat.beq a 0
        · rw [inner_zero_of (fun b e => by
            cases LibTwoSqUniq.two_sq_uniq hpr e huv with
            | inl h => exact absurd h.1 (Nat.ne_of_beq_eq_false ea)
            | inr h => exact absurd h.1 (Nat.ne_of_beq_eq_false eb))]
          rfl
        · rfl
      · have av : a = v := Nat.eq_of_beq_eq_true eb
        subst av
        rw [LibTwoSqP3.beq_false_of_ne (Nat.ne_of_gt v0), inner_one (t := u) (fun b e => by
            cases LibTwoSqUniq.two_sq_uniq hpr e huv with
            | inl h => exact absurd h.1.symm huv_ne
            | inr h => exact h.2) (by rw [← huv]; lib_nat) huP]
        rfl
    · have au : a = u := Nat.eq_of_beq_eq_true ea
      subst au
      rw [LibTwoSqP3.beq_false_of_ne huv_ne, LibTwoSqP3.beq_false_of_ne (Nat.ne_of_gt u0), inner_one (t := v) (fun b e => by
          cases LibTwoSqUniq.two_sq_uniq hpr e huv with
          | inl h => exact h.2
          | inr h => exact absurd h.1 huv_ne) huv hvP]
      rfl)]
  rw [S1_add, S1_ext _ (fun a => cond (Nat.beq a u) 1 0) _ (fun _ _ => rfl), S1_ind_lt u _ huP, S1_ind_lt v _ hvP]

end LibTwoSqP1

#print axioms LibTwoSqP1.S1_ind_ge
#print axioms LibTwoSqP1.S1_ind_lt
#print axioms LibTwoSqP1.S1_add
#print axioms LibTwoSqP1.inner_one
#print axioms LibTwoSqP1.Q_p1

lib/TwoSqP2.lean

lean_all/lib/TwoSqP2.lean

/-
  lib/TwoSqP2.lean — T46 shared library (G1515c, Jacobi's two-square formula): the prime 2, Q(2n) = Q(n).

  Multiplication by 1 + i followed by rotation into the quarter plane:
    sig (u, v) = (u − v, u + v) if v < u,  (u + v, v − u) otherwise      (u² + v² = n  ↦  a² + b² = 2n)
    tau (a, b) = ((a + b)/2, (b − a)/2) if a ≤ b,  ((a − b)/2, (a + b)/2) otherwise
    sig_spec, tau_spec   mutually inverse on the solution sets (u ≥ 1, a ≥ 1)
    Qb_QP                Qb as a single indexed count
    Q_two                Q(2n) = Q(n)
  Lean 4 core only, proofs by hand, no axioms.
-/
import lib.CountBij

namespace LibTwoSqP2
open LibMod LibSumScale LibTwoSqCount LibTwoSqP3 LibCountBij

theorem sub_left : ∀ v d : Nat, v + d - v = d
  | 0, d => by rw [Nat.zero_add]; rfl
  | v + 1, d => by rw [show v + 1 + d = (v + d) + 1 by lib_nat, Nat.succ_sub_succ]; exact sub_left v d

theorem half (w : Nat) : dv 2 (w + w) = w :=
  (md_unique (n := 2) (a := w + w) (q := w) (r := 0) (by decide) (by lib_nat)).2

theorem le_sq (x : Nat) : x ≤ x * x := by
  cases x with
  | zero => exact Nat.le_refl 0
  | succ x' => exact Nat.le_mul_of_pos_left _ (Nat.zero_lt_succ x')

theorem blt_true {v u : Nat} (h : v < u) : Nat.blt v u = true := Nat.ble_eq_true_of_le h

theorem blt_false {v u : Nat} (h : u ≤ v) : Nat.blt v u = false := by
  cases e : Nat.blt v u
  · rfl
  · exact absurd (Nat.lt_of_lt_of_le (Nat.le_of_ble_eq_true e) h) (Nat.lt_irrefl v)

theorem ble_true {a b : Nat} (h : a ≤ b) : Nat.ble a b = true := Nat.ble_eq_true_of_le h

theorem ble_false {a b : Nat} (h : b < a) : Nat.ble a b = false := by
  cases e : Nat.ble a b
  · rfl
  · exact absurd (Nat.lt_of_le_of_lt (Nat.le_of_ble_eq_true e) h) (Nat.lt_irrefl a)

def sig (u v : Nat) : Nat × Nat := cond (Nat.blt v u) (u - v, u + v) (u + v, v - u)

def tau (a b : Nat) : Nat × Nat := cond (Nat.ble a b) (dv 2 (a + b), dv 2 (b - a)) (dv 2 (a - b), dv 2 (a + b))

/-- parity: a² + (a + e)² even forces e even -/
theorem even_gap {a e n : Nat} (h : a * a + (a + e) * (a + e) = n + n) : ∃ w, e = w + w := by
  have sp := spec 2 e
  have hr := md_lt (n := 2) (by decide) e
  generalize dv 2 e = w at sp
  generalize md 2 e = r at sp hr
  cases r with
  | zero => exact ⟨w, by rw [sp]; lib_nat⟩
  | succ r' =>
    cases r' with
    | succ r'' => exact absurd hr (by intro h'; exact Nat.lt_irrefl 2 (Nat.lt_of_le_of_lt (Nat.succ_le_succ (Nat.succ_le_succ (Nat.zero_le r''))) h'))
    | zero =>
      exfalso
      subst sp
      have h1 := (md_unique (n := 2) (a := n + n) (q := n) (r := 0) (by decide) (by lib_nat)).1
      rw [← h] at h1
      have h2 := (md_unique (n := 2) (a := a * a + (a + (2 * w + 1)) * (a + (2 * w + 1)))
        (q := a * a + 2 * a * w + a + 2 * w * w + 2 * w) (r := 1) (by decide) (by lib_nat)).1
      rw [h1] at h2
      exact Nat.noConfusion h2

theorem sig_spec {u v n : Nat} (hu : 1 ≤ u) (h : u * u + v * v = n) :
    1 ≤ (sig u v).1 ∧ (sig u v).1 * (sig u v).1 + (sig u v).2 * (sig u v).2 = n + n ∧ tau (sig u v).1 (sig u v).2 = (u, v) := by
  cases Nat.lt_or_ge v u with
  | inl hl =>
    obtain ⟨d, hd⟩ := LibHeckeRel.ex_add (Nat.le_of_lt hl)
    have hd' : u = v + d := by rw [hd]; lib_nat
    have d1 : 1 ≤ d := by
      cases d with
      | zero => rw [Nat.add_zero] at hd'; subst hd'; exact absurd hl (Nat.lt_irrefl u)
      | succ d' => exact Nat.succ_le_succ (Nat.zero_le d')
    subst hd'
    unfold sig
    rw [blt_true hl, sub_left]
    refine ⟨d1, by show d * d + (v + d + v) * (v + d + v) = n + n; rw [← h]; lib_nat, ?_⟩
    show cond (Nat.ble d (v + d + v)) (dv 2 (d + (v + d + v)), dv 2 (v + d + v - d)) _ = (v + d, v)
    rw [ble_true (by rw [show v + d + v = d + (v + v) by lib_nat]; exact Nat.le_add_right d _)]
    show (dv 2 (d + (v + d + v)), dv 2 (v + d + v - d)) = (v + d, v)
    rw [show d + (v + d + v) = (v + d) + (v + d) by lib_nat, half, show v + d + v = d + (v + v) by lib_nat, sub_left, half]
  | inr hg =>
    obtain ⟨e, he⟩ := LibHeckeRel.ex_add hg
    have he' : v = u + e := by rw [he]; lib_nat
    subst he'
    unfold sig
    rw [blt_false hg, sub_left]
    refine ⟨Nat.le_trans hu (Nat.le_add_right u _), by show (u + (u + e)) * (u + (u + e)) + e * e = n + n; rw [← h]; lib_nat, ?_⟩
    show cond (Nat.ble (u + (u + e)) e) _ (dv 2 (u + (u + e) - e), dv 2 (u + (u + e) + e)) = (u, u + e)
    rw [ble_false (by rw [show u + (u + e) = e + (u + u) by lib_nat]; exact Nat.lt_add_of_pos_right (Nat.lt_of_lt_of_le hu (Nat.le_add_right u u)))]
    show (dv 2 (u + (u + e) - e), dv 2 (u + (u + e) + e)) = (u, u + e)
    rw [show u + (u + e) = e + (u + u) by lib_nat, sub_left, half,
      show e + (u + u) + e = (u + e) + (u + e) by lib_nat, half]

theorem tau_spec {a b n : Nat} (ha : 1 ≤ a) (h : a * a + b * b = n + n) :
    1 ≤ (tau a b).1 ∧ (tau a b).1 * (tau a b).1 + (tau a b).2 * (tau a b).2 = n ∧ sig (tau a b).1 (tau a b).2 = (a, b) := by
  cases Nat.lt_or_ge b a with
  | inr hg =>
    obtain ⟨e, he⟩ := LibHeckeRel.ex_add hg
    have he' : b = a + e := by rw [he]; lib_nat
    subst he'
    obtain ⟨w, hw⟩ := even_gap h
    subst hw
    unfold tau
    rw [ble_true hg, show a + (a + (w + w)) = (a + w) + (a + w) by lib_nat, half, sub_left, half]
    refine ⟨Nat.le_trans ha (Nat.le_add_right a w), ?_, ?_⟩
    · show (a + w) * (a + w) + w * w = n
      apply Nat.eq_of_mul_eq_mul_left (show 0 < 2 by decide)
      rw [show 2 * n = n + n by lib_nat, ← h]; lib_nat
    · show cond (Nat.blt w (a + w)) _ _ = (a, a + (w + w))
      rw [blt_true (by rw [show a + w = w + a by lib_nat]; exact Nat.lt_add_of_pos_right ha)]
      show (a + w - w, a + w + w) = (a, a + (w + w))
      rw [show a + w = w + a by lib_nat, sub_left, show w + a + w = a + (w + w) by lib_nat]
  | inl hl =>
    obtain ⟨e, he⟩ := LibHeckeRel.ex_add (Nat.le_of_lt hl)
    have he' : a = b + e := by rw [he]; lib_nat
    subst he'
    obtain ⟨w, hw⟩ := even_gap (a := b) (e := e) (n := n) (by rw [← h]; lib_nat)
    subst hw
    have w1 : 1 ≤ w := by
      cases w with
      | zero => rw [show b + (0 + 0) = b by lib_nat] at hl; exact absurd hl (Nat.lt_irrefl b)
      | succ w' => exact Nat.succ_le_succ (Nat.zero_le w')
    unfold tau
    rw [ble_false hl, sub_left, half, show b + (w + w) + b = (b + w) + (b + w) by lib_nat, half]
    refine ⟨w1, ?_, ?_⟩
    · show w * w + (b + w) * (b + w) = n
      apply Nat.eq_of_mul_eq_mul_left (show 0 < 2 by decide)
      rw [show 2 * n = n + n by lib_nat, ← h]; lib_nat
    · show cond (Nat.blt (b + w) w) _ _ = (b + (w + w), b)
      rw [blt_false (by rw [show b + w = w + b by lib_nat]; exact Nat.le_add_right w b)]
      show (w + (b + w), b + w - w) = (b + (w + w), b)
      rw [show b + w = w + b by lib_nat, sub_left, show w + (w + b) = b + (w + w) by lib_nat]

def P (n L i : Nat) : Bool := !(Nat.beq (dv L i) 0) && Nat.beq (dv L i * dv L i + md L i * md L i) n

theorem Qb_QP (n L : Nat) (hL : 0 < L) : Qb n L = S1 (fun i => cond (P n L i) 1 0) (L * L) := by
  unfold Qb
  rw [S1_ext _ (fun a => S1 (fun b => cond (!(Nat.beq a 0) && Nat.beq (a * a + b * b) n) 1 0) L) L (fun a _ => by
    cases e : Nat.beq a 0
    · rfl
    · show 0 = S1 (fun b => cond (!true && Nat.beq (a * a + b * b) n) 1 0) L
      exact ((S1_ext _ (fun _ => 0) L (fun _ _ => rfl)).trans (S1_zero_fn L)).symm)]
  exact S1_pair L hL _ L

theorem P_true {n L i : Nat} (h : P n L i = true) : 1 ≤ dv L i ∧ dv L i * dv L i + md L i * md L i = n := by
  unfold P at h
  cases e1 : Nat.beq (dv L i) 0 <;> cases e2 : Nat.beq (dv L i * dv L i + md L i * md L i) n <;> rw [e1, e2] at h
  · exact Bool.noConfusion h
  · exact ⟨Nat.pos_of_ne_zero (Nat.ne_of_beq_eq_false e1), Nat.eq_of_beq_eq_true e2⟩
  · exact Bool.noConfusion h
  · exact Bool.noConfusion h

theorem dec {L a b : Nat} (hb : b < L) : dv L (L * a + b) = a ∧ md L (L * a + b) = b := by
  have h := md_unique (n := L) (a := L * a + b) (q := a) (r := b) hb rfl
  exact ⟨h.2, h.1⟩

theorem P_mk {n L a b : Nat} (ha : 1 ≤ a) (h : a * a + b * b = n) (hb : b < L) : P n L (L * a + b) = true := by
  unfold P
  rw [(dec hb).1, (dec hb).2, LibTwoSqP3.beq_false_of_ne (Nat.ne_of_gt ha), h, LibMod.beq_self]
  rfl

theorem enc_lt {L a b : Nat} (ha : a < L) (hb : b < L) : L * a + b < L * L :=
  Nat.lt_of_lt_of_le (Nat.add_lt_add_left hb (L * a)) (by rw [show L * a + L = L * (a + 1) by lib_nat]; exact Nat.mul_le_mul_left L ha)

theorem lt_of_sq {x m : Nat} (h : x * x ≤ m) : x < m + 1 := Nat.lt_succ_of_le (Nat.le_trans (le_sq x) h)

theorem Q_two (n : Nat) : Q (n + n) = Q n := by
  rw [Q_Qb, Q_Qb, Qb_QP _ _ (Nat.zero_lt_succ _), Qb_QP _ _ (Nat.zero_lt_succ _)]
  symm
  refine count_bij _ _ (P n (n + 1)) (P (n + n) (n + n + 1))
    (fun i => (n + n + 1) * (sig (dv (n + 1) i) (md (n + 1) i)).1 + (sig (dv (n + 1) i) (md (n + 1) i)).2)
    (fun j => (n + 1) * (tau (dv (n + n + 1) j) (md (n + n + 1) j)).1 + (tau (dv (n + n + 1) j) (md (n + n + 1) j)).2)
    (fun i _ hp => ?_) (fun j _ hq => ?_) (fun j _ hq => ?_) (fun i _ hp => ?_)
  · obtain ⟨hu, hn⟩ := P_true hp
    obtain ⟨ha, hab, _⟩ := sig_spec hu hn
    have hA := lt_of_sq (Nat.le_trans (Nat.le_add_right _ _) (Nat.le_of_eq hab))
    have hB := lt_of_sq (Nat.le_trans (Nat.le_add_left _ _) (Nat.le_of_eq hab))
    exact ⟨enc_lt hA hB, P_mk ha hab hB⟩
  · obtain ⟨ha, hn⟩ := P_true hq
    obtain ⟨hu, huv, _⟩ := tau_spec ha hn
    have hU := lt_of_sq (Nat.le_trans (Nat.le_add_right _ _) (Nat.le_of_eq huv))
    have hV := lt_of_sq (Nat.le_trans (Nat.le_add_left _ _) (Nat.le_of_eq huv))
    exact ⟨enc_lt hU hV, P_mk hu huv hV⟩
  · obtain ⟨ha, hn⟩ := P_true hq
    obtain ⟨_, huv, hsig⟩ := tau_spec ha hn
    have hV := lt_of_sq (Nat.le_trans (Nat.le_add_left _ _) (Nat.le_of_eq huv))
    show (n + n + 1) * (sig (dv (n + 1) _) (md (n + 1) _)).1 + (sig (dv (n + 1) _) (md (n + 1) _)).2 = j
    rw [(dec hV).1, (dec hV).2, hsig]
    exact (spec (n + n + 1) j).symm
  · obtain ⟨hu, hn⟩ := P_true hp
    obtain ⟨_, hab, htau⟩ := sig_spec hu hn
    have hB := lt_of_sq (Nat.le_trans (Nat.le_add_left _ _) (Nat.le_of_eq hab))
    show (n + 1) * (tau (dv (n + n + 1) _) (md (n + n + 1) _)).1 + (tau (dv (n + n + 1) _) (md (n + n + 1) _)).2 = i
    rw [(dec hB).1, (dec hB).2, htau]
    exact (spec (n + 1) i).symm

end LibTwoSqP2

#print axioms LibTwoSqP2.sub_left
#print axioms LibTwoSqP2.half
#print axioms LibTwoSqP2.even_gap
#print axioms LibTwoSqP2.sig_spec
#print axioms LibTwoSqP2.tau_spec
#print axioms LibTwoSqP2.Qb_QP
#print axioms LibTwoSqP2.Q_two

lib/TwoSqP3.lean

lean_all/lib/TwoSqP3.lean

/-
  lib/TwoSqP3.lean — T46 shared library (G1515b, Jacobi's two-square formula): the prime p ≡ 3 (mod 4) part.

    inner, Qb      Qb n A = Σ_{1 ≤ a < A} #{b < A : a² + b² = n}
    cntB_S1, Q_Qb  Q n = Qb n (n + 1)
    Qb_ext         Qb n A = Qb n (n + 1 + B)   (larger ranges add nothing)
    Q_p3_scale     p = 4k+3 prime: Q(p²·m) = Q(m)
    Q_p3_zero      p = 4k+3 prime, p ∤ n: Q(p·n) = 0
  Lean 4 core only, proofs by hand, no axioms.
-/
import lib.SumScale
import lib.SumSqPrime

namespace LibTwoSqP3
open LibMod LibSumScale LibTwoSqCount

def inner (n a : Nat) (A : Nat) : Nat := S1 (fun b => cond (Nat.beq (a * a + b * b) n) 1 0) A

def Qb (n A : Nat) : Nat := S1 (fun a => cond (Nat.beq a 0) 0 (inner n a A)) A

theorem cntB_S1 (n a : Nat) : ∀ B, cntB n a B = inner n a B
  | 0 => rfl
  | b + 1 => by
    show cntB n a b + _ = inner n a b + _
    rw [cntB_S1 n a b]

theorem cntA_S1 (n : Nat) : ∀ A, cntA n A = S1 (fun a => cond (Nat.beq a 0) 0 (inner n a (n + 1))) (A + 1)
  | 0 => rfl
  | A + 1 => by
    show cntA n A + cntB n (A + 1) (n + 1) = S1 _ (A + 1) + cond (Nat.beq (A + 1) 0) 0 (inner n (A + 1) (n + 1))
    rw [cntA_S1 n A, cntB_S1]
    rfl

theorem Q_Qb (n : Nat) : Q n = Qb n (n + 1) := cntA_S1 n n

theorem sq_big {a b n : Nat} (h : n + 1 ≤ a) : a * a + b * b ≠ n := fun e => by
  have h1 : a ≤ a * a := by
    cases a with
    | zero => exact Nat.le_refl 0
    | succ a' => exact Nat.le_mul_of_pos_left _ (Nat.zero_lt_succ a')
  have h2 : a * a ≤ a * a + b * b := Nat.le_add_right _ _
  rw [e] at h2
  exact Nat.lt_irrefl n (Nat.lt_of_lt_of_le (Nat.lt_of_lt_of_le (Nat.lt_succ_self n) h) (Nat.le_trans h1 h2))

theorem beq_false_of_ne {x y : Nat} (h : x ≠ y) : Nat.beq x y = false := by
  cases e : Nat.beq x y
  · rfl
  · exact absurd (Nat.eq_of_beq_eq_true e) h

theorem inner_ext (n a B : Nat) : inner n a (n + 1 + B) = inner n a (n + 1) :=
  S1_zero_tail _ (n + 1) B (fun j _ => by
    show cond (Nat.beq (a * a + (n + 1 + j) * (n + 1 + j)) n) 1 0 = 0
    rw [beq_false_of_ne (fun e => sq_big (a := n + 1 + j) (b := a) (n := n) (Nat.le_add_right _ _)
      (by rw [Nat.add_comm]; exact e))]
    rfl)

theorem inner_big {n a : Nat} (h : n + 1 ≤ a) : ∀ B, inner n a B = 0
  | 0 => rfl
  | B + 1 => by
    show inner n a B + cond (Nat.beq (a * a + B * B) n) 1 0 = 0
    rw [inner_big h B, beq_false_of_ne (sq_big h)]
    rfl

theorem Qb_ext (n B : Nat) : Qb n (n + 1 + B) = Qb n (n + 1) := by
  unfold Qb
  rw [S1_zero_tail _ (n + 1) B (fun j _ => by
    show cond (Nat.beq (n + 1 + j) 0) 0 (inner n (n + 1 + j) (n + 1 + B)) = 0
    rw [inner_big (Nat.le_add_right _ _)]
    cases Nat.beq (n + 1 + j) 0 <;> rfl)]
  exact S1_ext _ _ _ (fun a _ => by
    show cond (Nat.beq a 0) 0 (inner n a (n + 1 + B)) = cond (Nat.beq a 0) 0 (inner n a (n + 1))
    rw [inner_ext])

theorem inner_zero_of {n a : Nat} (h : ∀ b, a * a + b * b ≠ n) : ∀ B, inner n a B = 0
  | 0 => rfl
  | B + 1 => by
    show inner n a B + cond (Nat.beq (a * a + B * B) n) 1 0 = 0
    rw [inner_zero_of h B, beq_false_of_ne (h B)]
    rfl

theorem both_dvd (k : Nat) (hp : LibZp.Prime1 (4 * k + 2)) {a b : Nat} (h : 4 * k + 2 + 1 ∣ a * a + b * b) :
    4 * k + 2 + 1 ∣ a ∧ 4 * k + 2 + 1 ∣ b :=
  ⟨LibSumSqPrime.p3_dvd k hp h, LibSumSqPrime.p3_dvd k hp (by rw [show b * b + a * a = a * a + b * b by lib_nat]; exact h)⟩

theorem beq_cancel {P X Y : Nat} (hP : 0 < P) : Nat.beq (P * P * X) (P * P * Y) = Nat.beq X Y := by
  cases e : Nat.beq X Y
  · exact beq_false_of_ne (fun h => by
      have := Nat.eq_of_mul_eq_mul_left (Nat.mul_pos hP hP) h
      rw [this] at e
      exact absurd e (by rw [LibMod.beq_self]; exact fun h => Bool.noConfusion h))
  · rw [Nat.eq_of_beq_eq_true e]; exact LibMod.beq_self _

theorem Q_p3_scale (k : Nat) (hp : LibZp.Prime1 (4 * k + 2)) (m : Nat) :
    Q ((4 * k + 2 + 1) * (4 * k + 2 + 1) * m) = Q m := by
  have hP : 0 < 4 * k + 2 + 1 := Nat.zero_lt_succ _
  generalize hPdef : 4 * k + 2 + 1 = P at hP
  have hpP : LibZp.Prime1 (4 * k + 2) := hp
  have dv : ∀ {a b : Nat}, P ∣ a * a + b * b → P ∣ a ∧ P ∣ b := fun h => by
    rw [← hPdef] at h ⊢; exact both_dvd k hpP h
  -- ranges
  obtain ⟨B1, hB1⟩ := LibHeckeRel.ex_add (show P * P * m + 1 ≤ P * (P * m + 1) by
    rw [show P * (P * m + 1) = P * P * m + P by lib_nat]; exact Nat.add_le_add_left hP _)
  obtain ⟨q, hq⟩ := LibHeckeRel.ex_add hP
  obtain ⟨B2, hB2⟩ := LibHeckeRel.ex_add (show m + 1 ≤ P * m + 1 from
    Nat.succ_le_succ (by rw [hq, show (q + 1) * m = q * m + m by lib_nat]; exact Nat.le_add_left m (q * m)))
  rw [Q_Qb, Q_Qb, ← Qb_ext (P * P * m) B1, ← Qb_ext m B2,
    show P * P * m + 1 + B1 = P * (P * m + 1) by rw [hB1]; lib_nat,
    show m + 1 + B2 = P * m + 1 by rw [hB2]; lib_nat]
  unfold Qb
  rw [S1_scale hP _ (fun a ha => by
    show cond (Nat.beq a 0) 0 (inner (P * P * m) a (P * (P * m + 1))) = 0
    rw [inner_zero_of (fun b e => ha (dv (by rw [e]; exact ⟨P * m, by lib_nat⟩)).1)]
    cases Nat.beq a 0 <;> rfl) (P * m + 1)]
  refine S1_ext _ _ _ (fun a' _ => ?_)
  show cond (Nat.beq (P * a') 0) 0 (inner (P * P * m) (P * a') (P * (P * m + 1)))
    = cond (Nat.beq a' 0) 0 (inner m a' (P * m + 1))
  have hz : Nat.beq (P * a') 0 = Nat.beq a' 0 := by
    cases a' with
    | zero => rfl
    | succ a'' =>
      rw [beq_false_of_ne (Nat.ne_of_gt (Nat.mul_pos hP (Nat.zero_lt_succ a''))),
        beq_false_of_ne (Nat.ne_of_gt (Nat.zero_lt_succ a''))]
  rw [hz]
  have hin : inner (P * P * m) (P * a') (P * (P * m + 1)) = inner m a' (P * m + 1) := by
    unfold inner
    rw [S1_scale hP _ (fun b hb => by
      show cond (Nat.beq (P * a' * (P * a') + b * b) (P * P * m)) 1 0 = 0
      rw [beq_false_of_ne (fun e => hb (dv (by rw [e]; exact ⟨P * m, by lib_nat⟩)).2)]
      rfl) (P * m + 1)]
    exact S1_ext _ _ _ (fun b' _ => by
      show cond (Nat.beq (P * a' * (P * a') + P * b' * (P * b')) (P * P * m)) 1 0
        = cond (Nat.beq (a' * a' + b' * b') m) 1 0
      rw [show P * a' * (P * a') + P * b' * (P * b') = P * P * (a' * a' + b' * b') by lib_nat, beq_cancel hP])
  rw [hin]

theorem Q_p3_zero (k : Nat) (hp : LibZp.Prime1 (4 * k + 2)) {n : Nat} (hn : ¬ 4 * k + 2 + 1 ∣ n) :
    Q ((4 * k + 2 + 1) * n) = 0 := by
  have hP : 0 < 4 * k + 2 + 1 := Nat.zero_lt_succ _
  rw [Q_Qb]
  unfold Qb
  have : ∀ A a, cond (Nat.beq a 0) 0 (inner ((4 * k + 2 + 1) * n) a A) = 0 := fun A a => by
    rw [inner_zero_of (fun b e => by
      obtain ⟨⟨a', ha'⟩, ⟨b', hb'⟩⟩ := both_dvd k hp (show 4 * k + 2 + 1 ∣ a * a + b * b by rw [e]; exact ⟨n, rfl⟩)
      apply hn
      refine ⟨a' * a' + b' * b', Nat.eq_of_mul_eq_mul_left hP ?_⟩
      rw [← e, ha', hb']; lib_nat)]
    cases Nat.beq a 0 <;> rfl
  generalize (4 * k + 2 + 1) * n + 1 = A
  exact (S1_ext _ (fun _ => 0) A (fun a _ => this A a)).trans (by
    induction A with
    | zero => rfl
    | succ A ih => show S1 (fun _ => 0) A + 0 = 0; rw [ih])

end LibTwoSqP3

#print axioms LibTwoSqP3.cntB_S1
#print axioms LibTwoSqP3.cntA_S1
#print axioms LibTwoSqP3.Q_Qb
#print axioms LibTwoSqP3.sq_big
#print axioms LibTwoSqP3.inner_ext
#print axioms LibTwoSqP3.inner_big
#print axioms LibTwoSqP3.Qb_ext
#print axioms LibTwoSqP3.inner_zero_of
#print axioms LibTwoSqP3.both_dvd
#print axioms LibTwoSqP3.beq_cancel
#print axioms LibTwoSqP3.Q_p3_scale
#print axioms LibTwoSqP3.Q_p3_zero

lib/TwoSqPi.lean

lean_all/lib/TwoSqPi.lean

/-
  lib/TwoSqPi.lean — T46 shared library (G1515d3, Jacobi's two-square formula): multiplication by π = u + vi, p = u² + v².

  Points (a, b) of the quarter plane (a ≥ 1, b ≥ 0) stand for the Gaussian integers a + bi up to units.
    sigp (c, d)   normalized π·(c + di):  (uc − vd, ud + vc) if vd < uc,  (ud + vc, vd − uc) otherwise
    taup (a, b)   normalized (a + bi)·π̄ / p
    inA a b       π ∣ a + bi:   p ∣ au + bv,  p ∣ bu − av,  p ∣ av − bu   (Nat truncated differences)
    sigp_spec     c ≥ 1, c² + d² = n ⇒ sigp(c, d) has a ≥ 1, a² + b² = pn, lies in inA, and taup(sigp(c, d)) = (c, d)
    pos_of_mul    1 ≤ x = p·c ⇒ 1 ≤ c
    taup_spec     u ≥ 1, a ≥ 1, a² + b² = pn, inA a b ⇒ taup(a, b) = (c, d) has c ≥ 1, c² + d² = n, sigp(c, d) = (a, b)
  Polynomial identities are proved in ℤ (lib_int) and pulled back with Int.ofNat.inj.
  Lean 4 core only, proofs by hand, no axioms.
-/
import lib.TwoSqP2

namespace LibTwoSqPi
open LibMod LibTwoSqP2

theorem nat_eq {x y : Nat} (h : (x : Int) = (y : Int)) : x = y := Int.ofNat.inj h

theorem sub_zero_le : ∀ x k : Nat, x - (x + k) = 0
  | 0, k => by rw [Nat.zero_add]; exact Nat.zero_sub k
  | x + 1, k => by rw [show x + 1 + k = (x + k) + 1 by lib_nat, Nat.succ_sub_succ]; exact sub_zero_le x k

theorem dv_mul {p k : Nat} (hp : 0 < p) : dv p (p * k) = k :=
  (md_unique (n := p) (a := p * k) (q := k) (r := 0) hp rfl).2

theorem cancel_p {p x y : Nat} (hp : 0 < p) (h : p * x = p * y) : x = y := Nat.eq_of_mul_eq_mul_left hp h

def sigp (u v c d : Nat) : Nat × Nat := cond (Nat.blt (v * d) (u * c)) (u * c - v * d, u * d + v * c) (u * d + v * c, v * d - u * c)

def taup (u v p a b : Nat) : Nat × Nat :=
  cond (Nat.ble (a * v) (b * u)) (dv p (a * u + b * v), dv p (b * u - a * v)) (dv p (a * v - b * u), dv p (a * u + b * v))

def inA (u v p a b : Nat) : Prop := p ∣ a * u + b * v ∧ p ∣ b * u - a * v ∧ p ∣ a * v - b * u

theorem sigp_spec {u v p c d n : Nat} (hp : u * u + v * v = p) (hp0 : 0 < p) (hv : 1 ≤ v) (hc : 1 ≤ c)
    (hn : c * c + d * d = n) :
    1 ≤ (sigp u v c d).1 ∧ (sigp u v c d).1 * (sigp u v c d).1 + (sigp u v c d).2 * (sigp u v c d).2 = p * n
      ∧ inA u v p (sigp u v c d).1 (sigp u v c d).2 ∧ taup u v p (sigp u v c d).1 (sigp u v c d).2 = (c, d) := by
  subst hp; subst hn
  cases Nat.lt_or_ge (v * d) (u * c) with
  | inl hl =>
    obtain ⟨e, he⟩ := LibHeckeRel.ex_add (Nat.le_of_lt hl)
    have H : u * c = v * d + e := by rw [he]; lib_nat
    have e1 : 1 ≤ e := by
      cases e with
      | zero => rw [Nat.add_zero] at H; rw [H] at hl; exact absurd hl (Nat.lt_irrefl _)
      | succ e' => exact Nat.succ_le_succ (Nat.zero_le e')
    have HI : (e : Int) = (u : Int) * c - (v : Int) * d := by
      have h' : ((u * c : Nat) : Int) = ((v * d + e : Nat) : Int) := congrArg _ H
      show (e : Int) = ((u * c : Nat) : Int) - ((v * d : Nat) : Int)
      rw [h']; show (e : Int) = ((v * d : Nat) : Int) + (e : Int) - ((v * d : Nat) : Int); lib_int
    unfold sigp
    rw [blt_true hl, H, sub_left]
    show 1 ≤ e ∧ e * e + (u * d + v * c) * (u * d + v * c) = (u * u + v * v) * (c * c + d * d)
      ∧ inA u v (u * u + v * v) e (u * d + v * c) ∧ taup u v (u * u + v * v) e (u * d + v * c) = (c, d)
    have i2 : e * u + (u * d + v * c) * v = (u * u + v * v) * c :=
      nat_eq (by show (e : Int) * u + ((u : Int) * d + (v : Int) * c) * v = ((u : Int) * u + (v : Int) * v) * c
                 rw [HI]; lib_int)
    have i3 : (u * d + v * c) * u = e * v + (u * u + v * v) * d :=
      nat_eq (by show ((u : Int) * d + (v : Int) * c) * u = (e : Int) * v + ((u : Int) * u + (v : Int) * v) * d
                 rw [HI]; lib_int)
    refine ⟨e1, nat_eq (by
        show (e : Int) * e + ((u : Int) * d + (v : Int) * c) * ((u : Int) * d + (v : Int) * c)
          = ((u : Int) * u + (v : Int) * v) * ((c : Int) * c + (d : Int) * d)
        rw [HI]; lib_int), ⟨⟨c, by rw [i2] <;> lib_nat⟩, ⟨d, by rw [i3, sub_left] <;> lib_nat⟩, ⟨0, by rw [i3, sub_zero_le] <;> lib_nat⟩⟩, ?_⟩
    unfold taup
    rw [ble_true (by rw [i3]; exact Nat.le_add_right _ _), i2, i3, sub_left, dv_mul hp0, dv_mul hp0]
    rfl
  | inr hg =>
    obtain ⟨f, hf⟩ := LibHeckeRel.ex_add hg
    have H : v * d = u * c + f := by rw [hf]; lib_nat
    have HI : (f : Int) = (v : Int) * d - (u : Int) * c := by
      have h' : ((v * d : Nat) : Int) = ((u * c + f : Nat) : Int) := congrArg _ H
      show (f : Int) = ((v * d : Nat) : Int) - ((u * c : Nat) : Int)
      rw [h']; show (f : Int) = ((u * c : Nat) : Int) + (f : Int) - ((u * c : Nat) : Int); lib_int
    unfold sigp
    rw [blt_false hg, H, sub_left]
    show 1 ≤ u * d + v * c ∧ (u * d + v * c) * (u * d + v * c) + f * f = (u * u + v * v) * (c * c + d * d)
      ∧ inA u v (u * u + v * v) (u * d + v * c) f ∧ taup u v (u * u + v * v) (u * d + v * c) f = (c, d)
    have i2 : (u * d + v * c) * u + f * v = (u * u + v * v) * d :=
      nat_eq (by show ((u : Int) * d + (v : Int) * c) * u + (f : Int) * v = ((u : Int) * u + (v : Int) * v) * d
                 rw [HI]; lib_int)
    have i3 : (u * d + v * c) * v = f * u + (u * u + v * v) * c :=
      nat_eq (by show ((u : Int) * d + (v : Int) * c) * v = (f : Int) * u + ((u : Int) * u + (v : Int) * v) * c
                 rw [HI]; lib_int)
    have hcp : 1 ≤ (u * u + v * v) * c := Nat.le_trans hc (Nat.le_mul_of_pos_left c hp0)
    refine ⟨Nat.le_trans (Nat.le_trans hc (Nat.le_mul_of_pos_left c hv)) (Nat.le_add_left _ _), nat_eq (by
        show ((u : Int) * d + (v : Int) * c) * ((u : Int) * d + (v : Int) * c) + (f : Int) * f
          = ((u : Int) * u + (v : Int) * v) * ((c : Int) * c + (d : Int) * d)
        rw [HI]; lib_int), ⟨⟨d, by rw [i2] <;> lib_nat⟩, ⟨0, by rw [i3, sub_zero_le] <;> lib_nat⟩, ⟨c, by rw [i3, sub_left] <;> lib_nat⟩⟩, ?_⟩
    unfold taup
    rw [ble_false (by rw [i3]; exact Nat.lt_of_lt_of_le (Nat.lt_succ_self _) (Nat.add_le_add_left hcp _)), i3, sub_left, i2,
      dv_mul hp0, dv_mul hp0]
    rfl

theorem pos_of_mul {p c x : Nat} (h1 : 1 ≤ x) (h : x = p * c) : 1 ≤ c := by
  cases c with
  | zero => rw [Nat.mul_zero] at h; rw [h] at h1; exact absurd h1 (Nat.not_succ_le_zero 0)
  | succ c' => exact Nat.succ_le_succ (Nat.zero_le c')

theorem taup_spec {u v p a b n : Nat} (hp : u * u + v * v = p) (hp0 : 0 < p) (hu : 1 ≤ u) (ha : 1 ≤ a)
    (hab : a * a + b * b = p * n) (hA : inA u v p a b) :
    1 ≤ (taup u v p a b).1 ∧ (taup u v p a b).1 * (taup u v p a b).1 + (taup u v p a b).2 * (taup u v p a b).2 = n
      ∧ sigp u v (taup u v p a b).1 (taup u v p a b).2 = (a, b) := by
  have hpI : (p : Int) = (u : Int) * u + (v : Int) * v := congrArg (fun x : Nat => (x : Int)) hp.symm
  have habI : (a : Int) * a + (b : Int) * b = (p : Int) * n := congrArg (fun x : Nat => (x : Int)) hab
  obtain ⟨⟨s, hs⟩, ⟨t1, ht1⟩, ⟨t2, ht2⟩⟩ := hA
  have hsI : (p : Int) * s = (a : Int) * u + (b : Int) * v := congrArg (fun x : Nat => (x : Int)) hs.symm
  cases Nat.lt_or_ge (b * u) (a * v) with
  | inr hle =>
    -- a v ≤ b u : c = s, d = t1
    obtain ⟨e, he⟩ := LibHeckeRel.ex_add hle
    have H : b * u = a * v + e := by rw [he]; lib_nat
    have hte : e = p * t1 := by rw [← ht1, H, sub_left]
    have HI : (p : Int) * t1 + (a : Int) * v = (b : Int) * u := by
      have h' : ((b * u : Nat) : Int) = ((a * v + p * t1 : Nat) : Int) := congrArg _ (by rw [H, hte])
      show ((p * t1 : Nat) : Int) + ((a * v : Nat) : Int) = ((b * u : Nat) : Int)
      rw [h']; show ((p * t1 : Nat) : Int) + ((a * v : Nat) : Int) = ((a * v : Nat) : Int) + ((p * t1 : Nat) : Int); lib_int
    have key : taup u v p a b = (s, t1) := by
      unfold taup; rw [ble_true hle, hs, ht1, dv_mul hp0, dv_mul hp0]; rfl
    rw [key]
    show 1 ≤ s ∧ s * s + t1 * t1 = n ∧ sigp u v s t1 = (a, b)
    have i1 : u * s = v * t1 + a := cancel_p hp0 (nat_eq (by
      show (p : Int) * ((u : Int) * s) = (p : Int) * ((v : Int) * t1 + a)
      have e1 : (p : Int) * ((u : Int) * s) = (u : Int) * ((p : Int) * s) := by lib_int
      have e2 : (p : Int) * ((v : Int) * t1 + a) = (v : Int) * ((p : Int) * t1 + (a : Int) * v) + (p : Int) * a - (v : Int) * ((a : Int) * v) := by lib_int
      rw [e1, e2, hsI, HI, hpI]; lib_int))
    have i2 : u * t1 + v * s = b := cancel_p hp0 (nat_eq (by
      show (p : Int) * ((u : Int) * t1 + (v : Int) * s) = (p : Int) * b
      have e1 : (p : Int) * ((u : Int) * t1 + (v : Int) * s) = (u : Int) * ((p : Int) * t1 + (a : Int) * v) - (u : Int) * ((a : Int) * v) + (v : Int) * ((p : Int) * s) := by lib_int
      rw [e1, hsI, HI, hpI]; lib_int))
    have i3 : s * s + t1 * t1 = n := cancel_p hp0 (cancel_p hp0 (nat_eq (by
      show (p : Int) * ((p : Int) * ((s : Int) * s + (t1 : Int) * t1)) = (p : Int) * ((p : Int) * n)
      have e1 : (p : Int) * ((p : Int) * ((s : Int) * s + (t1 : Int) * t1))
          = ((p : Int) * s) * ((p : Int) * s) + ((p : Int) * t1 + (a : Int) * v - (a : Int) * v) * ((p : Int) * t1 + (a : Int) * v - (a : Int) * v) := by lib_int
      have e2 : (p : Int) * ((p : Int) * n) = (p : Int) * ((a : Int) * a + (b : Int) * b) := by rw [habI]
      rw [e1, e2, hsI, HI, hpI]; lib_int)))
    have hs1 : 1 ≤ s := pos_of_mul (Nat.le_trans (Nat.mul_le_mul ha hu) (Nat.le_add_right _ _)) hs
    refine ⟨hs1, i3, ?_⟩
    have hlt : v * t1 < u * s := by rw [i1]; exact Nat.lt_of_lt_of_le (Nat.lt_succ_self _) (Nat.add_le_add_left ha _)
    unfold sigp; rw [blt_true hlt, i1, sub_left, i2]; rfl
  | inl hlt =>
    -- b u < a v : c = t2, d = s
    obtain ⟨e, he⟩ := LibHeckeRel.ex_add (Nat.le_of_lt hlt)
    have H : a * v = b * u + e := by rw [he]; lib_nat
    have hte : e = p * t2 := by rw [← ht2, H, sub_left]
    have e0 : 1 ≤ e := by
      cases e with
      | zero => rw [Nat.add_zero] at H; rw [H] at hlt; exact absurd hlt (Nat.lt_irrefl _)
      | succ e' => exact Nat.succ_le_succ (Nat.zero_le e')
    have HI : (p : Int) * t2 + (b : Int) * u = (a : Int) * v := by
      have h' : ((a * v : Nat) : Int) = ((b * u + p * t2 : Nat) : Int) := congrArg _ (by rw [H, hte])
      show ((p * t2 : Nat) : Int) + ((b * u : Nat) : Int) = ((a * v : Nat) : Int)
      rw [h']; show ((p * t2 : Nat) : Int) + ((b * u : Nat) : Int) = ((b * u : Nat) : Int) + ((p * t2 : Nat) : Int); lib_int
    have key : taup u v p a b = (t2, s) := by
      unfold taup; rw [ble_false hlt, hs, ht2, dv_mul hp0, dv_mul hp0]; rfl
    rw [key]
    show 1 ≤ t2 ∧ t2 * t2 + s * s = n ∧ sigp u v t2 s = (a, b)
    have i1 : v * s = u * t2 + b := cancel_p hp0 (nat_eq (by
      show (p : Int) * ((v : Int) * s) = (p : Int) * ((u : Int) * t2 + b)
      have e1 : (p : Int) * ((v : Int) * s) = (v : Int) * ((p : Int) * s) := by lib_int
      have e2 : (p : Int) * ((u : Int) * t2 + b) = (u : Int) * ((p : Int) * t2 + (b : Int) * u) + (p : Int) * b - (u : Int) * ((b : Int) * u) := by lib_int
      rw [e1, e2, hsI, HI, hpI]; lib_int))
    have i2 : u * s + v * t2 = a := cancel_p hp0 (nat_eq (by
      show (p : Int) * ((u : Int) * s + (v : Int) * t2) = (p : Int) * a
      have e1 : (p : Int) * ((u : Int) * s + (v : Int) * t2) = (u : Int) * ((p : Int) * s) + (v : Int) * ((p : Int) * t2 + (b : Int) * u) - (v : Int) * ((b : Int) * u) := by lib_int
      rw [e1, hsI, HI, hpI]; lib_int))
    have i3 : t2 * t2 + s * s = n := cancel_p hp0 (cancel_p hp0 (nat_eq (by
      show (p : Int) * ((p : Int) * ((t2 : Int) * t2 + (s : Int) * s)) = (p : Int) * ((p : Int) * n)
      have e1 : (p : Int) * ((p : Int) * ((t2 : Int) * t2 + (s : Int) * s))
          = ((p : Int) * t2 + (b : Int) * u - (b : Int) * u) * ((p : Int) * t2 + (b : Int) * u - (b : Int) * u) + ((p : Int) * s) * ((p : Int) * s) := by lib_int
      have e2 : (p : Int) * ((p : Int) * n) = (p : Int) * ((a : Int) * a + (b : Int) * b) := by rw [habI]
      rw [e1, e2, hsI, HI, hpI]; lib_int)))
    refine ⟨pos_of_mul e0 hte, i3, ?_⟩
    have hge : u * t2 ≤ v * s := by rw [i1]; exact Nat.le_add_right _ _
    unfold sigp; rw [blt_false hge, i1, sub_left, i2]
    rfl

end LibTwoSqPi

#print axioms LibTwoSqPi.sub_zero_le
#print axioms LibTwoSqPi.sigp_spec
#print axioms LibTwoSqPi.pos_of_mul
#print axioms LibTwoSqPi.taup_spec

lib/TwoSqPiCount.lean

lean_all/lib/TwoSqPiCount.lean

/-
  lib/TwoSqPiCount.lean — T46 shared library (G1515d3, Jacobi's two-square formula): divisibility by π = u + vi and π' = v + ui.

  p = u² + v² prime, u, v ≥ 1, p ∤ 2.  inA u v p a b says π ∣ a + bi; inA v u p a b says π' ∣ a + bi (π' = i·π̄).
    lt_p, ndvd_u       u < p and p ∤ u
    cover              a² + b² = p·n  ⇒  p ∣ au + bv  ∨  p ∣ av + bu
    inA_of_first       p ∣ au + bv, p ∤ u  ⇒  inA u v p a b
    inA_or             a² + b² = p·n  ⇒  inA u v p a b ∨ inA v u p a b
    both_dvd           inA u v p a b ∧ inA v u p a b  ⇒  p ∣ a ∧ p ∣ b
    inA_of_dvd         p ∣ a ∧ p ∣ b  ⇒  inA u v p a b
  Lean 4 core only, proofs by hand, no axioms.
-/
import lib.TwoSqPi
import lib.Gcd
import lib.FermatExp
import lib.TwoSqUniq

namespace LibTwoSqPiCount
open LibMod LibTwoSqP2 LibTwoSqPi

theorem lt_p {u v p : Nat} (hp : u * u + v * v = p) (hv : 1 ≤ v) : u < p := by
  rw [← hp]
  exact Nat.lt_of_le_of_lt (le_sq u) (Nat.lt_of_lt_of_le (Nat.lt_succ_self _) (Nat.add_le_add_left (Nat.mul_le_mul hv hv) _))

theorem ndvd_u {u v p : Nat} (hp : u * u + v * v = p) (hu : 1 ≤ u) (hv : 1 ≤ v) : ¬ p ∣ u := fun h => by
  have hl := lt_p hp hv
  have := LibTwoSqUniq.eq_of_dvd_le h hu (Nat.le_of_lt hl)
  rw [this] at hl; exact Nat.lt_irrefl p hl

theorem cover {u v p a b n : Nat} (hp : u * u + v * v = p) (hP : ∀ d, d ∣ p → d = 1 ∨ d = p)
    (hab : a * a + b * b = p * n) : p ∣ a * u + b * v ∨ p ∣ a * v + b * u := by
  apply LibGcd.euclid_prime hP
  refine ⟨u * v * n + a * b, ?_⟩
  have e : (a * u + b * v) * (a * v + b * u) = u * v * (a * a + b * b) + a * b * (u * u + v * v) := by lib_nat
  rw [e, hab, hp]; lib_nat

theorem inA_of_first {u v p a b : Nat} (hp : u * u + v * v = p) (hP : ∀ d, d ∣ p → d = 1 ∨ d = p)
    (hp0 : 0 < p) (hdu : ¬ p ∣ u) (h1 : p ∣ a * u + b * v) : inA u v p a b := by
  have hbp : p ∣ b * (u * u + v * v) := ⟨b, by rw [hp]; lib_nat⟩
  have hX : p ∣ v * (a * u + b * v) := by rw [Nat.mul_comm]; exact LibGcd.dvd_mul_right' h1 v
  refine ⟨h1, ?_⟩
  cases Nat.lt_or_ge (b * u) (a * v) with
  | inr hle =>
    obtain ⟨e, he⟩ := LibHeckeRel.ex_add hle
    have H : b * u = a * v + e := by rw [he]; lib_nat
    have id : u * e + v * (a * u + b * v) = b * (u * u + v * v) := by
      rw [show b * (u * u + v * v) = (b * u) * u + b * v * v by lib_nat, H]; lib_nat
    have hue : p ∣ u * e := LibFermatExp.dvd_of_add p _ _ hp0 (by rw [id]; exact hbp) hX
    have he' : p ∣ e := (LibGcd.euclid_prime hP hue).resolve_left hdu
    rw [H, sub_left, sub_zero_le]
    exact ⟨he', LibGcd.dvd_zero' p⟩
  | inl hlt =>
    obtain ⟨e, he⟩ := LibHeckeRel.ex_add (Nat.le_of_lt hlt)
    have H : a * v = b * u + e := by rw [he]; lib_nat
    have id : u * e + b * (u * u + v * v) = v * (a * u + b * v) := by
      rw [show v * (a * u + b * v) = (a * v) * u + b * v * v by lib_nat, H]; lib_nat
    have hue : p ∣ u * e := LibFermatExp.dvd_of_add p _ _ hp0 (by rw [id]; exact hX) hbp
    have he' : p ∣ e := (LibGcd.euclid_prime hP hue).resolve_left hdu
    rw [H, sub_left, sub_zero_le]
    exact ⟨LibGcd.dvd_zero' p, he'⟩

theorem inA_or {u v p a b n : Nat} (hp : u * u + v * v = p) (hP : ∀ d, d ∣ p → d = 1 ∨ d = p)
    (hu : 1 ≤ u) (hv : 1 ≤ v) (hab : a * a + b * b = p * n) : inA u v p a b ∨ inA v u p a b := by
  have hp0 : 0 < p := Nat.lt_of_le_of_lt (Nat.zero_le u) (lt_p hp hv)
  have hp' : v * v + u * u = p := by rw [← hp]; lib_nat
  cases cover hp hP hab with
  | inl h => exact Or.inl (inA_of_first hp hP hp0 (ndvd_u hp hu hv) h)
  | inr h => exact Or.inr (inA_of_first hp' hP hp0 (ndvd_u hp' hv hu) h)

theorem both_aux {p x y : Nat} (hP : ∀ d, d ∣ p → d = 1 ∨ d = p) (hp0 : 0 < p) (h2 : ¬ p ∣ 2)
    (h1 : p ∣ x + y) (he : ∀ e, x = y + e → p ∣ e) (hle : y ≤ x) : p ∣ x ∧ p ∣ y := by
  obtain ⟨e, hex⟩ := LibHeckeRel.ex_add hle
  have H : x = y + e := by rw [hex]; lib_nat
  have hpe := he e H
  have hyy : p ∣ 2 * y := LibFermatExp.dvd_of_add p _ e hp0 (by rw [show 2 * y + e = x + y by rw [H]; lib_nat]; exact h1) hpe
  have hy : p ∣ y := (LibGcd.euclid_prime hP hyy).resolve_left h2
  exact ⟨by rw [H]; exact LibGcd.dvd_add' hy hpe, hy⟩

theorem both_dvd {u v p a b : Nat} (hp : u * u + v * v = p) (hP : ∀ d, d ∣ p → d = 1 ∨ d = p) (h2 : ¬ p ∣ 2)
    (hu : 1 ≤ u) (hv : 1 ≤ v) (hA : inA u v p a b) (hB : inA v u p a b) : p ∣ a ∧ p ∣ b := by
  have hp0 : 0 < p := Nat.lt_of_le_of_lt (Nat.zero_le u) (lt_p hp hv)
  have hp' : v * v + u * u = p := by rw [← hp]; lib_nat
  obtain ⟨h1, _, _⟩ := hA
  obtain ⟨_, hB2, hB3⟩ := hB
  -- hB2 : p ∣ b * v - a * u,  hB3 : p ∣ a * u - b * v
  have xy : p ∣ a * u ∧ p ∣ b * v := by
    cases Nat.lt_or_ge (a * u) (b * v) with
    | inr hle => exact both_aux hP hp0 h2 h1 (fun e H => by rw [H, sub_left] at hB3; exact hB3) hle
    | inl hlt =>
      have r := both_aux hP hp0 h2 (x := b * v) (y := a * u) (by rw [show b * v + a * u = a * u + b * v by lib_nat]; exact h1)
        (fun e H => by rw [H, sub_left] at hB2; exact hB2) (Nat.le_of_lt hlt)
      exact ⟨r.2, r.1⟩
  exact ⟨(LibGcd.euclid_prime hP xy.1).resolve_right (ndvd_u hp hu hv),
    (LibGcd.euclid_prime hP xy.2).resolve_right (ndvd_u hp' hv hu)⟩

theorem dvd_sub_mul (p x y : Nat) : p ∣ p * x - p * y := by
  cases Nat.lt_or_ge x y with
  | inl hlt =>
    obtain ⟨k, hk⟩ := LibHeckeRel.ex_add (Nat.le_of_lt hlt)
    rw [show p * y = p * x + p * k by rw [hk]; lib_nat, sub_zero_le]; exact LibGcd.dvd_zero' p
  | inr hle =>
    obtain ⟨k, hk⟩ := LibHeckeRel.ex_add hle
    rw [show p * x = p * y + p * k by rw [hk]; lib_nat, sub_left]; exact ⟨k, rfl⟩

theorem inA_of_dvd {u v p a b : Nat} (ha : p ∣ a) (hb : p ∣ b) : inA u v p a b := by
  obtain ⟨a', ha'⟩ := ha
  obtain ⟨b', hb'⟩ := hb
  subst ha'; subst hb'
  refine ⟨⟨a' * u + b' * v, by lib_nat⟩, ?_, ?_⟩
  · rw [show p * b' * u = p * (b' * u) by lib_nat, show p * a' * v = p * (a' * v) by lib_nat]; exact dvd_sub_mul p _ _
  · rw [show p * a' * v = p * (a' * v) by lib_nat, show p * b' * u = p * (b' * u) by lib_nat]; exact dvd_sub_mul p _ _

end LibTwoSqPiCount

#print axioms LibTwoSqPiCount.lt_p
#print axioms LibTwoSqPiCount.ndvd_u
#print axioms LibTwoSqPiCount.cover
#print axioms LibTwoSqPiCount.inA_of_first
#print axioms LibTwoSqPiCount.inA_or
#print axioms LibTwoSqPiCount.both_aux
#print axioms LibTwoSqPiCount.both_dvd
#print axioms LibTwoSqPiCount.dvd_sub_mul
#print axioms LibTwoSqPiCount.inA_of_dvd

lib/TwoSqPiQ.lean

lean_all/lib/TwoSqPiQ.lean

/-
  lib/TwoSqPiQ.lean — T46 shared library (G1515d3, Jacobi's two-square formula): the prime p = u² + v² (p odd).

  Q n = #{(a, b) : a ≥ 1, b ≥ 0, a² + b² = n}.  Points of norm p·n split by π ∣ a + bi and π' ∣ a + bi (π' = v + ui):
    Abool, Abool_of, Abool_to   Boolean test for inA (π ∣ a + bi)
    count_A        #{norm pn, π ∣ ·} = Q n   (bijection sigp / taup)
    incl_excl      x ⇒ a ∨ b  gives  [x] + [x ∧ a ∧ b] = [x ∧ a] + [x ∧ b]
    Q_pi           Q(pn) + C(n) = 2 Q(n),  C(n) = #{norm pn, π ∣ ·, π' ∣ ·}
    C_zero, C_eq   C(n) = 0 if p ∤ n;  C(pm) = Q(m)
    Q_pi_ndvd      p ∤ n ⇒ Q(pn) = 2 Q(n)
    Q_pi_dvd       Q(p·pm) + Q(m) = 2 Q(pm)
  Lean 4 core only, proofs by hand, no axioms.
-/
import lib.TwoSqPiCount

namespace LibTwoSqPiQ
open LibMod LibSumScale LibTwoSqCount LibTwoSqP3 LibCountBij LibTwoSqP2 LibTwoSqPi LibTwoSqPiCount

def Abool (u v p a b : Nat) : Bool :=
  Nat.beq (md p (a * u + b * v)) 0 && Nat.beq (md p (b * u - a * v)) 0 && Nat.beq (md p (a * v - b * u)) 0

theorem and_split {x y : Bool} (h : (x && y) = true) : x = true ∧ y = true := by
  cases x <;> cases y
  · exact Bool.noConfusion h
  · exact Bool.noConfusion h
  · exact Bool.noConfusion h
  · exact ⟨rfl, rfl⟩

theorem Abool_of {u v p a b : Nat} (hp0 : 0 < p) (h : inA u v p a b) : Abool u v p a b = true := by
  obtain ⟨h1, h2, h3⟩ := h
  unfold Abool
  rw [md_zero_of_dvd hp0 h1, md_zero_of_dvd hp0 h2, md_zero_of_dvd hp0 h3]; rfl

theorem Abool_to {u v p a b : Nat} (h : Abool u v p a b = true) : inA u v p a b := by
  unfold Abool at h
  obtain ⟨h12, h3⟩ := and_split h
  obtain ⟨h1, h2⟩ := and_split h12
  exact ⟨dvd_of_md_zero (Nat.eq_of_beq_eq_true h1), dvd_of_md_zero (Nat.eq_of_beq_eq_true h2),
    dvd_of_md_zero (Nat.eq_of_beq_eq_true h3)⟩

/-- points of norm p·n with π ∣ a + bi -/
def RA (u v p n j : Nat) : Bool :=
  P (p * n) (p * n + 1) j && Abool u v p (dv (p * n + 1) j) (md (p * n + 1) j)

/-- points of norm p·n divisible by both π and π' -/
def RAB (u v p n j : Nat) : Bool := RA u v p n j && Abool v u p (dv (p * n + 1) j) (md (p * n + 1) j)

theorem Q_P (n : Nat) : Q n = S1 (fun i => cond (P n (n + 1) i) 1 0) ((n + 1) * (n + 1)) := by
  rw [Q_Qb, Qb_QP _ _ (Nat.zero_lt_succ _)]

theorem count_A {u v p : Nat} (hp : u * u + v * v = p) (hp0 : 0 < p) (hu : 1 ≤ u) (hv : 1 ≤ v) (n : Nat) :
    S1 (fun j => cond (RA u v p n j) 1 0) ((p * n + 1) * (p * n + 1)) = Q n := by
  rw [Q_P]
  symm
  refine count_bij _ _ (P n (n + 1)) (RA u v p n)
    (fun i => (p * n + 1) * (sigp u v (dv (n + 1) i) (md (n + 1) i)).1 + (sigp u v (dv (n + 1) i) (md (n + 1) i)).2)
    (fun j => (n + 1) * (taup u v p (dv (p * n + 1) j) (md (p * n + 1) j)).1 + (taup u v p (dv (p * n + 1) j) (md (p * n + 1) j)).2)
    (fun i _ hq => ?_) (fun j _ hq => ?_) (fun j _ hq => ?_) (fun i _ hq => ?_)
  · obtain ⟨hc, hn⟩ := P_true hq
    obtain ⟨ha, hab, hA, _⟩ := sigp_spec hp hp0 hv hc hn
    have hA' := lt_of_sq (Nat.le_trans (Nat.le_add_right _ _) (Nat.le_of_eq hab))
    have hB' := lt_of_sq (Nat.le_trans (Nat.le_add_left _ _) (Nat.le_of_eq hab))
    refine ⟨enc_lt hA' hB', ?_⟩
    unfold RA
    rw [P_mk ha hab hB', (dec hB').1, (dec hB').2, Abool_of hp0 hA]; rfl
  · unfold RA at hq
    obtain ⟨hP1, hA1⟩ := and_split hq
    obtain ⟨ha, hab⟩ := P_true hP1
    obtain ⟨hc, hcd, _⟩ := taup_spec hp hp0 hu ha hab (Abool_to hA1)
    have hC := lt_of_sq (Nat.le_trans (Nat.le_add_right _ _) (Nat.le_of_eq hcd))
    have hD := lt_of_sq (Nat.le_trans (Nat.le_add_left _ _) (Nat.le_of_eq hcd))
    exact ⟨enc_lt hC hD, P_mk hc hcd hD⟩
  · unfold RA at hq
    obtain ⟨hP1, hA1⟩ := and_split hq
    obtain ⟨ha, hab⟩ := P_true hP1
    obtain ⟨_, hcd, hs⟩ := taup_spec hp hp0 hu ha hab (Abool_to hA1)
    have hD := lt_of_sq (Nat.le_trans (Nat.le_add_left _ _) (Nat.le_of_eq hcd))
    show (p * n + 1) * (sigp u v (dv (n + 1) _) (md (n + 1) _)).1 + (sigp u v (dv (n + 1) _) (md (n + 1) _)).2 = j
    rw [(dec hD).1, (dec hD).2, hs]
    exact (spec (p * n + 1) j).symm
  · obtain ⟨hc, hn⟩ := P_true hq
    obtain ⟨_, hab, _, ht⟩ := sigp_spec hp hp0 hv hc hn
    have hB' := lt_of_sq (Nat.le_trans (Nat.le_add_left _ _) (Nat.le_of_eq hab))
    show (n + 1) * (taup u v p (dv (p * n + 1) _) (md (p * n + 1) _)).1 + (taup u v p (dv (p * n + 1) _) (md (p * n + 1) _)).2 = i
    rw [(dec hB').1, (dec hB').2, ht]
    exact (spec (n + 1) i).symm

theorem incl_excl (x a b : Bool) (h : x = true → a = true ∨ b = true) :
    cond x 1 0 + cond (x && a && b) 1 0 = cond (x && a) 1 0 + cond (x && b) 1 0 := by
  cases x
  · rfl
  · cases a <;> cases b
    · exfalso; cases h rfl with
      | inl e => exact Bool.noConfusion e
      | inr e => exact Bool.noConfusion e
    · rfl
    · rfl
    · rfl

theorem Q_pi {u v p : Nat} (hp : u * u + v * v = p) (hP : ∀ d, d ∣ p → d = 1 ∨ d = p) (hu : 1 ≤ u) (hv : 1 ≤ v) (n : Nat) :
    Q (p * n) + S1 (fun j => cond (RAB u v p n j) 1 0) ((p * n + 1) * (p * n + 1)) = Q n + Q n := by
  have hp0 : 0 < p := Nat.lt_of_le_of_lt (Nat.zero_le u) (lt_p hp hv)
  have hp' : v * v + u * u = p := by rw [← hp]; lib_nat
  have eB : S1 (fun j => cond (RA v u p n j) 1 0) ((p * n + 1) * (p * n + 1)) = Q n := count_A hp' hp0 hv hu n
  rw [show Q n + Q n = S1 (fun j => cond (RA u v p n j) 1 0) ((p * n + 1) * (p * n + 1))
      + S1 (fun j => cond (RA v u p n j) 1 0) ((p * n + 1) * (p * n + 1)) by rw [count_A hp hp0 hu hv n, eB]]
  rw [Q_P, ← S1_add2, ← S1_add2]
  refine S1_ext _ _ _ (fun j _ => ?_)
  show cond (P (p * n) (p * n + 1) j) 1 0
      + cond (P (p * n) (p * n + 1) j && Abool u v p (dv (p * n + 1) j) (md (p * n + 1) j)
          && Abool v u p (dv (p * n + 1) j) (md (p * n + 1) j)) 1 0
    = cond (P (p * n) (p * n + 1) j && Abool u v p (dv (p * n + 1) j) (md (p * n + 1) j)) 1 0
      + cond (P (p * n) (p * n + 1) j && Abool v u p (dv (p * n + 1) j) (md (p * n + 1) j)) 1 0
  refine incl_excl _ _ _ (fun hx => ?_)
  obtain ⟨_, hab⟩ := P_true hx
  cases inA_or hp hP hu hv hab with
  | inl h => exact Or.inl (Abool_of hp0 h)
  | inr h => exact Or.inr (Abool_of hp0 h)

theorem RAB_dvd {u v p n j : Nat} (hp : u * u + v * v = p) (hP : ∀ d, d ∣ p → d = 1 ∨ d = p) (h2 : ¬ p ∣ 2)
    (hu : 1 ≤ u) (hv : 1 ≤ v) (h : RAB u v p n j = true) :
    1 ≤ dv (p * n + 1) j ∧ dv (p * n + 1) j * dv (p * n + 1) j + md (p * n + 1) j * md (p * n + 1) j = p * n
      ∧ p ∣ dv (p * n + 1) j ∧ p ∣ md (p * n + 1) j := by
  unfold RAB RA at h
  obtain ⟨h12, hB⟩ := and_split h
  obtain ⟨hP1, hA⟩ := and_split h12
  obtain ⟨ha, hab⟩ := P_true hP1
  exact ⟨ha, hab, both_dvd hp hP h2 hu hv (Abool_to hA) (Abool_to hB)⟩

theorem C_zero {u v p : Nat} (hp : u * u + v * v = p) (hP : ∀ d, d ∣ p → d = 1 ∨ d = p) (h2 : ¬ p ∣ 2)
    (hu : 1 ≤ u) (hv : 1 ≤ v) {n : Nat} (hn : ¬ p ∣ n) :
    S1 (fun j => cond (RAB u v p n j) 1 0) ((p * n + 1) * (p * n + 1)) = 0 := by
  have hp0 : 0 < p := Nat.lt_of_le_of_lt (Nat.zero_le u) (lt_p hp hv)
  refine (S1_ext _ (fun _ => 0) _ (fun j _ => ?_)).trans (S1_zero_fn _)
  cases e : RAB u v p n j
  · rfl
  · exfalso
    obtain ⟨_, hab, ⟨a', ha'⟩, ⟨b', hb'⟩⟩ := RAB_dvd hp hP h2 hu hv e
    rw [ha', hb'] at hab
    exact hn ⟨a' * a' + b' * b', cancel_p hp0 (by rw [← hab]; lib_nat)⟩

theorem C_eq {u v p : Nat} (hp : u * u + v * v = p) (hP : ∀ d, d ∣ p → d = 1 ∨ d = p) (h2 : ¬ p ∣ 2)
    (hu : 1 ≤ u) (hv : 1 ≤ v) (m : Nat) :
    S1 (fun j => cond (RAB u v p (p * m) j) 1 0) ((p * (p * m) + 1) * (p * (p * m) + 1)) = Q m := by
  have hp0 : 0 < p := Nat.lt_of_le_of_lt (Nat.zero_le u) (lt_p hp hv)
  rw [Q_P]
  symm
  refine count_bij _ _ (P m (m + 1)) (RAB u v p (p * m))
    (fun i => (p * (p * m) + 1) * (p * dv (m + 1) i) + p * md (m + 1) i)
    (fun j => (m + 1) * dv p (dv (p * (p * m) + 1) j) + dv p (md (p * (p * m) + 1) j))
    (fun i _ hq => ?_) (fun j _ hq => ?_) (fun j _ hq => ?_) (fun i _ hq => ?_)
  · obtain ⟨ha, hab⟩ := P_true hq
    have ha1 : 1 ≤ p * dv (m + 1) i := Nat.mul_le_mul hp0 ha
    have hab' : p * dv (m + 1) i * (p * dv (m + 1) i) + p * md (m + 1) i * (p * md (m + 1) i) = p * (p * m) := by
      rw [show p * dv (m + 1) i * (p * dv (m + 1) i) + p * md (m + 1) i * (p * md (m + 1) i)
        = p * (p * (dv (m + 1) i * dv (m + 1) i + md (m + 1) i * md (m + 1) i)) by lib_nat, hab]
    have hA' := lt_of_sq (Nat.le_trans (Nat.le_add_right _ _) (Nat.le_of_eq hab'))
    have hB' := lt_of_sq (Nat.le_trans (Nat.le_add_left _ _) (Nat.le_of_eq hab'))
    refine ⟨enc_lt hA' hB', ?_⟩
    unfold RAB RA
    rw [P_mk ha1 hab' hB', (dec hB').1, (dec hB').2, Abool_of hp0 (inA_of_dvd ⟨_, rfl⟩ ⟨_, rfl⟩),
      Abool_of hp0 (inA_of_dvd ⟨_, rfl⟩ ⟨_, rfl⟩)]; rfl
  · obtain ⟨ha, hab, ⟨a', ha'⟩, ⟨b', hb'⟩⟩ := RAB_dvd hp hP h2 hu hv hq
    rw [ha', hb'] at hab
    rw [ha'] at ha
    rw [ha', hb', dv_mul hp0, dv_mul hp0]
    have hm : a' * a' + b' * b' = m := cancel_p hp0 (cancel_p hp0 (by rw [← hab]; lib_nat))
    have hC := lt_of_sq (Nat.le_trans (Nat.le_add_right _ _) (Nat.le_of_eq hm))
    have hD := lt_of_sq (Nat.le_trans (Nat.le_add_left _ _) (Nat.le_of_eq hm))
    exact ⟨enc_lt hC hD, P_mk (pos_of_mul ha rfl) hm hD⟩
  · obtain ⟨_, hab, ⟨a', ha'⟩, ⟨b', hb'⟩⟩ := RAB_dvd hp hP h2 hu hv hq
    rw [ha', hb'] at hab
    have hm : a' * a' + b' * b' = m := cancel_p hp0 (cancel_p hp0 (by rw [← hab]; lib_nat))
    have hD := lt_of_sq (Nat.le_trans (Nat.le_add_left _ _) (Nat.le_of_eq hm))
    show (p * (p * m) + 1) * (p * dv (m + 1) ((m + 1) * dv p (dv (p * (p * m) + 1) j) + dv p (md (p * (p * m) + 1) j)))
      + p * md (m + 1) ((m + 1) * dv p (dv (p * (p * m) + 1) j) + dv p (md (p * (p * m) + 1) j)) = j
    rw [ha', hb', dv_mul hp0, dv_mul hp0, (dec hD).1, (dec hD).2, ← ha', ← hb']
    exact (spec (p * (p * m) + 1) j).symm
  · obtain ⟨_, hab⟩ := P_true hq
    have hab' : p * dv (m + 1) i * (p * dv (m + 1) i) + p * md (m + 1) i * (p * md (m + 1) i) = p * (p * m) := by
      rw [show p * dv (m + 1) i * (p * dv (m + 1) i) + p * md (m + 1) i * (p * md (m + 1) i)
        = p * (p * (dv (m + 1) i * dv (m + 1) i + md (m + 1) i * md (m + 1) i)) by lib_nat, hab]
    have hB' := lt_of_sq (Nat.le_trans (Nat.le_add_left _ _) (Nat.le_of_eq hab'))
    show (m + 1) * dv p (dv (p * (p * m) + 1) ((p * (p * m) + 1) * (p * dv (m + 1) i) + p * md (m + 1) i))
      + dv p (md (p * (p * m) + 1) ((p * (p * m) + 1) * (p * dv (m + 1) i) + p * md (m + 1) i)) = i
    rw [(dec hB').1, (dec hB').2, dv_mul hp0, dv_mul hp0]
    exact (spec (m + 1) i).symm

theorem Q_pi_ndvd {u v p : Nat} (hp : u * u + v * v = p) (hP : ∀ d, d ∣ p → d = 1 ∨ d = p) (h2 : ¬ p ∣ 2)
    (hu : 1 ≤ u) (hv : 1 ≤ v) {n : Nat} (hn : ¬ p ∣ n) : Q (p * n) = Q n + Q n := by
  have h := Q_pi hp hP hu hv n
  rw [C_zero hp hP h2 hu hv hn, Nat.add_zero] at h
  exact h

theorem Q_pi_dvd {u v p : Nat} (hp : u * u + v * v = p) (hP : ∀ d, d ∣ p → d = 1 ∨ d = p) (h2 : ¬ p ∣ 2)
    (hu : 1 ≤ u) (hv : 1 ≤ v) (m : Nat) : Q (p * (p * m)) + Q m = Q (p * m) + Q (p * m) := by
  have h := Q_pi hp hP hu hv (p * m)
  rw [C_eq hp hP h2 hu hv m] at h
  exact h

end LibTwoSqPiQ

#print axioms LibTwoSqPiQ.and_split
#print axioms LibTwoSqPiQ.Abool_of
#print axioms LibTwoSqPiQ.Abool_to
#print axioms LibTwoSqPiQ.Q_P
#print axioms LibTwoSqPiQ.count_A
#print axioms LibTwoSqPiQ.incl_excl
#print axioms LibTwoSqPiQ.Q_pi
#print axioms LibTwoSqPiQ.RAB_dvd
#print axioms LibTwoSqPiQ.C_zero
#print axioms LibTwoSqPiQ.C_eq
#print axioms LibTwoSqPiQ.Q_pi_ndvd
#print axioms LibTwoSqPiQ.Q_pi_dvd

lib/TwoSqUniq.lean

lean_all/lib/TwoSqUniq.lean

/-
  lib/TwoSqUniq.lean — T46 shared library (G1515d2, Jacobi's two-square formula): uniqueness of p = u² + v².

    addc        n + i = n + j ⇒ i = j
    amgm2       2AB ≤ A² + B²
    sq_eq2      A² + B² = 2AB ⇒ A = B
    two_sq_uniq p prime, u² + v² = p = x² + y²  ⇒  (u = x ∧ v = y) ∨ (u = y ∧ v = x)
  Proof: (ux+vy)(uy+vx) = p(uv+xy), so p divides one factor; it is ≤ p by
  p² + 2(uy)(vx) = (ux+vy)² + (uy)² + (vx)², hence equal to p, which forces uy = vx and then u = x, v = y.
  Lean 4 core only, proofs by hand, no axioms.
-/
import lib.HeckeRel

namespace LibTwoSqUniq

theorem addc : ∀ n i j : Nat, n + i = n + j → i = j
  | 0, i, j, h => by rw [Nat.zero_add, Nat.zero_add] at h; exact h
  | n + 1, i, j, h => addc n i j (Nat.succ.inj (by rw [Nat.succ_add, Nat.succ_add] at h; exact h))

theorem zero_of_sq (u : Nat) (h : u * u = 0) : u = 0 := by
  cases u with
  | zero => rfl
  | succ u' => exact Nat.noConfusion (show Nat.succ ((u' + 1) * u' + u') = 0 from h)

theorem amgm2_le (B d : Nat) : 2 * (B + d) * B ≤ (B + d) * (B + d) + B * B := by
  rw [show (B + d) * (B + d) + B * B = 2 * (B + d) * B + d * d by lib_nat]
  exact Nat.le_add_right _ _

theorem amgm2 (A B : Nat) : 2 * A * B ≤ A * A + B * B := by
  cases Nat.lt_or_ge A B with
  | inl h =>
    obtain ⟨d, hd⟩ := LibHeckeRel.ex_add (Nat.le_of_lt h)
    subst hd
    have := amgm2_le A d
    rw [show 2 * A * (d + A) = 2 * (A + d) * A by lib_nat, show A * A + (d + A) * (d + A) = (A + d) * (A + d) + A * A by lib_nat]
    exact this
  | inr h =>
    obtain ⟨d, hd⟩ := LibHeckeRel.ex_add h
    subst hd
    rw [show 2 * (d + B) * B = 2 * (B + d) * B by lib_nat, show (d + B) * (d + B) + B * B = (B + d) * (B + d) + B * B by lib_nat]
    exact amgm2_le B d

theorem sq_eq2_aux (B d : Nat) (h : (B + d) * (B + d) + B * B = 2 * (B + d) * B) : d = 0 := by
  rw [show (B + d) * (B + d) + B * B = 2 * (B + d) * B + d * d by lib_nat,
    show 2 * (B + d) * B = 2 * (B + d) * B + 0 from rfl] at h
  exact zero_of_sq d (addc _ _ _ h)

theorem sq_eq2 {A B : Nat} (h : A * A + B * B = 2 * A * B) : A = B := by
  cases Nat.lt_or_ge A B with
  | inl hl =>
    obtain ⟨d, hd⟩ := LibHeckeRel.ex_add (Nat.le_of_lt hl)
    subst hd
    have := sq_eq2_aux A d (by
      rw [show (A + d) * (A + d) + A * A = A * A + (d + A) * (d + A) by lib_nat, h]; lib_nat)
    subst this; exact (Nat.zero_add A).symm
  | inr hg =>
    obtain ⟨d, hd⟩ := LibHeckeRel.ex_add hg
    subst hd
    have := sq_eq2_aux B d (by
      rw [show (B + d) * (B + d) + B * B = (d + B) * (d + B) + B * B by lib_nat, h]; lib_nat)
    subst this; exact Nat.zero_add B

theorem le_of_sq_le {s p : Nat} (h : s * s ≤ p * p) : s ≤ p := by
  cases Nat.lt_or_ge p s with
  | inl hl =>
    exfalso
    have h1 : (p + 1) * (p + 1) ≤ s * s := Nat.mul_le_mul hl hl
    have h2 : p * p < (p + 1) * (p + 1) := by
      rw [show (p + 1) * (p + 1) = p * p + (2 * p + 1) by lib_nat]
      exact Nat.lt_add_of_pos_right (Nat.zero_lt_succ _)
    exact Nat.lt_irrefl _ (Nat.lt_of_lt_of_le h2 (Nat.le_trans h1 h))
  | inr hg => exact hg

/-- a positive multiple of p that is ≤ p equals p -/
theorem eq_of_dvd_le {s p : Nat} (hd : p ∣ s) (hs : 0 < s) (hle : s ≤ p) : s = p := by
  obtain ⟨k, hk⟩ := hd
  subst hk
  cases k with
  | zero => exact absurd hs (by rw [Nat.mul_zero]; exact Nat.lt_irrefl 0)
  | succ k' =>
    cases k' with
    | zero => exact Nat.mul_one p
    | succ k'' =>
      exfalso
      have hp : 0 < p := Nat.pos_of_ne_zero (fun e => by rw [e, Nat.zero_mul] at hs; exact Nat.lt_irrefl 0 hs)
      rw [show p * (k'' + 1 + 1) = p + p * (k'' + 1) by lib_nat] at hle
      exact Nat.lt_irrefl p (Nat.lt_of_lt_of_le (Nat.lt_add_of_pos_right (Nat.mul_pos hp (Nat.zero_lt_succ k''))) hle)

theorem pos_of_prime_sq {u v p : Nat} (hp : LibPrimePow.Prime p) (h : u * u + v * v = p) : 0 < u := by
  cases u with
  | succ u' => exact Nat.zero_lt_succ u'
  | zero =>
    exfalso
    rw [show 0 * 0 + v * v = v * v by lib_nat] at h
    have hp0 : 0 < p := Nat.lt_trans (Nat.zero_lt_succ 0) hp.1
    cases hp.2 v ⟨v, h.symm⟩ with
    | inl e => subst e; rw [← h] at hp; exact Nat.lt_irrefl 1 hp.1
    | inr e =>
      subst e
      have : v * v = v * 1 := by rw [Nat.mul_one]; exact h
      have e1 := Nat.eq_of_mul_eq_mul_left hp0 this
      subst e1; exact Nat.lt_irrefl 1 hp.1

/-- s² + C = p² + D with D ≤ C gives s ≤ p. -/
theorem le_of_balance {s p C D : Nat} (h : s * s + C = p * p + D) (hDC : D ≤ C) : s ≤ p := by
  obtain ⟨e, he⟩ := LibHeckeRel.ex_add hDC
  subst he
  have h2 : D + (s * s + e) = D + p * p := by
    rw [show D + (s * s + e) = s * s + (e + D) by lib_nat, h]; lib_nat
  have h3 := addc _ _ _ h2
  exact le_of_sq_le (by rw [← h3]; exact Nat.le_add_right _ _)

theorem case_s {u v x y p : Nat} (hp0 : 0 < p) (hu : u * u + v * v = p) (hx : x * x + y * y = p)
    (hs : u * x + v * y = p) : u = x ∧ v = y := by
  have id : (u * u + v * v) * (x * x + y * y) + 2 * (u * y) * (v * x)
      = (u * x + v * y) * (u * x + v * y) + ((u * y) * (u * y) + (v * x) * (v * x)) := by lib_nat
  rw [hu, hx, hs] at id
  have hA : u * y = v * x := sq_eq2 (addc _ _ _ id).symm
  constructor
  · apply Nat.eq_of_mul_eq_mul_left hp0
    have e1 : p * u = u * (u * x + v * y) := by rw [hs]; lib_nat
    rw [e1, show u * (u * x + v * y) = u * u * x + v * (u * y) by lib_nat, hA, ← hu]
    lib_nat
  · apply Nat.eq_of_mul_eq_mul_left hp0
    have e1 : p * v = v * (u * x + v * y) := by rw [hs]; lib_nat
    rw [e1, show v * (u * x + v * y) = u * (v * x) + v * v * y by lib_nat, ← hA, ← hu]
    lib_nat

theorem two_sq_uniq {u v x y p : Nat} (hp : LibPrimePow.Prime p) (hu : u * u + v * v = p) (hx : x * x + y * y = p) :
    (u = x ∧ v = y) ∨ (u = y ∧ v = x) := by
  have hp0 : 0 < p := Nat.lt_trans (Nat.zero_lt_succ 0) hp.1
  have u0 := pos_of_prime_sq hp hu
  have x0 := pos_of_prime_sq hp hx
  have y0 := pos_of_prime_sq hp (show y * y + x * x = p by rw [← hx]; lib_nat)
  have hprod : (u * x + v * y) * (u * y + v * x) = p * (u * v + x * y) := by
    rw [show (u * x + v * y) * (u * y + v * x) = u * v * (x * x + y * y) + x * y * (u * u + v * v) by lib_nat, hx, hu]
    lib_nat
  cases LibGcd.euclid_prime hp.2 (show p ∣ (u * x + v * y) * (u * y + v * x) from ⟨_, hprod⟩) with
  | inl hd =>
    refine Or.inl (case_s hp0 hu hx (eq_of_dvd_le hd (Nat.lt_of_lt_of_le (Nat.mul_pos u0 x0) (Nat.le_add_right _ _)) ?_))
    have id : (u * x + v * y) * (u * x + v * y) + ((u * y) * (u * y) + (v * x) * (v * x))
        = (u * u + v * v) * (x * x + y * y) + 2 * (u * y) * (v * x) := by lib_nat
    rw [hu, hx] at id
    exact le_of_balance id (amgm2 _ _)
  | inr hd =>
    have hx' : y * y + x * x = p := by rw [← hx]; lib_nat
    have ht : u * y + v * x = p := eq_of_dvd_le hd (Nat.lt_of_lt_of_le (Nat.mul_pos u0 y0) (Nat.le_add_right _ _)) (by
      have id : (u * y + v * x) * (u * y + v * x) + ((u * x) * (u * x) + (v * y) * (v * y))
          = (u * u + v * v) * (y * y + x * x) + 2 * (u * x) * (v * y) := by lib_nat
      rw [hu, hx'] at id
      exact le_of_balance id (amgm2 _ _))
    exact Or.inr (case_s hp0 hu hx' ht)

end LibTwoSqUniq

#print axioms LibTwoSqUniq.addc
#print axioms LibTwoSqUniq.amgm2
#print axioms LibTwoSqUniq.sq_eq2
#print axioms LibTwoSqUniq.le_of_sq_le
#print axioms LibTwoSqUniq.eq_of_dvd_le
#print axioms LibTwoSqUniq.pos_of_prime_sq
#print axioms LibTwoSqUniq.le_of_balance
#print axioms LibTwoSqUniq.case_s
#print axioms LibTwoSqUniq.two_sq_uniq

lib/TwoSquare.lean

lean_all/lib/TwoSquare.lean

/-
  lib/TwoSquare.lean — T46 shared library, cycle 316 (G4-3): the second supplementary law.

  p = 2m + 3 prime, h = m + 1 = (p − 1)/2, 𝔽_p = znR (k m):
    two_sq       h ≡ 0, 3 (mod 4)  (p ≡ ±1 mod 8) ⇒ ∃ y, y² = 2
    two_nonsq    h ≡ 1, 2 (mod 4)  (p ≡ ±3 mod 8) ⇒ ∀ y, y² ≠ 2
  (GaussTwo2.two_pow + EulerFF.euler_hard / euler_sq; 𝔽_p as FFSum data (FFInst).)
  Lean 4 core only, no axioms.
-/
import lib.GaussTwo2
import lib.EulerFF2
import lib.Wilson

namespace LibTwoSquare
open DetChain LibRing LibMod LibZp LibEulerCrit LibFFInst LibGaussTwo2 LibLegSum

variable (m : Nat) (hp : Prime1 (k m))

theorem neg_one_pow_even (K : CRing (ZN (k m))) (b : Nat) : pw K (K.neg K.one) (2 * b) = K.one := LibWilson.pw_neg_one_even K b

theorem neg_one_pow_odd (K : CRing (ZN (k m))) (b : Nat) : pw K (K.neg K.one) (2 * b + 1) = K.neg K.one := by
  show K.mul (pw K (K.neg K.one) (2 * b)) (K.neg K.one) = _
  rw [LibWilson.pw_neg_one_even]; lib_ring

include hp in
theorem pow_eq_one (b : Nat) (h : m + 1 = 4 * b ∨ m + 1 = 4 * b + 3) :
    pw (znR (k m)) (castN (znR (k m)) 2) (m + 1) = (znR (k m)).one := by
  cases h with
  | inl e =>
    rw [two_pow_even (m + 1) hp (2 * b) (by rw [e]; lib_nat) (Nat.succ_le_succ (Nat.zero_le m))]
    exact neg_one_pow_even m _ b
  | inr e =>
    rw [two_pow_odd (m + 1) hp (2 * b + 1) (by rw [e]; lib_nat), show 2 * b + 1 + 1 = 2 * (b + 1) by lib_nat]
    exact neg_one_pow_even m _ (b + 1)

include hp in
theorem pow_eq_neg (b : Nat) (h : m + 1 = 4 * b + 1 ∨ m + 1 = 4 * b + 2) :
    pw (znR (k m)) (castN (znR (k m)) 2) (m + 1) = (znR (k m)).neg (znR (k m)).one := by
  cases h with
  | inl e =>
    rw [two_pow_odd (m + 1) hp (2 * b) (by rw [e]; lib_nat)]
    exact neg_one_pow_odd m _ b
  | inr e =>
    rw [two_pow_even (m + 1) hp (2 * b + 1) (by rw [e]; lib_nat) (Nat.succ_le_succ (Nat.zero_le m))]
    exact neg_one_pow_odd m _ b

theorem hlen : (allZN m).length = 2 * m + 1 + 2 := by
  rw [LibJac.len_allZN m]; show 2 * (m + 1) + 1 = 2 * m + 1 + 2; lib_nat

include hp in
/-- **p ≡ ±1 (mod 8) ⇒ 2 is a square.** -/
theorem two_sq (b : Nat) (h : m + 1 = 4 * b ∨ m + 1 = 4 * b + 3) : ∃ y, (znR (k m)).mul y y = castN (znR (k m)) 2 :=
  LibEulerFF.euler_hard (znR (k m)) (zp_noZD (k m) hp) (LibCubicCrit.one_ne_zero' m) (allZN m) (dist_allZN m)
    (mem_allZN m) (2 * m + 1) (hlen m) (fun x hx => LibFermat.fermat_unit (k m) hp x hx) (m + 1) (by lib_nat)
    (castN (znR (k m)) 2) (pow_eq_one m hp b h)

include hp in
/-- **p ≡ ±3 (mod 8) ⇒ 2 is not a square.** -/
theorem two_nonsq (b : Nat) (h : m + 1 = 4 * b + 1 ∨ m + 1 = 4 * b + 2) (y : ZN (k m)) :
    (znR (k m)).mul y y ≠ castN (znR (k m)) 2 := fun e => by
  have hy : y ≠ (znR (k m)).zero := fun e0 => by
    rw [e0] at e
    have h2 : castN (znR (k m)) 2 = (znR (k m)).zero := by rw [← e]; lib_ring
    have := pow_eq_neg m hp b h
    rw [h2, show m + 1 = m + 1 from rfl] at this
    have z : pw (znR (k m)) (znR (k m)).zero (m + 1) = (znR (k m)).zero := by
      show (znR (k m)).mul (pw (znR (k m)) (znR (k m)).zero m) (znR (k m)).zero = _; lib_ring
    rw [z] at this
    exact LibCubicCrit.one_ne_zero' m (by
      have : (znR (k m)).one = (znR (k m)).neg (znR (k m)).zero := by rw [this]; lib_ring
      rw [this]; lib_ring)
  have h1 := LibEulerFF.euler_sq (znR (k m)) (2 * m + 1) (fun x hx => LibFermat.fermat_unit (k m) hp x hx) (m + 1)
    (by lib_nat) y hy
  rw [e, pow_eq_neg m hp b h] at h1
  exact LibEulerFF2.neg_ne_one (znR (k m)) (zp_noZD (k m) hp) (LibCubicCrit.one_ne_zero' m) (allZN m) (dist_allZN m)
    (mem_allZN m) (2 * m + 1) (hlen m) (fun x hx => LibFermat.fermat_unit (k m) hp x hx) (m + 1) (by lib_nat) h1

end LibTwoSquare
#print axioms LibTwoSquare.two_sq
#print axioms LibTwoSquare.two_nonsq

lib/TwoTors.lean

lean_all/lib/TwoTors.lean

/-
  lib/TwoTors.lean — T46 shared library, cycle 689 (G536–G538): closed form for adding the 2-torsion point (0, 0).

  Curve y² = x³ + u x² + v x (= x(x² + ux + v); LibChordA.onE K u v 0), T = (0, 0):
    tauT_on     wx = 1, (x, y) on the curve ⇒ τ_T(x, y) = (v w, −v y w²) on the curve
    tauT_inv    if also zv = 1 ⇒ τ_T(τ_T P) = P   (inverse coordinate of τ_T: 1/(vw) = z x)
    tauT_chord  τ_T P = the point of ChordInv.add_T (t = 0, λ = y w)
  Lean 4 core only, no axioms.
-/
import lib.ChordInv

namespace LibTwoTors
open DetChain LibRing LibChordA

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

/-- **P + (0,0) = (v/x, −v y/x²).** -/
theorem tauT_on (u v x y w : R) (h : onE K u v K.zero x y) (hw : K.mul w x = K.one) :
    onE K u v K.zero (K.mul v w) (K.neg (K.mul (K.mul v y) (K.mul w w))) := by
  unfold onE at *
  lib_lc K [h, hw] [K.mul (K.mul v v) (K.mul (K.mul w w) (K.mul w w)),
    K.add (K.add (K.mul (K.mul (K.mul v v) w) (K.add (K.add (K.mul (K.mul w w) (K.mul x x)) (K.mul w x)) K.one))
      (K.mul (K.mul (K.mul u (K.mul v v)) (K.mul w w)) (K.add (K.mul w x) K.one)))
      (K.mul (K.mul (K.mul v v) v) (K.mul (K.mul w w) w))]

/-- **τ_T is an involution.** -/
theorem tauT_inv (v x y w z : R) (hw : K.mul w x = K.one) (hz : K.mul z v = K.one) :
    K.mul v (K.mul z x) = x ∧
    K.neg (K.mul (K.mul v (K.neg (K.mul (K.mul v y) (K.mul w w)))) (K.mul (K.mul z x) (K.mul z x))) = y := by
  refine ⟨?_, ?_⟩
  · have : K.mul v (K.mul z x) = K.mul (K.mul z v) x := by lib_ring
    rw [this, hz]; lib_ring
  · have : K.neg (K.mul (K.mul v (K.neg (K.mul (K.mul v y) (K.mul w w)))) (K.mul (K.mul z x) (K.mul z x)))
        = K.mul (K.mul (K.mul (K.mul z v) (K.mul z v)) (K.mul (K.mul w x) (K.mul w x))) y := by lib_ring
    rw [this, hz, hw]; lib_ring

/-- **Same point as ChordInv.add_T (t = 0).** -/
theorem tauT_chord (u v x y w : R) (h : onE K u v K.zero x y) (hw : K.mul w x = K.one) :
    sub K (sub K (sub K (K.mul (K.mul y w) (K.mul y w)) u) x) K.zero = K.mul v w ∧
    sub K (K.mul (K.mul y w) (sub K x (sub K (sub K (sub K (K.mul (K.mul y w) (K.mul y w)) u) x) K.zero))) y
      = K.neg (K.mul (K.mul v y) (K.mul w w)) := by
  unfold onE at h
  have hx3 : sub K (sub K (sub K (K.mul (K.mul y w) (K.mul y w)) u) x) K.zero = K.mul v w := by
    lib_lc K [h, hw] [K.mul w w, K.add (K.add (K.mul x (K.add (K.mul w x) K.one)) (K.mul u (K.add (K.mul w x) K.one))) (K.mul v w)]
  refine ⟨hx3, ?_⟩
  rw [hx3]
  lib_lc K [hw] [y]

end LibTwoTors

#print axioms LibTwoTors.tauT_on
#print axioms LibTwoTors.tauT_inv
#print axioms LibTwoTors.tauT_chord

lib/TwoTorsAB.lean

lean_all/lib/TwoTorsAB.lean

/-
  lib/TwoTorsAB.lean — T46 shared library, cycle 690 (G539, G540): action of the 2-torsion group on y² = x(x + α)(x + β).

  E : y² = x³ + (α+β)x² + αβ x (agent1 Z4Z2 has α = a², β = b²), T₀ = (0,0), T_α = (−α, 0), T_β = (−β, 0):
    tau0            τ₀(x, y) = (αβ w, −αβ y w²),  wx = 1
    tauA_on         w'(x + α) = 1 ⇒ τ_α(x, y) = (α(α−β)w' − α, −α(α−β) y w'²) ∈ E
    tauB_A_inv      inverse of x + α for τ_β P = (x + β) w r   (wx = 1, w_b(x + β) = 1, r(α − β) = 1)
    tauA_tauB       τ_α(τ_β P) = τ₀ P   (T_α + T_β = T₀)
  Lean 4 core only, no axioms.
-/
import lib.TwoTors

namespace LibTwoTorsAB
open DetChain LibRing LibChordA

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

/-- On E ⇔ on the shifted curve (X = x + α). -/
theorem shift_on (α β x y : R) (h : onE K (K.add α β) (K.mul α β) K.zero x y) :
    onE K (sub K β (K.add α α)) (K.mul α (sub K α β)) K.zero (K.add x α) y := by
  unfold onE at *; rw [h]; lib_ring

theorem unshift_on (α β X y : R) (h : onE K (sub K β (K.add α α)) (K.mul α (sub K α β)) K.zero X y) :
    onE K (K.add α β) (K.mul α β) K.zero (sub K X α) y := by
  unfold onE at *; rw [h]; lib_ring

/-- **Adding T_α.** -/
theorem tauA_on (α β x y w' : R) (h : onE K (K.add α β) (K.mul α β) K.zero x y) (hw : K.mul w' (K.add x α) = K.one) :
    onE K (K.add α β) (K.mul α β) K.zero (sub K (K.mul (K.mul α (sub K α β)) w') α)
      (K.neg (K.mul (K.mul (K.mul α (sub K α β)) y) (K.mul w' w'))) :=
  unshift_on K α β _ _ (LibTwoTors.tauT_on K _ _ (K.add x α) y w' (shift_on K α β x y h) hw)

/-- Inverse of (x + α) for τ_β P. -/
theorem tauB_A_inv (α β x w wb r : R) (hw : K.mul w x = K.one) (hwb : K.mul wb (K.add x β) = K.one)
    (hr : K.mul r (sub K α β) = K.one) :
    K.mul (K.mul (K.mul (K.add x β) w) r) (K.add (sub K (K.mul (K.mul β (sub K β α)) wb) β) α) = K.one := by
  lib_lc K [hwb, hw, hr] [K.mul (K.mul (K.mul β (sub K β α)) w) r, K.mul (sub K α β) r, K.one]

/-- **τ_α ∘ τ_β = τ₀.** -/
theorem tauA_tauB (α β x y w wb r : R) (hw : K.mul w x = K.one) (hwb : K.mul wb (K.add x β) = K.one)
    (hr : K.mul r (sub K α β) = K.one) :
    sub K (K.mul (K.mul α (sub K α β)) (K.mul (K.mul (K.add x β) w) r)) α = K.mul (K.mul α β) w ∧
    K.neg (K.mul (K.mul (K.mul α (sub K α β)) (K.neg (K.mul (K.mul (K.mul β (sub K β α)) y) (K.mul wb wb))))
      (K.mul (K.mul (K.mul (K.add x β) w) r) (K.mul (K.mul (K.add x β) w) r)))
      = K.neg (K.mul (K.mul (K.mul α β) y) (K.mul w w)) := by
  refine ⟨?_, ?_⟩
  · lib_lc K [hr, hw] [K.mul (K.mul α (K.add x β)) w, α]
  · lib_lc K [hwb, hr]
      [K.neg (K.mul (K.mul (K.mul (K.mul (K.mul α β) y) (K.mul w w)) (K.mul (K.mul (sub K α β) r) (K.mul (sub K α β) r)))
          (K.add (K.mul wb (K.add x β)) K.one)),
       K.neg (K.mul (K.mul (K.mul (K.mul α β) y) (K.mul w w)) (K.add (K.mul (sub K α β) r) K.one))]

end LibTwoTorsAB

#print axioms LibTwoTorsAB.tauA_on
#print axioms LibTwoTorsAB.tauB_A_inv
#print axioms LibTwoTorsAB.tauA_tauB

lib/UnitOrd.lean

lean_all/lib/UnitOrd.lean

/-
  lib/UnitOrd.lean — T46 shared library, cycle 672 (G507, G509): order bound for units of a finite ring, Pépin form.

    search      ∀ n, (∃ d, 0 < d ≤ n, x^d = 1) ∨ (∀ d, 0 < d → d ≤ n → x^d ≠ 1)   (DecidableEq, no classical logic)
    unit_ord    xy = 1, element list m (containing every element) ⇒ ∃ d, 0 < d ≤ |m|, x^d = 1   (pigeonhole)
    pepin       1 + 1 ≠ 0, x^{2^m} = −1 ⇒ x^{2^{m+1}} = 1, and if x^N = 1 (N > 0) then 2^{m+1} ≤ N
  Lean 4 core only, no axioms.
-/
import lib.Pow2Ord
import lib.GaussTwo
import lib.GaussTwo2
import lib.Distinct

namespace LibUnitOrd
open DetChain LibRing LibLucasDbl

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

theorem search [DecidableEq R] (x : R) : ∀ n, (∃ d, 0 < d ∧ d ≤ n ∧ pw K x d = K.one) ∨ (∀ d, 0 < d → d ≤ n → pw K x d ≠ K.one)
  | 0 => Or.inr (fun d hd h0 _ => Nat.lt_irrefl 0 (Nat.lt_of_lt_of_le hd h0))
  | n + 1 => by
    rcases search x n with ⟨d, h1, h2, h3⟩ | hno
    · exact Or.inl ⟨d, h1, Nat.le_succ_of_le h2, h3⟩
    · cases decEq (pw K x (n + 1)) K.one with
      | isTrue e => exact Or.inl ⟨n + 1, Nat.succ_pos n, Nat.le_refl _, e⟩
      | isFalse e =>
        refine Or.inr (fun d hd hdn => ?_)
        cases Nat.lt_or_ge d (n + 1) with
        | inl hl => exact hno d hd (Nat.le_of_lt_succ hl)
        | inr hg => rw [Nat.le_antisymm hdn hg]; exact e

theorem pw_unit (x y : R) (hxy : K.mul x y = K.one) (i : Nat) : K.mul (pw K x i) (pw K y i) = K.one := by
  rw [← mul_pw', hxy, LibFermat.pw_one]

/-- **Order bound for units (pigeonhole).** -/
theorem unit_ord [DecidableEq R] (x y : R) (hxy : K.mul x y = K.one) (m : List R) (hm : ∀ r, r ∈ m) :
    ∃ d, 0 < d ∧ d ≤ m.length ∧ pw K x d = K.one := by
  rcases search K x m.length with h | hno
  · exact h
  · exfalso
    have hinj : ∀ i j, i ∈ LibSum.rng (m.length + 1) → j ∈ LibSum.rng (m.length + 1) → pw K x i = pw K x j → i = j := by
      have key : ∀ i j, j < m.length + 1 → i < j → pw K x i = pw K x j → False := by
        intro i j hj hij he
        obtain ⟨c, hc⟩ := LibNatSub.le_ex (Nat.le_of_lt hij)
        subst hc
        have cpos : 0 < c := by
          cases c with
          | zero => exact absurd hij (Nat.lt_irrefl i)
          | succ c => exact Nat.succ_pos c
        have cle : c ≤ m.length := Nat.le_trans (Nat.le_add_left c i) (Nat.le_of_lt_succ hj)
        apply hno c cpos cle
        have e2 : K.mul (K.mul (pw K x i) (pw K y i)) (pw K x c) = K.mul (pw K x i) (pw K y i) := by
          have : K.mul (K.mul (pw K x i) (pw K y i)) (pw K x c) = K.mul (pw K y i) (K.mul (pw K x i) (pw K x c)) := by
            generalize pw K x i = a; generalize pw K y i = b; generalize pw K x c = d; lib_ring
          rw [this, ← pw_add' K, ← he]; generalize pw K x i = a; generalize pw K y i = b; lib_ring
        rw [pw_unit K x y hxy] at e2
        rw [← e2]; generalize pw K x c = d; lib_ring
      intro i j hi hj he
      cases Nat.lt_or_ge i j with
      | inl h => exact (key i j (LibBinom.mem_rng hj) h he).elim
      | inr h =>
        cases Nat.lt_or_ge j i with
        | inl h' => exact (key j i (LibBinom.mem_rng hi) h' he.symm).elim
        | inr h' => exact Nat.le_antisymm h' h
    have hp := LibDist.pigeon (fun i => pw K x i) (LibSum.rng (m.length + 1)) m (LibGaussTwo.dist_rng _) hinj (fun i _ => hm _)
    rw [LibGaussTwo2.len_rng'] at hp
    exact Nat.not_succ_le_self _ hp

/-- **Pépin form:** x^{2^m} = −1 ⇒ x^{2^{m+1}} = 1 and no smaller positive exponent works. -/
theorem pepin (h2 : K.add K.one K.one ≠ K.zero) (x : R) (m : Nat) (hx : pw K x (2 ^ m) = K.neg K.one) :
    pw K x (2 ^ (m + 1)) = K.one ∧ ∀ N, 0 < N → pw K x N = K.one → 2 ^ (m + 1) ≤ N := by
  refine ⟨?_, fun N hN h1 => ?_⟩
  · rw [Nat.pow_succ, Nat.mul_two, pw_add' K, hx]; lib_ring
  · obtain ⟨c, hc⟩ := LibPow2Ord.ord2 K h2 m x N hx h1
    subst hc
    cases c with
    | zero => exact absurd hN (Nat.lt_irrefl 0)
    | succ c => show 2 ^ (m + 1) ≤ 2 ^ (m + 1) * c + 2 ^ (m + 1); exact Nat.le_add_left _ _

end LibUnitOrd

#print axioms LibUnitOrd.search
#print axioms LibUnitOrd.unit_ord
#print axioms LibUnitOrd.pepin

lib/UnitRow.lean

lean_all/lib/UnitRow.lean

/-
  lib/UnitRow.lean — T46 shared library, cycle 699 (G567): det of a matrix whose row r is the unit vector e_c (G466 split 2).

    swR, moveR     move row r to the top (the rest 0..r−1 shift down one), det (moveR r A) = (−1)^r det A
    moveC          move column c to the front (via transpose), factor (−1)^c in det
    del r c A      matrix with row r and column c deleted
    det_unit_row   row r of A = e_c ⇒ det_{n+1} A = (−1)^{r+c} det_n (del r c A)
  Lean 4 core only, no axioms.
-/
import lib.RowSubsets
import lib.DetAlt3
import lib.DetTrans

namespace LibUnitRow
open DetChain LibRing LibDet LibRowSubsets

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

def swR (A : Nat → Nat → R) (i : Nat) : Nat → Nat → R := LibDetAlt3.setR A i (A (i + 1)) (A i)

def moveR : Nat → (Nat → Nat → R) → (Nat → Nat → R)
  | 0, A => A
  | r + 1, A => moveR r (swR A r)

theorem beq_ne {a b : Nat} (h : a ≠ b) : Nat.beq a b = false := by
  cases hb : Nat.beq a b with
  | false => rfl
  | true => exact absurd (Nat.eq_of_beq_eq_true hb) h

theorem det_moveR (n : Nat) : ∀ (r : Nat) (A : Nat → Nat → R), r < n → det K n (moveR r A) = K.mul (sg K r) (det K n A)
  | 0, A, _ => by show det K n A = K.mul K.one _; lib_ring
  | r + 1, A, h => by
    show det K n (moveR r (swR A r)) = _
    rw [det_moveR n r (swR A r) (Nat.lt_of_succ_lt h)]
    show K.mul (sg K r) (det K n (LibDetAlt3.setR A r (A (r + 1)) (A r))) = K.mul (K.mul (sg K r) (K.neg K.one)) (det K n A)
    rw [LibDetAlt3.det_swap_adj K A n r h]
    generalize sg K r = s; generalize det K n A = d; lib_ring

theorem moveR_zero : ∀ (r : Nat) (A : Nat → Nat → R) (j : Nat), moveR r A 0 j = A r j
  | 0, _, _ => rfl
  | r + 1, A, j => by
    show moveR r (swR A r) 0 j = _
    rw [moveR_zero r]
    show cond (Nat.beq r r) _ _ = _; rw [beq_self]; rfl

theorem moveR_succ : ∀ (r : Nat) (A : Nat → Nat → R) (i j : Nat), moveR r A (i + 1) j = cond (Nat.blt i r) (A i j) (A (i + 1) j)
  | 0, A, i, j => by show A (i + 1) j = cond (Nat.blt i 0) _ _; rw [blt_false (Nat.zero_le i)]; rfl
  | r + 1, A, i, j => by
    show moveR r (swR A r) (i + 1) j = _
    rw [moveR_succ r]
    cases Nat.lt_or_ge i r with
    | inl h =>
      rw [blt_true h, blt_true (Nat.lt_succ_of_lt h)]
      show cond (Nat.beq i r) _ (cond (Nat.beq i (r + 1)) _ _) = _
      rw [beq_ne (Nat.ne_of_lt h), beq_ne (Nat.ne_of_lt (Nat.lt_succ_of_lt h))]; rfl
    | inr h =>
      rw [blt_false h]
      cases Nat.decEq i r with
      | isTrue e =>
        subst e
        rw [blt_true (Nat.lt_succ_self i)]
        show cond (Nat.beq (i + 1) i) _ (cond (Nat.beq (i + 1) (i + 1)) _ _) = _
        rw [beq_ne (Nat.succ_ne_self i), beq_self]; rfl
      | isFalse e =>
        have hri : r < i := Nat.lt_of_le_of_ne h (fun e' => e e'.symm)
        rw [blt_false (Nat.succ_le_of_lt hri)]
        show cond (Nat.beq (i + 1) r) _ (cond (Nat.beq (i + 1) (r + 1)) _ _) = _
        rw [beq_ne (fun e' => Nat.lt_irrefl r (Nat.lt_trans hri (by rw [← e']; exact Nat.lt_succ_self i))),
          beq_ne (fun e' => e (Nat.succ.inj e'))]; rfl

def tp (A : Nat → Nat → R) : Nat → Nat → R := fun i j => A j i

theorem det_tp (n : Nat) (A : Nat → Nat → R) : det K n (tp A) = det K n A := LibDetTrans.det_trans K n A

def moveC (c : Nat) (A : Nat → Nat → R) : Nat → Nat → R := tp (moveR c (tp A))

theorem det_moveC (n c : Nat) (A : Nat → Nat → R) (h : c < n) : det K n (moveC c A) = K.mul (sg K c) (det K n A) := by
  show det K n (tp _) = _
  rw [det_tp, det_moveR K n c _ h, det_tp]

def sk (x i : Nat) : Nat := cond (Nat.blt i x) i (i + 1)

def del (r c : Nat) (A : Nat → Nat → R) : Nat → Nat → R := fun i j => A (sk r i) (sk c j)

/-- **Unit row:** row r = e_c ⇒ det A = (−1)^{r+c} det (del r c A). -/
theorem det_unit_row (n r c : Nat) (hr : r < n + 1) (hc : c < n + 1) (A : Nat → Nat → R)
    (h1 : A r c = K.one) (h0 : ∀ j, j ≠ c → A r j = K.zero) :
    det K (n + 1) A = K.mul (K.mul (sg K r) (sg K c)) (det K n (del r c A)) := by
  let B := moveC c (moveR r A)
  have hB : det K (n + 1) B = K.mul (sg K c) (K.mul (sg K r) (det K (n + 1) A)) := by
    show det K (n + 1) (moveC c (moveR r A)) = _
    rw [det_moveC K (n + 1) c _ hc, det_moveR K (n + 1) r A hr]
  have hrow : ∀ j, 0 < j → j < n + 1 → B 0 j = K.zero := by
    intro j hj _
    obtain ⟨j', rfl⟩ : ∃ j', j = j' + 1 := ⟨j - 1, by cases j with | zero => exact absurd hj (Nat.lt_irrefl 0) | succ j => rfl⟩
    show moveR c (tp (moveR r A)) (j' + 1) 0 = _
    rw [moveR_succ]
    show cond (Nat.blt j' c) (moveR r A 0 j') (moveR r A 0 (j' + 1)) = _
    rw [moveR_zero, moveR_zero]
    cases Nat.lt_or_ge j' c with
    | inl h => rw [blt_true h]; exact h0 j' (Nat.ne_of_lt h)
    | inr h => rw [blt_false h]; exact h0 (j' + 1) (fun e => Nat.not_succ_le_self j' (by have h' := h; rw [← e] at h'; exact h'))
  have h00 : B 0 0 = K.one := by
    show moveR c (tp (moveR r A)) 0 0 = _
    rw [moveR_zero]; show moveR r A 0 c = _; rw [moveR_zero]; exact h1
  have e := det_row0_single K n B hrow
  rw [h00] at e
  have hm : det K n (minor B 0) = det K n (del r c A) := by
    apply det_ext; intro i j _ _
    show moveR c (tp (moveR r A)) (cond (Nat.blt j 0) j (j + 1)) (i + 1) = A (sk r i) (sk c j)
    rw [blt_false (Nat.zero_le j)]
    show moveR c (tp (moveR r A)) (j + 1) (i + 1) = _
    rw [moveR_succ]
    show cond (Nat.blt j c) (moveR r A (i + 1) j) (moveR r A (i + 1) (j + 1)) = _
    rw [moveR_succ, moveR_succ]
    unfold sk
    cases Nat.lt_or_ge j c with
    | inl h =>
      rw [blt_true h]
      cases Nat.lt_or_ge i r with
      | inl h' => rw [blt_true h']; rfl
      | inr h' => rw [blt_false h']; rfl
    | inr h =>
      rw [blt_false h]
      cases Nat.lt_or_ge i r with
      | inl h' => rw [blt_true h']; rfl
      | inr h' => rw [blt_false h']; rfl
  rw [hm] at e
  have sq : ∀ m, K.mul (sg K m) (sg K m) = K.one := fun m => by
    induction m with
    | zero => show K.mul K.one K.one = K.one; lib_ring
    | succ m ih =>
      show K.mul (K.mul (sg K m) (K.neg K.one)) (K.mul (sg K m) (K.neg K.one)) = K.one
      have e2 : K.mul (K.mul (sg K m) (K.neg K.one)) (K.mul (sg K m) (K.neg K.one)) = K.mul (sg K m) (sg K m) := by
        generalize sg K m = a; lib_ring
      rw [e2, ih]
  have : det K (n + 1) A = K.mul (K.mul (sg K r) (sg K c)) (det K (n + 1) B) := by
    rw [hB]
    have h2 : K.mul (K.mul (sg K r) (sg K c)) (K.mul (sg K c) (K.mul (sg K r) (det K (n + 1) A)))
        = K.mul (K.mul (K.mul (sg K r) (sg K r)) (K.mul (sg K c) (sg K c))) (det K (n + 1) A) := by
      generalize sg K r = a; generalize sg K c = b; generalize det K (n + 1) A = d; lib_ring
    rw [h2, sq, sq]; generalize det K (n + 1) A = d; lib_ring
  rw [this, e]; generalize det K n (del r c A) = d; generalize sg K r = a; generalize sg K c = b; lib_ring

end LibUnitRow

#print axioms LibUnitRow.det_moveR
#print axioms LibUnitRow.moveR_succ
#print axioms LibUnitRow.det_unit_row

lib/UpperVand.lean

lean_all/lib/UpperVand.lean

/-
  lib/UpperVand.lean — T46 shared library (G1374 piece): upper Chu–Vandermonde in Nat.

    hockey0     Σ_(i ≤ n) C(b+i, i) = C(b+1+n, n)
    upper_vand  Σ_(i ≤ n) C(a+i, i) C(b+n−i, n−i) = C(a+b+1+n, n)
    lin_conv    Σ_(i ≤ n) (i+1)(n+1−i) = C(n+3, n)     (weights of the r = 2 self-convolution)
    conv_ext_right   pointwise-equal right factors give equal convolutions
  Convolution is LibVandermonde.conv. Lean 4 core only, proofs by hand, no axioms.
-/
import lib.Vandermonde

namespace LibUpperVand
open LibBinom LibVandermonde

/-- u a i = C(a+i, i). -/
def u (a i : Nat) : Nat := C (a + i) i

theorem C_self : ∀ n, C n n = 1
  | 0 => rfl
  | n + 1 => by
    rw [pascal, C_self n, show C n (n + 1) = 0 from C_gt n 0]

theorem u_zero (a : Nat) : u a 0 = 1 := C_zero_right (a + 0)

theorem u0 : ∀ i, u 0 i = 1
  | i => by unfold u; rw [Nat.zero_add]; exact C_self i

theorem u_succ (a : Nat) : ∀ i, u (a + 1) i = u a i + shift (u (a + 1)) i
  | 0 => by rw [u_zero, u_zero]; rfl
  | j + 1 => by
    show C (a + 1 + (j + 1)) (j + 1) = C (a + (j + 1)) (j + 1) + C (a + 1 + j) j
    rw [show a + 1 + (j + 1) = (a + 1 + j) + 1 by lib_nat, pascal, show a + (j + 1) = a + 1 + j by lib_nat, Nat.add_comm (C (a + 1 + j) j)]

theorem hockey0 (b : Nat) : ∀ n, conv (u 0) (u b) n = C (b + 1 + n) n
  | 0 => by show u 0 0 * u b 0 = C (b + 1 + 0) 0; rw [u_zero, u_zero, C_zero_right]
  | n + 1 => by
    show u 0 0 * u b (n + 1) + conv (fun i => u 0 (i + 1)) (u b) n = C (b + 1 + (n + 1)) (n + 1)
    rw [conv_ext (u b) n (fun i => u 0 (i + 1)) (u 0) (fun i => by rw [u0, u0]), hockey0 b n, u_zero, Nat.one_mul]
    show C (b + (n + 1)) (n + 1) + C (b + 1 + n) n = C (b + 1 + (n + 1)) (n + 1)
    rw [show b + 1 + (n + 1) = (b + 1 + n) + 1 by lib_nat, pascal, show b + (n + 1) = b + 1 + n by lib_nat, Nat.add_comm (C (b + 1 + n) n)]

theorem upper_vand_succ (b a : Nat) (ih : ∀ n, conv (u a) (u b) n = C (a + b + 1 + n) n) :
    ∀ n, conv (u (a + 1)) (u b) n = C (a + 1 + b + 1 + n) n
  | 0 => by show u (a + 1) 0 * u b 0 = C (a + 1 + b + 1 + 0) 0; rw [u_zero, u_zero, C_zero_right]
  | n + 1 => by
    rw [conv_ext (u b) (n + 1) (u (a + 1)) (fun i => u a i + shift (u (a + 1)) i) (u_succ a), conv_add,
      conv_shift, ih (n + 1), upper_vand_succ b a ih n]
    rw [show a + 1 + b + 1 + (n + 1) = (a + b + 1 + n + 1) + 1 by lib_nat, pascal,
      show a + b + 1 + (n + 1) = a + b + 1 + n + 1 by lib_nat, show a + 1 + b + 1 + n = a + b + 1 + n + 1 by lib_nat,
      Nat.add_comm (C (a + b + 1 + n + 1) n)]

theorem upper_vand (b : Nat) : ∀ a n, conv (u a) (u b) n = C (a + b + 1 + n) n
  | 0 => fun n => by rw [hockey0 b n, Nat.zero_add]
  | a + 1 => upper_vand_succ b a (upper_vand b a)

theorem conv_ext_right (f : Nat → Nat) : ∀ k (g g' : Nat → Nat), (∀ i, g i = g' i) → conv f g k = conv f g' k
  | 0, g, g', h => by show f 0 * g 0 = f 0 * g' 0; rw [h 0]
  | k + 1, g, g', h => by
    show f 0 * g (k + 1) + conv (fun i => f (i + 1)) g k = f 0 * g' (k + 1) + conv (fun i => f (i + 1)) g' k
    rw [h (k + 1), conv_ext_right (fun i => f (i + 1)) k g g' h]

theorem u1 : ∀ i, u 1 i = i + 1
  | 0 => rfl
  | i + 1 => by
    show C (1 + (i + 1)) (i + 1) = i + 1 + 1
    rw [show 1 + (i + 1) = (1 + i) + 1 by lib_nat, pascal]
    have h := u1 i
    unfold u at h
    rw [h, show 1 + i = i + 1 by lib_nat, C_self]

/-- Weights of the r = 2 self-convolution: Σ_(i ≤ n) (i+1)(n+1−i) = C(n+3, n). -/
theorem lin_conv (n : Nat) : conv (fun i => i + 1) (fun j => j + 1) n = C (n + 3) n := by
  rw [conv_ext (fun j => j + 1) n (fun i => i + 1) (u 1) (fun i => (u1 i).symm)]
  rw [conv_ext_right (u 1) n (fun j => j + 1) (u 1) (fun i => (u1 i).symm), upper_vand 1 1 n, show 1 + 1 + 1 + n = n + 3 by lib_nat]

end LibUpperVand

#print axioms LibUpperVand.C_self
#print axioms LibUpperVand.u_succ
#print axioms LibUpperVand.hockey0
#print axioms LibUpperVand.upper_vand
#print axioms LibUpperVand.u1
#print axioms LibUpperVand.lin_conv
#print axioms LibUpperVand.conv_ext_right
#print axioms LibUpperVand.upper_vand_succ

lib/ValMul.lean

lean_all/lib/ValMul.lean

/-
  lib/ValMul.lean — T46 shared library, cycle 117: uniqueness and multiplicativity of valuations.

    pow_add'           q^(a+b) = q^a · q^b
    vp_unique          q prime, n = q^e · m, q ∤ m (n > 0) ⇒ e = vp q n
    vp_mul             q prime, a, b > 0 ⇒ vp q (a b) = vp q a + vp q b
    vp_pow_mul         vp q (q^k · m) = k  (q ∤ m)
  (Next step after LibVal; for the 2-adic and odd-prime local arguments of agent2 BSD.)

  Lean 4 core only, proofs by hand, no axioms.
-/
import lib.Valuation
import lib.Burnside

namespace LibValMul
open LibMod LibPrimePow LibVal

theorem pow_add' (q a : Nat) : ∀ b, q ^ (a + b) = q ^ a * q ^ b
  | 0 => by show q ^ a = q ^ a * 1; lib_nat
  | b + 1 => by show q ^ (a + b) * q = q ^ a * (q ^ b * q); rw [pow_add' q a b]; lib_nat

/-- One direction: if e < e' then q ∣ m. -/
theorem lt_contra {q e e' m m' : Nat} (hq : 1 < q) (hm : ¬ q ∣ m) (h : q ^ e * m = q ^ e' * m') (hl : e < e') : False := by
  have hq0 : 0 < q := Nat.lt_trans (Nat.lt_succ_self 0) hq
  obtain ⟨d, hd⟩ := LibNatSub.le_ex hl
  have hpe : 0 < q ^ e := pow_pos hq0 e
  have e2 : m * q ^ e = (q ^ d * q * m') * q ^ e := by
    rw [show m * q ^ e = q ^ e * m by lib_nat, h, hd, show e + 1 + d = e + (d + 1) by lib_nat, pow_add']
    show q ^ e * (q ^ d * q) * m' = _; lib_nat
  have e3 := LibBurnside.nat_cancel hpe _ _ e2
  exact hm ⟨q ^ d * m', by rw [e3]; lib_nat⟩

/-- **Uniqueness of the exponent.** -/
theorem exp_eq {q e e' m m' : Nat} (hq : 1 < q) (hm : ¬ q ∣ m) (hm' : ¬ q ∣ m') (h : q ^ e * m = q ^ e' * m') : e = e' := by
  cases Nat.lt_or_ge e e' with
  | inl hl => exact absurd hl (fun hl => lt_contra hq hm h hl)
  | inr hg =>
    cases Nat.lt_or_eq_of_le hg with
    | inl hl => exact absurd hl (fun hl => lt_contra hq hm' h.symm hl)
    | inr he => exact he.symm

theorem vp_unique {q n e m : Nat} (hq : 1 < q) (hn : 0 < n) (h : n = q ^ e * m) (hm : ¬ q ∣ m) : e = vp q n := by
  have ⟨s1, s2⟩ := vp_spec hq hn
  exact exp_eq hq hm s2 (h.symm.trans s1)

/-- **Multiplicativity.** -/
theorem vp_mul {q a b : Nat} (hq : Prime q) (ha : 0 < a) (hb : 0 < b) : vp q (a * b) = vp q a + vp q b := by
  have ⟨a1, a2⟩ := vp_spec hq.1 ha
  have ⟨b1, b2⟩ := vp_spec hq.1 hb
  have hab : 0 < a * b := LibOrd.pos_mul ha hb
  refine (vp_unique hq.1 hab (m := vpRest q a * vpRest q b) ?_ ?_).symm
  · conv => lhs; rw [a1, b1]
    rw [pow_add']; lib_nat
  · intro hd
    cases euclid hq hd with
    | inl h => exact a2 h
    | inr h => exact b2 h

theorem vp_pow_mul {q k m : Nat} (hq : 1 < q) (hm : ¬ q ∣ m) (hm0 : 0 < m) : vp q (q ^ k * m) = k := by
  have hq0 : 0 < q := Nat.lt_trans (Nat.lt_succ_self 0) hq
  exact (vp_unique hq (LibOrd.pos_mul (pow_pos hq0 k) hm0) rfl hm).symm

example : vp 2 (96 * 40) = vp 2 96 + vp 2 40 := by decide

end LibValMul

#print axioms LibValMul.exp_eq
#print axioms LibValMul.vp_unique
#print axioms LibValMul.vp_mul
#print axioms LibValMul.vp_pow_mul

lib/Valuation.lean

lean_all/lib/Valuation.lean

/-
  lib/Valuation.lean — T46 shared library, cycle 116: the p-adic valuation v_p.

  For q > 1:
    qr q n              computes (n / q, n % q) in core and checks n = q c + r, r < q by Bool (otherwise LibMod's dv, md) — hence qr = (dv, md)
    vpF, vp q n         the number of times n can be divided by q, and the remaining quotient
    vp_spec             n > 0 ⇒ n = q^(vp n) · m, q ∤ m
    euclid              q prime, q ∣ ab ⇒ q ∣ a ∨ q ∣ b
    tests: vp 2 96 = 5, vp 3 162 = 4 (decide)
  (General version of agent2 BSD "2-adic valuation argument" and hodge/Fermat8.v2 (no specification).)

  Lean 4 core only, proofs by hand, no axioms.
-/
import lib.PrimePow
import lib.NatSub

namespace LibVal
open LibMod LibPrimePow

def qr (q n : Nat) : Nat × Nat :=
  cond (Nat.beq n (q * (n / q) + n % q)) (cond (Nat.blt (n % q) q) (n / q, n % q) (dv q n, md q n)) (dv q n, md q n)

theorem qr_spec {q : Nat} (_hq : 0 < q) (n : Nat) : (qr q n).1 = dv q n ∧ (qr q n).2 = md q n := by
  unfold qr
  cases h1 : Nat.beq n (q * (n / q) + n % q)
  · exact ⟨rfl, rfl⟩
  · cases h2 : Nat.blt (n % q) q
    · exact ⟨rfl, rfl⟩
    · have e := beq_true h1
      have lt : n % q < q := Nat.le_of_ble_eq_true h2
      have ⟨r1, r2⟩ := md_unique lt e
      exact ⟨r2.symm, r1.symm⟩

def vpF (q : Nat) : Nat → Nat → Nat × Nat
  | 0, n => (0, n)
  | f + 1, n =>
    cond (Nat.beq n 0) (0, n)
      (cond (Nat.beq (qr q n).2 0) ((vpF q f (qr q n).1).1 + 1, (vpF q f (qr q n).1).2) (0, n))

def vp (q n : Nat) : Nat := (vpF q n n).1
def vpRest (q n : Nat) : Nat := (vpF q n n).2

theorem pow_succ' (q e : Nat) : q ^ (e + 1) = q ^ e * q := rfl

/-- **Specification.** If fuel f ≥ n > 0, then n = q^e·m, q ∤ m. -/
theorem vpF_spec {q : Nat} (hq : 1 < q) : ∀ f n, 0 < n → n ≤ f →
    n = q ^ (vpF q f n).1 * (vpF q f n).2 ∧ ¬ q ∣ (vpF q f n).2
  | 0, n, hn, hf => absurd (Nat.lt_of_lt_of_le hn hf) (Nat.lt_irrefl 0)
  | f + 1, n, hn, hf => by
    have hq0 : 0 < q := Nat.lt_trans (Nat.lt_succ_self 0) hq
    have ⟨e1, e2⟩ := qr_spec hq0 n
    show n = q ^ (cond (Nat.beq n 0) _ _ : Nat × Nat).1 * (cond (Nat.beq n 0) _ _ : Nat × Nat).2 ∧
      ¬ q ∣ (cond (Nat.beq n 0) _ _ : Nat × Nat).2
    cases h0 : Nat.beq n 0
    · show n = q ^ (cond (Nat.beq (qr q n).2 0) _ _ : Nat × Nat).1 * (cond (Nat.beq (qr q n).2 0) _ _ : Nat × Nat).2 ∧
        ¬ q ∣ (cond (Nat.beq (qr q n).2 0) _ _ : Nat × Nat).2
      cases hr : Nat.beq (qr q n).2 0
      · -- q ∤ n
        refine ⟨by show n = 1 * n; lib_nat, fun hd => ?_⟩
        have hd' : q ∣ n := hd
        have := md_zero_of_dvd hq0 hd'
        rw [e2, this] at hr; exact Bool.noConfusion hr
      · -- n = q · c, c < n
        have hmd : md q n = 0 := by rw [← e2]; exact beq_true hr
        have hs := spec q n
        rw [hmd, ← e1] at hs
        generalize (qr q n).1 = c at hs ⊢
        have hc : 0 < c := by
          cases c with
          | zero => rw [hs] at hn; exact absurd hn (by show ¬ 0 < q * 0 + 0; rw [show q * 0 + 0 = 0 by lib_nat]; exact Nat.lt_irrefl 0)
          | succ c => exact Nat.succ_le_succ (Nat.zero_le c)
        have hcn : c < n := by
          rw [hs]
          have : c * 1 < c * q := Nat.mul_lt_mul_of_pos_left hq hc
          rw [show c * 1 = c by lib_nat] at this
          rw [show q * c + 0 = c * q by lib_nat]; exact this
        have ⟨i1, i2⟩ := vpF_spec hq f c hc (Nat.le_of_lt_succ (Nat.lt_of_lt_of_le hcn hf))
        refine ⟨?_, i2⟩
        show n = q ^ ((vpF q f c).1 + 1) * (vpF q f c).2
        rw [pow_succ', hs]
        conv => lhs; rw [i1]
        lib_nat
    · have : n = 0 := beq_true h0
      rw [this] at hn; exact absurd hn (Nat.lt_irrefl 0)

theorem vp_spec {q n : Nat} (hq : 1 < q) (hn : 0 < n) : n = q ^ vp q n * vpRest q n ∧ ¬ q ∣ vpRest q n :=
  vpF_spec hq n n hn (Nat.le_refl n)

/-- **Euclid's lemma.** -/
theorem euclid {q a b : Nat} (hq : Prime q) (h : q ∣ a * b) : q ∣ a ∨ q ∣ b := by
  have hq0 : 0 < q := Nat.lt_trans (Nat.lt_succ_self 0) hq.1
  cases dvd_dec q a hq0 with
  | inl ha => exact Or.inl ha
  | inr ha =>
    right
    exact LibGcd.gauss (cop_pow hq ha 1 |> fun c d d1 d2 => c d (by show d ∣ q ^ 0 * q; rw [show q ^ 0 * q = q by show 1 * q = q; lib_nat]; exact d1) d2)
      (by rw [show b * a = a * b by lib_nat]; exact h)

example : vp 2 96 = 5 := by decide
example : vp 3 162 = 4 := by decide
example : vpRest 2 96 = 3 := by decide

end LibVal

#print axioms LibVal.qr_spec
#print axioms LibVal.vp_spec
#print axioms LibVal.euclid

lib/VandDet.lean

lean_all/lib/VandDet.lean

/-
  lib/VandDet.lean — T46 shared library, cycle 284: Vandermonde determinant.

  Commutative ring without zero divisors, distinct points x_0, …, x_{n−1}:
    vand x i j = x_i^j
    pts x n    [x_1, …, x_n]
    vp x n     vp x (n+1) = (−1)^n Π_{i=1}^{n} (x_0 − x_i) · vp (x ∘ succ) n,  vp x 0 = 1     (= Π_{i<j} (x_j − x_i))
    vand_det   det n (vand x) = vp x n
    vand_ne    if 1 ≠ 0 then det n (vand x) ≠ 0                                              (cycle 288)
  Proof: with row 0 set to t^j, det is Σ_j c_j t^j (c_j independent of t); at t = x_i (i ≥ 1) two rows coincide (DetRowOp.det_eq_rows') so it is 0,
  hence c_n Π (t − x_i) by VandPrep.poly_roots, and the top coefficient c_n = (−1)^n det (vand (x ∘ succ)) by induction.
  Lean 4 core only, no axioms.
-/
import lib.VandPrep
import lib.DetDep

namespace LibVandDet
open DetChain LibRing LibSum LibSemi LibMat LibLegSum LibDet LibPoly LibPolyFactor LibVandPrep LibDetAlt

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

def vand (x : Nat → R) : Nat → Nat → R := fun i j => pw K (x i) j

def pts (x : Nat → R) : Nat → List R
  | 0 => []
  | n + 1 => x 1 :: pts (fun i => x (i + 1)) n

theorem len_pts : ∀ (x : Nat → R) (n : Nat), (pts x n).length = n
  | _, 0 => rfl
  | x, n + 1 => by show (pts (fun i => x (i + 1)) n).length + 1 = n + 1; rw [len_pts _ n]

theorem mem_pts : ∀ (x : Nat → R) (n : Nat) (y : R), y ∈ pts x n → ∃ i, i < n ∧ y = x (i + 1)
  | _, 0, _, h => nomatch h
  | x, n + 1, y, h => by
    cases h with
    | head => exact ⟨0, Nat.zero_lt_succ n, rfl⟩
    | tail _ h' =>
      obtain ⟨i, hi, e⟩ := mem_pts (fun i => x (i + 1)) n y h'
      exact ⟨i + 1, Nat.succ_lt_succ hi, e⟩

theorem dist_pts : ∀ (x : Nat → R) (n : Nat), (∀ i j, i < n + 1 → j < n + 1 → i ≠ j → x i ≠ x j) → Dist (pts x n)
  | _, 0, _ => trivial
  | x, n + 1, h => by
    refine ⟨fun b hb e => ?_, dist_pts (fun i => x (i + 1)) n (fun i j hi hj hij => h (i + 1) (j + 1)
      (Nat.succ_lt_succ hi) (Nat.succ_lt_succ hj) (fun e => hij (Nat.succ.inj e)))⟩
    obtain ⟨i, hi, eb⟩ := mem_pts (fun i => x (i + 1)) n b hb
    exact h 1 (i + 2) (Nat.succ_lt_succ (Nat.zero_lt_succ _)) (Nat.succ_lt_succ (Nat.succ_lt_succ hi))
      (fun e' => by have e2 : 0 = i + 1 := Nat.succ.inj e'; exact Nat.noConfusion e2) (e.symm.trans eb)

def vp : (Nat → R) → Nat → R
  | _, 0 => K.one
  | x, n + 1 => K.mul (K.mul (sg K n) (prodX K (x 0) (pts x n))) (vp (fun i => x (i + 1)) n)

/-- Points with only row 0 replaced by t. -/
def xt (x : Nat → R) (t : R) : Nat → R := fun i => Nat.casesOn i t (fun i' => x (i' + 1))

/-- **Vandermonde determinant.** -/
theorem vand_det (hK : NoZD K) : ∀ (n : Nat) (x : Nat → R), (∀ i j, i < n → j < n → i ≠ j → x i ≠ x j) →
    det K n (vand K x) = vp K x n
  | 0, _, _ => rfl
  | n + 1, x, hx => by
    let c : Nat → R := fun j => K.mul (sg K j) (det K n (minor (vand K x) j))
    -- det as a polynomial in t
    have hP : ∀ t, det K (n + 1) (vand K (xt x t)) = ev K (cl c (n + 1)) t := by
      intro t
      rw [ev_cl]
      show sum (toSemi K) (fun j => K.mul (K.mul (sg K j) (pw K t j)) (det K n (minor (vand K (xt x t)) j))) (rng (n + 1)) = _
      apply sum_ext (toSemi K); intro j
      rw [det_ext K n (minor (vand K (xt x t)) j) (minor (vand K x) j) (fun i k _ _ => rfl)]
      show _ = K.mul (K.mul (sg K j) (det K n (minor (vand K x) j))) (pw K t j); lib_ring
    have hroot : ∀ y, y ∈ pts x n → ev K (cl c (n + 1)) y = K.zero := by
      intro y hy
      obtain ⟨i, hi, ey⟩ := mem_pts x n y hy
      rw [← hP]
      exact LibDetRowOp.det_eq_rows' K (n + 1) _ 0 (i + 1) (fun e => Nat.noConfusion e) (Nat.zero_lt_succ n)
        (Nat.succ_lt_succ hi) (fun j => by rw [ey]; rfl)
    have hdist : Dist (pts x n) := dist_pts x n hx
    have hfac := poly_roots K hK c (pts x n) hdist (by rw [len_pts]; exact hroot) (x 0)
    rw [len_pts] at hfac
    have e0 : det K (n + 1) (vand K x) = det K (n + 1) (vand K (xt x (x 0))) :=
      det_ext K (n + 1) _ _ (fun i j _ _ => by cases i <;> rfl)
    rw [e0, hP, hfac]
    have htop : det K n (minor (vand K x) n) = vp K (fun i => x (i + 1)) n := by
      rw [det_ext K n (minor (vand K x) n) (vand K (fun i => x (i + 1))) (fun i k _ hk => by
        show pw K (x (i + 1)) (colMap n k) = pw K (x (i + 1)) k
        rw [colMap_lt hk])]
      exact vand_det hK n (fun i => x (i + 1)) (fun i j hi hj hij => hx (i + 1) (j + 1) (Nat.succ_lt_succ hi)
        (Nat.succ_lt_succ hj) (fun e => hij (Nat.succ.inj e)))
    show K.mul (K.mul (sg K n) (det K n (minor (vand K x) n))) (prodX K (x 0) (pts x n)) = K.mul (K.mul (sg K n) (prodX K (x 0) (pts x n))) (vp K (fun i => x (i + 1)) n)
    rw [htop]; lib_ring

theorem sg_ne (h10 : K.one ≠ K.zero) (n : Nat) : sg K n ≠ K.zero := fun e => by
  apply h10
  have := LibDisc.neg_one_sq' K n
  unfold sg at e; rw [e] at this; rw [← this]; lib_ring

theorem not_mem_pts (x : Nat → R) (n : Nat) (hx : ∀ i j, i < n + 1 → j < n + 1 → i ≠ j → x i ≠ x j) :
    ∀ y, y ∈ pts x n → y ≠ x 0 := by
  intro y hy e
  obtain ⟨i, hi, ey⟩ := mem_pts x n y hy
  exact hx 0 (i + 1) (Nat.zero_lt_succ n) (Nat.succ_lt_succ hi) (fun e' => Nat.noConfusion e') (by rw [← e, ey])

theorem vp_ne (hK : NoZD K) (h10 : K.one ≠ K.zero) : ∀ (n : Nat) (x : Nat → R),
    (∀ i j, i < n → j < n → i ≠ j → x i ≠ x j) → vp K x n ≠ K.zero
  | 0, _, _ => h10
  | n + 1, x, hx =>
    LibDisc.mul_ne K hK (LibDisc.mul_ne K hK (sg_ne K h10 n)
      (LibDisc.prodX_ne K hK h10 (x 0) (pts x n) (not_mem_pts x n hx)))
      (vp_ne hK h10 n (fun i => x (i + 1)) (fun i j hi hj hij => hx (i + 1) (j + 1) (Nat.succ_lt_succ hi)
        (Nat.succ_lt_succ hj) (fun e => hij (Nat.succ.inj e))))

/-- **The Vandermonde determinant of distinct points is nonzero.** -/
theorem vand_ne (hK : NoZD K) (h10 : K.one ≠ K.zero) (n : Nat) (x : Nat → R)
    (hx : ∀ i j, i < n → j < n → i ≠ j → x i ≠ x j) : det K n (vand K x) ≠ K.zero := by
  rw [vand_det K hK n x hx]; exact vp_ne K hK h10 n x hx

end LibVandDet

#print axioms LibVandDet.dist_pts
#print axioms LibVandDet.vand_det
#print axioms LibVandDet.vp_ne
#print axioms LibVandDet.vand_ne

lib/VandPrep.lean

lean_all/lib/VandPrep.lean

/-
  lib/VandPrep.lean — T46 shared library, cycle 282: preparation for the Vandermonde determinant — the end of the coefficient list.

    addP_long      |q| ≤ |a| ⇒ addP (a ++ [x]) q = addP a q ++ [x]
    addP_snoc      |a| = |b| ⇒ addP (a ++ [x]) (b ++ [y]) = addP a b ++ [x + y]
    len_addP_eq    |p| = |q| ⇒ |addP p q| = |p|
    mulC_snoc      mulC c (a ++ [x]) = mulC c a ++ [c x]
    bas_snoc       bas l = b' ++ [1],  |b'| = |l|                 (Π (X − r) is monic)
    ev_snoc        ev (a ++ [x]) t = ev a t + x·t^|a|
    cl c n         [c 0, …, c (n−1)];  ev_cl: ev (cl c n) t = Σ_{j<n} c_j t^j
    poly_roots     NoZD, distinct xs (|xs| = n) all roots of Σ_{j≤n} c_j t^j ⇒ for all t, Σ c_j t^j = c_n Π (t − x)   (cycle 283)
  Lean 4 core only, no axioms.
-/
import lib.Interp

namespace LibVandPrep
open DetChain LibRing LibSum LibSemi LibPoly LibPolyFactor LibInterp

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

theorem addP_long (x : R) : ∀ (a q : List R), q.length ≤ a.length → addP K (a ++ [x]) q = addP K a q ++ [x]
  | [], [], _ => rfl
  | [], _ :: _, h => absurd h (Nat.not_succ_le_zero _)
  | _ :: _, [], _ => rfl
  | a0 :: a, q0 :: q, h => by
    show K.add a0 q0 :: addP K (a ++ [x]) q = K.add a0 q0 :: addP K a q ++ [x]
    rw [addP_long x a q (Nat.le_of_succ_le_succ h)]; rfl

theorem addP_snoc (x y : R) : ∀ (a b : List R), a.length = b.length → addP K (a ++ [x]) (b ++ [y]) = addP K a b ++ [K.add x y]
  | [], [], _ => rfl
  | [], _ :: _, h => Nat.noConfusion h
  | _ :: _, [], h => Nat.noConfusion h
  | a0 :: a, b0 :: b, h => by
    show K.add a0 b0 :: addP K (a ++ [x]) (b ++ [y]) = K.add a0 b0 :: addP K a b ++ [K.add x y]
    rw [addP_snoc x y a b (Nat.succ.inj h)]; rfl

theorem len_addP_eq : ∀ (p q : List R), p.length = q.length → (addP K p q).length = p.length
  | [], [], _ => rfl
  | [], _ :: _, h => Nat.noConfusion h
  | _ :: _, [], h => Nat.noConfusion h
  | _ :: p, _ :: q, h => by
    show (addP K p q).length + 1 = p.length + 1
    rw [len_addP_eq p q (Nat.succ.inj h)]

theorem mulC_snoc (c x : R) : ∀ a : List R, mulC K c (a ++ [x]) = mulC K c a ++ [K.mul c x]
  | [] => rfl
  | a0 :: a => by
    show K.mul c a0 :: mulC K c (a ++ [x]) = K.mul c a0 :: mulC K c a ++ [K.mul c x]
    rw [mulC_snoc c x a]; rfl

theorem bas_snoc : ∀ l : List R, ∃ b', bas K l = b' ++ [K.one] ∧ b'.length = l.length
  | [] => ⟨[], rfl, rfl⟩
  | r :: l => by
    obtain ⟨b', e, hl⟩ := bas_snoc l
    refine ⟨addP K (K.zero :: b') (mulC K (K.neg r) (b' ++ [K.one])), ?_, ?_⟩
    · show addP K (K.zero :: bas K l) (mulC K (K.neg r) (bas K l)) = _
      rw [e]
      exact addP_long K K.one (K.zero :: b') _ (by rw [len_mulC, LibEulerFF.len_app]; show b'.length + 1 ≤ b'.length + 1; exact Nat.le_refl _)
    · rw [len_addP_eq K _ _ (by rw [len_mulC, LibEulerFF.len_app]; rfl)]
      show b'.length + 1 = l.length + 1; rw [hl]

theorem ev_snoc (x t : R) : ∀ a : List R, ev K (a ++ [x]) t = K.add (ev K a t) (K.mul x (pw K t a.length))
  | [] => by show K.add x (K.mul t K.zero) = K.add K.zero (K.mul x K.one); lib_ring
  | a0 :: a => by
    show K.add a0 (K.mul t (ev K (a ++ [x]) t)) = K.add (K.add a0 (K.mul t (ev K a t))) (K.mul x (K.mul (pw K t a.length) t))
    rw [ev_snoc x t a]; lib_ring

def cl (c : Nat → R) (n : Nat) : List R := (rng n).map c

theorem len_cl (c : Nat → R) (n : Nat) : (cl c n).length = n := by
  unfold cl; rw [LibEulerFF.len_map]
  induction n with
  | zero => rfl
  | succ n ih => show (rng n ++ [n]).length = n + 1; rw [LibEulerFF.len_app, ih]; rfl

theorem cl_succ (c : Nat → R) (n : Nat) : cl c (n + 1) = cl c n ++ [c n] := by
  unfold cl; show (rng n ++ [n]).map c = _
  induction (rng n) with
  | nil => rfl
  | cons a l ih => show c a :: (l ++ [n]).map c = c a :: l.map c ++ [c n]; rw [ih]; rfl

theorem ev_cl (c : Nat → R) (t : R) : ∀ n, ev K (cl c n) t = sum (toSemi K) (fun j => K.mul (c j) (pw K t j)) (rng n)
  | 0 => rfl
  | n + 1 => by
    rw [cl_succ, ev_snoc, ev_cl c t n, len_cl, sum_rng_succ]; rfl

/-- **n roots ⇒ factorization.** -/
theorem poly_roots (hK : NoZD K) (c : Nat → R) (xs : List R) (hd : Dist xs)
    (hr : ∀ x, x ∈ xs → ev K (cl c (xs.length + 1)) x = K.zero) (t : R) :
    ev K (cl c (xs.length + 1)) t = K.mul (c xs.length) (prodX K t xs) := by
  obtain ⟨b', eb, hb⟩ := bas_snoc K xs
  let n := xs.length
  let D' := addP K (cl c n) (mulC K (K.neg (c n)) b')
  have hD : ∀ s, ev K D' s = K.add (ev K (cl c (n + 1)) s) (K.neg (K.mul (c n) (prodX K s xs))) := by
    intro s
    have e1 : ev K (cl c (n + 1)) s = K.add (ev K (cl c n) s) (K.mul (c n) (pw K s n)) := by
      rw [cl_succ, ev_snoc, len_cl]
    have e2 : prodX K s xs = K.add (ev K b' s) (K.mul K.one (pw K s n)) := by
      rw [← ev_bas K s xs, eb, ev_snoc, hb]
    show ev K (addP K (cl c n) (mulC K (K.neg (c n)) b')) s = _
    rw [ev_addP, ev_mulC, e1, e2]; lib_ring
  have hlen : D'.length ≤ xs.length :=
    len_addP_le K _ _ _ (by rw [len_cl]; exact Nat.le_refl _) (by rw [len_mulC, hb]; exact Nat.le_refl _)
  have hz : ∀ x, x ∈ xs → ev K D' x = K.zero := by
    intro x hx; rw [hD, hr x hx, LibDisc.prodX_root K x xs hx]; lib_ring
  have hA := allZ_of_roots K hK xs D' hd hlen hz
  have e := ev_allZ K D' hA t
  rw [hD] at e
  have : ev K (cl c (n + 1)) t = K.add (K.add (ev K (cl c (n + 1)) t) (K.neg (K.mul (c n) (prodX K t xs)))) (K.mul (c n) (prodX K t xs)) := by lib_ring
  rw [this, e]; lib_ring

end LibVandPrep

#print axioms LibVandPrep.addP_long
#print axioms LibVandPrep.addP_snoc
#print axioms LibVandPrep.bas_snoc
#print axioms LibVandPrep.ev_snoc
#print axioms LibVandPrep.ev_cl
#print axioms LibVandPrep.poly_roots

lib/Vandermonde.lean

lean_all/lib/Vandermonde.lean

/-
  lib/Vandermonde.lean — T46 shared library, cycle 231: Vandermonde's identity.

    conv f g k     Σ_{i ≤ k} f(i) g(k − i)   (subtraction-free recursion: f(0) g(k+1) + conv (f ∘ succ) g k)
    conv_add, conv_ext, conv_shift, conv_zero_left    basic properties of convolution
    conv_sum       conv f g k = Σ_{i < k+1} f(i) g(k − i)       (as a LibSum sum)
    vandermonde    conv (C m) (C n) k = C(m + n, k)
    central_sq     conv (C n) (C n) n = C(2n, n)
  Lean 4 core only, no axioms.
-/
import lib.Binom

namespace LibVandermonde
open LibBinom LibSum LibSemi

def conv (f g : Nat → Nat) : Nat → Nat
  | 0 => f 0 * g 0
  | k + 1 => f 0 * g (k + 1) + conv (fun i => f (i + 1)) g k

theorem conv_ext (g : Nat → Nat) : ∀ k (f f' : Nat → Nat), (∀ i, f i = f' i) → conv f g k = conv f' g k
  | 0, f, f', h => by show f 0 * g 0 = f' 0 * g 0; rw [h 0]
  | k + 1, f, f', h => by
    show f 0 * g (k + 1) + conv (fun i => f (i + 1)) g k = f' 0 * g (k + 1) + conv (fun i => f' (i + 1)) g k
    rw [h 0, conv_ext g k _ _ (fun i => h (i + 1))]

theorem conv_add (g : Nat → Nat) : ∀ k (f f' : Nat → Nat),
    conv (fun i => f i + f' i) g k = conv f g k + conv f' g k
  | 0, f, f' => by show (f 0 + f' 0) * g 0 = f 0 * g 0 + f' 0 * g 0; lib_nat
  | k + 1, f, f' => by
    show (f 0 + f' 0) * g (k + 1) + conv (fun i => f (i + 1) + f' (i + 1)) g k
      = (f 0 * g (k + 1) + conv (fun i => f (i + 1)) g k) + (f' 0 * g (k + 1) + conv (fun i => f' (i + 1)) g k)
    rw [conv_add g k (fun i => f (i + 1)) (fun i => f' (i + 1))]
    generalize conv (fun i => f (i + 1)) g k = A
    generalize conv (fun i => f' (i + 1)) g k = B
    lib_nat

theorem conv_zero_left (g : Nat → Nat) : ∀ k (f : Nat → Nat), (∀ i, f i = 0) → conv f g k = 0
  | 0, f, h => by show f 0 * g 0 = 0; rw [h 0]; exact Nat.zero_mul _
  | k + 1, f, h => by
    show f 0 * g (k + 1) + conv (fun i => f (i + 1)) g k = 0
    rw [h 0, conv_zero_left g k _ (fun i => h (i + 1))]; lib_nat

/-- f shifted by one: shift f 0 = 0, shift f (i+1) = f i. -/
def shift (f : Nat → Nat) : Nat → Nat
  | 0 => 0
  | i + 1 => f i

theorem conv_shift (f g : Nat → Nat) (k : Nat) : conv (shift f) g (k + 1) = conv f g k := by
  show 0 * g (k + 1) + conv (fun i => shift f (i + 1)) g k = conv f g k
  rw [conv_ext g k (fun i => shift f (i + 1)) f (fun _ => rfl)]; lib_nat

theorem conv_sum (f g : Nat → Nat) : ∀ k, conv f g k = sum natS (fun i => f i * g (k - i)) (rng (k + 1))
  | 0 => rfl
  | k + 1 => by
    show f 0 * g (k + 1) + conv (fun i => f (i + 1)) g k = sum natS (fun i => f i * g (k + 1 - i)) (rng (k + 1 + 1))
    rw [conv_sum (fun i => f (i + 1)) g k]
    have e : rng (k + 1 + 1) = rng 1 ++ (rng (k + 1)).map (fun i => 1 + i) := by
      rw [show k + 1 + 1 = 1 + (k + 1) by lib_nat]; exact rng_add 1 (k + 1)
    rw [e, sum_append natS, sum_map natS]
    have e' : ∀ i, f (1 + i) * g (k + 1 - (1 + i)) = f (i + 1) * g (k - i) := fun i => by
      rw [show 1 + i = i + 1 by lib_nat, Nat.succ_sub_succ]
    rw [sum_ext natS (fun i => f (1 + i) * g (k + 1 - (1 + i))) _ e']
    rfl

/-- **Vandermonde.** Σ_{i ≤ k} C(m, i) C(n, k − i) = C(m + n, k). -/
theorem vandermonde (n : Nat) : ∀ m k, conv (C m) (C n) k = C (m + n) k
  | 0, k => by
    cases k with
    | zero => show C 0 0 * C n 0 = C (0 + n) 0; rw [C_zero_right, C_zero_right, C_zero_right]
    | succ k =>
      show C 0 0 * C n (k + 1) + conv (fun i => C 0 (i + 1)) (C n) k = C (0 + n) (k + 1)
      rw [conv_zero_left (C n) k (fun i => C 0 (i + 1)) (fun _ => rfl), show 0 + n = n by lib_nat]
      show 1 * C n (k + 1) + 0 = C n (k + 1); lib_nat
  | m + 1, 0 => by
    show C (m + 1) 0 * C n 0 = C (m + 1 + n) 0; rw [C_zero_right, C_zero_right, C_zero_right]
  | m + 1, k + 1 => by
    have hC : ∀ i, C (m + 1) i = shift (C m) i + C m i := fun i => by
      cases i with
      | zero => show C (m + 1) 0 = 0 + C m 0; rw [C_zero_right, C_zero_right]
      | succ i => show C m i + C m (i + 1) = C m i + C m (i + 1); rfl
    rw [conv_ext (C n) (k + 1) _ _ hC, conv_add, conv_shift, vandermonde n m k, vandermonde n m (k + 1),
      show m + 1 + n = (m + n) + 1 by lib_nat]
    rfl

theorem central_sq (n : Nat) : conv (C n) (C n) n = C (2 * n) n := by
  rw [vandermonde n n n, show n + n = 2 * n by lib_nat]

end LibVandermonde

#print axioms LibVandermonde.conv_ext
#print axioms LibVandermonde.conv_add
#print axioms LibVandermonde.conv_zero_left
#print axioms LibVandermonde.conv_shift
#print axioms LibVandermonde.conv_sum
#print axioms LibVandermonde.vandermonde
#print axioms LibVandermonde.central_sq

lib/VarMin.lean

lean_all/lib/VarMin.lean

/-
  lib/VarMin.lean — T46 shared library: the mean minimizes the sum of squares (ordered ring, no sign conditions).

    var_min   for every c:  Σ_{i<j}(x_i − x_j)² ≤ n · Σ_i (x_i + c)²
  Lean 4 core only, proofs by hand, no axioms.
-/
import lib.PairSqShift
import lib.PairSqBound
import lib.PsumPowMeanJ

namespace LibVarMin
open DetChain LibRing LibNewton YMChain LibPsumVar

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

theorem var_min (c : R) (l : List R) :
    O.le (pairSq K l) (K.mul (castN K l.length) (psum K (l.map (fun x => K.add x c)) 2)) := by
  have h := LibPairSqBound.pairSq_le K O (l.map (fun x => K.add x c))
  rw [LibPairSqShift.pairSq_shift K c l, LibPsumPowMeanJ.length_map_] at h
  exact h

end LibVarMin

#print axioms LibVarMin.var_min

lib/VarSum.lean

lean_all/lib/VarSum.lean

/-
  lib/VarSum.lean — T46 shared library: spread of a sum and of a difference (any commutative ring).

    pairSq_sum    pairSq (x + y) = pairSq x + pairSq y + 2 covPair
    pairSq_diff   pairSq (x − y) = pairSq x + pairSq y − 2 covPair
  for a list of pairs (x_i, y_i).
  Lean 4 core only, proofs by hand, no axioms.
-/
import lib.CovId
import lib.PsumVar

namespace LibVarSum
open DetChain LibRing LibCovId LibPsumVar

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

theorem rowSq_sum (a : R × R) : ∀ l : List (R × R),
    rowSq K (K.add a.1 a.2) (l.map (fun p => K.add p.1 p.2)) =
      K.add (K.add (rowSq K a.1 (l.map Prod.fst)) (rowSq K a.2 (l.map Prod.snd))) (K.add (covRow K a l) (covRow K a l))
  | [] => by show K.zero = K.add (K.add K.zero K.zero) (K.add K.zero K.zero); lib_ring
  | b :: l => by
    show K.add (K.mul (sub K (K.add a.1 a.2) (K.add b.1 b.2)) (sub K (K.add a.1 a.2) (K.add b.1 b.2)))
        (rowSq K (K.add a.1 a.2) (l.map (fun p => K.add p.1 p.2))) =
      K.add (K.add (K.add (K.mul (sub K a.1 b.1) (sub K a.1 b.1)) (rowSq K a.1 (l.map Prod.fst)))
          (K.add (K.mul (sub K a.2 b.2) (sub K a.2 b.2)) (rowSq K a.2 (l.map Prod.snd))))
        (K.add (K.add (K.mul (sub K a.1 b.1) (sub K a.2 b.2)) (covRow K a l))
          (K.add (K.mul (sub K a.1 b.1) (sub K a.2 b.2)) (covRow K a l)))
    rw [rowSq_sum a l]; unfold sub; lib_ring

theorem pairSq_sum : ∀ l : List (R × R),
    pairSq K (l.map (fun p => K.add p.1 p.2)) =
      K.add (K.add (pairSq K (l.map Prod.fst)) (pairSq K (l.map Prod.snd))) (K.add (covPair K l) (covPair K l))
  | [] => by show K.zero = K.add (K.add K.zero K.zero) (K.add K.zero K.zero); lib_ring
  | a :: l => by
    show K.add (rowSq K (K.add a.1 a.2) (l.map (fun p => K.add p.1 p.2))) (pairSq K (l.map (fun p => K.add p.1 p.2))) =
      K.add (K.add (K.add (rowSq K a.1 (l.map Prod.fst)) (pairSq K (l.map Prod.fst)))
          (K.add (rowSq K a.2 (l.map Prod.snd)) (pairSq K (l.map Prod.snd))))
        (K.add (K.add (covRow K a l) (covPair K l)) (K.add (covRow K a l) (covPair K l)))
    rw [rowSq_sum K a l, pairSq_sum l]; lib_ring

theorem rowSq_diff (a : R × R) : ∀ l : List (R × R),
    rowSq K (sub K a.1 a.2) (l.map (fun p => sub K p.1 p.2)) =
      K.add (K.add (rowSq K a.1 (l.map Prod.fst)) (rowSq K a.2 (l.map Prod.snd)))
        (K.neg (K.add (covRow K a l) (covRow K a l)))
  | [] => by show K.zero = K.add (K.add K.zero K.zero) (K.neg (K.add K.zero K.zero)); lib_ring
  | b :: l => by
    show K.add (K.mul (sub K (sub K a.1 a.2) (sub K b.1 b.2)) (sub K (sub K a.1 a.2) (sub K b.1 b.2)))
        (rowSq K (sub K a.1 a.2) (l.map (fun p => sub K p.1 p.2))) =
      K.add (K.add (K.add (K.mul (sub K a.1 b.1) (sub K a.1 b.1)) (rowSq K a.1 (l.map Prod.fst)))
          (K.add (K.mul (sub K a.2 b.2) (sub K a.2 b.2)) (rowSq K a.2 (l.map Prod.snd))))
        (K.neg (K.add (K.add (K.mul (sub K a.1 b.1) (sub K a.2 b.2)) (covRow K a l))
          (K.add (K.mul (sub K a.1 b.1) (sub K a.2 b.2)) (covRow K a l))))
    rw [rowSq_diff a l]; unfold sub; lib_ring

theorem pairSq_diff : ∀ l : List (R × R),
    pairSq K (l.map (fun p => sub K p.1 p.2)) =
      K.add (K.add (pairSq K (l.map Prod.fst)) (pairSq K (l.map Prod.snd))) (K.neg (K.add (covPair K l) (covPair K l)))
  | [] => by show K.zero = K.add (K.add K.zero K.zero) (K.neg (K.add K.zero K.zero)); lib_ring
  | a :: l => by
    show K.add (rowSq K (sub K a.1 a.2) (l.map (fun p => sub K p.1 p.2))) (pairSq K (l.map (fun p => sub K p.1 p.2))) =
      K.add (K.add (K.add (rowSq K a.1 (l.map Prod.fst)) (pairSq K (l.map Prod.fst)))
          (K.add (rowSq K a.2 (l.map Prod.snd)) (pairSq K (l.map Prod.snd))))
        (K.neg (K.add (K.add (covRow K a l) (covPair K l)) (K.add (covRow K a l) (covPair K l))))
    rw [rowSq_diff K a l, pairSq_diff l]; lib_ring

end LibVarSum

#print axioms LibVarSum.pairSq_sum
#print axioms LibVarSum.pairSq_diff

lib/VarSumLe.lean

lean_all/lib/VarSumLe.lean

/-
  lib/VarSumLe.lean — T46 shared library: bounds for the spread of a sum (ordered ring, no sign conditions).

    cov2_le       2 covPair ≤ pairSq x + pairSq y
    pairSq_sum_le pairSq (x + y) ≤ 2 (pairSq x + pairSq y)
  Lean 4 core only, proofs by hand, no axioms.
-/
import lib.VarSum
import lib.Esym2Var

namespace LibVarSumLe
open DetChain LibRing YMChain LibPascalLC LibCovId LibPsumVar

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

theorem cov2_le (l : List (R × R)) :
    O.le (K.add (covPair K l) (covPair K l)) (K.add (pairSq K (l.map Prod.fst)) (pairSq K (l.map Prod.snd))) := by
  have h := LibEsym2Var.pairSq_nn K O (l.map (fun p => sub K p.1 p.2))
  rw [LibVarSum.pairSq_diff K l] at h
  exact congr_le K O (by lib_ring) (by lib_ring)
    (O.add_le_add _ _ _ _ h (O.le_refl (K.add (covPair K l) (covPair K l))))

theorem pairSq_sum_le (l : List (R × R)) :
    O.le (pairSq K (l.map (fun p => K.add p.1 p.2)))
      (K.add (K.add (pairSq K (l.map Prod.fst)) (pairSq K (l.map Prod.snd)))
        (K.add (pairSq K (l.map Prod.fst)) (pairSq K (l.map Prod.snd)))) := by
  rw [LibVarSum.pairSq_sum K l]
  exact O.add_le_add _ _ _ _ (O.le_refl _) (cov2_le K O l)

end LibVarSumLe

#print axioms LibVarSumLe.cov2_le
#print axioms LibVarSumLe.pairSq_sum_le

lib/VecDep.lean

lean_all/lib/VecDep.lean

/-
  lib/VecDep.lean — T46 shared library, cycle 341 (G16): n + 1 vectors in Kⁿ are linearly dependent (field).

    det_col_zero     column k is 0 ⇒ det = 0
    dep_of_more      rows 0..n (n + 1 of them), columns 0..n−1 ⇒ ∃ x ≠ 0, Σ_{i≤n} x_i A_ij = 0 (j < n)
  (DetKernel2.ker applied to the (n+1)×(n+1) matrix with a zero column appended.)
  Lean 4 core only, no axioms.
-/
import lib.DetKernel2

namespace LibVecDep
open DetChain LibRing LibSum LibSemi LibMat LibLegSum LibDet

variable {R : Type} [DecidableEq R] (K : CRing R)

omit [DecidableEq R] in
theorem det_col_zero (n : Nat) (A : Nat → Nat → R) (k : Nat) (hk : k < n) (h : ∀ r, A r k = K.zero) : det K n A = K.zero := by
  have := LibDetCol.det_col_smul K n A A k K.zero hk (fun r => by rw [h r]; lib_ring) (fun _ _ _ => rfl)
  rw [this]; lib_ring

variable (hK : NoZD K) (h10 : K.one ≠ K.zero) (inv : R → R) (hinv : ∀ a, a ≠ K.zero → K.mul a (inv a) = K.one)

include hK h10 hinv in
/-- **n + 1 vectors (of length n) are linearly dependent.** -/
theorem dep_of_more (n : Nat) (A : Nat → Nat → R) :
    ∃ x : Nat → R, (∃ k, k < n + 1 ∧ x k ≠ K.zero) ∧
      ∀ j, j < n → sum (toSemi K) (fun i => K.mul (x i) (A i j)) (rng (n + 1)) = K.zero := by
  let A' : Nat → Nat → R := fun i j => cond (Nat.blt j n) (A i j) K.zero
  have hd : det K (n + 1) A' = K.zero := det_col_zero K (n + 1) A' n (Nat.lt_succ_self n) (fun r => by
    show cond (Nat.blt n n) _ _ = _; rw [LibDetAlt.blt_false (Nat.le_refl n)]; rfl)
  obtain ⟨x, hx0, hx⟩ := LibDetKernel2.ker K hK h10 inv hinv (n + 1) A' hd
  refine ⟨x, hx0, fun j hj => ?_⟩
  refine Eq.trans ?_ (hx j (Nat.lt_succ_of_lt hj))
  apply sum_ext (toSemi K); intro i
  show _ = K.mul (x i) (cond (Nat.blt j n) (A i j) K.zero)
  rw [LibDetAlt.blt_true hj]; rfl

end LibVecDep

#print axioms LibVecDep.det_col_zero
#print axioms LibVecDep.dep_of_more

lib/Vieta3.lean

lean_all/lib/Vieta3.lean

/-
  lib/Vieta3.lean — T46 shared library, cycle 3: factor theorem / Vieta for cubics (over an integral domain).
  (REQUESTS.md: agent2, needed for the descent exact sequence)

  K a commutative ring without zero divisors (DetChain.NoZD K), f(t) = t³ + c₂t² + c₁t + c₀,
  if distinct x₁, x₂, x₃ are all roots then
      c₂ = −(x₁+x₂+x₃),   c₁ = x₁x₂ + x₁x₃ + x₂x₃,   c₀ = −x₁x₂x₃           (vieta3)
  and f(t) = (t−x₁)(t−x₂)(t−x₃) holds for all t (factor3).
  Bonus, quadratics: two distinct roots ⇒ c₁ = −(x₁+x₂), c₀ = x₁x₂ (vieta2).
  Tools: sub_zero_eq (a − b = 0 ⇒ a = b), sub_ne_zero, cancel_left (a ≠ 0, ab = 0 ⇒ b = 0).
  All ring identities via `lib_ring` (lib/RingTac).

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

namespace LibVieta
open DetChain LibRing

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

/-- Value of a monic cubic. -/
def cubic (c2 c1 c0 t : R) : R :=
  K.add (K.add (K.add (pw K t 3) (K.mul c2 (pw K t 2))) (K.mul c1 t)) c0

/-- Value of a monic quadratic. -/
def quad (c1 c0 t : R) : R := K.add (K.add (pw K t 2) (K.mul c1 t)) c0

theorem sub_zero_eq (a b : R) (h : sub K a b = K.zero) : a = b := by
  have e : a = K.add (sub K a b) b := by lib_ring
  rw [e, h, K.zero_add]

theorem sub_ne_zero (a b : R) (h : a ≠ b) : sub K a b ≠ K.zero := fun h' => h (sub_zero_eq K a b h')

theorem cancel_left (hK : NoZD K) (a b : R) (ha : a ≠ K.zero) (h : K.mul a b = K.zero) : b = K.zero := by
  cases hK a b h with
  | inl h1 => exact absurd h1 ha
  | inr h2 => exact h2

theorem eq_zero_of (a b : R) (e : a = b) (hb : b = K.zero) : a = K.zero := e.trans hb

/-- Difference at two roots: f(x) − f(y) = (x − y)·g(x, y). -/
theorem cubic_diff (c2 c1 c0 x y : R) :
    sub K (cubic K c2 c1 c0 x) (cubic K c2 c1 c0 y)
      = K.mul (sub K x y)
          (K.add (K.add (K.add (K.add (K.mul x x) (K.mul x y)) (K.mul y y)) (K.mul c2 (K.add x y))) c1) := by
  unfold cubic; lib_ring

/-- **Vieta (cubic).** -/
theorem vieta3 (hK : NoZD K) (c2 c1 c0 x1 x2 x3 : R)
    (h12 : x1 ≠ x2) (h13 : x1 ≠ x3) (h23 : x2 ≠ x3)
    (f1 : cubic K c2 c1 c0 x1 = K.zero) (f2 : cubic K c2 c1 c0 x2 = K.zero) (f3 : cubic K c2 c1 c0 x3 = K.zero) :
    c2 = K.neg (K.add (K.add x1 x2) x3)
    ∧ c1 = K.add (K.add (K.mul x1 x2) (K.mul x1 x3)) (K.mul x2 x3)
    ∧ c0 = K.neg (K.mul (K.mul x1 x2) x3) := by
  have zz : sub K K.zero K.zero = K.zero := by lib_ring
  -- g₁₂ = 0, g₁₃ = 0
  have g12 := cancel_left K hK _ _ (sub_ne_zero K _ _ h12)
    (((cubic_diff K c2 c1 c0 x1 x2).symm.trans (by rw [f1, f2])).trans zz)
  have g13 := cancel_left K hK _ _ (sub_ne_zero K _ _ h13)
    (((cubic_diff K c2 c1 c0 x1 x3).symm.trans (by rw [f1, f3])).trans zz)
  -- s = c₂ + (x₁+x₂+x₃) = 0
  have es : K.mul (sub K x2 x3) (K.add c2 (K.add (K.add x1 x2) x3))
      = sub K (K.add (K.add (K.add (K.add (K.mul x1 x1) (K.mul x1 x2)) (K.mul x2 x2)) (K.mul c2 (K.add x1 x2))) c1)
              (K.add (K.add (K.add (K.add (K.mul x1 x1) (K.mul x1 x3)) (K.mul x3 x3)) (K.mul c2 (K.add x1 x3))) c1) := by
    lib_ring
  rw [g12, g13, zz] at es
  have hs := cancel_left K hK _ _ (sub_ne_zero K _ _ h23) es
  -- t = c₁ − e₂ = 0
  have et : sub K c1 (K.add (K.add (K.mul x1 x2) (K.mul x1 x3)) (K.mul x2 x3))
      = sub K (K.add (K.add (K.add (K.add (K.mul x1 x1) (K.mul x1 x2)) (K.mul x2 x2)) (K.mul c2 (K.add x1 x2))) c1)
              (K.mul (K.add c2 (K.add (K.add x1 x2) x3)) (K.add x1 x2)) := by
    lib_ring
  rw [g12, hs, zero_mul' K, zz] at et
  -- c₀ + e₃ = 0
  have eu : K.add c0 (K.mul (K.mul x1 x2) x3)
      = sub K (sub K (cubic K c2 c1 c0 x1) (K.mul (K.add c2 (K.add (K.add x1 x2) x3)) (pw K x1 2)))
              (K.mul (sub K c1 (K.add (K.add (K.mul x1 x2) (K.mul x1 x3)) (K.mul x2 x3))) x1) := by
    unfold cubic; lib_ring
  rw [f1, hs, et, zero_mul' K, zero_mul' K, zz, zz] at eu
  refine ⟨neg_unique K _ _ hs, sub_zero_eq K _ _ et, neg_unique K _ _ eu⟩

/-- **Factor theorem (cubic).** If the three roots are distinct then f(t) = (t − x₁)(t − x₂)(t − x₃). -/
theorem factor3 (hK : NoZD K) (c2 c1 c0 x1 x2 x3 : R)
    (h12 : x1 ≠ x2) (h13 : x1 ≠ x3) (h23 : x2 ≠ x3)
    (f1 : cubic K c2 c1 c0 x1 = K.zero) (f2 : cubic K c2 c1 c0 x2 = K.zero) (f3 : cubic K c2 c1 c0 x3 = K.zero)
    (t : R) : cubic K c2 c1 c0 t = K.mul (K.mul (sub K t x1) (sub K t x2)) (sub K t x3) := by
  have ⟨e2, e1, e0⟩ := vieta3 K hK c2 c1 c0 x1 x2 x3 h12 h13 h23 f1 f2 f3
  unfold cubic; rw [e2, e1, e0]; lib_ring

/-- **Vieta (quadratic).** -/
theorem vieta2 (hK : NoZD K) (c1 c0 x1 x2 : R) (h12 : x1 ≠ x2)
    (f1 : quad K c1 c0 x1 = K.zero) (f2 : quad K c1 c0 x2 = K.zero) :
    c1 = K.neg (K.add x1 x2) ∧ c0 = K.mul x1 x2 := by
  have zz : sub K K.zero K.zero = K.zero := by lib_ring
  have ed : K.mul (sub K x1 x2) (K.add c1 (K.add x1 x2)) = sub K (quad K c1 c0 x1) (quad K c1 c0 x2) := by
    unfold quad; lib_ring
  rw [f1, f2, zz] at ed
  have hs := cancel_left K hK _ _ (sub_ne_zero K _ _ h12) ed
  have eu : sub K c0 (K.mul x1 x2) = sub K (quad K c1 c0 x1) (K.mul (K.add c1 (K.add x1 x2)) x1) := by
    unfold quad; lib_ring
  rw [f1, hs, zero_mul' K, zz] at eu
  exact ⟨neg_unique K _ _ hs, sub_zero_eq K _ _ eu⟩

end LibVieta

#print axioms LibVieta.vieta3
#print axioms LibVieta.factor3
#print axioms LibVieta.vieta2
#print axioms LibVieta.cubic_diff

lib/VietaEsym.lean

lean_all/lib/VietaEsym.lean

/-
  lib/VietaEsym.lean — T46 shared library, cycle 435 (G97): coefficients of Π(x − r_i) = signed elementary symmetric polynomials.

  Any commutative ring:
    Lr r m          [r_{m−1}, …, r_0]   (Lr (m+1) = r_m :: Lr m)
    len_Lr          length m
    pl_esym         j + k = m ⇒ pl r m j = (−1)^k e_k(r_0, …, r_{m−1})     (VietaGen.esym, VietaFull.pl)
    vieta_esym      field, distinct roots: c_j = (−1)^k e_k(r)  (j + k = n + 1)  — Vieta's original form
  Lean 4 core only, no axioms.
-/
import lib.Circ2
import lib.VietaGen

namespace LibVietaEsym
open DetChain LibRing LibSum LibSemi LibLegSum LibDet LibVietaGen LibVietaFull

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

def Lr (r : Nat → R) : Nat → List R
  | 0 => []
  | m + 1 => r m :: Lr r m

theorem len_Lr (r : Nat → R) : ∀ m, (Lr r m).length = m
  | 0 => rfl
  | m + 1 => by show (Lr r m).length + 1 = m + 1; rw [len_Lr r m]

theorem pl_esym (r : Nat → R) : ∀ m j k, j + k = m → pl K r m j = K.mul (sg K k) (esym K (Lr r m) k)
  | 0, j, k, h => by
    have hj : j = 0 := Nat.le_antisymm (h ▸ Nat.le_add_right j k) (Nat.zero_le _)
    have hk : k = 0 := Nat.le_antisymm (h ▸ Nat.le_add_left k j) (Nat.zero_le _)
    subst hj; subst hk
    show indK (toSemi K) (decide ((0 : Nat) = 0)) = K.mul K.one K.one
    rw [decide_eq_true rfl]; show K.one = _; lib_ring
  | m + 1, 0, k, h => by
    have hk : k = m + 1 := by rw [← h]; lib_nat
    subst hk
    show K.neg (K.mul (r m) (pl K r m 0)) = K.mul (K.mul (sg K m) (K.neg K.one))
      (K.add (esym K (Lr r m) (m + 1)) (K.mul (r m) (esym K (Lr r m) m)))
    rw [pl_esym r m 0 m (by lib_nat), esym_big K (Lr r m) (m + 1) (by rw [len_Lr]; exact Nat.lt_succ_self m)]
    lib_ring
  | m + 1, j' + 1, k, h => by
    have h' : j' + k = m := Nat.succ.inj (show (j' + k) + 1 = m + 1 by rw [← h]; lib_nat)
    show K.add (pl K r m j') (K.neg (K.mul (r m) (pl K r m (j' + 1)))) = _
    rw [pl_esym r m j' k h']
    cases k with
    | zero =>
      have hj : j' = m := h'
      subst hj
      rw [pl_big K r j' (j' + 1) (Nat.lt_succ_self j')]
      show K.add (K.mul K.one (esym K (Lr r j') 0)) _ = K.mul K.one (esym K (r j' :: Lr r j') 0)
      rw [esym_zero, esym_zero]; lib_ring
    | succ k' =>
      rw [pl_esym r m (j' + 1) k' (by rw [← h']; lib_nat)]
      show _ = K.mul (K.mul (sg K k') (K.neg K.one)) (K.add (esym K (Lr r m) (k' + 1)) (K.mul (r m) (esym K (Lr r m) k')))
      show K.add (K.mul (K.mul (sg K k') (K.neg K.one)) (esym K (Lr r m) (k' + 1))) _ = _
      lib_ring

/-- **Vieta (original form): c_j = (−1)^k e_k(r_0, …, r_n), j + k = n + 1.** -/
theorem vieta_esym (hK : NoZD K) (h10 : K.one ≠ K.zero) (hinv : ∀ x : R, x ≠ K.zero → ∃ y, K.mul x y = K.one)
    (c : Nat → R) (n : Nat) (r : Nat → R)
    (hroot : ∀ i, i < n + 1 → K.add (pw K (r i) (n + 1)) (sum (toSemi K) (fun l => K.mul (c l) (pw K (r i) l)) (rng (n + 1))) = K.zero)
    (hdist : ∀ i j, i < n + 1 → j < n + 1 → i ≠ j → r i ≠ r j) (j k : Nat) (hjk : j + k = n + 1) (hj : j < n + 1) :
    c j = K.mul (sg K k) (esym K (Lr r (n + 1)) k) := by
  rw [vieta_full K hK h10 hinv c n r hroot hdist j hj]
  exact pl_esym K r (n + 1) j k hjk

end LibVietaEsym

#print axioms LibVietaEsym.pl_esym
#print axioms LibVietaEsym.vieta_esym

lib/VietaFull.lean

lean_all/lib/VietaFull.lean

/-
  lib/VietaFull.lean — T46 shared library, cycle 429 (G95): full Vieta — coefficients = coefficients of Π(x − r_i).

  Any commutative ring (first part) / field (end):
    mulLin r f        coefficients of (x − r)·f:  0 ↦ −r f_0,  j+1 ↦ f_j − r f_{j+1}
    pl r m            coefficients of Π_{i<m} (x − r_i) (pl 0 = 1)
    ev f D x          Σ_{j<D} f_j x^j
    ev_mulLin         ev (mulLin r f) (D+1) x = x ev f D x − r ev f (D+1) x
    pl_big / pl_top   j > m ⇒ pl_j = 0,  pl_m = 1
    ev_pl             ev (pl r (m+1)) (m+2) x = (x − r_m) ev (pl r m) (m+1) x
    pl_root           i < m ⇒ ev (pl r m) (m+1) r_i = 0
    vieta_full        field, p(x) = x^{n+1} + Σ c_j x^j with distinct roots r_0…r_n:  c_j = pl r (n+1) j  (j ≤ n)
  (The difference Σ_{j≤n}(c_j − pl_j) x^j vanishes at n+1 points → uniqueness of solutions of the Vandermonde system (LinSolve.solve_unique).)
  Lean 4 core only, no axioms.
-/
import lib.CircParseval
import lib.LinSolve

namespace LibVietaFull
open DetChain LibRing LibSum LibSemi LibMat LibLegSum LibDet

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

def mulLin (r : R) (f : Nat → R) : Nat → R
  | 0 => K.neg (K.mul r (f 0))
  | j + 1 => K.add (f j) (K.neg (K.mul r (f (j + 1))))

def pl (r : Nat → R) : Nat → Nat → R
  | 0 => fun j => indK (toSemi K) (decide (j = 0))
  | m + 1 => mulLin K (r m) (pl r m)

def ev (f : Nat → R) (D : Nat) (x : R) : R := sum (toSemi K) (fun j => K.mul (f j) (pw K x j)) (rng D)

theorem ev_succ (f : Nat → R) (D : Nat) (x : R) : ev K f (D + 1) x = K.add (ev K f D x) (K.mul (f D) (pw K x D)) :=
  LibDetRankOne.sum_snoc K _ D

theorem ev_mulLin (r : R) (f : Nat → R) (x : R) : ∀ D,
    ev K (mulLin K r f) (D + 1) x = K.add (K.mul x (ev K f D x)) (K.neg (K.mul r (ev K f (D + 1) x)))
  | 0 => by
    rw [ev_succ K (mulLin K r f) 0, ev_succ K f 0]
    show K.add K.zero (K.mul (K.neg (K.mul r (f 0))) K.one) = K.add (K.mul x K.zero) (K.neg (K.mul r (K.add K.zero (K.mul (f 0) K.one))))
    lib_ring
  | D + 1 => by
    rw [ev_succ K (mulLin K r f) (D + 1), ev_mulLin r f x D, ev_succ K f (D + 1), ev_succ K f D]
    show K.add (K.add (K.mul x (ev K f D x)) (K.neg (K.mul r (K.add (ev K f D x) (K.mul (f D) (pw K x D))))))
        (K.mul (K.add (f D) (K.neg (K.mul r (f (D + 1))))) (K.mul (pw K x D) x))
      = K.add (K.mul x (K.add (ev K f D x) (K.mul (f D) (pw K x D))))
        (K.neg (K.mul r (K.add (K.add (ev K f D x) (K.mul (f D) (pw K x D))) (K.mul (f (D + 1)) (K.mul (pw K x D) x)))))
    lib_ring

theorem pl_big (r : Nat → R) : ∀ m j, m < j → pl K r m j = K.zero
  | 0, j, h => by
    show indK (toSemi K) (decide (j = 0)) = K.zero
    rw [decide_eq_false (Nat.ne_of_gt h)]; rfl
  | m + 1, j, h => by
    cases j with
    | zero => exact absurd h (Nat.not_lt_zero _)
    | succ j' =>
      show K.add (pl K r m j') (K.neg (K.mul (r m) (pl K r m (j' + 1)))) = K.zero
      rw [pl_big r m j' (Nat.lt_of_succ_lt_succ h), pl_big r m (j' + 1) (Nat.lt_trans (Nat.lt_of_succ_lt_succ h) (Nat.lt_succ_self _))]
      lib_ring

theorem pl_top (r : Nat → R) : ∀ m, pl K r m m = K.one
  | 0 => by show indK (toSemi K) (decide ((0 : Nat) = 0)) = K.one; rw [decide_eq_true rfl]; rfl
  | m + 1 => by
    show K.add (pl K r m m) (K.neg (K.mul (r m) (pl K r m (m + 1)))) = K.one
    rw [pl_top r m, pl_big K r m (m + 1) (Nat.lt_succ_self m)]; lib_ring

theorem ev_pl (r : Nat → R) (m : Nat) (x : R) :
    ev K (pl K r (m + 1)) (m + 2) x = K.mul (K.add x (K.neg (r m))) (ev K (pl K r m) (m + 1) x) := by
  show ev K (mulLin K (r m) (pl K r m)) (m + 1 + 1) x = _
  rw [ev_mulLin, ev_succ K (pl K r m) (m + 1), pl_big K r m (m + 1) (Nat.lt_succ_self m)]
  show K.add (K.mul x (ev K (pl K r m) (m + 1) x)) (K.neg (K.mul (r m) (K.add (ev K (pl K r m) (m + 1) x) (K.mul K.zero _)))) = _
  lib_ring

theorem pl_root (r : Nat → R) : ∀ m i, i < m → ev K (pl K r m) (m + 1) (r i) = K.zero
  | 0, _, h => absurd h (Nat.not_lt_zero _)
  | m + 1, i, h => by
    rw [ev_pl]
    cases Nat.decEq i m with
    | isTrue e => rw [e]; lib_ring
    | isFalse e => rw [pl_root r m i (Nat.lt_of_le_of_ne (Nat.le_of_lt_succ h) e)]; lib_ring

/-- **Vieta: c_j = (x^j coefficient of Π_{i≤n}(x − r_i))** (field, distinct roots). -/
theorem vieta_full (hK : NoZD K) (h10 : K.one ≠ K.zero) (hinv : ∀ x : R, x ≠ K.zero → ∃ y, K.mul x y = K.one)
    (c : Nat → R) (n : Nat) (r : Nat → R)
    (hroot : ∀ i, i < n + 1 → K.add (pw K (r i) (n + 1)) (sum (toSemi K) (fun l => K.mul (c l) (pw K (r i) l)) (rng (n + 1))) = K.zero)
    (hdist : ∀ i j, i < n + 1 → j < n + 1 → i ≠ j → r i ≠ r j) (j : Nat) (hj : j < n + 1) :
    c j = pl K r (n + 1) j := by
  have ⟨d', hd'⟩ := hinv _ (LibVandDet.vand_ne K hK h10 (n + 1) r hdist)
  let a : Nat → R := fun l => K.add (c l) (K.neg (pl K r (n + 1) l))
  have hW : ∀ i, i < n + 1 → mvec (toSemi K) (n + 1) (LibVandDet.vand K r) a i
      = mvec (toSemi K) (n + 1) (LibVandDet.vand K r) (fun _ => K.zero) i := by
    intro i hi
    have hq := pl_root K r (n + 1) i hi
    rw [ev_succ, pl_top] at hq
    have hp := hroot i hi
    show sum (toSemi K) (fun l => K.mul (pw K (r i) l) (K.add (c l) (K.neg (pl K r (n + 1) l)))) (rng (n + 1))
      = sum (toSemi K) (fun l => K.mul (pw K (r i) l) K.zero) (rng (n + 1))
    rw [show sum (toSemi K) (fun l => K.mul (pw K (r i) l) K.zero) (rng (n + 1)) = K.zero from
      (sum_ext (toSemi K) _ (fun _ => K.zero) (fun l => by lib_ring) _).trans (sum_zero (toSemi K) _)]
    have hs : sum (toSemi K) (fun l => K.mul (pw K (r i) l) (K.add (c l) (K.neg (pl K r (n + 1) l)))) (rng (n + 1))
        = K.add (sum (toSemi K) (fun l => K.mul (c l) (pw K (r i) l)) (rng (n + 1)))
            (K.neg (ev K (pl K r (n + 1)) (n + 1) (r i))) := by
      rw [show ev K (pl K r (n + 1)) (n + 1) (r i)
          = sum (toSemi K) (fun l => K.mul (pl K r (n + 1) l) (pw K (r i) l)) (rng (n + 1)) from rfl, ← LibSum.sum_neg]
      refine Eq.trans ?_ (sum_add (toSemi K) _ _ _)
      exact sum_ext (toSemi K) _ _ (fun l => by show _ = K.add (K.mul (c l) _) (K.neg (K.mul (pl K r (n + 1) l) _)); lib_ring) _
    rw [hs]
    generalize sum (toSemi K) (fun l => K.mul (c l) (pw K (r i) l)) (rng (n + 1)) = S at hp ⊢
    generalize ev K (pl K r (n + 1)) (n + 1) (r i) = E at hq ⊢
    rw [show K.add S (K.neg E) = K.add (K.add (pw K (r i) (n + 1)) S) (K.neg (K.add E (K.mul K.one (pw K (r i) (n + 1))))) by lib_ring,
      hp, hq]; lib_ring
  have := LibLinSolve.solve_unique K (n + 1) (LibVandDet.vand K r) d' hd' a (fun _ => K.zero) hW j hj
  have e : K.add (c j) (K.neg (pl K r (n + 1) j)) = K.zero := this
  rw [show c j = K.add (K.add (c j) (K.neg (pl K r (n + 1) j))) (pl K r (n + 1) j) by lib_ring, e]; lib_ring

end LibVietaFull

#print axioms LibVietaFull.ev_mulLin
#print axioms LibVietaFull.pl_root
#print axioms LibVietaFull.vieta_full

lib/VietaGen.lean

lean_all/lib/VietaGen.lean

/-
  lib/VietaGen.lean — T46 shared library, cycle 292: elementary symmetric polynomials and general Vieta.

    esym l k         e_k(l): e_0 = 1, e_{k+1}(r :: l) = e_{k+1}(l) + r e_k(l)
    esym_big         k > |l| ⇒ e_k(l) = 0
    vieta_gen        Π_{r ∈ l} (1 + r t) = Σ_{k ≤ |l|} e_k(l) t^k
  (general version of Vieta3 (cubic), subtraction-free generating-function form: coefficients of Π(x − r) via t = −1/x.)
  Lean 4 core only, no axioms.
-/
import lib.Binom

namespace LibVietaGen
open DetChain LibRing LibSum LibSemi LibBinom

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

def esym : List R → Nat → R
  | [], 0 => K.one
  | [], _ + 1 => K.zero
  | _ :: _, 0 => K.one
  | r :: l, k + 1 => K.add (esym l (k + 1)) (K.mul r (esym l k))

theorem esym_zero : ∀ l : List R, esym K l 0 = K.one
  | [] => rfl
  | _ :: _ => rfl

theorem esym_big : ∀ (l : List R) (k : Nat), l.length < k → esym K l k = K.zero
  | [], 0, h => absurd h (Nat.lt_irrefl 0)
  | [], _ + 1, _ => rfl
  | _ :: _, 0, h => absurd h (Nat.not_lt_zero _)
  | r :: l, k + 1, h => by
    show K.add (esym K l (k + 1)) (K.mul r (esym K l k)) = K.zero
    have h' : l.length < k := Nat.lt_of_succ_lt_succ h
    rw [esym_big l (k + 1) (Nat.lt_succ_of_lt h'), esym_big l k h']; lib_ring

def prod1 (t : R) : List R → R
  | [] => K.one
  | r :: l => K.mul (K.add K.one (K.mul r t)) (prod1 t l)

theorem sum_shift1 (f : Nat → R) : ∀ N, sum (toSemi K) f (rng (N + 1)) = K.add (f 0) (sum (toSemi K) (fun j => f (j + 1)) (rng N))
  | 0 => by show K.add (f 0) K.zero = K.add (f 0) K.zero; rfl
  | N + 1 => by
    rw [sum_rng_succ (toSemi K) f (N + 1), sum_shift1 f N, sum_rng_succ (toSemi K) (fun j => f (j + 1)) N]
    show K.add (K.add (f 0) _) (f (N + 1)) = K.add (f 0) (K.add _ (f (N + 1))); lib_ring

/-- **General Vieta.** Π (1 + r t) = Σ_{k ≤ n} e_k t^k. -/
theorem vieta_gen (t : R) : ∀ l : List R,
    prod1 K t l = sum (toSemi K) (fun k => K.mul (esym K l k) (pw K t k)) (rng (l.length + 1))
  | [] => by show K.one = K.add (K.mul K.one K.one) K.zero; lib_ring
  | r :: l => by
    show K.mul (K.add K.one (K.mul r t)) (prod1 K t l) = sum (toSemi K) (fun k => K.mul (esym K (r :: l) k) (pw K t k)) (rng (l.length + 1 + 1))
    rw [vieta_gen t l]
    have hT := sum_shift1 K (fun k => K.mul (esym K l k) (pw K t k)) l.length
    have hR := sum_shift1 K (fun k => K.mul (esym K (r :: l) k) (pw K t k)) (l.length + 1)
    rw [hT, hR]
    have hS : sum (toSemi K) (fun j => K.mul (esym K (r :: l) (j + 1)) (pw K t (j + 1))) (rng (l.length + 1))
        = K.add (sum (toSemi K) (fun j => K.mul (esym K l (j + 1)) (pw K t (j + 1))) (rng (l.length + 1)))
            (K.mul (K.mul r t) (sum (toSemi K) (fun k => K.mul (esym K l k) (pw K t k)) (rng (l.length + 1)))) := by
      rw [show K.mul (K.mul r t) (sum (toSemi K) (fun k => K.mul (esym K l k) (pw K t k)) (rng (l.length + 1)))
          = sum (toSemi K) (fun k => K.mul (K.mul r t) (K.mul (esym K l k) (pw K t k))) (rng (l.length + 1)) from mul_sum (toSemi K) _ _ _,
        show K.add (sum (toSemi K) (fun j => K.mul (esym K l (j + 1)) (pw K t (j + 1))) (rng (l.length + 1)))
            (sum (toSemi K) (fun k => K.mul (K.mul r t) (K.mul (esym K l k) (pw K t k))) (rng (l.length + 1)))
          = sum (toSemi K) (fun j => K.add (K.mul (esym K l (j + 1)) (pw K t (j + 1))) (K.mul (K.mul r t) (K.mul (esym K l j) (pw K t j)))) (rng (l.length + 1))
          from (sum_add (toSemi K) _ _ _).symm]
      apply sum_ext (toSemi K); intro j
      show K.mul (K.add (esym K l (j + 1)) (K.mul r (esym K l j))) (K.mul (pw K t j) t)
        = K.add (K.mul (esym K l (j + 1)) (K.mul (pw K t j) t)) (K.mul (K.mul r t) (K.mul (esym K l j) (pw K t j)))
      lib_ring
    rw [hS, hT, sum_rng_succ (toSemi K) (fun j => K.mul (esym K l (j + 1)) (pw K t (j + 1))) l.length,
      esym_big K l (l.length + 1) (Nat.lt_succ_self _)]
    show _ = K.add (K.mul K.one K.one) _
    rw [esym_zero]
    generalize sum (toSemi K) (fun j => K.mul (esym K l (j + 1)) (pw K t (j + 1))) (rng l.length) = S
    show K.mul (K.add K.one (K.mul r t)) (K.add (K.mul K.one K.one) S)
      = K.add (K.mul K.one K.one) (K.add (K.add S (K.mul K.zero (pw K t (l.length + 1)))) (K.mul (K.mul r t) (K.add (K.mul K.one K.one) S)))
    lib_ring

end LibVietaGen

#print axioms LibVietaGen.esym_big
#print axioms LibVietaGen.vieta_gen

lib/WChebO.lean

lean_all/lib/WChebO.lean

/-
  lib/WChebO.lean — T46 shared library: the weighted Chebyshev (one-dimensional FKG) inequality in ORing form.

    wcheb_O   l = [(w, f, g), …] similarly ordered with w ≥ 0 (LibCheb.Sim for the cone of O):
              (Σwf)(Σwg) ≤ (Σw)(Σwfg)
  Lean 4 core only, no axioms.
-/
import lib.Chebyshev
import lib.ORingBridge

namespace LibWChebO
open DetChain LibRing LibCS LibCheb

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

theorem wcheb_O (l : List (Pt R)) (h : Sim K (LibORB.posOf K O) l) :
    O.le (K.mul (sF K l) (sG K l)) (K.mul (sZ K l) (sH K l)) :=
  LibORB.toO K O (chebyshev K (LibORB.posOf K O) l h)

end LibWChebO

#print axioms LibWChebO.wcheb_O

lib/WVarO.lean

lean_all/lib/WVarO.lean

/-
  lib/WVarO.lean — T46 shared library: the weighted variance inequality in ORing form.

    wvar_O   weights w_i ≥ 0:  (Σ w_i x_i)² ≤ (Σ w_i)(Σ w_i x_i²)
             (LibCheb.variance through the ORing bridge LibORB.posOf / toO; l is a list of (w_i, x_i))
  Lean 4 core only, no axioms.
-/
import lib.Chebyshev
import lib.ORingBridge

namespace LibWVarO
open DetChain LibRing LibCS LibCheb

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

theorem wvar_O (l : List (R × R)) (hw : ∀ p, p ∈ l → O.le K.zero p.1) :
    O.le (K.mul (sF K (diag l)) (sF K (diag l))) (K.mul (sZ K (diag l)) (sH K (diag l))) :=
  LibORB.toO K O (variance K (LibORB.posOf K O) l hw)

end LibWVarO

#print axioms LibWVarO.wvar_O

lib/WeierCancel.lean

lean_all/lib/WeierCancel.lean

/-
  lib/WeierCancel.lean — T46 shared library, cycle 791 (G726): (P + Q) + (−Q) = P (chord addition, general position).

  P = (x₀, y₀), Q = (x₁, y₁), y₁ = y₀ + l(x₁ − x₀), x₂ = l² + a₁l − a₂ − x₀ − x₁, y₂ = y₀ + l(x₂ − x₀),
  R = P + Q = (x₂, −y₂ − a₁x₂ − a₃), −Q = (x₁, −y₁ − a₁x₁ − a₃), l' = −l − a₁.
    slope_back    y_R − y_(−Q) = l'(x₂ − x₁)            (slope l' of the line through R and −Q)
    cancel_x      l'² + a₁l' − a₂ − x₂ − x₁ = x₀
    cancel_y      −(y_R + l'(x₀ − x₂)) − a₁x₀ − a₃ = y₀  — hence R + (−Q) = P
  Lean 4 core only, no axioms.
-/
import lib.WeierChangeCount

namespace LibWeierCancel
open DetChain LibRing

variable {R : Type} (K : CRing R) (a1 a2 a3 l x0 y0 x1 : R)

theorem slope_back : (K.add (K.add (K.add (K.neg (K.add y0 (K.mul l (K.add (K.add (K.add (K.add (K.add (K.mul l l) (K.mul a1 l)) (K.neg a2)) (K.neg x0)) (K.neg x1)) (K.neg x0))))) (K.neg (K.mul a1 (K.add (K.add (K.add (K.add (K.mul l l) (K.mul a1 l)) (K.neg a2)) (K.neg x0)) (K.neg x1))))) (K.neg a3)) (K.neg (K.add (K.add (K.neg (K.add y0 (K.mul l (K.add x1 (K.neg x0))))) (K.neg (K.mul a1 x1))) (K.  -- (+173 chars; full line in the Zenodo source)

theorem cancel_x : (K.add (K.add (K.add (K.add (K.mul (K.add (K.neg l) (K.neg a1)) (K.add (K.neg l) (K.neg a1))) (K.mul a1 (K.add (K.neg l) (K.neg a1)))) (K.neg a2)) (K.neg (K.add (K.add (K.add (K.add (K.mul l l) (K.mul a1 l)) (K.neg a2)) (K.neg x0)) (K.neg x1)))) (K.neg x1)) = x0 := by lib_ring

/-- **(P + Q) + (−Q) = P.** -/
theorem cancel_y : (K.add (K.add (K.neg (K.add (K.add (K.add (K.neg (K.add y0 (K.mul l (K.add (K.add (K.add (K.add (K.add (K.mul l l) (K.mul a1 l)) (K.neg a2)) (K.neg x0)) (K.neg x1)) (K.neg x0))))) (K.neg (K.mul a1 (K.add (K.add (K.add (K.add (K.mul l l) (K.mul a1 l)) (K.neg a2)) (K.neg x0)) (K.neg x1))))) (K.neg a3)) (K.mul (K.add (K.neg l) (K.neg a1)) (K.add x0 (K.neg (K.add (K.add (K.add (K.ad  -- (+123 chars; full line in the Zenodo source)

end LibWeierCancel

#print axioms LibWeierCancel.slope_back
#print axioms LibWeierCancel.cancel_x
#print axioms LibWeierCancel.cancel_y

lib/WeierChange.lean

lean_all/lib/WeierChange.lean

/-
  lib/WeierChange.lean — T46 shared library, cycle 782 (G711): change of variables of Weierstrass form (x, y) = (u²X + r, u³Y + su²X + t).

    a1c … a6c     old coefficients in terms of new coefficients b_i (inverse of Silverman Table 3.1, no division):
                  a₁ = ub₁ − 2s, a₂ = u²b₂ + sa₁ − 3r + s², a₃ = u³b₃ − ra₁ − 2t,
                  a₄ = u⁴b₄ + sa₃ − 2ra₂ + (t + rs)a₁ − 3r² + 2st, a₆ = u⁶b₆ − ra₄ − r²a₂ − r³ + ta₃ + t² + rta₁
    change_on     (X, Y) on E_b ⇒ (u²X + r, u³Y + su²X + t) on E_a   (E_a − RHS = u⁶ (E_b − RHS))
  Lean 4 core only, no axioms.
-/
import lib.WeierChord

namespace LibWeierChange
open DetChain LibRing LibWeierChord

variable {R : Type} (K : CRing R) (u r s t b1 b2 b3 b4 b6 : R)

/-- **Change of variables preserves the form.** -/
theorem change_on (X Y : R) (h : onW K b1 b2 b3 b4 b6 X Y) :
    onW K (K.add (K.mul u b1) (K.neg (K.mul (K.add K.one K.one) s))) (K.add (K.add (K.add (K.mul (K.mul u u) b2) (K.mul s (K.add (K.mul u b1) (K.neg (K.mul (K.add K.one K.one) s))))) (K.neg (K.mul (K.add (K.add K.one K.one) K.one) r))) (K.mul s s)) (K.add (K.add (K.mul (K.mul (K.mul u u) u) b3) (K.neg (K.mul r (K.add (K.mul u b1) (K.neg (K.mul (K.add K.one K.one) s)))))) (K.neg (K.mul (K.add K.one  -- (+2112 chars; full line in the Zenodo source)
  unfold onW at h ⊢
  lib_lc K [h] [(K.mul (K.mul (K.mul (K.mul (K.mul u u) u) u) u) u)]

end LibWeierChange

#print axioms LibWeierChange.change_on

lib/WeierChangeCount.lean

lean_all/lib/WeierChangeCount.lean

/-
  lib/WeierChangeCount.lean — T46 shared library, cycle 790 (G724): change of variables preserves the affine point count (u invertible).

    change_back    vu = 1: (u²X + r, u³Y + su²X + t) ∈ E_a ⇒ (X, Y) ∈ E_b      (converse of change_on)
    sum_affine     a invertible (ia·a = 1): Σ_z G(z) = Σ_z G(az + b)   (over els)
    count_change   Σ_x Σ_y [(x, y) ∈ E_a] = Σ_X Σ_Y [(X, Y) ∈ E_b]
  Lean 4 core only, no axioms.
-/
import lib.WeierFF

namespace LibWeierChangeCount
open DetChain LibRing LibSum LibLegSum LibWeierChord

variable {R : Type} [DecidableEq R] (K : CRing R) (u v r s t b1 b2 b3 b4 b6 : R)

/-- **Reverse direction.** -/
theorem change_back (hv : K.mul v u = K.one) (X Y : R) (h : onW K (K.add (K.mul u b1) (K.neg (K.mul (K.add K.one K.one) s))) (K.add (K.add (K.add (K.mul (K.mul u u) b2) (K.mul s (K.add (K.mul u b1) (K.neg (K.mul (K.add K.one K.one) s))))) (K.neg (K.mul (K.add (K.add K.one K.one) K.one) r))) (K.mul s s)) (K.add (K.add (K.mul (K.mul (K.mul u u) u) b3) (K.neg (K.mul r (K.add (K.mul u b1) (K.neg (K.mu  -- (+2165 chars; full line in the Zenodo source)
    onW K b1 b2 b3 b4 b6 X Y := by
  unfold onW at h ⊢
  lib_lc K [h, hv] [(K.mul (K.mul (K.mul (K.mul (K.mul v v) v) v) v) v), (K.neg (K.mul (K.add (K.add (K.add (K.add (K.add K.one (K.mul v u)) (K.mul (K.mul v u) (K.mul v u))) (K.mul (K.mul (K.mul v u) (K.mul v u)) (K.mul v u))) (K.mul (K.mul (K.mul (K.mul v u) (K.mul v u)) (K.mul v u)) (K.mul v u))) (K.mul (K.mul (K.mul (K.mul (K.mul v u) (K.mul v u)) (K.mul v u)) (K.mul v u)) (K.mul v u))) (K.add   -- (+160 chars; full line in the Zenodo source)

theorem sum_affine {L : Type} (M : CRing L) (a b ia : R) (hia : K.mul ia a = K.one) (els : List R) (hd : LibPoly.Dist els)
    (hall : ∀ z, z ∈ els) (G : R → L) :
    sum (toSemi M) G els = sum (toSemi M) (fun z => G (K.add (K.mul a z) b)) els := by
  have hinj : ∀ z z', z ∈ els → z' ∈ els → K.add (K.mul a z) b = K.add (K.mul a z') b → z = z' := fun z z' _ _ e => by
    have e2 : K.mul ia (K.add (K.mul a z) b) = K.mul ia (K.add (K.mul a z') b) := by rw [e]
    lib_lc K [e2, hia] [K.one, K.neg (K.add z (K.neg z'))]
  refine Eq.trans (LibCharOrth.sum_dist_eq M G els (els.map (fun z => K.add (K.mul a z) b)) hd
    (LibDist.dist_map _ els hd hinj) (fun z _ => by
      have e : z = K.add (K.mul a (K.mul ia (K.add z (K.neg b)))) b := by lib_lc K [hia] [K.neg (K.add z (K.neg b))]
      rw [e]; exact LibWeierCount.mem_map_of _ (hall _)) (fun z _ => hall z)) ?_
  exact sum_map (toSemi M) G _ els

/-- **Change of variables preserves the affine point count.** -/
theorem count_change (hv : K.mul v u = K.one) (els : List R) (hd : LibPoly.Dist els) (hall : ∀ z, z ∈ els) :
    sum (toSemi K) (fun x => sum (toSemi K) (fun y => indK (toSemi K) (decide (onW K (K.add (K.mul u b1) (K.neg (K.mul (K.add K.one K.one) s))) (K.add (K.add (K.add (K.mul (K.mul u u) b2) (K.mul s (K.add (K.mul u b1) (K.neg (K.mul (K.add K.one K.one) s))))) (K.neg (K.mul (K.add (K.add K.one K.one) K.one) r))) (K.mul s s)) (K.add (K.add (K.mul (K.mul (K.mul u u) u) b3) (K.neg (K.mul r (K.add (K.mul  -- (+2083 chars; full line in the Zenodo source)
      = sum (toSemi K) (fun X => sum (toSemi K) (fun Y => indK (toSemi K) (decide (onW K b1 b2 b3 b4 b6 X Y))) els) els := by
  have hu2 : K.mul (K.mul v v) (K.mul u u) = K.one := by lib_lc K [hv] [K.add (K.mul v u) K.one]
  have hu3 : K.mul (K.mul (K.mul v v) v) (K.mul (K.mul u u) u) = K.one := by
    lib_lc K [hv] [K.add (K.add (K.mul (K.mul v u) (K.mul v u)) (K.mul v u)) K.one]
  rw [sum_affine K K _ r _ hu2 els hd hall]
  apply sum_ext (toSemi K); intro X
  rw [sum_affine K K _ (K.add (K.mul s (K.mul (K.mul u u) X)) t) _ hu3 els hd hall]
  apply sum_ext (toSemi K); intro Y
  refine LibWeierCount.ind_iff K _ _ ⟨fun h => change_back K u v r s t b1 b2 b3 b4 b6 hv X Y ?_, fun h => ?_⟩
  · have e : K.add (K.mul (K.mul (K.mul u u) u) Y) (K.add (K.mul s (K.mul (K.mul u u) X)) t) = (K.add (K.add (K.mul (K.mul (K.mul u u) u) Y) (K.mul (K.mul s (K.mul u u)) X)) t) := by lib_ring
    have e' : K.add (K.mul (K.mul u u) X) r = (K.add (K.mul (K.mul u u) X) r) := rfl
    rw [e', e] at h; exact h
  · have e : K.add (K.mul (K.mul (K.mul u u) u) Y) (K.add (K.mul s (K.mul (K.mul u u) X)) t) = (K.add (K.add (K.mul (K.mul (K.mul u u) u) Y) (K.mul (K.mul s (K.mul u u)) X)) t) := by lib_ring
    rw [e]; exact LibWeierChange.change_on K u r s t b1 b2 b3 b4 b6 X Y h

end LibWeierChangeCount

#print axioms LibWeierChangeCount.change_back
#print axioms LibWeierChangeCount.count_change

lib/WeierChord.lean

lean_all/lib/WeierChord.lean

/-
  lib/WeierChord.lean — T46 shared library, cycle 781 (G708–G710): chord law for the general Weierstrass form (inverse witness, no integral domain hypothesis).

  E : y² + a₁xy + a₃y = x³ + a₂x² + a₄x + a₆  (onW).
    chord_third   P₀, Q = (x₁, y₀ + l(x₁ − x₀)) on the curve, w(x₁ − x₀) = 1 ⇒ (x', y₀ + l(x' − x₀)) on the curve,  x' = l² + a₁l − a₂ − x₀ − x₁
    neg_on        (x, y) on the curve ⇒ (x, −y − a₁x − a₃) on the curve
    tangent_third, tangent_on   l(2y₀ + a₁x₀ + a₃) = 3x₀² + 2a₂x₀ + a₄ − a₁y₀ ⇒ 2P on the curve
    chord_on      P₀ + Q = (x', −(y₀ + l(x' − x₀)) − a₁x' − a₃) on the curve   (general version of agent1 lang/Z8Chord)
  Proof: adding (X − x₀)(X − x₁)(X − x') to g(X) = E(X, line) gives a linear polynomial with slope A; the certificate
  g(x') = (1 − (x' − x₀)w)·g(x₀) + (x' − x₀)w·g(x₁) − A(x' − x₀)(w(x₁ − x₀) − 1) via lib_lc.
  Lean 4 core only, no axioms.
-/
import lib.DirMobius
import lib.LinComb

namespace LibWeierChord
open DetChain LibRing

variable {R : Type} (K : CRing R) (a1 a2 a3 a4 a6 : R)

def onW (x y : R) : Prop :=
  K.add (K.add (K.mul y y) (K.mul (K.mul a1 x) y)) (K.mul a3 y)
    = K.add (K.add (K.add (K.mul (K.mul x x) x) (K.mul a2 (K.mul x x))) (K.mul a4 x)) a6

/-- **Third intersection point.** -/
theorem chord_third (x0 y0 x1 l w : R) (h0 : onW K a1 a2 a3 a4 a6 x0 y0)
    (h1 : onW K a1 a2 a3 a4 a6 x1 (K.add y0 (K.mul l (K.add x1 (K.neg x0))))) (hw : K.mul w (K.add x1 (K.neg x0)) = K.one) :
    onW K a1 a2 a3 a4 a6 (K.add (K.add (K.add (K.add (K.mul l l) (K.mul a1 l)) (K.neg a2)) (K.neg x0)) (K.neg x1)) (K.add y0 (K.mul l (K.add (K.add (K.add (K.add (K.add (K.mul l l) (K.mul a1 l)) (K.neg a2)) (K.neg x0)) (K.neg x1)) (K.neg x0)))) := by
  unfold onW at h0 h1 ⊢
  lib_lc K [h0, h1, hw] [(K.add K.one (K.neg (K.mul (K.add (K.add (K.add (K.add (K.add (K.mul l l) (K.mul a1 l)) (K.neg a2)) (K.neg x0)) (K.neg x1)) (K.neg x0)) w))), (K.mul (K.add (K.add (K.add (K.add (K.add (K.mul l l) (K.mul a1 l)) (K.neg a2)) (K.neg x0)) (K.neg x1)) (K.neg x0)) w), (K.neg (K.mul (K.add (K.add (K.add (K.add (K.add (K.add (K.mul (K.mul (K.add K.one K.one) l) (K.add y0 (K.neg (K.  -- (+413 chars; full line in the Zenodo source)

/-- **Negation.** -/
theorem neg_on (x y : R) (h : onW K a1 a2 a3 a4 a6 x y) :
    onW K a1 a2 a3 a4 a6 x (K.add (K.add (K.neg y) (K.neg (K.mul a1 x))) (K.neg a3)) := by
  unfold onW at h ⊢
  lib_lc K [h] [K.one]

/-- **Chord law: P₀ + Q is on the curve.** -/
theorem chord_on (x0 y0 x1 l w : R) (h0 : onW K a1 a2 a3 a4 a6 x0 y0)
    (h1 : onW K a1 a2 a3 a4 a6 x1 (K.add y0 (K.mul l (K.add x1 (K.neg x0))))) (hw : K.mul w (K.add x1 (K.neg x0)) = K.one) :
    onW K a1 a2 a3 a4 a6 (K.add (K.add (K.add (K.add (K.mul l l) (K.mul a1 l)) (K.neg a2)) (K.neg x0)) (K.neg x1))
      (K.add (K.add (K.neg (K.add y0 (K.mul l (K.add (K.add (K.add (K.add (K.add (K.mul l l) (K.mul a1 l)) (K.neg a2)) (K.neg x0)) (K.neg x1)) (K.neg x0))))) (K.neg (K.mul a1 (K.add (K.add (K.add (K.add (K.mul l l) (K.mul a1 l)) (K.neg a2)) (K.neg x0)) (K.neg x1))))) (K.neg a3)) :=
  neg_on K a1 a2 a3 a4 a6 _ _ (chord_third K a1 a2 a3 a4 a6 x0 y0 x1 l w h0 h1 hw)

/-- **Tangent law: third intersection point (the negative of 2P).** l(2y₀ + a₁x₀ + a₃) = 3x₀² + 2a₂x₀ + a₄ − a₁y₀. -/
theorem tangent_third (x0 y0 l : R) (h0 : onW K a1 a2 a3 a4 a6 x0 y0)
    (ht : (K.mul l (K.add (K.add (K.mul (K.add K.one K.one) y0) (K.mul a1 x0)) a3)) = (K.add (K.add (K.add (K.mul (K.mul (K.add (K.add K.one K.one) K.one) x0) x0) (K.mul (K.mul (K.add K.one K.one) a2) x0)) a4) (K.neg (K.mul a1 y0)))) :
    onW K a1 a2 a3 a4 a6 (K.add (K.add (K.add (K.mul l l) (K.mul a1 l)) (K.neg a2)) (K.neg (K.mul (K.add K.one K.one) x0))) (K.add y0 (K.mul l (K.add (K.add (K.add (K.add (K.mul l l) (K.mul a1 l)) (K.neg a2)) (K.neg (K.mul (K.add K.one K.one) x0))) (K.neg x0)))) := by
  unfold onW at h0 ⊢
  lib_lc K [h0, ht] [K.one, (K.add (K.add (K.add (K.add (K.mul l l) (K.mul a1 l)) (K.neg a2)) (K.neg (K.mul (K.add K.one K.one) x0))) (K.neg x0))]

/-- **Tangent law: 2P is on the curve.** -/
theorem tangent_on (x0 y0 l : R) (h0 : onW K a1 a2 a3 a4 a6 x0 y0)
    (ht : (K.mul l (K.add (K.add (K.mul (K.add K.one K.one) y0) (K.mul a1 x0)) a3)) = (K.add (K.add (K.add (K.mul (K.mul (K.add (K.add K.one K.one) K.one) x0) x0) (K.mul (K.mul (K.add K.one K.one) a2) x0)) a4) (K.neg (K.mul a1 y0)))) :
    onW K a1 a2 a3 a4 a6 (K.add (K.add (K.add (K.mul l l) (K.mul a1 l)) (K.neg a2)) (K.neg (K.mul (K.add K.one K.one) x0))) (K.add (K.add (K.neg (K.add y0 (K.mul l (K.add (K.add (K.add (K.add (K.mul l l) (K.mul a1 l)) (K.neg a2)) (K.neg (K.mul (K.add K.one K.one) x0))) (K.neg x0))))) (K.neg (K.mul a1 (K.add (K.add (K.add (K.mul l l) (K.mul a1 l)) (K.neg a2)) (K.neg (K.mul (K.add K.one K.one) x0)))  -- (+18 chars; full line in the Zenodo source)
  neg_on K a1 a2 a3 a4 a6 _ _ (tangent_third K a1 a2 a3 a4 a6 x0 y0 l h0 ht)

end LibWeierChord

#print axioms LibWeierChord.chord_third
#print axioms LibWeierChord.chord_on
#print axioms LibWeierChord.tangent_on

lib/WeierComm.lean

lean_all/lib/WeierComm.lean

/-
  lib/WeierComm.lean — T46 shared library, cycle 787 (G721): commutativity of chord addition and involutivity of negation (general Weierstrass form).

    third_x_comm   x' = l² + a₁l − a₂ − x₀ − x₁ is symmetric in x₀, x₁
    line_comm      y₁ = y₀ + l(x₁ − x₀) ⇒ line value y₀ + l(X − x₀) = y₁ + l(X − x₁)  (all X)
    add_comm_pt    both coordinates of P₀ + Q = both coordinates of Q + P₀
    ny_ny          −(−P) = P
  Lean 4 core only, no axioms.
-/
import lib.WeierDom

namespace LibWeierComm
open DetChain LibRing LibWeierChord LibWeierDom

variable {R : Type} (K : CRing R) (a1 a2 a3 : R)

def thx (l x0 x1 : R) : R := K.add (K.add (K.add (K.add (K.mul l l) (K.mul a1 l)) (K.neg a2)) (K.neg x0)) (K.neg x1)

theorem third_x_comm (l x0 x1 : R) : thx K a1 a2 l x0 x1 = thx K a1 a2 l x1 x0 := by unfold thx; lib_ring

theorem line_comm (l x0 y0 x1 y1 X : R) (h : y1 = K.add y0 (K.mul l (K.add x1 (K.neg x0)))) :
    K.add y0 (K.mul l (K.add X (K.neg x0))) = K.add y1 (K.mul l (K.add X (K.neg x1))) := by
  rw [h]; lib_ring

/-- **P₀ + Q = Q + P₀ (coordinates).** -/
theorem add_comm_pt (l x0 y0 x1 y1 : R) (h : y1 = K.add y0 (K.mul l (K.add x1 (K.neg x0)))) :
    thx K a1 a2 l x0 x1 = thx K a1 a2 l x1 x0 ∧
    ny K a1 a3 (thx K a1 a2 l x0 x1) (K.add y0 (K.mul l (K.add (thx K a1 a2 l x0 x1) (K.neg x0))))
      = ny K a1 a3 (thx K a1 a2 l x1 x0) (K.add y1 (K.mul l (K.add (thx K a1 a2 l x1 x0) (K.neg x1)))) := by
  refine ⟨third_x_comm K a1 a2 l x0 x1, ?_⟩
  rw [← third_x_comm K a1 a2 l x0 x1, ← line_comm K l x0 y0 x1 y1 _ h]

theorem ny_ny (x y : R) : ny K a1 a3 x (ny K a1 a3 x y) = y := by unfold ny; lib_ring

end LibWeierComm

#print axioms LibWeierComm.add_comm_pt
#print axioms LibWeierComm.ny_ny

lib/WeierCount.lean

lean_all/lib/WeierCount.lean

/-
  lib/WeierCount.lean — T46 shared library, cycle 788 (G719): transferring the point count to η² = quartic (2 invertible).

  els is a list containing every element of R exactly once (Dist, every z ∈ els), i2·2 = 1.
    ind_iff       p ↔ q ⇒ [p] = [q]
    mem_map_of    w ∈ l ⇒ f w ∈ l.map f
    count_eta     for each x, #{y : (x, y) ∈ E} = #{η : η² = 4x³ + b₂x² + 2b₄x + b₆}   (as indicator sums)
  Lean 4 core only, no axioms.
-/
import lib.WeierComm
import lib.CharOrth
import lib.Distinct

namespace LibWeierCount
open DetChain LibRing LibSum LibLegSum LibWeierChord LibWeierSquare

variable {R : Type} [DecidableEq R] (K : CRing R) (a1 a2 a3 a4 a6 : R)

instance onW_dec (x y : R) : Decidable (onW K a1 a2 a3 a4 a6 x y) := by
  unfold onW; exact inferInstance

theorem ind_iff (p q : Prop) [Decidable p] [Decidable q] (h : p ↔ q) :
    indK (toSemi K) (decide p) = indK (toSemi K) (decide q) := by
  by_cases hp : p
  · rw [decide_eq_true hp, decide_eq_true (h.1 hp)]
  · rw [decide_eq_false hp, decide_eq_false (fun hq => hp (h.2 hq))]

theorem mem_map_of {A B : Type} (f : A → B) {w : A} : ∀ {l : List A}, w ∈ l → f w ∈ l.map f
  | _ :: _, .head _ => .head _
  | _ :: _, .tail _ h => .tail _ (mem_map_of f h)

/-- **Transfer of the point count.** -/
theorem count_eta (i2 : R) (hi2 : K.mul i2 (castN K 2) = K.one) (els : List R) (hd : LibPoly.Dist els)
    (hall : ∀ z, z ∈ els) (x : R) :
    sum (toSemi K) (fun y => indK (toSemi K) (decide (onW K a1 a2 a3 a4 a6 x y))) els
      = sum (toSemi K) (fun η => indK (toSemi K) (decide (K.mul η η = quartic K a1 a2 a3 a4 a6 x))) els := by
  have h4 : ∀ z, K.mul (castN K 4) z = K.zero → z = K.zero := fun z hz => by
    have e : z = K.mul (K.mul i2 i2) (K.mul (castN K 4) z) := by
      have h2 : K.mul i2 (K.add K.one (K.add K.one K.zero)) = K.one := hi2
      have : K.mul (K.mul i2 i2) (castN K 4) = K.one := by
        show K.mul (K.mul i2 i2) (K.add K.one (K.add K.one (K.add K.one (K.add K.one K.zero)))) = K.one
        lib_lc K [h2] [K.add (K.mul i2 (K.add K.one K.one)) K.one]
      rw [← K.mul_assoc, this, K.one_mul]
    rw [e, hz]; lib_ring
  let σ : R → R := fun y => eta K a1 a3 x y
  have hinj : ∀ y y', y ∈ els → y' ∈ els → σ y = σ y' → y = y' := fun y y' _ _ e => by
    have e' : K.mul (castN K 2) (K.add y (K.neg y')) = K.zero := by
      show K.mul (K.add K.one (K.add K.one K.zero)) (K.add y (K.neg y')) = K.zero
      have : K.add (K.add (K.mul (castN K 2) y) (K.mul a1 x)) a3 = K.add (K.add (K.mul (castN K 2) y') (K.mul a1 x)) a3 := e
      show _ = K.zero
      lib_lc K [this] [K.one]
    have : K.add y (K.neg y') = K.zero := by
      have := congrArg (K.mul i2) e'
      rw [← K.mul_assoc, hi2, K.one_mul] at this
      rw [this]; lib_ring
    lib_lc K [this] [K.one]
  have hdm : LibPoly.Dist (els.map σ) := LibDist.dist_map σ els hd hinj
  refine Eq.trans ?_ (LibCharOrth.sum_dist_eq K (fun η => indK (toSemi K) (decide (K.mul η η = quartic K a1 a2 a3 a4 a6 x))) els (els.map σ) hd hdm (fun z _ => by
      have e : z = σ (K.mul i2 (K.add z (K.neg (K.add (K.mul a1 x) a3)))) := by
        show z = K.add (K.add (K.mul (castN K 2) (K.mul i2 (K.add z (K.neg (K.add (K.mul a1 x) a3))))) (K.mul a1 x)) a3
        have h2 : K.mul i2 (castN K 2) = K.one := hi2
        lib_lc K [h2] [K.neg (K.add z (K.neg (K.add (K.mul a1 x) a3)))]
      rw [e]; exact mem_map_of σ (hall _)) (fun z _ => hall z)).symm
  rw [sum_map]
  apply sum_ext (toSemi K); intro y
  exact ind_iff K _ _ ⟨fun h => sq_complete K a1 a2 a3 a4 a6 x y h, fun h => sq_converse K a1 a2 a3 a4 a6 x y h4 h⟩

end LibWeierCount

#print axioms LibWeierCount.count_eta

lib/WeierDelta.lean

lean_all/lib/WeierDelta.lean

/-
  lib/WeierDelta.lean — T46 shared library, cycle 808 (G758, G759): transformation of the discriminant Δ(a) = u¹²Δ(b) and the j-invariant.

    b8_change    under change of variables b₈(a) = u⁸b₈(b) − 3r·b₆(a) − 3r²·b₄(a) − r³·b₂(a) − 3r⁴   (b_i(a) as b atoms)
    dl_shift     4w = xz − y² ⇒ Δ(shifted and scaled b) = u¹²Δ(x, y, z, w)   (difference = u⁸(6ru²x − 36r²)(4w − xz + y²), lib_ringz)
    dl_change    Δ(a) = u¹²Δ(b)
    j_inv        c₄(a)³·Δ(b) = c₄(b)³·Δ(a)   (j-invariant, without division)
  Lean 4 core only, no axioms.
-/
import lib.WeierDisc

namespace LibWeierDelta
open DetChain LibRing LibWeierInv

variable {R : Type} (K : CRing R) (u r s t b1 b2 b3 b4 b6 : R)

theorem b8_change : B8 K (K.add (K.mul u b1) (K.neg (K.mul (K.mul (K.add K.one K.one) K.one) s))) (K.add (K.add (K.add (K.mul (K.mul u u) b2) (K.mul s (K.add (K.mul u b1) (K.neg (K.mul (K.mul (K.add K.one K.one) K.one) s))))) (K.neg (K.mul (K.add (K.mul (K.add K.one K.one) K.one) K.one) r))) (K.mul s s)) (K.add (K.add (K.mul (K.mul (K.mul u u) u) b3) (K.neg (K.mul r (K.add (K.mul u b1) (K.neg (K.m  -- (+3240 chars; full line in the Zenodo source)
  unfold B2 B4 B6 B8; lib_ringz

theorem dl_shift (x y z w : R) (h : K.mul (castN K 4) w = K.add (K.mul x z) (K.neg (K.mul y y))) :
    Dlb K (K.add (K.mul (K.mul u u) x) (K.neg (K.mul (castN K 12) r))) (K.add (K.add (K.mul (K.mul (K.mul (K.mul u u) u) u) y) (K.neg (K.mul (K.mul r (K.mul u u)) x))) (K.mul (K.mul (castN K 6) r) r)) (K.add (K.add (K.add (K.mul (K.mul (K.mul (K.mul (K.mul (K.mul u u) u) u) u) u) z) (K.neg (K.mul (K.mul (K.mul (K.mul (K.add K.one K.one) K.one) r) (K.mul (K.mul (K.mul u u) u) u)) y))) (K.mul (K.mul  -- (+991 chars; full line in the Zenodo source)
  have key : sub K (Dlb K (K.add (K.mul (K.mul u u) x) (K.neg (K.mul (castN K 12) r))) (K.add (K.add (K.mul (K.mul (K.mul (K.mul u u) u) u) y) (K.neg (K.mul (K.mul r (K.mul u u)) x))) (K.mul (K.mul (castN K 6) r) r)) (K.add (K.add (K.add (K.mul (K.mul (K.mul (K.mul (K.mul (K.mul u u) u) u) u) u) z) (K.neg (K.mul (K.mul (K.mul (K.mul (K.add K.one K.one) K.one) r) (K.mul (K.mul (K.mul u u) u) u)) y)  -- (+1002 chars; full line in the Zenodo source)
      = K.mul (K.mul (K.mul (K.mul (K.mul (K.mul (K.mul (K.mul (K.mul u u) u) u) u) u) u) u) (K.add (K.mul (K.mul (K.mul (castN K 6) r) (K.mul u u)) x) (K.neg (K.mul (K.mul (castN K 36) r) r)))) (sub K (K.mul (castN K 4) w) (K.add (K.mul x z) (K.neg (K.mul y y)))) := by
    unfold Dlb; lib_ringz
  refine LibLC.close K _ _ _ key ?_
  rw [LibLC.sub_zero_of K h]; lib_ring

/-- **Δ(a) = u¹²Δ(b).** -/
theorem dl_change : Dl K (K.add (K.mul u b1) (K.neg (K.mul (K.mul (K.add K.one K.one) K.one) s))) (K.add (K.add (K.add (K.mul (K.mul u u) b2) (K.mul s (K.add (K.mul u b1) (K.neg (K.mul (K.mul (K.add K.one K.one) K.one) s))))) (K.neg (K.mul (K.add (K.mul (K.add K.one K.one) K.one) K.one) r))) (K.mul s s)) (K.add (K.add (K.mul (K.mul (K.mul u u) u) b3) (K.neg (K.mul r (K.add (K.mul u b1) (K.neg (K.m  -- (+2519 chars; full line in the Zenodo source)
  show Dlb K (B2 K _ _) (B4 K _ _ _) (B6 K _ _) (B8 K _ _ _ _ _) = _
  rw [b8_change, b2_change, b4_change, b6_change]
  show _ = K.mul _ (Dlb K (B2 K b1 b2) (B4 K b1 b3 b4) (B6 K b3 b6) (B8 K b1 b2 b3 b4 b6))
  have hb := b8_rel K b1 b2 b3 b4 b6
  generalize (B2 K b1 b2) = x at hb ⊢; generalize (B4 K b1 b3 b4) = y at hb ⊢; generalize (B6 K b3 b6) = z at hb ⊢; generalize (B8 K b1 b2 b3 b4 b6) = w at hb ⊢
  exact dl_shift K u r x y z w hb

/-- **j-invariant: c₄(a)³Δ(b) = c₄(b)³Δ(a).** -/
theorem j_inv : K.mul (K.mul (K.mul (C4 K (K.add (K.mul u b1) (K.neg (K.mul (K.mul (K.add K.one K.one) K.one) s))) (K.add (K.add (K.add (K.mul (K.mul u u) b2) (K.mul s (K.add (K.mul u b1) (K.neg (K.mul (K.mul (K.add K.one K.one) K.one) s))))) (K.neg (K.mul (K.add (K.mul (K.add K.one K.one) K.one) K.one) r))) (K.mul s s)) (K.add (K.add (K.mul (K.mul (K.mul u u) u) b3) (K.neg (K.mul r (K.add (K.mul   -- (+3457 chars; full line in the Zenodo source)
    = K.mul (K.mul (K.mul (C4 K b1 b2 b3 b4) (C4 K b1 b2 b3 b4)) (C4 K b1 b2 b3 b4)) (Dl K (K.add (K.mul u b1) (K.neg (K.mul (K.mul (K.add K.one K.one) K.one) s))) (K.add (K.add (K.add (K.mul (K.mul u u) b2) (K.mul s (K.add (K.mul u b1) (K.neg (K.mul (K.mul (K.add K.one K.one) K.one) s))))) (K.neg (K.mul (K.add (K.mul (K.add K.one K.one) K.one) K.one) r))) (K.mul s s)) (K.add (K.add (K.mul (K.mul   -- (+2444 chars; full line in the Zenodo source)
  rw [c4_change, dl_change]
  generalize C4 K b1 b2 b3 b4 = c; generalize Dl K b1 b2 b3 b4 b6 = d; lib_ringz

end LibWeierDelta

#print axioms LibWeierDelta.b8_change
#print axioms LibWeierDelta.dl_change
#print axioms LibWeierDelta.j_inv

lib/WeierDisc.lean

lean_all/lib/WeierDisc.lean

/-
  lib/WeierDisc.lean — T46 shared library, cycle 807 (G713b): 1728Δ = c₄³ − c₆² (general Weierstrass form, any commutative ring).

    disc_b     4b₈ = b₂b₆ − b₄² ⇒ 1728Δ = c₄³ − c₆²   (over b atoms; via lib_ringz, 1728Δ − (c₄³ − c₆²) = −432b₂²(4b₈ − b₂b₆ + b₄²))
    disc_rel   1728Δ(a) = c₄(a)³ − c₆(a)²
  Lean 4 core only, no axioms.
-/
import lib.RingZ
import lib.WeierInv
import lib.LinComb

namespace LibWeierDisc
open DetChain LibRing LibWeierInv

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

/-- **Discriminant relation over b atoms.** -/
theorem disc_b (b2 b4 b6 b8 : R)
    (h : K.mul (castN K 4) b8 = K.add (K.mul b2 b6) (K.neg (K.mul b4 b4))) :
    K.mul (castN K 1728) (Dlb K b2 b4 b6 b8)
      = K.add (K.mul (K.mul (c4b K b2 b4) (c4b K b2 b4)) (c4b K b2 b4)) (K.neg (K.mul (c6b K b2 b4 b6) (c6b K b2 b4 b6))) := by
  have key : sub K (K.mul (castN K 1728) (Dlb K b2 b4 b6 b8))
      (K.add (K.mul (K.mul (c4b K b2 b4) (c4b K b2 b4)) (c4b K b2 b4)) (K.neg (K.mul (c6b K b2 b4 b6) (c6b K b2 b4 b6))))
      = K.mul (K.neg (K.mul (castN K 432) (K.mul b2 b2))) (sub K (K.mul (castN K 4) b8) (K.add (K.mul b2 b6) (K.neg (K.mul b4 b4)))) := by
    unfold Dlb c4b c6b; lib_ringz
  refine LibLC.close K _ _ _ key ?_
  rw [LibLC.sub_zero_of K h]; lib_ring

/-- **1728Δ = c₄³ − c₆².** -/
theorem disc_rel (a1 a2 a3 a4 a6 : R) :
    K.mul (castN K 1728) (Dl K a1 a2 a3 a4 a6)
      = K.add (K.mul (K.mul (C4 K a1 a2 a3 a4) (C4 K a1 a2 a3 a4)) (C4 K a1 a2 a3 a4))
          (K.neg (K.mul (C6 K a1 a2 a3 a4 a6) (C6 K a1 a2 a3 a4 a6))) :=
  disc_b K _ _ _ _ (b8_rel K a1 a2 a3 a4 a6)

end LibWeierDisc

#print axioms LibWeierDisc.disc_b
#print axioms LibWeierDisc.disc_rel

lib/WeierDiv.lean

lean_all/lib/WeierDiv.lean

/-
  lib/WeierDiv.lean — T46 shared library, cycle 810 (G761–G763): division polynomial ψ₃ and the duplication formula (general Weierstrass form, without division).

  P = (x₀, y₀) ∈ E, η = 2y₀ + a₁x₀ + a₃, N = 3x₀² + 2a₂x₀ + a₄ − a₁y₀ (tangent slope l satisfies lη = N).
    psi3          ψ₃(x) = 3x⁴ + b₂x³ + 3b₄x² + 3b₆x + b₈
    psi3_rel      N² + a₁Nη − (a₂ + 3x₀)η² = −ψ₃(x₀)        (−(b₂ + 12x₀) times the curve difference)
    dup_rel       N² + a₁Nη − (a₂ + 2x₀)η² = x₀⁴ − b₄x₀² − 2b₆x₀ − b₈   (x(2P)·η², −(b₂ + 8x₀) times the curve difference)
    three_tors    if lη = N then (l² + a₁l − a₂ − 2x₀ − x₀)·η² = −ψ₃(x₀): the third x of the tangent is x₀ ⇔ (η invertible) ψ₃(x₀) = 0
  Lean 4 core only, no axioms.
-/
import lib.WeierShortForm

namespace LibWeierDiv
open DetChain LibRing LibWeierChord LibWeierInv

variable {R : Type} (K : CRing R) (a1 a2 a3 a4 a6 : R)

def psi3 (x : R) : R :=
  K.add (K.add (K.add (K.add (K.mul (castN K 3) (K.mul (K.mul x x) (K.mul x x))) (K.mul (B2 K a1 a2) (K.mul (K.mul x x) x)))
    (K.mul (castN K 3) (K.mul (B4 K a1 a3 a4) (K.mul x x)))) (K.mul (castN K 3) (K.mul (B6 K a3 a6) x))) (B8 K a1 a2 a3 a4 a6)

def eta (x y : R) : R := K.add (K.add (K.mul (castN K 2) y) (K.mul a1 x)) a3
def Nn (x y : R) : R := K.add (K.add (K.add (K.mul (castN K 3) (K.mul x x)) (K.mul (castN K 2) (K.mul a2 x))) a4) (K.neg (K.mul a1 y))

def Ediff (x y : R) : R := sub K (K.add (K.add (K.mul y y) (K.mul (K.mul a1 x) y)) (K.mul a3 y))
  (K.add (K.add (K.add (K.mul (K.mul x x) x) (K.mul a2 (K.mul x x))) (K.mul a4 x)) a6)

theorem psi3_key (x y : R) :
    sub K (K.add (K.add (K.mul (Nn K a1 a2 a4 x y) (Nn K a1 a2 a4 x y)) (K.mul a1 (K.mul (Nn K a1 a2 a4 x y) (eta K a1 a3 x y))))
        (K.neg (K.mul (K.add a2 (K.mul (castN K 3) x)) (K.mul (eta K a1 a3 x y) (eta K a1 a3 x y)))))
      (K.neg (psi3 K a1 a2 a3 a4 a6 x))
      = K.mul (K.neg (K.add (B2 K a1 a2) (K.mul (castN K 12) x))) (Ediff K a1 a2 a3 a4 a6 x y) := by
  unfold Nn eta psi3 Ediff B2 B4 B6 B8; lib_ringz

/-- **ψ₃ relation.** -/
theorem psi3_rel (x y : R) (h : onW K a1 a2 a3 a4 a6 x y) :
    K.add (K.add (K.mul (Nn K a1 a2 a4 x y) (Nn K a1 a2 a4 x y)) (K.mul a1 (K.mul (Nn K a1 a2 a4 x y) (eta K a1 a3 x y))))
        (K.neg (K.mul (K.add a2 (K.mul (castN K 3) x)) (K.mul (eta K a1 a3 x y) (eta K a1 a3 x y))))
      = K.neg (psi3 K a1 a2 a3 a4 a6 x) := by
  refine LibLC.close K _ _ _ (psi3_key K a1 a2 a3 a4 a6 x y) ?_
  have : Ediff K a1 a2 a3 a4 a6 x y = K.zero := LibLC.sub_zero_of K h
  rw [this]; lib_ring

theorem dup_key (x y : R) :
    sub K (K.add (K.add (K.mul (Nn K a1 a2 a4 x y) (Nn K a1 a2 a4 x y)) (K.mul a1 (K.mul (Nn K a1 a2 a4 x y) (eta K a1 a3 x y))))
        (K.neg (K.mul (K.add a2 (K.mul (castN K 2) x)) (K.mul (eta K a1 a3 x y) (eta K a1 a3 x y)))))
      (K.add (K.add (K.add (K.mul (K.mul x x) (K.mul x x)) (K.neg (K.mul (B4 K a1 a3 a4) (K.mul x x))))
        (K.neg (K.mul (castN K 2) (K.mul (B6 K a3 a6) x)))) (K.neg (B8 K a1 a2 a3 a4 a6)))
      = K.mul (K.neg (K.add (B2 K a1 a2) (K.mul (castN K 8) x))) (Ediff K a1 a2 a3 a4 a6 x y) := by
  unfold Nn eta Ediff B2 B4 B6 B8; lib_ringz

/-- **Duplication formula: x(2P)·η² = x⁴ − b₄x² − 2b₆x − b₈.** -/
theorem dup_rel (x y : R) (h : onW K a1 a2 a3 a4 a6 x y) :
    K.add (K.add (K.mul (Nn K a1 a2 a4 x y) (Nn K a1 a2 a4 x y)) (K.mul a1 (K.mul (Nn K a1 a2 a4 x y) (eta K a1 a3 x y))))
        (K.neg (K.mul (K.add a2 (K.mul (castN K 2) x)) (K.mul (eta K a1 a3 x y) (eta K a1 a3 x y))))
      = K.add (K.add (K.add (K.mul (K.mul x x) (K.mul x x)) (K.neg (K.mul (B4 K a1 a3 a4) (K.mul x x))))
        (K.neg (K.mul (castN K 2) (K.mul (B6 K a3 a6) x)))) (K.neg (B8 K a1 a2 a3 a4 a6)) := by
  refine LibLC.close K _ _ _ (dup_key K a1 a2 a3 a4 a6 x y) ?_
  have : Ediff K a1 a2 a3 a4 a6 x y = K.zero := LibLC.sub_zero_of K h
  rw [this]; lib_ring

/-- **3-torsion: if lη = N then (third x of the tangent − x₀)·η² = −ψ₃(x₀).** -/
theorem three_tors (x y l : R) (h : onW K a1 a2 a3 a4 a6 x y) (hl : K.mul l (eta K a1 a3 x y) = Nn K a1 a2 a4 x y) :
    K.mul (K.add (K.add (K.add (K.add (K.mul l l) (K.mul a1 l)) (K.neg a2)) (K.neg x)) (K.neg (K.mul (castN K 2) x)))
        (K.mul (eta K a1 a3 x y) (eta K a1 a3 x y)) = K.neg (psi3 K a1 a2 a3 a4 a6 x) := by
  rw [← psi3_rel K a1 a2 a3 a4 a6 x y h, ← hl]
  generalize eta K a1 a3 x y = e; lib_ring

end LibWeierDiv

#print axioms LibWeierDiv.psi3_rel
#print axioms LibWeierDiv.dup_rel
#print axioms LibWeierDiv.three_tors

lib/WeierDom.lean

lean_all/lib/WeierDom.lean

/-
  lib/WeierDom.lean — T46 shared library, cycle 786 (G718, G720): 2-torsion and the integral-domain version of the chord law (general Weierstrass form).

    two_tors      y = −y − a₁x − a₃ (P = −P) ⇔ η(P) = 2y + a₁x + a₃ = 0;  then 4x³ + b₂x² + 2b₄x + b₆ = 0
    chord_dom     integral domain (NoZD), x₁ ≠ x₀: the third intersection point lies on the curve (without an inverse witness)
    chord_on_dom  P₀ + Q lies on the curve
  Lean 4 core only, no axioms.
-/
import lib.WeierSquare

namespace LibWeierDom
open DetChain LibRing LibWeierChord LibWeierSquare

variable {R : Type} (K : CRing R) (a1 a2 a3 a4 a6 : R)

def ny (x y : R) : R := K.add (K.add (K.neg y) (K.neg (K.mul a1 x))) (K.neg a3)

/-- **P = −P ⇔ η = 0.** -/
theorem two_tors (x y : R) : (y = ny K a1 a3 x y) ↔ eta K a1 a3 x y = K.zero :=
  ⟨fun h => by unfold eta; unfold ny at h; lib_lc K [h] [K.one],
   fun h => by unfold ny; unfold eta at h; lib_lc K [h] [K.one]⟩

theorem two_tors_quartic (x y : R) (hP : onW K a1 a2 a3 a4 a6 x y) (h : y = ny K a1 a3 x y) :
    quartic K a1 a2 a3 a4 a6 x = K.zero := by
  rw [← sq_complete K a1 a2 a3 a4 a6 x y hP, (two_tors K a1 a3 x y).1 h]; lib_ring

/-- **Third intersection point, integral-domain version.** -/
theorem chord_dom (hK : NoZD K) (x0 y0 x1 l : R) (h0 : onW K a1 a2 a3 a4 a6 x0 y0)
    (h1 : onW K a1 a2 a3 a4 a6 x1 (K.add y0 (K.mul l (K.add x1 (K.neg x0))))) (hne : K.add x1 (K.neg x0) ≠ K.zero) :
    onW K a1 a2 a3 a4 a6 (K.add (K.add (K.add (K.add (K.mul l l) (K.mul a1 l)) (K.neg a2)) (K.neg x0)) (K.neg x1)) (K.add y0 (K.mul l (K.add (K.add (K.add (K.add (K.add (K.mul l l) (K.mul a1 l)) (K.neg a2)) (K.neg x0)) (K.neg x1)) (K.neg x0)))) := by
  unfold onW at h0 h1 ⊢
  have hA : K.mul (K.add (K.add (K.add (K.add (K.add (K.add (K.mul (K.mul (K.add K.one K.one) l) (K.add y0 (K.neg (K.mul l x0)))) (K.mul a1 (K.add y0 (K.neg (K.mul l x0))))) (K.mul a3 l)) (K.neg a4)) (K.mul x0 x1)) (K.mul x0 (K.add (K.add (K.add (K.add (K.mul l l) (K.mul a1 l)) (K.neg a2)) (K.neg x0)) (K.neg x1)))) (K.mul x1 (K.add (K.add (K.add (K.add (K.mul l l) (K.mul a1 l)) (K.neg a2)) (K.neg   -- (+55 chars; full line in the Zenodo source)
    lib_lc K [h1, h0] [K.one, K.neg K.one]
  have hA0 : (K.add (K.add (K.add (K.add (K.add (K.add (K.mul (K.mul (K.add K.one K.one) l) (K.add y0 (K.neg (K.mul l x0)))) (K.mul a1 (K.add y0 (K.neg (K.mul l x0))))) (K.mul a3 l)) (K.neg a4)) (K.mul x0 x1)) (K.mul x0 (K.add (K.add (K.add (K.add (K.mul l l) (K.mul a1 l)) (K.neg a2)) (K.neg x0)) (K.neg x1)))) (K.mul x1 (K.add (K.add (K.add (K.add (K.mul l l) (K.mul a1 l)) (K.neg a2)) (K.neg x0))   -- (+28 chars; full line in the Zenodo source)
    cases hK _ _ hA with
    | inl h => exact h
    | inr h => exact absurd h hne
  lib_lc K [h0, hA0] [K.one, (K.add (K.add (K.add (K.add (K.add (K.mul l l) (K.mul a1 l)) (K.neg a2)) (K.neg x0)) (K.neg x1)) (K.neg x0))]

theorem chord_on_dom (hK : NoZD K) (x0 y0 x1 l : R) (h0 : onW K a1 a2 a3 a4 a6 x0 y0)
    (h1 : onW K a1 a2 a3 a4 a6 x1 (K.add y0 (K.mul l (K.add x1 (K.neg x0))))) (hne : K.add x1 (K.neg x0) ≠ K.zero) :
    onW K a1 a2 a3 a4 a6 (K.add (K.add (K.add (K.add (K.mul l l) (K.mul a1 l)) (K.neg a2)) (K.neg x0)) (K.neg x1)) (ny K a1 a3 (K.add (K.add (K.add (K.add (K.mul l l) (K.mul a1 l)) (K.neg a2)) (K.neg x0)) (K.neg x1)) (K.add y0 (K.mul l (K.add (K.add (K.add (K.add (K.add (K.mul l l) (K.mul a1 l)) (K.neg a2)) (K.neg x0)) (K.neg x1)) (K.neg x0))))) :=
  neg_on K a1 a2 a3 a4 a6 _ _ (chord_dom K a1 a2 a3 a4 a6 hK x0 y0 x1 l h0 h1 hne)

end LibWeierDom

#print axioms LibWeierDom.two_tors
#print axioms LibWeierDom.two_tors_quartic
#print axioms LibWeierDom.chord_on_dom

lib/WeierFF.lean

lean_all/lib/WeierFF.lean

/-
  lib/WeierFF.lean — T46 shared library, cycle 789 (G723): point count over a finite field for general Weierstrass form (mod p).

  Finite-field data (as in SqrtCountFF: els contains every element once, x^{n+1} = 1 (x ≠ 0), q·1 = 0, q = n + 2, n + 1 = 2h).
    inv2          inverse of 2 = 2^n
    count_ff      Σ_x Σ_y [(x, y) ∈ E] = Σ_x (4x³ + b₂x² + 2b₄x + b₆)^h        (in K, i.e. #E_aff mod p)
  Lean 4 core only, no axioms.
-/
import lib.WeierCount
import lib.SqrtCountFF

namespace LibWeierFF
open DetChain LibRing LibSum LibLegSum LibWeierChord LibWeierSquare

variable {R : Type} [DecidableEq R] (K : CRing R) (a1 a2 a3 a4 a6 : R)
  (hK : NoZD K) (h10 : K.one ≠ K.zero) (els : List R) (hd : LibPoly.Dist els) (hall : ∀ x, x ∈ els) (n : Nat)
  (hlen : els.length = n + 2) (hfer : ∀ x, x ≠ K.zero → pw K x (n + 1) = K.one) (hq : castN K (n + 2) = K.zero)
  (h : Nat) (hn : n + 1 = 2 * h)

include h10 hfer hq hn in
theorem inv2 : K.mul (pw K (castN K 2) n) (castN K 2) = K.one :=
  hfer _ (LibSqrtFF.two_ne K h10 n hq h hn)

include hK h10 hd hall hlen hfer hq hn in
/-- **#E_aff ≡ Σ_x quartic(x)^h (mod p).** -/
theorem count_ff :
    sum (toSemi K) (fun x => sum (toSemi K) (fun y => indK (toSemi K) (decide (onW K a1 a2 a3 a4 a6 x y))) els) els
      = sum (toSemi K) (fun x => pw K (quartic K a1 a2 a3 a4 a6 x) h) els := by
  rw [sum_ext (toSemi K) _ _ (fun x => LibWeierCount.count_eta K a1 a2 a3 a4 a6 _ (inv2 K h10 n hfer hq h hn) els hd hall x)]
  exact LibSqrtFF.point_count K hK h10 els hd hall n hlen hfer hq h hn _

end LibWeierFF

#print axioms LibWeierFF.count_ff

lib/WeierInv.lean

lean_all/lib/WeierInv.lean

/-
  lib/WeierInv.lean — T46 shared library, cycle 783 (G713, G714): Weierstrass invariants b₂, b₄, b₆, b₈, c₄, c₆, Δ.

    b8_rel      4b₈ = b₂b₆ − b₄²
    (1728Δ = c₄³ − c₆² deferred: lib_ring kernel evaluation too heavy — LOG.md, G713b)
    b2_change, b4_change, b6_change   transformation of b_i under change of variables (a_i(b, u, r, s, t) of WeierChange)
    c4_change   c₄(a) = u⁴c₄(b);  c6_change: c₆(a) = u⁶c₆(b)
  (Large expansions exceed lib_ring's recursion limit, so b_i are kept as atoms and the work is split into steps.)
  Lean 4 core only, no axioms.
-/
import lib.WeierChange

namespace LibWeierInv
open DetChain LibRing

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

def B2 (a1 a2 : R) : R := (K.add (K.mul a1 a1) (K.mul (castN K 4) a2))
def B4 (a1 a3 a4 : R) : R := (K.add (K.mul (K.mul (K.add K.one K.one) K.one) a4) (K.mul a1 a3))
def B6 (a3 a6 : R) : R := (K.add (K.mul a3 a3) (K.mul (castN K 4) a6))
def B8 (a1 a2 a3 a4 a6 : R) : R := (K.add (K.add (K.add (K.add (K.mul (K.mul a1 a1) a6) (K.mul (K.mul (castN K 4) a2) a6)) (K.neg (K.mul (K.mul a1 a3) a4))) (K.mul (K.mul a2 a3) a3)) (K.neg (K.mul a4 a4)))
def c4b (b2 b4 : R) : R := (K.add (K.mul b2 b2) (K.neg (K.mul (castN K 24) b4)))
def c6b (b2 b4 b6 : R) : R := (K.add (K.add (K.neg (K.mul (K.mul b2 b2) b2)) (K.mul (K.mul (castN K 36) b2) b4)) (K.neg (K.mul (K.mul (castN K 6) (castN K 36)) b6)))
def Dlb (b2 b4 b6 b8 : R) : R := (K.add (K.add (K.add (K.neg (K.mul (K.mul b2 b2) b8)) (K.neg (K.mul (castN K 8) (K.mul (K.mul b4 b4) b4)))) (K.neg (K.mul (K.mul (castN K 27) b6) b6))) (K.mul (K.mul (K.mul (castN K 9) b2) b4) b6))
def C4 (a1 a2 a3 a4 : R) : R := c4b K (B2 K a1 a2) (B4 K a1 a3 a4)
def C6 (a1 a2 a3 a4 a6 : R) : R := c6b K (B2 K a1 a2) (B4 K a1 a3 a4) (B6 K a3 a6)
def Dl (a1 a2 a3 a4 a6 : R) : R := Dlb K (B2 K a1 a2) (B4 K a1 a3 a4) (B6 K a3 a6) (B8 K a1 a2 a3 a4 a6)

theorem b8_rel (a1 a2 a3 a4 a6 : R) : (K.mul (castN K 4) (B8 K a1 a2 a3 a4 a6)) = (K.add (K.mul (B2 K a1 a2) (B6 K a3 a6)) (K.neg (K.mul (B4 K a1 a3 a4) (B4 K a1 a3 a4)))) := by
  unfold B2 B4 B6 B8; lib_ring

variable (u r s t b1 b2 b3 b4 b6 : R)

theorem b2_change : B2 K (K.add (K.mul u b1) (K.neg (K.mul (K.mul (K.add K.one K.one) K.one) s))) (K.add (K.add (K.add (K.mul (K.mul u u) b2) (K.mul s (K.add (K.mul u b1) (K.neg (K.mul (K.mul (K.add K.one K.one) K.one) s))))) (K.neg (K.mul (K.add (K.mul (K.add K.one K.one) K.one) K.one) r))) (K.mul s s)) = (K.add (K.mul (K.mul u u) (B2 K b1 b2)) (K.neg (K.mul (castN K 12) r))) := by
  unfold B2; lib_ring

theorem b4_change : B4 K (K.add (K.mul u b1) (K.neg (K.mul (K.mul (K.add K.one K.one) K.one) s))) (K.add (K.add (K.mul (K.mul (K.mul u u) u) b3) (K.neg (K.mul r (K.add (K.mul u b1) (K.neg (K.mul (K.mul (K.add K.one K.one) K.one) s)))))) (K.neg (K.mul (K.mul (K.add K.one K.one) K.one) t))) (K.add (K.add (K.add (K.add (K.add (K.mul (K.mul (K.mul (K.mul u u) u) u) b4) (K.mul s (K.add (K.add (K.mul (K  -- (+838 chars; full line in the Zenodo source)
  unfold B2 B4; lib_ring

theorem b6_change : B6 K (K.add (K.add (K.mul (K.mul (K.mul u u) u) b3) (K.neg (K.mul r (K.add (K.mul u b1) (K.neg (K.mul (K.mul (K.add K.one K.one) K.one) s)))))) (K.neg (K.mul (K.mul (K.add K.one K.one) K.one) t))) (K.add (K.add (K.add (K.add (K.add (K.add (K.mul (K.mul (K.mul (K.mul (K.mul (K.mul u u) u) u) u) u) b6) (K.neg (K.mul r (K.add (K.add (K.add (K.add (K.add (K.mul (K.mul (K.mul (K.mul  -- (+1628 chars; full line in the Zenodo source)
  unfold B2 B4 B6; lib_ring

theorem c4_change : C4 K (K.add (K.mul u b1) (K.neg (K.mul (K.mul (K.add K.one K.one) K.one) s))) (K.add (K.add (K.add (K.mul (K.mul u u) b2) (K.mul s (K.add (K.mul u b1) (K.neg (K.mul (K.mul (K.add K.one K.one) K.one) s))))) (K.neg (K.mul (K.add (K.mul (K.add K.one K.one) K.one) K.one) r))) (K.mul s s)) (K.add (K.add (K.mul (K.mul (K.mul u u) u) b3) (K.neg (K.mul r (K.add (K.mul u b1) (K.neg (K.m  -- (+950 chars; full line in the Zenodo source)
  show c4b K (B2 K _ _) (B4 K _ _ _) = _
  rw [b2_change, b4_change]
  show _ = K.mul _ (c4b K (B2 K b1 b2) (B4 K b1 b3 b4))
  unfold c4b
  generalize (B2 K b1 b2) = x; generalize (B4 K b1 b3 b4) = y; lib_ring

set_option maxRecDepth 100000 in
theorem c6_shift (r X Y Z : R) :
    c6b K (K.add X (K.neg (K.mul (castN K 12) r))) (K.add (K.add Y (K.neg (K.mul r X))) (K.mul (castN K 6) (K.mul r r)))
      (K.add (K.add (K.add Z (K.neg (K.mul (K.add K.one K.one) (K.mul r Y)))) (K.mul (K.mul r r) X)) (K.neg (K.mul (castN K 4) (K.mul (K.mul r r) r))))
      = c6b K X Y Z := by
  unfold c6b; lib_ring

theorem c6_scale (u x y z : R) :
    c6b K (K.mul (K.mul u u) x) (K.mul (K.mul (K.mul (K.mul u u) u) u) y) (K.mul (K.mul (K.mul (K.mul (K.mul (K.mul u u) u) u) u) u) z)
      = K.mul (K.mul (K.mul (K.mul (K.mul (K.mul u u) u) u) u) u) (c6b K x y z) := by
  unfold c6b; lib_ring

theorem c6_shift_scale (u r x y z : R) : c6b K (K.add (K.mul (K.mul u u) x) (K.neg (K.mul (castN K 12) r))) (K.add (K.add (K.mul (K.mul (K.mul (K.mul u u) u) u) y) (K.neg (K.mul (K.mul r (K.mul u u)) x))) (K.mul (K.mul (castN K 6) r) r)) (K.add (K.add (K.add (K.mul (K.mul (K.mul (K.mul (K.mul (K.mul u u) u) u) u) u) z) (K.neg (K.mul (K.mul (K.mul (K.mul (K.add K.one K.one) K.one) r) (K.mul (K.mul   -- (+197 chars; full line in the Zenodo source)
  rw [show (K.add (K.mul (K.mul u u) x) (K.neg (K.mul (castN K 12) r))) = (K.add (K.mul (K.mul u u) x) (K.neg (K.mul (castN K 12) r))) by lib_ring, show (K.add (K.add (K.mul (K.mul (K.mul (K.mul u u) u) u) y) (K.neg (K.mul (K.mul r (K.mul u u)) x))) (K.mul (K.mul (castN K 6) r) r)) = (K.add (K.add (K.mul (K.mul (K.mul (K.mul u u) u) u) y) (K.neg (K.mul r (K.mul (K.mul u u) x)))) (K.mul (castN K 6)  -- (+593 chars; full line in the Zenodo source)
    c6_shift, c6_scale]

theorem c6_change : C6 K (K.add (K.mul u b1) (K.neg (K.mul (K.mul (K.add K.one K.one) K.one) s))) (K.add (K.add (K.add (K.mul (K.mul u u) b2) (K.mul s (K.add (K.mul u b1) (K.neg (K.mul (K.mul (K.add K.one K.one) K.one) s))))) (K.neg (K.mul (K.add (K.mul (K.add K.one K.one) K.one) K.one) r))) (K.mul s s)) (K.add (K.add (K.mul (K.mul (K.mul u u) u) b3) (K.neg (K.mul r (K.add (K.mul u b1) (K.neg (K.m  -- (+2461 chars; full line in the Zenodo source)
  show c6b K (B2 K _ _) (B4 K _ _ _) (B6 K _ _) = _
  rw [b2_change, b4_change, b6_change]
  show _ = K.mul _ (c6b K (B2 K b1 b2) (B4 K b1 b3 b4) (B6 K b3 b6))
  generalize (B2 K b1 b2) = x; generalize (B4 K b1 b3 b4) = y; generalize (B6 K b3 b6) = z
  exact c6_shift_scale K u r x y z

end LibWeierInv

#print axioms LibWeierInv.c4_change
#print axioms LibWeierInv.c6_change

lib/WeierShort.lean

lean_all/lib/WeierShort.lean

/-
  lib/WeierShort.lean — T46 shared library, cycle 784 (G717): invariants of the short form y² = x³ + Ax + B.

    short_b       b₂ = 0, b₄ = 2A, b₆ = 4B, b₈ = −A²
    short_c4      c₄ = −48A
    short_disc    Δ = −16(4A³ + 27B²)
  Lean 4 core only, no axioms.
-/
import lib.WeierInv

namespace LibWeierShort
open DetChain LibRing LibWeierInv

variable {R : Type} (K : CRing R) (A B : R)

theorem short_b2 : B2 K K.zero K.zero = K.zero := by unfold B2; lib_ring
theorem short_b4 : B4 K K.zero K.zero A = K.mul (castN K 2) A := by unfold B4; lib_ring
theorem short_b6 : B6 K K.zero B = K.mul (castN K 4) B := by unfold B6; lib_ring
theorem short_b8 : B8 K K.zero K.zero K.zero A B = K.neg (K.mul A A) := by unfold B8; lib_ring

theorem short_c4 : C4 K K.zero K.zero K.zero A = K.neg (K.mul (castN K 48) A) := by
  show c4b K (B2 K _ _) (B4 K _ _ _) = _
  rw [short_b2, short_b4]; unfold c4b; lib_ring

/-- **Δ = −16(4A³ + 27B²).** -/
theorem short_disc : Dl K K.zero K.zero K.zero A B
    = K.neg (K.mul (castN K 16) (K.add (K.mul (castN K 4) (K.mul (K.mul A A) A)) (K.mul (castN K 27) (K.mul B B)))) := by
  show Dlb K (B2 K _ _) (B4 K _ _ _) (B6 K _ _) (B8 K _ _ _ _ _) = _
  rw [short_b2, short_b4, short_b6, short_b8]; unfold Dlb; lib_ring

end LibWeierShort

#print axioms LibWeierShort.short_c4
#print axioms LibWeierShort.short_disc

lib/WeierShortForm.lean

lean_all/lib/WeierShortForm.lean

/-
  lib/WeierShortForm.lean — T46 shared library, cycle 809 (G760): to short form (without division).

    short_map   (x, y) ∈ E_a ⇒ (X, Y) = (36x + 3b₂, 108(2y + a₁x + a₃)) lies on Y² = X³ − 27c₄X − 54c₆
                (Y² − (X³ − 27c₄X − 54c₆) = 46656·(difference of E_a), lib_ringz)
    short_back  converse direction if 6 is not a zero divisor (6z = 0 ⇒ z = 0)
  Lean 4 core only, no axioms.
-/
import lib.WeierDelta

namespace LibWeierShortForm
open DetChain LibRing LibWeierChord LibWeierInv

variable {R : Type} (K : CRing R) (a1 a2 a3 a4 a6 : R)

def SX (x : R) : R := K.add (K.mul (castN K 36) x) (K.mul (castN K 3) (B2 K a1 a2))
def SY (x y : R) : R := K.mul (castN K 108) (K.add (K.add (K.mul (castN K 2) y) (K.mul a1 x)) a3)
def SR (X : R) : R := K.add (K.add (K.mul (K.mul X X) X) (K.neg (K.mul (castN K 27) (K.mul (C4 K a1 a2 a3 a4) X))))
  (K.neg (K.mul (castN K 54) (C6 K a1 a2 a3 a4 a6)))

theorem key (x y : R) :
    sub K (K.mul (SY K a1 a3 x y) (SY K a1 a3 x y)) (SR K a1 a2 a3 a4 a6 (SX K a1 a2 x))
      = K.mul (castN K 46656) (sub K (K.add (K.add (K.mul y y) (K.mul (K.mul a1 x) y)) (K.mul a3 y))
          (K.add (K.add (K.add (K.mul (K.mul x x) x) (K.mul a2 (K.mul x x))) (K.mul a4 x)) a6)) := by
  unfold SY SR SX C4 C6 c4b c6b B2 B4 B6; lib_ringz

/-- **Map to short form.** -/
theorem short_map (x y : R) (h : onW K a1 a2 a3 a4 a6 x y) :
    K.mul (SY K a1 a3 x y) (SY K a1 a3 x y) = SR K a1 a2 a3 a4 a6 (SX K a1 a2 x) := by
  unfold onW at h
  refine LibLC.close K _ _ _ (key K a1 a2 a3 a4 a6 x y) ?_
  rw [LibLC.sub_zero_of K h]; lib_ring

/-- **Converse direction (if 46656 = 6⁶ is not a zero divisor).** -/
theorem short_back (x y : R) (h6 : ∀ z, K.mul (castN K 46656) z = K.zero → z = K.zero)
    (h : K.mul (SY K a1 a3 x y) (SY K a1 a3 x y) = SR K a1 a2 a3 a4 a6 (SX K a1 a2 x)) : onW K a1 a2 a3 a4 a6 x y := by
  unfold onW
  have e := h6 _ ((key K a1 a2 a3 a4 a6 x y).symm.trans (LibLC.sub_zero_of K h))
  exact (LibLC.close K _ _ _ rfl e)

end LibWeierShortForm

#print axioms LibWeierShortForm.short_map
#print axioms LibWeierShortForm.short_back

lib/WeierSquare.lean

lean_all/lib/WeierSquare.lean

/-
  lib/WeierSquare.lean — T46 shared library, cycle 785 (G716): completing the square (without division).

    sq_complete   (x, y) ∈ E ⇒ (2y + a₁x + a₃)² = 4x³ + b₂x² + 2b₄x + b₆
    sq_converse   (2y + a₁x + a₃)² = 4x³ + b₂x² + 2b₄x + b₆, 4 not a zero divisor (4z = 0 ⇒ z = 0) ⇒ (x, y) ∈ E
  (If 2 is invertible, E ≅ η² = 4x³ + b₂x² + 2b₄x + b₆ with η = 2y + a₁x + a₃ — the step moving discriminant and point counts to the short form.)
  Lean 4 core only, no axioms.
-/
import lib.WeierShort

namespace LibWeierSquare
open DetChain LibRing LibWeierChord LibWeierInv

variable {R : Type} (K : CRing R) (a1 a2 a3 a4 a6 : R)

def eta (x y : R) : R := K.add (K.add (K.mul (castN K 2) y) (K.mul a1 x)) a3

def quartic (x : R) : R :=
  K.add (K.add (K.add (K.mul (castN K 4) (K.mul (K.mul x x) x)) (K.mul (B2 K a1 a2) (K.mul x x)))
    (K.mul (castN K 2) (K.mul (B4 K a1 a3 a4) x))) (B6 K a3 a6)

/-- **Completing the square.** -/
theorem sq_complete (x y : R) (h : onW K a1 a2 a3 a4 a6 x y) :
    K.mul (eta K a1 a3 x y) (eta K a1 a3 x y) = quartic K a1 a2 a3 a4 a6 x := by
  unfold onW at h; unfold eta quartic B2 B4 B6
  lib_lc K [h] [castN K 4]

/-- **Converse (if 4 is not a zero divisor).** -/
theorem sq_converse (x y : R) (h4 : ∀ z, K.mul (castN K 4) z = K.zero → z = K.zero)
    (h : K.mul (eta K a1 a3 x y) (eta K a1 a3 x y) = quartic K a1 a2 a3 a4 a6 x) : onW K a1 a2 a3 a4 a6 x y := by
  unfold onW
  have e := h4 (K.add (K.add (K.add (K.mul y y) (K.mul (K.mul a1 x) y)) (K.mul a3 y))
    (K.neg (K.add (K.add (K.add (K.mul (K.mul x x) x) (K.mul a2 (K.mul x x))) (K.mul a4 x)) a6))) (by
      unfold eta quartic B2 B4 B6 at h
      lib_lc K [h] [K.one])
  lib_lc K [e] [K.one]

end LibWeierSquare

#print axioms LibWeierSquare.sq_complete
#print axioms LibWeierSquare.sq_converse

lib/WeierTwoCount.lean

lean_all/lib/WeierTwoCount.lean

/-
  lib/WeierTwoCount.lean — T46 shared library, cycle 792 (G725): number of affine 2-torsion points = number of roots of a quartic (2 invertible).

    sum_eta       Σ_y G(2y + a₁x + a₃) = Σ_η G(η)                (over els, 2 invertible)
    sum_ind_eq    Σ_z [z = c] = 1                                 (Dist, c ∈ els)
    count_two     Σ_y [(x, y) ∈ E ∧ P = −P] = [4x³ + b₂x² + 2b₄x + b₆ = 0]
  Lean 4 core only, no axioms.
-/
import lib.WeierCancel

namespace LibWeierTwoCount
open DetChain LibRing LibSum LibLegSum LibWeierChord LibWeierSquare LibWeierDom

variable {R : Type} [DecidableEq R] (K : CRing R) (a1 a2 a3 a4 a6 : R)

theorem sum_eta {L : Type} (M : CRing L) (i2 : R) (hi2 : K.mul i2 (castN K 2) = K.one) (els : List R) (hd : LibPoly.Dist els)
    (hall : ∀ z, z ∈ els) (x : R) (G : R → L) :
    sum (toSemi M) (fun y => G (eta K a1 a3 x y)) els = sum (toSemi M) G els := by
  have e := LibWeierChangeCount.sum_affine K M (castN K 2) (K.add (K.mul a1 x) a3) i2 hi2 els hd hall G
  refine Eq.trans ?_ e.symm
  apply sum_ext (toSemi M); intro y
  show G (K.add (K.add (K.mul (castN K 2) y) (K.mul a1 x)) a3) = G (K.add (K.mul (castN K 2) y) (K.add (K.mul a1 x) a3))
  rw [K.add_assoc]

theorem sum_ind_eq (c : R) : ∀ (els : List R), LibPoly.Dist els → c ∈ els →
    sum (toSemi K) (fun z => indK (toSemi K) (decide (z = c))) els = K.one
  | a :: l, ⟨hn, hd⟩, hc => by
    show K.add (indK (toSemi K) (decide (a = c))) (sum (toSemi K) _ l) = K.one
    by_cases hac : a = c
    · subst hac
      rw [decide_eq_true rfl]
      have h0 : sum (toSemi K) (fun z => indK (toSemi K) (decide (z = a))) l = K.zero := by
        rw [sum_ext_mem (toSemi K) _ (fun _ => K.zero) l (fun z hz => by rw [decide_eq_false (hn z hz)]; rfl)]
        exact sum_zero (toSemi K) l
      rw [h0]; show K.add K.one K.zero = K.one; lib_ring
    · rw [decide_eq_false hac]
      have hcl : c ∈ l := by
        cases hc with
        | head => exact absurd rfl hac
        | tail _ h => exact h
      rw [sum_ind_eq c l hd hcl]; show K.add K.zero K.one = K.one; lib_ring

/-- **Number of affine 2-torsion points.** -/
theorem count_two (i2 : R) (hi2 : K.mul i2 (castN K 2) = K.one) (els : List R) (hd : LibPoly.Dist els)
    (hall : ∀ z, z ∈ els) (x : R) :
    sum (toSemi K) (fun y => indK (toSemi K) (decide (onW K a1 a2 a3 a4 a6 x y ∧ eta K a1 a3 x y = K.zero))) els
      = indK (toSemi K) (decide (quartic K a1 a2 a3 a4 a6 x = K.zero)) := by
  have h4 : ∀ z, K.mul (castN K 4) z = K.zero → z = K.zero := fun z hz => by
    have h2 : K.mul i2 (K.add K.one (K.add K.one K.zero)) = K.one := hi2
    have e : K.mul (K.mul i2 i2) (castN K 4) = K.one := by
      show K.mul (K.mul i2 i2) (K.add K.one (K.add K.one (K.add K.one (K.add K.one K.zero)))) = K.one
      lib_lc K [h2] [K.add (K.mul i2 (K.add K.one K.one)) K.one]
    have : z = K.mul (K.mul i2 i2) (K.mul (castN K 4) z) := by rw [← K.mul_assoc, e, K.one_mul]
    rw [this, hz]; lib_ring
  have step : ∀ y, indK (toSemi K) (decide (onW K a1 a2 a3 a4 a6 x y ∧ eta K a1 a3 x y = K.zero))
      = (fun η => K.mul (indK (toSemi K) (decide (η = K.zero))) (indK (toSemi K) (decide (quartic K a1 a2 a3 a4 a6 x = K.zero))))
          (eta K a1 a3 x y) := fun y => by
    show _ = K.mul (indK (toSemi K) (decide (eta K a1 a3 x y = K.zero))) _
    by_cases he : eta K a1 a3 x y = K.zero
    · rw [decide_eq_true he]
      have hiff : onW K a1 a2 a3 a4 a6 x y ↔ quartic K a1 a2 a3 a4 a6 x = K.zero := ⟨fun h => by
          rw [← sq_complete K a1 a2 a3 a4 a6 x y h, he]; lib_ring,
        fun h => sq_converse K a1 a2 a3 a4 a6 x y h4 (by rw [he, h]; lib_ring)⟩
      by_cases hq : quartic K a1 a2 a3 a4 a6 x = K.zero
      · rw [decide_eq_true ⟨hiff.2 hq, he⟩, decide_eq_true hq]; show K.one = K.mul K.one K.one; lib_ring
      · rw [decide_eq_false (fun h => hq (hiff.1 h.1)), decide_eq_false hq]; show K.zero = K.mul K.one K.zero; lib_ring
    · rw [decide_eq_false (fun h => he h.2), decide_eq_false he]
      show K.zero = K.mul K.zero _; generalize indK (toSemi K) _ = d; lib_ring
  rw [sum_ext (toSemi K) _ _ step]
  refine Eq.trans (sum_eta K a1 a3 K i2 hi2 els hd hall x (fun η => K.mul (indK (toSemi K) (decide (η = K.zero)))
    (indK (toSemi K) (decide (quartic K a1 a2 a3 a4 a6 x = K.zero))))) ?_
  refine Eq.trans (sum_mul (toSemi K) (indK (toSemi K) (decide (quartic K a1 a2 a3 a4 a6 x = K.zero)))
    (fun η => indK (toSemi K) (decide (η = K.zero))) els).symm ?_
  show K.mul (sum (toSemi K) (fun η => indK (toSemi K) (decide (η = K.zero))) els) _ = _
  rw [sum_ind_eq K K.zero els hd (hall _)]
  generalize indK (toSemi K) (decide (quartic K a1 a2 a3 a4 a6 x = K.zero)) = d; lib_ring

end LibWeierTwoCount

#print axioms LibWeierTwoCount.sum_eta
#print axioms LibWeierTwoCount.count_two

lib/Weierstrass.lean

lean_all/lib/Weierstrass.lean

/-
  lib/Weierstrass.lean — T46 shared library: Weierstrass product inequalities (ordered ring).

    prod1p_ge     x_i ≥ 0:       1 + p_1 ≤ Π (1 + x_i)
    prod1m_ge     0 ≤ x_i ≤ 1:   1 − p_1 ≤ Π (1 − x_i)
    prod1m_nn     0 ≤ x_i ≤ 1:   0 ≤ Π (1 − x_i)
    prod1p_sub    x_i ≥ 0, S ⊑ l:  Π_S (1 + x) ≤ Π_l (1 + x)
  Lean 4 core only, proofs by hand, no axioms.
-/
import lib.EsymSumProd
import lib.PsumBasic

namespace LibWeierstrass
open DetChain LibRing LibNewton YMChain LibPascalLC LibEsymSumProd

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

def prod1m : List R → R
  | [] => K.one
  | a :: l => K.mul (sub K K.one a) (prod1m l)

theorem prod1p_ge : ∀ l : List R, (∀ x, List.Mem x l → O.le K.zero x) →
    O.le (K.add K.one (psum K l 1)) (prod1p K l)
  | [], _ => by
    show O.le (K.add K.one K.zero) K.one
    exact congr_le K O (by lib_ring) rfl (O.le_refl K.one)
  | a :: l, hx => by
    have ha := hx a (List.Mem.head l)
    have hl : ∀ x, List.Mem x l → O.le K.zero x := fun x hm => hx x (List.Mem.tail a hm)
    have ih := prod1p_ge l hl
    have h1p : O.le K.zero (K.add K.one a) :=
      congr_le K O (by lib_ring) rfl (O.add_le_add _ _ _ _ (LibLCHeat.zero_le_one K O) ha)
    have h1 := O.mul_le_mul_right _ _ _ ih h1p
    have h2 := O.mul_nonneg _ _ ha (LibPsumBasic.psum_nn K O 1 l hl)
    show O.le (K.add K.one (K.add (K.mul K.one a) (psum K l 1))) (K.mul (K.add K.one a) (prod1p K l))
    exact O.le_trans _ _ _
      (congr_le K O (by lib_ring) rfl (O.add_le_add _ _ _ _ (O.le_refl (K.add K.one (K.add a (psum K l 1)))) h2))
      (congr_le K O (by lib_ring) (by lib_ring) h1)

theorem prod1m_nn : ∀ l : List R, (∀ x, List.Mem x l → O.le K.zero x ∧ O.le x K.one) → O.le K.zero (prod1m K l)
  | [], _ => LibLCHeat.zero_le_one K O
  | a :: l, hx => O.mul_nonneg _ _ (LibChebPow.sub_nn K O (hx a (List.Mem.head l)).2)
      (prod1m_nn l (fun x hm => hx x (List.Mem.tail a hm)))

theorem prod1m_ge : ∀ l : List R, (∀ x, List.Mem x l → O.le K.zero x ∧ O.le x K.one) →
    O.le (sub K K.one (psum K l 1)) (prod1m K l)
  | [], _ => by
    show O.le (sub K K.one K.zero) K.one
    exact congr_le K O (by unfold sub; lib_ring) rfl (O.le_refl K.one)
  | a :: l, hx => by
    have ha := hx a (List.Mem.head l)
    have hl : ∀ x, List.Mem x l → O.le K.zero x ∧ O.le x K.one := fun x hm => hx x (List.Mem.tail a hm)
    have ih := prod1m_ge l hl
    have h1 := O.mul_le_mul_right _ _ _ ih (LibChebPow.sub_nn K O ha.2)
    have hp : O.le K.zero (psum K l 1) := LibPsumBasic.psum_nn K O 1 l (fun x hm => (hl x hm).1)
    have h2 := O.mul_nonneg _ _ ha.1 hp
    show O.le (sub K K.one (K.add (K.mul K.one a) (psum K l 1))) (K.mul (sub K K.one a) (prod1m K l))
    exact O.le_trans _ _ _
      (congr_le K O (by lib_ring) rfl
        (O.add_le_add _ _ _ _ (O.le_refl (sub K K.one (K.add a (psum K l 1)))) h2))
      (congr_le K O (by unfold sub; lib_ring) (by lib_ring) h1)

theorem prod1p_nn : ∀ l : List R, (∀ x, List.Mem x l → O.le K.zero x) → O.le K.zero (prod1p K l)
  | [], _ => LibLCHeat.zero_le_one K O
  | a :: l, hx => O.mul_nonneg _ _
      (congr_le K O (by lib_ring) rfl (O.add_le_add _ _ _ _ (LibLCHeat.zero_le_one K O) (hx a (List.Mem.head l))))
      (prod1p_nn l (fun x hm => hx x (List.Mem.tail a hm)))

theorem prod1p_le_cons (a : R) (ha : O.le K.zero a) (l : List R) (hl : ∀ x, List.Mem x l → O.le K.zero x) :
    O.le (prod1p K l) (prod1p K (a :: l)) := by
  have h2 := O.mul_le_mul_right _ _ _ ha (prod1p_nn K O l hl)
  show O.le (prod1p K l) (K.mul (K.add K.one a) (prod1p K l))
  exact congr_le K O (by lib_ring) (by lib_ring) (O.add_le_add _ _ _ _ (O.le_refl (prod1p K l)) h2)

theorem prod1p_cons_mono (a : R) (ha : O.le K.zero a) (l1 l2 : List R) (h : O.le (prod1p K l1) (prod1p K l2)) :
    O.le (prod1p K (a :: l1)) (prod1p K (a :: l2)) := by
  have h1p : O.le K.zero (K.add K.one a) :=
    congr_le K O (by lib_ring) rfl (O.add_le_add _ _ _ _ (LibLCHeat.zero_le_one K O) ha)
  show O.le (K.mul (K.add K.one a) (prod1p K l1)) (K.mul (K.add K.one a) (prod1p K l2))
  exact congr_le K O (by lib_ring) (by lib_ring) (O.mul_le_mul_right _ _ _ h h1p)

theorem prod1p_sub {S l : List R} (h : List.Sublist S l) :
    (∀ x, List.Mem x l → O.le K.zero x) → O.le (prod1p K S) (prod1p K l) := by
  induction h with
  | slnil => intro _; exact O.le_refl _
  | cons a _ ih =>
    intro hx
    have hl : ∀ x, List.Mem x _ → O.le K.zero x := fun x hm => hx x (List.Mem.tail a hm)
    exact O.le_trans _ _ _ (ih hl) (prod1p_le_cons K O a (hx a (List.Mem.head _)) _ hl)
  | cons_cons a _ ih =>
    intro hx
    have hl : ∀ x, List.Mem x _ → O.le K.zero x := fun x hm => hx x (List.Mem.tail a hm)
    exact prod1p_cons_mono K O a (hx a (List.Mem.head _)) _ _ (ih hl)

end LibWeierstrass

#print axioms LibWeierstrass.prod1p_ge
#print axioms LibWeierstrass.prod1m_nn
#print axioms LibWeierstrass.prod1m_ge
#print axioms LibWeierstrass.prod1p_sub

lib/WeightedCS.lean

lean_all/lib/WeightedCS.lean

/-
  lib/WeightedCS.lean — T46 shared library, cycle 246: weighted Cauchy–Schwarz and log-convexity of moments.

  Positive cone (LibCS.Pos), list of triples l = [(w, x, y), …]:
    wA, wB, wC       Σ w x², Σ w y², Σ w x y
    wlagrange        wA·wB − wC² = Σ_{i<j} w_i w_j (x_i y_j − x_j y_i)²       (any commutative ring)
    weighted_cs      all w ≥ 0 ⇒ wC² ≤ wA·wB
    mom S k          Σ_i u_i^k
    log_convex       u_i ≥ 0 ⇒ S_{k+1}² ≤ S_k · S_{k+2}                       (Lyapunov)
    mom_cs           S_{a+b}² ≤ S_{2a} · S_{2b}                                (all u)
  (agent2 ns/L3Interp.l3_l2l4 ((Σu³)² ≤ Σu² Σu⁴) is the case k = 2 of log_convex.)
  Lean 4 core only, no axioms.
-/
import lib.SecondMoment
import lib.Euler

namespace LibWCS
open DetChain LibRing LibCS LibGron LibSum

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

def wA : List (R × R × R) → R
  | [] => K.zero
  | t :: l => K.add (K.mul t.1 (K.mul t.2.1 t.2.1)) (wA l)
def wB : List (R × R × R) → R
  | [] => K.zero
  | t :: l => K.add (K.mul t.1 (K.mul t.2.2 t.2.2)) (wB l)
def wC : List (R × R × R) → R
  | [] => K.zero
  | t :: l => K.add (K.mul t.1 (K.mul t.2.1 t.2.2)) (wC l)

def wcross (w x y : R) : List (R × R × R) → R
  | [] => K.zero
  | q :: l => K.add (K.mul (K.mul w q.1) (K.mul (sub K (K.mul x q.2.2) (K.mul q.2.1 y)) (sub K (K.mul x q.2.2) (K.mul q.2.1 y))))
      (wcross w x y l)

def wlag : List (R × R × R) → R
  | [] => K.zero
  | t :: l => K.add (wcross K t.1 t.2.1 t.2.2 l) (wlag l)

theorem wcross_eq (w x y : R) : ∀ l : List (R × R × R),
    wcross K w x y l = K.mul w (sub K (K.add (K.mul (K.mul x x) (wB K l)) (K.mul (K.mul y y) (wA K l)))
      (K.mul (castZ K 2) (K.mul (K.mul x y) (wC K l))))
  | [] => by
    show K.zero = K.mul w (sub K (K.add (K.mul (K.mul x x) K.zero) (K.mul (K.mul y y) K.zero)) (K.mul (castZ K 2) (K.mul (K.mul x y) K.zero)))
    lib_ring
  | q :: l => by
    show K.add (K.mul (K.mul w q.1) (K.mul (sub K (K.mul x q.2.2) (K.mul q.2.1 y)) (sub K (K.mul x q.2.2) (K.mul q.2.1 y))))
        (wcross K w x y l)
      = K.mul w (sub K (K.add (K.mul (K.mul x x) (K.add (K.mul q.1 (K.mul q.2.2 q.2.2)) (wB K l)))
          (K.mul (K.mul y y) (K.add (K.mul q.1 (K.mul q.2.1 q.2.1)) (wA K l))))
          (K.mul (castZ K 2) (K.mul (K.mul x y) (K.add (K.mul q.1 (K.mul q.2.1 q.2.2)) (wC K l)))))
    rw [wcross_eq w x y l]; lib_ring

/-- **Weighted Lagrange identity.** -/
theorem wlagrange : ∀ l : List (R × R × R), sub K (K.mul (wA K l) (wB K l)) (K.mul (wC K l) (wC K l)) = wlag K l
  | [] => by show sub K (K.mul K.zero K.zero) (K.mul K.zero K.zero) = K.zero; lib_ring
  | t :: l => by
    show sub K (K.mul (K.add (K.mul t.1 (K.mul t.2.1 t.2.1)) (wA K l)) (K.add (K.mul t.1 (K.mul t.2.2 t.2.2)) (wB K l)))
        (K.mul (K.add (K.mul t.1 (K.mul t.2.1 t.2.2)) (wC K l)) (K.add (K.mul t.1 (K.mul t.2.1 t.2.2)) (wC K l)))
      = K.add (wcross K t.1 t.2.1 t.2.2 l) (wlag K l)
    rw [wcross_eq, ← wlagrange l]; lib_ring

variable (P : Pos K)

theorem nn_wcross (w x y : R) (hw : P.nn w) : ∀ l : List (R × R × R), (∀ q, q ∈ l → P.nn q.1) → P.nn (wcross K w x y l)
  | [], _ => P.nn_zero
  | q :: l, h => P.nn_add _ _ (P.nn_mul _ _ (P.nn_mul _ _ hw (h q (.head _))) (P.nn_sq _))
      (nn_wcross w x y hw l (fun q' hq => h q' (.tail _ hq)))

theorem nn_wlag : ∀ l : List (R × R × R), (∀ q, q ∈ l → P.nn q.1) → P.nn (wlag K l)
  | [], _ => P.nn_zero
  | t :: l, h => P.nn_add _ _ (nn_wcross K P t.1 t.2.1 t.2.2 (h t (.head _)) l (fun q hq => h q (.tail _ hq)))
      (nn_wlag l (fun q hq => h q (.tail _ hq)))

/-- **Weighted Cauchy–Schwarz.** -/
theorem weighted_cs (l : List (R × R × R)) (h : ∀ q, q ∈ l → P.nn q.1) :
    le K P (K.mul (wC K l) (wC K l)) (K.mul (wA K l) (wB K l)) := by
  show P.nn (sub K (K.mul (wA K l) (wB K l)) (K.mul (wC K l) (wC K l)))
  rw [wlagrange]; exact nn_wlag K P l h

/-! ## Moments -/

def mom {A : Type} (u : A → R) (k : Nat) (l : List A) : R := sum (toSemi K) (fun i => pw K (u i) k) l

theorem nn_pw (a : R) (ha : P.nn a) : ∀ k, P.nn (pw K a k)
  | 0 => by
    have := P.nn_sq K.one; rw [show K.mul K.one K.one = K.one by lib_ring] at this; exact this
  | k + 1 => P.nn_mul _ _ (nn_pw a ha k) ha

theorem nn_one : P.nn K.one := by
  have := P.nn_sq K.one; rw [show K.mul K.one K.one = K.one by lib_ring] at this; exact this

/-- Each element of a mapped list has positive first component (without List.exists_of_mem_map). -/
theorem nn_map {A : Type} (F : A → R × R × R) (hF : ∀ i, P.nn (F i).1) : ∀ l : List A, ∀ q, q ∈ l.map F → P.nn q.1
  | [], _, h => nomatch h
  | i :: l, q, h => by
    cases h with
    | head => exact hF i
    | tail _ h' => exact nn_map F hF l q h'

def trip {A : Type} (u : A → R) (k : Nat) (l : List A) : List (R × R × R) := l.map (fun i => (pw K (u i) k, K.one, u i))

theorem trip_A {A : Type} (u : A → R) (k : Nat) : ∀ l : List A, wA K (trip K u k l) = mom K u k l
  | [] => rfl
  | i :: l => by
    show K.add (K.mul (pw K (u i) k) (K.mul K.one K.one)) (wA K (trip K u k l)) = K.add (pw K (u i) k) (mom K u k l)
    rw [trip_A u k l]; lib_ring
theorem trip_B {A : Type} (u : A → R) (k : Nat) : ∀ l : List A, wB K (trip K u k l) = mom K u (k + 2) l
  | [] => rfl
  | i :: l => by
    show K.add (K.mul (pw K (u i) k) (K.mul (u i) (u i))) (wB K (trip K u k l)) = K.add (K.mul (K.mul (pw K (u i) k) (u i)) (u i)) (mom K u (k + 2) l)
    rw [trip_B u k l]; lib_ring
theorem trip_C {A : Type} (u : A → R) (k : Nat) : ∀ l : List A, wC K (trip K u k l) = mom K u (k + 1) l
  | [] => rfl
  | i :: l => by
    show K.add (K.mul (pw K (u i) k) (K.mul K.one (u i))) (wC K (trip K u k l)) = K.add (K.mul (pw K (u i) k) (u i)) (mom K u (k + 1) l)
    rw [trip_C u k l]; lib_ring

/-- **Log-convexity of moments.** u ≥ 0 ⇒ S_{k+1}² ≤ S_k S_{k+2}. -/
theorem log_convex {A : Type} (u : A → R) (hu : ∀ i, P.nn (u i)) (k : Nat) (l : List A) :
    le K P (K.mul (mom K u (k + 1) l) (mom K u (k + 1) l)) (K.mul (mom K u k l) (mom K u (k + 2) l)) := by
  have h := weighted_cs K P (trip K u k l) (nn_map K P _ (fun i => nn_pw K P (u i) (hu i) k) l)
  rw [trip_A, trip_B, trip_C] at h; exact h

/-- **S_{a+b}² ≤ S_{2a} S_{2b}** (all u). -/
theorem mom_cs {A : Type} (u : A → R) (a b : Nat) (l : List A) :
    le K P (K.mul (mom K u (a + b) l) (mom K u (a + b) l)) (K.mul (mom K u (2 * a) l) (mom K u (2 * b) l)) := by
  have h := weighted_cs K P (l.map (fun i => (K.one, pw K (u i) a, pw K (u i) b))) (nn_map K P _ (fun _ => nn_one K P) l)
  have eA : ∀ l : List A, wA K (l.map (fun i => (K.one, pw K (u i) a, pw K (u i) b))) = mom K u (2 * a) l := by
    intro l; induction l with
    | nil => rfl
    | cons i l ih =>
      show K.add (K.mul K.one (K.mul (pw K (u i) a) (pw K (u i) a))) _ = K.add (pw K (u i) (2 * a)) (mom K u (2 * a) l)
      rw [ih, show 2 * a = a + a by lib_nat, LibEuler.pw_add_r]
      generalize mom K u (a + a) l = M; lib_ring
  have eB : ∀ l : List A, wB K (l.map (fun i => (K.one, pw K (u i) a, pw K (u i) b))) = mom K u (2 * b) l := by
    intro l; induction l with
    | nil => rfl
    | cons i l ih =>
      show K.add (K.mul K.one (K.mul (pw K (u i) b) (pw K (u i) b))) _ = K.add (pw K (u i) (2 * b)) (mom K u (2 * b) l)
      rw [ih, show 2 * b = b + b by lib_nat, LibEuler.pw_add_r]
      generalize mom K u (b + b) l = M; lib_ring
  have eC : ∀ l : List A, wC K (l.map (fun i => (K.one, pw K (u i) a, pw K (u i) b))) = mom K u (a + b) l := by
    intro l; induction l with
    | nil => rfl
    | cons i l ih =>
      show K.add (K.mul K.one (K.mul (pw K (u i) a) (pw K (u i) b))) _ = K.add (pw K (u i) (a + b)) (mom K u (a + b) l)
      rw [ih, LibEuler.pw_add_r]; lib_ring
  rw [eA, eB, eC] at h; exact h

end LibWCS

#print axioms LibWCS.wlagrange
#print axioms LibWCS.weighted_cs
#print axioms LibWCS.log_convex
#print axioms LibWCS.mom_cs

lib/WeightedDl.lean

lean_all/lib/WeightedDl.lean

/-
  lib/WeightedDl.lean — T46 shared library (G1374g piece): 2×2 minors of a weighted sequence X_k = w_k A_k.

    wdl_split   X_(p+1) X_q − X_p X_(q+1) = w_p w_(q+1) (A_(p+1)A_q − A_pA_(q+1)) + (w_(p+1) w_q − w_p w_(q+1)) A_(p+1) A_q
    lin_wdl     for w_k = k+1 and q = p+d: X_(p+1)X_(p+d) − X_pX_(p+d+1) = (p+1)(p+d+2) Δ + d · A_(p+1)A_(p+d)
  (the r = 2 case of G61: minors of X split into a log-concavity minor and a nonnegative product). Lean 4 core only, no axioms.
-/
import lib.Binom
import lib.RingTac

namespace LibWeightedDl
open DetChain LibRing

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

theorem wdl_split (w A : Nat → R) (p q : Nat) :
    sub K (K.mul (K.mul (w (p + 1)) (A (p + 1))) (K.mul (w q) (A q))) (K.mul (K.mul (w p) (A p)) (K.mul (w (q + 1)) (A (q + 1))))
      = K.add (K.mul (K.mul (w p) (w (q + 1))) (sub K (K.mul (A (p + 1)) (A q)) (K.mul (A p) (A (q + 1)))))
          (K.mul (sub K (K.mul (w (p + 1)) (w q)) (K.mul (w p) (w (q + 1)))) (K.mul (A (p + 1)) (A q))) := by
  unfold sub; lib_ring

theorem lin_wdl (A : Nat → R) (p d : Nat) :
    sub K (K.mul (K.mul (castN K (p + 2)) (A (p + 1))) (K.mul (castN K (p + d + 1)) (A (p + d))))
        (K.mul (K.mul (castN K (p + 1)) (A p)) (K.mul (castN K (p + d + 2)) (A (p + d + 1))))
      = K.add (K.mul (K.mul (castN K (p + 1)) (castN K (p + d + 2))) (sub K (K.mul (A (p + 1)) (A (p + d))) (K.mul (A p) (A (p + d + 1)))))
          (K.mul (castN K d) (K.mul (A (p + 1)) (A (p + d)))) := by
  simp only [LibBinom.castN_add K]
  unfold sub; lib_ring

end LibWeightedDl

#print axioms LibWeightedDl.wdl_split
#print axioms LibWeightedDl.lin_wdl

lib/WeightedLC.lean

lean_all/lib/WeightedLC.lean

/-
  lib/WeightedLC.lean — T46 shared library (G1364): log-concavity relative to a log-concave weight implies log-concavity.

  Ordered commutative ring (YMChain.ORing). c_n > 0 with c_n c_(n+2) ≤ c_(n+1)², x arbitrary:
    weighted_lc     c_(n+1)² x_n x_(n+2) ≤ c_n c_(n+2) x_(n+1)²  ⇒  x_n x_(n+2) ≤ x_(n+1)²
    nb2_lc          (n+1)(n+3) Y_(n+1)² ≥ (n+2)² Y_n Y_(n+2)  ⇒  Y_n Y_(n+2) ≤ Y_(n+1)²     (Y_n/(n+1) log-concave ⇒ Y log-concave)
  Lean 4 core only, proofs by hand, no axioms.
-/
import lib.LCHeat
import lib.PascalLC
import lib.LiggettULC1

namespace LibWeightedLC
open DetChain LibRing YMChain LibPascalLC

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

theorem weighted_lc (c0 c1 c2 x0 x1 x2 : R) (hc1 : Pos K O c1) (hc : O.le (K.mul c0 c2) (K.mul c1 c1))
    (h : O.le (K.mul (K.mul c1 c1) (K.mul x0 x2)) (K.mul (K.mul c0 c2) (K.mul x1 x1))) :
    O.le (K.mul x0 x2) (K.mul x1 x1) := by
  have sq := LibLiggettULC1.sq_nonneg' K O x1
  have m := O.mul_le_mul_right _ _ _ hc sq
  have t := O.le_trans _ _ _ h m
  have pc : O.le K.zero (K.mul c1 c1) := O.mul_nonneg _ _ hc1.1 hc1.1
  have nz : K.mul c1 c1 ≠ K.zero := fun e => hc1.2 (O.pos_mul_eq_zero _ _ hc1.1 hc1.2 e)
  apply O.pos_cancel _ _ _ pc nz
  exact congr_le K O (by lib_ring) (by lib_ring) t

/-- Y_n/(n+1) log-concave (division-free) ⇒ Y log-concave. -/
theorem nb2_lc (h10 : K.one ≠ K.zero) (Y : Nat → R) (n : Nat)
    (h : O.le (K.mul (K.mul (castN K (n + 2)) (castN K (n + 2))) (K.mul (Y n) (Y (n + 2))))
      (K.mul (K.mul (castN K (n + 1)) (castN K (n + 3))) (K.mul (Y (n + 1)) (Y (n + 1))))) :
    O.le (K.mul (Y n) (Y (n + 2))) (K.mul (Y (n + 1)) (Y (n + 1))) := by
  apply weighted_lc K O (castN K (n + 1)) (castN K (n + 2)) (castN K (n + 3)) _ _ _ (LibLCHeat.castN_succ_pos K O h10 (n + 1))
  · have l := LibLCHeat.lc_succ K O n
    exact l
  · exact h

end LibWeightedLC

#print axioms LibWeightedLC.weighted_lc
#print axioms LibWeightedLC.nb2_lc

lib/Wilson.lean

lean_all/lib/Wilson.lean

/-
  lib/Wilson.lean — T46 shared library, cycle 45: Wilson's theorem.

  p = 2m + 3 prime, 𝔽_p = LibMod.znR (k m):
    lead_app_one        lead (l ++ [1]) = 1
    prodL, prodX_zero   Π_{a∈l} (0 − a) = (−1)^|l| · Π_{a∈l} a
    xm1_factor          x^(p−1) − 1 = Π_{a≠0} (x − a)       (every nonzero element is a root: Fermat)
    prod_nzs            Π_{a≠0} a = −1
    wilson              (p − 1)! ≡ −1 (mod p), i.e. md p ((p−1)!) = p − 1
  (Built on PolyFactor.factor_all, NonRes.nzs, Fermat.fermat_unit.)

  Lean 4 core only, proofs by hand, no axioms.
-/
import lib.PolyFactor
import lib.NonRes

namespace LibWilson
open DetChain LibRing LibMod LibZp LibPoly LibPolyFactor LibEulerCrit LibNonRes

/-- Factorial (by hand; axiom-free definition). -/
def _root_.Nat.factorial' : Nat → Nat
  | 0 => 1
  | n + 1 => (n + 1) * Nat.factorial' n

section Gen
variable {R : Type} (K : CRing R)

theorem lead_app_one : ∀ l : List R, lead K (l ++ [K.one]) = K.one
  | [] => rfl
  | [_] => rfl
  | _ :: b :: l => by
    show lead K ((b :: l) ++ [K.one]) = K.one
    exact lead_app_one (b :: l)

def prodL : List R → R
  | [] => K.one
  | a :: l => K.mul a (prodL l)

theorem prodX_zero : ∀ l : List R, prodX K K.zero l = K.mul (pw K (K.neg K.one) l.length) (prodL K l)
  | [] => by show K.one = K.mul K.one K.one; lib_ring
  | a :: l => by
    show K.mul (sub K K.zero a) (prodX K K.zero l) = K.mul (K.mul (pw K (K.neg K.one) l.length) (K.neg K.one)) (K.mul a (prodL K l))
    rw [prodX_zero l]; lib_ring

theorem pw_neg_one_even : ∀ j, pw K (K.neg K.one) (2 * j) = K.one
  | 0 => rfl
  | j + 1 => by
    rw [show 2 * (j + 1) = 2 * j + 1 + 1 by lib_nat]
    show K.mul (K.mul (pw K (K.neg K.one) (2 * j)) (K.neg K.one)) (K.neg K.one) = K.one
    rw [pw_neg_one_even j]; lib_ring

end Gen

variable (m : Nat) (hp : Prime1 (k m))
include hp

/-- **Π_{a≠0} a = −1.** -/
theorem prod_nzs : prodL (znR (k m)) (nzs m (k m)) = (znR (k m)).neg (znR (k m)).one := by
  let K := znR (k m)
  -- P = −1 + x^(p−1)
  let P : List (ZN (k m)) := addP K [K.neg K.one] (monoP K (k m))
  have evP : ∀ x, ev K P x = sub K (pw K x (k m)) K.one := by
    intro x
    show ev K (addP K [K.neg K.one] (monoP K (k m))) x = _
    rw [ev_addP, ev_monoP]
    show K.add (K.add (K.neg K.one) (K.mul x K.zero)) (pw K x (k m)) = _
    lib_ring
  have lenP : P.length = (nzs m (k m)).length + 1 := by
    rw [len_nzs]
    show (K.add (K.neg K.one) K.zero :: (zeros K (2 * m + 1) ++ [K.one])).length = 2 * m + 1 + 1 + 1
    show (zeros K (2 * m + 1) ++ [K.one]).length + 1 = 2 * m + 1 + 1 + 1
    rw [show zeros K (2 * m + 1) ++ [K.one] = monoP K (2 * m + 1) from rfl, len_monoP]
  have leadP : lead K P = K.one := by
    show lead K (K.add (K.neg K.one) K.zero :: (zeros K (2 * m + 1) ++ [K.one])) = K.one
    exact lead_app_one K (K.add (K.neg K.one) K.zero :: zeros K (2 * m + 1))
  have roots : ∀ r, r ∈ nzs m (k m) → ev K P r = K.zero := by
    intro r hr
    rw [evP, LibFermat.fermat_unit (k m) hp r (nz_of_mem m Nat.le.refl hr)]; lib_ring
  have fac := factor_all K (zp_noZD (k m) hp) (nzs m (k m)) P (dist_nzs m (k m) Nat.le.refl) lenP leadP roots K.zero
  have hneg : pw K (K.neg K.one) (k m) = K.one := pw_neg_one_even K (m + 1)
  have z : pw K K.zero (k m) = K.zero := by
    show K.mul (pw K K.zero (2 * m + 1)) K.zero = K.zero; lib_ring
  rw [evP, prodX_zero, len_nzs, hneg, z] at fac
  -- −1 = 0^(p−1) − 1 = Π a
  have : K.neg K.one = K.mul K.one (prodL K (nzs m (k m))) := by rw [← fac]; lib_ring
  show prodL K (nzs m (k m)) = K.neg K.one
  rw [this]; lib_ring

omit hp in
/-- Π_{j=1}^{n} mk j = mk (n!). -/
theorem prod_fact : ∀ n, prodL (znR (k m)) (nzs m n) = mk (k m) (Nat.factorial' n)
  | 0 => rfl
  | n + 1 => by
    show (znR (k m)).mul (mk (k m) (n + 1)) (prodL (znR (k m)) (nzs m n)) = mk (k m) ((n + 1) * Nat.factorial' n)
    rw [prod_fact n]
    apply ext (k m)
    show md (k m + 1) (md (k m + 1) (n + 1) * md (k m + 1) (Nat.factorial' n)) = md (k m + 1) ((n + 1) * Nat.factorial' n)
    rw [← md_mul (pos (k m))]

/-- **Wilson.** (p − 1)! ≡ −1 (mod p): md p ((p − 1)!) = p − 1. -/
theorem wilson : md (k m + 1) (Nat.factorial' (k m)) = k m := by
  have h := (prod_fact m (k m)).symm.trans (prod_nzs m hp)
  have hv := congrArg Subtype.val h
  have e : (mk (k m) (Nat.factorial' (k m))).val = md (k m + 1) (Nat.factorial' (k m)) := rfl
  rw [e] at hv
  rw [hv]
  show md (k m + 1) (k m * md (k m + 1) 1) = k m
  rw [md_mul_r (pos (k m)), show k m * 1 = k m by lib_nat, md_of_lt (pos (k m)) (Nat.lt_succ_self _)]

end LibWilson

#print axioms LibWilson.prodX_zero
#print axioms LibWilson.prod_nzs
#print axioms LibWilson.prod_fact
#print axioms LibWilson.wilson

lib/WilsonConv.lean

lean_all/lib/WilsonConv.lean

/-
  lib/WilsonConv.lean — T46 shared library, cycle 134: converse of Wilson — if (n−1)! ≡ −1 (mod n) then n is prime.

    fact n, factMod n m        n!, n! mod m (checked remainder LibFPM.mm at each step)
    factMod_spec               m > 0 ⇒ factMod n m = md m (n!)
    dvd_fact                   1 ≤ d ≤ n ⇒ d ∣ n!
    wilson_conv                2 ≤ n, md n ((n−1)! + 1) = 0 ⇒ the divisors are 1, n
    test: 101 by decide
  Lean 4 core only, proofs by hand, no axioms.
-/
import lib.FastPowMod
import lib.PrimePow
import lib.IntRing

namespace LibWilsonC
open LibMod LibFPM

def fact : Nat → Nat
  | 0 => 1
  | n + 1 => fact n * (n + 1)

def factMod (m : Nat) : Nat → Nat
  | 0 => mm m 1
  | n + 1 => mm m (factMod m n * (n + 1))

theorem factMod_spec {m : Nat} (hm : 0 < m) : ∀ n, factMod m n = md m (fact n)
  | 0 => mm_eq hm 1
  | n + 1 => by
    show mm m (factMod m n * (n + 1)) = md m (fact n * (n + 1))
    rw [mm_eq hm, factMod_spec hm n, md_mul_l hm]

theorem dvd_fact {d : Nat} (hd : 1 ≤ d) : ∀ n, d ≤ n → d ∣ fact n
  | 0, h => absurd (Nat.le_trans hd h) (by decide)
  | n + 1, h => by
    cases Nat.lt_or_eq_of_le h with
    | inl hl => exact LibGcd.dvd_mul_right' (dvd_fact hd n (Nat.le_of_lt_succ hl)) _
    | inr he => rw [he]; exact ⟨fact n, by show fact n * (n + 1) = (n + 1) * fact n; lib_nat⟩

/-- **Converse of Wilson.** -/
theorem wilson_conv {n : Nat} (h0 : md (n + 2) (fact (n + 1) + 1) = 0) : ∀ d, d ∣ n + 2 → d = 1 ∨ d = n + 2 := by
  have hpos : 0 < n + 2 := Nat.succ_le_succ (Nat.zero_le _)
  have hdvd := dvd_of_md_zero h0
  intro d hd
  have dpos := LibPrimePow.pos_of_dvd hpos hd
  have dle := LibOrd.le_of_dvd' hpos hd
  cases Nat.lt_or_eq_of_le dle with
  | inr he => exact Or.inr he
  | inl hlt =>
    left
    have h1 : d ∣ fact (n + 1) := dvd_fact dpos (n + 1) (Nat.le_of_lt_succ hlt)
    have h2 : d ∣ fact (n + 1) + 1 := LibGcd.dvd_trans' hd hdvd
    obtain ⟨a, ha⟩ := h1
    obtain ⟨b, hb⟩ := h2
    apply LibGcd.dvd_one'
    apply LibGcd.idv_nat
    refine ⟨Int.ofNat b - Int.ofNat a, ?_⟩
    have e : Int.ofNat (fact (n + 1)) + 1 = Int.ofNat d * Int.ofNat b := by
      show Int.ofNat (fact (n + 1) + 1) = Int.ofNat (d * b); rw [hb]
    have e2 : Int.ofNat (fact (n + 1)) = Int.ofNat d * Int.ofNat a := by
      show Int.ofNat (fact (n + 1)) = Int.ofNat (d * a); rw [ha]
    calc Int.ofNat 1 = (Int.ofNat (fact (n + 1)) + 1) - Int.ofNat (fact (n + 1)) := by lib_int
      _ = Int.ofNat d * Int.ofNat b - Int.ofNat d * Int.ofNat a := by rw [e, ← e2]
      _ = Int.ofNat d * (Int.ofNat b - Int.ofNat a) := by lib_int

set_option maxRecDepth 20000 in
example : ∀ d, d ∣ 101 → d = 1 ∨ d = 101 :=
  wilson_conv (n := 99) (by rw [← md_add_l (by decide), ← factMod_spec (by decide)]; decide)

end LibWilsonC

#print axioms LibWilsonC.factMod_spec
#print axioms LibWilsonC.wilson_conv

lib/WilsonFF.lean

lean_all/lib/WilsonFF.lean

/-
  lib/WilsonFF.lean — T46 shared library, cycle 247: Wilson's theorem for a general finite field.

  Finite field data (LibFFSum: integral domain, els duplicate-free and exhaustive, |els| = n + 2, nonzero x satisfy x^(n+1) = 1):
    xq1 n            coefficient list of X^(n+1) − 1
    ev_xq1, lead_xq1
    factor_units     for all x: x^(n+1) − 1 = Π_{a ≠ 0} (x − a)
    wilson_ff        Π_{a ≠ 0} a = −(−1)^(n+1)
    wilson_odd       q odd (n + 1 = 2m): Π_{a ≠ 0} a = −1
  (lib/Wilson covers only ℤ/p; here all finite field data such as 𝔽_{p²}.)
  Lean 4 core only, no axioms.
-/
import lib.FFSum
import lib.Wilson
import lib.Euler

namespace LibWilsonFF
open DetChain LibRing LibSum LibSemi LibFFSum LibDist LibPoly LibPolyFactor LibLegSum

section
variable {R : Type} (K : CRing R)

def zo : Nat → List R
  | 0 => [K.one]
  | n + 1 => K.zero :: zo n

def xq1 (n : Nat) : List R := K.neg K.one :: zo K n

theorem ev_zo (x : R) : ∀ n, ev K (zo K n) x = pw K x n
  | 0 => by show K.add K.one (K.mul x K.zero) = K.one; lib_ring
  | n + 1 => by
    show K.add K.zero (K.mul x (ev K (zo K n) x)) = K.mul (pw K x n) x
    rw [ev_zo x n]; lib_ring

theorem ev_xq1 (x : R) (n : Nat) : ev K (xq1 K n) x = K.add (K.neg K.one) (pw K x (n + 1)) := by
  show K.add (K.neg K.one) (K.mul x (ev K (zo K n) x)) = K.add (K.neg K.one) (K.mul (pw K x n) x)
  rw [ev_zo]; lib_ring

theorem zo_cons : ∀ n, ∃ d p, zo K n = d :: p
  | 0 => ⟨K.one, [], rfl⟩
  | n + 1 => ⟨K.zero, zo K n, rfl⟩

theorem lead_skip (c : R) (n : Nat) : lead K (c :: zo K n) = lead K (zo K n) := by
  obtain ⟨d, p, e⟩ := zo_cons K n
  rw [e]; rfl

theorem lead_zo : ∀ n, lead K (zo K n) = K.one
  | 0 => rfl
  | n + 1 => by show lead K (K.zero :: zo K n) = K.one; rw [lead_skip]; exact lead_zo n

theorem lead_xq1 (n : Nat) : lead K (xq1 K n) = K.one := by
  show lead K (K.neg K.one :: zo K n) = K.one
  rw [lead_skip]; exact lead_zo K n

theorem len_zo : ∀ n, (zo K n).length = n + 1
  | 0 => rfl
  | n + 1 => by show (zo K n).length + 1 = n + 1 + 1; rw [len_zo n]
end

variable {R : Type} [DecidableEq R] (K : CRing R)
variable (hK : NoZD K) (els : List R) (hd : Dist els) (hall : ∀ x, x ∈ els) (n : Nat)
  (hlen : els.length = n + 2) (hfer : ∀ x, x ≠ K.zero → pw K x (n + 1) = K.one)

include hK hd hall hlen hfer in
theorem factor_units (x : R) :
    K.add (K.neg K.one) (pw K x (n + 1)) = prodX K x (units K els) := by
  rw [← ev_xq1]
  refine factor_all K hK (units K els) (xq1 K n) (dist_filt _ hd) ?_ (lead_xq1 K n) ?_ x
  · show (zo K n).length + 1 = (units K els).length + 1
    rw [len_zo, len_units K els hd hall n hlen]
  · intro r hr
    have hr0 : r ≠ K.zero := by
      have := (mem_filt hr).2
      intro e; rw [e, decide_eq_true rfl] at this; exact Bool.noConfusion this
    rw [ev_xq1, hfer r hr0]; lib_ring

include hK hd hall hlen hfer in
/-- **Π_{a ≠ 0} a = −(−1)^(n+1).** -/
theorem wilson_ff : LibWilson.prodL K (units K els) = K.neg (pw K (K.neg K.one) (n + 1)) := by
  have e := factor_units K hK els hd hall n hlen hfer K.zero
  rw [LibWilson.prodX_zero, len_units K els hd hall n hlen,
    show pw K K.zero (n + 1) = K.zero from (show K.mul (pw K K.zero n) K.zero = K.zero by lib_ring)] at e
  have s2 : K.mul (pw K (K.neg K.one) (n + 1)) (pw K (K.neg K.one) (n + 1)) = K.one := by
    have h := LibWilson.pw_neg_one_even K (n + 1)
    rw [show 2 * (n + 1) = (n + 1) + (n + 1) by lib_nat, LibEuler.pw_add_r] at h; exact h
  generalize pw K (K.neg K.one) (n + 1) = s at e s2 ⊢
  generalize LibWilson.prodL K (units K els) = P at e ⊢
  have : P = K.mul (K.mul s s) P := by rw [s2]; lib_ring
  rw [this, show K.mul (K.mul s s) P = K.mul s (K.mul s P) by lib_ring, ← e]; lib_ring

include hK hd hall hlen hfer in
/-- **q odd: Π_{a ≠ 0} a = −1.** -/
theorem wilson_odd (m : Nat) (hm : n + 1 = 2 * m) : LibWilson.prodL K (units K els) = K.neg K.one := by
  rw [wilson_ff K hK els hd hall n hlen hfer, hm, LibWilson.pw_neg_one_even]

end LibWilsonFF

#print axioms LibWilsonFF.ev_xq1
#print axioms LibWilsonFF.lead_xq1
#print axioms LibWilsonFF.factor_units
#print axioms LibWilsonFF.wilson_ff
#print axioms LibWilsonFF.wilson_odd

lib/Z4Z2Tors.lean

lean_all/lib/Z4Z2Tors.lean

/-
  lib/Z4Z2Tors.lean — T46 shared library, cycle 691 (G541–G545): Z/4 × Z/2 torsion of y² = x(x + a²)(x + b²) (closed form).

  E = LibChordA.onE K (a² + b²) (a²b²) 0. Seven affine torsion points:
    T₀ = (0,0), T_a = (−a², 0), T_b = (−b², 0), P = (ab, ab(a+b)), −P = (ab, −ab(a+b)), P + T_a = (−ab, ab(b−a)), P + T_b = (−ab, ab(a−b))
    tors_on     all seven points lie on E
    dbl_P       tangent slope a + b: λ·2y_P = 3x_P² + 2Ax_P + B, and the tangent-formula point = (0, 0)   (2P = T₀)
    P_plus_Ta   secant slope b: passes through P, T_a, and the third-point formula = (−ab, ab(b − a))
    P_plus_Tb   slope a: = (−ab, ab(a − b))
    P_plus_T0   slope a + b: = (ab, −ab(a + b)) = −P   (3P = −P)
  All division-free ring identities (lib_ring). For the Z/4 × Z/2 orbits of agent1 lang/Z4Z2.
  Lean 4 core only, no axioms.
-/
import lib.TwoTorsAB

namespace LibZ4Z2Tors
open DetChain LibRing LibChordA

variable {R : Type} (K : CRing R) (a b : R)

abbrev A : R := K.add (K.mul a a) (K.mul b b)
abbrev B : R := K.mul (K.mul a a) (K.mul b b)
abbrev E (x y : R) : Prop := onE K (A K a b) (B K a b) K.zero x y

/-- **The seven torsion points lie on the curve.** -/
theorem tors_on :
    E K a b K.zero K.zero ∧ E K a b (K.neg (K.mul a a)) K.zero ∧ E K a b (K.neg (K.mul b b)) K.zero ∧
    E K a b (K.mul a b) (K.mul (K.mul a b) (K.add a b)) ∧ E K a b (K.mul a b) (K.neg (K.mul (K.mul a b) (K.add a b))) ∧
    E K a b (K.neg (K.mul a b)) (K.mul (K.mul a b) (sub K b a)) ∧ E K a b (K.neg (K.mul a b)) (K.mul (K.mul a b) (sub K a b)) := by
  unfold E onE A B
  exact ⟨by lib_ring, by lib_ring, by lib_ring, by lib_ring, by lib_ring, by lib_ring, by lib_ring⟩

/-- **2P = T₀** (tangent slope a + b). -/
theorem dbl_P :
    K.mul (K.add a b) (K.mul (castZ K 2) (K.mul (K.mul a b) (K.add a b)))
      = K.add (K.add (K.mul (castZ K 3) (K.mul (K.mul a b) (K.mul a b))) (K.mul (castZ K 2) (K.mul (A K a b) (K.mul a b)))) (B K a b) ∧
    sub K (sub K (sub K (K.mul (K.add a b) (K.add a b)) (A K a b)) (K.mul a b)) (K.mul a b) = K.zero ∧
    sub K (K.mul (K.add a b) (sub K (K.mul a b) (sub K (sub K (sub K (K.mul (K.add a b) (K.add a b)) (A K a b)) (K.mul a b)) (K.mul a b))))
      (K.mul (K.mul a b) (K.add a b)) = K.zero := by
  unfold A B; exact ⟨by lib_ring, by lib_ring, by lib_ring⟩

/-- Secant formula (slope l, point (x₁, y₁), second x₂). -/
abbrev x3 (l x1 x2 : R) : R := sub K (sub K (sub K (K.mul l l) (A K a b)) x1) x2
abbrev y3 (l x1 y1 x2 : R) : R := sub K (K.mul l (sub K x1 (x3 K a b l x1 x2))) y1

/-- **P + T_a = (−ab, ab(b − a)).** -/
theorem P_plus_Ta :
    K.mul b (sub K (K.neg (K.mul a a)) (K.mul a b)) = sub K K.zero (K.mul (K.mul a b) (K.add a b)) ∧
    x3 K a b b (K.mul a b) (K.neg (K.mul a a)) = K.neg (K.mul a b) ∧
    y3 K a b b (K.mul a b) (K.mul (K.mul a b) (K.add a b)) (K.neg (K.mul a a)) = K.mul (K.mul a b) (sub K b a) := by
  unfold y3 x3 A; exact ⟨by lib_ring, by lib_ring, by lib_ring⟩

/-- **P + T_b = (−ab, ab(a − b)).** -/
theorem P_plus_Tb :
    K.mul a (sub K (K.neg (K.mul b b)) (K.mul a b)) = sub K K.zero (K.mul (K.mul a b) (K.add a b)) ∧
    x3 K a b a (K.mul a b) (K.neg (K.mul b b)) = K.neg (K.mul a b) ∧
    y3 K a b a (K.mul a b) (K.mul (K.mul a b) (K.add a b)) (K.neg (K.mul b b)) = K.mul (K.mul a b) (sub K a b) := by
  unfold y3 x3 A; exact ⟨by lib_ring, by lib_ring, by lib_ring⟩

/-- **P + T₀ = −P.** -/
theorem P_plus_T0 :
    K.mul (K.add a b) (sub K K.zero (K.mul a b)) = sub K K.zero (K.mul (K.mul a b) (K.add a b)) ∧
    x3 K a b (K.add a b) (K.mul a b) K.zero = K.mul a b ∧
    y3 K a b (K.add a b) (K.mul a b) (K.mul (K.mul a b) (K.add a b)) K.zero = K.neg (K.mul (K.mul a b) (K.add a b)) := by
  unfold y3 x3 A; exact ⟨by lib_ring, by lib_ring, by lib_ring⟩

end LibZ4Z2Tors

#print axioms LibZ4Z2Tors.tors_on
#print axioms LibZ4Z2Tors.dbl_P
#print axioms LibZ4Z2Tors.P_plus_Ta
#print axioms LibZ4Z2Tors.P_plus_T0

lib/ZMod2.lean

lean_all/lib/ZMod2.lean

/-
  lib/ZMod2.lean — T46 shared library (G1093a): integer-window sums split by parity.

    sum_two    Σ_{i<2N} g i = Σ_{t<N} (g(2t) + g(2t+1))
    zs_mod2    Supp G (2n) ⇒ zs G (2n) = zs (t ↦ G(2t)) n + zs (t ↦ G(2t+1)) n
  Lean 4 core only, proofs by hand, no axioms.
-/
import lib.ZSplit

namespace LibZMod2
open DetChain LibRing LibSum LibSemi LibInt LibMacMahon LibZSum LibZSplit LibQBinom

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

theorem sum_two (g : Nat → R) : ∀ N, sum (toSemi K) g (rng (2 * N))
    = sum (toSemi K) (fun t => K.add (g (2 * t)) (g (2 * t + 1))) (rng N)
  | 0 => rfl
  | N + 1 => by
    rw [show 2 * (N + 1) = 2 * N + 1 + 1 by lib_nat, sum_rng_succ (toSemi K) g (2 * N + 1), sum_rng_succ (toSemi K) g (2 * N),
      sum_two g N, sum_rng_succ (toSemi K) (fun t => K.add (g (2 * t)) (g (2 * t + 1))) N]
    show K.add (K.add (sum (toSemi K) (fun t => K.add (g (2 * t)) (g (2 * t + 1))) (rng N)) (g (2 * N))) (g (2 * N + 1))
      = K.add (sum (toSemi K) (fun t => K.add (g (2 * t)) (g (2 * t + 1))) (rng N)) (K.add (g (2 * N)) (g (2 * N + 1)))
    lib_ring

theorem zs_mod2 (G : Int → R) (n : Nat) (hS : Supp K G (2 * n)) :
    zs K G (2 * n) = K.add (zs K (fun t => G (2 * t)) n) (zs K (fun t => G (2 * t + 1)) n) := by
  have hw := zs_window K G (2 * n) hS 0 1
  rw [← hw]
  rw [show 0 + (2 * n + 2 * n + 1) + 1 = 2 * (n + n + 1) by lib_nat, sum_two K _ (n + n + 1)]
  unfold zs
  refine Eq.trans (sum_ext (toSemi K) _ _ (fun t => ?_) _) (sum_add (toSemi K) _ _ _)
  have e : ∀ s : Nat, Int.ofNat (2 * t + s) - Int.ofNat (2 * n + 0) = 2 * (Int.ofNat t - Int.ofNat n) + Int.ofNat s := fun s => by
    show 2 * Int.ofNat t + Int.ofNat s - (2 * Int.ofNat n + 0) = _; lib_int
  show K.add (G (Int.ofNat (2 * t + 0) - Int.ofNat (2 * n + 0))) (G (Int.ofNat (2 * t + 1) - Int.ofNat (2 * n + 0))) = _
  rw [e 0, e 1, show 2 * (Int.ofNat t - Int.ofNat n) + Int.ofNat 0 = 2 * (Int.ofNat t - Int.ofNat n) by lib_int]
  rfl

end LibZMod2

#print axioms LibZMod2.zs_mod2

lib/ZPerm.lean

lean_all/lib/ZPerm.lean

/-
  lib/ZPerm.lean — T46 shared library, cycle 626 (G395b): shifted identity matrix as a permutation matrix — determinant with three rows replaced.

  Any commutative ring, Z_i = minor I (i+1) (n×n, i < n), W = Z_i with row i replaced by e₀, π = (i ↦ 0, ρ ↦ cr i ρ):
    zW, zπ_lt, zπ_inj, zπ_surj, det_zW (= (−1)^i)
    three_row   det(Z_i with rows i, r, s replaced by u, v, w) = (−1)^i · d3(u, v, w; 0, c_r, c_s)
  Lean 4 core only, no axioms.
-/
import lib.PermRows

namespace LibZPerm
open DetChain LibRing LibSum LibSemi LibMat LibLegSum LibDet LibDetDep LibCharLin LibTwoRow LibTwoRowDet LibPermRows

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

def zp (i ρ : Nat) : Nat := cond (Nat.beq ρ i) 0 (cr i ρ)

theorem zp_i (i : Nat) : zp i i = 0 := by unfold zp; rw [LibDqRows.beq_self]; rfl
theorem zp_o (i ρ : Nat) (h : ρ ≠ i) : zp i ρ = cr i ρ := by unfold zp; rw [LibDetAlt.beq_ne h]; rfl

def zW (i : Nat) : Nat → Nat → R := setRow (Z K i) i (idM K 0)

theorem hW (i ρ c : Nat) : zW K i ρ c = idM K (zp i ρ) c := by
  cases Nat.decEq ρ i with
  | isTrue e => subst e; unfold zW; rw [sr_i, zp_i]
  | isFalse e => unfold zW; rw [sr_o _ _ _ _ _ e, zp_o i ρ e]; exact rowZ K i ρ c e

theorem cr_inj (i a b : Nat) (ha : a ≠ i) (hb : b ≠ i) (h : cr i a = cr i b) : a = b := by
  unfold cr at h
  cases h1 : Nat.blt a i <;> cases h2 : Nat.blt b i <;> rw [h1, h2] at h
  · exact h
  · exfalso
    have ea : i < a := Nat.lt_of_le_of_ne (bf h1) (fun e => ha e.symm)
    have eb := bt h2
    have h' : a = b + 1 := h
    rw [h'] at ea; exact Nat.lt_irrefl _ (Nat.lt_of_lt_of_le ea eb)
  · exfalso
    have eb : i < b := Nat.lt_of_le_of_ne (bf h2) (fun e => hb e.symm)
    have ea := bt h1
    have h' : a + 1 = b := h
    rw [← h'] at eb; exact Nat.lt_irrefl _ (Nat.lt_of_lt_of_le eb ea)
  · exact Nat.succ.inj h

theorem zp_lt (n i ρ : Nat) (hi : i < n) (hρ : ρ < n) : zp i ρ < n := by
  cases Nat.decEq ρ i with
  | isTrue e => rw [e, zp_i]; exact Nat.lt_of_le_of_lt (Nat.zero_le i) hi
  | isFalse e => rw [zp_o i ρ e]; exact cr_lt n i ρ hi hρ

theorem zp_inj (i a b : Nat) (h : zp i a = zp i b) : a = b := by
  cases Nat.decEq a i with
  | isTrue ea =>
    cases Nat.decEq b i with
    | isTrue eb => rw [ea, eb]
    | isFalse eb => rw [ea, zp_i, zp_o i b eb] at h; exact absurd h.symm (cr_ne0 i b eb)
  | isFalse ea =>
    cases Nat.decEq b i with
    | isTrue eb => rw [eb, zp_i, zp_o i a ea] at h; exact absurd h (cr_ne0 i a ea)
    | isFalse eb => rw [zp_o i a ea, zp_o i b eb] at h; exact cr_inj i a b ea eb h

theorem zp_surj (n i : Nat) (hi : i < n) (c : Nat) (hc : c < n) : ∃ ρ, ρ < n ∧ zp i ρ = c := by
  cases c with
  | zero => exact ⟨i, hi, zp_i i⟩
  | succ k' =>
    refine ⟨cond (Nat.blt k' i) k' (k' + 1), ?_, ?_⟩
    · cases Nat.blt k' i
      · exact hc
      · exact Nat.lt_trans (Nat.lt_succ_self k') hc
    · have hρi : cond (Nat.blt k' i) k' (k' + 1) ≠ i := by
        cases hb : Nat.blt k' i
        · intro e; have := bf hb; rw [← show k' + 1 = i from e] at this; exact Nat.not_succ_le_self k' this
        · intro e; have := bt hb; rw [show k' = i from e] at this; exact Nat.lt_irrefl _ this
      rw [zp_o i _ hρi]
      show cond (Nat.blt (cond (Nat.blt k' i) k' (k' + 1)) i) (cond (Nat.blt k' i) k' (k' + 1) + 1) (cond (Nat.blt k' i) k' (k' + 1)) = k' + 1
      cases hb : Nat.blt k' i
      · show cond (Nat.blt (k' + 1) i) (k' + 1 + 1) (k' + 1) = k' + 1
        rw [LibDetAlt.blt_false (Nat.le_trans (bf hb) (Nat.le_succ k'))]; rfl
      · show cond (Nat.blt k' i) (k' + 1) k' = k' + 1; rw [hb]; rfl

theorem det_zW (n i : Nat) (hi : i < n) : det K n (zW K i) = sg K i := by
  unfold zW Z; rw [LibShiftDet.shift_det K i n _ hi]; show K.mul (sg K i) K.one = _; generalize sg K i = s; lib_ring

/-- **det(Z_i with rows i, r, s replaced by u, v, w) = (−1)^i · d3(u, v, w; 0, c_r, c_s).** -/
theorem three_row (n i r s : Nat) (hi : i < n) (hr : r < n) (hs : s < n) (hir : i ≠ r) (his : i ≠ s) (hrs : r ≠ s) (u v w : Nat → R) :
    det K n (setRow (setRow (setRow (Z K i) i u) r v) s w) = K.mul (sg K i) (d3 K u v w 0 (cr i r) (cr i s)) := by
  have e : det K n (setRow (setRow (setRow (Z K i) i u) r v) s w) = det K n (setRow (setRow (setRow (zW K i) i u) r v) s w) :=
    det_ext K n _ _ (fun a j _ _ => by
      cases Nat.decEq a s with
      | isTrue e => subst e; rw [sr_i, sr_i]
      | isFalse e =>
        rw [sr_o _ _ _ _ _ e, sr_o _ _ _ _ _ e]
        cases Nat.decEq a r with
        | isTrue e' => subst e'; rw [sr_i, sr_i]
        | isFalse e' =>
          rw [sr_o _ _ _ _ _ e', sr_o _ _ _ _ _ e']
          cases Nat.decEq a i with
          | isTrue e'' => subst e''; rw [sr_i, sr_i]
          | isFalse e'' => rw [sr_o _ _ _ _ _ e'', sr_o _ _ _ _ _ e'']; unfold zW; rw [sr_o _ _ _ _ _ e''])
  rw [e, perm3 K n (zW K i) (zp i) (fun ρ c _ => hW K i ρ c) (zp_surj n i hi) (fun a b _ _ h => zp_inj i a b h)
    i r s hi hr hs hir his hrs (zp_lt n i i hi hi) (zp_lt n i r hi hr) (zp_lt n i s hi hs) u v w,
    det_zW K n i hi, zp_i, zp_o i r (fun h => hir h.symm), zp_o i s (fun h => his h.symm)]
  generalize sg K i = g; generalize d3 K u v w 0 (cr i r) (cr i s) = d; lib_ring

end LibZPerm

#print axioms LibZPerm.three_row

lib/ZPerm4.lean

lean_all/lib/ZPerm4.lean

/-
  lib/ZPerm4.lean — T46 shared library, cycle 651 (G445b): determinant of a shifted identity matrix with four rows replaced.

  Any commutative ring, Z_i = minor I (i+1), i, r, s, t distinct (all < n):
    sr_congr    the same setRow on two matrices with equal entries
    four_row    det(Z_i with rows i, r, s, t replaced by u, v, w, z) = (−1)^i · d4(u, v, w, z; 0, c_r, c_s, c_t)
  Lean 4 core only, no axioms.
-/
import lib.D3Rows

namespace LibZPerm4
open DetChain LibRing LibSum LibSemi LibMat LibLegSum LibDet LibDetDep LibCharLin LibTwoRow LibTwoRowDet LibPermRows LibZPerm LibPermRows4

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

theorem sr_congr (X Y : Nat → Nat → R) (h : ∀ a b, X a b = Y a b) (k : Nat) (v : Nat → R) (a b : Nat) :
    setRow X k v a b = setRow Y k v a b := by
  show cond (Nat.beq a k) (v b) (X a b) = cond (Nat.beq a k) (v b) (Y a b); rw [h a b]

theorem base_eq (i : Nat) (u : Nat → R) (a b : Nat) : setRow (Z K i) i u a b = setRow (zW K i) i u a b := by
  cases Nat.decEq a i with
  | isTrue e => subst e; rw [sr_i, sr_i]
  | isFalse e => rw [sr_o _ _ _ _ _ e, sr_o _ _ _ _ _ e]; unfold zW; rw [sr_o _ _ _ _ _ e]

/-- **det(Z_i with rows i, r, s, t replaced by u, v, w, z) = (−1)^i d4(u, v, w, z; 0, c_r, c_s, c_t).** -/
theorem four_row (n i r s t : Nat) (hi : i < n) (hr : r < n) (hs : s < n) (ht : t < n)
    (hir : i ≠ r) (his : i ≠ s) (hit : i ≠ t) (hrs : r ≠ s) (hrt : r ≠ t) (hst : s ≠ t) (u v w z : Nat → R) :
    det K n (setRow (setRow (setRow (setRow (Z K i) i u) r v) s w) t z)
      = K.mul (sg K i) (d4 K u v w z 0 (cr i r) (cr i s) (cr i t)) := by
  have e : det K n (setRow (setRow (setRow (setRow (Z K i) i u) r v) s w) t z) = det K n (setRow (setRow (setRow (setRow (zW K i) i u) r v) s w) t z) :=
    det_ext K n _ _ (fun a b _ _ => sr_congr _ _ (sr_congr _ _ (sr_congr _ _ (base_eq K i u) r v) s w) t z a b)
  rw [e, perm4 K n (zW K i) (zp i) (fun ρ c _ => LibZPerm.hW K i ρ c) (zp_surj n i hi) (fun a b _ _ h => zp_inj i a b h)
    i r s t hi hr hs ht hir his hit hrs hrt hst (zp_lt n i i hi hi) (zp_lt n i r hi hr) (zp_lt n i s hi hs) (zp_lt n i t hi ht) u v w z,
    det_zW K n i hi, zp_i, zp_o i r (fun h => hir h.symm), zp_o i s (fun h => his h.symm), zp_o i t (fun h => hit h.symm)]
  generalize sg K i = g; generalize d4 K u v w z 0 (cr i r) (cr i s) (cr i t) = d; lib_ring

end LibZPerm4

#print axioms LibZPerm4.four_row

lib/ZPow.lean

lean_all/lib/ZPow.lean

/-
  lib/ZPow.lean — T46 shared library (G1087f): integer powers of a unit, signs (−1)ᵏ for k ∈ ℤ, and powers P^x for x ∈ ℤ, x ≥ 0.

    zp z w k        zᵏ for k ∈ ℤ (w = z⁻¹, zw = 1):  zp_succ, zp_pred, zp_add (zp (a+b) = zp a · zp b), zp_neg, zp_mul_self_neg
    sg k            (−1)ᵏ = zp (−1) (−1) k:  sg_add, sg_neg, sg_sq (sg k · sg k = 1), sg_two_mul (sg (2k) = 1)
    pz P x          P^{toNat x}:  pz_add (x = a, y = b natural ⇒ pz (x + y) = pz x · pz y), pz_ofNat
  Lean 4 core only, proofs by hand (lib_int for index arithmetic), no axioms.
-/
import lib.IntRing
import lib.QPoch

namespace LibZPow
open DetChain LibRing LibSum LibSemi LibInt

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

def zp (z w : R) : Int → R
  | Int.ofNat n => pw K z n
  | Int.negSucc n => pw K w (n + 1)

section Unit
variable (z w : R) (h : K.mul z w = K.one)

include h in
theorem zp_succ : ∀ x : Int, zp K z w (x + 1) = K.mul (zp K z w x) z
  | Int.ofNat n => rfl
  | Int.negSucc 0 => by
    show pw K z 0 = K.mul (K.mul K.one w) z
    show K.one = _
    rw [show K.mul (K.mul K.one w) z = K.mul z w by lib_ring, h]
  | Int.negSucc (n + 1) => by
    show pw K w (n + 1) = K.mul (K.mul (pw K w (n + 1)) w) z
    rw [show K.mul (K.mul (pw K w (n + 1)) w) z = K.mul (pw K w (n + 1)) (K.mul z w) by lib_ring, h]
    lib_ring

include h in
theorem zp_pred (x : Int) : zp K z w (x - 1) = K.mul (zp K z w x) w := by
  have e := zp_succ K z w h (x - 1)
  rw [show x - 1 + 1 = x by lib_int] at e
  rw [e, show K.mul (K.mul (zp K z w (x - 1)) z) w = K.mul (zp K z w (x - 1)) (K.mul z w) by lib_ring, h]
  lib_ring

include h in
theorem zp_add_ofNat (a : Int) : ∀ n : Nat, zp K z w (a + Int.ofNat n) = K.mul (zp K z w a) (zp K z w (Int.ofNat n))
  | 0 => by rw [show a + Int.ofNat 0 = a by lib_int]; show _ = K.mul _ K.one; lib_ring
  | n + 1 => by
    rw [show a + Int.ofNat (n + 1) = a + Int.ofNat n + 1 by show a + (Int.ofNat n + 1) = _; lib_int, zp_succ K z w h,
      zp_add_ofNat a n]
    show _ = K.mul (zp K z w a) (K.mul (pw K z n) z)
    show K.mul (K.mul (zp K z w a) (pw K z n)) z = _
    lib_ring

include h in
theorem zp_add_negSucc (a : Int) : ∀ n : Nat, zp K z w (a + Int.negSucc n) = K.mul (zp K z w a) (zp K z w (Int.negSucc n))
  | 0 => by
    rw [show a + Int.negSucc 0 = a - 1 by show a + (-(Int.ofNat 0 + 1)) = _; lib_int, zp_pred K z w h]
    show _ = K.mul _ (K.mul K.one w); lib_ring
  | n + 1 => by
    rw [show a + Int.negSucc (n + 1) = a + Int.negSucc n - 1 by show a + (-(Int.ofNat n + 1 + 1)) = a + (-(Int.ofNat n + 1)) - 1; lib_int,
      zp_pred K z w h, zp_add_negSucc a n]
    show K.mul (K.mul (zp K z w a) (pw K w (n + 1))) w = K.mul (zp K z w a) (K.mul (pw K w (n + 1)) w)
    lib_ring

include h in
theorem zp_add (a : Int) : ∀ b : Int, zp K z w (a + b) = K.mul (zp K z w a) (zp K z w b)
  | Int.ofNat n => zp_add_ofNat K z w h a n
  | Int.negSucc n => zp_add_negSucc K z w h a n

include h in
theorem zp_neg (a : Int) : K.mul (zp K z w a) (zp K z w (-a)) = K.one := by
  rw [← zp_add K z w h, show a + -a = Int.ofNat 0 by lib_int]; rfl

theorem zp_zero : zp K z w 0 = K.one := rfl

end Unit

/-- (−1)ᵏ for k ∈ ℤ -/
def sg (k : Int) : R := zp K (K.neg K.one) (K.neg K.one) k

theorem negone_sq : K.mul (K.neg K.one) (K.neg K.one) = (K.one : R) := by lib_ring

theorem sg_add (a b : Int) : sg K (a + b) = K.mul (sg K a) (sg K b) := zp_add K _ _ (negone_sq K) a b

theorem sgn_sq' : ∀ b, K.mul (pw K (K.neg K.one) b) (pw K (K.neg K.one) b) = (K.one : R)
  | 0 => by show K.mul K.one K.one = K.one; lib_ring
  | b + 1 => by
    show K.mul (K.mul (pw K (K.neg K.one) b) (K.neg K.one)) (K.mul (pw K (K.neg K.one) b) (K.neg K.one)) = K.one
    have hb := sgn_sq' b
    rw [show K.mul (K.mul (pw K (K.neg K.one) b) (K.neg K.one)) (K.mul (pw K (K.neg K.one) b) (K.neg K.one))
        = K.mul (pw K (K.neg K.one) b) (pw K (K.neg K.one) b) by lib_ring, hb]

theorem sg_sq : ∀ a : Int, K.mul (sg K a) (sg K a) = (K.one : R)
  | Int.ofNat n => sgn_sq' K n
  | Int.negSucc n => sgn_sq' K (n + 1)

theorem sg_two (a : Int) : sg K (a + a) = (K.one : R) := by rw [sg_add]; exact sg_sq K a

theorem sg_neg (a : Int) : sg K (-a) = (sg K a : R) := by
  have h := zp_neg K _ _ (negone_sq K) a
  have h2 := sg_sq K a
  -- sg a · sg(−a) = 1 = sg a · sg a, and sg a is its own inverse
  calc sg K (-a) = K.mul (K.mul (sg K a) (sg K a)) (sg K (-a)) := by rw [h2]; lib_ring
    _ = K.mul (sg K a) (K.mul (sg K a) (sg K (-a))) := by lib_ring
    _ = K.mul (sg K a) K.one := by rw [show K.mul (sg K a) (sg K (-a)) = K.one from h]
    _ = sg K a := by lib_ring

/-- P^x for an integer x (meant for x ≥ 0): P^{toNat x}. -/
def pz (P : R) (x : Int) : R := pw K P (Int.toNat x)

theorem pz_ofNat (P : R) (a : Nat) : pz K P (Int.ofNat a) = pw K P a := rfl

theorem pz_add (P : R) (x y : Int) (a b : Nat) (hx : x = Int.ofNat a) (hy : y = Int.ofNat b) :
    pz K P (x + y) = K.mul (pz K P x) (pz K P y) := by
  rw [hx, hy]
  show pw K P (a + b) = K.mul (pw K P a) (pw K P b)
  exact LibLucasDbl.pw_add' K P a b

theorem pz_eq (P : R) (x y : Int) (h : x = y) : pz K P x = pz K P y := by rw [h]

end LibZPow

#print axioms LibZPow.zp_add
#print axioms LibZPow.zp_neg
#print axioms LibZPow.sg_add
#print axioms LibZPow.sg_two
#print axioms LibZPow.sg_neg
#print axioms LibZPow.pz_add

lib/ZSplit.lean

lean_all/lib/ZSplit.lean

/-
  lib/ZSplit.lean — T46 shared library (G1087g): integer-window sums — halves and residues mod 3.

    zs_succ     zs F (n+1) = F(−(n+1)) + zs F n + F(n+1)
    zs_halves   zs F n = Σ_{i≤n} F i + Σ_{i<n} F(−(i+1))       (the two-half form used by the triple-product files)
    sum_three   Σ_{i<3N} g i = Σ_{t<N} (g(3t) + g(3t+1) + g(3t+2))
    zs_mod3     Supp G (3n) ⇒ zs G (3n) = Σ_{s<3} zs (t ↦ G(3t + s)) n
  Lean 4 core only, proofs by hand, no axioms.
-/
import lib.ZSum

namespace LibZSplit
open DetChain LibRing LibSum LibSemi LibInt LibMacMahon LibZSum LibQBinom

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

theorem zs_succ (F : Int → R) (n : Nat) :
    zs K F (n + 1) = K.add (K.add (F (- Int.ofNat (n + 1))) (zs K F n)) (F (Int.ofNat (n + 1))) := by
  unfold zs
  rw [show n + 1 + (n + 1) + 1 = (n + n + 1 + 1) + 1 by lib_nat, sum_rng_succ (toSemi K), sum_head K _ (n + n + 1)]
  rw [show Int.ofNat 0 - Int.ofNat (n + 1) = - Int.ofNat (n + 1) by show 0 - Int.ofNat (n + 1) = _; lib_int,
    show Int.ofNat (n + n + 1 + 1) - Int.ofNat (n + 1) = Int.ofNat (n + 1) by
      show Int.ofNat n + Int.ofNat n + 1 + 1 - (Int.ofNat n + 1) = Int.ofNat n + 1; lib_int]
  rw [sum_ext (toSemi K) (fun j => F (Int.ofNat (j + 1) - Int.ofNat (n + 1))) (fun i => F (Int.ofNat i - Int.ofNat n))
    (fun j => congrArg F (show Int.ofNat (j + 1) - Int.ofNat (n + 1) = Int.ofNat j - Int.ofNat n by
      show Int.ofNat j + 1 - (Int.ofNat n + 1) = _; lib_int)) _]
  rfl

theorem zs_halves (F : Int → R) : ∀ n : Nat,
    zs K F n = K.add (sum (toSemi K) (fun i => F (Int.ofNat i)) (rng (n + 1))) (sum (toSemi K) (fun i => F (- Int.ofNat (i + 1))) (rng n))
  | 0 => by
    show K.add (F (Int.ofNat 0 - Int.ofNat 0)) K.zero = K.add (K.add (F (Int.ofNat 0)) K.zero) K.zero
    rw [show Int.ofNat 0 - Int.ofNat 0 = Int.ofNat 0 from rfl]
    lib_ring
  | n + 1 => by
    rw [zs_succ, zs_halves F n, sum_rng_succ (toSemi K) (fun i => F (Int.ofNat i)) (n + 1),
      sum_rng_succ (toSemi K) (fun i => F (- Int.ofNat (i + 1))) n]
    show _ = K.add (K.add (sum (toSemi K) (fun i => F (Int.ofNat i)) (rng (n + 1))) (F (Int.ofNat (n + 1))))
      (K.add (sum (toSemi K) (fun i => F (- Int.ofNat (i + 1))) (rng n)) (F (- Int.ofNat (n + 1))))
    lib_ring

theorem sum_three (g : Nat → R) : ∀ N, sum (toSemi K) g (rng (3 * N))
    = sum (toSemi K) (fun t => K.add (K.add (g (3 * t)) (g (3 * t + 1))) (g (3 * t + 2))) (rng N)
  | 0 => rfl
  | N + 1 => by
    rw [show 3 * (N + 1) = 3 * N + 1 + 1 + 1 by lib_nat, sum_rng_succ (toSemi K) g (3 * N + 1 + 1), sum_rng_succ (toSemi K) g (3 * N + 1),
      sum_rng_succ (toSemi K) g (3 * N), sum_three g N,
      sum_rng_succ (toSemi K) (fun t => K.add (K.add (g (3 * t)) (g (3 * t + 1))) (g (3 * t + 2))) N]
    show K.add (K.add (K.add (sum (toSemi K) (fun t => K.add (K.add (g (3 * t)) (g (3 * t + 1))) (g (3 * t + 2))) (rng N)) (g (3 * N)))
        (g (3 * N + 1))) (g (3 * N + 1 + 1))
      = K.add (sum (toSemi K) (fun t => K.add (K.add (g (3 * t)) (g (3 * t + 1))) (g (3 * t + 2))) (rng N))
          (K.add (K.add (g (3 * N)) (g (3 * N + 1))) (g (3 * N + 1 + 1)))
    lib_ring

theorem zs_mod3 (G : Int → R) (n : Nat) (hS : Supp K G (3 * n)) :
    zs K G (3 * n) = K.add (K.add (zs K (fun t => G (3 * t)) n) (zs K (fun t => G (3 * t + 1)) n)) (zs K (fun t => G (3 * t + 2)) n) := by
  have hw := zs_window K G (3 * n) hS 0 2
  rw [← hw]
  rw [show 0 + (3 * n + 3 * n + 1) + 2 = 3 * (n + n + 1) by lib_nat, sum_three K _ (n + n + 1)]
  unfold zs
  refine Eq.trans (sum_ext (toSemi K) _ _ (fun t => ?_) _)
    (Eq.trans (sum_add (toSemi K) _ _ _) (congrArg (fun x => K.add x _) (sum_add (toSemi K) _ _ _)))
  have e : ∀ s : Nat, Int.ofNat (3 * t + s) - Int.ofNat (3 * n + 0) = 3 * (Int.ofNat t - Int.ofNat n) + Int.ofNat s := fun s => by
    show 3 * Int.ofNat t + Int.ofNat s - (3 * Int.ofNat n + 0) = _; lib_int
  show K.add (K.add (G (Int.ofNat (3 * t + 0) - Int.ofNat (3 * n + 0))) (G (Int.ofNat (3 * t + 1) - Int.ofNat (3 * n + 0))))
      (G (Int.ofNat (3 * t + 2) - Int.ofNat (3 * n + 0))) = _
  rw [e 0, e 1, e 2, show 3 * (Int.ofNat t - Int.ofNat n) + Int.ofNat 0 = 3 * (Int.ofNat t - Int.ofNat n) by lib_int]
  rfl

end LibZSplit

#print axioms LibZSplit.zs_halves
#print axioms LibZSplit.zs_mod3

lib/ZSum.lean

lean_all/lib/ZSum.lean

/-
  lib/ZSum.lean — T46 shared library (G1087e): finite sums over integer windows [−n, n].

    zs F n = Σ_{x=−n}^{n} F x   (indexed by i < 2n+1, x = i − n)
    Supp F n : F (±(n+1+i)) = 0 for all i   (support in [−n, n]; stated without natAbs)
    sum_shift_zero   Σ_{i<N} f (d+i) = Σ_{i<N} f i  if f vanishes on [0,d) and [N, N+d)
    zs_window        Σ_{i < a+(2n+1)+b} F (i − (n+a)) = zs F n   for F supported in [−n, n]
    zs_extend        zs F (n+e) = zs F n
    zs_shift_pos     zs (F (· + d)) (n + (d+e)) = zs F n,   zs_shift_neg   zs (F (· − d)) (n + (d+e)) = zs F n
    zs_neg           zs (F ∘ neg) n = zs F n
    zs_swap          double sums commute
  Lean 4 core only, proofs by hand (lib_int for the index arithmetic), no axioms.
-/
import lib.IntRing
import lib.MacMahon
import lib.NatSub

namespace LibZSum
open DetChain LibRing LibSum LibSemi LibInt LibMacMahon

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

theorem sum_zero_of (f : Nat → R) (d : Nat) (h : ∀ i, i < d → f i = K.zero) : sum (toSemi K) f (rng d) = K.zero := by
  rw [sum_ext_mem (toSemi K) _ (fun _ => K.zero) _ (fun i hi => h i (LibBinom.mem_rng hi))]
  exact sum_zero (toSemi K) _

theorem sum_shift_zero (f : Nat → R) (d N : Nat) (h1 : ∀ i, i < d → f i = K.zero) (h2 : ∀ i, i < d → f (N + i) = K.zero) :
    sum (toSemi K) (fun i => f (d + i)) (rng N) = sum (toSemi K) f (rng N) := by
  have a := sum_rng_add (toSemi K) f d N
  have b := sum_rng_add (toSemi K) f N d
  rw [Nat.add_comm d N, b, sum_zero_of K f d h1, sum_zero_of K (fun j => f (N + j)) d h2] at a
  have a' : K.add (sum (toSemi K) f (rng N)) K.zero = K.add K.zero (sum (toSemi K) (fun j => f (d + j)) (rng N)) := a
  rw [show K.add (sum (toSemi K) f (rng N)) K.zero = sum (toSemi K) f (rng N) by lib_ring,
    show K.add K.zero (sum (toSemi K) (fun j => f (d + j)) (rng N)) = sum (toSemi K) (fun j => f (d + j)) (rng N) by lib_ring] at a'
  exact a'.symm

def zs (F : Int → R) (n : Nat) : R := sum (toSemi K) (fun i => F (Int.ofNat i - Int.ofNat n)) (rng (n + n + 1))

def Supp (F : Int → R) (n : Nat) : Prop := ∀ i : Nat, F (Int.ofNat (n + 1 + i)) = K.zero ∧ F (- Int.ofNat (n + 1 + i)) = K.zero

theorem zs_window (F : Int → R) (n : Nat) (hS : Supp K F n) (a b : Nat) :
    sum (toSemi K) (fun i => F (Int.ofNat i - Int.ofNat (n + a))) (rng (a + (n + n + 1) + b)) = zs K F n := by
  rw [sum_rng_add (toSemi K) _ (a + (n + n + 1)) b, sum_rng_add (toSemi K) _ a (n + n + 1)]
  have z1 : sum (toSemi K) (fun i => F (Int.ofNat i - Int.ofNat (n + a))) (rng a) = K.zero := by
    apply sum_zero_of K _ a
    intro i hi
    obtain ⟨j, hj⟩ := LibMacMahon.exists_add (Nat.succ_le_of_lt hi)
    rw [show Int.ofNat i - Int.ofNat (n + a) = - Int.ofNat (n + 1 + j) by
      rw [hj]; show Int.ofNat i - (Int.ofNat n + (Int.ofNat i + 1 + Int.ofNat j)) = -(Int.ofNat n + 1 + Int.ofNat j); lib_int]
    exact (hS j).2
  have z2 : sum (toSemi K) (fun j => F (Int.ofNat (a + (n + n + 1) + j) - Int.ofNat (n + a))) (rng b) = K.zero := by
    apply sum_zero_of K _ b
    intro j _
    rw [show Int.ofNat (a + (n + n + 1) + j) - Int.ofNat (n + a) = Int.ofNat (n + 1 + j) by
      show Int.ofNat a + (Int.ofNat n + Int.ofNat n + 1) + Int.ofNat j - (Int.ofNat n + Int.ofNat a) = Int.ofNat n + 1 + Int.ofNat j; lib_int]
    exact (hS j).1
  have mid : sum (toSemi K) (fun j => F (Int.ofNat (a + j) - Int.ofNat (n + a))) (rng (n + n + 1)) = zs K F n := by
    unfold zs
    exact sum_ext (toSemi K) _ _ (fun j => congrArg F (show Int.ofNat (a + j) - Int.ofNat (n + a) = Int.ofNat j - Int.ofNat n by
      show Int.ofNat a + Int.ofNat j - (Int.ofNat n + Int.ofNat a) = _; lib_int)) _
  rw [z1, z2, mid]
  show K.add (K.add K.zero (zs K F n)) K.zero = zs K F n
  lib_ring

theorem zs_extend (F : Int → R) (n e : Nat) (hS : Supp K F n) : zs K F (n + e) = zs K F n := by
  have h := zs_window K F n hS e e
  rw [show e + (n + n + 1) + e = (n + e) + (n + e) + 1 by lib_nat, show n + e = n + e from rfl] at h
  exact h

theorem zs_shift_pos (F : Int → R) (n d e : Nat) (hS : Supp K F n) :
    zs K (fun x => F (x + Int.ofNat d)) (n + (d + e)) = zs K F n := by
  have h := zs_window K F n hS e (e + d + d)
  rw [← h]
  unfold zs
  rw [show n + (d + e) + (n + (d + e)) + 1 = e + (n + n + 1) + (e + d + d) by lib_nat]
  exact sum_ext (toSemi K) _ _ (fun i => congrArg F (show Int.ofNat i - Int.ofNat (n + (d + e)) + Int.ofNat d = Int.ofNat i - Int.ofNat (n + e) by
    show Int.ofNat i - (Int.ofNat n + (Int.ofNat d + Int.ofNat e)) + Int.ofNat d = Int.ofNat i - (Int.ofNat n + Int.ofNat e); lib_int)) _

theorem zs_shift_neg (F : Int → R) (n d e : Nat) (hS : Supp K F n) :
    zs K (fun x => F (x - Int.ofNat d)) (n + (d + e)) = zs K F n := by
  have h := zs_window K F n hS (d + d + e) e
  rw [← h]
  unfold zs
  rw [show n + (d + e) + (n + (d + e)) + 1 = (d + d + e) + (n + n + 1) + e by lib_nat]
  exact sum_ext (toSemi K) _ _ (fun i => congrArg F (show Int.ofNat i - Int.ofNat (n + (d + e)) - Int.ofNat d = Int.ofNat i - Int.ofNat (n + (d + d + e)) by
    show Int.ofNat i - (Int.ofNat n + (Int.ofNat d + Int.ofNat e)) - Int.ofNat d = Int.ofNat i - (Int.ofNat n + (Int.ofNat d + Int.ofNat d + Int.ofNat e)); lib_int)) _

theorem ofNat_sub (a b : Nat) (h : b ≤ a) : Int.ofNat (a - b) = Int.ofNat a - Int.ofNat b := by
  obtain ⟨e, he⟩ := LibMacMahon.exists_add h
  rw [he, LibNatSub.add_sub_l]
  show Int.ofNat e = Int.ofNat b + Int.ofNat e - Int.ofNat b
  lib_int

theorem zs_neg (F : Int → R) (n : Nat) : zs K (fun x => F (- x)) n = zs K F n := by
  unfold zs
  rw [sum_rev K (fun i => F (Int.ofNat i - Int.ofNat n)) (n + n)]
  exact sum_ext_mem (toSemi K) _ _ _ (fun i hi => congrArg F (by
    have hi' : i ≤ n + n := Nat.le_of_lt_succ (LibBinom.mem_rng hi)
    rw [ofNat_sub (n + n) i hi']
    show -(Int.ofNat i - Int.ofNat n) = Int.ofNat n + Int.ofNat n - Int.ofNat i - Int.ofNat n
    lib_int))

theorem zs_swap (G : Int → Int → R) (n m : Nat) :
    zs K (fun x => zs K (fun y => G x y) m) n = zs K (fun y => zs K (fun x => G x y) n) m := by
  unfold zs
  exact sum_swap (toSemi K) _ _ _

theorem supp_mono (F : Int → R) (n e : Nat) (hS : Supp K F n) : Supp K F (n + e) := by
  intro i
  rw [show n + e + 1 + i = n + 1 + (e + i) by lib_nat]
  exact hS (e + i)

end LibZSum

#print axioms LibZSum.zs_window
#print axioms LibZSum.zs_extend
#print axioms LibZSum.zs_shift_pos
#print axioms LibZSum.zs_shift_neg
#print axioms LibZSum.zs_neg
#print axioms LibZSum.zs_swap

lib/ZpField.lean

lean_all/lib/ZpField.lean

/-
  lib/ZpField.lean — T46 shared library, cycle 17: ℤ/p is a field.

  If p = k + 1 is prime (its only divisors are 1 and p), then in LibMod.znR k
    zp_noZD     no zero divisors (DetChain.NoZD (znR k))       — so LibPoly.roots_lt, LibPolyFactor.factor_all apply over 𝔽_p
    zp_inv      x ≠ 0 ⇒ ∃ y, x · y = 1
    val_zero    x = 0 ↔ x.val = 0
  Euclid (LibGcd.euclid_prime) and modular inverse (LibCRT.inv_mod).

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

namespace LibZp
open LibMod

variable (k : Nat)

/-- p = k + 1 is prime. -/
def Prime1 : Prop := ∀ d, d ∣ k + 1 → d = 1 ∨ d = k + 1

theorem zero_val : ((znR k).zero).val = 0 := rfl

theorem eq_zero_of_val {x : ZN k} (h : x.val = 0) : x = (znR k).zero := ext k (h.trans (zero_val k).symm)

theorem val_of_eq_zero {x : ZN k} (h : x = (znR k).zero) : x.val = 0 := by rw [h]; rfl

/-- p ∣ v, v < p ⇒ v = 0. -/
theorem zero_of_dvd_lt {v : Nat} (hv : v < k + 1) (h : k + 1 ∣ v) : v = 0 := by
  have := md_zero_of_dvd (pos k) h
  rw [md_of_lt (pos k) hv] at this
  exact this

/-- **No zero divisors.** -/
theorem zp_noZD (hp : Prime1 k) : DetChain.NoZD (znR k) := by
  intro x y h
  have h0 : md (k + 1) (x.val * y.val) = 0 := val_of_eq_zero k h
  have hd : k + 1 ∣ x.val * y.val := dvd_of_md_zero h0
  cases LibGcd.euclid_prime hp hd with
  | inl hx => exact Or.inl (eq_zero_of_val k (zero_of_dvd_lt k x.property hx))
  | inr hy => exact Or.inr (eq_zero_of_val k (zero_of_dvd_lt k y.property hy))

/-- **Inverse.** -/
theorem zp_inv (hp : Prime1 k) (x : ZN k) (hx : x ≠ (znR k).zero) : ∃ y, (znR k).mul x y = (znR k).one := by
  have hc : ∀ d, d ∣ x.val → d ∣ k + 1 → d = 1 := by
    intro d d1 d2
    cases hp d d2 with
    | inl h1 => exact h1
    | inr hpd =>
      rw [hpd] at d1
      exact absurd (eq_zero_of_val k (zero_of_dvd_lt k x.property d1)) hx
  obtain ⟨s, hs⟩ := LibCRT.inv_mod (pos k) hc
  refine ⟨mk k s, ext k ?_⟩
  show md (k + 1) (x.val * md (k + 1) s) = md (k + 1) 1
  rw [md_mul_r (pos k), hs]

/-- Test: in 𝔽_7 the inverse of 3 is 5. -/
example : ((znR 6).mul (mk 6 3) (mk 6 5)).val = ((znR 6).one).val := rfl

end LibZp

#print axioms LibZp.zp_noZD
#print axioms LibZp.zp_inv

ns/AbelCount.lean

lean_all/ns/AbelCount.lean

/-
  ns/AbelCount.lean — T37 cycle 947 (goal G624c, piece 5): Abel summation against counting functions, over any
  commutative ring. For a list of naturals n_p ≤ M and any f : ℕ → R, with g_j = f_j − f_{j+1} and
  cnt(l, j) = #{p : n_p ≤ j}:
    abel_list   Σ_p f(n_p) = Σ_{j<M} cnt(l, j)·g_j + |l|·f(M).
  With n_p = |k_p|_∞, f(n) = n^{−4}, the cube count cnt(l, j) ≤ (2j+1)³ (CubeCount) and
  (2j+1)³((j+1)⁴ − j⁴) ≤ 51 j³(j+1)³ this bounds Σ_p |k_p|_∞^{−4} uniformly in the truncation.

  Lean 4 core plus NSChain (sumN, sumN_add, sumN_zero), lib/RingTac.
-/
import ns.DyadicLarge
import lib.RingTac

namespace AbelCount
open DetChain NSChain LibRing

/-- the number of entries ≤ j -/
def cnt : List Nat → Nat → Nat
  | [], _ => 0
  | n :: l, j => cond (Nat.ble n j) (cnt l j + 1) (cnt l j)

section Gen
variable {R : Type} (K : CRing R)

/-- indicator of n ≤ j -/
def ind (n j : Nat) : R := cond (Nat.ble n j) K.one K.zero

def gd (f : Nat → R) (j : Nat) : R := K.add (f j) (K.neg (f (j + 1)))

def lsumN (h : Nat → R) : List Nat → R
  | [] => K.zero
  | n :: l => K.add (h n) (lsumN h l)

theorem ble_false {n j : Nat} (h : j < n) : Nat.ble n j = false := by
  cases hb : Nat.ble n j with
  | false => rfl
  | true => exact absurd (Nat.lt_of_lt_of_le h (Nat.le_of_ble_eq_true hb)) (Nat.lt_irrefl j)

theorem zero_below (f : Nat → R) (n : Nat) :
    ∀ J, J ≤ n → sumN K (fun j => K.mul (ind K n j) (gd K f j)) J = K.zero
  | 0, _ => rfl
  | J + 1, h => by
    show K.add (sumN K (fun j => K.mul (ind K n j) (gd K f j)) J) (K.mul (ind K n J) (gd K f J)) = K.zero
    rw [zero_below f n J (Nat.le_of_lt h)]
    have hi : ind K n J = K.zero := by
      show cond (Nat.ble n J) K.one K.zero = K.zero
      rw [ble_false h]; rfl
    rw [hi]; lib_ring

theorem tele_one (f : Nat → R) (n : Nat) :
    ∀ M, n ≤ M → K.add (sumN K (fun j => K.mul (ind K n j) (gd K f j)) M) (f M) = f n
  | 0, h => by
    have e : n = 0 := Nat.eq_zero_of_le_zero h
    rw [e]; show K.add K.zero (f 0) = f 0; lib_ring
  | M + 1, h => by
    cases Nat.eq_or_lt_of_le h with
    | inl e =>
      rw [zero_below K f n (M + 1) (Nat.le_of_eq e.symm), e]; lib_ring
    | inr l =>
      have hM : n ≤ M := Nat.le_of_lt_succ l
      have ih := tele_one f n M hM
      have hi : ind K n M = K.one := by
        show cond (Nat.ble n M) K.one K.zero = K.one
        rw [Nat.ble_eq_true_of_le hM]; rfl
      show K.add (K.add (sumN K (fun j => K.mul (ind K n j) (gd K f j)) M) (K.mul (ind K n M) (gd K f M)))
        (f (M + 1)) = f n
      rw [hi, ← ih]
      show _ = K.add (sumN K (fun j => K.mul (ind K n j) (gd K f j)) M) (f M)
      generalize sumN K (fun j => K.mul (ind K n j) (gd K f j)) M = S
      show K.add (K.add S (K.mul K.one (K.add (f M) (K.neg (f (M + 1)))))) (f (M + 1)) = K.add S (f M)
      lib_ring

theorem sumN_congr (f g : Nat → R) (h : ∀ j, f j = g j) : ∀ N, sumN K f N = sumN K g N
  | 0 => rfl
  | N + 1 => by
    show K.add (sumN K f N) (f N) = K.add (sumN K g N) (g N)
    rw [sumN_congr f g h N, h N]

theorem cnt_step (n : Nat) (l : List Nat) (g : Nat → R) (j : Nat) :
    K.mul (castN K (cnt (n :: l) j)) (g j) = K.add (K.mul (ind K n j) (g j)) (K.mul (castN K (cnt l j)) (g j)) := by
  show K.mul (castN K (cond (Nat.ble n j) (cnt l j + 1) (cnt l j))) (g j)
    = K.add (K.mul (cond (Nat.ble n j) K.one K.zero) (g j)) (K.mul (castN K (cnt l j)) (g j))
  generalize Nat.ble n j = b
  cases b with
  | true =>
    show K.mul (K.add K.one (castN K (cnt l j))) (g j) = K.add (K.mul K.one (g j)) (K.mul (castN K (cnt l j)) (g j))
    lib_ring
  | false =>
    show K.mul (castN K (cnt l j)) (g j) = K.add (K.mul K.zero (g j)) (K.mul (castN K (cnt l j)) (g j))
    lib_ring

theorem abel_list (f : Nat → R) (M : Nat) :
    ∀ (l : List Nat), (∀ n, n ∈ l → n ≤ M) →
      lsumN K f l = K.add (sumN K (fun j => K.mul (castN K (cnt l j)) (gd K f j)) M) (K.mul (castN K l.length) (f M))
  | [], _ => by
    show K.zero = K.add (sumN K (fun j => K.mul K.zero (gd K f j)) M) (K.mul K.zero (f M))
    have hz : sumN K (fun j => K.mul K.zero (gd K f j)) M = sumN K (fun _ => K.zero) M := by
      exact sumN_congr K _ _ (fun j => by show K.mul K.zero (gd K f j) = K.zero; lib_ring) M
    rw [hz, sumN_zero]; lib_ring
  | n :: l, h => by
    have ih := abel_list f M l (fun x hx => h x (List.Mem.tail _ hx))
    have t := tele_one K f n M (h n (List.Mem.head _))
    have hs : sumN K (fun j => K.mul (castN K (cnt (n :: l) j)) (gd K f j)) M
        = K.add (sumN K (fun j => K.mul (ind K n j) (gd K f j)) M) (sumN K (fun j => K.mul (castN K (cnt l j)) (gd K f j)) M) := by
      rw [← sumN_add]; exact sumN_congr K _ _ (fun j => cnt_step K n l (gd K f) j) M
    show K.add (f n) (lsumN K f l) = K.add (sumN K (fun j => K.mul (castN K (cnt (n :: l) j)) (gd K f j)) M)
      (K.mul (K.add K.one (castN K l.length)) (f M))
    rw [hs, ih, ← t]
    generalize sumN K (fun j => K.mul (ind K n j) (gd K f j)) M = A
    generalize sumN K (fun j => K.mul (castN K (cnt l j)) (gd K f j)) M = B
    lib_ring

end Gen

end AbelCount

#print axioms AbelCount.zero_below
#print axioms AbelCount.tele_one
#print axioms AbelCount.abel_list

ns/Agmon.lean

lean_all/ns/Agmon.lean

/-
  ns/Agmon.lean — T37 cycle 137: Agmon's inequality for 3D truncations, with K free:
      (Σ_low |û| + Σ_high |û|)² ≤ 2·W_low·Σ|k|²|û|² + 2·W_high·Σ|k|⁴|û|²,
      W_low = Σ_{shells n<K} 26(n+1)²·u_n² = 26K,   W_high ≤ 26·u_K   (u_n = 1/(n+1))
  so (Σ|û|)² ≤ 52K‖∇u‖² + 52‖Δu‖²/(K+1) for every K — choosing K ≈ ‖Δu‖/‖∇u‖ gives ‖u‖_∞² ≤ C‖∇u‖‖Δu‖.

  Lean 4 core plus ns/Sobolev (sobolev, cN, lsum_le_len), ns/BaselTail, ns/Basel, lib/RingTac.
    sq_add_le    (a + b)² ≤ 2a² + 2b²
    low_weight   Σ_{n<K} 26(n+1)² u_n² = 26·K           (each shell term is 26)
    agmon        the combination, from two Sobolev-type bounds
-/
import ns.Sobolev
import ns.BaselTail

namespace Agmon
open DetChain NSChain LibRing Basel SumCS

variable {R : Type} (K : CRing R) {I : Type}

theorem sq_add_le (P : Cone K) (a b : R) :
    Le K P (sq K (K.add a b)) (K.add (K.add (sq K a) (sq K a)) (K.add (sq K b) (sq K b))) :=
  ⟨sq K (sub K a b), P.nn_sq _, by unfold sq; lib_ring⟩

theorem low_weight (u : Nat → R) (hu : ∀ n, K.mul (cN K (n + 1)) (u n) = K.one) :
    ∀ Kk, sumN K (fun n => K.mul (cN K 26) (K.mul (K.mul (cN K (n + 1)) (cN K (n + 1))) (K.mul (u n) (u n)))) Kk
      = K.mul (cN K 26) (cN K Kk)
  | 0 => by show K.zero = K.mul (cN K 26) K.zero; lib_ring
  | Kk + 1 => by
    show K.add (sumN K _ Kk) (K.mul (cN K 26) (K.mul (K.mul (cN K (Kk + 1)) (cN K (Kk + 1))) (K.mul (u Kk) (u Kk))))
      = K.mul (cN K 26) (K.add (cN K Kk) K.one)
    rw [low_weight u hu Kk]
    have h := hu Kk
    revert h
    generalize cN K (Kk + 1) = c
    generalize u Kk = a
    intro h
    rw [show K.mul (K.mul c c) (K.mul a a) = K.mul (K.mul c a) (K.mul c a) by lib_ring, h]; lib_ring

/-- **Agmon, combination step**: from Sobolev-type bounds on the low and the high part. -/
theorem agmon (P : Cone K) (XL XH WL WH ZL PH : R)
    (hL : Le K P (sq K XL) (K.mul WL ZL)) (hH : Le K P (sq K XH) (K.mul WH PH)) :
    Le K P (sq K (K.add XL XH))
      (K.add (K.add (K.mul WL ZL) (K.mul WL ZL)) (K.add (K.mul WH PH) (K.mul WH PH))) :=
  le_trans' K P _ _ _ (sq_add_le K P XL XH)
    (Riccati.le_add K P _ _ _ _ (Riccati.le_add K P _ _ _ _ hL hL) (Riccati.le_add K P _ _ _ _ hH hH))

end Agmon

#print axioms Agmon.sq_add_le
#print axioms Agmon.low_weight
#print axioms Agmon.agmon

ns/AgmonAbsorb.lean

lean_all/ns/AgmonAbsorb.lean

/-
  ns/AgmonAbsorb.lean — T37 (goal G859c2b2, piece d1): absorbing the two Agmon force terms into the H³ dissipation,
  over an ordered field (scalar lemmas). With x = Kc + 2 chosen by CutChoose (9x⁴Z ≤ 16B above the first cut) and
  the interpolation A² ≤ ZB:
    t1_quart   9³·(x³Z²)⁴ ≤ 16³·Z⁵B³
    t2_quart   9·(xZA)⁴ ≤ 16·Z⁵B³
    absorb     m·T⁴ ≤ c·Z⁵B³ and c·Z⁵ ≤ m·4e³D  ⇒  T ≤ e·B + D          (RootYoung.root4)
    absorb2    T² ≤ Z³B and Z³ ≤ 4eD  ⇒  T ≤ e·B + D                     (RootYoung.root2; the first cut, x = 2)
    za_sq      A² ≤ ZB  ⇒  (ZA)² ≤ Z³B
  so every force term is ≤ εB + C(ε)·Z^k: the H² step closes with an additive constant (G859c2c).

  Lean 4 core plus ns/RootYoung, ns/EnstGlobal, ns/ForceSob, ns/WeightedCS.
-/
import ns.RootYoung
import ns.EnstLocal
import ns.WeightedCS

namespace AgmonAbsorb
open DetChain NSChain DyadicSmallGlobal LibRing DyadicLarge RootYoung

section Gen
variable {R : Type} (K : CRing R) (O : OrdR K)

def q4 (x : R) : R := K.mul (K.mul x x) (K.mul x x)
def c3 (x : R) : R := K.mul (K.mul x x) x
def q5 (x : R) : R := K.mul x (q4 K x)

theorem cube_mono (X Y : R) (hX : O.nn X) (hY : O.nn Y) (h : Le K O.toCone X Y) :
    Le K O.toCone (c3 K X) (c3 K Y) := by
  have s := WeightedCS.le_mul_both' K O.toCone X Y X Y hX hY h h
  exact WeightedCS.le_mul_both' K O.toCone (K.mul X X) (K.mul Y Y) X Y (O.nn_sq X) hY s h

/-- 9³·(x³Z²)⁴ ≤ 16³·Z⁵B³ -/
theorem t1_quart (x Z B : R) (hZ : O.nn Z) (hB : O.nn B)
    (h : Le K O.toCone (K.mul (castN K 9) (K.mul (q4 K x) Z)) (K.mul (castN K 16) B)) :
    Le K O.toCone (K.mul (c3 K (castN K 9)) (q4 K (K.mul (c3 K x) (K.mul Z Z))))
      (K.mul (c3 K (castN K 16)) (K.mul (q5 K Z) (c3 K B))) := by
  have hX : O.nn (K.mul (castN K 9) (K.mul (q4 K x) Z)) :=
    O.nn_mul _ _ (RatModel.nn_cast K O.toCone 9) (O.nn_mul _ _ (O.nn_mul _ _ (O.nn_sq _) (O.nn_sq _)) hZ)
  have hY : O.nn (K.mul (castN K 16) B) := O.nn_mul _ _ (RatModel.nn_cast K O.toCone 16) hB
  have s := cube_mono K O _ _ hX hY h
  have hZ5 : O.nn (q5 K Z) := O.nn_mul _ _ hZ (O.nn_mul _ _ (O.nn_sq _) (O.nn_sq _))
  have s5 := ForceSob.le_mul_right' K O.toCone s hZ5
  have e1 : K.mul (c3 K (castN K 9)) (q4 K (K.mul (c3 K x) (K.mul Z Z)))
      = K.mul (c3 K (K.mul (castN K 9) (K.mul (q4 K x) Z))) (q5 K Z) := by
    unfold q5 q4 c3; lib_ring
  have e2 : K.mul (c3 K (castN K 16)) (K.mul (q5 K Z) (c3 K B)) = K.mul (c3 K (K.mul (castN K 16) B)) (q5 K Z) := by
    unfold q5 q4 c3; lib_ring
  rw [e1, e2]; exact s5

/-- 9·(xZA)⁴ ≤ 16·Z⁵B³ -/
theorem t2_quart (x Z A B : R) (hZ : O.nn Z) (hB : O.nn B)
    (h : Le K O.toCone (K.mul (castN K 9) (K.mul (q4 K x) Z)) (K.mul (castN K 16) B))
    (hA : Le K O.toCone (K.mul A A) (K.mul Z B)) :
    Le K O.toCone (K.mul (castN K 9) (q4 K (K.mul x (K.mul Z A))))
      (K.mul (castN K 16) (K.mul (q5 K Z) (c3 K B))) := by
  have hX : O.nn (K.mul (castN K 9) (K.mul (q4 K x) Z)) :=
    O.nn_mul _ _ (RatModel.nn_cast K O.toCone 9) (O.nn_mul _ _ (O.nn_mul _ _ (O.nn_sq _) (O.nn_sq _)) hZ)
  have hZB : O.nn (K.mul Z B) := O.nn_mul _ _ hZ hB
  have s2 := EnstLocal.sq_mono K O _ _ (O.nn_sq A) hZB hA
  have s := WeightedCS.le_mul_both' K O.toCone _ _ _ _ hX (O.nn_sq _) h s2
  have hZ3 : O.nn (c3 K Z) := O.nn_mul _ _ (O.nn_sq _) hZ
  have s3 := ForceSob.le_mul_right' K O.toCone s hZ3
  have e1 : K.mul (castN K 9) (q4 K (K.mul x (K.mul Z A)))
      = K.mul (K.mul (K.mul (castN K 9) (K.mul (q4 K x) Z)) (K.mul (K.mul A A) (K.mul A A))) (c3 K Z) := by
    unfold c3 q4; lib_ring
  have e2 : K.mul (castN K 16) (K.mul (q5 K Z) (c3 K B))
      = K.mul (K.mul (K.mul (castN K 16) B) (K.mul (K.mul Z B) (K.mul Z B))) (c3 K Z) := by
    unfold q5 q4 c3; lib_ring
  rw [e1, e2]; exact s3

/-- **m·T⁴ ≤ c·Z⁵B³, c·Z⁵ ≤ m·4e³D ⇒ T ≤ eB + D** -/
theorem absorb (T Z B c m e D : R) (hm : O.nn m) (hm0 : m ≠ K.zero) (hB : O.nn B) (he : O.nn e) (hD : O.nn D)
    (hT : Le K O.toCone (K.mul m (q4 K T)) (K.mul c (K.mul (q5 K Z) (c3 K B))))
    (hcD : Le K O.toCone (K.mul c (q5 K Z)) (K.mul m (K.mul (four K) (K.mul (c3 K e) D)))) :
    Le K O.toCone T (K.add (K.mul e B) D) := by
  have hB3 : O.nn (c3 K B) := O.nn_mul _ _ (O.nn_sq _) hB
  have s := ForceSob.le_mul_right' K O.toCone hcD hB3
  have e1 : K.mul c (K.mul (q5 K Z) (c3 K B)) = K.mul (K.mul c (q5 K Z)) (c3 K B) := by lib_ring
  have e2 : K.mul (K.mul m (K.mul (four K) (K.mul (c3 K e) D))) (c3 K B)
      = K.mul m (K.mul (four K) (K.mul (K.mul (K.mul (K.mul e B) (K.mul e B)) (K.mul e B)) D)) := by
    unfold c3; lib_ring
  rw [e1] at hT
  rw [e2] at s
  have t := le_trans' K O.toCone _ _ _ hT s
  have t' := EnstGlobal.le_of_mul_le K O m _ _ hm hm0 t
  exact root4 K O (K.mul e B) D T (O.nn_mul _ _ he hB) hD t'

/-- (ZA)² ≤ Z³B from A² ≤ ZB -/
theorem za_sq (Z A B : R) (hA : Le K O.toCone (K.mul A A) (K.mul Z B)) :
    Le K O.toCone (K.mul (K.mul Z A) (K.mul Z A)) (K.mul (c3 K Z) B) := by
  have s := le_mul_nn K O.toCone (K.mul Z Z) _ _ (O.nn_sq Z) hA
  have e1 : K.mul (K.mul Z A) (K.mul Z A) = K.mul (K.mul Z Z) (K.mul A A) := by lib_ring
  have e2 : K.mul (c3 K Z) B = K.mul (K.mul Z Z) (K.mul Z B) := by unfold c3; lib_ring
  rw [e1, e2]; exact s

/-- **T² ≤ Z³B, Z³ ≤ 4eD ⇒ T ≤ eB + D** -/
theorem absorb2 (T Z B e D : R) (hB : O.nn B) (he : O.nn e) (hD : O.nn D)
    (hT : Le K O.toCone (K.mul T T) (K.mul (c3 K Z) B))
    (hZD : Le K O.toCone (c3 K Z) (K.mul (four K) (K.mul e D))) :
    Le K O.toCone T (K.add (K.mul e B) D) := by
  have s := ForceSob.le_mul_right' K O.toCone hZD hB
  have e2 : K.mul (K.mul (four K) (K.mul e D)) B = K.mul (four K) (K.mul (K.mul e B) D) := by lib_ring
  rw [e2] at s
  exact root2 K O (K.mul e B) D T (O.nn_mul _ _ he hB) hD (le_trans' K O.toCone _ _ _ hT s)

end Gen

end AgmonAbsorb

#print axioms AgmonAbsorb.cube_mono
#print axioms AgmonAbsorb.t1_quart
#print axioms AgmonAbsorb.t2_quart
#print axioms AgmonAbsorb.absorb
#print axioms AgmonAbsorb.za_sq
#print axioms AgmonAbsorb.absorb2

ns/AgmonCollapse.lean

lean_all/ns/AgmonCollapse.lean

/-
  ns/AgmonCollapse.lean — T37 (goal G859c2b2, piece d2): collapsing the right side of ForceAgmonW.force_agmonW to two
  Agmon terms, over an ordered field (scalar lemma). With x = Kc + 2, C₁ = Kc + 1 ≤ x, x·i = 1 and the cut condition
  B ≤ 9x⁴Z (CutChoose), with the numerals 3, 9, 108 kept as parameters t₃, t₉, t₁₀₈ ≥ 0 (instantiated by castN):
    collapse   ι²(Z + i⁴B)(2·3(C₁²·108C₁ + x²·108x)·Z + 2(108C₁ + 108x)·A)
                 ≤ ι²·(k₁·x³Z² + k₂·xZA),   k₁ = (1+9)·2·3·(108+108),  k₂ = (1+9)·2·(108+108),
  the two terms that AgmonAbsorb bounds by εB + C(ε)Z⁵.

  Lean 4 core plus ns/AgmonAbsorb.
-/
import ns.AgmonAbsorb

namespace AgmonCollapse
open DetChain NSChain DyadicSmallGlobal LibRing DyadicLarge RootYoung AgmonAbsorb

section Gen
variable {R : Type} (K : CRing R) (O : OrdR K)

theorem le_eq (x y : R) (h : x = y) : Le K O.toCone x y := by
  rw [h]; exact le_refl' K O.toCone y

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

def k1 (t3 t9 t108 : R) : R := K.mul (K.add K.one (t9)) (K.mul (two K) (K.mul (t3) (K.add (t108) (t108))))
def k2 (t9 t108 : R) : R := K.mul (K.add K.one (t9)) (K.mul (two K) (K.add (t108) (t108)))

/-- **The collapse of the Agmon force bound.** -/
theorem collapse (t3 t9 t108 : R) (h3 : O.nn t3) (h9 : O.nn t9) (h108 : O.nn t108) (ι Z A B x C1 i : R) (hZ : O.nn Z) (hA : O.nn A) (hB : O.nn B) (hC1 : O.nn C1) (hx : O.nn x)
    (hC : Le K O.toCone C1 x) (hxi : K.mul x i = K.one)
    (hP : Le K O.toCone B (K.mul (t9) (K.mul (q4 K x) Z))) :
    Le K O.toCone
      (K.mul (K.mul ι ι)
        (K.mul (K.add Z (K.mul (K.mul (K.mul i i) (K.mul i i)) B))
          (K.add
            (K.mul (K.add K.one K.one) (K.mul
              (K.mul (t3) (K.add (K.mul (K.mul C1 C1) (K.mul (t108) C1)) (K.mul (K.mul x x) (K.mul (t108) x))))
              Z))
            (K.mul (K.add K.one K.one) (K.mul (K.add (K.mul (t108) C1) (K.mul (t108) x)) A)))))
      (K.mul (K.mul ι ι) (K.add (K.mul (k1 K t3 t9 t108) (K.mul (c3 K x) (K.mul Z Z))) (K.mul (k2 K t9 t108) (K.mul x (K.mul Z A))))) := by
  have n108 : O.nn (t108) := h108
  have n3 : O.nn (t3) := h3
  have n2 : O.nn (K.add K.one K.one) := O.nn_add _ _ (RatModel.nn_one' K O.toCone) (RatModel.nn_one' K O.toCone)
  have ni4 : O.nn (K.mul (K.mul i i) (K.mul i i)) := O.nn_mul _ _ (O.nn_sq _) (O.nn_sq _)
  -- first factor: Z + i⁴B ≤ Z + 9Z
  have f1 := le_mul_nn K O.toCone _ _ _ ni4 hP
  have e9 : K.mul (K.mul (K.mul i i) (K.mul i i)) (K.mul (t9) (K.mul (q4 K x) Z))
      = K.mul (t9) (K.mul (q4 K (K.mul x i)) Z) := by unfold q4; lib_ring
  rw [e9, hxi] at f1
  have e1 : K.mul (t9) (K.mul (q4 K K.one) Z) = K.mul (t9) Z := by unfold q4; lib_ring
  rw [e1] at f1
  have F1 := le_add' K O.toCone _ _ _ _ (le_refl' K O.toCone Z) f1
  -- second factor
  have c3le := cube_mono K O _ _ hC1 hx hC
  have g1 : Le K O.toCone (K.mul (K.mul C1 C1) (K.mul (t108) C1)) (K.mul (K.mul x x) (K.mul (t108) x)) := by
    have s := le_mul_nn K O.toCone _ _ _ n108 c3le
    have ea : K.mul (K.mul C1 C1) (K.mul (t108) C1) = K.mul (t108) (c3 K C1) := by unfold c3; lib_ring
    have eb : K.mul (K.mul x x) (K.mul (t108) x) = K.mul (t108) (c3 K x) := by unfold c3; lib_ring
    rw [ea, eb]; exact s
  have g2 := le_add' K O.toCone _ _ _ _ g1 (le_refl' K O.toCone (K.mul (K.mul x x) (K.mul (t108) x)))
  have g3 := le_mul_nn K O.toCone _ _ _ n2 (ForceSob.le_mul_right' K O.toCone (le_mul_nn K O.toCone _ _ _ n3 g2) hZ)
  have h1 := le_mul_nn K O.toCone _ _ _ n108 hC
  have h2 := le_add' K O.toCone _ _ _ _ h1 (le_refl' K O.toCone (K.mul (t108) x))
  have h3 := le_mul_nn K O.toCone _ _ _ n2 (ForceSob.le_mul_right' K O.toCone h2 hA)
  have F2 := le_add' K O.toCone _ _ _ _ g3 h3
  have nZ9 : O.nn (K.add Z (K.mul (t9) Z)) := O.nn_add _ _ hZ (O.nn_mul _ _ (h9) hZ)
  have nF1 : O.nn (K.add Z (K.mul (K.mul (K.mul i i) (K.mul i i)) B)) := O.nn_add _ _ hZ (O.nn_mul _ _ ni4 hB)
  have nx3 : O.nn (K.mul (K.mul x x) (K.mul (t108) x)) := O.nn_mul _ _ (O.nn_sq _) (O.nn_mul _ _ n108 hx)
  have nS2 : O.nn (K.add
      (K.mul (K.add K.one K.one) (K.mul (K.mul (t3) (K.add (K.mul (K.mul x x) (K.mul (t108) x))
        (K.mul (K.mul x x) (K.mul (t108) x)))) Z))
      (K.mul (K.add K.one K.one) (K.mul (K.add (K.mul (t108) x) (K.mul (t108) x)) A))) :=
    O.nn_add _ _ (O.nn_mul _ _ n2 (O.nn_mul _ _ (O.nn_mul _ _ n3 (O.nn_add _ _ nx3 nx3)) hZ))
      (O.nn_mul _ _ n2 (O.nn_mul _ _ (O.nn_add _ _ (O.nn_mul _ _ n108 hx) (O.nn_mul _ _ n108 hx)) hA))
  have P := WeightedCS.le_mul_both' K O.toCone _ _ _ _ nF1 nS2 F1 F2
  refine le_trans' K O.toCone _ _ _ (le_mul_nn K O.toCone _ _ _ (O.nn_sq ι) P) (le_eq K O _ _ ?_)
  unfold k1 k2 two c3; lib_ring

end Gen

end AgmonCollapse

#print axioms AgmonCollapse.le_eq
#print axioms AgmonCollapse.collapse

ns/AgmonHalves.lean

lean_all/ns/AgmonHalves.lean

/-
  ns/AgmonHalves.lean — T37 cycle 221 (goal G2, step 1): the shell weights of Agmon's two halves.

  Lean 4 core plus ns/Sobolev (cN_mul, cN_le, lsum_le_len), ns/BaselTail (tail), ns/Basel, lib/RingTac. Shell n
  (|k|_∞ = n + 1) has at most 26(n + 1)² modes; u_n = 1/(n + 1).
    low_weights    ω_i ≤ u_n² on shells n < K   ⇒  Σ_{n<K} Σ_{shell n} ω ≤ 26·K        (ω = |k|^{-2})
    high_weights   ω_i ≤ u_n⁴ on shells n > K   ⇒  Σ_{j<M} Σ_{shell K+1+j} ω ≤ 26·u_K   (ω = |k|^{-4})
  With Sobolev.sobolev: (Σ_low |û|)² ≤ 26K·Z and (Σ_high |û|)² ≤ 26 u_K·P — the hypotheses hL, hH of
  LocalContSobolev at cut K (low shells n < K, high shells n > K; shell K goes to either side with weight 26).
-/
import ns.Sobolev
import ns.BaselTail

namespace AgmonHalves
open DetChain NSChain LibRing Basel Sobolev

variable {R : Type} (K : CRing R) {I : Type}

theorem shell_bound (P : Cone K)
    (shell : Nat → List I) (ω : I → R) (n : Nat) (e : R) (he : P.nn e)
    (hlen : (shell n).length ≤ 26 * ((n + 1) * (n + 1)))
    (hw : ∀ i, List.Mem i (shell n) → Le K P (ω i) e) :
    Le K P (lsum K ω (shell n)) (K.mul (cN K 26) (K.mul (K.mul (cN K (n + 1)) (cN K (n + 1))) e)) := by
  refine le_trans' K P _ _ _ (lsum_le_len K P ω _ he (shell n) hw) ?_
  have c := cN_le K P _ _ hlen
  rw [cN_mul, cN_mul] at c
  have := Riccati.le_mul_left K P _ _ _ he c
  exact le_trans' K P _ _ _ ⟨K.zero, P.nn_zero, by lib_ring⟩ (le_trans' K P _ _ _ this ⟨K.zero, P.nn_zero, by lib_ring⟩)

theorem sumN_le (P : Cone K) (f g : Nat → R) (h : ∀ n, Le K P (f n) (g n)) : ∀ M, Le K P (sumN K f M) (sumN K g M)
  | 0 => le_refl' K P _
  | M + 1 => Riccati.le_add K P _ _ _ _ (sumN_le P f g h M) (h M)

/-- **Low shells.** -/
theorem low_weights (P : Cone K) (u : Nat → R) (hu : ∀ n, K.mul (cN K (n + 1)) (u n) = K.one)
    (shell : Nat → List I) (ω : I → R) (Kk : Nat)
    (hlen : ∀ n, (shell n).length ≤ 26 * ((n + 1) * (n + 1)))
    (hw : ∀ n i, List.Mem i (shell n) → Le K P (ω i) (K.mul (u n) (u n))) :
    Le K P (sumN K (fun n => lsum K ω (shell n)) Kk) (K.mul (cN K 26) (cN K Kk)) := by
  have s := sumN_le K P _ (fun _ => cN K 26)
    (fun n => by
      have b := shell_bound K P shell ω n _ (P.nn_sq _) (hlen n) (hw n)
      rw [show K.mul (cN K 26) (K.mul (K.mul (cN K (n + 1)) (cN K (n + 1))) (K.mul (u n) (u n)))
          = K.mul (cN K 26) (K.mul (K.mul (cN K (n + 1)) (u n)) (K.mul (cN K (n + 1)) (u n))) by lib_ring,
        hu n, show K.mul (cN K 26) (K.mul K.one K.one) = cN K 26 by lib_ring] at b
      exact b) Kk
  have c : ∀ M, sumN K (fun _ => cN K 26) M = K.mul (cN K 26) (cN K M) := by
    intro M; induction M with
    | zero => show K.zero = K.mul (cN K 26) K.zero; lib_ring
    | succ M ih => show K.add (sumN K (fun _ => cN K 26) M) (cN K 26) = K.mul (cN K 26) (K.add (cN K M) K.one); rw [ih]; lib_ring
  rw [c] at s; exact s

/-- **High shells.** -/
theorem high_weights (P : Cone K) (u : Nat → R) (hu : ∀ n, K.mul (cN K (n + 1)) (u n) = K.one)
    (shell : Nat → List I) (ω : I → R) (Kk M : Nat)
    (hlen : ∀ n, (shell n).length ≤ 26 * ((n + 1) * (n + 1)))
    (hw : ∀ n i, List.Mem i (shell n) → Le K P (ω i) (K.mul (K.mul (u n) (u n)) (K.mul (u n) (u n)))) :
    Le K P (sumN K (fun j => lsum K ω (shell (Kk + j + 1))) M) (K.mul (cN K 26) (u Kk)) := by
  have s := sumN_le K P _ (fun j => K.mul (cN K 26) (K.mul (u (Kk + j + 1)) (u (Kk + j + 1))))
    (fun j => by
      have b := shell_bound K P shell ω (Kk + j + 1) _ (P.nn_mul _ _ (P.nn_sq _) (P.nn_sq _)) (hlen _) (hw _)
      have e : K.mul (cN K 26) (K.mul (K.mul (cN K (Kk + j + 1 + 1)) (cN K (Kk + j + 1 + 1)))
            (K.mul (K.mul (u (Kk + j + 1)) (u (Kk + j + 1))) (K.mul (u (Kk + j + 1)) (u (Kk + j + 1)))))
          = K.mul (cN K 26) (K.mul (K.mul (K.mul (cN K (Kk + j + 1 + 1)) (u (Kk + j + 1)))
              (K.mul (cN K (Kk + j + 1 + 1)) (u (Kk + j + 1)))) (K.mul (u (Kk + j + 1)) (u (Kk + j + 1)))) := by lib_ring
      rw [e, hu (Kk + j + 1), show K.mul (cN K 26) (K.mul (K.mul K.one K.one) (K.mul (u (Kk + j + 1)) (u (Kk + j + 1))))
          = K.mul (cN K 26) (K.mul (u (Kk + j + 1)) (u (Kk + j + 1))) by lib_ring] at b
      exact b) M
  rw [sumN_mul_left] at s
  have t := Riccati.le_mul_left K P _ _ _ (cN_nn K P (Basel.one_nn K P) 26) (BaselTail.tail K P u hu Kk M)
  exact le_trans' K P _ _ _ s t

end AgmonHalves

#print axioms AgmonHalves.shell_bound
#print axioms AgmonHalves.sumN_le
#print axioms AgmonHalves.low_weights
#print axioms AgmonHalves.high_weights

ns/AgmonHalves2.lean

lean_all/ns/AgmonHalves2.lean

/-
  ns/AgmonHalves2.lean — T37 cycle 224 (goal G2, step 2): Agmon's two halves for the low and high mode lists.

  Lean 4 core plus ns/AgmonHalves, ns/Sobolev (sobolev), NSChain, lib/RingTac. lowL K = shells n < K, highL K M =
  shells K + 1 … K + M (concatenated). For x_i² ≤ ω_i, y_i² ≤ β_i:
    low_half    ω ≤ u_n² on shell n   ⇒  (Σ_{lowL} x y)² ≤ 26K · Σ_{lowL} β        (x = |k|^{-1}, y = |k||û|: Z)
    high_half   ω ≤ u_n⁴ on shell n   ⇒  (Σ_{highL} x y)² ≤ 26u_K · Σ_{highL} β     (x = |k|^{-2}, y = |k|²|û|: P)
  i.e. X_L² ≤ 26K·Z and X_H² ≤ 26u_K·P — the hypotheses hL, hH of LocalContSobolev (in the Cone order).
-/
import ns.AgmonHalves

namespace AgmonHalves2
open DetChain NSChain LibRing Basel Sobolev AgmonHalves

variable {R : Type} (K : CRing R) {I : Type}

def lowL (shell : Nat → List I) : Nat → List I
  | 0 => []
  | n + 1 => lowL shell n ++ shell n

def highL (shell : Nat → List I) (Kk : Nat) : Nat → List I
  | 0 => []
  | j + 1 => highL shell Kk j ++ shell (Kk + j + 1)

theorem lsum_append (f : I → R) : ∀ l1 l2 : List I, lsum K f (l1 ++ l2) = K.add (lsum K f l1) (lsum K f l2)
  | [], l2 => by show lsum K f l2 = K.add K.zero (lsum K f l2); lib_ring
  | x :: l1, l2 => by
    show K.add (f x) (lsum K f (l1 ++ l2)) = K.add (K.add (f x) (lsum K f l1)) (lsum K f l2)
    rw [lsum_append f l1 l2]; lib_ring

theorem lsum_lowL (f : I → R) (shell : Nat → List I) : ∀ n,
    lsum K f (lowL shell n) = sumN K (fun m => lsum K f (shell m)) n
  | 0 => rfl
  | n + 1 => by
    show lsum K f (lowL shell n ++ shell n) = K.add (sumN K (fun m => lsum K f (shell m)) n) (lsum K f (shell n))
    rw [lsum_append, lsum_lowL f shell n]

theorem lsum_highL (f : I → R) (shell : Nat → List I) (Kk : Nat) : ∀ j,
    lsum K f (highL shell Kk j) = sumN K (fun m => lsum K f (shell (Kk + m + 1))) j
  | 0 => rfl
  | j + 1 => by
    show lsum K f (highL shell Kk j ++ shell (Kk + j + 1))
      = K.add (sumN K (fun m => lsum K f (shell (Kk + m + 1))) j) (lsum K f (shell (Kk + j + 1)))
    rw [lsum_append, lsum_highL f shell Kk j]

/-- **Low half.** -/
theorem low_half (P : Cone K) (u : Nat → R) (hu : ∀ n, K.mul (cN K (n + 1)) (u n) = K.one)
    (shell : Nat → List I) (x y ω β : I → R) (Kk : Nat)
    (hlen : ∀ n, (shell n).length ≤ 26 * ((n + 1) * (n + 1)))
    (hw : ∀ n i, List.Mem i (shell n) → Le K P (ω i) (K.mul (u n) (u n)))
    (hx : ∀ i, Le K P (SumCS.sq K (x i)) (ω i)) (hy : ∀ i, Le K P (SumCS.sq K (y i)) (β i)) (hβ : ∀ i, P.nn (β i)) :
    Le K P (SumCS.sq K (lsum K (fun i => K.mul (x i) (y i)) (lowL shell Kk)))
      (K.mul (K.mul (cN K 26) (cN K Kk)) (lsum K β (lowL shell Kk))) := by
  apply sobolev K P x y ω β _ _ hx hy hβ
  rw [lsum_lowL]
  exact low_weights K P u hu shell ω Kk hlen hw

/-- **High half.** -/
theorem high_half (P : Cone K) (u : Nat → R) (hu : ∀ n, K.mul (cN K (n + 1)) (u n) = K.one)
    (shell : Nat → List I) (x y ω β : I → R) (Kk M : Nat)
    (hlen : ∀ n, (shell n).length ≤ 26 * ((n + 1) * (n + 1)))
    (hw : ∀ n i, List.Mem i (shell n) → Le K P (ω i) (K.mul (K.mul (u n) (u n)) (K.mul (u n) (u n))))
    (hx : ∀ i, Le K P (SumCS.sq K (x i)) (ω i)) (hy : ∀ i, Le K P (SumCS.sq K (y i)) (β i)) (hβ : ∀ i, P.nn (β i)) :
    Le K P (SumCS.sq K (lsum K (fun i => K.mul (x i) (y i)) (highL shell Kk M)))
      (K.mul (K.mul (cN K 26) (u Kk)) (lsum K β (highL shell Kk M))) := by
  apply sobolev K P x y ω β _ _ hx hy hβ
  rw [lsum_highL]
  exact high_weights K P u hu shell ω Kk M hlen hw

end AgmonHalves2

#print axioms AgmonHalves2.lsum_append
#print axioms AgmonHalves2.lsum_lowL
#print axioms AgmonHalves2.lsum_highL
#print axioms AgmonHalves2.low_half
#print axioms AgmonHalves2.high_half

ns/AgmonHalves3.lean

lean_all/ns/AgmonHalves3.lean

/-
  ns/AgmonHalves3.lean — T37 cycle 227 (goal G2, step 3): the hypotheses hL, hH of LocalContSobolev from the lattice.

  Lean 4 core plus ns/AgmonHalves2, ns/LocalContSobolev (c26), ns/LocalContAgmon (toCone, le_of_Le, Le_of_le). For a
  positivity cone P on R (so that both orders apply), cut K with k = cN K, w = u_K ((k + 1)w = 1), and
  Σ_low β_Z ≤ Z, Σ_high β_P ≤ P (the low/high parts of the enstrophy and of ‖Δu‖²):
    hL_of_lattice   (Σ_low x y)² ≤ 26 k Z          (x = |k|^{-1}, y = |k||û|)
    hH_of_lattice   (Σ_high x y)² ≤ 26 w P         (x = |k|^{-2}, y = |k|²|û|)
  exactly the per-step inputs hL, hH of local_cont_sobolev (with X_L, X_H the two partial Wiener sums).
-/
import ns.AgmonHalves2
import ns.LocalContSobolev

namespace AgmonHalves3
open DetChain NSChain LibRing LibCS LibGron Basel AgmonHalves2 LocalContAgmon LocalContSobolev

variable {R : Type} (K : CRing R) {I : Type}

theorem c26_eq : c26 K = cN K 26 := by
  unfold c26
  have h : ∀ n, castN K n = cN K n := by
    intro n; induction n with
    | zero => rfl
    | succ n ih => show K.add K.one (castN K n) = K.add (cN K n) K.one; rw [ih]; lib_ring
  exact h 26

theorem hL_of_lattice (P : Pos K) (u : Nat → R) (hu : ∀ n, K.mul (cN K (n + 1)) (u n) = K.one)
    (shell : Nat → List I) (x y ω β : I → R) (Kk : Nat) (Z : R)
    (hlen : ∀ n, (shell n).length ≤ 26 * ((n + 1) * (n + 1)))
    (hw : ∀ n i, List.Mem i (shell n) → Le K (toCone K P) (ω i) (K.mul (u n) (u n)))
    (hx : ∀ i, le K P (K.mul (x i) (x i)) (ω i)) (hy : ∀ i, le K P (K.mul (y i) (y i)) (β i)) (hβ : ∀ i, P.nn (β i))
    (hZ : le K P (lsum K β (lowL shell Kk)) Z) :
    le K P (K.mul (lsum K (fun i => K.mul (x i) (y i)) (lowL shell Kk)) (lsum K (fun i => K.mul (x i) (y i)) (lowL shell Kk)))
      (K.mul (K.mul (c26 K) (cN K Kk)) Z) := by
  have l := le_of_Le K P _ _ (low_half K (toCone K P) u hu shell x y ω β Kk hlen hw
    (fun i => Le_of_le K P _ _ (hx i)) (fun i => Le_of_le K P _ _ (hy i)) hβ)
  have m := le_mul_left K P (P.nn_mul _ _ (Basel.cN_nn K (toCone K P) (Basel.one_nn K (toCone K P)) 26)
    (Basel.cN_nn K (toCone K P) (Basel.one_nn K (toCone K P)) Kk)) hZ
  rw [c26_eq]
  exact le_trans K P l m

theorem hH_of_lattice (P : Pos K) (u : Nat → R) (hu : ∀ n, K.mul (cN K (n + 1)) (u n) = K.one)
    (shell : Nat → List I) (x y ω β : I → R) (Kk M : Nat) (Pp : R)
    (hlen : ∀ n, (shell n).length ≤ 26 * ((n + 1) * (n + 1)))
    (hw : ∀ n i, List.Mem i (shell n) → Le K (toCone K P) (ω i) (K.mul (K.mul (u n) (u n)) (K.mul (u n) (u n))))
    (hx : ∀ i, le K P (K.mul (x i) (x i)) (ω i)) (hy : ∀ i, le K P (K.mul (y i) (y i)) (β i)) (hβ : ∀ i, P.nn (β i))
    (hP : le K P (lsum K β (highL shell Kk M)) Pp) :
    le K P (K.mul (lsum K (fun i => K.mul (x i) (y i)) (highL shell Kk M)) (lsum K (fun i => K.mul (x i) (y i)) (highL shell Kk M)))
      (K.mul (K.mul (c26 K) (u Kk)) Pp) := by
  have l := le_of_Le K P _ _ (high_half K (toCone K P) u hu shell x y ω β Kk M hlen hw
    (fun i => Le_of_le K P _ _ (hx i)) (fun i => Le_of_le K P _ _ (hy i)) hβ)
  have m := le_mul_left K P (P.nn_mul _ _ (Basel.cN_nn K (toCone K P) (Basel.one_nn K (toCone K P)) 26)
    (Basel.u_nn K (toCone K P) u hu Kk)) hP
  rw [c26_eq]
  exact le_trans K P l m

end AgmonHalves3

#print axioms AgmonHalves3.c26_eq
#print axioms AgmonHalves3.hL_of_lattice
#print axioms AgmonHalves3.hH_of_lattice

ns/AgmonHalves4.lean

lean_all/ns/AgmonHalves4.lean

/-
  ns/AgmonHalves4.lean — T37 cycle 230 (goal G2b): the boundary shell K in the high half, and local continuation
  with every mode accounted for.

  Lean 4 core plus ns/AgmonHalves3, ns/LocalContSobolev, ns/LocalContAgmon, lib/RingTac.
    u_sq_le         u_K² ≤ u_K                               (u_K = 1/(K + 1) ≤ 1)
    high_weights0   ω ≤ u_n⁴ on shells n ≥ K  ⇒  Σ_{j<M} Σ_{shell K+j} ω ≤ 26(u_K + u_K)
    split_52_104    X_L² ≤ 26kZ, X_H² ≤ 26(w + w)P  ⇒  X² ≤ 52kZ + 104Pw
    local_cont_lattice   local_cont_agmon with a = 52, b = 104 and the two halves as hypotheses: low shells
                    n < K, high shells n ≥ K — the split K covers every mode.
-/
import ns.AgmonHalves3

namespace AgmonHalves4
open DetChain NSChain LibRing LibCS LibGron LibOF Basel AgmonHalves AgmonHalves2 LocalContAgmon LocalContSobolev
  SerrinDiscrete SerrinSum SerrinRun SerrinAgmon LocalCont

variable {R : Type} (K : CRing R) {I : Type}

theorem u_sq_le (P : Cone K) (u : Nat → R) (hu : ∀ n, K.mul (cN K (n + 1)) (u n) = K.one) (n : Nat) :
    Le K P (K.mul (u n) (u n)) (u n) := by
  -- u − u² = u·(1 − u) = u·(cN n · u) since (cN n + 1) u = 1
  refine ⟨K.mul (cN K n) (K.mul (u n) (u n)), P.nn_mul _ _ (cN_nn K P (one_nn K P) n) (P.nn_sq _), ?_⟩
  have h := hu n
  have c : cN K (n + 1) = K.add (cN K n) K.one := rfl
  rw [c] at h
  calc K.add (K.mul (u n) (u n)) (K.mul (cN K n) (K.mul (u n) (u n)))
      = K.mul (u n) (K.mul (K.add (cN K n) K.one) (u n)) := by lib_ring
    _ = u n := by rw [h]; lib_ring

/-- **High shells from K.** -/
theorem high_weights0 (P : Cone K) (u : Nat → R) (hu : ∀ n, K.mul (cN K (n + 1)) (u n) = K.one)
    (shell : Nat → List I) (ω : I → R) (Kk M : Nat)
    (hlen : ∀ n, (shell n).length ≤ 26 * ((n + 1) * (n + 1)))
    (hw : ∀ n i, List.Mem i (shell n) → Le K P (ω i) (K.mul (K.mul (u n) (u n)) (K.mul (u n) (u n)))) :
    Le K P (sumN K (fun j => lsum K ω (shell (Kk + j))) (M + 1)) (K.mul (cN K 26) (K.add (u Kk) (u Kk))) := by
  -- shell Kk alone ≤ 26 u_K², the rest is high_weights
  have b0 := shell_bound K P shell ω Kk _ (P.nn_mul _ _ (P.nn_sq _) (P.nn_sq _)) (hlen Kk) (hw Kk)
  rw [show K.mul (cN K 26) (K.mul (K.mul (cN K (Kk + 1)) (cN K (Kk + 1)))
        (K.mul (K.mul (u Kk) (u Kk)) (K.mul (u Kk) (u Kk))))
      = K.mul (cN K 26) (K.mul (K.mul (K.mul (cN K (Kk + 1)) (u Kk)) (K.mul (cN K (Kk + 1)) (u Kk))) (K.mul (u Kk) (u Kk)))
      by lib_ring, hu Kk] at b0
  have rest := high_weights K P u hu shell ω Kk M hlen hw
  -- Σ_{j<M+1} f(Kk + j) = f(Kk) + Σ_{j<M} f(Kk + j + 1)
  have shift : ∀ M, sumN K (fun j => lsum K ω (shell (Kk + j))) (M + 1)
      = K.add (lsum K ω (shell Kk)) (sumN K (fun j => lsum K ω (shell (Kk + j + 1))) M) := by
    intro M; induction M with
    | zero => show K.add K.zero (lsum K ω (shell (Kk + 0))) = K.add (lsum K ω (shell Kk)) K.zero; rw [Nat.add_zero]; lib_ring
    | succ M ih =>
      show K.add (sumN K (fun j => lsum K ω (shell (Kk + j))) (M + 1)) (lsum K ω (shell (Kk + (M + 1))))
        = K.add (lsum K ω (shell Kk)) (K.add (sumN K (fun j => lsum K ω (shell (Kk + j + 1))) M) (lsum K ω (shell (Kk + M + 1))))
      rw [ih, show Kk + (M + 1) = Kk + M + 1 from rfl]; lib_ring
  rw [shift]
  have s := Riccati.le_add K P _ _ _ _ b0 rest
  have uu := Riccati.le_mul_left K P (cN K 26) _ _ (cN_nn K P (one_nn K P) 26) (u_sq_le K P u hu Kk)
  refine le_trans' K P _ _ _ s ?_
  refine le_trans' K P _ _ _ (Riccati.le_add K P _ _ _ _ (le_trans' K P _ _ _ ⟨K.zero, P.nn_zero, by lib_ring⟩ uu)
    (le_refl' K P (K.mul (cN K 26) (u Kk)))) ⟨K.zero, P.nn_zero, by lib_ring⟩

/-- **The split with every mode.** -/
theorem split_52_104 (P : Pos K) (XL XH k w Z Pp : R)
    (hL : le K P (K.mul XL XL) (K.mul (K.mul (c26 K) k) Z))
    (hH : le K P (K.mul XH XH) (K.mul (K.mul (c26 K) (K.add w w)) Pp)) :
    le K P (K.mul (K.add XL XH) (K.add XL XH))
      (K.add (K.mul (c52 K) (K.mul k Z)) (K.mul (K.add (c52 K) (c52 K)) (K.mul Pp w))) := by
  have a := Agmon.agmon K (toCone K P) XL XH (K.mul (c26 K) k) (K.mul (c26 K) (K.add w w)) Z Pp
    (Le_of_le K P _ _ (by show le K P (K.mul XL XL) _; exact hL))
    (Le_of_le K P _ _ (by show le K P (K.mul XH XH) _; exact hH))
  have l := le_of_Le K P _ _ a
  unfold le at l ⊢
  rw [c52_eq]
  have e : sub K (K.add (K.mul (K.add (c26 K) (c26 K)) (K.mul k Z))
        (K.mul (K.add (K.add (c26 K) (c26 K)) (K.add (c26 K) (c26 K))) (K.mul Pp w)))
        (K.mul (K.add XL XH) (K.add XL XH))
      = sub K (K.add (K.add (K.mul (K.mul (c26 K) k) Z) (K.mul (K.mul (c26 K) k) Z))
          (K.add (K.mul (K.mul (c26 K) (K.add w w)) Pp) (K.mul (K.mul (c26 K) (K.add w w)) Pp)))
          (SumCS.sq K (K.add XL XH)) := by unfold SumCS.sq; lib_ring
  rw [e]; exact l

/-- **Local continuation, every mode in one of the halves.** -/
theorem local_cont_lattice (P : Pos K) (F : OF K P) (Z r s XL XH kc w : Nat → R) (h c ν M q q1 : R)
    (hh : P.nn h) (hν : P.nn ν)
    (hν0 : K.add ν ν ≠ K.zero) (hhν : K.mul h ν ≠ K.zero) (hr : ∀ k, K.mul (r k) (r k) = Z k)
    (step : ∀ k, le K P (Z (k + 1))
      (K.add (Z k) (K.mul h (sub K (K.mul (K.mul (K.mul c (K.add (XL k) (XH k))) (r k)) (s k))
        (K.mul ν (K.mul (s k) (s k)))))))
    (hM : P.nn M)
    (hkw : ∀ k, K.mul (K.add (kc k) K.one) (w k) = K.one)
    (h1 : ∀ k, le K P (K.mul (K.mul (kc k) (kc k)) (Z k)) (K.mul (s k) (s k)))
    (h2 : ∀ k, le K P (K.mul (s k) (s k)) (K.mul (K.mul (K.add (kc k) K.one) (K.add (kc k) K.one)) (Z k)))
    (hL : ∀ k, le K P (K.mul (XL k) (XL k)) (K.mul (K.mul (c26 K) (kc k)) (Z k)))
    (hH : ∀ k, le K P (K.mul (XH k) (XH k)) (K.mul (K.mul (c26 K) (K.add (w k) (w k))) (K.mul (s k) (s k))))
    (hq : P.nn q) (hq0 : q ≠ K.zero) (hq1 : le K P q1 K.one) (hq10 : sub K K.one q1 ≠ K.zero)
    (hM0 : le K P (Z 0) (K.mul M (sub K K.one q1))) (Nmax : Nat)
    (hα : ∀ N, N ≤ Nmax → le K P (alpha K P F h ν
      (K.add (K.add (K.mul (c52 K) (c52 K)) (K.mul (c52 K) (c52 K)))
        (K.add (K.mul (K.add (c52 K) (c52 K)) (K.add (c52 K) (c52 K))) (K.mul (K.add (c52 K) (c52 K)) (K.add (c52 K) (c52 K)))))
      M (Z 0) N) (K.mul q q))
    (hβ : ∀ N, N ≤ Nmax → le K P (K.mul (amp K P F h c ν)
      (K.add (beta K P F h c ν
        (K.add (K.add (K.mul (c52 K) (c52 K)) (K.mul (c52 K) (c52 K)))
          (K.add (K.mul (K.add (c52 K) (c52 K)) (K.add (c52 K) (c52 K))) (K.mul (K.add (c52 K) (c52 K)) (K.add (c52 K) (c52 K)))))
        M N) q)) q1) :
    ∀ N, N ≤ Nmax → le K P (Z N) M :=
  local_cont_agmon K P F Z r s (fun k => K.add (XL k) (XH k)) kc w h c ν (c52 K) (K.add (c52 K) (c52 K)) M q q1
    hh hν hν0 hhν hr step hM hkw h1 h2
    (fun k => split_52_104 K P (XL k) (XH k) (kc k) (w k) (Z k) (K.mul (s k) (s k)) (hL k) (hH k))
    hq hq0 hq1 hq10 hM0 Nmax hα hβ

end AgmonHalves4

#print axioms AgmonHalves4.u_sq_le
#print axioms AgmonHalves4.high_weights0
#print axioms AgmonHalves4.split_52_104
#print axioms AgmonHalves4.local_cont_lattice

ns/AgmonOpt.lean

lean_all/ns/AgmonOpt.lean

/-
  ns/AgmonOpt.lean — T37 cycle 139: the optimised Agmon inequality, square-root free.

  Lean 4 core plus ns/Agmon, ns/Riccati (le_sq, le_add, le_mul_left), lib/RingTac. If X² ≤ a·k·Z + b·P·w with
  (k + 1)w = 1, and k is chosen with k²Z ≤ P ≤ (k + 1)²Z (k ≈ √(P/Z)), then
      (X²)² ≤ 2(a² + b²) · Z · P          i.e. ‖u‖_∞⁴ ≤ C ‖∇u‖² ‖Δu‖²   (Z = ‖∇u‖², P = ‖Δu‖²)
-/
import ns.Agmon

namespace AgmonOpt
open DetChain NSChain LibRing SumCS

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

theorem opt (P : Cone K) (X a b k w Z Pp : R) (hX : P.nn (sq K X))
    (hZ : P.nn Z) (hP : P.nn Pp)
    (hkw : K.mul (K.add k K.one) w = K.one)
    (h1 : Le K P (K.mul (K.mul k k) Z) Pp) (h2 : Le K P Pp (K.mul (K.mul (K.add k K.one) (K.add k K.one)) Z))
    (hXb : Le K P (sq K X) (K.add (K.mul a (K.mul k Z)) (K.mul b (K.mul Pp w)))) :
    Le K P (K.mul (sq K X) (sq K X))
      (K.add (K.add (K.mul (K.mul a a) (K.mul Z Pp)) (K.mul (K.mul a a) (K.mul Z Pp)))
             (K.add (K.mul (K.mul b b) (K.mul Z Pp)) (K.mul (K.mul b b) (K.mul Z Pp)))) := by
  -- (X²)² ≤ (A + B)² ≤ 2A² + 2B²
  have s1 := Riccati.le_sq K P _ _ hX hXb
  have s2 := Agmon.sq_add_le K P (K.mul a (K.mul k Z)) (K.mul b (K.mul Pp w))
  -- A² = a²k²Z² ≤ a² Z P
  have hA : Le K P (sq K (K.mul a (K.mul k Z))) (K.mul (K.mul a a) (K.mul Z Pp)) := by
    have m := Riccati.le_mul_left K P (K.mul (K.mul a a) Z) _ _ (P.nn_mul _ _ (P.nn_sq a) hZ) h1
    have e : sq K (K.mul a (K.mul k Z)) = K.mul (K.mul (K.mul a a) Z) (K.mul (K.mul k k) Z) := by unfold sq; lib_ring
    rw [e]; refine le_trans' K P _ _ _ m ⟨K.zero, P.nn_zero, by lib_ring⟩
  -- B² = b²P²w² ≤ b² Z P   since P w² ≤ (k+1)² Z w² = Z
  have hB : Le K P (sq K (K.mul b (K.mul Pp w))) (K.mul (K.mul b b) (K.mul Z Pp)) := by
    have m := Riccati.le_mul_left K P (K.mul (K.mul (K.mul b b) Pp) (K.mul w w)) _ _
      (P.nn_mul _ _ (P.nn_mul _ _ (P.nn_sq b) hP) (P.nn_sq w)) h2
    have e : sq K (K.mul b (K.mul Pp w)) = K.mul (K.mul (K.mul (K.mul b b) Pp) (K.mul w w)) Pp := by unfold sq; lib_ring
    have e2 : K.mul (K.mul (K.mul (K.mul b b) Pp) (K.mul w w)) (K.mul (K.mul (K.add k K.one) (K.add k K.one)) Z)
        = K.mul (K.mul b b) (K.mul Z Pp) := by
      rw [show K.mul (K.mul (K.mul (K.mul b b) Pp) (K.mul w w)) (K.mul (K.mul (K.add k K.one) (K.add k K.one)) Z)
          = K.mul (K.mul (K.mul b b) (K.mul Z Pp)) (K.mul (K.mul (K.add k K.one) w) (K.mul (K.add k K.one) w)) by lib_ring,
        hkw]; lib_ring
    rw [e, ← e2]; exact m
  refine le_trans' K P _ _ _ s1 (le_trans' K P _ _ _ s2 ?_)
  exact Riccati.le_add K P _ _ _ _ (Riccati.le_add K P _ _ _ _ hA hA) (Riccati.le_add K P _ _ _ _ hB hB)

end AgmonOpt

#print axioms AgmonOpt.opt

ns/AgmonTime.lean

lean_all/ns/AgmonTime.lean

/-
  ns/AgmonTime.lean — T37 cycle 161: the time sum of ‖u‖²_∞ from Agmon and Cauchy–Schwarz, square-root free.

  Lean 4 core plus ns/SumCS (cs_sum), ns/Riccati (le_add, le_mul_left), NSChain, lib/RingTac. Over the steps
  l of a run, with X_k⁴ ≤ C Z_k P_k (AgmonOpt) and Z_k ≤ M:
      (Σ_k X_k²)² ≤ (Σ_k 1) · C · M · Σ_k P_k
  i.e. ∫‖u‖²_∞ ≤ (T · C · sup Z · ∫‖Δu‖²)^{1/2}: the budget S of SerrinRun is controlled by sup Z and the
  time integral of ‖Δu‖² — the latter is exactly what the energy does not give (R1-gap, discrete form).
-/
import ns.SumCS
import ns.Riccati

namespace AgmonTime
open DetChain NSChain LibRing SumCS

variable {R : Type} (K : CRing R) {I : Type}

theorem lsum_le (P : Cone K) (u v : I → R) (h : ∀ i, Le K P (u i) (v i)) :
    ∀ l : List I, Le K P (lsum K u l) (lsum K v l)
  | [] => le_refl' K P _
  | i :: l => Riccati.le_add K P _ _ _ _ (h i) (lsum_le P u v h l)

theorem lsum_nn (P : Cone K) (u : I → R) (h : ∀ i, P.nn (u i)) : ∀ l : List I, P.nn (lsum K u l)
  | [] => P.nn_zero
  | i :: l => P.nn_add _ _ (h i) (lsum_nn P u h l)

/-- **Time sum of ‖u‖²_∞.** -/
theorem agmon_time (P : Cone K) (X Z Pp : I → R) (C M : R) (hC : P.nn C) (hP : ∀ i, P.nn (Pp i))
    (hA : ∀ i, Le K P (sq K (sq K (X i))) (K.mul C (K.mul (Z i) (Pp i))))
    (hZ : ∀ i, Le K P (Z i) M) (l : List I) :
    Le K P (sq K (lsum K (fun i => sq K (X i)) l))
      (K.mul (lsum K (fun _ => sq K K.one) l) (K.mul (K.mul C M) (lsum K Pp l))) := by
  -- (Σ X²·1)² ≤ (Σ (X²)²)(Σ 1²)
  have cs := cs_sum K P (fun i => sq K (X i)) (fun _ => K.one) l
  rw [lsum_ext K (fun i => K.mul (sq K (X i)) K.one) (fun i => sq K (X i)) (fun i => by lib_ring) l] at cs
  -- Σ (X²)² ≤ Σ C Z P ≤ Σ C M P = C M Σ P
  have s1 := lsum_le K P _ _ hA l
  have s2 := lsum_le K P (fun i => K.mul C (K.mul (Z i) (Pp i))) (fun i => K.mul (K.mul C M) (Pp i)) (fun i => by
    have m := Riccati.le_mul_left K P (K.mul C (Pp i)) _ _ (P.nn_mul _ _ hC (hP i)) (hZ i)
    rw [show K.mul (K.mul C (Pp i)) (Z i) = K.mul C (K.mul (Z i) (Pp i)) by lib_ring,
        show K.mul (K.mul C (Pp i)) M = K.mul (K.mul C M) (Pp i) by lib_ring] at m
    exact m) l
  rw [lsum_mul_left K (K.mul C M) Pp l] at s2
  have s3 := le_trans' K P _ _ _ s1 s2
  have s4 := Riccati.le_mul_left K P (lsum K (fun _ => sq K K.one) l) _ _
    (lsum_nn K P _ (fun _ => P.nn_sq K.one) l) s3
  rw [show K.mul (lsum K (fun _ => sq K K.one) l) (lsum K (fun i => sq K (sq K (X i))) l)
      = K.mul (lsum K (fun i => sq K (sq K (X i))) l) (lsum K (fun _ => sq K K.one) l) by lib_ring] at s4
  exact le_trans' K P _ _ _ cs s4

end AgmonTime

#print axioms AgmonTime.lsum_le
#print axioms AgmonTime.lsum_nn
#print axioms AgmonTime.agmon_time

ns/BDecay.lean

lean_all/ns/BDecay.lean

/-
  ns/BDecay.lean — T37 cycle 767 (goal G478): the good-time midpoint bounds of GoodState decay exactly by 4 per
  shell: B(n+1) = 4·B(n+2), hence 4ⁿ·B(n+1) = B(1).

  Lean 4 core plus ns/GoodState (X, Bf), ns/GoodModesQ (mul_ne, cast_succ_ne, pw_ne), lib/RingTac.
    inv_unique   x·y = 1, x·z = 1 ⇒ y = z
    x_succ       X(n+1) = X(n)·4
    b_step       4·B(n+2) = B(n+1)
    b_pow        4ⁿ·B(n+1) = B(1)
-/
import ns.GoodState

namespace BDecay
open DetChain NSChain DyadicSmallGlobal LibRing RatModel GoodModesQ GoodState

section Gen
variable {R : Type} (K : CRing R)

theorem inv_unique (x y z : R) (hy : K.mul x y = K.one) (hz : K.mul x z = K.one) : y = z := by
  have : y = K.mul (K.mul x z) y := by rw [hz]; lib_ring
  rw [this, show K.mul (K.mul x z) y = K.mul (K.mul x y) z by lib_ring, hy]; lib_ring

theorem x_succ (h : R) (T n : Nat) : X K h T (n + 1) = K.mul (X K h T n) (castN K 4) := by
  unfold X
  show K.mul (castN K (T + 1)) (K.mul h (K.mul (castN K 3) (K.mul (P4 K (n + 1)) (castN K 4))))
    = K.mul (K.mul (castN K (T + 1)) (K.mul h (K.mul (castN K 3) (P4 K (n + 1))))) (castN K 4)
  lib_ring

theorem b_step (O : OrdR K) (inv : R → R) (mul_inv : ∀ x, x ≠ K.zero → K.mul x (inv x) = K.one)
    (h10 : K.one ≠ K.zero) (E h : R) (hh0 : h ≠ K.zero) (T n : Nat) :
    K.mul (castN K 4) (Bf K inv E h T (n + 2)) = Bf K inv E h T (n + 1) := by
  have hX0 : ∀ i, X K h T i ≠ K.zero := fun i =>
    mul_ne K O _ _ (cast_succ_ne K O h10 T) (mul_ne K O _ _ hh0 (mul_ne K O _ _ (cast_succ_ne K O h10 2)
      (pw_ne K O h10 _ (cast_succ_ne K O h10 3) (i + 1))))
  show K.mul (castN K 4) (K.mul E (inv (X K h T (n + 1)))) = K.mul E (inv (X K h T n))
  have h1 := mul_inv _ (hX0 (n + 1))
  rw [x_succ] at h1
  have h2 : K.mul (X K h T n) (K.mul (castN K 4) (inv (K.mul (X K h T n) (castN K 4)))) = K.one := by
    rw [← h1]; lib_ring
  have e := inv_unique K _ _ _ h2 (mul_inv _ (hX0 n))
  rw [x_succ, ← e]; lib_ring

theorem b_pow (O : OrdR K) (inv : R → R) (mul_inv : ∀ x, x ≠ K.zero → K.mul x (inv x) = K.one)
    (h10 : K.one ≠ K.zero) (E h : R) (hh0 : h ≠ K.zero) (T : Nat) :
    ∀ n, K.mul (P4 K n) (Bf K inv E h T (n + 1)) = Bf K inv E h T 1
  | 0 => by show K.mul K.one (Bf K inv E h T 1) = Bf K inv E h T 1; lib_ring
  | n + 1 => by
    have ih := b_pow O inv mul_inv h10 E h hh0 T n
    show K.mul (K.mul (P4 K n) (castN K 4)) (Bf K inv E h T (n + 2)) = Bf K inv E h T 1
    rw [show K.mul (K.mul (P4 K n) (castN K 4)) (Bf K inv E h T (n + 2))
        = K.mul (P4 K n) (K.mul (castN K 4) (Bf K inv E h T (n + 2))) by lib_ring,
      b_step K O inv mul_inv h10 E h hh0 T n, ih]

end Gen

end BDecay

#print axioms BDecay.inv_unique
#print axioms BDecay.x_succ
#print axioms BDecay.b_step
#print axioms BDecay.b_pow

ns/BKMDiscrete.lean

lean_all/ns/BKMDiscrete.lean

/-
  ns/BKMDiscrete.lean — T37 cycle 86: a Beale–Kato–Majda-type discrete closure.

  Lean 4 core plus the shared lib/OField (young), lib/Gronwall (gronwall_hom, le arithmetic). In an
  ordered field, a nonnegative enstrophy sequence with
      z_{k+1} ≤ z_k − hν p_k + h·2x_k y_k,     x_k² ≤ K z_k,  y_k² ≤ p_k
  (the stretching term written as 2xy with x controlled by a velocity-gradient bound K = ‖∇u‖²_∞-type and
  y² by the palinstrophy p) satisfies
      z_{k+1} ≤ (1 + hK/ν) z_k,   z_k ≤ (1 + hK/ν)^k z_0       (no blowup while K is bounded)
    step_bound   Young 2xy ≤ νy² + x²/ν absorbs the stretching into the dissipation
    bkm          the exponential bound by the shared Grönwall
-/
import lib.OField

namespace BKMDiscrete
open DetChain LibRing LibCS LibGron LibOF

variable {R : Type} (K : CRing R) (P : Pos K) (F : OF K P)

theorem step_bound (z z' p x y h nu Kc : R) (hh : P.nn h) (hnu : P.nn nu) (hnu0 : nu ≠ K.zero)
    (step : le K P z' (K.add (sub K z (K.mul h (K.mul nu p))) (K.mul h (K.mul (castZ K 2) (K.mul x y)))))
    (hx : le K P (K.mul x x) (K.mul Kc z)) (hy : le K P (K.mul y y) p) :
    le K P z' (K.mul (K.add K.one (K.mul h (K.mul Kc (F.inv nu)))) z) := by
  -- 2xy = 2yx ≤ ν y² + x² ν⁻¹
  have yg := young K P F nu y x hnu hnu0
  have comm : K.mul (castZ K 2) (K.mul x y) = K.mul (castZ K 2) (K.mul y x) := by lib_ring
  have inv_nn : P.nn (F.inv nu) := nn_inv K P F nu hnu hnu0
  -- ν y² ≤ ν p,  x² ν⁻¹ ≤ K z ν⁻¹
  have t1 : le K P (K.mul nu (K.mul y y)) (K.mul nu p) := le_mul_left K P hnu hy
  have t2 : le K P (K.mul (K.mul x x) (F.inv nu)) (K.mul (K.mul Kc z) (F.inv nu)) := by
    have := le_mul_left K P inv_nn hx
    rw [show K.mul (F.inv nu) (K.mul x x) = K.mul (K.mul x x) (F.inv nu) by lib_ring,
      show K.mul (F.inv nu) (K.mul Kc z) = K.mul (K.mul Kc z) (F.inv nu) by lib_ring] at this
    exact this
  have s : le K P (K.mul (castZ K 2) (K.mul x y)) (K.add (K.mul nu p) (K.mul (K.mul Kc z) (F.inv nu))) := by
    rw [comm]; exact le_trans K P yg (le_add2 K P t1 t2)
  have s2 := le_mul_left K P hh s
  have s3 := le_add K P (sub K z (K.mul h (K.mul nu p))) s2
  refine le_trans K P step ?_
  rw [show K.add (sub K z (K.mul h (K.mul nu p))) (K.mul h (K.mul (castZ K 2) (K.mul x y)))
      = K.add (K.mul h (K.mul (castZ K 2) (K.mul x y))) (sub K z (K.mul h (K.mul nu p))) by lib_ring]
  refine le_trans K P s3 ?_
  rw [show K.add (K.mul h (K.add (K.mul nu p) (K.mul (K.mul Kc z) (F.inv nu)))) (sub K z (K.mul h (K.mul nu p)))
      = K.mul (K.add K.one (K.mul h (K.mul Kc (F.inv nu)))) z by lib_ring]
  exact le_refl K P _

/-- **BKM-type bound**: z_k ≤ (1 + hK/ν)^k z_0. -/
theorem bkm (z p x y : Nat → R) (h nu Kc : R) (hh : P.nn h) (hnu : P.nn nu) (hnu0 : nu ≠ K.zero) (hK : P.nn Kc)
    (step : ∀ k, le K P (z (k + 1)) (K.add (sub K (z k) (K.mul h (K.mul nu (p k))))
      (K.mul h (K.mul (castZ K 2) (K.mul (x k) (y k))))))
    (hx : ∀ k, le K P (K.mul (x k) (x k)) (K.mul Kc (z k))) (hy : ∀ k, le K P (K.mul (y k) (y k)) (p k)) (n : Nat) :
    le K P (z n) (K.mul (pw K (K.add K.one (K.mul h (K.mul Kc (F.inv nu)))) n) (z 0)) :=
  gronwall_hom K P z _ (P.nn_add _ _ (nn_one K P) (P.nn_mul _ _ hh (P.nn_mul _ _ hK (nn_inv K P F nu hnu hnu0))))
    (fun k => step_bound K P F (z k) (z (k + 1)) (p k) (x k) (y k) h nu Kc hh hnu hnu0 (step k) (hx k) (hy k)) n

end BKMDiscrete

#print axioms BKMDiscrete.step_bound
#print axioms BKMDiscrete.bkm

ns/BadCount.lean

lean_all/ns/BadCount.lean

/-
  ns/BadCount.lean — T37 (goal G859c2-s1): the discrete form of Leray's structure statement — the number of time
  steps with large enstrophy is bounded by the data, with no dependence on the truncation.
    ind, countB        indicator of a Boolean flag and the number of flagged steps s < n
    le_of_eq''         equality gives ≤
    bad_sum            flagged steps have c ≤ Y_s, all Y_s ≥ 0  ⇒  #flagged·c ≤ Σ_{s<n} Y_s
    bad_count          the same with Σ_{s<n} Y_s ≤ S  ⇒  #flagged·c ≤ S
    count_split        #flagged + #unflagged = n
    diss_total         under the cfl_global hypotheses: Σ_{t ≤ n} hν·Z_{t+1} ≤ E_0 + hν·Z_0
    leray_bad_count    every step t ≤ n flagged only when b ≤ Z_{t+1} satisfies
                       #flagged·(hν·b) ≤ E_0 + hν·Z_0.
  Lean 4 core plus ns/EventSmall.
-/
import ns.EventSmall

namespace BadCount
open DetChain NSChain DyadicSmallGlobal LibRing DyadicLarge FourierGalerkin GalerkinForce ForceBound ForceSob
  ModeCount CubeCount ForceUniform NormCompare EnstStep EnstGalerkin CFLStep CFLGlobal EventSmall

/-- indicator of a Boolean flag -/
def ind : Bool → Nat
  | true => 1
  | false => 0

/-- number of flagged steps among s < n -/
def countB (bad : Nat → Bool) : Nat → Nat
  | 0 => 0
  | n + 1 => countB bad n + ind (bad n)

theorem count_split (bad : Nat → Bool) : ∀ n, countB bad n + countB (fun s => !(bad s)) n = n
  | 0 => rfl
  | n + 1 => by
    have ih := count_split bad n
    show countB bad n + ind (bad n) + (countB (fun s => !(bad s)) n + ind (!(bad n))) = n + 1
    cases bad n with
    | true =>
      show countB bad n + 1 + countB (fun s => !(bad s)) n = n + 1
      rw [Nat.add_right_comm, ih]
    | false =>
      show countB bad n + (countB (fun s => !(bad s)) n + 1) = n + 1
      rw [← Nat.add_assoc, ih]

section Gen
variable {R : Type} (K : CRing R) (O : OrdR K) {I : Type}
variable (inv : R → R) (mul_inv : ∀ y, y ≠ K.zero → K.mul y (inv y) = K.one) (h10 : K.one ≠ K.zero)

theorem le_of_eq'' (P : Cone K) (x y : R) (e : x = y) : Le K P x y :=
  ⟨K.zero, P.nn_zero, by rw [add_zero' K]; exact e⟩

theorem bad_sum (P : Cone K) (bad : Nat → Bool) (Y : Nat → R) (c : R) (hY : ∀ s, P.nn (Y s)) :
    ∀ n, (∀ s, s < n → bad s = true → Le K P c (Y s)) →
      Le K P (K.mul (castN K (countB bad n)) c) (sumN K Y n)
  | 0, _ => ⟨K.zero, P.nn_zero, by show K.add (K.mul K.zero c) K.zero = K.zero; lib_ring⟩
  | n + 1, h => by
    have ih := bad_sum P bad Y c hY n (fun s hs hb => h s (Nat.lt_succ_of_lt hs) hb)
    show Le K P (K.mul (castN K (countB bad n + ind (bad n))) c) (K.add (sumN K Y n) (Y n))
    cases hb : bad n with
    | true =>
      have s := le_add' K P _ _ _ _ ih (h n (Nat.lt_succ_self n) hb)
      refine le_trans' K P _ _ _ (le_of_eq'' K P _ _ ?_) s
      show K.mul (K.add K.one (castN K (countB bad n))) c = K.add (K.mul (castN K (countB bad n)) c) c
      lib_ring
    | false =>
      have s := le_add' K P _ _ _ _ ih (⟨Y n, hY n, K.zero_add _⟩ : Le K P K.zero (Y n))
      refine le_trans' K P _ _ _ (le_of_eq'' K P _ _ ?_) s
      show K.mul (castN K (countB bad n)) c = K.add (K.mul (castN K (countB bad n)) c) K.zero
      lib_ring

theorem bad_count (P : Cone K) (bad : Nat → Bool) (Y : Nat → R) (c S : R) (hY : ∀ s, P.nn (Y s)) (n : Nat)
    (h : ∀ s, s < n → bad s = true → Le K P c (Y s)) (hS : Le K P (sumN K Y n) S) :
    Le K P (K.mul (castN K (countB bad n)) c) S :=
  le_trans' K P _ _ _ (bad_sum K P bad Y c hY n h) hS

include mul_inv h10 in
/-- the integrated enstrophy over the first n + 1 steps is bounded by the data -/
theorem diss_total (l : List I) (kz : I → Mode) (hd : List.Pairwise (fun a b => kz a ≠ kz b) l)
    (h1 : ∀ p, 1 ≤ nrm (kz p)) (M : Nat) (hM : ∀ p, p ∈ l → nrm (kz p) ≤ M + 1)
    (ι h ν Es : R) (hh : O.nn h) (hν : O.nn ν) (hEs : O.nn Es) (a N : Nat → I → V3 R)
    (hs : ∀ t r, Step K h ν (wt K kz r) (a (t + 1) r) (a t r) (N t r))
    (hneut : ∀ t, lsum K (fun r => dot3 K (N t r) (a t r)) l = K.zero)
    (hP : ∀ t, Le K O.toCone (lsum K (fun r => dot3 K (N t r) (N t r)) l)
      (lsum K (fun r => dot3 K (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (a t) (a t) r)
        (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (a t) (a t) r)) l))
    (hcfl : Le K O.toCone (beta K M ι h ν Es) (K.mul h ν))
    (h0 : Le K O.toCone (K.add (lsum K (fun r => dot3 K (a 0 r) (a 0 r)) l)
      (K.mul (beta K M ι h ν Es) (lsum K (fun r => K.mul (wt K kz r) (dot3 K (a 0 r) (a 0 r))) l))) Es) (n : Nat) :
    Le K O.toCone
      (sumN K (fun t => K.mul (K.mul h ν) (lsum K (fun r => K.mul (wt K kz r) (dot3 K (a (t + 1) r) (a (t + 1) r))) l)) (n + 1))
      (K.add (lsum K (fun r => dot3 K (a 0 r) (a 0 r)) l)
        (K.mul (K.mul h ν) (lsum K (fun r => K.mul (wt K kz r) (dot3 K (a 0 r) (a 0 r))) l))) := by
  have hw : ∀ r, O.nn (wt K kz r) := fun r => dot3_nn K O.toCone _
  have nZ : ∀ t, O.nn (lsum K (fun r => K.mul (wt K kz r) (dot3 K (a t r) (a t r))) l) :=
    fun t => lsum_nn K O.toCone _ (fun r => O.nn_mul _ _ (hw r) (dot3_nn K O.toCone _)) l
  have nE : ∀ t, O.nn (lsum K (fun r => dot3 K (a t r) (a t r)) l) :=
    fun t => lsum_nn K O.toCone _ (fun r => dot3_nn K O.toCone _) l
  have nβ : O.nn (beta K M ι h ν Es) := by
    have three : O.nn (K.add (K.add K.one K.one) K.one) := O.nn_add _ _ (O.nn_add _ _ (RatModel.nn_one' K O.toCone) (RatModel.nn_one' K O.toCone)) (RatModel.nn_one' K O.toCone)
    have two : O.nn (K.add K.one K.one) := O.nn_add _ _ (RatModel.nn_one' K O.toCone) (RatModel.nn_one' K O.toCone)
    have nc := cM_nn K O M
    exact O.nn_mul _ _ (O.nn_sq h) (O.nn_add _ _ (O.nn_mul _ _ three (O.nn_mul _ _ (O.nn_sq ι) (O.nn_mul _ _ (RatModel.nn_cast K O.toCone 135) (O.nn_mul _ _ (O.nn_sq _) hEs))))
      (O.nn_mul _ _ two (O.nn_mul _ _ (O.nn_sq ν) nc)))
  have g := cfl_global K O inv mul_inv h10 l kz hd h1 M hM ι h ν Es hh hν hEs a N hs hneut hP hcfl h0 (n + 1)
  have e1 : Le K O.toCone (K.mul (K.mul h ν) (sumN K (fun t => lsum K (fun r => K.mul (wt K kz r) (dot3 K (a (t + 1) r) (a (t + 1) r))) l) (n + 1)))
      (K.add (K.add (lsum K (fun r => dot3 K (a (n + 1) r) (a (n + 1) r)) l)
        (K.mul (K.mul h ν) (sumN K (fun t => lsum K (fun r => K.mul (wt K kz r) (dot3 K (a (t + 1) r) (a (t + 1) r))) l) (n + 1))))
        (K.mul (beta K M ι h ν Es) (lsum K (fun r => K.mul (wt K kz r) (dot3 K (a (n + 1) r) (a (n + 1) r))) l))) :=
    ⟨K.add (lsum K (fun r => dot3 K (a (n + 1) r) (a (n + 1) r)) l) (K.mul (beta K M ι h ν Es) (lsum K (fun r => K.mul (wt K kz r) (dot3 K (a (n + 1) r) (a (n + 1) r))) l)),
      O.nn_add _ _ (nE (n + 1)) (O.nn_mul _ _ nβ (nZ (n + 1))), by lib_ring⟩
  have e2 := le_add' K O.toCone _ _ _ _ (le_refl' K O.toCone (lsum K (fun r => dot3 K (a 0 r) (a 0 r)) l))
    (le_mul_right' K O.toCone hcfl (nZ 0))
  have tot := le_trans' K O.toCone _ _ _ e1 (le_trans' K O.toCone _ _ _ g e2)
  have sm : sumN K (fun t => K.mul (K.mul h ν) (lsum K (fun r => K.mul (wt K kz r) (dot3 K (a (t + 1) r) (a (t + 1) r))) l)) (n + 1)
      = K.mul (K.mul h ν) (sumN K (fun t => lsum K (fun r => K.mul (wt K kz r) (dot3 K (a (t + 1) r) (a (t + 1) r))) l) (n + 1)) :=
    NSChain.sumN_mul_left K _ _ (n + 1)
  rw [← sm] at tot
  exact tot

include mul_inv h10 in
/-- Leray's structure statement, discrete: steps with enstrophy at least b are few, uniformly in the truncation -/
theorem leray_bad_count (l : List I) (kz : I → Mode) (hd : List.Pairwise (fun a b => kz a ≠ kz b) l)
    (h1 : ∀ p, 1 ≤ nrm (kz p)) (M : Nat) (hM : ∀ p, p ∈ l → nrm (kz p) ≤ M + 1)
    (ι h ν Es : R) (hh : O.nn h) (hν : O.nn ν) (hEs : O.nn Es) (a N : Nat → I → V3 R)
    (hs : ∀ t r, Step K h ν (wt K kz r) (a (t + 1) r) (a t r) (N t r))
    (hneut : ∀ t, lsum K (fun r => dot3 K (N t r) (a t r)) l = K.zero)
    (hP : ∀ t, Le K O.toCone (lsum K (fun r => dot3 K (N t r) (N t r)) l)
      (lsum K (fun r => dot3 K (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (a t) (a t) r)
        (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (a t) (a t) r)) l))
    (hcfl : Le K O.toCone (beta K M ι h ν Es) (K.mul h ν))
    (h0 : Le K O.toCone (K.add (lsum K (fun r => dot3 K (a 0 r) (a 0 r)) l)
      (K.mul (beta K M ι h ν Es) (lsum K (fun r => K.mul (wt K kz r) (dot3 K (a 0 r) (a 0 r))) l))) Es) (n : Nat)
    (bad : Nat → Bool) (b : R)
    (hbad : ∀ t, t < n + 1 → bad t = true →
      Le K O.toCone b (lsum K (fun r => K.mul (wt K kz r) (dot3 K (a (t + 1) r) (a (t + 1) r))) l)) :
    Le K O.toCone (K.mul (castN K (countB bad (n + 1))) (K.mul (K.mul h ν) b))
      (K.add (lsum K (fun r => dot3 K (a 0 r) (a 0 r)) l)
        (K.mul (K.mul h ν) (lsum K (fun r => K.mul (wt K kz r) (dot3 K (a 0 r) (a 0 r))) l))) := by
  have hw : ∀ r, O.nn (wt K kz r) := fun r => dot3_nn K O.toCone _
  have hY : ∀ t, O.nn (K.mul (K.mul h ν) (lsum K (fun r => K.mul (wt K kz r) (dot3 K (a (t + 1) r) (a (t + 1) r))) l)) :=
    fun t => O.nn_mul _ _ (O.nn_mul _ _ hh hν) (lsum_nn K O.toCone _ (fun r => O.nn_mul _ _ (hw r) (dot3_nn K O.toCone _)) l)
  exact bad_count K O.toCone bad _ _ _ hY (n + 1)
    (fun t ht hb => le_mul_nn K O.toCone _ _ _ (O.nn_mul _ _ hh hν) (hbad t ht hb))
    (diss_total K O inv mul_inv h10 l kz hd h1 M hM ι h ν Es hh hν hEs a N hs hneut hP hcfl h0 n)

end Gen

end BadCount

#print axioms BadCount.count_split
#print axioms BadCount.le_of_eq''
#print axioms BadCount.bad_sum
#print axioms BadCount.bad_count
#print axioms BadCount.diss_total
#print axioms BadCount.leray_bad_count

ns/BadRun.lean

lean_all/ns/BadRun.lean

/-
  ns/BadRun.lean — T37 (goal G859c2-s2): a run of consecutive steps with large enstrophy is short, uniformly in the
  truncation — the discrete form of "the singular times of a Leray solution have finite measure".
    sum_mono       nonnegative terms: Σ_{s<m} Y_s ≤ Σ_{s<m+k} Y_s
    run_sum        c ≤ Y_{s+t} for t < L, all Y ≥ 0  ⇒  L·c ≤ Σ_{t < s+L} Y_t
    leray_run      under the cfl_global hypotheses, if Z_{t+1} ≥ b for s ≤ t < s + L (s + L ≤ n + 1), then
                   L·(hν·b) ≤ E_0 + hν·Z_0.
  Lean 4 core plus ns/BadCount.
-/
import ns.BadCount

namespace BadRun
open DetChain NSChain DyadicSmallGlobal LibRing DyadicLarge FourierGalerkin GalerkinForce ForceBound ForceSob
  ModeCount CubeCount ForceUniform NormCompare EnstStep EnstGalerkin CFLStep CFLGlobal EventSmall BadCount

section Gen
variable {R : Type} (K : CRing R) (O : OrdR K) {I : Type}
variable (inv : R → R) (mul_inv : ∀ y, y ≠ K.zero → K.mul y (inv y) = K.one) (h10 : K.one ≠ K.zero)

theorem sum_mono (P : Cone K) (Y : Nat → R) (hY : ∀ s, P.nn (Y s)) (m : Nat) :
    ∀ k, Le K P (sumN K Y m) (sumN K Y (m + k))
  | 0 => le_refl' K P _
  | k + 1 => by
    have ih := sum_mono P Y hY m k
    show Le K P (sumN K Y m) (K.add (sumN K Y (m + k)) (Y (m + k)))
    exact le_trans' K P _ _ _ ih ⟨Y (m + k), hY (m + k), rfl⟩

theorem run_sum (P : Cone K) (Y : Nat → R) (c : R) (hY : ∀ s, P.nn (Y s)) (s : Nat) :
    ∀ L, (∀ t, t < L → Le K P c (Y (s + t))) → Le K P (K.mul (castN K L) c) (sumN K Y (s + L))
  | 0, _ => by
    refine le_trans' K P _ _ _ (le_of_eq'' K P _ K.zero ?_) ⟨_, sumN_nn K P Y hY s, K.zero_add _⟩
    show K.mul K.zero c = K.zero
    lib_ring
  | L + 1, h => by
    have ih := run_sum P Y c hY s L (fun t ht => h t (Nat.lt_succ_of_lt ht))
    have s2 := le_add' K P _ _ _ _ ih (h L (Nat.lt_succ_self L))
    show Le K P (K.mul (K.add K.one (castN K L)) c) (K.add (sumN K Y (s + L)) (Y (s + L)))
    refine le_trans' K P _ _ _ (le_of_eq'' K P _ _ ?_) s2
    lib_ring

theorem le_split {a b : Nat} (h : a ≤ b) : ∃ k, b = a + k := by
  induction h with
  | refl => exact ⟨0, rfl⟩
  | step _ ih =>
    obtain ⟨k, hk⟩ := ih
    exact ⟨k + 1, by rw [hk]; rfl⟩

include mul_inv h10 in
/-- **A run of steps with enstrophy ≥ b has length L with L·hν·b ≤ E_0 + hνZ_0.** -/
theorem leray_run (l : List I) (kz : I → Mode) (hd : List.Pairwise (fun a b => kz a ≠ kz b) l)
    (h1 : ∀ p, 1 ≤ nrm (kz p)) (M : Nat) (hM : ∀ p, p ∈ l → nrm (kz p) ≤ M + 1)
    (ι h ν Es : R) (hh : O.nn h) (hν : O.nn ν) (hEs : O.nn Es) (a N : Nat → I → V3 R)
    (hs : ∀ t r, Step K h ν (wt K kz r) (a (t + 1) r) (a t r) (N t r))
    (hneut : ∀ t, lsum K (fun r => dot3 K (N t r) (a t r)) l = K.zero)
    (hP : ∀ t, Le K O.toCone (lsum K (fun r => dot3 K (N t r) (N t r)) l)
      (lsum K (fun r => dot3 K (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (a t) (a t) r)
        (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (a t) (a t) r)) l))
    (hcfl : Le K O.toCone (beta K M ι h ν Es) (K.mul h ν))
    (h0 : Le K O.toCone (K.add (lsum K (fun r => dot3 K (a 0 r) (a 0 r)) l)
      (K.mul (beta K M ι h ν Es) (lsum K (fun r => K.mul (wt K kz r) (dot3 K (a 0 r) (a 0 r))) l))) Es) (n : Nat)
    (b : R) (s L : Nat) (hsL : s + L ≤ n + 1)
    (hrun : ∀ t, t < L →
      Le K O.toCone b (lsum K (fun r => K.mul (wt K kz r) (dot3 K (a (s + t + 1) r) (a (s + t + 1) r))) l)) :
    Le K O.toCone (K.mul (castN K L) (K.mul (K.mul h ν) b))
      (K.add (lsum K (fun r => dot3 K (a 0 r) (a 0 r)) l)
        (K.mul (K.mul h ν) (lsum K (fun r => K.mul (wt K kz r) (dot3 K (a 0 r) (a 0 r))) l))) := by
  have hw : ∀ r, O.nn (wt K kz r) := fun r => dot3_nn K O.toCone _
  have hY : ∀ t, O.nn (K.mul (K.mul h ν) (lsum K (fun r => K.mul (wt K kz r) (dot3 K (a (t + 1) r) (a (t + 1) r))) l)) :=
    fun t => O.nn_mul _ _ (O.nn_mul _ _ hh hν) (lsum_nn K O.toCone _ (fun r => O.nn_mul _ _ (hw r) (dot3_nn K O.toCone _)) l)
  have r1 := run_sum K O.toCone _ (K.mul (K.mul h ν) b) hY s L
    (fun t ht => le_mul_nn K O.toCone _ _ _ (O.nn_mul _ _ hh hν) (hrun t ht))
  obtain ⟨k, hk⟩ := le_split hsL
  have r2 := sum_mono K O.toCone _ hY (s + L) k
  rw [← hk] at r2
  exact le_trans' K O.toCone _ _ _ r1 (le_trans' K O.toCone _ _ _ r2
    (diss_total K O inv mul_inv h10 l kz hd h1 M hM ι h ν Es hh hν hEs a N hs hneut hP hcfl h0 n))

end Gen

end BadRun

#print axioms BadRun.sum_mono
#print axioms BadRun.run_sum
#print axioms BadRun.le_split
#print axioms BadRun.leray_run

ns/Basel.lean

lean_all/ns/Basel.lean

/-
  ns/Basel.lean — T37 cycle 71: Σ_{n≤N} 1/n² ≤ 2 in every ordered field, and the lattice weight bound.

  Lean 4 core plus ns/LatticeSum (tele_le), NSChain (Cone, Le, sumN) and lib/RingTac. Let u_n = 1/(n+1),
  given by (n+1)·u_n = 1 (no field structure needed: only these elements).
    u_nn        u_n ≥ 0                      (u_n = (n+1)u_n², and n+1 is a sum of ones)
    u_step      u_n − u_{n+1} = u_n u_{n+1}
    sq_le_diff  u_{n+1}² ≤ u_n − u_{n+1}
    basel       Σ_{n<N} u_n² ≤ 2             (Σ_{k=1}^N 1/k² ≤ 2)
    lattice     Σ_{n<N} 26(n+1)²·u_n⁴ ≤ 52   (shell n+1 has ≤ 26(n+1)² points, |k|^{-4} ≤ u_n⁴ there)
-/
import ns.LatticeSum

namespace Basel
open DetChain NSChain LibRing

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

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

theorem cN_nn (P : Cone K) (one_nn : P.nn K.one) : ∀ n, P.nn (cN K n)
  | 0 => P.nn_zero
  | n + 1 => P.nn_add _ _ (cN_nn P one_nn n) one_nn

theorem one_nn (P : Cone K) : P.nn K.one := by
  have e := P.nn_sq K.one; rw [K.one_mul] at e; exact e

theorem u_nn (P : Cone K) (u : Nat → R) (hu : ∀ n, K.mul (cN K (n + 1)) (u n) = K.one) (n : Nat) :
    P.nn (u n) := by
  have e : u n = K.mul (cN K (n + 1)) (K.mul (u n) (u n)) := by
    rw [← K.mul_assoc, hu n, K.one_mul]
  rw [e]; exact P.nn_mul _ _ (cN_nn K P (one_nn K P) _) (P.nn_sq _)

theorem u_step (u : Nat → R) (hu : ∀ n, K.mul (cN K (n + 1)) (u n) = K.one) (n : Nat) :
    sub K (u n) (u (n + 1)) = K.mul (u n) (u (n + 1)) := by
  have h1 := hu n
  have h2 := hu (n + 1)
  have c2 : cN K (n + 2) = K.add (cN K (n + 1)) K.one := rfl
  rw [c2] at h2
  revert h1 h2
  generalize cN K (n + 1) = c
  generalize u n = a
  generalize u (n + 1) = b
  intro h1 h2
  have ea : a = K.mul a (K.mul (K.add c K.one) b) := by rw [h2]; lib_ring
  have eb : b = K.mul b (K.mul c a) := by rw [h1]; lib_ring
  calc sub K a b = sub K (K.mul a (K.mul (K.add c K.one) b)) (K.mul b (K.mul c a)) := by
        rw [← ea, ← eb]
    _ = K.mul a b := by lib_ring

theorem sq_le_diff (P : Cone K) (u : Nat → R) (hu : ∀ n, K.mul (cN K (n + 1)) (u n) = K.one) (n : Nat) :
    Le K P (K.mul (u (n + 1)) (u (n + 1))) (sub K (u n) (u (n + 1))) := by
  rw [u_step K u hu n]
  -- u_n u_{n+1} − u_{n+1}² = u_{n+1}(u_n − u_{n+1}) = u_n u_{n+1}²
  refine ⟨K.mul (u n) (K.mul (u (n + 1)) (u (n + 1))), P.nn_mul _ _ (u_nn K P u hu n) (P.nn_sq _), ?_⟩
  have s := u_step K u hu n
  have e : K.mul (u n) (u (n + 1)) = K.add (K.mul (u (n + 1)) (u (n + 1)))
      (K.mul (u (n + 1)) (sub K (u n) (u (n + 1)))) := by lib_ring
  rw [s] at e
  rw [e]; lib_ring

/-- **Σ_{k=1}^N 1/k² ≤ 2.** -/
theorem basel (P : Cone K) (u : Nat → R) (hu : ∀ n, K.mul (cN K (n + 1)) (u n) = K.one) (N : Nat) :
    Le K P (sumN K (fun n => K.mul (u n) (u n)) (N + 1)) (K.add K.one K.one) := by
  have u0 : u 0 = K.one := by
    have h := hu 0
    have c1 : cN K 1 = K.one := by show K.add K.zero K.one = K.one; rw [K.zero_add]
    rw [c1, K.one_mul] at h; exact h
  -- Σ_{n<N+1} u_n² = u_0² + Σ_{n<N} u_{n+1}²
  have split : ∀ M, sumN K (fun n => K.mul (u n) (u n)) (M + 1)
      = K.add (K.mul (u 0) (u 0)) (sumN K (fun n => K.mul (u (n + 1)) (u (n + 1))) M) := by
    intro M
    induction M with
    | zero => show K.add K.zero (K.mul (u 0) (u 0)) = K.add (K.mul (u 0) (u 0)) K.zero; lib_ring
    | succ M ih =>
      show K.add (sumN K (fun n => K.mul (u n) (u n)) (M + 1)) (K.mul (u (M + 1)) (u (M + 1)))
        = K.add (K.mul (u 0) (u 0)) (K.add (sumN K (fun n => K.mul (u (n + 1)) (u (n + 1))) M)
            (K.mul (u (M + 1)) (u (M + 1))))
      rw [ih]; lib_ring
  rw [split N]
  have t := LatticeSum.tele_le K P u (fun n => K.mul (u n) (u n)) (u_nn K P u hu) (sq_le_diff K P u hu) N
  rw [u0] at t ⊢
  rw [K.one_mul]
  exact Riccati.le_add K P _ _ _ _ (le_refl' K P _) t

/-- **Lattice weight**: Σ_{n<N+1} 26·(n+1)²·u_n⁴ ≤ 26·2. -/
theorem lattice (P : Cone K) (u : Nat → R) (hu : ∀ n, K.mul (cN K (n + 1)) (u n) = K.one) (N : Nat) :
    Le K P (sumN K (fun n => K.mul (cN K 26) (K.mul (K.mul (cN K (n + 1)) (cN K (n + 1)))
        (K.mul (K.mul (u n) (u n)) (K.mul (u n) (u n))))) (N + 1))
      (K.mul (cN K 26) (K.add K.one K.one)) := by
  have e : ∀ n, K.mul (cN K 26) (K.mul (K.mul (cN K (n + 1)) (cN K (n + 1)))
        (K.mul (K.mul (u n) (u n)) (K.mul (u n) (u n)))) = K.mul (cN K 26) (K.mul (u n) (u n)) := by
    intro n
    have h := hu n
    revert h
    generalize cN K (n + 1) = c
    generalize u n = a
    intro h
    have r : K.mul (K.mul c c) (K.mul (K.mul a a) (K.mul a a)) = K.mul (K.mul (K.mul c a) (K.mul c a)) (K.mul a a) := by
      lib_ring
    rw [r, h]; lib_ring
  rw [sumN_ext K _ _ e, sumN_mul_left]
  exact Riccati.le_mul_left K P _ _ _ (cN_nn K P (one_nn K P) 26) (basel K P u hu N)

end Basel

#print axioms Basel.cN_nn
#print axioms Basel.one_nn
#print axioms Basel.u_nn
#print axioms Basel.u_step
#print axioms Basel.sq_le_diff
#print axioms Basel.basel
#print axioms Basel.lattice

ns/BaselQ.lean

lean_all/ns/BaselQ.lean

/-
  ns/BaselQ.lean — T37 cycle 942 (goal G624c, piece 2): the Basel partial sums over an ordered field (ℚ included):
    Σ_{n<N} 1/(n+1)² ≤ 2   for every N,
  from TeleSum.basel_le with w_n = 1/(n+1): the step 1/(n+2)² + 1/(n+2) + 1/((n+1)(n+2)²) = 1/(n+1) is checked
  after multiplying by (n+1)(n+2)² ≠ 0 (no zero divisors), and inverses of positive elements are nonnegative.

  Lean 4 core plus ns/TeleSum (basel_le), ns/GoodModesQ (cast_succ_ne, mul_ne, nn_cast), lib/RingTac.
-/
import ns.TeleSum
import ns.GoodModesQ

namespace BaselQ
open DetChain NSChain DyadicSmallGlobal LibRing DyadicLarge TeleSum GoodModesQ

section Gen
variable {R : Type} (K : CRing R) (O : OrdR K)
include O

theorem inv_nn (inv : R → R) (y : R) (hy : O.nn y) (hyi : K.mul y (inv y) = K.one) : O.nn (inv y) := by
  have e : inv y = K.mul y (K.mul (inv y) (inv y)) := by
    calc inv y = K.mul (K.mul y (inv y)) (inv y) := by rw [hyi, K.one_mul]
      _ = _ := by lib_ring
  rw [e]; exact O.nn_mul _ _ hy (O.nn_sq _)

theorem cancel (c x y : R) (hc : c ≠ K.zero) (h : K.mul c x = K.mul c y) : x = y := by
  have e : K.mul c (K.add x (K.neg y)) = K.zero := by
    have : K.mul c (K.add x (K.neg y)) = K.add (K.mul c x) (K.neg (K.mul c y)) := by lib_ring
    rw [this, h]; lib_ring
  cases O.noZD _ _ e with
  | inl h0 => exact absurd h0 hc
  | inr h0 =>
    calc x = K.add (K.add x (K.neg y)) y := by lib_ring
      _ = y := by rw [h0]; lib_ring

theorem basel (inv : R → R) (mul_inv : ∀ y, y ≠ K.zero → K.mul y (inv y) = K.one) (h10 : K.one ≠ K.zero) (N : Nat) :
    Le K O.toCone (sumN K (fun n => inv (K.mul (castN K (n + 1)) (castN K (n + 1)))) N) (K.add K.one K.one) := by
  have hA : ∀ n, castN K (n + 1) ≠ K.zero := fun n => cast_succ_ne K O h10 n
  have hAn : ∀ n, O.nn (castN K (n + 1)) := fun n => RatModel.nn_cast K O.toCone (n + 1)
  refine basel_le K O.toCone (fun n => inv (K.mul (castN K (n + 1)) (castN K (n + 1)))) (fun n => inv (castN K (n + 1)))
    (fun n => inv_nn K O inv _ (hAn n) (mul_inv _ (hA n))) ?_ ?_ ?_ N
  · -- w_0 = inv 1 = 1
    show inv (K.add K.one K.zero) = K.one
    have h1 : K.add K.one K.zero = K.one := by lib_ring
    rw [h1]
    have := mul_inv K.one h10
    rw [K.one_mul] at this; exact this
  · refine ⟨K.zero, O.nn_zero, ?_⟩
    show K.add (inv (K.mul (K.add K.one K.zero) (K.add K.one K.zero))) K.zero = K.one
    have h1 : K.mul (K.add K.one K.zero) (K.add K.one K.zero) = K.one := by lib_ring
    rw [h1]
    have := mul_inv K.one h10
    rw [K.one_mul] at this; rw [this]; lib_ring
  · intro n
    generalize hA' : castN K (n + 1) = A
    have hB : castN K (n + 1 + 1) = K.add K.one A := by rw [← hA']; rfl
    rw [hB]
    have A0 : A ≠ K.zero := by rw [← hA']; exact hA n
    have B0 : K.add K.one A ≠ K.zero := by rw [← hB]; exact hA (n + 1)
    have An : O.nn A := by rw [← hA']; exact hAn n
    generalize hBdef : K.add K.one A = B at B0 ⊢
    have Bn : O.nn B := by rw [← hBdef]; exact O.nn_add _ _ (RatModel.nn_one' K O.toCone) An
    have BB0 : K.mul B B ≠ K.zero := mul_ne K O _ _ B0 B0
    have c0 : K.mul A (K.mul B B) ≠ K.zero := mul_ne K O _ _ A0 BB0
    have iA := mul_inv A A0
    have iB := mul_inv B B0
    have iBB := mul_inv _ BB0
    have ic := mul_inv _ c0
    refine ⟨inv (K.mul A (K.mul B B)), inv_nn K O inv _ (O.nn_mul _ _ An (O.nn_mul _ _ Bn Bn)) ic, ?_⟩
    apply cancel K O (K.mul A (K.mul B B)) _ _ c0
    generalize inv A = wA at iA ⊢
    generalize inv B = wB at iB ⊢
    generalize inv (K.mul B B) = zB at iBB ⊢
    generalize inv (K.mul A (K.mul B B)) = d at ic ⊢
    -- c(zB + wB + d) = A + A B + 1 = B² = c wA   (B = 1 + A)
    calc K.mul (K.mul A (K.mul B B)) (K.add (K.add zB wB) d)
        = K.add (K.add (K.mul A (K.mul (K.mul B B) zB)) (K.mul (K.mul A B) (K.mul B wB))) (K.mul (K.mul A (K.mul B B)) d) := by lib_ring
      _ = K.add (K.add A (K.mul A B)) K.one := by rw [iBB, iB, ic]; lib_ring
      _ = K.mul (K.mul B B) (K.mul A wA) := by rw [iA, ← hBdef]; lib_ring
      _ = _ := by lib_ring

end Gen

end BaselQ

#print axioms BaselQ.inv_nn
#print axioms BaselQ.cancel
#print axioms BaselQ.basel

ns/BaselTail.lean

lean_all/ns/BaselTail.lean

/-
  ns/BaselTail.lean — T37 cycle 134: the tail of Σ 1/n² (piece of Agmon's inequality ‖u‖_∞² ≤ C‖∇u‖‖Δu‖ in
  3D, uniform in the truncation: the high modes cost 1/K).

  Lean 4 core plus ns/Basel (u_n = 1/(n+1), sq_le_diff, u_nn), ns/LatticeSum (tele_le).
    tail     Σ_{j<N} u_{K+j+1}² ≤ u_K        (Σ_{n>K} 1/(n+1)² ≤ 1/(K+1), every N)
-/
import ns.Basel

namespace BaselTail
open DetChain NSChain LibRing Basel

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

theorem tail (P : Cone K) (u : Nat → R) (hu : ∀ n, K.mul (cN K (n + 1)) (u n) = K.one) (Kk N : Nat) :
    Le K P (sumN K (fun j => K.mul (u (Kk + j + 1)) (u (Kk + j + 1))) N) (u Kk) :=
  LatticeSum.tele_le K P (fun j => u (Kk + j)) (fun j => K.mul (u (Kk + j)) (u (Kk + j)))
    (fun j => u_nn K P u hu (Kk + j)) (fun j => sq_le_diff K P u hu (Kk + j)) N

end BaselTail

#print axioms BaselTail.tail

ns/BiotSavart.lean

lean_all/ns/BiotSavart.lean

/-
  ns/BiotSavart.lean — T37 cycle 91: the Fourier Biot–Savart identities.

  Lean 4 core plus ns/Commutator (d3, coordinates) and lib/RingTac. For a Fourier mode û at wave vector k
  with k·û = 0 (incompressibility), ω̂ = k × û:
      |k × û|² + (k·û)² = |k|²|û|²       (Lagrange)          lagrange_c
      |ω̂|² = |k|²|û|²                     (k·û = 0)            vort_norm
  and on the integer lattice (|k|² ≥ 1, i.e. |k|² = 1 + s with s ≥ 0), |ω̂|² ≥ |û|²: enstrophy ≥ energy
  mode by mode (Poincaré on the torus)                                                    poincare_mode
-/
import ns.Commutator

namespace BiotSavart
open DetChain NSChain LibRing Commutator

section Ring
variable {R : Type} (K : CRing R)

def cx (_k1 k2 k3 _u1 u2 u3 : R) : R := sub K (K.mul k2 u3) (K.mul k3 u2)
def cy (k1 _k2 k3 u1 _u2 u3 : R) : R := sub K (K.mul k3 u1) (K.mul k1 u3)
def cz (k1 k2 _k3 u1 u2 _u3 : R) : R := sub K (K.mul k1 u2) (K.mul k2 u1)

theorem lagrange_c (k1 k2 k3 u1 u2 u3 : R) :
    K.add (d3 K (cx K k1 k2 k3 u1 u2 u3) (cy K k1 k2 k3 u1 u2 u3) (cz K k1 k2 k3 u1 u2 u3)
              (cx K k1 k2 k3 u1 u2 u3) (cy K k1 k2 k3 u1 u2 u3) (cz K k1 k2 k3 u1 u2 u3))
      (K.mul (d3 K k1 k2 k3 u1 u2 u3) (d3 K k1 k2 k3 u1 u2 u3))
      = K.mul (d3 K k1 k2 k3 k1 k2 k3) (d3 K u1 u2 u3 u1 u2 u3) := by
  unfold d3 cx cy cz; lib_ring

/-- **|ω̂|² = |k|²|û|²** for divergence-free û. -/
theorem vort_norm (k1 k2 k3 u1 u2 u3 : R) (div : d3 K k1 k2 k3 u1 u2 u3 = K.zero) :
    d3 K (cx K k1 k2 k3 u1 u2 u3) (cy K k1 k2 k3 u1 u2 u3) (cz K k1 k2 k3 u1 u2 u3)
        (cx K k1 k2 k3 u1 u2 u3) (cy K k1 k2 k3 u1 u2 u3) (cz K k1 k2 k3 u1 u2 u3)
      = K.mul (d3 K k1 k2 k3 k1 k2 k3) (d3 K u1 u2 u3 u1 u2 u3) := by
  have l := lagrange_c K k1 k2 k3 u1 u2 u3
  rw [div] at l
  rw [← l]; lib_ring

/-- **Poincaré, one mode**: |k|² = 1 + s, s ≥ 0 ⇒ |û|² ≤ |ω̂|². -/
theorem poincare_mode (P : Cone K) (k1 k2 k3 u1 u2 u3 s : R) (hs : P.nn s)
    (hk : d3 K k1 k2 k3 k1 k2 k3 = K.add K.one s) (div : d3 K k1 k2 k3 u1 u2 u3 = K.zero) :
    Le K P (d3 K u1 u2 u3 u1 u2 u3)
      (d3 K (cx K k1 k2 k3 u1 u2 u3) (cy K k1 k2 k3 u1 u2 u3) (cz K k1 k2 k3 u1 u2 u3)
        (cx K k1 k2 k3 u1 u2 u3) (cy K k1 k2 k3 u1 u2 u3) (cz K k1 k2 k3 u1 u2 u3)) := by
  rw [vort_norm K k1 k2 k3 u1 u2 u3 div, hk]
  exact ⟨K.mul s (d3 K u1 u2 u3 u1 u2 u3), P.nn_mul _ _ hs (d3_nn K P _ _ _), by lib_ring⟩

end Ring

end BiotSavart

#print axioms BiotSavart.lagrange_c
#print axioms BiotSavart.vort_norm
#print axioms BiotSavart.poincare_mode

ns/BoxClose.lean

lean_all/ns/BoxClose.lean

/-
  ns/BoxClose.lean — T37 cycle 1740 (goal G852c): two values in the same box of a δ-grid are δ-close.
  Over any cone: if L ≤ x ≤ L + δ and L ≤ y ≤ L + δ then (x − y)² ≤ δ² (since δ − (x − y) ≥ 0 and
  δ + (x − y) ≥ 0, their product δ² − (x − y)² is ≥ 0). With PigeonVec: two truncations whose quantised
  coordinates agree are δ-close in every coordinate. Lean 4 core, hand proofs, no axioms.
-/
import NSChain
import lib.RingTac

namespace BoxClose
open DetChain NSChain LibRing

section Gen
variable {R : Type} (K : CRing R) (P : Cone K)

theorem box_close (L δ x y : R) (hx1 : Le K P L x) (hx2 : Le K P x (K.add L δ))
    (hy1 : Le K P L y) (hy2 : Le K P y (K.add L δ)) :
    Le K P (K.mul (sub K x y) (sub K x y)) (K.mul δ δ) := by
  cases hx1 with
  | intro a ha =>
    cases hx2 with
    | intro b hb =>
      cases hy1 with
      | intro c hc =>
        cases hy2 with
        | intro d hd =>
          -- x = L + a, L + δ = x + b, y = L + c, L + δ = y + d
          -- δ − (x − y) = b + c,  δ + (x − y) = a + d
          refine ⟨K.mul (K.add b c) (K.add a d), P.nn_mul _ _ (P.nn_add _ _ hb.1 hc.1) (P.nn_add _ _ ha.1 hd.1), ?_⟩
          have ex : x = K.add L a := ha.2.symm
          have ey : y = K.add L c := hc.2.symm
          have eδ : δ = K.add a b := by
            have h := hb.2
            rw [ex] at h
            have : δ = K.add (K.add L δ) (K.neg L) := by lib_ring
            rw [this, ← h]; lib_ring
          have ed : d = K.add (K.add a b) (K.neg c) := by
            have h := hd.2
            rw [ey, eδ] at h
            have : d = K.add (K.add (K.add L c) d) (K.add (K.neg L) (K.neg c)) := by lib_ring
            rw [this, h]; lib_ring
          rw [ex, ey, eδ, ed]
          lib_ring

end Gen
end BoxClose

#print axioms BoxClose.box_close

ns/CFKernel.lean

lean_all/ns/CFKernel.lean

/-
  ns/CFKernel.lean — T37 cycle 37: the algebraic heart of Constantin–Fefferman (route R3).

  Lean 4 core plus NSChain (CRing, Cone, Le) and tools/RingNorm (the normaliser).

  The vortex-stretching integrand of the Biot–Savart law is (ê·ξ(x)) det(ê, ξ(x+y), ξ(x)); it vanishes
  when the directions are parallel and is bounded by the sine of their angle. In every ordered
  commutative ring:
    semi             (a·b)² + Σ_cyc[(a₂b₃)² + (a₃b₂)²] = (a·a)(b·b) + 2Σ_cyc (a₂b₃)(a₃b₂)   (normaliser)
    sq_sub           (x − y)² + 2xy = x² + y²
    lagrange3        (a·b)² + |a × b|² = (a·a)(b·b)                               (3D Lagrange identity)
    cauchy_schwarz3  (a·b)² ≤ (a·a)(b·b)
    triple_bound     ê·ê = 1, ξ·ξ = 1, w = η × ξ:  (ê·ξ)² (ê·w)² ≤ w·w = |η × ξ|²
                     — the squared stretching kernel is at most sin²∠(η, ξ) for unit η
  With the Beirão da Veiga–Berselli condition |sin∠(ξ(x+y), ξ(x))| ≤ ρ|y|^{1/2}, this is what turns the
  kernel |y|^{-3} into the integrable |y|^{-5/2} (R3; measured quotient: NS_MAP R3-meas2).
-/
import NSChain
import tools.RingNorm

namespace CFKernel
open DetChain NSChain RingNorm

def x1 := PE.var 0
def x2 := PE.var 1
def x3 := PE.var 2
def y1 := PE.var 3
def y2 := PE.var 4
def y3 := PE.var 5
def d (p q r s t w : PE) : PE := .add (.add (.mul p s) (.mul q t)) (.mul r w)
def sq (e : PE) : PE := .mul e e
def LHS : PE := .add (sq (d x1 x2 x3 y1 y2 y3))
  (.add (.add (.add (sq (.mul x2 y3)) (sq (.mul x3 y2))) (.add (sq (.mul x3 y1)) (sq (.mul x1 y3))))
    (.add (sq (.mul x1 y2)) (sq (.mul x2 y1))))
def RHS : PE := .add (.mul (d x1 x2 x3 x1 x2 x3) (d y1 y2 y3 y1 y2 y3))
  (.add (.add (.add (.mul (.mul x2 y3) (.mul x3 y2)) (.mul (.mul x2 y3) (.mul x3 y2)))
      (.add (.mul (.mul x3 y1) (.mul x1 y3)) (.mul (.mul x3 y1) (.mul x1 y3))))
    (.add (.mul (.mul x1 y2) (.mul x2 y1)) (.mul (.mul x1 y2) (.mul x2 y1))))
theorem chk : nf LHS = nf RHS := rfl

section Ring
variable {R : Type} (K : CRing R)

structure V (R : Type) where
  x : R
  y : R
  z : R

def rho (a b : V R) (i : Nat) : R :=
  match i with
  | 0 => a.x | 1 => a.y | 2 => a.z | 3 => b.x | 4 => b.y | _ => b.z

def dot (a b : V R) : R := K.add (K.add (K.mul a.x b.x) (K.mul a.y b.y)) (K.mul a.z b.z)
def sub (u v : R) : R := K.add u (K.neg v)
def cross (a b : V R) : V R :=
  ⟨sub K (K.mul a.y b.z) (K.mul a.z b.y), sub K (K.mul a.z b.x) (K.mul a.x b.z),
   sub K (K.mul a.x b.y) (K.mul a.y b.x)⟩

theorem semi (a b : V R) :
    K.add (K.mul (dot K a b) (dot K a b))
      (K.add (K.add (K.add (K.mul (K.mul a.y b.z) (K.mul a.y b.z)) (K.mul (K.mul a.z b.y) (K.mul a.z b.y)))
        (K.add (K.mul (K.mul a.z b.x) (K.mul a.z b.x)) (K.mul (K.mul a.x b.z) (K.mul a.x b.z))))
        (K.add (K.mul (K.mul a.x b.y) (K.mul a.x b.y)) (K.mul (K.mul a.y b.x) (K.mul a.y b.x))))
    = K.add (K.mul (dot K a a) (dot K b b))
      (K.add (K.add (K.add (K.mul (K.mul a.y b.z) (K.mul a.z b.y)) (K.mul (K.mul a.y b.z) (K.mul a.z b.y)))
        (K.add (K.mul (K.mul a.z b.x) (K.mul a.x b.z)) (K.mul (K.mul a.z b.x) (K.mul a.x b.z))))
        (K.add (K.mul (K.mul a.x b.y) (K.mul a.y b.x)) (K.mul (K.mul a.x b.y) (K.mul a.y b.x)))) :=
  norm_sound K (rho a b) LHS RHS chk

theorem sq_sub (u v : R) :
    K.add (K.mul (sub K u v) (sub K u v)) (K.add (K.mul u v) (K.mul u v)) = K.add (K.mul u u) (K.mul v v) := by
  unfold sub
  rw [right_distrib K, K.distrib, K.distrib, mul_neg' K, neg_mul_left K, neg_mul_left K, mul_neg' K,
    neg_neg' K, K.mul_comm v u]
  generalize K.mul u u = A
  generalize K.mul u v = B
  generalize K.mul v v = C
  -- (A + −B) + (−B + C) + (B + B) = A + C
  rw [add_mid4 K, K.add_assoc, K.add_assoc, ← K.add_assoc (K.neg B) B, add_neg' K, K.zero_add,
    K.add_comm (K.neg B) C, K.add_assoc C, add_neg' K, add_zero']

def crossSq (a b : V R) : R :=
  K.add (K.add (K.mul (cross K a b).x (cross K a b).x) (K.mul (cross K a b).y (cross K a b).y))
    (K.mul (cross K a b).z (cross K a b).z)

/-- **3D Lagrange identity.** -/
theorem lagrange3 (a b : V R) :
    K.add (K.mul (dot K a b) (dot K a b)) (crossSq K a b) = K.mul (dot K a a) (dot K b b) := by
  have h := semi K a b
  -- S = crossSq + T (componentwise sq_sub)
  have hS : K.add (K.add (K.add (K.mul (K.mul a.y b.z) (K.mul a.y b.z)) (K.mul (K.mul a.z b.y) (K.mul a.z b.y)))
        (K.add (K.mul (K.mul a.z b.x) (K.mul a.z b.x)) (K.mul (K.mul a.x b.z) (K.mul a.x b.z))))
        (K.add (K.mul (K.mul a.x b.y) (K.mul a.x b.y)) (K.mul (K.mul a.y b.x) (K.mul a.y b.x)))
      = K.add (crossSq K a b)
        (K.add (K.add (K.add (K.mul (K.mul a.y b.z) (K.mul a.z b.y)) (K.mul (K.mul a.y b.z) (K.mul a.z b.y)))
        (K.add (K.mul (K.mul a.z b.x) (K.mul a.x b.z)) (K.mul (K.mul a.z b.x) (K.mul a.x b.z))))
        (K.add (K.mul (K.mul a.x b.y) (K.mul a.y b.x)) (K.mul (K.mul a.x b.y) (K.mul a.y b.x)))) := by
    unfold crossSq cross
    show _ = K.add (K.add (K.add (K.mul (sub K (K.mul a.y b.z) (K.mul a.z b.y)) (sub K (K.mul a.y b.z) (K.mul a.z b.y)))
        (K.mul (sub K (K.mul a.z b.x) (K.mul a.x b.z)) (sub K (K.mul a.z b.x) (K.mul a.x b.z))))
        (K.mul (sub K (K.mul a.x b.y) (K.mul a.y b.x)) (sub K (K.mul a.x b.y) (K.mul a.y b.x)))) _
    rw [← sq_sub K (K.mul a.y b.z) (K.mul a.z b.y), ← sq_sub K (K.mul a.z b.x) (K.mul a.x b.z),
      ← sq_sub K (K.mul a.x b.y) (K.mul a.y b.x)]
    generalize K.mul (sub K (K.mul a.y b.z) (K.mul a.z b.y)) (sub K (K.mul a.y b.z) (K.mul a.z b.y)) = S1
    generalize K.mul (sub K (K.mul a.z b.x) (K.mul a.x b.z)) (sub K (K.mul a.z b.x) (K.mul a.x b.z)) = S2
    generalize K.mul (sub K (K.mul a.x b.y) (K.mul a.y b.x)) (sub K (K.mul a.x b.y) (K.mul a.y b.x)) = S3
    generalize K.add (K.mul (K.mul a.y b.z) (K.mul a.z b.y)) (K.mul (K.mul a.y b.z) (K.mul a.z b.y)) = T1
    generalize K.add (K.mul (K.mul a.z b.x) (K.mul a.x b.z)) (K.mul (K.mul a.z b.x) (K.mul a.x b.z)) = T2
    generalize K.add (K.mul (K.mul a.x b.y) (K.mul a.y b.x)) (K.mul (K.mul a.x b.y) (K.mul a.y b.x)) = T3
    rw [add_mid4 K (K.add S1 S2) S3 (K.add T1 T2) T3, add_mid4 K S1 S2 T1 T2]
  rw [hS, ← K.add_assoc] at h
  exact add_cancel_left K _ _ _ (by rw [K.add_comm _ (K.add _ (crossSq K a b)), h, K.add_comm])

theorem crossSq_nn (P : Cone K) (a b : V R) : P.nn (crossSq K a b) :=
  P.nn_add _ _ (P.nn_add _ _ (P.nn_sq _) (P.nn_sq _)) (P.nn_sq _)

/-- **Cauchy–Schwarz in 3D.** -/
theorem cauchy_schwarz3 (P : Cone K) (a b : V R) :
    Le K P (K.mul (dot K a b) (dot K a b)) (K.mul (dot K a a) (dot K b b)) :=
  ⟨crossSq K a b, crossSq_nn K P a b, lagrange3 K a b⟩

/-- **The stretching kernel is bounded by the angle.** For unit ê, ξ and w = η × ξ:
    (ê·ξ)² (ê·w)² ≤ w·w. -/
theorem triple_bound (P : Cone K) (e xi eta : V R) (he : dot K e e = K.one) (hxi : dot K xi xi = K.one) :
    Le K P (K.mul (K.mul (dot K e xi) (dot K e xi)) (K.mul (dot K e (cross K eta xi)) (dot K e (cross K eta xi))))
      (dot K (cross K eta xi) (cross K eta xi)) := by
  let w := cross K eta xi
  have c1 := cauchy_schwarz3 K P e xi
  rw [he, hxi, K.one_mul] at c1
  have c2 := cauchy_schwarz3 K P e w
  rw [he, K.one_mul] at c2
  -- X ≤ 1, Y ≤ Z, X ≥ 0 ⇒ X·Y ≤ X·Z ≤ Z
  cases c1 with
  | intro d1 h1 =>
    cases c2 with
    | intro d2 h2 =>
      -- X Y + (X d2 + d1 Z) = Z
      refine ⟨K.add (K.mul (K.mul (dot K e xi) (dot K e xi)) d2) (K.mul d1 (dot K w w)),
        P.nn_add _ _ (P.nn_mul _ _ (P.nn_sq _) h2.1) (P.nn_mul _ _ h1.1 (by
          show P.nn (dot K w w)
          unfold dot
          exact P.nn_add _ _ (P.nn_add _ _ (P.nn_sq _) (P.nn_sq _)) (P.nn_sq _))), ?_⟩
      rw [← K.add_assoc, ← K.distrib, h2.2, ← right_distrib K, h1.2, K.one_mul]

end Ring

end CFKernel

#print axioms CFKernel.chk
#print axioms CFKernel.semi
#print axioms CFKernel.sq_sub
#print axioms CFKernel.lagrange3
#print axioms CFKernel.crossSq_nn
#print axioms CFKernel.cauchy_schwarz3
#print axioms CFKernel.triple_bound

ns/CFLBound.lean

lean_all/ns/CFLBound.lean

/-
  ns/CFLBound.lean — T37 cycle 859 (goal G568): the weighted (H¹-type, w = 4ⁿ) bound for the semi-implicit
  concrete model under a CFL condition, over any ordered ring without zero divisors (ℚ included). Hypotheses:
  the semi-implicit steps a_{t+1}(n+1) − a_t(n+1) = −x_n(a_{t+1}(n+1) + a_t(n+1)) + h·dyN(2ⁿ, a_t)_n with
  x_n = h·3·4^{n+1}, 4x_n ≤ 1 (n < N), boundary zeros, energies Σ a_t(n+1)² ≤ A² (A = m; e.g. from SemiDyadic), and
  the remaining step terms h Σ w Δ N + 2 Σ w x (hN)² ≤ ρ'. Then with E_t = Σ 4^{n+1} a_t(n+1)², G_t = Σ 4^{n+1} x_n a_t(n+1)²:
    (4E_T + G_T) + 6 Σ_{t<T} G_{t+1} ≤ (4E_0 + G_0) + 4·T·(2h·B_head + ρ').

  Lean 4 core plus ns/LowCFL (step_low), ns/StateRate (state_rate), ns/TwoLevelConst, ns/LowAssemble (m2).
-/
import ns.LowCFL
import ns.StateRate
import ns.TwoLevelConst

namespace CFLBound
open DetChain NSChain DyadicSmallGlobal LibRing DyadicLarge RatModel LowAssemble TwoLevel LowCFL StateRate TwoLevelConst

theorem cfl_bound {R : Type} (K : CRing R) (O : OrdR K) (h10 : K.one ≠ K.zero)
    (a : Nat → Nat → R) (h rho : R) (m N T : Nat) (hh : O.nn h)
    (h4 : ∀ n, Le K O.toCone (K.add (K.add (K.mul h (K.mul (castN K 3) (P4 K (n + 1)))) (K.mul h (K.mul (castN K 3) (P4 K (n + 1)))))
      (K.add (K.mul h (K.mul (castN K 3) (P4 K (n + 1)))) (K.mul h (K.mul (castN K 3) (P4 K (n + 1)))))) K.one)
    (st : ∀ t n, K.add (a (t + 1) (n + 1)) (K.neg (a t (n + 1)))
      = K.add (K.neg (K.mul (K.mul h (K.mul (castN K 3) (P4 K (n + 1)))) (K.add (a (t + 1) (n + 1)) (a t (n + 1)))))
        (K.mul h (dyN K (P2 K) (a t) n)))
    (h0 : ∀ t, a t 0 = K.zero) (hN : ∀ t i, N + 1 ≤ i → a t i = K.zero)
    (hE : ∀ t, Le K O.toCone (sumN K (fun n => K.mul (a t (n + 1)) (a t (n + 1))) N) (K.mul (castN K m) (castN K m)))
    (hrest : ∀ t, Le K O.toCone
      (K.add (K.mul h (sumN K (fun n => K.mul (P4 K (n + 1)) (K.mul (K.add (a (t + 1) (n + 1)) (K.neg (a t (n + 1))))
          (dyN K (P2 K) (a t) n))) N))
        (m2 K (sumN K (fun n => K.mul (P4 K (n + 1)) (K.mul (K.mul h (K.mul (castN K 3) (P4 K (n + 1))))
          (K.mul (K.mul h (dyN K (P2 K) (a t) n)) (K.mul h (dyN K (P2 K) (a t) n))))) N))) rho) :
    Le K O.toCone
      (K.add (K.add (c4 K (sumN K (fun n => K.mul (P4 K (n + 1)) (K.mul (a T (n + 1)) (a T (n + 1)))) N))
          (sumN K (fun n => K.mul (P4 K (n + 1)) (K.mul (K.mul h (K.mul (castN K 3) (P4 K (n + 1)))) (K.mul (a T (n + 1)) (a T (n + 1))))) N))
        (c6 K (sumN K (fun t => sumN K (fun n => K.mul (P4 K (n + 1)) (K.mul (K.mul h (K.mul (castN K 3) (P4 K (n + 1))))
          (K.mul (a (t + 1) (n + 1)) (a (t + 1) (n + 1))))) N) T)))
      (K.add (K.add (c4 K (sumN K (fun n => K.mul (P4 K (n + 1)) (K.mul (a 0 (n + 1)) (a 0 (n + 1)))) N))
          (sumN K (fun n => K.mul (P4 K (n + 1)) (K.mul (K.mul h (K.mul (castN K 3) (P4 K (n + 1)))) (K.mul (a 0 (n + 1)) (a 0 (n + 1))))) N))
        (c4 K (K.mul (castN K T) (K.add (K.mul (m2 K h)
          (sumN K (fun n => if n < m then K.mul (qq K (P4 K) (P2 K) n) (K.mul (K.mul (castN K m) (castN K m)) (castN K m)) else K.zero) N))
          rho)))) := by
  let B := sumN K (fun n => if n < m then K.mul (qq K (P4 K) (P2 K) n) (K.mul (K.mul (castN K m) (castN K m)) (castN K m)) else K.zero) N
  have hx : ∀ n, O.nn (K.mul h (K.mul (castN K 3) (P4 K (n + 1)))) := fun n =>
    O.nn_mul _ _ hh (O.nn_mul _ _ (nn_cast K O.toCone 3) (nn_pw K O.toCone _ (nn_cast K O.toCone 4) (n + 1)))
  have hw : ∀ n, O.nn (P4 K (n + 1)) := fun n => nn_pw K O.toCone _ (nn_cast K O.toCone 4) (n + 1)
  refine two_level_const K O.toCone
    (fun t => sumN K (fun n => K.mul (P4 K (n + 1)) (K.mul (a t (n + 1)) (a t (n + 1)))) N)
    (fun t => sumN K (fun n => K.mul (P4 K (n + 1)) (K.mul (K.mul h (K.mul (castN K 3) (P4 K (n + 1)))) (K.mul (a t (n + 1)) (a t (n + 1))))) N)
    (fun t => K.add (K.add (K.mul (m2 K h) B)
      (K.mul h (sumN K (fun n => K.mul (P4 K (n + 1)) (K.mul (K.add (a (t + 1) (n + 1)) (K.neg (a t (n + 1)))) (dyN K (P2 K) (a t) n))) N)))
      (m2 K (sumN K (fun n => K.mul (P4 K (n + 1)) (K.mul (K.mul h (K.mul (castN K 3) (P4 K (n + 1))))
        (K.mul (K.mul h (dyN K (P2 K) (a t) n)) (K.mul h (dyN K (P2 K) (a t) n))))) N)))
    (K.add (K.mul (m2 K h) B) rho) ?_ ?_ T
  · intro t
    exact step_low K O h10 (fun n => P4 K (n + 1)) (fun n => P4 K (n + 1)) (fun n => a t (n + 1)) (fun n => a (t + 1) (n + 1))
      (fun n => dyN K (P2 K) (a t) n) h (castN K 3) B N hh hw hx h4 (st t)
      (state_rate K O (a t) m N (h0 t) (hN t) (hE t))
  · intro t
    have e : K.add (K.add (K.mul (m2 K h) B)
        (K.mul h (sumN K (fun n => K.mul (P4 K (n + 1)) (K.mul (K.add (a (t + 1) (n + 1)) (K.neg (a t (n + 1)))) (dyN K (P2 K) (a t) n))) N)))
        (m2 K (sumN K (fun n => K.mul (P4 K (n + 1)) (K.mul (K.mul h (K.mul (castN K 3) (P4 K (n + 1))))
          (K.mul (K.mul h (dyN K (P2 K) (a t) n)) (K.mul h (dyN K (P2 K) (a t) n))))) N))
        = K.add (K.mul (m2 K h) B) (K.add
          (K.mul h (sumN K (fun n => K.mul (P4 K (n + 1)) (K.mul (K.add (a (t + 1) (n + 1)) (K.neg (a t (n + 1)))) (dyN K (P2 K) (a t) n))) N))
          (m2 K (sumN K (fun n => K.mul (P4 K (n + 1)) (K.mul (K.mul h (K.mul (castN K 3) (P4 K (n + 1))))
            (K.mul (K.mul h (dyN K (P2 K) (a t) n)) (K.mul h (dyN K (P2 K) (a t) n))))) N))) := by lib_ring
    rw [e]
    exact le_add' K O.toCone _ _ _ _ (le_refl' K O.toCone _) (hrest t)

end CFLBound

#print axioms CFLBound.cfl_bound

ns/CFLDecay.lean

lean_all/ns/CFLDecay.lean

/-
  ns/CFLDecay.lean — T37 cycle 1311 (goal G787a): exponential decay of the CFL Lyapunov functional, over an ordered
  ring. With V_k = E_k + β·Z_k, suppose for every k
      E_{k+1} + 2c·Z_{k+1} ≤ E_k + β·Z_k      (CFLStep, once E_k ≤ E* is known from CFLGlobal)
      E_k ≤ Z_k                                (Poincaré: every mode has |k| ≥ 1)
  with 0 ≤ β ≤ c. Then
    decay_step   (1 + β + c)·V_{k+1} ≤ (1 + β)·V_k
    cfl_decay    (1 + β + c)ⁿ·V_n ≤ (1 + β)ⁿ·V_0,
  so for arbitrary data and fixed truncation the discrete energy decays geometrically, at the rate
  (1 + β)/(1 + β + hν) per step (c = hν).

  Lean 4 core plus ns/CFLEnergy (Le API, TruncRate.nn_of_le), lib/RingTac.
-/
import ns.CFLEnergy

namespace CFLDecay
open DetChain NSChain DyadicSmallGlobal LibRing DyadicLarge FourierGalerkin ForceBound

section Gen
variable {R : Type} (K : CRing R) (O : OrdR K)

theorem decay_step (E E' Z Z' c β : R) (hβ : O.nn β) (hβc : Le K O.toCone β c) (hZ' : O.nn Z')
    (hP : Le K O.toCone E' Z')
    (hs : Le K O.toCone (K.add E' (K.mul (K.add c c) Z')) (K.add E (K.mul β Z))) :
    Le K O.toCone (K.mul (K.add (K.add K.one β) c) (K.add E' (K.mul β Z')))
      (K.mul (K.add K.one β) (K.add E (K.mul β Z))) := by
  have one : O.nn (K.add K.one β) := O.nn_add _ _ (RatModel.nn_one' K O.toCone) hβ
  have hc : O.nn c := TruncRate.nn_of_le K O _ _ hβ hβc
  cases hs with
  | intro s hs =>
    cases hP with
    | intro p hp =>
      cases hβc with
      | intro d hd =>
        refine ⟨K.add (K.add (K.mul (K.add K.one β) s) (K.mul c p)) (K.mul (K.mul (K.add K.one β) d) Z'),
          O.nn_add _ _ (O.nn_add _ _ (O.nn_mul _ _ one hs.1) (O.nn_mul _ _ hc hp.1))
            (O.nn_mul _ _ (O.nn_mul _ _ one hd.1) hZ'), ?_⟩
        rw [← hs.2]
        have ez : Z' = K.add E' p := hp.2.symm
        have ec : c = K.add β d := hd.2.symm
        rw [ez, ec]
        lib_ring

theorem pw_nn (x : R) (hx : O.nn x) : ∀ n, O.nn (pw K x n)
  | 0 => RatModel.nn_one' K O.toCone
  | n + 1 => O.nn_mul _ _ (pw_nn x hx n) hx

theorem cfl_decay (E Z : Nat → R) (c β : R) (hβ : O.nn β) (hβc : Le K O.toCone β c) (hZ : ∀ k, O.nn (Z k))
    (hP : ∀ k, Le K O.toCone (E k) (Z k))
    (hstep : ∀ k, Le K O.toCone (K.add (E (k + 1)) (K.mul (K.add c c) (Z (k + 1)))) (K.add (E k) (K.mul β (Z k)))) :
    ∀ n, Le K O.toCone (K.mul (pw K (K.add (K.add K.one β) c) n) (K.add (E n) (K.mul β (Z n))))
      (K.mul (pw K (K.add K.one β) n) (K.add (E 0) (K.mul β (Z 0))))
  | 0 => ⟨K.zero, O.nn_zero, by show K.add (K.mul K.one _) K.zero = K.mul K.one _; lib_ring⟩
  | n + 1 => by
    have ih := cfl_decay E Z c β hβ hβc hZ hP hstep n
    have hc : O.nn c := TruncRate.nn_of_le K O _ _ hβ hβc
    have nA : O.nn (K.add (K.add K.one β) c) := O.nn_add _ _ (O.nn_add _ _ (RatModel.nn_one' K O.toCone) hβ) hc
    have nB : O.nn (K.add K.one β) := O.nn_add _ _ (RatModel.nn_one' K O.toCone) hβ
    have st := decay_step K O (E n) (E (n + 1)) (Z n) (Z (n + 1)) c β hβ hβc (hZ (n + 1)) (hP (n + 1)) (hstep n)
    have m1 := le_mul_nn K O.toCone (pw K (K.add (K.add K.one β) c) n) _ _ (pw_nn K O _ nA n) st
    have m2 := le_mul_nn K O.toCone (K.add K.one β) _ _ nB ih
    refine le_trans' K O.toCone _ _ _ ⟨K.zero, O.nn_zero, ?_⟩
      (le_trans' K O.toCone _ _ _ m1 (le_trans' K O.toCone _ _ _ ⟨K.zero, O.nn_zero, ?_⟩
        (le_trans' K O.toCone _ _ _ m2 ⟨K.zero, O.nn_zero, ?_⟩)))
    · show K.add (K.mul (K.mul (pw K (K.add (K.add K.one β) c) n) (K.add (K.add K.one β) c)) _) K.zero = _
      lib_ring
    · show K.add (K.mul (pw K (K.add (K.add K.one β) c) n) (K.mul (K.add K.one β) (K.add (E n) (K.mul β (Z n))))) K.zero
        = K.mul (K.add K.one β) (K.mul (pw K (K.add (K.add K.one β) c) n) (K.add (E n) (K.mul β (Z n))))
      lib_ring
    · show K.add (K.mul (K.add K.one β) (K.mul (pw K (K.add K.one β) n) (K.add (E 0) (K.mul β (Z 0))))) K.zero
        = K.mul (K.mul (pw K (K.add K.one β) n) (K.add K.one β)) (K.add (E 0) (K.mul β (Z 0)))
      lib_ring

end Gen

end CFLDecay

#print axioms CFLDecay.decay_step
#print axioms CFLDecay.pw_nn
#print axioms CFLDecay.cfl_decay

ns/CFLDecayG.lean

lean_all/ns/CFLDecayG.lean

/-
  ns/CFLDecayG.lean — T37 cycle 1313 (goal G787 — done): exponential energy decay for arbitrary data and fixed
  truncation |k|_∞ ≤ M + 1, over an ordered field. Under the hypotheses of CFLGlobal.cfl_global (Leray-projected,
  energy-neutral force, CFL condition β ≤ hν, E_0 + β·Z_0 ≤ E*), with V_n = E_n + β·Z_n:
    wt_ge_one        1 ≤ |k|²  for every integer mode with |k|_∞ ≥ 1
    poincare         Σ|a_r|² ≤ Σ w_r|a_r|²            (discrete Poincaré on mean-zero modes)
    cfl_decay_global (1 + β + hν)ⁿ·V_n ≤ (1 + β)ⁿ·V_0,
  so every discrete run decays geometrically at the rate (1 + β)/(1 + β + hν) per step — no smallness of the data.

  Lean 4 core plus ns/CFLGlobal, ns/CFLDecay, ns/NormCompare, ns/RatModel.
-/
import ns.CFLGlobal
import ns.CFLDecay

namespace CFLDecayG
open DetChain NSChain DyadicSmallGlobal LibRing DyadicLarge FourierGalerkin GalerkinForce ForceBound ForceSob
  ModeCount CubeCount ForceUniform NormCompare EnstStep EnstGalerkin CFLStep CFLGlobal CFLDecay

section Gen
variable {R : Type} (K : CRing R) (O : OrdR K) {I : Type}
variable (inv : R → R) (mul_inv : ∀ y, y ≠ K.zero → K.mul y (inv y) = K.one) (h10 : K.one ≠ K.zero)

theorem wt_ge_one (kz : I → Mode) (r : I) (h1 : 1 ≤ nrm (kz r)) : Le K O.toCone K.one (wt K kz r) := by
  have c1 : castN K 1 = K.one := by show K.add K.one K.zero = K.one; lib_ring
  have hm := RatModel.cast_mono K O.toCone 1 (nrm (kz r)) h1
  have hd := nrm_le_dot K O.toCone (kz r)
  refine le_trans' K O.toCone _ _ _ ?_ hd
  cases hm with
  | intro e he =>
    refine ⟨K.add (K.add e e) (K.mul e e), O.nn_add _ _ (O.nn_add _ _ he.1 he.1) (O.nn_sq e), ?_⟩
    rw [← he.2, c1]
    lib_ring

theorem poincare (l : List I) (kz : I → Mode) (h1 : ∀ p, 1 ≤ nrm (kz p)) (a : I → V3 R) :
    Le K O.toCone (lsum K (fun r => dot3 K (a r) (a r)) l) (lsum K (fun r => K.mul (wt K kz r) (dot3 K (a r) (a r))) l) := by
  refine lsum_le K O.toCone _ _ (fun r => ?_) l
  have m := le_mul_right' K O.toCone (wt_ge_one K O kz r (h1 r)) (dot3_nn K O.toCone (a r))
  exact le_trans' K O.toCone _ _ _ ⟨K.zero, O.nn_zero, by lib_ring⟩ m

include mul_inv h10 in
theorem cfl_decay_global (l : List I) (kz : I → Mode) (hd : List.Pairwise (fun a b => kz a ≠ kz b) l)
    (h1 : ∀ p, 1 ≤ nrm (kz p)) (M : Nat) (hM : ∀ p, p ∈ l → nrm (kz p) ≤ M + 1)
    (ι h ν Es : R) (hh : O.nn h) (hν : O.nn ν) (hEs : O.nn Es) (a N : Nat → I → V3 R)
    (hs : ∀ t r, Step K h ν (wt K kz r) (a (t + 1) r) (a t r) (N t r))
    (hneut : ∀ t, lsum K (fun r => dot3 K (N t r) (a t r)) l = K.zero)
    (hP : ∀ t, Le K O.toCone (lsum K (fun r => dot3 K (N t r) (N t r)) l)
      (lsum K (fun r => dot3 K (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (a t) (a t) r)
        (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (a t) (a t) r)) l))
    (hcfl : Le K O.toCone (beta K M ι h ν Es) (K.mul h ν))
    (h0 : Le K O.toCone (K.add (lsum K (fun r => dot3 K (a 0 r) (a 0 r)) l)
      (K.mul (beta K M ι h ν Es) (lsum K (fun r => K.mul (wt K kz r) (dot3 K (a 0 r) (a 0 r))) l))) Es) (n : Nat) :
    Le K O.toCone
      (K.mul (pw K (K.add (K.add K.one (beta K M ι h ν Es)) (K.mul h ν)) n)
        (K.add (lsum K (fun r => dot3 K (a n r) (a n r)) l)
          (K.mul (beta K M ι h ν Es) (lsum K (fun r => K.mul (wt K kz r) (dot3 K (a n r) (a n r))) l))))
      (K.mul (pw K (K.add K.one (beta K M ι h ν Es)) n)
        (K.add (lsum K (fun r => dot3 K (a 0 r) (a 0 r)) l)
          (K.mul (beta K M ι h ν Es) (lsum K (fun r => K.mul (wt K kz r) (dot3 K (a 0 r) (a 0 r))) l)))) := by
  have hw : ∀ r, O.nn (wt K kz r) := fun r => dot3_nn K O.toCone _
  have nZ : ∀ t, O.nn (lsum K (fun r => K.mul (wt K kz r) (dot3 K (a t r) (a t r))) l) :=
    fun t => lsum_nn K O.toCone _ (fun r => O.nn_mul _ _ (hw r) (dot3_nn K O.toCone _)) l
  have nβ : O.nn (beta K M ι h ν Es) := by
    have three : O.nn (K.add (K.add K.one K.one) K.one) := O.nn_add _ _ (O.nn_add _ _ (RatModel.nn_one' K O.toCone) (RatModel.nn_one' K O.toCone)) (RatModel.nn_one' K O.toCone)
    have two : O.nn (K.add K.one K.one) := O.nn_add _ _ (RatModel.nn_one' K O.toCone) (RatModel.nn_one' K O.toCone)
    have nc := cM_nn K O M
    exact O.nn_mul _ _ (O.nn_sq h) (O.nn_add _ _ (O.nn_mul _ _ three (O.nn_mul _ _ (O.nn_sq ι) (O.nn_mul _ _ (RatModel.nn_cast K O.toCone 135) (O.nn_mul _ _ (O.nn_sq _) hEs))))
      (O.nn_mul _ _ two (O.nn_mul _ _ (O.nn_sq ν) nc)))
  have hc : O.nn (K.mul h ν) := O.nn_mul _ _ hh hν
  -- E_t ≤ E* for every t (CFLGlobal)
  have hE : ∀ t, Le K O.toCone (lsum K (fun r => dot3 K (a t r) (a t r)) l) Es := by
    intro t
    have g := cfl_global K O inv mul_inv h10 l kz hd h1 M hM ι h ν Es hh hν hEs a N hs hneut hP hcfl h0 t
    have hS : O.nn (sumN K (fun t => lsum K (fun r => K.mul (wt K kz r) (dot3 K (a (t + 1) r) (a (t + 1) r))) l) t) :=
      NSChain.sumN_nn K O.toCone _ (fun k => nZ (k + 1)) t
    refine le_trans' K O.toCone _ _ _ ⟨K.add (K.mul (K.mul h ν) (sumN K (fun t => lsum K (fun r => K.mul (wt K kz r) (dot3 K (a (t + 1) r) (a (t + 1) r))) l) t))
        (K.mul (beta K M ι h ν Es) (lsum K (fun r => K.mul (wt K kz r) (dot3 K (a t r) (a t r))) l)),
      O.nn_add _ _ (O.nn_mul _ _ hc hS) (O.nn_mul _ _ nβ (nZ t)), ?_⟩ (le_trans' K O.toCone _ _ _ g h0)
    lib_ring
  exact cfl_decay K O (fun t => lsum K (fun r => dot3 K (a t r) (a t r)) l)
    (fun t => lsum K (fun r => K.mul (wt K kz r) (dot3 K (a t r) (a t r))) l) (K.mul h ν) (beta K M ι h ν Es) nβ hcfl nZ
    (fun t => poincare K O l kz h1 (a t))
    (fun t => cfl_step K O inv mul_inv h10 l kz hd h1 M hM ι h ν Es hh hν (a (t + 1)) (a t) (N t) (hs t) (hneut t) (hP t) (hE t))
    n

end Gen

end CFLDecayG

#print axioms CFLDecayG.wt_ge_one
#print axioms CFLDecayG.poincare
#print axioms CFLDecayG.cfl_decay_global

ns/CFLEnergy.lean

lean_all/ns/CFLEnergy.lean

/-
  ns/CFLEnergy.lean — T37 cycle 1215 (goal G775a): the abstract energy induction behind a global, arbitrary-data
  energy bound for the implicit-viscosity scheme under a CFL condition, over an ordered ring. Suppose that whenever
  E_k ≤ E*,
      E_{k+1} + 2c·Z_{k+1} ≤ E_k + β·Z_k          (EnergyStep with the force error absorbed by the CFL bound)
  with 0 ≤ β ≤ c and E_0 + β·Z_0 ≤ E*. Then for every n
    cfl_energy   E_n + c·Σ_{k<n} Z_{k+1} + β·Z_n ≤ E_0 + β·Z_0,
  so E_n ≤ E* for all n and the time-integrated enstrophy is bounded — the a priori bounds of Leray's weak solutions,
  for the truncated scheme and arbitrary data (no smallness), once h is small for the truncation.

  Lean 4 core plus NSChain (sumN), lib/RingTac.
-/
import ns.TimeLipN
import ns.TruncRate

namespace CFLEnergy
open DetChain NSChain DyadicSmallGlobal LibRing DyadicLarge FourierGalerkin ForceBound

section Gen
variable {R : Type} (K : CRing R) (O : OrdR K)

theorem cfl_energy (E Z : Nat → R) (c β Es : R) (hβ : O.nn β) (hβc : Le K O.toCone β c) (hZ : ∀ k, O.nn (Z k))
    (hstep : ∀ k, Le K O.toCone (E k) Es →
      Le K O.toCone (K.add (E (k + 1)) (K.mul (K.add c c) (Z (k + 1)))) (K.add (E k) (K.mul β (Z k))))
    (h0 : Le K O.toCone (K.add (E 0) (K.mul β (Z 0))) Es) : ∀ n,
    Le K O.toCone (K.add (K.add (E n) (K.mul c (sumN K (fun k => Z (k + 1)) n))) (K.mul β (Z n)))
      (K.add (E 0) (K.mul β (Z 0)))
  | 0 => ⟨K.zero, O.nn_zero, by show K.add (K.add (K.add (E 0) (K.mul c K.zero)) (K.mul β (Z 0))) K.zero = _; lib_ring⟩
  | n + 1 => by
    have ih := cfl_energy E Z c β Es hβ hβc hZ hstep h0 n
    -- E_n ≤ E* from the induction hypothesis
    have hEn : Le K O.toCone (E n) Es := by
      refine le_trans' K O.toCone _ _ _ ⟨K.add (K.mul c (sumN K (fun k => Z (k + 1)) n)) (K.mul β (Z n)), ?_, ?_⟩ (le_trans' K O.toCone _ _ _ ih h0)
      · have hc : O.nn c := TruncRate.nn_of_le K O _ _ hβ hβc
        have hs : O.nn (sumN K (fun k => Z (k + 1)) n) := NSChain.sumN_nn K O.toCone (fun k => Z (k + 1)) (fun k => hZ (k + 1)) n
        exact O.nn_add _ _ (O.nn_mul _ _ hc hs) (O.nn_mul _ _ hβ (hZ n))
      · lib_ring
    have st := hstep n hEn
    -- (β − c)·Z_{n+1} ≤ 0
    cases hβc with
    | intro d hd =>
      -- c = β + d
      have ec : c = K.add β d := hd.2.symm
      refine le_trans' K O.toCone _ _ _ ⟨K.mul d (Z (n + 1)), O.nn_mul _ _ hd.1 (hZ (n + 1)), ?_⟩
        (le_trans' K O.toCone _ _ _ (le_add' K O.toCone _ _ _ _ st (le_refl' K O.toCone (K.mul c (sumN K (fun k => Z (k + 1)) n)))) ?_)
      · show K.add (K.add (K.add (E (n + 1)) (K.mul c (K.add (sumN K (fun k => Z (k + 1)) n) (Z (n + 1))))) (K.mul β (Z (n + 1)))) (K.mul d (Z (n + 1)))
          = K.add (K.add (E (n + 1)) (K.mul (K.add c c) (Z (n + 1)))) (K.mul c (sumN K (fun k => Z (k + 1)) n))
        rw [ec]; lib_ring
      · refine le_trans' K O.toCone _ _ _ ⟨K.zero, O.nn_zero, ?_⟩ ih
        lib_ring

end Gen

end CFLEnergy

#print axioms CFLEnergy.cfl_energy

ns/CFLGlobal.lean

lean_all/ns/CFLGlobal.lean

/-
  ns/CFLGlobal.lean — T37 cycle 1219 (goal G775 — done): global discrete energy bound for arbitrary data, fixed
  truncation |k|_∞ ≤ M + 1, under a CFL condition, over an ordered field. With E_t = Σ|a_t|², Z_t = Σ w|a_t|²,
  β = h²(3ι²·135·c_M²·E* + 2ν²·c_M), c_M = 3(M+1)²: if the force is energy-neutral and Leray-projected at every step,
  β ≤ hν, and E_0 + β·Z_0 ≤ E*, then for every n
    cfl_global   E_n + hν·Σ_{t<n} Z_{t+1} + β·Z_n ≤ E_0 + β·Z_0
  — the a priori bounds of Leray's weak solutions (L^∞L² and L²H¹) for the truncated implicit scheme, every datum.

  Lean 4 core plus ns/CFLStep, ns/CFLEnergy.
-/
import ns.CFLStep
import ns.CFLEnergy

namespace CFLGlobal
open DetChain NSChain DyadicSmallGlobal LibRing DyadicLarge FourierGalerkin GalerkinForce ForceBound ForceSob
  ModeCount CubeCount ForceUniform NormCompare EnstStep EnstGalerkin EnstDiff WeightH2 EnergyStep CFLStep CFLEnergy

section Gen
variable {R : Type} (K : CRing R) (O : OrdR K) {I : Type}
variable (inv : R → R) (mul_inv : ∀ y, y ≠ K.zero → K.mul y (inv y) = K.one) (h10 : K.one ≠ K.zero)

def beta (M : Nat) (ι h ν Es : R) : R :=
  K.mul (K.mul h h) (K.add (K.mul (K.add (K.add K.one K.one) K.one) (K.mul (K.mul ι ι) (K.mul (castN K 135) (K.mul (K.mul (cM K M) (cM K M)) Es))))
    (K.mul (K.add K.one K.one) (K.mul (K.mul ν ν) (cM K M))))

include mul_inv h10 in
theorem cfl_global (l : List I) (kz : I → Mode) (hd : List.Pairwise (fun a b => kz a ≠ kz b) l)
    (h1 : ∀ p, 1 ≤ nrm (kz p)) (M : Nat) (hM : ∀ p, p ∈ l → nrm (kz p) ≤ M + 1)
    (ι h ν Es : R) (hh : O.nn h) (hν : O.nn ν) (hEs : O.nn Es) (a N : Nat → I → V3 R)
    (hs : ∀ t r, Step K h ν (wt K kz r) (a (t + 1) r) (a t r) (N t r))
    (hneut : ∀ t, lsum K (fun r => dot3 K (N t r) (a t r)) l = K.zero)
    (hP : ∀ t, Le K O.toCone (lsum K (fun r => dot3 K (N t r) (N t r)) l)
      (lsum K (fun r => dot3 K (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (a t) (a t) r)
        (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (a t) (a t) r)) l))
    (hcfl : Le K O.toCone (beta K M ι h ν Es) (K.mul h ν))
    (h0 : Le K O.toCone (K.add (lsum K (fun r => dot3 K (a 0 r) (a 0 r)) l)
      (K.mul (beta K M ι h ν Es) (lsum K (fun r => K.mul (wt K kz r) (dot3 K (a 0 r) (a 0 r))) l))) Es) (n : Nat) :
    Le K O.toCone (K.add (K.add (lsum K (fun r => dot3 K (a n r) (a n r)) l)
        (K.mul (K.mul h ν) (sumN K (fun t => lsum K (fun r => K.mul (wt K kz r) (dot3 K (a (t + 1) r) (a (t + 1) r))) l) n)))
        (K.mul (beta K M ι h ν Es) (lsum K (fun r => K.mul (wt K kz r) (dot3 K (a n r) (a n r))) l)))
      (K.add (lsum K (fun r => dot3 K (a 0 r) (a 0 r)) l)
        (K.mul (beta K M ι h ν Es) (lsum K (fun r => K.mul (wt K kz r) (dot3 K (a 0 r) (a 0 r))) l))) := by
  have hw : ∀ r, O.nn (wt K kz r) := fun r => dot3_nn K O.toCone _
  have nβ : O.nn (beta K M ι h ν Es) := by
    have three : O.nn (K.add (K.add K.one K.one) K.one) := O.nn_add _ _ (O.nn_add _ _ (RatModel.nn_one' K O.toCone) (RatModel.nn_one' K O.toCone)) (RatModel.nn_one' K O.toCone)
    have two : O.nn (K.add K.one K.one) := O.nn_add _ _ (RatModel.nn_one' K O.toCone) (RatModel.nn_one' K O.toCone)
    have nc := cM_nn K O M
    exact O.nn_mul _ _ (O.nn_sq h) (O.nn_add _ _ (O.nn_mul _ _ three (O.nn_mul _ _ (O.nn_sq ι) (O.nn_mul _ _ (RatModel.nn_cast K O.toCone 135) (O.nn_mul _ _ (O.nn_sq _) hEs))))
      (O.nn_mul _ _ two (O.nn_mul _ _ (O.nn_sq ν) nc)))
  exact cfl_energy K O (fun t => lsum K (fun r => dot3 K (a t r) (a t r)) l)
    (fun t => lsum K (fun r => K.mul (wt K kz r) (dot3 K (a t r) (a t r))) l) (K.mul h ν) (beta K M ι h ν Es) Es nβ hcfl
    (fun t => lsum_nn K O.toCone _ (fun r => O.nn_mul _ _ (hw r) (dot3_nn K O.toCone _)) l)
    (fun t hE => cfl_step K O inv mul_inv h10 l kz hd h1 M hM ι h ν Es hh hν (a (t + 1)) (a t) (N t) (hs t) (hneut t) (hP t) hE)
    h0 n

end Gen

end CFLGlobal

#print axioms CFLGlobal.cfl_global

ns/CFLRate.lean

lean_all/ns/CFLRate.lean

/-
  ns/CFLRate.lean — T37 cycle 1315 (goal G788 — done): a decay rate independent of β, over an ordered field.
  With V_k = E_k + β·Z_k, the CFL step E_{k+1} + 2c·Z_{k+1} ≤ E_k + β·Z_k, Poincaré E ≤ Z and 0 ≤ β ≤ c, β ≤ 1:
    rate_step         (2 + c)·V_{k+1} ≤ 2·V_k          (witness 2s + c·p + Z'(β(1−β) + (c−β)(3−β)))
    cfl_rate          (2 + c)ⁿ·V_n ≤ 2ⁿ·V_0
    cfl_rate_global   for the implicit Fourier–Galerkin scheme under the cfl_global hypotheses and hν ≤ 1:
                      (2 + hν)ⁿ·V_n ≤ 2ⁿ·V_0,
  i.e. V(t) ≤ (1 + hν/2)^{−t/h}·V(0) → e^{−νt/2}·V(0): every datum, fixed truncation, rate ν/2 per unit time.

  Lean 4 core plus ns/CFLDecayG (poincare, cfl_global, cfl_step), ns/CFLDecay (pw_nn), lib/RingTac.
-/
import ns.CFLDecayG

namespace CFLRate
open DetChain NSChain DyadicSmallGlobal LibRing DyadicLarge FourierGalerkin GalerkinForce ForceBound ForceSob
  ModeCount CubeCount ForceUniform NormCompare EnstStep EnstGalerkin CFLStep CFLGlobal CFLDecay CFLDecayG

section Gen
variable {R : Type} (K : CRing R) (O : OrdR K) {I : Type}
variable (inv : R → R) (mul_inv : ∀ y, y ≠ K.zero → K.mul y (inv y) = K.one) (h10 : K.one ≠ K.zero)

theorem rate_step (E E' Z Z' c β : R) (hβ : O.nn β) (hβc : Le K O.toCone β c) (hβ1 : Le K O.toCone β K.one)
    (hZ' : O.nn Z') (hP : Le K O.toCone E' Z')
    (hs : Le K O.toCone (K.add E' (K.mul (K.add c c) Z')) (K.add E (K.mul β Z))) :
    Le K O.toCone (K.mul (K.add (K.add K.one K.one) c) (K.add E' (K.mul β Z')))
      (K.mul (K.add K.one K.one) (K.add E (K.mul β Z))) := by
  have hc : O.nn c := TruncRate.nn_of_le K O _ _ hβ hβc
  have two : O.nn (K.add K.one K.one) := O.nn_add _ _ (RatModel.nn_one' K O.toCone) (RatModel.nn_one' K O.toCone)
  cases hs with
  | intro s hs =>
    cases hP with
    | intro p hp =>
      cases hβc with
      | intro d hd =>
        cases hβ1 with
        | intro b hb =>
          refine ⟨K.add (K.add (K.mul (K.add K.one K.one) s) (K.mul c p))
              (K.mul Z' (K.add (K.mul β b) (K.mul d (K.add (K.add K.one K.one) b)))),
            O.nn_add _ _ (O.nn_add _ _ (O.nn_mul _ _ two hs.1) (O.nn_mul _ _ hc hp.1))
              (O.nn_mul _ _ hZ' (O.nn_add _ _ (O.nn_mul _ _ hβ hb.1) (O.nn_mul _ _ hd.1 (O.nn_add _ _ two hb.1)))), ?_⟩
          rw [← hs.2]
          have ez : Z' = K.add E' p := hp.2.symm
          have ec : c = K.add β d := hd.2.symm
          have eb : K.add β b = K.one := hb.2
          rw [ez, ec]
          -- the defect equals Z'·c·(β + b − 1), which vanishes since β + b = 1
          calc _ = K.add (K.mul (K.add (K.add K.one K.one) (K.add β d)) (K.add E' (K.mul β (K.add E' p))))
                (K.add (K.add (K.mul (K.add K.one K.one) s) (K.mul (K.add β d) p))
                  (K.mul (K.add E' p) (K.add (K.mul β b) (K.mul d (K.add (K.add K.one K.one) b))))) := rfl
            _ = K.add (K.mul (K.add K.one K.one) (K.add (K.add E' (K.mul (K.add (K.add β d) (K.add β d)) (K.add E' p))) s))
                (K.mul (K.mul (K.add E' p) (K.add β d)) (K.add (K.add β b) (K.neg K.one))) := by lib_ring
            _ = _ := by rw [eb]; lib_ring

theorem cfl_rate (E Z : Nat → R) (c β : R) (hβ : O.nn β) (hβc : Le K O.toCone β c) (hβ1 : Le K O.toCone β K.one)
    (hZ : ∀ k, O.nn (Z k)) (hP : ∀ k, Le K O.toCone (E k) (Z k))
    (hstep : ∀ k, Le K O.toCone (K.add (E (k + 1)) (K.mul (K.add c c) (Z (k + 1)))) (K.add (E k) (K.mul β (Z k)))) :
    ∀ n, Le K O.toCone (K.mul (pw K (K.add (K.add K.one K.one) c) n) (K.add (E n) (K.mul β (Z n))))
      (K.mul (pw K (K.add K.one K.one) n) (K.add (E 0) (K.mul β (Z 0))))
  | 0 => ⟨K.zero, O.nn_zero, by show K.add (K.mul K.one _) K.zero = K.mul K.one _; lib_ring⟩
  | n + 1 => by
    have ih := cfl_rate E Z c β hβ hβc hβ1 hZ hP hstep n
    have hc : O.nn c := TruncRate.nn_of_le K O _ _ hβ hβc
    have two : O.nn (K.add K.one K.one) := O.nn_add _ _ (RatModel.nn_one' K O.toCone) (RatModel.nn_one' K O.toCone)
    have nA : O.nn (K.add (K.add K.one K.one) c) := O.nn_add _ _ two hc
    have st := rate_step K O (E n) (E (n + 1)) (Z n) (Z (n + 1)) c β hβ hβc hβ1 (hZ (n + 1)) (hP (n + 1)) (hstep n)
    have m1 := le_mul_nn K O.toCone (pw K (K.add (K.add K.one K.one) c) n) _ _ (pw_nn K O _ nA n) st
    have m2 := le_mul_nn K O.toCone (K.add K.one K.one) _ _ two ih
    refine le_trans' K O.toCone _ _ _ ⟨K.zero, O.nn_zero, ?_⟩
      (le_trans' K O.toCone _ _ _ m1 (le_trans' K O.toCone _ _ _ ⟨K.zero, O.nn_zero, ?_⟩
        (le_trans' K O.toCone _ _ _ m2 ⟨K.zero, O.nn_zero, ?_⟩)))
    · show K.add (K.mul (K.mul (pw K (K.add (K.add K.one K.one) c) n) (K.add (K.add K.one K.one) c)) _) K.zero = _
      lib_ring
    · show K.add (K.mul (pw K (K.add (K.add K.one K.one) c) n) (K.mul (K.add K.one K.one) (K.add (E n) (K.mul β (Z n))))) K.zero
        = K.mul (K.add K.one K.one) (K.mul (pw K (K.add (K.add K.one K.one) c) n) (K.add (E n) (K.mul β (Z n))))
      lib_ring
    · show K.add (K.mul (K.add K.one K.one) (K.mul (pw K (K.add K.one K.one) n) (K.add (E 0) (K.mul β (Z 0))))) K.zero
        = K.mul (K.mul (pw K (K.add K.one K.one) n) (K.add K.one K.one)) (K.add (E 0) (K.mul β (Z 0)))
      lib_ring

include mul_inv h10 in
theorem cfl_rate_global (l : List I) (kz : I → Mode) (hd : List.Pairwise (fun a b => kz a ≠ kz b) l)
    (h1 : ∀ p, 1 ≤ nrm (kz p)) (M : Nat) (hM : ∀ p, p ∈ l → nrm (kz p) ≤ M + 1)
    (ι h ν Es : R) (hh : O.nn h) (hν : O.nn ν) (hEs : O.nn Es) (hhν : Le K O.toCone (K.mul h ν) K.one)
    (a N : Nat → I → V3 R)
    (hs : ∀ t r, Step K h ν (wt K kz r) (a (t + 1) r) (a t r) (N t r))
    (hneut : ∀ t, lsum K (fun r => dot3 K (N t r) (a t r)) l = K.zero)
    (hP : ∀ t, Le K O.toCone (lsum K (fun r => dot3 K (N t r) (N t r)) l)
      (lsum K (fun r => dot3 K (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (a t) (a t) r)
        (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (a t) (a t) r)) l))
    (hcfl : Le K O.toCone (beta K M ι h ν Es) (K.mul h ν))
    (h0 : Le K O.toCone (K.add (lsum K (fun r => dot3 K (a 0 r) (a 0 r)) l)
      (K.mul (beta K M ι h ν Es) (lsum K (fun r => K.mul (wt K kz r) (dot3 K (a 0 r) (a 0 r))) l))) Es) (n : Nat) :
    Le K O.toCone
      (K.mul (pw K (K.add (K.add K.one K.one) (K.mul h ν)) n)
        (K.add (lsum K (fun r => dot3 K (a n r) (a n r)) l)
          (K.mul (beta K M ι h ν Es) (lsum K (fun r => K.mul (wt K kz r) (dot3 K (a n r) (a n r))) l))))
      (K.mul (pw K (K.add K.one K.one) n)
        (K.add (lsum K (fun r => dot3 K (a 0 r) (a 0 r)) l)
          (K.mul (beta K M ι h ν Es) (lsum K (fun r => K.mul (wt K kz r) (dot3 K (a 0 r) (a 0 r))) l)))) := by
  have hw : ∀ r, O.nn (wt K kz r) := fun r => dot3_nn K O.toCone _
  have nZ : ∀ t, O.nn (lsum K (fun r => K.mul (wt K kz r) (dot3 K (a t r) (a t r))) l) :=
    fun t => lsum_nn K O.toCone _ (fun r => O.nn_mul _ _ (hw r) (dot3_nn K O.toCone _)) l
  have nβ : O.nn (beta K M ι h ν Es) := by
    have three : O.nn (K.add (K.add K.one K.one) K.one) := O.nn_add _ _ (O.nn_add _ _ (RatModel.nn_one' K O.toCone) (RatModel.nn_one' K O.toCone)) (RatModel.nn_one' K O.toCone)
    have two : O.nn (K.add K.one K.one) := O.nn_add _ _ (RatModel.nn_one' K O.toCone) (RatModel.nn_one' K O.toCone)
    have nc := cM_nn K O M
    exact O.nn_mul _ _ (O.nn_sq h) (O.nn_add _ _ (O.nn_mul _ _ three (O.nn_mul _ _ (O.nn_sq ι) (O.nn_mul _ _ (RatModel.nn_cast K O.toCone 135) (O.nn_mul _ _ (O.nn_sq _) hEs))))
      (O.nn_mul _ _ two (O.nn_mul _ _ (O.nn_sq ν) nc)))
  have hc : O.nn (K.mul h ν) := O.nn_mul _ _ hh hν
  have hE : ∀ t, Le K O.toCone (lsum K (fun r => dot3 K (a t r) (a t r)) l) Es := by
    intro t
    have g := cfl_global K O inv mul_inv h10 l kz hd h1 M hM ι h ν Es hh hν hEs a N hs hneut hP hcfl h0 t
    have hS : O.nn (sumN K (fun t => lsum K (fun r => K.mul (wt K kz r) (dot3 K (a (t + 1) r) (a (t + 1) r))) l) t) :=
      NSChain.sumN_nn K O.toCone _ (fun k => nZ (k + 1)) t
    refine le_trans' K O.toCone _ _ _ ⟨K.add (K.mul (K.mul h ν) (sumN K (fun t => lsum K (fun r => K.mul (wt K kz r) (dot3 K (a (t + 1) r) (a (t + 1) r))) l) t))
        (K.mul (beta K M ι h ν Es) (lsum K (fun r => K.mul (wt K kz r) (dot3 K (a t r) (a t r))) l)),
      O.nn_add _ _ (O.nn_mul _ _ hc hS) (O.nn_mul _ _ nβ (nZ t)), ?_⟩ (le_trans' K O.toCone _ _ _ g h0)
    lib_ring
  exact cfl_rate K O (fun t => lsum K (fun r => dot3 K (a t r) (a t r)) l)
    (fun t => lsum K (fun r => K.mul (wt K kz r) (dot3 K (a t r) (a t r))) l) (K.mul h ν) (beta K M ι h ν Es) nβ hcfl
    (le_trans' K O.toCone _ _ _ hcfl hhν) nZ
    (fun t => poincare K O l kz h1 (a t))
    (fun t => cfl_step K O inv mul_inv h10 l kz hd h1 M hM ι h ν Es hh hν (a (t + 1)) (a t) (N t) (hs t) (hneut t) (hP t) (hE t))
    n

end Gen

end CFLRate

#print axioms CFLRate.rate_step
#print axioms CFLRate.cfl_rate
#print axioms CFLRate.cfl_rate_global

ns/CFLStep.lean

lean_all/ns/CFLStep.lean

/-
  ns/CFLStep.lean — T37 cycle 1217 (goal G775b): the CFL energy step of the implicit-viscosity Galerkin scheme for a
  fixed truncation |k|_∞ ≤ M + 1 and arbitrary data, over an ordered field. With c_M = 3(M+1)²:
    wt_le      w_r ≤ c_M on the modes;  A ≤ c_M·Z,  Z ≤ c_M·E
    cfl_step   if E(a) ≤ E*, the force is energy-neutral at a and Σ|N|² ≤ Σ|Nl(a,a)|², then
               E(a') + 2hν·Z(a') ≤ E(a) + β·Z(a),   β = h²·(3ι²·135·c_M²·E* + 2ν²·c_M)
  (EnergyStep, ForceUniform with NormCompare.nrm4_le). With CFLEnergy.cfl_energy (c = hν, condition β ≤ hν, i.e.
  h·(3ι²·135·c_M²E* + 2ν²c_M) ≤ ν): a global energy bound for every initial datum.

  Lean 4 core plus ns/EnergyStep, ns/ForceUniform, ns/WeightH2, ns/NormCompare, ns/EnstGalerkin, ns/TruncRate.
-/
import ns.EnergyStep
import ns.WeightH2
import ns.EnstGalerkin
import ns.TruncRate

namespace CFLStep
open DetChain NSChain DyadicSmallGlobal LibRing DyadicLarge FourierGalerkin GalerkinForce ForceBound ForceSob
  ModeCount CubeCount ForceUniform NormCompare EnstStep EnstGalerkin EnstDiff WeightH2 EnergyStep

section Gen
variable {R : Type} (K : CRing R) (O : OrdR K) {I : Type}
variable (inv : R → R) (mul_inv : ∀ y, y ≠ K.zero → K.mul y (inv y) = K.one) (h10 : K.one ≠ K.zero)

def cM (M : Nat) : R := K.add (K.add (K.mul (castN K (M + 1)) (castN K (M + 1))) (K.mul (castN K (M + 1)) (castN K (M + 1))))
  (K.mul (castN K (M + 1)) (castN K (M + 1)))

theorem cM_nn (M : Nat) : O.nn (cM K M) :=
  O.nn_add _ _ (O.nn_add _ _ (O.nn_sq _) (O.nn_sq _)) (O.nn_sq _)

theorem lsum_le_mem (f g : I → R) : ∀ l : List I, (∀ r, r ∈ l → Le K O.toCone (f r) (g r)) → Le K O.toCone (lsum K f l) (lsum K g l)
  | [], _ => le_refl' K O.toCone _
  | x :: l, h => le_add' K O.toCone _ _ _ _ (h x (List.Mem.head _)) (lsum_le_mem f g l (fun r hr => h r (List.Mem.tail _ hr)))

theorem wt_le (kz : I → Mode) (M : Nat) (r : I) (h : nrm (kz r) ≤ M + 1) : Le K O.toCone (wt K kz r) (cM K M) := by
  have d := dot_le_3nrm K O (kz r)
  have c := RatModel.cast_mono K O.toCone _ _ h
  have s := EnstLocal.sq_mono K O _ _ (RatModel.nn_cast K O.toCone _) (RatModel.nn_cast K O.toCone _) c
  exact le_trans' K O.toCone _ _ _ d (le_add' K O.toCone _ _ _ _ (le_add' K O.toCone _ _ _ _ s s) s)

theorem A_le_Z (l : List I) (kz : I → Mode) (M : Nat) (hM : ∀ p, p ∈ l → nrm (kz p) ≤ M + 1) (a : I → V3 R) :
    Le K O.toCone (lsum K (fun r => K.mul (K.mul (wt K kz r) (wt K kz r)) (dot3 K (a r) (a r))) l)
      (K.mul (cM K M) (lsum K (fun r => K.mul (wt K kz r) (dot3 K (a r) (a r))) l)) := by
  rw [← lsum_mul_left]
  apply lsum_le_mem K O
  intro r hr
  have hwr : O.nn (wt K kz r) := dot3_nn K O.toCone _
  have m := ForceSob.le_mul_right' K O.toCone (wt_le K O kz M r (hM r hr)) (O.nn_mul _ _ hwr (dot3_nn K O.toCone (a r)))
  exact le_trans' K O.toCone _ _ _ ⟨K.zero, O.nn_zero, by lib_ring⟩ (le_trans' K O.toCone _ _ _ m ⟨K.zero, O.nn_zero, by lib_ring⟩)

theorem Z_le_E (l : List I) (kz : I → Mode) (M : Nat) (hM : ∀ p, p ∈ l → nrm (kz p) ≤ M + 1) (a : I → V3 R) :
    Le K O.toCone (lsum K (fun r => K.mul (wt K kz r) (dot3 K (a r) (a r))) l)
      (K.mul (cM K M) (lsum K (fun r => dot3 K (a r) (a r)) l)) := by
  rw [← lsum_mul_left]
  apply lsum_le_mem K O
  intro r hr
  exact ForceSob.le_mul_right' K O.toCone (wt_le K O kz M r (hM r hr)) (dot3_nn K O.toCone (a r))

include mul_inv h10 in
theorem cfl_step (l : List I) (kz : I → Mode) (hd : List.Pairwise (fun a b => kz a ≠ kz b) l)
    (h1 : ∀ p, 1 ≤ nrm (kz p)) (M : Nat) (hM : ∀ p, p ∈ l → nrm (kz p) ≤ M + 1)
    (ι h ν Es : R) (hh : O.nn h) (hν : O.nn ν) (a' a N : I → V3 R)
    (hs : ∀ r, Step K h ν (wt K kz r) (a' r) (a r) (N r))
    (hneut : lsum K (fun r => dot3 K (N r) (a r)) l = K.zero)
    (hP : Le K O.toCone (lsum K (fun r => dot3 K (N r) (N r)) l)
      (lsum K (fun r => dot3 K (Nl K l (TriadSparse.delta K kz) ι (kv K kz) a a r) (Nl K l (TriadSparse.delta K kz) ι (kv K kz) a a r)) l))
    (hE : Le K O.toCone (lsum K (fun r => dot3 K (a r) (a r)) l) Es) :
    Le K O.toCone (K.add (lsum K (fun r => dot3 K (a' r) (a' r)) l) (K.mul (K.add (K.mul h ν) (K.mul h ν)) (lsum K (fun r => K.mul (wt K kz r) (dot3 K (a' r) (a' r))) l)))
      (K.add (lsum K (fun r => dot3 K (a r) (a r)) l)
        (K.mul (K.mul (K.mul h h) (K.add (K.mul (K.add (K.add K.one K.one) K.one) (K.mul (K.mul ι ι) (K.mul (castN K 135) (K.mul (K.mul (cM K M) (cM K M)) Es))))
          (K.mul (K.add K.one K.one) (K.mul (K.mul ν ν) (cM K M))))) (lsum K (fun r => K.mul (wt K kz r) (dot3 K (a r) (a r))) l))) := by
  have hw : ∀ r, O.nn (wt K kz r) := fun r => dot3_nn K O.toCone _
  have es := energy_step K O.toCone l h ν hh hν (wt K kz) hw a' a N hs hneut
  have fu := force_uniform K O inv mul_inv h10 l kz hd h1 M hM ι (kv K kz) a a
  have hH := lsum_le K O.toCone _ _ (fun p => le_mul_right' K O.toCone (nrm4_le K O.toCone (kz p)) (dot3_nn K O.toCone (a p))) l
  have hA := A_le_Z K O l kz M hM a
  have hZ := Z_le_E K O l kz M hM a
  have nZ : O.nn (lsum K (fun r => K.mul (wt K kz r) (dot3 K (a r) (a r))) l) :=
    lsum_nn K O.toCone _ (fun r => O.nn_mul _ _ (hw r) (dot3_nn K O.toCone _)) l
  have nA : O.nn (lsum K (fun r => K.mul (K.mul (wt K kz r) (wt K kz r)) (dot3 K (a r) (a r))) l) :=
    lsum_nn K O.toCone _ (fun r => O.nn_mul _ _ (O.nn_sq _) (dot3_nn K O.toCone _)) l
  have ncM := cM_nn K O M
  -- Σ|N|² ≤ ι²·A·(135·Z) ≤ ι²·(c_M Z)·(135·c_M E*)
  have hZE : Le K O.toCone (lsum K (fun r => K.mul (wt K kz r) (dot3 K (a r) (a r))) l) (K.mul (cM K M) Es) :=
    le_trans' K O.toCone _ _ _ hZ (le_mul_nn K O.toCone _ _ _ ncM hE)
  have n1 : Le K O.toCone (lsum K (fun r => dot3 K (N r) (N r)) l)
      (K.mul (K.mul ι ι) (K.mul (K.mul (cM K M) (lsum K (fun r => K.mul (wt K kz r) (dot3 K (a r) (a r))) l)) (K.mul (castN K 135) (K.mul (cM K M) Es)))) := by
    refine le_trans' K O.toCone _ _ _ hP (le_trans' K O.toCone _ _ _ fu (le_mul_nn K O.toCone _ _ _ (O.nn_sq ι) ?_))
    exact TruncRate.mm K O _ _ _ _ (lsum_nn K O.toCone _ (fun p => O.nn_mul _ _ (O.nn_sq _) (dot3_nn K O.toCone _)) l)
      (O.nn_mul _ _ (RatModel.nn_cast K O.toCone 135) (O.nn_mul _ _ ncM (TruncRate.nn_of_le K O _ _ (lsum_nn K O.toCone _ (fun r => dot3_nn K O.toCone _) l) hE)))
      (le_trans' K O.toCone _ _ _ hH hA) (le_mul_nn K O.toCone _ _ _ (RatModel.nn_cast K O.toCone 135) hZE)
  have three : O.nn (K.add (K.add K.one K.one) K.one) := O.nn_add _ _ (O.nn_add _ _ (RatModel.nn_one' K O.toCone) (RatModel.nn_one' K O.toCone)) (RatModel.nn_one' K O.toCone)
  have two : O.nn (K.add K.one K.one) := O.nn_add _ _ (RatModel.nn_one' K O.toCone) (RatModel.nn_one' K O.toCone)
  have err := le_add' K O.toCone _ _ _ _ (le_mul_nn K O.toCone _ _ _ (O.nn_mul _ _ three (O.nn_sq h)) n1)
    (le_mul_nn K O.toCone _ _ _ (O.nn_mul _ _ two (O.nn_mul _ _ (O.nn_sq h) (O.nn_sq ν))) hA)
  refine le_trans' K O.toCone _ _ _ es (le_trans' K O.toCone _ _ _ (le_add' K O.toCone _ _ _ _ (le_refl' K O.toCone _) err) ⟨K.zero, O.nn_zero, ?_⟩)
  generalize lsum K (fun r => dot3 K (a r) (a r)) l = E
  generalize lsum K (fun r => K.mul (wt K kz r) (dot3 K (a r) (a r))) l = Z
  generalize cM K M = c
  generalize castN K 135 = d
  lib_ring

end Gen

end CFLStep

#print axioms CFLStep.cM_nn
#print axioms CFLStep.lsum_le_mem
#print axioms CFLStep.wt_le
#print axioms CFLStep.A_le_Z
#print axioms CFLStep.Z_le_E
#print axioms CFLStep.cfl_step

ns/CNSerrin.lean

lean_all/ns/CNSerrin.lean

/-
  ns/CNSerrin.lean — T37 cycle 218 (goal G1, step 5): the discrete Prodi–Serrin bound for a Crank–Nicolson run of a
  3D Galerkin truncation, from the step equations and the transfer bound.

  Lean 4 core plus ns/SerrinMid, ns/MidpointStep, ns/Midpoint, ns/LocalCont (le_of_mul_le), lib/OField. For each time
  t: modes i ∈ l, a_{t+1,i} − a_{t,i} = h(−ν k_i s_i + N_{t,i}), s = a_{t+1} + a_t, weights w ≥ 0,
  σ_t² = Σ w k s², ρ_t² = Σ w s², and the transfer bound h Σ w s N_t ≤ h c X_t ρ_t σ_t (TriadLinf.stretch). Then
  with b_t = (2ν)⁻¹ h c² X_t², 0 ≤ b_t ≤ 1 and S_N = Σ_{t<N} b_t ≤ 1:
      Z_N (1 − S_N)² ≤ Z_0,   Z_t = Σ_i w_i a_{t,i}²                                   (cn_serrin)
-/
import ns.SerrinMid
import ns.MidpointStep
import ns.Midpoint
import ns.LocalCont

namespace CNSerrin
open DetChain NSChain LibRing LibCS LibGron LibOF SerrinDiscrete SerrinSum SerrinMid MidpointStep Midpoint
  EnstrophyStep LocalCont

variable {R : Type} (K : CRing R) {I : Type}

def bk (P : Pos K) (F : OF K P) (h c ν : R) (X : Nat → R) (t : Nat) : R :=
  K.mul (F.inv (K.add ν ν)) (K.mul h (K.mul (K.mul c c) (K.mul (X t) (X t))))

theorem cn_serrin (P : Pos K) (F : OF K P) (w k : I → R) (a Nn : Nat → I → R) (X ρ σ : Nat → R) (h c ν : R)
    (l : List I) (hw : ∀ i, P.nn (w i)) (hh : P.nn h) (hν : P.nn ν) (hν0 : K.add ν ν ≠ K.zero)
    (h4 : K.add (K.add ν ν) (K.add ν ν) ≠ K.zero)
    (step : ∀ t i, sub K (a (t + 1) i) (a t i)
      = K.mul h (K.add (K.neg (K.mul ν (K.mul (k i) (K.add (a (t + 1) i) (a t i))))) (Nn t i)))
    (hσ : ∀ t, K.mul (σ t) (σ t)
      = lsum K (fun i => K.mul (w i) (K.mul (k i) (K.mul (K.add (a (t + 1) i) (a t i)) (K.add (a (t + 1) i) (a t i))))) l)
    (hρ : ∀ t, K.mul (ρ t) (ρ t) = lsum K (fun i => K.mul (w i) (K.mul (K.add (a (t + 1) i) (a t i)) (K.add (a (t + 1) i) (a t i)))) l)
    (hT : ∀ t, le K P (K.mul h (lsum K (fun i => K.mul (w i) (K.mul (K.add (a (t + 1) i) (a t i)) (Nn t i))) l))
      (K.mul h (K.mul (K.mul (K.mul c (X t)) (ρ t)) (σ t))))
    (hb1 : ∀ t, P.nn (sub K K.one (bk K P F h c ν X t)))
    (N : Nat) (hS : P.nn (sub K K.one (sumA K (bk K P F h c ν X) N))) :
    le K P (K.mul (Zw K w (a N) l) (K.mul (sub K K.one (sumA K (bk K P F h c ν X) N))
      (sub K K.one (sumA K (bk K P F h c ν X) N)))) (Zw K w (a 0) l) := by
  have hone := nn_one K P
  have hinv := nn_inv K P F _ (P.nn_add _ _ hν hν) hν0
  have hi := F.mul_inv _ hν0
  have hb : ∀ t, P.nn (bk K P F h c ν X t) := fun t =>
    P.nn_mul _ _ hinv (P.nn_mul _ _ hh (P.nn_mul _ _ (P.nn_sq c) (P.nn_sq (X t))))
  have hZ : ∀ t, P.nn (Zw K w (a t) l) := fun t => by
    unfold Zw
    have g : ∀ l : List I, P.nn (lsum K (fun i => K.mul (w i) (K.mul (a t i) (a t i))) l) := by
      intro l; induction l with
      | nil => exact P.nn_zero
      | cons x l ih => exact P.nn_add _ _ (P.nn_mul _ _ (hw x) (P.nn_sq _)) ih
    exact g l
  apply serrin_mid K P (fun t => Zw K w (a t) l) (bk K P F h c ν X) hb hb1 hone hZ _ N hS
  intro t
  have es := enstrophy_step K P w (a t) (a (t + 1)) k (Nn t) h ν c (X t) (ρ t) (σ t) l (step t) (hσ t) (hT t)
  have mp := midpoint K P w (a t) (a (t + 1)) hw l
  rw [← hρ t] at mp
  have ms := midpoint_step K P (Zw K w (a t) l) (Zw K w (a (t + 1)) l) h c (X t) (ρ t) (σ t) ν hh hν es
    (by
      unfold le at mp ⊢
      rw [show sub K (K.add (K.add (Zw K w (a t) l) (Zw K w (a t) l)) (K.add (Zw K w (a (t + 1)) l) (Zw K w (a (t + 1)) l)))
          (K.mul (ρ t) (ρ t))
        = sub K (K.add (K.add (Zw K w (a (t + 1)) l) (Zw K w (a (t + 1)) l)) (K.add (Zw K w (a t) l) (Zw K w (a t) l)))
          (K.mul (ρ t) (ρ t)) by lib_ring]
      exact mp)
  -- divide 2·(2ν)·[(1 − b) Z'] ≤ 2·(2ν)·[(1 + b) Z] by 2·2ν
  apply le_of_mul_le K P F _ _ (K.add (K.add ν ν) (K.add ν ν)) (P.nn_add _ _ (P.nn_add _ _ hν hν) (P.nn_add _ _ hν hν)) h4
  unfold bk
  generalize Zw K w (a (t + 1)) l = Z' at ms ⊢
  generalize Zw K w (a t) l = Z at ms ⊢
  generalize K.mul h (K.mul (K.mul c c) (K.mul (X t) (X t))) = A at ms ⊢
  unfold le at ms ⊢
  have e1 : K.mul (K.add (K.add ν ν) (K.add ν ν)) (K.mul (K.add K.one (K.mul (F.inv (K.add ν ν)) A)) Z)
      = K.add (K.mul (K.add (K.add ν ν) (K.mul (K.mul (K.add ν ν) (F.inv (K.add ν ν))) A)) Z)
              (K.mul (K.add (K.add ν ν) (K.mul (K.mul (K.add ν ν) (F.inv (K.add ν ν))) A)) Z) := by lib_ring
  have e2 : K.mul (K.add (K.add ν ν) (K.add ν ν)) (K.mul Z' (sub K K.one (K.mul (F.inv (K.add ν ν)) A)))
      = K.add (K.mul (sub K (K.add ν ν) (K.mul (K.mul (K.add ν ν) (F.inv (K.add ν ν))) A)) Z')
              (K.mul (sub K (K.add ν ν) (K.mul (K.mul (K.add ν ν) (F.inv (K.add ν ν))) A)) Z') := by lib_ring
  rw [e1, e2, hi, show K.mul K.one A = A by lib_ring]
  exact ms

end CNSerrin

#print axioms CNSerrin.cn_serrin

ns/CNSerrinTriad.lean

lean_all/ns/CNSerrinTriad.lean

/-
  ns/CNSerrinTriad.lean — T37 cycle 257 (goal G11): Prodi–Serrin for a Crank–Nicolson Galerkin run with the
  transfer bound derived from the triad sum.

  Lean 4 core plus ns/CNSerrin, ns/TransferBound, ns/SignedSum. As CNSerrin.cn_serrin with c = 1, except that the
  hypothesis hT is replaced, at every time t, by the triad data: magnitudes a_t ≥ 0 (X_t = Σ a_t, the Wiener
  norm), b_t with Σ b_t² = ρ_t², c_t with Σ_q c_t(σ(p,q))² ≤ σ_t² for every p, X_tρ_tσ_t ≠ 0, and the domination
  T_t² ≤ (Σ_p a_p Σ_q b_q c_{σ(p,q)})² of the transfer T_t = Σ_i w_i s_i N_{t,i} (SignedSum.signed_sum when T_t is a
  sum of terms dominated termwise). Then
      Z_N (1 − S_N)² ≤ Z_0,   S_N = Σ_{t<N} h X_t²/(2ν)                                   (cn_serrin_triad)
-/
import ns.CNSerrin
import ns.SignedSum

namespace CNSerrinTriad
open DetChain NSChain LibRing LibCS LibGron LibOF SerrinSum EnstrophyStep CNSerrin TransferBound

variable {R : Type} (K : CRing R) {I : Type}

theorem cn_serrin_triad (P : Pos K) (F : OF K P) (w k : I → R) (a Nn : Nat → I → R) (X ρ σ : Nat → R) (h ν : R)
    (l : List I) (A B C : Nat → I → R) (σf : Nat → I → I → I) (lt : Nat → List I)
    (hw : ∀ i, P.nn (w i)) (hh : P.nn h) (hν : P.nn ν) (hν0 : K.add ν ν ≠ K.zero)
    (h4 : K.add (K.add ν ν) (K.add ν ν) ≠ K.zero)
    (step : ∀ t i, sub K (a (t + 1) i) (a t i)
      = K.mul h (K.add (K.neg (K.mul ν (K.mul (k i) (K.add (a (t + 1) i) (a t i))))) (Nn t i)))
    (hσ : ∀ t, K.mul (σ t) (σ t)
      = lsum K (fun i => K.mul (w i) (K.mul (k i) (K.mul (K.add (a (t + 1) i) (a t i)) (K.add (a (t + 1) i) (a t i))))) l)
    (hρ : ∀ t, K.mul (ρ t) (ρ t) = lsum K (fun i => K.mul (w i) (K.mul (K.add (a (t + 1) i) (a t i)) (K.add (a (t + 1) i) (a t i)))) l)
    -- triad data at every time
    (hA : ∀ t i, P.nn (A t i)) (hX : ∀ t, X t = lsum K (A t) (lt t))
    (hB : ∀ t, K.mul (ρ t) (ρ t) = lsum K (fun q => K.mul (B t q) (B t q)) (lt t))
    (hC : ∀ t p, le K P (lsum K (fun q => K.mul (C t (σf t p q)) (C t (σf t p q))) (lt t)) (K.mul (σ t) (σ t)))
    (hXn : ∀ t, P.nn (X t)) (hρn : ∀ t, P.nn (ρ t)) (hσn : ∀ t, P.nn (σ t))
    (h0 : ∀ t, K.mul (K.mul (X t) (ρ t)) (σ t) ≠ K.zero)
    (hdom : ∀ t, le K P
      (K.mul (lsum K (fun i => K.mul (w i) (K.mul (K.add (a (t + 1) i) (a t i)) (Nn t i))) l)
             (lsum K (fun i => K.mul (w i) (K.mul (K.add (a (t + 1) i) (a t i)) (Nn t i))) l))
      (K.mul (lsum K (fun p => K.mul (A t p) (lsum K (fun q => K.mul (B t q) (C t (σf t p q))) (lt t))) (lt t))
             (lsum K (fun p => K.mul (A t p) (lsum K (fun q => K.mul (B t q) (C t (σf t p q))) (lt t))) (lt t))))
    (hb1 : ∀ t, P.nn (sub K K.one (bk K P F h K.one ν X t)))
    (N : Nat) (hS : P.nn (sub K K.one (sumA K (bk K P F h K.one ν X) N))) :
    le K P (K.mul (Zw K w (a N) l) (K.mul (sub K K.one (sumA K (bk K P F h K.one ν X) N))
      (sub K K.one (sumA K (bk K P F h K.one ν X) N)))) (Zw K w (a 0) l) :=
  cn_serrin K P F w k a Nn X ρ σ h K.one ν l hw hh hν hν0 h4 step hσ hρ
    (fun t => transfer_bound K P F (A t) (B t) (C t) (σf t) (lt t) _ h (X t) (ρ t) (σ t) hh (hA t) (hX t) (hB t)
      (hC t) (hXn t) (hρn t) (hσn t) (h0 t) (hdom t))
    hb1 N hS

end CNSerrinTriad

#print axioms CNSerrinTriad.cn_serrin_triad

ns/CNUnique.lean

lean_all/ns/CNUnique.lean

/-
  ns/CNUnique.lean — T37 cycle 773 (goal G484): uniqueness of a Crank–Nicolson Galerkin step for small h, over any
  ordered ring without zero divisors (ℚ included), when the nonlinearity is one-sided Lipschitz on the pair:
  if a', b' both solve the step from a (with nonlinear terms Na, Nb), d = a' − b', and
    Σ_{n<N} d_n (Na_n − Nb_n) ≤ L·Σ d_n²,   h·L ≤ 1 − c  (c ≥ 0, c ≠ 0),   h, ν, k_n ≥ 0,
  then a'_n = b'_n for every n < N.

  Lean 4 core plus ns/GoodModesQ (mul_ne), ns/DyadicModelMode (term_le), ns/RatModelU (le_mul_rg),
  ns/DyadicLarge (le_add', le_mul_nn), lib/RingTac (lib_ring).
    diff_step    d_n = h(−ν k_n d_n + (Na_n − Nb_n))
    sq_zero'     x·x = 0 ⇒ x = 0
    cn_unique    the statement
-/
import ns.GoodModesQ
import ns.DyadicModelMode

namespace CNUnique
open DetChain NSChain DyadicSmallGlobal LibRing DyadicLarge GoodModesQ RatModelU DyadicModelMode

section Gen
variable {R : Type} (K : CRing R)

theorem diff_step (a a' b' k Na Nb h nu : R)
    (s1 : K.add a' (K.neg a) = K.mul h (K.add (K.neg (K.mul nu (K.mul k (K.add a' a)))) Na))
    (s2 : K.add b' (K.neg a) = K.mul h (K.add (K.neg (K.mul nu (K.mul k (K.add b' a)))) Nb)) :
    K.add a' (K.neg b') = K.mul h (K.add (K.neg (K.mul nu (K.mul k (K.add a' (K.neg b'))))) (K.add Na (K.neg Nb))) := by
  have z1 : K.add (K.add a' (K.neg a)) (K.neg (K.mul h (K.add (K.neg (K.mul nu (K.mul k (K.add a' a)))) Na))) = K.zero := by
    rw [s1]; lib_ring
  have z2 : K.add (K.add b' (K.neg a)) (K.neg (K.mul h (K.add (K.neg (K.mul nu (K.mul k (K.add b' a)))) Nb))) = K.zero := by
    rw [s2]; lib_ring
  have t : K.add a' (K.neg b') = K.add (K.mul h (K.add (K.neg (K.mul nu (K.mul k (K.add a' (K.neg b'))))) (K.add Na (K.neg Nb))))
      (K.add (K.add (K.add a' (K.neg a)) (K.neg (K.mul h (K.add (K.neg (K.mul nu (K.mul k (K.add a' a)))) Na))))
        (K.neg (K.add (K.add b' (K.neg a)) (K.neg (K.mul h (K.add (K.neg (K.mul nu (K.mul k (K.add b' a)))) Nb)))))) := by
    lib_ring
  exact t.trans (by rw [z1, z2]; lib_ring)

variable (O : OrdR K)
include O

theorem sq_zero' (x : R) (h : K.mul x x = K.zero) : x = K.zero := by
  cases O.noZD x x h with
  | inl e => exact e
  | inr e => exact e

theorem sum_sq_zero (f : Nat → R) (N : Nat) (h : sumN K (fun n => K.mul (f n) (f n)) N = K.zero) :
    ∀ n, n < N → f n = K.zero := by
  intro n hn
  have l := term_le K O.toCone (fun n => K.mul (f n) (f n)) (fun n => O.nn_sq _) n N hn
  rw [h] at l
  apply sq_zero' K O
  exact O.antisym _ _ l ⟨_, O.nn_sq (f n), K.zero_add _⟩

theorem cn_unique (a a' b' k Na Nb : Nat → R) (h nu L c : R) (N : Nat)
    (hh : O.nn h) (hnu : O.nn nu) (hk : ∀ n, O.nn (k n)) (hc : O.nn c) (hc0 : c ≠ K.zero)
    (s1 : ∀ n, K.add (a' n) (K.neg (a n)) = K.mul h (K.add (K.neg (K.mul nu (K.mul (k n) (K.add (a' n) (a n))))) (Na n)))
    (s2 : ∀ n, K.add (b' n) (K.neg (a n)) = K.mul h (K.add (K.neg (K.mul nu (K.mul (k n) (K.add (b' n) (a n))))) (Nb n)))
    (lip : Le K O.toCone (sumN K (fun n => K.mul (K.add (a' n) (K.neg (b' n))) (K.add (Na n) (K.neg (Nb n)))) N)
      (K.mul L (sumN K (fun n => K.mul (K.add (a' n) (K.neg (b' n))) (K.add (a' n) (K.neg (b' n)))) N)))
    (small : Le K O.toCone (K.mul h L) (K.add K.one (K.neg c))) :
    ∀ n, n < N → a' n = b' n := by
  let d : Nat → R := fun n => K.add (a' n) (K.neg (b' n))
  let D : Nat → R := fun n => K.add (Na n) (K.neg (Nb n))
  have ds : ∀ n, d n = K.mul h (K.add (K.neg (K.mul nu (K.mul (k n) (d n)))) (D n)) := fun n =>
    diff_step K (a n) (a' n) (b' n) (k n) (Na n) (Nb n) h nu (s1 n) (s2 n)
  -- Σ d² + hν Σ k d² = h Σ d D
  have ident : ∀ M, K.add (sumN K (fun n => K.mul (d n) (d n)) M)
      (K.mul h (K.mul nu (sumN K (fun n => K.mul (k n) (K.mul (d n) (d n))) M)))
      = K.mul h (sumN K (fun n => K.mul (d n) (D n)) M) := by
    intro M
    induction M with
    | zero => show K.add K.zero (K.mul h (K.mul nu K.zero)) = K.mul h K.zero; lib_ring
    | succ M ih =>
      show K.add (K.add (sumN K (fun n => K.mul (d n) (d n)) M) (K.mul (d M) (d M)))
          (K.mul h (K.mul nu (K.add (sumN K (fun n => K.mul (k n) (K.mul (d n) (d n))) M) (K.mul (k M) (K.mul (d M) (d M))))))
        = K.mul h (K.add (sumN K (fun n => K.mul (d n) (D n)) M) (K.mul (d M) (D M)))
      have z : K.add (d M) (K.neg (K.mul h (K.add (K.neg (K.mul nu (K.mul (k M) (d M)))) (D M)))) = K.zero := by
        have := ds M
        exact (show K.add (d M) (K.neg (K.mul h (K.add (K.neg (K.mul nu (K.mul (k M) (d M)))) (D M))))
          = K.add (d M) (K.neg (d M)) from by rw [← this]).trans (by lib_ring)
      have e : K.add (K.mul (d M) (d M)) (K.mul h (K.mul nu (K.mul (k M) (K.mul (d M) (d M)))))
          = K.mul h (K.mul (d M) (D M)) := by
        have t : K.add (K.mul (d M) (d M)) (K.mul h (K.mul nu (K.mul (k M) (K.mul (d M) (d M)))))
            = K.add (K.mul h (K.mul (d M) (D M)))
              (K.mul (d M) (K.add (d M) (K.neg (K.mul h (K.add (K.neg (K.mul nu (K.mul (k M) (d M)))) (D M)))))) := by
          lib_ring
        exact t.trans (by rw [z]; lib_ring)
      have r : K.add (K.add (sumN K (fun n => K.mul (d n) (d n)) M) (K.mul (d M) (d M)))
          (K.mul h (K.mul nu (K.add (sumN K (fun n => K.mul (k n) (K.mul (d n) (d n))) M) (K.mul (k M) (K.mul (d M) (d M))))))
          = K.add (K.add (sumN K (fun n => K.mul (d n) (d n)) M)
              (K.mul h (K.mul nu (sumN K (fun n => K.mul (k n) (K.mul (d n) (d n))) M))))
            (K.add (K.mul (d M) (d M)) (K.mul h (K.mul nu (K.mul (k M) (K.mul (d M) (d M)))))) := by lib_ring
      rw [r, ih, e]
      lib_ring
  generalize hS : sumN K (fun n => K.mul (d n) (d n)) N = S
  have hSnn : O.nn S := by rw [← hS]; exact sumN_nn K O.toCone _ (fun n => O.nn_sq _) N
  have hP : O.nn (K.mul h (K.mul nu (sumN K (fun n => K.mul (k n) (K.mul (d n) (d n))) N))) :=
    O.nn_mul _ _ hh (O.nn_mul _ _ hnu (sumN_nn K O.toCone _ (fun n => O.nn_mul _ _ (hk n) (O.nn_sq _)) N))
  have id2 := ident N
  rw [hS] at id2 lip
  -- S ≤ S + P = h Σ dD ≤ h L S ≤ (1 − c) S
  have l1 : Le K O.toCone S (K.mul h (sumN K (fun n => K.mul (d n) (D n)) N)) := ⟨_, hP, id2⟩
  have l2 := le_mul_nn K O.toCone h _ _ hh lip
  have l3 : Le K O.toCone (K.mul h (K.mul L S)) (K.mul (K.add K.one (K.neg c)) S) := by
    rw [show K.mul h (K.mul L S) = K.mul (K.mul h L) S by lib_ring]
    exact le_mul_rg K O.toCone S _ _ hSnn small
  have l4 := le_trans' K O.toCone _ _ _ l1 (le_trans' K O.toCone _ _ _ l2 l3)
  -- c·S ≤ 0 and c·S ≥ 0 ⇒ c·S = 0 ⇒ S = 0
  have cS : K.mul c S = K.zero := by
    cases l4 with
    | intro e he =>
      apply O.antisym
      · exact ⟨e, he.1, by
          have : K.add (K.mul c S) e = K.add (K.add S e) (K.neg (K.mul (K.add K.one (K.neg c)) S)) := by lib_ring
          rw [this, he.2]; lib_ring⟩
      · exact ⟨_, O.nn_mul _ _ hc hSnn, K.zero_add _⟩
  have S0 : S = K.zero := by
    cases O.noZD _ _ cS with
    | inl e => exact absurd e hc0
    | inr e => exact e
  intro n hn
  have := sum_sq_zero K O d N (by rw [hS, S0]) n hn
  have : a' n = K.add (d n) (b' n) := by show a' n = K.add (K.add (a' n) (K.neg (b' n))) (b' n); lib_ring
  rw [this, show d n = K.zero from sum_sq_zero K O d N (by rw [hS, S0]) n hn]; lib_ring

end Gen

end CNUnique

#print axioms CNUnique.diff_step
#print axioms CNUnique.sq_zero'
#print axioms CNUnique.sum_sq_zero
#print axioms CNUnique.cn_unique

ns/CastZAdd.lean

lean_all/ns/CastZAdd.lean

/-
  ns/CastZAdd.lean — T37 cycle 1063 (goal G656b, piece iii): the integer cast into a commutative ring is additive, and
  integer triads satisfy the triangle inequality for |k|².
    castZ_sNN     castZ (m ⊖ n) = castN m − castN n        (subNatNat)
    castZ_add     castZ (a + b) = castZ a + castZ b
    castZ_neg     castZ (−a) = −castZ a
    tri_sq        k_r = −(k_p + k_q) (integer triad, componentwise) ⇒ |k_r|² ≤ 2|k_p|² + 2|k_q|² (ring vectors)
  (witness |k_p − k_q|²). This supplies the hypothesis of ForceSobW.total_sobW for W = |k|².

  Lean 4 core plus lib/IntRing (subNatNat lemmas), ns/RatModel (cast_add), ns/NormCompare (kvec), lib/RingTac.
-/
import lib.IntRing
import ns.NormCompare
import ns.TriadSparse

namespace CastZAdd
open DetChain NSChain LibRing DyadicLarge FourierGalerkin LibInt NormCompare CubeCount

section Gen
variable {R : Type} (K : CRing R)

theorem castZ_sNN (m n : Nat) : castZ K (Int.subNatNat m n) = K.add (castN K m) (K.neg (castN K n)) := by
  cases Nat.lt_or_ge m n with
  | inr hge =>
    have ⟨d, hd⟩ := LibGcd.le_dest hge
    rw [hd, show Int.subNatNat (n + d) n = Int.subNatNat (d + n) (0 + n) by rw [Nat.add_comm n d, Nat.zero_add],
      sNN_add_k, sNN_zero]
    show castN K d = K.add (castN K (n + d)) (K.neg (castN K n))
    rw [RatModel.cast_add K n d]; lib_ring
  | inl hlt =>
    have ⟨d, hd⟩ := LibGcd.le_dest (Nat.succ_le_of_lt hlt)
    rw [hd, show Int.subNatNat m (m + 1 + d) = Int.subNatNat (0 + m) ((d + 1) + m) by
      rw [Nat.zero_add, show m + 1 + d = (d + 1) + m by lib_nat], sNN_add_k, sNN_zero_left]
    show K.neg (castN K (d + 1)) = K.add (castN K m) (K.neg (castN K (m + 1 + d)))
    rw [show m + 1 + d = m + (d + 1) by lib_nat, RatModel.cast_add K m (d + 1)]; lib_ring

theorem castZ_add (a b : Int) : castZ K (a + b) = K.add (castZ K a) (castZ K b) := by
  cases a with
  | ofNat m => cases b with
    | ofNat n => show castN K (m + n) = K.add (castN K m) (castN K n); exact RatModel.cast_add K m n
    | negSucc n =>
      show castZ K (Int.subNatNat m (n + 1)) = K.add (castN K m) (K.neg (castN K (n + 1)))
      exact castZ_sNN K m (n + 1)
  | negSucc m => cases b with
    | ofNat n =>
      show castZ K (Int.subNatNat n (m + 1)) = K.add (K.neg (castN K (m + 1))) (castN K n)
      rw [castZ_sNN]; lib_ring
    | negSucc n =>
      show K.neg (castN K (m + n + 1 + 1)) = K.add (K.neg (castN K (m + 1))) (K.neg (castN K (n + 1)))
      rw [show m + n + 1 + 1 = (m + 1) + (n + 1) by lib_nat, RatModel.cast_add K (m + 1) (n + 1)]; lib_ring

theorem castZ_neg (a : Int) : castZ K (-a) = K.neg (castZ K a) := by
  have h := castZ_add K a (-a)
  rw [show a + -a = 0 by lib_int] at h
  have h0 : castZ K 0 = K.zero := rfl
  rw [h0] at h
  calc castZ K (-a) = K.add (K.add (castZ K a) (castZ K (-a))) (K.neg (castZ K a)) := by lib_ring
    _ = K.neg (castZ K a) := by rw [← h]; lib_ring

theorem comp_neg (x y z : Int) (h : x + y + z = 0) : castZ K z = K.neg (K.add (castZ K x) (castZ K y)) := by
  have e : z = -(x + y) := by
    have : z = (x + y + z) + (-(x + y)) := by lib_int
    rw [this, h]; lib_int
  rw [e, castZ_neg, castZ_add]

variable (P : Cone K)

/-- integer triad k_p + k_q + k_r = 0 ⇒ |k_r|² ≤ 2|k_p|² + 2|k_q|² -/
theorem tri_sq (kp kq kr : Mode) (h : TriadSparse.tadd (TriadSparse.tadd kp kq) kr = TriadSparse.zero3) :
    Le K P (dot3 K (kvec K kr) (kvec K kr))
      (K.add (K.add (dot3 K (kvec K kp) (kvec K kp)) (dot3 K (kvec K kp) (kvec K kp)))
        (K.add (dot3 K (kvec K kq) (kvec K kq)) (dot3 K (kvec K kq) (kvec K kq)))) := by
  have h1 : kp.1 + kq.1 + kr.1 = 0 := congrArg Prod.fst h
  have h2 : kp.2.1 + kq.2.1 + kr.2.1 = 0 := congrArg (fun t => t.2.1) h
  have h3 : kp.2.2 + kq.2.2 + kr.2.2 = 0 := congrArg (fun t => t.2.2) h
  have e1 := comp_neg K _ _ _ h1
  have e2 := comp_neg K _ _ _ h2
  have e3 := comp_neg K _ _ _ h3
  refine ⟨K.add (K.add (K.mul (K.add (castZ K kp.1) (K.neg (castZ K kq.1))) (K.add (castZ K kp.1) (K.neg (castZ K kq.1))))
      (K.mul (K.add (castZ K kp.2.1) (K.neg (castZ K kq.2.1))) (K.add (castZ K kp.2.1) (K.neg (castZ K kq.2.1)))))
      (K.mul (K.add (castZ K kp.2.2) (K.neg (castZ K kq.2.2))) (K.add (castZ K kp.2.2) (K.neg (castZ K kq.2.2)))),
    P.nn_add _ _ (P.nn_add _ _ (P.nn_sq _) (P.nn_sq _)) (P.nn_sq _), ?_⟩
  show K.add (K.add (K.add (K.mul (castZ K kr.1) (castZ K kr.1)) (K.mul (castZ K kr.2.1) (castZ K kr.2.1)))
      (K.mul (castZ K kr.2.2) (castZ K kr.2.2))) _ = _
  rw [e1, e2, e3]
  show _ = K.add (K.add (K.add (K.add (K.mul (castZ K kp.1) (castZ K kp.1)) (K.mul (castZ K kp.2.1) (castZ K kp.2.1)))
      (K.mul (castZ K kp.2.2) (castZ K kp.2.2))) (K.add (K.add (K.mul (castZ K kp.1) (castZ K kp.1)) (K.mul (castZ K kp.2.1) (castZ K kp.2.1)))
      (K.mul (castZ K kp.2.2) (castZ K kp.2.2))))
    (K.add (K.add (K.add (K.mul (castZ K kq.1) (castZ K kq.1)) (K.mul (castZ K kq.2.1) (castZ K kq.2.1)))
      (K.mul (castZ K kq.2.2) (castZ K kq.2.2))) (K.add (K.add (K.mul (castZ K kq.1) (castZ K kq.1)) (K.mul (castZ K kq.2.1) (castZ K kq.2.1)))
      (K.mul (castZ K kq.2.2) (castZ K kq.2.2))))
  lib_ring

/-- the hypothesis of ForceSobW.total_sobW for W = |k|² and the integer triad indicator -/
theorem delta_tri {I : Type} (kz : I → Mode) (p q r : I) :
    Le K P (K.mul (TriadSparse.delta K kz p q r) (dot3 K (kvec K (kz r)) (kvec K (kz r))))
      (K.mul (TriadSparse.delta K kz p q r) (K.add (K.add (dot3 K (kvec K (kz p)) (kvec K (kz p))) (dot3 K (kvec K (kz p)) (kvec K (kz p))))
        (K.add (dot3 K (kvec K (kz q)) (kvec K (kz q))) (dot3 K (kvec K (kz q)) (kvec K (kz q)))))) := by
  show Le K P (K.mul (cond (decide (TriadSparse.tadd (TriadSparse.tadd (kz p) (kz q)) (kz r) = TriadSparse.zero3)) K.one K.zero) _)
    (K.mul (cond (decide (TriadSparse.tadd (TriadSparse.tadd (kz p) (kz q)) (kz r) = TriadSparse.zero3)) K.one K.zero) _)
  cases hd : decide (TriadSparse.tadd (TriadSparse.tadd (kz p) (kz q)) (kz r) = TriadSparse.zero3) with
  | false => exact ⟨K.zero, P.nn_zero, by show K.add (K.mul K.zero _) K.zero = K.mul K.zero _; lib_ring⟩
  | true =>
    have t := tri_sq K P (kz p) (kz q) (kz r) (of_decide_eq_true hd)
    have m := le_mul_nn K P K.one _ _ (RatModel.nn_one' K P) t
    exact m

end Gen

end CastZAdd

#print axioms CastZAdd.castZ_sNN
#print axioms CastZAdd.castZ_add
#print axioms CastZAdd.castZ_neg
#print axioms CastZAdd.comp_neg
#print axioms CastZAdd.tri_sq
#print axioms CastZAdd.delta_tri

ns/ChooseAbsorb.lean

lean_all/ns/ChooseAbsorb.lean

/-
  ns/ChooseAbsorb.lean — T37 (goal G859c2e): explicit choices of the absorption parameters of H2Large, over an
  ordered field with inverses. For X ≠ 0 the choice D = Y·X⁻¹ makes X·D = Y (fill), products of nonzero elements are
  nonzero (nz_mul), and 4 ≠ 0 (four_ne); so with e = ν²·S⁻¹ (S = ι²(k₁ + k₂(1 + 2)) ≠ 0) and
  D₁ = 16³Zb⁵·(9³·4e³)⁻¹, D₂ = 16Zb⁵·(9·4e³)⁻¹, D₃ = Zb³·(4e)⁻¹ the hypotheses H1–H3 of H2Large hold with equality.

  Lean 4 core plus ns/AgmonAbsorb.
-/
import ns.AgmonAbsorb

namespace ChooseAbsorb
open DetChain NSChain DyadicSmallGlobal LibRing DyadicLarge RootYoung AgmonAbsorb

section Gen
variable {R : Type} (K : CRing R) (O : OrdR K)
variable (inv : R → R) (mul_inv : ∀ y, y ≠ K.zero → K.mul y (inv y) = K.one)

include mul_inv in
theorem fill (X Y : R) (hX : X ≠ K.zero) : K.mul X (K.mul Y (inv X)) = Y := by
  have e := mul_inv X hX
  calc K.mul X (K.mul Y (inv X)) = K.mul Y (K.mul X (inv X)) := by lib_ring
    _ = Y := by rw [e]; lib_ring

include O in
theorem nz_mul (x y : R) (hx : x ≠ K.zero) (hy : y ≠ K.zero) : K.mul x y ≠ K.zero := by
  intro h
  cases O.noZD _ _ h with
  | inl h1 => exact hx h1
  | inr h1 => exact hy h1

include O in
theorem four_ne (h10 : K.one ≠ K.zero) : four K ≠ K.zero := by
  intro h
  have n1 : O.nn K.one := RatModel.nn_one' K O.toCone
  have n3 : O.nn (K.add (K.add K.one K.one) K.one) := O.nn_add _ _ (O.nn_add _ _ n1 n1) n1
  have e : K.add K.one (K.add (K.add K.one K.one) K.one) = K.zero := by
    rw [← h]; unfold four; lib_ring
  exact h10 (nn_sum_zero K O _ _ n1 n3 e)

include O mul_inv in
/-- H1-type hypothesis with equality: c·Z ≤ m·(4·(e³·D)) for D = c·Z·(m·4e³)⁻¹ -/
theorem choose_H (c m e Z : R) (h10 : K.one ≠ K.zero) (hm : m ≠ K.zero) (he : e ≠ K.zero) :
    Le K O.toCone (K.mul c Z)
      (K.mul m (K.mul (four K) (K.mul (c3 K e) (K.mul (K.mul c Z) (inv (K.mul m (K.mul (four K) (c3 K e)))))))) := by
  have hX : K.mul m (K.mul (four K) (c3 K e)) ≠ K.zero :=
    nz_mul K O _ _ hm (nz_mul K O _ _ (four_ne K O h10) (nz_mul K O _ _ (nz_mul K O _ _ he he) he))
  have f := fill K inv mul_inv _ (K.mul c Z) hX
  refine ⟨K.zero, O.nn_zero, ?_⟩
  calc K.add (K.mul c Z) K.zero = K.mul c Z := by lib_ring
    _ = K.mul (K.mul m (K.mul (four K) (c3 K e))) (K.mul (K.mul c Z) (inv (K.mul m (K.mul (four K) (c3 K e))))) := f.symm
    _ = _ := by lib_ring

include O mul_inv in
/-- the choice e = ν²·S⁻¹ gives S·e ≤ ν² (with equality) -/
theorem choose_eps (S ν : R) (hS : S ≠ K.zero) :
    Le K O.toCone (K.mul S (K.mul (K.mul ν ν) (inv S))) (K.mul ν ν) := by
  rw [fill K inv mul_inv S (K.mul ν ν) hS]
  exact le_refl' K O.toCone _

end Gen

end ChooseAbsorb

#print axioms ChooseAbsorb.fill
#print axioms ChooseAbsorb.nz_mul
#print axioms ChooseAbsorb.four_ne
#print axioms ChooseAbsorb.choose_H
#print axioms ChooseAbsorb.choose_eps

ns/ChooseCutoff.lean

lean_all/ns/ChooseCutoff.lean

/-
  ns/ChooseCutoff.lean — T37 cycle 1005 (goal G655): the cutoffs of the large-data local bound exist. Over an ordered
  field with inverses that is Archimedean (every element is below some castN n; ℚ is), for every level Zb ≥ 0,
  coupling ι and viscosity ν ≠ 0 there are R, t₀ ∈ ℕ with
    c₂(R, t₀) · Zb ≤ ν²,   c₂ = 2ι²((2R+1)³/(t₀+1)² + 135/(R+1))
  (choose_cutoff), so EnstLocalGalerkin.local_galerkin applies to arbitrary data with constants fixed by Zb alone.
  Step lemma (small_inv): if X ≤ castN(n+1) then X·(n+1)^{−1} ≤ 1.

  Lean 4 core plus ns/EnstLocalGalerkin (C2, cc), ns/LatticeSum (iv), ns/EnstGlobal (le_of_mul_le).
-/
import ns.EnstLocalGalerkin

namespace ChooseCutoff
open DetChain NSChain DyadicSmallGlobal LibRing DyadicLarge EnstGlobal LatticeSum EnstLocalGalerkin ForceTwoScale
  GoodModesQ

section Gen
variable {R : Type} (K : CRing R) (O : OrdR K)
variable (inv : R → R) (mul_inv : ∀ y, y ≠ K.zero → K.mul y (inv y) = K.one) (h10 : K.one ≠ K.zero)

include mul_inv h10 in
theorem small_inv (X : R) (n : Nat) (h : Le K O.toCone X (castN K (n + 1))) :
    Le K O.toCone (K.mul X (iv K inv (n + 1))) K.one := by
  have m := ForceSob.le_mul_right' K O.toCone h (iv_nn K O inv mul_inv h10 n)
  rw [iv_mul K O inv mul_inv h10 n] at m
  exact m

include O in
theorem two_ne (h1 : K.one ≠ K.zero) : K.add K.one K.one ≠ K.zero := by
  intro e
  have n1 : O.nn K.one := RatModel.nn_one' K O.toCone
  have l1 : Le K O.toCone K.one K.zero := ⟨K.one, n1, e⟩
  have l2 : Le K O.toCone K.zero K.one := ⟨K.one, n1, by lib_ring⟩
  exact h1 (O.antisym _ _ l1 l2)

/-- if c·(Y·ν⁻²)·x ≤ 1-type bound: from X·iv ≤ 1 with X = W·iν·iν, get W·iv ≤ ν² -/
theorem scale_back (W ν iν v : R) (hν : K.mul ν iν = K.one) (hW : Le K O.toCone (K.mul (K.mul W (K.mul iν iν)) v) K.one) :
    Le K O.toCone (K.mul W v) (K.mul ν ν) := by
  have m := le_mul_nn K O.toCone (K.mul ν ν) _ _ (O.nn_sq ν) hW
  have e : K.mul (K.mul ν ν) (K.mul (K.mul W (K.mul iν iν)) v) = K.mul (K.mul W v) (K.mul (K.mul ν iν) (K.mul ν iν)) := by
    lib_ring
  rw [e, hν] at m
  have e2 : K.mul (K.mul W v) (K.mul K.one K.one) = K.mul W v := by lib_ring
  have e3 : K.mul (K.mul ν ν) K.one = K.mul ν ν := by lib_ring
  rw [e2, e3] at m
  exact m

include mul_inv h10 in
theorem choose_cutoff (arch : ∀ x : R, ∃ n : Nat, Le K O.toCone x (castN K n))
    (ι ν Zb : R) (hν0 : ν ≠ K.zero) (hZb : O.nn Zb) :
    ∃ Rc t0 : Nat, Le K O.toCone (K.mul (cc K ι (C2 K inv Rc t0)) Zb) (K.mul ν ν) := by
  have hν := mul_inv ν hν0
  generalize inv ν = iν at hν
  -- 4ι²·135·Zb·ν⁻² ≤ R + 1
  have ⟨n1, hn1⟩ := arch (K.mul (K.mul (K.mul (castN K 4) (K.mul ι ι)) (K.mul (castN K 135) Zb)) (K.mul iν iν))
  -- n1 ≤ n1 + 1 as casts
  have hR : Le K O.toCone (K.mul (K.mul (K.mul (castN K 4) (K.mul ι ι)) (K.mul (castN K 135) Zb)) (K.mul iν iν)) (castN K (n1 + 1)) :=
    le_trans' K O.toCone _ _ _ hn1 (RatModel.cast_mono K O.toCone n1 (n1 + 1) (Nat.le_succ n1))
  have bR := scale_back K O _ ν iν _ hν (by
    have := small_inv K O inv mul_inv h10 _ n1 hR
    have e : K.mul (K.mul (K.mul (K.mul (castN K 4) (K.mul ι ι)) (K.mul (castN K 135) Zb)) (K.mul iν iν)) (iv K inv (n1 + 1))
        = K.mul (K.mul (K.mul (K.mul (castN K 4) (K.mul ι ι)) (K.mul (castN K 135) Zb)) (K.mul iν iν)) (iv K inv (n1 + 1)) := rfl
    exact this)
  -- 4ι²·cube(R)·Zb·ν⁻² ≤ t₀ + 1
  have ⟨n2, hn2⟩ := arch (K.mul (K.mul (K.mul (castN K 4) (K.mul ι ι)) (K.mul (castN K (cube n1)) Zb)) (K.mul iν iν))
  have hT : Le K O.toCone (K.mul (K.mul (K.mul (castN K 4) (K.mul ι ι)) (K.mul (castN K (cube n1)) Zb)) (K.mul iν iν)) (castN K (n2 + 1)) :=
    le_trans' K O.toCone _ _ _ hn2 (RatModel.cast_mono K O.toCone n2 (n2 + 1) (Nat.le_succ n2))
  have bT := scale_back K O _ ν iν _ hν (small_inv K O inv mul_inv h10 _ n2 hT)
  refine ⟨n1, n2, ?_⟩
  -- iT² ≤ iT since iT ≤ 1
  have iTn := iv_nn K O inv mul_inv h10 n2
  have iT1 : Le K O.toCone (iv K inv (n2 + 1)) K.one := by
    have e := iv_mul K O inv mul_inv h10 n2
    refine ⟨K.mul (castN K n2) (iv K inv (n2 + 1)), O.nn_mul _ _ (RatModel.nn_cast K O.toCone n2) iTn, ?_⟩
    rw [← e]; show _ = K.mul (K.add K.one (castN K n2)) _; lib_ring
  have iTT := le_mul_nn K O.toCone (iv K inv (n2 + 1)) _ _ iTn iT1
  have e1 : K.mul (iv K inv (n2 + 1)) K.one = iv K inv (n2 + 1) := by lib_ring
  rw [e1] at iTT
  -- the T-term: 2ι²·cube·iT²·Zb ≤ 2ι²·cube·iT·Zb ≤ ν²/2
  have nW : O.nn (K.mul (K.mul (castN K 4) (K.mul ι ι)) (K.mul (castN K (cube n1)) Zb)) :=
    O.nn_mul _ _ (O.nn_mul _ _ (RatModel.nn_cast K O.toCone 4) (O.nn_sq ι)) (O.nn_mul _ _ (RatModel.nn_cast K O.toCone _) hZb)
  have tT := le_trans' K O.toCone _ _ _ (le_mul_nn K O.toCone _ _ _ nW iTT) bT
  -- sum: 2·(c₂ Zb) ≤ 2 ν²  ⇒  c₂ Zb ≤ ν²
  have s := le_add' K O.toCone _ _ _ _ tT bR
  apply le_of_mul_le K O (K.add K.one K.one) _ _ (O.nn_add _ _ (RatModel.nn_one' K O.toCone) (RatModel.nn_one' K O.toCone))
    (two_ne K O h10)
  refine le_trans' K O.toCone _ _ _ ⟨K.zero, O.nn_zero, ?_⟩ (le_trans' K O.toCone _ _ _ s ⟨K.zero, O.nn_zero, ?_⟩)
  · show K.add (K.mul (K.add K.one K.one) (K.mul (K.mul (K.mul (K.add K.one K.one) (K.mul ι ι))
        (K.add (K.mul (castN K (cube n1)) (K.mul (inv (castN K (n2 + 1))) (inv (castN K (n2 + 1)))))
          (K.mul (castN K 135) (iv K inv (n1 + 1))))) Zb)) K.zero
      = K.add (K.mul (K.mul (K.mul (castN K 4) (K.mul ι ι)) (K.mul (castN K (cube n1)) Zb))
          (K.mul (iv K inv (n2 + 1)) (iv K inv (n2 + 1))))
        (K.mul (K.mul (K.mul (castN K 4) (K.mul ι ι)) (K.mul (castN K 135) Zb)) (iv K inv (n1 + 1)))
    have c4 : castN K 4 = K.add (K.add K.one K.one) (K.add K.one K.one) := by
      show K.add K.one (K.add K.one (K.add K.one (K.add K.one K.zero))) = _; lib_ring
    rw [c4]; show _ = K.add (K.mul (K.mul (K.mul (K.add (K.add K.one K.one) (K.add K.one K.one)) (K.mul ι ι))
        (K.mul (castN K (cube n1)) Zb)) (K.mul (inv (castN K (n2 + 1))) (inv (castN K (n2 + 1))))) _
    lib_ring
  · lib_ring

end Gen

end ChooseCutoff

#print axioms ChooseCutoff.small_inv
#print axioms ChooseCutoff.two_ne
#print axioms ChooseCutoff.scale_back
#print axioms ChooseCutoff.choose_cutoff

ns/CloseInterp.lean

lean_all/ns/CloseInterp.lean

/-
  ns/CloseInterp.lean — T37 cycle 1742 (goal G852c): two schemes a, b with the grid Hölder bound
  |x_{k+j} − x_k|² ≤ j·G that are close at a grid time, (a_k − b_k)² ≤ D, stay close inside the step:
      (u_a(k + θ) − u_b(k + θ))² ≤ 3(θG + D + θG)    (0 ≤ θ ≤ 1).
  With PigeonVec + BoxClose (D = δ² at the grid times) and InterpGrid this is the ε/3 step of the
  Arzelà–Ascoli substitute. Over any cone; Lean 4 core, hand proofs, no axioms.
-/
import ns.InterpGrid

namespace CloseInterp
open DetChain NSChain LibRing InterpStep InterpCross InterpGrid

section Gen
variable {R : Type} (K : CRing R) (P : Cone K)

theorem close_interp (a b : Nat → R) (G D : R)
    (ha : ∀ k j, Le K P (K.mul (sub K (a (k + j)) (a k)) (sub K (a (k + j)) (a k))) (nm K j G))
    (hb : ∀ k j, Le K P (K.mul (sub K (b (k + j)) (b k)) (sub K (b (k + j)) (b k))) (nm K j G))
    (k : Nat) (hD : Le K P (K.mul (sub K (a k) (b k)) (sub K (a k) (b k))) D)
    (θ e : R) (hθ : P.nn θ) (he : P.nn e) (hθe : K.add θ e = K.one) :
    Le K P (K.mul (sub K (interp K a k θ) (interp K b k θ)) (sub K (interp K a k θ) (interp K b k θ)))
      (K.mul (three K) (K.add (K.mul θ G) (K.add D (K.mul θ G)))) := by
  have h1 := step_interp K P a G ha k θ e hθ he hθe
  have h3 := step_interp K P b G hb k θ e hθ he hθe
  have e3 : K.mul (sub K (interp K b k θ) (b k)) (sub K (interp K b k θ) (b k))
      = K.mul (sub K (b k) (interp K b k θ)) (sub K (b k) (interp K b k θ)) := by lib_ring
  rw [e3] at h3
  have s := le_add2' K P h1 (le_add2' K P hD h3)
  have e1 : sub K (interp K a k θ) (interp K b k θ)
      = K.add (sub K (interp K a k θ) (a k)) (K.add (sub K (a k) (b k)) (sub K (b k) (interp K b k θ))) := by
    lib_ring
  rw [e1]
  exact le_trans' K P _ _ _ (three_sq K P _ _ _) (le_mul_l K P (three K) (nn_three K P) s)

end Gen
end CloseInterp

#print axioms CloseInterp.close_interp

ns/Commutator.lean

lean_all/ns/Commutator.lean

/-
  ns/Commutator.lean — T37 cycle 88: the triad commutator gain.

  Lean 4 core plus NSChain (Cone, Le), ns/TriadBound (le_mul_both) and lib/RingTac. For p, q ∈ R³ and the
  third wave vector r = −(p + q) of the triad, in every ordered commutative ring,
      |p|² − |q|² = (q − p)·r,        (|p|² − |q|²)² ≤ |r|² · (2|p|² + 2|q|²).
  The enstrophy-weight difference across a triad costs one factor |r| instead of |p|²: the commutator
  structure behind the H^s local theory (the low mode r carries the extra derivative).
  Coordinates are written out (the normaliser reads ring expressions, not structure projections).
-/
import ns.TriadBound
import lib.RingTac

namespace Commutator
open DetChain NSChain LibRing

section Ring
variable {R : Type} (K : CRing R)

def d3 (a1 a2 a3 b1 b2 b3 : R) : R := K.add (K.add (K.mul a1 b1) (K.mul a2 b2)) (K.mul a3 b3)

theorem d3_nn (P : Cone K) (a1 a2 a3 : R) : P.nn (d3 K a1 a2 a3 a1 a2 a3) := by
  unfold d3; exact P.nn_add _ _ (P.nn_add _ _ (P.nn_sq _) (P.nn_sq _)) (P.nn_sq _)

theorem diff_eq (p1 p2 p3 q1 q2 q3 : R) :
    sub K (d3 K p1 p2 p3 p1 p2 p3) (d3 K q1 q2 q3 q1 q2 q3)
      = d3 K (sub K q1 p1) (sub K q2 p2) (sub K q3 p3)
          (K.neg (K.add p1 q1)) (K.neg (K.add p2 q2)) (K.neg (K.add p3 q3)) := by
  unfold d3; lib_ring

/-- Cauchy–Schwarz in coordinates (from CFKernel's Lagrange identity, restated). -/
theorem cs3 (P : Cone K) (a1 a2 a3 b1 b2 b3 : R) :
    Le K P (K.mul (d3 K a1 a2 a3 b1 b2 b3) (d3 K a1 a2 a3 b1 b2 b3))
      (K.mul (d3 K a1 a2 a3 a1 a2 a3) (d3 K b1 b2 b3 b1 b2 b3)) :=
  ⟨K.add (K.add (K.mul (sub K (K.mul a2 b3) (K.mul a3 b2)) (sub K (K.mul a2 b3) (K.mul a3 b2)))
      (K.mul (sub K (K.mul a3 b1) (K.mul a1 b3)) (sub K (K.mul a3 b1) (K.mul a1 b3))))
      (K.mul (sub K (K.mul a1 b2) (K.mul a2 b1)) (sub K (K.mul a1 b2) (K.mul a2 b1))),
    P.nn_add _ _ (P.nn_add _ _ (P.nn_sq _) (P.nn_sq _)) (P.nn_sq _),
    by unfold d3; lib_ring⟩

/-- **Commutator bound** across a triad. -/
theorem comm_bound (P : Cone K) (p1 p2 p3 q1 q2 q3 : R) :
    Le K P (K.mul (sub K (d3 K p1 p2 p3 p1 p2 p3) (d3 K q1 q2 q3 q1 q2 q3))
              (sub K (d3 K p1 p2 p3 p1 p2 p3) (d3 K q1 q2 q3 q1 q2 q3)))
      (K.mul (d3 K (K.neg (K.add p1 q1)) (K.neg (K.add p2 q2)) (K.neg (K.add p3 q3))
                   (K.neg (K.add p1 q1)) (K.neg (K.add p2 q2)) (K.neg (K.add p3 q3)))
        (K.add (K.add (d3 K p1 p2 p3 p1 p2 p3) (d3 K p1 p2 p3 p1 p2 p3))
               (K.add (d3 K q1 q2 q3 q1 q2 q3) (d3 K q1 q2 q3 q1 q2 q3)))) := by
  rw [diff_eq K]
  refine le_trans' K P _ _ _ (cs3 K P _ _ _ _ _ _) ?_
  rw [K.mul_comm (d3 K (sub K q1 p1) (sub K q2 p2) (sub K q3 p3) (sub K q1 p1) (sub K q2 p2) (sub K q3 p3))]
  have pl : Le K P (d3 K (sub K q1 p1) (sub K q2 p2) (sub K q3 p3) (sub K q1 p1) (sub K q2 p2) (sub K q3 p3))
      (K.add (K.add (d3 K p1 p2 p3 p1 p2 p3) (d3 K p1 p2 p3 p1 p2 p3))
             (K.add (d3 K q1 q2 q3 q1 q2 q3) (d3 K q1 q2 q3 q1 q2 q3))) :=
    ⟨d3 K (K.add p1 q1) (K.add p2 q2) (K.add p3 q3) (K.add p1 q1) (K.add p2 q2) (K.add p3 q3),
      d3_nn K P _ _ _, by unfold d3; lib_ring⟩
  exact TriadBound.le_mul_both K P _ _ _ _ (d3_nn K P _ _ _)
    (P.nn_add _ _ (P.nn_add _ _ (d3_nn K P _ _ _) (d3_nn K P _ _ _)) (P.nn_add _ _ (d3_nn K P _ _ _) (d3_nn K P _ _ _)))
    (le_refl' K P _) pl

end Ring

end Commutator

#print axioms Commutator.d3_nn
#print axioms Commutator.diff_eq
#print axioms Commutator.cs3
#print axioms Commutator.comm_bound

ns/ContrRec.lean

lean_all/ns/ContrRec.lean

/-
  ns/ContrRec.lean — T37 cycle 1832 (goal G858a, piece 3): a contracting recursion with a source is bounded
  uniformly in n, over an ordered ring. If r, d, c, s ≥ 0 with r + d = 1 and c·d = 1 (c = 1/(1 − r)), and
  x_{n+1} ≤ r·x_n + s, then
    contr_inv   r·X + s + d·x_0 = X   for X = x_0 + s·c
    contr_rec   x_n ≤ x_0 + s·c   for every n
  — with r = (1 + k)ρ² (ρ the DampContract ratio) and s the step-size defect (1 + k)k³C this bounds the step-size
  error by x_0 + (1 + k)k³C/(1 − r) for all times, instead of gⁿ·n·(…) (ErrRec, StepCauchy).
  Lean 4 core plus ns/EnstGlobal (OrdR, le_mul_nn, lib_ring).
-/
import ns.EnstGlobal

namespace ContrRec
open DetChain NSChain DyadicSmallGlobal LibRing DyadicLarge

section Gen
variable {R : Type} (K : CRing R) (O : OrdR K)

theorem contr_inv (r d c s x0 : R) (hr : K.add r d = K.one) (hc : K.mul c d = K.one) :
    K.add (K.add (K.mul r (K.add x0 (K.mul s c))) s) (K.mul d x0) = K.add x0 (K.mul s c) := by
  have e1 : K.add (K.add (K.mul r (K.add x0 (K.mul s c))) s) (K.mul d x0)
      = K.add (K.add x0 (K.mul s c)) (K.add (K.mul (K.add (K.add r d) (K.neg K.one)) (K.add x0 (K.mul s c)))
          (K.mul s (K.add K.one (K.neg (K.mul c d))))) := by lib_ring
  rw [hr, hc] at e1
  rw [e1]
  lib_ring

theorem contr_rec (r d c s : R) (x : Nat → R) (hr0 : O.nn r) (hd : O.nn d) (hr : K.add r d = K.one)
    (hc : K.mul c d = K.one) (hx0 : O.nn (x 0)) (hs : O.nn s) (hcn : O.nn c)
    (step : ∀ n, Le K O.toCone (x (n + 1)) (K.add (K.mul r (x n)) s)) :
    ∀ n, Le K O.toCone (x n) (K.add (x 0) (K.mul s c))
  | 0 => ⟨K.mul s c, O.nn_mul _ _ hs hcn, rfl⟩
  | n + 1 => by
    have ih := contr_rec r d c s x hr0 hd hr hc hx0 hs hcn step n
    have a := le_add' K O.toCone _ _ _ _ (le_mul_nn K O.toCone r _ _ hr0 ih) (le_refl' K O.toCone s)
    exact le_trans' K O.toCone _ _ _ (step n) (le_trans' K O.toCone _ _ _ a
      ⟨K.mul d (x 0), O.nn_mul _ _ hd hx0, contr_inv K r d c s (x 0) hr hc⟩)

end Gen
end ContrRec

#print axioms ContrRec.contr_inv
#print axioms ContrRec.contr_rec

ns/CubeCount.lean

lean_all/ns/CubeCount.lean

/-
  ns/CubeCount.lean — T37 cycle 945 (goal G624c, piece 4): counting lattice modes in a cube.
  The integers with |x| ≤ n form an explicit list of length 2n + 1, the cube [−n, n]³ ⊂ ℤ³ an explicit list of
  length (2n + 1)·((2n + 1)·(2n + 1)), and so (NodupLen.len_le) any duplicate-free list of modes k with
  |k|_∞ ≤ n has at most (2n + 1)³ members.

  Lean 4 core plus ns/NodupLen; own list lemmas (core List.length_map / length_append use propext).
-/
import ns.NodupLen

namespace CubeCount

abbrev Mode := Int × Int × Int

/-- the integers of absolute value at most n -/
def ilist : Nat → List Int
  | 0 => [0]
  | n + 1 => Int.ofNat (n + 1) :: Int.negSucc n :: ilist n

theorem ilist_len : ∀ n, (ilist n).length = 2 * n + 1
  | 0 => rfl
  | n + 1 => by
    show (ilist n).length + 1 + 1 = 2 * (n + 1) + 1
    rw [ilist_len n]; rfl

theorem le_succ_cases {a n : Nat} (h : a ≤ n + 1) : a = n + 1 ∨ a ≤ n := by
  cases Nat.eq_or_lt_of_le h with
  | inl e => exact Or.inl e
  | inr l => exact Or.inr (Nat.le_of_lt_succ l)

theorem mem_ilist : ∀ (n : Nat) (x : Int), x.natAbs ≤ n → x ∈ ilist n
  | 0, x, h => by
    have h0 : x.natAbs = 0 := Nat.eq_zero_of_le_zero h
    have e : x = 0 := by
      cases x with
      | ofNat k =>
        have : k = 0 := h0
        rw [this]; rfl
      | negSucc k => exact nomatch h0
    rw [e]; exact List.Mem.head _
  | n + 1, x, h => by
    cases le_succ_cases h with
    | inl e =>
      cases x with
      | ofNat k =>
        have : k = n + 1 := e
        rw [this]; exact List.Mem.head _
      | negSucc k =>
        have : k + 1 = n + 1 := e
        have hk : k = n := Nat.succ.inj this
        rw [hk]; exact List.Mem.tail _ (List.Mem.head _)
    | inr l => exact List.Mem.tail _ (List.Mem.tail _ (mem_ilist n x 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.add_right_comm]

def row (a b : Int) : List Int → List Mode
  | [] => []
  | c :: cs => (a, b, c) :: row a b cs

def plane (a : Int) (M : List Int) : List Int → List Mode
  | [] => []
  | b :: bs => row a b M ++ plane a M bs

def cube (M : List Int) : List Int → List Mode
  | [] => []
  | a :: as => plane a M M ++ cube M as

theorem row_len (a b : Int) : ∀ (M : List Int), (row a b M).length = M.length
  | [] => rfl
  | _ :: cs => by
    show (row a b cs).length + 1 = cs.length + 1
    rw [row_len a b cs]

theorem plane_len (a : Int) (M : List Int) : ∀ (B : List Int), (plane a M B).length = B.length * M.length
  | [] => (Nat.zero_mul _).symm
  | _ :: bs => by
    show (row a _ M ++ plane a M bs).length = (bs.length + 1) * M.length
    rw [len_app, row_len, plane_len a M bs, Nat.succ_mul, Nat.add_comm]

theorem cube_len (M : List Int) : ∀ (A : List Int), (cube M A).length = A.length * (M.length * M.length)
  | [] => (Nat.zero_mul _).symm
  | _ :: as => by
    show (plane _ M M ++ cube M as).length = (as.length + 1) * (M.length * M.length)
    rw [len_app, plane_len, cube_len M as, Nat.succ_mul, Nat.add_comm]

theorem mem_row (a b c : Int) : ∀ (M : List Int), c ∈ M → (a, b, c) ∈ row a b M
  | [], h => nomatch h
  | _ :: cs, h => by
    cases h with
    | head => exact List.Mem.head _
    | tail _ h' => exact List.Mem.tail _ (mem_row a b c cs h')

theorem mem_plane (a b c : Int) (M : List Int) (hc : c ∈ M) : ∀ (B : List Int), b ∈ B → (a, b, c) ∈ plane a M B
  | [], h => nomatch h
  | _ :: bs, h => by
    cases h with
    | head => exact List.mem_append_left _ (mem_row a _ c M hc)
    | tail _ h' => exact List.mem_append_right _ (mem_plane a b c M hc bs h')

theorem mem_cube (a b c : Int) (M : List Int) (hb : b ∈ M) (hc : c ∈ M) :
    ∀ (A : List Int), a ∈ A → (a, b, c) ∈ cube M A
  | [], h => nomatch h
  | _ :: as, h => by
    cases h with
    | head => exact List.mem_append_left _ (mem_plane _ b c M hc M hb)
    | tail _ h' => exact List.mem_append_right _ (mem_cube a b c M hb hc as h')

/-- the sup norm bound |k|_∞ ≤ n -/
def InCube (n : Nat) (k : Mode) : Prop := k.1.natAbs ≤ n ∧ k.2.1.natAbs ≤ n ∧ k.2.2.natAbs ≤ n

/-- at most (2n + 1)³ distinct modes have |k|_∞ ≤ n -/
theorem count_cube (n : Nat) (l : List Mode) (hd : List.Pairwise (fun a b => a ≠ b) l)
    (hc : ∀ k, k ∈ l → InCube n k) :
    l.length ≤ (2 * n + 1) * ((2 * n + 1) * (2 * n + 1)) := by
  have h := NodupLen.len_le l (cube (ilist n) (ilist n)) hd (fun k hk => by
    have q := hc k hk
    exact mem_cube k.1 k.2.1 k.2.2 (ilist n) (mem_ilist n _ q.2.1) (mem_ilist n _ q.2.2) (ilist n)
      (mem_ilist n _ q.1))
  rw [cube_len, ilist_len] at h
  exact h

end CubeCount

#print axioms CubeCount.ilist_len
#print axioms CubeCount.mem_ilist
#print axioms CubeCount.cube_len
#print axioms CubeCount.mem_cube
#print axioms CubeCount.count_cube

ns/CutChoose.lean

lean_all/ns/CutChoose.lean

/-
  ns/CutChoose.lean — T37 (goal G859c2b2, piece b): choosing the Agmon cut, over an ordered field. For Z, B and a
  largest admissible index Mm with B ≤ 9(Mm+2)⁴Z, there is a cut Kc ≤ Mm with
    B ≤ 9(Kc+2)⁴Z     and     (Kc = 0  or  9(Kc+1)⁴Z ≤ B),
  i.e. (Kc+2)⁴ ≈ B/Z up to the factor (Kc+2)⁴ ≤ (2(Kc+1))⁴ (cut_ratio). Constructive: a scan over the indices using
  only the totality of the order (no decidability). This is the choice of K in the H² force bound
  ForceAgmonW.force_agmonW (Kc + 1 ≤ M with M = Mm + 1).

  Lean 4 core plus ns/LatticeStep, ns/EnstLocal.
-/
import ns.LatticeStep
import ns.EnstLocal

namespace CutChoose
open DetChain NSChain DyadicSmallGlobal LibRing DyadicLarge LatticeStep

section Gen
variable {R : Type} (K : CRing R) (O : OrdR K)

/-- the cut j is high enough: B ≤ 9(j+2)⁴Z -/
def Pc (Z B : R) (j : Nat) : Prop := Le K O.toCone B (K.mul (castN K 9) (K.mul (p4 K (castN K (j + 2))) Z))

/-- the cut j − 1 was too low: 9(j+1)⁴Z ≤ B -/
def Qc (Z B : R) (j : Nat) : Prop := Le K O.toCone (K.mul (castN K 9) (K.mul (p4 K (castN K (j + 1))) Z)) B

theorem cut_scan (Z B : R) : ∀ n : Nat, (∃ j, j ≤ n ∧ Pc K O Z B j ∧ (j = 0 ∨ Qc K O Z B j)) ∨ Qc K O Z B (n + 1)
  | 0 => by
    cases O.total B (K.mul (castN K 9) (K.mul (p4 K (castN K (0 + 2))) Z)) with
    | inl h => exact Or.inl ⟨0, Nat.le_refl 0, h, Or.inl rfl⟩
    | inr h => exact Or.inr h
  | n + 1 => by
    cases cut_scan Z B n with
    | inl h =>
      have ⟨j, hj, hp, hq⟩ := h
      exact Or.inl ⟨j, Nat.le_succ_of_le hj, hp, hq⟩
    | inr hq =>
      cases O.total B (K.mul (castN K 9) (K.mul (p4 K (castN K (n + 1 + 2))) Z)) with
      | inl h => exact Or.inl ⟨n + 1, Nat.le_refl _, h, Or.inr hq⟩
      | inr h => exact Or.inr h

/-- **The cut exists.** -/
theorem cut_choose (Z B : R) (Mm : Nat) (hBM : Pc K O Z B Mm) :
    ∃ Kc, Kc ≤ Mm ∧ Pc K O Z B Kc ∧ (Kc = 0 ∨ Qc K O Z B Kc) := by
  cases Mm with
  | zero => exact ⟨0, Nat.le_refl 0, hBM, Or.inl rfl⟩
  | succ m =>
    cases cut_scan K O Z B m with
    | inl h =>
      have ⟨j, hj, hp, hq⟩ := h
      exact ⟨j, Nat.le_succ_of_le hj, hp, hq⟩
    | inr hq => exact ⟨m + 1, Nat.le_refl _, hBM, Or.inr hq⟩

/-- (j+2)⁴ ≤ (2(j+1))⁴ -/
theorem cut_ratio (j : Nat) :
    Le K O.toCone (p4 K (castN K (j + 2))) (p4 K (K.add (castN K (j + 1)) (castN K (j + 1)))) := by
  have e : castN K (j + 2) = K.add (castN K (j + 1)) (castN K 1) := RatModel.cast_add K (j + 1) 1
  have h1 : Le K O.toCone (castN K 1) (castN K (j + 1)) :=
    RatModel.cast_mono K O.toCone 1 (j + 1) (Nat.succ_le_succ (Nat.zero_le j))
  have hx : Le K O.toCone (castN K (j + 2)) (K.add (castN K (j + 1)) (castN K (j + 1))) := by
    rw [e]; exact le_add' K O.toCone _ _ _ _ (le_refl' K O.toCone _) h1
  have n1 : O.nn (castN K (j + 2)) := RatModel.nn_cast K O.toCone _
  have n2 : O.nn (K.add (castN K (j + 1)) (castN K (j + 1))) :=
    O.nn_add _ _ (RatModel.nn_cast K O.toCone _) (RatModel.nn_cast K O.toCone _)
  have s := EnstLocal.sq_mono K O _ _ n1 n2 hx
  exact EnstLocal.sq_mono K O _ _ (O.nn_sq _) (O.nn_sq _) s

end Gen

end CutChoose

#print axioms CutChoose.cut_scan
#print axioms CutChoose.cut_choose
#print axioms CutChoose.cut_ratio

ns/DampContract.lean

lean_all/ns/DampContract.lean

/-
  ns/DampContract.lean — T37 cycle 1830 (goal G858a, piece 2): strict contraction of the enstrophy difference, over an
  ordered field. With Q_t = νZ_t + hν²A_t, Z_t ≤ A_t, α_t ≥ 0 and the difference step
      Q_{t+1} ≤ νZ_t + h(α_tZ_t + β_tA_t)   (ForcedDiffStep with zero defect),
  if 2(α_t + β_t) ≤ ν² then
    contr_diss  2Q_{t+1} + hν²A_t ≤ 2Q_t
    geo_step    2m·Q_{t+1} ≤ (2ν + hν²)·Q_t,   m = ν + hν²           (as GeomDecay, from any such dissipation)
    geo         (2m)ⁿ·Q_n ≤ (2ν + hν²)ⁿ·Q_0
  so two runs of the scheme approach each other geometrically: the factor that absorbs the (1 + k) of SplitK in the
  step-size comparison.
  Lean 4 core plus ns/GeomDecay.
-/
import ns.GeomDecay

namespace DampContract
open DetChain NSChain DyadicSmallGlobal LibRing DyadicLarge EnstGlobal EnstGronwall

section Gen
variable {R : Type} (K : CRing R) (O : OrdR K)

theorem contr_diss (ν h α β Z A Z' A' : R) (hh : O.nn h) (hα : O.nn α) (hA : O.nn A)
    (hZA : Le K O.toCone Z A) (hab : Le K O.toCone (K.mul (K.add K.one K.one) (K.add α β)) (K.mul ν ν))
    (step : Le K O.toCone (K.add (K.mul ν Z') (K.mul (K.mul h (K.mul ν ν)) A'))
      (K.add (K.mul ν Z) (K.mul h (K.add (K.mul α Z) (K.mul β A))))) :
    Le K O.toCone (K.add (K.mul (K.add K.one K.one) (K.add (K.mul ν Z') (K.mul (K.mul h (K.mul ν ν)) A')))
        (K.mul (K.mul h (K.mul ν ν)) A))
      (K.mul (K.add K.one K.one) (K.add (K.mul ν Z) (K.mul (K.mul h (K.mul ν ν)) A))) := by
  have two : O.nn (K.add K.one K.one) := O.nn_add _ _ (RatModel.nn_one' K O.toCone) (RatModel.nn_one' K O.toCone)
  have s2 := le_mul_nn K O.toCone (K.add K.one K.one) _ _ two step
  have b1 := le_mul_nn K O.toCone α _ _ hα hZA
  have b2 := le_mul_nn K O.toCone (K.mul (K.add K.one K.one) h) _ _ (O.nn_mul _ _ two hh)
    (le_add' K O.toCone _ _ _ _ b1 (le_refl' K O.toCone (K.mul β A)))
  have b3 := le_mul_nn K O.toCone (K.mul h A) _ _ (O.nn_mul _ _ hh hA) hab
  have c := le_add' K O.toCone _ _ _ _ s2 (le_refl' K O.toCone (K.mul (K.mul h (K.mul ν ν)) A))
  refine le_trans' K O.toCone _ _ _ c (le_trans' K O.toCone _ _ _ ⟨K.zero, O.nn_zero, ?_⟩
    (le_trans' K O.toCone _ _ _ (le_add' K O.toCone _ _ _ _ (le_add' K O.toCone _ _ _ _
      (le_refl' K O.toCone (K.mul (K.add K.one K.one) (K.mul ν Z))) (le_trans' K O.toCone _ _ _ b2
        (le_trans' K O.toCone _ _ _ ⟨K.zero, O.nn_zero, ?_⟩ b3)))
      (le_refl' K O.toCone (K.mul (K.mul h (K.mul ν ν)) A))) ⟨K.zero, O.nn_zero, ?_⟩))
  · show K.add (K.add (K.mul (K.add K.one K.one) (K.add (K.mul ν Z) (K.mul h (K.add (K.mul α Z) (K.mul β A)))))
        (K.mul (K.mul h (K.mul ν ν)) A)) K.zero
      = K.add (K.add (K.mul (K.add K.one K.one) (K.mul ν Z))
          (K.mul (K.mul (K.add K.one K.one) h) (K.add (K.mul α Z) (K.mul β A)))) (K.mul (K.mul h (K.mul ν ν)) A)
    lib_ring
  · show K.add (K.mul (K.mul (K.add K.one K.one) h) (K.add (K.mul α A) (K.mul β A))) K.zero
      = K.mul (K.mul h A) (K.mul (K.add K.one K.one) (K.add α β))
    lib_ring
  · show K.add (K.add (K.add (K.mul (K.add K.one K.one) (K.mul ν Z)) (K.mul (K.mul h A) (K.mul ν ν)))
        (K.mul (K.mul h (K.mul ν ν)) A)) K.zero
      = K.mul (K.add K.one K.one) (K.add (K.mul ν Z) (K.mul (K.mul h (K.mul ν ν)) A))
    lib_ring

theorem geo_step (ν h : R) (Z A : Nat → R) (hν : O.nn ν) (hh : O.nn h) (hZA : ∀ t, Le K O.toCone (Z t) (A t))
    (diss : ∀ t, Le K O.toCone (K.add (K.mul (K.add K.one K.one) (Q K ν h Z A (t + 1))) (K.mul (K.mul h (K.mul ν ν)) (A t)))
      (K.mul (K.add K.one K.one) (Q K ν h Z A t))) (t : Nat) :
    Le K O.toCone (K.mul (K.mul (K.add K.one K.one) (K.add ν (K.mul h (K.mul ν ν)))) (Q K ν h Z A (t + 1)))
      (K.mul (K.add (K.add ν ν) (K.mul h (K.mul ν ν))) (Q K ν h Z A t)) := by
  have hm : O.nn (K.add ν (K.mul h (K.mul ν ν))) := O.nn_add _ _ hν (O.nn_mul _ _ hh (O.nn_sq ν))
  have hk : O.nn (K.mul h (K.mul ν ν)) := O.nn_mul _ _ hh (O.nn_sq ν)
  have d := diss t
  have d2 := le_mul_nn K O.toCone _ _ _ hm d
  have q : Le K O.toCone (K.mul (K.mul h (K.mul ν ν)) (Q K ν h Z A t)) (K.mul (K.mul h (K.mul ν ν)) (K.mul (K.add ν (K.mul h (K.mul ν ν))) (A t))) :=
    le_mul_nn K O.toCone (K.mul h (K.mul ν ν)) _ _ hk (GeomDecay.q_le_mA K O ν h (Z t) (A t) hν (hZA t))
  have e1 : K.add (K.add (K.mul (K.mul (K.add K.one K.one) (K.add ν (K.mul h (K.mul ν ν)))) (Q K ν h Z A (t + 1))) (K.mul (K.mul h (K.mul ν ν)) (K.mul (K.add ν (K.mul h (K.mul ν ν))) (A t)))) K.zero
      = K.mul (K.add ν (K.mul h (K.mul ν ν))) (K.add (K.mul (K.add K.one K.one) (Q K ν h Z A (t + 1))) (K.mul (K.mul h (K.mul ν ν)) (A t))) := by
    generalize Q K ν h Z A (t + 1) = Q1
    lib_ring
  have s2 := le_trans' K O.toCone _ _ _ (le_add' K O.toCone _ _ _ _ (le_refl' K O.toCone (K.mul (K.mul (K.add K.one K.one) (K.add ν (K.mul h (K.mul ν ν)))) (Q K ν h Z A (t + 1)))) q)
    (le_trans' K O.toCone _ _ _ ⟨K.zero, O.nn_zero, e1⟩ d2)
  obtain ⟨e, he, hee⟩ := s2
  refine ⟨e, he, ?_⟩
  calc K.add (K.mul (K.mul (K.add K.one K.one) (K.add ν (K.mul h (K.mul ν ν)))) (Q K ν h Z A (t + 1))) e
      = K.add (K.add (K.add (K.mul (K.mul (K.add K.one K.one) (K.add ν (K.mul h (K.mul ν ν)))) (Q K ν h Z A (t + 1))) (K.mul (K.mul h (K.mul ν ν)) (Q K ν h Z A t))) e) (K.neg (K.mul (K.mul h (K.mul ν ν)) (Q K ν h Z A t))) := by
        generalize Q K ν h Z A (t + 1) = Q1
        generalize Q K ν h Z A t = Q0
        lib_ring
    _ = K.add (K.mul (K.add ν (K.mul h (K.mul ν ν))) (K.mul (K.add K.one K.one) (Q K ν h Z A t))) (K.neg (K.mul (K.mul h (K.mul ν ν)) (Q K ν h Z A t))) := by rw [hee]
    _ = K.mul (K.add (K.add ν ν) (K.mul h (K.mul ν ν))) (Q K ν h Z A t) := by
        generalize Q K ν h Z A t = Q0
        lib_ring

theorem geo (ν h : R) (Z A : Nat → R) (hν : O.nn ν) (hh : O.nn h) (hZA : ∀ t, Le K O.toCone (Z t) (A t))
    (diss : ∀ t, Le K O.toCone (K.add (K.mul (K.add K.one K.one) (Q K ν h Z A (t + 1))) (K.mul (K.mul h (K.mul ν ν)) (A t)))
      (K.mul (K.add K.one K.one) (Q K ν h Z A t))) :
    ∀ n, Le K O.toCone (K.mul (pwr K (K.mul (K.add K.one K.one) (K.add ν (K.mul h (K.mul ν ν)))) n) (Q K ν h Z A n))
      (K.mul (pwr K (K.add (K.add ν ν) (K.mul h (K.mul ν ν))) n) (Q K ν h Z A 0))
  | 0 => le_refl' K O.toCone _
  | n + 1 => by
    have ih := geo ν h Z A hν hh hZA diss n
    have ds := geo_step K O ν h Z A hν hh hZA diss n
    have hk : O.nn (K.mul h (K.mul ν ν)) := O.nn_mul _ _ hh (O.nn_sq ν)
    have nb : O.nn (K.add (K.add ν ν) (K.mul h (K.mul ν ν))) := O.nn_add _ _ (O.nn_add _ _ hν hν) hk
    have nm : O.nn (K.mul (K.add K.one K.one) (K.add ν (K.mul h (K.mul ν ν)))) :=
      O.nn_mul _ _ (O.nn_add _ _ (RatModel.nn_one' K O.toCone) (RatModel.nn_one' K O.toCone)) (O.nn_add _ _ hν hk)
    have a := le_mul_nn K O.toCone (pwr K (K.mul (K.add K.one K.one) (K.add ν (K.mul h (K.mul ν ν)))) n) _ _
      (pwr_nn K O _ nm n) ds
    have b := le_mul_nn K O.toCone (K.add (K.add ν ν) (K.mul h (K.mul ν ν))) _ _ nb ih
    refine le_trans' K O.toCone _ _ _ ⟨K.zero, O.nn_zero, ?_⟩ (le_trans' K O.toCone _ _ _ a
      (le_trans' K O.toCone _ _ _ ⟨K.zero, O.nn_zero, ?_⟩ (le_trans' K O.toCone _ _ _ b ⟨K.zero, O.nn_zero, ?_⟩)))
    · show K.add (K.mul (K.mul (K.mul (K.add K.one K.one) (K.add ν (K.mul h (K.mul ν ν)))) (pwr K (K.mul (K.add K.one K.one) (K.add ν (K.mul h (K.mul ν ν)))) n)) (Q K ν h Z A (n + 1))) K.zero
        = K.mul (pwr K (K.mul (K.add K.one K.one) (K.add ν (K.mul h (K.mul ν ν)))) n) (K.mul (K.mul (K.add K.one K.one) (K.add ν (K.mul h (K.mul ν ν)))) (Q K ν h Z A (n + 1)))
      generalize Q K ν h Z A (n + 1) = Q1
      generalize pwr K (K.mul (K.add K.one K.one) (K.add ν (K.mul h (K.mul ν ν)))) n = P
      lib_ring
    · generalize Q K ν h Z A n = Qn
      generalize pwr K (K.mul (K.add K.one K.one) (K.add ν (K.mul h (K.mul ν ν)))) n = P
      lib_ring
    · show K.add (K.mul (K.add (K.add ν ν) (K.mul h (K.mul ν ν))) (K.mul (pwr K (K.add (K.add ν ν) (K.mul h (K.mul ν ν))) n) (Q K ν h Z A 0))) K.zero
        = K.mul (K.mul (K.add (K.add ν ν) (K.mul h (K.mul ν ν))) (pwr K (K.add (K.add ν ν) (K.mul h (K.mul ν ν))) n)) (Q K ν h Z A 0)
      generalize Q K ν h Z A 0 = Q0
      generalize pwr K (K.add (K.add ν ν) (K.mul h (K.mul ν ν))) n = P
      lib_ring

end Gen
end DampContract

#print axioms DampContract.contr_diss
#print axioms DampContract.geo_step
#print axioms DampContract.geo

ns/DampGronwall.lean

lean_all/ns/DampGronwall.lean

/-
  ns/DampGronwall.lean — T37 cycle 1806 (goal G857a): the enstrophy-difference recursion without exponential growth,
  over an ordered ring. With Q_t = νZ_t + hν²A_t and the step of GronwallSrc / ForcedDiffStep,
      Q_{t+1} ≤ νZ_t + h(α_tZ_t + β_tA_t) + h·S_t,
  if Z_t ≤ A_t (w ≥ 1 on integer modes, EnstH2.Z_le_A) and α_t + β_t ≤ ν², then
    damp_step   Q_{t+1} ≤ Q_t + h·S_t
    damp_all    S_t ≤ Sb  ⇒  Q_n ≤ Q_0 + n·h·Sb
  — the dissipation hν²A absorbs the whole Lipschitz term, so the comparison constants grow linearly in time.
  Lean 4 core plus ns/EnstGlobal (Q, le_mul_nn).
-/
import ns.EnstGlobal

namespace DampGronwall
open DetChain NSChain DyadicSmallGlobal DyadicLarge LibRing EnstGlobal

section Gen
variable {R : Type} (K : CRing R) (O : OrdR K)

theorem damp_step (ν h α β S Z A Z' A' : R) (hh : O.nn h) (hα : O.nn α) (hA : O.nn A)
    (hZA : Le K O.toCone Z A) (hab : Le K O.toCone (K.add α β) (K.mul ν ν))
    (step : Le K O.toCone (K.add (K.mul ν Z') (K.mul (K.mul h (K.mul ν ν)) A'))
      (K.add (K.add (K.mul ν Z) (K.mul h (K.add (K.mul α Z) (K.mul β A)))) (K.mul h S))) :
    Le K O.toCone (K.add (K.mul ν Z') (K.mul (K.mul h (K.mul ν ν)) A'))
      (K.add (K.add (K.mul ν Z) (K.mul (K.mul h (K.mul ν ν)) A)) (K.mul h S)) := by
  have b1 := le_mul_nn K O.toCone α _ _ hα hZA
  have b2 := le_add' K O.toCone _ _ _ _ b1 (le_refl' K O.toCone (K.mul β A))
  have b3 := le_mul_nn K O.toCone A _ _ hA hab
  have b4 : Le K O.toCone (K.add (K.mul α Z) (K.mul β A)) (K.mul A (K.mul ν ν)) :=
    le_trans' K O.toCone _ _ _ b2 (le_trans' K O.toCone _ _ _ ⟨K.zero, O.nn_zero, by lib_ring⟩ b3)
  have b5 := le_mul_nn K O.toCone h _ _ hh b4
  refine le_trans' K O.toCone _ _ _ step (le_trans' K O.toCone _ _ _
    (le_add' K O.toCone _ _ _ _ (le_add' K O.toCone _ _ _ _ (le_refl' K O.toCone (K.mul ν Z)) b5)
      (le_refl' K O.toCone (K.mul h S))) ⟨K.zero, O.nn_zero, ?_⟩)
  lib_ring

theorem damp_all (ν h Sb : R) (Z A α β S : Nat → R) (hh : O.nn h) (hα : ∀ t, O.nn (α t))
    (hA : ∀ t, O.nn (A t)) (hZA : ∀ t, Le K O.toCone (Z t) (A t))
    (hab : ∀ t, Le K O.toCone (K.add (α t) (β t)) (K.mul ν ν)) (hS : ∀ t, Le K O.toCone (S t) Sb)
    (step : ∀ t, Le K O.toCone (Q K ν h Z A (t + 1))
      (K.add (K.add (K.mul ν (Z t)) (K.mul h (K.add (K.mul (α t) (Z t)) (K.mul (β t) (A t))))) (K.mul h (S t)))) :
    ∀ n, Le K O.toCone (Q K ν h Z A n) (K.add (Q K ν h Z A 0) (K.mul (castN K n) (K.mul h Sb)))
  | 0 => ⟨K.zero, O.nn_zero, by show K.add (Q K ν h Z A 0) K.zero = K.add (Q K ν h Z A 0) (K.mul K.zero (K.mul h Sb)); lib_ring⟩
  | n + 1 => by
    have d := damp_step K O ν h (α n) (β n) (S n) (Z n) (A n) (Z (n + 1)) (A (n + 1)) hh (hα n) (hA n) (hZA n)
      (hab n) (step n)
    have ih := damp_all ν h Sb Z A α β S hh hα hA hZA hab hS step n
    have c := le_add' K O.toCone _ _ _ _ ih (le_mul_nn K O.toCone h _ _ hh (hS n))
    refine le_trans' K O.toCone _ _ _ d (le_trans' K O.toCone _ _ _ c ⟨K.zero, O.nn_zero, ?_⟩)
    show K.add (K.add (K.add (Q K ν h Z A 0) (K.mul (castN K n) (K.mul h Sb))) (K.mul h Sb)) K.zero
      = K.add (Q K ν h Z A 0) (K.mul (K.add K.one (castN K n)) (K.mul h Sb))
    lib_ring

end Gen
end DampGronwall

#print axioms DampGronwall.damp_step
#print axioms DampGronwall.damp_all

ns/DampSum.lean

lean_all/ns/DampSum.lean

/-
  ns/DampSum.lean — T37 cycle 1816 (goal G857d, piece 1): DampGronwall with a time-dependent source, over an ordered
  ring. With Q_t = νZ_t + hν²A_t, Z_t ≤ A_t, α_t + β_t ≤ ν² and the step
      Q_{t+1} ≤ νZ_t + h(α_tZ_t + β_tA_t) + h·S_t,
    damp_sum   Q_n ≤ Q_0 + Σ_{t<n} h·S_t
  so a source whose time integral is finite (dissipation sums, EnstDiss) gives a bound uniform in n.
  Lean 4 core plus ns/DampGronwall, ns/SchemeTele (tsum).
-/
import ns.DampGronwall
import ns.SchemeTele

namespace DampSum
open DetChain NSChain DyadicSmallGlobal DyadicLarge LibRing EnstGlobal SchemeTele

section Gen
variable {R : Type} (K : CRing R) (O : OrdR K)

theorem damp_sum (ν h : R) (Z A α β S : Nat → R) (hh : O.nn h) (hα : ∀ t, O.nn (α t))
    (hA : ∀ t, O.nn (A t)) (hZA : ∀ t, Le K O.toCone (Z t) (A t))
    (hab : ∀ t, Le K O.toCone (K.add (α t) (β t)) (K.mul ν ν))
    (step : ∀ t, Le K O.toCone (Q K ν h Z A (t + 1))
      (K.add (K.add (K.mul ν (Z t)) (K.mul h (K.add (K.mul (α t) (Z t)) (K.mul (β t) (A t))))) (K.mul h (S t)))) :
    ∀ n, Le K O.toCone (Q K ν h Z A n) (K.add (Q K ν h Z A 0) (tsum K (fun t => K.mul h (S t)) n))
  | 0 => ⟨K.zero, O.nn_zero, by show K.add (Q K ν h Z A 0) K.zero = K.add (Q K ν h Z A 0) K.zero; rfl⟩
  | n + 1 => by
    have d := DampGronwall.damp_step K O ν h (α n) (β n) (S n) (Z n) (A n) (Z (n + 1)) (A (n + 1)) hh (hα n) (hA n)
      (hZA n) (hab n) (step n)
    have ih := damp_sum ν h Z A α β S hh hα hA hZA hab step n
    have c := le_add' K O.toCone _ _ _ _ ih (le_refl' K O.toCone (K.mul h (S n)))
    refine le_trans' K O.toCone _ _ _ d (le_trans' K O.toCone _ _ _ c ⟨K.zero, O.nn_zero, ?_⟩)
    show K.add (K.add (K.add (Q K ν h Z A 0) (tsum K (fun t => K.mul h (S t)) n)) (K.mul h (S n))) K.zero
      = K.add (Q K ν h Z A 0) (K.add (tsum K (fun t => K.mul h (S t)) n) (K.mul h (S n)))
    lib_ring

end Gen
end DampSum

#print axioms DampSum.damp_sum

ns/DefectA0.lean

lean_all/ns/DefectA0.lean

/-
  ns/DefectA0.lean — T37 cycle 1085 (goal G717, piece a): the viscous part of the one-step/two-step defect. Over an
  ordered ring without zero divisors, for x ≥ 0 and c = (1 + 2x)(1 + x)²:
    c_ge_x        x ≤ c
    defect_a0     c·y = x²·a₀  ⇒  y² ≤ x²·a₀²
  (c²y² = x⁴a₀² ≤ x²c²a₀², cancel c² ≠ 0). With x = kνw this bounds the a₀-term of StepHalf.half_defect by
  k²ν²w²|a₀|², i.e. Z-energy ≤ k²ν²·Σw³|a₀|² = k²ν²B (EnstH2 controls Σ_t k B_t).

  Lean 4 core plus ns/EnstGlobal (le_of_mul_le), ns/EnstLocal (sq_mono), lib/RingTac.
-/
import ns.EnstGlobal
import ns.EnstLocal
import ns.ForceSob

namespace DefectA0
open DetChain NSChain DyadicSmallGlobal LibRing DyadicLarge EnstGlobal

section Gen
variable {R : Type} (K : CRing R) (O : OrdR K)

def cc (x : R) : R := K.mul (K.add K.one (K.add x x)) (K.mul (K.add K.one x) (K.add K.one x))

def k2 : R := K.add K.one K.one
def k3 : R := K.add (k2 K) K.one
def k4 : R := K.add (k2 K) (k2 K)
def k5 : R := K.add (k4 K) K.one

theorem kn (h1 : O.nn K.one) : O.nn (k2 K) ∧ O.nn (k3 K) ∧ O.nn (k4 K) ∧ O.nn (k5 K) := by
  have a : O.nn (k2 K) := O.nn_add _ _ h1 h1
  have b : O.nn (k4 K) := O.nn_add _ _ a a
  exact ⟨a, O.nn_add _ _ a h1, b, O.nn_add _ _ b h1⟩

theorem c_ge_x (x : R) (hx : O.nn x) : Le K O.toCone x (cc K x) := by
  have ⟨n2, n3, _, n5⟩ := kn K O (RatModel.nn_one' K O.toCone)
  refine ⟨K.add K.one (K.add (K.mul (k3 K) x) (K.add (K.mul (k5 K) (K.mul x x)) (K.mul (k2 K) (K.mul (K.mul x x) x)))),
    O.nn_add _ _ (RatModel.nn_one' K O.toCone) (O.nn_add _ _ (O.nn_mul _ _ n3 hx)
      (O.nn_add _ _ (O.nn_mul _ _ n5 (O.nn_sq x)) (O.nn_mul _ _ n2 (O.nn_mul _ _ (O.nn_sq x) hx)))), ?_⟩
  unfold cc k5 k4 k3 k2; lib_ring

theorem cc_ne (x : R) (hx : O.nn x) (h10 : K.one ≠ K.zero) : cc K x ≠ K.zero := by
  have ⟨n2, _, n4, n5⟩ := kn K O (RatModel.nn_one' K O.toCone)
  intro e
  have h : Le K O.toCone K.one (cc K x) := by
    refine ⟨K.add (K.mul (k4 K) x) (K.add (K.mul (k5 K) (K.mul x x)) (K.mul (k2 K) (K.mul (K.mul x x) x))),
      O.nn_add _ _ (O.nn_mul _ _ n4 hx) (O.nn_add _ _ (O.nn_mul _ _ n5 (O.nn_sq x)) (O.nn_mul _ _ n2 (O.nn_mul _ _ (O.nn_sq x) hx))), ?_⟩
    unfold cc k5 k4 k2; lib_ring
  rw [e] at h
  have h2 : Le K O.toCone K.zero K.one := ⟨K.one, RatModel.nn_one' K O.toCone, by lib_ring⟩
  exact h10 (O.antisym _ _ h h2)

theorem defect_a0 (x y a0 : R) (hx : O.nn x) (h10 : K.one ≠ K.zero) (h : K.mul (cc K x) y = K.mul (K.mul x x) a0) :
    Le K O.toCone (K.mul y y) (K.mul (K.mul x x) (K.mul a0 a0)) := by
  have hc := c_ge_x K O x hx
  have hcn : O.nn (cc K x) := by
    cases hc with
    | intro d hd => rw [← hd.2]; exact O.nn_add _ _ hx hd.1
  have sq := EnstLocal.sq_mono K O _ _ hx hcn hc
  -- c²y² = x⁴a₀² ≤ c²x²a₀²
  have m := ForceSob.le_mul_right' K O.toCone sq (O.nn_mul _ _ (O.nn_sq x) (O.nn_sq a0))
  have e : K.mul (K.mul x x) (K.mul (K.mul x x) (K.mul a0 a0)) = K.mul (K.mul (cc K x) (cc K x)) (K.mul y y) := by
    calc _ = K.mul (K.mul (K.mul x x) a0) (K.mul (K.mul x x) a0) := by lib_ring
      _ = K.mul (K.mul (cc K x) y) (K.mul (cc K x) y) := by rw [h]
      _ = _ := by lib_ring
  rw [e] at m
  have ccne : K.mul (cc K x) (cc K x) ≠ K.zero := by
    intro z
    cases O.noZD _ _ z with
    | inl h' => exact cc_ne K O x hx h10 h'
    | inr h' => exact cc_ne K O x hx h10 h'
  exact le_of_mul_le K O _ _ _ (O.nn_sq _) ccne m

end Gen

end DefectA0

#print axioms DefectA0.kn
#print axioms DefectA0.c_ge_x
#print axioms DefectA0.cc_ne
#print axioms DefectA0.defect_a0

ns/DefectOrder.lean

lean_all/ns/DefectOrder.lean

/-
  ns/DefectOrder.lean — T37 cycle 1111 (goal G717, piece b8b): the order count of the one-step/two-step defect, over an
  ordered field (all quantities ≥ 0, 0 ≤ k ≤ 1):
    k2_le       k²X ≤ kX
    lip_k       2k²G + 2k²ν²B ≤ k(2G + 2ν²B)                     (TimeLipW inputs are O(k))
    visc_k3     kν²B₅ ≤ Q  ⇒  k⁴ν⁴B₅ ≤ k³(ν²Q)                    (EnstH4 controls hν²B₅)
    fd_k        FD ≤ 2ι²·B_d·P + 2ι²·B_b·(2S₁Z_d + 2S₀A_d), B_d ≤ kB₁, Z_d ≤ kZ₁, A_d ≤ kA₁, P ≤ P₀, B_b ≤ E,
                S₁ ≤ s₁, S₀ ≤ s₀  ⇒  FD ≤ k·(2ι²B₁P₀ + 2ι²E(2s₁Z₁ + 2s₀A₁))   (ForceDiffW)
    defect_k3   S ≤ 3(k⁴ν⁴B₅ + k²FD + k³νG), k⁴ν⁴B₅ ≤ k³V, FD ≤ kF, G ≤ E  ⇒  S ≤ k³·3(V + F + νE)
    defect_qe   νZ(D) + kν²A(D) ≤ k³·3(ν(V + F + νE) + ν²(ν²E + E + νE)) from the Z- and A-defect bounds (k ≤ 1)
    defect_qe'  the same with k·H₃ ≤ E in place of H₃ ≤ E (uniform in the step: H₃ ~ Σw³|N|² is only O(1/k))
  so the weighted defect Σw|a₁ − b₂|² of DefectSum.defect_sum4 is O(k³) per step under the small-data H⁴ bounds.

  Lean 4 core plus ns/DefectA0 (k3), ns/TruncRate (mm), ns/ForceSob (le_mul_right').
-/
import ns.DefectA0
import ns.TruncRate

namespace DefectOrder
open DetChain NSChain DyadicSmallGlobal LibRing DyadicLarge DefectA0

section Gen
variable {R : Type} (K : CRing R) (O : OrdR K)

theorem k2_le (k X : R) (hk : O.nn k) (hk1 : Le K O.toCone k K.one) (hX : O.nn X) :
    Le K O.toCone (K.mul (K.mul k k) X) (K.mul k X) := by
  have m := le_mul_nn K O.toCone k _ _ hk hk1
  have m2 := ForceSob.le_mul_right' K O.toCone m hX
  refine le_trans' K O.toCone _ _ _ m2 ⟨K.zero, O.nn_zero, ?_⟩
  lib_ring

theorem lip_k (k ν G B : R) (hk : O.nn k) (hk1 : Le K O.toCone k K.one) (hG : O.nn G) (hB : O.nn B) :
    Le K O.toCone (K.add (K.mul (K.mul (K.add K.one K.one) (K.mul k k)) G)
        (K.mul (K.mul (K.add K.one K.one) (K.mul (K.mul k k) (K.mul ν ν))) B))
      (K.mul k (K.add (K.mul (K.add K.one K.one) G) (K.mul (K.add K.one K.one) (K.mul (K.mul ν ν) B)))) := by
  have two : O.nn (K.add K.one K.one) := O.nn_add _ _ (RatModel.nn_one' K O.toCone) (RatModel.nn_one' K O.toCone)
  have X := k2_le K O k (K.add (K.mul (K.add K.one K.one) G) (K.mul (K.add K.one K.one) (K.mul (K.mul ν ν) B))) hk hk1
    (O.nn_add _ _ (O.nn_mul _ _ two hG) (O.nn_mul _ _ two (O.nn_mul _ _ (O.nn_sq ν) hB)))
  refine le_trans' K O.toCone _ _ _ ⟨K.zero, O.nn_zero, ?_⟩ X
  lib_ring

theorem visc_k3 (k ν B5 Q : R) (hk : O.nn k) (h : Le K O.toCone (K.mul (K.mul k (K.mul ν ν)) B5) Q) :
    Le K O.toCone (K.mul (K.mul (K.mul (K.mul k k) (K.mul k k)) (K.mul (K.mul ν ν) (K.mul ν ν))) B5)
      (K.mul (K.mul (K.mul k k) k) (K.mul (K.mul ν ν) Q)) := by
  have m := le_mul_nn K O.toCone (K.mul (K.mul (K.mul k k) k) (K.mul ν ν)) _ _
    (O.nn_mul _ _ (O.nn_mul _ _ (O.nn_sq k) hk) (O.nn_sq ν)) h
  refine le_trans' K O.toCone _ _ _ ⟨K.zero, O.nn_zero, ?_⟩ (le_trans' K O.toCone _ _ _ m ⟨K.zero, O.nn_zero, ?_⟩)
  · lib_ring
  · lib_ring

theorem fd_k (k ι FD Bd P Bb Zd Ad S1 S0 B1 Z1 A1 P0 E s1 s0 : R) (hk : O.nn k)
    (nBd : O.nn Bd) (nBb : O.nn Bb) (nS1 : O.nn S1) (nS0 : O.nn S0)
    (nP0 : O.nn P0) (nZ1 : O.nn Z1) (nA1 : O.nn A1) (ns1 : O.nn s1) (ns0 : O.nn s0)
    (hFD : Le K O.toCone FD (K.add (K.mul (K.add K.one K.one) (K.mul (K.mul ι ι) (K.mul Bd P)))
      (K.mul (K.add K.one K.one) (K.mul (K.mul ι ι) (K.mul Bb
        (K.add (K.mul (K.add K.one K.one) (K.mul S1 Zd)) (K.mul (K.add K.one K.one) (K.mul S0 Ad))))))))
    (hBd : Le K O.toCone Bd (K.mul k B1)) (hZd : Le K O.toCone Zd (K.mul k Z1)) (hAd : Le K O.toCone Ad (K.mul k A1))
    (hP : Le K O.toCone P P0) (hBb : Le K O.toCone Bb E) (hS1 : Le K O.toCone S1 s1) (hS0 : Le K O.toCone S0 s0) :
    Le K O.toCone FD (K.mul k (K.add (K.mul (K.add K.one K.one) (K.mul (K.mul ι ι) (K.mul B1 P0)))
      (K.mul (K.add K.one K.one) (K.mul (K.mul ι ι) (K.mul E
        (K.add (K.mul (K.add K.one K.one) (K.mul s1 Z1)) (K.mul (K.add K.one K.one) (K.mul s0 A1)))))))) := by
  have two : O.nn (K.add K.one K.one) := O.nn_add _ _ (RatModel.nn_one' K O.toCone) (RatModel.nn_one' K O.toCone)
  have t1 := TruncRate.mm K O _ _ _ _ nBd nP0 hBd hP
  have u1 := TruncRate.mm K O _ _ _ _ nS1 (O.nn_mul _ _ hk nZ1) hS1 hZd
  have u2 := TruncRate.mm K O _ _ _ _ nS0 (O.nn_mul _ _ hk nA1) hS0 hAd
  have u := le_add' K O.toCone _ _ _ _ (le_mul_nn K O.toCone _ _ _ two u1) (le_mul_nn K O.toCone _ _ _ two u2)
  have t2 := TruncRate.mm K O _ _ _ _ nBb
    (O.nn_add _ _ (O.nn_mul _ _ two (O.nn_mul _ _ ns1 (O.nn_mul _ _ hk nZ1))) (O.nn_mul _ _ two (O.nn_mul _ _ ns0 (O.nn_mul _ _ hk nA1)))) hBb u
  have s := le_add' K O.toCone _ _ _ _ (le_mul_nn K O.toCone _ _ _ two (le_mul_nn K O.toCone _ _ _ (O.nn_sq ι) t1))
    (le_mul_nn K O.toCone _ _ _ two (le_mul_nn K O.toCone _ _ _ (O.nn_sq ι) t2))
  refine le_trans' K O.toCone _ _ _ hFD (le_trans' K O.toCone _ _ _ s ⟨K.zero, O.nn_zero, ?_⟩)
  lib_ring

theorem defect_k3 (k ν S B5 FD G V F E : R) (hk : O.nn k) (hν : O.nn ν)
    (hS : Le K O.toCone S (K.mul (k3 K) (K.add (K.add (K.mul (K.mul (K.mul (K.mul k k) (K.mul k k)) (K.mul (K.mul ν ν) (K.mul ν ν))) B5)
      (K.mul (K.mul k k) FD)) (K.mul (K.mul (K.mul k k) (K.mul k ν)) G))))
    (hV : Le K O.toCone (K.mul (K.mul (K.mul (K.mul k k) (K.mul k k)) (K.mul (K.mul ν ν) (K.mul ν ν))) B5) (K.mul (K.mul (K.mul k k) k) V))
    (hF : Le K O.toCone FD (K.mul k F)) (hG : Le K O.toCone G E) :
    Le K O.toCone S (K.mul (K.mul (K.mul k k) k) (K.mul (k3 K) (K.add (K.add V F) (K.mul ν E)))) := by
  have ⟨_, n3, _, _⟩ := kn K O (RatModel.nn_one' K O.toCone)
  have a := le_add' K O.toCone _ _ _ _ (le_add' K O.toCone _ _ _ _ hV (le_mul_nn K O.toCone _ _ _ (O.nn_sq k) hF))
    (le_mul_nn K O.toCone _ _ _ (O.nn_mul _ _ (O.nn_sq k) (O.nn_mul _ _ hk hν)) hG)
  refine le_trans' K O.toCone _ _ _ hS (le_trans' K O.toCone _ _ _ (le_mul_nn K O.toCone _ _ _ n3 a) ⟨K.zero, O.nn_zero, ?_⟩)
  generalize k3 K = c
  lib_ring

theorem defect_qe (k ν ZD AD B5 FD H B4 FD2 H3 V F E : R) (hk : O.nn k) (hk1 : Le K O.toCone k K.one) (hν : O.nn ν)
    (hE : O.nn E)
    (hZ : Le K O.toCone ZD (K.mul (k3 K) (K.add (K.add (K.mul (K.mul (K.mul (K.mul k k) (K.mul k k)) (K.mul (K.mul ν ν) (K.mul ν ν))) B5)
      (K.mul (K.mul k k) FD)) (K.mul (K.mul (K.mul k k) (K.mul k ν)) H))))
    (hA : Le K O.toCone AD (K.mul (k3 K) (K.add (K.add (K.mul (K.mul (K.mul k k) (K.mul ν ν)) B4)
      (K.mul (K.mul k k) FD2)) (K.mul (K.mul (K.mul k k) (K.mul k ν)) H3))))
    (hV : Le K O.toCone (K.mul (K.mul (K.mul (K.mul k k) (K.mul k k)) (K.mul (K.mul ν ν) (K.mul ν ν))) B5) (K.mul (K.mul (K.mul k k) k) V))
    (hF : Le K O.toCone FD (K.mul k F)) (hH : Le K O.toCone H E) (hB4 : Le K O.toCone B4 E)
    (hF2 : Le K O.toCone FD2 E) (hH3 : Le K O.toCone H3 E) :
    Le K O.toCone (K.add (K.mul ν ZD) (K.mul (K.mul k (K.mul ν ν)) AD))
      (K.mul (K.mul (K.mul k k) k) (K.mul (k3 K) (K.add (K.mul ν (K.add (K.add V F) (K.mul ν E)))
        (K.mul (K.mul ν ν) (K.add (K.add (K.mul (K.mul ν ν) E) E) (K.mul ν E)))))) := by
  have ⟨_, n3, _, _⟩ := kn K O (RatModel.nn_one' K O.toCone)
  have z := defect_k3 K O k ν ZD B5 FD H V F E hk hν hZ hV hF hH
  -- k·k⁴ ≤ k·k³ part of the A-term: k³νH3 ≤ k²·(νE) ... use k ≤ 1 on the last term
  have a1 := le_mul_nn K O.toCone (K.mul (K.mul k k) (K.mul ν ν)) _ _ (O.nn_mul _ _ (O.nn_sq k) (O.nn_sq ν)) hB4
  have a2 := le_mul_nn K O.toCone (K.mul k k) _ _ (O.nn_sq k) hF2
  have hkk : Le K O.toCone (K.mul (K.mul (K.mul k k) (K.mul k ν)) H3) (K.mul (K.mul k k) (K.mul ν E)) := by
    have m1 := le_mul_nn K O.toCone (K.mul (K.mul (K.mul k k) k) ν) _ _ (O.nn_mul _ _ (O.nn_mul _ _ (O.nn_sq k) hk) hν) hH3
    have m2 := ForceSob.le_mul_right' K O.toCone (le_mul_nn K O.toCone (K.mul k k) _ _ (O.nn_sq k) hk1) (O.nn_mul _ _ hν hE)
    refine le_trans' K O.toCone _ _ _ ⟨K.zero, O.nn_zero, ?_⟩ (le_trans' K O.toCone _ _ _ m1 (le_trans' K O.toCone _ _ _ ⟨K.zero, O.nn_zero, ?_⟩
      (le_trans' K O.toCone _ _ _ m2 ⟨K.zero, O.nn_zero, ?_⟩)))
    · lib_ring
    · lib_ring
    · lib_ring
  have a := le_trans' K O.toCone _ _ _ hA (le_mul_nn K O.toCone _ _ _ n3 (le_add' K O.toCone _ _ _ _ (le_add' K O.toCone _ _ _ _ a1 a2) hkk))
  have s := le_add' K O.toCone _ _ _ _ (le_mul_nn K O.toCone ν _ _ hν z) (le_mul_nn K O.toCone (K.mul k (K.mul ν ν)) _ _ (O.nn_mul _ _ hk (O.nn_sq ν)) a)
  refine le_trans' K O.toCone _ _ _ s ⟨K.zero, O.nn_zero, ?_⟩
  generalize k3 K = c
  lib_ring

theorem defect_qe' (k ν ZD AD B5 FD H B4 FD2 H3 V F E : R) (hk : O.nn k) (hν : O.nn ν)
    (hZ : Le K O.toCone ZD (K.mul (k3 K) (K.add (K.add (K.mul (K.mul (K.mul (K.mul k k) (K.mul k k)) (K.mul (K.mul ν ν) (K.mul ν ν))) B5)
      (K.mul (K.mul k k) FD)) (K.mul (K.mul (K.mul k k) (K.mul k ν)) H))))
    (hA : Le K O.toCone AD (K.mul (k3 K) (K.add (K.add (K.mul (K.mul (K.mul k k) (K.mul ν ν)) B4)
      (K.mul (K.mul k k) FD2)) (K.mul (K.mul (K.mul k k) (K.mul k ν)) H3))))
    (hV : Le K O.toCone (K.mul (K.mul (K.mul (K.mul k k) (K.mul k k)) (K.mul (K.mul ν ν) (K.mul ν ν))) B5) (K.mul (K.mul (K.mul k k) k) V))
    (hF : Le K O.toCone FD (K.mul k F)) (hH : Le K O.toCone H E) (hB4 : Le K O.toCone B4 E)
    (hF2 : Le K O.toCone FD2 E) (hH3 : Le K O.toCone (K.mul k H3) E) :
    Le K O.toCone (K.add (K.mul ν ZD) (K.mul (K.mul k (K.mul ν ν)) AD))
      (K.mul (K.mul (K.mul k k) k) (K.mul (k3 K) (K.add (K.mul ν (K.add (K.add V F) (K.mul ν E)))
        (K.mul (K.mul ν ν) (K.add (K.add (K.mul (K.mul ν ν) E) E) (K.mul ν E)))))) := by
  have ⟨_, n3, _, _⟩ := kn K O (RatModel.nn_one' K O.toCone)
  have z := defect_k3 K O k ν ZD B5 FD H V F E hk hν hZ hV hF hH
  -- k·k⁴ ≤ k·k³ part of the A-term: k³νH3 ≤ k²·(νE) ... use k ≤ 1 on the last term
  have a1 := le_mul_nn K O.toCone (K.mul (K.mul k k) (K.mul ν ν)) _ _ (O.nn_mul _ _ (O.nn_sq k) (O.nn_sq ν)) hB4
  have a2 := le_mul_nn K O.toCone (K.mul k k) _ _ (O.nn_sq k) hF2
  have hkk : Le K O.toCone (K.mul (K.mul (K.mul k k) (K.mul k ν)) H3) (K.mul (K.mul k k) (K.mul ν E)) := by
    have m1 := le_mul_nn K O.toCone (K.mul (K.mul k k) ν) _ _ (O.nn_mul _ _ (O.nn_sq k) hν) hH3
    refine le_trans' K O.toCone _ _ _ ⟨K.zero, O.nn_zero, ?_⟩ (le_trans' K O.toCone _ _ _ m1 ⟨K.zero, O.nn_zero, ?_⟩)
    · lib_ring
    · lib_ring
  have a := le_trans' K O.toCone _ _ _ hA (le_mul_nn K O.toCone _ _ _ n3 (le_add' K O.toCone _ _ _ _ (le_add' K O.toCone _ _ _ _ a1 a2) hkk))
  have s := le_add' K O.toCone _ _ _ _ (le_mul_nn K O.toCone ν _ _ hν z) (le_mul_nn K O.toCone (K.mul k (K.mul ν ν)) _ _ (O.nn_mul _ _ hk (O.nn_sq ν)) a)
  refine le_trans' K O.toCone _ _ _ s ⟨K.zero, O.nn_zero, ?_⟩
  generalize k3 K = c
  lib_ring

end Gen

end DefectOrder

#print axioms DefectOrder.k2_le
#print axioms DefectOrder.lip_k
#print axioms DefectOrder.visc_k3
#print axioms DefectOrder.fd_k
#print axioms DefectOrder.defect_k3
#print axioms DefectOrder.defect_qe
#print axioms DefectOrder.defect_qe'

ns/DefectSplit.lean

lean_all/ns/DefectSplit.lean

/-
  ns/DefectSplit.lean — T37 cycle 1095 (goal G717, piece b1): the scalar splitting of the one-step/two-step defect.
  Over an ordered ring without zero divisors, with x ≥ 0, c = (1 + 2x)(1 + x)², p = 1 + 2x + 2x²:
    p_le_c, one_le_c, x_le_csq   p ≤ c,  1 ≤ c,  x ≤ c²
    split3                        (A + B + C)² ≤ 3(A² + B² + C²)
    defect_split                  c·D = x²a₀ + k·p·M − k·x·N₁  ⇒  D² ≤ 3(x²a₀² + k²M² + k²·x·N₁²)
    half_split                    for the StepHalf scheme (x = kνw, M = N₀ − N₁):
                                  (a₁ − b₂)² ≤ 3(x²a₀² + k²(N₀ − N₁)² + k²·x·N₁²)
    defect_split4, half_split4    the same with x⁴a₀² in place of x²a₀² (1 ≤ c): the viscous part is O(k²) per step
  The three terms are O(k²)·B (viscous), k²·|N₀ − N₁|² = O(k⁴) (force Lipschitz, b₁ − a₀ = O(k)), and
  k³ν·w|N₁|² (controlled by the H³ force bound ForceH3 and EnstH3).

  Lean 4 core plus ns/DefectA0, ns/StepHalf, ns/EnstGlobal, ns/EnstLocal, ns/TruncRate.
-/
import ns.DefectA0
import ns.StepHalf
import ns.TruncRate

namespace DefectSplit
open DetChain NSChain DyadicSmallGlobal LibRing DyadicLarge EnstGlobal DefectA0

section Gen
variable {R : Type} (K : CRing R) (O : OrdR K)

def pp (x : R) : R := K.add (K.add K.one (K.add x x)) (K.add (K.mul x x) (K.mul x x))

theorem p_le_c (x : R) (hx : O.nn x) : Le K O.toCone (pp K x) (cc K x) := by
  have ⟨n2, n3, _, _⟩ := kn K O (RatModel.nn_one' K O.toCone)
  refine ⟨K.add (K.add x x) (K.add (K.mul (k3 K) (K.mul x x)) (K.mul (k2 K) (K.mul (K.mul x x) x))),
    O.nn_add _ _ (O.nn_add _ _ hx hx) (O.nn_add _ _ (O.nn_mul _ _ n3 (O.nn_sq x)) (O.nn_mul _ _ n2 (O.nn_mul _ _ (O.nn_sq x) hx))), ?_⟩
  unfold pp cc k3 k2; lib_ring

theorem one_le_c (x : R) (hx : O.nn x) : Le K O.toCone K.one (cc K x) := by
  have ⟨n2, _, n4, n5⟩ := kn K O (RatModel.nn_one' K O.toCone)
  refine ⟨K.add (K.mul (k4 K) x) (K.add (K.mul (k5 K) (K.mul x x)) (K.mul (k2 K) (K.mul (K.mul x x) x))),
    O.nn_add _ _ (O.nn_mul _ _ n4 hx) (O.nn_add _ _ (O.nn_mul _ _ n5 (O.nn_sq x)) (O.nn_mul _ _ n2 (O.nn_mul _ _ (O.nn_sq x) hx))), ?_⟩
  unfold cc k5 k4 k2; lib_ring

theorem x_le_csq (x : R) (hx : O.nn x) : Le K O.toCone x (K.mul (cc K x) (cc K x)) := by
  have h1 := one_le_c K O x hx
  have hcn : O.nn (cc K x) := TruncRate.nn_of_le K O _ _ (RatModel.nn_one' K O.toCone) h1
  have m := le_mul_nn K O.toCone (cc K x) _ _ hcn h1
  refine le_trans' K O.toCone _ _ _ (c_ge_x K O x hx) (le_trans' K O.toCone _ _ _ ⟨K.zero, O.nn_zero, ?_⟩ m)
  show K.add (cc K x) K.zero = K.mul (cc K x) K.one
  lib_ring

theorem split3 (A B C : R) :
    Le K O.toCone (K.mul (K.add (K.add A B) C) (K.add (K.add A B) C))
      (K.mul (k3 K) (K.add (K.add (K.mul A A) (K.mul B B)) (K.mul C C))) :=
  ⟨K.add (K.add (K.mul (K.add A (K.neg B)) (K.add A (K.neg B))) (K.mul (K.add B (K.neg C)) (K.add B (K.neg C))))
      (K.mul (K.add A (K.neg C)) (K.add A (K.neg C))),
    O.nn_add _ _ (O.nn_add _ _ (O.nn_sq _) (O.nn_sq _)) (O.nn_sq _),
    by unfold k3 k2; lib_ring⟩

theorem defect_split (x k a0 M N1 D : R) (hx : O.nn x) (h10 : K.one ≠ K.zero)
    (h : K.mul (cc K x) D = K.add (K.add (K.mul (K.mul x x) a0) (K.mul (K.mul k (pp K x)) M)) (K.neg (K.mul (K.mul k x) N1))) :
    Le K O.toCone (K.mul D D)
      (K.mul (k3 K) (K.add (K.add (K.mul (K.mul x x) (K.mul a0 a0)) (K.mul (K.mul k k) (K.mul M M)))
        (K.mul (K.mul k k) (K.mul x (K.mul N1 N1))))) := by
  have ⟨_, n3, _, _⟩ := kn K O (RatModel.nn_one' K O.toCone)
  have hc1 := one_le_c K O x hx
  have hcn : O.nn (cc K x) := TruncRate.nn_of_le K O _ _ (RatModel.nn_one' K O.toCone) hc1
  have hpn : O.nn (pp K x) := O.nn_add _ _ (O.nn_add _ _ (RatModel.nn_one' K O.toCone) (O.nn_add _ _ hx hx)) (O.nn_add _ _ (O.nn_sq x) (O.nn_sq x))
  have bA := ForceSob.le_mul_right' K O.toCone (EnstLocal.sq_mono K O _ _ hx hcn (c_ge_x K O x hx))
    (O.nn_mul _ _ (O.nn_sq x) (O.nn_sq a0))
  have bB := ForceSob.le_mul_right' K O.toCone (EnstLocal.sq_mono K O _ _ hpn hcn (p_le_c K O x hx))
    (O.nn_mul _ _ (O.nn_sq k) (O.nn_sq M))
  have bC := ForceSob.le_mul_right' K O.toCone (le_mul_nn K O.toCone x _ _ hx (x_le_csq K O x hx))
    (O.nn_mul _ _ (O.nn_sq k) (O.nn_sq N1))
  have S3 := le_mul_nn K O.toCone (k3 K) _ _ n3 (le_add' K O.toCone _ _ _ _ (le_add' K O.toCone _ _ _ _ bA bB) bC)
  have sp := split3 K O (K.mul (K.mul x x) a0) (K.mul (K.mul k (pp K x)) M) (K.neg (K.mul (K.mul k x) N1))
  have e1 : K.mul (K.mul (cc K x) (cc K x)) (K.mul D D)
      = K.mul (K.add (K.add (K.mul (K.mul x x) a0) (K.mul (K.mul k (pp K x)) M)) (K.neg (K.mul (K.mul k x) N1)))
          (K.add (K.add (K.mul (K.mul x x) a0) (K.mul (K.mul k (pp K x)) M)) (K.neg (K.mul (K.mul k x) N1))) := by
    rw [← h]; lib_ring
  have T1 : Le K O.toCone (K.mul (K.mul (cc K x) (cc K x)) (K.mul D D))
      (K.mul (k3 K) (K.add (K.add (K.mul (K.mul x x) (K.mul (K.mul x x) (K.mul a0 a0)))
        (K.mul (K.mul (pp K x) (pp K x)) (K.mul (K.mul k k) (K.mul M M))))
        (K.mul (K.mul x x) (K.mul (K.mul k k) (K.mul N1 N1))))) := by
    rw [e1]
    refine le_trans' K O.toCone _ _ _ sp ⟨K.zero, O.nn_zero, ?_⟩
    lib_ring
  have T2 := le_trans' K O.toCone _ _ _ T1 S3
  have T : Le K O.toCone (K.mul (K.mul (cc K x) (cc K x)) (K.mul D D))
      (K.mul (K.mul (cc K x) (cc K x)) (K.mul (k3 K) (K.add (K.add (K.mul (K.mul x x) (K.mul a0 a0)) (K.mul (K.mul k k) (K.mul M M)))
        (K.mul (K.mul k k) (K.mul x (K.mul N1 N1)))))) := by
    refine le_trans' K O.toCone _ _ _ T2 ⟨K.zero, O.nn_zero, ?_⟩
    lib_ring
  have ccne : K.mul (cc K x) (cc K x) ≠ K.zero := by
    intro z
    cases O.noZD _ _ z with
    | inl h' => exact cc_ne K O x hx h10 h'
    | inr h' => exact cc_ne K O x hx h10 h'
  exact le_of_mul_le K O _ _ _ (O.nn_sq _) ccne T

theorem half_split (k ν w a0 a1 b1 b2 N0 N1 : R) (hk : O.nn k) (hν : O.nn ν) (hw : O.nn w) (h10 : K.one ≠ K.zero)
    (h1 : K.mul (K.add K.one (K.mul (K.mul (K.add k k) ν) w)) a1 = K.add a0 (K.mul (K.add k k) N0))
    (h2 : K.mul (K.add K.one (K.mul (K.mul k ν) w)) b1 = K.add a0 (K.mul k N0))
    (h3 : K.mul (K.add K.one (K.mul (K.mul k ν) w)) b2 = K.add b1 (K.mul k N1)) :
    Le K O.toCone (K.mul (K.add a1 (K.neg b2)) (K.add a1 (K.neg b2)))
      (K.mul (k3 K) (K.add (K.add (K.mul (K.mul (K.mul (K.mul k ν) w) (K.mul (K.mul k ν) w)) (K.mul a0 a0))
          (K.mul (K.mul k k) (K.mul (K.add N0 (K.neg N1)) (K.add N0 (K.neg N1)))))
        (K.mul (K.mul k k) (K.mul (K.mul (K.mul k ν) w) (K.mul N1 N1))))) := by
  have hx : O.nn (K.mul (K.mul k ν) w) := O.nn_mul _ _ (O.nn_mul _ _ hk hν) hw
  have e := StepHalf.half_defect K k ν w a0 a1 b1 b2 N0 N1 h1 h2 h3
  refine defect_split K O _ k a0 _ N1 _ hx h10 ?_
  have e2 : K.mul (K.mul (K.add k k) ν) w = K.add (K.mul (K.mul k ν) w) (K.mul (K.mul k ν) w) := by lib_ring
  rw [e2] at e
  generalize K.mul (K.mul k ν) w = x at e ⊢
  unfold cc pp
  calc _ = K.mul (K.mul (K.add K.one (K.add x x)) (K.mul (K.add K.one x) (K.add K.one x))) (K.add a1 (K.neg b2)) := by lib_ring
    _ = _ := e
    _ = _ := by lib_ring

theorem defect_split4 (x k a0 M N1 D : R) (hx : O.nn x) (h10 : K.one ≠ K.zero)
    (h : K.mul (cc K x) D = K.add (K.add (K.mul (K.mul x x) a0) (K.mul (K.mul k (pp K x)) M)) (K.neg (K.mul (K.mul k x) N1))) :
    Le K O.toCone (K.mul D D)
      (K.mul (k3 K) (K.add (K.add (K.mul (K.mul x x) (K.mul (K.mul x x) (K.mul a0 a0))) (K.mul (K.mul k k) (K.mul M M)))
        (K.mul (K.mul k k) (K.mul x (K.mul N1 N1))))) := by
  have ⟨_, n3, _, _⟩ := kn K O (RatModel.nn_one' K O.toCone)
  have hc1 := one_le_c K O x hx
  have hcn : O.nn (cc K x) := TruncRate.nn_of_le K O _ _ (RatModel.nn_one' K O.toCone) hc1
  have hpn : O.nn (pp K x) := O.nn_add _ _ (O.nn_add _ _ (RatModel.nn_one' K O.toCone) (O.nn_add _ _ hx hx)) (O.nn_add _ _ (O.nn_sq x) (O.nn_sq x))
  have bA := ForceSob.le_mul_right' K O.toCone (EnstLocal.sq_mono K O _ _ (RatModel.nn_one' K O.toCone) hcn hc1)
    (O.nn_mul _ _ (O.nn_sq x) (O.nn_mul _ _ (O.nn_sq x) (O.nn_sq a0)))
  have bB := ForceSob.le_mul_right' K O.toCone (EnstLocal.sq_mono K O _ _ hpn hcn (p_le_c K O x hx))
    (O.nn_mul _ _ (O.nn_sq k) (O.nn_sq M))
  have bC := ForceSob.le_mul_right' K O.toCone (le_mul_nn K O.toCone x _ _ hx (x_le_csq K O x hx))
    (O.nn_mul _ _ (O.nn_sq k) (O.nn_sq N1))
  have S3 := le_mul_nn K O.toCone (k3 K) _ _ n3 (le_add' K O.toCone _ _ _ _ (le_add' K O.toCone _ _ _ _ bA bB) bC)
  have sp := split3 K O (K.mul (K.mul x x) a0) (K.mul (K.mul k (pp K x)) M) (K.neg (K.mul (K.mul k x) N1))
  have e1 : K.mul (K.mul (cc K x) (cc K x)) (K.mul D D)
      = K.mul (K.add (K.add (K.mul (K.mul x x) a0) (K.mul (K.mul k (pp K x)) M)) (K.neg (K.mul (K.mul k x) N1)))
          (K.add (K.add (K.mul (K.mul x x) a0) (K.mul (K.mul k (pp K x)) M)) (K.neg (K.mul (K.mul k x) N1))) := by
    rw [← h]; lib_ring
  have T1 : Le K O.toCone (K.mul (K.mul (cc K x) (cc K x)) (K.mul D D))
      (K.mul (k3 K) (K.add (K.add (K.mul (K.mul K.one K.one) (K.mul (K.mul x x) (K.mul (K.mul x x) (K.mul a0 a0))))
        (K.mul (K.mul (pp K x) (pp K x)) (K.mul (K.mul k k) (K.mul M M))))
        (K.mul (K.mul x x) (K.mul (K.mul k k) (K.mul N1 N1))))) := by
    rw [e1]
    refine le_trans' K O.toCone _ _ _ sp ⟨K.zero, O.nn_zero, ?_⟩
    lib_ring
  have T2 := le_trans' K O.toCone _ _ _ T1 S3
  have T : Le K O.toCone (K.mul (K.mul (cc K x) (cc K x)) (K.mul D D))
      (K.mul (K.mul (cc K x) (cc K x)) (K.mul (k3 K) (K.add (K.add (K.mul (K.mul x x) (K.mul (K.mul x x) (K.mul a0 a0))) (K.mul (K.mul k k) (K.mul M M)))
        (K.mul (K.mul k k) (K.mul x (K.mul N1 N1)))))) := by
    refine le_trans' K O.toCone _ _ _ T2 ⟨K.zero, O.nn_zero, ?_⟩
    lib_ring
  have ccne : K.mul (cc K x) (cc K x) ≠ K.zero := by
    intro z
    cases O.noZD _ _ z with
    | inl h' => exact cc_ne K O x hx h10 h'
    | inr h' => exact cc_ne K O x hx h10 h'
  exact le_of_mul_le K O _ _ _ (O.nn_sq _) ccne T

theorem half_split4 (k ν w a0 a1 b1 b2 N0 N1 : R) (hk : O.nn k) (hν : O.nn ν) (hw : O.nn w) (h10 : K.one ≠ K.zero)
    (h1 : K.mul (K.add K.one (K.mul (K.mul (K.add k k) ν) w)) a1 = K.add a0 (K.mul (K.add k k) N0))
    (h2 : K.mul (K.add K.one (K.mul (K.mul k ν) w)) b1 = K.add a0 (K.mul k N0))
    (h3 : K.mul (K.add K.one (K.mul (K.mul k ν) w)) b2 = K.add b1 (K.mul k N1)) :
    Le K O.toCone (K.mul (K.add a1 (K.neg b2)) (K.add a1 (K.neg b2)))
      (K.mul (k3 K) (K.add (K.add (K.mul (K.mul (K.mul (K.mul k ν) w) (K.mul (K.mul k ν) w)) (K.mul (K.mul (K.mul (K.mul k ν) w) (K.mul (K.mul k ν) w)) (K.mul a0 a0)))
          (K.mul (K.mul k k) (K.mul (K.add N0 (K.neg N1)) (K.add N0 (K.neg N1)))))
        (K.mul (K.mul k k) (K.mul (K.mul (K.mul k ν) w) (K.mul N1 N1))))) := by
  have hx : O.nn (K.mul (K.mul k ν) w) := O.nn_mul _ _ (O.nn_mul _ _ hk hν) hw
  have e := StepHalf.half_defect K k ν w a0 a1 b1 b2 N0 N1 h1 h2 h3
  refine defect_split4 K O _ k a0 _ N1 _ hx h10 ?_
  have e2 : K.mul (K.mul (K.add k k) ν) w = K.add (K.mul (K.mul k ν) w) (K.mul (K.mul k ν) w) := by lib_ring
  rw [e2] at e
  generalize K.mul (K.mul k ν) w = x at e ⊢
  unfold cc pp
  calc _ = K.mul (K.mul (K.add K.one (K.add x x)) (K.mul (K.add K.one x) (K.add K.one x))) (K.add a1 (K.neg b2)) := by lib_ring
    _ = _ := e
    _ = _ := by lib_ring

end Gen

end DefectSplit

#print axioms DefectSplit.p_le_c
#print axioms DefectSplit.one_le_c
#print axioms DefectSplit.x_le_csq
#print axioms DefectSplit.split3
#print axioms DefectSplit.defect_split
#print axioms DefectSplit.half_split
#print axioms DefectSplit.defect_split4
#print axioms DefectSplit.half_split4

ns/DefectSum.lean

lean_all/ns/DefectSum.lean

/-
  ns/DefectSum.lean — T37 cycle 1097 (goal G717, piece b2): the weighted one-step/two-step defect, summed over modes.
  For the implicit-viscosity scheme with one step of size 2k (a₀ → a₁, force N₀) against two steps of size k
  (a₀ → b₁ → b₂, forces N₀, N₁), over an ordered ring without zero divisors (k, ν ≥ 0, w_r ≥ 0):
    mode_half    |a₁ − b₂|² ≤ 3(x²|a₀|² + k²|N₀ − N₁|² + k²x|N₁|²),   x = kνw
    defect_sum   Σ_r w_r|a₁,r − b₂,r|² ≤ 3(k²ν²·Σ w³|a₀|² + k²·Σ w|N₀ − N₁|² + k³ν·Σ w²|N₁|²).
    defect_sumV  the weak form with a general weight V_r (V = w² gives the A-part of the defect)
    defect_sum4  the same with k⁴ν⁴·Σ w⁵|a₀|² as the first term (sharp viscous part).
  The first sum is B (EnstH2), the last is bounded by ForceH3 with the H³ energy of EnstH3.

  Lean 4 core plus ns/DefectSplit, ns/EnstDiff (sub3), ns/ForceBound (lsum_le).
-/
import ns.DefectSplit
import ns.EnstDiff

namespace DefectSum
open DetChain NSChain DyadicSmallGlobal LibRing DyadicLarge FourierGalerkin ForceBound EnstStep EnstDiff DefectA0 DefectSplit

section Gen
variable {R : Type} (K : CRing R) (O : OrdR K) {I : Type}

theorem mode_half (k ν w : R) (hk : O.nn k) (hν : O.nn ν) (hw : O.nn w) (h10 : K.one ≠ K.zero)
    (a0 a1 b1 b2 N0 N1 : V3 R) (s1 : Step K (K.add k k) ν w a1 a0 N0) (s2 : Step K k ν w b1 a0 N0)
    (s3 : Step K k ν w b2 b1 N1) :
    Le K O.toCone (dot3 K (sub3 K a1 b2) (sub3 K a1 b2))
      (K.mul (k3 K) (K.add (K.add (K.mul (K.mul (K.mul (K.mul k ν) w) (K.mul (K.mul k ν) w)) (dot3 K a0 a0))
          (K.mul (K.mul k k) (dot3 K (sub3 K N0 N1) (sub3 K N0 N1))))
        (K.mul (K.mul k k) (K.mul (K.mul (K.mul k ν) w) (dot3 K N1 N1))))) := by
  have cx := half_split K O k ν w a0.x a1.x b1.x b2.x N0.x N1.x hk hν hw h10 s1.1 s2.1 s3.1
  have cy := half_split K O k ν w a0.y a1.y b1.y b2.y N0.y N1.y hk hν hw h10 s1.2.1 s2.2.1 s3.2.1
  have cz := half_split K O k ν w a0.z a1.z b1.z b2.z N0.z N1.z hk hν hw h10 s1.2.2 s2.2.2 s3.2.2
  have s := le_add' K O.toCone _ _ _ _ (le_add' K O.toCone _ _ _ _ cx cy) cz
  refine le_trans' K O.toCone _ _ _ ⟨K.zero, O.nn_zero, ?_⟩ (le_trans' K O.toCone _ _ _ s ⟨K.zero, O.nn_zero, ?_⟩)
  · show K.add (K.add (K.add (K.mul (K.add a1.x (K.neg b2.x)) (K.add a1.x (K.neg b2.x)))
        (K.mul (K.add a1.y (K.neg b2.y)) (K.add a1.y (K.neg b2.y)))) (K.mul (K.add a1.z (K.neg b2.z)) (K.add a1.z (K.neg b2.z)))) K.zero = _
    lib_ring
  · show _ = K.mul (k3 K) (K.add (K.add (K.mul (K.mul (K.mul (K.mul k ν) w) (K.mul (K.mul k ν) w))
          (K.add (K.add (K.mul a0.x a0.x) (K.mul a0.y a0.y)) (K.mul a0.z a0.z)))
        (K.mul (K.mul k k) (K.add (K.add (K.mul (K.add N0.x (K.neg N1.x)) (K.add N0.x (K.neg N1.x)))
          (K.mul (K.add N0.y (K.neg N1.y)) (K.add N0.y (K.neg N1.y)))) (K.mul (K.add N0.z (K.neg N1.z)) (K.add N0.z (K.neg N1.z))))))
      (K.mul (K.mul k k) (K.mul (K.mul (K.mul k ν) w) (K.add (K.add (K.mul N1.x N1.x) (K.mul N1.y N1.y)) (K.mul N1.z N1.z)))))
    generalize k3 K = c
    lib_ring

theorem defect_sum (l : List I) (k ν : R) (hk : O.nn k) (hν : O.nn ν) (w : I → R) (hw : ∀ r, O.nn (w r))
    (h10 : K.one ≠ K.zero) (a0 a1 b1 b2 N0 N1 : I → V3 R)
    (s1 : ∀ r, Step K (K.add k k) ν (w r) (a1 r) (a0 r) (N0 r)) (s2 : ∀ r, Step K k ν (w r) (b1 r) (a0 r) (N0 r))
    (s3 : ∀ r, Step K k ν (w r) (b2 r) (b1 r) (N1 r)) :
    Le K O.toCone (lsum K (fun r => K.mul (w r) (dot3 K (sub3 K (a1 r) (b2 r)) (sub3 K (a1 r) (b2 r)))) l)
      (K.mul (k3 K) (K.add (K.add
        (K.mul (K.mul (K.mul k k) (K.mul ν ν)) (lsum K (fun r => K.mul (K.mul (K.mul (w r) (w r)) (w r)) (dot3 K (a0 r) (a0 r))) l))
        (K.mul (K.mul k k) (lsum K (fun r => K.mul (w r) (dot3 K (sub3 K (N0 r) (N1 r)) (sub3 K (N0 r) (N1 r)))) l)))
        (K.mul (K.mul (K.mul k k) (K.mul k ν)) (lsum K (fun r => K.mul (K.mul (w r) (w r)) (dot3 K (N1 r) (N1 r))) l)))) := by
  have t := lsum_le K O.toCone _ _ (fun r => le_mul_nn K O.toCone (w r) _ _ (hw r)
    (mode_half K O k ν (w r) hk hν (hw r) h10 (a0 r) (a1 r) (b1 r) (b2 r) (N0 r) (N1 r) (s1 r) (s2 r) (s3 r))) l
  refine le_trans' K O.toCone _ _ _ t ⟨K.zero, O.nn_zero, ?_⟩
  rw [add_zero', ← lsum_mul_left, ← lsum_mul_left, ← lsum_mul_left, ← lsum_add, ← lsum_add, ← lsum_mul_left]
  refine lsum_ext K _ _ (fun r => ?_) l
  generalize k3 K = c
  lib_ring

theorem mode_half4 (k ν w : R) (hk : O.nn k) (hν : O.nn ν) (hw : O.nn w) (h10 : K.one ≠ K.zero)
    (a0 a1 b1 b2 N0 N1 : V3 R) (s1 : Step K (K.add k k) ν w a1 a0 N0) (s2 : Step K k ν w b1 a0 N0)
    (s3 : Step K k ν w b2 b1 N1) :
    Le K O.toCone (dot3 K (sub3 K a1 b2) (sub3 K a1 b2))
      (K.mul (k3 K) (K.add (K.add (K.mul (K.mul (K.mul (K.mul k ν) w) (K.mul (K.mul k ν) w)) (K.mul (K.mul (K.mul (K.mul k ν) w) (K.mul (K.mul k ν) w)) (dot3 K a0 a0)))
          (K.mul (K.mul k k) (dot3 K (sub3 K N0 N1) (sub3 K N0 N1))))
        (K.mul (K.mul k k) (K.mul (K.mul (K.mul k ν) w) (dot3 K N1 N1))))) := by
  have cx := half_split4 K O k ν w a0.x a1.x b1.x b2.x N0.x N1.x hk hν hw h10 s1.1 s2.1 s3.1
  have cy := half_split4 K O k ν w a0.y a1.y b1.y b2.y N0.y N1.y hk hν hw h10 s1.2.1 s2.2.1 s3.2.1
  have cz := half_split4 K O k ν w a0.z a1.z b1.z b2.z N0.z N1.z hk hν hw h10 s1.2.2 s2.2.2 s3.2.2
  have s := le_add' K O.toCone _ _ _ _ (le_add' K O.toCone _ _ _ _ cx cy) cz
  refine le_trans' K O.toCone _ _ _ ⟨K.zero, O.nn_zero, ?_⟩ (le_trans' K O.toCone _ _ _ s ⟨K.zero, O.nn_zero, ?_⟩)
  · show K.add (K.add (K.add (K.mul (K.add a1.x (K.neg b2.x)) (K.add a1.x (K.neg b2.x)))
        (K.mul (K.add a1.y (K.neg b2.y)) (K.add a1.y (K.neg b2.y)))) (K.mul (K.add a1.z (K.neg b2.z)) (K.add a1.z (K.neg b2.z)))) K.zero = _
    lib_ring
  · show _ = K.mul (k3 K) (K.add (K.add (K.mul (K.mul (K.mul (K.mul k ν) w) (K.mul (K.mul k ν) w)) (K.mul (K.mul (K.mul (K.mul k ν) w) (K.mul (K.mul k ν) w))
          (K.add (K.add (K.mul a0.x a0.x) (K.mul a0.y a0.y)) (K.mul a0.z a0.z))))
        (K.mul (K.mul k k) (K.add (K.add (K.mul (K.add N0.x (K.neg N1.x)) (K.add N0.x (K.neg N1.x)))
          (K.mul (K.add N0.y (K.neg N1.y)) (K.add N0.y (K.neg N1.y)))) (K.mul (K.add N0.z (K.neg N1.z)) (K.add N0.z (K.neg N1.z))))))
      (K.mul (K.mul k k) (K.mul (K.mul (K.mul k ν) w) (K.add (K.add (K.mul N1.x N1.x) (K.mul N1.y N1.y)) (K.mul N1.z N1.z)))))
    generalize k3 K = c
    lib_ring

theorem defect_sum4 (l : List I) (k ν : R) (hk : O.nn k) (hν : O.nn ν) (w : I → R) (hw : ∀ r, O.nn (w r))
    (h10 : K.one ≠ K.zero) (a0 a1 b1 b2 N0 N1 : I → V3 R)
    (s1 : ∀ r, Step K (K.add k k) ν (w r) (a1 r) (a0 r) (N0 r)) (s2 : ∀ r, Step K k ν (w r) (b1 r) (a0 r) (N0 r))
    (s3 : ∀ r, Step K k ν (w r) (b2 r) (b1 r) (N1 r)) :
    Le K O.toCone (lsum K (fun r => K.mul (w r) (dot3 K (sub3 K (a1 r) (b2 r)) (sub3 K (a1 r) (b2 r)))) l)
      (K.mul (k3 K) (K.add (K.add
        (K.mul (K.mul (K.mul (K.mul k k) (K.mul k k)) (K.mul (K.mul ν ν) (K.mul ν ν))) (lsum K (fun r => K.mul (K.mul (K.mul (K.mul (w r) (w r)) (K.mul (w r) (w r))) (w r)) (dot3 K (a0 r) (a0 r))) l))
        (K.mul (K.mul k k) (lsum K (fun r => K.mul (w r) (dot3 K (sub3 K (N0 r) (N1 r)) (sub3 K (N0 r) (N1 r)))) l)))
        (K.mul (K.mul (K.mul k k) (K.mul k ν)) (lsum K (fun r => K.mul (K.mul (w r) (w r)) (dot3 K (N1 r) (N1 r))) l)))) := by
  have t := lsum_le K O.toCone _ _ (fun r => le_mul_nn K O.toCone (w r) _ _ (hw r)
    (mode_half4 K O k ν (w r) hk hν (hw r) h10 (a0 r) (a1 r) (b1 r) (b2 r) (N0 r) (N1 r) (s1 r) (s2 r) (s3 r))) l
  refine le_trans' K O.toCone _ _ _ t ⟨K.zero, O.nn_zero, ?_⟩
  rw [add_zero', ← lsum_mul_left, ← lsum_mul_left, ← lsum_mul_left, ← lsum_add, ← lsum_add, ← lsum_mul_left]
  refine lsum_ext K _ _ (fun r => ?_) l
  generalize k3 K = c
  lib_ring

theorem defect_sumV (l : List I) (k ν : R) (hk : O.nn k) (hν : O.nn ν) (w V : I → R) (hw : ∀ r, O.nn (w r))
    (hV : ∀ r, O.nn (V r)) (h10 : K.one ≠ K.zero) (a0 a1 b1 b2 N0 N1 : I → V3 R)
    (s1 : ∀ r, Step K (K.add k k) ν (w r) (a1 r) (a0 r) (N0 r)) (s2 : ∀ r, Step K k ν (w r) (b1 r) (a0 r) (N0 r))
    (s3 : ∀ r, Step K k ν (w r) (b2 r) (b1 r) (N1 r)) :
    Le K O.toCone (lsum K (fun r => K.mul (V r) (dot3 K (sub3 K (a1 r) (b2 r)) (sub3 K (a1 r) (b2 r)))) l)
      (K.mul (k3 K) (K.add (K.add
        (K.mul (K.mul (K.mul k k) (K.mul ν ν)) (lsum K (fun r => K.mul (K.mul (V r) (K.mul (w r) (w r))) (dot3 K (a0 r) (a0 r))) l))
        (K.mul (K.mul k k) (lsum K (fun r => K.mul (V r) (dot3 K (sub3 K (N0 r) (N1 r)) (sub3 K (N0 r) (N1 r)))) l)))
        (K.mul (K.mul (K.mul k k) (K.mul k ν)) (lsum K (fun r => K.mul (K.mul (V r) (w r)) (dot3 K (N1 r) (N1 r))) l)))) := by
  have t := lsum_le K O.toCone _ _ (fun r => le_mul_nn K O.toCone (V r) _ _ (hV r)
    (mode_half K O k ν (w r) hk hν (hw r) h10 (a0 r) (a1 r) (b1 r) (b2 r) (N0 r) (N1 r) (s1 r) (s2 r) (s3 r))) l
  refine le_trans' K O.toCone _ _ _ t ⟨K.zero, O.nn_zero, ?_⟩
  rw [add_zero', ← lsum_mul_left, ← lsum_mul_left, ← lsum_mul_left, ← lsum_add, ← lsum_add, ← lsum_mul_left]
  refine lsum_ext K _ _ (fun r => ?_) l
  generalize k3 K = c
  lib_ring

end Gen

end DefectSum

#print axioms DefectSum.mode_half
#print axioms DefectSum.defect_sum
#print axioms DefectSum.mode_half4
#print axioms DefectSum.defect_sum4
#print axioms DefectSum.defect_sumV

ns/DiscreteYoung.lean

lean_all/ns/DiscreteYoung.lean

/-
  ns/DiscreteYoung.lean — T37 cycle 109: the discrete Young inequality for triad sums of a truncation.

  Lean 4 core plus ns/SumCS (cs_sum), ns/WeightedJensen (wcs2), ns/TriadSum (lsum_le, lsum_nn),
  ns/Riccati (le_mul_left), lib/RingTac. For the modes l, weights a ≥ 0, and for every p a map σ_p (the
  third mode of the triad (p, q, σ_p q)) whose shifted squares are bounded, Σ_q c(σ_p q)² ≤ C:
      2 (Σ_p a_p Σ_q b_q c(σ_p q))² ≤ 2 (Σ a)² · (Σ b²) · C
  i.e. |Σ_{p+q+r=0} a_p b_q c_r| ≤ ‖a‖₁ ‖b‖₂ ‖c‖₂ (C = ‖c‖₂² when σ_p is injective on the truncation).
-/
import ns.SumCS
import ns.WeightedJensen
import ns.TriadSum

namespace DiscreteYoung
open DetChain NSChain LibRing SumCS TriadSum

variable {R : Type} (K : CRing R) {I : Type}

theorem young (P : Cone K) (a b : I → R) (c : I → R) (σ : I → I → I) (l : List I) (C : R)
    (ha : ∀ p, P.nn (a p)) (hC : ∀ p, Le K P (lsum K (fun q => sq K (c (σ p q))) l) C) :
    Le K P (K.add (sq K (lsum K (fun p => K.mul (a p) (lsum K (fun q => K.mul (b q) (c (σ p q))) l)) l))
                  (sq K (lsum K (fun p => K.mul (a p) (lsum K (fun q => K.mul (b q) (c (σ p q))) l)) l)))
      (K.add (K.mul (K.mul (lsum K a l) (lsum K a l)) (K.mul (lsum K (fun q => sq K (b q)) l) C))
             (K.mul (K.mul (lsum K a l) (lsum K a l)) (K.mul (lsum K (fun q => sq K (b q)) l) C))) := by
  let X : I → R := fun p => lsum K (fun q => K.mul (b q) (c (σ p q))) l
  -- inner: X_p² ≤ (Σb²) C
  have bn : P.nn (lsum K (fun q => sq K (b q)) l) := lsum_nn K P _ (fun q => P.nn_sq _) l
  have inner : ∀ p, Le K P (K.mul (X p) (X p)) (K.mul (lsum K (fun q => sq K (b q)) l) C) := fun p =>
    le_trans' K P _ _ _ (cs_sum K P b (fun q => c (σ p q)) l) (Riccati.le_mul_left K P _ _ _ bn (hC p))
  -- outer: 2(ΣaX)² ≤ 2(Σa)(ΣaX²) ≤ 2(Σa)(Σa)(Σb²)C
  have w := WeightedJensen.wcs2 K P a X l ha
  have s2 : Le K P (lsum K (fun p => K.mul (a p) (K.mul (X p) (X p))) l)
      (K.mul (lsum K a l) (K.mul (lsum K (fun q => sq K (b q)) l) C)) := by
    refine le_trans' K P _ _ _ (lsum_le K P _ (fun p => K.mul (a p) (K.mul (lsum K (fun q => sq K (b q)) l) C))
      (fun p => Riccati.le_mul_left K P _ _ _ (ha p) (inner p)) l) ⟨K.zero, P.nn_zero, ?_⟩
    rw [show K.add (lsum K (fun p => K.mul (a p) (K.mul (lsum K (fun q => sq K (b q)) l) C)) l) K.zero
        = lsum K (fun p => K.mul (a p) (K.mul (lsum K (fun q => sq K (b q)) l) C)) l by lib_ring]
    generalize K.mul (lsum K (fun q => sq K (b q)) l) C = BC
    rw [lsum_ext K _ (fun p => K.mul BC (a p)) (fun p => K.mul_comm _ _) l, lsum_mul_left, K.mul_comm]
  have an : P.nn (lsum K a l) := lsum_nn K P a ha l
  have m := Riccati.le_mul_left K P _ _ _ an s2
  refine le_trans' K P _ _ _ w (Riccati.le_add K P _ _ _ _ ?_ ?_) <;>
  · refine le_trans' K P _ _ _ m ⟨K.zero, P.nn_zero, by lib_ring⟩

end DiscreteYoung

#print axioms DiscreteYoung.young

ns/DissBudget.lean

lean_all/ns/DissBudget.lean

/-
  ns/DissBudget.lean — T37 cycle 740 (goal G453): the exact dissipation budget of a Crank–Nicolson Galerkin scheme
  over any commutative ring, and its bound over any ordered ring (ℚ included).

  Lean 4 core plus NSChain (cn_energy, dissipation_nonneg), lib/RingTac (lib_ring).
    budget       E(T) + Σ_{t<T} D_t = E(0),  D_t = h ν Σ_n k_n (a_{t+1}(n) + a_t(n))²     (exact)
    budget_le    Σ_{t<T} D_t ≤ E(0)      (only E(T) ≥ 0 is used)
  The total dissipation over all time is bounded by the initial energy: the discrete form of
  ν ∫₀^∞ ‖∇u‖² dt ≤ ½‖u₀‖².
-/
import NSChain
import lib.RingTac

namespace DissBudget
open DetChain NSChain LibRing

section Gen
variable {R : Type} (K : CRing R)

def En (a : Nat → Nat → R) (N t : Nat) : R := sumN K (fun n => K.mul (a t n) (a t n)) N
def D (a : Nat → Nat → R) (k : Nat → R) (h nu : R) (N t : Nat) : R :=
  K.mul h (K.mul nu (sumN K (fun n => K.mul (k n) (K.mul (K.add (a (t + 1) n) (a t n)) (K.add (a (t + 1) n) (a t n)))) N))

theorem budget (a : Nat → Nat → R) (k : Nat → R) (Nn : Nat → Nat → R) (h nu : R) (N : Nat)
    (step : ∀ t n, K.add (a (t + 1) n) (K.neg (a t n))
      = K.mul h (K.add (K.neg (K.mul nu (K.mul (k n) (K.add (a (t + 1) n) (a t n))))) (Nn t n)))
    (neutral : ∀ t, sumN K (fun n => K.mul (K.add (a (t + 1) n) (a t n)) (Nn t n)) N = K.zero) :
    ∀ T, K.add (En K a N T) (sumN K (D K a k h nu N) T) = En K a N 0
  | 0 => by show K.add (En K a N 0) K.zero = En K a N 0; lib_ring
  | T + 1 => by
    have e := cn_energy K (a T) (a (T + 1)) k (Nn T) h nu N (step T) (neutral T)
    have ih := budget a k Nn h nu N step neutral T
    show K.add (En K a N (T + 1)) (K.add (sumN K (D K a k h nu N) T) (D K a k h nu N T)) = En K a N 0
    have r : K.add (En K a N (T + 1)) (K.add (sumN K (D K a k h nu N) T) (D K a k h nu N T))
        = K.add (K.add (En K a N (T + 1)) (D K a k h nu N T)) (sumN K (D K a k h nu N) T) := by lib_ring
    rw [r]
    have e' : K.add (En K a N (T + 1)) (D K a k h nu N T) = En K a N T := e
    rw [e', ih]

theorem budget_le (P : Cone K) (a : Nat → Nat → R) (k : Nat → R) (Nn : Nat → Nat → R) (h nu : R) (N : Nat)
    (step : ∀ t n, K.add (a (t + 1) n) (K.neg (a t n))
      = K.mul h (K.add (K.neg (K.mul nu (K.mul (k n) (K.add (a (t + 1) n) (a t n))))) (Nn t n)))
    (neutral : ∀ t, sumN K (fun n => K.mul (K.add (a (t + 1) n) (a t n)) (Nn t n)) N = K.zero) (T : Nat) :
    Le K P (sumN K (D K a k h nu N) T) (En K a N 0) :=
  ⟨En K a N T, sq_sum_nonneg K P (a T) N, by
    rw [K.add_comm]; exact budget K a k Nn h nu N step neutral T⟩

end Gen

end DissBudget

#print axioms DissBudget.budget
#print axioms DissBudget.budget_le

ns/DivFree.lean

lean_all/ns/DivFree.lean

/-
  ns/DivFree.lean — T37 cycle 1804 (goal G856e): the implicit scheme preserves the divergence-free condition mode by
  mode, over an ordered field. For a mode vector k, weight w ≥ 0, h, ν ≥ 0 and the step (1 + hνw)a' = a + hN
  (EnstStep.Step):
    one_add_ne  1 + d ≠ 0 for d ≥ 0
    div_step    k·a = 0, k·N = 0  ⇒  k·a' = 0
    div_all     k·a_0 = 0 and k·N_t = 0 for all t (Leray-projected force)  ⇒  k·a_t = 0 for all t
  Each approximant satisfies the linear constraint k·a_k = 0 exactly, so every limit of coefficients (TruncCoef,
  DyadicCoef) is divergence-free.
  Lean 4 core plus ns/EnstStep, ns/DyadicSmallGlobal (OrdR, nn_sum_zero).
-/
import ns.EnstStep
import ns.DyadicSmallGlobal

namespace DivFree
open DetChain NSChain DyadicSmallGlobal LibRing FourierGalerkin EnstStep

section Gen
variable {R : Type} (K : CRing R) (O : OrdR K) (h10 : K.one ≠ K.zero)

include h10 in
theorem one_add_ne (d : R) (hd : O.nn d) : K.add K.one d ≠ K.zero := fun e =>
  h10 (nn_sum_zero K O K.one d (RatModel.nn_one' K O.toCone) hd e)

include h10 in
theorem div_step (h ν w : R) (hh : O.nn h) (hν : O.nn ν) (hw : O.nn w) (k a a' N : V3 R)
    (s : Step K h ν w a' a N) (ka : dot3 K k a = K.zero) (kN : dot3 K k N = K.zero) : dot3 K k a' = K.zero := by
  have sx : K.mul (K.add K.one (K.mul (K.mul h ν) w)) a'.x = K.add a.x (K.mul h N.x) := s.1
  have sy : K.mul (K.add K.one (K.mul (K.mul h ν) w)) a'.y = K.add a.y (K.mul h N.y) := s.2.1
  have sz : K.mul (K.add K.one (K.mul (K.mul h ν) w)) a'.z = K.add a.z (K.mul h N.z) := s.2.2
  have ka' : K.add (K.add (K.mul k.x a.x) (K.mul k.y a.y)) (K.mul k.z a.z) = K.zero := ka
  have kN' : K.add (K.add (K.mul k.x N.x) (K.mul k.y N.y)) (K.mul k.z N.z) = K.zero := kN
  have e : K.mul (K.add K.one (K.mul (K.mul h ν) w)) (dot3 K k a') = K.zero := by
    calc K.mul (K.add K.one (K.mul (K.mul h ν) w)) (dot3 K k a')
        = K.add (K.add (K.mul k.x (K.mul (K.add K.one (K.mul (K.mul h ν) w)) a'.x))
            (K.mul k.y (K.mul (K.add K.one (K.mul (K.mul h ν) w)) a'.y)))
            (K.mul k.z (K.mul (K.add K.one (K.mul (K.mul h ν) w)) a'.z)) := by
          show K.mul (K.add K.one (K.mul (K.mul h ν) w)) (K.add (K.add (K.mul k.x a'.x) (K.mul k.y a'.y)) (K.mul k.z a'.z)) = _
          lib_ring
      _ = K.add (K.add (K.mul k.x (K.add a.x (K.mul h N.x))) (K.mul k.y (K.add a.y (K.mul h N.y))))
            (K.mul k.z (K.add a.z (K.mul h N.z))) := by rw [sx, sy, sz]
      _ = K.add (K.add (K.add (K.mul k.x a.x) (K.mul k.y a.y)) (K.mul k.z a.z))
            (K.mul h (K.add (K.add (K.mul k.x N.x) (K.mul k.y N.y)) (K.mul k.z N.z))) := by lib_ring
      _ = K.zero := by rw [ka', kN']; lib_ring
  cases O.noZD _ _ e with
  | inl c => exact absurd c (one_add_ne K O h10 _ (O.nn_mul _ _ (O.nn_mul _ _ hh hν) hw))
  | inr d => exact d

include h10 in
theorem div_all (h ν w : R) (hh : O.nn h) (hν : O.nn ν) (hw : O.nn w) (k : V3 R) (a N : Nat → V3 R)
    (s : ∀ t, Step K h ν w (a (t + 1)) (a t) (N t)) (k0 : dot3 K k (a 0) = K.zero)
    (kN : ∀ t, dot3 K k (N t) = K.zero) : ∀ t, dot3 K k (a t) = K.zero
  | 0 => k0
  | t + 1 => div_step K O h10 h ν w hh hν hw k (a t) (a (t + 1)) (N t) (s t) (div_all h ν w hh hν hw k a N s k0 kN t) (kN t)

end Gen
end DivFree

#print axioms DivFree.one_add_ne
#print axioms DivFree.div_step
#print axioms DivFree.div_all

ns/DyCN.lean

lean_all/ns/DyCN.lean

/-
  ns/DyCN.lean — T37 cycle 800 (goal G512 = G509b): for a fixed truncation N the dyadic nonlinearity is controlled
  by the energy, over any ring with a positive cone (ℚ included): with b_0 = 0, b_{N+1} = 0, E = Σ_{n<N} b_{n+1}²,
    Σ_{n<N} (x_n + 2)·dyN(c, b)_n² ≤ C_N·E²,   C_N = Σ_{n<N} (x_n + 2)·(2c_n² + 2c_{n+1}²).
  This is the hypothesis of SemiGlobal, so the semi-implicit dyadic scheme is energy-stable on horizons
  4T·h²·C_N·E_0 ≤ 1.

  Lean 4 core plus ns/DyNBound (dyN_sq), ns/SemiBound (two), ns/DyUnique (sumN_ext_g), ns/RatModel (nn_one'),
  NSChain (mode_le_energy, sumN_mul_left).
    mode_E      b_i² ≤ E for every i (inside by mode_le_energy, outside 0)
    dy_cn       the statement
-/
import ns.DyNBound
import ns.SemiBound
import ns.DyUnique

namespace DyCN
open DetChain NSChain LibRing DyadicLarge DyNBound

section Gen
variable {R : Type} (K : CRing R) (P : Cone K)

theorem mode_E (b : Nat → R) (N : Nat) (h0 : b 0 = K.zero) (hN : ∀ i, N + 1 ≤ i → b i = K.zero) :
    ∀ i, Le K P (K.mul (b i) (b i)) (sumN K (fun n => K.mul (b (n + 1)) (b (n + 1))) N) := by
  have hE : P.nn (sumN K (fun n => K.mul (b (n + 1)) (b (n + 1))) N) := sq_sum_nonneg K P (fun n => b (n + 1)) N
  have z : ∀ i, b i = K.zero → Le K P (K.mul (b i) (b i)) (sumN K (fun n => K.mul (b (n + 1)) (b (n + 1))) N) := by
    intro i hi; rw [hi, mul_zero K]; exact ⟨_, hE, K.zero_add _⟩
  intro i
  cases i with
  | zero => exact z 0 h0
  | succ j =>
    cases Nat.lt_or_ge j N with
    | inl l => exact mode_le_energy K P (fun n => b (n + 1)) N j l
    | inr g => exact z (j + 1) (hN (j + 1) (Nat.succ_le_succ g))

theorem dy_cn (b c x : Nat → R) (N : Nat) (hx : ∀ n, P.nn (x n)) (h0 : b 0 = K.zero)
    (hN : ∀ i, N + 1 ≤ i → b i = K.zero) :
    Le K P (sumN K (fun n => K.mul (K.add (x n) (K.add K.one K.one)) (K.mul (dyN K c b n) (dyN K c b n))) N)
      (K.mul (sumN K (fun n => K.mul (K.add (x n) (K.add K.one K.one))
          (K.add (SemiBound.two K (K.mul (c n) (c n))) (SemiBound.two K (K.mul (c (n + 1)) (c (n + 1)))))) N)
        (K.mul (sumN K (fun n => K.mul (b (n + 1)) (b (n + 1))) N) (sumN K (fun n => K.mul (b (n + 1)) (b (n + 1))) N))) := by
  generalize hEd : sumN K (fun n => K.mul (b (n + 1)) (b (n + 1))) N = E
  have mE := mode_E K P b N h0 hN
  rw [hEd] at mE
  have hEn : P.nn E := by rw [← hEd]; exact sq_sum_nonneg K P (fun n => b (n + 1)) N
  have pt : ∀ n, Le K P (K.mul (K.add (x n) (K.add K.one K.one)) (K.mul (dyN K c b n) (dyN K c b n)))
      (K.mul (K.mul (K.add (x n) (K.add K.one K.one))
          (K.add (SemiBound.two K (K.mul (c n) (c n))) (SemiBound.two K (K.mul (c (n + 1)) (c (n + 1)))))) (K.mul E E)) := by
    intro n
    have d := dyN_sq K P c b n E E E hEn (mE n) (mE (n + 1)) (mE (n + 2)) hEn
    have d' := le_mul_nn K P (K.add (x n) (K.add K.one K.one)) _ _
      (P.nn_add _ _ (hx n) (P.nn_add _ _ (RatModel.nn_one' K P) (RatModel.nn_one' K P))) d
    apply le_trans' K P _ _ _ d'
    refine ⟨K.zero, P.nn_zero, ?_⟩
    unfold MidRecover.two SemiBound.two; lib_ring
  have sm := sum_le K P _ _ pt N
  have e : sumN K (fun n => K.mul (K.mul (K.add (x n) (K.add K.one K.one))
        (K.add (SemiBound.two K (K.mul (c n) (c n))) (SemiBound.two K (K.mul (c (n + 1)) (c (n + 1)))))) (K.mul E E)) N
      = K.mul (sumN K (fun n => K.mul (K.add (x n) (K.add K.one K.one))
        (K.add (SemiBound.two K (K.mul (c n) (c n))) (SemiBound.two K (K.mul (c (n + 1)) (c (n + 1)))))) N) (K.mul E E) := by
    rw [K.mul_comm (sumN K (fun n => K.mul (K.add (x n) (K.add K.one K.one))
        (K.add (SemiBound.two K (K.mul (c n) (c n))) (SemiBound.two K (K.mul (c (n + 1)) (c (n + 1)))))) N) (K.mul E E),
      ← sumN_mul_left]
    apply DyUnique.sumN_ext_g
    intro n _; lib_ring
  rw [e] at sm; exact sm

end Gen

end DyCN

#print axioms DyCN.mode_E
#print axioms DyCN.dy_cn

ns/DyLip.lean

lean_all/ns/DyLip.lean

/-
  ns/DyLip.lean — T37 cycle 779 (goal G490 = G484b, pointwise part): the dyadic nonlinearity is one-sided Lipschitz
  mode by mode under coefficient bounds, over any ordered ring without zero divisors (ℚ included). With d = s − r,
  X1 = c_n(s_n + r_n), X2 = c_{n+1}s_{n+2}, X3 = c_{n+1}r_{n+1} and X_i² ≤ M²:
    2·d_{n+1}·(dyN(c,s)_n − dyN(c,r)_n) ≤ M(d_n² + d_{n+1}²) + 2M·d_{n+1}² + M(d_{n+1}² + d_{n+2}²).

  Lean 4 core plus ns/YoungM (young_m), ns/DyadicLarge (le_add'), NSChain (dyN), lib/RingTac.
    dy_diff      dyN(c,s)_n − dyN(c,r)_n = X1·d_n − X2·d_{n+1} − X3·d_{n+2}       (exact, d = s − r)
    dy_lip_pt    the inequality above
-/
import ns.YoungM

namespace DyLip
open DetChain NSChain DyadicSmallGlobal LibRing DyadicLarge YoungM

section Gen
variable {R : Type} (K : CRing R)

theorem dy_diff (c s r : Nat → R) (n : Nat) :
    K.add (dyN K c s n) (K.neg (dyN K c r n))
      = K.add (K.add (K.mul (K.mul (c n) (K.add (s n) (r n))) (K.add (s n) (K.neg (r n))))
          (K.neg (K.mul (K.mul (c (n + 1)) (s (n + 2))) (K.add (s (n + 1)) (K.neg (r (n + 1)))))))
        (K.neg (K.mul (K.mul (c (n + 1)) (r (n + 1))) (K.add (s (n + 2)) (K.neg (r (n + 2)))))) := by
  unfold dyN; lib_ring

variable (O : OrdR K)
include O

theorem dy_lip_pt (c s r : Nat → R) (n : Nat) (M : R) (hM : O.nn M) (hM0 : M ≠ K.zero)
    (h1 : Le K O.toCone (K.mul (K.mul (c n) (K.add (s n) (r n))) (K.mul (c n) (K.add (s n) (r n)))) (K.mul M M))
    (h2 : Le K O.toCone (K.mul (K.mul (c (n + 1)) (s (n + 2))) (K.mul (c (n + 1)) (s (n + 2)))) (K.mul M M))
    (h3 : Le K O.toCone (K.mul (K.mul (c (n + 1)) (r (n + 1))) (K.mul (c (n + 1)) (r (n + 1)))) (K.mul M M)) :
    Le K O.toCone
      (K.mul (K.add K.one K.one) (K.mul (K.add (s (n + 1)) (K.neg (r (n + 1))))
        (K.add (dyN K c s n) (K.neg (dyN K c r n)))))
      (K.add (K.add (K.mul M (K.add (K.mul (K.add (s n) (K.neg (r n))) (K.add (s n) (K.neg (r n))))
                                    (K.mul (K.add (s (n + 1)) (K.neg (r (n + 1)))) (K.add (s (n + 1)) (K.neg (r (n + 1)))))))
                     (K.mul M (K.add (K.mul (K.add (s (n + 1)) (K.neg (r (n + 1)))) (K.add (s (n + 1)) (K.neg (r (n + 1)))))
                                     (K.mul (K.add (s (n + 1)) (K.neg (r (n + 1)))) (K.add (s (n + 1)) (K.neg (r (n + 1))))))))
        (K.mul M (K.add (K.mul (K.add (s (n + 1)) (K.neg (r (n + 1)))) (K.add (s (n + 1)) (K.neg (r (n + 1)))))
                        (K.mul (K.add (s (n + 2)) (K.neg (r (n + 2)))) (K.add (s (n + 2)) (K.neg (r (n + 2)))))))) := by
  rw [dy_diff]
  generalize K.add (s n) (K.neg (r n)) = d0
  generalize K.add (s (n + 1)) (K.neg (r (n + 1))) = d1
  generalize K.add (s (n + 2)) (K.neg (r (n + 2))) = d2
  generalize hX1 : K.mul (c n) (K.add (s n) (r n)) = X1 at h1
  generalize hX2 : K.mul (c (n + 1)) (s (n + 2)) = X2 at h2
  generalize hX3 : K.mul (c (n + 1)) (r (n + 1)) = X3 at h3
  have n2 : Le K O.toCone (K.mul (K.neg X2) (K.neg X2)) (K.mul M M) := by
    rw [show K.mul (K.neg X2) (K.neg X2) = K.mul X2 X2 by lib_ring]; exact h2
  have n3 : Le K O.toCone (K.mul (K.neg X3) (K.neg X3)) (K.mul M M) := by
    rw [show K.mul (K.neg X3) (K.neg X3) = K.mul X3 X3 by lib_ring]; exact h3
  have y1 := young_m K O M X1 d0 d1 hM hM0 h1
  have y2 := young_m K O M (K.neg X2) d1 d1 hM hM0 n2
  have y3 := young_m K O M (K.neg X3) d1 d2 hM hM0 n3
  have s := le_add' K O.toCone _ _ _ _ (le_add' K O.toCone _ _ _ _ y1 y2) y3
  have e : K.mul (K.add K.one K.one) (K.mul d1 (K.add (K.add (K.mul X1 d0) (K.neg (K.mul X2 d1))) (K.neg (K.mul X3 d2))))
      = K.add (K.add (K.mul (K.add K.one K.one) (K.mul (K.mul d0 d1) X1))
          (K.mul (K.add K.one K.one) (K.mul (K.mul d1 d1) (K.neg X2))))
        (K.mul (K.add K.one K.one) (K.mul (K.mul d1 d2) (K.neg X3))) := by lib_ring
  rw [e]; exact s

end Gen

end DyLip

#print axioms DyLip.dy_diff
#print axioms DyLip.dy_lip_pt

ns/DyNBound.lean

lean_all/ns/DyNBound.lean

/-
  ns/DyNBound.lean — T37 cycle 756 (goal G469): the dyadic nonlinearity is controlled by mode bounds, over any ring
  with a positive cone (ℚ included): if s_i² ≤ B_i for i = n, n+1, n+2 then
    dyN(c, s)_n² ≤ 2 c_n² B_n² + 2 c_{n+1}² B_{n+1} B_{n+2}.
  With MidRecover and GoodModes this bounds a_{t+1} at a good time by the midpoint mode bounds.

  Lean 4 core plus ns/MidRecover (sq_add_le, le_two_mul), NSChain (dyN), lib/RingTac (lib_ring).
    mul_le_mul'  0 ≤ x ≤ X, 0 ≤ y ≤ Y ⇒ x y ≤ X Y
    dyN_sq       the bound above
-/
import ns.MidRecover

namespace DyNBound
open DetChain NSChain LibRing DyadicLarge MidRecover

section Gen
variable {R : Type} (K : CRing R) (P : Cone K)

theorem mul_le_mul' (x X y Y : R) (hx : P.nn x) (hY : P.nn Y) (h1 : Le K P x X) (h2 : Le K P y Y) :
    Le K P (K.mul x y) (K.mul X Y) := by
  have a1 : Le K P (K.mul x y) (K.mul x Y) := le_two_mul K P x y Y hx h2
  have a2 : Le K P (K.mul x Y) (K.mul X Y) := by
    cases h1 with
    | intro d hd => exact ⟨K.mul d Y, P.nn_mul _ _ hd.1 hY, by rw [← hd.2]; lib_ring⟩
  exact le_trans' K P _ _ _ a1 a2

theorem dyN_sq (c s : Nat → R) (n : Nat) (B0 B1 B2 : R) (hB2 : P.nn B2)
    (h0 : Le K P (K.mul (s n) (s n)) B0) (h1 : Le K P (K.mul (s (n + 1)) (s (n + 1))) B1)
    (h2 : Le K P (K.mul (s (n + 2)) (s (n + 2))) B2) (hB0 : P.nn B0) :
    Le K P (K.mul (dyN K c s n) (dyN K c s n))
      (K.add (two K (K.mul (K.mul (c n) (c n)) (K.mul B0 B0)))
        (two K (K.mul (K.mul (c (n + 1)) (c (n + 1))) (K.mul B1 B2)))) := by
  unfold dyN
  have l := sq_add_le K P (K.mul (c n) (K.mul (s n) (s n))) (K.neg (K.mul (c (n + 1)) (K.mul (s (n + 1)) (s (n + 2)))))
  apply le_trans' K P _ _ _ l
  unfold two
  -- (c s²)² = c² (s²)(s²) ≤ c² B0 B0 ;  (−c' s1 s2)² = c'² (s1²)(s2²) ≤ c'² B1 B2
  have p0 := mul_le_mul' K P _ _ _ _ (P.nn_sq (s n)) hB0 h0 h0
  have p1 := mul_le_mul' K P _ _ _ _ (P.nn_sq (s (n + 1))) hB2 h1 h2
  have q0 := le_two_mul K P (K.mul (c n) (c n)) _ _ (P.nn_sq _) p0
  have q1 := le_two_mul K P (K.mul (c (n + 1)) (c (n + 1))) _ _ (P.nn_sq _) p1
  have e0 : K.mul (K.mul (c n) (K.mul (s n) (s n))) (K.mul (c n) (K.mul (s n) (s n)))
      = K.mul (K.mul (c n) (c n)) (K.mul (K.mul (s n) (s n)) (K.mul (s n) (s n))) := by lib_ring
  have e1 : K.mul (K.neg (K.mul (c (n + 1)) (K.mul (s (n + 1)) (s (n + 2)))))
      (K.neg (K.mul (c (n + 1)) (K.mul (s (n + 1)) (s (n + 2)))))
      = K.mul (K.mul (c (n + 1)) (c (n + 1))) (K.mul (K.mul (s (n + 1)) (s (n + 1))) (K.mul (s (n + 2)) (s (n + 2)))) := by
    lib_ring
  rw [e0, e1]
  exact le_add' K P _ _ _ _ (le_add' K P _ _ _ _ q0 q0) (le_add' K P _ _ _ _ q1 q1)

end Gen

end DyNBound

#print axioms DyNBound.mul_le_mul'
#print axioms DyNBound.dyN_sq

ns/DyUnique.lean

lean_all/ns/DyUnique.lean

/-
  ns/DyUnique.lean — T37 cycle 783 (goal G494 = G490b): uniqueness of one Crank–Nicolson step of the dyadic model
  for small h, over any ordered ring without zero divisors (ℚ included). If a', b' both solve the step from a
  (modes 1..N, a(0) = 0, zero beyond N), the midpoints s = a' + a, r = b' + a have coefficient bounds
  (c_n(s_n + r_n))², (c_{n+1}s_{n+2})², (c_{n+1}r_{n+1})² ≤ M² (M > 0) and h·3M ≤ 1 − c (c > 0), then a' = b'.

  Lean 4 core plus ns/DyLip (dy_lip_pt), ns/CNUnique (cn_unique), ns/YoungM (pos_cancel), lib/RingTac.
    tele_r       Σ_{n<N} f n + f N = f 0 + Σ_{n<N} f (n+1)
    shift_le     d_0 = 0 ⇒ Σ_{n<N} d_n² ≤ Σ_{n<N} d_{n+1}²;   d_{N+1} = 0 ⇒ Σ_{n<N} d_{n+2}² ≤ Σ_{n<N} d_{n+1}²
    dy_lip       Σ_{n<N} d_{n+1}(dyN(s)_n − dyN(r)_n) ≤ 3M·Σ_{n<N} d_{n+1}²
    dy_unique    the statement
-/
import ns.DyLip
import ns.CNUnique

namespace DyUnique
open DetChain NSChain DyadicSmallGlobal LibRing DyadicLarge YoungM DyLip CNUnique GoodModesQ RatModel

section Gen
variable {R : Type} (K : CRing R)

theorem tele_r (f : Nat → R) : ∀ N, K.add (sumN K f N) (f N) = K.add (f 0) (sumN K (fun n => f (n + 1)) N)
  | 0 => by show K.add K.zero (f 0) = K.add (f 0) K.zero; lib_ring
  | N + 1 => by
    show K.add (K.add (sumN K f N) (f N)) (f (N + 1)) = K.add (f 0) (K.add (sumN K (fun n => f (n + 1)) N) (f (N + 1)))
    rw [tele_r f N]; lib_ring

variable (O : OrdR K)
include O

theorem shift_lo (d : Nat → R) (h0 : d 0 = K.zero) (N : Nat) :
    Le K O.toCone (sumN K (fun n => K.mul (d n) (d n)) N) (sumN K (fun n => K.mul (d (n + 1)) (d (n + 1))) N) := by
  have t : K.add (sumN K (fun n => K.mul (d n) (d n)) N) (K.mul (d N) (d N))
      = K.add (K.mul (d 0) (d 0)) (sumN K (fun n => K.mul (d (n + 1)) (d (n + 1))) N) :=
    tele_r K (fun n => K.mul (d n) (d n)) N
  rw [h0] at t
  exact ⟨_, O.nn_sq (d N), t.trans (by lib_ring)⟩

theorem shift_hi (d : Nat → R) (N : Nat) (hN : d (N + 1) = K.zero) :
    Le K O.toCone (sumN K (fun n => K.mul (d (n + 2)) (d (n + 2))) N) (sumN K (fun n => K.mul (d (n + 1)) (d (n + 1))) N) := by
  have t : K.add (sumN K (fun n => K.mul (d (n + 1)) (d (n + 1))) N) (K.mul (d (N + 1)) (d (N + 1)))
      = K.add (K.mul (d 1) (d 1)) (sumN K (fun n => K.mul (d (n + 2)) (d (n + 2))) N) :=
    tele_r K (fun n => K.mul (d (n + 1)) (d (n + 1))) N
  rw [hN] at t
  exact ⟨_, O.nn_sq (d 1), (show K.add (sumN K (fun n => K.mul (d (n + 2)) (d (n + 2))) N) (K.mul (d 1) (d 1))
      = K.add (K.mul (d 1) (d 1)) (sumN K (fun n => K.mul (d (n + 2)) (d (n + 2))) N) by lib_ring).trans
    (t.symm.trans (by lib_ring))⟩

theorem dy_lip (c s r : Nat → R) (N : Nat) (M : R) (hM : O.nn M) (hM0 : M ≠ K.zero) (h10 : K.one ≠ K.zero)
    (hd0 : K.add (s 0) (K.neg (r 0)) = K.zero) (hdN : K.add (s (N + 1)) (K.neg (r (N + 1))) = K.zero)
    (h1 : ∀ n, Le K O.toCone (K.mul (K.mul (c n) (K.add (s n) (r n))) (K.mul (c n) (K.add (s n) (r n)))) (K.mul M M))
    (h2 : ∀ n, Le K O.toCone (K.mul (K.mul (c (n + 1)) (s (n + 2))) (K.mul (c (n + 1)) (s (n + 2)))) (K.mul M M))
    (h3 : ∀ n, Le K O.toCone (K.mul (K.mul (c (n + 1)) (r (n + 1))) (K.mul (c (n + 1)) (r (n + 1)))) (K.mul M M)) :
    Le K O.toCone
      (sumN K (fun n => K.mul (K.add (s (n + 1)) (K.neg (r (n + 1)))) (K.add (dyN K c s n) (K.neg (dyN K c r n)))) N)
      (K.mul (K.add K.one (K.add K.one K.one)) (K.mul M
        (sumN K (fun n => K.mul (K.add (s (n + 1)) (K.neg (r (n + 1)))) (K.add (s (n + 1)) (K.neg (r (n + 1))))) N))) := by
  let d : Nat → R := fun n => K.add (s n) (K.neg (r n))
  let q : Nat → R := fun n => K.mul (d n) (d n)
  show Le K O.toCone (sumN K (fun n => K.mul (d (n + 1)) (K.add (dyN K c s n) (K.neg (dyN K c r n)))) N)
    (K.mul (K.add K.one (K.add K.one K.one)) (K.mul M (sumN K (fun n => q (n + 1)) N)))
  have pt := fun n => dy_lip_pt K O c s r n M hM hM0 (h1 n) (h2 n) (h3 n)
  have sm := sum_le K O.toCone _ _ pt N
  -- Σ 2·d(n+1)ΔN ≤ M(A + 4B + C) ≤ 6M·B
  have hA := shift_lo K O d hd0 N
  have hC := shift_hi K O d N hdN
  -- rewrite both sums as linear combinations
  have eL : sumN K (fun n => K.mul (K.add K.one K.one) (K.mul (d (n + 1)) (K.add (dyN K c s n) (K.neg (dyN K c r n))))) N
      = K.mul (K.add K.one K.one) (sumN K (fun n => K.mul (d (n + 1)) (K.add (dyN K c s n) (K.neg (dyN K c r n)))) N) :=
    sumN_mul_left K _ _ N
  have eR : ∀ N', sumN K (fun n => K.add (K.add (K.mul M (K.add (q n) (q (n + 1)))) (K.mul M (K.add (q (n + 1)) (q (n + 1)))))
        (K.mul M (K.add (q (n + 1)) (q (n + 2))))) N'
      = K.mul M (K.add (K.add (sumN K q N') (K.mul (K.add (K.add K.one K.one) (K.add K.one K.one)) (sumN K (fun n => q (n + 1)) N')))
          (sumN K (fun n => q (n + 2)) N')) := by
    intro N'
    induction N' with
    | zero => show K.zero = K.mul M (K.add (K.add K.zero (K.mul (K.add (K.add K.one K.one) (K.add K.one K.one)) K.zero)) K.zero); lib_ring
    | succ N' ih =>
      show K.add (sumN K (fun n => K.add (K.add (K.mul M (K.add (q n) (q (n + 1)))) (K.mul M (K.add (q (n + 1)) (q (n + 1)))))
          (K.mul M (K.add (q (n + 1)) (q (n + 2))))) N')
        (K.add (K.add (K.mul M (K.add (q N') (q (N' + 1)))) (K.mul M (K.add (q (N' + 1)) (q (N' + 1)))))
          (K.mul M (K.add (q (N' + 1)) (q (N' + 2))))) = K.mul M (K.add (K.add (K.add (sumN K q N') (q N'))
        (K.mul (K.add (K.add K.one K.one) (K.add K.one K.one)) (K.add (sumN K (fun n => q (n + 1)) N') (q (N' + 1)))))
        (K.add (sumN K (fun n => q (n + 2)) N') (q (N' + 2))))
      rw [ih]; lib_ring
  have sm' := sm
  rw [eL, eR] at sm'
  -- bound A + 4B + C ≤ 6B
  have hAB : Le K O.toCone (K.add (K.add (sumN K q N) (K.mul (K.add (K.add K.one K.one) (K.add K.one K.one)) (sumN K (fun n => q (n + 1)) N)))
      (sumN K (fun n => q (n + 2)) N))
      (K.mul (K.add (K.add K.one (K.add K.one K.one)) (K.add K.one (K.add K.one K.one))) (sumN K (fun n => q (n + 1)) N)) := by
    have := le_add' K O.toCone _ _ _ _ (le_add' K O.toCone _ _ _ _ hA (le_refl' K O.toCone
      (K.mul (K.add (K.add K.one K.one) (K.add K.one K.one)) (sumN K (fun n => q (n + 1)) N)))) hC
    have e6 : K.add (K.add (sumN K (fun n => q (n + 1)) N) (K.mul (K.add (K.add K.one K.one) (K.add K.one K.one)) (sumN K (fun n => q (n + 1)) N)))
        (sumN K (fun n => q (n + 1)) N)
        = K.mul (K.add (K.add K.one (K.add K.one K.one)) (K.add K.one (K.add K.one K.one))) (sumN K (fun n => q (n + 1)) N) := by lib_ring
    rw [e6] at this; exact this
  have l2 := le_trans' K O.toCone _ _ _ sm' (le_mul_nn K O.toCone M _ _ hM hAB)
  -- 2·S ≤ 2·(3·(M·B))  ⇒  S ≤ 3·(M·B)
  have h2ne : K.add K.one K.one ≠ K.zero := ne_of_one_add K O h10 K.one (nn_one' K O.toCone)
  have l3 : Le K O.toCone (K.mul (K.add K.one K.one) (sumN K (fun n => K.mul (d (n + 1)) (K.add (dyN K c s n) (K.neg (dyN K c r n)))) N))
      (K.mul (K.add K.one K.one) (K.mul (K.add K.one (K.add K.one K.one)) (K.mul M (sumN K (fun n => q (n + 1)) N)))) := by
    have e : K.mul M (K.mul (K.add (K.add K.one (K.add K.one K.one)) (K.add K.one (K.add K.one K.one))) (sumN K (fun n => q (n + 1)) N))
        = K.mul (K.add K.one K.one) (K.mul (K.add K.one (K.add K.one K.one)) (K.mul M (sumN K (fun n => q (n + 1)) N))) := by lib_ring
    rw [← e]; exact l2
  cases l3 with
  | intro e he =>
    have he2 : e = K.mul (K.add K.one K.one) (K.add (K.mul (K.add K.one (K.add K.one K.one)) (K.mul M (sumN K (fun n => q (n + 1)) N)))
        (K.neg (sumN K (fun n => K.mul (d (n + 1)) (K.add (dyN K c s n) (K.neg (dyN K c r n)))) N))) := by
      have : e = K.add (K.add (K.mul (K.add K.one K.one) (sumN K (fun n => K.mul (d (n + 1)) (K.add (dyN K c s n) (K.neg (dyN K c r n)))) N)) e)
          (K.neg (K.mul (K.add K.one K.one) (sumN K (fun n => K.mul (d (n + 1)) (K.add (dyN K c s n) (K.neg (dyN K c r n)))) N))) := by lib_ring
      rw [this, he.2]; lib_ring
    have hnn := pos_cancel K O (K.add K.one K.one) _ (O.nn_add _ _ (nn_one' K O.toCone) (nn_one' K O.toCone)) h2ne
      (by rw [← he2]; exact he.1)
    exact ⟨_, hnn, by lib_ring⟩

omit O in
theorem sumN_ext_g (f g : Nat → R) : ∀ N, (∀ n, n < N → f n = g n) → sumN K f N = sumN K g N
  | 0, _ => rfl
  | N + 1, h => by
    show K.add (sumN K f N) (f N) = K.add (sumN K g N) (g N)
    rw [sumN_ext_g f g N (fun n hn => h n (Nat.lt_succ_of_lt hn)), h N (Nat.lt_succ_self N)]

theorem dy_unique (c k : Nat → R) (a a' b' : Nat → R) (h nu M cc : R) (N : Nat)
    (hh : O.nn h) (hnu : O.nn nu) (hk : ∀ n, O.nn (k n)) (hM : O.nn M) (hM0 : M ≠ K.zero)
    (hcc : O.nn cc) (hcc0 : cc ≠ K.zero) (h10 : K.one ≠ K.zero)
    (a0' : a' 0 = K.zero) (b0' : b' 0 = K.zero) (aN' : a' (N + 1) = K.zero) (bN' : b' (N + 1) = K.zero)
    (s1 : ∀ n, K.add (a' (n + 1)) (K.neg (a (n + 1))) = K.mul h (K.add (K.neg (K.mul nu (K.mul (k (n + 1))
      (K.add (a' (n + 1)) (a (n + 1)))))) (dyN K c (fun i => K.add (a' i) (a i)) n)))
    (s2 : ∀ n, K.add (b' (n + 1)) (K.neg (a (n + 1))) = K.mul h (K.add (K.neg (K.mul nu (K.mul (k (n + 1))
      (K.add (b' (n + 1)) (a (n + 1)))))) (dyN K c (fun i => K.add (b' i) (a i)) n)))
    (h1 : ∀ n, Le K O.toCone (K.mul (K.mul (c n) (K.add (K.add (a' n) (a n)) (K.add (b' n) (a n))))
      (K.mul (c n) (K.add (K.add (a' n) (a n)) (K.add (b' n) (a n))))) (K.mul M M))
    (h2 : ∀ n, Le K O.toCone (K.mul (K.mul (c (n + 1)) (K.add (a' (n + 2)) (a (n + 2))))
      (K.mul (c (n + 1)) (K.add (a' (n + 2)) (a (n + 2))))) (K.mul M M))
    (h3 : ∀ n, Le K O.toCone (K.mul (K.mul (c (n + 1)) (K.add (b' (n + 1)) (a (n + 1))))
      (K.mul (c (n + 1)) (K.add (b' (n + 1)) (a (n + 1))))) (K.mul M M))
    (small : Le K O.toCone (K.mul h (K.mul (K.add K.one (K.add K.one K.one)) M)) (K.add K.one (K.neg cc))) :
    ∀ n, n < N → a' (n + 1) = b' (n + 1) := by
  let sA : Nat → R := fun i => K.add (a' i) (a i)
  let sB : Nat → R := fun i => K.add (b' i) (a i)
  have lp := dy_lip K O c sA sB N M hM hM0 h10
    (by show K.add (K.add (a' 0) (a 0)) (K.neg (K.add (b' 0) (a 0))) = K.zero; rw [a0', b0']; lib_ring)
    (by show K.add (K.add (a' (N + 1)) (a (N + 1))) (K.neg (K.add (b' (N + 1)) (a (N + 1)))) = K.zero
        rw [aN', bN']; lib_ring) h1 h2 h3
  have e1 := sumN_ext_g K (fun n => K.mul (K.add (sA (n + 1)) (K.neg (sB (n + 1)))) (K.add (dyN K c sA n) (K.neg (dyN K c sB n))))
    (fun n => K.mul (K.add (a' (n + 1)) (K.neg (b' (n + 1)))) (K.add (dyN K c sA n) (K.neg (dyN K c sB n)))) N
    (fun n _ => by show K.mul (K.add (K.add (a' (n + 1)) (a (n + 1))) (K.neg (K.add (b' (n + 1)) (a (n + 1))))) _ = _; lib_ring)
  have e2 := sumN_ext_g K (fun n => K.mul (K.add (sA (n + 1)) (K.neg (sB (n + 1)))) (K.add (sA (n + 1)) (K.neg (sB (n + 1)))))
    (fun n => K.mul (K.add (a' (n + 1)) (K.neg (b' (n + 1)))) (K.add (a' (n + 1)) (K.neg (b' (n + 1))))) N
    (fun n _ => by
      show K.mul (K.add (K.add (a' (n + 1)) (a (n + 1))) (K.neg (K.add (b' (n + 1)) (a (n + 1)))))
        (K.add (K.add (a' (n + 1)) (a (n + 1))) (K.neg (K.add (b' (n + 1)) (a (n + 1))))) = _; lib_ring)
  rw [e1, e2] at lp
  have lp' : Le K O.toCone (sumN K (fun n => K.mul (K.add (a' (n + 1)) (K.neg (b' (n + 1))))
      (K.add (dyN K c sA n) (K.neg (dyN K c sB n)))) N)
      (K.mul (K.mul (K.add K.one (K.add K.one K.one)) M)
        (sumN K (fun n => K.mul (K.add (a' (n + 1)) (K.neg (b' (n + 1)))) (K.add (a' (n + 1)) (K.neg (b' (n + 1))))) N)) := by
    rw [show K.mul (K.mul (K.add K.one (K.add K.one K.one)) M)
        (sumN K (fun n => K.mul (K.add (a' (n + 1)) (K.neg (b' (n + 1)))) (K.add (a' (n + 1)) (K.neg (b' (n + 1))))) N)
      = K.mul (K.add K.one (K.add K.one K.one)) (K.mul M
        (sumN K (fun n => K.mul (K.add (a' (n + 1)) (K.neg (b' (n + 1)))) (K.add (a' (n + 1)) (K.neg (b' (n + 1))))) N)) by lib_ring]
    exact lp
  exact cn_unique K O (fun n => a (n + 1)) (fun n => a' (n + 1)) (fun n => b' (n + 1)) (fun n => k (n + 1))
    (fun n => dyN K c sA n) (fun n => dyN K c sB n) h nu (K.mul (K.add K.one (K.add K.one K.one)) M) cc N
    hh hnu (fun n => hk (n + 1)) hcc hcc0 s1 s2 lp' small

end Gen

end DyUnique

#print axioms DyUnique.tele_r
#print axioms DyUnique.shift_lo
#print axioms DyUnique.shift_hi
#print axioms DyUnique.dy_lip
#print axioms DyUnique.sumN_ext_g
#print axioms DyUnique.dy_unique

ns/Dyadic.lean

lean_all/ns/Dyadic.lean

/-
  ns/Dyadic.lean — T37 cycle 1133 (goal G736): summing a dyadic sequence of step-size errors without square roots,
  over an ordered ring. For a quadratic form Q(u) = Σ_r μ_r|u_r|² (μ_r ≥ 0):
    geom_le    r, c ≥ 0, c(1 − r) = 1  ⇒  Σ_{j<J} r^j ≤ c
    tele_vs    Σ_{j<J} (x_{j+1} − x_j) = x_J − x_0   (per mode)
    dyadic     Q(x_{j+1} − x_j) ≤ C·k₀hʲ for all j, pq = 1, c_p(1 − p) = 1, c_r(1 − qh) = 1  ⇒
               Q(x_J − x_0) ≤ c_p·(C·k₀·c_r)   for every J.
  With h = 1/2, p = 3/4, q = 4/3 (qh = 2/3): Q(x_J − x_0) ≤ 12·C·k₀, so the step-halving sequence of StepCauchy is
  Cauchy in Qe with a modulus independent of the truncation (WCS.wcs_form).

  Lean 4 core plus ns/WCS, ns/EnstGronwall (pwr), ns/ForceBilin (v3_ext), ns/ForceDiff (dF).
-/
import ns.WCS
import ns.EnstGronwall
import ns.ForceDiff
import ns.TruncRate

namespace Dyadic
open DetChain NSChain DyadicSmallGlobal LibRing DyadicLarge FourierGalerkin ForceBound EnstDiff TimeLipN WCS EnstGronwall ForceBilin ForceDiff

section Gen
variable {R : Type} (K : CRing R) (O : OrdR K) {I : Type}

theorem sumN_le (f g : Nat → R) (h : ∀ n, Le K O.toCone (f n) (g n)) : ∀ M, Le K O.toCone (sumN K f M) (sumN K g M)
  | 0 => le_refl' K O.toCone _
  | M + 1 => le_add' K O.toCone _ _ _ _ (sumN_le f g h M) (h M)

theorem pwr_nn (x : R) (hx : O.nn x) : ∀ n, O.nn (pwr K x n)
  | 0 => RatModel.nn_one' K O.toCone
  | n + 1 => O.nn_mul _ _ hx (pwr_nn x hx n)

theorem pwr_mul (p q : R) : ∀ n, K.mul (pwr K p n) (pwr K q n) = pwr K (K.mul p q) n
  | 0 => by show K.mul K.one K.one = K.one; lib_ring
  | n + 1 => by
    show K.mul (K.mul p (pwr K p n)) (K.mul q (pwr K q n)) = K.mul (K.mul p q) (pwr K (K.mul p q) n)
    rw [← pwr_mul p q n]; lib_ring

theorem geom_id (r : R) : ∀ J, K.add (K.mul (K.add K.one (K.neg r)) (sumN K (pwr K r) J)) (pwr K r J) = K.one
  | 0 => by show K.add (K.mul (K.add K.one (K.neg r)) K.zero) K.one = K.one; lib_ring
  | J + 1 => by
    have ih := geom_id r J
    show K.add (K.mul (K.add K.one (K.neg r)) (K.add (sumN K (pwr K r) J) (pwr K r J))) (K.mul r (pwr K r J)) = K.one
    generalize sumN K (pwr K r) J = S at ih ⊢
    generalize pwr K r J = P at ih ⊢
    exact Eq.trans (by lib_ring) ih

theorem geom_le (r c : R) (hr : O.nn r) (hc : O.nn c) (h : K.mul c (K.add K.one (K.neg r)) = K.one) (J : Nat) :
    Le K O.toCone (sumN K (pwr K r) J) c := by
  have g := geom_id K r J
  refine ⟨K.mul c (pwr K r J), O.nn_mul _ _ hc (pwr_nn K O r hr J), ?_⟩
  calc K.add (sumN K (pwr K r) J) (K.mul c (pwr K r J))
      = K.add (K.mul (K.mul c (K.add K.one (K.neg r))) (sumN K (pwr K r) J)) (K.mul c (pwr K r J)) := by rw [h]; lib_ring
    _ = K.mul c (K.add (K.mul (K.add K.one (K.neg r)) (sumN K (pwr K r) J)) (pwr K r J)) := by lib_ring
    _ = c := by rw [g]; lib_ring

theorem tele_vs (x : Nat → I → V3 R) (J : Nat) (r : I) :
    vs K (fun j => dF K (x (j + 1)) (x j)) J r = sub3 K (x J r) (x 0 r) := by
  apply v3_ext
  · exact tele_fwd K (fun j => (x j r).x) J
  · exact tele_fwd K (fun j => (x j r).y) J
  · exact tele_fwd K (fun j => (x j r).z) J

theorem pwr_one : ∀ n, pwr K K.one n = K.one
  | 0 => rfl
  | n + 1 => by show K.mul K.one (pwr K K.one n) = K.one; rw [pwr_one n]; lib_ring

theorem dyadic (l : List I) (μ : I → R) (hμ : ∀ r, O.nn (μ r)) (x : Nat → I → V3 R) (C k0 h p q cp cr : R)
    (hC : O.nn C) (hk0 : O.nn k0) (hh : O.nn h) (hp : O.nn p) (hq : O.nn q) (hcp : O.nn cp) (hcr : O.nn cr)
    (hpq : K.mul p q = K.one) (hgp : K.mul cp (K.add K.one (K.neg p)) = K.one)
    (hgr : K.mul cr (K.add K.one (K.neg (K.mul q h))) = K.one)
    (hd : ∀ j, Le K O.toCone (lsum K (fun r => K.mul (μ r) (dot3 K (dF K (x (j + 1)) (x j) r) (dF K (x (j + 1)) (x j) r))) l)
      (K.mul C (K.mul k0 (pwr K h j)))) (J : Nat) :
    Le K O.toCone (lsum K (fun r => K.mul (μ r) (dot3 K (sub3 K (x J r) (x 0 r)) (sub3 K (x J r) (x 0 r)))) l)
      (K.mul cp (K.mul C (K.mul k0 cr))) := by
  have w := wcs_form K O.toCone l μ hμ (pwr K p) (pwr K q) (pwr_nn K O p hp)
    (fun i => by rw [pwr_mul, hpq]; exact pwr_one K i) (fun j => dF K (x (j + 1)) (x j)) J
  have e : lsum K (fun r => K.mul (μ r) (dot3 K (vs K (fun j => dF K (x (j + 1)) (x j)) J r) (vs K (fun j => dF K (x (j + 1)) (x j)) J r))) l
      = lsum K (fun r => K.mul (μ r) (dot3 K (sub3 K (x J r) (x 0 r)) (sub3 K (x J r) (x 0 r)))) l :=
    lsum_ext K _ _ (fun r => by rw [tele_vs K x J r]) l
  rw [e] at w
  have s1 := sumN_le K O _ (fun j => K.mul (pwr K q j) (K.mul C (K.mul k0 (pwr K h j))))
    (fun j => le_mul_nn K O.toCone _ _ _ (pwr_nn K O q hq j) (hd j)) J
  have e2 : sumN K (fun j => K.mul (pwr K q j) (K.mul C (K.mul k0 (pwr K h j)))) J
      = K.mul (K.mul C k0) (sumN K (pwr K (K.mul q h)) J) := by
    rw [sumN_mul_l]
    apply AbelCount.sumN_congr; intro j
    rw [← pwr_mul]
    generalize pwr K q j = a; generalize pwr K h j = b
    lib_ring
  rw [e2] at s1
  have s2 := le_mul_nn K O.toCone (K.mul C k0) _ _ (O.nn_mul _ _ hC hk0)
    (geom_le K O (K.mul q h) cr (O.nn_mul _ _ hq hh) hcr hgr J)
  have s3 := geom_le K O p cp hp hcp hgp J
  have nW : O.nn (sumN K (fun j => K.mul (pwr K q j) (lsum K (fun r => K.mul (μ r) (dot3 K (dF K (x (j + 1)) (x j) r) (dF K (x (j + 1)) (x j) r))) l)) J) :=
    WCS.sumN_nn K O.toCone _ (fun j => O.nn_mul _ _ (pwr_nn K O q hq j) (lsum_nn K O.toCone _ (fun r => O.nn_mul _ _ (hμ r) (dot3_nn K O.toCone _)) l)) J
  have m := TruncRate.mm K O _ _ _ _ (WCS.sumN_nn K O.toCone _ (pwr_nn K O p hp) J) (O.nn_mul _ _ (O.nn_mul _ _ hC hk0) hcr) s3
    (le_trans' K O.toCone _ _ _ s1 s2)
  refine le_trans' K O.toCone _ _ _ w (le_trans' K O.toCone _ _ _ m ⟨K.zero, O.nn_zero, ?_⟩)
  lib_ring

end Gen

end Dyadic

#print axioms Dyadic.sumN_le
#print axioms Dyadic.pwr_nn
#print axioms Dyadic.pwr_mul
#print axioms Dyadic.geom_id
#print axioms Dyadic.geom_le
#print axioms Dyadic.tele_vs
#print axioms Dyadic.pwr_one
#print axioms Dyadic.dyadic

ns/DyadicBudget.lean

lean_all/ns/DyadicBudget.lean

/-
  ns/DyadicBudget.lean — T37 cycle 743 (goal G456): the dissipation budget for the dyadic model over any ring, and
  for the concrete supercritical model over any ordered ring / ℚ: the discrete L²_t H¹_x bound of the midpoints.

  Lean 4 core plus ns/DissBudget (budget, budget_le), ns/RatModel (P4, P2), ns/DyadicLargeData (E0),
  NSChain (dyadic_energy_neutral).
    dy_budget     E(T) + Σ_{t<T} h ν Σ_{n<N} k_{n+1} (a_{t+1}(n+1) + a_t(n+1))² = E(0)       (exact, any ring)
    model_budget  Σ_{t<T} h·3·Σ_{n<N} 4^{n+1} (a_{t+1}(n+1) + a_t(n+1))² ≤ E₀           (any ordered ring, ℚ)
-/
import ns.DissBudget
import ns.RatModel

namespace DyadicBudget
open DetChain NSChain LibRing DyadicLargeData DissBudget RatModel

section Gen
variable {R : Type} (K : CRing R)

theorem dy_budget (c k : Nat → R) (a : Nat → Nat → R) (h nu : R) (N : Nat)
    (h0 : ∀ t, a t 0 = K.zero) (hz : ∀ t n, N + 1 ≤ n → a t n = K.zero)
    (step : ∀ t n, K.add (a (t + 1) (n + 1)) (K.neg (a t (n + 1)))
      = K.mul h (K.add (K.neg (K.mul nu (K.mul (k (n + 1)) (K.add (a (t + 1) (n + 1)) (a t (n + 1))))))
          (dyN K c (fun i => K.add (a (t + 1) i) (a t i)) n))) (T : Nat) :
    K.add (En K (fun t n => a t (n + 1)) N T) (sumN K (D K (fun t n => a t (n + 1)) (fun n => k (n + 1)) h nu N) T)
      = E0 K a N :=
  budget K (fun t n => a t (n + 1)) (fun n => k (n + 1))
    (fun t n => dyN K c (fun i => K.add (a (t + 1) i) (a t i)) n) h nu N step
    (fun t => dyadic_energy_neutral K c (fun i => K.add (a (t + 1) i) (a t i)) N
      (by show K.add (a (t + 1) 0) (a t 0) = K.zero; rw [h0, h0, K.zero_add])
      (by show K.add (a (t + 1) (N + 1)) (a t (N + 1)) = K.zero
          rw [hz (t + 1) (N + 1) (Nat.le_refl _), hz t (N + 1) (Nat.le_refl _), K.zero_add])) T

theorem model_budget (P : Cone K) (a : Nat → Nat → R) (h : R) (N : Nat)
    (h0 : ∀ t, a t 0 = K.zero) (hz : ∀ t n, N + 1 ≤ n → a t n = K.zero)
    (step : ∀ t n, K.add (a (t + 1) (n + 1)) (K.neg (a t (n + 1)))
      = K.mul h (K.add (K.neg (K.mul (castN K 3) (K.mul (P4 K (n + 1)) (K.add (a (t + 1) (n + 1)) (a t (n + 1))))))
          (dyN K (P2 K) (fun i => K.add (a (t + 1) i) (a t i)) n))) (T : Nat) :
    Le K P (sumN K (D K (fun t n => a t (n + 1)) (fun n => P4 K (n + 1)) h (castN K 3) N) T) (E0 K a N) :=
  ⟨_, sq_sum_nonneg K P (fun n => a T (n + 1)) N, by
    rw [K.add_comm]; exact dy_budget K (P2 K) (P4 K) a h (castN K 3) N h0 hz step T⟩

end Gen

end DyadicBudget

#print axioms DyadicBudget.dy_budget
#print axioms DyadicBudget.model_budget

ns/DyadicCN.lean

lean_all/ns/DyadicCN.lean

/-
  ns/DyadicCN.lean — T37 cycle 20: the small-data H^s bound of ns/DyadicHs for actual time steps.

  Lean 4 core plus ns/DyadicHs. Crank–Nicolson for the viscous dyadic model on shells 1..N:
      a'_{n+1} − a_{n+1} = h (−ν k_{n+1} s_{n+1} + N_n(s)),   s = a' + a,
  (the midpoint factors absorbed into h and the coefficients). Then
      E_w(a') = E_w(a) + h · Rate(s),   E_w(a) = Σ_{n<N} w_{n+1} a_{n+1}²,
  where Rate is the H^s rate of DyadicHs.hs_rate evaluated at s. With the small-data condition on s
  (every shell of s ≤ M, (w_{n+1} − w_n)c_n M = ν w_n k_n), Rate(s) ≤ 0 (DyadicHs.small_data), so:
    cn_hs_identity   E_w(a') = E_w(a) + h·Rate(s), every N, every ring
    cn_hs_step       E_w(a') ≤ E_w(a) for h ≥ 0
    cn_hs_global     if every step satisfies the small-data condition, E_w(a_t) ≤ E_w(a_0) for all t
                     (λ = 2, s = 1, α = 1/2: the H¹ energy of every truncation stays below its
                     initial value as long as the shells of a_t + a_{t+1} stay below ν/3)
-/
import ns.DyadicHs

namespace DyadicCN
open DetChain NSChain DyadicHs

section Ring
variable {R : Type} (K : CRing R)

def rate (w c k s : Nat → R) (nu : R) (N : Nat) : R :=
  sumN K (fun n => K.mul (w (n + 1)) (K.mul (s (n + 1))
    (K.add (K.neg (K.mul nu (K.mul (k (n + 1)) (s (n + 1))))) (dyN K c s n)))) N

def Ew (w a : Nat → R) (N : Nat) : R := sumN K (fun n => K.mul (w (n + 1)) (K.mul (a (n + 1)) (a (n + 1)))) N

theorem cn_hs_identity (w c k a a' s : Nat → R) (h nu : R) (N : Nat)
    (hs : ∀ i, s i = K.add (a' i) (a i))
    (step : ∀ n, K.add (a' (n + 1)) (K.neg (a (n + 1)))
      = K.mul h (K.add (K.neg (K.mul nu (K.mul (k (n + 1)) (s (n + 1))))) (dyN K c s n))) :
    Ew K w a' N = K.add (Ew K w a N) (K.mul h (rate K w c k s nu N)) := by
  unfold Ew rate
  rw [← sumN_mul_left, ← sumN_add]
  apply sumN_ext
  intro n
  rw [sq_diff K (a' (n + 1)) (a (n + 1)), step n, K.distrib, ← hs (n + 1)]
  congr 1
  generalize K.add (K.neg (K.mul nu (K.mul (k (n + 1)) (s (n + 1))))) (dyN K c s n) = X
  rw [K.mul_assoc h X (s (n + 1)), K.mul_comm X (s (n + 1)), ← K.mul_assoc (w (n + 1)) h,
    K.mul_comm (w (n + 1)) h, K.mul_assoc h (w (n + 1))]

theorem cn_hs_step (P : Cone K) (w c k a a' : Nat → R) (h nu M : R) (N : Nat) (hh : P.nn h)
    (h0 : a 0 = K.zero) (h0' : a' 0 = K.zero) (hN : a (N + 1) = K.zero) (hN' : a' (N + 1) = K.zero)
    (step : ∀ n, K.add (a' (n + 1)) (K.neg (a (n + 1)))
      = K.mul h (K.add (K.neg (K.mul nu (K.mul (k (n + 1)) (K.add (a' (n + 1)) (a (n + 1))))))
          (dyN K c (fun i => K.add (a' i) (a i)) n)))
    (hpos : ∀ n, P.nn (K.mul (K.add (w (n + 1)) (K.neg (w n))) (c n)))
    (hcrit : ∀ n, K.mul (K.mul (K.add (w (n + 1)) (K.neg (w n))) (c n)) M = K.mul nu (K.mul (w n) (k n)))
    (hsmall : ∀ n, Le K P (K.add (a' (n + 1)) (a (n + 1))) M)
    (hnuD : ∀ n, P.nn (K.mul nu (K.mul (w n) (K.mul (k n)
      (K.mul (K.add (a' n) (a n)) (K.add (a' n) (a n))))))) :
    Le K P (Ew K w a' N) (Ew K w a N) := by
  have s0 : K.add (a' 0) (a 0) = K.zero := by rw [h0, h0', K.zero_add]
  have sN : K.add (a' (N + 1)) (a (N + 1)) = K.zero := by rw [hN, hN', K.zero_add]
  have hr := small_data K P w c k (fun i => K.add (a' i) (a i)) nu M N s0 sN hpos hcrit hsmall hnuD
  cases hr with
  | intro d hd =>
    refine ⟨K.mul h d, P.nn_mul _ _ hh hd.1, ?_⟩
    rw [cn_hs_identity K w c k a a' (fun i => K.add (a' i) (a i)) h nu N (fun _ => rfl) step,
      K.add_assoc, ← K.distrib]
    have hd2 : K.add (rate K w c k (fun i => K.add (a' i) (a i)) nu N) d = K.zero := hd.2
    rw [hd2, mul_zero K, add_zero']

/-- All time steps: the H^s energy never exceeds its initial value. -/
theorem cn_hs_global (P : Cone K) (w c k : Nat → R) (a : Nat → Nat → R) (h nu M : R) (N : Nat)
    (hh : P.nn h)
    (h0 : ∀ t, a t 0 = K.zero) (hN : ∀ t, a t (N + 1) = K.zero)
    (step : ∀ t n, K.add (a (t + 1) (n + 1)) (K.neg (a t (n + 1)))
      = K.mul h (K.add (K.neg (K.mul nu (K.mul (k (n + 1)) (K.add (a (t + 1) (n + 1)) (a t (n + 1))))))
          (dyN K c (fun i => K.add (a (t + 1) i) (a t i)) n)))
    (hpos : ∀ n, P.nn (K.mul (K.add (w (n + 1)) (K.neg (w n))) (c n)))
    (hcrit : ∀ n, K.mul (K.mul (K.add (w (n + 1)) (K.neg (w n))) (c n)) M = K.mul nu (K.mul (w n) (k n)))
    (hsmall : ∀ t n, Le K P (K.add (a (t + 1) (n + 1)) (a t (n + 1))) M)
    (hnuD : ∀ t n, P.nn (K.mul nu (K.mul (w n) (K.mul (k n)
      (K.mul (K.add (a (t + 1) n) (a t n)) (K.add (a (t + 1) n) (a t n))))))) :
    ∀ t, Le K P (Ew K w (a t) N) (Ew K w (a 0) N)
  | 0 => le_refl' K P _
  | t + 1 => le_trans' K P _ _ _
      (cn_hs_step K P w c k (a t) (a (t + 1)) h nu M N hh (h0 t) (h0 (t + 1)) (hN t) (hN (t + 1))
        (step t) hpos hcrit (hsmall t) (hnuD t))
      (cn_hs_global P w c k a h nu M N hh h0 hN step hpos hcrit hsmall hnuD t)

end Ring

end DyadicCN

#print axioms DyadicCN.cn_hs_identity
#print axioms DyadicCN.cn_hs_step
#print axioms DyadicCN.cn_hs_global

ns/DyadicCoef.lean

lean_all/ns/DyadicCoef.lean

/-
  ns/DyadicCoef.lean — T37 cycle 1175 (goal G762): mode-by-mode Cauchy with an explicit modulus for the step-halving
  solutions, over an ordered ring — the data of a constructive (Bishop-style) limit without real numbers.
    term_le     r ∈ l, all terms ≥ 0  ⇒  f r ≤ Σ_{l} f
    dyadic_from Q(x_{j+i+1} − x_{j+i}) ≤ C·(k₀hʲ)·hⁱ for all i  ⇒  Q(x_{j+J} − x_j) ≤ c_p·(C·(k₀hʲ)·c_r)
    coef_cauchy for every mode r ∈ l: μ_r·|x_{j+J,r} − x_{j,r}|² ≤ c_p·(C·(k₀hʲ)·c_r)
  With μ = νw (enstrophy weight, w_r ≥ 1) and h = 1/2: every Fourier coefficient of the step-halving sequence
  (StepCauchy) moves by at most 12·C·k₀·2^{−j}/ν after level j, uniformly in J and in the truncation.

  Lean 4 core plus ns/Dyadic.
-/
import ns.Dyadic

namespace DyadicCoef
open DetChain NSChain DyadicSmallGlobal LibRing DyadicLarge FourierGalerkin ForceBound EnstDiff TimeLipN WCS EnstGronwall
  ForceBilin ForceDiff Dyadic

section Gen
variable {R : Type} (K : CRing R) (O : OrdR K) {I : Type}

theorem term_le (f : I → R) (hf : ∀ r, O.nn (f r)) : ∀ (l : List I) (r : I), r ∈ l → Le K O.toCone (f r) (lsum K f l)
  | [], _, h => nomatch h
  | x :: l, r, h => by
    cases h with
    | head => exact ⟨lsum K f l, lsum_nn K O.toCone _ hf l, rfl⟩
    | tail _ h' =>
      have ih := term_le f hf l r h'
      refine le_trans' K O.toCone _ _ _ ih ⟨f x, hf x, ?_⟩
      show K.add (lsum K f l) (f x) = K.add (f x) (lsum K f l)
      lib_ring

theorem pwr_add (h : R) (j : Nat) : ∀ i, pwr K h (j + i) = K.mul (pwr K h j) (pwr K h i)
  | 0 => by show pwr K h j = K.mul (pwr K h j) K.one; lib_ring
  | i + 1 => by
    show K.mul h (pwr K h (j + i)) = K.mul (pwr K h j) (K.mul h (pwr K h i))
    rw [pwr_add h j i]; lib_ring

theorem dyadic_from (l : List I) (μ : I → R) (hμ : ∀ r, O.nn (μ r)) (x : Nat → I → V3 R) (C k0 h p q cp cr : R)
    (hC : O.nn C) (hk0 : O.nn k0) (hh : O.nn h) (hp : O.nn p) (hq : O.nn q) (hcp : O.nn cp) (hcr : O.nn cr)
    (hpq : K.mul p q = K.one) (hgp : K.mul cp (K.add K.one (K.neg p)) = K.one)
    (hgr : K.mul cr (K.add K.one (K.neg (K.mul q h))) = K.one)
    (hd : ∀ i, Le K O.toCone (lsum K (fun r => K.mul (μ r) (dot3 K (dF K (x (i + 1)) (x i) r) (dF K (x (i + 1)) (x i) r))) l)
      (K.mul C (K.mul k0 (pwr K h i)))) (j J : Nat) :
    Le K O.toCone (lsum K (fun r => K.mul (μ r) (dot3 K (sub3 K (x (j + J) r) (x j r)) (sub3 K (x (j + J) r) (x j r)))) l)
      (K.mul cp (K.mul C (K.mul (K.mul k0 (pwr K h j)) cr))) := by
  have d := dyadic K O l μ hμ (fun i => x (j + i)) C (K.mul k0 (pwr K h j)) h p q cp cr hC
    (O.nn_mul _ _ hk0 (Dyadic.pwr_nn K O h hh j)) hh hp hq hcp hcr hpq hgp hgr (fun i => by
      have e := hd (j + i)
      rw [pwr_add K h j i] at e
      refine le_trans' K O.toCone _ _ _ e ⟨K.zero, O.nn_zero, ?_⟩
      lib_ring) J
  exact d

theorem coef_cauchy (l : List I) (μ : I → R) (hμ : ∀ r, O.nn (μ r)) (x : Nat → I → V3 R) (C k0 h p q cp cr : R)
    (hC : O.nn C) (hk0 : O.nn k0) (hh : O.nn h) (hp : O.nn p) (hq : O.nn q) (hcp : O.nn cp) (hcr : O.nn cr)
    (hpq : K.mul p q = K.one) (hgp : K.mul cp (K.add K.one (K.neg p)) = K.one)
    (hgr : K.mul cr (K.add K.one (K.neg (K.mul q h))) = K.one)
    (hd : ∀ i, Le K O.toCone (lsum K (fun r => K.mul (μ r) (dot3 K (dF K (x (i + 1)) (x i) r) (dF K (x (i + 1)) (x i) r))) l)
      (K.mul C (K.mul k0 (pwr K h i)))) (j J : Nat) (r : I) (hr : r ∈ l) :
    Le K O.toCone (K.mul (μ r) (dot3 K (sub3 K (x (j + J) r) (x j r)) (sub3 K (x (j + J) r) (x j r))))
      (K.mul cp (K.mul C (K.mul (K.mul k0 (pwr K h j)) cr))) :=
  le_trans' K O.toCone _ _ _
    (term_le K O (fun r => K.mul (μ r) (dot3 K (sub3 K (x (j + J) r) (x j r)) (sub3 K (x (j + J) r) (x j r))))
      (fun r => O.nn_mul _ _ (hμ r) (dot3_nn K O.toCone _)) l r hr)
    (dyadic_from K O l μ hμ x C k0 h p q cp cr hC hk0 hh hp hq hcp hcr hpq hgp hgr hd j J)

end Gen

end DyadicCoef

#print axioms DyadicCoef.term_le
#print axioms DyadicCoef.pwr_add
#print axioms DyadicCoef.dyadic_from
#print axioms DyadicCoef.coef_cauchy

ns/DyadicHs.lean

lean_all/ns/DyadicHs.lean

/-
  ns/DyadicHs.lean — T37 cycle 1: the H^s energy of the dyadic model, and small data at the
  critical dissipation.

  Lean 4 core plus NSChain (CRing from DetChain, sumN, dyN, flux, Cone, Le). Hand-written proofs.

  The dyadic model (NSChain.dyN): shell n+1 has the nonlinearity c_n a_n² − c_{n+1} a_{n+1} a_{n+2},
  a_0 = 0 (boundary), a_{N+1} = 0 (truncation), and the flux through shell n is
  g_n = c_n a_n² a_{n+1}. For weights w (w_n = λ^{2sn} gives the H^s energy Σ w_n a_n²):

    weighted_flux   Σ_{n<N} w_{n+1} a_{n+1} N_n(a) = Σ_{n<N} (w_{n+1} − w_n) g_n      (Abel summation;
                    for w ≡ 1 it is NSChain.dyadic_energy_neutral)
    hs_rate         the H^s rate of the viscous model
                       Σ_{n<N} w_{n+1} a_{n+1}(−ν k_{n+1} a_{n+1} + N_n)
                    plus the dissipation of the shells 0..N equals Σ_{n<N} (w_{n+1} − w_n) g_n
    small_data      if (w_{n+1} − w_n) c_n ≥ 0, (w_{n+1} − w_n) c_n M = ν w_n k_n and a_{n+1} ≤ M for
                    every n, then the H^s rate is ≤ 0 (Le … 0): the H^s energy does not increase.
                    For λ = 2, s = 1, dissipation degree α = 1/2 (w = 4^n, c = 2^n, k = 2^n) the
                    condition is 3M = ν: every shell below ν/3 ⇒ H¹ non-increasing, for every N.
    small_data_check  the arithmetic of that instance: (4^{n+1} − 4^n) 2^n = 3 · 4^n 2^n
-/
import NSChain

namespace DyadicHs
open DetChain NSChain

section Ring
variable {R : Type} (K : CRing R)

theorem neg_add' (x y : R) : K.neg (K.add x y) = K.add (K.neg x) (K.neg y) := by
  apply (neg_unique K _ _ _).symm
  rw [add_mid4 K, add_neg' K, add_neg' K, K.zero_add]

/-- The one-step identity of Abel summation:
    w₁(g₀ − g₁) − (w₁ − w₀) g₀ = w₀ g₀ − w₁ g₁. -/
theorem abel_step (w0 w1 g0 g1 : R) :
    K.add (K.mul w1 (K.add g0 (K.neg g1))) (K.neg (K.mul (K.add w1 (K.neg w0)) g0))
      = K.add (K.mul w0 g0) (K.neg (K.mul w1 g1)) := by
  rw [K.distrib, mul_neg' K, right_distrib K, neg_mul_left K, neg_add' K, neg_neg' K, add_mid4 K,
    K.add_neg, K.zero_add, K.add_comm]

/-- Abel summation: Σ w_{n+1}(g_n − g_{n+1}) − Σ (w_{n+1} − w_n) g_n = w_0 g_0 − w_N g_N. -/
theorem abel (w g : Nat → R) : ∀ N,
    K.add (sumN K (fun n => K.mul (w (n + 1)) (K.add (g n) (K.neg (g (n + 1))))) N)
      (K.neg (sumN K (fun n => K.mul (K.add (w (n + 1)) (K.neg (w n))) (g n)) N))
      = K.add (K.mul (w 0) (g 0)) (K.neg (K.mul (w N) (g N)))
  | 0 => by
    show K.add K.zero (K.neg K.zero) = _
    rw [K.add_neg]
    exact (K.add_neg _).symm
  | N + 1 => by
    show K.add (K.add (sumN K (fun n => K.mul (w (n + 1)) (K.add (g n) (K.neg (g (n + 1))))) N)
        (K.mul (w (N + 1)) (K.add (g N) (K.neg (g (N + 1))))))
      (K.neg (K.add (sumN K (fun n => K.mul (K.add (w (n + 1)) (K.neg (w n))) (g n)) N)
        (K.mul (K.add (w (N + 1)) (K.neg (w N))) (g N)))) = _
    rw [neg_add' K, add_mid4 K, abel w g N, K.add_assoc, abel_step K]
    rw [← K.add_assoc (K.neg (K.mul (w N) (g N))), add_neg' K, K.zero_add]

/-- The weighted flux identity. -/
theorem weighted_flux (w c a : Nat → R) (N : Nat) (h0 : a 0 = K.zero) (hN : a (N + 1) = K.zero) :
    sumN K (fun n => K.mul (w (n + 1)) (K.mul (a (n + 1)) (dyN K c a n))) N
      = sumN K (fun n => K.mul (K.add (w (n + 1)) (K.neg (w n))) (flux K c a n)) N := by
  have e : ∀ n, K.mul (w (n + 1)) (K.mul (a (n + 1)) (dyN K c a n))
      = K.mul (w (n + 1)) (K.add (flux K c a n) (K.neg (flux K c a (n + 1)))) :=
    fun n => by rw [dyadic_term K c a n]
  rw [sumN_ext K _ _ e N]
  have hab := abel K w (flux K c a) N
  have g0 : flux K c a 0 = K.zero := by
    unfold flux
    rw [h0, mul_zero K, mul_zero K, zero_mul']
  have gN : flux K c a N = K.zero := by
    unfold flux
    rw [hN, mul_zero K]
  rw [g0, gN, mul_zero K, mul_zero K, K.add_neg] at hab
  -- X + (−Y) = 0  ⇒  X = Y
  have := neg_unique K _ _ hab
  rw [neg_neg' K] at this
  exact this

/-- Shift of a sum: Σ_{n<N+1} f n = f 0 + Σ_{n<N} f (n+1). -/
theorem sumN_shift (f : Nat → R) : ∀ N,
    sumN K f (N + 1) = K.add (f 0) (sumN K (fun n => f (n + 1)) N)
  | 0 => by
    show K.add K.zero (f 0) = K.add (f 0) K.zero
    exact K.add_comm _ _
  | N + 1 => by
    show K.add (sumN K f (N + 1)) (f (N + 1)) = K.add (f 0) (K.add (sumN K (fun n => f (n + 1)) N) (f (N + 1)))
    rw [sumN_shift f N, K.add_assoc]

/-- The H^s rate: with D_n = ν w_n k_n a_n² (dissipation of shell n),
    Σ_{n<N} w_{n+1} a_{n+1}(−ν k_{n+1} a_{n+1} + N_n) + Σ_{n<N+1} D_n = Σ_{n<N} (w_{n+1} − w_n) g_n. -/
theorem hs_rate (w c k a : Nat → R) (nu : R) (N : Nat) (h0 : a 0 = K.zero)
    (hN : a (N + 1) = K.zero) :
    K.add (sumN K (fun n => K.mul (w (n + 1)) (K.mul (a (n + 1))
        (K.add (K.neg (K.mul nu (K.mul (k (n + 1)) (a (n + 1))))) (dyN K c a n)))) N)
      (sumN K (fun n => K.mul nu (K.mul (w n) (K.mul (k n) (K.mul (a n) (a n))))) (N + 1))
      = sumN K (fun n => K.mul (K.add (w (n + 1)) (K.neg (w n))) (flux K c a n)) N := by
  let D : Nat → R := fun n => K.mul nu (K.mul (w n) (K.mul (k n) (K.mul (a n) (a n))))
  have e : ∀ n, K.mul (w (n + 1)) (K.mul (a (n + 1))
        (K.add (K.neg (K.mul nu (K.mul (k (n + 1)) (a (n + 1))))) (dyN K c a n)))
      = K.add (K.neg (D (n + 1))) (K.mul (w (n + 1)) (K.mul (a (n + 1)) (dyN K c a n))) := by
    intro n
    rw [K.distrib, K.distrib, mul_neg' K, mul_neg' K]
    show K.add (K.neg (K.mul (w (n + 1)) (K.mul (a (n + 1)) (K.mul nu (K.mul (k (n + 1)) (a (n + 1)))))))
        (K.mul (w (n + 1)) (K.mul (a (n + 1)) (dyN K c a n)))
      = K.add (K.neg (K.mul nu (K.mul (w (n + 1)) (K.mul (k (n + 1)) (K.mul (a (n + 1)) (a (n + 1)))))))
        (K.mul (w (n + 1)) (K.mul (a (n + 1)) (dyN K c a n)))
    have m : K.mul (w (n + 1)) (K.mul (a (n + 1)) (K.mul nu (K.mul (k (n + 1)) (a (n + 1)))))
        = K.mul nu (K.mul (w (n + 1)) (K.mul (k (n + 1)) (K.mul (a (n + 1)) (a (n + 1))))) := by
      rw [← K.mul_assoc (a (n + 1)), K.mul_comm (a (n + 1)) nu, K.mul_assoc nu,
        ← K.mul_assoc (w (n + 1)) nu, K.mul_comm (w (n + 1)) nu, K.mul_assoc nu]
      rw [← K.mul_assoc (a (n + 1)) (k (n + 1)), K.mul_comm (a (n + 1)) (k (n + 1)),
        K.mul_assoc (k (n + 1))]
    rw [m]
  rw [sumN_ext K _ _ e N, sumN_add, sumN_neg, weighted_flux K w c a N h0 hN, sumN_shift K D N]
  have d0 : D 0 = K.zero := by
    show K.mul nu (K.mul (w 0) (K.mul (k 0) (K.mul (a 0) (a 0)))) = K.zero
    rw [h0, mul_zero K, mul_zero K, mul_zero K, mul_zero K]
  rw [d0, K.zero_add, K.add_comm (K.neg _), K.add_assoc, add_neg' K, add_zero']

/-- Small data at the critical dissipation: the H^s energy does not increase. -/
theorem small_data (P : Cone K) (w c k a : Nat → R) (nu M : R) (N : Nat)
    (h0 : a 0 = K.zero) (hN : a (N + 1) = K.zero)
    (hpos : ∀ n, P.nn (K.mul (K.add (w (n + 1)) (K.neg (w n))) (c n)))
    (hcrit : ∀ n, K.mul (K.mul (K.add (w (n + 1)) (K.neg (w n))) (c n)) M = K.mul nu (K.mul (w n) (k n)))
    (hsmall : ∀ n, Le K P (a (n + 1)) M) (hnuD : ∀ n, P.nn (K.mul nu (K.mul (w n) (K.mul (k n) (K.mul (a n) (a n)))))) :
    Le K P (sumN K (fun n => K.mul (w (n + 1)) (K.mul (a (n + 1))
        (K.add (K.neg (K.mul nu (K.mul (k (n + 1)) (a (n + 1))))) (dyN K c a n)))) N) K.zero := by
  let Rt := sumN K (fun n => K.mul (w (n + 1)) (K.mul (a (n + 1))
        (K.add (K.neg (K.mul nu (K.mul (k (n + 1)) (a (n + 1))))) (dyN K c a n)))) N
  let D : Nat → R := fun n => K.mul nu (K.mul (w n) (K.mul (k n) (K.mul (a n) (a n))))
  let F : Nat → R := fun n => K.mul (K.add (w (n + 1)) (K.neg (w n))) (flux K c a n)
  -- per shell: F n + (D n − F n) = D n with D n − F n ≥ 0
  have per : ∀ n, ∃ d, P.nn d ∧ K.add (F n) d = D n := by
    intro n
    cases hsmall n with
    | intro e he =>
      let q := K.mul (K.add (w (n + 1)) (K.neg (w n))) (c n)
      refine ⟨K.mul (K.mul q (K.mul (a n) (a n))) e,
        P.nn_mul _ _ (P.nn_mul _ _ (hpos n) (P.nn_sq (a n))) he.1, ?_⟩
      -- F n = q a_n² a_{n+1},  D n = q a_n² M
      have hF : F n = K.mul (K.mul q (K.mul (a n) (a n))) (a (n + 1)) := by
        show K.mul (K.add (w (n + 1)) (K.neg (w n))) (K.mul (K.mul (c n) (K.mul (a n) (a n))) (a (n + 1)))
          = K.mul (K.mul (K.mul (K.add (w (n + 1)) (K.neg (w n))) (c n)) (K.mul (a n) (a n))) (a (n + 1))
        rw [K.mul_assoc (K.add (w (n + 1)) (K.neg (w n))) (c n), ← K.mul_assoc _ (K.mul (c n) _)]
      have hD : D n = K.mul (K.mul q (K.mul (a n) (a n))) M := by
        show K.mul nu (K.mul (w n) (K.mul (k n) (K.mul (a n) (a n))))
          = K.mul (K.mul q (K.mul (a n) (a n))) M
        show K.mul nu (K.mul (w n) (K.mul (k n) (K.mul (a n) (a n))))
          = K.mul (K.mul (K.mul (K.add (w (n + 1)) (K.neg (w n))) (c n)) (K.mul (a n) (a n))) M
        rw [K.mul_assoc (K.mul (K.add (w (n + 1)) (K.neg (w n))) (c n)) (K.mul (a n) (a n)) M,
          K.mul_comm (K.mul (a n) (a n)) M, ← K.mul_assoc _ M (K.mul (a n) (a n)), hcrit n,
          K.mul_assoc nu, K.mul_assoc (w n)]
      rw [hF, hD, ← K.distrib, he.2]
  -- Σ F + Σ d = Σ_{n<N} D ≤ Σ_{n<N+1} D, and Rt + Σ_{n<N+1} D = Σ F
  have sumPer : ∀ M', ∃ d, P.nn d ∧ K.add (sumN K F M') d = sumN K D M' := by
    intro M'
    induction M' with
    | zero => exact ⟨K.zero, P.nn_zero, add_zero' K _⟩
    | succ M' ih =>
      cases ih with
      | intro d hd =>
        cases per M' with
        | intro d' hd' =>
          refine ⟨K.add d d', P.nn_add _ _ hd.1 hd'.1, ?_⟩
          show K.add (K.add (sumN K F M') (F M')) (K.add d d') = K.add (sumN K D M') (D M')
          rw [add_mid4 K, hd.2, hd'.2]
  have hr := hs_rate K w c k a nu N h0 hN
  cases sumPer N with
  | intro d hd =>
    -- Rt + (Σ_{<N} D + D_N) = Σ F ;  Σ F + d = Σ_{<N} D  ⇒  Rt + (d + D_N) + Σ F = Σ F
    refine ⟨K.add d (D N), P.nn_add _ _ hd.1 (hnuD N), ?_⟩
    show K.add Rt (K.add d (D N)) = K.zero
    have hr' : K.add Rt (K.add (sumN K D N) (D N)) = sumN K F N := hr
    rw [← hd.2] at hr'
    -- Rt + ((ΣF + d) + D_N) = ΣF
    apply add_cancel_left K (sumN K F N)
    rw [add_zero']
    rw [← K.add_assoc (sumN K F N) Rt, K.add_comm (sumN K F N) Rt, K.add_assoc Rt,
      ← K.add_assoc (sumN K F N) d]
    exact hr'

end Ring

/-- The instance λ = 2, s = 1, α = 1/2: (4^{n+1} − 4^n)·2^n = 3·4^n·2^n (checked for n < 12). -/
theorem small_data_check :
    (List.range 12).all (fun n => Nat.beq ((4 ^ (n + 1) - 4 ^ n) * 2 ^ n) (3 * (4 ^ n * 2 ^ n))) = true := rfl

end DyadicHs

#print axioms DyadicHs.neg_add'
#print axioms DyadicHs.abel_step
#print axioms DyadicHs.abel
#print axioms DyadicHs.weighted_flux
#print axioms DyadicHs.sumN_shift
#print axioms DyadicHs.hs_rate
#print axioms DyadicHs.small_data
#print axioms DyadicHs.small_data_check

ns/DyadicLarge.lean

lean_all/ns/DyadicLarge.lean

/-
  ns/DyadicLarge.lean — T37 cycle 4: large data for the dyadic model with supercritical dissipation.

  Lean 4 core plus ns/DyadicHs. If every shell is bounded by A (e.g. A² = E(0), NSChain.cn_global)
  and the critical threshold M_n of shell n (q_n M_n = ν w_n k_n, q_n = (w_{n+1} − w_n) c_n) is ≥ A
  for every n ≥ n₀, then the H^s rate is bounded by B = Σ_{n<n₀} q_n E A, independently of the
  truncation N: the H^s energy grows at most linearly in time, so no truncation blows up and the
  bound is uniform in N. For λ = 2, s = 1 and dissipation degree α > 1/2, M_n = (ν/3)·2^{(2α−1)n}
  grows, so n₀ exists for every A (large data).

  Proved here:
    le_add', le_mul_nn, le_of_eq_add      order lemmas in a Cone
    shell_bound                           q a_n² a_{n+1} ≤ q a_n² M when a_{n+1} ≤ M
    rate_bound                            H^s rate ≤ Σ_{n<N} B'_n, B'_n = [n < n₀] q_n E A
    head_stable                           Σ_{n<N} B'_n = Σ_{n<n₀} B'_n for N ≥ n₀ (uniform in N)
-/
import ns.DyadicHs

namespace DyadicLarge
open DetChain NSChain DyadicHs

section Ring
variable {R : Type} (K : CRing R)

theorem le_add' (P : Cone K) (x y u v : R) (h1 : Le K P x y) (h2 : Le K P u v) :
    Le K P (K.add x u) (K.add y v) := by
  cases h1 with
  | intro d1 h1 =>
    cases h2 with
    | intro d2 h2 =>
      refine ⟨K.add d1 d2, P.nn_add _ _ h1.1 h2.1, ?_⟩
      rw [add_mid4 K, h1.2, h2.2]

theorem le_mul_nn (P : Cone K) (c x y : R) (hc : P.nn c) (h : Le K P x y) :
    Le K P (K.mul c x) (K.mul c y) := by
  cases h with
  | intro d h =>
    exact ⟨K.mul c d, P.nn_mul _ _ hc h.1, by rw [← K.distrib, h.2]⟩

theorem le_of_nn_add (P : Cone K) (x d : R) (hd : P.nn d) : Le K P x (K.add x d) := ⟨d, hd, rfl⟩

theorem le_zero_add (P : Cone K) (x d : R) (hd : P.nn d) : Le K P x (K.add d x) :=
  ⟨d, hd, K.add_comm x d⟩

theorem sum_le (P : Cone K) (f g : Nat → R) (h : ∀ n, Le K P (f n) (g n)) :
    ∀ N, Le K P (sumN K f N) (sumN K g N)
  | 0 => le_refl' K P _
  | N + 1 => le_add' K P _ _ _ _ (sum_le P f g h N) (h N)

/-- One shell: q a² a' ≤ q a² M when a' ≤ M and q ≥ 0. -/
theorem shell_bound (P : Cone K) (q an an1 M : R) (hq : P.nn q) (h : Le K P an1 M) :
    Le K P (K.mul (K.mul q (K.mul an an)) an1) (K.mul (K.mul q (K.mul an an)) M) :=
  le_mul_nn K P _ _ _ (P.nn_mul _ _ hq (P.nn_sq an)) h

def qq (w c : Nat → R) (n : Nat) : R := K.mul (K.add (w (n + 1)) (K.neg (w n))) (c n)

theorem flux_eq (w c a : Nat → R) (n : Nat) :
    K.mul (K.add (w (n + 1)) (K.neg (w n))) (flux K c a n)
      = K.mul (K.mul (qq K w c n) (K.mul (a n) (a n))) (a (n + 1)) := by
  unfold flux qq
  rw [K.mul_assoc (K.add (w (n + 1)) (K.neg (w n))) (c n), ← K.mul_assoc _ (K.mul (c n) _)]

/-- The H^s rate is bounded by the head of the flux, uniformly in N. -/
theorem rate_bound (P : Cone K) (w c k a M : Nat → R) (nu A E : R) (n0 N : Nat)
    (h0 : a 0 = K.zero) (hN : a (N + 1) = K.zero)
    (hq : ∀ n, P.nn (qq K w c n))
    (hcrit : ∀ n, n0 ≤ n → K.mul (qq K w c n) (M n) = K.mul nu (K.mul (w n) (k n)))
    (htail : ∀ n, n0 ≤ n → Le K P (a (n + 1)) (M n))
    (hA : ∀ n, Le K P (a (n + 1)) A) (hAnn : P.nn A)
    (hE : ∀ n, Le K P (K.mul (a n) (a n)) E)
    (hD : ∀ n, P.nn (K.mul nu (K.mul (w n) (K.mul (k n) (K.mul (a n) (a n)))))) :
    Le K P (sumN K (fun n => K.mul (w (n + 1)) (K.mul (a (n + 1))
        (K.add (K.neg (K.mul nu (K.mul (k (n + 1)) (a (n + 1))))) (dyN K c a n)))) N)
      (sumN K (fun n => if n < n0 then K.mul (qq K w c n) (K.mul E A) else K.zero) N) := by
  let D : Nat → R := fun n => K.mul nu (K.mul (w n) (K.mul (k n) (K.mul (a n) (a n))))
  let F : Nat → R := fun n => K.mul (K.add (w (n + 1)) (K.neg (w n))) (flux K c a n)
  let B : Nat → R := fun n => if n < n0 then K.mul (qq K w c n) (K.mul E A) else K.zero
  -- per shell: F n ≤ B n + D n
  have per : ∀ n, Le K P (F n) (K.add (B n) (D n)) := by
    intro n
    show Le K P (K.mul (K.add (w (n + 1)) (K.neg (w n))) (flux K c a n)) (K.add (B n) (D n))
    rw [flux_eq K w c a n]
    cases Nat.decLt n n0 with
    | isTrue hl =>
      have hB : B n = K.mul (qq K w c n) (K.mul E A) := ite_eq_left hl
      rw [hB]
      -- q a² a' ≤ q a² A ≤ q E A ≤ q E A + D
      have s1 := shell_bound K P (qq K w c n) (a n) (a (n + 1)) A (hq n) (hA n)
      have s2 : Le K P (K.mul (K.mul (qq K w c n) (K.mul (a n) (a n))) A) (K.mul (qq K w c n) (K.mul E A)) := by
        rw [K.mul_assoc]
        apply le_mul_nn K P _ _ _ (hq n)
        rw [K.mul_comm (K.mul (a n) (a n)) A, K.mul_comm E A]
        exact le_mul_nn K P _ _ _ hAnn (hE n)
      exact le_trans' K P _ _ _ (le_trans' K P _ _ _ s1 s2) (le_of_nn_add K P _ _ (hD n))
    | isFalse hg =>
      have hge : n0 ≤ n := Nat.le_of_not_lt hg
      have hB : B n = K.zero := ite_eq_right hg
      rw [hB, K.zero_add]
      have s1 := shell_bound K P (qq K w c n) (a n) (a (n + 1)) (M n) (hq n) (htail n hge)
      have e : K.mul (K.mul (qq K w c n) (K.mul (a n) (a n))) (M n) = D n := by
        show _ = K.mul nu (K.mul (w n) (K.mul (k n) (K.mul (a n) (a n))))
        rw [K.mul_assoc, K.mul_comm (K.mul (a n) (a n)) (M n), ← K.mul_assoc, hcrit n hge,
          K.mul_assoc nu, K.mul_assoc (w n)]
      rw [e] at s1
      exact s1
  have hr := hs_rate K w c k a nu N h0 hN
  -- Rt + (Σ_{<N} D + D_N) = Σ F ≤ Σ B + Σ_{<N} D
  have hF := sum_le K P F (fun n => K.add (B n) (D n)) per N
  rw [sumN_add] at hF
  cases hF with
  | intro d hd =>
    -- Rt + (ΣD + D_N) + d = ΣB + ΣD  ⇒  Rt + (d + D_N) = ΣB
    refine ⟨K.add d (D N), P.nn_add _ _ hd.1 (hD N), ?_⟩
    have hr' : K.add (sumN K (fun n => K.mul (w (n + 1)) (K.mul (a (n + 1))
        (K.add (K.neg (K.mul nu (K.mul (k (n + 1)) (a (n + 1))))) (dyN K c a n)))) N)
        (K.add (sumN K D N) (D N)) = sumN K F N := hr
    have h2 := hd.2
    rw [← hr'] at h2
    apply add_cancel_left K (sumN K D N)
    rw [K.add_comm (sumN K D N) (sumN K B N), ← h2]
    -- ΣD + (Rt + (d + D_N)) = (Rt + (ΣD + D_N)) + d
    generalize sumN K (fun n => K.mul (w (n + 1)) (K.mul (a (n + 1))
        (K.add (K.neg (K.mul nu (K.mul (k (n + 1)) (a (n + 1))))) (dyN K c a n)))) N = Rt
    generalize sumN K D N = SD
    rw [K.add_assoc Rt, K.add_comm (K.add SD (D N)) d, ← K.add_assoc d SD, K.add_comm d SD,
      K.add_assoc SD d, ← K.add_assoc Rt SD, K.add_comm Rt SD, K.add_assoc SD Rt]

/-- The bound does not depend on N ≥ n₀. -/
theorem head_stable (B0 : Nat → R) (n0 : Nat) :
    ∀ j, sumN K (fun n => if n < n0 then B0 n else K.zero) (n0 + j)
      = sumN K (fun n => if n < n0 then B0 n else K.zero) n0
  | 0 => rfl
  | j + 1 => by
    show K.add (sumN K (fun n => if n < n0 then B0 n else K.zero) (n0 + j))
        (if n0 + j < n0 then B0 (n0 + j) else K.zero) = _
    rw [head_stable B0 n0 j, ite_eq_right (Nat.not_lt_of_le (Nat.le_add_right n0 j)), add_zero']

end Ring

end DyadicLarge

#print axioms DyadicLarge.le_add'
#print axioms DyadicLarge.le_mul_nn
#print axioms DyadicLarge.le_of_nn_add
#print axioms DyadicLarge.le_zero_add
#print axioms DyadicLarge.sum_le
#print axioms DyadicLarge.shell_bound
#print axioms DyadicLarge.flux_eq
#print axioms DyadicLarge.rate_bound
#print axioms DyadicLarge.head_stable

ns/DyadicLargeCN.lean

lean_all/ns/DyadicLargeCN.lean

/-
  ns/DyadicLargeCN.lean — T37 cycle 686 (goal G399): the large-data bound of ns/DyadicLarge for actual
  Crank–Nicolson time steps.

  Lean 4 core plus ns/DyadicCN, ns/DyadicLarge. With s_t = a_{t+1} + a_t and, for every step, the hypotheses
  of DyadicLarge.rate_bound on s_t (shells ≤ A, s² ≤ E, tail shells n ≥ n₀ below the critical threshold M_n),
    cn_large_step    E_w(a_{t+1}) ≤ E_w(a_t) + h·B,        B = Σ_{n<N} [n < n₀] q_n E A
    cn_large_global  E_w(a_t) ≤ E_w(a_0) + Σ_{j<t} h·B    (linear growth in t)
  and B does not depend on N ≥ n₀ (DyadicLarge.head_stable): no truncation of the supercritical model
  blows up, uniformly in N.
-/
import ns.DyadicCN
import ns.DyadicLarge

namespace DyadicLargeCN
open DetChain NSChain DyadicHs DyadicCN DyadicLarge

section Ring
variable {R : Type} (K : CRing R)

def headB (w c : Nat → R) (E A : R) (n0 N : Nat) : R :=
  sumN K (fun n => if n < n0 then K.mul (qq K w c n) (K.mul E A) else K.zero) N

theorem cn_large_step (P : Cone K) (w c k a a' M : Nat → R) (h nu A E : R) (n0 N : Nat) (hh : P.nn h)
    (h0 : a 0 = K.zero) (h0' : a' 0 = K.zero) (hN : a (N + 1) = K.zero) (hN' : a' (N + 1) = K.zero)
    (step : ∀ n, K.add (a' (n + 1)) (K.neg (a (n + 1)))
      = K.mul h (K.add (K.neg (K.mul nu (K.mul (k (n + 1)) (K.add (a' (n + 1)) (a (n + 1))))))
          (dyN K c (fun i => K.add (a' i) (a i)) n)))
    (hq : ∀ n, P.nn (qq K w c n))
    (hcrit : ∀ n, n0 ≤ n → K.mul (qq K w c n) (M n) = K.mul nu (K.mul (w n) (k n)))
    (htail : ∀ n, n0 ≤ n → Le K P (K.add (a' (n + 1)) (a (n + 1))) (M n))
    (hA : ∀ n, Le K P (K.add (a' (n + 1)) (a (n + 1))) A) (hAnn : P.nn A)
    (hE : ∀ n, Le K P (K.mul (K.add (a' n) (a n)) (K.add (a' n) (a n))) E)
    (hD : ∀ n, P.nn (K.mul nu (K.mul (w n) (K.mul (k n)
      (K.mul (K.add (a' n) (a n)) (K.add (a' n) (a n))))))) :
    Le K P (Ew K w a' N) (K.add (Ew K w a N) (K.mul h (headB K w c E A n0 N))) := by
  have s0 : K.add (a' 0) (a 0) = K.zero := by rw [h0, h0', K.zero_add]
  have sN : K.add (a' (N + 1)) (a (N + 1)) = K.zero := by rw [hN, hN', K.zero_add]
  have hr := rate_bound K P w c k (fun i => K.add (a' i) (a i)) M nu A E n0 N s0 sN hq hcrit htail hA hAnn hE hD
  rw [cn_hs_identity K w c k a a' (fun i => K.add (a' i) (a i)) h nu N (fun _ => rfl) step]
  exact le_add' K P _ _ _ _ (le_refl' K P _) (le_mul_nn K P h _ _ hh hr)

theorem cn_large_global (P : Cone K) (w c k M : Nat → R) (a : Nat → Nat → R) (h nu A E : R) (n0 N : Nat)
    (hh : P.nn h)
    (h0 : ∀ t, a t 0 = K.zero) (hN : ∀ t, a t (N + 1) = K.zero)
    (step : ∀ t n, K.add (a (t + 1) (n + 1)) (K.neg (a t (n + 1)))
      = K.mul h (K.add (K.neg (K.mul nu (K.mul (k (n + 1)) (K.add (a (t + 1) (n + 1)) (a t (n + 1))))))
          (dyN K c (fun i => K.add (a (t + 1) i) (a t i)) n)))
    (hq : ∀ n, P.nn (qq K w c n))
    (hcrit : ∀ n, n0 ≤ n → K.mul (qq K w c n) (M n) = K.mul nu (K.mul (w n) (k n)))
    (htail : ∀ t n, n0 ≤ n → Le K P (K.add (a (t + 1) (n + 1)) (a t (n + 1))) (M n))
    (hA : ∀ t n, Le K P (K.add (a (t + 1) (n + 1)) (a t (n + 1))) A) (hAnn : P.nn A)
    (hE : ∀ t n, Le K P (K.mul (K.add (a (t + 1) n) (a t n)) (K.add (a (t + 1) n) (a t n))) E)
    (hD : ∀ t n, P.nn (K.mul nu (K.mul (w n) (K.mul (k n)
      (K.mul (K.add (a (t + 1) n) (a t n)) (K.add (a (t + 1) n) (a t n))))))) :
    ∀ t, Le K P (Ew K w (a t) N)
      (K.add (Ew K w (a 0) N) (sumN K (fun _ => K.mul h (headB K w c E A n0 N)) t))
  | 0 => ⟨K.zero, P.nn_zero, by show K.add _ _ = K.add _ K.zero; rw [add_zero']⟩
  | t + 1 => by
      have s1 := cn_large_step K P w c k (a t) (a (t + 1)) M h nu A E n0 N hh (h0 t) (h0 (t + 1)) (hN t)
        (hN (t + 1)) (step t) hq hcrit (htail t) (hA t) hAnn (hE t) (hD t)
      have ih := cn_large_global P w c k M a h nu A E n0 N hh h0 hN step hq hcrit htail hA hAnn hE hD t
      have s2 := le_add' K P _ _ _ _ ih (le_refl' K P (K.mul h (headB K w c E A n0 N)))
      have s3 := le_trans' K P _ _ _ s1 s2
      show Le K P _ (K.add (Ew K w (a 0) N)
        (K.add (sumN K (fun _ => K.mul h (headB K w c E A n0 N)) t) (K.mul h (headB K w c E A n0 N))))
      rw [← K.add_assoc]
      exact s3

end Ring

end DyadicLargeCN

#print axioms DyadicLargeCN.cn_large_step
#print axioms DyadicLargeCN.cn_large_global

ns/DyadicLargeData.lean

lean_all/ns/DyadicLargeData.lean

/-
  ns/DyadicLargeData.lean — T37 cycle 688 (goal G401): the supercritical large-data bound of the dyadic model
  in terms of the data alone.

  Lean 4 core plus ns/DyadicLargeEnergy, NSChain.cn_global. For Crank–Nicolson steps of the viscous dyadic model on
  shells 1..N (h, ν, k_n, w_n ≥ 0), the CN energy decay gives a_t(n)² ≤ E₀ = Σ_{n<N} a_0(n+1)² for every t, n
  (NSChain.cn_global with the energy-neutral nonlinearity); so with 4E₀ ≤ A² and the critical threshold M_n ≥ A
  for n ≥ n₀,
    cn_large_data   E_w(a_t) ≤ E_w(a_0) + Σ_{j<t} h·B,   B = Σ_{n<N} [n < n₀] q_n (4E₀) A,
  every truncation N, every time t; B depends only on the data (E₀, A) and n₀, not on N ≥ n₀.
-/
import ns.DyadicLargeEnergy

namespace DyadicLargeData
open DetChain NSChain DyadicHs DyadicCN DyadicLarge DyadicLargeCN DyadicSmallGlobal DyadicLargeEnergy

section Ring
variable {R : Type} (K : CRing R)

def E0 (a : Nat → Nat → R) (N : Nat) : R := sumN K (fun n => K.mul (a 0 (n + 1)) (a 0 (n + 1))) N

/-- Every mode of every time step is bounded by the initial energy. -/
theorem mode_bound (P : Cone K) (c k : Nat → R) (a : Nat → Nat → R) (h nu : R) (N : Nat)
    (hh : P.nn h) (hnu : P.nn nu) (hk : ∀ n, P.nn (k n))
    (h0 : ∀ t, a t 0 = K.zero) (hz : ∀ t n, N + 1 ≤ n → a t n = K.zero)
    (step : ∀ t n, K.add (a (t + 1) (n + 1)) (K.neg (a t (n + 1)))
      = K.mul h (K.add (K.neg (K.mul nu (K.mul (k (n + 1)) (K.add (a (t + 1) (n + 1)) (a t (n + 1))))))
          (dyN K c (fun i => K.add (a (t + 1) i) (a t i)) n))) :
    ∀ t n, Le K P (K.mul (a t n) (a t n)) (E0 K a N) := by
  have g := cn_global K P (fun t n => a t (n + 1)) (fun n => k (n + 1))
    (fun t n => dyN K c (fun i => K.add (a (t + 1) i) (a t i)) n) h nu N hh hnu (fun n => hk (n + 1)) step
    (fun t => dyadic_energy_neutral K c (fun i => K.add (a (t + 1) i) (a t i)) N
      (by show K.add (a (t + 1) 0) (a t 0) = K.zero; rw [h0, h0, K.zero_add])
      (by show K.add (a (t + 1) (N + 1)) (a t (N + 1)) = K.zero
          rw [hz (t + 1) (N + 1) (Nat.le_refl _), hz t (N + 1) (Nat.le_refl _), K.zero_add]))
  have hE0 : P.nn (E0 K a N) := sq_sum_nonneg K P (fun n => a 0 (n + 1)) N
  have zle : ∀ t n, a t n = K.zero → Le K P (K.mul (a t n) (a t n)) (E0 K a N) := by
    intro t n hn
    rw [hn, mul_zero K]
    exact ⟨E0 K a N, hE0, K.zero_add _⟩
  intro t n
  cases n with
  | zero => exact zle t 0 (h0 t)
  | succ m =>
    cases Nat.decLt m N with
    | isTrue hm => exact g.2 t m hm
    | isFalse hm => exact zle t (m + 1) (hz t (m + 1) (Nat.succ_le_succ (Nat.le_of_not_lt hm)))

theorem cn_large_data (O : OrdR K) (w c k M : Nat → R) (a : Nat → Nat → R) (h nu A : R) (n0 N : Nat)
    (hh : O.nn h) (hnu : O.nn nu) (hk : ∀ n, O.nn (k n)) (hw : ∀ n, O.nn (w n))
    (h0 : ∀ t, a t 0 = K.zero) (hz : ∀ t n, N + 1 ≤ n → a t n = K.zero)
    (step : ∀ t n, K.add (a (t + 1) (n + 1)) (K.neg (a t (n + 1)))
      = K.mul h (K.add (K.neg (K.mul nu (K.mul (k (n + 1)) (K.add (a (t + 1) (n + 1)) (a t (n + 1))))))
          (dyN K c (fun i => K.add (a (t + 1) i) (a t i)) n)))
    (hq : ∀ n, O.nn (qq K w c n))
    (hcrit : ∀ n, n0 ≤ n → K.mul (qq K w c n) (M n) = K.mul nu (K.mul (w n) (k n)))
    (hA2 : Le K O.toCone (four K (E0 K a N)) (K.mul A A)) (hAnn : O.nn A)
    (hM : ∀ n, n0 ≤ n → Le K O.toCone A (M n)) :
    ∀ t, Le K O.toCone (Ew K w (a t) N)
      (K.add (Ew K w (a 0) N) (sumN K (fun _ => K.mul h (headB K w c (four K (E0 K a N)) A n0 N)) t)) :=
  cn_large_energy K O w c k M a h nu A (E0 K a N) n0 N hh h0 (fun t => hz t (N + 1) (Nat.le_refl _)) step hq
    hcrit (mode_bound K O.toCone c k a h nu N hh hnu hk h0 hz step) hA2 hAnn hM
    (fun _ n => O.nn_mul _ _ hnu (O.nn_mul _ _ (hw n) (O.nn_mul _ _ (hk n) (O.nn_sq _))))

end Ring

end DyadicLargeData

#print axioms DyadicLargeData.mode_bound
#print axioms DyadicLargeData.cn_large_data

ns/DyadicLargeEnergy.lean

lean_all/ns/DyadicLargeEnergy.lean

/-
  ns/DyadicLargeEnergy.lean — T37 cycle 687 (goal G400): the per-step hypotheses of ns/DyadicLargeCN from one
  energy bound.

  Lean 4 core plus ns/DyadicLargeCN, ns/DyadicSmallGlobal (ordered ring OrdR), lib/RingTac.
    sum_sq_le        x² ≤ E, y² ≤ E ⇒ (x + y)² ≤ 4E          ((x+y)² + (x−y)² = 2x² + 2y²)
    shell_le         x² ≤ E, y² ≤ E, 4E ≤ A², A ≥ 0 ⇒ x + y ≤ A
    cn_large_energy  if every mode of every a_t has a_t(n)² ≤ E (e.g. E = E(a_0), NSChain.mode_le_energy with the
                     energy decay of the CN scheme), 4E ≤ A², and the critical threshold M_n ≥ A for n ≥ n₀, then
                     E_w(a_t) ≤ E_w(a_0) + Σ_{j<t} h·B with B = Σ_{n<N} [n < n₀] q_n (4E) A.
-/
import ns.DyadicLargeCN
import ns.DyadicSmallGlobal
import lib.RingTac

namespace DyadicLargeEnergy
open DetChain NSChain DyadicHs DyadicCN DyadicLarge DyadicLargeCN DyadicSmallGlobal

section Ring
variable {R : Type} (K : CRing R)

def four (E : R) : R := K.add (K.add E E) (K.add E E)

theorem sum_sq_le (P : Cone K) (x y E : R) (hx : Le K P (K.mul x x) E) (hy : Le K P (K.mul y y) E) :
    Le K P (K.mul (K.add x y) (K.add x y)) (four K E) := by
  have e : K.add (K.mul (K.add x y) (K.add x y)) (K.mul (K.add x (K.neg y)) (K.add x (K.neg y)))
      = K.add (K.add (K.mul x x) (K.mul y y)) (K.add (K.mul x x) (K.mul y y)) := by lib_ring
  have s1 : Le K P (K.mul (K.add x y) (K.add x y))
      (K.add (K.add (K.mul x x) (K.mul y y)) (K.add (K.mul x x) (K.mul y y))) :=
    ⟨_, P.nn_sq _, e⟩
  have s2 : Le K P (K.add (K.add (K.mul x x) (K.mul y y)) (K.add (K.mul x x) (K.mul y y))) (four K E) :=
    le_add' K P _ _ _ _ (le_add' K P _ _ _ _ hx hy) (le_add' K P _ _ _ _ hx hy)
  exact le_trans' K P _ _ _ s1 s2

theorem shell_le (O : OrdR K) (x y E A : R) (hx : Le K O.toCone (K.mul x x) E)
    (hy : Le K O.toCone (K.mul y y) E) (hA : Le K O.toCone (four K E) (K.mul A A)) (hAnn : O.nn A) :
    Le K O.toCone (K.add x y) A :=
  le_of_sq_le K O _ _ hAnn (le_trans' K O.toCone _ _ _ (sum_sq_le K O.toCone x y E hx hy) hA)

theorem cn_large_energy (O : OrdR K) (w c k M : Nat → R) (a : Nat → Nat → R) (h nu A E : R) (n0 N : Nat)
    (hh : O.nn h)
    (h0 : ∀ t, a t 0 = K.zero) (hN : ∀ t, a t (N + 1) = K.zero)
    (step : ∀ t n, K.add (a (t + 1) (n + 1)) (K.neg (a t (n + 1)))
      = K.mul h (K.add (K.neg (K.mul nu (K.mul (k (n + 1)) (K.add (a (t + 1) (n + 1)) (a t (n + 1))))))
          (dyN K c (fun i => K.add (a (t + 1) i) (a t i)) n)))
    (hq : ∀ n, O.nn (qq K w c n))
    (hcrit : ∀ n, n0 ≤ n → K.mul (qq K w c n) (M n) = K.mul nu (K.mul (w n) (k n)))
    (hmode : ∀ t n, Le K O.toCone (K.mul (a t n) (a t n)) E)
    (hA2 : Le K O.toCone (four K E) (K.mul A A)) (hAnn : O.nn A)
    (hM : ∀ n, n0 ≤ n → Le K O.toCone A (M n))
    (hD : ∀ t n, O.nn (K.mul nu (K.mul (w n) (K.mul (k n)
      (K.mul (K.add (a (t + 1) n) (a t n)) (K.add (a (t + 1) n) (a t n))))))) :
    ∀ t, Le K O.toCone (Ew K w (a t) N)
      (K.add (Ew K w (a 0) N) (sumN K (fun _ => K.mul h (headB K w c (four K E) A n0 N)) t)) :=
  have hA : ∀ t n, Le K O.toCone (K.add (a (t + 1) (n + 1)) (a t (n + 1))) A := fun t n =>
    shell_le K O _ _ E A (hmode (t + 1) (n + 1)) (hmode t (n + 1)) hA2 hAnn
  cn_large_global K O.toCone w c k M a h nu A (four K E) n0 N hh h0 hN step hq hcrit
    (fun t n hn => le_trans' K O.toCone _ _ _ (hA t n) (hM n hn)) hA hAnn
    (fun t n => sum_sq_le K O.toCone _ _ E (hmode (t + 1) n) (hmode t n)) hD

end Ring

end DyadicLargeEnergy

#print axioms DyadicLargeEnergy.sum_sq_le
#print axioms DyadicLargeEnergy.shell_le
#print axioms DyadicLargeEnergy.cn_large_energy

ns/DyadicModelB.lean

lean_all/ns/DyadicModelB.lean

/-
  ns/DyadicModelB.lean — T37 cycle 699 (goal G412): the constant of DyadicModelZ.model_bound made explicit.

  Lean 4 core plus ns/DyadicModelZ. For the model w_n = k_n = 4ⁿ, c_n = 2ⁿ, ν = 3 over ℤ, q_n = 3·8ⁿ and
    geom           7·Σ_{n<m} 8ⁿ + 1 = 8ᵐ
    head_le        B = Σ_{n<N} [n < m] q_n X ≤ 8ᵐ·X  for every N (X = 4E₀·A ≥ 0, m = A)
    model_explicit E_w(a_t) ≤ E_w(a_0) + Σ_{j<t} h·(8^A·(4E₀·A))   for integer CN data with 4E₀ ≤ A²,
  every truncation N and every time t.
-/
import ns.DyadicModelZ

namespace DyadicModelB
open DetChain NSChain DyadicSmallGlobal LibInt LibRing IntOrd DyadicHs DyadicCN DyadicLarge DyadicLargeCN
  DyadicLargeEnergy DyadicLargeData DyadicModelZ

def P8 (n : Nat) : Int := pw intR 8 n

theorem p8_eq : ∀ n, P4 n * P2 n = P8 n
  | 0 => rfl
  | n + 1 => by
    show intR.mul (P4 n) 4 * intR.mul (P2 n) 2 = intR.mul (P8 n) 8
    rw [← p8_eq n]
    show P4 n * 4 * (P2 n * 2) = P4 n * P2 n * 8
    lib_int

theorem nn_p8 (n : Nat) : NN (P8 n) := by
  rw [← p8_eq]; exact intCone.nn_mul _ _ (nn_p4 n) (nn_p2 n)

def S (m : Nat) : Int := sumN intR P8 m

theorem geom : ∀ m, 7 * S m + 1 = P8 m
  | 0 => rfl
  | m + 1 => by
    show 7 * (S m + P8 m) + 1 = P8 m * 8
    rw [← geom m]
    lib_int

theorem nn_S (m : Nat) : NN (S m) := sumN_nn intR intCone P8 nn_p8 m

theorem three_le (m : Nat) : Le intR intCone (3 * S m) (P8 m) :=
  ⟨4 * S m + 1, intCone.nn_add _ _ (intCone.nn_mul _ _ ⟨4, rfl⟩ (nn_S m)) ⟨1, rfl⟩,
    by show 3 * S m + (4 * S m + 1) = P8 m; rw [← geom m]; lib_int⟩

theorem sumN_ext_lt (f g : Nat → Int) : ∀ N, (∀ n, n < N → f n = g n) → sumN intR f N = sumN intR g N
  | 0, _ => rfl
  | N + 1, h => by
    show intR.add (sumN intR f N) (f N) = intR.add (sumN intR g N) (g N)
    rw [sumN_ext_lt f g N (fun n hn => h n (Nat.lt_succ_of_lt hn)), h N (Nat.lt_succ_self N)]

theorem mono (f : Nat → Int) (hf : ∀ n, NN (f n)) (N : Nat) :
    ∀ j, Le intR intCone (sumN intR f N) (sumN intR f (N + j))
  | 0 => le_refl' intR intCone _
  | j + 1 => le_trans' intR intCone _ _ _ (mono f hf N j) (le_of_nn_add intR intCone _ _ (hf (N + j)))

theorem head_le (E A : Int) (hE : NN E) (hA : NN A) (m N : Nat) :
    Le intR intCone (headB intR w c E A m N) ((E * A) * P8 m) := by
  let f : Nat → Int := fun n => if n < m then intR.mul (qq intR w c n) (intR.mul E A) else intR.zero
  have hX : NN (E * A) := intCone.nn_mul _ _ hE hA
  have hf : ∀ n, NN (f n) := by
    intro n
    cases Nat.decLt n m with
    | isTrue hl =>
      show NN (if n < m then _ else _); rw [ite_eq_left hl]
      exact intCone.nn_mul _ _ (nn_qq n) hX
    | isFalse hg =>
      show NN (if n < m then _ else _); rw [ite_eq_right hg]
      exact ⟨0, rfl⟩
  -- Σ_{n<N} f ≤ Σ_{n<m} f
  have s1 : Le intR intCone (sumN intR f N) (sumN intR f m) := by
    cases Nat.decLe N m with
    | isTrue hl =>
      cases le_dest hl with
      | intro j hj => rw [← hj]; exact mono f hf N j
    | isFalse hg =>
      cases le_dest (Nat.le_of_lt (Nat.lt_of_not_le hg)) with
      | intro j hj =>
        rw [← hj, head_stable intR (fun n => intR.mul (qq intR w c n) (intR.mul E A)) m j]
        exact le_refl' intR intCone _
  -- Σ_{n<m} f = (3·(E·A))·S m
  have s2 : sumN intR f m = intR.mul (3 * (E * A)) (S m) := by
    rw [sumN_ext_lt f (fun n => intR.mul (3 * (E * A)) (P8 n)) m (fun n hn => by
      show (if n < m then _ else _) = _
      rw [ite_eq_left hn, qq_eq, ← p8_eq]
      show 3 * (P4 n * P2 n) * (E * A) = 3 * (E * A) * (P4 n * P2 n)
      lib_int)]
    exact sumN_mul_left intR (3 * (E * A)) P8 m
  have s3 : Le intR intCone (intR.mul (3 * (E * A)) (S m)) ((E * A) * P8 m) := by
    have := le_mul_nn intR intCone (E * A) _ _ hX (three_le m)
    rw [show intR.mul (3 * (E * A)) (S m) = intR.mul (E * A) (3 * S m) by
      show 3 * (E * A) * S m = E * A * (3 * S m); lib_int]
    exact this
  rw [s2] at s1
  exact le_trans' intR intCone _ _ _ s1 s3

theorem model_explicit (a : Nat → Nat → Int) (h : Int) (m N : Nat) (hh : NN h)
    (h0 : ∀ t, a t 0 = 0) (hz : ∀ t n, N + 1 ≤ n → a t n = 0)
    (step : ∀ t n, intR.add (a (t + 1) (n + 1)) (intR.neg (a t (n + 1)))
      = intR.mul h (intR.add (intR.neg (intR.mul 3 (intR.mul (k (n + 1)) (intR.add (a (t + 1) (n + 1)) (a t (n + 1))))))
          (dyN intR c (fun i => intR.add (a (t + 1) i) (a t i)) n)))
    (hA2 : Le intR intCone (four intR (E0 intR a N)) (intR.mul (Int.ofNat m) (Int.ofNat m))) :
    ∀ t, Le intR intCone (Ew intR w (a t) N)
      (intR.add (Ew intR w (a 0) N)
        (sumN intR (fun _ => intR.mul h ((four intR (E0 intR a N) * Int.ofNat m) * P8 m)) t)) := by
  intro t
  have hE : NN (four intR (E0 intR a N)) := by
    have e0 : NN (E0 intR a N) := sq_sum_nonneg intR intCone (fun n => a 0 (n + 1)) N
    exact intCone.nn_add _ _ (intCone.nn_add _ _ e0 e0) (intCone.nn_add _ _ e0 e0)
  have hb := model_bound a h m N hh h0 hz step hA2 t
  have hs := sum_le intR intCone _ _ (fun _ => le_mul_nn intR intCone h _ _ hh
    (head_le (four intR (E0 intR a N)) (Int.ofNat m) hE ⟨m, rfl⟩ m N)) t
  exact le_trans' intR intCone _ _ _ hb (le_add' intR intCone _ _ _ _ (le_refl' intR intCone _) hs)

end DyadicModelB

#print axioms DyadicModelB.p8_eq
#print axioms DyadicModelB.nn_p8
#print axioms DyadicModelB.geom
#print axioms DyadicModelB.nn_S
#print axioms DyadicModelB.three_le
#print axioms DyadicModelB.sumN_ext_lt
#print axioms DyadicModelB.mono
#print axioms DyadicModelB.head_le
#print axioms DyadicModelB.model_explicit

ns/DyadicModelMode.lean

lean_all/ns/DyadicModelMode.lean

/-
  ns/DyadicModelMode.lean — T37 cycle 709 (goal G422): mode-by-mode decay from the uniform bound of DyadicModelU.

  Lean 4 core plus ns/DyadicModelU. For any ordered ring with cone P:
    term_le     f ≥ 0 termwise, n < N  ⇒  f n ≤ Σ_{j<N} f j
  and for the model w_n = k_n = 4ⁿ, c_n = 2ⁿ, ν = 3 over ℤ:
    mode_decay  E_w(a_0) ≤ W, 4E₀ ≤ F ≤ A², n < N, t ≤ T  ⇒  4^{n+1}·a_t(n+1)² ≤ W + T·h·F·A·8^A,
  one constant for every mode, every time up to T and every truncation: the shell amplitudes decay like 2^{-n}.
-/
import ns.DyadicModelU

namespace DyadicModelMode
open DetChain NSChain DyadicSmallGlobal LibInt LibRing IntOrd DyadicHs DyadicCN DyadicLarge DyadicLargeCN
  DyadicLargeEnergy DyadicLargeData DyadicModelZ DyadicModelB DyadicModelT DyadicModelU

section Ring
variable {R : Type} (K : CRing R)

theorem term_le (P : Cone K) (f : Nat → R) (hf : ∀ n, P.nn (f n)) (n : Nat) :
    ∀ N, n < N → Le K P (f n) (sumN K f N)
  | 0, h => absurd h (Nat.not_lt_zero n)
  | N + 1, h => by
    cases Nat.lt_or_ge n N with
    | inl l =>
      exact le_trans' K P _ _ _ (term_le P f hf n N l) (le_of_nn_add K P _ _ (hf N))
    | inr g =>
      have e : n = N := Nat.le_antisymm (Nat.le_of_lt_succ h) g
      rw [e]
      exact le_zero_add K P _ _ (sumN_nn K P f hf N)

end Ring

theorem mode_decay (a : Nat → Nat → Int) (h W F : Int) (m N T : Nat) (hh : NN h)
    (h0 : ∀ t, a t 0 = 0) (hz : ∀ t n, N + 1 ≤ n → a t n = 0)
    (step : ∀ t n, intR.add (a (t + 1) (n + 1)) (intR.neg (a t (n + 1)))
      = intR.mul h (intR.add (intR.neg (intR.mul 3 (intR.mul (k (n + 1)) (intR.add (a (t + 1) (n + 1)) (a t (n + 1))))))
          (dyN intR c (fun i => intR.add (a (t + 1) i) (a t i)) n)))
    (hW : Le intR intCone (Ew intR w (a 0) N) W)
    (hF : Le intR intCone (four intR (E0 intR a N)) F)
    (hFA : Le intR intCone F (intR.mul (Int.ofNat m) (Int.ofNat m)))
    (n t : Nat) (hn : n < N) (ht : t ≤ T) :
    Le intR intCone (intR.mul (w (n + 1)) (intR.mul (a t (n + 1)) (a t (n + 1))))
      (intR.add W (Int.ofNat T * (h * ((F * Int.ofNat m) * P8 m)))) :=
  le_trans' intR intCone _ _ _
    (term_le intR intCone (fun j => intR.mul (w (j + 1)) (intR.mul (a t (j + 1)) (a t (j + 1))))
      (fun j => intCone.nn_mul _ _ (nn_p4 (j + 1)) (nn_sq' _)) n N hn)
    (model_uniform a h W F m N T hh h0 hz step hW hF hFA t ht)

end DyadicModelMode

#print axioms DyadicModelMode.term_le
#print axioms DyadicModelMode.mode_decay

ns/DyadicModelSupp.lean

lean_all/ns/DyadicModelSupp.lean

/-
  ns/DyadicModelSupp.lean — T37 cycle 715 (goal G428): in the integer model the solution lives on finitely many
  shells, the same for every time t ≤ T and every truncation N.

  Lean 4 core plus ns/DyadicModelMode. For the model w_n = k_n = 4ⁿ, c_n = 2ⁿ, ν = 3 over ℤ:
    one_le_sq   a ≠ 0  ⇒  1 ≤ a²
    no_le_pred  ¬ (C + 1 ≤ C)
    contra, zero_of   4ⁿ·a² ≤ C < 4ⁿ  ⇒  a = 0
    support     C = W + T·h·F·A·8^A, C < 4^{n+1}, n < N, t ≤ T  ⇒  a_t(n+1) = 0
  Integrality turns the uniform weighted bound into a uniform cutoff: no energy reaches shells with 4ⁿ > C.
-/
import ns.DyadicModelMode

namespace DyadicModelSupp
open DetChain NSChain DyadicSmallGlobal LibInt LibRing IntOrd DyadicHs DyadicCN DyadicLarge DyadicLargeCN
  DyadicLargeEnergy DyadicLargeData DyadicModelZ DyadicModelB DyadicModelT DyadicModelU DyadicModelMode

theorem one_le_sq : ∀ a : Int, a ≠ 0 → Le intR intCone 1 (a * a)
  | Int.ofNat 0, h => absurd rfl h
  | Int.ofNat (k + 1), _ => by
    refine ⟨Int.ofNat (k * (k + 1) + k), ⟨_, rfl⟩, ?_⟩
    show Int.ofNat (1 + (k * (k + 1) + k)) = Int.ofNat ((k + 1) * (k + 1))
    have e : 1 + (k * (k + 1) + k) = (k + 1) * (k + 1) := by lib_nat
    rw [e]
  | Int.negSucc k, _ => by
    refine ⟨Int.ofNat (k * (k + 1) + k), ⟨_, rfl⟩, ?_⟩
    show Int.ofNat (1 + (k * (k + 1) + k)) = Int.ofNat ((k + 1) * (k + 1))
    have e : 1 + (k * (k + 1) + k) = (k + 1) * (k + 1) := by lib_nat
    rw [e]

theorem no_le_pred (C : Int) : ¬ Le intR intCone (C + 1) C := by
  intro h
  cases h with
  | intro d hd =>
    cases hd.1 with
    | intro n hn =>
      have e : d = -1 := by
        have h2 : C + 1 + d = C := hd.2
        have h3 : d = (C + 1 + d) + -(C + 1) := by lib_int
        rw [h3, h2]; lib_int
      rw [e] at hn
      exact Int.noConfusion hn

theorem contra (C x a : Int) (hx : NN x) (ha : a ≠ 0) (h1 : Le intR intCone (intR.mul x (intR.mul a a)) C)
    (h2 : Le intR intCone (C + 1) x) : False := by
  have l1 : Le intR intCone (intR.mul x 1) (intR.mul x (intR.mul a a)) :=
    le_mul_nn intR intCone x _ _ hx (one_le_sq a ha)
  have ex : intR.mul x 1 = x := by show x * 1 = x; lib_int
  rw [ex] at l1
  exact no_le_pred C (le_trans' intR intCone _ _ _ h2 (le_trans' intR intCone _ _ _ l1 h1))

theorem zero_of (C x : Int) (hx : NN x) : ∀ a : Int, Le intR intCone (intR.mul x (intR.mul a a)) C →
    Le intR intCone (C + 1) x → a = 0
  | Int.ofNat 0, _, _ => rfl
  | Int.ofNat (k + 1), h1, h2 => (contra C x _ hx (fun e => Nat.noConfusion (Int.ofNat.inj (e : Int.ofNat (k + 1) = Int.ofNat 0))) h1 h2).elim
  | Int.negSucc _, h1, h2 => (contra C x _ hx (fun e => Int.noConfusion e) h1 h2).elim

theorem support (a : Nat → Nat → Int) (h W F : Int) (m N T : Nat) (hh : NN h)
    (h0 : ∀ t, a t 0 = 0) (hz : ∀ t n, N + 1 ≤ n → a t n = 0)
    (step : ∀ t n, intR.add (a (t + 1) (n + 1)) (intR.neg (a t (n + 1)))
      = intR.mul h (intR.add (intR.neg (intR.mul 3 (intR.mul (k (n + 1)) (intR.add (a (t + 1) (n + 1)) (a t (n + 1))))))
          (dyN intR c (fun i => intR.add (a (t + 1) i) (a t i)) n)))
    (hW : Le intR intCone (Ew intR w (a 0) N) W)
    (hF : Le intR intCone (four intR (E0 intR a N)) F)
    (hFA : Le intR intCone F (intR.mul (Int.ofNat m) (Int.ofNat m)))
    (n t : Nat) (hn : n < N) (ht : t ≤ T)
    (hc : Le intR intCone (intR.add W (Int.ofNat T * (h * ((F * Int.ofNat m) * P8 m))) + 1) (P4 (n + 1))) :
    a t (n + 1) = 0 :=
  zero_of _ _ (nn_p4 (n + 1)) _ (mode_decay a h W F m N T hh h0 hz step hW hF hFA n t hn ht) hc

end DyadicModelSupp

#print axioms DyadicModelSupp.one_le_sq
#print axioms DyadicModelSupp.no_le_pred
#print axioms DyadicModelSupp.contra
#print axioms DyadicModelSupp.zero_of
#print axioms DyadicModelSupp.support

ns/DyadicModelT.lean

lean_all/ns/DyadicModelT.lean

/-
  ns/DyadicModelT.lean — T37 cycle 703 (goal G416): the explicit bound of DyadicModelB as a closed form
  and on a time horizon.

  Lean 4 core plus ns/DyadicModelB. For the model w_n = k_n = 4ⁿ, c_n = 2ⁿ, ν = 3 over ℤ with X = 4E₀·A·8^A,
    sumN_const     Σ_{j<t} Y = t·Y
    model_linear   E_w(a_t) ≤ E_w(a_0) + t·(h·X)
    model_horizon  t ≤ T  →  E_w(a_t) ≤ E_w(a_0) + T·(h·X)   (one bound for every t ≤ T and every truncation N).
-/
import ns.DyadicModelB

namespace DyadicModelT
open DetChain NSChain DyadicSmallGlobal LibInt LibRing IntOrd DyadicHs DyadicCN DyadicLarge DyadicLargeCN
  DyadicLargeEnergy DyadicLargeData DyadicModelZ DyadicModelB

theorem sumN_const (Y : Int) : ∀ t, sumN intR (fun _ => Y) t = Int.ofNat t * Y
  | 0 => by show (0 : Int) = 0 * Y; lib_int
  | t + 1 => by
    show intR.add (sumN intR (fun _ => Y) t) Y = (Int.ofNat t + 1) * Y
    rw [sumN_const Y t]; show Int.ofNat t * Y + Y = (Int.ofNat t + 1) * Y; lib_int

theorem nat_mul_le (Y : Int) (hY : NN Y) (t T : Nat) (h : t ≤ T) :
    Le intR intCone (Int.ofNat t * Y) (Int.ofNat T * Y) := by
  cases le_dest h with
  | intro d hd =>
    refine ⟨Int.ofNat d * Y, intCone.nn_mul _ _ ⟨d, rfl⟩ hY, ?_⟩
    rw [← hd]
    show Int.ofNat t * Y + Int.ofNat d * Y = (Int.ofNat t + Int.ofNat d) * Y
    lib_int

theorem nn_X (a : Nat → Nat → Int) (m N : Nat) :
    NN ((four intR (E0 intR a N) * Int.ofNat m) * P8 m) := by
  have e0 : NN (E0 intR a N) := sq_sum_nonneg intR intCone (fun n => a 0 (n + 1)) N
  have hE : NN (four intR (E0 intR a N)) :=
    intCone.nn_add _ _ (intCone.nn_add _ _ e0 e0) (intCone.nn_add _ _ e0 e0)
  exact intCone.nn_mul _ _ (intCone.nn_mul _ _ hE ⟨m, rfl⟩) (nn_p8 m)

theorem model_linear (a : Nat → Nat → Int) (h : Int) (m N : Nat) (hh : NN h)
    (h0 : ∀ t, a t 0 = 0) (hz : ∀ t n, N + 1 ≤ n → a t n = 0)
    (step : ∀ t n, intR.add (a (t + 1) (n + 1)) (intR.neg (a t (n + 1)))
      = intR.mul h (intR.add (intR.neg (intR.mul 3 (intR.mul (k (n + 1)) (intR.add (a (t + 1) (n + 1)) (a t (n + 1))))))
          (dyN intR c (fun i => intR.add (a (t + 1) i) (a t i)) n)))
    (hA2 : Le intR intCone (four intR (E0 intR a N)) (intR.mul (Int.ofNat m) (Int.ofNat m))) :
    ∀ t, Le intR intCone (Ew intR w (a t) N)
      (intR.add (Ew intR w (a 0) N) (Int.ofNat t * (h * ((four intR (E0 intR a N) * Int.ofNat m) * P8 m)))) := by
  intro t
  have hb := model_explicit a h m N hh h0 hz step hA2 t
  rw [sumN_const] at hb
  exact hb

theorem model_horizon (a : Nat → Nat → Int) (h : Int) (m N T : Nat) (hh : NN h)
    (h0 : ∀ t, a t 0 = 0) (hz : ∀ t n, N + 1 ≤ n → a t n = 0)
    (step : ∀ t n, intR.add (a (t + 1) (n + 1)) (intR.neg (a t (n + 1)))
      = intR.mul h (intR.add (intR.neg (intR.mul 3 (intR.mul (k (n + 1)) (intR.add (a (t + 1) (n + 1)) (a t (n + 1))))))
          (dyN intR c (fun i => intR.add (a (t + 1) i) (a t i)) n)))
    (hA2 : Le intR intCone (four intR (E0 intR a N)) (intR.mul (Int.ofNat m) (Int.ofNat m))) :
    ∀ t, t ≤ T → Le intR intCone (Ew intR w (a t) N)
      (intR.add (Ew intR w (a 0) N) (Int.ofNat T * (h * ((four intR (E0 intR a N) * Int.ofNat m) * P8 m)))) := by
  intro t ht
  exact le_trans' intR intCone _ _ _ (model_linear a h m N hh h0 hz step hA2 t)
    (le_add' intR intCone _ _ _ _ (le_refl' intR intCone _)
      (nat_mul_le _ (intCone.nn_mul _ _ hh (nn_X a m N)) t T ht))

end DyadicModelT

#print axioms DyadicModelT.sumN_const
#print axioms DyadicModelT.nat_mul_le
#print axioms DyadicModelT.nn_X
#print axioms DyadicModelT.model_linear
#print axioms DyadicModelT.model_horizon

ns/DyadicModelTail.lean

lean_all/ns/DyadicModelTail.lean

/-
  ns/DyadicModelTail.lean — T37 cycle 712 (goal G425): tightness — the L² energy beyond shell M is ≤ C/4^M,
  uniformly in the truncation and in t ≤ T.

  Lean 4 core plus ns/DyadicModelMode. For the model w_n = k_n = 4ⁿ, c_n = 2ⁿ, ν = 3 over ℤ:
    pow_mono    M ≤ n ⇒ 2^M ≤ 2^n
    p4_mono     M ≤ n ⇒ 4^M ≤ 4^n                                    (in the cone order of ℤ)
    tail_le     4^M · Σ_{j<N, M ≤ j+1} a(j+1)² ≤ E_w(a)
    tail_decay  4^M · Σ_{j<N, M ≤ j+1} a_t(j+1)² ≤ W + T·h·F·A·8^A   (t ≤ T, every N)
  The uniform tail bound is the compactness (tightness) input for N → ∞.
-/
import ns.DyadicModelMode

namespace DyadicModelTail
open DetChain NSChain DyadicSmallGlobal LibInt LibRing IntOrd DyadicHs DyadicCN DyadicLarge DyadicLargeCN
  DyadicLargeEnergy DyadicLargeData DyadicModelZ DyadicModelB DyadicModelT DyadicModelU DyadicModelMode

theorem pow_mono (M : Nat) : ∀ d, 2 ^ M ≤ 2 ^ (M + d)
  | 0 => Nat.le_refl _
  | d + 1 => by
    show 2 ^ M ≤ 2 ^ (M + d) * 2
    have e : 2 ^ (M + d) * 2 = 2 ^ (M + d) + 2 ^ (M + d) := by lib_nat
    rw [e]
    exact Nat.le_trans (pow_mono M d) (Nat.le_add_right _ _)

theorem p2_mono (M n : Nat) (h : M ≤ n) : Le intR intCone (P2 M) (P2 n) := by
  cases le_dest h with
  | intro d hd =>
    rw [p2_nat, p2_nat, ← hd]
    exact ofNat_le (pow_mono M d)

theorem p4_mono (M n : Nat) (h : M ≤ n) : Le intR intCone (P4 M) (P4 n) := by
  rw [p4_sq, p4_sq]
  exact le_trans' intR intCone _ _ _ (le_mul_nn intR intCone (P2 M) _ _ (nn_p2 M) (p2_mono M n h))
    (le_mul_r _ _ _ (nn_p2 n) (p2_mono M n h))

def tl (M : Nat) (b : Nat → Int) (j : Nat) : Int :=
  match Nat.ble M (j + 1) with
  | true => intR.mul (b (j + 1)) (b (j + 1))
  | false => 0

theorem term (M : Nat) (b : Nat → Int) (j : Nat) :
    Le intR intCone (intR.mul (P4 M) (tl M b j)) (intR.mul (w (j + 1)) (intR.mul (b (j + 1)) (b (j + 1)))) := by
  unfold tl
  cases e : Nat.ble M (j + 1) with
  | true =>
    have hm := Nat.le_of_ble_eq_true e
    show Le intR intCone (P4 M * intR.mul (b (j + 1)) (b (j + 1))) (P4 (j + 1) * intR.mul (b (j + 1)) (b (j + 1)))
    exact le_mul_r _ _ _ (nn_sq' _) (p4_mono M (j + 1) hm)
  | false =>
    have hx : NN (intR.mul (w (j + 1)) (intR.mul (b (j + 1)) (b (j + 1)))) :=
      intCone.nn_mul _ _ (nn_p4 (j + 1)) (nn_sq' _)
    refine ⟨_, hx, ?_⟩
    show P4 M * 0 + intR.mul (w (j + 1)) (intR.mul (b (j + 1)) (b (j + 1)))
      = intR.mul (w (j + 1)) (intR.mul (b (j + 1)) (b (j + 1)))
    generalize intR.mul (w (j + 1)) (intR.mul (b (j + 1)) (b (j + 1))) = z
    lib_int

theorem tail_le (M N : Nat) (b : Nat → Int) :
    Le intR intCone (intR.mul (P4 M) (sumN intR (tl M b) N)) (Ew intR w b N) := by
  rw [← sumN_mul_left]
  exact sum_le intR intCone _ _ (term M b) N

theorem tail_decay (a : Nat → Nat → Int) (h W F : Int) (m N T M : Nat) (hh : NN h)
    (h0 : ∀ t, a t 0 = 0) (hz : ∀ t n, N + 1 ≤ n → a t n = 0)
    (step : ∀ t n, intR.add (a (t + 1) (n + 1)) (intR.neg (a t (n + 1)))
      = intR.mul h (intR.add (intR.neg (intR.mul 3 (intR.mul (k (n + 1)) (intR.add (a (t + 1) (n + 1)) (a t (n + 1))))))
          (dyN intR c (fun i => intR.add (a (t + 1) i) (a t i)) n)))
    (hW : Le intR intCone (Ew intR w (a 0) N) W)
    (hF : Le intR intCone (four intR (E0 intR a N)) F)
    (hFA : Le intR intCone F (intR.mul (Int.ofNat m) (Int.ofNat m)))
    (t : Nat) (ht : t ≤ T) :
    Le intR intCone (intR.mul (P4 M) (sumN intR (tl M (a t)) N))
      (intR.add W (Int.ofNat T * (h * ((F * Int.ofNat m) * P8 m)))) :=
  le_trans' intR intCone _ _ _ (tail_le M N (a t)) (model_uniform a h W F m N T hh h0 hz step hW hF hFA t ht)

end DyadicModelTail

#print axioms DyadicModelTail.pow_mono
#print axioms DyadicModelTail.p2_mono
#print axioms DyadicModelTail.p4_mono
#print axioms DyadicModelTail.term
#print axioms DyadicModelTail.tail_le
#print axioms DyadicModelTail.tail_decay

ns/DyadicModelU.lean

lean_all/ns/DyadicModelU.lean

/-
  ns/DyadicModelU.lean — T37 cycle 706 (goal G419): the horizon bound of DyadicModelT uniform in the truncation.

  Lean 4 core plus ns/DyadicModelT. For the model w_n = k_n = 4ⁿ, c_n = 2ⁿ, ν = 3 over ℤ:
    le_mul_r       x ≤ y, c ≥ 0  ⇒  x·c ≤ y·c
    model_uniform  E_w(a_0) ≤ W, 4E₀ ≤ F ≤ A²  ⇒  E_w(a_t) ≤ W + T·h·F·A·8^A   for every t ≤ T,
  with W, F, A, T independent of the truncation N: the a priori bound needed to pass N → ∞.
-/
import ns.DyadicModelT

namespace DyadicModelU
open DetChain NSChain DyadicSmallGlobal LibInt LibRing IntOrd DyadicHs DyadicCN DyadicLarge DyadicLargeCN
  DyadicLargeEnergy DyadicLargeData DyadicModelZ DyadicModelB DyadicModelT

theorem le_mul_r (c x y : Int) (hc : NN c) (h : Le intR intCone x y) :
    Le intR intCone (x * c) (y * c) := by
  cases h with
  | intro d h =>
    refine ⟨d * c, intCone.nn_mul _ _ h.1 hc, ?_⟩
    rw [← h.2]
    show x * c + d * c = (x + d) * c
    lib_int

theorem model_uniform (a : Nat → Nat → Int) (h W F : Int) (m N T : Nat) (hh : NN h)
    (h0 : ∀ t, a t 0 = 0) (hz : ∀ t n, N + 1 ≤ n → a t n = 0)
    (step : ∀ t n, intR.add (a (t + 1) (n + 1)) (intR.neg (a t (n + 1)))
      = intR.mul h (intR.add (intR.neg (intR.mul 3 (intR.mul (k (n + 1)) (intR.add (a (t + 1) (n + 1)) (a t (n + 1))))))
          (dyN intR c (fun i => intR.add (a (t + 1) i) (a t i)) n)))
    (hW : Le intR intCone (Ew intR w (a 0) N) W)
    (hF : Le intR intCone (four intR (E0 intR a N)) F)
    (hFA : Le intR intCone F (intR.mul (Int.ofNat m) (Int.ofNat m))) :
    ∀ t, t ≤ T → Le intR intCone (Ew intR w (a t) N)
      (intR.add W (Int.ofNat T * (h * ((F * Int.ofNat m) * P8 m)))) := by
  intro t ht
  have hA2 := le_trans' intR intCone _ _ _ hF hFA
  have hb := model_horizon a h m N T hh h0 hz step hA2 t ht
  have hT : NN (Int.ofNat T) := ⟨T, rfl⟩
  have hX : Le intR intCone ((four intR (E0 intR a N) * Int.ofNat m) * P8 m) ((F * Int.ofNat m) * P8 m) :=
    le_mul_r _ _ _ (nn_p8 m) (le_mul_r _ _ _ ⟨m, rfl⟩ hF)
  have hY := le_mul_nn intR intCone (Int.ofNat T) _ _ hT (le_mul_nn intR intCone h _ _ hh hX)
  exact le_trans' intR intCone _ _ _ hb (le_add' intR intCone _ _ _ _ hW hY)

end DyadicModelU

#print axioms DyadicModelU.le_mul_r
#print axioms DyadicModelU.model_uniform

ns/DyadicModelZ.lean

lean_all/ns/DyadicModelZ.lean

/-
  ns/DyadicModelZ.lean — T37 cycle 690 (goal G403): a concrete supercritical dyadic model over ℤ with explicit n₀.

  Lean 4 core plus ns/IntOrd. Weights w_n = 4ⁿ (H¹), coupling c_n = 2ⁿ (λ = 2), dissipation k_n = 4ⁿ (α = 1),
  ν = 3. Then q_n = (w_{n+1} − w_n) c_n = 3·4ⁿ·2ⁿ, the critical threshold is M_n = 2ⁿ (q_n M_n = ν w_n k_n), and
  M_n ≥ A for every n ≥ n₀ = A. So for integer data with 4E₀ ≤ A²:
    model_bound   E_w(a_t) ≤ E_w(a_0) + Σ_{j<t} h·B,  B = Σ_{n<N} [n < A] q_n (4E₀) A,  every N, every t.
  (Powers are LibRing.pw intR, since core Int.pow lemmas need propext.)
-/
import ns.IntOrd

namespace DyadicModelZ
open DetChain NSChain DyadicSmallGlobal LibInt LibRing IntOrd DyadicHs DyadicCN DyadicLarge DyadicLargeCN
  DyadicLargeEnergy DyadicLargeData

def P2 (n : Nat) : Int := pw intR 2 n
def P4 (n : Nat) : Int := pw intR 4 n

theorem p2_nat : ∀ n, P2 n = Int.ofNat (2 ^ n)
  | 0 => rfl
  | n + 1 => by
    show intR.mul (P2 n) 2 = Int.ofNat (2 ^ n * 2)
    rw [p2_nat n]; rfl

theorem p4_sq : ∀ n, P4 n = P2 n * P2 n
  | 0 => rfl
  | n + 1 => by
    show intR.mul (P4 n) 4 = intR.mul (P2 n) 2 * intR.mul (P2 n) 2
    rw [p4_sq n]
    show P2 n * P2 n * 4 = P2 n * 2 * (P2 n * 2)
    lib_int

theorem nn_p2 (n : Nat) : NN (P2 n) := ⟨2 ^ n, p2_nat n⟩
theorem nn_p4 (n : Nat) : NN (P4 n) := by rw [p4_sq]; exact intCone.nn_mul _ _ (nn_p2 n) (nn_p2 n)

def w : Nat → Int := P4
def c : Nat → Int := P2
def k : Nat → Int := P4
def M : Nat → Int := P2

theorem qq_eq (n : Nat) : qq intR w c n = 3 * (P4 n * P2 n) := by
  show (intR.mul (P4 n) 4 + -P4 n) * P2 n = 3 * (P4 n * P2 n)
  show (P4 n * 4 + -P4 n) * P2 n = 3 * (P4 n * P2 n)
  lib_int

theorem crit (n : Nat) : intR.mul (qq intR w c n) (M n) = intR.mul 3 (intR.mul (w n) (k n)) := by
  rw [qq_eq]
  show 3 * (P4 n * P2 n) * P2 n = 3 * (P4 n * P4 n)
  rw [p4_sq n]
  lib_int

theorem nn_qq (n : Nat) : NN (qq intR w c n) := by
  rw [qq_eq]
  exact intCone.nn_mul _ _ ⟨3, rfl⟩ (intCone.nn_mul _ _ (nn_p4 n) (nn_p2 n))

theorem one_le_two_pow : ∀ n : Nat, 1 ≤ 2 ^ n
  | 0 => Nat.le_refl 1
  | n + 1 => by
    show 1 ≤ 2 ^ n * 2
    have e : 2 ^ n * 2 = 2 ^ n + 2 ^ n := by lib_nat
    rw [e]; exact Nat.le_trans (one_le_two_pow n) (Nat.le_add_right _ _)

theorem le_two_pow : ∀ n : Nat, n ≤ 2 ^ n
  | 0 => Nat.zero_le _
  | n + 1 => by
    show n + 1 ≤ 2 ^ n * 2
    have h1 : n + 1 ≤ 2 ^ n + 2 ^ n := Nat.add_le_add (le_two_pow n) (one_le_two_pow n)
    have e : 2 ^ n * 2 = 2 ^ n + 2 ^ n := by lib_nat
    rw [e]; exact h1

theorem le_dest {m p : Nat} (h : m ≤ p) : ∃ d, m + d = p := by
  induction h with
  | refl => exact ⟨0, rfl⟩
  | step _ ih =>
    cases ih with
    | intro d hd => exact ⟨d + 1, by rw [← hd]; rfl⟩

theorem ofNat_le {m p : Nat} (h : m ≤ p) : Le intR intCone (Int.ofNat m) (Int.ofNat p) := by
  cases le_dest h with
  | intro d hd => exact ⟨Int.ofNat d, ⟨d, rfl⟩, by rw [← hd]; rfl⟩

/-- The critical threshold M_n = 2ⁿ is ≥ A = m for every n ≥ m. -/
theorem thresh (m n : Nat) (hn : m ≤ n) : Le intR intCone (Int.ofNat m) (M n) := by
  show Le intR intCone _ (P2 n)
  rw [p2_nat]
  exact ofNat_le (Nat.le_trans hn (le_two_pow n))

theorem model_bound (a : Nat → Nat → Int) (h : Int) (m N : Nat) (hh : NN h)
    (h0 : ∀ t, a t 0 = 0) (hz : ∀ t n, N + 1 ≤ n → a t n = 0)
    (step : ∀ t n, intR.add (a (t + 1) (n + 1)) (intR.neg (a t (n + 1)))
      = intR.mul h (intR.add (intR.neg (intR.mul 3 (intR.mul (k (n + 1)) (intR.add (a (t + 1) (n + 1)) (a t (n + 1))))))
          (dyN intR c (fun i => intR.add (a (t + 1) i) (a t i)) n)))
    (hA2 : Le intR intCone (four intR (E0 intR a N)) (intR.mul (Int.ofNat m) (Int.ofNat m))) :
    ∀ t, Le intR intCone (Ew intR w (a t) N)
      (intR.add (Ew intR w (a 0) N)
        (sumN intR (fun _ => intR.mul h (headB intR w c (four intR (E0 intR a N)) (Int.ofNat m) m N)) t)) :=
  int_large_data w c k M a h 3 (Int.ofNat m) m N hh ⟨3, rfl⟩ nn_p4 nn_p4 h0 hz step nn_qq
    (fun n _ => crit n) hA2 ⟨m, rfl⟩ (fun n hn => thresh m n hn)

end DyadicModelZ

#print axioms DyadicModelZ.p2_nat
#print axioms DyadicModelZ.p4_sq
#print axioms DyadicModelZ.nn_p2
#print axioms DyadicModelZ.nn_p4
#print axioms DyadicModelZ.qq_eq
#print axioms DyadicModelZ.crit
#print axioms DyadicModelZ.nn_qq
#print axioms DyadicModelZ.one_le_two_pow
#print axioms DyadicModelZ.le_two_pow
#print axioms DyadicModelZ.le_dest
#print axioms DyadicModelZ.ofNat_le
#print axioms DyadicModelZ.thresh
#print axioms DyadicModelZ.model_bound

ns/DyadicScaling.lean

lean_all/ns/DyadicScaling.lean

/-
  ns/DyadicScaling.lean — T37 cycle 26: the exact scaling symmetry of the critical dyadic model.

  Lean 4 core plus NSChain (CRing, dyN). For c_{n+1} = 2c_n (c_n = 2ⁿ) and k_{n+1} = 2k_n
  (dissipation degree α = 1/2: k_n = 2ⁿ), the vector field of shell n+1,
      F_n(a) = −ν k_{n+1} a_{n+1} + c_n a_n² − c_{n+1} a_{n+1} a_{n+2},
  satisfies F_{n+1}(a) = 2 F_n(a ∘ succ): the shifted sequence b_n = a_{n+1} solves the same model with
  time dilated by 2, b_n(t) = a_{n+1}(t/2). Consequences (text): the amplitude sup_n |a_n| is the
  scale-invariant (critical) quantity, exactly the quantity of the small-data theorems
  (ns/DyadicHs.small_data, ns/DyadicSmallGlobal: every shell ≤ ν/3 resp. energy ≤ (ν/6)²); and the
  energy Σ a_n² is not invariant (it drops the first shell), i.e. supercritical, as in 3D NS.
    dyN_shift      dyN c a (n+1) = 2 · dyN c (a ∘ succ) n
    visc_shift     ν k_{n+2} a_{n+2} = 2 · ν k_{n+1} (a ∘ succ)_{n+1}
    field_shift    F_{n+1}(a) = 2 · F_n(a ∘ succ)
-/
import NSChain

namespace DyadicScaling
open DetChain NSChain

section Ring
variable {R : Type} (K : CRing R)

theorem dyN_shift (two : R) (c a : Nat → R) (hc : ∀ n, c (n + 1) = K.mul two (c n)) (n : Nat) :
    dyN K c a (n + 1) = K.mul two (dyN K c (fun i => a (i + 1)) n) := by
  unfold dyN
  rw [hc (n + 1), hc n, K.distrib, mul_neg' K, K.mul_assoc two, K.mul_assoc two]

def field (c k a : Nat → R) (nu : R) (n : Nat) : R :=
  K.add (K.neg (K.mul nu (K.mul (k (n + 1)) (a (n + 1))))) (dyN K c a n)

theorem field_shift (two : R) (c k a : Nat → R) (nu : R)
    (hc : ∀ n, c (n + 1) = K.mul two (c n)) (hk : ∀ n, k (n + 1) = K.mul two (k n)) (n : Nat) :
    field K c k a nu (n + 1) = K.mul two (field K c k (fun i => a (i + 1)) nu n) := by
  unfold field
  rw [dyN_shift K two c a hc n, K.distrib, mul_neg' K, hk (n + 1)]
  show K.add (K.neg (K.mul nu (K.mul (K.mul two (k (n + 1))) (a (n + 1 + 1)))))
      (K.mul two (dyN K c (fun i => a (i + 1)) n)) =
    K.add (K.neg (K.mul two (K.mul nu (K.mul (k (n + 1)) (a (n + 1 + 1))))))
      (K.mul two (dyN K c (fun i => a (i + 1)) n))
  rw [K.mul_assoc two (k (n + 1)), ← K.mul_assoc nu two, K.mul_comm nu two, K.mul_assoc two nu]

end Ring

end DyadicScaling

#print axioms DyadicScaling.dyN_shift
#print axioms DyadicScaling.field_shift

ns/DyadicSmallGlobal.lean

lean_all/ns/DyadicSmallGlobal.lean

/-
  ns/DyadicSmallGlobal.lean — T37 cycle 23: global small-data theorem for the discrete critical
  dyadic model.

  Lean 4 core plus NSChain (cn_global, dyadic_energy_neutral) and ns/DyadicCN (cn_hs_global).

  An ordered commutative ring (`OrdR`: a nonnegative cone with totality, antisymmetry and no zero
  divisors) is enough. For the Crank–Nicolson dyadic model on shells 1..N:
    le_of_sq_le          x² ≤ q², q ≥ 0 ⇒ x ≤ q
    small_data_global    if the initial energy is ≤ (M/2)² (M/2 = `half`) and M is the critical
                         threshold ((w_{n+1} − w_n) c_n M = ν w_n k_n), then for every time step t and
                         every truncation N:  E_w(a_t) ≤ E_w(a_0).
                         The energy bound (NSChain.cn_global) keeps every shell ≤ M/2 for all time, so
                         a_t + a_{t+1} ≤ M, and DyadicCN.cn_hs_global applies at every step.
  For λ = 2, s = 1, α = 1/2: initial energy ≤ (ν/6)² ⇒ the H¹ energy of every truncation never
  exceeds its initial value. (Large data at α = 1/2: measured, NS_MAP E4c.)
-/
import ns.DyadicCN

namespace DyadicSmallGlobal
open DetChain NSChain DyadicHs DyadicCN

section Ring
variable {R : Type} (K : CRing R)

structure OrdR (K : CRing R) extends Cone K where
  total : ∀ x y, Le K toCone x y ∨ Le K toCone y x
  antisym : ∀ x y, Le K toCone x y → Le K toCone y x → x = y
  noZD : ∀ x y, K.mul x y = K.zero → x = K.zero ∨ y = K.zero

theorem nn_sum_zero (O : OrdR K) (u v : R) (hu : O.nn u) (hv : O.nn v) (h : K.add u v = K.zero) :
    u = K.zero := by
  apply O.antisym
  · exact ⟨v, hv, h⟩
  · exact ⟨u, hu, K.zero_add u⟩

/-- x² ≤ q² with q ≥ 0 ⇒ x ≤ q. -/
theorem le_of_sq_le (O : OrdR K) (x q : R) (hq : O.nn q)
    (h : Le K O.toCone (K.mul x x) (K.mul q q)) : Le K O.toCone x q := by
  cases O.total x q with
  | inl hl => exact hl
  | inr hg =>
    cases hg with
    | intro d hd =>
      -- x = q + d, d ≥ 0;  x² = q² + d(q + q + d) ≥ q², and x² ≤ q² ⇒ d(q + q + d) = 0
      have ex : K.mul x x = K.add (K.mul q q) (K.mul d (K.add (K.add q q) d)) := by
        rw [← hd.2, right_distrib K, K.distrib, K.distrib, K.distrib, K.distrib]
        rw [K.mul_comm q d]
        generalize K.mul q q = A
        generalize K.mul d q = B
        generalize K.mul d d = C
        -- A + B + (B + C) = A + (B + B + C)
        rw [K.add_assoc A B, ← K.add_assoc B B C]
      have ge : Le K O.toCone (K.mul q q) (K.mul x x) :=
        ⟨_, O.nn_mul _ _ hd.1 (O.nn_add _ _ (O.nn_add _ _ hq hq) hd.1), ex.symm⟩
      have eq := O.antisym _ _ h ge
      have z : K.mul d (K.add (K.add q q) d) = K.zero := by
        apply add_cancel_left K (K.mul q q)
        rw [add_zero', ← ex, eq]
      have d0 : d = K.zero := by
        cases O.noZD _ _ z with
        | inl h0 => exact h0
        | inr h1 =>
          have := nn_sum_zero K O (K.add q q) d (O.nn_add _ _ hq hq) hd.1 h1
          rw [this, K.zero_add] at h1
          exact h1
      rw [← hd.2, d0, add_zero']
      exact le_refl' K O.toCone q

/-- **Global small data, critical dyadic model, every truncation.** -/
theorem small_data_global (O : OrdR K) (w c k : Nat → R) (a : Nat → Nat → R) (h nu M half : R) (N : Nat)
    (hh : O.nn h) (hnu : O.nn nu) (hk : ∀ n, O.nn (k n)) (hw : ∀ n, O.nn (w n)) (hhalf : O.nn half)
    (hM : K.add half half = M)
    (h0 : ∀ t, a t 0 = K.zero) (hz : ∀ t n, N ≤ n → a t (n + 1) = K.zero)
    (step : ∀ t n, K.add (a (t + 1) (n + 1)) (K.neg (a t (n + 1)))
      = K.mul h (K.add (K.neg (K.mul nu (K.mul (k (n + 1)) (K.add (a (t + 1) (n + 1)) (a t (n + 1))))))
          (dyN K c (fun i => K.add (a (t + 1) i) (a t i)) n)))
    (hpos : ∀ n, O.nn (K.mul (K.add (w (n + 1)) (K.neg (w n))) (c n)))
    (hcrit : ∀ n, K.mul (K.mul (K.add (w (n + 1)) (K.neg (w n))) (c n)) M = K.mul nu (K.mul (w n) (k n)))
    (hE0 : Le K O.toCone (sumN K (fun n => K.mul (a 0 (n + 1)) (a 0 (n + 1))) N) (K.mul half half)) :
    ∀ t, Le K O.toCone (Ew K w (a t) N) (Ew K w (a 0) N) := by
  have hN : ∀ t, a t (N + 1) = K.zero := fun t => hz t N (Nat.le_refl N)
  -- energy bound on the shifted sequence b t n = a t (n+1)
  have eb := cn_global K O.toCone (fun t n => a t (n + 1)) (fun n => k (n + 1))
    (fun t n => dyN K c (fun i => K.add (a (t + 1) i) (a t i)) n) h nu N hh hnu (fun n => hk (n + 1))
    (fun t n => step t n)
    (fun t => dyadic_energy_neutral K c (fun i => K.add (a (t + 1) i) (a t i)) N
      (by show K.add (a (t + 1) 0) (a t 0) = K.zero; rw [h0, h0, K.zero_add])
      (by show K.add (a (t + 1) (N + 1)) (a t (N + 1)) = K.zero; rw [hN, hN, K.zero_add]))
  -- every shell, every time: a ≤ half
  have shell : ∀ t n, Le K O.toCone (a t (n + 1)) half := by
    intro t n
    cases Nat.lt_or_ge n N with
    | inl hn =>
      exact le_of_sq_le K O _ half hhalf (le_trans' K O.toCone _ _ _ (eb.2 t n hn) hE0)
    | inr hn =>
      rw [hz t n hn]; exact ⟨half, hhalf, K.zero_add half⟩
  have hsmall : ∀ t n, Le K O.toCone (K.add (a (t + 1) (n + 1)) (a t (n + 1))) M := by
    intro t n
    cases shell (t + 1) n with
    | intro d1 h1 =>
      cases shell t n with
      | intro d2 h2 =>
        refine ⟨K.add d1 d2, O.nn_add _ _ h1.1 h2.1, ?_⟩
        rw [add_mid4 K, h1.2, h2.2, hM]
  exact cn_hs_global K O.toCone w c k a h nu M N hh h0 hN step hpos hcrit hsmall
    (fun t n => O.nn_mul _ _ hnu (O.nn_mul _ _ (hw n) (O.nn_mul _ _ (hk n) (O.nn_sq _))))

end Ring

end DyadicSmallGlobal

#print axioms DyadicSmallGlobal.nn_sum_zero
#print axioms DyadicSmallGlobal.le_of_sq_le
#print axioms DyadicSmallGlobal.small_data_global

ns/EnergyDecay.lean

lean_all/ns/EnergyDecay.lean

/-
  ns/EnergyDecay.lean — T37 cycle 170: exponential energy decay of every discrete run on T³ (mean zero).

  Lean 4 core plus lib/Gronwall (le, gronwall_hom, pw), lib/RingTac. If each step satisfies the discrete Leray
  inequality E_{k+1} + hν Z_k ≤ E_k and the Poincaré inequality E_k ≤ Z_k (all modes |k| ≥ 1; BiotSavart.
  poincare_mode per mode), and hν ≤ 1, then
      E_N ≤ (1 − hν)^N E_0 .
  The energy of every run decays at the viscous rate, whatever the nonlinearity does (it is energy-neutral):
  only the enstrophy can grow, which is where the regularity question sits.
-/
import lib.Gronwall

namespace EnergyDecay
open DetChain LibRing LibCS LibGron

variable {R : Type} (K : CRing R) (P : Pos K)

theorem decay_step (E E' Z hν : R) (hh : P.nn hν) (hstep : le K P (K.add E' (K.mul hν Z)) E)
    (hp : le K P E Z) : le K P E' (K.mul (sub K K.one hν) E) := by
  unfold le at hstep hp ⊢
  rw [show sub K (K.mul (sub K K.one hν) E) E'
      = K.add (sub K E (K.add E' (K.mul hν Z))) (K.mul hν (sub K Z E)) by lib_ring]
  exact P.nn_add _ _ hstep (P.nn_mul _ _ hh hp)

/-- **Exponential energy decay.** -/
theorem decay (E Z : Nat → R) (hν : R) (hh : P.nn hν) (h1 : P.nn (sub K K.one hν))
    (hstep : ∀ k, le K P (K.add (E (k + 1)) (K.mul hν (Z k))) (E k)) (hp : ∀ k, le K P (E k) (Z k)) (N : Nat) :
    le K P (E N) (K.mul (pw K (sub K K.one hν) N) (E 0)) :=
  gronwall_hom K P E (sub K K.one hν) h1 (fun k => decay_step K P _ _ _ hν hh (hstep k) (hp k)) N

end EnergyDecay

#print axioms EnergyDecay.decay_step
#print axioms EnergyDecay.decay

ns/EnergyStep.lean

lean_all/ns/EnergyStep.lean

/-
  ns/EnergyStep.lean — T37 cycle 1201 (goal G773): the discrete energy inequality of the implicit-viscosity scheme,
  over any commutative ring with a nonnegative cone (h, ν, w ≥ 0). With E = Σ|a|², Z = Σ w|a|² and a force that is
  energy-neutral at the old state, Σ_r a_r·N_r = 0 (GalerkinForce.proj_neutral):
    comp_energy   a'² + 2x·a'² ≤ a² + 2h·n·a'                        ((1+x)a' = a + hn, x = hνw)
    energy_step   E(a') + 2hν·Z(a') ≤ E(a) + 3h²·Σ|N|² + 2h²ν²·Σ w²|a|²
  (dot the step with a', ⟨a, a'⟩ ≤ (|a|² + |a'|²)/2, neutrality moves the force onto a' − a, Young and TimeLip).
  Summed over n steps of size h the error is O(h): the energy inequality E(t) + 2ν∫Z ≤ E(0) holds in the limit.

  Lean 4 core plus ns/TimeLip, ns/EnstDiff (sub3), ns/ForceBound (lsum_le), lib/RingTac.
-/
import ns.TimeLip

namespace EnergyStep
open DetChain NSChain LibRing DyadicLarge FourierGalerkin ForceBound EnstStep EnstDiff TimeLip

section Gen
variable {R : Type} (K : CRing R) (P : Cone K) {I : Type}

theorem comp_energy (h ν w a' a n : R) (hs : StepC K h ν w a' a n) :
    Le K P (K.add (K.mul a' a') (K.mul (K.add (K.mul (K.mul h ν) w) (K.mul (K.mul h ν) w)) (K.mul a' a')))
      (K.add (K.mul a a) (K.mul (K.add h h) (K.mul n a'))) := by
  unfold StepC at hs
  refine ⟨K.mul (K.add a (K.neg a')) (K.add a (K.neg a')), P.nn_sq _, ?_⟩
  -- 2(1+x)a'² = 2a a' + 2h n a'
  have e : K.mul (K.add K.one (K.mul (K.mul h ν) w)) (K.mul a' a') = K.add (K.mul a a') (K.mul h (K.mul n a')) := by
    calc _ = K.mul (K.mul (K.add K.one (K.mul (K.mul h ν) w)) a') a' := by lib_ring
      _ = K.mul (K.add a (K.mul h n)) a' := by rw [hs]
      _ = _ := by lib_ring
  generalize K.mul (K.mul h ν) w = x at e ⊢
  calc K.add (K.add (K.mul a' a') (K.mul (K.add x x) (K.mul a' a'))) (K.mul (K.add a (K.neg a')) (K.add a (K.neg a')))
      = K.add (K.add (K.mul a a) (K.neg (K.add (K.mul a a') (K.mul a a'))))
          (K.add (K.mul (K.add K.one x) (K.mul a' a')) (K.mul (K.add K.one x) (K.mul a' a'))) := by lib_ring
    _ = K.add (K.add (K.mul a a) (K.neg (K.add (K.mul a a') (K.mul a a'))))
          (K.add (K.add (K.mul a a') (K.mul h (K.mul n a'))) (K.add (K.mul a a') (K.mul h (K.mul n a')))) := by rw [e]
    _ = _ := by lib_ring

theorem mode_energy (h ν w : R) (a' a N : V3 R) (hs : Step K h ν w a' a N) :
    Le K P (K.add (dot3 K a' a') (K.mul (K.add (K.mul (K.mul h ν) w) (K.mul (K.mul h ν) w)) (dot3 K a' a')))
      (K.add (dot3 K a a) (K.mul (K.add h h) (dot3 K N a'))) := by
  have cx := comp_energy K P h ν w a'.x a.x N.x hs.1
  have cy := comp_energy K P h ν w a'.y a.y N.y hs.2.1
  have cz := comp_energy K P h ν w a'.z a.z N.z hs.2.2
  have s := le_add' K P _ _ _ _ (le_add' K P _ _ _ _ cx cy) cz
  refine le_trans' K P _ _ _ ⟨K.zero, P.nn_zero, ?_⟩ (le_trans' K P _ _ _ s ⟨K.zero, P.nn_zero, ?_⟩)
  · show K.add (K.add (K.add (K.add (K.mul a'.x a'.x) (K.mul a'.y a'.y)) (K.mul a'.z a'.z))
        (K.mul (K.add (K.mul (K.mul h ν) w) (K.mul (K.mul h ν) w)) (K.add (K.add (K.mul a'.x a'.x) (K.mul a'.y a'.y)) (K.mul a'.z a'.z)))) K.zero = _
    lib_ring
  · show _ = K.add (K.add (K.add (K.mul a.x a.x) (K.mul a.y a.y)) (K.mul a.z a.z))
      (K.mul (K.add h h) (K.add (K.add (K.mul N.x a'.x) (K.mul N.y a'.y)) (K.mul N.z a'.z)))
    lib_ring

/-- Young per mode: 2h⟨N, d⟩ ≤ h²|N|² + |d|² -/
theorem young3 (h : R) (N d : V3 R) :
    Le K P (K.mul (K.add h h) (dot3 K N d)) (K.add (K.mul (K.mul h h) (dot3 K N N)) (dot3 K d d)) := by
  refine ⟨K.add (K.add (K.mul (K.add (K.mul h N.x) (K.neg d.x)) (K.add (K.mul h N.x) (K.neg d.x)))
      (K.mul (K.add (K.mul h N.y) (K.neg d.y)) (K.add (K.mul h N.y) (K.neg d.y))))
      (K.mul (K.add (K.mul h N.z) (K.neg d.z)) (K.add (K.mul h N.z) (K.neg d.z))),
    P.nn_add _ _ (P.nn_add _ _ (P.nn_sq _) (P.nn_sq _)) (P.nn_sq _), ?_⟩
  show K.add (K.mul (K.add h h) (K.add (K.add (K.mul N.x d.x) (K.mul N.y d.y)) (K.mul N.z d.z))) _
    = K.add (K.mul (K.mul h h) (K.add (K.add (K.mul N.x N.x) (K.mul N.y N.y)) (K.mul N.z N.z)))
      (K.add (K.add (K.mul d.x d.x) (K.mul d.y d.y)) (K.mul d.z d.z))
  lib_ring

theorem energy_step (l : List I) (h ν : R) (hh : P.nn h) (hν : P.nn ν) (w : I → R) (hw : ∀ r, P.nn (w r))
    (a' a N : I → V3 R) (hs : ∀ r, Step K h ν (w r) (a' r) (a r) (N r))
    (hneut : lsum K (fun r => dot3 K (N r) (a r)) l = K.zero) :
    Le K P (K.add (lsum K (fun r => dot3 K (a' r) (a' r)) l) (K.mul (K.add (K.mul h ν) (K.mul h ν)) (lsum K (fun r => K.mul (w r) (dot3 K (a' r) (a' r))) l)))
      (K.add (lsum K (fun r => dot3 K (a r) (a r)) l)
        (K.add (K.mul (K.mul (K.add (K.add K.one K.one) K.one) (K.mul h h)) (lsum K (fun r => dot3 K (N r) (N r)) l))
          (K.mul (K.mul (K.add K.one K.one) (K.mul (K.mul h h) (K.mul ν ν))) (lsum K (fun r => K.mul (K.mul (w r) (w r)) (dot3 K (a r) (a r))) l)))) := by
  -- per-mode energy, summed
  have t := lsum_le K P _ _ (fun r => mode_energy K P h ν (w r) (a' r) (a r) (N r) (hs r)) l
  -- Σ⟨N, a'⟩ = Σ⟨N, a' − a⟩ by neutrality, then Young
  have y := lsum_le K P _ _ (fun r => young3 K P h (N r) (sub3 K (a' r) (a r))) l
  have tl := time_lip K P l h ν hh hν w hw a' a N hs
  have eN : lsum K (fun r => K.mul (K.add h h) (dot3 K (N r) (a' r))) l
      = K.add (lsum K (fun r => K.mul (K.add h h) (dot3 K (N r) (sub3 K (a' r) (a r)))) l)
          (K.mul (K.add h h) (lsum K (fun r => dot3 K (N r) (a r)) l)) := by
    rw [← lsum_mul_left, ← lsum_add]
    refine lsum_ext K _ _ (fun r => ?_) l
    show K.mul (K.add h h) (K.add (K.add (K.mul (N r).x (a' r).x) (K.mul (N r).y (a' r).y)) (K.mul (N r).z (a' r).z))
      = K.add (K.mul (K.add h h) (K.add (K.add (K.mul (N r).x (K.add (a' r).x (K.neg (a r).x))) (K.mul (N r).y (K.add (a' r).y (K.neg (a r).y))))
          (K.mul (N r).z (K.add (a' r).z (K.neg (a r).z)))))
        (K.mul (K.add h h) (K.add (K.add (K.mul (N r).x (a r).x) (K.mul (N r).y (a r).y)) (K.mul (N r).z (a r).z)))
    lib_ring
  rw [hneut] at eN
  have L1 : lsum K (fun r => K.add (dot3 K (a' r) (a' r)) (K.mul (K.add (K.mul (K.mul h ν) (w r)) (K.mul (K.mul h ν) (w r))) (dot3 K (a' r) (a' r)))) l
      = K.add (lsum K (fun r => dot3 K (a' r) (a' r)) l) (K.mul (K.add (K.mul h ν) (K.mul h ν)) (lsum K (fun r => K.mul (w r) (dot3 K (a' r) (a' r))) l)) := by
    rw [← lsum_mul_left, ← lsum_add]; exact lsum_ext K _ _ (fun r => by lib_ring) l
  have R1 : lsum K (fun r => K.add (dot3 K (a r) (a r)) (K.mul (K.add h h) (dot3 K (N r) (a' r)))) l
      = K.add (lsum K (fun r => dot3 K (a r) (a r)) l) (lsum K (fun r => K.mul (K.add h h) (dot3 K (N r) (a' r))) l) := by
    rw [lsum_add]
  rw [L1, R1, eN] at t
  have Y1 : lsum K (fun r => K.add (K.mul (K.mul h h) (dot3 K (N r) (N r))) (dot3 K (sub3 K (a' r) (a r)) (sub3 K (a' r) (a r)))) l
      = K.add (K.mul (K.mul h h) (lsum K (fun r => dot3 K (N r) (N r)) l)) (lsum K (fun r => dot3 K (sub3 K (a' r) (a r)) (sub3 K (a' r) (a r))) l) := by
    rw [← lsum_mul_left, lsum_add]
  rw [Y1] at y
  have two : P.nn (K.add K.one K.one) := P.nn_add _ _ (RatModel.nn_one' K P) (RatModel.nn_one' K P)
  have c1 := le_trans' K P _ _ _ y (le_add' K P _ _ _ _ (le_refl' K P _) tl)
  have c2 := le_add' K P _ _ _ _ (le_refl' K P (lsum K (fun r => dot3 K (a r) (a r)) l))
    (le_add' K P _ _ _ _ c1 (le_refl' K P (K.mul (K.add h h) K.zero)))
  refine le_trans' K P _ _ _ t (le_trans' K P _ _ _ c2 ⟨K.zero, P.nn_zero, ?_⟩)
  generalize lsum K (fun r => dot3 K (a r) (a r)) l = E
  generalize lsum K (fun r => dot3 K (N r) (N r)) l = G
  generalize lsum K (fun r => K.mul (K.mul (w r) (w r)) (dot3 K (a r) (a r))) l = A
  lib_ring

end Gen

end EnergyStep
#print axioms EnergyStep.comp_energy
#print axioms EnergyStep.mode_energy
#print axioms EnergyStep.young3
#print axioms EnergyStep.energy_step

ns/EnergySum.lean

lean_all/ns/EnergySum.lean

/-
  ns/EnergySum.lean — T37 cycle 1203 (goal G774): the accumulated discrete energy inequality, over any commutative ring
  with a nonnegative cone. If every step satisfies E_{k+1} + c·Z_{k+1} ≤ E_k + s_k with s_k ≤ S, then
    energy_sum   E_n + c·Σ_{k<n} Z_{k+1} ≤ E_0 + n·S.
  With EnergyStep (c = 2hν, s_k = 3h²Σ|N_k|² + 2h²ν²Σw²|a_k|², S = h²(3G + 2ν²A) from ForceNorms/EnstH2):
  E(nh) + 2ν·hΣZ ≤ E(0) + (nh)·h·(3G + 2ν²A) — the energy inequality with an O(h) error on [0, nh].

  Lean 4 core plus NSChain (sumN), lib/RingTac.
-/
import ns.TimeLipN

namespace EnergySum
open DetChain NSChain LibRing DyadicLarge FourierGalerkin ForceBound

section Gen
variable {R : Type} (K : CRing R) (P : Cone K)

theorem energy_sum (E Z s : Nat → R) (c S : R) (hstep : ∀ k, Le K P (K.add (E (k + 1)) (K.mul c (Z (k + 1)))) (K.add (E k) (s k)))
    (hs : ∀ k, Le K P (s k) S) : ∀ n,
    Le K P (K.add (E n) (K.mul c (sumN K (fun k => Z (k + 1)) n))) (K.add (E 0) (K.mul (castN K n) S))
  | 0 => ⟨K.zero, P.nn_zero, by show K.add (K.add (E 0) (K.mul c K.zero)) K.zero = K.add (E 0) (K.mul K.zero S); lib_ring⟩
  | n + 1 => by
    have ih := energy_sum E Z s c S hstep hs n
    have st := hstep n
    -- E_{n+1} + c(ΣZ_n + Z_{n+1}) ≤ E_n + s_n + cΣZ_n ≤ E_0 + nS + s_n ≤ E_0 + (n+1)S
    have a := le_add' K P _ _ _ _ st (le_refl' K P (K.mul c (sumN K (fun k => Z (k + 1)) n)))
    have b := le_add' K P _ _ _ _ ih (hs n)
    refine le_trans' K P _ _ _ ⟨K.zero, P.nn_zero, ?_⟩ (le_trans' K P _ _ _ a (le_trans' K P _ _ _ ⟨K.zero, P.nn_zero, ?_⟩
      (le_trans' K P _ _ _ b ⟨K.zero, P.nn_zero, ?_⟩)))
    · show K.add (K.add (E (n + 1)) (K.mul c (K.add (sumN K (fun k => Z (k + 1)) n) (Z (n + 1))))) K.zero = _
      lib_ring
    · lib_ring
    · show K.add (K.add (K.add (E 0) (K.mul (castN K n) S)) S) K.zero = K.add (E 0) (K.mul (K.add K.one (castN K n)) S)
      lib_ring

end Gen

end EnergySum

#print axioms EnergySum.energy_sum

ns/EnstBound.lean

lean_all/ns/EnstBound.lean

/-
  ns/EnstBound.lean — T37 cycle 985 (goal G643, last part): the enstrophy bound in closed form, for every step size.
  For the Leray-projected implicit-viscosity Galerkin scheme (EnstProj.enst_proj), with c = 135ι²:
    c·(Z_0 + hν A_0) ≤ ν²   ⇒   Z_t ≤ Z_0 + hν A_0   for every t,
  uniformly in the truncation; as h → 0 (H² data, A_0 fixed) the condition becomes 135ι² Z_0 < ν² and the bound
  Z_t ≤ Z_0 + o(1) — the discrete form of the classical small-enstrophy global bound in 3D.

  Lean 4 core plus ns/EnstProj, ns/EnstGlobal (le_of_mul_le).
-/
import ns.EnstProj

namespace EnstBound
open DetChain NSChain DyadicSmallGlobal LibRing DyadicLarge FourierGalerkin GalerkinForce ForceBound ForceSob
  ModeCount CubeCount ForceUniform NormCompare EnstStep EnstGlobal EnstGalerkin LerayProj EnstProj

section Gen
variable {R : Type} (K : CRing R) (O : OrdR K) {I : Type}
variable (inv : R → R) (mul_inv : ∀ y, y ≠ K.zero → K.mul y (inv y) = K.one) (h10 : K.one ≠ K.zero)

include mul_inv h10 in
theorem enst_bound (l : List I) (kz : I → Mode) (hd : List.Pairwise (fun a b => kz a ≠ kz b) l)
    (h1 : ∀ p, 1 ≤ nrm (kz p)) (M : Nat) (hM : ∀ p, p ∈ l → nrm (kz p) ≤ M + 1)
    (ι h ν : R) (hh : O.nn h) (hν : O.nn ν) (hν0 : ν ≠ K.zero) (a : Nat → I → V3 R)
    (κ : I → R) (hκn : ∀ r, O.nn (κ r)) (hκ : ∀ r, K.mul (dot3 K (kv K kz r) (kv K kz r)) (κ r) = K.one)
    (hs : ∀ t r, Step K h ν (wt K kz r) (a (t + 1) r) (a t r)
      (proj K (κ r) (kv K kz r) (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (a t) (a t) r)))
    (small : Le K O.toCone (K.mul (K.mul (castN K 135) (K.mul ι ι))
      (K.add (Zs K l kz a 0) (K.mul (K.mul h ν) (As K l kz a 0)))) (K.mul ν ν))
    (t : Nat) :
    Le K O.toCone (Zs K l kz a t) (K.add (Zs K l kz a 0) (K.mul (K.mul h ν) (As K l kz a 0))) := by
  have hQ0 : Q K ν h (Zs K l kz a) (As K l kz a) 0 = K.mul ν (K.add (Zs K l kz a 0) (K.mul (K.mul h ν) (As K l kz a 0))) := by
    show K.add (K.mul ν (Zs K l kz a 0)) (K.mul (K.mul h (K.mul ν ν)) (As K l kz a 0)) = _
    lib_ring
  have sm : Le K O.toCone (K.mul (K.mul (castN K 135) (K.mul ι ι)) (Q K ν h (Zs K l kz a) (As K l kz a) 0))
      (K.mul ν (K.mul ν ν)) := by
    rw [hQ0]
    have m := le_mul_nn K O.toCone ν _ _ hν small
    have e : K.mul ν (K.mul (K.mul (castN K 135) (K.mul ι ι)) (K.add (Zs K l kz a 0) (K.mul (K.mul h ν) (As K l kz a 0))))
        = K.mul (K.mul (castN K 135) (K.mul ι ι)) (K.mul ν (K.add (Zs K l kz a 0) (K.mul (K.mul h ν) (As K l kz a 0)))) := by
      lib_ring
    rw [e] at m; exact m
  have q := enst_proj K O inv mul_inv h10 l kz hd h1 M hM ι h ν hh hν hν0 a κ hκn hκ hs sm t
  rw [hQ0] at q
  have hA : O.nn (As K l kz a t) :=
    lsum_nn K O.toCone _ (fun r => O.nn_mul _ _ (O.nn_sq _) (dot3_nn K O.toCone _)) l
  have hz : Le K O.toCone (K.mul ν (Zs K l kz a t)) (Q K ν h (Zs K l kz a) (As K l kz a) t) :=
    ⟨K.mul (K.mul h (K.mul ν ν)) (As K l kz a t), O.nn_mul _ _ (O.nn_mul _ _ hh (O.nn_sq ν)) hA, rfl⟩
  exact le_of_mul_le K O ν _ _ hν hν0 (le_trans' K O.toCone _ _ _ hz q)

end Gen

end EnstBound

#print axioms EnstBound.enst_bound

ns/EnstDiff.lean

lean_all/ns/EnstDiff.lean

/-
  ns/EnstDiff.lean — T37 cycle 1029 (goal G671, piece a): the implicit-viscosity step is linear, so the difference of
  two solutions obeys the same step with the force difference:
    step_sub   Step(a', a, N_a), Step(b', b, N_b)  ⇒  Step(a' − b', a − b, N_a − N_b)
  (componentwise (1 + hνw)(a' − b') = (a − b) + h(N_a − N_b)). Then EnstStep.enst_step bounds the enstrophy of the
  difference by the force difference, which ForceBilin.Nl_diff splits as N(δ, a) + N(b, δ).

  Lean 4 core plus ns/EnstStep, ns/FourierGalerkin (add3, neg3).
-/
import ns.EnstStep

namespace EnstDiff
open DetChain NSChain LibRing FourierGalerkin EnstStep

section Gen
variable {R : Type} (K : CRing R)

def sub3 (X Y : V3 R) : V3 R := add3 K X (neg3 K Y)

theorem stepc_sub (h ν w a' a n b' b m : R) (ha : StepC K h ν w a' a n) (hb : StepC K h ν w b' b m) :
    StepC K h ν w (K.add a' (K.neg b')) (K.add a (K.neg b)) (K.add n (K.neg m)) := by
  unfold StepC at *
  have e : K.mul (K.add K.one (K.mul (K.mul h ν) w)) (K.add a' (K.neg b'))
      = K.add (K.mul (K.add K.one (K.mul (K.mul h ν) w)) a') (K.neg (K.mul (K.add K.one (K.mul (K.mul h ν) w)) b')) := by
    lib_ring
  rw [e, ha, hb]; lib_ring

theorem step_sub (h ν w : R) (a' a Na b' b Nb : V3 R) (ha : Step K h ν w a' a Na) (hb : Step K h ν w b' b Nb) :
    Step K h ν w (sub3 K a' b') (sub3 K a b) (sub3 K Na Nb) :=
  ⟨stepc_sub K h ν w _ _ _ _ _ _ ha.1 hb.1, stepc_sub K h ν w _ _ _ _ _ _ ha.2.1 hb.2.1,
   stepc_sub K h ν w _ _ _ _ _ _ ha.2.2 hb.2.2⟩

end Gen

end EnstDiff

#print axioms EnstDiff.stepc_sub
#print axioms EnstDiff.step_sub

ns/EnstDiss.lean

lean_all/ns/EnstDiss.lean

/-
  ns/EnstDiss.lean — T37 cycle 1323 (goal G792a): the dissipated H² mass in the small-data regime, over an ordered
  field. Sequences Z_t, A_t ≥ 0, ν > 0, h, c ≥ 0, Q_t = νZ_t + hν²A_t with the enstrophy step
      Q_{t+1} ≤ νZ_t + h·c·A_t·Z_t
  (EnstStep with Σ|N|² ≤ c·A·Z). If 2c·Q_0 ≤ ν³ then
    diss_step   2Q_{t+1} + hν²A_t ≤ 2Q_t
    enst_diss   2Q_n + hν²·Σ_{t<n} A_t ≤ 2Q_0,
  so Σ_t h·A_t ≤ 2Q_0/ν²: the time-integrated H² norm is bounded (by a constant independent of the truncation once
  c = 135ι²), and a pigeonhole (EventSmall.pigeon) yields a time where A is small.

  Lean 4 core plus ns/EnstGlobal (Q, q_le, le_of_mul_le), ns/RatModel, lib/RingTac.
-/
import ns.EnstGlobal
import ns.RatModel

namespace EnstDiss
open DetChain NSChain DyadicSmallGlobal LibRing DyadicLarge EnstGlobal

section Gen
variable {R : Type} (K : CRing R) (O : OrdR K)

theorem diss_step (ν h c : R) (Z A : Nat → R) (hν : O.nn ν) (hν0 : ν ≠ K.zero) (hh : O.nn h) (hc : O.nn c)
    (hZ : ∀ t, O.nn (Z t)) (hA : ∀ t, O.nn (A t))
    (step : ∀ t, Le K O.toCone (Q K ν h Z A (t + 1)) (K.add (K.mul ν (Z t)) (K.mul (K.mul h c) (K.mul (A t) (Z t)))))
    (small : Le K O.toCone (K.mul (K.add c c) (Q K ν h Z A 0)) (K.mul ν (K.mul ν ν))) (t : Nat) :
    Le K O.toCone (K.add (K.mul (K.add K.one K.one) (Q K ν h Z A (t + 1))) (K.mul (K.mul h (K.mul ν ν)) (A t)))
      (K.mul (K.add K.one K.one) (Q K ν h Z A t)) := by
  have nQ0 : O.nn (Q K ν h Z A 0) :=
    O.nn_add _ _ (O.nn_mul _ _ hν (hZ 0)) (O.nn_mul _ _ (O.nn_mul _ _ hh (O.nn_sq ν)) (hA 0))
  -- c·Q_0 ≤ 2c·Q_0 ≤ ν³
  have small1 : Le K O.toCone (K.mul c (Q K ν h Z A 0)) (K.mul ν (K.mul ν ν)) :=
    le_trans' K O.toCone _ _ _ ⟨K.mul c (Q K ν h Z A 0), O.nn_mul _ _ hc nQ0, by lib_ring⟩ small
  have hq := q_le K O ν h c Z A hν hν0 hh hc hZ hA step small1 t
  -- ν·(2c·Z_t) ≤ ν·ν²
  have hz : Le K O.toCone (K.mul ν (Z t)) (Q K ν h Z A t) :=
    ⟨K.mul (K.mul h (K.mul ν ν)) (A t), O.nn_mul _ _ (O.nn_mul _ _ hh (O.nn_sq ν)) (hA t), rfl⟩
  have h2 := le_trans' K O.toCone _ _ _
    (le_mul_nn K O.toCone (K.add c c) _ _ (O.nn_add _ _ hc hc) (le_trans' K O.toCone _ _ _ hz hq)) small
  have e : K.mul (K.add c c) (K.mul ν (Z t)) = K.mul ν (K.mul (K.add c c) (Z t)) := by lib_ring
  rw [e] at h2
  have hcz := le_of_mul_le K O ν _ _ hν hν0 h2
  -- h·A_t·(2cZ_t) ≤ h·A_t·ν²
  have h4 := le_mul_nn K O.toCone (K.mul h (A t)) _ _ (O.nn_mul _ _ hh (hA t)) hcz
  have st := le_add' K O.toCone _ _ _ _ (le_mul_nn K O.toCone (K.add K.one K.one) _ _
    (O.nn_add _ _ (RatModel.nn_one' K O.toCone) (RatModel.nn_one' K O.toCone)) (step t))
    (le_refl' K O.toCone (K.mul (K.mul h (K.mul ν ν)) (A t)))
  refine le_trans' K O.toCone _ _ _ st ?_
  refine le_trans' K O.toCone _ _ _ ⟨K.zero, O.nn_zero, ?_⟩
    (le_trans' K O.toCone _ _ _ (le_add' K O.toCone _ _ _ _ (le_refl' K O.toCone (K.add (K.add (K.mul ν (Z t)) (K.mul ν (Z t))) (K.mul (K.mul h (K.mul ν ν)) (A t)))) h4)
      ⟨K.zero, O.nn_zero, ?_⟩)
  · show K.add (K.add (K.mul (K.add K.one K.one) (K.add (K.mul ν (Z t)) (K.mul (K.mul h c) (K.mul (A t) (Z t)))))
        (K.mul (K.mul h (K.mul ν ν)) (A t))) K.zero
      = K.add (K.add (K.add (K.mul ν (Z t)) (K.mul ν (Z t))) (K.mul (K.mul h (K.mul ν ν)) (A t)))
          (K.mul (K.mul h (A t)) (K.mul (K.add c c) (Z t)))
    lib_ring
  · show K.add (K.add (K.add (K.add (K.mul ν (Z t)) (K.mul ν (Z t))) (K.mul (K.mul h (K.mul ν ν)) (A t)))
        (K.mul (K.mul h (A t)) (K.mul ν ν))) K.zero
      = K.mul (K.add K.one K.one) (K.add (K.mul ν (Z t)) (K.mul (K.mul h (K.mul ν ν)) (A t)))
    lib_ring

theorem enst_diss (ν h c : R) (Z A : Nat → R) (hν : O.nn ν) (hν0 : ν ≠ K.zero) (hh : O.nn h) (hc : O.nn c)
    (hZ : ∀ t, O.nn (Z t)) (hA : ∀ t, O.nn (A t))
    (step : ∀ t, Le K O.toCone (Q K ν h Z A (t + 1)) (K.add (K.mul ν (Z t)) (K.mul (K.mul h c) (K.mul (A t) (Z t)))))
    (small : Le K O.toCone (K.mul (K.add c c) (Q K ν h Z A 0)) (K.mul ν (K.mul ν ν))) :
    ∀ n, Le K O.toCone (K.add (K.mul (K.add K.one K.one) (Q K ν h Z A n)) (K.mul (K.mul h (K.mul ν ν)) (sumN K A n)))
      (K.mul (K.add K.one K.one) (Q K ν h Z A 0))
  | 0 => ⟨K.zero, O.nn_zero, by show K.add (K.add (K.mul (K.add K.one K.one) (Q K ν h Z A 0)) (K.mul (K.mul h (K.mul ν ν)) K.zero)) K.zero = _; lib_ring⟩
  | n + 1 => by
    have ih := enst_diss ν h c Z A hν hν0 hh hc hZ hA step small n
    have ds := diss_step K O ν h c Z A hν hν0 hh hc hZ hA step small n
    have s := le_add' K O.toCone _ _ _ _ ds (le_refl' K O.toCone (K.mul (K.mul h (K.mul ν ν)) (sumN K A n)))
    refine le_trans' K O.toCone _ _ _ ⟨K.zero, O.nn_zero, ?_⟩ (le_trans' K O.toCone _ _ _ s ih)
    show K.add (K.add (K.mul (K.add K.one K.one) (Q K ν h Z A (n + 1))) (K.mul (K.mul h (K.mul ν ν)) (K.add (sumN K A n) (A n)))) K.zero
      = K.add (K.add (K.mul (K.add K.one K.one) (Q K ν h Z A (n + 1))) (K.mul (K.mul h (K.mul ν ν)) (A n)))
          (K.mul (K.mul h (K.mul ν ν)) (sumN K A n))
    lib_ring

end Gen

end EnstDiss

#print axioms EnstDiss.diss_step
#print axioms EnstDiss.enst_diss

ns/EnstExp.lean

lean_all/ns/EnstExp.lean

/-
  ns/EnstExp.lean — T37 cycle 1337 (goal G796 — done): exponential decay of the enstrophy functional in the
  small-data regime, over an ordered field. With Q_t = νZ_t + hν²A_t, the step Q_{t+1} ≤ νZ_t + h·c·A_t·Z_t,
  2c·Q_0 ≤ ν³ and Z_t ≤ A_t (|k| ≥ 1):
    exp_step    (2 + 2hν)·Q_{t+1} ≤ (2 + hν)·Q_t        (witness hν²(A − Z) + (1 + hν)·s from EnstDiss.diss_step)
    enst_exp    (2 + 2hν)ⁿ·Q_n ≤ (2 + hν)ⁿ·Q_0
    proj_exp    the same for the implicit Fourier–Galerkin scheme with Leray-projected force (EventH2.proj_step,
                EnstH2.Z_le_A), c = 135ι², constants independent of the truncation.
  So after the EventRegU time (where 2cQ ≤ ν³ can be reached by doubling the waiting time) the enstrophy decays at a
  rate ≈ ν/2 per unit time.

  Lean 4 core plus ns/EnstDiss, ns/EventH2 (proj_step), ns/EnstH2 (Z_le_A), ns/CFLDecay (pw_nn).
-/
import ns.EnstDiss
import ns.EventH2
import ns.CFLDecay

namespace EnstExp
open DetChain NSChain DyadicSmallGlobal LibRing DyadicLarge FourierGalerkin GalerkinForce ForceBound ForceSob
  ModeCount CubeCount ForceUniform NormCompare EnstStep EnstGalerkin EnstGlobal

section Gen
variable {R : Type} (K : CRing R) (O : OrdR K) {I : Type}
variable (inv : R → R) (mul_inv : ∀ y, y ≠ K.zero → K.mul y (inv y) = K.one) (h10 : K.one ≠ K.zero)

theorem exp_step (ν h c : R) (Z A : Nat → R) (hν : O.nn ν) (hν0 : ν ≠ K.zero) (hh : O.nn h) (hc : O.nn c)
    (hZ : ∀ t, O.nn (Z t)) (hA : ∀ t, O.nn (A t)) (hZA : ∀ t, Le K O.toCone (Z t) (A t))
    (step : ∀ t, Le K O.toCone (Q K ν h Z A (t + 1)) (K.add (K.mul ν (Z t)) (K.mul (K.mul h c) (K.mul (A t) (Z t)))))
    (small : Le K O.toCone (K.mul (K.add c c) (Q K ν h Z A 0)) (K.mul ν (K.mul ν ν))) (t : Nat) :
    Le K O.toCone (K.mul (K.add (K.add K.one K.one) (K.add (K.mul h ν) (K.mul h ν))) (Q K ν h Z A (t + 1)))
      (K.mul (K.add (K.add K.one K.one) (K.mul h ν)) (Q K ν h Z A t)) := by
  have ds := EnstDiss.diss_step K O ν h c Z A hν hν0 hh hc hZ hA step small t
  have hhν : O.nn (K.mul h ν) := O.nn_mul _ _ hh hν
  cases ds with
  | intro s hs =>
    cases hZA t with
    | intro p hp =>
      refine ⟨K.add (K.mul (K.mul h (K.mul ν ν)) p) (K.mul (K.add K.one (K.mul h ν)) s),
        O.nn_add _ _ (O.nn_mul _ _ (O.nn_mul _ _ hh (O.nn_sq ν)) hp.1)
          (O.nn_mul _ _ (O.nn_add _ _ (RatModel.nn_one' K O.toCone) hhν) hs.1), ?_⟩
      have eA : A t = K.add (Z t) p := hp.2.symm
      -- (1 + hν)·(2Q' + hν²A + s) = (1 + hν)·2Q
      have e2 := congrArg (K.mul (K.add K.one (K.mul h ν))) hs.2
      generalize Q K ν h Z A (t + 1) = Q' at e2 ⊢
      show _ = K.mul (K.add (K.add K.one K.one) (K.mul h ν)) (K.add (K.mul ν (Z t)) (K.mul (K.mul h (K.mul ν ν)) (A t)))
      have e3 : K.mul (K.add K.one (K.mul h ν)) (K.mul (K.add K.one K.one) (Q K ν h Z A t))
          = K.mul (K.add K.one (K.mul h ν)) (K.mul (K.add K.one K.one) (K.add (K.mul ν (Z t)) (K.mul (K.mul h (K.mul ν ν)) (A t)))) := rfl
      rw [e3, eA] at e2
      rw [eA]
      calc _ = K.add (K.mul (K.add K.one (K.mul h ν)) (K.add (K.add (K.mul (K.add K.one K.one) Q') (K.mul (K.mul h (K.mul ν ν)) (K.add (Z t) p))) s)) (K.neg (K.add (K.mul (K.mul h (K.mul ν ν)) (Z t)) (K.mul (K.mul (K.mul h h) (K.mul ν (K.mul ν ν))) (K.add (Z t) p)))) := by lib_ring
        _ = K.add (K.mul (K.add K.one (K.mul h ν)) (K.mul (K.add K.one K.one) (K.add (K.mul ν (Z t)) (K.mul (K.mul h (K.mul ν ν)) (K.add (Z t) p))))) (K.neg (K.add (K.mul (K.mul h (K.mul ν ν)) (Z t)) (K.mul (K.mul (K.mul h h) (K.mul ν (K.mul ν ν))) (K.add (Z t) p)))) := by rw [e2]
        _ = _ := by lib_ring

theorem enst_exp (ν h c : R) (Z A : Nat → R) (hν : O.nn ν) (hν0 : ν ≠ K.zero) (hh : O.nn h) (hc : O.nn c)
    (hZ : ∀ t, O.nn (Z t)) (hA : ∀ t, O.nn (A t)) (hZA : ∀ t, Le K O.toCone (Z t) (A t))
    (step : ∀ t, Le K O.toCone (Q K ν h Z A (t + 1)) (K.add (K.mul ν (Z t)) (K.mul (K.mul h c) (K.mul (A t) (Z t)))))
    (small : Le K O.toCone (K.mul (K.add c c) (Q K ν h Z A 0)) (K.mul ν (K.mul ν ν))) :
    ∀ n, Le K O.toCone (K.mul (pw K (K.add (K.add K.one K.one) (K.add (K.mul h ν) (K.mul h ν))) n) (Q K ν h Z A n))
      (K.mul (pw K (K.add (K.add K.one K.one) (K.mul h ν)) n) (Q K ν h Z A 0))
  | 0 => ⟨K.zero, O.nn_zero, by show K.add (K.mul K.one _) K.zero = K.mul K.one _; lib_ring⟩
  | n + 1 => by
    have ih := enst_exp ν h c Z A hν hν0 hh hc hZ hA hZA step small n
    have hhν : O.nn (K.mul h ν) := O.nn_mul _ _ hh hν
    have two : O.nn (K.add K.one K.one) := O.nn_add _ _ (RatModel.nn_one' K O.toCone) (RatModel.nn_one' K O.toCone)
    have nA : O.nn (K.add (K.add K.one K.one) (K.add (K.mul h ν) (K.mul h ν))) := O.nn_add _ _ two (O.nn_add _ _ hhν hhν)
    have nB : O.nn (K.add (K.add K.one K.one) (K.mul h ν)) := O.nn_add _ _ two hhν
    have st := exp_step K O ν h c Z A hν hν0 hh hc hZ hA hZA step small n
    have m1 := le_mul_nn K O.toCone (pw K (K.add (K.add K.one K.one) (K.add (K.mul h ν) (K.mul h ν))) n) _ _
      (CFLDecay.pw_nn K O _ nA n) st
    have m2 := le_mul_nn K O.toCone (K.add (K.add K.one K.one) (K.mul h ν)) _ _ nB ih
    refine le_trans' K O.toCone _ _ _ ⟨K.zero, O.nn_zero, ?_⟩
      (le_trans' K O.toCone _ _ _ m1 (le_trans' K O.toCone _ _ _ ⟨K.zero, O.nn_zero, ?_⟩
        (le_trans' K O.toCone _ _ _ m2 ⟨K.zero, O.nn_zero, ?_⟩)))
    · show K.add (K.mul (K.mul (pw K (K.add (K.add K.one K.one) (K.add (K.mul h ν) (K.mul h ν))) n)
          (K.add (K.add K.one K.one) (K.add (K.mul h ν) (K.mul h ν)))) (Q K ν h Z A (n + 1))) K.zero = _
      generalize Q K ν h Z A (n + 1) = q
      generalize pw K (K.add (K.add K.one K.one) (K.add (K.mul h ν) (K.mul h ν))) n = P
      lib_ring
    · generalize Q K ν h Z A n = q
      generalize pw K (K.add (K.add K.one K.one) (K.add (K.mul h ν) (K.mul h ν))) n = P
      lib_ring
    · show K.add (K.mul (K.add (K.add K.one K.one) (K.mul h ν)) (K.mul (pw K (K.add (K.add K.one K.one) (K.mul h ν)) n) (Q K ν h Z A 0))) K.zero
        = K.mul (K.mul (pw K (K.add (K.add K.one K.one) (K.mul h ν)) n) (K.add (K.add K.one K.one) (K.mul h ν))) (Q K ν h Z A 0)
      generalize Q K ν h Z A 0 = q
      generalize pw K (K.add (K.add K.one K.one) (K.mul h ν)) n = P
      lib_ring

include mul_inv h10 in
theorem proj_exp (l : List I) (kz : I → Mode) (hd : List.Pairwise (fun a b => kz a ≠ kz b) l)
    (h1 : ∀ p, 1 ≤ nrm (kz p)) (M : Nat) (hM : ∀ p, p ∈ l → nrm (kz p) ≤ M + 1)
    (ι h ν : R) (hh : O.nn h) (hν : O.nn ν) (hν0 : ν ≠ K.zero) (a N : Nat → I → V3 R)
    (hN : ∀ t, Le K O.toCone (lsum K (fun r => dot3 K (N t r) (N t r)) l)
      (lsum K (fun r => dot3 K (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (a t) (a t) r)
        (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (a t) (a t) r)) l))
    (hs : ∀ t r, Step K h ν (wt K kz r) (a (t + 1) r) (a t r) (N t r))
    (small : Le K O.toCone (K.mul (K.add (K.mul (castN K 135) (K.mul ι ι)) (K.mul (castN K 135) (K.mul ι ι)))
      (Q K ν h (Zs K l kz a) (As K l kz a) 0)) (K.mul ν (K.mul ν ν))) (n : Nat) :
    Le K O.toCone (K.mul (pw K (K.add (K.add K.one K.one) (K.add (K.mul h ν) (K.mul h ν))) n) (Q K ν h (Zs K l kz a) (As K l kz a) n))
      (K.mul (pw K (K.add (K.add K.one K.one) (K.mul h ν)) n) (Q K ν h (Zs K l kz a) (As K l kz a) 0)) := by
  have hw : ∀ r, O.nn (wt K kz r) := fun r => dot3_nn K O.toCone _
  exact enst_exp K O ν h (K.mul (castN K 135) (K.mul ι ι)) (Zs K l kz a) (As K l kz a) hν hν0 hh
    (O.nn_mul _ _ (RatModel.nn_cast K O.toCone 135) (O.nn_sq ι))
    (fun t => lsum_nn K O.toCone _ (fun r => O.nn_mul _ _ (hw r) (dot3_nn K O.toCone _)) l)
    (fun t => lsum_nn K O.toCone _ (fun r => O.nn_mul _ _ (O.nn_sq _) (dot3_nn K O.toCone _)) l)
    (fun t => EnstH2.Z_le_A K O kz h1 l (a t))
    (fun t => EventH2.proj_step K O inv mul_inv h10 l kz hd h1 M hM ι h ν hh hν a N hN hs t) small n

end Gen

end EnstExp

#print axioms EnstExp.exp_step
#print axioms EnstExp.enst_exp
#print axioms EnstExp.proj_exp

ns/EnstGalerkin.lean

lean_all/ns/EnstGalerkin.lean

/-
  ns/EnstGalerkin.lean — T37 cycle 979 (goal G638, piece e): global small-data enstrophy bound for the Fourier–Galerkin
  scheme of 3D Navier–Stokes with implicit viscosity, uniform in the truncation, over an ordered field.
  Modes: integer kz_p (nonzero, distinct on the mode list l), ring wave vectors k_p = kvec(kz_p), w_p = |k_p|².
  Scheme: (1 + hν w_r) a^{t+1}_r = a^t_r + h N_r(a^t),  N = Galerkin force Nl with the triad indicator
  [kz_p + kz_q + kz_r = 0] and coupling ι. With Z_t = Σ w|a^t|², A_t = Σ w²|a^t|², Q_t = νZ_t + hν²A_t, c = 135ι²:
    enst_global   c·Q_0 ≤ ν³  ⇒  Q_t ≤ Q_0 for every t
  (so Z_t ≤ Q_0/ν for all time; the constant 135 and the smallness condition do not depend on the number of modes).

  Lean 4 core plus ns/ForceUniform, ns/NormCompare, ns/EnstStep, ns/EnstGlobal.
-/
import ns.ForceUniform
import ns.NormCompare
import ns.EnstStep
import ns.EnstGlobal

namespace EnstGalerkin
open DetChain NSChain DyadicSmallGlobal LibRing DyadicLarge FourierGalerkin GalerkinForce ForceBound ForceSob
  ModeCount CubeCount ForceUniform NormCompare EnstStep EnstGlobal

section Gen
variable {R : Type} (K : CRing R) (O : OrdR K) {I : Type}
variable (inv : R → R) (mul_inv : ∀ y, y ≠ K.zero → K.mul y (inv y) = K.one) (h10 : K.one ≠ K.zero)

def kv (kz : I → Mode) (p : I) : V3 R := kvec K (kz p)
def wt (kz : I → Mode) (p : I) : R := dot3 K (kv K kz p) (kv K kz p)
def Zs (l : List I) (kz : I → Mode) (a : Nat → I → V3 R) (t : Nat) : R :=
  lsum K (fun r => K.mul (wt K kz r) (dot3 K (a t r) (a t r))) l
def As (l : List I) (kz : I → Mode) (a : Nat → I → V3 R) (t : Nat) : R :=
  lsum K (fun r => K.mul (K.mul (wt K kz r) (wt K kz r)) (dot3 K (a t r) (a t r))) l

include mul_inv h10 in
theorem enst_global (l : List I) (kz : I → Mode) (hd : List.Pairwise (fun a b => kz a ≠ kz b) l)
    (h1 : ∀ p, 1 ≤ nrm (kz p)) (M : Nat) (hM : ∀ p, p ∈ l → nrm (kz p) ≤ M + 1)
    (ι h ν : R) (hh : O.nn h) (hν : O.nn ν) (hν0 : ν ≠ K.zero) (a : Nat → I → V3 R)
    (hs : ∀ t r, Step K h ν (wt K kz r) (a (t + 1) r) (a t r) (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (a t) (a t) r))
    (small : Le K O.toCone (K.mul (K.mul (castN K 135) (K.mul ι ι)) (Q K ν h (Zs K l kz a) (As K l kz a) 0)) (K.mul ν (K.mul ν ν)))
    (t : Nat) :
    Le K O.toCone (Q K ν h (Zs K l kz a) (As K l kz a) t) (Q K ν h (Zs K l kz a) (As K l kz a) 0) := by
  have hw : ∀ r, O.nn (wt K kz r) := fun r => dot3_nn K O.toCone _
  have hZ : ∀ t, O.nn (Zs K l kz a t) := fun t =>
    lsum_nn K O.toCone _ (fun r => O.nn_mul _ _ (hw r) (dot3_nn K O.toCone _)) l
  have hA : ∀ t, O.nn (As K l kz a t) := fun t =>
    lsum_nn K O.toCone _ (fun r => O.nn_mul _ _ (O.nn_sq _) (dot3_nn K O.toCone _)) l
  have hc : O.nn (K.mul (castN K 135) (K.mul ι ι)) := O.nn_mul _ _ (RatModel.nn_cast K O.toCone 135) (O.nn_sq ι)
  refine q_le K O ν h _ (Zs K l kz a) (As K l kz a) hν hν0 hh hc hZ hA ?_ small t
  intro s
  -- one step: νZ' + hν²A' ≤ νZ + hB
  have st := enst_step K O.toCone l h ν hh hν (wt K kz) hw (a (s + 1)) (a s) _ (hs s)
  -- B ≤ ι² H (135 Z),  H ≤ A
  have fu := force_uniform K O inv mul_inv h10 l kz hd h1 M hM ι (kv K kz) (a s) (a s)
  have hH : Le K O.toCone (lsum K (fun p => K.mul (K.mul (mu K kz p) (mu K kz p)) (dot3 K (a s p) (a s p))) l)
      (As K l kz a s) :=
    lsum_le K O.toCone _ _ (fun p => le_mul_right' K O.toCone (nrm4_le K O.toCone (kz p)) (dot3_nn K O.toCone _)) l
  have hZn := hZ s
  have hb : Le K O.toCone (lsum K (fun r => dot3 K (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (a s) (a s) r)
      (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (a s) (a s) r)) l)
      (K.mul (K.mul (castN K 135) (K.mul ι ι)) (K.mul (As K l kz a s) (Zs K l kz a s))) := by
    have m1 := le_mul_right' K O.toCone hH (O.nn_mul _ _ (RatModel.nn_cast K O.toCone 135) hZn)
    have m2 := le_mul_nn K O.toCone (K.mul ι ι) _ _ (O.nn_sq ι) m1
    have t2 := le_trans' K O.toCone _ _ _ fu m2
    have e : K.mul (K.mul ι ι) (K.mul (As K l kz a s) (K.mul (castN K 135) (Zs K l kz a s)))
        = K.mul (K.mul (castN K 135) (K.mul ι ι)) (K.mul (As K l kz a s) (Zs K l kz a s)) := by lib_ring
    rw [e] at t2
    exact t2
  have hb2 := le_mul_nn K O.toCone h _ _ hh hb
  have st2 := le_trans' K O.toCone _ _ _ st (le_add' K O.toCone _ _ _ _ (le_refl' K O.toCone _) hb2)
  have e2 : K.mul h (K.mul (K.mul (castN K 135) (K.mul ι ι)) (K.mul (As K l kz a s) (Zs K l kz a s)))
      = K.mul (K.mul h (K.mul (castN K 135) (K.mul ι ι))) (K.mul (As K l kz a s) (Zs K l kz a s)) := by lib_ring
  rw [e2] at st2
  exact st2

end Gen

end EnstGalerkin

#print axioms EnstGalerkin.enst_global

ns/EnstGlobal.lean

lean_all/ns/EnstGlobal.lean

/-
  ns/EnstGlobal.lean — T37 cycle 977 (goal G638, piece d): the small-data invariant of the enstrophy step, over an
  ordered ring without zero divisors (ℚ included). Sequences Z_t, A_t ≥ 0, constants ν > 0, h ≥ 0, c ≥ 0 with
    νZ_{t+1} + hν²A_{t+1} ≤ νZ_t + h·c·A_t·Z_t           (EnstStep.enst_step with Σ|N|² ≤ c·A·Z, c = 135ι²)
  and Q_t = νZ_t + hν²A_t. If c·Q_0 ≤ ν³ then for every t
    q_le     Q_t ≤ Q_0,      z_small   c·Z_t ≤ ν²,
  i.e. the enstrophy of the Galerkin scheme stays below Q_0/ν for all time, with constants independent of the
  truncation (global small-data bound in 3D).

  Lean 4 core plus ns/DyadicSmallGlobal (OrdR), ns/DyadicLarge (le_mul_nn), lib/RingTac.
-/
import ns.DyadicSmallGlobal
import ns.DyadicLarge
import lib.RingTac

namespace EnstGlobal
open DetChain NSChain DyadicSmallGlobal LibRing DyadicLarge

section Gen
variable {R : Type} (K : CRing R) (O : OrdR K)

/-- cancel a positive factor: ν x ≤ ν y, ν ≥ 0, ν ≠ 0 ⇒ x ≤ y -/
theorem le_of_mul_le (ν x y : R) (hν : O.nn ν) (hν0 : ν ≠ K.zero) (h : Le K O.toCone (K.mul ν x) (K.mul ν y)) :
    Le K O.toCone x y := by
  cases O.total x y with
  | inl hxy => exact hxy
  | inr hyx =>
    have h2 := le_mul_nn K O.toCone ν y x hν hyx
    have e := O.antisym _ _ h h2
    have z : K.mul ν (K.add x (K.neg y)) = K.zero := by
      have : K.mul ν (K.add x (K.neg y)) = K.add (K.mul ν x) (K.neg (K.mul ν y)) := by lib_ring
      rw [this, e]; lib_ring
    cases O.noZD _ _ z with
    | inl h0 => exact absurd h0 hν0
    | inr h0 =>
      have : x = y := by
        calc x = K.add (K.add x (K.neg y)) y := by lib_ring
          _ = y := by rw [h0]; lib_ring
      rw [this]; exact le_refl' K O.toCone y

def Q (ν h : R) (Z A : Nat → R) (t : Nat) : R := K.add (K.mul ν (Z t)) (K.mul (K.mul h (K.mul ν ν)) (A t))

theorem q_le (ν h c : R) (Z A : Nat → R) (hν : O.nn ν) (hν0 : ν ≠ K.zero) (hh : O.nn h) (hc : O.nn c)
    (hZ : ∀ t, O.nn (Z t)) (hA : ∀ t, O.nn (A t))
    (step : ∀ t, Le K O.toCone (Q K ν h Z A (t + 1)) (K.add (K.mul ν (Z t)) (K.mul (K.mul h c) (K.mul (A t) (Z t)))))
    (small : Le K O.toCone (K.mul c (Q K ν h Z A 0)) (K.mul ν (K.mul ν ν))) :
    ∀ t, Le K O.toCone (Q K ν h Z A t) (Q K ν h Z A 0)
  | 0 => le_refl' K O.toCone _
  | t + 1 => by
    have ih := q_le ν h c Z A hν hν0 hh hc hZ hA step small t
    -- νZ_t ≤ Q_t ≤ Q_0, so ν(cZ_t) ≤ cQ_0 ≤ ν·ν², hence cZ_t ≤ ν²
    have hq : Le K O.toCone (K.mul ν (Z t)) (Q K ν h Z A t) :=
      ⟨K.mul (K.mul h (K.mul ν ν)) (A t), O.nn_mul _ _ (O.nn_mul _ _ hh (O.nn_sq ν)) (hA t), rfl⟩
    have h1 := le_trans' K O.toCone _ _ _ hq ih
    have h2 := le_mul_nn K O.toCone c _ _ hc h1
    have h3 := le_trans' K O.toCone _ _ _ h2 small
    have e3 : K.mul c (K.mul ν (Z t)) = K.mul ν (K.mul c (Z t)) := by lib_ring
    rw [e3] at h3
    have hcz := le_of_mul_le K O ν _ _ hν hν0 h3
    -- h c A Z ≤ h ν² A
    have h4 := le_mul_nn K O.toCone (K.mul h (A t)) _ _ (O.nn_mul _ _ hh (hA t)) hcz
    have e4 : K.mul (K.mul h (A t)) (K.mul c (Z t)) = K.mul (K.mul h c) (K.mul (A t) (Z t)) := by lib_ring
    have e5 : K.mul (K.mul h (A t)) (K.mul ν ν) = K.mul (K.mul h (K.mul ν ν)) (A t) := by lib_ring
    rw [e4, e5] at h4
    have h5 := le_add' K O.toCone _ _ _ _ (le_refl' K O.toCone (K.mul ν (Z t))) h4
    exact le_trans' K O.toCone _ _ _ (step t) (le_trans' K O.toCone _ _ _ h5 ih)

theorem z_small (ν h c : R) (Z A : Nat → R) (hν : O.nn ν) (hν0 : ν ≠ K.zero) (hh : O.nn h) (hc : O.nn c)
    (hZ : ∀ t, O.nn (Z t)) (hA : ∀ t, O.nn (A t))
    (step : ∀ t, Le K O.toCone (Q K ν h Z A (t + 1)) (K.add (K.mul ν (Z t)) (K.mul (K.mul h c) (K.mul (A t) (Z t)))))
    (small : Le K O.toCone (K.mul c (Q K ν h Z A 0)) (K.mul ν (K.mul ν ν))) (t : Nat) :
    Le K O.toCone (K.mul c (Z t)) (K.mul ν ν) := by
  have ih := q_le K O ν h c Z A hν hν0 hh hc hZ hA step small t
  have hq : Le K O.toCone (K.mul ν (Z t)) (Q K ν h Z A t) :=
    ⟨K.mul (K.mul h (K.mul ν ν)) (A t), O.nn_mul _ _ (O.nn_mul _ _ hh (O.nn_sq ν)) (hA t), rfl⟩
  have h3 := le_trans' K O.toCone _ _ _ (le_mul_nn K O.toCone c _ _ hc (le_trans' K O.toCone _ _ _ hq ih)) small
  have e3 : K.mul c (K.mul ν (Z t)) = K.mul ν (K.mul c (Z t)) := by lib_ring
  rw [e3] at h3
  exact le_of_mul_le K O ν _ _ hν hν0 h3

end Gen

end EnstGlobal

#print axioms EnstGlobal.le_of_mul_le
#print axioms EnstGlobal.q_le
#print axioms EnstGlobal.z_small

ns/EnstGronwall.lean

lean_all/ns/EnstGronwall.lean

/-
  ns/EnstGronwall.lean — T37 cycle 1033 (goal G671, piece c): product-form discrete Gronwall for the difference
  enstrophy, over an ordered ring. With Q_t = νZ_t + hν²A_t, A_t, α_t, h, ν ≥ 0 and β_t ≤ ν²:
    step:  νZ_{t+1} + hν²A_{t+1} ≤ νZ_t + h(α_t Z_t + β_t A_t)
    ⇒      ν^T · Q_T ≤ Π_{t<T} (ν + hα_t) · Q_0          (diff_gronwall)
  (one step: νQ_{t+1} ≤ νQ_t + hα_t·νZ_t ≤ (ν + hα_t)Q_t). For two solutions of the Galerkin scheme, ForceDiff gives
  α_t ≈ ι²(C₁Z(a_t) + A(b_t)) and β_t ≈ ι²C₂Z(a_t), so the difference is controlled by its initial value with a
  factor independent of the truncation.

  Lean 4 core plus ns/EnstGlobal (Q), ns/ForceSob (le_mul_right'), ns/RatModel (nn_one'), lib/RingTac.
-/
import ns.EnstGlobal
import ns.ForceSob
import ns.RatModel

namespace EnstGronwall
open DetChain NSChain DyadicSmallGlobal LibRing DyadicLarge EnstGlobal

section Gen
variable {R : Type} (K : CRing R) (O : OrdR K)

def prodN (f : Nat → R) : Nat → R
  | 0 => K.one
  | n + 1 => K.mul (f n) (prodN f n)

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

theorem prod_nn (f : Nat → R) (hf : ∀ n, O.nn (f n)) : ∀ n, O.nn (prodN K f n)
  | 0 => RatModel.nn_one' K O.toCone
  | n + 1 => O.nn_mul _ _ (hf n) (prod_nn f hf n)

theorem pwr_nn (x : R) (hx : O.nn x) : ∀ n, O.nn (pwr K x n)
  | 0 => RatModel.nn_one' K O.toCone
  | n + 1 => O.nn_mul _ _ hx (pwr_nn x hx n)

theorem one_step (ν h : R) (Z A α β : Nat → R) (hν : O.nn ν) (hh : O.nn h) (hA : ∀ t, O.nn (A t))
    (hα : ∀ t, O.nn (α t)) (hβ : ∀ t, Le K O.toCone (β t) (K.mul ν ν))
    (step : ∀ t, Le K O.toCone (Q K ν h Z A (t + 1))
      (K.add (K.mul ν (Z t)) (K.mul h (K.add (K.mul (α t) (Z t)) (K.mul (β t) (A t)))))) (t : Nat) :
    Le K O.toCone (K.mul ν (Q K ν h Z A (t + 1))) (K.mul (K.add ν (K.mul h (α t))) (Q K ν h Z A t)) := by
  -- β A ≤ ν² A
  have b1 := ForceSob.le_mul_right' K O.toCone (hβ t) (hA t)
  have b2 := le_mul_nn K O.toCone h _ _ hh (le_add' K O.toCone _ _ _ _ (le_refl' K O.toCone (K.mul (α t) (Z t))) b1)
  have s1 := le_trans' K O.toCone _ _ _ (step t) (le_add' K O.toCone _ _ _ _ (le_refl' K O.toCone (K.mul ν (Z t))) b2)
  -- times ν
  have s2 := le_mul_nn K O.toCone ν _ _ hν s1
  refine le_trans' K O.toCone _ _ _ s2 ⟨K.mul (K.mul h (α t)) (K.mul (K.mul h (K.mul ν ν)) (A t)),
    O.nn_mul _ _ (O.nn_mul _ _ hh (hα t)) (O.nn_mul _ _ (O.nn_mul _ _ hh (O.nn_sq ν)) (hA t)), ?_⟩
  show K.add (K.mul ν (K.add (K.mul ν (Z t)) (K.mul h (K.add (K.mul (α t) (Z t)) (K.mul (K.mul ν ν) (A t))))))
      (K.mul (K.mul h (α t)) (K.mul (K.mul h (K.mul ν ν)) (A t)))
    = K.mul (K.add ν (K.mul h (α t))) (K.add (K.mul ν (Z t)) (K.mul (K.mul h (K.mul ν ν)) (A t)))
  lib_ring

theorem diff_gronwall (ν h : R) (Z A α β : Nat → R) (hν : O.nn ν) (hh : O.nn h)
    (hA : ∀ t, O.nn (A t)) (hα : ∀ t, O.nn (α t)) (hβ : ∀ t, Le K O.toCone (β t) (K.mul ν ν))
    (step : ∀ t, Le K O.toCone (Q K ν h Z A (t + 1))
      (K.add (K.mul ν (Z t)) (K.mul h (K.add (K.mul (α t) (Z t)) (K.mul (β t) (A t)))))) :
    ∀ T, Le K O.toCone (K.mul (pwr K ν T) (Q K ν h Z A T))
      (K.mul (prodN K (fun t => K.add ν (K.mul h (α t))) T) (Q K ν h Z A 0))
  | 0 => by
    show Le K O.toCone (K.mul K.one _) (K.mul K.one _)
    exact le_refl' K O.toCone _
  | T + 1 => by
    have ih := diff_gronwall ν h Z A α β hν hh hA hα hβ step T
    have os := one_step K O ν h Z A α β hν hh hA hα hβ step T
    -- ν^{T+1} Q_{T+1} = ν^T (ν Q_{T+1}) ≤ ν^T (ν + hα_T) Q_T = (ν + hα_T)(ν^T Q_T) ≤ (ν + hα_T) P_T Q_0
    have m1 := le_mul_nn K O.toCone (pwr K ν T) _ _ (pwr_nn K O ν hν T) os
    have m2 := le_mul_nn K O.toCone (K.add ν (K.mul h (α T))) _ _ (O.nn_add _ _ hν (O.nn_mul _ _ hh (hα T))) ih
    have e1 : K.mul (pwr K ν (T + 1)) (Q K ν h Z A (T + 1)) = K.mul (pwr K ν T) (K.mul ν (Q K ν h Z A (T + 1))) := by
      show K.mul (K.mul ν (pwr K ν T)) _ = _; lib_ring
    have e2 : K.mul (pwr K ν T) (K.mul (K.add ν (K.mul h (α T))) (Q K ν h Z A T))
        = K.mul (K.add ν (K.mul h (α T))) (K.mul (pwr K ν T) (Q K ν h Z A T)) := by lib_ring
    have e3 : K.mul (K.add ν (K.mul h (α T))) (K.mul (prodN K (fun t => K.add ν (K.mul h (α t))) T) (Q K ν h Z A 0))
        = K.mul (prodN K (fun t => K.add ν (K.mul h (α t))) (T + 1)) (Q K ν h Z A 0) := by
      show _ = K.mul (K.mul (K.add ν (K.mul h (α T))) (prodN K (fun t => K.add ν (K.mul h (α t))) T)) _; lib_ring
    rw [e1]; rw [e2] at m1; rw [e3] at m2
    exact le_trans' K O.toCone _ _ _ m1 m2

end Gen

end EnstGronwall

#print axioms EnstGronwall.one_step
#print axioms EnstGronwall.diff_gronwall

ns/EnstH2.lean

lean_all/ns/EnstH2.lean

/-
  ns/EnstH2.lean — T37 cycle 1069 (goal G656b, piece iv-c): the small-data global H² bound for the implicit-viscosity
  Galerkin scheme, uniform in the truncation, over an ordered field. With A = Σ w²|a|², B = Σ w³|a|² (w = |k|²):
    S0_le / S1_le   Σ_p |k_p|_∞^{−6} ≤ 135,  Σ_p |k_p|_∞^{−6} w_p ≤ 3·135
    Z_le_A          Σ w|a|² ≤ Σ w²|a|²   (w ≥ 1 on nonzero integer modes)
    h2_global       cH·ι²(νA_0 + hν²B_0) ≤ ν³,  cH = 2·3·135 + 2·135 = 1080  ⇒  νA_t + hν²B_t ≤ νA_0 + hν²B_0 for every t
  (EnstStepV with V = w, ForceH2, then EnstGlobal.q_le with c = 1080ι²).

  Lean 4 core plus ns/ForceH2, ns/EnstStepV, ns/EnstGlobal, ns/LatticeBound, ns/ForceUniform (lsum_norms).
-/
import ns.ForceH2
import ns.EnstStepV

namespace EnstH2
open DetChain NSChain DyadicSmallGlobal LibRing DyadicLarge FourierGalerkin GalerkinForce ForceBound ForceSob
  ModeCount CubeCount ForceUniform NormCompare EnstGalerkin ForceTwoScale ForceSobW CastZAdd WeightH2 LatticeSum
  LatticeBound ForceH2 EnstStep EnstStepV EnstGlobal

section Gen
variable {R : Type} (K : CRing R) (O : OrdR K) {I : Type}
variable (inv : R → R) (mul_inv : ∀ y, y ≠ K.zero → K.mul y (inv y) = K.one) (h10 : K.one ≠ K.zero)

include mul_inv h10 in
theorem pt6_le4 (kz : I → Mode) (h1 : ∀ p, 1 ≤ nrm (kz p)) (p : I) :
    Le K O.toCone (K.mul (cube3 K (ivn K inv kz p)) (cube3 K (ivn K inv kz p))) (f4 K inv (nrm (kz p))) := by
  have ⟨hi, hle⟩ := ivn_le K O inv mul_inv h10 kz h1 p
  have h4 : O.nn (K.mul (K.mul (ivn K inv kz p) (ivn K inv kz p)) (K.mul (ivn K inv kz p) (ivn K inv kz p))) :=
    O.nn_mul _ _ (O.nn_sq _) (O.nn_sq _)
  have m := le_mul_nn K O.toCone _ _ _ h4 hle
  have e1 : K.mul (cube3 K (ivn K inv kz p)) (cube3 K (ivn K inv kz p))
      = K.mul (K.mul (K.mul (ivn K inv kz p) (ivn K inv kz p)) (K.mul (ivn K inv kz p) (ivn K inv kz p)))
          (K.mul (ivn K inv kz p) (ivn K inv kz p)) := by unfold cube3; lib_ring
  rw [e1]
  refine le_trans' K O.toCone _ _ _ m ⟨K.zero, O.nn_zero, ?_⟩
  show K.add (K.mul _ K.one) K.zero = K.mul (K.mul (inv (castN K (nrm (kz p)))) (inv (castN K (nrm (kz p)))))
    (K.mul (inv (castN K (nrm (kz p)))) (inv (castN K (nrm (kz p)))))
  show K.add (K.mul (K.mul (K.mul (inv (castN K (nrm (kz p)))) (inv (castN K (nrm (kz p)))))
    (K.mul (inv (castN K (nrm (kz p)))) (inv (castN K (nrm (kz p)))))) K.one) K.zero = _
  lib_ring

include mul_inv h10 in
theorem pt6w_le (kz : I → Mode) (h1 : ∀ p, 1 ≤ nrm (kz p)) (p : I) :
    Le K O.toCone (K.mul (K.mul (cube3 K (ivn K inv kz p)) (cube3 K (ivn K inv kz p))) (wt K kz p))
      (K.mul (K.add (K.add K.one K.one) K.one) (f4 K inv (nrm (kz p)))) := by
  have e := nn_ivn K O inv mul_inv h10 kz h1 p
  have hi := (ivn_le K O inv mul_inv h10 kz h1 p).1
  have w3 := dot_le_3nrm K O (kz p)
  have m := le_mul_nn K O.toCone (K.mul (cube3 K (ivn K inv kz p)) (cube3 K (ivn K inv kz p))) _ _ (O.nn_sq _) w3
  refine le_trans' K O.toCone _ _ _ m ⟨K.zero, O.nn_zero, ?_⟩
  show K.add (K.mul (K.mul (cube3 K (ivn K inv kz p)) (cube3 K (ivn K inv kz p)))
      (K.add (K.add (K.mul (nn K kz p) (nn K kz p)) (K.mul (nn K kz p) (nn K kz p))) (K.mul (nn K kz p) (nn K kz p)))) K.zero
    = K.mul (K.add (K.add K.one K.one) K.one) (K.mul (K.mul (ivn K inv kz p) (ivn K inv kz p)) (K.mul (ivn K inv kz p) (ivn K inv kz p)))
  unfold cube3
  generalize nn K kz p = n at e
  generalize ivn K inv kz p = v at e
  calc _ = K.mul (K.add (K.add K.one K.one) K.one) (K.mul (K.mul (K.mul v v) (K.mul v v)) (K.mul (K.mul n v) (K.mul n v))) := by lib_ring
    _ = _ := by rw [e]; lib_ring

include mul_inv h10 in
theorem S0_le (l : List I) (kz : I → Mode) (hd : List.Pairwise (fun a b => kz a ≠ kz b) l)
    (h1 : ∀ p, 1 ≤ nrm (kz p)) (M : Nat) (hM : ∀ p, p ∈ l → nrm (kz p) ≤ M + 1) :
    Le K O.toCone (lsum K (fun p => K.mul (cube3 K (ivn K inv kz p)) (cube3 K (ivn K inv kz p))) l) (castN K 135) := by
  have a := lsum_le K O.toCone _ _ (fun p => pt6_le4 K O inv mul_inv h10 kz h1 p) l
  rw [lsum_norms K (f4 K inv) kz l] at a
  exact le_trans' K O.toCone _ _ _ a (lattice_bound K O inv mul_inv h10 (l.map kz) (pw_map kz l hd)
    (fun k hk => by have ⟨q, _, e⟩ := mem_map_inv kz k l hk; rw [← e]; exact h1 q) M
    (fun k hk => by have ⟨q, hq, e⟩ := mem_map_inv kz k l hk; rw [← e]; exact hM q hq))

include mul_inv h10 in
theorem S1_le (l : List I) (kz : I → Mode) (hd : List.Pairwise (fun a b => kz a ≠ kz b) l)
    (h1 : ∀ p, 1 ≤ nrm (kz p)) (M : Nat) (hM : ∀ p, p ∈ l → nrm (kz p) ≤ M + 1) :
    Le K O.toCone (lsum K (fun p => K.mul (K.mul (cube3 K (ivn K inv kz p)) (cube3 K (ivn K inv kz p))) (wt K kz p)) l)
      (K.mul (K.add (K.add K.one K.one) K.one) (castN K 135)) := by
  have a := lsum_le K O.toCone _ _ (fun p => pt6w_le K O inv mul_inv h10 kz h1 p) l
  rw [lsum_mul_left, lsum_norms K (f4 K inv) kz l] at a
  refine le_trans' K O.toCone _ _ _ a (le_mul_nn K O.toCone _ _ _ ?_ (lattice_bound K O inv mul_inv h10 (l.map kz) (pw_map kz l hd)
    (fun k hk => by have ⟨q, _, e⟩ := mem_map_inv kz k l hk; rw [← e]; exact h1 q) M
    (fun k hk => by have ⟨q, hq, e⟩ := mem_map_inv kz k l hk; rw [← e]; exact hM q hq)))
  exact O.nn_add _ _ (O.nn_add _ _ (RatModel.nn_one' K O.toCone) (RatModel.nn_one' K O.toCone)) (RatModel.nn_one' K O.toCone)

def cH : R := K.add (K.mul (K.add K.one K.one) (K.mul (K.add (K.add K.one K.one) K.one) (castN K 135)))
  (K.mul (K.add K.one K.one) (castN K 135))

theorem Z_le_A (kz : I → Mode) (h1 : ∀ p, 1 ≤ nrm (kz p)) (l : List I) (a : I → V3 R) :
    Le K O.toCone (lsum K (fun p => K.mul (wt K kz p) (dot3 K (a p) (a p))) l)
      (lsum K (fun p => K.mul (K.mul (wt K kz p) (wt K kz p)) (dot3 K (a p) (a p))) l) := by
  refine lsum_le K O.toCone _ _ (fun p => ?_) l
  have c1 := RatModel.cast_mono K O.toCone 1 _ (h1 p)
  have one1 : castN K 1 = K.one := by show K.add K.one K.zero = K.one; lib_ring
  rw [one1] at c1
  have sq := EnstLocal.sq_mono K O _ _ (RatModel.nn_one' K O.toCone) (RatModel.nn_cast K O.toCone _) c1
  have w1 := le_trans' K O.toCone _ _ _ sq (nrm_le_dot K O.toCone (kz p))
  rw [show K.mul K.one K.one = K.one by lib_ring] at w1
  cases w1 with
  | intro d hd =>
    refine ⟨K.mul (K.mul (wt K kz p) d) (dot3 K (a p) (a p)),
      O.nn_mul _ _ (O.nn_mul _ _ (dot3_nn K O.toCone _) hd.1) (dot3_nn K O.toCone _), ?_⟩
    have hw : wt K kz p = K.add K.one d := hd.2.symm
    rw [hw]; lib_ring

include mul_inv h10 in
/-- small-data global H² bound, uniform in the truncation -/
theorem h2_global (l : List I) (kz : I → Mode) (hd : List.Pairwise (fun a b => kz a ≠ kz b) l)
    (h1 : ∀ p, 1 ≤ nrm (kz p)) (M : Nat) (hM : ∀ p, p ∈ l → nrm (kz p) ≤ M + 1)
    (ι h ν : R) (hh : O.nn h) (hν : O.nn ν) (hν0 : ν ≠ K.zero) (a N : Nat → I → V3 R)
    (hN : ∀ t, Le K O.toCone (lsum K (fun r => K.mul (wt K kz r) (dot3 K (N t r) (N t r))) l)
      (lsum K (fun r => K.mul (wt K kz r) (dot3 K (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (a t) (a t) r)
        (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (a t) (a t) r))) l))
    (hs : ∀ t r, Step K h ν (wt K kz r) (a (t + 1) r) (a t r) (N t r))
    (small : Le K O.toCone (K.mul (K.mul (cH K) (K.mul ι ι))
      (Q K ν h (fun t => lsum K (fun p => K.mul (K.mul (wt K kz p) (wt K kz p)) (dot3 K (a t p) (a t p))) l)
        (fun t => lsum K (fun p => K.mul (K.mul (K.mul (wt K kz p) (wt K kz p)) (wt K kz p)) (dot3 K (a t p) (a t p))) l) 0))
      (K.mul ν (K.mul ν ν))) (t : Nat) :
    Le K O.toCone (Q K ν h (fun t => lsum K (fun p => K.mul (K.mul (wt K kz p) (wt K kz p)) (dot3 K (a t p) (a t p))) l)
        (fun t => lsum K (fun p => K.mul (K.mul (K.mul (wt K kz p) (wt K kz p)) (wt K kz p)) (dot3 K (a t p) (a t p))) l) t)
      (Q K ν h (fun t => lsum K (fun p => K.mul (K.mul (wt K kz p) (wt K kz p)) (dot3 K (a t p) (a t p))) l)
        (fun t => lsum K (fun p => K.mul (K.mul (K.mul (wt K kz p) (wt K kz p)) (wt K kz p)) (dot3 K (a t p) (a t p))) l) 0) := by
  have hw : ∀ r, O.nn (wt K kz r) := fun r => dot3_nn K O.toCone _
  have two : O.nn (K.add K.one K.one) := O.nn_add _ _ (RatModel.nn_one' K O.toCone) (RatModel.nn_one' K O.toCone)
  have three : O.nn (K.add (K.add K.one K.one) K.one) := O.nn_add _ _ two (RatModel.nn_one' K O.toCone)
  have n135 : O.nn (castN K 135) := RatModel.nn_cast K O.toCone 135
  have ncH : O.nn (cH K) := O.nn_add _ _ (O.nn_mul _ _ two (O.nn_mul _ _ three n135)) (O.nn_mul _ _ two n135)
  have hc : O.nn (K.mul (cH K) (K.mul ι ι)) := O.nn_mul _ _ ncH (O.nn_sq ι)
  refine q_le K O ν h _ _ _ hν hν0 hh hc
    (fun t => lsum_nn K O.toCone _ (fun p => O.nn_mul _ _ (O.nn_sq _) (dot3_nn K O.toCone _)) l)
    (fun t => lsum_nn K O.toCone _ (fun p => O.nn_mul _ _ (O.nn_mul _ _ (O.nn_sq _) (hw p)) (dot3_nn K O.toCone _)) l) ?_ small t
  intro s
  have st := enst_stepV K O.toCone l h ν hh hν (wt K kz) (wt K kz) hw hw (a (s + 1)) (a s) (N s) (hs s)
  have fh := force_h2 K O inv mul_inv h10 l kz hd h1 ι (a s)
  have hS0 := S0_le K O inv mul_inv h10 l kz hd h1 M hM
  have hS1 := S1_le K O inv mul_inv h10 l kz hd h1 M hM
  have hZA := Z_le_A K O kz h1 l (a s)
  have nZ : O.nn (lsum K (fun q => K.mul (wt K kz q) (dot3 K (a s q) (a s q))) l) :=
    lsum_nn K O.toCone _ (fun q => O.nn_mul _ _ (hw q) (dot3_nn K O.toCone _)) l
  have nA : O.nn (lsum K (fun p => K.mul (K.mul (wt K kz p) (wt K kz p)) (dot3 K (a s p) (a s p))) l) :=
    lsum_nn K O.toCone _ (fun p => O.nn_mul _ _ (O.nn_sq _) (dot3_nn K O.toCone _)) l
  have nB : O.nn (lsum K (fun p => K.mul (K.mul (K.mul (wt K kz p) (wt K kz p)) (wt K kz p)) (dot3 K (a s p) (a s p))) l) :=
    lsum_nn K O.toCone _ (fun p => O.nn_mul _ _ (O.nn_mul _ _ (O.nn_sq _) (hw p)) (dot3_nn K O.toCone _)) l
  have nS1 : O.nn (lsum K (fun p => K.mul (K.mul (cube3 K (ivn K inv kz p)) (cube3 K (ivn K inv kz p))) (wt K kz p)) l) :=
    lsum_nn K O.toCone _ (fun p => O.nn_mul _ _ (O.nn_sq _) (hw p)) l
  have nS0 : O.nn (lsum K (fun p => K.mul (cube3 K (ivn K inv kz p)) (cube3 K (ivn K inv kz p))) l) :=
    lsum_nn K O.toCone _ (fun p => O.nn_sq _) l
  -- 2 S1 Z + 2 S0 A ≤ cH · A
  have i1 := TruncRate.mm K O _ _ _ _ nS1 nA hS1 hZA
  have i2 := ForceSob.le_mul_right' K O.toCone hS0 nA
  have inner := le_add' K O.toCone _ _ _ _ (le_mul_nn K O.toCone _ _ _ two i1) (le_mul_nn K O.toCone _ _ _ two i2)
  have b1 := le_trans' K O.toCone _ _ _ (hN s) fh
  have b2 := le_trans' K O.toCone _ _ _ b1 (le_mul_nn K O.toCone _ _ _ (O.nn_sq ι) (le_mul_nn K O.toCone _ _ _ nB inner))
  have hb := le_mul_nn K O.toCone h _ _ hh b2
  have st2 := le_trans' K O.toCone _ _ _ st (le_add' K O.toCone _ _ _ _ (le_refl' K O.toCone _) hb)
  refine le_trans' K O.toCone _ _ _ st2 ⟨K.zero, O.nn_zero, ?_⟩
  unfold cH
  generalize lsum K (fun p => K.mul (K.mul (wt K kz p) (wt K kz p)) (dot3 K (a s p) (a s p))) l = As'
  generalize lsum K (fun p => K.mul (K.mul (K.mul (wt K kz p) (wt K kz p)) (wt K kz p)) (dot3 K (a s p) (a s p))) l = Bs
  generalize castN K 135 = c
  lib_ring

end Gen

end EnstH2

#print axioms EnstH2.pt6_le4
#print axioms EnstH2.pt6w_le
#print axioms EnstH2.S0_le
#print axioms EnstH2.S1_le
#print axioms EnstH2.Z_le_A
#print axioms EnstH2.h2_global

ns/EnstH3.lean

lean_all/ns/EnstH3.lean

/-
  ns/EnstH3.lean — T37 cycle 1093 (goal G717 preparation): the small-data global H³ bound for the implicit-viscosity
  Galerkin scheme, uniform in the truncation, over an ordered field. With B = Σ w³|a|², B₄ = Σ w⁴|a|² (w = |k|²):
    S0h_le / S1h_le   Σ_p |k_p|_∞^{−8} ≤ 135,  Σ_p |k_p|_∞^{−8} w_p² ≤ 9·135
    Z_le_B            Σ w|a|² ≤ Σ w³|a|²   (w ≥ 1 on nonzero integer modes)
    h3_global         c₃ι²(νB_0 + hν²B₄_0) ≤ ν³,  c₃ = 8(9·135 + 135)  ⇒  νB_t + hν²B₄_t ≤ νB_0 + hν²B₄_0 for every t
  (EnstStepV with V = w², ForceH3, then EnstGlobal.q_le with c = c₃ι²).

  Lean 4 core plus ns/ForceH3, ns/EnstH2, ns/EnstStepV, ns/EnstGlobal, ns/LatticeBound.
-/
import ns.ForceH3
import ns.EnstH2

namespace EnstH3
open DetChain NSChain DyadicSmallGlobal LibRing DyadicLarge FourierGalerkin GalerkinForce ForceBound ForceSob
  ModeCount CubeCount ForceUniform NormCompare EnstGalerkin ForceTwoScale ForceSobW CastZAdd WeightH2 LatticeSum
  LatticeBound LatticeStep ForceH3 Tri4 EnstStep EnstStepV EnstGlobal

section Gen
variable {R : Type} (K : CRing R) (O : OrdR K) {I : Type}
variable (inv : R → R) (mul_inv : ∀ y, y ≠ K.zero → K.mul y (inv y) = K.one) (h10 : K.one ≠ K.zero)

def k9 : R := K.mul (K.add (K.add K.one K.one) K.one) (K.add (K.add K.one K.one) K.one)

theorem nn9 : O.nn (k9 K) := by
  have three : O.nn (K.add (K.add K.one K.one) K.one) :=
    O.nn_add _ _ (O.nn_add _ _ (RatModel.nn_one' K O.toCone) (RatModel.nn_one' K O.toCone)) (RatModel.nn_one' K O.toCone)
  exact O.nn_mul _ _ three three

theorem nn8 : O.nn (k8 K) := by
  have two : O.nn (K.add K.one K.one) := O.nn_add _ _ (RatModel.nn_one' K O.toCone) (RatModel.nn_one' K O.toCone)
  exact O.nn_add _ _ (O.nn_add _ _ two two) (O.nn_add _ _ two two)

include mul_inv h10 in
theorem pt8_le4 (kz : I → Mode) (h1 : ∀ p, 1 ≤ nrm (kz p)) (p : I) :
    Le K O.toCone (K.mul (p4 K (ivn K inv kz p)) (p4 K (ivn K inv kz p))) (f4 K inv (nrm (kz p))) := by
  have ⟨_, hle⟩ := ivn_le K O inv mul_inv h10 kz h1 p
  have q := TruncRate.mm K O _ _ _ _ (O.nn_sq (ivn K inv kz p)) (RatModel.nn_one' K O.toCone) hle hle
  have m := le_mul_nn K O.toCone (p4 K (ivn K inv kz p)) _ _ (by unfold p4; exact O.nn_mul _ _ (O.nn_sq _) (O.nn_sq _)) q
  refine le_trans' K O.toCone _ _ _ ⟨K.zero, O.nn_zero, ?_⟩ (le_trans' K O.toCone _ _ _ m ⟨K.zero, O.nn_zero, ?_⟩)
  · unfold p4; lib_ring
  · show K.add (K.mul (p4 K (ivn K inv kz p)) (K.mul K.one K.one)) K.zero = p4 K (iv K inv (nrm (kz p)))
    show K.add (K.mul (p4 K (inv (castN K (nrm (kz p))))) (K.mul K.one K.one)) K.zero = p4 K (inv (castN K (nrm (kz p))))
    unfold p4; lib_ring

include mul_inv h10 in
theorem pt8w_le (kz : I → Mode) (h1 : ∀ p, 1 ≤ nrm (kz p)) (p : I) :
    Le K O.toCone (K.mul (K.mul (p4 K (ivn K inv kz p)) (p4 K (ivn K inv kz p))) (K.mul (wt K kz p) (wt K kz p)))
      (K.mul (k9 K) (f4 K inv (nrm (kz p)))) := by
  have e := nn_ivn K O inv mul_inv h10 kz h1 p
  have w3 := dot_le_3nrm K O (kz p)
  have hw : O.nn (wt K kz p) := dot3_nn K O.toCone _
  have w9 := TruncRate.mm K O _ _ _ _ hw (O.nn_add _ _ (O.nn_add _ _ (O.nn_sq _) (O.nn_sq _)) (O.nn_sq _)) w3 w3
  have m := le_mul_nn K O.toCone (K.mul (p4 K (ivn K inv kz p)) (p4 K (ivn K inv kz p))) _ _ (O.nn_sq _) w9
  refine le_trans' K O.toCone _ _ _ m ⟨K.zero, O.nn_zero, ?_⟩
  show K.add (K.mul (K.mul (p4 K (ivn K inv kz p)) (p4 K (ivn K inv kz p)))
      (K.mul (K.add (K.add (K.mul (nn K kz p) (nn K kz p)) (K.mul (nn K kz p) (nn K kz p))) (K.mul (nn K kz p) (nn K kz p)))
        (K.add (K.add (K.mul (nn K kz p) (nn K kz p)) (K.mul (nn K kz p) (nn K kz p))) (K.mul (nn K kz p) (nn K kz p))))) K.zero
    = K.mul (k9 K) (p4 K (ivn K inv kz p))
  unfold p4 k9
  generalize nn K kz p = n at e
  generalize ivn K inv kz p = v at e
  calc _ = K.mul (K.mul (K.add (K.add K.one K.one) K.one) (K.add (K.add K.one K.one) K.one))
        (K.mul (K.mul (K.mul v v) (K.mul v v)) (K.mul (K.mul (K.mul n v) (K.mul n v)) (K.mul (K.mul n v) (K.mul n v)))) := by lib_ring
    _ = _ := by rw [e]; lib_ring

include mul_inv h10 in
theorem S0h_le (l : List I) (kz : I → Mode) (hd : List.Pairwise (fun a b => kz a ≠ kz b) l)
    (h1 : ∀ p, 1 ≤ nrm (kz p)) (M : Nat) (hM : ∀ p, p ∈ l → nrm (kz p) ≤ M + 1) :
    Le K O.toCone (lsum K (fun p => K.mul (p4 K (ivn K inv kz p)) (p4 K (ivn K inv kz p))) l) (castN K 135) := by
  have a := lsum_le K O.toCone _ _ (fun p => pt8_le4 K O inv mul_inv h10 kz h1 p) l
  rw [lsum_norms K (f4 K inv) kz l] at a
  exact le_trans' K O.toCone _ _ _ a (lattice_bound K O inv mul_inv h10 (l.map kz) (pw_map kz l hd)
    (fun k hk => by have ⟨q, _, e⟩ := mem_map_inv kz k l hk; rw [← e]; exact h1 q) M
    (fun k hk => by have ⟨q, hq, e⟩ := mem_map_inv kz k l hk; rw [← e]; exact hM q hq))

include mul_inv h10 in
theorem S1h_le (l : List I) (kz : I → Mode) (hd : List.Pairwise (fun a b => kz a ≠ kz b) l)
    (h1 : ∀ p, 1 ≤ nrm (kz p)) (M : Nat) (hM : ∀ p, p ∈ l → nrm (kz p) ≤ M + 1) :
    Le K O.toCone (lsum K (fun p => K.mul (K.mul (p4 K (ivn K inv kz p)) (p4 K (ivn K inv kz p))) (K.mul (wt K kz p) (wt K kz p))) l)
      (K.mul (k9 K) (castN K 135)) := by
  have a := lsum_le K O.toCone _ _ (fun p => pt8w_le K O inv mul_inv h10 kz h1 p) l
  rw [lsum_mul_left, lsum_norms K (f4 K inv) kz l] at a
  exact le_trans' K O.toCone _ _ _ a (le_mul_nn K O.toCone _ _ _ (nn9 K O) (lattice_bound K O inv mul_inv h10 (l.map kz) (pw_map kz l hd)
    (fun k hk => by have ⟨q, _, e⟩ := mem_map_inv kz k l hk; rw [← e]; exact h1 q) M
    (fun k hk => by have ⟨q, hq, e⟩ := mem_map_inv kz k l hk; rw [← e]; exact hM q hq)))

theorem Z_le_B (kz : I → Mode) (h1 : ∀ p, 1 ≤ nrm (kz p)) (l : List I) (a : I → V3 R) :
    Le K O.toCone (lsum K (fun p => K.mul (wt K kz p) (dot3 K (a p) (a p))) l)
      (lsum K (fun p => K.mul (K.mul (K.mul (wt K kz p) (wt K kz p)) (wt K kz p)) (dot3 K (a p) (a p))) l) := by
  refine lsum_le K O.toCone _ _ (fun p => ?_) l
  have c1 := RatModel.cast_mono K O.toCone 1 _ (h1 p)
  have one1 : castN K 1 = K.one := by show K.add K.one K.zero = K.one; lib_ring
  rw [one1] at c1
  have sq := EnstLocal.sq_mono K O _ _ (RatModel.nn_one' K O.toCone) (RatModel.nn_cast K O.toCone _) c1
  have w1 := le_trans' K O.toCone _ _ _ sq (nrm_le_dot K O.toCone (kz p))
  rw [show K.mul K.one K.one = K.one by lib_ring] at w1
  cases w1 with
  | intro d hd =>
    refine ⟨K.mul (K.mul (K.mul (wt K kz p) d) (K.add (K.add K.one K.one) d)) (dot3 K (a p) (a p)),
      O.nn_mul _ _ (O.nn_mul _ _ (O.nn_mul _ _ (dot3_nn K O.toCone _) hd.1)
        (O.nn_add _ _ (O.nn_add _ _ (RatModel.nn_one' K O.toCone) (RatModel.nn_one' K O.toCone)) hd.1)) (dot3_nn K O.toCone _), ?_⟩
    have hw : wt K kz p = K.add K.one d := hd.2.symm
    rw [hw]; lib_ring

def c3 : R := K.mul (k8 K) (K.add (K.mul (k9 K) (castN K 135)) (castN K 135))

theorem Bform (kz : I → Mode) (l : List I) (a : I → V3 R) :
    lsum K (fun p => K.mul (K.mul (K.mul (wt K kz p) (wt K kz p)) (wt K kz p)) (dot3 K (a p) (a p))) l
      = lsum K (fun p => K.mul (K.mul (wt K kz p) (K.mul (wt K kz p) (wt K kz p))) (dot3 K (a p) (a p))) l :=
  lsum_ext K _ _ (fun p => by lib_ring) l

include mul_inv h10 in
/-- small-data global H³ bound, uniform in the truncation -/
theorem h3_global (l : List I) (kz : I → Mode) (hd : List.Pairwise (fun a b => kz a ≠ kz b) l)
    (h1 : ∀ p, 1 ≤ nrm (kz p)) (M : Nat) (hM : ∀ p, p ∈ l → nrm (kz p) ≤ M + 1)
    (ι h ν : R) (hh : O.nn h) (hν : O.nn ν) (hν0 : ν ≠ K.zero) (a N : Nat → I → V3 R)
    (hN : ∀ t, Le K O.toCone (lsum K (fun r => K.mul (K.mul (wt K kz r) (wt K kz r)) (dot3 K (N t r) (N t r))) l)
      (lsum K (fun r => K.mul (K.mul (wt K kz r) (wt K kz r)) (dot3 K (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (a t) (a t) r)
        (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (a t) (a t) r))) l))
    (hs : ∀ t r, Step K h ν (wt K kz r) (a (t + 1) r) (a t r) (N t r))
    (small : Le K O.toCone (K.mul (K.mul (c3 K) (K.mul ι ι))
      (Q K ν h (fun t => lsum K (fun p => K.mul (K.mul (wt K kz p) (K.mul (wt K kz p) (wt K kz p))) (dot3 K (a t p) (a t p))) l)
        (fun t => lsum K (fun p => K.mul (K.mul (K.mul (wt K kz p) (wt K kz p)) (K.mul (wt K kz p) (wt K kz p))) (dot3 K (a t p) (a t p))) l) 0))
      (K.mul ν (K.mul ν ν))) (t : Nat) :
    Le K O.toCone (Q K ν h (fun t => lsum K (fun p => K.mul (K.mul (wt K kz p) (K.mul (wt K kz p) (wt K kz p))) (dot3 K (a t p) (a t p))) l)
        (fun t => lsum K (fun p => K.mul (K.mul (K.mul (wt K kz p) (wt K kz p)) (K.mul (wt K kz p) (wt K kz p))) (dot3 K (a t p) (a t p))) l) t)
      (Q K ν h (fun t => lsum K (fun p => K.mul (K.mul (wt K kz p) (K.mul (wt K kz p) (wt K kz p))) (dot3 K (a t p) (a t p))) l)
        (fun t => lsum K (fun p => K.mul (K.mul (K.mul (wt K kz p) (wt K kz p)) (K.mul (wt K kz p) (wt K kz p))) (dot3 K (a t p) (a t p))) l) 0) := by
  have hw : ∀ r, O.nn (wt K kz r) := fun r => dot3_nn K O.toCone _
  have n135 : O.nn (castN K 135) := RatModel.nn_cast K O.toCone 135
  have nc3 : O.nn (c3 K) := O.nn_mul _ _ (nn8 K O) (O.nn_add _ _ (O.nn_mul _ _ (nn9 K O) n135) n135)
  have hc : O.nn (K.mul (c3 K) (K.mul ι ι)) := O.nn_mul _ _ nc3 (O.nn_sq ι)
  refine q_le K O ν h _ _ _ hν hν0 hh hc
    (fun t => lsum_nn K O.toCone _ (fun p => O.nn_mul _ _ (O.nn_mul _ _ (hw p) (O.nn_sq _)) (dot3_nn K O.toCone _)) l)
    (fun t => lsum_nn K O.toCone _ (fun p => O.nn_mul _ _ (O.nn_mul _ _ (O.nn_sq _) (O.nn_sq _)) (dot3_nn K O.toCone _)) l) ?_ small t
  intro s
  have st := enst_stepV K O.toCone l h ν hh hν (wt K kz) (fun r => K.mul (wt K kz r) (wt K kz r)) hw (fun r => O.nn_sq _)
    (a (s + 1)) (a s) (N s) (hs s)
  have fh := force_h3 K O inv mul_inv h10 l kz hd h1 ι (a s)
  have hS0 := S0h_le K O inv mul_inv h10 l kz hd h1 M hM
  have hS1 := S1h_le K O inv mul_inv h10 l kz hd h1 M hM
  have hZB := Z_le_B K O kz h1 l (a s)
  rw [Bform K kz l (a s)] at hZB fh
  have nB : O.nn (lsum K (fun p => K.mul (K.mul (wt K kz p) (K.mul (wt K kz p) (wt K kz p))) (dot3 K (a s p) (a s p))) l) :=
    lsum_nn K O.toCone _ (fun p => O.nn_mul _ _ (O.nn_mul _ _ (hw p) (O.nn_sq _)) (dot3_nn K O.toCone _)) l
  have nB4 : O.nn (lsum K (fun p => K.mul (K.mul (K.mul (wt K kz p) (wt K kz p)) (K.mul (wt K kz p) (wt K kz p))) (dot3 K (a s p) (a s p))) l) :=
    lsum_nn K O.toCone _ (fun p => O.nn_mul _ _ (O.nn_mul _ _ (O.nn_sq _) (O.nn_sq _)) (dot3_nn K O.toCone _)) l
  have nS1 : O.nn (lsum K (fun p => K.mul (K.mul (p4 K (ivn K inv kz p)) (p4 K (ivn K inv kz p))) (K.mul (wt K kz p) (wt K kz p))) l) :=
    lsum_nn K O.toCone _ (fun p => O.nn_mul _ _ (O.nn_sq _) (O.nn_sq _)) l
  have i1 := TruncRate.mm K O _ _ _ _ nS1 nB hS1 hZB
  have i2 := ForceSob.le_mul_right' K O.toCone hS0 nB
  have inner := le_mul_nn K O.toCone _ _ _ (nn8 K O) (le_add' K O.toCone _ _ _ _ i1 i2)
  have b1 := le_trans' K O.toCone _ _ _ (hN s) fh
  have b2 := le_trans' K O.toCone _ _ _ b1 (le_mul_nn K O.toCone _ _ _ (O.nn_sq ι) (le_mul_nn K O.toCone _ _ _ nB4 inner))
  have hb := le_mul_nn K O.toCone h _ _ hh b2
  have st2 := le_trans' K O.toCone _ _ _ st (le_add' K O.toCone _ _ _ _ (le_refl' K O.toCone _) hb)
  refine le_trans' K O.toCone _ _ _ st2 ⟨K.zero, O.nn_zero, ?_⟩
  unfold c3
  generalize lsum K (fun p => K.mul (K.mul (wt K kz p) (K.mul (wt K kz p) (wt K kz p))) (dot3 K (a s p) (a s p))) l = Bs
  generalize lsum K (fun p => K.mul (K.mul (K.mul (wt K kz p) (wt K kz p)) (K.mul (wt K kz p) (wt K kz p))) (dot3 K (a s p) (a s p))) l = B4
  generalize castN K 135 = c
  generalize k8 K = e8
  generalize k9 K = e9
  lib_ring

end Gen

end EnstH3

#print axioms EnstH3.nn9
#print axioms EnstH3.nn8
#print axioms EnstH3.pt8_le4
#print axioms EnstH3.pt8w_le
#print axioms EnstH3.S0h_le
#print axioms EnstH3.S1h_le
#print axioms EnstH3.Z_le_B
#print axioms EnstH3.Bform
#print axioms EnstH3.h3_global

ns/EnstH4.lean

lean_all/ns/EnstH4.lean

/-
  ns/EnstH4.lean — T37 cycle 1105 (goal G717, piece b6): the small-data global H⁴ bound for the implicit-viscosity
  Galerkin scheme, uniform in the truncation, over an ordered field. With B₄ = Σ w⁴|a|², B₅ = Σ w⁵|a|² (w = |k|²):
    S0f_le / S1f_le   Σ_p |k_p|_∞^{−10} ≤ 135,  Σ_p |k_p|_∞^{−10} w_p³ ≤ 27·135
    Z_le_B4           Σ w|a|² ≤ Σ w⁴|a|²
    h4_global         c₄ι²(νB₄_0 + hν²B₅_0) ≤ ν³,  c₄ = 32(27·135 + 135)  ⇒  νB₄_t + hν²B₅_t ≤ νB₄_0 + hν²B₅_0
  (EnstStepV with V = w³, ForceH4 with u = v = a, then EnstGlobal.q_le with c = c₄ι²).

  Lean 4 core plus ns/ForceH4, ns/EnstH3, ns/EnstStepV, ns/EnstGlobal, ns/LatticeBound.
-/
import ns.ForceH4
import ns.EnstH3

namespace EnstH4
open DetChain NSChain DyadicSmallGlobal LibRing DyadicLarge FourierGalerkin GalerkinForce ForceBound ForceSob
  ModeCount CubeCount ForceUniform NormCompare EnstGalerkin ForceTwoScale ForceSobW CastZAdd WeightH2 LatticeSum
  LatticeBound LatticeStep ForceH3 Tri4 EnstStep EnstStepV EnstGlobal ForceH4 EnstH3

section Gen
variable {R : Type} (K : CRing R) (O : OrdR K) {I : Type}
variable (inv : R → R) (mul_inv : ∀ y, y ≠ K.zero → K.mul y (inv y) = K.one) (h10 : K.one ≠ K.zero)

def k27 : R := K.mul (k9 K) (K.add (K.add K.one K.one) K.one)

theorem nn27 : O.nn (k27 K) :=
  O.nn_mul _ _ (nn9 K O) (O.nn_add _ _ (O.nn_add _ _ (RatModel.nn_one' K O.toCone) (RatModel.nn_one' K O.toCone)) (RatModel.nn_one' K O.toCone))

include mul_inv h10 in
theorem pt10_le4 (kz : I → Mode) (h1 : ∀ p, 1 ≤ nrm (kz p)) (p : I) :
    Le K O.toCone (K.mul (q5 K (ivn K inv kz p)) (q5 K (ivn K inv kz p))) (f4 K inv (nrm (kz p))) := by
  have ⟨_, hle⟩ := ivn_le K O inv mul_inv h10 kz h1 p
  have one := RatModel.nn_one' K O.toCone
  have q := TruncRate.mm K O _ _ _ _ (O.nn_sq (ivn K inv kz p)) one hle hle
  have q' := TruncRate.mm K O _ _ _ _ (O.nn_mul _ _ (O.nn_sq _) (O.nn_sq _)) one q hle
  have m := le_mul_nn K O.toCone (p4 K (ivn K inv kz p)) _ _ (by unfold p4; exact O.nn_mul _ _ (O.nn_sq _) (O.nn_sq _)) q'
  refine le_trans' K O.toCone _ _ _ ⟨K.zero, O.nn_zero, ?_⟩ (le_trans' K O.toCone _ _ _ m ⟨K.zero, O.nn_zero, ?_⟩)
  · unfold q5 p4; lib_ring
  · show K.add (K.mul (p4 K (inv (castN K (nrm (kz p))))) (K.mul (K.mul K.one K.one) K.one)) K.zero = p4 K (inv (castN K (nrm (kz p))))
    unfold p4; lib_ring

include mul_inv h10 in
theorem pt10w_le (kz : I → Mode) (h1 : ∀ p, 1 ≤ nrm (kz p)) (p : I) :
    Le K O.toCone (K.mul (K.mul (q5 K (ivn K inv kz p)) (q5 K (ivn K inv kz p))) (w3 K kz p))
      (K.mul (k27 K) (f4 K inv (nrm (kz p)))) := by
  have e := nn_ivn K O inv mul_inv h10 kz h1 p
  have w3' := dot_le_3nrm K O (kz p)
  have hw : O.nn (wt K kz p) := dot3_nn K O.toCone _
  have n3 : O.nn (K.add (K.add (K.mul (nn K kz p) (nn K kz p)) (K.mul (nn K kz p) (nn K kz p))) (K.mul (nn K kz p) (nn K kz p))) :=
    O.nn_add _ _ (O.nn_add _ _ (O.nn_sq _) (O.nn_sq _)) (O.nn_sq _)
  have w9 := TruncRate.mm K O _ _ _ _ hw n3 w3' w3'
  have w27 := TruncRate.mm K O _ _ _ _ (O.nn_sq _) n3 w9 w3'
  have m := le_mul_nn K O.toCone (K.mul (q5 K (ivn K inv kz p)) (q5 K (ivn K inv kz p))) _ _ (O.nn_sq _) w27
  refine le_trans' K O.toCone _ _ _ m ⟨K.zero, O.nn_zero, ?_⟩
  show K.add (K.mul (K.mul (q5 K (ivn K inv kz p)) (q5 K (ivn K inv kz p)))
      (K.mul (K.mul (K.add (K.add (K.mul (nn K kz p) (nn K kz p)) (K.mul (nn K kz p) (nn K kz p))) (K.mul (nn K kz p) (nn K kz p)))
        (K.add (K.add (K.mul (nn K kz p) (nn K kz p)) (K.mul (nn K kz p) (nn K kz p))) (K.mul (nn K kz p) (nn K kz p))))
        (K.add (K.add (K.mul (nn K kz p) (nn K kz p)) (K.mul (nn K kz p) (nn K kz p))) (K.mul (nn K kz p) (nn K kz p))))) K.zero
    = K.mul (k27 K) (p4 K (ivn K inv kz p))
  unfold q5 p4 k27 k9
  generalize nn K kz p = n at e
  generalize ivn K inv kz p = v at e
  calc _ = K.mul (K.mul (K.mul (K.add (K.add K.one K.one) K.one) (K.add (K.add K.one K.one) K.one)) (K.add (K.add K.one K.one) K.one))
        (K.mul (K.mul (K.mul v v) (K.mul v v)) (K.mul (K.mul (K.mul (K.mul n v) (K.mul n v)) (K.mul (K.mul n v) (K.mul n v))) (K.mul (K.mul n v) (K.mul n v)))) := by lib_ring
    _ = _ := by rw [e]; lib_ring

include mul_inv h10 in
theorem S0f_le (l : List I) (kz : I → Mode) (hd : List.Pairwise (fun a b => kz a ≠ kz b) l)
    (h1 : ∀ p, 1 ≤ nrm (kz p)) (M : Nat) (hM : ∀ p, p ∈ l → nrm (kz p) ≤ M + 1) :
    Le K O.toCone (lsum K (fun p => K.mul (q5 K (ivn K inv kz p)) (q5 K (ivn K inv kz p))) l) (castN K 135) := by
  have a := lsum_le K O.toCone _ _ (fun p => pt10_le4 K O inv mul_inv h10 kz h1 p) l
  rw [lsum_norms K (f4 K inv) kz l] at a
  exact le_trans' K O.toCone _ _ _ a (lattice_bound K O inv mul_inv h10 (l.map kz) (pw_map kz l hd)
    (fun k hk => by have ⟨q, _, e⟩ := mem_map_inv kz k l hk; rw [← e]; exact h1 q) M
    (fun k hk => by have ⟨q, hq, e⟩ := mem_map_inv kz k l hk; rw [← e]; exact hM q hq))

include mul_inv h10 in
theorem S1f_le (l : List I) (kz : I → Mode) (hd : List.Pairwise (fun a b => kz a ≠ kz b) l)
    (h1 : ∀ p, 1 ≤ nrm (kz p)) (M : Nat) (hM : ∀ p, p ∈ l → nrm (kz p) ≤ M + 1) :
    Le K O.toCone (lsum K (fun p => K.mul (K.mul (q5 K (ivn K inv kz p)) (q5 K (ivn K inv kz p))) (w3 K kz p)) l)
      (K.mul (k27 K) (castN K 135)) := by
  have a := lsum_le K O.toCone _ _ (fun p => pt10w_le K O inv mul_inv h10 kz h1 p) l
  rw [lsum_mul_left, lsum_norms K (f4 K inv) kz l] at a
  exact le_trans' K O.toCone _ _ _ a (le_mul_nn K O.toCone _ _ _ (nn27 K O) (lattice_bound K O inv mul_inv h10 (l.map kz) (pw_map kz l hd)
    (fun k hk => by have ⟨q, _, e⟩ := mem_map_inv kz k l hk; rw [← e]; exact h1 q) M
    (fun k hk => by have ⟨q, hq, e⟩ := mem_map_inv kz k l hk; rw [← e]; exact hM q hq)))

theorem Z_le_B4 (kz : I → Mode) (h1 : ∀ p, 1 ≤ nrm (kz p)) (l : List I) (a : I → V3 R) :
    Le K O.toCone (lsum K (fun p => K.mul (wt K kz p) (dot3 K (a p) (a p))) l)
      (lsum K (fun p => K.mul (K.mul (wt K kz p) (w3 K kz p)) (dot3 K (a p) (a p))) l) := by
  refine lsum_le K O.toCone _ _ (fun p => ?_) l
  have c1 := RatModel.cast_mono K O.toCone 1 _ (h1 p)
  have one1 : castN K 1 = K.one := by show K.add K.one K.zero = K.one; lib_ring
  rw [one1] at c1
  have sq := EnstLocal.sq_mono K O _ _ (RatModel.nn_one' K O.toCone) (RatModel.nn_cast K O.toCone _) c1
  have w1 := le_trans' K O.toCone _ _ _ sq (nrm_le_dot K O.toCone (kz p))
  rw [show K.mul K.one K.one = K.one by lib_ring] at w1
  have hw : O.nn (wt K kz p) := dot3_nn K O.toCone _
  cases w1 with
  | intro d hd =>
    refine ⟨K.mul (K.mul (K.mul (wt K kz p) d) (K.add (K.add (K.mul (wt K kz p) (wt K kz p)) (wt K kz p)) K.one)) (dot3 K (a p) (a p)),
      O.nn_mul _ _ (O.nn_mul _ _ (O.nn_mul _ _ hw hd.1)
        (O.nn_add _ _ (O.nn_add _ _ (O.nn_sq _) hw) (RatModel.nn_one' K O.toCone))) (dot3_nn K O.toCone _), ?_⟩
    unfold w3
    have hw' : wt K kz p = K.add K.one d := hd.2.symm
    rw [hw']; lib_ring

def c4 : R := K.mul (k32 K) (K.add (K.mul (k27 K) (castN K 135)) (castN K 135))

theorem B5form (kz : I → Mode) (l : List I) (a : I → V3 R) :
    lsum K (fun p => K.mul (K.mul (K.mul (K.mul (wt K kz p) (wt K kz p)) (K.mul (wt K kz p) (wt K kz p))) (wt K kz p)) (dot3 K (a p) (a p))) l
      = lsum K (fun p => K.mul (K.mul (K.mul (wt K kz p) (wt K kz p)) (w3 K kz p)) (dot3 K (a p) (a p))) l :=
  lsum_ext K _ _ (fun p => by unfold w3; lib_ring) l

theorem B4form (kz : I → Mode) (l : List I) (a : I → V3 R) :
    lsum K (fun p => K.mul (K.mul (w3 K kz p) (wt K kz p)) (dot3 K (a p) (a p))) l
      = lsum K (fun p => K.mul (K.mul (wt K kz p) (w3 K kz p)) (dot3 K (a p) (a p))) l :=
  lsum_ext K _ _ (fun p => by lib_ring) l

include mul_inv h10 in
/-- small-data global H⁴ bound, uniform in the truncation -/
theorem h4_global (l : List I) (kz : I → Mode) (hd : List.Pairwise (fun a b => kz a ≠ kz b) l)
    (h1 : ∀ p, 1 ≤ nrm (kz p)) (M : Nat) (hM : ∀ p, p ∈ l → nrm (kz p) ≤ M + 1)
    (ι h ν : R) (hh : O.nn h) (hν : O.nn ν) (hν0 : ν ≠ K.zero) (a N : Nat → I → V3 R)
    (hN : ∀ t, Le K O.toCone (lsum K (fun r => K.mul (w3 K kz r) (dot3 K (N t r) (N t r))) l)
      (lsum K (fun r => K.mul (w3 K kz r) (dot3 K (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (a t) (a t) r)
        (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (a t) (a t) r))) l))
    (hs : ∀ t r, Step K h ν (wt K kz r) (a (t + 1) r) (a t r) (N t r))
    (small : Le K O.toCone (K.mul (K.mul (c4 K) (K.mul ι ι))
      (Q K ν h (fun t => lsum K (fun p => K.mul (K.mul (wt K kz p) (w3 K kz p)) (dot3 K (a t p) (a t p))) l)
        (fun t => lsum K (fun p => K.mul (K.mul (K.mul (wt K kz p) (wt K kz p)) (w3 K kz p)) (dot3 K (a t p) (a t p))) l) 0))
      (K.mul ν (K.mul ν ν))) (t : Nat) :
    Le K O.toCone (Q K ν h (fun t => lsum K (fun p => K.mul (K.mul (wt K kz p) (w3 K kz p)) (dot3 K (a t p) (a t p))) l)
        (fun t => lsum K (fun p => K.mul (K.mul (K.mul (wt K kz p) (wt K kz p)) (w3 K kz p)) (dot3 K (a t p) (a t p))) l) t)
      (Q K ν h (fun t => lsum K (fun p => K.mul (K.mul (wt K kz p) (w3 K kz p)) (dot3 K (a t p) (a t p))) l)
        (fun t => lsum K (fun p => K.mul (K.mul (K.mul (wt K kz p) (wt K kz p)) (w3 K kz p)) (dot3 K (a t p) (a t p))) l) 0) := by
  have hw : ∀ r, O.nn (wt K kz r) := fun r => dot3_nn K O.toCone _
  have hw3 : ∀ r, O.nn (w3 K kz r) := fun r => O.nn_mul _ _ (O.nn_sq _) (hw r)
  have n135 : O.nn (castN K 135) := RatModel.nn_cast K O.toCone 135
  have nc4 : O.nn (c4 K) := O.nn_mul _ _ (nn32 K O) (O.nn_add _ _ (O.nn_mul _ _ (nn27 K O) n135) n135)
  have hc : O.nn (K.mul (c4 K) (K.mul ι ι)) := O.nn_mul _ _ nc4 (O.nn_sq ι)
  refine q_le K O ν h _ _ _ hν hν0 hh hc
    (fun t => lsum_nn K O.toCone _ (fun p => O.nn_mul _ _ (O.nn_mul _ _ (hw p) (hw3 p)) (dot3_nn K O.toCone _)) l)
    (fun t => lsum_nn K O.toCone _ (fun p => O.nn_mul _ _ (O.nn_mul _ _ (O.nn_sq _) (hw3 p)) (dot3_nn K O.toCone _)) l) ?_ small t
  intro s
  have st := enst_stepV K O.toCone l h ν hh hν (wt K kz) (w3 K kz) hw hw3 (a (s + 1)) (a s) (N s) (hs s)
  have fh := force_h4 K O inv mul_inv h10 l kz hd h1 ι (a s) (a s)
  rw [B5form K kz l (a s), B4form K kz l (a s)] at fh
  have hS0 := S0f_le K O inv mul_inv h10 l kz hd h1 M hM
  have hS1 := S1f_le K O inv mul_inv h10 l kz hd h1 M hM
  have hZB := Z_le_B4 K O kz h1 l (a s)
  have nB4 : O.nn (lsum K (fun p => K.mul (K.mul (wt K kz p) (w3 K kz p)) (dot3 K (a s p) (a s p))) l) :=
    lsum_nn K O.toCone _ (fun p => O.nn_mul _ _ (O.nn_mul _ _ (hw p) (hw3 p)) (dot3_nn K O.toCone _)) l
  have nB5 : O.nn (lsum K (fun p => K.mul (K.mul (K.mul (wt K kz p) (wt K kz p)) (w3 K kz p)) (dot3 K (a s p) (a s p))) l) :=
    lsum_nn K O.toCone _ (fun p => O.nn_mul _ _ (O.nn_mul _ _ (O.nn_sq _) (hw3 p)) (dot3_nn K O.toCone _)) l
  have nS1 : O.nn (lsum K (fun p => K.mul (K.mul (q5 K (ivn K inv kz p)) (q5 K (ivn K inv kz p))) (w3 K kz p)) l) :=
    lsum_nn K O.toCone _ (fun p => O.nn_mul _ _ (O.nn_sq _) (hw3 p)) l
  have i1 := TruncRate.mm K O _ _ _ _ nS1 nB4 hS1 hZB
  have i2 := ForceSob.le_mul_right' K O.toCone hS0 nB4
  have inner := le_mul_nn K O.toCone _ _ _ (nn32 K O) (le_add' K O.toCone _ _ _ _ i1 i2)
  have b1 := le_trans' K O.toCone _ _ _ (hN s) fh
  have b2 := le_trans' K O.toCone _ _ _ b1 (le_mul_nn K O.toCone _ _ _ (O.nn_sq ι) (le_mul_nn K O.toCone _ _ _ nB5 inner))
  have hb := le_mul_nn K O.toCone h _ _ hh b2
  have st2 := le_trans' K O.toCone _ _ _ st (le_add' K O.toCone _ _ _ _ (le_refl' K O.toCone _) hb)
  refine le_trans' K O.toCone _ _ _ st2 ⟨K.zero, O.nn_zero, ?_⟩
  unfold c4
  generalize lsum K (fun p => K.mul (K.mul (wt K kz p) (w3 K kz p)) (dot3 K (a s p) (a s p))) l = B4
  generalize lsum K (fun p => K.mul (K.mul (K.mul (wt K kz p) (wt K kz p)) (w3 K kz p)) (dot3 K (a s p) (a s p))) l = B5
  generalize castN K 135 = c
  generalize k32 K = e32
  generalize k27 K = e27
  lib_ring

end Gen

end EnstH4

#print axioms EnstH4.nn27
#print axioms EnstH4.pt10_le4
#print axioms EnstH4.pt10w_le
#print axioms EnstH4.S0f_le
#print axioms EnstH4.S1f_le
#print axioms EnstH4.Z_le_B4
#print axioms EnstH4.B5form
#print axioms EnstH4.B4form
#print axioms EnstH4.h4_global

ns/EnstLipschitz.lean

lean_all/ns/EnstLipschitz.lean

/-
  ns/EnstLipschitz.lean — T37 cycle 1035 (goal G671, piece d — done): continuous dependence of the implicit-viscosity
  Fourier–Galerkin scheme in the enstrophy norm, with factors independent of the truncation, over an ordered field.
  Two solutions a, b (forces N^a, N^b with Σ|N^a_t − N^b_t|² ≤ Σ|Nl(a_t,a_t) − Nl(b_t,b_t)|², e.g. Leray-projected),
  d_t = a_t − b_t, Qd_t = νZ(d_t) + hν²A(d_t). With
    α_t = 2·2ι²C₁·Z(a_t) + 2ι²·135·A(b_t),   β_t = 2·2ι²C₂·Z(a_t)  and  β_t ≤ ν² (cutoffs, Z(a_t) ≤ Zb),
  for every T:
    lipschitz   ν^T · Qd_T ≤ Π_{t<T}(ν + hα_t) · Qd_0.
  (EnstDiff.step_sub + EnstStep.enst_step + ForceDiff.force_diff + EnstGronwall.diff_gronwall.)

  Lean 4 core plus ns/ForceDiff, ns/EnstGronwall, ns/EnstGalerkin.
-/
import ns.ForceDiff
import ns.EnstGronwall

namespace EnstLipschitz
open DetChain NSChain DyadicSmallGlobal LibRing DyadicLarge FourierGalerkin GalerkinForce ForceBound ForceSob
  ModeCount CubeCount ForceUniform NormCompare EnstStep EnstGlobal EnstGalerkin ForceTwoScale ForceBilin EnstDiff
  ForceDiff EnstGronwall LatticeSum

section Gen
variable {R : Type} (K : CRing R) (O : OrdR K) {I : Type}
variable (inv : R → R) (mul_inv : ∀ y, y ≠ K.zero → K.mul y (inv y) = K.one) (h10 : K.one ≠ K.zero)

def C1 (Rc : Nat) : R := K.add (K.mul (castN K (cube Rc)) K.one) (K.mul (castN K 135) (iv K inv (Rc + 1)))
def C2 (Rc t0 : Nat) : R := K.add (K.mul (castN K (cube Rc)) (K.mul (inv (castN K (t0 + 1))) (inv (castN K (t0 + 1)))))
  (K.mul (castN K 135) (iv K inv (Rc + 1)))
def two : R := K.add K.one K.one

def dS (a b : Nat → I → V3 R) (t : Nat) (p : I) : V3 R := dF K (a t) (b t) p

include mul_inv h10 in
theorem lipschitz (l : List I) (kz : I → Mode) (hd : List.Pairwise (fun a b => kz a ≠ kz b) l)
    (h1 : ∀ p, 1 ≤ nrm (kz p)) (M : Nat) (hM : ∀ p, p ∈ l → nrm (kz p) ≤ M + 1) (Rc t0 : Nat) (hRM : Rc ≤ M)
    (ι h ν : R) (hh : O.nn h) (hν : O.nn ν) (a b Na Nb : Nat → I → V3 R)
    (hN : ∀ t, Le K O.toCone (lsum K (fun r => dot3 K (sub3 K (Na t r) (Nb t r)) (sub3 K (Na t r) (Nb t r))) l)
      (lsum K (fun r => dot3 K (sub3 K (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (a t) (a t) r)
          (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (b t) (b t) r))
        (sub3 K (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (a t) (a t) r)
          (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (b t) (b t) r))) l))
    (ha : ∀ t r, Step K h ν (wt K kz r) (a (t + 1) r) (a t r) (Na t r))
    (hb : ∀ t r, Step K h ν (wt K kz r) (b (t + 1) r) (b t r) (Nb t r))
    (hβ : ∀ t, Le K O.toCone (K.mul (two K) (K.mul (K.mul (two K) (K.mul ι ι)) (K.mul (C2 K inv Rc t0) (Zs K l kz a t))))
      (K.mul ν ν))
    (T : Nat) :
    Le K O.toCone (K.mul (pwr K ν T) (Q K ν h (Zs K l kz (dS K a b)) (As K l kz (dS K a b)) T))
      (K.mul (prodN K (fun t => K.add ν (K.mul h
          (K.add (K.mul (two K) (K.mul (K.mul (two K) (K.mul ι ι)) (K.mul (C1 K inv Rc) (Zs K l kz a t))))
            (K.mul (two K) (K.mul (K.mul ι ι) (K.mul (castN K 135) (As K l kz b t))))))) T)
        (Q K ν h (Zs K l kz (dS K a b)) (As K l kz (dS K a b)) 0)) := by
  have hw : ∀ r, O.nn (wt K kz r) := fun r => dot3_nn K O.toCone _
  have hZn : ∀ (u : Nat → I → V3 R) t, O.nn (Zs K l kz u t) := fun u t =>
    lsum_nn K O.toCone _ (fun r => O.nn_mul _ _ (hw r) (dot3_nn K O.toCone _)) l
  have hAn : ∀ (u : Nat → I → V3 R) t, O.nn (As K l kz u t) := fun u t =>
    lsum_nn K O.toCone _ (fun r => O.nn_mul _ _ (O.nn_sq _) (dot3_nn K O.toCone _)) l
  have hiv : O.nn (iv K inv (Rc + 1)) := iv_nn K O inv mul_inv h10 Rc
  have n2 : O.nn (two K) := O.nn_add _ _ (RatModel.nn_one' K O.toCone) (RatModel.nn_one' K O.toCone)
  have nC1 : O.nn (C1 K inv Rc) := O.nn_add _ _ (O.nn_mul _ _ (RatModel.nn_cast K O.toCone _) (RatModel.nn_one' K O.toCone))
    (O.nn_mul _ _ (RatModel.nn_cast K O.toCone _) hiv)
  refine diff_gronwall K O ν h _ _ _ _ hν hh (hAn (dS K a b)) (fun t => O.nn_add _ _
    (O.nn_mul _ _ n2 (O.nn_mul _ _ (O.nn_mul _ _ n2 (O.nn_sq ι)) (O.nn_mul _ _ nC1 (hZn a t))))
    (O.nn_mul _ _ n2 (O.nn_mul _ _ (O.nn_sq ι) (O.nn_mul _ _ (RatModel.nn_cast K O.toCone 135) (hAn b t))))) hβ ?_ T
  intro s
  have st := enst_step K O.toCone l h ν hh hν (wt K kz) hw (dS K a b (s + 1)) (dS K a b s)
    (fun r => sub3 K (Na s r) (Nb s r)) (fun r => step_sub K h ν (wt K kz r) _ _ _ _ _ _ (ha s r) (hb s r))
  have fd := force_diff K O inv mul_inv h10 l kz hd h1 M hM Rc t0 hRM ι (a s) (b s)
  have hb2 := le_mul_nn K O.toCone h _ _ hh (le_trans' K O.toCone _ _ _ (hN s) fd)
  have st2 := le_trans' K O.toCone _ _ _ st (le_add' K O.toCone _ _ _ _ (le_refl' K O.toCone _) hb2)
  refine le_trans' K O.toCone _ _ _ st2 ⟨K.zero, O.nn_zero, ?_⟩
  show K.add (K.add (K.mul ν (Zs K l kz (dS K a b) s)) (K.mul h (K.add
      (K.mul (two K) (K.add
        (K.mul (two K) (K.mul (K.mul ι ι) (K.mul (Zs K l kz (dS K a b) s) (K.mul (C1 K inv Rc) (Zs K l kz a s)))))
        (K.mul (two K) (K.mul (K.mul ι ι) (K.mul (As K l kz (dS K a b) s) (K.mul (C2 K inv Rc t0) (Zs K l kz a s)))))))
      (K.mul (two K) (K.mul (K.mul ι ι) (K.mul (As K l kz b s) (K.mul (castN K 135) (Zs K l kz (dS K a b) s)))))))) K.zero
    = K.add (K.mul ν (Zs K l kz (dS K a b) s)) (K.mul h (K.add
      (K.mul (K.add (K.mul (two K) (K.mul (K.mul (two K) (K.mul ι ι)) (K.mul (C1 K inv Rc) (Zs K l kz a s))))
          (K.mul (two K) (K.mul (K.mul ι ι) (K.mul (castN K 135) (As K l kz b s))))) (Zs K l kz (dS K a b) s))
      (K.mul (K.mul (two K) (K.mul (K.mul (two K) (K.mul ι ι)) (K.mul (C2 K inv Rc t0) (Zs K l kz a s))))
        (As K l kz (dS K a b) s))))
  lib_ring

end Gen

end EnstLipschitz

#print axioms EnstLipschitz.lipschitz

ns/EnstLocal.lean

lean_all/ns/EnstLocal.lean

/-
  ns/EnstLocal.lean — T37 cycle 995 (goal G648, piece c): the large-data local-in-time enstrophy bound, abstract form,
  over an ordered ring without zero divisors. Sequences Z_t, A_t ≥ 0, constants ν > 0, h, c₁, c₂ ≥ 0, a level Zb ≥ 0:
    step     νZ_{t+1} + hν²A_{t+1} ≤ νZ_t + h(c₁ Z_t Z_t + c₂ A_t Z_t)     (EnstStep + ForceTwoScale)
    choice   c₂ Zb ≤ ν²                                                   (cutoff R, T chosen from Zb)
    budget   Q_0 + n·h·c₁·Zb² ≤ ν Zb,          Q_t = νZ_t + hν²A_t
  ⇒ for every t ≤ n:  Q_t ≤ Q_0 + t·h·c₁·Zb²  and  Z_t ≤ Zb.
  No smallness of the data: with Zb = 2Q_0/ν the number of steps n·h ~ ν²/(c₁ Q_0) is a time depending only on Q_0
  (and on R through c₁), not on the truncation.

  Lean 4 core plus ns/EnstGlobal (Q, le_of_mul_le), ns/RatModel (cast_mono), ns/ForceSob (le_mul_right').
-/
import ns.EnstGlobal
import ns.RatModel
import ns.ForceSob

namespace EnstLocal
open DetChain NSChain DyadicSmallGlobal LibRing DyadicLarge EnstGlobal

section Gen
variable {R : Type} (K : CRing R) (O : OrdR K)

theorem sq_mono (x y : R) (hx : O.nn x) (hy : O.nn y) (h : Le K O.toCone x y) :
    Le K O.toCone (K.mul x x) (K.mul y y) := by
  have a := le_mul_nn K O.toCone x _ _ hx h
  have b := le_mul_nn K O.toCone y _ _ hy h
  rw [K.mul_comm y x] at b
  exact le_trans' K O.toCone _ _ _ a b

theorem local_bound (ν h c1 c2 Zb : R) (Z A : Nat → R) (hν : O.nn ν) (hν0 : ν ≠ K.zero) (hh : O.nn h)
    (hc1 : O.nn c1) (hc2 : O.nn c2) (hZb : O.nn Zb) (hZ : ∀ t, O.nn (Z t)) (hA : ∀ t, O.nn (A t))
    (step : ∀ t, Le K O.toCone (Q K ν h Z A (t + 1))
      (K.add (K.mul ν (Z t)) (K.mul h (K.add (K.mul c1 (K.mul (Z t) (Z t))) (K.mul c2 (K.mul (A t) (Z t)))))))
    (choice : Le K O.toCone (K.mul c2 Zb) (K.mul ν ν)) (n : Nat)
    (budget : Le K O.toCone (K.add (Q K ν h Z A 0) (K.mul (castN K n) (K.mul h (K.mul c1 (K.mul Zb Zb))))) (K.mul ν Zb)) :
    ∀ t, t ≤ n → Le K O.toCone (Q K ν h Z A t) (K.add (Q K ν h Z A 0) (K.mul (castN K t) (K.mul h (K.mul c1 (K.mul Zb Zb)))))
  | 0, _ => ⟨K.zero, O.nn_zero, by show K.add (Q K ν h Z A 0) K.zero = K.add (Q K ν h Z A 0) (K.mul K.zero _); lib_ring⟩
  | t + 1, ht => by
    have ih := local_bound ν h c1 c2 Zb Z A hν hν0 hh hc1 hc2 hZb hZ hA step choice n budget t (Nat.le_of_succ_le ht)
    have hD : O.nn (K.mul h (K.mul c1 (K.mul Zb Zb))) := O.nn_mul _ _ hh (O.nn_mul _ _ hc1 (O.nn_sq Zb))
    -- Q_t ≤ Q_0 + nD ≤ νZb, so Z_t ≤ Zb
    have tn : Le K O.toCone (K.mul (castN K t) (K.mul h (K.mul c1 (K.mul Zb Zb))))
        (K.mul (castN K n) (K.mul h (K.mul c1 (K.mul Zb Zb)))) :=
      ForceSob.le_mul_right' K O.toCone (RatModel.cast_mono K O.toCone t n (Nat.le_of_succ_le ht)) hD
    have q1 := le_trans' K O.toCone _ _ _ ih (le_trans' K O.toCone _ _ _
      (le_add' K O.toCone _ _ _ _ (le_refl' K O.toCone _) tn) budget)
    have hz : Le K O.toCone (K.mul ν (Z t)) (Q K ν h Z A t) :=
      ⟨K.mul (K.mul h (K.mul ν ν)) (A t), O.nn_mul _ _ (O.nn_mul _ _ hh (O.nn_sq ν)) (hA t), rfl⟩
    have zt : Le K O.toCone (Z t) Zb := le_of_mul_le K O ν _ _ hν hν0 (le_trans' K O.toCone _ _ _ hz q1)
    -- c₁Z_t² ≤ c₁Zb²,  c₂A_tZ_t ≤ ν²A_t
    have s1 := le_mul_nn K O.toCone c1 _ _ hc1 (sq_mono K O _ _ (hZ t) hZb zt)
    have c2z := le_trans' K O.toCone _ _ _ (le_mul_nn K O.toCone c2 _ _ hc2 zt) choice
    have s2 := le_mul_nn K O.toCone (A t) _ _ (hA t) c2z
    have e2 : K.mul (A t) (K.mul c2 (Z t)) = K.mul c2 (K.mul (A t) (Z t)) := by lib_ring
    rw [e2] at s2
    have s3 := le_mul_nn K O.toCone h _ _ hh (le_add' K O.toCone _ _ _ _ s1 s2)
    have s4 := le_trans' K O.toCone _ _ _ (step t) (le_add' K O.toCone _ _ _ _ (le_refl' K O.toCone (K.mul ν (Z t))) s3)
    -- νZ_t + h(c₁Zb² + A_tν²) = Q_t + D ≤ Q_0 + tD + D
    have e4 : K.add (K.mul ν (Z t)) (K.mul h (K.add (K.mul c1 (K.mul Zb Zb)) (K.mul (A t) (K.mul ν ν))))
        = K.add (Q K ν h Z A t) (K.mul h (K.mul c1 (K.mul Zb Zb))) := by
      show _ = K.add (K.add (K.mul ν (Z t)) (K.mul (K.mul h (K.mul ν ν)) (A t))) _
      lib_ring
    rw [e4] at s4
    have s5 := le_trans' K O.toCone _ _ _ s4 (le_add' K O.toCone _ _ _ _ ih (le_refl' K O.toCone (K.mul h (K.mul c1 (K.mul Zb Zb)))))
    refine le_trans' K O.toCone _ _ _ s5 ⟨K.zero, O.nn_zero, ?_⟩
    show K.add (K.add (K.add (Q K ν h Z A 0) (K.mul (castN K t) (K.mul h (K.mul c1 (K.mul Zb Zb)))))
      (K.mul h (K.mul c1 (K.mul Zb Zb)))) K.zero
      = K.add (Q K ν h Z A 0) (K.mul (K.add K.one (castN K t)) (K.mul h (K.mul c1 (K.mul Zb Zb))))
    lib_ring

end Gen

end EnstLocal

#print axioms EnstLocal.sq_mono
#print axioms EnstLocal.local_bound

ns/EnstLocalGalerkin.lean

lean_all/ns/EnstLocalGalerkin.lean

/-
  ns/EnstLocalGalerkin.lean — T37 cycle 997 (goal G648, piece d): N-uniform local-in-time enstrophy bound for the
  implicit-viscosity Fourier–Galerkin scheme of 3D Navier–Stokes with arbitrary (large) data, over an ordered field.
  Scheme (1 + hν|k_r|²) a^{t+1}_r = a^t_r + h N^t_r with Σ_r|N^t_r|² ≤ Σ_r|Nl_r(a^t, a^t)|² (e.g. the Leray-projected
  Galerkin force). For a cutoff R ≤ M, T = t₀ + 1, a level Zb ≥ 0 with c₂Zb ≤ ν² and Q_0 + n·h·c₁Zb² ≤ νZb, where
    c₁ = 2ι²((2R+1)³ + 135/(R+1)),   c₂ = 2ι²((2R+1)³/T² + 135/(R+1)),
  every t ≤ n has Q_t ≤ Q_0 + t·h·c₁Zb² and Z_t ≤ Zb (local_galerkin). The constants do not depend on the number of
  modes: given Q_0, pick Zb = 2Q_0/ν, then R and T so that c₂Zb ≤ ν², and the time n·h ≤ Q_0/(c₁Zb²).

  Lean 4 core plus ns/ForceTwoScale, ns/EnstLocal, ns/EnstStep, ns/EnstGalerkin.
-/
import ns.ForceTwoScale
import ns.EnstLocal

namespace EnstLocalGalerkin
open DetChain NSChain DyadicSmallGlobal LibRing DyadicLarge FourierGalerkin GalerkinForce ForceBound ForceSob
  ModeCount CubeCount ForceUniform NormCompare EnstStep EnstGlobal EnstGalerkin ForceTwoScale EnstLocal LatticeSum

section Gen
variable {R : Type} (K : CRing R) (O : OrdR K) {I : Type}
variable (inv : R → R) (mul_inv : ∀ y, y ≠ K.zero → K.mul y (inv y) = K.one) (h10 : K.one ≠ K.zero)

def C1 (Rc : Nat) : R := K.add (K.mul (castN K (cube Rc)) K.one) (K.mul (castN K 135) (iv K inv (Rc + 1)))
def C2 (Rc t0 : Nat) : R := K.add (K.mul (castN K (cube Rc)) (K.mul (inv (castN K (t0 + 1))) (inv (castN K (t0 + 1)))))
  (K.mul (castN K 135) (iv K inv (Rc + 1)))
def cc (ι C : R) : R := K.mul (K.mul (K.add K.one K.one) (K.mul ι ι)) C

include mul_inv h10 in
theorem local_galerkin (l : List I) (kz : I → Mode) (hd : List.Pairwise (fun a b => kz a ≠ kz b) l)
    (h1 : ∀ p, 1 ≤ nrm (kz p)) (M : Nat) (hM : ∀ p, p ∈ l → nrm (kz p) ≤ M + 1) (Rc t0 : Nat) (hRM : Rc ≤ M)
    (ι h ν Zb : R) (hh : O.nn h) (hν : O.nn ν) (hν0 : ν ≠ K.zero) (hZb : O.nn Zb)
    (a N : Nat → I → V3 R)
    (hN : ∀ t, Le K O.toCone (lsum K (fun r => dot3 K (N t r) (N t r)) l)
      (lsum K (fun r => dot3 K (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (a t) (a t) r)
        (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (a t) (a t) r)) l))
    (hs : ∀ t r, Step K h ν (wt K kz r) (a (t + 1) r) (a t r) (N t r))
    (choice : Le K O.toCone (K.mul (cc K ι (C2 K inv Rc t0)) Zb) (K.mul ν ν)) (n : Nat)
    (budget : Le K O.toCone (K.add (Q K ν h (Zs K l kz a) (As K l kz a) 0)
      (K.mul (castN K n) (K.mul h (K.mul (cc K ι (C1 K inv Rc)) (K.mul Zb Zb))))) (K.mul ν Zb))
    (t : Nat) (ht : t ≤ n) :
    Le K O.toCone (Q K ν h (Zs K l kz a) (As K l kz a) t)
      (K.add (Q K ν h (Zs K l kz a) (As K l kz a) 0) (K.mul (castN K t) (K.mul h (K.mul (cc K ι (C1 K inv Rc)) (K.mul Zb Zb))))) := by
  have hw : ∀ r, O.nn (wt K kz r) := fun r => dot3_nn K O.toCone _
  have hZ : ∀ t, O.nn (Zs K l kz a t) := fun t =>
    lsum_nn K O.toCone _ (fun r => O.nn_mul _ _ (hw r) (dot3_nn K O.toCone _)) l
  have hA : ∀ t, O.nn (As K l kz a t) := fun t =>
    lsum_nn K O.toCone _ (fun r => O.nn_mul _ _ (O.nn_sq _) (dot3_nn K O.toCone _)) l
  have hiv : O.nn (iv K inv (Rc + 1)) := iv_nn K O inv mul_inv h10 Rc
  have two : O.nn (K.add K.one K.one) := O.nn_add _ _ (RatModel.nn_one' K O.toCone) (RatModel.nn_one' K O.toCone)
  have hc1 : O.nn (cc K ι (C1 K inv Rc)) := O.nn_mul _ _ (O.nn_mul _ _ two (O.nn_sq ι))
    (O.nn_add _ _ (O.nn_mul _ _ (RatModel.nn_cast K O.toCone _) (RatModel.nn_one' K O.toCone))
      (O.nn_mul _ _ (RatModel.nn_cast K O.toCone _) hiv))
  have hc2 : O.nn (cc K ι (C2 K inv Rc t0)) := O.nn_mul _ _ (O.nn_mul _ _ two (O.nn_sq ι))
    (O.nn_add _ _ (O.nn_mul _ _ (RatModel.nn_cast K O.toCone _) (O.nn_sq _))
      (O.nn_mul _ _ (RatModel.nn_cast K O.toCone _) hiv))
  refine local_bound K O ν h _ _ Zb (Zs K l kz a) (As K l kz a) hν hν0 hh hc1 hc2 hZb hZ hA ?_ choice n budget t ht
  intro s
  have st := enst_step K O.toCone l h ν hh hν (wt K kz) hw (a (s + 1)) (a s) (N s) (hs s)
  have ts := two_scale K O inv mul_inv h10 l kz hd h1 M hM Rc t0 hRM ι (a s) (a s)
  have hb := le_mul_nn K O.toCone h _ _ hh (le_trans' K O.toCone _ _ _ (hN s) ts)
  have st2 := le_trans' K O.toCone _ _ _ st (le_add' K O.toCone _ _ _ _ (le_refl' K O.toCone _) hb)
  refine le_trans' K O.toCone _ _ _ st2 ⟨K.zero, O.nn_zero, ?_⟩
  show K.add (K.add (K.mul ν (Zs K l kz a s)) (K.mul h (K.add
      (K.mul (K.add K.one K.one) (K.mul (K.mul ι ι) (K.mul (Zs K l kz a s) (K.mul (C1 K inv Rc) (Zs K l kz a s)))))
      (K.mul (K.add K.one K.one) (K.mul (K.mul ι ι) (K.mul (As K l kz a s) (K.mul (C2 K inv Rc t0) (Zs K l kz a s)))))))) K.zero
    = K.add (K.mul ν (Zs K l kz a s)) (K.mul h (K.add (K.mul (K.mul (K.mul (K.add K.one K.one) (K.mul ι ι)) (C1 K inv Rc))
        (K.mul (Zs K l kz a s) (Zs K l kz a s)))
      (K.mul (K.mul (K.mul (K.add K.one K.one) (K.mul ι ι)) (C2 K inv Rc t0)) (K.mul (As K l kz a s) (Zs K l kz a s)))))
  lib_ring

end Gen

end EnstLocalGalerkin

#print axioms EnstLocalGalerkin.local_galerkin

ns/EnstLocalTime.lean

lean_all/ns/EnstLocalTime.lean

/-
  ns/EnstLocalTime.lean — T37 cycle 1027 (goal G675): the large-data local bound in closed form. With Zb chosen so
  that ν·Zb = 2Q_0 and cutoffs with c₂Zb ≤ ν² (ChooseCutoff), every step count n with n·h·c₁·Zb² ≤ Q_0 gives, for
  all t ≤ n,
    Q_t ≤ 2 Q_0     and     Z_t ≤ Zb = 2Q_0/ν,
  i.e. the enstrophy at most doubles (relative to Q_0/ν) on the time interval n·h ≤ Q_0/(c₁ Zb²) = ν²/(4 c₁ Q_0),
  which depends only on Q_0, ν and the cutoffs — not on the truncation (local_time).

  Lean 4 core plus ns/EnstLocalGalerkin, ns/EnstGlobal (le_of_mul_le).
-/
import ns.EnstLocalGalerkin

namespace EnstLocalTime
open DetChain NSChain DyadicSmallGlobal LibRing DyadicLarge FourierGalerkin GalerkinForce ForceBound ForceSob
  ModeCount CubeCount ForceUniform NormCompare EnstStep EnstGlobal EnstGalerkin ForceTwoScale EnstLocal
  EnstLocalGalerkin LatticeSum

section Gen
variable {R : Type} (K : CRing R) (O : OrdR K) {I : Type}
variable (inv : R → R) (mul_inv : ∀ y, y ≠ K.zero → K.mul y (inv y) = K.one) (h10 : K.one ≠ K.zero)

include mul_inv h10 in
theorem local_time (l : List I) (kz : I → Mode) (hd : List.Pairwise (fun a b => kz a ≠ kz b) l)
    (h1 : ∀ p, 1 ≤ nrm (kz p)) (M : Nat) (hM : ∀ p, p ∈ l → nrm (kz p) ≤ M + 1) (Rc t0 : Nat) (hRM : Rc ≤ M)
    (ι h ν Zb : R) (hh : O.nn h) (hν : O.nn ν) (hν0 : ν ≠ K.zero) (hZb : O.nn Zb)
    (a N : Nat → I → V3 R)
    (hN : ∀ t, Le K O.toCone (lsum K (fun r => dot3 K (N t r) (N t r)) l)
      (lsum K (fun r => dot3 K (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (a t) (a t) r)
        (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (a t) (a t) r)) l))
    (hs : ∀ t r, Step K h ν (wt K kz r) (a (t + 1) r) (a t r) (N t r))
    (choice : Le K O.toCone (K.mul (cc K ι (C2 K inv Rc t0)) Zb) (K.mul ν ν))
    (hZ0 : K.mul ν Zb = K.add (Q K ν h (Zs K l kz a) (As K l kz a) 0) (Q K ν h (Zs K l kz a) (As K l kz a) 0))
    (n : Nat) (hn : Le K O.toCone (K.mul (castN K n) (K.mul h (K.mul (cc K ι (C1 K inv Rc)) (K.mul Zb Zb))))
      (Q K ν h (Zs K l kz a) (As K l kz a) 0))
    (t : Nat) (ht : t ≤ n) :
    Le K O.toCone (Q K ν h (Zs K l kz a) (As K l kz a) t)
        (K.add (Q K ν h (Zs K l kz a) (As K l kz a) 0) (Q K ν h (Zs K l kz a) (As K l kz a) 0))
      ∧ Le K O.toCone (Zs K l kz a t) Zb := by
  have budget : Le K O.toCone (K.add (Q K ν h (Zs K l kz a) (As K l kz a) 0)
      (K.mul (castN K n) (K.mul h (K.mul (cc K ι (C1 K inv Rc)) (K.mul Zb Zb))))) (K.mul ν Zb) := by
    rw [hZ0]; exact le_add' K O.toCone _ _ _ _ (le_refl' K O.toCone _) hn
  have q := local_galerkin K O inv mul_inv h10 l kz hd h1 M hM Rc t0 hRM ι h ν Zb hh hν hν0 hZb a N hN hs choice n
    budget t ht
  have hD : O.nn (K.mul h (K.mul (cc K ι (C1 K inv Rc)) (K.mul Zb Zb))) := by
    have hiv : O.nn (iv K inv (Rc + 1)) := iv_nn K O inv mul_inv h10 Rc
    have two : O.nn (K.add K.one K.one) := O.nn_add _ _ (RatModel.nn_one' K O.toCone) (RatModel.nn_one' K O.toCone)
    exact O.nn_mul _ _ hh (O.nn_mul _ _ (O.nn_mul _ _ (O.nn_mul _ _ two (O.nn_sq ι))
      (O.nn_add _ _ (O.nn_mul _ _ (RatModel.nn_cast K O.toCone _) (RatModel.nn_one' K O.toCone))
        (O.nn_mul _ _ (RatModel.nn_cast K O.toCone _) hiv))) (O.nn_sq Zb))
  have tn := ForceSob.le_mul_right' K O.toCone (RatModel.cast_mono K O.toCone t n ht) hD
  have q2 := le_trans' K O.toCone _ _ _ q (le_add' K O.toCone _ _ _ _ (le_refl' K O.toCone _) (le_trans' K O.toCone _ _ _ tn hn))
  refine ⟨q2, ?_⟩
  have hA : O.nn (As K l kz a t) :=
    lsum_nn K O.toCone _ (fun r => O.nn_mul _ _ (O.nn_sq _) (dot3_nn K O.toCone _)) l
  have hz : Le K O.toCone (K.mul ν (Zs K l kz a t)) (Q K ν h (Zs K l kz a) (As K l kz a) t) :=
    ⟨K.mul (K.mul h (K.mul ν ν)) (As K l kz a t), O.nn_mul _ _ (O.nn_mul _ _ hh (O.nn_sq ν)) hA, rfl⟩
  rw [← hZ0] at q2
  exact le_of_mul_le K O ν _ _ hν hν0 (le_trans' K O.toCone _ _ _ hz q2)

end Gen

end EnstLocalTime

#print axioms EnstLocalTime.local_time

ns/EnstPair.lean

lean_all/ns/EnstPair.lean

/-
  ns/EnstPair.lean — T37 cycle 973 (goal G638, piece a): the pairing inequality for the enstrophy step, over any
  commutative ring with a nonnegative cone. For weights w_r (w_r = |k_r|² in the enstrophy step), vectors a_r, N_r:
    pair_bound   2ε Σ_r w_r (a_r·N_r) ≤ ε² Σ_r w_r²|a_r|² + Σ_r |N_r|²
  from Σ_r |ε w_r a_r − N_r|² ≥ 0. With ForceUniform (Σ|N_r|² ≤ 135 ι² H Z, H ≤ Σ w_r²|a_r|²) the nonlinear term
  of d/dt Σ|k|²|a|² is at most (ε² + 135 ι² Z)/(2ε) · Σ|k|⁴|a|², which the dissipation 2ν Σ|k|⁴|a|² absorbs when
  Z = Σ|k|²|a|² is small — the N-uniform small-data enstrophy bound.

  Lean 4 core plus ns/ForceBound (lsum_le), ns/FourierGalerkin (V3, dot3), lib/RingTac.
-/
import ns.ForceBound

namespace EnstPair
open DetChain NSChain LibRing DyadicLarge FourierGalerkin ForceBound

section Gen
variable {R : Type} (K : CRing R) (P : Cone K) {I : Type}

theorem pair_one (ε w : R) (a N : V3 R) :
    Le K P (K.mul (K.mul (K.add K.one K.one) ε) (K.mul w (dot3 K a N)))
      (K.add (K.mul (K.mul ε ε) (K.mul (K.mul w w) (dot3 K a a))) (dot3 K N N)) := by
  refine ⟨K.add (K.add (K.mul (K.add (K.mul (K.mul ε w) a.x) (K.neg N.x)) (K.add (K.mul (K.mul ε w) a.x) (K.neg N.x)))
      (K.mul (K.add (K.mul (K.mul ε w) a.y) (K.neg N.y)) (K.add (K.mul (K.mul ε w) a.y) (K.neg N.y))))
      (K.mul (K.add (K.mul (K.mul ε w) a.z) (K.neg N.z)) (K.add (K.mul (K.mul ε w) a.z) (K.neg N.z))),
    P.nn_add _ _ (P.nn_add _ _ (P.nn_sq _) (P.nn_sq _)) (P.nn_sq _), ?_⟩
  unfold dot3
  lib_ring

theorem pair_bound (l : List I) (ε : R) (w : I → R) (a N : I → V3 R) :
    Le K P (lsum K (fun r => K.mul (K.mul (K.add K.one K.one) ε) (K.mul (w r) (dot3 K (a r) (N r)))) l)
      (K.add (K.mul (K.mul ε ε) (lsum K (fun r => K.mul (K.mul (w r) (w r)) (dot3 K (a r) (a r))) l))
        (lsum K (fun r => dot3 K (N r) (N r)) l)) := by
  have h := lsum_le K P _ _ (fun r => pair_one K P ε (w r) (a r) (N r)) l
  rw [lsum_add, lsum_mul_left K (K.mul ε ε)] at h
  exact h

end Gen

end EnstPair

#print axioms EnstPair.pair_one
#print axioms EnstPair.pair_bound

ns/EnstProj.lean

lean_all/ns/EnstProj.lean

/-
  ns/EnstProj.lean — T37 cycle 981 (goal G643): the global small-data enstrophy bound for any force N_t whose energy is
  at most that of the Galerkin force (enst_global_gen), in particular for the Leray-projected force
  P_r N_r = N_r − (k_r·N_r) κ_r k_r with κ_r |k_r|² = 1 (enst_proj, via LerayProj3.proj_le). Scheme:
  (1 + hν|k_r|²) a^{t+1}_r = a^t_r + h P_r N_r(a^t); if 135ι²(νZ_0 + hν²A_0) ≤ ν³ then νZ_t + hν²A_t stays below
  its initial value for every t, uniformly in the truncation.

  Lean 4 core plus ns/EnstGalerkin, ns/LerayProj3.
-/
import ns.EnstGalerkin
import ns.LerayProj3

namespace EnstProj
open DetChain NSChain DyadicSmallGlobal LibRing DyadicLarge FourierGalerkin GalerkinForce ForceBound ForceSob
  ModeCount CubeCount ForceUniform NormCompare EnstStep EnstGlobal EnstGalerkin LerayProj LerayProj3

section Gen
variable {R : Type} (K : CRing R) (O : OrdR K) {I : Type}
variable (inv : R → R) (mul_inv : ∀ y, y ≠ K.zero → K.mul y (inv y) = K.one) (h10 : K.one ≠ K.zero)

include mul_inv h10 in
theorem enst_global_gen (l : List I) (kz : I → Mode) (hd : List.Pairwise (fun a b => kz a ≠ kz b) l)
    (h1 : ∀ p, 1 ≤ nrm (kz p)) (M : Nat) (hM : ∀ p, p ∈ l → nrm (kz p) ≤ M + 1)
    (ι h ν : R) (hh : O.nn h) (hν : O.nn ν) (hν0 : ν ≠ K.zero) (a : Nat → I → V3 R) (N : Nat → I → V3 R)
    (hN : ∀ t, Le K O.toCone (lsum K (fun r => dot3 K (N t r) (N t r)) l)
      (lsum K (fun r => dot3 K (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (a t) (a t) r)
        (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (a t) (a t) r)) l))
    (hs : ∀ t r, Step K h ν (wt K kz r) (a (t + 1) r) (a t r) (N t r))
    (small : Le K O.toCone (K.mul (K.mul (castN K 135) (K.mul ι ι)) (Q K ν h (Zs K l kz a) (As K l kz a) 0)) (K.mul ν (K.mul ν ν)))
    (t : Nat) :
    Le K O.toCone (Q K ν h (Zs K l kz a) (As K l kz a) t) (Q K ν h (Zs K l kz a) (As K l kz a) 0) := by
  have hw : ∀ r, O.nn (wt K kz r) := fun r => dot3_nn K O.toCone _
  have hZ : ∀ t, O.nn (Zs K l kz a t) := fun t =>
    lsum_nn K O.toCone _ (fun r => O.nn_mul _ _ (hw r) (dot3_nn K O.toCone _)) l
  have hA : ∀ t, O.nn (As K l kz a t) := fun t =>
    lsum_nn K O.toCone _ (fun r => O.nn_mul _ _ (O.nn_sq _) (dot3_nn K O.toCone _)) l
  have hc : O.nn (K.mul (castN K 135) (K.mul ι ι)) := O.nn_mul _ _ (RatModel.nn_cast K O.toCone 135) (O.nn_sq ι)
  refine q_le K O ν h _ (Zs K l kz a) (As K l kz a) hν hν0 hh hc hZ hA ?_ small t
  intro s
  -- one step: νZ' + hν²A' ≤ νZ + hB
  have st := enst_step K O.toCone l h ν hh hν (wt K kz) hw (a (s + 1)) (a s) _ (hs s)
  -- B ≤ ι² H (135 Z),  H ≤ A
  have fu := force_uniform K O inv mul_inv h10 l kz hd h1 M hM ι (kv K kz) (a s) (a s)
  have hH : Le K O.toCone (lsum K (fun p => K.mul (K.mul (mu K kz p) (mu K kz p)) (dot3 K (a s p) (a s p))) l)
      (As K l kz a s) :=
    lsum_le K O.toCone _ _ (fun p => le_mul_right' K O.toCone (nrm4_le K O.toCone (kz p)) (dot3_nn K O.toCone _)) l
  have hZn := hZ s
  have hb : Le K O.toCone (lsum K (fun r => dot3 K (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (a s) (a s) r)
      (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (a s) (a s) r)) l)
      (K.mul (K.mul (castN K 135) (K.mul ι ι)) (K.mul (As K l kz a s) (Zs K l kz a s))) := by
    have m1 := le_mul_right' K O.toCone hH (O.nn_mul _ _ (RatModel.nn_cast K O.toCone 135) hZn)
    have m2 := le_mul_nn K O.toCone (K.mul ι ι) _ _ (O.nn_sq ι) m1
    have t2 := le_trans' K O.toCone _ _ _ fu m2
    have e : K.mul (K.mul ι ι) (K.mul (As K l kz a s) (K.mul (castN K 135) (Zs K l kz a s)))
        = K.mul (K.mul (castN K 135) (K.mul ι ι)) (K.mul (As K l kz a s) (Zs K l kz a s)) := by lib_ring
    rw [e] at t2
    exact t2
  have hb := le_trans' K O.toCone _ _ _ (hN s) hb
  have hb2 := le_mul_nn K O.toCone h _ _ hh hb
  have st2 := le_trans' K O.toCone _ _ _ st (le_add' K O.toCone _ _ _ _ (le_refl' K O.toCone _) hb2)
  have e2 : K.mul h (K.mul (K.mul (castN K 135) (K.mul ι ι)) (K.mul (As K l kz a s) (Zs K l kz a s)))
      = K.mul (K.mul h (K.mul (castN K 135) (K.mul ι ι))) (K.mul (As K l kz a s) (Zs K l kz a s)) := by lib_ring
  rw [e2] at st2
  exact st2


include mul_inv h10 in
theorem enst_proj (l : List I) (kz : I → Mode) (hd : List.Pairwise (fun a b => kz a ≠ kz b) l)
    (h1 : ∀ p, 1 ≤ nrm (kz p)) (M : Nat) (hM : ∀ p, p ∈ l → nrm (kz p) ≤ M + 1)
    (ι h ν : R) (hh : O.nn h) (hν : O.nn ν) (hν0 : ν ≠ K.zero) (a : Nat → I → V3 R)
    (κ : I → R) (hκn : ∀ r, O.nn (κ r)) (hκ : ∀ r, K.mul (dot3 K (kv K kz r) (kv K kz r)) (κ r) = K.one)
    (hs : ∀ t r, Step K h ν (wt K kz r) (a (t + 1) r) (a t r)
      (proj K (κ r) (kv K kz r) (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (a t) (a t) r)))
    (small : Le K O.toCone (K.mul (K.mul (castN K 135) (K.mul ι ι)) (Q K ν h (Zs K l kz a) (As K l kz a) 0)) (K.mul ν (K.mul ν ν)))
    (t : Nat) :
    Le K O.toCone (Q K ν h (Zs K l kz a) (As K l kz a) t) (Q K ν h (Zs K l kz a) (As K l kz a) 0) :=
  enst_global_gen K O inv mul_inv h10 l kz hd h1 M hM ι h ν hh hν hν0 a
    (fun t r => proj K (κ r) (kv K kz r) (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (a t) (a t) r))
    (fun _ => lsum_le K O.toCone _ _ (fun r => proj_le K O.toCone (κ r) (kv K kz r) _ (hκn r) (hκ r)) l)
    hs small t

end Gen

end EnstProj

#print axioms EnstProj.enst_global_gen
#print axioms EnstProj.enst_proj

ns/EnstStep.lean

lean_all/ns/EnstStep.lean

/-
  ns/EnstStep.lean — T37 cycle 976 (goal G638, piece c): one step of the enstrophy inequality for the implicit-viscosity
  (backward Euler) Galerkin step (1 + hν w_r) a'_r = a_r + h N_r, over any commutative ring with a nonnegative cone
  (h, ν, w_r ≥ 0; no inverses needed):
    mode_step   ν w|a'|² + h ν² w²|a'|² ≤ ν w|a|² + h|N|²
    enst_step   ν Z' + h ν² A' ≤ ν Z + h Σ_r |N_r|²,   Z = Σ w|a|²,  A = Σ w²|a|².
  Slack: ν w|a' − a|² + h|ν w a' − N|² (from 2a'·a ≤ |a'|² + |a|² and 2νw a'·N ≤ ν²w²|a'|² + |N|²).
  With w_r = |k_r|² and ForceUniform (Σ|N|² ≤ 135ι²·A·Z) the quantity νZ + hν²A is nonincreasing while
  135ι²Z ≤ ν² — next piece.

  Lean 4 core plus ns/ForceBound (lsum_le), ns/FourierGalerkin (V3, dot3), lib/RingTac.
-/
import ns.ForceBound

namespace EnstStep
open DetChain NSChain LibRing DyadicLarge FourierGalerkin ForceBound

section Gen
variable {R : Type} (K : CRing R) (P : Cone K) {I : Type}

/-- the step relation on one component: (1 + hνw) a' = a + h N -/
def StepC (h ν w a' a n : R) : Prop := K.mul (K.add K.one (K.mul (K.mul h ν) w)) a' = K.add a (K.mul h n)

def Step (h ν w : R) (a' a N : V3 R) : Prop :=
  StepC K h ν w a'.x a.x N.x ∧ StepC K h ν w a'.y a.y N.y ∧ StepC K h ν w a'.z a.z N.z

theorem comp_eq (h ν w a' a n : R) (hs : StepC K h ν w a' a n) :
    a = K.add (K.mul (K.add K.one (K.mul (K.mul h ν) w)) a') (K.neg (K.mul h n)) := by
  rw [hs]; lib_ring

theorem mode_step (h ν w : R) (hh : P.nn h) (hν : P.nn ν) (hw : P.nn w) (a' a N : V3 R) (hs : Step K h ν w a' a N) :
    Le K P (K.add (K.mul (K.mul ν w) (dot3 K a' a')) (K.mul (K.mul h (K.mul (K.mul ν ν) (K.mul w w))) (dot3 K a' a')))
      (K.add (K.mul (K.mul ν w) (dot3 K a a)) (K.mul h (dot3 K N N))) := by
  have hνw : P.nn (K.mul ν w) := P.nn_mul _ _ hν hw
  have sq3 : ∀ v : V3 R, P.nn (dot3 K v v) := fun v => dot3_nn K P v
  let dv : V3 R := ⟨K.add a'.x (K.neg a.x), K.add a'.y (K.neg a.y), K.add a'.z (K.neg a.z)⟩
  let ev : V3 R := ⟨K.add (K.mul (K.mul ν w) a'.x) (K.neg N.x), K.add (K.mul (K.mul ν w) a'.y) (K.neg N.y),
    K.add (K.mul (K.mul ν w) a'.z) (K.neg N.z)⟩
  refine ⟨K.add (K.mul (K.mul ν w) (dot3 K dv dv)) (K.mul h (dot3 K ev ev)),
    P.nn_add _ _ (P.nn_mul _ _ hνw (sq3 dv)) (P.nn_mul _ _ hh (sq3 ev)), ?_⟩
  have ex := comp_eq K h ν w a'.x a.x N.x hs.1
  have ey := comp_eq K h ν w a'.y a.y N.y hs.2.1
  have ez := comp_eq K h ν w a'.z a.z N.z hs.2.2
  show K.add (K.add (K.mul (K.mul ν w) (dot3 K a' a')) (K.mul (K.mul h (K.mul (K.mul ν ν) (K.mul w w))) (dot3 K a' a')))
      (K.add (K.mul (K.mul ν w) (dot3 K dv dv)) (K.mul h (dot3 K ev ev)))
    = K.add (K.mul (K.mul ν w) (dot3 K a a)) (K.mul h (dot3 K N N))
  unfold dot3
  show K.add (K.add (K.mul (K.mul ν w) (K.add (K.add (K.mul a'.x a'.x) (K.mul a'.y a'.y)) (K.mul a'.z a'.z)))
      (K.mul (K.mul h (K.mul (K.mul ν ν) (K.mul w w))) (K.add (K.add (K.mul a'.x a'.x) (K.mul a'.y a'.y)) (K.mul a'.z a'.z))))
      (K.add (K.mul (K.mul ν w) (K.add (K.add (K.mul (K.add a'.x (K.neg a.x)) (K.add a'.x (K.neg a.x)))
          (K.mul (K.add a'.y (K.neg a.y)) (K.add a'.y (K.neg a.y)))) (K.mul (K.add a'.z (K.neg a.z)) (K.add a'.z (K.neg a.z)))))
        (K.mul h (K.add (K.add (K.mul (K.add (K.mul (K.mul ν w) a'.x) (K.neg N.x)) (K.add (K.mul (K.mul ν w) a'.x) (K.neg N.x)))
          (K.mul (K.add (K.mul (K.mul ν w) a'.y) (K.neg N.y)) (K.add (K.mul (K.mul ν w) a'.y) (K.neg N.y))))
          (K.mul (K.add (K.mul (K.mul ν w) a'.z) (K.neg N.z)) (K.add (K.mul (K.mul ν w) a'.z) (K.neg N.z))))))
    = K.add (K.mul (K.mul ν w) (K.add (K.add (K.mul a.x a.x) (K.mul a.y a.y)) (K.mul a.z a.z)))
      (K.mul h (K.add (K.add (K.mul N.x N.x) (K.mul N.y N.y)) (K.mul N.z N.z)))
  rw [ex, ey, ez]
  lib_ring

theorem enst_step (l : List I) (h ν : R) (hh : P.nn h) (hν : P.nn ν) (w : I → R) (hw : ∀ r, P.nn (w r))
    (a' a N : I → V3 R) (hs : ∀ r, Step K h ν (w r) (a' r) (a r) (N r)) :
    Le K P (K.add (K.mul ν (lsum K (fun r => K.mul (w r) (dot3 K (a' r) (a' r))) l))
        (K.mul (K.mul h (K.mul ν ν)) (lsum K (fun r => K.mul (K.mul (w r) (w r)) (dot3 K (a' r) (a' r))) l)))
      (K.add (K.mul ν (lsum K (fun r => K.mul (w r) (dot3 K (a r) (a r))) l))
        (K.mul h (lsum K (fun r => dot3 K (N r) (N r)) l))) := by
  have t := lsum_le K P _ _ (fun r => mode_step K P h ν (w r) hh hν (hw r) (a' r) (a r) (N r) (hs r)) l
  rw [lsum_add, lsum_add] at t
  have e1 : lsum K (fun r => K.mul (K.mul ν (w r)) (dot3 K (a' r) (a' r))) l
      = K.mul ν (lsum K (fun r => K.mul (w r) (dot3 K (a' r) (a' r))) l) := by
    rw [← lsum_mul_left]; exact lsum_ext K _ _ (fun r => by lib_ring) l
  have e2 : lsum K (fun r => K.mul (K.mul h (K.mul (K.mul ν ν) (K.mul (w r) (w r)))) (dot3 K (a' r) (a' r))) l
      = K.mul (K.mul h (K.mul ν ν)) (lsum K (fun r => K.mul (K.mul (w r) (w r)) (dot3 K (a' r) (a' r))) l) := by
    rw [← lsum_mul_left]; exact lsum_ext K _ _ (fun r => by lib_ring) l
  have e3 : lsum K (fun r => K.mul (K.mul ν (w r)) (dot3 K (a r) (a r))) l
      = K.mul ν (lsum K (fun r => K.mul (w r) (dot3 K (a r) (a r))) l) := by
    rw [← lsum_mul_left]; exact lsum_ext K _ _ (fun r => by lib_ring) l
  have e4 : lsum K (fun r => K.mul h (dot3 K (N r) (N r))) l = K.mul h (lsum K (fun r => dot3 K (N r) (N r)) l) :=
    lsum_mul_left K h _ l
  rw [e1, e2, e3, e4] at t
  exact t

end Gen

end EnstStep

#print axioms EnstStep.mode_step
#print axioms EnstStep.enst_step

ns/EnstStepV.lean

lean_all/ns/EnstStepV.lean

/-
  ns/EnstStepV.lean — T37 cycle 1059 (goal G656b, piece i): the enstrophy step with an extra weight V ≥ 0, over a
  commutative ring with a nonnegative cone. For (1 + hνw_r) a'_r = a_r + h N_r:
    enst_stepV   ν Σ wV|a'|² + hν² Σ w²V|a'|² ≤ ν Σ wV|a|² + h Σ V|N|²
  (mode_step times V). With V = w = |k|² this is the H²-energy step νA' + hν²Σw³|a'|² ≤ νA + hΣw|N|².

  Lean 4 core plus ns/EnstStep.
-/
import ns.EnstStep

namespace EnstStepV
open DetChain NSChain LibRing DyadicLarge FourierGalerkin ForceBound EnstStep

section Gen
variable {R : Type} (K : CRing R) (P : Cone K) {I : Type}

theorem enst_stepV (l : List I) (h ν : R) (hh : P.nn h) (hν : P.nn ν) (w V : I → R) (hw : ∀ r, P.nn (w r))
    (hV : ∀ r, P.nn (V r)) (a' a N : I → V3 R) (hs : ∀ r, Step K h ν (w r) (a' r) (a r) (N r)) :
    Le K P (K.add (K.mul ν (lsum K (fun r => K.mul (K.mul (w r) (V r)) (dot3 K (a' r) (a' r))) l))
        (K.mul (K.mul h (K.mul ν ν)) (lsum K (fun r => K.mul (K.mul (K.mul (w r) (w r)) (V r)) (dot3 K (a' r) (a' r))) l)))
      (K.add (K.mul ν (lsum K (fun r => K.mul (K.mul (w r) (V r)) (dot3 K (a r) (a r))) l))
        (K.mul h (lsum K (fun r => K.mul (V r) (dot3 K (N r) (N r))) l))) := by
  have t := lsum_le K P _ _ (fun r => le_mul_nn K P (V r) _ _ (hV r)
    (mode_step K P h ν (w r) hh hν (hw r) (a' r) (a r) (N r) (hs r))) l
  have eL : lsum K (fun r => K.mul (V r) (K.add (K.mul (K.mul ν (w r)) (dot3 K (a' r) (a' r)))
      (K.mul (K.mul h (K.mul (K.mul ν ν) (K.mul (w r) (w r)))) (dot3 K (a' r) (a' r))))) l
      = K.add (K.mul ν (lsum K (fun r => K.mul (K.mul (w r) (V r)) (dot3 K (a' r) (a' r))) l))
        (K.mul (K.mul h (K.mul ν ν)) (lsum K (fun r => K.mul (K.mul (K.mul (w r) (w r)) (V r)) (dot3 K (a' r) (a' r))) l)) := by
    rw [← lsum_mul_left, ← lsum_mul_left, ← lsum_add]; exact lsum_ext K _ _ (fun r => by lib_ring) l
  have eR : lsum K (fun r => K.mul (V r) (K.add (K.mul (K.mul ν (w r)) (dot3 K (a r) (a r))) (K.mul h (dot3 K (N r) (N r))))) l
      = K.add (K.mul ν (lsum K (fun r => K.mul (K.mul (w r) (V r)) (dot3 K (a r) (a r))) l))
        (K.mul h (lsum K (fun r => K.mul (V r) (dot3 K (N r) (N r))) l)) := by
    rw [← lsum_mul_left, ← lsum_mul_left, ← lsum_add]; exact lsum_ext K _ _ (fun r => by lib_ring) l
  rw [eL, eR] at t
  exact t

end Gen

end EnstStepV

#print axioms EnstStepV.enst_stepV

ns/Enstrophy2D.lean

lean_all/ns/Enstrophy2D.lean

/-
  ns/Enstrophy2D.lean — T37 cycle 11: why two dimensions are regular, in the Galerkin setting.

  Lean 4 core plus NSChain (CRing, Cone, sumN). Hand proofs.

  2D Navier–Stokes in Fourier variables (stream function ψ, vorticity ω_k = |k|² ψ_k) couples the
  modes in triads k + p + q = 0 with the coefficient J(p, q) = p₁q₂ − p₂q₁ times differences of
  |·|². Proved:
    J_cyclic           k + p + q = 0 ⇒ J(p,q) = J(q,k) = J(k,p)
    triad_energy       J(p,q)(|q|²−|p|²) + J(q,k)(|k|²−|q|²) + J(k,p)(|p|²−|k|²) = 0
    triad_enstrophy    |k|²J(p,q)(|q|²−|p|²) + |p|²J(q,k)(|k|²−|q|²) + |q|²J(k,p)(|p|²−|k|²) = 0
                       (detailed conservation of energy AND enstrophy, triad by triad)
    cn_weighted        Crank–Nicolson with weights w_n: E_w(a') + hν Σ w_n k_n s_n² = E_w(a) whenever
                       the nonlinearity is neutral for the weighted product (Σ w_n s_n N_n = 0)
    cn_weighted_le     with w, h, ν, k ≥ 0: E_w(a') ≤ E_w(a)
  With w_n = |k_n|² (enstrophy) and the triad identity, every 2D Galerkin truncation has enstrophy
  non-increasing at every step, uniformly in the truncation: the H¹ bound that 3D lacks (in 3D the
  vortex-stretching term makes the weighted product non-neutral; no such triad identity holds).
-/
import NSChain

namespace Enstrophy2D
open DetChain NSChain

section Ring
variable {R : Type} (K : CRing R)

structure V2 (R : Type) where
  x : R
  y : R

def J (a b : V2 R) : R := K.add (K.mul a.x b.y) (K.neg (K.mul a.y b.x))
def n2 (a : V2 R) : R := K.add (K.mul a.x a.x) (K.mul a.y a.y)
def sub (x y : R) : R := K.add x (K.neg y)

theorem neg_add2 (x y : R) : K.neg (K.add x y) = K.add (K.neg x) (K.neg y) := by
  apply (neg_unique K _ _ _).symm
  rw [add_mid4 K, add_neg' K, add_neg' K, K.zero_add]

/-- k = −(p + q) componentwise. -/
def Triad (k p q : V2 R) : Prop :=
  k.x = K.neg (K.add p.x q.x) ∧ k.y = K.neg (K.add p.y q.y)

theorem J_cyclic1 (k p q : V2 R) (t : Triad K k p q) : J K q k = J K p q := by
  unfold J
  rw [t.1, t.2, mul_neg' K, mul_neg' K, K.distrib, K.distrib, neg_add2 K, neg_add2 K, neg_add2 K,
    neg_neg' K, neg_neg' K]
  -- (−q.x p.y + −q.x q.y) + (q.y p.x + q.y q.x) = p.x q.y + −(p.y q.x)
  rw [K.mul_comm q.y q.x, add_mid4 K, add_neg' K, add_zero', K.add_comm, K.mul_comm q.y p.x,
    K.mul_comm q.x p.y]

theorem J_cyclic2 (k p q : V2 R) (t : Triad K k p q) : J K k p = J K p q := by
  unfold J
  rw [t.1, t.2, K.mul_comm (K.neg (K.add p.x q.x)) p.y, K.mul_comm (K.neg (K.add p.y q.y)) p.x,
    mul_neg' K, mul_neg' K, K.distrib, K.distrib, neg_add2 K, neg_neg' K]
  rw [K.mul_comm p.y p.x, add_mid4 K, add_neg' K, K.zero_add, K.add_comm]

theorem sum3_zero (a b c : R) : K.add (K.add (sub K a b) (sub K c a)) (sub K b c) = K.zero := by
  unfold sub
  rw [K.add_assoc, K.add_comm (K.add c (K.neg a)) (K.add b (K.neg c)), K.add_assoc b,
    ← K.add_assoc (K.neg c) c, add_neg' K, K.zero_add, K.add_assoc a, ← K.add_assoc (K.neg b) b,
    add_neg' K, K.zero_add, K.add_neg]

theorem sum3_zero' (x y z : R) : K.add (K.add (sub K x y) (sub K y z)) (sub K z x) = K.zero := by
  unfold sub
  rw [K.add_assoc x, ← K.add_assoc (K.neg y) y, add_neg' K, K.zero_add, K.add_assoc x,
    ← K.add_assoc (K.neg z) z, add_neg' K, K.zero_add, K.add_neg]

theorem mul_sub (c a b : R) : K.mul c (sub K a b) = sub K (K.mul c a) (K.mul c b) := by
  unfold sub; rw [K.distrib, mul_neg' K]

theorem triad_energy (k p q : V2 R) (t : Triad K k p q) :
    K.add (K.add (K.mul (J K p q) (sub K (n2 K q) (n2 K p))) (K.mul (J K q k) (sub K (n2 K k) (n2 K q))))
      (K.mul (J K k p) (sub K (n2 K p) (n2 K k))) = K.zero := by
  rw [J_cyclic1 K k p q t, J_cyclic2 K k p q t, ← K.distrib, ← K.distrib, sum3_zero, mul_zero K]

theorem triad_enstrophy (k p q : V2 R) (t : Triad K k p q) :
    K.add (K.add (K.mul (n2 K k) (K.mul (J K p q) (sub K (n2 K q) (n2 K p))))
        (K.mul (n2 K p) (K.mul (J K q k) (sub K (n2 K k) (n2 K q)))))
      (K.mul (n2 K q) (K.mul (J K k p) (sub K (n2 K p) (n2 K k)))) = K.zero := by
  rw [J_cyclic1 K k p q t, J_cyclic2 K k p q t]
  generalize J K p q = j
  generalize n2 K k = c
  generalize n2 K p = b
  generalize n2 K q = a
  -- c(j(a−b)) + b(j(c−a)) + a(j(b−c)) = j·((ca − cb) + (bc − ba) + (ab − ac)) = 0
  rw [← K.mul_assoc c j, K.mul_comm c j, K.mul_assoc j c, ← K.mul_assoc b j, K.mul_comm b j,
    K.mul_assoc j b, ← K.mul_assoc a j, K.mul_comm a j, K.mul_assoc j a, ← K.distrib, ← K.distrib,
    mul_sub K c a b, mul_sub K b c a, mul_sub K a b c]
  rw [K.mul_comm c b, K.mul_comm b a, K.mul_comm a c, K.mul_comm c a]
  -- (ac − bc) + (bc − ab) + (ab − ac)
  rw [sum3_zero' K (K.mul a c) (K.mul b c) (K.mul a b), mul_zero K]

/-- The per-mode Crank–Nicolson identity (the step inside NSChain.cn_energy). -/
theorem per_mode (a a' k Nn h nu : R)
    (step : K.add a' (K.neg a) = K.mul h (K.add (K.neg (K.mul nu (K.mul k (K.add a' a)))) Nn)) :
    K.mul a' a' = K.add (K.mul a a)
      (K.add (K.neg (K.mul h (K.mul nu (K.mul k (K.mul (K.add a' a) (K.add a' a))))))
        (K.mul h (K.mul (K.add a' a) Nn))) := by
  rw [sq_diff K a' a, step, K.mul_assoc, right_distrib K, neg_mul_left K, K.distrib, mul_neg' K,
    K.mul_comm Nn (K.add a' a), K.mul_assoc nu, K.mul_assoc k]

/-- **Weighted Crank–Nicolson identity.** -/
theorem cn_weighted (w a a' k Nn : Nat → R) (h nu : R) (N : Nat)
    (step : ∀ n, K.add (a' n) (K.neg (a n))
      = K.mul h (K.add (K.neg (K.mul nu (K.mul (k n) (K.add (a' n) (a n))))) (Nn n)))
    (neutral : sumN K (fun n => K.mul (w n) (K.mul (K.add (a' n) (a n)) (Nn n))) N = K.zero) :
    K.add (sumN K (fun n => K.mul (w n) (K.mul (a' n) (a' n))) N)
      (K.mul h (K.mul nu (sumN K (fun n => K.mul (w n) (K.mul (k n) (K.mul (K.add (a' n) (a n)) (K.add (a' n) (a n))))) N)))
      = sumN K (fun n => K.mul (w n) (K.mul (a n) (a n))) N := by
  let s : Nat → R := fun n => K.add (a' n) (a n)
  let D : Nat → R := fun n => K.mul (w n) (K.mul (k n) (K.mul (s n) (s n)))
  have e1 : ∀ n, K.mul (w n) (K.mul (a' n) (a' n))
      = K.add (K.mul (w n) (K.mul (a n) (a n)))
          (K.add (K.neg (K.mul h (K.mul nu (D n)))) (K.mul h (K.mul (w n) (K.mul (s n) (Nn n))))) := by
    intro n
    rw [per_mode K (a n) (a' n) (k n) (Nn n) h nu (step n), K.distrib, K.distrib, mul_neg' K]
    show K.add (K.mul (w n) (K.mul (a n) (a n)))
        (K.add (K.neg (K.mul (w n) (K.mul h (K.mul nu (K.mul (k n) (K.mul (s n) (s n)))))))
          (K.mul (w n) (K.mul h (K.mul (s n) (Nn n)))))
      = K.add (K.mul (w n) (K.mul (a n) (a n)))
          (K.add (K.neg (K.mul h (K.mul nu (K.mul (w n) (K.mul (k n) (K.mul (s n) (s n)))))))
            (K.mul h (K.mul (w n) (K.mul (s n) (Nn n)))))
    rw [← K.mul_assoc (w n) h, K.mul_comm (w n) h, K.mul_assoc h (w n),
      ← K.mul_assoc (w n) nu, K.mul_comm (w n) nu, K.mul_assoc nu (w n),
      ← K.mul_assoc (w n) h (K.mul (s n) (Nn n)), K.mul_comm (w n) h, K.mul_assoc h (w n)]
  rw [sumN_ext K _ _ e1 N, sumN_add, sumN_add, sumN_neg, sumN_mul_left, sumN_mul_left, sumN_mul_left]
  show K.add (K.add (sumN K (fun n => K.mul (w n) (K.mul (a n) (a n))) N)
      (K.add (K.neg (K.mul h (K.mul nu (sumN K D N))))
        (K.mul h (sumN K (fun n => K.mul (w n) (K.mul (s n) (Nn n))) N))))
    (K.mul h (K.mul nu (sumN K D N))) = _
  have neutral' : sumN K (fun n => K.mul (w n) (K.mul (s n) (Nn n))) N = K.zero := neutral
  rw [neutral', mul_zero K, add_zero', K.add_assoc, add_neg' K, add_zero']

theorem cn_weighted_le (P : Cone K) (w a a' k Nn : Nat → R) (h nu : R) (N : Nat)
    (hw : ∀ n, P.nn (w n)) (hk : ∀ n, P.nn (k n)) (hh : P.nn h) (hnu : P.nn nu)
    (step : ∀ n, K.add (a' n) (K.neg (a n))
      = K.mul h (K.add (K.neg (K.mul nu (K.mul (k n) (K.add (a' n) (a n))))) (Nn n)))
    (neutral : sumN K (fun n => K.mul (w n) (K.mul (K.add (a' n) (a n)) (Nn n))) N = K.zero) :
    Le K P (sumN K (fun n => K.mul (w n) (K.mul (a' n) (a' n))) N)
      (sumN K (fun n => K.mul (w n) (K.mul (a n) (a n))) N) :=
  ⟨_, P.nn_mul _ _ hh (P.nn_mul _ _ hnu (sumN_nn K P _ (fun n => P.nn_mul _ _ (hw n)
      (P.nn_mul _ _ (hk n) (P.nn_sq _))) N)),
    cn_weighted K w a a' k Nn h nu N step neutral⟩

end Ring

end Enstrophy2D

#print axioms Enstrophy2D.neg_add2
#print axioms Enstrophy2D.J_cyclic1
#print axioms Enstrophy2D.J_cyclic2
#print axioms Enstrophy2D.sum3_zero
#print axioms Enstrophy2D.sum3_zero'
#print axioms Enstrophy2D.mul_sub
#print axioms Enstrophy2D.triad_energy
#print axioms Enstrophy2D.triad_enstrophy
#print axioms Enstrophy2D.per_mode
#print axioms Enstrophy2D.cn_weighted
#print axioms Enstrophy2D.cn_weighted_le

ns/EnstrophyStep.lean

lean_all/ns/EnstrophyStep.lean

/-
  ns/EnstrophyStep.lean — T37 cycle 206 (goal G1, step 1): the Crank–Nicolson enstrophy step of a 3D truncation in
  the step form of LocalCont, with midpoint quantities.

  Lean 4 core plus ns/ForcedEnstrophy (step_identity), lib/Gronwall, NSChain, lib/RingTac. Modes i ∈ l,
  a′_i − a_i = h(−ν k_i s_i + N_i), s = a′ + a, weights w_i ≥ 0 (w = |k|² gives the enstrophy). No neutrality:
    enstrophy_identity   Z′ + hν Σ w k s² = Z + h Σ w s N               (the transfer T = Σ w s N stays)
    enstrophy_step       if h T ≤ h c X ρ σ with ρ² = Σ w s², σ² = Σ w k s² (TriadLinf.stretch), then
                         Z′ ≤ Z + h (c X ρ σ − ν σ²)
  The step of LocalCont with (r, s) evaluated at the midpoint a′ + a; ρ² ≤ 2(Z + Z′) links ρ to r (next step of G1).
-/
import ns.ForcedEnstrophy

namespace EnstrophyStep
open DetChain NSChain LibRing LibCS LibGron

variable {R : Type} (K : CRing R) {I : Type}

def Zw (w a : I → R) (l : List I) : R := lsum K (fun i => K.mul (w i) (K.mul (a i) (a i))) l

theorem enstrophy_identity (w a a' k Nn : I → R) (h nu : R) (l : List I)
    (step : ∀ i, sub K (a' i) (a i) = K.mul h (K.add (K.neg (K.mul nu (K.mul (k i) (K.add (a' i) (a i))))) (Nn i))) :
    K.add (Zw K w a' l)
        (K.mul (K.mul h nu) (lsum K (fun i => K.mul (w i) (K.mul (k i) (K.mul (K.add (a' i) (a i)) (K.add (a' i) (a i))))) l))
      = K.add (Zw K w a l) (K.mul h (lsum K (fun i => K.mul (w i) (K.mul (K.add (a' i) (a i)) (Nn i))) l)) := by
  -- step_identity with the transfer as forcing and zero nonlinearity
  have id := ForcedEnstrophy.step_identity K w a a' k (fun _ => K.zero) Nn h nu l
    (fun i => by rw [step i]; lib_ring)
    (by
      have e := lsum_ext K (fun i => K.mul (w i) (K.mul (K.add (a' i) (a i)) K.zero)) (fun _ => K.zero)
        (fun i => by lib_ring) l
      rw [e]
      have z : ∀ l : List I, lsum K (fun _ => K.zero) l = K.zero := by
        intro l; induction l with
        | nil => rfl
        | cons x l ih => show K.add K.zero (lsum K (fun _ => K.zero) l) = K.zero; rw [ih]; lib_ring
      exact z l)
  exact id

/-- **Enstrophy step, LocalCont form.** -/
theorem enstrophy_step (P : Pos K) (w a a' k Nn : I → R) (h nu c X ρ σ : R) (l : List I)
    (step : ∀ i, sub K (a' i) (a i) = K.mul h (K.add (K.neg (K.mul nu (K.mul (k i) (K.add (a' i) (a i))))) (Nn i)))
    (hσ : K.mul σ σ = lsum K (fun i => K.mul (w i) (K.mul (k i) (K.mul (K.add (a' i) (a i)) (K.add (a' i) (a i))))) l)
    (hT : le K P (K.mul h (lsum K (fun i => K.mul (w i) (K.mul (K.add (a' i) (a i)) (Nn i))) l))
      (K.mul h (K.mul (K.mul (K.mul c X) ρ) σ))) :
    le K P (Zw K w a' l) (K.add (Zw K w a l) (K.mul h (sub K (K.mul (K.mul (K.mul c X) ρ) σ) (K.mul nu (K.mul σ σ))))) := by
  have id := enstrophy_identity K w a a' k Nn h nu l step
  rw [← hσ] at id
  unfold le at hT ⊢
  have e : sub K (K.add (Zw K w a l) (K.mul h (sub K (K.mul (K.mul (K.mul c X) ρ) σ) (K.mul nu (K.mul σ σ)))))
        (Zw K w a' l)
      = K.add (sub K (K.mul h (K.mul (K.mul (K.mul c X) ρ) σ))
          (K.mul h (lsum K (fun i => K.mul (w i) (K.mul (K.add (a' i) (a i)) (Nn i))) l)))
        (sub K (K.add (Zw K w a l) (K.mul h (lsum K (fun i => K.mul (w i) (K.mul (K.add (a' i) (a i)) (Nn i))) l)))
          (K.add (Zw K w a' l) (K.mul (K.mul h nu) (K.mul σ σ)))) := by lib_ring
  rw [e, ← id, show sub K (K.add (Zw K w a' l) (K.mul (K.mul h nu) (K.mul σ σ)))
      (K.add (Zw K w a' l) (K.mul (K.mul h nu) (K.mul σ σ))) = K.zero by lib_ring]
  rw [show K.add (sub K (K.mul h (K.mul (K.mul (K.mul c X) ρ) σ))
      (K.mul h (lsum K (fun i => K.mul (w i) (K.mul (K.add (a' i) (a i)) (Nn i))) l))) K.zero
      = sub K (K.mul h (K.mul (K.mul (K.mul c X) ρ) σ))
      (K.mul h (lsum K (fun i => K.mul (w i) (K.mul (K.add (a' i) (a i)) (Nn i))) l)) by lib_ring]
  exact hT

end EnstrophyStep

#print axioms EnstrophyStep.enstrophy_identity
#print axioms EnstrophyStep.enstrophy_step

ns/Epochs.lean

lean_all/ns/Epochs.lean

/-
  ns/Epochs.lean — T37 cycle 116: epochs of small dissipation (Leray's argument, discrete).

  Lean 4 core plus the shared lib/OField (totality), lib/Gronwall (le). For d_t ≥ 0, t < T:
    min_exists   ∃ i < T+1, ∀ j < T+1, d_i ≤ d_j                 (totality, no classical logic)
    epoch        Σ_{t<T+1} d_t ≤ S  ⇒  ∃ i < T+1, (T+1)·d_i ≤ S
  With LerayDiscrete (Σ_t hνD_t ≤ E_0): every run of every truncation has a step with
  hνD_t ≤ E_0/(T+1) — the discrete form of "regular times have full measure".
-/
import lib.OField
import NSChain

namespace Epochs
open DetChain LibRing LibCS LibGron LibOF

variable {R : Type} (K : CRing R) (P : Pos K)

def sumN' (d : Nat → R) : Nat → R
  | 0 => K.zero
  | n + 1 => K.add (sumN' d n) (d n)

theorem min_exists (F : OF K P) (d : Nat → R) : ∀ T, ∃ i, i < T + 1 ∧ ∀ j, j < T + 1 → le K P (d i) (d j)
  | 0 => ⟨0, Nat.zero_lt_succ 0, fun j hj => by
      have : j = 0 := Nat.le_zero.mp (Nat.le_of_lt_succ hj)
      rw [this]; exact le_refl K P _⟩
  | T + 1 => by
    have ⟨i, hi, hmin⟩ := min_exists F d T
    cases F.total (sub K (d (T + 1)) (d i)) with
    | inl h =>
      -- d_i ≤ d_{T+1}: keep i
      refine ⟨i, Nat.lt_succ_of_lt hi, fun j hj => ?_⟩
      cases Nat.eq_or_lt_of_le (Nat.le_of_lt_succ hj) with
      | inl e => rw [e]; exact h
      | inr l => exact hmin j l
    | inr h =>
      -- d_{T+1} ≤ d_i
      have h' : le K P (d (T + 1)) (d i) := by
        unfold le; rw [show sub K (d i) (d (T + 1)) = K.neg (sub K (d (T + 1)) (d i)) by lib_ring]; exact h
      refine ⟨T + 1, Nat.lt_succ_self _, fun j hj => ?_⟩
      cases Nat.eq_or_lt_of_le (Nat.le_of_lt_succ hj) with
      | inl e => rw [e]; exact le_refl K P _
      | inr l => exact le_trans K P h' (hmin j l)

theorem sum_ge (d : Nat → R) (m : R) : ∀ n, (∀ j, j < n → le K P m (d j)) →
    le K P (K.mul (castN K n) m) (sumN' K d n)
  | 0, _ => by unfold le; rw [show sub K (sumN' K d 0) (K.mul (castN K 0) m) = K.zero by
      show sub K K.zero (K.mul K.zero m) = K.zero; lib_ring]; exact P.nn_zero
  | n + 1, h => by
    have ih := sum_ge d m n (fun j hj => h j (Nat.lt_succ_of_lt hj))
    have hn := h n (Nat.lt_succ_self n)
    have := le_add2 K P ih hn
    rw [show K.add (K.mul (castN K n) m) m = K.mul (castN K (n + 1)) m by
      show _ = K.mul (K.add K.one (castN K n)) m; lib_ring] at this
    exact this

/-- **An epoch of small dissipation.** -/
theorem epoch (F : OF K P) (d : Nat → R) (S : R) (T : Nat) (hS : le K P (sumN' K d (T + 1)) S) :
    ∃ i, i < T + 1 ∧ le K P (K.mul (castN K (T + 1)) (d i)) S := by
  have ⟨i, hi, hmin⟩ := min_exists K P F d T
  exact ⟨i, hi, le_trans K P (sum_ge K P d (d i) (T + 1) hmin) hS⟩

end Epochs

#print axioms Epochs.min_exists
#print axioms Epochs.sum_ge
#print axioms Epochs.epoch

ns/EquiCont.lean

lean_all/ns/EquiCont.lean

/-
  ns/EquiCont.lean — T37 cycle 1081 (goal G656c): truncation-uniform equicontinuity of the implicit-viscosity
  Fourier–Galerkin scheme, over an ordered field. If on the time interval Z(a_t) ≤ Zb and A(a_t) ≤ Aa (EnstLocalTime),
  and the force satisfies Σ|N_t|² ≤ Σ|Nl(a_t,a_t)|² (e.g. Leray-projected), then for every t, n:
    equicont   Σ_r |a_{t+n,r} − a_{t,r}|² ≤ n·n·(2h²·Aa·(135ι²·Zb + ν²)),
  i.e. |a(t + nh) − a(t)|_{L²} ≤ (nh)·sqrt(2Aa(135ι²Zb + ν²)): Lipschitz in time with a constant independent of the
  number of modes (TimeLip + ForceUniform + TimeLipN).

  Lean 4 core plus ns/TimeLipN, ns/TimeLip, ns/ForceUniform, ns/NormCompare, ns/EnstGalerkin.
-/
import ns.TimeLipN
import ns.TimeLip
import ns.EnstGalerkin
import ns.TruncRate

namespace EquiCont
open DetChain NSChain DyadicSmallGlobal LibRing DyadicLarge FourierGalerkin GalerkinForce ForceBound ForceSob
  ModeCount CubeCount ForceUniform NormCompare EnstStep EnstGalerkin EnstDiff TimeLip TimeLipN

section Gen
variable {R : Type} (K : CRing R) (O : OrdR K) {I : Type}
variable (inv : R → R) (mul_inv : ∀ y, y ≠ K.zero → K.mul y (inv y) = K.one) (h10 : K.one ≠ K.zero)

theorem sumN_const_le (f : Nat → R) (C : R) (h : ∀ j, Le K O.toCone (f j) C) : ∀ n,
    Le K O.toCone (sumN K f n) (K.mul (castN K n) C)
  | 0 => ⟨K.zero, O.nn_zero, by show K.add K.zero K.zero = K.mul K.zero C; lib_ring⟩
  | n + 1 => by
    have ih := sumN_const_le f C h n
    have s := le_add' K O.toCone _ _ _ _ ih (h n)
    refine le_trans' K O.toCone _ _ _ s ⟨K.zero, O.nn_zero, ?_⟩
    show K.add (K.add (K.mul (castN K n) C) C) K.zero = K.mul (K.add K.one (castN K n)) C
    lib_ring

include mul_inv h10 in
theorem equicont (l : List I) (kz : I → Mode) (hd : List.Pairwise (fun a b => kz a ≠ kz b) l)
    (h1 : ∀ p, 1 ≤ nrm (kz p)) (M : Nat) (hM : ∀ p, p ∈ l → nrm (kz p) ≤ M + 1)
    (ι h ν Zb Aa : R) (hh : O.nn h) (hν : O.nn ν) (hZb : O.nn Zb) (a N : Nat → I → V3 R)
    (hN : ∀ t, Le K O.toCone (lsum K (fun r => dot3 K (N t r) (N t r)) l)
      (lsum K (fun r => dot3 K (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (a t) (a t) r)
        (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (a t) (a t) r)) l))
    (hs : ∀ t r, Step K h ν (wt K kz r) (a (t + 1) r) (a t r) (N t r))
    (hZ : ∀ t, Le K O.toCone (Zs K l kz a t) Zb) (hA : ∀ t, Le K O.toCone (As K l kz a t) Aa) (t n : Nat) :
    Le K O.toCone (lsum K (fun r => dot3 K (sub3 K (a (t + n) r) (a t r)) (sub3 K (a (t + n) r) (a t r))) l)
      (K.mul (castN K n) (K.mul (castN K n)
        (K.mul (K.mul (K.add K.one K.one) (K.mul h h)) (K.mul Aa (K.add (K.mul (K.mul (castN K 135) (K.mul ι ι)) Zb) (K.mul ν ν)))))) := by
  have hw : ∀ r, O.nn (wt K kz r) := fun r => dot3_nn K O.toCone _
  have two : O.nn (K.add K.one K.one) := O.nn_add _ _ (RatModel.nn_one' K O.toCone) (RatModel.nn_one' K O.toCone)
  -- one step bound
  have step : ∀ j, Le K O.toCone
      (lsum K (fun r => dot3 K (sub3 K (a (t + j + 1) r) (a (t + j) r)) (sub3 K (a (t + j + 1) r) (a (t + j) r))) l)
      (K.mul (K.mul (K.add K.one K.one) (K.mul h h)) (K.mul Aa (K.add (K.mul (K.mul (castN K 135) (K.mul ι ι)) Zb) (K.mul ν ν)))) := by
    intro j
    have tl := time_lip K O.toCone l h ν hh hν (wt K kz) hw (a (t + j + 1)) (a (t + j)) (N (t + j)) (hs (t + j))
    have fu := force_uniform K O inv mul_inv h10 l kz hd h1 M hM ι (kv K kz) (a (t + j)) (a (t + j))
    have hH := lsum_le K O.toCone _ _ (fun p => le_mul_right' K O.toCone (nrm4_le K O.toCone (kz p)) (dot3_nn K O.toCone (a (t + j) p))) l
    -- Σ|N|² ≤ ι²·A·(135·Z) ≤ 135 ι² Aa Zb
    have b1 := le_trans' K O.toCone _ _ _ (hN (t + j)) fu
    have nA : O.nn (As K l kz a (t + j)) := lsum_nn K O.toCone _ (fun p => O.nn_mul _ _ (O.nn_sq _) (dot3_nn K O.toCone _)) l
    have nZ : O.nn (Zs K l kz a (t + j)) := lsum_nn K O.toCone _ (fun p => O.nn_mul _ _ (hw p) (dot3_nn K O.toCone _)) l
    have b2 : Le K O.toCone (lsum K (fun r => dot3 K (N (t + j) r) (N (t + j) r)) l)
        (K.mul (K.mul ι ι) (K.mul Aa (K.mul (castN K 135) Zb))) := by
      refine le_trans' K O.toCone _ _ _ b1 (le_mul_nn K O.toCone _ _ _ (O.nn_sq ι) ?_)
      exact TruncRate.mm K O _ _ _ _ (lsum_nn K O.toCone _ (fun p => O.nn_mul _ _ (O.nn_sq _) (dot3_nn K O.toCone _)) l)
        (O.nn_mul _ _ (RatModel.nn_cast K O.toCone 135) hZb) (le_trans' K O.toCone _ _ _ hH (hA (t + j)))
        (le_mul_nn K O.toCone _ _ _ (RatModel.nn_cast K O.toCone 135) (hZ (t + j)))
    have s1 := le_add' K O.toCone _ _ _ _
      (le_mul_nn K O.toCone _ _ _ (O.nn_mul _ _ two (O.nn_sq h)) b2)
      (le_mul_nn K O.toCone _ _ _ (O.nn_mul _ _ two (O.nn_mul _ _ (O.nn_sq h) (O.nn_sq ν))) (hA (t + j)))
    refine le_trans' K O.toCone _ _ _ tl (le_trans' K O.toCone _ _ _ s1 ⟨K.zero, O.nn_zero, ?_⟩)
    show K.add (K.add _ _) K.zero = _
    lib_ring
  have ln := lip_n K O.toCone l (fun j => a (t + j)) n
  have sc := sumN_const_le K O _ _ step n
  have m := le_mul_nn K O.toCone (castN K n) _ _ (RatModel.nn_cast K O.toCone n) sc
  exact le_trans' K O.toCone _ _ _ ln m

end Gen

end EquiCont

#print axioms EquiCont.sumN_const_le
#print axioms EquiCont.equicont

ns/EquiContW.lean

lean_all/ns/EquiContW.lean

/-
  ns/EquiContW.lean — T37 cycle 1137 (goal G744): weighted (H¹-level) equicontinuity of the implicit-viscosity
  scheme, over an ordered ring. For weights μ_r, V_r ≥ 0:
    lip_nW      Σ_r μ_r|a_{n,r} − a_{0,r}|² ≤ n·Σ_{j<n} Σ_r μ_r|a_{j+1,r} − a_{j,r}|²
    equicontW   if Σ V|N_t|² ≤ G and Σ V w²|a_t|² ≤ B for all t, then
                Σ_r V_r|a_{t+n,r} − a_{t,r}|² ≤ n·n·(2h²·G + 2h²ν²·B)
  With V = w: Lipschitz in time in the enstrophy norm, G from ForceH2 and B from EnstH2 (truncation-uniform).

  Lean 4 core plus ns/TimeLipN (mode_lipN, lsum_sumN), ns/TimeLipW, ns/EquiCont (sumN_const_le).
-/
import ns.TimeLipW
import ns.EquiCont
import ns.WCS

namespace EquiContW
open DetChain NSChain DyadicSmallGlobal LibRing DyadicLarge FourierGalerkin GalerkinForce ForceBound ModeCount CubeCount
  ForceUniform NormCompare EnstGalerkin EnstStep EnstDiff TimeLipN TimeLipW

section Gen
variable {R : Type} (K : CRing R) (O : OrdR K) {I : Type}

theorem lip_nW (l : List I) (μ : I → R) (hμ : ∀ r, O.nn (μ r)) (a : Nat → I → V3 R) (n : Nat) :
    Le K O.toCone (lsum K (fun r => K.mul (μ r) (dot3 K (sub3 K (a n r) (a 0 r)) (sub3 K (a n r) (a 0 r)))) l)
      (K.mul (castN K n) (sumN K (fun j => lsum K (fun r => K.mul (μ r) (dot3 K (sub3 K (a (j + 1) r) (a j r)) (sub3 K (a (j + 1) r) (a j r)))) l) n)) := by
  have t := lsum_le K O.toCone _ _ (fun r => le_mul_nn K O.toCone (μ r) _ _ (hμ r) (mode_lipN K O.toCone (fun j => a j r) n)) l
  refine le_trans' K O.toCone _ _ _ t ⟨K.zero, O.nn_zero, ?_⟩
  rw [add_zero']
  have e1 : lsum K (fun r => K.mul (μ r) (K.mul (castN K n) (sumN K (fun j => dot3 K (sub3 K (a (j + 1) r) (a j r)) (sub3 K (a (j + 1) r) (a j r))) n))) l
      = K.mul (castN K n) (lsum K (fun r => sumN K (fun j => K.mul (μ r) (dot3 K (sub3 K (a (j + 1) r) (a j r)) (sub3 K (a (j + 1) r) (a j r)))) n) l) := by
    rw [← lsum_mul_left]
    refine lsum_ext K _ _ (fun r => ?_) l
    show K.mul (μ r) (K.mul (castN K n) (sumN K (fun j => dot3 K (sub3 K (a (j + 1) r) (a j r)) (sub3 K (a (j + 1) r) (a j r))) n)) = _
    rw [← WCS.sumN_mul_l K (μ r)]
    generalize sumN K (fun j => dot3 K (sub3 K (a (j + 1) r) (a j r)) (sub3 K (a (j + 1) r) (a j r))) n = S
    lib_ring
  rw [e1, lsum_sumN K (fun r j => K.mul (μ r) (dot3 K (sub3 K (a (j + 1) r) (a j r)) (sub3 K (a (j + 1) r) (a j r)))) l n]

theorem equicontW (l : List I) (kz : I → Mode) (h ν G B : R) (hh : O.nn h) (hν : O.nn ν) (V : I → R) (hV : ∀ r, O.nn (V r))
    (a N : Nat → I → V3 R) (hs : ∀ t r, Step K h ν (wt K kz r) (a (t + 1) r) (a t r) (N t r))
    (hG : ∀ t, Le K O.toCone (lsum K (fun r => K.mul (V r) (dot3 K (N t r) (N t r))) l) G)
    (hB : ∀ t, Le K O.toCone (lsum K (fun r => K.mul (K.mul (V r) (K.mul (wt K kz r) (wt K kz r))) (dot3 K (a t r) (a t r))) l) B)
    (t n : Nat) :
    Le K O.toCone (lsum K (fun r => K.mul (V r) (dot3 K (sub3 K (a (t + n) r) (a t r)) (sub3 K (a (t + n) r) (a t r)))) l)
      (K.mul (castN K n) (K.mul (castN K n)
        (K.add (K.mul (K.mul (K.add K.one K.one) (K.mul h h)) G) (K.mul (K.mul (K.add K.one K.one) (K.mul (K.mul h h) (K.mul ν ν))) B)))) := by
  have hw : ∀ r, O.nn (wt K kz r) := fun r => dot3_nn K O.toCone _
  have two : O.nn (K.add K.one K.one) := O.nn_add _ _ (RatModel.nn_one' K O.toCone) (RatModel.nn_one' K O.toCone)
  have step : ∀ j, Le K O.toCone
      (lsum K (fun r => K.mul (V r) (dot3 K (sub3 K (a (t + j + 1) r) (a (t + j) r)) (sub3 K (a (t + j + 1) r) (a (t + j) r)))) l)
      (K.add (K.mul (K.mul (K.add K.one K.one) (K.mul h h)) G) (K.mul (K.mul (K.add K.one K.one) (K.mul (K.mul h h) (K.mul ν ν))) B)) := by
    intro j
    have tl := time_lipW K O.toCone l h ν hh hν (wt K kz) V hw hV (a (t + j + 1)) (a (t + j)) (N (t + j)) (hs (t + j))
    exact le_trans' K O.toCone _ _ _ tl (le_add' K O.toCone _ _ _ _
      (le_mul_nn K O.toCone _ _ _ (O.nn_mul _ _ two (O.nn_sq h)) (hG (t + j)))
      (le_mul_nn K O.toCone _ _ _ (O.nn_mul _ _ two (O.nn_mul _ _ (O.nn_sq h) (O.nn_sq ν))) (hB (t + j))))
  have ln := lip_nW K O l V hV (fun j => a (t + j)) n
  have sc := EquiCont.sumN_const_le K O _ _ step n
  have m := le_mul_nn K O.toCone (castN K n) _ _ (RatModel.nn_cast K O.toCone n) sc
  exact le_trans' K O.toCone _ _ _ ln m

end Gen

end EquiContW

#print axioms EquiContW.lip_nW
#print axioms EquiContW.equicontW

ns/ErrRec.lean

lean_all/ns/ErrRec.lean

/-
  ns/ErrRec.lean — T37 cycle 1117 (goal G717, piece c3): the abstract error recursion for the step-size comparison,
  over an ordered ring:
    one_le_pwr   1 ≤ g  ⇒  1 ≤ gⁿ
    rec_src      x_{n+1} ≤ g·x_n + s,  1 ≤ g,  s ≥ 0  ⇒  x_n ≤ gⁿ·(x_0 + n·s)
    rec_split    k·x_{n+1} ≤ k(1+k)·L_n + (1+k)·Δ_n,  L_n ≤ G·x_n,  Δ_n ≤ k·k·(k·C)  (k ≥ 0, G ≥ 0)
                 ⇒  k·x_{n+1} ≤ k·((1+k)G·x_n + (1+k)·k·k·C)
    err_bound    combined, with x₀ = 0 and g = (1+k)G ≥ 1:  x_n ≤ gⁿ·(n·(1+k)k²C)
  With x_n the Qd-error between the 2k-scheme and the k-scheme at time 2nk, L_n the two-step Lipschitz image
  (EnstLipschitz, T = 2), Δ_n the O(k³) defect (DefectOrder), and x_0 = 0: x_n ≤ gⁿ·n·(1+k)k²C = O(k) for nk ≤ T.

  Lean 4 core plus ns/EnstGronwall (pwr), ns/TruncRate (mm), lib/RingTac.
-/
import ns.EnstGronwall
import ns.TruncRate
import ns.EnstGlobal

namespace ErrRec
open DetChain NSChain DyadicSmallGlobal LibRing DyadicLarge EnstGronwall

section Gen
variable {R : Type} (K : CRing R) (O : OrdR K)

theorem one_le_pwr (g : R) (hg : Le K O.toCone K.one g) : ∀ n, Le K O.toCone K.one (pwr K g n)
  | 0 => le_refl' K O.toCone _
  | n + 1 => by
    have ih := one_le_pwr g hg n
    have m := TruncRate.mm K O _ _ _ _ (RatModel.nn_one' K O.toCone) (TruncRate.nn_of_le K O _ _ (RatModel.nn_one' K O.toCone) ih) hg ih
    refine le_trans' K O.toCone _ _ _ ⟨K.zero, O.nn_zero, ?_⟩ m
    lib_ring

theorem rec_src (g s : R) (x : Nat → R) (hg : Le K O.toCone K.one g) (hs : O.nn s)
    (step : ∀ n, Le K O.toCone (x (n + 1)) (K.add (K.mul g (x n)) s)) :
    ∀ n, Le K O.toCone (x n) (K.mul (pwr K g n) (K.add (x 0) (K.mul (castN K n) s)))
  | 0 => ⟨K.zero, O.nn_zero, by show K.add (x 0) K.zero = K.mul K.one (K.add (x 0) (K.mul K.zero s)); lib_ring⟩
  | n + 1 => by
    have ih := rec_src g s x hg hs step n
    have hgn : O.nn g := TruncRate.nn_of_le K O _ _ (RatModel.nn_one' K O.toCone) hg
    have a := le_mul_nn K O.toCone g _ _ hgn ih
    have p1 := one_le_pwr K O g hg (n + 1)
    have b := ForceSob.le_mul_right' K O.toCone p1 hs
    have c := le_add' K O.toCone _ _ _ _ a b
    have c' := le_trans' K O.toCone _ _ _ ⟨K.zero, O.nn_zero, (by lib_ring :
      K.add (K.add (K.mul g (x n)) s) K.zero = K.add (K.mul g (x n)) (K.mul K.one s))⟩ c
    refine le_trans' K O.toCone _ _ _ (step n) (le_trans' K O.toCone _ _ _ c' ⟨K.zero, O.nn_zero, ?_⟩)
    show K.add (K.add (K.mul g (K.mul (pwr K g n) (K.add (x 0) (K.mul (castN K n) s)))) (K.mul (K.mul g (pwr K g n)) s)) K.zero
      = K.mul (K.mul g (pwr K g n)) (K.add (x 0) (K.mul (K.add K.one (castN K n)) s))
    generalize pwr K g n = P
    generalize castN K n = c
    lib_ring

theorem rec_split (k G C x x' L Δ : R) (hk : O.nn k)
    (hsp : Le K O.toCone (K.mul k x') (K.add (K.mul (K.mul k (K.add K.one k)) L) (K.mul (K.add K.one k) Δ)))
    (hL : Le K O.toCone L (K.mul G x)) (hΔ : Le K O.toCone Δ (K.mul (K.mul k k) (K.mul k C))) :
    Le K O.toCone (K.mul k x')
      (K.mul k (K.add (K.mul (K.mul (K.add K.one k) G) x) (K.mul (K.add K.one k) (K.mul (K.mul k k) C)))) := by
  have n1k : O.nn (K.add K.one k) := O.nn_add _ _ (RatModel.nn_one' K O.toCone) hk
  have a := le_add' K O.toCone _ _ _ _ (le_mul_nn K O.toCone _ _ _ (O.nn_mul _ _ hk n1k) hL) (le_mul_nn K O.toCone _ _ _ n1k hΔ)
  refine le_trans' K O.toCone _ _ _ hsp (le_trans' K O.toCone _ _ _ a ⟨K.zero, O.nn_zero, ?_⟩)
  lib_ring

/-- the step-size error bound: from the split recursion with x₀ = 0, x_n ≤ gⁿ·(n·((1+k)k²C)), g = (1+k)G ≥ 1 -/
theorem err_bound (k G C : R) (x L Δ : Nat → R) (hk : O.nn k) (hk0 : k ≠ K.zero) (hC : O.nn C)
    (hg : Le K O.toCone K.one (K.mul (K.add K.one k) G)) (hx0 : x 0 = K.zero)
    (hsp : ∀ n, Le K O.toCone (K.mul k (x (n + 1))) (K.add (K.mul (K.mul k (K.add K.one k)) (L n)) (K.mul (K.add K.one k) (Δ n))))
    (hL : ∀ n, Le K O.toCone (L n) (K.mul G (x n))) (hΔ : ∀ n, Le K O.toCone (Δ n) (K.mul (K.mul k k) (K.mul k C))) (n : Nat) :
    Le K O.toCone (x n) (K.mul (pwr K (K.mul (K.add K.one k) G) n)
      (K.mul (castN K n) (K.mul (K.add K.one k) (K.mul (K.mul k k) C)))) := by
  have n1k : O.nn (K.add K.one k) := O.nn_add _ _ (RatModel.nn_one' K O.toCone) hk
  have step : ∀ n, Le K O.toCone (x (n + 1)) (K.add (K.mul (K.mul (K.add K.one k) G) (x n)) (K.mul (K.add K.one k) (K.mul (K.mul k k) C))) := by
    intro n
    have r := rec_split K O k G C (x n) (x (n + 1)) (L n) (Δ n) hk (hsp n) (hL n) (hΔ n)
    refine EnstGlobal.le_of_mul_le K O k _ _ hk hk0 (le_trans' K O.toCone _ _ _ r ⟨K.zero, O.nn_zero, ?_⟩)
    lib_ring
  have b := rec_src K O _ _ x hg (O.nn_mul _ _ n1k (O.nn_mul _ _ (O.nn_sq k) hC)) step n
  rw [hx0] at b
  refine le_trans' K O.toCone _ _ _ b ⟨K.zero, O.nn_zero, ?_⟩
  lib_ring

end Gen

end ErrRec

#print axioms ErrRec.one_le_pwr
#print axioms ErrRec.rec_src
#print axioms ErrRec.rec_split
#print axioms ErrRec.err_bound

ns/EventDecay.lean

lean_all/ns/EventDecay.lean

/-
  ns/EventDecay.lean — T37 cycle 1341 (goal G799 — done): eventual exponential enstrophy decay for arbitrary data,
  over an ordered field. Under the cfl_global hypotheses, h, ν ≠ 0, hν·c_M ≤ 1, c = 135ι², W = E_0 + hν·Z_0 and
  2·(2c·W) ≤ ν²·T, T = (n + 1)hν, there is t ≤ n with
    event_decay   2c·Q_{t+1} ≤ ν³   and   (2 + 2hν)ˢ·Q_{t+1+s} ≤ (2 + hν)ˢ·Q_{t+1} for every s,
  Q = νZ + hν²A: after a waiting time independent of the truncation the enstrophy decays geometrically, rate ≈ ν/2.

  Lean 4 core plus ns/EventH2 (good_time), ns/EnstExp (proj_exp).
-/
import ns.EventH2
import ns.EnstExp

namespace EventDecay
open DetChain NSChain DyadicSmallGlobal LibRing DyadicLarge FourierGalerkin GalerkinForce ForceBound ForceSob
  ModeCount CubeCount ForceUniform NormCompare EnstStep EnstGalerkin EnstGlobal CFLStep CFLGlobal EventSmall EventReg
  EventH2

section Gen
variable {R : Type} (K : CRing R) (O : OrdR K) {I : Type}
variable (inv : R → R) (mul_inv : ∀ y, y ≠ K.zero → K.mul y (inv y) = K.one) (h10 : K.one ≠ K.zero)

include mul_inv h10 in
theorem event_decay (l : List I) (kz : I → Mode) (hd : List.Pairwise (fun a b => kz a ≠ kz b) l)
    (h1 : ∀ p, 1 ≤ nrm (kz p)) (M : Nat) (hM : ∀ p, p ∈ l → nrm (kz p) ≤ M + 1)
    (ι h ν Es : R) (hh : O.nn h) (hν : O.nn ν) (hh0 : h ≠ K.zero) (hν0 : ν ≠ K.zero) (hEs : O.nn Es)
    (hstepM : Le K O.toCone (K.mul (K.mul h ν) (cM K M)) K.one)
    (a N : Nat → I → V3 R)
    (hs : ∀ t r, Step K h ν (wt K kz r) (a (t + 1) r) (a t r) (N t r))
    (hneut : ∀ t, lsum K (fun r => dot3 K (N t r) (a t r)) l = K.zero)
    (hP : ∀ t, Le K O.toCone (lsum K (fun r => dot3 K (N t r) (N t r)) l)
      (lsum K (fun r => dot3 K (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (a t) (a t) r)
        (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (a t) (a t) r)) l))
    (hcfl : Le K O.toCone (beta K M ι h ν Es) (K.mul h ν))
    (h0 : Le K O.toCone (K.add (lsum K (fun r => dot3 K (a 0 r) (a 0 r)) l)
      (K.mul (beta K M ι h ν Es) (lsum K (fun r => K.mul (wt K kz r) (dot3 K (a 0 r) (a 0 r))) l))) Es) (n : Nat)
    (htime : Le K O.toCone
      (K.mul (K.add K.one K.one) (K.mul (K.add (K.mul (castN K 135) (K.mul ι ι)) (K.mul (castN K 135) (K.mul ι ι)))
        (K.add (lsum K (fun r => dot3 K (a 0 r) (a 0 r)) l)
          (K.mul (K.mul h ν) (lsum K (fun r => K.mul (wt K kz r) (dot3 K (a 0 r) (a 0 r))) l)))))
      (K.mul (K.mul ν ν) (K.mul (castN K (n + 1)) (K.mul h ν)))) :
    ∃ t, t < n + 1 ∧
      Le K O.toCone (K.mul (K.add (K.mul (castN K 135) (K.mul ι ι)) (K.mul (castN K 135) (K.mul ι ι)))
        (Q K ν h (Zs K l kz a) (As K l kz a) (t + 1))) (K.mul ν (K.mul ν ν)) ∧
      ∀ s, Le K O.toCone (K.mul (pw K (K.add (K.add K.one K.one) (K.add (K.mul h ν) (K.mul h ν))) s)
          (Q K ν h (Zs K l kz a) (As K l kz a) (t + 1 + s)))
        (K.mul (pw K (K.add (K.add K.one K.one) (K.mul h ν)) s) (Q K ν h (Zs K l kz a) (As K l kz a) (t + 1))) := by
  have hc : O.nn (K.mul (castN K 135) (K.mul ι ι)) := O.nn_mul _ _ (RatModel.nn_cast K O.toCone 135) (O.nn_sq ι)
  have ⟨t, ht, hq⟩ := good_time K O inv mul_inv h10 l kz hd h1 M hM ι h ν Es
    (K.add (K.mul (castN K 135) (K.mul ι ι)) (K.mul (castN K 135) (K.mul ι ι)))
    hh hν hh0 hν0 hEs (O.nn_add _ _ hc hc) hstepM a N hs hneut hP hcfl h0 n htime
  refine ⟨t, ht, hq, fun s => ?_⟩
  exact EnstExp.proj_exp K O inv mul_inv h10 l kz hd h1 M hM ι h ν hh hν hν0
    (fun s => a (t + 1 + s)) (fun s => N (t + 1 + s)) (fun s => hP (t + 1 + s)) (fun s r => hs (t + 1 + s) r) hq s

end Gen

end EventDecay

#print axioms EventDecay.event_decay

ns/EventH2.lean

lean_all/ns/EventH2.lean

/-
  ns/EventH2.lean — T37 cycle 1325 (goal G792b — done): discrete eventual H² regularity for arbitrary data, over an
  ordered field. Implicit Fourier–Galerkin scheme, Leray-projected energy-neutral force (Σ|N|² ≤ Σ|Nl|² and
  Σw|N|² ≤ Σw|Nl|²), cfl_global hypotheses, step bound hν·c_M ≤ 1, h, ν, ι ≠ 0, c = 135ι², W = E_0 + hν·Z_0.
  If T_1 = (n + 1)hν and T_2 = (n_2 + 1)hν satisfy
      2·(2c·W) ≤ ν²·T_1        and        2·1080ι² ≤ c·T_2       (i.e. T_2 ≥ 16, a pure number)
  then there is a time t_2 (≤ n + 1 + n_2) with, writing Q₂ = νA + hν²B (A = Σw²|a|², B = Σw³|a|²),
    h2_eventual   1080ι²·Q₂(t_2) ≤ ν³   and   Q₂(t_2 + s) ≤ Q₂(t_2) for every s,
  so from t_2 on the H² norm stays below ν²/(1080ι²). Waiting times and bounds do not depend on the truncation.
  Pieces: alg_q (Q ≤ 2νZ from A ≤ c_M Z, hνc_M ≤ 1), alg_cancel1/2 (cancel the time factor), B_le_A (B ≤ c_M A),
  proj_step (the enstrophy step for the projected force), good_time (κ·Q_{t+1} ≤ ν³ for a general κ).

  Lean 4 core plus ns/EventRegU, ns/EnstDiss, ns/EnstH2 (h2_global, cH), ns/EnstProj.
-/
import ns.EventRegU
import ns.EnstDiss
import ns.EnstH2

namespace EventH2
open DetChain NSChain DyadicSmallGlobal LibRing DyadicLarge FourierGalerkin GalerkinForce ForceBound ForceSob
  ModeCount CubeCount ForceUniform NormCompare EnstStep EnstGalerkin EnstGlobal CFLStep CFLGlobal EventSmall EventReg

section Gen
variable {R : Type} (K : CRing R) (O : OrdR K) {I : Type}
variable (inv : R → R) (mul_inv : ∀ y, y ≠ K.zero → K.mul y (inv y) = K.one) (h10 : K.one ≠ K.zero)

/-- νZ + hν²A ≤ 2νZ when A ≤ c_M·Z and hν·c_M ≤ 1 -/
theorem alg_q (ν h cm Z A : R) (hν : O.nn ν) (hh : O.nn h) (hZ : O.nn Z)
    (hA : Le K O.toCone A (K.mul cm Z)) (hs : Le K O.toCone (K.mul (K.mul h ν) cm) K.one) :
    Le K O.toCone (K.add (K.mul ν Z) (K.mul (K.mul h (K.mul ν ν)) A)) (K.mul (K.add K.one K.one) (K.mul ν Z)) := by
  have m1 := le_mul_nn K O.toCone (K.mul h (K.mul ν ν)) _ _ (O.nn_mul _ _ hh (O.nn_sq ν)) hA
  have m2 := le_mul_right' K O.toCone hs (O.nn_mul _ _ hν hZ)
  have s := le_add' K O.toCone _ _ _ _ (le_refl' K O.toCone (K.mul ν Z))
    (le_trans' K O.toCone _ _ _ m1 (le_trans' K O.toCone _ _ _ ⟨K.zero, O.nn_zero, ?_⟩ m2))
  · refine le_trans' K O.toCone _ _ _ s ⟨K.zero, O.nn_zero, ?_⟩
    show K.add (K.add (K.mul ν Z) (K.mul K.one (K.mul ν Z))) K.zero = _
    lib_ring
  · lib_ring

/-- m·Z ≤ W, Q ≤ 2νZ, 2κW ≤ ν²m, m ≠ 0  ⇒  κQ ≤ ν³ -/
theorem alg_cancel1 (κ ν m Z W Qv : R) (hκ : O.nn κ) (hν : O.nn ν) (hm : O.nn m) (hm0 : m ≠ K.zero)
    (hz : Le K O.toCone (K.mul m Z) W) (hQ : Le K O.toCone Qv (K.mul (K.add K.one K.one) (K.mul ν Z)))
    (ht : Le K O.toCone (K.mul (K.add K.one K.one) (K.mul κ W)) (K.mul (K.mul ν ν) m)) :
    Le K O.toCone (K.mul κ Qv) (K.mul ν (K.mul ν ν)) := by
  have two : O.nn (K.add K.one K.one) := O.nn_add _ _ (RatModel.nn_one' K O.toCone) (RatModel.nn_one' K O.toCone)
  have k1 := le_mul_nn K O.toCone (K.mul m κ) _ _ (O.nn_mul _ _ hm hκ) hQ
  have k2 := le_mul_nn K O.toCone (K.mul (K.add K.one K.one) (K.mul κ ν)) _ _ (O.nn_mul _ _ two (O.nn_mul _ _ hκ hν)) hz
  have k3 := le_mul_nn K O.toCone ν _ _ hν ht
  have key : Le K O.toCone (K.mul m (K.mul κ Qv)) (K.mul m (K.mul ν (K.mul ν ν))) := by
    refine le_trans' K O.toCone _ _ _ ⟨K.zero, O.nn_zero, ?_⟩ (le_trans' K O.toCone _ _ _ k1
      (le_trans' K O.toCone _ _ _ ⟨K.zero, O.nn_zero, ?_⟩ (le_trans' K O.toCone _ _ _ k2
        (le_trans' K O.toCone _ _ _ ⟨K.zero, O.nn_zero, ?_⟩ (le_trans' K O.toCone _ _ _ k3 ⟨K.zero, O.nn_zero, ?_⟩)))))
    all_goals lib_ring
  exact le_of_mul_le K O _ _ _ hm hm0 key

/-- m·(νA) ≤ 2Q₁, 2cQ₁ ≤ ν³, 2d ≤ c·m, Q₂ ≤ 2νA, c·m ≠ 0  ⇒  d·Q₂ ≤ ν³ -/
theorem alg_cancel2 (c d m ν A Q1 Q2 : R) (hc : O.nn c) (hd : O.nn d) (hm : O.nn m) (hν : O.nn ν)
    (hcm0 : K.mul c m ≠ K.zero)
    (hP : Le K O.toCone (K.mul m (K.mul ν A)) (K.add Q1 Q1))
    (hS : Le K O.toCone (K.mul (K.add c c) Q1) (K.mul ν (K.mul ν ν)))
    (hT : Le K O.toCone (K.mul (K.add K.one K.one) d) (K.mul c m))
    (hQ : Le K O.toCone Q2 (K.mul (K.add K.one K.one) (K.mul ν A))) :
    Le K O.toCone (K.mul d Q2) (K.mul ν (K.mul ν ν)) := by
  have two : O.nn (K.add K.one K.one) := O.nn_add _ _ (RatModel.nn_one' K O.toCone) (RatModel.nn_one' K O.toCone)
  have k1 : Le K O.toCone (K.mul c (K.mul m (K.mul ν A))) (K.mul ν (K.mul ν ν)) :=
    le_trans' K O.toCone _ _ _ (le_mul_nn K O.toCone c _ _ hc hP)
      (le_trans' K O.toCone _ _ _ ⟨K.zero, O.nn_zero, by lib_ring⟩ hS)
  have k2 := le_mul_nn K O.toCone (K.mul (K.mul c m) d) _ _ (O.nn_mul _ _ (O.nn_mul _ _ hc hm) hd) hQ
  have k3 := le_mul_nn K O.toCone (K.mul (K.add K.one K.one) d) _ _ (O.nn_mul _ _ two hd) k1
  have k4 := le_mul_right' K O.toCone hT (O.nn_mul _ _ hν (O.nn_sq ν))
  have key : Le K O.toCone (K.mul (K.mul c m) (K.mul d Q2)) (K.mul (K.mul c m) (K.mul ν (K.mul ν ν))) := by
    refine le_trans' K O.toCone _ _ _ ⟨K.zero, O.nn_zero, ?_⟩ (le_trans' K O.toCone _ _ _ k2
      (le_trans' K O.toCone _ _ _ ⟨K.zero, O.nn_zero, ?_⟩ (le_trans' K O.toCone _ _ _ k3
        (le_trans' K O.toCone _ _ _ k4 ⟨K.zero, O.nn_zero, ?_⟩))))
    all_goals lib_ring
  exact le_of_mul_le K O _ _ _ (O.nn_mul _ _ hc hm) hcm0 key

theorem B_le_A (l : List I) (kz : I → Mode) (M : Nat) (hM : ∀ p, p ∈ l → nrm (kz p) ≤ M + 1) (a : I → V3 R) :
    Le K O.toCone (lsum K (fun r => K.mul (K.mul (K.mul (wt K kz r) (wt K kz r)) (wt K kz r)) (dot3 K (a r) (a r))) l)
      (K.mul (cM K M) (lsum K (fun r => K.mul (K.mul (wt K kz r) (wt K kz r)) (dot3 K (a r) (a r))) l)) := by
  rw [← lsum_mul_left]
  apply lsum_le_mem K O
  intro r hr
  have hwr : O.nn (wt K kz r) := dot3_nn K O.toCone _
  have m := ForceSob.le_mul_right' K O.toCone (wt_le K O kz M r (hM r hr)) (O.nn_mul _ _ (O.nn_sq (wt K kz r)) (dot3_nn K O.toCone (a r)))
  exact le_trans' K O.toCone _ _ _ ⟨K.zero, O.nn_zero, by lib_ring⟩ (le_trans' K O.toCone _ _ _ m ⟨K.zero, O.nn_zero, by lib_ring⟩)

include mul_inv h10 in
/-- the enstrophy step for any force whose energy is at most that of the Galerkin force -/
theorem proj_step (l : List I) (kz : I → Mode) (hd : List.Pairwise (fun a b => kz a ≠ kz b) l)
    (h1 : ∀ p, 1 ≤ nrm (kz p)) (M : Nat) (hM : ∀ p, p ∈ l → nrm (kz p) ≤ M + 1)
    (ι h ν : R) (hh : O.nn h) (hν : O.nn ν) (a N : Nat → I → V3 R)
    (hN : ∀ t, Le K O.toCone (lsum K (fun r => dot3 K (N t r) (N t r)) l)
      (lsum K (fun r => dot3 K (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (a t) (a t) r)
        (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (a t) (a t) r)) l))
    (hs : ∀ t r, Step K h ν (wt K kz r) (a (t + 1) r) (a t r) (N t r)) (s : Nat) :
    Le K O.toCone (Q K ν h (Zs K l kz a) (As K l kz a) (s + 1))
      (K.add (K.mul ν (Zs K l kz a s)) (K.mul (K.mul h (K.mul (castN K 135) (K.mul ι ι))) (K.mul (As K l kz a s) (Zs K l kz a s)))) := by
  have hw : ∀ r, O.nn (wt K kz r) := fun r => dot3_nn K O.toCone _
  have hZn : O.nn (Zs K l kz a s) := lsum_nn K O.toCone _ (fun r => O.nn_mul _ _ (hw r) (dot3_nn K O.toCone _)) l
  have st := enst_step K O.toCone l h ν hh hν (wt K kz) hw (a (s + 1)) (a s) _ (hs s)
  have fu := force_uniform K O inv mul_inv h10 l kz hd h1 M hM ι (kv K kz) (a s) (a s)
  have hH : Le K O.toCone (lsum K (fun p => K.mul (K.mul (mu K kz p) (mu K kz p)) (dot3 K (a s p) (a s p))) l)
      (As K l kz a s) :=
    lsum_le K O.toCone _ _ (fun p => le_mul_right' K O.toCone (nrm4_le K O.toCone (kz p)) (dot3_nn K O.toCone _)) l
  have hb : Le K O.toCone (lsum K (fun r => dot3 K (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (a s) (a s) r)
      (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (a s) (a s) r)) l)
      (K.mul (K.mul (castN K 135) (K.mul ι ι)) (K.mul (As K l kz a s) (Zs K l kz a s))) := by
    have m1 := le_mul_right' K O.toCone hH (O.nn_mul _ _ (RatModel.nn_cast K O.toCone 135) hZn)
    have m2 := le_mul_nn K O.toCone (K.mul ι ι) _ _ (O.nn_sq ι) m1
    have t2 := le_trans' K O.toCone _ _ _ fu m2
    have e : K.mul (K.mul ι ι) (K.mul (As K l kz a s) (K.mul (castN K 135) (Zs K l kz a s)))
        = K.mul (K.mul (castN K 135) (K.mul ι ι)) (K.mul (As K l kz a s) (Zs K l kz a s)) := by lib_ring
    rw [e] at t2
    exact t2
  have hb := le_trans' K O.toCone _ _ _ (hN s) hb
  have hb2 := le_mul_nn K O.toCone h _ _ hh hb
  have st2 := le_trans' K O.toCone _ _ _ st (le_add' K O.toCone _ _ _ _ (le_refl' K O.toCone _) hb2)
  have e2 : K.mul h (K.mul (K.mul (castN K 135) (K.mul ι ι)) (K.mul (As K l kz a s) (Zs K l kz a s)))
      = K.mul (K.mul h (K.mul (castN K 135) (K.mul ι ι))) (K.mul (As K l kz a s) (Zs K l kz a s)) := by lib_ring
  rw [e2] at st2
  exact st2

include mul_inv h10 in
/-- a good time for a general constant κ: κ·Q_{t+1} ≤ ν³ -/
theorem good_time (l : List I) (kz : I → Mode) (hd : List.Pairwise (fun a b => kz a ≠ kz b) l)
    (h1 : ∀ p, 1 ≤ nrm (kz p)) (M : Nat) (hM : ∀ p, p ∈ l → nrm (kz p) ≤ M + 1)
    (ι h ν Es κ : R) (hh : O.nn h) (hν : O.nn ν) (hh0 : h ≠ K.zero) (hν0 : ν ≠ K.zero) (hEs : O.nn Es) (hκ : O.nn κ)
    (hstepM : Le K O.toCone (K.mul (K.mul h ν) (cM K M)) K.one)
    (a N : Nat → I → V3 R)
    (hs : ∀ t r, Step K h ν (wt K kz r) (a (t + 1) r) (a t r) (N t r))
    (hneut : ∀ t, lsum K (fun r => dot3 K (N t r) (a t r)) l = K.zero)
    (hP : ∀ t, Le K O.toCone (lsum K (fun r => dot3 K (N t r) (N t r)) l)
      (lsum K (fun r => dot3 K (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (a t) (a t) r)
        (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (a t) (a t) r)) l))
    (hcfl : Le K O.toCone (beta K M ι h ν Es) (K.mul h ν))
    (h0 : Le K O.toCone (K.add (lsum K (fun r => dot3 K (a 0 r) (a 0 r)) l)
      (K.mul (beta K M ι h ν Es) (lsum K (fun r => K.mul (wt K kz r) (dot3 K (a 0 r) (a 0 r))) l))) Es) (n : Nat)
    (htime : Le K O.toCone
      (K.mul (K.add K.one K.one) (K.mul κ
        (K.add (lsum K (fun r => dot3 K (a 0 r) (a 0 r)) l)
          (K.mul (K.mul h ν) (lsum K (fun r => K.mul (wt K kz r) (dot3 K (a 0 r) (a 0 r))) l)))))
      (K.mul (K.mul ν ν) (K.mul (castN K (n + 1)) (K.mul h ν)))) :
    ∃ t, t < n + 1 ∧ Le K O.toCone (K.mul κ (Q K ν h (Zs K l kz a) (As K l kz a) (t + 1))) (K.mul ν (K.mul ν ν)) := by
  have ⟨t, ht, hz⟩ := event_small K O inv mul_inv h10 l kz hd h1 M hM ι h ν Es hh hν hEs a N hs hneut hP hcfl h0 n
  have nm : O.nn (K.mul (castN K (n + 1)) (K.mul h ν)) := O.nn_mul _ _ (RatModel.nn_cast K O.toCone _) (O.nn_mul _ _ hh hν)
  have m0 : K.mul (castN K (n + 1)) (K.mul h ν) ≠ K.zero := by
    intro e
    cases O.noZD _ _ e with
    | inl e1 => exact cast_succ_ne K O h10 n e1
    | inr e2 =>
      cases O.noZD _ _ e2 with
      | inl e3 => exact hh0 e3
      | inr e4 => exact hν0 e4
  have hZ : O.nn (Zs K l kz a (t + 1)) := lsum_nn K O.toCone _ (fun r => O.nn_mul _ _ (dot3_nn K O.toCone _) (dot3_nn K O.toCone _)) l
  have hQ := alg_q K O ν h (cM K M) (Zs K l kz a (t + 1)) (As K l kz a (t + 1)) hν hh hZ (A_le_Z K O l kz M hM (a (t + 1))) hstepM
  refine ⟨t, ht, alg_cancel1 K O κ ν _ (Zs K l kz a (t + 1)) _ _ hκ hν nm m0 ?_ hQ htime⟩
  refine le_trans' K O.toCone _ _ _ ⟨K.zero, O.nn_zero, ?_⟩ hz
  show K.add (K.mul (K.mul (castN K (n + 1)) (K.mul h ν)) (Zs K l kz a (t + 1))) K.zero
    = K.mul (castN K (n + 1)) (K.mul (K.mul h ν) (Zs K l kz a (t + 1)))
  lib_ring

include mul_inv h10 in
theorem h2_eventual (l : List I) (kz : I → Mode) (hd : List.Pairwise (fun a b => kz a ≠ kz b) l)
    (h1 : ∀ p, 1 ≤ nrm (kz p)) (M : Nat) (hM : ∀ p, p ∈ l → nrm (kz p) ≤ M + 1)
    (ι h ν Es : R) (hh : O.nn h) (hν : O.nn ν) (hh0 : h ≠ K.zero) (hν0 : ν ≠ K.zero) (hι0 : ι ≠ K.zero)
    (hEs : O.nn Es)
    (hstepM : Le K O.toCone (K.mul (K.mul h ν) (cM K M)) K.one)
    (a N : Nat → I → V3 R)
    (hs : ∀ t r, Step K h ν (wt K kz r) (a (t + 1) r) (a t r) (N t r))
    (hneut : ∀ t, lsum K (fun r => dot3 K (N t r) (a t r)) l = K.zero)
    (hP : ∀ t, Le K O.toCone (lsum K (fun r => dot3 K (N t r) (N t r)) l)
      (lsum K (fun r => dot3 K (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (a t) (a t) r)
        (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (a t) (a t) r)) l))
    (hPw : ∀ t, Le K O.toCone (lsum K (fun r => K.mul (wt K kz r) (dot3 K (N t r) (N t r))) l)
      (lsum K (fun r => K.mul (wt K kz r) (dot3 K (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (a t) (a t) r)
        (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (a t) (a t) r))) l))
    (hcfl : Le K O.toCone (beta K M ι h ν Es) (K.mul h ν))
    (h0 : Le K O.toCone (K.add (lsum K (fun r => dot3 K (a 0 r) (a 0 r)) l)
      (K.mul (beta K M ι h ν Es) (lsum K (fun r => K.mul (wt K kz r) (dot3 K (a 0 r) (a 0 r))) l))) Es) (n n2 : Nat)
    (htime1 : Le K O.toCone
      (K.mul (K.add K.one K.one) (K.mul (K.add (K.mul (castN K 135) (K.mul ι ι)) (K.mul (castN K 135) (K.mul ι ι)))
        (K.add (lsum K (fun r => dot3 K (a 0 r) (a 0 r)) l)
          (K.mul (K.mul h ν) (lsum K (fun r => K.mul (wt K kz r) (dot3 K (a 0 r) (a 0 r))) l)))))
      (K.mul (K.mul ν ν) (K.mul (castN K (n + 1)) (K.mul h ν))))
    (htime2 : Le K O.toCone (K.mul (K.add K.one K.one) (K.mul (EnstH2.cH K) (K.mul ι ι)))
      (K.mul (K.mul (castN K 135) (K.mul ι ι)) (K.mul (castN K (n2 + 1)) (K.mul h ν)))) :
    ∃ t2, Le K O.toCone (K.mul (K.mul (EnstH2.cH K) (K.mul ι ι))
        (Q K ν h (fun t => lsum K (fun p => K.mul (K.mul (wt K kz p) (wt K kz p)) (dot3 K (a t p) (a t p))) l)
          (fun t => lsum K (fun p => K.mul (K.mul (K.mul (wt K kz p) (wt K kz p)) (wt K kz p)) (dot3 K (a t p) (a t p))) l) t2))
        (K.mul ν (K.mul ν ν)) ∧
      ∀ s, Le K O.toCone
        (Q K ν h (fun t => lsum K (fun p => K.mul (K.mul (wt K kz p) (wt K kz p)) (dot3 K (a t p) (a t p))) l)
          (fun t => lsum K (fun p => K.mul (K.mul (K.mul (wt K kz p) (wt K kz p)) (wt K kz p)) (dot3 K (a t p) (a t p))) l) (t2 + s))
        (Q K ν h (fun t => lsum K (fun p => K.mul (K.mul (wt K kz p) (wt K kz p)) (dot3 K (a t p) (a t p))) l)
          (fun t => lsum K (fun p => K.mul (K.mul (K.mul (wt K kz p) (wt K kz p)) (wt K kz p)) (dot3 K (a t p) (a t p))) l) t2) := by
  have hw : ∀ r, O.nn (wt K kz r) := fun r => dot3_nn K O.toCone _
  have hZ : ∀ t, O.nn (Zs K l kz a t) := fun t => lsum_nn K O.toCone _ (fun r => O.nn_mul _ _ (hw r) (dot3_nn K O.toCone _)) l
  have hA : ∀ t, O.nn (As K l kz a t) := fun t => lsum_nn K O.toCone _ (fun r => O.nn_mul _ _ (O.nn_sq _) (dot3_nn K O.toCone _)) l
  have hc : O.nn (K.mul (castN K 135) (K.mul ι ι)) := O.nn_mul _ _ (RatModel.nn_cast K O.toCone 135) (O.nn_sq ι)
  have c0 : K.mul (castN K 135) (K.mul ι ι) ≠ K.zero := by
    intro e
    cases O.noZD _ _ e with
    | inl e1 => exact cast_succ_ne K O h10 134 e1
    | inr e2 =>
      cases O.noZD _ _ e2 with
      | inl e3 => exact hι0 e3
      | inr e4 => exact hι0 e4
  -- stage 1: a time t1 with 2c·Q(t1) ≤ ν³
  have ⟨t, _, hq1⟩ := good_time K O inv mul_inv h10 l kz hd h1 M hM ι h ν Es
    (K.add (K.mul (castN K 135) (K.mul ι ι)) (K.mul (castN K 135) (K.mul ι ι)))
    hh hν hh0 hν0 hEs (O.nn_add _ _ hc hc) hstepM a N hs hneut hP hcfl h0 n htime1
  -- stage 2: dissipated H² mass after t1, then a time with small A
  have dis := EnstDiss.enst_diss K O ν h (K.mul (castN K 135) (K.mul ι ι)) (fun j => Zs K l kz a (t + 1 + j))
    (fun j => As K l kz a (t + 1 + j)) hν hν0 hh hc (fun j => hZ _) (fun j => hA _)
    (fun j => by
      have st := proj_step K O inv mul_inv h10 l kz hd h1 M hM ι h ν hh hν a N hP hs (t + 1 + j)
      exact st)
    hq1 (n2 + 1)
  have nQ : ∀ j, O.nn (Q K ν h (fun j => Zs K l kz a (t + 1 + j)) (fun j => As K l kz a (t + 1 + j)) j) := fun j =>
    O.nn_add _ _ (O.nn_mul _ _ hν (hZ _)) (O.nn_mul _ _ (O.nn_mul _ _ hh (O.nn_sq ν)) (hA _))
  have hsum : Le K O.toCone (sumN K (fun j => K.mul (K.mul h (K.mul ν ν)) (As K l kz a (t + 1 + j))) (n2 + 1))
      (K.mul (K.add K.one K.one) (Q K ν h (fun j => Zs K l kz a (t + 1 + j)) (fun j => As K l kz a (t + 1 + j)) 0)) := by
    rw [NSChain.sumN_mul_left K _ _ (n2 + 1)]
    refine le_trans' K O.toCone _ _ _ ⟨K.mul (K.add K.one K.one) (Q K ν h (fun j => Zs K l kz a (t + 1 + j)) (fun j => As K l kz a (t + 1 + j)) (n2 + 1)),
      O.nn_mul _ _ (O.nn_add _ _ (RatModel.nn_one' K O.toCone) (RatModel.nn_one' K O.toCone)) (nQ _), by lib_ring⟩ dis
  have ⟨j, _, hj⟩ := pigeon K O _ _ n2 hsum
  -- stage 3: the H² smallness at t2 = t + 1 + j
  have nm2 : O.nn (K.mul (castN K (n2 + 1)) (K.mul h ν)) := O.nn_mul _ _ (RatModel.nn_cast K O.toCone _) (O.nn_mul _ _ hh hν)
  have m20 : K.mul (castN K (n2 + 1)) (K.mul h ν) ≠ K.zero := by
    intro e
    cases O.noZD _ _ e with
    | inl e1 => exact cast_succ_ne K O h10 n2 e1
    | inr e2 =>
      cases O.noZD _ _ e2 with
      | inl e3 => exact hh0 e3
      | inr e4 => exact hν0 e4
  have cm0 : K.mul (K.mul (castN K 135) (K.mul ι ι)) (K.mul (castN K (n2 + 1)) (K.mul h ν)) ≠ K.zero := by
    intro e
    cases O.noZD _ _ e with
    | inl e1 => exact c0 e1
    | inr e2 => exact m20 e2
  have hAt := lsum_nn K O.toCone _ (fun r => O.nn_mul _ _ (O.nn_sq (wt K kz r)) (dot3_nn K O.toCone (a (t + 1 + j) r))) l
  have hQ2 := alg_q K O ν h (cM K M) (As K l kz a (t + 1 + j))
    (lsum K (fun p => K.mul (K.mul (K.mul (wt K kz p) (wt K kz p)) (wt K kz p)) (dot3 K (a (t + 1 + j) p) (a (t + 1 + j) p))) l)
    hν hh hAt (B_le_A K O l kz M hM (a (t + 1 + j))) hstepM
  have hdd : O.nn (K.mul (EnstH2.cH K) (K.mul ι ι)) := by
    have two : O.nn (K.add K.one K.one) := O.nn_add _ _ (RatModel.nn_one' K O.toCone) (RatModel.nn_one' K O.toCone)
    have three : O.nn (K.add (K.add K.one K.one) K.one) := O.nn_add _ _ two (RatModel.nn_one' K O.toCone)
    have c135 := RatModel.nn_cast K O.toCone 135
    have : O.nn (EnstH2.cH K) :=
      O.nn_add _ _ (O.nn_mul _ _ two (O.nn_mul _ _ three c135)) (O.nn_mul _ _ two c135)
    exact O.nn_mul _ _ this (O.nn_sq ι)
  have hj' : Le K O.toCone (K.mul (castN K (n2 + 1)) (K.mul (K.mul h (K.mul ν ν)) (As K l kz a (t + 1 + j))))
      (K.mul (K.add K.one K.one) (Q K ν h (fun j => Zs K l kz a (t + 1 + j)) (fun j => As K l kz a (t + 1 + j)) 0)) := hj
  have hq1' : Le K O.toCone (K.mul (K.add (K.mul (castN K 135) (K.mul ι ι)) (K.mul (castN K 135) (K.mul ι ι)))
      (Q K ν h (fun j => Zs K l kz a (t + 1 + j)) (fun j => As K l kz a (t + 1 + j)) 0)) (K.mul ν (K.mul ν ν)) := hq1
  generalize Q K ν h (fun j => Zs K l kz a (t + 1 + j)) (fun j => As K l kz a (t + 1 + j)) 0 = Q1 at hj' hq1'
  have small2 := alg_cancel2 K O (K.mul (castN K 135) (K.mul ι ι)) (K.mul (EnstH2.cH K) (K.mul ι ι))
    (K.mul (castN K (n2 + 1)) (K.mul h ν)) ν (As K l kz a (t + 1 + j)) Q1 _
    hc hdd nm2 hν cm0
    (le_trans' K O.toCone _ _ _ ⟨K.zero, O.nn_zero, by lib_ring⟩ (le_trans' K O.toCone _ _ _ hj' ⟨K.zero, O.nn_zero, by lib_ring⟩))
    hq1' htime2 hQ2
  refine ⟨t + 1 + j, small2, fun s => ?_⟩
  exact EnstH2.h2_global K O inv mul_inv h10 l kz hd h1 M hM ι h ν hh hν hν0
    (fun s => a (t + 1 + j + s)) (fun s => N (t + 1 + j + s)) (fun s => hPw (t + 1 + j + s))
    (fun s r => hs (t + 1 + j + s) r) small2 s

end Gen

end EventH2

#print axioms EventH2.alg_q
#print axioms EventH2.alg_cancel1
#print axioms EventH2.alg_cancel2
#print axioms EventH2.B_le_A
#print axioms EventH2.proj_step
#print axioms EventH2.good_time
#print axioms EventH2.h2_eventual

ns/EventH2K.lean

lean_all/ns/EventH2K.lean

/-
  ns/EventH2K.lean — T37 cycle 1327 (goal G793a): EventH2.h2_eventual with a general smallness constant κ₂ ≥ 1080ι²:
  if 2κ₂ ≤ c·T_2 (and the T_1 condition of h2_eventual), some time t_2 has κ₂·(νA + hν²B)(t_2) ≤ ν³ and νA + hν²B
  never increases afterwards. With κ₂ = 2·1080ι² this is the input of EnstDiss for the H³ stage (G793).

  Lean 4 core plus ns/EventH2.
-/
import ns.EventH2

namespace EventH2K
open DetChain NSChain DyadicSmallGlobal LibRing DyadicLarge FourierGalerkin GalerkinForce ForceBound ForceSob
  ModeCount CubeCount ForceUniform NormCompare EnstStep EnstGalerkin EnstGlobal CFLStep CFLGlobal EventSmall EventReg
  EventH2

section Gen
variable {R : Type} (K : CRing R) (O : OrdR K) {I : Type}
variable (inv : R → R) (mul_inv : ∀ y, y ≠ K.zero → K.mul y (inv y) = K.one) (h10 : K.one ≠ K.zero)

include mul_inv h10 in
theorem h2_eventual_k (l : List I) (kz : I → Mode) (hd : List.Pairwise (fun a b => kz a ≠ kz b) l)
    (h1 : ∀ p, 1 ≤ nrm (kz p)) (M : Nat) (hM : ∀ p, p ∈ l → nrm (kz p) ≤ M + 1)
    (ι h ν Es : R) (hh : O.nn h) (hν : O.nn ν) (hh0 : h ≠ K.zero) (hν0 : ν ≠ K.zero) (hι0 : ι ≠ K.zero)
    (hEs : O.nn Es) (κ2 : R) (hκ2 : O.nn κ2) (hκle : Le K O.toCone (K.mul (EnstH2.cH K) (K.mul ι ι)) κ2)
    (hstepM : Le K O.toCone (K.mul (K.mul h ν) (cM K M)) K.one)
    (a N : Nat → I → V3 R)
    (hs : ∀ t r, Step K h ν (wt K kz r) (a (t + 1) r) (a t r) (N t r))
    (hneut : ∀ t, lsum K (fun r => dot3 K (N t r) (a t r)) l = K.zero)
    (hP : ∀ t, Le K O.toCone (lsum K (fun r => dot3 K (N t r) (N t r)) l)
      (lsum K (fun r => dot3 K (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (a t) (a t) r)
        (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (a t) (a t) r)) l))
    (hPw : ∀ t, Le K O.toCone (lsum K (fun r => K.mul (wt K kz r) (dot3 K (N t r) (N t r))) l)
      (lsum K (fun r => K.mul (wt K kz r) (dot3 K (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (a t) (a t) r)
        (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (a t) (a t) r))) l))
    (hcfl : Le K O.toCone (beta K M ι h ν Es) (K.mul h ν))
    (h0 : Le K O.toCone (K.add (lsum K (fun r => dot3 K (a 0 r) (a 0 r)) l)
      (K.mul (beta K M ι h ν Es) (lsum K (fun r => K.mul (wt K kz r) (dot3 K (a 0 r) (a 0 r))) l))) Es) (n n2 : Nat)
    (htime1 : Le K O.toCone
      (K.mul (K.add K.one K.one) (K.mul (K.add (K.mul (castN K 135) (K.mul ι ι)) (K.mul (castN K 135) (K.mul ι ι)))
        (K.add (lsum K (fun r => dot3 K (a 0 r) (a 0 r)) l)
          (K.mul (K.mul h ν) (lsum K (fun r => K.mul (wt K kz r) (dot3 K (a 0 r) (a 0 r))) l)))))
      (K.mul (K.mul ν ν) (K.mul (castN K (n + 1)) (K.mul h ν))))
    (htime2 : Le K O.toCone (K.mul (K.add K.one K.one) κ2)
      (K.mul (K.mul (castN K 135) (K.mul ι ι)) (K.mul (castN K (n2 + 1)) (K.mul h ν)))) :
    ∃ t2, Le K O.toCone (K.mul κ2
        (Q K ν h (fun t => lsum K (fun p => K.mul (K.mul (wt K kz p) (wt K kz p)) (dot3 K (a t p) (a t p))) l)
          (fun t => lsum K (fun p => K.mul (K.mul (K.mul (wt K kz p) (wt K kz p)) (wt K kz p)) (dot3 K (a t p) (a t p))) l) t2))
        (K.mul ν (K.mul ν ν)) ∧
      ∀ s, Le K O.toCone
        (Q K ν h (fun t => lsum K (fun p => K.mul (K.mul (wt K kz p) (wt K kz p)) (dot3 K (a t p) (a t p))) l)
          (fun t => lsum K (fun p => K.mul (K.mul (K.mul (wt K kz p) (wt K kz p)) (wt K kz p)) (dot3 K (a t p) (a t p))) l) (t2 + s))
        (Q K ν h (fun t => lsum K (fun p => K.mul (K.mul (wt K kz p) (wt K kz p)) (dot3 K (a t p) (a t p))) l)
          (fun t => lsum K (fun p => K.mul (K.mul (K.mul (wt K kz p) (wt K kz p)) (wt K kz p)) (dot3 K (a t p) (a t p))) l) t2) := by
  have hw : ∀ r, O.nn (wt K kz r) := fun r => dot3_nn K O.toCone _
  have hZ : ∀ t, O.nn (Zs K l kz a t) := fun t => lsum_nn K O.toCone _ (fun r => O.nn_mul _ _ (hw r) (dot3_nn K O.toCone _)) l
  have hA : ∀ t, O.nn (As K l kz a t) := fun t => lsum_nn K O.toCone _ (fun r => O.nn_mul _ _ (O.nn_sq _) (dot3_nn K O.toCone _)) l
  have hc : O.nn (K.mul (castN K 135) (K.mul ι ι)) := O.nn_mul _ _ (RatModel.nn_cast K O.toCone 135) (O.nn_sq ι)
  have c0 : K.mul (castN K 135) (K.mul ι ι) ≠ K.zero := by
    intro e
    cases O.noZD _ _ e with
    | inl e1 => exact cast_succ_ne K O h10 134 e1
    | inr e2 =>
      cases O.noZD _ _ e2 with
      | inl e3 => exact hι0 e3
      | inr e4 => exact hι0 e4
  -- stage 1: a time t1 with 2c·Q(t1) ≤ ν³
  have ⟨t, _, hq1⟩ := good_time K O inv mul_inv h10 l kz hd h1 M hM ι h ν Es
    (K.add (K.mul (castN K 135) (K.mul ι ι)) (K.mul (castN K 135) (K.mul ι ι)))
    hh hν hh0 hν0 hEs (O.nn_add _ _ hc hc) hstepM a N hs hneut hP hcfl h0 n htime1
  -- stage 2: dissipated H² mass after t1, then a time with small A
  have dis := EnstDiss.enst_diss K O ν h (K.mul (castN K 135) (K.mul ι ι)) (fun j => Zs K l kz a (t + 1 + j))
    (fun j => As K l kz a (t + 1 + j)) hν hν0 hh hc (fun j => hZ _) (fun j => hA _)
    (fun j => by
      have st := proj_step K O inv mul_inv h10 l kz hd h1 M hM ι h ν hh hν a N hP hs (t + 1 + j)
      exact st)
    hq1 (n2 + 1)
  have nQ : ∀ j, O.nn (Q K ν h (fun j => Zs K l kz a (t + 1 + j)) (fun j => As K l kz a (t + 1 + j)) j) := fun j =>
    O.nn_add _ _ (O.nn_mul _ _ hν (hZ _)) (O.nn_mul _ _ (O.nn_mul _ _ hh (O.nn_sq ν)) (hA _))
  have hsum : Le K O.toCone (sumN K (fun j => K.mul (K.mul h (K.mul ν ν)) (As K l kz a (t + 1 + j))) (n2 + 1))
      (K.mul (K.add K.one K.one) (Q K ν h (fun j => Zs K l kz a (t + 1 + j)) (fun j => As K l kz a (t + 1 + j)) 0)) := by
    rw [NSChain.sumN_mul_left K _ _ (n2 + 1)]
    refine le_trans' K O.toCone _ _ _ ⟨K.mul (K.add K.one K.one) (Q K ν h (fun j => Zs K l kz a (t + 1 + j)) (fun j => As K l kz a (t + 1 + j)) (n2 + 1)),
      O.nn_mul _ _ (O.nn_add _ _ (RatModel.nn_one' K O.toCone) (RatModel.nn_one' K O.toCone)) (nQ _), by lib_ring⟩ dis
  have ⟨j, _, hj⟩ := pigeon K O _ _ n2 hsum
  -- stage 3: the H² smallness at t2 = t + 1 + j
  have nm2 : O.nn (K.mul (castN K (n2 + 1)) (K.mul h ν)) := O.nn_mul _ _ (RatModel.nn_cast K O.toCone _) (O.nn_mul _ _ hh hν)
  have m20 : K.mul (castN K (n2 + 1)) (K.mul h ν) ≠ K.zero := by
    intro e
    cases O.noZD _ _ e with
    | inl e1 => exact cast_succ_ne K O h10 n2 e1
    | inr e2 =>
      cases O.noZD _ _ e2 with
      | inl e3 => exact hh0 e3
      | inr e4 => exact hν0 e4
  have cm0 : K.mul (K.mul (castN K 135) (K.mul ι ι)) (K.mul (castN K (n2 + 1)) (K.mul h ν)) ≠ K.zero := by
    intro e
    cases O.noZD _ _ e with
    | inl e1 => exact c0 e1
    | inr e2 => exact m20 e2
  have hAt := lsum_nn K O.toCone _ (fun r => O.nn_mul _ _ (O.nn_sq (wt K kz r)) (dot3_nn K O.toCone (a (t + 1 + j) r))) l
  have hQ2 := alg_q K O ν h (cM K M) (As K l kz a (t + 1 + j))
    (lsum K (fun p => K.mul (K.mul (K.mul (wt K kz p) (wt K kz p)) (wt K kz p)) (dot3 K (a (t + 1 + j) p) (a (t + 1 + j) p))) l)
    hν hh hAt (B_le_A K O l kz M hM (a (t + 1 + j))) hstepM
  have hdd : O.nn (K.mul (EnstH2.cH K) (K.mul ι ι)) := by
    have two : O.nn (K.add K.one K.one) := O.nn_add _ _ (RatModel.nn_one' K O.toCone) (RatModel.nn_one' K O.toCone)
    have three : O.nn (K.add (K.add K.one K.one) K.one) := O.nn_add _ _ two (RatModel.nn_one' K O.toCone)
    have c135 := RatModel.nn_cast K O.toCone 135
    have : O.nn (EnstH2.cH K) :=
      O.nn_add _ _ (O.nn_mul _ _ two (O.nn_mul _ _ three c135)) (O.nn_mul _ _ two c135)
    exact O.nn_mul _ _ this (O.nn_sq ι)
  have hj' : Le K O.toCone (K.mul (castN K (n2 + 1)) (K.mul (K.mul h (K.mul ν ν)) (As K l kz a (t + 1 + j))))
      (K.mul (K.add K.one K.one) (Q K ν h (fun j => Zs K l kz a (t + 1 + j)) (fun j => As K l kz a (t + 1 + j)) 0)) := hj
  have hq1' : Le K O.toCone (K.mul (K.add (K.mul (castN K 135) (K.mul ι ι)) (K.mul (castN K 135) (K.mul ι ι)))
      (Q K ν h (fun j => Zs K l kz a (t + 1 + j)) (fun j => As K l kz a (t + 1 + j)) 0)) (K.mul ν (K.mul ν ν)) := hq1
  generalize Q K ν h (fun j => Zs K l kz a (t + 1 + j)) (fun j => As K l kz a (t + 1 + j)) 0 = Q1 at hj' hq1'
  have small2 := alg_cancel2 K O (K.mul (castN K 135) (K.mul ι ι)) κ2
    (K.mul (castN K (n2 + 1)) (K.mul h ν)) ν (As K l kz a (t + 1 + j)) Q1 _
    hc hκ2 nm2 hν cm0
    (le_trans' K O.toCone _ _ _ ⟨K.zero, O.nn_zero, by lib_ring⟩ (le_trans' K O.toCone _ _ _ hj' ⟨K.zero, O.nn_zero, by lib_ring⟩))
    hq1' htime2 hQ2
  have nQ2 : O.nn (Q K ν h (fun t => lsum K (fun p => K.mul (K.mul (wt K kz p) (wt K kz p)) (dot3 K (a t p) (a t p))) l)
      (fun t => lsum K (fun p => K.mul (K.mul (K.mul (wt K kz p) (wt K kz p)) (wt K kz p)) (dot3 K (a t p) (a t p))) l) (t + 1 + j)) :=
    O.nn_add _ _ (O.nn_mul _ _ hν hAt) (O.nn_mul _ _ (O.nn_mul _ _ hh (O.nn_sq ν))
      (lsum_nn K O.toCone _ (fun p => O.nn_mul _ _ (O.nn_mul _ _ (O.nn_sq _) (hw p)) (dot3_nn K O.toCone _)) l))
  have smallH := le_trans' K O.toCone _ _ _ (le_mul_right' K O.toCone hκle nQ2) small2
  refine ⟨t + 1 + j, small2, fun s => ?_⟩
  exact EnstH2.h2_global K O inv mul_inv h10 l kz hd h1 M hM ι h ν hh hν hν0
    (fun s => a (t + 1 + j + s)) (fun s => N (t + 1 + j + s)) (fun s => hPw (t + 1 + j + s))
    (fun s r => hs (t + 1 + j + s) r) smallH s

end Gen

end EventH2K

#print axioms EventH2K.h2_eventual_k

ns/EventH3.lean

lean_all/ns/EventH3.lean

/-
  ns/EventH3.lean — T37 cycle 1329 (goal G793b — done): discrete eventual H³ regularity for arbitrary data, over an
  ordered field. Hypotheses of EventH2.h2_eventual (with κ₂ = 2·1080ι²) plus Σw²|N|² ≤ Σw²|Nl|² (Leray projection).
  With d₂ = 1080ι², c₃ = 8(9·135 + 135), B̃ = Σw(w·w)|a|², B₄ = Σ(w·w)(w·w)|a|², Q₃ = νB̃ + hν²B₄: if moreover
      2·c₃ι² ≤ d₂·T_3,    T_3 = (n_3 + 1)hν          (a pure-number waiting time, T_3 ≥ 2c₃/1080)
  then some time t_3 has
    event_h3   c₃ι²·Q₃(t_3) ≤ ν³   and   Q₃(t_3 + s) ≤ Q₃(t_3) for every s,
  all constants independent of the truncation. Route: EventH2K (κ₂ = 2d₂) → EnstDiss on (A, B) with H2Step.h2_step
  → pigeonhole → B₄ ≤ c_M B̃ and alg_cancel2 → EnstH3.h3_global on the shifted run.

  Lean 4 core plus ns/EventH2K, ns/H2Step, ns/EnstH3, ns/EnstDiss.
-/
import ns.EventH2K
import ns.H2Step
import ns.EnstH3

namespace EventH3
open DetChain NSChain DyadicSmallGlobal LibRing DyadicLarge FourierGalerkin GalerkinForce ForceBound ForceSob
  ModeCount CubeCount ForceUniform NormCompare EnstStep EnstGalerkin EnstGlobal CFLStep CFLGlobal EventSmall EventReg
  EventH2

section Gen
variable {R : Type} (K : CRing R) (O : OrdR K) {I : Type}
variable (inv : R → R) (mul_inv : ∀ y, y ≠ K.zero → K.mul y (inv y) = K.one) (h10 : K.one ≠ K.zero)

include O h10 in
theorem cH_ne : EnstH2.cH K ≠ K.zero := by
  have e : EnstH2.cH K = K.mul (K.add K.one (K.add (K.add (K.add K.one K.one) K.one) (K.add (K.add K.one K.one) (K.add K.one K.one))))
      (castN K 135) := by
    unfold EnstH2.cH; lib_ring
  have two : O.nn (K.add K.one K.one) := O.nn_add _ _ (RatModel.nn_one' K O.toCone) (RatModel.nn_one' K O.toCone)
  intro h0
  rw [e] at h0
  cases O.noZD _ _ h0 with
  | inl e1 =>
    exact h10 (nn_sum_zero K O _ _ (RatModel.nn_one' K O.toCone)
      (O.nn_add _ _ (O.nn_add _ _ two (RatModel.nn_one' K O.toCone)) (O.nn_add _ _ two two)) e1)
  | inr e2 => exact cast_succ_ne K O h10 134 e2

theorem B4_le (l : List I) (kz : I → Mode) (M : Nat) (hM : ∀ p, p ∈ l → nrm (kz p) ≤ M + 1) (a : I → V3 R) :
    Le K O.toCone (lsum K (fun r => K.mul (K.mul (K.mul (wt K kz r) (wt K kz r)) (K.mul (wt K kz r) (wt K kz r))) (dot3 K (a r) (a r))) l)
      (K.mul (cM K M) (lsum K (fun r => K.mul (K.mul (wt K kz r) (K.mul (wt K kz r) (wt K kz r))) (dot3 K (a r) (a r))) l)) := by
  rw [← lsum_mul_left]
  apply lsum_le_mem K O
  intro r hr
  have hwr : O.nn (wt K kz r) := dot3_nn K O.toCone _
  have m := ForceSob.le_mul_right' K O.toCone (wt_le K O kz M r (hM r hr))
    (O.nn_mul _ _ (O.nn_mul _ _ hwr (O.nn_sq (wt K kz r))) (dot3_nn K O.toCone (a r)))
  exact le_trans' K O.toCone _ _ _ ⟨K.zero, O.nn_zero, by lib_ring⟩ (le_trans' K O.toCone _ _ _ m ⟨K.zero, O.nn_zero, by lib_ring⟩)

include mul_inv h10 in
theorem event_h3 (l : List I) (kz : I → Mode) (hd : List.Pairwise (fun a b => kz a ≠ kz b) l)
    (h1 : ∀ p, 1 ≤ nrm (kz p)) (M : Nat) (hM : ∀ p, p ∈ l → nrm (kz p) ≤ M + 1)
    (ι h ν Es : R) (hh : O.nn h) (hν : O.nn ν) (hh0 : h ≠ K.zero) (hν0 : ν ≠ K.zero) (hι0 : ι ≠ K.zero)
    (hEs : O.nn Es)
    (hstepM : Le K O.toCone (K.mul (K.mul h ν) (cM K M)) K.one)
    (a N : Nat → I → V3 R)
    (hs : ∀ t r, Step K h ν (wt K kz r) (a (t + 1) r) (a t r) (N t r))
    (hneut : ∀ t, lsum K (fun r => dot3 K (N t r) (a t r)) l = K.zero)
    (hP : ∀ t, Le K O.toCone (lsum K (fun r => dot3 K (N t r) (N t r)) l)
      (lsum K (fun r => dot3 K (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (a t) (a t) r)
        (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (a t) (a t) r)) l))
    (hPw : ∀ t, Le K O.toCone (lsum K (fun r => K.mul (wt K kz r) (dot3 K (N t r) (N t r))) l)
      (lsum K (fun r => K.mul (wt K kz r) (dot3 K (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (a t) (a t) r)
        (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (a t) (a t) r))) l))
    (hPw2 : ∀ t, Le K O.toCone (lsum K (fun r => K.mul (K.mul (wt K kz r) (wt K kz r)) (dot3 K (N t r) (N t r))) l)
      (lsum K (fun r => K.mul (K.mul (wt K kz r) (wt K kz r)) (dot3 K (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (a t) (a t) r)
        (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (a t) (a t) r))) l))
    (hcfl : Le K O.toCone (beta K M ι h ν Es) (K.mul h ν))
    (h0 : Le K O.toCone (K.add (lsum K (fun r => dot3 K (a 0 r) (a 0 r)) l)
      (K.mul (beta K M ι h ν Es) (lsum K (fun r => K.mul (wt K kz r) (dot3 K (a 0 r) (a 0 r))) l))) Es) (n n2 n3 : Nat)
    (htime1 : Le K O.toCone
      (K.mul (K.add K.one K.one) (K.mul (K.add (K.mul (castN K 135) (K.mul ι ι)) (K.mul (castN K 135) (K.mul ι ι)))
        (K.add (lsum K (fun r => dot3 K (a 0 r) (a 0 r)) l)
          (K.mul (K.mul h ν) (lsum K (fun r => K.mul (wt K kz r) (dot3 K (a 0 r) (a 0 r))) l)))))
      (K.mul (K.mul ν ν) (K.mul (castN K (n + 1)) (K.mul h ν))))
    (htime2 : Le K O.toCone (K.mul (K.add K.one K.one) (K.add (K.mul (EnstH2.cH K) (K.mul ι ι)) (K.mul (EnstH2.cH K) (K.mul ι ι))))
      (K.mul (K.mul (castN K 135) (K.mul ι ι)) (K.mul (castN K (n2 + 1)) (K.mul h ν))))
    (htime3 : Le K O.toCone (K.mul (K.add K.one K.one) (K.mul (EnstH3.c3 K) (K.mul ι ι)))
      (K.mul (K.mul (EnstH2.cH K) (K.mul ι ι)) (K.mul (castN K (n3 + 1)) (K.mul h ν)))) :
    ∃ t3, Le K O.toCone (K.mul (K.mul (EnstH3.c3 K) (K.mul ι ι))
        (Q K ν h (fun t => lsum K (fun p => K.mul (K.mul (wt K kz p) (K.mul (wt K kz p) (wt K kz p))) (dot3 K (a t p) (a t p))) l)
          (fun t => lsum K (fun p => K.mul (K.mul (K.mul (wt K kz p) (wt K kz p)) (K.mul (wt K kz p) (wt K kz p))) (dot3 K (a t p) (a t p))) l) t3))
        (K.mul ν (K.mul ν ν)) ∧
      ∀ s, Le K O.toCone
        (Q K ν h (fun t => lsum K (fun p => K.mul (K.mul (wt K kz p) (K.mul (wt K kz p) (wt K kz p))) (dot3 K (a t p) (a t p))) l)
          (fun t => lsum K (fun p => K.mul (K.mul (K.mul (wt K kz p) (wt K kz p)) (K.mul (wt K kz p) (wt K kz p))) (dot3 K (a t p) (a t p))) l) (t3 + s))
        (Q K ν h (fun t => lsum K (fun p => K.mul (K.mul (wt K kz p) (K.mul (wt K kz p) (wt K kz p))) (dot3 K (a t p) (a t p))) l)
          (fun t => lsum K (fun p => K.mul (K.mul (K.mul (wt K kz p) (wt K kz p)) (K.mul (wt K kz p) (wt K kz p))) (dot3 K (a t p) (a t p))) l) t3) := by
  have hw : ∀ r, O.nn (wt K kz r) := fun r => dot3_nn K O.toCone _
  have two : O.nn (K.add K.one K.one) := O.nn_add _ _ (RatModel.nn_one' K O.toCone) (RatModel.nn_one' K O.toCone)
  have three : O.nn (K.add (K.add K.one K.one) K.one) := O.nn_add _ _ two (RatModel.nn_one' K O.toCone)
  have n135 : O.nn (castN K 135) := RatModel.nn_cast K O.toCone 135
  have ncH : O.nn (EnstH2.cH K) := O.nn_add _ _ (O.nn_mul _ _ two (O.nn_mul _ _ three n135)) (O.nn_mul _ _ two n135)
  have hd2 : O.nn (K.mul (EnstH2.cH K) (K.mul ι ι)) := O.nn_mul _ _ ncH (O.nn_sq ι)
  have hc3 : O.nn (K.mul (EnstH3.c3 K) (K.mul ι ι)) :=
    O.nn_mul _ _ (O.nn_mul _ _ (EnstH3.nn8 K O) (O.nn_add _ _ (O.nn_mul _ _ (EnstH3.nn9 K O) n135) n135)) (O.nn_sq ι)
  have hA : ∀ t, O.nn (lsum K (fun p => K.mul (K.mul (wt K kz p) (wt K kz p)) (dot3 K (a t p) (a t p))) l) := fun t =>
    lsum_nn K O.toCone _ (fun p => O.nn_mul _ _ (O.nn_sq _) (dot3_nn K O.toCone _)) l
  have hB : ∀ t, O.nn (lsum K (fun p => K.mul (K.mul (K.mul (wt K kz p) (wt K kz p)) (wt K kz p)) (dot3 K (a t p) (a t p))) l) := fun t =>
    lsum_nn K O.toCone _ (fun p => O.nn_mul _ _ (O.nn_mul _ _ (O.nn_sq _) (hw p)) (dot3_nn K O.toCone _)) l
  -- stage H²: a time t2 with 2d₂·Q₂(t2) ≤ ν³
  have ⟨t2, hk, _⟩ := EventH2K.h2_eventual_k K O inv mul_inv h10 l kz hd h1 M hM ι h ν Es hh hν hh0 hν0 hι0 hEs
    (K.add (K.mul (EnstH2.cH K) (K.mul ι ι)) (K.mul (EnstH2.cH K) (K.mul ι ι))) (O.nn_add _ _ hd2 hd2)
    ⟨K.mul (EnstH2.cH K) (K.mul ι ι), hd2, rfl⟩ hstepM a N hs hneut hP hPw hcfl h0 n n2 htime1 htime2
  -- dissipated H³ mass after t2
  have dis := EnstDiss.enst_diss K O ν h (K.mul (EnstH2.cH K) (K.mul ι ι))
    (fun j => lsum K (fun p => K.mul (K.mul (wt K kz p) (wt K kz p)) (dot3 K (a (t2 + j) p) (a (t2 + j) p))) l)
    (fun j => lsum K (fun p => K.mul (K.mul (K.mul (wt K kz p) (wt K kz p)) (wt K kz p)) (dot3 K (a (t2 + j) p) (a (t2 + j) p))) l)
    hν hν0 hh hd2 (fun j => hA _) (fun j => hB _)
    (fun j => H2Step.h2_step K O inv mul_inv h10 l kz hd h1 M hM ι h ν hh hν a N hPw hs (t2 + j)) hk (n3 + 1)
  have nQ : ∀ j, O.nn (Q K ν h (fun j => lsum K (fun p => K.mul (K.mul (wt K kz p) (wt K kz p)) (dot3 K (a (t2 + j) p) (a (t2 + j) p))) l)
      (fun j => lsum K (fun p => K.mul (K.mul (K.mul (wt K kz p) (wt K kz p)) (wt K kz p)) (dot3 K (a (t2 + j) p) (a (t2 + j) p))) l) j) :=
    fun j => O.nn_add _ _ (O.nn_mul _ _ hν (hA _)) (O.nn_mul _ _ (O.nn_mul _ _ hh (O.nn_sq ν)) (hB _))
  have hsum : Le K O.toCone (sumN K (fun j => K.mul (K.mul h (K.mul ν ν))
        (lsum K (fun p => K.mul (K.mul (K.mul (wt K kz p) (wt K kz p)) (wt K kz p)) (dot3 K (a (t2 + j) p) (a (t2 + j) p))) l)) (n3 + 1))
      (K.mul (K.add K.one K.one) (Q K ν h (fun j => lsum K (fun p => K.mul (K.mul (wt K kz p) (wt K kz p)) (dot3 K (a (t2 + j) p) (a (t2 + j) p))) l)
        (fun j => lsum K (fun p => K.mul (K.mul (K.mul (wt K kz p) (wt K kz p)) (wt K kz p)) (dot3 K (a (t2 + j) p) (a (t2 + j) p))) l) 0)) := by
    rw [NSChain.sumN_mul_left K _ _ (n3 + 1)]
    exact le_trans' K O.toCone _ _ _ ⟨K.mul (K.add K.one K.one) (Q K ν h _ _ (n3 + 1)), O.nn_mul _ _ two (nQ _), by lib_ring⟩ dis
  have ⟨j, _, hj⟩ := pigeon K O _ _ n3 hsum
  have hj0 : Le K O.toCone (K.mul (castN K (n3 + 1)) (K.mul (K.mul h (K.mul ν ν))
        (lsum K (fun p => K.mul (K.mul (K.mul (wt K kz p) (wt K kz p)) (wt K kz p)) (dot3 K (a (t2 + j) p) (a (t2 + j) p))) l)))
      (K.mul (K.add K.one K.one) (Q K ν h (fun j => lsum K (fun p => K.mul (K.mul (wt K kz p) (wt K kz p)) (dot3 K (a (t2 + j) p) (a (t2 + j) p))) l)
        (fun j => lsum K (fun p => K.mul (K.mul (K.mul (wt K kz p) (wt K kz p)) (wt K kz p)) (dot3 K (a (t2 + j) p) (a (t2 + j) p))) l) 0)) := hj
  rw [EnstH3.Bform K kz l (a (t2 + j))] at hj0
  have hk' : Le K O.toCone (K.mul (K.add (K.mul (EnstH2.cH K) (K.mul ι ι)) (K.mul (EnstH2.cH K) (K.mul ι ι)))
      (Q K ν h (fun j => lsum K (fun p => K.mul (K.mul (wt K kz p) (wt K kz p)) (dot3 K (a (t2 + j) p) (a (t2 + j) p))) l)
        (fun j => lsum K (fun p => K.mul (K.mul (K.mul (wt K kz p) (wt K kz p)) (wt K kz p)) (dot3 K (a (t2 + j) p) (a (t2 + j) p))) l) 0))
      (K.mul ν (K.mul ν ν)) := hk
  generalize Q K ν h (fun j => lsum K (fun p => K.mul (K.mul (wt K kz p) (wt K kz p)) (dot3 K (a (t2 + j) p) (a (t2 + j) p))) l)
    (fun j => lsum K (fun p => K.mul (K.mul (K.mul (wt K kz p) (wt K kz p)) (wt K kz p)) (dot3 K (a (t2 + j) p) (a (t2 + j) p))) l) 0 = Q1 at hj0 hk'
  -- stage H³ at t3 = t2 + j
  have nm3 : O.nn (K.mul (castN K (n3 + 1)) (K.mul h ν)) := O.nn_mul _ _ (RatModel.nn_cast K O.toCone _) (O.nn_mul _ _ hh hν)
  have m30 : K.mul (castN K (n3 + 1)) (K.mul h ν) ≠ K.zero := by
    intro e
    cases O.noZD _ _ e with
    | inl e1 => exact cast_succ_ne K O h10 n3 e1
    | inr e2 =>
      cases O.noZD _ _ e2 with
      | inl e3 => exact hh0 e3
      | inr e4 => exact hν0 e4
  have cm0 : K.mul (K.mul (EnstH2.cH K) (K.mul ι ι)) (K.mul (castN K (n3 + 1)) (K.mul h ν)) ≠ K.zero := by
    intro e
    cases O.noZD _ _ e with
    | inl e1 =>
      cases O.noZD _ _ e1 with
      | inl e2 => exact cH_ne K O h10 e2
      | inr e3 =>
        cases O.noZD _ _ e3 with
        | inl e4 => exact hι0 e4
        | inr e5 => exact hι0 e5
    | inr e2 => exact m30 e2
  have hBt := lsum_nn K O.toCone _ (fun r => O.nn_mul _ _ (O.nn_mul _ _ (hw r) (O.nn_sq (wt K kz r))) (dot3_nn K O.toCone (a (t2 + j) r))) l
  have hQ3 := alg_q K O ν h (cM K M)
    (lsum K (fun p => K.mul (K.mul (wt K kz p) (K.mul (wt K kz p) (wt K kz p))) (dot3 K (a (t2 + j) p) (a (t2 + j) p))) l)
    (lsum K (fun p => K.mul (K.mul (K.mul (wt K kz p) (wt K kz p)) (K.mul (wt K kz p) (wt K kz p))) (dot3 K (a (t2 + j) p) (a (t2 + j) p))) l)
    hν hh hBt (B4_le K O l kz M hM (a (t2 + j))) hstepM
  have small3 := alg_cancel2 K O (K.mul (EnstH2.cH K) (K.mul ι ι)) (K.mul (EnstH3.c3 K) (K.mul ι ι))
    (K.mul (castN K (n3 + 1)) (K.mul h ν)) ν
    (lsum K (fun p => K.mul (K.mul (wt K kz p) (K.mul (wt K kz p) (wt K kz p))) (dot3 K (a (t2 + j) p) (a (t2 + j) p))) l) Q1 _
    hd2 hc3 nm3 hν cm0
    (le_trans' K O.toCone _ _ _ ⟨K.zero, O.nn_zero, by lib_ring⟩ (le_trans' K O.toCone _ _ _ hj0 ⟨K.zero, O.nn_zero, by lib_ring⟩))
    hk' htime3 hQ3
  refine ⟨t2 + j, small3, fun s => ?_⟩
  exact EnstH3.h3_global K O inv mul_inv h10 l kz hd h1 M hM ι h ν hh hν hν0
    (fun s => a (t2 + j + s)) (fun s => N (t2 + j + s)) (fun s => hPw2 (t2 + j + s))
    (fun s r => hs (t2 + j + s) r) small3 s

end Gen

end EventH3

#print axioms EventH3.cH_ne
#print axioms EventH3.B4_le
#print axioms EventH3.event_h3

ns/EventH3K.lean

lean_all/ns/EventH3K.lean

/-
  ns/EventH3K.lean — T37 cycle 1331 (goal G794a): EventH3.event_h3 with a general smallness constant κ₃ ≥ c₃ι²:
  if 2κ₃ ≤ 1080ι²·T_3 (and the T_1, T_2 conditions of event_h3), some time t_3 has κ₃·(νB̃ + hν²B₄)(t_3) ≤ ν³ and
  νB̃ + hν²B₄ never increases afterwards. With κ₃ = 2c₃ι² this feeds EnstDiss at the H⁴ stage.

  Lean 4 core plus ns/EventH3.
-/
import ns.EventH3

namespace EventH3K
open DetChain NSChain DyadicSmallGlobal LibRing DyadicLarge FourierGalerkin GalerkinForce ForceBound ForceSob
  ModeCount CubeCount ForceUniform NormCompare EnstStep EnstGalerkin EnstGlobal CFLStep CFLGlobal EventSmall EventReg
  EventH2 EventH3

section Gen
variable {R : Type} (K : CRing R) (O : OrdR K) {I : Type}
variable (inv : R → R) (mul_inv : ∀ y, y ≠ K.zero → K.mul y (inv y) = K.one) (h10 : K.one ≠ K.zero)

include mul_inv h10 in
theorem event_h3_k (l : List I) (kz : I → Mode) (hd : List.Pairwise (fun a b => kz a ≠ kz b) l)
    (h1 : ∀ p, 1 ≤ nrm (kz p)) (M : Nat) (hM : ∀ p, p ∈ l → nrm (kz p) ≤ M + 1)
    (ι h ν Es : R) (hh : O.nn h) (hν : O.nn ν) (hh0 : h ≠ K.zero) (hν0 : ν ≠ K.zero) (hι0 : ι ≠ K.zero)
    (hEs : O.nn Es) (κ3 : R) (hκ3 : O.nn κ3) (hκle : Le K O.toCone (K.mul (EnstH3.c3 K) (K.mul ι ι)) κ3)
    (hstepM : Le K O.toCone (K.mul (K.mul h ν) (cM K M)) K.one)
    (a N : Nat → I → V3 R)
    (hs : ∀ t r, Step K h ν (wt K kz r) (a (t + 1) r) (a t r) (N t r))
    (hneut : ∀ t, lsum K (fun r => dot3 K (N t r) (a t r)) l = K.zero)
    (hP : ∀ t, Le K O.toCone (lsum K (fun r => dot3 K (N t r) (N t r)) l)
      (lsum K (fun r => dot3 K (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (a t) (a t) r)
        (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (a t) (a t) r)) l))
    (hPw : ∀ t, Le K O.toCone (lsum K (fun r => K.mul (wt K kz r) (dot3 K (N t r) (N t r))) l)
      (lsum K (fun r => K.mul (wt K kz r) (dot3 K (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (a t) (a t) r)
        (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (a t) (a t) r))) l))
    (hPw2 : ∀ t, Le K O.toCone (lsum K (fun r => K.mul (K.mul (wt K kz r) (wt K kz r)) (dot3 K (N t r) (N t r))) l)
      (lsum K (fun r => K.mul (K.mul (wt K kz r) (wt K kz r)) (dot3 K (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (a t) (a t) r)
        (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (a t) (a t) r))) l))
    (hcfl : Le K O.toCone (beta K M ι h ν Es) (K.mul h ν))
    (h0 : Le K O.toCone (K.add (lsum K (fun r => dot3 K (a 0 r) (a 0 r)) l)
      (K.mul (beta K M ι h ν Es) (lsum K (fun r => K.mul (wt K kz r) (dot3 K (a 0 r) (a 0 r))) l))) Es) (n n2 n3 : Nat)
    (htime1 : Le K O.toCone
      (K.mul (K.add K.one K.one) (K.mul (K.add (K.mul (castN K 135) (K.mul ι ι)) (K.mul (castN K 135) (K.mul ι ι)))
        (K.add (lsum K (fun r => dot3 K (a 0 r) (a 0 r)) l)
          (K.mul (K.mul h ν) (lsum K (fun r => K.mul (wt K kz r) (dot3 K (a 0 r) (a 0 r))) l)))))
      (K.mul (K.mul ν ν) (K.mul (castN K (n + 1)) (K.mul h ν))))
    (htime2 : Le K O.toCone (K.mul (K.add K.one K.one) (K.add (K.mul (EnstH2.cH K) (K.mul ι ι)) (K.mul (EnstH2.cH K) (K.mul ι ι))))
      (K.mul (K.mul (castN K 135) (K.mul ι ι)) (K.mul (castN K (n2 + 1)) (K.mul h ν))))
    (htime3 : Le K O.toCone (K.mul (K.add K.one K.one) κ3)
      (K.mul (K.mul (EnstH2.cH K) (K.mul ι ι)) (K.mul (castN K (n3 + 1)) (K.mul h ν)))) :
    ∃ t3, Le K O.toCone (K.mul κ3
        (Q K ν h (fun t => lsum K (fun p => K.mul (K.mul (wt K kz p) (K.mul (wt K kz p) (wt K kz p))) (dot3 K (a t p) (a t p))) l)
          (fun t => lsum K (fun p => K.mul (K.mul (K.mul (wt K kz p) (wt K kz p)) (K.mul (wt K kz p) (wt K kz p))) (dot3 K (a t p) (a t p))) l) t3))
        (K.mul ν (K.mul ν ν)) ∧
      ∀ s, Le K O.toCone
        (Q K ν h (fun t => lsum K (fun p => K.mul (K.mul (wt K kz p) (K.mul (wt K kz p) (wt K kz p))) (dot3 K (a t p) (a t p))) l)
          (fun t => lsum K (fun p => K.mul (K.mul (K.mul (wt K kz p) (wt K kz p)) (K.mul (wt K kz p) (wt K kz p))) (dot3 K (a t p) (a t p))) l) (t3 + s))
        (Q K ν h (fun t => lsum K (fun p => K.mul (K.mul (wt K kz p) (K.mul (wt K kz p) (wt K kz p))) (dot3 K (a t p) (a t p))) l)
          (fun t => lsum K (fun p => K.mul (K.mul (K.mul (wt K kz p) (wt K kz p)) (K.mul (wt K kz p) (wt K kz p))) (dot3 K (a t p) (a t p))) l) t3) := by
  have hw : ∀ r, O.nn (wt K kz r) := fun r => dot3_nn K O.toCone _
  have two : O.nn (K.add K.one K.one) := O.nn_add _ _ (RatModel.nn_one' K O.toCone) (RatModel.nn_one' K O.toCone)
  have three : O.nn (K.add (K.add K.one K.one) K.one) := O.nn_add _ _ two (RatModel.nn_one' K O.toCone)
  have n135 : O.nn (castN K 135) := RatModel.nn_cast K O.toCone 135
  have ncH : O.nn (EnstH2.cH K) := O.nn_add _ _ (O.nn_mul _ _ two (O.nn_mul _ _ three n135)) (O.nn_mul _ _ two n135)
  have hd2 : O.nn (K.mul (EnstH2.cH K) (K.mul ι ι)) := O.nn_mul _ _ ncH (O.nn_sq ι)
  have hc3 : O.nn (K.mul (EnstH3.c3 K) (K.mul ι ι)) :=
    O.nn_mul _ _ (O.nn_mul _ _ (EnstH3.nn8 K O) (O.nn_add _ _ (O.nn_mul _ _ (EnstH3.nn9 K O) n135) n135)) (O.nn_sq ι)
  have hA : ∀ t, O.nn (lsum K (fun p => K.mul (K.mul (wt K kz p) (wt K kz p)) (dot3 K (a t p) (a t p))) l) := fun t =>
    lsum_nn K O.toCone _ (fun p => O.nn_mul _ _ (O.nn_sq _) (dot3_nn K O.toCone _)) l
  have hB : ∀ t, O.nn (lsum K (fun p => K.mul (K.mul (K.mul (wt K kz p) (wt K kz p)) (wt K kz p)) (dot3 K (a t p) (a t p))) l) := fun t =>
    lsum_nn K O.toCone _ (fun p => O.nn_mul _ _ (O.nn_mul _ _ (O.nn_sq _) (hw p)) (dot3_nn K O.toCone _)) l
  -- stage H²: a time t2 with 2d₂·Q₂(t2) ≤ ν³
  have ⟨t2, hk, _⟩ := EventH2K.h2_eventual_k K O inv mul_inv h10 l kz hd h1 M hM ι h ν Es hh hν hh0 hν0 hι0 hEs
    (K.add (K.mul (EnstH2.cH K) (K.mul ι ι)) (K.mul (EnstH2.cH K) (K.mul ι ι))) (O.nn_add _ _ hd2 hd2)
    ⟨K.mul (EnstH2.cH K) (K.mul ι ι), hd2, rfl⟩ hstepM a N hs hneut hP hPw hcfl h0 n n2 htime1 htime2
  -- dissipated H³ mass after t2
  have dis := EnstDiss.enst_diss K O ν h (K.mul (EnstH2.cH K) (K.mul ι ι))
    (fun j => lsum K (fun p => K.mul (K.mul (wt K kz p) (wt K kz p)) (dot3 K (a (t2 + j) p) (a (t2 + j) p))) l)
    (fun j => lsum K (fun p => K.mul (K.mul (K.mul (wt K kz p) (wt K kz p)) (wt K kz p)) (dot3 K (a (t2 + j) p) (a (t2 + j) p))) l)
    hν hν0 hh hd2 (fun j => hA _) (fun j => hB _)
    (fun j => H2Step.h2_step K O inv mul_inv h10 l kz hd h1 M hM ι h ν hh hν a N hPw hs (t2 + j)) hk (n3 + 1)
  have nQ : ∀ j, O.nn (Q K ν h (fun j => lsum K (fun p => K.mul (K.mul (wt K kz p) (wt K kz p)) (dot3 K (a (t2 + j) p) (a (t2 + j) p))) l)
      (fun j => lsum K (fun p => K.mul (K.mul (K.mul (wt K kz p) (wt K kz p)) (wt K kz p)) (dot3 K (a (t2 + j) p) (a (t2 + j) p))) l) j) :=
    fun j => O.nn_add _ _ (O.nn_mul _ _ hν (hA _)) (O.nn_mul _ _ (O.nn_mul _ _ hh (O.nn_sq ν)) (hB _))
  have hsum : Le K O.toCone (sumN K (fun j => K.mul (K.mul h (K.mul ν ν))
        (lsum K (fun p => K.mul (K.mul (K.mul (wt K kz p) (wt K kz p)) (wt K kz p)) (dot3 K (a (t2 + j) p) (a (t2 + j) p))) l)) (n3 + 1))
      (K.mul (K.add K.one K.one) (Q K ν h (fun j => lsum K (fun p => K.mul (K.mul (wt K kz p) (wt K kz p)) (dot3 K (a (t2 + j) p) (a (t2 + j) p))) l)
        (fun j => lsum K (fun p => K.mul (K.mul (K.mul (wt K kz p) (wt K kz p)) (wt K kz p)) (dot3 K (a (t2 + j) p) (a (t2 + j) p))) l) 0)) := by
    rw [NSChain.sumN_mul_left K _ _ (n3 + 1)]
    exact le_trans' K O.toCone _ _ _ ⟨K.mul (K.add K.one K.one) (Q K ν h _ _ (n3 + 1)), O.nn_mul _ _ two (nQ _), by lib_ring⟩ dis
  have ⟨j, _, hj⟩ := pigeon K O _ _ n3 hsum
  have hj0 : Le K O.toCone (K.mul (castN K (n3 + 1)) (K.mul (K.mul h (K.mul ν ν))
        (lsum K (fun p => K.mul (K.mul (K.mul (wt K kz p) (wt K kz p)) (wt K kz p)) (dot3 K (a (t2 + j) p) (a (t2 + j) p))) l)))
      (K.mul (K.add K.one K.one) (Q K ν h (fun j => lsum K (fun p => K.mul (K.mul (wt K kz p) (wt K kz p)) (dot3 K (a (t2 + j) p) (a (t2 + j) p))) l)
        (fun j => lsum K (fun p => K.mul (K.mul (K.mul (wt K kz p) (wt K kz p)) (wt K kz p)) (dot3 K (a (t2 + j) p) (a (t2 + j) p))) l) 0)) := hj
  rw [EnstH3.Bform K kz l (a (t2 + j))] at hj0
  have hk' : Le K O.toCone (K.mul (K.add (K.mul (EnstH2.cH K) (K.mul ι ι)) (K.mul (EnstH2.cH K) (K.mul ι ι)))
      (Q K ν h (fun j => lsum K (fun p => K.mul (K.mul (wt K kz p) (wt K kz p)) (dot3 K (a (t2 + j) p) (a (t2 + j) p))) l)
        (fun j => lsum K (fun p => K.mul (K.mul (K.mul (wt K kz p) (wt K kz p)) (wt K kz p)) (dot3 K (a (t2 + j) p) (a (t2 + j) p))) l) 0))
      (K.mul ν (K.mul ν ν)) := hk
  generalize Q K ν h (fun j => lsum K (fun p => K.mul (K.mul (wt K kz p) (wt K kz p)) (dot3 K (a (t2 + j) p) (a (t2 + j) p))) l)
    (fun j => lsum K (fun p => K.mul (K.mul (K.mul (wt K kz p) (wt K kz p)) (wt K kz p)) (dot3 K (a (t2 + j) p) (a (t2 + j) p))) l) 0 = Q1 at hj0 hk'
  -- stage H³ at t3 = t2 + j
  have nm3 : O.nn (K.mul (castN K (n3 + 1)) (K.mul h ν)) := O.nn_mul _ _ (RatModel.nn_cast K O.toCone _) (O.nn_mul _ _ hh hν)
  have m30 : K.mul (castN K (n3 + 1)) (K.mul h ν) ≠ K.zero := by
    intro e
    cases O.noZD _ _ e with
    | inl e1 => exact cast_succ_ne K O h10 n3 e1
    | inr e2 =>
      cases O.noZD _ _ e2 with
      | inl e3 => exact hh0 e3
      | inr e4 => exact hν0 e4
  have cm0 : K.mul (K.mul (EnstH2.cH K) (K.mul ι ι)) (K.mul (castN K (n3 + 1)) (K.mul h ν)) ≠ K.zero := by
    intro e
    cases O.noZD _ _ e with
    | inl e1 =>
      cases O.noZD _ _ e1 with
      | inl e2 => exact cH_ne K O h10 e2
      | inr e3 =>
        cases O.noZD _ _ e3 with
        | inl e4 => exact hι0 e4
        | inr e5 => exact hι0 e5
    | inr e2 => exact m30 e2
  have hBt := lsum_nn K O.toCone _ (fun r => O.nn_mul _ _ (O.nn_mul _ _ (hw r) (O.nn_sq (wt K kz r))) (dot3_nn K O.toCone (a (t2 + j) r))) l
  have hQ3 := alg_q K O ν h (cM K M)
    (lsum K (fun p => K.mul (K.mul (wt K kz p) (K.mul (wt K kz p) (wt K kz p))) (dot3 K (a (t2 + j) p) (a (t2 + j) p))) l)
    (lsum K (fun p => K.mul (K.mul (K.mul (wt K kz p) (wt K kz p)) (K.mul (wt K kz p) (wt K kz p))) (dot3 K (a (t2 + j) p) (a (t2 + j) p))) l)
    hν hh hBt (B4_le K O l kz M hM (a (t2 + j))) hstepM
  have small3 := alg_cancel2 K O (K.mul (EnstH2.cH K) (K.mul ι ι)) κ3
    (K.mul (castN K (n3 + 1)) (K.mul h ν)) ν
    (lsum K (fun p => K.mul (K.mul (wt K kz p) (K.mul (wt K kz p) (wt K kz p))) (dot3 K (a (t2 + j) p) (a (t2 + j) p))) l) Q1 _
    hd2 hκ3 nm3 hν cm0
    (le_trans' K O.toCone _ _ _ ⟨K.zero, O.nn_zero, by lib_ring⟩ (le_trans' K O.toCone _ _ _ hj0 ⟨K.zero, O.nn_zero, by lib_ring⟩))
    hk' htime3 hQ3
  have nB4 := lsum_nn K O.toCone _ (fun r => O.nn_mul _ _ (O.nn_mul _ _ (O.nn_sq (wt K kz r)) (O.nn_sq (wt K kz r))) (dot3_nn K O.toCone (a (t2 + j) r))) l
  have nQ3 : O.nn (Q K ν h (fun t => lsum K (fun p => K.mul (K.mul (wt K kz p) (K.mul (wt K kz p) (wt K kz p))) (dot3 K (a t p) (a t p))) l)
      (fun t => lsum K (fun p => K.mul (K.mul (K.mul (wt K kz p) (wt K kz p)) (K.mul (wt K kz p) (wt K kz p))) (dot3 K (a t p) (a t p))) l) (t2 + j)) :=
    O.nn_add _ _ (O.nn_mul _ _ hν hBt) (O.nn_mul _ _ (O.nn_mul _ _ hh (O.nn_sq ν)) nB4)
  have smallH := le_trans' K O.toCone _ _ _ (le_mul_right' K O.toCone hκle nQ3) small3
  refine ⟨t2 + j, small3, fun s => ?_⟩
  exact EnstH3.h3_global K O inv mul_inv h10 l kz hd h1 M hM ι h ν hh hν hν0
    (fun s => a (t2 + j + s)) (fun s => N (t2 + j + s)) (fun s => hPw2 (t2 + j + s))
    (fun s r => hs (t2 + j + s) r) smallH s

end Gen

end EventH3K

#print axioms EventH3K.event_h3_k

ns/EventH4.lean

lean_all/ns/EventH4.lean

/-
  ns/EventH4.lean — T37 cycle 1333 (goal G794 — done): discrete eventual H⁴ regularity for arbitrary data, over an
  ordered field. Hypotheses of EventH3.event_h3 (with κ₃ = 2c₃ι²) plus Σw3|N|² ≤ Σw3|Nl|² (w3 = w³, Leray
  projection). With c₄ = 32(27·135 + 135), B₄' = Σw·w3|a|², B₅ = Σ(w·w)w3|a|², Q₄ = νB₄' + hν²B₅: if moreover
      2·c₄ι² ≤ c₃ι²·T_4,    T_4 = (n_4 + 1)hν          (a pure-number waiting time)
  then some time t_4 has
    event_h4   c₄ι²·Q₄(t_4) ≤ ν³   and   Q₄(t_4 + s) ≤ Q₄(t_4) for every s,
  all constants independent of the truncation. Pieces: c3_ne (c₃ ≠ 0), B4form ((w·w)(w·w) = w·w3), B5_le (B₅ ≤ c_M B₄').

  Lean 4 core plus ns/EventH3K, ns/H3Step, ns/EnstH4.
-/
import ns.EventH3K
import ns.H3Step
import ns.EnstH4

namespace EventH4
open DetChain NSChain DyadicSmallGlobal LibRing DyadicLarge FourierGalerkin GalerkinForce ForceBound ForceSob
  ModeCount CubeCount ForceUniform NormCompare EnstStep EnstGalerkin EnstGlobal CFLStep CFLGlobal EventSmall EventReg
  EventH2 ForceH4

section Gen
variable {R : Type} (K : CRing R) (O : OrdR K) {I : Type}
variable (inv : R → R) (mul_inv : ∀ y, y ≠ K.zero → K.mul y (inv y) = K.one) (h10 : K.one ≠ K.zero)

include O h10 in
theorem c3_ne : EnstH3.c3 K ≠ K.zero := by
  have e : EnstH3.c3 K = K.mul (K.add (Tri4.k8 K) (K.mul (Tri4.k8 K) (EnstH3.k9 K))) (castN K 135) := by
    unfold EnstH3.c3; lib_ring
  have one := RatModel.nn_one' K O.toCone
  have two : O.nn (K.add K.one K.one) := O.nn_add _ _ one one
  have four : O.nn (K.add (K.add K.one K.one) (K.add K.one K.one)) := O.nn_add _ _ two two
  intro h0
  rw [e] at h0
  cases O.noZD _ _ h0 with
  | inl e1 =>
    have k0 := nn_sum_zero K O _ _ (EnstH3.nn8 K O) (O.nn_mul _ _ (EnstH3.nn8 K O) (EnstH3.nn9 K O)) e1
    have f0 : 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.zero := k0
    have g0 := nn_sum_zero K O _ _ four four f0
    have t0 := nn_sum_zero K O _ _ two two g0
    exact h10 (nn_sum_zero K O _ _ one one t0)
  | inr e2 => exact cast_succ_ne K O h10 134 e2

theorem B4form (kz : I → Mode) (l : List I) (a : I → V3 R) :
    lsum K (fun p => K.mul (K.mul (K.mul (wt K kz p) (wt K kz p)) (K.mul (wt K kz p) (wt K kz p))) (dot3 K (a p) (a p))) l
      = lsum K (fun p => K.mul (K.mul (wt K kz p) (w3 K kz p)) (dot3 K (a p) (a p))) l :=
  lsum_ext K _ _ (fun p => by unfold w3; lib_ring) l

theorem B5_le (l : List I) (kz : I → Mode) (M : Nat) (hM : ∀ p, p ∈ l → nrm (kz p) ≤ M + 1) (a : I → V3 R) :
    Le K O.toCone (lsum K (fun r => K.mul (K.mul (K.mul (wt K kz r) (wt K kz r)) (w3 K kz r)) (dot3 K (a r) (a r))) l)
      (K.mul (cM K M) (lsum K (fun r => K.mul (K.mul (wt K kz r) (w3 K kz r)) (dot3 K (a r) (a r))) l)) := by
  rw [← lsum_mul_left]
  apply lsum_le_mem K O
  intro r hr
  have hwr : O.nn (wt K kz r) := dot3_nn K O.toCone _
  have m := ForceSob.le_mul_right' K O.toCone (wt_le K O kz M r (hM r hr))
    (O.nn_mul _ _ (O.nn_mul _ _ hwr (O.nn_mul _ _ (O.nn_sq (wt K kz r)) hwr)) (dot3_nn K O.toCone (a r)))
  exact le_trans' K O.toCone _ _ _ ⟨K.zero, O.nn_zero, by unfold w3; lib_ring⟩ (le_trans' K O.toCone _ _ _ m ⟨K.zero, O.nn_zero, by unfold w3; lib_ring⟩)

include mul_inv h10 in
theorem event_h4 (l : List I) (kz : I → Mode) (hd : List.Pairwise (fun a b => kz a ≠ kz b) l)
    (h1 : ∀ p, 1 ≤ nrm (kz p)) (M : Nat) (hM : ∀ p, p ∈ l → nrm (kz p) ≤ M + 1)
    (ι h ν Es : R) (hh : O.nn h) (hν : O.nn ν) (hh0 : h ≠ K.zero) (hν0 : ν ≠ K.zero) (hι0 : ι ≠ K.zero)
    (hEs : O.nn Es)
    (hstepM : Le K O.toCone (K.mul (K.mul h ν) (cM K M)) K.one)
    (a N : Nat → I → V3 R)
    (hs : ∀ t r, Step K h ν (wt K kz r) (a (t + 1) r) (a t r) (N t r))
    (hneut : ∀ t, lsum K (fun r => dot3 K (N t r) (a t r)) l = K.zero)
    (hP : ∀ t, Le K O.toCone (lsum K (fun r => dot3 K (N t r) (N t r)) l)
      (lsum K (fun r => dot3 K (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (a t) (a t) r)
        (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (a t) (a t) r)) l))
    (hPw : ∀ t, Le K O.toCone (lsum K (fun r => K.mul (wt K kz r) (dot3 K (N t r) (N t r))) l)
      (lsum K (fun r => K.mul (wt K kz r) (dot3 K (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (a t) (a t) r)
        (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (a t) (a t) r))) l))
    (hPw2 : ∀ t, Le K O.toCone (lsum K (fun r => K.mul (K.mul (wt K kz r) (wt K kz r)) (dot3 K (N t r) (N t r))) l)
      (lsum K (fun r => K.mul (K.mul (wt K kz r) (wt K kz r)) (dot3 K (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (a t) (a t) r)
        (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (a t) (a t) r))) l))
    (hPw3 : ∀ t, Le K O.toCone (lsum K (fun r => K.mul (w3 K kz r) (dot3 K (N t r) (N t r))) l)
      (lsum K (fun r => K.mul (w3 K kz r) (dot3 K (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (a t) (a t) r)
        (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (a t) (a t) r))) l))
    (hcfl : Le K O.toCone (beta K M ι h ν Es) (K.mul h ν))
    (h0 : Le K O.toCone (K.add (lsum K (fun r => dot3 K (a 0 r) (a 0 r)) l)
      (K.mul (beta K M ι h ν Es) (lsum K (fun r => K.mul (wt K kz r) (dot3 K (a 0 r) (a 0 r))) l))) Es) (n n2 n3 n4 : Nat)
    (htime1 : Le K O.toCone
      (K.mul (K.add K.one K.one) (K.mul (K.add (K.mul (castN K 135) (K.mul ι ι)) (K.mul (castN K 135) (K.mul ι ι)))
        (K.add (lsum K (fun r => dot3 K (a 0 r) (a 0 r)) l)
          (K.mul (K.mul h ν) (lsum K (fun r => K.mul (wt K kz r) (dot3 K (a 0 r) (a 0 r))) l)))))
      (K.mul (K.mul ν ν) (K.mul (castN K (n + 1)) (K.mul h ν))))
    (htime2 : Le K O.toCone (K.mul (K.add K.one K.one) (K.add (K.mul (EnstH2.cH K) (K.mul ι ι)) (K.mul (EnstH2.cH K) (K.mul ι ι))))
      (K.mul (K.mul (castN K 135) (K.mul ι ι)) (K.mul (castN K (n2 + 1)) (K.mul h ν))))
    (htime3 : Le K O.toCone (K.mul (K.add K.one K.one) (K.add (K.mul (EnstH3.c3 K) (K.mul ι ι)) (K.mul (EnstH3.c3 K) (K.mul ι ι))))
      (K.mul (K.mul (EnstH2.cH K) (K.mul ι ι)) (K.mul (castN K (n3 + 1)) (K.mul h ν))))
    (htime4 : Le K O.toCone (K.mul (K.add K.one K.one) (K.mul (EnstH4.c4 K) (K.mul ι ι)))
      (K.mul (K.mul (EnstH3.c3 K) (K.mul ι ι)) (K.mul (castN K (n4 + 1)) (K.mul h ν)))) :
    ∃ t4, Le K O.toCone (K.mul (K.mul (EnstH4.c4 K) (K.mul ι ι))
        (Q K ν h (fun t => lsum K (fun p => K.mul (K.mul (wt K kz p) (w3 K kz p)) (dot3 K (a t p) (a t p))) l)
          (fun t => lsum K (fun p => K.mul (K.mul (K.mul (wt K kz p) (wt K kz p)) (w3 K kz p)) (dot3 K (a t p) (a t p))) l) t4))
        (K.mul ν (K.mul ν ν)) ∧
      ∀ s, Le K O.toCone
        (Q K ν h (fun t => lsum K (fun p => K.mul (K.mul (wt K kz p) (w3 K kz p)) (dot3 K (a t p) (a t p))) l)
          (fun t => lsum K (fun p => K.mul (K.mul (K.mul (wt K kz p) (wt K kz p)) (w3 K kz p)) (dot3 K (a t p) (a t p))) l) (t4 + s))
        (Q K ν h (fun t => lsum K (fun p => K.mul (K.mul (wt K kz p) (w3 K kz p)) (dot3 K (a t p) (a t p))) l)
          (fun t => lsum K (fun p => K.mul (K.mul (K.mul (wt K kz p) (wt K kz p)) (w3 K kz p)) (dot3 K (a t p) (a t p))) l) t4) := by
  have hw : ∀ r, O.nn (wt K kz r) := fun r => dot3_nn K O.toCone _
  have two : O.nn (K.add K.one K.one) := O.nn_add _ _ (RatModel.nn_one' K O.toCone) (RatModel.nn_one' K O.toCone)
  have n135 : O.nn (castN K 135) := RatModel.nn_cast K O.toCone 135
  have hc3 : O.nn (K.mul (EnstH3.c3 K) (K.mul ι ι)) :=
    O.nn_mul _ _ (O.nn_mul _ _ (EnstH3.nn8 K O) (O.nn_add _ _ (O.nn_mul _ _ (EnstH3.nn9 K O) n135) n135)) (O.nn_sq ι)
  have hc4 : O.nn (K.mul (EnstH4.c4 K) (K.mul ι ι)) :=
    O.nn_mul _ _ (O.nn_mul _ _ (ForceH4.nn32 K O) (O.nn_add _ _ (O.nn_mul _ _ (EnstH4.nn27 K O) n135) n135)) (O.nn_sq ι)
  have hA : ∀ t, O.nn (lsum K (fun p => K.mul (K.mul (wt K kz p) (K.mul (wt K kz p) (wt K kz p))) (dot3 K (a t p) (a t p))) l) := fun t =>
    lsum_nn K O.toCone _ (fun p => O.nn_mul _ _ (O.nn_mul _ _ (hw p) (O.nn_sq _)) (dot3_nn K O.toCone _)) l
  have hB : ∀ t, O.nn (lsum K (fun p => K.mul (K.mul (K.mul (wt K kz p) (wt K kz p)) (K.mul (wt K kz p) (wt K kz p))) (dot3 K (a t p) (a t p))) l) := fun t =>
    lsum_nn K O.toCone _ (fun p => O.nn_mul _ _ (O.nn_mul _ _ (O.nn_sq _) (O.nn_sq _)) (dot3_nn K O.toCone _)) l
  -- stage H³: a time t3 with 2c₃ι²·Q₃(t3) ≤ ν³
  have ⟨t3, hk, _⟩ := EventH3K.event_h3_k K O inv mul_inv h10 l kz hd h1 M hM ι h ν Es hh hν hh0 hν0 hι0 hEs
    (K.add (K.mul (EnstH3.c3 K) (K.mul ι ι)) (K.mul (EnstH3.c3 K) (K.mul ι ι))) (O.nn_add _ _ hc3 hc3)
    ⟨K.mul (EnstH3.c3 K) (K.mul ι ι), hc3, rfl⟩ hstepM a N hs hneut hP hPw hPw2 hcfl h0 n n2 n3 htime1 htime2 htime3
  -- dissipated H⁴ mass after t3
  have dis := EnstDiss.enst_diss K O ν h (K.mul (EnstH3.c3 K) (K.mul ι ι)) (fun j => lsum K (fun p => K.mul (K.mul (wt K kz p) (K.mul (wt K kz p) (wt K kz p))) (dot3 K (a (t3 + j) p) (a (t3 + j) p))) l) (fun j => lsum K (fun p => K.mul (K.mul (K.mul (wt K kz p) (wt K kz p)) (K.mul (wt K kz p) (wt K kz p))) (dot3 K (a (t3 + j) p) (a (t3 + j) p))) l)
    hν hν0 hh hc3 (fun j => hA _) (fun j => hB _)
    (fun j => H3Step.h3_step K O inv mul_inv h10 l kz hd h1 M hM ι h ν hh hν a N hPw2 hs (t3 + j)) hk (n4 + 1)
  have nQ : ∀ j, O.nn (Q K ν h (fun j => lsum K (fun p => K.mul (K.mul (wt K kz p) (K.mul (wt K kz p) (wt K kz p))) (dot3 K (a (t3 + j) p) (a (t3 + j) p))) l) (fun j => lsum K (fun p => K.mul (K.mul (K.mul (wt K kz p) (wt K kz p)) (K.mul (wt K kz p) (wt K kz p))) (dot3 K (a (t3 + j) p) (a (t3 + j) p))) l) j) :=
    fun j => O.nn_add _ _ (O.nn_mul _ _ hν (hA _)) (O.nn_mul _ _ (O.nn_mul _ _ hh (O.nn_sq ν)) (hB _))
  have hsum : Le K O.toCone (sumN K (fun j => K.mul (K.mul h (K.mul ν ν))
        (lsum K (fun p => K.mul (K.mul (K.mul (wt K kz p) (wt K kz p)) (K.mul (wt K kz p) (wt K kz p))) (dot3 K (a (t3 + j) p) (a (t3 + j) p))) l)) (n4 + 1))
      (K.mul (K.add K.one K.one) (Q K ν h (fun j => lsum K (fun p => K.mul (K.mul (wt K kz p) (K.mul (wt K kz p) (wt K kz p))) (dot3 K (a (t3 + j) p) (a (t3 + j) p))) l) (fun j => lsum K (fun p => K.mul (K.mul (K.mul (wt K kz p) (wt K kz p)) (K.mul (wt K kz p) (wt K kz p))) (dot3 K (a (t3 + j) p) (a (t3 + j) p))) l) 0)) := by
    rw [NSChain.sumN_mul_left K _ _ (n4 + 1)]
    exact le_trans' K O.toCone _ _ _ ⟨K.mul (K.add K.one K.one) (Q K ν h _ _ (n4 + 1)), O.nn_mul _ _ two (nQ _), by lib_ring⟩ dis
  have ⟨j, _, hj⟩ := pigeon K O _ _ n4 hsum
  have hj0 : Le K O.toCone (K.mul (castN K (n4 + 1)) (K.mul (K.mul h (K.mul ν ν)) (lsum K (fun p => K.mul (K.mul (K.mul (wt K kz p) (wt K kz p)) (K.mul (wt K kz p) (wt K kz p))) (dot3 K (a (t3 + j) p) (a (t3 + j) p))) l)))
      (K.mul (K.add K.one K.one) (Q K ν h (fun j => lsum K (fun p => K.mul (K.mul (wt K kz p) (K.mul (wt K kz p) (wt K kz p))) (dot3 K (a (t3 + j) p) (a (t3 + j) p))) l) (fun j => lsum K (fun p => K.mul (K.mul (K.mul (wt K kz p) (wt K kz p)) (K.mul (wt K kz p) (wt K kz p))) (dot3 K (a (t3 + j) p) (a (t3 + j) p))) l) 0)) := hj
  rw [B4form K kz l (a (t3 + j))] at hj0
  have hk' : Le K O.toCone (K.mul (K.add (K.mul (EnstH3.c3 K) (K.mul ι ι)) (K.mul (EnstH3.c3 K) (K.mul ι ι)))
      (Q K ν h (fun j => lsum K (fun p => K.mul (K.mul (wt K kz p) (K.mul (wt K kz p) (wt K kz p))) (dot3 K (a (t3 + j) p) (a (t3 + j) p))) l) (fun j => lsum K (fun p => K.mul (K.mul (K.mul (wt K kz p) (wt K kz p)) (K.mul (wt K kz p) (wt K kz p))) (dot3 K (a (t3 + j) p) (a (t3 + j) p))) l) 0)) (K.mul ν (K.mul ν ν)) := hk
  generalize Q K ν h (fun j => lsum K (fun p => K.mul (K.mul (wt K kz p) (K.mul (wt K kz p) (wt K kz p))) (dot3 K (a (t3 + j) p) (a (t3 + j) p))) l) (fun j => lsum K (fun p => K.mul (K.mul (K.mul (wt K kz p) (wt K kz p)) (K.mul (wt K kz p) (wt K kz p))) (dot3 K (a (t3 + j) p) (a (t3 + j) p))) l) 0 = Q1 at hj0 hk'
  -- stage H⁴ at t4 = t3 + j
  have nm4 : O.nn (K.mul (castN K (n4 + 1)) (K.mul h ν)) := O.nn_mul _ _ (RatModel.nn_cast K O.toCone _) (O.nn_mul _ _ hh hν)
  have m40 : K.mul (castN K (n4 + 1)) (K.mul h ν) ≠ K.zero := by
    intro e
    cases O.noZD _ _ e with
    | inl e1 => exact cast_succ_ne K O h10 n4 e1
    | inr e2 =>
      cases O.noZD _ _ e2 with
      | inl e3 => exact hh0 e3
      | inr e4 => exact hν0 e4
  have cm0 : K.mul (K.mul (EnstH3.c3 K) (K.mul ι ι)) (K.mul (castN K (n4 + 1)) (K.mul h ν)) ≠ K.zero := by
    intro e
    cases O.noZD _ _ e with
    | inl e1 =>
      cases O.noZD _ _ e1 with
      | inl e2 => exact c3_ne K O h10 e2
      | inr e3 =>
        cases O.noZD _ _ e3 with
        | inl e4 => exact hι0 e4
        | inr e5 => exact hι0 e5
    | inr e2 => exact m40 e2
  have hBt : O.nn (lsum K (fun p => K.mul (K.mul (wt K kz p) (w3 K kz p)) (dot3 K (a (t3 + j) p) (a (t3 + j) p))) l) :=
    lsum_nn K O.toCone _ (fun r => O.nn_mul _ _ (O.nn_mul _ _ (hw r) (O.nn_mul _ _ (O.nn_sq _) (hw r))) (dot3_nn K O.toCone _)) l
  have hQ4 := alg_q K O ν h (cM K M) (lsum K (fun p => K.mul (K.mul (wt K kz p) (w3 K kz p)) (dot3 K (a (t3 + j) p) (a (t3 + j) p))) l) (lsum K (fun p => K.mul (K.mul (K.mul (wt K kz p) (wt K kz p)) (w3 K kz p)) (dot3 K (a (t3 + j) p) (a (t3 + j) p))) l) hν hh hBt (B5_le K O l kz M hM (a (t3 + j))) hstepM
  have small4 := alg_cancel2 K O (K.mul (EnstH3.c3 K) (K.mul ι ι)) (K.mul (EnstH4.c4 K) (K.mul ι ι))
    (K.mul (castN K (n4 + 1)) (K.mul h ν)) ν (lsum K (fun p => K.mul (K.mul (wt K kz p) (w3 K kz p)) (dot3 K (a (t3 + j) p) (a (t3 + j) p))) l) Q1 _
    hc3 hc4 nm4 hν cm0
    (le_trans' K O.toCone _ _ _ ⟨K.zero, O.nn_zero, by lib_ring⟩ (le_trans' K O.toCone _ _ _ hj0 ⟨K.zero, O.nn_zero, by lib_ring⟩))
    hk' htime4 hQ4
  refine ⟨t3 + j, small4, fun s => ?_⟩
  exact EnstH4.h4_global K O inv mul_inv h10 l kz hd h1 M hM ι h ν hh hν hν0
    (fun s => a (t3 + j + s)) (fun s => N (t3 + j + s)) (fun s => hPw3 (t3 + j + s))
    (fun s r => hs (t3 + j + s) r) small4 s

end Gen

end EventH4

#print axioms EventH4.c3_ne
#print axioms EventH4.B4form
#print axioms EventH4.B5_le
#print axioms EventH4.event_h4

ns/EventHolder.lean

lean_all/ns/EventHolder.lean

/-
  ns/EventHolder.lean — T37 cycle 1355 (goal G804 — done): eventual Hölder-1/2 equicontinuity for arbitrary data,
  over an ordered field. Under the cfl_global hypotheses, h, ν ≠ 0, hν·c_M ≤ 1, c = 135ι², W = E_0 + hν·Z_0 and
  4c·W ≤ ν²·T (T = (n + 1)hν), there is t ≤ n such that, with Q* = Q_{t+1},
    event_holder   2c·Q* ≤ ν³  and  ν³·Σ_r |a_{t+1+s+k,r} − a_{t+1+s,r}|² ≤ k·(4h(cQ* + ν³)Q*)  for all s, k,
  a time modulus independent of the truncation after a truncation-free waiting time.

  Lean 4 core plus ns/EventH2 (good_time), ns/HolderSmall.
-/
import ns.EventH2
import ns.HolderSmall

namespace EventHolder
open DetChain NSChain DyadicSmallGlobal LibRing DyadicLarge FourierGalerkin GalerkinForce ForceBound ForceSob
  ModeCount CubeCount ForceUniform NormCompare EnstStep EnstGalerkin EnstGlobal CFLStep CFLGlobal EventSmall EventReg
  EventH2

section Gen
variable {R : Type} (K : CRing R) (O : OrdR K) {I : Type}
variable (inv : R → R) (mul_inv : ∀ y, y ≠ K.zero → K.mul y (inv y) = K.one) (h10 : K.one ≠ K.zero)

include mul_inv h10 in
theorem event_holder (l : List I) (kz : I → Mode) (hd : List.Pairwise (fun a b => kz a ≠ kz b) l)
    (h1 : ∀ p, 1 ≤ nrm (kz p)) (M : Nat) (hM : ∀ p, p ∈ l → nrm (kz p) ≤ M + 1)
    (ι h ν Es : R) (hh : O.nn h) (hν : O.nn ν) (hh0 : h ≠ K.zero) (hν0 : ν ≠ K.zero) (hEs : O.nn Es)
    (hstepM : Le K O.toCone (K.mul (K.mul h ν) (cM K M)) K.one)
    (a N : Nat → I → V3 R)
    (hs : ∀ t r, Step K h ν (wt K kz r) (a (t + 1) r) (a t r) (N t r))
    (hneut : ∀ t, lsum K (fun r => dot3 K (N t r) (a t r)) l = K.zero)
    (hP : ∀ t, Le K O.toCone (lsum K (fun r => dot3 K (N t r) (N t r)) l)
      (lsum K (fun r => dot3 K (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (a t) (a t) r)
        (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (a t) (a t) r)) l))
    (hcfl : Le K O.toCone (beta K M ι h ν Es) (K.mul h ν))
    (h0 : Le K O.toCone (K.add (lsum K (fun r => dot3 K (a 0 r) (a 0 r)) l)
      (K.mul (beta K M ι h ν Es) (lsum K (fun r => K.mul (wt K kz r) (dot3 K (a 0 r) (a 0 r))) l))) Es) (n : Nat)
    (htime : Le K O.toCone
      (K.mul (K.add K.one K.one) (K.mul (K.add (K.mul (castN K 135) (K.mul ι ι)) (K.mul (castN K 135) (K.mul ι ι)))
        (K.add (lsum K (fun r => dot3 K (a 0 r) (a 0 r)) l)
          (K.mul (K.mul h ν) (lsum K (fun r => K.mul (wt K kz r) (dot3 K (a 0 r) (a 0 r))) l)))))
      (K.mul (K.mul ν ν) (K.mul (castN K (n + 1)) (K.mul h ν)))) :
    ∃ t, t < n + 1 ∧
      Le K O.toCone (K.mul (K.add (K.mul (castN K 135) (K.mul ι ι)) (K.mul (castN K 135) (K.mul ι ι)))
        (Q K ν h (Zs K l kz a) (As K l kz a) (t + 1))) (K.mul ν (K.mul ν ν)) ∧
      ∀ s k, Le K O.toCone
        (K.mul (K.mul ν (K.mul ν ν)) (lsum K (fun r => dot3 K (EnstDiff.sub3 K (a (t + 1 + (s + k)) r) (a (t + 1 + s) r))
          (EnstDiff.sub3 K (a (t + 1 + (s + k)) r) (a (t + 1 + s) r))) l))
        (K.mul (castN K k) (K.mul (K.mul (K.add (K.add K.one K.one) (K.add K.one K.one)) h)
          (K.mul (K.add (K.mul (K.mul (castN K 135) (K.mul ι ι)) (Q K ν h (Zs K l kz a) (As K l kz a) (t + 1))) (K.mul ν (K.mul ν ν)))
            (Q K ν h (Zs K l kz a) (As K l kz a) (t + 1))))) := by
  have hc : O.nn (K.mul (castN K 135) (K.mul ι ι)) := O.nn_mul _ _ (RatModel.nn_cast K O.toCone 135) (O.nn_sq ι)
  have ⟨t, ht, hq⟩ := good_time K O inv mul_inv h10 l kz hd h1 M hM ι h ν Es
    (K.add (K.mul (castN K 135) (K.mul ι ι)) (K.mul (castN K 135) (K.mul ι ι)))
    hh hν hh0 hν0 hEs (O.nn_add _ _ hc hc) hstepM a N hs hneut hP hcfl h0 n htime
  refine ⟨t, ht, hq, fun s k => ?_⟩
  exact HolderSmall.holder_small K O inv mul_inv h10 l kz hd h1 M hM ι h ν hh hν hν0
    (fun j => a (t + 1 + j)) (fun j => N (t + 1 + j)) (fun j => hP (t + 1 + j)) (fun j r => hs (t + 1 + j) r) hq s k

end Gen

end EventHolder

#print axioms EventHolder.event_holder

ns/EventReg.lean

lean_all/ns/EventReg.lean

/-
  ns/EventReg.lean — T37 cycle 1319 (goal G790 — done): discrete eventual regularity for arbitrary data, over an
  ordered field. Implicit Fourier–Galerkin scheme with Leray-projected, energy-neutral force, truncation
  |k|_∞ ≤ M + 1, CFL condition β ≤ hν and E_0 + β·Z_0 ≤ E* (the cfl_global hypotheses), h, ν ≠ 0, c = 135ι²,
  W = E_0 + hν·Z_0. If the time T = (n + 1)hν satisfies
      c·(ν + hν²·c_M)·W ≤ ν³·T,
  then there is t ≤ n such that, with Q = νZ + hν²A,
    event_reg    c·Q_{t+1} ≤ ν³   and   Q_{t+1+s} ≤ Q_{t+1} for every s ≥ 0.
  So after the good time the enstrophy stays below ν²/c forever. When hν·c_M ≤ 1 (a CFL-type step bound) the
  waiting time T ≥ 2c·W/ν³ and the final bound ν²/c do not depend on the truncation — Leray's eventual regularity
  for the discrete scheme.

  Lean 4 core plus ns/EventSmall, ns/EnstProj (enst_global_gen), ns/CFLStep (A_le_Z), ns/EnstGlobal (le_of_mul_le).
-/
import ns.EventSmall
import ns.EnstProj

namespace EventReg
open DetChain NSChain DyadicSmallGlobal LibRing DyadicLarge FourierGalerkin GalerkinForce ForceBound ForceSob
  ModeCount CubeCount ForceUniform NormCompare EnstStep EnstGalerkin EnstGlobal CFLStep CFLGlobal EventSmall

section Gen
variable {R : Type} (K : CRing R) (O : OrdR K) {I : Type}
variable (inv : R → R) (mul_inv : ∀ y, y ≠ K.zero → K.mul y (inv y) = K.one) (h10 : K.one ≠ K.zero)

include O h10 in
theorem cast_succ_ne (n : Nat) : castN K (n + 1) ≠ K.zero := by
  intro e
  have e' : K.add K.one (castN K n) = K.zero := e
  exact h10 (nn_sum_zero K O _ _ (RatModel.nn_one' K O.toCone) (RatModel.nn_cast K O.toCone n) e')

include mul_inv h10 in
theorem event_reg (l : List I) (kz : I → Mode) (hd : List.Pairwise (fun a b => kz a ≠ kz b) l)
    (h1 : ∀ p, 1 ≤ nrm (kz p)) (M : Nat) (hM : ∀ p, p ∈ l → nrm (kz p) ≤ M + 1)
    (ι h ν Es : R) (hh : O.nn h) (hν : O.nn ν) (hh0 : h ≠ K.zero) (hν0 : ν ≠ K.zero) (hEs : O.nn Es)
    (a N : Nat → I → V3 R)
    (hs : ∀ t r, Step K h ν (wt K kz r) (a (t + 1) r) (a t r) (N t r))
    (hneut : ∀ t, lsum K (fun r => dot3 K (N t r) (a t r)) l = K.zero)
    (hP : ∀ t, Le K O.toCone (lsum K (fun r => dot3 K (N t r) (N t r)) l)
      (lsum K (fun r => dot3 K (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (a t) (a t) r)
        (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (a t) (a t) r)) l))
    (hcfl : Le K O.toCone (beta K M ι h ν Es) (K.mul h ν))
    (h0 : Le K O.toCone (K.add (lsum K (fun r => dot3 K (a 0 r) (a 0 r)) l)
      (K.mul (beta K M ι h ν Es) (lsum K (fun r => K.mul (wt K kz r) (dot3 K (a 0 r) (a 0 r))) l))) Es) (n : Nat)
    (htime : Le K O.toCone
      (K.mul (K.mul (castN K 135) (K.mul ι ι)) (K.mul (K.add ν (K.mul (K.mul h (K.mul ν ν)) (cM K M)))
        (K.add (lsum K (fun r => dot3 K (a 0 r) (a 0 r)) l)
          (K.mul (K.mul h ν) (lsum K (fun r => K.mul (wt K kz r) (dot3 K (a 0 r) (a 0 r))) l)))))
      (K.mul (K.mul ν (K.mul ν ν)) (K.mul (castN K (n + 1)) (K.mul h ν)))) :
    ∃ t, t < n + 1 ∧
      Le K O.toCone (K.mul (K.mul (castN K 135) (K.mul ι ι)) (Q K ν h (Zs K l kz a) (As K l kz a) (t + 1))) (K.mul ν (K.mul ν ν)) ∧
      ∀ s, Le K O.toCone (Q K ν h (Zs K l kz a) (As K l kz a) (t + 1 + s)) (Q K ν h (Zs K l kz a) (As K l kz a) (t + 1)) := by
  have ⟨t, ht, hz⟩ := event_small K O inv mul_inv h10 l kz hd h1 M hM ι h ν Es hh hν hEs a N hs hneut hP hcfl h0 n
  have hc : O.nn (K.mul (castN K 135) (K.mul ι ι)) := O.nn_mul _ _ (RatModel.nn_cast K O.toCone 135) (O.nn_sq ι)
  have nm : O.nn (K.mul (castN K (n + 1)) (K.mul h ν)) := O.nn_mul _ _ (RatModel.nn_cast K O.toCone _) (O.nn_mul _ _ hh hν)
  have nf : O.nn (K.add ν (K.mul (K.mul h (K.mul ν ν)) (cM K M))) :=
    O.nn_add _ _ hν (O.nn_mul _ _ (O.nn_mul _ _ hh (O.nn_sq ν)) (cM_nn K O M))
  have m0 : K.mul (castN K (n + 1)) (K.mul h ν) ≠ K.zero := by
    intro e
    cases O.noZD _ _ e with
    | inl e1 => exact cast_succ_ne K O h10 n e1
    | inr e2 =>
      cases O.noZD _ _ e2 with
      | inl e3 => exact hh0 e3
      | inr e4 => exact hν0 e4
  -- Q_{t+1} ≤ (ν + hν²c_M)·Z_{t+1}
  have hA := A_le_Z K O l kz M hM (a (t + 1))
  have hQ : Le K O.toCone (Q K ν h (Zs K l kz a) (As K l kz a) (t + 1))
      (K.mul (K.add ν (K.mul (K.mul h (K.mul ν ν)) (cM K M))) (Zs K l kz a (t + 1))) := by
    have s := le_add' K O.toCone _ _ _ _ (le_refl' K O.toCone (K.mul ν (Zs K l kz a (t + 1))))
      (le_mul_nn K O.toCone (K.mul h (K.mul ν ν)) _ _ (O.nn_mul _ _ hh (O.nn_sq ν)) hA)
    refine le_trans' K O.toCone _ _ _ s ⟨K.zero, O.nn_zero, ?_⟩
    show K.add (K.add (K.mul ν (Zs K l kz a (t + 1))) (K.mul (K.mul h (K.mul ν ν)) (K.mul (cM K M) (Zs K l kz a (t + 1))))) K.zero = _
    lib_ring
  -- (n+1)hν·(c·Q_{t+1}) ≤ (n+1)hν·ν³
  have k1 := le_mul_nn K O.toCone (K.mul (K.mul (castN K 135) (K.mul ι ι)) (K.mul (castN K (n + 1)) (K.mul h ν))) _ _
    (O.nn_mul _ _ hc nm) hQ
  have k2 := le_mul_nn K O.toCone (K.mul (castN K 135) (K.mul ι ι)) _ _ hc
    (le_mul_nn K O.toCone (K.add ν (K.mul (K.mul h (K.mul ν ν)) (cM K M))) _ _ nf hz)
  have key : Le K O.toCone (K.mul (K.mul (castN K (n + 1)) (K.mul h ν)) (K.mul (K.mul (castN K 135) (K.mul ι ι)) (Q K ν h (Zs K l kz a) (As K l kz a) (t + 1))))
      (K.mul (K.mul (castN K (n + 1)) (K.mul h ν)) (K.mul ν (K.mul ν ν))) := by
    refine le_trans' K O.toCone _ _ _ ⟨K.zero, O.nn_zero, ?_⟩
      (le_trans' K O.toCone _ _ _ k1 (le_trans' K O.toCone _ _ _ ⟨K.zero, O.nn_zero, ?_⟩
        (le_trans' K O.toCone _ _ _ k2 (le_trans' K O.toCone _ _ _ htime ⟨K.zero, O.nn_zero, ?_⟩))))
    · lib_ring
    · show K.add (K.mul (K.mul (K.mul (castN K 135) (K.mul ι ι)) (K.mul (castN K (n + 1)) (K.mul h ν)))
          (K.mul (K.add ν (K.mul (K.mul h (K.mul ν ν)) (cM K M))) (Zs K l kz a (t + 1)))) K.zero
        = K.mul (K.mul (castN K 135) (K.mul ι ι)) (K.mul (K.add ν (K.mul (K.mul h (K.mul ν ν)) (cM K M)))
          (K.mul (castN K (n + 1)) (K.mul (K.mul h ν) (lsum K (fun r => K.mul (wt K kz r) (dot3 K (a (t + 1) r) (a (t + 1) r))) l))))
      show _ = K.mul _ (K.mul _ (K.mul _ (K.mul _ (Zs K l kz a (t + 1)))))
      lib_ring
    · lib_ring
  have small := le_of_mul_le K O _ _ _ nm m0 key
  refine ⟨t, ht, small, fun s => ?_⟩
  exact EnstProj.enst_global_gen K O inv mul_inv h10 l kz hd h1 M hM ι h ν hh hν hν0
    (fun s => a (t + 1 + s)) (fun s => N (t + 1 + s)) (fun s => hP (t + 1 + s)) (fun s r => hs (t + 1 + s) r) small s

end Gen

end EventReg

#print axioms EventReg.cast_succ_ne
#print axioms EventReg.event_reg

ns/EventRegU.lean

lean_all/ns/EventRegU.lean

/-
  ns/EventRegU.lean — T37 cycle 1321 (goal G791 — done): the truncation-free form of EventReg, over an ordered field.
  Under the cfl_global hypotheses, h, ν ≠ 0, the step bound hν·c_M ≤ 1, c = 135ι², W = E_0 + hν·Z_0 and a time
  T = (n + 1)hν with
      2c·W ≤ ν²·T,
  there is t ≤ n with
    event_reg_uniform   c·Q_{t+1} ≤ ν³  and  Q_{t+1+s} ≤ Q_{t+1} for all s,
  where neither the waiting time 2cW/ν² nor the final bound (Z ≤ ν²/c after t + 1) depends on the truncation.

  Lean 4 core plus ns/EventReg.
-/
import ns.EventReg

namespace EventRegU
open DetChain NSChain DyadicSmallGlobal LibRing DyadicLarge FourierGalerkin GalerkinForce ForceBound ForceSob
  ModeCount CubeCount ForceUniform NormCompare EnstStep EnstGalerkin EnstGlobal CFLStep CFLGlobal EventSmall EventReg

section Gen
variable {R : Type} (K : CRing R) (O : OrdR K) {I : Type}
variable (inv : R → R) (mul_inv : ∀ y, y ≠ K.zero → K.mul y (inv y) = K.one) (h10 : K.one ≠ K.zero)

include mul_inv h10 in
theorem event_reg_uniform (l : List I) (kz : I → Mode) (hd : List.Pairwise (fun a b => kz a ≠ kz b) l)
    (h1 : ∀ p, 1 ≤ nrm (kz p)) (M : Nat) (hM : ∀ p, p ∈ l → nrm (kz p) ≤ M + 1)
    (ι h ν Es : R) (hh : O.nn h) (hν : O.nn ν) (hh0 : h ≠ K.zero) (hν0 : ν ≠ K.zero) (hEs : O.nn Es)
    (hstepM : Le K O.toCone (K.mul (K.mul h ν) (cM K M)) K.one)
    (a N : Nat → I → V3 R)
    (hs : ∀ t r, Step K h ν (wt K kz r) (a (t + 1) r) (a t r) (N t r))
    (hneut : ∀ t, lsum K (fun r => dot3 K (N t r) (a t r)) l = K.zero)
    (hP : ∀ t, Le K O.toCone (lsum K (fun r => dot3 K (N t r) (N t r)) l)
      (lsum K (fun r => dot3 K (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (a t) (a t) r)
        (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (a t) (a t) r)) l))
    (hcfl : Le K O.toCone (beta K M ι h ν Es) (K.mul h ν))
    (h0 : Le K O.toCone (K.add (lsum K (fun r => dot3 K (a 0 r) (a 0 r)) l)
      (K.mul (beta K M ι h ν Es) (lsum K (fun r => K.mul (wt K kz r) (dot3 K (a 0 r) (a 0 r))) l))) Es) (n : Nat)
    (htime : Le K O.toCone
      (K.mul (K.add K.one K.one) (K.mul (K.mul (castN K 135) (K.mul ι ι))
        (K.add (lsum K (fun r => dot3 K (a 0 r) (a 0 r)) l)
          (K.mul (K.mul h ν) (lsum K (fun r => K.mul (wt K kz r) (dot3 K (a 0 r) (a 0 r))) l)))))
      (K.mul (K.mul ν ν) (K.mul (castN K (n + 1)) (K.mul h ν)))) :
    ∃ t, t < n + 1 ∧
      Le K O.toCone (K.mul (K.mul (castN K 135) (K.mul ι ι)) (Q K ν h (Zs K l kz a) (As K l kz a) (t + 1))) (K.mul ν (K.mul ν ν)) ∧
      ∀ s, Le K O.toCone (Q K ν h (Zs K l kz a) (As K l kz a) (t + 1 + s)) (Q K ν h (Zs K l kz a) (As K l kz a) (t + 1)) := by
  have hc : O.nn (K.mul (castN K 135) (K.mul ι ι)) := O.nn_mul _ _ (RatModel.nn_cast K O.toCone 135) (O.nn_sq ι)
  have nW : O.nn (K.add (lsum K (fun r => dot3 K (a 0 r) (a 0 r)) l)
      (K.mul (K.mul h ν) (lsum K (fun r => K.mul (wt K kz r) (dot3 K (a 0 r) (a 0 r))) l))) :=
    O.nn_add _ _ (lsum_nn K O.toCone _ (fun r => dot3_nn K O.toCone _) l)
      (O.nn_mul _ _ (O.nn_mul _ _ hh hν) (lsum_nn K O.toCone _ (fun r => O.nn_mul _ _ (dot3_nn K O.toCone _) (dot3_nn K O.toCone _)) l))
  -- ν + hν²c_M ≤ ν + ν
  have f1 : Le K O.toCone (K.add ν (K.mul (K.mul h (K.mul ν ν)) (cM K M))) (K.add ν ν) := by
    have m := le_mul_nn K O.toCone ν _ _ hν hstepM
    refine le_add' K O.toCone _ _ _ _ (le_refl' K O.toCone ν) (le_trans' K O.toCone _ _ _ ⟨K.zero, O.nn_zero, ?_⟩
      (le_trans' K O.toCone _ _ _ m ⟨K.zero, O.nn_zero, ?_⟩))
    · show K.add (K.mul (K.mul h (K.mul ν ν)) (cM K M)) K.zero = K.mul ν (K.mul (K.mul h ν) (cM K M))
      lib_ring
    · show K.add (K.mul ν K.one) K.zero = ν
      lib_ring
  have g1 := le_mul_nn K O.toCone (K.mul (castN K 135) (K.mul ι ι)) _ _ hc (ForceSob.le_mul_right' K O.toCone f1 nW)
  have g2 := le_mul_nn K O.toCone ν _ _ hν htime
  refine event_reg K O inv mul_inv h10 l kz hd h1 M hM ι h ν Es hh hν hh0 hν0 hEs a N hs hneut hP hcfl h0 n
    (le_trans' K O.toCone _ _ _ g1 (le_trans' K O.toCone _ _ _ ⟨K.zero, O.nn_zero, ?_⟩ (le_trans' K O.toCone _ _ _ g2 ⟨K.zero, O.nn_zero, ?_⟩)))
  · lib_ring
  · lib_ring

end Gen

end EventRegU

#print axioms EventRegU.event_reg_uniform

ns/EventSmall.lean

lean_all/ns/EventSmall.lean

/-
  ns/EventSmall.lean — T37 cycle 1317 (goal G789 — done): a good time with small enstrophy, for arbitrary data, over
  an ordered field (pigeonhole on the integrated enstrophy of CFLGlobal).
    argmin          every finite family Y_0 … Y_n has a smallest member (order totality)
    sum_ge          c ≤ Y_s for all s < n  ⇒  n·c ≤ Σ_{s<n} Y_s
    pigeon          Σ_{s<n} Y_s ≤ S  ⇒  ∃ t < n + 1 … with (n + 1)·Y_t ≤ S   (n + 1 terms)
    event_small     under the cfl_global hypotheses, for every n there is t ≤ n with
                    (n + 1)·hν·Z_{t+1} ≤ E_0 + hν·Z_0,
  a bound on the right with no dependence on the truncation (β ≤ hν): among the first n + 1 steps one has enstrophy
  at most (E_0 + hνZ_0)/((n + 1)hν) — the discrete form of Leray's "eventually small" enstrophy.

  Lean 4 core plus ns/CFLGlobal, lib/RingTac.
-/
import ns.CFLGlobal

namespace EventSmall
open DetChain NSChain DyadicSmallGlobal LibRing DyadicLarge FourierGalerkin GalerkinForce ForceBound ForceSob
  ModeCount CubeCount ForceUniform NormCompare EnstStep EnstGalerkin CFLStep CFLGlobal

section Gen
variable {R : Type} (K : CRing R) (O : OrdR K) {I : Type}
variable (inv : R → R) (mul_inv : ∀ y, y ≠ K.zero → K.mul y (inv y) = K.one) (h10 : K.one ≠ K.zero)

theorem lt_succ_cases {s n : Nat} (h : s < n + 1) : s < n ∨ s = n :=
  Nat.lt_or_eq_of_le (Nat.le_of_lt_succ h)

theorem argmin (Y : Nat → R) : ∀ n, ∃ t, t < n + 1 ∧ ∀ s, s < n + 1 → Le K O.toCone (Y t) (Y s)
  | 0 => ⟨0, Nat.lt_succ_self 0, fun s hs => by
      cases lt_succ_cases hs with
      | inl h => exact absurd h (Nat.not_lt_zero s)
      | inr h => rw [h]; exact le_refl' K O.toCone _⟩
  | n + 1 => by
    have ⟨t, ht, hm⟩ := argmin Y n
    cases O.total (Y t) (Y (n + 1)) with
    | inl hle =>
      refine ⟨t, Nat.lt_succ_of_lt ht, fun s hs => ?_⟩
      cases lt_succ_cases hs with
      | inl h => exact hm s h
      | inr h => rw [h]; exact hle
    | inr hge =>
      refine ⟨n + 1, Nat.lt_succ_self (n + 1), fun s hs => ?_⟩
      cases lt_succ_cases hs with
      | inl h => exact le_trans' K O.toCone _ _ _ hge (hm s h)
      | inr h => rw [h]; exact le_refl' K O.toCone _

theorem sum_ge (Y : Nat → R) (c : R) : ∀ n, (∀ s, s < n → Le K O.toCone c (Y s)) →
    Le K O.toCone (K.mul (castN K n) c) (sumN K Y n)
  | 0, _ => ⟨K.zero, O.nn_zero, by show K.add (K.mul K.zero c) K.zero = K.zero; lib_ring⟩
  | n + 1, h => by
    have ih := sum_ge Y c n (fun s hs => h s (Nat.lt_succ_of_lt hs))
    have s := le_add' K O.toCone _ _ _ _ ih (h n (Nat.lt_succ_self n))
    refine le_trans' K O.toCone _ _ _ ⟨K.zero, O.nn_zero, ?_⟩ s
    show K.add (K.mul (K.add K.one (castN K n)) c) K.zero = K.add (K.mul (castN K n) c) c
    lib_ring

theorem pigeon (Y : Nat → R) (S : R) (n : Nat) (h : Le K O.toCone (sumN K Y (n + 1)) S) :
    ∃ t, t < n + 1 ∧ Le K O.toCone (K.mul (castN K (n + 1)) (Y t)) S := by
  have ⟨t, ht, hm⟩ := argmin K O Y n
  exact ⟨t, ht, le_trans' K O.toCone _ _ _ (sum_ge K O Y (Y t) (n + 1) hm) h⟩

include mul_inv h10 in
theorem event_small (l : List I) (kz : I → Mode) (hd : List.Pairwise (fun a b => kz a ≠ kz b) l)
    (h1 : ∀ p, 1 ≤ nrm (kz p)) (M : Nat) (hM : ∀ p, p ∈ l → nrm (kz p) ≤ M + 1)
    (ι h ν Es : R) (hh : O.nn h) (hν : O.nn ν) (hEs : O.nn Es) (a N : Nat → I → V3 R)
    (hs : ∀ t r, Step K h ν (wt K kz r) (a (t + 1) r) (a t r) (N t r))
    (hneut : ∀ t, lsum K (fun r => dot3 K (N t r) (a t r)) l = K.zero)
    (hP : ∀ t, Le K O.toCone (lsum K (fun r => dot3 K (N t r) (N t r)) l)
      (lsum K (fun r => dot3 K (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (a t) (a t) r)
        (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (a t) (a t) r)) l))
    (hcfl : Le K O.toCone (beta K M ι h ν Es) (K.mul h ν))
    (h0 : Le K O.toCone (K.add (lsum K (fun r => dot3 K (a 0 r) (a 0 r)) l)
      (K.mul (beta K M ι h ν Es) (lsum K (fun r => K.mul (wt K kz r) (dot3 K (a 0 r) (a 0 r))) l))) Es) (n : Nat) :
    ∃ t, t < n + 1 ∧ Le K O.toCone
      (K.mul (castN K (n + 1)) (K.mul (K.mul h ν) (lsum K (fun r => K.mul (wt K kz r) (dot3 K (a (t + 1) r) (a (t + 1) r))) l)))
      (K.add (lsum K (fun r => dot3 K (a 0 r) (a 0 r)) l)
        (K.mul (K.mul h ν) (lsum K (fun r => K.mul (wt K kz r) (dot3 K (a 0 r) (a 0 r))) l))) := by
  have hw : ∀ r, O.nn (wt K kz r) := fun r => dot3_nn K O.toCone _
  have nZ : ∀ t, O.nn (lsum K (fun r => K.mul (wt K kz r) (dot3 K (a t r) (a t r))) l) :=
    fun t => lsum_nn K O.toCone _ (fun r => O.nn_mul _ _ (hw r) (dot3_nn K O.toCone _)) l
  have nE : ∀ t, O.nn (lsum K (fun r => dot3 K (a t r) (a t r)) l) :=
    fun t => lsum_nn K O.toCone _ (fun r => dot3_nn K O.toCone _) l
  have nβ : O.nn (beta K M ι h ν Es) := by
    have three : O.nn (K.add (K.add K.one K.one) K.one) := O.nn_add _ _ (O.nn_add _ _ (RatModel.nn_one' K O.toCone) (RatModel.nn_one' K O.toCone)) (RatModel.nn_one' K O.toCone)
    have two : O.nn (K.add K.one K.one) := O.nn_add _ _ (RatModel.nn_one' K O.toCone) (RatModel.nn_one' K O.toCone)
    have nc := cM_nn K O M
    exact O.nn_mul _ _ (O.nn_sq h) (O.nn_add _ _ (O.nn_mul _ _ three (O.nn_mul _ _ (O.nn_sq ι) (O.nn_mul _ _ (RatModel.nn_cast K O.toCone 135) (O.nn_mul _ _ (O.nn_sq _) hEs))))
      (O.nn_mul _ _ two (O.nn_mul _ _ (O.nn_sq ν) nc)))
  have g := cfl_global K O inv mul_inv h10 l kz hd h1 M hM ι h ν Es hh hν hEs a N hs hneut hP hcfl h0 (n + 1)
  -- hν·Σ_{t ≤ n} Z_{t+1} ≤ V_0 ≤ E_0 + hν·Z_0
  have e1 : Le K O.toCone (K.mul (K.mul h ν) (sumN K (fun t => lsum K (fun r => K.mul (wt K kz r) (dot3 K (a (t + 1) r) (a (t + 1) r))) l) (n + 1)))
      (K.add (K.add (lsum K (fun r => dot3 K (a (n + 1) r) (a (n + 1) r)) l)
        (K.mul (K.mul h ν) (sumN K (fun t => lsum K (fun r => K.mul (wt K kz r) (dot3 K (a (t + 1) r) (a (t + 1) r))) l) (n + 1))))
        (K.mul (beta K M ι h ν Es) (lsum K (fun r => K.mul (wt K kz r) (dot3 K (a (n + 1) r) (a (n + 1) r))) l))) :=
    ⟨K.add (lsum K (fun r => dot3 K (a (n + 1) r) (a (n + 1) r)) l) (K.mul (beta K M ι h ν Es) (lsum K (fun r => K.mul (wt K kz r) (dot3 K (a (n + 1) r) (a (n + 1) r))) l)),
      O.nn_add _ _ (nE (n + 1)) (O.nn_mul _ _ nβ (nZ (n + 1))), by lib_ring⟩
  have e2 := le_add' K O.toCone _ _ _ _ (le_refl' K O.toCone (lsum K (fun r => dot3 K (a 0 r) (a 0 r)) l))
    (le_mul_right' K O.toCone hcfl (nZ 0))
  have tot := le_trans' K O.toCone _ _ _ e1 (le_trans' K O.toCone _ _ _ g e2)
  have sm : sumN K (fun t => K.mul (K.mul h ν) (lsum K (fun r => K.mul (wt K kz r) (dot3 K (a (t + 1) r) (a (t + 1) r))) l)) (n + 1)
      = K.mul (K.mul h ν) (sumN K (fun t => lsum K (fun r => K.mul (wt K kz r) (dot3 K (a (t + 1) r) (a (t + 1) r))) l) (n + 1)) :=
    NSChain.sumN_mul_left K _ _ (n + 1)
  rw [← sm] at tot
  exact pigeon K O _ _ n tot

end Gen

end EventSmall

#print axioms EventSmall.lt_succ_cases
#print axioms EventSmall.argmin
#print axioms EventSmall.sum_ge
#print axioms EventSmall.pigeon
#print axioms EventSmall.event_small

ns/FDk.lean

lean_all/ns/FDk.lean

/-
  ns/FDk.lean — T37 cycle 1129 (goal G717e, piece 3): the force difference inside the step-size defect is O(k).
  With b₁ the k-step from a₀ (force N₀), N₁ the force at b₁, d = b₁ − a₀, over an ordered field (0 ≤ k ≤ 1):
    lip_kQ     2k²G + 2k²ν²B₅ ≤ k(2G + 2Q)   when kν²B₅ ≤ Q
    fd_scheme  Σ w|N₀ − N₁|² ≤ k·(2ι²(2G₃ + 2Q)P₀ + 2ι²E(2s₁(2G₁ + 2ν²B₃) + 2s₀(2G₂ + 2ν²B₄)))
  given the projection bound Σw|N₀ − N₁|² ≤ Σw|N(b₁) − N(a₀)|², Σw^j|N₀|² ≤ G_j (j = 1, 2, 3), Σw³|a₀|² = B₃,
  Σw⁴|a₀|² = B₄, kν²Σw⁵|a₀|² ≤ Q, 2S₁Z(b₁) + 2S₀A(b₁) ≤ P₀, B(a₀) ≤ E, S₁ ≤ s₁, S₀ ≤ s₀
    fd_scheme'  the same with k·Σw³|N₀|² ≤ G₃ (uniform in the step)
  (ForceDiffW + TimeLipW with V = w³, w, w² + DefectOrder.fd_k).

  Lean 4 core plus ns/ForceDiffW, ns/TimeLipW, ns/DefectOrder.
-/
import ns.ForceDiffW
import ns.TimeLipW
import ns.DefectOrder

namespace FDk
open DetChain NSChain DyadicSmallGlobal LibRing DyadicLarge FourierGalerkin GalerkinForce ForceBound ForceSob
  ModeCount CubeCount ForceUniform NormCompare EnstGalerkin ForceTwoScale ForceSobW CastZAdd WeightH2 LatticeSum
  LatticeBound ForceH2 ForceH2Bi ForceDiff EnstDiff ForceDiffW EnstStep TimeLipW DefectOrder

section Gen
variable {R : Type} (K : CRing R) (O : OrdR K) {I : Type}
variable (inv : R → R) (mul_inv : ∀ y, y ≠ K.zero → K.mul y (inv y) = K.one) (h10 : K.one ≠ K.zero)

theorem lip_kQ (k ν G B5 Q : R) (hk : O.nn k) (hk1 : Le K O.toCone k K.one) (hG : O.nn G)
    (hQ : Le K O.toCone (K.mul (K.mul k (K.mul ν ν)) B5) Q) :
    Le K O.toCone (K.add (K.mul (K.mul (K.add K.one K.one) (K.mul k k)) G)
        (K.mul (K.mul (K.add K.one K.one) (K.mul (K.mul k k) (K.mul ν ν))) B5))
      (K.mul k (K.add (K.mul (K.add K.one K.one) G) (K.mul (K.add K.one K.one) Q))) := by
  have two : O.nn (K.add K.one K.one) := O.nn_add _ _ (RatModel.nn_one' K O.toCone) (RatModel.nn_one' K O.toCone)
  have a := k2_le K O k (K.mul (K.add K.one K.one) G) hk hk1 (O.nn_mul _ _ two hG)
  have b := le_mul_nn K O.toCone (K.mul (K.add K.one K.one) k) _ _ (O.nn_mul _ _ two hk) hQ
  refine le_trans' K O.toCone _ _ _ ⟨K.zero, O.nn_zero, ?_⟩ (le_trans' K O.toCone _ _ _ (le_add' K O.toCone _ _ _ _ a b) ⟨K.zero, O.nn_zero, ?_⟩)
  · lib_ring
  · lib_ring

theorem lip_kQ' (k ν X G B5 Q : R) (hk : O.nn k) (hX : Le K O.toCone (K.mul k X) G)
    (hQ : Le K O.toCone (K.mul (K.mul k (K.mul ν ν)) B5) Q) :
    Le K O.toCone (K.add (K.mul (K.mul (K.add K.one K.one) (K.mul k k)) X)
        (K.mul (K.mul (K.add K.one K.one) (K.mul (K.mul k k) (K.mul ν ν))) B5))
      (K.mul k (K.add (K.mul (K.add K.one K.one) G) (K.mul (K.add K.one K.one) Q))) := by
  have two : O.nn (K.add K.one K.one) := O.nn_add _ _ (RatModel.nn_one' K O.toCone) (RatModel.nn_one' K O.toCone)
  have a := le_mul_nn K O.toCone (K.mul (K.add K.one K.one) k) _ _ (O.nn_mul _ _ two hk) hX
  have b := le_mul_nn K O.toCone (K.mul (K.add K.one K.one) k) _ _ (O.nn_mul _ _ two hk) hQ
  refine le_trans' K O.toCone _ _ _ ⟨K.zero, O.nn_zero, ?_⟩ (le_trans' K O.toCone _ _ _ (le_add' K O.toCone _ _ _ _ a b) ⟨K.zero, O.nn_zero, ?_⟩)
  · lib_ring
  · lib_ring

include mul_inv h10 in
theorem fd_scheme (l : List I) (kz : I → Mode) (hd : List.Pairwise (fun a b => kz a ≠ kz b) l)
    (h1 : ∀ p, 1 ≤ nrm (kz p)) (ι k ν G1 G2 G3 B3 B4 Q P0 E s1 s0 : R) (hk : O.nn k) (hk1 : Le K O.toCone k K.one)
    (hν : O.nn ν) (nG1 : O.nn G1) (nG2 : O.nn G2) (nG3 : O.nn G3) (nB3 : O.nn B3) (nB4 : O.nn B4)
    (nP0 : O.nn P0) (ns1 : O.nn s1) (ns0 : O.nn s0) (a0 b1 N0 N1 : I → V3 R)
    (s2 : ∀ r, Step K k ν (wt K kz r) (b1 r) (a0 r) (N0 r))
    (hproj : Le K O.toCone (lsum K (fun r => K.mul (wt K kz r) (dot3 K (sub3 K (N0 r) (N1 r)) (sub3 K (N0 r) (N1 r)))) l)
      (lsum K (fun r => K.mul (wt K kz r) (dot3 K (sub3 K (Nl K l (TriadSparse.delta K kz) ι (kv K kz) b1 b1 r) (Nl K l (TriadSparse.delta K kz) ι (kv K kz) a0 a0 r))
        (sub3 K (Nl K l (TriadSparse.delta K kz) ι (kv K kz) b1 b1 r) (Nl K l (TriadSparse.delta K kz) ι (kv K kz) a0 a0 r)))) l))
    (hG1 : Le K O.toCone (lsum K (fun r => K.mul (wt K kz r) (dot3 K (N0 r) (N0 r))) l) G1)
    (hG2 : Le K O.toCone (lsum K (fun r => K.mul (K.mul (wt K kz r) (wt K kz r)) (dot3 K (N0 r) (N0 r))) l) G2)
    (hG3 : Le K O.toCone (lsum K (fun r => K.mul (K.mul (K.mul (wt K kz r) (wt K kz r)) (wt K kz r)) (dot3 K (N0 r) (N0 r))) l) G3)
    (hB3 : Le K O.toCone (lsum K (fun r => K.mul (K.mul (wt K kz r) (K.mul (wt K kz r) (wt K kz r))) (dot3 K (a0 r) (a0 r))) l) B3)
    (hB4 : Le K O.toCone (lsum K (fun r => K.mul (K.mul (K.mul (wt K kz r) (wt K kz r)) (K.mul (wt K kz r) (wt K kz r))) (dot3 K (a0 r) (a0 r))) l) B4)
    (hQ : Le K O.toCone (K.mul (K.mul k (K.mul ν ν))
      (lsum K (fun r => K.mul (K.mul (K.mul (K.mul (wt K kz r) (wt K kz r)) (wt K kz r)) (K.mul (wt K kz r) (wt K kz r))) (dot3 K (a0 r) (a0 r))) l)) Q)
    (hP : Le K O.toCone (K.add (K.mul (K.add K.one K.one) (K.mul (S1w K inv kz l) (Zw K kz l b1)))
      (K.mul (K.add K.one K.one) (K.mul (S0w K inv kz l) (Aw K kz l b1)))) P0)
    (hBb : Le K O.toCone (Bw K kz l a0) E) (hS1 : Le K O.toCone (S1w K inv kz l) s1) (hS0 : Le K O.toCone (S0w K inv kz l) s0) :
    Le K O.toCone (lsum K (fun r => K.mul (wt K kz r) (dot3 K (sub3 K (N0 r) (N1 r)) (sub3 K (N0 r) (N1 r)))) l)
      (K.mul k (K.add (K.mul (K.add K.one K.one) (K.mul (K.mul ι ι) (K.mul (K.add (K.mul (K.add K.one K.one) G3) (K.mul (K.add K.one K.one) Q)) P0)))
        (K.mul (K.add K.one K.one) (K.mul (K.mul ι ι) (K.mul E
          (K.add (K.mul (K.add K.one K.one) (K.mul s1 (K.add (K.mul (K.add K.one K.one) G1) (K.mul (K.add K.one K.one) (K.mul (K.mul ν ν) B3)))))
            (K.mul (K.add K.one K.one) (K.mul s0 (K.add (K.mul (K.add K.one K.one) G2) (K.mul (K.add K.one K.one) (K.mul (K.mul ν ν) B4))))))))))) := by
  have hw : ∀ r, O.nn (wt K kz r) := fun r => dot3_nn K O.toCone _
  have two : O.nn (K.add K.one K.one) := O.nn_add _ _ (RatModel.nn_one' K O.toCone) (RatModel.nn_one' K O.toCone)
  have fdw := force_diffW K O inv mul_inv h10 l kz hd h1 ι b1 a0
  -- the three TimeLipW bounds for d = b₁ − a₀
  have tB := time_lipW K O.toCone l k ν hk hν (wt K kz) (fun r => K.mul (K.mul (wt K kz r) (wt K kz r)) (wt K kz r)) hw
    (fun r => O.nn_mul _ _ (O.nn_sq _) (hw r)) b1 a0 N0 s2
  have tZ := time_lipW K O.toCone l k ν hk hν (wt K kz) (wt K kz) hw hw b1 a0 N0 s2
  have tA := time_lipW K O.toCone l k ν hk hν (wt K kz) (fun r => K.mul (wt K kz r) (wt K kz r)) hw (fun r => O.nn_sq _) b1 a0 N0 s2
  have nB5 : O.nn (lsum K (fun r => K.mul (K.mul (K.mul (K.mul (wt K kz r) (wt K kz r)) (wt K kz r)) (K.mul (wt K kz r) (wt K kz r))) (dot3 K (a0 r) (a0 r))) l) :=
    lsum_nn K O.toCone _ (fun r => O.nn_mul _ _ (O.nn_mul _ _ (O.nn_mul _ _ (O.nn_sq _) (hw r)) (O.nn_sq _)) (dot3_nn K O.toCone _)) l
  have nZN : O.nn (lsum K (fun r => K.mul (wt K kz r) (dot3 K (N0 r) (N0 r))) l) := lsum_nn K O.toCone _ (fun r => O.nn_mul _ _ (hw r) (dot3_nn K O.toCone _)) l
  have nAN : O.nn (lsum K (fun r => K.mul (K.mul (wt K kz r) (wt K kz r)) (dot3 K (N0 r) (N0 r))) l) := lsum_nn K O.toCone _ (fun r => O.nn_mul _ _ (O.nn_sq _) (dot3_nn K O.toCone _)) l
  have nBN : O.nn (lsum K (fun r => K.mul (K.mul (K.mul (wt K kz r) (wt K kz r)) (wt K kz r)) (dot3 K (N0 r) (N0 r))) l) := lsum_nn K O.toCone _ (fun r => O.nn_mul _ _ (O.nn_mul _ _ (O.nn_sq _) (hw r)) (dot3_nn K O.toCone _)) l
  -- B(d) ≤ k(2G₃ + 2Q)
  have hBd : Le K O.toCone (Bw K kz l (dF K b1 a0)) (K.mul k (K.add (K.mul (K.add K.one K.one) G3) (K.mul (K.add K.one K.one) Q))) := by
    have m := le_add' K O.toCone _ _ _ _ (le_mul_nn K O.toCone _ _ _ (O.nn_mul _ _ two (O.nn_sq k)) hG3) (le_refl' K O.toCone
      (K.mul (K.mul (K.add K.one K.one) (K.mul (K.mul k k) (K.mul ν ν)))
        (lsum K (fun r => K.mul (K.mul (K.mul (K.mul (wt K kz r) (wt K kz r)) (wt K kz r)) (K.mul (wt K kz r) (wt K kz r))) (dot3 K (a0 r) (a0 r))) l)))
    exact le_trans' K O.toCone _ _ _ tB (le_trans' K O.toCone _ _ _ m (lip_kQ K O k ν G3 _ Q hk hk1 nG3 hQ))
  -- Z(d) ≤ k(2G₁ + 2ν²B₃),  A(d) ≤ k(2G₂ + 2ν²B₄)
  have hZd : Le K O.toCone (Zw K kz l (dF K b1 a0)) (K.mul k (K.add (K.mul (K.add K.one K.one) G1) (K.mul (K.add K.one K.one) (K.mul (K.mul ν ν) B3)))) := by
    have m := le_add' K O.toCone _ _ _ _ (le_mul_nn K O.toCone _ _ _ (O.nn_mul _ _ two (O.nn_sq k)) hG1)
      (le_mul_nn K O.toCone _ _ _ (O.nn_mul _ _ two (O.nn_mul _ _ (O.nn_sq k) (O.nn_sq ν))) hB3)
    exact le_trans' K O.toCone _ _ _ tZ (le_trans' K O.toCone _ _ _ m (lip_k K O k ν G1 B3 hk hk1 nG1 nB3))
  have hAd : Le K O.toCone (Aw K kz l (dF K b1 a0)) (K.mul k (K.add (K.mul (K.add K.one K.one) G2) (K.mul (K.add K.one K.one) (K.mul (K.mul ν ν) B4)))) := by
    have m := le_add' K O.toCone _ _ _ _ (le_mul_nn K O.toCone _ _ _ (O.nn_mul _ _ two (O.nn_sq k)) hG2)
      (le_mul_nn K O.toCone _ _ _ (O.nn_mul _ _ two (O.nn_mul _ _ (O.nn_sq k) (O.nn_sq ν))) hB4)
    exact le_trans' K O.toCone _ _ _ tA (le_trans' K O.toCone _ _ _ m (lip_k K O k ν G2 B4 hk hk1 nG2 nB4))
  have nBd : O.nn (Bw K kz l (dF K b1 a0)) := lsum_nn K O.toCone _ (fun r => O.nn_mul _ _ (O.nn_mul _ _ (O.nn_sq _) (hw r)) (dot3_nn K O.toCone _)) l
  have nBb : O.nn (Bw K kz l a0) := lsum_nn K O.toCone _ (fun r => O.nn_mul _ _ (O.nn_mul _ _ (O.nn_sq _) (hw r)) (dot3_nn K O.toCone _)) l
  have nS1 : O.nn (S1w K inv kz l) := lsum_nn K O.toCone _ (fun p => O.nn_mul _ _ (O.nn_sq _) (hw p)) l
  have nS0 : O.nn (S0w K inv kz l) := lsum_nn K O.toCone _ (fun p => O.nn_sq _) l
  have fk := fd_k K O k ι _ _ _ _ _ _ _ _ _ _ _ P0 E s1 s0 hk nBd nBb nS1 nS0 nP0
    (O.nn_add _ _ (O.nn_mul _ _ two nG1) (O.nn_mul _ _ two (O.nn_mul _ _ (O.nn_sq ν) nB3)))
    (O.nn_add _ _ (O.nn_mul _ _ two nG2) (O.nn_mul _ _ two (O.nn_mul _ _ (O.nn_sq ν) nB4))) ns1 ns0
    fdw hBd hZd hAd hP hBb hS1 hS0
  exact le_trans' K O.toCone _ _ _ hproj fk

include mul_inv h10 in
theorem fd_scheme' (l : List I) (kz : I → Mode) (hd : List.Pairwise (fun a b => kz a ≠ kz b) l)
    (h1 : ∀ p, 1 ≤ nrm (kz p)) (ι k ν G1 G2 G3 B3 B4 Q P0 E s1 s0 : R) (hk : O.nn k) (hk1 : Le K O.toCone k K.one)
    (hν : O.nn ν) (nG1 : O.nn G1) (nG2 : O.nn G2) (nB3 : O.nn B3) (nB4 : O.nn B4)
    (nP0 : O.nn P0) (ns1 : O.nn s1) (ns0 : O.nn s0) (a0 b1 N0 N1 : I → V3 R)
    (s2 : ∀ r, Step K k ν (wt K kz r) (b1 r) (a0 r) (N0 r))
    (hproj : Le K O.toCone (lsum K (fun r => K.mul (wt K kz r) (dot3 K (sub3 K (N0 r) (N1 r)) (sub3 K (N0 r) (N1 r)))) l)
      (lsum K (fun r => K.mul (wt K kz r) (dot3 K (sub3 K (Nl K l (TriadSparse.delta K kz) ι (kv K kz) b1 b1 r) (Nl K l (TriadSparse.delta K kz) ι (kv K kz) a0 a0 r))
        (sub3 K (Nl K l (TriadSparse.delta K kz) ι (kv K kz) b1 b1 r) (Nl K l (TriadSparse.delta K kz) ι (kv K kz) a0 a0 r)))) l))
    (hG1 : Le K O.toCone (lsum K (fun r => K.mul (wt K kz r) (dot3 K (N0 r) (N0 r))) l) G1)
    (hG2 : Le K O.toCone (lsum K (fun r => K.mul (K.mul (wt K kz r) (wt K kz r)) (dot3 K (N0 r) (N0 r))) l) G2)
    (hG3 : Le K O.toCone (K.mul k (lsum K (fun r => K.mul (K.mul (K.mul (wt K kz r) (wt K kz r)) (wt K kz r)) (dot3 K (N0 r) (N0 r))) l)) G3)
    (hB3 : Le K O.toCone (lsum K (fun r => K.mul (K.mul (wt K kz r) (K.mul (wt K kz r) (wt K kz r))) (dot3 K (a0 r) (a0 r))) l) B3)
    (hB4 : Le K O.toCone (lsum K (fun r => K.mul (K.mul (K.mul (wt K kz r) (wt K kz r)) (K.mul (wt K kz r) (wt K kz r))) (dot3 K (a0 r) (a0 r))) l) B4)
    (hQ : Le K O.toCone (K.mul (K.mul k (K.mul ν ν))
      (lsum K (fun r => K.mul (K.mul (K.mul (K.mul (wt K kz r) (wt K kz r)) (wt K kz r)) (K.mul (wt K kz r) (wt K kz r))) (dot3 K (a0 r) (a0 r))) l)) Q)
    (hP : Le K O.toCone (K.add (K.mul (K.add K.one K.one) (K.mul (S1w K inv kz l) (Zw K kz l b1)))
      (K.mul (K.add K.one K.one) (K.mul (S0w K inv kz l) (Aw K kz l b1)))) P0)
    (hBb : Le K O.toCone (Bw K kz l a0) E) (hS1 : Le K O.toCone (S1w K inv kz l) s1) (hS0 : Le K O.toCone (S0w K inv kz l) s0) :
    Le K O.toCone (lsum K (fun r => K.mul (wt K kz r) (dot3 K (sub3 K (N0 r) (N1 r)) (sub3 K (N0 r) (N1 r)))) l)
      (K.mul k (K.add (K.mul (K.add K.one K.one) (K.mul (K.mul ι ι) (K.mul (K.add (K.mul (K.add K.one K.one) G3) (K.mul (K.add K.one K.one) Q)) P0)))
        (K.mul (K.add K.one K.one) (K.mul (K.mul ι ι) (K.mul E
          (K.add (K.mul (K.add K.one K.one) (K.mul s1 (K.add (K.mul (K.add K.one K.one) G1) (K.mul (K.add K.one K.one) (K.mul (K.mul ν ν) B3)))))
            (K.mul (K.add K.one K.one) (K.mul s0 (K.add (K.mul (K.add K.one K.one) G2) (K.mul (K.add K.one K.one) (K.mul (K.mul ν ν) B4))))))))))) := by
  have hw : ∀ r, O.nn (wt K kz r) := fun r => dot3_nn K O.toCone _
  have two : O.nn (K.add K.one K.one) := O.nn_add _ _ (RatModel.nn_one' K O.toCone) (RatModel.nn_one' K O.toCone)
  have fdw := force_diffW K O inv mul_inv h10 l kz hd h1 ι b1 a0
  -- the three TimeLipW bounds for d = b₁ − a₀
  have tB := time_lipW K O.toCone l k ν hk hν (wt K kz) (fun r => K.mul (K.mul (wt K kz r) (wt K kz r)) (wt K kz r)) hw
    (fun r => O.nn_mul _ _ (O.nn_sq _) (hw r)) b1 a0 N0 s2
  have tZ := time_lipW K O.toCone l k ν hk hν (wt K kz) (wt K kz) hw hw b1 a0 N0 s2
  have tA := time_lipW K O.toCone l k ν hk hν (wt K kz) (fun r => K.mul (wt K kz r) (wt K kz r)) hw (fun r => O.nn_sq _) b1 a0 N0 s2
  have nB5 : O.nn (lsum K (fun r => K.mul (K.mul (K.mul (K.mul (wt K kz r) (wt K kz r)) (wt K kz r)) (K.mul (wt K kz r) (wt K kz r))) (dot3 K (a0 r) (a0 r))) l) :=
    lsum_nn K O.toCone _ (fun r => O.nn_mul _ _ (O.nn_mul _ _ (O.nn_mul _ _ (O.nn_sq _) (hw r)) (O.nn_sq _)) (dot3_nn K O.toCone _)) l
  have nZN : O.nn (lsum K (fun r => K.mul (wt K kz r) (dot3 K (N0 r) (N0 r))) l) := lsum_nn K O.toCone _ (fun r => O.nn_mul _ _ (hw r) (dot3_nn K O.toCone _)) l
  have nAN : O.nn (lsum K (fun r => K.mul (K.mul (wt K kz r) (wt K kz r)) (dot3 K (N0 r) (N0 r))) l) := lsum_nn K O.toCone _ (fun r => O.nn_mul _ _ (O.nn_sq _) (dot3_nn K O.toCone _)) l
  have nBN : O.nn (lsum K (fun r => K.mul (K.mul (K.mul (wt K kz r) (wt K kz r)) (wt K kz r)) (dot3 K (N0 r) (N0 r))) l) := lsum_nn K O.toCone _ (fun r => O.nn_mul _ _ (O.nn_mul _ _ (O.nn_sq _) (hw r)) (dot3_nn K O.toCone _)) l
  -- B(d) ≤ k(2G₃ + 2Q)
  have hBd : Le K O.toCone (Bw K kz l (dF K b1 a0)) (K.mul k (K.add (K.mul (K.add K.one K.one) G3) (K.mul (K.add K.one K.one) Q))) :=
    le_trans' K O.toCone _ _ _ tB (lip_kQ' K O k ν _ G3 _ Q hk hG3 hQ)
  -- Z(d) ≤ k(2G₁ + 2ν²B₃),  A(d) ≤ k(2G₂ + 2ν²B₄)
  have hZd : Le K O.toCone (Zw K kz l (dF K b1 a0)) (K.mul k (K.add (K.mul (K.add K.one K.one) G1) (K.mul (K.add K.one K.one) (K.mul (K.mul ν ν) B3)))) := by
    have m := le_add' K O.toCone _ _ _ _ (le_mul_nn K O.toCone _ _ _ (O.nn_mul _ _ two (O.nn_sq k)) hG1)
      (le_mul_nn K O.toCone _ _ _ (O.nn_mul _ _ two (O.nn_mul _ _ (O.nn_sq k) (O.nn_sq ν))) hB3)
    exact le_trans' K O.toCone _ _ _ tZ (le_trans' K O.toCone _ _ _ m (lip_k K O k ν G1 B3 hk hk1 nG1 nB3))
  have hAd : Le K O.toCone (Aw K kz l (dF K b1 a0)) (K.mul k (K.add (K.mul (K.add K.one K.one) G2) (K.mul (K.add K.one K.one) (K.mul (K.mul ν ν) B4)))) := by
    have m := le_add' K O.toCone _ _ _ _ (le_mul_nn K O.toCone _ _ _ (O.nn_mul _ _ two (O.nn_sq k)) hG2)
      (le_mul_nn K O.toCone _ _ _ (O.nn_mul _ _ two (O.nn_mul _ _ (O.nn_sq k) (O.nn_sq ν))) hB4)
    exact le_trans' K O.toCone _ _ _ tA (le_trans' K O.toCone _ _ _ m (lip_k K O k ν G2 B4 hk hk1 nG2 nB4))
  have nBd : O.nn (Bw K kz l (dF K b1 a0)) := lsum_nn K O.toCone _ (fun r => O.nn_mul _ _ (O.nn_mul _ _ (O.nn_sq _) (hw r)) (dot3_nn K O.toCone _)) l
  have nBb : O.nn (Bw K kz l a0) := lsum_nn K O.toCone _ (fun r => O.nn_mul _ _ (O.nn_mul _ _ (O.nn_sq _) (hw r)) (dot3_nn K O.toCone _)) l
  have nS1 : O.nn (S1w K inv kz l) := lsum_nn K O.toCone _ (fun p => O.nn_mul _ _ (O.nn_sq _) (hw p)) l
  have nS0 : O.nn (S0w K inv kz l) := lsum_nn K O.toCone _ (fun p => O.nn_sq _) l
  have fk := fd_k K O k ι _ _ _ _ _ _ _ _ _ _ _ P0 E s1 s0 hk nBd nBb nS1 nS0 nP0
    (O.nn_add _ _ (O.nn_mul _ _ two nG1) (O.nn_mul _ _ two (O.nn_mul _ _ (O.nn_sq ν) nB3)))
    (O.nn_add _ _ (O.nn_mul _ _ two nG2) (O.nn_mul _ _ two (O.nn_mul _ _ (O.nn_sq ν) nB4))) ns1 ns0
    fdw hBd hZd hAd hP hBb hS1 hS0
  exact le_trans' K O.toCone _ _ _ hproj fk

end Gen

end FDk

#print axioms FDk.lip_kQ
#print axioms FDk.fd_scheme
#print axioms FDk.lip_kQ'
#print axioms FDk.fd_scheme'

ns/FiniteAA.lean

lean_all/ns/FiniteAA.lean

/-
  ns/FiniteAA.lean — T37 cycle 1750 (goal G852f): a finite Arzelà–Ascoli statement for the scheme's time
  interpolants. Truncations M ∈ S give scalar runs a_M (one coordinate) with the grid Hölder bound
  |a_M(k + j) − a_M(k)|² ≤ j·G, quantised at grid times k < d: L_k(b k M) ≤ a_M(k) ≤ L_k(b k M) + δ with box
  index b k M < N. If |S| ≥ m·N^d + 1, then m + 1 truncations of S are pairwise uniformly close on [0, d):
      (u_M(k + θ) − u_M'(k + θ))² ≤ 3(θG + δ² + θG)    for all k < d, 0 ≤ θ ≤ 1.
  Lean 4 core, hand proofs, no axioms (NestExtract, BoxClose, CloseInterp).
-/
import ns.NestExtract
import ns.BoxClose
import ns.CloseInterp

namespace FiniteAA
open DetChain NSChain LibRing InterpStep InterpCross InterpGrid

section Gen
variable {R : Type} (K : CRing R) (P : Cone K)

theorem finite_aa (N m d : Nat) (S : List Nat) (hS : m * N ^ d + 1 ≤ S.length)
    (a : Nat → Nat → R) (G δ : R) (Lb : Nat → Nat → R) (b : Nat → Nat → Nat)
    (hb : ∀ k M, M ∈ S → b k M < N)
    (hlo : ∀ k M, M ∈ S → Le K P (Lb k (b k M)) (a M k))
    (hhi : ∀ k M, M ∈ S → Le K P (a M k) (K.add (Lb k (b k M)) δ))
    (hH : ∀ M, M ∈ S → ∀ k j, Le K P (K.mul (sub K (a M (k + j)) (a M k)) (sub K (a M (k + j)) (a M k))) (nm K j G)) :
    ∃ S' : List Nat, (∀ M, M ∈ S' → M ∈ S) ∧ m + 1 ≤ S'.length ∧
      ∀ M M', M ∈ S' → M' ∈ S' → ∀ k, k < d → ∀ θ e : R, P.nn θ → P.nn e → K.add θ e = K.one →
        Le K P (K.mul (sub K (interp K (a M) k θ) (interp K (a M') k θ)) (sub K (interp K (a M) k θ) (interp K (a M') k θ)))
          (K.mul (three K) (K.add (K.mul θ G) (K.add (K.mul δ δ) (K.mul θ G)))) := by
  cases NestExtract.extract N m b d S (fun k M hM => hb k M hM) hS with
  | intro S' h =>
    refine ⟨S', h.1, h.2.1, ?_⟩
    intro M M' hM hM' k hk θ e hθ he hθe
    have hMS := h.1 M hM
    have hM'S := h.1 M' hM'
    have eb : b k M = b k M' := h.2.2 M M' hM hM' k hk
    have hD := BoxClose.box_close K P (Lb k (b k M)) δ (a M k) (a M' k) (hlo k M hMS) (hhi k M hMS)
      (by rw [eb]; exact hlo k M' hM'S) (by rw [eb]; exact hhi k M' hM'S)
    exact CloseInterp.close_interp K P (a M) (a M') G _ (hH M hMS) (hH M' hM'S) k hD θ e hθ he hθe

end Gen
end FiniteAA

#print axioms FiniteAA.finite_aa

ns/Flatten3.lean

lean_all/ns/Flatten3.lean

/-
  ns/Flatten3.lean — T37 cycle 881 (goal G594): flattening 3D mode arrays to scalar arrays, over any commutative
  ring. For a : Nat → V3 R let flat a (3p + c) be the c-th component of a p. Then
    Σ_{n<3N} flat a n · flat b n = Σ_{p<N} a_p · b_p,
  so the scalar semi-implicit theorems (SemiGeneral, SemiBudgetArr, QuadBound) apply to 3D Galerkin arrays with
  E = Σ_p |a_p|² and the neutrality Σ_p a_p·F_p = 0.

  Lean 4 core plus ns/FourierGalerkin (V3, dot3), NSChain (sumN), lib/RingTac.
    sum3, sum_flat     block sums of three and the identity Σ_blocks = Σ_p a_p·b_p
    sumN_three         Σ_{n<3N} f n = Σ_{p<N} (f(3p) + f(3p+1) + f(3p+2))
    sum_flat_scalar    Σ_{n<3N} fa n · fb n = Σ_{p<N} a_p·b_p for arrays holding the components at 3p + c
-/
import ns.FourierGalerkin
import lib.RingTac
import lib.Semi

namespace Flatten3
open DetChain NSChain LibRing FourierGalerkin

section Gen
variable {R : Type} (K : CRing R)

/-- flatC a p c: component c ∈ {0, 1, 2} of a p (others give zero). -/
def comp (v : V3 R) : Nat → R
  | 0 => v.x
  | 1 => v.y
  | 2 => v.z
  | _ => K.zero

/-- Σ_{n < 3N} of a function of the flattened index, block by block. -/
def sum3 (f : Nat → Nat → R) : Nat → R
  | 0 => K.zero
  | N + 1 => K.add (sum3 f N) (K.add (K.add (f N 0) (f N 1)) (f N 2))

theorem sum_flat (a b : Nat → V3 R) : ∀ N,
    sum3 K (fun p c => K.mul (comp K (a p) c) (comp K (b p) c)) N = sumN K (fun p => dot3 K (a p) (b p)) N
  | 0 => rfl
  | N + 1 => by
    show K.add (sum3 K (fun p c => K.mul (comp K (a p) c) (comp K (b p) c)) N)
        (K.add (K.add (K.mul (a N).x (b N).x) (K.mul (a N).y (b N).y)) (K.mul (a N).z (b N).z))
      = K.add (sumN K (fun p => dot3 K (a p) (b p)) N) (dot3 K (a N) (b N))
    rw [sum_flat a b N]; rfl

theorem sum3_ext (f g : Nat → Nat → R) (h : ∀ p c, c < 3 → f p c = g p c) : ∀ N, sum3 K f N = sum3 K g N
  | 0 => rfl
  | N + 1 => by
    show K.add (sum3 K f N) (K.add (K.add (f N 0) (f N 1)) (f N 2)) = K.add (sum3 K g N) (K.add (K.add (g N 0) (g N 1)) (g N 2))
    rw [sum3_ext f g h N, h N 0 (Nat.succ_pos 2), h N 1 (Nat.succ_lt_succ (Nat.succ_pos 1)),
      h N 2 (Nat.succ_lt_succ (Nat.succ_lt_succ (Nat.succ_pos 0)))]

/-- A scalar sum over 3N indices is the block sum. -/
theorem sumN_three (f : Nat → R) : ∀ N, sumN K f (3 * N) = sum3 K (fun p c => f (3 * p + c)) N
  | 0 => rfl
  | N + 1 => by
    have e : 3 * (N + 1) = 3 * N + 2 + 1 := by lib_nat
    rw [e]
    show K.add (K.add (K.add (sumN K f (3 * N)) (f (3 * N))) (f (3 * N + 1))) (f (3 * N + 2))
      = K.add (sum3 K (fun p c => f (3 * p + c)) N) (K.add (K.add (f (3 * N + 0)) (f (3 * N + 1))) (f (3 * N + 2)))
    rw [sumN_three f N]
    generalize sum3 K (fun p c => f (3 * p + c)) N = S
    show K.add (K.add (K.add S (f (3 * N))) (f (3 * N + 1))) (f (3 * N + 2))
      = K.add S (K.add (K.add (f (3 * N)) (f (3 * N + 1))) (f (3 * N + 2)))
    generalize f (3 * N) = u; generalize f (3 * N + 1) = v; generalize f (3 * N + 2) = w
    lib_ring

/-- The energy of the flattened array: Σ_{n<3N} (flat a n)(flat b n) = Σ_{p<N} a_p · b_p, for any scalar
    arrays fa, fb that hold component c of block p at index 3p + c. -/
theorem sum_flat_scalar (a b : Nat → V3 R) (fa fb : Nat → R)
    (hfa : ∀ p c, c < 3 → fa (3 * p + c) = comp K (a p) c) (hfb : ∀ p c, c < 3 → fb (3 * p + c) = comp K (b p) c) (N : Nat) :
    sumN K (fun n => K.mul (fa n) (fb n)) (3 * N) = sumN K (fun p => dot3 K (a p) (b p)) N := by
  rw [sumN_three K _ N, ← sum_flat K a b N]
  apply sum3_ext K
  intro p c hc
  show K.mul (fa (3 * p + c)) (fb (3 * p + c)) = K.mul (comp K (a p) c) (comp K (b p) c)
  rw [hfa p c hc, hfb p c hc]

end Gen

end Flatten3

#print axioms Flatten3.sum_flat
#print axioms Flatten3.sum3_ext
#print axioms Flatten3.sumN_three
#print axioms Flatten3.sum_flat_scalar

ns/ForceAbsorb.lean

lean_all/ns/ForceAbsorb.lean

/-
  ns/ForceAbsorb.lean — T37 (goal G859c2b2, piece d3): the H¹-weighted Galerkin force absorbed into the H³ norm,
  uniform in the truncation, over an ordered field. For integer modes (nonzero, distinct on l, |k|_∞ ≤ Mm + 2) and
  any e₁, D₁, e₂, D₂, e₃, D₃ ≥ 0 with 16³Z⁵ ≤ 9³·4e₁³D₁, 16Z⁵ ≤ 9·4e₂³D₂, Z³ ≤ 4e₃D₃:
    force_absorb   Σ_r w_r|N_r(a,a)|² ≤ ι²·(k₁(e₁B + D₁ + 8Z²) + k₂(e₂B + D₂ + 2(e₃B + D₃))),
  Z = Σw|a|², B = Σw³|a|². The cut Kc is chosen by CutChoose (B ≤ 9(Kc+2)⁴Z, and Kc = 0 or 9(Kc+1)⁴Z ≤ B);
  ForceAgmonW and AgmonCollapse give ι²(k₁x³Z² + k₂xZA); AgmonAbsorb bounds both terms (A² ≤ ZB by WeightedJensen).
  With e's small this is Σw|N|² ≤ εB + C(ε)(Z⁵ + Z³ + Z²): the H² step closes with an additive constant (G859c2c).

  Lean 4 core plus ns/ForceAgmonW, ns/CutChoose, ns/AgmonCollapse, ns/WeightedJensen.
-/
import ns.ForceAgmonW
import ns.CutChoose
import ns.AgmonCollapse
import ns.WeightedJensen

namespace ForceAbsorb
open DetChain NSChain DyadicSmallGlobal LibRing DyadicLarge FourierGalerkin GalerkinForce ForceBound ForceSob
  ModeCount CubeCount ForceUniform NormCompare EnstGalerkin ForceTwoScale ForceSobW CastZAdd WeightH2 LatticeSum
  LatticeBound LatticeStep LatticeStep2 LatticeSum2 LatticeBound2 LatticeTail4 ForceAgmon ForceAgmonW AbelCount
  RootYoung AgmonAbsorb AgmonCollapse CutChoose

section Gen
variable {R : Type} (K : CRing R) (O : OrdR K) {I : Type}
variable (inv : R → R) (mul_inv : ∀ y, y ≠ K.zero → K.mul y (inv y) = K.one) (h10 : K.one ≠ K.zero)

def Zs (kz : I → Mode) (l : List I) (a : I → V3 R) : R := lsum K (fun p => K.mul (wt K kz p) (dot3 K (a p) (a p))) l
def As (kz : I → Mode) (l : List I) (a : I → V3 R) : R :=
  lsum K (fun p => K.mul (K.mul (wt K kz p) (wt K kz p)) (dot3 K (a p) (a p))) l
def Bs (kz : I → Mode) (l : List I) (a : I → V3 R) : R :=
  lsum K (fun p => K.mul (K.mul (K.mul (wt K kz p) (wt K kz p)) (wt K kz p)) (dot3 K (a p) (a p))) l

include O in
theorem ne_c3 (y : R) (hy : y ≠ K.zero) : c3 K y ≠ K.zero := by
  intro h
  cases O.noZD _ _ h with
  | inl h2 => cases O.noZD _ _ h2 with
    | inl h3 => exact hy h3
    | inr h3 => exact hy h3
  | inr h2 => exact hy h2

theorem e16 : castN K 16 = q4 K (K.add K.one K.one) := by
  have e : castN K 16 = K.add K.one (K.add K.one (K.add K.one (K.add K.one (K.add K.one (K.add K.one (K.add K.one
      (K.add K.one (K.add K.one (K.add K.one (K.add K.one (K.add K.one (K.add K.one (K.add K.one (K.add K.one
      (K.add K.one (K.zero)))))))))))))))) := rfl
  rw [e]; unfold q4; lib_ring

include h10 in
/-- A² ≤ ZB (weighted Cauchy–Schwarz) -/
theorem a_sq (kz : I → Mode) (l : List I) (a : I → V3 R) :
    Le K O.toCone (K.mul (As K kz l a) (As K kz l a)) (K.mul (Zs K kz l a) (Bs K kz l a)) := by
  have W : Le K O.toCone
      (K.add (K.mul (lsum K (fun p => K.mul (K.mul (wt K kz p) (dot3 K (a p) (a p))) (wt K kz p)) l)
          (lsum K (fun p => K.mul (K.mul (wt K kz p) (dot3 K (a p) (a p))) (wt K kz p)) l))
        (K.mul (lsum K (fun p => K.mul (K.mul (wt K kz p) (dot3 K (a p) (a p))) (wt K kz p)) l)
          (lsum K (fun p => K.mul (K.mul (wt K kz p) (dot3 K (a p) (a p))) (wt K kz p)) l)))
      (K.add (K.mul (Zs K kz l a)
          (lsum K (fun p => K.mul (K.mul (wt K kz p) (dot3 K (a p) (a p))) (K.mul (wt K kz p) (wt K kz p))) l))
        (K.mul (Zs K kz l a)
          (lsum K (fun p => K.mul (K.mul (wt K kz p) (dot3 K (a p) (a p))) (K.mul (wt K kz p) (wt K kz p))) l))) :=
    WeightedJensen.wcs2 K O.toCone (fun p => K.mul (wt K kz p) (dot3 K (a p) (a p))) (wt K kz) l
      (fun p => O.nn_mul _ _ (dot3_nn K O.toCone _) (dot3_nn K O.toCone _))
  have eA : lsum K (fun p => K.mul (K.mul (wt K kz p) (dot3 K (a p) (a p))) (wt K kz p)) l = As K kz l a :=
    lsum_ext K _ _ (fun p => by lib_ring) l
  have eB : lsum K (fun p => K.mul (K.mul (wt K kz p) (dot3 K (a p) (a p))) (K.mul (wt K kz p) (wt K kz p))) l
      = Bs K kz l a := lsum_ext K _ _ (fun p => by lib_ring) l
  rw [eA, eB] at W
  have e2 : castN K 2 = K.add K.one K.one := by show K.add K.one (K.add K.one K.zero) = _; lib_ring
  have W' : Le K O.toCone (K.mul (castN K 2) (K.mul (As K kz l a) (As K kz l a)))
      (K.mul (castN K 2) (K.mul (Zs K kz l a) (Bs K kz l a))) := by
    rw [e2]
    have x1 : K.mul (K.add K.one K.one) (K.mul (As K kz l a) (As K kz l a))
        = K.add (K.mul (As K kz l a) (As K kz l a)) (K.mul (As K kz l a) (As K kz l a)) := by lib_ring
    have x2 : K.mul (K.add K.one K.one) (K.mul (Zs K kz l a) (Bs K kz l a))
        = K.add (K.mul (Zs K kz l a) (Bs K kz l a)) (K.mul (Zs K kz l a) (Bs K kz l a)) := by unfold Zs Bs; lib_ring
    rw [x1, x2]; exact W
  exact EnstGlobal.le_of_mul_le K O _ _ _ (RatModel.nn_cast K O.toCone 2) (GoodModesQ.cast_succ_ne K O h10 1) W'

/-- B ≤ 9(Mm+2)⁴Z for |k|_∞ ≤ Mm + 2 -/
theorem b_le (kz : I → Mode) (l : List I) (a : I → V3 R) (Mm : Nat) (hM : ∀ p, p ∈ l → nrm (kz p) ≤ Mm + 2) :
    Pc K O (Zs K kz l a) (Bs K kz l a) Mm := by
  unfold Pc Bs Zs
  rw [← lsum_mul_left, ← lsum_mul_left]
  apply lsum_le_on K O
  intro p hp
  have hw := dot_le_3nrm K O (kz p)
  have c := RatModel.cast_mono K O.toCone _ _ (hM p hp)
  have s := EnstLocal.sq_mono K O _ _ (RatModel.nn_cast K O.toCone _) (RatModel.nn_cast K O.toCone _) c
  have s3 := le_add' K O.toCone _ _ _ _ (le_add' K O.toCone _ _ _ _ s s) s
  have w3 := le_trans' K O.toCone _ _ _ hw s3
  have hwn : O.nn (wt K kz p) := dot3_nn K O.toCone _
  have n3 : O.nn (K.add (K.add (K.mul (castN K (Mm + 2)) (castN K (Mm + 2))) (K.mul (castN K (Mm + 2)) (castN K (Mm + 2))))
      (K.mul (castN K (Mm + 2)) (castN K (Mm + 2)))) :=
    O.nn_add _ _ (O.nn_add _ _ (O.nn_sq _) (O.nn_sq _)) (O.nn_sq _)
  have w2 := EnstLocal.sq_mono K O _ _ hwn n3 w3
  have hd : O.nn (K.mul (wt K kz p) (dot3 K (a p) (a p))) := O.nn_mul _ _ hwn (dot3_nn K O.toCone _)
  have w4 := ForceSob.le_mul_right' K O.toCone w2 hd
  have e9 : castN K 9 = K.add K.one (K.add K.one (K.add K.one (K.add K.one (K.add K.one (K.add K.one (K.add K.one
      (K.add K.one (K.add K.one K.zero)))))))) := rfl
  refine le_trans' K O.toCone _ _ _ ?_ (le_trans' K O.toCone _ _ _ w4 ?_)
  · apply ForceAgmonW.le_eq K O
    show K.mul (K.mul (K.mul (wt K kz p) (wt K kz p)) (wt K kz p)) (dot3 K (a p) (a p)) = _
    lib_ring
  · apply ForceAgmonW.le_eq K O
    show _ = K.mul (castN K 9) (K.mul (p4 K (castN K (Mm + 2))) (K.mul (wt K kz p) (dot3 K (a p) (a p))))
    rw [e9]; unfold p4; lib_ring

include mul_inv h10 in
/-- **The H¹-weighted force absorbed into B.** -/
theorem force_absorb (l : List I) (kz : I → Mode) (hd : List.Pairwise (fun a b => kz a ≠ kz b) l)
    (h1 : ∀ p, 1 ≤ nrm (kz p)) (ι : R) (a : I → V3 R) (Mm : Nat) (hM : ∀ p, p ∈ l → nrm (kz p) ≤ Mm + 2)
    (e1 D1 e2 D2 e3 D3 : R) (he1 : O.nn e1) (hD1 : O.nn D1) (he2 : O.nn e2) (hD2 : O.nn D2)
    (he3 : O.nn e3) (hD3 : O.nn D3)
    (H1 : Le K O.toCone (K.mul (c3 K (castN K 16)) (q5 K (Zs K kz l a)))
      (K.mul (c3 K (castN K 9)) (K.mul (four K) (K.mul (c3 K e1) D1))))
    (H2 : Le K O.toCone (K.mul (castN K 16) (q5 K (Zs K kz l a)))
      (K.mul (castN K 9) (K.mul (four K) (K.mul (c3 K e2) D2))))
    (H3 : Le K O.toCone (c3 K (Zs K kz l a)) (K.mul (four K) (K.mul e3 D3))) :
    Le K O.toCone (lsum K (fun r => K.mul (wt K kz r) (dot3 K (Nl K l (TriadSparse.delta K kz) ι (kv K kz) a a r)
        (Nl K l (TriadSparse.delta K kz) ι (kv K kz) a a r))) l)
      (K.mul (K.mul ι ι)
        (K.add (K.mul (k1 K (castN K 3) (castN K 9) (castN K 108)) (K.add (K.add (K.mul e1 (Bs K kz l a)) D1) (K.mul (c3 K (castN K 2)) (K.mul (Zs K kz l a) (Zs K kz l a)))))
          (K.mul (k2 K (castN K 9) (castN K 108)) (K.add (K.add (K.mul e2 (Bs K kz l a)) D2) (K.mul (castN K 2) (K.add (K.mul e3 (Bs K kz l a)) D3)))))) := by
  have nZ : O.nn (Zs K kz l a) := lsum_nn K O.toCone _ (fun q => O.nn_mul _ _ (dot3_nn K O.toCone _) (dot3_nn K O.toCone _)) l
  have nA : O.nn (As K kz l a) := lsum_nn K O.toCone _ (fun q => O.nn_mul _ _ (O.nn_sq _) (dot3_nn K O.toCone _)) l
  have nB : O.nn (Bs K kz l a) :=
    lsum_nn K O.toCone _ (fun q => O.nn_mul _ _ (O.nn_mul _ _ (O.nn_sq _) (dot3_nn K O.toCone _)) (dot3_nn K O.toCone _)) l
  have hAZB := a_sq K O h10 kz l a
  have ⟨Kc, hKc, hP, hQ⟩ := cut_choose K O (Zs K kz l a) (Bs K kz l a) Mm (b_le K O kz l a Mm hM)
  have F := force_agmonW K O inv mul_inv h10 l kz hd h1 ι a a Kc (Mm + 1) (Nat.succ_le_succ hKc) hM
  have Cl := collapse K O (castN K 3) (castN K 9) (castN K 108) (RatModel.nn_cast K O.toCone 3)
    (RatModel.nn_cast K O.toCone 9) (RatModel.nn_cast K O.toCone 108) ι (Zs K kz l a) (As K kz l a) (Bs K kz l a) (castN K (Kc + 2)) (castN K (Kc + 1))
    (iv K inv (Kc + 2)) nZ nA nB (RatModel.nn_cast K O.toCone _) (RatModel.nn_cast K O.toCone _)
    (RatModel.cast_mono K O.toCone _ _ (Nat.le_succ _)) (iv_mul K O inv mul_inv h10 (Kc + 1)) hP
  have F2 := le_trans' K O.toCone _ _ _ F Cl
  have nE1 : O.nn (K.add (K.mul e1 (Bs K kz l a)) D1) := O.nn_add _ _ (O.nn_mul _ _ he1 nB) hD1
  have nE2 : O.nn (K.add (K.mul e2 (Bs K kz l a)) D2) := O.nn_add _ _ (O.nn_mul _ _ he2 nB) hD2
  have n2 : O.nn (castN K 2) := RatModel.nn_cast K O.toCone 2
  have n9 : castN K 9 ≠ K.zero := GoodModesQ.cast_succ_ne K O h10 8
  -- the term x·Z·A at x = 2 and the bound of Z·A
  have za := absorb2 K O (K.mul (Zs K kz l a) (As K kz l a)) (Zs K kz l a) (Bs K kz l a) e3 D3 nB he3 hD3
    (za_sq K O (Zs K kz l a) (As K kz l a) (Bs K kz l a) hAZB) H3
  have hT : Le K O.toCone (K.mul (c3 K (castN K (Kc + 2))) (K.mul (Zs K kz l a) (Zs K kz l a)))
        (K.add (K.add (K.mul e1 (Bs K kz l a)) D1) (K.mul (c3 K (castN K 2)) (K.mul (Zs K kz l a) (Zs K kz l a))))
      ∧ Le K O.toCone (K.mul (castN K (Kc + 2)) (K.mul (Zs K kz l a) (As K kz l a)))
        (K.add (K.add (K.mul e2 (Bs K kz l a)) D2) (K.mul (castN K 2) (K.add (K.mul e3 (Bs K kz l a)) D3))) := by
    cases hQ with
    | inl h0 =>
      subst h0
      have nT := O.nn_mul _ _ n2 (O.nn_add _ _ (O.nn_mul _ _ he3 nB) hD3)
      refine ⟨⟨K.add (K.mul e1 (Bs K kz l a)) D1, nE1, ?_⟩, ?_⟩
      · show K.add (K.mul (c3 K (castN K 2)) (K.mul (Zs K kz l a) (Zs K kz l a))) (K.add (K.mul e1 (Bs K kz l a)) D1) = _
        lib_ring
      · have m := le_mul_nn K O.toCone _ _ _ n2 za
        exact le_trans' K O.toCone _ _ _ m ⟨K.add (K.mul e2 (Bs K kz l a)) D2, nE2, by lib_ring⟩
    | inr hq =>
      -- 9x⁴Z ≤ 16B
      have r := cut_ratio K O Kc
      have r9 := le_mul_nn K O.toCone (castN K 9) _ _ (RatModel.nn_cast K O.toCone 9)
        (ForceSob.le_mul_right' K O.toCone r nZ)
      have q16 := le_mul_nn K O.toCone (castN K 16) _ _ (RatModel.nn_cast K O.toCone 16) hq
      have e : K.mul (castN K 9) (K.mul (p4 K (K.add (castN K (Kc + 1)) (castN K (Kc + 1)))) (Zs K kz l a))
          = K.mul (castN K 16) (K.mul (castN K 9) (K.mul (p4 K (castN K (Kc + 1))) (Zs K kz l a))) := by
        rw [e16 K]; unfold p4 q4; lib_ring
      rw [e] at r9
      have h16 : Le K O.toCone (K.mul (castN K 9) (K.mul (q4 K (castN K (Kc + 2))) (Zs K kz l a)))
          (K.mul (castN K 16) (Bs K kz l a)) := le_trans' K O.toCone _ _ _ r9 q16
      have t1 := t1_quart K O (castN K (Kc + 2)) (Zs K kz l a) (Bs K kz l a) nZ nB h16
      have t2 := t2_quart K O (castN K (Kc + 2)) (Zs K kz l a) (As K kz l a) (Bs K kz l a) nZ nB h16 hAZB
      have T1 := absorb K O _ (Zs K kz l a) (Bs K kz l a) _ _ e1 D1
        (O.nn_mul _ _ (O.nn_sq _) (RatModel.nn_cast K O.toCone 9)) (ne_c3 K O _ n9) nB he1 hD1 t1 H1
      have T2 := absorb K O _ (Zs K kz l a) (Bs K kz l a) _ _ e2 D2
        (RatModel.nn_cast K O.toCone 9) n9 nB he2 hD2 t2 H2
      have nX : O.nn (K.mul (c3 K (castN K 2)) (K.mul (Zs K kz l a) (Zs K kz l a))) :=
        O.nn_mul _ _ (O.nn_mul _ _ (O.nn_sq _) n2) (O.nn_sq _)
      have nY : O.nn (K.mul (castN K 2) (K.add (K.mul e3 (Bs K kz l a)) D3)) :=
        O.nn_mul _ _ n2 (O.nn_add _ _ (O.nn_mul _ _ he3 nB) hD3)
      exact ⟨le_trans' K O.toCone _ _ _ T1 ⟨_, nX, rfl⟩, le_trans' K O.toCone _ _ _ T2 ⟨_, nY, rfl⟩⟩
  have nk1 : O.nn (k1 K (castN K 3) (castN K 9) (castN K 108)) := by
    unfold k1 two
    exact O.nn_mul _ _ (O.nn_add _ _ (RatModel.nn_one' K O.toCone) (RatModel.nn_cast K O.toCone 9))
      (O.nn_mul _ _ (O.nn_add _ _ (RatModel.nn_one' K O.toCone) (RatModel.nn_one' K O.toCone))
        (O.nn_mul _ _ (RatModel.nn_cast K O.toCone 3)
          (O.nn_add _ _ (RatModel.nn_cast K O.toCone 108) (RatModel.nn_cast K O.toCone 108))))
  have nk2 : O.nn (k2 K (castN K 9) (castN K 108)) := by
    unfold k2 two
    exact O.nn_mul _ _ (O.nn_add _ _ (RatModel.nn_one' K O.toCone) (RatModel.nn_cast K O.toCone 9))
      (O.nn_mul _ _ (O.nn_add _ _ (RatModel.nn_one' K O.toCone) (RatModel.nn_one' K O.toCone))
        (O.nn_add _ _ (RatModel.nn_cast K O.toCone 108) (RatModel.nn_cast K O.toCone 108)))
  have G := le_add' K O.toCone _ _ _ _ (le_mul_nn K O.toCone _ _ _ nk1 hT.1) (le_mul_nn K O.toCone _ _ _ nk2 hT.2)
  exact le_trans' K O.toCone _ _ _ F2 (le_mul_nn K O.toCone _ _ _ (O.nn_sq ι) G)

end Gen

end ForceAbsorb

#print axioms ForceAbsorb.ne_c3
#print axioms ForceAbsorb.e16
#print axioms ForceAbsorb.a_sq
#print axioms ForceAbsorb.b_le
#print axioms ForceAbsorb.force_absorb

ns/ForceAgmon.lean

lean_all/ns/ForceAgmon.lean

/-
  ns/ForceAgmon.lean — T37 cycle 1958 (goal G859c2b1, piece e): the Galerkin force bound with the Agmon cut, uniform
  in the truncation, over an ordered field. For integer modes (nonzero, distinct on l, |k|_∞ ≤ M + 1) and a cut
  Kc + 1 ≤ M, with the weights μ_p = |k_p|_∞ (|k_p|_∞ ≤ Kc + 1) and μ_p = |k_p|_∞²/(Kc + 2) (above), ν_p = 1/μ_p:
    force_agmon   Σ_r |N_r(u,v)|² ≤ ι²·(Z(u) + A(u)/(Kc+2)²)·((108(Kc+1) + 108(Kc+2))·Z(v)),
  Z = Σ w|·|², A = Σ w²|·|² (w = |k|²). With (Kc + 2)² ≈ A/Z this is the discrete ‖(u·∇)v‖² ≲ ‖∇u‖‖Δu‖‖∇v‖²
  (Agmon), the force bound that lets the H² step close on large data (G859c2).
  ForceSob.total_sob with the cut μ; Σν² from LatticeBound2 (low) and LatticeTail4 (high).

  Lean 4 core plus ns/ForceH2, ns/LatticeBound2, ns/LatticeTail4, ns/WeightedCS.
-/
import ns.ForceH2
import ns.LatticeBound2
import ns.LatticeTail4
import ns.WeightedCS

namespace ForceAgmon
open DetChain NSChain DyadicSmallGlobal LibRing DyadicLarge FourierGalerkin GalerkinForce ForceBound ForceSob
  ModeCount CubeCount ForceUniform NormCompare EnstGalerkin ForceTwoScale ForceSobW CastZAdd WeightH2 LatticeSum
  LatticeBound LatticeStep LatticeStep2 LatticeSum2 LatticeBound2 LatticeTail4

theorem ble_le : ∀ a b : Nat, Nat.ble a b = true → a ≤ b
  | 0, _, _ => Nat.zero_le _
  | _ + 1, 0, h => nomatch h
  | a + 1, b + 1, h => Nat.succ_le_succ (ble_le a b h)

theorem gt_of_ble_false : ∀ a b : Nat, Nat.ble a b = false → b < a
  | 0, _, h => nomatch h
  | a + 1, 0, _ => Nat.succ_pos a
  | a + 1, b + 1, h => Nat.succ_lt_succ (gt_of_ble_false a b h)

section Lists
variable {I : Type}

theorem mem_filter_inv (q : I → Bool) (x : I) : ∀ l : List I, x ∈ l.filter q → q x = true ∧ x ∈ l
  | [], h => nomatch h
  | y :: l, h => by
    cases hy : q y with
    | true =>
      have h' : x ∈ y :: l.filter q := by
        have e : (y :: l).filter q = y :: l.filter q := by
          show (match q y with | true => y :: l.filter q | false => l.filter q) = _
          rw [hy]
        rw [e] at h; exact h
      cases h' with
      | head => exact ⟨hy, List.Mem.head _⟩
      | tail _ h2 => have ⟨a, b⟩ := mem_filter_inv q x l h2; exact ⟨a, List.Mem.tail _ b⟩
    | false =>
      have h' : x ∈ l.filter q := by
        have e : (y :: l).filter q = l.filter q := by
          show (match q y with | true => y :: l.filter q | false => l.filter q) = _
          rw [hy]
        rw [e] at h; exact h
      have ⟨a, b⟩ := mem_filter_inv q x l h'
      exact ⟨a, List.Mem.tail _ b⟩

theorem pw_filter (Rl : I → I → Prop) (q : I → Bool) : ∀ l : List I, List.Pairwise Rl l → List.Pairwise Rl (l.filter q)
  | [], _ => List.Pairwise.nil
  | x :: l, h => by
    cases h with
    | cons hx hl =>
      cases hq : q x with
      | true =>
        have e : (x :: l).filter q = x :: l.filter q := by
          show (match q x with | true => x :: l.filter q | false => l.filter q) = _
          rw [hq]
        rw [e]
        exact List.Pairwise.cons (fun y hy => hx y (mem_filter_inv q y l hy).2) (pw_filter Rl q l hl)
      | false =>
        have e : (x :: l).filter q = l.filter q := by
          show (match q x with | true => x :: l.filter q | false => l.filter q) = _
          rw [hq]
        rw [e]
        exact pw_filter Rl q l hl

end Lists

section Gen
variable {R : Type} (K : CRing R) (O : OrdR K) {I : Type}
variable (inv : R → R) (mul_inv : ∀ y, y ≠ K.zero → K.mul y (inv y) = K.one) (h10 : K.one ≠ K.zero)

theorem lsum_split (q : I → Bool) (f : I → R) : ∀ l : List I,
    lsum K f l = K.add (lsum K f (l.filter q)) (lsum K f (l.filter (fun p => !(q p))))
  | [] => by show K.zero = K.add K.zero K.zero; lib_ring
  | x :: l => by
    have ih := lsum_split q f l
    cases hq : q x with
    | true =>
      have e1 : (x :: l).filter q = x :: l.filter q := by
        show (match q x with | true => x :: l.filter q | false => l.filter q) = _
        rw [hq]
      have e2 : (x :: l).filter (fun p => !(q p)) = l.filter (fun p => !(q p)) := by
        show (match !(q x) with | true => x :: l.filter (fun p => !(q p)) | false => l.filter (fun p => !(q p))) = _
        rw [hq]; rfl
      rw [e1, e2]
      show K.add (f x) (lsum K f l) = K.add (K.add (f x) (lsum K f (l.filter q))) (lsum K f (l.filter (fun p => !(q p))))
      rw [ih]; lib_ring
    | false =>
      have e1 : (x :: l).filter q = l.filter q := by
        show (match q x with | true => x :: l.filter q | false => l.filter q) = _
        rw [hq]
      have e2 : (x :: l).filter (fun p => !(q p)) = x :: l.filter (fun p => !(q p)) := by
        show (match !(q x) with | true => x :: l.filter (fun p => !(q p)) | false => l.filter (fun p => !(q p))) = _
        rw [hq]; rfl
      rw [e1, e2]
      show K.add (f x) (lsum K f l) = K.add (lsum K f (l.filter q)) (K.add (f x) (lsum K f (l.filter (fun p => !(q p)))))
      rw [ih]; lib_ring

theorem lsum_congr_mem (f g : I → R) : ∀ l : List I, (∀ x, x ∈ l → f x = g x) → lsum K f l = lsum K g l
  | [], _ => rfl
  | x :: l, h => by
    show K.add (f x) (lsum K f l) = K.add (g x) (lsum K g l)
    rw [h x (List.Mem.head _), lsum_congr_mem f g l (fun y hy => h y (List.Mem.tail _ hy))]

/-- the low/high predicate of the cut -/
def low (kz : I → Mode) (Kc : Nat) (p : I) : Bool := Nat.ble (nrm (kz p)) (Kc + 1)

def mu (kz : I → Mode) (Kc : Nat) (p : I) : R :=
  cond (low kz Kc p) (nn K kz p) (K.mul (K.mul (nn K kz p) (nn K kz p)) (iv K inv (Kc + 2)))

def nu (kz : I → Mode) (Kc : Nat) (p : I) : R :=
  cond (low kz Kc p) (ivn K inv kz p) (K.mul (K.mul (ivn K inv kz p) (ivn K inv kz p)) (castN K (Kc + 2)))

include O mul_inv h10 in
theorem munu (kz : I → Mode) (h1 : ∀ p, 1 ≤ nrm (kz p)) (Kc : Nat) (p : I) :
    K.mul (mu K inv kz Kc p) (nu K inv kz Kc p) = K.one := by
  have e := nn_ivn K O inv mul_inv h10 kz h1 p
  unfold mu nu
  cases low kz Kc p with
  | true => exact e
  | false =>
    have ec : K.mul (castN K (Kc + 2)) (iv K inv (Kc + 2)) = K.one := iv_mul K O inv mul_inv h10 (Kc + 1)
    show K.mul (K.mul (K.mul (nn K kz p) (nn K kz p)) (iv K inv (Kc + 2)))
      (K.mul (K.mul (ivn K inv kz p) (ivn K inv kz p)) (castN K (Kc + 2))) = K.one
    calc _ = K.mul (K.mul (K.mul (nn K kz p) (ivn K inv kz p)) (K.mul (nn K kz p) (ivn K inv kz p)))
          (K.mul (castN K (Kc + 2)) (iv K inv (Kc + 2))) := by lib_ring
      _ = K.one := by rw [e, ec]; lib_ring

/-- the u-side weight: μ²|u|² ≤ w|u|² + iv(Kc+2)²·w²|u|² -/
theorem mu_le (kz : I → Mode) (Kc : Nat) (u : I → V3 R) (p : I) :
    Le K O.toCone (K.mul (K.mul (mu K inv kz Kc p) (mu K inv kz Kc p)) (dot3 K (u p) (u p)))
      (K.add (K.mul (wt K kz p) (dot3 K (u p) (u p)))
        (K.mul (K.mul (iv K inv (Kc + 2)) (iv K inv (Kc + 2))) (K.mul (K.mul (wt K kz p) (wt K kz p)) (dot3 K (u p) (u p))))) := by
  have hw := nrm_le_dot K O.toCone (kz p)
  have hu : O.nn (dot3 K (u p) (u p)) := dot3_nn K O.toCone _
  have hwn : O.nn (wt K kz p) := dot3_nn K O.toCone _
  have hn2 : O.nn (K.mul (nn K kz p) (nn K kz p)) := O.nn_sq _
  have nnT : O.nn (K.mul (K.mul (iv K inv (Kc + 2)) (iv K inv (Kc + 2))) (K.mul (K.mul (wt K kz p) (wt K kz p)) (dot3 K (u p) (u p)))) :=
    O.nn_mul _ _ (O.nn_sq _) (O.nn_mul _ _ (O.nn_sq _) hu)
  unfold mu
  cases low kz Kc p with
  | true =>
    have s := le_mul_right' K O.toCone hw hu
    refine le_trans' K O.toCone _ _ _ s ⟨_, nnT, rfl⟩
  | false =>
    have s4 := EnstLocal.sq_mono K O _ _ hn2 hwn hw
    have s := le_mul_right' K O.toCone s4 (O.nn_mul _ _ (O.nn_sq (iv K inv (Kc + 2))) hu)
    have e1 : K.mul (K.mul (K.mul (K.mul (nn K kz p) (nn K kz p)) (iv K inv (Kc + 2)))
        (K.mul (K.mul (nn K kz p) (nn K kz p)) (iv K inv (Kc + 2)))) (dot3 K (u p) (u p))
        = K.mul (K.mul (K.mul (nn K kz p) (nn K kz p)) (K.mul (nn K kz p) (nn K kz p)))
          (K.mul (K.mul (iv K inv (Kc + 2)) (iv K inv (Kc + 2))) (dot3 K (u p) (u p))) := by lib_ring
    show Le K O.toCone (K.mul (K.mul (K.mul (K.mul (nn K kz p) (nn K kz p)) (iv K inv (Kc + 2)))
        (K.mul (K.mul (nn K kz p) (nn K kz p)) (iv K inv (Kc + 2)))) (dot3 K (u p) (u p))) _
    rw [e1]
    refine le_trans' K O.toCone _ _ _ s ⟨K.mul (wt K kz p) (dot3 K (u p) (u p)), O.nn_mul _ _ hwn hu, ?_⟩
    lib_ring

include mul_inv h10 in
/-- Σν² ≤ 108(Kc+1) + 108(Kc+2) -/
theorem nu_sum (l : List I) (kz : I → Mode) (hd : List.Pairwise (fun a b => kz a ≠ kz b) l)
    (h1 : ∀ p, 1 ≤ nrm (kz p)) (Kc M : Nat) (hKM : Kc + 1 ≤ M) (hM : ∀ p, p ∈ l → nrm (kz p) ≤ M + 1) :
    Le K O.toCone (lsum K (fun p => K.mul (nu K inv kz Kc p) (nu K inv kz Kc p)) l)
      (K.add (K.mul (castN K 108) (castN K (Kc + 1))) (K.mul (castN K 108) (castN K (Kc + 2)))) := by
  rw [lsum_split K (low kz Kc)]
  -- low part
  have eL : lsum K (fun p => K.mul (nu K inv kz Kc p) (nu K inv kz Kc p)) (l.filter (low kz Kc))
      = lsum K (fun p => f2 K inv (nrm (kz p))) (l.filter (low kz Kc)) := by
    apply lsum_congr_mem
    intro p hp
    have hq := (mem_filter_inv _ p l hp).1
    unfold nu; rw [hq]; rfl
  have eH : lsum K (fun p => K.mul (nu K inv kz Kc p) (nu K inv kz Kc p)) (l.filter (fun p => !(low kz Kc p)))
      = K.mul (K.mul (castN K (Kc + 2)) (castN K (Kc + 2)))
          (lsum K (fun p => f4 K inv (nrm (kz p))) (l.filter (fun p => !(low kz Kc p)))) := by
    rw [← lsum_mul_left]
    apply lsum_congr_mem
    intro p hp
    have hq := (mem_filter_inv _ p l hp).1
    have hq' : low kz Kc p = false := by
      cases e : low kz Kc p with
      | false => rfl
      | true => rw [e] at hq; exact nomatch hq
    unfold nu; rw [hq']
    show K.mul (K.mul (K.mul (ivn K inv kz p) (ivn K inv kz p)) (castN K (Kc + 2)))
        (K.mul (K.mul (ivn K inv kz p) (ivn K inv kz p)) (castN K (Kc + 2)))
      = K.mul (K.mul (castN K (Kc + 2)) (castN K (Kc + 2))) (p4 K (iv K inv (nrm (kz p))))
    unfold p4 ivn iv; lib_ring
  rw [eL, eH, lsum_norms K (f2 K inv) kz, lsum_norms K (f4 K inv) kz]
  have L1 := lattice_bound2 K O inv mul_inv h10 ((l.filter (low kz Kc)).map kz)
    (pw_map kz _ (pw_filter _ _ l hd))
    (fun k hk => by have ⟨q, _, e⟩ := mem_map_inv kz k _ hk; rw [← e]; exact h1 q) Kc
    (fun k hk => by
      have ⟨q, hq, e⟩ := mem_map_inv kz k _ hk
      rw [← e]; exact ble_le _ _ (mem_filter_inv _ q l hq).1)
  have L2 := lattice_tail4 K O inv mul_inv h10 ((l.filter (fun p => !(low kz Kc p))).map kz)
    (pw_map kz _ (pw_filter _ _ l hd)) (Kc + 1) M hKM
    (fun k hk => by
      have ⟨q, hq, e⟩ := mem_map_inv kz k _ hk
      rw [← e]
      have hq1 := (mem_filter_inv _ q l hq).1
      have hq' : low kz Kc q = false := by
        cases e2 : low kz Kc q with
        | false => rfl
        | true => rw [e2] at hq1; exact nomatch hq1
      exact gt_of_ble_false _ _ hq')
    (fun k hk => by have ⟨q, hq, e⟩ := mem_map_inv kz k _ hk; rw [← e]; exact hM q (mem_filter_inv _ q l hq).2)
  have c2 : O.nn (K.mul (castN K (Kc + 2)) (castN K (Kc + 2))) := O.nn_sq _
  have L2' := le_mul_nn K O.toCone _ _ _ c2 L2
  have ec : K.mul (castN K (Kc + 2)) (iv K inv (Kc + 2)) = K.one := iv_mul K O inv mul_inv h10 (Kc + 1)
  have e3 : K.mul (K.mul (castN K (Kc + 2)) (castN K (Kc + 2))) (K.mul (castN K 108) (iv K inv (Kc + 2)))
      = K.mul (castN K 108) (castN K (Kc + 2)) := by
    calc _ = K.mul (K.mul (castN K 108) (castN K (Kc + 2))) (K.mul (castN K (Kc + 2)) (iv K inv (Kc + 2))) := by
          lib_ring
      _ = _ := by rw [ec]; lib_ring
  have L2e : Le K O.toCone (K.mul (K.mul (castN K (Kc + 2)) (castN K (Kc + 2)))
      (AbelCount.lsumN K (f4 K inv) (norms ((l.filter (fun p => !(low kz Kc p))).map kz))))
      (K.mul (K.mul (castN K (Kc + 2)) (castN K (Kc + 2))) (K.mul (castN K 108) (iv K inv (Kc + 2)))) := L2'
  rw [e3] at L2e
  exact le_add' K O.toCone _ _ _ _ L1 L2e

include mul_inv h10 in
/-- **The force bound with the Agmon cut.** -/
theorem force_agmon (l : List I) (kz : I → Mode) (hd : List.Pairwise (fun a b => kz a ≠ kz b) l)
    (h1 : ∀ p, 1 ≤ nrm (kz p)) (ι : R) (u v : I → V3 R) (Kc M : Nat) (hKM : Kc + 1 ≤ M)
    (hM : ∀ p, p ∈ l → nrm (kz p) ≤ M + 1) :
    Le K O.toCone (lsum K (fun r => dot3 K (Nl K l (TriadSparse.delta K kz) ι (kv K kz) u v r)
        (Nl K l (TriadSparse.delta K kz) ι (kv K kz) u v r)) l)
      (K.mul (K.mul ι ι)
        (K.mul (K.add (lsum K (fun p => K.mul (wt K kz p) (dot3 K (u p) (u p))) l)
            (K.mul (K.mul (iv K inv (Kc + 2)) (iv K inv (Kc + 2)))
              (lsum K (fun p => K.mul (K.mul (wt K kz p) (wt K kz p)) (dot3 K (u p) (u p))) l)))
          (K.mul (K.add (K.mul (castN K 108) (castN K (Kc + 1))) (K.mul (castN K 108) (castN K (Kc + 2))))
            (lsum K (fun q => K.mul (wt K kz q) (dot3 K (v q) (v q))) l)))) := by
  have T := total_sob K O.toCone l (TriadSparse.delta K kz) ι (kv K kz) u v (mu K inv kz Kc) (nu K inv kz Kc)
    (delta01 K kz) (munu K O inv mul_inv h10 kz h1 Kc)
    (fun p r => TriadSparse.spA K O.toCone kz l hd p r) (fun p q => TriadSparse.spB K O.toCone kz l hd p q)
  refine le_trans' K O.toCone _ _ _ T ?_
  have hH := lsum_le K O.toCone _ _ (fun p => mu_le K O inv kz Kc u p) l
  rw [lsum_add, lsum_mul_left] at hH
  have hN := nu_sum K O inv mul_inv h10 l kz hd h1 Kc M hKM hM
  have hZv : O.nn (lsum K (fun q => K.mul (wt K kz q) (dot3 K (v q) (v q))) l) :=
    lsum_nn K O.toCone _ (fun q => O.nn_mul _ _ (dot3_nn K O.toCone _) (dot3_nn K O.toCone _)) l
  have hSZ := le_mul_right' K O.toCone hN hZv
  have hHn : O.nn (lsum K (fun p => K.mul (K.mul (mu K inv kz Kc p) (mu K inv kz Kc p)) (dot3 K (u p) (u p))) l) :=
    lsum_nn K O.toCone _ (fun p => O.nn_mul _ _ (O.nn_sq _) (dot3_nn K O.toCone _)) l
  have hS'n : O.nn (K.mul (K.add (K.mul (castN K 108) (castN K (Kc + 1))) (K.mul (castN K 108) (castN K (Kc + 2))))
      (lsum K (fun q => K.mul (wt K kz q) (dot3 K (v q) (v q))) l)) :=
    O.nn_mul _ _ (O.nn_add _ _ (O.nn_mul _ _ (RatModel.nn_cast K O.toCone 108) (RatModel.nn_cast K O.toCone _))
      (O.nn_mul _ _ (RatModel.nn_cast K O.toCone 108) (RatModel.nn_cast K O.toCone _))) hZv
  have hB := WeightedCS.le_mul_both' K O.toCone _ _ _ _ hHn hS'n hH hSZ
  exact le_mul_nn K O.toCone _ _ _ (O.nn_sq ι) hB

end Gen

end ForceAgmon

#print axioms ForceAgmon.ble_le
#print axioms ForceAgmon.gt_of_ble_false
#print axioms ForceAgmon.mem_filter_inv
#print axioms ForceAgmon.pw_filter
#print axioms ForceAgmon.lsum_split
#print axioms ForceAgmon.lsum_congr_mem
#print axioms ForceAgmon.munu
#print axioms ForceAgmon.mu_le
#print axioms ForceAgmon.nu_sum
#print axioms ForceAgmon.force_agmon

ns/ForceAgmonW.lean

lean_all/ns/ForceAgmonW.lean

/-
  ns/ForceAgmonW.lean — T37 (goal G859c2b2, piece a): the H¹-weighted Galerkin force bound with a cubic cut,
  uniform in the truncation, over an ordered field. For integer modes (nonzero, distinct on l, |k|_∞ ≤ M + 1) and a
  cut Kc + 1 ≤ M, with μ_p = |k_p|_∞ (|k_p|_∞ ≤ Kc + 1), μ_p = |k_p|_∞³/(Kc+2)² (above), ν_p = 1/μ_p:
    force_agmonW   Σ_r w_r|N_r(u,v)|² ≤ ι²·(Z(u) + B(u)/(Kc+2)⁴)·(2·S₁·Z(v) + 2·S₀·A(v)),
      S₁ = 3·(108(Kc+1)³ + 108(Kc+2)³),  S₀ = 108(Kc+1) + 108(Kc+2),
  Z = Σ w|·|², A = Σ w²|·|², B = Σ w³|·|² (w = |k|²). With (Kc+2)⁴ ≈ B/Z the right side is
  ≲ Z^{5/4}B^{3/4} + Z^{3/4}·A·B^{1/4}, the weighted force bound for the uniform H² step on large data (G859c2c).
  ForceSobW.total_sobW with the cut; Σν² and Σν²w from LatticeBound2 (low) and LatticeTail4 (high).

  Lean 4 core plus ns/ForceAgmon.
-/
import ns.ForceAgmon

namespace ForceAgmonW
open DetChain NSChain DyadicSmallGlobal LibRing DyadicLarge FourierGalerkin GalerkinForce ForceBound ForceSob
  ModeCount CubeCount ForceUniform NormCompare EnstGalerkin ForceTwoScale ForceSobW CastZAdd WeightH2 LatticeSum
  LatticeBound LatticeStep LatticeStep2 LatticeSum2 LatticeBound2 LatticeTail4 ForceAgmon

section Gen
variable {R : Type} (K : CRing R) (O : OrdR K) {I : Type}
variable (inv : R → R) (mul_inv : ∀ y, y ≠ K.zero → K.mul y (inv y) = K.one) (h10 : K.one ≠ K.zero)

theorem lsum_le_on (f g : I → R) : ∀ l : List I, (∀ p, p ∈ l → Le K O.toCone (f p) (g p)) →
    Le K O.toCone (lsum K f l) (lsum K g l)
  | [], _ => le_refl' K O.toCone _
  | x :: l, h => le_add' K O.toCone _ _ _ _ (h x (List.Mem.head _)) (lsum_le_on f g l (fun p hp => h p (List.Mem.tail _ hp)))

theorem le_eq (x y : R) (h : x = y) : Le K O.toCone x y := by
  rw [h]; exact le_refl' K O.toCone y

def mu3 (kz : I → Mode) (Kc : Nat) (p : I) : R :=
  cond (low kz Kc p) (nn K kz p)
    (K.mul (K.mul (K.mul (nn K kz p) (nn K kz p)) (nn K kz p)) (K.mul (iv K inv (Kc + 2)) (iv K inv (Kc + 2))))

def nu3 (kz : I → Mode) (Kc : Nat) (p : I) : R :=
  cond (low kz Kc p) (ivn K inv kz p)
    (K.mul (K.mul (K.mul (ivn K inv kz p) (ivn K inv kz p)) (ivn K inv kz p)) (K.mul (castN K (Kc + 2)) (castN K (Kc + 2))))

include O mul_inv h10 in
theorem munu3c (kz : I → Mode) (h1 : ∀ p, 1 ≤ nrm (kz p)) (Kc : Nat) (p : I) :
    K.mul (mu3 K inv kz Kc p) (nu3 K inv kz Kc p) = K.one := by
  have e := nn_ivn K O inv mul_inv h10 kz h1 p
  unfold mu3 nu3
  cases low kz Kc p with
  | true => exact e
  | false =>
    have ec : K.mul (castN K (Kc + 2)) (iv K inv (Kc + 2)) = K.one := iv_mul K O inv mul_inv h10 (Kc + 1)
    show K.mul (K.mul (K.mul (K.mul (nn K kz p) (nn K kz p)) (nn K kz p)) (K.mul (iv K inv (Kc + 2)) (iv K inv (Kc + 2))))
      (K.mul (K.mul (K.mul (ivn K inv kz p) (ivn K inv kz p)) (ivn K inv kz p)) (K.mul (castN K (Kc + 2)) (castN K (Kc + 2))))
      = K.one
    calc _ = K.mul (K.mul (K.mul (K.mul (nn K kz p) (ivn K inv kz p)) (K.mul (nn K kz p) (ivn K inv kz p)))
            (K.mul (nn K kz p) (ivn K inv kz p)))
          (K.mul (K.mul (castN K (Kc + 2)) (iv K inv (Kc + 2))) (K.mul (castN K (Kc + 2)) (iv K inv (Kc + 2)))) := by
          lib_ring
      _ = K.one := by rw [e, ec]; lib_ring

/-- the u-side weight: μ²|u|² ≤ w|u|² + iv(Kc+2)⁴·w³|u|² -/
theorem mu3_le (kz : I → Mode) (Kc : Nat) (u : I → V3 R) (p : I) :
    Le K O.toCone (K.mul (K.mul (mu3 K inv kz Kc p) (mu3 K inv kz Kc p)) (dot3 K (u p) (u p)))
      (K.add (K.mul (wt K kz p) (dot3 K (u p) (u p)))
        (K.mul (K.mul (K.mul (iv K inv (Kc + 2)) (iv K inv (Kc + 2))) (K.mul (iv K inv (Kc + 2)) (iv K inv (Kc + 2))))
          (K.mul (K.mul (K.mul (wt K kz p) (wt K kz p)) (wt K kz p)) (dot3 K (u p) (u p))))) := by
  have hw : Le K O.toCone (K.mul (nn K kz p) (nn K kz p)) (wt K kz p) := nrm_le_dot K O.toCone (kz p)
  have hu : O.nn (dot3 K (u p) (u p)) := dot3_nn K O.toCone _
  have hwn : O.nn (wt K kz p) := dot3_nn K O.toCone _
  have hn2 : O.nn (K.mul (nn K kz p) (nn K kz p)) := O.nn_sq _
  have nnT : O.nn (K.mul (K.mul (K.mul (iv K inv (Kc + 2)) (iv K inv (Kc + 2))) (K.mul (iv K inv (Kc + 2)) (iv K inv (Kc + 2))))
      (K.mul (K.mul (K.mul (wt K kz p) (wt K kz p)) (wt K kz p)) (dot3 K (u p) (u p)))) :=
    O.nn_mul _ _ (O.nn_mul _ _ (O.nn_sq _) (O.nn_sq _)) (O.nn_mul _ _ (O.nn_mul _ _ (O.nn_sq _) hwn) hu)
  unfold mu3
  cases low kz Kc p with
  | true =>
    have s := le_mul_right' K O.toCone hw hu
    exact le_trans' K O.toCone _ _ _ s ⟨_, nnT, rfl⟩
  | false =>
    have s4 := WeightedCS.le_mul_both' K O.toCone _ _ _ _ hn2 hwn hw hw
    have s6 := WeightedCS.le_mul_both' K O.toCone _ _ _ _ (O.nn_mul _ _ hn2 hn2) hwn s4 hw
    have s := le_mul_right' K O.toCone s6
      (O.nn_mul _ _ (O.nn_mul _ _ (O.nn_sq (iv K inv (Kc + 2))) (O.nn_sq (iv K inv (Kc + 2)))) hu)
    have e1 : K.mul (K.mul (K.mul (K.mul (K.mul (nn K kz p) (nn K kz p)) (nn K kz p))
          (K.mul (iv K inv (Kc + 2)) (iv K inv (Kc + 2))))
        (K.mul (K.mul (K.mul (nn K kz p) (nn K kz p)) (nn K kz p)) (K.mul (iv K inv (Kc + 2)) (iv K inv (Kc + 2)))))
        (dot3 K (u p) (u p))
        = K.mul (K.mul (K.mul (K.mul (nn K kz p) (nn K kz p)) (K.mul (nn K kz p) (nn K kz p))) (K.mul (nn K kz p) (nn K kz p)))
          (K.mul (K.mul (K.mul (iv K inv (Kc + 2)) (iv K inv (Kc + 2))) (K.mul (iv K inv (Kc + 2)) (iv K inv (Kc + 2))))
            (dot3 K (u p) (u p))) := by lib_ring
    show Le K O.toCone (K.mul (K.mul (K.mul (K.mul (K.mul (nn K kz p) (nn K kz p)) (nn K kz p))
          (K.mul (iv K inv (Kc + 2)) (iv K inv (Kc + 2))))
        (K.mul (K.mul (K.mul (nn K kz p) (nn K kz p)) (nn K kz p)) (K.mul (iv K inv (Kc + 2)) (iv K inv (Kc + 2)))))
        (dot3 K (u p) (u p))) _
    rw [e1]
    refine le_trans' K O.toCone _ _ _ s ⟨K.mul (wt K kz p) (dot3 K (u p) (u p)), O.nn_mul _ _ hwn hu, ?_⟩
    lib_ring

include mul_inv h10 in
/-- above the cut, 1/|k|_∞² ≤ 1/(Kc+2)² -/
theorem ivn_sq_le (kz : I → Mode) (Kc : Nat) (p : I) (h : Kc + 2 ≤ nrm (kz p)) :
    Le K O.toCone (K.mul (ivn K inv kz p) (ivn K inv kz p)) (K.mul (iv K inv (Kc + 2)) (iv K inv (Kc + 2))) := by
  have e : ivn K inv kz p = iv K inv (nrm (kz p)) := rfl
  rw [e]
  cases hn : nrm (kz p) with
  | zero => rw [hn] at h; exact absurd h (Nat.not_succ_le_zero _)
  | succ b =>
    rw [hn] at h
    have hb : Kc + 1 ≤ b := Nat.le_of_succ_le_succ h
    have a := LatticeTail.iv_anti K O inv mul_inv h10 (Kc + 1) b hb
    exact EnstLocal.sq_mono K O _ _ (iv_nn K O inv mul_inv h10 b) (iv_nn K O inv mul_inv h10 (Kc + 1)) a

theorem low_true (kz : I → Mode) (Kc : Nat) (l : List I) (p : I) (hp : p ∈ l.filter (low kz Kc)) :
    low kz Kc p = true := (mem_filter_inv _ p l hp).1

theorem low_false (kz : I → Mode) (Kc : Nat) (l : List I) (p : I) (hp : p ∈ l.filter (fun p => !(low kz Kc p))) :
    low kz Kc p = false := by
  have hq1 := (mem_filter_inv _ p l hp).1
  cases e2 : low kz Kc p with
  | false => rfl
  | true => rw [e2] at hq1; exact nomatch hq1

theorem low_nrm (kz : I → Mode) (Kc : Nat) (p : I) (h : low kz Kc p = true) : nrm (kz p) ≤ Kc + 1 :=
  ble_le _ _ h

theorem high_nrm (kz : I → Mode) (Kc : Nat) (p : I) (h : low kz Kc p = false) : Kc + 2 ≤ nrm (kz p) :=
  gt_of_ble_false _ _ h

include mul_inv h10 in
/-- Σν² ≤ 108(Kc+1) + 108(Kc+2) for the cubic cut -/
theorem nu3_sum0 (l : List I) (kz : I → Mode) (hd : List.Pairwise (fun a b => kz a ≠ kz b) l)
    (h1 : ∀ p, 1 ≤ nrm (kz p)) (Kc M : Nat) (hKM : Kc + 1 ≤ M) (hM : ∀ p, p ∈ l → nrm (kz p) ≤ M + 1) :
    Le K O.toCone (lsum K (fun p => K.mul (nu3 K inv kz Kc p) (nu3 K inv kz Kc p)) l)
      (K.add (K.mul (castN K 108) (castN K (Kc + 1))) (K.mul (castN K 108) (castN K (Kc + 2)))) := by
  rw [lsum_split K (low kz Kc)]
  have eL : lsum K (fun p => K.mul (nu3 K inv kz Kc p) (nu3 K inv kz Kc p)) (l.filter (low kz Kc))
      = lsum K (fun p => f2 K inv (nrm (kz p))) (l.filter (low kz Kc)) := by
    apply lsum_congr_mem
    intro p hp
    unfold nu3; rw [low_true kz Kc l p hp]; rfl
  have c4 : O.nn (K.mul (K.mul (castN K (Kc + 2)) (castN K (Kc + 2))) (K.mul (K.mul (castN K (Kc + 2)) (castN K (Kc + 2)))
      (K.mul (iv K inv (Kc + 2)) (iv K inv (Kc + 2))))) :=
    O.nn_mul _ _ (O.nn_sq _) (O.nn_mul _ _ (O.nn_sq _) (O.nn_sq _))
  have hH : Le K O.toCone
      (lsum K (fun p => K.mul (nu3 K inv kz Kc p) (nu3 K inv kz Kc p)) (l.filter (fun p => !(low kz Kc p))))
      (lsum K (fun p => K.mul (K.mul (K.mul (castN K (Kc + 2)) (castN K (Kc + 2))) (K.mul (K.mul (castN K (Kc + 2)) (castN K (Kc + 2)))
          (K.mul (iv K inv (Kc + 2)) (iv K inv (Kc + 2))))) (f4 K inv (nrm (kz p)))) (l.filter (fun p => !(low kz Kc p)))) := by
    apply lsum_le_on K O
    intro p hp
    have hl := low_false kz Kc l p hp
    have hiv := ivn_sq_le K O inv mul_inv h10 kz Kc p (high_nrm kz Kc p hl)
    have cn : O.nn (K.mul (K.mul (K.mul (castN K (Kc + 2)) (castN K (Kc + 2))) (K.mul (castN K (Kc + 2)) (castN K (Kc + 2))))
        (K.mul (K.mul (ivn K inv kz p) (ivn K inv kz p)) (K.mul (ivn K inv kz p) (ivn K inv kz p)))) :=
      O.nn_mul _ _ (O.nn_mul _ _ (O.nn_sq _) (O.nn_sq _)) (O.nn_mul _ _ (O.nn_sq _) (O.nn_sq _))
    have m := le_mul_nn K O.toCone _ _ _ cn hiv
    unfold nu3; rw [hl]
    show Le K O.toCone (K.mul (K.mul (K.mul (K.mul (ivn K inv kz p) (ivn K inv kz p)) (ivn K inv kz p))
        (K.mul (castN K (Kc + 2)) (castN K (Kc + 2))))
        (K.mul (K.mul (K.mul (ivn K inv kz p) (ivn K inv kz p)) (ivn K inv kz p)) (K.mul (castN K (Kc + 2)) (castN K (Kc + 2)))))
      (K.mul (K.mul (K.mul (castN K (Kc + 2)) (castN K (Kc + 2))) (K.mul (K.mul (castN K (Kc + 2)) (castN K (Kc + 2)))
          (K.mul (iv K inv (Kc + 2)) (iv K inv (Kc + 2)))))
        (K.mul (K.mul (ivn K inv kz p) (ivn K inv kz p)) (K.mul (ivn K inv kz p) (ivn K inv kz p))))
    have e1 : K.mul (K.mul (K.mul (K.mul (ivn K inv kz p) (ivn K inv kz p)) (ivn K inv kz p))
        (K.mul (castN K (Kc + 2)) (castN K (Kc + 2))))
        (K.mul (K.mul (K.mul (ivn K inv kz p) (ivn K inv kz p)) (ivn K inv kz p)) (K.mul (castN K (Kc + 2)) (castN K (Kc + 2))))
        = K.mul (K.mul (K.mul (K.mul (castN K (Kc + 2)) (castN K (Kc + 2))) (K.mul (castN K (Kc + 2)) (castN K (Kc + 2))))
          (K.mul (K.mul (ivn K inv kz p) (ivn K inv kz p)) (K.mul (ivn K inv kz p) (ivn K inv kz p))))
          (K.mul (ivn K inv kz p) (ivn K inv kz p)) := by lib_ring
    have e2 : K.mul (K.mul (K.mul (castN K (Kc + 2)) (castN K (Kc + 2))) (K.mul (K.mul (castN K (Kc + 2)) (castN K (Kc + 2)))
          (K.mul (iv K inv (Kc + 2)) (iv K inv (Kc + 2)))))
        (K.mul (K.mul (ivn K inv kz p) (ivn K inv kz p)) (K.mul (ivn K inv kz p) (ivn K inv kz p)))
        = K.mul (K.mul (K.mul (K.mul (castN K (Kc + 2)) (castN K (Kc + 2))) (K.mul (castN K (Kc + 2)) (castN K (Kc + 2))))
          (K.mul (K.mul (ivn K inv kz p) (ivn K inv kz p)) (K.mul (ivn K inv kz p) (ivn K inv kz p))))
          (K.mul (iv K inv (Kc + 2)) (iv K inv (Kc + 2))) := by lib_ring
    rw [e1, e2]; exact m
  rw [lsum_mul_left, lsum_norms K (f4 K inv) kz] at hH
  rw [eL, lsum_norms K (f2 K inv) kz]
  have L1 := lattice_bound2 K O inv mul_inv h10 ((l.filter (low kz Kc)).map kz)
    (pw_map kz _ (pw_filter _ _ l hd))
    (fun k hk => by have ⟨q, _, e⟩ := mem_map_inv kz k _ hk; rw [← e]; exact h1 q) Kc
    (fun k hk => by
      have ⟨q, hq, e⟩ := mem_map_inv kz k _ hk
      rw [← e]; exact low_nrm kz Kc q (low_true kz Kc l q hq))
  have L2 := lattice_tail4 K O inv mul_inv h10 ((l.filter (fun p => !(low kz Kc p))).map kz)
    (pw_map kz _ (pw_filter _ _ l hd)) (Kc + 1) M hKM
    (fun k hk => by
      have ⟨q, hq, e⟩ := mem_map_inv kz k _ hk
      rw [← e]; exact high_nrm kz Kc q (low_false kz Kc l q hq))
    (fun k hk => by have ⟨q, hq, e⟩ := mem_map_inv kz k _ hk; rw [← e]; exact hM q (mem_filter_inv _ q l hq).2)
  have L2'' := le_mul_nn K O.toCone _ _ _ c4 L2
  have L2' : Le K O.toCone (K.mul (K.mul (K.mul (castN K (Kc + 2)) (castN K (Kc + 2))) (K.mul (K.mul (castN K (Kc + 2)) (castN K (Kc + 2)))
        (K.mul (iv K inv (Kc + 2)) (iv K inv (Kc + 2))))) (AbelCount.lsumN K (f4 K inv) (norms ((l.filter (fun p => !(low kz Kc p))).map kz))))
      (K.mul (K.mul (K.mul (castN K (Kc + 2)) (castN K (Kc + 2))) (K.mul (K.mul (castN K (Kc + 2)) (castN K (Kc + 2)))
        (K.mul (iv K inv (Kc + 2)) (iv K inv (Kc + 2))))) (K.mul (castN K 108) (iv K inv (Kc + 2)))) := L2''
  have ec : K.mul (castN K (Kc + 2)) (iv K inv (Kc + 2)) = K.one := iv_mul K O inv mul_inv h10 (Kc + 1)
  have e3 : K.mul (K.mul (K.mul (castN K (Kc + 2)) (castN K (Kc + 2))) (K.mul (K.mul (castN K (Kc + 2)) (castN K (Kc + 2)))
        (K.mul (iv K inv (Kc + 2)) (iv K inv (Kc + 2))))) (K.mul (castN K 108) (iv K inv (Kc + 2)))
      = K.mul (castN K 108) (castN K (Kc + 2)) := by
    calc _ = K.mul (K.mul (castN K 108) (castN K (Kc + 2)))
          (K.mul (K.mul (castN K (Kc + 2)) (iv K inv (Kc + 2))) (K.mul (K.mul (castN K (Kc + 2)) (iv K inv (Kc + 2)))
            (K.mul (castN K (Kc + 2)) (iv K inv (Kc + 2))))) := by lib_ring
      _ = _ := by rw [ec]; lib_ring
  rw [e3] at L2'
  exact le_add' K O.toCone _ _ _ _ L1 (le_trans' K O.toCone _ _ _ hH L2')

include mul_inv h10 in
/-- Σν²w ≤ 3·(108(Kc+1)³ + 108(Kc+2)³) for the cubic cut -/
theorem nu3_sum1 (l : List I) (kz : I → Mode) (hd : List.Pairwise (fun a b => kz a ≠ kz b) l)
    (h1 : ∀ p, 1 ≤ nrm (kz p)) (Kc M : Nat) (hKM : Kc + 1 ≤ M) (hM : ∀ p, p ∈ l → nrm (kz p) ≤ M + 1) :
    Le K O.toCone (lsum K (fun p => K.mul (K.mul (nu3 K inv kz Kc p) (nu3 K inv kz Kc p)) (wt K kz p)) l)
      (K.mul (castN K 3) (K.add (K.mul (K.mul (castN K (Kc + 1)) (castN K (Kc + 1))) (K.mul (castN K 108) (castN K (Kc + 1))))
        (K.mul (K.mul (castN K (Kc + 2)) (castN K (Kc + 2))) (K.mul (castN K 108) (castN K (Kc + 2)))))) := by
  have e3c : castN K 3 = K.add K.one (K.add K.one (K.add K.one K.zero)) := rfl
  rw [lsum_split K (low kz Kc)]
  -- low part: ν²w ≤ 3(Kc+1)²·|k|_∞^{-2}
  have hL : Le K O.toCone
      (lsum K (fun p => K.mul (K.mul (nu3 K inv kz Kc p) (nu3 K inv kz Kc p)) (wt K kz p)) (l.filter (low kz Kc)))
      (lsum K (fun p => K.mul (K.mul (castN K 3) (K.mul (castN K (Kc + 1)) (castN K (Kc + 1)))) (f2 K inv (nrm (kz p))))
        (l.filter (low kz Kc))) := by
    apply lsum_le_on K O
    intro p hp
    have hl := low_true kz Kc l p hp
    have c1 := RatModel.cast_mono K O.toCone _ _ (low_nrm kz Kc p hl)
    have s2 := EnstLocal.sq_mono K O _ _ (RatModel.nn_cast K O.toCone _) (RatModel.nn_cast K O.toCone _) c1
    have w3 := dot_le_3nrm K O (kz p)
    have s3 := le_add' K O.toCone _ _ _ _ (le_add' K O.toCone _ _ _ _ s2 s2) s2
    have w3' := le_trans' K O.toCone _ _ _ w3 s3
    have m := le_mul_nn K O.toCone (K.mul (ivn K inv kz p) (ivn K inv kz p)) _ _ (O.nn_sq _) w3'
    unfold nu3; rw [hl]
    show Le K O.toCone (K.mul (K.mul (ivn K inv kz p) (ivn K inv kz p)) (dot3 K (kvec K (kz p)) (kvec K (kz p))))
      (K.mul (K.mul (castN K 3) (K.mul (castN K (Kc + 1)) (castN K (Kc + 1)))) (K.mul (ivn K inv kz p) (ivn K inv kz p)))
    have e : K.mul (K.mul (castN K 3) (K.mul (castN K (Kc + 1)) (castN K (Kc + 1)))) (K.mul (ivn K inv kz p) (ivn K inv kz p))
        = K.mul (K.mul (ivn K inv kz p) (ivn K inv kz p))
          (K.add (K.add (K.mul (castN K (Kc + 1)) (castN K (Kc + 1))) (K.mul (castN K (Kc + 1)) (castN K (Kc + 1))))
            (K.mul (castN K (Kc + 1)) (castN K (Kc + 1)))) := by rw [e3c]; lib_ring
    rw [e]; exact m
  -- high part: ν²w ≤ 3(Kc+2)⁴·|k|_∞^{-4}
  have hH : Le K O.toCone
      (lsum K (fun p => K.mul (K.mul (nu3 K inv kz Kc p) (nu3 K inv kz Kc p)) (wt K kz p)) (l.filter (fun p => !(low kz Kc p))))
      (lsum K (fun p => K.mul (K.mul (castN K 3) (K.mul (K.mul (castN K (Kc + 2)) (castN K (Kc + 2)))
          (K.mul (castN K (Kc + 2)) (castN K (Kc + 2))))) (f4 K inv (nrm (kz p)))) (l.filter (fun p => !(low kz Kc p)))) := by
    apply lsum_le_on K O
    intro p hp
    have hl := low_false kz Kc l p hp
    have e := nn_ivn K O inv mul_inv h10 kz h1 p
    have w3 := dot_le_3nrm K O (kz p)
    have cn : O.nn (K.mul (K.mul (K.mul (K.mul (ivn K inv kz p) (ivn K inv kz p)) (ivn K inv kz p))
        (K.mul (castN K (Kc + 2)) (castN K (Kc + 2))))
        (K.mul (K.mul (K.mul (ivn K inv kz p) (ivn K inv kz p)) (ivn K inv kz p)) (K.mul (castN K (Kc + 2)) (castN K (Kc + 2))))) :=
      O.nn_sq _
    have m := le_mul_nn K O.toCone _ _ _ cn w3
    unfold nu3; rw [hl]
    show Le K O.toCone (K.mul (K.mul (K.mul (K.mul (K.mul (ivn K inv kz p) (ivn K inv kz p)) (ivn K inv kz p))
        (K.mul (castN K (Kc + 2)) (castN K (Kc + 2))))
        (K.mul (K.mul (K.mul (ivn K inv kz p) (ivn K inv kz p)) (ivn K inv kz p)) (K.mul (castN K (Kc + 2)) (castN K (Kc + 2)))))
        (dot3 K (kvec K (kz p)) (kvec K (kz p))))
      (K.mul (K.mul (castN K 3) (K.mul (K.mul (castN K (Kc + 2)) (castN K (Kc + 2))) (K.mul (castN K (Kc + 2)) (castN K (Kc + 2)))))
        (K.mul (K.mul (ivn K inv kz p) (ivn K inv kz p)) (K.mul (ivn K inv kz p) (ivn K inv kz p))))
    refine le_trans' K O.toCone _ _ _ m ?_
    apply le_eq K O
    calc _ = K.mul (K.mul (K.mul (castN K 3) (K.mul (K.mul (castN K (Kc + 2)) (castN K (Kc + 2))) (K.mul (castN K (Kc + 2)) (castN K (Kc + 2)))))
            (K.mul (K.mul (ivn K inv kz p) (ivn K inv kz p)) (K.mul (ivn K inv kz p) (ivn K inv kz p))))
          (K.mul (K.mul (nn K kz p) (ivn K inv kz p)) (K.mul (nn K kz p) (ivn K inv kz p))) := by
          show K.mul _ (K.add (K.add (K.mul (castN K (nrm (kz p))) (castN K (nrm (kz p))))
              (K.mul (castN K (nrm (kz p))) (castN K (nrm (kz p))))) (K.mul (castN K (nrm (kz p))) (castN K (nrm (kz p))))) = _
          rw [e3c]; unfold nn; lib_ring
      _ = _ := by rw [e]; lib_ring
  rw [lsum_mul_left, lsum_norms K (f2 K inv) kz] at hL
  rw [lsum_mul_left, lsum_norms K (f4 K inv) kz] at hH
  have L1 := lattice_bound2 K O inv mul_inv h10 ((l.filter (low kz Kc)).map kz)
    (pw_map kz _ (pw_filter _ _ l hd))
    (fun k hk => by have ⟨q, _, e⟩ := mem_map_inv kz k _ hk; rw [← e]; exact h1 q) Kc
    (fun k hk => by
      have ⟨q, hq, e⟩ := mem_map_inv kz k _ hk
      rw [← e]; exact low_nrm kz Kc q (low_true kz Kc l q hq))
  have L2 := lattice_tail4 K O inv mul_inv h10 ((l.filter (fun p => !(low kz Kc p))).map kz)
    (pw_map kz _ (pw_filter _ _ l hd)) (Kc + 1) M hKM
    (fun k hk => by
      have ⟨q, hq, e⟩ := mem_map_inv kz k _ hk
      rw [← e]; exact high_nrm kz Kc q (low_false kz Kc l q hq))
    (fun k hk => by have ⟨q, hq, e⟩ := mem_map_inv kz k _ hk; rw [← e]; exact hM q (mem_filter_inv _ q l hq).2)
  have n3 : O.nn (castN K 3) := RatModel.nn_cast K O.toCone 3
  have L1' := le_mul_nn K O.toCone (K.mul (castN K 3) (K.mul (castN K (Kc + 1)) (castN K (Kc + 1)))) _ _
    (O.nn_mul _ _ n3 (O.nn_sq _)) L1
  have L2'' := le_mul_nn K O.toCone (K.mul (castN K 3) (K.mul (K.mul (castN K (Kc + 2)) (castN K (Kc + 2)))
      (K.mul (castN K (Kc + 2)) (castN K (Kc + 2))))) _ _
    (O.nn_mul _ _ n3 (O.nn_mul _ _ (O.nn_sq _) (O.nn_sq _))) L2
  have L2' : Le K O.toCone (K.mul (K.mul (castN K 3) (K.mul (K.mul (castN K (Kc + 2)) (castN K (Kc + 2)))
      (K.mul (castN K (Kc + 2)) (castN K (Kc + 2))))) (AbelCount.lsumN K (f4 K inv) (norms ((l.filter (fun p => !(low kz Kc p))).map kz))))
      (K.mul (K.mul (castN K 3) (K.mul (K.mul (castN K (Kc + 2)) (castN K (Kc + 2)))
      (K.mul (castN K (Kc + 2)) (castN K (Kc + 2))))) (K.mul (castN K 108) (iv K inv (Kc + 2)))) := L2''
  have ec : K.mul (castN K (Kc + 2)) (iv K inv (Kc + 2)) = K.one := iv_mul K O inv mul_inv h10 (Kc + 1)
  have e4 : K.mul (K.mul (castN K 3) (K.mul (K.mul (castN K (Kc + 2)) (castN K (Kc + 2))) (K.mul (castN K (Kc + 2)) (castN K (Kc + 2)))))
        (K.mul (castN K 108) (iv K inv (Kc + 2)))
      = K.mul (castN K 3) (K.mul (K.mul (castN K (Kc + 2)) (castN K (Kc + 2))) (K.mul (castN K 108) (castN K (Kc + 2)))) := by
    calc _ = K.mul (K.mul (castN K 3) (K.mul (K.mul (castN K (Kc + 2)) (castN K (Kc + 2))) (K.mul (castN K 108) (castN K (Kc + 2)))))
          (K.mul (castN K (Kc + 2)) (iv K inv (Kc + 2))) := by lib_ring
      _ = _ := by rw [ec]; lib_ring
  rw [e4] at L2'
  have T := le_add' K O.toCone _ _ _ _ (le_trans' K O.toCone _ _ _ hL L1') (le_trans' K O.toCone _ _ _ hH L2')
  refine le_trans' K O.toCone _ _ _ T ?_
  apply le_eq K O
  lib_ring

include mul_inv h10 in
/-- **The H¹-weighted force bound with the cubic cut.** -/
theorem force_agmonW (l : List I) (kz : I → Mode) (hd : List.Pairwise (fun a b => kz a ≠ kz b) l)
    (h1 : ∀ p, 1 ≤ nrm (kz p)) (ι : R) (u v : I → V3 R) (Kc M : Nat) (hKM : Kc + 1 ≤ M)
    (hM : ∀ p, p ∈ l → nrm (kz p) ≤ M + 1) :
    Le K O.toCone (lsum K (fun r => K.mul (wt K kz r) (dot3 K (Nl K l (TriadSparse.delta K kz) ι (kv K kz) u v r)
        (Nl K l (TriadSparse.delta K kz) ι (kv K kz) u v r))) l)
      (K.mul (K.mul ι ι)
        (K.mul (K.add (lsum K (fun p => K.mul (wt K kz p) (dot3 K (u p) (u p))) l)
            (K.mul (K.mul (K.mul (iv K inv (Kc + 2)) (iv K inv (Kc + 2))) (K.mul (iv K inv (Kc + 2)) (iv K inv (Kc + 2))))
              (lsum K (fun p => K.mul (K.mul (K.mul (wt K kz p) (wt K kz p)) (wt K kz p)) (dot3 K (u p) (u p))) l)))
          (K.add
            (K.mul (K.add K.one K.one) (K.mul
              (K.mul (castN K 3) (K.add (K.mul (K.mul (castN K (Kc + 1)) (castN K (Kc + 1))) (K.mul (castN K 108) (castN K (Kc + 1))))
                (K.mul (K.mul (castN K (Kc + 2)) (castN K (Kc + 2))) (K.mul (castN K 108) (castN K (Kc + 2))))))
              (lsum K (fun q => K.mul (wt K kz q) (dot3 K (v q) (v q))) l)))
            (K.mul (K.add K.one K.one) (K.mul
              (K.add (K.mul (castN K 108) (castN K (Kc + 1))) (K.mul (castN K 108) (castN K (Kc + 2))))
              (lsum K (fun q => K.mul (K.mul (wt K kz q) (wt K kz q)) (dot3 K (v q) (v q))) l)))))) := by
  have T := total_sobW K O.toCone l (TriadSparse.delta K kz) ι (kv K kz) u v
    (mu3 K inv kz Kc) (nu3 K inv kz Kc) (wt K kz) (fun r => dot3_nn K O.toCone _)
    (fun p q r => delta_tri K O.toCone kz p q r) (delta01 K kz) (munu3c K O inv mul_inv h10 kz h1 Kc)
    (fun p r => TriadSparse.spA K O.toCone kz l hd p r) (fun p q => TriadSparse.spB K O.toCone kz l hd p q)
  refine le_trans' K O.toCone _ _ _ T ?_
  have hH := lsum_le K O.toCone _ _ (fun p => mu3_le K O inv kz Kc u p) l
  rw [lsum_add, lsum_mul_left] at hH
  have e : lsum K (fun p => lsum K (fun q => K.mul (K.mul (K.mul (nu3 K inv kz Kc p) (nu3 K inv kz Kc p))
        (K.add (K.add (wt K kz p) (wt K kz p)) (K.add (wt K kz q) (wt K kz q))))
        (K.mul (dot3 K (kv K kz q) (kv K kz q)) (dot3 K (v q) (v q)))) l) l
      = K.add (K.mul (K.add K.one K.one) (K.mul (lsum K (fun p => K.mul (K.mul (nu3 K inv kz Kc p) (nu3 K inv kz Kc p)) (wt K kz p)) l)
            (lsum K (fun q => K.mul (wt K kz q) (dot3 K (v q) (v q))) l)))
          (K.mul (K.add K.one K.one) (K.mul (lsum K (fun p => K.mul (nu3 K inv kz Kc p) (nu3 K inv kz Kc p)) l)
            (lsum K (fun q => K.mul (K.mul (wt K kz q) (wt K kz q)) (dot3 K (v q) (v q))) l))) := by
    rw [← double_factor, ← double_factor, ← lsum_add]
    apply lsum_ext; intro p
    rw [← lsum_add]
    apply lsum_ext; intro q
    show K.mul (K.mul (K.mul (nu3 K inv kz Kc p) (nu3 K inv kz Kc p))
        (K.add (K.add (wt K kz p) (wt K kz p)) (K.add (wt K kz q) (wt K kz q))))
        (K.mul (wt K kz q) (dot3 K (v q) (v q)))
      = K.add (K.mul (K.add K.one K.one) (K.mul (K.mul (K.mul (nu3 K inv kz Kc p) (nu3 K inv kz Kc p)) (wt K kz p))
          (K.mul (wt K kz q) (dot3 K (v q) (v q)))))
        (K.mul (K.add K.one K.one) (K.mul (K.mul (nu3 K inv kz Kc p) (nu3 K inv kz Kc p))
          (K.mul (K.mul (wt K kz q) (wt K kz q)) (dot3 K (v q) (v q)))))
    lib_ring
  rw [e]
  have two : O.nn (K.add K.one K.one) := O.nn_add _ _ (RatModel.nn_one' K O.toCone) (RatModel.nn_one' K O.toCone)
  have hZv : O.nn (lsum K (fun q => K.mul (wt K kz q) (dot3 K (v q) (v q))) l) :=
    lsum_nn K O.toCone _ (fun q => O.nn_mul _ _ (dot3_nn K O.toCone _) (dot3_nn K O.toCone _)) l
  have hAv : O.nn (lsum K (fun q => K.mul (K.mul (wt K kz q) (wt K kz q)) (dot3 K (v q) (v q))) l) :=
    lsum_nn K O.toCone _ (fun q => O.nn_mul _ _ (O.nn_sq _) (dot3_nn K O.toCone _)) l
  have S1 := nu3_sum1 K O inv mul_inv h10 l kz hd h1 Kc M hKM hM
  have S0 := nu3_sum0 K O inv mul_inv h10 l kz hd h1 Kc M hKM hM
  have hD := le_add' K O.toCone _ _ _ _ (le_mul_nn K O.toCone _ _ _ two (le_mul_right' K O.toCone S1 hZv))
    (le_mul_nn K O.toCone _ _ _ two (le_mul_right' K O.toCone S0 hAv))
  have hHn : O.nn (lsum K (fun p => K.mul (K.mul (mu3 K inv kz Kc p) (mu3 K inv kz Kc p)) (dot3 K (u p) (u p))) l) :=
    lsum_nn K O.toCone _ (fun p => O.nn_mul _ _ (O.nn_sq _) (dot3_nn K O.toCone _)) l
  have n108 := RatModel.nn_cast K O.toCone 108
  have nC := fun n => RatModel.nn_cast K O.toCone n
  have hDn : O.nn (K.add
      (K.mul (K.add K.one K.one) (K.mul
        (K.mul (castN K 3) (K.add (K.mul (K.mul (castN K (Kc + 1)) (castN K (Kc + 1))) (K.mul (castN K 108) (castN K (Kc + 1))))
          (K.mul (K.mul (castN K (Kc + 2)) (castN K (Kc + 2))) (K.mul (castN K 108) (castN K (Kc + 2))))))
        (lsum K (fun q => K.mul (wt K kz q) (dot3 K (v q) (v q))) l)))
      (K.mul (K.add K.one K.one) (K.mul
        (K.add (K.mul (castN K 108) (castN K (Kc + 1))) (K.mul (castN K 108) (castN K (Kc + 2))))
        (lsum K (fun q => K.mul (K.mul (wt K kz q) (wt K kz q)) (dot3 K (v q) (v q))) l)))) :=
    O.nn_add _ _
      (O.nn_mul _ _ two (O.nn_mul _ _ (O.nn_mul _ _ (nC 3) (O.nn_add _ _ (O.nn_mul _ _ (O.nn_sq _) (O.nn_mul _ _ n108 (nC _)))
        (O.nn_mul _ _ (O.nn_sq _) (O.nn_mul _ _ n108 (nC _))))) hZv))
      (O.nn_mul _ _ two (O.nn_mul _ _ (O.nn_add _ _ (O.nn_mul _ _ n108 (nC _)) (O.nn_mul _ _ n108 (nC _))) hAv))
  have hB := WeightedCS.le_mul_both' K O.toCone _ _ _ _ hHn hDn hH hD
  exact le_mul_nn K O.toCone _ _ _ (O.nn_sq ι) hB

end Gen

end ForceAgmonW

#print axioms ForceAgmonW.lsum_le_on
#print axioms ForceAgmonW.le_eq
#print axioms ForceAgmonW.munu3c
#print axioms ForceAgmonW.mu3_le
#print axioms ForceAgmonW.ivn_sq_le
#print axioms ForceAgmonW.low_true
#print axioms ForceAgmonW.low_false
#print axioms ForceAgmonW.low_nrm
#print axioms ForceAgmonW.high_nrm
#print axioms ForceAgmonW.nu3_sum0
#print axioms ForceAgmonW.nu3_sum1
#print axioms ForceAgmonW.force_agmonW

ns/ForceBilin.lean

lean_all/ns/ForceBilin.lean

/-
  ns/ForceBilin.lean — T37 cycle 918 (goal G614): the Galerkin force N(u, v) of GalerkinForce is bilinear, over any
  commutative ring:
    Nl_add_left    N(u₁ + u₂, v) = N(u₁, v) + N(u₂, v)
    Nl_add_right   N(u, v₁ + v₂) = N(u, v₁) + N(u, v₂)
    Nl_diff        N(a, a) = N(b, b) + N(a − b, a) + N(b, a − b)   (with a = b + d)
  so the difference of two forces is controlled by ForceBound.force_mode applied to N(d, a) and N(b, d): the first
  step of continuous dependence (discrete uniqueness/stability) for the projected 3D scheme.

  Lean 4 core plus ns/GalerkinForce (Nl, coef), NSChain (lsum lemmas), lib/RingTac.
-/
import ns.GalerkinForce

namespace ForceBilin
open DetChain NSChain LibRing FourierGalerkin GalerkinForce

section Gen
variable {R : Type} (K : CRing R) {I : Type}

def addF (u w : I → V3 R) : I → V3 R := fun p => add3 K (u p) (w p)

theorem lsum2_add (l : List I) (f g : I → I → R) :
    lsum K (fun p => lsum K (fun q => K.add (f p q) (g p q)) l) l
      = K.add (lsum K (fun p => lsum K (fun q => f p q) l) l) (lsum K (fun p => lsum K (fun q => g p q) l) l) := by
  rw [← lsum_add]
  apply lsum_ext; intro p
  exact lsum_add K _ _ l

theorem v3_ext (a b : V3 R) (hx : a.x = b.x) (hy : a.y = b.y) (hz : a.z = b.z) : a = b := by
  cases a; cases b
  cases hx; cases hy; cases hz; rfl

theorem Nl_add_left (l : List I) (δ : I → I → I → R) (ι : R) (k u₁ u₂ v : I → V3 R) (r : I) :
    Nl K l δ ι k (addF K u₁ u₂) v r = add3 K (Nl K l δ ι k u₁ v r) (Nl K l δ ι k u₂ v r) := by
  have hc : ∀ p q, coef K δ ι k (addF K u₁ u₂) p q r = K.add (coef K δ ι k u₁ p q r) (coef K δ ι k u₂ p q r) := by
    intro p q
    show K.mul (δ p q r) (K.mul ι (dot3 K (add3 K (u₁ p) (u₂ p)) (k q)))
      = K.add (K.mul (δ p q r) (K.mul ι (dot3 K (u₁ p) (k q)))) (K.mul (δ p q r) (K.mul ι (dot3 K (u₂ p) (k q))))
    rw [dot3_comm K (add3 K (u₁ p) (u₂ p)), dot3_add, dot3_comm K (k q), dot3_comm K (k q)]
    generalize dot3 K (u₁ p) (k q) = e1; generalize dot3 K (u₂ p) (k q) = e2
    lib_ring
  apply v3_ext
  · show lsum K (fun p => lsum K (fun q => K.mul (coef K δ ι k (addF K u₁ u₂) p q r) (v q).x) l) l
      = K.add (lsum K (fun p => lsum K (fun q => K.mul (coef K δ ι k u₁ p q r) (v q).x) l) l)
        (lsum K (fun p => lsum K (fun q => K.mul (coef K δ ι k u₂ p q r) (v q).x) l) l)
    rw [← lsum2_add]; apply lsum_ext; intro p; apply lsum_ext; intro q; rw [hc]; lib_ring
  · show lsum K (fun p => lsum K (fun q => K.mul (coef K δ ι k (addF K u₁ u₂) p q r) (v q).y) l) l
      = K.add (lsum K (fun p => lsum K (fun q => K.mul (coef K δ ι k u₁ p q r) (v q).y) l) l)
        (lsum K (fun p => lsum K (fun q => K.mul (coef K δ ι k u₂ p q r) (v q).y) l) l)
    rw [← lsum2_add]; apply lsum_ext; intro p; apply lsum_ext; intro q; rw [hc]; lib_ring
  · show lsum K (fun p => lsum K (fun q => K.mul (coef K δ ι k (addF K u₁ u₂) p q r) (v q).z) l) l
      = K.add (lsum K (fun p => lsum K (fun q => K.mul (coef K δ ι k u₁ p q r) (v q).z) l) l)
        (lsum K (fun p => lsum K (fun q => K.mul (coef K δ ι k u₂ p q r) (v q).z) l) l)
    rw [← lsum2_add]; apply lsum_ext; intro p; apply lsum_ext; intro q; rw [hc]; lib_ring

theorem Nl_add_right (l : List I) (δ : I → I → I → R) (ι : R) (k u v₁ v₂ : I → V3 R) (r : I) :
    Nl K l δ ι k u (addF K v₁ v₂) r = add3 K (Nl K l δ ι k u v₁ r) (Nl K l δ ι k u v₂ r) := by
  apply v3_ext
  · show lsum K (fun p => lsum K (fun q => K.mul (coef K δ ι k u p q r) (K.add (v₁ q).x (v₂ q).x)) l) l
      = K.add (lsum K (fun p => lsum K (fun q => K.mul (coef K δ ι k u p q r) (v₁ q).x) l) l)
        (lsum K (fun p => lsum K (fun q => K.mul (coef K δ ι k u p q r) (v₂ q).x) l) l)
    rw [← lsum2_add]; apply lsum_ext; intro p; apply lsum_ext; intro q; exact K.distrib _ _ _
  · show lsum K (fun p => lsum K (fun q => K.mul (coef K δ ι k u p q r) (K.add (v₁ q).y (v₂ q).y)) l) l
      = K.add (lsum K (fun p => lsum K (fun q => K.mul (coef K δ ι k u p q r) (v₁ q).y) l) l)
        (lsum K (fun p => lsum K (fun q => K.mul (coef K δ ι k u p q r) (v₂ q).y) l) l)
    rw [← lsum2_add]; apply lsum_ext; intro p; apply lsum_ext; intro q; exact K.distrib _ _ _
  · show lsum K (fun p => lsum K (fun q => K.mul (coef K δ ι k u p q r) (K.add (v₁ q).z (v₂ q).z)) l) l
      = K.add (lsum K (fun p => lsum K (fun q => K.mul (coef K δ ι k u p q r) (v₁ q).z) l) l)
        (lsum K (fun p => lsum K (fun q => K.mul (coef K δ ι k u p q r) (v₂ q).z) l) l)
    rw [← lsum2_add]; apply lsum_ext; intro p; apply lsum_ext; intro q; exact K.distrib _ _ _

/-- With a = b + d: N(a, a) = N(b, b) + N(d, a) + N(b, d). -/
theorem Nl_diff (l : List I) (δ : I → I → I → R) (ι : R) (k b d : I → V3 R) (r : I) :
    Nl K l δ ι k (addF K b d) (addF K b d) r
      = add3 K (add3 K (Nl K l δ ι k b b r) (Nl K l δ ι k d (addF K b d) r)) (Nl K l δ ι k b d r) := by
  rw [Nl_add_left, Nl_add_right K l δ ι k b b d]
  generalize Nl K l δ ι k b b r = A; generalize Nl K l δ ι k b d r = B; generalize Nl K l δ ι k d (addF K b d) r = C
  apply v3_ext
  · show K.add (K.add A.x B.x) C.x = K.add (K.add A.x C.x) B.x
    lib_ring
  · show K.add (K.add A.y B.y) C.y = K.add (K.add A.y C.y) B.y
    lib_ring
  · show K.add (K.add A.z B.z) C.z = K.add (K.add A.z C.z) B.z
    lib_ring

end Gen

end ForceBilin

#print axioms ForceBilin.lsum2_add
#print axioms ForceBilin.v3_ext
#print axioms ForceBilin.Nl_add_left
#print axioms ForceBilin.Nl_add_right
#print axioms ForceBilin.Nl_diff

ns/ForceBound.lean

lean_all/ns/ForceBound.lean

/-
  ns/ForceBound.lean — T37 cycle 901 (goal G592c, first piece): Cauchy–Schwarz in the two forms the Galerkin force
  bound needs, over any commutative ring with a nonnegative cone (ℚ included):
    dot3_cs     (a·b)² ≤ |a|² |b|²                                      (3-vectors)
    double_cs   (Σ_{p∈l} Σ_{q∈l} f g)² ≤ (Σ_p Σ_q f²)(Σ_p Σ_q g²)        (double sums over a mode list)
  via lib/CauchySchwarz on explicit lists of pairs.

  Lean 4 core plus lib/CauchySchwarz (sA, sB, sC, cauchy_schwarz), ns/QuadBound (sA_app, sB_app), NSChain (lsum, Le).
-/
import ns.QuadBound
import ns.FourierGalerkin
import ns.GalerkinForce

namespace ForceBound
open DetChain NSChain LibRing LibCS FourierGalerkin QuadBound GalerkinForce DyadicLarge

section Gen
variable {R : Type} (K : CRing R) (P : Cone K) {I : Type}

def pos : Pos K := ⟨P.nn, P.nn_sq, P.nn_add, P.nn_mul, P.nn_zero⟩

theorem le_of_cs (A B C : R) (h : P.nn (sub K (K.mul A B) (K.mul C C))) : Le K P (K.mul C C) (K.mul A B) :=
  ⟨_, h, by unfold sub; lib_ring⟩

theorem dot3_cs (a b : V3 R) : Le K P (K.mul (dot3 K a b) (dot3 K a b)) (K.mul (dot3 K a a) (dot3 K b b)) := by
  have cs := cauchy_schwarz K (pos K P) [(a.x, b.x), (a.y, b.y), (a.z, b.z)]
  have eA : sA K [(a.x, b.x), (a.y, b.y), (a.z, b.z)] = dot3 K a a := by
    show K.add (K.mul a.x a.x) (K.add (K.mul a.y a.y) (K.add (K.mul a.z a.z) K.zero)) = dot3 K a a
    unfold dot3; lib_ring
  have eB : sB K [(a.x, b.x), (a.y, b.y), (a.z, b.z)] = dot3 K b b := by
    show K.add (K.mul b.x b.x) (K.add (K.mul b.y b.y) (K.add (K.mul b.z b.z) K.zero)) = dot3 K b b
    unfold dot3; lib_ring
  have eC : sC K [(a.x, b.x), (a.y, b.y), (a.z, b.z)] = dot3 K a b := by
    show K.add (K.mul a.x b.x) (K.add (K.mul a.y b.y) (K.add (K.mul a.z b.z) K.zero)) = dot3 K a b
    unfold dot3; lib_ring
  rw [eA, eB, eC] at cs
  exact le_of_cs K P _ _ _ cs

def prow (f g : I → I → R) (p : I) : List I → List (R × R)
  | [] => []
  | q :: m => (f p q, g p q) :: prow f g p m

def pgrid (f g : I → I → R) (l : List I) : List I → List (R × R)
  | [] => []
  | p :: m => prow f g p l ++ pgrid f g l m

theorem sA_prow (f g : I → I → R) (p : I) : ∀ m, sA K (prow f g p m) = lsum K (fun q => K.mul (f p q) (f p q)) m
  | [] => rfl
  | q :: m => by
    show K.add (K.mul (f p q) (f p q)) (sA K (prow f g p m)) = _
    rw [sA_prow f g p m]; rfl

theorem sB_prow (f g : I → I → R) (p : I) : ∀ m, sB K (prow f g p m) = lsum K (fun q => K.mul (g p q) (g p q)) m
  | [] => rfl
  | q :: m => by
    show K.add (K.mul (g p q) (g p q)) (sB K (prow f g p m)) = _
    rw [sB_prow f g p m]; rfl

theorem sC_prow (f g : I → I → R) (p : I) : ∀ m, sC K (prow f g p m) = lsum K (fun q => K.mul (f p q) (g p q)) m
  | [] => rfl
  | q :: m => by
    show K.add (K.mul (f p q) (g p q)) (sC K (prow f g p m)) = _
    rw [sC_prow f g p m]; rfl

theorem sC_app : ∀ l1 l2 : List (R × R), sC K (l1 ++ l2) = K.add (sC K l1) (sC K l2)
  | [], l2 => (K.zero_add _).symm
  | p :: l1, l2 => by
    show K.add (K.mul p.1 p.2) (sC K (l1 ++ l2)) = K.add (K.add (K.mul p.1 p.2) (sC K l1)) (sC K l2)
    rw [sC_app l1 l2, K.add_assoc]

theorem sA_grid (f g : I → I → R) (l : List I) : ∀ m,
    sA K (pgrid f g l m) = lsum K (fun p => lsum K (fun q => K.mul (f p q) (f p q)) l) m
  | [] => rfl
  | p :: m => by
    show sA K (prow f g p l ++ pgrid f g l m) = _
    rw [sA_app, sA_prow, sA_grid f g l m]; rfl

theorem sB_grid (f g : I → I → R) (l : List I) : ∀ m,
    sB K (pgrid f g l m) = lsum K (fun p => lsum K (fun q => K.mul (g p q) (g p q)) l) m
  | [] => rfl
  | p :: m => by
    show sB K (prow f g p l ++ pgrid f g l m) = _
    rw [sB_app, sB_prow, sB_grid f g l m]; rfl

theorem sC_grid (f g : I → I → R) (l : List I) : ∀ m,
    sC K (pgrid f g l m) = lsum K (fun p => lsum K (fun q => K.mul (f p q) (g p q)) l) m
  | [] => rfl
  | p :: m => by
    show sC K (prow f g p l ++ pgrid f g l m) = _
    rw [sC_app, sC_prow, sC_grid f g l m]; rfl

theorem double_cs (f g : I → I → R) (l : List I) :
    Le K P (K.mul (lsum K (fun p => lsum K (fun q => K.mul (f p q) (g p q)) l) l)
        (lsum K (fun p => lsum K (fun q => K.mul (f p q) (g p q)) l) l))
      (K.mul (lsum K (fun p => lsum K (fun q => K.mul (f p q) (f p q)) l) l)
        (lsum K (fun p => lsum K (fun q => K.mul (g p q) (g p q)) l) l)) := by
  have cs := cauchy_schwarz K (pos K P) (pgrid f g l l)
  rw [sA_grid, sB_grid, sC_grid] at cs
  exact le_of_cs K P _ _ _ cs

theorem sum3_split (l : List I) (Acoef : R) (vx vy vz : I → R) :
    K.add (K.add (K.mul Acoef (lsum K (fun _ => lsum K (fun q => K.mul (vx q) (vx q)) l) l))
      (K.mul Acoef (lsum K (fun _ => lsum K (fun q => K.mul (vy q) (vy q)) l) l)))
      (K.mul Acoef (lsum K (fun _ => lsum K (fun q => K.mul (vz q) (vz q)) l) l))
    = K.mul Acoef (lsum K (fun _ => lsum K (fun q =>
        K.add (K.add (K.mul (vx q) (vx q)) (K.mul (vy q) (vy q))) (K.mul (vz q) (vz q))) l) l) := by
  rw [← K.distrib, ← K.distrib, ← lsum_add, ← lsum_add]
  congr 1
  apply lsum_ext; intro p
  rw [← lsum_add, ← lsum_add]

/-- |N_r|² ≤ (Σ_p Σ_q c(p,q,r)²) · (Σ_p Σ_q |v_q|²). -/
theorem force_sq (l : List I) (δ : I → I → I → R) (ι : R) (k u v : I → V3 R) (r : I) :
    Le K P (dot3 K (Nl K l δ ι k u v r) (Nl K l δ ι k u v r))
      (K.mul (lsum K (fun p => lsum K (fun q => K.mul (coef K δ ι k u p q r) (coef K δ ι k u p q r)) l) l)
        (lsum K (fun _ => lsum K (fun q => dot3 K (v q) (v q)) l) l)) := by
  have cx := double_cs K P (fun p q => coef K δ ι k u p q r) (fun _ q => (v q).x) l
  have cy := double_cs K P (fun p q => coef K δ ι k u p q r) (fun _ q => (v q).y) l
  have cz := double_cs K P (fun p q => coef K δ ι k u p q r) (fun _ q => (v q).z) l
  have s := le_add' K P _ _ _ _ (le_add' K P _ _ _ _ cx cy) cz
  rw [sum3_split K l _ (fun q => (v q).x) (fun q => (v q).y) (fun q => (v q).z)] at s
  exact s

theorem lsum_le (f g : I → R) (h : ∀ i, Le K P (f i) (g i)) : ∀ l : List I, Le K P (lsum K f l) (lsum K g l)
  | [] => le_refl' K P _
  | i :: l => le_add' K P _ _ _ _ (h i) (lsum_le f g h l)

theorem lsum_nn (f : I → R) (h : ∀ i, P.nn (f i)) : ∀ l : List I, P.nn (lsum K f l)
  | [] => P.nn_zero
  | i :: l => P.nn_add _ _ (h i) (lsum_nn f h l)

theorem dot3_nn (a : V3 R) : P.nn (dot3 K a a) :=
  P.nn_add _ _ (P.nn_add _ _ (P.nn_sq _) (P.nn_sq _)) (P.nn_sq _)

/-- c(p,q,r)² ≤ ι² |u_p|² |k_q|² for a triad indicator δ ∈ {0, 1}. -/
theorem coef_sq (δ : I → I → I → R) (ι : R) (k u : I → V3 R) (p q r : I)
    (hδ : δ p q r = K.zero ∨ δ p q r = K.one) :
    Le K P (K.mul (coef K δ ι k u p q r) (coef K δ ι k u p q r))
      (K.mul (K.mul ι ι) (K.mul (dot3 K (u p) (u p)) (dot3 K (k q) (k q)))) := by
  unfold coef
  cases hδ with
  | inl h0 =>
    rw [h0]
    refine ⟨K.mul (K.mul ι ι) (K.mul (dot3 K (u p) (u p)) (dot3 K (k q) (k q))),
      P.nn_mul _ _ (P.nn_sq ι) (P.nn_mul _ _ (dot3_nn K P _) (dot3_nn K P _)), ?_⟩
    generalize dot3 K (u p) (k q) = w
    generalize K.mul (K.mul ι ι) (K.mul (dot3 K (u p) (u p)) (dot3 K (k q) (k q))) = z
    lib_ring
  | inr h1 =>
    rw [h1]
    have cs := dot3_cs K P (u p) (k q)
    have l := le_mul_nn K P (K.mul ι ι) _ _ (P.nn_sq ι) cs
    cases l with
    | intro d hd =>
      refine ⟨d, hd.1, Eq.trans ?_ hd.2⟩
      generalize dot3 K (u p) (k q) = w
      lib_ring

theorem double_factor (l : List I) (c : R) (a b : I → R) :
    lsum K (fun p => lsum K (fun q => K.mul c (K.mul (a p) (b q))) l) l
      = K.mul c (K.mul (lsum K a l) (lsum K b l)) := by
  have e1 : ∀ p, lsum K (fun q => K.mul c (K.mul (a p) (b q))) l = K.mul (K.mul c (a p)) (lsum K b l) := by
    intro p
    rw [← lsum_mul_left]
    apply lsum_ext; intro q; lib_ring
  rw [lsum_ext K _ _ e1 l]
  have e2 : lsum K (fun p => K.mul (K.mul c (a p)) (lsum K b l)) l
      = lsum K (fun p => K.mul (K.mul c (lsum K b l)) (a p)) l := lsum_ext K _ _ (fun p => by lib_ring) l
  rw [e2, lsum_mul_left]; lib_ring

/-- |N_r|² ≤ ι² (Σ_p |u_p|²)(Σ_q |k_q|²) · Σ_p Σ_q |v_q|². -/
theorem force_mode (l : List I) (δ : I → I → I → R) (ι : R) (k u v : I → V3 R) (r : I)
    (hδ : ∀ p q r, δ p q r = K.zero ∨ δ p q r = K.one) :
    Le K P (dot3 K (Nl K l δ ι k u v r) (Nl K l δ ι k u v r))
      (K.mul (K.mul (K.mul ι ι) (K.mul (lsum K (fun p => dot3 K (u p) (u p)) l) (lsum K (fun q => dot3 K (k q) (k q)) l)))
        (lsum K (fun _ => lsum K (fun q => dot3 K (v q) (v q)) l) l)) := by
  have hc := lsum_le K P _ _ (fun p => lsum_le K P _ _ (fun q => coef_sq K P δ ι k u p q r (hδ p q r)) l) l
  rw [double_factor K l (K.mul ι ι) (fun p => dot3 K (u p) (u p)) (fun q => dot3 K (k q) (k q))] at hc
  have hv : P.nn (lsum K (fun _ => lsum K (fun q => dot3 K (v q) (v q)) l) l) :=
    lsum_nn K P _ (fun _ => lsum_nn K P _ (fun q => dot3_nn K P (v q)) l) l
  have m := le_mul_nn K P _ _ _ hv hc
  rw [K.mul_comm _ (lsum K (fun p => lsum K (fun q => K.mul (coef K δ ι k u p q r) (coef K δ ι k u p q r)) l) l),
    K.mul_comm (lsum K (fun _ => lsum K (fun q => dot3 K (v q) (v q)) l) l)] at m
  exact le_trans' K P _ _ _ (force_sq K P l δ ι k u v r) m

end Gen

end ForceBound

#print axioms ForceBound.dot3_cs
#print axioms ForceBound.sA_grid
#print axioms ForceBound.sB_grid
#print axioms ForceBound.sC_grid
#print axioms ForceBound.double_cs
#print axioms ForceBound.sum3_split
#print axioms ForceBound.force_sq
#print axioms ForceBound.lsum_le
#print axioms ForceBound.coef_sq
#print axioms ForceBound.double_factor
#print axioms ForceBound.force_mode

ns/ForceDiff.lean

lean_all/ns/ForceDiff.lean

/-
  ns/ForceDiff.lean — T37 cycle 1031 (goal G671, piece b): the force difference in the enstrophy setting, over an
  ordered field, uniformly in the truncation. With d = a − b (componentwise), N(a,a) − N(b,b) = N(d, a) + N(b, d), and
    force_diff   Σ_r |N_r(a,a) − N_r(b,b)|²
                   ≤ 2·(2ι²[Z(d)C₁Z(a) + A(d)C₂Z(a)]) + 2·(ι²·A(b)·(135·Z(d)))
  (ForceTwoScale.two_scale for N(d, a), ForceUniform.force_uniform with NormCompare.nrm4_le for N(b, d)).

  Lean 4 core plus ns/ForceTwoScale, ns/EnstDiff.
-/
import ns.ForceTwoScale
import ns.EnstDiff

namespace ForceDiff
open DetChain NSChain DyadicSmallGlobal LibRing DyadicLarge FourierGalerkin GalerkinForce ForceBound ForceSob
  ModeCount CubeCount ForceUniform NormCompare EnstGalerkin ForceTwoScale ForceBilin EnstDiff LatticeSum

section Gen
variable {R : Type} (K : CRing R) (O : OrdR K) {I : Type}
variable (inv : R → R) (mul_inv : ∀ y, y ≠ K.zero → K.mul y (inv y) = K.one) (h10 : K.one ≠ K.zero)

def dF (a b : I → V3 R) (p : I) : V3 R := sub3 K (a p) (b p)

theorem a_split (a b : I → V3 R) (p : I) : a p = addF K b (dF K a b) p := by
  show a p = add3 K (b p) (add3 K (a p) (neg3 K (b p)))
  apply v3_ext
  · show (a p).x = K.add (b p).x (K.add (a p).x (K.neg (b p).x)); lib_ring
  · show (a p).y = K.add (b p).y (K.add (a p).y (K.neg (b p).y)); lib_ring
  · show (a p).z = K.add (b p).z (K.add (a p).z (K.neg (b p).z)); lib_ring

theorem diff_eq (l : List I) (δ : I → I → I → R) (ι : R) (k a b : I → V3 R) (r : I) :
    sub3 K (Nl K l δ ι k a a r) (Nl K l δ ι k b b r)
      = add3 K (Nl K l δ ι k (dF K a b) a r) (Nl K l δ ι k b (dF K a b) r) := by
  rw [ForceLip.Nl_congr K l δ ι k a (addF K b (dF K a b)) a (addF K b (dF K a b)) r (a_split K a b) (a_split K a b),
    Nl_diff]
  rw [ForceLip.Nl_congr K l δ ι k (dF K a b) (dF K a b) (addF K b (dF K a b)) a r (fun _ => rfl)
    (fun p => (a_split K a b p).symm)]
  generalize Nl K l δ ι k b b r = A; generalize Nl K l δ ι k (dF K a b) a r = B
  generalize Nl K l δ ι k b (dF K a b) r = C
  apply v3_ext
  · show K.add (K.add (K.add A.x B.x) C.x) (K.neg A.x) = K.add B.x C.x; lib_ring
  · show K.add (K.add (K.add A.y B.y) C.y) (K.neg A.y) = K.add B.y C.y; lib_ring
  · show K.add (K.add (K.add A.z B.z) C.z) (K.neg A.z) = K.add B.z C.z; lib_ring

include mul_inv h10 in
theorem force_diff (l : List I) (kz : I → Mode) (hd : List.Pairwise (fun a b => kz a ≠ kz b) l)
    (h1 : ∀ p, 1 ≤ nrm (kz p)) (M : Nat) (hM : ∀ p, p ∈ l → nrm (kz p) ≤ M + 1) (Rc t0 : Nat) (hRM : Rc ≤ M)
    (ι : R) (a b : I → V3 R) :
    Le K O.toCone
      (lsum K (fun r => dot3 K (sub3 K (Nl K l (TriadSparse.delta K kz) ι (kv K kz) a a r) (Nl K l (TriadSparse.delta K kz) ι (kv K kz) b b r))
        (sub3 K (Nl K l (TriadSparse.delta K kz) ι (kv K kz) a a r) (Nl K l (TriadSparse.delta K kz) ι (kv K kz) b b r))) l)
      (K.add
        (K.mul (K.add K.one K.one) (K.add
          (K.mul (K.add K.one K.one) (K.mul (K.mul ι ι) (K.mul (lsum K (fun p => K.mul (wt K kz p) (dot3 K (dF K a b p) (dF K a b p))) l)
            (K.mul (K.add (K.mul (castN K (cube Rc)) K.one) (K.mul (castN K 135) (iv K inv (Rc + 1))))
              (lsum K (fun q => K.mul (wt K kz q) (dot3 K (a q) (a q))) l)))))
          (K.mul (K.add K.one K.one) (K.mul (K.mul ι ι) (K.mul (lsum K (fun p => K.mul (K.mul (wt K kz p) (wt K kz p)) (dot3 K (dF K a b p) (dF K a b p))) l)
            (K.mul (K.add (K.mul (castN K (cube Rc)) (K.mul (inv (castN K (t0 + 1))) (inv (castN K (t0 + 1)))))
                (K.mul (castN K 135) (iv K inv (Rc + 1))))
              (lsum K (fun q => K.mul (wt K kz q) (dot3 K (a q) (a q))) l)))))))
        (K.mul (K.add K.one K.one) (K.mul (K.mul ι ι) (K.mul (lsum K (fun p => K.mul (K.mul (wt K kz p) (wt K kz p)) (dot3 K (b p) (b p))) l)
          (K.mul (castN K 135) (lsum K (fun q => K.mul (wt K kz q) (dot3 K (dF K a b q) (dF K a b q))) l)))))) := by
  rw [lsum_ext K _ _ (fun r => by rw [diff_eq K l _ ι _ a b r]) l]
  have s1 := lsum_le K O.toCone _ _ (fun r => sq2 K O
    (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (dF K a b) a r)
    (Nl K l (TriadSparse.delta K kz) ι (kv K kz) b (dF K a b) r)) l
  rw [lsum_add, lsum_mul_left, lsum_mul_left] at s1
  refine le_trans' K O.toCone _ _ _ s1 (le_add' K O.toCone _ _ _ _ ?_ ?_)
  · exact le_mul_nn K O.toCone _ _ _ (O.nn_add _ _ (RatModel.nn_one' K O.toCone) (RatModel.nn_one' K O.toCone))
      (two_scale K O inv mul_inv h10 l kz hd h1 M hM Rc t0 hRM ι (dF K a b) a)
  · have fu := force_uniform K O inv mul_inv h10 l kz hd h1 M hM ι (kv K kz) b (dF K a b)
    have hH := lsum_le K O.toCone _ _ (fun p => le_mul_right' K O.toCone (nrm4_le K O.toCone (kz p)) (dot3_nn K O.toCone (b p))) l
    have nZ : O.nn (lsum K (fun q => K.mul (wt K kz q) (dot3 K (dF K a b q) (dF K a b q))) l) :=
      lsum_nn K O.toCone _ (fun q => O.nn_mul _ _ (dot3_nn K O.toCone _) (dot3_nn K O.toCone _)) l
    have m1 := le_mul_right' K O.toCone hH (O.nn_mul _ _ (RatModel.nn_cast K O.toCone 135) nZ)
    have m2 := le_mul_nn K O.toCone (K.mul ι ι) _ _ (O.nn_sq ι) m1
    exact le_mul_nn K O.toCone _ _ _ (O.nn_add _ _ (RatModel.nn_one' K O.toCone) (RatModel.nn_one' K O.toCone))
      (le_trans' K O.toCone _ _ _ fu m2)

end Gen

end ForceDiff

#print axioms ForceDiff.a_split
#print axioms ForceDiff.diff_eq
#print axioms ForceDiff.force_diff

ns/ForceDiffW.lean

lean_all/ns/ForceDiffW.lean

/-
  ns/ForceDiffW.lean — T37 cycle 1107 (goal G717, piece b7): the H¹-weighted force difference, uniform in the
  truncation, over an ordered field. With d = a − b and N(a,a) − N(b,b) = N(d,a) + N(b,d) (ForceDiff.diff_eq):
    force_diffW   Σ_r w_r|N_r(a,a) − N_r(b,b)|²
                    ≤ 2·ι²B(d)(2S₁Z(a) + 2S₀A(a)) + 2·ι²B(b)(2S₁Z(d) + 2S₀A(d))
  (ForceH2Bi twice; B = Σw³|·|², Z = Σw|·|², A = Σw²|·|², S₁ ≤ 3·135, S₀ ≤ 135 by EnstH2). In the step-size defect
  d = b₁ − a₀ is O(k) in H³ (TimeLipW with V = w³, ForceH4, EnstH4).

  Lean 4 core plus ns/ForceH2Bi, ns/ForceDiff, ns/ForceTwoScale (sq2).
-/
import ns.ForceH2Bi
import ns.ForceDiff

namespace ForceDiffW
open DetChain NSChain DyadicSmallGlobal LibRing DyadicLarge FourierGalerkin GalerkinForce ForceBound ForceSob
  ModeCount CubeCount ForceUniform NormCompare EnstGalerkin ForceTwoScale ForceSobW CastZAdd WeightH2 LatticeSum
  LatticeBound ForceH2 ForceH2Bi ForceDiff EnstDiff

section Gen
variable {R : Type} (K : CRing R) (O : OrdR K) {I : Type}
variable (inv : R → R) (mul_inv : ∀ y, y ≠ K.zero → K.mul y (inv y) = K.one) (h10 : K.one ≠ K.zero)

def Bw (kz : I → Mode) (l : List I) (u : I → V3 R) : R :=
  lsum K (fun p => K.mul (K.mul (K.mul (wt K kz p) (wt K kz p)) (wt K kz p)) (dot3 K (u p) (u p))) l
def Zw (kz : I → Mode) (l : List I) (u : I → V3 R) : R := lsum K (fun q => K.mul (wt K kz q) (dot3 K (u q) (u q))) l
def Aw (kz : I → Mode) (l : List I) (u : I → V3 R) : R :=
  lsum K (fun q => K.mul (K.mul (wt K kz q) (wt K kz q)) (dot3 K (u q) (u q))) l
def S1w (kz : I → Mode) (l : List I) : R :=
  lsum K (fun p => K.mul (K.mul (cube3 K (ivn K inv kz p)) (cube3 K (ivn K inv kz p))) (wt K kz p)) l
def S0w (kz : I → Mode) (l : List I) : R := lsum K (fun p => K.mul (cube3 K (ivn K inv kz p)) (cube3 K (ivn K inv kz p))) l

include mul_inv h10 in
theorem force_diffW (l : List I) (kz : I → Mode) (hd : List.Pairwise (fun a b => kz a ≠ kz b) l)
    (h1 : ∀ p, 1 ≤ nrm (kz p)) (ι : R) (a b : I → V3 R) :
    Le K O.toCone
      (lsum K (fun r => K.mul (wt K kz r) (dot3 K (sub3 K (Nl K l (TriadSparse.delta K kz) ι (kv K kz) a a r) (Nl K l (TriadSparse.delta K kz) ι (kv K kz) b b r))
        (sub3 K (Nl K l (TriadSparse.delta K kz) ι (kv K kz) a a r) (Nl K l (TriadSparse.delta K kz) ι (kv K kz) b b r)))) l)
      (K.add
        (K.mul (K.add K.one K.one) (K.mul (K.mul ι ι) (K.mul (Bw K kz l (dF K a b))
          (K.add (K.mul (K.add K.one K.one) (K.mul (S1w K inv kz l) (Zw K kz l a)))
            (K.mul (K.add K.one K.one) (K.mul (S0w K inv kz l) (Aw K kz l a)))))))
        (K.mul (K.add K.one K.one) (K.mul (K.mul ι ι) (K.mul (Bw K kz l b)
          (K.add (K.mul (K.add K.one K.one) (K.mul (S1w K inv kz l) (Zw K kz l (dF K a b))))
            (K.mul (K.add K.one K.one) (K.mul (S0w K inv kz l) (Aw K kz l (dF K a b))))))))) := by
  rw [lsum_ext K _ _ (fun r => by rw [diff_eq K l _ ι _ a b r]) l]
  have two : O.nn (K.add K.one K.one) := O.nn_add _ _ (RatModel.nn_one' K O.toCone) (RatModel.nn_one' K O.toCone)
  have s1 := lsum_le K O.toCone _ _ (fun r => le_mul_nn K O.toCone (wt K kz r) _ _ (dot3_nn K O.toCone _) (sq2 K O
    (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (dF K a b) a r)
    (Nl K l (TriadSparse.delta K kz) ι (kv K kz) b (dF K a b) r))) l
  have e : lsum K (fun r => K.mul (wt K kz r) (K.add (K.mul (K.add K.one K.one)
        (dot3 K (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (dF K a b) a r) (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (dF K a b) a r)))
      (K.mul (K.add K.one K.one) (dot3 K (Nl K l (TriadSparse.delta K kz) ι (kv K kz) b (dF K a b) r) (Nl K l (TriadSparse.delta K kz) ι (kv K kz) b (dF K a b) r))))) l
      = K.add (K.mul (K.add K.one K.one) (lsum K (fun r => K.mul (wt K kz r)
          (dot3 K (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (dF K a b) a r) (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (dF K a b) a r))) l))
        (K.mul (K.add K.one K.one) (lsum K (fun r => K.mul (wt K kz r)
          (dot3 K (Nl K l (TriadSparse.delta K kz) ι (kv K kz) b (dF K a b) r) (Nl K l (TriadSparse.delta K kz) ι (kv K kz) b (dF K a b) r))) l)) := by
    rw [← lsum_mul_left, ← lsum_mul_left, ← lsum_add]
    exact lsum_ext K _ _ (fun r => by lib_ring) l
  rw [e] at s1
  refine le_trans' K O.toCone _ _ _ s1 (le_add' K O.toCone _ _ _ _ ?_ ?_)
  · exact le_mul_nn K O.toCone _ _ _ two (force_h2_bi K O inv mul_inv h10 l kz hd h1 ι (dF K a b) a)
  · exact le_mul_nn K O.toCone _ _ _ two (force_h2_bi K O inv mul_inv h10 l kz hd h1 ι b (dF K a b))

end Gen

end ForceDiffW

#print axioms ForceDiffW.force_diffW

ns/ForceEnst.lean

lean_all/ns/ForceEnst.lean

/-
  ns/ForceEnst.lean — T37 cycle 936 (goal G624): the enstrophy form of the Galerkin force bound, over any commutative
  ring with a nonnegative cone (ℚ included). Grouping Cauchy–Schwarz differently from ForceBound.force_mode:
    |N_r(u, v)|² ≤ ι² · (Σ_p Σ_q δ(p,q,r)²) · Σ_p Σ_q |u_p|² |k_q|² |v_q|²
                 = ι² · T_r · E(u) · Z(v)      (T_r = Σ_p Σ_q δ², Z(v) = Σ_q |k_q|²|v_q|²)
  — the discrete form of |(u·∇)v| ≲ |u| |∇v|: the truncation enters only through the triad count T_r, which is at
  most the number of modes p (for each p, r at most one q closes the triad k_q = −k_p − k_r).

  Lean 4 core plus ns/ForceBound (double_cs, dot3_cs, lsum_le, dot3_nn, sum3_split), ns/GalerkinForce (Nl, coef).
-/
import ns.ForceBound

namespace ForceEnst
open DetChain NSChain LibRing DyadicLarge FourierGalerkin GalerkinForce ForceBound

section Gen
variable {R : Type} (K : CRing R) (P : Cone K) {I : Type}

theorem force_enst (l : List I) (δ : I → I → I → R) (ι : R) (k u v : I → V3 R) (r : I) :
    Le K P (dot3 K (Nl K l δ ι k u v r) (Nl K l δ ι k u v r))
      (K.mul (K.mul ι ι) (K.mul (lsum K (fun p => lsum K (fun q => K.mul (δ p q r) (δ p q r)) l) l)
        (lsum K (fun p => lsum K (fun q => K.mul (dot3 K (u p) (u p)) (K.mul (dot3 K (k q) (k q)) (dot3 K (v q) (v q)))) l) l))) := by
  -- component c: N_{r,c} = Σ Σ (δ ι) · ((u_p·k_q) v_{q,c})
  have comp : ∀ (vc : I → R), Le K P
      (K.mul (lsum K (fun p => lsum K (fun q => K.mul (coef K δ ι k u p q r) (vc q)) l) l)
        (lsum K (fun p => lsum K (fun q => K.mul (coef K δ ι k u p q r) (vc q)) l) l))
      (K.mul (lsum K (fun p => lsum K (fun q => K.mul (K.mul ι (δ p q r)) (K.mul ι (δ p q r))) l) l)
        (lsum K (fun p => lsum K (fun q => K.mul (K.mul (dot3 K (u p) (k q)) (vc q)) (K.mul (dot3 K (u p) (k q)) (vc q))) l) l)) := by
    intro vc
    have cs := double_cs K P (fun p q => K.mul ι (δ p q r)) (fun p q => K.mul (dot3 K (u p) (k q)) (vc q)) l
    have e : ∀ p q, K.mul (coef K δ ι k u p q r) (vc q) = K.mul (K.mul ι (δ p q r)) (K.mul (dot3 K (u p) (k q)) (vc q)) := by
      intro p q; unfold coef; lib_ring
    rw [lsum_ext K _ _ (fun p => lsum_ext K _ _ (fun q => e p q) l) l]
    exact cs
  have s := le_add' K P _ _ _ _ (le_add' K P _ _ _ _ (comp (fun q => (v q).x)) (comp (fun q => (v q).y))) (comp (fun q => (v q).z))
  generalize hD : lsum K (fun p => lsum K (fun q => K.mul (K.mul ι (δ p q r)) (K.mul ι (δ p q r))) l) l = Dl at s
  have hDn : P.nn Dl := by rw [← hD]; exact lsum_nn K P _ (fun p => lsum_nn K P _ (fun q => P.nn_sq _) l) l
  -- regroup: Σ_c Dl·ΣΣ (w v_c)² = Dl · ΣΣ w² |v_q|²   (w = u_p·k_q)
  have e1 : ∀ (A : R), K.add (K.add (K.mul A (lsum K (fun p => lsum K (fun q => K.mul (K.mul (dot3 K (u p) (k q)) (v q).x) (K.mul (dot3 K (u p) (k q)) (v q).x)) l) l))
        (K.mul A (lsum K (fun p => lsum K (fun q => K.mul (K.mul (dot3 K (u p) (k q)) (v q).y) (K.mul (dot3 K (u p) (k q)) (v q).y)) l) l)))
        (K.mul A (lsum K (fun p => lsum K (fun q => K.mul (K.mul (dot3 K (u p) (k q)) (v q).z) (K.mul (dot3 K (u p) (k q)) (v q).z)) l) l))
      = K.mul A (lsum K (fun p => lsum K (fun q => K.mul (K.mul (dot3 K (u p) (k q)) (dot3 K (u p) (k q))) (dot3 K (v q) (v q))) l) l) := by
    intro A
    rw [← K.distrib, ← K.distrib, ← lsum_add, ← lsum_add]
    congr 1
    apply lsum_ext; intro p
    rw [← lsum_add, ← lsum_add]
    apply lsum_ext; intro q
    show _ = K.mul (K.mul (dot3 K (u p) (k q)) (dot3 K (u p) (k q))) (K.add (K.add (K.mul (v q).x (v q).x) (K.mul (v q).y (v q).y)) (K.mul (v q).z (v q).z))
    generalize dot3 K (u p) (k q) = w
    lib_ring
  rw [e1] at s
  have b := lsum_le K P _ _ (fun p => lsum_le K P _ _ (fun q =>
      le_mul_nn K P (dot3 K (v q) (v q)) _ _ (dot3_nn K P (v q)) (dot3_cs K P (u p) (k q))) l) l
  have b2 := le_mul_nn K P _ _ _ hDn b
  have hDl : Dl = K.mul (K.mul ι ι) (lsum K (fun p => lsum K (fun q => K.mul (δ p q r) (δ p q r)) l) l) := by
    rw [← hD, ← lsum_mul_left]
    apply lsum_ext; intro p
    rw [← lsum_mul_left]
    apply lsum_ext; intro q; lib_ring
  have b3 : Le K P (K.mul Dl (lsum K (fun p => lsum K (fun q => K.mul (K.mul (dot3 K (u p) (k q)) (dot3 K (u p) (k q))) (dot3 K (v q) (v q))) l) l))
      (K.mul (K.mul ι ι) (K.mul (lsum K (fun p => lsum K (fun q => K.mul (δ p q r) (δ p q r)) l) l)
        (lsum K (fun p => lsum K (fun q => K.mul (dot3 K (u p) (u p)) (K.mul (dot3 K (k q) (k q)) (dot3 K (v q) (v q)))) l) l))) := by
    have e2 : lsum K (fun p => lsum K (fun q => K.mul (dot3 K (v q) (v q)) (K.mul (dot3 K (u p) (k q)) (dot3 K (u p) (k q)))) l) l
        = lsum K (fun p => lsum K (fun q => K.mul (K.mul (dot3 K (u p) (k q)) (dot3 K (u p) (k q))) (dot3 K (v q) (v q))) l) l :=
      lsum_ext K _ _ (fun p => lsum_ext K _ _ (fun q => K.mul_comm _ _) l) l
    have e3 : lsum K (fun p => lsum K (fun q => K.mul (dot3 K (v q) (v q)) (K.mul (dot3 K (u p) (u p)) (dot3 K (k q) (k q)))) l) l
        = lsum K (fun p => lsum K (fun q => K.mul (dot3 K (u p) (u p)) (K.mul (dot3 K (k q) (k q)) (dot3 K (v q) (v q)))) l) l :=
      lsum_ext K _ _ (fun p => lsum_ext K _ _ (fun q => by lib_ring) l) l
    rw [e2, e3, hDl] at b2
    cases b2 with
    | intro w hw => exact ⟨w, hw.1, by rw [hDl]; exact hw.2.trans (by lib_ring)⟩
  exact le_trans' K P _ _ _ s b3

end Gen

end ForceEnst

#print axioms ForceEnst.force_enst

ns/ForceH2.lean

lean_all/ns/ForceH2.lean

/-
  ns/ForceH2.lean — T37 cycle 1067 (goal G656b, piece iv-b): the H²-level Galerkin force bound, uniform in the
  truncation, over an ordered field. For integer modes (nonzero, distinct on l), w_r = |k_r|²:
    force_h2   Σ_r w_r|N_r(a,a)|² ≤ ι²·B·(2·S₁·Z + 2·S₀·A),   B = Σ w³|a|²,  Z = Σ w|a|²,  A = Σ w²|a|²,
  with S₁ = Σ_p |k_p|_∞^{−6}w_p and S₀ = Σ_p |k_p|_∞^{−6} (each bounded by a multiple of 135 via LatticeBound; next).
  ForceSobW.total_sobW with μ = |k|_∞³, the triad triangle inequality (CastZAdd.delta_tri), |k|_∞² ≤ |k|² ≤ 3|k|_∞².

  Lean 4 core plus ns/ForceSobW, ns/CastZAdd, ns/WeightH2, ns/ForceTwoScale, ns/ForceUniform.
-/
import ns.ForceSobW
import ns.CastZAdd
import ns.WeightH2
import ns.ForceTwoScale
import ns.TruncRate

namespace ForceH2
open DetChain NSChain DyadicSmallGlobal LibRing DyadicLarge FourierGalerkin GalerkinForce ForceBound ForceSob
  ModeCount CubeCount ForceUniform NormCompare EnstGalerkin ForceTwoScale ForceSobW CastZAdd WeightH2 LatticeSum
  LatticeBound

section Gen
variable {R : Type} (K : CRing R) (O : OrdR K) {I : Type}
variable (inv : R → R) (mul_inv : ∀ y, y ≠ K.zero → K.mul y (inv y) = K.one) (h10 : K.one ≠ K.zero)

def cube3 (x : R) : R := K.mul (K.mul x x) x

include O mul_inv h10 in
theorem munu3 (kz : I → Mode) (h1 : ∀ p, 1 ≤ nrm (kz p)) (p : I) :
    K.mul (cube3 K (nn K kz p)) (cube3 K (ivn K inv kz p)) = K.one := by
  have e := nn_ivn K O inv mul_inv h10 kz h1 p
  unfold cube3
  calc _ = K.mul (K.mul (K.mul (nn K kz p) (ivn K inv kz p)) (K.mul (nn K kz p) (ivn K inv kz p)))
        (K.mul (nn K kz p) (ivn K inv kz p)) := by lib_ring
    _ = K.one := by rw [e]; lib_ring

/-- the u-side weight: n⁶|a|² ≤ w³|a|² -/
theorem mu6_le (kz : I → Mode) (p : I) :
    Le K O.toCone (K.mul (cube3 K (nn K kz p)) (cube3 K (nn K kz p)))
      (K.mul (K.mul (wt K kz p) (wt K kz p)) (wt K kz p)) := by
  have h := nrm_le_dot K O.toCone (kz p)
  have hn : O.nn (K.mul (nn K kz p) (nn K kz p)) := O.nn_sq _
  have hw : O.nn (wt K kz p) := dot3_nn K O.toCone _
  have s2 := EnstLocal.sq_mono K O _ _ hn hw h
  have s3 := TruncRate.mm K O _ _ _ _ (O.nn_mul _ _ hn hn) hw s2 h
  have e : K.mul (cube3 K (nn K kz p)) (cube3 K (nn K kz p))
      = K.mul (K.mul (K.mul (nn K kz p) (nn K kz p)) (K.mul (nn K kz p) (nn K kz p))) (K.mul (nn K kz p) (nn K kz p)) := by
    unfold cube3; lib_ring
  rw [e]; exact s3

include mul_inv h10 in
theorem force_h2 (l : List I) (kz : I → Mode) (hd : List.Pairwise (fun a b => kz a ≠ kz b) l)
    (h1 : ∀ p, 1 ≤ nrm (kz p)) (ι : R) (a : I → V3 R) :
    Le K O.toCone (lsum K (fun r => K.mul (wt K kz r) (dot3 K (Nl K l (TriadSparse.delta K kz) ι (kv K kz) a a r)
        (Nl K l (TriadSparse.delta K kz) ι (kv K kz) a a r))) l)
      (K.mul (K.mul ι ι) (K.mul (lsum K (fun p => K.mul (K.mul (K.mul (wt K kz p) (wt K kz p)) (wt K kz p)) (dot3 K (a p) (a p))) l)
        (K.add (K.mul (K.add K.one K.one) (K.mul (lsum K (fun p => K.mul (K.mul (cube3 K (ivn K inv kz p)) (cube3 K (ivn K inv kz p))) (wt K kz p)) l)
            (lsum K (fun q => K.mul (wt K kz q) (dot3 K (a q) (a q))) l)))
          (K.mul (K.add K.one K.one) (K.mul (lsum K (fun p => K.mul (cube3 K (ivn K inv kz p)) (cube3 K (ivn K inv kz p))) l)
            (lsum K (fun q => K.mul (K.mul (wt K kz q) (wt K kz q)) (dot3 K (a q) (a q))) l)))))) := by
  have T := total_sobW K O.toCone l (TriadSparse.delta K kz) ι (kv K kz) a a
    (fun p => cube3 K (nn K kz p)) (fun p => cube3 K (ivn K inv kz p)) (wt K kz) (fun r => dot3_nn K O.toCone _)
    (fun p q r => delta_tri K O.toCone kz p q r) (delta01 K kz) (munu3 K O inv mul_inv h10 kz h1)
    (fun p r => TriadSparse.spA K O.toCone kz l hd p r) (fun p q => TriadSparse.spB K O.toCone kz l hd p q)
  refine le_trans' K O.toCone _ _ _ T ?_
  have hH := lsum_le K O.toCone _ _ (fun p => le_mul_right' K O.toCone (mu6_le K O kz p) (dot3_nn K O.toCone (a p))) l
  -- the double sum factors
  have e : lsum K (fun p => lsum K (fun q => K.mul (K.mul (K.mul (cube3 K (ivn K inv kz p)) (cube3 K (ivn K inv kz p)))
        (K.add (K.add (wt K kz p) (wt K kz p)) (K.add (wt K kz q) (wt K kz q))))
        (K.mul (dot3 K (kv K kz q) (kv K kz q)) (dot3 K (a q) (a q)))) l) l
      = K.add (K.mul (K.add K.one K.one) (K.mul (lsum K (fun p => K.mul (K.mul (cube3 K (ivn K inv kz p)) (cube3 K (ivn K inv kz p))) (wt K kz p)) l)
            (lsum K (fun q => K.mul (wt K kz q) (dot3 K (a q) (a q))) l)))
          (K.mul (K.add K.one K.one) (K.mul (lsum K (fun p => K.mul (cube3 K (ivn K inv kz p)) (cube3 K (ivn K inv kz p))) l)
            (lsum K (fun q => K.mul (K.mul (wt K kz q) (wt K kz q)) (dot3 K (a q) (a q))) l))) := by
    rw [← double_factor, ← double_factor, ← lsum_add]
    apply lsum_ext; intro p
    rw [← lsum_add]
    apply lsum_ext; intro q
    show _ = K.add (K.mul (K.add K.one K.one) (K.mul (K.mul (K.mul (cube3 K (ivn K inv kz p)) (cube3 K (ivn K inv kz p))) (wt K kz p))
        (K.mul (wt K kz q) (dot3 K (a q) (a q)))))
      (K.mul (K.add K.one K.one) (K.mul (K.mul (cube3 K (ivn K inv kz p)) (cube3 K (ivn K inv kz p)))
        (K.mul (K.mul (wt K kz q) (wt K kz q)) (dot3 K (a q) (a q)))))
    show K.mul (K.mul (K.mul (cube3 K (ivn K inv kz p)) (cube3 K (ivn K inv kz p)))
        (K.add (K.add (wt K kz p) (wt K kz p)) (K.add (wt K kz q) (wt K kz q))))
        (K.mul (wt K kz q) (dot3 K (a q) (a q))) = _
    lib_ring
  rw [e]
  have nS : O.nn (K.add (K.mul (K.add K.one K.one) (K.mul (lsum K (fun p => K.mul (K.mul (cube3 K (ivn K inv kz p)) (cube3 K (ivn K inv kz p))) (wt K kz p)) l)
            (lsum K (fun q => K.mul (wt K kz q) (dot3 K (a q) (a q))) l)))
          (K.mul (K.add K.one K.one) (K.mul (lsum K (fun p => K.mul (cube3 K (ivn K inv kz p)) (cube3 K (ivn K inv kz p))) l)
            (lsum K (fun q => K.mul (K.mul (wt K kz q) (wt K kz q)) (dot3 K (a q) (a q))) l)))) := by
    have two : O.nn (K.add K.one K.one) := O.nn_add _ _ (RatModel.nn_one' K O.toCone) (RatModel.nn_one' K O.toCone)
    exact O.nn_add _ _
      (O.nn_mul _ _ two (O.nn_mul _ _ (lsum_nn K O.toCone _ (fun p => O.nn_mul _ _ (O.nn_sq _) (dot3_nn K O.toCone _)) l)
        (lsum_nn K O.toCone _ (fun q => O.nn_mul _ _ (dot3_nn K O.toCone _) (dot3_nn K O.toCone _)) l)))
      (O.nn_mul _ _ two (O.nn_mul _ _ (lsum_nn K O.toCone _ (fun p => O.nn_sq _) l)
        (lsum_nn K O.toCone _ (fun q => O.nn_mul _ _ (O.nn_sq _) (dot3_nn K O.toCone _)) l)))
  exact le_mul_nn K O.toCone _ _ _ (O.nn_sq ι) (ForceSob.le_mul_right' K O.toCone hH nS)

end Gen

end ForceH2

#print axioms ForceH2.munu3
#print axioms ForceH2.mu6_le
#print axioms ForceH2.force_h2

ns/ForceH2Bi.lean

lean_all/ns/ForceH2Bi.lean

/-
  ns/ForceH2Bi.lean — T37 cycle 1101 (goal G717, piece b4): the H¹-weighted Galerkin force bound for two different
  inputs, uniform in the truncation, over an ordered field (integer modes, nonzero, distinct on l; w = |k|²):
    force_h2_bi   Σ_r w_r|N_r(u,v)|² ≤ ι²·B(u)·(2·S₁·Z(v) + 2·S₀·A(v)),
  B(u) = Σ w³|u|², Z(v) = Σ w|v|², A(v) = Σ w²|v|², S₁, S₀ as in ForceH2 (≤ 3·135, 135 by EnstH2).
  For the force difference N(a,a) − N(b,b) = N(a − b, a) + N(b, a − b) in the step-size defect.

  Lean 4 core plus ns/ForceH2.
-/
import ns.ForceH2

namespace ForceH2Bi
open DetChain NSChain DyadicSmallGlobal LibRing DyadicLarge FourierGalerkin GalerkinForce ForceBound ForceSob
  ModeCount CubeCount ForceUniform NormCompare EnstGalerkin ForceTwoScale ForceSobW CastZAdd WeightH2 LatticeSum
  LatticeBound ForceH2

section Gen
variable {R : Type} (K : CRing R) (O : OrdR K) {I : Type}
variable (inv : R → R) (mul_inv : ∀ y, y ≠ K.zero → K.mul y (inv y) = K.one) (h10 : K.one ≠ K.zero)

include mul_inv h10 in
theorem force_h2_bi (l : List I) (kz : I → Mode) (hd : List.Pairwise (fun a b => kz a ≠ kz b) l)
    (h1 : ∀ p, 1 ≤ nrm (kz p)) (ι : R) (u v : I → V3 R) :
    Le K O.toCone (lsum K (fun r => K.mul (wt K kz r) (dot3 K (Nl K l (TriadSparse.delta K kz) ι (kv K kz) u v r)
        (Nl K l (TriadSparse.delta K kz) ι (kv K kz) u v r))) l)
      (K.mul (K.mul ι ι) (K.mul (lsum K (fun p => K.mul (K.mul (K.mul (wt K kz p) (wt K kz p)) (wt K kz p)) (dot3 K (u p) (u p))) l)
        (K.add (K.mul (K.add K.one K.one) (K.mul (lsum K (fun p => K.mul (K.mul (cube3 K (ivn K inv kz p)) (cube3 K (ivn K inv kz p))) (wt K kz p)) l)
            (lsum K (fun q => K.mul (wt K kz q) (dot3 K (v q) (v q))) l)))
          (K.mul (K.add K.one K.one) (K.mul (lsum K (fun p => K.mul (cube3 K (ivn K inv kz p)) (cube3 K (ivn K inv kz p))) l)
            (lsum K (fun q => K.mul (K.mul (wt K kz q) (wt K kz q)) (dot3 K (v q) (v q))) l)))))) := by
  have T := total_sobW K O.toCone l (TriadSparse.delta K kz) ι (kv K kz) u v
    (fun p => cube3 K (nn K kz p)) (fun p => cube3 K (ivn K inv kz p)) (wt K kz) (fun r => dot3_nn K O.toCone _)
    (fun p q r => delta_tri K O.toCone kz p q r) (delta01 K kz) (munu3 K O inv mul_inv h10 kz h1)
    (fun p r => TriadSparse.spA K O.toCone kz l hd p r) (fun p q => TriadSparse.spB K O.toCone kz l hd p q)
  refine le_trans' K O.toCone _ _ _ T ?_
  have hH := lsum_le K O.toCone _ _ (fun p => le_mul_right' K O.toCone (mu6_le K O kz p) (dot3_nn K O.toCone (u p))) l
  -- the double sum factors
  have e : lsum K (fun p => lsum K (fun q => K.mul (K.mul (K.mul (cube3 K (ivn K inv kz p)) (cube3 K (ivn K inv kz p)))
        (K.add (K.add (wt K kz p) (wt K kz p)) (K.add (wt K kz q) (wt K kz q))))
        (K.mul (dot3 K (kv K kz q) (kv K kz q)) (dot3 K (v q) (v q)))) l) l
      = K.add (K.mul (K.add K.one K.one) (K.mul (lsum K (fun p => K.mul (K.mul (cube3 K (ivn K inv kz p)) (cube3 K (ivn K inv kz p))) (wt K kz p)) l)
            (lsum K (fun q => K.mul (wt K kz q) (dot3 K (v q) (v q))) l)))
          (K.mul (K.add K.one K.one) (K.mul (lsum K (fun p => K.mul (cube3 K (ivn K inv kz p)) (cube3 K (ivn K inv kz p))) l)
            (lsum K (fun q => K.mul (K.mul (wt K kz q) (wt K kz q)) (dot3 K (v q) (v q))) l))) := by
    rw [← double_factor, ← double_factor, ← lsum_add]
    apply lsum_ext; intro p
    rw [← lsum_add]
    apply lsum_ext; intro q
    show _ = K.add (K.mul (K.add K.one K.one) (K.mul (K.mul (K.mul (cube3 K (ivn K inv kz p)) (cube3 K (ivn K inv kz p))) (wt K kz p))
        (K.mul (wt K kz q) (dot3 K (v q) (v q)))))
      (K.mul (K.add K.one K.one) (K.mul (K.mul (cube3 K (ivn K inv kz p)) (cube3 K (ivn K inv kz p)))
        (K.mul (K.mul (wt K kz q) (wt K kz q)) (dot3 K (v q) (v q)))))
    show K.mul (K.mul (K.mul (cube3 K (ivn K inv kz p)) (cube3 K (ivn K inv kz p)))
        (K.add (K.add (wt K kz p) (wt K kz p)) (K.add (wt K kz q) (wt K kz q))))
        (K.mul (wt K kz q) (dot3 K (v q) (v q))) = _
    lib_ring
  rw [e]
  have nS : O.nn (K.add (K.mul (K.add K.one K.one) (K.mul (lsum K (fun p => K.mul (K.mul (cube3 K (ivn K inv kz p)) (cube3 K (ivn K inv kz p))) (wt K kz p)) l)
            (lsum K (fun q => K.mul (wt K kz q) (dot3 K (v q) (v q))) l)))
          (K.mul (K.add K.one K.one) (K.mul (lsum K (fun p => K.mul (cube3 K (ivn K inv kz p)) (cube3 K (ivn K inv kz p))) l)
            (lsum K (fun q => K.mul (K.mul (wt K kz q) (wt K kz q)) (dot3 K (v q) (v q))) l)))) := by
    have two : O.nn (K.add K.one K.one) := O.nn_add _ _ (RatModel.nn_one' K O.toCone) (RatModel.nn_one' K O.toCone)
    exact O.nn_add _ _
      (O.nn_mul _ _ two (O.nn_mul _ _ (lsum_nn K O.toCone _ (fun p => O.nn_mul _ _ (O.nn_sq _) (dot3_nn K O.toCone _)) l)
        (lsum_nn K O.toCone _ (fun q => O.nn_mul _ _ (dot3_nn K O.toCone _) (dot3_nn K O.toCone _)) l)))
      (O.nn_mul _ _ two (O.nn_mul _ _ (lsum_nn K O.toCone _ (fun p => O.nn_sq _) l)
        (lsum_nn K O.toCone _ (fun q => O.nn_mul _ _ (O.nn_sq _) (dot3_nn K O.toCone _)) l)))
  exact le_mul_nn K O.toCone _ _ _ (O.nn_sq ι) (ForceSob.le_mul_right' K O.toCone hH nS)

end Gen

end ForceH2Bi

#print axioms ForceH2Bi.force_h2_bi

ns/ForceH3.lean

lean_all/ns/ForceH3.lean

/-
  ns/ForceH3.lean — T37 cycle 1091 (goal G717 preparation): the H³-level Galerkin force bound, uniform in the
  truncation, over an ordered field. For integer modes (nonzero, distinct on l), w_r = |k_r|²:
    force_h3   Σ_r w_r²|N_r(a,a)|² ≤ ι²·B₄·(8·(S₁·Z + S₀·B)),   B₄ = Σ w⁴|a|²,  B = Σ w³|a|²,
  with S₁ = Σ_p |k_p|_∞^{−8}w_p² and S₀ = Σ_p |k_p|_∞^{−8}.
  ForceSobW.total_sobWc with W = |k|⁴, c = 8 (Tri4.delta_tri4), μ = |k|_∞⁴.

  Lean 4 core plus ns/ForceH2, ns/Tri4, ns/ForceSobW (total_sobWc).
-/
import ns.ForceH2
import ns.Tri4

namespace ForceH3
open DetChain NSChain DyadicSmallGlobal LibRing DyadicLarge FourierGalerkin GalerkinForce ForceBound ForceSob
  ModeCount CubeCount ForceUniform NormCompare EnstGalerkin ForceTwoScale ForceSobW CastZAdd WeightH2 LatticeSum
  LatticeBound ForceH2 Tri4 LatticeStep

section Gen
variable {R : Type} (K : CRing R) (O : OrdR K) {I : Type}
variable (inv : R → R) (mul_inv : ∀ y, y ≠ K.zero → K.mul y (inv y) = K.one) (h10 : K.one ≠ K.zero)

include O mul_inv h10 in
theorem munu4 (kz : I → Mode) (h1 : ∀ p, 1 ≤ nrm (kz p)) (p : I) :
    K.mul (p4 K (nn K kz p)) (p4 K (ivn K inv kz p)) = K.one := by
  have e := nn_ivn K O inv mul_inv h10 kz h1 p
  unfold p4
  calc _ = K.mul (K.mul (K.mul (nn K kz p) (ivn K inv kz p)) (K.mul (nn K kz p) (ivn K inv kz p)))
        (K.mul (K.mul (nn K kz p) (ivn K inv kz p)) (K.mul (nn K kz p) (ivn K inv kz p))) := by lib_ring
    _ = K.one := by rw [e]; lib_ring

/-- n⁸ ≤ w⁴ -/
theorem mu8_le (kz : I → Mode) (p : I) :
    Le K O.toCone (K.mul (p4 K (nn K kz p)) (p4 K (nn K kz p)))
      (K.mul (K.mul (wt K kz p) (wt K kz p)) (K.mul (wt K kz p) (wt K kz p))) := by
  have h := nrm_le_dot K O.toCone (kz p)
  have hn : O.nn (K.mul (nn K kz p) (nn K kz p)) := O.nn_sq _
  have hw : O.nn (wt K kz p) := dot3_nn K O.toCone _
  have s2 := EnstLocal.sq_mono K O _ _ hn hw h
  have s4 := EnstLocal.sq_mono K O _ _ (O.nn_sq _) (O.nn_sq _) s2
  have e : K.mul (p4 K (nn K kz p)) (p4 K (nn K kz p))
      = K.mul (K.mul (K.mul (nn K kz p) (nn K kz p)) (K.mul (nn K kz p) (nn K kz p)))
          (K.mul (K.mul (nn K kz p) (nn K kz p)) (K.mul (nn K kz p) (nn K kz p))) := by unfold p4; lib_ring
  rw [e]; exact s4

include mul_inv h10 in
theorem force_h3 (l : List I) (kz : I → Mode) (hd : List.Pairwise (fun a b => kz a ≠ kz b) l)
    (h1 : ∀ p, 1 ≤ nrm (kz p)) (ι : R) (a : I → V3 R) :
    Le K O.toCone (lsum K (fun r => K.mul (K.mul (wt K kz r) (wt K kz r)) (dot3 K (Nl K l (TriadSparse.delta K kz) ι (kv K kz) a a r)
        (Nl K l (TriadSparse.delta K kz) ι (kv K kz) a a r))) l)
      (K.mul (K.mul ι ι) (K.mul (lsum K (fun p => K.mul (K.mul (K.mul (wt K kz p) (wt K kz p)) (K.mul (wt K kz p) (wt K kz p))) (dot3 K (a p) (a p))) l)
        (K.mul (k8 K) (K.add
          (K.mul (lsum K (fun p => K.mul (K.mul (p4 K (ivn K inv kz p)) (p4 K (ivn K inv kz p))) (K.mul (wt K kz p) (wt K kz p))) l)
            (lsum K (fun q => K.mul (wt K kz q) (dot3 K (a q) (a q))) l))
          (K.mul (lsum K (fun p => K.mul (p4 K (ivn K inv kz p)) (p4 K (ivn K inv kz p))) l)
            (lsum K (fun q => K.mul (K.mul (K.mul (wt K kz q) (wt K kz q)) (wt K kz q)) (dot3 K (a q) (a q))) l)))))) := by
  have T := total_sobWc K O.toCone l (TriadSparse.delta K kz) ι (kv K kz) a a
    (fun p => p4 K (nn K kz p)) (fun p => p4 K (ivn K inv kz p)) (fun r => K.mul (wt K kz r) (wt K kz r))
    (fun r => O.nn_sq _) (k8 K) (by unfold k8; exact O.nn_add _ _ (O.nn_add _ _ (O.nn_add _ _ (RatModel.nn_one' K O.toCone) (RatModel.nn_one' K O.toCone))
      (O.nn_add _ _ (RatModel.nn_one' K O.toCone) (RatModel.nn_one' K O.toCone))) (O.nn_add _ _ (O.nn_add _ _ (RatModel.nn_one' K O.toCone) (RatModel.nn_one' K O.toCone))
      (O.nn_add _ _ (RatModel.nn_one' K O.toCone) (RatModel.nn_one' K O.toCone))))
    (fun p q r => delta_tri4 K O kz p q r) (delta01 K kz) (munu4 K O inv mul_inv h10 kz h1)
    (fun p r => TriadSparse.spA K O.toCone kz l hd p r) (fun p q => TriadSparse.spB K O.toCone kz l hd p q)
  refine le_trans' K O.toCone _ _ _ T ?_
  have hH := lsum_le K O.toCone _ _ (fun p => le_mul_right' K O.toCone (mu8_le K O kz p) (dot3_nn K O.toCone (a p))) l
  have e : lsum K (fun p => lsum K (fun q => K.mul (K.mul (K.mul (p4 K (ivn K inv kz p)) (p4 K (ivn K inv kz p)))
        (K.mul (k8 K) (K.add (K.mul (wt K kz p) (wt K kz p)) (K.mul (wt K kz q) (wt K kz q)))))
        (K.mul (dot3 K (kv K kz q) (kv K kz q)) (dot3 K (a q) (a q)))) l) l
      = K.mul (k8 K) (K.add
          (K.mul (lsum K (fun p => K.mul (K.mul (p4 K (ivn K inv kz p)) (p4 K (ivn K inv kz p))) (K.mul (wt K kz p) (wt K kz p))) l)
            (lsum K (fun q => K.mul (wt K kz q) (dot3 K (a q) (a q))) l))
          (K.mul (lsum K (fun p => K.mul (p4 K (ivn K inv kz p)) (p4 K (ivn K inv kz p))) l)
            (lsum K (fun q => K.mul (K.mul (K.mul (wt K kz q) (wt K kz q)) (wt K kz q)) (dot3 K (a q) (a q))) l))) := by
    have d1 := double_factor K l (k8 K) (fun p => K.mul (K.mul (p4 K (ivn K inv kz p)) (p4 K (ivn K inv kz p))) (K.mul (wt K kz p) (wt K kz p)))
      (fun q => K.mul (wt K kz q) (dot3 K (a q) (a q)))
    have d2 := double_factor K l (k8 K) (fun p => K.mul (p4 K (ivn K inv kz p)) (p4 K (ivn K inv kz p)))
      (fun q => K.mul (K.mul (K.mul (wt K kz q) (wt K kz q)) (wt K kz q)) (dot3 K (a q) (a q)))
    rw [show K.mul (k8 K) (K.add (K.mul (lsum K (fun p => K.mul (K.mul (p4 K (ivn K inv kz p)) (p4 K (ivn K inv kz p))) (K.mul (wt K kz p) (wt K kz p))) l)
            (lsum K (fun q => K.mul (wt K kz q) (dot3 K (a q) (a q))) l))
          (K.mul (lsum K (fun p => K.mul (p4 K (ivn K inv kz p)) (p4 K (ivn K inv kz p))) l)
            (lsum K (fun q => K.mul (K.mul (K.mul (wt K kz q) (wt K kz q)) (wt K kz q)) (dot3 K (a q) (a q))) l)))
        = K.add (K.mul (k8 K) (K.mul (lsum K (fun p => K.mul (K.mul (p4 K (ivn K inv kz p)) (p4 K (ivn K inv kz p))) (K.mul (wt K kz p) (wt K kz p))) l)
            (lsum K (fun q => K.mul (wt K kz q) (dot3 K (a q) (a q))) l)))
          (K.mul (k8 K) (K.mul (lsum K (fun p => K.mul (p4 K (ivn K inv kz p)) (p4 K (ivn K inv kz p))) l)
            (lsum K (fun q => K.mul (K.mul (K.mul (wt K kz q) (wt K kz q)) (wt K kz q)) (dot3 K (a q) (a q))) l))) by lib_ring,
      ← d1, ← d2, ← lsum_add]
    apply lsum_ext; intro p
    rw [← lsum_add]
    apply lsum_ext; intro q
    show K.mul (K.mul (K.mul (p4 K (ivn K inv kz p)) (p4 K (ivn K inv kz p)))
        (K.mul (k8 K) (K.add (K.mul (wt K kz p) (wt K kz p)) (K.mul (wt K kz q) (wt K kz q)))))
        (K.mul (wt K kz q) (dot3 K (a q) (a q))) = _
    lib_ring
  rw [e]
  have nk8 : O.nn (k8 K) := by
    unfold k8; exact O.nn_add _ _ (O.nn_add _ _ (O.nn_add _ _ (RatModel.nn_one' K O.toCone) (RatModel.nn_one' K O.toCone))
      (O.nn_add _ _ (RatModel.nn_one' K O.toCone) (RatModel.nn_one' K O.toCone))) (O.nn_add _ _ (O.nn_add _ _ (RatModel.nn_one' K O.toCone) (RatModel.nn_one' K O.toCone))
      (O.nn_add _ _ (RatModel.nn_one' K O.toCone) (RatModel.nn_one' K O.toCone)))
  have nS : O.nn (K.mul (k8 K) (K.add
          (K.mul (lsum K (fun p => K.mul (K.mul (p4 K (ivn K inv kz p)) (p4 K (ivn K inv kz p))) (K.mul (wt K kz p) (wt K kz p))) l)
            (lsum K (fun q => K.mul (wt K kz q) (dot3 K (a q) (a q))) l))
          (K.mul (lsum K (fun p => K.mul (p4 K (ivn K inv kz p)) (p4 K (ivn K inv kz p))) l)
            (lsum K (fun q => K.mul (K.mul (K.mul (wt K kz q) (wt K kz q)) (wt K kz q)) (dot3 K (a q) (a q))) l)))) :=
    O.nn_mul _ _ nk8 (O.nn_add _ _
      (O.nn_mul _ _ (lsum_nn K O.toCone _ (fun p => O.nn_mul _ _ (O.nn_sq _) (O.nn_sq _)) l)
        (lsum_nn K O.toCone _ (fun q => O.nn_mul _ _ (dot3_nn K O.toCone _) (dot3_nn K O.toCone _)) l))
      (O.nn_mul _ _ (lsum_nn K O.toCone _ (fun p => O.nn_sq _) l)
        (lsum_nn K O.toCone _ (fun q => O.nn_mul _ _ (O.nn_mul _ _ (O.nn_sq _) (dot3_nn K O.toCone _)) (dot3_nn K O.toCone _)) l)))
  exact le_mul_nn K O.toCone _ _ _ (O.nn_sq ι) (ForceSob.le_mul_right' K O.toCone hH nS)

end Gen

end ForceH3


#print axioms ForceH3.munu4
#print axioms ForceH3.mu8_le
#print axioms ForceH3.force_h3

ns/ForceH4.lean

lean_all/ns/ForceH4.lean

/-
  ns/ForceH4.lean — T37 cycle 1103 (goal G717, piece b5): the H⁴-level bilinear Galerkin force bound, uniform in the
  truncation, over an ordered field. For integer modes (nonzero, distinct on l), w_r = |k_r|², W = w³:
    tri6        |k_r|⁶ ≤ 32(|k_p|⁶ + |k_q|⁶) for integer triads   ((a+b)³ ≤ 4(a³ + b³) for a, b ≥ 0)
    force_h4    Σ_r w_r³|N_r(u,v)|² ≤ ι²·B₅(u)·(32·(S₁·Z(v) + S₀·Σ w⁴|v|²)),   B₅(u) = Σ w⁵|u|²,
  with S₁ = Σ_p |k_p|_∞^{−10}w_p³ and S₀ = Σ_p |k_p|_∞^{−10}.
  ForceSobW.total_sobWc with W = |k|⁶, c = 32, μ = |k|_∞⁵.

  Lean 4 core plus ns/ForceH3, ns/Tri4, ns/ForceSobW (total_sobWc).
-/
import ns.ForceH3

namespace ForceH4
open DetChain NSChain DyadicSmallGlobal LibRing DyadicLarge FourierGalerkin GalerkinForce ForceBound ForceSob
  ModeCount CubeCount ForceUniform NormCompare EnstGalerkin ForceTwoScale ForceSobW CastZAdd WeightH2 LatticeSum
  LatticeBound ForceH2 Tri4 LatticeStep ForceH3

section Gen
variable {R : Type} (K : CRing R) (O : OrdR K) {I : Type}
variable (inv : R → R) (mul_inv : ∀ y, y ≠ K.zero → K.mul y (inv y) = K.one) (h10 : K.one ≠ K.zero)

def k32 : R := K.mul (k8 K) (K.add (K.add K.one K.one) (K.add K.one K.one))
def cb (y : R) : R := K.mul (K.mul y y) y
def q5 (x : R) : R := K.mul (p4 K x) x
def w3 (kz : I → Mode) (r : I) : R := K.mul (K.mul (wt K kz r) (wt K kz r)) (wt K kz r)

theorem nn32 : O.nn (k32 K) := by
  have two : O.nn (K.add K.one K.one) := O.nn_add _ _ (RatModel.nn_one' K O.toCone) (RatModel.nn_one' K O.toCone)
  exact O.nn_mul _ _ (O.nn_add _ _ (O.nn_add _ _ two two) (O.nn_add _ _ two two)) (O.nn_add _ _ two two)

theorem tri6 (kp kq kr : Mode) (h : TriadSparse.tadd (TriadSparse.tadd kp kq) kr = TriadSparse.zero3) :
    Le K O.toCone (cb K (dot3 K (kvec K kr) (kvec K kr)))
      (K.mul (k32 K) (K.add (cb K (dot3 K (kvec K kp) (kvec K kp))) (cb K (dot3 K (kvec K kq) (kvec K kq))))) := by
  have t := tri_sq K O.toCone kp kq kr h
  have na : O.nn (dot3 K (kvec K kp) (kvec K kp)) := ForceBound.dot3_nn K O.toCone _
  have nb : O.nn (dot3 K (kvec K kq) (kvec K kq)) := ForceBound.dot3_nn K O.toCone _
  have nr : O.nn (dot3 K (kvec K kr) (kvec K kr)) := ForceBound.dot3_nn K O.toCone _
  generalize dot3 K (kvec K kp) (kvec K kp) = a at t na ⊢
  generalize dot3 K (kvec K kq) (kvec K kq) = b at t nb ⊢
  generalize dot3 K (kvec K kr) (kvec K kr) = c at t nr ⊢
  have hs : O.nn (K.add (K.add a a) (K.add b b)) := O.nn_add _ _ (O.nn_add _ _ na na) (O.nn_add _ _ nb nb)
  have sq := EnstLocal.sq_mono K O _ _ nr hs t
  have c3 := TruncRate.mm K O _ _ _ _ (O.nn_sq c) hs sq t
  have one := RatModel.nn_one' K O.toCone
  refine le_trans' K O.toCone _ _ _ c3 ⟨K.mul (k8 K) (K.mul (K.add (K.add K.one K.one) K.one)
      (K.mul (K.add a b) (K.mul (K.add a (K.neg b)) (K.add a (K.neg b))))),
    O.nn_mul _ _ (by unfold k8; exact O.nn_add _ _ (O.nn_add _ _ (O.nn_add _ _ one one) (O.nn_add _ _ one one)) (O.nn_add _ _ (O.nn_add _ _ one one) (O.nn_add _ _ one one)))
      (O.nn_mul _ _ (O.nn_add _ _ (O.nn_add _ _ one one) one) (O.nn_mul _ _ (O.nn_add _ _ na nb) (O.nn_sq _))), ?_⟩
  unfold k32 cb k8; lib_ring

theorem delta_tri6 (kz : I → Mode) (p q r : I) :
    Le K O.toCone (K.mul (TriadSparse.delta K kz p q r) (w3 K kz r))
      (K.mul (TriadSparse.delta K kz p q r) (K.mul (k32 K) (K.add (w3 K kz p) (w3 K kz q)))) := by
  show Le K O.toCone (K.mul (cond (decide (TriadSparse.tadd (TriadSparse.tadd (kz p) (kz q)) (kz r) = TriadSparse.zero3)) K.one K.zero) _)
    (K.mul (cond (decide (TriadSparse.tadd (TriadSparse.tadd (kz p) (kz q)) (kz r) = TriadSparse.zero3)) K.one K.zero) _)
  cases hd : decide (TriadSparse.tadd (TriadSparse.tadd (kz p) (kz q)) (kz r) = TriadSparse.zero3) with
  | false => exact ⟨K.zero, O.nn_zero, by show K.add (K.mul K.zero _) K.zero = K.mul K.zero _; lib_ring⟩
  | true => exact le_mul_nn K O.toCone K.one _ _ (RatModel.nn_one' K O.toCone) (tri6 K O (kz p) (kz q) (kz r) (of_decide_eq_true hd))

include O mul_inv h10 in
theorem munu5 (kz : I → Mode) (h1 : ∀ p, 1 ≤ nrm (kz p)) (p : I) :
    K.mul (q5 K (nn K kz p)) (q5 K (ivn K inv kz p)) = K.one := by
  have e := nn_ivn K O inv mul_inv h10 kz h1 p
  unfold q5 p4
  calc _ = K.mul (K.mul (K.mul (K.mul (nn K kz p) (ivn K inv kz p)) (K.mul (nn K kz p) (ivn K inv kz p)))
        (K.mul (K.mul (nn K kz p) (ivn K inv kz p)) (K.mul (nn K kz p) (ivn K inv kz p)))) (K.mul (nn K kz p) (ivn K inv kz p)) := by lib_ring
    _ = K.one := by rw [e]; lib_ring

/-- n¹⁰ ≤ w⁵ -/
theorem mu10_le (kz : I → Mode) (p : I) :
    Le K O.toCone (K.mul (q5 K (nn K kz p)) (q5 K (nn K kz p)))
      (K.mul (K.mul (K.mul (wt K kz p) (wt K kz p)) (K.mul (wt K kz p) (wt K kz p))) (wt K kz p)) := by
  have h := nrm_le_dot K O.toCone (kz p)
  have hn : O.nn (K.mul (nn K kz p) (nn K kz p)) := O.nn_sq _
  have hw : O.nn (wt K kz p) := dot3_nn K O.toCone _
  have s2 := EnstLocal.sq_mono K O _ _ hn hw h
  have s4 := EnstLocal.sq_mono K O _ _ (O.nn_sq _) (O.nn_sq _) s2
  have s5 := TruncRate.mm K O _ _ _ _ (O.nn_mul _ _ (O.nn_sq _) (O.nn_sq _)) hw s4 h
  have e : K.mul (q5 K (nn K kz p)) (q5 K (nn K kz p))
      = K.mul (K.mul (K.mul (K.mul (nn K kz p) (nn K kz p)) (K.mul (nn K kz p) (nn K kz p)))
          (K.mul (K.mul (nn K kz p) (nn K kz p)) (K.mul (nn K kz p) (nn K kz p)))) (K.mul (nn K kz p) (nn K kz p)) := by
    unfold q5 p4; lib_ring
  rw [e]; exact s5

include mul_inv h10 in
theorem force_h4 (l : List I) (kz : I → Mode) (hd : List.Pairwise (fun a b => kz a ≠ kz b) l)
    (h1 : ∀ p, 1 ≤ nrm (kz p)) (ι : R) (u v : I → V3 R) :
    Le K O.toCone (lsum K (fun r => K.mul (w3 K kz r) (dot3 K (Nl K l (TriadSparse.delta K kz) ι (kv K kz) u v r)
        (Nl K l (TriadSparse.delta K kz) ι (kv K kz) u v r))) l)
      (K.mul (K.mul ι ι) (K.mul (lsum K (fun p => K.mul (K.mul (K.mul (K.mul (wt K kz p) (wt K kz p)) (K.mul (wt K kz p) (wt K kz p))) (wt K kz p)) (dot3 K (u p) (u p))) l)
        (K.mul (k32 K) (K.add
          (K.mul (lsum K (fun p => K.mul (K.mul (q5 K (ivn K inv kz p)) (q5 K (ivn K inv kz p))) (w3 K kz p)) l)
            (lsum K (fun q => K.mul (wt K kz q) (dot3 K (v q) (v q))) l))
          (K.mul (lsum K (fun p => K.mul (q5 K (ivn K inv kz p)) (q5 K (ivn K inv kz p))) l)
            (lsum K (fun q => K.mul (K.mul (w3 K kz q) (wt K kz q)) (dot3 K (v q) (v q))) l)))))) := by
  have hw : ∀ r, O.nn (wt K kz r) := fun r => dot3_nn K O.toCone _
  have hw3 : ∀ r, O.nn (w3 K kz r) := fun r => O.nn_mul _ _ (O.nn_sq _) (hw r)
  have T := total_sobWc K O.toCone l (TriadSparse.delta K kz) ι (kv K kz) u v
    (fun p => q5 K (nn K kz p)) (fun p => q5 K (ivn K inv kz p)) (w3 K kz) hw3 (k32 K) (nn32 K O)
    (fun p q r => delta_tri6 K O kz p q r) (delta01 K kz) (munu5 K O inv mul_inv h10 kz h1)
    (fun p r => TriadSparse.spA K O.toCone kz l hd p r) (fun p q => TriadSparse.spB K O.toCone kz l hd p q)
  refine le_trans' K O.toCone _ _ _ T ?_
  have hH := lsum_le K O.toCone _ _ (fun p => le_mul_right' K O.toCone (mu10_le K O kz p) (dot3_nn K O.toCone (u p))) l
  have e : lsum K (fun p => lsum K (fun q => K.mul (K.mul (K.mul (q5 K (ivn K inv kz p)) (q5 K (ivn K inv kz p)))
        (K.mul (k32 K) (K.add (w3 K kz p) (w3 K kz q))))
        (K.mul (dot3 K (kv K kz q) (kv K kz q)) (dot3 K (v q) (v q)))) l) l
      = K.mul (k32 K) (K.add
          (K.mul (lsum K (fun p => K.mul (K.mul (q5 K (ivn K inv kz p)) (q5 K (ivn K inv kz p))) (w3 K kz p)) l)
            (lsum K (fun q => K.mul (wt K kz q) (dot3 K (v q) (v q))) l))
          (K.mul (lsum K (fun p => K.mul (q5 K (ivn K inv kz p)) (q5 K (ivn K inv kz p))) l)
            (lsum K (fun q => K.mul (K.mul (w3 K kz q) (wt K kz q)) (dot3 K (v q) (v q))) l))) := by
    have d1 := double_factor K l (k32 K) (fun p => K.mul (K.mul (q5 K (ivn K inv kz p)) (q5 K (ivn K inv kz p))) (w3 K kz p))
      (fun q => K.mul (wt K kz q) (dot3 K (v q) (v q)))
    have d2 := double_factor K l (k32 K) (fun p => K.mul (q5 K (ivn K inv kz p)) (q5 K (ivn K inv kz p)))
      (fun q => K.mul (K.mul (w3 K kz q) (wt K kz q)) (dot3 K (v q) (v q)))
    rw [show K.mul (k32 K) (K.add
          (K.mul (lsum K (fun p => K.mul (K.mul (q5 K (ivn K inv kz p)) (q5 K (ivn K inv kz p))) (w3 K kz p)) l)
            (lsum K (fun q => K.mul (wt K kz q) (dot3 K (v q) (v q))) l))
          (K.mul (lsum K (fun p => K.mul (q5 K (ivn K inv kz p)) (q5 K (ivn K inv kz p))) l)
            (lsum K (fun q => K.mul (K.mul (w3 K kz q) (wt K kz q)) (dot3 K (v q) (v q))) l)))
        = K.add (K.mul (k32 K) (K.mul (lsum K (fun p => K.mul (K.mul (q5 K (ivn K inv kz p)) (q5 K (ivn K inv kz p))) (w3 K kz p)) l)
            (lsum K (fun q => K.mul (wt K kz q) (dot3 K (v q) (v q))) l)))
          (K.mul (k32 K) (K.mul (lsum K (fun p => K.mul (q5 K (ivn K inv kz p)) (q5 K (ivn K inv kz p))) l)
            (lsum K (fun q => K.mul (K.mul (w3 K kz q) (wt K kz q)) (dot3 K (v q) (v q))) l))) by lib_ring,
      ← d1, ← d2, ← lsum_add]
    apply lsum_ext; intro p
    rw [← lsum_add]
    apply lsum_ext; intro q
    show K.mul (K.mul (K.mul (q5 K (ivn K inv kz p)) (q5 K (ivn K inv kz p)))
        (K.mul (k32 K) (K.add (w3 K kz p) (w3 K kz q))))
        (K.mul (wt K kz q) (dot3 K (v q) (v q))) = _
    lib_ring
  rw [e]
  have nS : O.nn (K.mul (k32 K) (K.add
          (K.mul (lsum K (fun p => K.mul (K.mul (q5 K (ivn K inv kz p)) (q5 K (ivn K inv kz p))) (w3 K kz p)) l)
            (lsum K (fun q => K.mul (wt K kz q) (dot3 K (v q) (v q))) l))
          (K.mul (lsum K (fun p => K.mul (q5 K (ivn K inv kz p)) (q5 K (ivn K inv kz p))) l)
            (lsum K (fun q => K.mul (K.mul (w3 K kz q) (wt K kz q)) (dot3 K (v q) (v q))) l)))) :=
    O.nn_mul _ _ (nn32 K O) (O.nn_add _ _
      (O.nn_mul _ _ (lsum_nn K O.toCone _ (fun p => O.nn_mul _ _ (O.nn_sq _) (hw3 p)) l)
        (lsum_nn K O.toCone _ (fun q => O.nn_mul _ _ (hw q) (dot3_nn K O.toCone _)) l))
      (O.nn_mul _ _ (lsum_nn K O.toCone _ (fun p => O.nn_sq _) l)
        (lsum_nn K O.toCone _ (fun q => O.nn_mul _ _ (O.nn_mul _ _ (hw3 q) (hw q)) (dot3_nn K O.toCone _)) l)))
  exact le_mul_nn K O.toCone _ _ _ (O.nn_sq ι) (ForceSob.le_mul_right' K O.toCone hH nS)

end Gen

end ForceH4

#print axioms ForceH4.nn32
#print axioms ForceH4.tri6
#print axioms ForceH4.delta_tri6
#print axioms ForceH4.munu5
#print axioms ForceH4.mu10_le
#print axioms ForceH4.force_h4

ns/ForceHyps.lean

lean_all/ns/ForceHyps.lean

/-
  ns/ForceHyps.lean — T37 cycle 1191 (goal G768, piece 2): the force-norm hypotheses of the defect chain from energy
  bounds, over an ordered field. For a force N with the projection bounds Σ W|N|² ≤ Σ W|N(a,a)|² (W = w, w², w³):
    g1_le   Σ w |N|² ≤ ι²·E₃·(cH·E₂)              if B(a) ≤ E₃, A(a) ≤ E₂
    g2_le   Σ w²|N|² ≤ ι²·E₄·(c₃·E₃)              if B₄(a) ≤ E₄, B(a) ≤ E₃
    g3_le   k·ν²·Σ w³|N|² ≤ ι²·Q·(c₄·E₄)          if k·ν²·B₅(a) ≤ Q, B₄(a) ≤ E₄
    diff_le Σ V|N₀ − N₁|² ≤ 2ΣV|N₀|² + 2ΣV|N₁|²
  (ForceNorms.nrm_h1–h3 and monotonicity). These are the constants G₁, G₂ and, after dividing by ν², G₃ of
  FDk.fd_scheme' and E of LocalDefect.local_defect', uniform in the step and the truncation.

  Lean 4 core plus ns/ForceNorms.
-/
import ns.ForceNorms

namespace ForceHyps
open DetChain NSChain DyadicSmallGlobal LibRing DyadicLarge FourierGalerkin GalerkinForce ForceBound ForceSob
  ModeCount CubeCount ForceUniform NormCompare EnstGalerkin ForceTwoScale ForceSobW CastZAdd WeightH2 LatticeSum
  LatticeBound LatticeStep ForceH2 ForceH3 Tri4 ForceH4 EnstH2 EnstH3 EnstH4 ForceNorms

section Gen
variable {R : Type} (K : CRing R) (O : OrdR K) {I : Type}
variable (inv : R → R) (mul_inv : ∀ y, y ≠ K.zero → K.mul y (inv y) = K.one) (h10 : K.one ≠ K.zero)

theorem cH_nn : O.nn (cH K) := by
  have two : O.nn (K.add K.one K.one) := O.nn_add _ _ (RatModel.nn_one' K O.toCone) (RatModel.nn_one' K O.toCone)
  exact O.nn_add _ _ (O.nn_mul _ _ two (O.nn_mul _ _ (O.nn_add _ _ two (RatModel.nn_one' K O.toCone)) (RatModel.nn_cast K O.toCone 135)))
    (O.nn_mul _ _ two (RatModel.nn_cast K O.toCone 135))

include mul_inv h10 in
theorem g1_le (l : List I) (kz : I → Mode) (hd : List.Pairwise (fun a b => kz a ≠ kz b) l)
    (h1 : ∀ p, 1 ≤ nrm (kz p)) (M : Nat) (hM : ∀ p, p ∈ l → nrm (kz p) ≤ M + 1) (ι E3 E2 : R) (a N : I → V3 R)
    (hP : Le K O.toCone (lsum K (fun r => K.mul (wt K kz r) (dot3 K (N r) (N r))) l)
      (lsum K (fun r => K.mul (wt K kz r) (dot3 K (Nl K l (TriadSparse.delta K kz) ι (kv K kz) a a r)
        (Nl K l (TriadSparse.delta K kz) ι (kv K kz) a a r))) l))
    (hB : Le K O.toCone (lsum K (fun p => K.mul (K.mul (K.mul (wt K kz p) (wt K kz p)) (wt K kz p)) (dot3 K (a p) (a p))) l) E3)
    (hA : Le K O.toCone (lsum K (fun p => K.mul (K.mul (wt K kz p) (wt K kz p)) (dot3 K (a p) (a p))) l) E2) :
    Le K O.toCone (lsum K (fun r => K.mul (wt K kz r) (dot3 K (N r) (N r))) l) (K.mul (K.mul ι ι) (K.mul E3 (K.mul (cH K) E2))) := by
  have n := nrm_h1 K O inv mul_inv h10 l kz hd h1 M hM ι a
  have nA : O.nn (lsum K (fun p => K.mul (K.mul (wt K kz p) (wt K kz p)) (dot3 K (a p) (a p))) l) :=
    lsum_nn K O.toCone _ (fun p => O.nn_mul _ _ (O.nn_sq _) (dot3_nn K O.toCone _)) l
  have nB : O.nn (lsum K (fun p => K.mul (K.mul (K.mul (wt K kz p) (wt K kz p)) (wt K kz p)) (dot3 K (a p) (a p))) l) :=
    lsum_nn K O.toCone _ (fun p => O.nn_mul _ _ (O.nn_mul _ _ (O.nn_sq _) (dot3_nn K O.toCone _)) (dot3_nn K O.toCone _)) l
  have m := TruncRate.mm K O _ _ _ _ nB (O.nn_mul _ _ (cH_nn K O) (TruncRate.nn_of_le K O _ _ nA hA)) hB
    (le_mul_nn K O.toCone _ _ _ (cH_nn K O) hA)
  exact le_trans' K O.toCone _ _ _ hP (le_trans' K O.toCone _ _ _ n (le_mul_nn K O.toCone _ _ _ (O.nn_sq ι) m))

theorem c3_nn : O.nn (c3 K) := O.nn_mul _ _ (nn8 K O) (O.nn_add _ _ (O.nn_mul _ _ (nn9 K O) (RatModel.nn_cast K O.toCone 135)) (RatModel.nn_cast K O.toCone 135))

theorem c4_nn : O.nn (c4 K) := O.nn_mul _ _ (nn32 K O) (O.nn_add _ _ (O.nn_mul _ _ (nn27 K O) (RatModel.nn_cast K O.toCone 135)) (RatModel.nn_cast K O.toCone 135))

include mul_inv h10 in
theorem g2_le (l : List I) (kz : I → Mode) (hd : List.Pairwise (fun a b => kz a ≠ kz b) l)
    (h1 : ∀ p, 1 ≤ nrm (kz p)) (M : Nat) (hM : ∀ p, p ∈ l → nrm (kz p) ≤ M + 1) (ι E4 E3 : R) (a N : I → V3 R)
    (hP : Le K O.toCone (lsum K (fun r => K.mul (K.mul (wt K kz r) (wt K kz r)) (dot3 K (N r) (N r))) l)
      (lsum K (fun r => K.mul (K.mul (wt K kz r) (wt K kz r)) (dot3 K (Nl K l (TriadSparse.delta K kz) ι (kv K kz) a a r)
        (Nl K l (TriadSparse.delta K kz) ι (kv K kz) a a r))) l))
    (hB4 : Le K O.toCone (lsum K (fun p => K.mul (K.mul (K.mul (wt K kz p) (wt K kz p)) (K.mul (wt K kz p) (wt K kz p))) (dot3 K (a p) (a p))) l) E4)
    (hB : Le K O.toCone (lsum K (fun p => K.mul (K.mul (wt K kz p) (K.mul (wt K kz p) (wt K kz p))) (dot3 K (a p) (a p))) l) E3) :
    Le K O.toCone (lsum K (fun r => K.mul (K.mul (wt K kz r) (wt K kz r)) (dot3 K (N r) (N r))) l) (K.mul (K.mul ι ι) (K.mul E4 (K.mul (c3 K) E3))) := by
  have n := nrm_h2 K O inv mul_inv h10 l kz hd h1 M hM ι a
  have hw : ∀ r, O.nn (wt K kz r) := fun r => dot3_nn K O.toCone _
  have nB4 : O.nn (lsum K (fun p => K.mul (K.mul (K.mul (wt K kz p) (wt K kz p)) (K.mul (wt K kz p) (wt K kz p))) (dot3 K (a p) (a p))) l) :=
    lsum_nn K O.toCone _ (fun p => O.nn_mul _ _ (O.nn_mul _ _ (O.nn_sq _) (O.nn_sq _)) (dot3_nn K O.toCone _)) l
  have nB : O.nn (lsum K (fun p => K.mul (K.mul (wt K kz p) (K.mul (wt K kz p) (wt K kz p))) (dot3 K (a p) (a p))) l) :=
    lsum_nn K O.toCone _ (fun p => O.nn_mul _ _ (O.nn_mul _ _ (hw p) (O.nn_sq _)) (dot3_nn K O.toCone _)) l
  have m := TruncRate.mm K O _ _ _ _ nB4 (O.nn_mul _ _ (c3_nn K O) (TruncRate.nn_of_le K O _ _ nB hB)) hB4
    (le_mul_nn K O.toCone _ _ _ (c3_nn K O) hB)
  exact le_trans' K O.toCone _ _ _ hP (le_trans' K O.toCone _ _ _ n (le_mul_nn K O.toCone _ _ _ (O.nn_sq ι) m))

include mul_inv h10 in
theorem g3_le (l : List I) (kz : I → Mode) (hd : List.Pairwise (fun a b => kz a ≠ kz b) l)
    (h1 : ∀ p, 1 ≤ nrm (kz p)) (M : Nat) (hM : ∀ p, p ∈ l → nrm (kz p) ≤ M + 1) (ι k ν Q E4 : R) (hk : O.nn k) (hν : O.nn ν)
    (a N : I → V3 R)
    (hP : Le K O.toCone (lsum K (fun r => K.mul (w3 K kz r) (dot3 K (N r) (N r))) l)
      (lsum K (fun r => K.mul (w3 K kz r) (dot3 K (Nl K l (TriadSparse.delta K kz) ι (kv K kz) a a r)
        (Nl K l (TriadSparse.delta K kz) ι (kv K kz) a a r))) l))
    (hQ : Le K O.toCone (K.mul (K.mul k (K.mul ν ν))
      (lsum K (fun p => K.mul (K.mul (K.mul (wt K kz p) (wt K kz p)) (w3 K kz p)) (dot3 K (a p) (a p))) l)) Q)
    (hB4 : Le K O.toCone (lsum K (fun p => K.mul (K.mul (wt K kz p) (w3 K kz p)) (dot3 K (a p) (a p))) l) E4) :
    Le K O.toCone (K.mul (K.mul k (K.mul ν ν)) (lsum K (fun r => K.mul (w3 K kz r) (dot3 K (N r) (N r))) l))
      (K.mul (K.mul ι ι) (K.mul Q (K.mul (c4 K) E4))) := by
  have n := nrm_h3 K O inv mul_inv h10 l kz hd h1 M hM ι a
  have hw : ∀ r, O.nn (wt K kz r) := fun r => dot3_nn K O.toCone _
  have hw3 : ∀ r, O.nn (w3 K kz r) := fun r => O.nn_mul _ _ (O.nn_sq _) (hw r)
  have nB4 : O.nn (lsum K (fun p => K.mul (K.mul (wt K kz p) (w3 K kz p)) (dot3 K (a p) (a p))) l) :=
    lsum_nn K O.toCone _ (fun p => O.nn_mul _ _ (O.nn_mul _ _ (hw p) (hw3 p)) (dot3_nn K O.toCone _)) l
  have nB5 : O.nn (lsum K (fun p => K.mul (K.mul (K.mul (wt K kz p) (wt K kz p)) (w3 K kz p)) (dot3 K (a p) (a p))) l) :=
    lsum_nn K O.toCone _ (fun p => O.nn_mul _ _ (O.nn_mul _ _ (O.nn_sq _) (hw3 p)) (dot3_nn K O.toCone _)) l
  have hkv : O.nn (K.mul k (K.mul ν ν)) := O.nn_mul _ _ hk (O.nn_sq ν)
  have a1 := le_mul_nn K O.toCone _ _ _ hkv (le_trans' K O.toCone _ _ _ hP n)
  have m := TruncRate.mm K O _ _ _ _ (O.nn_mul _ _ hkv nB5) (O.nn_mul _ _ (c4_nn K O) (TruncRate.nn_of_le K O _ _ nB4 hB4)) hQ
    (le_mul_nn K O.toCone _ _ _ (c4_nn K O) hB4)
  refine le_trans' K O.toCone _ _ _ a1 (le_trans' K O.toCone _ _ _ ⟨K.zero, O.nn_zero, ?_⟩ (le_mul_nn K O.toCone _ _ _ (O.nn_sq ι) m))
  generalize lsum K (fun p => K.mul (K.mul (K.mul (wt K kz p) (wt K kz p)) (w3 K kz p)) (dot3 K (a p) (a p))) l = B5
  generalize lsum K (fun p => K.mul (K.mul (wt K kz p) (w3 K kz p)) (dot3 K (a p) (a p))) l = B4
  generalize c4 K = c
  lib_ring

/-- Σ V|N₀ − N₁|² ≤ 2Σ V|N₀|² + 2Σ V|N₁|² -/
theorem diff_le (l : List I) (V : I → R) (hV : ∀ r, O.nn (V r)) (N0 N1 : I → V3 R) :
    Le K O.toCone (lsum K (fun r => K.mul (V r) (dot3 K (EnstDiff.sub3 K (N0 r) (N1 r)) (EnstDiff.sub3 K (N0 r) (N1 r)))) l)
      (K.add (K.mul (K.add K.one K.one) (lsum K (fun r => K.mul (V r) (dot3 K (N0 r) (N0 r))) l))
        (K.mul (K.add K.one K.one) (lsum K (fun r => K.mul (V r) (dot3 K (N1 r) (N1 r))) l))) := by
  have t := lsum_le K O.toCone _ _ (fun r => le_mul_nn K O.toCone (V r) _ _ (hV r)
    (sq2 K O (N0 r) (FourierGalerkin.neg3 K (N1 r)))) l
  refine le_trans' K O.toCone _ _ _ ⟨K.zero, O.nn_zero, ?_⟩ (le_trans' K O.toCone _ _ _ t ⟨K.zero, O.nn_zero, ?_⟩)
  · rw [add_zero']; rfl
  · rw [add_zero', ← lsum_mul_left, ← lsum_mul_left, ← lsum_add]
    refine lsum_ext K _ _ (fun r => ?_) l
    show K.mul (V r) (K.add (K.mul (K.add K.one K.one) (dot3 K (N0 r) (N0 r)))
        (K.mul (K.add K.one K.one) (K.add (K.add (K.mul (K.neg (N1 r).x) (K.neg (N1 r).x)) (K.mul (K.neg (N1 r).y) (K.neg (N1 r).y)))
          (K.mul (K.neg (N1 r).z) (K.neg (N1 r).z))))) = _
    show _ = K.add (K.mul (K.add K.one K.one) (K.mul (V r) (dot3 K (N0 r) (N0 r))))
      (K.mul (K.add K.one K.one) (K.mul (V r) (K.add (K.add (K.mul (N1 r).x (N1 r).x) (K.mul (N1 r).y (N1 r).y)) (K.mul (N1 r).z (N1 r).z))))
    lib_ring

end Gen

end ForceHyps

#print axioms ForceHyps.cH_nn
#print axioms ForceHyps.g1_le
#print axioms ForceHyps.c3_nn
#print axioms ForceHyps.c4_nn
#print axioms ForceHyps.g2_le
#print axioms ForceHyps.g3_le
#print axioms ForceHyps.diff_le

ns/ForceLip.lean

lean_all/ns/ForceLip.lean

/-
  ns/ForceLip.lean — T37 cycle 923 (goal G614b, piece 3): the projected Galerkin force is locally Lipschitz, over
  any commutative ring with a nonnegative cone (ℚ included). For two states a, b with d = a − b, on a mode list l:
    Nl_sub     N(a, a) − N(b, b) = N(d, a) + N(b, d)
    lip_mode   |P_r N_r(a, a) − P_r N_r(b, b)|²
                 ≤ 2ι² (Σ_q|k_q|²) [ (Σ_p|d_p|²)(Σ_pΣ_q|a_q|²) + (Σ_p|b_p|²)(Σ_pΣ_q|d_q|²) ]
  (Leray projection linear and contracting, ForceBilin.Nl_diff, ForceBound.force_mode, |X + Y|² ≤ 2|X|² + 2|Y|²).

  Lean 4 core plus ns/ForceBilin, ns/ForceBound, ns/LerayProj3 (proj_le), ns/StepStab3 (sub3).
-/
import ns.ForceBilin
import ns.ForceBound
import ns.LerayProj3
import ns.StepStab3

namespace ForceLip
open DetChain NSChain LibRing DyadicLarge FourierGalerkin GalerkinForce ForceBilin ForceBound LerayProj LerayProj3 StepStab3

section Gen
variable {R : Type} (K : CRing R) (P : Cone K) {I : Type}

theorem Nl_congr (l : List I) (δ : I → I → I → R) (ι : R) (k u u' v v' : I → V3 R) (r : I)
    (hu : ∀ p, u p = u' p) (hv : ∀ q, v q = v' q) : Nl K l δ ι k u v r = Nl K l δ ι k u' v' r := by
  have hc : ∀ p q, coef K δ ι k u p q r = coef K δ ι k u' p q r := by
    intro p q; unfold coef; rw [hu p]
  apply v3_ext
  · show lsum K (fun p => lsum K (fun q => K.mul (coef K δ ι k u p q r) (v q).x) l) l
      = lsum K (fun p => lsum K (fun q => K.mul (coef K δ ι k u' p q r) (v' q).x) l) l
    apply lsum_ext; intro p; apply lsum_ext; intro q; rw [hc, hv]
  · show lsum K (fun p => lsum K (fun q => K.mul (coef K δ ι k u p q r) (v q).y) l) l
      = lsum K (fun p => lsum K (fun q => K.mul (coef K δ ι k u' p q r) (v' q).y) l) l
    apply lsum_ext; intro p; apply lsum_ext; intro q; rw [hc, hv]
  · show lsum K (fun p => lsum K (fun q => K.mul (coef K δ ι k u p q r) (v q).z) l) l
      = lsum K (fun p => lsum K (fun q => K.mul (coef K δ ι k u' p q r) (v' q).z) l) l
    apply lsum_ext; intro p; apply lsum_ext; intro q; rw [hc, hv]

theorem add_sub3 (b a : V3 R) : add3 K b (sub3 K a b) = a := by
  apply v3_ext
  · show K.add b.x (K.add a.x (K.neg b.x)) = a.x
    lib_ring
  · show K.add b.y (K.add a.y (K.neg b.y)) = a.y
    lib_ring
  · show K.add b.z (K.add a.z (K.neg b.z)) = a.z
    lib_ring

theorem Nl_sub (l : List I) (δ : I → I → I → R) (ι : R) (k a b : I → V3 R) (r : I) :
    sub3 K (Nl K l δ ι k a a r) (Nl K l δ ι k b b r)
      = add3 K (Nl K l δ ι k (fun p => sub3 K (a p) (b p)) a r) (Nl K l δ ι k b (fun p => sub3 K (a p) (b p)) r) := by
  have ha : ∀ p, addF K b (fun p => sub3 K (a p) (b p)) p = a p := fun p => add_sub3 K (b p) (a p)
  have e := Nl_diff K l δ ι k b (fun p => sub3 K (a p) (b p)) r
  rw [Nl_congr K l δ ι k _ a _ a r ha ha,
    Nl_congr K l δ ι k (fun p => sub3 K (a p) (b p)) (fun p => sub3 K (a p) (b p)) _ a r (fun _ => rfl) ha] at e
  rw [e]
  generalize Nl K l δ ι k b b r = A; generalize Nl K l δ ι k (fun p => sub3 K (a p) (b p)) a r = B
  generalize Nl K l δ ι k b (fun p => sub3 K (a p) (b p)) r = C
  apply v3_ext
  · show K.add (K.add (K.add A.x B.x) C.x) (K.neg A.x) = K.add B.x C.x
    lib_ring
  · show K.add (K.add (K.add A.y B.y) C.y) (K.neg A.y) = K.add B.y C.y
    lib_ring
  · show K.add (K.add (K.add A.z B.z) C.z) (K.neg A.z) = K.add B.z C.z
    lib_ring

theorem proj_sub (κ : R) (k v w : V3 R) : sub3 K (proj K κ k v) (proj K κ k w) = proj K κ k (sub3 K v w) := by
  cases k with
  | mk k1 k2 k3 => cases v with
    | mk v1 v2 v3 => cases w with
      | mk w1 w2 w3 =>
        apply v3_ext
        · show K.add (K.add v1 (K.neg (K.mul (K.mul (K.add (K.add (K.mul k1 v1) (K.mul k2 v2)) (K.mul k3 v3)) κ) k1)))
              (K.neg (K.add w1 (K.neg (K.mul (K.mul (K.add (K.add (K.mul k1 w1) (K.mul k2 w2)) (K.mul k3 w3)) κ) k1))))
            = K.add (K.add v1 (K.neg w1)) (K.neg (K.mul (K.mul (K.add (K.add (K.mul k1 (K.add v1 (K.neg w1)))
                (K.mul k2 (K.add v2 (K.neg w2)))) (K.mul k3 (K.add v3 (K.neg w3)))) κ) k1))
          lib_ring
        · show K.add (K.add v2 (K.neg (K.mul (K.mul (K.add (K.add (K.mul k1 v1) (K.mul k2 v2)) (K.mul k3 v3)) κ) k2)))
              (K.neg (K.add w2 (K.neg (K.mul (K.mul (K.add (K.add (K.mul k1 w1) (K.mul k2 w2)) (K.mul k3 w3)) κ) k2))))
            = K.add (K.add v2 (K.neg w2)) (K.neg (K.mul (K.mul (K.add (K.add (K.mul k1 (K.add v1 (K.neg w1)))
                (K.mul k2 (K.add v2 (K.neg w2)))) (K.mul k3 (K.add v3 (K.neg w3)))) κ) k2))
          lib_ring
        · show K.add (K.add v3 (K.neg (K.mul (K.mul (K.add (K.add (K.mul k1 v1) (K.mul k2 v2)) (K.mul k3 v3)) κ) k3)))
              (K.neg (K.add w3 (K.neg (K.mul (K.mul (K.add (K.add (K.mul k1 w1) (K.mul k2 w2)) (K.mul k3 w3)) κ) k3))))
            = K.add (K.add v3 (K.neg w3)) (K.neg (K.mul (K.mul (K.add (K.add (K.mul k1 (K.add v1 (K.neg w1)))
                (K.mul k2 (K.add v2 (K.neg w2)))) (K.mul k3 (K.add v3 (K.neg w3)))) κ) k3))
          lib_ring

theorem sq_add_le (X Y : V3 R) :
    Le K P (dot3 K (add3 K X Y) (add3 K X Y)) (K.add (K.mul (K.add K.one K.one) (dot3 K X X)) (K.mul (K.add K.one K.one) (dot3 K Y Y))) := by
  refine ⟨dot3 K (sub3 K X Y) (sub3 K X Y), dot3_nn K P _, ?_⟩
  show K.add (K.add (K.add (K.mul (K.add X.x Y.x) (K.add X.x Y.x)) (K.mul (K.add X.y Y.y) (K.add X.y Y.y))) (K.mul (K.add X.z Y.z) (K.add X.z Y.z)))
      (K.add (K.add (K.mul (K.add X.x (K.neg Y.x)) (K.add X.x (K.neg Y.x))) (K.mul (K.add X.y (K.neg Y.y)) (K.add X.y (K.neg Y.y))))
        (K.mul (K.add X.z (K.neg Y.z)) (K.add X.z (K.neg Y.z))))
    = K.add (K.mul (K.add K.one K.one) (K.add (K.add (K.mul X.x X.x) (K.mul X.y X.y)) (K.mul X.z X.z)))
      (K.mul (K.add K.one K.one) (K.add (K.add (K.mul Y.x Y.x) (K.mul Y.y Y.y)) (K.mul Y.z Y.z)))
  lib_ring

theorem lip_mode (l : List I) (δ : I → I → I → R) (ι : R) (k a b : I → V3 R) (κ : R) (r : I)
    (hδ : ∀ p q r, δ p q r = K.zero ∨ δ p q r = K.one) (hκ : P.nn κ) (hκk : K.mul (dot3 K (k r) (k r)) κ = K.one) :
    Le K P (dot3 K (sub3 K (proj K κ (k r) (Nl K l δ ι k a a r)) (proj K κ (k r) (Nl K l δ ι k b b r)))
        (sub3 K (proj K κ (k r) (Nl K l δ ι k a a r)) (proj K κ (k r) (Nl K l δ ι k b b r))))
      (K.add (K.mul (K.add K.one K.one) (K.mul (K.mul (K.mul ι ι) (K.mul (lsum K (fun p => dot3 K (sub3 K (a p) (b p)) (sub3 K (a p) (b p))) l)
          (lsum K (fun q => dot3 K (k q) (k q)) l))) (lsum K (fun _ => lsum K (fun q => dot3 K (a q) (a q)) l) l)))
        (K.mul (K.add K.one K.one) (K.mul (K.mul (K.mul ι ι) (K.mul (lsum K (fun p => dot3 K (b p) (b p)) l)
          (lsum K (fun q => dot3 K (k q) (k q)) l))) (lsum K (fun _ => lsum K (fun q => dot3 K (sub3 K (a q) (b q)) (sub3 K (a q) (b q))) l) l)))) := by
  rw [proj_sub, Nl_sub]
  have two_nn : P.nn (K.add K.one K.one) := P.nn_add _ _ (StepStab.one_nn K P) (StepStab.one_nn K P)
  have f1 := le_mul_nn K P _ _ _ two_nn (force_mode K P l δ ι k (fun p => sub3 K (a p) (b p)) a r hδ)
  have f2 := le_mul_nn K P _ _ _ two_nn (force_mode K P l δ ι k b (fun p => sub3 K (a p) (b p)) r hδ)
  exact le_trans' K P _ _ _ (proj_le K P κ (k r) _ hκ hκk)
    (le_trans' K P _ _ _ (sq_add_le K P _ _) (le_add' K P _ _ _ _ f1 f2))

end Gen

end ForceLip

#print axioms ForceLip.Nl_congr
#print axioms ForceLip.add_sub3
#print axioms ForceLip.Nl_sub
#print axioms ForceLip.proj_sub
#print axioms ForceLip.sq_add_le
#print axioms ForceLip.lip_mode

ns/ForceLipZ.lean

lean_all/ns/ForceLipZ.lean

/-
  ns/ForceLipZ.lean — T37 cycle 1898 (goal G860e, piece b2): on a fixed finite mode list the Galerkin force is
  Lipschitz in the enstrophy distance. With W ≥ every weight |k_p|² on l, Z(a) ≤ Za and A(b) ≤ Ab (H² bounds):
    lsum_le_mem   termwise bounds on the members of l only
    ad_le         A(d) ≤ W·Z(d)
    force_lip     Σ_r |N_r(a,a) − N_r(b,b)|² ≤ L·Z(a − b),
                  L = 2(2ι²·C₁·Za) + 2(2ι²·W·C₂·Za) + 2ι²·Ab·135      (ForceDiff.force_diff)
    force_comp    (N_r(a,a).x − N_r(b,b).x)² ≤ L·Z(a − b) for r ∈ l (any component selector)
  The constant depends on l through W (the time-step limit is taken at a fixed truncation; the truncation limit is
  TruncUniform's). With QeComp, Z(a − b) ≤ Qe(a, b)/ν.
  Lean 4 core plus ns/ForceDiff, ns/QeComp.
-/
import ns.ForceDiff
import ns.QeComp

namespace ForceLipZ
open DetChain NSChain DyadicSmallGlobal LibRing DyadicLarge FourierGalerkin GalerkinForce ForceBound ForceSob
  ModeCount CubeCount ForceUniform NormCompare EnstGalerkin ForceTwoScale ForceBilin EnstDiff LatticeSum ForceDiff
  RealCoef DyadicCoef

section Gen
variable {R : Type} (K : CRing R) (O : OrdR K) {I : Type}
variable (inv : R → R) (mul_inv : ∀ y, y ≠ K.zero → K.mul y (inv y) = K.one) (h10 : K.one ≠ K.zero)

theorem lsum_le_mem (f g : I → R) : ∀ l : List I, (∀ p, p ∈ l → Le K O.toCone (f p) (g p)) →
    Le K O.toCone (lsum K f l) (lsum K g l)
  | [], _ => le_refl' K O.toCone _
  | x :: l, h => le_add' K O.toCone _ _ _ _ (h x (List.Mem.head _)) (lsum_le_mem f g l (fun p hp => h p (List.Mem.tail _ hp)))

theorem nn_of_le (x y : R) (hx : O.nn x) (h : Le K O.toCone x y) : O.nn y := by
  cases h with
  | intro d hd => rw [← hd.2]; exact O.nn_add _ _ hx hd.1

theorem ad_le (kz : I → Mode) (l : List I) (W : R) (hW : ∀ p, p ∈ l → Le K O.toCone (wt K kz p) W) (d : I → V3 R) :
    Le K O.toCone (lsum K (fun p => K.mul (K.mul (wt K kz p) (wt K kz p)) (dot3 K (d p) (d p))) l)
      (K.mul W (lsum K (fun p => K.mul (wt K kz p) (dot3 K (d p) (d p))) l)) := by
  rw [← lsum_mul_left K W]
  refine lsum_le_mem K O _ _ l (fun p hp => ?_)
  have a := le_mul_right' K O.toCone (z := K.mul (wt K kz p) (dot3 K (d p) (d p))) (hW p hp)
    (O.nn_mul _ _ (dot3_nn K O.toCone (kv K kz p)) (dot3_nn K O.toCone (d p)))
  refine le_trans' K O.toCone _ _ _ ⟨K.zero, O.nn_zero, ?_⟩ (le_trans' K O.toCone _ _ _ a ⟨K.zero, O.nn_zero, ?_⟩) <;> lib_ring

include mul_inv h10 in
theorem force_lip (l : List I) (kz : I → Mode) (hd : List.Pairwise (fun a b => kz a ≠ kz b) l)
    (h1 : ∀ p, 1 ≤ nrm (kz p)) (M : Nat) (hM : ∀ p, p ∈ l → nrm (kz p) ≤ M + 1) (Rc t0 : Nat) (hRM : Rc ≤ M)
    (ι W Za Ab : R) (hW : ∀ p, p ∈ l → Le K O.toCone (wt K kz p) W) (a b : I → V3 R)
    (hZa : Le K O.toCone (lsum K (fun q => K.mul (wt K kz q) (dot3 K (a q) (a q))) l) Za)
    (hAb : Le K O.toCone (lsum K (fun p => K.mul (K.mul (wt K kz p) (wt K kz p)) (dot3 K (b p) (b p))) l) Ab) :
    Le K O.toCone
      (lsum K (fun r => dot3 K (sub3 K (Nl K l (TriadSparse.delta K kz) ι (kv K kz) a a r) (Nl K l (TriadSparse.delta K kz) ι (kv K kz) b b r))
        (sub3 K (Nl K l (TriadSparse.delta K kz) ι (kv K kz) a a r) (Nl K l (TriadSparse.delta K kz) ι (kv K kz) b b r))) l)
      (K.mul (K.add (K.add
          (K.mul (K.add K.one K.one) (K.mul (K.add K.one K.one) (K.mul (K.mul ι ι)
            (K.mul (K.add (K.mul (castN K (cube Rc)) K.one) (K.mul (castN K 135) (iv K inv (Rc + 1)))) Za))))
          (K.mul (K.add K.one K.one) (K.mul (K.add K.one K.one) (K.mul (K.mul ι ι) (K.mul W
            (K.mul (K.add (K.mul (castN K (cube Rc)) (K.mul (inv (castN K (t0 + 1))) (inv (castN K (t0 + 1)))))
                (K.mul (castN K 135) (iv K inv (Rc + 1)))) Za))))))
          (K.mul (K.add K.one K.one) (K.mul (K.mul ι ι) (K.mul Ab (castN K 135)))))
        (lsum K (fun p => K.mul (wt K kz p) (dot3 K (dF K a b p) (dF K a b p))) l)) := by
  have fd := force_diff K O inv mul_inv h10 l kz hd h1 M hM Rc t0 hRM ι a b
  have two : O.nn (K.add K.one K.one) := O.nn_add _ _ (RatModel.nn_one' K O.toCone) (RatModel.nn_one' K O.toCone)
  have nii : O.nn (K.mul ι ι) := O.nn_sq ι
  have hiv : O.nn (iv K inv (Rc + 1)) := iv_nn K O inv mul_inv h10 Rc
  have nC1 : O.nn (K.add (K.mul (castN K (cube Rc)) K.one) (K.mul (castN K 135) (iv K inv (Rc + 1)))) :=
    O.nn_add _ _ (O.nn_mul _ _ (RatModel.nn_cast K O.toCone _) (RatModel.nn_one' K O.toCone)) (O.nn_mul _ _ (RatModel.nn_cast K O.toCone _) hiv)
  have nC2 : O.nn (K.add (K.mul (castN K (cube Rc)) (K.mul (inv (castN K (t0 + 1))) (inv (castN K (t0 + 1)))))
      (K.mul (castN K 135) (iv K inv (Rc + 1)))) :=
    O.nn_add _ _ (O.nn_mul _ _ (RatModel.nn_cast K O.toCone _) (O.nn_sq _)) (O.nn_mul _ _ (RatModel.nn_cast K O.toCone _) hiv)
  have hw : ∀ r, O.nn (wt K kz r) := fun r => dot3_nn K O.toCone _
  have nZd : O.nn (lsum K (fun p => K.mul (wt K kz p) (dot3 K (dF K a b p) (dF K a b p))) l) :=
    lsum_nn K O.toCone _ (fun p => O.nn_mul _ _ (hw p) (dot3_nn K O.toCone _)) l
  have nAd : O.nn (lsum K (fun p => K.mul (K.mul (wt K kz p) (wt K kz p)) (dot3 K (dF K a b p) (dF K a b p))) l) :=
    lsum_nn K O.toCone _ (fun p => O.nn_mul _ _ (O.nn_sq _) (dot3_nn K O.toCone _)) l
  have nZa : O.nn Za := nn_of_le K O _ _ (lsum_nn K O.toCone _ (fun p => O.nn_mul _ _ (hw p) (dot3_nn K O.toCone _)) l) hZa
  have n135 : O.nn (castN K 135) := RatModel.nn_cast K O.toCone 135
  have hAd := ad_le K O kz l W hW (dF K a b)
  have t1 := le_mul_nn K O.toCone (K.add K.one K.one) _ _ two (le_mul_nn K O.toCone (K.mul ι ι) _ _ nii
    (le_mul_nn K O.toCone _ _ _ nZd (le_mul_nn K O.toCone _ _ _ nC1 hZa)))
  have t2 := le_mul_nn K O.toCone (K.add K.one K.one) _ _ two (le_mul_nn K O.toCone (K.mul ι ι) _ _ nii
    (le_trans' K O.toCone _ _ _ (le_mul_nn K O.toCone _ _ _ nAd (le_mul_nn K O.toCone _ _ _ nC2 hZa))
      (le_mul_right' K O.toCone hAd (O.nn_mul _ _ nC2 nZa))))
  have t3 := le_mul_nn K O.toCone (K.add K.one K.one) _ _ two (le_mul_nn K O.toCone (K.mul ι ι) _ _ nii
    (le_mul_right' K O.toCone hAb (O.nn_mul _ _ n135 nZd)))
  refine le_trans' K O.toCone _ _ _ fd (le_trans' K O.toCone _ _ _
    (le_add' K O.toCone _ _ _ _ (le_mul_nn K O.toCone _ _ _ two (le_add' K O.toCone _ _ _ _ t1 t2)) t3) ⟨K.zero, O.nn_zero, ?_⟩)
  generalize lsum K (fun p => K.mul (wt K kz p) (dot3 K (dF K a b p) (dF K a b p))) l = Zd
  generalize castN K 135 = c135
  generalize K.add (K.mul (castN K (cube Rc)) K.one) (K.mul c135 (iv K inv (Rc + 1))) = C1
  generalize K.add (K.mul (castN K (cube Rc)) (K.mul (inv (castN K (t0 + 1))) (inv (castN K (t0 + 1))))) (K.mul c135 (iv K inv (Rc + 1))) = C2
  lib_ring

end Gen
end ForceLipZ

#print axioms ForceLipZ.lsum_le_mem
#print axioms ForceLipZ.ad_le
#print axioms ForceLipZ.force_lip

ns/ForceNorms.lean

lean_all/ns/ForceNorms.lean

/-
  ns/ForceNorms.lean — T37 cycle 1141 (goal G746): explicit truncation-uniform force norms, over an ordered field
  (integer modes, nonzero, distinct, |k|_∞ ≤ M + 1; Z ≤ A ≤ B ≤ B₄ ≤ B₅ the weighted energies Σ w^j|a|²):
    nrm_h1   Σ w |N(a,a)|² ≤ ι²·B·(cH·A),     cH = 2·3·135 + 2·135             (ForceH2, EnstH2)
    nrm_h2   Σ w²|N(a,a)|² ≤ ι²·B₄·(c₃·B),    c₃ = 8(9·135 + 135)              (ForceH3, EnstH3)
    nrm_h3   Σ w³|N(a,a)|² ≤ ι²·B₅·(c₄·B₄),   c₄ = 32(27·135 + 135)            (ForceH4, EnstH4)
  Under the small-data bounds νB_t ≤ Q₃, νB₄_t ≤ Q₄ and hν²B₅_t ≤ Q₄ these give the hypotheses of the defect chain
  (FDk.fd_scheme', LocalDefect.local_defect') uniformly in the step and the truncation.

  Lean 4 core plus ns/EnstH4 (and through it EnstH2, EnstH3, ForceH2–H4).
-/
import ns.EnstH4

namespace ForceNorms
open DetChain NSChain DyadicSmallGlobal LibRing DyadicLarge FourierGalerkin GalerkinForce ForceBound ForceSob
  ModeCount CubeCount ForceUniform NormCompare EnstGalerkin ForceTwoScale ForceSobW CastZAdd WeightH2 LatticeSum
  LatticeBound LatticeStep ForceH2 ForceH3 Tri4 ForceH4 EnstH2 EnstH3 EnstH4

section Gen
variable {R : Type} (K : CRing R) (O : OrdR K) {I : Type}
variable (inv : R → R) (mul_inv : ∀ y, y ≠ K.zero → K.mul y (inv y) = K.one) (h10 : K.one ≠ K.zero)

include mul_inv h10 in
theorem nrm_h1 (l : List I) (kz : I → Mode) (hd : List.Pairwise (fun a b => kz a ≠ kz b) l)
    (h1 : ∀ p, 1 ≤ nrm (kz p)) (M : Nat) (hM : ∀ p, p ∈ l → nrm (kz p) ≤ M + 1) (ι : R) (a : I → V3 R) :
    Le K O.toCone (lsum K (fun r => K.mul (wt K kz r) (dot3 K (Nl K l (TriadSparse.delta K kz) ι (kv K kz) a a r)
        (Nl K l (TriadSparse.delta K kz) ι (kv K kz) a a r))) l)
      (K.mul (K.mul ι ι) (K.mul (lsum K (fun p => K.mul (K.mul (K.mul (wt K kz p) (wt K kz p)) (wt K kz p)) (dot3 K (a p) (a p))) l)
        (K.mul (cH K) (lsum K (fun p => K.mul (K.mul (wt K kz p) (wt K kz p)) (dot3 K (a p) (a p))) l)))) := by
  have hw : ∀ r, O.nn (wt K kz r) := fun r => dot3_nn K O.toCone _
  have two : O.nn (K.add K.one K.one) := O.nn_add _ _ (RatModel.nn_one' K O.toCone) (RatModel.nn_one' K O.toCone)
  have fh := force_h2 K O inv mul_inv h10 l kz hd h1 ι a
  have hS0 := S0_le K O inv mul_inv h10 l kz hd h1 M hM
  have hS1 := S1_le K O inv mul_inv h10 l kz hd h1 M hM
  have hZA := Z_le_A K O kz h1 l a
  have nA : O.nn (lsum K (fun p => K.mul (K.mul (wt K kz p) (wt K kz p)) (dot3 K (a p) (a p))) l) :=
    lsum_nn K O.toCone _ (fun p => O.nn_mul _ _ (O.nn_sq _) (dot3_nn K O.toCone _)) l
  have nB : O.nn (lsum K (fun p => K.mul (K.mul (K.mul (wt K kz p) (wt K kz p)) (wt K kz p)) (dot3 K (a p) (a p))) l) :=
    lsum_nn K O.toCone _ (fun p => O.nn_mul _ _ (O.nn_mul _ _ (O.nn_sq _) (hw p)) (dot3_nn K O.toCone _)) l
  have nS1 : O.nn (lsum K (fun p => K.mul (K.mul (cube3 K (ivn K inv kz p)) (cube3 K (ivn K inv kz p))) (wt K kz p)) l) :=
    lsum_nn K O.toCone _ (fun p => O.nn_mul _ _ (O.nn_sq _) (hw p)) l
  have i1 := TruncRate.mm K O _ _ _ _ nS1 nA hS1 hZA
  have i2 := ForceSob.le_mul_right' K O.toCone hS0 nA
  have inner := le_add' K O.toCone _ _ _ _ (le_mul_nn K O.toCone _ _ _ two i1) (le_mul_nn K O.toCone _ _ _ two i2)
  refine le_trans' K O.toCone _ _ _ fh (le_trans' K O.toCone _ _ _ (le_mul_nn K O.toCone _ _ _ (O.nn_sq ι) (le_mul_nn K O.toCone _ _ _ nB inner)) ⟨K.zero, O.nn_zero, ?_⟩)
  unfold cH
  generalize lsum K (fun p => K.mul (K.mul (wt K kz p) (wt K kz p)) (dot3 K (a p) (a p))) l = A
  generalize lsum K (fun p => K.mul (K.mul (K.mul (wt K kz p) (wt K kz p)) (wt K kz p)) (dot3 K (a p) (a p))) l = B
  generalize castN K 135 = c
  lib_ring

include mul_inv h10 in
theorem nrm_h2 (l : List I) (kz : I → Mode) (hd : List.Pairwise (fun a b => kz a ≠ kz b) l)
    (h1 : ∀ p, 1 ≤ nrm (kz p)) (M : Nat) (hM : ∀ p, p ∈ l → nrm (kz p) ≤ M + 1) (ι : R) (a : I → V3 R) :
    Le K O.toCone (lsum K (fun r => K.mul (K.mul (wt K kz r) (wt K kz r)) (dot3 K (Nl K l (TriadSparse.delta K kz) ι (kv K kz) a a r)
        (Nl K l (TriadSparse.delta K kz) ι (kv K kz) a a r))) l)
      (K.mul (K.mul ι ι) (K.mul (lsum K (fun p => K.mul (K.mul (K.mul (wt K kz p) (wt K kz p)) (K.mul (wt K kz p) (wt K kz p))) (dot3 K (a p) (a p))) l)
        (K.mul (c3 K) (lsum K (fun p => K.mul (K.mul (wt K kz p) (K.mul (wt K kz p) (wt K kz p))) (dot3 K (a p) (a p))) l)))) := by
  have hw : ∀ r, O.nn (wt K kz r) := fun r => dot3_nn K O.toCone _
  have fh := force_h3 K O inv mul_inv h10 l kz hd h1 ι a
  have hS0 := S0h_le K O inv mul_inv h10 l kz hd h1 M hM
  have hS1 := S1h_le K O inv mul_inv h10 l kz hd h1 M hM
  have hZB := Z_le_B K O kz h1 l a
  rw [Bform K kz l a] at hZB fh
  have nB : O.nn (lsum K (fun p => K.mul (K.mul (wt K kz p) (K.mul (wt K kz p) (wt K kz p))) (dot3 K (a p) (a p))) l) :=
    lsum_nn K O.toCone _ (fun p => O.nn_mul _ _ (O.nn_mul _ _ (hw p) (O.nn_sq _)) (dot3_nn K O.toCone _)) l
  have nB4 : O.nn (lsum K (fun p => K.mul (K.mul (K.mul (wt K kz p) (wt K kz p)) (K.mul (wt K kz p) (wt K kz p))) (dot3 K (a p) (a p))) l) :=
    lsum_nn K O.toCone _ (fun p => O.nn_mul _ _ (O.nn_mul _ _ (O.nn_sq _) (O.nn_sq _)) (dot3_nn K O.toCone _)) l
  have nS1 : O.nn (lsum K (fun p => K.mul (K.mul (p4 K (ivn K inv kz p)) (p4 K (ivn K inv kz p))) (K.mul (wt K kz p) (wt K kz p))) l) :=
    lsum_nn K O.toCone _ (fun p => O.nn_mul _ _ (O.nn_sq _) (O.nn_sq _)) l
  have i1 := TruncRate.mm K O _ _ _ _ nS1 nB hS1 hZB
  have i2 := ForceSob.le_mul_right' K O.toCone hS0 nB
  have inner := le_mul_nn K O.toCone _ _ _ (nn8 K O) (le_add' K O.toCone _ _ _ _ i1 i2)
  refine le_trans' K O.toCone _ _ _ fh (le_trans' K O.toCone _ _ _ (le_mul_nn K O.toCone _ _ _ (O.nn_sq ι) (le_mul_nn K O.toCone _ _ _ nB4 inner)) ⟨K.zero, O.nn_zero, ?_⟩)
  unfold c3
  generalize lsum K (fun p => K.mul (K.mul (wt K kz p) (K.mul (wt K kz p) (wt K kz p))) (dot3 K (a p) (a p))) l = B
  generalize lsum K (fun p => K.mul (K.mul (K.mul (wt K kz p) (wt K kz p)) (K.mul (wt K kz p) (wt K kz p))) (dot3 K (a p) (a p))) l = B4
  generalize castN K 135 = c
  generalize k8 K = e8
  generalize k9 K = e9
  lib_ring

include mul_inv h10 in
theorem nrm_h3 (l : List I) (kz : I → Mode) (hd : List.Pairwise (fun a b => kz a ≠ kz b) l)
    (h1 : ∀ p, 1 ≤ nrm (kz p)) (M : Nat) (hM : ∀ p, p ∈ l → nrm (kz p) ≤ M + 1) (ι : R) (a : I → V3 R) :
    Le K O.toCone (lsum K (fun r => K.mul (w3 K kz r) (dot3 K (Nl K l (TriadSparse.delta K kz) ι (kv K kz) a a r)
        (Nl K l (TriadSparse.delta K kz) ι (kv K kz) a a r))) l)
      (K.mul (K.mul ι ι) (K.mul (lsum K (fun p => K.mul (K.mul (K.mul (wt K kz p) (wt K kz p)) (w3 K kz p)) (dot3 K (a p) (a p))) l)
        (K.mul (c4 K) (lsum K (fun p => K.mul (K.mul (wt K kz p) (w3 K kz p)) (dot3 K (a p) (a p))) l)))) := by
  have hw : ∀ r, O.nn (wt K kz r) := fun r => dot3_nn K O.toCone _
  have hw3 : ∀ r, O.nn (w3 K kz r) := fun r => O.nn_mul _ _ (O.nn_sq _) (hw r)
  have fh := force_h4 K O inv mul_inv h10 l kz hd h1 ι a a
  rw [B5form K kz l a, B4form K kz l a] at fh
  have hS0 := S0f_le K O inv mul_inv h10 l kz hd h1 M hM
  have hS1 := S1f_le K O inv mul_inv h10 l kz hd h1 M hM
  have hZB := Z_le_B4 K O kz h1 l a
  have nB4 : O.nn (lsum K (fun p => K.mul (K.mul (wt K kz p) (w3 K kz p)) (dot3 K (a p) (a p))) l) :=
    lsum_nn K O.toCone _ (fun p => O.nn_mul _ _ (O.nn_mul _ _ (hw p) (hw3 p)) (dot3_nn K O.toCone _)) l
  have nB5 : O.nn (lsum K (fun p => K.mul (K.mul (K.mul (wt K kz p) (wt K kz p)) (w3 K kz p)) (dot3 K (a p) (a p))) l) :=
    lsum_nn K O.toCone _ (fun p => O.nn_mul _ _ (O.nn_mul _ _ (O.nn_sq _) (hw3 p)) (dot3_nn K O.toCone _)) l
  have nS1 : O.nn (lsum K (fun p => K.mul (K.mul (q5 K (ivn K inv kz p)) (q5 K (ivn K inv kz p))) (w3 K kz p)) l) :=
    lsum_nn K O.toCone _ (fun p => O.nn_mul _ _ (O.nn_sq _) (hw3 p)) l
  have i1 := TruncRate.mm K O _ _ _ _ nS1 nB4 hS1 hZB
  have i2 := ForceSob.le_mul_right' K O.toCone hS0 nB4
  have inner := le_mul_nn K O.toCone _ _ _ (nn32 K O) (le_add' K O.toCone _ _ _ _ i1 i2)
  refine le_trans' K O.toCone _ _ _ fh (le_trans' K O.toCone _ _ _ (le_mul_nn K O.toCone _ _ _ (O.nn_sq ι) (le_mul_nn K O.toCone _ _ _ nB5 inner)) ⟨K.zero, O.nn_zero, ?_⟩)
  unfold c4
  generalize lsum K (fun p => K.mul (K.mul (wt K kz p) (w3 K kz p)) (dot3 K (a p) (a p))) l = B4
  generalize lsum K (fun p => K.mul (K.mul (K.mul (wt K kz p) (wt K kz p)) (w3 K kz p)) (dot3 K (a p) (a p))) l = B5
  generalize castN K 135 = c
  generalize k32 K = e32
  generalize k27 K = e27
  lib_ring

end Gen

end ForceNorms

#print axioms ForceNorms.nrm_h1
#print axioms ForceNorms.nrm_h2
#print axioms ForceNorms.nrm_h3

ns/ForceSob.lean

lean_all/ns/ForceSob.lean

/-
  ns/ForceSob.lean — T37 cycle 939 (goal G624b): a truncation-uniform form of the Galerkin force bound, over any
  commutative ring with a nonnegative cone (ℚ included). Weights μ_p ν_p = 1, a triad indicator δ ∈ {0, 1} with the
  triad sparsity of Fourier modes (for fixed p and r at most one q, for fixed p and q at most one r):
    spA : Σ_{q∈l} δ(p,q,r) ≤ 1,     spB : Σ_{r∈l} δ(p,q,r) ≤ 1.
  Then, with H_μ(u) = Σ_p μ_p²|u_p|², Z(v) = Σ_q |k_q|²|v_q|²,
    mode_sob    |N_r(u,v)|² ≤ ι² · Σ_pΣ_q δ μ_p²|u_p|² · Σ_pΣ_q δ ν_p²|k_q|²|v_q|²
    total_sob   Σ_r |N_r(u,v)|² ≤ ι² · H_μ(u) · (Σ_p ν_p²) · Z(v)
  — the discrete ‖(u·∇)v‖² ≤ ‖u‖²_{L∞}‖∇v‖² with ‖u‖²_{L∞} ≤ H_μ(u)·Σ_p ν_p²; with μ_p² = |k_p|^{2s} the factor
  Σ_p ν_p² = Σ_p |k_p|^{−2s} is bounded uniformly in the truncation for s > 3/2.

  Lean 4 core plus ns/VecCS (vec_double_cs), ns/ForceBound (lsum_le, lsum_nn, dot3_nn), ns/GalerkinForce (Nl, coef).
-/
import ns.VecCS

namespace ForceSob
open DetChain NSChain LibRing DyadicLarge FourierGalerkin GalerkinForce ForceBound VecCS LerayProj

section Gen
variable {R : Type} (K : CRing R) (P : Cone K) {I : Type}

theorem le_mul_right' {x y z : R} (h : Le K P x y) (hz : P.nn z) : Le K P (K.mul x z) (K.mul y z) := by
  cases h with
  | intro d hd => exact ⟨K.mul d z, P.nn_mul _ _ hd.1 hz, by rw [← hd.2]; lib_ring⟩

def sm (c : R) (v : V3 R) : V3 R := ⟨K.mul c v.x, K.mul c v.y, K.mul c v.z⟩

theorem dot_sm (c d : R) (a b : V3 R) : dot3 K (sm K c a) (sm K d b) = K.mul (K.mul c d) (dot3 K a b) := by
  show K.add (K.add (K.mul (K.mul c a.x) (K.mul d b.x)) (K.mul (K.mul c a.y) (K.mul d b.y))) (K.mul (K.mul c a.z) (K.mul d b.z))
    = K.mul (K.mul c d) (K.add (K.add (K.mul a.x b.x) (K.mul a.y b.y)) (K.mul a.z b.z))
  lib_ring

/-- One component of the force as a double sum of dot products. -/
theorem comp_eq (l : List I) (δ : I → I → I → R) (ι : R) (k u : I → V3 R) (μ ν : I → R) (vc : I → R) (r : I)
    (hδ : ∀ p q, K.mul (δ p q r) (δ p q r) = δ p q r) (hμν : ∀ p, K.mul (μ p) (ν p) = K.one) :
    lsum K (fun p => lsum K (fun q => K.mul (coef K δ ι k u p q r) (vc q)) l) l
      = lsum K (fun p => lsum K (fun q => dot3 K (sm K (K.mul (K.mul (δ p q r) ι) (μ p)) (u p))
          (sm K (K.mul (K.mul (δ p q r) (ν p)) (vc q)) (k q))) l) l := by
  apply lsum_ext; intro p; apply lsum_ext; intro q
  rw [dot_sm]
  unfold coef
  have e : K.mul (K.mul (K.mul (δ p q r) ι) (μ p)) (K.mul (K.mul (δ p q r) (ν p)) (vc q))
      = K.mul (K.mul (K.mul (δ p q r) (δ p q r)) ι) (K.mul (K.mul (μ p) (ν p)) (vc q)) := by lib_ring
  rw [e, hδ, hμν]
  generalize dot3 K (u p) (k q) = w
  lib_ring

theorem mode_sob (l : List I) (δ : I → I → I → R) (ι : R) (k u v : I → V3 R) (μ ν : I → R) (r : I)
    (hδ : ∀ p q, K.mul (δ p q r) (δ p q r) = δ p q r) (hμν : ∀ p, K.mul (μ p) (ν p) = K.one) :
    Le K P (dot3 K (Nl K l δ ι k u v r) (Nl K l δ ι k u v r))
      (K.mul (lsum K (fun p => lsum K (fun q => K.mul (K.mul (K.mul (K.mul (δ p q r) ι) (μ p)) (K.mul (K.mul (δ p q r) ι) (μ p)))
          (dot3 K (u p) (u p))) l) l)
        (lsum K (fun p => lsum K (fun q => K.mul (K.mul (K.mul (δ p q r) (ν p)) (K.mul (δ p q r) (ν p)))
          (K.mul (dot3 K (k q) (k q)) (dot3 K (v q) (v q)))) l) l)) := by
  have cs : ∀ vc : I → R, Le K P
      (K.mul (lsum K (fun p => lsum K (fun q => K.mul (coef K δ ι k u p q r) (vc q)) l) l)
        (lsum K (fun p => lsum K (fun q => K.mul (coef K δ ι k u p q r) (vc q)) l) l))
      (K.mul (lsum K (fun p => lsum K (fun q => K.mul (K.mul (K.mul (K.mul (δ p q r) ι) (μ p)) (K.mul (K.mul (δ p q r) ι) (μ p)))
          (dot3 K (u p) (u p))) l) l)
        (lsum K (fun p => lsum K (fun q => K.mul (K.mul (K.mul (K.mul (δ p q r) (ν p)) (vc q)) (K.mul (K.mul (δ p q r) (ν p)) (vc q)))
          (dot3 K (k q) (k q))) l) l)) := by
    intro vc
    rw [comp_eq K l δ ι k u μ ν vc r hδ hμν]
    have c := vec_double_cs K P (fun p q => sm K (K.mul (K.mul (δ p q r) ι) (μ p)) (u p))
      (fun p q => sm K (K.mul (K.mul (δ p q r) (ν p)) (vc q)) (k q)) l
    rw [lsum_ext K _ _ (fun p => lsum_ext K _ _ (fun q => dot_sm K _ _ (u p) (u p)) l) l,
      lsum_ext K _ _ (fun p => lsum_ext K _ _ (fun q => dot_sm K _ _ (k q) (k q)) l) l] at c
    exact c
  have s := le_add' K P _ _ _ _ (le_add' K P _ _ _ _ (cs (fun q => (v q).x)) (cs (fun q => (v q).y))) (cs (fun q => (v q).z))
  refine le_trans' K P _ _ _ s ⟨K.zero, P.nn_zero, ?_⟩
  rw [add_zero', ← K.distrib, ← K.distrib, ← lsum_add, ← lsum_add]
  congr 1
  apply lsum_ext; intro p
  rw [← lsum_add, ← lsum_add]
  apply lsum_ext; intro q
  show _ = K.mul (K.mul (K.mul (δ p q r) (ν p)) (K.mul (δ p q r) (ν p)))
    (K.mul (dot3 K (k q) (k q)) (K.add (K.add (K.mul (v q).x (v q).x) (K.mul (v q).y (v q).y)) (K.mul (v q).z (v q).z)))
  generalize dot3 K (k q) (k q) = kk
  generalize K.mul (δ p q r) (ν p) = dn
  lib_ring

theorem total_sob (l : List I) (δ : I → I → I → R) (ι : R) (k u v : I → V3 R) (μ ν : I → R)
    (h01 : ∀ p q r, δ p q r = K.zero ∨ δ p q r = K.one) (hμν : ∀ p, K.mul (μ p) (ν p) = K.one)
    (spA : ∀ p r, Le K P (lsum K (fun q => δ p q r) l) K.one) (spB : ∀ p q, Le K P (lsum K (fun r => δ p q r) l) K.one) :
    Le K P (lsum K (fun r => dot3 K (Nl K l δ ι k u v r) (Nl K l δ ι k u v r)) l)
      (K.mul (K.mul ι ι) (K.mul (lsum K (fun p => K.mul (K.mul (μ p) (μ p)) (dot3 K (u p) (u p))) l)
        (K.mul (lsum K (fun p => K.mul (ν p) (ν p)) l) (lsum K (fun q => K.mul (dot3 K (k q) (k q)) (dot3 K (v q) (v q))) l)))) := by
  have hδ : ∀ p q r, K.mul (δ p q r) (δ p q r) = δ p q r := by
    intro p q r
    cases h01 p q r with
    | inl h => rw [h]; exact zero_mul' K _
    | inr h => rw [h]; exact K.one_mul _
  have hδn : ∀ p q r, P.nn (δ p q r) := by
    intro p q r; rw [← hδ]; exact P.nn_sq _
  generalize hH : lsum K (fun p => K.mul (K.mul (μ p) (μ p)) (dot3 K (u p) (u p))) l = H
  -- A_r ≤ ι² H
  have hA : ∀ r, Le K P (lsum K (fun p => lsum K (fun q => K.mul (K.mul (K.mul (K.mul (δ p q r) ι) (μ p)) (K.mul (K.mul (δ p q r) ι) (μ p)))
      (dot3 K (u p) (u p))) l) l) (K.mul (K.mul ι ι) H) := by
    intro r
    have e : lsum K (fun p => lsum K (fun q => K.mul (K.mul (K.mul (K.mul (δ p q r) ι) (μ p)) (K.mul (K.mul (δ p q r) ι) (μ p)))
        (dot3 K (u p) (u p))) l) l
        = K.mul (K.mul ι ι) (lsum K (fun p => K.mul (K.mul (K.mul (μ p) (μ p)) (dot3 K (u p) (u p))) (lsum K (fun q => δ p q r) l)) l) := by
      rw [← lsum_mul_left]
      apply lsum_ext; intro p
      rw [← lsum_mul_left, ← lsum_mul_left]
      apply lsum_ext; intro q
      have t : K.mul (K.mul (K.mul (K.mul (δ p q r) ι) (μ p)) (K.mul (K.mul (δ p q r) ι) (μ p))) (dot3 K (u p) (u p))
          = K.mul (K.mul (δ p q r) (δ p q r)) (K.mul (K.mul ι ι) (K.mul (K.mul (μ p) (μ p)) (dot3 K (u p) (u p)))) := by lib_ring
      rw [t, hδ]; lib_ring
    rw [e, ← hH]
    apply le_mul_nn K P _ _ _ (P.nn_sq ι)
    refine le_trans' K P _ _ _ (lsum_le K P _ (fun p => K.mul (K.mul (K.mul (μ p) (μ p)) (dot3 K (u p) (u p))) K.one)
      (fun p => le_mul_nn K P _ _ _ (P.nn_mul _ _ (P.nn_sq _) (dot3_nn K P _)) (spA p r)) l) ⟨K.zero, P.nn_zero, ?_⟩
    rw [add_zero']; apply lsum_ext; intro p; exact mul_one' K _
  -- B_r ≥ 0 and Σ_r B_r ≤ (Σν²) Z
  have hBn : ∀ r, P.nn (lsum K (fun p => lsum K (fun q => K.mul (K.mul (K.mul (δ p q r) (ν p)) (K.mul (δ p q r) (ν p)))
      (K.mul (dot3 K (k q) (k q)) (dot3 K (v q) (v q)))) l) l) := fun r =>
    lsum_nn K P _ (fun p => lsum_nn K P _ (fun q => P.nn_mul _ _ (P.nn_sq _) (P.nn_mul _ _ (dot3_nn K P _) (dot3_nn K P _))) l) l
  have s1 := lsum_le K P _ _ (fun r => le_trans' K P _ _ _ (mode_sob K P l δ ι k u v μ ν r (fun p q => hδ p q r) hμν)
    (le_mul_right' K P (hA r) (hBn r))) l
  refine le_trans' K P _ _ _ s1 ?_
  rw [lsum_mul_left]
  have Hn : P.nn H := by rw [← hH]; exact lsum_nn K P _ (fun p => P.nn_mul _ _ (P.nn_sq _) (dot3_nn K P _)) l
  have key : Le K P (lsum K (fun r => lsum K (fun p => lsum K (fun q => K.mul (K.mul (K.mul (δ p q r) (ν p)) (K.mul (δ p q r) (ν p)))
      (K.mul (dot3 K (k q) (k q)) (dot3 K (v q) (v q)))) l) l) l)
      (K.mul (lsum K (fun p => K.mul (ν p) (ν p)) l) (lsum K (fun q => K.mul (dot3 K (k q) (k q)) (dot3 K (v q) (v q))) l)) := by
    -- Σ_r Σ_p Σ_q (δν)² W_q = Σ_p Σ_q ν_p² W_q Σ_r δ ≤ Σ_p Σ_q ν_p² W_q
    have sw : lsum K (fun r => lsum K (fun p => lsum K (fun q => K.mul (K.mul (K.mul (δ p q r) (ν p)) (K.mul (δ p q r) (ν p)))
        (K.mul (dot3 K (k q) (k q)) (dot3 K (v q) (v q)))) l) l) l
        = lsum K (fun p => lsum K (fun q => K.mul (K.mul (K.mul (ν p) (ν p)) (K.mul (dot3 K (k q) (k q)) (dot3 K (v q) (v q))))
            (lsum K (fun r => δ p q r) l)) l) l := by
      rw [lsum_swap K (fun r p => lsum K (fun q => K.mul (K.mul (K.mul (δ p q r) (ν p)) (K.mul (δ p q r) (ν p)))
        (K.mul (dot3 K (k q) (k q)) (dot3 K (v q) (v q)))) l) l l]
      apply lsum_ext; intro p
      rw [lsum_swap K (fun r q => K.mul (K.mul (K.mul (δ p q r) (ν p)) (K.mul (δ p q r) (ν p)))
        (K.mul (dot3 K (k q) (k q)) (dot3 K (v q) (v q)))) l l]
      apply lsum_ext; intro q
      rw [← lsum_mul_left]
      apply lsum_ext; intro r
      have t : K.mul (K.mul (K.mul (δ p q r) (ν p)) (K.mul (δ p q r) (ν p))) (K.mul (dot3 K (k q) (k q)) (dot3 K (v q) (v q)))
          = K.mul (K.mul (δ p q r) (δ p q r)) (K.mul (K.mul (ν p) (ν p)) (K.mul (dot3 K (k q) (k q)) (dot3 K (v q) (v q)))) := by lib_ring
      rw [t, hδ]; lib_ring
    rw [sw]
    refine le_trans' K P _ _ _ (lsum_le K P _ _ (fun p => lsum_le K P _
      (fun q => K.mul (K.mul (K.mul (ν p) (ν p)) (K.mul (dot3 K (k q) (k q)) (dot3 K (v q) (v q)))) K.one)
      (fun q => le_mul_nn K P _ _ _ (P.nn_mul _ _ (P.nn_sq _) (P.nn_mul _ _ (dot3_nn K P _) (dot3_nn K P _))) (spB p q)) l) l)
      ⟨K.zero, P.nn_zero, ?_⟩
    rw [add_zero']
    have e2 := double_factor K l K.one (fun p => K.mul (ν p) (ν p)) (fun q => K.mul (dot3 K (k q) (k q)) (dot3 K (v q) (v q)))
    rw [K.one_mul] at e2
    rw [← e2]
    apply lsum_ext; intro p; apply lsum_ext; intro q; lib_ring

  have m := le_mul_nn K P (K.mul (K.mul ι ι) H) _ _ (P.nn_mul _ _ (P.nn_sq ι) Hn) key
  refine le_trans' K P _ _ _ m ⟨K.zero, P.nn_zero, ?_⟩
  rw [add_zero']
  generalize lsum K (fun p => K.mul (ν p) (ν p)) l = Nu
  generalize lsum K (fun q => K.mul (dot3 K (k q) (k q)) (dot3 K (v q) (v q))) l = Z
  lib_ring
end Gen

end ForceSob

#print axioms ForceSob.dot_sm
#print axioms ForceSob.comp_eq
#print axioms ForceSob.mode_sob
#print axioms ForceSob.total_sob

ns/ForceSobW.lean

lean_all/ns/ForceSobW.lean

/-
  ns/ForceSobW.lean — T37 cycle 1061 (goal G656b, piece ii): the Galerkin force bound with an output weight W_r, over
  a commutative ring with a nonnegative cone. If the weight satisfies the triad triangle inequality
  δ(p,q,r)·W_r ≤ δ(p,q,r)·(2W_p + 2W_q) (for W = |k|² and k_r = −(k_p + k_q)), then
    total_sobW   Σ_r W_r|N_r(u,v)|² ≤ ι²·H_μ(u)·Σ_pΣ_q ν_p²(2W_p + 2W_q)|k_q|²|v_q|²,
  the derivative of the force distributed onto the inputs — the H²-level counterpart of ForceSob.total_sob.
    total_sobWc  the same with δ·W_r ≤ δ·c(W_p + W_q) for a constant c ≥ 0 (for W = |k|⁴, c = 8).

  Lean 4 core plus ns/ForceSob (mode_sob, le_mul_right'), ns/FourierGalerkin (lsum_swap).
-/
import ns.ForceSob

namespace ForceSobW
open DetChain NSChain LibRing DyadicLarge FourierGalerkin GalerkinForce ForceBound VecCS LerayProj ForceSob

section Gen
variable {R : Type} (K : CRing R) (P : Cone K) {I : Type}

theorem total_sobW (l : List I) (δ : I → I → I → R) (ι : R) (k u v : I → V3 R) (μ ν W : I → R) (hW : ∀ r, P.nn (W r))
    (htri : ∀ p q r, Le K P (K.mul (δ p q r) (W r)) (K.mul (δ p q r) (K.add (K.add (W p) (W p)) (K.add (W q) (W q)))))
    (h01 : ∀ p q r, δ p q r = K.zero ∨ δ p q r = K.one) (hμν : ∀ p, K.mul (μ p) (ν p) = K.one)
    (spA : ∀ p r, Le K P (lsum K (fun q => δ p q r) l) K.one) (spB : ∀ p q, Le K P (lsum K (fun r => δ p q r) l) K.one) :
    Le K P (lsum K (fun r => K.mul (W r) (dot3 K (Nl K l δ ι k u v r) (Nl K l δ ι k u v r))) l)
      (K.mul (K.mul ι ι) (K.mul (lsum K (fun p => K.mul (K.mul (μ p) (μ p)) (dot3 K (u p) (u p))) l)
        (lsum K (fun p => lsum K (fun q => K.mul (K.mul (K.mul (ν p) (ν p)) (K.add (K.add (W p) (W p)) (K.add (W q) (W q))))
          (K.mul (dot3 K (k q) (k q)) (dot3 K (v q) (v q)))) l) l))) := by
  have hδ : ∀ p q r, K.mul (δ p q r) (δ p q r) = δ p q r := by
    intro p q r
    cases h01 p q r with
    | inl h => rw [h]; exact zero_mul' K _
    | inr h => rw [h]; exact K.one_mul _
  have hδn : ∀ p q r, P.nn (δ p q r) := by
    intro p q r; rw [← hδ]; exact P.nn_sq _
  generalize hH : lsum K (fun p => K.mul (K.mul (μ p) (μ p)) (dot3 K (u p) (u p))) l = H
  -- A_r ≤ ι² H
  have hA : ∀ r, Le K P (lsum K (fun p => lsum K (fun q => K.mul (K.mul (K.mul (K.mul (δ p q r) ι) (μ p)) (K.mul (K.mul (δ p q r) ι) (μ p)))
      (dot3 K (u p) (u p))) l) l) (K.mul (K.mul ι ι) H) := by
    intro r
    have e : lsum K (fun p => lsum K (fun q => K.mul (K.mul (K.mul (K.mul (δ p q r) ι) (μ p)) (K.mul (K.mul (δ p q r) ι) (μ p)))
        (dot3 K (u p) (u p))) l) l
        = K.mul (K.mul ι ι) (lsum K (fun p => K.mul (K.mul (K.mul (μ p) (μ p)) (dot3 K (u p) (u p))) (lsum K (fun q => δ p q r) l)) l) := by
      rw [← lsum_mul_left]
      apply lsum_ext; intro p
      rw [← lsum_mul_left, ← lsum_mul_left]
      apply lsum_ext; intro q
      have t : K.mul (K.mul (K.mul (K.mul (δ p q r) ι) (μ p)) (K.mul (K.mul (δ p q r) ι) (μ p))) (dot3 K (u p) (u p))
          = K.mul (K.mul (δ p q r) (δ p q r)) (K.mul (K.mul ι ι) (K.mul (K.mul (μ p) (μ p)) (dot3 K (u p) (u p)))) := by lib_ring
      rw [t, hδ]; lib_ring
    rw [e, ← hH]
    apply le_mul_nn K P _ _ _ (P.nn_sq ι)
    refine le_trans' K P _ _ _ (lsum_le K P _ (fun p => K.mul (K.mul (K.mul (μ p) (μ p)) (dot3 K (u p) (u p))) K.one)
      (fun p => le_mul_nn K P _ _ _ (P.nn_mul _ _ (P.nn_sq _) (dot3_nn K P _)) (spA p r)) l) ⟨K.zero, P.nn_zero, ?_⟩
    rw [add_zero']; apply lsum_ext; intro p; exact mul_one' K _
  have hBn : ∀ r, P.nn (lsum K (fun p => lsum K (fun q => K.mul (K.mul (K.mul (δ p q r) (ν p)) (K.mul (δ p q r) (ν p)))
      (K.mul (dot3 K (k q) (k q)) (dot3 K (v q) (v q)))) l) l) := fun r =>
    lsum_nn K P _ (fun p => lsum_nn K P _ (fun q => P.nn_mul _ _ (P.nn_sq _) (P.nn_mul _ _ (dot3_nn K P _) (dot3_nn K P _))) l) l
  have s1 := lsum_le K P _ _ (fun r => le_mul_nn K P (W r) _ _ (hW r)
    (le_trans' K P _ _ _ (mode_sob K P l δ ι k u v μ ν r (fun p q => hδ p q r) hμν) (le_mul_right' K P (hA r) (hBn r)))) l
  refine le_trans' K P _ _ _ s1 ?_
  have Hn : P.nn H := by rw [← hH]; exact lsum_nn K P _ (fun p => P.nn_mul _ _ (P.nn_sq _) (dot3_nn K P _)) l
  have e0 : lsum K (fun r => K.mul (W r) (K.mul (K.mul (K.mul ι ι) H)
      (lsum K (fun p => lsum K (fun q => K.mul (K.mul (K.mul (δ p q r) (ν p)) (K.mul (δ p q r) (ν p)))
        (K.mul (dot3 K (k q) (k q)) (dot3 K (v q) (v q)))) l) l))) l
      = K.mul (K.mul (K.mul ι ι) H) (lsum K (fun r => K.mul (W r)
      (lsum K (fun p => lsum K (fun q => K.mul (K.mul (K.mul (δ p q r) (ν p)) (K.mul (δ p q r) (ν p)))
        (K.mul (dot3 K (k q) (k q)) (dot3 K (v q) (v q)))) l) l)) l) := by
    rw [← lsum_mul_left]; exact lsum_ext K _ _ (fun r => by lib_ring) l
  rw [e0]
  -- Σ_r W_r B_r = Σ_p Σ_q ν_p² X_q Σ_r δ W_r ≤ Σ_p Σ_q ν_p² X_q (2W_p + 2W_q) Σ_r δ ≤ ...
  have sw : lsum K (fun r => K.mul (W r) (lsum K (fun p => lsum K (fun q => K.mul (K.mul (K.mul (δ p q r) (ν p)) (K.mul (δ p q r) (ν p)))
        (K.mul (dot3 K (k q) (k q)) (dot3 K (v q) (v q)))) l) l)) l
      = lsum K (fun p => lsum K (fun q => lsum K (fun r => K.mul (K.mul (K.mul (ν p) (ν p)) (K.mul (dot3 K (k q) (k q)) (dot3 K (v q) (v q))))
          (K.mul (δ p q r) (W r))) l) l) l := by
    have e1 : lsum K (fun r => K.mul (W r) (lsum K (fun p => lsum K (fun q => K.mul (K.mul (K.mul (δ p q r) (ν p)) (K.mul (δ p q r) (ν p)))
        (K.mul (dot3 K (k q) (k q)) (dot3 K (v q) (v q)))) l) l)) l
        = lsum K (fun r => lsum K (fun p => lsum K (fun q => K.mul (K.mul (K.mul (ν p) (ν p)) (K.mul (dot3 K (k q) (k q)) (dot3 K (v q) (v q))))
          (K.mul (δ p q r) (W r))) l) l) l := by
      apply lsum_ext; intro r
      rw [← lsum_mul_left]
      apply lsum_ext; intro p
      rw [← lsum_mul_left]
      apply lsum_ext; intro q
      have t : K.mul (W r) (K.mul (K.mul (K.mul (δ p q r) (ν p)) (K.mul (δ p q r) (ν p))) (K.mul (dot3 K (k q) (k q)) (dot3 K (v q) (v q))))
          = K.mul (K.mul (K.mul (ν p) (ν p)) (K.mul (dot3 K (k q) (k q)) (dot3 K (v q) (v q)))) (K.mul (K.mul (δ p q r) (δ p q r)) (W r)) := by lib_ring
      rw [t, hδ]
    rw [e1, lsum_swap K (fun r p => lsum K (fun q => K.mul (K.mul (K.mul (ν p) (ν p)) (K.mul (dot3 K (k q) (k q)) (dot3 K (v q) (v q))))
          (K.mul (δ p q r) (W r))) l) l l]
    apply lsum_ext; intro p
    exact lsum_swap K (fun r q => K.mul (K.mul (K.mul (ν p) (ν p)) (K.mul (dot3 K (k q) (k q)) (dot3 K (v q) (v q))))
          (K.mul (δ p q r) (W r))) l l
  rw [sw]
  suffices key : Le K P (lsum K (fun p => lsum K (fun q => lsum K (fun r => K.mul (K.mul (K.mul (ν p) (ν p))
      (K.mul (dot3 K (k q) (k q)) (dot3 K (v q) (v q)))) (K.mul (δ p q r) (W r))) l) l) l)
      (lsum K (fun p => lsum K (fun q => K.mul (K.mul (K.mul (ν p) (ν p)) (K.add (K.add (W p) (W p)) (K.add (W q) (W q))))
        (K.mul (dot3 K (k q) (k q)) (dot3 K (v q) (v q)))) l) l) by
    have m := le_mul_nn K P (K.mul (K.mul ι ι) H) _ _ (P.nn_mul _ _ (P.nn_sq ι) Hn) key
    exact le_trans' K P _ _ _ m ⟨K.zero, P.nn_zero, by rw [add_zero']; lib_ring⟩
  refine lsum_le K P _ _ (fun p => lsum_le K P _ _ (fun q => ?_) l) l
  -- Σ_r X (δ W_r) ≤ Σ_r X δ (2W_p + 2W_q) = X (2W_p+2W_q) Σ_r δ ≤ X (2W_p + 2W_q)
  have hX : P.nn (K.mul (K.mul (ν p) (ν p)) (K.mul (dot3 K (k q) (k q)) (dot3 K (v q) (v q)))) :=
    P.nn_mul _ _ (P.nn_sq _) (P.nn_mul _ _ (dot3_nn K P _) (dot3_nn K P _))
  have hWpq : P.nn (K.add (K.add (W p) (W p)) (K.add (W q) (W q))) :=
    P.nn_add _ _ (P.nn_add _ _ (hW p) (hW p)) (P.nn_add _ _ (hW q) (hW q))
  have a1 := lsum_le K P _ _ (fun r => le_mul_nn K P _ _ _ hX (htri p q r)) l
  have e2 : lsum K (fun r => K.mul (K.mul (K.mul (ν p) (ν p)) (K.mul (dot3 K (k q) (k q)) (dot3 K (v q) (v q))))
      (K.mul (δ p q r) (K.add (K.add (W p) (W p)) (K.add (W q) (W q))))) l
      = K.mul (K.mul (K.mul (K.mul (ν p) (ν p)) (K.mul (dot3 K (k q) (k q)) (dot3 K (v q) (v q))))
          (K.add (K.add (W p) (W p)) (K.add (W q) (W q)))) (lsum K (fun r => δ p q r) l) := by
    rw [← lsum_mul_left]; exact lsum_ext K _ _ (fun r => by lib_ring) l
  rw [e2] at a1
  have a2 := le_mul_nn K P _ _ _ (P.nn_mul _ _ hX hWpq) (spB p q)
  refine le_trans' K P _ _ _ a1 (le_trans' K P _ _ _ a2 ⟨K.zero, P.nn_zero, ?_⟩)
  rw [add_zero']; lib_ring

theorem total_sobWc (l : List I) (δ : I → I → I → R) (ι : R) (k u v : I → V3 R) (μ ν W : I → R) (hW : ∀ r, P.nn (W r))
    (c : R) (hc : P.nn c) (htri : ∀ p q r, Le K P (K.mul (δ p q r) (W r)) (K.mul (δ p q r) (K.mul c (K.add (W p) (W q)))))
    (h01 : ∀ p q r, δ p q r = K.zero ∨ δ p q r = K.one) (hμν : ∀ p, K.mul (μ p) (ν p) = K.one)
    (spA : ∀ p r, Le K P (lsum K (fun q => δ p q r) l) K.one) (spB : ∀ p q, Le K P (lsum K (fun r => δ p q r) l) K.one) :
    Le K P (lsum K (fun r => K.mul (W r) (dot3 K (Nl K l δ ι k u v r) (Nl K l δ ι k u v r))) l)
      (K.mul (K.mul ι ι) (K.mul (lsum K (fun p => K.mul (K.mul (μ p) (μ p)) (dot3 K (u p) (u p))) l)
        (lsum K (fun p => lsum K (fun q => K.mul (K.mul (K.mul (ν p) (ν p)) (K.mul c (K.add (W p) (W q))))
          (K.mul (dot3 K (k q) (k q)) (dot3 K (v q) (v q)))) l) l))) := by
  have hδ : ∀ p q r, K.mul (δ p q r) (δ p q r) = δ p q r := by
    intro p q r
    cases h01 p q r with
    | inl h => rw [h]; exact zero_mul' K _
    | inr h => rw [h]; exact K.one_mul _
  have hδn : ∀ p q r, P.nn (δ p q r) := by
    intro p q r; rw [← hδ]; exact P.nn_sq _
  generalize hH : lsum K (fun p => K.mul (K.mul (μ p) (μ p)) (dot3 K (u p) (u p))) l = H
  -- A_r ≤ ι² H
  have hA : ∀ r, Le K P (lsum K (fun p => lsum K (fun q => K.mul (K.mul (K.mul (K.mul (δ p q r) ι) (μ p)) (K.mul (K.mul (δ p q r) ι) (μ p)))
      (dot3 K (u p) (u p))) l) l) (K.mul (K.mul ι ι) H) := by
    intro r
    have e : lsum K (fun p => lsum K (fun q => K.mul (K.mul (K.mul (K.mul (δ p q r) ι) (μ p)) (K.mul (K.mul (δ p q r) ι) (μ p)))
        (dot3 K (u p) (u p))) l) l
        = K.mul (K.mul ι ι) (lsum K (fun p => K.mul (K.mul (K.mul (μ p) (μ p)) (dot3 K (u p) (u p))) (lsum K (fun q => δ p q r) l)) l) := by
      rw [← lsum_mul_left]
      apply lsum_ext; intro p
      rw [← lsum_mul_left, ← lsum_mul_left]
      apply lsum_ext; intro q
      have t : K.mul (K.mul (K.mul (K.mul (δ p q r) ι) (μ p)) (K.mul (K.mul (δ p q r) ι) (μ p))) (dot3 K (u p) (u p))
          = K.mul (K.mul (δ p q r) (δ p q r)) (K.mul (K.mul ι ι) (K.mul (K.mul (μ p) (μ p)) (dot3 K (u p) (u p)))) := by lib_ring
      rw [t, hδ]; lib_ring
    rw [e, ← hH]
    apply le_mul_nn K P _ _ _ (P.nn_sq ι)
    refine le_trans' K P _ _ _ (lsum_le K P _ (fun p => K.mul (K.mul (K.mul (μ p) (μ p)) (dot3 K (u p) (u p))) K.one)
      (fun p => le_mul_nn K P _ _ _ (P.nn_mul _ _ (P.nn_sq _) (dot3_nn K P _)) (spA p r)) l) ⟨K.zero, P.nn_zero, ?_⟩
    rw [add_zero']; apply lsum_ext; intro p; exact mul_one' K _
  have hBn : ∀ r, P.nn (lsum K (fun p => lsum K (fun q => K.mul (K.mul (K.mul (δ p q r) (ν p)) (K.mul (δ p q r) (ν p)))
      (K.mul (dot3 K (k q) (k q)) (dot3 K (v q) (v q)))) l) l) := fun r =>
    lsum_nn K P _ (fun p => lsum_nn K P _ (fun q => P.nn_mul _ _ (P.nn_sq _) (P.nn_mul _ _ (dot3_nn K P _) (dot3_nn K P _))) l) l
  have s1 := lsum_le K P _ _ (fun r => le_mul_nn K P (W r) _ _ (hW r)
    (le_trans' K P _ _ _ (mode_sob K P l δ ι k u v μ ν r (fun p q => hδ p q r) hμν) (le_mul_right' K P (hA r) (hBn r)))) l
  refine le_trans' K P _ _ _ s1 ?_
  have Hn : P.nn H := by rw [← hH]; exact lsum_nn K P _ (fun p => P.nn_mul _ _ (P.nn_sq _) (dot3_nn K P _)) l
  have e0 : lsum K (fun r => K.mul (W r) (K.mul (K.mul (K.mul ι ι) H)
      (lsum K (fun p => lsum K (fun q => K.mul (K.mul (K.mul (δ p q r) (ν p)) (K.mul (δ p q r) (ν p)))
        (K.mul (dot3 K (k q) (k q)) (dot3 K (v q) (v q)))) l) l))) l
      = K.mul (K.mul (K.mul ι ι) H) (lsum K (fun r => K.mul (W r)
      (lsum K (fun p => lsum K (fun q => K.mul (K.mul (K.mul (δ p q r) (ν p)) (K.mul (δ p q r) (ν p)))
        (K.mul (dot3 K (k q) (k q)) (dot3 K (v q) (v q)))) l) l)) l) := by
    rw [← lsum_mul_left]; exact lsum_ext K _ _ (fun r => by lib_ring) l
  rw [e0]
  -- Σ_r W_r B_r = Σ_p Σ_q ν_p² X_q Σ_r δ W_r ≤ Σ_p Σ_q ν_p² X_q (2W_p + 2W_q) Σ_r δ ≤ ...
  have sw : lsum K (fun r => K.mul (W r) (lsum K (fun p => lsum K (fun q => K.mul (K.mul (K.mul (δ p q r) (ν p)) (K.mul (δ p q r) (ν p)))
        (K.mul (dot3 K (k q) (k q)) (dot3 K (v q) (v q)))) l) l)) l
      = lsum K (fun p => lsum K (fun q => lsum K (fun r => K.mul (K.mul (K.mul (ν p) (ν p)) (K.mul (dot3 K (k q) (k q)) (dot3 K (v q) (v q))))
          (K.mul (δ p q r) (W r))) l) l) l := by
    have e1 : lsum K (fun r => K.mul (W r) (lsum K (fun p => lsum K (fun q => K.mul (K.mul (K.mul (δ p q r) (ν p)) (K.mul (δ p q r) (ν p)))
        (K.mul (dot3 K (k q) (k q)) (dot3 K (v q) (v q)))) l) l)) l
        = lsum K (fun r => lsum K (fun p => lsum K (fun q => K.mul (K.mul (K.mul (ν p) (ν p)) (K.mul (dot3 K (k q) (k q)) (dot3 K (v q) (v q))))
          (K.mul (δ p q r) (W r))) l) l) l := by
      apply lsum_ext; intro r
      rw [← lsum_mul_left]
      apply lsum_ext; intro p
      rw [← lsum_mul_left]
      apply lsum_ext; intro q
      have t : K.mul (W r) (K.mul (K.mul (K.mul (δ p q r) (ν p)) (K.mul (δ p q r) (ν p))) (K.mul (dot3 K (k q) (k q)) (dot3 K (v q) (v q))))
          = K.mul (K.mul (K.mul (ν p) (ν p)) (K.mul (dot3 K (k q) (k q)) (dot3 K (v q) (v q)))) (K.mul (K.mul (δ p q r) (δ p q r)) (W r)) := by lib_ring
      rw [t, hδ]
    rw [e1, lsum_swap K (fun r p => lsum K (fun q => K.mul (K.mul (K.mul (ν p) (ν p)) (K.mul (dot3 K (k q) (k q)) (dot3 K (v q) (v q))))
          (K.mul (δ p q r) (W r))) l) l l]
    apply lsum_ext; intro p
    exact lsum_swap K (fun r q => K.mul (K.mul (K.mul (ν p) (ν p)) (K.mul (dot3 K (k q) (k q)) (dot3 K (v q) (v q))))
          (K.mul (δ p q r) (W r))) l l
  rw [sw]
  suffices key : Le K P (lsum K (fun p => lsum K (fun q => lsum K (fun r => K.mul (K.mul (K.mul (ν p) (ν p))
      (K.mul (dot3 K (k q) (k q)) (dot3 K (v q) (v q)))) (K.mul (δ p q r) (W r))) l) l) l)
      (lsum K (fun p => lsum K (fun q => K.mul (K.mul (K.mul (ν p) (ν p)) (K.mul c (K.add (W p) (W q))))
        (K.mul (dot3 K (k q) (k q)) (dot3 K (v q) (v q)))) l) l) by
    have m := le_mul_nn K P (K.mul (K.mul ι ι) H) _ _ (P.nn_mul _ _ (P.nn_sq ι) Hn) key
    exact le_trans' K P _ _ _ m ⟨K.zero, P.nn_zero, by rw [add_zero']; lib_ring⟩
  refine lsum_le K P _ _ (fun p => lsum_le K P _ _ (fun q => ?_) l) l
  -- Σ_r X (δ W_r) ≤ Σ_r X δ (2W_p + 2W_q) = X (2W_p+2W_q) Σ_r δ ≤ X (2W_p + 2W_q)
  have hX : P.nn (K.mul (K.mul (ν p) (ν p)) (K.mul (dot3 K (k q) (k q)) (dot3 K (v q) (v q)))) :=
    P.nn_mul _ _ (P.nn_sq _) (P.nn_mul _ _ (dot3_nn K P _) (dot3_nn K P _))
  have hWpq : P.nn (K.mul c (K.add (W p) (W q))) :=
    P.nn_mul _ _ hc (P.nn_add _ _ (hW p) (hW q))
  have a1 := lsum_le K P _ _ (fun r => le_mul_nn K P _ _ _ hX (htri p q r)) l
  have e2 : lsum K (fun r => K.mul (K.mul (K.mul (ν p) (ν p)) (K.mul (dot3 K (k q) (k q)) (dot3 K (v q) (v q))))
      (K.mul (δ p q r) (K.mul c (K.add (W p) (W q))))) l
      = K.mul (K.mul (K.mul (K.mul (ν p) (ν p)) (K.mul (dot3 K (k q) (k q)) (dot3 K (v q) (v q))))
          (K.mul c (K.add (W p) (W q)))) (lsum K (fun r => δ p q r) l) := by
    rw [← lsum_mul_left]; exact lsum_ext K _ _ (fun r => by lib_ring) l
  rw [e2] at a1
  have a2 := le_mul_nn K P _ _ _ (P.nn_mul _ _ hX hWpq) (spB p q)
  refine le_trans' K P _ _ _ a1 (le_trans' K P _ _ _ a2 ⟨K.zero, P.nn_zero, ?_⟩)
  rw [add_zero']; lib_ring

end Gen

end ForceSobW

#print axioms ForceSobW.total_sobW
#print axioms ForceSobW.total_sobWc

ns/ForceSum.lean

lean_all/ns/ForceSum.lean

/-
  ns/ForceSum.lean — T37 cycle 905 (goal G592d): the weighted force bound Galerkin3D needs, for the actual projected
  Galerkin force on the modes p < N, over any commutative ring with a nonnegative cone (ℚ included). With
  E = Σ_{p<N} |a_p|², K_k = Σ_{q<N} |k_q|², weights w_r ≥ 0 (w_r = x_r + 2) and κ_r ≥ 0, κ_r (k_r·k_r) = 1:
    Σ_{r<N} w_r |P_r N_r(a, a)|² ≤ (Σ_r w_r) · ι² · K_k · N · E².
  So Galerkin3D.global3 applies to the projected 3D Fourier–Galerkin scheme with C = (Σ_r w_r) ι² K_k N.

  Lean 4 core plus ns/ForceBound (force_mode), ns/LerayProj3 (proj_le), NSChain (sumN, lsum), lib/Ring (castN).
    rl, lsum_rl       the mode list [N−1, …, 0] and lsum over it = sumN
    lsum_const        Σ_{p<N} c = N·c
    force_weighted    the statement
-/
import ns.ForceBound
import ns.LerayProj3

namespace ForceSum
open DetChain NSChain LibRing FourierGalerkin GalerkinForce ForceBound LerayProj LerayProj3 DyadicLarge

section Gen
variable {R : Type} (K : CRing R) (P : Cone K)

def rl : Nat → List Nat
  | 0 => []
  | N + 1 => N :: rl N

theorem lsum_rl (f : Nat → R) : ∀ N, lsum K f (rl N) = sumN K f N
  | 0 => rfl
  | N + 1 => by
    show K.add (f N) (lsum K f (rl N)) = K.add (sumN K f N) (f N)
    rw [lsum_rl f N, K.add_comm]

theorem lsum_const (c : R) : ∀ N, lsum K (fun _ => c) (rl N) = K.mul (castN K N) c
  | 0 => by show K.zero = K.mul K.zero c; lib_ring
  | N + 1 => by
    show K.add c (lsum K (fun _ => c) (rl N)) = K.mul (K.add K.one (castN K N)) c
    rw [lsum_const c N]; lib_ring

theorem force_weighted (δ : Nat → Nat → Nat → R) (ι : R) (k a : Nat → V3 R) (κ w : Nat → R) (N : Nat)
    (hδ : ∀ p q r, δ p q r = K.zero ∨ δ p q r = K.one) (hκ : ∀ r, P.nn (κ r))
    (hκk : ∀ r, K.mul (dot3 K (k r) (k r)) (κ r) = K.one) (hw : ∀ r, P.nn (w r)) :
    Le K P (sumN K (fun r => K.mul (w r) (dot3 K (proj K (κ r) (k r) (Nl K (rl N) δ ι k a a r))
        (proj K (κ r) (k r) (Nl K (rl N) δ ι k a a r)))) N)
      (K.mul (sumN K w N) (K.mul (K.mul (K.mul ι ι) (sumN K (fun q => dot3 K (k q) (k q)) N))
        (K.mul (castN K N) (K.mul (sumN K (fun p => dot3 K (a p) (a p)) N) (sumN K (fun p => dot3 K (a p) (a p)) N))))) := by
  generalize hE : sumN K (fun p => dot3 K (a p) (a p)) N = E
  generalize hKk : sumN K (fun q => dot3 K (k q) (k q)) N = Kk
  have pt : ∀ r, Le K P (K.mul (w r) (dot3 K (proj K (κ r) (k r) (Nl K (rl N) δ ι k a a r))
        (proj K (κ r) (k r) (Nl K (rl N) δ ι k a a r))))
      (K.mul (w r) (K.mul (K.mul (K.mul ι ι) Kk) (K.mul (castN K N) (K.mul E E)))) := by
    intro r
    apply le_mul_nn K P _ _ _ (hw r)
    have fm := force_mode K P (rl N) δ ι k a a r hδ
    rw [lsum_rl, lsum_rl, hE, hKk, lsum_const] at fm
    have e : K.mul (K.mul (K.mul ι ι) (K.mul E Kk)) (K.mul (castN K N) E)
        = K.mul (K.mul (K.mul ι ι) Kk) (K.mul (castN K N) (K.mul E E)) := by lib_ring
    rw [e] at fm
    exact le_trans' K P _ _ _ (proj_le K P (κ r) (k r) _ (hκ r) (hκk r)) fm
  have s := sum_le K P _ _ pt N
  have e : sumN K (fun r => K.mul (w r) (K.mul (K.mul (K.mul ι ι) Kk) (K.mul (castN K N) (K.mul E E)))) N
      = K.mul (sumN K w N) (K.mul (K.mul (K.mul ι ι) Kk) (K.mul (castN K N) (K.mul E E))) := by
    rw [K.mul_comm (sumN K w N), ← sumN_mul_left]
    exact sumN_ext K _ _ (fun r => K.mul_comm _ _) N
  rw [e] at s
  exact s

end Gen

end ForceSum

#print axioms ForceSum.lsum_rl
#print axioms ForceSum.lsum_const
#print axioms ForceSum.force_weighted

ns/ForceTime.lean

lean_all/ns/ForceTime.lean

/-
  ns/ForceTime.lean — T37 cycle 1800 (goal G856c): the time-summed force difference, over an ordered ring. If the
  force difference energy obeys the Lipschitz form of ForceDiff/EnstLipschitz, D_t ≤ αZ_t + βA_t (Z, A the Z- and
  A-energies of the coefficient difference d_t), and Q_t = νZ_t + hν²A_t ≤ q for t < n (TruncEps/TruncTri), then
    step_le     ν²·h·D_t ≤ (hαν + β)·q
    force_time  ν²·Σ_{t<n} h·D_t ≤ n·(hαν + β)·q
    mode_le     r ∈ L  ⇒  |ΔN_r|² ≤ Σ_L |ΔN|²   (so the same bound holds for each low mode)
  — the force term of TeleCS.tele_diff is controlled by the truncation Cauchy bound q.
  Lean 4 core plus ns/SchemeTele, ns/DyadicCoef (term_le), ns/DyadicLarge.
-/
import ns.SchemeTele
import ns.DyadicCoef

namespace ForceTime
open DetChain NSChain DyadicSmallGlobal DyadicLarge LibRing FourierGalerkin ForceBound SchemeTele

section Gen
variable {R : Type} (K : CRing R) (O : OrdR K)

theorem step_le (ν h α β q Z A D : R) (hν : O.nn ν) (hh : O.nn h) (hα : O.nn α) (hβ : O.nn β) (hZ : O.nn Z) (hA : O.nn A)
    (hD : Le K O.toCone D (K.add (K.mul α Z) (K.mul β A)))
    (hQ : Le K O.toCone (K.add (K.mul ν Z) (K.mul (K.mul h (K.mul ν ν)) A)) q) :
    Le K O.toCone (K.mul (K.mul ν ν) (K.mul h D)) (K.mul (K.add (K.mul (K.mul h α) ν) β) q) := by
  have nZ : O.nn (K.mul ν Z) := O.nn_mul _ _ hν hZ
  have nA : O.nn (K.mul (K.mul h (K.mul ν ν)) A) := O.nn_mul _ _ (O.nn_mul _ _ hh (O.nn_sq ν)) hA
  have q1 : Le K O.toCone (K.mul ν Z) q := le_trans' K O.toCone _ _ _ ⟨_, nA, rfl⟩ hQ
  have q2 : Le K O.toCone (K.mul (K.mul h (K.mul ν ν)) A) q :=
    le_trans' K O.toCone _ _ _ ⟨K.mul ν Z, nZ, (by lib_ring)⟩ hQ
  have s1 := le_mul_nn K O.toCone (K.mul (K.mul ν ν) h) _ _ (O.nn_mul _ _ (O.nn_sq ν) hh) hD
  have s2 := le_add' K O.toCone _ _ _ _ (le_mul_nn K O.toCone (K.mul (K.mul h α) ν) _ _
      (O.nn_mul _ _ (O.nn_mul _ _ hh hα) hν) q1) (le_mul_nn K O.toCone β _ _ hβ q2)
  refine le_trans' K O.toCone _ _ _ ⟨K.zero, O.nn_zero, ?_⟩ (le_trans' K O.toCone _ _ _ s1 (le_trans' K O.toCone _ _ _
    ⟨K.zero, O.nn_zero, ?_⟩ (le_trans' K O.toCone _ _ _ s2 ⟨K.zero, O.nn_zero, ?_⟩)))
  · lib_ring
  · lib_ring
  · lib_ring

theorem force_time (ν h α β q : R) (Z A D : Nat → R) (hν : O.nn ν) (hh : O.nn h) (hα : O.nn α) (hβ : O.nn β)
    (hZ : ∀ t, O.nn (Z t)) (hA : ∀ t, O.nn (A t))
    (hD : ∀ t, Le K O.toCone (D t) (K.add (K.mul α (Z t)) (K.mul β (A t))))
    (hQ : ∀ t, Le K O.toCone (K.add (K.mul ν (Z t)) (K.mul (K.mul h (K.mul ν ν)) (A t))) q) :
    ∀ n, Le K O.toCone (K.mul (K.mul ν ν) (tsum K (fun t => K.mul h (D t)) n))
      (K.mul (castN K n) (K.mul (K.add (K.mul (K.mul h α) ν) β) q))
  | 0 => ⟨K.zero, O.nn_zero, by show K.add (K.mul (K.mul ν ν) K.zero) K.zero = K.mul K.zero _; lib_ring⟩
  | n + 1 => by
    have ih := force_time ν h α β q Z A D hν hh hα hβ hZ hA hD hQ n
    have st := step_le K O ν h α β q (Z n) (A n) (D n) hν hh hα hβ (hZ n) (hA n) (hD n) (hQ n)
    refine le_trans' K O.toCone _ _ _ ⟨K.zero, O.nn_zero, ?_⟩
      (le_trans' K O.toCone _ _ _ (le_add' K O.toCone _ _ _ _ ih st) ⟨K.zero, O.nn_zero, ?_⟩)
    · show K.add (K.mul (K.mul ν ν) (K.add (tsum K (fun t => K.mul h (D t)) n) (K.mul h (D n)))) K.zero
        = K.add (K.mul (K.mul ν ν) (tsum K (fun t => K.mul h (D t)) n)) (K.mul (K.mul ν ν) (K.mul h (D n)))
      lib_ring
    · show K.add (K.add (K.mul (castN K n) (K.mul (K.add (K.mul (K.mul h α) ν) β) q))
          (K.mul (K.add (K.mul (K.mul h α) ν) β) q)) K.zero
        = K.mul (K.add K.one (castN K n)) (K.mul (K.add (K.mul (K.mul h α) ν) β) q)
      lib_ring

theorem mode_le {I : Type} (L : List I) (ΔN : I → V3 R) (r : I) (hr : r ∈ L) :
    Le K O.toCone (dot3 K (ΔN r) (ΔN r)) (lsum K (fun p => dot3 K (ΔN p) (ΔN p)) L) :=
  DyadicCoef.term_le K O (fun p => dot3 K (ΔN p) (ΔN p)) (fun p => dot3_nn K O.toCone (ΔN p)) L r hr

end Gen
end ForceTime

#print axioms ForceTime.step_le
#print axioms ForceTime.force_time
#print axioms ForceTime.mode_le

ns/ForceTwoScale.lean

lean_all/ns/ForceTwoScale.lean

/-
  ns/ForceTwoScale.lean — T37 cycle 993 (goal G648, piece b2): the two-scale Galerkin force bound, over an ordered field.
  Split u at the cutoff |kz|_∞ = R into u_low + u_high (Nl is linear in u, |X+Y|² ≤ 2|X|² + 2|Y|²) and apply
  ForceSob.total_sob twice with cast weights (no square roots):
    low part:  μ = |k|_∞ on low modes, |k|_∞² on high modes   ⇒  H ≤ Z(u),  Σν² ≤ (2R+1)³ + 135/(R+1)
    high part: μ = T on low modes,      |k|_∞² on high modes   ⇒  H ≤ A(u),  Σν² ≤ (2R+1)³/T² + 135/(R+1)
  Result (two_scale):
    Σ_r |N_r(u,v)|² ≤ 2ι²·[Z(u)·C₁·Z(v) + A(u)·C₂·Z(v)],
    C₁ = (2R+1)³ + 135/(R+1),  C₂ = (2R+1)³/T² + 135/(R+1),
  with Z(u) = Σ|k|²|u|², A(u) = Σ|k|⁴|u|², uniformly in the truncation. With T large and R ~ Z/ν² the A-term is
  absorbed by the dissipation (next piece).

  Lean 4 core plus ns/WeightSplit, ns/ForceBilin (Nl_add_left), ns/ForceLip (Nl_congr), ns/NormCompare, ns/EnstGalerkin.
-/
import ns.WeightSplit
import ns.ForceBilin
import ns.ForceLip
import ns.NormCompare
import ns.EnstGalerkin

namespace ForceTwoScale
open DetChain NSChain DyadicSmallGlobal LibRing DyadicLarge FourierGalerkin GalerkinForce ForceBound ForceSob
  AbelCount LatticeStep LatticeSum ModeCount CubeCount LatticeBound ForceUniform NormCompare WeightSplit
  EnstGalerkin ForceBilin GoodModesQ

section Gen
variable {R : Type} (K : CRing R) (O : OrdR K) {I : Type}
variable (inv : R → R) (mul_inv : ∀ y, y ≠ K.zero → K.mul y (inv y) = K.one) (h10 : K.one ≠ K.zero)

def z3 : V3 R := ⟨K.zero, K.zero, K.zero⟩

def uLo (Rc : Nat) (kz : I → Mode) (u : I → V3 R) (p : I) : V3 R := cond (lowB Rc kz p) (u p) (z3 K)
def uHi (Rc : Nat) (kz : I → Mode) (u : I → V3 R) (p : I) : V3 R := cond (lowB Rc kz p) (z3 K) (u p)

theorem split_u (Rc : Nat) (kz : I → Mode) (u : I → V3 R) (p : I) :
    u p = addF K (uLo K Rc kz u) (uHi K Rc kz u) p := by
  show u p = add3 K (cond (lowB Rc kz p) (u p) (z3 K)) (cond (lowB Rc kz p) (z3 K) (u p))
  cases lowB Rc kz p with
  | true =>
    apply ForceBilin.v3_ext
    · show (u p).x = K.add (u p).x K.zero; lib_ring
    · show (u p).y = K.add (u p).y K.zero; lib_ring
    · show (u p).z = K.add (u p).z K.zero; lib_ring
  | false =>
    apply ForceBilin.v3_ext
    · show (u p).x = K.add K.zero (u p).x; lib_ring
    · show (u p).y = K.add K.zero (u p).y; lib_ring
    · show (u p).z = K.add K.zero (u p).z; lib_ring

theorem sq2 (X Y : V3 R) :
    Le K O.toCone (dot3 K (add3 K X Y) (add3 K X Y))
      (K.add (K.mul (K.add K.one K.one) (dot3 K X X)) (K.mul (K.add K.one K.one) (dot3 K Y Y))) :=
  ⟨dot3 K (add3 K X (neg3 K Y)) (add3 K X (neg3 K Y)), dot3_nn K O.toCone _, by
    show K.add (K.add (K.add (K.mul (K.add X.x Y.x) (K.add X.x Y.x)) (K.mul (K.add X.y Y.y) (K.add X.y Y.y)))
        (K.mul (K.add X.z Y.z) (K.add X.z Y.z)))
      (K.add (K.add (K.mul (K.add X.x (K.neg Y.x)) (K.add X.x (K.neg Y.x))) (K.mul (K.add X.y (K.neg Y.y)) (K.add X.y (K.neg Y.y))))
        (K.mul (K.add X.z (K.neg Y.z)) (K.add X.z (K.neg Y.z))))
      = K.add (K.mul (K.add K.one K.one) (K.add (K.add (K.mul X.x X.x) (K.mul X.y X.y)) (K.mul X.z X.z)))
        (K.mul (K.add K.one K.one) (K.add (K.add (K.mul Y.x Y.x) (K.mul Y.y Y.y)) (K.mul Y.z Y.z)))
    lib_ring⟩

def nn (kz : I → Mode) (p : I) : R := castN K (nrm (kz p))
def ivn (kz : I → Mode) (p : I) : R := inv (castN K (nrm (kz p)))

def mu1 (Rc : Nat) (kz : I → Mode) (p : I) : R :=
  cond (lowB Rc kz p) (nn K kz p) (K.mul (nn K kz p) (nn K kz p))
def nu1 (Rc : Nat) (kz : I → Mode) (p : I) : R :=
  cond (lowB Rc kz p) (ivn K inv kz p) (K.mul (ivn K inv kz p) (ivn K inv kz p))
def mu2 (Rc t : Nat) (kz : I → Mode) (p : I) : R :=
  cond (lowB Rc kz p) (castN K (t + 1)) (K.mul (nn K kz p) (nn K kz p))
def nu2 (Rc t : Nat) (kz : I → Mode) (p : I) : R :=
  cond (lowB Rc kz p) (inv (castN K (t + 1))) (K.mul (ivn K inv kz p) (ivn K inv kz p))

include O mul_inv h10 in
theorem nn_ivn (kz : I → Mode) (h1 : ∀ p, 1 ≤ nrm (kz p)) (p : I) : K.mul (nn K kz p) (ivn K inv kz p) = K.one :=
  inv_cast K O inv mul_inv h10 _ (h1 p)

include O mul_inv h10 in
theorem munu1 (Rc : Nat) (kz : I → Mode) (h1 : ∀ p, 1 ≤ nrm (kz p)) (p : I) :
    K.mul (mu1 K Rc kz p) (nu1 K inv Rc kz p) = K.one := by
  have e := nn_ivn K O inv mul_inv h10 kz h1 p
  show K.mul (cond (lowB Rc kz p) _ _) (cond (lowB Rc kz p) _ _) = K.one
  cases lowB Rc kz p with
  | true => exact e
  | false =>
    show K.mul (K.mul (nn K kz p) (nn K kz p)) (K.mul (ivn K inv kz p) (ivn K inv kz p)) = K.one
    calc _ = K.mul (K.mul (nn K kz p) (ivn K inv kz p)) (K.mul (nn K kz p) (ivn K inv kz p)) := by lib_ring
      _ = K.one := by rw [e]; lib_ring

include O mul_inv h10 in
theorem munu2 (Rc t : Nat) (kz : I → Mode) (h1 : ∀ p, 1 ≤ nrm (kz p)) (p : I) :
    K.mul (mu2 K Rc t kz p) (nu2 K inv Rc t kz p) = K.one := by
  have e := nn_ivn K O inv mul_inv h10 kz h1 p
  show K.mul (cond (lowB Rc kz p) _ _) (cond (lowB Rc kz p) _ _) = K.one
  cases lowB Rc kz p with
  | true => exact mul_inv _ (cast_succ_ne K O h10 t)
  | false =>
    show K.mul (K.mul (nn K kz p) (nn K kz p)) (K.mul (ivn K inv kz p) (ivn K inv kz p)) = K.one
    calc _ = K.mul (K.mul (nn K kz p) (ivn K inv kz p)) (K.mul (nn K kz p) (ivn K inv kz p)) := by lib_ring
      _ = K.one := by rw [e]; lib_ring

include O mul_inv h10 in
theorem ivn_le (kz : I → Mode) (h1 : ∀ p, 1 ≤ nrm (kz p)) (p : I) :
    O.nn (ivn K inv kz p) ∧ Le K O.toCone (K.mul (ivn K inv kz p) (ivn K inv kz p)) K.one := by
  have e := nn_ivn K O inv mul_inv h10 kz h1 p
  have hn : O.nn (nn K kz p) := RatModel.nn_cast K O.toCone _
  have hi : O.nn (ivn K inv kz p) := BaselQ.inv_nn K O inv _ hn e
  refine ⟨hi, ?_⟩
  -- ivn ≤ 1: 1 = n·ivn = ivn + (n − 1)·ivn
  have hle : Le K O.toCone (ivn K inv kz p) K.one := by
    cases hm : nrm (kz p) with
    | zero =>
      have h1p := h1 p
      rw [hm] at h1p
      exact absurd h1p (Nat.not_succ_le_zero 0)
    | succ m =>
      have e' : K.mul (castN K (m + 1)) (ivn K inv kz p) = K.one := by
        have : nn K kz p = castN K (m + 1) := by show castN K (nrm (kz p)) = _; rw [hm]
        rw [← this]; exact e
      refine ⟨K.mul (castN K m) (ivn K inv kz p), O.nn_mul _ _ (RatModel.nn_cast K O.toCone m) hi, ?_⟩
      rw [← e']; show _ = K.mul (K.add K.one (castN K m)) _; lib_ring
  have h2 := le_mul_nn K O.toCone (ivn K inv kz p) _ _ hi hle
  have e1 : K.mul (ivn K inv kz p) K.one = ivn K inv kz p := by lib_ring
  rw [e1] at h2
  exact le_trans' K O.toCone _ _ _ h2 hle

theorem H1p (Rc : Nat) (kz : I → Mode) (u : I → V3 R) (p : I) :
    Le K O.toCone (K.mul (K.mul (mu1 K Rc kz p) (mu1 K Rc kz p)) (dot3 K (uLo K Rc kz u p) (uLo K Rc kz u p)))
      (K.mul (wt K kz p) (dot3 K (u p) (u p))) := by
  have hw : O.nn (K.mul (wt K kz p) (dot3 K (u p) (u p))) := O.nn_mul _ _ (dot3_nn K O.toCone _) (dot3_nn K O.toCone _)
  show Le K O.toCone (K.mul (K.mul (cond (lowB Rc kz p) _ _) (cond (lowB Rc kz p) _ _))
    (dot3 K (cond (lowB Rc kz p) (u p) (z3 K)) (cond (lowB Rc kz p) (u p) (z3 K)))) _
  cases lowB Rc kz p with
  | true => exact le_mul_right' K O.toCone (nrm_le_dot K O.toCone (kz p)) (dot3_nn K O.toCone _)
  | false =>
    refine ⟨K.mul (wt K kz p) (dot3 K (u p) (u p)), hw, ?_⟩
    show K.add (K.mul (K.mul (K.mul (nn K kz p) (nn K kz p)) (K.mul (nn K kz p) (nn K kz p)))
      (K.add (K.add (K.mul K.zero K.zero) (K.mul K.zero K.zero)) (K.mul K.zero K.zero))) _ = _
    lib_ring

theorem H2p (Rc t : Nat) (kz : I → Mode) (u : I → V3 R) (p : I) :
    Le K O.toCone (K.mul (K.mul (mu2 K Rc t kz p) (mu2 K Rc t kz p)) (dot3 K (uHi K Rc kz u p) (uHi K Rc kz u p)))
      (K.mul (K.mul (wt K kz p) (wt K kz p)) (dot3 K (u p) (u p))) := by
  have hw : O.nn (K.mul (K.mul (wt K kz p) (wt K kz p)) (dot3 K (u p) (u p))) :=
    O.nn_mul _ _ (O.nn_sq _) (dot3_nn K O.toCone _)
  show Le K O.toCone (K.mul (K.mul (cond (lowB Rc kz p) _ _) (cond (lowB Rc kz p) _ _))
    (dot3 K (cond (lowB Rc kz p) (z3 K) (u p)) (cond (lowB Rc kz p) (z3 K) (u p)))) _
  cases lowB Rc kz p with
  | true =>
    refine ⟨K.mul (K.mul (wt K kz p) (wt K kz p)) (dot3 K (u p) (u p)), hw, ?_⟩
    show K.add (K.mul (K.mul (castN K (t + 1)) (castN K (t + 1)))
      (K.add (K.add (K.mul K.zero K.zero) (K.mul K.zero K.zero)) (K.mul K.zero K.zero))) _ = _
    lib_ring
  | false => exact le_mul_right' K O.toCone (nrm4_le K O.toCone (kz p)) (dot3_nn K O.toCone _)

include O mul_inv h10 in
theorem S1p (Rc : Nat) (kz : I → Mode) (h1 : ∀ p, 1 ≤ nrm (kz p)) (p : I) :
    Le K O.toCone (K.mul (nu1 K inv Rc kz p) (nu1 K inv Rc kz p)) (cond (lowB Rc kz p) K.one (f4 K inv (nrm (kz p)))) := by
  have hb := (ivn_le K O inv mul_inv h10 kz h1 p).2
  show Le K O.toCone (K.mul (cond (lowB Rc kz p) _ _) (cond (lowB Rc kz p) _ _)) (cond (lowB Rc kz p) _ _)
  cases lowB Rc kz p with
  | true => exact hb
  | false => exact le_refl' K O.toCone _

theorem S2p (Rc t : Nat) (kz : I → Mode) (p : I) :
    Le K O.toCone (K.mul (nu2 K inv Rc t kz p) (nu2 K inv Rc t kz p))
      (cond (lowB Rc kz p) (K.mul (inv (castN K (t + 1))) (inv (castN K (t + 1)))) (f4 K inv (nrm (kz p)))) := by
  show Le K O.toCone (K.mul (cond (lowB Rc kz p) _ _) (cond (lowB Rc kz p) _ _)) (cond (lowB Rc kz p) _ _)
  cases lowB Rc kz p with
  | true => exact le_refl' K O.toCone _
  | false => exact le_refl' K O.toCone _

def cube (Rc : Nat) : Nat := (2 * Rc + 1) * ((2 * Rc + 1) * (2 * Rc + 1))

/-- monotone product step: x ≤ x', y ≤ y', x ≥ 0, c ≥ 0 ⇒ c·(x·(y·z)) ≤ c·(x'·(y'·z)) for z ≥ 0 -/
theorem mono3 (c x x' y y' z : R) (hc : O.nn c) (hx : O.nn x) (hz : O.nn z) (hy'z : O.nn (K.mul y' z))
    (h1 : Le K O.toCone x x') (h2 : Le K O.toCone y y') :
    Le K O.toCone (K.mul c (K.mul x (K.mul y z))) (K.mul c (K.mul x' (K.mul y' z))) := by
  have a := le_mul_right' K O.toCone h2 hz
  have b := le_mul_nn K O.toCone x _ _ hx a
  have d := le_mul_right' K O.toCone h1 hy'z
  exact le_mul_nn K O.toCone c _ _ hc (le_trans' K O.toCone _ _ _ b d)

include mul_inv h10 in
theorem two_scale (l : List I) (kz : I → Mode) (hd : List.Pairwise (fun a b => kz a ≠ kz b) l)
    (h1 : ∀ p, 1 ≤ nrm (kz p)) (M : Nat) (hM : ∀ p, p ∈ l → nrm (kz p) ≤ M + 1) (Rc t : Nat) (hRM : Rc ≤ M)
    (ι : R) (u v : I → V3 R) :
    Le K O.toCone (lsum K (fun r => dot3 K (Nl K l (TriadSparse.delta K kz) ι (kv K kz) u v r)
        (Nl K l (TriadSparse.delta K kz) ι (kv K kz) u v r)) l)
      (K.add
        (K.mul (K.add K.one K.one) (K.mul (K.mul ι ι) (K.mul (lsum K (fun p => K.mul (wt K kz p) (dot3 K (u p) (u p))) l)
          (K.mul (K.add (K.mul (castN K (cube Rc)) K.one) (K.mul (castN K 135) (iv K inv (Rc + 1))))
            (lsum K (fun q => K.mul (wt K kz q) (dot3 K (v q) (v q))) l)))))
        (K.mul (K.add K.one K.one) (K.mul (K.mul ι ι) (K.mul (lsum K (fun p => K.mul (K.mul (wt K kz p) (wt K kz p)) (dot3 K (u p) (u p))) l)
          (K.mul (K.add (K.mul (castN K (cube Rc)) (K.mul (inv (castN K (t + 1))) (inv (castN K (t + 1)))))
              (K.mul (castN K 135) (iv K inv (Rc + 1))))
            (lsum K (fun q => K.mul (wt K kz q) (dot3 K (v q) (v q))) l)))))) := by
  have hsplit : ∀ r, Nl K l (TriadSparse.delta K kz) ι (kv K kz) u v r
      = add3 K (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (uLo K Rc kz u) v r)
          (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (uHi K Rc kz u) v r) := by
    intro r
    rw [ForceLip.Nl_congr K l _ ι _ u (addF K (uLo K Rc kz u) (uHi K Rc kz u)) v v r (split_u K Rc kz u) (fun _ => rfl),
      Nl_add_left]
  rw [lsum_ext K _ _ (fun r => by rw [hsplit r]) l]
  have s1 := lsum_le K O.toCone _ _ (fun r => sq2 K O
    (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (uLo K Rc kz u) v r)
    (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (uHi K Rc kz u) v r)) l
  rw [lsum_add, lsum_mul_left, lsum_mul_left] at s1
  refine le_trans' K O.toCone _ _ _ s1 (le_add' K O.toCone _ _ _ _ ?_ ?_)
  · have T := total_sob K O.toCone l (TriadSparse.delta K kz) ι (kv K kz) (uLo K Rc kz u) v (mu1 K Rc kz) (nu1 K inv Rc kz)
      (delta01 K kz) (munu1 K O inv mul_inv h10 Rc kz h1)
      (fun p r => TriadSparse.spA K O.toCone kz l hd p r) (fun p q => TriadSparse.spB K O.toCone kz l hd p q)
    have hH := lsum_le K O.toCone _ _ (fun p => H1p K O Rc kz u p) l
    have hS := le_trans' K O.toCone _ _ _ (lsum_le K O.toCone _ _ (fun p => S1p K O inv mul_inv h10 Rc kz h1 p) l)
      (s_split K O inv mul_inv h10 Rc l kz hd M hM hRM K.one (RatModel.nn_one' K O.toCone))
    have nH : O.nn (lsum K (fun p => K.mul (K.mul (mu1 K Rc kz p) (mu1 K Rc kz p)) (dot3 K (uLo K Rc kz u p) (uLo K Rc kz u p))) l) :=
      lsum_nn K O.toCone _ (fun p => O.nn_mul _ _ (O.nn_sq _) (dot3_nn K O.toCone _)) l
    have nZu : O.nn (lsum K (fun p => K.mul (wt K kz p) (dot3 K (u p) (u p))) l) :=
      lsum_nn K O.toCone _ (fun p => O.nn_mul _ _ (dot3_nn K O.toCone _) (dot3_nn K O.toCone _)) l
    have nZv : O.nn (lsum K (fun q => K.mul (wt K kz q) (dot3 K (v q) (v q))) l) :=
      lsum_nn K O.toCone _ (fun q => O.nn_mul _ _ (dot3_nn K O.toCone _) (dot3_nn K O.toCone _)) l
    have nC : O.nn (K.add (K.mul (castN K (cube Rc)) K.one) (K.mul (castN K 135) (iv K inv (Rc + 1)))) :=
      O.nn_add _ _ (O.nn_mul _ _ (RatModel.nn_cast K O.toCone _) (RatModel.nn_one' K O.toCone))
        (O.nn_mul _ _ (RatModel.nn_cast K O.toCone _) (iv_nn K O inv mul_inv h10 Rc))
    have m := mono3 K O (K.mul ι ι) _ _ _ _ _ (O.nn_sq ι) nH nZv (O.nn_mul _ _ nC nZv) hH hS
    exact le_mul_nn K O.toCone _ _ _ (O.nn_add _ _ (RatModel.nn_one' K O.toCone) (RatModel.nn_one' K O.toCone))
      (le_trans' K O.toCone _ _ _ T m)
  · have T := total_sob K O.toCone l (TriadSparse.delta K kz) ι (kv K kz) (uHi K Rc kz u) v (mu2 K Rc t kz) (nu2 K inv Rc t kz)
      (delta01 K kz) (munu2 K O inv mul_inv h10 Rc t kz h1)
      (fun p r => TriadSparse.spA K O.toCone kz l hd p r) (fun p q => TriadSparse.spB K O.toCone kz l hd p q)
    have hH := lsum_le K O.toCone _ _ (fun p => H2p K O Rc t kz u p) l
    have hS := le_trans' K O.toCone _ _ _ (lsum_le K O.toCone _ _ (fun p => S2p K O inv Rc t kz p) l)
      (s_split K O inv mul_inv h10 Rc l kz hd M hM hRM _ (O.nn_sq _))
    have nH : O.nn (lsum K (fun p => K.mul (K.mul (mu2 K Rc t kz p) (mu2 K Rc t kz p)) (dot3 K (uHi K Rc kz u p) (uHi K Rc kz u p))) l) :=
      lsum_nn K O.toCone _ (fun p => O.nn_mul _ _ (O.nn_sq _) (dot3_nn K O.toCone _)) l
    have nAu : O.nn (lsum K (fun p => K.mul (K.mul (wt K kz p) (wt K kz p)) (dot3 K (u p) (u p))) l) :=
      lsum_nn K O.toCone _ (fun p => O.nn_mul _ _ (O.nn_sq _) (dot3_nn K O.toCone _)) l
    have nZv : O.nn (lsum K (fun q => K.mul (wt K kz q) (dot3 K (v q) (v q))) l) :=
      lsum_nn K O.toCone _ (fun q => O.nn_mul _ _ (dot3_nn K O.toCone _) (dot3_nn K O.toCone _)) l
    have nC : O.nn (K.add (K.mul (castN K (cube Rc)) (K.mul (inv (castN K (t + 1))) (inv (castN K (t + 1)))))
        (K.mul (castN K 135) (iv K inv (Rc + 1)))) :=
      O.nn_add _ _ (O.nn_mul _ _ (RatModel.nn_cast K O.toCone _) (O.nn_sq _))
        (O.nn_mul _ _ (RatModel.nn_cast K O.toCone _) (iv_nn K O inv mul_inv h10 Rc))
    have m := mono3 K O (K.mul ι ι) _ _ _ _ _ (O.nn_sq ι) nH nZv (O.nn_mul _ _ nC nZv) hH hS
    exact le_mul_nn K O.toCone _ _ _ (O.nn_add _ _ (RatModel.nn_one' K O.toCone) (RatModel.nn_one' K O.toCone))
      (le_trans' K O.toCone _ _ _ T m)

end Gen

end ForceTwoScale

#print axioms ForceTwoScale.split_u
#print axioms ForceTwoScale.sq2
#print axioms ForceTwoScale.munu1
#print axioms ForceTwoScale.munu2
#print axioms ForceTwoScale.ivn_le
#print axioms ForceTwoScale.H1p
#print axioms ForceTwoScale.H2p
#print axioms ForceTwoScale.S1p
#print axioms ForceTwoScale.S2p
#print axioms ForceTwoScale.two_scale

ns/ForceUniform.lean

lean_all/ns/ForceUniform.lean

/-
  ns/ForceUniform.lean — T37 cycle 969 (goal G637): the Galerkin force bound with a constant independent of the
  truncation, over an ordered field. For integer modes kz : I → ℤ³, nonzero (every index) and pairwise distinct on the mode list
  l (any finite truncation), the triad indicator δ = [kz_p + kz_q + kz_r = 0] (TriadSparse.delta), any ring vectors
  k, u, v and weights μ_p = |kz_p|_∞², ν_p = |kz_p|_∞^{−2}:
    force_uniform   Σ_r |N_r(u,v)|² ≤ ι² · H_μ(u) · (135 · Z(v)),
  H_μ(u) = Σ_p |kz_p|_∞⁴ |u_p|², Z(v) = Σ_q |k_q|² |v_q|² — the discrete ‖(u·∇)v‖² ≲ ‖u‖²_{H²}‖∇v‖² with a constant
  (135) that does not depend on the number of modes. ForceSob.total_sob + TriadSparse (spA, spB) +
  LatticeBound.lattice_bound (Σ_p ν_p² = Σ_p |kz_p|_∞^{−4} ≤ 135).

  Lean 4 core plus ns/ForceSob, ns/TriadSparse, ns/LatticeBound.
-/
import ns.ForceSob
import ns.TriadSparse
import ns.LatticeBound

namespace ForceUniform
open DetChain NSChain DyadicSmallGlobal LibRing DyadicLarge FourierGalerkin GalerkinForce ForceBound ForceSob
  AbelCount LatticeStep LatticeSum ModeCount CubeCount LatticeBound GoodModesQ

section Gen
variable {R : Type} (K : CRing R) (O : OrdR K) {I : Type}
variable (inv : R → R) (mul_inv : ∀ y, y ≠ K.zero → K.mul y (inv y) = K.one) (h10 : K.one ≠ K.zero)

theorem lsum_norms (g : Nat → R) (kz : I → Mode) :
    ∀ (l : List I), lsum K (fun p => g (nrm (kz p))) l = lsumN K g (norms (l.map kz))
  | [] => rfl
  | p :: l => by
    show K.add (g (nrm (kz p))) (lsum K (fun p => g (nrm (kz p))) l) = K.add (g (nrm (kz p))) (lsumN K g (norms (l.map kz)))
    rw [lsum_norms g kz l]

theorem mem_map_inv (kz : I → Mode) (y : Mode) : ∀ (l : List I), y ∈ l.map kz → ∃ p, p ∈ l ∧ kz p = y
  | [], h => nomatch h
  | p :: l, h => by
    cases h with
    | head => exact ⟨p, List.Mem.head _, rfl⟩
    | tail _ h' =>
      have ⟨q, hq, e⟩ := mem_map_inv kz y l h'
      exact ⟨q, List.Mem.tail _ hq, e⟩

theorem pw_map (kz : I → Mode) : ∀ (l : List I), List.Pairwise (fun a b => kz a ≠ kz b) l →
    List.Pairwise (fun a b => a ≠ b) (l.map kz)
  | [], _ => List.Pairwise.nil
  | p :: l, hp => by
    cases hp with
    | cons hx hl =>
      exact List.Pairwise.cons (fun y hy => by
        have ⟨q, hq, e⟩ := mem_map_inv kz y l hy
        rw [← e]; exact hx q hq) (pw_map kz l hl)

theorem delta01 (kz : I → Mode) (p q r : I) :
    TriadSparse.delta K kz p q r = K.zero ∨ TriadSparse.delta K kz p q r = K.one := by
  show cond _ K.one K.zero = K.zero ∨ cond _ K.one K.zero = K.one
  generalize decide (TriadSparse.tadd (TriadSparse.tadd (kz p) (kz q)) (kz r) = TriadSparse.zero3) = b
  cases b with
  | false => exact Or.inl rfl
  | true => exact Or.inr rfl

include O mul_inv h10 in
theorem inv_cast (n : Nat) (hn : 1 ≤ n) : K.mul (castN K n) (inv (castN K n)) = K.one := by
  cases n with
  | zero => exact absurd hn (Nat.not_succ_le_zero 0)
  | succ n' => exact mul_inv _ (cast_succ_ne K O h10 n')

def mu (kz : I → Mode) (p : I) : R := K.mul (castN K (nrm (kz p))) (castN K (nrm (kz p)))
def nu (kz : I → Mode) (p : I) : R := K.mul (inv (castN K (nrm (kz p)))) (inv (castN K (nrm (kz p))))

include mul_inv h10 in
theorem force_uniform (l : List I) (kz : I → Mode) (hd : List.Pairwise (fun a b => kz a ≠ kz b) l)
    (h1 : ∀ p, 1 ≤ nrm (kz p)) (M : Nat) (hM : ∀ p, p ∈ l → nrm (kz p) ≤ M + 1)
    (ι : R) (k u v : I → V3 R) :
    Le K O.toCone (lsum K (fun r => dot3 K (Nl K l (TriadSparse.delta K kz) ι k u v r) (Nl K l (TriadSparse.delta K kz) ι k u v r)) l)
      (K.mul (K.mul ι ι) (K.mul (lsum K (fun p => K.mul (K.mul (mu K kz p) (mu K kz p)) (dot3 K (u p) (u p))) l)
        (K.mul (castN K 135) (lsum K (fun q => K.mul (dot3 K (k q) (k q)) (dot3 K (v q) (v q))) l)))) := by
  have hμν : ∀ p, K.mul (mu K kz p) (nu K inv kz p) = K.one := by
    intro p
    have hc := inv_cast K O inv mul_inv h10 (nrm (kz p)) (h1 p)
    show K.mul (K.mul (castN K (nrm (kz p))) (castN K (nrm (kz p))))
      (K.mul (inv (castN K (nrm (kz p)))) (inv (castN K (nrm (kz p))))) = K.one
    generalize castN K (nrm (kz p)) = c at hc
    calc K.mul (K.mul c c) (K.mul (inv c) (inv c)) = K.mul (K.mul c (inv c)) (K.mul c (inv c)) := by lib_ring
      _ = K.one := by rw [hc]; lib_ring
  have T := total_sob K O.toCone l (TriadSparse.delta K kz) ι k u v (mu K kz) (nu K inv kz) (delta01 K kz) hμν
    (fun p r => TriadSparse.spA K O.toCone kz l hd p r) (fun p q => TriadSparse.spB K O.toCone kz l hd p q)
  have eS : lsum K (fun p => K.mul (nu K inv kz p) (nu K inv kz p)) l = lsumN K (f4 K inv) (norms (l.map kz)) :=
    lsum_norms K (f4 K inv) kz l
  have hS := lattice_bound K O inv mul_inv h10 (l.map kz) (pw_map kz l hd)
    (fun k hk => by have ⟨q, _, e⟩ := mem_map_inv kz k l hk; rw [← e]; exact h1 q) M
    (fun k hk => by have ⟨q, hq, e⟩ := mem_map_inv kz k l hk; rw [← e]; exact hM q hq)
  rw [← eS] at hS
  generalize lsum K (fun p => K.mul (nu K inv kz p) (nu K inv kz p)) l = S at T hS
  have Zn : O.nn (lsum K (fun q => K.mul (dot3 K (k q) (k q)) (dot3 K (v q) (v q))) l) :=
    lsum_nn K O.toCone _ (fun q => O.nn_mul _ _ (dot3_nn K O.toCone _) (dot3_nn K O.toCone _)) l
  have Hn : O.nn (lsum K (fun p => K.mul (K.mul (mu K kz p) (mu K kz p)) (dot3 K (u p) (u p))) l) :=
    lsum_nn K O.toCone _ (fun p => O.nn_mul _ _ (O.nn_sq _) (dot3_nn K O.toCone _)) l
  have s1 := le_mul_right' K O.toCone hS Zn
  have s2 := le_mul_nn K O.toCone _ _ _ Hn s1
  have s3 := le_mul_nn K O.toCone _ _ _ (O.nn_sq ι) s2
  exact le_trans' K O.toCone _ _ _ T s3

end Gen

end ForceUniform

#print axioms ForceUniform.lsum_norms
#print axioms ForceUniform.pw_map
#print axioms ForceUniform.force_uniform

ns/ForcedCN.lean

lean_all/ns/ForcedCN.lean

/-
  ns/ForcedCN.lean — T37 cycle 786 (goal G497): one mode of the linear Crank–Nicolson step with a given forcing f
  (the semi-implicit scheme: dissipation implicit, nonlinearity explicit), over an ordered field (ℚ included):
    a' − a = −x(a' + a) + h f,   x = hνk ≥ 0
  has the solution a' = ((1 − x)a + h f)·(1 + x)⁻¹ and no other. So the semi-implicit dyadic scheme always has a
  unique next state over ℚ (the implicit CN step needs roots of a quadratic system).

  Lean 4 core plus ns/StokesCN, ns/GoodModesQ (ne_of_one_add), lib/RingTac.
    forced_exist   the formula solves the step
    forced_unique  any two solutions coincide
-/
import ns.StokesCN

namespace ForcedCN
open DetChain NSChain DyadicSmallGlobal LibRing DyadicLarge GoodModesQ

section Gen
variable {R : Type} (K : CRing R) (O : OrdR K)
include O

theorem forced_exist (inv : R → R) (mul_inv : ∀ y, y ≠ K.zero → K.mul y (inv y) = K.one) (h10 : K.one ≠ K.zero)
    (a x hf : R) (hx : O.nn x) :
    let a' := K.mul (K.add (K.mul (K.add K.one (K.neg x)) a) hf) (inv (K.add K.one x))
    K.add a' (K.neg a) = K.add (K.neg (K.mul x (K.add a' a))) hf := by
  intro a'
  have hi := mul_inv _ (ne_of_one_add K O h10 x hx)
  -- (1+x)a' = (1−x)a + hf, then rearrange
  have e : K.mul (K.add K.one x) a' = K.add (K.mul (K.add K.one (K.neg x)) a) hf := by
    show K.mul (K.add K.one x) (K.mul (K.add (K.mul (K.add K.one (K.neg x)) a) hf) (inv (K.add K.one x))) = _
    have : K.mul (K.add K.one x) (K.mul (K.add (K.mul (K.add K.one (K.neg x)) a) hf) (inv (K.add K.one x)))
        = K.mul (K.mul (K.add K.one x) (inv (K.add K.one x))) (K.add (K.mul (K.add K.one (K.neg x)) a) hf) := by lib_ring
    rw [this, hi]; lib_ring
  have t : K.add a' (K.neg a) = K.add (K.add (K.neg (K.mul x (K.add a' a))) hf)
      (K.add (K.mul (K.add K.one x) a') (K.neg (K.add (K.mul (K.add K.one (K.neg x)) a) hf))) := by lib_ring
  exact t.trans (by rw [e]; lib_ring)

theorem forced_unique (h10 : K.one ≠ K.zero) (a a' b' x hf : R) (hx : O.nn x)
    (s1 : K.add a' (K.neg a) = K.add (K.neg (K.mul x (K.add a' a))) hf)
    (s2 : K.add b' (K.neg a) = K.add (K.neg (K.mul x (K.add b' a))) hf) : a' = b' := by
  have d : K.mul (K.add K.one x) (K.add a' (K.neg b')) = K.zero := by
    have t : K.mul (K.add K.one x) (K.add a' (K.neg b'))
        = K.add (K.add (K.add a' (K.neg a)) (K.neg (K.add (K.neg (K.mul x (K.add a' a))) hf)))
          (K.neg (K.add (K.add b' (K.neg a)) (K.neg (K.add (K.neg (K.mul x (K.add b' a))) hf)))) := by lib_ring
    exact t.trans (by rw [s1, s2]; lib_ring)
  cases O.noZD _ _ d with
  | inl e => exact absurd e (ne_of_one_add K O h10 x hx)
  | inr e =>
    have : a' = K.add (K.add a' (K.neg b')) b' := by lib_ring
    rw [this, e]; lib_ring

end Gen

end ForcedCN

#print axioms ForcedCN.forced_exist
#print axioms ForcedCN.forced_unique

ns/ForcedDamp.lean

lean_all/ns/ForcedDamp.lean

/-
  ns/ForcedDamp.lean — T37 cycle 809 (goal G521): mode damping of the semi-implicit step with forcing, over any
  ring with a positive cone (ℚ included): with x ≥ 0 and a' − a = −x(a' + a) + h f,
    (1 + x)²·a'² ≤ 2(1 + x)²·a² + 2h²f².
  High modes (large x = hνk) are slaved to the forcing: a'² ≤ 2a² + 2h²f²/(1 + x)².

  Lean 4 core plus ns/ForcedCN (lin form), ns/MidRecover (sq_add_le, two), lib/RingTac.
    forced_form   (1 + x)a' = (1 − x)a + h f
    forced_damp   the inequality
-/
import ns.ForcedCN
import ns.MidRecover

namespace ForcedDamp
open DetChain NSChain LibRing DyadicLarge MidRecover

section Gen
variable {R : Type} (K : CRing R) (P : Cone K)

theorem forced_form (a a' x hf : R) (st : K.add a' (K.neg a) = K.add (K.neg (K.mul x (K.add a' a))) hf) :
    K.mul (K.add K.one x) a' = K.add (K.mul (K.add K.one (K.neg x)) a) hf := by
  have t : K.mul (K.add K.one x) a' = K.add (K.add (K.mul (K.add K.one (K.neg x)) a) hf)
      (K.add (K.add a' (K.neg a)) (K.neg (K.add (K.neg (K.mul x (K.add a' a))) hf))) := by lib_ring
  exact t.trans (by rw [st]; lib_ring)

theorem forced_damp (a a' x hf : R) (hx : P.nn x) (st : K.add a' (K.neg a) = K.add (K.neg (K.mul x (K.add a' a))) hf) :
    Le K P (K.mul (K.mul (K.add K.one x) (K.add K.one x)) (K.mul a' a'))
      (K.add (two K (K.mul (K.mul (K.add K.one x) (K.add K.one x)) (K.mul a a))) (two K (K.mul hf hf))) := by
  have e := forced_form K a a' x hf st
  have l := sq_add_le K P (K.mul (K.add K.one (K.neg x)) a) hf
  have e2 : K.mul (K.mul (K.add K.one x) (K.add K.one x)) (K.mul a' a')
      = K.mul (K.add (K.mul (K.add K.one (K.neg x)) a) hf) (K.add (K.mul (K.add K.one (K.neg x)) a) hf) := by
    rw [← e]; lib_ring
  rw [e2]
  apply le_trans' K P _ _ _ l
  apply le_add' K P _ _ _ _ _ (le_refl' K P _)
  -- (1 − x)² a² ≤ (1 + x)² a²: difference 4x a² ≥ 0
  unfold two
  refine ⟨K.add (K.mul (K.add (K.add x x) (K.add x x)) (K.mul a a)) (K.mul (K.add (K.add x x) (K.add x x)) (K.mul a a)),
    P.nn_add _ _ (P.nn_mul _ _ (P.nn_add _ _ (P.nn_add _ _ hx hx) (P.nn_add _ _ hx hx)) (P.nn_sq a))
      (P.nn_mul _ _ (P.nn_add _ _ (P.nn_add _ _ hx hx) (P.nn_add _ _ hx hx)) (P.nn_sq a)), ?_⟩
  lib_ring

end Gen

end ForcedDamp

#print axioms ForcedDamp.forced_form
#print axioms ForcedDamp.forced_damp

ns/ForcedDiffStep.lean

lean_all/ns/ForcedDiffStep.lean

/-
  ns/ForcedDiffStep.lean — T37 cycle 1766 (goal G852g, piece b): one step of the enstrophy-difference estimate when
  the forces differ from the Galerkin nonlinearities by a defect: Σ|N^a_t − N^b_t|² ≤ 2Σ|Nl(a_t) − Nl(b_t)|² + 2D_t
  (e.g. b = the low-mode projection of a finer truncation, D_t its truncation defect, TruncBound). Then
      Q_{t+1}(a − b) ≤ νZ_t + h(α_tZ_t + β_tA_t) + h·(2D_t)
  with α_t, β_t twice those of EnstLipschitz — exactly the step hypothesis of GronwallSrc.gronwall_src.
  Lean 4 core plus ns/EnstLipschitz.
-/
import ns.EnstLipschitz

namespace ForcedDiffStep
open DetChain NSChain DyadicSmallGlobal LibRing DyadicLarge FourierGalerkin GalerkinForce ForceBound ForceSob
  ModeCount CubeCount ForceUniform NormCompare EnstStep EnstGlobal EnstGalerkin ForceTwoScale ForceBilin EnstDiff
  ForceDiff EnstGronwall LatticeSum EnstLipschitz

section Gen
variable {R : Type} (K : CRing R) (O : OrdR K) {I : Type}
variable (inv : R → R) (mul_inv : ∀ y, y ≠ K.zero → K.mul y (inv y) = K.one) (h10 : K.one ≠ K.zero)

include mul_inv h10 in
theorem forced_step (l : List I) (kz : I → Mode) (hd : List.Pairwise (fun a b => kz a ≠ kz b) l)
    (h1 : ∀ p, 1 ≤ nrm (kz p)) (M : Nat) (hM : ∀ p, p ∈ l → nrm (kz p) ≤ M + 1) (Rc t0 : Nat) (hRM : Rc ≤ M)
    (ι h ν : R) (hh : O.nn h) (hν : O.nn ν) (a b Na Nb : Nat → I → V3 R) (D : Nat → R)
    (hN : ∀ t, Le K O.toCone (lsum K (fun r => dot3 K (sub3 K (Na t r) (Nb t r)) (sub3 K (Na t r) (Nb t r))) l)
      (K.add (K.mul (two K) (lsum K (fun r => dot3 K (sub3 K (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (a t) (a t) r)
          (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (b t) (b t) r))
        (sub3 K (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (a t) (a t) r)
          (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (b t) (b t) r))) l)) (K.mul (two K) (D t))))
    (ha : ∀ t r, Step K h ν (wt K kz r) (a (t + 1) r) (a t r) (Na t r))
    (hb : ∀ t r, Step K h ν (wt K kz r) (b (t + 1) r) (b t r) (Nb t r)) (s : Nat) :
    Le K O.toCone (Q K ν h (Zs K l kz (dS K a b)) (As K l kz (dS K a b)) (s + 1))
      (K.add (K.add (K.mul ν (Zs K l kz (dS K a b) s))
        (K.mul h (K.add
          (K.mul (K.mul (two K) (K.add (K.mul (two K) (K.mul (K.mul (two K) (K.mul ι ι)) (K.mul (C1 K inv Rc) (Zs K l kz a s))))
            (K.mul (two K) (K.mul (K.mul ι ι) (K.mul (castN K 135) (As K l kz b s)))))) (Zs K l kz (dS K a b) s))
          (K.mul (K.mul (two K) (K.mul (two K) (K.mul (K.mul (two K) (K.mul ι ι)) (K.mul (C2 K inv Rc t0) (Zs K l kz a s)))))
            (As K l kz (dS K a b) s)))))
        (K.mul h (K.mul (two K) (D s)))) := by
  have hw : ∀ r, O.nn (wt K kz r) := fun r => dot3_nn K O.toCone _
  have n2 : O.nn (two K) := O.nn_add _ _ (RatModel.nn_one' K O.toCone) (RatModel.nn_one' K O.toCone)
  have st := enst_step K O.toCone l h ν hh hν (wt K kz) hw (dS K a b (s + 1)) (dS K a b s)
    (fun r => sub3 K (Na s r) (Nb s r)) (fun r => step_sub K h ν (wt K kz r) _ _ _ _ _ _ (ha s r) (hb s r))
  have fd := force_diff K O inv mul_inv h10 l kz hd h1 M hM Rc t0 hRM ι (a s) (b s)
  have fd2 := le_add' K O.toCone _ _ _ _ (le_mul_nn K O.toCone (two K) _ _ n2 fd) (le_refl' K O.toCone (K.mul (two K) (D s)))
  have hb2 := le_mul_nn K O.toCone h _ _ hh (le_trans' K O.toCone _ _ _ (hN s) fd2)
  have st2 := le_trans' K O.toCone _ _ _ st (le_add' K O.toCone _ _ _ _ (le_refl' K O.toCone _) hb2)
  refine le_trans' K O.toCone _ _ _ st2 ⟨K.zero, O.nn_zero, ?_⟩
  show K.add (K.add (K.mul ν (Zs K l kz (dS K a b) s)) (K.mul h (K.add (K.mul (two K) (K.add
      (K.mul (two K) (K.add
        (K.mul (two K) (K.mul (K.mul ι ι) (K.mul (Zs K l kz (dS K a b) s) (K.mul (C1 K inv Rc) (Zs K l kz a s)))))
        (K.mul (two K) (K.mul (K.mul ι ι) (K.mul (As K l kz (dS K a b) s) (K.mul (C2 K inv Rc t0) (Zs K l kz a s)))))))
      (K.mul (two K) (K.mul (K.mul ι ι) (K.mul (As K l kz b s) (K.mul (castN K 135) (Zs K l kz (dS K a b) s)))))))
      (K.mul (two K) (D s))))) K.zero
    = K.add (K.add (K.mul ν (Zs K l kz (dS K a b) s))
        (K.mul h (K.add
          (K.mul (K.mul (two K) (K.add (K.mul (two K) (K.mul (K.mul (two K) (K.mul ι ι)) (K.mul (C1 K inv Rc) (Zs K l kz a s))))
            (K.mul (two K) (K.mul (K.mul ι ι) (K.mul (castN K 135) (As K l kz b s)))))) (Zs K l kz (dS K a b) s))
          (K.mul (K.mul (two K) (K.mul (two K) (K.mul (K.mul (two K) (K.mul ι ι)) (K.mul (C2 K inv Rc t0) (Zs K l kz a s)))))
            (As K l kz (dS K a b) s)))))
        (K.mul h (K.mul (two K) (D s)))
  lib_ring

end Gen
end ForcedDiffStep

#print axioms ForcedDiffStep.forced_step

ns/ForcedEnstrophy.lean

lean_all/ns/ForcedEnstrophy.lean

/-
  ns/ForcedEnstrophy.lean — T37 cycle 120: the energy inequality with forcing, one Crank–Nicolson step.

  Lean 4 core plus the shared lib/OField (young), lib/Gronwall (le), NSChain (lsum), lib/RingTac.
  Modes i ∈ l, a′_i − a_i = h(−ν k_i s_i + N_i + f_i), s = a′ + a, Σ s N = 0 (energy-neutral nonlinearity),
  k_i = 1 + κ_i with κ_i ≥ 0 (lattice: |k|² ≥ 1), h, ν ≥ 0, ν ≠ 0:
    step_identity   E′ + hν Σ k s² = E + h Σ s f
    forced_step     2E′ + hν Σ k s² ≤ 2E + (h/ν) Σ f²           (Young with ε = ν)
-/
import lib.OField
import NSChain

namespace ForcedEnstrophy
open DetChain NSChain LibRing LibCS LibGron LibOF

variable {R : Type} (K : CRing R) {I : Type}

theorem step_identity (w a a' k Nn f : I → R) (h nu : R) (l : List I)
    (step : ∀ i, sub K (a' i) (a i) = K.mul h (K.add (K.add (K.neg (K.mul nu (K.mul (k i) (K.add (a' i) (a i))))) (Nn i)) (f i)))
    (neutral : lsum K (fun i => K.mul (w i) (K.mul (K.add (a' i) (a i)) (Nn i))) l = K.zero) :
    K.add (lsum K (fun i => K.mul (w i) (K.mul (a' i) (a' i))) l)
        (K.mul (K.mul h nu) (lsum K (fun i => K.mul (w i) (K.mul (k i) (K.mul (K.add (a' i) (a i)) (K.add (a' i) (a i))))) l))
      = K.add (lsum K (fun i => K.mul (w i) (K.mul (a i) (a i))) l)
          (K.mul h (lsum K (fun i => K.mul (w i) (K.mul (K.add (a' i) (a i)) (f i))) l)) := by
  -- per mode: a′² + hν k s² = a² + h s N + h s f
  have pm : ∀ i, K.add (K.mul (w i) (K.mul (a' i) (a' i))) (K.mul (K.mul h nu) (K.mul (w i) (K.mul (k i) (K.mul (K.add (a' i) (a i)) (K.add (a' i) (a i))))))
      = K.add (K.add (K.mul (w i) (K.mul (a i) (a i))) (K.mul h (K.mul (w i) (K.mul (K.add (a' i) (a i)) (f i)))))
          (K.mul h (K.mul (w i) (K.mul (K.add (a' i) (a i)) (Nn i)))) := by
    intro i
    have e : K.mul (a' i) (a' i) = K.add (K.mul (a i) (a i)) (K.mul (sub K (a' i) (a i)) (K.add (a' i) (a i))) := by lib_ring
    rw [e, step i]; lib_ring
  have sum_pm := lsum_ext K _ _ pm l
  rw [lsum_add, lsum_add, lsum_add, lsum_mul_left, lsum_mul_left, lsum_mul_left, neutral] at sum_pm
  rw [show K.add (lsum K (fun i => K.mul (w i) (K.mul (a i) (a i))) l) (K.mul h (lsum K (fun i => K.mul (w i) (K.mul (K.add (a' i) (a i)) (f i))) l))
      = K.add (K.add (lsum K (fun i => K.mul (w i) (K.mul (a i) (a i))) l) (K.mul h (lsum K (fun i => K.mul (w i) (K.mul (K.add (a' i) (a i)) (f i))) l)))
          (K.mul h K.zero) by lib_ring]
  exact sum_pm

theorem lsum_le' (P : Pos K) (u v : I → R) (h : ∀ i, le K P (u i) (v i)) :
    ∀ l : List I, le K P (lsum K u l) (lsum K v l)
  | [] => le_refl K P _
  | i :: l => le_add2 K P (h i) (lsum_le' P u v h l)

/-- **Forced step**: 2E′ + hνΣks² ≤ 2E + (h/ν)Σf². -/
theorem forced_step (P : Pos K) (F : OF K P) (w a a' κ Nn f : I → R) (hw : ∀ i, P.nn (w i)) (h nu : R) (l : List I)
    (hh : P.nn h) (hnu : P.nn nu) (hnu0 : nu ≠ K.zero) (hκ : ∀ i, P.nn (κ i))
    (step : ∀ i, sub K (a' i) (a i) = K.mul h (K.add (K.add (K.neg (K.mul nu (K.mul (K.add K.one (κ i)) (K.add (a' i) (a i))))) (Nn i)) (f i)))
    (neutral : lsum K (fun i => K.mul (w i) (K.mul (K.add (a' i) (a i)) (Nn i))) l = K.zero) :
    le K P (K.add (K.add (lsum K (fun i => K.mul (w i) (K.mul (a' i) (a' i))) l) (lsum K (fun i => K.mul (w i) (K.mul (a' i) (a' i))) l))
              (K.mul (K.mul h nu) (lsum K (fun i => K.mul (w i) (K.mul (K.add K.one (κ i)) (K.mul (K.add (a' i) (a i)) (K.add (a' i) (a i))))) l)))
      (K.add (K.add (lsum K (fun i => K.mul (w i) (K.mul (a i) (a i))) l) (lsum K (fun i => K.mul (w i) (K.mul (a i) (a i))) l))
             (K.mul (K.mul h (F.inv nu)) (lsum K (fun i => K.mul (w i) (K.mul (f i) (f i))) l))) := by
  have id := step_identity K w a a' (fun i => K.add K.one (κ i)) Nn f h nu l step neutral
  -- weighted Young per mode: w·2sf ≤ ν w (1+κ) s² + w f²/ν
  have y : ∀ i, le K P (K.mul (castZ K 2) (K.mul (w i) (K.mul (K.add (a' i) (a i)) (f i))))
      (K.add (K.mul nu (K.mul (w i) (K.mul (K.add K.one (κ i)) (K.mul (K.add (a' i) (a i)) (K.add (a' i) (a i))))))
             (K.mul (F.inv nu) (K.mul (w i) (K.mul (f i) (f i))))) := by
    intro i
    have y0 := le_mul_left K P (hw i) (young K P F nu (K.add (a' i) (a i)) (f i) hnu hnu0)
    refine le_trans K P (by
        unfold le; rw [show sub K (K.mul (w i) (K.mul (castZ K 2) (K.mul (K.add (a' i) (a i)) (f i))))
            (K.mul (castZ K 2) (K.mul (w i) (K.mul (K.add (a' i) (a i)) (f i)))) = K.zero by lib_ring]
        exact P.nn_zero) (le_trans K P y0 ?_)
    unfold le
    rw [show sub K (K.add (K.mul nu (K.mul (w i) (K.mul (K.add K.one (κ i)) (K.mul (K.add (a' i) (a i)) (K.add (a' i) (a i))))))
             (K.mul (F.inv nu) (K.mul (w i) (K.mul (f i) (f i)))))
          (K.mul (w i) (K.add (K.mul nu (K.mul (K.add (a' i) (a i)) (K.add (a' i) (a i)))) (K.mul (K.mul (f i) (f i)) (F.inv nu))))
        = K.mul nu (K.mul (w i) (K.mul (κ i) (K.mul (K.add (a' i) (a i)) (K.add (a' i) (a i))))) by lib_ring]
    exact P.nn_mul _ _ hnu (P.nn_mul _ _ (hw i) (P.nn_mul _ _ (hκ i) (P.nn_sq _)))
  have ys := le_mul_left K P hh (lsum_le' K P _ _ y l)
  rw [lsum_mul_left, lsum_add, lsum_mul_left, lsum_mul_left] at ys
  unfold le at ys ⊢
  revert ys id
  generalize lsum K (fun i => K.mul (w i) (K.mul (a' i) (a' i))) l = E'
  generalize lsum K (fun i => K.mul (w i) (K.mul (a i) (a i))) l = E
  generalize lsum K (fun i => K.mul (w i) (K.mul (K.add K.one (κ i)) (K.mul (K.add (a' i) (a i)) (K.add (a' i) (a i))))) l = D
  generalize lsum K (fun i => K.mul (w i) (K.mul (K.add (a' i) (a i)) (f i))) l = SF
  generalize lsum K (fun i => K.mul (w i) (K.mul (f i) (f i))) l = FF
  intro id ys
  have e : sub K (K.add (K.add E E) (K.mul (K.mul h (F.inv nu)) FF)) (K.add (K.add E' E') (K.mul (K.mul h nu) D))
      = K.add (sub K (K.mul h (K.add (K.mul nu D) (K.mul (F.inv nu) FF))) (K.mul h (K.mul (castZ K 2) SF)))
          (K.add (sub K (K.add E (K.mul h SF)) (K.add E' (K.mul (K.mul h nu) D)))
                 (sub K (K.add E (K.mul h SF)) (K.add E' (K.mul (K.mul h nu) D)))) := by lib_ring
  rw [e, ← id]
  rw [show sub K (K.add E' (K.mul (K.mul h nu) D)) (K.add E' (K.mul (K.mul h nu) D)) = K.zero by lib_ring]
  rw [show K.add (sub K (K.mul h (K.add (K.mul nu D) (K.mul (F.inv nu) FF))) (K.mul h (K.mul (castZ K 2) SF))) (K.add K.zero K.zero)
      = sub K (K.mul h (K.add (K.mul nu D) (K.mul (F.inv nu) FF))) (K.mul h (K.mul (castZ K 2) SF)) by lib_ring]
  exact ys

end ForcedEnstrophy

#print axioms ForcedEnstrophy.step_identity
#print axioms ForcedEnstrophy.lsum_le'
#print axioms ForcedEnstrophy.forced_step

ns/ForcedLeray.lean

lean_all/ns/ForcedLeray.lean

/-
  ns/ForcedLeray.lean — T37 cycle 120: the energy inequality with forcing, one Crank–Nicolson step.

  Lean 4 core plus the shared lib/OField (young), lib/Gronwall (le), NSChain (lsum), lib/RingTac.
  Modes i ∈ l, a′_i − a_i = h(−ν k_i s_i + N_i + f_i), s = a′ + a, Σ s N = 0 (energy-neutral nonlinearity),
  k_i = 1 + κ_i with κ_i ≥ 0 (lattice: |k|² ≥ 1), h, ν ≥ 0, ν ≠ 0:
    step_identity   E′ + hν Σ k s² = E + h Σ s f
    forced_step     2E′ + hν Σ k s² ≤ 2E + (h/ν) Σ f²           (Young with ε = ν)
-/
import lib.OField
import NSChain

namespace ForcedLeray
open DetChain NSChain LibRing LibCS LibGron LibOF

variable {R : Type} (K : CRing R) {I : Type}

theorem step_identity (a a' k Nn f : I → R) (h nu : R) (l : List I)
    (step : ∀ i, sub K (a' i) (a i) = K.mul h (K.add (K.add (K.neg (K.mul nu (K.mul (k i) (K.add (a' i) (a i))))) (Nn i)) (f i)))
    (neutral : lsum K (fun i => K.mul (K.add (a' i) (a i)) (Nn i)) l = K.zero) :
    K.add (lsum K (fun i => K.mul (a' i) (a' i)) l)
        (K.mul (K.mul h nu) (lsum K (fun i => K.mul (k i) (K.mul (K.add (a' i) (a i)) (K.add (a' i) (a i)))) l))
      = K.add (lsum K (fun i => K.mul (a i) (a i)) l)
          (K.mul h (lsum K (fun i => K.mul (K.add (a' i) (a i)) (f i)) l)) := by
  -- per mode: a′² + hν k s² = a² + h s N + h s f
  have pm : ∀ i, K.add (K.mul (a' i) (a' i)) (K.mul (K.mul h nu) (K.mul (k i) (K.mul (K.add (a' i) (a i)) (K.add (a' i) (a i)))))
      = K.add (K.add (K.mul (a i) (a i)) (K.mul h (K.mul (K.add (a' i) (a i)) (f i))))
          (K.mul h (K.mul (K.add (a' i) (a i)) (Nn i))) := by
    intro i
    have e : K.mul (a' i) (a' i) = K.add (K.mul (a i) (a i)) (K.mul (sub K (a' i) (a i)) (K.add (a' i) (a i))) := by lib_ring
    rw [e, step i]; lib_ring
  have sum_pm := lsum_ext K _ _ pm l
  rw [lsum_add, lsum_add, lsum_add, lsum_mul_left, lsum_mul_left, lsum_mul_left, neutral] at sum_pm
  rw [show K.add (lsum K (fun i => K.mul (a i) (a i)) l) (K.mul h (lsum K (fun i => K.mul (K.add (a' i) (a i)) (f i)) l))
      = K.add (K.add (lsum K (fun i => K.mul (a i) (a i)) l) (K.mul h (lsum K (fun i => K.mul (K.add (a' i) (a i)) (f i)) l)))
          (K.mul h K.zero) by lib_ring]
  exact sum_pm

theorem lsum_le' (P : Pos K) (u v : I → R) (h : ∀ i, le K P (u i) (v i)) :
    ∀ l : List I, le K P (lsum K u l) (lsum K v l)
  | [] => le_refl K P _
  | i :: l => le_add2 K P (h i) (lsum_le' P u v h l)

/-- **Forced step**: 2E′ + hνΣks² ≤ 2E + (h/ν)Σf². -/
theorem forced_step (P : Pos K) (F : OF K P) (a a' κ Nn f : I → R) (h nu : R) (l : List I)
    (hh : P.nn h) (hnu : P.nn nu) (hnu0 : nu ≠ K.zero) (hκ : ∀ i, P.nn (κ i))
    (step : ∀ i, sub K (a' i) (a i) = K.mul h (K.add (K.add (K.neg (K.mul nu (K.mul (K.add K.one (κ i)) (K.add (a' i) (a i))))) (Nn i)) (f i)))
    (neutral : lsum K (fun i => K.mul (K.add (a' i) (a i)) (Nn i)) l = K.zero) :
    le K P (K.add (K.add (lsum K (fun i => K.mul (a' i) (a' i)) l) (lsum K (fun i => K.mul (a' i) (a' i)) l))
              (K.mul (K.mul h nu) (lsum K (fun i => K.mul (K.add K.one (κ i)) (K.mul (K.add (a' i) (a i)) (K.add (a' i) (a i)))) l)))
      (K.add (K.add (lsum K (fun i => K.mul (a i) (a i)) l) (lsum K (fun i => K.mul (a i) (a i)) l))
             (K.mul (K.mul h (F.inv nu)) (lsum K (fun i => K.mul (f i) (f i)) l))) := by
  have id := step_identity K a a' (fun i => K.add K.one (κ i)) Nn f h nu l step neutral
  -- Young per mode: 2 s f ≤ ν (1+κ) s² + f²/ν
  have y : ∀ i, le K P (K.mul (castZ K 2) (K.mul (K.add (a' i) (a i)) (f i)))
      (K.add (K.mul nu (K.mul (K.add K.one (κ i)) (K.mul (K.add (a' i) (a i)) (K.add (a' i) (a i)))))
             (K.mul (K.mul (f i) (f i)) (F.inv nu))) := by
    intro i
    refine le_trans K P (young K P F nu (K.add (a' i) (a i)) (f i) hnu hnu0) (le_add K P _ ?_)
    unfold le
    rw [show sub K (K.mul nu (K.mul (K.add K.one (κ i)) (K.mul (K.add (a' i) (a i)) (K.add (a' i) (a i)))))
        (K.mul nu (K.mul (K.add (a' i) (a i)) (K.add (a' i) (a i))))
        = K.mul nu (K.mul (κ i) (K.mul (K.add (a' i) (a i)) (K.add (a' i) (a i)))) by lib_ring]
    exact P.nn_mul _ _ hnu (P.nn_mul _ _ (hκ i) (P.nn_sq _))
  have ys := le_mul_left K P hh (lsum_le' K P _ _ y l)
  unfold le at ys ⊢
  revert ys id
  rw [show lsum K (fun i => K.mul (castZ K 2) (K.mul (K.add (a' i) (a i)) (f i))) l
      = K.mul (castZ K 2) (lsum K (fun i => K.mul (K.add (a' i) (a i)) (f i)) l) from lsum_mul_left K _ _ l,
    show lsum K (fun i => K.add (K.mul nu (K.mul (K.add K.one (κ i)) (K.mul (K.add (a' i) (a i)) (K.add (a' i) (a i)))))
        (K.mul (K.mul (f i) (f i)) (F.inv nu))) l
      = K.add (K.mul nu (lsum K (fun i => K.mul (K.add K.one (κ i)) (K.mul (K.add (a' i) (a i)) (K.add (a' i) (a i)))) l))
          (K.mul (F.inv nu) (lsum K (fun i => K.mul (f i) (f i)) l)) by
      rw [lsum_add, lsum_mul_left]
      rw [lsum_ext K _ (fun i => K.mul (F.inv nu) (K.mul (f i) (f i))) (fun i => K.mul_comm _ _) l, lsum_mul_left]]
  generalize lsum K (fun i => K.mul (a' i) (a' i)) l = E'
  generalize lsum K (fun i => K.mul (a i) (a i)) l = E
  generalize lsum K (fun i => K.mul (K.add K.one (κ i)) (K.mul (K.add (a' i) (a i)) (K.add (a' i) (a i)))) l = D
  generalize lsum K (fun i => K.mul (K.add (a' i) (a i)) (f i)) l = SF
  generalize lsum K (fun i => K.mul (f i) (f i)) l = FF
  intro id ys
  have e : sub K (K.add (K.add E E) (K.mul (K.mul h (F.inv nu)) FF)) (K.add (K.add E' E') (K.mul (K.mul h nu) D))
      = K.add (sub K (K.mul h (K.add (K.mul nu D) (K.mul (F.inv nu) FF))) (K.mul h (K.mul (castZ K 2) SF)))
          (K.add (sub K (K.add E (K.mul h SF)) (K.add E' (K.mul (K.mul h nu) D)))
                 (sub K (K.add E (K.mul h SF)) (K.add E' (K.mul (K.mul h nu) D)))) := by
    lib_ring
  rw [e, ← id]
  rw [show sub K (K.add E' (K.mul (K.mul h nu) D)) (K.add E' (K.mul (K.mul h nu) D)) = K.zero by lib_ring]
  rw [show K.add (sub K (K.mul h (K.add (K.mul nu D) (K.mul (F.inv nu) FF))) (K.mul h (K.mul (castZ K 2) SF))) (K.add K.zero K.zero)
      = sub K (K.mul h (K.add (K.mul nu D) (K.mul (F.inv nu) FF))) (K.mul h (K.mul (castZ K 2) SF)) by lib_ring]
  exact ys

end ForcedLeray

#print axioms ForcedLeray.step_identity
#print axioms ForcedLeray.lsum_le'
#print axioms ForcedLeray.forced_step

ns/ForcedRun.lean

lean_all/ns/ForcedRun.lean

/-
  ns/ForcedRun.lean — T37 cycle 122: the forced Leray inequality along a whole run.

  Lean 4 core plus ns/ForcedLeray, lib/Gronwall (le arithmetic), lib/RingTac. If every step satisfies
      2E_{t+1} + hν D_t ≤ 2E_t + c F_t      (ForcedLeray.forced_step, c = h/ν)
  then for every T
      2E_T + hν Σ_{t<T} D_t ≤ 2E_0 + c Σ_{t<T} F_t.
  With F_t = ‖f‖² constant: energy grows at most linearly, and the total dissipation is bounded by the
  initial energy plus the work of the force — every truncation, every dimension.
-/
import ns.ForcedLeray

namespace ForcedRun
open DetChain NSChain LibRing LibCS LibGron

variable {R : Type} (K : CRing R) (P : Pos K)

def acc (d : Nat → R) : Nat → R
  | 0 => K.zero
  | n + 1 => K.add (acc d n) (d n)

theorem run (E D F : Nat → R) (hnu c : R)
    (step : ∀ t, le K P (K.add (K.add (E (t + 1)) (E (t + 1))) (K.mul hnu (D t)))
                        (K.add (K.add (E t) (E t)) (K.mul c (F t)))) :
    ∀ T, le K P (K.add (K.add (E T) (E T)) (K.mul hnu (acc K D T)))
                (K.add (K.add (E 0) (E 0)) (K.mul c (acc K F T)))
  | 0 => by
    unfold le
    rw [show sub K (K.add (K.add (E 0) (E 0)) (K.mul c (acc K F 0))) (K.add (K.add (E 0) (E 0)) (K.mul hnu (acc K D 0)))
        = K.zero by show sub K (K.add (K.add (E 0) (E 0)) (K.mul c K.zero)) (K.add (K.add (E 0) (E 0)) (K.mul hnu K.zero)) = K.zero; lib_ring]
    exact P.nn_zero
  | T + 1 => by
    have ih := run E D F hnu c step T
    have s := step T
    unfold le at ih s ⊢
    rw [show sub K (K.add (K.add (E 0) (E 0)) (K.mul c (acc K F (T + 1))))
          (K.add (K.add (E (T + 1)) (E (T + 1))) (K.mul hnu (acc K D (T + 1))))
        = K.add (sub K (K.add (K.add (E 0) (E 0)) (K.mul c (acc K F T))) (K.add (K.add (E T) (E T)) (K.mul hnu (acc K D T))))
            (sub K (K.add (K.add (E T) (E T)) (K.mul c (F T))) (K.add (K.add (E (T + 1)) (E (T + 1))) (K.mul hnu (D T)))) by
      show sub K (K.add (K.add (E 0) (E 0)) (K.mul c (K.add (acc K F T) (F T))))
          (K.add (K.add (E (T + 1)) (E (T + 1))) (K.mul hnu (K.add (acc K D T) (D T)))) = _
      lib_ring]
    exact P.nn_add _ _ ih s

end ForcedRun

#print axioms ForcedRun.run

ns/FourierGalerkin.lean

lean_all/ns/FourierGalerkin.lean

/-
  ns/FourierGalerkin.lean — T37 cycle 8: the Fourier–Galerkin nonlinearity of 3D Navier–Stokes is
  antisymmetric, for every finite set of modes (every truncation), in every commutative ring.

  Lean 4 core plus NSChain (CRing, lsum and its lemmas). Hand proofs.

  Modes p ∈ I (a list l), wave vectors k_p ∈ R³, velocity coefficients u_p ∈ R³ with k_p · u_p = 0
  (divergence free), a triad indicator δ(p,q,r) ∈ {0, 1} that is 1 only if k_q + k_r = −k_p and is
  symmetric in (q, r), and a constant ι (the imaginary unit for complex coefficients). The
  trilinear form of the truncated nonlinearity (u·∇)v tested against w is
      B(u, v, w) = Σ_p Σ_q Σ_r δ(p,q,r) · ι (u_p · k_q) · (v_q · w_r).
  Proved:
    lsum_swap        Σ_q Σ_r f q r = Σ_r Σ_q f q r
    triad_zero       δ(p,q,r) (u_p · (k_q + k_r)) = 0
    antisym          B(u, v, w) + B(u, w, v) = 0
    neutral          B(u, v, v) = 0 when 2x = 0 ⇒ x = 0 (characteristic ≠ 2: ℤ, ℚ, ℝ, ℂ)
  With NSChain.cn_energy / cn_global this makes every Fourier–Galerkin truncation of 3D NS, stepped by
  Crank–Nicolson, exactly energy-dissipating and globally bounded mode by mode (uniform in time and
  in the truncation) — the discrete form of Leray's energy bound. What it does not give is a bound
  on Σ |k|² |u_k|² uniform in the truncation (R1-gap).
-/
import NSChain

namespace FourierGalerkin
open DetChain NSChain

section Ring
variable {R : Type} (K : CRing R)

structure V3 (R : Type) where
  x : R
  y : R
  z : R

def dot3 (a b : V3 R) : R := K.add (K.add (K.mul a.x b.x) (K.mul a.y b.y)) (K.mul a.z b.z)
def add3 (a b : V3 R) : V3 R := ⟨K.add a.x b.x, K.add a.y b.y, K.add a.z b.z⟩
def neg3 (a : V3 R) : V3 R := ⟨K.neg a.x, K.neg a.y, K.neg a.z⟩

theorem dot3_add (a b c : V3 R) : dot3 K a (add3 K b c) = K.add (dot3 K a b) (dot3 K a c) := by
  unfold dot3 add3
  show K.add (K.add (K.mul a.x (K.add b.x c.x)) (K.mul a.y (K.add b.y c.y))) (K.mul a.z (K.add b.z c.z)) = _
  rw [K.distrib, K.distrib, K.distrib, add_mid4 K (K.mul a.x b.x)]
  exact add_mid4 K _ _ _ _

theorem dot3_neg (a b : V3 R) : dot3 K a (neg3 K b) = K.neg (dot3 K a b) := by
  unfold dot3 neg3
  show K.add (K.add (K.mul a.x (K.neg b.x)) (K.mul a.y (K.neg b.y))) (K.mul a.z (K.neg b.z)) = _
  rw [mul_neg' K, mul_neg' K, mul_neg' K]
  apply neg_unique K
  generalize K.mul a.x b.x = X
  generalize K.mul a.y b.y = Y
  generalize K.mul a.z b.z = Z
  rw [add_mid4 K (K.add (K.neg X) (K.neg Y)) (K.neg Z) (K.add X Y) Z,
    add_mid4 K (K.neg X) (K.neg Y) X Y, add_neg' K, add_neg' K, K.zero_add, add_neg' K, K.zero_add]

theorem dot3_comm (a b : V3 R) : dot3 K a b = dot3 K b a := by
  unfold dot3
  rw [K.mul_comm a.x, K.mul_comm a.y, K.mul_comm a.z]

theorem lsum_swap {I : Type} (f : I → I → R) : ∀ l m : List I,
    lsum K (fun q => lsum K (fun r => f q r) m) l = lsum K (fun r => lsum K (fun q => f q r) l) m
  | [], m => (lsum_zero K m).symm
  | q :: l, m => by
    show K.add (lsum K (fun r => f q r) m) (lsum K (fun q => lsum K (fun r => f q r) m) l) = _
    rw [lsum_swap f l m, ← lsum_add]
    rfl

variable {I : Type}

/-- The truncated trilinear form. -/
def B (l : List I) (δ : I → I → I → R) (ι : R) (k u v w : I → V3 R) : R :=
  lsum K (fun p => lsum K (fun q => lsum K (fun r =>
    K.mul (K.mul (δ p q r) (K.mul ι (dot3 K (u p) (k q)))) (dot3 K (v q) (w r))) l) l) l

theorem triad_zero (δ : I → I → I → R) (k u : I → V3 R)
    (hdiv : ∀ p, dot3 K (u p) (k p) = K.zero)
    (htri : ∀ p q r, δ p q r = K.zero ∨ (δ p q r = K.one ∧ add3 K (k q) (k r) = neg3 K (k p)))
    (p q r : I) : K.mul (δ p q r) (dot3 K (u p) (add3 K (k q) (k r))) = K.zero := by
  cases htri p q r with
  | inl h0 => rw [h0, zero_mul']
  | inr h1 => rw [h1.2, dot3_neg, hdiv p, h1.1, K.one_mul]; exact (neg_unique K _ _ (K.zero_add _)).symm

/-- **Antisymmetry**: B(u,v,w) + B(u,w,v) = 0. -/
theorem antisym (l : List I) (δ : I → I → I → R) (ι : R) (k u v w : I → V3 R)
    (hdiv : ∀ p, dot3 K (u p) (k p) = K.zero)
    (htri : ∀ p q r, δ p q r = K.zero ∨ (δ p q r = K.one ∧ add3 K (k q) (k r) = neg3 K (k p)))
    (hsym : ∀ p q r, δ p q r = δ p r q) :
    K.add (B K l δ ι k u v w) (B K l δ ι k u w v) = K.zero := by
  unfold B
  rw [← lsum_add]
  apply (lsum_ext K _ (fun _ => K.zero) _ l).trans (lsum_zero K l)
  intro p
  -- swap q, r in the second double sum
  rw [lsum_swap K (fun q r => K.mul (K.mul (δ p q r) (K.mul ι (dot3 K (u p) (k q)))) (dot3 K (w q) (v r))) l l,
    ← lsum_add]
  apply (lsum_ext K _ (fun _ => K.zero) _ l).trans (lsum_zero K l)
  intro q
  rw [← lsum_add]
  apply (lsum_ext K _ (fun _ => K.zero) _ l).trans (lsum_zero K l)
  intro r
  -- δpqr ι (u·k_q)(v_q·w_r) + δprq ι (u·k_r)(w_r·v_q) = δpqr ι (u·(k_q + k_r)) (v_q·w_r) = 0
  show K.add (K.mul (K.mul (δ p q r) (K.mul ι (dot3 K (u p) (k q)))) (dot3 K (v q) (w r)))
      (K.mul (K.mul (δ p r q) (K.mul ι (dot3 K (u p) (k r)))) (dot3 K (w r) (v q))) = K.zero
  rw [← hsym p q r, dot3_comm K (w r) (v q), ← right_distrib K, ← K.distrib, ← K.distrib,
    ← dot3_add]
  rw [K.mul_comm ι, ← K.mul_assoc, triad_zero K δ k u hdiv htri p q r, zero_mul', zero_mul']

/-- **Energy neutrality**: B(u,v,v) = 0 (characteristic ≠ 2). -/
theorem neutral (l : List I) (δ : I → I → I → R) (ι : R) (k u v : I → V3 R)
    (hdiv : ∀ p, dot3 K (u p) (k p) = K.zero)
    (htri : ∀ p q r, δ p q r = K.zero ∨ (δ p q r = K.one ∧ add3 K (k q) (k r) = neg3 K (k p)))
    (hsym : ∀ p q r, δ p q r = δ p r q) (h2 : ∀ x : R, K.add x x = K.zero → x = K.zero) :
    B K l δ ι k u v v = K.zero :=
  h2 _ (antisym K l δ ι k u v v hdiv htri hsym)

end Ring

end FourierGalerkin

#print axioms FourierGalerkin.dot3_add
#print axioms FourierGalerkin.dot3_neg
#print axioms FourierGalerkin.dot3_comm
#print axioms FourierGalerkin.lsum_swap
#print axioms FourierGalerkin.triad_zero
#print axioms FourierGalerkin.antisym
#print axioms FourierGalerkin.neutral

ns/FourierTriad.lean

lean_all/ns/FourierTriad.lean

/-
  ns/FourierTriad.lean — T37 cycle 273 (goal G15): the termwise triad bounds of TriadDom for Fourier–Galerkin triads.

  Lean 4 core plus ns/TriadDom, ns/TriadBound (triad_sq_bound), ns/LocalContAgmon (toCone, le_of_Le). One triad term
  t = (u_p·k_q)(v_q·w_r) with magnitudes A² = u·u, B² = (k·k)(v·v), C² = w·w satisfies t² ≤ (A·(B·C))²
  (triad_term), so cn_serrin_triad_terms applies with the vectors of the run (cn_serrin_fourier):
      Z_N (1 − S_N)² ≤ Z_0,  S_N = Σ_{t<N} h X_t²/(2ν),  X_t = Σ_p A_{t,p}
  given the triad expansion of the transfer, the magnitude identities, and the non-vanishing hypotheses.
-/
import ns.TriadDom
import ns.TriadBound
import ns.TriadDomEps

namespace FourierTriad
open DetChain NSChain LibRing LibCS LibGron LibOF SerrinSum EnstrophyStep CNSerrin TransferBound SignedSum
  LocalContAgmon
open CFKernel (V dot)

variable {R : Type} (K : CRing R) {I : Type}

/-- **One Fourier triad term against its magnitudes.** -/
theorem triad_term (P : Pos K) (u k v w : V R) (A B C : R) (hA : K.mul A A = dot K u u)
    (hB : K.mul B B = K.mul (dot K k k) (dot K v v)) (hC : K.mul C C = dot K w w) :
    le K P (K.mul (K.mul (dot K u k) (dot K v w)) (K.mul (dot K u k) (dot K v w)))
      (K.mul (K.mul A (K.mul B C)) (K.mul A (K.mul B C))) := by
  have s := le_of_Le K P _ _ (TriadBound.triad_sq_bound K (toCone K P) u k v w)
  rw [show K.mul (K.mul (dot K u k) (dot K v w)) (K.mul (dot K u k) (dot K v w))
      = K.mul (K.mul (dot K u k) (dot K u k)) (K.mul (dot K v w) (dot K v w)) by lib_ring,
    show K.mul (K.mul A (K.mul B C)) (K.mul A (K.mul B C)) = K.mul (K.mul A A) (K.mul (K.mul B B) (K.mul C C)) by lib_ring,
    hA, hB, hC,
    show K.mul (dot K u u) (K.mul (K.mul (dot K k k) (dot K v v)) (dot K w w))
      = K.mul (K.mul (dot K u u) (dot K k k)) (K.mul (dot K v v) (dot K w w)) by lib_ring]
  exact s

/-- **Prodi–Serrin for a CN Fourier–Galerkin run, triads written out.** -/
theorem cn_serrin_fourier (P : Pos K) (F : OF K P) (w k : I → R) (a Nn : Nat → I → R) (X ρ σ : Nat → R) (h ν : R)
    (l : List I) (A B C : Nat → I → R) (σf : Nat → I → I → I) (lt : Nat → List I)
    (U Kv Vv W : Nat → I → V R)
    (hw : ∀ i, P.nn (w i)) (hh : P.nn h) (hν : P.nn ν) (hν0 : K.add ν ν ≠ K.zero)
    (h4 : K.add (K.add ν ν) (K.add ν ν) ≠ K.zero)
    (step : ∀ t i, sub K (a (t + 1) i) (a t i)
      = K.mul h (K.add (K.neg (K.mul ν (K.mul (k i) (K.add (a (t + 1) i) (a t i))))) (Nn t i)))
    (hσ : ∀ t, K.mul (σ t) (σ t)
      = lsum K (fun i => K.mul (w i) (K.mul (k i) (K.mul (K.add (a (t + 1) i) (a t i)) (K.add (a (t + 1) i) (a t i))))) l)
    (hρ : ∀ t, K.mul (ρ t) (ρ t) = lsum K (fun i => K.mul (w i) (K.mul (K.add (a (t + 1) i) (a t i)) (K.add (a (t + 1) i) (a t i)))) l)
    (hA : ∀ t i, P.nn (A t i)) (hBn : ∀ t i, P.nn (B t i)) (hCn : ∀ t i, P.nn (C t i))
    (hX : ∀ t, X t = lsum K (A t) (lt t))
    (hB : ∀ t, K.mul (ρ t) (ρ t) = lsum K (fun q => K.mul (B t q) (B t q)) (lt t))
    (hC : ∀ t p, le K P (lsum K (fun q => K.mul (C t (σf t p q)) (C t (σf t p q))) (lt t)) (K.mul (σ t) (σ t)))
    (hXn : ∀ t, P.nn (X t)) (hρn : ∀ t, P.nn (ρ t)) (hσn : ∀ t, P.nn (σ t))
    (h0 : ∀ t, K.mul (K.mul (X t) (ρ t)) (σ t) ≠ K.zero)
    -- Fourier triads: transfer = Σ_p Σ_q (U_p·Kv_q)(Vv_q·W_{σ(p,q)}), magnitudes
    (hexp : ∀ t, lsum K (fun i => K.mul (w i) (K.mul (K.add (a (t + 1) i) (a t i)) (Nn t i))) l
      = lsum K (fun p => lsum K (fun q => K.mul (dot K (U t p) (Kv t q)) (dot K (Vv t q) (W t (σf t p q)))) (lt t)) (lt t))
    (hAA : ∀ t p, K.mul (A t p) (A t p) = dot K (U t p) (U t p))
    (hBB : ∀ t q, K.mul (B t q) (B t q) = K.mul (dot K (Kv t q) (Kv t q)) (dot K (Vv t q) (Vv t q)))
    (hCC : ∀ t r, K.mul (C t r) (C t r) = dot K (W t r) (W t r))
    (hA0 : ∀ t p, A t p ≠ K.zero) (hBC0 : ∀ t p q, K.mul (B t q) (C t (σf t p q)) ≠ K.zero)
    (hb1 : ∀ t, P.nn (sub K K.one (bk K P F h K.one ν X t)))
    (N : Nat) (hS : P.nn (sub K K.one (sumA K (bk K P F h K.one ν X) N))) :
    le K P (K.mul (Zw K w (a N) l) (K.mul (sub K K.one (sumA K (bk K P F h K.one ν X) N))
      (sub K K.one (sumA K (bk K P F h K.one ν X) N)))) (Zw K w (a 0) l) :=
  TriadDom.cn_serrin_triad_terms K P F w k a Nn X ρ σ h ν l A B C σf lt
    (fun t p q => K.mul (dot K (U t p) (Kv t q)) (dot K (Vv t q) (W t (σf t p q))))
    hw hh hν hν0 h4 step hσ hρ hA hBn hCn hX hB hC hXn hρn hσn h0 hexp hA0 hBC0
    (fun t p q => triad_term K P (U t p) (Kv t q) (Vv t q) (W t (σf t p q)) (A t p) (B t q) (C t (σf t p q))
      (hAA t p) (hBB t q) (hCC t _))
    hb1 N hS

/-- **The same with ε-shifted magnitudes: no non-vanishing hypothesis on A, B, C.** -/
theorem cn_serrin_fourier_eps (P : Pos K) (F : OF K P) (w k : I → R) (a Nn : Nat → I → R) (X ρ σ : Nat → R) (h ν : R)
    (l : List I) (A B C : Nat → I → R) (σf : Nat → I → I → I) (lt : Nat → List I)
    (U Kv Vv W : Nat → I → V R)
    (hw : ∀ i, P.nn (w i)) (hh : P.nn h) (hν : P.nn ν) (hν0 : K.add ν ν ≠ K.zero)
    (h4 : K.add (K.add ν ν) (K.add ν ν) ≠ K.zero)
    (step : ∀ t i, sub K (a (t + 1) i) (a t i)
      = K.mul h (K.add (K.neg (K.mul ν (K.mul (k i) (K.add (a (t + 1) i) (a t i))))) (Nn t i)))
    (hσ : ∀ t, K.mul (σ t) (σ t)
      = lsum K (fun i => K.mul (w i) (K.mul (k i) (K.mul (K.add (a (t + 1) i) (a t i)) (K.add (a (t + 1) i) (a t i))))) l)
    (hρ : ∀ t, K.mul (ρ t) (ρ t) = lsum K (fun i => K.mul (w i) (K.mul (K.add (a (t + 1) i) (a t i)) (K.add (a (t + 1) i) (a t i)))) l)
    (hA : ∀ t i, P.nn (A t i)) (hBn : ∀ t i, P.nn (B t i)) (hCn : ∀ t i, P.nn (C t i))
    (e : R) (he : P.nn e) (he0 : e ≠ K.zero)
    (hX : ∀ t, X t = lsum K (fun p => K.add (A t p) e) (lt t))
    (hB : ∀ t, K.mul (ρ t) (ρ t) = lsum K (fun q => K.mul (K.add (B t q) e) (K.add (B t q) e)) (lt t))
    (hC : ∀ t p, le K P (lsum K (fun q => K.mul (K.add (C t (σf t p q)) e) (K.add (C t (σf t p q)) e)) (lt t)) (K.mul (σ t) (σ t)))
    (hXn : ∀ t, P.nn (X t)) (hρn : ∀ t, P.nn (ρ t)) (hσn : ∀ t, P.nn (σ t))
    (h0 : ∀ t, K.mul (K.mul (X t) (ρ t)) (σ t) ≠ K.zero)
    -- Fourier triads: transfer = Σ_p Σ_q (U_p·Kv_q)(Vv_q·W_{σ(p,q)}), magnitudes
    (hexp : ∀ t, lsum K (fun i => K.mul (w i) (K.mul (K.add (a (t + 1) i) (a t i)) (Nn t i))) l
      = lsum K (fun p => lsum K (fun q => K.mul (dot K (U t p) (Kv t q)) (dot K (Vv t q) (W t (σf t p q)))) (lt t)) (lt t))
    (hAA : ∀ t p, K.mul (A t p) (A t p) = dot K (U t p) (U t p))
    (hBB : ∀ t q, K.mul (B t q) (B t q) = K.mul (dot K (Kv t q) (Kv t q)) (dot K (Vv t q) (Vv t q)))
    (hCC : ∀ t r, K.mul (C t r) (C t r) = dot K (W t r) (W t r))
    (hb1 : ∀ t, P.nn (sub K K.one (bk K P F h K.one ν X t)))
    (N : Nat) (hS : P.nn (sub K K.one (sumA K (bk K P F h K.one ν X) N))) :
    le K P (K.mul (Zw K w (a N) l) (K.mul (sub K K.one (sumA K (bk K P F h K.one ν X) N))
      (sub K K.one (sumA K (bk K P F h K.one ν X) N)))) (Zw K w (a 0) l) :=
  CNSerrinTriad.cn_serrin_triad K P F w k a Nn X ρ σ h ν l (fun t p => K.add (A t p) e) (fun t q => K.add (B t q) e)
    (fun t r => K.add (C t r) e) σf lt hw hh hν hν0 h4 step hσ hρ (fun t p => P.nn_add _ _ (hA t p) he) hX hB hC
    hXn hρn hσn h0
    (fun t => by
      rw [hexp t]
      exact TriadDomEps.triad_dom_eps K P F (A t) (B t) (C t) (σf t)
        (fun p q => K.mul (dot K (U t p) (Kv t q)) (dot K (Vv t q) (W t (σf t p q)))) (lt t) _ e rfl
        (hA t) (hBn t) (hCn t) he he0
        (fun p q => triad_term K P (U t p) (Kv t q) (Vv t q) (W t (σf t p q)) (A t p) (B t q) (C t (σf t p q))
          (hAA t p) (hBB t q) (hCC t _)))
    hb1 N hS

end FourierTriad

#print axioms FourierTriad.triad_term
#print axioms FourierTriad.cn_serrin_fourier
#print axioms FourierTriad.cn_serrin_fourier_eps

ns/Galerkin2D.lean

lean_all/ns/Galerkin2D.lean

/-
  ns/Galerkin2D.lean — T37 cycle 41: every 2D Fourier–Galerkin truncation conserves energy AND
  enstrophy under the nonlinearity (sum over all triads), in every commutative ring without 3-torsion.

  Lean 4 core plus ns/Enstrophy2D (J, n2, Triad, triad_energy, triad_enstrophy) and NSChain (lsum).

  Modes p ∈ l with wave vectors k_p ∈ R², stream-function coefficients ψ_p, a triad indicator δ ∈ {0,1}
  symmetric in its three arguments and 1 only on triads k_p + k_q + k_r = 0. With
  c(p,q,r) = J(k_q, k_r)(|k_r|² − |k_q|²), the energy and enstrophy rates of the nonlinearity are
      E(ψ) = Σ_{p,q,r} δ c(p,q,r) ψ_p ψ_q ψ_r ,   Z(ψ) = Σ_{p,q,r} δ |k_p|² c(p,q,r) ψ_p ψ_q ψ_r .
    triple_cycle     Σ_p Σ_q Σ_r g(q,r,p) = Σ_p Σ_q Σ_r g(p,q,r)
    energy_zero      E(ψ) = 0
    enstrophy_zero   Z(ψ) = 0
  (both after 3·(·) = 0 ⇒ (·) = 0). With Enstrophy2D.cn_weighted this gives, for every 2D truncation and
  every Crank–Nicolson step, non-increasing energy and enstrophy: the H¹ bound uniform in N and t.
-/
import ns.Enstrophy2D
import ns.FourierGalerkin

namespace Galerkin2D
open DetChain NSChain Enstrophy2D

section Ring
variable {R : Type} (K : CRing R) {I : Type}

theorem triple_cycle (g : I → I → I → R) (l : List I) :
    lsum K (fun p => lsum K (fun q => lsum K (fun r => g q r p) l) l) l
      = lsum K (fun p => lsum K (fun q => lsum K (fun r => g p q r) l) l) l := by
  rw [FourierGalerkin.lsum_swap K (fun p q => lsum K (fun r => g q r p) l) l l]
  apply lsum_ext
  intro q
  exact FourierGalerkin.lsum_swap K (fun p r => g q r p) l l

def c (k : I → V2 R) (_p q r : I) : R := K.mul (J K (k q) (k r)) (sub K (n2 K (k r)) (n2 K (k q)))

def f (k : I → V2 R) (δ : I → I → I → R) (ψ : I → R) (p q r : I) : R :=
  K.mul (K.mul (δ p q r) (c K k p q r)) (K.mul (ψ p) (K.mul (ψ q) (ψ r)))

def fZ (k : I → V2 R) (δ : I → I → I → R) (ψ : I → R) (p q r : I) : R :=
  K.mul (K.mul (δ p q r) (K.mul (n2 K (k p)) (c K k p q r))) (K.mul (ψ p) (K.mul (ψ q) (ψ r)))

def S3 (F : I → I → I → R) (l : List I) : R :=
  lsum K (fun p => lsum K (fun q => lsum K (fun r => F p q r) l) l) l

theorem sum3_add (F G H : I → I → I → R) (l : List I) :
    K.add (K.add (S3 K F l) (S3 K G l)) (S3 K H l)
      = S3 K (fun p q r => K.add (K.add (F p q r) (G p q r)) (H p q r)) l := by
  unfold S3
  rw [← lsum_add, ← lsum_add]
  apply lsum_ext; intro p
  rw [← lsum_add, ← lsum_add]
  apply lsum_ext; intro q
  rw [← lsum_add, ← lsum_add]

theorem psi_cycle (ψ : I → R) (p q r : I) :
    K.mul (ψ q) (K.mul (ψ r) (ψ p)) = K.mul (ψ p) (K.mul (ψ q) (ψ r)) := by
  rw [K.mul_comm (ψ r) (ψ p), ← K.mul_assoc, K.mul_comm (ψ q) (ψ p), K.mul_assoc]

/-- The three cyclic copies of the energy integrand add to zero, triad by triad. -/
theorem cyc_zero (k : I → V2 R) (δ : I → I → I → R) (ψ : I → R)
    (hsym1 : ∀ p q r, δ q r p = δ p q r) (hsym2 : ∀ p q r, δ r p q = δ p q r)
    (htri : ∀ p q r, δ p q r = K.zero ∨ (δ p q r = K.one ∧ Triad K (k p) (k q) (k r))) (p q r : I) :
    K.add (K.add (f K k δ ψ p q r) (f K k δ ψ q r p)) (f K k δ ψ r p q) = K.zero := by
  unfold f
  rw [hsym1 p q r, hsym2 p q r, psi_cycle K ψ q r p, psi_cycle K ψ p q r, ← right_distrib K, ← right_distrib K,
    ← K.distrib, ← K.distrib]
  cases htri p q r with
  | inl h0 => rw [h0, zero_mul', zero_mul']
  | inr h1 =>
    unfold c
    rw [triad_energy K (k p) (k q) (k r) h1.2, mul_zero K, zero_mul']

theorem cycZ_zero (k : I → V2 R) (δ : I → I → I → R) (ψ : I → R)
    (hsym1 : ∀ p q r, δ q r p = δ p q r) (hsym2 : ∀ p q r, δ r p q = δ p q r)
    (htri : ∀ p q r, δ p q r = K.zero ∨ (δ p q r = K.one ∧ Triad K (k p) (k q) (k r))) (p q r : I) :
    K.add (K.add (fZ K k δ ψ p q r) (fZ K k δ ψ q r p)) (fZ K k δ ψ r p q) = K.zero := by
  unfold fZ
  rw [hsym1 p q r, hsym2 p q r, psi_cycle K ψ q r p, psi_cycle K ψ p q r, ← right_distrib K, ← right_distrib K,
    ← K.distrib, ← K.distrib]
  cases htri p q r with
  | inl h0 => rw [h0, zero_mul', zero_mul']
  | inr h1 =>
    unfold c
    rw [triad_enstrophy K (k p) (k q) (k r) h1.2, mul_zero K, zero_mul']

/-- **Energy**: 3E = 0 ⇒ E = 0. -/
theorem energy_zero (k : I → V2 R) (δ : I → I → I → R) (ψ : I → R) (l : List I)
    (hsym1 : ∀ p q r, δ q r p = δ p q r) (hsym2 : ∀ p q r, δ r p q = δ p q r)
    (htri : ∀ p q r, δ p q r = K.zero ∨ (δ p q r = K.one ∧ Triad K (k p) (k q) (k r)))
    (h3 : ∀ x : R, K.add (K.add x x) x = K.zero → x = K.zero) :
    S3 K (f K k δ ψ) l = K.zero := by
  apply h3
  have e1 : S3 K (fun p q r => f K k δ ψ q r p) l = S3 K (f K k δ ψ) l := triple_cycle K _ l
  have e2 : S3 K (fun p q r => f K k δ ψ r p q) l = S3 K (f K k δ ψ) l := by
    rw [← e1]; exact triple_cycle K (fun p q r => f K k δ ψ q r p) l
  have t : K.add (K.add (S3 K (f K k δ ψ) l) (S3 K (f K k δ ψ) l)) (S3 K (f K k δ ψ) l)
      = K.add (K.add (S3 K (f K k δ ψ) l) (S3 K (fun p q r => f K k δ ψ q r p) l))
          (S3 K (fun p q r => f K k δ ψ r p q) l) := by rw [e1, e2]
  rw [t, sum3_add]
  unfold S3
  apply (lsum_ext K _ (fun _ => K.zero) _ l).trans (lsum_zero K l); intro p
  apply (lsum_ext K _ (fun _ => K.zero) _ l).trans (lsum_zero K l); intro q
  apply (lsum_ext K _ (fun _ => K.zero) _ l).trans (lsum_zero K l); intro r
  exact cyc_zero K k δ ψ hsym1 hsym2 htri p q r

/-- **Enstrophy**: 3Z = 0 ⇒ Z = 0. -/
theorem enstrophy_zero (k : I → V2 R) (δ : I → I → I → R) (ψ : I → R) (l : List I)
    (hsym1 : ∀ p q r, δ q r p = δ p q r) (hsym2 : ∀ p q r, δ r p q = δ p q r)
    (htri : ∀ p q r, δ p q r = K.zero ∨ (δ p q r = K.one ∧ Triad K (k p) (k q) (k r)))
    (h3 : ∀ x : R, K.add (K.add x x) x = K.zero → x = K.zero) :
    S3 K (fZ K k δ ψ) l = K.zero := by
  apply h3
  have e1 : S3 K (fun p q r => fZ K k δ ψ q r p) l = S3 K (fZ K k δ ψ) l := triple_cycle K _ l
  have e2 : S3 K (fun p q r => fZ K k δ ψ r p q) l = S3 K (fZ K k δ ψ) l := by
    rw [← e1]; exact triple_cycle K (fun p q r => fZ K k δ ψ q r p) l
  have t : K.add (K.add (S3 K (fZ K k δ ψ) l) (S3 K (fZ K k δ ψ) l)) (S3 K (fZ K k δ ψ) l)
      = K.add (K.add (S3 K (fZ K k δ ψ) l) (S3 K (fun p q r => fZ K k δ ψ q r p) l))
          (S3 K (fun p q r => fZ K k δ ψ r p q) l) := by rw [e1, e2]
  rw [t, sum3_add]
  unfold S3
  apply (lsum_ext K _ (fun _ => K.zero) _ l).trans (lsum_zero K l); intro p
  apply (lsum_ext K _ (fun _ => K.zero) _ l).trans (lsum_zero K l); intro q
  apply (lsum_ext K _ (fun _ => K.zero) _ l).trans (lsum_zero K l); intro r
  exact cycZ_zero K k δ ψ hsym1 hsym2 htri p q r

end Ring

end Galerkin2D

#print axioms Galerkin2D.triple_cycle
#print axioms Galerkin2D.sum3_add
#print axioms Galerkin2D.psi_cycle
#print axioms Galerkin2D.cyc_zero
#print axioms Galerkin2D.cycZ_zero
#print axioms Galerkin2D.energy_zero
#print axioms Galerkin2D.enstrophy_zero

ns/Galerkin2DCN.lean

lean_all/ns/Galerkin2DCN.lean

/-
  ns/Galerkin2DCN.lean — T37 cycle 53: every Crank–Nicolson step of every 2D Fourier–Galerkin
  truncation is unconditionally non-increasing in energy and in enstrophy.

  Lean 4 core plus ns/Galerkin2D (energy_zero, enstrophy_zero) and ns/Enstrophy2D (cn_weighted_le).
  Modes 0..N−1, wave vectors k_n ∈ R², CN step with the nonlinearity evaluated at s = a' + a:
      a'_n − a_n = h(−ν|k_n|² s_n + Nn_n),   Nn_n = Σ_{q,r} δ(n,q,r) c(n,q,r) s_q s_r
  (δ symmetric, supported on triads). Then for w ≡ 1 (energy) and w = |k|² (enstrophy):
      Σ w_n a'_n² ≤ Σ w_n a_n²      — no smallness, no time-step restriction (ν, h ≥ 0).
    sumN_lsum      sumN g N = lsum g [N−1, …, 0]
    cn2d_energy, cn2d_enstrophy
-/
import ns.Galerkin2D
import lib.RingTac

namespace Galerkin2DCN
open DetChain NSChain Enstrophy2D Galerkin2D

section Ring
variable {R : Type} (K : CRing R)

def lst : Nat → List Nat
  | 0 => []
  | N + 1 => N :: lst N

theorem sumN_lsum (g : Nat → R) : ∀ N, sumN K g N = lsum K g (lst N)
  | 0 => rfl
  | N + 1 => by
    show K.add (sumN K g N) (g N) = K.add (g N) (lsum K g (lst N))
    rw [sumN_lsum g N, K.add_comm]

def Nn (k : Nat → V2 R) (δ : Nat → Nat → Nat → R) (s : Nat → R) (N : Nat) (p : Nat) : R :=
  lsum K (fun q => lsum K (fun r => K.mul (K.mul (δ p q r) (c K k p q r)) (K.mul (s q) (s r))) (lst N)) (lst N)

/-- Energy part of the nonlinearity at s, as the Galerkin triple sum. -/
theorem neutral_energy (k : Nat → V2 R) (δ : Nat → Nat → Nat → R) (s : Nat → R) (N : Nat) :
    sumN K (fun n => K.mul K.one (K.mul (s n) (Nn K k δ s N n))) N = S3 K (f K k δ s) (lst N) := by
  rw [sumN_lsum]
  unfold S3
  apply lsum_ext; intro p
  rw [K.one_mul]
  unfold Nn
  rw [← lsum_mul_left]
  apply lsum_ext; intro q
  rw [← lsum_mul_left]
  apply lsum_ext; intro r
  unfold f
  rw [← K.mul_assoc, K.mul_comm (s p), K.mul_assoc]

/-- Enstrophy part (weight |k_p|²). -/
theorem neutral_enstrophy (k : Nat → V2 R) (δ : Nat → Nat → Nat → R) (s : Nat → R) (N : Nat) :
    sumN K (fun n => K.mul (n2 K (k n)) (K.mul (s n) (Nn K k δ s N n))) N = S3 K (fZ K k δ s) (lst N) := by
  rw [sumN_lsum]
  unfold S3
  apply lsum_ext; intro p
  unfold Nn
  rw [← lsum_mul_left, ← lsum_mul_left]
  apply lsum_ext; intro q
  rw [← lsum_mul_left, ← lsum_mul_left]
  apply lsum_ext; intro r
  unfold fZ
  generalize δ p q r = d
  generalize c K k p q r = cc
  generalize n2 K (k p) = m
  generalize K.mul (s q) (s r) = t
  generalize s p = x
  lib_ring

variable (P : Cone K)

theorem n2_nn (v : V2 R) : P.nn (n2 K v) := P.nn_add _ _ (P.nn_sq _) (P.nn_sq _)

theorem one_nn : P.nn K.one := by
  have e := P.nn_sq K.one
  rw [K.one_mul] at e
  exact e

/-- **2D CN energy**: Σ a'² ≤ Σ a² for every step. -/
theorem cn2d_energy (k : Nat → V2 R) (δ : Nat → Nat → Nat → R) (a a' : Nat → R) (h nu : R) (N : Nat)
    (hh : P.nn h) (hnu : P.nn nu)
    (hsym1 : ∀ p q r, δ q r p = δ p q r) (hsym2 : ∀ p q r, δ r p q = δ p q r)
    (htri : ∀ p q r, δ p q r = K.zero ∨ (δ p q r = K.one ∧ Triad K (k p) (k q) (k r)))
    (h3 : ∀ x : R, K.add (K.add x x) x = K.zero → x = K.zero)
    (step : ∀ n, K.add (a' n) (K.neg (a n)) = K.mul h (K.add (K.neg (K.mul nu (K.mul (n2 K (k n))
      (K.add (a' n) (a n))))) (Nn K k δ (fun m => K.add (a' m) (a m)) N n))) :
    Le K P (sumN K (fun n => K.mul K.one (K.mul (a' n) (a' n))) N)
      (sumN K (fun n => K.mul K.one (K.mul (a n) (a n))) N) :=
  cn_weighted_le K P (fun _ => K.one) a a' (fun n => n2 K (k n)) (Nn K k δ (fun m => K.add (a' m) (a m)) N)
    h nu N (fun _ => one_nn K P) (fun n => n2_nn K P (k n)) hh hnu step
    ((neutral_energy K k δ _ N).trans (energy_zero K k δ _ (lst N) hsym1 hsym2 htri h3))

/-- **2D CN enstrophy**: Σ |k|² a'² ≤ Σ |k|² a² for every step. -/
theorem cn2d_enstrophy (k : Nat → V2 R) (δ : Nat → Nat → Nat → R) (a a' : Nat → R) (h nu : R) (N : Nat)
    (hh : P.nn h) (hnu : P.nn nu)
    (hsym1 : ∀ p q r, δ q r p = δ p q r) (hsym2 : ∀ p q r, δ r p q = δ p q r)
    (htri : ∀ p q r, δ p q r = K.zero ∨ (δ p q r = K.one ∧ Triad K (k p) (k q) (k r)))
    (h3 : ∀ x : R, K.add (K.add x x) x = K.zero → x = K.zero)
    (step : ∀ n, K.add (a' n) (K.neg (a n)) = K.mul h (K.add (K.neg (K.mul nu (K.mul (n2 K (k n))
      (K.add (a' n) (a n))))) (Nn K k δ (fun m => K.add (a' m) (a m)) N n))) :
    Le K P (sumN K (fun n => K.mul (n2 K (k n)) (K.mul (a' n) (a' n))) N)
      (sumN K (fun n => K.mul (n2 K (k n)) (K.mul (a n) (a n))) N) :=
  cn_weighted_le K P (fun n => n2 K (k n)) a a' (fun n => n2 K (k n)) (Nn K k δ (fun m => K.add (a' m) (a m)) N)
    h nu N (fun n => n2_nn K P (k n)) (fun n => n2_nn K P (k n)) hh hnu step
    ((neutral_enstrophy K k δ _ N).trans (enstrophy_zero K k δ _ (lst N) hsym1 hsym2 htri h3))

end Ring

end Galerkin2DCN

#print axioms Galerkin2DCN.sumN_lsum
#print axioms Galerkin2DCN.neutral_energy
#print axioms Galerkin2DCN.neutral_enstrophy
#print axioms Galerkin2DCN.n2_nn
#print axioms Galerkin2DCN.one_nn
#print axioms Galerkin2DCN.cn2d_energy
#print axioms Galerkin2DCN.cn2d_enstrophy

ns/Galerkin3D.lean

lean_all/ns/Galerkin3D.lean

/-
  ns/Galerkin3D.lean — T37 cycle 893 (goal G592b): global energy bound for the semi-implicit scheme on 3D mode
  arrays, over any ordered ring without zero divisors (ℚ included). States a_t : Nat → V3 R (modes p < N), damping
  x_p ≥ 0, forcing F_t : Nat → V3 R with, for every t, mode p and component c < 3,
    a_{t+1,p,c} − a_{t,p,c} = −x_p (a_{t+1,p,c} + a_{t,p,c}) + h F_{t,p,c},
  energy neutrality Σ_p a_{t,p}·F_{t,p} = 0 and Σ_p (x_p + 2)|F_{t,p}|² ≤ C·E_t² (E_t = Σ_p |a_{t,p}|²).
  If 4T·h²·C·E_0 ≤ 1 then E_t ≤ 2E_0 for all t ≤ T.
  With F = P N (projected Galerkin force) the hypotheses are supplied by ProjStep, GalerkinForce.proj_neutral and
  LerayProj3.proj_sum_le.

  Lean 4 core plus ns/SemiGlobal (semi_global), ns/Flatten3 (comp, sum3, sumN_three), lib/RingTac, lib/Semi.
    go, go_at     flattening by recursion (no division): go a (3p + c) q = comp (a (q + p)) c
    go_cover      every index is 3p + c for some p, c < 3 (in the form needed)
    sum3_block    Σ over 3N of a flattened expression = Σ_p of the block sum
    global3       the statement
-/
import ns.SemiGlobal
import ns.Flatten3

namespace Galerkin3D
open DetChain NSChain DyadicSmallGlobal LibRing FourierGalerkin Flatten3 SemiGlobal

section Gen
variable {R : Type} (K : CRing R)

def go (a : Nat → V3 R) : Nat → Nat → R
  | 0, q => (a q).x
  | 1, q => (a q).y
  | 2, q => (a q).z
  | n + 3, q => go a n (q + 1)

theorem go_at (a : Nat → V3 R) (c : Nat) (hc : c < 3) : ∀ p q, go a (3 * p + c) q = comp K (a (q + p)) c
  | 0, q => by
    show go a (3 * 0 + c) q = comp K (a q) c
    rw [show 3 * 0 + c = c by lib_nat]
    cases c with
    | zero => rfl
    | succ c => cases c with
      | zero => rfl
      | succ c => cases c with
        | zero => rfl
        | succ c => exact absurd hc (Nat.not_lt_of_le (Nat.le_add_left 3 c))
  | p + 1, q => by
    rw [show 3 * (p + 1) + c = (3 * p + c) + 3 by lib_nat]
    show go a (3 * p + c) (q + 1) = _
    rw [go_at a c hc p (q + 1), show q + 1 + p = q + (p + 1) by lib_nat]

theorem go_cover : ∀ n q, ∃ p c, c < 3 ∧ ∀ a : Nat → V3 R, go a n q = comp K (a p) c
  | 0, q => ⟨q, 0, Nat.succ_pos 2, fun _ => rfl⟩
  | 1, q => ⟨q, 1, Nat.succ_lt_succ (Nat.succ_pos 1), fun _ => rfl⟩
  | 2, q => ⟨q, 2, Nat.succ_lt_succ (Nat.succ_lt_succ (Nat.succ_pos 0)), fun _ => rfl⟩
  | n + 3, q => by
    cases go_cover n (q + 1) with
    | intro p hp => cases hp with
      | intro c hc => exact ⟨p, c, hc.1, fun a => hc.2 a⟩

def fl (a : Nat → V3 R) (n : Nat) : R := go a n 0

theorem fl_at (a : Nat → V3 R) (p c : Nat) (hc : c < 3) : fl a (3 * p + c) = comp K (a p) c := by
  unfold fl; rw [go_at K a c hc p 0, Nat.zero_add]

theorem sum3_eq (f : Nat → Nat → R) : ∀ N, sum3 K f N = sumN K (fun p => K.add (K.add (f p 0) (f p 1)) (f p 2)) N
  | 0 => rfl
  | N + 1 => by
    show K.add (sum3 K f N) _ = K.add (sumN K _ N) _
    rw [sum3_eq f N]

/-- Σ over 3N of a flattened expression. -/
theorem sum_block (φ : Nat → R) (ψ : Nat → Nat → R) (h : ∀ p c, c < 3 → φ (3 * p + c) = ψ p c) (N : Nat) :
    sumN K φ (3 * N) = sumN K (fun p => K.add (K.add (ψ p 0) (ψ p 1)) (ψ p 2)) N := by
  rw [sumN_three K φ N, sum3_ext K _ ψ (fun p c hc => h p c hc) N, sum3_eq K ψ N]

def vx (x : Nat → R) (p : Nat) : V3 R := ⟨x p, x p, x p⟩

theorem comp_vx (x : Nat → R) (p c : Nat) (hc : c < 3) : comp K (vx x p) c = x p := by
  cases c with
  | zero => rfl
  | succ c => cases c with
    | zero => rfl
    | succ c => cases c with
      | zero => rfl
      | succ c => exact absurd hc (Nat.not_lt_of_le (Nat.le_add_left 3 c))

theorem energy_flat (a : Nat → V3 R) (N : Nat) :
    sumN K (fun n => K.mul (fl a n) (fl a n)) (3 * N) = sumN K (fun p => dot3 K (a p) (a p)) N :=
  (sum_block K _ (fun p c => K.mul (comp K (a p) c) (comp K (a p) c))
    (fun p c hc => by show K.mul (fl a (3 * p + c)) (fl a (3 * p + c)) = _; rw [fl_at K a p c hc]) N).trans
    (sumN_ext K _ _ (fun p => rfl) N)

theorem dot_flat (a b : Nat → V3 R) (N : Nat) :
    sumN K (fun n => K.mul (fl a n) (fl b n)) (3 * N) = sumN K (fun p => dot3 K (a p) (b p)) N :=
  (sum_block K _ (fun p c => K.mul (comp K (a p) c) (comp K (b p) c))
    (fun p c hc => by show K.mul (fl a (3 * p + c)) (fl b (3 * p + c)) = _; rw [fl_at K a p c hc, fl_at K b p c hc]) N).trans
    (sumN_ext K _ _ (fun p => rfl) N)

theorem weight_flat (x : Nat → R) (F : Nat → V3 R) (N : Nat) :
    sumN K (fun n => K.mul (K.add (fl (vx x) n) (K.add K.one K.one)) (K.mul (fl F n) (fl F n))) (3 * N)
      = sumN K (fun p => K.mul (K.add (x p) (K.add K.one K.one)) (dot3 K (F p) (F p))) N := by
  rw [sum_block K _ (fun p c => K.mul (K.add (x p) (K.add K.one K.one)) (K.mul (comp K (F p) c) (comp K (F p) c)))
    (fun p c hc => by
      show K.mul (K.add (fl (vx x) (3 * p + c)) (K.add K.one K.one)) (K.mul (fl F (3 * p + c)) (fl F (3 * p + c))) = _
      rw [fl_at K (vx x) p c hc, fl_at K F p c hc, comp_vx K x p c hc]) N]
  apply sumN_ext
  intro p
  show K.add (K.add (K.mul (K.add (x p) (K.add K.one K.one)) (K.mul (F p).x (F p).x))
      (K.mul (K.add (x p) (K.add K.one K.one)) (K.mul (F p).y (F p).y)))
      (K.mul (K.add (x p) (K.add K.one K.one)) (K.mul (F p).z (F p).z))
    = K.mul (K.add (x p) (K.add K.one K.one)) (K.add (K.add (K.mul (F p).x (F p).x) (K.mul (F p).y (F p).y)) (K.mul (F p).z (F p).z))
  generalize (F p).x = u; generalize (F p).y = v; generalize (F p).z = w; generalize x p = y
  lib_ring

variable (O : OrdR K)
include O

/-- **Global bound for the 3D semi-implicit Galerkin scheme.** -/
theorem global3 (h10 : K.one ≠ K.zero) (a : Nat → Nat → V3 R) (x : Nat → R) (F : Nat → Nat → V3 R)
    (h C : R) (N T : Nat) (hx : ∀ p, O.nn (x p)) (hC : O.nn C)
    (st : ∀ t p c, c < 3 → K.add (comp K (a (t + 1) p) c) (K.neg (comp K (a t p) c))
      = K.add (K.neg (K.mul (x p) (K.add (comp K (a (t + 1) p) c) (comp K (a t p) c)))) (K.mul h (comp K (F t p) c)))
    (neutral : ∀ t, sumN K (fun p => dot3 K (a t p) (F t p)) N = K.zero)
    (nb : ∀ t, Le K O.toCone (sumN K (fun p => K.mul (K.add (x p) (K.add K.one K.one)) (dot3 K (F t p) (F t p))) N)
      (K.mul C (K.mul (sumN K (fun p => dot3 K (a t p) (a t p)) N) (sumN K (fun p => dot3 K (a t p) (a t p)) N))))
    (small : Le K O.toCone (K.mul (castN K (4 * T)) (K.mul (K.mul (K.mul h h) C) (sumN K (fun p => dot3 K (a 0 p) (a 0 p)) N)))
      K.one) :
    ∀ t, t ≤ T → Le K O.toCone (sumN K (fun p => dot3 K (a t p) (a t p)) N)
      (K.add (sumN K (fun p => dot3 K (a 0 p) (a 0 p)) N) (sumN K (fun p => dot3 K (a 0 p) (a 0 p)) N)) := by
  intro t ht
  have g := semi_global K O h10 (fun t n => fl (a t) n) (fun n => fl (vx x) n) (fun t n => fl (F t) n) h C (3 * N) T
    (fun n => by
      cases go_cover K n 0 with
      | intro p hp => cases hp with
        | intro c hc =>
          show O.nn (go (vx x) n 0)
          rw [hc.2 (vx x), comp_vx K x p c hc.1]; exact hx p)
    hC
    (fun t n => by
      cases go_cover K n 0 with
      | intro p hp => cases hp with
        | intro c hc =>
          show K.add (go (a (t + 1)) n 0) (K.neg (go (a t) n 0))
            = K.add (K.neg (K.mul (go (vx x) n 0) (K.add (go (a (t + 1)) n 0) (go (a t) n 0)))) (K.mul h (go (F t) n 0))
          rw [hc.2 (a (t + 1)), hc.2 (a t), hc.2 (vx x), hc.2 (F t), comp_vx K x p c hc.1]
          exact st t p c hc.1)
    (fun t => by rw [dot_flat K (a t) (F t) N]; exact neutral t)
    (fun t => by rw [weight_flat K x (F t) N, energy_flat K (a t) N]; exact nb t)
    (by rw [energy_flat K (a 0) N]; exact small) t ht
  rw [energy_flat K (a t) N, energy_flat K (a 0) N] at g
  exact g

end Gen

end Galerkin3D

#print axioms Galerkin3D.go_at
#print axioms Galerkin3D.go_cover
#print axioms Galerkin3D.sum_block
#print axioms Galerkin3D.energy_flat
#print axioms Galerkin3D.dot_flat
#print axioms Galerkin3D.weight_flat
#print axioms Galerkin3D.global3

ns/Galerkin3DB.lean

lean_all/ns/Galerkin3DB.lean

/-
  ns/Galerkin3DB.lean — T37 cycle 910 (goal G608): the dissipation budget for the semi-implicit scheme on 3D mode
  arrays (the 3D form of SemiBudgetArr), over any ordered ring without zero divisors (ℚ included): under the
  hypotheses of Galerkin3D.global3,
    Σ_{t<T} Σ_{p<N} x_p |a_{t+1,p} + a_{t,p}|² ≤ 3E_0
  — the discrete ν∫‖∇u‖² dt ≤ (3/2)‖u_0‖² (x_p = νh|k_p|²/2), and (budget) its corollary for NS3DScheme.

  Lean 4 core plus ns/Galerkin3D (flattening), ns/SemiBudgetArr (semi_budget), ns/NS3DScheme.
-/
import ns.Galerkin3D
import ns.SemiBudgetArr
import ns.NS3DScheme

namespace Galerkin3DB
open DetChain NSChain DyadicSmallGlobal LibRing FourierGalerkin Flatten3 Galerkin3D SemiBudgetArr

section Gen
variable {R : Type} (K : CRing R)

theorem diss_flat (x : Nat → R) (a b : Nat → V3 R) (N : Nat) :
    sumN K (fun n => K.mul (fl (vx x) n) (K.mul (K.add (fl b n) (fl a n)) (K.add (fl b n) (fl a n)))) (3 * N)
      = sumN K (fun p => K.mul (x p) (dot3 K (add3 K (b p) (a p)) (add3 K (b p) (a p)))) N := by
  rw [sum_block K _ (fun p c => K.mul (x p) (K.mul (K.add (comp K (b p) c) (comp K (a p) c)) (K.add (comp K (b p) c) (comp K (a p) c))))
    (fun p c hc => by
      show K.mul (fl (vx x) (3 * p + c)) (K.mul (K.add (fl b (3 * p + c)) (fl a (3 * p + c))) (K.add (fl b (3 * p + c)) (fl a (3 * p + c)))) = _
      rw [fl_at K (vx x) p c hc, fl_at K b p c hc, fl_at K a p c hc, comp_vx K x p c hc]) N]
  apply sumN_ext
  intro p
  show K.add (K.add (K.mul (x p) (K.mul (K.add (b p).x (a p).x) (K.add (b p).x (a p).x)))
      (K.mul (x p) (K.mul (K.add (b p).y (a p).y) (K.add (b p).y (a p).y))))
      (K.mul (x p) (K.mul (K.add (b p).z (a p).z) (K.add (b p).z (a p).z)))
    = K.mul (x p) (K.add (K.add (K.mul (K.add (b p).x (a p).x) (K.add (b p).x (a p).x))
      (K.mul (K.add (b p).y (a p).y) (K.add (b p).y (a p).y))) (K.mul (K.add (b p).z (a p).z) (K.add (b p).z (a p).z)))
  generalize (b p).x = b1; generalize (b p).y = b2; generalize (b p).z = b3
  generalize (a p).x = a1; generalize (a p).y = a2; generalize (a p).z = a3; generalize x p = y
  lib_ring

variable (O : OrdR K)
include O

theorem budget3d (h10 : K.one ≠ K.zero) (a : Nat → Nat → V3 R) (x : Nat → R) (F : Nat → Nat → V3 R)
    (h C : R) (N T : Nat) (hx : ∀ p, O.nn (x p)) (hC : O.nn C)
    (st : ∀ t p c, c < 3 → K.add (comp K (a (t + 1) p) c) (K.neg (comp K (a t p) c))
      = K.add (K.neg (K.mul (x p) (K.add (comp K (a (t + 1) p) c) (comp K (a t p) c)))) (K.mul h (comp K (F t p) c)))
    (neutral : ∀ t, sumN K (fun p => dot3 K (a t p) (F t p)) N = K.zero)
    (nb : ∀ t, Le K O.toCone (sumN K (fun p => K.mul (K.add (x p) (K.add K.one K.one)) (dot3 K (F t p) (F t p))) N)
      (K.mul C (K.mul (sumN K (fun p => dot3 K (a t p) (a t p)) N) (sumN K (fun p => dot3 K (a t p) (a t p)) N))))
    (small : Le K O.toCone (K.mul (castN K (4 * T)) (K.mul (K.mul (K.mul h h) C) (sumN K (fun p => dot3 K (a 0 p) (a 0 p)) N)))
      K.one) :
    Le K O.toCone (sumN K (fun t => sumN K (fun p => K.mul (x p) (dot3 K (add3 K (a (t + 1) p) (a t p)) (add3 K (a (t + 1) p) (a t p)))) N) T)
      (K.add (K.add (sumN K (fun p => dot3 K (a 0 p) (a 0 p)) N) (sumN K (fun p => dot3 K (a 0 p) (a 0 p)) N))
        (sumN K (fun p => dot3 K (a 0 p) (a 0 p)) N)) := by
  have g := semi_budget K O h10 (fun t n => fl (a t) n) (fun n => fl (vx x) n) (fun t n => fl (F t) n) h C (3 * N) T
    (fun n => by
      cases go_cover K n 0 with
      | intro p hp => cases hp with
        | intro c hc =>
          show O.nn (go (vx x) n 0)
          rw [hc.2 (vx x), comp_vx K x p c hc.1]; exact hx p)
    hC
    (fun t n => by
      cases go_cover K n 0 with
      | intro p hp => cases hp with
        | intro c hc =>
          show K.add (go (a (t + 1)) n 0) (K.neg (go (a t) n 0))
            = K.add (K.neg (K.mul (go (vx x) n 0) (K.add (go (a (t + 1)) n 0) (go (a t) n 0)))) (K.mul h (go (F t) n 0))
          rw [hc.2 (a (t + 1)), hc.2 (a t), hc.2 (vx x), hc.2 (F t), comp_vx K x p c hc.1]
          exact st t p c hc.1)
    (fun t => by rw [dot_flat K (a t) (F t) N]; exact neutral t)
    (fun t => by rw [weight_flat K x (F t) N, energy_flat K (a t) N]; exact nb t)
    (by rw [energy_flat K (a 0) N]; exact small)
  rw [energy_flat K (a 0) N, sumN_ext K _ _ (fun t => diss_flat K x (a t) (a (t + 1)) N) T] at g
  exact g

end Gen

section Scheme
open LerayProj GalerkinForce ProjStep ForceSum NS3DScheme
variable {R : Type} (K : CRing R) (O : OrdR K)
include O

/-- **Dissipation budget for the projected 3D Galerkin scheme.** -/
theorem scheme_budget (inv : R → R) (mul_inv : ∀ y, y ≠ K.zero → K.mul y (inv y) = K.one) (h10 : K.one ≠ K.zero)
    (δ : Nat → Nat → Nat → R) (ι h : R) (k : Nat → V3 R) (x κ : Nat → R) (N T : Nat) (a0 : Nat → V3 R)
    (hx : ∀ p, O.nn (x p)) (hκ : ∀ p, O.nn (κ p)) (hκk : ∀ p, K.mul (dot3 K (k p) (k p)) (κ p) = K.one)
    (htri : ∀ p q r, δ p q r = K.zero ∨ (δ p q r = K.one ∧ add3 K (k q) (k r) = neg3 K (k p)))
    (hsym : ∀ p q r, δ p q r = δ p r q) (h2 : ∀ y : R, K.add y y = K.zero → y = K.zero)
    (h0 : ∀ p, dot3 K (k p) (a0 p) = K.zero)
    (small : Le K O.toCone (K.mul (castN K (4 * T)) (K.mul (K.mul (K.mul h h)
        (K.mul (sumN K (fun r => K.add (x r) (K.add K.one K.one)) N)
          (K.mul (K.mul (K.mul ι ι) (sumN K (fun q => dot3 K (k q) (k q)) N)) (castN K N))))
        (sumN K (fun p => dot3 K (a0 p) (a0 p)) N))) K.one) :
    Le K O.toCone (sumN K (fun t => sumN K (fun p => K.mul (x p) (dot3 K (add3 K (sch K inv δ ι h k x κ N a0 (t + 1) p)
        (sch K inv δ ι h k x κ N a0 t p)) (add3 K (sch K inv δ ι h k x κ N a0 (t + 1) p) (sch K inv δ ι h k x κ N a0 t p)))) N) T)
      (K.add (K.add (sumN K (fun p => dot3 K (a0 p) (a0 p)) N) (sumN K (fun p => dot3 K (a0 p) (a0 p)) N))
        (sumN K (fun p => dot3 K (a0 p) (a0 p)) N)) := by
  have hδ : ∀ p q r, δ p q r = K.zero ∨ δ p q r = K.one := fun p q r =>
    (htri p q r).elim Or.inl (fun h => Or.inr h.1)
  have hdiv := sch_div K inv δ ι h k x κ N a0 hκk h0
  have hw : ∀ r, O.nn (K.add (x r) (K.add K.one K.one)) := fun r =>
    O.nn_add _ _ (hx r) (O.nn_add _ _ (RatModel.nn_one' K O.toCone) (RatModel.nn_one' K O.toCone))
  let F : Nat → Nat → V3 R := fun t p =>
    proj K (κ p) (k p) (Nl K (rl N) δ ι k (sch K inv δ ι h k x κ N a0 t) (sch K inv δ ι h k x κ N a0 t) p)
  have hC : O.nn (K.mul (sumN K (fun r => K.add (x r) (K.add K.one K.one)) N)
      (K.mul (K.mul (K.mul ι ι) (sumN K (fun q => dot3 K (k q) (k q)) N)) (castN K N))) :=
    O.nn_mul _ _ (sumN_nn K O.toCone _ hw N) (O.nn_mul _ _ (O.nn_mul _ _ (O.nn_sq ι)
      (sumN_nn K O.toCone _ (fun q => ForceBound.dot3_nn K O.toCone (k q)) N)) (castN_nn K O N))
  refine budget3d K O h10 (sch K inv δ ι h k x κ N a0) x F h _ N T hx hC ?_ ?_ ?_ small
  · intro t p c hc
    have e := comb_step K O inv mul_inv h10 (x p) h (comp K (sch K inv δ ι h k x κ N a0 t p) c) (comp K (F t p) c) (hx p)
    have ec : comp K (sch K inv δ ι h k x κ N a0 (t + 1) p) c
        = K.mul (K.add (K.mul (K.add K.one (K.neg (x p))) (comp K (sch K inv δ ι h k x κ N a0 t p) c))
          (K.mul h (comp K (F t p) c))) (inv (K.add K.one (x p))) := by
      cases c with
      | zero => rfl
      | succ c => cases c with
        | zero => rfl
        | succ c => cases c with
          | zero => rfl
          | succ c => exact absurd hc (Nat.not_lt_of_le (Nat.le_add_left 3 c))
    rw [ec]; exact e
  · intro t
    rw [← lsum_rl K _ N]
    exact proj_neutral K (rl N) δ ι k (sch K inv δ ι h k x κ N a0 t) κ
      (fun p => by rw [dot3_comm K]; exact hdiv t p) htri hsym h2
  · intro t
    have fw := force_weighted K O.toCone δ ι k (sch K inv δ ι h k x κ N a0 t) κ (fun r => K.add (x r) (K.add K.one K.one)) N
      hδ hκ hκk hw
    generalize sumN K (fun p => dot3 K (sch K inv δ ι h k x κ N a0 t p) (sch K inv δ ι h k x κ N a0 t p)) N = E at fw ⊢
    cases fw with
    | intro d hd =>
      refine ⟨d, hd.1, hd.2.trans ?_⟩
      generalize sumN K (fun r => K.add (x r) (K.add K.one K.one)) N = W
      generalize sumN K (fun q => dot3 K (k q) (k q)) N = Kk
      generalize castN K N = n
      lib_ring

end Scheme

end Galerkin3DB

#print axioms Galerkin3DB.diss_flat
#print axioms Galerkin3DB.budget3d
#print axioms Galerkin3DB.scheme_budget

ns/GalerkinForce.lean

lean_all/ns/GalerkinForce.lean

/-
  ns/GalerkinForce.lean — T37 cycle 887 (goal G574b): the truncated nonlinearity as a vector field on the modes.
  Over any commutative ring, with c(p,q,r) = δ(p,q,r)·ι(u_p·k_q) and
    Nl_r = Σ_{p∈l} Σ_{q∈l} c(p,q,r) v_q        (a vector in R³ for every mode r),
  the trilinear form of FourierGalerkin is B(u, v, w) = Σ_{r∈l} w_r · Nl_r. Hence (neutral_force) for a
  divergence-free u and symmetric triads, Σ_r u_r · Nl_r(u, u) = 0 in characteristic ≠ 2: the forcing −Nl of the
  semi-implicit Galerkin step is energy-neutral, and stays so after the Leray projection (LerayProj2.proj_test).

  Lean 4 core plus ns/FourierGalerkin (B, antisym/neutral, lsum_swap), NSChain (lsum lemmas), lib/RingTac.
-/
import ns.FourierGalerkin
import ns.LerayProj2
import lib.RingTac

namespace GalerkinForce
open DetChain NSChain LibRing FourierGalerkin LerayProj LerayProj2

section Gen
variable {R : Type} (K : CRing R) {I : Type}

def coef (δ : I → I → I → R) (ι : R) (k u : I → V3 R) (p q r : I) : R :=
  K.mul (δ p q r) (K.mul ι (dot3 K (u p) (k q)))

/-- The nonlinear force at mode r. -/
def Nl (l : List I) (δ : I → I → I → R) (ι : R) (k u v : I → V3 R) (r : I) : V3 R :=
  ⟨lsum K (fun p => lsum K (fun q => K.mul (coef K δ ι k u p q r) (v q).x) l) l,
   lsum K (fun p => lsum K (fun q => K.mul (coef K δ ι k u p q r) (v q).y) l) l,
   lsum K (fun p => lsum K (fun q => K.mul (coef K δ ι k u p q r) (v q).z) l) l⟩

theorem dot_force (l : List I) (δ : I → I → I → R) (ι : R) (k u v : I → V3 R) (w : V3 R) (r : I) :
    dot3 K w (Nl K l δ ι k u v r)
      = lsum K (fun p => lsum K (fun q => K.mul (coef K δ ι k u p q r) (dot3 K (v q) w)) l) l := by
  show K.add (K.add (K.mul w.x (lsum K (fun p => lsum K (fun q => K.mul (coef K δ ι k u p q r) (v q).x) l) l))
      (K.mul w.y (lsum K (fun p => lsum K (fun q => K.mul (coef K δ ι k u p q r) (v q).y) l) l)))
      (K.mul w.z (lsum K (fun p => lsum K (fun q => K.mul (coef K δ ι k u p q r) (v q).z) l) l)) = _
  rw [← lsum_mul_left K w.x, ← lsum_mul_left K w.y, ← lsum_mul_left K w.z, ← lsum_add, ← lsum_add]
  apply lsum_ext
  intro p
  rw [← lsum_mul_left K w.x, ← lsum_mul_left K w.y, ← lsum_mul_left K w.z, ← lsum_add, ← lsum_add]
  apply lsum_ext
  intro q
  show K.add (K.add (K.mul w.x (K.mul (coef K δ ι k u p q r) (v q).x)) (K.mul w.y (K.mul (coef K δ ι k u p q r) (v q).y)))
      (K.mul w.z (K.mul (coef K δ ι k u p q r) (v q).z))
    = K.mul (coef K δ ι k u p q r) (K.add (K.add (K.mul (v q).x w.x) (K.mul (v q).y w.y)) (K.mul (v q).z w.z))
  generalize coef K δ ι k u p q r = c
  lib_ring

/-- B(u, v, w) = Σ_r w_r · Nl_r. -/
theorem B_eq (l : List I) (δ : I → I → I → R) (ι : R) (k u v w : I → V3 R) :
    B K l δ ι k u v w = lsum K (fun r => dot3 K (w r) (Nl K l δ ι k u v r)) l := by
  rw [lsum_ext K _ _ (fun r => dot_force K l δ ι k u v (w r) r) l]
  unfold B
  -- Σ_p Σ_q Σ_r X  →  Σ_p Σ_r Σ_q X  →  Σ_r Σ_p Σ_q X
  rw [lsum_ext K _ (fun p => lsum K (fun r => lsum K (fun q =>
      K.mul (K.mul (δ p q r) (K.mul ι (dot3 K (u p) (k q)))) (dot3 K (v q) (w r))) l) l)
    (fun p => lsum_swap K (fun q r => K.mul (K.mul (δ p q r) (K.mul ι (dot3 K (u p) (k q)))) (dot3 K (v q) (w r))) l l) l]
  rw [lsum_swap K (fun p r => lsum K (fun q =>
      K.mul (K.mul (δ p q r) (K.mul ι (dot3 K (u p) (k q)))) (dot3 K (v q) (w r))) l) l l]
  rfl

/-- **Neutral force**: Σ_r u_r · Nl_r(u, u) = 0 for divergence-free u, triads, char ≠ 2. -/
theorem neutral_force (l : List I) (δ : I → I → I → R) (ι : R) (k u : I → V3 R)
    (hdiv : ∀ p, dot3 K (u p) (k p) = K.zero)
    (htri : ∀ p q r, δ p q r = K.zero ∨ (δ p q r = K.one ∧ add3 K (k q) (k r) = neg3 K (k p)))
    (hsym : ∀ p q r, δ p q r = δ p r q) (h2 : ∀ x : R, K.add x x = K.zero → x = K.zero) :
    lsum K (fun r => dot3 K (u r) (Nl K l δ ι k u u r)) l = K.zero := by
  rw [← B_eq]; exact neutral K l δ ι k u u hdiv htri hsym h2

/-- **Projected neutral force**: with the Leray projection on every mode, Σ_r u_r · P_r(Nl_r(u, u)) = 0. -/
theorem proj_neutral (l : List I) (δ : I → I → I → R) (ι : R) (k u : I → V3 R) (κ : I → R)
    (hdiv : ∀ p, dot3 K (u p) (k p) = K.zero)
    (htri : ∀ p q r, δ p q r = K.zero ∨ (δ p q r = K.one ∧ add3 K (k q) (k r) = neg3 K (k p)))
    (hsym : ∀ p q r, δ p q r = δ p r q) (h2 : ∀ x : R, K.add x x = K.zero → x = K.zero) :
    lsum K (fun r => dot3 K (u r) (proj K (κ r) (k r) (Nl K l δ ι k u u r))) l = K.zero := by
  rw [lsum_ext K _ (fun r => dot3 K (u r) (Nl K l δ ι k u u r)) (fun r => by
    rw [proj_test K (κ r) (k r) (u r) _ (by rw [dot3_comm K]; exact hdiv r)]) l]
  exact neutral_force K l δ ι k u hdiv htri hsym h2

end Gen

end GalerkinForce

#print axioms GalerkinForce.dot_force
#print axioms GalerkinForce.B_eq
#print axioms GalerkinForce.neutral_force
#print axioms GalerkinForce.proj_neutral

ns/GeomDecay.lean

lean_all/ns/GeomDecay.lean

/-
  ns/GeomDecay.lean — T37 cycle 1826 (goal G858d): geometric decay of the dissipated quantity for small data, over an
  ordered field. Sequences Z, A ≥ 0 with Z_t ≤ A_t (w ≥ 1), Q_t = νZ_t + hν²A_t, m = ν + hν², the step and smallness
  of EnstDiss (so 2Q_{t+1} + hν²A_t ≤ 2Q_t, EnstDiss.diss_step):
    q_le_mA     Q_t ≤ m·A_t
    decay_step  2m·Q_{t+1} ≤ (2ν + hν²)·Q_t
    decay       (2m)ⁿ·Q_n ≤ (2ν + hν²)ⁿ·Q_0
  ratio (2ν + hν²)/(2ν + 2hν²) < 1 per step: Q decays geometrically, i.e. like e^{−νt/2} in continuous time t = nh
  for small h. Applied at the H² level (Z := A, A := B, H2Step.h2_step) this is the discrete exponential decay of
  the H² norm of small solutions, uniformly in the truncation.
  Lean 4 core plus ns/EnstDiss, ns/EnstGronwall (pwr).
-/
import ns.EnstDiss
import ns.EnstGronwall

namespace GeomDecay
open DetChain NSChain DyadicSmallGlobal LibRing DyadicLarge EnstGlobal EnstGronwall

section Gen
variable {R : Type} (K : CRing R) (O : OrdR K)

theorem q_le_mA (ν h Z A : R) (hν : O.nn ν) (hZA : Le K O.toCone Z A) :
    Le K O.toCone (K.add (K.mul ν Z) (K.mul (K.mul h (K.mul ν ν)) A)) (K.mul (K.add ν (K.mul h (K.mul ν ν))) A) :=
  le_trans' K O.toCone _ _ _ (le_add' K O.toCone _ _ _ _ (le_mul_nn K O.toCone ν _ _ hν hZA)
    (le_refl' K O.toCone (K.mul (K.mul h (K.mul ν ν)) A))) ⟨K.zero, O.nn_zero, by lib_ring⟩

theorem decay_step (ν h c : R) (Z A : Nat → R) (hν : O.nn ν) (hν0 : ν ≠ K.zero) (hh : O.nn h) (hc : O.nn c)
    (hZ : ∀ t, O.nn (Z t)) (hA : ∀ t, O.nn (A t)) (hZA : ∀ t, Le K O.toCone (Z t) (A t))
    (step : ∀ t, Le K O.toCone (Q K ν h Z A (t + 1)) (K.add (K.mul ν (Z t)) (K.mul (K.mul h c) (K.mul (A t) (Z t)))))
    (small : Le K O.toCone (K.mul (K.add c c) (Q K ν h Z A 0)) (K.mul ν (K.mul ν ν))) (t : Nat) :
    Le K O.toCone (K.mul (K.mul (K.add K.one K.one) (K.add ν (K.mul h (K.mul ν ν)))) (Q K ν h Z A (t + 1)))
      (K.mul (K.add (K.add ν ν) (K.mul h (K.mul ν ν))) (Q K ν h Z A t)) := by
  have hm : O.nn (K.add ν (K.mul h (K.mul ν ν))) := O.nn_add _ _ hν (O.nn_mul _ _ hh (O.nn_sq ν))
  have hk : O.nn (K.mul h (K.mul ν ν)) := O.nn_mul _ _ hh (O.nn_sq ν)
  have d := EnstDiss.diss_step K O ν h c Z A hν hν0 hh hc hZ hA step small t
  have d2 := le_mul_nn K O.toCone _ _ _ hm d
  have q : Le K O.toCone (K.mul (K.mul h (K.mul ν ν)) (Q K ν h Z A t)) (K.mul (K.mul h (K.mul ν ν)) (K.mul (K.add ν (K.mul h (K.mul ν ν))) (A t))) :=
    le_mul_nn K O.toCone (K.mul h (K.mul ν ν)) _ _ hk (q_le_mA K O ν h (Z t) (A t) hν (hZA t))
  have e1 : K.add (K.add (K.mul (K.mul (K.add K.one K.one) (K.add ν (K.mul h (K.mul ν ν)))) (Q K ν h Z A (t + 1))) (K.mul (K.mul h (K.mul ν ν)) (K.mul (K.add ν (K.mul h (K.mul ν ν))) (A t)))) K.zero
      = K.mul (K.add ν (K.mul h (K.mul ν ν))) (K.add (K.mul (K.add K.one K.one) (Q K ν h Z A (t + 1))) (K.mul (K.mul h (K.mul ν ν)) (A t))) := by
    generalize Q K ν h Z A (t + 1) = Q1
    lib_ring
  have s2 := le_trans' K O.toCone _ _ _ (le_add' K O.toCone _ _ _ _ (le_refl' K O.toCone (K.mul (K.mul (K.add K.one K.one) (K.add ν (K.mul h (K.mul ν ν)))) (Q K ν h Z A (t + 1)))) q)
    (le_trans' K O.toCone _ _ _ ⟨K.zero, O.nn_zero, e1⟩ d2)
  obtain ⟨e, he, hee⟩ := s2
  refine ⟨e, he, ?_⟩
  calc K.add (K.mul (K.mul (K.add K.one K.one) (K.add ν (K.mul h (K.mul ν ν)))) (Q K ν h Z A (t + 1))) e
      = K.add (K.add (K.add (K.mul (K.mul (K.add K.one K.one) (K.add ν (K.mul h (K.mul ν ν)))) (Q K ν h Z A (t + 1))) (K.mul (K.mul h (K.mul ν ν)) (Q K ν h Z A t))) e) (K.neg (K.mul (K.mul h (K.mul ν ν)) (Q K ν h Z A t))) := by
        generalize Q K ν h Z A (t + 1) = Q1
        generalize Q K ν h Z A t = Q0
        lib_ring
    _ = K.add (K.mul (K.add ν (K.mul h (K.mul ν ν))) (K.mul (K.add K.one K.one) (Q K ν h Z A t))) (K.neg (K.mul (K.mul h (K.mul ν ν)) (Q K ν h Z A t))) := by rw [hee]
    _ = K.mul (K.add (K.add ν ν) (K.mul h (K.mul ν ν))) (Q K ν h Z A t) := by
        generalize Q K ν h Z A t = Q0
        lib_ring

theorem decay (ν h c : R) (Z A : Nat → R) (hν : O.nn ν) (hν0 : ν ≠ K.zero) (hh : O.nn h) (hc : O.nn c)
    (hZ : ∀ t, O.nn (Z t)) (hA : ∀ t, O.nn (A t)) (hZA : ∀ t, Le K O.toCone (Z t) (A t))
    (step : ∀ t, Le K O.toCone (Q K ν h Z A (t + 1)) (K.add (K.mul ν (Z t)) (K.mul (K.mul h c) (K.mul (A t) (Z t)))))
    (small : Le K O.toCone (K.mul (K.add c c) (Q K ν h Z A 0)) (K.mul ν (K.mul ν ν))) :
    ∀ n, Le K O.toCone (K.mul (pwr K (K.mul (K.add K.one K.one) (K.add ν (K.mul h (K.mul ν ν)))) n) (Q K ν h Z A n))
      (K.mul (pwr K (K.add (K.add ν ν) (K.mul h (K.mul ν ν))) n) (Q K ν h Z A 0))
  | 0 => le_refl' K O.toCone _
  | n + 1 => by
    have ih := decay ν h c Z A hν hν0 hh hc hZ hA hZA step small n
    have ds := decay_step K O ν h c Z A hν hν0 hh hc hZ hA hZA step small n
    have hk : O.nn (K.mul h (K.mul ν ν)) := O.nn_mul _ _ hh (O.nn_sq ν)
    have nb : O.nn (K.add (K.add ν ν) (K.mul h (K.mul ν ν))) := O.nn_add _ _ (O.nn_add _ _ hν hν) hk
    have nm : O.nn (K.mul (K.add K.one K.one) (K.add ν (K.mul h (K.mul ν ν)))) :=
      O.nn_mul _ _ (O.nn_add _ _ (RatModel.nn_one' K O.toCone) (RatModel.nn_one' K O.toCone)) (O.nn_add _ _ hν hk)
    have a := le_mul_nn K O.toCone (pwr K (K.mul (K.add K.one K.one) (K.add ν (K.mul h (K.mul ν ν)))) n) _ _
      (pwr_nn K O _ nm n) ds
    have b := le_mul_nn K O.toCone (K.add (K.add ν ν) (K.mul h (K.mul ν ν))) _ _ nb ih
    refine le_trans' K O.toCone _ _ _ ⟨K.zero, O.nn_zero, ?_⟩ (le_trans' K O.toCone _ _ _ a
      (le_trans' K O.toCone _ _ _ ⟨K.zero, O.nn_zero, ?_⟩ (le_trans' K O.toCone _ _ _ b ⟨K.zero, O.nn_zero, ?_⟩)))
    · show K.add (K.mul (K.mul (K.mul (K.add K.one K.one) (K.add ν (K.mul h (K.mul ν ν)))) (pwr K (K.mul (K.add K.one K.one) (K.add ν (K.mul h (K.mul ν ν)))) n)) (Q K ν h Z A (n + 1))) K.zero
        = K.mul (pwr K (K.mul (K.add K.one K.one) (K.add ν (K.mul h (K.mul ν ν)))) n) (K.mul (K.mul (K.add K.one K.one) (K.add ν (K.mul h (K.mul ν ν)))) (Q K ν h Z A (n + 1)))
      generalize Q K ν h Z A (n + 1) = Q1
      generalize pwr K (K.mul (K.add K.one K.one) (K.add ν (K.mul h (K.mul ν ν)))) n = P
      lib_ring
    · generalize Q K ν h Z A n = Qn
      generalize pwr K (K.mul (K.add K.one K.one) (K.add ν (K.mul h (K.mul ν ν)))) n = P
      lib_ring
    · show K.add (K.mul (K.add (K.add ν ν) (K.mul h (K.mul ν ν))) (K.mul (pwr K (K.add (K.add ν ν) (K.mul h (K.mul ν ν))) n) (Q K ν h Z A 0))) K.zero
        = K.mul (K.mul (K.add (K.add ν ν) (K.mul h (K.mul ν ν))) (pwr K (K.add (K.add ν ν) (K.mul h (K.mul ν ν))) n)) (Q K ν h Z A 0)
      generalize Q K ν h Z A 0 = Q0
      generalize pwr K (K.add (K.add ν ν) (K.mul h (K.mul ν ν))) n = P
      lib_ring

end Gen
end GeomDecay

#print axioms GeomDecay.q_le_mA
#print axioms GeomDecay.decay_step
#print axioms GeomDecay.decay

ns/Global2DRun.lean

lean_all/ns/Global2DRun.lean

/-
  ns/Global2DRun.lean — T37 cycle 69: 2D global regularity of every Galerkin truncation, along a whole
  Crank–Nicolson run.

  Lean 4 core plus ns/Galerkin2DCN (Nn, neutral_enstrophy) , ns/Galerkin2D (enstrophy_zero),
  ns/Enstrophy2D (cn_weighted), ns/LerayDiscrete (tele_run). Modes 0..N−1 of a 2D truncation, any run
  a^0, a^1, … of CN steps with the nonlinearity at the midpoint:
      Z_T + Σ_{t<T} hν Σ_n |k_n|²·|k_n|²(a^{t+1}_n + a^t_n)² = Z_0,    Z_t = Σ_n |k_n|² (a^t_n)²
  so the enstrophy never exceeds its initial value and the total H²-dissipation is ≤ Z_0, for every N, T
  (no smallness, no step restriction): the discrete 2D global regularity theorem, in Lean.
-/
import ns.Galerkin2DCN
import ns.LerayDiscrete

namespace Global2DRun
open DetChain NSChain Enstrophy2D Galerkin2D Galerkin2DCN

section Ring
variable {R : Type} (K : CRing R)

def Zt (k : Nat → V2 R) (a : Nat → Nat → R) (N t : Nat) : R :=
  sumN K (fun n => K.mul (n2 K (k n)) (K.mul (a t n) (a t n))) N

def Dt (k : Nat → V2 R) (a : Nat → Nat → R) (h nu : R) (N t : Nat) : R :=
  K.mul h (K.mul nu (sumN K (fun n => K.mul (n2 K (k n)) (K.mul (n2 K (k n))
    (K.mul (K.add (a (t + 1) n) (a t n)) (K.add (a (t + 1) n) (a t n))))) N))

/-- **2D enstrophy equality along a run.** -/
theorem run_enstrophy (k : Nat → V2 R) (δ : Nat → Nat → Nat → R) (a : Nat → Nat → R) (h nu : R) (N : Nat)
    (hsym1 : ∀ p q r, δ q r p = δ p q r) (hsym2 : ∀ p q r, δ r p q = δ p q r)
    (htri : ∀ p q r, δ p q r = K.zero ∨ (δ p q r = K.one ∧ Triad K (k p) (k q) (k r)))
    (h3 : ∀ x : R, K.add (K.add x x) x = K.zero → x = K.zero)
    (step : ∀ t n, K.add (a (t + 1) n) (K.neg (a t n)) = K.mul h (K.add (K.neg (K.mul nu (K.mul (n2 K (k n))
      (K.add (a (t + 1) n) (a t n))))) (Nn K k δ (fun m => K.add (a (t + 1) m) (a t m)) N n))) (T : Nat) :
    K.add (Zt K k a N T) (sumN K (Dt K k a h nu N) T) = Zt K k a N 0 :=
  LerayDiscrete.tele_run K (Zt K k a N) (Dt K k a h nu N) (fun t =>
    cn_weighted K (fun n => n2 K (k n)) (a t) (a (t + 1)) (fun n => n2 K (k n))
      (Nn K k δ (fun m => K.add (a (t + 1) m) (a t m)) N) h nu N (step t)
      ((neutral_enstrophy K k δ _ N).trans (enstrophy_zero K k δ _ (lst N) hsym1 hsym2 htri h3))) T

/-- **2D global bound**: Z_T ≤ Z_0 and Σ dissipation ≤ Z_0. -/
theorem run_bound (P : Cone K) (k : Nat → V2 R) (δ : Nat → Nat → Nat → R) (a : Nat → Nat → R) (h nu : R) (N : Nat)
    (hh : P.nn h) (hnu : P.nn nu)
    (hsym1 : ∀ p q r, δ q r p = δ p q r) (hsym2 : ∀ p q r, δ r p q = δ p q r)
    (htri : ∀ p q r, δ p q r = K.zero ∨ (δ p q r = K.one ∧ Triad K (k p) (k q) (k r)))
    (h3 : ∀ x : R, K.add (K.add x x) x = K.zero → x = K.zero)
    (step : ∀ t n, K.add (a (t + 1) n) (K.neg (a t n)) = K.mul h (K.add (K.neg (K.mul nu (K.mul (n2 K (k n))
      (K.add (a (t + 1) n) (a t n))))) (Nn K k δ (fun m => K.add (a (t + 1) m) (a t m)) N n))) (T : Nat) :
    Le K P (Zt K k a N T) (Zt K k a N 0) ∧ Le K P (sumN K (Dt K k a h nu N) T) (Zt K k a N 0) := by
  have eq := run_enstrophy K k δ a h nu N hsym1 hsym2 htri h3 step T
  have nk : ∀ n, P.nn (n2 K (k n)) := fun n => n2_nn K P (k n)
  have hZ : P.nn (Zt K k a N T) := sumN_nn K P _ (fun n => P.nn_mul _ _ (nk n) (P.nn_sq _)) N
  have hD : P.nn (sumN K (Dt K k a h nu N) T) := sumN_nn K P _ (fun t =>
    P.nn_mul _ _ hh (P.nn_mul _ _ hnu (sumN_nn K P _ (fun n =>
      P.nn_mul _ _ (nk n) (P.nn_mul _ _ (nk n) (P.nn_sq _))) N))) T
  refine ⟨⟨_, hD, eq⟩, ⟨Zt K k a N T, hZ, ?_⟩⟩
  rw [K.add_comm]; exact eq

end Ring

end Global2DRun

#print axioms Global2DRun.run_enstrophy
#print axioms Global2DRun.run_bound

ns/GlobalEZ.lean

lean_all/ns/GlobalEZ.lean

/-
  ns/GlobalEZ.lean — T37 cycle 1343 (goal G800 — done): a global, truncation-uniform enstrophy bound for large
  enstrophy and small energy·enstrophy, over an ordered field. Combine
    EnstLocalTime.local_time   Q_t ≤ 2Q_0 for t ≤ n_L, where n_L·h·c₁Zb² ≤ Q_0, νZb = 2Q_0
    EventRegU.event_reg_uniform  a good time t + 1 ≤ n + 1 with Q_{t+1+s} ≤ Q_{t+1} for all s, once 2cW ≤ ν²(n+1)hν
  with the windows matched (n_L = n + 1):
    global_ez   Q_T ≤ 2Q_0 for every T.
  The two window conditions are compatible when 2cW/ν³ ≤ Q_0/(c₁Zb²) = ν²/(4c₁Q_0), i.e. 8c·c₁·W·Q_0 ≤ ν⁵ — a
  smallness condition on energy × enstrophy (Leray's product form) rather than on the enstrophy alone; the bound
  2Q_0 is independent of the truncation.

  Lean 4 core plus ns/EventRegU, ns/EnstLocalTime, ns/DyadicModelZ (le_dest).
-/
import ns.EventRegU
import ns.EnstLocalTime

namespace GlobalEZ
open DetChain NSChain DyadicSmallGlobal LibRing DyadicLarge FourierGalerkin GalerkinForce ForceBound ForceSob
  ModeCount CubeCount ForceUniform NormCompare EnstStep EnstGalerkin EnstGlobal CFLStep CFLGlobal EventSmall EventReg

section Gen
variable {R : Type} (K : CRing R) (O : OrdR K) {I : Type}
variable (inv : R → R) (mul_inv : ∀ y, y ≠ K.zero → K.mul y (inv y) = K.one) (h10 : K.one ≠ K.zero)

include mul_inv h10 in
theorem global_ez (l : List I) (kz : I → Mode) (hd : List.Pairwise (fun a b => kz a ≠ kz b) l)
    (h1 : ∀ p, 1 ≤ nrm (kz p)) (M : Nat) (hM : ∀ p, p ∈ l → nrm (kz p) ≤ M + 1) (Rc t0 : Nat) (hRM : Rc ≤ M)
    (ι h ν Es Zb : R) (hh : O.nn h) (hν : O.nn ν) (hh0 : h ≠ K.zero) (hν0 : ν ≠ K.zero) (hEs : O.nn Es) (hZb : O.nn Zb)
    (hstepM : Le K O.toCone (K.mul (K.mul h ν) (cM K M)) K.one)
    (a N : Nat → I → V3 R)
    (hs : ∀ t r, Step K h ν (wt K kz r) (a (t + 1) r) (a t r) (N t r))
    (hneut : ∀ t, lsum K (fun r => dot3 K (N t r) (a t r)) l = K.zero)
    (hP : ∀ t, Le K O.toCone (lsum K (fun r => dot3 K (N t r) (N t r)) l)
      (lsum K (fun r => dot3 K (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (a t) (a t) r)
        (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (a t) (a t) r)) l))
    (hcfl : Le K O.toCone (beta K M ι h ν Es) (K.mul h ν))
    (h0 : Le K O.toCone (K.add (lsum K (fun r => dot3 K (a 0 r) (a 0 r)) l)
      (K.mul (beta K M ι h ν Es) (lsum K (fun r => K.mul (wt K kz r) (dot3 K (a 0 r) (a 0 r))) l))) Es)
    (choice : Le K O.toCone (K.mul (EnstLocalGalerkin.cc K ι (EnstLocalGalerkin.C2 K inv Rc t0)) Zb) (K.mul ν ν))
    (hZ0 : K.mul ν Zb = K.add (Q K ν h (Zs K l kz a) (As K l kz a) 0) (Q K ν h (Zs K l kz a) (As K l kz a) 0))
    (n : Nat)
    (hn : Le K O.toCone (K.mul (castN K (n + 1)) (K.mul h (K.mul (EnstLocalGalerkin.cc K ι (EnstLocalGalerkin.C1 K inv Rc)) (K.mul Zb Zb))))
      (Q K ν h (Zs K l kz a) (As K l kz a) 0))
    (htime : Le K O.toCone
      (K.mul (K.add K.one K.one) (K.mul (K.mul (castN K 135) (K.mul ι ι))
        (K.add (lsum K (fun r => dot3 K (a 0 r) (a 0 r)) l)
          (K.mul (K.mul h ν) (lsum K (fun r => K.mul (wt K kz r) (dot3 K (a 0 r) (a 0 r))) l)))))
      (K.mul (K.mul ν ν) (K.mul (castN K (n + 1)) (K.mul h ν)))) (T : Nat) :
    Le K O.toCone (Q K ν h (Zs K l kz a) (As K l kz a) T)
      (K.add (Q K ν h (Zs K l kz a) (As K l kz a) 0) (Q K ν h (Zs K l kz a) (As K l kz a) 0)) := by
  have ⟨t, ht, _, hmono⟩ := EventRegU.event_reg_uniform K O inv mul_inv h10 l kz hd h1 M hM ι h ν Es hh hν hh0 hν0 hEs
    hstepM a N hs hneut hP hcfl h0 n htime
  have loc := fun T (hT : T ≤ n + 1) => EnstLocalTime.local_time K O inv mul_inv h10 l kz hd h1 M hM Rc t0 hRM ι h ν Zb
    hh hν hν0 hZb a N hP hs choice hZ0 (n + 1) hn T hT
  have ht1 : t + 1 ≤ n + 1 := Nat.succ_le_of_lt ht
  cases Nat.lt_or_ge T (t + 1) with
  | inl hlt => exact (loc T (Nat.le_trans (Nat.le_of_lt_succ hlt) (Nat.le_succ_of_le (Nat.le_of_lt_succ ht)))).1
  | inr hge =>
    have ⟨s, hsT⟩ := DyadicModelZ.le_dest hge
    rw [← hsT]
    exact le_trans' K O.toCone _ _ _ (hmono s) (loc (t + 1) ht1).1

end Gen

end GlobalEZ

#print axioms GlobalEZ.global_ez

ns/GlobalEZAll.lean

lean_all/ns/GlobalEZAll.lean

/-
  ns/GlobalEZAll.lean — T37 cycle 1379 (goal G813 — done): one statement for data with small energy × enstrophy,
  over an ordered field. Under the hypotheses of GlobalHolder.global_holder and νZb = 2Q_0:
    global_ez_all   Z_T ≤ Zb for every T, and ν³·Σ_r|a_{T+k,r} − a_{T,r}|² ≤ k·(2G) for all T, k,
  a truncation-uniform bound and a truncation-uniform time modulus (the Arzelà–Ascoli input). The GlobalEZ hypotheses
  are derived: c₂Zb ≤ ν² from 2c₂Zb ≤ ν², the window (n+1)hc₁Zb² ≤ Q_0 from the budget Q_0 + (n+1)hc₁Zb² ≤ νZb = 2Q_0,
  and the κ = c waiting condition from the κ = 2c one.

  Lean 4 core plus ns/GlobalEZZ, ns/GlobalHolder.
-/
import ns.GlobalEZZ
import ns.GlobalHolder

namespace GlobalEZAll
open DetChain NSChain DyadicSmallGlobal LibRing DyadicLarge FourierGalerkin GalerkinForce ForceBound ForceSob
  ModeCount CubeCount ForceUniform NormCompare EnstStep EnstGlobal EnstGalerkin ForceTwoScale EnstLocal
  EnstLocalGalerkin LatticeSum CFLStep CFLGlobal

section Gen
variable {R : Type} (K : CRing R) (O : OrdR K) {I : Type}
variable (inv : R → R) (mul_inv : ∀ y, y ≠ K.zero → K.mul y (inv y) = K.one) (h10 : K.one ≠ K.zero)

include mul_inv h10 in
theorem global_ez_all (l : List I) (kz : I → Mode) (hd : List.Pairwise (fun a b => kz a ≠ kz b) l)
    (h1 : ∀ p, 1 ≤ nrm (kz p)) (M : Nat) (hM : ∀ p, p ∈ l → nrm (kz p) ≤ M + 1) (Rc t0 : Nat) (hRM : Rc ≤ M)
    (ι h ν Es Zb : R) (hh : O.nn h) (hν : O.nn ν) (hh0 : h ≠ K.zero) (hν0 : ν ≠ K.zero) (hEs : O.nn Es) (hZb : O.nn Zb)
    (hstepM : Le K O.toCone (K.mul (K.mul h ν) (cM K M)) K.one)
    (a N : Nat → I → V3 R)
    (hs : ∀ t r, Step K h ν (wt K kz r) (a (t + 1) r) (a t r) (N t r))
    (hneut : ∀ t, lsum K (fun r => dot3 K (N t r) (a t r)) l = K.zero)
    (hP : ∀ t, Le K O.toCone (lsum K (fun r => dot3 K (N t r) (N t r)) l)
      (lsum K (fun r => dot3 K (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (a t) (a t) r)
        (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (a t) (a t) r)) l))
    (hcfl : Le K O.toCone (beta K M ι h ν Es) (K.mul h ν))
    (h0 : Le K O.toCone (K.add (lsum K (fun r => dot3 K (a 0 r) (a 0 r)) l)
      (K.mul (beta K M ι h ν Es) (lsum K (fun r => K.mul (wt K kz r) (dot3 K (a 0 r) (a 0 r))) l))) Es)
    (choice2 : Le K O.toCone (K.mul (K.add (cc K ι (C2 K inv Rc t0)) (cc K ι (C2 K inv Rc t0))) Zb) (K.mul ν ν))
    (hZ0 : K.mul ν Zb = K.add (Q K ν h (Zs K l kz a) (As K l kz a) 0) (Q K ν h (Zs K l kz a) (As K l kz a) 0))
    (n : Nat)
    (budget : Le K O.toCone (K.add (Q K ν h (Zs K l kz a) (As K l kz a) 0)
      (K.mul (castN K (n + 1)) (K.mul h (K.mul (cc K ι (C1 K inv Rc)) (K.mul Zb Zb))))) (K.mul ν Zb))
    (htime : Le K O.toCone
      (K.mul (K.add K.one K.one) (K.mul (K.add (K.mul (castN K 135) (K.mul ι ι)) (K.mul (castN K 135) (K.mul ι ι)))
        (K.add (lsum K (fun r => dot3 K (a 0 r) (a 0 r)) l)
          (K.mul (K.mul h ν) (lsum K (fun r => K.mul (wt K kz r) (dot3 K (a 0 r) (a 0 r))) l)))))
      (K.mul (K.mul ν ν) (K.mul (castN K (n + 1)) (K.mul h ν)))) :
    (∀ T, Le K O.toCone (Zs K l kz a T) Zb) ∧
    (∀ T k, Le K O.toCone
      (K.mul (K.mul ν (K.mul ν ν)) (lsum K (fun r => dot3 K (EnstDiff.sub3 K (a (T + k) r) (a T r)) (EnstDiff.sub3 K (a (T + k) r) (a T r))) l))
      (K.mul (castN K k) (K.mul (K.add K.one K.one)
        (K.add (K.mul ν (K.mul (K.mul (K.add (K.add K.one K.one) (K.add K.one K.one)) (K.mul h ν))
            (K.mul (K.add (K.mul (K.mul (castN K 135) (K.mul ι ι)) Zb) (K.mul ν ν)) Zb)))
          (K.mul (K.mul (K.add (K.add K.one K.one) (K.add K.one K.one)) h)
            (K.mul (K.add (K.mul ν (K.mul ν ν)) (K.mul ν (K.mul ν ν))) (K.mul ν Zb))))))) := by
  have two : O.nn (K.add K.one K.one) := O.nn_add _ _ (RatModel.nn_one' K O.toCone) (RatModel.nn_one' K O.toCone)
  have hiv : O.nn (iv K inv (Rc + 1)) := iv_nn K O inv mul_inv h10 Rc
  have hc2 : O.nn (cc K ι (C2 K inv Rc t0)) := O.nn_mul _ _ (O.nn_mul _ _ two (O.nn_sq ι))
    (O.nn_add _ _ (O.nn_mul _ _ (RatModel.nn_cast K O.toCone _) (O.nn_sq _))
      (O.nn_mul _ _ (RatModel.nn_cast K O.toCone _) hiv))
  have choice : Le K O.toCone (K.mul (cc K ι (C2 K inv Rc t0)) Zb) (K.mul ν ν) :=
    le_trans' K O.toCone _ _ _ ⟨K.mul (cc K ι (C2 K inv Rc t0)) Zb, O.nn_mul _ _ hc2 hZb, by lib_ring⟩ choice2
  -- (n+1)·D ≤ Q_0 from Q_0 + (n+1)D ≤ νZb = Q_0 + Q_0
  have hn : Le K O.toCone (K.mul (castN K (n + 1)) (K.mul h (K.mul (cc K ι (C1 K inv Rc)) (K.mul Zb Zb))))
      (Q K ν h (Zs K l kz a) (As K l kz a) 0) := by
    rw [hZ0] at budget
    cases budget with
    | intro d hdd =>
      refine ⟨d, hdd.1, ?_⟩
      have e := hdd.2
      generalize Q K ν h (Zs K l kz a) (As K l kz a) 0 = Q0 at e ⊢
      generalize K.mul (castN K (n + 1)) (K.mul h (K.mul (cc K ι (C1 K inv Rc)) (K.mul Zb Zb))) = X at e ⊢
      calc K.add X d = K.add (K.neg Q0) (K.add (K.add Q0 X) d) := by lib_ring
        _ = K.add (K.neg Q0) (K.add Q0 Q0) := by rw [e]
        _ = Q0 := by lib_ring
  have hc : O.nn (K.mul (castN K 135) (K.mul ι ι)) := O.nn_mul _ _ (RatModel.nn_cast K O.toCone 135) (O.nn_sq ι)
  have hW : O.nn (K.add (lsum K (fun r => dot3 K (a 0 r) (a 0 r)) l)
      (K.mul (K.mul h ν) (lsum K (fun r => K.mul (wt K kz r) (dot3 K (a 0 r) (a 0 r))) l))) :=
    O.nn_add _ _ (lsum_nn K O.toCone _ (fun r => dot3_nn K O.toCone _) l)
      (O.nn_mul _ _ (O.nn_mul _ _ hh hν) (lsum_nn K O.toCone _ (fun r => O.nn_mul _ _ (dot3_nn K O.toCone _) (dot3_nn K O.toCone _)) l))
  have htime1 : Le K O.toCone
      (K.mul (K.add K.one K.one) (K.mul (K.mul (castN K 135) (K.mul ι ι))
        (K.add (lsum K (fun r => dot3 K (a 0 r) (a 0 r)) l)
          (K.mul (K.mul h ν) (lsum K (fun r => K.mul (wt K kz r) (dot3 K (a 0 r) (a 0 r))) l)))))
      (K.mul (K.mul ν ν) (K.mul (castN K (n + 1)) (K.mul h ν))) :=
    le_trans' K O.toCone _ _ _ ⟨K.mul (K.add K.one K.one) (K.mul (K.mul (castN K 135) (K.mul ι ι))
        (K.add (lsum K (fun r => dot3 K (a 0 r) (a 0 r)) l)
          (K.mul (K.mul h ν) (lsum K (fun r => K.mul (wt K kz r) (dot3 K (a 0 r) (a 0 r))) l)))),
      O.nn_mul _ _ two (O.nn_mul _ _ hc hW), by lib_ring⟩ htime
  exact ⟨fun T => GlobalEZZ.global_ez_Z K O inv mul_inv h10 l kz hd h1 M hM Rc t0 hRM ι h ν Es Zb hh hν hh0 hν0 hEs hZb
      hstepM a N hs hneut hP hcfl h0 choice hZ0 n hn htime1 T,
    fun T k => GlobalHolder.global_holder K O inv mul_inv h10 l kz hd h1 M hM Rc t0 hRM ι h ν Es Zb hh hν hh0 hν0 hEs hZb
      hstepM a N hs hneut hP hcfl h0 choice2 n budget htime T k⟩

end Gen

end GlobalEZAll

#print axioms GlobalEZAll.global_ez_all

ns/GlobalEZD.lean

lean_all/ns/GlobalEZD.lean

/-
  ns/GlobalEZD.lean — T37 cycle 1351 (goal G802 — done): bounded for all time and eventually exponentially decaying,
  for data with small energy × enstrophy, over an ordered field. Under the hypotheses of GlobalEZ.global_ez and the
  waiting-time condition of EventDecay (4c·W ≤ ν²·T₂, T₂ = (n₂ + 1)hν):
    global_ez_decay   Q_T ≤ 2Q_0 for every T, and some t ≤ n₂ has 2c·Q_{t+1} ≤ ν³ and
                      (2 + 2hν)ˢ·Q_{t+1+s} ≤ (2 + hν)ˢ·Q_{t+1} for every s,
  all constants independent of the truncation.

  Lean 4 core plus ns/GlobalEZ, ns/EventDecay.
-/
import ns.GlobalEZ
import ns.EventDecay

namespace GlobalEZD
open DetChain NSChain DyadicSmallGlobal LibRing DyadicLarge FourierGalerkin GalerkinForce ForceBound ForceSob
  ModeCount CubeCount ForceUniform NormCompare EnstStep EnstGalerkin EnstGlobal CFLStep CFLGlobal EventSmall EventReg

section Gen
variable {R : Type} (K : CRing R) (O : OrdR K) {I : Type}
variable (inv : R → R) (mul_inv : ∀ y, y ≠ K.zero → K.mul y (inv y) = K.one) (h10 : K.one ≠ K.zero)

include mul_inv h10 in
theorem global_ez_decay (l : List I) (kz : I → Mode) (hd : List.Pairwise (fun a b => kz a ≠ kz b) l)
    (h1 : ∀ p, 1 ≤ nrm (kz p)) (M : Nat) (hM : ∀ p, p ∈ l → nrm (kz p) ≤ M + 1) (Rc t0 : Nat) (hRM : Rc ≤ M)
    (ι h ν Es Zb : R) (hh : O.nn h) (hν : O.nn ν) (hh0 : h ≠ K.zero) (hν0 : ν ≠ K.zero) (hEs : O.nn Es) (hZb : O.nn Zb)
    (hstepM : Le K O.toCone (K.mul (K.mul h ν) (cM K M)) K.one)
    (a N : Nat → I → V3 R)
    (hs : ∀ t r, Step K h ν (wt K kz r) (a (t + 1) r) (a t r) (N t r))
    (hneut : ∀ t, lsum K (fun r => dot3 K (N t r) (a t r)) l = K.zero)
    (hP : ∀ t, Le K O.toCone (lsum K (fun r => dot3 K (N t r) (N t r)) l)
      (lsum K (fun r => dot3 K (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (a t) (a t) r)
        (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (a t) (a t) r)) l))
    (hcfl : Le K O.toCone (beta K M ι h ν Es) (K.mul h ν))
    (h0 : Le K O.toCone (K.add (lsum K (fun r => dot3 K (a 0 r) (a 0 r)) l)
      (K.mul (beta K M ι h ν Es) (lsum K (fun r => K.mul (wt K kz r) (dot3 K (a 0 r) (a 0 r))) l))) Es)
    (choice : Le K O.toCone (K.mul (EnstLocalGalerkin.cc K ι (EnstLocalGalerkin.C2 K inv Rc t0)) Zb) (K.mul ν ν))
    (hZ0 : K.mul ν Zb = K.add (Q K ν h (Zs K l kz a) (As K l kz a) 0) (Q K ν h (Zs K l kz a) (As K l kz a) 0))
    (n n2 : Nat)
    (hn : Le K O.toCone (K.mul (castN K (n + 1)) (K.mul h (K.mul (EnstLocalGalerkin.cc K ι (EnstLocalGalerkin.C1 K inv Rc)) (K.mul Zb Zb))))
      (Q K ν h (Zs K l kz a) (As K l kz a) 0))
    (htime : Le K O.toCone
      (K.mul (K.add K.one K.one) (K.mul (K.mul (castN K 135) (K.mul ι ι))
        (K.add (lsum K (fun r => dot3 K (a 0 r) (a 0 r)) l)
          (K.mul (K.mul h ν) (lsum K (fun r => K.mul (wt K kz r) (dot3 K (a 0 r) (a 0 r))) l)))))
      (K.mul (K.mul ν ν) (K.mul (castN K (n + 1)) (K.mul h ν))))
    (htime2 : Le K O.toCone
      (K.mul (K.add K.one K.one) (K.mul (K.add (K.mul (castN K 135) (K.mul ι ι)) (K.mul (castN K 135) (K.mul ι ι)))
        (K.add (lsum K (fun r => dot3 K (a 0 r) (a 0 r)) l)
          (K.mul (K.mul h ν) (lsum K (fun r => K.mul (wt K kz r) (dot3 K (a 0 r) (a 0 r))) l)))))
      (K.mul (K.mul ν ν) (K.mul (castN K (n2 + 1)) (K.mul h ν)))) :
    (∀ T, Le K O.toCone (Q K ν h (Zs K l kz a) (As K l kz a) T)
      (K.add (Q K ν h (Zs K l kz a) (As K l kz a) 0) (Q K ν h (Zs K l kz a) (As K l kz a) 0))) ∧
    ∃ t, t < n2 + 1 ∧
      Le K O.toCone (K.mul (K.add (K.mul (castN K 135) (K.mul ι ι)) (K.mul (castN K 135) (K.mul ι ι)))
        (Q K ν h (Zs K l kz a) (As K l kz a) (t + 1))) (K.mul ν (K.mul ν ν)) ∧
      ∀ s, Le K O.toCone (K.mul (pw K (K.add (K.add K.one K.one) (K.add (K.mul h ν) (K.mul h ν))) s)
          (Q K ν h (Zs K l kz a) (As K l kz a) (t + 1 + s)))
        (K.mul (pw K (K.add (K.add K.one K.one) (K.mul h ν)) s) (Q K ν h (Zs K l kz a) (As K l kz a) (t + 1))) :=
  ⟨fun T => GlobalEZ.global_ez K O inv mul_inv h10 l kz hd h1 M hM Rc t0 hRM ι h ν Es Zb hh hν hh0 hν0 hEs hZb hstepM a N
      hs hneut hP hcfl h0 choice hZ0 n hn htime T,
    EventDecay.event_decay K O inv mul_inv h10 l kz hd h1 M hM ι h ν Es hh hν hh0 hν0 hEs hstepM a N hs hneut hP hcfl h0
      n2 htime2⟩

end Gen

end GlobalEZD

#print axioms GlobalEZD.global_ez_decay

ns/GlobalEZP.lean

lean_all/ns/GlobalEZP.lean

/-
  ns/GlobalEZP.lean — T37 cycle 1345 (goal G801 — done): GlobalEZ under a single product condition, over an ordered
  field. With X = c₁·Zb² (c₁ = cc(ι, C1), νZb = 2Q_0), c = 135ι², W = E_0 + hν·Z_0, choose the time
  τ = (n + 1)h with τ·X = Q_0 (τ = Q_0/X). If
      2c·W·X ≤ ν³·Q_0          (energy × enstrophy small: with Zb = 2Q_0/ν this is 8c·c₁·W·Q_0 ≤ ν⁵)
  then both window conditions of GlobalEZ hold and
    global_ez_prod   Q_T ≤ 2Q_0 for every T,
  uniformly in the truncation.

  Lean 4 core plus ns/GlobalEZ.
-/
import ns.GlobalEZ

namespace GlobalEZP
open DetChain NSChain DyadicSmallGlobal LibRing DyadicLarge FourierGalerkin GalerkinForce ForceBound ForceSob
  ModeCount CubeCount ForceUniform NormCompare EnstStep EnstGalerkin EnstGlobal CFLStep CFLGlobal EventSmall EventReg

section Gen
variable {R : Type} (K : CRing R) (O : OrdR K) {I : Type}
variable (inv : R → R) (mul_inv : ∀ y, y ≠ K.zero → K.mul y (inv y) = K.one) (h10 : K.one ≠ K.zero)

include mul_inv h10 in
theorem global_ez_prod (l : List I) (kz : I → Mode) (hd : List.Pairwise (fun a b => kz a ≠ kz b) l)
    (h1 : ∀ p, 1 ≤ nrm (kz p)) (M : Nat) (hM : ∀ p, p ∈ l → nrm (kz p) ≤ M + 1) (Rc t0 : Nat) (hRM : Rc ≤ M)
    (ι h ν Es Zb τ : R) (hh : O.nn h) (hν : O.nn ν) (hh0 : h ≠ K.zero) (hν0 : ν ≠ K.zero) (hEs : O.nn Es) (hZb : O.nn Zb)
    (hstepM : Le K O.toCone (K.mul (K.mul h ν) (cM K M)) K.one)
    (a N : Nat → I → V3 R)
    (hs : ∀ t r, Step K h ν (wt K kz r) (a (t + 1) r) (a t r) (N t r))
    (hneut : ∀ t, lsum K (fun r => dot3 K (N t r) (a t r)) l = K.zero)
    (hP : ∀ t, Le K O.toCone (lsum K (fun r => dot3 K (N t r) (N t r)) l)
      (lsum K (fun r => dot3 K (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (a t) (a t) r)
        (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (a t) (a t) r)) l))
    (hcfl : Le K O.toCone (beta K M ι h ν Es) (K.mul h ν))
    (h0 : Le K O.toCone (K.add (lsum K (fun r => dot3 K (a 0 r) (a 0 r)) l)
      (K.mul (beta K M ι h ν Es) (lsum K (fun r => K.mul (wt K kz r) (dot3 K (a 0 r) (a 0 r))) l))) Es)
    (choice : Le K O.toCone (K.mul (EnstLocalGalerkin.cc K ι (EnstLocalGalerkin.C2 K inv Rc t0)) Zb) (K.mul ν ν))
    (hZ0 : K.mul ν Zb = K.add (Q K ν h (Zs K l kz a) (As K l kz a) 0) (Q K ν h (Zs K l kz a) (As K l kz a) 0))
    (hX : O.nn (K.mul (EnstLocalGalerkin.cc K ι (EnstLocalGalerkin.C1 K inv Rc)) (K.mul Zb Zb)))
    (hX0 : K.mul (EnstLocalGalerkin.cc K ι (EnstLocalGalerkin.C1 K inv Rc)) (K.mul Zb Zb) ≠ K.zero)
    (n : Nat) (hτ : K.mul (castN K (n + 1)) h = τ)
    (hτX : K.mul τ (K.mul (EnstLocalGalerkin.cc K ι (EnstLocalGalerkin.C1 K inv Rc)) (K.mul Zb Zb)) = Q K ν h (Zs K l kz a) (As K l kz a) 0)
    (hprod : Le K O.toCone
      (K.mul (K.mul (K.add K.one K.one) (K.mul (K.mul (castN K 135) (K.mul ι ι))
        (K.add (lsum K (fun r => dot3 K (a 0 r) (a 0 r)) l)
          (K.mul (K.mul h ν) (lsum K (fun r => K.mul (wt K kz r) (dot3 K (a 0 r) (a 0 r))) l)))))
        (K.mul (EnstLocalGalerkin.cc K ι (EnstLocalGalerkin.C1 K inv Rc)) (K.mul Zb Zb)))
      (K.mul (K.mul ν (K.mul ν ν)) (Q K ν h (Zs K l kz a) (As K l kz a) 0))) (T : Nat) :
    Le K O.toCone (Q K ν h (Zs K l kz a) (As K l kz a) T)
      (K.add (Q K ν h (Zs K l kz a) (As K l kz a) 0) (Q K ν h (Zs K l kz a) (As K l kz a) 0)) := by
  generalize hXd : K.mul (EnstLocalGalerkin.cc K ι (EnstLocalGalerkin.C1 K inv Rc)) (K.mul Zb Zb) = X at hX hX0 hτX hprod
  have hn : Le K O.toCone (K.mul (castN K (n + 1)) (K.mul h X)) (Q K ν h (Zs K l kz a) (As K l kz a) 0) :=
    ⟨K.zero, O.nn_zero, by rw [← hτX, ← hτ]; lib_ring⟩
  have htime : Le K O.toCone
      (K.mul (K.add K.one K.one) (K.mul (K.mul (castN K 135) (K.mul ι ι))
        (K.add (lsum K (fun r => dot3 K (a 0 r) (a 0 r)) l)
          (K.mul (K.mul h ν) (lsum K (fun r => K.mul (wt K kz r) (dot3 K (a 0 r) (a 0 r))) l)))))
      (K.mul (K.mul ν ν) (K.mul (castN K (n + 1)) (K.mul h ν))) := by
    refine EnstGlobal.le_of_mul_le K O X _ _ hX hX0 ?_
    refine le_trans' K O.toCone _ _ _ ⟨K.zero, O.nn_zero, by lib_ring⟩ (le_trans' K O.toCone _ _ _ hprod ⟨K.zero, O.nn_zero, ?_⟩)
    rw [← hτX, ← hτ]; lib_ring
  rw [← hXd] at hn
  exact GlobalEZ.global_ez K O inv mul_inv h10 l kz hd h1 M hM Rc t0 hRM ι h ν Es Zb hh hν hh0 hν0 hEs hZb hstepM a N
    hs hneut hP hcfl h0 choice hZ0 n hn htime T

end Gen

end GlobalEZP

#print axioms GlobalEZP.global_ez_prod

ns/GlobalEZZ.lean

lean_all/ns/GlobalEZZ.lean

/-
  ns/GlobalEZZ.lean — T37 cycle 1377 (goal G812 — done): the pointwise enstrophy bound for data with small
  energy × enstrophy, over an ordered field. Under the hypotheses of GlobalEZ.global_ez (νZb = 2Q_0):
    global_ez_Z   Z_T ≤ Zb = 2Q_0/ν for every T,
  since νZ_T ≤ Q_T ≤ 2Q_0 = νZb; the bound is independent of the truncation.

  Lean 4 core plus ns/GlobalEZ.
-/
import ns.GlobalEZ

namespace GlobalEZZ
open DetChain NSChain DyadicSmallGlobal LibRing DyadicLarge FourierGalerkin GalerkinForce ForceBound ForceSob
  ModeCount CubeCount ForceUniform NormCompare EnstStep EnstGalerkin EnstGlobal CFLStep CFLGlobal EventSmall EventReg

section Gen
variable {R : Type} (K : CRing R) (O : OrdR K) {I : Type}
variable (inv : R → R) (mul_inv : ∀ y, y ≠ K.zero → K.mul y (inv y) = K.one) (h10 : K.one ≠ K.zero)

include mul_inv h10 in
theorem global_ez_Z (l : List I) (kz : I → Mode) (hd : List.Pairwise (fun a b => kz a ≠ kz b) l)
    (h1 : ∀ p, 1 ≤ nrm (kz p)) (M : Nat) (hM : ∀ p, p ∈ l → nrm (kz p) ≤ M + 1) (Rc t0 : Nat) (hRM : Rc ≤ M)
    (ι h ν Es Zb : R) (hh : O.nn h) (hν : O.nn ν) (hh0 : h ≠ K.zero) (hν0 : ν ≠ K.zero) (hEs : O.nn Es) (hZb : O.nn Zb)
    (hstepM : Le K O.toCone (K.mul (K.mul h ν) (cM K M)) K.one)
    (a N : Nat → I → V3 R)
    (hs : ∀ t r, Step K h ν (wt K kz r) (a (t + 1) r) (a t r) (N t r))
    (hneut : ∀ t, lsum K (fun r => dot3 K (N t r) (a t r)) l = K.zero)
    (hP : ∀ t, Le K O.toCone (lsum K (fun r => dot3 K (N t r) (N t r)) l)
      (lsum K (fun r => dot3 K (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (a t) (a t) r)
        (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (a t) (a t) r)) l))
    (hcfl : Le K O.toCone (beta K M ι h ν Es) (K.mul h ν))
    (h0 : Le K O.toCone (K.add (lsum K (fun r => dot3 K (a 0 r) (a 0 r)) l)
      (K.mul (beta K M ι h ν Es) (lsum K (fun r => K.mul (wt K kz r) (dot3 K (a 0 r) (a 0 r))) l))) Es)
    (choice : Le K O.toCone (K.mul (EnstLocalGalerkin.cc K ι (EnstLocalGalerkin.C2 K inv Rc t0)) Zb) (K.mul ν ν))
    (hZ0 : K.mul ν Zb = K.add (Q K ν h (Zs K l kz a) (As K l kz a) 0) (Q K ν h (Zs K l kz a) (As K l kz a) 0))
    (n : Nat)
    (hn : Le K O.toCone (K.mul (castN K (n + 1)) (K.mul h (K.mul (EnstLocalGalerkin.cc K ι (EnstLocalGalerkin.C1 K inv Rc)) (K.mul Zb Zb))))
      (Q K ν h (Zs K l kz a) (As K l kz a) 0))
    (htime : Le K O.toCone
      (K.mul (K.add K.one K.one) (K.mul (K.mul (castN K 135) (K.mul ι ι))
        (K.add (lsum K (fun r => dot3 K (a 0 r) (a 0 r)) l)
          (K.mul (K.mul h ν) (lsum K (fun r => K.mul (wt K kz r) (dot3 K (a 0 r) (a 0 r))) l)))))
      (K.mul (K.mul ν ν) (K.mul (castN K (n + 1)) (K.mul h ν)))) (T : Nat) :
    Le K O.toCone (Zs K l kz a T) Zb := by
  have g := GlobalEZ.global_ez K O inv mul_inv h10 l kz hd h1 M hM Rc t0 hRM ι h ν Es Zb hh hν hh0 hν0 hEs hZb hstepM a N
    hs hneut hP hcfl h0 choice hZ0 n hn htime T
  have hA : O.nn (As K l kz a T) := lsum_nn K O.toCone _ (fun r => O.nn_mul _ _ (O.nn_sq _) (dot3_nn K O.toCone _)) l
  have hz : Le K O.toCone (K.mul ν (Zs K l kz a T)) (Q K ν h (Zs K l kz a) (As K l kz a) T) :=
    ⟨K.mul (K.mul h (K.mul ν ν)) (As K l kz a T), O.nn_mul _ _ (O.nn_mul _ _ hh (O.nn_sq ν)) hA, rfl⟩
  rw [← hZ0] at g
  exact le_of_mul_le K O ν _ _ hν hν0 (le_trans' K O.toCone _ _ _ hz g)

end Gen

end GlobalEZZ

#print axioms GlobalEZZ.global_ez_Z

ns/GlobalHolder.lean

lean_all/ns/GlobalHolder.lean

/-
  ns/GlobalHolder.lean — T37 cycle 1361 (goal G805c — done): Hölder-1/2 equicontinuity for all time for data with
  small energy × enstrophy, over an ordered field. Hypotheses: cfl_global's (projected, energy-neutral force, CFL),
  h, ν ≠ 0, hν·c_M ≤ 1, the local window w = n + 1 (2c₂Zb ≤ ν², Q_0 + w·h·c₁Zb² ≤ νZb) and the good-time condition
  4c·W ≤ ν²·w·hν (c = 135ι², W = E_0 + hνZ_0). With L = 4hν(cZb + ν²)Zb, S₀ = 4h(ν³ + ν³)(νZb), G = νL + S₀:
    tri_sq        |x − z|² ≤ 2|x − y|² + 2|y − z|²  (summed over modes)
    cancel_add    T + a = T + b ⇒ a = b              (hand proof; Nat.add_left_cancel carries propext)
    global_holder ν³·Σ_r |a_{T+k,r} − a_{T,r}|² ≤ k·(2G) for all T, k,
  i.e. |a(T + τ) − a(T)|² ≤ 2Gτ/(hν³) with a constant independent of the truncation and of T: LocalHolder before the
  end of the window, HolderSmall after the good time t + 1 ≤ w, the triangle inequality across it.

  Lean 4 core plus ns/LocalHolder, ns/HolderSmall, ns/EventH2 (good_time).
-/
import ns.LocalHolder
import ns.HolderSmall
import ns.EventH2

namespace GlobalHolder
open DetChain NSChain DyadicSmallGlobal LibRing DyadicLarge FourierGalerkin GalerkinForce ForceBound ForceSob
  ModeCount CubeCount ForceUniform NormCompare EnstStep EnstGlobal EnstGalerkin ForceTwoScale EnstLocal
  EnstLocalGalerkin LatticeSum CFLStep CFLGlobal

section Gen
variable {R : Type} (K : CRing R) (O : OrdR K) {I : Type}
variable (inv : R → R) (mul_inv : ∀ y, y ≠ K.zero → K.mul y (inv y) = K.one) (h10 : K.one ≠ K.zero)

theorem tri3 (x y z : V3 R) : Le K O.toCone (dot3 K (EnstDiff.sub3 K x z) (EnstDiff.sub3 K x z))
    (K.add (K.mul (K.add K.one K.one) (dot3 K (EnstDiff.sub3 K x y) (EnstDiff.sub3 K x y)))
      (K.mul (K.add K.one K.one) (dot3 K (EnstDiff.sub3 K y z) (EnstDiff.sub3 K y z)))) := by
  refine ⟨dot3 K (EnstDiff.sub3 K (EnstDiff.sub3 K x y) (EnstDiff.sub3 K y z)) (EnstDiff.sub3 K (EnstDiff.sub3 K x y) (EnstDiff.sub3 K y z)),
    dot3_nn K O.toCone _, ?_⟩
  show K.add (K.add (K.add (K.mul (K.add x.x (K.neg z.x)) (K.add x.x (K.neg z.x))) (K.mul (K.add x.y (K.neg z.y)) (K.add x.y (K.neg z.y)))) (K.mul (K.add x.z (K.neg z.z)) (K.add x.z (K.neg z.z)))) (K.add (K.add (K.mul (K.add (K.add x.x (K.neg y.x)) (K.neg (K.add y.x (K.neg z.x)))) (K.add (K.add x.x (K.neg y.x)) (K.neg (K.add y.x (K.neg z.x))))) (K.mul (K.add (K.add x.y (K.neg y.y)) (K.neg (K.add y  -- (+652 chars; full line in the Zenodo source)
  lib_ring

theorem tri_sq (l : List I) (x y z : I → V3 R) :
    Le K O.toCone (lsum K (fun r => dot3 K (EnstDiff.sub3 K (x r) (z r)) (EnstDiff.sub3 K (x r) (z r))) l)
      (K.add (K.mul (K.add K.one K.one) (lsum K (fun r => dot3 K (EnstDiff.sub3 K (x r) (y r)) (EnstDiff.sub3 K (x r) (y r))) l))
        (K.mul (K.add K.one K.one) (lsum K (fun r => dot3 K (EnstDiff.sub3 K (y r) (z r)) (EnstDiff.sub3 K (y r) (z r))) l))) := by
  have t := lsum_le K O.toCone _ _ (fun r => tri3 K O (x r) (y r) (z r)) l
  rw [lsum_add, lsum_mul_left, lsum_mul_left] at t
  exact t

theorem cancel_add : ∀ T a b : Nat, T + a = T + b → a = b
  | 0, a, b, h => by rw [Nat.zero_add, Nat.zero_add] at h; exact h
  | T + 1, a, b, h => by
    rw [Nat.succ_add, Nat.succ_add] at h
    exact cancel_add T a b (Nat.succ.inj h)

include mul_inv h10 in
theorem global_holder (l : List I) (kz : I → Mode) (hd : List.Pairwise (fun a b => kz a ≠ kz b) l)
    (h1 : ∀ p, 1 ≤ nrm (kz p)) (M : Nat) (hM : ∀ p, p ∈ l → nrm (kz p) ≤ M + 1) (Rc t0 : Nat) (hRM : Rc ≤ M)
    (ι h ν Es Zb : R) (hh : O.nn h) (hν : O.nn ν) (hh0 : h ≠ K.zero) (hν0 : ν ≠ K.zero) (hEs : O.nn Es) (hZb : O.nn Zb)
    (hstepM : Le K O.toCone (K.mul (K.mul h ν) (cM K M)) K.one)
    (a N : Nat → I → V3 R)
    (hs : ∀ t r, Step K h ν (wt K kz r) (a (t + 1) r) (a t r) (N t r))
    (hneut : ∀ t, lsum K (fun r => dot3 K (N t r) (a t r)) l = K.zero)
    (hP : ∀ t, Le K O.toCone (lsum K (fun r => dot3 K (N t r) (N t r)) l)
      (lsum K (fun r => dot3 K (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (a t) (a t) r)
        (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (a t) (a t) r)) l))
    (hcfl : Le K O.toCone (beta K M ι h ν Es) (K.mul h ν))
    (h0 : Le K O.toCone (K.add (lsum K (fun r => dot3 K (a 0 r) (a 0 r)) l)
      (K.mul (beta K M ι h ν Es) (lsum K (fun r => K.mul (wt K kz r) (dot3 K (a 0 r) (a 0 r))) l))) Es)
    (choice2 : Le K O.toCone (K.mul (K.add (cc K ι (C2 K inv Rc t0)) (cc K ι (C2 K inv Rc t0))) Zb) (K.mul ν ν)) (n : Nat)
    (budget : Le K O.toCone (K.add (Q K ν h (Zs K l kz a) (As K l kz a) 0)
      (K.mul (castN K (n + 1)) (K.mul h (K.mul (cc K ι (C1 K inv Rc)) (K.mul Zb Zb))))) (K.mul ν Zb))
    (htime : Le K O.toCone
      (K.mul (K.add K.one K.one) (K.mul (K.add (K.mul (castN K 135) (K.mul ι ι)) (K.mul (castN K 135) (K.mul ι ι)))
        (K.add (lsum K (fun r => dot3 K (a 0 r) (a 0 r)) l)
          (K.mul (K.mul h ν) (lsum K (fun r => K.mul (wt K kz r) (dot3 K (a 0 r) (a 0 r))) l)))))
      (K.mul (K.mul ν ν) (K.mul (castN K (n + 1)) (K.mul h ν)))) (T k : Nat) :
    Le K O.toCone
      (K.mul (K.mul ν (K.mul ν ν)) (lsum K (fun r => dot3 K (EnstDiff.sub3 K (a (T + k) r) (a T r)) (EnstDiff.sub3 K (a (T + k) r) (a T r))) l))
      (K.mul (castN K k) (K.mul (K.add K.one K.one)
        (K.add (K.mul ν (K.mul (K.mul (K.add (K.add K.one K.one) (K.add K.one K.one)) (K.mul h ν))
            (K.mul (K.add (K.mul (K.mul (castN K 135) (K.mul ι ι)) Zb) (K.mul ν ν)) Zb)))
          (K.mul (K.mul (K.add (K.add K.one K.one) (K.add K.one K.one)) h)
            (K.mul (K.add (K.mul ν (K.mul ν ν)) (K.mul ν (K.mul ν ν))) (K.mul ν Zb)))))) := by
  have hw : ∀ r, O.nn (wt K kz r) := fun r => dot3_nn K O.toCone _
  have hZ : ∀ t, O.nn (Zs K l kz a t) := fun t => lsum_nn K O.toCone _ (fun r => O.nn_mul _ _ (hw r) (dot3_nn K O.toCone _)) l
  have hA : ∀ t, O.nn (As K l kz a t) := fun t => lsum_nn K O.toCone _ (fun r => O.nn_mul _ _ (O.nn_sq _) (dot3_nn K O.toCone _)) l
  have two : O.nn (K.add K.one K.one) := O.nn_add _ _ (RatModel.nn_one' K O.toCone) (RatModel.nn_one' K O.toCone)
  have four : O.nn (K.add (K.add K.one K.one) (K.add K.one K.one)) := O.nn_add _ _ two two
  have hc : O.nn (K.mul (castN K 135) (K.mul ι ι)) := O.nn_mul _ _ (RatModel.nn_cast K O.toCone 135) (O.nn_sq ι)
  have hiv : O.nn (iv K inv (Rc + 1)) := iv_nn K O inv mul_inv h10 Rc
  have hc1 : O.nn (cc K ι (C1 K inv Rc)) := O.nn_mul _ _ (O.nn_mul _ _ two (O.nn_sq ι))
    (O.nn_add _ _ (O.nn_mul _ _ (RatModel.nn_cast K O.toCone _) (RatModel.nn_one' K O.toCone))
      (O.nn_mul _ _ (RatModel.nn_cast K O.toCone _) hiv))
  have hc2 : O.nn (cc K ι (C2 K inv Rc t0)) := O.nn_mul _ _ (O.nn_mul _ _ two (O.nn_sq ι))
    (O.nn_add _ _ (O.nn_mul _ _ (RatModel.nn_cast K O.toCone _) (O.nn_sq _))
      (O.nn_mul _ _ (RatModel.nn_cast K O.toCone _) hiv))
  have choice : Le K O.toCone (K.mul (cc K ι (C2 K inv Rc t0)) Zb) (K.mul ν ν) :=
    le_trans' K O.toCone _ _ _ ⟨K.mul (cc K ι (C2 K inv Rc t0)) Zb, O.nn_mul _ _ hc2 hZb, by lib_ring⟩ choice2
  have hD : O.nn (K.mul h (K.mul (cc K ι (C1 K inv Rc)) (K.mul Zb Zb))) := O.nn_mul _ _ hh (O.nn_mul _ _ hc1 (O.nn_sq Zb))
  have nν3 : O.nn (K.mul ν (K.mul ν ν)) := O.nn_mul _ _ hν (O.nn_sq ν)
  -- the good time τ = t + 1 ≤ n + 1 with 2c·Q_τ ≤ ν³
  have ⟨t, ht, hq⟩ := EventH2.good_time K O inv mul_inv h10 l kz hd h1 M hM ι h ν Es
    (K.add (K.mul (castN K 135) (K.mul ι ι)) (K.mul (castN K 135) (K.mul ι ι)))
    hh hν hh0 hν0 hEs (O.nn_add _ _ hc hc) hstepM a N hs hneut hP hcfl h0 n htime
  have hτw : t + 1 ≤ n + 1 := Nat.succ_le_of_lt ht
  -- Q_τ ≤ νZb (local bound on the window)
  have stp := LocalHolder.local_step K O inv mul_inv h10 l kz hd h1 M hM Rc t0 hRM ι h ν hh hν a N hP hs
  have qτ : Le K O.toCone (Q K ν h (Zs K l kz a) (As K l kz a) (t + 1)) (K.mul ν Zb) := by
    have lb := local_bound K O ν h _ _ Zb (Zs K l kz a) (As K l kz a) hν hν0 hh hc1 hc2 hZb hZ hA stp choice (n + 1) budget (t + 1) hτw
    have tn := ForceSob.le_mul_right' K O.toCone (RatModel.cast_mono K O.toCone (t + 1) (n + 1) hτw) hD
    exact le_trans' K O.toCone _ _ _ lb (le_trans' K O.toCone _ _ _ (le_add' K O.toCone _ _ _ _ (le_refl' K O.toCone _) tn) budget)
  have nQτ : O.nn (Q K ν h (Zs K l kz a) (As K l kz a) (t + 1)) :=
    O.nn_add _ _ (O.nn_mul _ _ hν (hZ _)) (O.nn_mul _ _ (O.nn_mul _ _ hh (O.nn_sq ν)) (hA _))
  have cqτ : Le K O.toCone (K.mul (K.mul (castN K 135) (K.mul ι ι)) (Q K ν h (Zs K l kz a) (As K l kz a) (t + 1))) (K.mul ν (K.mul ν ν)) :=
    le_trans' K O.toCone _ _ _ ⟨K.mul (K.mul (castN K 135) (K.mul ι ι)) (Q K ν h (Zs K l kz a) (As K l kz a) (t + 1)),
      O.nn_mul _ _ hc nQτ, by lib_ring⟩ hq
  -- the small-regime constant S ≤ S₀
  have hSS : Le K O.toCone
      (K.mul (K.mul (K.add (K.add K.one K.one) (K.add K.one K.one)) h)
        (K.mul (K.add (K.mul (K.mul (castN K 135) (K.mul ι ι)) (Q K ν h (Zs K l kz a) (As K l kz a) (t + 1))) (K.mul ν (K.mul ν ν)))
          (Q K ν h (Zs K l kz a) (As K l kz a) (t + 1))))
      (K.mul (K.mul (K.add (K.add K.one K.one) (K.add K.one K.one)) h)
        (K.mul (K.add (K.mul ν (K.mul ν ν)) (K.mul ν (K.mul ν ν))) (K.mul ν Zb))) :=
    le_mul_nn K O.toCone _ _ _ (O.nn_mul _ _ four hh)
      (TruncRate.mm K O _ _ _ _ (O.nn_add _ _ (O.nn_mul _ _ hc nQτ) nν3) (O.nn_mul _ _ hν hZb)
        (le_add' K O.toCone _ _ _ _ cqτ (le_refl' K O.toCone _)) qτ)
  generalize hLd : K.mul ν (K.mul (K.mul (K.add (K.add K.one K.one) (K.add K.one K.one)) (K.mul h ν))
      (K.mul (K.add (K.mul (K.mul (castN K 135) (K.mul ι ι)) Zb) (K.mul ν ν)) Zb)) = Lν
  generalize hS0d : K.mul (K.mul (K.add (K.add K.one K.one) (K.add K.one K.one)) h)
      (K.mul (K.add (K.mul ν (K.mul ν ν)) (K.mul ν (K.mul ν ν))) (K.mul ν Zb)) = S0 at hSS
  have nL : O.nn Lν := by
    rw [← hLd]
    exact O.nn_mul _ _ hν (O.nn_mul _ _ (O.nn_mul _ _ four (O.nn_mul _ _ hh hν)) (O.nn_mul _ _ (O.nn_add _ _ (O.nn_mul _ _ hc hZb) (O.nn_sq ν)) hZb))
  have nS0 : O.nn S0 := by
    rw [← hS0d]
    exact O.nn_mul _ _ (O.nn_mul _ _ four hh) (O.nn_mul _ _ (O.nn_add _ _ nν3 nν3) (O.nn_mul _ _ hν hZb))
  -- window bound and small-regime bound
  have hL : ∀ T k, T + k ≤ n + 1 → Le K O.toCone
      (K.mul (K.mul ν (K.mul ν ν)) (lsum K (fun r => dot3 K (EnstDiff.sub3 K (a (T + k) r) (a T r)) (EnstDiff.sub3 K (a (T + k) r) (a T r))) l))
      (K.mul (castN K k) Lν) := by
    intro T k htk
    have lh := LocalHolder.local_holder K O inv mul_inv h10 l kz hd h1 M hM Rc t0 hRM ι h ν Zb hh hν hν0 hZb a N hP hs choice2 (n + 1) budget T k htk
    have m := le_mul_nn K O.toCone ν _ _ hν lh
    rw [← hLd]
    exact le_trans' K O.toCone _ _ _ ⟨K.zero, O.nn_zero, by lib_ring⟩ (le_trans' K O.toCone _ _ _ m ⟨K.zero, O.nn_zero, by lib_ring⟩)
  have hS : ∀ s k, Le K O.toCone
      (K.mul (K.mul ν (K.mul ν ν)) (lsum K (fun r => dot3 K (EnstDiff.sub3 K (a (t + 1 + s + k) r) (a (t + 1 + s) r))
        (EnstDiff.sub3 K (a (t + 1 + s + k) r) (a (t + 1 + s) r))) l))
      (K.mul (castN K k) S0) := by
    intro s k
    have hb := HolderSmall.holder_small K O inv mul_inv h10 l kz hd h1 M hM ι h ν hh hν hν0
      (fun j => a (t + 1 + j)) (fun j => N (t + 1 + j)) (fun j => hP (t + 1 + j)) (fun j r => hs (t + 1 + j) r) hq s k
    have hb' : Le K O.toCone
        (K.mul (K.mul ν (K.mul ν ν)) (lsum K (fun r => dot3 K (EnstDiff.sub3 K (a (t + 1 + (s + k)) r) (a (t + 1 + s) r))
          (EnstDiff.sub3 K (a (t + 1 + (s + k)) r) (a (t + 1 + s) r))) l))
        (K.mul (castN K k) (K.mul (K.mul (K.add (K.add K.one K.one) (K.add K.one K.one)) h)
          (K.mul (K.add (K.mul (K.mul (castN K 135) (K.mul ι ι)) (Q K ν h (Zs K l kz a) (As K l kz a) (t + 1))) (K.mul ν (K.mul ν ν)))
            (Q K ν h (Zs K l kz a) (As K l kz a) (t + 1))))) := hb
    rw [← Nat.add_assoc] at hb'
    exact le_trans' K O.toCone _ _ _ hb' (le_mul_nn K O.toCone _ _ _ (RatModel.nn_cast K O.toCone k) hSS)
  have kG : ∀ X, Le K O.toCone X Lν → Le K O.toCone X (K.mul (K.add K.one K.one) (K.add Lν S0)) := fun X hX =>
    le_trans' K O.toCone _ _ _ hX ⟨K.add Lν (K.add S0 S0), O.nn_add _ _ nL (O.nn_add _ _ nS0 nS0), by lib_ring⟩
  have kG' : ∀ X, Le K O.toCone X S0 → Le K O.toCone X (K.mul (K.add K.one K.one) (K.add Lν S0)) := fun X hX =>
    le_trans' K O.toCone _ _ _ hX ⟨K.add (K.add Lν Lν) S0, O.nn_add _ _ (O.nn_add _ _ nL nL) nS0, by lib_ring⟩
  have nk := RatModel.nn_cast K O.toCone k
  cases Nat.lt_or_ge (n + 1) (T + k) with
  | inr hwin =>
    exact le_trans' K O.toCone _ _ _ (hL T k hwin) (le_mul_nn K O.toCone _ _ _ nk (kG Lν (le_refl' K O.toCone _)))
  | inl hout =>
    cases Nat.lt_or_ge T (t + 1) with
    | inr hge =>
      have ⟨s, hs'⟩ := DyadicModelZ.le_dest hge
      rw [← hs']
      exact le_trans' K O.toCone _ _ _ (hS s k) (le_mul_nn K O.toCone _ _ _ nk (kG' S0 (le_refl' K O.toCone _)))
    | inl hlt =>
      have ⟨k1, hk1⟩ := DyadicModelZ.le_dest (Nat.le_of_lt hlt)
      have ⟨k2, hk2⟩ := DyadicModelZ.le_dest (Nat.le_of_lt (Nat.lt_of_le_of_lt hτw hout))
      have e : k1 + k2 = k := by
        apply cancel_add T
        rw [← Nat.add_assoc, hk1, hk2]
      have tr := tri_sq K O l (a (T + k)) (a (t + 1)) (a T)
      have b1 := hS 0 k2
      have b1' : Le K O.toCone
          (K.mul (K.mul ν (K.mul ν ν)) (lsum K (fun r => dot3 K (EnstDiff.sub3 K (a (T + k) r) (a (t + 1) r))
            (EnstDiff.sub3 K (a (T + k) r) (a (t + 1) r))) l)) (K.mul (castN K k2) S0) := by
        rw [← hk2]; exact b1
      have b2 := hL T k1 (by rw [hk1]; exact hτw)
      rw [hk1] at b2
      have nk1 := RatModel.nn_cast K O.toCone k1
      have nk2 := RatModel.nn_cast K O.toCone k2
      have m := le_mul_nn K O.toCone (K.mul ν (K.mul ν ν)) _ _ nν3 tr
      have comb := le_add' K O.toCone _ _ _ _ (le_mul_nn K O.toCone (K.add K.one K.one) _ _ two b1')
        (le_mul_nn K O.toCone (K.add K.one K.one) _ _ two b2)
      have ce : castN K k = K.add (castN K k1) (castN K k2) := by rw [← e, RatModel.cast_add]
      rw [ce]
      refine le_trans' K O.toCone _ _ _ m (le_trans' K O.toCone _ _ _ ⟨K.zero, O.nn_zero, ?_⟩
        (le_trans' K O.toCone _ _ _ comb ⟨K.add (K.mul (K.add K.one K.one) (K.mul (castN K k1) S0))
          (K.mul (K.add K.one K.one) (K.mul (castN K k2) Lν)),
          O.nn_add _ _ (O.nn_mul _ _ two (O.nn_mul _ _ nk1 nS0)) (O.nn_mul _ _ two (O.nn_mul _ _ nk2 nL)), ?_⟩))
      · lib_ring
      · lib_ring

end Gen

end GlobalHolder

#print axioms GlobalHolder.tri3
#print axioms GlobalHolder.tri_sq
#print axioms GlobalHolder.cancel_add
#print axioms GlobalHolder.global_holder

ns/GoodModes.lean

lean_all/ns/GoodModes.lean

/-
  ns/GoodModes.lean — T37 cycle 750 (goal G463): at a good time every midpoint mode decays like 2^{-n}, with no
  large-data hypothesis: for the concrete model over any ordered ring (ℚ included) there is t ≤ T with
    (T+1)·h·3·4^{n+1}·(a_{t+1}(n+1) + a_t(n+1))² ≤ E₀   for every n < N.

  Lean 4 core plus ns/GoodTime (good_time), ns/DyadicModelMode (term_le), ns/RatModel.
    good_modes   the statement above
-/
import ns.GoodTime
import ns.DyadicModelMode

namespace GoodModes
open DetChain NSChain DyadicSmallGlobal LibRing DyadicLargeData DissBudget RatModel DyadicBudget DyadicLarge
  GoodTime DyadicModelMode

theorem good_modes {R : Type} (K : CRing R) (O : OrdR K) (a : Nat → Nat → R) (h : R) (N : Nat) (hh : O.nn h)
    (h0 : ∀ t, a t 0 = K.zero) (hz : ∀ t n, N + 1 ≤ n → a t n = K.zero)
    (step : ∀ t n, K.add (a (t + 1) (n + 1)) (K.neg (a t (n + 1)))
      = K.mul h (K.add (K.neg (K.mul (castN K 3) (K.mul (P4 K (n + 1)) (K.add (a (t + 1) (n + 1)) (a t (n + 1))))))
          (dyN K (P2 K) (fun i => K.add (a (t + 1) i) (a t i)) n))) (T : Nat) :
    ∃ t, t ≤ T ∧ ∀ n, n < N → Le K O.toCone
      (K.mul (castN K (T + 1)) (K.mul h (K.mul (castN K 3) (K.mul (P4 K (n + 1))
        (K.mul (K.add (a (t + 1) (n + 1)) (a t (n + 1))) (K.add (a (t + 1) (n + 1)) (a t (n + 1))))))))
      (E0 K a N) := by
  have ⟨t, ht, hle⟩ := good_time K O a h N hh h0 hz step T
  refine ⟨t, ht, fun n hn => ?_⟩
  have tl := term_le K O.toCone (fun j => K.mul (P4 K (j + 1))
      (K.mul (K.add (a (t + 1) (j + 1)) (a t (j + 1))) (K.add (a (t + 1) (j + 1)) (a t (j + 1)))))
    (fun j => O.nn_mul _ _ (nn_pw K O.toCone _ (nn_cast K O.toCone 4) (j + 1)) (O.nn_sq _)) n N hn
  have l1 := le_mul_nn K O.toCone (castN K 3) _ _ (nn_cast K O.toCone 3) tl
  have l2 := le_mul_nn K O.toCone h _ _ hh l1
  have l3 := le_mul_nn K O.toCone (castN K (T + 1)) _ _ (nn_cast K O.toCone (T + 1)) l2
  exact le_trans' K O.toCone _ _ _ l3 hle

end GoodModes

#print axioms GoodModes.good_modes

ns/GoodModesQ.lean

lean_all/ns/GoodModesQ.lean

/-
  ns/GoodModesQ.lean — T37 cycle 760 (goal G472): GoodModes in divided form over an ordered field (ℚ included):
  there is t ≤ T with, for every n < N,
    (a_{t+1}(n+1) + a_t(n+1))² ≤ E₀ · X_n⁻¹,   X_n = (T+1)·h·3·4^{n+1}    (h ≠ 0).

  Lean 4 core plus ns/GoodModes, ns/RatModelU (le_mul_rg), lib/RingTac (lib_ring).
    ne_of_one_add   1 ≠ 0, x ≥ 0 ⇒ 1 + x ≠ 0
    cast_succ_ne    castN (n+1) ≠ 0;  pw_ne  x ≠ 0 ⇒ xⁿ ≠ 0;  mul_ne  (no zero divisors)
    nn_inv'         x ≥ 0, x·x⁻¹ = 1 ⇒ x⁻¹ ≥ 0
    div_le          X ≥ 0, X·X⁻¹ = 1, X·y ≤ E ⇒ y ≤ E·X⁻¹
    good_modes_q    the statement
-/
import ns.GoodModes
import ns.RatModelU

namespace GoodModesQ
open DetChain NSChain DyadicSmallGlobal LibRing DyadicLargeData DissBudget RatModel DyadicBudget DyadicLarge
  GoodTime DyadicModelMode GoodModes RatModelU

section Gen
variable {R : Type} (K : CRing R) (O : OrdR K)
include O

theorem one_eq_zero_of (h10 : K.one ≠ K.zero) (x : R) (hx : O.nn x) (e : K.add K.one x = K.zero) : False :=
  h10 (O.antisym _ _ ⟨x, hx, e⟩ ⟨K.one, nn_one' K O.toCone, K.zero_add _⟩)

theorem ne_of_one_add (h10 : K.one ≠ K.zero) (x : R) (hx : O.nn x) : K.add K.one x ≠ K.zero :=
  fun e => one_eq_zero_of K O h10 x hx e

theorem cast_succ_ne (h10 : K.one ≠ K.zero) (n : Nat) : castN K (n + 1) ≠ K.zero :=
  ne_of_one_add K O h10 _ (nn_cast K O.toCone n)

theorem mul_ne (x y : R) (hx : x ≠ K.zero) (hy : y ≠ K.zero) : K.mul x y ≠ K.zero := fun e => by
  cases O.noZD x y e with
  | inl h => exact hx h
  | inr h => exact hy h

theorem pw_ne (h10 : K.one ≠ K.zero) (x : R) (hx : x ≠ K.zero) : ∀ n, pw K x n ≠ K.zero
  | 0 => h10
  | n + 1 => mul_ne K O _ _ (pw_ne h10 x hx n) hx

theorem nn_inv' (h10 : K.one ≠ K.zero) (x xi : R) (hx : O.nn x) (hm : K.mul x xi = K.one) : O.nn xi := by
  cases O.total K.zero xi with
  | inl l => cases l with
    | intro d hd => rw [← hd.2, K.zero_add]; exact hd.1
  | inr l => cases l with
    | intro d hd =>
      exfalso
      have e : K.add K.one (K.mul x d) = K.zero := by
        have : d = K.neg xi := by
          have : d = K.add (K.add xi d) (K.neg xi) := by lib_ring
          rw [this, hd.2]; lib_ring
        rw [this, ← hm]; lib_ring
      exact one_eq_zero_of K O h10 _ (O.nn_mul _ _ hx hd.1) e

theorem div_le (X Xi y E : R) (h10 : K.one ≠ K.zero) (hX : O.nn X) (hm : K.mul X Xi = K.one)
    (h : Le K O.toCone (K.mul X y) E) : Le K O.toCone y (K.mul E Xi) := by
  have l := le_mul_rg K O.toCone Xi _ _ (nn_inv' K O h10 X Xi hX hm) h
  have e : K.mul (K.mul X y) Xi = y := by
    have : K.mul (K.mul X y) Xi = K.mul (K.mul X Xi) y := by lib_ring
    rw [this, hm]; lib_ring
  rw [e] at l; exact l

end Gen

theorem good_modes_q {R : Type} (K : CRing R) (O : OrdR K) (inv : R → R)
    (mul_inv : ∀ x, x ≠ K.zero → K.mul x (inv x) = K.one) (h10 : K.one ≠ K.zero)
    (a : Nat → Nat → R) (h : R) (N : Nat) (hh : O.nn h) (hh0 : h ≠ K.zero)
    (h0 : ∀ t, a t 0 = K.zero) (hz : ∀ t n, N + 1 ≤ n → a t n = K.zero)
    (step : ∀ t n, K.add (a (t + 1) (n + 1)) (K.neg (a t (n + 1)))
      = K.mul h (K.add (K.neg (K.mul (castN K 3) (K.mul (P4 K (n + 1)) (K.add (a (t + 1) (n + 1)) (a t (n + 1))))))
          (dyN K (P2 K) (fun i => K.add (a (t + 1) i) (a t i)) n))) (T : Nat) :
    ∃ t, t ≤ T ∧ ∀ n, n < N → Le K O.toCone
      (K.mul (K.add (a (t + 1) (n + 1)) (a t (n + 1))) (K.add (a (t + 1) (n + 1)) (a t (n + 1))))
      (K.mul (E0 K a N) (inv (K.mul (castN K (T + 1)) (K.mul h (K.mul (castN K 3) (P4 K (n + 1))))))) := by
  have ⟨t, ht, hb⟩ := good_modes K O a h N hh h0 hz step T
  refine ⟨t, ht, fun n hn => ?_⟩
  have hX0 : K.mul (castN K (T + 1)) (K.mul h (K.mul (castN K 3) (P4 K (n + 1)))) ≠ K.zero :=
    mul_ne K O _ _ (cast_succ_ne K O h10 T) (mul_ne K O _ _ hh0 (mul_ne K O _ _ (cast_succ_ne K O h10 2)
      (pw_ne K O h10 _ (cast_succ_ne K O h10 3) (n + 1))))
  have hXn : O.nn (K.mul (castN K (T + 1)) (K.mul h (K.mul (castN K 3) (P4 K (n + 1))))) :=
    O.nn_mul _ _ (nn_cast K O.toCone _) (O.nn_mul _ _ hh (O.nn_mul _ _ (nn_cast K O.toCone 3)
      (nn_pw K O.toCone _ (nn_cast K O.toCone 4) (n + 1))))
  apply div_le K O _ _ _ _ h10 hXn (mul_inv _ hX0)
  have e : K.mul (K.mul (castN K (T + 1)) (K.mul h (K.mul (castN K 3) (P4 K (n + 1)))))
      (K.mul (K.add (a (t + 1) (n + 1)) (a t (n + 1))) (K.add (a (t + 1) (n + 1)) (a t (n + 1))))
      = K.mul (castN K (T + 1)) (K.mul h (K.mul (castN K 3) (K.mul (P4 K (n + 1))
        (K.mul (K.add (a (t + 1) (n + 1)) (a t (n + 1))) (K.add (a (t + 1) (n + 1)) (a t (n + 1))))))) := by lib_ring
  rw [e]; exact hb n hn

end GoodModesQ

#print axioms GoodModesQ.one_eq_zero_of
#print axioms GoodModesQ.ne_of_one_add
#print axioms GoodModesQ.cast_succ_ne
#print axioms GoodModesQ.mul_ne
#print axioms GoodModesQ.pw_ne
#print axioms GoodModesQ.nn_inv'
#print axioms GoodModesQ.div_le
#print axioms GoodModesQ.good_modes_q

ns/GoodState.lean

lean_all/ns/GoodState.lean

/-
  ns/GoodState.lean — T37 cycle 763 (goal G475): at a good time the new state itself is bounded mode by mode, over
  an ordered field (ℚ included), with no large-data hypothesis. With B(i) = E₀·X_{i−1}⁻¹ (i ≥ 1), B(0) = E₀,
  X_n = (T+1)·h·3·4^{n+1}: there is t ≤ T with, for every n < N,
    4·a_{t+1}(n+1)² ≤ 2B(n+1) + 4h²(9·16^{n+1}·B(n+1) + 2·4ⁿ·B(n)² + 2·4^{n+1}·B(n+1)B(n+2)).

  Lean 4 core plus ns/GoodModesQ, ns/DyNBound (dyN_sq), ns/MidRecover (recover, le_two_mul).
    Bf, s_le_B     every midpoint mode is ≤ B (interior: GoodModesQ; boundary and beyond the truncation: 0)
    good_state     the statement
-/
import ns.GoodModesQ
import ns.DyNBound

namespace GoodState
open DetChain NSChain DyadicSmallGlobal LibRing DyadicLargeData RatModel DyadicLarge GoodModesQ MidRecover
  DyNBound RatModelU

section Gen
variable {R : Type} (K : CRing R) (O : OrdR K) (inv : R → R)

def X (h : R) (T n : Nat) : R := K.mul (castN K (T + 1)) (K.mul h (K.mul (castN K 3) (P4 K (n + 1))))

def Bf (E h : R) (T : Nat) : Nat → R
  | 0 => E
  | i + 1 => K.mul E (inv (X K h T i))

end Gen

theorem good_state {R : Type} (K : CRing R) (O : OrdR K) (inv : R → R)
    (mul_inv : ∀ x, x ≠ K.zero → K.mul x (inv x) = K.one) (h10 : K.one ≠ K.zero)
    (a : Nat → Nat → R) (h : R) (N : Nat) (hh : O.nn h) (hh0 : h ≠ K.zero)
    (h0 : ∀ t, a t 0 = K.zero) (hz : ∀ t n, N + 1 ≤ n → a t n = K.zero)
    (step : ∀ t n, K.add (a (t + 1) (n + 1)) (K.neg (a t (n + 1)))
      = K.mul h (K.add (K.neg (K.mul (castN K 3) (K.mul (P4 K (n + 1)) (K.add (a (t + 1) (n + 1)) (a t (n + 1))))))
          (dyN K (P2 K) (fun i => K.add (a (t + 1) i) (a t i)) n))) (T : Nat) :
    ∃ t, t ≤ T ∧ ∀ n, n < N → Le K O.toCone
      (K.mul (K.add (K.add K.one K.one) (K.add K.one K.one)) (K.mul (a (t + 1) (n + 1)) (a (t + 1) (n + 1))))
      (K.add (two K (Bf K inv (E0 K a N) h T (n + 1)))
        (two K (two K (K.mul (K.mul h h)
          (K.add (K.mul (K.mul (castN K 3) (castN K 3)) (K.mul (K.mul (P4 K (n + 1)) (P4 K (n + 1))) (Bf K inv (E0 K a N) h T (n + 1))))
            (K.add (two K (K.mul (K.mul (P2 K n) (P2 K n)) (K.mul (Bf K inv (E0 K a N) h T n) (Bf K inv (E0 K a N) h T n))))
              (two K (K.mul (K.mul (P2 K (n + 1)) (P2 K (n + 1)))
                (K.mul (Bf K inv (E0 K a N) h T (n + 1)) (Bf K inv (E0 K a N) h T (n + 2))))))))))) := by
  have ⟨t, ht, hb⟩ := good_modes_q K O inv mul_inv h10 a h N hh hh0 h0 hz step T
  refine ⟨t, ht, fun n hn => ?_⟩
  let s : Nat → R := fun i => K.add (a (t + 1) i) (a t i)
  have hE : O.nn (E0 K a N) := sq_sum_nonneg K O.toCone (fun n => a 0 (n + 1)) N
  have hXnn : ∀ i, O.nn (X K h T i) := fun i =>
    O.nn_mul _ _ (nn_cast K O.toCone _) (O.nn_mul _ _ hh (O.nn_mul _ _ (nn_cast K O.toCone 3)
      (nn_pw K O.toCone _ (nn_cast K O.toCone 4) (i + 1))))
  have hX0 : ∀ i, X K h T i ≠ K.zero := fun i =>
    mul_ne K O _ _ (cast_succ_ne K O h10 T) (mul_ne K O _ _ hh0 (mul_ne K O _ _ (cast_succ_ne K O h10 2)
      (pw_ne K O h10 _ (cast_succ_ne K O h10 3) (i + 1))))
  have hBnn : ∀ i, O.nn (Bf K inv (E0 K a N) h T i) := fun i => by
    cases i with
    | zero => exact hE
    | succ i => exact O.nn_mul _ _ hE (nn_inv' K O h10 _ _ (hXnn i) (mul_inv _ (hX0 i)))
  have zero_le : ∀ i, s i = K.zero → Le K O.toCone (K.mul (s i) (s i)) (Bf K inv (E0 K a N) h T i) := by
    intro i hi
    rw [hi, mul_zero K]; exact ⟨_, hBnn i, K.zero_add _⟩
  have sB : ∀ i, Le K O.toCone (K.mul (s i) (s i)) (Bf K inv (E0 K a N) h T i) := by
    intro i
    cases i with
    | zero => exact zero_le 0 (by show K.add (a (t + 1) 0) (a t 0) = K.zero; rw [h0, h0, K.zero_add])
    | succ i =>
      cases Nat.lt_or_ge i N with
      | inl l => exact hb i l
      | inr g => exact zero_le (i + 1) (by
          show K.add (a (t + 1) (i + 1)) (a t (i + 1)) = K.zero
          rw [hz (t + 1) (i + 1) (Nat.succ_le_succ g), hz t (i + 1) (Nat.succ_le_succ g), K.zero_add])
  have rc := recover K O.toCone (a t (n + 1)) (a (t + 1) (n + 1)) (P4 K (n + 1))
    (dyN K (P2 K) s n) h (castN K 3) (step t n)
  apply le_trans' K O.toCone _ _ _ rc
  have dn := dyN_sq K O.toCone (P2 K) s n _ _ _ (hBnn (n + 2)) (sB n) (sB (n + 1)) (sB (n + 2)) (hBnn n)
  have kk := le_two_mul K O.toCone (K.mul (castN K 3) (castN K 3)) _ _ (O.nn_sq _)
    (le_two_mul K O.toCone (K.mul (P4 K (n + 1)) (P4 K (n + 1))) _ _ (O.nn_sq _) (sB (n + 1)))
  have inner := le_add' K O.toCone _ _ _ _ kk dn
  have hh2 := le_two_mul K O.toCone (K.mul h h) _ _ (O.nn_sq h) inner
  have tw : ∀ x y, Le K O.toCone x y → Le K O.toCone (two K x) (two K y) := fun x y l => le_add' K O.toCone _ _ _ _ l l
  exact le_add' K O.toCone _ _ _ _ (tw _ _ (sB (n + 1))) (tw _ _ (tw _ _ hh2))

end GoodState

#print axioms GoodState.good_state

ns/GoodStep.lean

lean_all/ns/GoodStep.lean

/-
  ns/GoodStep.lean — T37 (goal G859c2-s3a): every long enough stretch of steps contains a step with small enstrophy,
  for arbitrary data and uniformly in the truncation (the corollary of ns/BadRun used to place window starts).
    split_run      for a family Y and a bound b over an ordered ring: either Y_t ≤ b for some t < L, or b ≤ Y_t for all t < L
                   (finite case split on the total order, no choice)
    good_step      under the cfl_global hypotheses, if L·hν·b ≤ E_0 + hν·Z_0 fails and s + L ≤ n + 1, then some
                   s ≤ t < s + L has Z_{t+1} ≤ b.
  Lean 4 core plus ns/BadRun.
-/
import ns.BadRun

namespace GoodStep
open DetChain NSChain DyadicSmallGlobal LibRing DyadicLarge FourierGalerkin GalerkinForce ForceBound ForceSob
  ModeCount CubeCount ForceUniform NormCompare EnstStep EnstGalerkin CFLStep CFLGlobal EventSmall BadCount BadRun

section Gen
variable {R : Type} (K : CRing R) (O : OrdR K) {I : Type}
variable (inv : R → R) (mul_inv : ∀ y, y ≠ K.zero → K.mul y (inv y) = K.one) (h10 : K.one ≠ K.zero)

theorem split_run (Y : Nat → R) (b : R) :
    ∀ L, (∃ t, t < L ∧ Le K O.toCone (Y t) b) ∨ (∀ t, t < L → Le K O.toCone b (Y t))
  | 0 => Or.inr (fun t ht => absurd ht (Nat.not_lt_zero t))
  | L + 1 => by
    cases split_run Y b L with
    | inl h =>
      obtain ⟨t, ht, hb⟩ := h
      exact Or.inl ⟨t, Nat.lt_succ_of_lt ht, hb⟩
    | inr h =>
      cases O.total (Y L) b with
      | inl hb => exact Or.inl ⟨L, Nat.lt_succ_self L, hb⟩
      | inr hb =>
        refine Or.inr (fun t ht => ?_)
        cases Nat.lt_or_eq_of_le (Nat.le_of_lt_succ ht) with
        | inl h' => exact h t h'
        | inr h' => rw [h']; exact hb

include mul_inv h10 in
/-- **A long stretch contains a good step.** -/
theorem good_step (l : List I) (kz : I → Mode) (hd : List.Pairwise (fun a b => kz a ≠ kz b) l)
    (h1 : ∀ p, 1 ≤ nrm (kz p)) (M : Nat) (hM : ∀ p, p ∈ l → nrm (kz p) ≤ M + 1)
    (ι h ν Es : R) (hh : O.nn h) (hν : O.nn ν) (hEs : O.nn Es) (a N : Nat → I → V3 R)
    (hs : ∀ t r, Step K h ν (wt K kz r) (a (t + 1) r) (a t r) (N t r))
    (hneut : ∀ t, lsum K (fun r => dot3 K (N t r) (a t r)) l = K.zero)
    (hP : ∀ t, Le K O.toCone (lsum K (fun r => dot3 K (N t r) (N t r)) l)
      (lsum K (fun r => dot3 K (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (a t) (a t) r)
        (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (a t) (a t) r)) l))
    (hcfl : Le K O.toCone (beta K M ι h ν Es) (K.mul h ν))
    (h0 : Le K O.toCone (K.add (lsum K (fun r => dot3 K (a 0 r) (a 0 r)) l)
      (K.mul (beta K M ι h ν Es) (lsum K (fun r => K.mul (wt K kz r) (dot3 K (a 0 r) (a 0 r))) l))) Es) (n : Nat)
    (b : R) (s L : Nat) (hsL : s + L ≤ n + 1)
    (hL : ¬ Le K O.toCone (K.mul (castN K L) (K.mul (K.mul h ν) b))
      (K.add (lsum K (fun r => dot3 K (a 0 r) (a 0 r)) l)
        (K.mul (K.mul h ν) (lsum K (fun r => K.mul (wt K kz r) (dot3 K (a 0 r) (a 0 r))) l)))) :
    ∃ t, t < L ∧
      Le K O.toCone (lsum K (fun r => K.mul (wt K kz r) (dot3 K (a (s + t + 1) r) (a (s + t + 1) r))) l) b := by
  cases split_run K O (fun t => lsum K (fun r => K.mul (wt K kz r) (dot3 K (a (s + t + 1) r) (a (s + t + 1) r))) l) b L with
  | inl h' => exact h'
  | inr h' =>
    exact absurd (leray_run K O inv mul_inv h10 l kz hd h1 M hM ι h ν Es hh hν hEs a N hs hneut hP hcfl h0 n b s L hsL h') hL

end Gen

end GoodStep

#print axioms GoodStep.split_run
#print axioms GoodStep.good_step

ns/GoodTime.lean

lean_all/ns/GoodTime.lean

/-
  ns/GoodTime.lean — T37 cycle 746 (goal G459): good times. Among the first T+1 steps of the concrete model over
  any ordered ring (ℚ included) there is one whose dissipation is at most E₀/(T+1) — the discrete form of Leray's
  "u(t) ∈ H¹ for almost every t" (the set of bad times has small measure).

  Lean 4 core plus ns/DyadicBudget (model_budget), ns/RatModel (cast lemmas), lib/RingTac (lib_ring).
    avg_le      ∃ t ≤ T, (T+1)·d_t ≤ Σ_{j≤T} d_j      (total order, constructive by induction)
    good_time   ∃ t ≤ T, (T+1)·D_t ≤ E₀  for the concrete model
-/
import ns.DyadicBudget

namespace GoodTime
open DetChain NSChain DyadicSmallGlobal LibRing DyadicLargeData DissBudget RatModel DyadicBudget DyadicLarge

section Gen
variable {R : Type} (K : CRing R) (O : OrdR K)

theorem avg_le (d : Nat → R) (hd : ∀ t, O.nn (d t)) : ∀ T, ∃ t, t ≤ T ∧
    Le K O.toCone (K.mul (castN K (T + 1)) (d t)) (sumN K d (T + 1))
  | 0 => ⟨0, Nat.le_refl 0, ⟨K.zero, O.nn_zero, by
      show K.add (K.mul (K.add K.one K.zero) (d 0)) K.zero = K.add K.zero (d 0); lib_ring⟩⟩
  | T + 1 => by
    have ⟨t, ht, hle⟩ := avg_le d hd T
    have hc : O.nn (castN K (T + 1)) := nn_cast K O.toCone (T + 1)
    cases O.total (d t) (d (T + 1)) with
    | inl l =>
      refine ⟨t, Nat.le_succ_of_le ht, ?_⟩
      have := le_add' K O.toCone _ _ _ _ hle l
      have e : K.mul (castN K (T + 1 + 1)) (d t) = K.add (K.mul (castN K (T + 1)) (d t)) (d t) := by
        show K.mul (K.add K.one (castN K (T + 1))) (d t) = _; lib_ring
      rw [e]; exact this
    | inr l =>
      refine ⟨T + 1, Nat.le_refl _, ?_⟩
      have l2 := le_mul_nn K O.toCone _ _ _ hc l
      have := le_add' K O.toCone _ _ _ _ (le_trans' K O.toCone _ _ _ l2 hle) (le_refl' K O.toCone (d (T + 1)))
      have e : K.mul (castN K (T + 1 + 1)) (d (T + 1))
          = K.add (K.mul (castN K (T + 1)) (d (T + 1))) (d (T + 1)) := by
        show K.mul (K.add K.one (castN K (T + 1))) (d (T + 1)) = _; lib_ring
      rw [e]; exact this

theorem good_time (a : Nat → Nat → R) (h : R) (N : Nat) (hh : O.nn h)
    (h0 : ∀ t, a t 0 = K.zero) (hz : ∀ t n, N + 1 ≤ n → a t n = K.zero)
    (step : ∀ t n, K.add (a (t + 1) (n + 1)) (K.neg (a t (n + 1)))
      = K.mul h (K.add (K.neg (K.mul (castN K 3) (K.mul (P4 K (n + 1)) (K.add (a (t + 1) (n + 1)) (a t (n + 1))))))
          (dyN K (P2 K) (fun i => K.add (a (t + 1) i) (a t i)) n))) (T : Nat) :
    ∃ t, t ≤ T ∧ Le K O.toCone
      (K.mul (castN K (T + 1)) (D K (fun t n => a t (n + 1)) (fun n => P4 K (n + 1)) h (castN K 3) N t)) (E0 K a N) := by
  have hd : ∀ t, O.nn (D K (fun t n => a t (n + 1)) (fun n => P4 K (n + 1)) h (castN K 3) N t) := fun t =>
    O.nn_mul _ _ hh (O.nn_mul _ _ (nn_cast K O.toCone 3)
      (sumN_nn K O.toCone _ (fun n => O.nn_mul _ _ (nn_pw K O.toCone _ (nn_cast K O.toCone 4) (n + 1)) (O.nn_sq _)) N))
  have ⟨t, ht, hle⟩ := avg_le K O _ hd T
  exact ⟨t, ht, le_trans' K O.toCone _ _ _ hle (model_budget K O.toCone a h N h0 hz step (T + 1))⟩

end Gen

end GoodTime

#print axioms GoodTime.avg_le
#print axioms GoodTime.good_time

ns/GronwallFactor.lean

lean_all/ns/GronwallFactor.lean

/-
  ns/GronwallFactor.lean — T37 (goal G859c2h, piece d): the Gronwall factor bounded by the window length, over an
  ordered ring. For a ≥ 0 and 2m·a ≤ 1:
    factor_pow   (1 + a)^(m·k) ≤ 2^k,
  so with PowFactor.pwr_factor, (ν + hα)^(m·k) ≤ ν^(m·k)·2^k where k ≈ 2·T·hα/ν depends only on T·h — the constant of
  TruncRateUpto stays bounded as h → 0 at fixed window length (G859c2h).

  Lean 4 core plus ns/PowBound, ns/PowFactor, ns/WeightedCS.
-/
import ns.PowBound
import ns.PowFactor
import ns.WeightedCS

namespace GronwallFactor
open DetChain NSChain DyadicSmallGlobal LibRing DyadicLarge EnstGronwall

section Gen
variable {R : Type} (K : CRing R) (O : OrdR K)

/-- **(1 + a)^(m·k) ≤ 2^k** -/
theorem factor_pow (a : R) (ha : O.nn a) (m : Nat)
    (hm : Le K O.toCone (K.mul (K.add K.one K.one) (K.mul (castN K m) a)) K.one) :
    ∀ k : Nat, Le K O.toCone (pwr K (K.add K.one a) (m * k)) (pwr K (K.add K.one K.one) k)
  | 0 => le_refl' K O.toCone _
  | k + 1 => by
    have ih := factor_pow a ha m hm k
    have h1 := PowBound.pow_le_two K O a ha m hm
    have e : pwr K (K.add K.one a) (m * (k + 1)) = K.mul (pwr K (K.add K.one a) (m * k)) (pwr K (K.add K.one a) m) :=
      PowFactor.pwr_mul K (K.add K.one a) (m * k) m
    rw [e]
    have nP : O.nn (pwr K (K.add K.one a) (m * k)) :=
      pwr_nn K O _ (O.nn_add _ _ (RatModel.nn_one' K O.toCone) ha) _
    have n2 : O.nn (K.add K.one K.one) := O.nn_add _ _ (RatModel.nn_one' K O.toCone) (RatModel.nn_one' K O.toCone)
    have s := WeightedCS.le_mul_both' K O.toCone _ _ _ _ nP n2 ih h1
    refine le_trans' K O.toCone _ _ _ s ⟨K.zero, O.nn_zero, ?_⟩
    show K.add (K.mul (pwr K (K.add K.one K.one) k) (K.add K.one K.one)) K.zero
      = K.mul (K.add K.one K.one) (pwr K (K.add K.one K.one) k)
    lib_ring

end Gen

end GronwallFactor

#print axioms GronwallFactor.factor_pow

ns/GronwallH.lean

lean_all/ns/GronwallH.lean

/-
  ns/GronwallH.lean — T37 (goal G859c2h, piece e): the Gronwall factor of the truncation Cauchy estimate, uniform in
  the time step, over an ordered field with inverses. For ν ≥ 0 with ν·ν⁻¹ = 1, hα·ν⁻¹ ≥ 0 and 2m·hαν⁻¹ ≤ 1:
    gronwall_h   (ν + hα)^(m·k) ≤ ν^(m·k)·2^k.
  With T = m·k steps and m ≈ ν/(2hα), k ≈ 2Thα/ν = 2(T·h)α/ν depends only on the time T·h: the factor in
  LargeCauchy.large_cauchy stays bounded as h → 0 at fixed window length.

  Lean 4 core plus ns/GronwallFactor.
-/
import ns.GronwallFactor

namespace GronwallH
open DetChain NSChain DyadicSmallGlobal LibRing DyadicLarge EnstGronwall

section Gen
variable {R : Type} (K : CRing R) (O : OrdR K)

/-- **(ν + hα)^(m·k) ≤ ν^(m·k)·2^k** -/
theorem gronwall_h (ν hα iν : R) (hν : O.nn ν) (hνi : K.mul ν iν = K.one) (ha : O.nn (K.mul hα iν)) (m k : Nat)
    (hm : Le K O.toCone (K.mul (K.add K.one K.one) (K.mul (castN K m) (K.mul hα iν))) K.one) :
    Le K O.toCone (pwr K (K.add ν hα) (m * k)) (K.mul (pwr K ν (m * k)) (pwr K (K.add K.one K.one) k)) := by
  rw [PowFactor.pwr_factor K ν hα iν hνi (m * k)]
  exact le_mul_nn K O.toCone _ _ _ (pwr_nn K O ν hν _) (GronwallFactor.factor_pow K O (K.mul hα iν) ha m hm k)

end Gen

end GronwallH

#print axioms GronwallH.gronwall_h

ns/GronwallSource.lean

lean_all/ns/GronwallSource.lean

/-
  ns/GronwallSource.lean — T37 cycle 1043 (goal G684, piece a): discrete Gronwall with a source, constant
  coefficients, over an ordered ring. With Q_t = νZ_t + hν²A_t, A_t ≥ 0, β_t ≤ ν², 0 ≤ α_t ≤ α, 0 ≤ S_t ≤ S:
    step:  νZ_{t+1} + hν²A_{t+1} ≤ νZ_t + h(α_t Z_t + β_t A_t) + h S_t
    ⇒      ν^T · Q_T ≤ (ν + hα)^T · (Q_0 + T·h·S)                          (gronwall_src)
  Used for the restriction of a large-truncation solution against the small-truncation solution, where S is the
  truncation error energy (TruncBound) and Q_0 = 0.

  Lean 4 core plus ns/EnstGronwall (pwr, one-step pattern), ns/EnstGlobal (Q).
-/
import ns.EnstGronwall

namespace GronwallSource
open DetChain NSChain DyadicSmallGlobal LibRing DyadicLarge EnstGlobal EnstGronwall

section Gen
variable {R : Type} (K : CRing R) (O : OrdR K)

theorem pwr_mono (x y : R) (hx : O.nn x) (hxy : Le K O.toCone x y) : ∀ n, Le K O.toCone (pwr K x n) (pwr K y n)
  | 0 => le_refl' K O.toCone _
  | n + 1 => by
    have ih := pwr_mono x y hx hxy n
    have hy : O.nn y := by
      cases hxy with
      | intro d hd => rw [← hd.2]; exact O.nn_add _ _ hx hd.1
    have m1 := ForceSob.le_mul_right' K O.toCone hxy (pwr_nn K O x hx n)
    have m2 := le_mul_nn K O.toCone y _ _ hy ih
    exact le_trans' K O.toCone _ _ _ m1 m2

theorem gronwall_src (ν h α S : R) (Z A αs β Ss : Nat → R) (hν : O.nn ν) (hh : O.nn h) (hα : O.nn α) (hS : O.nn S)
    (hA : ∀ t, O.nn (A t)) (hαs : ∀ t, O.nn (αs t)) (hαle : ∀ t, Le K O.toCone (αs t) α)
    (hSs : ∀ t, O.nn (Ss t)) (hSle : ∀ t, Le K O.toCone (Ss t) S) (hβ : ∀ t, Le K O.toCone (β t) (K.mul ν ν))
    (hZ : ∀ t, O.nn (Z t))
    (step : ∀ t, Le K O.toCone (Q K ν h Z A (t + 1))
      (K.add (K.add (K.mul ν (Z t)) (K.mul h (K.add (K.mul (αs t) (Z t)) (K.mul (β t) (A t))))) (K.mul h (Ss t)))) :
    ∀ T, Le K O.toCone (K.mul (pwr K ν T) (Q K ν h Z A T))
      (K.mul (pwr K (K.add ν (K.mul h α)) T) (K.add (Q K ν h Z A 0) (K.mul (castN K T) (K.mul h S))))
  | 0 => ⟨K.zero, O.nn_zero, by show K.add (K.mul K.one _) K.zero = K.mul K.one (K.add _ (K.mul K.zero _)); lib_ring⟩
  | T + 1 => by
    have ih := gronwall_src ν h α S Z A αs β Ss hν hh hα hS hA hαs hαle hSs hSle hβ hZ step T
    have hQ : O.nn (Q K ν h Z A T) := O.nn_add _ _ (O.nn_mul _ _ hν (hZ T)) (O.nn_mul _ _ (O.nn_mul _ _ hh (O.nn_sq ν)) (hA T))
    -- one step: νQ_{T+1} ≤ (ν + hα)Q_T + hνS
    have b1 := ForceSob.le_mul_right' K O.toCone (hβ T) (hA T)
    have a1 := ForceSob.le_mul_right' K O.toCone (hαle T) (hZ T)
    have c1 := le_mul_nn K O.toCone h _ _ hh (le_add' K O.toCone _ _ _ _ a1 b1)
    have s1 := le_trans' K O.toCone _ _ _ (step T) (le_add' K O.toCone _ _ _ _
      (le_add' K O.toCone _ _ _ _ (le_refl' K O.toCone (K.mul ν (Z T))) c1)
      (le_mul_nn K O.toCone h _ _ hh (hSle T)))
    have s2 := le_mul_nn K O.toCone ν _ _ hν s1
    have os : Le K O.toCone (K.mul ν (Q K ν h Z A (T + 1)))
        (K.add (K.mul (K.add ν (K.mul h α)) (Q K ν h Z A T)) (K.mul ν (K.mul h S))) := by
      refine le_trans' K O.toCone _ _ _ s2 ⟨K.mul (K.mul h α) (K.mul (K.mul h (K.mul ν ν)) (A T)),
        O.nn_mul _ _ (O.nn_mul _ _ hh hα) (O.nn_mul _ _ (O.nn_mul _ _ hh (O.nn_sq ν)) (hA T)), ?_⟩
      show K.add (K.mul ν (K.add (K.add (K.mul ν (Z T)) (K.mul h (K.add (K.mul α (Z T)) (K.mul (K.mul ν ν) (A T))))) (K.mul h S)))
          (K.mul (K.mul h α) (K.mul (K.mul h (K.mul ν ν)) (A T)))
        = K.add (K.mul (K.add ν (K.mul h α)) (K.add (K.mul ν (Z T)) (K.mul (K.mul h (K.mul ν ν)) (A T)))) (K.mul ν (K.mul h S))
      lib_ring
    -- ν^{T+1}Q_{T+1} ≤ (ν+hα)·ν^T Q_T + ν^{T+1} hS ≤ (ν+hα)^{T+1}(Q_0 + T hνS) + (ν+hα)^{T+1} hνS
    have hνα : O.nn (K.add ν (K.mul h α)) := O.nn_add _ _ hν (O.nn_mul _ _ hh hα)
    have m1 := le_mul_nn K O.toCone (pwr K ν T) _ _ (pwr_nn K O ν hν T) os
    have pm := pwr_mono K O ν (K.add ν (K.mul h α)) hν ⟨K.mul h α, O.nn_mul _ _ hh hα, rfl⟩ (T + 1)
    have hhS : O.nn (K.mul h S) := O.nn_mul _ _ hh hS
    have src := ForceSob.le_mul_right' K O.toCone pm hhS
    have main := le_mul_nn K O.toCone (K.add ν (K.mul h α)) _ _ hνα ih
    have e0 : K.mul (pwr K ν (T + 1)) (Q K ν h Z A (T + 1)) = K.mul (pwr K ν T) (K.mul ν (Q K ν h Z A (T + 1))) := by
      show K.mul (K.mul ν (pwr K ν T)) _ = _; lib_ring
    rw [e0]
    refine le_trans' K O.toCone _ _ _ m1 ?_
    have e1 : K.mul (pwr K ν T) (K.add (K.mul (K.add ν (K.mul h α)) (Q K ν h Z A T)) (K.mul ν (K.mul h S)))
        = K.add (K.mul (K.add ν (K.mul h α)) (K.mul (pwr K ν T) (Q K ν h Z A T))) (K.mul (pwr K ν (T + 1)) (K.mul h S)) := by
      show _ = K.add _ (K.mul (K.mul ν (pwr K ν T)) _); lib_ring
    rw [e1]
    refine le_trans' K O.toCone _ _ _ (le_add' K O.toCone _ _ _ _ main src) ⟨K.zero, O.nn_zero, ?_⟩
    show K.add (K.add (K.mul (K.add ν (K.mul h α)) (K.mul (pwr K (K.add ν (K.mul h α)) T)
        (K.add (Q K ν h Z A 0) (K.mul (castN K T) (K.mul h S)))))
        (K.mul (K.mul (K.add ν (K.mul h α)) (pwr K (K.add ν (K.mul h α)) T)) (K.mul h S))) K.zero
      = K.mul (K.mul (K.add ν (K.mul h α)) (pwr K (K.add ν (K.mul h α)) T))
        (K.add (Q K ν h Z A 0) (K.mul (K.add K.one (castN K T)) (K.mul h S)))
    lib_ring

end Gen

end GronwallSource

#print axioms GronwallSource.pwr_mono
#print axioms GronwallSource.gronwall_src

ns/GronwallSrc.lean

lean_all/ns/GronwallSrc.lean

/-
  ns/GronwallSrc.lean — T37 cycle 1764 (goal G852g, piece a): the enstrophy-difference Gronwall step with a source,
  for comparing a scheme with a perturbed one (truncation defect). Over an ordered field, Q_t = νZ_t + hν²A_t:
    one_step_src   Q_{t+1} ≤ νZ_t + h(α_tZ_t + β_tA_t) + h·S_t,  β_t ≤ ν²   ⇒   νQ_{t+1} ≤ (ν + hα_t)Q_t + νhS_t
    gronwall_src   if moreover ν + hα_t ≤ νg, 1 ≤ g, S_t ≤ S (ν ≠ 0)   ⇒   Q_n ≤ gⁿ·(Q_0 + n·hS)
  Lean 4 core plus ns/EnstGronwall, ns/ErrRec.
-/
import ns.EnstGronwall
import ns.ErrRec

namespace GronwallSrc
open DetChain NSChain DyadicSmallGlobal LibRing DyadicLarge EnstGlobal EnstGronwall

section Gen
variable {R : Type} (K : CRing R) (O : OrdR K)

theorem one_step_src (ν h : R) (Z A α β S : Nat → R) (hν : O.nn ν) (hh : O.nn h) (hA : ∀ t, O.nn (A t))
    (hα : ∀ t, O.nn (α t)) (hβ : ∀ t, Le K O.toCone (β t) (K.mul ν ν))
    (step : ∀ t, Le K O.toCone (Q K ν h Z A (t + 1))
      (K.add (K.add (K.mul ν (Z t)) (K.mul h (K.add (K.mul (α t) (Z t)) (K.mul (β t) (A t))))) (K.mul h (S t))))
    (t : Nat) :
    Le K O.toCone (K.mul ν (Q K ν h Z A (t + 1)))
      (K.add (K.mul (K.add ν (K.mul h (α t))) (Q K ν h Z A t)) (K.mul ν (K.mul h (S t)))) := by
  have b1 := ForceSob.le_mul_right' K O.toCone (hβ t) (hA t)
  have b2 := le_mul_nn K O.toCone h _ _ hh (le_add' K O.toCone _ _ _ _ (le_refl' K O.toCone (K.mul (α t) (Z t))) b1)
  have s1 := le_trans' K O.toCone _ _ _ (step t)
    (le_add' K O.toCone _ _ _ _ (le_add' K O.toCone _ _ _ _ (le_refl' K O.toCone (K.mul ν (Z t))) b2)
      (le_refl' K O.toCone (K.mul h (S t))))
  have s2 := le_mul_nn K O.toCone ν _ _ hν s1
  refine le_trans' K O.toCone _ _ _ s2 ⟨K.mul (K.mul h (α t)) (K.mul (K.mul h (K.mul ν ν)) (A t)),
    O.nn_mul _ _ (O.nn_mul _ _ hh (hα t)) (O.nn_mul _ _ (O.nn_mul _ _ hh (O.nn_sq ν)) (hA t)), ?_⟩
  show K.add (K.mul ν (K.add (K.add (K.mul ν (Z t)) (K.mul h (K.add (K.mul (α t) (Z t)) (K.mul (K.mul ν ν) (A t)))))
        (K.mul h (S t))))
      (K.mul (K.mul h (α t)) (K.mul (K.mul h (K.mul ν ν)) (A t)))
    = K.add (K.mul (K.add ν (K.mul h (α t))) (K.add (K.mul ν (Z t)) (K.mul (K.mul h (K.mul ν ν)) (A t))))
        (K.mul ν (K.mul h (S t)))
  lib_ring

theorem gronwall_src (ν h g Sb : R) (Z A α β S : Nat → R) (hν : O.nn ν) (hν0 : ν ≠ K.zero) (hh : O.nn h)
    (hZ : ∀ t, O.nn (Z t)) (hA : ∀ t, O.nn (A t)) (hα : ∀ t, O.nn (α t))
    (hβ : ∀ t, Le K O.toCone (β t) (K.mul ν ν))
    (hg : Le K O.toCone K.one g) (hαg : ∀ t, Le K O.toCone (K.add ν (K.mul h (α t))) (K.mul ν g))
    (hS : ∀ t, Le K O.toCone (S t) Sb) (hSb : O.nn Sb)
    (step : ∀ t, Le K O.toCone (Q K ν h Z A (t + 1))
      (K.add (K.add (K.mul ν (Z t)) (K.mul h (K.add (K.mul (α t) (Z t)) (K.mul (β t) (A t))))) (K.mul h (S t)))) :
    ∀ n, Le K O.toCone (Q K ν h Z A n)
      (K.mul (pwr K g n) (K.add (Q K ν h Z A 0) (K.mul (castN K n) (K.mul h Sb)))) := by
  have hQ : ∀ t, O.nn (Q K ν h Z A t) := fun t =>
    O.nn_add _ _ (O.nn_mul _ _ hν (hZ t)) (O.nn_mul _ _ (O.nn_mul _ _ hh (O.nn_sq ν)) (hA t))
  apply ErrRec.rec_src K O g (K.mul h Sb) (Q K ν h Z A) hg (O.nn_mul _ _ hh hSb)
  intro t
  have o := one_step_src K O ν h Z A α β S hν hh hA hα hβ step t
  have c1 := ForceSob.le_mul_right' K O.toCone (hαg t) (hQ t)
  have c2 := le_mul_nn K O.toCone ν _ _ hν (le_mul_nn K O.toCone h _ _ hh (hS t))
  have c := le_trans' K O.toCone _ _ _ o (le_add' K O.toCone _ _ _ _ c1 c2)
  apply EnstGlobal.le_of_mul_le K O ν _ _ hν hν0
  refine le_trans' K O.toCone _ _ _ c ⟨K.zero, O.nn_zero, ?_⟩
  lib_ring

end Gen
end GronwallSrc

#print axioms GronwallSrc.one_step_src
#print axioms GronwallSrc.gronwall_src

ns/GronwallUpto.lean

lean_all/ns/GronwallUpto.lean

/-
  ns/GronwallUpto.lean — T37 (goal G859c2g, piece b1): discrete Gronwall with a source on a finite window, over an
  ordered ring. The hypotheses of GronwallSource.gronwall_src (coefficient bounds and the step inequality) are asked
  only for t < n; the conclusion
    gronwall_upto   ν^T·Q_T ≤ (ν + hα)^T·(Q_0 + T·h·S)   for T ≤ n
  holds on the window. This is what the large-data local bounds (H2LargeAll: Z_t ≤ Zb, A_t ≤ Aa only for t ≤ n) feed
  into the truncation Cauchy estimate (TruncCauchy) — same proof as gronwall_src.

  Lean 4 core plus ns/GronwallSource.
-/
import ns.GronwallSource

namespace GronwallUpto
open DetChain NSChain DyadicSmallGlobal LibRing DyadicLarge EnstGlobal EnstGronwall

section Gen
variable {R : Type} (K : CRing R) (O : OrdR K)

theorem gronwall_upto (ν h α S : R) (Z A αs β Ss : Nat → R) (hν : O.nn ν) (hh : O.nn h) (hα : O.nn α) (hS : O.nn S)
    (hA : ∀ t, O.nn (A t)) (n : Nat) (hαs : ∀ t, t < n → O.nn (αs t)) (hαle : ∀ t, t < n → Le K O.toCone (αs t) α)
    (hSs : ∀ t, t < n → O.nn (Ss t)) (hSle : ∀ t, t < n → Le K O.toCone (Ss t) S)
    (hβ : ∀ t, t < n → Le K O.toCone (β t) (K.mul ν ν))
    (hZ : ∀ t, O.nn (Z t))
    (step : ∀ t, t < n → Le K O.toCone (Q K ν h Z A (t + 1))
      (K.add (K.add (K.mul ν (Z t)) (K.mul h (K.add (K.mul (αs t) (Z t)) (K.mul (β t) (A t))))) (K.mul h (Ss t)))) :
    ∀ T, T ≤ n → Le K O.toCone (K.mul (pwr K ν T) (Q K ν h Z A T))
      (K.mul (pwr K (K.add ν (K.mul h α)) T) (K.add (Q K ν h Z A 0) (K.mul (castN K T) (K.mul h S))))
  | 0, _ => ⟨K.zero, O.nn_zero, by show K.add (K.mul K.one _) K.zero = K.mul K.one (K.add _ (K.mul K.zero _)); lib_ring⟩
  | T + 1, hT => by
    have hTn : T < n := hT
    have ih := gronwall_upto ν h α S Z A αs β Ss hν hh hα hS hA n hαs hαle hSs hSle hβ hZ step T (Nat.le_of_lt hTn)
    have hQ : O.nn (Q K ν h Z A T) := O.nn_add _ _ (O.nn_mul _ _ hν (hZ T)) (O.nn_mul _ _ (O.nn_mul _ _ hh (O.nn_sq ν)) (hA T))
    -- one step: νQ_{T+1} ≤ (ν + hα)Q_T + hνS
    have b1 := ForceSob.le_mul_right' K O.toCone (hβ T hTn) (hA T)
    have a1 := ForceSob.le_mul_right' K O.toCone (hαle T hTn) (hZ T)
    have c1 := le_mul_nn K O.toCone h _ _ hh (le_add' K O.toCone _ _ _ _ a1 b1)
    have s1 := le_trans' K O.toCone _ _ _ (step T hTn) (le_add' K O.toCone _ _ _ _
      (le_add' K O.toCone _ _ _ _ (le_refl' K O.toCone (K.mul ν (Z T))) c1)
      (le_mul_nn K O.toCone h _ _ hh (hSle T hTn)))
    have s2 := le_mul_nn K O.toCone ν _ _ hν s1
    have os : Le K O.toCone (K.mul ν (Q K ν h Z A (T + 1)))
        (K.add (K.mul (K.add ν (K.mul h α)) (Q K ν h Z A T)) (K.mul ν (K.mul h S))) := by
      refine le_trans' K O.toCone _ _ _ s2 ⟨K.mul (K.mul h α) (K.mul (K.mul h (K.mul ν ν)) (A T)),
        O.nn_mul _ _ (O.nn_mul _ _ hh hα) (O.nn_mul _ _ (O.nn_mul _ _ hh (O.nn_sq ν)) (hA T)), ?_⟩
      show K.add (K.mul ν (K.add (K.add (K.mul ν (Z T)) (K.mul h (K.add (K.mul α (Z T)) (K.mul (K.mul ν ν) (A T))))) (K.mul h S)))
          (K.mul (K.mul h α) (K.mul (K.mul h (K.mul ν ν)) (A T)))
        = K.add (K.mul (K.add ν (K.mul h α)) (K.add (K.mul ν (Z T)) (K.mul (K.mul h (K.mul ν ν)) (A T)))) (K.mul ν (K.mul h S))
      lib_ring
    -- ν^{T+1}Q_{T+1} ≤ (ν+hα)·ν^T Q_T + ν^{T+1} hS ≤ (ν+hα)^{T+1}(Q_0 + T hνS) + (ν+hα)^{T+1} hνS
    have hνα : O.nn (K.add ν (K.mul h α)) := O.nn_add _ _ hν (O.nn_mul _ _ hh hα)
    have m1 := le_mul_nn K O.toCone (pwr K ν T) _ _ (pwr_nn K O ν hν T) os
    have pm := GronwallSource.pwr_mono K O ν (K.add ν (K.mul h α)) hν ⟨K.mul h α, O.nn_mul _ _ hh hα, rfl⟩ (T + 1)
    have hhS : O.nn (K.mul h S) := O.nn_mul _ _ hh hS
    have src := ForceSob.le_mul_right' K O.toCone pm hhS
    have main := le_mul_nn K O.toCone (K.add ν (K.mul h α)) _ _ hνα ih
    have e0 : K.mul (pwr K ν (T + 1)) (Q K ν h Z A (T + 1)) = K.mul (pwr K ν T) (K.mul ν (Q K ν h Z A (T + 1))) := by
      show K.mul (K.mul ν (pwr K ν T)) _ = _; lib_ring
    rw [e0]
    refine le_trans' K O.toCone _ _ _ m1 ?_
    have e1 : K.mul (pwr K ν T) (K.add (K.mul (K.add ν (K.mul h α)) (Q K ν h Z A T)) (K.mul ν (K.mul h S)))
        = K.add (K.mul (K.add ν (K.mul h α)) (K.mul (pwr K ν T) (Q K ν h Z A T))) (K.mul (pwr K ν (T + 1)) (K.mul h S)) := by
      show _ = K.add _ (K.mul (K.mul ν (pwr K ν T)) _); lib_ring
    rw [e1]
    refine le_trans' K O.toCone _ _ _ (le_add' K O.toCone _ _ _ _ main src) ⟨K.zero, O.nn_zero, ?_⟩
    show K.add (K.add (K.mul (K.add ν (K.mul h α)) (K.mul (pwr K (K.add ν (K.mul h α)) T)
        (K.add (Q K ν h Z A 0) (K.mul (castN K T) (K.mul h S)))))
        (K.mul (K.mul (K.add ν (K.mul h α)) (pwr K (K.add ν (K.mul h α)) T)) (K.mul h S))) K.zero
      = K.mul (K.mul (K.add ν (K.mul h α)) (pwr K (K.add ν (K.mul h α)) T))
        (K.add (Q K ν h Z A 0) (K.mul (K.add K.one (castN K T)) (K.mul h S)))
    lib_ring

end Gen

end GronwallUpto

#print axioms GronwallUpto.gronwall_upto

ns/H2Large.lean

lean_all/ns/H2Large.lean

/-
  ns/H2Large.lean — T37 (goal G859c2d): the large-data local H² bound, uniform in the truncation, over an ordered
  field. Under the hypotheses of EnstLocalTime.local_time (cutoffs Rc ≤ M, step count n with n·h·c₁Zb² ≤ Q₀,
  ν·Zb = 2Q₀) and those of H2LocalUniform.h2_local_uniform (e, D₁, D₂, D₃ for Zb, ε ≤ ν²), for every t ≤ n:
    h2_large   Z_t ≤ Zb   and   νA_t + hεB_t ≤ νA_0 + hεB_0 + t·h·D,
  D = ι²(k₁(D₁ + 8Zb²) + k₂(D₂ + 2D₃)): on the local-time interval both the enstrophy and the H² energy are bounded
  independently of the Galerkin truncation — the H² bound R_r of G859c2 on [0, t₂] for t₂ = n·h.

  Lean 4 core plus ns/EnstLocalTime, ns/H2LocalUniform.
-/
import ns.EnstLocalTime
import ns.H2LocalUniform

namespace H2Large
open DetChain NSChain DyadicSmallGlobal LibRing DyadicLarge FourierGalerkin GalerkinForce ForceBound ForceSob
  ModeCount CubeCount ForceUniform NormCompare EnstGalerkin ForceTwoScale EnstStep

section Gen
variable {R : Type} (K : CRing R) (O : OrdR K) {I : Type}
variable (inv : R → R) (mul_inv : ∀ y, y ≠ K.zero → K.mul y (inv y) = K.one) (h10 : K.one ≠ K.zero)

include mul_inv h10 in
/-- **The large-data local H² bound, uniform in the truncation.** -/
theorem h2_large (l : List I) (kz : I → Mode) (hd : List.Pairwise (fun a b => kz a ≠ kz b) l)
    (h1 : ∀ p, 1 ≤ nrm (kz p)) (Mm : Nat) (hM : ∀ p, p ∈ l → nrm (kz p) ≤ Mm + 2) (Rc t0 : Nat) (hRM : Rc ≤ Mm + 1)
    (ι h ν Zb : R) (hh : O.nn h) (hν : O.nn ν) (hν0 : ν ≠ K.zero) (hZb : O.nn Zb)
    (a N : Nat → I → V3 R)
    (hN0 : ∀ t, Le K O.toCone (lsum K (fun r => dot3 K (N t r) (N t r)) l)
      (lsum K (fun r => dot3 K (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (a t) (a t) r)
        (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (a t) (a t) r)) l))
    (hN : ∀ t, Le K O.toCone (lsum K (fun r => K.mul (wt K kz r) (dot3 K (N t r) (N t r))) l)
      (lsum K (fun r => K.mul (wt K kz r) (dot3 K (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (a t) (a t) r)
        (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (a t) (a t) r))) l))
    (hs : ∀ t r, Step K h ν (wt K kz r) (a (t + 1) r) (a t r) (N t r))
    (choice : Le K O.toCone (K.mul (EnstLocalGalerkin.cc K ι (EnstLocalGalerkin.C2 K inv Rc t0)) Zb) (K.mul ν ν))
    (hZ0 : K.mul ν Zb = K.add (EnstGlobal.Q K ν h (EnstGalerkin.Zs K l kz a) (EnstGalerkin.As K l kz a) 0)
      (EnstGlobal.Q K ν h (EnstGalerkin.Zs K l kz a) (EnstGalerkin.As K l kz a) 0))
    (n : Nat) (hn : Le K O.toCone (K.mul (castN K n) (K.mul h (K.mul (EnstLocalGalerkin.cc K ι (EnstLocalGalerkin.C1 K inv Rc))
      (K.mul Zb Zb)))) (EnstGlobal.Q K ν h (EnstGalerkin.Zs K l kz a) (EnstGalerkin.As K l kz a) 0))
    (e D1 D2 D3 : R) (he : O.nn e) (hD1 : O.nn D1) (hD2 : O.nn D2) (hD3 : O.nn D3)
    (H1 : Le K O.toCone (K.mul (AgmonAbsorb.c3 K (castN K 16)) (AgmonAbsorb.q5 K Zb))
      (K.mul (AgmonAbsorb.c3 K (castN K 9)) (K.mul (RootYoung.four K) (K.mul (AgmonAbsorb.c3 K e) D1))))
    (H2 : Le K O.toCone (K.mul (castN K 16) (AgmonAbsorb.q5 K Zb))
      (K.mul (castN K 9) (K.mul (RootYoung.four K) (K.mul (AgmonAbsorb.c3 K e) D2))))
    (H3 : Le K O.toCone (AgmonAbsorb.c3 K Zb) (K.mul (RootYoung.four K) (K.mul e D3)))
    (hε : Le K O.toCone (K.mul (K.mul ι ι) (K.add (K.mul (AgmonCollapse.k1 K (castN K 3) (castN K 9) (castN K 108)) e)
      (K.mul (AgmonCollapse.k2 K (castN K 9) (castN K 108)) (K.add e (K.mul (castN K 2) e))))) (K.mul ν ν))
    (t : Nat) (ht : t ≤ n) :
    Le K O.toCone (EnstGalerkin.Zs K l kz a t) Zb ∧
    Le K O.toCone (H2Linear.En K ν h (K.mul (K.mul ι ι) (K.add (K.mul (AgmonCollapse.k1 K (castN K 3) (castN K 9) (castN K 108)) e)
        (K.mul (AgmonCollapse.k2 K (castN K 9) (castN K 108)) (K.add e (K.mul (castN K 2) e)))))
        (fun s => ForceAbsorb.As K kz l (a s)) (fun s => ForceAbsorb.Bs K kz l (a s)) t)
      (K.add (H2Linear.En K ν h (K.mul (K.mul ι ι) (K.add (K.mul (AgmonCollapse.k1 K (castN K 3) (castN K 9) (castN K 108)) e)
        (K.mul (AgmonCollapse.k2 K (castN K 9) (castN K 108)) (K.add e (K.mul (castN K 2) e)))))
        (fun s => ForceAbsorb.As K kz l (a s)) (fun s => ForceAbsorb.Bs K kz l (a s)) 0)
        (K.mul (castN K t) (K.mul h (K.mul (K.mul ι ι)
          (K.add (K.mul (AgmonCollapse.k1 K (castN K 3) (castN K 9) (castN K 108))
              (K.add D1 (K.mul (AgmonAbsorb.c3 K (castN K 2)) (K.mul Zb Zb))))
            (K.mul (AgmonCollapse.k2 K (castN K 9) (castN K 108)) (K.add D2 (K.mul (castN K 2) D3)))))))) := by
  have loc := fun T (hT : T ≤ n) => EnstLocalTime.local_time K O inv mul_inv h10 l kz hd h1 (Mm + 1) hM Rc t0 hRM
    ι h ν Zb hh hν hν0 hZb a N hN0 hs choice hZ0 n hn T hT
  refine ⟨(loc t ht).2, ?_⟩
  exact H2LocalUniform.h2_local_uniform K O inv mul_inv h10 l kz hd h1 Mm hM ι h ν hh hν a N hN hs Zb e D1 D2 D3
    hZb he hD1 hD2 hD3 H1 H2 H3 hε t (fun s hst => (loc s (Nat.le_trans (Nat.le_of_lt hst) ht)).2)

end Gen

end H2Large

#print axioms H2Large.h2_large

ns/H2LargeAll.lean

lean_all/ns/H2LargeAll.lean

/-
  ns/H2LargeAll.lean — T37 (goal G859c2f): the large-data local H² bound without free absorption parameters, over an
  ordered field with inverses. Under the hypotheses of EnstLocalTime.local_time, with ν ≠ 0 and
  S = ι²(k₁ + k₂(1 + 2)) ≠ 0, there are e ≥ 0 and D such that for every t ≤ n
    h2_large_all   Z_t ≤ Zb   and   νA_t + hεB_t ≤ νA_0 + hεB_0 + t·h·D,   ε = ι²(k₁e + k₂(e + 2e)) ≤ ν²
  (e, D in closed form by H2LargeExplicit.params).

  Lean 4 core plus ns/H2Large, ns/H2LargeExplicit.
-/
import ns.H2Large
import ns.H2LargeExplicit

namespace H2LargeAll
open DetChain NSChain DyadicSmallGlobal LibRing DyadicLarge FourierGalerkin GalerkinForce ForceBound ForceSob
  ModeCount CubeCount ForceUniform NormCompare EnstGalerkin ForceTwoScale EnstStep

section Gen
variable {R : Type} (K : CRing R) (O : OrdR K) {I : Type}
variable (inv : R → R) (mul_inv : ∀ y, y ≠ K.zero → K.mul y (inv y) = K.one) (h10 : K.one ≠ K.zero)

include mul_inv h10 in
/-- **The large-data local H² bound, uniform in the truncation, parameters chosen.** -/
theorem h2_large_all (l : List I) (kz : I → Mode) (hd : List.Pairwise (fun a b => kz a ≠ kz b) l)
    (h1 : ∀ p, 1 ≤ nrm (kz p)) (Mm : Nat) (hM : ∀ p, p ∈ l → nrm (kz p) ≤ Mm + 2) (Rc t0 : Nat) (hRM : Rc ≤ Mm + 1)
    (ι h ν Zb : R) (hh : O.nn h) (hν : O.nn ν) (hν0 : ν ≠ K.zero) (hZb : O.nn Zb)
    (a N : Nat → I → V3 R)
    (hN0 : ∀ t, Le K O.toCone (lsum K (fun r => dot3 K (N t r) (N t r)) l)
      (lsum K (fun r => dot3 K (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (a t) (a t) r)
        (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (a t) (a t) r)) l))
    (hN : ∀ t, Le K O.toCone (lsum K (fun r => K.mul (wt K kz r) (dot3 K (N t r) (N t r))) l)
      (lsum K (fun r => K.mul (wt K kz r) (dot3 K (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (a t) (a t) r)
        (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (a t) (a t) r))) l))
    (hs : ∀ t r, Step K h ν (wt K kz r) (a (t + 1) r) (a t r) (N t r))
    (choice : Le K O.toCone (K.mul (EnstLocalGalerkin.cc K ι (EnstLocalGalerkin.C2 K inv Rc t0)) Zb) (K.mul ν ν))
    (hZ0 : K.mul ν Zb = K.add (EnstGlobal.Q K ν h (EnstGalerkin.Zs K l kz a) (EnstGalerkin.As K l kz a) 0)
      (EnstGlobal.Q K ν h (EnstGalerkin.Zs K l kz a) (EnstGalerkin.As K l kz a) 0))
    (n : Nat) (hn : Le K O.toCone (K.mul (castN K n) (K.mul h (K.mul (EnstLocalGalerkin.cc K ι (EnstLocalGalerkin.C1 K inv Rc))
      (K.mul Zb Zb)))) (EnstGlobal.Q K ν h (EnstGalerkin.Zs K l kz a) (EnstGalerkin.As K l kz a) 0))
    (h9 : castN K 9 ≠ K.zero)
    (S : R) (hS : S = K.mul (K.mul ι ι) (K.add (AgmonCollapse.k1 K (castN K 3) (castN K 9) (castN K 108)) (K.mul (AgmonCollapse.k2 K (castN K 9) (castN K 108)) (K.add K.one (castN K 2)))))
    (hSn : O.nn S) (hS0 : S ≠ K.zero) :
    ∃ e D : R, O.nn e ∧ ∀ t, t ≤ n →
      Le K O.toCone (EnstGalerkin.Zs K l kz a t) Zb ∧
      Le K O.toCone (H2Linear.En K ν h (K.mul (K.mul ι ι) (K.add (K.mul (AgmonCollapse.k1 K (castN K 3) (castN K 9) (castN K 108)) e) (K.mul (AgmonCollapse.k2 K (castN K 9) (castN K 108)) (K.add e (K.mul (castN K 2) e)))))
          (fun s => ForceAbsorb.As K kz l (a s)) (fun s => ForceAbsorb.Bs K kz l (a s)) t)
        (K.add (H2Linear.En K ν h (K.mul (K.mul ι ι) (K.add (K.mul (AgmonCollapse.k1 K (castN K 3) (castN K 9) (castN K 108)) e) (K.mul (AgmonCollapse.k2 K (castN K 9) (castN K 108)) (K.add e (K.mul (castN K 2) e)))))
          (fun s => ForceAbsorb.As K kz l (a s)) (fun s => ForceAbsorb.Bs K kz l (a s)) 0)
          (K.mul (castN K t) (K.mul h D))) := by
  have ⟨e, D1, D2, D3, he, hD1, hD2, hD3, H1, H2, H3, hε⟩ :=
    H2LargeExplicit.params K O inv mul_inv ι ν Zb h10 hν0 hZb h9 S hS hSn hS0
  exact ⟨e, _, he, fun t ht => H2Large.h2_large K O inv mul_inv h10 l kz hd h1 Mm hM Rc t0 hRM ι h ν Zb hh hν hν0 hZb
    a N hN0 hN hs choice hZ0 n hn e D1 D2 D3 he hD1 hD2 hD3 H1 H2 H3 hε t ht⟩

end Gen

end H2LargeAll

#print axioms H2LargeAll.h2_large_all

ns/H2LargeExplicit.lean

lean_all/ns/H2LargeExplicit.lean

/-
  ns/H2LargeExplicit.lean — T37 (goal G859c2e, piece b): the absorption parameters of H2Large in closed form, over an
  ordered field with inverses. For S = ι²(k₁ + k₂(1 + 2)) ≠ 0 and ν ≠ 0, with
    e = ν²·S⁻¹,  D₁ = 16³Zb⁵·(9³·4e³)⁻¹,  D₂ = 16Zb⁵·(9·4e³)⁻¹,  D₃ = Zb³·(4e)⁻¹,
  the hypotheses H1, H2, H3 and ε ≤ ν² of H2Large.h2_large hold (h1_eq, h2_eq, h3_eq, eps_eq); so the local H² bound
  holds with D = ι²(k₁(D₁ + 8Zb²) + k₂(D₂ + 2D₃)), a polynomial in Zb, ι, ν⁻¹.

  Lean 4 core plus ns/ChooseAbsorb.
-/
import ns.ChooseAbsorb
import ns.ForceAgmonW
import ns.AgmonCollapse

namespace H2LargeExplicit
open DetChain NSChain DyadicSmallGlobal LibRing DyadicLarge RootYoung AgmonAbsorb ChooseAbsorb

section Gen
variable {R : Type} (K : CRing R) (O : OrdR K)
variable (inv : R → R) (mul_inv : ∀ y, y ≠ K.zero → K.mul y (inv y) = K.one)

include O mul_inv in
/-- the inverse of a nonnegative nonzero element is nonnegative -/
theorem inv_nn (y : R) (h10 : K.one ≠ K.zero) (hy : O.nn y) (hy0 : y ≠ K.zero) : O.nn (inv y) := by
  cases O.total K.zero (inv y) with
  | inl h =>
    have ⟨d, hd, e⟩ := h
    have e' : d = inv y := by rw [← e]; lib_ring
    rw [← e']; exact hd
  | inr h =>
    have ⟨d, hd, e⟩ := h
    have hm := mul_inv y hy0
    have e2 : K.add K.one (K.mul y d) = K.zero := by
      calc K.add K.one (K.mul y d) = K.add (K.mul y (inv y)) (K.mul y d) := by rw [hm]
        _ = K.mul y (K.add (inv y) d) := by lib_ring
        _ = K.zero := by rw [e]; lib_ring
    exact absurd (nn_sum_zero K O _ _ (RatModel.nn_one' K O.toCone) (O.nn_mul _ _ hy hd) e2) h10

def Dfor (c m e Z : R) : R := K.mul (K.mul c Z) (inv (K.mul m (K.mul (four K) (c3 K e))))

include O mul_inv in
theorem dfor_nn (c m e Z : R) (h10 : K.one ≠ K.zero) (hc : O.nn c) (hm : O.nn m) (he : O.nn e) (hZ : O.nn Z)
    (hm0 : m ≠ K.zero) (he0 : e ≠ K.zero) : O.nn (Dfor K inv c m e Z) := by
  have hX : K.mul m (K.mul (four K) (c3 K e)) ≠ K.zero :=
    nz_mul K O _ _ hm0 (nz_mul K O _ _ (four_ne K O h10) (nz_mul K O _ _ (nz_mul K O _ _ he0 he0) he0))
  have nX : O.nn (K.mul m (K.mul (four K) (c3 K e))) :=
    O.nn_mul _ _ hm (O.nn_mul _ _ (four_nn K O) (O.nn_mul _ _ (O.nn_sq _) he))
  exact O.nn_mul _ _ (O.nn_mul _ _ hc hZ) (inv_nn K O inv mul_inv _ h10 nX hX)

include O mul_inv in
theorem h1_eq (e Zb : R) (h10 : K.one ≠ K.zero) (he : e ≠ K.zero) (h9 : castN K 9 ≠ K.zero) :
    Le K O.toCone (K.mul (c3 K (castN K 16)) (q5 K Zb))
      (K.mul (c3 K (castN K 9)) (K.mul (four K) (K.mul (c3 K e) (Dfor K inv (c3 K (castN K 16)) (c3 K (castN K 9)) e (q5 K Zb))))) :=
  choose_H K O inv mul_inv _ _ e _ h10 (nz_mul K O _ _ (nz_mul K O _ _ h9 h9) h9) he

include O mul_inv in
theorem h2_eq (e Zb : R) (h10 : K.one ≠ K.zero) (he : e ≠ K.zero) (h9 : castN K 9 ≠ K.zero) :
    Le K O.toCone (K.mul (castN K 16) (q5 K Zb))
      (K.mul (castN K 9) (K.mul (four K) (K.mul (c3 K e) (Dfor K inv (castN K 16) (castN K 9) e (q5 K Zb))))) :=
  choose_H K O inv mul_inv _ _ e _ h10 h9 he

include O mul_inv in
theorem h3_eq (e Zb : R) (h10 : K.one ≠ K.zero) (he : e ≠ K.zero) :
    Le K O.toCone (c3 K Zb) (K.mul (four K) (K.mul e (K.mul (c3 K Zb) (inv (K.mul (four K) e))))) := by
  have hX : K.mul (four K) e ≠ K.zero := nz_mul K O _ _ (four_ne K O h10) he
  have f := fill K inv mul_inv _ (c3 K Zb) hX
  refine ⟨K.zero, O.nn_zero, ?_⟩
  calc K.add (c3 K Zb) K.zero = c3 K Zb := by lib_ring
    _ = K.mul (K.mul (four K) e) (K.mul (c3 K Zb) (inv (K.mul (four K) e))) := f.symm
    _ = _ := by lib_ring

include O mul_inv in
/-- **The absorption parameters of H2Large exist (in closed form).** -/
theorem params (ι ν Zb : R) (h10 : K.one ≠ K.zero) (hν0 : ν ≠ K.zero) (hZb : O.nn Zb) (h9 : castN K 9 ≠ K.zero)
    (S : R) (hS : S = K.mul (K.mul ι ι) (K.add (AgmonCollapse.k1 K (castN K 3) (castN K 9) (castN K 108))
      (K.mul (AgmonCollapse.k2 K (castN K 9) (castN K 108)) (K.add K.one (castN K 2)))))
    (hSn : O.nn S) (hS0 : S ≠ K.zero) :
    ∃ e D1 D2 D3 : R, O.nn e ∧ O.nn D1 ∧ O.nn D2 ∧ O.nn D3 ∧
      Le K O.toCone (K.mul (c3 K (castN K 16)) (q5 K Zb)) (K.mul (c3 K (castN K 9)) (K.mul (four K) (K.mul (c3 K e) D1))) ∧
      Le K O.toCone (K.mul (castN K 16) (q5 K Zb)) (K.mul (castN K 9) (K.mul (four K) (K.mul (c3 K e) D2))) ∧
      Le K O.toCone (c3 K Zb) (K.mul (four K) (K.mul e D3)) ∧
      Le K O.toCone (K.mul (K.mul ι ι) (K.add (K.mul (AgmonCollapse.k1 K (castN K 3) (castN K 9) (castN K 108)) e)
        (K.mul (AgmonCollapse.k2 K (castN K 9) (castN K 108)) (K.add e (K.mul (castN K 2) e))))) (K.mul ν ν) := by
  have hiS : inv S ≠ K.zero := by
    intro h; have m := mul_inv S hS0; rw [h] at m; exact h10 (by rw [← m]; lib_ring)
  have he0 : K.mul (K.mul ν ν) (inv S) ≠ K.zero := nz_mul K O _ _ (nz_mul K O _ _ hν0 hν0) hiS
  have hen : O.nn (K.mul (K.mul ν ν) (inv S)) := O.nn_mul _ _ (O.nn_sq ν) (inv_nn K O inv mul_inv S h10 hSn hS0)
  have n16 := RatModel.nn_cast K O.toCone 16
  have n9 := RatModel.nn_cast K O.toCone 9
  have hZ5 : O.nn (q5 K Zb) := O.nn_mul _ _ hZb (O.nn_mul _ _ (O.nn_sq _) (O.nn_sq _))
  have hX3 : K.mul (four K) (K.mul (K.mul ν ν) (inv S)) ≠ K.zero := nz_mul K O _ _ (four_ne K O h10) he0
  refine ⟨K.mul (K.mul ν ν) (inv S),
    Dfor K inv (c3 K (castN K 16)) (c3 K (castN K 9)) (K.mul (K.mul ν ν) (inv S)) (q5 K Zb),
    Dfor K inv (castN K 16) (castN K 9) (K.mul (K.mul ν ν) (inv S)) (q5 K Zb),
    K.mul (c3 K Zb) (inv (K.mul (four K) (K.mul (K.mul ν ν) (inv S)))), hen,
    dfor_nn K O inv mul_inv _ _ _ _ h10 (O.nn_mul _ _ (O.nn_sq _) n16) (O.nn_mul _ _ (O.nn_sq _) n9) hen hZ5
      (nz_mul K O _ _ (nz_mul K O _ _ h9 h9) h9) he0,
    dfor_nn K O inv mul_inv _ _ _ _ h10 n16 n9 hen hZ5 h9 he0,
    O.nn_mul _ _ (O.nn_mul _ _ (O.nn_sq _) hZb)
      (inv_nn K O inv mul_inv _ h10 (O.nn_mul _ _ (four_nn K O) hen) hX3),
    h1_eq K O inv mul_inv _ Zb h10 he0 h9, h2_eq K O inv mul_inv _ Zb h10 he0 h9, h3_eq K O inv mul_inv _ Zb h10 he0, ?_⟩
  have c := choose_eps K O inv mul_inv S ν hS0
  refine le_trans' K O.toCone _ _ _ (ForceAgmonW.le_eq K O _ _ ?_) c
  rw [hS]; lib_ring

end Gen

end H2LargeExplicit

#print axioms H2LargeExplicit.inv_nn
#print axioms H2LargeExplicit.dfor_nn
#print axioms H2LargeExplicit.h1_eq
#print axioms H2LargeExplicit.h2_eq
#print axioms H2LargeExplicit.h3_eq
#print axioms H2LargeExplicit.params

ns/H2LargeLe.lean

lean_all/ns/H2LargeLe.lean

/-
  ns/H2LargeLe.lean — T37 (goal G795b-h, piece b): H2Large.h2_large with the budget Q₀ + n·h·c₁Zb² ≤ ν·Zb in
  place of the equation νZb = 2Q₀ (LocalTimeLe.local_time_le):
    h2_large_le   Z_t ≤ Zb and the window H² energy bound for every t ≤ n,
  so the H² energy bound of every window can be taken with one Zb fixed by a bound Qmax on Q at the window starts.

  Lean 4 core plus ns/H2Large, ns/LocalTimeLe.
-/
import ns.H2Large
import ns.LocalTimeLe

namespace H2LargeLe
open DetChain NSChain DyadicSmallGlobal LibRing DyadicLarge FourierGalerkin GalerkinForce ForceBound ForceSob
  ModeCount CubeCount ForceUniform NormCompare EnstGalerkin ForceTwoScale EnstStep

section Gen
variable {R : Type} (K : CRing R) (O : OrdR K) {I : Type}
variable (inv : R → R) (mul_inv : ∀ y, y ≠ K.zero → K.mul y (inv y) = K.one) (h10 : K.one ≠ K.zero)

include mul_inv h10 in
/-- **H2Large with the budget as an inequality** -/
theorem h2_large_le (l : List I) (kz : I → Mode) (hd : List.Pairwise (fun a b => kz a ≠ kz b) l)
    (h1 : ∀ p, 1 ≤ nrm (kz p)) (Mm : Nat) (hM : ∀ p, p ∈ l → nrm (kz p) ≤ Mm + 2) (Rc t0 : Nat) (hRM : Rc ≤ Mm + 1)
    (ι h ν Zb : R) (hh : O.nn h) (hν : O.nn ν) (hν0 : ν ≠ K.zero) (hZb : O.nn Zb)
    (a N : Nat → I → V3 R)
    (hN0 : ∀ t, Le K O.toCone (lsum K (fun r => dot3 K (N t r) (N t r)) l)
      (lsum K (fun r => dot3 K (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (a t) (a t) r)
        (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (a t) (a t) r)) l))
    (hN : ∀ t, Le K O.toCone (lsum K (fun r => K.mul (wt K kz r) (dot3 K (N t r) (N t r))) l)
      (lsum K (fun r => K.mul (wt K kz r) (dot3 K (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (a t) (a t) r)
        (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (a t) (a t) r))) l))
    (hs : ∀ t r, Step K h ν (wt K kz r) (a (t + 1) r) (a t r) (N t r))
    (choice : Le K O.toCone (K.mul (EnstLocalGalerkin.cc K ι (EnstLocalGalerkin.C2 K inv Rc t0)) Zb) (K.mul ν ν))
    (n : Nat) (budget : Le K O.toCone (K.add (EnstGlobal.Q K ν h (EnstGalerkin.Zs K l kz a) (EnstGalerkin.As K l kz a) 0)
      (K.mul (castN K n) (K.mul h (K.mul (EnstLocalGalerkin.cc K ι (EnstLocalGalerkin.C1 K inv Rc)) (K.mul Zb Zb)))))
      (K.mul ν Zb))
    (e D1 D2 D3 : R) (he : O.nn e) (hD1 : O.nn D1) (hD2 : O.nn D2) (hD3 : O.nn D3)
    (H1 : Le K O.toCone (K.mul (AgmonAbsorb.c3 K (castN K 16)) (AgmonAbsorb.q5 K Zb))
      (K.mul (AgmonAbsorb.c3 K (castN K 9)) (K.mul (RootYoung.four K) (K.mul (AgmonAbsorb.c3 K e) D1))))
    (H2 : Le K O.toCone (K.mul (castN K 16) (AgmonAbsorb.q5 K Zb))
      (K.mul (castN K 9) (K.mul (RootYoung.four K) (K.mul (AgmonAbsorb.c3 K e) D2))))
    (H3 : Le K O.toCone (AgmonAbsorb.c3 K Zb) (K.mul (RootYoung.four K) (K.mul e D3)))
    (hε : Le K O.toCone (K.mul (K.mul ι ι) (K.add (K.mul (AgmonCollapse.k1 K (castN K 3) (castN K 9) (castN K 108)) e)
      (K.mul (AgmonCollapse.k2 K (castN K 9) (castN K 108)) (K.add e (K.mul (castN K 2) e))))) (K.mul ν ν))
    (t : Nat) (ht : t ≤ n) :
    Le K O.toCone (EnstGalerkin.Zs K l kz a t) Zb ∧
    Le K O.toCone (H2Linear.En K ν h (K.mul (K.mul ι ι) (K.add (K.mul (AgmonCollapse.k1 K (castN K 3) (castN K 9) (castN K 108)) e)
        (K.mul (AgmonCollapse.k2 K (castN K 9) (castN K 108)) (K.add e (K.mul (castN K 2) e)))))
        (fun s => ForceAbsorb.As K kz l (a s)) (fun s => ForceAbsorb.Bs K kz l (a s)) t)
      (K.add (H2Linear.En K ν h (K.mul (K.mul ι ι) (K.add (K.mul (AgmonCollapse.k1 K (castN K 3) (castN K 9) (castN K 108)) e)
        (K.mul (AgmonCollapse.k2 K (castN K 9) (castN K 108)) (K.add e (K.mul (castN K 2) e)))))
        (fun s => ForceAbsorb.As K kz l (a s)) (fun s => ForceAbsorb.Bs K kz l (a s)) 0)
        (K.mul (castN K t) (K.mul h (K.mul (K.mul ι ι)
          (K.add (K.mul (AgmonCollapse.k1 K (castN K 3) (castN K 9) (castN K 108))
              (K.add D1 (K.mul (AgmonAbsorb.c3 K (castN K 2)) (K.mul Zb Zb))))
            (K.mul (AgmonCollapse.k2 K (castN K 9) (castN K 108)) (K.add D2 (K.mul (castN K 2) D3)))))))) := by
  have loc := fun T (hT : T ≤ n) => LocalTimeLe.local_time_le K O inv mul_inv h10 l kz hd h1 (Mm + 1) hM Rc t0 hRM
    ι h ν Zb hh hν hν0 hZb a N hN0 hs choice n budget T hT
  refine ⟨loc t ht, ?_⟩
  exact H2LocalUniform.h2_local_uniform K O inv mul_inv h10 l kz hd h1 Mm hM ι h ν hh hν a N hN hs Zb e D1 D2 D3
    hZb he hD1 hD2 hD3 H1 H2 H3 hε t (fun s hst => loc s (Nat.le_trans (Nat.le_of_lt hst) ht))

end Gen

end H2LargeLe

#print axioms H2LargeLe.h2_large_le

ns/H2Linear.lean

lean_all/ns/H2Linear.lean

/-
  ns/H2Linear.lean — T37 (goal G859c2c, piece a): linear growth of the H² energy of the implicit-viscosity scheme
  once the force is absorbed, over an ordered field (scalar lemma). If, for every step s,
      ν·A_{s+1} + hν²·B_{s+1} ≤ ν·A_s + hε·B_s + h·D      with ε ≤ ν², h ≥ 0, B ≥ 0,
  (EnstStepV.enst_stepV with ForceAbsorb.force_absorb: ε = ι²(k₁e₁ + k₂e₂ + 2k₂e₃), D = D(Z_s) bounded on the
  EnstLocalTime interval), then E_s = ν·A_s + hε·B_s satisfies
    h2_linear   E_n ≤ E_0 + n·h·D,
  i.e. the H² norm grows at most linearly on [0, t], uniformly in the truncation.

  Lean 4 core plus ns/ForceSob, ns/DyadicLarge.
-/
import ns.ForceSob
import ns.DyadicLarge

namespace H2Linear
open DetChain NSChain DyadicSmallGlobal LibRing DyadicLarge

section Gen
variable {R : Type} (K : CRing R) (O : OrdR K)

theorem le_eq (x y : R) (h : x = y) : Le K O.toCone x y := by
  rw [h]; exact le_refl' K O.toCone y

def En (ν h ε : R) (A B : Nat → R) (s : Nat) : R := K.add (K.mul ν (A s)) (K.mul (K.mul h ε) (B s))

/-- **Linear growth of the H² energy.** -/
theorem h2_linear (ν h ε D : R) (A B : Nat → R) (hh : O.nn h) (hB : ∀ s, O.nn (B s))
    (hε : Le K O.toCone ε (K.mul ν ν))
    (hs : ∀ s, Le K O.toCone (K.add (K.mul ν (A (s + 1))) (K.mul (K.mul h (K.mul ν ν)) (B (s + 1))))
      (K.add (En K ν h ε A B s) (K.mul h D))) :
    ∀ n, Le K O.toCone (En K ν h ε A B n) (K.add (En K ν h ε A B 0) (K.mul (castN K n) (K.mul h D)))
  | 0 => ⟨K.zero, O.nn_zero, by show K.add (En K ν h ε A B 0) K.zero = K.add (En K ν h ε A B 0) (K.mul K.zero (K.mul h D)); lib_ring⟩
  | n + 1 => by
    have ih := h2_linear ν h ε D A B hh hB hε hs n
    -- E_{n+1} ≤ ν A_{n+1} + hν² B_{n+1}
    have m1 := le_mul_nn K O.toCone h _ _ hh (ForceSob.le_mul_right' K O.toCone hε (hB (n + 1)))
    have s1 : Le K O.toCone (En K ν h ε A B (n + 1))
        (K.add (K.mul ν (A (n + 1))) (K.mul (K.mul h (K.mul ν ν)) (B (n + 1)))) := by
      refine le_trans' K O.toCone _ _ _ ?_ (le_trans' K O.toCone _ _ _
        (le_add' K O.toCone _ _ _ _ (le_refl' K O.toCone (K.mul ν (A (n + 1)))) m1) ?_)
      · apply le_eq K O; unfold En; lib_ring
      · apply le_eq K O; lib_ring
    have s2 := le_trans' K O.toCone _ _ _ s1 (hs n)
    have s3 := le_add' K O.toCone _ _ _ _ ih (le_refl' K O.toCone (K.mul h D))
    refine le_trans' K O.toCone _ _ _ (le_trans' K O.toCone _ _ _ s2 s3) ?_
    apply le_eq K O
    show K.add (K.add (En K ν h ε A B 0) (K.mul (castN K n) (K.mul h D))) (K.mul h D)
      = K.add (En K ν h ε A B 0) (K.mul (K.add K.one (castN K n)) (K.mul h D))
    lib_ring

end Gen

end H2Linear

#print axioms H2Linear.le_eq
#print axioms H2Linear.h2_linear

ns/H2LocalUniform.lean

lean_all/ns/H2LocalUniform.lean

/-
  ns/H2LocalUniform.lean — T37 (goal G859c2c, piece b): the H² energy of the implicit-viscosity Galerkin scheme grows at
  most linearly while the enstrophy stays below Zb, uniformly in the truncation, over an ordered field. For integer
  modes (nonzero, distinct on l, |k|_∞ ≤ Mm + 2), a force with Σw|N|² ≤ Σw|Nl|², parameters e, D₁, D₂, D₃ ≥ 0 with
  16³Zb⁵ ≤ 9³·4e³D₁, 16Zb⁵ ≤ 9·4e³D₂, Zb³ ≤ 4eD₃ and ε = ι²(k₁e + k₂(e + 2·e)) ≤ ν²:
    h2_local_uniform   Z_s ≤ Zb for s < n  ⇒  νA_n + hεB_n ≤ νA_0 + hεB_0 + n·h·D,
      D = ι²(k₁(D₁ + 8Zb²) + k₂(D₂ + 2D₃)).
  EnstStepV.enst_stepV (the step), ForceAbsorb.force_absorb (the force), and the bounded form of H2Linear.

  Lean 4 core plus ns/ForceAbsorb, ns/H2Linear, ns/EnstStepV.
-/
import ns.ForceAbsorb
import ns.H2Linear
import ns.EnstStepV

namespace H2LocalUniform
open DetChain NSChain DyadicSmallGlobal LibRing DyadicLarge FourierGalerkin GalerkinForce ForceBound ForceSob
  ModeCount CubeCount ForceUniform NormCompare EnstGalerkin ForceTwoScale ForceSobW CastZAdd WeightH2 LatticeSum
  RootYoung AgmonAbsorb AgmonCollapse ForceAbsorb EnstStep EnstStepV

section Gen
variable {R : Type} (K : CRing R) (O : OrdR K) {I : Type}
variable (inv : R → R) (mul_inv : ∀ y, y ≠ K.zero → K.mul y (inv y) = K.one) (h10 : K.one ≠ K.zero)

theorem q5_mono (x y : R) (hx : O.nn x) (hy : O.nn y) (h : Le K O.toCone x y) : Le K O.toCone (q5 K x) (q5 K y) := by
  have s2 := EnstLocal.sq_mono K O _ _ hx hy h
  have s4 := EnstLocal.sq_mono K O _ _ (O.nn_sq x) (O.nn_sq y) s2
  exact WeightedCS.le_mul_both' K O.toCone _ _ _ _ hx (O.nn_mul _ _ (O.nn_sq _) (O.nn_sq _)) h s4

/-- the bounded linear-growth lemma -/
theorem lin_upto (ν h ε D : R) (A B : Nat → R) (hh : O.nn h) (hB : ∀ s, O.nn (B s))
    (hε : Le K O.toCone ε (K.mul ν ν)) (n : Nat)
    (hs : ∀ s, s < n → Le K O.toCone (K.add (K.mul ν (A (s + 1))) (K.mul (K.mul h (K.mul ν ν)) (B (s + 1))))
      (K.add (H2Linear.En K ν h ε A B s) (K.mul h D))) :
    ∀ m, m ≤ n → Le K O.toCone (H2Linear.En K ν h ε A B m)
      (K.add (H2Linear.En K ν h ε A B 0) (K.mul (castN K m) (K.mul h D)))
  | 0, _ => ⟨K.zero, O.nn_zero, by
      show K.add (H2Linear.En K ν h ε A B 0) K.zero = K.add (H2Linear.En K ν h ε A B 0) (K.mul K.zero (K.mul h D)); lib_ring⟩
  | m + 1, hm => by
    have ih := lin_upto ν h ε D A B hh hB hε n hs m (Nat.le_of_succ_le hm)
    have m1 := le_mul_nn K O.toCone h _ _ hh (ForceSob.le_mul_right' K O.toCone hε (hB (m + 1)))
    have s1 : Le K O.toCone (H2Linear.En K ν h ε A B (m + 1))
        (K.add (K.mul ν (A (m + 1))) (K.mul (K.mul h (K.mul ν ν)) (B (m + 1)))) := by
      refine le_trans' K O.toCone _ _ _ ?_ (le_trans' K O.toCone _ _ _
        (le_add' K O.toCone _ _ _ _ (le_refl' K O.toCone (K.mul ν (A (m + 1)))) m1) ?_)
      · apply H2Linear.le_eq K O; unfold H2Linear.En; lib_ring
      · apply H2Linear.le_eq K O; lib_ring
    have s2 := le_trans' K O.toCone _ _ _ s1 (hs m hm)
    have s3 := le_add' K O.toCone _ _ _ _ ih (le_refl' K O.toCone (K.mul h D))
    refine le_trans' K O.toCone _ _ _ (le_trans' K O.toCone _ _ _ s2 s3) ?_
    apply H2Linear.le_eq K O
    show K.add (K.add (H2Linear.En K ν h ε A B 0) (K.mul (castN K m) (K.mul h D))) (K.mul h D)
      = K.add (H2Linear.En K ν h ε A B 0) (K.mul (K.add K.one (castN K m)) (K.mul h D))
    lib_ring

include mul_inv h10 in
/-- **The H² energy grows at most linearly while Z ≤ Zb, uniformly in the truncation.** -/
theorem h2_local_uniform (l : List I) (kz : I → Mode) (hd : List.Pairwise (fun a b => kz a ≠ kz b) l)
    (h1 : ∀ p, 1 ≤ nrm (kz p)) (Mm : Nat) (hM : ∀ p, p ∈ l → nrm (kz p) ≤ Mm + 2)
    (ι h ν : R) (hh : O.nn h) (hν : O.nn ν) (a N : Nat → I → V3 R)
    (hN : ∀ t, Le K O.toCone (lsum K (fun r => K.mul (wt K kz r) (dot3 K (N t r) (N t r))) l)
      (lsum K (fun r => K.mul (wt K kz r) (dot3 K (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (a t) (a t) r)
        (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (a t) (a t) r))) l))
    (hs : ∀ t r, Step K h ν (wt K kz r) (a (t + 1) r) (a t r) (N t r))
    (Zb e D1 D2 D3 : R) (hZb : O.nn Zb) (he : O.nn e) (hD1 : O.nn D1) (hD2 : O.nn D2) (hD3 : O.nn D3)
    (H1 : Le K O.toCone (K.mul (c3 K (castN K 16)) (q5 K Zb)) (K.mul (c3 K (castN K 9)) (K.mul (four K) (K.mul (c3 K e) D1))))
    (H2 : Le K O.toCone (K.mul (castN K 16) (q5 K Zb)) (K.mul (castN K 9) (K.mul (four K) (K.mul (c3 K e) D2))))
    (H3 : Le K O.toCone (c3 K Zb) (K.mul (four K) (K.mul e D3)))
    (hε : Le K O.toCone (K.mul (K.mul ι ι) (K.add (K.mul (k1 K (castN K 3) (castN K 9) (castN K 108)) e)
      (K.mul (k2 K (castN K 9) (castN K 108)) (K.add e (K.mul (castN K 2) e))))) (K.mul ν ν))
    (n : Nat) (hZ : ∀ s, s < n → Le K O.toCone (Zs K kz l (a s)) Zb) :
    Le K O.toCone (H2Linear.En K ν h (K.mul (K.mul ι ι) (K.add (K.mul (k1 K (castN K 3) (castN K 9) (castN K 108)) e)
        (K.mul (k2 K (castN K 9) (castN K 108)) (K.add e (K.mul (castN K 2) e))))) (fun s => As K kz l (a s)) (fun s => Bs K kz l (a s)) n)
      (K.add (H2Linear.En K ν h (K.mul (K.mul ι ι) (K.add (K.mul (k1 K (castN K 3) (castN K 9) (castN K 108)) e)
        (K.mul (k2 K (castN K 9) (castN K 108)) (K.add e (K.mul (castN K 2) e))))) (fun s => As K kz l (a s)) (fun s => Bs K kz l (a s)) 0)
        (K.mul (castN K n) (K.mul h (K.mul (K.mul ι ι)
          (K.add (K.mul (k1 K (castN K 3) (castN K 9) (castN K 108)) (K.add D1 (K.mul (c3 K (castN K 2)) (K.mul Zb Zb))))
            (K.mul (k2 K (castN K 9) (castN K 108)) (K.add D2 (K.mul (castN K 2) D3)))))))) := by
  have hw : ∀ r, O.nn (wt K kz r) := fun r => dot3_nn K O.toCone _
  have nBs : ∀ s, O.nn (Bs K kz l (a s)) := fun s =>
    lsum_nn K O.toCone _ (fun q => O.nn_mul _ _ (O.nn_mul _ _ (O.nn_sq _) (dot3_nn K O.toCone _)) (dot3_nn K O.toCone _)) l
  refine lin_upto K O ν h _ _ (fun s => As K kz l (a s)) (fun s => Bs K kz l (a s)) hh nBs hε n ?_ n (Nat.le_refl n)
  intro s hsn
  have nZ : O.nn (Zs K kz l (a s)) :=
    lsum_nn K O.toCone _ (fun q => O.nn_mul _ _ (dot3_nn K O.toCone _) (dot3_nn K O.toCone _)) l
  have hZs := hZ s hsn
  have n16 := RatModel.nn_cast K O.toCone 16
  have n9 := RatModel.nn_cast K O.toCone 9
  have q5m := q5_mono K O _ _ nZ hZb hZs
  have H1' := le_trans' K O.toCone _ _ _ (le_mul_nn K O.toCone (c3 K (castN K 16)) _ _ (O.nn_mul _ _ (O.nn_sq _) n16) q5m) H1
  have H2' := le_trans' K O.toCone _ _ _ (le_mul_nn K O.toCone _ _ _ n16 q5m) H2
  have H3' := le_trans' K O.toCone _ _ _ (cube_mono K O _ _ nZ hZb hZs) H3
  have F := force_absorb K O inv mul_inv h10 l kz hd h1 ι (a s) Mm hM e D1 e D2 e D3 he hD1 he hD2 he hD3 H1' H2' H3'
  have st := enst_stepV K O.toCone l h ν hh hν (wt K kz) (wt K kz) hw hw (a (s + 1)) (a s) (N s) (hs s)
  -- Z_s² ≤ Zb² inside the force bound
  have z2 := EnstLocal.sq_mono K O _ _ nZ hZb hZs
  have nk1 : O.nn (k1 K (castN K 3) (castN K 9) (castN K 108)) := by
    unfold k1 two
    exact O.nn_mul _ _ (O.nn_add _ _ (RatModel.nn_one' K O.toCone) n9)
      (O.nn_mul _ _ (O.nn_add _ _ (RatModel.nn_one' K O.toCone) (RatModel.nn_one' K O.toCone))
        (O.nn_mul _ _ (RatModel.nn_cast K O.toCone 3)
          (O.nn_add _ _ (RatModel.nn_cast K O.toCone 108) (RatModel.nn_cast K O.toCone 108))))
  have n8 : O.nn (c3 K (castN K 2)) := O.nn_mul _ _ (O.nn_sq _) (RatModel.nn_cast K O.toCone 2)
  have z8 := le_mul_nn K O.toCone (c3 K (castN K 2)) _ _ n8 z2
  have g1 := le_mul_nn K O.toCone _ _ _ nk1
    (le_add' K O.toCone _ _ _ _ (le_refl' K O.toCone (K.add (K.mul e (Bs K kz l (a s))) D1)) z8)
  have G := le_mul_nn K O.toCone _ _ _ (O.nn_sq ι)
    (le_add' K O.toCone _ _ _ _ g1 (le_refl' K O.toCone (K.mul (k2 K (castN K 9) (castN K 108))
      (K.add (K.add (K.mul e (Bs K kz l (a s))) D2) (K.mul (castN K 2) (K.add (K.mul e (Bs K kz l (a s))) D3))))))
  have Fs := le_trans' K O.toCone _ _ _ (hN s) (le_trans' K O.toCone _ _ _ F G)
  have Fh := le_add' K O.toCone _ _ _ _ (le_refl' K O.toCone (K.mul ν (As K kz l (a s)))) (le_mul_nn K O.toCone _ _ _ hh Fs)
  refine le_trans' K O.toCone _ _ _ (le_trans' K O.toCone _ _ _ st Fh) ?_
  apply H2Linear.le_eq K O
  unfold H2Linear.En
  show _ = K.add (K.add (K.mul ν (As K kz l (a s))) (K.mul (K.mul h (K.mul (K.mul ι ι)
      (K.add (K.mul (k1 K (castN K 3) (castN K 9) (castN K 108)) e) (K.mul (k2 K (castN K 9) (castN K 108)) (K.add e (K.mul (castN K 2) e))))))
      (Bs K kz l (a s)))) (K.mul h (K.mul (K.mul ι ι)
          (K.add (K.mul (k1 K (castN K 3) (castN K 9) (castN K 108)) (K.add D1 (K.mul (c3 K (castN K 2)) (K.mul Zb Zb))))
            (K.mul (k2 K (castN K 9) (castN K 108)) (K.add D2 (K.mul (castN K 2) D3))))))
  unfold ForceAbsorb.As ForceAbsorb.Bs
  lib_ring

end Gen

end H2LocalUniform

#print axioms H2LocalUniform.q5_mono
#print axioms H2LocalUniform.lin_upto
#print axioms H2LocalUniform.h2_local_uniform

ns/H2Step.lean

lean_all/ns/H2Step.lean

/-
  ns/H2Step.lean — T37 cycle 1327 (goal G793a): the H² enstrophy step of the implicit-viscosity Galerkin scheme as a
  standalone lemma, over an ordered field. With A = Σw²|a|², B = Σw³|a|², Q₂ = νA + hν²B, cH = 1080 and any force with
  Σw|N|² ≤ Σw|Nl|² (Leray projection):
    h2_step   Q₂(s + 1) ≤ νA_s + h·(cH ι²)·B_s·A_s
  (the step inside EnstH2.h2_global, exposed so that EnstDiss can bound the dissipated H³ mass).

  Lean 4 core plus ns/EnstH2.
-/
import ns.EnstH2

namespace H2Step
open DetChain NSChain DyadicSmallGlobal LibRing DyadicLarge FourierGalerkin GalerkinForce ForceBound ForceSob
  ModeCount CubeCount ForceUniform NormCompare EnstGalerkin ForceTwoScale ForceSobW CastZAdd WeightH2 LatticeSum
  LatticeBound ForceH2 EnstStep EnstStepV EnstGlobal EnstH2

section Gen
variable {R : Type} (K : CRing R) (O : OrdR K) {I : Type}
variable (inv : R → R) (mul_inv : ∀ y, y ≠ K.zero → K.mul y (inv y) = K.one) (h10 : K.one ≠ K.zero)

include mul_inv h10 in
theorem h2_step (l : List I) (kz : I → Mode) (hd : List.Pairwise (fun a b => kz a ≠ kz b) l)
    (h1 : ∀ p, 1 ≤ nrm (kz p)) (M : Nat) (hM : ∀ p, p ∈ l → nrm (kz p) ≤ M + 1)
    (ι h ν : R) (hh : O.nn h) (hν : O.nn ν) (a N : Nat → I → V3 R)
    (hN : ∀ t, Le K O.toCone (lsum K (fun r => K.mul (wt K kz r) (dot3 K (N t r) (N t r))) l)
      (lsum K (fun r => K.mul (wt K kz r) (dot3 K (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (a t) (a t) r)
        (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (a t) (a t) r))) l))
    (hs : ∀ t r, Step K h ν (wt K kz r) (a (t + 1) r) (a t r) (N t r)) (s : Nat) :
    Le K O.toCone (Q K ν h (fun t => lsum K (fun p => K.mul (K.mul (wt K kz p) (wt K kz p)) (dot3 K (a t p) (a t p))) l)
        (fun t => lsum K (fun p => K.mul (K.mul (K.mul (wt K kz p) (wt K kz p)) (wt K kz p)) (dot3 K (a t p) (a t p))) l) (s + 1))
      (K.add (K.mul ν (lsum K (fun p => K.mul (K.mul (wt K kz p) (wt K kz p)) (dot3 K (a s p) (a s p))) l))
        (K.mul (K.mul h (K.mul (cH K) (K.mul ι ι)))
          (K.mul (lsum K (fun p => K.mul (K.mul (K.mul (wt K kz p) (wt K kz p)) (wt K kz p)) (dot3 K (a s p) (a s p))) l)
            (lsum K (fun p => K.mul (K.mul (wt K kz p) (wt K kz p)) (dot3 K (a s p) (a s p))) l)))) := by
  have hw : ∀ r, O.nn (wt K kz r) := fun r => dot3_nn K O.toCone _
  have two : O.nn (K.add K.one K.one) := O.nn_add _ _ (RatModel.nn_one' K O.toCone) (RatModel.nn_one' K O.toCone)
  have three : O.nn (K.add (K.add K.one K.one) K.one) := O.nn_add _ _ two (RatModel.nn_one' K O.toCone)
  have n135 : O.nn (castN K 135) := RatModel.nn_cast K O.toCone 135
  have ncH : O.nn (cH K) := O.nn_add _ _ (O.nn_mul _ _ two (O.nn_mul _ _ three n135)) (O.nn_mul _ _ two n135)
  have hc : O.nn (K.mul (cH K) (K.mul ι ι)) := O.nn_mul _ _ ncH (O.nn_sq ι)
  have st := enst_stepV K O.toCone l h ν hh hν (wt K kz) (wt K kz) hw hw (a (s + 1)) (a s) (N s) (hs s)
  have fh := force_h2 K O inv mul_inv h10 l kz hd h1 ι (a s)
  have hS0 := S0_le K O inv mul_inv h10 l kz hd h1 M hM
  have hS1 := S1_le K O inv mul_inv h10 l kz hd h1 M hM
  have hZA := Z_le_A K O kz h1 l (a s)
  have nZ : O.nn (lsum K (fun q => K.mul (wt K kz q) (dot3 K (a s q) (a s q))) l) :=
    lsum_nn K O.toCone _ (fun q => O.nn_mul _ _ (hw q) (dot3_nn K O.toCone _)) l
  have nA : O.nn (lsum K (fun p => K.mul (K.mul (wt K kz p) (wt K kz p)) (dot3 K (a s p) (a s p))) l) :=
    lsum_nn K O.toCone _ (fun p => O.nn_mul _ _ (O.nn_sq _) (dot3_nn K O.toCone _)) l
  have nB : O.nn (lsum K (fun p => K.mul (K.mul (K.mul (wt K kz p) (wt K kz p)) (wt K kz p)) (dot3 K (a s p) (a s p))) l) :=
    lsum_nn K O.toCone _ (fun p => O.nn_mul _ _ (O.nn_mul _ _ (O.nn_sq _) (hw p)) (dot3_nn K O.toCone _)) l
  have nS1 : O.nn (lsum K (fun p => K.mul (K.mul (cube3 K (ivn K inv kz p)) (cube3 K (ivn K inv kz p))) (wt K kz p)) l) :=
    lsum_nn K O.toCone _ (fun p => O.nn_mul _ _ (O.nn_sq _) (hw p)) l
  have nS0 : O.nn (lsum K (fun p => K.mul (cube3 K (ivn K inv kz p)) (cube3 K (ivn K inv kz p))) l) :=
    lsum_nn K O.toCone _ (fun p => O.nn_sq _) l
  -- 2 S1 Z + 2 S0 A ≤ cH · A
  have i1 := TruncRate.mm K O _ _ _ _ nS1 nA hS1 hZA
  have i2 := ForceSob.le_mul_right' K O.toCone hS0 nA
  have inner := le_add' K O.toCone _ _ _ _ (le_mul_nn K O.toCone _ _ _ two i1) (le_mul_nn K O.toCone _ _ _ two i2)
  have b1 := le_trans' K O.toCone _ _ _ (hN s) fh
  have b2 := le_trans' K O.toCone _ _ _ b1 (le_mul_nn K O.toCone _ _ _ (O.nn_sq ι) (le_mul_nn K O.toCone _ _ _ nB inner))
  have hb := le_mul_nn K O.toCone h _ _ hh b2
  have st2 := le_trans' K O.toCone _ _ _ st (le_add' K O.toCone _ _ _ _ (le_refl' K O.toCone _) hb)
  refine le_trans' K O.toCone _ _ _ st2 ⟨K.zero, O.nn_zero, ?_⟩
  unfold cH
  generalize lsum K (fun p => K.mul (K.mul (wt K kz p) (wt K kz p)) (dot3 K (a s p) (a s p))) l = As'
  generalize lsum K (fun p => K.mul (K.mul (K.mul (wt K kz p) (wt K kz p)) (wt K kz p)) (dot3 K (a s p) (a s p))) l = Bs
  generalize castN K 135 = c
  lib_ring

end Gen

end H2Step

#print axioms H2Step.h2_step

ns/H2ToA.lean

lean_all/ns/H2ToA.lean

/-
  ns/H2ToA.lean — T37 (goal G859c2g, piece a): from the H² energy bound to an H² bound, over an ordered field with
  inverses. If ν > 0 (nonnegative, nonzero), h, ε, B ≥ 0 and νA + hεB ≤ X, then
    a_of_E   A ≤ X·ν⁻¹,
  the bound Aa (large truncation) and Ab (small truncation) that TruncRate.trunc_cauchy needs, from H2LargeAll.

  Lean 4 core plus ns/H2LargeExplicit.
-/
import ns.H2LargeExplicit

namespace H2ToA
open DetChain NSChain DyadicSmallGlobal LibRing DyadicLarge

section Gen
variable {R : Type} (K : CRing R) (O : OrdR K)
variable (inv : R → R) (mul_inv : ∀ y, y ≠ K.zero → K.mul y (inv y) = K.one)

include O mul_inv in
/-- **νA + hεB ≤ X ⇒ A ≤ X/ν** -/
theorem a_of_E (ν h ε A B X : R) (h10 : K.one ≠ K.zero) (hν : O.nn ν) (hν0 : ν ≠ K.zero) (hh : O.nn h)
    (hε : O.nn ε) (hB : O.nn B)
    (hE : Le K O.toCone (K.add (K.mul ν A) (K.mul (K.mul h ε) B)) X) : Le K O.toCone A (K.mul X (inv ν)) := by
  have s1 : Le K O.toCone (K.mul ν A) X :=
    le_trans' K O.toCone _ _ _ ⟨K.mul (K.mul h ε) B, O.nn_mul _ _ (O.nn_mul _ _ hh hε) hB, rfl⟩ hE
  have hi := H2LargeExplicit.inv_nn K O inv mul_inv ν h10 hν hν0
  have s2 := le_mul_nn K O.toCone (inv ν) _ _ hi s1
  have m := mul_inv ν hν0
  have e1 : K.mul (inv ν) (K.mul ν A) = A := by
    calc K.mul (inv ν) (K.mul ν A) = K.mul (K.mul ν (inv ν)) A := by lib_ring
      _ = A := by rw [m]; lib_ring
  have e2 : K.mul (inv ν) X = K.mul X (inv ν) := by lib_ring
  rw [e1, e2] at s2
  exact s2

end Gen

end H2ToA

#print axioms H2ToA.a_of_E

ns/H3Step.lean

lean_all/ns/H3Step.lean

/-
  ns/H3Step.lean — T37 cycle 1331 (goal G794a): the H³ enstrophy step of the implicit-viscosity Galerkin scheme as a
  standalone lemma, over an ordered field. With B̃ = Σw(w·w)|a|², B₄ = Σ(w·w)(w·w)|a|², Q₃ = νB̃ + hν²B₄,
  c₃ = 8(9·135 + 135), and any force with Σw²|N|² ≤ Σw²|Nl|²:
    h3_step   Q₃(s + 1) ≤ νB̃_s + h·(c₃ι²)·B₄_s·B̃_s
  (the step inside EnstH3.h3_global, exposed for EnstDiss at the H⁴ stage).

  Lean 4 core plus ns/EnstH3.
-/
import ns.EnstH3

namespace H3Step
open DetChain NSChain DyadicSmallGlobal LibRing DyadicLarge FourierGalerkin GalerkinForce ForceBound ForceSob
  ModeCount CubeCount ForceUniform NormCompare EnstGalerkin ForceTwoScale ForceSobW CastZAdd WeightH2 LatticeSum
  LatticeBound LatticeStep ForceH3 Tri4 EnstStep EnstStepV EnstGlobal EnstH2 EnstH3

section Gen
variable {R : Type} (K : CRing R) (O : OrdR K) {I : Type}
variable (inv : R → R) (mul_inv : ∀ y, y ≠ K.zero → K.mul y (inv y) = K.one) (h10 : K.one ≠ K.zero)

include mul_inv h10 in
theorem h3_step (l : List I) (kz : I → Mode) (hd : List.Pairwise (fun a b => kz a ≠ kz b) l)
    (h1 : ∀ p, 1 ≤ nrm (kz p)) (M : Nat) (hM : ∀ p, p ∈ l → nrm (kz p) ≤ M + 1)
    (ι h ν : R) (hh : O.nn h) (hν : O.nn ν) (a N : Nat → I → V3 R)
    (hN : ∀ t, Le K O.toCone (lsum K (fun r => K.mul (K.mul (wt K kz r) (wt K kz r)) (dot3 K (N t r) (N t r))) l)
      (lsum K (fun r => K.mul (K.mul (wt K kz r) (wt K kz r)) (dot3 K (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (a t) (a t) r)
        (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (a t) (a t) r))) l))
    (hs : ∀ t r, Step K h ν (wt K kz r) (a (t + 1) r) (a t r) (N t r)) (s : Nat) :
    Le K O.toCone (Q K ν h (fun t => lsum K (fun p => K.mul (K.mul (wt K kz p) (K.mul (wt K kz p) (wt K kz p))) (dot3 K (a t p) (a t p))) l)
        (fun t => lsum K (fun p => K.mul (K.mul (K.mul (wt K kz p) (wt K kz p)) (K.mul (wt K kz p) (wt K kz p))) (dot3 K (a t p) (a t p))) l) (s + 1))
      (K.add (K.mul ν (lsum K (fun p => K.mul (K.mul (wt K kz p) (K.mul (wt K kz p) (wt K kz p))) (dot3 K (a s p) (a s p))) l))
        (K.mul (K.mul h (K.mul (c3 K) (K.mul ι ι)))
          (K.mul (lsum K (fun p => K.mul (K.mul (K.mul (wt K kz p) (wt K kz p)) (K.mul (wt K kz p) (wt K kz p))) (dot3 K (a s p) (a s p))) l)
            (lsum K (fun p => K.mul (K.mul (wt K kz p) (K.mul (wt K kz p) (wt K kz p))) (dot3 K (a s p) (a s p))) l)))) := by
  have hw : ∀ r, O.nn (wt K kz r) := fun r => dot3_nn K O.toCone _
  have n135 : O.nn (castN K 135) := RatModel.nn_cast K O.toCone 135
  have nc3 : O.nn (c3 K) := O.nn_mul _ _ (nn8 K O) (O.nn_add _ _ (O.nn_mul _ _ (nn9 K O) n135) n135)
  have hc : O.nn (K.mul (c3 K) (K.mul ι ι)) := O.nn_mul _ _ nc3 (O.nn_sq ι)
  have st := enst_stepV K O.toCone l h ν hh hν (wt K kz) (fun r => K.mul (wt K kz r) (wt K kz r)) hw (fun r => O.nn_sq _)
    (a (s + 1)) (a s) (N s) (hs s)
  have fh := force_h3 K O inv mul_inv h10 l kz hd h1 ι (a s)
  have hS0 := S0h_le K O inv mul_inv h10 l kz hd h1 M hM
  have hS1 := S1h_le K O inv mul_inv h10 l kz hd h1 M hM
  have hZB := Z_le_B K O kz h1 l (a s)
  rw [Bform K kz l (a s)] at hZB fh
  have nB : O.nn (lsum K (fun p => K.mul (K.mul (wt K kz p) (K.mul (wt K kz p) (wt K kz p))) (dot3 K (a s p) (a s p))) l) :=
    lsum_nn K O.toCone _ (fun p => O.nn_mul _ _ (O.nn_mul _ _ (hw p) (O.nn_sq _)) (dot3_nn K O.toCone _)) l
  have nB4 : O.nn (lsum K (fun p => K.mul (K.mul (K.mul (wt K kz p) (wt K kz p)) (K.mul (wt K kz p) (wt K kz p))) (dot3 K (a s p) (a s p))) l) :=
    lsum_nn K O.toCone _ (fun p => O.nn_mul _ _ (O.nn_mul _ _ (O.nn_sq _) (O.nn_sq _)) (dot3_nn K O.toCone _)) l
  have nS1 : O.nn (lsum K (fun p => K.mul (K.mul (p4 K (ivn K inv kz p)) (p4 K (ivn K inv kz p))) (K.mul (wt K kz p) (wt K kz p))) l) :=
    lsum_nn K O.toCone _ (fun p => O.nn_mul _ _ (O.nn_sq _) (O.nn_sq _)) l
  have i1 := TruncRate.mm K O _ _ _ _ nS1 nB hS1 hZB
  have i2 := ForceSob.le_mul_right' K O.toCone hS0 nB
  have inner := le_mul_nn K O.toCone _ _ _ (nn8 K O) (le_add' K O.toCone _ _ _ _ i1 i2)
  have b1 := le_trans' K O.toCone _ _ _ (hN s) fh
  have b2 := le_trans' K O.toCone _ _ _ b1 (le_mul_nn K O.toCone _ _ _ (O.nn_sq ι) (le_mul_nn K O.toCone _ _ _ nB4 inner))
  have hb := le_mul_nn K O.toCone h _ _ hh b2
  have st2 := le_trans' K O.toCone _ _ _ st (le_add' K O.toCone _ _ _ _ (le_refl' K O.toCone _) hb)
  refine le_trans' K O.toCone _ _ _ st2 ⟨K.zero, O.nn_zero, ?_⟩
  unfold c3
  generalize lsum K (fun p => K.mul (K.mul (wt K kz p) (K.mul (wt K kz p) (wt K kz p))) (dot3 K (a s p) (a s p))) l = Bs
  generalize lsum K (fun p => K.mul (K.mul (K.mul (wt K kz p) (wt K kz p)) (K.mul (wt K kz p) (wt K kz p))) (dot3 K (a s p) (a s p))) l = B4
  generalize castN K 135 = c
  generalize k8 K = e8
  generalize k9 K = e9
  lib_ring

end Gen

end H3Step

#print axioms H3Step.h3_step

ns/HolderSmall.lean

lean_all/ns/HolderSmall.lean

/-
  ns/HolderSmall.lean — T37 cycle 1353 (goal G803 — done): Hölder-1/2 equicontinuity in time for small data with no
  H² hypothesis, over an ordered field. Implicit Fourier–Galerkin scheme, Leray-projected force (Σ|N|² ≤ Σ|Nl|²),
  c = 135ι², Q = νZ + hν²A. If 2c·Q_0 ≤ ν³ then for every t and n
    holder_small   ν³·Σ_r |a_{t+n,r} − a_{t,r}|² ≤ n·(4h·(c·Q_0 + ν³)·Q_0),
  i.e. |a(t + τ) − a(t)|²_{L²} ≤ 4τ(cQ_0 + ν³)Q_0/ν³ for τ = nh: a modulus √τ independent of the truncation, using
  only the time-integrated H² norm (EnstDiss) instead of a pointwise H² bound (compare EquiCont).
  Pieces: step_nu (ν·|a_{s+1} − a_s|² ≤ 2h²(cQ_0 + ν³)A_s), then TimeLipN.lip_n and EnstDiss.enst_diss from time t.

  Lean 4 core plus ns/TimeLip, ns/TimeLipN, ns/EnstDiss, ns/EventH2 (proj_step), ns/Dyadic (sumN_le).
-/
import ns.TimeLip
import ns.TimeLipN
import ns.EnstDiss
import ns.EventH2
import ns.Dyadic

namespace HolderSmall
open DetChain NSChain DyadicSmallGlobal LibRing DyadicLarge FourierGalerkin GalerkinForce ForceBound ForceSob
  ModeCount CubeCount ForceUniform NormCompare EnstStep EnstGalerkin EnstGlobal

section Gen
variable {R : Type} (K : CRing R) (O : OrdR K) {I : Type}
variable (inv : R → R) (mul_inv : ∀ y, y ≠ K.zero → K.mul y (inv y) = K.one) (h10 : K.one ≠ K.zero)

include mul_inv h10 in
theorem holder_small (l : List I) (kz : I → Mode) (hd : List.Pairwise (fun a b => kz a ≠ kz b) l)
    (h1 : ∀ p, 1 ≤ nrm (kz p)) (M : Nat) (hM : ∀ p, p ∈ l → nrm (kz p) ≤ M + 1)
    (ι h ν : R) (hh : O.nn h) (hν : O.nn ν) (hν0 : ν ≠ K.zero) (a N : Nat → I → V3 R)
    (hN : ∀ t, Le K O.toCone (lsum K (fun r => dot3 K (N t r) (N t r)) l)
      (lsum K (fun r => dot3 K (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (a t) (a t) r)
        (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (a t) (a t) r)) l))
    (hs : ∀ t r, Step K h ν (wt K kz r) (a (t + 1) r) (a t r) (N t r))
    (small : Le K O.toCone (K.mul (K.add (K.mul (castN K 135) (K.mul ι ι)) (K.mul (castN K 135) (K.mul ι ι)))
      (Q K ν h (Zs K l kz a) (As K l kz a) 0)) (K.mul ν (K.mul ν ν))) (t n : Nat) :
    Le K O.toCone
      (K.mul (K.mul ν (K.mul ν ν)) (lsum K (fun r => dot3 K (EnstDiff.sub3 K (a (t + n) r) (a t r)) (EnstDiff.sub3 K (a (t + n) r) (a t r))) l))
      (K.mul (castN K n) (K.mul (K.mul (K.add (K.add K.one K.one) (K.add K.one K.one)) h)
        (K.mul (K.add (K.mul (K.mul (castN K 135) (K.mul ι ι)) (Q K ν h (Zs K l kz a) (As K l kz a) 0)) (K.mul ν (K.mul ν ν)))
          (Q K ν h (Zs K l kz a) (As K l kz a) 0)))) := by
  have hw : ∀ r, O.nn (wt K kz r) := fun r => dot3_nn K O.toCone _
  have hZ : ∀ t, O.nn (Zs K l kz a t) := fun t => lsum_nn K O.toCone _ (fun r => O.nn_mul _ _ (hw r) (dot3_nn K O.toCone _)) l
  have hA : ∀ t, O.nn (As K l kz a t) := fun t => lsum_nn K O.toCone _ (fun r => O.nn_mul _ _ (O.nn_sq _) (dot3_nn K O.toCone _)) l
  have hc : O.nn (K.mul (castN K 135) (K.mul ι ι)) := O.nn_mul _ _ (RatModel.nn_cast K O.toCone 135) (O.nn_sq ι)
  have two : O.nn (K.add K.one K.one) := O.nn_add _ _ (RatModel.nn_one' K O.toCone) (RatModel.nn_one' K O.toCone)
  generalize hcd : K.mul (castN K 135) (K.mul ι ι) = c at hc small ⊢
  have nQ : ∀ s, O.nn (Q K ν h (Zs K l kz a) (As K l kz a) s) := fun s =>
    O.nn_add _ _ (O.nn_mul _ _ hν (hZ s)) (O.nn_mul _ _ (O.nn_mul _ _ hh (O.nn_sq ν)) (hA s))
  -- the projected step in the form of EnstDiss
  have pstep : ∀ s, Le K O.toCone (Q K ν h (Zs K l kz a) (As K l kz a) (s + 1))
      (K.add (K.mul ν (Zs K l kz a s)) (K.mul (K.mul h c) (K.mul (As K l kz a s) (Zs K l kz a s)))) := by
    intro s; rw [← hcd]
    exact EventH2.proj_step K O inv mul_inv h10 l kz hd h1 M hM ι h ν hh hν a N hN hs s
  -- Σ|N_s|² ≤ c·A_s·Z_s
  have hb : ∀ s, Le K O.toCone (lsum K (fun r => dot3 K (N s r) (N s r)) l) (K.mul c (K.mul (As K l kz a s) (Zs K l kz a s))) := by
    intro s
    have fu := force_uniform K O inv mul_inv h10 l kz hd h1 M hM ι (kv K kz) (a s) (a s)
    have hH : Le K O.toCone (lsum K (fun p => K.mul (K.mul (mu K kz p) (mu K kz p)) (dot3 K (a s p) (a s p))) l) (As K l kz a s) :=
      lsum_le K O.toCone _ _ (fun p => le_mul_right' K O.toCone (nrm4_le K O.toCone (kz p)) (dot3_nn K O.toCone _)) l
    have m1 := le_mul_right' K O.toCone hH (O.nn_mul _ _ (RatModel.nn_cast K O.toCone 135) (hZ s))
    have m2 := le_mul_nn K O.toCone (K.mul ι ι) _ _ (O.nn_sq ι) m1
    refine le_trans' K O.toCone _ _ _ (hN s) (le_trans' K O.toCone _ _ _ fu (le_trans' K O.toCone _ _ _ m2 ⟨K.zero, O.nn_zero, ?_⟩))
    rw [← hcd]; lib_ring
  -- Q_s ≤ Q_0, so ν·Z_s ≤ Q_0
  have small1 : Le K O.toCone (K.mul c (Q K ν h (Zs K l kz a) (As K l kz a) 0)) (K.mul ν (K.mul ν ν)) :=
    le_trans' K O.toCone _ _ _ ⟨K.mul c (Q K ν h (Zs K l kz a) (As K l kz a) 0), O.nn_mul _ _ hc (nQ 0), by lib_ring⟩ small
  have hq : ∀ s, Le K O.toCone (Q K ν h (Zs K l kz a) (As K l kz a) s) (Q K ν h (Zs K l kz a) (As K l kz a) 0) :=
    q_le K O ν h c (Zs K l kz a) (As K l kz a) hν hν0 hh hc hZ hA pstep small1
  have hz : ∀ s, Le K O.toCone (K.mul ν (Zs K l kz a s)) (Q K ν h (Zs K l kz a) (As K l kz a) 0) := fun s =>
    le_trans' K O.toCone _ _ _ ⟨K.mul (K.mul h (K.mul ν ν)) (As K l kz a s), O.nn_mul _ _ (O.nn_mul _ _ hh (O.nn_sq ν)) (hA s), rfl⟩ (hq s)
  generalize Q K ν h (Zs K l kz a) (As K l kz a) 0 = Q0 at hz hq small ⊢
  -- one step: ν·D_s ≤ 2h²(cQ_0 + ν³)·A_s
  have step_nu : ∀ s, Le K O.toCone
      (K.mul ν (lsum K (fun r => dot3 K (EnstDiff.sub3 K (a (s + 1) r) (a s r)) (EnstDiff.sub3 K (a (s + 1) r) (a s r))) l))
      (K.mul (K.mul (K.mul (K.add K.one K.one) (K.mul h h)) (K.add (K.mul c Q0) (K.mul ν (K.mul ν ν)))) (As K l kz a s)) := by
    intro s
    have tl := TimeLip.time_lip K O.toCone l h ν hh hν (wt K kz) hw (a (s + 1)) (a s) (N s) (hs s)
    have m := le_mul_nn K O.toCone ν _ _ hν tl
    have k1 := le_mul_nn K O.toCone (K.mul ν (K.mul (K.add K.one K.one) (K.mul h h))) _ _
      (O.nn_mul _ _ hν (O.nn_mul _ _ two (O.nn_sq h))) (hb s)
    have k2 := le_mul_nn K O.toCone (K.mul (K.mul (K.add K.one K.one) (K.mul h h)) (K.mul c (As K l kz a s))) _ _
      (O.nn_mul _ _ (O.nn_mul _ _ two (O.nn_sq h)) (O.nn_mul _ _ hc (hA s))) (hz s)
    refine le_trans' K O.toCone _ _ _ m (le_trans' K O.toCone _ _ _ ⟨K.zero, O.nn_zero, ?_⟩
      (le_trans' K O.toCone _ _ _ (le_add' K O.toCone _ _ _ _ k1 (le_refl' K O.toCone
        (K.mul (K.mul ν (K.mul (K.add K.one K.one) (K.mul (K.mul h h) (K.mul ν ν)))) (As K l kz a s)))) ?_))
    · show K.add (K.mul ν (K.add (K.mul (K.mul (K.add K.one K.one) (K.mul h h)) (lsum K (fun r => dot3 K (N s r) (N s r)) l))
          (K.mul (K.mul (K.add K.one K.one) (K.mul (K.mul h h) (K.mul ν ν)))
            (lsum K (fun r => K.mul (K.mul (wt K kz r) (wt K kz r)) (dot3 K (a s r) (a s r))) l)))) K.zero
        = K.add (K.mul (K.mul ν (K.mul (K.add K.one K.one) (K.mul h h))) (lsum K (fun r => dot3 K (N s r) (N s r)) l))
          (K.mul (K.mul ν (K.mul (K.add K.one K.one) (K.mul (K.mul h h) (K.mul ν ν)))) (As K l kz a s))
      show _ = K.add _ (K.mul _ (lsum K (fun r => K.mul (K.mul (wt K kz r) (wt K kz r)) (dot3 K (a s r) (a s r))) l))
      lib_ring
    · refine le_trans' K O.toCone _ _ _ (le_add' K O.toCone _ _ _ _ ⟨K.zero, O.nn_zero, ?_⟩ (le_refl' K O.toCone
        (K.mul (K.mul ν (K.mul (K.add K.one K.one) (K.mul (K.mul h h) (K.mul ν ν)))) (As K l kz a s))))
        (le_trans' K O.toCone _ _ _ (le_add' K O.toCone _ _ _ _ k2 (le_refl' K O.toCone _)) ⟨K.zero, O.nn_zero, ?_⟩)
      · show K.add (K.mul (K.mul ν (K.mul (K.add K.one K.one) (K.mul h h))) (K.mul c (K.mul (As K l kz a s) (Zs K l kz a s)))) K.zero
          = K.mul (K.mul (K.mul (K.add K.one K.one) (K.mul h h)) (K.mul c (As K l kz a s))) (K.mul ν (Zs K l kz a s))
        lib_ring
      · lib_ring
  -- dissipation from time t: hν²Σ_{j<n} A_{t+j} ≤ 2Q_t ≤ 2Q_0
  have dis := EnstDiss.enst_diss K O ν h c (fun j => Zs K l kz a (t + j)) (fun j => As K l kz a (t + j)) hν hν0 hh hc
    (fun j => hZ _) (fun j => hA _) (fun j => pstep (t + j))
    (le_trans' K O.toCone _ _ _ (le_mul_nn K O.toCone (K.add c c) _ _ (O.nn_add _ _ hc hc) (hq t)) small) n
  have nQt : O.nn (Q K ν h (fun j => Zs K l kz a (t + j)) (fun j => As K l kz a (t + j)) n) :=
    O.nn_add _ _ (O.nn_mul _ _ hν (hZ _)) (O.nn_mul _ _ (O.nn_mul _ _ hh (O.nn_sq ν)) (hA _))
  have dis2 : Le K O.toCone (K.mul (K.mul h (K.mul ν ν)) (sumN K (fun j => As K l kz a (t + j)) n)) (K.mul (K.add K.one K.one) Q0) :=
    le_trans' K O.toCone _ _ _ ⟨K.mul (K.add K.one K.one) (Q K ν h (fun j => Zs K l kz a (t + j)) (fun j => As K l kz a (t + j)) n),
      O.nn_mul _ _ two nQt, by lib_ring⟩ (le_trans' K O.toCone _ _ _ dis (le_mul_nn K O.toCone _ _ _ two (hq t)))
  -- summed steps
  have ss := Dyadic.sumN_le K O _ _ (fun j => step_nu (t + j)) n
  rw [NSChain.sumN_mul_left K _ _ n, NSChain.sumN_mul_left K _ _ n] at ss
  have ln := TimeLipN.lip_n K O.toCone l (fun j => a (t + j)) n
  have hDn : O.nn (sumN K (fun j => lsum K (fun r => dot3 K (EnstDiff.sub3 K (a (t + j + 1) r) (a (t + j) r))
      (EnstDiff.sub3 K (a (t + j + 1) r) (a (t + j) r))) l) n) :=
    NSChain.sumN_nn K O.toCone _ (fun j => lsum_nn K O.toCone _ (fun r => dot3_nn K O.toCone _) l) n
  generalize sumN K (fun j => lsum K (fun r => dot3 K (EnstDiff.sub3 K (a (t + j + 1) r) (a (t + j) r))
      (EnstDiff.sub3 K (a (t + j + 1) r) (a (t + j) r))) l) n = D at ss ln hDn
  generalize sumN K (fun j => As K l kz a (t + j)) n = SA at ss dis2
  -- ν³·D ≤ 4h(cQ_0 + ν³)Q_0
  have f1 := le_mul_nn K O.toCone (K.mul ν ν) _ _ (O.nn_sq ν) ss
  have f2 := le_mul_nn K O.toCone (K.mul (K.mul (K.add K.one K.one) h) (K.add (K.mul c Q0) (K.mul ν (K.mul ν ν)))) _ _
    (O.nn_mul _ _ (O.nn_mul _ _ two hh) (O.nn_add _ _ (O.nn_mul _ _ hc (TruncRate.nn_of_le K O _ _ (O.nn_mul _ _ hν (hZ 0)) (hz 0)))
      (O.nn_mul _ _ hν (O.nn_sq ν)))) dis2
  have fD : Le K O.toCone (K.mul (K.mul ν (K.mul ν ν)) D)
      (K.mul (K.mul (K.add (K.add K.one K.one) (K.add K.one K.one)) h) (K.mul (K.add (K.mul c Q0) (K.mul ν (K.mul ν ν))) Q0)) := by
    refine le_trans' K O.toCone _ _ _ ⟨K.zero, O.nn_zero, ?_⟩ (le_trans' K O.toCone _ _ _ f1
      (le_trans' K O.toCone _ _ _ ⟨K.zero, O.nn_zero, ?_⟩ (le_trans' K O.toCone _ _ _ f2 ⟨K.zero, O.nn_zero, ?_⟩)))
    all_goals lib_ring
  have g := le_mul_nn K O.toCone (castN K n) _ _ (RatModel.nn_cast K O.toCone n) fD
  have l3 := le_mul_nn K O.toCone (K.mul ν (K.mul ν ν)) _ _ (O.nn_mul _ _ hν (O.nn_sq ν)) ln
  refine le_trans' K O.toCone _ _ _ l3 (le_trans' K O.toCone _ _ _ ⟨K.zero, O.nn_zero, ?_⟩ g)
  show K.add (K.mul (K.mul ν (K.mul ν ν)) (K.mul (castN K n) D)) K.zero = K.mul (castN K n) (K.mul (K.mul ν (K.mul ν ν)) D)
  lib_ring

end Gen

end HolderSmall

#print axioms HolderSmall.holder_small

ns/IntDrops.lean

lean_all/ns/IntDrops.lean

/-
  ns/IntDrops.lean — T37 cycle 719 (goal G432): in an integer Crank–Nicolson Galerkin scheme the energy drops
  strictly at most E₀ times.

  Lean 4 core plus ns/IntOrd (intR, intCone), NSChain (cn_global), lib/IntRing (lib_int).
    succ_le_of_ne  x ≤ y, x ≠ y  ⇒  x + 1 ≤ y                  (in ℤ)
    drops_le       e(t+1) ≤ e(t) for all t  ⇒  #{t < T : e(t+1) ≠ e(t)} + e(T) ≤ e(0)
    step_mono      CN step with neutral nonlinearity  ⇒  E(t+1) ≤ E(t)   (cn_global on the shifted solution)
    int_drops      #{t < T : E(t+1) ≠ E(t)} ≤ E(0)            for every T
  For integer data the scheme is energy-conserving except at no more than E₀ steps, whatever the time horizon.
-/
import ns.IntOrd

namespace IntDrops
open DetChain NSChain DyadicSmallGlobal LibInt IntOrd

theorem succ_le_of_ne (x y : Int) (h : Le intR intCone x y) (hne : x ≠ y) : Le intR intCone (x + 1) y := by
  cases h with
  | intro d hd =>
    cases hd.1 with
    | intro n hn =>
      match n, hn with
      | 0, hn =>
        have e : x = y := by
          have h2 : x + d = y := hd.2
          rw [← h2, hn]; show x = x + 0; lib_int
        exact absurd e hne
      | n + 1, hn =>
        refine ⟨Int.ofNat n, ⟨n, rfl⟩, ?_⟩
        have h2 : x + d = y := hd.2
        rw [← h2, hn]
        show x + 1 + Int.ofNat n = x + (Int.ofNat n + 1)
        lib_int

def dr (e : Nat → Int) (t : Nat) : Int :=
  match Int.decEq (e (t + 1)) (e t) with
  | isTrue _ => 0
  | isFalse _ => 1

theorem dr_step (e : Nat → Int) (t : Nat) (h : Le intR intCone (e (t + 1)) (e t)) :
    Le intR intCone (dr e t + e (t + 1)) (e t) := by
  unfold dr
  cases Int.decEq (e (t + 1)) (e t) with
  | isTrue q =>
    show Le intR intCone (0 + e (t + 1)) (e t)
    have z : (0 : Int) + e (t + 1) = e (t + 1) := by lib_int
    rw [z]; exact h
  | isFalse q =>
    show Le intR intCone (1 + e (t + 1)) (e t)
    have z : (1 : Int) + e (t + 1) = e (t + 1) + 1 := by lib_int
    rw [z]; exact succ_le_of_ne _ _ h q

theorem drops_le (e : Nat → Int) (mono : ∀ t, Le intR intCone (e (t + 1)) (e t)) :
    ∀ T, Le intR intCone (sumN intR (dr e) T + e T) (e 0)
  | 0 => by
    show Le intR intCone (0 + e 0) (e 0)
    have z : (0 : Int) + e 0 = e 0 := by lib_int
    rw [z]; exact le_refl' intR intCone _
  | T + 1 => by
    have r : sumN intR (dr e) (T + 1) + e (T + 1) = sumN intR (dr e) T + (dr e T + e (T + 1)) := by
      show sumN intR (dr e) T + dr e T + e (T + 1) = sumN intR (dr e) T + (dr e T + e (T + 1)); lib_int
    rw [r]
    exact le_trans' intR intCone _ _ _
      (le_add2 (sumN intR (dr e) T) _ _ (dr_step e T (mono T))) (drops_le e mono T)
where
  le_add2 (s x y : Int) (h : Le intR intCone x y) : Le intR intCone (s + x) (s + y) := by
    cases h with
    | intro d hd => exact ⟨d, hd.1, by rw [← hd.2]; show s + x + d = s + (x + d); lib_int⟩

def En (a : Nat → Nat → Int) (N t : Nat) : Int := sumN intR (fun n => intR.mul (a t n) (a t n)) N

theorem step_mono (a : Nat → Nat → Int) (k : Nat → Int) (Nn : Nat → Nat → Int) (h nu : Int) (N : Nat)
    (hh : NN h) (hnu : NN nu) (hk : ∀ n, NN (k n))
    (step : ∀ t n, intR.add (a (t + 1) n) (intR.neg (a t n))
      = intR.mul h (intR.add (intR.neg (intR.mul nu (intR.mul (k n) (intR.add (a (t + 1) n) (a t n))))) (Nn t n)))
    (neutral : ∀ t, sumN intR (fun n => intR.mul (intR.add (a (t + 1) n) (a t n)) (Nn t n)) N = intR.zero)
    (t : Nat) : Le intR intCone (En a N (t + 1)) (En a N t) :=
  (cn_global intR intCone (fun u => a (t + u)) k (fun u => Nn (t + u)) h nu N hh hnu hk
    (fun u n => step (t + u) n) (fun u => neutral (t + u))).1 1

theorem int_drops (a : Nat → Nat → Int) (k : Nat → Int) (Nn : Nat → Nat → Int) (h nu : Int) (N : Nat)
    (hh : NN h) (hnu : NN nu) (hk : ∀ n, NN (k n))
    (step : ∀ t n, intR.add (a (t + 1) n) (intR.neg (a t n))
      = intR.mul h (intR.add (intR.neg (intR.mul nu (intR.mul (k n) (intR.add (a (t + 1) n) (a t n))))) (Nn t n)))
    (neutral : ∀ t, sumN intR (fun n => intR.mul (intR.add (a (t + 1) n) (a t n)) (Nn t n)) N = intR.zero)
    (T : Nat) : Le intR intCone (sumN intR (dr (En a N)) T) (En a N 0) := by
  have d := drops_le (En a N) (step_mono a k Nn h nu N hh hnu hk step neutral) T
  have hE : NN (En a N T) := sq_sum_nonneg intR intCone (a T) N
  exact le_trans' intR intCone _ _ _ ⟨_, hE, rfl⟩ d

end IntDrops

#print axioms IntDrops.succ_le_of_ne
#print axioms IntDrops.dr_step
#print axioms IntDrops.drops_le
#print axioms IntDrops.step_mono
#print axioms IntDrops.int_drops

ns/IntFlip.lean

lean_all/ns/IntFlip.lean

/-
  ns/IntFlip.lean — T37 cycle 722 (goal G435): at an energy-conserving step of an integer Crank–Nicolson Galerkin
  scheme (h, ν, k_n ≥ 1) every mode flips sign: a_{t+1}(n) = −a_t(n).

  Lean 4 core plus ns/DyadicModelSupp (one_le_sq, no_le_pred), ns/DyadicModelMode (term_le), ns/IntDrops,
  NSChain (cn_energy: E' + hν Σ k (a'+a)² = E, exact).
    nn_le_zero    x ≥ 0, x ≤ 0 ⇒ x = 0
    le_mul_ge1    1 ≤ h, X ≥ 0 ⇒ X ≤ h·X
    zero_of_mul   1 ≤ h, X ≥ 0, h·X = 0 ⇒ X = 0
    sq_zero       a·a = 0 ⇒ a = 0
    flip          E(t+1) = E(t) ⇒ a_{t+1}(n) = −a_t(n) for n < N
  With IntDrops: all but at most E₀ steps of an integer scheme are pure sign flips.
-/
import ns.DyadicModelSupp
import ns.IntDrops

namespace IntFlip
open DetChain NSChain DyadicSmallGlobal LibInt LibRing IntOrd DyadicLarge DyadicModelMode DyadicModelSupp

theorem nn_le_zero (x : Int) (hx : NN x) (h : Le intR intCone x 0) : x = 0 := by
  cases hx with
  | intro p hp =>
    cases h with
    | intro d hd =>
      cases hd.1 with
      | intro q hq =>
        have e : Int.ofNat p + Int.ofNat q = 0 := by rw [← hp, ← hq]; exact hd.2
        have e2 : Int.ofNat (p + q) = Int.ofNat 0 := e
        have e3 : p + q = 0 := Int.ofNat.inj e2
        rw [hp, Nat.eq_zero_of_add_eq_zero_right e3]; rfl

theorem le_mul_ge1 (h X : Int) (h1 : Le intR intCone 1 h) (hX : NN X) : Le intR intCone X (h * X) := by
  cases h1 with
  | intro d hd =>
    refine ⟨d * X, intCone.nn_mul _ _ hd.1 hX, ?_⟩
    have e : (1 : Int) + d = h := hd.2
    rw [← e]; show X + d * X = (1 + d) * X; lib_int

theorem zero_of_mul (h X : Int) (h1 : Le intR intCone 1 h) (hX : NN X) (e : h * X = 0) : X = 0 :=
  nn_le_zero X hX (by have l := le_mul_ge1 h X h1 hX; rw [e] at l; exact l)

theorem sq_zero : ∀ a : Int, a * a = 0 → a = 0
  | Int.ofNat 0, _ => rfl
  | Int.ofNat (k + 1), e => by
    have l := one_le_sq (Int.ofNat (k + 1)) (fun q => Nat.noConfusion (Int.ofNat.inj (q : Int.ofNat (k + 1) = Int.ofNat 0)))
    rw [e] at l; exact absurd l (no_le_pred 0)
  | Int.negSucc k, e => by
    have l := one_le_sq (Int.negSucc k) (fun q => Int.noConfusion q)
    rw [e] at l; exact absurd l (no_le_pred 0)

theorem nn_of_ge1 (h : Int) (h1 : Le intR intCone 1 h) : NN h := by
  cases h1 with
  | intro d hd => rw [← hd.2]; exact intCone.nn_add _ _ ⟨1, rfl⟩ hd.1

theorem flip (a a' k Nn : Nat → Int) (h nu : Int) (N : Nat)
    (hh : Le intR intCone 1 h) (hnu : Le intR intCone 1 nu) (hk : ∀ n, Le intR intCone 1 (k n))
    (step : ∀ n, intR.add (a' n) (intR.neg (a n))
      = intR.mul h (intR.add (intR.neg (intR.mul nu (intR.mul (k n) (intR.add (a' n) (a n))))) (Nn n)))
    (neutral : sumN intR (fun n => intR.mul (intR.add (a' n) (a n)) (Nn n)) N = intR.zero)
    (heq : sumN intR (fun n => intR.mul (a' n) (a' n)) N = sumN intR (fun n => intR.mul (a n) (a n)) N) :
    ∀ n, n < N → a' n = -a n := by
  intro n hn
  have ce := cn_energy intR a a' k Nn h nu N step neutral
  rw [heq] at ce
  generalize sumN intR (fun n => intR.mul (a n) (a n)) N = E at ce
  let f : Nat → Int := fun n => intR.mul (k n) (intR.mul (intR.add (a' n) (a n)) (intR.add (a' n) (a n)))
  have hf : ∀ j, NN (f j) := fun j => intCone.nn_mul _ _ (nn_of_ge1 _ (hk j)) (nn_sq' _)
  have hS : NN (sumN intR f N) := sumN_nn intR intCone f hf N
  have hD : intR.mul h (intR.mul nu (sumN intR f N)) = 0 := by
    have c2 : E + intR.mul h (intR.mul nu (sumN intR f N)) = E := ce
    generalize intR.mul h (intR.mul nu (sumN intR f N)) = D at c2
    have : D = (E + D) + -E := by lib_int
    rw [this, c2]; lib_int
  have hS0 : sumN intR f N = 0 :=
    zero_of_mul nu _ hnu hS (zero_of_mul h _ hh (intCone.nn_mul _ _ (nn_of_ge1 _ hnu) hS) hD)
  have hfn : f n = 0 := by
    have l := term_le intR intCone f hf n N hn
    rw [hS0] at l
    exact nn_le_zero _ (hf n) l
  have hs2 : intR.mul (intR.add (a' n) (a n)) (intR.add (a' n) (a n)) = 0 :=
    zero_of_mul (k n) _ (hk n) (nn_sq' _) hfn
  have hs : a' n + a n = 0 := sq_zero _ hs2
  have : a' n = (a' n + a n) + -a n := by lib_int
  rw [this, hs]; lib_int

end IntFlip

#print axioms IntFlip.nn_le_zero
#print axioms IntFlip.le_mul_ge1
#print axioms IntFlip.zero_of_mul
#print axioms IntFlip.sq_zero
#print axioms IntFlip.nn_of_ge1
#print axioms IntFlip.flip

ns/IntForce.lean

lean_all/ns/IntForce.lean

/-
  ns/IntForce.lean — T37 cycle 1802 (goal G856d): the time-integrated forces of two schemes are Cauchy, over an
  ordered ring. With f_t one component of the force difference of a low mode and D_t the force-difference energy
  (ForceTime):
    comp_le     (a.x)² ≤ |a|²  (and .y, .z)
    tsum_le     f_t ≤ g_t for all t, h ≥ 0  ⇒  Σ_{t<n} h f_t ≤ Σ_{t<n} h g_t
    int_force   f_t² ≤ D_t, D_t ≤ αZ_t + βA_t, νZ_t + hν²A_t ≤ q  ⇒
                ν²·(Σ_{t<n} h f_t)² ≤ (n·h)·(n·(hαν + β)·q)
  Together with SchemeTele.tele (x_n − x_0 = Σh(N_t − νw x_{t+1}) exactly, for each scheme) and TruncCoef (the
  coefficients are Cauchy), every term of the integral equation is Cauchy with an explicit modulus in q: the
  limit coefficients satisfy x(T) = x(0) + ∫₀ᵀ (N − νwx) in the Bishop sense.
  Lean 4 core plus ns/ForceTime, ns/TeleCS.
-/
import ns.ForceTime
import ns.TeleCS

namespace IntForce
open DetChain NSChain DyadicSmallGlobal DyadicLarge LibRing FourierGalerkin ForceBound SchemeTele

section Gen
variable {R : Type} (K : CRing R) (O : OrdR K)

theorem comp_le (a : V3 R) : Le K O.toCone (K.mul a.x a.x) (dot3 K a a) :=
  ⟨K.add (K.mul a.y a.y) (K.mul a.z a.z), O.nn_add _ _ (O.nn_sq _) (O.nn_sq _),
    by show K.add (K.mul a.x a.x) (K.add (K.mul a.y a.y) (K.mul a.z a.z))
      = K.add (K.add (K.mul a.x a.x) (K.mul a.y a.y)) (K.mul a.z a.z); lib_ring⟩

theorem tsum_le (h : R) (hh : O.nn h) (f g : Nat → R) (hfg : ∀ t, Le K O.toCone (f t) (g t)) :
    ∀ n, Le K O.toCone (tsum K (fun t => K.mul h (f t)) n) (tsum K (fun t => K.mul h (g t)) n)
  | 0 => le_refl' K O.toCone _
  | n + 1 => le_add' K O.toCone _ _ _ _ (tsum_le h hh f g hfg n) (le_mul_nn K O.toCone h _ _ hh (hfg n))

theorem int_force (ν h α β q : R) (f D Z A : Nat → R) (hν : O.nn ν) (hh : O.nn h) (hα : O.nn α) (hβ : O.nn β)
    (hZ : ∀ t, O.nn (Z t)) (hA : ∀ t, O.nn (A t)) (hf : ∀ t, Le K O.toCone (K.mul (f t) (f t)) (D t))
    (hD : ∀ t, Le K O.toCone (D t) (K.add (K.mul α (Z t)) (K.mul β (A t))))
    (hQ : ∀ t, Le K O.toCone (K.add (K.mul ν (Z t)) (K.mul (K.mul h (K.mul ν ν)) (A t))) q) (n : Nat) :
    Le K O.toCone (K.mul (K.mul ν ν) (K.mul (tsum K (fun t => K.mul h (f t)) n) (tsum K (fun t => K.mul h (f t)) n)))
      (K.mul (K.mul (castN K n) h) (K.mul (castN K n) (K.mul (K.add (K.mul (K.mul h α) ν) β) q))) := by
  have nnh : O.nn (K.mul (castN K n) h) := O.nn_mul _ _ (RatModel.nn_cast K O.toCone n) hh
  have cs := TeleCS.tsum_cs K O h f n
  have mono := tsum_le K O h hh _ _ hf n
  have ft := ForceTime.force_time K O ν h α β q Z A D hν hh hα hβ hZ hA hD hQ n
  have c1 := le_mul_nn K O.toCone (K.mul ν ν) _ _ (O.nn_sq ν) cs
  have c2 := le_mul_nn K O.toCone (K.mul (K.mul ν ν) (K.mul (castN K n) h)) _ _ (O.nn_mul _ _ (O.nn_sq ν) nnh) mono
  have c3 := le_mul_nn K O.toCone (K.mul (castN K n) h) _ _ nnh ft
  refine le_trans' K O.toCone _ _ _ c1 (le_trans' K O.toCone _ _ _ ⟨K.zero, O.nn_zero, ?_⟩
    (le_trans' K O.toCone _ _ _ c2 (le_trans' K O.toCone _ _ _ ⟨K.zero, O.nn_zero, ?_⟩ c3)))
  · generalize tsum K (fun t => K.mul h (K.mul (f t) (f t))) n = S
    generalize castN K n = c
    lib_ring
  · generalize tsum K (fun t => K.mul h (D t)) n = S
    generalize castN K n = c
    lib_ring

end Gen
end IntForce

#print axioms IntForce.comp_le
#print axioms IntForce.tsum_le
#print axioms IntForce.int_force

ns/IntOrd.lean

lean_all/ns/IntOrd.lean

/-
  ns/IntOrd.lean — T37 cycle 689 (goal G402): the integers as an ordered ring (DyadicSmallGlobal.OrdR) without
  axioms, so that the ordered-ring theorems of the dyadic model (small_data_global, DyadicLargeData.cn_large_data)
  apply to integer data.

  Lean 4 core plus lib/IntRing (LibInt.intR, int_noZD, lib_int). nn x := ∃ n : Nat, x = ofNat n.
    nn_or_neg   every integer z has nn z or nn (−z)
    intOrd      OrdR LibInt.intR (cone, total, antisymmetric, no zero divisors)
    int_large_data   DyadicLargeData.cn_large_data for integer data (CN dyadic model over ℤ)
-/
import ns.DyadicLargeData
import lib.IntRing

namespace IntOrd
open DetChain NSChain DyadicSmallGlobal LibInt

def NN (x : Int) : Prop := ∃ n : Nat, x = Int.ofNat n

theorem nn_or_neg : ∀ z : Int, NN z ∨ NN (-z)
  | Int.ofNat m => Or.inl ⟨m, rfl⟩
  | Int.negSucc m => Or.inr ⟨m + 1, rfl⟩

theorem nn_sq' : ∀ x : Int, NN (x * x)
  | Int.ofNat m => ⟨m * m, rfl⟩
  | Int.negSucc m => ⟨(m + 1) * (m + 1), rfl⟩

def intCone : Cone intR where
  nn := NN
  nn_zero := ⟨0, rfl⟩
  nn_add := fun x y hx hy => by
    cases hx with
    | intro m hm => cases hy with
      | intro n hn => exact ⟨m + n, by rw [hm, hn]; rfl⟩
  nn_mul := fun x y hx hy => by
    cases hx with
    | intro m hm => cases hy with
      | intro n hn => exact ⟨m * n, by rw [hm, hn]; rfl⟩
  nn_sq := nn_sq'

theorem nat_add_eq_zero : ∀ m n : Nat, m + n = 0 → m = 0
  | 0, _, _ => rfl
  | _ + 1, 0, h => Nat.noConfusion h
  | _ + 1, _ + 1, h => Nat.noConfusion h

theorem int_total (x y : Int) : Le intR intCone x y ∨ Le intR intCone y x := by
  cases nn_or_neg (y + -x) with
  | inl h => exact Or.inl ⟨y + -x, h, show x + (y + -x) = y by lib_int⟩
  | inr h => exact Or.inr ⟨-(y + -x), h, show y + -(y + -x) = x by lib_int⟩

theorem int_antisym (x y : Int) (h1 : Le intR intCone x y) (h2 : Le intR intCone y x) : x = y := by
  cases h1 with
  | intro d hd => cases h2 with
    | intro e he =>
      have e1 : x + d = y := hd.2
      have e2 : y + e = x := he.2
      have s : d + e = 0 := by
        have t : x + (d + e) = x := by
          rw [show x + (d + e) = (x + d) + e by lib_int, e1, e2]
        rw [show d + e = (x + (d + e)) + -x by lib_int, t]
        lib_int
      cases hd.1 with
      | intro m hm => cases he.1 with
        | intro n hn =>
          rw [hm, hn] at s
          have m0 : m = 0 := nat_add_eq_zero m n (Int.ofNat.inj s)
          rw [← e1, hm, m0]
          show x = x + 0
          lib_int

def intOrd : OrdR intR where
  toCone := intCone
  total := int_total
  antisym := int_antisym
  noZD := int_noZD

open DyadicHs DyadicCN DyadicLarge DyadicLargeCN DyadicLargeEnergy DyadicLargeData in
theorem int_large_data (w c k M : Nat → Int) (a : Nat → Nat → Int) (h nu A : Int) (n0 N : Nat)
    (hh : NN h) (hnu : NN nu) (hk : ∀ n, NN (k n)) (hw : ∀ n, NN (w n))
    (h0 : ∀ t, a t 0 = 0) (hz : ∀ t n, N + 1 ≤ n → a t n = 0)
    (step : ∀ t n, intR.add (a (t + 1) (n + 1)) (intR.neg (a t (n + 1)))
      = intR.mul h (intR.add (intR.neg (intR.mul nu (intR.mul (k (n + 1)) (intR.add (a (t + 1) (n + 1)) (a t (n + 1))))))
          (dyN intR c (fun i => intR.add (a (t + 1) i) (a t i)) n)))
    (hq : ∀ n, NN (qq intR w c n))
    (hcrit : ∀ n, n0 ≤ n → intR.mul (qq intR w c n) (M n) = intR.mul nu (intR.mul (w n) (k n)))
    (hA2 : Le intR intCone (four intR (E0 intR a N)) (intR.mul A A)) (hAnn : NN A)
    (hM : ∀ n, n0 ≤ n → Le intR intCone A (M n)) :
    ∀ t, Le intR intCone (Ew intR w (a t) N)
      (intR.add (Ew intR w (a 0) N) (sumN intR (fun _ => intR.mul h (headB intR w c (four intR (E0 intR a N)) A n0 N)) t)) :=
  cn_large_data intR intOrd w c k M a h nu A n0 N hh hnu hk hw h0 hz step hq hcrit hA2 hAnn hM

end IntOrd

#print axioms IntOrd.nn_or_neg
#print axioms IntOrd.nn_sq'
#print axioms IntOrd.nat_add_eq_zero
#print axioms IntOrd.int_total
#print axioms IntOrd.int_antisym
#print axioms IntOrd.intOrd
#print axioms IntOrd.int_large_data

ns/IntStretch.lean

lean_all/ns/IntStretch.lean

/-
  ns/IntStretch.lean — T37 cycle 725 (goal G438): an integer Crank–Nicolson Galerkin scheme (h, ν, k_n ≥ 1) has,
  for every L, a stretch of L consecutive pure sign-flip steps that starts before time L·E₀ (exactly: in one of
  the first E₀ + 1 blocks of length L).

  Lean 4 core plus ns/IntFlip (flip, nn_le_zero, nn_of_ge1), ns/IntDrops (dr, int_drops, succ_le_of_ne).
    sumN_shift   Σ_{i<n+L} f = Σ_{i<n} f + Σ_{i<L} f(n+i)
    blocks       Σ_{i<L·q} f = Σ_{j<q} Σ_{i<L} f(L·j+i)
    search       B ≥ 0  ⇒  (∃ j < q, B j = 0) ∨ q ≤ Σ_{j<q} B j
    stretch      E(0) = e  ⇒  ∃ j ≤ e, ∀ i < L, ∀ n < N, a_{Lj+i+1}(n) = −a_{Lj+i}(n)
  So the integer dynamics is 2-periodic (a_{t+2} = a_t) on arbitrarily long windows: pigeonhole on the ≤ E₀ drops.
-/
import ns.IntFlip

namespace IntStretch
open DetChain NSChain DyadicSmallGlobal LibInt LibRing IntOrd DyadicLarge DyadicModelMode DyadicModelSupp
  IntDrops IntFlip

theorem sumN_shift (f : Nat → Int) (n : Nat) : ∀ L,
    sumN intR f (n + L) = sumN intR f n + sumN intR (fun i => f (n + i)) L
  | 0 => by show sumN intR f n = sumN intR f n + 0; lib_int
  | L + 1 => by
    show sumN intR f (n + L) + f (n + L) = sumN intR f n + (sumN intR (fun i => f (n + i)) L + f (n + L))
    rw [sumN_shift f n L]; lib_int

theorem blocks (f : Nat → Int) (L : Nat) : ∀ q,
    sumN intR f (L * q) = sumN intR (fun j => sumN intR (fun i => f (L * j + i)) L) q
  | 0 => rfl
  | q + 1 => by
    show sumN intR f (L * q + L) = sumN intR (fun j => sumN intR (fun i => f (L * j + i)) L) q
      + sumN intR (fun i => f (L * q + i)) L
    rw [sumN_shift, blocks f L q]

theorem search (B : Nat → Int) (hB : ∀ j, NN (B j)) : ∀ q,
    (∃ j, j < q ∧ B j = 0) ∨ Le intR intCone (Int.ofNat q) (sumN intR B q)
  | 0 => Or.inr (le_refl' intR intCone _)
  | q + 1 => by
    cases search B hB q with
    | inl l =>
      cases l with
      | intro j hj => exact Or.inl ⟨j, Nat.lt_succ_of_lt hj.1, hj.2⟩
    | inr r =>
      cases Int.decEq (B q) 0 with
      | isTrue z => exact Or.inl ⟨q, Nat.lt_succ_self q, z⟩
      | isFalse z =>
        have h0 : Le intR intCone 0 (B q) := ⟨B q, hB q, by show 0 + B q = B q; lib_int⟩
        have h1 := succ_le_of_ne 0 (B q) h0 (fun e => z e.symm)
        have h1' : Le intR intCone 1 (B q) := by
          have e : (0 : Int) + 1 = 1 := rfl
          rw [e] at h1; exact h1
        exact Or.inr (le_add' intR intCone _ _ _ _ r h1')

theorem dr_nn (e : Nat → Int) (t : Nat) : NN (dr e t) := by
  unfold dr
  cases Int.decEq (e (t + 1)) (e t) with
  | isTrue _ => exact ⟨0, rfl⟩
  | isFalse _ => exact ⟨1, rfl⟩

theorem eq_of_dr (e : Nat → Int) (t : Nat) (h : dr e t = 0) : e (t + 1) = e t := by
  unfold dr at h
  cases q : Int.decEq (e (t + 1)) (e t) with
  | isTrue p => exact p
  | isFalse _ =>
    rw [q] at h
    exact Int.noConfusion h (fun e' => Nat.noConfusion e')

theorem stretch (a : Nat → Nat → Int) (k : Nat → Int) (Nn : Nat → Nat → Int) (h nu : Int) (N : Nat)
    (hh : Le intR intCone 1 h) (hnu : Le intR intCone 1 nu) (hk : ∀ n, Le intR intCone 1 (k n))
    (step : ∀ t n, intR.add (a (t + 1) n) (intR.neg (a t n))
      = intR.mul h (intR.add (intR.neg (intR.mul nu (intR.mul (k n) (intR.add (a (t + 1) n) (a t n))))) (Nn t n)))
    (neutral : ∀ t, sumN intR (fun n => intR.mul (intR.add (a (t + 1) n) (a t n)) (Nn t n)) N = intR.zero)
    (e : Nat) (hE : IntDrops.En a N 0 = Int.ofNat e) (L : Nat) :
    ∃ j, j < e + 1 ∧ ∀ i, i < L → ∀ n, n < N → a (L * j + i + 1) n = -a (L * j + i) n := by
  have d := int_drops a k Nn h nu N (nn_of_ge1 _ hh) (nn_of_ge1 _ hnu) (fun n => nn_of_ge1 _ (hk n))
    step neutral (L * (e + 1))
  rw [blocks, hE] at d
  let B : Nat → Int := fun j => sumN intR (fun i => dr (IntDrops.En a N) (L * j + i)) L
  have hB : ∀ j, NN (B j) := fun j => sumN_nn intR intCone _ (fun i => dr_nn _ _) L
  cases search B hB (e + 1) with
  | inr r => exact absurd (le_trans' intR intCone _ _ _ r d) (no_le_pred (Int.ofNat e))
  | inl l =>
    cases l with
    | intro j hj =>
      refine ⟨j, hj.1, ?_⟩
      intro i hi n hn
      have hz : dr (IntDrops.En a N) (L * j + i) = 0 := by
        have l1 := term_le intR intCone (fun i => dr (IntDrops.En a N) (L * j + i)) (fun i => dr_nn _ _) i L hi
        have hB0 : B j = 0 := hj.2
        have l2 : Le intR intCone (dr (IntDrops.En a N) (L * j + i)) 0 := by
          have : sumN intR (fun i => dr (IntDrops.En a N) (L * j + i)) L = 0 := hB0
          rw [this] at l1; exact l1
        exact nn_le_zero _ (dr_nn _ _) l2
      have heq := eq_of_dr _ _ hz
      exact flip (a (L * j + i)) (a (L * j + i + 1)) k (Nn (L * j + i)) h nu N hh hnu hk
        (step (L * j + i)) (neutral (L * j + i)) heq n hn

end IntStretch

#print axioms IntStretch.sumN_shift
#print axioms IntStretch.blocks
#print axioms IntStretch.search
#print axioms IntStretch.dr_nn
#print axioms IntStretch.eq_of_dr
#print axioms IntStretch.stretch

ns/IntVanish.lean

lean_all/ns/IntVanish.lean

/-
  ns/IntVanish.lean — T37 cycle 728 (goal G441): an integer Crank–Nicolson Galerkin scheme (h, ν, k_n ≥ 1) whose
  nonlinearity vanishes with the midpoint sum (Nn = N(a_{t+1} + a_t), N(0) = 0, as for the dyadic model) is
  identically zero after at most E₀ steps.

  Lean 4 core plus ns/IntStretch, ns/DyadicModelT (sumN_const), ns/DyadicModelB (sumN_ext_lt).
    dbl_zero     x + x = 0 ⇒ x = 0                         (ℤ, by constructors)
    vanish       E(t+1) = E(t) ⇒ a_t(n) = 0 for n < N  and E(t) = 0
    e_mono       E(t + d) ≤ E(t)
    count        (∃ t < T, E t = 0) ∨ T ≤ #drops before T
    extinct      E(0) = e ⇒ E(e) = 0, so a_e(n) = 0 for all n < N
-/
import ns.IntStretch
import ns.DyadicModelT

namespace IntVanish
open DetChain NSChain DyadicSmallGlobal LibInt LibRing IntOrd DyadicLarge DyadicModelMode DyadicModelSupp
  IntDrops IntFlip IntStretch DyadicModelB DyadicModelT

theorem dbl_zero : ∀ x : Int, x + x = 0 → x = 0
  | Int.ofNat p, h => by
    have h' : Int.ofNat (p + p) = Int.ofNat 0 := h
    have e := Int.ofNat.inj h'
    rw [Nat.eq_zero_of_add_eq_zero_left e]; rfl
  | Int.negSucc p, h => by
    have h' : Int.negSucc (Nat.succ (p + p)) = 0 := h
    exact Int.noConfusion h'

theorem sum_zero (f : Nat → Int) (N : Nat) (h : ∀ n, n < N → f n = 0) : sumN intR f N = 0 := by
  rw [sumN_ext_lt f (fun _ => 0) N h, sumN_const]; show Int.ofNat N * 0 = 0; lib_int

theorem vanish (a a' k Nn : Nat → Int) (h nu : Int) (N : Nat)
    (hh : Le intR intCone 1 h) (hnu : Le intR intCone 1 nu) (hk : ∀ n, Le intR intCone 1 (k n))
    (step : ∀ n, intR.add (a' n) (intR.neg (a n))
      = intR.mul h (intR.add (intR.neg (intR.mul nu (intR.mul (k n) (intR.add (a' n) (a n))))) (Nn n)))
    (neutral : sumN intR (fun n => intR.mul (intR.add (a' n) (a n)) (Nn n)) N = intR.zero)
    (hN : (∀ n, n < N → a' n + a n = 0) → ∀ n, n < N → Nn n = 0)
    (heq : sumN intR (fun n => intR.mul (a' n) (a' n)) N = sumN intR (fun n => intR.mul (a n) (a n)) N) :
    (∀ n, n < N → a n = 0) ∧ sumN intR (fun n => intR.mul (a n) (a n)) N = 0 := by
  have fl := flip a a' k Nn h nu N hh hnu hk step neutral heq
  have hs : ∀ n, n < N → a' n + a n = 0 := fun n hn => by rw [fl n hn]; lib_int
  have hz := hN hs
  have ha : ∀ n, n < N → a n = 0 := by
    intro n hn
    have st := step n
    have s0 : a' n + a n = 0 := hs n hn
    have z0 : Nn n = 0 := hz n hn
    have st' : a' n + -a n = h * (-(nu * (k n * (a' n + a n))) + Nn n) := st
    rw [s0, z0] at st'
    have st2 : a' n + -a n = 0 := by rw [st']; lib_int
    have f0 : a' n = -a n := fl n hn
    rw [f0] at st2
    have : a n + a n = -(-a n + -a n) := by lib_int
    exact dbl_zero _ (by rw [this, st2]; rfl)
  exact ⟨ha, sum_zero _ N (fun n hn => by show a n * a n = 0; rw [ha n hn]; rfl)⟩

theorem e_mono (E : Nat → Int) (mono : ∀ t, Le intR intCone (E (t + 1)) (E t)) (t : Nat) :
    ∀ d, Le intR intCone (E (t + d)) (E t)
  | 0 => le_refl' intR intCone _
  | d + 1 => le_trans' intR intCone _ _ _ (mono (t + d)) (e_mono E mono t d)

theorem dr_one (E : Nat → Int) (t : Nat) (h : E (t + 1) ≠ E t) : dr E t = 1 := by
  unfold dr
  cases Int.decEq (E (t + 1)) (E t) with
  | isTrue p => exact absurd p h
  | isFalse _ => rfl

theorem count (E : Nat → Int) (hv : ∀ t, E (t + 1) = E t → E t = 0) : ∀ T,
    (∃ t, t < T ∧ E t = 0) ∨ Le intR intCone (Int.ofNat T) (sumN intR (dr E) T)
  | 0 => Or.inr (le_refl' intR intCone _)
  | T + 1 => by
    cases count E hv T with
    | inl l => cases l with
      | intro t ht => exact Or.inl ⟨t, Nat.lt_succ_of_lt ht.1, ht.2⟩
    | inr r =>
      cases Int.decEq (E T) 0 with
      | isTrue z => exact Or.inl ⟨T, Nat.lt_succ_self T, z⟩
      | isFalse z =>
        have d1 : dr E T = 1 := dr_one E T (fun q => z (hv T q))
        refine Or.inr ?_
        show Le intR intCone (intR.add (Int.ofNat T) 1) (intR.add (sumN intR (dr E) T) (dr E T))
        rw [d1]
        exact le_add' intR intCone _ _ _ _ r (le_refl' intR intCone 1)

theorem extinct (a : Nat → Nat → Int) (k : Nat → Int) (Nn : Nat → Nat → Int) (h nu : Int) (N : Nat)
    (hh : Le intR intCone 1 h) (hnu : Le intR intCone 1 nu) (hk : ∀ n, Le intR intCone 1 (k n))
    (step : ∀ t n, intR.add (a (t + 1) n) (intR.neg (a t n))
      = intR.mul h (intR.add (intR.neg (intR.mul nu (intR.mul (k n) (intR.add (a (t + 1) n) (a t n))))) (Nn t n)))
    (neutral : ∀ t, sumN intR (fun n => intR.mul (intR.add (a (t + 1) n) (a t n)) (Nn t n)) N = intR.zero)
    (hN : ∀ t, (∀ n, n < N → a (t + 1) n + a t n = 0) → ∀ n, n < N → Nn t n = 0)
    (e : Nat) (hE : IntDrops.En a N 0 = Int.ofNat e) :
    IntDrops.En a N e = 0 := by
  have hv : ∀ t, IntDrops.En a N (t + 1) = IntDrops.En a N t → IntDrops.En a N t = 0 := fun t q =>
    (vanish (a t) (a (t + 1)) k (Nn t) h nu N hh hnu hk (step t) (neutral t) (hN t) q).2
  have mono := step_mono a k Nn h nu N (nn_of_ge1 _ hh) (nn_of_ge1 _ hnu) (fun n => nn_of_ge1 _ (hk n)) step neutral
  have dd := int_drops a k Nn h nu N (nn_of_ge1 _ hh) (nn_of_ge1 _ hnu) (fun n => nn_of_ge1 _ (hk n))
    step neutral (e + 1)
  rw [hE] at dd
  cases count (IntDrops.En a N) hv (e + 1) with
  | inr r => exact absurd (le_trans' intR intCone _ _ _ r dd) (no_le_pred (Int.ofNat e))
  | inl l =>
    cases l with
    | intro t ht =>
      have ⟨d, hd⟩ := DyadicModelZ.le_dest (Nat.le_of_lt_succ ht.1)
      have l2 := e_mono _ mono t d
      rw [hd, ht.2] at l2
      exact nn_le_zero _ (sq_sum_nonneg intR intCone (a e) N) l2

end IntVanish

#print axioms IntVanish.dbl_zero
#print axioms IntVanish.sum_zero
#print axioms IntVanish.vanish
#print axioms IntVanish.e_mono
#print axioms IntVanish.dr_one
#print axioms IntVanish.count
#print axioms IntVanish.extinct

ns/IntegralForm.lean

lean_all/ns/IntegralForm.lean

/-
  ns/IntegralForm.lean — T37 cycle 1177 (goal G764a): the integrated form of the implicit-viscosity scheme, over any
  commutative ring. From StepC: (1 + hνw)a' = a + hN, i.e. a' − a = h(N − νw·a'):
    step_diff    a_{k+1} − a_k = h·(N_k − νw·a_{k+1})
    integral     a_n − a_0 = Σ_{k<n} h·(N_k − νw·a_{k+1})            (discrete Duhamel / weak form)
  So along the step-halving sequence the coefficients satisfy the time-integrated Galerkin equation with Riemann sums
  of the force and the dissipation; with the Cauchy modulus of DyadicCoef this is the discrete form of
  a(t) − a(0) = ∫₀ᵗ (N(a) − νw a) ds for the limit.

  Lean 4 core plus NSChain (sumN), ns/EnstStep (StepC), ns/TimeLipN (tele_fwd), lib/RingTac.
-/
import ns.TimeLipN
import ns.AbelCount

namespace IntegralForm
open DetChain NSChain LibRing EnstStep TimeLipN

section Gen
variable {R : Type} (K : CRing R)

theorem step_diff (h ν w a' a n : R) (hs : StepC K h ν w a' a n) :
    K.add a' (K.neg a) = K.mul h (K.add n (K.neg (K.mul (K.mul ν w) a'))) := by
  unfold StepC at hs
  calc K.add a' (K.neg a) = K.add (K.add (K.mul (K.add K.one (K.mul (K.mul h ν) w)) a') (K.neg a))
        (K.neg (K.mul (K.mul (K.mul h ν) w) a')) := by lib_ring
    _ = K.add (K.add (K.add a (K.mul h n)) (K.neg a)) (K.neg (K.mul (K.mul (K.mul h ν) w) a')) := by rw [hs]
    _ = _ := by lib_ring

theorem integral (h ν w : R) (a N : Nat → R) (hs : ∀ k, StepC K h ν w (a (k + 1)) (a k) (N k)) (n : Nat) :
    K.add (a n) (K.neg (a 0)) = sumN K (fun k => K.mul h (K.add (N k) (K.neg (K.mul (K.mul ν w) (a (k + 1)))))) n := by
  rw [← tele_fwd K a n]
  exact AbelCount.sumN_congr K _ _ (fun k => step_diff K h ν w (a (k + 1)) (a k) (N k) (hs k)) n

end Gen

end IntegralForm

#print axioms IntegralForm.step_diff
#print axioms IntegralForm.integral

ns/Interp.lean

lean_all/ns/Interp.lean

/-
  ns/Interp.lean — T37 cycle 94: spectral interpolation Z² ≤ E·P for every truncation.

  Lean 4 core plus ns/SumCS (cs_sum) and lib/RingTac. For modes i ∈ l with amplitudes a_i and weights
  κ_i = |k_i|²:
      E = Σ a²,   Z = Σ κ a²,   P = Σ κ² a²       ⇒      Z² ≤ E · P
  (the discrete form of ‖∇u‖² ≤ ‖u‖·‖Δu‖, used to close enstrophy estimates between the energy and the
  dissipation; uniform in the truncation).
-/
import ns.SumCS

namespace Interp
open DetChain NSChain LibRing SumCS

variable {R : Type} (K : CRing R) {I : Type}

theorem interp (P : Cone K) (a κ : I → R) (l : List I) :
    Le K P (sq K (lsum K (fun i => K.mul (κ i) (K.mul (a i) (a i))) l))
      (K.mul (lsum K (fun i => K.mul (a i) (a i)) l) (lsum K (fun i => K.mul (K.mul (κ i) (κ i)) (K.mul (a i) (a i))) l)) := by
  have cs := cs_sum K P a (fun i => K.mul (κ i) (a i)) l
  have e1 : lsum K (fun i => K.mul (a i) ((fun i => K.mul (κ i) (a i)) i)) l
      = lsum K (fun i => K.mul (κ i) (K.mul (a i) (a i))) l :=
    lsum_ext K _ _ (fun i => by show K.mul (a i) (K.mul (κ i) (a i)) = _; lib_ring) l
  have e2 : lsum K (fun i => sq K (a i)) l = lsum K (fun i => K.mul (a i) (a i)) l := rfl
  have e3 : lsum K (fun i => sq K ((fun i => K.mul (κ i) (a i)) i)) l
      = lsum K (fun i => K.mul (K.mul (κ i) (κ i)) (K.mul (a i) (a i))) l :=
    lsum_ext K _ _ (fun i => by show sq K (K.mul (κ i) (a i)) = _; unfold sq; lib_ring) l
  rw [e1, e2, e3] at cs
  exact cs

end Interp

#print axioms Interp.interp

ns/InterpCross.lean

lean_all/ns/InterpCross.lean

/-
  ns/InterpCross.lean — T37 cycle 1722 (goal G850b): the piecewise-linear time interpolation of the scheme across
  steps. With u(k + θ) = a_k + θ(a_{k+1} − a_k), θ + e = 1 (θ, e ≥ 0), and for a later step
  u(k + j + θ') = b_0 + θ'(b_1 − b_0): if |a_1 − a_0|² ≤ G, |b_1 − b_0|² ≤ G and |b_0 − a_1|² ≤ M then
      |u(k + j + θ') − u(k + θ)|² ≤ 3(eG + M + θ'G).
  With M = (j − 1)G (GlobalHolder) the right side is 3|s' − s|G: the interpolants are uniformly Hölder-1/2 in
  continuous time. Over any cone; Lean 4 core, hand proofs, no axioms.
-/
import ns.InterpStep

namespace InterpCross
open DetChain NSChain LibRing InterpStep

section Gen
variable {R : Type} (K : CRing R) (P : Cone K)

def three : R := K.add K.one (K.add K.one K.one)

theorem nn_one : P.nn K.one := by
  have h := P.nn_sq K.one
  rw [K.one_mul] at h
  exact h

theorem nn_three : P.nn (three K) :=
  P.nn_add _ _ (nn_one K P) (P.nn_add _ _ (nn_one K P) (nn_one K P))

theorem le_add2' {x X y Y : R} (h1 : Le K P x X) (h2 : Le K P y Y) : Le K P (K.add x y) (K.add X Y) := by
  cases h1 with
  | intro d1 h1 =>
    cases h2 with
    | intro d2 h2 =>
      refine ⟨K.add d1 d2, P.nn_add _ _ h1.1 h2.1, ?_⟩
      rw [← h1.2, ← h2.2]
      lib_ring

theorem le_mul_l {x X : R} (c : R) (hc : P.nn c) (h : Le K P x X) : Le K P (K.mul c x) (K.mul c X) := by
  cases h with
  | intro d hd =>
    refine ⟨K.mul c d, P.nn_mul _ _ hc hd.1, ?_⟩
    rw [← hd.2]
    lib_ring

/-- (x + y + z)² ≤ 3(x² + y² + z²). -/
theorem three_sq (x y z : R) :
    Le K P (K.mul (K.add x (K.add y z)) (K.add x (K.add y z)))
      (K.mul (three K) (K.add (K.mul x x) (K.add (K.mul y y) (K.mul z z)))) := by
  refine ⟨K.add (K.mul (sub K x y) (sub K x y)) (K.add (K.mul (sub K y z) (sub K y z)) (K.mul (sub K x z) (sub K x z))),
    P.nn_add _ _ (P.nn_sq _) (P.nn_add _ _ (P.nn_sq _) (P.nn_sq _)), ?_⟩
  unfold three
  lib_ring

/-- **Across steps.** -/
theorem cross_step (a0 a1 b0 b1 θ e θ' e' G M : R)
    (hθ : P.nn θ) (he : P.nn e) (hθe : K.add θ e = K.one)
    (hθ' : P.nn θ') (he' : P.nn e') (hθe' : K.add θ' e' = K.one)
    (ha : Le K P (K.mul (sub K a1 a0) (sub K a1 a0)) G) (hb : Le K P (K.mul (sub K b1 b0) (sub K b1 b0)) G)
    (hm : Le K P (K.mul (sub K b0 a1) (sub K b0 a1)) M) :
    Le K P (K.mul (sub K (K.add b0 (K.mul θ' (sub K b1 b0))) (K.add a0 (K.mul θ (sub K a1 a0))))
                  (sub K (K.add b0 (K.mul θ' (sub K b1 b0))) (K.add a0 (K.mul θ (sub K a1 a0)))))
      (K.mul (three K) (K.add (K.mul e G) (K.add M (K.mul θ' G)))) := by
  have hθv : θ = sub K K.one e := by rw [← hθe]; lib_ring
  have e1 : sub K (K.add b0 (K.mul θ' (sub K b1 b0))) (K.add a0 (K.mul θ (sub K a1 a0)))
      = K.add (K.mul e (sub K a1 a0)) (K.add (sub K b0 a1) (K.mul θ' (sub K b1 b0))) := by
    rw [hθv]; lib_ring
  rw [e1]
  have he1 : Le K P e K.one := ⟨θ, hθ, by rw [← hθe]; lib_ring⟩
  have hθ1 : Le K P θ' K.one := ⟨e', he', hθe'⟩
  have hx := interp_step K P e (sub K a1 a0) G he he1 ha
  have hz := interp_step K P θ' (sub K b1 b0) G hθ' hθ1 hb
  have hs := le_add2' K P hx (le_add2' K P hm hz)
  exact le_trans' K P _ _ _ (three_sq K P _ _ _) (le_mul_l K P (three K) (nn_three K P) hs)

end Gen
end InterpCross

#print axioms InterpCross.nn_three
#print axioms InterpCross.le_add2'
#print axioms InterpCross.le_mul_l
#print axioms InterpCross.three_sq
#print axioms InterpCross.cross_step

ns/InterpGrid.lean

lean_all/ns/InterpGrid.lean

/-
  ns/InterpGrid.lean — T37 cycle 1724 (goal G850c): from the grid Hölder bound of the scheme
  (GlobalHolder: |a_{k+j} − a_k|² ≤ j·G for all k, j) to the continuous-time interpolant
  u(k + θ) = a_k + θ(a_{k+1} − a_k): for θ + e = 1, θ' + e' = 1 (all ≥ 0) and every k, j,
      |u(k + 1 + j + θ') − u(k + θ)|² ≤ 3(e·G + j·G + θ'·G) = 3·|s' − s|·G,
  and inside one step |u(k + θ) − a_k|² ≤ θ·G. Over any cone; Lean 4 core, hand proofs, no axioms.
-/
import ns.InterpCross

namespace InterpGrid
open DetChain NSChain LibRing InterpStep InterpCross

section Gen
variable {R : Type} (K : CRing R) (P : Cone K)

/-- j · x as a repeated sum. -/
def nm : Nat → R → R
  | 0, _ => K.zero
  | j + 1, x => K.add x (nm j x)

/-- The interpolant at k + θ. -/
def interp (a : Nat → R) (k : Nat) (θ : R) : R := K.add (a k) (K.mul θ (sub K (a (k + 1)) (a k)))

theorem nm_one (x : R) : nm K 1 x = x := by
  show K.add x K.zero = x
  lib_ring

theorem grid_interp (a : Nat → R) (G : R)
    (hH : ∀ k j, Le K P (K.mul (sub K (a (k + j)) (a k)) (sub K (a (k + j)) (a k))) (nm K j G))
    (k j : Nat) (θ e θ' e' : R) (hθ : P.nn θ) (he : P.nn e) (hθe : K.add θ e = K.one)
    (hθ' : P.nn θ') (he' : P.nn e') (hθe' : K.add θ' e' = K.one) :
    Le K P (K.mul (sub K (interp K a (k + 1 + j) θ') (interp K a k θ)) (sub K (interp K a (k + 1 + j) θ') (interp K a k θ)))
      (K.mul (three K) (K.add (K.mul e G) (K.add (nm K j G) (K.mul θ' G)))) := by
  have ha := hH k 1
  rw [nm_one] at ha
  have hb := hH (k + 1 + j) 1
  rw [nm_one] at hb
  exact cross_step K P (a k) (a (k + 1)) (a (k + 1 + j)) (a (k + 1 + j + 1)) θ e θ' e' G (nm K j G)
    hθ he hθe hθ' he' hθe' ha hb (hH (k + 1) j)

theorem step_interp (a : Nat → R) (G : R)
    (hH : ∀ k j, Le K P (K.mul (sub K (a (k + j)) (a k)) (sub K (a (k + j)) (a k))) (nm K j G))
    (k : Nat) (θ e : R) (hθ : P.nn θ) (he : P.nn e) (hθe : K.add θ e = K.one) :
    Le K P (K.mul (sub K (interp K a k θ) (a k)) (sub K (interp K a k θ) (a k))) (K.mul θ G) := by
  have ha := hH k 1
  rw [nm_one] at ha
  have e1 : sub K (interp K a k θ) (a k) = K.mul θ (sub K (a (k + 1)) (a k)) := by
    unfold interp; lib_ring
  rw [e1]
  exact interp_step K P θ _ G hθ ⟨e, he, hθe⟩ ha

end Gen
end InterpGrid

#print axioms InterpGrid.nm_one
#print axioms InterpGrid.grid_interp
#print axioms InterpGrid.step_interp

ns/InterpModes.lean

lean_all/ns/InterpModes.lean

/-
  ns/InterpModes.lean — T37 cycle 1728 (goal G850d): the continuous-time interpolation for the Galerkin scheme
  itself, with the mode-sum norm N(v) = Σ_r |v_r|² of GlobalHolder. For the interpolant
  u_r(k + θ) = a_{k,r} + θ(a_{k+1,r} − a_{k,r}) and θ + e = 1, θ' + e' = 1 (all ≥ 0): if N(a_{k+1} − a_k) ≤ G,
  N(a_{k+j+2} − a_{k+j+1}) ≤ G and N(a_{k+j+1} − a_{k+1}) ≤ M then
      N(u(k + 1 + j + θ') − u(k + θ)) ≤ 3(eG + M + θ'G).
  Per component the difference is e(a₁ − a₀) + (b₀ − a₁) + θ'(b₁ − b₀) (θ = 1 − e), then (x + y + z)² ≤ 3(x² + y² + z²)
  and the sum over modes. Over any cone; Lean 4 core, hand proofs, no axioms.
-/
import ns.InterpCross
import ns.ForceBound
import ns.EnstDiff

namespace InterpModes
open DetChain NSChain LibRing FourierGalerkin InterpStep InterpCross

section Gen
variable {R : Type} (K : CRing R) (P : Cone K) {I : Type}

/-- The interpolant at k + θ, mode r. -/
def interp3 (a : Nat → I → V3 R) (k : Nat) (θ : R) (r : I) : V3 R :=
  ⟨K.add (a k r).x (K.mul θ (sub K (a (k + 1) r).x (a k r).x)),
   K.add (a k r).y (K.mul θ (sub K (a (k + 1) r).y (a k r).y)),
   K.add (a k r).z (K.mul θ (sub K (a (k + 1) r).z (a k r).z))⟩

def nq (l : List I) (v : I → V3 R) : R := lsum K (fun r => dot3 K (v r) (v r)) l

def sq (x : R) : R := K.mul x x

/-- One coordinate. -/
theorem comp_bound (α0 α1 β0 β1 θ e θ' : R) (hθe : K.add θ e = K.one) :
    Le K P (sq K (sub K (K.add β0 (K.mul θ' (sub K β1 β0))) (K.add α0 (K.mul θ (sub K α1 α0)))))
      (K.mul (three K) (K.add (K.mul (K.mul e e) (sq K (sub K α1 α0)))
        (K.add (sq K (sub K β0 α1)) (K.mul (K.mul θ' θ') (sq K (sub K β1 β0)))))) := by
  have hθv : θ = sub K K.one e := by rw [← hθe]; lib_ring
  have e1 : sub K (K.add β0 (K.mul θ' (sub K β1 β0))) (K.add α0 (K.mul θ (sub K α1 α0)))
      = K.add (K.mul e (sub K α1 α0)) (K.add (sub K β0 α1) (K.mul θ' (sub K β1 β0))) := by
    rw [hθv]; lib_ring
  have e2 : K.mul (three K) (K.add (K.mul (K.mul e e) (sq K (sub K α1 α0)))
        (K.add (sq K (sub K β0 α1)) (K.mul (K.mul θ' θ') (sq K (sub K β1 β0)))))
      = K.mul (three K) (K.add (K.mul (K.mul e (sub K α1 α0)) (K.mul e (sub K α1 α0)))
        (K.add (K.mul (sub K β0 α1) (sub K β0 α1)) (K.mul (K.mul θ' (sub K β1 β0)) (K.mul θ' (sub K β1 β0))))) := by
    unfold sq; lib_ring
  unfold sq at e2 ⊢
  rw [e1, e2]
  exact three_sq K P _ _ _

theorem regroup (xa ya za xb yb zb xc yc zc e θ' : R) :
    K.add (K.add (K.mul (three K) (K.add (K.mul (K.mul e e) xa) (K.add xb (K.mul (K.mul θ' θ') xc))))
        (K.mul (three K) (K.add (K.mul (K.mul e e) ya) (K.add yb (K.mul (K.mul θ' θ') yc)))))
      (K.mul (three K) (K.add (K.mul (K.mul e e) za) (K.add zb (K.mul (K.mul θ' θ') zc))))
    = K.mul (three K) (K.add (K.mul (K.mul e e) (K.add (K.add xa ya) za))
        (K.add (K.add (K.add xb yb) zb) (K.mul (K.mul θ' θ') (K.add (K.add xc yc) zc)))) := by
  lib_ring

/-- One mode. -/
theorem mode_bound (A0 A1 B0 B1 : V3 R) (θ e θ' : R) (hθe : K.add θ e = K.one) :
    Le K P (dot3 K (EnstDiff.sub3 K
        (⟨K.add B0.x (K.mul θ' (sub K B1.x B0.x)), K.add B0.y (K.mul θ' (sub K B1.y B0.y)), K.add B0.z (K.mul θ' (sub K B1.z B0.z))⟩ : V3 R)
        (⟨K.add A0.x (K.mul θ (sub K A1.x A0.x)), K.add A0.y (K.mul θ (sub K A1.y A0.y)), K.add A0.z (K.mul θ (sub K A1.z A0.z))⟩ : V3 R))
      (EnstDiff.sub3 K
        (⟨K.add B0.x (K.mul θ' (sub K B1.x B0.x)), K.add B0.y (K.mul θ' (sub K B1.y B0.y)), K.add B0.z (K.mul θ' (sub K B1.z B0.z))⟩ : V3 R)
        (⟨K.add A0.x (K.mul θ (sub K A1.x A0.x)), K.add A0.y (K.mul θ (sub K A1.y A0.y)), K.add A0.z (K.mul θ (sub K A1.z A0.z))⟩ : V3 R)))
      (K.mul (three K) (K.add (K.mul (K.mul e e) (dot3 K (EnstDiff.sub3 K A1 A0) (EnstDiff.sub3 K A1 A0)))
        (K.add (dot3 K (EnstDiff.sub3 K B0 A1) (EnstDiff.sub3 K B0 A1))
          (K.mul (K.mul θ' θ') (dot3 K (EnstDiff.sub3 K B1 B0) (EnstDiff.sub3 K B1 B0)))))) := by
  have hx := comp_bound K P A0.x A1.x B0.x B1.x θ e θ' hθe
  have hy := comp_bound K P A0.y A1.y B0.y B1.y θ e θ' hθe
  have hz := comp_bound K P A0.z A1.z B0.z B1.z θ e θ' hθe
  have s := le_add2' K P (le_add2' K P hx hy) hz
  rw [regroup] at s
  exact s

/-- e ≤ 1, 0 ≤ x ≤ G: e²x ≤ eG. -/
theorem scale_le (e x G : R) (he : P.nn e) (he1 : Le K P e K.one) (hx : P.nn x) (h : Le K P x G) :
    Le K P (K.mul (K.mul e e) x) (K.mul e G) := by
  have h1 : Le K P (K.mul e x) G := le_trans' K P _ _ _ (mul_le_self K P e x he1 hx) h
  have e1 : K.mul (K.mul e e) x = K.mul e (K.mul e x) := by lib_ring
  rw [e1]
  exact le_mul_l K P e he h1

/-- **Across steps, summed over modes.** -/
theorem modes_cross (l : List I) (a : Nat → I → V3 R) (k j : Nat) (θ e θ' e' G M : R)
    (hθ : P.nn θ) (he : P.nn e) (hθe : K.add θ e = K.one)
    (hθ' : P.nn θ') (he' : P.nn e') (hθe' : K.add θ' e' = K.one)
    (hA : Le K P (nq K l (fun r => EnstDiff.sub3 K (a (k + 1) r) (a k r))) G)
    (hB : Le K P (nq K l (fun r => EnstDiff.sub3 K (a (k + 1 + j + 1) r) (a (k + 1 + j) r))) G)
    (hM : Le K P (nq K l (fun r => EnstDiff.sub3 K (a (k + 1 + j) r) (a (k + 1) r))) M) :
    Le K P (nq K l (fun r => EnstDiff.sub3 K (interp3 K a (k + 1 + j) θ' r) (interp3 K a k θ r)))
      (K.mul (three K) (K.add (K.mul e G) (K.add M (K.mul θ' G)))) := by
  have s := ForceBound.lsum_le K P _ _
    (fun r => mode_bound K P (a k r) (a (k + 1) r) (a (k + 1 + j) r) (a (k + 1 + j + 1) r) θ e θ' hθe) l
  rw [lsum_mul_left, lsum_add, lsum_mul_left, lsum_add, lsum_mul_left] at s
  have he1 : Le K P e K.one := ⟨θ, hθ, by rw [← hθe]; lib_ring⟩
  have hθ1 : Le K P θ' K.one := ⟨e', he', hθe'⟩
  have nA := ForceBound.lsum_nn K P (fun r => dot3 K (EnstDiff.sub3 K (a (k + 1) r) (a k r)) (EnstDiff.sub3 K (a (k + 1) r) (a k r)))
    (fun r => ForceBound.dot3_nn K P _) l
  have nC := ForceBound.lsum_nn K P (fun r => dot3 K (EnstDiff.sub3 K (a (k + 1 + j + 1) r) (a (k + 1 + j) r)) (EnstDiff.sub3 K (a (k + 1 + j + 1) r) (a (k + 1 + j) r)))
    (fun r => ForceBound.dot3_nn K P _) l
  have b1 := scale_le K P e _ G he he1 nA hA
  have b3 := scale_le K P θ' _ G hθ' hθ1 nC hB
  have b := le_mul_l K P (three K) (nn_three K P) (le_add2' K P b1 (le_add2' K P hM b3))
  exact le_trans' K P _ _ _ s b

/-- **Weighted form** (w = ν³ in GlobalHolder): bounds on w·N give the bound on w·N of the interpolant difference. -/
theorem modes_cross_w (l : List I) (a : Nat → I → V3 R) (k j : Nat) (w θ e θ' e' G M : R) (hw : P.nn w)
    (hθ : P.nn θ) (he : P.nn e) (hθe : K.add θ e = K.one)
    (hθ' : P.nn θ') (he' : P.nn e') (hθe' : K.add θ' e' = K.one)
    (hA : Le K P (K.mul w (nq K l (fun r => EnstDiff.sub3 K (a (k + 1) r) (a k r)))) G)
    (hB : Le K P (K.mul w (nq K l (fun r => EnstDiff.sub3 K (a (k + 1 + j + 1) r) (a (k + 1 + j) r)))) G)
    (hM : Le K P (K.mul w (nq K l (fun r => EnstDiff.sub3 K (a (k + 1 + j) r) (a (k + 1) r)))) M) :
    Le K P (K.mul w (nq K l (fun r => EnstDiff.sub3 K (interp3 K a (k + 1 + j) θ' r) (interp3 K a k θ r))))
      (K.mul (three K) (K.add (K.mul e G) (K.add M (K.mul θ' G)))) := by
  have s := ForceBound.lsum_le K P _ _
    (fun r => mode_bound K P (a k r) (a (k + 1) r) (a (k + 1 + j) r) (a (k + 1 + j + 1) r) θ e θ' hθe) l
  rw [lsum_mul_left, lsum_add, lsum_mul_left, lsum_add, lsum_mul_left] at s
  have sw := le_mul_l K P w hw s
  have he1 : Le K P e K.one := ⟨θ, hθ, by rw [← hθe]; lib_ring⟩
  have hθ1 : Le K P θ' K.one := ⟨e', he', hθe'⟩
  have nA := P.nn_mul _ _ hw (ForceBound.lsum_nn K P (fun r => dot3 K (EnstDiff.sub3 K (a (k + 1) r) (a k r)) (EnstDiff.sub3 K (a (k + 1) r) (a k r)))
    (fun r => ForceBound.dot3_nn K P _) l)
  have nC := P.nn_mul _ _ hw (ForceBound.lsum_nn K P (fun r => dot3 K (EnstDiff.sub3 K (a (k + 1 + j + 1) r) (a (k + 1 + j) r)) (EnstDiff.sub3 K (a (k + 1 + j + 1) r) (a (k + 1 + j) r)))
    (fun r => ForceBound.dot3_nn K P _) l)
  have b1 := scale_le K P e _ G he he1 nA hA
  have b3 := scale_le K P θ' _ G hθ' hθ1 nC hB
  have b := le_mul_l K P (three K) (nn_three K P) (le_add2' K P b1 (le_add2' K P hM b3))
  have e1 : ∀ A B C : R, K.mul w (K.mul (three K) (K.add (K.mul (K.mul e e) A) (K.add B (K.mul (K.mul θ' θ') C))))
      = K.mul (three K) (K.add (K.mul (K.mul e e) (K.mul w A)) (K.add (K.mul w B) (K.mul (K.mul θ' θ') (K.mul w C)))) := by
    intro A B C; unfold three; lib_ring
  rw [e1] at sw
  exact le_trans' K P _ _ _ sw b

/-- **Hölder modulus of the interpolants from a grid bound** (the conclusion of GlobalHolder.global_holder,
    w = ν³): w·N(a(T + k) − a(T)) ≤ k·C for all T, k gives, for s = k + θ and s' = k + 1 + j + θ',
    w·N(u(s') − u(s)) ≤ 3(e·C + j·C + θ'·C) = 3(s' − s)·C. -/
theorem holder_interp (l : List I) (a : Nat → I → V3 R) (w C : R) (hw : P.nn w)
    (HG : ∀ T k, Le K P (K.mul w (nq K l (fun r => EnstDiff.sub3 K (a (T + k) r) (a T r)))) (K.mul (castN K k) C))
    (k j : Nat) (θ e θ' e' : R) (hθ : P.nn θ) (he : P.nn e) (hθe : K.add θ e = K.one)
    (hθ' : P.nn θ') (he' : P.nn e') (hθe' : K.add θ' e' = K.one) :
    Le K P (K.mul w (nq K l (fun r => EnstDiff.sub3 K (interp3 K a (k + 1 + j) θ' r) (interp3 K a k θ r))))
      (K.mul (three K) (K.add (K.mul e (K.mul (castN K 1) C)) (K.add (K.mul (castN K j) C) (K.mul θ' (K.mul (castN K 1) C))))) :=
  modes_cross_w K P l a k j w θ e θ' e' (K.mul (castN K 1) C) (K.mul (castN K j) C) hw hθ he hθe hθ' he' hθe'
    (HG k 1) (HG (k + 1 + j) 1) (HG (k + 1) j)

end Gen
end InterpModes

#print axioms InterpModes.comp_bound
#print axioms InterpModes.regroup
#print axioms InterpModes.mode_bound
#print axioms InterpModes.scale_le
#print axioms InterpModes.modes_cross
#print axioms InterpModes.modes_cross_w
#print axioms InterpModes.holder_interp

ns/InterpStep.lean

lean_all/ns/InterpStep.lean

/-
  ns/InterpStep.lean — T37 cycle 1720 (goal G850a): the time interpolation of the scheme inside one step keeps the
  Hölder-1/2 modulus. Over an ordered commutative ring (cone P): if 0 ≤ θ ≤ 1 and d² ≤ G then (θd)² ≤ θG, i.e.
  for u(k + θ) = a_k + θ(a_{k+1} − a_k) one has |u(k + θ) − a_k|² ≤ θ·G whenever |a_{k+1} − a_k|² ≤ G
  (GlobalHolder with k = 1). Summed over any list of modes as well. Lean 4 core, hand proofs, no axioms.
-/
import NSChain
import lib.RingTac

namespace InterpStep
open DetChain NSChain LibRing

section Gen
variable {R : Type} (K : CRing R) (P : Cone K)

/-- θ ≤ 1 and 0 ≤ x give θx ≤ x. -/
theorem mul_le_self (θ x : R) (hθ1 : Le K P θ K.one) (hx : P.nn x) : Le K P (K.mul θ x) x := by
  cases hθ1 with
  | intro e he =>
    refine ⟨K.mul e x, P.nn_mul _ _ he.1 hx, ?_⟩
    have h : K.add (K.mul θ x) (K.mul e x) = K.mul (K.add θ e) x := by lib_ring
    rw [h, he.2, K.one_mul]

/-- **One step.** 0 ≤ θ ≤ 1 and d² ≤ G give (θd)² ≤ θG. -/
theorem interp_step (θ d G : R) (hθ : P.nn θ) (hθ1 : Le K P θ K.one) (hd : Le K P (K.mul d d) G) :
    Le K P (K.mul (K.mul θ d) (K.mul θ d)) (K.mul θ G) := by
  have h1 : Le K P (K.mul θ (K.mul d d)) (K.mul d d) := mul_le_self K P θ (K.mul d d) hθ1 (P.nn_sq d)
  have h2 : Le K P (K.mul θ (K.mul d d)) G := le_trans' K P _ _ _ h1 hd
  have e1 : K.mul (K.mul θ d) (K.mul θ d) = K.mul θ (K.mul θ (K.mul d d)) := by lib_ring
  rw [e1]
  cases h2 with
  | intro e he =>
    refine ⟨K.mul θ e, P.nn_mul _ _ hθ he.1, ?_⟩
    rw [← he.2]
    lib_ring

end Gen
end InterpStep

#print axioms InterpStep.mul_le_self
#print axioms InterpStep.interp_step

ns/L3Interp.lean

lean_all/ns/L3Interp.lean

/-
  ns/L3Interp.lean — T37 cycle 167: two square-root free bounds for the critical L³ norm (R1 branch).

  Lean 4 core plus ns/SumCS (cs_sum), ns/AgmonTime (lsum_le, lsum_nn), ns/Riccati, lib/RingTac.
  For values u_i ≥ 0 on a grid (or Fourier weights):
    l3_l2l4     (Σ u³)² ≤ (Σ u²)(Σ u⁴)                (Hölder 3 = (2 + 4)/2, by Cauchy–Schwarz)
    l3_linf     u_i ≤ M  ⇒  Σ u³ ≤ M Σ u²            (L³³ ≤ ‖u‖_∞ ‖u‖²₂)
  With the energy bound on Σu² the critical norm is controlled by ‖u‖_∞ (or by L⁴): the R1-gap asks for a
  bound of Σu³ by the data alone, which neither inequality gives.
-/
import ns.AgmonTime

namespace L3Interp
open DetChain NSChain LibRing SumCS AgmonTime

variable {R : Type} (K : CRing R) {I : Type}

theorem l3_l2l4 (P : Cone K) (u : I → R) (l : List I) :
    Le K P (sq K (lsum K (fun i => K.mul (u i) (sq K (u i))) l))
      (K.mul (lsum K (fun i => sq K (u i)) l) (lsum K (fun i => sq K (sq K (u i))) l)) :=
  cs_sum K P u (fun i => sq K (u i)) l

theorem l3_linf (P : Cone K) (u : I → R) (M : R) (hu : ∀ i, Le K P (u i) M) (l : List I) :
    Le K P (lsum K (fun i => K.mul (u i) (sq K (u i))) l) (K.mul M (lsum K (fun i => sq K (u i)) l)) := by
  have s := lsum_le K P (fun i => K.mul (u i) (sq K (u i))) (fun i => K.mul M (sq K (u i))) (fun i => by
    have m := Riccati.le_mul_left K P (sq K (u i)) _ _ (P.nn_sq (u i)) (hu i)
    rw [show K.mul (sq K (u i)) (u i) = K.mul (u i) (sq K (u i)) by lib_ring,
        show K.mul (sq K (u i)) M = K.mul M (sq K (u i)) by lib_ring] at m
    exact m) l
  rw [lsum_mul_left K M (fun i => sq K (u i)) l] at s
  exact s

end L3Interp

#print axioms L3Interp.l3_l2l4
#print axioms L3Interp.l3_linf

ns/LargeCauchy.lean

lean_all/ns/LargeCauchy.lean

/-
  ns/LargeCauchy.lean — T37 (goal G859c2g, piece c2): the truncation Cauchy estimate on the large-data window, over an
  ordered field with inverses. If the large truncation A and the small truncation B satisfy the window H² energy
  bounds of H2LargeAll (νA_t + hε B_t ≤ E + t·h·D for t ≤ n) and Z(A_t) ≤ Zb for t < n, then with
  Aa = (E_A + n·h·D_A)/ν and Ab = (E_B + n·h·D_B)/ν (WindowA) the explicit truncation Cauchy bound of
  TruncRateUpto.trunc_rate_upto holds for every T ≤ n:
    large_cauchy   ν^T Q_d(T) ≤ (ν + hα)^T (Q_d(0) + T·h·S),  S → 0 as the cutoff grows,
  i.e. on the large-data local-time window the Galerkin truncations form a Cauchy family.

  Lean 4 core plus ns/TruncRateUpto, ns/WindowA.
-/
import ns.TruncRateUpto
import ns.WindowA

namespace LargeCauchy
open DetChain NSChain DyadicSmallGlobal LibRing DyadicLarge FourierGalerkin GalerkinForce ForceBound ForceSob
  ModeCount CubeCount ForceUniform NormCompare EnstStep EnstGlobal EnstGalerkin ForceTwoScale ForceBilin EnstDiff
  ForceDiff TruncError TruncBound RestrStep GronwallSource EnstGronwall WeightSplit LatticeSum TruncCauchy TruncRate TruncRate

section Gen
variable {R : Type} (K : CRing R) (O : OrdR K) {I : Type}
variable (inv : R → R) (mul_inv : ∀ y, y ≠ K.zero → K.mul y (inv y) = K.one) (h10 : K.one ≠ K.zero)

include mul_inv h10 in
/-- **Truncation Cauchy on the large-data window.** -/
theorem large_cauchy (l : List I) (kz : I → Mode) (hd : List.Pairwise (fun a b => kz a ≠ kz b) l)
    (h1 : ∀ p, 1 ≤ nrm (kz p)) (M : Nat) (hM : ∀ p, p ∈ l → nrm (kz p) ≤ M + 1) (Rc R' t0 : Nat) (hRM : R' ≤ M)
    (ι h ν Zb Aa Ab iR : R) (hh : O.nn h) (hν : O.nn ν) (hZb : O.nn Zb) (hAa : O.nn Aa) (hAb : O.nn Ab) (hiRn : O.nn iR)
    (hiR : K.mul (K.mul (castN K (Rc + 1)) (castN K (Rc + 1))) iR = K.one)
    (A B : Nat → I → V3 R)
    (hA : ∀ t r, Step K h ν (wt K kz r) (A (t + 1) r) (A t r) (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (A t) (A t) r))
    (hB : ∀ t r, r ∈ l.filter (lowB Rc kz) → Step K h ν (wt K kz r) (B (t + 1) r) (B t r)
      (Nl K (l.filter (lowB Rc kz)) (TriadSparse.delta K kz) ι (kv K kz) (B t) (B t) r))
    (n : Nat) (hZA : ∀ t, t < n → Le K O.toCone (Zl K kz l (A t)) Zb)
    (εA εB DA DB EA EB : R) (hεA : O.nn εA) (hεB : O.nn εB) (hDA : O.nn DA) (hDB : O.nn DB) (hν0 : ν ≠ K.zero)
    (hEA : ∀ t, t ≤ n → Le K O.toCone (K.add (K.mul ν (Al K kz l (A t)))
      (K.mul (K.mul h εA) (lsum K (fun p => K.mul (K.mul (K.mul (wt K kz p) (wt K kz p)) (wt K kz p)) (dot3 K (A t p) (A t p))) l)))
      (K.add EA (K.mul (castN K t) (K.mul h DA))))
    (hEB : ∀ t, t ≤ n → Le K O.toCone (K.add (K.mul ν (Al K kz (l.filter (lowB Rc kz)) (B t)))
      (K.mul (K.mul h εB) (lsum K (fun p => K.mul (K.mul (K.mul (wt K kz p) (wt K kz p)) (wt K kz p)) (dot3 K (B t p) (B t p)))
        (l.filter (lowB Rc kz)))))
      (K.add EB (K.mul (castN K t) (K.mul h DB))))
    (hAa_def : Aa = K.mul (K.add EA (K.mul (castN K n) (K.mul h DA))) (inv ν))
    (hAb_def : Ab = K.mul (K.add EB (K.mul (castN K n) (K.mul h DB))) (inv ν))
    (hβc : Le K O.toCone (K.mul (K.mul (two' K) (K.mul (two' K) (two' K))) (K.mul (K.mul ι ι) (K.mul (C2' K inv R' t0) Zb)))
      (K.mul ν ν)) (T : Nat) (hT : T ≤ n) :
    Le K O.toCone (K.mul (pwr K ν T)
        (Q K ν h (fun t => Zl K kz (l.filter (lowB Rc kz)) (dF K (restr K (lowB Rc kz) (A t)) (B t)))
          (fun t => Al K kz (l.filter (lowB Rc kz)) (dF K (restr K (lowB Rc kz) (A t)) (B t))) T))
      (K.mul (pwr K (K.add ν (K.mul h
          (K.add (K.mul (K.mul (two' K) (K.mul (two' K) (two' K))) (K.mul (K.mul ι ι) (K.mul (C1' K inv R') Zb)))
            (K.mul (K.mul (two' K) (two' K)) (K.mul (K.mul ι ι) (K.mul (castN K 135) Ab)))))) T)
        (K.add (Q K ν h (fun t => Zl K kz (l.filter (lowB Rc kz)) (dF K (restr K (lowB Rc kz) (A t)) (B t)))
          (fun t => Al K kz (l.filter (lowB Rc kz)) (dF K (restr K (lowB Rc kz) (A t)) (B t))) 0)
          (K.mul (castN K T) (K.mul h (K.mul (two' K)
            (K.add
              (K.mul (two' K) (K.add
                (K.mul (two' K) (K.mul (K.mul ι ι) (K.mul (K.mul Aa iR) (K.mul (C1' K inv R') Zb))))
                (K.mul (two' K) (K.mul (K.mul ι ι) (K.mul Aa (K.mul (C2' K inv R' t0) Zb))))))
              (K.mul (two' K) (K.mul (K.mul ι ι) (K.mul Aa (K.mul (castN K 135) (K.mul Aa iR))))))))))) := by
  have nB3 : ∀ (L : List I) (u : I → V3 R), O.nn (lsum K (fun p => K.mul (K.mul (K.mul (wt K kz p) (wt K kz p)) (wt K kz p))
      (dot3 K (u p) (u p))) L) := fun L u =>
    lsum_nn K O.toCone _ (fun p => O.nn_mul _ _ (O.nn_mul _ _ (O.nn_sq _) (dot3_nn K O.toCone _)) (dot3_nn K O.toCone _)) L
  have wa := WindowA.window_a K O inv mul_inv ν h εA DA EA (fun t => Al K kz l (A t))
    (fun t => lsum K (fun p => K.mul (K.mul (K.mul (wt K kz p) (wt K kz p)) (wt K kz p)) (dot3 K (A t p) (A t p))) l)
    h10 hν hν0 hh hεA hDA (fun t => nB3 l (A t)) n hEA
  have wb := WindowA.window_a K O inv mul_inv ν h εB DB EB (fun t => Al K kz (l.filter (lowB Rc kz)) (B t))
    (fun t => lsum K (fun p => K.mul (K.mul (K.mul (wt K kz p) (wt K kz p)) (wt K kz p)) (dot3 K (B t p) (B t p)))
      (l.filter (lowB Rc kz)))
    h10 hν hν0 hh hεB hDB (fun t => nB3 _ (B t)) n hEB
  exact TruncRateUpto.trunc_rate_upto K O inv mul_inv h10 l kz hd h1 M hM Rc R' t0 hRM ι h ν Zb Aa Ab iR hh hν hZb hAa hAb
    hiRn hiR A B hA hB n hZA (fun t ht => by rw [hAa_def]; exact wa t (Nat.le_of_lt ht))
    (fun t ht => by rw [hAb_def]; exact wb t (Nat.le_of_lt ht)) hβc T hT

end Gen

end LargeCauchy

#print axioms LargeCauchy.large_cauchy

ns/LatticeBound.lean

lean_all/ns/LatticeBound.lean

/-
  ns/LatticeBound.lean — T37 cycle 958 (goal G624c, piece 8c): the uniform lattice bound. Over an ordered field,
  for every duplicate-free list l of nonzero modes k ∈ ℤ³ (any truncation, any M ≥ |k|_∞):
    lattice_bound   Σ_{k∈l} |k|_∞^{−4} ≤ 135.
  Abel identity (AbelCount.abel_list) = shell sum (≤ 108, LatticeSum.shell_sum) + |l|·(M+1)^{−4} (≤ 27, since
  |l| ≤ (2M+3)³ ≤ 27(M+1)³ by the cube count). This is the factor Σ_p ν_p² of ForceSob for s = 2, bounded
  independently of the Galerkin truncation.

  Lean 4 core plus ns/LatticeSum, ns/ModeCount, ns/PolyBound (mul3).
-/
import ns.LatticeSum
import ns.ModeCount

namespace LatticeBound
open DetChain NSChain DyadicSmallGlobal LibRing DyadicLarge AbelCount LatticeStep LatticeSum ModeCount CubeCount

theorem mem_norms (n : Nat) : ∀ (l : List Mode), n ∈ norms l → ∃ k, k ∈ l ∧ n = nrm k
  | [], h => nomatch h
  | k :: l, h => by
    cases h with
    | head => exact ⟨k, List.Mem.head _, rfl⟩
    | tail _ h' =>
      have ⟨k', hk', e⟩ := mem_norms n l h'
      exact ⟨k', List.Mem.tail _ hk', e⟩

theorem cnt_all (j : Nat) : ∀ (L : List Nat), (∀ n, n ∈ L → n ≤ j) → cnt L j = L.length
  | [], _ => rfl
  | n :: L, h => by
    show cond (Nat.ble n j) (cnt L j + 1) (cnt L j) = L.length + 1
    rw [Nat.ble_eq_true_of_le (h n (List.Mem.head _)), cnt_all j L (fun x hx => h x (List.Mem.tail _ hx))]
    rfl

theorem cube27 (a : Nat) (ha : 1 ≤ a) : (2 * a + 1) * ((2 * a + 1) * (2 * a + 1)) ≤ 27 * (a * a * a) := by
  have h3 : 2 * a + 1 ≤ 3 * a := by
    have : 2 * a + 1 ≤ 2 * a + a := Nat.add_le_add_left ha _
    have e : 2 * a + a = 3 * a := by lib_nat
    rw [← e]; exact this
  have m := PolyBound.mul3 h3
  have e1 : (2 * a + 1) * ((2 * a + 1) * (2 * a + 1)) = (2 * a + 1) * (2 * a + 1) * (2 * a + 1) := by lib_nat
  have e2 : 3 * a * (3 * a) * (3 * a) = 27 * (a * a * a) := by lib_nat
  rw [e1, ← e2]; exact m

section Gen
variable {R : Type} (K : CRing R) (O : OrdR K)
variable (inv : R → R) (mul_inv : ∀ y, y ≠ K.zero → K.mul y (inv y) = K.one) (h10 : K.one ≠ K.zero)

include mul_inv h10 in
/-- the last Abel term: c·(M+1)^{−4} ≤ 27 when c ≤ (2(M+1)+1)³ -/
theorem last_term (M c : Nat) (hc : c ≤ (2 * (M + 1) + 1) * ((2 * (M + 1) + 1) * (2 * (M + 1) + 1))) :
    Le K O.toCone (K.mul (castN K c) (f4 K inv (M + 1))) (castN K 27) := by
  have hc27 := Nat.le_trans hc (cube27 (M + 1) (Nat.succ_le_succ (Nat.zero_le M)))
  have m0 := RatModel.cast_mono K O.toCone _ _ hc27
  have hA := iv_mul K O inv mul_inv h10 M
  have hiA := iv_nn K O inv mul_inv h10 M
  have cR : castN K (27 * ((M + 1) * (M + 1) * (M + 1)))
      = K.mul (castN K 27) (K.mul (K.mul (castN K (M + 1)) (castN K (M + 1))) (castN K (M + 1))) := by
    rw [castN_mul K 27, castN_mul K ((M + 1) * (M + 1)), castN_mul K (M + 1) (M + 1)]
  rw [cR] at m0
  have hMn : O.nn (castN K M) := RatModel.nn_cast K O.toCone M
  have f4e : f4 K inv (M + 1) = p4 K (iv K inv (M + 1)) := rfl
  rw [f4e]
  generalize iv K inv (M + 1) = iA at hA hiA ⊢
  have cA : castN K (M + 1) = K.add K.one (castN K M) := rfl
  rw [cA] at hA m0
  generalize castN K M = X at hA m0 hMn
  have p4n : O.nn (p4 K iA) := by unfold p4; exact O.nn_mul _ _ (O.nn_sq _) (O.nn_sq _)
  have s1 := le_mul_nn K O.toCone _ _ _ p4n m0
  rw [K.mul_comm (p4 K iA), K.mul_comm (p4 K iA)] at s1
  -- 27 A³ iA⁴ = 27 (A iA)³ iA = 27 iA
  have e : K.mul (K.mul (castN K 27) (K.mul (K.mul (K.add K.one X) (K.add K.one X)) (K.add K.one X))) (p4 K iA)
      = K.mul (castN K 27) iA := by
    have : K.mul (K.mul (castN K 27) (K.mul (K.mul (K.add K.one X) (K.add K.one X)) (K.add K.one X))) (p4 K iA)
        = K.mul (K.mul (castN K 27) (K.mul (K.mul (K.mul (K.add K.one X) iA) (K.mul (K.add K.one X) iA))
            (K.mul (K.add K.one X) iA))) iA := by unfold p4; lib_ring
    rw [this, hA]; lib_ring
  rw [e] at s1
  -- iA ≤ 1 since 1 = iA + X·iA
  have s2 : Le K O.toCone iA K.one := ⟨K.mul X iA, O.nn_mul _ _ hMn hiA, by rw [← hA]; lib_ring⟩
  have s3 := le_mul_nn K O.toCone (castN K 27) _ _ (RatModel.nn_cast K O.toCone 27) s2
  have e3 : K.mul (castN K 27) K.one = castN K 27 := by lib_ring
  rw [e3] at s3
  exact le_trans' K O.toCone _ _ _ s1 s3

include mul_inv h10 in
/-- Σ_{k∈l} |k|_∞^{−4} ≤ 135 for every duplicate-free list of nonzero modes -/
theorem lattice_bound (l : List Mode) (hd : List.Pairwise (fun a b => a ≠ b) l)
    (h1 : ∀ k, k ∈ l → 1 ≤ nrm k) (M : Nat) (hM : ∀ k, k ∈ l → nrm k ≤ M + 1) :
    Le K O.toCone (lsumN K (f4 K inv) (norms l)) (castN K 135) := by
  have hb : ∀ n, n ∈ norms l → n ≤ M + 1 := fun n hn => by
    have ⟨k, hk, e⟩ := mem_norms n l hn
    rw [e]; exact hM k hk
  have h1' : ∀ n, n ∈ norms l → 1 ≤ n := fun n hn => by
    have ⟨k, hk, e⟩ := mem_norms n l hn
    rw [e]; exact h1 k hk
  rw [abel_list K (f4 K inv) (M + 1) (norms l) hb]
  have S := shell_sum K O inv mul_inv h10 (norms l) h1' (fun j => cnt_cube l hd j) (M + 1)
  have hlen : (norms l).length = cnt (norms l) (M + 1) := (cnt_all (M + 1) (norms l) hb).symm
  have T := last_term K O inv mul_inv h10 M (norms l).length (by rw [hlen]; exact cnt_cube l hd (M + 1))
  have U := le_add' K O.toCone _ _ _ _ S T
  have e : K.add (castN K 108) (castN K 27) = castN K 135 := (RatModel.cast_add K 108 27).symm
  rw [e] at U; exact U

end Gen

end LatticeBound

#print axioms LatticeBound.mem_norms
#print axioms LatticeBound.cnt_all
#print axioms LatticeBound.cube27
#print axioms LatticeBound.last_term
#print axioms LatticeBound.lattice_bound

ns/LatticeBound2.lean

lean_all/ns/LatticeBound2.lean

/-
  ns/LatticeBound2.lean — T37 cycle 1956 (goal G859c2b1, piece c): the low Agmon weight. Over an ordered field, for
  every duplicate-free list l of nonzero modes k ∈ ℤ³ with |k|_∞ ≤ M + 1:
    last_term2      |l|·(M+1)^{−2} ≤ 27·(M+1)        (|l| ≤ (2M+3)³ ≤ 27(M+1)³)
    lattice_bound2  Σ_{k∈l} |k|_∞^{−2} ≤ 108·(M+1)
  (Abel identity AbelCount.abel_list, shell sum LatticeSum2.shell2_sum ≤ 81(M+1), last term ≤ 27(M+1)). This is the
  factor Σ_p ν_p² of ForceSob on the low shells of the Agmon cut, linear in the cut.

  Lean 4 core plus ns/LatticeBound, ns/LatticeSum2.
-/
import ns.LatticeBound
import ns.LatticeSum2

namespace LatticeBound2
open DetChain NSChain DyadicSmallGlobal LibRing DyadicLarge AbelCount LatticeStep LatticeSum ModeCount CubeCount
  LatticeBound LatticeStep2 LatticeSum2

section Gen
variable {R : Type} (K : CRing R) (O : OrdR K)
variable (inv : R → R) (mul_inv : ∀ y, y ≠ K.zero → K.mul y (inv y) = K.one) (h10 : K.one ≠ K.zero)

include mul_inv h10 in
theorem last_term2 (M c : Nat) (hc : c ≤ (2 * (M + 1) + 1) * ((2 * (M + 1) + 1) * (2 * (M + 1) + 1))) :
    Le K O.toCone (K.mul (castN K c) (f2 K inv (M + 1))) (K.mul (castN K 27) (castN K (M + 1))) := by
  have hc27 := Nat.le_trans hc (cube27 (M + 1) (Nat.succ_le_succ (Nat.zero_le M)))
  have m0 := RatModel.cast_mono K O.toCone _ _ hc27
  have hA := iv_mul K O inv mul_inv h10 M
  have cR : castN K (27 * ((M + 1) * (M + 1) * (M + 1)))
      = K.mul (castN K 27) (K.mul (K.mul (castN K (M + 1)) (castN K (M + 1))) (castN K (M + 1))) := by
    rw [castN_mul K 27, castN_mul K ((M + 1) * (M + 1)), castN_mul K (M + 1) (M + 1)]
  rw [cR] at m0
  have f2e : f2 K inv (M + 1) = p2 K (iv K inv (M + 1)) := rfl
  rw [f2e]
  generalize iv K inv (M + 1) = iA at hA ⊢
  generalize castN K (M + 1) = A at hA m0 ⊢
  have p2n : O.nn (p2 K iA) := O.nn_sq _
  have s1 := le_mul_nn K O.toCone _ _ _ p2n m0
  rw [K.mul_comm (p2 K iA), K.mul_comm (p2 K iA)] at s1
  have e : K.mul (K.mul (castN K 27) (K.mul (K.mul A A) A)) (p2 K iA) = K.mul (castN K 27) A := by
    have : K.mul (K.mul (castN K 27) (K.mul (K.mul A A) A)) (p2 K iA)
        = K.mul (K.mul (castN K 27) A) (K.mul (K.mul A iA) (K.mul A iA)) := by unfold p2; lib_ring
    rw [this, hA]; lib_ring
  rw [e] at s1
  exact s1

include mul_inv h10 in
/-- Σ_{k∈l} |k|_∞^{−2} ≤ 108·(M+1) for every duplicate-free list of nonzero modes with |k|_∞ ≤ M + 1 -/
theorem lattice_bound2 (l : List Mode) (hd : List.Pairwise (fun a b => a ≠ b) l)
    (h1 : ∀ k, k ∈ l → 1 ≤ nrm k) (M : Nat) (hM : ∀ k, k ∈ l → nrm k ≤ M + 1) :
    Le K O.toCone (lsumN K (f2 K inv) (norms l)) (K.mul (castN K 108) (castN K (M + 1))) := by
  have hb : ∀ n, n ∈ norms l → n ≤ M + 1 := fun n hn => by
    have ⟨k, hk, e⟩ := mem_norms n l hn
    rw [e]; exact hM k hk
  have h1' : ∀ n, n ∈ norms l → 1 ≤ n := fun n hn => by
    have ⟨k, hk, e⟩ := mem_norms n l hn
    rw [e]; exact h1 k hk
  rw [abel_list K (f2 K inv) (M + 1) (norms l) hb]
  have S := shell2_sum K O inv mul_inv h10 (norms l) h1' (fun j => cnt_cube l hd j) (M + 1)
  have hlen : (norms l).length = cnt (norms l) (M + 1) := (cnt_all (M + 1) (norms l) hb).symm
  have T := last_term2 K O inv mul_inv h10 M (norms l).length (by rw [hlen]; exact cnt_cube l hd (M + 1))
  have U := le_add' K O.toCone _ _ _ _ S T
  refine le_trans' K O.toCone _ _ _ U ⟨K.zero, O.nn_zero, ?_⟩
  rw [show castN K 108 = K.add (castN K 81) (castN K 27) from RatModel.cast_add K 81 27]
  lib_ring

end Gen

end LatticeBound2

#print axioms LatticeBound2.last_term2
#print axioms LatticeBound2.lattice_bound2

ns/LatticeStep.lean

lean_all/ns/LatticeStep.lean

/-
  ns/LatticeStep.lean — T37 cycle 951 (goal G624c, piece 8a): the per-shell inequality over a commutative ring
  with a nonnegative cone. With A·iA = 1, B·iB = 1, B = A + 1, iA, iB ≥ 0, D = B³ + B²A + BA² + A³ and a
  nonnegative W with C·D + W = 108·A³B³ (PolyBound.poly_bound, cast):
    step_le   C·(iA⁴ − iB⁴) ≤ 108·(iA − iB),
  i.e. (2j+1)³ (j^{−4} − (j+1)^{−4}) ≤ 108 (j^{−1} − (j+1)^{−1}); summed over j this telescopes.
  Proof: iA⁴ − iB⁴ = D·iA⁴iB⁴ and iA − iB = iA·iB = A³B³·iA⁴iB⁴, so the difference is W·iA⁴iB⁴ ≥ 0.

  Lean 4 core plus NSChain (Cone, Le), lib/RingTac.
-/
import ns.DyadicLarge
import lib.RingTac

namespace LatticeStep
open DetChain NSChain LibRing

section Gen
variable {R : Type} (K : CRing R) (P : Cone K)

def p4 (x : R) : R := K.mul (K.mul x x) (K.mul x x)
def p3 (x : R) : R := K.mul (K.mul x x) x

theorem inv4_diff (A B iA iB : R) (hA : K.mul A iA = K.one) (hB : K.mul B iB = K.one) (hBA : B = K.add A K.one) :
    K.add (p4 K iA) (K.neg (p4 K iB)) =
      K.mul (K.add (K.add (K.add (p3 K B) (K.mul (K.mul B B) A)) (K.mul B (K.mul A A))) (p3 K A))
        (K.mul (p4 K iA) (p4 K iB)) := by
  unfold p4 p3
  -- D·iA⁴iB⁴ = (B⁴ − A⁴)·iA⁴iB⁴ = (B iB)⁴ iA⁴ − (A iA)⁴ iB⁴
  have e1 : K.mul (K.add (K.add (K.add (K.mul (K.mul B B) B) (K.mul (K.mul B B) A)) (K.mul B (K.mul A A))) (K.mul (K.mul A A) A))
        (K.mul (K.mul (K.mul iA iA) (K.mul iA iA)) (K.mul (K.mul iB iB) (K.mul iB iB)))
      = K.add (K.mul (K.mul (K.mul (K.mul B iB) (K.mul B iB)) (K.mul (K.mul B iB) (K.mul B iB))) (K.mul (K.mul iA iA) (K.mul iA iA)))
          (K.neg (K.mul (K.mul (K.mul (K.mul A iA) (K.mul A iA)) (K.mul (K.mul A iA) (K.mul A iA))) (K.mul (K.mul iB iB) (K.mul iB iB)))) := by
    rw [hBA]; lib_ring
  rw [e1, hA, hB]; lib_ring

theorem step_le (A B iA iB C W : R) (hA : K.mul A iA = K.one) (hB : K.mul B iB = K.one) (hBA : B = K.add A K.one)
    (hW : P.nn W)
    (hCW : K.add (K.mul C (K.add (K.add (K.add (p3 K B) (K.mul (K.mul B B) A)) (K.mul B (K.mul A A))) (p3 K A))) W
      = K.mul (K.mul (castN K 108) (p3 K A)) (p3 K B)) :
    Le K P (K.mul C (K.add (p4 K iA) (K.neg (p4 K iB)))) (K.mul (castN K 108) (K.add iA (K.neg iB))) := by
  have hq : P.nn (K.mul (p4 K iA) (p4 K iB)) := by
    unfold p4
    exact P.nn_mul _ _ (P.nn_mul _ _ (P.nn_sq iA) (P.nn_sq iA)) (P.nn_mul _ _ (P.nn_sq iB) (P.nn_sq iB))
  refine ⟨K.mul W (K.mul (p4 K iA) (p4 K iB)), P.nn_mul _ _ hW hq, ?_⟩
  rw [inv4_diff K A B iA iB hA hB hBA]
  generalize hD : K.add (K.add (K.add (p3 K B) (K.mul (K.mul B B) A)) (K.mul B (K.mul A A))) (p3 K A) = D at hCW
  -- C·D·q + W·q = (C·D + W)·q = 108 A³B³ q
  have e2 : K.add (K.mul C (K.mul D (K.mul (p4 K iA) (p4 K iB)))) (K.mul W (K.mul (p4 K iA) (p4 K iB)))
      = K.mul (K.add (K.mul C D) W) (K.mul (p4 K iA) (p4 K iB)) := by lib_ring
  rw [e2, hCW]
  -- 108 A³B³ iA⁴iB⁴ = 108 (A iA)³ (B iB)³ iA iB = 108 iA iB = 108 (B − A) iA iB = 108 (iA (B iB) − iB (A iA))
  unfold p3 p4
  have e3 : K.mul (K.mul (K.mul (castN K 108) (K.mul (K.mul A A) A)) (K.mul (K.mul B B) B))
        (K.mul (K.mul (K.mul iA iA) (K.mul iA iA)) (K.mul (K.mul iB iB) (K.mul iB iB)))
      = K.mul (K.mul (castN K 108) (K.mul (K.mul (K.mul A iA) (K.mul A iA)) (K.mul A iA)))
          (K.mul (K.mul (K.mul (K.mul B iB) (K.mul B iB)) (K.mul B iB)) (K.mul iA iB)) := by lib_ring
  have e4 : K.add iA (K.neg iB) = K.mul (K.add B (K.neg A)) (K.mul iA iB) := by
    have : K.mul (K.add B (K.neg A)) (K.mul iA iB) = K.add (K.mul iA (K.mul B iB)) (K.neg (K.mul iB (K.mul A iA))) := by
      lib_ring
    rw [this, hA, hB]; lib_ring
  rw [e3, e4, hA, hB, hBA]; lib_ring

end Gen

end LatticeStep

#print axioms LatticeStep.inv4_diff
#print axioms LatticeStep.step_le

ns/LatticeStep2.lean

lean_all/ns/LatticeStep2.lean

/-
  ns/LatticeStep2.lean — T37 cycle 1952 (goal G859c2b1, piece a): the per-shell inequality for the weight |k|^{-2}
  (the low-shell weight of the Agmon cut), over a commutative ring with a nonnegative cone. With A·iA = 1, B·iB = 1,
  B = A + 1 and a nonnegative W with C·(A + B) + W = 81·A²B²:
    inv2_diff   iA² − iB² = (A + B)·iA²·iB²
    step2_le    C·(iA² − iB²) ≤ 81
  i.e. a shell of at most (2j+1)³ modes contributes at most 81 to the Abel sum of |k|^{-2}: Σ_{|k|∞ ≤ K} |k|∞^{-2} ≤ 81K.

  Lean 4 core plus ns/LatticeStep, lib/RingTac.
-/
import ns.LatticeStep

namespace LatticeStep2
open DetChain NSChain LibRing

section Gen
variable {R : Type} (K : CRing R) (P : Cone K)

def p2 (x : R) : R := K.mul x x

theorem inv2_diff (A B iA iB : R) (hA : K.mul A iA = K.one) (hB : K.mul B iB = K.one) (hBA : B = K.add A K.one) :
    K.add (p2 K iA) (K.neg (p2 K iB)) = K.mul (K.add A B) (K.mul (p2 K iA) (p2 K iB)) := by
  unfold p2
  have e1 : K.mul (K.add A B) (K.mul (K.mul iA iA) (K.mul iB iB))
      = K.add (K.mul (K.mul (K.mul B iB) (K.mul B iB)) (K.mul iA iA))
          (K.neg (K.mul (K.mul (K.mul A iA) (K.mul A iA)) (K.mul iB iB))) := by
    rw [hBA]; lib_ring
  rw [e1, hA, hB]; lib_ring

theorem step2_le (A B iA iB C W : R) (hA : K.mul A iA = K.one) (hB : K.mul B iB = K.one) (hBA : B = K.add A K.one)
    (hW : P.nn W) (hCW : K.add (K.mul C (K.add A B)) W = K.mul (castN K 81) (K.mul (p2 K A) (p2 K B))) :
    Le K P (K.mul C (K.add (p2 K iA) (K.neg (p2 K iB)))) (castN K 81) := by
  have hq : P.nn (K.mul (p2 K iA) (p2 K iB)) := P.nn_mul _ _ (P.nn_sq iA) (P.nn_sq iB)
  refine ⟨K.mul W (K.mul (p2 K iA) (p2 K iB)), P.nn_mul _ _ hW hq, ?_⟩
  rw [inv2_diff K A B iA iB hA hB hBA]
  have e2 : K.add (K.mul C (K.mul (K.add A B) (K.mul (p2 K iA) (p2 K iB)))) (K.mul W (K.mul (p2 K iA) (p2 K iB)))
      = K.mul (K.add (K.mul C (K.add A B)) W) (K.mul (p2 K iA) (p2 K iB)) := by lib_ring
  rw [e2, hCW]
  unfold p2
  have e3 : K.mul (K.mul (castN K 81) (K.mul (K.mul A A) (K.mul B B))) (K.mul (K.mul iA iA) (K.mul iB iB))
      = K.mul (castN K 81) (K.mul (K.mul (K.mul A iA) (K.mul A iA)) (K.mul (K.mul B iB) (K.mul B iB))) := by lib_ring
  rw [e3, hA, hB]; lib_ring

end Gen

end LatticeStep2

#print axioms LatticeStep2.inv2_diff
#print axioms LatticeStep2.step2_le

ns/LatticeSum.lean

lean_all/ns/LatticeSum.lean

/-
  ns/LatticeSum.lean — T37 cycle 954 (goal G624c, piece 8b): the shell sum of the Abel identity is bounded by 108,
  over an ordered field (inverses of nonzero elements), uniformly in M. With iv j = 1/j, f j = (iv j)⁴, and a list
  L of naturals ≥ 1 whose counting function satisfies cnt(L, j) ≤ (2j+1)³ (ModeCount.cnt_cube):
    shell_term   cnt(L, i+1)·(f(i+1) − f(i+2)) ≤ 108·(iv(i+1) − iv(i+2))
    shell_sum    Σ_{j<M} cnt(L, j)·(f j − f(j+1)) ≤ 108.
  The term j = 0 vanishes (all entries ≥ 1); the others use PolyBound (cast), LatticeStep.step_le and telescoping.

  Lean 4 core plus ns/AbelCount, ns/PolyBound, ns/LatticeStep, ns/BaselQ (inv_nn), ns/GoodModesQ, ns/RatModel.
-/
import ns.AbelCount
import ns.PolyBound
import ns.LatticeStep
import ns.BaselQ

namespace LatticeSum
open DetChain NSChain DyadicSmallGlobal LibRing DyadicLarge AbelCount LatticeStep GoodModesQ

theorem cnt_zero : ∀ (L : List Nat), (∀ n, n ∈ L → 1 ≤ n) → cnt L 0 = 0
  | [], _ => rfl
  | n :: L, h => by
    have hn : 1 ≤ n := h n (List.Mem.head _)
    have hb : Nat.ble n 0 = false := AbelCount.ble_false hn
    show cond (Nat.ble n 0) (cnt L 0 + 1) (cnt L 0) = 0
    rw [hb]; exact cnt_zero L (fun x hx => h x (List.Mem.tail _ hx))

section Gen
variable {R : Type} (K : CRing R) (O : OrdR K)

theorem castN_mul (a : Nat) : ∀ b, castN K (a * b) = K.mul (castN K a) (castN K b)
  | 0 => by show K.zero = K.mul (castN K a) K.zero; lib_ring
  | b + 1 => by
    show castN K (a * b + a) = K.mul (castN K a) (K.add K.one (castN K b))
    rw [RatModel.cast_add, castN_mul a b]; lib_ring

variable (inv : R → R) (mul_inv : ∀ y, y ≠ K.zero → K.mul y (inv y) = K.one) (h10 : K.one ≠ K.zero)

def iv (j : Nat) : R := inv (castN K j)
def f4 (j : Nat) : R := p4 K (iv K inv j)

include O mul_inv h10 in
theorem iv_mul (n : Nat) : K.mul (castN K (n + 1)) (iv K inv (n + 1)) = K.one :=
  mul_inv _ (cast_succ_ne K O h10 n)

include mul_inv h10 in
theorem iv_nn (n : Nat) : O.nn (iv K inv (n + 1)) :=
  BaselQ.inv_nn K O inv _ (RatModel.nn_cast K O.toCone (n + 1)) (iv_mul K O inv mul_inv h10 n)

/-- one shell, with the inverses as variables: c·(iA⁴ − iB⁴) ≤ 108·(iA − iB) for A = a, B = a + 1 -/
theorem shell_core (a : Nat) (ha : 1 ≤ a) (c : Nat) (hc : c ≤ (a + (a + 1)) * ((a + (a + 1)) * (a + (a + 1))))
    (iA iB : R) (hA : K.mul (castN K a) iA = K.one) (hB : K.mul (castN K (a + 1)) iB = K.one) :
    Le K O.toCone (K.mul (castN K c) (K.add (p4 K iA) (K.neg (p4 K iB))))
      (K.mul (castN K 108) (K.add iA (K.neg iB))) := by
  have pb := PolyBound.poly_bound a ha
  have ⟨w, hw⟩ := DyadicModelZ.le_dest pb
  have hcast := congrArg (castN K) hw
  -- explicit casts
  have cB : castN K (a + 1) = K.add (castN K a) K.one := by show K.add K.one (castN K a) = _; lib_ring
  have cS : castN K (a + (a + 1)) = K.add (castN K a) (K.add (castN K a) K.one) := by
    rw [RatModel.cast_add K a (a + 1), cB]
  have c3 : ∀ x, castN K (x * x * x) = K.mul (K.mul (castN K x) (castN K x)) (castN K x) := fun x => by
    rw [castN_mul K (x * x) x, castN_mul K x x]
  have cD : castN K ((a + 1) * (a + 1) * (a + 1) + (a + 1) * (a + 1) * a + (a + 1) * a * a + a * a * a)
      = K.add (K.add (K.add (K.mul (K.mul (castN K (a + 1)) (castN K (a + 1))) (castN K (a + 1)))
          (K.mul (K.mul (castN K (a + 1)) (castN K (a + 1))) (castN K a)))
          (K.mul (K.mul (castN K (a + 1)) (castN K a)) (castN K a))) (K.mul (K.mul (castN K a) (castN K a)) (castN K a)) := by
    rw [RatModel.cast_add K ((a + 1) * (a + 1) * (a + 1) + (a + 1) * (a + 1) * a + (a + 1) * a * a) (a * a * a),
      RatModel.cast_add K ((a + 1) * (a + 1) * (a + 1) + (a + 1) * (a + 1) * a) ((a + 1) * a * a),
      RatModel.cast_add K ((a + 1) * (a + 1) * (a + 1)) ((a + 1) * (a + 1) * a),
      c3 (a + 1), c3 a, castN_mul K ((a + 1) * (a + 1)) a, castN_mul K (a + 1) (a + 1),
      castN_mul K ((a + 1) * a) a, castN_mul K (a + 1) a]
  have cL : castN K ((a + (a + 1)) * (a + (a + 1)) * (a + (a + 1)) *
        ((a + 1) * (a + 1) * (a + 1) + (a + 1) * (a + 1) * a + (a + 1) * a * a + a * a * a) + w)
      = K.add (K.mul (castN K ((a + (a + 1)) * (a + (a + 1)) * (a + (a + 1))))
          (castN K ((a + 1) * (a + 1) * (a + 1) + (a + 1) * (a + 1) * a + (a + 1) * a * a + a * a * a))) (castN K w) := by
    rw [RatModel.cast_add, castN_mul]
  have cR : castN K (108 * (a * a * a) * ((a + 1) * (a + 1) * (a + 1)))
      = K.mul (K.mul (castN K 108) (castN K (a * a * a))) (castN K ((a + 1) * (a + 1) * (a + 1))) := by
    rw [castN_mul K (108 * (a * a * a)), castN_mul K 108]
  rw [cL, cR, cD, c3 (a + (a + 1)), c3 a, c3 (a + 1), cS, cB] at hcast
  have cC : castN K ((a + (a + 1)) * ((a + (a + 1)) * (a + (a + 1))))
      = K.mul (K.mul (K.add (castN K a) (K.add (castN K a) K.one)) (K.add (castN K a) (K.add (castN K a) K.one)))
          (K.add (castN K a) (K.add (castN K a) K.one)) := by
    rw [castN_mul K (a + (a + 1)), castN_mul K (a + (a + 1)), cS]; lib_ring
  rw [cB] at hB
  have An0 : O.nn (castN K a) := RatModel.nn_cast K O.toCone a
  have m0 := RatModel.cast_mono K O.toCone _ _ hc
  rw [cC] at m0
  generalize castN K a = A at hA hB hcast m0 An0
  have Bn : O.nn (K.add A K.one) := O.nn_add _ _ An0 (RatModel.nn_one' K O.toCone)
  have hD := inv4_diff K A (K.add A K.one) iA iB hA hB rfl
  have Dn : O.nn (K.add (K.add (K.add (p3 K (K.add A K.one)) (K.mul (K.mul (K.add A K.one) (K.add A K.one)) A))
      (K.mul (K.add A K.one) (K.mul A A))) (p3 K A)) := by
    unfold p3
    exact O.nn_add _ _ (O.nn_add _ _ (O.nn_add _ _ (O.nn_mul _ _ (O.nn_sq _) Bn) (O.nn_mul _ _ (O.nn_sq _) An0))
      (O.nn_mul _ _ Bn (O.nn_sq _))) (O.nn_mul _ _ (O.nn_sq _) An0)
  have gn : O.nn (K.add (p4 K iA) (K.neg (p4 K iB))) := by
    rw [hD]; unfold p4
    exact O.nn_mul _ _ Dn (O.nn_mul _ _ (O.nn_mul _ _ (O.nn_sq _) (O.nn_sq _)) (O.nn_mul _ _ (O.nn_sq _) (O.nn_sq _)))
  have h1 := le_mul_nn K O.toCone _ _ _ gn m0
  rw [K.mul_comm _ (castN K c), K.mul_comm (K.add (p4 K iA) (K.neg (p4 K iB)))] at h1
  have h2 := step_le K O.toCone A (K.add A K.one) iA iB
    (K.mul (K.mul (K.add A (K.add A K.one)) (K.add A (K.add A K.one))) (K.add A (K.add A K.one))) (castN K w) hA hB rfl (RatModel.nn_cast K O.toCone w) (by
    unfold p3; rw [← hcast]; lib_ring)
  exact le_trans' K O.toCone _ _ _ h1 h2

include mul_inv h10 in
theorem shell_term (L : List Nat) (i : Nat)
    (hc : cnt L (i + 1) ≤ (2 * (i + 1) + 1) * ((2 * (i + 1) + 1) * (2 * (i + 1) + 1))) :
    Le K O.toCone (K.mul (castN K (cnt L (i + 1))) (gd K (f4 K inv) (i + 1)))
      (K.mul (castN K 108) (K.add (iv K inv (i + 1)) (K.neg (iv K inv (i + 1 + 1))))) := by
  have e2 : 2 * (i + 1) + 1 = (i + 1) + ((i + 1) + 1) := by lib_nat
  rw [e2] at hc
  exact shell_core K O (i + 1) (Nat.succ_le_succ (Nat.zero_le i)) _ hc _ _
    (iv_mul K O inv mul_inv h10 i) (iv_mul K O inv mul_inv h10 (i + 1))

def u (j : Nat) : R := match j with
  | 0 => iv K inv 1
  | j + 1 => iv K inv (j + 1)

include mul_inv h10 in
theorem u_nn : ∀ j, O.nn (u K inv j)
  | 0 => iv_nn K O inv mul_inv h10 0
  | j + 1 => iv_nn K O inv mul_inv h10 j

include O mul_inv h10 in
theorem iv_one : iv K inv 1 = K.one := by
  have h := iv_mul K O inv mul_inv h10 0
  have e : castN K (0 + 1) = K.one := by show K.add K.one K.zero = K.one; lib_ring
  rw [e] at h
  calc iv K inv 1 = K.mul K.one (iv K inv 1) := by lib_ring
    _ = K.one := h

include mul_inv h10 in
theorem shell_sum (L : List Nat) (h1 : ∀ n, n ∈ L → 1 ≤ n)
    (hc : ∀ j, cnt L j ≤ (2 * j + 1) * ((2 * j + 1) * (2 * j + 1))) (M : Nat) :
    Le K O.toCone (sumN K (fun j => K.mul (castN K (cnt L j)) (gd K (f4 K inv) j)) M) (castN K 108) := by
  have term : ∀ j, Le K O.toCone (K.mul (castN K (cnt L j)) (gd K (f4 K inv) j))
      (K.mul (castN K 108) (K.add (u K inv j) (K.neg (u K inv (j + 1))))) := by
    intro j
    cases j with
    | zero =>
      rw [cnt_zero L h1]
      refine ⟨K.zero, O.nn_zero, ?_⟩
      show K.add (K.mul K.zero (gd K (f4 K inv) 0)) K.zero = K.mul (castN K 108) (K.add (iv K inv 1) (K.neg (iv K inv 1)))
      lib_ring
    | succ i => exact shell_term K O inv mul_inv h10 L i (hc (i + 1))
  have hs := sum_le K O.toCone _ _ term M
  rw [sumN_mul_left, telescope] at hs
  refine le_trans' K O.toCone _ _ _ hs ⟨K.mul (castN K 108) (u K inv M), O.nn_mul _ _ (RatModel.nn_cast K O.toCone 108)
    (u_nn K O inv mul_inv h10 M), ?_⟩
  show K.add (K.mul (castN K 108) (K.add (iv K inv 1) (K.neg (u K inv M)))) (K.mul (castN K 108) (u K inv M)) = castN K 108
  rw [iv_one K O inv mul_inv h10]; lib_ring

end Gen

end LatticeSum

#print axioms LatticeSum.cnt_zero
#print axioms LatticeSum.castN_mul
#print axioms LatticeSum.shell_term
#print axioms LatticeSum.shell_sum

ns/LatticeSum2.lean

lean_all/ns/LatticeSum2.lean

/-
  ns/LatticeSum2.lean — T37 cycle 1954 (goal G859c2b1, piece b): the Abel shell sum for the weight |k|^{-2}, over an
  ordered field. With f2 j = (1/j)² and a list L of naturals ≥ 1 with cnt(L, j) ≤ (2j+1)³ (ModeCount.cnt_cube):
    poly2        c ≤ (2a+1)³, a ≥ 1  ⇒  c·(2a+1) ≤ 81·a²(a+1)²
    shell2_term  cnt(L, i+1)·(f2(i+1) − f2(i+2)) ≤ 81
    shell2_sum   Σ_{j<M} cnt(L, j)·(f2 j − f2(j+1)) ≤ M·81
  (no telescoping: the low Agmon weight Σ_{|k|∞ ≤ K}|k|∞^{-2} grows linearly in the cut K).

  Lean 4 core plus ns/LatticeSum, ns/LatticeStep2.
-/
import ns.LatticeSum
import ns.LatticeStep2

namespace LatticeSum2
open DetChain NSChain DyadicSmallGlobal LibRing DyadicLarge AbelCount LatticeStep LatticeSum LatticeStep2

theorem poly2 (a c : Nat) (ha : 1 ≤ a) (hc : c ≤ (a + (a + 1)) * ((a + (a + 1)) * (a + (a + 1)))) :
    c * (a + (a + 1)) ≤ 81 * (a * a) * ((a + 1) * (a + 1)) := by
  have hs : a + (a + 1) ≤ 3 * a := by
    rw [show 3 * a = a + (a + a) by lib_nat]
    exact Nat.add_le_add_left (Nat.add_le_add_left ha a) a
  have h1 : c * (a + (a + 1)) ≤ (a + (a + 1)) * ((a + (a + 1)) * (a + (a + 1))) * (a + (a + 1)) :=
    Nat.mul_le_mul_right _ hc
  have h2 : (a + (a + 1)) * ((a + (a + 1)) * (a + (a + 1))) * (a + (a + 1)) ≤ (3 * a) * ((3 * a) * (3 * a)) * (3 * a) :=
    Nat.mul_le_mul (Nat.mul_le_mul hs (Nat.mul_le_mul hs hs)) hs
  have h3 : (3 * a) * ((3 * a) * (3 * a)) * (3 * a) ≤ 81 * (a * a) * ((a + 1) * (a + 1)) := by
    rw [show (3 * a) * ((3 * a) * (3 * a)) * (3 * a) = 81 * (a * a) * (a * a) by lib_nat]
    have ha1 : a ≤ a + 1 := Nat.le_succ a
    exact Nat.mul_le_mul_left _ (Nat.mul_le_mul ha1 ha1)
  exact Nat.le_trans h1 (Nat.le_trans h2 h3)

section Gen
variable {R : Type} (K : CRing R) (O : OrdR K)
variable (inv : R → R) (mul_inv : ∀ y, y ≠ K.zero → K.mul y (inv y) = K.one) (h10 : K.one ≠ K.zero)

def f2 (j : Nat) : R := p2 K (iv K inv j)

/-- one shell: c·(iA² − iB²) ≤ 81 for A = a, B = a + 1 -/
theorem shell2_core (a : Nat) (ha : 1 ≤ a) (c : Nat) (hc : c ≤ (a + (a + 1)) * ((a + (a + 1)) * (a + (a + 1))))
    (iA iB : R) (hA : K.mul (castN K a) iA = K.one) (hB : K.mul (castN K (a + 1)) iB = K.one) :
    Le K O.toCone (K.mul (castN K c) (K.add (p2 K iA) (K.neg (p2 K iB)))) (castN K 81) := by
  have pb := poly2 a c ha hc
  have ⟨w, hw⟩ := DyadicModelZ.le_dest pb
  have hcast := congrArg (castN K) hw
  have cB : castN K (a + 1) = K.add (castN K a) K.one := by show K.add K.one (castN K a) = _; lib_ring
  have cS : castN K (a + (a + 1)) = K.add (castN K a) (K.add (castN K a) K.one) := by
    rw [RatModel.cast_add K a (a + 1), cB]
  rw [RatModel.cast_add, castN_mul K c, castN_mul K (81 * (a * a)), castN_mul K 81, castN_mul K a a,
    castN_mul K (a + 1) (a + 1), cS, cB] at hcast
  rw [cB] at hB
  generalize castN K a = A at hA hB hcast
  exact step2_le K O.toCone A (K.add A K.one) iA iB (castN K c) (castN K w) hA hB rfl (RatModel.nn_cast K O.toCone w)
    (by unfold p2; rw [hcast]; lib_ring)

include mul_inv h10 in
theorem shell2_term (L : List Nat) (i : Nat)
    (hc : cnt L (i + 1) ≤ (2 * (i + 1) + 1) * ((2 * (i + 1) + 1) * (2 * (i + 1) + 1))) :
    Le K O.toCone (K.mul (castN K (cnt L (i + 1))) (gd K (f2 K inv) (i + 1))) (castN K 81) := by
  have e2 : 2 * (i + 1) + 1 = (i + 1) + ((i + 1) + 1) := by lib_nat
  rw [e2] at hc
  exact shell2_core K O (i + 1) (Nat.succ_le_succ (Nat.zero_le i)) _ hc _ _
    (iv_mul K O inv mul_inv h10 i) (iv_mul K O inv mul_inv h10 (i + 1))

theorem sum_const_le (f : Nat → R) (C : R) (h : ∀ j, Le K O.toCone (f j) C) : ∀ n,
    Le K O.toCone (sumN K f n) (K.mul (castN K n) C)
  | 0 => ⟨K.zero, O.nn_zero, by show K.add K.zero K.zero = K.mul K.zero C; lib_ring⟩
  | n + 1 => by
    have ih := sum_const_le f C h n
    have s := le_add' K O.toCone _ _ _ _ ih (h n)
    refine le_trans' K O.toCone _ _ _ s ⟨K.zero, O.nn_zero, ?_⟩
    show K.add (K.add (K.mul (castN K n) C) C) K.zero = K.mul (K.add K.one (castN K n)) C
    lib_ring

include mul_inv h10 in
theorem shell2_sum (L : List Nat) (h1 : ∀ n, n ∈ L → 1 ≤ n)
    (hc : ∀ j, cnt L j ≤ (2 * j + 1) * ((2 * j + 1) * (2 * j + 1))) (M : Nat) :
    Le K O.toCone (sumN K (fun j => K.mul (castN K (cnt L j)) (gd K (f2 K inv) j)) M)
      (K.mul (castN K M) (castN K 81)) := by
  apply sum_const_le K O
  intro j
  cases j with
  | zero =>
    rw [cnt_zero L h1]
    refine ⟨castN K 81, RatModel.nn_cast K O.toCone 81, ?_⟩
    show K.add (K.mul K.zero (gd K (f2 K inv) 0)) (castN K 81) = castN K 81
    lib_ring
  | succ i => exact shell2_term K O inv mul_inv h10 L i (hc (i + 1))

end Gen

end LatticeSum2

#print axioms LatticeSum2.poly2
#print axioms LatticeSum2.shell2_core
#print axioms LatticeSum2.shell2_term
#print axioms LatticeSum2.sum_const_le
#print axioms LatticeSum2.shell2_sum

ns/LatticeTail.lean

lean_all/ns/LatticeTail.lean

/-
  ns/LatticeTail.lean — T37 cycle 987 (goal G648, piece a): the tail of the lattice sum. Over an ordered field, for a
  duplicate-free list l of modes k ∈ ℤ³ with R + 1 ≤ |k|_∞ ≤ M + 1:
    lattice_tail   Σ_{k∈l} |k|_∞^{−4} ≤ 135 / (R + 1),
  uniformly in M. Same Abel route as LatticeBound: the shells j ≤ R are empty, the shells j > R telescope to
  108·(R+1)^{−1}, and the last Abel term is ≤ 27·(M+1)^{−1} ≤ 27·(R+1)^{−1}. This is the high-frequency half of the
  cutoff argument for the large-data enstrophy step (‖u‖_∞ split at |k|_∞ = R).

  Lean 4 core plus ns/LatticeBound, ns/LatticeSum, ns/AbelCount, ns/EnstGlobal (le_of_mul_le).
-/
import ns.LatticeBound
import ns.EnstGlobal

namespace LatticeTail
open DetChain NSChain DyadicSmallGlobal LibRing DyadicLarge AbelCount LatticeStep LatticeSum ModeCount CubeCount
  LatticeBound GoodModesQ

theorem cnt_zero_below (R : Nat) : ∀ (L : List Nat), (∀ n, n ∈ L → R + 1 ≤ n) → ∀ j, j ≤ R → cnt L j = 0
  | [], _, _, _ => rfl
  | n :: L, h, j, hj => by
    have hn : j < n := Nat.lt_of_le_of_lt hj (h n (List.Mem.head _))
    show cond (Nat.ble n j) (cnt L j + 1) (cnt L j) = 0
    rw [AbelCount.ble_false hn]
    exact cnt_zero_below R L (fun x hx => h x (List.Mem.tail _ hx)) j hj

section Gen
variable {R : Type} (K : CRing R) (O : OrdR K)
variable (inv : R → R) (mul_inv : ∀ y, y ≠ K.zero → K.mul y (inv y) = K.one) (h10 : K.one ≠ K.zero)

include mul_inv h10 in
/-- 1/(n+1) is antitone -/
theorem iv_anti (a b : Nat) (h : a ≤ b) : Le K O.toCone (iv K inv (b + 1)) (iv K inv (a + 1)) := by
  have hA := iv_mul K O inv mul_inv h10 a
  have hB := iv_mul K O inv mul_inv h10 b
  have nA := iv_nn K O inv mul_inv h10 a
  have nB := iv_nn K O inv mul_inv h10 b
  have m := RatModel.cast_mono K O.toCone _ _ (Nat.succ_le_succ h)
  -- (a+1)·iB ≤ (b+1)·iB = 1 = (a+1)·iA
  have m1 := le_mul_nn K O.toCone (iv K inv (b + 1)) _ _ nB m
  rw [K.mul_comm _ (castN K (b + 1)), hB, ← hA, K.mul_comm (iv K inv (b + 1))] at m1
  exact EnstGlobal.le_of_mul_le K O _ _ _ (RatModel.nn_cast K O.toCone (a + 1)) (cast_succ_ne K O h10 a) m1

/-- u_R j = 1/(R+1) for j ≤ R + 1, 1/j beyond -/
def uR (Rc : Nat) (j : Nat) : R := cond (Nat.ble j (Rc + 1)) (iv K inv (Rc + 1)) (iv K inv j)

include mul_inv h10 in
theorem tail_shell (Rc : Nat) (L : List Nat) (h1 : ∀ n, n ∈ L → Rc + 1 ≤ n)
    (hc : ∀ j, cnt L j ≤ (2 * j + 1) * ((2 * j + 1) * (2 * j + 1))) (M : Nat) :
    Le K O.toCone (sumN K (fun j => K.mul (castN K (cnt L j)) (gd K (f4 K inv) j)) M)
      (K.mul (castN K 108) (iv K inv (Rc + 1))) := by
  have term : ∀ j, Le K O.toCone (K.mul (castN K (cnt L j)) (gd K (f4 K inv) j))
      (K.mul (castN K 108) (K.add (uR K inv Rc j) (K.neg (uR K inv Rc (j + 1))))) := by
    intro j
    cases Nat.lt_or_ge j (Rc + 1) with
    | inl hj =>
      have hj' : j ≤ Rc := Nat.le_of_lt_succ hj
      rw [cnt_zero_below Rc L h1 j hj']
      have e1 : uR K inv Rc j = iv K inv (Rc + 1) := by
        show cond (Nat.ble j (Rc + 1)) _ _ = _; rw [Nat.ble_eq_true_of_le (Nat.le_of_lt hj)]; rfl
      have e2 : uR K inv Rc (j + 1) = iv K inv (Rc + 1) := by
        show cond (Nat.ble (j + 1) (Rc + 1)) _ _ = _; rw [Nat.ble_eq_true_of_le hj]; rfl
      rw [e1, e2]
      exact ⟨K.zero, O.nn_zero, by show K.add (K.mul K.zero (gd K (f4 K inv) j)) K.zero = _; lib_ring⟩
    | inr hj =>
      -- j = i + 1 with i ≥ Rc
      cases j with
      | zero => exact absurd hj (Nat.not_succ_le_zero _)
      | succ i =>
        have e1 : uR K inv Rc (i + 1) = iv K inv (i + 1) := by
          show cond (Nat.ble (i + 1) (Rc + 1)) _ _ = _
          cases Nat.eq_or_lt_of_le hj with
          | inl e => rw [← e, Nat.ble_eq_true_of_le (Nat.le_refl _)]; rfl
          | inr l => rw [AbelCount.ble_false l]; rfl
        have e2 : uR K inv Rc (i + 1 + 1) = iv K inv (i + 1 + 1) := by
          show cond (Nat.ble (i + 1 + 1) (Rc + 1)) _ _ = _
          rw [AbelCount.ble_false (Nat.lt_succ_of_le hj)]; rfl
        rw [e1, e2]
        exact shell_term K O inv mul_inv h10 L i (hc (i + 1))
  have hs := sum_le K O.toCone _ _ term M
  rw [sumN_mul_left, telescope] at hs
  have u0 : uR K inv Rc 0 = iv K inv (Rc + 1) := by
    show cond (Nat.ble 0 (Rc + 1)) _ _ = _; rw [Nat.ble_eq_true_of_le (Nat.zero_le _)]; rfl
  have uMn : O.nn (uR K inv Rc M) := by
    cases hb : Nat.ble M (Rc + 1) with
    | true => show O.nn (cond (Nat.ble M (Rc + 1)) _ _); rw [hb]; exact iv_nn K O inv mul_inv h10 Rc
    | false =>
      cases M with
      | zero => exact absurd (Nat.ble_eq_true_of_le (Nat.zero_le (Rc + 1))) (by rw [hb]; exact Bool.noConfusion)
      | succ M' => show O.nn (cond (Nat.ble (M' + 1) (Rc + 1)) _ _); rw [hb]; exact iv_nn K O inv mul_inv h10 M'
  rw [u0] at hs
  refine le_trans' K O.toCone _ _ _ hs ⟨K.mul (castN K 108) (uR K inv Rc M), O.nn_mul _ _ (RatModel.nn_cast K O.toCone 108) uMn, ?_⟩
  lib_ring

include mul_inv h10 in
theorem last_term_iv (M c : Nat) (hc : c ≤ (2 * (M + 1) + 1) * ((2 * (M + 1) + 1) * (2 * (M + 1) + 1))) :
    Le K O.toCone (K.mul (castN K c) (f4 K inv (M + 1))) (K.mul (castN K 27) (iv K inv (M + 1))) := by
  have hc27 := Nat.le_trans hc (cube27 (M + 1) (Nat.succ_le_succ (Nat.zero_le M)))
  have m0 := RatModel.cast_mono K O.toCone _ _ hc27
  have hA := iv_mul K O inv mul_inv h10 M
  have cR : castN K (27 * ((M + 1) * (M + 1) * (M + 1)))
      = K.mul (castN K 27) (K.mul (K.mul (castN K (M + 1)) (castN K (M + 1))) (castN K (M + 1))) := by
    rw [castN_mul K 27, castN_mul K ((M + 1) * (M + 1)), castN_mul K (M + 1) (M + 1)]
  rw [cR] at m0
  have f4e : f4 K inv (M + 1) = p4 K (iv K inv (M + 1)) := rfl
  rw [f4e]
  generalize iv K inv (M + 1) = iA at hA ⊢
  generalize castN K (M + 1) = A at hA m0
  have p4n : O.nn (p4 K iA) := by unfold p4; exact O.nn_mul _ _ (O.nn_sq _) (O.nn_sq _)
  have s1 := le_mul_nn K O.toCone _ _ _ p4n m0
  rw [K.mul_comm (p4 K iA), K.mul_comm (p4 K iA)] at s1
  have e : K.mul (K.mul (castN K 27) (K.mul (K.mul A A) A)) (p4 K iA) = K.mul (castN K 27) iA := by
    have : K.mul (K.mul (castN K 27) (K.mul (K.mul A A) A)) (p4 K iA)
        = K.mul (K.mul (castN K 27) (K.mul (K.mul (K.mul A iA) (K.mul A iA)) (K.mul A iA))) iA := by unfold p4; lib_ring
    rw [this, hA]; lib_ring
  rw [e] at s1
  exact s1

include mul_inv h10 in
/-- Σ_{k∈l} |k|_∞^{−4} ≤ 135/(R+1) when every mode has R + 1 ≤ |k|_∞ -/
theorem lattice_tail (Rc : Nat) (l : List Mode) (hd : List.Pairwise (fun a b => a ≠ b) l)
    (h1 : ∀ k, k ∈ l → Rc + 1 ≤ nrm k) (M : Nat) (hM : ∀ k, k ∈ l → nrm k ≤ M + 1) (hRM : Rc ≤ M) :
    Le K O.toCone (lsumN K (f4 K inv) (norms l)) (K.mul (castN K 135) (iv K inv (Rc + 1))) := by
  have hb : ∀ n, n ∈ norms l → n ≤ M + 1 := fun n hn => by
    have ⟨k, hk, e⟩ := mem_norms n l hn
    rw [e]; exact hM k hk
  have h1' : ∀ n, n ∈ norms l → Rc + 1 ≤ n := fun n hn => by
    have ⟨k, hk, e⟩ := mem_norms n l hn
    rw [e]; exact h1 k hk
  rw [abel_list K (f4 K inv) (M + 1) (norms l) hb]
  have S := tail_shell K O inv mul_inv h10 Rc (norms l) h1' (fun j => cnt_cube l hd j) (M + 1)
  have hlen : (norms l).length = cnt (norms l) (M + 1) := (cnt_all (M + 1) (norms l) hb).symm
  have T := last_term_iv K O inv mul_inv h10 M (norms l).length (by rw [hlen]; exact cnt_cube l hd (M + 1))
  have T2 := le_trans' K O.toCone _ _ _ T
    (le_mul_nn K O.toCone (castN K 27) _ _ (RatModel.nn_cast K O.toCone 27) (iv_anti K O inv mul_inv h10 Rc M hRM))
  have U := le_add' K O.toCone _ _ _ _ S T2
  have e : K.add (K.mul (castN K 108) (iv K inv (Rc + 1))) (K.mul (castN K 27) (iv K inv (Rc + 1)))
      = K.mul (castN K 135) (iv K inv (Rc + 1)) := by
    rw [show castN K 135 = K.add (castN K 108) (castN K 27) from RatModel.cast_add K 108 27]; lib_ring
  rw [e] at U; exact U

end Gen

end LatticeTail

#print axioms LatticeTail.cnt_zero_below
#print axioms LatticeTail.iv_anti
#print axioms LatticeTail.tail_shell
#print axioms LatticeTail.last_term_iv
#print axioms LatticeTail.lattice_tail

ns/LatticeTail4.lean

lean_all/ns/LatticeTail4.lean

/-
  ns/LatticeTail4.lean — T37 cycle 1956 (goal G859c2b1, piece d): the high Agmon weight. Over an ordered field, for
  every duplicate-free list l of modes with K + 1 ≤ |k|_∞ ≤ M + 1 (K ≤ M):
    lattice_tail4   Σ_{k∈l} |k|_∞^{−4} ≤ 108·(K+1)^{−1}
  Abel identity; the shells j ≤ K are empty (cnt_below), the shells j > K telescope against v j = (max(j, K+1))^{−1}
  (LatticeSum.shell_term), and the last term is |l|·(M+1)^{−4} ≤ 27·(M+1)^{−1} (last_term').

  Lean 4 core plus ns/LatticeBound.
-/
import ns.LatticeBound

namespace LatticeTail4
open DetChain NSChain DyadicSmallGlobal LibRing DyadicLarge AbelCount LatticeStep LatticeSum ModeCount CubeCount
  LatticeBound

theorem ble_of_le : ∀ a b : Nat, a ≤ b → Nat.ble a b = true
  | 0, _, _ => rfl
  | _ + 1, 0, h => absurd h (Nat.not_succ_le_zero _)
  | a + 1, b + 1, h => ble_of_le a b (Nat.le_of_succ_le_succ h)

theorem cnt_below (j : Nat) : ∀ (L : List Nat), (∀ n, n ∈ L → j < n) → cnt L j = 0
  | [], _ => rfl
  | n :: L, h => by
    have hb : Nat.ble n j = false := AbelCount.ble_false (h n (List.Mem.head _))
    show cond (Nat.ble n j) (cnt L j + 1) (cnt L j) = 0
    rw [hb]; exact cnt_below j L (fun x hx => h x (List.Mem.tail _ hx))

section Gen
variable {R : Type} (K : CRing R) (O : OrdR K)
variable (inv : R → R) (mul_inv : ∀ y, y ≠ K.zero → K.mul y (inv y) = K.one) (h10 : K.one ≠ K.zero)

def v (Kc : Nat) (j : Nat) : R := cond (Nat.ble j Kc) (iv K inv (Kc + 1)) (iv K inv j)

theorem v_low (Kc j : Nat) (h : j ≤ Kc) : v K inv Kc j = iv K inv (Kc + 1) := by
  unfold v; rw [ble_of_le j Kc h]; rfl

theorem v_high (Kc j : Nat) (h : Kc < j) : v K inv Kc j = iv K inv j := by
  unfold v; rw [AbelCount.ble_false h]; rfl

include mul_inv h10 in
theorem last_term' (M c : Nat) (hc : c ≤ (2 * (M + 1) + 1) * ((2 * (M + 1) + 1) * (2 * (M + 1) + 1))) :
    Le K O.toCone (K.mul (castN K c) (f4 K inv (M + 1))) (K.mul (castN K 27) (iv K inv (M + 1))) := by
  have hc27 := Nat.le_trans hc (cube27 (M + 1) (Nat.succ_le_succ (Nat.zero_le M)))
  have m0 := RatModel.cast_mono K O.toCone _ _ hc27
  have hA := iv_mul K O inv mul_inv h10 M
  have cR : castN K (27 * ((M + 1) * (M + 1) * (M + 1)))
      = K.mul (castN K 27) (K.mul (K.mul (castN K (M + 1)) (castN K (M + 1))) (castN K (M + 1))) := by
    rw [castN_mul K 27, castN_mul K ((M + 1) * (M + 1)), castN_mul K (M + 1) (M + 1)]
  rw [cR] at m0
  have f4e : f4 K inv (M + 1) = p4 K (iv K inv (M + 1)) := rfl
  rw [f4e]
  generalize iv K inv (M + 1) = iA at hA ⊢
  generalize castN K (M + 1) = A at hA m0 ⊢
  have p4n : O.nn (p4 K iA) := by unfold p4; exact O.nn_mul _ _ (O.nn_sq _) (O.nn_sq _)
  have s1 := le_mul_nn K O.toCone _ _ _ p4n m0
  rw [K.mul_comm (p4 K iA), K.mul_comm (p4 K iA)] at s1
  have e : K.mul (K.mul (castN K 27) (K.mul (K.mul A A) A)) (p4 K iA) = K.mul (castN K 27) iA := by
    have : K.mul (K.mul (castN K 27) (K.mul (K.mul A A) A)) (p4 K iA)
        = K.mul (K.mul (castN K 27) (K.mul (K.mul (K.mul A iA) (K.mul A iA)) (K.mul A iA))) iA := by unfold p4; lib_ring
    rw [this, hA]; lib_ring
  rw [e] at s1
  exact s1

include mul_inv h10 in
theorem lattice_tail4 (l : List Mode) (hd : List.Pairwise (fun a b => a ≠ b) l) (Kc M : Nat) (hKM : Kc ≤ M)
    (h1 : ∀ k, k ∈ l → Kc + 1 ≤ nrm k) (hM : ∀ k, k ∈ l → nrm k ≤ M + 1) :
    Le K O.toCone (lsumN K (f4 K inv) (norms l)) (K.mul (castN K 108) (iv K inv (Kc + 1))) := by
  have hb : ∀ n, n ∈ norms l → n ≤ M + 1 := fun n hn => by
    have ⟨k, hk, e⟩ := mem_norms n l hn
    rw [e]; exact hM k hk
  have h1' : ∀ n, n ∈ norms l → Kc + 1 ≤ n := fun n hn => by
    have ⟨k, hk, e⟩ := mem_norms n l hn
    rw [e]; exact h1 k hk
  rw [abel_list K (f4 K inv) (M + 1) (norms l) hb]
  have term : ∀ j, Le K O.toCone (K.mul (castN K (cnt (norms l) j)) (gd K (f4 K inv) j))
      (K.mul (castN K 108) (K.add (v K inv Kc j) (K.neg (v K inv Kc (j + 1))))) := by
    intro j
    by_cases hj : j ≤ Kc
    · rw [cnt_below j (norms l) (fun n hn => Nat.lt_of_lt_of_le (Nat.lt_succ_of_le hj) (h1' n hn)),
        v_low K inv Kc j hj]
      have e : v K inv Kc (j + 1) = iv K inv (Kc + 1) := by
        by_cases hj' : j + 1 ≤ Kc
        · exact v_low K inv Kc (j + 1) hj'
        · have e1 : j + 1 = Kc + 1 := Nat.le_antisymm (Nat.succ_le_succ hj) (Nat.gt_of_not_le hj')
          rw [e1]; exact v_high K inv Kc (Kc + 1) (Nat.lt_succ_self Kc)
      rw [e]
      refine ⟨K.zero, O.nn_zero, ?_⟩
      show K.add (K.mul K.zero (gd K (f4 K inv) j)) K.zero
        = K.mul (castN K 108) (K.add (iv K inv (Kc + 1)) (K.neg (iv K inv (Kc + 1))))
      lib_ring
    · have hlt : Kc < j := Nat.gt_of_not_le hj
      cases j with
      | zero => exact absurd hlt (Nat.not_lt_zero Kc)
      | succ i =>
        rw [v_high K inv Kc (i + 1) hlt, v_high K inv Kc (i + 1 + 1) (Nat.lt_succ_of_lt hlt)]
        exact shell_term K O inv mul_inv h10 (norms l) i (cnt_cube l hd (i + 1))
  have hs := sum_le K O.toCone _ _ term (M + 1)
  rw [sumN_mul_left, telescope] at hs
  have hlen : (norms l).length = cnt (norms l) (M + 1) := (cnt_all (M + 1) (norms l) hb).symm
  have T := last_term' K O inv mul_inv h10 M (norms l).length (by rw [hlen]; exact cnt_cube l hd (M + 1))
  have U := le_add' K O.toCone _ _ _ _ hs T
  rw [v_low K inv Kc 0 (Nat.zero_le Kc), v_high K inv Kc (M + 1) (Nat.lt_succ_of_le hKM)] at U
  refine le_trans' K O.toCone _ _ _ U ⟨K.mul (castN K 81) (iv K inv (M + 1)),
    O.nn_mul _ _ (RatModel.nn_cast K O.toCone 81) (iv_nn K O inv mul_inv h10 M), ?_⟩
  rw [show castN K 108 = K.add (castN K 81) (castN K 27) from RatModel.cast_add K 81 27]
  lib_ring

end Gen

end LatticeTail4

#print axioms LatticeTail4.ble_of_le
#print axioms LatticeTail4.cnt_below
#print axioms LatticeTail4.v_low
#print axioms LatticeTail4.v_high
#print axioms LatticeTail4.last_term'
#print axioms LatticeTail4.lattice_tail4

ns/Leray.lean

lean_all/ns/Leray.lean

/-
  ns/Leray.lean — T37 cycle 105: the Leray projector in Fourier space.

  Lean 4 core plus ns/Commutator (d3) and lib/RingTac. For a mode k ≠ 0 with κ·|k|² = 1 (κ = 1/|k|²),
      P u = u − κ (k·u) k
    div_free     k · P u = 0
    idem         P (P u) = P u                 (OneMove: reading then placing is idempotent)
    fixes        k · u = 0  ⇒  P u = u          (placing a divergence-free field then reading is the identity)
  Every Galerkin truncation applies P mode by mode, so it keeps fields divergence-free.
-/
import ns.Commutator

namespace Leray
open DetChain LibRing Commutator

section Ring
variable {R : Type} (K : CRing R)

def P1 (κ k1 k2 k3 u1 u2 u3 : R) : R := sub K u1 (K.mul (K.mul κ (d3 K k1 k2 k3 u1 u2 u3)) k1)
def P2 (κ k1 k2 k3 u1 u2 u3 : R) : R := sub K u2 (K.mul (K.mul κ (d3 K k1 k2 k3 u1 u2 u3)) k2)
def P3 (κ k1 k2 k3 u1 u2 u3 : R) : R := sub K u3 (K.mul (K.mul κ (d3 K k1 k2 k3 u1 u2 u3)) k3)

theorem div_free (κ k1 k2 k3 u1 u2 u3 : R) (hκ : K.mul κ (d3 K k1 k2 k3 k1 k2 k3) = K.one) :
    d3 K k1 k2 k3 (P1 K κ k1 k2 k3 u1 u2 u3) (P2 K κ k1 k2 k3 u1 u2 u3) (P3 K κ k1 k2 k3 u1 u2 u3) = K.zero := by
  have e : d3 K k1 k2 k3 (P1 K κ k1 k2 k3 u1 u2 u3) (P2 K κ k1 k2 k3 u1 u2 u3) (P3 K κ k1 k2 k3 u1 u2 u3)
      = K.mul (d3 K k1 k2 k3 u1 u2 u3) (sub K K.one (K.mul κ (d3 K k1 k2 k3 k1 k2 k3))) := by
    unfold P1 P2 P3 d3; lib_ring
  rw [e, hκ]; lib_ring

theorem fixes (κ k1 k2 k3 u1 u2 u3 : R) (h : d3 K k1 k2 k3 u1 u2 u3 = K.zero) :
    P1 K κ k1 k2 k3 u1 u2 u3 = u1 ∧ P2 K κ k1 k2 k3 u1 u2 u3 = u2 ∧ P3 K κ k1 k2 k3 u1 u2 u3 = u3 := by
  unfold P1 P2 P3; rw [h]
  exact ⟨by lib_ring, by lib_ring, by lib_ring⟩

theorem idem (κ k1 k2 k3 u1 u2 u3 : R) (hκ : K.mul κ (d3 K k1 k2 k3 k1 k2 k3) = K.one) :
    P1 K κ k1 k2 k3 (P1 K κ k1 k2 k3 u1 u2 u3) (P2 K κ k1 k2 k3 u1 u2 u3) (P3 K κ k1 k2 k3 u1 u2 u3)
        = P1 K κ k1 k2 k3 u1 u2 u3 ∧
      P2 K κ k1 k2 k3 (P1 K κ k1 k2 k3 u1 u2 u3) (P2 K κ k1 k2 k3 u1 u2 u3) (P3 K κ k1 k2 k3 u1 u2 u3)
        = P2 K κ k1 k2 k3 u1 u2 u3 ∧
      P3 K κ k1 k2 k3 (P1 K κ k1 k2 k3 u1 u2 u3) (P2 K κ k1 k2 k3 u1 u2 u3) (P3 K κ k1 k2 k3 u1 u2 u3)
        = P3 K κ k1 k2 k3 u1 u2 u3 :=
  fixes K κ k1 k2 k3 _ _ _ (div_free K κ k1 k2 k3 u1 u2 u3 hκ)

end Ring

end Leray

#print axioms Leray.div_free
#print axioms Leray.fixes
#print axioms Leray.idem

ns/LerayDiscrete.lean

lean_all/ns/LerayDiscrete.lean

/-
  ns/LerayDiscrete.lean — T37 cycle 66: the discrete Leray energy inequality, 2D and 3D.

  Lean 4 core plus NSChain (cn_energy, Cone, Le, sumN). For a run of Crank–Nicolson steps
  a^{t+1} from a^t (every t, energy-neutral nonlinearity — true for every Galerkin truncation in any
  dimension), with E_t = Σ_n (a^t_n)² and D_t = Σ_n k_n (a^{t+1}_n + a^t_n)²:
      E_T + Σ_{t<T} hν D_t = E_0                 (energy equality, every T)       leray_eq
      Σ_{t<T} hν D_t ≤ E_0,   E_T ≤ E_0           (Leray: dissipation bounded)     leray_le
  uniformly in the truncation size N and the number of steps T. This is the a priori bound that every
  3D weak-solution construction rests on; R1 asks for the same uniformity one derivative higher.
    tele_run   E(t+1) + d t = E t  ⇒  E T + Σ_{t<T} d t = E 0
-/
import NSChain
import lib.RingTac

namespace LerayDiscrete
open DetChain NSChain LibRing

section Ring
variable {R : Type} (K : CRing R)

theorem tele_run (E d : Nat → R) (h : ∀ t, K.add (E (t + 1)) (d t) = E t) :
    ∀ T, K.add (E T) (sumN K d T) = E 0
  | 0 => by show K.add (E 0) K.zero = E 0; lib_ring
  | T + 1 => by
    show K.add (E (T + 1)) (K.add (sumN K d T) (d T)) = E 0
    rw [← tele_run E d h T, ← h T]; lib_ring

def En (a : Nat → Nat → R) (N t : Nat) : R := sumN K (fun n => K.mul (a t n) (a t n)) N

def Dn (a : Nat → Nat → R) (k : Nat → R) (h nu : R) (N t : Nat) : R :=
  K.mul h (K.mul nu (sumN K (fun n => K.mul (k n) (K.mul (K.add (a (t + 1) n) (a t n))
    (K.add (a (t + 1) n) (a t n)))) N))

/-- **Energy equality** along a run. -/
theorem leray_eq (a : Nat → Nat → R) (k : Nat → R) (Nn : Nat → Nat → R) (h nu : R) (N : Nat)
    (step : ∀ t n, K.add (a (t + 1) n) (K.neg (a t n))
      = K.mul h (K.add (K.neg (K.mul nu (K.mul (k n) (K.add (a (t + 1) n) (a t n))))) (Nn t n)))
    (neutral : ∀ t, sumN K (fun n => K.mul (K.add (a (t + 1) n) (a t n)) (Nn t n)) N = K.zero) (T : Nat) :
    K.add (En K a N T) (sumN K (Dn K a k h nu N) T) = En K a N 0 :=
  tele_run K (En K a N) (Dn K a k h nu N)
    (fun t => cn_energy K (a t) (a (t + 1)) k (Nn t) h nu N (step t) (neutral t)) T

/-- **Leray**: total dissipation ≤ initial energy, and E_T ≤ E_0. -/
theorem leray_le (P : Cone K) (a : Nat → Nat → R) (k : Nat → R) (Nn : Nat → Nat → R) (h nu : R) (N : Nat)
    (hh : P.nn h) (hnu : P.nn nu) (hk : ∀ n, P.nn (k n))
    (step : ∀ t n, K.add (a (t + 1) n) (K.neg (a t n))
      = K.mul h (K.add (K.neg (K.mul nu (K.mul (k n) (K.add (a (t + 1) n) (a t n))))) (Nn t n)))
    (neutral : ∀ t, sumN K (fun n => K.mul (K.add (a (t + 1) n) (a t n)) (Nn t n)) N = K.zero) (T : Nat) :
    Le K P (sumN K (Dn K a k h nu N) T) (En K a N 0) ∧ Le K P (En K a N T) (En K a N 0) := by
  have eq := leray_eq K a k Nn h nu N step neutral T
  have hE : P.nn (En K a N T) := sumN_nn K P _ (fun n => P.nn_sq _) N
  have hD : P.nn (sumN K (Dn K a k h nu N) T) :=
    sumN_nn K P _ (fun t => dissipation_nonneg K P h nu k _ N hh hnu hk) T
  refine ⟨⟨En K a N T, hE, ?_⟩, ⟨_, hD, eq⟩⟩
  rw [K.add_comm]; exact eq

end Ring

end LerayDiscrete

#print axioms LerayDiscrete.tele_run
#print axioms LerayDiscrete.leray_eq
#print axioms LerayDiscrete.leray_le

ns/LerayProj.lean

lean_all/ns/LerayProj.lean

/-
  ns/LerayProj.lean — T37 cycle 871 (goal G580): the discrete Leray projection on one Fourier mode, over an ordered
  field (ℚ included): for a wave vector k with k·k ≠ 0 and κ = (k·k)⁻¹,
    P v = v − (k·v)κ k
  satisfies  k·(P v) = 0 (divergence-free),  P(P v) = P v,  and  |P v|² + (k·v)²κ = |v|²  (so |P v|² ≤ |v|²).
  This is the projection the semi-implicit 3D Galerkin scheme needs to keep the state divergence-free, so that
  FourierGalerkin.neutral applies at every step (G574b).

  Lean 4 core plus ns/FourierGalerkin (V3, dot3), lib/RingTac.
-/
import ns.FourierGalerkin
import lib.RingTac

namespace LerayProj
open DetChain NSChain LibRing FourierGalerkin

section Gen
variable {R : Type} (K : CRing R)

def smul3 (c : R) (v : V3 R) : V3 R := ⟨K.mul c v.x, K.mul c v.y, K.mul c v.z⟩

def proj (κ : R) (k v : V3 R) : V3 R := add3 K v (neg3 K (smul3 K (K.mul (dot3 K k v) κ) k))

theorem div_free (κ : R) (k v : V3 R) (hκ : K.mul (dot3 K k k) κ = K.one) : dot3 K k (proj K κ k v) = K.zero := by
  unfold proj add3 neg3 smul3 dot3
  unfold dot3 at hκ
  generalize hd : K.add (K.add (K.mul k.x v.x) (K.mul k.y v.y)) (K.mul k.z v.z) = d
  have t : K.add (K.add (K.mul k.x (K.add v.x (K.neg (K.mul (K.mul d κ) k.x))))
      (K.mul k.y (K.add v.y (K.neg (K.mul (K.mul d κ) k.y))))) (K.mul k.z (K.add v.z (K.neg (K.mul (K.mul d κ) k.z))))
      = K.add (K.add (K.add (K.mul k.x v.x) (K.mul k.y v.y)) (K.mul k.z v.z))
        (K.neg (K.mul d (K.mul (K.add (K.add (K.mul k.x k.x) (K.mul k.y k.y)) (K.mul k.z k.z)) κ))) := by lib_ring
  rw [t, hκ, hd]; lib_ring

theorem energy (κ : R) (k v : V3 R) (hκ : K.mul (dot3 K k k) κ = K.one) :
    K.add (dot3 K (proj K κ k v) (proj K κ k v)) (K.mul (K.mul (dot3 K k v) (dot3 K k v)) κ) = dot3 K v v := by
  unfold proj add3 neg3 smul3 dot3
  unfold dot3 at hκ
  generalize hd : K.add (K.add (K.mul k.x v.x) (K.mul k.y v.y)) (K.mul k.z v.z) = d
  generalize hk : K.add (K.add (K.mul k.x k.x) (K.mul k.y k.y)) (K.mul k.z k.z) = kk at hκ
  -- expand: |v|² − 2dκ(k·v) + d²κ²|k|² + d²κ = |v|² − 2d²κ + d²κ(κ kk) + d²κ = |v|²
  have t : K.add (K.add (K.add (K.mul (K.add v.x (K.neg (K.mul (K.mul d κ) k.x))) (K.add v.x (K.neg (K.mul (K.mul d κ) k.x))))
      (K.mul (K.add v.y (K.neg (K.mul (K.mul d κ) k.y))) (K.add v.y (K.neg (K.mul (K.mul d κ) k.y)))))
      (K.mul (K.add v.z (K.neg (K.mul (K.mul d κ) k.z))) (K.add v.z (K.neg (K.mul (K.mul d κ) k.z)))))
      (K.mul (K.mul d d) κ)
      = K.add (K.add (K.add (K.mul v.x v.x) (K.mul v.y v.y)) (K.mul v.z v.z))
        (K.add (K.mul (K.mul (K.mul d d) κ) (K.add (K.mul kk κ) (K.neg K.one)))
          (K.mul (K.add (K.mul d K.one) (K.neg (K.add (K.add (K.mul k.x v.x) (K.mul k.y v.y)) (K.mul k.z v.z))))
            (K.mul (K.add d d) κ))) := by
    rw [← hk]; lib_ring
  rw [t, hκ, hd]; lib_ring

theorem idem (κ : R) (k v : V3 R) (hκ : K.mul (dot3 K k k) κ = K.one) : proj K κ k (proj K κ k v) = proj K κ k v := by
  have h0 := div_free K κ k v hκ
  show add3 K (proj K κ k v) (neg3 K (smul3 K (K.mul (dot3 K k (proj K κ k v)) κ) k)) = proj K κ k v
  rw [h0]
  generalize proj K κ k v = w
  cases w with
  | mk wx wy wz =>
    show (⟨K.add wx (K.neg (K.mul (K.mul K.zero κ) k.x)), K.add wy (K.neg (K.mul (K.mul K.zero κ) k.y)),
      K.add wz (K.neg (K.mul (K.mul K.zero κ) k.z))⟩ : V3 R) = ⟨wx, wy, wz⟩
    have ex : K.add wx (K.neg (K.mul (K.mul K.zero κ) k.x)) = wx := by lib_ring
    have ey : K.add wy (K.neg (K.mul (K.mul K.zero κ) k.y)) = wy := by lib_ring
    have ez : K.add wz (K.neg (K.mul (K.mul K.zero κ) k.z)) = wz := by lib_ring
    rw [ex, ey, ez]

end Gen

end LerayProj

#print axioms LerayProj.div_free
#print axioms LerayProj.energy
#print axioms LerayProj.idem

ns/LerayProj2.lean

lean_all/ns/LerayProj2.lean

/-
  ns/LerayProj2.lean — T37 cycle 874 (goal G583): more on the one-mode Leray projection P v = v − (k·v)κk, over any
  commutative ring (κ given with κ·(k·k) = 1 only where needed):
    selfadj      (P v)·w = v·(P w)
    fix          k·a = 0  ⇒  P a = a
    proj_test    k·a = 0  ⇒  a·(P F) = a·F
  So projecting the explicit forcing of the semi-implicit 3D Galerkin step keeps the energy neutrality
  Σ_p a_p·(P F)_p = Σ_p a_p·F_p = B(a, a, a) = 0 (FourierGalerkin.neutral) for divergence-free states.

  Lean 4 core plus ns/LerayProj (proj, smul3), lib/RingTac.
-/
import ns.LerayProj

namespace LerayProj2
open DetChain NSChain LibRing FourierGalerkin LerayProj

section Gen
variable {R : Type} (K : CRing R)

theorem selfadj (κ : R) (k v w : V3 R) : dot3 K (proj K κ k v) w = dot3 K v (proj K κ k w) := by
  cases k with
  | mk kx ky kz => cases v with
    | mk vx vy vz => cases w with
      | mk wx wy wz =>
        show K.add (K.add (K.mul (K.add vx (K.neg (K.mul (K.mul (K.add (K.add (K.mul kx vx) (K.mul ky vy)) (K.mul kz vz)) κ) kx))) wx)
            (K.mul (K.add vy (K.neg (K.mul (K.mul (K.add (K.add (K.mul kx vx) (K.mul ky vy)) (K.mul kz vz)) κ) ky))) wy))
            (K.mul (K.add vz (K.neg (K.mul (K.mul (K.add (K.add (K.mul kx vx) (K.mul ky vy)) (K.mul kz vz)) κ) kz))) wz)
          = K.add (K.add (K.mul vx (K.add wx (K.neg (K.mul (K.mul (K.add (K.add (K.mul kx wx) (K.mul ky wy)) (K.mul kz wz)) κ) kx))))
            (K.mul vy (K.add wy (K.neg (K.mul (K.mul (K.add (K.add (K.mul kx wx) (K.mul ky wy)) (K.mul kz wz)) κ) ky)))))
            (K.mul vz (K.add wz (K.neg (K.mul (K.mul (K.add (K.add (K.mul kx wx) (K.mul ky wy)) (K.mul kz wz)) κ) kz))))
        lib_ring

theorem fix (κ : R) (k a : V3 R) (ha : dot3 K k a = K.zero) : proj K κ k a = a := by
  unfold proj
  rw [ha]
  cases a with
  | mk ax ay az =>
    show (⟨K.add ax (K.neg (K.mul (K.mul K.zero κ) k.x)), K.add ay (K.neg (K.mul (K.mul K.zero κ) k.y)),
      K.add az (K.neg (K.mul (K.mul K.zero κ) k.z))⟩ : V3 R) = ⟨ax, ay, az⟩
    have ex : K.add ax (K.neg (K.mul (K.mul K.zero κ) k.x)) = ax := by lib_ring
    have ey : K.add ay (K.neg (K.mul (K.mul K.zero κ) k.y)) = ay := by lib_ring
    have ez : K.add az (K.neg (K.mul (K.mul K.zero κ) k.z)) = az := by lib_ring
    rw [ex, ey, ez]

theorem proj_test (κ : R) (k a F : V3 R) (ha : dot3 K k a = K.zero) : dot3 K a (proj K κ k F) = dot3 K a F := by
  rw [← selfadj K κ k a F, fix K κ k a ha]

end Gen

end LerayProj2

#print axioms LerayProj2.selfadj
#print axioms LerayProj2.fix
#print axioms LerayProj2.proj_test

ns/LerayProj3.lean

lean_all/ns/LerayProj3.lean

/-
  ns/LerayProj3.lean — T37 cycle 892 (G592b piece): the Leray projection does not increase energy, mode by mode and
  summed, over any commutative ring with a nonnegative cone (ℚ included): for κ_p ≥ 0 with κ_p (k_p·k_p) = 1,
    |P_p v|² ≤ |v|²     and     Σ_{p<N} |P_p v_p|² ≤ Σ_{p<N} |v_p|².
  So a bound Σ_p |N_p|² ≤ C·E² for the Galerkin force passes to the projected force P N, as SemiGlobal needs.

  Lean 4 core plus ns/LerayProj (proj, energy), ns/DyadicLarge (sum_le).
-/
import ns.LerayProj
import ns.DyadicLarge

namespace LerayProj3
open DetChain NSChain FourierGalerkin LerayProj DyadicLarge

section Gen
variable {R : Type} (K : CRing R) (P : Cone K)

theorem proj_le (κ : R) (k v : V3 R) (hnn : P.nn κ) (hκ : K.mul (dot3 K k k) κ = K.one) :
    Le K P (dot3 K (proj K κ k v) (proj K κ k v)) (dot3 K v v) :=
  ⟨K.mul (K.mul (dot3 K k v) (dot3 K k v)) κ, P.nn_mul _ _ (P.nn_sq _) hnn, energy K κ k v hκ⟩

theorem proj_sum_le (κ : Nat → R) (k v : Nat → V3 R) (hnn : ∀ p, P.nn (κ p))
    (hκ : ∀ p, K.mul (dot3 K (k p) (k p)) (κ p) = K.one) (N : Nat) :
    Le K P (sumN K (fun p => dot3 K (proj K (κ p) (k p) (v p)) (proj K (κ p) (k p) (v p))) N)
      (sumN K (fun p => dot3 K (v p) (v p)) N) :=
  sum_le K P _ _ (fun p => proj_le K P (κ p) (k p) (v p) (hnn p) (hκ p)) N

end Gen

end LerayProj3

#print axioms LerayProj3.proj_le
#print axioms LerayProj3.proj_sum_le

ns/LevelQ.lean

lean_all/ns/LevelQ.lean

/-
  ns/LevelQ.lean — T37 cycle 1908 (goal G860e, piece b4b): the equal-time per-level bounds qa, qf of RiemannLevel from
  the step-size distance. If Qe(a, b) ≤ X·p (a, b the level-j and level-(j+1) states at the same time; StepUniformS),
  ν·iν = 1, and r ∈ l with |k_r|² ≥ 1:
    qa_comp   (f(b_r) − f(a_r))² ≤ (iν·X)·p                          (any component selector f)
    z_le_qe   ν·Z(u − v) ≤ Qe(u, v)
    qf_comp   (f(N_r(b,b)) − f(N_r(a,a)))² ≤ (L·(iν·X))·p           (ForceLipZ with Z(b) ≤ Za, A(a) ≤ Ab)
  Lean 4 core plus ns/ForceLipZ, ns/RHUniform.
-/
import ns.ForceLipZ
import ns.RHUniform

namespace LevelQ
open DetChain NSChain DyadicSmallGlobal LibRing DyadicLarge FourierGalerkin GalerkinForce ForceBound ForceSob
  ModeCount CubeCount ForceUniform NormCompare EnstGalerkin ForceTwoScale ForceBilin EnstDiff LatticeSum ForceDiff
  RealCoef DyadicCoef StepCauchy QeComp ForceLipZ

section Gen
variable {R : Type} (K : CRing R) (O : OrdR K) {I : Type}

theorem qa_comp (ν k iν X p : R) (hν : O.nn ν) (hk : O.nn k) (hi : O.nn iν) (hνi : K.mul ν iν = K.one)
    (l : List I) (kz : I → Mode) (a b : I → V3 R) (r : I) (hr : r ∈ l) (hw : Le K O.toCone K.one (wt K kz r))
    (f : V3 R → R) (hfc : ∀ w, Le K O.toCone (K.mul (f w) (f w)) (dot3 K w w))
    (hf : ∀ u v, f (sub3 K u v) = sub K (f u) (f v))
    (hQ : Le K O.toCone (Qe K ν k l kz a b) (K.mul X p)) :
    Le K O.toCone (K.mul (sub K (f (b r)) (f (a r))) (sub K (f (b r)) (f (a r)))) (K.mul (K.mul iν X) p) := by
  have c := qe_comp K O ν k hν hk l kz b a r hr hw f hfc
  rw [RHUniform.qe_symm K ν k l kz b a] at c
  have d := div_nu K O ν iν _ _ hi hνi (le_trans' K O.toCone _ _ _ c hQ)
  rw [show f (dF K b a r) = sub K (f (b r)) (f (a r)) from hf _ _] at d
  exact le_trans' K O.toCone _ _ _ d ⟨K.zero, O.nn_zero, by lib_ring⟩

theorem z_le_qe (ν k : R) (hk : O.nn k) (l : List I) (kz : I → Mode) (u v : I → V3 R) :
    Le K O.toCone (K.mul ν (lsum K (fun p => K.mul (wt K kz p) (dot3 K (dF K u v p) (dF K u v p))) l)) (Qe K ν k l kz u v) :=
  ⟨_, O.nn_mul _ _ (O.nn_mul _ _ hk (O.nn_sq ν))
    (lsum_nn K O.toCone _ (fun _ => O.nn_mul _ _ (O.nn_sq _) (dot3_nn K O.toCone _)) l), rfl⟩

variable (inv : R → R) (mul_inv : ∀ y, y ≠ K.zero → K.mul y (inv y) = K.one) (h10 : K.one ≠ K.zero)

include mul_inv h10 in
theorem qf_comp (l : List I) (kz : I → Mode) (hd : List.Pairwise (fun a b => kz a ≠ kz b) l)
    (h1 : ∀ p, 1 ≤ nrm (kz p)) (M : Nat) (hM : ∀ p, p ∈ l → nrm (kz p) ≤ M + 1) (Rc t0 : Nat) (hRM : Rc ≤ M)
    (ι W Za Ab ν k iν X p Lc : R) (hW : ∀ q, q ∈ l → Le K O.toCone (wt K kz q) W) (a b : I → V3 R)
    (hk : O.nn k) (hi : O.nn iν) (hνi : K.mul ν iν = K.one) (hLc : O.nn Lc)
    (hZb : Le K O.toCone (lsum K (fun q => K.mul (wt K kz q) (dot3 K (b q) (b q))) l) Za)
    (hAa : Le K O.toCone (lsum K (fun q => K.mul (K.mul (wt K kz q) (wt K kz q)) (dot3 K (a q) (a q))) l) Ab)
    (hL : Lc = K.add (K.add
          (K.mul (K.add K.one K.one) (K.mul (K.add K.one K.one) (K.mul (K.mul ι ι)
            (K.mul (K.add (K.mul (castN K (cube Rc)) K.one) (K.mul (castN K 135) (iv K inv (Rc + 1)))) Za))))
          (K.mul (K.add K.one K.one) (K.mul (K.add K.one K.one) (K.mul (K.mul ι ι) (K.mul W
            (K.mul (K.add (K.mul (castN K (cube Rc)) (K.mul (inv (castN K (t0 + 1))) (inv (castN K (t0 + 1)))))
                (K.mul (castN K 135) (iv K inv (Rc + 1)))) Za))))))
          (K.mul (K.add K.one K.one) (K.mul (K.mul ι ι) (K.mul Ab (castN K 135)))))
    (hQ : Le K O.toCone (Qe K ν k l kz a b) (K.mul X p)) (r : I) (hr : r ∈ l)
    (f : V3 R → R) (hfc : ∀ w, Le K O.toCone (K.mul (f w) (f w)) (dot3 K w w))
    (hf : ∀ u v, f (sub3 K u v) = sub K (f u) (f v)) :
    Le K O.toCone
      (K.mul (sub K (f (Nl K l (TriadSparse.delta K kz) ι (kv K kz) b b r)) (f (Nl K l (TriadSparse.delta K kz) ι (kv K kz) a a r)))
        (sub K (f (Nl K l (TriadSparse.delta K kz) ι (kv K kz) b b r)) (f (Nl K l (TriadSparse.delta K kz) ι (kv K kz) a a r))))
      (K.mul (K.mul Lc (K.mul iν X)) p) := by
  have fl := force_lip K O inv mul_inv h10 l kz hd h1 M hM Rc t0 hRM ι W Za Ab hW b a hZb hAa
  rw [← hL] at fl
  have tl := term_le K O (fun r => dot3 K (sub3 K (Nl K l (TriadSparse.delta K kz) ι (kv K kz) b b r) (Nl K l (TriadSparse.delta K kz) ι (kv K kz) a a r))
      (sub3 K (Nl K l (TriadSparse.delta K kz) ι (kv K kz) b b r) (Nl K l (TriadSparse.delta K kz) ι (kv K kz) a a r)))
    (fun r => dot3_nn K O.toCone _) l r hr
  have cf := hfc (sub3 K (Nl K l (TriadSparse.delta K kz) ι (kv K kz) b b r) (Nl K l (TriadSparse.delta K kz) ι (kv K kz) a a r))
  rw [hf] at cf
  have zq := z_le_qe K O ν k hk l kz b a
  rw [RHUniform.qe_symm K ν k l kz b a] at zq
  have zd := div_nu K O ν iν _ _ hi hνi (le_trans' K O.toCone _ _ _ zq hQ)
  refine le_trans' K O.toCone _ _ _ cf (le_trans' K O.toCone _ _ _ tl (le_trans' K O.toCone _ _ _ fl
    (le_trans' K O.toCone _ _ _ (le_mul_nn K O.toCone Lc _ _ hLc zd) ⟨K.zero, O.nn_zero, by lib_ring⟩)))

end Gen
end LevelQ

#print axioms LevelQ.qa_comp
#print axioms LevelQ.z_le_qe
#print axioms LevelQ.qf_comp

ns/LevelS.lean

lean_all/ns/LevelS.lean

/-
  ns/LevelS.lean — T37 cycle 1910 (goal G860e, piece b5a): the force moves by O(step) over one fine step, giving the
  bound sf of RiemannLevel. For a Galerkin step b(m) → b(m+1) of size s with s² ≤ S₀·p, Σ_r|N_r(b(m))|² ≤ BN,
  Σ_r w_r²|b_r(m)|² ≤ BA, weights ≤ W on l:
    z_le_w    Z(d) ≤ W·Σ_r |d_r|²
    sf_comp   (f(N_r(b(m+1))) − f(N_r(b(m))))² ≤ (L·(W·(S₀·(2BN + 2ν²BA))))·p
  (ForceLipZ.force_lip along the step, TimeLip.time_lip for Σ|b(m+1) − b(m)|²).
  Lean 4 core plus ns/LevelQ, ns/TimeLip, ns/StepLevel.
-/
import ns.LevelQ
import ns.TimeLip
import ns.StepLevel

namespace LevelS
open DetChain NSChain DyadicSmallGlobal LibRing DyadicLarge FourierGalerkin GalerkinForce ForceBound ForceSob
  ModeCount CubeCount ForceUniform NormCompare EnstGalerkin ForceTwoScale ForceBilin EnstDiff LatticeSum ForceDiff
  RealCoef DyadicCoef StepCauchy QeComp ForceLipZ EnstStep

section Gen
variable {R : Type} (K : CRing R) (O : OrdR K) {I : Type}

theorem z_le_w (kz : I → Mode) (l : List I) (W : R) (hW : ∀ q, q ∈ l → Le K O.toCone (wt K kz q) W) (d : I → V3 R) :
    Le K O.toCone (lsum K (fun q => K.mul (wt K kz q) (dot3 K (d q) (d q))) l) (K.mul W (lsum K (fun q => dot3 K (d q) (d q)) l)) := by
  rw [← lsum_mul_left K W]
  exact lsum_le_mem K O _ _ l (fun q hq => le_mul_right' K O.toCone (hW q hq) (dot3_nn K O.toCone (d q)))

variable (inv : R → R) (mul_inv : ∀ y, y ≠ K.zero → K.mul y (inv y) = K.one) (h10 : K.one ≠ K.zero)

include mul_inv h10 in
theorem sf_comp (l : List I) (kz : I → Mode) (hd : List.Pairwise (fun a b => kz a ≠ kz b) l)
    (h1 : ∀ p, 1 ≤ nrm (kz p)) (M : Nat) (hM : ∀ p, p ∈ l → nrm (kz p) ≤ M + 1) (Rc t0 : Nat) (hRM : Rc ≤ M)
    (ι W Za Ab ν s S0 p BN BA Lc : R) (hW : ∀ q, q ∈ l → Le K O.toCone (wt K kz q) W) (hWn : O.nn W) (hLc : O.nn Lc)
    (hs0 : O.nn s) (hν : O.nn ν) (b' b : I → V3 R)
    (hstep : ∀ r, Step K s ν (wt K kz r) (b' r) (b r) (Nl K l (TriadSparse.delta K kz) ι (kv K kz) b b r))
    (hZb : Le K O.toCone (lsum K (fun q => K.mul (wt K kz q) (dot3 K (b' q) (b' q))) l) Za)
    (hAa : Le K O.toCone (lsum K (fun q => K.mul (K.mul (wt K kz q) (wt K kz q)) (dot3 K (b q) (b q))) l) Ab)
    (hL : Lc = K.add (K.add
          (K.mul (K.add K.one K.one) (K.mul (K.add K.one K.one) (K.mul (K.mul ι ι)
            (K.mul (K.add (K.mul (castN K (cube Rc)) K.one) (K.mul (castN K 135) (iv K inv (Rc + 1)))) Za))))
          (K.mul (K.add K.one K.one) (K.mul (K.add K.one K.one) (K.mul (K.mul ι ι) (K.mul W
            (K.mul (K.add (K.mul (castN K (cube Rc)) (K.mul (inv (castN K (t0 + 1))) (inv (castN K (t0 + 1)))))
                (K.mul (castN K 135) (iv K inv (Rc + 1)))) Za))))))
          (K.mul (K.add K.one K.one) (K.mul (K.mul ι ι) (K.mul Ab (castN K 135)))))
    (hBN : Le K O.toCone (lsum K (fun r => dot3 K (Nl K l (TriadSparse.delta K kz) ι (kv K kz) b b r) (Nl K l (TriadSparse.delta K kz) ι (kv K kz) b b r)) l) BN)
    (hBA : Le K O.toCone (lsum K (fun r => K.mul (K.mul (wt K kz r) (wt K kz r)) (dot3 K (b r) (b r))) l) BA)
    (hss : Le K O.toCone (K.mul s s) (K.mul S0 p)) (r : I) (hr : r ∈ l)
    (f : V3 R → R) (hfc : ∀ w, Le K O.toCone (K.mul (f w) (f w)) (dot3 K w w))
    (hf : ∀ u v, f (sub3 K u v) = sub K (f u) (f v)) :
    Le K O.toCone
      (K.mul (sub K (f (Nl K l (TriadSparse.delta K kz) ι (kv K kz) b' b' r)) (f (Nl K l (TriadSparse.delta K kz) ι (kv K kz) b b r)))
        (sub K (f (Nl K l (TriadSparse.delta K kz) ι (kv K kz) b' b' r)) (f (Nl K l (TriadSparse.delta K kz) ι (kv K kz) b b r))))
      (K.mul (K.mul Lc (K.mul W (K.mul S0 (K.add (K.mul (K.add K.one K.one) BN) (K.mul (K.add K.one K.one) (K.mul (K.mul ν ν) BA)))))) p) := by
  have two : O.nn (K.add K.one K.one) := O.nn_add _ _ (RatModel.nn_one' K O.toCone) (RatModel.nn_one' K O.toCone)
  have fl := force_lip K O inv mul_inv h10 l kz hd h1 M hM Rc t0 hRM ι W Za Ab hW b' b hZb hAa
  rw [← hL] at fl
  have tl := term_le K O (fun r => dot3 K (sub3 K (Nl K l (TriadSparse.delta K kz) ι (kv K kz) b' b' r) (Nl K l (TriadSparse.delta K kz) ι (kv K kz) b b r))
      (sub3 K (Nl K l (TriadSparse.delta K kz) ι (kv K kz) b' b' r) (Nl K l (TriadSparse.delta K kz) ι (kv K kz) b b r)))
    (fun r => dot3_nn K O.toCone _) l r hr
  have cf := hfc (sub3 K (Nl K l (TriadSparse.delta K kz) ι (kv K kz) b' b' r) (Nl K l (TriadSparse.delta K kz) ι (kv K kz) b b r))
  rw [hf] at cf
  have zw := z_le_w K O kz l W hW (dF K b' b)
  have tlip := TimeLip.time_lip K O.toCone l s ν hs0 hν (wt K kz) (fun r => dot3_nn K O.toCone _) b' b
    (fun r => Nl K l (TriadSparse.delta K kz) ι (kv K kz) b b r) hstep
  have hBNn : O.nn BN := ForceLipZ.nn_of_le K O _ _ (lsum_nn K O.toCone _ (fun _ => dot3_nn K O.toCone _) l) hBN
  have hBAn : O.nn BA := ForceLipZ.nn_of_le K O _ _ (lsum_nn K O.toCone _ (fun _ => O.nn_mul _ _ (O.nn_sq _) (dot3_nn K O.toCone _)) l) hBA
  -- Σ|b' − b|² ≤ 2s²·BN + 2s²ν²·BA ≤ S₀p·(2BN + 2ν²BA)
  have t2 : Le K O.toCone (K.add (K.mul (K.mul (K.add K.one K.one) (K.mul s s)) (lsum K (fun r => dot3 K (Nl K l (TriadSparse.delta K kz) ι (kv K kz) b b r) (Nl K l (TriadSparse.delta K kz) ι (kv K kz) b b r)) l))
        (K.mul (K.mul (K.add K.one K.one) (K.mul (K.mul s s) (K.mul ν ν))) (lsum K (fun r => K.mul (K.mul (wt K kz r) (wt K kz r)) (dot3 K (b r) (b r))) l)))
      (K.mul (K.mul S0 p) (K.add (K.mul (K.add K.one K.one) BN) (K.mul (K.add K.one K.one) (K.mul (K.mul ν ν) BA)))) := by
    have u1 := StepLevel.mul_le2 K O (K.mul s s) (K.mul S0 p) _ BN (O.nn_sq s) hBNn hss hBN
    have u2 := StepLevel.mul_le2 K O (K.mul s s) (K.mul S0 p) _ (K.mul (K.mul ν ν) BA) (O.nn_sq s) (O.nn_mul _ _ (O.nn_sq ν) hBAn) hss
      (le_mul_nn K O.toCone (K.mul ν ν) _ _ (O.nn_sq ν) hBA)
    refine le_trans' K O.toCone _ _ _ ⟨K.zero, O.nn_zero, ?_⟩ (le_trans' K O.toCone _ _ _
      (le_add' K O.toCone _ _ _ _ (le_mul_nn K O.toCone _ _ _ two u1) (le_mul_nn K O.toCone _ _ _ two u2)) ⟨K.zero, O.nn_zero, ?_⟩)
    · lib_ring
    · lib_ring
  have dsum : Le K O.toCone (lsum K (fun q => dot3 K (dF K b' b q) (dF K b' b q)) l)
      (K.mul (K.mul S0 p) (K.add (K.mul (K.add K.one K.one) BN) (K.mul (K.add K.one K.one) (K.mul (K.mul ν ν) BA)))) :=
    le_trans' K O.toCone _ _ _ tlip t2
  refine le_trans' K O.toCone _ _ _ cf (le_trans' K O.toCone _ _ _ tl (le_trans' K O.toCone _ _ _ fl
    (le_trans' K O.toCone _ _ _ (le_mul_nn K O.toCone Lc _ _ hLc (le_trans' K O.toCone _ _ _ zw (le_mul_nn K O.toCone W _ _ hWn dsum)))
      ⟨K.zero, O.nn_zero, by lib_ring⟩)))

end Gen
end LevelS

#print axioms LevelS.z_le_w
#print axioms LevelS.sf_comp

ns/LipDamp.lean

lean_all/ns/LipDamp.lean

/-
  ns/LipDamp.lean — T37 cycle 1828 (goal G858a, piece 1): the scheme is non-expansive in the enstrophy-difference
  norm for small data, over an ordered field. Two Galerkin solutions a, b on the same modes (forces Nl(a), Nl(b));
  with the doubled EnstLipschitz rates α_t, β_t of ForcedDiffStep (TruncDefect.alphaT, betaT),
    lip_damp   α_t + β_t ≤ ν² for all t  ⇒  Q_{a−b}(n) ≤ Q_{a−b}(0) for every n
  (ForcedDiffStep.forced_step with zero defect as the step of DampGronwall.damp_all). Unlike EnstLipschitz there is
  no factor Π(ν + hα_t)/ν: the comparison of two runs is uniform in time.
  Lean 4 core plus ns/TruncDefect, ns/DampGronwall, ns/EnstH2.
-/
import ns.TruncDefect
import ns.DampGronwall
import ns.EnstH2

namespace LipDamp
open DetChain NSChain DyadicSmallGlobal LibRing DyadicLarge FourierGalerkin GalerkinForce ForceBound ForceSob
  ModeCount CubeCount ForceUniform NormCompare EnstStep EnstGlobal EnstGalerkin ForceTwoScale ForceBilin EnstDiff
  ForceDiff EnstGronwall LatticeSum EnstLipschitz TruncDefect

section Gen
variable {R : Type} (K : CRing R) (O : OrdR K) {I : Type}
variable (inv : R → R) (mul_inv : ∀ y, y ≠ K.zero → K.mul y (inv y) = K.one) (h10 : K.one ≠ K.zero)

include mul_inv h10 in
theorem lip_damp (l : List I) (kz : I → Mode) (hd : List.Pairwise (fun a b => kz a ≠ kz b) l)
    (h1 : ∀ p, 1 ≤ nrm (kz p)) (M : Nat) (hM : ∀ p, p ∈ l → nrm (kz p) ≤ M + 1) (Rc t0 : Nat) (hRM : Rc ≤ M)
    (ι h ν : R) (hh : O.nn h) (hν : O.nn ν) (a b : Nat → I → V3 R)
    (ha : ∀ t r, Step K h ν (wt K kz r) (a (t + 1) r) (a t r) (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (a t) (a t) r))
    (hb : ∀ t r, Step K h ν (wt K kz r) (b (t + 1) r) (b t r) (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (b t) (b t) r))
    (hab : ∀ t, Le K O.toCone (K.add (alphaT K inv ι Rc l kz a b t) (betaT K inv ι Rc t0 l kz a t)) (K.mul ν ν)) (n : Nat) :
    Le K O.toCone (Q K ν h (Zs K l kz (dS K a b)) (As K l kz (dS K a b)) n)
      (Q K ν h (Zs K l kz (dS K a b)) (As K l kz (dS K a b)) 0) := by
  have hw : ∀ r, O.nn (wt K kz r) := fun r => dot3_nn K O.toCone _
  have n2 : O.nn (two K) := O.nn_add _ _ (RatModel.nn_one' K O.toCone) (RatModel.nn_one' K O.toCone)
  have hZn : ∀ (u : Nat → I → V3 R) t, O.nn (Zs K l kz u t) := fun u t =>
    lsum_nn K O.toCone _ (fun r => O.nn_mul _ _ (hw r) (dot3_nn K O.toCone _)) l
  have hAn : ∀ (u : Nat → I → V3 R) t, O.nn (As K l kz u t) := fun u t =>
    lsum_nn K O.toCone _ (fun r => O.nn_mul _ _ (O.nn_sq _) (dot3_nn K O.toCone _)) l
  have hiv : O.nn (iv K inv (Rc + 1)) := iv_nn K O inv mul_inv h10 Rc
  have nC1 : O.nn (C1 K inv Rc) := O.nn_add _ _ (O.nn_mul _ _ (RatModel.nn_cast K O.toCone _) (RatModel.nn_one' K O.toCone))
    (O.nn_mul _ _ (RatModel.nn_cast K O.toCone _) hiv)
  have hα : ∀ t, O.nn (alphaT K inv ι Rc l kz a b t) := fun t => O.nn_mul _ _ n2 (O.nn_add _ _
    (O.nn_mul _ _ n2 (O.nn_mul _ _ (O.nn_mul _ _ n2 (O.nn_sq ι)) (O.nn_mul _ _ nC1 (hZn a t))))
    (O.nn_mul _ _ n2 (O.nn_mul _ _ (O.nn_sq ι) (O.nn_mul _ _ (RatModel.nn_cast K O.toCone 135) (hAn b t)))))
  have hN : ∀ t, Le K O.toCone
      (lsum K (fun r => dot3 K (sub3 K (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (a t) (a t) r)
          (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (b t) (b t) r))
        (sub3 K (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (a t) (a t) r)
          (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (b t) (b t) r))) l)
      (K.add (K.mul (two K) (lsum K (fun r => dot3 K (sub3 K (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (a t) (a t) r)
          (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (b t) (b t) r))
        (sub3 K (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (a t) (a t) r)
          (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (b t) (b t) r))) l)) (K.mul (two K) K.zero)) := by
    intro t
    have hX := lsum_nn K O.toCone (fun r => dot3 K (sub3 K (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (a t) (a t) r) (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (b t) (b t) r)) (sub3 K (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (a t) (a t) r) (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (b t) (b t) r)))
      (fun r => dot3_nn K O.toCone (sub3 K (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (a t) (a t) r) (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (b t) (b t) r))) l
    generalize lsum K (fun r => dot3 K (sub3 K (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (a t) (a t) r) (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (b t) (b t) r)) (sub3 K (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (a t) (a t) r) (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (b t) (b t) r))) l = X at hX ⊢
    exact ⟨K.add X K.zero, O.nn_add _ _ hX O.nn_zero,
      by show K.add X (K.add X K.zero) = K.add (K.mul (K.add K.one K.one) X) (K.mul (K.add K.one K.one) K.zero); lib_ring⟩
  have st := fun s => ForcedDiffStep.forced_step K O inv mul_inv h10 l kz hd h1 M hM Rc t0 hRM ι h ν hh hν a b
    (fun t r => Nl K l (TriadSparse.delta K kz) ι (kv K kz) (a t) (a t) r) (fun t r => Nl K l (TriadSparse.delta K kz) ι (kv K kz) (b t) (b t) r) (fun _ => K.zero) hN ha hb s
  have t := DampGronwall.damp_all K O ν h K.zero (Zs K l kz (dS K a b)) (As K l kz (dS K a b))
    (alphaT K inv ι Rc l kz a b) (betaT K inv ι Rc t0 l kz a) (fun _ => K.mul (two K) K.zero) hh hα (hAn _)
    (fun t => EnstH2.Z_le_A K O kz h1 l (dS K a b t)) hab (fun _ => ⟨K.zero, O.nn_zero, by lib_ring⟩) st n
  refine le_trans' K O.toCone _ _ _ t ⟨K.zero, O.nn_zero, ?_⟩
  generalize Q K ν h (Zs K l kz (dS K a b)) (As K l kz (dS K a b)) 0 = Q0
  lib_ring

end Gen
end LipDamp

#print axioms LipDamp.lip_damp

ns/LipG.lean

lean_all/ns/LipG.lean

/-
  ns/LipG.lean — T37 cycle 1121 (goal G717, piece c4b): the two-step Lipschitz factor in the step-size recursion,
  over an ordered field (ν > 0 with inverse iν, k ≥ 0, 0 ≤ α_t ≤ α):
    lip_G     ν²·L ≤ (ν + kα₀)(ν + kα₁)·x,  x ≥ 0  ⇒  L ≤ G·x,   G = (ν + kα)²·iν²   (EnstLipschitz with T = 2)
    G_ge_one  1 ≤ G,  hence 1 ≤ (1 + k)·G   (the hypothesis of ErrRec.err_bound)

  Lean 4 core plus ns/EnstGlobal, ns/EnstLocal (sq_mono), ns/TruncRate (mm).
-/
import ns.EnstGlobal
import ns.EnstLocal
import ns.TruncRate

namespace LipG
open DetChain NSChain DyadicSmallGlobal LibRing DyadicLarge EnstGlobal

section Gen
variable {R : Type} (K : CRing R) (O : OrdR K)

def GG (ν iν k α : R) : R := K.mul (K.mul (K.add ν (K.mul k α)) (K.add ν (K.mul k α))) (K.mul iν iν)

theorem lip_G (ν iν k α α0 α1 L x : R) (hν : O.nn ν) (hiν : K.mul ν iν = K.one) (hk : O.nn k)
    (ha0 : O.nn α0) (ha1 : O.nn α1) (h0 : Le K O.toCone α0 α) (h1 : Le K O.toCone α1 α) (hx : O.nn x)
    (hL : Le K O.toCone (K.mul (K.mul ν ν) L) (K.mul (K.mul (K.add ν (K.mul k α0)) (K.add ν (K.mul k α1))) x)) :
    Le K O.toCone L (K.mul (GG K ν iν k α) x) := by
  have e0 := le_add' K O.toCone _ _ _ _ (le_refl' K O.toCone ν) (le_mul_nn K O.toCone k _ _ hk h0)
  have e1 := le_add' K O.toCone _ _ _ _ (le_refl' K O.toCone ν) (le_mul_nn K O.toCone k _ _ hk h1)
  have p := TruncRate.mm K O _ _ _ _ (O.nn_add _ _ hν (O.nn_mul _ _ hk ha0))
    (TruncRate.nn_of_le K O _ _ (O.nn_add _ _ hν (O.nn_mul _ _ hk ha1)) e1) e0 e1
  have q := le_trans' K O.toCone _ _ _ hL (ForceSob.le_mul_right' K O.toCone p hx)
  have r := le_mul_nn K O.toCone (K.mul iν iν) _ _ (O.nn_sq iν) q
  refine le_trans' K O.toCone _ _ _ ⟨K.zero, O.nn_zero, ?_⟩ (le_trans' K O.toCone _ _ _ r ⟨K.zero, O.nn_zero, ?_⟩)
  · calc K.add L K.zero = K.mul (K.mul (K.mul ν iν) (K.mul ν iν)) L := by rw [hiν]; lib_ring
      _ = _ := by lib_ring
  · unfold GG; lib_ring

theorem G_ge_one (ν iν k α : R) (hν : O.nn ν) (hiν : K.mul ν iν = K.one) (hk : O.nn k) (hα : O.nn α) :
    Le K O.toCone K.one (GG K ν iν k α) ∧ Le K O.toCone K.one (K.mul (K.add K.one k) (GG K ν iν k α)) := by
  have e := le_add' K O.toCone _ _ _ _ (le_refl' K O.toCone ν) (⟨K.mul k α, O.nn_mul _ _ hk hα, by lib_ring⟩ :
    Le K O.toCone K.zero (K.mul k α))
  have s := EnstLocal.sq_mono K O _ _ hν (O.nn_add _ _ hν (O.nn_mul _ _ hk hα)) (le_trans' K O.toCone _ _ _ ⟨K.zero, O.nn_zero, by lib_ring⟩ e)
  have m := le_mul_nn K O.toCone (K.mul iν iν) _ _ (O.nn_sq iν) s
  have g1 : Le K O.toCone K.one (GG K ν iν k α) := by
    refine le_trans' K O.toCone _ _ _ ⟨K.zero, O.nn_zero, ?_⟩ (le_trans' K O.toCone _ _ _ m ⟨K.zero, O.nn_zero, ?_⟩)
    · calc K.add K.one K.zero = K.mul (K.mul ν iν) (K.mul ν iν) := by rw [hiν]; lib_ring
        _ = _ := by lib_ring
    · unfold GG; lib_ring
  refine ⟨g1, ?_⟩
  have gn : O.nn (GG K ν iν k α) := TruncRate.nn_of_le K O _ _ (RatModel.nn_one' K O.toCone) g1
  have a := TruncRate.mm K O _ _ _ _ (RatModel.nn_one' K O.toCone) gn
    (⟨k, hk, by lib_ring⟩ : Le K O.toCone K.one (K.add K.one k)) g1
  exact le_trans' K O.toCone _ _ _ ⟨K.zero, O.nn_zero, by lib_ring⟩ a

end Gen

end LipG

#print axioms LipG.lip_G
#print axioms LipG.G_ge_one

ns/LocalCont.lean

lean_all/ns/LocalCont.lean

/-
  ns/LocalCont.lean — T37 cycle 194: discrete local continuation from Agmon alone (no assumption on ‖u‖_∞).

  Lean 4 core plus ns/SelfBudget, lib/OField, lib/RingTac. For a run with the stretching step, r_k² = Z_k and the
  Agmon bound X_k⁴ ≤ C Z_k s_k², put A = h c²/(2ν) and, for N steps,
      α_N = (hν)⁻¹ · 2NCM · Z_0,      β_N = (hν)⁻¹ · 2NCM · M A .
  If Z_0 ≤ M(1 − q₁) and for every N ≤ N_max:  α_N ≤ q²  and  A(β_N + q) ≤ q₁  (0 < q, 0 < q₁ < 1), then
      Z_N ≤ M  for every N ≤ N_max                                                     (local_cont)
  The proof closes the loop: Z ≤ M on [0, N) ⇒ (SelfBudget) T² ≤ α + βT ⇒ (quad_root) T ≤ β + q ⇒ the Serrin sum
  A·T ≤ q₁ ⇒ (serrin_run) Z_N(1 − q₁) ≤ Z_0 ≤ M(1 − q₁). Both conditions hold for N h ≲ ν³/(c⁴ C M²): a local
  existence time obtained from the Serrin route, the same order as the Riccati route (RiccatiGen).
-/
import ns.SelfBudget

namespace LocalCont
open DetChain LibRing LibCS LibGron LibOF SerrinDiscrete SerrinSum SerrinRun SerrinAgmon DissBudget SelfBudget

variable {R : Type} (K : CRing R) (P : Pos K)

def alpha (F : OF K P) (h ν C M Z0 : R) (N : Nat) : R :=
  K.mul (F.inv (K.mul h ν)) (K.mul (K.add (K.mul (castN K N) (K.mul C M)) (K.mul (castN K N) (K.mul C M))) Z0)

def beta (F : OF K P) (h c ν C M : R) (N : Nat) : R :=
  K.mul (F.inv (K.mul h ν)) (K.mul (K.add (K.mul (castN K N) (K.mul C M)) (K.mul (castN K N) (K.mul C M)))
    (K.mul M (amp K P F h c ν)))

/-- dividing a nonnegative difference by a positive factor -/
theorem le_of_mul_le (F : OF K P) (x y d : R) (hd : P.nn d) (hd0 : d ≠ K.zero)
    (h : le K P (K.mul d x) (K.mul d y)) : le K P x y := by
  have hi := F.mul_inv _ hd0
  unfold le at h ⊢
  have e : K.mul (F.inv d) (sub K (K.mul d y) (K.mul d x)) = K.mul (sub K y x) (K.mul d (F.inv d)) := by lib_ring
  rw [hi] at e
  rw [show sub K y x = K.mul (sub K y x) K.one by lib_ring, ← e]
  exact P.nn_mul _ _ (nn_inv K P F _ hd hd0) h

/-- **Local continuation.** -/
theorem local_cont (F : OF K P) (Z r s X : Nat → R) (h c ν C M q q1 : R) (hh : P.nn h) (hν : P.nn ν)
    (hν0 : K.add ν ν ≠ K.zero) (hhν : K.mul h ν ≠ K.zero) (hr : ∀ k, K.mul (r k) (r k) = Z k)
    (step : ∀ k, le K P (Z (k + 1))
      (K.add (Z k) (K.mul h (sub K (K.mul (K.mul (K.mul c (X k)) (r k)) (s k)) (K.mul ν (K.mul (s k) (s k)))))))
    (hC : P.nn C) (hM : P.nn M)
    (hA : ∀ k, le K P (K.mul (K.mul (X k) (X k)) (K.mul (X k) (X k))) (K.mul C (K.mul (Z k) (K.mul (s k) (s k)))))
    (hq : P.nn q) (hq0 : q ≠ K.zero) (hq1 : le K P q1 K.one) (hq10 : sub K K.one q1 ≠ K.zero)
    (hM0 : le K P (Z 0) (K.mul M (sub K K.one q1))) (Nmax : Nat)
    (hα : ∀ N, N ≤ Nmax → le K P (alpha K P F h ν C M (Z 0) N) (K.mul q q))
    (hβ : ∀ N, N ≤ Nmax → le K P (K.mul (amp K P F h c ν) (K.add (beta K P F h c ν C M N) q)) q1) :
    ∀ N, N ≤ Nmax → le K P (Z N) M := by
  have hamp : P.nn (amp K P F h c ν) :=
    P.nn_mul _ _ (nn_inv K P F _ (P.nn_add _ _ hν hν) hν0) (P.nn_mul _ _ hh (P.nn_sq c))
  have hhν' : P.nn (K.mul h ν) := P.nn_mul _ _ hh hν
  have hinv := nn_inv K P F _ hhν' hhν
  have hi := F.mul_inv _ hhν
  have hone := nn_one K P
  have ha : ∀ k, P.nn (aRun K P F h c ν X k) := fun k =>
    P.nn_mul _ _ (nn_inv K P F _ (P.nn_add _ _ hν hν) hν0)
      (P.nn_mul _ _ hh (P.nn_mul _ _ (P.nn_sq c) (P.nn_sq (X k))))
  have one_step : ∀ N, N ≤ Nmax → (∀ k, k < N → le K P (Z k) M) → le K P (Z N) M := by
    intro N hN hZ
    have sb := self_budget K P F Z r s X h c ν C M hh hν hν0 hr step hC hM hA N hZ
    have hβnn : P.nn (beta K P F h c ν C M N) :=
      P.nn_mul _ _ hinv (P.nn_mul _ _ (P.nn_add _ _ (P.nn_mul _ _ (nn_castN K P N) (P.nn_mul _ _ hC hM))
        (P.nn_mul _ _ (nn_castN K P N) (P.nn_mul _ _ hC hM))) (P.nn_mul _ _ hM hamp))
    -- divide by hν:  T² ≤ α + β T
    have quad : le K P (K.mul (sumA K (fun k => K.mul (X k) (X k)) N) (sumA K (fun k => K.mul (X k) (X k)) N))
        (K.add (alpha K P F h ν C M (Z 0) N) (K.mul (beta K P F h c ν C M N) (sumA K (fun k => K.mul (X k) (X k)) N))) := by
      apply le_of_mul_le K P F _ _ (K.mul h ν) hhν' hhν
      unfold alpha beta
      generalize sumA K (fun k => K.mul (X k) (X k)) N = T at sb ⊢
      generalize amp K P F h c ν = a at sb ⊢
      generalize castN K N = n at sb ⊢
      have e : K.mul (K.mul h ν) (K.add (K.mul (F.inv (K.mul h ν))
            (K.mul (K.add (K.mul n (K.mul C M)) (K.mul n (K.mul C M))) (Z 0)))
          (K.mul (K.mul (F.inv (K.mul h ν)) (K.mul (K.add (K.mul n (K.mul C M)) (K.mul n (K.mul C M))) (K.mul M a))) T))
          = K.mul (K.mul (K.mul h ν) (F.inv (K.mul h ν)))
            (K.mul (K.add (K.mul n (K.mul C M)) (K.mul n (K.mul C M))) (K.add (Z 0) (K.mul M (K.mul a T)))) := by lib_ring
      rw [e, hi, show K.mul K.one (K.mul (K.add (K.mul n (K.mul C M)) (K.mul n (K.mul C M))) (K.add (Z 0) (K.mul M (K.mul a T))))
        = K.mul (K.add (K.mul n (K.mul C M)) (K.mul n (K.mul C M))) (K.add (Z 0) (K.mul M (K.mul a T))) by lib_ring]
      exact sb
    have Tb := quad_root K P F _ _ _ q hβnn hq hq0 (hα N hN) quad
    -- the Serrin sum: S = A T ≤ A (β + q) ≤ q₁
    have Sb : le K P (sumA K (aRun K P F h c ν X) N) q1 := by
      rw [sum_aRun]
      exact le_trans K P (le_mul_left K P hamp Tb) (hβ N hN)
    have hS : P.nn (sub K K.one (sumA K (aRun K P F h c ν X) N)) := by
      have e : sub K K.one (sumA K (aRun K P F h c ν X) N)
          = K.add (sub K K.one q1) (sub K q1 (sumA K (aRun K P F h c ν X) N)) := by lib_ring
      rw [e]; exact P.nn_add _ _ hq1 Sb
    have hZ0 : P.nn (Z 0) := by rw [← hr 0]; exact P.nn_sq _
    have run := serrin_run K P F Z r s X h c ν hh hν hν0 hr hZ0 step N hS
    have hZN : P.nn (Z N) := by rw [← hr N]; exact P.nn_sq _
    have m := le_mul_left K P hZN (show le K P (sub K K.one q1) (sub K K.one (sumA K (aRun K P F h c ν X) N)) by
      unfold le
      rw [show sub K (sub K K.one (sumA K (aRun K P F h c ν X) N)) (sub K K.one q1)
          = sub K q1 (sumA K (aRun K P F h c ν X) N) by lib_ring]
      exact Sb)
    have t := le_trans K P (le_trans K P m run) hM0
    -- Z_N (1 − q₁) ≤ M (1 − q₁)  ⇒  Z_N ≤ M
    apply le_of_mul_le K P F _ _ (sub K K.one q1) hq1 hq10
    rw [show K.mul (sub K K.one q1) (Z N) = K.mul (Z N) (sub K K.one q1) by lib_ring,
        show K.mul (sub K K.one q1) M = K.mul M (sub K K.one q1) by lib_ring]
    exact t
  have claim : ∀ N, N ≤ Nmax → ∀ k, k ≤ N → le K P (Z k) M := by
    intro N
    induction N with
    | zero =>
      intro _ k hk
      rw [Nat.le_zero.mp hk]
      exact one_step 0 (Nat.zero_le _) (fun k hk => absurd hk (Nat.not_lt_zero k))
    | succ n ih =>
      intro hN k hk
      have ih' := ih (Nat.le_of_succ_le hN)
      cases Nat.lt_or_eq_of_le hk with
      | inl hl => exact ih' k (Nat.le_of_lt_succ hl)
      | inr he =>
        rw [he]
        exact one_step (n + 1) hN (fun j hj => ih' j (Nat.le_of_lt_succ hj))
  exact fun N hN => claim N hN N (Nat.le_refl N)

end LocalCont

#print axioms LocalCont.le_of_mul_le
#print axioms LocalCont.local_cont

ns/LocalContAgmon.lean

lean_all/ns/LocalContAgmon.lean

/-
  ns/LocalContAgmon.lean — T37 cycle 197: the two order frameworks agree, and LocalCont with the Agmon bound proved
  (AgmonOpt) instead of assumed.

  Lean 4 core plus ns/LocalCont, ns/AgmonOpt, lib/RingTac.
    toCone, le_of_Le, Le_of_le   a positivity cone (lib, `Pos`/`le`) is a cone of NSChain (`Cone`/`Le`), and the two
                                 orders coincide — so the Cone-side results (Agmon, AgmonOpt, TriadSum, …) and the
                                 Pos-side results (Gronwall, SerrinRun, LocalCont, …) combine
    agmon_step                   AgmonOpt in the form LocalCont needs: X⁴ ≤ C Z P with C = 2(a² + b²)
    local_cont_agmon             LocalCont whose Agmon hypothesis is replaced by the hypotheses of AgmonOpt at
                                 every step (X_k² ≤ a k_k Z_k + b P_k w_k, (k_k + 1) w_k = 1, k_k² Z_k ≤ P_k ≤
                                 (k_k + 1)² Z_k: the dyadic Agmon split at the cut k_k ≈ √(P_k/Z_k))
-/
import ns.LocalCont
import ns.AgmonOpt

namespace LocalContAgmon
open DetChain LibRing LibCS LibGron LibOF SerrinDiscrete SerrinSum SerrinRun SerrinAgmon DissBudget SelfBudget
  LocalCont

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

def toCone (P : Pos K) : NSChain.Cone K :=
  { nn := P.nn, nn_zero := P.nn_zero, nn_add := P.nn_add, nn_mul := P.nn_mul, nn_sq := P.nn_sq }

theorem le_of_Le (P : Pos K) (x y : R) (h : NSChain.Le K (toCone K P) x y) : le K P x y := by
  cases h with
  | intro d hd =>
    unfold le
    rw [← hd.2, show sub K (K.add x d) x = d by lib_ring]
    exact hd.1

theorem Le_of_le (P : Pos K) (x y : R) (h : le K P x y) : NSChain.Le K (toCone K P) x y :=
  ⟨sub K y x, h, by lib_ring⟩

/-- **AgmonOpt in LocalCont form.** -/
theorem agmon_step (P : Pos K) (X a b k w Z Pp : R) (hZ : P.nn Z) (hP : P.nn Pp)
    (hkw : K.mul (K.add k K.one) w = K.one)
    (h1 : le K P (K.mul (K.mul k k) Z) Pp) (h2 : le K P Pp (K.mul (K.mul (K.add k K.one) (K.add k K.one)) Z))
    (hXb : le K P (K.mul X X) (K.add (K.mul a (K.mul k Z)) (K.mul b (K.mul Pp w)))) :
    le K P (K.mul (K.mul X X) (K.mul X X))
      (K.mul (K.add (K.add (K.mul a a) (K.mul a a)) (K.add (K.mul b b) (K.mul b b))) (K.mul Z Pp)) := by
  have o := AgmonOpt.opt K (toCone K P) X a b k w Z Pp (P.nn_sq X) hZ hP hkw
    (Le_of_le K P _ _ h1) (Le_of_le K P _ _ h2) (Le_of_le K P _ _ hXb)
  have l := le_of_Le K P _ _ o
  unfold le at l ⊢
  have e : sub K (K.mul (K.add (K.add (K.mul a a) (K.mul a a)) (K.add (K.mul b b) (K.mul b b))) (K.mul Z Pp))
        (K.mul (K.mul X X) (K.mul X X))
      = sub K (K.add (K.add (K.mul (K.mul a a) (K.mul Z Pp)) (K.mul (K.mul a a) (K.mul Z Pp)))
             (K.add (K.mul (K.mul b b) (K.mul Z Pp)) (K.mul (K.mul b b) (K.mul Z Pp))))
          (K.mul (SumCS.sq K X) (SumCS.sq K X)) := by unfold SumCS.sq; lib_ring
  rw [e]; exact l

/-- **Local continuation with the Agmon bound proved.** -/
theorem local_cont_agmon (P : Pos K) (F : OF K P) (Z r s X kc w : Nat → R) (h c ν a b M q q1 : R)
    (hh : P.nn h) (hν : P.nn ν)
    (hν0 : K.add ν ν ≠ K.zero) (hhν : K.mul h ν ≠ K.zero) (hr : ∀ k, K.mul (r k) (r k) = Z k)
    (step : ∀ k, le K P (Z (k + 1))
      (K.add (Z k) (K.mul h (sub K (K.mul (K.mul (K.mul c (X k)) (r k)) (s k)) (K.mul ν (K.mul (s k) (s k)))))))
    (hM : P.nn M)
    (hkw : ∀ k, K.mul (K.add (kc k) K.one) (w k) = K.one)
    (h1 : ∀ k, le K P (K.mul (K.mul (kc k) (kc k)) (Z k)) (K.mul (s k) (s k)))
    (h2 : ∀ k, le K P (K.mul (s k) (s k)) (K.mul (K.mul (K.add (kc k) K.one) (K.add (kc k) K.one)) (Z k)))
    (hXb : ∀ k, le K P (K.mul (X k) (X k))
      (K.add (K.mul a (K.mul (kc k) (Z k))) (K.mul b (K.mul (K.mul (s k) (s k)) (w k)))))
    (hq : P.nn q) (hq0 : q ≠ K.zero) (hq1 : le K P q1 K.one) (hq10 : sub K K.one q1 ≠ K.zero)
    (hM0 : le K P (Z 0) (K.mul M (sub K K.one q1))) (Nmax : Nat)
    (hα : ∀ N, N ≤ Nmax → le K P (alpha K P F h ν (K.add (K.add (K.mul a a) (K.mul a a)) (K.add (K.mul b b) (K.mul b b)))
      M (Z 0) N) (K.mul q q))
    (hβ : ∀ N, N ≤ Nmax → le K P (K.mul (amp K P F h c ν)
      (K.add (beta K P F h c ν (K.add (K.add (K.mul a a) (K.mul a a)) (K.add (K.mul b b) (K.mul b b))) M N) q)) q1) :
    ∀ N, N ≤ Nmax → le K P (Z N) M := by
  have hZ : ∀ k, P.nn (Z k) := fun k => by rw [← hr k]; exact P.nn_sq _
  exact local_cont K P F Z r s X h c ν _ M q q1 hh hν hν0 hhν hr step
    (P.nn_add _ _ (P.nn_add _ _ (P.nn_sq a) (P.nn_sq a)) (P.nn_add _ _ (P.nn_sq b) (P.nn_sq b))) hM
    (fun k => agmon_step K P (X k) a b (kc k) (w k) (Z k) (K.mul (s k) (s k)) (hZ k) (P.nn_sq _) (hkw k) (h1 k) (h2 k)
      (hXb k))
    hq hq0 hq1 hq10 hM0 Nmax hα hβ

end LocalContAgmon

#print axioms LocalContAgmon.le_of_Le
#print axioms LocalContAgmon.Le_of_le
#print axioms LocalContAgmon.agmon_step
#print axioms LocalContAgmon.local_cont_agmon

ns/LocalContSobolev.lean

lean_all/ns/LocalContSobolev.lean

/-
  ns/LocalContSobolev.lean — T37 cycle 200: local continuation whose L^∞ input is the Wiener norm split into a low
  and a high part, each bounded by a Sobolev-type sum (Agmon's two halves).

  Lean 4 core plus ns/LocalContAgmon, ns/Agmon, lib/RingTac. With X = X_L + X_H (Σ_low |û| + Σ_high |û| ≥ ‖u‖_∞),
      X_L² ≤ 26k · Z,   X_H² ≤ 26w · P        (Agmon: W_low = 26K, W_high ≤ 26/(K + 1) = 26w)
  Agmon.agmon gives X² ≤ 52kZ + 52Pw, which is the split of AgmonOpt with a = b = 52:
    split_from_halves   X² ≤ 52 k Z + 52 P w
    local_cont_sobolev  local_cont_agmon with the split replaced by the two Sobolev halves at every step;
                        the constant is C = 2(52² + 52²) = 10816.
-/
import ns.LocalContAgmon
import ns.Agmon

namespace LocalContSobolev
open DetChain LibRing LibCS LibGron LibOF SerrinDiscrete SerrinSum SerrinRun SerrinAgmon DissBudget SelfBudget
  LocalCont LocalContAgmon

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

def c26 : R := castN K 26
def c52 : R := castN K 52

theorem c52_eq : c52 K = K.add (c26 K) (c26 K) := by
  unfold c52 c26
  show castN K 52 = K.add (castN K 26) (castN K 26)
  -- castN is unary; 52 = 26 + 26 by induction on the second summand
  have h : ∀ a b : Nat, castN K (a + b) = K.add (castN K a) (castN K b) := by
    intro a b
    induction b with
    | zero => show castN K a = K.add (castN K a) K.zero; lib_ring
    | succ b ih =>
      show K.add K.one (castN K (a + b)) = K.add (castN K a) (K.add K.one (castN K b))
      rw [ih]; lib_ring
  exact h 26 26

/-- **The split from Agmon's two halves.** -/
theorem split_from_halves (P : Pos K) (XL XH k w Z Pp : R)
    (hL : le K P (K.mul XL XL) (K.mul (K.mul (c26 K) k) Z))
    (hH : le K P (K.mul XH XH) (K.mul (K.mul (c26 K) w) Pp)) :
    le K P (K.mul (K.add XL XH) (K.add XL XH))
      (K.add (K.mul (c52 K) (K.mul k Z)) (K.mul (c52 K) (K.mul Pp w))) := by
  have a := Agmon.agmon K (toCone K P) XL XH (K.mul (c26 K) k) (K.mul (c26 K) w) Z Pp
    (Le_of_le K P _ _ (by show le K P (K.mul XL XL) _; exact hL))
    (Le_of_le K P _ _ (by show le K P (K.mul XH XH) _; exact hH))
  have l := le_of_Le K P _ _ a
  unfold le at l ⊢
  rw [c52_eq]
  have e : sub K (K.add (K.mul (K.add (c26 K) (c26 K)) (K.mul k Z)) (K.mul (K.add (c26 K) (c26 K)) (K.mul Pp w)))
        (K.mul (K.add XL XH) (K.add XL XH))
      = sub K (K.add (K.add (K.mul (K.mul (c26 K) k) Z) (K.mul (K.mul (c26 K) k) Z))
          (K.add (K.mul (K.mul (c26 K) w) Pp) (K.mul (K.mul (c26 K) w) Pp)))
          (SumCS.sq K (K.add XL XH)) := by unfold SumCS.sq; lib_ring
  rw [e]; exact l

/-- **Local continuation from the Sobolev halves.** -/
theorem local_cont_sobolev (P : Pos K) (F : OF K P) (Z r s XL XH kc w : Nat → R) (h c ν M q q1 : R)
    (hh : P.nn h) (hν : P.nn ν)
    (hν0 : K.add ν ν ≠ K.zero) (hhν : K.mul h ν ≠ K.zero) (hr : ∀ k, K.mul (r k) (r k) = Z k)
    (step : ∀ k, le K P (Z (k + 1))
      (K.add (Z k) (K.mul h (sub K (K.mul (K.mul (K.mul c (K.add (XL k) (XH k))) (r k)) (s k))
        (K.mul ν (K.mul (s k) (s k)))))))
    (hM : P.nn M)
    (hkw : ∀ k, K.mul (K.add (kc k) K.one) (w k) = K.one)
    (h1 : ∀ k, le K P (K.mul (K.mul (kc k) (kc k)) (Z k)) (K.mul (s k) (s k)))
    (h2 : ∀ k, le K P (K.mul (s k) (s k)) (K.mul (K.mul (K.add (kc k) K.one) (K.add (kc k) K.one)) (Z k)))
    (hL : ∀ k, le K P (K.mul (XL k) (XL k)) (K.mul (K.mul (c26 K) (kc k)) (Z k)))
    (hH : ∀ k, le K P (K.mul (XH k) (XH k)) (K.mul (K.mul (c26 K) (w k)) (K.mul (s k) (s k))))
    (hq : P.nn q) (hq0 : q ≠ K.zero) (hq1 : le K P q1 K.one) (hq10 : sub K K.one q1 ≠ K.zero)
    (hM0 : le K P (Z 0) (K.mul M (sub K K.one q1))) (Nmax : Nat)
    (hα : ∀ N, N ≤ Nmax → le K P (alpha K P F h ν
      (K.add (K.add (K.mul (c52 K) (c52 K)) (K.mul (c52 K) (c52 K))) (K.add (K.mul (c52 K) (c52 K)) (K.mul (c52 K) (c52 K))))
      M (Z 0) N) (K.mul q q))
    (hβ : ∀ N, N ≤ Nmax → le K P (K.mul (amp K P F h c ν)
      (K.add (beta K P F h c ν
        (K.add (K.add (K.mul (c52 K) (c52 K)) (K.mul (c52 K) (c52 K))) (K.add (K.mul (c52 K) (c52 K)) (K.mul (c52 K) (c52 K))))
        M N) q)) q1) :
    ∀ N, N ≤ Nmax → le K P (Z N) M :=
  local_cont_agmon K P F Z r s (fun k => K.add (XL k) (XH k)) kc w h c ν (c52 K) (c52 K) M q q1 hh hν hν0 hhν hr
    step hM hkw h1 h2
    (fun k => split_from_halves K P (XL k) (XH k) (kc k) (w k) (Z k) (K.mul (s k) (s k)) (hL k) (hH k))
    hq hq0 hq1 hq10 hM0 Nmax hα hβ

end LocalContSobolev

#print axioms LocalContSobolev.c52_eq
#print axioms LocalContSobolev.split_from_halves
#print axioms LocalContSobolev.local_cont_sobolev

ns/LocalDefect.lean

lean_all/ns/LocalDefect.lean

/-
  ns/LocalDefect.lean — T37 cycle 1127 (goal G717e, piece 2): the local defect in the Qe norm, from the scheme
  relations. One step of size 2k (a₀ → a₁, force N₀) against two steps of size k (a₀ → b₁ → b₂, forces N₀, N₁), over
  an ordered ring without zero divisors. If k ≤ 1, k⁴ν⁴B₅(a₀) ≤ k³V, Σw|N₀ − N₁|² ≤ kF, and Σw²|N₁|², Σw⁴|a₀|²,
  Σw²|N₀ − N₁|², Σw³|N₁|² ≤ E, then
    local_defect   Qe(a₁, b₂) = νZ(a₁ − b₂) + kν²A(a₁ − b₂) ≤ k³·3(ν(V + F + νE) + ν²(ν²E + E + νE)),
    local_defect'  the same with k·Σw³|N₁|² ≤ E (uniform in the step) and without k ≤ 1
  the hypothesis hΔ of StepCauchy.step_cauchy (DefectSum.defect_sum4, defect_sumV with V = w², DefectOrder.defect_qe).

  Lean 4 core plus ns/DefectSum, ns/DefectOrder, ns/StepCauchy (Qe).
-/
import ns.DefectSum
import ns.DefectOrder
import ns.StepCauchy

namespace LocalDefect
open DetChain NSChain DyadicSmallGlobal LibRing DyadicLarge FourierGalerkin ForceBound EnstStep EnstDiff DefectA0
  DefectSum DefectOrder StepCauchy ForceDiff GalerkinForce ModeCount CubeCount ForceUniform NormCompare EnstGalerkin

section Gen
variable {R : Type} (K : CRing R) (O : OrdR K) {I : Type}

theorem local_defect (l : List I) (kz : I → Mode) (k ν V F E : R) (hk : O.nn k) (hk1 : Le K O.toCone k K.one)
    (hν : O.nn ν) (hE : O.nn E) (h10 : K.one ≠ K.zero) (a0 a1 b1 b2 N0 N1 : I → V3 R)
    (s1 : ∀ r, Step K (K.add k k) ν (wt K kz r) (a1 r) (a0 r) (N0 r)) (s2 : ∀ r, Step K k ν (wt K kz r) (b1 r) (a0 r) (N0 r))
    (s3 : ∀ r, Step K k ν (wt K kz r) (b2 r) (b1 r) (N1 r))
    (hV : Le K O.toCone (K.mul (K.mul (K.mul (K.mul k k) (K.mul k k)) (K.mul (K.mul ν ν) (K.mul ν ν)))
      (lsum K (fun r => K.mul (K.mul (K.mul (K.mul (wt K kz r) (wt K kz r)) (K.mul (wt K kz r) (wt K kz r))) (wt K kz r)) (dot3 K (a0 r) (a0 r))) l))
      (K.mul (K.mul (K.mul k k) k) V))
    (hF : Le K O.toCone (lsum K (fun r => K.mul (wt K kz r) (dot3 K (sub3 K (N0 r) (N1 r)) (sub3 K (N0 r) (N1 r)))) l) (K.mul k F))
    (hH : Le K O.toCone (lsum K (fun r => K.mul (K.mul (wt K kz r) (wt K kz r)) (dot3 K (N1 r) (N1 r))) l) E)
    (hB4 : Le K O.toCone (lsum K (fun r => K.mul (K.mul (K.mul (wt K kz r) (wt K kz r)) (K.mul (wt K kz r) (wt K kz r))) (dot3 K (a0 r) (a0 r))) l) E)
    (hF2 : Le K O.toCone (lsum K (fun r => K.mul (K.mul (wt K kz r) (wt K kz r)) (dot3 K (sub3 K (N0 r) (N1 r)) (sub3 K (N0 r) (N1 r)))) l) E)
    (hH3 : Le K O.toCone (lsum K (fun r => K.mul (K.mul (K.mul (wt K kz r) (wt K kz r)) (wt K kz r)) (dot3 K (N1 r) (N1 r))) l) E) :
    Le K O.toCone (Qe K ν k l kz a1 b2)
      (K.mul (K.mul (K.mul k k) k) (K.mul (k3 K) (K.add (K.mul ν (K.add (K.add V F) (K.mul ν E)))
        (K.mul (K.mul ν ν) (K.add (K.add (K.mul (K.mul ν ν) E) E) (K.mul ν E)))))) := by
  have hw : ∀ r, O.nn (wt K kz r) := fun r => dot3_nn K O.toCone _
  have dz := defect_sum4 K O l k ν hk hν (wt K kz) hw h10 a0 a1 b1 b2 N0 N1 s1 s2 s3
  have da := defect_sumV K O l k ν hk hν (wt K kz) (fun r => K.mul (wt K kz r) (wt K kz r)) hw (fun r => O.nn_sq _) h10
    a0 a1 b1 b2 N0 N1 s1 s2 s3
  have q := defect_qe K O k ν _ _ _ _ _ _ _ _ V F E hk hk1 hν hE dz da hV hF hH hB4 hF2 hH3
  exact q

theorem local_defect' (l : List I) (kz : I → Mode) (k ν V F E : R) (hk : O.nn k)
    (hν : O.nn ν) (h10 : K.one ≠ K.zero) (a0 a1 b1 b2 N0 N1 : I → V3 R)
    (s1 : ∀ r, Step K (K.add k k) ν (wt K kz r) (a1 r) (a0 r) (N0 r)) (s2 : ∀ r, Step K k ν (wt K kz r) (b1 r) (a0 r) (N0 r))
    (s3 : ∀ r, Step K k ν (wt K kz r) (b2 r) (b1 r) (N1 r))
    (hV : Le K O.toCone (K.mul (K.mul (K.mul (K.mul k k) (K.mul k k)) (K.mul (K.mul ν ν) (K.mul ν ν)))
      (lsum K (fun r => K.mul (K.mul (K.mul (K.mul (wt K kz r) (wt K kz r)) (K.mul (wt K kz r) (wt K kz r))) (wt K kz r)) (dot3 K (a0 r) (a0 r))) l))
      (K.mul (K.mul (K.mul k k) k) V))
    (hF : Le K O.toCone (lsum K (fun r => K.mul (wt K kz r) (dot3 K (sub3 K (N0 r) (N1 r)) (sub3 K (N0 r) (N1 r)))) l) (K.mul k F))
    (hH : Le K O.toCone (lsum K (fun r => K.mul (K.mul (wt K kz r) (wt K kz r)) (dot3 K (N1 r) (N1 r))) l) E)
    (hB4 : Le K O.toCone (lsum K (fun r => K.mul (K.mul (K.mul (wt K kz r) (wt K kz r)) (K.mul (wt K kz r) (wt K kz r))) (dot3 K (a0 r) (a0 r))) l) E)
    (hF2 : Le K O.toCone (lsum K (fun r => K.mul (K.mul (wt K kz r) (wt K kz r)) (dot3 K (sub3 K (N0 r) (N1 r)) (sub3 K (N0 r) (N1 r)))) l) E)
    (hH3 : Le K O.toCone (K.mul k (lsum K (fun r => K.mul (K.mul (K.mul (wt K kz r) (wt K kz r)) (wt K kz r)) (dot3 K (N1 r) (N1 r))) l)) E) :
    Le K O.toCone (Qe K ν k l kz a1 b2)
      (K.mul (K.mul (K.mul k k) k) (K.mul (k3 K) (K.add (K.mul ν (K.add (K.add V F) (K.mul ν E)))
        (K.mul (K.mul ν ν) (K.add (K.add (K.mul (K.mul ν ν) E) E) (K.mul ν E)))))) := by
  have hw : ∀ r, O.nn (wt K kz r) := fun r => dot3_nn K O.toCone _
  have dz := defect_sum4 K O l k ν hk hν (wt K kz) hw h10 a0 a1 b1 b2 N0 N1 s1 s2 s3
  have da := defect_sumV K O l k ν hk hν (wt K kz) (fun r => K.mul (wt K kz r) (wt K kz r)) hw (fun r => O.nn_sq _) h10
    a0 a1 b1 b2 N0 N1 s1 s2 s3
  have q := defect_qe' K O k ν _ _ _ _ _ _ _ _ V F E hk hν dz da hV hF hH hB4 hF2 hH3
  exact q

end Gen

end LocalDefect

#print axioms LocalDefect.local_defect
#print axioms LocalDefect.local_defect'

ns/LocalDiss.lean

lean_all/ns/LocalDiss.lean

/-
  ns/LocalDiss.lean — T37 cycle 1357 (goal G805a): the dissipated H² mass on the large-data local window, abstract
  form, over an ordered field. Hypotheses of EnstLocal.local_bound with the cutoff choice tightened to
  2c₂Zb ≤ ν²; D = h·c₁·Zb². Then for every t ≤ n
    local_diss   2Q_t + hν²·Σ_{j<t} A_j ≤ 2Q_0 + 2t·D,
  so on the window n·h ≤ (νZb − Q_0)/(c₁Zb²) the time-integrated H² norm is bounded by constants independent of the
  truncation (no smallness of the data).

  Lean 4 core plus ns/EnstLocal (local_bound, sq_mono), ns/EnstGlobal (le_of_mul_le).
-/
import ns.EnstLocal

namespace LocalDiss
open DetChain NSChain DyadicSmallGlobal LibRing DyadicLarge EnstGlobal EnstLocal

section Gen
variable {R : Type} (K : CRing R) (O : OrdR K)

theorem local_diss (ν h c1 c2 Zb : R) (Z A : Nat → R) (hν : O.nn ν) (hν0 : ν ≠ K.zero) (hh : O.nn h)
    (hc1 : O.nn c1) (hc2 : O.nn c2) (hZb : O.nn Zb) (hZ : ∀ t, O.nn (Z t)) (hA : ∀ t, O.nn (A t))
    (step : ∀ t, Le K O.toCone (Q K ν h Z A (t + 1))
      (K.add (K.mul ν (Z t)) (K.mul h (K.add (K.mul c1 (K.mul (Z t) (Z t))) (K.mul c2 (K.mul (A t) (Z t)))))))
    (choice2 : Le K O.toCone (K.mul (K.add c2 c2) Zb) (K.mul ν ν)) (n : Nat)
    (budget : Le K O.toCone (K.add (Q K ν h Z A 0) (K.mul (castN K n) (K.mul h (K.mul c1 (K.mul Zb Zb))))) (K.mul ν Zb)) :
    ∀ t, t ≤ n → Le K O.toCone
      (K.add (K.mul (K.add K.one K.one) (Q K ν h Z A t)) (K.mul (K.mul h (K.mul ν ν)) (sumN K A t)))
      (K.add (K.mul (K.add K.one K.one) (Q K ν h Z A 0))
        (K.mul (K.add K.one K.one) (K.mul (castN K t) (K.mul h (K.mul c1 (K.mul Zb Zb))))))
  | 0, _ => ⟨K.zero, O.nn_zero, by
      show K.add (K.add (K.mul (K.add K.one K.one) (Q K ν h Z A 0)) (K.mul (K.mul h (K.mul ν ν)) K.zero)) K.zero
        = K.add (K.mul (K.add K.one K.one) (Q K ν h Z A 0)) (K.mul (K.add K.one K.one) (K.mul K.zero (K.mul h (K.mul c1 (K.mul Zb Zb)))))
      lib_ring⟩
  | t + 1, ht => by
    have ih := local_diss ν h c1 c2 Zb Z A hν hν0 hh hc1 hc2 hZb hZ hA step choice2 n budget t (Nat.le_of_succ_le ht)
    have choice : Le K O.toCone (K.mul c2 Zb) (K.mul ν ν) :=
      le_trans' K O.toCone _ _ _ ⟨K.mul c2 Zb, O.nn_mul _ _ hc2 hZb, by lib_ring⟩ choice2
    have lb := local_bound K O ν h c1 c2 Zb Z A hν hν0 hh hc1 hc2 hZb hZ hA step choice n budget t (Nat.le_of_succ_le ht)
    have hD : O.nn (K.mul h (K.mul c1 (K.mul Zb Zb))) := O.nn_mul _ _ hh (O.nn_mul _ _ hc1 (O.nn_sq Zb))
    have tn := ForceSob.le_mul_right' K O.toCone (RatModel.cast_mono K O.toCone t n (Nat.le_of_succ_le ht)) hD
    have q1 := le_trans' K O.toCone _ _ _ lb (le_trans' K O.toCone _ _ _ (le_add' K O.toCone _ _ _ _ (le_refl' K O.toCone _) tn) budget)
    have hz : Le K O.toCone (K.mul ν (Z t)) (Q K ν h Z A t) :=
      ⟨K.mul (K.mul h (K.mul ν ν)) (A t), O.nn_mul _ _ (O.nn_mul _ _ hh (O.nn_sq ν)) (hA t), rfl⟩
    have zt : Le K O.toCone (Z t) Zb := le_of_mul_le K O ν _ _ hν hν0 (le_trans' K O.toCone _ _ _ hz q1)
    -- 2c₁Z² ≤ 2c₁Zb²,  2c₂AZ ≤ ν²A
    have s1 := le_mul_nn K O.toCone c1 _ _ hc1 (sq_mono K O _ _ (hZ t) hZb zt)
    have c2z := le_trans' K O.toCone _ _ _ (le_mul_nn K O.toCone (K.add c2 c2) _ _ (O.nn_add _ _ hc2 hc2) zt) choice2
    have s2 := le_mul_nn K O.toCone (A t) _ _ (hA t) c2z
    have two : O.nn (K.add K.one K.one) := O.nn_add _ _ (RatModel.nn_one' K O.toCone) (RatModel.nn_one' K O.toCone)
    have st2 := le_mul_nn K O.toCone (K.add K.one K.one) _ _ two (step t)
    -- 2Q_{t+1} + hν²A_t ≤ 2Q_t + 2D
    have one : Le K O.toCone
        (K.add (K.mul (K.add K.one K.one) (Q K ν h Z A (t + 1))) (K.mul (K.mul h (K.mul ν ν)) (A t)))
        (K.add (K.mul (K.add K.one K.one) (Q K ν h Z A t)) (K.mul (K.add K.one K.one) (K.mul h (K.mul c1 (K.mul Zb Zb))))) := by
      have a1 := le_add' K O.toCone _ _ _ _ st2 (le_refl' K O.toCone (K.mul (K.mul h (K.mul ν ν)) (A t)))
      have a2 := le_mul_nn K O.toCone (K.add h h) _ _ (O.nn_add _ _ hh hh) s1
      have a3 := le_mul_nn K O.toCone h _ _ hh s2
      refine le_trans' K O.toCone _ _ _ a1 (le_trans' K O.toCone _ _ _ ⟨K.zero, O.nn_zero, ?_⟩
        (le_trans' K O.toCone _ _ _ (le_add' K O.toCone _ _ _ _ (le_add' K O.toCone _ _ _ _ a2 a3)
          (le_refl' K O.toCone (K.add (K.add (K.mul ν (Z t)) (K.mul ν (Z t))) (K.mul (K.mul h (K.mul ν ν)) (A t))))) ⟨K.zero, O.nn_zero, ?_⟩))
      · show K.add (K.add (K.mul (K.add K.one K.one) (K.add (K.mul ν (Z t))
            (K.mul h (K.add (K.mul c1 (K.mul (Z t) (Z t))) (K.mul c2 (K.mul (A t) (Z t)))))))
            (K.mul (K.mul h (K.mul ν ν)) (A t))) K.zero
          = K.add (K.add (K.mul (K.add h h) (K.mul c1 (K.mul (Z t) (Z t)))) (K.mul h (K.mul (A t) (K.mul (K.add c2 c2) (Z t)))))
              (K.add (K.add (K.mul ν (Z t)) (K.mul ν (Z t))) (K.mul (K.mul h (K.mul ν ν)) (A t)))
        lib_ring
      · show K.add (K.add (K.add (K.mul (K.add h h) (K.mul c1 (K.mul Zb Zb))) (K.mul h (K.mul (A t) (K.mul ν ν))))
            (K.add (K.add (K.mul ν (Z t)) (K.mul ν (Z t))) (K.mul (K.mul h (K.mul ν ν)) (A t)))) K.zero
          = K.add (K.mul (K.add K.one K.one) (K.add (K.mul ν (Z t)) (K.mul (K.mul h (K.mul ν ν)) (A t))))
              (K.mul (K.add K.one K.one) (K.mul h (K.mul c1 (K.mul Zb Zb))))
        lib_ring
    have s := le_add' K O.toCone _ _ _ _ one (le_refl' K O.toCone (K.mul (K.mul h (K.mul ν ν)) (sumN K A t)))
    have ih' := le_add' K O.toCone _ _ _ _ ih (le_refl' K O.toCone (K.mul (K.add K.one K.one) (K.mul h (K.mul c1 (K.mul Zb Zb)))))
    refine le_trans' K O.toCone _ _ _ ⟨K.zero, O.nn_zero, ?_⟩ (le_trans' K O.toCone _ _ _ s
      (le_trans' K O.toCone _ _ _ ⟨K.zero, O.nn_zero, ?_⟩ (le_trans' K O.toCone _ _ _ ih' ⟨K.zero, O.nn_zero, ?_⟩)))
    · show K.add (K.add (K.mul (K.add K.one K.one) (Q K ν h Z A (t + 1))) (K.mul (K.mul h (K.mul ν ν)) (K.add (sumN K A t) (A t)))) K.zero = _
      generalize Q K ν h Z A (t + 1) = q1'
      generalize Q K ν h Z A t = q0'
      lib_ring
    · generalize Q K ν h Z A t = q0'
      lib_ring
    · show K.add (K.add (K.add (K.mul (K.add K.one K.one) (Q K ν h Z A 0))
          (K.mul (K.add K.one K.one) (K.mul (castN K t) (K.mul h (K.mul c1 (K.mul Zb Zb))))))
          (K.mul (K.add K.one K.one) (K.mul h (K.mul c1 (K.mul Zb Zb))))) K.zero
        = K.add (K.mul (K.add K.one K.one) (Q K ν h Z A 0))
          (K.mul (K.add K.one K.one) (K.mul (K.add K.one (castN K t)) (K.mul h (K.mul c1 (K.mul Zb Zb)))))
      generalize Q K ν h Z A 0 = q00
      generalize castN K t = ct
      lib_ring

end Gen

end LocalDiss

#print axioms LocalDiss.local_diss

ns/LocalHolder.lean

lean_all/ns/LocalHolder.lean

/-
  ns/LocalHolder.lean — T37 cycle 1359 (goal G805b): Hölder-1/2 equicontinuity on the large-data local window, over
  an ordered field. Hypotheses of EnstLocalGalerkin.local_galerkin with the cutoff choice tightened to
  2c₂Zb ≤ ν² (c₁ = cc(ι, C1), c₂ = cc(ι, C2)) and the budget Q_0 + n·h·c₁Zb² ≤ νZb; c = 135ι². For t + k ≤ n:
    local_step     the local enstrophy step νZ' + hν²A' ≤ νZ + h(c₁Z² + c₂AZ) as a lemma
    sum_le_upto    termwise bounds below k give the bound on the partial sum
    sumN_split     Σ_{j<t+k} A_j = Σ_{j<t} A_j + Σ_{j<k} A_{t+j}
    local_holder   ν²·Σ_r |a_{t+k,r} − a_{t,r}|² ≤ k·(4hν(cZb + ν²)Zb)
  no smallness of the data, constants independent of the truncation.

  Lean 4 core plus ns/LocalDiss, ns/EnstLocalGalerkin, ns/TimeLip, ns/TimeLipN.
-/
import ns.LocalDiss
import ns.EnstLocalGalerkin
import ns.TimeLip
import ns.TimeLipN

namespace LocalHolder
open DetChain NSChain DyadicSmallGlobal LibRing DyadicLarge FourierGalerkin GalerkinForce ForceBound ForceSob
  ModeCount CubeCount ForceUniform NormCompare EnstStep EnstGlobal EnstGalerkin ForceTwoScale EnstLocal
  EnstLocalGalerkin LatticeSum

section Gen
variable {R : Type} (K : CRing R) (O : OrdR K) {I : Type}
variable (inv : R → R) (mul_inv : ∀ y, y ≠ K.zero → K.mul y (inv y) = K.one) (h10 : K.one ≠ K.zero)

include mul_inv h10 in
theorem local_step (l : List I) (kz : I → Mode) (hd : List.Pairwise (fun a b => kz a ≠ kz b) l)
    (h1 : ∀ p, 1 ≤ nrm (kz p)) (M : Nat) (hM : ∀ p, p ∈ l → nrm (kz p) ≤ M + 1) (Rc t0 : Nat) (hRM : Rc ≤ M)
    (ι h ν : R) (hh : O.nn h) (hν : O.nn ν) (a N : Nat → I → V3 R)
    (hN : ∀ t, Le K O.toCone (lsum K (fun r => dot3 K (N t r) (N t r)) l)
      (lsum K (fun r => dot3 K (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (a t) (a t) r)
        (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (a t) (a t) r)) l))
    (hs : ∀ t r, Step K h ν (wt K kz r) (a (t + 1) r) (a t r) (N t r)) (s : Nat) :
    Le K O.toCone (Q K ν h (Zs K l kz a) (As K l kz a) (s + 1))
      (K.add (K.mul ν (Zs K l kz a s)) (K.mul h (K.add (K.mul (cc K ι (C1 K inv Rc)) (K.mul (Zs K l kz a s) (Zs K l kz a s)))
        (K.mul (cc K ι (C2 K inv Rc t0)) (K.mul (As K l kz a s) (Zs K l kz a s)))))) := by
  have hw : ∀ r, O.nn (wt K kz r) := fun r => dot3_nn K O.toCone _
  have st := enst_step K O.toCone l h ν hh hν (wt K kz) hw (a (s + 1)) (a s) (N s) (hs s)
  have ts := two_scale K O inv mul_inv h10 l kz hd h1 M hM Rc t0 hRM ι (a s) (a s)
  have hb := le_mul_nn K O.toCone h _ _ hh (le_trans' K O.toCone _ _ _ (hN s) ts)
  have st2 := le_trans' K O.toCone _ _ _ st (le_add' K O.toCone _ _ _ _ (le_refl' K O.toCone _) hb)
  refine le_trans' K O.toCone _ _ _ st2 ⟨K.zero, O.nn_zero, ?_⟩
  show K.add (K.add (K.mul ν (Zs K l kz a s)) (K.mul h (K.add
      (K.mul (K.add K.one K.one) (K.mul (K.mul ι ι) (K.mul (Zs K l kz a s) (K.mul (C1 K inv Rc) (Zs K l kz a s)))))
      (K.mul (K.add K.one K.one) (K.mul (K.mul ι ι) (K.mul (As K l kz a s) (K.mul (C2 K inv Rc t0) (Zs K l kz a s)))))))) K.zero
    = K.add (K.mul ν (Zs K l kz a s)) (K.mul h (K.add (K.mul (K.mul (K.mul (K.add K.one K.one) (K.mul ι ι)) (C1 K inv Rc))
        (K.mul (Zs K l kz a s) (Zs K l kz a s)))
      (K.mul (K.mul (K.mul (K.add K.one K.one) (K.mul ι ι)) (C2 K inv Rc t0)) (K.mul (As K l kz a s) (Zs K l kz a s)))))
  lib_ring

theorem sum_le_upto (f g : Nat → R) : ∀ k, (∀ j, j < k → Le K O.toCone (f j) (g j)) →
    Le K O.toCone (sumN K f k) (sumN K g k)
  | 0, _ => le_refl' K O.toCone _
  | k + 1, h => le_add' K O.toCone _ _ _ _ (sum_le_upto f g k (fun j hj => h j (Nat.lt_succ_of_lt hj))) (h k (Nat.lt_succ_self k))

theorem sumN_split (A : Nat → R) (t : Nat) : ∀ k, sumN K A (t + k) = K.add (sumN K A t) (sumN K (fun j => A (t + j)) k)
  | 0 => by show sumN K A t = K.add (sumN K A t) K.zero; lib_ring
  | k + 1 => by
    show K.add (sumN K A (t + k)) (A (t + k)) = K.add (sumN K A t) (K.add (sumN K (fun j => A (t + j)) k) (A (t + k)))
    rw [sumN_split A t k]; lib_ring

include mul_inv h10 in
theorem local_holder (l : List I) (kz : I → Mode) (hd : List.Pairwise (fun a b => kz a ≠ kz b) l)
    (h1 : ∀ p, 1 ≤ nrm (kz p)) (M : Nat) (hM : ∀ p, p ∈ l → nrm (kz p) ≤ M + 1) (Rc t0 : Nat) (hRM : Rc ≤ M)
    (ι h ν Zb : R) (hh : O.nn h) (hν : O.nn ν) (hν0 : ν ≠ K.zero) (hZb : O.nn Zb)
    (a N : Nat → I → V3 R)
    (hN : ∀ t, Le K O.toCone (lsum K (fun r => dot3 K (N t r) (N t r)) l)
      (lsum K (fun r => dot3 K (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (a t) (a t) r)
        (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (a t) (a t) r)) l))
    (hs : ∀ t r, Step K h ν (wt K kz r) (a (t + 1) r) (a t r) (N t r))
    (choice2 : Le K O.toCone (K.mul (K.add (cc K ι (C2 K inv Rc t0)) (cc K ι (C2 K inv Rc t0))) Zb) (K.mul ν ν)) (n : Nat)
    (budget : Le K O.toCone (K.add (Q K ν h (Zs K l kz a) (As K l kz a) 0)
      (K.mul (castN K n) (K.mul h (K.mul (cc K ι (C1 K inv Rc)) (K.mul Zb Zb))))) (K.mul ν Zb))
    (t k : Nat) (htk : t + k ≤ n) :
    Le K O.toCone
      (K.mul (K.mul ν ν) (lsum K (fun r => dot3 K (EnstDiff.sub3 K (a (t + k) r) (a t r)) (EnstDiff.sub3 K (a (t + k) r) (a t r))) l))
      (K.mul (castN K k) (K.mul (K.mul (K.add (K.add K.one K.one) (K.add K.one K.one)) (K.mul h ν))
        (K.mul (K.add (K.mul (K.mul (castN K 135) (K.mul ι ι)) Zb) (K.mul ν ν)) Zb))) := by
  have hw : ∀ r, O.nn (wt K kz r) := fun r => dot3_nn K O.toCone _
  have hZ : ∀ t, O.nn (Zs K l kz a t) := fun t => lsum_nn K O.toCone _ (fun r => O.nn_mul _ _ (hw r) (dot3_nn K O.toCone _)) l
  have hA : ∀ t, O.nn (As K l kz a t) := fun t => lsum_nn K O.toCone _ (fun r => O.nn_mul _ _ (O.nn_sq _) (dot3_nn K O.toCone _)) l
  have hiv : O.nn (iv K inv (Rc + 1)) := iv_nn K O inv mul_inv h10 Rc
  have two : O.nn (K.add K.one K.one) := O.nn_add _ _ (RatModel.nn_one' K O.toCone) (RatModel.nn_one' K O.toCone)
  have hc1 : O.nn (cc K ι (C1 K inv Rc)) := O.nn_mul _ _ (O.nn_mul _ _ two (O.nn_sq ι))
    (O.nn_add _ _ (O.nn_mul _ _ (RatModel.nn_cast K O.toCone _) (RatModel.nn_one' K O.toCone))
      (O.nn_mul _ _ (RatModel.nn_cast K O.toCone _) hiv))
  have hc2 : O.nn (cc K ι (C2 K inv Rc t0)) := O.nn_mul _ _ (O.nn_mul _ _ two (O.nn_sq ι))
    (O.nn_add _ _ (O.nn_mul _ _ (RatModel.nn_cast K O.toCone _) (O.nn_sq _))
      (O.nn_mul _ _ (RatModel.nn_cast K O.toCone _) hiv))
  have hc : O.nn (K.mul (castN K 135) (K.mul ι ι)) := O.nn_mul _ _ (RatModel.nn_cast K O.toCone 135) (O.nn_sq ι)
  have choice : Le K O.toCone (K.mul (cc K ι (C2 K inv Rc t0)) Zb) (K.mul ν ν) :=
    le_trans' K O.toCone _ _ _ ⟨K.mul (cc K ι (C2 K inv Rc t0)) Zb, O.nn_mul _ _ hc2 hZb, by lib_ring⟩ choice2
  have stp := local_step K O inv mul_inv h10 l kz hd h1 M hM Rc t0 hRM ι h ν hh hν a N hN hs
  have hD : O.nn (K.mul h (K.mul (cc K ι (C1 K inv Rc)) (K.mul Zb Zb))) := O.nn_mul _ _ hh (O.nn_mul _ _ hc1 (O.nn_sq Zb))
  -- Z_s ≤ Zb for s ≤ n
  have zs : ∀ s, s ≤ n → Le K O.toCone (Zs K l kz a s) Zb := by
    intro s hsn
    have lb := local_bound K O ν h _ _ Zb (Zs K l kz a) (As K l kz a) hν hν0 hh hc1 hc2 hZb hZ hA stp choice n budget s hsn
    have tn := ForceSob.le_mul_right' K O.toCone (RatModel.cast_mono K O.toCone s n hsn) hD
    have q1 := le_trans' K O.toCone _ _ _ lb (le_trans' K O.toCone _ _ _ (le_add' K O.toCone _ _ _ _ (le_refl' K O.toCone _) tn) budget)
    have hz : Le K O.toCone (K.mul ν (Zs K l kz a s)) (Q K ν h (Zs K l kz a) (As K l kz a) s) :=
      ⟨K.mul (K.mul h (K.mul ν ν)) (As K l kz a s), O.nn_mul _ _ (O.nn_mul _ _ hh (O.nn_sq ν)) (hA s), rfl⟩
    exact le_of_mul_le K O ν _ _ hν hν0 (le_trans' K O.toCone _ _ _ hz q1)
  -- one step: D_s ≤ 2h²(cZb + ν²)A_s
  have stepD : ∀ s, s ≤ n → Le K O.toCone
      (lsum K (fun r => dot3 K (EnstDiff.sub3 K (a (s + 1) r) (a s r)) (EnstDiff.sub3 K (a (s + 1) r) (a s r))) l)
      (K.mul (K.mul (K.add K.one K.one) (K.mul h h)) (K.mul (K.add (K.mul (K.mul (castN K 135) (K.mul ι ι)) Zb) (K.mul ν ν)) (As K l kz a s))) := by
    intro s hsn
    have tl := TimeLip.time_lip K O.toCone l h ν hh hν (wt K kz) hw (a (s + 1)) (a s) (N s) (hs s)
    have fu := force_uniform K O inv mul_inv h10 l kz hd h1 M hM ι (kv K kz) (a s) (a s)
    have hH : Le K O.toCone (lsum K (fun p => K.mul (K.mul (mu K kz p) (mu K kz p)) (dot3 K (a s p) (a s p))) l) (As K l kz a s) :=
      lsum_le K O.toCone _ _ (fun p => le_mul_right' K O.toCone (nrm4_le K O.toCone (kz p)) (dot3_nn K O.toCone _)) l
    have m1 := le_mul_right' K O.toCone hH (O.nn_mul _ _ (RatModel.nn_cast K O.toCone 135) (hZ s))
    have m2 := le_mul_nn K O.toCone (K.mul ι ι) _ _ (O.nn_sq ι) m1
    have hb : Le K O.toCone (lsum K (fun r => dot3 K (N s r) (N s r)) l)
        (K.mul (K.mul (castN K 135) (K.mul ι ι)) (K.mul (As K l kz a s) Zb)) := by
      have z := le_mul_nn K O.toCone (K.mul (K.mul (castN K 135) (K.mul ι ι)) (As K l kz a s)) _ _ (O.nn_mul _ _ hc (hA s)) (zs s hsn)
      refine le_trans' K O.toCone _ _ _ (hN s) (le_trans' K O.toCone _ _ _ fu (le_trans' K O.toCone _ _ _ m2
        (le_trans' K O.toCone _ _ _ ⟨K.zero, O.nn_zero, ?_⟩ (le_trans' K O.toCone _ _ _ z ⟨K.zero, O.nn_zero, ?_⟩))))
      all_goals lib_ring
    have b2 := le_mul_nn K O.toCone (K.mul (K.add K.one K.one) (K.mul h h)) _ _ (O.nn_mul _ _ two (O.nn_sq h)) hb
    refine le_trans' K O.toCone _ _ _ tl (le_trans' K O.toCone _ _ _ (le_add' K O.toCone _ _ _ _ b2 (le_refl' K O.toCone _)) ⟨K.zero, O.nn_zero, ?_⟩)
    unfold As
    lib_ring
  -- sum over the k steps from t
  have ss := sum_le_upto K O _ _ k (fun j hj => stepD (t + j) (Nat.le_trans (Nat.le_of_lt (Nat.add_lt_add_left hj t)) htk))
  rw [NSChain.sumN_mul_left K _ _ k, NSChain.sumN_mul_left K _ _ k] at ss
  -- dissipation up to t + k
  have ld := LocalDiss.local_diss K O ν h _ _ Zb (Zs K l kz a) (As K l kz a) hν hν0 hh hc1 hc2 hZb hZ hA stp choice2 n budget (t + k) htk
  rw [sumN_split K (As K l kz a) t k] at ld
  have tn := ForceSob.le_mul_right' K O.toCone (RatModel.cast_mono K O.toCone (t + k) n htk) hD
  have nQ := O.nn_add _ _ (O.nn_mul _ _ hν (hZ (t + k))) (O.nn_mul _ _ (O.nn_mul _ _ hh (O.nn_sq ν)) (hA (t + k)))
  have nS0 := NSChain.sumN_nn K O.toCone (As K l kz a) hA t
  have ln := TimeLipN.lip_n K O.toCone l (fun j => a (t + j)) k
  generalize sumN K (fun j => lsum K (fun r => dot3 K (EnstDiff.sub3 K (a (t + j + 1) r) (a (t + j) r))
      (EnstDiff.sub3 K (a (t + j + 1) r) (a (t + j) r))) l) k = D at ss ln
  generalize sumN K (fun j => As K l kz a (t + j)) k = SA at ss ld
  -- hν²·SA ≤ 2νZb
  have sa : Le K O.toCone (K.mul (K.mul h (K.mul ν ν)) SA) (K.mul (K.add K.one K.one) (K.mul ν Zb)) := by
    have b2 := le_mul_nn K O.toCone (K.add K.one K.one) _ _ two
      (le_trans' K O.toCone _ _ _ (le_add' K O.toCone _ _ _ _ (le_refl' K O.toCone (Q K ν h (Zs K l kz a) (As K l kz a) 0)) tn) budget)
    refine le_trans' K O.toCone _ _ _ ⟨K.add (K.mul (K.add K.one K.one) (Q K ν h (Zs K l kz a) (As K l kz a) (t + k)))
        (K.mul (K.mul h (K.mul ν ν)) (sumN K (As K l kz a) t)),
      O.nn_add _ _ (O.nn_mul _ _ two nQ) (O.nn_mul _ _ (O.nn_mul _ _ hh (O.nn_sq ν)) nS0), by lib_ring⟩
      (le_trans' K O.toCone _ _ _ ld (le_trans' K O.toCone _ _ _ ⟨K.zero, O.nn_zero, by lib_ring⟩ b2))
  have hcz : O.nn (K.add (K.mul (K.mul (castN K 135) (K.mul ι ι)) Zb) (K.mul ν ν)) := O.nn_add _ _ (O.nn_mul _ _ hc hZb) (O.nn_sq ν)
  have f1 := le_mul_nn K O.toCone (K.mul ν ν) _ _ (O.nn_sq ν) ss
  have f2 := le_mul_nn K O.toCone (K.mul (K.mul (K.add K.one K.one) h) (K.add (K.mul (K.mul (castN K 135) (K.mul ι ι)) Zb) (K.mul ν ν))) _ _
    (O.nn_mul _ _ (O.nn_mul _ _ two hh) hcz) sa
  have fD : Le K O.toCone (K.mul (K.mul ν ν) D)
      (K.mul (K.mul (K.add (K.add K.one K.one) (K.add K.one K.one)) (K.mul h ν))
        (K.mul (K.add (K.mul (K.mul (castN K 135) (K.mul ι ι)) Zb) (K.mul ν ν)) Zb)) := by
    refine le_trans' K O.toCone _ _ _ f1 (le_trans' K O.toCone _ _ _ ⟨K.zero, O.nn_zero, ?_⟩ (le_trans' K O.toCone _ _ _ f2 ⟨K.zero, O.nn_zero, ?_⟩))
    all_goals lib_ring
  have g := le_mul_nn K O.toCone (castN K k) _ _ (RatModel.nn_cast K O.toCone k) fD
  have l3 := le_mul_nn K O.toCone (K.mul ν ν) _ _ (O.nn_sq ν) ln
  refine le_trans' K O.toCone _ _ _ l3 (le_trans' K O.toCone _ _ _ ⟨K.zero, O.nn_zero, ?_⟩ g)
  show K.add (K.mul (K.mul ν ν) (K.mul (castN K k) D)) K.zero = K.mul (castN K k) (K.mul (K.mul ν ν) D)
  lib_ring

end Gen

end LocalHolder

#print axioms LocalHolder.local_step
#print axioms LocalHolder.sum_le_upto
#print axioms LocalHolder.sumN_split
#print axioms LocalHolder.local_holder

ns/LocalTimeLe.lean

lean_all/ns/LocalTimeLe.lean

/-
  ns/LocalTimeLe.lean — T37 (goal G795b-h, piece a): the large-data local bound with the enstrophy level as an upper
  bound instead of an equation, and restarted at any window start. With cutoffs c₂Zb ≤ ν² (as in
  EnstLocalTime.local_time) and the budget Q_0 + n·h·c₁Zb² ≤ ν·Zb (no equation νZb = 2Q_0 needed):
    local_time_le   Z_t ≤ Zb for every t ≤ n,
    window_z        if Q(n·k) ≤ Qmax and Qmax + n·h·c₁Zb² ≤ ν·Zb, then Z_{n·k+t} ≤ Zb for every t ≤ n.
  So one bound Qmax on Q at the window starts fixes one Zb for every window (the hZA hypothesis of
  WindowShift.large_chain).

  Lean 4 core plus ns/EnstLocalGalerkin.
-/
import ns.EnstLocalGalerkin

namespace LocalTimeLe
open DetChain NSChain DyadicSmallGlobal LibRing DyadicLarge FourierGalerkin GalerkinForce ForceBound ForceSob
  ModeCount CubeCount ForceUniform NormCompare EnstStep EnstGlobal EnstGalerkin ForceTwoScale EnstLocal
  EnstLocalGalerkin LatticeSum

section Gen
variable {R : Type} (K : CRing R) (O : OrdR K) {I : Type}
variable (inv : R → R) (mul_inv : ∀ y, y ≠ K.zero → K.mul y (inv y) = K.one) (h10 : K.one ≠ K.zero)

include mul_inv h10 in
/-- **Z_t ≤ Zb on t ≤ n from the budget Q_0 + n h c₁ Zb² ≤ ν Zb** -/
theorem local_time_le (l : List I) (kz : I → Mode) (hd : List.Pairwise (fun a b => kz a ≠ kz b) l)
    (h1 : ∀ p, 1 ≤ nrm (kz p)) (M : Nat) (hM : ∀ p, p ∈ l → nrm (kz p) ≤ M + 1) (Rc t0 : Nat) (hRM : Rc ≤ M)
    (ι h ν Zb : R) (hh : O.nn h) (hν : O.nn ν) (hν0 : ν ≠ K.zero) (hZb : O.nn Zb)
    (a N : Nat → I → V3 R)
    (hN : ∀ t, Le K O.toCone (lsum K (fun r => dot3 K (N t r) (N t r)) l)
      (lsum K (fun r => dot3 K (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (a t) (a t) r)
        (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (a t) (a t) r)) l))
    (hs : ∀ t r, Step K h ν (wt K kz r) (a (t + 1) r) (a t r) (N t r))
    (choice : Le K O.toCone (K.mul (cc K ι (C2 K inv Rc t0)) Zb) (K.mul ν ν))
    (n : Nat) (budget : Le K O.toCone (K.add (Q K ν h (Zs K l kz a) (As K l kz a) 0)
      (K.mul (castN K n) (K.mul h (K.mul (cc K ι (C1 K inv Rc)) (K.mul Zb Zb))))) (K.mul ν Zb))
    (t : Nat) (ht : t ≤ n) : Le K O.toCone (Zs K l kz a t) Zb := by
  have q := local_galerkin K O inv mul_inv h10 l kz hd h1 M hM Rc t0 hRM ι h ν Zb hh hν hν0 hZb a N hN hs choice n
    budget t ht
  have hD : O.nn (K.mul h (K.mul (cc K ι (C1 K inv Rc)) (K.mul Zb Zb))) := by
    have hiv : O.nn (iv K inv (Rc + 1)) := iv_nn K O inv mul_inv h10 Rc
    have two : O.nn (K.add K.one K.one) := O.nn_add _ _ (RatModel.nn_one' K O.toCone) (RatModel.nn_one' K O.toCone)
    exact O.nn_mul _ _ hh (O.nn_mul _ _ (O.nn_mul _ _ (O.nn_mul _ _ two (O.nn_sq ι))
      (O.nn_add _ _ (O.nn_mul _ _ (RatModel.nn_cast K O.toCone _) (RatModel.nn_one' K O.toCone))
        (O.nn_mul _ _ (RatModel.nn_cast K O.toCone _) hiv))) (O.nn_sq Zb))
  have tn := ForceSob.le_mul_right' K O.toCone (RatModel.cast_mono K O.toCone t n ht) hD
  have q2 := le_trans' K O.toCone _ _ _ q (le_add' K O.toCone _ _ _ _ (le_refl' K O.toCone _) tn)
  have q3 := le_trans' K O.toCone _ _ _ q2 budget
  have hA : O.nn (As K l kz a t) :=
    lsum_nn K O.toCone _ (fun r => O.nn_mul _ _ (O.nn_sq _) (dot3_nn K O.toCone _)) l
  have hz : Le K O.toCone (K.mul ν (Zs K l kz a t)) (Q K ν h (Zs K l kz a) (As K l kz a) t) :=
    ⟨K.mul (K.mul h (K.mul ν ν)) (As K l kz a t), O.nn_mul _ _ (O.nn_mul _ _ hh (O.nn_sq ν)) hA, rfl⟩
  exact le_of_mul_le K O ν _ _ hν hν0 (le_trans' K O.toCone _ _ _ hz q3)

include mul_inv h10 in
/-- **one Qmax at the window starts gives one Zb on every window** -/
theorem window_z (l : List I) (kz : I → Mode) (hd : List.Pairwise (fun a b => kz a ≠ kz b) l)
    (h1 : ∀ p, 1 ≤ nrm (kz p)) (M : Nat) (hM : ∀ p, p ∈ l → nrm (kz p) ≤ M + 1) (Rc t0 : Nat) (hRM : Rc ≤ M)
    (ι h ν Zb : R) (hh : O.nn h) (hν : O.nn ν) (hν0 : ν ≠ K.zero) (hZb : O.nn Zb)
    (a N : Nat → I → V3 R)
    (hN : ∀ t, Le K O.toCone (lsum K (fun r => dot3 K (N t r) (N t r)) l)
      (lsum K (fun r => dot3 K (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (a t) (a t) r)
        (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (a t) (a t) r)) l))
    (hs : ∀ t r, Step K h ν (wt K kz r) (a (t + 1) r) (a t r) (N t r))
    (choice : Le K O.toCone (K.mul (cc K ι (C2 K inv Rc t0)) Zb) (K.mul ν ν))
    (n k : Nat) (Qmax : R) (hQ : Le K O.toCone (Q K ν h (Zs K l kz a) (As K l kz a) (n * k)) Qmax)
    (hbud : Le K O.toCone (K.add Qmax (K.mul (castN K n) (K.mul h (K.mul (cc K ι (C1 K inv Rc)) (K.mul Zb Zb)))))
      (K.mul ν Zb))
    (t : Nat) (ht : t ≤ n) : Le K O.toCone (Zs K l kz a (n * k + t)) Zb :=
  local_time_le K O inv mul_inv h10 l kz hd h1 M hM Rc t0 hRM ι h ν Zb hh hν hν0 hZb
    (fun s => a (n * k + s)) (fun s => N (n * k + s)) (fun s => hN (n * k + s)) (fun s r => hs (n * k + s) r) choice n
    (le_trans' K O.toCone _ _ _ (le_add' K O.toCone _ _ _ _ hQ (le_refl' K O.toCone _)) hbud) t ht

end Gen

end LocalTimeLe

#print axioms LocalTimeLe.local_time_le
#print axioms LocalTimeLe.window_z

ns/LogConvex.lean

lean_all/ns/LogConvex.lean

/-
  ns/LogConvex.lean — T37 cycle 182: log-convexity of the Sobolev norms of a truncation, every order.

  Lean 4 core plus ns/SumCS (cs_sum, sq), NSChain (lsum, lsum_ext), lib/Ring (pw), lib/RingTac. For modes i with
  |k_i| = κ_i and amplitudes a_i, write H_j = Σ_i (κ_i^j a_i)². Then for every j
      H_{j+1}² ≤ H_j · H_{j+2}
  (Cauchy–Schwarz with A = κ^j a, B = κ^{j+2} a). j = 0 is Interp (Z² ≤ E P); j = 1 is P² ≤ Z·‖∇Δu‖². log H_j
  is convex in j: two norms control every norm between them, none above.
-/
import ns.SumCS

namespace LogConvex
open DetChain NSChain LibRing SumCS

variable {R : Type} (K : CRing R) {I : Type}

def H (κ a : I → R) (j : Nat) (l : List I) : R := lsum K (fun i => sq K (K.mul (pw K (κ i) j) (a i))) l

theorem logconvex (P : Cone K) (κ a : I → R) (j : Nat) (l : List I) :
    Le K P (sq K (H K κ a (j + 1) l)) (K.mul (H K κ a j l) (H K κ a (j + 2) l)) := by
  have cs := cs_sum K P (fun i => K.mul (pw K (κ i) j) (a i))
    (fun i => K.mul (K.mul (K.mul (pw K (κ i) j) (κ i)) (κ i)) (a i)) l
  have e1 : lsum K (fun i => K.mul (K.mul (pw K (κ i) j) (a i)) (K.mul (K.mul (K.mul (pw K (κ i) j) (κ i)) (κ i)) (a i))) l
      = H K κ a (j + 1) l :=
    lsum_ext K _ _ (fun i => by show _ = sq K (K.mul (K.mul (pw K (κ i) j) (κ i)) (a i)); unfold sq; lib_ring) l
  rw [e1] at cs
  exact cs

end LogConvex

#print axioms LogConvex.logconvex

ns/LowAssemble.lean

lean_all/ns/LowAssemble.lean

/-
  ns/LowAssemble.lean — T37 cycle 839 (goal G549): the scalar assembly for the CFL version of the weighted
  semi-implicit bound, over any ordered ring without zero divisors (ℚ included): from
    (i)  (E' − E) + 2G' ≤ 2hB + D + hX          (SemiWStep2)
    (ii) 8D ≤ 2G' + 2G + 16q                     (LowStep summed)
  follows  4E' + 7G' ≤ 4E + G + 4(2hB + hX + 2q)  — the hypothesis of TwoLevel with R = 2hB + hX + 2q.

  Lean 4 core plus ns/SemiGlobal (halve), ns/RatModel (nn_one'), ns/DyadicLarge, lib/RingTac.
-/
import ns.SemiGlobal

namespace LowAssemble
open DetChain NSChain DyadicSmallGlobal LibRing DyadicLarge SemiGlobal RatModel

section Gen
variable {R : Type} (K : CRing R) (O : OrdR K)
include O

def m2 (y : R) : R := K.add y y
def m4 (y : R) : R := m2 K (m2 K y)
def m8 (y : R) : R := m2 K (m4 K y)

theorem low_assemble (h10 : K.one ≠ K.zero) (E E' G G' B D X q h : R)
    (i1 : Le K O.toCone (K.add (K.add E' (K.neg E)) (m2 K G')) (K.add (K.add (K.mul (m2 K h) B) D) (K.mul h X)))
    (i2 : Le K O.toCone (m8 K D) (K.add (K.add (m2 K G') (m2 K G)) (m8 K (m2 K q)))) :
    Le K O.toCone (K.add (m4 K E') (K.add (m4 K G') (K.add (m2 K G') G')))
      (K.add (K.add (m4 K E) G) (m4 K (K.add (K.add (K.mul (m2 K h) B) (K.mul h X)) (m2 K q)))) := by
  -- 8·(i): 8E' − 8E + 16G' ≤ 16hB + 8D + 8hX ≤ 16hB + 2G' + 2G + 16q + 8hX
  have s8 := le_mul_nn K O.toCone (m8 K K.one) _ _ (by unfold m8 m4 m2; exact O.nn_add _ _ (O.nn_add _ _ (O.nn_add _ _ (nn_one' K O.toCone) (nn_one' K O.toCone)) (O.nn_add _ _ (nn_one' K O.toCone) (nn_one' K O.toCone))) (O.nn_add _ _ (O.nn_add _ _ (nn_one' K O.toCone) (nn_one' K O.toCone)) (O.nn_add _ _ (nn_one' K O.toCone) (nn_one' K O.toCone)))) i1
  have s := le_trans' K O.toCone _ _ _ s8
    (show Le K O.toCone (K.mul (m8 K K.one) (K.add (K.add (K.mul (m2 K h) B) D) (K.mul h X)))
      (K.add (K.add (K.add (m2 K G') (m2 K G)) (m8 K (m2 K q))) (K.mul (m8 K K.one) (K.add (K.mul (m2 K h) B) (K.mul h X)))) from by
      have := le_add' K O.toCone _ _ _ _ i2 (le_refl' K O.toCone (K.mul (m8 K K.one) (K.add (K.mul (m2 K h) B) (K.mul h X))))
      have e : K.mul (m8 K K.one) (K.add (K.add (K.mul (m2 K h) B) D) (K.mul h X))
          = K.add (m8 K D) (K.mul (m8 K K.one) (K.add (K.mul (m2 K h) B) (K.mul h X))) := by unfold m8 m4 m2; lib_ring
      rw [e]; exact this)
  -- rearrange into 2·(target) ≤ 2·(rhs) + 0, then halve
  have H : Le K O.toCone (K.add (m4 K E') (K.add (m4 K G') (K.add (m2 K G') G')))
      (K.add (K.add (K.add (m4 K E) G) (m4 K (K.add (K.add (K.mul (m2 K h) B) (K.mul h X)) (m2 K q)))) K.zero) := by
    apply halve K O h10 _ _ K.zero O.nn_zero
    cases s with
    | intro d hd =>
      refine ⟨d, hd.1, ?_⟩
      have e1 : K.add (K.add (K.add (m4 K E') (K.add (m4 K G') (K.add (m2 K G') G')))
          (K.add (m4 K E') (K.add (m4 K G') (K.add (m2 K G') G')))) d
          = K.add (K.add (K.add (K.mul (m8 K K.one) (K.add (K.add E' (K.neg E)) (m2 K G'))) d) (m8 K E)) (K.neg (m2 K G')) := by
        unfold m8 m4 m2; lib_ring
      rw [e1, hd.2]; unfold m8 m4 m2; lib_ring
  rw [show K.add (K.add (K.add (m4 K E) G) (m4 K (K.add (K.add (K.mul (m2 K h) B) (K.mul h X)) (m2 K q)))) K.zero
    = K.add (K.add (m4 K E) G) (m4 K (K.add (K.add (K.mul (m2 K h) B) (K.mul h X)) (m2 K q))) by lib_ring] at H
  exact H

end Gen

end LowAssemble

#print axioms LowAssemble.low_assemble

ns/LowCFL.lean

lean_all/ns/LowCFL.lean

/-
  ns/LowCFL.lean — T37 cycle 846 (goal G556 = G546b): the weighted semi-implicit bound under the CFL-type condition
  4x_n ≤ 1 (x_n = hνk_n, all n < N), over any ordered ring without zero divisors (ℚ included). One step:
    4E' + 7G' ≤ 4E + G + 4R,   E = Σ w a², G = Σ w x a², R = 2hB + h Σ w (a' − a) N + 2 Σ w x (hN)²,
  given the rate bound Σ w a(−νka + N) ≤ B at the state (SemiWStep2 + LowSum + LowAssemble). Summed over time by
  TwoLevel: (4E_T + G_T) + 6 Σ G ≤ (4E_0 + G_0) + 4 Σ R.

  Lean 4 core plus ns/SemiWStep2, ns/LowSum, ns/LowAssemble, ns/TwoLevel, lib/RingTac.
    step_low    the one-step inequality in TwoLevel's form
-/
import ns.SemiWStep2
import ns.LowSum
import ns.TwoLevel

namespace LowCFL
open DetChain NSChain DyadicSmallGlobal LibRing DyadicLarge SemiWStep2 LowSum LowAssemble TwoLevel

section Gen
variable {R : Type} (K : CRing R) (O : OrdR K)
include O

theorem step_low (h10 : K.one ≠ K.zero) (w k a a' Nn : Nat → R) (h nu B : R) (N : Nat)
    (hh : O.nn h) (hw : ∀ n, O.nn (w n)) (hx : ∀ n, O.nn (K.mul h (K.mul nu (k n))))
    (h4 : ∀ n, Le K O.toCone (K.add (K.add (K.mul h (K.mul nu (k n))) (K.mul h (K.mul nu (k n))))
      (K.add (K.mul h (K.mul nu (k n))) (K.mul h (K.mul nu (k n))))) K.one)
    (st : ∀ n, K.add (a' n) (K.neg (a n)) = K.add (K.neg (K.mul (K.mul h (K.mul nu (k n))) (K.add (a' n) (a n))))
      (K.mul h (Nn n)))
    (rate : Le K O.toCone (sumN K (fun n => K.mul (w n) (K.mul (a n) (K.add (K.neg (K.mul nu (K.mul (k n) (a n)))) (Nn n)))) N) B) :
    Le K O.toCone
      (K.add (c4 K (sumN K (fun n => K.mul (w n) (K.mul (a' n) (a' n))) N))
        (c7 K (sumN K (fun n => K.mul (w n) (K.mul (K.mul h (K.mul nu (k n))) (K.mul (a' n) (a' n)))) N)))
      (K.add (K.add (c4 K (sumN K (fun n => K.mul (w n) (K.mul (a n) (a n))) N))
          (sumN K (fun n => K.mul (w n) (K.mul (K.mul h (K.mul nu (k n))) (K.mul (a n) (a n)))) N))
        (c4 K (K.add (K.add (K.mul (m2 K h) B) (K.mul h (sumN K (fun n => K.mul (w n) (K.mul (K.add (a' n) (K.neg (a n))) (Nn n))) N)))
          (m2 K (sumN K (fun n => K.mul (w n) (K.mul (K.mul h (K.mul nu (k n))) (K.mul (K.mul h (Nn n)) (K.mul h (Nn n))))) N))))) := by
  have s1 := semi_wstep2 K O.toCone w k a a' Nn h nu B N hh st rate
  have s2 := low_sum K O w (fun n => K.mul h (K.mul nu (k n))) a a' Nn h N hw hx h4 st
  generalize hE' : sumN K (fun n => K.mul (w n) (K.mul (a' n) (a' n))) N = E' 
  generalize hE : sumN K (fun n => K.mul (w n) (K.mul (a n) (a n))) N = E
  generalize hG' : sumN K (fun n => K.mul (w n) (K.mul (K.mul h (K.mul nu (k n))) (K.mul (a' n) (a' n)))) N = G' at s2 ⊢
  generalize hG : sumN K (fun n => K.mul (w n) (K.mul (K.mul h (K.mul nu (k n))) (K.mul (a n) (a n)))) N = G at s2 ⊢
  -- convert s1 to LowAssemble's (i1)
  have eS : sumN K (fun n => K.mul (w n) (K.add (K.mul (a' n) (a' n)) (K.neg (K.mul (a n) (a n))))) N = K.add E' (K.neg E) := by
    have t : K.add (sumN K (fun n => K.mul (w n) (K.add (K.mul (a' n) (a' n)) (K.neg (K.mul (a n) (a n))))) N) E = E' := by
      rw [← hE, ← hE', ← sumN_add]; apply DyUnique.sumN_ext_g; intro n _; lib_ring
    have : sumN K (fun n => K.mul (w n) (K.add (K.mul (a' n) (a' n)) (K.neg (K.mul (a n) (a n))))) N
        = K.add (K.add (sumN K (fun n => K.mul (w n) (K.add (K.mul (a' n) (a' n)) (K.neg (K.mul (a n) (a n))))) N) E) (K.neg E) := by
      lib_ring
    rw [this, t]
  have eW : sumN K (fun n => K.mul (w n) (K.mul (K.mul h (K.mul nu (k n))) (K.add (K.mul (a' n) (a' n)) (K.mul (a' n) (a' n))))) N
      = m2 K G' := by
    rw [← hG', ← m2_sum]; apply DyUnique.sumN_ext_g; intro n _; unfold m2; lib_ring
  rw [eS, eW] at s1
  have asm := low_assemble K O h10 E E' G G' B
    (sumN K (fun n => K.mul (w n) (K.mul (K.mul h (K.mul nu (k n))) (K.mul (K.add (a' n) (K.neg (a n))) (K.add (a' n) (K.neg (a n)))))) N)
    (sumN K (fun n => K.mul (w n) (K.mul (K.add (a' n) (K.neg (a n))) (Nn n))) N)
    (sumN K (fun n => K.mul (w n) (K.mul (K.mul h (K.mul nu (k n))) (K.mul (K.mul h (Nn n)) (K.mul h (Nn n))))) N) h
    (by
      have e : K.add (K.add (K.mul (m2 K h) B)
          (sumN K (fun n => K.mul (w n) (K.mul (K.mul h (K.mul nu (k n))) (K.mul (K.add (a' n) (K.neg (a n))) (K.add (a' n) (K.neg (a n)))))) N))
          (K.mul h (sumN K (fun n => K.mul (w n) (K.mul (K.add (a' n) (K.neg (a n))) (Nn n))) N))
          = K.add (K.add (K.mul (K.add h h) B)
          (sumN K (fun n => K.mul (w n) (K.mul (K.mul h (K.mul nu (k n))) (K.mul (K.add (a' n) (K.neg (a n))) (K.add (a' n) (K.neg (a n)))))) N))
          (K.mul h (sumN K (fun n => K.mul (w n) (K.mul (K.add (a' n) (K.neg (a n))) (Nn n))) N)) := rfl
      rw [e]; exact s1)
    s2
  cases asm with
  | intro d hd =>
    refine ⟨d, hd.1, ?_⟩
    have : K.add (K.add (c4 K E') (c7 K G')) d = K.add (K.add (m4 K E') (K.add (m4 K G') (K.add (m2 K G') G'))) d := by
      unfold c7 c6 c4 m4 m2; lib_ring
    rw [this, hd.2]; unfold c4 m4 m2; lib_ring

end Gen

end LowCFL

#print axioms LowCFL.step_low

ns/LowMode.lean

lean_all/ns/LowMode.lean

/-
  ns/LowMode.lean — T37 cycle 826 (goal G537): the low-mode part of the semi-implicit Δ-terms (G528b), over any
  ordered ring (ℚ included): for a mode with 0 ≤ x and 4x ≤ 1 and Δ = −x s + hN,
    8x·Δ² ≤ x·s² + 16x·h²N².
  So on low modes Σ w x Δ² ≤ (1/8)Σ w x s² + 2h²Σ w x N²: an eighth of the midpoint dissipation plus O(h²).
  (High modes, 4x > 1, are handled by ForcedDamp.)

  Lean 4 core plus ns/RiccatiH (sq_le), ns/MidRecover (sq_add_le, two), lib/RingTac.
-/
import ns.RiccatiH
import ns.MidRecover

namespace LowMode
open DetChain NSChain DyadicSmallGlobal LibRing DyadicLarge RiccatiH MidRecover

section Gen
variable {R : Type} (K : CRing R) (O : OrdR K)
include O

theorem low_mode (x s hN : R) (hx : O.nn x)
    (h4 : Le K O.toCone (K.add (K.add x x) (K.add x x)) K.one) :
    Le K O.toCone (K.mul (K.add (K.add (K.add x x) (K.add x x)) (K.add (K.add x x) (K.add x x)))
        (K.mul (K.add (K.neg (K.mul x s)) hN) (K.add (K.neg (K.mul x s)) hN)))
      (K.add (K.mul x (K.mul s s))
        (K.mul (K.mul (K.add (K.add (K.add x x) (K.add x x)) (K.add (K.add x x) (K.add x x)))
          (K.add K.one K.one)) (K.mul hN hN))) := by
  -- Δ² ≤ 2x²s² + 2(hN)²
  have q := sq_add_le K O.toCone (K.neg (K.mul x s)) hN
  have hx8 : O.nn (K.add (K.add (K.add x x) (K.add x x)) (K.add (K.add x x) (K.add x x))) :=
    O.nn_add _ _ (O.nn_add _ _ (O.nn_add _ _ hx hx) (O.nn_add _ _ hx hx)) (O.nn_add _ _ (O.nn_add _ _ hx hx) (O.nn_add _ _ hx hx))
  have q8 := le_mul_nn K O.toCone _ _ _ hx8 q
  apply le_trans' K O.toCone _ _ _ q8
  -- 8x·2x²s² = x·(4x)²·s² ≤ x·s²
  have f4 : O.nn (K.add (K.add x x) (K.add x x)) := O.nn_add _ _ (O.nn_add _ _ hx hx) (O.nn_add _ _ hx hx)
  have sq1 := sq_le K O _ _ f4 h4
  have t1 := le_mul_nn K O.toCone x _ _ hx sq1
  have t2 : Le K O.toCone (K.mul (K.mul x (K.mul (K.add (K.add x x) (K.add x x)) (K.add (K.add x x) (K.add x x)))) (K.mul s s))
      (K.mul (K.mul x (K.mul K.one K.one)) (K.mul s s)) := by
    cases t1 with
    | intro d hd => exact ⟨K.mul d (K.mul s s), O.nn_mul _ _ hd.1 (O.nn_sq s), by rw [← hd.2]; lib_ring⟩
  have tot := le_add' K O.toCone _ _ _ _ t2 (le_refl' K O.toCone
    (K.mul (K.mul (K.add (K.add (K.add x x) (K.add x x)) (K.add (K.add x x) (K.add x x))) (K.add K.one K.one)) (K.mul hN hN)))
  have e1 : K.mul (K.add (K.add (K.add x x) (K.add x x)) (K.add (K.add x x) (K.add x x)))
        (K.add (two K (K.mul (K.neg (K.mul x s)) (K.neg (K.mul x s)))) (two K (K.mul hN hN)))
      = K.add (K.mul (K.mul x (K.mul (K.add (K.add x x) (K.add x x)) (K.add (K.add x x) (K.add x x)))) (K.mul s s))
        (K.mul (K.mul (K.add (K.add (K.add x x) (K.add x x)) (K.add (K.add x x) (K.add x x))) (K.add K.one K.one)) (K.mul hN hN)) := by
    unfold two; lib_ring
  have e2 : K.add (K.mul (K.mul x (K.mul K.one K.one)) (K.mul s s))
        (K.mul (K.mul (K.add (K.add (K.add x x) (K.add x x)) (K.add (K.add x x) (K.add x x))) (K.add K.one K.one)) (K.mul hN hN))
      = K.add (K.mul x (K.mul s s))
        (K.mul (K.mul (K.add (K.add (K.add x x) (K.add x x)) (K.add (K.add x x) (K.add x x))) (K.add K.one K.one)) (K.mul hN hN)) := by
    lib_ring
  rw [e1]; rw [e2] at tot; exact tot

end Gen

end LowMode

#print axioms LowMode.low_mode

ns/LowStep.lean

lean_all/ns/LowStep.lean

/-
  ns/LowStep.lean — T37 cycle 836 (goal G546): when every mode is low (4x_n ≤ 1, a CFL-type condition for fixed
  truncation), the Δ-term of SemiWStep2 is absorbed: per mode, over any ordered ring (ℚ included),
    8x·Δ² ≤ 2x·a'² + 2x·a² + 16x·h²N²          (Δ = a' − a = −x(a' + a) + hN, 0 ≤ x, 4x ≤ 1),
  (summed with weights w ≥ 0 and combined with SemiWStep2 it yields the hypothesis 4E' + 7G' ≤ 4E + G + 4R of
  TwoLevel).

  Lean 4 core plus ns/LowMode (low_mode), ns/MidRecover (sq_add_le), ns/DyadicLarge, lib/RingTac.
    low_pt     the per-mode inequality
-/
import ns.LowMode

namespace LowStep
open DetChain NSChain DyadicSmallGlobal LibRing DyadicLarge LowMode MidRecover

section Gen
variable {R : Type} (K : CRing R) (O : OrdR K)
include O

def e8 (y : R) : R := K.add (K.add (K.add y y) (K.add y y)) (K.add (K.add y y) (K.add y y))

theorem low_pt (a a' x hN : R) (hx : O.nn x) (h4 : Le K O.toCone (K.add (K.add x x) (K.add x x)) K.one)
    (st : K.add a' (K.neg a) = K.add (K.neg (K.mul x (K.add a' a))) hN) :
    Le K O.toCone (e8 K (K.mul x (K.mul (K.add a' (K.neg a)) (K.add a' (K.neg a)))))
      (K.add (K.add (K.add (K.mul x (K.mul a' a')) (K.mul x (K.mul a' a'))) (K.add (K.mul x (K.mul a a)) (K.mul x (K.mul a a))))
        (K.mul (K.mul (e8 K x) (K.add K.one K.one)) (K.mul hN hN))) := by
  have lm := low_mode K O x (K.add a' a) hN hx h4
  rw [← st] at lm
  have e1 : e8 K (K.mul x (K.mul (K.add a' (K.neg a)) (K.add a' (K.neg a))))
      = K.mul (K.add (K.add (K.add x x) (K.add x x)) (K.add (K.add x x) (K.add x x)))
        (K.mul (K.add a' (K.neg a)) (K.add a' (K.neg a))) := by unfold e8; lib_ring
  rw [e1]
  apply le_trans' K O.toCone _ _ _ lm
  -- x s² ≤ 2x a'² + 2x a²   (s² + Δ² = 2a'² + 2a², x ≥ 0)
  have q : Le K O.toCone (K.mul x (K.mul (K.add a' a) (K.add a' a)))
      (K.add (K.add (K.mul x (K.mul a' a')) (K.mul x (K.mul a' a'))) (K.add (K.mul x (K.mul a a)) (K.mul x (K.mul a a)))) :=
    ⟨K.mul x (K.mul (K.add a' (K.neg a)) (K.add a' (K.neg a))), O.nn_mul _ _ hx (O.nn_sq _), by lib_ring⟩
  have e2 : K.mul (K.mul (K.add (K.add (K.add x x) (K.add x x)) (K.add (K.add x x) (K.add x x))) (K.add K.one K.one)) (K.mul hN hN)
      = K.mul (K.mul (e8 K x) (K.add K.one K.one)) (K.mul hN hN) := by unfold e8; rfl
  rw [e2]
  exact le_add' K O.toCone _ _ _ _ q (le_refl' K O.toCone _)

end Gen

end LowStep

#print axioms LowStep.low_pt

ns/LowSum.lean

lean_all/ns/LowSum.lean

/-
  ns/LowSum.lean — T37 cycle 842 (goal G552): the weighted sum of LowStep, in the form of LowAssemble's (ii), over
  any ordered ring (ℚ included): if 0 ≤ x_n, 4x_n ≤ 1, w_n ≥ 0 and a'_n − a_n = −x_n(a'_n + a_n) + h N_n for n < N,
    8·D ≤ 2·G' + 2·G + 16·q,
  D = Σ w x (a' − a)²,  G' = Σ w x a'²,  G = Σ w x a²,  q = Σ w x (hN)².

  Lean 4 core plus ns/LowStep (low_pt), ns/LowAssemble (m2, m8), ns/DyadicLarge (sum_le), lib/RingTac.
-/
import ns.LowStep
import ns.LowAssemble

namespace LowSum
open DetChain NSChain DyadicSmallGlobal LibRing DyadicLarge LowStep LowAssemble

section Gen
variable {R : Type} (K : CRing R) (O : OrdR K)
include O

omit O in
theorem m2_sum (f : Nat → R) : ∀ M, sumN K (fun n => m2 K (f n)) M = m2 K (sumN K f M)
  | 0 => by show K.zero = K.add K.zero K.zero; lib_ring
  | M + 1 => by
    show K.add (sumN K (fun n => m2 K (f n)) M) (m2 K (f M)) = m2 K (K.add (sumN K f M) (f M))
    rw [m2_sum f M]; unfold m2; lib_ring

omit O in
theorem m8_sum (f : Nat → R) (M : Nat) : sumN K (fun n => m8 K (f n)) M = m8 K (sumN K f M) := by
  unfold m8 m4
  rw [m2_sum K (fun n => m2 K (m2 K (f n))), m2_sum K (fun n => m2 K (f n)), m2_sum]

theorem low_sum (w x a a' Nn : Nat → R) (h : R) (N : Nat) (hw : ∀ n, O.nn (w n)) (hx : ∀ n, O.nn (x n))
    (h4 : ∀ n, Le K O.toCone (K.add (K.add (x n) (x n)) (K.add (x n) (x n))) K.one)
    (st : ∀ n, K.add (a' n) (K.neg (a n)) = K.add (K.neg (K.mul (x n) (K.add (a' n) (a n)))) (K.mul h (Nn n))) :
    Le K O.toCone (m8 K (sumN K (fun n => K.mul (w n) (K.mul (x n) (K.mul (K.add (a' n) (K.neg (a n))) (K.add (a' n) (K.neg (a n)))))) N))
      (K.add (K.add (m2 K (sumN K (fun n => K.mul (w n) (K.mul (x n) (K.mul (a' n) (a' n)))) N))
          (m2 K (sumN K (fun n => K.mul (w n) (K.mul (x n) (K.mul (a n) (a n)))) N)))
        (m8 K (m2 K (sumN K (fun n => K.mul (w n) (K.mul (x n) (K.mul (K.mul h (Nn n)) (K.mul h (Nn n))))) N)))) := by
  have pt : ∀ n, Le K O.toCone (m8 K (K.mul (w n) (K.mul (x n) (K.mul (K.add (a' n) (K.neg (a n))) (K.add (a' n) (K.neg (a n)))))))
      (K.add (K.add (m2 K (K.mul (w n) (K.mul (x n) (K.mul (a' n) (a' n))))) (m2 K (K.mul (w n) (K.mul (x n) (K.mul (a n) (a n))))))
        (m8 K (m2 K (K.mul (w n) (K.mul (x n) (K.mul (K.mul h (Nn n)) (K.mul h (Nn n)))))))) := by
    intro n
    have l := le_mul_nn K O.toCone (w n) _ _ (hw n) (low_pt K O (a n) (a' n) (x n) (K.mul h (Nn n)) (hx n) (h4 n) (st n))
    cases l with
    | intro d hd =>
      refine ⟨d, hd.1, ?_⟩
      have e1 : K.add (m8 K (K.mul (w n) (K.mul (x n) (K.mul (K.add (a' n) (K.neg (a n))) (K.add (a' n) (K.neg (a n))))))) d
          = K.add (K.mul (w n) (e8 K (K.mul (x n) (K.mul (K.add (a' n) (K.neg (a n))) (K.add (a' n) (K.neg (a n))))))) d := by
        unfold m8 m4 m2 e8; lib_ring
      rw [e1, hd.2]; unfold m8 m4 m2 e8; lib_ring
  have sm := sum_le K O.toCone _ _ pt N
  have L : sumN K (fun n => m8 K (K.mul (w n) (K.mul (x n) (K.mul (K.add (a' n) (K.neg (a n))) (K.add (a' n) (K.neg (a n))))))) N
      = m8 K (sumN K (fun n => K.mul (w n) (K.mul (x n) (K.mul (K.add (a' n) (K.neg (a n))) (K.add (a' n) (K.neg (a n)))))) N) := by
    exact m8_sum K _ N
  have Rr : sumN K (fun n => K.add (K.add (m2 K (K.mul (w n) (K.mul (x n) (K.mul (a' n) (a' n))))) (m2 K (K.mul (w n) (K.mul (x n) (K.mul (a n) (a n))))))
        (m8 K (m2 K (K.mul (w n) (K.mul (x n) (K.mul (K.mul h (Nn n)) (K.mul h (Nn n)))))))) N
      = K.add (K.add (m2 K (sumN K (fun n => K.mul (w n) (K.mul (x n) (K.mul (a' n) (a' n)))) N))
          (m2 K (sumN K (fun n => K.mul (w n) (K.mul (x n) (K.mul (a n) (a n)))) N)))
        (m8 K (m2 K (sumN K (fun n => K.mul (w n) (K.mul (x n) (K.mul (K.mul h (Nn n)) (K.mul h (Nn n))))) N))) := by
    rw [sumN_add, sumN_add, m2_sum K (fun n => K.mul (w n) (K.mul (x n) (K.mul (a' n) (a' n)))),
      m2_sum K (fun n => K.mul (w n) (K.mul (x n) (K.mul (a n) (a n)))),
      m8_sum K (fun n => m2 K (K.mul (w n) (K.mul (x n) (K.mul (K.mul h (Nn n)) (K.mul h (Nn n)))))),
      m2_sum K (fun n => K.mul (w n) (K.mul (x n) (K.mul (K.mul h (Nn n)) (K.mul h (Nn n)))))]
  rw [L, Rr] at sm; exact sm

end Gen

end LowSum

#print axioms LowSum.m2_sum
#print axioms LowSum.m8_sum
#print axioms LowSum.low_sum

ns/MidRecover.lean

lean_all/ns/MidRecover.lean

/-
  ns/MidRecover.lean — T37 cycle 753 (goal G466): recovering a_{t+1} from the midpoint sum s = a_{t+1} + a_t and
  the step, mode by mode, over any ring with a positive cone (ℚ included):
    4·a_{t+1}(n)² ≤ 2 s_n² + 4 h² (ν² k_n² s_n² + N_n²)
  where the CN step reads a_{t+1}(n) − a_t(n) = h(−ν k_n s_n + N_n) (N_n = dyN for the dyadic model).

  Lean 4 core plus ns/DyadicLarge (le_add'), NSChain (Cone, Le), lib/RingTac (lib_ring).
    sq_add_le   (x + y)² ≤ 2x² + 2y²
    le_two_mul  x ≤ y, c ≥ 0 ⇒ c·x ≤ c·y
    recover     the inequality above
-/
import ns.DyadicLarge
import lib.RingTac

namespace MidRecover
open DetChain NSChain LibRing DyadicLarge

section Gen
variable {R : Type} (K : CRing R) (P : Cone K)

def two (x : R) : R := K.add x x

theorem sq_add_le (x y : R) :
    Le K P (K.mul (K.add x y) (K.add x y)) (K.add (two K (K.mul x x)) (two K (K.mul y y))) :=
  ⟨K.mul (K.add x (K.neg y)) (K.add x (K.neg y)), P.nn_sq _, by unfold two; lib_ring⟩

theorem le_two_mul (c x y : R) (hc : P.nn c) (h : Le K P x y) : Le K P (K.mul c x) (K.mul c y) := by
  cases h with
  | intro d hd => exact ⟨K.mul c d, P.nn_mul _ _ hc hd.1, by rw [← hd.2]; lib_ring⟩

theorem recover (a a' k Nn : R) (h nu : R)
    (step : K.add a' (K.neg a) = K.mul h (K.add (K.neg (K.mul nu (K.mul k (K.add a' a)))) Nn)) :
    Le K P (K.mul (K.add (K.add K.one K.one) (K.add K.one K.one)) (K.mul a' a'))
      (K.add (two K (K.mul (K.add a' a) (K.add a' a)))
        (two K (two K (K.mul (K.mul h h)
          (K.add (K.mul (K.mul nu nu) (K.mul (K.mul k k) (K.mul (K.add a' a) (K.add a' a)))) (K.mul Nn Nn)))))) := by
  -- 2a' = s + Δ with Δ = a' − a = h(−ν k s + N)
  have e1 : K.mul (K.add (K.add K.one K.one) (K.add K.one K.one)) (K.mul a' a')
      = K.mul (K.add (K.add a' a) (K.add a' (K.neg a))) (K.add (K.add a' a) (K.add a' (K.neg a))) := by lib_ring
  rw [e1]
  have l1 := sq_add_le K P (K.add a' a) (K.add a' (K.neg a))
  apply le_trans' K P _ _ _ l1
  apply le_add' K P _ _ _ _ (le_refl' K P _)
  unfold two
  rw [step]
  -- Δ² = h² (−ν k s + N)² ≤ h² · 2 (ν²k²s² + N²)
  have l2 := sq_add_le K P (K.neg (K.mul nu (K.mul k (K.add a' a)))) Nn
  have l3 := le_two_mul K P (K.mul h h) _ _ (P.nn_sq h) l2
  have l4 := le_add' K P _ _ _ _ l3 l3
  have e2 : K.mul (K.mul h (K.add (K.neg (K.mul nu (K.mul k (K.add a' a)))) Nn))
      (K.mul h (K.add (K.neg (K.mul nu (K.mul k (K.add a' a)))) Nn))
      = K.mul (K.mul h h) (K.mul (K.add (K.neg (K.mul nu (K.mul k (K.add a' a)))) Nn)
          (K.add (K.neg (K.mul nu (K.mul k (K.add a' a)))) Nn)) := by lib_ring
  rw [e2]
  have e3 : K.add (K.mul (K.mul h h) (K.add (two K (K.mul (K.neg (K.mul nu (K.mul k (K.add a' a))))
        (K.neg (K.mul nu (K.mul k (K.add a' a)))))) (two K (K.mul Nn Nn))))
      (K.mul (K.mul h h) (K.add (two K (K.mul (K.neg (K.mul nu (K.mul k (K.add a' a))))
        (K.neg (K.mul nu (K.mul k (K.add a' a)))))) (two K (K.mul Nn Nn))))
      = K.add (K.add (K.mul (K.mul h h) (K.add (K.mul (K.mul nu nu) (K.mul (K.mul k k) (K.mul (K.add a' a) (K.add a' a)))) (K.mul Nn Nn)))
          (K.mul (K.mul h h) (K.add (K.mul (K.mul nu nu) (K.mul (K.mul k k) (K.mul (K.add a' a) (K.add a' a)))) (K.mul Nn Nn))))
        (K.add (K.mul (K.mul h h) (K.add (K.mul (K.mul nu nu) (K.mul (K.mul k k) (K.mul (K.add a' a) (K.add a' a)))) (K.mul Nn Nn)))
          (K.mul (K.mul h h) (K.add (K.mul (K.mul nu nu) (K.mul (K.mul k k) (K.mul (K.add a' a) (K.add a' a)))) (K.mul Nn Nn)))) := by
    unfold two; lib_ring
  unfold two at l4
  unfold two at e3
  rw [e3] at l4
  exact l4

end Gen

end MidRecover

#print axioms MidRecover.sq_add_le
#print axioms MidRecover.le_two_mul
#print axioms MidRecover.recover

ns/Midpoint.lean

lean_all/ns/Midpoint.lean

/-
  ns/Midpoint.lean — T37 cycle 209 (goal G1, step 2): the midpoint enstrophy of a Crank–Nicolson step is controlled
  by the endpoint enstrophies.

  Lean 4 core plus ns/EnstrophyStep (Zw), lib/Gronwall, NSChain, lib/RingTac. For weights w ≥ 0:
      ρ² = Σ w (a′ + a)² ≤ 2 Z′ + 2 Z ,   Z = Σ w a²,  Z′ = Σ w a′²          (midpoint)
  ((x + y)² + (x − y)² = 2x² + 2y²). With EnstrophyStep: Z′ ≤ Z + h(cXρσ − νσ²) and ρ² ≤ 2(Z + Z′).
-/
import ns.EnstrophyStep

namespace Midpoint
open DetChain NSChain LibRing LibCS LibGron EnstrophyStep

variable {R : Type} (K : CRing R) {I : Type}

theorem midpoint (P : Pos K) (w a a' : I → R) (hw : ∀ i, P.nn (w i)) : ∀ l : List I,
    le K P (lsum K (fun i => K.mul (w i) (K.mul (K.add (a' i) (a i)) (K.add (a' i) (a i)))) l)
      (K.add (K.add (Zw K w a' l) (Zw K w a' l)) (K.add (Zw K w a l) (Zw K w a l)))
  | [] => by
    unfold le; show P.nn (sub K (K.add (K.add K.zero K.zero) (K.add K.zero K.zero)) K.zero)
    rw [show sub K (K.add (K.add K.zero K.zero) (K.add K.zero K.zero)) K.zero = K.zero by lib_ring]; exact P.nn_zero
  | i :: l => by
    have ih := midpoint P w a a' hw l
    unfold le at ih ⊢
    show P.nn (sub K (K.add (K.add (K.add (K.mul (w i) (K.mul (a' i) (a' i))) (Zw K w a' l))
        (K.add (K.mul (w i) (K.mul (a' i) (a' i))) (Zw K w a' l)))
        (K.add (K.add (K.mul (w i) (K.mul (a i) (a i))) (Zw K w a l)) (K.add (K.mul (w i) (K.mul (a i) (a i))) (Zw K w a l))))
      (K.add (K.mul (w i) (K.mul (K.add (a' i) (a i)) (K.add (a' i) (a i))))
        (lsum K (fun i => K.mul (w i) (K.mul (K.add (a' i) (a i)) (K.add (a' i) (a i)))) l)))
    have e : sub K (K.add (K.add (K.add (K.mul (w i) (K.mul (a' i) (a' i))) (Zw K w a' l))
        (K.add (K.mul (w i) (K.mul (a' i) (a' i))) (Zw K w a' l)))
        (K.add (K.add (K.mul (w i) (K.mul (a i) (a i))) (Zw K w a l)) (K.add (K.mul (w i) (K.mul (a i) (a i))) (Zw K w a l))))
      (K.add (K.mul (w i) (K.mul (K.add (a' i) (a i)) (K.add (a' i) (a i))))
        (lsum K (fun i => K.mul (w i) (K.mul (K.add (a' i) (a i)) (K.add (a' i) (a i)))) l))
      = K.add (K.mul (w i) (K.mul (sub K (a' i) (a i)) (sub K (a' i) (a i))))
          (sub K (K.add (K.add (Zw K w a' l) (Zw K w a' l)) (K.add (Zw K w a l) (Zw K w a l)))
            (lsum K (fun i => K.mul (w i) (K.mul (K.add (a' i) (a i)) (K.add (a' i) (a i)))) l)) := by lib_ring
    rw [e]; exact P.nn_add _ _ (P.nn_mul _ _ (hw i) (P.nn_sq _)) ih

end Midpoint

#print axioms Midpoint.midpoint

ns/MidpointStep.lean

lean_all/ns/MidpointStep.lean

/-
  ns/MidpointStep.lean — T37 cycle 212 (goal G1, step 3): Young's step for the Crank–Nicolson (midpoint) form.

  Lean 4 core plus lib/Gronwall (le, le_trans, le_mul_left), lib/RingTac. If
      Z′ ≤ Z + h(cXρσ − νσ²)   and   ρ² ≤ 2(Z + Z′)          (EnstrophyStep, Midpoint)
  then, with (2νσ − cXρ)² ≥ 0,
      2(2ν − hc²X²) Z′ ≤ 2(2ν + hc²X²) Z                                            (midpoint_step)
  i.e. Z′ ≤ Z (1 + a)/(1 − a), a = hc²X²/(2ν) < 1: the CN analogue of YoungStep (Z′ ≤ (1 + a) Z); the product
  over a run gives Π (1 + a_k)/(1 − a_k), bounded while Σ a_k stays small (Serrin, midpoint form).
-/
import lib.Gronwall

namespace MidpointStep
open DetChain LibRing LibCS LibGron

variable {R : Type} (K : CRing R) (P : Pos K)

theorem midpoint_step (Z Z' h c X ρ σ ν : R) (hh : P.nn h) (hν : P.nn ν)
    (step : le K P Z' (K.add Z (K.mul h (sub K (K.mul (K.mul (K.mul c X) ρ) σ) (K.mul ν (K.mul σ σ))))))
    (hρ : le K P (K.mul ρ ρ) (K.add (K.add Z Z) (K.add Z' Z'))) :
    le K P (K.add (K.mul (sub K (K.add ν ν) (K.mul h (K.mul (K.mul c c) (K.mul X X)))) Z')
                  (K.mul (sub K (K.add ν ν) (K.mul h (K.mul (K.mul c c) (K.mul X X)))) Z'))
      (K.add (K.mul (K.add (K.add ν ν) (K.mul h (K.mul (K.mul c c) (K.mul X X)))) Z)
             (K.mul (K.add (K.add ν ν) (K.mul h (K.mul (K.mul c c) (K.mul X X)))) Z)) := by
  have s1 := le_mul_left K P (P.nn_add _ _ (P.nn_add _ _ hν hν) (P.nn_add _ _ hν hν)) step
  have s2 := le_mul_left K P (P.nn_mul _ _ hh (P.nn_mul _ _ (P.nn_sq c) (P.nn_sq X))) hρ
  unfold le at s1 s2 ⊢
  have g3 : P.nn (K.mul h (K.mul (sub K (K.mul (K.add ν ν) σ) (K.mul (K.mul c X) ρ))
      (sub K (K.mul (K.add ν ν) σ) (K.mul (K.mul c X) ρ)))) := P.nn_mul _ _ hh (P.nn_sq _)
  have e : sub K (K.add (K.mul (K.add (K.add ν ν) (K.mul h (K.mul (K.mul c c) (K.mul X X)))) Z)
             (K.mul (K.add (K.add ν ν) (K.mul h (K.mul (K.mul c c) (K.mul X X)))) Z))
        (K.add (K.mul (sub K (K.add ν ν) (K.mul h (K.mul (K.mul c c) (K.mul X X)))) Z')
               (K.mul (sub K (K.add ν ν) (K.mul h (K.mul (K.mul c c) (K.mul X X)))) Z'))
      = K.add (K.add
          (sub K (K.mul (K.add (K.add ν ν) (K.add ν ν))
            (K.add Z (K.mul h (sub K (K.mul (K.mul (K.mul c X) ρ) σ) (K.mul ν (K.mul σ σ))))))
            (K.mul (K.add (K.add ν ν) (K.add ν ν)) Z'))
          (sub K (K.mul (K.mul h (K.mul (K.mul c c) (K.mul X X))) (K.add (K.add Z Z) (K.add Z' Z')))
            (K.mul (K.mul h (K.mul (K.mul c c) (K.mul X X))) (K.mul ρ ρ))))
          (K.mul h (K.mul (sub K (K.mul (K.add ν ν) σ) (K.mul (K.mul c X) ρ))
            (sub K (K.mul (K.add ν ν) σ) (K.mul (K.mul c X) ρ)))) := by lib_ring
  rw [e]; exact P.nn_add _ _ (P.nn_add _ _ s1 s2) g3

end MidpointStep

#print axioms MidpointStep.midpoint_step

ns/ModeCount.lean

lean_all/ns/ModeCount.lean

/-
  ns/ModeCount.lean — T37 cycle 948 (goal G624c, piece 6): the counting function of a set of distinct modes.
  For a duplicate-free list l of modes k ∈ ℤ³ with sup norms nrm k = max(|k₁|, |k₂|, |k₃|):
    cnt_cube   cnt(norms l, j) ≤ (2j + 1)·((2j + 1)·(2j + 1))   for every j,
  i.e. the input of AbelCount.abel_list is bounded by the cube count (CubeCount.count_cube) — the entries of l
  with nrm ≤ j form a duplicate-free sublist inside the cube [−j, j]³.

  Lean 4 core plus ns/CubeCount, ns/AbelCount (cnt); own max and filter (core Nat.max lemmas use propext).
-/
import ns.CubeCount
import ns.AbelCount

namespace ModeCount
open CubeCount AbelCount

def mx (a b : Nat) : Nat := cond (Nat.ble a b) b a

theorem le_mx_left (a b : Nat) : a ≤ mx a b := by
  show a ≤ cond (Nat.ble a b) b a
  cases hb : Nat.ble a b with
  | true => exact Nat.le_of_ble_eq_true hb
  | false => exact Nat.le_refl a

theorem le_mx_right (a b : Nat) : b ≤ mx a b := by
  show b ≤ cond (Nat.ble a b) b a
  cases hb : Nat.ble a b with
  | true => exact Nat.le_refl b
  | false =>
    cases Nat.lt_or_ge a b with
    | inl h => exact absurd (Nat.ble_eq_true_of_le (Nat.le_of_lt h)) (by rw [hb]; exact Bool.noConfusion)
    | inr h => exact h

/-- sup norm |k|_∞ -/
def nrm (k : Mode) : Nat := mx k.1.natAbs (mx k.2.1.natAbs k.2.2.natAbs)

def norms : List Mode → List Nat
  | [] => []
  | k :: l => nrm k :: norms l

/-- the modes with nrm ≤ j -/
def flt (j : Nat) : List Mode → List Mode
  | [] => []
  | k :: l => cond (Nat.ble (nrm k) j) (k :: flt j l) (flt j l)

theorem flt_len (j : Nat) : ∀ (l : List Mode), (flt j l).length = cnt (norms l) j
  | [] => rfl
  | k :: l => by
    show (cond (Nat.ble (nrm k) j) (k :: flt j l) (flt j l)).length = cond (Nat.ble (nrm k) j) (cnt (norms l) j + 1) (cnt (norms l) j)
    cases Nat.ble (nrm k) j with
    | true => show (flt j l).length + 1 = cnt (norms l) j + 1; rw [flt_len j l]
    | false => exact flt_len j l

theorem mem_flt (j : Nat) (x : Mode) : ∀ (l : List Mode), x ∈ flt j l → x ∈ l ∧ nrm x ≤ j
  | [], h => nomatch h
  | k :: l, h => by
    have h' : x ∈ cond (Nat.ble (nrm k) j) (k :: flt j l) (flt j l) := h
    cases hb : Nat.ble (nrm k) j with
    | true =>
      rw [hb] at h'
      cases h' with
      | head => exact ⟨List.Mem.head _, Nat.le_of_ble_eq_true hb⟩
      | tail _ h'' =>
        have r := mem_flt j x l h''
        exact ⟨List.Mem.tail _ r.1, r.2⟩
    | false =>
      rw [hb] at h'
      have r := mem_flt j x l h'
      exact ⟨List.Mem.tail _ r.1, r.2⟩

theorem flt_pw (j : Nat) : ∀ (l : List Mode), List.Pairwise (fun a b => a ≠ b) l →
    List.Pairwise (fun a b => a ≠ b) (flt j l)
  | [], _ => List.Pairwise.nil
  | k :: l, hp => by
    cases hp with
    | cons hk hl =>
      show List.Pairwise _ (cond (Nat.ble (nrm k) j) (k :: flt j l) (flt j l))
      cases Nat.ble (nrm k) j with
      | true => exact List.Pairwise.cons (fun y hy => hk y (mem_flt j y l hy).1) (flt_pw j l hl)
      | false => exact flt_pw j l hl

theorem incube (j : Nat) (k : Mode) (h : nrm k ≤ j) : InCube j k :=
  ⟨Nat.le_trans (le_mx_left _ _) h,
   Nat.le_trans (Nat.le_trans (le_mx_left _ _) (le_mx_right _ _)) h,
   Nat.le_trans (Nat.le_trans (le_mx_right _ _) (le_mx_right _ _)) h⟩

theorem cnt_cube (l : List Mode) (hd : List.Pairwise (fun a b => a ≠ b) l) (j : Nat) :
    cnt (norms l) j ≤ (2 * j + 1) * ((2 * j + 1) * (2 * j + 1)) := by
  rw [← flt_len j l]
  exact count_cube j (flt j l) (flt_pw j l hd) (fun k hk => incube j k (mem_flt j k l hk).2)

end ModeCount

#print axioms ModeCount.le_mx_left
#print axioms ModeCount.le_mx_right
#print axioms ModeCount.flt_len
#print axioms ModeCount.cnt_cube

ns/NS3DScheme.lean

lean_all/ns/NS3DScheme.lean

/-
  ns/NS3DScheme.lean — T37 cycle 906 (goal G592e): the projected semi-implicit scheme for the Fourier–Galerkin
  truncation of 3D Navier–Stokes on the modes p < N, with an explicit next state, over any ordered field (ℚ included):
    a_{t+1,p} = ((1 − x_p) a_{t,p} + h · P_p N_p(a_t, a_t)) / (1 + x_p)
  (x_p = νh|k_p|²/2 ≥ 0, P_p the Leray projection, N_p the truncated nonlinearity with triad indicator δ and
  coefficient ι). If a_0 is divergence-free and 4T·h²·C·E_0 ≤ 1 with the explicit truncation constant
    C = (Σ_{r<N} (x_r + 2)) · ι² · (Σ_{q<N} |k_q|²) · N,
  then every a_t is divergence-free and E_t ≤ 2E_0 for all t ≤ T.

  Lean 4 core plus ns/Galerkin3D (global3), ns/ProjStep (comb, comb_div, comb_step), ns/GalerkinForce
  (proj_neutral), ns/ForceSum (force_weighted, rl, lsum_rl), ns/RatModel (nn_one').
-/
import ns.Galerkin3D
import ns.ProjStep
import ns.ForceSum

namespace NS3DScheme
open DetChain NSChain DyadicSmallGlobal LibRing FourierGalerkin LerayProj GalerkinForce ProjStep ForceSum Galerkin3D Flatten3

section Gen
variable {R : Type} (K : CRing R) (O : OrdR K)

/-- The scheme. -/
def sch (inv : R → R) (δ : Nat → Nat → Nat → R) (ι h : R) (k : Nat → V3 R) (x κ : Nat → R) (N : Nat)
    (a0 : Nat → V3 R) : Nat → Nat → V3 R
  | 0 => a0
  | t + 1 => fun p => comb K (inv (K.add K.one (x p))) (x p) h (sch inv δ ι h k x κ N a0 t p)
      (proj K (κ p) (k p) (Nl K (rl N) δ ι k (sch inv δ ι h k x κ N a0 t) (sch inv δ ι h k x κ N a0 t) p))

theorem sch_div (inv : R → R) (δ : Nat → Nat → Nat → R) (ι h : R) (k : Nat → V3 R) (x κ : Nat → R) (N : Nat)
    (a0 : Nat → V3 R) (hκk : ∀ p, K.mul (dot3 K (k p) (k p)) (κ p) = K.one)
    (h0 : ∀ p, dot3 K (k p) (a0 p) = K.zero) : ∀ t p, dot3 K (k p) (sch K inv δ ι h k x κ N a0 t p) = K.zero
  | 0, p => h0 p
  | t + 1, p => comb_div K _ (κ p) (x p) h (k p) _ _ (sch_div inv δ ι h k x κ N a0 hκk h0 t p) (hκk p)

theorem castN_nn : ∀ n, O.nn (castN K n)
  | 0 => O.nn_zero
  | n + 1 => O.nn_add _ _ (RatModel.nn_one' K O.toCone) (castN_nn n)

include O

/-- **Global bound for the projected 3D Galerkin scheme.** -/
theorem global (inv : R → R) (mul_inv : ∀ y, y ≠ K.zero → K.mul y (inv y) = K.one) (h10 : K.one ≠ K.zero)
    (δ : Nat → Nat → Nat → R) (ι h : R) (k : Nat → V3 R) (x κ : Nat → R) (N T : Nat) (a0 : Nat → V3 R)
    (hx : ∀ p, O.nn (x p)) (hκ : ∀ p, O.nn (κ p)) (hκk : ∀ p, K.mul (dot3 K (k p) (k p)) (κ p) = K.one)
    (htri : ∀ p q r, δ p q r = K.zero ∨ (δ p q r = K.one ∧ add3 K (k q) (k r) = neg3 K (k p)))
    (hsym : ∀ p q r, δ p q r = δ p r q) (h2 : ∀ y : R, K.add y y = K.zero → y = K.zero)
    (h0 : ∀ p, dot3 K (k p) (a0 p) = K.zero)
    (small : Le K O.toCone (K.mul (castN K (4 * T)) (K.mul (K.mul (K.mul h h)
        (K.mul (sumN K (fun r => K.add (x r) (K.add K.one K.one)) N)
          (K.mul (K.mul (K.mul ι ι) (sumN K (fun q => dot3 K (k q) (k q)) N)) (castN K N))))
        (sumN K (fun p => dot3 K (a0 p) (a0 p)) N))) K.one) :
    ∀ t, t ≤ T → Le K O.toCone (sumN K (fun p => dot3 K (sch K inv δ ι h k x κ N a0 t p) (sch K inv δ ι h k x κ N a0 t p)) N)
      (K.add (sumN K (fun p => dot3 K (a0 p) (a0 p)) N) (sumN K (fun p => dot3 K (a0 p) (a0 p)) N)) := by
  have hδ : ∀ p q r, δ p q r = K.zero ∨ δ p q r = K.one := fun p q r =>
    (htri p q r).elim Or.inl (fun h => Or.inr h.1)
  have hdiv := sch_div K inv δ ι h k x κ N a0 hκk h0
  have hw : ∀ r, O.nn (K.add (x r) (K.add K.one K.one)) := fun r =>
    O.nn_add _ _ (hx r) (O.nn_add _ _ (RatModel.nn_one' K O.toCone) (RatModel.nn_one' K O.toCone))
  let F : Nat → Nat → V3 R := fun t p =>
    proj K (κ p) (k p) (Nl K (rl N) δ ι k (sch K inv δ ι h k x κ N a0 t) (sch K inv δ ι h k x κ N a0 t) p)
  have hC : O.nn (K.mul (sumN K (fun r => K.add (x r) (K.add K.one K.one)) N)
      (K.mul (K.mul (K.mul ι ι) (sumN K (fun q => dot3 K (k q) (k q)) N)) (castN K N))) :=
    O.nn_mul _ _ (sumN_nn K O.toCone _ hw N) (O.nn_mul _ _ (O.nn_mul _ _ (O.nn_sq ι)
      (sumN_nn K O.toCone _ (fun q => ForceBound.dot3_nn K O.toCone (k q)) N)) (castN_nn K O N))
  refine global3 K O h10 (sch K inv δ ι h k x κ N a0) x F h _ N T hx hC ?_ ?_ ?_ small
  · intro t p c hc
    have e := comb_step K O inv mul_inv h10 (x p) h (comp K (sch K inv δ ι h k x κ N a0 t p) c) (comp K (F t p) c) (hx p)
    have ec : comp K (sch K inv δ ι h k x κ N a0 (t + 1) p) c
        = K.mul (K.add (K.mul (K.add K.one (K.neg (x p))) (comp K (sch K inv δ ι h k x κ N a0 t p) c))
          (K.mul h (comp K (F t p) c))) (inv (K.add K.one (x p))) := by
      cases c with
      | zero => rfl
      | succ c => cases c with
        | zero => rfl
        | succ c => cases c with
          | zero => rfl
          | succ c => exact absurd hc (Nat.not_lt_of_le (Nat.le_add_left 3 c))
    rw [ec]; exact e
  · intro t
    rw [← lsum_rl K _ N]
    exact proj_neutral K (rl N) δ ι k (sch K inv δ ι h k x κ N a0 t) κ
      (fun p => by rw [dot3_comm K]; exact hdiv t p) htri hsym h2
  · intro t
    have fw := force_weighted K O.toCone δ ι k (sch K inv δ ι h k x κ N a0 t) κ (fun r => K.add (x r) (K.add K.one K.one)) N
      hδ hκ hκk hw
    generalize sumN K (fun p => dot3 K (sch K inv δ ι h k x κ N a0 t p) (sch K inv δ ι h k x κ N a0 t p)) N = E at fw ⊢
    cases fw with
    | intro d hd =>
      refine ⟨d, hd.1, hd.2.trans ?_⟩
      generalize sumN K (fun r => K.add (x r) (K.add K.one K.one)) N = W
      generalize sumN K (fun q => dot3 K (k q) (k q)) N = Kk
      generalize castN K N = n
      lib_ring

end Gen

end NS3DScheme

#print axioms NS3DScheme.sch_div
#print axioms NS3DScheme.castN_nn
#print axioms NS3DScheme.global

ns/NestExtract.lean

lean_all/ns/NestExtract.lean

/-
  ns/NestExtract.lean — T37 cycle 1746 (goal G852d): finite-depth extraction on nested grids. Given box indices
  b ℓ i < N for levels ℓ and indices i (truncations) in S, if |S| ≥ m·N^d + 1 then some S' ⊆ S with |S'| ≥ m + 1
  has all its members in the same box at every level ℓ < d — by d applications of the generalized pigeonhole.
  With BoxClose and CloseInterp on grids δ_ℓ this is the finite form of the diagonal (Cauchy) extraction.
  Lean 4 core, hand proofs, no axioms.
-/
import ns.PigeonMany

namespace NestExtract
open Pigeon PigeonMany

def filt (p : Nat → Bool) : List Nat → List Nat
  | [] => []
  | x :: l => cond (p x) (x :: filt p l) (filt p l)

theorem mem_filt (p : Nat → Bool) (x : Nat) : ∀ l : List Nat, x ∈ filt p l → x ∈ l ∧ p x = true
  | [], h => nomatch h
  | y :: l, h => by
    cases e : p y with
    | true =>
      have h' : x ∈ y :: filt p l := by
        have : filt p (y :: l) = y :: filt p l := by show cond (p y) _ _ = _; rw [e]; rfl
        rw [this] at h; exact h
      cases h' with
      | head => exact ⟨List.Mem.head l, e⟩
      | tail _ h2 => exact ⟨List.Mem.tail y (mem_filt p x l h2).1, (mem_filt p x l h2).2⟩
    | false =>
      have h' : x ∈ filt p l := by
        have : filt p (y :: l) = filt p l := by show cond (p y) _ _ = _; rw [e]; rfl
        rw [this] at h; exact h
      exact ⟨List.Mem.tail y (mem_filt p x l h').1, (mem_filt p x l h').2⟩

theorem cnt_map (b : Nat → Nat) (v : Nat) : ∀ l : List Nat,
    cnt v (l.map b) = (filt (fun i => Nat.beq v (b i)) l).length
  | [] => rfl
  | y :: l => by
    show cond (Nat.beq v (b y)) 1 0 + cnt v (l.map b) = (cond (Nat.beq v (b y)) (y :: filt _ l) (filt _ l)).length
    rw [cnt_map b v l]
    cases Nat.beq v (b y) with
    | true => show 1 + _ = _ + 1; exact Nat.add_comm _ _
    | false => exact Nat.zero_add _

theorem allLt_map (N : Nat) (b : Nat → Nat) : ∀ l : List Nat, (∀ i, i ∈ l → b i < N) → allLt N (l.map b)
  | [], _ => True.intro
  | y :: l, h => ⟨h y (List.Mem.head l), allLt_map N b l (fun i hi => h i (List.Mem.tail y hi))⟩

theorem len_map (f : Nat → Nat) : ∀ l : List Nat, (l.map f).length = l.length
  | [] => rfl
  | _ :: l => congrArg (· + 1) (len_map f l)

theorem lt_cases {l d : Nat} (h : l < d + 1) : l < d ∨ l = d := by
  cases Nat.lt_or_ge l d with
  | inl hl => exact Or.inl hl
  | inr hg => exact Or.inr (Nat.le_antisymm (Nat.le_of_lt_succ h) hg)

/-- **Nested extraction.** -/
theorem extract (N m : Nat) (b : Nat → Nat → Nat) : ∀ (d : Nat) (S : List Nat),
    (∀ l i, i ∈ S → b l i < N) → m * N ^ d + 1 ≤ S.length →
    ∃ S' : List Nat, (∀ i, i ∈ S' → i ∈ S) ∧ m + 1 ≤ S'.length ∧
      ∀ i j, i ∈ S' → j ∈ S' → ∀ l, l < d → b l i = b l j
  | 0, S, _, hl => ⟨S, fun _ h => h, by
      have e : m * N ^ 0 = m := by show m * 0 + m = m; rw [Nat.mul_zero, Nat.zero_add]
      rw [e] at hl; exact hl,
      fun _ _ _ _ _ h => absurd h (Nat.not_lt_zero _)⟩
  | d + 1, S, hb, hl => by
    have hl' : m * N ^ d * N + 1 ≤ S.length := by
      have e : m * N ^ (d + 1) = m * N ^ d * N := by show m * (N ^ d * N) = m * N ^ d * N; lib_nat
      rw [← e]; exact hl
    cases pigeon_many (m * N ^ d) N (S.map (b d)) (by rw [len_map]; exact hl')
        (allLt_map N (b d) S (fun i hi => hb d i hi)) with
    | intro v hv =>
      rw [cnt_map] at hv
      let S1 := filt (fun i => Nat.beq v (b d i)) S
      have hS1 : ∀ i, i ∈ S1 → i ∈ S ∧ Nat.beq v (b d i) = true := fun i hi => mem_filt _ i S hi
      cases extract N m b d S1 (fun l i hi => hb l i (hS1 i hi).1) hv.2 with
      | intro S' hS' =>
        refine ⟨S', fun i hi => (hS1 i (hS'.1 i hi)).1, hS'.2.1, ?_⟩
        intro i j hi hj l hl2
        cases lt_cases hl2 with
        | inl hlt => exact hS'.2.2 i j hi hj l hlt
        | inr heq =>
          rw [heq]
          have ei := beq_eq _ _ (hS1 i (hS'.1 i hi)).2
          have ej := beq_eq _ _ (hS1 j (hS'.1 j hj)).2
          rw [← ei, ← ej]

end NestExtract

#print axioms NestExtract.mem_filt
#print axioms NestExtract.cnt_map
#print axioms NestExtract.extract

ns/NodupLen.lean

lean_all/ns/NodupLen.lean

/-
  ns/NodupLen.lean — T37 cycle 944 (goal G624c, piece 3): the pigeonhole bound for lists.
  A duplicate-free list whose members all lie in a list L is no longer than L:
    Pairwise (· ≠ ·) l,  (∀ x ∈ l, x ∈ L)  ⟹  l.length ≤ L.length.
  Used to count Fourier modes in a cube of the lattice (|k|_∞ ≤ n gives at most (2n+1)³ distinct modes),
  which turns the weighted sum Σ_p |k_p|^{-4} over any set of distinct modes into a bound independent of N.

  Lean 4 core only (own removal function, hand proofs).
-/

namespace NodupLen

variable {α : Type} [DecidableEq α]

/-- remove the first occurrence of x -/
def rem (x : α) : List α → List α
  | [] => []
  | y :: ys => if y = x then ys else y :: rem x ys

theorem rem_len (x : α) : ∀ (L : List α), x ∈ L → (rem x L).length + 1 = L.length
  | [], h => nomatch h
  | y :: ys, h => by
    show (if y = x then ys else y :: rem x ys).length + 1 = ys.length + 1
    by_cases e : y = x
    · rw [ite_eq_left e]
    · rw [ite_eq_right e]
      have hx : x ∈ ys := by
        cases h with
        | head => exact absurd rfl e
        | tail _ h' => exact h'
      show (rem x ys).length + 1 + 1 = ys.length + 1
      rw [rem_len x ys hx]

theorem mem_rem (x : α) : ∀ (L : List α) (y : α), y ∈ L → y ≠ x → y ∈ rem x L
  | [], _, h, _ => nomatch h
  | z :: zs, y, h, hyx => by
    show y ∈ (if z = x then zs else z :: rem x zs)
    by_cases e : z = x
    · rw [ite_eq_left e]
      cases h with
      | head => exact absurd e hyx
      | tail _ h' => exact h'
    · rw [ite_eq_right e]
      cases h with
      | head => exact List.Mem.head _
      | tail _ h' => exact List.Mem.tail _ (mem_rem x zs y h' hyx)

theorem len_le : ∀ (l L : List α), List.Pairwise (fun a b => a ≠ b) l → (∀ x, x ∈ l → x ∈ L) →
    l.length ≤ L.length
  | [], L, _, _ => Nat.zero_le _
  | x :: xs, L, hp, hs => by
    cases hp with
    | cons hx hxs =>
      have hxL : x ∈ L := hs x (List.Mem.head _)
      have sub : ∀ y, y ∈ xs → y ∈ rem x L := fun y hy =>
        mem_rem x L y (hs y (List.Mem.tail _ hy)) (fun e => hx y hy e.symm)
      have ih := len_le xs (rem x L) hxs sub
      show xs.length + 1 ≤ L.length
      rw [← rem_len x L hxL]
      exact Nat.succ_le_succ ih

end NodupLen

#print axioms NodupLen.rem_len
#print axioms NodupLen.mem_rem
#print axioms NodupLen.len_le

ns/NormCompare.lean

lean_all/ns/NormCompare.lean

/-
  ns/NormCompare.lean — T37 cycle 975 (goal G638, piece b): sup norm against Euclidean norm for integer modes cast
  into a commutative ring with a nonnegative cone. For kz ∈ ℤ³ with ring vector kv = (castZ kz₁, castZ kz₂, castZ kz₃):
    sq_castZ      castZ x · castZ x = castN |x| · castN |x|
    nrm_le_dot    |kz|_∞² ≤ |kv|²                (as ring elements)
    nrm4_le       |kz|_∞⁴ ≤ |kv|⁴
  so H_μ(u) = Σ |kz_p|_∞⁴|u_p|² ≤ Σ |k_p|⁴|u_p|² = ‖Δu‖² in ForceUniform.

  Lean 4 core plus ns/ModeCount (nrm, mx), ns/FourierGalerkin (V3, dot3), ns/ForceSob (le_mul_right').
-/
import ns.ModeCount
import ns.ForceSob

namespace NormCompare
open DetChain NSChain LibRing DyadicLarge FourierGalerkin ModeCount CubeCount

theorem mx_cases (a b : Nat) : mx a b = a ∨ mx a b = b := by
  show cond (Nat.ble a b) b a = a ∨ cond (Nat.ble a b) b a = b
  cases Nat.ble a b with
  | true => exact Or.inr rfl
  | false => exact Or.inl rfl

section Gen
variable {R : Type} (K : CRing R) (P : Cone K)

def kvec (k : Mode) : V3 R := ⟨castZ K k.1, castZ K k.2.1, castZ K k.2.2⟩

theorem sq_castZ (x : Int) : K.mul (castZ K x) (castZ K x) = K.mul (castN K x.natAbs) (castN K x.natAbs) := by
  cases x with
  | ofNat n => rfl
  | negSucc n =>
    show K.mul (K.neg (castN K (n + 1))) (K.neg (castN K (n + 1))) = K.mul (castN K (n + 1)) (castN K (n + 1))
    lib_ring

theorem nrm_le_dot (k : Mode) :
    Le K P (K.mul (castN K (nrm k)) (castN K (nrm k))) (dot3 K (kvec K k) (kvec K k)) := by
  show Le K P _ (K.add (K.add (K.mul (castZ K k.1) (castZ K k.1)) (K.mul (castZ K k.2.1) (castZ K k.2.1)))
    (K.mul (castZ K k.2.2) (castZ K k.2.2)))
  rw [sq_castZ, sq_castZ, sq_castZ]
  generalize hx : k.1.natAbs = x
  generalize hy : k.2.1.natAbs = y
  generalize hz : k.2.2.natAbs = z
  have hn : nrm k = mx x (mx y z) := by rw [← hx, ← hy, ← hz]; rfl
  rw [hn]
  have sx := P.nn_sq (castN K x)
  have sy := P.nn_sq (castN K y)
  have sz := P.nn_sq (castN K z)
  cases mx_cases x (mx y z) with
  | inl e =>
    rw [e]
    exact ⟨K.add (K.mul (castN K y) (castN K y)) (K.mul (castN K z) (castN K z)), P.nn_add _ _ sy sz, by lib_ring⟩
  | inr e =>
    rw [e]
    cases mx_cases y z with
    | inl e2 =>
      rw [e2]
      exact ⟨K.add (K.mul (castN K x) (castN K x)) (K.mul (castN K z) (castN K z)), P.nn_add _ _ sx sz, by lib_ring⟩
    | inr e2 =>
      rw [e2]
      exact ⟨K.add (K.mul (castN K x) (castN K x)) (K.mul (castN K y) (castN K y)), P.nn_add _ _ sx sy, by lib_ring⟩

theorem nrm4_le (k : Mode) :
    Le K P (K.mul (K.mul (castN K (nrm k)) (castN K (nrm k))) (K.mul (castN K (nrm k)) (castN K (nrm k))))
      (K.mul (dot3 K (kvec K k) (kvec K k)) (dot3 K (kvec K k) (kvec K k))) := by
  have h := nrm_le_dot K P k
  have hs := P.nn_sq (castN K (nrm k))
  generalize K.mul (castN K (nrm k)) (castN K (nrm k)) = s at h hs
  generalize dot3 K (kvec K k) (kvec K k) = d at h
  cases h with
  | intro e he =>
    -- (s + e)² = s² + (2se + e²), s = n² ≥ 0
    exact ⟨K.add (K.mul (K.add s s) e) (K.mul e e), P.nn_add _ _ (P.nn_mul _ _ (P.nn_add _ _ hs hs) he.1) (P.nn_sq e),
      by rw [← he.2]; lib_ring⟩

end Gen

end NormCompare

#print axioms NormCompare.sq_castZ
#print axioms NormCompare.nrm_le_dot
#print axioms NormCompare.nrm4_le

ns/Pigeon.lean

lean_all/ns/Pigeon.lean

/-
  ns/Pigeon.lean — T37 cycle 1736 (goal G852a): the discrete core of Bolzano–Weierstrass used as a compactness
  substitute for the continuous-time limit: if f 0, …, f n all lie in {0, …, n − 1} (n boxes of a δ-grid of a
  bounded interval) then two of them coincide. Lean 4 core, hand proofs (bounded Boolean search), no axioms.
-/
namespace Pigeon

/-- Is v among f 0, …, f (k − 1)? -/
def search (f : Nat → Nat) (v : Nat) : Nat → Bool
  | 0 => false
  | k + 1 => search f v k || Nat.beq (f k) v

theorem beq_eq : ∀ a b : Nat, Nat.beq a b = true → a = b
  | 0, 0, _ => rfl
  | 0, _ + 1, h => Bool.noConfusion h
  | _ + 1, 0, h => Bool.noConfusion h
  | a + 1, b + 1, h => congrArg (· + 1) (beq_eq a b h)

theorem beq_refl : ∀ a : Nat, Nat.beq a a = true
  | 0 => rfl
  | a + 1 => beq_refl a

theorem or_cases : ∀ {a b : Bool}, (a || b) = true → a = true ∨ b = true
  | true, _, _ => Or.inl rfl
  | false, _, h => Or.inr h

theorem search_true (f : Nat → Nat) (v : Nat) : ∀ k, search f v k = true → ∃ i, i < k ∧ f i = v
  | 0, h => Bool.noConfusion h
  | k + 1, h => by
    cases or_cases h with
    | inl h1 =>
      cases search_true f v k h1 with
      | intro i hi => exact ⟨i, Nat.lt_succ_of_lt hi.1, hi.2⟩
    | inr h2 => exact ⟨k, Nat.lt_succ_self k, beq_eq _ _ h2⟩

theorem search_false (f : Nat → Nat) (v : Nat) : ∀ k, search f v k = false → ∀ i, i < k → f i ≠ v
  | 0, _, _, hi, _ => absurd hi (Nat.not_lt_zero _)
  | k + 1, h, i, hi, e => by
    cases h1 : search f v k with
    | true =>
      have : search f v (k + 1) = true := by show (search f v k || Nat.beq (f k) v) = true; rw [h1]; rfl
      rw [h] at this; exact Bool.noConfusion this
    | false =>
      cases Nat.lt_or_ge i k with
      | inl hl => exact search_false f v k h1 i hl e
      | inr hg =>
        have hik : i = k := Nat.le_antisymm (Nat.le_of_lt_succ hi) hg
        have hb : Nat.beq (f k) v = true := by rw [← hik, e]; exact beq_refl v
        have : search f v (k + 1) = true := by show (search f v k || Nat.beq (f k) v) = true; rw [h1, hb]; rfl
        rw [h] at this; exact Bool.noConfusion this

theorem lt_of_lt_succ_ne {a n : Nat} (h : a < n + 1) (hn : a ≠ n) : a < n := by
  cases Nat.lt_or_ge a n with
  | inl hl => exact hl
  | inr hg => exact absurd (Nat.le_antisymm (Nat.le_of_lt_succ h) hg) hn

/-- **Pigeonhole.** -/
theorem pigeon : ∀ (n : Nat) (f : Nat → Nat), (∀ i, i ≤ n → f i < n) → ∃ i j, i < j ∧ j ≤ n ∧ f i = f j
  | 0, f, h => absurd (h 0 (Nat.le_refl 0)) (Nat.not_lt_zero _)
  | n + 1, f, h => by
    cases hs : search f (f (n + 1)) (n + 1) with
    | true =>
      cases search_true f _ _ hs with
      | intro i hi => exact ⟨i, n + 1, hi.1, Nat.le_refl _, hi.2⟩
    | false =>
      have hne := search_false f _ _ hs
      let v := f (n + 1)
      let g : Nat → Nat := fun i => cond (Nat.beq (f i) n) v (f i)
      have hg : ∀ i, i ≤ n → g i < n := by
        intro i hi
        have hfi := h i (Nat.le_succ_of_le hi)
        have hv := h (n + 1) (Nat.le_refl _)
        cases e : Nat.beq (f i) n with
        | true =>
          have fin : f i = n := beq_eq _ _ e
          show cond (Nat.beq (f i) n) v (f i) < n
          rw [e]
          show f (n + 1) < n
          exact lt_of_lt_succ_ne hv (fun ev => hne i (Nat.lt_succ_of_le hi) (by rw [fin]; exact ev.symm))
        | false =>
          show cond (Nat.beq (f i) n) v (f i) < n
          rw [e]
          show f i < n
          exact lt_of_lt_succ_ne hfi (fun en => by rw [en, beq_refl] at e; exact Bool.noConfusion e)
      cases pigeon n g hg with
      | intro i hij =>
        cases hij with
        | intro j hj =>
          have hgij : g i = g j := hj.2.2
          refine ⟨i, j, hj.1, Nat.le_succ_of_le hj.2.1, ?_⟩
          cases ei : Nat.beq (f i) n with
          | true =>
            cases ej : Nat.beq (f j) n with
            | true => rw [beq_eq _ _ ei, beq_eq _ _ ej]
            | false =>
              have : v = f j := by
                have a : g i = v := by show cond (Nat.beq (f i) n) v (f i) = v; rw [ei]; rfl
                have b : g j = f j := by show cond (Nat.beq (f j) n) v (f j) = f j; rw [ej]; rfl
                rw [← a, ← b]; exact hgij
              exact absurd this.symm (hne j (Nat.lt_succ_of_le hj.2.1))
          | false =>
            cases ej : Nat.beq (f j) n with
            | true =>
              have : f i = v := by
                have a : g i = f i := by show cond (Nat.beq (f i) n) v (f i) = f i; rw [ei]; rfl
                have b : g j = v := by show cond (Nat.beq (f j) n) v (f j) = v; rw [ej]; rfl
                rw [← a, ← b]; exact hgij
              exact absurd this (hne i (Nat.lt_succ_of_le (Nat.le_of_lt (Nat.lt_of_lt_of_le hj.1 hj.2.1))))
            | false =>
              have a : g i = f i := by show cond (Nat.beq (f i) n) v (f i) = f i; rw [ei]; rfl
              have b : g j = f j := by show cond (Nat.beq (f j) n) v (f j) = f j; rw [ej]; rfl
              rw [← a, ← b]; exact hgij

end Pigeon

#print axioms Pigeon.search_true
#print axioms Pigeon.search_false
#print axioms Pigeon.pigeon

ns/PigeonMany.lean

lean_all/ns/PigeonMany.lean

/-
  ns/PigeonMany.lean — T37 cycle 1744 (goal G852d): generalized pigeonhole, the form needed to iterate the
  extraction on nested grids: a list of m·N + 1 values below N takes some value at least m + 1 times.
  Σ_{v<N} #v(L) = |L| for entries < N, so if every count were ≤ m the sum would be ≤ m·N. The value is found
  by a bounded Boolean search. Lean 4 core, hand proofs, no axioms.
-/
import ns.Pigeon
import lib.Semi

namespace PigeonMany
open Pigeon

def cnt (v : Nat) : List Nat → Nat
  | [] => 0
  | y :: l => cond (Nat.beq v y) 1 0 + cnt v l

/-- Σ_{v < N} g v. -/
def sumB (g : Nat → Nat) : Nat → Nat
  | 0 => 0
  | N + 1 => sumB g N + g N

def allLt (N : Nat) : List Nat → Prop
  | [] => True
  | y :: l => y < N ∧ allLt N l

theorem lt_irr : ∀ m : Nat, ¬ m < m
  | 0, h => Nat.not_lt_zero 0 h
  | m + 1, h => lt_irr m (Nat.lt_of_succ_lt_succ h)

theorem beq_ne {a b : Nat} (h : a ≠ b) : Nat.beq a b = false := by
  cases e : Nat.beq a b with
  | false => rfl
  | true => exact absurd (beq_eq _ _ e) h

theorem delta_zero (y : Nat) : ∀ N, N ≤ y → sumB (fun v => cond (Nat.beq v y) 1 0) N = 0
  | 0, _ => rfl
  | N + 1, h => by
    show sumB (fun v => cond (Nat.beq v y) 1 0) N + cond (Nat.beq N y) 1 0 = 0
    have hne : N ≠ y := fun e => by rw [e] at h; exact lt_irr y h
    rw [delta_zero y N (Nat.le_of_succ_le h), beq_ne hne]
    rfl

theorem delta_one (y : Nat) : ∀ N, y < N → sumB (fun v => cond (Nat.beq v y) 1 0) N = 1
  | 0, h => absurd h (Nat.not_lt_zero _)
  | N + 1, h => by
    show sumB (fun v => cond (Nat.beq v y) 1 0) N + cond (Nat.beq N y) 1 0 = 1
    cases Nat.lt_or_ge y N with
    | inl hl =>
      have hne : N ≠ y := fun e => by rw [e] at hl; exact lt_irr y hl
      rw [delta_one y N hl, beq_ne hne]
      rfl
    | inr hg =>
      have e : y = N := Nat.le_antisymm (Nat.le_of_lt_succ h) hg
      rw [delta_zero y N hg, e, beq_refl]
      rfl

theorem sumB_add (f g : Nat → Nat) : ∀ N, sumB (fun v => f v + g v) N = sumB f N + sumB g N
  | 0 => rfl
  | N + 1 => by
    show sumB (fun v => f v + g v) N + (f N + g N) = (sumB f N + f N) + (sumB g N + g N)
    rw [sumB_add f g N]
    lib_nat

theorem sum_cnt (N : Nat) : ∀ L : List Nat, allLt N L → sumB (fun v => cnt v L) N = L.length
  | [], _ => by
    show sumB (fun _ => 0) N = 0
    induction N with
    | zero => rfl
    | succ N ih => show sumB (fun _ => 0) N + 0 = 0; rw [ih True.intro]
  | y :: l, h => by
    show sumB (fun v => cond (Nat.beq v y) 1 0 + cnt v l) N = l.length + 1
    rw [sumB_add, delta_one y N h.1, sum_cnt N l h.2, Nat.add_comm]

theorem sum_le (g : Nat → Nat) (m : Nat) : ∀ N, (∀ v, v < N → g v ≤ m) → sumB g N ≤ m * N
  | 0, _ => Nat.le_refl 0
  | N + 1, h => by
    show sumB g N + g N ≤ m * (N + 1)
    rw [Nat.mul_succ]
    exact Nat.add_le_add (sum_le g m N (fun v hv => h v (Nat.lt_succ_of_lt hv))) (h N (Nat.lt_succ_self N))

def searchM (L : List Nat) (m : Nat) : Nat → Bool
  | 0 => false
  | N + 1 => searchM L m N || Nat.blt m (cnt N L)

theorem searchM_true (L : List Nat) (m : Nat) : ∀ N, searchM L m N = true → ∃ v, v < N ∧ m + 1 ≤ cnt v L
  | 0, h => Bool.noConfusion h
  | N + 1, h => by
    cases or_cases h with
    | inl h1 =>
      cases searchM_true L m N h1 with
      | intro v hv => exact ⟨v, Nat.lt_succ_of_lt hv.1, hv.2⟩
    | inr h2 => exact ⟨N, Nat.lt_succ_self N, Nat.le_of_ble_eq_true h2⟩

theorem ble_of_le : ∀ a b : Nat, a ≤ b → Nat.ble a b = true
  | 0, _, _ => rfl
  | _ + 1, 0, h => absurd h (Nat.not_succ_le_zero _)
  | a + 1, b + 1, h => ble_of_le a b (Nat.le_of_succ_le_succ h)

theorem searchM_false (L : List Nat) (m : Nat) : ∀ N, searchM L m N = false → ∀ v, v < N → cnt v L ≤ m
  | 0, _, _, hv => absurd hv (Nat.not_lt_zero _)
  | N + 1, h, v, hv => by
    have hN : Nat.blt m (cnt N L) = false := by
      cases e : Nat.blt m (cnt N L) with
      | false => rfl
      | true =>
        have : searchM L m (N + 1) = true := by
          show (searchM L m N || Nat.blt m (cnt N L)) = true; rw [e]; cases searchM L m N <;> rfl
        rw [h] at this; exact Bool.noConfusion this
    have hS : searchM L m N = false := by
      cases e : searchM L m N with
      | false => rfl
      | true =>
        have : searchM L m (N + 1) = true := by show (searchM L m N || Nat.blt m (cnt N L)) = true; rw [e]; rfl
        rw [h] at this; exact Bool.noConfusion this
    cases Nat.lt_or_ge v N with
    | inl hl => exact searchM_false L m N hS v hl
    | inr hg =>
      have e : v = N := Nat.le_antisymm (Nat.le_of_lt_succ hv) hg
      rw [e]
      cases Nat.lt_or_ge (cnt N L) (m + 1) with
      | inl hl => exact Nat.le_of_lt_succ hl
      | inr hg' =>
        have : Nat.blt m (cnt N L) = true := ble_of_le _ _ hg'
        rw [hN] at this; exact Bool.noConfusion this

/-- **Generalized pigeonhole.** -/
theorem pigeon_many (m N : Nat) (L : List Nat) (hl : m * N + 1 ≤ L.length) (hL : allLt N L) :
    ∃ v, v < N ∧ m + 1 ≤ cnt v L := by
  cases e : searchM L m N with
  | true => exact searchM_true L m N e
  | false =>
    have hs := sum_le (fun v => cnt v L) m N (searchM_false L m N e)
    rw [sum_cnt N L hL] at hs
    exact absurd (Nat.le_trans hl hs) (fun h => lt_irr _ (Nat.lt_of_lt_of_le (Nat.lt_succ_self _) h))

end PigeonMany

#print axioms PigeonMany.delta_one
#print axioms PigeonMany.sum_cnt
#print axioms PigeonMany.sum_le
#print axioms PigeonMany.searchM_false
#print axioms PigeonMany.pigeon_many

ns/PigeonVec.lean

lean_all/ns/PigeonVec.lean

/-
  ns/PigeonVec.lean — T37 cycle 1738 (goal G852b): pigeonhole for vectors of k quantised coordinates.
  If F 0, …, F N are lists of length k with entries < n and N = n^k, two of them are equal: base-n code
  (code (a :: as) = a + n · code as) is < n^k and injective, then Pigeon.pigeon. For finitely many (mode, time)
  coordinates of bounded scheme values on a δ-grid: among more than n^k truncations two are δ-close everywhere.
  Lean 4 core, hand proofs, no axioms.
-/
import ns.Pigeon

namespace PigeonVec

def code (n : Nat) : List Nat → Nat
  | [] => 0
  | a :: as => a + n * code n as

/-- All entries < n. -/
def small (n : Nat) : List Nat → Prop
  | [] => True
  | a :: as => a < n ∧ small n as

theorem cancel_left : ∀ T a b : Nat, T + a = T + b → a = b
  | 0, a, b, h => by rw [Nat.zero_add, Nat.zero_add] at h; exact h
  | T + 1, a, b, h => by
    rw [Nat.succ_add, Nat.succ_add] at h
    exact cancel_left T a b (Nat.succ.inj h)

theorem code_lt (n : Nat) : ∀ (k : Nat) (v : List Nat), v.length = k → small n v → code n v < n ^ k
  | 0, [], _, _ => Nat.lt_succ_self 0
  | 0, _ :: _, h, _ => Nat.noConfusion h
  | _ + 1, [], h, _ => Nat.noConfusion h
  | k + 1, a :: as, h, hs => by
    have ih := code_lt n k as (Nat.succ.inj h) hs.2
    show a + n * code n as < n ^ k * n
    have h1 : a + n * code n as < n + n * code n as := Nat.add_lt_add_right hs.1 _
    have h2 : n + n * code n as = n * (code n as + 1) := by rw [Nat.mul_succ, Nat.add_comm]
    have h3 : n * (code n as + 1) ≤ n * n ^ k := Nat.mul_le_mul_left n ih
    rw [h2] at h1
    rw [Nat.mul_comm (n ^ k) n]
    exact Nat.lt_of_lt_of_le h1 h3

/-- a, b < n and x < y: a + n x < b + n y. -/
theorem lt_mix (n a b x y : Nat) (ha : a < n) (hxy : x < y) : a + n * x < b + n * y := by
  have h1 : a + n * x < n + n * x := Nat.add_lt_add_right ha _
  have h2 : n + n * x = n * (x + 1) := by rw [Nat.mul_succ, Nat.add_comm]
  have h3 : n * (x + 1) ≤ n * y := Nat.mul_le_mul_left n hxy
  have h4 : n * y ≤ b + n * y := Nat.le_add_left _ _
  rw [h2] at h1
  exact Nat.lt_of_lt_of_le h1 (Nat.le_trans h3 h4)

theorem lt_irr : ∀ m : Nat, ¬ m < m
  | 0, h => Nat.not_lt_zero 0 h
  | m + 1, h => lt_irr m (Nat.lt_of_succ_lt_succ h)

theorem code_inj (n : Nat) : ∀ u v : List Nat, u.length = v.length → small n u → small n v →
    code n u = code n v → u = v
  | [], [], _, _, _, _ => rfl
  | [], _ :: _, h, _, _, _ => Nat.noConfusion h
  | _ :: _, [], h, _, _, _ => Nat.noConfusion h
  | a :: as, b :: bs, hl, hu, hv, hc => by
    have hc' : a + n * code n as = b + n * code n bs := hc
    have ex : code n as = code n bs := by
      cases Nat.lt_or_ge (code n as) (code n bs) with
      | inl hlt => exact absurd hc' (fun e => lt_irr _ (by
          have := lt_mix n a b _ _ hu.1 hlt; rw [e] at this; exact this))
      | inr hge =>
        cases Nat.lt_or_ge (code n bs) (code n as) with
        | inl hlt => exact absurd hc' (fun e => lt_irr _ (by
            have := lt_mix n b a _ _ hv.1 hlt; rw [← e] at this; exact this))
        | inr hge' => exact Nat.le_antisymm hge' hge
    rw [ex, Nat.add_comm a, Nat.add_comm b] at hc'
    have ea : a = b := cancel_left _ _ _ hc'
    rw [ea, code_inj n as bs (Nat.succ.inj hl) hu.2 hv.2 ex]

/-- **Vector pigeonhole.** -/
theorem pigeon_vec (n k : Nat) (F : Nat → List Nat)
    (hF : ∀ i, i ≤ n ^ k → (F i).length = k ∧ small n (F i)) :
    ∃ i j, i < j ∧ j ≤ n ^ k ∧ F i = F j := by
  cases Pigeon.pigeon (n ^ k) (fun i => code n (F i))
      (fun i hi => code_lt n k (F i) (hF i hi).1 (hF i hi).2) with
  | intro i hij =>
    cases hij with
    | intro j hj =>
      refine ⟨i, j, hj.1, hj.2.1, ?_⟩
      have hi := hF i (Nat.le_of_lt (Nat.lt_of_lt_of_le hj.1 hj.2.1))
      have hj' := hF j hj.2.1
      exact code_inj n (F i) (F j) (by rw [hi.1, hj'.1]) hi.2 hj'.2 hj.2.2

end PigeonVec

#print axioms PigeonVec.code_lt
#print axioms PigeonVec.lt_mix
#print axioms PigeonVec.code_inj
#print axioms PigeonVec.pigeon_vec

ns/PolyBound.lean

lean_all/ns/PolyBound.lean

/-
  ns/PolyBound.lean — T37 cycle 949 (goal G624c, piece 7): the polynomial inequality behind the uniform bound
  of Σ_p |k_p|_∞^{−4}. With a ≥ 1, b = a + 1 and D = b³ + b²a + ba² + a³ (so b⁴ = a⁴ + D):
    four_diff   b⁴ = a⁴ + D
    poly_bound  (a + b)³ · D ≤ 108 · a³ · b³
  from a + b ≤ 3a and D ≤ 4b³; i.e. (2a+1)³(a^{−4} − (a+1)^{−4}) ≤ 108 (a^{−1} − (a+1)^{−1}).
  (The sharp constant is 51 — computed — but its witness identity is too heavy for the kernel; 108 suffices.)

  Lean 4 core plus lib/Semi (lib_nat).
-/
import lib.Semi

namespace PolyBound

theorem four_diff (a : Nat) :
    (a + 1) * (a + 1) * (a + 1) * (a + 1) = a * a * a * a +
      ((a + 1) * (a + 1) * (a + 1) + (a + 1) * (a + 1) * a + (a + 1) * a * a + a * a * a) := by
  lib_nat

theorem mul3 {x y : Nat} (h : x ≤ y) : x * x * x ≤ y * y * y :=
  Nat.mul_le_mul (Nat.mul_le_mul h h) h

theorem poly_bound (a : Nat) (ha : 1 ≤ a) :
    (a + (a + 1)) * (a + (a + 1)) * (a + (a + 1)) *
      ((a + 1) * (a + 1) * (a + 1) + (a + 1) * (a + 1) * a + (a + 1) * a * a + a * a * a)
      ≤ 108 * (a * a * a) * ((a + 1) * (a + 1) * (a + 1)) := by
  have hb : a + 1 ≤ a + a := Nat.add_le_add_left ha a
  have h3 : a + (a + 1) ≤ 3 * a := by
    have : a + (a + 1) ≤ a + (a + a) := Nat.add_le_add_left hb a
    have e : a + (a + a) = 3 * a := by lib_nat
    rw [← e]; exact this
  have hab : a ≤ a + 1 := Nat.le_succ a
  let b := a + 1
  have t1 : (a + 1) * (a + 1) * a ≤ b * b * b := Nat.mul_le_mul_left _ hab
  have t2 : (a + 1) * a * a ≤ b * b * b := Nat.mul_le_mul (Nat.mul_le_mul_left _ hab) hab
  have t3 : a * a * a ≤ b * b * b := mul3 hab
  have hD : (a + 1) * (a + 1) * (a + 1) + (a + 1) * (a + 1) * a + (a + 1) * a * a + a * a * a ≤ 4 * (b * b * b) := by
    have s := Nat.add_le_add (Nat.add_le_add (Nat.add_le_add (Nat.le_refl (b * b * b)) t1) t2) t3
    have e : b * b * b + b * b * b + b * b * b + b * b * b = 4 * (b * b * b) := by lib_nat
    rw [← e]; exact s
  have hC := mul3 h3
  have e2 : 3 * a * (3 * a) * (3 * a) * (4 * (b * b * b)) = 108 * (a * a * a) * (b * b * b) := by lib_nat
  rw [← e2]
  exact Nat.mul_le_mul hC hD

end PolyBound

#print axioms PolyBound.four_diff
#print axioms PolyBound.poly_bound

ns/PowBound.lean

lean_all/ns/PowBound.lean

/-
  ns/PowBound.lean — T37 (goal G859c2h, piece b): a root-free bound for (1 + a)^m, over an ordered ring. For a ≥ 0:
    weier      (1 + a)^m · (1 − m·a) ≤ 1                   (Weierstrass; the step loses (m + 1)a²(1 + a)^m ≥ 0)
    pow_le_two 2·m·a ≤ 1  ⇒  (1 + a)^m ≤ 2
  so (1 + hα/ν)^n ≤ 2^⌈2nhα/ν⌉: the Gronwall factor (ν + hα)^T/ν^T of TruncRateUpto stays bounded uniformly as
  h → 0 with T·h fixed (G859c2h).

  Lean 4 core plus ns/EnstGronwall (pwr).
-/
import ns.EnstGronwall

namespace PowBound
open DetChain NSChain DyadicSmallGlobal LibRing DyadicLarge EnstGronwall

section Gen
variable {R : Type} (K : CRing R) (O : OrdR K)

/-- **(1 + a)^m (1 − m a) ≤ 1** -/
theorem weier (a : R) (ha : O.nn a) : ∀ m : Nat,
    Le K O.toCone (K.mul (pwr K (K.add K.one a) m) (K.add K.one (K.neg (K.mul (castN K m) a)))) K.one
  | 0 => ⟨K.zero, O.nn_zero, by show K.add (K.mul K.one (K.add K.one (K.neg (K.mul K.zero a)))) K.zero = K.one; lib_ring⟩
  | m + 1 => by
    have ih := weier a ha m
    have hP : O.nn (pwr K (K.add K.one a) m) := pwr_nn K O _ (O.nn_add _ _ (RatModel.nn_one' K O.toCone) ha) m
    -- loss term (m + 1) a² (1 + a)^m ≥ 0
    have hL : O.nn (K.mul (K.add K.one (castN K m)) (K.mul (K.mul a a) (pwr K (K.add K.one a) m))) :=
      O.nn_mul _ _ (RatModel.nn_cast K O.toCone (m + 1)) (O.nn_mul _ _ (O.nn_sq a) hP)
    refine le_trans' K O.toCone _ _ _ ⟨_, hL, ?_⟩ ih
    show K.add (K.mul (K.mul (K.add K.one a) (pwr K (K.add K.one a) m))
        (K.add K.one (K.neg (K.mul (K.add K.one (castN K m)) a))))
        (K.mul (K.add K.one (castN K m)) (K.mul (K.mul a a) (pwr K (K.add K.one a) m)))
      = K.mul (pwr K (K.add K.one a) m) (K.add K.one (K.neg (K.mul (castN K m) a)))
    lib_ring

/-- **2ma ≤ 1 ⇒ (1 + a)^m ≤ 2** -/
theorem pow_le_two (a : R) (ha : O.nn a) (m : Nat)
    (hm : Le K O.toCone (K.mul (K.add K.one K.one) (K.mul (castN K m) a)) K.one) :
    Le K O.toCone (pwr K (K.add K.one a) m) (K.add K.one K.one) := by
  have w := weier K O a ha m
  have hP : O.nn (pwr K (K.add K.one a) m) := pwr_nn K O _ (O.nn_add _ _ (RatModel.nn_one' K O.toCone) ha) m
  -- 2ma + d = 1, hence P + P·d = 2·P(1 − ma) ≤ 2
  have ⟨d, hd, e⟩ := hm
  have h2 := le_mul_nn K O.toCone (K.add K.one K.one) _ _
    (O.nn_add _ _ (RatModel.nn_one' K O.toCone) (RatModel.nn_one' K O.toCone)) w
  refine le_trans' K O.toCone _ _ _ (le_trans' K O.toCone _ _ _ ⟨K.mul (pwr K (K.add K.one a) m) d, O.nn_mul _ _ hP hd, ?_⟩ h2)
    ⟨K.zero, O.nn_zero, by lib_ring⟩
  calc K.add (pwr K (K.add K.one a) m) (K.mul (pwr K (K.add K.one a) m) d)
      = K.add (K.mul (K.add K.one K.one) (K.mul (pwr K (K.add K.one a) m) (K.add K.one (K.neg (K.mul (castN K m) a)))))
          (K.mul (pwr K (K.add K.one a) m) (K.add (K.add (K.mul (K.add K.one K.one) (K.mul (castN K m) a)) d)
            (K.neg K.one))) := by lib_ring
    _ = K.mul (K.add K.one K.one) (K.mul (pwr K (K.add K.one a) m) (K.add K.one (K.neg (K.mul (castN K m) a)))) := by
          rw [e]; lib_ring

end Gen

end PowBound

#print axioms PowBound.weier
#print axioms PowBound.pow_le_two

ns/PowFactor.lean

lean_all/ns/PowFactor.lean

/-
  ns/PowFactor.lean — T37 (goal G859c2h, piece c): the Gronwall factor of TruncRateUpto split off, over an ordered
  field with inverses. With ν·ν⁻¹ = 1:
    pwr_factor   (ν + hα)^T = ν^T·(1 + hαν⁻¹)^T,
    pwr_mul      (x)^(T₁ + T₂) = x^T₁·x^T₂,
  so with PowBound.pow_le_two, (ν + hα)^T ≤ ν^T·2^k whenever T is cut into k pieces of length m with 2m·hαν⁻¹ ≤ 1 —
  a bound depending only on T·h, uniform as h → 0.

  Lean 4 core plus ns/EnstGronwall (pwr).
-/
import ns.EnstGronwall

namespace PowFactor
open DetChain NSChain DyadicSmallGlobal LibRing DyadicLarge EnstGronwall

section Gen
variable {R : Type} (K : CRing R)

theorem pwr_factor (ν hα iν : R) (hν : K.mul ν iν = K.one) : ∀ T : Nat,
    pwr K (K.add ν hα) T = K.mul (pwr K ν T) (pwr K (K.add K.one (K.mul hα iν)) T)
  | 0 => by show K.one = K.mul K.one K.one; lib_ring
  | T + 1 => by
    show K.mul (K.add ν hα) (pwr K (K.add ν hα) T)
      = K.mul (K.mul ν (pwr K ν T)) (K.mul (K.add K.one (K.mul hα iν)) (pwr K (K.add K.one (K.mul hα iν)) T))
    rw [pwr_factor ν hα iν hν T]
    calc K.mul (K.add ν hα) (K.mul (pwr K ν T) (pwr K (K.add K.one (K.mul hα iν)) T))
        = K.add (K.mul ν (K.mul (pwr K ν T) (pwr K (K.add K.one (K.mul hα iν)) T)))
            (K.mul hα (K.mul (pwr K ν T) (pwr K (K.add K.one (K.mul hα iν)) T))) := by lib_ring
      _ = K.add (K.mul ν (K.mul (pwr K ν T) (pwr K (K.add K.one (K.mul hα iν)) T)))
            (K.mul (K.mul hα (K.mul ν iν)) (K.mul (pwr K ν T) (pwr K (K.add K.one (K.mul hα iν)) T))) := by
          rw [hν]; lib_ring
      _ = _ := by lib_ring

theorem pwr_mul (x : R) (T₁ : Nat) : ∀ T₂ : Nat, pwr K x (T₁ + T₂) = K.mul (pwr K x T₁) (pwr K x T₂)
  | 0 => by show pwr K x T₁ = K.mul (pwr K x T₁) K.one; lib_ring
  | T₂ + 1 => by
    show K.mul x (pwr K x (T₁ + T₂)) = K.mul (pwr K x T₁) (K.mul x (pwr K x T₂))
    rw [pwr_mul x T₁ T₂]; lib_ring

end Gen

end PowFactor

#print axioms PowFactor.pwr_factor
#print axioms PowFactor.pwr_mul

ns/PowOne.lean

lean_all/ns/PowOne.lean

/-
  ns/PowOne.lean — T37 (goal G859c2-s3b, second piece): powers of a factor ≥ 1 are monotone in the exponent, so the
  window factor c_k = ((ν + hα)·ν⁻¹)^{n_k} of a window of length n_k ≤ n is at most the factor of length n — a uniform
  c for the variable-length window chain.
    one_le_mul     1 ≤ x, 1 ≤ y ⇒ 1 ≤ x·y
    one_le_pwr     1 ≤ x ⇒ 1 ≤ x^k
    pwr_le_add     1 ≤ x ⇒ x^a ≤ x^{a+k}
    pwr_le_of_le   1 ≤ x, a ≤ b ⇒ x^a ≤ x^b
    factor_le      1 ≤ (ν + hα)·ν⁻¹, n_k ≤ n ⇒ (ν + hα)^{n_k}(ν⁻¹)^{n_k} ≤ ((ν + hα)·ν⁻¹)^n
  Lean 4 core plus ns/DyadicCoef, ns/Dyadic, ns/BadRun.
-/
import ns.DyadicCoef
import ns.BadRun

namespace PowOne
open DetChain NSChain DyadicSmallGlobal LibRing DyadicLarge EnstGronwall

section Ring
variable {R : Type} (K : CRing R) (O : OrdR K)

theorem one_le_mul (x y : R) (hx : Le K O.toCone K.one x) (hy : Le K O.toCone K.one y) :
    Le K O.toCone K.one (K.mul x y) := by
  obtain ⟨a, ha, ea⟩ := hx
  obtain ⟨b, hb, eb⟩ := hy
  refine ⟨K.add (K.add a b) (K.mul a b), O.nn_add _ _ (O.nn_add _ _ ha hb) (O.nn_mul _ _ ha hb), ?_⟩
  rw [← ea, ← eb]
  lib_ring

theorem one_le_pwr (x : R) (hx : Le K O.toCone K.one x) : ∀ k, Le K O.toCone K.one (pwr K x k)
  | 0 => le_refl' K O.toCone _
  | k + 1 => one_le_mul K O x (pwr K x k) hx (one_le_pwr x hx k)

theorem pwr_le_add (x : R) (hx : Le K O.toCone K.one x) (a k : Nat) :
    Le K O.toCone (pwr K x a) (pwr K x (a + k)) := by
  obtain ⟨d, hd, ed⟩ := one_le_pwr K O x hx k
  obtain ⟨e, he, ee⟩ := one_le_pwr K O x hx a
  refine ⟨K.mul (pwr K x a) d, O.nn_mul _ _ ?_ hd, ?_⟩
  · rw [← ee]; exact O.nn_add _ _ (RatModel.nn_one' K O.toCone) he
  · rw [DyadicCoef.pwr_add K x a k, ← ed]
    lib_ring

theorem pwr_le_of_le (x : R) (hx : Le K O.toCone K.one x) (a b : Nat) (hab : a ≤ b) :
    Le K O.toCone (pwr K x a) (pwr K x b) := by
  obtain ⟨k, hk⟩ := BadRun.le_split hab
  rw [hk]
  exact pwr_le_add K O x hx a k

theorem factor_le (u iν : R) (hu : Le K O.toCone K.one (K.mul u iν)) (nk n : Nat) (hn : nk ≤ n) :
    Le K O.toCone (K.mul (pwr K u nk) (pwr K iν nk)) (pwr K (K.mul u iν) n) := by
  rw [Dyadic.pwr_mul K u iν nk]
  exact pwr_le_of_le K O _ hu nk n hn

end Ring

end PowOne

#print axioms PowOne.one_le_mul
#print axioms PowOne.one_le_pwr
#print axioms PowOne.pwr_le_add
#print axioms PowOne.pwr_le_of_le
#print axioms PowOne.factor_le

ns/ProjStep.lean

lean_all/ns/ProjStep.lean

/-
  ns/ProjStep.lean — T37 cycle 878 (goal G592): the projected semi-implicit step on one 3D Fourier mode, over an
  ordered field (ℚ included). For a divergence-free a (k·a = 0), x ≥ 0, a forcing F and ι = (1 + x)⁻¹:
    a' = ((1 − x)·a + h·P F)·ι          (P the Leray projection; P a = a)
  satisfies k·a' = 0 and, componentwise, a'_c − a_c = −x(a'_c + a_c) + h(P F)_c (ForcedCN). So the 3D semi-implicit
  Galerkin scheme with projected forcing keeps every mode divergence-free and is of SemiGeneral's form with the
  neutral forcing P F (LerayProj2.proj_test).

  Lean 4 core plus ns/LerayProj2, ns/ForcedCN (forced_exist), lib/RingTac.
-/
import ns.LerayProj2
import ns.ForcedCN

namespace ProjStep
open DetChain NSChain DyadicSmallGlobal LibRing FourierGalerkin LerayProj LerayProj2 ForcedCN

section Gen
variable {R : Type} (K : CRing R)

def comb (ι x h : R) (a G : V3 R) : V3 R :=
  ⟨K.mul (K.add (K.mul (K.add K.one (K.neg x)) a.x) (K.mul h G.x)) ι,
   K.mul (K.add (K.mul (K.add K.one (K.neg x)) a.y) (K.mul h G.y)) ι,
   K.mul (K.add (K.mul (K.add K.one (K.neg x)) a.z) (K.mul h G.z)) ι⟩

theorem comb_div (ι κ x h : R) (k a F : V3 R) (ha : dot3 K k a = K.zero) (hκ : K.mul (dot3 K k k) κ = K.one) :
    dot3 K k (comb K ι x h a (proj K κ k F)) = K.zero := by
  have hp := div_free K κ k F hκ
  have t : dot3 K k (comb K ι x h a (proj K κ k F))
      = K.mul (K.add (K.mul (K.add K.one (K.neg x)) (dot3 K k a)) (K.mul h (dot3 K k (proj K κ k F)))) ι := by
    generalize proj K κ k F = G
    cases k with
    | mk kx ky kz => cases a with
      | mk ax ay az => cases G with
        | mk gx gy gz =>
          show K.add (K.add (K.mul kx (K.mul (K.add (K.mul (K.add K.one (K.neg x)) ax) (K.mul h gx)) ι))
              (K.mul ky (K.mul (K.add (K.mul (K.add K.one (K.neg x)) ay) (K.mul h gy)) ι)))
              (K.mul kz (K.mul (K.add (K.mul (K.add K.one (K.neg x)) az) (K.mul h gz)) ι))
            = K.mul (K.add (K.mul (K.add K.one (K.neg x)) (K.add (K.add (K.mul kx ax) (K.mul ky ay)) (K.mul kz az)))
              (K.mul h (K.add (K.add (K.mul kx gx) (K.mul ky gy)) (K.mul kz gz)))) ι
          lib_ring
  rw [t, ha, hp]; lib_ring

variable (O : OrdR K)
include O

theorem comb_step (inv : R → R) (mul_inv : ∀ y, y ≠ K.zero → K.mul y (inv y) = K.one) (h10 : K.one ≠ K.zero)
    (x h ac Gc : R) (hx : O.nn x) :
    K.add (K.mul (K.add (K.mul (K.add K.one (K.neg x)) ac) (K.mul h Gc)) (inv (K.add K.one x))) (K.neg ac)
      = K.add (K.neg (K.mul x (K.add (K.mul (K.add (K.mul (K.add K.one (K.neg x)) ac) (K.mul h Gc)) (inv (K.add K.one x))) ac)))
        (K.mul h Gc) :=
  forced_exist K O inv mul_inv h10 ac x (K.mul h Gc) hx

end Gen

end ProjStep

#print axioms ProjStep.comb_div
#print axioms ProjStep.comb_step

ns/QeComp.lean

lean_all/ns/QeComp.lean

/-
  ns/QeComp.lean — T37 cycle 1896 (goal G860e, piece b1): the step-size distance Qe controls each coefficient
  component: for r ∈ l with |k_r|² ≥ 1,
    qe_comp_x/y/z   ν·(u_r.x − v_r.x)² ≤ Qe(u, v)     (likewise y, z)
    div_nu          ν·y ≤ X, ν·iν = 1, iν ≥ 0  ⇒  y ≤ iν·X
  so a level-to-level bound Qe ≤ X·hʲ (StepUniformS) gives the squared component bounds (iν·X)·hʲ needed by
  RiemannInst for the dissipation term A.
  Lean 4 core plus ns/StepCauchy, ns/RealCoef.
-/
import ns.StepCauchy
import ns.RealCoef

namespace QeComp
open DetChain NSChain DyadicSmallGlobal LibRing DyadicLarge FourierGalerkin ForceBound EnstDiff EnstGalerkin ForceDiff
  StepCauchy RealCoef DyadicCoef ModeCount CubeCount

section Gen
variable {R : Type} (K : CRing R) (O : OrdR K) {I : Type}

theorem qe_term (ν k : R) (hν : O.nn ν) (hk : O.nn k) (l : List I) (kz : I → Mode) (u v : I → V3 R) (r : I)
    (hr : r ∈ l) (hw : Le K O.toCone K.one (wt K kz r)) :
    Le K O.toCone (K.mul ν (dot3 K (dF K u v r) (dF K u v r))) (Qe K ν k l kz u v) := by
  have a := le_mu K O (wt K kz r) _ hw (dot3_nn K O.toCone (dF K u v r))
  have b := term_le K O (fun r => K.mul (wt K kz r) (dot3 K (dF K u v r) (dF K u v r)))
    (fun r => O.nn_mul _ _ (dot3_nn K O.toCone _) (dot3_nn K O.toCone _)) l r hr
  have c := le_mul_nn K O.toCone ν _ _ hν (le_trans' K O.toCone _ _ _ a b)
  refine le_trans' K O.toCone _ _ _ c ⟨_, O.nn_mul _ _ (O.nn_mul _ _ hk (O.nn_sq ν))
    (lsum_nn K O.toCone _ (fun r => O.nn_mul _ _ (O.nn_sq _) (dot3_nn K O.toCone _)) l), rfl⟩

theorem qe_comp (ν k : R) (hν : O.nn ν) (hk : O.nn k) (l : List I) (kz : I → Mode) (u v : I → V3 R) (r : I)
    (hr : r ∈ l) (hw : Le K O.toCone K.one (wt K kz r)) (f : V3 R → R)
    (hfc : ∀ w, Le K O.toCone (K.mul (f w) (f w)) (dot3 K w w)) :
    Le K O.toCone (K.mul ν (K.mul (f (dF K u v r)) (f (dF K u v r)))) (Qe K ν k l kz u v) :=
  le_trans' K O.toCone _ _ _ (le_mul_nn K O.toCone ν _ _ hν (hfc _)) (qe_term K O ν k hν hk l kz u v r hr hw)

theorem qe_comp_x (ν k : R) (hν : O.nn ν) (hk : O.nn k) (l : List I) (kz : I → Mode) (u v : I → V3 R) (r : I)
    (hr : r ∈ l) (hw : Le K O.toCone K.one (wt K kz r)) :
    Le K O.toCone (K.mul ν (K.mul (sub K (u r).x (v r).x) (sub K (u r).x (v r).x))) (Qe K ν k l kz u v) :=
  qe_comp K O ν k hν hk l kz u v r hr hw (fun w => w.x) (comp_x K O)

theorem qe_comp_y (ν k : R) (hν : O.nn ν) (hk : O.nn k) (l : List I) (kz : I → Mode) (u v : I → V3 R) (r : I)
    (hr : r ∈ l) (hw : Le K O.toCone K.one (wt K kz r)) :
    Le K O.toCone (K.mul ν (K.mul (sub K (u r).y (v r).y) (sub K (u r).y (v r).y))) (Qe K ν k l kz u v) :=
  qe_comp K O ν k hν hk l kz u v r hr hw (fun w => w.y) (comp_y K O)

theorem qe_comp_z (ν k : R) (hν : O.nn ν) (hk : O.nn k) (l : List I) (kz : I → Mode) (u v : I → V3 R) (r : I)
    (hr : r ∈ l) (hw : Le K O.toCone K.one (wt K kz r)) :
    Le K O.toCone (K.mul ν (K.mul (sub K (u r).z (v r).z) (sub K (u r).z (v r).z))) (Qe K ν k l kz u v) :=
  qe_comp K O ν k hν hk l kz u v r hr hw (fun w => w.z) (comp_z K O)

theorem div_nu (ν iν y X : R) (hi : O.nn iν) (hνi : K.mul ν iν = K.one) (h : Le K O.toCone (K.mul ν y) X) :
    Le K O.toCone y (K.mul iν X) := by
  have a := le_mul_nn K O.toCone iν _ _ hi h
  rw [show K.mul iν (K.mul ν y) = K.mul (K.mul ν iν) y by lib_ring, hνi, show K.mul K.one y = y by lib_ring] at a
  exact a

end Gen
end QeComp

#print axioms QeComp.qe_term
#print axioms QeComp.qe_comp_x
#print axioms QeComp.qe_comp_y
#print axioms QeComp.qe_comp_z
#print axioms QeComp.div_nu

ns/QeTri.lean

lean_all/ns/QeTri.lean

/-
  ns/QeTri.lean — T37 cycle 1135 (goal G740): one norm for the truncation and step-size comparisons.
  Over any commutative ring with a nonnegative cone:
    qe_tri     Qe(u, w) ≤ 2·Qe(u, v) + 2·Qe(v, w)        (|x + y|² ≤ 2|x|² + 2|y|² per mode)
    q_eq_qe    the quantity Q(νZl, hν²Al) of TruncRate/TruncCauchy at time T is Qe(restr A_T, B_T) on the low modes
  so a solution (truncation N, step k) and the refined one (truncation N', step k/2ʲ) are compared by splitting at the
  intermediate solution (truncation N', step k): truncation part from TruncRate, step part from StepCauchy/Dyadic.

  Lean 4 core plus ns/StepCauchy (Qe, dF_split), ns/TruncCauchy (Zl, Al), ns/ForceTwoScale (sq2).
-/
import ns.StepCauchy
import ns.TruncCauchy

namespace QeTri
open DetChain NSChain DyadicSmallGlobal LibRing DyadicLarge FourierGalerkin GalerkinForce ForceBound ForceSob
  ModeCount CubeCount ForceUniform NormCompare EnstStep EnstGlobal EnstGalerkin ForceTwoScale ForceBilin EnstDiff
  ForceDiff StepCauchy

section Gen
variable {R : Type} (K : CRing R) (O : OrdR K) {I : Type}

theorem qe_tri (ν k : R) (hν : O.nn ν) (hk : O.nn k) (l : List I) (kz : I → Mode) (u v w : I → V3 R) :
    Le K O.toCone (Qe K ν k l kz u w)
      (K.add (K.mul (K.add K.one K.one) (Qe K ν k l kz u v)) (K.mul (K.add K.one K.one) (Qe K ν k l kz v w))) := by
  have hw : ∀ r, O.nn (wt K kz r) := fun r => dot3_nn K O.toCone _
  have two : O.nn (K.add K.one K.one) := O.nn_add _ _ (RatModel.nn_one' K O.toCone) (RatModel.nn_one' K O.toCone)
  have pt : ∀ (μ : I → R), (∀ r, O.nn (μ r)) →
      Le K O.toCone (lsum K (fun r => K.mul (μ r) (dot3 K (dF K u w r) (dF K u w r))) l)
        (K.add (K.mul (K.add K.one K.one) (lsum K (fun r => K.mul (μ r) (dot3 K (dF K u v r) (dF K u v r))) l))
          (K.mul (K.add K.one K.one) (lsum K (fun r => K.mul (μ r) (dot3 K (dF K v w r) (dF K v w r))) l))) := by
    intro μ hμ
    have t := lsum_le K O.toCone _ _ (fun r => le_mul_nn K O.toCone (μ r) _ _ (hμ r) (sq2 K O (dF K v w r) (dF K u v r))) l
    have e : lsum K (fun r => K.mul (μ r) (dot3 K (dF K u w r) (dF K u w r))) l
        = lsum K (fun r => K.mul (μ r) (dot3 K (add3 K (dF K v w r) (dF K u v r)) (add3 K (dF K v w r) (dF K u v r)))) l :=
      lsum_ext K _ _ (fun r => by rw [dF_split K u v w r]) l
    rw [e]
    refine le_trans' K O.toCone _ _ _ t ⟨K.zero, O.nn_zero, ?_⟩
    rw [add_zero', ← lsum_mul_left, ← lsum_mul_left, ← lsum_add]
    exact lsum_ext K _ _ (fun r => by lib_ring) l
  have a := pt (wt K kz) hw
  have b := pt (fun r => K.mul (wt K kz r) (wt K kz r)) (fun r => O.nn_sq _)
  have s := le_add' K O.toCone _ _ _ _ (le_mul_nn K O.toCone ν _ _ hν a)
    (le_mul_nn K O.toCone (K.mul k (K.mul ν ν)) _ _ (O.nn_mul _ _ hk (O.nn_sq ν)) b)
  unfold Qe
  refine le_trans' K O.toCone _ _ _ s ⟨K.zero, O.nn_zero, ?_⟩
  generalize lsum K (fun r => K.mul (wt K kz r) (dot3 K (dF K u v r) (dF K u v r))) l = Z1
  generalize lsum K (fun r => K.mul (wt K kz r) (dot3 K (dF K v w r) (dF K v w r))) l = Z2
  generalize lsum K (fun r => K.mul (K.mul (wt K kz r) (wt K kz r)) (dot3 K (dF K u v r) (dF K u v r))) l = A1
  generalize lsum K (fun r => K.mul (K.mul (wt K kz r) (wt K kz r)) (dot3 K (dF K v w r) (dF K v w r))) l = A2
  lib_ring

theorem q_eq_qe (ν h : R) (L : List I) (kz : I → Mode) (A B : Nat → I → V3 R) (T : Nat) :
    Q K ν h (fun t => TruncCauchy.Zl K kz L (dF K (A t) (B t))) (fun t => TruncCauchy.Al K kz L (dF K (A t) (B t))) T
      = Qe K ν h L kz (A T) (B T) := rfl

end Gen

end QeTri

#print axioms QeTri.qe_tri
#print axioms QeTri.q_eq_qe

ns/QuadBound.lean

lean_all/ns/QuadBound.lean

/-
  ns/QuadBound.lean — T37 cycle 865 (goal G574): any quadratic nonlinearity N_n(a) = Σ_{p,q<N} T_{npq} a_p a_q (every
  Galerkin truncation, 3D Fourier–Galerkin included, after flattening modes and components to n < N) is controlled by
  the energy, over any ordered ring (ℚ included), by Cauchy–Schwarz (lib/CauchySchwarz):
    N_n(a)² ≤ (Σ_{p,q} T_{npq}²) · E(a)²,     Σ_n (x_n + 2) N_n(a)² ≤ C · E(a)²,   C = Σ_n (x_n + 2) Σ_{p,q} T_{npq}².
  With energy neutrality this is exactly SemiGeneral's hypothesis: the semi-implicit Galerkin scheme over ℚ has an
  explicit solution with E_t ≤ 2E_0 for 4T·h²·C·E_0 ≤ 1.

  Lean 4 core plus lib/CauchySchwarz (sA, sB, sC, cauchy_schwarz), ns/SemiGeneral, ns/DyadicLarge, lib/RingTac.
    row, grid     the list of pairs (T_{npq}, a_p a_q)
    sB_grid       Σ (a_p a_q)² = E²
    sA_indep      Σ T² does not depend on the state
    quad_sq       N_n² ≤ (ΣT²)·E²
    quad_nb       the weighted bound
    quad_semi     the semi-implicit Galerkin scheme: E_t ≤ 2E_0
-/
import lib.CauchySchwarz
import ns.SemiGeneral

namespace QuadBound
open DetChain NSChain DyadicSmallGlobal LibRing DyadicLarge LibCS

section Gen
variable {R : Type} (K : CRing R)

def row (T : Nat → Nat → Nat → R) (a : Nat → R) (n p : Nat) : Nat → List (R × R)
  | 0 => []
  | q + 1 => (T n p q, K.mul (a p) (a q)) :: row T a n p q

def grid (T : Nat → Nat → Nat → R) (a : Nat → R) (n N : Nat) : Nat → List (R × R)
  | 0 => []
  | p + 1 => row K T a n p N ++ grid T a n N p

def Nq (T : Nat → Nat → Nat → R) (N : Nat) (a : Nat → R) (n : Nat) : R := sC K (grid K T a n N N)

theorem sB_app : ∀ l1 l2 : List (R × R), sB K (l1 ++ l2) = K.add (sB K l1) (sB K l2)
  | [], l2 => by show sB K l2 = K.add K.zero (sB K l2); lib_ring
  | p :: l1, l2 => by
    show K.add (K.mul p.2 p.2) (sB K (l1 ++ l2)) = K.add (K.add (K.mul p.2 p.2) (sB K l1)) (sB K l2)
    rw [sB_app l1 l2]; lib_ring

theorem sA_app : ∀ l1 l2 : List (R × R), sA K (l1 ++ l2) = K.add (sA K l1) (sA K l2)
  | [], l2 => by show sA K l2 = K.add K.zero (sA K l2); lib_ring
  | p :: l1, l2 => by
    show K.add (K.mul p.1 p.1) (sA K (l1 ++ l2)) = K.add (K.add (K.mul p.1 p.1) (sA K l1)) (sA K l2)
    rw [sA_app l1 l2]; lib_ring

theorem sB_row (T : Nat → Nat → Nat → R) (a : Nat → R) (n p : Nat) :
    ∀ M, sB K (row K T a n p M) = K.mul (K.mul (a p) (a p)) (sumN K (fun q => K.mul (a q) (a q)) M)
  | 0 => by show K.zero = K.mul _ K.zero; lib_ring
  | M + 1 => by
    show K.add (K.mul (K.mul (a p) (a M)) (K.mul (a p) (a M))) (sB K (row K T a n p M))
      = K.mul (K.mul (a p) (a p)) (K.add (sumN K (fun q => K.mul (a q) (a q)) M) (K.mul (a M) (a M)))
    rw [sB_row T a n p M]; lib_ring

theorem sB_grid (T : Nat → Nat → Nat → R) (a : Nat → R) (n N : Nat) :
    ∀ P, sB K (grid K T a n N P) = K.mul (sumN K (fun p => K.mul (a p) (a p)) P) (sumN K (fun q => K.mul (a q) (a q)) N)
  | 0 => by show K.zero = K.mul K.zero _; lib_ring
  | P + 1 => by
    show sB K (row K T a n P N ++ grid K T a n N P) = _
    rw [sB_app, sB_row, sB_grid T a n N P]
    show _ = K.mul (K.add (sumN K (fun p => K.mul (a p) (a p)) P) (K.mul (a P) (a P))) _
    lib_ring

theorem sA_row (T : Nat → Nat → Nat → R) (a b : Nat → R) (n p : Nat) : ∀ M, sA K (row K T a n p M) = sA K (row K T b n p M)
  | 0 => rfl
  | M + 1 => by
    show K.add (K.mul (T n p M) (T n p M)) (sA K (row K T a n p M)) = K.add (K.mul (T n p M) (T n p M)) (sA K (row K T b n p M))
    rw [sA_row T a b n p M]

theorem sA_indep (T : Nat → Nat → Nat → R) (a b : Nat → R) (n N : Nat) : ∀ P, sA K (grid K T a n N P) = sA K (grid K T b n N P)
  | 0 => rfl
  | P + 1 => by
    show sA K (row K T a n P N ++ grid K T a n N P) = sA K (row K T b n P N ++ grid K T b n N P)
    rw [sA_app, sA_app, sA_row K T a b, sA_indep T a b n N P]

variable (O : OrdR K)
include O

def posO : Pos K := ⟨O.nn, O.nn_sq, O.nn_add, O.nn_mul, O.nn_zero⟩

theorem quad_sq (T : Nat → Nat → Nat → R) (a : Nat → R) (n N : Nat) :
    Le K O.toCone (K.mul (Nq K T N a n) (Nq K T N a n))
      (K.mul (sA K (grid K T a n N N)) (K.mul (sumN K (fun p => K.mul (a p) (a p)) N) (sumN K (fun p => K.mul (a p) (a p)) N))) := by
  have cs := cauchy_schwarz K (posO K O) (grid K T a n N N)
  rw [sB_grid] at cs
  exact ⟨_, cs, by unfold Nq sub; lib_ring⟩

theorem quad_nb (T : Nat → Nat → Nat → R) (x : Nat → R) (hx : ∀ n, O.nn (x n)) (N : Nat) (a : Nat → R) :
    Le K O.toCone (sumN K (fun n => K.mul (K.add (x n) (K.add K.one K.one)) (K.mul (Nq K T N a n) (Nq K T N a n))) N)
      (K.mul (sumN K (fun n => K.mul (K.add (x n) (K.add K.one K.one)) (sA K (grid K T (fun _ => K.zero) n N N))) N)
        (K.mul (sumN K (fun p => K.mul (a p) (a p)) N) (sumN K (fun p => K.mul (a p) (a p)) N))) := by
  have pt : ∀ n, Le K O.toCone (K.mul (K.add (x n) (K.add K.one K.one)) (K.mul (Nq K T N a n) (Nq K T N a n)))
      (K.mul (K.mul (K.add (x n) (K.add K.one K.one)) (sA K (grid K T (fun _ => K.zero) n N N)))
        (K.mul (sumN K (fun p => K.mul (a p) (a p)) N) (sumN K (fun p => K.mul (a p) (a p)) N))) := by
    intro n
    have q := quad_sq K O T a n N
    rw [sA_indep K T a (fun _ => K.zero)] at q
    have l := le_mul_nn K O.toCone (K.add (x n) (K.add K.one K.one)) _ _
      (O.nn_add _ _ (hx n) (O.nn_add _ _ (RatModel.nn_one' K O.toCone) (RatModel.nn_one' K O.toCone))) q
    cases l with
    | intro d hd => exact ⟨d, hd.1, hd.2.trans (by lib_ring)⟩
  generalize hE : sumN K (fun p => K.mul (a p) (a p)) N = E at pt ⊢
  have sm := sum_le K O.toCone _ _ pt N
  cases sm with
  | intro d hd =>
    refine ⟨d, hd.1, hd.2.trans ?_⟩
    rw [K.mul_comm (sumN K (fun n => K.mul (K.add (x n) (K.add K.one K.one)) (sA K (grid K T (fun _ => K.zero) n N N))) N) (K.mul E E),
      ← sumN_mul_left]
    apply DyUnique.sumN_ext_g; intro n _; lib_ring

end Gen

theorem quad_semi {R : Type} (K : CRing R) (O : OrdR K) (inv : R → R)
    (mul_inv : ∀ y, y ≠ K.zero → K.mul y (inv y) = K.one) (h10 : K.one ≠ K.zero)
    (T : Nat → Nat → Nat → R) (x : Nat → R) (h : R) (N S : Nat) (b : Nat → R) (hx : ∀ n, O.nn (x n))
    (neutral : ∀ a : Nat → R, sumN K (fun n => K.mul (a n) (Nq K T N a n)) N = K.zero)
    (small : Le K O.toCone (K.mul (castN K (4 * S)) (K.mul (K.mul (K.mul h h)
      (sumN K (fun n => K.mul (K.add (x n) (K.add K.one K.one)) (sA K (grid K T (fun _ => K.zero) n N N))) N))
      (sumN K (fun n => K.mul (b n) (b n)) N))) K.one) :
    ∀ t, t ≤ S → Le K O.toCone (sumN K (fun n => K.mul (SemiGeneral.sq K inv (Nq K T N) x h N b t n) (SemiGeneral.sq K inv (Nq K T N) x h N b t n)) N)
      (K.add (sumN K (fun n => K.mul (b n) (b n)) N) (sumN K (fun n => K.mul (b n) (b n)) N)) :=
  SemiGeneral.semi_general K O inv mul_inv h10 (Nq K T N) x h _ N S b hx
    (sumN_nn K O.toCone _ (fun n => O.nn_mul _ _ (O.nn_add _ _ (hx n) (O.nn_add _ _ (RatModel.nn_one' K O.toCone) (RatModel.nn_one' K O.toCone)))
      (by
        clear neutral small
        induction N with
        | zero => exact O.nn_zero
        | succ N _ => exact (show O.nn (sA K (grid K T (fun _ => K.zero) n (N + 1) (N + 1))) from nn_sA (grid K T (fun _ => K.zero) n (N + 1) (N + 1)))) ) N)
    neutral (quad_nb K O T x hx N) small
where
  nn_sA : ∀ l : List (R × R), O.nn (sA K l)
    | [] => O.nn_zero
    | p :: l => O.nn_add _ _ (O.nn_sq _) (nn_sA l)

end QuadBound

#print axioms QuadBound.sB_app
#print axioms QuadBound.sA_app
#print axioms QuadBound.sB_row
#print axioms QuadBound.sB_grid
#print axioms QuadBound.sA_row
#print axioms QuadBound.sA_indep
#print axioms QuadBound.quad_sq
#print axioms QuadBound.quad_nb
#print axioms QuadBound.quad_semi

ns/RHCoef.lean

lean_all/ns/RHCoef.lean

/-
  ns/RHCoef.lean — T37 cycle 1848 (goal G858f): the two-parameter comparison mode by mode, uniform in time, over an
  ordered ring. For r ∈ L and the hypotheses of RHUniform.rh_combine:
    rh_coef   ν²·ν·w_r·|u_r − w_r|² ≤ 2X + 2ν²Y
  — each low Fourier coefficient of the restricted fine (l, k) scheme and of the coarse (L, 2k) scheme agree within
  the explicit bound for every time.
  Lean 4 core plus ns/RHUniform, ns/DyadicCoef (term_le).
-/
import ns.RHUniform
import ns.DyadicCoef

namespace RHCoef
open DetChain NSChain DyadicSmallGlobal LibRing DyadicLarge FourierGalerkin GalerkinForce ForceBound ForceSob
  ModeCount CubeCount ForceUniform NormCompare EnstStep EnstGlobal EnstGalerkin ForceTwoScale ForceBilin EnstDiff
  ForceDiff StepCauchy

section Gen
variable {R : Type} (K : CRing R) (O : OrdR K) {I : Type}

theorem qe_coef (ν k : R) (hν : O.nn ν) (hk : O.nn k) (L : List I) (kz : I → Mode) (u w : I → V3 R) (r : I) (hr : r ∈ L) :
    Le K O.toCone (K.mul ν (K.mul (wt K kz r) (dot3 K (dF K u w r) (dF K u w r)))) (Qe K ν k L kz u w) := by
  have t1 := DyadicCoef.term_le K O (fun p => K.mul (wt K kz p) (dot3 K (dF K u w p) (dF K u w p)))
    (fun p => O.nn_mul _ _ (dot3_nn K O.toCone _) (dot3_nn K O.toCone _)) L r hr
  refine le_trans' K O.toCone _ _ _ (le_mul_nn K O.toCone ν _ _ hν t1) ⟨_, O.nn_mul _ _ (O.nn_mul _ _ hk (O.nn_sq ν))
    (lsum_nn K O.toCone _ (fun p => O.nn_mul _ _ (O.nn_sq _) (dot3_nn K O.toCone _)) L), rfl⟩

theorem rh_coef (ν k X Y : R) (hν : O.nn ν) (hk : O.nn k) (L : List I) (kz : I → Mode) (u v w : I → V3 R)
    (r : I) (hr : r ∈ L)
    (hX : Le K O.toCone (K.mul (K.mul ν ν) (Qe K ν k L kz u v)) X) (hY : Le K O.toCone (Qe K ν k L kz w v) Y) :
    Le K O.toCone (K.mul (K.mul ν ν) (K.mul ν (K.mul (wt K kz r) (dot3 K (dF K u w r) (dF K u w r)))))
      (K.add (K.mul (K.add K.one K.one) X) (K.mul (K.add K.one K.one) (K.mul (K.mul ν ν) Y))) :=
  le_trans' K O.toCone _ _ _ (le_mul_nn K O.toCone (K.mul ν ν) _ _ (O.nn_sq ν) (qe_coef K O ν k hν hk L kz u w r hr))
    (RHUniform.rh_combine K O ν k X Y hν hk L kz u v w hX hY)

end Gen
end RHCoef

#print axioms RHCoef.qe_coef
#print axioms RHCoef.rh_coef

ns/RHUniform.lean

lean_all/ns/RHUniform.lean

/-
  ns/RHUniform.lean — T37 cycle 1846 (goal G858c): the two-parameter (truncation R, step k) comparison, uniform in
  time, over an ordered ring. Qe(u, v) = νZ(u − v) + kν²A(u − v) on the coarse modes L:
    qe_symm     Qe(u, v) = Qe(v, u)
    rh_combine  ν²·Qe(u, v) ≤ X and Qe(w, v) ≤ Y  ⇒  ν²·Qe(u, w) ≤ 2X + 2ν²Y      (QeTri.qe_tri)
  With u = the restriction of the fine (l, k) scheme at time 2n, v = the coarse (L, k) scheme at 2n (TruncUniform:
  X = iR·κ·2Q₂(0)) and w = the coarse (L, 2k) scheme at n (StepUniformS: Y = Qe(w_0, v_0) + (1 + k)k²C·c), the
  restricted fine scheme and the coarse 2k-scheme stay within 2X + 2ν²Y for all times: the convergence in (R, k) is
  uniform on the whole time axis for small data.
  Lean 4 core plus ns/QeTri.
-/
import ns.QeTri

namespace RHUniform
open DetChain NSChain DyadicSmallGlobal LibRing DyadicLarge FourierGalerkin GalerkinForce ForceBound ForceSob
  ModeCount CubeCount ForceUniform NormCompare EnstStep EnstGlobal EnstGalerkin ForceTwoScale ForceBilin EnstDiff
  ForceDiff StepCauchy

section Gen
variable {R : Type} (K : CRing R) (O : OrdR K) {I : Type}

theorem lsum_symm (μ : I → R) (u v : I → V3 R) : ∀ L : List I,
    lsum K (fun r => K.mul (μ r) (dot3 K (dF K u v r) (dF K u v r))) L
      = lsum K (fun r => K.mul (μ r) (dot3 K (dF K v u r) (dF K v u r))) L
  | [] => rfl
  | p :: L => by
    show K.add (K.mul (μ p) (K.add (K.add
        (K.mul (K.add (u p).x (K.neg (v p).x)) (K.add (u p).x (K.neg (v p).x)))
        (K.mul (K.add (u p).y (K.neg (v p).y)) (K.add (u p).y (K.neg (v p).y))))
        (K.mul (K.add (u p).z (K.neg (v p).z)) (K.add (u p).z (K.neg (v p).z)))))
      (lsum K (fun r => K.mul (μ r) (dot3 K (dF K u v r) (dF K u v r))) L)
      = K.add (K.mul (μ p) (K.add (K.add
        (K.mul (K.add (v p).x (K.neg (u p).x)) (K.add (v p).x (K.neg (u p).x)))
        (K.mul (K.add (v p).y (K.neg (u p).y)) (K.add (v p).y (K.neg (u p).y))))
        (K.mul (K.add (v p).z (K.neg (u p).z)) (K.add (v p).z (K.neg (u p).z)))))
      (lsum K (fun r => K.mul (μ r) (dot3 K (dF K v u r) (dF K v u r))) L)
    rw [lsum_symm μ u v L]
    lib_ring

theorem qe_symm (ν k : R) (L : List I) (kz : I → Mode) (u v : I → V3 R) :
    Qe K ν k L kz u v = Qe K ν k L kz v u := by
  show K.add (K.mul ν (lsum K (fun r => K.mul (wt K kz r) (dot3 K (dF K u v r) (dF K u v r))) L))
      (K.mul (K.mul k (K.mul ν ν)) (lsum K (fun r => K.mul (K.mul (wt K kz r) (wt K kz r)) (dot3 K (dF K u v r) (dF K u v r))) L))
    = K.add (K.mul ν (lsum K (fun r => K.mul (wt K kz r) (dot3 K (dF K v u r) (dF K v u r))) L))
      (K.mul (K.mul k (K.mul ν ν)) (lsum K (fun r => K.mul (K.mul (wt K kz r) (wt K kz r)) (dot3 K (dF K v u r) (dF K v u r))) L))
  rw [lsum_symm K (wt K kz) u v L, lsum_symm K (fun r => K.mul (wt K kz r) (wt K kz r)) u v L]

theorem rh_combine (ν k X Y : R) (hν : O.nn ν) (hk : O.nn k) (L : List I) (kz : I → Mode) (u v w : I → V3 R)
    (hX : Le K O.toCone (K.mul (K.mul ν ν) (Qe K ν k L kz u v)) X) (hY : Le K O.toCone (Qe K ν k L kz w v) Y) :
    Le K O.toCone (K.mul (K.mul ν ν) (Qe K ν k L kz u w))
      (K.add (K.mul (K.add K.one K.one) X) (K.mul (K.add K.one K.one) (K.mul (K.mul ν ν) Y))) := by
  have two : O.nn (K.add K.one K.one) := O.nn_add _ _ (RatModel.nn_one' K O.toCone) (RatModel.nn_one' K O.toCone)
  have t := le_mul_nn K O.toCone (K.mul ν ν) _ _ (O.nn_sq ν) (QeTri.qe_tri K O ν k hν hk L kz u v w)
  rw [qe_symm K ν k L kz v w] at t
  have y := le_mul_nn K O.toCone (K.mul ν ν) _ _ (O.nn_sq ν) hY
  have s := le_add' K O.toCone _ _ _ _ (le_mul_nn K O.toCone _ _ _ two hX) (le_mul_nn K O.toCone _ _ _ two y)
  refine le_trans' K O.toCone _ _ _ t (le_trans' K O.toCone _ _ _ ⟨K.zero, O.nn_zero, ?_⟩ s)
  generalize Qe K ν k L kz u v = A
  generalize Qe K ν k L kz w v = B
  lib_ring

end Gen
end RHUniform

#print axioms RHUniform.lsum_symm
#print axioms RHUniform.qe_symm
#print axioms RHUniform.rh_combine

ns/RatArch.lean

lean_all/ns/RatArch.lean

/-
  ns/RatArch.lean — T37 cycle 1870 (goal G860c2): ℚ is Archimedean, so halving rates over ℚ are Cauchy.
    cast_q        n·1 = n/1 in lib/Rat (normal form)
    rat_arch      every x ∈ ℚ satisfies x ≤ |num x|·1   (ratOrd of ns/RatModel)
    rat_cauchy    (s_{j+J} − s_j)² ≤ D·hʲ over ℚ with 2h = 1  ⇒  s is a Cauchy sequence (lib/CauchySeq)
  With RealArch this closes the Archimedean hypothesis of RealBridge for rational schemes.
  Lean 4 core plus ns/RealArch, ns/RatModel, lib/CReal.
-/
import ns.RealArch
import lib.CReal

namespace RatArch
open DetChain NSChain DyadicSmallGlobal LibRing LibCS LibGron LibCauchy RealBridge RealArch LibRat LibInt RatModel

theorem one_pos' : 0 < 1 := Nat.succ_le_succ (Nat.zero_le 0)

theorem cast_q : ∀ k : Nat, castN ratR k = normQ (Int.ofNat k) 1 one_pos'
  | 0 => rfl
  | k + 1 => by
    show addQ oneQ (castN ratR k) = normQ (Int.ofNat (k + 1)) 1 one_pos'
    rw [cast_q k]
    show addQ (normQ 1 1 one_pos') (normQ (Int.ofNat k) 1 one_pos') = normQ (Int.ofNat (k + 1)) 1 one_pos'
    rw [add_nn]
    apply (normQ_eq_iff _ _ _ _ _ _).2
    have e1 : Int.ofNat (k + 1) = Int.ofNat k + 1 := rfl
    have e2 : Int.ofNat (1 * 1) = 1 := rfl
    rw [e1, e2]
    lib_int

theorem nn_of (n : Nat) (num : Int) (den : Nat) (hd : 0 < den) (hn : n = absN num) :
    ∃ k, Int.ofNat n * Int.ofNat den + -num * Int.ofNat 1 = Int.ofNat k := by
  cases den with
  | zero => exact absurd hd (Nat.lt_irrefl 0)
  | succ e =>
    cases num with
    | ofNat a =>
      rw [hn]
      refine ⟨a * e, ?_⟩
      show Int.ofNat a * Int.ofNat (e + 1) + -(Int.ofNat a) * Int.ofNat 1 = Int.ofNat a * Int.ofNat e
      have e1 : Int.ofNat (e + 1) = Int.ofNat e + 1 := rfl
      rw [e1]; lib_int
    | negSucc a =>
      rw [hn]
      refine ⟨(a + 1) * (e + 1) + (a + 1), ?_⟩
      show Int.ofNat (a + 1) * Int.ofNat (e + 1) + -(Int.negSucc a) * Int.ofNat 1
        = Int.ofNat (a + 1) * Int.ofNat (e + 1) + Int.ofNat (a + 1)
      have e1 : Int.negSucc a = -(Int.ofNat (a + 1)) := rfl
      rw [e1]; lib_int

theorem rat_arch (x : Q) : ∃ n, Le ratR ratOrd.toCone x (castN ratR n) := by
  refine ⟨absN x.num, ratR.add (castN ratR (absN x.num)) (ratR.neg x), ?_, by lib_ring⟩
  show NN (addQ (castN ratR (absN x.num)) (negQ x))
  have hx : negQ x = negQ (normQ x.num x.den x.den_pos) := congrArg negQ (self_norm x)
  rw [cast_q, hx, neg_n, add_nn]
  exact nn_normQ _ (nn_of (absN x.num) x.num x.den x.den_pos rfl)

theorem rat_cauchy (D hh : Q) (hhn : ratOrd.nn hh) (hh2 : ratR.mul hh (tw ratR) = ratR.one) (s : Nat → Q)
    (hb : ∀ j J, Le ratR ratOrd.toCone (ratR.mul (sub ratR (s (j + J)) (s j)) (sub ratR (s (j + J)) (s j)))
      (ratR.mul D (EnstGronwall.pwr ratR hh j))) :
    Cauchy ratR (posO ratR ratOrd) s :=
  cauchy_half ratR ratOrd invQ mul_invQ LibRat.one_ne_zero rat_arch D hh hhn hh2 s hb

end RatArch

#print axioms RatArch.cast_q
#print axioms RatArch.nn_of
#print axioms RatArch.rat_arch
#print axioms RatArch.rat_cauchy

ns/RatModel.lean

lean_all/ns/RatModel.lean

/-
  ns/RatModel.lean — T37 cycle 731 (goal G444): the concrete supercritical dyadic model over any ordered ring, in
  particular over ℚ (lib/RatField from lib_shared), so the large-data bound applies to non-integer data and time
  steps (the integer schemes are degenerate: ns/IntVanish).

  Lean 4 core plus ns/DyadicLargeData (cn_large_data), lib/RatField (ratR, ratPos, rat_total, rat_antisymm,
  rat_noZD), lib/RingTac (lib_ring).
    ratOrd        ℚ as an OrdR (cone of nonnegative numerators, total, antisymmetric, no zero divisors)
    crit_g        q_n M_n = ν w_n k_n for w = k = 4ⁿ, c = M = 2ⁿ, ν = 3 (any ring)
    cast_le_p2    n ≤ 2ⁿ as ring elements; cast_mono  m ≤ n ⇒ m ≤ n
    model_gen     E_w(a_t) ≤ E_w(a_0) + Σ_{j<t} h·B for data with 4E₀ ≤ A², A = m·1 (any OrdR)
    model_rat     the same over ℚ
-/
import ns.DyadicLargeData
import lib.RatField
import ns.DyadicModelZ

namespace RatModel
open DetChain NSChain DyadicSmallGlobal LibRing DyadicHs DyadicCN DyadicLarge DyadicLargeCN
  DyadicLargeEnergy DyadicLargeData

section Gen
variable {R : Type} (K : CRing R)

def P2 (n : Nat) : R := pw K (castN K 2) n
def P4 (n : Nat) : R := pw K (castN K 4) n

theorem p4_sq : ∀ n, P4 K n = K.mul (P2 K n) (P2 K n)
  | 0 => by show K.one = K.mul K.one K.one; lib_ring
  | n + 1 => by
    show K.mul (P4 K n) (castN K 4) = K.mul (K.mul (P2 K n) (castN K 2)) (K.mul (P2 K n) (castN K 2))
    rw [p4_sq n]
    show K.mul (K.mul (P2 K n) (P2 K n)) (K.add K.one (K.add K.one (K.add K.one (K.add K.one K.zero))))
      = K.mul (K.mul (P2 K n) (K.add K.one (K.add K.one K.zero))) (K.mul (P2 K n) (K.add K.one (K.add K.one K.zero)))
    lib_ring

theorem crit_g (n : Nat) :
    K.mul (qq K (P4 K) (P2 K) n) (P2 K n) = K.mul (castN K 3) (K.mul (P4 K n) (P4 K n)) := by
  unfold qq
  show K.mul (K.mul (K.add (K.mul (P4 K n) (castN K 4)) (K.neg (P4 K n))) (P2 K n)) (P2 K n)
    = K.mul (castN K 3) (K.mul (P4 K n) (P4 K n))
  rw [p4_sq K n]
  show K.mul (K.mul (K.add (K.mul (K.mul (P2 K n) (P2 K n)) (K.add K.one (K.add K.one (K.add K.one (K.add K.one K.zero)))))
      (K.neg (K.mul (P2 K n) (P2 K n)))) (P2 K n)) (P2 K n)
    = K.mul (K.add K.one (K.add K.one (K.add K.one K.zero))) (K.mul (K.mul (P2 K n) (P2 K n)) (K.mul (P2 K n) (P2 K n)))
  lib_ring

variable (P : Cone K)

theorem nn_one' : P.nn K.one := by
  have := P.nn_sq K.one
  rw [show K.mul K.one K.one = K.one by lib_ring] at this; exact this

theorem nn_cast : ∀ n, P.nn (castN K n)
  | 0 => P.nn_zero
  | n + 1 => P.nn_add _ _ (nn_one' K P) (nn_cast n)

theorem nn_pw (x : R) (hx : P.nn x) : ∀ n, P.nn (pw K x n)
  | 0 => nn_one' K P
  | n + 1 => P.nn_mul _ _ (nn_pw x hx n) hx

theorem cast_add (m : Nat) : ∀ d, castN K (m + d) = K.add (castN K m) (castN K d)
  | 0 => by show castN K m = K.add (castN K m) K.zero; lib_ring
  | d + 1 => by
    show K.add K.one (castN K (m + d)) = K.add (castN K m) (K.add K.one (castN K d))
    rw [cast_add m d]; lib_ring

theorem cast_mono (m n : Nat) (h : m ≤ n) : Le K P (castN K m) (castN K n) := by
  have ⟨d, hd⟩ := DyadicModelZ.le_dest h
  exact ⟨castN K d, nn_cast K P d, by rw [← hd, cast_add]⟩

theorem one_le_p2 : ∀ n, Le K P K.one (P2 K n)
  | 0 => le_refl' K P _
  | n + 1 => by
    have ⟨d, hd⟩ := one_le_p2 n
    refine ⟨K.add (K.add d d) K.one, P.nn_add _ _ (P.nn_add _ _ hd.1 hd.1) (nn_one' K P), ?_⟩
    show K.add K.one (K.add (K.add d d) K.one) = K.mul (P2 K n) (K.add K.one (K.add K.one K.zero))
    rw [← hd.2]; lib_ring

theorem cast_le_p2 : ∀ n, Le K P (castN K n) (P2 K n)
  | 0 => ⟨K.one, nn_one' K P, by show K.add K.zero K.one = K.one; lib_ring⟩
  | n + 1 => by
    have ⟨d, hd⟩ := cast_le_p2 n
    have ⟨e, he⟩ := one_le_p2 K P n
    refine ⟨K.add d e, P.nn_add _ _ hd.1 he.1, ?_⟩
    show K.add (K.add K.one (castN K n)) (K.add d e) = K.mul (P2 K n) (K.add K.one (K.add K.one K.zero))
    have r : K.mul (P2 K n) (K.add K.one (K.add K.one K.zero)) = K.add (P2 K n) (P2 K n) := by lib_ring
    have r2 : K.add (P2 K n) (P2 K n) = K.add (K.add (castN K n) d) (K.add K.one e) := by rw [hd.2, he.2]
    rw [r, r2]; lib_ring

theorem nn_qq_g (n : Nat) : P.nn (qq K (P4 K) (P2 K) n) := by
  unfold qq
  have e : K.mul (K.add (P4 K (n + 1)) (K.neg (P4 K n))) (P2 K n)
      = K.mul (castN K 3) (K.mul (P4 K n) (P2 K n)) := by
    show K.mul (K.add (K.mul (P4 K n) (K.add K.one (K.add K.one (K.add K.one (K.add K.one K.zero))))) (K.neg (P4 K n))) (P2 K n)
      = K.mul (K.add K.one (K.add K.one (K.add K.one K.zero))) (K.mul (P4 K n) (P2 K n))
    lib_ring
  rw [e]
  exact P.nn_mul _ _ (nn_cast K P 3) (P.nn_mul _ _ (nn_pw K P _ (nn_cast K P 4) n) (nn_pw K P _ (nn_cast K P 2) n))

theorem model_gen (O : OrdR K) (a : Nat → Nat → R) (h : R) (m N : Nat) (hh : O.nn h)
    (h0 : ∀ t, a t 0 = K.zero) (hz : ∀ t n, N + 1 ≤ n → a t n = K.zero)
    (step : ∀ t n, K.add (a (t + 1) (n + 1)) (K.neg (a t (n + 1)))
      = K.mul h (K.add (K.neg (K.mul (castN K 3) (K.mul (P4 K (n + 1)) (K.add (a (t + 1) (n + 1)) (a t (n + 1))))))
          (dyN K (P2 K) (fun i => K.add (a (t + 1) i) (a t i)) n)))
    (hA2 : Le K O.toCone (four K (E0 K a N)) (K.mul (castN K m) (castN K m))) :
    ∀ t, Le K O.toCone (Ew K (P4 K) (a t) N)
      (K.add (Ew K (P4 K) (a 0) N)
        (sumN K (fun _ => K.mul h (headB K (P4 K) (P2 K) (four K (E0 K a N)) (castN K m) m N)) t)) :=
  cn_large_data K O (P4 K) (P2 K) (P4 K) (P2 K) a h (castN K 3) (castN K m) m N hh (nn_cast K O.toCone 3)
    (fun n => nn_pw K O.toCone _ (nn_cast K O.toCone 4) n) (fun n => nn_pw K O.toCone _ (nn_cast K O.toCone 4) n)
    h0 hz step (nn_qq_g K O.toCone) (fun n _ => crit_g K n) hA2 (nn_cast K O.toCone m)
    (fun n hn => le_trans' K O.toCone _ _ _ (cast_mono K O.toCone m n hn) (cast_le_p2 K O.toCone n))

end Gen

open LibRat in
def ratCone : Cone ratR where
  nn := ratPos.nn
  nn_zero := ratPos.nn_zero
  nn_add := ratPos.nn_add
  nn_mul := ratPos.nn_mul
  nn_sq := ratPos.nn_sq

open LibRat in
def ratOrd : OrdR ratR where
  toCone := ratCone
  total := fun x y => by
    cases rat_total (ratR.add y (ratR.neg x)) with
    | inl p => exact Or.inl ⟨_, p, by lib_ring⟩
    | inr p => exact Or.inr ⟨_, p, by lib_ring⟩
  antisym := fun x y h1 h2 => by
    cases h1 with
    | intro d hd => cases h2 with
      | intro e he =>
        have hde : e = ratR.neg d := by
          have s : ratR.add d e = ratR.zero := by
            have : ratR.add d e = ratR.add (ratR.add (ratR.add x d) e) (ratR.neg x) := by lib_ring
            rw [this, hd.2, he.2]; lib_ring
          have : e = ratR.add (ratR.add d e) (ratR.neg d) := by lib_ring
          rw [this, s]; lib_ring
        have d0 : d = ratR.zero := rat_antisymm d hd.1 (by rw [← hde]; exact he.1)
        rw [← hd.2, d0]; lib_ring
  noZD := rat_noZD

theorem model_rat (a : Nat → Nat → LibRat.Q) (h : LibRat.Q) (m N : Nat) (hh : ratOrd.nn h)
    (h0 : ∀ t, a t 0 = LibRat.ratR.zero) (hz : ∀ t n, N + 1 ≤ n → a t n = LibRat.ratR.zero)
    (step : ∀ t n, LibRat.ratR.add (a (t + 1) (n + 1)) (LibRat.ratR.neg (a t (n + 1)))
      = LibRat.ratR.mul h (LibRat.ratR.add (LibRat.ratR.neg (LibRat.ratR.mul (castN LibRat.ratR 3)
          (LibRat.ratR.mul (P4 LibRat.ratR (n + 1)) (LibRat.ratR.add (a (t + 1) (n + 1)) (a t (n + 1))))))
          (dyN LibRat.ratR (P2 LibRat.ratR) (fun i => LibRat.ratR.add (a (t + 1) i) (a t i)) n)))
    (hA2 : Le LibRat.ratR ratOrd.toCone (four LibRat.ratR (E0 LibRat.ratR a N))
      (LibRat.ratR.mul (castN LibRat.ratR m) (castN LibRat.ratR m))) :
    ∀ t, Le LibRat.ratR ratOrd.toCone (Ew LibRat.ratR (P4 LibRat.ratR) (a t) N)
      (LibRat.ratR.add (Ew LibRat.ratR (P4 LibRat.ratR) (a 0) N)
        (sumN LibRat.ratR (fun _ => LibRat.ratR.mul h
          (headB LibRat.ratR (P4 LibRat.ratR) (P2 LibRat.ratR) (four LibRat.ratR (E0 LibRat.ratR a N))
            (castN LibRat.ratR m) m N)) t)) :=
  model_gen LibRat.ratR ratOrd a h m N hh h0 hz step hA2

end RatModel

#print axioms RatModel.p4_sq
#print axioms RatModel.crit_g
#print axioms RatModel.nn_one'
#print axioms RatModel.nn_cast
#print axioms RatModel.nn_pw
#print axioms RatModel.cast_add
#print axioms RatModel.one_le_p2
#print axioms RatModel.cast_mono
#print axioms RatModel.cast_le_p2
#print axioms RatModel.nn_qq_g
#print axioms RatModel.model_gen
#print axioms RatModel.ratOrd
#print axioms RatModel.model_rat

ns/RatModelTail.lean

lean_all/ns/RatModelTail.lean

/-
  ns/RatModelTail.lean — T37 cycle 737 (goal G450): tightness for the concrete model over any ordered ring / ℚ —
  the L² energy beyond shell M is ≤ C/4^M, uniformly in the truncation and in t ≤ T (G425 over ℚ).

  Lean 4 core plus ns/RatModelU (uniform_g), ns/RatModel (P4, cast lemmas), lib/RingTac (lib_ring).
    pw_step     x ≥ 1, y ≥ 0 ⇒ y ≤ y·x
    p4_mono_g   M ≤ n ⇒ 4^M ≤ 4^n
    tail_le_g   4^M · Σ_{j<N, M ≤ j+1} a(j+1)² ≤ E_w(a)
    tail_g      4^M · Σ_{j<N, M ≤ j+1} a_t(j+1)² ≤ W + T·h·B(F)
-/
import ns.RatModelU

namespace RatModelTail
open DetChain NSChain DyadicSmallGlobal LibRing DyadicHs DyadicCN DyadicLarge DyadicLargeCN
  DyadicLargeEnergy DyadicLargeData RatModel RatModelU

section Gen
variable {R : Type} (K : CRing R) (P : Cone K)

theorem pw_step (x y : R) (hx : Le K P K.one x) (hy : P.nn y) : Le K P y (K.mul y x) := by
  cases hx with
  | intro d hd => exact ⟨K.mul y d, P.nn_mul _ _ hy hd.1, by rw [← hd.2]; lib_ring⟩

theorem one_le_four : Le K P K.one (castN K 4) :=
  ⟨castN K 3, nn_cast K P 3, by show K.add K.one (castN K 3) = K.add K.one (castN K 3); rfl⟩

theorem p4_mono_g (M : Nat) : ∀ d, Le K P (P4 K M) (P4 K (M + d))
  | 0 => le_refl' K P _
  | d + 1 => le_trans' K P _ _ _ (p4_mono_g M d)
      (pw_step K P _ _ (one_le_four K P) (nn_pw K P _ (nn_cast K P 4) (M + d)))

def tl (M : Nat) (b : Nat → R) (j : Nat) : R :=
  match Nat.ble M (j + 1) with
  | true => K.mul (b (j + 1)) (b (j + 1))
  | false => K.zero

theorem term_g (M : Nat) (b : Nat → R) (j : Nat) :
    Le K P (K.mul (P4 K M) (tl K M b j)) (K.mul (P4 K (j + 1)) (K.mul (b (j + 1)) (b (j + 1)))) := by
  unfold tl
  cases e : Nat.ble M (j + 1) with
  | true =>
    have ⟨d, hd⟩ := DyadicModelZ.le_dest (Nat.le_of_ble_eq_true e)
    have l := le_mul_rg K P (K.mul (b (j + 1)) (b (j + 1))) _ _ (P.nn_sq _) (p4_mono_g K P M d)
    rw [hd] at l; exact l
  | false =>
    refine ⟨K.mul (P4 K (j + 1)) (K.mul (b (j + 1)) (b (j + 1))),
      P.nn_mul _ _ (nn_pw K P _ (nn_cast K P 4) (j + 1)) (P.nn_sq _), ?_⟩
    show K.add (K.mul (P4 K M) K.zero) (K.mul (P4 K (j + 1)) (K.mul (b (j + 1)) (b (j + 1))))
      = K.mul (P4 K (j + 1)) (K.mul (b (j + 1)) (b (j + 1)))
    lib_ring

theorem tail_le_g (M N : Nat) (b : Nat → R) :
    Le K P (K.mul (P4 K M) (sumN K (tl K M b) N)) (Ew K (P4 K) b N) := by
  rw [← sumN_mul_left]
  exact sum_le K P _ _ (term_g K P M b) N

end Gen

theorem tail_g {R : Type} (K : CRing R) (O : OrdR K) (a : Nat → Nat → R) (h W F : R) (m N T M : Nat)
    (hh : O.nn h)
    (h0 : ∀ t, a t 0 = K.zero) (hz : ∀ t n, N + 1 ≤ n → a t n = K.zero)
    (step : ∀ t n, K.add (a (t + 1) (n + 1)) (K.neg (a t (n + 1)))
      = K.mul h (K.add (K.neg (K.mul (castN K 3) (K.mul (P4 K (n + 1)) (K.add (a (t + 1) (n + 1)) (a t (n + 1))))))
          (dyN K (P2 K) (fun i => K.add (a (t + 1) i) (a t i)) n)))
    (hW : Le K O.toCone (Ew K (P4 K) (a 0) N) W)
    (hF : Le K O.toCone (four K (E0 K a N)) F)
    (hFA : Le K O.toCone F (K.mul (castN K m) (castN K m)))
    (t : Nat) (ht : t ≤ T) :
    Le K O.toCone (K.mul (P4 K M) (sumN K (tl K M (a t)) N))
      (K.add W (K.mul (castN K T) (K.mul h (headB K (P4 K) (P2 K) F (castN K m) m N)))) :=
  le_trans' K O.toCone _ _ _ (tail_le_g K O.toCone M N (a t)) (uniform_g K O a h W F m N T hh h0 hz step hW hF hFA t ht)

end RatModelTail

#print axioms RatModelTail.pw_step
#print axioms RatModelTail.one_le_four
#print axioms RatModelTail.p4_mono_g
#print axioms RatModelTail.term_g
#print axioms RatModelTail.tail_le_g
#print axioms RatModelTail.tail_g

ns/RatModelU.lean

lean_all/ns/RatModelU.lean

/-
  ns/RatModelU.lean — T37 cycle 734 (goal G447): the horizon, truncation-uniform and mode bounds (G416–G422)
  for the concrete model over any ordered ring, in particular ℚ.

  Lean 4 core plus ns/RatModel (model_gen, ratOrd), ns/DyadicModelMode (term_le), lib/RingTac (lib_ring).
    sumN_const_g   Σ_{j<t} X = t·X
    le_mul_rg      x ≤ y, c ≥ 0 ⇒ x·c ≤ y·c
    headB_mono     E ≤ F ⇒ B(E) ≤ B(F)
    uniform_g      E_w(a_0) ≤ W, 4E₀ ≤ F ≤ A², t ≤ T ⇒ E_w(a_t) ≤ W + T·h·B(F)
    mode_g         … ⇒ 4^{n+1}·a_t(n+1)² ≤ W + T·h·B(F)   (n < N)
    uniform_rat    uniform_g over ℚ
-/
import ns.RatModel
import ns.DyadicModelMode

namespace RatModelU
open DetChain NSChain DyadicSmallGlobal LibRing DyadicHs DyadicCN DyadicLarge DyadicLargeCN
  DyadicLargeEnergy DyadicLargeData RatModel DyadicModelMode

section Gen
variable {R : Type} (K : CRing R)

theorem sumN_const_g (X : R) : ∀ t, sumN K (fun _ => X) t = K.mul (castN K t) X
  | 0 => by show K.zero = K.mul K.zero X; lib_ring
  | t + 1 => by
    show K.add (sumN K (fun _ => X) t) X = K.mul (K.add K.one (castN K t)) X
    rw [sumN_const_g X t]; lib_ring

variable (P : Cone K)

theorem le_mul_rg (c x y : R) (hc : P.nn c) (h : Le K P x y) : Le K P (K.mul x c) (K.mul y c) := by
  cases h with
  | intro d hd =>
    exact ⟨K.mul d c, P.nn_mul _ _ hd.1 hc, by rw [← hd.2]; lib_ring⟩

theorem ite_le (c : Prop) (inst : Decidable c) (x y : R) (h : c → Le K P x y) :
    Le K P (@ite _ c inst x K.zero) (@ite _ c inst y K.zero) := by
  cases inst with
  | isTrue p => exact h p
  | isFalse _ => exact le_refl' K P _

theorem ite_nn (c : Prop) (inst : Decidable c) (x : R) (h : c → P.nn x) :
    P.nn (@ite _ c inst x K.zero) := by
  cases inst with
  | isTrue p => exact h p
  | isFalse _ => exact P.nn_zero

theorem headB_mono (w c : Nat → R) (E F A : R) (n0 N : Nat) (hq : ∀ n, P.nn (qq K w c n)) (hA : P.nn A)
    (hEF : Le K P E F) : Le K P (headB K w c E A n0 N) (headB K w c F A n0 N) := by
  unfold headB
  apply sum_le K P
  intro n
  exact ite_le K P _ _ _ _ (fun _ => le_mul_nn K P _ _ _ (hq n) (le_mul_rg K P A E F hA hEF))

theorem nn_headB (w c : Nat → R) (F A : R) (n0 N : Nat) (hq : ∀ n, P.nn (qq K w c n)) (hA : P.nn A)
    (hF : P.nn F) : P.nn (headB K w c F A n0 N) := by
  unfold headB
  apply sumN_nn K P
  intro n
  exact ite_nn K P _ _ _ (fun _ => P.nn_mul _ _ (hq n) (P.nn_mul _ _ hF hA))

theorem nn_four (E : R) (hE : P.nn E) : P.nn (four K E) :=
  P.nn_add _ _ (P.nn_add _ _ hE hE) (P.nn_add _ _ hE hE)

end Gen

theorem uniform_g {R : Type} (K : CRing R) (O : OrdR K) (a : Nat → Nat → R) (h W F : R) (m N T : Nat)
    (hh : O.nn h)
    (h0 : ∀ t, a t 0 = K.zero) (hz : ∀ t n, N + 1 ≤ n → a t n = K.zero)
    (step : ∀ t n, K.add (a (t + 1) (n + 1)) (K.neg (a t (n + 1)))
      = K.mul h (K.add (K.neg (K.mul (castN K 3) (K.mul (P4 K (n + 1)) (K.add (a (t + 1) (n + 1)) (a t (n + 1))))))
          (dyN K (P2 K) (fun i => K.add (a (t + 1) i) (a t i)) n)))
    (hW : Le K O.toCone (Ew K (P4 K) (a 0) N) W)
    (hF : Le K O.toCone (four K (E0 K a N)) F)
    (hFA : Le K O.toCone F (K.mul (castN K m) (castN K m)))
    (t : Nat) (ht : t ≤ T) :
    Le K O.toCone (Ew K (P4 K) (a t) N)
      (K.add W (K.mul (castN K T) (K.mul h (headB K (P4 K) (P2 K) F (castN K m) m N)))) := by
  have hA2 := le_trans' K O.toCone _ _ _ hF hFA
  have hb := model_gen K O a h m N hh h0 hz step hA2 t
  rw [sumN_const_g] at hb
  have e0 : O.nn (four K (E0 K a N)) := nn_four K O.toCone _ (sq_sum_nonneg K O.toCone (fun n => a 0 (n + 1)) N)
  have hFn : O.nn F := by
    cases hF with
    | intro d hd => rw [← hd.2]; exact O.nn_add _ _ e0 hd.1
  have hq := nn_qq_g K O.toCone
  have hAm := nn_cast K O.toCone m
  have hB := headB_mono K O.toCone (P4 K) (P2 K) _ _ (castN K m) m N hq hAm hF
  have hX := le_mul_nn K O.toCone h _ _ hh hB
  have hY := le_mul_nn K O.toCone (castN K t) _ _ (nn_cast K O.toCone t) hX
  have hT := le_mul_rg K O.toCone (K.mul h (headB K (P4 K) (P2 K) F (castN K m) m N)) _ _
    (O.nn_mul _ _ hh (nn_headB K O.toCone _ _ F _ m N hq hAm hFn)) (cast_mono K O.toCone t T ht)
  exact le_trans' K O.toCone _ _ _ hb
    (le_add' K O.toCone _ _ _ _ hW (le_trans' K O.toCone _ _ _ hY hT))

theorem mode_g {R : Type} (K : CRing R) (O : OrdR K) (a : Nat → Nat → R) (h W F : R) (m N T : Nat)
    (hh : O.nn h)
    (h0 : ∀ t, a t 0 = K.zero) (hz : ∀ t n, N + 1 ≤ n → a t n = K.zero)
    (step : ∀ t n, K.add (a (t + 1) (n + 1)) (K.neg (a t (n + 1)))
      = K.mul h (K.add (K.neg (K.mul (castN K 3) (K.mul (P4 K (n + 1)) (K.add (a (t + 1) (n + 1)) (a t (n + 1))))))
          (dyN K (P2 K) (fun i => K.add (a (t + 1) i) (a t i)) n)))
    (hW : Le K O.toCone (Ew K (P4 K) (a 0) N) W)
    (hF : Le K O.toCone (four K (E0 K a N)) F)
    (hFA : Le K O.toCone F (K.mul (castN K m) (castN K m)))
    (n t : Nat) (hn : n < N) (ht : t ≤ T) :
    Le K O.toCone (K.mul (P4 K (n + 1)) (K.mul (a t (n + 1)) (a t (n + 1))))
      (K.add W (K.mul (castN K T) (K.mul h (headB K (P4 K) (P2 K) F (castN K m) m N)))) :=
  le_trans' K O.toCone _ _ _
    (term_le K O.toCone (fun j => K.mul (P4 K (j + 1)) (K.mul (a t (j + 1)) (a t (j + 1))))
      (fun j => O.nn_mul _ _ (nn_pw K O.toCone _ (nn_cast K O.toCone 4) (j + 1)) (O.nn_sq _)) n N hn)
    (uniform_g K O a h W F m N T hh h0 hz step hW hF hFA t ht)

theorem uniform_rat (a : Nat → Nat → LibRat.Q) (h W F : LibRat.Q) (m N T : Nat) (hh : ratOrd.nn h)
    (h0 : ∀ t, a t 0 = LibRat.ratR.zero) (hz : ∀ t n, N + 1 ≤ n → a t n = LibRat.ratR.zero)
    (step : ∀ t n, LibRat.ratR.add (a (t + 1) (n + 1)) (LibRat.ratR.neg (a t (n + 1)))
      = LibRat.ratR.mul h (LibRat.ratR.add (LibRat.ratR.neg (LibRat.ratR.mul (castN LibRat.ratR 3)
          (LibRat.ratR.mul (P4 LibRat.ratR (n + 1)) (LibRat.ratR.add (a (t + 1) (n + 1)) (a t (n + 1))))))
          (dyN LibRat.ratR (P2 LibRat.ratR) (fun i => LibRat.ratR.add (a (t + 1) i) (a t i)) n)))
    (hW : Le LibRat.ratR ratOrd.toCone (Ew LibRat.ratR (P4 LibRat.ratR) (a 0) N) W)
    (hF : Le LibRat.ratR ratOrd.toCone (four LibRat.ratR (E0 LibRat.ratR a N)) F)
    (hFA : Le LibRat.ratR ratOrd.toCone F (LibRat.ratR.mul (castN LibRat.ratR m) (castN LibRat.ratR m)))
    (t : Nat) (ht : t ≤ T) :
    Le LibRat.ratR ratOrd.toCone (Ew LibRat.ratR (P4 LibRat.ratR) (a t) N)
      (LibRat.ratR.add W (LibRat.ratR.mul (castN LibRat.ratR T)
        (LibRat.ratR.mul h (headB LibRat.ratR (P4 LibRat.ratR) (P2 LibRat.ratR) F (castN LibRat.ratR m) m N)))) :=
  uniform_g LibRat.ratR ratOrd a h W F m N T hh h0 hz step hW hF hFA t ht

end RatModelU

#print axioms RatModelU.sumN_const_g
#print axioms RatModelU.le_mul_rg
#print axioms RatModelU.ite_le
#print axioms RatModelU.ite_nn
#print axioms RatModelU.headB_mono
#print axioms RatModelU.nn_headB
#print axioms RatModelU.nn_four
#print axioms RatModelU.uniform_g
#print axioms RatModelU.mode_g
#print axioms RatModelU.uniform_rat

ns/RealArch.lean

lean_all/ns/RealArch.lean

/-
  ns/RealArch.lean — T37 cycle 1870 (goal G860c, generic part): in an ordered ring with inverses where every element
  is below some n·1 (Archimedean), a halving rate D·(1/2)^j gives the Archimedean choice of RealBridge:
    pwr_mul2      aⁿ·bⁿ = (ab)ⁿ
    pwr_one'      1ⁿ = 1
    cast_le_pow   1 ≤ 2ⁿ and n·1 ≤ 2ⁿ
    arch_half     for every e > 0 some q, N with q + q ≤ e and D·hᴺ ≤ q²   (h·2 = 1, q = e·h)
    cauchy_half   (s_{j+J} − s_j)² ≤ D·hʲ for all j, J  ⇒  s is Cauchy (lib/CauchySeq)
  The Archimedean hypothesis for ℚ itself is G860c2.
  Lean 4 core plus ns/RealBridge, ns/EnstGronwall, ns/DyadicLarge, ns/RatModel.
-/
import ns.RealBridge
import ns.EnstGronwall
import ns.DyadicLarge
import ns.RatModel

namespace RealArch
open DetChain NSChain DyadicSmallGlobal LibRing LibCS LibGron LibCauchy RealBridge EnstGronwall DyadicLarge

section Gen
variable {R : Type} (K : CRing R) (O : OrdR K)

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

theorem pwr_mul2 (a b : R) : ∀ n, K.mul (pwr K a n) (pwr K b n) = pwr K (K.mul a b) n
  | 0 => by show K.mul K.one K.one = K.one; lib_ring
  | n + 1 => by
    show K.mul (K.mul a (pwr K a n)) (K.mul b (pwr K b n)) = K.mul (K.mul a b) (pwr K (K.mul a b) n)
    rw [← pwr_mul2 a b n]; lib_ring

theorem pwr_one' : ∀ n, pwr K K.one n = K.one
  | 0 => rfl
  | n + 1 => by
    show K.mul K.one (pwr K K.one n) = K.one
    rw [pwr_one' n]; lib_ring

theorem cast_le_pow : ∀ n, Le K O.toCone K.one (pwr K (tw K) n) ∧ Le K O.toCone (castN K n) (pwr K (tw K) n)
  | 0 => ⟨le_refl' K O.toCone _, ⟨K.one, RatModel.nn_one' K O.toCone, by show K.add K.zero K.one = K.one; lib_ring⟩⟩
  | n + 1 => by
    cases cast_le_pow n with
    | intro h1 h2 =>
      cases h1 with
      | intro d1 hd1 =>
        cases h2 with
        | intro d2 hd2 =>
          constructor
          · refine ⟨K.add (pwr K (tw K) n) d1, O.nn_add _ _ (pwr_nn K O _ (O.nn_add _ _ (RatModel.nn_one' K O.toCone)
              (RatModel.nn_one' K O.toCone)) n) hd1.1, ?_⟩
            show K.add K.one (K.add (pwr K (tw K) n) d1) = K.mul (tw K) (pwr K (tw K) n)
            rw [← hd1.2]; unfold tw; lib_ring
          · refine ⟨K.add d1 d2, O.nn_add _ _ hd1.1 hd2.1, ?_⟩
            show K.add (K.add K.one (castN K n)) (K.add d1 d2) = K.mul (tw K) (pwr K (tw K) n)
            calc K.add (K.add K.one (castN K n)) (K.add d1 d2)
                = K.add (K.add K.one d1) (K.add (castN K n) d2) := by lib_ring
              _ = K.add (pwr K (tw K) n) (pwr K (tw K) n) := by rw [hd1.2, hd2.2]
              _ = K.mul (tw K) (pwr K (tw K) n) := by unfold tw; lib_ring

variable (inv : R → R) (mul_inv : ∀ y, y ≠ K.zero → K.mul y (inv y) = K.one) (h10 : K.one ≠ K.zero)

include mul_inv h10 in
theorem arch_half (arch : ∀ x, ∃ n, Le K O.toCone x (castN K n)) (D hh : R) (hhn : O.nn hh)
    (hh2 : K.mul hh (tw K) = K.one) :
    ∀ e, PosE K (posO K O) e → ∃ q N, O.nn q ∧ Le K O.toCone (K.add q q) e
      ∧ Le K O.toCone (K.mul D (pwr K hh N)) (K.mul q q) := by
  intro e he
  have hen : O.nn e := he.1
  have hE0 : K.mul (K.mul e e) (K.mul hh hh) ≠ K.zero := by
    intro z
    cases O.noZD _ _ z with
    | inl a =>
      cases O.noZD _ _ a with
      | inl b => exact he.2 b
      | inr b => exact he.2 b
    | inr a =>
      have hz : hh = K.zero := by
        cases O.noZD _ _ a with
        | inl b => exact b
        | inr b => exact b
      apply h10
      rw [← hh2, hz]; lib_ring
  have hEn : O.nn (K.mul (K.mul e e) (K.mul hh hh)) := O.nn_mul _ _ (O.nn_sq e) (O.nn_sq hh)
  have hy := mul_inv _ hE0
  cases arch (K.mul D (inv (K.mul (K.mul e e) (K.mul hh hh)))) with
  | intro n hn =>
    have h := le_trans' K O.toCone _ _ _ hn (cast_le_pow K O n).2
    have m1 := le_mul_nn K O.toCone (pwr K hh n) _ _ (pwr_nn K O hh hhn n)
      (le_mul_nn K O.toCone (K.mul (K.mul e e) (K.mul hh hh)) _ _ hEn h)
    have e1 : K.mul (pwr K hh n) (K.mul (K.mul (K.mul e e) (K.mul hh hh)) (K.mul D (inv (K.mul (K.mul e e) (K.mul hh hh)))))
        = K.mul D (pwr K hh n) := by
      calc _ = K.mul (K.mul D (pwr K hh n)) (K.mul (K.mul (K.mul e e) (K.mul hh hh)) (inv (K.mul (K.mul e e) (K.mul hh hh)))) := by
            lib_ring
        _ = K.mul D (pwr K hh n) := by rw [hy]; lib_ring
    have e2 : K.mul (pwr K hh n) (K.mul (K.mul (K.mul e e) (K.mul hh hh)) (pwr K (tw K) n)) = K.mul (K.mul e hh) (K.mul e hh) := by
      calc _ = K.mul (K.mul (K.mul e e) (K.mul hh hh)) (K.mul (pwr K hh n) (pwr K (tw K) n)) := by lib_ring
        _ = K.mul (K.mul (K.mul e e) (K.mul hh hh)) K.one := by rw [pwr_mul2, hh2, pwr_one']
        _ = K.mul (K.mul e hh) (K.mul e hh) := by lib_ring
    rw [e1, e2] at m1
    refine ⟨K.mul e hh, n, O.nn_mul _ _ hen hhn, ⟨K.zero, O.nn_zero, ?_⟩, m1⟩
    calc K.add (K.add (K.mul e hh) (K.mul e hh)) K.zero = K.mul e (K.mul hh (tw K)) := by unfold tw; lib_ring
      _ = e := by rw [hh2]; lib_ring

include mul_inv h10 in
theorem cauchy_half (arch : ∀ x, ∃ n, Le K O.toCone x (castN K n)) (D hh : R) (hhn : O.nn hh)
    (hh2 : K.mul hh (tw K) = K.one) (s : Nat → R)
    (hb : ∀ j J, Le K O.toCone (K.mul (sub K (s (j + J)) (s j)) (sub K (s (j + J)) (s j))) (K.mul D (pwr K hh j))) :
    Cauchy K (posO K O) s :=
  cauchy_of_sq_rate K O s (fun j => K.mul D (pwr K hh j)) hb (arch_half K O inv mul_inv h10 arch D hh hhn hh2)

end Gen
end RealArch

#print axioms RealArch.pwr_mul2
#print axioms RealArch.pwr_one'
#print axioms RealArch.cast_le_pow
#print axioms RealArch.arch_half
#print axioms RealArch.cauchy_half

ns/RealBridge.lean

lean_all/ns/RealBridge.lean

/-
  ns/RealBridge.lean — T37 cycle 1866 (goal G860a, real layer): the NS ordered ring (OrdR, cone order Le) as a positive
  cone of the shared Cauchy-real library (lib/CauchySeq), and a squared rate with an Archimedean hypothesis gives a
  Cauchy sequence in the library sense:
    posO              the cone of O as LibCS.Pos
    le_of_Le          Le x y  ⇒  le x y
    close_of_sq       (a − b)² ≤ q², q ≥ 0  ⇒  Close q a b
    close_mono        Close e a b, e ≤ e'  ⇒  Close e' a b
    cauchy_of_sq_rate (s_{j+J} − s_j)² ≤ b_j for all j, J, and for every e > 0 some q, N with q + q ≤ e and b_N ≤ q²
                      ⇒  Cauchy s
  This turns the explicit moduli of DyadicCoef/StepCauchy into elements of the Cauchy reals (lib/CReal).
  Lean 4 core plus ns/DyadicSmallGlobal, lib/CauchySeq.
-/
import ns.DyadicSmallGlobal
import lib.CauchySeq

namespace RealBridge
open DetChain NSChain DyadicSmallGlobal LibRing LibCS LibGron LibCauchy

section Gen
variable {R : Type} (K : CRing R) (O : OrdR K)

def posO : Pos K := { nn := O.nn, nn_sq := O.nn_sq, nn_add := O.nn_add, nn_mul := O.nn_mul, nn_zero := O.nn_zero }

theorem le_of_Le {x y : R} (h : Le K O.toCone x y) : LibGron.le K (posO K O) x y := by
  cases h with
  | intro d hd =>
    show O.nn (sub K y x)
    rw [← hd.2, show sub K (K.add x d) x = d by lib_ring]
    exact hd.1

theorem close_of_sq (a b q : R) (hq : O.nn q)
    (h : Le K O.toCone (K.mul (sub K a b) (sub K a b)) (K.mul q q)) : Close K (posO K O) q a b := by
  have h1 := le_of_sq_le K O (sub K a b) q hq h
  have e : K.mul (sub K b a) (sub K b a) = K.mul (sub K a b) (sub K a b) := by lib_ring
  have h2 := le_of_sq_le K O (sub K b a) q hq (by rw [e]; exact h)
  exact ⟨le_of_Le K O h1, le_of_Le K O h2⟩

theorem le_dest' {m p : Nat} (h : m ≤ p) : ∃ d, m + d = p := by
  induction h with
  | refl => exact ⟨0, rfl⟩
  | step _ ih =>
    cases ih with
    | intro d hd => exact ⟨d + 1, congrArg Nat.succ hd⟩

theorem close_mono (P : Pos K) {e e' a b : R} (h : Close K P e a b) (he : LibGron.le K P e e') : Close K P e' a b :=
  ⟨LibGron.le_trans K P h.1 he, LibGron.le_trans K P h.2 he⟩

theorem cauchy_of_sq_rate (s b : Nat → R)
    (hb : ∀ j J, Le K O.toCone (K.mul (sub K (s (j + J)) (s j)) (sub K (s (j + J)) (s j))) (b j))
    (arch : ∀ e, PosE K (posO K O) e → ∃ q N, O.nn q ∧ Le K O.toCone (K.add q q) e ∧ Le K O.toCone (b N) (K.mul q q)) :
    Cauchy K (posO K O) s := by
  intro e he
  cases arch e he with
  | intro q hq' =>
    cases hq' with
    | intro N hN =>
      refine ⟨N, fun m n hm hn => ?_⟩
      cases le_dest' hm with
      | intro k hk =>
        cases le_dest' hn with
        | intro k' hk' =>
          have c1 := close_of_sq K O (s (N + k)) (s N) q hN.1 (le_trans' K O.toCone _ _ _ (hb N k) hN.2.2)
          have c2 := close_of_sq K O (s (N + k')) (s N) q hN.1 (le_trans' K O.toCone _ _ _ (hb N k') hN.2.2)
          rw [hk] at c1
          rw [hk'] at c2
          exact close_mono K (posO K O) (close_tri K (posO K O) c1 (close_symm K (posO K O) c2)) (le_of_Le K O hN.2.1)

end Gen
end RealBridge

#print axioms RealBridge.le_of_Le
#print axioms RealBridge.close_of_sq
#print axioms RealBridge.le_dest'
#print axioms RealBridge.close_mono
#print axioms RealBridge.cauchy_of_sq_rate

ns/RealCoef.lean

lean_all/ns/RealCoef.lean

/-
  ns/RealCoef.lean — T37 cycle 1872 (goal G860c3): every Fourier coefficient component of the step-halving sequence
  is a Cauchy sequence of the shared Cauchy-real library, in any Archimedean ordered field (in particular ℚ).
    comp_x/y/z     (v.x)², (v.y)², (v.z)² ≤ |v|²
    le_mu          μ ≥ 1, d ≥ 0  ⇒  d ≤ μ·d
    coef_real      with the hypotheses of DyadicCoef.coef_cauchy, h·2 = 1 and μ_r ≥ 1, for any component selector f
                   (f(a − b) = f a − f b, (f v)² ≤ |v|²): j ↦ f(x_j(r)) is Cauchy
    coef_real_x/y/z  the three components
  So the limit coefficients of NS_LOG cycle 1175 exist as elements of the Cauchy reals (lib/CReal) — no choice.
  Lean 4 core plus ns/DyadicCoef, ns/RealArch.
-/
import ns.DyadicCoef
import ns.RealArch

namespace RealCoef
open DetChain NSChain DyadicSmallGlobal LibRing DyadicLarge FourierGalerkin ForceBound EnstDiff TimeLipN WCS EnstGronwall
  ForceBilin ForceDiff Dyadic LibCS LibGron LibCauchy RealBridge RealArch DyadicCoef

section Gen
variable {R : Type} (K : CRing R) (O : OrdR K) {I : Type}

theorem comp_x (v : V3 R) : Le K O.toCone (K.mul v.x v.x) (dot3 K v v) :=
  ⟨K.add (K.mul v.y v.y) (K.mul v.z v.z), O.nn_add _ _ (O.nn_sq _) (O.nn_sq _), by unfold dot3; lib_ring⟩
theorem comp_y (v : V3 R) : Le K O.toCone (K.mul v.y v.y) (dot3 K v v) :=
  ⟨K.add (K.mul v.x v.x) (K.mul v.z v.z), O.nn_add _ _ (O.nn_sq _) (O.nn_sq _), by unfold dot3; lib_ring⟩
theorem comp_z (v : V3 R) : Le K O.toCone (K.mul v.z v.z) (dot3 K v v) :=
  ⟨K.add (K.mul v.x v.x) (K.mul v.y v.y), O.nn_add _ _ (O.nn_sq _) (O.nn_sq _), by unfold dot3; lib_ring⟩

theorem le_mu (μ d : R) (hμ : Le K O.toCone K.one μ) (hd : O.nn d) : Le K O.toCone d (K.mul μ d) := by
  cases hμ with
  | intro e he =>
    refine ⟨K.mul e d, O.nn_mul _ _ he.1 hd, ?_⟩
    rw [← he.2]; lib_ring

variable (inv : R → R) (mul_inv : ∀ y, y ≠ K.zero → K.mul y (inv y) = K.one) (h10 : K.one ≠ K.zero)

include mul_inv h10 in
theorem coef_real (arch : ∀ x, ∃ n, Le K O.toCone x (castN K n))
    (f : V3 R → R) (hf : ∀ a b, f (sub3 K a b) = sub K (f a) (f b)) (hfc : ∀ v, Le K O.toCone (K.mul (f v) (f v)) (dot3 K v v))
    (l : List I) (μ : I → R) (hμ : ∀ r, O.nn (μ r)) (x : Nat → I → V3 R) (C k0 h p q cp cr : R)
    (hC : O.nn C) (hk0 : O.nn k0) (hh : O.nn h) (hp : O.nn p) (hq : O.nn q) (hcp : O.nn cp) (hcr : O.nn cr)
    (hpq : K.mul p q = K.one) (hgp : K.mul cp (K.add K.one (K.neg p)) = K.one)
    (hgr : K.mul cr (K.add K.one (K.neg (K.mul q h))) = K.one)
    (hd : ∀ i, Le K O.toCone (lsum K (fun r => K.mul (μ r) (dot3 K (dF K (x (i + 1)) (x i) r) (dF K (x (i + 1)) (x i) r))) l)
      (K.mul C (K.mul k0 (pwr K h i))))
    (hh2 : K.mul h (tw K) = K.one) (r : I) (hr : r ∈ l) (hμ1 : Le K O.toCone K.one (μ r)) :
    Cauchy K (posO K O) (fun j => f (x j r)) := by
  refine cauchy_half K O inv mul_inv h10 arch (K.mul (K.mul (K.mul cp C) k0) cr) h hh hh2 (fun j => f (x j r)) ?_
  intro j J
  have c := coef_cauchy K O l μ hμ x C k0 h p q cp cr hC hk0 hh hp hq hcp hcr hpq hgp hgr hd j J r hr
  have s1 := hfc (sub3 K (x (j + J) r) (x j r))
  rw [hf] at s1
  have s2 := le_mu K O (μ r) _ hμ1 (dot3_nn K O.toCone (sub3 K (x (j + J) r) (x j r)))
  refine le_trans' K O.toCone _ _ _ s1 (le_trans' K O.toCone _ _ _ s2 (le_trans' K O.toCone _ _ _ c ⟨K.zero, O.nn_zero, ?_⟩))
  lib_ring

include mul_inv h10 in
theorem coef_real_x (arch : ∀ x, ∃ n, Le K O.toCone x (castN K n))
    (l : List I) (μ : I → R) (hμ : ∀ r, O.nn (μ r)) (x : Nat → I → V3 R) (C k0 h p q cp cr : R)
    (hC : O.nn C) (hk0 : O.nn k0) (hh : O.nn h) (hp : O.nn p) (hq : O.nn q) (hcp : O.nn cp) (hcr : O.nn cr)
    (hpq : K.mul p q = K.one) (hgp : K.mul cp (K.add K.one (K.neg p)) = K.one)
    (hgr : K.mul cr (K.add K.one (K.neg (K.mul q h))) = K.one)
    (hd : ∀ i, Le K O.toCone (lsum K (fun r => K.mul (μ r) (dot3 K (dF K (x (i + 1)) (x i) r) (dF K (x (i + 1)) (x i) r))) l)
      (K.mul C (K.mul k0 (pwr K h i))))
    (hh2 : K.mul h (tw K) = K.one) (r : I) (hr : r ∈ l) (hμ1 : Le K O.toCone K.one (μ r)) :
    Cauchy K (posO K O) (fun j => (x j r).x) :=
  coef_real K O inv mul_inv h10 arch (fun v => v.x) (fun _ _ => rfl) (comp_x K O) l μ hμ x C k0 h p q cp cr
    hC hk0 hh hp hq hcp hcr hpq hgp hgr hd hh2 r hr hμ1

include mul_inv h10 in
theorem coef_real_y (arch : ∀ x, ∃ n, Le K O.toCone x (castN K n))
    (l : List I) (μ : I → R) (hμ : ∀ r, O.nn (μ r)) (x : Nat → I → V3 R) (C k0 h p q cp cr : R)
    (hC : O.nn C) (hk0 : O.nn k0) (hh : O.nn h) (hp : O.nn p) (hq : O.nn q) (hcp : O.nn cp) (hcr : O.nn cr)
    (hpq : K.mul p q = K.one) (hgp : K.mul cp (K.add K.one (K.neg p)) = K.one)
    (hgr : K.mul cr (K.add K.one (K.neg (K.mul q h))) = K.one)
    (hd : ∀ i, Le K O.toCone (lsum K (fun r => K.mul (μ r) (dot3 K (dF K (x (i + 1)) (x i) r) (dF K (x (i + 1)) (x i) r))) l)
      (K.mul C (K.mul k0 (pwr K h i))))
    (hh2 : K.mul h (tw K) = K.one) (r : I) (hr : r ∈ l) (hμ1 : Le K O.toCone K.one (μ r)) :
    Cauchy K (posO K O) (fun j => (x j r).y) :=
  coef_real K O inv mul_inv h10 arch (fun v => v.y) (fun _ _ => rfl) (comp_y K O) l μ hμ x C k0 h p q cp cr
    hC hk0 hh hp hq hcp hcr hpq hgp hgr hd hh2 r hr hμ1

include mul_inv h10 in
theorem coef_real_z (arch : ∀ x, ∃ n, Le K O.toCone x (castN K n))
    (l : List I) (μ : I → R) (hμ : ∀ r, O.nn (μ r)) (x : Nat → I → V3 R) (C k0 h p q cp cr : R)
    (hC : O.nn C) (hk0 : O.nn k0) (hh : O.nn h) (hp : O.nn p) (hq : O.nn q) (hcp : O.nn cp) (hcr : O.nn cr)
    (hpq : K.mul p q = K.one) (hgp : K.mul cp (K.add K.one (K.neg p)) = K.one)
    (hgr : K.mul cr (K.add K.one (K.neg (K.mul q h))) = K.one)
    (hd : ∀ i, Le K O.toCone (lsum K (fun r => K.mul (μ r) (dot3 K (dF K (x (i + 1)) (x i) r) (dF K (x (i + 1)) (x i) r))) l)
      (K.mul C (K.mul k0 (pwr K h i))))
    (hh2 : K.mul h (tw K) = K.one) (r : I) (hr : r ∈ l) (hμ1 : Le K O.toCone K.one (μ r)) :
    Cauchy K (posO K O) (fun j => (x j r).z) :=
  coef_real K O inv mul_inv h10 arch (fun v => v.z) (fun _ _ => rfl) (comp_z K O) l μ hμ x C k0 h p q cp cr
    hC hk0 hh hp hq hcp hcr hpq hgp hgr hd hh2 r hr hμ1

end Gen
end RealCoef

#print axioms RealCoef.comp_x
#print axioms RealCoef.le_mu
#print axioms RealCoef.coef_real
#print axioms RealCoef.coef_real_x
#print axioms RealCoef.coef_real_y
#print axioms RealCoef.coef_real_z

ns/RealForce.lean

lean_all/ns/RealForce.lean

/-
  ns/RealForce.lean — T37 cycle 1872 (goal G860d1): the Galerkin force of Cauchy coefficients is Cauchy. On a fixed
  finite mode list l, N_r(a, a) is a finite sum of products of coefficient components with fixed wave vectors, so
  componentwise Cauchy coefficients a_j(p) (p ∈ l, e.g. from RealCoef) give componentwise Cauchy forces N_r(a_j, a_j):
    c_lsum      finite sums of Cauchy sequences are Cauchy
    c_dot3      j ↦ ⟨u_j, w⟩ is Cauchy for a fixed vector w
    nl_cauchy   every component of j ↦ N_r(a_j, a_j) is Cauchy
  In the Cauchy reals (lib/CReal), whose operations are pointwise, this is N evaluated at the limit coefficients.
  Lean 4 core plus ns/RealIntegral (ofO), ns/GalerkinForce, lib/CauchyMul.
-/
import ns.RealIntegral
import ns.GalerkinForce
import lib.CauchyMul

namespace RealForce
open DetChain NSChain DyadicSmallGlobal LibRing LibCS LibGron LibCauchy RealBridge RealIntegral FourierGalerkin
  GalerkinForce

section Gen
variable {R : Type} (K : CRing R) (O : OrdR K) {I : Type}
variable (inv : R → R) (mul_inv : ∀ y, y ≠ K.zero → K.mul y (inv y) = K.one) (h10 : K.one ≠ K.zero)

def CauchyV (u : Nat → V3 R) : Prop :=
  Cauchy K (posO K O) (fun j => (u j).x) ∧ Cauchy K (posO K O) (fun j => (u j).y) ∧ Cauchy K (posO K O) (fun j => (u j).z)

include mul_inv in
theorem c_add {s t : Nat → R} (hs : Cauchy K (posO K O) s) (ht : Cauchy K (posO K O) t) :
    Cauchy K (posO K O) (fun j => K.add (s j) (t j)) :=
  cauchy_add K (posO K O) (ofO K O inv mul_inv) hs ht

include mul_inv h10 in
theorem c_mul {s t : Nat → R} (hs : Cauchy K (posO K O) s) (ht : Cauchy K (posO K O) t) :
    Cauchy K (posO K O) (fun j => K.mul (s j) (t j)) :=
  cauchy_mul K (posO K O) (ofO K O inv mul_inv) h10 hs ht

include mul_inv h10 in
theorem c_cmul (c : R) {s : Nat → R} (hs : Cauchy K (posO K O) s) : Cauchy K (posO K O) (fun j => K.mul c (s j)) :=
  c_mul K O inv mul_inv h10 (cauchy_const K (posO K O) c) hs

include mul_inv in
theorem c_lsum (l : List I) (f : I → Nat → R) :
    (∀ p, p ∈ l → Cauchy K (posO K O) (f p)) → Cauchy K (posO K O) (fun j => lsum K (fun p => f p j) l) := by
  induction l with
  | nil => intro _; exact cauchy_const K (posO K O) K.zero
  | cons x l ih =>
    intro h
    exact c_add K O inv mul_inv (h x (List.Mem.head _)) (ih (fun p hp => h p (List.Mem.tail _ hp)))

include mul_inv h10 in
theorem c_dot3 (u : Nat → V3 R) (w : V3 R) (hu : CauchyV K O u) : Cauchy K (posO K O) (fun j => dot3 K (u j) w) := by
  have m := fun (c : R) (s : Nat → R) (hs : Cauchy K (posO K O) s) =>
    c_mul K O inv mul_inv h10 hs (cauchy_const K (posO K O) c)
  exact c_add K O inv mul_inv (c_add K O inv mul_inv (m w.x _ hu.1) (m w.y _ hu.2.1)) (m w.z _ hu.2.2)

include mul_inv h10 in
theorem c_coef (l : List I) (δ : I → I → I → R) (ι : R) (k : I → V3 R) (a : Nat → I → V3 R)
    (ha : ∀ p, p ∈ l → CauchyV K O (fun j => a j p)) (p q r : I) (hp : p ∈ l) :
    Cauchy K (posO K O) (fun j => coef K δ ι k (a j) p q r) :=
  c_cmul K O inv mul_inv h10 (δ p q r) (c_cmul K O inv mul_inv h10 ι (c_dot3 K O inv mul_inv h10 (fun j => a j p) (k q) (ha p hp)))

include mul_inv h10 in
theorem nl_cauchy (l : List I) (δ : I → I → I → R) (ι : R) (k : I → V3 R) (a : Nat → I → V3 R)
    (ha : ∀ p, p ∈ l → CauchyV K O (fun j => a j p)) (r : I) :
    CauchyV K O (fun j => Nl K l δ ι k (a j) (a j) r) := by
  refine ⟨?_, ?_, ?_⟩
  · exact c_lsum K O inv mul_inv l _ (fun p hp => c_lsum K O inv mul_inv l _ (fun q hq =>
      c_mul K O inv mul_inv h10 (c_coef K O inv mul_inv h10 l δ ι k a ha p q r hp) (ha q hq).1))
  · exact c_lsum K O inv mul_inv l _ (fun p hp => c_lsum K O inv mul_inv l _ (fun q hq =>
      c_mul K O inv mul_inv h10 (c_coef K O inv mul_inv h10 l δ ι k a ha p q r hp) (ha q hq).2.1))
  · exact c_lsum K O inv mul_inv l _ (fun p hp => c_lsum K O inv mul_inv l _ (fun q hq =>
      c_mul K O inv mul_inv h10 (c_coef K O inv mul_inv h10 l δ ι k a ha p q r hp) (ha q hq).2.2))

end Gen
end RealForce

#print axioms RealForce.c_lsum
#print axioms RealForce.c_dot3
#print axioms RealForce.c_coef
#print axioms RealForce.nl_cauchy

ns/RealIntegral.lean

lean_all/ns/RealIntegral.lean

/-
  ns/RealIntegral.lean — T37 cycle 1868 (goal G860b, real layer): the integrated Galerkin equation passes to the
  limit in the Cauchy reals.
    ofO           the NS ordered ring with an inverse as an ordered field of lib/OField (LibOF.OF)
    lim_diff      x, y Cauchy and x_j − y_j = S_j for every j  ⇒  S Cauchy and x − y ≈ S
    lim_integral  for step-halving solutions a_j (StepC with step h_j) and grid times n_j: if j ↦ a_j(n_j) and
                  j ↦ a_j(0) are Cauchy, the Riemann sums S_j = Σ_{k<n_j} h_j(N_{j,k} − νw·a_j(k+1)) are Cauchy and
                  lim a_j(n_j) − lim a_j(0) ≈ lim S_j   (the limit of the Riemann sums is the time integral).
  Lean 4 core plus ns/RealBridge, ns/IntegralForm.
-/
import ns.RealBridge
import ns.IntegralForm
import lib.CReal

namespace RealIntegral
open DetChain NSChain DyadicSmallGlobal LibRing LibCS LibGron LibCauchy RealBridge

section Gen
variable {R : Type} (K : CRing R) (O : OrdR K)
variable (inv : R → R) (mul_inv : ∀ y, y ≠ K.zero → K.mul y (inv y) = K.one)

def ofO : LibOF.OF K (posO K O) where
  inv := inv
  mul_inv := mul_inv
  total := fun x => by
    cases O.total K.zero x with
    | inl h =>
      cases h with
      | intro d hd =>
        left
        show O.nn x
        rw [← hd.2, K.zero_add]
        exact hd.1
    | inr h =>
      cases h with
      | intro d hd =>
        right
        show O.nn (K.neg x)
        have e : K.neg x = d := by
          rw [show K.neg x = K.add (K.neg x) (K.add x d) by rw [hd.2]; lib_ring]
          lib_ring
        rw [e]
        exact hd.1
  antisymm := fun x h1 h2 =>
    O.antisym x K.zero ⟨K.neg x, h2, by lib_ring⟩ ⟨x, h1, K.zero_add x⟩

theorem lim_diff (P : Pos K) (F : LibOF.OF K P) (x y S : Nat → R) (hx : Cauchy K P x) (hy : Cauchy K P y)
    (h : ∀ j, K.add (x j) (K.neg (y j)) = S j) :
    Cauchy K P S ∧ Eqv K P (addS K x (negS K y)) S := by
  have c := cauchy_add K P F hx (cauchy_neg K P hy)
  refine ⟨fun e he => ?_, LibCReal.eqv_of_pt K P h⟩
  cases c e he with
  | intro N hN =>
    refine ⟨N, fun m n hm hn => ?_⟩
    have t := hN m n hm hn
    rw [show addS K x (negS K y) m = S m from h m, show addS K x (negS K y) n = S n from h n] at t
    exact t

include mul_inv in
theorem lim_integral (ν w : R) (hj : Nat → R) (a N : Nat → Nat → R) (nT : Nat → Nat)
    (hs : ∀ j k, EnstStep.StepC K (hj j) ν w (a j (k + 1)) (a j k) (N j k))
    (hT : Cauchy K (posO K O) (fun j => a j (nT j))) (h0 : Cauchy K (posO K O) (fun j => a j 0)) :
    Cauchy K (posO K O) (fun j => sumN K (fun k => K.mul (hj j) (K.add (N j k) (K.neg (K.mul (K.mul ν w) (a j (k + 1)))))) (nT j))
    ∧ Eqv K (posO K O) (addS K (fun j => a j (nT j)) (negS K (fun j => a j 0)))
        (fun j => sumN K (fun k => K.mul (hj j) (K.add (N j k) (K.neg (K.mul (K.mul ν w) (a j (k + 1)))))) (nT j)) :=
  lim_diff K (posO K O) (ofO K O inv mul_inv) _ _ _ hT h0
    (fun j => IntegralForm.integral K (hj j) ν w (a j) (N j) (hs j) (nT j))

end Gen
end RealIntegral

#print axioms RealIntegral.ofO
#print axioms RealIntegral.lim_diff
#print axioms RealIntegral.lim_integral

ns/RealLinear.lean

lean_all/ns/RealLinear.lean

/-
  ns/RealLinear.lean — T37 cycle 1880 (goal G860d2c, piece): a linear geometric rate gives a Cauchy sequence in an
  Archimedean ordered field (lib/CauchySeq's Close, the cone of RealBridge):
    geo_id      2h = 1  ⇒  Σ_{i<J} hⁱ + 2hᴶ = 2
    close_tele  |s_{i+1} − s_i| ≤ D·hⁱ for all i  ⇒  |s_{j+J} − s_j| ≤ Σ_{i<J} D·h^{j+i}
    close_geo   …  ≤ 2D·hʲ
    cauchy_lin  |s_{i+1} − s_i| ≤ D·hⁱ (2h = 1, Archimedean)  ⇒  s is Cauchy
  For the Riemann sums of the step-halving scheme (RiemannStep: |S_{j+1} − S_j| ≤ T(ε_j + δ_j) = O(h_j)).
  Lean 4 core plus ns/RealArch, ns/RiemannStep.
-/
import ns.RealArch
import ns.RiemannStep

namespace RealLinear
open DetChain NSChain DyadicSmallGlobal LibRing LibCS LibGron LibCauchy RealBridge RealArch EnstGronwall DyadicLarge
  RiemannStep

section Gen
variable {R : Type} (K : CRing R) (O : OrdR K)

theorem geo_id (h : R) (hh2 : K.mul h (tw K) = K.one) : ∀ J,
    K.add (sumN K (fun i => pwr K h i) J) (K.mul (tw K) (pwr K h J)) = tw K
  | 0 => by show K.add K.zero (K.mul (tw K) K.one) = tw K; lib_ring
  | J + 1 => by
    show K.add (K.add (sumN K (fun i => pwr K h i) J) (pwr K h J)) (K.mul (tw K) (K.mul h (pwr K h J))) = tw K
    have e : K.add (K.add (sumN K (fun i => pwr K h i) J) (pwr K h J)) (K.mul (tw K) (K.mul h (pwr K h J)))
        = K.add (K.add (sumN K (fun i => pwr K h i) J) (pwr K h J)) (K.mul (K.mul h (tw K)) (pwr K h J)) := by lib_ring
    rw [e, hh2, ← geo_id h hh2 J]; unfold tw; lib_ring

theorem close_tele (s : Nat → R) (D h : R) (hs : ∀ i, Close K (posO K O) (K.mul D (pwr K h i)) (s (i + 1)) (s i))
    (j : Nat) : ∀ J, Close K (posO K O) (sumN K (fun i => K.mul D (pwr K h (j + i))) J) (s (j + J)) (s j)
  | 0 => close_refl K (posO K O) O.nn_zero (s j)
  | J + 1 => by
    have a := close_tele s D h hs j J
    have b := hs (j + J)
    have c' := close_tri K (posO K O) b a
    show Close K (posO K O) (K.add (sumN K (fun i => K.mul D (pwr K h (j + i))) J) (K.mul D (pwr K h (j + J)))) (s (j + J + 1)) (s j)
    rw [show K.add (sumN K (fun i => K.mul D (pwr K h (j + i))) J) (K.mul D (pwr K h (j + J)))
        = K.add (K.mul D (pwr K h (j + J))) (sumN K (fun i => K.mul D (pwr K h (j + i))) J) by lib_ring]
    exact c'

theorem pwr_add' (h : R) (j : Nat) : ∀ i, pwr K h (j + i) = K.mul (pwr K h j) (pwr K h i)
  | 0 => by show pwr K h j = K.mul (pwr K h j) K.one; lib_ring
  | i + 1 => by
    show K.mul h (pwr K h (j + i)) = K.mul (pwr K h j) (K.mul h (pwr K h i))
    rw [pwr_add' h j i]; lib_ring

theorem sum_scale (D h : R) (j : Nat) : ∀ J,
    sumN K (fun i => K.mul D (pwr K h (j + i))) J = K.mul (K.mul D (pwr K h j)) (sumN K (fun i => pwr K h i) J)
  | 0 => by show K.zero = K.mul (K.mul D (pwr K h j)) K.zero; lib_ring
  | J + 1 => by
    show K.add (sumN K (fun i => K.mul D (pwr K h (j + i))) J) (K.mul D (pwr K h (j + J)))
      = K.mul (K.mul D (pwr K h j)) (K.add (sumN K (fun i => pwr K h i) J) (pwr K h J))
    rw [sum_scale D h j J, pwr_add' K h j J]; lib_ring

theorem sum_nn (h : R) (hh : O.nn h) : ∀ J, O.nn (sumN K (fun i => pwr K h i) J)
  | 0 => O.nn_zero
  | J + 1 => O.nn_add _ _ (sum_nn h hh J) (pwr_nn K O h hh J)

theorem close_geo (s : Nat → R) (D h : R) (hD : O.nn D) (hh : O.nn h) (hh2 : K.mul h (tw K) = K.one)
    (hs : ∀ i, Close K (posO K O) (K.mul D (pwr K h i)) (s (i + 1)) (s i)) (j J : Nat) :
    Close K (posO K O) (K.mul (tw K) (K.mul D (pwr K h j))) (s (j + J)) (s j) := by
  have a := close_tele K O s D h hs j J
  rw [sum_scale K D h j J] at a
  refine close_mono K (posO K O) a (le_of_Le K O ⟨K.mul (K.mul D (pwr K h j)) (K.mul (tw K) (pwr K h J)),
    O.nn_mul _ _ (O.nn_mul _ _ hD (pwr_nn K O h hh j)) (O.nn_mul _ _ (O.nn_add _ _ (RatModel.nn_one' K O.toCone)
      (RatModel.nn_one' K O.toCone)) (pwr_nn K O h hh J)), ?_⟩)
  calc K.add (K.mul (K.mul D (pwr K h j)) (sumN K (fun i => pwr K h i) J)) (K.mul (K.mul D (pwr K h j)) (K.mul (tw K) (pwr K h J)))
      = K.mul (K.mul D (pwr K h j)) (K.add (sumN K (fun i => pwr K h i) J) (K.mul (tw K) (pwr K h J))) := by lib_ring
    _ = K.mul (tw K) (K.mul D (pwr K h j)) := by rw [geo_id K h hh2 J]; lib_ring

variable (inv : R → R) (mul_inv : ∀ y, y ≠ K.zero → K.mul y (inv y) = K.one) (h10 : K.one ≠ K.zero)

include mul_inv in
theorem cauchy_lin (arch : ∀ x, ∃ n, Le K O.toCone x (castN K n)) (s : Nat → R) (D h : R) (hD : O.nn D)
    (hh : O.nn h) (hh2 : K.mul h (tw K) = K.one)
    (hs : ∀ i, Close K (posO K O) (K.mul D (pwr K h i)) (s (i + 1)) (s i)) : Cauchy K (posO K O) s := by
  intro e he
  have hen : O.nn e := he.1
  have hy := mul_inv e he.2
  have ntw : O.nn (tw K) := O.nn_add _ _ (RatModel.nn_one' K O.toCone) (RatModel.nn_one' K O.toCone)
  cases arch (K.mul (K.mul (K.mul (tw K) (tw K)) D) (inv e)) with
  | intro n hn =>
    have h1 := le_trans' K O.toCone _ _ _ hn (cast_le_pow K O n).2
    have m1 := le_mul_nn K O.toCone (K.mul e (pwr K h n)) _ _ (O.nn_mul _ _ hen (pwr_nn K O h hh n)) h1
    have e1 : K.mul (K.mul e (pwr K h n)) (K.mul (K.mul (K.mul (tw K) (tw K)) D) (inv e))
        = K.mul (K.mul (K.mul (K.mul (tw K) (tw K)) D) (pwr K h n)) (K.mul e (inv e)) := by lib_ring
    have e2 : K.mul (K.mul e (pwr K h n)) (pwr K (tw K) n) = K.mul e (pwr K (K.mul h (tw K)) n) := by
      rw [← pwr_mul2 K h (tw K) n]; lib_ring
    rw [e1, hy, e2, hh2, pwr_one'] at m1
    rw [show K.mul (K.mul (K.mul (K.mul (tw K) (tw K)) D) (pwr K h n)) K.one
        = K.mul (K.mul (K.mul (tw K) (tw K)) D) (pwr K h n) by lib_ring, show K.mul e K.one = e by lib_ring] at m1
    refine ⟨n, fun a b ha hb => ?_⟩
    cases le_dest' ha with
    | intro k hk =>
      cases le_dest' hb with
      | intro k' hk' =>
        have c1 := close_geo K O s D h hD hh hh2 hs n k
        have c2 := close_geo K O s D h hD hh hh2 hs n k'
        rw [hk] at c1
        rw [hk'] at c2
        have c := close_tri K (posO K O) c1 (close_symm K (posO K O) c2)
        refine close_mono K (posO K O) c (le_of_Le K O (le_trans' K O.toCone _ _ _ ⟨K.zero, O.nn_zero, ?_⟩ m1))
        show K.add (K.add (K.mul (tw K) (K.mul D (pwr K h n))) (K.mul (tw K) (K.mul D (pwr K h n)))) K.zero
          = K.mul (K.mul (K.mul (tw K) (tw K)) D) (pwr K h n)
        unfold tw; lib_ring

end Gen
end RealLinear

#print axioms RealLinear.geo_id
#print axioms RealLinear.close_tele
#print axioms RealLinear.close_geo
#print axioms RealLinear.cauchy_lin

ns/RealSqrtRate.lean

lean_all/ns/RealSqrtRate.lean

/-
  ns/RealSqrtRate.lean — T37 cycle 1890 (goal G860d2c, rate √(1/2)): a squared linear rate still gives a Cauchy
  sequence. The step-size estimate (StepUniformS) bounds squared distances by O(k_j) = O(2^{−j}), so coefficient
  differences per level are O(2^{−j/2}) — irrational ratio. Grouping levels in pairs avoids square roots:
    sq_bound     D·h^{2i} ≤ ((D + 1)·hⁱ)²   (D ≥ 0)
    pwr_le_one   0 ≤ h, 2h = 1  ⇒  hᵏ ≤ 1;   pwr_anti  h^{N+k} ≤ hᴺ
    parity       every m is i + i or i + i + 1;  half_ge   N + N ≤ m = i + i (+1)  ⇒  N ≤ i
    cauchy_sq    (s_{j+1} − s_j)² ≤ D·hʲ for all j (2h = 1, Archimedean)  ⇒  s is Cauchy
  (even subsequence t_i = s_{2i} has the linear rate 2(D + 1)hⁱ → RealLinear.close_geo; odd terms are within
  (D + 1)hⁱ of the even ones).
  Lean 4 core plus ns/RealLinear.
-/
import ns.RealLinear

namespace RealSqrtRate
open DetChain NSChain DyadicSmallGlobal LibRing LibCS LibGron LibCauchy RealBridge RealArch EnstGronwall DyadicLarge
  RiemannStep RealLinear

theorem parity : ∀ m : Nat, ∃ i, m = i + i ∨ m = i + i + 1
  | 0 => ⟨0, Or.inl rfl⟩
  | m + 1 => by
    cases parity m with
    | intro i h =>
      cases h with
      | inl h => exact ⟨i, Or.inr (by rw [h])⟩
      | inr h => exact ⟨i + 1, Or.inl (by rw [h]; exact congrArg Nat.succ (Nat.succ_add i i).symm)⟩

theorem half_ge (N m i : Nat) (hm : N + N ≤ m) (h : m = i + i ∨ m = i + i + 1) : N ≤ i := by
  cases Nat.lt_or_ge i N with
  | inr g => exact g
  | inl l =>
    exfalso
    have a : (i + 1) + (i + 1) ≤ N + N := Nat.add_le_add l l
    have e : (i + 1) + (i + 1) = i + i + 1 + 1 := congrArg Nat.succ (Nat.succ_add i i)
    have hm' : m ≤ i + i + 1 := by
      cases h with
      | inl h => rw [h]; exact Nat.le_succ _
      | inr h => rw [h]; exact Nat.le_refl _
    rw [e] at a
    exact Nat.lt_irrefl _ (Nat.lt_of_lt_of_le (Nat.lt_of_le_of_lt hm' (Nat.lt_succ_self _)) (Nat.le_trans a hm))

section Gen
variable {R : Type} (K : CRing R) (O : OrdR K)

theorem sq_bound (D x : R) (hD : O.nn D) :
    Le K O.toCone (K.mul D (K.mul x x)) (K.mul (K.mul (K.add D K.one) x) (K.mul (K.add D K.one) x)) :=
  ⟨K.mul (K.add (K.mul D D) (K.add D K.one)) (K.mul x x),
    O.nn_mul _ _ (O.nn_add _ _ (O.nn_sq D) (O.nn_add _ _ hD (RatModel.nn_one' K O.toCone))) (O.nn_sq x), by lib_ring⟩

theorem h_le_one (h : R) (hh : O.nn h) (hh2 : K.mul h (tw K) = K.one) : Le K O.toCone h K.one :=
  ⟨h, hh, by rw [← hh2]; unfold tw; lib_ring⟩

theorem pwr_le_one (h : R) (hh : O.nn h) (hh2 : K.mul h (tw K) = K.one) : ∀ k, Le K O.toCone (pwr K h k) K.one
  | 0 => le_refl' K O.toCone _
  | k + 1 => by
    have a := le_mul_nn K O.toCone h _ _ hh (pwr_le_one h hh hh2 k)
    rw [show K.mul h K.one = h by lib_ring] at a
    exact le_trans' K O.toCone _ _ _ a (h_le_one K O h hh hh2)

theorem pwr_anti (h : R) (hh : O.nn h) (hh2 : K.mul h (tw K) = K.one) (N k : Nat) :
    Le K O.toCone (pwr K h (N + k)) (pwr K h N) := by
  rw [pwr_add' K h N k]
  have a := le_mul_nn K O.toCone (pwr K h N) _ _ (pwr_nn K O h hh N) (pwr_le_one K O h hh hh2 k)
  rw [show K.mul (pwr K h N) K.one = pwr K h N by lib_ring] at a
  exact a

variable (inv : R → R) (mul_inv : ∀ y, y ≠ K.zero → K.mul y (inv y) = K.one)

include mul_inv in
theorem cauchy_sq (arch : ∀ x, ∃ n, Le K O.toCone x (castN K n)) (s : Nat → R) (D h : R) (hD : O.nn D)
    (hh : O.nn h) (hh2 : K.mul h (tw K) = K.one)
    (hs : ∀ j, Le K O.toCone (K.mul (sub K (s (j + 1)) (s j)) (sub K (s (j + 1)) (s j))) (K.mul D (pwr K h j))) :
    Cauchy K (posO K O) s := by
  have hq : O.nn (K.add D K.one) := O.nn_add _ _ hD (RatModel.nn_one' K O.toCone)
  have ntw : O.nn (tw K) := O.nn_add _ _ (RatModel.nn_one' K O.toCone) (RatModel.nn_one' K O.toCone)
  -- odd step: |s_{2i+1} − s_{2i}| ≤ q hⁱ;  even step: |s_{2i+2} − s_{2i+1}| ≤ q hⁱ
  have c1 : ∀ i, Close K (posO K O) (K.mul (K.add D K.one) (pwr K h i)) (s (i + i + 1)) (s (i + i)) := by
    intro i
    refine close_of_sq K O _ _ _ (O.nn_mul _ _ hq (pwr_nn K O h hh i)) (le_trans' K O.toCone _ _ _ (hs (i + i)) ?_)
    rw [pwr_add' K h i i]; exact sq_bound K O D (pwr K h i) hD
  have c2 : ∀ i, Close K (posO K O) (K.mul (K.add D K.one) (pwr K h i)) (s (i + i + 1 + 1)) (s (i + i + 1)) := by
    intro i
    refine close_of_sq K O _ _ _ (O.nn_mul _ _ hq (pwr_nn K O h hh i)) (le_trans' K O.toCone _ _ _ (hs (i + i + 1)) ?_)
    refine le_trans' K O.toCone _ _ _ (le_mul_nn K O.toCone D _ _ hD (pwr_anti K O h hh hh2 (i + i) 1)) ?_
    rw [pwr_add' K h i i]; exact sq_bound K O D (pwr K h i) hD
  -- the even subsequence t_i = s_{2i} has the linear rate (tw·q)·hⁱ
  have ct : ∀ i, Close K (posO K O) (K.mul (K.mul (tw K) (K.add D K.one)) (pwr K h i)) (s ((i + 1) + (i + 1))) (s (i + i)) := by
    intro i
    have c := close_tri K (posO K O) (c2 i) (c1 i)
    rw [show (i + 1) + (i + 1) = i + i + 1 + 1 from congrArg Nat.succ (Nat.succ_add i i)]
    rw [show K.add (K.mul (K.add D K.one) (pwr K h i)) (K.mul (K.add D K.one) (pwr K h i))
        = K.mul (K.mul (tw K) (K.add D K.one)) (pwr K h i) by unfold tw; lib_ring] at c
    exact c
  have geo := fun N J => close_geo K O (fun i => s (i + i)) (K.mul (tw K) (K.add D K.one)) h
    (O.nn_mul _ _ ntw hq) hh hh2 ct N J
  -- choose N with Kc·hᴺ ≤ e, Kc = (q + tw·(tw·q)) + (tw·(tw·q) + q)
  intro e he
  have hen : O.nn e := he.1
  have hy := mul_inv e he.2
  cases arch (K.mul (K.mul (tw K) (K.add (K.add D K.one) (K.mul (tw K) (K.mul (tw K) (K.add D K.one))))) (inv e)) with
  | intro N hN =>
    have h1 := le_trans' K O.toCone _ _ _ hN (cast_le_pow K O N).2
    have m1 := le_mul_nn K O.toCone (K.mul e (pwr K h N)) _ _ (O.nn_mul _ _ hen (pwr_nn K O h hh N)) h1
    have e2 : K.mul (K.mul e (pwr K h N)) (pwr K (tw K) N) = K.mul e (pwr K (K.mul h (tw K)) N) := by
      rw [← pwr_mul2 K h (tw K) N]; lib_ring
    rw [e2, hh2, pwr_one', show K.mul e K.one = e by lib_ring,
      show K.mul (K.mul e (pwr K h N)) (K.mul (K.mul (tw K) (K.add (K.add D K.one) (K.mul (tw K) (K.mul (tw K) (K.add D K.one))))) (inv e))
        = K.mul (K.mul (K.mul (tw K) (K.add (K.add D K.one) (K.mul (tw K) (K.mul (tw K) (K.add D K.one))))) (pwr K h N)) (K.mul e (inv e)) by lib_ring,
      hy, show K.mul (K.mul (K.mul (tw K) (K.add (K.add D K.one) (K.mul (tw K) (K.mul (tw K) (K.add D K.one))))) (pwr K h N)) K.one
        = K.mul (K.mul (tw K) (K.add (K.add D K.one) (K.mul (tw K) (K.mul (tw K) (K.add D K.one))))) (pwr K h N) by lib_ring] at m1
    -- every index m ≥ N + N is within q·hᴺ + tw·(tw·q)·hᴺ of s_{2N}
    have near : ∀ m, N + N ≤ m → Close K (posO K O)
        (K.add (K.mul (K.add D K.one) (pwr K h N)) (K.mul (tw K) (K.mul (K.mul (tw K) (K.add D K.one)) (pwr K h N)))) (s m) (s (N + N)) := by
      intro m hm
      cases parity m with
      | intro i hi =>
        have hNi := half_ge N m i hm hi
        cases le_dest' hNi with
        | intro J hJ =>
          have g := geo N J
          rw [hJ] at g
          have g' : Close K (posO K O) (K.mul (tw K) (K.mul (K.mul (tw K) (K.add D K.one)) (pwr K h N))) (s (i + i)) (s (N + N)) := g
          have qa : Le K O.toCone (K.mul (K.add D K.one) (pwr K h i)) (K.mul (K.add D K.one) (pwr K h N)) := by
            rw [← hJ]; exact le_mul_nn K O.toCone _ _ _ hq (pwr_anti K O h hh hh2 N J)
          cases hi with
          | inl e0 =>
            rw [e0]
            exact close_mono K (posO K O) g' (le_of_Le K O ⟨K.mul (K.add D K.one) (pwr K h N),
              O.nn_mul _ _ hq (pwr_nn K O h hh N), by lib_ring⟩)
          | inr e1 =>
            rw [e1]
            have c := close_tri K (posO K O) (close_mono K (posO K O) (c1 i) (le_of_Le K O qa)) g'
            exact c
    refine ⟨N + N, fun m n hm hn => ?_⟩
    have c := close_tri K (posO K O) (near m hm) (close_symm K (posO K O) (near n hn))
    refine close_mono K (posO K O) c (le_of_Le K O (le_trans' K O.toCone _ _ _ ⟨K.zero, O.nn_zero, ?_⟩ m1))
    unfold tw; lib_ring

end Gen
end RealSqrtRate

#print axioms RealSqrtRate.parity
#print axioms RealSqrtRate.half_ge
#print axioms RealSqrtRate.sq_bound
#print axioms RealSqrtRate.pwr_anti
#print axioms RealSqrtRate.cauchy_sq

ns/RestrStep.lean

lean_all/ns/RestrStep.lean

/-
  ns/RestrStep.lean — T37 cycle 1045 (goal G684, piece b1): the restriction of a large-truncation solution solves the
  small-truncation scheme up to the truncation error. For a solution A of the implicit-viscosity scheme on the mode list
  l with force N_l(A, A), and the sub-truncation l_N = l.filter inN, the restricted field a = A|_N satisfies, at every
  mode r of l_N,
    restr_step   (1 + hνw_r) a'_r = a_r + h (N_{l_N}(a, a)_r + E_r),    E = N_l(A, A) − N_{l_N}(A, A),
  using N_{l_N}(a, a) = N_{l_N}(A, A) (TruncError.restr_eq twice, restriction is idempotent).

  Lean 4 core plus ns/TruncError, ns/EnstStep (Step).
-/
import ns.TruncError

namespace RestrStep
open DetChain NSChain LibRing FourierGalerkin GalerkinForce ForceBilin ForceTwoScale EnstDiff EnstStep TruncError

section Gen
variable {R : Type} (K : CRing R) {I : Type}

theorem restr_idem (inN : I → Bool) (u : I → V3 R) (p : I) : restr K inN (restr K inN u) p = restr K inN u p := by
  show cond (inN p) (cond (inN p) (u p) (z3 K)) (z3 K) = cond (inN p) (u p) (z3 K)
  cases inN p <;> rfl

theorem force_restr (l : List I) (inN : I → Bool) (δ : I → I → I → R) (ι : R) (k A : I → V3 R) (r : I) :
    Nl K (l.filter inN) δ ι k (restr K inN A) (restr K inN A) r = Nl K (l.filter inN) δ ι k A A r := by
  rw [restr_eq K l inN δ ι k (restr K inN A) (restr K inN A) r, restr_eq K l inN δ ι k A A r]
  exact ForceLip.Nl_congr K l δ ι k _ _ _ _ r (restr_idem K inN A) (restr_idem K inN A)

theorem restr_step (l : List I) (inN : I → Bool) (δ : I → I → I → R) (ι h ν : R) (w : I → R) (k : I → V3 R)
    (A : Nat → I → V3 R) (hA : ∀ t r, Step K h ν (w r) (A (t + 1) r) (A t r) (Nl K l δ ι k (A t) (A t) r))
    (t : Nat) (r : I) (hr : inN r = true) :
    Step K h ν (w r) (restr K inN (A (t + 1)) r) (restr K inN (A t) r)
      (add3 K (Nl K (l.filter inN) δ ι k (restr K inN (A t)) (restr K inN (A t)) r)
        (sub3 K (Nl K l δ ι k (A t) (A t) r) (Nl K (l.filter inN) δ ι k (A t) (A t) r))) := by
  rw [force_restr K l inN δ ι k (A t) r]
  have e1 : restr K inN (A (t + 1)) r = A (t + 1) r := by show cond (inN r) _ _ = _; rw [hr]; rfl
  have e0 : restr K inN (A t) r = A t r := by show cond (inN r) _ _ = _; rw [hr]; rfl
  rw [e1, e0]
  have hs := hA t r
  generalize Nl K (l.filter inN) δ ι k (A t) (A t) r = Q at hs ⊢
  generalize Nl K l δ ι k (A t) (A t) r = P at hs ⊢
  have ex : K.add Q.x (K.add P.x (K.neg Q.x)) = P.x := by lib_ring
  have ey : K.add Q.y (K.add P.y (K.neg Q.y)) = P.y := by lib_ring
  have ez : K.add Q.z (K.add P.z (K.neg Q.z)) = P.z := by lib_ring
  refine ⟨?_, ?_, ?_⟩
  · show StepC K h ν (w r) _ _ (K.add Q.x (K.add P.x (K.neg Q.x))); rw [ex]; exact hs.1
  · show StepC K h ν (w r) _ _ (K.add Q.y (K.add P.y (K.neg Q.y))); rw [ey]; exact hs.2.1
  · show StepC K h ν (w r) _ _ (K.add Q.z (K.add P.z (K.neg Q.z))); rw [ez]; exact hs.2.2

end Gen

end RestrStep

#print axioms RestrStep.restr_idem
#print axioms RestrStep.force_restr
#print axioms RestrStep.restr_step

ns/Riccati.lean

lean_all/ns/Riccati.lean

/-
  ns/Riccati.lean — T37 cycle 48: the discrete Riccati comparison (local existence time).

  Lean 4 core plus NSChain (CRing, Cone, Le) and lib/RingTac. A nonnegative sequence with
      z_{k+1} ≤ z_k + δ z_k²      (the explicit-Euler form of Z' ≤ C Z², δ = hC)
  and z_0 ≤ B stays below 2B for N steps whenever N·δ·(2B)² ≤ B: the discrete local existence time
  N h ≈ 1/(4 C Z_0), which depends only on the initial size, not on the truncation. With the triad bound
  (TriadBound) summed into Z' ≤ C Z² (R1-gap: the C uniform in N), this is the local regularity step.
    le_add, le_mul_left, le_sq, rep_mono     cone arithmetic
    riccati_inv    z_k ≤ B + k·δ(2B)²  for k ≤ N
    riccati        z_k ≤ 2B           for k ≤ N
-/
import NSChain
import lib.RingTac

namespace Riccati
open DetChain NSChain

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

def rep (x : R) : Nat → R
  | 0 => K.zero
  | n + 1 => K.add (rep x n) x

theorem le_add (P : Cone K) (x y u v : R) (h1 : Le K P x y) (h2 : Le K P u v) :
    Le K P (K.add x u) (K.add y v) := by
  cases h1 with
  | intro d1 h1 => cases h2 with
    | intro d2 h2 =>
      refine ⟨K.add d1 d2, P.nn_add _ _ h1.1 h2.1, ?_⟩
      rw [← h1.2, ← h2.2]; lib_ring

theorem le_mul_left (P : Cone K) (c x y : R) (hc : P.nn c) (h : Le K P x y) :
    Le K P (K.mul c x) (K.mul c y) := by
  cases h with
  | intro d h =>
    refine ⟨K.mul c d, P.nn_mul _ _ hc h.1, ?_⟩
    rw [← h.2]; lib_ring

theorem le_sq (P : Cone K) (x y : R) (hx : P.nn x) (h : Le K P x y) :
    Le K P (K.mul x x) (K.mul y y) := by
  cases h with
  | intro d h =>
    refine ⟨K.add (K.mul (K.add x x) d) (K.mul d d),
      P.nn_add _ _ (P.nn_mul _ _ (P.nn_add _ _ hx hx) h.1) (P.nn_sq d), ?_⟩
    rw [← h.2]; lib_ring

theorem rep_nn (P : Cone K) (q : R) (hq : P.nn q) : ∀ n, P.nn (rep K q n)
  | 0 => P.nn_zero
  | n + 1 => P.nn_add _ _ (rep_nn P q hq n) hq

theorem rep_mono (P : Cone K) (q : R) (hq : P.nn q) (k : Nat) : ∀ j, Le K P (rep K q k) (rep K q (k + j))
  | 0 => le_refl' K P _
  | j + 1 => le_trans' K P _ _ _ (rep_mono P q hq k j) ⟨q, hq, rfl⟩

theorem le_ex {k N : Nat} (h : k ≤ N) : ∃ j, N = k + j := by
  induction h with
  | refl => exact ⟨0, rfl⟩
  | step _ ih => cases ih with
    | intro j hj => exact ⟨j + 1, by rw [hj]; rfl⟩

theorem riccati_inv (P : Cone K) (z : Nat → R) (B δ : R) (N : Nat)
    (hz : ∀ k, P.nn (z k)) (hδ : P.nn δ)
    (step : ∀ k, Le K P (z (k + 1)) (K.add (z k) (K.mul δ (K.mul (z k) (z k)))))
    (h0 : Le K P (z 0) B)
    (hN : Le K P (rep K (K.mul δ (K.mul (K.add B B) (K.add B B))) N) B) :
    ∀ k, k ≤ N → Le K P (z k) (K.add B (rep K (K.mul δ (K.mul (K.add B B) (K.add B B))) k))
  | 0, _ => by
    show Le K P (z 0) (K.add B K.zero)
    rw [add_zero']; exact h0
  | k + 1, hk => by
    have q_nn : P.nn (K.mul δ (K.mul (K.add B B) (K.add B B))) := P.nn_mul _ _ hδ (P.nn_sq _)
    have ih := riccati_inv P z B δ N hz hδ step h0 hN k (Nat.le_of_succ_le hk)
    -- z_k ≤ 2B
    cases le_ex (Nat.le_of_succ_le hk) with
    | intro j hj =>
    have r : Le K P (rep K (K.mul δ (K.mul (K.add B B) (K.add B B))) k) B := by
      have m := rep_mono K P _ q_nn k j
      rw [← hj] at m
      exact le_trans' K P _ _ _ m hN
    have z2 : Le K P (z k) (K.add B B) := le_trans' K P _ _ _ ih (le_add K P _ _ _ _ (le_refl' K P B) r)
    have sq := le_mul_left K P δ _ _ hδ (le_sq K P _ _ (hz k) z2)
    refine le_trans' K P _ _ _ (step k) ?_
    have e : K.add B (rep K (K.mul δ (K.mul (K.add B B) (K.add B B))) (k + 1))
        = K.add (K.add B (rep K (K.mul δ (K.mul (K.add B B) (K.add B B))) k))
            (K.mul δ (K.mul (K.add B B) (K.add B B))) := by
      show K.add B (K.add _ _) = _; rw [K.add_assoc]
    rw [e]
    exact le_add K P _ _ _ _ ih sq

/-- **Discrete local existence**: z_k ≤ 2B for every k ≤ N. -/
theorem riccati (P : Cone K) (z : Nat → R) (B δ : R) (N : Nat)
    (hz : ∀ k, P.nn (z k)) (hδ : P.nn δ)
    (step : ∀ k, Le K P (z (k + 1)) (K.add (z k) (K.mul δ (K.mul (z k) (z k)))))
    (h0 : Le K P (z 0) B)
    (hN : Le K P (rep K (K.mul δ (K.mul (K.add B B) (K.add B B))) N) B)
    (k : Nat) (hk : k ≤ N) : Le K P (z k) (K.add B B) := by
  have q_nn : P.nn (K.mul δ (K.mul (K.add B B) (K.add B B))) := P.nn_mul _ _ hδ (P.nn_sq _)
  cases le_ex hk with
  | intro j hj =>
  have m := rep_mono K P _ q_nn k j
  rw [← hj] at m
  exact le_trans' K P _ _ _ (riccati_inv K P z B δ N hz hδ step h0 hN k hk)
    (le_add K P _ _ _ _ (le_refl' K P B) (le_trans' K P _ _ _ m hN))

end Riccati

#print axioms Riccati.le_add
#print axioms Riccati.le_mul_left
#print axioms Riccati.le_sq
#print axioms Riccati.rep_nn
#print axioms Riccati.rep_mono
#print axioms Riccati.le_ex
#print axioms Riccati.riccati_inv
#print axioms Riccati.riccati

ns/Riccati32.lean

lean_all/ns/Riccati32.lean

/-
  ns/Riccati32.lean — T37 cycle 114: the discrete Riccati comparison with exponent 3/2 (square-root free).

  Lean 4 core plus the shared lib/OField (ordered field: totality, antisymmetry, inverses), lib/Gronwall
  (le arithmetic). [DecidableEq R] is a hypothesis on R (no classical logic).
    sq_le_mono   0 ≤ Y, y² ≤ Y²  ⇒  y ≤ Y
    riccati32    z_{k+1} ≤ z_k + y_k,  y_k² ≤ δ² z_k³,  z ≥ 0,  z_0 ≤ B,  Y ≥ 0, Y² = δ²(2B)³,  N·Y ≤ B
                 ⇒  z_k ≤ 2B for k ≤ N
  The H²-type local existence time N h ≈ B/(δ(2B)^{3/2}) for Z' ≤ C Z^{3/2} (Z = ‖Δu‖², with TriadH2).
-/
import lib.OField

namespace Riccati32
open DetChain LibRing LibCS LibGron LibOF

variable {R : Type} (K : CRing R) (P : Pos K)

theorem sq_le_mono [DecidableEq R] (F : OF K P) (y Y : R) (hY : P.nn Y) (h : le K P (K.mul y y) (K.mul Y Y)) : le K P y Y := by
  unfold le at *
  cases F.total (sub K Y y) with
  | inl hh => exact hh
  | inr hh =>
    -- d = y − Y ≥ 0
    have hd : P.nn (sub K y Y) := by rw [show sub K y Y = K.neg (sub K Y y) by lib_ring]; exact hh
    -- d(y + Y) = y² − Y² ≥ 0 and ≤ 0  ⇒ = 0
    have hyY : P.nn (K.add y Y) := by
      rw [show K.add y Y = K.add (sub K y Y) (K.add Y Y) by lib_ring]
      exact P.nn_add _ _ hd (P.nn_add _ _ hY hY)
    have p1 : P.nn (sub K (K.mul y y) (K.mul Y Y)) := by
      rw [show sub K (K.mul y y) (K.mul Y Y) = K.mul (sub K y Y) (K.add y Y) by lib_ring]
      exact P.nn_mul _ _ hd hyY
    have z := F.antisymm _ p1 (by rw [show K.neg (sub K (K.mul y y) (K.mul Y Y)) = sub K (K.mul Y Y) (K.mul y y) by lib_ring]; exact h)
    by_cases hs : K.add y Y = K.zero
    · -- y = −Y: Y − y = Y + Y ≥ 0
      rw [show sub K Y y = K.add (K.add Y Y) (K.neg (K.add y Y)) by lib_ring, hs,
        show K.add (K.add Y Y) (K.neg K.zero) = K.add Y Y by lib_ring]
      exact P.nn_add _ _ hY hY
    · -- d = (y² − Y²)·(y + Y)⁻¹ = 0
      have hi := F.mul_inv _ hs
      have d0 : sub K y Y = K.zero := by
        rw [show sub K y Y = K.mul (K.mul (sub K y Y) (K.add y Y)) (F.inv (K.add y Y)) by
          rw [K.mul_assoc, hi]; lib_ring]
        rw [show K.mul (sub K y Y) (K.add y Y) = sub K (K.mul y y) (K.mul Y Y) by lib_ring, z]; lib_ring
      rw [show sub K Y y = K.neg (sub K y Y) by lib_ring, d0, show K.neg K.zero = K.zero by lib_ring]
      exact P.nn_zero

theorem castN_add (a : Nat) : ∀ b, castN K (a + b) = K.add (castN K a) (castN K b)
  | 0 => by show castN K a = K.add (castN K a) K.zero; lib_ring
  | b + 1 => by
    show K.add K.one (castN K (a + b)) = K.add (castN K a) (K.add K.one (castN K b))
    rw [castN_add a b]; lib_ring

theorem le_mul2 {a b c d : R} (ha : P.nn a) (hc : P.nn c) (h1 : le K P a b) (h2 : le K P c d) :
    le K P (K.mul a c) (K.mul b d) := by
  unfold le at *
  rw [show sub K (K.mul b d) (K.mul a c) = K.add (K.mul (sub K b a) (K.add (sub K d c) c)) (K.mul a (sub K d c)) by lib_ring]
  exact P.nn_add _ _ (P.nn_mul _ _ h1 (by
    rw [show K.add (sub K d c) c = d by lib_ring]
    have : P.nn (K.add (sub K d c) c) := P.nn_add _ _ h2 hc
    rw [show K.add (sub K d c) c = d by lib_ring] at this; exact this)) (P.nn_mul _ _ ha h2)

theorem castN_le (Y : R) (hY : P.nn Y) {k N : Nat} (h : k ≤ N) :
    le K P (K.mul (castN K k) Y) (K.mul (castN K N) Y) := by
  have ⟨j, hj⟩ := Nat.le.dest h
  unfold le
  rw [← hj, show sub K (K.mul (castN K (k + j)) Y) (K.mul (castN K k) Y) = K.mul (castN K j) Y by
    rw [castN_add K k j]; lib_ring]
  exact P.nn_mul _ _ (nn_castN K P j) hY

/-- **Riccati with exponent 3/2.** -/
theorem riccati32 [DecidableEq R] (F : OF K P) (z y : Nat → R) (D B Y : R) (N : Nat)
    (hz : ∀ k, P.nn (z k)) (hD : P.nn D) (hY : P.nn Y) (hB : P.nn B)
    (step : ∀ k, le K P (z (k + 1)) (K.add (z k) (y k)))
    (hy : ∀ k, le K P (K.mul (y k) (y k)) (K.mul D (K.mul (z k) (K.mul (z k) (z k)))))
    (hYdef : K.mul Y Y = K.mul D (K.mul (K.add B B) (K.mul (K.add B B) (K.add B B))))
    (h0 : le K P (z 0) B) (hN : le K P (K.mul (castN K N) Y) B) :
    ∀ k, k ≤ N → le K P (z k) (K.add B (K.mul (castN K k) Y)) := by
  intro k
  induction k with
  | zero => intro _; rw [show K.add B (K.mul (castN K 0) Y) = B by show K.add B (K.mul K.zero Y) = B; lib_ring]; exact h0
  | succ k ih =>
    intro hk
    have ihk := ih (Nat.le_of_succ_le hk)
    -- z_k ≤ 2B
    have z2 : le K P (z k) (K.add B B) := le_trans K P ihk (le_trans K P (by
        unfold le; rw [show sub K (K.add B (K.mul (castN K N) Y)) (K.add B (K.mul (castN K k) Y))
          = sub K (K.mul (castN K N) Y) (K.mul (castN K k) Y) by lib_ring]
        exact castN_le K P Y hY (Nat.le_of_succ_le hk)) (by
        unfold le at hN ⊢; rw [show sub K (K.add B B) (K.add B (K.mul (castN K N) Y)) = sub K B (K.mul (castN K N) Y) by lib_ring]
        exact hN))
    have B2 : P.nn (K.add B B) := P.nn_add _ _ hB hB
    have c3 : le K P (K.mul D (K.mul (z k) (K.mul (z k) (z k))))
        (K.mul D (K.mul (K.add B B) (K.mul (K.add B B) (K.add B B)))) :=
      le_mul_left K P hD (le_mul2 K P (hz k) (P.nn_mul _ _ (hz k) (hz k)) z2
        (le_mul2 K P (hz k) (hz k) z2 z2))
    have yY : le K P (y k) Y := sq_le_mono K P F (y k) Y hY (by rw [hYdef]; exact le_trans K P (hy k) c3)
    refine le_trans K P (step k) ?_
    have := le_add2 K P ihk yY
    refine le_trans K P this ?_
    rw [show K.add (K.add B (K.mul (castN K k) Y)) Y = K.add B (K.mul (castN K (k + 1)) Y) by
      show _ = K.add B (K.mul (K.add K.one (castN K k)) Y); lib_ring]
    exact le_refl K P _

end Riccati32

#print axioms Riccati32.sq_le_mono
#print axioms Riccati32.castN_add
#print axioms Riccati32.le_mul2
#print axioms Riccati32.castN_le
#print axioms Riccati32.riccati32

ns/RiccatiGen.lean

lean_all/ns/RiccatiGen.lean

/-
  ns/RiccatiGen.lean — T37 cycle 179: the discrete Riccati comparison for any monotone growth, and the cubic case.

  Lean 4 core plus ns/Riccati (rep, le_add, le_mul_left, le_sq, rep_mono, le_ex), NSChain, lib/RingTac.
    riccati_gen   z_{k+1} ≤ z_k + δ g(z_k), g(x) ≤ G on [0, 2B], z_0 ≤ B, N·δG ≤ B  ⇒  z_k ≤ 2B (k ≤ N)
    cube_mono     0 ≤ x ≤ y  ⇒  x³ ≤ y³
    riccati3      z_{k+1} ≤ z_k + δ z_k³, N·δ(2B)³ ≤ B  ⇒  z_k ≤ 2B (k ≤ N)
  With Young34 (z' ≤ z + h(κ⁴/4ν³)z³): the 3D local existence time from the measured stretching bound,
  N h ≤ ν³/(2κ⁴Z_0²) (δ = hκ⁴/(4ν³), B = Z_0), uniform in the truncation.
-/
import ns.Riccati

namespace RiccatiGen
open DetChain NSChain LibRing Riccati

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

theorem riccati_gen_inv (P : Cone K) (z : Nat → R) (g : R → R) (B G δ : R) (N : Nat)
    (hz : ∀ k, P.nn (z k)) (hδ : P.nn δ) (hG : P.nn G)
    (hg : ∀ x, P.nn x → Le K P x (K.add B B) → Le K P (g x) G)
    (step : ∀ k, Le K P (z (k + 1)) (K.add (z k) (K.mul δ (g (z k)))))
    (h0 : Le K P (z 0) B) (hN : Le K P (rep K (K.mul δ G) N) B) :
    ∀ k, k ≤ N → Le K P (z k) (K.add B (rep K (K.mul δ G) k))
  | 0, _ => by
    show Le K P (z 0) (K.add B K.zero)
    rw [add_zero']; exact h0
  | k + 1, hk => by
    have q_nn : P.nn (K.mul δ G) := P.nn_mul _ _ hδ hG
    have ih := riccati_gen_inv P z g B G δ N hz hδ hG hg step h0 hN k (Nat.le_of_succ_le hk)
    cases le_ex (Nat.le_of_succ_le hk) with
    | intro j hj =>
    have r : Le K P (rep K (K.mul δ G) k) B := by
      have m := rep_mono K P _ q_nn k j
      rw [← hj] at m
      exact le_trans' K P _ _ _ m hN
    have z2 : Le K P (z k) (K.add B B) := le_trans' K P _ _ _ ih (le_add K P _ _ _ _ (le_refl' K P B) r)
    have gg := Riccati.le_mul_left K P δ _ _ hδ (hg (z k) (hz k) z2)
    refine le_trans' K P _ _ _ (step k) ?_
    have e : K.add B (rep K (K.mul δ G) (k + 1)) = K.add (K.add B (rep K (K.mul δ G) k)) (K.mul δ G) := by
      show K.add B (K.add _ _) = _; rw [K.add_assoc]
    rw [e]
    exact le_add K P _ _ _ _ ih gg

/-- **Discrete local existence for any monotone growth.** -/
theorem riccati_gen (P : Cone K) (z : Nat → R) (g : R → R) (B G δ : R) (N : Nat)
    (hz : ∀ k, P.nn (z k)) (hδ : P.nn δ) (hG : P.nn G)
    (hg : ∀ x, P.nn x → Le K P x (K.add B B) → Le K P (g x) G)
    (step : ∀ k, Le K P (z (k + 1)) (K.add (z k) (K.mul δ (g (z k)))))
    (h0 : Le K P (z 0) B) (hN : Le K P (rep K (K.mul δ G) N) B)
    (k : Nat) (hk : k ≤ N) : Le K P (z k) (K.add B B) := by
  cases le_ex hk with
  | intro j hj =>
  have m := rep_mono K P _ (P.nn_mul _ _ hδ hG) k j
  rw [← hj] at m
  exact le_trans' K P _ _ _ (riccati_gen_inv K P z g B G δ N hz hδ hG hg step h0 hN k hk)
    (le_add K P _ _ _ _ (le_refl' K P B) (le_trans' K P _ _ _ m hN))

theorem cube_mono (P : Cone K) (x y : R) (hx : P.nn x) (h : Le K P x y) :
    Le K P (K.mul x (K.mul x x)) (K.mul y (K.mul y y)) := by
  cases h with
  | intro d h =>
    -- y³ − x³ = d(3x² + 3xd + d²) with x, d ≥ 0
    refine ⟨K.add (K.add (K.mul d (K.add (K.add (K.mul x x) (K.mul x x)) (K.mul x x)))
        (K.mul (K.mul d d) (K.add (K.add x x) x))) (K.mul d (K.mul d d)), ?_, ?_⟩
    · have hd := h.1
      have hxx := P.nn_sq x
      exact P.nn_add _ _ (P.nn_add _ _ (P.nn_mul _ _ hd (P.nn_add _ _ (P.nn_add _ _ hxx hxx) hxx))
        (P.nn_mul _ _ (P.nn_sq d) (P.nn_add _ _ (P.nn_add _ _ hx hx) hx))) (P.nn_mul _ _ hd (P.nn_sq d))
    · rw [← h.2]; lib_ring

/-- **Cubic Riccati (3D enstrophy with the stretching bound).** -/
theorem riccati3 (P : Cone K) (z : Nat → R) (B δ : R) (N : Nat)
    (hz : ∀ k, P.nn (z k)) (hδ : P.nn δ) (hB : P.nn B)
    (step : ∀ k, Le K P (z (k + 1)) (K.add (z k) (K.mul δ (K.mul (z k) (K.mul (z k) (z k))))))
    (h0 : Le K P (z 0) B)
    (hN : Le K P (rep K (K.mul δ (K.mul (K.add B B) (K.mul (K.add B B) (K.add B B)))) N) B)
    (k : Nat) (hk : k ≤ N) : Le K P (z k) (K.add B B) :=
  riccati_gen K P z (fun x => K.mul x (K.mul x x)) B _ δ N hz hδ
    (P.nn_mul _ _ (P.nn_add _ _ hB hB) (P.nn_sq _))
    (fun x hx hxB => cube_mono K P x _ hx hxB) step h0 hN k hk

end RiccatiGen

#print axioms RiccatiGen.riccati_gen_inv
#print axioms RiccatiGen.riccati_gen
#print axioms RiccatiGen.cube_mono
#print axioms RiccatiGen.riccati3

ns/RiccatiH.lean

lean_all/ns/RiccatiH.lean

/-
  ns/RiccatiH.lean — T37 cycle 795 (goal G506): a discrete Riccati horizon over any ordered ring (ℚ included):
    0 ≤ e_t,  e_{t+1} ≤ e_t + K·e_t²,  K ≥ 0,  4·T·K·e_0 ≤ 1   ⇒   e_t ≤ e_0 + 4tK e_0² ≤ 2 e_0   for t ≤ T.
  For the semi-implicit scheme (SemiBound: E' ≤ E + h²C E² after bounding N² by the energy) this gives
  E_t ≤ 2E_0 on horizons t·h ≤ 1/(4hC E_0) — growing like 1/h as h → 0.

  Lean 4 core plus ns/DyNBound (mul_le_mul'), ns/RatModel (castN lemmas), ns/DyadicLarge, lib/RingTac.
    sq_le         0 ≤ x ≤ y ⇒ x² ≤ y²
    ric_step      the invariant e_t ≤ e_0 + 4tK e_0²
    ric_horizon   e_t ≤ 2e_0 for t ≤ T
-/
import ns.DyNBound
import ns.RatModel

namespace RiccatiH
open DetChain NSChain DyadicSmallGlobal LibRing DyadicLarge DyNBound RatModel

section Gen
variable {R : Type} (K : CRing R) (O : OrdR K)
include O

theorem sq_le (x y : R) (hx : O.nn x) (h : Le K O.toCone x y) : Le K O.toCone (K.mul x x) (K.mul y y) := by
  have hy : O.nn y := by cases h with | intro d hd => rw [← hd.2]; exact O.nn_add _ _ hx hd.1
  exact mul_le_mul' K O.toCone x y x y hx hy h h

theorem ric_step (e : Nat → R) (Kc : R) (T : Nat) (hK : O.nn Kc) (he : ∀ t, O.nn (e t))
    (rec : ∀ t, Le K O.toCone (e (t + 1)) (K.add (e t) (K.mul Kc (K.mul (e t) (e t)))))
    (small : Le K O.toCone (K.mul (castN K (4 * T)) (K.mul Kc (e 0))) K.one) :
    ∀ t, t ≤ T → Le K O.toCone (e t) (K.add (e 0) (K.mul (castN K (4 * t)) (K.mul Kc (K.mul (e 0) (e 0))))) := by
  intro t
  induction t with
  | zero => intro _; exact ⟨_, O.nn_mul _ _ (nn_cast K O.toCone 0) (O.nn_mul _ _ hK (O.nn_sq _)), rfl⟩
  | succ t ih =>
    intro ht
    have ih' := ih (Nat.le_of_succ_le ht)
    -- e_t ≤ 2 e_0
    have q : Le K O.toCone (K.mul (castN K (4 * t)) (K.mul Kc (K.mul (e 0) (e 0)))) (e 0) := by
      have c1 : Le K O.toCone (castN K (4 * t)) (castN K (4 * T)) := cast_mono K O.toCone _ _ (Nat.mul_le_mul_left 4 (Nat.le_of_succ_le ht))
      have c2 := le_mul_rg' (castN K (4 * t)) (castN K (4 * T)) (K.mul Kc (e 0)) (O.nn_mul _ _ hK (he 0)) c1
      have c3 := le_trans' K O.toCone _ _ _ c2 small
      have c4 := le_mul_rg' _ _ (e 0) (he 0) c3
      rw [show K.mul (K.mul (castN K (4 * t)) (K.mul Kc (e 0))) (e 0)
          = K.mul (castN K (4 * t)) (K.mul Kc (K.mul (e 0) (e 0))) by lib_ring,
        show K.mul K.one (e 0) = e 0 by lib_ring] at c4
      exact c4
    have two : Le K O.toCone (e t) (K.add (e 0) (e 0)) :=
      le_trans' K O.toCone _ _ _ ih' (le_add' K O.toCone _ _ _ _ (le_refl' K O.toCone _) q)
    have sq := sq_le K O _ _ (he t) two
    have r := rec t
    have s1 := le_add' K O.toCone _ _ _ _ ih' (le_mul_nn K O.toCone Kc _ _ hK sq)
    apply le_trans' K O.toCone _ _ _ r
    apply le_trans' K O.toCone _ _ _ s1
    refine ⟨K.zero, O.nn_zero, ?_⟩
    show K.add (K.add (K.add (e 0) (K.mul (castN K (4 * t)) (K.mul Kc (K.mul (e 0) (e 0)))))
        (K.mul Kc (K.mul (K.add (e 0) (e 0)) (K.add (e 0) (e 0))))) K.zero
      = K.add (e 0) (K.mul (castN K (4 * t + 4)) (K.mul Kc (K.mul (e 0) (e 0))))
    rw [cast_add K (4 * t) 4]
    show _ = K.add (e 0) (K.mul (K.add (castN K (4 * t)) (K.add K.one (K.add K.one (K.add K.one (K.add K.one K.zero)))))
      (K.mul Kc (K.mul (e 0) (e 0))))
    lib_ring
where
  le_mul_rg' (x y c : R) (hc : O.nn c) (h : Le K O.toCone x y) : Le K O.toCone (K.mul x c) (K.mul y c) := by
    cases h with
    | intro d hd => exact ⟨K.mul d c, O.nn_mul _ _ hd.1 hc, by rw [← hd.2]; lib_ring⟩

theorem ric_horizon (e : Nat → R) (Kc : R) (T : Nat) (hK : O.nn Kc) (he : ∀ t, O.nn (e t))
    (rec : ∀ t, Le K O.toCone (e (t + 1)) (K.add (e t) (K.mul Kc (K.mul (e t) (e t)))))
    (small : Le K O.toCone (K.mul (castN K (4 * T)) (K.mul Kc (e 0))) K.one) :
    ∀ t, t ≤ T → Le K O.toCone (e t) (K.add (e 0) (e 0)) := by
  intro t ht
  have st := ric_step K O e Kc T hK he rec small t ht
  have c1 : Le K O.toCone (castN K (4 * t)) (castN K (4 * T)) := cast_mono K O.toCone _ _ (Nat.mul_le_mul_left 4 ht)
  have c2 := ric_step.le_mul_rg' K O (castN K (4 * t)) (castN K (4 * T)) (K.mul Kc (e 0)) (O.nn_mul _ _ hK (he 0)) c1
  have c3 := le_trans' K O.toCone _ _ _ c2 small
  have c4 := ric_step.le_mul_rg' K O _ _ (e 0) (he 0) c3
  rw [show K.mul (K.mul (castN K (4 * t)) (K.mul Kc (e 0))) (e 0)
      = K.mul (castN K (4 * t)) (K.mul Kc (K.mul (e 0) (e 0))) by lib_ring,
    show K.mul K.one (e 0) = e 0 by lib_ring] at c4
  exact le_trans' K O.toCone _ _ _ st (le_add' K O.toCone _ _ _ _ (le_refl' K O.toCone _) c4)

end Gen

end RiccatiH

#print axioms RiccatiH.sq_le
#print axioms RiccatiH.ric_step
#print axioms RiccatiH.ric_horizon

ns/RiemannCauchy.lean

lean_all/ns/RiemannCauchy.lean

/-
  ns/RiemannCauchy.lean — T37 cycle 1888 (goal G860d2c, abstract part): Riemann sums on dyadic grids converge.
  Level j has n_j cells of width 2h'_j (h'_j the step of level j + 1) and integrand f_j; level j + 1 has 2n_j cells of
  width h'_j and integrand f_{j+1} = g_j. If on each coarse cell |g_j(2k) − f_j(k)| ≤ ε_j and |g_j(2k+1) − f_j(k)| ≤ δ_j
  and the budget Σ_{k<n_j} h'_j(ε_j + δ_j) is at most D·hʲ (2h = 1), then j ↦ S_j is Cauchy (Archimedean field):
    riemann_cauchy   (RiemannStep.riemann_step + RealLinear.cauchy_lin)
  The NS instance takes ε_j from the step-size estimate (StepUniformS) and δ_j from one fine step (TimeLip).
  Lean 4 core plus ns/RealLinear.
-/
import ns.RealLinear

namespace RiemannCauchy
open DetChain NSChain DyadicSmallGlobal LibRing LibCS LibGron LibCauchy RealBridge RealArch EnstGronwall DyadicLarge
  RiemannStep RealLinear

section Gen
variable {R : Type} (K : CRing R) (O : OrdR K)
variable (inv : R → R) (mul_inv : ∀ y, y ≠ K.zero → K.mul y (inv y) = K.one)

include mul_inv in
theorem riemann_cauchy (arch : ∀ x, ∃ n, Le K O.toCone x (castN K n)) (S : Nat → R) (n : Nat → Nat)
    (hp ε δ : Nat → R) (f : Nat → Nat → R) (D h : R) (hD : O.nn D) (hh : O.nn h) (hh2 : K.mul h (tw K) = K.one)
    (hpn : ∀ j, O.nn (hp j))
    (hS0 : ∀ j, S j = sumN K (fun k => K.mul (K.add (hp j) (hp j)) (f j k)) (n j))
    (hS1 : ∀ j, S (j + 1) = sumN K (fun i => K.mul (hp j) (f (j + 1) i)) (2 * n j))
    (h1 : ∀ j k, k < n j → Close K (posO K O) (ε j) (f (j + 1) (2 * k)) (f j k))
    (h2 : ∀ j k, k < n j → Close K (posO K O) (δ j) (f (j + 1) (2 * k + 1)) (f j k))
    (hb : ∀ j, Le K O.toCone (sumN K (fun _ => K.mul (hp j) (K.add (ε j) (δ j))) (n j)) (K.mul D (pwr K h j))) :
    Cauchy K (posO K O) S := by
  refine cauchy_lin K O inv mul_inv arch S D h hD hh hh2 (fun j => ?_)
  have c := riemann_step K (posO K O) (hp j) (ε j) (δ j) (hpn j) (f j) (f (j + 1)) (n j) (h1 j) (h2 j)
  rw [← hS0 j, ← hS1 j] at c
  exact close_mono K (posO K O) c (le_of_Le K O (hb j))

end Gen
end RiemannCauchy

#print axioms RiemannCauchy.riemann_cauchy

ns/RiemannCauchySq.lean

lean_all/ns/RiemannCauchySq.lean

/-
  ns/RiemannCauchySq.lean — T37 cycle 1894 (goal G860d2c): Riemann sums converge under a squared budget, matching
  the step-size estimate (squared distances O(2^{−j})):
    sq_of_close        |x − y| ≤ b  ⇒  (x − y)² ≤ b²
    riemann_cauchy_sq  local cell bounds ε_j, δ_j as in RiemannCauchy, with B_j = Σ_{k<n_j} h'_j(ε_j + δ_j) ≥ 0
                       and B_j² ≤ D·hʲ (2h = 1)  ⇒  j ↦ S_j is Cauchy (RiemannStep + RealSqrtRate.cauchy_sq)
  Lean 4 core plus ns/RealSqrtRate, ns/RiemannCauchy.
-/
import ns.RealSqrtRate
import ns.RiemannCauchy

namespace RiemannCauchySq
open DetChain NSChain DyadicSmallGlobal LibRing LibCS LibGron LibCauchy RealBridge RealArch EnstGronwall DyadicLarge
  RiemannStep RealLinear RealSqrtRate

section Gen
variable {R : Type} (K : CRing R) (O : OrdR K)

theorem sq_of_close (b x y : R) (c : Close K (posO K O) b x y) :
    Le K O.toCone (K.mul (sub K x y) (sub K x y)) (K.mul b b) := by
  have h1 : O.nn (sub K b (sub K x y)) := c.1
  have h2 : O.nn (sub K b (sub K y x)) := c.2
  refine ⟨K.mul (sub K b (sub K x y)) (sub K b (sub K y x)), O.nn_mul _ _ h1 h2, ?_⟩
  unfold sub; lib_ring

variable (inv : R → R) (mul_inv : ∀ y, y ≠ K.zero → K.mul y (inv y) = K.one)

include mul_inv in
theorem riemann_cauchy_sq (arch : ∀ x, ∃ n, Le K O.toCone x (castN K n)) (S : Nat → R) (n : Nat → Nat)
    (hp ε δ : Nat → R) (f : Nat → Nat → R) (D h : R) (hD : O.nn D) (hh : O.nn h) (hh2 : K.mul h (tw K) = K.one)
    (hpn : ∀ j, O.nn (hp j))
    (hS0 : ∀ j, S j = sumN K (fun k => K.mul (K.add (hp j) (hp j)) (f j k)) (n j))
    (hS1 : ∀ j, S (j + 1) = sumN K (fun i => K.mul (hp j) (f (j + 1) i)) (2 * n j))
    (h1 : ∀ j k, k < n j → Close K (posO K O) (ε j) (f (j + 1) (2 * k)) (f j k))
    (h2 : ∀ j k, k < n j → Close K (posO K O) (δ j) (f (j + 1) (2 * k + 1)) (f j k))
    (hb : ∀ j, Le K O.toCone (K.mul (sumN K (fun _ => K.mul (hp j) (K.add (ε j) (δ j))) (n j))
      (sumN K (fun _ => K.mul (hp j) (K.add (ε j) (δ j))) (n j))) (K.mul D (pwr K h j))) :
    Cauchy K (posO K O) S := by
  refine cauchy_sq K O inv mul_inv arch S D h hD hh hh2 (fun j => ?_)
  have c := riemann_step K (posO K O) (hp j) (ε j) (δ j) (hpn j) (f j) (f (j + 1)) (n j) (h1 j) (h2 j)
  rw [← hS0 j, ← hS1 j] at c
  exact le_trans' K O.toCone _ _ _ (sq_of_close K O _ _ _ c) (hb j)

end Gen
end RiemannCauchySq

#print axioms RiemannCauchySq.sq_of_close
#print axioms RiemannCauchySq.riemann_cauchy_sq

ns/RiemannGalerkin.lean

lean_all/ns/RiemannGalerkin.lean

/-
  ns/RiemannGalerkin.lean — T37 cycle 1912 (goal G860e, piece b5b; G764b): the Riemann sums of the integrated
  Galerkin equation converge with an explicit level-to-level residual. For a step-halving family a_j (level j has step
  s_j, s_{j+1} + s_{j+1} = s_j, n_{j+1} = 2n_j, n_j·s_{j+1} = T'), each level a Galerkin implicit-viscosity scheme on l,
  with
    the step-size bound  Qe(a_j(k), a_{j+1}(2k)) ≤ X·hʲ          (StepUniformS),
    the H² ball          Z(a_j(m)) ≤ Za, A(a_j(m)) ≤ Ab, Σ_r|N_r(a_j(m))|² ≤ BN,
    step sizes           s_{j+1}² ≤ S₀·hʲ,   weights 1 ≤ w_r, w_q ≤ W on l,
  the sums S_j = Σ_{k<n_j} s_j·(N_r(a_j(k)).x − c·a_j(k+1)_r.x) are Cauchy (2h = 1, Archimedean field):
    riemann_galerkin   RiemannLevel.riemann_level with qa, qf (LevelQ), sa (StepLevel), sf (LevelS).
  With RealIntegral (lim a(T) − lim a(0) ≈ lim S_j for c = νw_r) the limit coefficients satisfy the integrated Galerkin
  equation, the Riemann residual between levels being O(2^{−j/2}) explicitly.
  Lean 4 core plus ns/LevelS.
-/
import ns.LevelS

namespace RiemannGalerkin
open DetChain NSChain DyadicSmallGlobal LibRing DyadicLarge FourierGalerkin GalerkinForce ForceBound ForceSob
  ModeCount CubeCount ForceUniform NormCompare EnstGalerkin ForceTwoScale ForceBilin EnstDiff LatticeSum ForceDiff
  RealCoef DyadicCoef StepCauchy QeComp ForceLipZ EnstStep LibCS LibCauchy RealBridge RealArch EnstGronwall RiemannLevel

section Gen
variable {R : Type} (K : CRing R) (O : OrdR K) {I : Type}
variable (inv : R → R) (mul_inv : ∀ y, y ≠ K.zero → K.mul y (inv y) = K.one) (h10 : K.one ≠ K.zero)

include mul_inv h10 in
theorem riemann_galerkin (arch : ∀ x, ∃ n, Le K O.toCone x (castN K n))
    (l : List I) (kz : I → Mode) (hd : List.Pairwise (fun a b => kz a ≠ kz b) l)
    (h1 : ∀ p, 1 ≤ nrm (kz p)) (M : Nat) (hM : ∀ p, p ∈ l → nrm (kz p) ≤ M + 1) (Rc t0 : Nat) (hRM : Rc ≤ M)
    (ι ν iν W Za Ab BN X S0 h T' c Lc : R) (hν : O.nn ν) (hi : O.nn iν) (hνi : K.mul ν iν = K.one) (hWn : O.nn W)
    (hX : O.nn X) (hS0 : O.nn S0) (hBN : O.nn BN) (hAb : O.nn Ab) (hLc : O.nn Lc) (hh : O.nn h)
    (hh2 : K.mul h (tw K) = K.one)
    (hW : ∀ q, q ∈ l → Le K O.toCone (wt K kz q) W)
    (hL : Lc = K.add (K.add
          (K.mul (K.add K.one K.one) (K.mul (K.add K.one K.one) (K.mul (K.mul ι ι)
            (K.mul (K.add (K.mul (castN K (cube Rc)) K.one) (K.mul (castN K 135) (iv K inv (Rc + 1)))) Za))))
          (K.mul (K.add K.one K.one) (K.mul (K.add K.one K.one) (K.mul (K.mul ι ι) (K.mul W
            (K.mul (K.add (K.mul (castN K (cube Rc)) (K.mul (inv (castN K (t0 + 1))) (inv (castN K (t0 + 1)))))
                (K.mul (castN K 135) (iv K inv (Rc + 1)))) Za))))))
          (K.mul (K.add K.one K.one) (K.mul (K.mul ι ι) (K.mul Ab (castN K 135)))))
    (a : Nat → Nat → I → V3 R) (s : Nat → R) (n : Nat → Nat) (hsn : ∀ j, O.nn (s j))
    (hstep : ∀ j m r, Step K (s j) ν (wt K kz r) (a j (m + 1) r) (a j m r) (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (a j m) (a j m) r))
    (hn2 : ∀ j, n (j + 1) = 2 * n j) (hs2 : ∀ j, K.add (s (j + 2)) (s (j + 2)) = s (j + 1))
    (hnT : ∀ j, K.mul (castN K (n j)) (s (j + 1)) = T')
    (hss : ∀ j, Le K O.toCone (K.mul (s (j + 1)) (s (j + 1))) (K.mul S0 (pwr K h j)))
    (hQ : ∀ j k, Le K O.toCone (Qe K ν (s (j + 1)) l kz (a j k) (a (j + 1) (2 * k))) (K.mul X (pwr K h j)))
    (hZ : ∀ j m, Le K O.toCone (lsum K (fun q => K.mul (wt K kz q) (dot3 K (a j m q) (a j m q))) l) Za)
    (hA : ∀ j m, Le K O.toCone (lsum K (fun q => K.mul (K.mul (wt K kz q) (wt K kz q)) (dot3 K (a j m q) (a j m q))) l) Ab)
    (hN : ∀ j m, Le K O.toCone (lsum K (fun r => dot3 K (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (a j m) (a j m) r)
      (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (a j m) (a j m) r)) l) BN)
    (r : I) (hr : r ∈ l) (hw1 : Le K O.toCone K.one (wt K kz r)) :
    Cauchy K (posO K O) (fun j => sumN K (fun k => K.mul (K.add (s (j + 1)) (s (j + 1)))
      (sub K (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (a j k) (a j k) r).x (K.mul c (a j (k + 1) r).x))) (n j)) := by
  have two : O.nn (K.add K.one K.one) := O.nn_add _ _ (RatModel.nn_one' K O.toCone) (RatModel.nn_one' K O.toCone)
  have hx : ∀ u v : V3 R, (sub3 K u v).x = sub K u.x v.x := fun _ _ => rfl
  have hZa : O.nn Za := ForceLipZ.nn_of_le K O _ _ (lsum_nn K O.toCone _ (fun _ => O.nn_mul _ _ (dot3_nn K O.toCone _) (dot3_nn K O.toCone _)) l) (hZ 0 0)
  -- component bounds: (N_r.x)² ≤ BN, (a_r.x)² ≤ Za
  have nB : ∀ j m, Le K O.toCone (K.mul (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (a j m) (a j m) r).x
      (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (a j m) (a j m) r).x) BN := fun j m =>
    le_trans' K O.toCone _ _ _ (comp_x K O _) (le_trans' K O.toCone _ _ _
      (term_le K O (fun r => dot3 K (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (a j m) (a j m) r)
        (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (a j m) (a j m) r)) (fun _ => dot3_nn K O.toCone _) l r hr) (hN j m))
  have aB : ∀ j m, Le K O.toCone (K.mul (a j m r).x (a j m r).x) Za := fun j m =>
    le_trans' K O.toCone _ _ _ (comp_x K O _) (le_trans' K O.toCone _ _ _ (le_mu K O _ _ hw1 (dot3_nn K O.toCone _))
      (le_trans' K O.toCone _ _ _ (term_le K O (fun q => K.mul (wt K kz q) (dot3 K (a j m q) (a j m q)))
        (fun _ => O.nn_mul _ _ (dot3_nn K O.toCone _) (dot3_nn K O.toCone _)) l r hr) (hZ j m)))
  refine riemann_level K O inv mul_inv arch
    (fun j k => (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (a j k) (a j k) r).x) (fun j k => (a j k r).x) c
    (K.mul iν X) (K.mul Lc (K.mul iν X))
    (K.mul S0 (K.add (K.mul (K.add K.one K.one) BN) (K.mul (K.add K.one K.one) (K.mul (K.mul (K.mul ν ν) (K.mul W W)) Za))))
    (K.mul Lc (K.mul W (K.mul S0 (K.add (K.mul (K.add K.one K.one) BN) (K.mul (K.add K.one K.one) (K.mul (K.mul ν ν) Ab))))))
    h T' n (fun j => s (j + 1)) (O.nn_mul _ _ hi hX) (O.nn_mul _ _ hLc (O.nn_mul _ _ hi hX))
    (O.nn_mul _ _ hS0 (O.nn_add _ _ (O.nn_mul _ _ two hBN) (O.nn_mul _ _ two (O.nn_mul _ _ (O.nn_mul _ _ (O.nn_sq ν) (O.nn_mul _ _ hWn hWn)) hZa))))
    (O.nn_mul _ _ hLc (O.nn_mul _ _ hWn (O.nn_mul _ _ hS0 (O.nn_add _ _ (O.nn_mul _ _ two hBN) (O.nn_mul _ _ two (O.nn_mul _ _ (O.nn_sq ν) hAb))))))
    hh hh2 (fun j => hsn (j + 1)) hnT hn2 hs2 ?_ ?_ ?_ ?_
  · intro j k
    exact LevelQ.qa_comp K O ν (s (j + 1)) iν X (pwr K h j) hν (hsn (j + 1)) hi hνi l kz (a j k) (a (j + 1) (2 * k)) r hr hw1
      (fun v => v.x) (comp_x K O) hx (hQ j k)
  · intro j k
    exact LevelQ.qf_comp K O inv mul_inv h10 l kz hd h1 M hM Rc t0 hRM ι W Za Ab ν (s (j + 1)) iν X (pwr K h j) Lc hW
      (a j k) (a (j + 1) (2 * k)) (hsn (j + 1)) hi hνi hLc (hZ (j + 1) (2 * k)) (hA j k) hL (hQ j k) r hr
      (fun v => v.x) (comp_x K O) hx
  · intro j m
    exact StepLevel.sa_bound K O (s (j + 1)) ν (wt K kz r) _ _ _ BN Za W S0 (pwr K h j) (hstep (j + 1) m r).1
      (hsn (j + 1)) hν (dot3_nn K O.toCone _) hBN hZa (hW r hr) (nB (j + 1) m) (aB (j + 1) m) (hss j)
  · intro j m
    exact LevelS.sf_comp K O inv mul_inv h10 l kz hd h1 M hM Rc t0 hRM ι W Za Ab ν (s (j + 1)) S0 (pwr K h j) BN Ab Lc
      hW hWn hLc (hsn (j + 1)) hν (a (j + 1) (m + 1)) (a (j + 1) m) (hstep (j + 1) m) (hZ (j + 1) (m + 1)) (hA (j + 1) m) hL
      (hN (j + 1) m) (hA (j + 1) m) (hss j) r hr (fun v => v.x) (comp_x K O) hx

end Gen
end RiemannGalerkin

#print axioms RiemannGalerkin.riemann_galerkin

ns/RiemannInst.lean

lean_all/ns/RiemannInst.lean

/-
  ns/RiemannInst.lean — T37 cycle 1896 (goal G860e, piece a): Riemann sums of an integrand f_j(k) = F_j(k) − c·A_j(k+1)
  (force minus implicit dissipation) converge when F and A have squared level-to-level differences O(hʲ) (2h = 1):
    sq_diff2      (x − c y)² ≤ 2x² + 2c²y²
    g             g 0 = g 1 = 1, g (j+2) = h·g j   (g j = h^⌊j/2⌋, no division)
    g_lower       hʲ ≤ (g j)²;   g_upper  (g j)² ≤ 2hʲ
    riemann_inst  hypotheses: n_j cells of width 2h'_j with n_j·h'_j = T', and for k < n_j
                    (F_{j+1}(2k) − F_j(k))², (F_{j+1}(2k+1) − F_j(k))² ≤ P·hʲ,
                    (A_{j+1}(2k+1) − A_j(k+1))², (A_{j+1}(2k+2) − A_j(k+1))² ≤ Q·hʲ
                  ⇒  S_j = Σ_{k<n_j} 2h'_j f_j(k) is Cauchy   (ε_j = δ_j = (M + 1)·g j, M = 2P + 2c²Q;
                  RiemannCauchySq with D = 8T'²(M + 1)²).
  Lean 4 core plus ns/RiemannCauchySq.
-/
import ns.RiemannCauchySq

namespace RiemannInst
open DetChain NSChain DyadicSmallGlobal LibRing LibCS LibGron LibCauchy RealBridge RealArch EnstGronwall DyadicLarge
  RiemannStep RealLinear RealSqrtRate RiemannCauchySq

section Gen
variable {R : Type} (K : CRing R) (O : OrdR K)

theorem sq_diff2 (x y c : R) :
    Le K O.toCone (K.mul (sub K x (K.mul c y)) (sub K x (K.mul c y)))
      (K.add (K.mul (tw K) (K.mul x x)) (K.mul (tw K) (K.mul (K.mul c c) (K.mul y y)))) :=
  ⟨K.mul (K.add x (K.mul c y)) (K.add x (K.mul c y)), O.nn_sq _, by unfold sub tw; lib_ring⟩

def g (h : R) : Nat → R
  | 0 => K.one
  | 1 => K.one
  | j + 2 => K.mul h (g h j)

theorem g_nn (h : R) (hh : O.nn h) : ∀ j, O.nn (g K h j)
  | 0 => RatModel.nn_one' K O.toCone
  | 1 => RatModel.nn_one' K O.toCone
  | j + 2 => O.nn_mul _ _ hh (g_nn h hh j)

theorem g_lower (h : R) (hh : O.nn h) (hh2 : K.mul h (tw K) = K.one) : ∀ j, Le K O.toCone (pwr K h j) (K.mul (g K h j) (g K h j))
  | 0 => ⟨K.zero, O.nn_zero, by show K.add K.one K.zero = K.mul K.one K.one; lib_ring⟩
  | 1 => by
    have a := h_le_one K O h hh hh2
    show Le K O.toCone (K.mul h K.one) (K.mul K.one K.one)
    rw [show K.mul h K.one = h by lib_ring, show K.mul K.one K.one = K.one by lib_ring]; exact a
  | j + 2 => by
    have a := le_mul_nn K O.toCone (K.mul h h) _ _ (O.nn_sq h) (g_lower h hh hh2 j)
    show Le K O.toCone (K.mul h (K.mul h (pwr K h j))) (K.mul (K.mul h (g K h j)) (K.mul h (g K h j)))
    rw [show K.mul h (K.mul h (pwr K h j)) = K.mul (K.mul h h) (pwr K h j) by lib_ring,
      show K.mul (K.mul h (g K h j)) (K.mul h (g K h j)) = K.mul (K.mul h h) (K.mul (g K h j) (g K h j)) by lib_ring]
    exact a

theorem g_upper (h : R) (hh : O.nn h) (hh2 : K.mul h (tw K) = K.one) :
    ∀ j, Le K O.toCone (K.mul (g K h j) (g K h j)) (K.mul (tw K) (pwr K h j))
  | 0 => ⟨K.one, RatModel.nn_one' K O.toCone, by show K.add (K.mul K.one K.one) K.one = K.mul (tw K) K.one; unfold tw; lib_ring⟩
  | 1 => ⟨K.zero, O.nn_zero, by
      show K.add (K.mul K.one K.one) K.zero = K.mul (tw K) (K.mul h K.one)
      rw [show K.mul (tw K) (K.mul h K.one) = K.mul h (tw K) by lib_ring, hh2]; lib_ring⟩
  | j + 2 => by
    have a := le_mul_nn K O.toCone (K.mul h h) _ _ (O.nn_sq h) (g_upper h hh hh2 j)
    show Le K O.toCone (K.mul (K.mul h (g K h j)) (K.mul h (g K h j))) (K.mul (tw K) (K.mul h (K.mul h (pwr K h j))))
    rw [show K.mul (K.mul h (g K h j)) (K.mul h (g K h j)) = K.mul (K.mul h h) (K.mul (g K h j) (g K h j)) by lib_ring,
      show K.mul (tw K) (K.mul h (K.mul h (pwr K h j))) = K.mul (K.mul h h) (K.mul (tw K) (pwr K h j)) by lib_ring]
    exact a

theorem sumN_const (x : R) : ∀ n, sumN K (fun _ => x) n = K.mul (castN K n) x
  | 0 => by show K.zero = K.mul K.zero x; lib_ring
  | n + 1 => by
    show K.add (sumN K (fun _ => x) n) x = K.mul (K.add K.one (castN K n)) x
    rw [sumN_const x n]; lib_ring

/-- a local cell bound from squared bounds on F and A -/
theorem cell_close (x x' y y' c P Q e : R) (hP : Le K O.toCone (K.mul (sub K x' x) (sub K x' x)) P)
    (hQ : Le K O.toCone (K.mul (sub K y' y) (sub K y' y)) Q) (he : O.nn e)
    (hM : Le K O.toCone (K.add (K.mul (tw K) P) (K.mul (tw K) (K.mul (K.mul c c) Q))) (K.mul e e)) :
    Close K (posO K O) e (sub K x' (K.mul c y')) (sub K x (K.mul c y)) := by
  have ntw : O.nn (tw K) := O.nn_add _ _ (RatModel.nn_one' K O.toCone) (RatModel.nn_one' K O.toCone)
  have s := sq_diff2 K O (sub K x' x) (sub K y' y) c
  rw [show sub K (sub K x' x) (K.mul c (sub K y' y)) = sub K (sub K x' (K.mul c y')) (sub K x (K.mul c y)) by unfold sub; lib_ring] at s
  refine close_of_sq K O _ _ _ he (le_trans' K O.toCone _ _ _ s (le_trans' K O.toCone _ _ _ ?_ hM))
  exact le_add' K O.toCone _ _ _ _ (le_mul_nn K O.toCone _ _ _ ntw hP)
    (le_mul_nn K O.toCone _ _ _ ntw (le_mul_nn K O.toCone _ _ _ (O.nn_sq c) hQ))

variable (inv : R → R) (mul_inv : ∀ y, y ≠ K.zero → K.mul y (inv y) = K.one)

include mul_inv in
theorem riemann_inst (arch : ∀ x, ∃ n, Le K O.toCone x (castN K n)) (F A : Nat → Nat → R) (c P Q h T' : R)
    (n : Nat → Nat) (hp : Nat → R) (hP : O.nn P) (hQ : O.nn Q) (hh : O.nn h) (hh2 : K.mul h (tw K) = K.one)
    (hpn : ∀ j, O.nn (hp j)) (hnT : ∀ j, K.mul (castN K (n j)) (hp j) = T')
    (hn2 : ∀ j, n (j + 1) = 2 * n j) (hp2 : ∀ j, K.add (hp (j + 1)) (hp (j + 1)) = hp j)
    (hF0 : ∀ j k, k < n j → Le K O.toCone (K.mul (sub K (F (j + 1) (2 * k)) (F j k)) (sub K (F (j + 1) (2 * k)) (F j k))) (K.mul P (pwr K h j)))
    (hF1 : ∀ j k, k < n j → Le K O.toCone (K.mul (sub K (F (j + 1) (2 * k + 1)) (F j k)) (sub K (F (j + 1) (2 * k + 1)) (F j k))) (K.mul P (pwr K h j)))
    (hA0 : ∀ j k, k < n j → Le K O.toCone (K.mul (sub K (A (j + 1) (2 * k + 1)) (A j (k + 1))) (sub K (A (j + 1) (2 * k + 1)) (A j (k + 1)))) (K.mul Q (pwr K h j)))
    (hA1 : ∀ j k, k < n j → Le K O.toCone (K.mul (sub K (A (j + 1) (2 * k + 1 + 1)) (A j (k + 1))) (sub K (A (j + 1) (2 * k + 1 + 1)) (A j (k + 1)))) (K.mul Q (pwr K h j))) :
    Cauchy K (posO K O) (fun j => sumN K (fun k => K.mul (K.add (hp j) (hp j)) (sub K (F j k) (K.mul c (A j (k + 1))))) (n j)) := by
  have ntw : O.nn (tw K) := O.nn_add _ _ (RatModel.nn_one' K O.toCone) (RatModel.nn_one' K O.toCone)
  -- M = 2P + 2c²Q, e_j = (M + 1)·g j
  have hM : O.nn (K.add (K.mul (tw K) P) (K.mul (tw K) (K.mul (K.mul c c) Q))) :=
    O.nn_add _ _ (O.nn_mul _ _ ntw hP) (O.nn_mul _ _ ntw (O.nn_mul _ _ (O.nn_sq c) hQ))
  have hM1 : O.nn (K.add (K.add (K.mul (tw K) P) (K.mul (tw K) (K.mul (K.mul c c) Q))) K.one) :=
    O.nn_add _ _ hM (RatModel.nn_one' K O.toCone)
  have he : ∀ j, O.nn (K.mul (K.add (K.add (K.mul (tw K) P) (K.mul (tw K) (K.mul (K.mul c c) Q))) K.one) (g K h j)) :=
    fun j => O.nn_mul _ _ hM1 (g_nn K O h hh j)
  -- the local bound: (2P + 2c²Q)·hʲ ≤ ((M + 1)·g j)²
  have hloc : ∀ j, Le K O.toCone
      (K.add (K.mul (tw K) (K.mul P (pwr K h j))) (K.mul (tw K) (K.mul (K.mul c c) (K.mul Q (pwr K h j)))))
      (K.mul (K.mul (K.add (K.add (K.mul (tw K) P) (K.mul (tw K) (K.mul (K.mul c c) Q))) K.one) (g K h j))
        (K.mul (K.add (K.add (K.mul (tw K) P) (K.mul (tw K) (K.mul (K.mul c c) Q))) K.one) (g K h j))) := by
    intro j
    have a := le_mul_nn K O.toCone (K.add (K.mul (tw K) P) (K.mul (tw K) (K.mul (K.mul c c) Q))) _ _ hM (g_lower K O h hh hh2 j)
    have b := sq_bound K O (K.add (K.mul (tw K) P) (K.mul (tw K) (K.mul (K.mul c c) Q))) (g K h j) hM
    rw [show K.add (K.mul (tw K) (K.mul P (pwr K h j))) (K.mul (tw K) (K.mul (K.mul c c) (K.mul Q (pwr K h j))))
        = K.mul (K.add (K.mul (tw K) P) (K.mul (tw K) (K.mul (K.mul c c) Q))) (pwr K h j) by lib_ring]
    exact le_trans' K O.toCone _ _ _ a b
  refine riemann_cauchy_sq K O inv mul_inv arch _ n hp
    (fun j => K.mul (K.add (K.add (K.mul (tw K) P) (K.mul (tw K) (K.mul (K.mul c c) Q))) K.one) (g K h j))
    (fun j => K.mul (K.add (K.add (K.mul (tw K) P) (K.mul (tw K) (K.mul (K.mul c c) Q))) K.one) (g K h j))
    (fun j k => sub K (F j k) (K.mul c (A j (k + 1))))
    (K.mul (K.mul (K.mul (tw K) (tw K)) (K.mul (tw K) (K.mul T' T')))
      (K.mul (K.add (K.add (K.mul (tw K) P) (K.mul (tw K) (K.mul (K.mul c c) Q))) K.one)
        (K.add (K.add (K.mul (tw K) P) (K.mul (tw K) (K.mul (K.mul c c) Q))) K.one)))
    h (O.nn_mul _ _ (O.nn_mul _ _ (O.nn_mul _ _ ntw ntw) (O.nn_mul _ _ ntw (O.nn_sq T'))) (O.nn_sq _)) hh hh2 hpn
    (fun j => rfl) (fun j => by show _ = _; rw [hn2 j, hp2 j])
    (fun j k hk => cell_close K O _ _ _ _ c _ _ _ (hF0 j k hk) (hA0 j k hk) (he j) (hloc j))
    (fun j k hk => cell_close K O _ _ _ _ c _ _ _ (hF1 j k hk) (hA1 j k hk) (he j) (hloc j)) (fun j => ?_)
  rw [sumN_const K _ (n j)]
  rw [show K.mul (castN K (n j)) (K.mul (hp j) (K.add
        (K.mul (K.add (K.add (K.mul (tw K) P) (K.mul (tw K) (K.mul (K.mul c c) Q))) K.one) (g K h j))
        (K.mul (K.add (K.add (K.mul (tw K) P) (K.mul (tw K) (K.mul (K.mul c c) Q))) K.one) (g K h j))))
      = K.mul (K.mul (castN K (n j)) (hp j)) (K.mul (tw K)
        
        (K.mul (K.add (K.add (K.mul (tw K) P) (K.mul (tw K) (K.mul (K.mul c c) Q))) K.one) (g K h j))) by unfold tw; lib_ring, hnT j]
  have u := le_mul_nn K O.toCone (K.mul (K.mul (K.mul (tw K) (tw K)) (K.mul T' T'))
      (K.mul (K.add (K.add (K.mul (tw K) P) (K.mul (tw K) (K.mul (K.mul c c) Q))) K.one)
        (K.add (K.add (K.mul (tw K) P) (K.mul (tw K) (K.mul (K.mul c c) Q))) K.one))) _ _
    (O.nn_mul _ _ (O.nn_mul _ _ (O.nn_mul _ _ ntw ntw) (O.nn_sq T')) (O.nn_sq _)) (g_upper K O h hh hh2 j)
  refine le_trans' K O.toCone _ _ _ ⟨K.zero, O.nn_zero, ?_⟩ (le_trans' K O.toCone _ _ _ u ⟨K.zero, O.nn_zero, ?_⟩)
  · generalize g K h j = G
    generalize K.add (K.add (K.mul (tw K) P) (K.mul (tw K) (K.mul (K.mul c c) Q))) K.one = M1
    lib_ring
  · generalize g K h j = G
    generalize K.add (K.add (K.mul (tw K) P) (K.mul (tw K) (K.mul (K.mul c c) Q))) K.one = M1
    lib_ring

end Gen
end RiemannInst

#print axioms RiemannInst.sq_diff2
#print axioms RiemannInst.g_lower
#print axioms RiemannInst.g_upper
#print axioms RiemannInst.cell_close
#print axioms RiemannInst.riemann_inst

ns/RiemannLevel.lean

lean_all/ns/RiemannLevel.lean

/-
  ns/RiemannLevel.lean — T37 cycle 1904 (goal G860e, piece b3): RiemannInst's four cell hypotheses from four simpler
  per-level bounds (2h = 1, every k, m):
    qa  (A_{j+1}(2k) − A_j(k))² ≤ α·hʲ        coefficient, same time      (StepUniformS via QeComp)
    qf  (F_{j+1}(2k) − F_j(k))² ≤ φ·hʲ        force, same time            (ForceLipZ + QeComp + StepUniformS)
    sa  (A_{j+1}(m+1) − A_{j+1}(m))² ≤ β·hʲ   one fine step               (TimeLip.comp_lip)
    sf  (F_{j+1}(m+1) − F_{j+1}(m))² ≤ ψ·hʲ   force over one fine step    (ForceLipZ + TimeLip)
    tri_sq        (x − z)² ≤ 2(x − y)² + 2(y − z)²
    riemann_level ⇒ the Riemann sums of F − c·A(+1) are Cauchy   (RiemannInst with P = 2φ + 2ψ, Q = 2α + 2β)
  Lean 4 core plus ns/RiemannInst.
-/
import ns.RiemannInst

namespace RiemannLevel
open DetChain NSChain DyadicSmallGlobal LibRing LibCS LibGron LibCauchy RealBridge RealArch EnstGronwall DyadicLarge
  RiemannStep RealLinear RealSqrtRate RiemannCauchySq RiemannInst

section Gen
variable {R : Type} (K : CRing R) (O : OrdR K)

theorem tri_sq (x y z : R) :
    Le K O.toCone (K.mul (sub K x z) (sub K x z))
      (K.add (K.mul (tw K) (K.mul (sub K x y) (sub K x y))) (K.mul (tw K) (K.mul (sub K y z) (sub K y z)))) :=
  ⟨K.mul (sub K (K.add x z) (K.add y y)) (sub K (K.add x z) (K.add y y)), O.nn_sq _, by unfold sub tw; lib_ring⟩

theorem two_bound (x y z u v : R) (hu : Le K O.toCone (K.mul (sub K x y) (sub K x y)) u)
    (hv : Le K O.toCone (K.mul (sub K y z) (sub K y z)) v) :
    Le K O.toCone (K.mul (sub K x z) (sub K x z)) (K.add (K.mul (tw K) u) (K.mul (tw K) v)) := by
  have ntw : O.nn (tw K) := O.nn_add _ _ (RatModel.nn_one' K O.toCone) (RatModel.nn_one' K O.toCone)
  exact le_trans' K O.toCone _ _ _ (tri_sq K O x y z)
    (le_add' K O.toCone _ _ _ _ (le_mul_nn K O.toCone _ _ _ ntw hu) (le_mul_nn K O.toCone _ _ _ ntw hv))

theorem sq_symm (x y : R) : K.mul (sub K x y) (sub K x y) = K.mul (sub K y x) (sub K y x) := by unfold sub; lib_ring

theorem widen (s a b h : R) (hb : O.nn b) (hh : O.nn h) (ha : Le K O.toCone s (K.mul a h)) :
    Le K O.toCone s (K.mul (K.add a b) h) :=
  le_trans' K O.toCone _ _ _ ha ⟨K.mul b h, O.nn_mul _ _ hb hh, by lib_ring⟩

variable (inv : R → R) (mul_inv : ∀ y, y ≠ K.zero → K.mul y (inv y) = K.one)

include mul_inv in
theorem riemann_level (arch : ∀ x, ∃ n, Le K O.toCone x (castN K n)) (F A : Nat → Nat → R) (c α φ β ψ h T' : R)
    (n : Nat → Nat) (hp : Nat → R) (hα : O.nn α) (hφ : O.nn φ) (hβ : O.nn β) (hψ : O.nn ψ) (hh : O.nn h)
    (hh2 : K.mul h (tw K) = K.one) (hpn : ∀ j, O.nn (hp j)) (hnT : ∀ j, K.mul (castN K (n j)) (hp j) = T')
    (hn2 : ∀ j, n (j + 1) = 2 * n j) (hp2 : ∀ j, K.add (hp (j + 1)) (hp (j + 1)) = hp j)
    (qa : ∀ j k, Le K O.toCone (K.mul (sub K (A (j + 1) (2 * k)) (A j k)) (sub K (A (j + 1) (2 * k)) (A j k))) (K.mul α (pwr K h j)))
    (qf : ∀ j k, Le K O.toCone (K.mul (sub K (F (j + 1) (2 * k)) (F j k)) (sub K (F (j + 1) (2 * k)) (F j k))) (K.mul φ (pwr K h j)))
    (sa : ∀ j m, Le K O.toCone (K.mul (sub K (A (j + 1) (m + 1)) (A (j + 1) m)) (sub K (A (j + 1) (m + 1)) (A (j + 1) m))) (K.mul β (pwr K h j)))
    (sf : ∀ j m, Le K O.toCone (K.mul (sub K (F (j + 1) (m + 1)) (F (j + 1) m)) (sub K (F (j + 1) (m + 1)) (F (j + 1) m))) (K.mul ψ (pwr K h j))) :
    Cauchy K (posO K O) (fun j => sumN K (fun k => K.mul (K.add (hp j) (hp j)) (sub K (F j k) (K.mul c (A j (k + 1))))) (n j)) := by
  have ntw : O.nn (tw K) := O.nn_add _ _ (RatModel.nn_one' K O.toCone) (RatModel.nn_one' K O.toCone)
  have nP : O.nn (K.add (K.mul (tw K) φ) (K.mul (tw K) ψ)) := O.nn_add _ _ (O.nn_mul _ _ ntw hφ) (O.nn_mul _ _ ntw hψ)
  have nQ : O.nn (K.add (K.mul (tw K) α) (K.mul (tw K) β)) := O.nn_add _ _ (O.nn_mul _ _ ntw hα) (O.nn_mul _ _ ntw hβ)
  have ph : ∀ j, O.nn (pwr K h j) := pwr_nn K O h hh
  -- the two-term bound, rearranged to (2u + 2v)·hʲ
  have tb : ∀ (j : Nat) (x y z u v : R), Le K O.toCone (K.mul (sub K x y) (sub K x y)) (K.mul u (pwr K h j)) →
      Le K O.toCone (K.mul (sub K y z) (sub K y z)) (K.mul v (pwr K h j)) →
      Le K O.toCone (K.mul (sub K x z) (sub K x z)) (K.mul (K.add (K.mul (tw K) u) (K.mul (tw K) v)) (pwr K h j)) := by
    intro j x y z u v h1 h2
    refine le_trans' K O.toCone _ _ _ (two_bound K O x y z _ _ h1 h2) ⟨K.zero, O.nn_zero, ?_⟩
    lib_ring
  refine riemann_inst K O inv mul_inv arch F A c (K.add (K.mul (tw K) φ) (K.mul (tw K) ψ))
    (K.add (K.mul (tw K) α) (K.mul (tw K) β)) h T' n hp nP nQ hh hh2 hpn hnT hn2 hp2 ?_ ?_ ?_ ?_
  · -- hF0
    intro j k _
    refine le_trans' K O.toCone _ _ _ (widen K O _ φ (K.add φ (K.mul (tw K) ψ)) _ (O.nn_add _ _ hφ (O.nn_mul _ _ ntw hψ)) (ph j) (qf j k))
      ⟨K.zero, O.nn_zero, ?_⟩
    unfold tw; lib_ring
  · -- hF1: via F_{j+1}(2k)
    intro j k _
    exact tb j _ _ _ ψ φ (sf j (2 * k)) (qf j k) |> fun t => le_trans' K O.toCone _ _ _ t ⟨K.zero, O.nn_zero, by lib_ring⟩
  · -- hA0: via A_{j+1}(2k+2)
    intro j k _
    have q := qa j (k + 1)
    have s := sa j (2 * k + 1)
    rw [sq_symm K (A (j + 1) (2 * k + 1 + 1)) (A (j + 1) (2 * k + 1))] at s
    exact le_trans' K O.toCone _ _ _ (tb j _ (A (j + 1) (2 * k + 1 + 1)) _ β α s q) ⟨K.zero, O.nn_zero, by lib_ring⟩
  · -- hA1
    intro j k _
    have q : Le K O.toCone (K.mul (sub K (A (j + 1) (2 * k + 1 + 1)) (A j (k + 1))) (sub K (A (j + 1) (2 * k + 1 + 1)) (A j (k + 1))))
        (K.mul α (pwr K h j)) := qa j (k + 1)
    refine le_trans' K O.toCone _ _ _ (widen K O _ α (K.add α (K.mul (tw K) β)) _ (O.nn_add _ _ hα (O.nn_mul _ _ ntw hβ)) (ph j) q)
      ⟨K.zero, O.nn_zero, ?_⟩
    unfold tw; lib_ring

end Gen
end RiemannLevel

#print axioms RiemannLevel.tri_sq
#print axioms RiemannLevel.two_bound
#print axioms RiemannLevel.riemann_level

ns/RiemannRefine.lean

lean_all/ns/RiemannRefine.lean

/-
  ns/RiemannRefine.lean — T37 cycle 1876 (goal G860d2, piece a): Riemann sums under dyadic refinement, as exact
  identities over any commutative ring.
    sumN_pair     Σ_{i<2n} g_i = Σ_{k<n} (g_{2k} + g_{2k+1})
    refine_diff   with the fine step h' and the coarse step h' + h':
                  Σ_{i<2n} h'·g_i − Σ_{k<n} (h' + h')·f_k = Σ_{k<n} h'·((g_{2k} − f_k) + (g_{2k+1} − f_k))
  so the change of the Riemann sum from level j to level j + 1 is a sum of local differences of the integrands on
  each coarse cell — bounded by the coefficient convergence (RealCoef) and the time-Lipschitz bound (TimeLipN).
  Lean 4 core plus ns/AbelCount (sumN_congr).
-/
import ns.AbelCount

namespace RiemannRefine
open DetChain NSChain LibRing

section Gen
variable {R : Type} (K : CRing R)

theorem sumN_pair (g : Nat → R) : ∀ n, sumN K g (2 * n) = sumN K (fun k => K.add (g (2 * k)) (g (2 * k + 1))) n
  | 0 => rfl
  | n + 1 => by
    show K.add (K.add (sumN K g (2 * n)) (g (2 * n))) (g (2 * n + 1))
      = K.add (sumN K (fun k => K.add (g (2 * k)) (g (2 * k + 1))) n) (K.add (g (2 * n)) (g (2 * n + 1)))
    rw [sumN_pair g n]; lib_ring

theorem sumN_lin (f g : Nat → R) : ∀ n, K.add (sumN K f n) (K.neg (sumN K g n)) = sumN K (fun k => K.add (f k) (K.neg (g k))) n
  | 0 => by show K.add K.zero (K.neg K.zero) = K.zero; lib_ring
  | n + 1 => by
    show K.add (K.add (sumN K f n) (f n)) (K.neg (K.add (sumN K g n) (g n)))
      = K.add (sumN K (fun k => K.add (f k) (K.neg (g k))) n) (K.add (f n) (K.neg (g n)))
    rw [← sumN_lin f g n]; lib_ring

theorem refine_diff (h' : R) (f g : Nat → R) (n : Nat) :
    K.add (sumN K (fun i => K.mul h' (g i)) (2 * n)) (K.neg (sumN K (fun k => K.mul (K.add h' h') (f k)) n))
      = sumN K (fun k => K.mul h' (K.add (K.add (g (2 * k)) (K.neg (f k))) (K.add (g (2 * k + 1)) (K.neg (f k))))) n := by
  rw [sumN_pair K (fun i => K.mul h' (g i)) n, sumN_lin]
  exact AbelCount.sumN_congr K _ _ (fun k => by lib_ring) n

end Gen
end RiemannRefine

#print axioms RiemannRefine.sumN_pair
#print axioms RiemannRefine.sumN_lin
#print axioms RiemannRefine.refine_diff

ns/RiemannStep.lean

lean_all/ns/RiemannStep.lean

/-
  ns/RiemannStep.lean — T37 cycle 1878 (goal G860d2, piece b): the change of a Riemann sum under dyadic refinement is
  bounded by the local differences, over any commutative ring with a positive cone (lib/CauchySeq's Close):
    close_cmul    Close e x y, c ≥ 0  ⇒  Close (c·e) (c·x) (c·y)
    close_sumN    Close (e_k) (x_k) (y_k) for k < n  ⇒  Close (Σ e_k) (Σ x_k) (Σ y_k)
    riemann_step  h' ≥ 0, |g_{2k} − f_k| ≤ ε, |g_{2k+1} − f_k| ≤ δ for k < n  ⇒
                  |Σ_{i<2n} h'·g_i − Σ_{k<n} 2h'·f_k| ≤ Σ_{k<n} h'·(ε + δ)
  With ε = the level-to-level coefficient bound at a fixed grid time and δ = ε + one fine step of TimeLipN, the
  Riemann sums S_j form a Cauchy sequence whose limit is the time integral on dyadic grids.
  Lean 4 core plus ns/RiemannRefine, lib/CauchyMul.
-/
import ns.RiemannRefine
import lib.CauchyMul

namespace RiemannStep
open DetChain NSChain LibRing LibCS LibGron LibCauchy RiemannRefine

section Gen
variable {R : Type} (K : CRing R) (P : Pos K)

theorem close_cmul {e x y c : R} (hc : P.nn c) (h : Close K P e x y) : Close K P (K.mul c e) (K.mul c x) (K.mul c y) := by
  constructor
  · rw [show sub K (K.mul c x) (K.mul c y) = K.mul c (sub K x y) by lib_ring]; exact le_mul_left K P hc h.1
  · rw [show sub K (K.mul c y) (K.mul c x) = K.mul c (sub K y x) by lib_ring]; exact le_mul_left K P hc h.2

theorem close_sumN (e x y : Nat → R) : ∀ n, (∀ k, k < n → Close K P (e k) (x k) (y k)) →
    Close K P (sumN K e n) (sumN K x n) (sumN K y n)
  | 0, _ => close_refl K P P.nn_zero K.zero
  | n + 1, h => close_add K P (close_sumN e x y n (fun k hk => h k (Nat.lt_succ_of_lt hk))) (h n (Nat.lt_succ_self n))

theorem sumN_zero : ∀ n, K.zero = sumN K (fun _ => K.zero) n
  | 0 => rfl
  | n + 1 => by show K.zero = K.add (sumN K (fun _ => K.zero) n) K.zero; rw [← sumN_zero n]; lib_ring

theorem riemann_step (h' ε δ : R) (hh : P.nn h') (f g : Nat → R) (n : Nat)
    (h1 : ∀ k, k < n → Close K P ε (g (2 * k)) (f k)) (h2 : ∀ k, k < n → Close K P δ (g (2 * k + 1)) (f k)) :
    Close K P (sumN K (fun _ => K.mul h' (K.add ε δ)) n)
      (sumN K (fun i => K.mul h' (g i)) (2 * n)) (sumN K (fun k => K.mul (K.add h' h') (f k)) n) := by
  apply close_of_sub K P
  have e := refine_diff K h' f g n
  rw [show sub K (sumN K (fun i => K.mul h' (g i)) (2 * n)) (sumN K (fun k => K.mul (K.add h' h') (f k)) n)
      = K.add (sumN K (fun i => K.mul h' (g i)) (2 * n)) (K.neg (sumN K (fun k => K.mul (K.add h' h') (f k)) n)) from rfl, e]
  rw [sumN_zero K n]
  refine close_sumN K P _ _ _ n (fun k hk => ?_)
  have a := close_sub K P (h1 k hk)
  have b := close_sub K P (h2 k hk)
  have c := close_cmul K P hh (close_add K P a b)
  rw [show K.mul h' (K.add K.zero K.zero) = K.zero by lib_ring] at c
  exact c

end Gen
end RiemannStep

#print axioms RiemannStep.close_cmul
#print axioms RiemannStep.close_sumN
#print axioms RiemannStep.riemann_step

ns/RootYoung.lean

lean_all/ns/RootYoung.lean

/-
  ns/RootYoung.lean — T37 (goal G859c2b2, piece c): root-free Young inequalities over an ordered field, for absorbing
  the Agmon force terms into the dissipation. For a, D, T ≥ 0:
    root2   T² ≤ 4aD   ⇒  T ≤ a + D        ((a + D)² − 4aD = (a − D)²)
    root4   T⁴ ≤ 4a³D  ⇒  T ≤ a + D        ((a + D)⁴ − 4a³D = a⁴ + 6a²D² + 4aD³ + D⁴)
  Use: both Agmon terms x³Z² and xZA satisfy T⁴ ≤ c·Z⁵B³ once x⁴Z ≲ B and A² ≤ ZB; with a = εB this gives
  T ≤ εB + c Z⁵/(4ε³), the H² step with an additive constant.

  Lean 4 core plus ns/DyadicSmallGlobal, ns/DyadicLarge.
-/
import ns.DyadicSmallGlobal
import ns.DyadicLarge
import ns.RatModel

namespace RootYoung
open DetChain NSChain DyadicSmallGlobal LibRing DyadicLarge

section Gen
variable {R : Type} (K : CRing R) (O : OrdR K)

def four : R := K.add (K.add K.one K.one) (K.add K.one K.one)

theorem four_nn : O.nn (four K) :=
  O.nn_add _ _ (O.nn_add _ _ (RatModel.nn_one' K O.toCone) (RatModel.nn_one' K O.toCone)) (O.nn_add _ _ (RatModel.nn_one' K O.toCone) (RatModel.nn_one' K O.toCone))

/-- **T² ≤ 4aD ⇒ T ≤ a + D** -/
theorem root2 (a D T : R) (ha : O.nn a) (hD : O.nn D)
    (h : Le K O.toCone (K.mul T T) (K.mul (four K) (K.mul a D))) : Le K O.toCone T (K.add a D) := by
  have hq : O.nn (K.add a D) := O.nn_add _ _ ha hD
  have e : K.mul (K.add a D) (K.add a D)
      = K.add (K.mul (four K) (K.mul a D)) (K.mul (K.add a (K.neg D)) (K.add a (K.neg D))) := by
    unfold four; lib_ring
  have h2 : Le K O.toCone (K.mul T T) (K.mul (K.add a D) (K.add a D)) := by
    rw [e]; exact le_trans' K O.toCone _ _ _ h ⟨_, O.nn_sq _, rfl⟩
  exact le_of_sq_le K O T (K.add a D) hq h2

/-- **T⁴ ≤ 4a³D ⇒ T ≤ a + D** -/
theorem root4 (a D T : R) (ha : O.nn a) (hD : O.nn D)
    (h : Le K O.toCone (K.mul (K.mul T T) (K.mul T T)) (K.mul (four K) (K.mul (K.mul (K.mul a a) a) D))) :
    Le K O.toCone T (K.add a D) := by
  have hq : O.nn (K.add a D) := O.nn_add _ _ ha hD
  have hq2 : O.nn (K.mul (K.add a D) (K.add a D)) := O.nn_sq _
  have rest : O.nn (K.add (K.add (K.mul (K.mul a a) (K.mul a a))
      (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 (K.mul a a) (K.mul D D))))
      (K.add (K.mul (four K) (K.mul a (K.mul (K.mul D D) D))) (K.mul (K.mul D D) (K.mul D D)))) := by
    have n2 : O.nn (K.add K.one K.one) := O.nn_add _ _ (RatModel.nn_one' K O.toCone) (RatModel.nn_one' K O.toCone)
    exact O.nn_add _ _ (O.nn_add _ _ (O.nn_mul _ _ (O.nn_sq _) (O.nn_sq _))
        (O.nn_mul _ _ (O.nn_add _ _ (four_nn K O) n2) (O.nn_mul _ _ (O.nn_sq _) (O.nn_sq _))))
      (O.nn_add _ _ (O.nn_mul _ _ (four_nn K O) (O.nn_mul _ _ ha (O.nn_mul _ _ (O.nn_sq _) hD)))
        (O.nn_mul _ _ (O.nn_sq _) (O.nn_sq _)))
  have e : K.mul (K.mul (K.add a D) (K.add a D)) (K.mul (K.add a D) (K.add a D))
      = K.add (K.mul (four K) (K.mul (K.mul (K.mul a a) a) D)) (K.add (K.add (K.mul (K.mul a a) (K.mul a a))
      (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 (K.mul a a) (K.mul D D))))
      (K.add (K.mul (four K) (K.mul a (K.mul (K.mul D D) D))) (K.mul (K.mul D D) (K.mul D D)))) := by
    unfold four; lib_ring
  have h4 : Le K O.toCone (K.mul (K.mul T T) (K.mul T T))
      (K.mul (K.mul (K.add a D) (K.add a D)) (K.mul (K.add a D) (K.add a D))) := by
    rw [e]; exact le_trans' K O.toCone _ _ _ h ⟨_, rest, rfl⟩
  have h2 := le_of_sq_le K O (K.mul T T) (K.mul (K.add a D) (K.add a D)) hq2 h4
  exact le_of_sq_le K O T (K.add a D) hq h2

end Gen

end RootYoung

#print axioms RootYoung.four_nn
#print axioms RootYoung.root2
#print axioms RootYoung.root4

ns/SchemeLip.lean

lean_all/ns/SchemeLip.lean

/-
  ns/SchemeLip.lean — T37 cycle 925 (goal G614b): continuous dependence for the projected semi-implicit 3D Galerkin
  scheme NS3DScheme.sch, over any ordered field (ℚ included). For two solutions a, b (same parameters, initial data
  a0, b0), with D_t = Σ_{p<N} |a_{t,p} − b_{t,p}|², E^a_t, E^b_t their energies and K_k = Σ_{q<N} |k_q|²:
    D_{t+1} ≤ (1 + h + (h + h²) · 2ι² K_k N² (E^a_t + E^b_t)) · D_t.
  With NS3DScheme.global (E ≤ 2E_0) this gives D_t ≤ ρ^t D_0 on the bounded horizon: discrete uniqueness and
  Lipschitz dependence on the data for every truncation.

  Lean 4 core plus ns/NS3DScheme (sch), ns/StepStab3 (comb_sub, sum_stab), ns/ForceLip (lip_mode),
  ns/ForceSum (rl, lsum_rl, lsum_const), ns/ForcedCN (ne_of_one_add via GoodModesQ).
-/
import ns.NS3DScheme
import ns.StepStab3
import ns.ForceLip

namespace SchemeLip
open DetChain NSChain DyadicSmallGlobal LibRing DyadicLarge FourierGalerkin LerayProj GalerkinForce ProjStep ForceSum
  NS3DScheme StepStab3 ForceLip GoodModesQ

section Gen
variable {R : Type} (K : CRing R) (O : OrdR K)

theorem sumN_const (c : R) : ∀ N, sumN K (fun _ => c) N = K.mul (castN K N) c
  | 0 => by show K.zero = K.mul K.zero c; lib_ring
  | N + 1 => by
    show K.add (sumN K (fun _ => c) N) c = K.mul (K.add K.one (castN K N)) c
    rw [sumN_const c N]; lib_ring

include O

theorem diff_step (inv : R → R) (mul_inv : ∀ y, y ≠ K.zero → K.mul y (inv y) = K.one) (h10 : K.one ≠ K.zero)
    (δ : Nat → Nat → Nat → R) (ι h : R) (k : Nat → V3 R) (x κ : Nat → R) (N : Nat) (a0 b0 : Nat → V3 R)
    (hx : ∀ p, O.nn (x p)) (hh : O.nn h) (hκ : ∀ p, O.nn (κ p)) (hκk : ∀ p, K.mul (dot3 K (k p) (k p)) (κ p) = K.one)
    (hδ : ∀ p q r, δ p q r = K.zero ∨ δ p q r = K.one) (t : Nat) :
    Le K O.toCone (sumN K (fun p => dot3 K (sub3 K (sch K inv δ ι h k x κ N a0 (t + 1) p) (sch K inv δ ι h k x κ N b0 (t + 1) p))
        (sub3 K (sch K inv δ ι h k x κ N a0 (t + 1) p) (sch K inv δ ι h k x κ N b0 (t + 1) p))) N)
      (K.mul (K.add (K.add K.one h) (K.mul (K.add h (K.mul h h)) (K.mul (K.mul (K.add K.one K.one)
          (K.mul (K.mul ι ι) (K.mul (sumN K (fun q => dot3 K (k q) (k q)) N) (K.mul (castN K N) (castN K N)))))
          (K.add (sumN K (fun p => dot3 K (sch K inv δ ι h k x κ N a0 t p) (sch K inv δ ι h k x κ N a0 t p)) N)
            (sumN K (fun p => dot3 K (sch K inv δ ι h k x κ N b0 t p) (sch K inv δ ι h k x κ N b0 t p)) N)))))
        (sumN K (fun p => dot3 K (sub3 K (sch K inv δ ι h k x κ N a0 t p) (sch K inv δ ι h k x κ N b0 t p))
          (sub3 K (sch K inv δ ι h k x κ N a0 t p) (sch K inv δ ι h k x κ N b0 t p))) N)) := by
  generalize ha : sch K inv δ ι h k x κ N a0 t = a
  generalize hb : sch K inv δ ι h k x κ N b0 t = b
  have hι : ∀ p, K.mul (K.add K.one (x p)) (inv (K.add K.one (x p))) = K.one :=
    fun p => mul_inv _ (ne_of_one_add K O h10 (x p) (hx p))
  let G : Nat → V3 R := fun p => sub3 K (proj K (κ p) (k p) (Nl K (rl N) δ ι k a a p)) (proj K (κ p) (k p) (Nl K (rl N) δ ι k b b p))
  have step : ∀ p, sub3 K (sch K inv δ ι h k x κ N a0 (t + 1) p) (sch K inv δ ι h k x κ N b0 (t + 1) p)
      = comb K (inv (K.add K.one (x p))) (x p) h (sub3 K (a p) (b p)) (G p) := by
    intro p
    show sub3 K (comb K (inv (K.add K.one (x p))) (x p) h (sch K inv δ ι h k x κ N a0 t p)
        (proj K (κ p) (k p) (Nl K (rl N) δ ι k (sch K inv δ ι h k x κ N a0 t) (sch K inv δ ι h k x κ N a0 t) p)))
      (comb K (inv (K.add K.one (x p))) (x p) h (sch K inv δ ι h k x κ N b0 t p)
        (proj K (κ p) (k p) (Nl K (rl N) δ ι k (sch K inv δ ι h k x κ N b0 t) (sch K inv δ ι h k x κ N b0 t) p))) = _
    rw [ha, hb, comb_sub]
  rw [sumN_ext K _ _ (fun p => by rw [step p]) N]
  have s1 := sum_stab K O.toCone (fun p => inv (K.add K.one (x p))) x h (fun p => sub3 K (a p) (b p)) G hx hh hι N
  -- Σ_r |G_r|² ≤ N · (2ι²(D K_k)(N E_a) + 2ι²(E_b K_k)(N D))
  have s2 := sum_le K O.toCone _ _ (fun r => lip_mode K O.toCone (rl N) δ ι k a b (κ r) r hδ (hκ r) (hκk r)) N
  rw [lsum_const, lsum_const] at s2
  repeat rw [lsum_rl] at s2
  rw [sumN_const] at s2
  have s3 := le_mul_nn K O.toCone (K.add h (K.mul h h)) _ _ (O.nn_add _ _ hh (O.nn_sq h)) s2
  have s4 := le_trans' K O.toCone _ _ _ s1 (le_add' K O.toCone _ _ _ _ (le_refl' K O.toCone _) s3)
  refine le_trans' K O.toCone _ _ _ s4 ⟨K.zero, O.nn_zero, ?_⟩
  generalize sumN K (fun p => dot3 K (sub3 K (a p) (b p)) (sub3 K (a p) (b p))) N = D
  generalize sumN K (fun q => dot3 K (k q) (k q)) N = Kk
  generalize sumN K (fun p => dot3 K (a p) (a p)) N = Ea
  generalize sumN K (fun p => dot3 K (b p) (b p)) N = Eb
  generalize castN K N = n
  lib_ring

/-- Discrete Gronwall: D_{t+1} ≤ c_t D_t with 0 ≤ c_t ≤ ρ, D_t ≥ 0 ⇒ D_t ≤ ρ^t D_0 for t ≤ T. -/
theorem gronwall (D c : Nat → R) (ρ : R) (T : Nat) (hD : ∀ t, O.nn (D t)) (hcn : ∀ t, O.nn (c t))
    (hc : ∀ t, t < T → Le K O.toCone (c t) ρ) (st : ∀ t, t < T → Le K O.toCone (D (t + 1)) (K.mul (c t) (D t))) :
    ∀ t, t ≤ T → Le K O.toCone (D t) (K.mul (pw K ρ t) (D 0))
  | 0, _ => ⟨K.zero, O.nn_zero, by show K.add (D 0) K.zero = K.mul K.one (D 0); lib_ring⟩
  | t + 1, ht => by
    have ih := gronwall D c ρ T hD hcn hc st t (Nat.le_of_lt ht)
    have ρnn : O.nn ρ := by
      cases hc t ht with
      | intro w hw => rw [← hw.2]; exact O.nn_add _ _ (hcn t) hw.1
    cases st t ht with
    | intro w1 hw1 => cases hc t ht with
      | intro w2 hw2 => cases ih with
        | intro w3 hw3 =>
          refine ⟨K.add w1 (K.add (K.mul w2 (D t)) (K.mul ρ w3)),
            O.nn_add _ _ hw1.1 (O.nn_add _ _ (O.nn_mul _ _ hw2.1 (hD t)) (O.nn_mul _ _ ρnn hw3.1)), ?_⟩
          show K.add (D (t + 1)) (K.add w1 (K.add (K.mul w2 (D t)) (K.mul ρ w3))) = K.mul (K.mul (pw K ρ t) ρ) (D 0)
          have e1 := hw1.2; have e2 := hw2.2; have e3 := hw3.2
          calc K.add (D (t + 1)) (K.add w1 (K.add (K.mul w2 (D t)) (K.mul ρ w3)))
              = K.add (K.add (D (t + 1)) w1) (K.add (K.mul w2 (D t)) (K.mul ρ w3)) := by lib_ring
            _ = K.add (K.mul (c t) (D t)) (K.add (K.mul w2 (D t)) (K.mul ρ w3)) := by rw [e1]
            _ = K.add (K.mul (K.add (c t) w2) (D t)) (K.mul ρ w3) := by lib_ring
            _ = K.add (K.mul ρ (D t)) (K.mul ρ w3) := by rw [e2]
            _ = K.mul ρ (K.add (D t) w3) := by lib_ring
            _ = K.mul ρ (K.mul (pw K ρ t) (D 0)) := by rw [e3]
            _ = _ := by lib_ring

/-- **Lipschitz dependence on the data** for the projected 3D scheme, on the horizon of NS3DScheme.global. -/
theorem lipschitz (inv : R → R) (mul_inv : ∀ y, y ≠ K.zero → K.mul y (inv y) = K.one) (h10 : K.one ≠ K.zero)
    (δ : Nat → Nat → Nat → R) (ι h : R) (k : Nat → V3 R) (x κ : Nat → R) (N T : Nat) (a0 b0 : Nat → V3 R)
    (hx : ∀ p, O.nn (x p)) (hh : O.nn h) (hκ : ∀ p, O.nn (κ p)) (hκk : ∀ p, K.mul (dot3 K (k p) (k p)) (κ p) = K.one)
    (htri : ∀ p q r, δ p q r = K.zero ∨ (δ p q r = K.one ∧ add3 K (k q) (k r) = neg3 K (k p)))
    (hsym : ∀ p q r, δ p q r = δ p r q) (h2 : ∀ y : R, K.add y y = K.zero → y = K.zero)
    (ha0 : ∀ p, dot3 K (k p) (a0 p) = K.zero) (hb0 : ∀ p, dot3 K (k p) (b0 p) = K.zero)
    (smalla : Le K O.toCone (K.mul (castN K (4 * T)) (K.mul (K.mul (K.mul h h)
        (K.mul (sumN K (fun r => K.add (x r) (K.add K.one K.one)) N)
          (K.mul (K.mul (K.mul ι ι) (sumN K (fun q => dot3 K (k q) (k q)) N)) (castN K N))))
        (sumN K (fun p => dot3 K (a0 p) (a0 p)) N))) K.one)
    (smallb : Le K O.toCone (K.mul (castN K (4 * T)) (K.mul (K.mul (K.mul h h)
        (K.mul (sumN K (fun r => K.add (x r) (K.add K.one K.one)) N)
          (K.mul (K.mul (K.mul ι ι) (sumN K (fun q => dot3 K (k q) (k q)) N)) (castN K N))))
        (sumN K (fun p => dot3 K (b0 p) (b0 p)) N))) K.one) :
    ∀ t, t ≤ T → Le K O.toCone
      (sumN K (fun p => dot3 K (sub3 K (sch K inv δ ι h k x κ N a0 t p) (sch K inv δ ι h k x κ N b0 t p))
        (sub3 K (sch K inv δ ι h k x κ N a0 t p) (sch K inv δ ι h k x κ N b0 t p))) N)
      (K.mul (pw K (K.add (K.add K.one h) (K.mul (K.add h (K.mul h h)) (K.mul (K.mul (K.add K.one K.one)
          (K.mul (K.mul ι ι) (K.mul (sumN K (fun q => dot3 K (k q) (k q)) N) (K.mul (castN K N) (castN K N)))))
          (K.add (K.add (sumN K (fun p => dot3 K (a0 p) (a0 p)) N) (sumN K (fun p => dot3 K (a0 p) (a0 p)) N))
            (K.add (sumN K (fun p => dot3 K (b0 p) (b0 p)) N) (sumN K (fun p => dot3 K (b0 p) (b0 p)) N)))))) t)
        (sumN K (fun p => dot3 K (sub3 K (a0 p) (b0 p)) (sub3 K (a0 p) (b0 p))) N)) := by
  have hδ : ∀ p q r, δ p q r = K.zero ∨ δ p q r = K.one := fun p q r =>
    (htri p q r).elim Or.inl (fun h => Or.inr h.1)
  have ga := NS3DScheme.global K O inv mul_inv h10 δ ι h k x κ N T a0 hx hκ hκk htri hsym h2 ha0 smalla
  have gb := NS3DScheme.global K O inv mul_inv h10 δ ι h k x κ N T b0 hx hκ hκk htri hsym h2 hb0 smallb
  have Qnn : O.nn (K.mul (K.add K.one K.one) (K.mul (K.mul ι ι) (K.mul (sumN K (fun q => dot3 K (k q) (k q)) N)
      (K.mul (castN K N) (castN K N))))) :=
    O.nn_mul _ _ (O.nn_add _ _ (StepStab.one_nn K O.toCone) (StepStab.one_nn K O.toCone))
      (O.nn_mul _ _ (O.nn_sq ι) (O.nn_mul _ _ (sumN_nn K O.toCone _ (fun q => ForceBound.dot3_nn K O.toCone (k q)) N)
        (O.nn_mul _ _ (castN_nn K O N) (castN_nn K O N))))
  have hhh : O.nn (K.add h (K.mul h h)) := O.nn_add _ _ hh (O.nn_sq h)
  have h1h : O.nn (K.add K.one h) := O.nn_add _ _ (StepStab.one_nn K O.toCone) hh
  generalize hQ : K.mul (K.add K.one K.one) (K.mul (K.mul ι ι) (K.mul (sumN K (fun q => dot3 K (k q) (k q)) N)
      (K.mul (castN K N) (castN K N)))) = Q at Qnn ⊢
  refine gronwall K O _ (fun t => K.add (K.add K.one h) (K.mul (K.add h (K.mul h h)) (K.mul Q
      (K.add (sumN K (fun p => dot3 K (sch K inv δ ι h k x κ N a0 t p) (sch K inv δ ι h k x κ N a0 t p)) N)
        (sumN K (fun p => dot3 K (sch K inv δ ι h k x κ N b0 t p) (sch K inv δ ι h k x κ N b0 t p)) N))))) _ T
    (fun t => sumN_nn K O.toCone _ (fun p => ForceBound.dot3_nn K O.toCone _) N)
    (fun t => O.nn_add _ _ h1h (O.nn_mul _ _ hhh (O.nn_mul _ _ Qnn (O.nn_add _ _
      (sumN_nn K O.toCone _ (fun p => ForceBound.dot3_nn K O.toCone _) N) (sumN_nn K O.toCone _ (fun p => ForceBound.dot3_nn K O.toCone _) N)))))
    (fun t ht => le_add' K O.toCone _ _ _ _ (le_refl' K O.toCone _) (le_mul_nn K O.toCone _ _ _ hhh
      (le_mul_nn K O.toCone _ _ _ Qnn (le_add' K O.toCone _ _ _ _ (ga t (Nat.le_of_lt ht)) (gb t (Nat.le_of_lt ht))))))
    (fun t _ => ?_)
  have d := diff_step K O inv mul_inv h10 δ ι h k x κ N a0 b0 hx hh hκ hκk hδ t
  rw [hQ] at d
  exact d

end Gen

end SchemeLip

#print axioms SchemeLip.sumN_const
#print axioms SchemeLip.diff_step
#print axioms SchemeLip.gronwall
#print axioms SchemeLip.lipschitz

ns/SchemeTele.lean

lean_all/ns/SchemeTele.lean

/-
  ns/SchemeTele.lean — T37 cycle 1794 (goal G856a): the time-integrated form of the implicit scheme, one coefficient
  at a time, over a commutative ring. If (1 + hνw)·x_{t+1} = x_t + h·N_t for all t (EnstStep.StepC), then
    step_incr   x_{t+1} = x_t + h·(N_t − νw·x_{t+1})
    tele        x_n = x_0 + Σ_{t<n} h·(N_t − νw·x_{t+1})
  — the discrete integral equation x(T) = x(0) + ∫₀ᵀ (N − νwx) that the limit of the coefficient sequences
  (TruncCoef, DyadicCoef) is to satisfy.
  Lean 4 core plus ns/EnstStep.
-/
import ns.EnstStep

namespace SchemeTele
open DetChain NSChain LibRing EnstStep

section Gen
variable {R : Type} (K : CRing R)

def tsum (f : Nat → R) : Nat → R
  | 0 => K.zero
  | n + 1 => K.add (tsum f n) (f n)

def incr (h ν w : R) (x N : Nat → R) (t : Nat) : R :=
  K.mul h (K.add (N t) (K.neg (K.mul (K.mul ν w) (x (t + 1)))))

theorem step_incr (h ν w : R) (x N : Nat → R) (t : Nat) (s : StepC K h ν w (x (t + 1)) (x t) (N t)) :
    x (t + 1) = K.add (x t) (incr K h ν w x N t) := by
  have key : K.add (x (t + 1)) (K.neg (K.add (x t) (incr K h ν w x N t)))
      = K.add (K.mul (K.add K.one (K.mul (K.mul h ν) w)) (x (t + 1))) (K.neg (K.add (x t) (K.mul h (N t)))) := by
    show K.add (x (t + 1)) (K.neg (K.add (x t) (K.mul h (K.add (N t) (K.neg (K.mul (K.mul ν w) (x (t + 1))))))))
      = K.add (K.mul (K.add K.one (K.mul (K.mul h ν) w)) (x (t + 1))) (K.neg (K.add (x t) (K.mul h (N t))))
    lib_ring
  have s' : K.mul (K.add K.one (K.mul (K.mul h ν) w)) (x (t + 1)) = K.add (x t) (K.mul h (N t)) := s
  rw [s'] at key
  calc x (t + 1) = K.add (K.add (x (t + 1)) (K.neg (K.add (x t) (incr K h ν w x N t))))
        (K.add (x t) (incr K h ν w x N t)) := by lib_ring
    _ = K.add (K.add (K.add (x t) (K.mul h (N t))) (K.neg (K.add (x t) (K.mul h (N t)))))
        (K.add (x t) (incr K h ν w x N t)) := by rw [key]
    _ = K.add (x t) (incr K h ν w x N t) := by lib_ring

theorem tele (h ν w : R) (x N : Nat → R) (s : ∀ t, StepC K h ν w (x (t + 1)) (x t) (N t)) :
    ∀ n, x n = K.add (x 0) (tsum K (incr K h ν w x N) n)
  | 0 => by show x 0 = K.add (x 0) K.zero; lib_ring
  | n + 1 => by
    rw [step_incr K h ν w x N n (s n), tele h ν w x N s n]
    show K.add (K.add (x 0) (tsum K (incr K h ν w x N) n)) (incr K h ν w x N n)
      = K.add (x 0) (K.add (tsum K (incr K h ν w x N) n) (incr K h ν w x N n))
    lib_ring

end Gen
end SchemeTele

#print axioms SchemeTele.step_incr
#print axioms SchemeTele.tele

ns/SelfBudget.lean

lean_all/ns/SelfBudget.lean

/-
  ns/SelfBudget.lean — T37 cycle 191: the Serrin budget bounds itself (quadratic inequality), short-time form.

  Lean 4 core plus ns/DissBudget, ns/SerrinAgmon, lib/OField, lib/RingTac. For a run with
      Z_{k+1} ≤ Z_k + h (c X_k r_k s_k − ν s_k²),  r_k² = Z_k,  X_k⁴ ≤ C Z_k s_k²  (Agmon, P_k = s_k²),
  Z_k ≤ M for k < N, and T = Σ_{k<N} X_k², A = h c²/(2ν):
      h ν T² ≤ (2 N C M) (Z_0 + M A T)                                                      (self_budget)
  (DissBudget bounds hνΣs² by 2Z_0 + 2MAT; prefix Cauchy–Schwarz and Agmon bound T² by N C M Σs².) And for any
  ordered field, the root-free reading of a quadratic inequality:
      T² ≤ α + β T,  β, q ≥ 0,  α ≤ q²,  q ≠ 0   ⇒   T ≤ β + q                                (quad_root)
  So T stays below the positive root: the budget A·T is small for N h ≲ ν³/(c⁴ C M²) — the local time of RiccatiGen.
-/
import ns.DissBudget

namespace SelfBudget
open DetChain LibRing LibCS LibGron LibOF SerrinDiscrete SerrinSum SerrinRun SerrinAgmon DissBudget

variable {R : Type} (K : CRing R) (P : Pos K)

/-- **Root-free quadratic bound.** -/
theorem quad_root (F : OF K P) (T α β q : R) (hβ : P.nn β) (hq : P.nn q) (hq0 : q ≠ K.zero)
    (hα : le K P α (K.mul q q)) (h : le K P (K.mul T T) (K.add α (K.mul β T))) :
    le K P T (K.add β q) := by
  cases le_total K P F T β with
  | inl hl =>
    unfold le at hl ⊢
    rw [show sub K (K.add β q) T = K.add (sub K β T) q by lib_ring]
    exact P.nn_add _ _ hl hq
  | inr hg =>
    -- d = T − β ≥ 0:  d² = d T − β d ≤ d T = T² − β T ≤ α ≤ q²
    have hd : P.nn (sub K T β) := hg
    have sq : le K P (K.mul (sub K T β) (sub K T β)) (K.mul q q) := by
      unfold le at h hα ⊢
      have e : sub K (K.mul q q) (K.mul (sub K T β) (sub K T β))
          = K.add (K.add (sub K (K.mul q q) α) (sub K (K.add α (K.mul β T)) (K.mul T T)))
              (K.mul β (sub K T β)) := by lib_ring
      rw [e]; exact P.nn_add _ _ (P.nn_add _ _ hα h) (P.nn_mul _ _ hβ hd)
    have r := le_of_sq K P F _ q hd hq hq0 sq
    unfold le at r ⊢
    rw [show sub K (K.add β q) T = sub K q (sub K T β) by lib_ring]
    exact r

/-- **The Serrin budget bounds itself.** -/
theorem self_budget (F : OF K P) (Z r s X : Nat → R) (h c ν C M : R) (hh : P.nn h) (hν : P.nn ν)
    (hν0 : K.add ν ν ≠ K.zero) (hr : ∀ k, K.mul (r k) (r k) = Z k)
    (step : ∀ k, le K P (Z (k + 1))
      (K.add (Z k) (K.mul h (sub K (K.mul (K.mul (K.mul c (X k)) (r k)) (s k)) (K.mul ν (K.mul (s k) (s k)))))))
    (hC : P.nn C) (hM : P.nn M)
    (hA : ∀ k, le K P (K.mul (K.mul (X k) (X k)) (K.mul (X k) (X k))) (K.mul C (K.mul (Z k) (K.mul (s k) (s k)))))
    (N : Nat) (hZ : ∀ k, k < N → le K P (Z k) M) :
    le K P (K.mul (K.mul h ν) (K.mul (sumA K (fun k => K.mul (X k) (X k)) N) (sumA K (fun k => K.mul (X k) (X k)) N)))
      (K.mul (K.add (K.mul (castN K N) (K.mul C M)) (K.mul (castN K N) (K.mul C M)))
        (K.add (Z 0) (K.mul M (K.mul (amp K P F h c ν) (sumA K (fun k => K.mul (X k) (X k)) N))))) := by
  have hamp : P.nn (amp K P F h c ν) :=
    P.nn_mul _ _ (nn_inv K P F _ (P.nn_add _ _ hν hν) hν0) (P.nn_mul _ _ hh (P.nn_sq c))
  have ha : ∀ k, P.nn (aRun K P F h c ν X k) := fun k =>
    P.nn_mul _ _ (nn_inv K P F _ (P.nn_add _ _ hν hν) hν0)
      (P.nn_mul _ _ hh (P.nn_mul _ _ (P.nn_sq c) (P.nn_sq (X k))))
  -- (1) dissipation: hν Σs² ≤ 2 Z_0 + 2 Σ a_k Z_k
  have db := diss_budget K P F Z r s X h c ν hh hν hν0 hr step N
  have hZN : P.nn (Z N) := by rw [← hr N]; exact P.nn_sq _
  -- Σ a_k Z_k ≤ M Σ a_k = M A T
  have az := sumA_le K P (fun k => K.mul (aRun K P F h c ν X k) (Z k)) (fun k => K.mul M (aRun K P F h c ν X k)) N
    (fun k hk => by
      have m := le_mul_left K P (ha k) (hZ k hk)
      rw [show K.mul (aRun K P F h c ν X k) M = K.mul M (aRun K P F h c ν X k) by lib_ring] at m
      exact m)
  rw [sumA_mul K M, sum_aRun] at az
  have ds : le K P (K.mul (K.mul h ν) (sumA K (fun k => K.mul (s k) (s k)) N))
      (K.add (K.add (Z 0) (Z 0)) (K.add (K.mul M (K.mul (amp K P F h c ν) (sumA K (fun k => K.mul (X k) (X k)) N)))
        (K.mul M (K.mul (amp K P F h c ν) (sumA K (fun k => K.mul (X k) (X k)) N))))) := by
    have t := le_trans K P db (le_add2 K P (le_refl K P (K.add (Z 0) (Z 0))) (le_add2 K P az az))
    unfold le at t ⊢
    rw [show sub K (K.add (K.add (Z 0) (Z 0)) (K.add (K.mul M (K.mul (amp K P F h c ν) (sumA K (fun k => K.mul (X k) (X k)) N)))
          (K.mul M (K.mul (amp K P F h c ν) (sumA K (fun k => K.mul (X k) (X k)) N)))))
          (K.mul (K.mul h ν) (sumA K (fun k => K.mul (s k) (s k)) N))
        = K.add (sub K (K.add (K.add (Z 0) (Z 0)) (K.add (K.mul M (K.mul (amp K P F h c ν) (sumA K (fun k => K.mul (X k) (X k)) N)))
          (K.mul M (K.mul (amp K P F h c ν) (sumA K (fun k => K.mul (X k) (X k)) N)))))
          (K.add (K.add (Z N) (Z N)) (K.mul (K.mul h ν) (sumA K (fun k => K.mul (s k) (s k)) N)))) (K.add (Z N) (Z N)) by lib_ring]
    exact P.nn_add _ _ t (P.nn_add _ _ hZN hZN)
  -- (2) Agmon prefix: T² ≤ N (C M) Σs²
  have ag := agmon_prefix K P X Z (fun k => K.mul (s k) (s k)) C M N hC (fun k => P.nn_sq _) hA hZ
  -- hν T² ≤ N C M · hν Σs² ≤ N C M (2Z_0 + 2MAT)
  have m1 := le_mul_left K P (P.nn_mul _ _ hh hν) ag
  have m2 := le_mul_left K P (P.nn_mul _ _ (nn_castN K P N) (P.nn_mul _ _ hC hM)) ds
  generalize sumA K (fun k => K.mul (X k) (X k)) N = T at m1 m2 ⊢
  generalize sumA K (fun k => K.mul (s k) (s k)) N = S at m1 m2
  generalize amp K P F h c ν = a at m1 m2 ⊢
  generalize castN K N = n at m1 m2 ⊢
  unfold le at m1 m2 ⊢
  have e : sub K (K.mul (K.add (K.mul n (K.mul C M)) (K.mul n (K.mul C M))) (K.add (Z 0) (K.mul M (K.mul a T))))
        (K.mul (K.mul h ν) (K.mul T T))
      = K.add (sub K (K.mul (K.mul n (K.mul C M)) (K.add (K.add (Z 0) (Z 0)) (K.add (K.mul M (K.mul a T)) (K.mul M (K.mul a T)))))
          (K.mul (K.mul n (K.mul C M)) (K.mul (K.mul h ν) S)))
        (sub K (K.mul (K.mul h ν) (K.mul n (K.mul (K.mul C M) S))) (K.mul (K.mul h ν) (K.mul T T))) := by lib_ring
  rw [e]; exact P.nn_add _ _ m2 m1

end SelfBudget

#print axioms SelfBudget.quad_root
#print axioms SelfBudget.self_budget

ns/SemiBound.lean

lean_all/ns/SemiBound.lean

/-
  ns/SemiBound.lean — T37 cycle 792 (goal G503): the energy inequality of the semi-implicit scheme over any ring
  with a positive cone (ℚ included): with a'_n − a_n = −x_n s_n + h N_n (s = a' + a), x_n ≥ 0 and Σ a N = 0,
    2E' + Σ x_n s_n² ≤ 2E + h² Σ (x_n + 2) N_n².
  The growth per step is O(h²)·Σ(x+2)N²: second order, as for CN, but with an explicit next state (ForcedCN).

  Lean 4 core plus ns/SemiEnergy (semi_energy), ns/DyadicLarge (le_add', sum_le), lib/RingTac.
    mode_bound   2(a'² − a²) + x s² ≤ h²(x + 2)N² + 4h·a·N      (completing the square x(s + hN)² ≥ 0)
    semi_bound   the summed inequality
-/
import ns.SemiEnergy
import ns.DyadicLarge

namespace SemiBound
open DetChain NSChain LibRing DyadicLarge SemiEnergy

section Gen
variable {R : Type} (K : CRing R) (P : Cone K)

def two (y : R) : R := K.add y y

theorem mode_bound (a a' x hN0 h : R) (hx : P.nn x)
    (st : K.add a' (K.neg a) = K.add (K.neg (K.mul x (K.add a' a))) (K.mul h hN0)) :
    Le K P (K.add (two K (K.mul a' a')) (K.mul x (K.mul (K.add a' a) (K.add a' a))))
      (K.add (two K (K.mul a a)) (K.add (K.mul (K.mul h h) (K.mul (K.add x (K.add K.one K.one)) (K.mul hN0 hN0)))
        (two K (two K (K.mul h (K.mul a hN0)))))) := by
  -- witness: x (s + hN)²
  refine ⟨K.mul x (K.mul (K.add (K.add a' a) (K.mul h hN0)) (K.add (K.add a' a) (K.mul h hN0))),
    P.nn_mul _ _ hx (P.nn_sq _), ?_⟩
  -- use a' = a − x s + hN to eliminate a'² via the step
  have z : K.add (K.add a' (K.neg a)) (K.neg (K.add (K.neg (K.mul x (K.add a' a))) (K.mul h hN0))) = K.zero := by
    rw [st]; lib_ring
  have t : K.add (K.add (two K (K.mul a' a')) (K.mul x (K.mul (K.add a' a) (K.add a' a))))
        (K.mul x (K.mul (K.add (K.add a' a) (K.mul h hN0)) (K.add (K.add a' a) (K.mul h hN0))))
      = K.add (K.add (two K (K.mul a a)) (K.add (K.mul (K.mul h h) (K.mul (K.add x (K.add K.one K.one)) (K.mul hN0 hN0)))
          (two K (two K (K.mul h (K.mul a hN0))))))
        (K.mul (K.add (two K (K.add a' a)) (two K (K.mul h hN0)))
          (K.add (K.add a' (K.neg a)) (K.neg (K.add (K.neg (K.mul x (K.add a' a))) (K.mul h hN0))))) := by
    unfold two; lib_ring
  exact t.trans (by rw [z]; lib_ring)

theorem two_sum (f : Nat → R) : ∀ M, sumN K (fun n => two K (f n)) M = two K (sumN K f M)
  | 0 => by show K.zero = K.add K.zero K.zero; lib_ring
  | M + 1 => by
    show K.add (sumN K (fun n => two K (f n)) M) (two K (f M)) = two K (K.add (sumN K f M) (f M))
    rw [two_sum f M]; unfold two; lib_ring

theorem semi_bound (a a' x Nn : Nat → R) (h : R) (N : Nat) (hx : ∀ n, P.nn (x n))
    (st : ∀ n, K.add (a' n) (K.neg (a n)) = K.add (K.neg (K.mul (x n) (K.add (a' n) (a n)))) (K.mul h (Nn n)))
    (neutral : sumN K (fun n => K.mul (a n) (Nn n)) N = K.zero) :
    Le K P (K.add (two K (sumN K (fun n => K.mul (a' n) (a' n)) N))
        (sumN K (fun n => K.mul (x n) (K.mul (K.add (a' n) (a n)) (K.add (a' n) (a n)))) N))
      (K.add (two K (sumN K (fun n => K.mul (a n) (a n)) N))
        (K.mul (K.mul h h) (sumN K (fun n => K.mul (K.add (x n) (K.add K.one K.one)) (K.mul (Nn n) (Nn n))) N))) := by
  have sm := sum_le K P _ _ (fun n => mode_bound K P (a n) (a' n) (x n) (Nn n) h (hx n) (st n)) N
  let A' : Nat → R := fun n => K.mul (a' n) (a' n)
  let A : Nat → R := fun n => K.mul (a n) (a n)
  let D : Nat → R := fun n => K.mul (x n) (K.mul (K.add (a' n) (a n)) (K.add (a' n) (a n)))
  let G : Nat → R := fun n => K.mul (K.add (x n) (K.add K.one K.one)) (K.mul (Nn n) (Nn n))
  let C : Nat → R := fun n => K.mul (a n) (Nn n)
  have L : sumN K (fun n => K.add (two K (A' n)) (D n)) N = K.add (two K (sumN K A' N)) (sumN K D N) := by
    rw [sumN_add, two_sum]
  have Rr : sumN K (fun n => K.add (two K (A n)) (K.add (K.mul (K.mul h h) (G n)) (two K (two K (K.mul h (C n)))))) N
      = K.add (K.add (two K (sumN K A N)) (K.mul (K.mul h h) (sumN K G N))) (two K (two K (K.mul h (sumN K C N)))) := by
    rw [sumN_add, sumN_add, two_sum, sumN_mul_left, two_sum, two_sum, sumN_mul_left]; lib_ring
  have sm' : Le K P (sumN K (fun n => K.add (two K (A' n)) (D n)) N)
      (sumN K (fun n => K.add (two K (A n)) (K.add (K.mul (K.mul h h) (G n)) (two K (two K (K.mul h (C n)))))) N) := sm
  rw [L, Rr] at sm'
  have hC : sumN K C N = K.zero := neutral
  rw [hC] at sm'
  have e : K.add (K.add (two K (sumN K A N)) (K.mul (K.mul h h) (sumN K G N))) (two K (two K (K.mul h K.zero)))
      = K.add (two K (sumN K A N)) (K.mul (K.mul h h) (sumN K G N)) := by unfold two; lib_ring
  rw [e] at sm'; exact sm'

end Gen

end SemiBound

#print axioms SemiBound.mode_bound
#print axioms SemiBound.two_sum
#print axioms SemiBound.semi_bound

ns/SemiBudget.lean

lean_all/ns/SemiBudget.lean

/-
  ns/SemiBudget.lean — T37 cycle 806 (goal G518): the dissipation budget of a scheme with a quadratic defect,
  over any ordered ring (ℚ included): if
    2e_{t+1} + D_t ≤ 2e_t + K e_t²,   e_t ≥ 0,   e_t ≤ 2e_0 (t < T),   4T·K·e_0 ≤ 1,
  then Σ_{t<T} D_t ≤ 3e_0. For the semi-implicit dyadic scheme (SemiBound + DyCN + SemiDyadic) this is
  Σ_t Σ_n hνk (a_{t+1} + a_t)² ≤ 3E_0 — the discrete ν∫‖∇u‖² bound with an explicit next state.

  Lean 4 core plus ns/RiccatiH (sq_le, castN lemmas), ns/DyadicLarge, lib/RingTac.
    tele_budget   2e_T + Σ_{t<T} D_t ≤ 2e_0 + Σ_{t<T} K e_t²
    budget3       Σ_{t<T} D_t ≤ 3e_0
-/
import ns.RiccatiH

namespace SemiBudget
open DetChain NSChain DyadicSmallGlobal LibRing DyadicLarge RatModel RiccatiH

section Gen
variable {R : Type} (K : CRing R) (O : OrdR K)
include O

theorem tele_budget (e D : Nat → R) (Kc : R)
    (st : ∀ t, Le K O.toCone (K.add (K.add (e (t + 1)) (e (t + 1))) (D t)) (K.add (K.add (e t) (e t)) (K.mul Kc (K.mul (e t) (e t))))) :
    ∀ T, Le K O.toCone (K.add (K.add (e T) (e T)) (sumN K D T))
      (K.add (K.add (e 0) (e 0)) (sumN K (fun t => K.mul Kc (K.mul (e t) (e t))) T))
  | 0 => ⟨K.zero, O.nn_zero, by show K.add (K.add (K.add (e 0) (e 0)) K.zero) K.zero = K.add (K.add (e 0) (e 0)) K.zero; lib_ring⟩
  | T + 1 => by
    have ih := tele_budget e D Kc st T
    have s := le_add' K O.toCone _ _ _ _ (st T) (le_refl' K O.toCone (sumN K D T))
    have s2 := le_add' K O.toCone _ _ _ _ ih (le_refl' K O.toCone (K.mul Kc (K.mul (e T) (e T))))
    -- combine: 2e_{T+1} + Σ_{T+1} D = (2e_{T+1} + D_T) + Σ_T D ≤ (2e_T + K e_T²) + Σ_T D ≤ 2e_0 + Σ_{T+1} K e²
    have e1 : K.add (K.add (e (T + 1)) (e (T + 1))) (K.add (sumN K D T) (D T))
        = K.add (K.add (K.add (e (T + 1)) (e (T + 1))) (D T)) (sumN K D T) := by lib_ring
    have e2 : K.add (K.add (K.add (e T) (e T)) (K.mul Kc (K.mul (e T) (e T)))) (sumN K D T)
        = K.add (K.add (K.add (e T) (e T)) (sumN K D T)) (K.mul Kc (K.mul (e T) (e T))) := by lib_ring
    show Le K O.toCone (K.add (K.add (e (T + 1)) (e (T + 1))) (K.add (sumN K D T) (D T)))
      (K.add (K.add (e 0) (e 0)) (K.add (sumN K (fun t => K.mul Kc (K.mul (e t) (e t))) T) (K.mul Kc (K.mul (e T) (e T)))))
    rw [e1]
    rw [e2] at s
    apply le_trans' K O.toCone _ _ _ s
    apply le_trans' K O.toCone _ _ _ s2
    exact ⟨K.zero, O.nn_zero, by lib_ring⟩

theorem budget3 (e D : Nat → R) (Kc : R) (T : Nat) (hK : O.nn Kc) (he : ∀ t, O.nn (e t))
    (st : ∀ t, Le K O.toCone (K.add (K.add (e (t + 1)) (e (t + 1))) (D t)) (K.add (K.add (e t) (e t)) (K.mul Kc (K.mul (e t) (e t)))))
    (b2 : ∀ t, t < T → Le K O.toCone (e t) (K.add (e 0) (e 0)))
    (small : Le K O.toCone (K.mul (castN K (4 * T)) (K.mul Kc (e 0))) K.one) :
    Le K O.toCone (sumN K D T) (K.add (K.add (e 0) (e 0)) (e 0)) := by
  have tb := tele_budget K O e D Kc st T
  -- Σ_{t<T} K e_t² ≤ T·K·(2e_0)² = 4T K e_0² ≤ e_0
  have sq4 : ∀ t, t < T → Le K O.toCone (K.mul Kc (K.mul (e t) (e t))) (K.mul Kc (K.mul (K.add (e 0) (e 0)) (K.add (e 0) (e 0)))) :=
    fun t ht => le_mul_nn K O.toCone Kc _ _ hK (sq_le K O _ _ (he t) (b2 t ht))
  have sumB : ∀ M, M ≤ T → Le K O.toCone (sumN K (fun t => K.mul Kc (K.mul (e t) (e t))) M)
      (K.mul (castN K M) (K.mul Kc (K.mul (K.add (e 0) (e 0)) (K.add (e 0) (e 0))))) := by
    intro M
    induction M with
    | zero => intro _; exact ⟨K.zero, O.nn_zero, by show K.add K.zero K.zero = K.mul K.zero _; lib_ring⟩
    | succ M ih =>
      intro hM
      have a := le_add' K O.toCone _ _ _ _ (ih (Nat.le_of_succ_le hM)) (sq4 M (Nat.lt_of_succ_le hM))
      apply le_trans' K O.toCone _ _ _ a
      exact ⟨K.zero, O.nn_zero, by show _ = K.mul (K.add K.one (castN K M)) _; lib_ring⟩
  have sB := sumB T (Nat.le_refl T)
  -- T·K·4e_0² = (4T)·(K e_0)·e_0 ≤ 1·e_0
  have c1 : Le K O.toCone (K.mul (castN K T) (K.mul Kc (K.mul (K.add (e 0) (e 0)) (K.add (e 0) (e 0)))))
      (K.mul (K.mul (castN K (4 * T)) (K.mul Kc (e 0))) (e 0)) := by
    refine ⟨K.zero, O.nn_zero, ?_⟩
    have : castN K (4 * T) = K.mul (castN K 4) (castN K T) := by
      clear sB sumB sq4 tb small b2 st he
      induction T with
      | zero => show K.zero = K.mul _ K.zero; lib_ring
      | succ T ih =>
        rw [show 4 * (T + 1) = 4 * T + 4 by rfl, cast_add K (4 * T) 4, ih]
        show _ = K.mul (castN K 4) (K.add K.one (castN K T))
        show K.add (K.mul (castN K 4) (castN K T)) (K.add K.one (K.add K.one (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 (castN K T))
        lib_ring
    rw [this]
    show _ = K.mul (K.mul (K.mul (K.add K.one (K.add K.one (K.add K.one (K.add K.one K.zero)))) (castN K T)) (K.mul Kc (e 0))) (e 0)
    lib_ring
  have c2 := le_mul_rg_e (e 0) (he 0) small
  have tot := le_trans' K O.toCone _ _ _ sB (le_trans' K O.toCone _ _ _ c1 c2)
  have tb2 := le_trans' K O.toCone _ _ _ tb (le_add' K O.toCone _ _ _ _ (le_refl' K O.toCone _) tot)
  -- drop 2e_T ≥ 0
  have hT : O.nn (K.add (e T) (e T)) := O.nn_add _ _ (he T) (he T)
  cases tb2 with
  | intro d hd =>
    refine ⟨K.add d (K.add (e T) (e T)), O.nn_add _ _ hd.1 hT, ?_⟩
    have : K.add (sumN K D T) (K.add d (K.add (e T) (e T))) = K.add (K.add (K.add (e T) (e T)) (sumN K D T)) d := by lib_ring
    rw [this, hd.2]; lib_ring
where
  le_mul_rg_e (y : R) (hy : O.nn y) {a b : R} (h : Le K O.toCone a b) : Le K O.toCone (K.mul a y) (K.mul b y) := by
    cases h with
    | intro d hd => exact ⟨K.mul d y, O.nn_mul _ _ hd.1 hy, by rw [← hd.2]; lib_ring⟩

end Gen

end SemiBudget

#print axioms SemiBudget.tele_budget
#print axioms SemiBudget.budget3

ns/SemiBudgetArr.lean

lean_all/ns/SemiBudgetArr.lean

/-
  ns/SemiBudgetArr.lean — T37 cycle 868 (goal G577): the dissipation budget of the semi-implicit scheme for any
  energy-neutral nonlinearity bounded by C·E² (arrays a_t(n), n < N; every Galerkin truncation), over any ordered
  ring without zero divisors (ℚ included): if 4T·h²·C·E_0 ≤ 1 then
    Σ_{t<T} Σ_{n<N} x_n (a_{t+1}(n) + a_t(n))² ≤ 3E_0
  — the discrete ν∫‖∇u‖² dt ≤ (3/2)‖u_0‖² for the scheme with an explicit next state (SemiGeneral).

  Lean 4 core plus ns/SemiGlobal (semi_global, halve), ns/SemiBound (semi_bound), ns/SemiBudget (budget3).
-/
import ns.SemiGlobal
import ns.SemiBudget

namespace SemiBudgetArr
open DetChain NSChain DyadicSmallGlobal LibRing DyadicLarge SemiBound SemiGlobal SemiBudget

theorem semi_budget {R : Type} (K : CRing R) (O : OrdR K) (h10 : K.one ≠ K.zero)
    (a : Nat → Nat → R) (x : Nat → R) (Nn : Nat → Nat → R) (h C : R) (N T : Nat) (hx : ∀ n, O.nn (x n)) (hC : O.nn C)
    (st : ∀ t n, K.add (a (t + 1) n) (K.neg (a t n)) = K.add (K.neg (K.mul (x n) (K.add (a (t + 1) n) (a t n))))
      (K.mul h (Nn t n)))
    (neutral : ∀ t, sumN K (fun n => K.mul (a t n) (Nn t n)) N = K.zero)
    (nb : ∀ t, Le K O.toCone (sumN K (fun n => K.mul (K.add (x n) (K.add K.one K.one)) (K.mul (Nn t n) (Nn t n))) N)
      (K.mul C (K.mul (sumN K (fun n => K.mul (a t n) (a t n)) N) (sumN K (fun n => K.mul (a t n) (a t n)) N))))
    (small : Le K O.toCone (K.mul (castN K (4 * T)) (K.mul (K.mul (K.mul h h) C) (sumN K (fun n => K.mul (a 0 n) (a 0 n)) N)))
      K.one) :
    Le K O.toCone (sumN K (fun t => sumN K (fun n => K.mul (x n) (K.mul (K.add (a (t + 1) n) (a t n)) (K.add (a (t + 1) n) (a t n)))) N) T)
      (K.add (K.add (sumN K (fun n => K.mul (a 0 n) (a 0 n)) N) (sumN K (fun n => K.mul (a 0 n) (a 0 n)) N))
        (sumN K (fun n => K.mul (a 0 n) (a 0 n)) N)) := by
  have hE : ∀ t, O.nn (sumN K (fun n => K.mul (a t n) (a t n)) N) := fun t => sq_sum_nonneg K O.toCone (a t) N
  have step : ∀ t, Le K O.toCone (K.add (K.add (sumN K (fun n => K.mul (a (t + 1) n) (a (t + 1) n)) N)
        (sumN K (fun n => K.mul (a (t + 1) n) (a (t + 1) n)) N))
      (sumN K (fun n => K.mul (x n) (K.mul (K.add (a (t + 1) n) (a t n)) (K.add (a (t + 1) n) (a t n)))) N))
      (K.add (K.add (sumN K (fun n => K.mul (a t n) (a t n)) N) (sumN K (fun n => K.mul (a t n) (a t n)) N))
        (K.mul (K.mul (K.mul h h) C) (K.mul (sumN K (fun n => K.mul (a t n) (a t n)) N) (sumN K (fun n => K.mul (a t n) (a t n)) N)))) := by
    intro t
    have sb := semi_bound K O.toCone (a t) (a (t + 1)) x (Nn t) h N hx (st t) (neutral t)
    have d2 := le_mul_nn K O.toCone (K.mul h h) _ _ (O.nn_sq h) (nb t)
    have d3 := le_trans' K O.toCone _ _ _ sb (le_add' K O.toCone _ _ _ _ (le_refl' K O.toCone _) d2)
    cases d3 with
    | intro d hd => exact ⟨d, hd.1, by unfold SemiBound.two at hd; exact hd.2.trans (by lib_ring)⟩
  exact budget3 K O (fun t => sumN K (fun n => K.mul (a t n) (a t n)) N) _ (K.mul (K.mul h h) C) T (O.nn_mul _ _ (O.nn_sq h) hC) hE step
    (fun t ht => semi_global K O h10 a x Nn h C N T hx hC st neutral nb small t (Nat.le_of_lt ht)) small

end SemiBudgetArr

#print axioms SemiBudgetArr.semi_budget

ns/SemiDyadic.lean

lean_all/ns/SemiDyadic.lean

/-
  ns/SemiDyadic.lean — T37 cycle 803 (goal G515 = G512b): the semi-implicit dyadic scheme over an ordered field
  (ℚ included) — an explicit solution and its energy bound. For data b (b_0 = 0, b_i = 0 for i > N), x_j ≥ 0
  (x_j = hνk_{j+1}), the sequence
    a_0 = b,   a_{t+1}(j+1) = ((1 − x_j)·a_t(j+1) + h·dyN(c, a_t)_j)·(1 + x_j)⁻¹   (j < N),   0 elsewhere
  solves a_{t+1} − a_t = −x(a_{t+1} + a_t) + h·dyN(c, a_t) on the modes 1..N (ForcedCN), and if
  4T·h²·C_N·E_0 ≤ 1 (C_N of DyCN) then E_t ≤ 2E_0 for every t ≤ T (SemiGlobal).

  Lean 4 core plus ns/SemiGlobal, ns/DyCN, ns/ForcedCN, ns/DyUnique (sumN_ext_g), NSChain (dyadic_energy_neutral).
    sq           the sequence;  sq_zero0, sq_zeroN  boundary zeros;  sq_step  the scheme on modes 1..N
    semi_dyadic  the energy bound
-/
import ns.SemiGlobal
import ns.DyCN
import ns.ForcedCN

namespace SemiDyadic
open DetChain NSChain DyadicSmallGlobal LibRing DyadicLarge GoodModesQ RatModel ForcedCN

section Gen
variable {R : Type} (K : CRing R)

def upd (inv : R → R) (c x : Nat → R) (h : R) (N : Nat) (a : Nat → R) : Nat → R
  | 0 => K.zero
  | j + 1 => match Nat.ble (j + 1) N with
    | true => K.mul (K.add (K.mul (K.add K.one (K.neg (x j))) (a (j + 1))) (K.mul h (dyN K c a j))) (inv (K.add K.one (x j)))
    | false => K.zero

def sq (inv : R → R) (c x : Nat → R) (h : R) (N : Nat) (b : Nat → R) : Nat → Nat → R
  | 0 => b
  | t + 1 => upd K inv c x h N (sq inv c x h N b t)

theorem upd_in (inv : R → R) (c x : Nat → R) (h : R) (N : Nat) (a : Nat → R) (j : Nat) (hj : j < N) :
    upd K inv c x h N a (j + 1)
      = K.mul (K.add (K.mul (K.add K.one (K.neg (x j))) (a (j + 1))) (K.mul h (dyN K c a j))) (inv (K.add K.one (x j))) := by
  show (match Nat.ble (j + 1) N with
    | true => K.mul (K.add (K.mul (K.add K.one (K.neg (x j))) (a (j + 1))) (K.mul h (dyN K c a j))) (inv (K.add K.one (x j)))
    | false => K.zero) = _
  rw [Nat.ble_eq_true_of_le hj]

theorem upd_out (inv : R → R) (c x : Nat → R) (h : R) (N : Nat) (a : Nat → R) (j : Nat) (hj : N ≤ j) :
    upd K inv c x h N a (j + 1) = K.zero := by
  show (match Nat.ble (j + 1) N with
    | true => K.mul (K.add (K.mul (K.add K.one (K.neg (x j))) (a (j + 1))) (K.mul h (dyN K c a j))) (inv (K.add K.one (x j)))
    | false => K.zero) = _
  cases e : Nat.ble (j + 1) N with
  | false => rfl
  | true => exact absurd (Nat.le_of_ble_eq_true e) (Nat.not_le_of_lt (Nat.lt_succ_of_le hj))

theorem sq_zero0 (inv : R → R) (c x : Nat → R) (h : R) (N : Nat) (b : Nat → R) (hb0 : b 0 = K.zero) :
    ∀ t, sq K inv c x h N b t 0 = K.zero
  | 0 => hb0
  | _ + 1 => rfl

theorem sq_zeroN (inv : R → R) (c x : Nat → R) (h : R) (N : Nat) (b : Nat → R) (hbN : ∀ i, N + 1 ≤ i → b i = K.zero) :
    ∀ t i, N + 1 ≤ i → sq K inv c x h N b t i = K.zero
  | 0, i, hi => hbN i hi
  | t + 1, i, hi => by
    cases i with
    | zero => rfl
    | succ j => exact upd_out K inv c x h N _ j (Nat.le_of_succ_le_succ hi)

end Gen

theorem semi_dyadic {R : Type} (K : CRing R) (O : OrdR K) (inv : R → R)
    (mul_inv : ∀ y, y ≠ K.zero → K.mul y (inv y) = K.one) (h10 : K.one ≠ K.zero)
    (c x : Nat → R) (h : R) (N T : Nat) (b : Nat → R) (hx : ∀ n, O.nn (x n))
    (hb0 : b 0 = K.zero) (hbN : ∀ i, N + 1 ≤ i → b i = K.zero)
    (small : Le K O.toCone (K.mul (castN K (4 * T)) (K.mul (K.mul (K.mul h h)
        (sumN K (fun n => K.mul (K.add (x n) (K.add K.one K.one))
          (K.add (SemiBound.two K (K.mul (c n) (c n))) (SemiBound.two K (K.mul (c (n + 1)) (c (n + 1)))))) N))
        (sumN K (fun n => K.mul (b (n + 1)) (b (n + 1))) N))) K.one) :
    ∀ t, t ≤ T → Le K O.toCone (sumN K (fun n => K.mul (sq K inv c x h N b t (n + 1)) (sq K inv c x h N b t (n + 1))) N)
      (K.add (sumN K (fun n => K.mul (b (n + 1)) (b (n + 1))) N) (sumN K (fun n => K.mul (b (n + 1)) (b (n + 1))) N)) := by
  let A : Nat → Nat → R := fun t n => sq K inv c x h N b t (n + 1)
  let Nn : Nat → Nat → R := fun t n => match Nat.ble (n + 1) N with
    | true => dyN K c (sq K inv c x h N b t) n
    | false => K.zero
  let C := sumN K (fun n => K.mul (K.add (x n) (K.add K.one K.one))
    (K.add (SemiBound.two K (K.mul (c n) (c n))) (SemiBound.two K (K.mul (c (n + 1)) (c (n + 1)))))) N
  have hC : O.nn C := sumN_nn K O.toCone _ (fun n => O.nn_mul _ _ (O.nn_add _ _ (hx n)
    (O.nn_add _ _ (nn_one' K O.toCone) (nn_one' K O.toCone)))
    (O.nn_add _ _ (O.nn_add _ _ (O.nn_sq _) (O.nn_sq _)) (O.nn_add _ _ (O.nn_sq _) (O.nn_sq _)))) N
  have st : ∀ t n, K.add (A (t + 1) n) (K.neg (A t n)) = K.add (K.neg (K.mul (x n) (K.add (A (t + 1) n) (A t n))))
      (K.mul h (Nn t n)) := by
    intro t n
    show K.add (upd K inv c x h N (sq K inv c x h N b t) (n + 1)) (K.neg (sq K inv c x h N b t (n + 1)))
      = K.add (K.neg (K.mul (x n) (K.add (upd K inv c x h N (sq K inv c x h N b t) (n + 1)) (sq K inv c x h N b t (n + 1)))))
        (K.mul h (match Nat.ble (n + 1) N with
          | true => dyN K c (sq K inv c x h N b t) n
          | false => K.zero))
    cases Nat.lt_or_ge n N with
    | inl hn =>
      have m1 : (match Nat.ble (n + 1) N with
          | true => dyN K c (sq K inv c x h N b t) n
          | false => K.zero) = dyN K c (sq K inv c x h N b t) n := by rw [Nat.ble_eq_true_of_le hn]
      rw [m1, upd_in K inv c x h N _ n hn]
      exact forced_exist K O inv mul_inv h10 (sq K inv c x h N b t (n + 1)) (x n)
        (K.mul h (dyN K c (sq K inv c x h N b t) n)) (hx n)
    | inr hn =>
      have m0 : (match Nat.ble (n + 1) N with
          | true => dyN K c (sq K inv c x h N b t) n
          | false => K.zero) = K.zero := by
        cases e : Nat.ble (n + 1) N with
        | false => rfl
        | true => exact absurd (Nat.le_of_ble_eq_true e) (Nat.not_le_of_lt (Nat.lt_succ_of_le hn))
      have z : sq K inv c x h N b t (n + 1) = K.zero :=
        sq_zeroN K inv c x h N b hbN t (n + 1) (Nat.succ_le_succ hn)
      rw [m0, upd_out K inv c x h N _ n hn, z]; lib_ring
  have inner : ∀ t n, n < N → Nn t n = dyN K c (sq K inv c x h N b t) n := by
    intro t n hn
    show (match Nat.ble (n + 1) N with
      | true => dyN K c (sq K inv c x h N b t) n
      | false => K.zero) = _
    rw [Nat.ble_eq_true_of_le hn]
  have neutral : ∀ t, sumN K (fun n => K.mul (A t n) (Nn t n)) N = K.zero := by
    intro t
    rw [DyUnique.sumN_ext_g K _ (fun n => K.mul (sq K inv c x h N b t (n + 1)) (dyN K c (sq K inv c x h N b t) n)) N
      (fun n hn => by show K.mul (A t n) (Nn t n) = _; rw [inner t n hn])]
    exact dyadic_energy_neutral K c (sq K inv c x h N b t) N (sq_zero0 K inv c x h N b hb0 t)
      (sq_zeroN K inv c x h N b hbN t (N + 1) (Nat.le_refl _))
  have nb : ∀ t, Le K O.toCone (sumN K (fun n => K.mul (K.add (x n) (K.add K.one K.one)) (K.mul (Nn t n) (Nn t n))) N)
      (K.mul C (K.mul (sumN K (fun n => K.mul (A t n) (A t n)) N) (sumN K (fun n => K.mul (A t n) (A t n)) N))) := by
    intro t
    rw [DyUnique.sumN_ext_g K _ (fun n => K.mul (K.add (x n) (K.add K.one K.one))
      (K.mul (dyN K c (sq K inv c x h N b t) n) (dyN K c (sq K inv c x h N b t) n))) N
      (fun n hn => by show K.mul _ (K.mul (Nn t n) (Nn t n)) = _; rw [inner t n hn])]
    exact DyCN.dy_cn K O.toCone (sq K inv c x h N b t) c x N hx (sq_zero0 K inv c x h N b hb0 t)
      (sq_zeroN K inv c x h N b hbN t)
  exact SemiGlobal.semi_global K O h10 A x Nn h C N T hx hC st neutral nb small

end SemiDyadic

#print axioms SemiDyadic.upd_in
#print axioms SemiDyadic.upd_out
#print axioms SemiDyadic.sq_zero0
#print axioms SemiDyadic.sq_zeroN
#print axioms SemiDyadic.semi_dyadic

ns/SemiEnergy.lean

lean_all/ns/SemiEnergy.lean

/-
  ns/SemiEnergy.lean — T37 cycle 789 (goal G500): the exact energy identity of the semi-implicit scheme
  (dissipation implicit, nonlinearity explicit) over any commutative ring:
    a'_n − a_n = −x_n (a'_n + a_n) + h N_n,   Σ_{n<N} a_n N_n = 0  (neutrality at the old state)
  ⇒ E' + Σ x_n (a'_n + a_n)² = E + h Σ (a'_n − a_n) N_n.
  The defect h Σ (a' − a) N is O(h²) (a' − a = O(h)); with it bounded, the large-data argument carries over.

  Lean 4 core plus NSChain (sumN), lib/RingTac (lib_ring).
    step_energy   one mode:  a'² − a² + x(a'+a)² = h (a' − a) N + 2 h a N
    semi_energy   the summed identity
-/
import NSChain
import lib.RingTac

namespace SemiEnergy
open DetChain NSChain LibRing

section Gen
variable {R : Type} (K : CRing R)

theorem step_energy (a a' x hN : R) (st : K.add a' (K.neg a) = K.add (K.neg (K.mul x (K.add a' a))) hN) :
    K.add (K.mul a' a') (K.mul x (K.mul (K.add a' a) (K.add a' a)))
      = K.add (K.add (K.mul a a) (K.mul (K.add a' (K.neg a)) hN)) (K.add (K.mul a hN) (K.mul a hN)) := by
  -- (a' − a)(a' + a) = −x s² + hN·s, and hN·s = hN(a' − a) + 2 a hN
  have t : K.add (K.mul a' a') (K.mul x (K.mul (K.add a' a) (K.add a' a)))
      = K.add (K.add (K.mul a a) (K.mul (K.add a' (K.neg a)) (K.add a' a)))
        (K.mul x (K.mul (K.add a' a) (K.add a' a))) := by lib_ring
  have z : K.mul (K.add a' (K.neg a)) (K.add a' a)
      = K.add (K.neg (K.mul x (K.mul (K.add a' a) (K.add a' a)))) (K.mul hN (K.add a' a)) := by
    have u : K.mul (K.add a' (K.neg a)) (K.add a' a)
        = K.add (K.mul (K.add (K.neg (K.mul x (K.add a' a))) hN) (K.add a' a))
          (K.mul (K.add (K.add a' (K.neg a)) (K.neg (K.add (K.neg (K.mul x (K.add a' a))) hN))) (K.add a' a)) := by lib_ring
    exact u.trans (by rw [st]; lib_ring)
  rw [t, z]
  have w : K.mul hN (K.add a' a) = K.add (K.mul (K.add a' (K.neg a)) hN) (K.add (K.mul a hN) (K.mul a hN)) := by lib_ring
  rw [w]; lib_ring

theorem semi_energy (a a' x Nn : Nat → R) (h : R)
    (st : ∀ n, K.add (a' n) (K.neg (a n)) = K.add (K.neg (K.mul (x n) (K.add (a' n) (a n)))) (K.mul h (Nn n)))
    (N : Nat) (neutral : sumN K (fun n => K.mul (a n) (Nn n)) N = K.zero) :
    K.add (sumN K (fun n => K.mul (a' n) (a' n)) N) (sumN K (fun n => K.mul (x n) (K.mul (K.add (a' n) (a n)) (K.add (a' n) (a n)))) N)
      = K.add (sumN K (fun n => K.mul (a n) (a n)) N)
          (K.mul h (sumN K (fun n => K.mul (K.add (a' n) (K.neg (a n))) (Nn n)) N)) := by
  have gen : ∀ M, K.add (sumN K (fun n => K.mul (a' n) (a' n)) M)
      (sumN K (fun n => K.mul (x n) (K.mul (K.add (a' n) (a n)) (K.add (a' n) (a n)))) M)
      = K.add (K.add (sumN K (fun n => K.mul (a n) (a n)) M)
          (K.mul h (sumN K (fun n => K.mul (K.add (a' n) (K.neg (a n))) (Nn n)) M)))
        (K.mul (K.add h h) (sumN K (fun n => K.mul (a n) (Nn n)) M)) := by
    intro M
    induction M with
    | zero => show K.add K.zero K.zero = K.add (K.add K.zero (K.mul h K.zero)) (K.mul (K.add h h) K.zero); lib_ring
    | succ M ih =>
      have e := step_energy K (a M) (a' M) (x M) (K.mul h (Nn M)) (st M)
      show K.add (K.add (sumN K (fun n => K.mul (a' n) (a' n)) M) (K.mul (a' M) (a' M)))
          (K.add (sumN K (fun n => K.mul (x n) (K.mul (K.add (a' n) (a n)) (K.add (a' n) (a n)))) M)
            (K.mul (x M) (K.mul (K.add (a' M) (a M)) (K.add (a' M) (a M)))))
        = K.add (K.add (K.add (sumN K (fun n => K.mul (a n) (a n)) M) (K.mul (a M) (a M)))
            (K.mul h (K.add (sumN K (fun n => K.mul (K.add (a' n) (K.neg (a n))) (Nn n)) M)
              (K.mul (K.add (a' M) (K.neg (a M))) (Nn M)))))
          (K.mul (K.add h h) (K.add (sumN K (fun n => K.mul (a n) (Nn n)) M) (K.mul (a M) (Nn M))))
      have r : K.add (K.add (sumN K (fun n => K.mul (a' n) (a' n)) M) (K.mul (a' M) (a' M)))
          (K.add (sumN K (fun n => K.mul (x n) (K.mul (K.add (a' n) (a n)) (K.add (a' n) (a n)))) M)
            (K.mul (x M) (K.mul (K.add (a' M) (a M)) (K.add (a' M) (a M)))))
          = K.add (K.add (sumN K (fun n => K.mul (a' n) (a' n)) M)
              (sumN K (fun n => K.mul (x n) (K.mul (K.add (a' n) (a n)) (K.add (a' n) (a n)))) M))
            (K.add (K.mul (a' M) (a' M)) (K.mul (x M) (K.mul (K.add (a' M) (a M)) (K.add (a' M) (a M))))) := by lib_ring
      rw [r, ih, e]; lib_ring
  rw [gen, neutral]; lib_ring

end Gen

end SemiEnergy

#print axioms SemiEnergy.step_energy
#print axioms SemiEnergy.semi_energy

ns/SemiGeneral.lean

lean_all/ns/SemiGeneral.lean

/-
  ns/SemiGeneral.lean — T37 cycle 862 (goal G571): the semi-implicit scheme for ANY energy-neutral nonlinearity over
  an ordered field (ℚ included) — covering every Galerkin truncation, in particular 3D Fourier–Galerkin (neutral by
  the triad antisymmetry, FourierGalerkin.neutral), once flattened to scalar modes n < N. Given
    Nf : (Nat → R) → Nat → R   with   Σ_{n<N} a_n Nf(a)_n = 0   and   Σ_{n<N} (x_n + 2) Nf(a)_n² ≤ C·E(a)²
  for every state a (E(a) = Σ_{n<N} a_n²), the sequence
    a_{t+1}(n) = ((1 − x_n) a_t(n) + h Nf(a_t)_n)·(1 + x_n)⁻¹   (n < N),   0 otherwise
  solves the semi-implicit step on n < N, and E_t ≤ 2E_0 for t ≤ T whenever 4T·h²·C·E_0 ≤ 1.

  Lean 4 core plus ns/SemiGlobal (semi_global), ns/ForcedCN (forced_exist), ns/DyUnique (sumN_ext_g).
-/
import ns.SemiGlobal
import ns.ForcedCN
import ns.DyUnique

namespace SemiGeneral
open DetChain NSChain DyadicSmallGlobal LibRing DyadicLarge GoodModesQ RatModel ForcedCN

section Gen
variable {R : Type} (K : CRing R)

def upd (inv : R → R) (Nf : (Nat → R) → Nat → R) (x : Nat → R) (h : R) (N : Nat) (a : Nat → R) (n : Nat) : R :=
  match Nat.ble (n + 1) N with
  | true => K.mul (K.add (K.mul (K.add K.one (K.neg (x n))) (a n)) (K.mul h (Nf a n))) (inv (K.add K.one (x n)))
  | false => K.zero

def sq (inv : R → R) (Nf : (Nat → R) → Nat → R) (x : Nat → R) (h : R) (N : Nat) (b : Nat → R) : Nat → Nat → R
  | 0 => b
  | t + 1 => upd K inv Nf x h N (sq inv Nf x h N b t)

theorem upd_in (inv : R → R) (Nf : (Nat → R) → Nat → R) (x : Nat → R) (h : R) (N : Nat) (a : Nat → R) (n : Nat)
    (hn : n < N) :
    upd K inv Nf x h N a n = K.mul (K.add (K.mul (K.add K.one (K.neg (x n))) (a n)) (K.mul h (Nf a n))) (inv (K.add K.one (x n))) := by
  show (match Nat.ble (n + 1) N with
    | true => K.mul (K.add (K.mul (K.add K.one (K.neg (x n))) (a n)) (K.mul h (Nf a n))) (inv (K.add K.one (x n)))
    | false => K.zero) = _
  rw [Nat.ble_eq_true_of_le hn]

end Gen

theorem semi_general {R : Type} (K : CRing R) (O : OrdR K) (inv : R → R)
    (mul_inv : ∀ y, y ≠ K.zero → K.mul y (inv y) = K.one) (h10 : K.one ≠ K.zero)
    (Nf : (Nat → R) → Nat → R) (x : Nat → R) (h C : R) (N T : Nat) (b : Nat → R) (hx : ∀ n, O.nn (x n)) (hC : O.nn C)
    (neutral : ∀ a : Nat → R, sumN K (fun n => K.mul (a n) (Nf a n)) N = K.zero)
    (nb : ∀ a : Nat → R, Le K O.toCone (sumN K (fun n => K.mul (K.add (x n) (K.add K.one K.one)) (K.mul (Nf a n) (Nf a n))) N)
      (K.mul C (K.mul (sumN K (fun n => K.mul (a n) (a n)) N) (sumN K (fun n => K.mul (a n) (a n)) N))))
    (small : Le K O.toCone (K.mul (castN K (4 * T)) (K.mul (K.mul (K.mul h h) C) (sumN K (fun n => K.mul (b n) (b n)) N))) K.one) :
    ∀ t, t ≤ T → Le K O.toCone (sumN K (fun n => K.mul (sq K inv Nf x h N b t n) (sq K inv Nf x h N b t n)) N)
      (K.add (sumN K (fun n => K.mul (b n) (b n)) N) (sumN K (fun n => K.mul (b n) (b n)) N)) := by
  let A : Nat → Nat → R := fun t n => match Nat.ble (n + 1) N with
    | true => sq K inv Nf x h N b t n
    | false => K.zero
  let Nn : Nat → Nat → R := fun t n => match Nat.ble (n + 1) N with
    | true => Nf (sq K inv Nf x h N b t) n
    | false => K.zero
  have inA : ∀ t n, n < N → A t n = sq K inv Nf x h N b t n := by
    intro t n hn; show (match Nat.ble (n + 1) N with | true => _ | false => _) = _; rw [Nat.ble_eq_true_of_le hn]
  have inN : ∀ t n, n < N → Nn t n = Nf (sq K inv Nf x h N b t) n := by
    intro t n hn; show (match Nat.ble (n + 1) N with | true => _ | false => _) = _; rw [Nat.ble_eq_true_of_le hn]
  have outA : ∀ t n, N ≤ n → A t n = K.zero := by
    intro t n hn; show (match Nat.ble (n + 1) N with | true => _ | false => _) = _
    cases e : Nat.ble (n + 1) N with
    | false => rfl
    | true => exact absurd (Nat.le_of_ble_eq_true e) (Nat.not_le_of_lt (Nat.lt_succ_of_le hn))
  have outN : ∀ t n, N ≤ n → Nn t n = K.zero := by
    intro t n hn; show (match Nat.ble (n + 1) N with | true => _ | false => _) = _
    cases e : Nat.ble (n + 1) N with
    | false => rfl
    | true => exact absurd (Nat.le_of_ble_eq_true e) (Nat.not_le_of_lt (Nat.lt_succ_of_le hn))
  have sumA : ∀ t, sumN K (fun n => K.mul (A t n) (A t n)) N = sumN K (fun n => K.mul (sq K inv Nf x h N b t n) (sq K inv Nf x h N b t n)) N :=
    fun t => DyUnique.sumN_ext_g K _ _ N (fun n hn => by rw [inA t n hn])
  have st : ∀ t n, K.add (A (t + 1) n) (K.neg (A t n)) = K.add (K.neg (K.mul (x n) (K.add (A (t + 1) n) (A t n)))) (K.mul h (Nn t n)) := by
    intro t n
    cases Nat.lt_or_ge n N with
    | inl hn =>
      rw [inA (t + 1) n hn, inA t n hn, inN t n hn]
      show K.add (upd K inv Nf x h N (sq K inv Nf x h N b t) n) (K.neg (sq K inv Nf x h N b t n))
        = K.add (K.neg (K.mul (x n) (K.add (upd K inv Nf x h N (sq K inv Nf x h N b t) n) (sq K inv Nf x h N b t n))))
          (K.mul h (Nf (sq K inv Nf x h N b t) n))
      rw [upd_in K inv Nf x h N _ n hn]
      exact forced_exist K O inv mul_inv h10 (sq K inv Nf x h N b t n) (x n) (K.mul h (Nf (sq K inv Nf x h N b t) n)) (hx n)
    | inr hn =>
      rw [outA (t + 1) n hn, outA t n hn, outN t n hn]; lib_ring
  have neu : ∀ t, sumN K (fun n => K.mul (A t n) (Nn t n)) N = K.zero := by
    intro t
    rw [DyUnique.sumN_ext_g K _ (fun n => K.mul (sq K inv Nf x h N b t n) (Nf (sq K inv Nf x h N b t) n)) N
      (fun n hn => by rw [inA t n hn, inN t n hn])]
    exact neutral _
  have nbt : ∀ t, Le K O.toCone (sumN K (fun n => K.mul (K.add (x n) (K.add K.one K.one)) (K.mul (Nn t n) (Nn t n))) N)
      (K.mul C (K.mul (sumN K (fun n => K.mul (A t n) (A t n)) N) (sumN K (fun n => K.mul (A t n) (A t n)) N))) := by
    intro t
    rw [sumA t, DyUnique.sumN_ext_g K _ (fun n => K.mul (K.add (x n) (K.add K.one K.one))
      (K.mul (Nf (sq K inv Nf x h N b t) n) (Nf (sq K inv Nf x h N b t) n))) N (fun n hn => by rw [inN t n hn])]
    exact nb _
  have small' : Le K O.toCone (K.mul (castN K (4 * T)) (K.mul (K.mul (K.mul h h) C) (sumN K (fun n => K.mul (A 0 n) (A 0 n)) N))) K.one := by
    rw [sumA 0]; exact small
  intro t ht
  have g := SemiGlobal.semi_global K O h10 A x Nn h C N T hx hC st neu nbt small' t ht
  rw [sumA t, sumA 0] at g
  exact g

end SemiGeneral

#print axioms SemiGeneral.upd_in
#print axioms SemiGeneral.semi_general

ns/SemiGlobal.lean

lean_all/ns/SemiGlobal.lean

/-
  ns/SemiGlobal.lean — T37 cycle 797 (goal G509): a global bound for the semi-implicit scheme over any ordered ring
  without zero divisors (ℚ included). If every step satisfies
    a_{t+1}(n) − a_t(n) = −x_n (a_{t+1}(n) + a_t(n)) + h N_t(n),  x_n ≥ 0,  Σ a_t N_t = 0,
    Σ_n (x_n + 2) N_t(n)² ≤ C·E_t²                     (nonlinearity controlled by the energy),
  and 4T·h²C·E_0 ≤ 1, then E_t ≤ 2E_0 for every t ≤ T. (For fixed truncation N the dyadic N satisfies the
  nonlinearity hypothesis with C = C_N by DyNBound and mode ≤ energy.)

  Lean 4 core plus ns/SemiBound (semi_bound), ns/RiccatiH (ric_horizon), ns/YoungM (pos_cancel), lib/RingTac.
    halve        2x ≤ 2y + w, w ≥ 0 ⇒ x ≤ y + w
    semi_global  the statement
-/
import ns.SemiBound
import ns.RiccatiH
import ns.YoungM

namespace SemiGlobal
open DetChain NSChain DyadicSmallGlobal LibRing DyadicLarge SemiBound RiccatiH YoungM GoodModesQ RatModel

section Gen
variable {R : Type} (K : CRing R) (O : OrdR K)
include O

theorem halve (h10 : K.one ≠ K.zero) (x y w : R) (hw : O.nn w)
    (h : Le K O.toCone (K.add x x) (K.add (K.add y y) w)) : Le K O.toCone x (K.add y w) := by
  cases h with
  | intro d hd =>
    -- 2(y + w − x) = d + w ≥ 0
    have e : K.mul (K.add K.one K.one) (K.add (K.add y w) (K.neg x)) = K.add d w := by
      have t : K.mul (K.add K.one K.one) (K.add (K.add y w) (K.neg x))
          = K.add (K.add d w) (K.add (K.add (K.add y y) w) (K.neg (K.add (K.add x x) d))) := by lib_ring
      exact t.trans (by rw [hd.2]; lib_ring)
    have hnn := pos_cancel K O (K.add K.one K.one) _ (O.nn_add _ _ (nn_one' K O.toCone) (nn_one' K O.toCone))
      (ne_of_one_add K O h10 K.one (nn_one' K O.toCone)) (by rw [e]; exact O.nn_add _ _ hd.1 hw)
    exact ⟨_, hnn, by lib_ring⟩

theorem semi_global (h10 : K.one ≠ K.zero) (a : Nat → Nat → R) (x : Nat → R) (Nn : Nat → Nat → R)
    (h C : R) (N T : Nat) (hx : ∀ n, O.nn (x n)) (hC : O.nn C)
    (st : ∀ t n, K.add (a (t + 1) n) (K.neg (a t n)) = K.add (K.neg (K.mul (x n) (K.add (a (t + 1) n) (a t n))))
      (K.mul h (Nn t n)))
    (neutral : ∀ t, sumN K (fun n => K.mul (a t n) (Nn t n)) N = K.zero)
    (nb : ∀ t, Le K O.toCone (sumN K (fun n => K.mul (K.add (x n) (K.add K.one K.one)) (K.mul (Nn t n) (Nn t n))) N)
      (K.mul C (K.mul (sumN K (fun n => K.mul (a t n) (a t n)) N) (sumN K (fun n => K.mul (a t n) (a t n)) N))))
    (small : Le K O.toCone (K.mul (castN K (4 * T)) (K.mul (K.mul (K.mul h h) C) (sumN K (fun n => K.mul (a 0 n) (a 0 n)) N)))
      K.one) :
    ∀ t, t ≤ T → Le K O.toCone (sumN K (fun n => K.mul (a t n) (a t n)) N)
      (K.add (sumN K (fun n => K.mul (a 0 n) (a 0 n)) N) (sumN K (fun n => K.mul (a 0 n) (a 0 n)) N)) := by
  let E : Nat → R := fun t => sumN K (fun n => K.mul (a t n) (a t n)) N
  have hE : ∀ t, O.nn (E t) := fun t => sq_sum_nonneg K O.toCone (a t) N
  have hrec : ∀ t, Le K O.toCone (E (t + 1)) (K.add (E t) (K.mul (K.mul (K.mul h h) C) (K.mul (E t) (E t)))) := by
    intro t
    have sb := semi_bound K O.toCone (a t) (a (t + 1)) x (Nn t) h N hx (st t) (neutral t)
    -- drop the dissipation and bound the nonlinearity
    have d0 : Le K O.toCone (K.add (E (t + 1)) (E (t + 1)))
        (K.add (two K (sumN K (fun n => K.mul (a (t + 1) n) (a (t + 1) n)) N))
          (sumN K (fun n => K.mul (x n) (K.mul (K.add (a (t + 1) n) (a t n)) (K.add (a (t + 1) n) (a t n)))) N)) :=
      ⟨_, sumN_nn K O.toCone _ (fun n => O.nn_mul _ _ (hx n) (O.nn_sq _)) N, rfl⟩
    have d1 := le_trans' K O.toCone _ _ _ d0 sb
    have d2 := le_mul_nn K O.toCone (K.mul h h) _ _ (O.nn_sq h) (nb t)
    have d3 := le_trans' K O.toCone _ _ _ d1 (le_add' K O.toCone _ _ _ _ (le_refl' K O.toCone _) d2)
    have w0 : O.nn (K.mul (K.mul (K.mul h h) C) (K.mul (E t) (E t))) :=
      O.nn_mul _ _ (O.nn_mul _ _ (O.nn_sq h) hC) (O.nn_sq _)
    apply halve K O h10 _ _ _ w0
    have e : K.add (two K (E t)) (K.mul (K.mul h h) (K.mul C (K.mul (E t) (E t))))
        = K.add (K.add (E t) (E t)) (K.mul (K.mul (K.mul h h) C) (K.mul (E t) (E t))) := by unfold two; lib_ring
    have d4 : Le K O.toCone (K.add (E (t + 1)) (E (t + 1)))
        (K.add (K.add (E t) (E t)) (K.mul (K.mul (K.mul h h) C) (K.mul (E t) (E t)))) := by
      rw [← e]; exact d3
    exact d4
  exact ric_horizon K O E (K.mul (K.mul h h) C) T (O.nn_mul _ _ (O.nn_sq h) hC) hE hrec small

end Gen

end SemiGlobal

#print axioms SemiGlobal.halve
#print axioms SemiGlobal.semi_global

ns/SemiStab.lean

lean_all/ns/SemiStab.lean

/-
  ns/SemiStab.lean — T37 cycle 820 (goal G531): one-step stability of the semi-implicit scheme in the data, per
  mode, over any ring with a positive cone (ℚ included): if a' and b' are the next states from a and b,
    a' − a = −x(a' + a) + h·Na,   b' − b = −x(b' + b) + h·Nb,   x ≥ 0,
  then d = a − b, d' = a' − b' satisfy
    (1 + x)²·d'² ≤ 2(1 + x)²·d² + 2h²(Na − Nb)².
  (The difference solves the same forced step with forcing Na − Nb; ForcedDamp.)

  Lean 4 core plus ns/ForcedDamp (forced_damp), lib/RingTac.
    diff_forced   d' − d = −x(d' + d) + h(Na − Nb)
    semi_stab     the inequality
-/
import ns.ForcedDamp

namespace SemiStab
open DetChain NSChain LibRing DyadicLarge MidRecover ForcedDamp

section Gen
variable {R : Type} (K : CRing R) (P : Cone K)

theorem diff_forced (a b a' b' x h Na Nb : R)
    (s1 : K.add a' (K.neg a) = K.add (K.neg (K.mul x (K.add a' a))) (K.mul h Na))
    (s2 : K.add b' (K.neg b) = K.add (K.neg (K.mul x (K.add b' b))) (K.mul h Nb)) :
    K.add (K.add a' (K.neg b')) (K.neg (K.add a (K.neg b)))
      = K.add (K.neg (K.mul x (K.add (K.add a' (K.neg b')) (K.add a (K.neg b))))) (K.mul h (K.add Na (K.neg Nb))) := by
  have t : K.add (K.add a' (K.neg b')) (K.neg (K.add a (K.neg b)))
      = K.add (K.add (K.neg (K.mul x (K.add (K.add a' (K.neg b')) (K.add a (K.neg b))))) (K.mul h (K.add Na (K.neg Nb))))
        (K.add (K.add (K.add a' (K.neg a)) (K.neg (K.add (K.neg (K.mul x (K.add a' a))) (K.mul h Na))))
          (K.neg (K.add (K.add b' (K.neg b)) (K.neg (K.add (K.neg (K.mul x (K.add b' b))) (K.mul h Nb)))))) := by lib_ring
  exact t.trans (by rw [s1, s2]; lib_ring)

theorem semi_stab (a b a' b' x h Na Nb : R) (hx : P.nn x)
    (s1 : K.add a' (K.neg a) = K.add (K.neg (K.mul x (K.add a' a))) (K.mul h Na))
    (s2 : K.add b' (K.neg b) = K.add (K.neg (K.mul x (K.add b' b))) (K.mul h Nb)) :
    Le K P (K.mul (K.mul (K.add K.one x) (K.add K.one x)) (K.mul (K.add a' (K.neg b')) (K.add a' (K.neg b'))))
      (K.add (two K (K.mul (K.mul (K.add K.one x) (K.add K.one x)) (K.mul (K.add a (K.neg b)) (K.add a (K.neg b)))))
        (two K (K.mul (K.mul h (K.add Na (K.neg Nb))) (K.mul h (K.add Na (K.neg Nb)))))) :=
  forced_damp K P (K.add a (K.neg b)) (K.add a' (K.neg b')) x (K.mul h (K.add Na (K.neg Nb))) hx
    (diff_forced K a b a' b' x h Na Nb s1 s2)

end Gen

end SemiStab

#print axioms SemiStab.diff_forced
#print axioms SemiStab.semi_stab

ns/SemiWDy.lean

lean_all/ns/SemiWDy.lean

/-
  ns/SemiWDy.lean — T37 cycle 823 (goal G534): the weighted semi-implicit step for the dyadic model, over any ring
  with a positive cone (ℚ included): SemiWStep with its rate hypothesis supplied by DyadicLarge.rate_bound
  (large-data head bound at the state a). With Δ_n = a'(n+1) − a(n+1), x_n = hνk_{n+1}:
    Σ w_{n+1}(a'(n+1)² − a(n+1)²) ≤ 2h·B_head + Σ w_{n+1} x_n Δ_n² + h Σ w_{n+1} Δ_n dyN(c, a)_n,
  B_head = Σ_{n<N} [n < n0] q_n E A  (the same head constant as in the CN large-data bound).

  Lean 4 core plus ns/SemiWStep (semi_wstep), ns/DyadicLarge (rate_bound).
-/
import ns.SemiWStep

namespace SemiWDy
open DetChain NSChain LibRing DyadicLarge SemiWStep

theorem semi_wdy {R : Type} (K : CRing R) (P : Cone K) (w c k a a' M : Nat → R) (h nu A E : R) (n0 N : Nat)
    (hw : ∀ n, P.nn (w n)) (hk : ∀ n, P.nn (k n)) (hh : P.nn h) (hnu : P.nn nu)
    (st : ∀ n, K.add (a' (n + 1)) (K.neg (a (n + 1))) = K.add (K.neg (K.mul (K.mul h (K.mul nu (k (n + 1))))
      (K.add (a' (n + 1)) (a (n + 1))))) (K.mul h (dyN K c a n)))
    (h0 : a 0 = K.zero) (hN : a (N + 1) = K.zero)
    (hq : ∀ n, P.nn (qq K w c n))
    (hcrit : ∀ n, n0 ≤ n → K.mul (qq K w c n) (M n) = K.mul nu (K.mul (w n) (k n)))
    (htail : ∀ n, n0 ≤ n → Le K P (a (n + 1)) (M n))
    (hA : ∀ n, Le K P (a (n + 1)) A) (hAnn : P.nn A)
    (hE : ∀ n, Le K P (K.mul (a n) (a n)) E) :
    Le K P (sumN K (fun n => K.mul (w (n + 1)) (K.add (K.mul (a' (n + 1)) (a' (n + 1))) (K.neg (K.mul (a (n + 1)) (a (n + 1)))))) N)
      (K.add (K.add (K.mul (K.add h h) (sumN K (fun n => if n < n0 then K.mul (qq K w c n) (K.mul E A) else K.zero) N))
          (sumN K (fun n => K.mul (w (n + 1)) (K.mul (K.mul h (K.mul nu (k (n + 1))))
            (K.mul (K.add (a' (n + 1)) (K.neg (a (n + 1)))) (K.add (a' (n + 1)) (K.neg (a (n + 1))))))) N))
        (K.mul h (sumN K (fun n => K.mul (w (n + 1)) (K.mul (K.add (a' (n + 1)) (K.neg (a (n + 1)))) (dyN K c a n))) N))) :=
  semi_wstep K P (fun n => w (n + 1)) (fun n => k (n + 1)) (fun n => a (n + 1)) (fun n => a' (n + 1)) (fun n => dyN K c a n)
    h nu _ N (fun n => hw (n + 1)) (fun n => hk (n + 1)) hh hnu st
    (rate_bound K P w c k a M nu A E n0 N h0 hN hq hcrit htail hA hAnn hE
      (fun n => P.nn_mul _ _ hnu (P.nn_mul _ _ (hw n) (P.nn_mul _ _ (hk n) (P.nn_sq _)))))

end SemiWDy

#print axioms SemiWDy.semi_wdy

ns/SemiWStep.lean

lean_all/ns/SemiWStep.lean

/-
  ns/SemiWStep.lean — T37 cycle 817 (goal G528): one weighted step of the semi-implicit scheme over any ring with a
  positive cone (ℚ included). With x_n = hνk_n, w_n, k_n, h, ν ≥ 0, Δ = a' − a, and the state rate bound
    Σ w_n a_n (−ν k_n a_n + N_n) ≤ B                  (DyadicLarge.rate_bound gives this for the dyadic N),
  one has
    Σ w (a'² − a²) ≤ 2hB + Σ w x Δ² + h Σ w Δ N.
  The dissipation at the midpoint cancels the one at the state up to Σ w x Δ² = O(h²)-type terms.

  Lean 4 core plus ns/SemiWeighted (semi_weighted), ns/DyadicLarge, ns/DyUnique (sumN_ext_g), lib/RingTac.
  The midpoint/state exchange uses the parallelogram identity s² + Δ² = 2a'² + 2a² termwise.
    semi_wstep  the inequality
-/
import ns.SemiWeighted
import ns.DyadicLarge
import ns.DyUnique

namespace SemiWStep
open DetChain NSChain LibRing DyadicLarge SemiWeighted

section Gen
variable {R : Type} (K : CRing R) (P : Cone K)

theorem semi_wstep (w k a a' Nn : Nat → R) (h nu B : R) (N : Nat)
    (hw : ∀ n, P.nn (w n)) (hk : ∀ n, P.nn (k n)) (hh : P.nn h) (hnu : P.nn nu)
    (st : ∀ n, K.add (a' n) (K.neg (a n)) = K.add (K.neg (K.mul (K.mul h (K.mul nu (k n))) (K.add (a' n) (a n))))
      (K.mul h (Nn n)))
    (rate : Le K P (sumN K (fun n => K.mul (w n) (K.mul (a n) (K.add (K.neg (K.mul nu (K.mul (k n) (a n)))) (Nn n)))) N) B) :
    Le K P (sumN K (fun n => K.mul (w n) (K.add (K.mul (a' n) (a' n)) (K.neg (K.mul (a n) (a n))))) N)
      (K.add (K.add (K.mul (K.add h h) B)
          (sumN K (fun n => K.mul (w n) (K.mul (K.mul h (K.mul nu (k n)))
            (K.mul (K.add (a' n) (K.neg (a n))) (K.add (a' n) (K.neg (a n)))))) N))
        (K.mul h (sumN K (fun n => K.mul (w n) (K.mul (K.add (a' n) (K.neg (a n))) (Nn n))) N))) := by
  have id := semi_weighted K w a a' (fun n => K.mul h (K.mul nu (k n))) Nn h st N
  -- abbreviations as sums
  generalize hS1 : sumN K (fun n => K.mul (w n) (K.add (K.mul (a' n) (a' n)) (K.neg (K.mul (a n) (a n))))) N = S1 at id ⊢
  generalize hDs : sumN K (fun n => K.mul (w n) (K.mul (K.mul h (K.mul nu (k n))) (K.mul (K.add (a' n) (a n)) (K.add (a' n) (a n))))) N = Ds at id
  generalize hDd : sumN K (fun n => K.mul (w n) (K.mul (K.mul h (K.mul nu (k n)))
      (K.mul (K.add (a' n) (K.neg (a n))) (K.add (a' n) (K.neg (a n)))))) N = Dd
  generalize hX : sumN K (fun n => K.mul (w n) (K.mul (K.add (a' n) (K.neg (a n))) (Nn n))) N = X at id ⊢
  generalize hY : sumN K (fun n => K.mul (w n) (K.mul (a n) (Nn n))) N = Y at id
  -- Y = Σ w a(−νka + N) + ν Σ w k a² ≤ B + ν Σ w k a²
  let Da : R := sumN K (fun n => K.mul (w n) (K.mul nu (K.mul (k n) (K.mul (a n) (a n))))) N
  have eY : Y = K.add (sumN K (fun n => K.mul (w n) (K.mul (a n) (K.add (K.neg (K.mul nu (K.mul (k n) (a n)))) (Nn n)))) N) Da := by
    rw [← hY, ← sumN_add]
    apply DyUnique.sumN_ext_g
    intro n _; lib_ring
  -- 2hνΣ w k a² ≤ Ds + Dd  (parallelogram, termwise)
  have par : Le K P (K.mul (K.add h h) Da) (K.add Ds Dd) := by
    rw [← hDs, ← hDd, ← sumN_add, ← sumN_mul_left]
    apply sum_le K P
    intro n
    refine ⟨K.mul (w n) (K.mul (K.mul h (K.mul nu (k n))) (K.add (K.mul (a' n) (a' n)) (K.mul (a' n) (a' n)))),
      P.nn_mul _ _ (hw n) (P.nn_mul _ _ (P.nn_mul _ _ hh (P.nn_mul _ _ hnu (hk n))) (P.nn_add _ _ (P.nn_sq _) (P.nn_sq _))), ?_⟩
    lib_ring
  -- assemble: S1 = hX + 2hY − Ds ≤ hX + 2h(B + Da) − Ds ≤ hX + 2hB + Dd
  have r2 := le_mul_nn K P (K.add h h) _ _ (P.nn_add _ _ hh hh) rate
  cases r2 with
  | intro e1 he1 =>
    cases par with
    | intro e2 he2 =>
      refine ⟨K.add e1 e2, P.nn_add _ _ he1.1 he2.1, ?_⟩
      have hS : S1 = K.add (K.add (K.mul h X) (K.mul (K.add h h) Y)) (K.neg Ds) := by
        have : S1 = K.add (K.add S1 Ds) (K.neg Ds) := by lib_ring
        rw [this, id]
      rw [hS, eY]
      have u1 := he1.2
      have u2 := he2.2
      generalize sumN K (fun n => K.mul (w n) (K.mul (a n) (K.add (K.neg (K.mul nu (K.mul (k n) (a n)))) (Nn n)))) N = Z at u1 ⊢
      generalize Da = D at u2 ⊢
      -- u1: 2h·Z + e1 = 2h·B ;  u2: 2h·D + e2 = Ds + Dd
      have : K.add (K.add (K.add (K.mul h X) (K.mul (K.add h h) (K.add Z D))) (K.neg Ds)) (K.add e1 e2)
          = K.add (K.add (K.add (K.mul (K.add h h) Z) e1) (K.add (K.mul (K.add h h) D) e2)) (K.add (K.mul h X) (K.neg Ds)) := by
        lib_ring
      rw [this, u1, u2]; lib_ring

end Gen

end SemiWStep

#print axioms SemiWStep.semi_wstep

ns/SemiWStep2.lean

lean_all/ns/SemiWStep2.lean

/-
  ns/SemiWStep2.lean — T37 cycle 830 (goal G540): one weighted step of the semi-implicit scheme over any ring with a
  positive cone (ℚ included). With x_n = hνk_n, h ≥ 0 (no sign needed on w, k, ν), Δ = a' − a, and the rate bound
    Σ w_n a_n (−ν k_n a_n + N_n) ≤ B                  (DyadicLarge.rate_bound gives this for the dyadic N),
  one has the sharper
    Σ w (a'² − a²) + 2 Σ w x a'² ≤ 2hB + Σ w x Δ² + h Σ w Δ N
  (the parallelogram identity x s² + x Δ² = 2x a² + 2x a'² is used exactly, keeping the new-state dissipation).

  Lean 4 core plus ns/SemiWeighted (semi_weighted), ns/DyadicLarge, ns/DyUnique (sumN_ext_g), lib/RingTac.
  The midpoint/state exchange uses the parallelogram identity s² + Δ² = 2a'² + 2a² termwise.
    semi_wstep  the inequality
-/
import ns.SemiWeighted
import ns.DyadicLarge
import ns.DyUnique

namespace SemiWStep2
open DetChain NSChain LibRing DyadicLarge SemiWeighted

section Gen
variable {R : Type} (K : CRing R) (P : Cone K)

theorem semi_wstep2 (w k a a' Nn : Nat → R) (h nu B : R) (N : Nat)
    (hh : P.nn h)
    (st : ∀ n, K.add (a' n) (K.neg (a n)) = K.add (K.neg (K.mul (K.mul h (K.mul nu (k n))) (K.add (a' n) (a n))))
      (K.mul h (Nn n)))
    (rate : Le K P (sumN K (fun n => K.mul (w n) (K.mul (a n) (K.add (K.neg (K.mul nu (K.mul (k n) (a n)))) (Nn n)))) N) B) :
    Le K P (K.add (sumN K (fun n => K.mul (w n) (K.add (K.mul (a' n) (a' n)) (K.neg (K.mul (a n) (a n))))) N)
        (sumN K (fun n => K.mul (w n) (K.mul (K.mul h (K.mul nu (k n))) (K.add (K.mul (a' n) (a' n)) (K.mul (a' n) (a' n))))) N))
      (K.add (K.add (K.mul (K.add h h) B)
          (sumN K (fun n => K.mul (w n) (K.mul (K.mul h (K.mul nu (k n)))
            (K.mul (K.add (a' n) (K.neg (a n))) (K.add (a' n) (K.neg (a n)))))) N))
        (K.mul h (sumN K (fun n => K.mul (w n) (K.mul (K.add (a' n) (K.neg (a n))) (Nn n))) N))) := by
  have id := semi_weighted K w a a' (fun n => K.mul h (K.mul nu (k n))) Nn h st N
  -- abbreviations as sums
  generalize hS1 : sumN K (fun n => K.mul (w n) (K.add (K.mul (a' n) (a' n)) (K.neg (K.mul (a n) (a n))))) N = S1 at id ⊢
  generalize hDs : sumN K (fun n => K.mul (w n) (K.mul (K.mul h (K.mul nu (k n))) (K.mul (K.add (a' n) (a n)) (K.add (a' n) (a n))))) N = Ds at id
  generalize hDd : sumN K (fun n => K.mul (w n) (K.mul (K.mul h (K.mul nu (k n)))
      (K.mul (K.add (a' n) (K.neg (a n))) (K.add (a' n) (K.neg (a n)))))) N = Dd
  generalize hX : sumN K (fun n => K.mul (w n) (K.mul (K.add (a' n) (K.neg (a n))) (Nn n))) N = X at id ⊢
  generalize hY : sumN K (fun n => K.mul (w n) (K.mul (a n) (Nn n))) N = Y at id
  -- Y = Σ w a(−νka + N) + ν Σ w k a² ≤ B + ν Σ w k a²
  let Da : R := sumN K (fun n => K.mul (w n) (K.mul nu (K.mul (k n) (K.mul (a n) (a n))))) N
  have eY : Y = K.add (sumN K (fun n => K.mul (w n) (K.mul (a n) (K.add (K.neg (K.mul nu (K.mul (k n) (a n)))) (Nn n)))) N) Da := by
    rw [← hY, ← sumN_add]
    apply DyUnique.sumN_ext_g
    intro n _; lib_ring
  -- Ds + Dd = 2hν Σ w k a² + Σ w x (2a'²)  (parallelogram, exact)
  generalize hW : sumN K (fun n => K.mul (w n) (K.mul (K.mul h (K.mul nu (k n))) (K.add (K.mul (a' n) (a' n)) (K.mul (a' n) (a' n))))) N = W
  have par : K.add Ds Dd = K.add (K.mul (K.add h h) Da) W := by
    rw [← hDs, ← hDd, ← hW, ← sumN_add, ← sumN_mul_left, ← sumN_add]
    apply DyUnique.sumN_ext_g
    intro n _; lib_ring
  -- assemble: S1 + W = hX + 2hY − Ds + W = hX + 2h(Z + Da) − Ds + W = hX + 2hZ + Dd ≤ hX + 2hB + Dd
  have r2 := le_mul_nn K P (K.add h h) _ _ (P.nn_add _ _ hh hh) rate
  cases r2 with
  | intro e1 he1 =>
    refine ⟨e1, he1.1, ?_⟩
    have hS : S1 = K.add (K.add (K.mul h X) (K.mul (K.add h h) Y)) (K.neg Ds) := by
      have : S1 = K.add (K.add S1 Ds) (K.neg Ds) := by lib_ring
      rw [this, id]
    rw [hS, eY]
    have u1 := he1.2
    generalize sumN K (fun n => K.mul (w n) (K.mul (a n) (K.add (K.neg (K.mul nu (K.mul (k n) (a n)))) (Nn n)))) N = Z at u1 ⊢
    generalize Da = D at par ⊢
    have : K.add (K.add (K.add (K.add (K.mul h X) (K.mul (K.add h h) (K.add Z D))) (K.neg Ds)) W) e1
        = K.add (K.add (K.add (K.mul (K.add h h) Z) e1) (K.add (K.mul (K.add h h) D) W)) (K.add (K.mul h X) (K.neg Ds)) := by
      lib_ring
    rw [this, u1, ← par]; lib_ring

end Gen

end SemiWStep2

#print axioms SemiWStep2.semi_wstep2

ns/SemiWeighted.lean

lean_all/ns/SemiWeighted.lean

/-
  ns/SemiWeighted.lean — T37 cycle 814 (goal G525): the weighted energy identity of the semi-implicit scheme over any
  commutative ring: with weights w_n and a'_n − a_n = −x_n(a'_n + a_n) + hN_n,
    Σ w(a'² − a²) + Σ w x (a' + a)² = h Σ w (a' − a) N + 2h Σ w a N            (exact).
  For w ≡ 1 and a neutral nonlinearity the last sum vanishes (SemiEnergy); for the dyadic model with w_n = 4ⁿ it is
  the flux sum Σ (w_{n+1} − w_n)·flux_n, the term the large-data argument controls.

  Lean 4 core plus ns/SemiEnergy (step_energy), lib/RingTac.
    semi_weighted   the identity
-/
import ns.SemiEnergy

namespace SemiWeighted
open DetChain NSChain LibRing SemiEnergy

section Gen
variable {R : Type} (K : CRing R)

theorem semi_weighted (w a a' x Nn : Nat → R) (h : R)
    (st : ∀ n, K.add (a' n) (K.neg (a n)) = K.add (K.neg (K.mul (x n) (K.add (a' n) (a n)))) (K.mul h (Nn n))) :
    ∀ N, K.add (sumN K (fun n => K.mul (w n) (K.add (K.mul (a' n) (a' n)) (K.neg (K.mul (a n) (a n))))) N)
        (sumN K (fun n => K.mul (w n) (K.mul (x n) (K.mul (K.add (a' n) (a n)) (K.add (a' n) (a n))))) N)
      = K.add (K.mul h (sumN K (fun n => K.mul (w n) (K.mul (K.add (a' n) (K.neg (a n))) (Nn n))) N))
          (K.mul (K.add h h) (sumN K (fun n => K.mul (w n) (K.mul (a n) (Nn n))) N))
  | 0 => by show K.add K.zero K.zero = K.add (K.mul h K.zero) (K.mul (K.add h h) K.zero); lib_ring
  | N + 1 => by
    have ih := semi_weighted w a a' x Nn h st N
    have e := step_energy K (a N) (a' N) (x N) (K.mul h (Nn N)) (st N)
    show K.add (K.add (sumN K (fun n => K.mul (w n) (K.add (K.mul (a' n) (a' n)) (K.neg (K.mul (a n) (a n))))) N)
          (K.mul (w N) (K.add (K.mul (a' N) (a' N)) (K.neg (K.mul (a N) (a N))))))
        (K.add (sumN K (fun n => K.mul (w n) (K.mul (x n) (K.mul (K.add (a' n) (a n)) (K.add (a' n) (a n))))) N)
          (K.mul (w N) (K.mul (x N) (K.mul (K.add (a' N) (a N)) (K.add (a' N) (a N))))))
      = K.add (K.mul h (K.add (sumN K (fun n => K.mul (w n) (K.mul (K.add (a' n) (K.neg (a n))) (Nn n))) N)
            (K.mul (w N) (K.mul (K.add (a' N) (K.neg (a N))) (Nn N)))))
          (K.mul (K.add h h) (K.add (sumN K (fun n => K.mul (w n) (K.mul (a n) (Nn n))) N) (K.mul (w N) (K.mul (a N) (Nn N)))))
    -- per mode: w(a'² − a²) + w x s² = w·(h(a'−a)N + 2haN), from step_energy
    have m1 : K.add (K.mul (w N) (K.add (K.mul (a' N) (a' N)) (K.neg (K.mul (a N) (a N)))))
        (K.mul (w N) (K.mul (x N) (K.mul (K.add (a' N) (a N)) (K.add (a' N) (a N)))))
        = K.add (K.mul h (K.mul (w N) (K.mul (K.add (a' N) (K.neg (a N))) (Nn N))))
          (K.mul (K.add h h) (K.mul (w N) (K.mul (a N) (Nn N)))) := by
      have t : K.add (K.mul (w N) (K.add (K.mul (a' N) (a' N)) (K.neg (K.mul (a N) (a N)))))
          (K.mul (w N) (K.mul (x N) (K.mul (K.add (a' N) (a N)) (K.add (a' N) (a N)))))
          = K.add (K.mul (w N) (K.add (K.add (K.mul (a' N) (a' N)) (K.mul (x N) (K.mul (K.add (a' N) (a N)) (K.add (a' N) (a N)))))
              (K.neg (K.mul (a N) (a N))))) K.zero := by lib_ring
      rw [t, e]; lib_ring
    have r : K.add (K.add (sumN K (fun n => K.mul (w n) (K.add (K.mul (a' n) (a' n)) (K.neg (K.mul (a n) (a n))))) N)
          (K.mul (w N) (K.add (K.mul (a' N) (a' N)) (K.neg (K.mul (a N) (a N))))))
        (K.add (sumN K (fun n => K.mul (w n) (K.mul (x n) (K.mul (K.add (a' n) (a n)) (K.add (a' n) (a n))))) N)
          (K.mul (w N) (K.mul (x N) (K.mul (K.add (a' N) (a N)) (K.add (a' N) (a N))))))
        = K.add (K.add (sumN K (fun n => K.mul (w n) (K.add (K.mul (a' n) (a' n)) (K.neg (K.mul (a n) (a n))))) N)
            (sumN K (fun n => K.mul (w n) (K.mul (x n) (K.mul (K.add (a' n) (a n)) (K.add (a' n) (a n))))) N))
          (K.add (K.mul (w N) (K.add (K.mul (a' N) (a' N)) (K.neg (K.mul (a N) (a N)))))
            (K.mul (w N) (K.mul (x N) (K.mul (K.add (a' N) (a N)) (K.add (a' N) (a N)))))) := by lib_ring
    rw [r, ih, m1]; lib_ring

end Gen

end SemiWeighted

#print axioms SemiWeighted.semi_weighted

ns/SerrinAgmon.lean

lean_all/ns/SerrinAgmon.lean

/-
  ns/SerrinAgmon.lean — T37 cycle 185: the discrete L^∞ continuation criterion from sup Z and ∫‖Δu‖².

  Lean 4 core plus ns/SerrinRun, lib/OField, lib/RingTac. Over the prefix k < N of a run, with
      X_k⁴ ≤ C Z_k P_k   (Agmon),   A = h c² / (2ν),   S_N = A Σ_{k<N} X_k²,
  prefix Cauchy–Schwarz (by the Lagrange identity, no lists) gives (Σ X²)² ≤ N · C · M · Σ P when Z_k ≤ M, so
      A² · N · C · M · Σ_{k<N} P_k ≤ q²,  0 < q ≤ 1   ⇒   Z_N (1 − q) ≤ Z_0                     (serrin_agmon)
  and, choosing M with Z_0 ≤ M (1 − q), q < 1, the bound closes on itself:
      budget for every N  ⇒  Z_N ≤ M for every N                                            (continuation)
  Enstrophy stays bounded as long as N·Σ_{k<N}‖Δu_k‖² stays below q²/(A² C M): the R1-gap in discrete form.
-/
import ns.SerrinRun

namespace SerrinAgmon
open DetChain LibRing LibCS LibGron LibOF SerrinDiscrete SerrinSum YoungStep SerrinRun

variable {R : Type} (K : CRing R) (P : Pos K)

/-! ## Prefix sums -/

theorem sumA_succ (a : Nat → R) (n : Nat) : sumA K a (n + 1) = K.add (sumA K a n) (a n) := rfl

theorem sumA_ext (a b : Nat → R) (h : ∀ k, a k = b k) : ∀ n, sumA K a n = sumA K b n
  | 0 => rfl
  | n + 1 => by rw [sumA_succ, sumA_succ, sumA_ext a b h n, h n]

theorem sumA_mul (c : R) (a : Nat → R) : ∀ n, sumA K (fun k => K.mul c (a k)) n = K.mul c (sumA K a n)
  | 0 => by show K.zero = K.mul c K.zero; lib_ring
  | n + 1 => by rw [sumA_succ, sumA_succ, sumA_mul c a n]; lib_ring

theorem sumA_le (u v : Nat → R) : ∀ n, (∀ k, k < n → le K P (u k) (v k)) →
    le K P (sumA K u n) (sumA K v n)
  | 0, _ => le_refl K P _
  | n + 1, h => by
    rw [sumA_succ, sumA_succ]
    exact le_add2 K P (sumA_le u v n (fun k hk => h k (Nat.lt_succ_of_lt hk))) (h n (Nat.lt_succ_self n))

/-! ## Prefix Cauchy–Schwarz by the Lagrange identity -/

def lagIn (x : Nat → R) (y : R) : Nat → R
  | 0 => K.zero
  | n + 1 => K.add (lagIn x y n) (K.mul (sub K (x n) y) (sub K (x n) y))

def lagD (x : Nat → R) : Nat → R
  | 0 => K.zero
  | n + 1 => K.add (lagD x n) (lagIn K x (x n) n)

theorem lagIn_eq (x : Nat → R) (y : R) : ∀ n,
    lagIn K x y n = K.add (sub K (sumA K (fun i => K.mul (x i) (x i)) n) (K.mul (K.add y y) (sumA K x n)))
      (K.mul (castN K n) (K.mul y y))
  | 0 => by
    show K.zero = K.add (sub K K.zero (K.mul (K.add y y) K.zero)) (K.mul K.zero (K.mul y y)); lib_ring
  | n + 1 => by
    have e : lagIn K x y (n + 1) = K.add (lagIn K x y n) (K.mul (sub K (x n) y) (sub K (x n) y)) := rfl
    have c : castN K (n + 1) = K.add K.one (castN K n) := rfl
    rw [e, lagIn_eq x y n, sumA_succ, sumA_succ, c]; lib_ring

theorem lagrange (x : Nat → R) : ∀ n,
    sub K (K.mul (castN K n) (sumA K (fun i => K.mul (x i) (x i)) n)) (K.mul (sumA K x n) (sumA K x n)) = lagD K x n
  | 0 => by show sub K (K.mul K.zero K.zero) (K.mul K.zero K.zero) = K.zero; lib_ring
  | n + 1 => by
    have e : lagD K x (n + 1) = K.add (lagD K x n) (lagIn K x (x n) n) := rfl
    have c : castN K (n + 1) = K.add K.one (castN K n) := rfl
    rw [e, ← lagrange x n, lagIn_eq, sumA_succ, sumA_succ, c]; lib_ring

theorem lagIn_nn (x : Nat → R) (y : R) : ∀ n, P.nn (lagIn K x y n)
  | 0 => P.nn_zero
  | n + 1 => P.nn_add _ _ (lagIn_nn x y n) (P.nn_sq _)

theorem lagD_nn (x : Nat → R) : ∀ n, P.nn (lagD K x n)
  | 0 => P.nn_zero
  | n + 1 => P.nn_add _ _ (lagD_nn x n) (lagIn_nn K P x (x n) n)

/-- (Σ_{k<n} x_k)² ≤ n Σ_{k<n} x_k². -/
theorem cs_prefix (x : Nat → R) (n : Nat) :
    le K P (K.mul (sumA K x n) (sumA K x n)) (K.mul (castN K n) (sumA K (fun i => K.mul (x i) (x i)) n)) := by
  unfold le; rw [lagrange]; exact lagD_nn K P x n

/-- **Agmon over a prefix.** (Σ_{k<n} X_k²)² ≤ n · (C M) · Σ_{k<n} P_k. -/
theorem agmon_prefix (X Z Pp : Nat → R) (C M : R) (n : Nat) (hC : P.nn C) (hP : ∀ k, P.nn (Pp k))
    (hA : ∀ k, le K P (K.mul (K.mul (X k) (X k)) (K.mul (X k) (X k))) (K.mul C (K.mul (Z k) (Pp k))))
    (hZ : ∀ k, k < n → le K P (Z k) M) :
    le K P (K.mul (sumA K (fun k => K.mul (X k) (X k)) n) (sumA K (fun k => K.mul (X k) (X k)) n))
      (K.mul (castN K n) (K.mul (K.mul C M) (sumA K Pp n))) := by
  have cs := cs_prefix K P (fun k => K.mul (X k) (X k)) n
  have s1 := sumA_le K P _ _ n (fun k _ => hA k)
  have s2 := sumA_le K P (fun k => K.mul C (K.mul (Z k) (Pp k))) (fun k => K.mul (K.mul C M) (Pp k)) n
    (fun k hk => by
      have m := le_mul_left K P (P.nn_mul _ _ hC (hP k)) (hZ k hk)
      rw [show K.mul (K.mul C (Pp k)) (Z k) = K.mul C (K.mul (Z k) (Pp k)) by lib_ring,
          show K.mul (K.mul C (Pp k)) M = K.mul (K.mul C M) (Pp k) by lib_ring] at m
      exact m)
  rw [sumA_mul K (K.mul C M) Pp n] at s2
  exact le_trans K P cs (le_mul_left K P (nn_castN K P n) (le_trans K P s1 s2))

/-! ## Square roots without roots -/

theorem le_of_sq (F : OF K P) (S q : R) (hS : P.nn S) (hq : P.nn q) (hq0 : q ≠ K.zero)
    (h : le K P (K.mul S S) (K.mul q q)) : le K P S q := by
  have hqs : K.add q S ≠ K.zero := fun e => by
    have en : K.neg q = S := by
      rw [show K.neg q = K.add (K.add q S) (K.neg q) by rw [e]; lib_ring]; lib_ring
    exact hq0 (F.antisymm q hq (by rw [en]; exact hS))
  have hi := F.mul_inv _ hqs
  have e : K.mul (sub K (K.mul q q) (K.mul S S)) (F.inv (K.add q S))
      = K.mul (sub K q S) (K.mul (K.add q S) (F.inv (K.add q S))) := by lib_ring
  rw [hi] at e
  unfold le
  rw [show sub K q S = K.mul (sub K q S) K.one by lib_ring, ← e]
  exact P.nn_mul _ _ h (nn_inv K P F _ (P.nn_add _ _ hq hS) hqs)

/-! ## The criterion -/

def amp (F : OF K P) (h c ν : R) : R := K.mul (F.inv (K.add ν ν)) (K.mul h (K.mul c c))

theorem sum_aRun (F : OF K P) (h c ν : R) (X : Nat → R) (n : Nat) :
    sumA K (aRun K P F h c ν X) n = K.mul (amp K P F h c ν) (sumA K (fun k => K.mul (X k) (X k)) n) := by
  rw [← sumA_mul]
  exact sumA_ext K _ _ (fun k => by show K.mul _ _ = K.mul _ _; unfold amp; lib_ring) n

/-- **Discrete L^∞ continuation over a prefix.** -/
theorem serrin_agmon (F : OF K P) (Z r s X Pp : Nat → R) (h c ν C M q : R) (hh : P.nn h) (hν : P.nn ν)
    (hν0 : K.add ν ν ≠ K.zero) (hr : ∀ k, K.mul (r k) (r k) = Z k)
    (step : ∀ k, le K P (Z (k + 1))
      (K.add (Z k) (K.mul h (sub K (K.mul (K.mul (K.mul c (X k)) (r k)) (s k)) (K.mul ν (K.mul (s k) (s k)))))))
    (hC : P.nn C) (hP : ∀ k, P.nn (Pp k))
    (hA : ∀ k, le K P (K.mul (K.mul (X k) (X k)) (K.mul (X k) (X k))) (K.mul C (K.mul (Z k) (Pp k))))
    (N : Nat) (hZ : ∀ k, k < N → le K P (Z k) M)
    (hq : P.nn q) (hq0 : q ≠ K.zero) (hq1 : le K P q K.one)
    (budget : le K P (K.mul (K.mul (amp K P F h c ν) (amp K P F h c ν))
      (K.mul (castN K N) (K.mul (K.mul C M) (sumA K Pp N)))) (K.mul q q)) :
    le K P (K.mul (Z N) (sub K K.one q)) (Z 0) := by
  have hamp : P.nn (amp K P F h c ν) :=
    P.nn_mul _ _ (nn_inv K P F _ (P.nn_add _ _ hν hν) hν0) (P.nn_mul _ _ hh (P.nn_sq c))
  have ha : ∀ k, P.nn (aRun K P F h c ν X k) := fun k =>
    P.nn_mul _ _ (nn_inv K P F _ (P.nn_add _ _ hν hν) hν0)
      (P.nn_mul _ _ hh (P.nn_mul _ _ (P.nn_sq c) (P.nn_sq (X k))))
  -- S² = A² T² ≤ A² · N C M ΣP ≤ q²
  have ag := le_mul_left K P (P.nn_sq (amp K P F h c ν)) (agmon_prefix K P X Z Pp C M N hC hP hA hZ)
  have sq : le K P (K.mul (sumA K (aRun K P F h c ν X) N) (sumA K (aRun K P F h c ν X) N)) (K.mul q q) := by
    rw [sum_aRun]
    rw [show K.mul (K.mul (amp K P F h c ν) (sumA K (fun k => K.mul (X k) (X k)) N))
          (K.mul (amp K P F h c ν) (sumA K (fun k => K.mul (X k) (X k)) N))
        = K.mul (K.mul (amp K P F h c ν) (amp K P F h c ν))
          (K.mul (sumA K (fun k => K.mul (X k) (X k)) N) (sumA K (fun k => K.mul (X k) (X k)) N)) by lib_ring]
    exact le_trans K P ag budget
  have Sq := le_of_sq K P F _ q (sumA_nn K P _ ha N) hq hq0 sq
  -- 1 − S ≥ 1 − q ≥ 0
  have hS : P.nn (sub K K.one (sumA K (aRun K P F h c ν X) N)) := by
    have e : sub K K.one (sumA K (aRun K P F h c ν X) N)
        = K.add (sub K K.one q) (sub K q (sumA K (aRun K P F h c ν X) N)) := by lib_ring
    rw [e]; exact P.nn_add _ _ hq1 Sq
  have hZ0 : P.nn (Z 0) := by rw [← hr 0]; exact P.nn_sq _
  have run := serrin_run K P F Z r s X h c ν hh hν hν0 hr hZ0 step N hS
  have hZN : P.nn (Z N) := by rw [← hr N]; exact P.nn_sq _
  -- Z_N (1 − q) ≤ Z_N (1 − S)
  have m := le_mul_left K P hZN (show le K P (sub K K.one q) (sub K K.one (sumA K (aRun K P F h c ν X) N)) by
    unfold le
    rw [show sub K (sub K K.one (sumA K (aRun K P F h c ν X) N)) (sub K K.one q)
        = sub K q (sumA K (aRun K P F h c ν X) N) by lib_ring]
    exact Sq)
  exact le_trans K P m run

theorem lt_succ_cases {k n : Nat} (h : k < n + 1) : k < n ∨ k = n :=
  Nat.lt_or_eq_of_le (Nat.le_of_lt_succ h)

/-- **Continuation: the bound closes on itself.** -/
theorem continuation (F : OF K P) (Z r s X Pp : Nat → R) (h c ν C M q : R) (hh : P.nn h) (hν : P.nn ν)
    (hν0 : K.add ν ν ≠ K.zero) (hr : ∀ k, K.mul (r k) (r k) = Z k)
    (step : ∀ k, le K P (Z (k + 1))
      (K.add (Z k) (K.mul h (sub K (K.mul (K.mul (K.mul c (X k)) (r k)) (s k)) (K.mul ν (K.mul (s k) (s k)))))))
    (hC : P.nn C) (hP : ∀ k, P.nn (Pp k))
    (hA : ∀ k, le K P (K.mul (K.mul (X k) (X k)) (K.mul (X k) (X k))) (K.mul C (K.mul (Z k) (Pp k))))
    (hq : P.nn q) (hq0 : q ≠ K.zero) (hq1 : le K P q K.one) (hq1' : sub K K.one q ≠ K.zero)
    (hM : le K P (Z 0) (K.mul M (sub K K.one q)))
    (budget : ∀ N, le K P (K.mul (K.mul (amp K P F h c ν) (amp K P F h c ν))
      (K.mul (castN K N) (K.mul (K.mul C M) (sumA K Pp N)))) (K.mul q q)) :
    ∀ N, le K P (Z N) M := by
  have claim : ∀ N, ∀ k, k < N → le K P (Z k) M := by
    intro N
    induction N with
    | zero => intro k hk; exact absurd hk (Nat.not_lt_zero k)
    | succ n ih =>
      intro k hk
      cases lt_succ_cases hk with
      | inl hl => exact ih k hl
      | inr he =>
        rw [he]
        have b := serrin_agmon K P F Z r s X Pp h c ν C M q hh hν hν0 hr step hC hP hA n ih hq hq0 hq1 (budget n)
        -- (M − Z_n)(1 − q) ≥ 0, then divide by 1 − q
        have t := le_trans K P b hM
        unfold le at t ⊢
        have hi := F.mul_inv _ hq1'
        have e : K.mul (sub K (K.mul M (sub K K.one q)) (K.mul (Z n) (sub K K.one q))) (F.inv (sub K K.one q))
            = K.mul (sub K M (Z n)) (K.mul (sub K K.one q) (F.inv (sub K K.one q))) := by lib_ring
        rw [hi] at e
        rw [show sub K M (Z n) = K.mul (sub K M (Z n)) K.one by lib_ring, ← e]
        exact P.nn_mul _ _ t (nn_inv K P F _ hq1 hq1')
  exact fun N => claim (N + 1) N (Nat.lt_succ_self N)

end SerrinAgmon

#print axioms SerrinAgmon.sumA_succ
#print axioms SerrinAgmon.sumA_ext
#print axioms SerrinAgmon.sumA_mul
#print axioms SerrinAgmon.sumA_le
#print axioms SerrinAgmon.lagIn_eq
#print axioms SerrinAgmon.lagrange
#print axioms SerrinAgmon.lagIn_nn
#print axioms SerrinAgmon.lagD_nn
#print axioms SerrinAgmon.cs_prefix
#print axioms SerrinAgmon.agmon_prefix
#print axioms SerrinAgmon.le_of_sq
#print axioms SerrinAgmon.sum_aRun
#print axioms SerrinAgmon.serrin_agmon
#print axioms SerrinAgmon.lt_succ_cases
#print axioms SerrinAgmon.continuation

ns/SerrinDiscrete.lean

lean_all/ns/SerrinDiscrete.lean

/-
  ns/SerrinDiscrete.lean — T37 cycle 145: a Prodi–Serrin-type discrete criterion.

  Lean 4 core plus the shared lib/Gronwall (comparison, le), lib/RingTac. If z_{k+1} ≤ (1 + a_k) z_k with
  a_k ≥ 0 (a_k = hC‖u_k‖²_∞/ν from the stretching estimate), then
      z_N ≤ Π_{k<N} (1 + a_k) · z_0
  so the enstrophy stays bounded on any interval where Σ_k a_k (i.e. ∫‖u‖²_∞ dt) is finite — the product
  is bounded by any bound on Π(1 + a_k), e.g. by Bernoulli-type estimates for small a_k.
-/
import lib.Gronwall

namespace SerrinDiscrete
open DetChain LibRing LibCS LibGron

variable {R : Type} (K : CRing R) (P : Pos K)

def prodC (a : Nat → R) : Nat → R
  | 0 => K.one
  | n + 1 => K.mul (K.add K.one (a n)) (prodC a n)

theorem serrin (z a : Nat → R) (ha : ∀ k, P.nn (a k)) (hone : P.nn K.one)
    (step : ∀ k, le K P (z (k + 1)) (K.mul (K.add K.one (a k)) (z k))) :
    ∀ N, le K P (z N) (K.mul (prodC K a N) (z 0)) := by
  have c := comparison K P z (fun N => K.mul (prodC K a N) (z 0)) (fun k => K.add K.one (a k)) (fun _ => K.zero)
    (fun k => P.nn_add _ _ hone (ha k))
    (fun k => by rw [show K.add (K.mul (K.add K.one (a k)) (z k)) K.zero = K.mul (K.add K.one (a k)) (z k) by lib_ring]; exact step k)
    (fun k => by show K.mul (K.mul (K.add K.one (a k)) (prodC K a k)) (z 0) = K.add (K.mul (K.add K.one (a k)) (K.mul (prodC K a k) (z 0))) K.zero; lib_ring)
    (by show le K P (z 0) (K.mul K.one (z 0)); unfold le; rw [show sub K (K.mul K.one (z 0)) (z 0) = K.zero by lib_ring]; exact P.nn_zero)
  exact c

end SerrinDiscrete

#print axioms SerrinDiscrete.serrin

ns/SerrinForced.lean

lean_all/ns/SerrinForced.lean

/-
  ns/SerrinForced.lean — T37 cycle 173: the discrete Prodi–Serrin bound with a force.

  Lean 4 core plus ns/SerrinSum (sumA, prodC_nn), ns/SerrinDiscrete (prodC), lib/Gronwall, lib/RingTac.
    prod_ge_one   a_k ≥ 0  ⇒  Π_{k<n}(1 + a_k) ≥ 1
    serrin_forced z_{k+1} ≤ (1 + a_k) z_k + b_k, a_k, b_k ≥ 0  ⇒  z_N ≤ Π_{k<N}(1 + a_k) · (z_0 + Σ_{k<N} b_k)
  With a_k = hc²‖u_k‖²_∞/(2ν) and b_k = (h/ν)‖f_k‖² (ForcedEnstrophy): the forced enstrophy stays bounded
  while Σ‖u_k‖²_∞ and Σ‖f_k‖² are finite.
-/
import ns.SerrinSum

namespace SerrinForced
open DetChain LibRing LibCS LibGron SerrinDiscrete SerrinSum

variable {R : Type} (K : CRing R) (P : Pos K)

theorem prod_ge_one (a : Nat → R) (ha : ∀ k, P.nn (a k)) (hone : P.nn K.one) :
    ∀ n, le K P K.one (prodC K a n)
  | 0 => le_refl K P _
  | n + 1 => by
      have ih := prod_ge_one a ha hone n
      unfold le at ih ⊢
      rw [show sub K (prodC K a (n + 1)) K.one
          = K.add (K.mul (K.add K.one (a n)) (sub K (prodC K a n) K.one)) (a n) by
        show sub K (K.mul (K.add K.one (a n)) (prodC K a n)) K.one = _; lib_ring]
      exact P.nn_add _ _ (P.nn_mul _ _ (P.nn_add _ _ hone (ha n)) ih) (ha n)

/-- **Forced discrete Serrin bound.** -/
theorem serrin_forced (z a b : Nat → R) (ha : ∀ k, P.nn (a k)) (hb : ∀ k, P.nn (b k)) (hone : P.nn K.one)
    (step : ∀ k, le K P (z (k + 1)) (K.add (K.mul (K.add K.one (a k)) (z k)) (b k))) :
    ∀ N, le K P (z N) (K.mul (prodC K a N) (K.add (z 0) (sumA K b N)))
  | 0 => by
      show le K P (z 0) (K.mul K.one (K.add (z 0) K.zero))
      unfold le; rw [show sub K (K.mul K.one (K.add (z 0) K.zero)) (z 0) = K.zero by lib_ring]; exact P.nn_zero
  | N + 1 => by
      have ih := serrin_forced z a b ha hb hone step N
      have g := prod_ge_one K P a ha hone (N + 1)
      have s := step N
      unfold le at ih g s ⊢
      -- target − z_{N+1} = [(1+a)Πz-part − z_{N+1}] + (1+a)(Π(z0+B) − z_N) + (Π_{N+1} − 1) b_N
      rw [show sub K (K.mul (prodC K a (N + 1)) (K.add (z 0) (sumA K b (N + 1)))) (z (N + 1))
          = K.add (K.add (sub K (K.add (K.mul (K.add K.one (a N)) (z N)) (b N)) (z (N + 1)))
              (K.mul (K.add K.one (a N)) (sub K (K.mul (prodC K a N) (K.add (z 0) (sumA K b N))) (z N))))
              (K.mul (sub K (K.mul (K.add K.one (a N)) (prodC K a N)) K.one) (b N)) by
        show sub K (K.mul (K.mul (K.add K.one (a N)) (prodC K a N)) (K.add (z 0) (K.add (sumA K b N) (b N)))) (z (N + 1)) = _
        lib_ring]
      exact P.nn_add _ _ (P.nn_add _ _ s (P.nn_mul _ _ (P.nn_add _ _ hone (ha N)) ih)) (P.nn_mul _ _ g (hb N))

end SerrinForced

#print axioms SerrinForced.prod_ge_one
#print axioms SerrinForced.serrin_forced

ns/SerrinMid.lean

lean_all/ns/SerrinMid.lean

/-
  ns/SerrinMid.lean — T37 cycle 215 (goal G1, step 4): the discrete Prodi–Serrin bound for Crank–Nicolson runs.

  Lean 4 core plus ns/SerrinSum (sumA, prodC, bern), lib/Gronwall, lib/RingTac. With 0 ≤ b_k ≤ 1 and
      z_{k+1} (1 − b_k) ≤ (1 + b_k) z_k        (MidpointStep with b = hc²X²/(2ν), after dividing by 2·2ν)
    prodM          Π_{k<n} (1 − b_k)
    weier          Π (1 − b_k) ≥ 1 − Σ b_k                    (Weierstrass product inequality)
    mid_prod       z_N Π (1 − b_k) ≤ z_0 Π (1 + b_k)
    serrin_mid     z_N (1 − S_N)² ≤ z_0       (S_N = Σ_{k<N} b_k ≤ 1)
  The CN enstrophy stays below z_0/(1 − S)² while (hc²/2ν) Σ ‖u_k‖²_∞ < 1 — the square is the price of the midpoint.
-/
import ns.SerrinSum

namespace SerrinMid
open DetChain LibRing LibCS LibGron SerrinDiscrete SerrinSum

variable {R : Type} (K : CRing R) (P : Pos K)

def prodM (b : Nat → R) : Nat → R
  | 0 => K.one
  | n + 1 => K.mul (sub K K.one (b n)) (prodM b n)

theorem prodM_nn (b : Nat → R) (hb1 : ∀ k, P.nn (sub K K.one (b k))) (hone : P.nn K.one) :
    ∀ n, P.nn (prodM K b n)
  | 0 => hone
  | n + 1 => P.nn_mul _ _ (hb1 n) (prodM_nn b hb1 hone n)

/-- **Weierstrass.** -/
theorem weier (b : Nat → R) (hb : ∀ k, P.nn (b k)) (hb1 : ∀ k, P.nn (sub K K.one (b k))) :
    ∀ n, le K P (sub K K.one (sumA K b n)) (prodM K b n)
  | 0 => by
    unfold le; show P.nn (sub K K.one (sub K K.one K.zero))
    rw [show sub K K.one (sub K K.one K.zero) = K.zero by lib_ring]; exact P.nn_zero
  | n + 1 => by
    have ih := weier b hb hb1 n
    unfold le at ih ⊢
    show P.nn (sub K (K.mul (sub K K.one (b n)) (prodM K b n)) (sub K K.one (K.add (sumA K b n) (b n))))
    rw [show sub K (K.mul (sub K K.one (b n)) (prodM K b n)) (sub K K.one (K.add (sumA K b n) (b n)))
        = K.add (K.mul (sub K K.one (b n)) (sub K (prodM K b n) (sub K K.one (sumA K b n))))
            (K.mul (b n) (sumA K b n)) by lib_ring]
    exact P.nn_add _ _ (P.nn_mul _ _ (hb1 n) ih) (P.nn_mul _ _ (hb n) (sumA_nn K P b hb n))

theorem mid_prod (z b : Nat → R) (hb : ∀ k, P.nn (b k)) (hb1 : ∀ k, P.nn (sub K K.one (b k)))
    (hone : P.nn K.one)
    (step : ∀ k, le K P (K.mul (z (k + 1)) (sub K K.one (b k))) (K.mul (K.add K.one (b k)) (z k))) :
    ∀ N, le K P (K.mul (z N) (prodM K b N)) (K.mul (z 0) (prodC K b N))
  | 0 => by
    unfold le; show P.nn (sub K (K.mul (z 0) K.one) (K.mul (z 0) K.one))
    rw [show sub K (K.mul (z 0) K.one) (K.mul (z 0) K.one) = K.zero by lib_ring]; exact P.nn_zero
  | N + 1 => by
    have ih := mid_prod z b hb hb1 hone step N
    have s := le_mul_left K P (prodM_nn K P b hb1 hone N) (step N)
    have t := le_mul_left K P (P.nn_add _ _ hone (hb N)) ih
    unfold le at s t ⊢
    show P.nn (sub K (K.mul (z 0) (K.mul (K.add K.one (b N)) (prodC K b N)))
      (K.mul (z (N + 1)) (K.mul (sub K K.one (b N)) (prodM K b N))))
    rw [show sub K (K.mul (z 0) (K.mul (K.add K.one (b N)) (prodC K b N)))
          (K.mul (z (N + 1)) (K.mul (sub K K.one (b N)) (prodM K b N)))
        = K.add (sub K (K.mul (K.add K.one (b N)) (K.mul (z 0) (prodC K b N)))
              (K.mul (K.add K.one (b N)) (K.mul (z N) (prodM K b N))))
            (sub K (K.mul (prodM K b N) (K.mul (K.add K.one (b N)) (z N)))
              (K.mul (prodM K b N) (K.mul (z (N + 1)) (sub K K.one (b N))))) by lib_ring]
    exact P.nn_add _ _ t s

/-- **Discrete Prodi–Serrin, Crank–Nicolson form.** -/
theorem serrin_mid (z b : Nat → R) (hb : ∀ k, P.nn (b k)) (hb1 : ∀ k, P.nn (sub K K.one (b k)))
    (hone : P.nn K.one) (hz : ∀ k, P.nn (z k))
    (step : ∀ k, le K P (K.mul (z (k + 1)) (sub K K.one (b k))) (K.mul (K.add K.one (b k)) (z k)))
    (N : Nat) (hS : P.nn (sub K K.one (sumA K b N))) :
    le K P (K.mul (z N) (K.mul (sub K K.one (sumA K b N)) (sub K K.one (sumA K b N)))) (z 0) := by
  -- z_N (1−S)(1−S) ≤ z_N Π(1−b) (1−S) ≤ z_0 Π(1+b) (1−S) ≤ z_0
  have w := le_mul_left K P (P.nn_mul _ _ (hz N) hS) (weier K P b hb hb1 N)
  have m := le_mul_left K P hS (mid_prod K P z b hb hb1 hone step N)
  have br := le_mul_left K P (hz 0) (bern K P b hb hone N)
  unfold le at w m br ⊢
  rw [show sub K (z 0) (K.mul (z N) (K.mul (sub K K.one (sumA K b N)) (sub K K.one (sumA K b N))))
      = K.add (K.add (sub K (K.mul (K.mul (z N) (sub K K.one (sumA K b N))) (prodM K b N))
              (K.mul (K.mul (z N) (sub K K.one (sumA K b N))) (sub K K.one (sumA K b N))))
            (sub K (K.mul (sub K K.one (sumA K b N)) (K.mul (z 0) (prodC K b N)))
              (K.mul (sub K K.one (sumA K b N)) (K.mul (z N) (prodM K b N)))))
          (sub K (K.mul (z 0) K.one) (K.mul (z 0) (K.mul (prodC K b N) (sub K K.one (sumA K b N))))) by lib_ring]
  exact P.nn_add _ _ (P.nn_add _ _ w m) br

end SerrinMid

#print axioms SerrinMid.prodM_nn
#print axioms SerrinMid.weier
#print axioms SerrinMid.mid_prod
#print axioms SerrinMid.serrin_mid

ns/SerrinRun.lean

lean_all/ns/SerrinRun.lean

/-
  ns/SerrinRun.lean — T37 cycle 158: the discrete Prodi–Serrin continuation bound for a whole run.

  Lean 4 core plus ns/YoungStep, ns/SerrinSum, lib/OField (inv, nn_inv), lib/RingTac. If every step obeys
      Z_{k+1} ≤ Z_k + h (c X_k r_k s_k − ν s_k²),   r_k² = Z_k,
  (X_k = ‖u_k‖_∞, s_k² = ‖Δu_k‖²), then with a_k = h c² X_k² / (2ν) and S_N = Σ_{k<N} a_k ≤ 1,
      Z_N (1 − S_N) ≤ Z_0 .
  The enstrophy of a run stays below Z_0/(1 − S) while (h c²/2ν) Σ_k ‖u_k‖²_∞ < 1 — the L²_t L^∞_x criterion.
-/
import ns.YoungStep
import ns.SerrinSum
import lib.OField

namespace SerrinRun
open DetChain LibRing LibCS LibGron LibOF SerrinDiscrete SerrinSum YoungStep

variable {R : Type} (K : CRing R) (P : Pos K)

def aRun (F : OF K P) (h c ν : R) (X : Nat → R) (k : Nat) : R :=
  K.mul (F.inv (K.add ν ν)) (K.mul h (K.mul (K.mul c c) (K.mul (X k) (X k))))

theorem step_div (F : OF K P) (Z Z' b ν : R) (hν : P.nn ν) (hν0 : K.add ν ν ≠ K.zero)
    (h : le K P (K.mul (K.add ν ν) Z') (K.mul (K.add (K.add ν ν) b) Z)) :
    le K P Z' (K.mul (K.add K.one (K.mul (F.inv (K.add ν ν)) b)) Z) := by
  have m := le_mul_left K P (nn_inv K P F _ (P.nn_add _ _ hν hν) hν0) h
  have hi := F.mul_inv _ hν0
  rw [show K.mul (F.inv (K.add ν ν)) (K.mul (K.add ν ν) Z') = K.mul (K.mul (K.add ν ν) (F.inv (K.add ν ν))) Z' by lib_ring,
      show K.mul (F.inv (K.add ν ν)) (K.mul (K.add (K.add ν ν) b) Z)
        = K.add (K.mul (K.mul (K.add ν ν) (F.inv (K.add ν ν))) Z) (K.mul (K.mul (F.inv (K.add ν ν)) b) Z) by lib_ring,
      hi] at m
  rw [show K.mul (K.add K.one (K.mul (F.inv (K.add ν ν)) b)) Z
      = K.add (K.mul K.one Z) (K.mul (K.mul (F.inv (K.add ν ν)) b) Z) by lib_ring,
    show Z' = K.mul K.one Z' by lib_ring]
  exact m

/-- **Discrete Prodi–Serrin bound for a run.** -/
theorem serrin_run (F : OF K P) (Z r s X : Nat → R) (h c ν : R) (hh : P.nn h) (hν : P.nn ν)
    (hν0 : K.add ν ν ≠ K.zero) (hr : ∀ k, K.mul (r k) (r k) = Z k) (hZ0 : P.nn (Z 0))
    (step : ∀ k, le K P (Z (k + 1))
      (K.add (Z k) (K.mul h (sub K (K.mul (K.mul (K.mul c (X k)) (r k)) (s k)) (K.mul ν (K.mul (s k) (s k)))))))
    (N : Nat) (hS : P.nn (sub K K.one (sumA K (aRun K P F h c ν X) N))) :
    le K P (K.mul (Z N) (sub K K.one (sumA K (aRun K P F h c ν X) N))) (Z 0) := by
  have hone := nn_one K P
  have ha : ∀ k, P.nn (aRun K P F h c ν X k) := fun k =>
    P.nn_mul _ _ (nn_inv K P F _ (P.nn_add _ _ hν hν) hν0)
      (P.nn_mul _ _ hh (P.nn_mul _ _ (P.nn_sq c) (P.nn_sq (X k))))
  exact serrin_sum K P Z (aRun K P F h c ν X) ha hone hZ0
    (fun k => step_div K P F _ _ _ ν hν hν0
      (young_step K P (Z k) (Z (k + 1)) h c (X k) (r k) (s k) ν hh hν (hr k) (step k))) N hS

end SerrinRun

#print axioms SerrinRun.step_div
#print axioms SerrinRun.serrin_run

ns/SerrinSum.lean

lean_all/ns/SerrinSum.lean

/-
  ns/SerrinSum.lean — T37 cycle 152: the discrete Prodi–Serrin continuation bound with a sum condition.

  Lean 4 core plus ns/SerrinDiscrete, lib/Gronwall, lib/RingTac. For a_k ≥ 0 and S_n = Σ_{k<n} a_k,
      Π_{k<n}(1 + a_k) · (1 − S_n) ≤ 1                         (no sign condition on 1 − S_n)
  and hence, if z_{k+1} ≤ (1 + a_k) z_k, z_0 ≥ 0 and S_N ≤ 1,
      z_N · (1 − S_N) ≤ z_0 .
  With a_k = h C ‖u_k‖²_∞ / ν: the enstrophy stays below z_0 / (1 − S) while h C Σ ‖u_k‖²_∞ / ν < 1.
-/
import ns.SerrinDiscrete

namespace SerrinSum
open DetChain LibRing LibCS LibGron SerrinDiscrete

variable {R : Type} (K : CRing R) (P : Pos K)

def sumA (a : Nat → R) : Nat → R
  | 0 => K.zero
  | n + 1 => K.add (sumA a n) (a n)

theorem sumA_nn (a : Nat → R) (ha : ∀ k, P.nn (a k)) : ∀ n, P.nn (sumA K a n)
  | 0 => P.nn_zero
  | n + 1 => P.nn_add _ _ (sumA_nn a ha n) (ha n)

theorem prodC_nn (a : Nat → R) (ha : ∀ k, P.nn (a k)) (hone : P.nn K.one) : ∀ n, P.nn (prodC K a n)
  | 0 => hone
  | n + 1 => P.nn_mul _ _ (P.nn_add _ _ hone (ha n)) (prodC_nn a ha hone n)

theorem bern (a : Nat → R) (ha : ∀ k, P.nn (a k)) (hone : P.nn K.one) :
    ∀ n, le K P (K.mul (prodC K a n) (sub K K.one (sumA K a n))) K.one
  | 0 => by
      unfold le
      rw [show sub K K.one (K.mul (prodC K a 0) (sub K K.one (sumA K a 0))) = K.zero by
        show sub K K.one (K.mul K.one (sub K K.one K.zero)) = K.zero; lib_ring]
      exact P.nn_zero
  | n + 1 => by
      have ih := bern a ha hone n
      unfold le at ih ⊢
      have hX := prodC_nn K P a ha hone n
      have hS := sumA_nn K P a ha n
      rw [show sub K K.one (K.mul (prodC K a (n + 1)) (sub K K.one (sumA K a (n + 1))))
          = K.add (K.add (sub K K.one (K.mul (prodC K a n) (sub K K.one (sumA K a n))))
                         (K.mul (a n) (K.mul (prodC K a n) (sumA K a n))))
                  (K.mul (K.mul (a n) (a n)) (prodC K a n)) by
        show sub K K.one (K.mul (K.mul (K.add K.one (a n)) (prodC K a n)) (sub K K.one (K.add (sumA K a n) (a n))))
          = _
        lib_ring]
      exact P.nn_add _ _ (P.nn_add _ _ ih (P.nn_mul _ _ (ha n) (P.nn_mul _ _ hX hS)))
        (P.nn_mul _ _ (P.nn_sq _) hX)

theorem serrin_sum (z a : Nat → R) (ha : ∀ k, P.nn (a k)) (hone : P.nn K.one) (hz0 : P.nn (z 0))
    (step : ∀ k, le K P (z (k + 1)) (K.mul (K.add K.one (a k)) (z k)))
    (N : Nat) (hS : P.nn (sub K K.one (sumA K a N))) :
    le K P (K.mul (z N) (sub K K.one (sumA K a N))) (z 0) := by
  have s := serrin K P z a ha hone step N
  have m := le_mul_left K P hS s
  have b := le_mul_left K P hz0 (bern K P a ha hone N)
  have m' : le K P (K.mul (z N) (sub K K.one (sumA K a N)))
      (K.mul (z 0) (K.mul (prodC K a N) (sub K K.one (sumA K a N)))) := by
    rw [show K.mul (z N) (sub K K.one (sumA K a N)) = K.mul (sub K K.one (sumA K a N)) (z N) by lib_ring,
        show K.mul (z 0) (K.mul (prodC K a N) (sub K K.one (sumA K a N)))
          = K.mul (sub K K.one (sumA K a N)) (K.mul (prodC K a N) (z 0)) by lib_ring]
    exact m
  have t := le_trans K P m' b
  rw [show K.mul (z 0) K.one = z 0 by lib_ring] at t
  exact t

end SerrinSum

#print axioms SerrinSum.sumA_nn
#print axioms SerrinSum.prodC_nn
#print axioms SerrinSum.bern
#print axioms SerrinSum.serrin_sum

ns/SerrinWindows.lean

lean_all/ns/SerrinWindows.lean

/-
  ns/SerrinWindows.lean — T37 cycle 164: continuation by windows of half budget.

  Lean 4 core plus ns/SerrinSum (serrin_sum, sumA), lib/Gronwall (gronwall_hom, pw), lib/RingTac.
    window    z ≥ 0, z_{k+1} ≤ (1 + a_k) z_k, 2 S_L ≤ 1  ⇒  z_L ≤ 2 z_0
    windows   if every window [jL, (j + 1)L) has budget 2 Σ a ≤ 1, then z_{JL} ≤ 2^J z_0
  With a_k = hc²‖u_k‖²_∞/(2ν): if ‖u‖_∞ is bounded on the run, windows of a fixed length L have budget ≤ 1/2 and
  the enstrophy grows at most like 2^{N/L} — Prodi–Serrin continuation in discrete form.
-/
import ns.SerrinSum

namespace SerrinWindows
open DetChain LibRing LibCS LibGron SerrinDiscrete SerrinSum

variable {R : Type} (K : CRing R) (P : Pos K)

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

theorem window (z a : Nat → R) (ha : ∀ k, P.nn (a k)) (hone : P.nn K.one) (hz : ∀ k, P.nn (z k))
    (step : ∀ k, le K P (z (k + 1)) (K.mul (K.add K.one (a k)) (z k)))
    (L : Nat) (hS : P.nn (sub K K.one (K.add (sumA K a L) (sumA K a L)))) :
    le K P (z L) (K.mul (two K) (z 0)) := by
  have hS1 : P.nn (sub K K.one (sumA K a L)) := by
    rw [show sub K K.one (sumA K a L) = K.add (sub K K.one (K.add (sumA K a L) (sumA K a L))) (sumA K a L) by lib_ring]
    exact P.nn_add _ _ hS (sumA_nn K P a ha L)
  have s := serrin_sum K P z a ha hone (hz 0) step L hS1
  -- z_L ≤ 2 z_L (1 − S) ≤ 2 z_0
  have m := le_mul_left K P (P.nn_add _ _ hone hone) s
  have g : le K P (z L) (K.mul (two K) (K.mul (z L) (sub K K.one (sumA K a L)))) := by
    unfold le
    rw [show sub K (K.mul (two K) (K.mul (z L) (sub K K.one (sumA K a L)))) (z L)
        = K.mul (z L) (sub K K.one (K.add (sumA K a L) (sumA K a L))) by unfold two; lib_ring]
    exact P.nn_mul _ _ (hz L) hS
  exact le_trans K P g m

/-- **Windows of half budget.** -/
theorem windows (z a : Nat → R) (ha : ∀ k, P.nn (a k)) (hone : P.nn K.one) (hz : ∀ k, P.nn (z k))
    (step : ∀ k, le K P (z (k + 1)) (K.mul (K.add K.one (a k)) (z k))) (L : Nat)
    (hS : ∀ j, P.nn (sub K K.one (K.add (sumA K (fun k => a (j * L + k)) L) (sumA K (fun k => a (j * L + k)) L))))
    (J : Nat) : le K P (z (J * L)) (K.mul (pw K (two K) J) (z 0)) := by
  have hw : ∀ j, le K P (z ((j + 1) * L)) (K.mul (two K) (z (j * L))) := fun j => by
    have w := window K P (fun k => z (j * L + k)) (fun k => a (j * L + k)) (fun k => ha _) hone (fun k => hz _)
      (fun k => step (j * L + k)) L (hS j)
    rw [show (j + 1) * L = j * L + L by rw [Nat.succ_mul]]
    exact w
  have g := gronwall_hom K P (fun j => z (j * L)) (two K) (P.nn_add _ _ hone hone) hw J
  rw [show z (0 * L) = z 0 by rw [Nat.zero_mul]] at g
  exact g

end SerrinWindows

#print axioms SerrinWindows.window
#print axioms SerrinWindows.windows

ns/SignedSum.lean

lean_all/ns/SignedSum.lean

/-
  ns/SignedSum.lean — T37 cycle 251 (goal G9): the triangle inequality for sums, in squared form, every ordered field.

  Lean 4 core plus ns/TransferBound (le_of_sq_signed), lib/OField, lib/RingTac.
    mul_le_of_sq   t² ≤ m², T² ≤ M², m, M ≥ 0, mM ≠ 0  ⇒  tT ≤ mM
    signed_sum     t_i² ≤ m_i², m_i ≥ 0, m_i ≠ 0  ⇒  (Σ t)² ≤ (Σ m)²   (prod_ne, sum_ne: no zero products)
  With t = a signed triad term and m its magnitude (TriadBound: t² ≤ |u|²|k|²|v|²|w|²), this is the domination
  T² ≤ (Σ a S)² that TransferBound takes as input.
-/
import ns.TransferBound

namespace SignedSum
open DetChain NSChain LibRing LibCS LibGron LibOF TransferBound

variable {R : Type} (K : CRing R) {I : Type}

theorem mul_le_of_sq (P : Pos K) (F : OF K P) (t T m M : R) (hm : P.nn m) (hM : P.nn M)
    (h0 : K.mul m M ≠ K.zero)
    (ht : le K P (K.mul t t) (K.mul m m)) (hT : le K P (K.mul T T) (K.mul M M)) :
    le K P (K.mul t T) (K.mul m M) := by
  apply le_of_sq_signed K P F _ _ (P.nn_mul _ _ hm hM) h0
  -- (tT)² = t²T² ≤ m²T² ≤ m²M²
  have a := le_mul_left K P (P.nn_sq T) ht
  have b := le_mul_left K P (P.nn_sq m) hT
  unfold le at a b ⊢
  rw [show sub K (K.mul (K.mul m M) (K.mul m M)) (K.mul (K.mul t T) (K.mul t T))
      = K.add (sub K (K.mul (K.mul m m) (K.mul M M)) (K.mul (K.mul m m) (K.mul T T)))
          (sub K (K.mul (K.mul T T) (K.mul m m)) (K.mul (K.mul T T) (K.mul t t))) by lib_ring]
  exact P.nn_add _ _ b a

theorem prod_ne (P : Pos K) (F : OF K P) (x y : R) (hx : x ≠ K.zero) (hy : y ≠ K.zero) : K.mul x y ≠ K.zero := by
  intro h
  apply hy
  have hi := F.mul_inv x hx
  calc y = K.mul (K.mul x (F.inv x)) y := by rw [hi]; lib_ring
    _ = K.mul (F.inv x) (K.mul x y) := by lib_ring
    _ = K.zero := by rw [h]; lib_ring

theorem sum_ne (P : Pos K) (F : OF K P) (m : I → R) (hm : ∀ i, P.nn (m i)) (h0 : ∀ i, m i ≠ K.zero) :
    ∀ (j : I) (l : List I), lsum K m (j :: l) ≠ K.zero := by
  intro j l h
  have hr : P.nn (lsum K m l) := by
    have g : ∀ l : List I, P.nn (lsum K m l) := by
      intro l; induction l with
      | nil => exact P.nn_zero
      | cons x l ihl => exact P.nn_add _ _ (hm x) ihl
    exact g l
  apply h0 j
  apply F.antisymm _ (hm j)
  have e : K.neg (m j) = lsum K m l := by
    have h' : K.add (m j) (lsum K m l) = K.zero := h
    calc K.neg (m j) = K.add (K.neg (m j)) (K.add (m j) (lsum K m l)) := by rw [h']; lib_ring
      _ = lsum K m l := by lib_ring
  rw [e]; exact hr

theorem signed_sum (P : Pos K) (F : OF K P) (t m : I → R) (hm : ∀ i, P.nn (m i))
    (h0 : ∀ i, m i ≠ K.zero) (ht : ∀ i, le K P (K.mul (t i) (t i)) (K.mul (m i) (m i))) :
    ∀ l : List I, le K P (K.mul (lsum K t l) (lsum K t l)) (K.mul (lsum K m l) (lsum K m l))
  | [] => by
    unfold le; show P.nn (sub K (K.mul K.zero K.zero) (K.mul K.zero K.zero))
    rw [show sub K (K.mul K.zero K.zero) (K.mul K.zero K.zero) = K.zero by lib_ring]; exact P.nn_zero
  | i :: l => by
    have ih := signed_sum P F t m hm h0 ht l
    have hM : P.nn (lsum K m l) := by
      have g : ∀ l : List I, P.nn (lsum K m l) := by
        intro l; induction l with
        | nil => exact P.nn_zero
        | cons x l ihl => exact P.nn_add _ _ (hm x) ihl
      exact g l
    have cross : le K P (K.mul (t i) (lsum K t l)) (K.mul (m i) (lsum K m l)) := by
      cases l with
      | nil =>
        unfold le; show P.nn (sub K (K.mul (m i) K.zero) (K.mul (t i) K.zero))
        rw [show sub K (K.mul (m i) K.zero) (K.mul (t i) K.zero) = K.zero by lib_ring]; exact P.nn_zero
      | cons j l' =>
        exact mul_le_of_sq K P F _ _ _ _ (hm i) hM (prod_ne K P F _ _ (h0 i) (sum_ne K P F m hm h0 j l')) (ht i) ih
    unfold le at ih cross ⊢
    show P.nn (sub K (K.mul (K.add (m i) (lsum K m l)) (K.add (m i) (lsum K m l)))
      (K.mul (K.add (t i) (lsum K t l)) (K.add (t i) (lsum K t l))))
    rw [show sub K (K.mul (K.add (m i) (lsum K m l)) (K.add (m i) (lsum K m l)))
        (K.mul (K.add (t i) (lsum K t l)) (K.add (t i) (lsum K t l)))
      = K.add (K.add (sub K (K.mul (m i) (m i)) (K.mul (t i) (t i)))
          (sub K (K.mul (lsum K m l) (lsum K m l)) (K.mul (lsum K t l) (lsum K t l))))
          (K.add (sub K (K.mul (m i) (lsum K m l)) (K.mul (t i) (lsum K t l)))
                 (sub K (K.mul (m i) (lsum K m l)) (K.mul (t i) (lsum K t l)))) by lib_ring]
    exact P.nn_add _ _ (P.nn_add _ _ (ht i) ih) (P.nn_add _ _ cross cross)

end SignedSum

#print axioms SignedSum.mul_le_of_sq
#print axioms SignedSum.prod_ne
#print axioms SignedSum.sum_ne
#print axioms SignedSum.signed_sum

ns/SmallAlpha.lean

lean_all/ns/SmallAlpha.lean

/-
  ns/SmallAlpha.lean — T37 cycle 1187 (goal G768, piece 1): the Lipschitz rate of StepCauchy under uniform bounds.
  If Z(u_t) ≤ Zb and A(v_t) ≤ Ab for all t, then
    alpha_le   α_t = 2·2ι²C₁·Z(u_t) + 2ι²·135·A(v_t) ≤ 2·2ι²C₁·Zb + 2ι²·135·Ab
  over an ordered field (the hypothesis hαle of StepCauchy.step_cauchy with an explicit α; Zb, Ab from the small-data
  enstrophy and H² bounds EnstGalerkin, EnstH2).

  Lean 4 core plus ns/StepCauchy (alpha).
-/
import ns.StepCauchy

namespace SmallAlpha
open DetChain NSChain DyadicSmallGlobal LibRing DyadicLarge FourierGalerkin GalerkinForce ForceBound ForceSob
  ModeCount CubeCount ForceUniform NormCompare EnstStep EnstGlobal EnstGalerkin ForceTwoScale ForceBilin EnstDiff
  ForceDiff EnstGronwall LatticeSum EnstLipschitz StepCauchy

section Gen
variable {R : Type} (K : CRing R) (O : OrdR K) {I : Type}
variable (inv : R → R) (mul_inv : ∀ y, y ≠ K.zero → K.mul y (inv y) = K.one) (h10 : K.one ≠ K.zero)

include mul_inv h10 in
theorem alpha_le (ι Zb Ab : R) (Rc : Nat) (l : List I) (kz : I → Mode) (u v : Nat → I → V3 R) (t : Nat)
    (hZ : Le K O.toCone (Zs K l kz u t) Zb) (hA : Le K O.toCone (As K l kz v t) Ab) :
    Le K O.toCone (alpha K inv ι Rc l kz u v t)
      (K.add (K.mul (two K) (K.mul (K.mul (two K) (K.mul ι ι)) (K.mul (C1 K inv Rc) Zb)))
        (K.mul (two K) (K.mul (K.mul ι ι) (K.mul (castN K 135) Ab)))) := by
  have n2 : O.nn (two K) := O.nn_add _ _ (RatModel.nn_one' K O.toCone) (RatModel.nn_one' K O.toCone)
  have nC1 : O.nn (C1 K inv Rc) := O.nn_add _ _ (O.nn_mul _ _ (RatModel.nn_cast K O.toCone _) (RatModel.nn_one' K O.toCone))
    (O.nn_mul _ _ (RatModel.nn_cast K O.toCone _) (iv_nn K O inv mul_inv h10 Rc))
  exact le_add' K O.toCone _ _ _ _
    (le_mul_nn K O.toCone _ _ _ n2 (le_mul_nn K O.toCone _ _ _ (O.nn_mul _ _ n2 (O.nn_sq ι)) (le_mul_nn K O.toCone _ _ _ nC1 hZ)))
    (le_mul_nn K O.toCone _ _ _ n2 (le_mul_nn K O.toCone _ _ _ (O.nn_sq ι) (le_mul_nn K O.toCone _ _ _ (RatModel.nn_cast K O.toCone 135) hA)))

end Gen

end SmallAlpha

#print axioms SmallAlpha.alpha_le

ns/SmallData.lean

lean_all/ns/SmallData.lean

/-
  ns/SmallData.lean — T37 cycle 74: the small-data global comparison (discrete).

  Lean 4 core plus NSChain (Cone, Le) and ns/Riccati (le_add, le_mul_left), lib/RingTac.
  A nonnegative sequence with
      z_{k+1} ≤ z_k − a z_k + b z_k²      (a = hνλ₁ dissipation, b = hC the transfer constant)
  and b z_0 ≤ a is non-increasing for all k:  z_{k+1} ≤ z_k ≤ z_0.
  This is the shape of the 3D small-data global theorem (Z_0 ≤ ν²λ₁/C): once the transfer bound
  Z' ≤ −νλ₁Z + CZ² holds with C uniform in the truncation (R1), every truncation with small enstrophy
  stays small forever.
    small_step    b z ≤ a, z ≥ 0  ⇒  z − a z + b z² ≤ z
    small_global  z_k ≤ z_0 and b z_k ≤ a for every k
-/
import ns.Riccati

namespace SmallData
open DetChain NSChain LibRing

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

theorem small_step (P : Cone K) (z a b : R) (hz : P.nn z) (h : Le K P (K.mul b z) a) :
    Le K P (K.add (sub K z (K.mul a z)) (K.mul b (K.mul z z))) z := by
  cases h with
  | intro d hd =>
    refine ⟨K.mul d z, P.nn_mul _ _ hd.1 hz, ?_⟩
    rw [← hd.2]; lib_ring

theorem small_global (P : Cone K) (z : Nat → R) (a b : R) (hz : ∀ k, P.nn (z k)) (hb : P.nn b)
    (step : ∀ k, Le K P (z (k + 1)) (K.add (sub K (z k) (K.mul a (z k))) (K.mul b (K.mul (z k) (z k)))))
    (h0 : Le K P (K.mul b (z 0)) a) :
    ∀ k, Le K P (z k) (z 0) ∧ Le K P (K.mul b (z k)) a
  | 0 => ⟨le_refl' K P _, h0⟩
  | k + 1 => by
    have ⟨ih1, ih2⟩ := small_global P z a b hz hb step h0 k
    have dec : Le K P (z (k + 1)) (z k) := le_trans' K P _ _ _ (step k) (small_step K P (z k) a b (hz k) ih2)
    exact ⟨le_trans' K P _ _ _ dec ih1,
      le_trans' K P _ _ _ (Riccati.le_mul_left K P b _ _ hb dec) ih2⟩

end SmallData

#print axioms SmallData.small_step
#print axioms SmallData.small_global

ns/Sobolev.lean

lean_all/ns/Sobolev.lean

/-
  ns/Sobolev.lean — T37 cycle 97: the discrete Sobolev embedding H² ⊂ L^∞ (Fourier side) in 3D, with a
  constant independent of the truncation.

  Lean 4 core plus ns/Basel (cN, lattice), ns/WeightedCS (weighted_cs), ns/TriadSum (lsum_le, lsum_nn).
  Modes are grouped in cube shells: shell n (|k|∞ = n + 1) is a list of at most 26(n+1)² modes, each with
  weight ω_i = |k_i|^{-4} ≤ u_n⁴ (u_n = 1/(n+1)). Then
      Σ_{n≤N} Σ_{i∈shell n} ω_i ≤ 52                                       shell_weight
      (Σ_i x_i y_i)² ≤ W · Σ_i β_i   when x_i² ≤ ω_i, y_i² ≤ β_i, Σω ≤ W            sobolev
  (with |a_i| = |k_i|^{-2}·|k_i|²|a_i|, β = |k|⁴a² and W = 52 from shell_weight)
  i.e. ‖u‖_∞ ≤ Σ|û_k| ≤ √52 ‖Δu‖ for every truncation (the shell count is LatticeSum.shell_count).
-/
import ns.Basel
import ns.WeightedCS

namespace Sobolev
open DetChain NSChain LibRing Basel TriadSum

variable {R : Type} (K : CRing R) {I : Type}

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; lib_ring
  | b + 1 => by
    show K.add (cN K (a + b)) K.one = K.add (cN K a) (K.add (cN K b) K.one)
    rw [cN_add a b]; lib_ring

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; lib_ring
  | b + 1 => by
    show cN K (a * b + a) = K.mul (cN K a) (K.add (cN K b) K.one)
    rw [cN_add, cN_mul a b]; lib_ring

theorem cN_le (P : Cone K) (a b : Nat) (h : a ≤ b) : Le K P (cN K a) (cN K b) := by
  have ⟨c, hc⟩ := Nat.le.dest h
  exact ⟨cN K c, cN_nn K P (Basel.one_nn K P) c, by rw [← hc, cN_add]⟩

/-- Σ over a list ≤ length · bound. -/
theorem lsum_le_len (P : Cone K) (f : I → R) (b : R) (hb : P.nn b) :
    ∀ l : List I, (∀ i, List.Mem i l → Le K P (f i) b) → Le K P (lsum K f l) (K.mul (cN K l.length) b)
  | [], _ => ⟨K.zero, P.nn_zero, by show K.add K.zero K.zero = K.mul K.zero b; lib_ring⟩
  | i :: l, h => by
    have ih := lsum_le_len P f b hb l (fun j hj => h j (List.Mem.tail i hj))
    have hi := h i (List.Mem.head l)
    have s := Riccati.le_add K P _ _ _ _ hi ih
    refine le_trans' K P _ _ _ s ⟨K.zero, P.nn_zero, ?_⟩
    show K.add (K.add b (K.mul (cN K l.length) b)) K.zero = K.mul (K.add (cN K l.length) K.one) b
    lib_ring

/-- **Shell weight**: Σ_{n≤N} Σ_{i∈shell n} ω_i ≤ 52. -/
theorem shell_weight (P : Cone K) (u : Nat → R) (hu : ∀ n, K.mul (cN K (n + 1)) (u n) = K.one)
    (shell : Nat → List I) (ω : I → R) (N : Nat)
    (hlen : ∀ n, (shell n).length ≤ 26 * ((n + 1) * (n + 1)))
    (hw : ∀ n i, List.Mem i (shell n) → Le K P (ω i) (K.mul (K.mul (u n) (u n)) (K.mul (u n) (u n)))) :
    Le K P (sumN K (fun n => lsum K ω (shell n)) (N + 1)) (K.mul (cN K 26) (K.add K.one K.one)) := by
  have u4 : ∀ n, P.nn (K.mul (K.mul (u n) (u n)) (K.mul (u n) (u n))) := fun n => P.nn_mul _ _ (P.nn_sq _) (P.nn_sq _)
  have step : ∀ n, Le K P (lsum K ω (shell n))
      (K.mul (cN K 26) (K.mul (K.mul (cN K (n + 1)) (cN K (n + 1))) (K.mul (K.mul (u n) (u n)) (K.mul (u n) (u n))))) := by
    intro n
    refine le_trans' K P _ _ _ (lsum_le_len K P ω _ (u4 n) (shell n) (hw n)) ?_
    have c := cN_le K P _ _ (hlen n)
    rw [cN_mul, cN_mul] at c
    have := Riccati.le_mul_left K P _ _ _ (u4 n) c
    refine le_trans' K P _ _ _ ⟨K.zero, P.nn_zero, by lib_ring⟩ (le_trans' K P _ _ _ this ⟨K.zero, P.nn_zero, by lib_ring⟩)
  have tot : ∀ M, Le K P (sumN K (fun n => lsum K ω (shell n)) M)
      (sumN K (fun n => K.mul (cN K 26) (K.mul (K.mul (cN K (n + 1)) (cN K (n + 1)))
        (K.mul (K.mul (u n) (u n)) (K.mul (u n) (u n))))) M) := by
    intro M
    induction M with
    | zero => exact le_refl' K P _
    | succ M ih => exact Riccati.le_add K P _ _ _ _ ih (step M)
  exact le_trans' K P _ _ _ (tot (N + 1)) (lattice K P u hu N)

/-- **Sobolev, Fourier side**: (Σ x_i y_i)² ≤ W·Σβ when x_i² ≤ ω_i, y_i² ≤ β_i and Σω ≤ W. -/
theorem sobolev (P : Cone K) (x y ω β : I → R) (l : List I) (W : R)
    (hx : ∀ i, Le K P (SumCS.sq K (x i)) (ω i)) (hy : ∀ i, Le K P (SumCS.sq K (y i)) (β i))
    (hβ : ∀ i, P.nn (β i)) (hW : Le K P (lsum K ω l) W) :
    Le K P (SumCS.sq K (lsum K (fun i => K.mul (x i) (y i)) l)) (K.mul W (lsum K β l)) := by
  have w := WeightedCS.weighted_cs K P x y ω β l hx hy hβ
  have m := Riccati.le_mul_left K P _ _ _ (lsum_nn K P β hβ l) hW
  rw [K.mul_comm (lsum K β l), K.mul_comm (lsum K β l)] at m
  exact le_trans' K P _ _ _ w m

end Sobolev

#print axioms Sobolev.cN_add
#print axioms Sobolev.cN_mul
#print axioms Sobolev.cN_le
#print axioms Sobolev.lsum_le_len
#print axioms Sobolev.shell_weight
#print axioms Sobolev.sobolev

ns/SplitK.lean

lean_all/ns/SplitK.lean

/-
  ns/SplitK.lean — T37 cycle 1113 (goal G717, piece c1): the Young splitting used to accumulate the step-size defect,
  over any commutative ring with a nonnegative cone (any k, weights μ_r ≥ 0):
    split_k      k(X + Y)² ≤ k(1 + k)X² + (1 + k)Y²        ((1+k)(kX² + Y²) − k(X+Y)² = (kX − Y)²)
    split_k_sum  k·Σ_r μ_r|X_r + Y_r|² ≤ k(1 + k)·Σ_r μ_r|X_r|² + (1 + k)·Σ_r μ_r|Y_r|²
  With X = (Lipschitz part), Y = (defect D_n): the error recursion k·E_{n+1} ≤ k(1+k)·L·E_n + (1+k)·|D_n|².

  Lean 4 core plus ns/EnstDiff (add3), ns/ForceBound (lsum_le), lib/RingTac.
-/
import ns.EnstDiff

namespace SplitK
open DetChain NSChain LibRing DyadicLarge FourierGalerkin ForceBound EnstDiff

section Gen
variable {R : Type} (K : CRing R) (P : Cone K) {I : Type}

theorem split_k (k X Y : R) :
    Le K P (K.mul k (K.mul (K.add X Y) (K.add X Y)))
      (K.add (K.mul (K.mul k (K.add K.one k)) (K.mul X X)) (K.mul (K.add K.one k) (K.mul Y Y))) :=
  ⟨K.mul (K.add (K.mul k X) (K.neg Y)) (K.add (K.mul k X) (K.neg Y)), P.nn_sq _, by lib_ring⟩

theorem split_k3 (k : R) (X Y : V3 R) :
    Le K P (K.mul k (dot3 K (add3 K X Y) (add3 K X Y)))
      (K.add (K.mul (K.mul k (K.add K.one k)) (dot3 K X X)) (K.mul (K.add K.one k) (dot3 K Y Y))) := by
  have cx := split_k K P k X.x Y.x
  have cy := split_k K P k X.y Y.y
  have cz := split_k K P k X.z Y.z
  have s := le_add' K P _ _ _ _ (le_add' K P _ _ _ _ cx cy) cz
  refine le_trans' K P _ _ _ ⟨K.zero, P.nn_zero, ?_⟩ (le_trans' K P _ _ _ s ⟨K.zero, P.nn_zero, ?_⟩)
  · show K.add (K.mul k (K.add (K.add (K.mul (K.add X.x Y.x) (K.add X.x Y.x)) (K.mul (K.add X.y Y.y) (K.add X.y Y.y)))
      (K.mul (K.add X.z Y.z) (K.add X.z Y.z)))) K.zero = _
    lib_ring
  · show _ = K.add (K.mul (K.mul k (K.add K.one k)) (K.add (K.add (K.mul X.x X.x) (K.mul X.y X.y)) (K.mul X.z X.z)))
      (K.mul (K.add K.one k) (K.add (K.add (K.mul Y.x Y.x) (K.mul Y.y Y.y)) (K.mul Y.z Y.z)))
    lib_ring

theorem split_k_sum (l : List I) (k : R) (μ : I → R) (hμ : ∀ r, P.nn (μ r)) (X Y : I → V3 R) :
    Le K P (K.mul k (lsum K (fun r => K.mul (μ r) (dot3 K (add3 K (X r) (Y r)) (add3 K (X r) (Y r)))) l))
      (K.add (K.mul (K.mul k (K.add K.one k)) (lsum K (fun r => K.mul (μ r) (dot3 K (X r) (X r))) l))
        (K.mul (K.add K.one k) (lsum K (fun r => K.mul (μ r) (dot3 K (Y r) (Y r))) l))) := by
  have t := lsum_le K P _ _ (fun r => le_mul_nn K P (μ r) _ _ (hμ r) (split_k3 K P k (X r) (Y r))) l
  refine le_trans' K P _ _ _ ⟨K.zero, P.nn_zero, ?_⟩ (le_trans' K P _ _ _ t ⟨K.zero, P.nn_zero, ?_⟩)
  · rw [add_zero', ← lsum_mul_left]
    exact lsum_ext K _ _ (fun r => by lib_ring) l
  · rw [add_zero', ← lsum_mul_left, ← lsum_mul_left, ← lsum_add]
    exact lsum_ext K _ _ (fun r => by lib_ring) l

end Gen

end SplitK

#print axioms SplitK.split_k
#print axioms SplitK.split_k3
#print axioms SplitK.split_k_sum

ns/SqRoot.lean

lean_all/ns/SqRoot.lean

/-
  ns/SqRoot.lean — T37 cycle 853 (goal G562): square bounds give value bounds, over any ordered ring without zero
  divisors (ℚ included):  A ≥ 0, a² ≤ A²  ⇒  a ≤ A.
  Used to turn the energy bound a_n² ≤ 2E_0 ≤ A² into the mode bound a_{n+1} ≤ A required by rate_bound (uniform
  remainder ρ in the CFL-version weighted bound).

  Lean 4 core plus ns/YoungM (pos_cancel), ns/GoodModesQ, lib/RingTac.
    le_of_sq_le   the statement
-/
import ns.YoungM

namespace SqRoot
open DetChain NSChain DyadicSmallGlobal LibRing DyadicLarge YoungM

section Gen
variable {R : Type} (K : CRing R) (O : OrdR K)
include O

theorem le_of_sq_le (a A : R) (hA : O.nn A) (h : Le K O.toCone (K.mul a a) (K.mul A A)) : Le K O.toCone a A := by
  cases O.total a A with
  | inl l => exact l
  | inr l =>
    -- A ≤ a: write a = A + d, d ≥ 0; then a² = A² + d(2A + d) ≤ A² forces d(2A + d) ≤ 0, so d(2A + d) = 0
    cases l with
    | intro d hd =>
      have ha : a = K.add A d := hd.2.symm
      cases h with
      | intro e he =>
        -- (A + d)² + e = A²  ⇒  d(2A + d) + e = 0
        have z : K.add (K.mul d (K.add (K.add A A) d)) e = K.zero := by
          have t : K.add (K.mul d (K.add (K.add A A) d)) e
              = K.add (K.add (K.mul a a) e) (K.neg (K.mul A A)) := by rw [ha]; lib_ring
          rw [t, he.2]; lib_ring
        have nn1 : O.nn (K.mul d (K.add (K.add A A) d)) := O.nn_mul _ _ hd.1 (O.nn_add _ _ (O.nn_add _ _ hA hA) hd.1)
        have z1 : K.mul d (K.add (K.add A A) d) = K.zero := nn_sum_zero K O _ _ nn1 he.1 z
        -- d = 0 or 2A + d = 0; in the second case d = 0 too (both ≥ 0)
        have d0 : d = K.zero := by
          cases O.noZD _ _ z1 with
          | inl q => exact q
          | inr q =>
            have : K.add (K.add A A) d = K.zero := q
            exact (nn_sum_zero K O _ _ hd.1 (O.nn_add _ _ hA hA) (by rw [K.add_comm]; exact this))
        rw [ha, d0]; exact ⟨K.zero, O.nn_zero, by lib_ring⟩

end Gen

end SqRoot

#print axioms SqRoot.le_of_sq_le

ns/StateRate.lean

lean_all/ns/StateRate.lean

/-
  ns/StateRate.lean — T37 cycle 856 (goal G565): the rate bound at any state of energy ≤ A², for the concrete
  supercritical model over any ordered ring without zero divisors (ℚ included). With w = k = 4ⁿ, c = M = 2ⁿ, ν = 3,
  A = m·1, a_0 = 0, a_{N+1} = 0 and Σ_{n<N} a_{n+1}² ≤ A²:
    Σ_{n<N} 4^{n+1} a_{n+1}(−3·4^{n+1} a_{n+1} + dyN(c, a)_n) ≤ B_head(A², A, n₀ = m).
  This is SemiWDy's rate hypothesis at every step of a scheme whose energy stays ≤ A² (e.g. A² ≥ 2E₀ with
  SemiDyadic), giving a uniform remainder for the CFL-version weighted bound.

  Lean 4 core plus ns/RatModel (P2, P4, crit_g, nn_qq_g, cast lemmas), ns/SqRoot (le_of_sq_le),
  ns/DyCN (mode_E), ns/DyadicLarge (rate_bound).
-/
import ns.SqRoot
import ns.DyCN
import ns.RatModel

namespace StateRate
open DetChain NSChain DyadicSmallGlobal LibRing DyadicLarge RatModel SqRoot DyCN

theorem state_rate {R : Type} (K : CRing R) (O : OrdR K) (a : Nat → R) (m N : Nat)
    (h0 : a 0 = K.zero) (hN : ∀ i, N + 1 ≤ i → a i = K.zero)
    (hE : Le K O.toCone (sumN K (fun n => K.mul (a (n + 1)) (a (n + 1))) N) (K.mul (castN K m) (castN K m))) :
    Le K O.toCone (sumN K (fun n => K.mul (P4 K (n + 1)) (K.mul (a (n + 1))
        (K.add (K.neg (K.mul (castN K 3) (K.mul (P4 K (n + 1)) (a (n + 1))))) (dyN K (P2 K) a n)))) N)
      (sumN K (fun n => if n < m then K.mul (qq K (P4 K) (P2 K) n) (K.mul (K.mul (castN K m) (castN K m)) (castN K m)) else K.zero) N) := by
  have hA : O.nn (castN K m) := nn_cast K O.toCone m
  have mE := mode_E K O.toCone a N h0 hN
  have mEA : ∀ i, Le K O.toCone (K.mul (a i) (a i)) (K.mul (castN K m) (castN K m)) :=
    fun i => le_trans' K O.toCone _ _ _ (mE i) hE
  have aA : ∀ i, Le K O.toCone (a i) (castN K m) := fun i => SqRoot.le_of_sq_le K O (a i) (castN K m) hA (mEA i)
  exact rate_bound K O.toCone (P4 K) (P2 K) (P4 K) a (P2 K) (castN K 3) (castN K m) (K.mul (castN K m) (castN K m)) m N
    h0 (hN (N + 1) (Nat.le_refl _)) (nn_qq_g K O.toCone) (fun n _ => crit_g K n)
    (fun n hn => le_trans' K O.toCone _ _ _ (aA (n + 1))
      (le_trans' K O.toCone _ _ _ (cast_mono K O.toCone m n hn) (cast_le_p2 K O.toCone n)))
    (fun n => aA (n + 1)) hA mEA
    (fun n => O.nn_mul _ _ (nn_cast K O.toCone 3) (O.nn_mul _ _ (nn_pw K O.toCone _ (nn_cast K O.toCone 4) n)
      (O.nn_mul _ _ (nn_pw K O.toCone _ (nn_cast K O.toCone 4) n) (O.nn_sq _))))

end StateRate

#print axioms StateRate.state_rate

ns/StepCauchy.lean

lean_all/ns/StepCauchy.lean

/-
  ns/StepCauchy.lean — T37 cycle 1123 (goal G717d): the step-size Cauchy estimate for the implicit-viscosity
  Fourier–Galerkin scheme, over an ordered field. Let a be the scheme with step 2k, b the scheme with step k, and for
  each n let c n be the k-scheme started at a_n (c n 0 = a n). Measure errors in Qe(u, v) = νZ(u − v) + kν²A(u − v).
    split_Qe      k·Qe(u, v) ≤ k(1 + k)·Qe(c, v) + (1 + k)·Qe(u, c)              (SplitK)
    step_cauchy   if the local defect Qe(a_{n+1}, c n 2) ≤ k³C (DefectSum/DefectOrder), the k-scheme pairs satisfy
                  the hypotheses of EnstLipschitz with α_t ≤ α, and Qe(a_0, b_0) = 0, then for every n
                    Qe(a_n, b_{2n}) ≤ gⁿ·(n·(1 + k)k²C),   g = (1 + k)(ν + kα)²ν⁻²,
  i.e. O(k) on any interval nk ≤ T with a factor independent of the truncation (EnstLipschitz with T = 2, LipG,
  ErrRec.err_bound).

  Lean 4 core plus ns/EnstLipschitz, ns/SplitK, ns/ErrRec, ns/LipG.
-/
import ns.EnstLipschitz
import ns.SplitK
import ns.ErrRec
import ns.LipG

namespace StepCauchy
open DetChain NSChain DyadicSmallGlobal LibRing DyadicLarge FourierGalerkin GalerkinForce ForceBound ForceSob
  ModeCount CubeCount ForceUniform NormCompare EnstStep EnstGlobal EnstGalerkin ForceTwoScale ForceBilin EnstDiff
  ForceDiff EnstGronwall LatticeSum EnstLipschitz SplitK ErrRec LipG

section Gen
variable {R : Type} (K : CRing R) (O : OrdR K) {I : Type}
variable (inv : R → R) (mul_inv : ∀ y, y ≠ K.zero → K.mul y (inv y) = K.one) (h10 : K.one ≠ K.zero)

def Qe (ν k : R) (l : List I) (kz : I → Mode) (u v : I → V3 R) : R :=
  K.add (K.mul ν (lsum K (fun r => K.mul (wt K kz r) (dot3 K (dF K u v r) (dF K u v r))) l))
    (K.mul (K.mul k (K.mul ν ν)) (lsum K (fun r => K.mul (K.mul (wt K kz r) (wt K kz r)) (dot3 K (dF K u v r) (dF K u v r))) l))

theorem dF_split (u c v : I → V3 R) (r : I) : dF K u v r = add3 K (dF K c v r) (dF K u c r) := by
  show sub3 K (u r) (v r) = add3 K (sub3 K (c r) (v r)) (sub3 K (u r) (c r))
  apply v3_ext
  · show K.add (u r).x (K.neg (v r).x) = K.add (K.add (c r).x (K.neg (v r).x)) (K.add (u r).x (K.neg (c r).x)); lib_ring
  · show K.add (u r).y (K.neg (v r).y) = K.add (K.add (c r).y (K.neg (v r).y)) (K.add (u r).y (K.neg (c r).y)); lib_ring
  · show K.add (u r).z (K.neg (v r).z) = K.add (K.add (c r).z (K.neg (v r).z)) (K.add (u r).z (K.neg (c r).z)); lib_ring

theorem split_Qe (ν k : R) (hν : O.nn ν) (hk : O.nn k) (l : List I) (kz : I → Mode) (u c v : I → V3 R) :
    Le K O.toCone (K.mul k (Qe K ν k l kz u v))
      (K.add (K.mul (K.mul k (K.add K.one k)) (Qe K ν k l kz c v)) (K.mul (K.add K.one k) (Qe K ν k l kz u c))) := by
  have hw : ∀ r, O.nn (wt K kz r) := fun r => dot3_nn K O.toCone _
  have s1 := split_k_sum K O.toCone l k (wt K kz) hw (dF K c v) (dF K u c)
  have s2 := split_k_sum K O.toCone l k (fun r => K.mul (wt K kz r) (wt K kz r)) (fun r => O.nn_sq _) (dF K c v) (dF K u c)
  have e1 : lsum K (fun r => K.mul (wt K kz r) (dot3 K (dF K u v r) (dF K u v r))) l
      = lsum K (fun r => K.mul (wt K kz r) (dot3 K (add3 K (dF K c v r) (dF K u c r)) (add3 K (dF K c v r) (dF K u c r)))) l :=
    lsum_ext K _ _ (fun r => by rw [dF_split K u c v r]) l
  have e2 : lsum K (fun r => K.mul (K.mul (wt K kz r) (wt K kz r)) (dot3 K (dF K u v r) (dF K u v r))) l
      = lsum K (fun r => K.mul (K.mul (wt K kz r) (wt K kz r)) (dot3 K (add3 K (dF K c v r) (dF K u c r)) (add3 K (dF K c v r) (dF K u c r)))) l :=
    lsum_ext K _ _ (fun r => by rw [dF_split K u c v r]) l
  have t := le_add' K O.toCone _ _ _ _ (le_mul_nn K O.toCone ν _ _ hν s1)
    (le_mul_nn K O.toCone (K.mul k (K.mul ν ν)) _ _ (O.nn_mul _ _ hk (O.nn_sq ν)) s2)
  unfold Qe
  rw [e1, e2]
  refine le_trans' K O.toCone _ _ _ ⟨K.zero, O.nn_zero, ?_⟩ (le_trans' K O.toCone _ _ _ t ⟨K.zero, O.nn_zero, ?_⟩)
  · generalize lsum K (fun r => K.mul (wt K kz r) (dot3 K (add3 K (dF K c v r) (dF K u c r)) (add3 K (dF K c v r) (dF K u c r)))) l = Z
    generalize lsum K (fun r => K.mul (K.mul (wt K kz r) (wt K kz r)) (dot3 K (add3 K (dF K c v r) (dF K u c r)) (add3 K (dF K c v r) (dF K u c r)))) l = A
    lib_ring
  · show _ = K.add (K.mul (K.mul k (K.add K.one k))
        (K.add (K.mul ν (lsum K (fun r => K.mul (wt K kz r) (dot3 K (dF K c v r) (dF K c v r))) l))
          (K.mul (K.mul k (K.mul ν ν)) (lsum K (fun r => K.mul (K.mul (wt K kz r) (wt K kz r)) (dot3 K (dF K c v r) (dF K c v r))) l))))
      (K.mul (K.add K.one k)
        (K.add (K.mul ν (lsum K (fun r => K.mul (wt K kz r) (dot3 K (dF K u c r) (dF K u c r))) l))
          (K.mul (K.mul k (K.mul ν ν)) (lsum K (fun r => K.mul (K.mul (wt K kz r) (wt K kz r)) (dot3 K (dF K u c r) (dF K u c r))) l))))
    generalize lsum K (fun r => K.mul (wt K kz r) (dot3 K (dF K c v r) (dF K c v r))) l = Z1
    generalize lsum K (fun r => K.mul (K.mul (wt K kz r) (wt K kz r)) (dot3 K (dF K c v r) (dF K c v r))) l = A1
    generalize lsum K (fun r => K.mul (wt K kz r) (dot3 K (dF K u c r) (dF K u c r))) l = Z2
    generalize lsum K (fun r => K.mul (K.mul (wt K kz r) (wt K kz r)) (dot3 K (dF K u c r) (dF K u c r))) l = A2
    lib_ring

/-- the rate α_t of EnstLipschitz for the pair (u, v) at time t -/
def alpha (ι : R) (Rc : Nat) (l : List I) (kz : I → Mode) (u v : Nat → I → V3 R) (t : Nat) : R :=
  K.add (K.mul (two K) (K.mul (K.mul (two K) (K.mul ι ι)) (K.mul (C1 K inv Rc) (Zs K l kz u t))))
    (K.mul (two K) (K.mul (K.mul ι ι) (K.mul (castN K 135) (As K l kz v t))))

include mul_inv h10 in
theorem alpha_nn (ι : R) (Rc : Nat) (l : List I) (kz : I → Mode) (u v : Nat → I → V3 R) (t : Nat) :
    O.nn (alpha K inv ι Rc l kz u v t) := by
  have hw : ∀ r, O.nn (wt K kz r) := fun r => dot3_nn K O.toCone _
  have n2 : O.nn (two K) := O.nn_add _ _ (RatModel.nn_one' K O.toCone) (RatModel.nn_one' K O.toCone)
  have nC1 : O.nn (C1 K inv Rc) := O.nn_add _ _ (O.nn_mul _ _ (RatModel.nn_cast K O.toCone _) (RatModel.nn_one' K O.toCone))
    (O.nn_mul _ _ (RatModel.nn_cast K O.toCone _) (iv_nn K O inv mul_inv h10 Rc))
  exact O.nn_add _ _
    (O.nn_mul _ _ n2 (O.nn_mul _ _ (O.nn_mul _ _ n2 (O.nn_sq ι)) (O.nn_mul _ _ nC1
      (lsum_nn K O.toCone _ (fun r => O.nn_mul _ _ (hw r) (dot3_nn K O.toCone _)) l))))
    (O.nn_mul _ _ n2 (O.nn_mul _ _ (O.nn_sq ι) (O.nn_mul _ _ (RatModel.nn_cast K O.toCone 135)
      (lsum_nn K O.toCone _ (fun r => O.nn_mul _ _ (O.nn_sq _) (dot3_nn K O.toCone _)) l))))

include mul_inv h10 in
theorem step_cauchy (l : List I) (kz : I → Mode) (hd : List.Pairwise (fun a b => kz a ≠ kz b) l)
    (h1 : ∀ p, 1 ≤ nrm (kz p)) (M : Nat) (hM : ∀ p, p ∈ l → nrm (kz p) ≤ M + 1) (Rc t0 : Nat) (hRM : Rc ≤ M)
    (ι k ν iν α C : R) (hk : O.nn k) (hk0 : k ≠ K.zero) (hν : O.nn ν) (hiν : K.mul ν iν = K.one) (hα : O.nn α) (hC : O.nn C)
    (a b Nb : Nat → I → V3 R) (c Nc : Nat → Nat → I → V3 R)
    (hc0 : ∀ n, c n 0 = a n)
    (hcs : ∀ n t r, Step K k ν (wt K kz r) (c n (t + 1) r) (c n t r) (Nc n t r))
    (hbs : ∀ t r, Step K k ν (wt K kz r) (b (t + 1) r) (b t r) (Nb t r))
    (hN : ∀ n t, Le K O.toCone (lsum K (fun r => dot3 K (sub3 K (Nc n t r) (Nb (2 * n + t) r)) (sub3 K (Nc n t r) (Nb (2 * n + t) r))) l)
      (lsum K (fun r => dot3 K (sub3 K (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (c n t) (c n t) r)
          (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (b (2 * n + t)) (b (2 * n + t)) r))
        (sub3 K (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (c n t) (c n t) r)
          (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (b (2 * n + t)) (b (2 * n + t)) r))) l))
    (hβ : ∀ n t, Le K O.toCone (K.mul (two K) (K.mul (K.mul (two K) (K.mul ι ι)) (K.mul (C2 K inv Rc t0) (Zs K l kz (c n) t))))
      (K.mul ν ν))
    (hαle : ∀ n t, Le K O.toCone (alpha K inv ι Rc l kz (c n) (fun s => b (2 * n + s)) t) α)
    (hΔ : ∀ n, Le K O.toCone (Qe K ν k l kz (a (n + 1)) (c n 2)) (K.mul (K.mul k k) (K.mul k C)))
    (hx0 : Qe K ν k l kz (a 0) (b 0) = K.zero) (n : Nat) :
    Le K O.toCone (Qe K ν k l kz (a n) (b (2 * n)))
      (K.mul (pwr K (K.mul (K.add K.one k) (GG K ν iν k α)) n)
        (K.mul (castN K n) (K.mul (K.add K.one k) (K.mul (K.mul k k) C)))) := by
  have hw : ∀ r, O.nn (wt K kz r) := fun r => dot3_nn K O.toCone _
  have hQn : ∀ u v, O.nn (Qe K ν k l kz u v) := fun u v =>
    O.nn_add _ _ (O.nn_mul _ _ hν (lsum_nn K O.toCone _ (fun r => O.nn_mul _ _ (hw r) (dot3_nn K O.toCone _)) l))
      (O.nn_mul _ _ (O.nn_mul _ _ hk (O.nn_sq ν)) (lsum_nn K O.toCone _ (fun r => O.nn_mul _ _ (O.nn_sq _) (dot3_nn K O.toCone _)) l))
  have hL : ∀ n, Le K O.toCone (Qe K ν k l kz (c n 2) (b (2 * n + 2))) (K.mul (GG K ν iν k α) (Qe K ν k l kz (a n) (b (2 * n)))) := by
    intro n
    have lp := lipschitz K O inv mul_inv h10 l kz hd h1 M hM Rc t0 hRM ι k ν hk hν (c n) (fun s => b (2 * n + s)) (Nc n)
      (fun s => Nb (2 * n + s)) (hN n) (hcs n) (fun t r => hbs (2 * n + t) r) (hβ n) 2
    have lp' : Le K O.toCone (K.mul (K.mul ν ν) (Qe K ν k l kz (c n 2) (b (2 * n + 2))))
        (K.mul (K.mul (K.add ν (K.mul k (alpha K inv ι Rc l kz (c n) (fun s => b (2 * n + s)) 0)))
          (K.add ν (K.mul k (alpha K inv ι Rc l kz (c n) (fun s => b (2 * n + s)) 1)))) (Qe K ν k l kz (a n) (b (2 * n)))) := by
      rw [← hc0 n]
      refine le_trans' K O.toCone _ _ _ ⟨K.zero, O.nn_zero, ?_⟩ (le_trans' K O.toCone _ _ _ lp ⟨K.zero, O.nn_zero, ?_⟩)
      · show K.add (K.mul (K.mul ν ν) (Qe K ν k l kz (c n 2) (b (2 * n + 2)))) K.zero
          = K.mul (K.mul ν (K.mul ν K.one)) (Qe K ν k l kz (c n 2) (b (2 * n + 2)))
        lib_ring
      · show K.add (K.mul (K.mul (K.add ν (K.mul k (alpha K inv ι Rc l kz (c n) (fun s => b (2 * n + s)) 1)))
            (K.mul (K.add ν (K.mul k (alpha K inv ι Rc l kz (c n) (fun s => b (2 * n + s)) 0))) K.one))
            (Qe K ν k l kz (c n 0) (b (2 * n + 0)))) K.zero = _
        generalize alpha K inv ι Rc l kz (c n) (fun s => b (2 * n + s)) 0 = x0
        generalize alpha K inv ι Rc l kz (c n) (fun s => b (2 * n + s)) 1 = x1
        generalize Qe K ν k l kz (c n 0) (b (2 * n + 0)) = q
        lib_ring
    exact lip_G K O ν iν k α _ _ _ _ hν hiν hk (alpha_nn K O inv mul_inv h10 ι Rc l kz _ _ 0)
      (alpha_nn K O inv mul_inv h10 ι Rc l kz _ _ 1) (hαle n 0) (hαle n 1) (hQn _ _) lp'
  have gg := (G_ge_one K O ν iν k α hν hiν hk hα).2
  exact err_bound K O k (GG K ν iν k α) C (fun n => Qe K ν k l kz (a n) (b (2 * n)))
    (fun n => Qe K ν k l kz (c n 2) (b (2 * n + 2))) (fun n => Qe K ν k l kz (a (n + 1)) (c n 2)) hk hk0 hC gg hx0
    (fun n => split_Qe K O ν k hν hk l kz (a (n + 1)) (c n 2) (b (2 * n + 2))) hL hΔ n

end Gen

end StepCauchy

#print axioms StepCauchy.dF_split
#print axioms StepCauchy.split_Qe
#print axioms StepCauchy.alpha_nn
#print axioms StepCauchy.step_cauchy

ns/StepContract.lean

lean_all/ns/StepContract.lean

/-
  ns/StepContract.lean — T37 cycle 1840 (goal G858a, piece 4b): two k-steps of the scheme from two different states
  contract the enstrophy difference, over an ordered field. For Galerkin solutions u, v on the same modes (forces
  Nl(u), Nl(v)) with the doubled rates of ForcedDiffStep satisfying 2(α_t + β_t) ≤ ν², m = ν + kν²:
    two_step   Q(u − v)(2) ≤ iP·(2ν + kν²)²·Q(u − v)(0)   whenever iP·(2m)² = 1, iP ≥ 0
  (ForcedDiffStep.forced_step with zero defect, DampContract.contr_diss, DampContract.geo with n = 2). With
  u = c_n (k-steps from a_n), v = b_{2n+·} this is the hypothesis y_n ≤ G·x_n of StepUniform.step_uniform,
  G = iP·(2ν + kν²)² < 1.
  Lean 4 core plus ns/LipDamp, ns/DampContract.
-/
import ns.LipDamp
import ns.DampContract

namespace StepContract
open DetChain NSChain DyadicSmallGlobal LibRing DyadicLarge FourierGalerkin GalerkinForce ForceBound ForceSob
  ModeCount CubeCount ForceUniform NormCompare EnstStep EnstGlobal EnstGalerkin ForceTwoScale ForceBilin EnstDiff
  ForceDiff EnstGronwall LatticeSum EnstLipschitz TruncDefect

section Gen
variable {R : Type} (K : CRing R) (O : OrdR K) {I : Type}
variable (inv : R → R) (mul_inv : ∀ y, y ≠ K.zero → K.mul y (inv y) = K.one) (h10 : K.one ≠ K.zero)

include mul_inv h10 in
theorem two_step (l : List I) (kz : I → Mode) (hd : List.Pairwise (fun a b => kz a ≠ kz b) l)
    (h1 : ∀ p, 1 ≤ nrm (kz p)) (M : Nat) (hM : ∀ p, p ∈ l → nrm (kz p) ≤ M + 1) (Rc t0 : Nat) (hRM : Rc ≤ M)
    (ι k ν iP : R) (hk : O.nn k) (hν : O.nn ν) (hiPn : O.nn iP) (hiP : K.mul iP (pwr K (K.mul (K.add K.one K.one) (K.add ν (K.mul k (K.mul ν ν)))) 2) = K.one) (u v : Nat → I → V3 R)
    (hu : ∀ t r, Step K k ν (wt K kz r) (u (t + 1) r) (u t r) (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (u t) (u t) r))
    (hv : ∀ t r, Step K k ν (wt K kz r) (v (t + 1) r) (v t r) (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (v t) (v t) r))
    (hab : ∀ t, Le K O.toCone (K.mul (K.add K.one K.one) (K.add (alphaT K inv ι Rc l kz u v t) (betaT K inv ι Rc t0 l kz u t))) (K.mul ν ν)) :
    Le K O.toCone (Q K ν k (Zs K l kz (dS K u v)) (As K l kz (dS K u v)) 2) (K.mul (K.mul iP (pwr K (K.add (K.add ν ν) (K.mul k (K.mul ν ν))) 2)) (Q K ν k (Zs K l kz (dS K u v)) (As K l kz (dS K u v)) 0)) := by
  have hw : ∀ r, O.nn (wt K kz r) := fun r => dot3_nn K O.toCone _
  have n2 : O.nn (two K) := O.nn_add _ _ (RatModel.nn_one' K O.toCone) (RatModel.nn_one' K O.toCone)
  have hZn : ∀ (u : Nat → I → V3 R) t, O.nn (Zs K l kz u t) := fun u t =>
    lsum_nn K O.toCone _ (fun r => O.nn_mul _ _ (hw r) (dot3_nn K O.toCone _)) l
  have hAn : ∀ (u : Nat → I → V3 R) t, O.nn (As K l kz u t) := fun u t =>
    lsum_nn K O.toCone _ (fun r => O.nn_mul _ _ (O.nn_sq _) (dot3_nn K O.toCone _)) l
  have hiv : O.nn (iv K inv (Rc + 1)) := iv_nn K O inv mul_inv h10 Rc
  have nC1 : O.nn (C1 K inv Rc) := O.nn_add _ _ (O.nn_mul _ _ (RatModel.nn_cast K O.toCone _) (RatModel.nn_one' K O.toCone))
    (O.nn_mul _ _ (RatModel.nn_cast K O.toCone _) hiv)
  have hα : ∀ t, O.nn (alphaT K inv ι Rc l kz u v t) := fun t => O.nn_mul _ _ n2 (O.nn_add _ _
    (O.nn_mul _ _ n2 (O.nn_mul _ _ (O.nn_mul _ _ n2 (O.nn_sq ι)) (O.nn_mul _ _ nC1 (hZn u t))))
    (O.nn_mul _ _ n2 (O.nn_mul _ _ (O.nn_sq ι) (O.nn_mul _ _ (RatModel.nn_cast K O.toCone 135) (hAn v t)))))
  have hN : ∀ t, Le K O.toCone
      (lsum K (fun r => dot3 K (sub3 K (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (u t) (u t) r)
          (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (v t) (v t) r))
        (sub3 K (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (u t) (u t) r)
          (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (v t) (v t) r))) l)
      (K.add (K.mul (two K) (lsum K (fun r => dot3 K (sub3 K (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (u t) (u t) r)
          (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (v t) (v t) r))
        (sub3 K (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (u t) (u t) r)
          (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (v t) (v t) r))) l)) (K.mul (two K) K.zero)) := by
    intro t
    have hX := lsum_nn K O.toCone (fun r => dot3 K (sub3 K (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (u t) (u t) r) (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (v t) (v t) r)) (sub3 K (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (u t) (u t) r) (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (v t) (v t) r)))
      (fun r => dot3_nn K O.toCone (sub3 K (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (u t) (u t) r) (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (v t) (v t) r))) l
    generalize lsum K (fun r => dot3 K (sub3 K (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (u t) (u t) r) (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (v t) (v t) r)) (sub3 K (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (u t) (u t) r) (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (v t) (v t) r))) l = X at hX ⊢
    exact ⟨K.add X K.zero, O.nn_add _ _ hX O.nn_zero,
      by show K.add X (K.add X K.zero) = K.add (K.mul (K.add K.one K.one) X) (K.mul (K.add K.one K.one) K.zero); lib_ring⟩
  have st := fun s => ForcedDiffStep.forced_step K O inv mul_inv h10 l kz hd h1 M hM Rc t0 hRM ι k ν hk hν u v
    (fun t r => Nl K l (TriadSparse.delta K kz) ι (kv K kz) (u t) (u t) r) (fun t r => Nl K l (TriadSparse.delta K kz) ι (kv K kz) (v t) (v t) r) (fun _ => K.zero) hN hu hv s
  have hZA : ∀ t, Le K O.toCone (Zs K l kz (dS K u v) t) (As K l kz (dS K u v) t) := fun t =>
    EnstH2.Z_le_A K O kz h1 l (dS K u v t)
  have diss : ∀ t, Le K O.toCone (K.add (K.mul (K.add K.one K.one) (Q K ν k (Zs K l kz (dS K u v)) (As K l kz (dS K u v)) (t + 1))) (K.mul (K.mul k (K.mul ν ν)) (As K l kz (dS K u v) t)))
      (K.mul (K.add K.one K.one) (Q K ν k (Zs K l kz (dS K u v)) (As K l kz (dS K u v)) t)) := fun t =>
    DampContract.contr_diss K O ν k (alphaT K inv ι Rc l kz u v t) (betaT K inv ι Rc t0 l kz u t)
      (Zs K l kz (dS K u v) t) (As K l kz (dS K u v) t) (Zs K l kz (dS K u v) (t + 1)) (As K l kz (dS K u v) (t + 1))
      hk (hα t) (hAn _ t) (hZA t) (hab t)
      (le_trans' K O.toCone _ _ _ (st t) ⟨K.zero, O.nn_zero, by unfold alphaT betaT; unfold two; lib_ring⟩)
  have g := DampContract.geo K O ν k (Zs K l kz (dS K u v)) (As K l kz (dS K u v)) hν hk hZA diss 2
  refine le_trans' K O.toCone _ _ _ ⟨K.zero, O.nn_zero, ?_⟩
    (le_trans' K O.toCone _ _ _ (le_mul_nn K O.toCone iP _ _ hiPn g) ⟨K.zero, O.nn_zero, ?_⟩)
  · generalize Q K ν k (Zs K l kz (dS K u v)) (As K l kz (dS K u v)) 2 = Q2
    rw [show K.mul iP (K.mul (pwr K (K.mul (K.add K.one K.one) (K.add ν (K.mul k (K.mul ν ν)))) 2) Q2) = K.mul (K.mul iP (pwr K (K.mul (K.add K.one K.one) (K.add ν (K.mul k (K.mul ν ν)))) 2)) Q2 by lib_ring, hiP]
    lib_ring
  · generalize Q K ν k (Zs K l kz (dS K u v)) (As K l kz (dS K u v)) 0 = Q0
    generalize (pwr K (K.add (K.add ν ν) (K.mul k (K.mul ν ν))) 2) = b2
    lib_ring

end Gen
end StepContract

#print axioms StepContract.two_step

ns/StepHalf.lean

lean_all/ns/StepHalf.lean

/-
  ns/StepHalf.lean — T37 cycle 1057 (goal G656a): one implicit-viscosity step of size 2k against two steps of size k,
  exact identity, over any commutative ring (componentwise). With x = kνw and
    (1 + 2x) a₁ = a₀ + 2k N₀,     (1 + x) b₁ = a₀ + k N₀,     (1 + x) b₂ = b₁ + k N₁,
  the defect satisfies
    half_defect   (1 + 2x)(1 + x)²(a₁ − b₂) = x² a₀ + k(N₀ − N₁) + kx(2N₀ − 3N₁) + 2k x²(N₀ − N₁).
  So the one-step/two-step difference is of order k² once N₀ − N₁ = O(k) (Lipschitz force, a₀ − b₁ = O(k)) —
  the consistency step for h → 0 (G656b, c).

  Lean 4 core plus ns/EnstStep (StepC), lib/RingTac.
-/
import ns.EnstStep

namespace StepHalf
open DetChain NSChain LibRing EnstStep

section Gen
variable {R : Type} (K : CRing R)

theorem half_defect (k ν w a0 a1 b1 b2 N0 N1 : R)
    (h1 : K.mul (K.add K.one (K.mul (K.mul (K.add k k) ν) w)) a1 = K.add a0 (K.mul (K.add k k) N0))
    (h2 : K.mul (K.add K.one (K.mul (K.mul k ν) w)) b1 = K.add a0 (K.mul k N0))
    (h3 : K.mul (K.add K.one (K.mul (K.mul k ν) w)) b2 = K.add b1 (K.mul k N1)) :
    K.mul (K.mul (K.add K.one (K.mul (K.mul (K.add k k) ν) w))
        (K.mul (K.add K.one (K.mul (K.mul k ν) w)) (K.add K.one (K.mul (K.mul k ν) w)))) (K.add a1 (K.neg b2))
      = K.add (K.add (K.add (K.mul (K.mul (K.mul (K.mul k ν) w) (K.mul (K.mul k ν) w)) a0)
          (K.mul k (K.add N0 (K.neg N1))))
          (K.mul (K.mul k (K.mul (K.mul k ν) w)) (K.add (K.add N0 N0) (K.neg (K.add (K.add N1 N1) N1)))))
        (K.mul (K.mul (K.add k k) (K.mul (K.mul (K.mul k ν) w) (K.mul (K.mul k ν) w))) (K.add N0 (K.neg N1))) := by
  generalize hx : K.mul (K.mul k ν) w = x at h2 h3
  have hx2 : K.mul (K.mul (K.add k k) ν) w = K.add x x := by rw [← hx]; lib_ring
  rw [hx2] at h1
  rw [hx2]
  -- (1+2x)(1+x)² a₁ = (1+x)²(a₀ + 2kN₀)
  have ea : K.mul (K.mul (K.add K.one (K.add x x)) (K.mul (K.add K.one x) (K.add K.one x))) a1
      = K.mul (K.mul (K.add K.one x) (K.add K.one x)) (K.add a0 (K.mul (K.add k k) N0)) := by
    rw [← h1]; lib_ring
  -- (1+2x)(1+x)² b₂ = (1+2x)(a₀ + kN₀ + k(1+x)N₁)
  have eb : K.mul (K.mul (K.add K.one (K.add x x)) (K.mul (K.add K.one x) (K.add K.one x))) b2
      = K.mul (K.add K.one (K.add x x)) (K.add (K.add a0 (K.mul k N0)) (K.mul (K.mul k (K.add K.one x)) N1)) := by
    have : K.mul (K.mul (K.add K.one (K.add x x)) (K.mul (K.add K.one x) (K.add K.one x))) b2
        = K.mul (K.add K.one (K.add x x)) (K.add (K.mul (K.add K.one x) b1) (K.mul (K.mul k (K.add K.one x)) N1)) := by
      calc _ = K.mul (K.mul (K.add K.one (K.add x x)) (K.add K.one x)) (K.mul (K.add K.one x) b2) := by lib_ring
        _ = K.mul (K.mul (K.add K.one (K.add x x)) (K.add K.one x)) (K.add b1 (K.mul k N1)) := by rw [h3]
        _ = _ := by lib_ring
    rw [this, h2]
  have e : K.mul (K.mul (K.add K.one (K.add x x)) (K.mul (K.add K.one x) (K.add K.one x))) (K.add a1 (K.neg b2))
      = K.add (K.mul (K.mul (K.add K.one (K.add x x)) (K.mul (K.add K.one x) (K.add K.one x))) a1)
          (K.neg (K.mul (K.mul (K.add K.one (K.add x x)) (K.mul (K.add K.one x) (K.add K.one x))) b2)) := by lib_ring
  rw [e, ea, eb]; lib_ring

end Gen

end StepHalf

#print axioms StepHalf.half_defect

ns/StepLevel.lean

lean_all/ns/StepLevel.lean

/-
  ns/StepLevel.lean — T37 cycle 1906 (goal G860e, piece b4a): one implicit-viscosity step of size s moves a
  coefficient component by O(s), so with s² ≤ S₀·hʲ the per-level bound sa of RiemannLevel holds:
    mul_le2    0 ≤ x ≤ X, 0 ≤ y ≤ Y  ⇒  xy ≤ XY
    sa_bound   StepC s ν w a' a n, n² ≤ Bn, a² ≤ Ba, 0 ≤ w ≤ W, s² ≤ S₀·p  ⇒
               (a' − a)² ≤ (S₀·(2Bn + 2(ν²W²)Ba))·p
  (TimeLip.comp_lip: (a' − a)² ≤ 2s²n² + 2(sνw)²a²).
  Lean 4 core plus ns/TimeLip, ns/RiemannLevel.
-/
import ns.TimeLip
import ns.RiemannLevel

namespace StepLevel
open DetChain NSChain DyadicSmallGlobal LibRing DyadicLarge EnstStep RiemannLevel

section Gen
variable {R : Type} (K : CRing R) (O : OrdR K)

theorem mul_le2 (x X y Y : R) (hx : O.nn x) (hY : O.nn Y) (h1 : Le K O.toCone x X) (h2 : Le K O.toCone y Y) :
    Le K O.toCone (K.mul x y) (K.mul X Y) := by
  have a := le_mul_nn K O.toCone x _ _ hx h2
  have b := le_mul_nn K O.toCone Y _ _ hY h1
  rw [show K.mul Y x = K.mul x Y by lib_ring, show K.mul Y X = K.mul X Y by lib_ring] at b
  exact le_trans' K O.toCone _ _ _ a b

theorem sa_bound (s ν w a' a n Bn Ba W S0 p : R) (hs : StepC K s ν w a' a n) (hsn : O.nn s) (hν : O.nn ν)
    (hw : O.nn w) (hBn : O.nn Bn) (hBa : O.nn Ba) (hWW : Le K O.toCone w W)
    (hn : Le K O.toCone (K.mul n n) Bn) (ha : Le K O.toCone (K.mul a a) Ba) (hss : Le K O.toCone (K.mul s s) (K.mul S0 p)) :
    Le K O.toCone (K.mul (sub K a' a) (sub K a' a))
      (K.mul (K.mul S0 (K.add (K.mul (K.add K.one K.one) Bn)
        (K.mul (K.add K.one K.one) (K.mul (K.mul (K.mul ν ν) (K.mul W W)) Ba)))) p) := by
  have c := TimeLip.comp_lip K O.toCone s ν w a' a n hsn hν hw hs
  have two : O.nn (K.add K.one K.one) := O.nn_add _ _ (RatModel.nn_one' K O.toCone) (RatModel.nn_one' K O.toCone)
  have hWn : O.nn W := by cases hWW with | intro d hd => rw [← hd.2]; exact O.nn_add _ _ hw hd.1
  have ww := mul_le2 K O w W w W hw hWn hWW hWW
  have b1 := mul_le2 K O (K.mul s s) (K.mul S0 p) (K.mul n n) Bn (O.nn_sq s) hBn hss hn
  have vw := mul_le2 K O (K.mul ν ν) (K.mul ν ν) (K.mul w w) (K.mul W W) (O.nn_sq ν) (O.nn_mul _ _ hWn hWn) (le_refl' K O.toCone _) ww
  have b2 := mul_le2 K O (K.mul (K.mul ν ν) (K.mul w w)) (K.mul (K.mul ν ν) (K.mul W W)) (K.mul a a) Ba
    (O.nn_mul _ _ (O.nn_sq ν) (O.nn_sq w)) hBa vw ha
  have b3 := mul_le2 K O (K.mul s s) (K.mul S0 p) (K.mul (K.mul (K.mul ν ν) (K.mul w w)) (K.mul a a))
    (K.mul (K.mul (K.mul ν ν) (K.mul W W)) Ba) (O.nn_sq s) (O.nn_mul _ _ (O.nn_mul _ _ (O.nn_sq ν) (O.nn_mul _ _ hWn hWn)) hBa) hss b2
  have s1 := le_add' K O.toCone _ _ _ _ (le_mul_nn K O.toCone _ _ _ two b1) (le_mul_nn K O.toCone _ _ _ two b3)
  refine le_trans' K O.toCone _ _ _ ⟨K.zero, O.nn_zero, ?_⟩ (le_trans' K O.toCone _ _ _ c (le_trans' K O.toCone _ _ _ ⟨K.zero, O.nn_zero, ?_⟩
    (le_trans' K O.toCone _ _ _ s1 ⟨K.zero, O.nn_zero, ?_⟩)))
  · unfold sub; lib_ring
  · lib_ring
  · lib_ring

end Gen
end StepLevel

#print axioms StepLevel.mul_le2
#print axioms StepLevel.sa_bound

ns/StepStab.lean

lean_all/ns/StepStab.lean

/-
  ns/StepStab.lean — T37 cycle 920 (goal G614b, piece 1): one-step stability of the semi-implicit step for a
  difference, over any commutative ring with a nonnegative cone (ℚ included). If (1 + x)·ι = 1, x ≥ 0, h ≥ 0 and
    d' = ((1 − x)·d + h·G)·ι,
  then  d'² ≤ (1 + h)·d² + (h + h²)·G².
  Summed over modes and components this is the step of the continuous-dependence estimate for two solutions of the
  projected 3D scheme (G = difference of the projected forces, bounded by ForceBound via ForceBilin.Nl_diff).

  Lean 4 core plus NSChain (Cone, Le), ns/DyadicLarge (le_add', le_mul_nn), lib/RingTac.
    sq_le_one_of    ι²(1 + x)² = 1, x ≥ 0  ⇒  ι² ≤ 1 and ((1 − x)ι)² ≤ 1
    two_mul_le      2uv ≤ u² + v²
    step_diff       the statement
-/
import ns.DyadicLarge
import lib.RingTac

namespace StepStab
open DetChain NSChain LibRing DyadicLarge

section Gen
variable {R : Type} (K : CRing R) (P : Cone K)

theorem one_nn : P.nn K.one := by
  have h := P.nn_sq K.one
  rwa [K.one_mul] at h

theorem two_mul_le (u v : R) : Le K P (K.mul (K.add K.one K.one) (K.mul u v)) (K.add (K.mul u u) (K.mul v v)) :=
  ⟨K.mul (K.add u (K.neg v)) (K.add u (K.neg v)), P.nn_sq _, by lib_ring⟩

theorem iota_sq_le (x ι : R) (hx : P.nn x) (hι : K.mul (K.add K.one x) ι = K.one) : Le K P (K.mul ι ι) K.one := by
  -- ι² (1 + x)² = 1 and (1 + x)² = 1 + (2x + x²)
  refine ⟨K.mul (K.mul ι ι) (K.add (K.add x x) (K.mul x x)),
    P.nn_mul _ _ (P.nn_sq ι) (P.nn_add _ _ (P.nn_add _ _ hx hx) (P.nn_sq x)), ?_⟩
  have e : K.add (K.mul ι ι) (K.mul (K.mul ι ι) (K.add (K.add x x) (K.mul x x)))
      = K.mul (K.mul (K.add K.one x) ι) (K.mul (K.add K.one x) ι) := by lib_ring
  rw [e, hι, K.one_mul]

theorem c_sq_le (x ι : R) (hx : P.nn x) (hι : K.mul (K.add K.one x) ι = K.one) :
    Le K P (K.mul (K.mul (K.add K.one (K.neg x)) ι) (K.mul (K.add K.one (K.neg x)) ι)) K.one := by
  -- (1 − x)² ι² + 4x ι² = (1 + x)² ι² = 1
  refine ⟨K.mul (K.mul ι ι) (K.add (K.add x x) (K.add x x)),
    P.nn_mul _ _ (P.nn_sq ι) (P.nn_add _ _ (P.nn_add _ _ hx hx) (P.nn_add _ _ hx hx)), ?_⟩
  have e : K.add (K.mul (K.mul (K.add K.one (K.neg x)) ι) (K.mul (K.add K.one (K.neg x)) ι))
      (K.mul (K.mul ι ι) (K.add (K.add x x) (K.add x x)))
      = K.mul (K.mul (K.add K.one x) ι) (K.mul (K.add K.one x) ι) := by lib_ring
  rw [e, hι, K.one_mul]

theorem step_diff (x ι h d G : R) (hx : P.nn x) (hh : P.nn h) (hι : K.mul (K.add K.one x) ι = K.one) :
    Le K P (K.mul (K.mul (K.add (K.mul (K.add K.one (K.neg x)) d) (K.mul h G)) ι)
        (K.mul (K.add (K.mul (K.add K.one (K.neg x)) d) (K.mul h G)) ι))
      (K.add (K.mul (K.add K.one h) (K.mul d d)) (K.mul (K.add h (K.mul h h)) (K.mul G G))) := by
  have hc2 := c_sq_le K P x ι hx hι
  have he2 := iota_sq_le K P x ι hx hι
  have e1 : K.mul (K.mul (K.add (K.mul (K.add K.one (K.neg x)) d) (K.mul h G)) ι)
        (K.mul (K.add (K.mul (K.add K.one (K.neg x)) d) (K.mul h G)) ι)
      = K.add (K.add (K.mul (K.mul (K.mul (K.add K.one (K.neg x)) ι) d) (K.mul (K.mul (K.add K.one (K.neg x)) ι) d))
          (K.mul h (K.mul (K.add K.one K.one) (K.mul (K.mul (K.mul (K.add K.one (K.neg x)) ι) d) (K.mul ι G)))))
        (K.mul (K.mul h h) (K.mul (K.mul ι G) (K.mul ι G))) := by lib_ring
  rw [e1]
  generalize K.mul (K.add K.one (K.neg x)) ι = c at hc2 ⊢
  have y := le_mul_nn K P h _ _ hh (two_mul_le K P (K.mul c d) (K.mul ι G))
  have S1 : P.nn (K.mul (K.add K.one h) (K.mul d d)) := P.nn_mul _ _ (P.nn_add _ _ (one_nn K P) hh) (P.nn_sq d)
  have S2 : P.nn (K.mul (K.add h (K.mul h h)) (K.mul G G)) := P.nn_mul _ _ (P.nn_add _ _ hh (P.nn_sq h)) (P.nn_sq G)
  have z1 := le_mul_nn K P _ _ _ S1 hc2
  have z2 := le_mul_nn K P _ _ _ S2 he2
  cases y with
  | intro w hw => cases z1 with
    | intro w1 hw1 => cases z2 with
      | intro w2 hw2 =>
        refine ⟨K.add w (K.add w1 w2), P.nn_add _ _ hw.1 (P.nn_add _ _ hw1.1 hw2.1), ?_⟩
        have t1 := hw.2; have t2 := hw1.2; have t3 := hw2.2
        generalize K.mul h (K.mul (K.add K.one K.one) (K.mul (K.mul c d) (K.mul ι G))) = B at t1 ⊢
        calc _ = K.add (K.add (K.add (K.mul (K.mul c d) (K.mul c d)) (K.mul (K.mul h h) (K.mul (K.mul ι G) (K.mul ι G))))
                (K.add B w)) (K.add w1 w2) := by lib_ring
          _ = K.add (K.add (K.add (K.mul (K.mul c d) (K.mul c d)) (K.mul (K.mul h h) (K.mul (K.mul ι G) (K.mul ι G))))
                (K.mul h (K.add (K.mul (K.mul c d) (K.mul c d)) (K.mul (K.mul ι G) (K.mul ι G))))) (K.add w1 w2) := by rw [t1]
          _ = K.add (K.add (K.mul (K.mul (K.add K.one h) (K.mul d d)) (K.mul c c)) w1)
                (K.add (K.mul (K.mul (K.add h (K.mul h h)) (K.mul G G)) (K.mul ι ι)) w2) := by lib_ring
          _ = _ := by rw [t2, t3, mul_one' K, mul_one' K]

end Gen

end StepStab

#print axioms StepStab.two_mul_le
#print axioms StepStab.iota_sq_le
#print axioms StepStab.c_sq_le
#print axioms StepStab.step_diff

ns/StepStab3.lean

lean_all/ns/StepStab3.lean

/-
  ns/StepStab3.lean — T37 cycle 922 (goal G614b, piece 2): the 3D form of StepStab, over any commutative ring with
  a nonnegative cone (ℚ included). For the projected step comb (ProjStep) with (1 + x_p)ι_p = 1:
    comb_sub      comb(a, G) − comb(b, H) = comb(a − b, G − H)          (the step is linear)
    mode_stab     |comb(d, G)|² ≤ (1 + h)|d|² + (h + h²)|G|²
    sum_stab      Σ_{p<N} |comb_p(d_p, G_p)|² ≤ (1 + h) Σ_p |d_p|² + (h + h²) Σ_p |G_p|²
  So two solutions a, b of the projected 3D scheme satisfy
    D_{t+1} ≤ (1 + h) D_t + (h + h²) Σ_p |P N_p(a_t) − P N_p(b_t)|²,   D_t = Σ_p |a_{t,p} − b_{t,p}|².

  Lean 4 core plus ns/StepStab (step_diff), ns/ProjStep (comb), ns/DyadicLarge (sum_le, le_add').
-/
import ns.StepStab
import ns.ProjStep

namespace StepStab3
open DetChain NSChain LibRing DyadicLarge FourierGalerkin ProjStep StepStab

section Gen
variable {R : Type} (K : CRing R) (P : Cone K)

def sub3 (a b : V3 R) : V3 R := ⟨K.add a.x (K.neg b.x), K.add a.y (K.neg b.y), K.add a.z (K.neg b.z)⟩

theorem comb_sub (ι x h : R) (a b G H : V3 R) :
    sub3 K (comb K ι x h a G) (comb K ι x h b H) = comb K ι x h (sub3 K a b) (sub3 K G H) := by
  cases a with
  | mk a1 a2 a3 => cases b with
    | mk b1 b2 b3 => cases G with
      | mk g1 g2 g3 => cases H with
        | mk h1 h2 h3 =>
          show (⟨K.add (K.mul (K.add (K.mul (K.add K.one (K.neg x)) a1) (K.mul h g1)) ι)
                  (K.neg (K.mul (K.add (K.mul (K.add K.one (K.neg x)) b1) (K.mul h h1)) ι)),
                K.add (K.mul (K.add (K.mul (K.add K.one (K.neg x)) a2) (K.mul h g2)) ι)
                  (K.neg (K.mul (K.add (K.mul (K.add K.one (K.neg x)) b2) (K.mul h h2)) ι)),
                K.add (K.mul (K.add (K.mul (K.add K.one (K.neg x)) a3) (K.mul h g3)) ι)
                  (K.neg (K.mul (K.add (K.mul (K.add K.one (K.neg x)) b3) (K.mul h h3)) ι))⟩ : V3 R)
            = ⟨K.mul (K.add (K.mul (K.add K.one (K.neg x)) (K.add a1 (K.neg b1))) (K.mul h (K.add g1 (K.neg h1)))) ι,
               K.mul (K.add (K.mul (K.add K.one (K.neg x)) (K.add a2 (K.neg b2))) (K.mul h (K.add g2 (K.neg h2)))) ι,
               K.mul (K.add (K.mul (K.add K.one (K.neg x)) (K.add a3 (K.neg b3))) (K.mul h (K.add g3 (K.neg h3)))) ι⟩
          have e1 : K.add (K.mul (K.add (K.mul (K.add K.one (K.neg x)) a1) (K.mul h g1)) ι)
              (K.neg (K.mul (K.add (K.mul (K.add K.one (K.neg x)) b1) (K.mul h h1)) ι))
              = K.mul (K.add (K.mul (K.add K.one (K.neg x)) (K.add a1 (K.neg b1))) (K.mul h (K.add g1 (K.neg h1)))) ι := by lib_ring
          have e2 : K.add (K.mul (K.add (K.mul (K.add K.one (K.neg x)) a2) (K.mul h g2)) ι)
              (K.neg (K.mul (K.add (K.mul (K.add K.one (K.neg x)) b2) (K.mul h h2)) ι))
              = K.mul (K.add (K.mul (K.add K.one (K.neg x)) (K.add a2 (K.neg b2))) (K.mul h (K.add g2 (K.neg h2)))) ι := by lib_ring
          have e3 : K.add (K.mul (K.add (K.mul (K.add K.one (K.neg x)) a3) (K.mul h g3)) ι)
              (K.neg (K.mul (K.add (K.mul (K.add K.one (K.neg x)) b3) (K.mul h h3)) ι))
              = K.mul (K.add (K.mul (K.add K.one (K.neg x)) (K.add a3 (K.neg b3))) (K.mul h (K.add g3 (K.neg h3)))) ι := by lib_ring
          rw [e1, e2, e3]

theorem mode_stab (ι x h : R) (d G : V3 R) (hx : P.nn x) (hh : P.nn h) (hι : K.mul (K.add K.one x) ι = K.one) :
    Le K P (dot3 K (comb K ι x h d G) (comb K ι x h d G))
      (K.add (K.mul (K.add K.one h) (dot3 K d d)) (K.mul (K.add h (K.mul h h)) (dot3 K G G))) := by
  have s := le_add' K P _ _ _ _ (le_add' K P _ _ _ _ (step_diff K P x ι h d.x G.x hx hh hι) (step_diff K P x ι h d.y G.y hx hh hι))
    (step_diff K P x ι h d.z G.z hx hh hι)
  cases s with
  | intro w hw =>
    refine ⟨w, hw.1, ?_⟩
    have t := hw.2
    show K.add (K.add (K.add (K.mul (K.mul (K.add (K.mul (K.add K.one (K.neg x)) d.x) (K.mul h G.x)) ι)
        (K.mul (K.add (K.mul (K.add K.one (K.neg x)) d.x) (K.mul h G.x)) ι))
        (K.mul (K.mul (K.add (K.mul (K.add K.one (K.neg x)) d.y) (K.mul h G.y)) ι)
        (K.mul (K.add (K.mul (K.add K.one (K.neg x)) d.y) (K.mul h G.y)) ι)))
        (K.mul (K.mul (K.add (K.mul (K.add K.one (K.neg x)) d.z) (K.mul h G.z)) ι)
        (K.mul (K.add (K.mul (K.add K.one (K.neg x)) d.z) (K.mul h G.z)) ι))) w
      = K.add (K.mul (K.add K.one h) (K.add (K.add (K.mul d.x d.x) (K.mul d.y d.y)) (K.mul d.z d.z)))
        (K.mul (K.add h (K.mul h h)) (K.add (K.add (K.mul G.x G.x) (K.mul G.y G.y)) (K.mul G.z G.z)))
    rw [t]; lib_ring

theorem sum_stab (ι x : Nat → R) (h : R) (d G : Nat → V3 R) (hx : ∀ p, P.nn (x p)) (hh : P.nn h)
    (hι : ∀ p, K.mul (K.add K.one (x p)) (ι p) = K.one) (N : Nat) :
    Le K P (sumN K (fun p => dot3 K (comb K (ι p) (x p) h (d p) (G p)) (comb K (ι p) (x p) h (d p) (G p))) N)
      (K.add (K.mul (K.add K.one h) (sumN K (fun p => dot3 K (d p) (d p)) N))
        (K.mul (K.add h (K.mul h h)) (sumN K (fun p => dot3 K (G p) (G p)) N))) := by
  have s := sum_le K P _ _ (fun p => mode_stab K P (ι p) (x p) h (d p) (G p) (hx p) hh (hι p)) N
  rw [sumN_add K, sumN_mul_left, sumN_mul_left] at s
  exact s

end Gen

end StepStab3

#print axioms StepStab3.comb_sub
#print axioms StepStab3.mode_stab
#print axioms StepStab3.sum_stab

ns/StepStabE.lean

lean_all/ns/StepStabE.lean

/-
  ns/StepStabE.lean — T37 cycle 1335 (goal G795a): stability form of the step-size comparison, over an ordered field.
    err_bound0   the split recursion without x₀ = 0:  x_n ≤ gⁿ·(x_0 + n·(1+k)k²C),  g = (1+k)G ≥ 1
    step_stab    StepCauchy.step_cauchy without Qe(a_0, b_0) = 0:
                   Qe(a_n, b_{2n}) ≤ gⁿ·(Qe(a_0, b_0) + n·(1+k)k²C)
  so two runs of steps 2k and k that start from nearby states stay nearby on bounded time intervals, with constants
  independent of the truncation. With EventH4 (all H¹…H⁴ small after a truncation-free time) this is the form of the
  convergence chain usable after the eventual-regularity time.

  Lean 4 core plus ns/StepCauchy, ns/ErrRec.
-/
import ns.StepCauchy

namespace StepStabE
open DetChain NSChain DyadicSmallGlobal LibRing DyadicLarge FourierGalerkin GalerkinForce ForceBound ForceSob
  ModeCount CubeCount ForceUniform NormCompare EnstStep EnstGlobal EnstGalerkin ForceTwoScale ForceBilin EnstDiff
  ForceDiff EnstGronwall LatticeSum EnstLipschitz SplitK ErrRec LipG StepCauchy

section Gen
variable {R : Type} (K : CRing R) (O : OrdR K) {I : Type}
variable (inv : R → R) (mul_inv : ∀ y, y ≠ K.zero → K.mul y (inv y) = K.one) (h10 : K.one ≠ K.zero)

theorem err_bound0 (k G C : R) (x L Δ : Nat → R) (hk : O.nn k) (hk0 : k ≠ K.zero) (hC : O.nn C)
    (hg : Le K O.toCone K.one (K.mul (K.add K.one k) G))
    (hsp : ∀ n, Le K O.toCone (K.mul k (x (n + 1))) (K.add (K.mul (K.mul k (K.add K.one k)) (L n)) (K.mul (K.add K.one k) (Δ n))))
    (hL : ∀ n, Le K O.toCone (L n) (K.mul G (x n))) (hΔ : ∀ n, Le K O.toCone (Δ n) (K.mul (K.mul k k) (K.mul k C))) (n : Nat) :
    Le K O.toCone (x n) (K.mul (pwr K (K.mul (K.add K.one k) G) n)
      (K.add (x 0) (K.mul (castN K n) (K.mul (K.add K.one k) (K.mul (K.mul k k) C))))) := by
  have n1k : O.nn (K.add K.one k) := O.nn_add _ _ (RatModel.nn_one' K O.toCone) hk
  have step : ∀ n, Le K O.toCone (x (n + 1)) (K.add (K.mul (K.mul (K.add K.one k) G) (x n)) (K.mul (K.add K.one k) (K.mul (K.mul k k) C))) := by
    intro n
    have r := rec_split K O k G C (x n) (x (n + 1)) (L n) (Δ n) hk (hsp n) (hL n) (hΔ n)
    refine EnstGlobal.le_of_mul_le K O k _ _ hk hk0 (le_trans' K O.toCone _ _ _ r ⟨K.zero, O.nn_zero, ?_⟩)
    lib_ring
  exact rec_src K O _ _ x hg (O.nn_mul _ _ n1k (O.nn_mul _ _ (O.nn_sq k) hC)) step n

include mul_inv h10 in
theorem step_stab (l : List I) (kz : I → Mode) (hd : List.Pairwise (fun a b => kz a ≠ kz b) l)
    (h1 : ∀ p, 1 ≤ nrm (kz p)) (M : Nat) (hM : ∀ p, p ∈ l → nrm (kz p) ≤ M + 1) (Rc t0 : Nat) (hRM : Rc ≤ M)
    (ι k ν iν α C : R) (hk : O.nn k) (hk0 : k ≠ K.zero) (hν : O.nn ν) (hiν : K.mul ν iν = K.one) (hα : O.nn α) (hC : O.nn C)
    (a b Nb : Nat → I → V3 R) (c Nc : Nat → Nat → I → V3 R)
    (hc0 : ∀ n, c n 0 = a n)
    (hcs : ∀ n t r, Step K k ν (wt K kz r) (c n (t + 1) r) (c n t r) (Nc n t r))
    (hbs : ∀ t r, Step K k ν (wt K kz r) (b (t + 1) r) (b t r) (Nb t r))
    (hN : ∀ n t, Le K O.toCone (lsum K (fun r => dot3 K (sub3 K (Nc n t r) (Nb (2 * n + t) r)) (sub3 K (Nc n t r) (Nb (2 * n + t) r))) l)
      (lsum K (fun r => dot3 K (sub3 K (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (c n t) (c n t) r)
          (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (b (2 * n + t)) (b (2 * n + t)) r))
        (sub3 K (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (c n t) (c n t) r)
          (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (b (2 * n + t)) (b (2 * n + t)) r))) l))
    (hβ : ∀ n t, Le K O.toCone (K.mul (two K) (K.mul (K.mul (two K) (K.mul ι ι)) (K.mul (C2 K inv Rc t0) (Zs K l kz (c n) t))))
      (K.mul ν ν))
    (hαle : ∀ n t, Le K O.toCone (alpha K inv ι Rc l kz (c n) (fun s => b (2 * n + s)) t) α)
    (hΔ : ∀ n, Le K O.toCone (Qe K ν k l kz (a (n + 1)) (c n 2)) (K.mul (K.mul k k) (K.mul k C)))
    (n : Nat) :
    Le K O.toCone (Qe K ν k l kz (a n) (b (2 * n)))
      (K.mul (pwr K (K.mul (K.add K.one k) (GG K ν iν k α)) n)
        (K.add (Qe K ν k l kz (a 0) (b 0)) (K.mul (castN K n) (K.mul (K.add K.one k) (K.mul (K.mul k k) C))))) := by
  have hw : ∀ r, O.nn (wt K kz r) := fun r => dot3_nn K O.toCone _
  have hQn : ∀ u v, O.nn (Qe K ν k l kz u v) := fun u v =>
    O.nn_add _ _ (O.nn_mul _ _ hν (lsum_nn K O.toCone _ (fun r => O.nn_mul _ _ (hw r) (dot3_nn K O.toCone _)) l))
      (O.nn_mul _ _ (O.nn_mul _ _ hk (O.nn_sq ν)) (lsum_nn K O.toCone _ (fun r => O.nn_mul _ _ (O.nn_sq _) (dot3_nn K O.toCone _)) l))
  have hL : ∀ n, Le K O.toCone (Qe K ν k l kz (c n 2) (b (2 * n + 2))) (K.mul (GG K ν iν k α) (Qe K ν k l kz (a n) (b (2 * n)))) := by
    intro n
    have lp := lipschitz K O inv mul_inv h10 l kz hd h1 M hM Rc t0 hRM ι k ν hk hν (c n) (fun s => b (2 * n + s)) (Nc n)
      (fun s => Nb (2 * n + s)) (hN n) (hcs n) (fun t r => hbs (2 * n + t) r) (hβ n) 2
    have lp' : Le K O.toCone (K.mul (K.mul ν ν) (Qe K ν k l kz (c n 2) (b (2 * n + 2))))
        (K.mul (K.mul (K.add ν (K.mul k (alpha K inv ι Rc l kz (c n) (fun s => b (2 * n + s)) 0)))
          (K.add ν (K.mul k (alpha K inv ι Rc l kz (c n) (fun s => b (2 * n + s)) 1)))) (Qe K ν k l kz (a n) (b (2 * n)))) := by
      rw [← hc0 n]
      refine le_trans' K O.toCone _ _ _ ⟨K.zero, O.nn_zero, ?_⟩ (le_trans' K O.toCone _ _ _ lp ⟨K.zero, O.nn_zero, ?_⟩)
      · show K.add (K.mul (K.mul ν ν) (Qe K ν k l kz (c n 2) (b (2 * n + 2)))) K.zero
          = K.mul (K.mul ν (K.mul ν K.one)) (Qe K ν k l kz (c n 2) (b (2 * n + 2)))
        lib_ring
      · show K.add (K.mul (K.mul (K.add ν (K.mul k (alpha K inv ι Rc l kz (c n) (fun s => b (2 * n + s)) 1)))
            (K.mul (K.add ν (K.mul k (alpha K inv ι Rc l kz (c n) (fun s => b (2 * n + s)) 0))) K.one))
            (Qe K ν k l kz (c n 0) (b (2 * n + 0)))) K.zero = _
        generalize alpha K inv ι Rc l kz (c n) (fun s => b (2 * n + s)) 0 = x0
        generalize alpha K inv ι Rc l kz (c n) (fun s => b (2 * n + s)) 1 = x1
        generalize Qe K ν k l kz (c n 0) (b (2 * n + 0)) = q
        lib_ring
    exact lip_G K O ν iν k α _ _ _ _ hν hiν hk (alpha_nn K O inv mul_inv h10 ι Rc l kz _ _ 0)
      (alpha_nn K O inv mul_inv h10 ι Rc l kz _ _ 1) (hαle n 0) (hαle n 1) (hQn _ _) lp'
  have gg := (G_ge_one K O ν iν k α hν hiν hk hα).2
  exact err_bound0 K O k (GG K ν iν k α) C (fun n => Qe K ν k l kz (a n) (b (2 * n)))
    (fun n => Qe K ν k l kz (c n 2) (b (2 * n + 2))) (fun n => Qe K ν k l kz (a (n + 1)) (c n 2)) hk hk0 hC gg
    (fun n => split_Qe K O ν k hν hk l kz (a (n + 1)) (c n 2) (b (2 * n + 2))) hL hΔ n

end Gen

end StepStabE

#print axioms StepStabE.err_bound0
#print axioms StepStabE.step_stab

ns/StepUniform.lean

lean_all/ns/StepUniform.lean

/-
  ns/StepUniform.lean — T37 cycle 1838 (goal G858a, piece 4a): the step-size comparison uniform in time, abstract
  form, over an ordered field. With x_n = Qe(a_n, b_{2n}) (2k-scheme against k-scheme), y_n = Qe(c_n(2), b_{2n+2})
  (two k-steps from a_n against two k-steps from b_{2n}) and z_n = Qe(a_{n+1}, c_n(2)) (the local defect):
      k·x_{n+1} ≤ k(1 + k)·y_n + (1 + k)·z_n      (StepCauchy.split_Qe)
      y_n ≤ G·x_n                                   (two contracting k-steps, DampContract.geo)
      z_n ≤ k·k·(k·C)                               (LocalDefect)
  and r = (1 + k)G with r + d = 1, c·d = 1 (r < 1: the contraction beats the split factor), then
    step_rec      x_{n+1} ≤ r·x_n + (1 + k)·k²·C
    step_uniform  x_n ≤ x_0 + (1 + k)·k²·C·c   for every n
  — O(k²)·(1/(1 − r)); since 1 − r is of order k (r ≈ 1 − θk), the step-size error is O(k) uniformly on the whole
  time axis, against gⁿ·n·k³ in StepCauchy.
  Lean 4 core plus ns/ContrRec, ns/EnstGlobal (le_of_mul_le).
-/
import ns.ContrRec
import ns.RatModel

namespace StepUniform
open DetChain NSChain DyadicSmallGlobal LibRing DyadicLarge EnstGlobal

section Gen
variable {R : Type} (K : CRing R) (O : OrdR K)

theorem step_rec (k G C xn xn1 yn zn : R) (hk : O.nn k) (hk0 : k ≠ K.zero)
    (split : Le K O.toCone (K.mul k xn1) (K.add (K.mul (K.mul k (K.add K.one k)) yn) (K.mul (K.add K.one k) zn)))
    (hy : Le K O.toCone yn (K.mul G xn)) (hz : Le K O.toCone zn (K.mul (K.mul k k) (K.mul k C))) :
    Le K O.toCone xn1 (K.add (K.mul (K.mul (K.add K.one k) G) xn) (K.mul (K.add K.one k) (K.mul (K.mul k k) C))) := by
  have n1k : O.nn (K.add K.one k) := O.nn_add _ _ (RatModel.nn_one' K O.toCone) hk
  have a := le_add' K O.toCone _ _ _ _ (le_mul_nn K O.toCone (K.mul k (K.add K.one k)) _ _ (O.nn_mul _ _ hk n1k) hy)
    (le_mul_nn K O.toCone (K.add K.one k) _ _ n1k hz)
  apply EnstGlobal.le_of_mul_le K O k _ _ hk hk0
  refine le_trans' K O.toCone _ _ _ split (le_trans' K O.toCone _ _ _ a ⟨K.zero, O.nn_zero, ?_⟩)
  lib_ring

theorem step_uniform (k G C d c : R) (x y z : Nat → R) (hk : O.nn k) (hk0 : k ≠ K.zero) (hG : O.nn G)
    (hC : O.nn C) (hd : O.nn d) (hc : O.nn c) (hx0 : O.nn (x 0))
    (hr : K.add (K.mul (K.add K.one k) G) d = K.one) (hcd : K.mul c d = K.one)
    (split : ∀ n, Le K O.toCone (K.mul k (x (n + 1)))
      (K.add (K.mul (K.mul k (K.add K.one k)) (y n)) (K.mul (K.add K.one k) (z n))))
    (hy : ∀ n, Le K O.toCone (y n) (K.mul G (x n))) (hz : ∀ n, Le K O.toCone (z n) (K.mul (K.mul k k) (K.mul k C))) :
    ∀ n, Le K O.toCone (x n) (K.add (x 0) (K.mul (K.mul (K.add K.one k) (K.mul (K.mul k k) C)) c)) := by
  have n1k : O.nn (K.add K.one k) := O.nn_add _ _ (RatModel.nn_one' K O.toCone) hk
  exact ContrRec.contr_rec K O (K.mul (K.add K.one k) G) d c (K.mul (K.add K.one k) (K.mul (K.mul k k) C)) x
    (O.nn_mul _ _ n1k hG) hd hr hcd hx0 (O.nn_mul _ _ n1k (O.nn_mul _ _ (O.nn_sq k) hC)) hc
    (fun n => step_rec K O k G C (x n) (x (n + 1)) (y n) (z n) hk hk0 (split n) (hy n) (hz n))

end Gen
end StepUniform

#print axioms StepUniform.step_rec
#print axioms StepUniform.step_uniform

ns/StepUniformS.lean

lean_all/ns/StepUniformS.lean

/-
  ns/StepUniformS.lean — T37 cycle 1842 (goal G858a — done): the time-uniform step-size estimate for the scheme.
  a = the 2k-scheme, b = the k-scheme, c_n = the k-scheme started at a_n (all Galerkin, forces Nl, on the modes l),
  Qe(u, v) = νZ(u − v) + kν²A(u − v). If for every n the pair (c_n, b_{2n+·}) has 2(α_t + β_t) ≤ ν² (doubled rates,
  small data), the local defect Qe(a_{n+1}, c_n(2)) ≤ k³C, iP·(2m)² = 1 (m = ν + kν²), G = iP·(2ν + kν²)² and
  (1 + k)G + d = 1, c·d = 1, then
    step_uniform_s   Qe(a_n, b_{2n}) ≤ Qe(a_0, b_0) + (1 + k)k²C·c   for every n
  (StepCauchy.split_Qe, StepContract.two_step, StepUniform.step_uniform): the step-size error does not grow in time.
  Lean 4 core plus ns/StepUniform, ns/StepContract, ns/StepCauchy.
-/
import ns.StepUniform
import ns.StepContract
import ns.StepCauchy

namespace StepUniformS
open DetChain NSChain DyadicSmallGlobal LibRing DyadicLarge FourierGalerkin GalerkinForce ForceBound ForceSob
  ModeCount CubeCount ForceUniform NormCompare EnstStep EnstGlobal EnstGalerkin ForceTwoScale ForceBilin EnstDiff
  ForceDiff EnstGronwall LatticeSum EnstLipschitz TruncDefect StepCauchy

section Gen
variable {R : Type} (K : CRing R) (O : OrdR K) {I : Type}
variable (inv : R → R) (mul_inv : ∀ y, y ≠ K.zero → K.mul y (inv y) = K.one) (h10 : K.one ≠ K.zero)

include mul_inv h10 in
theorem step_uniform_s (l : List I) (kz : I → Mode) (hd : List.Pairwise (fun a b => kz a ≠ kz b) l)
    (h1 : ∀ p, 1 ≤ nrm (kz p)) (M : Nat) (hM : ∀ p, p ∈ l → nrm (kz p) ≤ M + 1) (Rc t0 : Nat) (hRM : Rc ≤ M)
    (ι k ν iP C d c : R) (hk : O.nn k) (hk0 : k ≠ K.zero) (hν : O.nn ν) (hiPn : O.nn iP) (hiP : K.mul iP (pwr K (K.mul (K.add K.one K.one) (K.add ν (K.mul k (K.mul ν ν)))) 2) = K.one)
    (hC : O.nn C) (hdn : O.nn d) (hcn : O.nn c)
    (hr : K.add (K.mul (K.add K.one k) (K.mul iP (pwr K (K.add (K.add ν ν) (K.mul k (K.mul ν ν))) 2))) d = K.one) (hcd : K.mul c d = K.one)
    (a b : Nat → I → V3 R) (cc : Nat → Nat → I → V3 R) (hc0 : ∀ n, cc n 0 = a n)
    (hcs : ∀ n t r, Step K k ν (wt K kz r) (cc n (t + 1) r) (cc n t r)
      (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (cc n t) (cc n t) r))
    (hbs : ∀ t r, Step K k ν (wt K kz r) (b (t + 1) r) (b t r) (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (b t) (b t) r))
    (hab : ∀ n t, Le K O.toCone (K.mul (K.add K.one K.one) (K.add (alphaT K inv ι Rc l kz (cc n) (fun s => b (2 * n + s)) t)
      (betaT K inv ι Rc t0 l kz (cc n) t))) (K.mul ν ν))
    (hΔ : ∀ n, Le K O.toCone (Qe K ν k l kz (a (n + 1)) (cc n 2)) (K.mul (K.mul k k) (K.mul k C))) (n : Nat) :
    Le K O.toCone (Qe K ν k l kz (a n) (b (2 * n)))
      (K.add (Qe K ν k l kz (a 0) (b 0)) (K.mul (K.mul (K.add K.one k) (K.mul (K.mul k k) C)) c)) := by
  have hy : ∀ n, Le K O.toCone (Qe K ν k l kz (cc n 2) (b (2 * n + 2)))
      (K.mul (K.mul iP (pwr K (K.add (K.add ν ν) (K.mul k (K.mul ν ν))) 2)) (Qe K ν k l kz (a n) (b (2 * n)))) := by
    intro n
    have t := StepContract.two_step K O inv mul_inv h10 l kz hd h1 M hM Rc t0 hRM ι k ν iP hk hν hiPn hiP (cc n)
      (fun s => b (2 * n + s)) (hcs n) (fun t r => hbs (2 * n + t) r) (hab n)
    rw [← hc0 n]
    exact t
  have hG : O.nn (K.mul iP (pwr K (K.add (K.add ν ν) (K.mul k (K.mul ν ν))) 2)) := O.nn_mul _ _ hiPn (EnstGronwall.pwr_nn K O _
    (O.nn_add _ _ (O.nn_add _ _ hν hν) (O.nn_mul _ _ hk (O.nn_sq ν))) 2)
  have hQn : ∀ u v, O.nn (Qe K ν k l kz u v) := fun u v =>
    O.nn_add _ _ (O.nn_mul _ _ hν (lsum_nn K O.toCone _ (fun r => O.nn_mul _ _ (dot3_nn K O.toCone _) (dot3_nn K O.toCone _)) l))
      (O.nn_mul _ _ (O.nn_mul _ _ hk (O.nn_sq ν)) (lsum_nn K O.toCone _ (fun r => O.nn_mul _ _ (O.nn_sq _) (dot3_nn K O.toCone _)) l))
  exact StepUniform.step_uniform K O k (K.mul iP (pwr K (K.add (K.add ν ν) (K.mul k (K.mul ν ν))) 2)) C d c (fun n => Qe K ν k l kz (a n) (b (2 * n)))
    (fun n => Qe K ν k l kz (cc n 2) (b (2 * n + 2))) (fun n => Qe K ν k l kz (a (n + 1)) (cc n 2))
    hk hk0 hG hC hdn hcn (hQn _ _) hr hcd
    (fun n => split_Qe K O ν k hν hk l kz (a (n + 1)) (cc n 2) (b (2 * n + 2))) hy hΔ n

end Gen
end StepUniformS

#print axioms StepUniformS.step_uniform_s

ns/StepUnique.lean

lean_all/ns/StepUnique.lean

/-
  ns/StepUnique.lean — T37 cycle 1229: the implicit-viscosity step is well defined, over an ordered field.
    step_unique   StepC h ν w a' a n, StepC h ν w b' a n, h, ν, w ≥ 0  ⇒  a' = b'
  (1 + hνw ≥ 1 is invertible), so a scheme run is determined by its data and its forces; with the force a function of
  the state (Nl), the whole trajectory is determined by the initial datum.

  Lean 4 core plus ns/EnstStep (StepC), ns/BaselQ (cancel), ns/DefectSplit (one_le pattern).
-/
import ns.EnstStep
import ns.BaselQ
import ns.TruncRate

namespace StepUnique
open DetChain NSChain DyadicSmallGlobal LibRing DyadicLarge EnstStep

section Gen
variable {R : Type} (K : CRing R) (O : OrdR K)

theorem step_unique (h ν w a' b' a n : R) (hh : O.nn h) (hν : O.nn ν) (hw : O.nn w) (h10 : K.one ≠ K.zero)
    (ha : StepC K h ν w a' a n) (hb : StepC K h ν w b' a n) : a' = b' := by
  unfold StepC at ha hb
  have hx : O.nn (K.mul (K.mul h ν) w) := O.nn_mul _ _ (O.nn_mul _ _ hh hν) hw
  have hne : K.add K.one (K.mul (K.mul h ν) w) ≠ K.zero := by
    intro e
    have h1 : Le K O.toCone K.one (K.add K.one (K.mul (K.mul h ν) w)) := ⟨K.mul (K.mul h ν) w, hx, rfl⟩
    rw [e] at h1
    exact h10 (O.antisym _ _ h1 ⟨K.one, RatModel.nn_one' K O.toCone, by lib_ring⟩)
  exact BaselQ.cancel K O _ _ _ hne (ha.trans hb.symm)

end Gen

end StepUnique

#print axioms StepUnique.step_unique

ns/StepZeroExt.lean

lean_all/ns/StepZeroExt.lean

/-
  ns/StepZeroExt.lean — T37 (goal G795b-j): the Galerkin Step hypothesis only on the modes of the list.
  A system that satisfies Step only for r ∈ l (e.g. the low-mode part on l.filter (lowB Rc)) is extended by 0 off
  the list (extT; membership given by a Boolean test memb with memb r = true ↔ r ∈ l, no decidability axiom).
  Off the list Step 0 0 0 holds, and every quantity of the chain (lsum over l, Nl, Zs, As, Bs, En, Q) only reads
  values on l, so the existing theorems apply to the extension unchanged.
    lsum_congr_mem   f = g on l  ⇒  lsum f l = lsum g l
    nl_ext           Nl over l of the extension = Nl of the original
    step_ext         Step on l  ⇒  Step of the extension everywhere
    h2_large_le_mem  H2LargeLe.h2_large_le with Step assumed only on l
    energy_upto_mem  WindowEnergy.energy_upto with Step assumed only on l
  Lean 4 core plus ns/H2LargeLe, ns/WindowEnergy.
-/
import ns.H2LargeLe
import ns.WindowEnergy

namespace StepZeroExt
open DetChain NSChain DyadicSmallGlobal LibRing DyadicLarge FourierGalerkin GalerkinForce ForceBound ForceSob
  ModeCount CubeCount ForceUniform NormCompare EnstGalerkin ForceTwoScale EnstStep

section Basic
variable {R : Type} (K : CRing R) {I : Type}

/-- the extension by 0 off the modes with memb r = false -/
def extT (memb : I → Bool) (a : Nat → I → V3 R) (t : Nat) (r : I) : V3 R :=
  match memb r with
  | true => a t r
  | false => ⟨K.zero, K.zero, K.zero⟩

theorem lsum_congr_mem (f g : I → R) : ∀ l : List I, (∀ r, r ∈ l → f r = g r) → lsum K f l = lsum K g l
  | [] => fun _ => rfl
  | x :: l => fun h => by
    show K.add (f x) (lsum K f l) = K.add (g x) (lsum K g l)
    rw [h x (List.Mem.head _), lsum_congr_mem f g l (fun r hr => h r (List.Mem.tail _ hr))]

theorem extT_eq (memb : I → Bool) (l : List I) (hm : ∀ r, r ∈ l → memb r = true) (a : Nat → I → V3 R)
    (t : Nat) (r : I) (hr : r ∈ l) : extT K memb a t r = a t r := by
  unfold extT; rw [hm r hr]

theorem nl_ext (memb : I → Bool) (l : List I) (hm : ∀ r, r ∈ l → memb r = true) (δ : I → I → I → R) (ι : R)
    (k : I → V3 R) (a : Nat → I → V3 R) (t : Nat) (r : I) :
    Nl K l δ ι k (extT K memb a t) (extT K memb a t) r = Nl K l δ ι k (a t) (a t) r := by
  have hc : ∀ p q, p ∈ l → coef K δ ι k (extT K memb a t) p q r = coef K δ ι k (a t) p q r := fun p q hp => by
    unfold coef; rw [extT_eq K memb l hm a t p hp]
  have e : ∀ sel : V3 R → R,
      lsum K (fun p => lsum K (fun q => K.mul (coef K δ ι k (extT K memb a t) p q r) (sel (extT K memb a t q))) l) l
        = lsum K (fun p => lsum K (fun q => K.mul (coef K δ ι k (a t) p q r) (sel (a t q))) l) l := fun sel =>
    lsum_congr_mem K _ _ l (fun p hp => lsum_congr_mem K _ _ l (fun q hq => by
      rw [hc p q hp, extT_eq K memb l hm a t q hq]))
  unfold Nl
  rw [e V3.x, e V3.y, e V3.z]

theorem step_zero (h ν w : R) : Step K h ν w ⟨K.zero, K.zero, K.zero⟩ ⟨K.zero, K.zero, K.zero⟩ ⟨K.zero, K.zero, K.zero⟩ := by
  have e : StepC K h ν w K.zero K.zero K.zero := by unfold StepC; lib_ring
  exact ⟨e, e, e⟩

theorem step_ext (memb : I → Bool) (l : List I) (hm' : ∀ r, memb r = true → r ∈ l) (h ν : R) (w : I → R)
    (a N : Nat → I → V3 R) (hs : ∀ t r, r ∈ l → Step K h ν (w r) (a (t + 1) r) (a t r) (N t r)) :
    ∀ t r, Step K h ν (w r) (extT K memb a (t + 1) r) (extT K memb a t r) (extT K memb N t r) := by
  intro t r
  unfold extT
  cases e : memb r with
  | true => exact hs t r (hm' r e)
  | false => exact step_zero K h ν (w r)

theorem le_congr (P : Cone K) {x x' y y' : R} (e1 : x = x') (e2 : y = y') (h : Le K P x y) : Le K P x' y' := by
  rw [← e1, ← e2]; exact h

theorem en_congr (ν h ε : R) (A A' B B' : Nat → R) (s : Nat) (hA : A s = A' s) (hB : B s = B' s) :
    H2Linear.En K ν h ε A B s = H2Linear.En K ν h ε A' B' s := by
  unfold H2Linear.En; rw [hA, hB]

end Basic

section Gen
variable {R : Type} (K : CRing R) (O : OrdR K) {I : Type}
variable (inv : R → R) (mul_inv : ∀ y, y ≠ K.zero → K.mul y (inv y) = K.one) (h10 : K.one ≠ K.zero)

include mul_inv h10 in
/-- **H2LargeLe.h2_large_le with the Step hypothesis only on the modes of the list** -/
theorem h2_large_le_mem (l : List I) (kz : I → Mode) (hd : List.Pairwise (fun a b => kz a ≠ kz b) l)
    (h1 : ∀ p, 1 ≤ nrm (kz p)) (Mm : Nat) (hM : ∀ p, p ∈ l → nrm (kz p) ≤ Mm + 2) (Rc t0 : Nat) (hRM : Rc ≤ Mm + 1)
    (ι h ν Zb : R) (hh : O.nn h) (hν : O.nn ν) (hν0 : ν ≠ K.zero) (hZb : O.nn Zb)
    (a N : Nat → I → V3 R)
    (hN0 : ∀ t, Le K O.toCone (lsum K (fun r => dot3 K (N t r) (N t r)) l)
      (lsum K (fun r => dot3 K (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (a t) (a t) r)
        (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (a t) (a t) r)) l))
    (hN : ∀ t, Le K O.toCone (lsum K (fun r => K.mul (wt K kz r) (dot3 K (N t r) (N t r))) l)
      (lsum K (fun r => K.mul (wt K kz r) (dot3 K (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (a t) (a t) r)
        (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (a t) (a t) r))) l))
    (memb : I → Bool) (hm : ∀ r, r ∈ l → memb r = true) (hm' : ∀ r, memb r = true → r ∈ l)
    (hs : ∀ t r, r ∈ l → Step K h ν (wt K kz r) (a (t + 1) r) (a t r) (N t r))
    (choice : Le K O.toCone (K.mul (EnstLocalGalerkin.cc K ι (EnstLocalGalerkin.C2 K inv Rc t0)) Zb) (K.mul ν ν))
    (n : Nat) (budget : Le K O.toCone (K.add (EnstGlobal.Q K ν h (EnstGalerkin.Zs K l kz a) (EnstGalerkin.As K l kz a) 0)
      (K.mul (castN K n) (K.mul h (K.mul (EnstLocalGalerkin.cc K ι (EnstLocalGalerkin.C1 K inv Rc)) (K.mul Zb Zb)))))
      (K.mul ν Zb))
    (e D1 D2 D3 : R) (he : O.nn e) (hD1 : O.nn D1) (hD2 : O.nn D2) (hD3 : O.nn D3)
    (H1 : Le K O.toCone (K.mul (AgmonAbsorb.c3 K (castN K 16)) (AgmonAbsorb.q5 K Zb))
      (K.mul (AgmonAbsorb.c3 K (castN K 9)) (K.mul (RootYoung.four K) (K.mul (AgmonAbsorb.c3 K e) D1))))
    (H2 : Le K O.toCone (K.mul (castN K 16) (AgmonAbsorb.q5 K Zb))
      (K.mul (castN K 9) (K.mul (RootYoung.four K) (K.mul (AgmonAbsorb.c3 K e) D2))))
    (H3 : Le K O.toCone (AgmonAbsorb.c3 K Zb) (K.mul (RootYoung.four K) (K.mul e D3)))
    (hε : Le K O.toCone (K.mul (K.mul ι ι) (K.add (K.mul (AgmonCollapse.k1 K (castN K 3) (castN K 9) (castN K 108)) e)
      (K.mul (AgmonCollapse.k2 K (castN K 9) (castN K 108)) (K.add e (K.mul (castN K 2) e))))) (K.mul ν ν))
    (t : Nat) (ht : t ≤ n) :
    Le K O.toCone (EnstGalerkin.Zs K l kz a t) Zb ∧
    Le K O.toCone (H2Linear.En K ν h (K.mul (K.mul ι ι) (K.add (K.mul (AgmonCollapse.k1 K (castN K 3) (castN K 9) (castN K 108)) e)
        (K.mul (AgmonCollapse.k2 K (castN K 9) (castN K 108)) (K.add e (K.mul (castN K 2) e)))))
        (fun s => ForceAbsorb.As K kz l (a s)) (fun s => ForceAbsorb.Bs K kz l (a s)) t)
      (K.add (H2Linear.En K ν h (K.mul (K.mul ι ι) (K.add (K.mul (AgmonCollapse.k1 K (castN K 3) (castN K 9) (castN K 108)) e)
        (K.mul (AgmonCollapse.k2 K (castN K 9) (castN K 108)) (K.add e (K.mul (castN K 2) e)))))
        (fun s => ForceAbsorb.As K kz l (a s)) (fun s => ForceAbsorb.Bs K kz l (a s)) 0)
        (K.mul (castN K t) (K.mul h (K.mul (K.mul ι ι)
          (K.add (K.mul (AgmonCollapse.k1 K (castN K 3) (castN K 9) (castN K 108))
              (K.add D1 (K.mul (AgmonAbsorb.c3 K (castN K 2)) (K.mul Zb Zb))))
            (K.mul (AgmonCollapse.k2 K (castN K 9) (castN K 108)) (K.add D2 (K.mul (castN K 2) D3)))))))) := by
  have ea : ∀ s r, r ∈ l → extT K memb a s r = a s r := fun s r hr => extT_eq K memb l hm a s r hr
  have eN : ∀ s r, r ∈ l → extT K memb N s r = N s r := fun s r hr => extT_eq K memb l hm N s r hr
  have eZs : ∀ s, EnstGalerkin.Zs K l kz a s = EnstGalerkin.Zs K l kz (extT K memb a) s := fun s => by
    unfold EnstGalerkin.Zs; exact lsum_congr_mem K _ _ l (fun r hr => by rw [ea s r hr])
  have eAs : ∀ s, EnstGalerkin.As K l kz a s = EnstGalerkin.As K l kz (extT K memb a) s := fun s => by
    unfold EnstGalerkin.As; exact lsum_congr_mem K _ _ l (fun r hr => by rw [ea s r hr])
  have eFA : ∀ s, ForceAbsorb.As K kz l (a s) = ForceAbsorb.As K kz l (extT K memb a s) := fun s => by
    unfold ForceAbsorb.As; exact lsum_congr_mem K _ _ l (fun r hr => by rw [ea s r hr])
  have eFB : ∀ s, ForceAbsorb.Bs K kz l (a s) = ForceAbsorb.Bs K kz l (extT K memb a s) := fun s => by
    unfold ForceAbsorb.Bs; exact lsum_congr_mem K _ _ l (fun r hr => by rw [ea s r hr])
  have eNl : ∀ s r, Nl K l (TriadSparse.delta K kz) ι (kv K kz) (a s) (a s) r
      = Nl K l (TriadSparse.delta K kz) ι (kv K kz) (extT K memb a s) (extT K memb a s) r := fun s r =>
    (nl_ext K memb l hm (TriadSparse.delta K kz) ι (kv K kz) a s r).symm
  have hN0' : ∀ s, Le K O.toCone (lsum K (fun r => dot3 K (extT K memb N s r) (extT K memb N s r)) l)
      (lsum K (fun r => dot3 K (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (extT K memb a s) (extT K memb a s) r)
        (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (extT K memb a s) (extT K memb a s) r)) l) := fun s =>
    le_congr K O.toCone (lsum_congr_mem K _ _ l (fun r hr => by rw [eN s r hr]))
      (lsum_congr_mem K _ _ l (fun r _ => by rw [eNl s r])) (hN0 s)
  have hN' : ∀ s, Le K O.toCone (lsum K (fun r => K.mul (wt K kz r) (dot3 K (extT K memb N s r) (extT K memb N s r))) l)
      (lsum K (fun r => K.mul (wt K kz r)
        (dot3 K (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (extT K memb a s) (extT K memb a s) r)
          (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (extT K memb a s) (extT K memb a s) r))) l) := fun s =>
    le_congr K O.toCone (lsum_congr_mem K _ _ l (fun r hr => by rw [eN s r hr]))
      (lsum_congr_mem K _ _ l (fun r _ => by rw [eNl s r])) (hN s)
  have budget' : Le K O.toCone (K.add (EnstGlobal.Q K ν h (EnstGalerkin.Zs K l kz (extT K memb a))
      (EnstGalerkin.As K l kz (extT K memb a)) 0)
      (K.mul (castN K n) (K.mul h (K.mul (EnstLocalGalerkin.cc K ι (EnstLocalGalerkin.C1 K inv Rc)) (K.mul Zb Zb)))))
      (K.mul ν Zb) :=
    le_congr K O.toCone (by unfold EnstGlobal.Q; rw [eZs 0, eAs 0]) rfl budget
  have r := H2LargeLe.h2_large_le K O inv mul_inv h10 l kz hd h1 Mm hM Rc t0 hRM ι h ν Zb hh hν hν0 hZb
    (extT K memb a) (extT K memb N) hN0' hN' (step_ext K memb l hm' h ν (wt K kz) a N hs) choice n budget'
    e D1 D2 D3 he hD1 hD2 hD3 H1 H2 H3 hε t ht
  have eEn : ∀ ε s, H2Linear.En K ν h ε (fun s => ForceAbsorb.As K kz l (a s)) (fun s => ForceAbsorb.Bs K kz l (a s)) s
      = H2Linear.En K ν h ε (fun s => ForceAbsorb.As K kz l (extT K memb a s))
          (fun s => ForceAbsorb.Bs K kz l (extT K memb a s)) s := fun ε s =>
    en_congr K ν h ε _ _ _ _ s (eFA s) (eFB s)
  exact ⟨le_congr K O.toCone (eZs t).symm rfl r.1,
    le_congr K O.toCone (eEn _ t).symm (by rw [eEn _ 0]) r.2⟩

include mul_inv h10 in
/-- **WindowEnergy.energy_upto with the Step hypothesis only on the modes of the list** -/
theorem energy_upto_mem (l : List I) (kz : I → Mode) (hd : List.Pairwise (fun a b => kz a ≠ kz b) l)
    (h1 : ∀ p, 1 ≤ nrm (kz p)) (Mm : Nat) (hM : ∀ p, p ∈ l → nrm (kz p) ≤ Mm + 2) (Rc t0 : Nat) (hRM : Rc ≤ Mm + 1)
    (ι h ν Zb : R) (hh : O.nn h) (hν : O.nn ν) (hν0 : ν ≠ K.zero) (hZb : O.nn Zb)
    (a N : Nat → I → V3 R)
    (hN0 : ∀ t, Le K O.toCone (lsum K (fun r => dot3 K (N t r) (N t r)) l)
      (lsum K (fun r => dot3 K (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (a t) (a t) r)
        (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (a t) (a t) r)) l))
    (hN : ∀ t, Le K O.toCone (lsum K (fun r => K.mul (wt K kz r) (dot3 K (N t r) (N t r))) l)
      (lsum K (fun r => K.mul (wt K kz r) (dot3 K (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (a t) (a t) r)
        (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (a t) (a t) r))) l))
    (memb : I → Bool) (hm : ∀ r, r ∈ l → memb r = true) (hm' : ∀ r, memb r = true → r ∈ l)
    (hs : ∀ t r, r ∈ l → Step K h ν (wt K kz r) (a (t + 1) r) (a t r) (N t r))
    (choice : Le K O.toCone (K.mul (EnstLocalGalerkin.cc K ι (EnstLocalGalerkin.C2 K inv Rc t0)) Zb) (K.mul ν ν))
    (n : Nat) (Qmax : R)
    (hQ : ∀ k, Le K O.toCone (EnstGlobal.Q K ν h (EnstGalerkin.Zs K l kz a) (EnstGalerkin.As K l kz a) (n * k)) Qmax)
    (hbud : Le K O.toCone (K.add Qmax
      (K.mul (castN K n) (K.mul h (K.mul (EnstLocalGalerkin.cc K ι (EnstLocalGalerkin.C1 K inv Rc)) (K.mul Zb Zb)))))
      (K.mul ν Zb))
    (e D1 D2 D3 : R) (he : O.nn e) (hD1 : O.nn D1) (hD2 : O.nn D2) (hD3 : O.nn D3)
    (H1 : Le K O.toCone (K.mul (AgmonAbsorb.c3 K (castN K 16)) (AgmonAbsorb.q5 K Zb))
      (K.mul (AgmonAbsorb.c3 K (castN K 9)) (K.mul (RootYoung.four K) (K.mul (AgmonAbsorb.c3 K e) D1))))
    (H2 : Le K O.toCone (K.mul (castN K 16) (AgmonAbsorb.q5 K Zb))
      (K.mul (castN K 9) (K.mul (RootYoung.four K) (K.mul (AgmonAbsorb.c3 K e) D2))))
    (H3 : Le K O.toCone (AgmonAbsorb.c3 K Zb) (K.mul (RootYoung.four K) (K.mul e D3)))
    (hε : Le K O.toCone (K.mul (K.mul ι ι) (K.add (K.mul (AgmonCollapse.k1 K (castN K 3) (castN K 9) (castN K 108)) e)
      (K.mul (AgmonCollapse.k2 K (castN K 9) (castN K 108)) (K.add e (K.mul (castN K 2) e))))) (K.mul ν ν))
    (K' : Nat) :
    ∀ j, j ≤ K' → Le K O.toCone (H2Linear.En K ν h (K.mul (K.mul ι ι) (K.add (K.mul (AgmonCollapse.k1 K (castN K 3) (castN K 9) (castN K 108)) e)
        (K.mul (AgmonCollapse.k2 K (castN K 9) (castN K 108)) (K.add e (K.mul (castN K 2) e)))))
        (fun s => ForceAbsorb.As K kz l (a s)) (fun s => ForceAbsorb.Bs K kz l (a s)) (n * j))
      (K.add (H2Linear.En K ν h (K.mul (K.mul ι ι) (K.add (K.mul (AgmonCollapse.k1 K (castN K 3) (castN K 9) (castN K 108)) e)
        (K.mul (AgmonCollapse.k2 K (castN K 9) (castN K 108)) (K.add e (K.mul (castN K 2) e)))))
        (fun s => ForceAbsorb.As K kz l (a s)) (fun s => ForceAbsorb.Bs K kz l (a s)) (n * 0)) (K.mul (castN K j) (K.mul (castN K n) (K.mul h (K.mul (K.mul ι ι)
          (K.add (K.mul (AgmonCollapse.k1 K (castN K 3) (castN K 9) (castN K 108))
              (K.add D1 (K.mul (AgmonAbsorb.c3 K (castN K 2)) (K.mul Zb Zb))))
            (K.mul (AgmonCollapse.k2 K (castN K 9) (castN K 108)) (K.add D2 (K.mul (castN K 2) D3))))))))) := by
  have ea : ∀ s r, r ∈ l → extT K memb a s r = a s r := fun s r hr => extT_eq K memb l hm a s r hr
  have eN : ∀ s r, r ∈ l → extT K memb N s r = N s r := fun s r hr => extT_eq K memb l hm N s r hr
  have eZs : ∀ s, EnstGalerkin.Zs K l kz a s = EnstGalerkin.Zs K l kz (extT K memb a) s := fun s => by
    unfold EnstGalerkin.Zs; exact lsum_congr_mem K _ _ l (fun r hr => by rw [ea s r hr])
  have eAs : ∀ s, EnstGalerkin.As K l kz a s = EnstGalerkin.As K l kz (extT K memb a) s := fun s => by
    unfold EnstGalerkin.As; exact lsum_congr_mem K _ _ l (fun r hr => by rw [ea s r hr])
  have eFA : ∀ s, ForceAbsorb.As K kz l (a s) = ForceAbsorb.As K kz l (extT K memb a s) := fun s => by
    unfold ForceAbsorb.As; exact lsum_congr_mem K _ _ l (fun r hr => by rw [ea s r hr])
  have eFB : ∀ s, ForceAbsorb.Bs K kz l (a s) = ForceAbsorb.Bs K kz l (extT K memb a s) := fun s => by
    unfold ForceAbsorb.Bs; exact lsum_congr_mem K _ _ l (fun r hr => by rw [ea s r hr])
  have eNl : ∀ s r, Nl K l (TriadSparse.delta K kz) ι (kv K kz) (a s) (a s) r
      = Nl K l (TriadSparse.delta K kz) ι (kv K kz) (extT K memb a s) (extT K memb a s) r := fun s r =>
    (nl_ext K memb l hm (TriadSparse.delta K kz) ι (kv K kz) a s r).symm
  have hN0' : ∀ s, Le K O.toCone (lsum K (fun r => dot3 K (extT K memb N s r) (extT K memb N s r)) l)
      (lsum K (fun r => dot3 K (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (extT K memb a s) (extT K memb a s) r)
        (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (extT K memb a s) (extT K memb a s) r)) l) := fun s =>
    le_congr K O.toCone (lsum_congr_mem K _ _ l (fun r hr => by rw [eN s r hr]))
      (lsum_congr_mem K _ _ l (fun r _ => by rw [eNl s r])) (hN0 s)
  have hN' : ∀ s, Le K O.toCone (lsum K (fun r => K.mul (wt K kz r) (dot3 K (extT K memb N s r) (extT K memb N s r))) l)
      (lsum K (fun r => K.mul (wt K kz r)
        (dot3 K (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (extT K memb a s) (extT K memb a s) r)
          (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (extT K memb a s) (extT K memb a s) r))) l) := fun s =>
    le_congr K O.toCone (lsum_congr_mem K _ _ l (fun r hr => by rw [eN s r hr]))
      (lsum_congr_mem K _ _ l (fun r _ => by rw [eNl s r])) (hN s)
  have hQ' : ∀ k, Le K O.toCone (EnstGlobal.Q K ν h (EnstGalerkin.Zs K l kz (extT K memb a))
      (EnstGalerkin.As K l kz (extT K memb a)) (n * k)) Qmax := fun k =>
    le_congr K O.toCone (by unfold EnstGlobal.Q; rw [eZs (n * k), eAs (n * k)]) rfl (hQ k)
  have r := WindowEnergy.energy_upto K O inv mul_inv h10 l kz hd h1 Mm hM Rc t0 hRM ι h ν Zb hh hν hν0 hZb
    (extT K memb a) (extT K memb N) hN0' hN' (step_ext K memb l hm' h ν (wt K kz) a N hs) choice n Qmax hQ' hbud
    e D1 D2 D3 he hD1 hD2 hD3 H1 H2 H3 hε K'
  have eEn : ∀ ε s, H2Linear.En K ν h ε (fun s => ForceAbsorb.As K kz l (a s)) (fun s => ForceAbsorb.Bs K kz l (a s)) s
      = H2Linear.En K ν h ε (fun s => ForceAbsorb.As K kz l (extT K memb a s))
          (fun s => ForceAbsorb.Bs K kz l (extT K memb a s)) s := fun ε s =>
    en_congr K ν h ε _ _ _ _ s (eFA s) (eFB s)
  exact fun j hj => le_congr K O.toCone (eEn _ (n * j)).symm (by rw [eEn _ (n * 0)]) (r j hj)

end Gen

end StepZeroExt

#print axioms StepZeroExt.lsum_congr_mem
#print axioms StepZeroExt.extT_eq
#print axioms StepZeroExt.nl_ext
#print axioms StepZeroExt.step_zero
#print axioms StepZeroExt.step_ext
#print axioms StepZeroExt.le_congr
#print axioms StepZeroExt.en_congr
#print axioms StepZeroExt.h2_large_le_mem
#print axioms StepZeroExt.energy_upto_mem

ns/StokesCN.lean

lean_all/ns/StokesCN.lean

/-
  ns/StokesCN.lean — T37 cycle 770 (goal G481): one mode of the linear (Stokes) Crank–Nicolson step, over any
  ordered ring with no zero divisors (ℚ included). With x = hνk ≥ 0 and a' − a = −x(a' + a):
    lin_form     (1 + x)·a' = (1 − x)·a
    lin_unique   two solutions a', b' from the same a coincide (1 + x ≠ 0)
    lin_damp     a'² ≤ a²     ((1 + x)² a'² = (1 − x)² a² and (1 − x)² ≤ (1 + x)²)
  The damping factor (1 − x)/(1 + x) ∈ [−1, 1]; G478b (the nonlinear step) is the perturbation of this.

  Lean 4 core plus ns/GoodModesQ (ne_of_one_add, mul_ne, one_eq_zero_of), ns/RatModelU (le_mul_rg),
  lib/RingTac (lib_ring).
-/
import ns.GoodModesQ

namespace StokesCN
open DetChain NSChain DyadicSmallGlobal LibRing DyadicLarge GoodModesQ RatModelU

section Gen
variable {R : Type} (K : CRing R) (O : OrdR K)
include O

omit O in
theorem lin_form (a a' x : R) (step : K.add a' (K.neg a) = K.neg (K.mul x (K.add a' a))) :
    K.mul (K.add K.one x) a' = K.mul (K.add K.one (K.neg x)) a := by
  have : K.mul (K.add K.one x) a' = K.add (K.add (K.add a' (K.neg a)) (K.mul x (K.add a' a)))
      (K.mul (K.add K.one (K.neg x)) a) := by lib_ring
  rw [this, step]; lib_ring

theorem lin_unique (h10 : K.one ≠ K.zero) (a a' b' x : R) (hx : O.nn x)
    (s1 : K.add a' (K.neg a) = K.neg (K.mul x (K.add a' a)))
    (s2 : K.add b' (K.neg a) = K.neg (K.mul x (K.add b' a))) : a' = b' := by
  have e1 := lin_form K a a' x s1
  have e2 := lin_form K a b' x s2
  have d : K.mul (K.add K.one x) (K.add a' (K.neg b')) = K.zero := by
    have : K.mul (K.add K.one x) (K.add a' (K.neg b')) = K.add (K.mul (K.add K.one x) a')
        (K.neg (K.mul (K.add K.one x) b')) := by lib_ring
    rw [this, e1, e2]; lib_ring
  cases O.noZD _ _ d with
  | inl h => exact absurd h (ne_of_one_add K O h10 x hx)
  | inr h =>
    have : a' = K.add (K.add a' (K.neg b')) b' := by lib_ring
    rw [this, h]; lib_ring

theorem lin_damp (h10 : K.one ≠ K.zero) (a a' x : R) (hx : O.nn x)
    (step : K.add a' (K.neg a) = K.neg (K.mul x (K.add a' a))) :
    Le K O.toCone (K.mul a' a') (K.mul a a) := by
  have e := lin_form K a a' x step
  -- (1+x)² (a² − a'²) = ((1+x)² − (1−x)²) a² = 4x a² ≥ 0
  have key : K.mul (K.mul (K.add K.one x) (K.add K.one x)) (K.add (K.mul a a) (K.neg (K.mul a' a')))
      = K.mul (K.add (K.add x x) (K.add x x)) (K.mul a a) := by
    have : K.mul (K.mul (K.add K.one x) (K.add K.one x)) (K.mul a' a')
        = K.mul (K.mul (K.add K.one (K.neg x)) (K.add K.one (K.neg x))) (K.mul a a) := by
      rw [show K.mul (K.mul (K.add K.one x) (K.add K.one x)) (K.mul a' a')
          = K.mul (K.mul (K.add K.one x) a') (K.mul (K.add K.one x) a') by lib_ring, e]; lib_ring
    rw [show K.mul (K.mul (K.add K.one x) (K.add K.one x)) (K.add (K.mul a a) (K.neg (K.mul a' a')))
        = K.add (K.mul (K.mul (K.add K.one x) (K.add K.one x)) (K.mul a a))
          (K.neg (K.mul (K.mul (K.add K.one x) (K.add K.one x)) (K.mul a' a'))) by lib_ring, this]
    lib_ring
  -- so D = a² − a'² satisfies (1+x)²·D ≥ 0 with (1+x)² > 0, hence D ≥ 0
  have hR : O.nn (K.mul (K.add (K.add x x) (K.add x x)) (K.mul a a)) :=
    O.nn_mul _ _ (O.nn_add _ _ (O.nn_add _ _ hx hx) (O.nn_add _ _ hx hx)) (O.nn_sq a)
  generalize hD : K.add (K.mul a a) (K.neg (K.mul a' a')) = D at key
  have hD' : O.nn D := by
    cases O.total K.zero D with
    | inl l => cases l with
      | intro d hd => rw [← hd.2, K.zero_add]; exact hd.1
    | inr l => cases l with
      | intro d hd =>
        -- D + d = 0, d ≥ 0, and (1+x)²·D ≥ 0 ⇒ (1+x)²·d ≤ 0 ⇒ (1+x)²·d = 0 ⇒ d = 0 ⇒ D = 0
        have dD : D = K.neg d := by
          have : D = K.add (K.add D d) (K.neg d) := by lib_ring
          rw [this, hd.2]; lib_ring
        have q0 : O.nn (K.neg (K.mul (K.mul (K.add K.one x) (K.add K.one x)) d)) := by
          rw [show K.neg (K.mul (K.mul (K.add K.one x) (K.add K.one x)) d)
              = K.mul (K.mul (K.add K.one x) (K.add K.one x)) D by rw [dD]; lib_ring, key]; exact hR
        have q1 : O.nn (K.mul (K.mul (K.add K.one x) (K.add K.one x)) d) := O.nn_mul _ _ (O.nn_sq _) hd.1
        have z : K.mul (K.mul (K.add K.one x) (K.add K.one x)) d = K.zero :=
          O.antisym _ _ ⟨_, q0, by lib_ring⟩ ⟨_, q1, K.zero_add _⟩
        have d0 : d = K.zero := by
          cases O.noZD _ _ z with
          | inl h => exact absurd h (mul_ne K O _ _ (ne_of_one_add K O h10 x hx) (ne_of_one_add K O h10 x hx))
          | inr h => exact h
        rw [dD, d0]; show O.nn (K.neg K.zero)
        rw [show K.neg K.zero = K.zero by lib_ring]; exact O.nn_zero
  exact ⟨D, hD', by rw [← hD]; lib_ring⟩

end Gen

end StokesCN

#print axioms StokesCN.lin_form
#print axioms StokesCN.lin_unique
#print axioms StokesCN.lin_damp

ns/Stretch3D.lean

lean_all/ns/Stretch3D.lean

/-
  ns/Stretch3D.lean — T37 cycle 14: in three dimensions a single triad transfers enstrophy.

  Lean 4 core; concrete integer computation by rfl.

  For the trilinear form of ns/FourierGalerkin (ι factored out),
      B(u, v, w) = Σ_{p,q,r distinct, k_p + k_q + k_r = 0} (u_p · k_q)(v_q · w_r),
  on the triad k₀ = (1,0,0), k₁ = (0,1,0), k₂ = (−1,−1,0) with divergence-free modes
  u₀ = (0,1,3), u₁ = (3,0,0), u₂ = (2,−2,−1):
    energy_zero        B(u, u, u) = 0                     (FourierGalerkin.neutral, here by computation)
    enstrophy_nonzero  B(u, u, |k|² u) = −9 ≠ 0            the |k|²-weighted product is not neutral
    planar_zero        with the out-of-plane components removed (a 2D flow), B(u, u, |k|² u) = 0
                       (Enstrophy2D.triad_enstrophy)
  So the hypothesis `neutral` of Enstrophy2D.cn_weighted with w = |k|² fails in 3D already for one
  triad: the vortex-stretching transfer, the place where the 2D argument stops (R1-gap).
-/
namespace Stretch3D

def dot (a b : Int × Int × Int) : Int := a.1 * b.1 + a.2.1 * b.2.1 + a.2.2 * b.2.2
def addv (a b : Int × Int × Int) : Int × Int × Int := (a.1 + b.1, a.2.1 + b.2.1, a.2.2 + b.2.2)
def z0 (z : Int) : Bool := match z with
  | Int.ofNat n => Nat.beq n 0
  | Int.negSucc _ => false
def isZero (a : Int × Int × Int) : Bool := z0 a.1 && z0 a.2.1 && z0 a.2.2

def kv : Nat → Int × Int × Int
  | 0 => (1, 0, 0)
  | 1 => (0, 1, 0)
  | _ => (-1, -1, 0)

def u3 : Nat → Int × Int × Int
  | 0 => (0, 1, 3)
  | 1 => (3, 0, 0)
  | _ => (2, -2, -1)

def u2 : Nat → Int × Int × Int
  | 0 => (0, 1, 0)
  | 1 => (3, 0, 0)
  | _ => (2, -2, 0)

def scale (c : Int) (a : Int × Int × Int) : Int × Int × Int := (c * a.1, c * a.2.1, c * a.2.2)

def idx : List Nat := [0, 1, 2]

def B (u v w : Nat → Int × Int × Int) : Int :=
  idx.foldl (fun s p => idx.foldl (fun s q => idx.foldl (fun s r =>
    cond ((!(Nat.beq p q) && !(Nat.beq q r) && !(Nat.beq p r)) && isZero (addv (addv (kv p) (kv q)) (kv r)))
      (s + dot (u p) (kv q) * dot (v q) (w r)) s) s) s) 0

def weighted (u : Nat → Int × Int × Int) : Nat → Int × Int × Int := fun i => scale (dot (kv i) (kv i)) (u i)

theorem div_free : dot (u3 0) (kv 0) = 0 ∧ dot (u3 1) (kv 1) = 0 ∧ dot (u3 2) (kv 2) = 0 ∧
    dot (u2 0) (kv 0) = 0 ∧ dot (u2 1) (kv 1) = 0 ∧ dot (u2 2) (kv 2) = 0 :=
  ⟨rfl, rfl, rfl, rfl, rfl, rfl⟩

theorem energy_zero : B u3 u3 u3 = 0 := rfl
theorem enstrophy_nonzero : B u3 u3 (weighted u3) = -9 := rfl
theorem planar_zero : B u2 u2 (weighted u2) = 0 := rfl

end Stretch3D

#print axioms Stretch3D.div_free
#print axioms Stretch3D.energy_zero
#print axioms Stretch3D.enstrophy_nonzero
#print axioms Stretch3D.planar_zero

ns/SumCS.lean

lean_all/ns/SumCS.lean

/-
  ns/SumCS.lean — T37 cycle 51: Cauchy–Schwarz for finite sums, by the Lagrange identity.

  Lean 4 core plus NSChain (lsum, Cone, Le) and lib/RingTac. For a list l and a, b : I → R:
      (Σa²)(Σb²) = (Σab)² + D,   D = Σ_{i before j} (a_i b_j − b_i a_j)²        (lagrange_sum)
      (Σab)² ≤ (Σa²)(Σb²)                                                     (cs_sum)
  in every ordered commutative ring. This is the tool that sums the per-triad bound (TriadBound) over
  all triads of a truncation: Σ|T| ≤ (Σ…)^{1/2}(Σ…)^{1/2} in squared form.
-/
import NSChain
import lib.RingTac

namespace SumCS
open DetChain NSChain LibRing

variable {R : Type} (K : CRing R) {I : Type}

def sq (x : R) : R := K.mul x x

def cross (a b : I → R) (x y : R) (l : List I) : R :=
  lsum K (fun i => sq K (sub K (K.mul x (b i)) (K.mul y (a i)))) l

def D (a b : I → R) : List I → R
  | [] => K.zero
  | i :: l => K.add (D a b l) (cross K a b (a i) (b i) l)

theorem inner (a b : I → R) (x y : R) : ∀ l : List I,
    K.add (K.mul (sq K x) (lsum K (fun i => sq K (b i)) l)) (K.mul (sq K y) (lsum K (fun i => sq K (a i)) l))
      = K.add (K.mul (K.add (K.mul x y) (K.mul x y)) (lsum K (fun i => K.mul (a i) (b i)) l)) (cross K a b x y l)
  | [] => by show K.add (K.mul (sq K x) K.zero) (K.mul (sq K y) K.zero) = K.add (K.mul _ K.zero) K.zero; lib_ring
  | i :: l => by
    have ih := inner a b x y l
    unfold cross at ih ⊢
    show K.add (K.mul (sq K x) (K.add (sq K (b i)) (lsum K (fun i => sq K (b i)) l)))
        (K.mul (sq K y) (K.add (sq K (a i)) (lsum K (fun i => sq K (a i)) l)))
      = K.add (K.mul (K.add (K.mul x y) (K.mul x y)) (K.add (K.mul (a i) (b i)) (lsum K (fun i => K.mul (a i) (b i)) l)))
          (K.add (sq K (sub K (K.mul x (b i)) (K.mul y (a i)))) (lsum K (fun i => sq K (sub K (K.mul x (b i)) (K.mul y (a i)))) l))
    revert ih
    generalize lsum K (fun i => sq K (b i)) l = B
    generalize lsum K (fun i => sq K (a i)) l = A
    generalize lsum K (fun i => K.mul (a i) (b i)) l = C
    generalize lsum K (fun i => sq K (sub K (K.mul x (b i)) (K.mul y (a i)))) l = X
    intro ih
    have e : K.add (K.mul (sq K x) (K.add (sq K (b i)) B)) (K.mul (sq K y) (K.add (sq K (a i)) A))
        = K.add (K.add (K.mul (sq K x) B) (K.mul (sq K y) A))
            (K.add (K.mul (sq K x) (sq K (b i))) (K.mul (sq K y) (sq K (a i)))) := by unfold sq; lib_ring
    rw [e, ih]; unfold sq; lib_ring

/-- **Lagrange identity for finite sums.** -/
theorem lagrange_sum (a b : I → R) : ∀ l : List I,
    K.mul (lsum K (fun i => sq K (a i)) l) (lsum K (fun i => sq K (b i)) l)
      = K.add (sq K (lsum K (fun i => K.mul (a i) (b i)) l)) (D K a b l)
  | [] => by show K.mul K.zero K.zero = K.add (K.mul K.zero K.zero) K.zero; lib_ring
  | i :: l => by
    have ih := lagrange_sum a b l
    have hin := inner K a b (a i) (b i) l
    show K.mul (K.add (sq K (a i)) (lsum K (fun i => sq K (a i)) l)) (K.add (sq K (b i)) (lsum K (fun i => sq K (b i)) l))
      = K.add (sq K (K.add (K.mul (a i) (b i)) (lsum K (fun i => K.mul (a i) (b i)) l)))
          (K.add (D K a b l) (cross K a b (a i) (b i) l))
    revert ih hin
    generalize lsum K (fun i => sq K (b i)) l = B
    generalize lsum K (fun i => sq K (a i)) l = A
    generalize lsum K (fun i => K.mul (a i) (b i)) l = C
    generalize cross K a b (a i) (b i) l = X
    generalize D K a b l = Dl
    intro ih hin
    have e : K.mul (K.add (sq K (a i)) A) (K.add (sq K (b i)) B)
        = K.add (K.add (K.mul (sq K (a i)) (sq K (b i))) (K.add (K.mul (sq K (a i)) B) (K.mul (sq K (b i)) A)))
            (K.mul A B) := by unfold sq; lib_ring
    rw [e, hin, ih]; unfold sq; lib_ring

theorem D_nn (P : Cone K) (a b : I → R) : ∀ l : List I, P.nn (D K a b l)
  | [] => P.nn_zero
  | i :: l => P.nn_add _ _ (D_nn P a b l) (by
      unfold cross
      generalize l = m
      induction m with
      | nil => exact P.nn_zero
      | cons j m ihm => exact P.nn_add _ _ (P.nn_sq _) ihm)

/-- **Cauchy–Schwarz for finite sums.** -/
theorem cs_sum (P : Cone K) (a b : I → R) (l : List I) :
    Le K P (sq K (lsum K (fun i => K.mul (a i) (b i)) l))
      (K.mul (lsum K (fun i => sq K (a i)) l) (lsum K (fun i => sq K (b i)) l)) :=
  ⟨D K a b l, D_nn K P a b l, (lagrange_sum K a b l).symm⟩

end SumCS

#print axioms SumCS.inner
#print axioms SumCS.lagrange_sum
#print axioms SumCS.D_nn
#print axioms SumCS.cs_sum

ns/TeleCS.lean

lean_all/ns/TeleCS.lean

/-
  ns/TeleCS.lean — T37 cycle 1796 (goal G856b): comparing two time-integrated scheme equations (SchemeTele), over
  an ordered commutative ring.
    tsum_eq    Σ_{t<n} f_t (SchemeTele.tsum) = the list sum over [n−1, …, 0]
    tsum_cs    (Σ_{t<n} h·f_t)² ≤ (n·h)·Σ_{t<n} h·f_t²                     (SumCS.cs_sum with a = h, b = f)
    sq3        (x + y + z)² ≤ 3(x² + y² + z²)
    tele_diff  for x_n = x_0 + Σ h(N_t − c·x_{t+1}) and x'_n likewise (same c = νw):
               (x_n − x'_n)² ≤ 3[(x_0 − x'_0)² + (n·h)·Σ h(N_t − N'_t)² + c²·(n·h)·Σ h(x_{t+1} − x'_{t+1})²]
  so on [0, T] (nh = T) the coefficient difference is controlled by the initial difference and the L²-in-time
  differences of the forces and coefficients: the integral equation passes to the limit once these converge.
  Lean 4 core plus ns/SchemeTele, ns/SumCS.
-/
import ns.SchemeTele
import ns.SumCS
import ns.DyadicLarge

namespace TeleCS
open DetChain NSChain DyadicSmallGlobal DyadicLarge LibRing EnstStep SchemeTele

section Gen
variable {R : Type} (K : CRing R) (O : OrdR K)

def tl : Nat → List Nat
  | 0 => []
  | n + 1 => n :: tl n

theorem tsum_eq (f : Nat → R) : ∀ n, tsum K f n = lsum K f (tl n)
  | 0 => rfl
  | n + 1 => by
    show K.add (tsum K f n) (f n) = K.add (f n) (lsum K f (tl n))
    rw [tsum_eq f n]; lib_ring

theorem lsum_const (c : R) : ∀ n, lsum K (fun _ => c) (tl n) = K.mul (castN K n) c
  | 0 => by show K.zero = K.mul K.zero c; lib_ring
  | n + 1 => by
    show K.add c (lsum K (fun _ => c) (tl n)) = K.mul (K.add K.one (castN K n)) c
    rw [lsum_const c n]; lib_ring

theorem tsum_cs (h : R) (f : Nat → R) (n : Nat) :
    Le K O.toCone (K.mul (tsum K (fun t => K.mul h (f t)) n) (tsum K (fun t => K.mul h (f t)) n))
      (K.mul (K.mul (castN K n) h) (tsum K (fun t => K.mul h (K.mul (f t) (f t))) n)) := by
  have cs := SumCS.cs_sum K O.toCone (fun _ => h) f (tl n)
  rw [tsum_eq K _ n, tsum_eq K _ n]
  have e1 : lsum K (fun _ => SumCS.sq K h) (tl n) = K.mul (castN K n) (K.mul h h) := lsum_const K (SumCS.sq K h) n
  have e2 : lsum K (fun t => K.mul h (K.mul (f t) (f t))) (tl n) = K.mul h (lsum K (fun t => K.mul (f t) (f t)) (tl n)) :=
    lsum_mul_left K h _ (tl n)
  rw [e2]
  have cs' : Le K O.toCone (K.mul (lsum K (fun t => K.mul h (f t)) (tl n)) (lsum K (fun t => K.mul h (f t)) (tl n)))
      (K.mul (lsum K (fun _ => SumCS.sq K h) (tl n)) (lsum K (fun t => K.mul (f t) (f t)) (tl n))) := cs
  rw [e1] at cs'
  refine le_trans' K O.toCone _ _ _ cs' ⟨K.zero, O.nn_zero, ?_⟩
  generalize lsum K (fun t => K.mul (f t) (f t)) (tl n) = S
  generalize castN K n = c
  lib_ring

theorem sq3 (x y z : R) :
    Le K O.toCone (K.mul (K.add (K.add x y) z) (K.add (K.add x y) z))
      (K.mul (K.add (K.add K.one K.one) K.one) (K.add (K.add (K.mul x x) (K.mul y y)) (K.mul z z))) := by
  refine ⟨K.add (K.add (K.mul (K.add x (K.neg y)) (K.add x (K.neg y))) (K.mul (K.add y (K.neg z)) (K.add y (K.neg z))))
      (K.mul (K.add x (K.neg z)) (K.add x (K.neg z))),
    O.nn_add _ _ (O.nn_add _ _ (O.nn_sq _) (O.nn_sq _)) (O.nn_sq _), ?_⟩
  lib_ring

theorem tele_diff (h c : R) (x x' N N' : Nat → R) (n : Nat)
    (ex : x n = K.add (x 0) (tsum K (fun t => K.mul h (K.add (N t) (K.neg (K.mul c (x (t + 1)))))) n))
    (ex' : x' n = K.add (x' 0) (tsum K (fun t => K.mul h (K.add (N' t) (K.neg (K.mul c (x' (t + 1)))))) n)) :
    Le K O.toCone (K.mul (K.add (x n) (K.neg (x' n))) (K.add (x n) (K.neg (x' n))))
      (K.mul (K.add (K.add K.one K.one) K.one)
        (K.add (K.add (K.mul (K.add (x 0) (K.neg (x' 0))) (K.add (x 0) (K.neg (x' 0))))
          (K.mul (K.mul (castN K n) h) (tsum K (fun t => K.mul h (K.mul (K.add (N t) (K.neg (N' t))) (K.add (N t) (K.neg (N' t))))) n)))
          (K.mul (K.mul c c) (K.mul (K.mul (castN K n) h) (tsum K (fun t => K.mul h
            (K.mul (K.add (x (t + 1)) (K.neg (x' (t + 1)))) (K.add (x (t + 1)) (K.neg (x' (t + 1)))))) n))))) := by
  have three : O.nn (K.add (K.add K.one K.one) K.one) :=
    O.nn_add _ _ (O.nn_add _ _ (RatModel.nn_one' K O.toCone) (RatModel.nn_one' K O.toCone)) (RatModel.nn_one' K O.toCone)
  -- the difference as d0 + Σh(ΔN) + (−c)·Σh(Δx)
  have split : ∀ m, K.add (tsum K (fun t => K.mul h (K.add (N t) (K.neg (K.mul c (x (t + 1)))))) m)
      (K.neg (tsum K (fun t => K.mul h (K.add (N' t) (K.neg (K.mul c (x' (t + 1)))))) m))
      = K.add (tsum K (fun t => K.mul h (K.add (N t) (K.neg (N' t)))) m)
        (K.mul (K.neg c) (tsum K (fun t => K.mul h (K.add (x (t + 1)) (K.neg (x' (t + 1))))) m)) := by
    intro m
    induction m with
    | zero => show K.add K.zero (K.neg K.zero) = K.add K.zero (K.mul (K.neg c) K.zero); lib_ring
    | succ m ih =>
      show K.add (K.add (tsum K (fun t => K.mul h (K.add (N t) (K.neg (K.mul c (x (t + 1)))))) m)
          (K.mul h (K.add (N m) (K.neg (K.mul c (x (m + 1)))))))
        (K.neg (K.add (tsum K (fun t => K.mul h (K.add (N' t) (K.neg (K.mul c (x' (t + 1)))))) m)
          (K.mul h (K.add (N' m) (K.neg (K.mul c (x' (m + 1))))))))
        = K.add (K.add (tsum K (fun t => K.mul h (K.add (N t) (K.neg (N' t)))) m) (K.mul h (K.add (N m) (K.neg (N' m)))))
          (K.mul (K.neg c) (K.add (tsum K (fun t => K.mul h (K.add (x (t + 1)) (K.neg (x' (t + 1))))) m)
            (K.mul h (K.add (x (m + 1)) (K.neg (x' (m + 1)))))))
      have e : K.add (K.add (tsum K (fun t => K.mul h (K.add (N t) (K.neg (K.mul c (x (t + 1)))))) m)
          (K.mul h (K.add (N m) (K.neg (K.mul c (x (m + 1)))))))
        (K.neg (K.add (tsum K (fun t => K.mul h (K.add (N' t) (K.neg (K.mul c (x' (t + 1)))))) m)
          (K.mul h (K.add (N' m) (K.neg (K.mul c (x' (m + 1))))))))
        = K.add (K.add (tsum K (fun t => K.mul h (K.add (N t) (K.neg (K.mul c (x (t + 1)))))) m)
          (K.neg (tsum K (fun t => K.mul h (K.add (N' t) (K.neg (K.mul c (x' (t + 1)))))) m)))
          (K.add (K.mul h (K.add (N m) (K.neg (K.mul c (x (m + 1))))))
            (K.neg (K.mul h (K.add (N' m) (K.neg (K.mul c (x' (m + 1)))))))) := by lib_ring
      rw [e, ih]
      generalize tsum K (fun t => K.mul h (K.add (N t) (K.neg (N' t)))) m = A
      generalize tsum K (fun t => K.mul h (K.add (x (t + 1)) (K.neg (x' (t + 1))))) m = B
      lib_ring
  have d : K.add (x n) (K.neg (x' n)) = K.add (K.add (K.add (x 0) (K.neg (x' 0)))
      (tsum K (fun t => K.mul h (K.add (N t) (K.neg (N' t)))) n))
      (K.mul (K.neg c) (tsum K (fun t => K.mul h (K.add (x (t + 1)) (K.neg (x' (t + 1))))) n)) := by
    rw [ex, ex']
    have s := split n
    generalize tsum K (fun t => K.mul h (K.add (N t) (K.neg (K.mul c (x (t + 1)))))) n = P at s ⊢
    generalize tsum K (fun t => K.mul h (K.add (N' t) (K.neg (K.mul c (x' (t + 1)))))) n = P' at s ⊢
    calc K.add (K.add (x 0) P) (K.neg (K.add (x' 0) P')) = K.add (K.add (x 0) (K.neg (x' 0))) (K.add P (K.neg P')) := by lib_ring
      _ = _ := by rw [s]; lib_ring
  rw [d]
  have q := sq3 K O (K.add (x 0) (K.neg (x' 0))) (tsum K (fun t => K.mul h (K.add (N t) (K.neg (N' t)))) n)
    (K.mul (K.neg c) (tsum K (fun t => K.mul h (K.add (x (t + 1)) (K.neg (x' (t + 1))))) n))
  have c1 := tsum_cs K O h (fun t => K.add (N t) (K.neg (N' t))) n
  have c2 := tsum_cs K O h (fun t => K.add (x (t + 1)) (K.neg (x' (t + 1)))) n
  have c2' : Le K O.toCone (K.mul (K.mul (K.neg c) (tsum K (fun t => K.mul h (K.add (x (t + 1)) (K.neg (x' (t + 1))))) n))
      (K.mul (K.neg c) (tsum K (fun t => K.mul h (K.add (x (t + 1)) (K.neg (x' (t + 1))))) n)))
      (K.mul (K.mul c c) (K.mul (K.mul (castN K n) h) (tsum K (fun t => K.mul h
        (K.mul (K.add (x (t + 1)) (K.neg (x' (t + 1)))) (K.add (x (t + 1)) (K.neg (x' (t + 1)))))) n))) := by
    have m := le_mul_nn K O.toCone (K.mul c c) _ _ (O.nn_sq c) c2
    refine le_trans' K O.toCone _ _ _ ⟨K.zero, O.nn_zero, ?_⟩ m
    generalize tsum K (fun t => K.mul h (K.add (x (t + 1)) (K.neg (x' (t + 1))))) n = B
    lib_ring
  exact le_trans' K O.toCone _ _ _ q (le_mul_nn K O.toCone _ _ _ three
    (le_add' K O.toCone _ _ _ _ (le_add' K O.toCone _ _ _ _ (le_refl' K O.toCone _) c1) c2'))

end Gen
end TeleCS

#print axioms TeleCS.tsum_eq
#print axioms TeleCS.lsum_const
#print axioms TeleCS.tsum_cs
#print axioms TeleCS.sq3
#print axioms TeleCS.tele_diff

ns/TeleSum.lean

lean_all/ns/TeleSum.lean

/-
  ns/TeleSum.lean — T37 cycle 941 (goal G624c, piece 1): telescoping bounds for the lattice sums of ForceSob, over any
  commutative ring with a nonnegative cone (ℚ included).
    tele_le     a_n + b_{n+1} ≤ b_n for all n  ⇒  Σ_{n<N} a_n + b_N ≤ b_0
    basel_le    z_0 ≤ w_0 = 1, z_{n+1} + w_{n+1} ≤ w_n, w ≥ 0  ⇒  Σ_{n<N} z_n ≤ 2
                (with z_n = 1/(n+1)², w_n = 1/(n+1): the Basel partial sums are bounded uniformly in N, since
                1/(n+2)² ≤ 1/((n+1)(n+2)) = 1/(n+1) − 1/(n+2)).
  The shell count of the lattice cube (24n² + 2 points with |k|_∞ = n) then gives Σ_{0<|k|_∞≤N} |k|^{−4} ≤ C
  uniformly in N.

  Lean 4 core plus NSChain (Cone, Le, sumN), ns/DyadicLarge, lib/RingTac.
-/
import ns.DyadicLarge
import lib.RingTac

namespace TeleSum
open DetChain NSChain LibRing DyadicLarge

section Gen
variable {R : Type} (K : CRing R) (P : Cone K)

theorem tele_le (a b : Nat → R) (h : ∀ n, Le K P (K.add (a n) (b (n + 1))) (b n)) :
    ∀ N, Le K P (K.add (sumN K a N) (b N)) (b 0)
  | 0 => ⟨K.zero, P.nn_zero, by show K.add (K.add K.zero (b 0)) K.zero = b 0; lib_ring⟩
  | N + 1 => by
    have ih := tele_le a b h N
    cases h N with
    | intro d hd => cases ih with
      | intro d' hd' =>
        refine ⟨K.add d d', P.nn_add _ _ hd.1 hd'.1, ?_⟩
        show K.add (K.add (K.add (sumN K a N) (a N)) (b (N + 1))) (K.add d d') = b 0
        rw [← hd'.2, ← hd.2]; lib_ring

theorem basel_le (z w : Nat → R) (hw : ∀ n, P.nn (w n)) (hw0 : w 0 = K.one) (hz0 : Le K P (z 0) K.one)
    (h : ∀ n, Le K P (K.add (z (n + 1)) (w (n + 1))) (w n)) :
    ∀ N, Le K P (sumN K z N) (K.add K.one K.one)
  | 0 => ⟨K.add K.one K.one, P.nn_add _ _ (by have := P.nn_sq K.one; rwa [K.one_mul] at this)
      (by have := P.nn_sq K.one; rwa [K.one_mul] at this), by show K.add K.zero _ = _; lib_ring⟩
  | N + 1 => by
    -- Σ_{n<N+1} z_n = z_0 + Σ_{n<N} z_{n+1} ≤ 1 + (w_0 − w_N) ≤ 1 + 1
    have t := tele_le K P (fun n => z (n + 1)) w h N
    have shift : ∀ M, sumN K z (M + 1) = K.add (z 0) (sumN K (fun n => z (n + 1)) M) := by
      intro M
      induction M with
      | zero => show K.add K.zero (z 0) = K.add (z 0) K.zero; lib_ring
      | succ M ih =>
        show K.add (sumN K z (M + 1)) (z (M + 1)) = K.add (z 0) (K.add (sumN K (fun n => z (n + 1)) M) (z (M + 1)))
        rw [ih]; lib_ring
    rw [shift]
    rw [hw0] at t
    cases t with
    | intro d hd => cases hz0 with
      | intro d0 hd0 =>
        refine ⟨K.add d0 (K.add d (w N)), P.nn_add _ _ hd0.1 (P.nn_add _ _ hd.1 (hw N)), ?_⟩
        rw [← hd.2]
        calc _ = K.add (K.add (z 0) d0) (K.add (K.add (sumN K (fun n => z (n + 1)) N) (w N)) d) := by lib_ring
          _ = K.add K.one (K.add (K.add (sumN K (fun n => z (n + 1)) N) (w N)) d) := by rw [hd0.2]
          _ = _ := by rw [hd.2]

end Gen

end TeleSum

#print axioms TeleSum.tele_le
#print axioms TeleSum.basel_le

ns/TimeLip.lean

lean_all/ns/TimeLip.lean

/-
  ns/TimeLip.lean — T37 cycle 1075 (goal G656, equicontinuity): one implicit-viscosity step moves the solution by at most
    time_lip   Σ_r |a'_r − a_r|² ≤ 2h²·(Σ_r |N_r|² + ν²·Σ_r w_r²|a_r|²)
  over any commutative ring with a nonnegative cone (h, ν, w ≥ 0): from (1 + x)(a' − a) = hN − x a with x = hνw ≥ 0,
  |a' − a| ≤ |hN − x a| ((1+x)² ≥ 1) and |hN − xa|² ≤ 2h²|N|² + 2x²|a|². With ForceUniform (Σ|N|² ≤ 135ι²·A·Z) and the
  local bounds this is a truncation-uniform time-Lipschitz (equicontinuity) estimate in L², an ingredient for the h → 0
  compactness.

  Lean 4 core plus ns/EnstDiff (sub3; EnstStep: StepC, Step), ns/ForceBound (lsum_le), lib/RingTac.
-/
import ns.EnstDiff

namespace TimeLip
open DetChain NSChain LibRing DyadicLarge FourierGalerkin ForceBound EnstStep

section Gen
variable {R : Type} (K : CRing R) (P : Cone K) {I : Type}

theorem comp_lip (h ν w a' a n : R) (hh : P.nn h) (hν : P.nn ν) (hw : P.nn w) (hs : StepC K h ν w a' a n) :
    Le K P (K.mul (K.add a' (K.neg a)) (K.add a' (K.neg a)))
      (K.add (K.mul (K.add K.one K.one) (K.mul (K.mul h h) (K.mul n n)))
        (K.mul (K.add K.one K.one) (K.mul (K.mul (K.mul (K.mul h ν) w) (K.mul (K.mul h ν) w)) (K.mul a a)))) := by
  have hx : P.nn (K.mul (K.mul h ν) w) := P.nn_mul _ _ (P.nn_mul _ _ hh hν) hw
  unfold StepC at hs
  generalize hxd : K.mul (K.mul h ν) w = x at hs hx
  -- y = a' − a satisfies (1 + x) y = hN − x a
  generalize hy : K.add a' (K.neg a) = y
  have hy2 : K.mul (K.add K.one x) y = K.add (K.mul h n) (K.neg (K.mul x a)) := by
    rw [← hy]
    calc _ = K.add (K.mul (K.add K.one x) a') (K.neg (K.mul (K.add K.one x) a)) := by lib_ring
      _ = _ := by rw [hs]; lib_ring
  generalize hz : K.add (K.mul h n) (K.neg (K.mul x a)) = z at hy2
  -- y² ≤ z² = y²(1+x)²;  z² ≤ 2h²n² + 2x²a²
  refine ⟨K.add (K.mul (K.mul y y) (K.add (K.add x x) (K.mul x x)))
      (K.mul (K.add (K.mul h n) (K.mul x a)) (K.add (K.mul h n) (K.mul x a))),
    P.nn_add _ _ (P.nn_mul _ _ (P.nn_sq _) (P.nn_add _ _ (P.nn_add _ _ hx hx) (P.nn_sq _))) (P.nn_sq _), ?_⟩
  have hzz : K.mul z z = K.mul (K.mul (K.add K.one x) (K.add K.one x)) (K.mul y y) := by
    rw [← hy2]; lib_ring
  have zdef : z = K.add (K.mul h n) (K.neg (K.mul x a)) := hz.symm
  calc K.add (K.mul y y) (K.add (K.mul (K.mul y y) (K.add (K.add x x) (K.mul x x)))
        (K.mul (K.add (K.mul h n) (K.mul x a)) (K.add (K.mul h n) (K.mul x a))))
      = K.add (K.mul (K.mul (K.add K.one x) (K.add K.one x)) (K.mul y y))
          (K.mul (K.add (K.mul h n) (K.mul x a)) (K.add (K.mul h n) (K.mul x a))) := by lib_ring
    _ = K.add (K.mul z z) (K.mul (K.add (K.mul h n) (K.mul x a)) (K.add (K.mul h n) (K.mul x a))) := by rw [hzz]
    _ = _ := by rw [zdef]; lib_ring

theorem mode_lip (h ν w : R) (hh : P.nn h) (hν : P.nn ν) (hw : P.nn w) (a' a N : V3 R) (hs : Step K h ν w a' a N) :
    Le K P (dot3 K (EnstDiff.sub3 K a' a) (EnstDiff.sub3 K a' a))
      (K.add (K.mul (K.add K.one K.one) (K.mul (K.mul h h) (dot3 K N N)))
        (K.mul (K.add K.one K.one) (K.mul (K.mul (K.mul (K.mul h ν) w) (K.mul (K.mul h ν) w)) (dot3 K a a)))) := by
  have cx := comp_lip K P h ν w a'.x a.x N.x hh hν hw hs.1
  have cy := comp_lip K P h ν w a'.y a.y N.y hh hν hw hs.2.1
  have cz := comp_lip K P h ν w a'.z a.z N.z hh hν hw hs.2.2
  have s := le_add' K P _ _ _ _ (le_add' K P _ _ _ _ cx cy) cz
  refine le_trans' K P _ _ _ s ⟨K.zero, P.nn_zero, ?_⟩
  show K.add (K.add (K.add _ _) _) K.zero
    = K.add (K.mul (K.add K.one K.one) (K.mul (K.mul h h) (K.add (K.add (K.mul N.x N.x) (K.mul N.y N.y)) (K.mul N.z N.z))))
      (K.mul (K.add K.one K.one) (K.mul (K.mul (K.mul (K.mul h ν) w) (K.mul (K.mul h ν) w))
        (K.add (K.add (K.mul a.x a.x) (K.mul a.y a.y)) (K.mul a.z a.z))))
  lib_ring

/-- Σ_r |a'_r − a_r|² ≤ 2h²Σ|N_r|² + 2h²ν²Σ w_r²|a_r|² -/
theorem time_lip (l : List I) (h ν : R) (hh : P.nn h) (hν : P.nn ν) (w : I → R) (hw : ∀ r, P.nn (w r))
    (a' a N : I → V3 R) (hs : ∀ r, Step K h ν (w r) (a' r) (a r) (N r)) :
    Le K P (lsum K (fun r => dot3 K (EnstDiff.sub3 K (a' r) (a r)) (EnstDiff.sub3 K (a' r) (a r))) l)
      (K.add (K.mul (K.mul (K.add K.one K.one) (K.mul h h)) (lsum K (fun r => dot3 K (N r) (N r)) l))
        (K.mul (K.mul (K.add K.one K.one) (K.mul (K.mul h h) (K.mul ν ν)))
          (lsum K (fun r => K.mul (K.mul (w r) (w r)) (dot3 K (a r) (a r))) l))) := by
  have t := lsum_le K P _ _ (fun r => mode_lip K P h ν (w r) hh hν (hw r) (a' r) (a r) (N r) (hs r)) l
  refine le_trans' K P _ _ _ t ⟨K.zero, P.nn_zero, ?_⟩
  rw [add_zero', ← lsum_mul_left, ← lsum_mul_left, ← lsum_add]
  exact lsum_ext K _ _ (fun r => by lib_ring) l

end Gen

end TimeLip

#print axioms TimeLip.comp_lip
#print axioms TimeLip.mode_lip
#print axioms TimeLip.time_lip

ns/TimeLipN.lean

lean_all/ns/TimeLipN.lean

/-
  ns/TimeLipN.lean — T37 cycle 1077 (goal G656c, equicontinuity over many steps): over any commutative ring with a
  nonnegative cone,
    cs_sum     (Σ_{j<n} x_j)² ≤ n · Σ_{j<n} x_j²            (Lagrange: n Q − S² = Σ_{i<j}(x_i − x_j)², by induction)
    lip_n      Σ_r |a_{t+n,r} − a_{t,r}|² ≤ n · Σ_{j<n} Σ_r |a_{t+j+1,r} − a_{t+j,r}|²
  so with TimeLip (each step ≤ 2h²C) the solution moves by at most n²·2h²C = 2C·(nh)² in n steps: Lipschitz in time
  with a constant independent of the truncation.

  Lean 4 core plus NSChain (sumN), ns/EnstDiff (sub3), ns/ForceBound, lib/RingTac.
-/
import ns.EnstDiff

namespace TimeLipN
open DetChain NSChain LibRing DyadicLarge FourierGalerkin ForceBound EnstDiff

section Gen
variable {R : Type} (K : CRing R) (P : Cone K)

/-- Σ_{i<n} (x_i − y)² = Q_n − 2yS_n + n y² -/
theorem sq_dev (x : Nat → R) (y : R) : ∀ n,
    sumN K (fun i => K.mul (K.add (x i) (K.neg y)) (K.add (x i) (K.neg y))) n
      = K.add (K.add (sumN K (fun i => K.mul (x i) (x i)) n) (K.neg (K.mul (K.add y y) (sumN K x n))))
          (K.mul (castN K n) (K.mul y y))
  | 0 => by show K.zero = K.add (K.add K.zero (K.neg (K.mul (K.add y y) K.zero))) (K.mul K.zero (K.mul y y)); lib_ring
  | n + 1 => by
    show K.add (sumN K (fun i => K.mul (K.add (x i) (K.neg y)) (K.add (x i) (K.neg y))) n) (K.mul (K.add (x n) (K.neg y)) (K.add (x n) (K.neg y)))
      = K.add (K.add (K.add (sumN K (fun i => K.mul (x i) (x i)) n) (K.mul (x n) (x n)))
          (K.neg (K.mul (K.add y y) (K.add (sumN K x n) (x n))))) (K.mul (K.add K.one (castN K n)) (K.mul y y))
    rw [sq_dev x y n]; lib_ring

theorem sumN_sq_nn (f : Nat → R) : ∀ m, P.nn (sumN K (fun i => K.mul (f i) (f i)) m)
  | 0 => P.nn_zero
  | m + 1 => P.nn_add _ _ (sumN_sq_nn f m) (P.nn_sq _)

theorem cs_sum (x : Nat → R) : ∀ n, Le K P (K.mul (sumN K x n) (sumN K x n))
    (K.mul (castN K n) (sumN K (fun i => K.mul (x i) (x i)) n))
  | 0 => ⟨K.zero, P.nn_zero, by show K.add (K.mul K.zero K.zero) K.zero = K.mul K.zero K.zero; lib_ring⟩
  | n + 1 => by
    have ih := cs_sum x n
    cases ih with
    | intro D hD =>
      refine ⟨K.add D (sumN K (fun i => K.mul (K.add (x i) (K.neg (x n))) (K.add (x i) (K.neg (x n)))) n),
        P.nn_add _ _ hD.1 (sumN_sq_nn K P (fun i => K.add (x i) (K.neg (x n))) n), ?_⟩
      rw [sq_dev K x (x n) n]
      show K.add (K.mul (K.add (sumN K x n) (x n)) (K.add (sumN K x n) (x n))) _
        = K.mul (K.add K.one (castN K n)) (K.add (sumN K (fun i => K.mul (x i) (x i)) n) (K.mul (x n) (x n)))
      have key := hD.2
      generalize sumN K x n = S at key ⊢
      generalize sumN K (fun i => K.mul (x i) (x i)) n = Q at key ⊢
      generalize castN K n = c at key ⊢
      calc _ = K.add (K.add (K.mul S S) D) (K.add (K.add Q (K.mul (x n) (x n))) (K.mul c (K.mul (x n) (x n)))) := by lib_ring
        _ = K.add (K.mul c Q) (K.add (K.add Q (K.mul (x n) (x n))) (K.mul c (K.mul (x n) (x n)))) := by rw [key]
        _ = _ := by lib_ring

theorem tele_fwd (g : Nat → R) : ∀ n, sumN K (fun j => K.add (g (j + 1)) (K.neg (g j))) n = K.add (g n) (K.neg (g 0))
  | 0 => by show K.zero = K.add (g 0) (K.neg (g 0)); lib_ring
  | n + 1 => by
    show K.add (sumN K (fun j => K.add (g (j + 1)) (K.neg (g j))) n) (K.add (g (n + 1)) (K.neg (g n)))
      = K.add (g (n + 1)) (K.neg (g 0))
    rw [tele_fwd g n]; lib_ring

theorem sumN_add3 (f g h : Nat → R) : ∀ n,
    K.add (K.add (sumN K f n) (sumN K g n)) (sumN K h n) = sumN K (fun j => K.add (K.add (f j) (g j)) (h j)) n
  | 0 => by show K.add (K.add K.zero K.zero) K.zero = K.zero; lib_ring
  | n + 1 => by
    show K.add (K.add (K.add (sumN K f n) (f n)) (K.add (sumN K g n) (g n))) (K.add (sumN K h n) (h n))
      = K.add (sumN K (fun j => K.add (K.add (f j) (g j)) (h j)) n) (K.add (K.add (f n) (g n)) (h n))
    rw [← sumN_add3 f g h n]; lib_ring

/-- one mode: |a_n − a_0|² ≤ n Σ_{j<n} |a_{j+1} − a_j|² -/
theorem mode_lipN (a : Nat → V3 R) (n : Nat) :
    Le K P (dot3 K (sub3 K (a n) (a 0)) (sub3 K (a n) (a 0)))
      (K.mul (castN K n) (sumN K (fun j => dot3 K (sub3 K (a (j + 1)) (a j)) (sub3 K (a (j + 1)) (a j))) n)) := by
  have cx := cs_sum K P (fun j => K.add (a (j + 1)).x (K.neg (a j).x)) n
  have cy := cs_sum K P (fun j => K.add (a (j + 1)).y (K.neg (a j).y)) n
  have cz := cs_sum K P (fun j => K.add (a (j + 1)).z (K.neg (a j).z)) n
  rw [tele_fwd K (fun j => (a j).x)] at cx
  rw [tele_fwd K (fun j => (a j).y)] at cy
  rw [tele_fwd K (fun j => (a j).z)] at cz
  have s := le_add' K P _ _ _ _ (le_add' K P _ _ _ _ cx cy) cz
  refine le_trans' K P _ _ _ s ⟨K.zero, P.nn_zero, ?_⟩
  have e := sumN_add3 K (fun j => K.mul (K.add (a (j + 1)).x (K.neg (a j).x)) (K.add (a (j + 1)).x (K.neg (a j).x)))
    (fun j => K.mul (K.add (a (j + 1)).y (K.neg (a j).y)) (K.add (a (j + 1)).y (K.neg (a j).y)))
    (fun j => K.mul (K.add (a (j + 1)).z (K.neg (a j).z)) (K.add (a (j + 1)).z (K.neg (a j).z))) n
  show K.add (K.add (K.add (K.mul (castN K n) _) (K.mul (castN K n) _)) (K.mul (castN K n) _)) K.zero = K.mul (castN K n) _
  rw [add_zero', ← K.distrib, ← K.distrib, e]
  rfl

theorem lsum_sumN {I : Type} (f : I → Nat → R) (l : List I) : ∀ n,
    lsum K (fun r => sumN K (f r) n) l = sumN K (fun j => lsum K (fun r => f r j) l) n
  | 0 => lsum_zero K l
  | n + 1 => by
    show lsum K (fun r => K.add (sumN K (f r) n) (f r n)) l = K.add (sumN K (fun j => lsum K (fun r => f r j) l) n) (lsum K (fun r => f r n) l)
    rw [lsum_add, lsum_sumN f l n]

/-- Σ_r |a_{n,r} − a_{0,r}|² ≤ n · Σ_{j<n} Σ_r |a_{j+1,r} − a_{j,r}|² -/
theorem lip_n {I : Type} (l : List I) (a : Nat → I → V3 R) (n : Nat) :
    Le K P (lsum K (fun r => dot3 K (sub3 K (a n r) (a 0 r)) (sub3 K (a n r) (a 0 r))) l)
      (K.mul (castN K n) (sumN K (fun j => lsum K (fun r => dot3 K (sub3 K (a (j + 1) r) (a j r)) (sub3 K (a (j + 1) r) (a j r))) l) n)) := by
  have t := lsum_le K P _ _ (fun r => mode_lipN K P (fun j => a j r) n) l
  rw [lsum_mul_left, lsum_sumN K (fun r j => dot3 K (sub3 K (a (j + 1) r) (a j r)) (sub3 K (a (j + 1) r) (a j r))) l n] at t
  exact t

end Gen

end TimeLipN

#print axioms TimeLipN.sq_dev
#print axioms TimeLipN.cs_sum
#print axioms TimeLipN.tele_fwd
#print axioms TimeLipN.mode_lipN
#print axioms TimeLipN.sumN_sq_nn
#print axioms TimeLipN.sumN_add3
#print axioms TimeLipN.lsum_sumN
#print axioms TimeLipN.lip_n

ns/TimeLipW.lean

lean_all/ns/TimeLipW.lean

/-
  ns/TimeLipW.lean — T37 cycle 1099 (goal G717, piece b3): the weighted one-step time-Lipschitz bound. Over any
  commutative ring with a nonnegative cone (h, ν, w_r, V_r ≥ 0):
    time_lipW   Σ_r V_r|a'_r − a_r|² ≤ 2h²·Σ V_r|N_r|² + 2h²ν²·Σ V_r w_r²|a_r|².
  With V = w (H¹ weight): Σ w|b₁ − a₀|² ≤ 2k²Σ w|N₀|² + 2k²ν²B, the O(k²) input of the force difference
  N₀ − N₁ in the step-size defect (DefectSum).

  Lean 4 core plus ns/TimeLip (mode_lip), ns/ForceBound (lsum_le), lib/RingTac.
-/
import ns.TimeLip

namespace TimeLipW
open DetChain NSChain LibRing DyadicLarge FourierGalerkin ForceBound EnstStep TimeLip

section Gen
variable {R : Type} (K : CRing R) (P : Cone K) {I : Type}

theorem time_lipW (l : List I) (h ν : R) (hh : P.nn h) (hν : P.nn ν) (w V : I → R) (hw : ∀ r, P.nn (w r))
    (hV : ∀ r, P.nn (V r)) (a' a N : I → V3 R) (hs : ∀ r, Step K h ν (w r) (a' r) (a r) (N r)) :
    Le K P (lsum K (fun r => K.mul (V r) (dot3 K (EnstDiff.sub3 K (a' r) (a r)) (EnstDiff.sub3 K (a' r) (a r)))) l)
      (K.add (K.mul (K.mul (K.add K.one K.one) (K.mul h h)) (lsum K (fun r => K.mul (V r) (dot3 K (N r) (N r))) l))
        (K.mul (K.mul (K.add K.one K.one) (K.mul (K.mul h h) (K.mul ν ν)))
          (lsum K (fun r => K.mul (K.mul (V r) (K.mul (w r) (w r))) (dot3 K (a r) (a r))) l))) := by
  have t := lsum_le K P _ _ (fun r => le_mul_nn K P (V r) _ _ (hV r)
    (mode_lip K P h ν (w r) hh hν (hw r) (a' r) (a r) (N r) (hs r))) l
  refine le_trans' K P _ _ _ t ⟨K.zero, P.nn_zero, ?_⟩
  rw [add_zero', ← lsum_mul_left, ← lsum_mul_left, ← lsum_add]
  exact lsum_ext K _ _ (fun r => by lib_ring) l

end Gen

end TimeLipW

#print axioms TimeLipW.time_lipW

ns/TransferBound.lean

lean_all/ns/TransferBound.lean

/-
  ns/TransferBound.lean — T37 cycle 248 (goal G6): the transfer hypothesis hT of CNSerrin from the triad sum.

  Lean 4 core plus ns/TriadLinf (stretch), ns/LocalContAgmon (toCone, le_of_Le), ns/SerrinAgmon (le_of_sq),
  lib/OField. If the transfer T of a step is dominated by the triad sum of magnitudes,
      T² ≤ (Σ_p a_p Σ_q b_q c_{σ(p,q)})²,   a ≥ 0,   Σ_q c_{σ(p,q)}² ≤ σ² for every p,   ρ² = Σ b²,
  then with X = Σ a (Wiener norm) and X, ρ, σ ≥ 0, Xρσ ≠ 0:
      T ≤ X ρ σ,   h T ≤ h (1·X) ρ σ                                             (transfer_bound)
  — the hypothesis hT of CNSerrin.cn_serrin with c = 1. The domination T² ≤ (Σ a S)² is the triangle inequality
  for the Fourier triad sum (|û_p·k_q||û_q·w_r| ≤ |û_p||k_q||û_q||w_r|, TriadBound), left as the per-truncation input.
-/
import ns.TriadLinf
import ns.LocalContAgmon

namespace TransferBound
open DetChain NSChain LibRing LibCS LibGron LibOF SerrinAgmon LocalContAgmon TriadLinf

variable {R : Type} (K : CRing R) {I : Type}

theorem le_of_sq_signed (P : Pos K) (F : OF K P) (T q : R) (hq : P.nn q) (hq0 : q ≠ K.zero)
    (h : le K P (K.mul T T) (K.mul q q)) : le K P T q := by
  cases F.total T with
  | inl hT => exact le_of_sq K P F T q hT hq hq0 h
  | inr hT =>
    unfold le
    rw [show sub K q T = K.add q (K.neg T) by rfl]
    exact P.nn_add _ _ hq hT

theorem transfer_bound (P : Pos K) (F : OF K P) (a b c : I → R) (σf : I → I → I) (l : List I)
    (T h X ρ σ : R) (hh : P.nn h) (ha : ∀ i, P.nn (a i))
    (hX : X = lsum K a l) (hρ : K.mul ρ ρ = lsum K (fun q => K.mul (b q) (b q)) l)
    (hC : ∀ p, le K P (lsum K (fun q => K.mul (c (σf p q)) (c (σf p q))) l) (K.mul σ σ))
    (hXn : P.nn X) (hρn : P.nn ρ) (hσn : P.nn σ) (h0 : K.mul (K.mul X ρ) σ ≠ K.zero)
    (hdom : le K P (K.mul T T)
      (K.mul (lsum K (fun p => K.mul (a p) (lsum K (fun q => K.mul (b q) (c (σf p q))) l)) l)
             (lsum K (fun p => K.mul (a p) (lsum K (fun q => K.mul (b q) (c (σf p q))) l)) l))) :
    le K P (K.mul h T) (K.mul h (K.mul (K.mul (K.mul K.one X) ρ) σ)) := by
  have st := le_of_Le K P _ _
    (stretch K (toCone K P) a b c σf (K.mul σ σ) l ha (fun p => Le_of_le K P _ _ (hC p)))
  have t := le_trans K P hdom st
  have e : K.mul (SumCS.sq K (lsum K a l)) (K.mul (lsum K (fun q => SumCS.sq K (b q)) l) (K.mul σ σ))
      = K.mul (K.mul (K.mul X ρ) σ) (K.mul (K.mul X ρ) σ) := by
    rw [hX] at *
    have : lsum K (fun q => SumCS.sq K (b q)) l = K.mul ρ ρ := by rw [hρ]; rfl
    rw [this]; unfold SumCS.sq; lib_ring
  rw [e] at t
  have r := le_of_sq_signed K P F T _ (P.nn_mul _ _ (P.nn_mul _ _ hXn hρn) hσn) h0 t
  have m := le_mul_left K P hh r
  rw [show K.mul (K.mul (K.mul K.one X) ρ) σ = K.mul (K.mul X ρ) σ by lib_ring]
  exact m

end TransferBound

#print axioms TransferBound.le_of_sq_signed
#print axioms TransferBound.transfer_bound

ns/Tri4.lean

lean_all/ns/Tri4.lean

/-
  ns/Tri4.lean — T37 cycle 1089 (goal G717 preparation): the fourth-power triad inequality. For integer triads
  k_p + k_q + k_r = 0 (cast into an ordered field):
    tri4          |k_r|⁴ ≤ 8(|k_p|⁴ + |k_q|⁴)      (square tri_sq and (a+b)² ≤ 2a² + 2b²)
    delta_tri4    δ·W_r ≤ δ·8(W_p + W_q)  for W = |k|⁴ — the hypothesis of ForceSobW.total_sobWc with c = 8.

  Lean 4 core plus ns/CastZAdd (tri_sq), ns/EnstLocal (sq_mono).
-/
import ns.CastZAdd
import ns.EnstLocal

namespace Tri4
open DetChain NSChain DyadicSmallGlobal LibRing DyadicLarge FourierGalerkin NormCompare CubeCount CastZAdd

section Gen
variable {R : Type} (K : CRing R) (O : OrdR K)

def k8 : R := 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))

theorem tri4 (kp kq kr : Mode) (h : TriadSparse.tadd (TriadSparse.tadd kp kq) kr = TriadSparse.zero3) :
    Le K O.toCone (K.mul (dot3 K (kvec K kr) (kvec K kr)) (dot3 K (kvec K kr) (kvec K kr)))
      (K.mul (k8 K) (K.add (K.mul (dot3 K (kvec K kp) (kvec K kp)) (dot3 K (kvec K kp) (kvec K kp)))
        (K.mul (dot3 K (kvec K kq) (kvec K kq)) (dot3 K (kvec K kq) (kvec K kq))))) := by
  have t := tri_sq K O.toCone kp kq kr h
  have nr : O.nn (dot3 K (kvec K kr) (kvec K kr)) := ForceBound.dot3_nn K O.toCone _
  generalize dot3 K (kvec K kp) (kvec K kp) = a at t ⊢
  generalize dot3 K (kvec K kq) (kvec K kq) = b at t ⊢
  generalize dot3 K (kvec K kr) (kvec K kr) = c at t nr ⊢
  have hs : O.nn (K.add (K.add a a) (K.add b b)) := by
    cases t with
    | intro d hd => rw [← hd.2]; exact O.nn_add _ _ nr hd.1
  have sq := EnstLocal.sq_mono K O _ _ nr hs t
  refine le_trans' K O.toCone _ _ _ sq ⟨K.mul (K.add (K.add K.one K.one) (K.add K.one K.one)) (K.mul (K.add a (K.neg b)) (K.add a (K.neg b))),
    O.nn_mul _ _ (O.nn_add _ _ (O.nn_add _ _ (RatModel.nn_one' K O.toCone) (RatModel.nn_one' K O.toCone))
      (O.nn_add _ _ (RatModel.nn_one' K O.toCone) (RatModel.nn_one' K O.toCone))) (O.nn_sq _), ?_⟩
  unfold k8; lib_ring

theorem delta_tri4 {I : Type} (kz : I → Mode) (p q r : I) :
    Le K O.toCone (K.mul (TriadSparse.delta K kz p q r)
        (K.mul (dot3 K (kvec K (kz r)) (kvec K (kz r))) (dot3 K (kvec K (kz r)) (kvec K (kz r)))))
      (K.mul (TriadSparse.delta K kz p q r) (K.mul (k8 K)
        (K.add (K.mul (dot3 K (kvec K (kz p)) (kvec K (kz p))) (dot3 K (kvec K (kz p)) (kvec K (kz p))))
          (K.mul (dot3 K (kvec K (kz q)) (kvec K (kz q))) (dot3 K (kvec K (kz q)) (kvec K (kz q))))))) := by
  show Le K O.toCone (K.mul (cond (decide (TriadSparse.tadd (TriadSparse.tadd (kz p) (kz q)) (kz r) = TriadSparse.zero3)) K.one K.zero) _)
    (K.mul (cond (decide (TriadSparse.tadd (TriadSparse.tadd (kz p) (kz q)) (kz r) = TriadSparse.zero3)) K.one K.zero) _)
  cases hd : decide (TriadSparse.tadd (TriadSparse.tadd (kz p) (kz q)) (kz r) = TriadSparse.zero3) with
  | false => exact ⟨K.zero, O.nn_zero, by show K.add (K.mul K.zero _) K.zero = K.mul K.zero _; lib_ring⟩
  | true => exact le_mul_nn K O.toCone K.one _ _ (RatModel.nn_one' K O.toCone) (tri4 K O (kz p) (kz q) (kz r) (of_decide_eq_true hd))

end Gen

end Tri4

#print axioms Tri4.tri4
#print axioms Tri4.delta_tri4

ns/TriadBound.lean

lean_all/ns/TriadBound.lean

/-
  ns/TriadBound.lean — T37 cycle 45: the size of one triad's transfer, by Cauchy–Schwarz.

  Lean 4 core plus ns/CFKernel (dot, cauchy_schwarz3, Cone, Le). For one term of the Fourier–Galerkin
  trilinear form, T = (u_p·k_q)(v_q·w_r),
      T² ≤ (|u_p|²|k_q|²)(|v_q|²|w_r|²)       (squares; no square roots needed)
  With w_r = |k_r|² u_r (enstrophy weight) this is the triad form of |dZ/dt| ≲ Σ |k_q||k_r|²|u_p||u_q||u_r|,
  the discrete local-existence estimate (Z' ≤ C Z^{3/2}-type), and with ns/Stretch3D it is attained up
  to constants by an explicit triad. What it cannot give is a bound uniform in the number of triads
  (R1-gap).
    le_mul_both    0 ≤ a ≤ b, 0 ≤ c ≤ d ⇒ ac ≤ bd (in a cone with the needed nonnegativity)
    triad_sq_bound T² ≤ (u·u)(k·k)(v·v)(w·w)
-/
import ns.CFKernel

namespace TriadBound
open DetChain NSChain CFKernel

section Ring
variable {R : Type} (K : CRing R)

theorem le_mul_both (P : Cone K) (a b c d : R) (ha : P.nn a) (hd : P.nn d)
    (h1 : Le K P a b) (h2 : Le K P c d) : Le K P (K.mul a c) (K.mul b d) := by
  cases h1 with
  | intro e1 h1 =>
    cases h2 with
    | intro e2 h2 =>
      -- b d = (a + e1)(c + e2)... use: a c + (a e2 + e1 d) = a d + e1 d = b d
      refine ⟨K.add (K.mul a e2) (K.mul e1 d), P.nn_add _ _ (P.nn_mul _ _ ha h2.1) (P.nn_mul _ _ h1.1 hd), ?_⟩
      rw [← K.add_assoc, ← K.distrib, h2.2, ← right_distrib K, h1.2]

/-- **One triad term, squared, is bounded by the product of the squared norms.** -/
theorem triad_sq_bound (P : Cone K) (u k v w : V R) :
    Le K P (K.mul (K.mul (dot K u k) (dot K u k)) (K.mul (dot K v w) (dot K v w)))
      (K.mul (K.mul (dot K u u) (dot K k k)) (K.mul (dot K v v) (dot K w w))) := by
  have nnd : ∀ a : V R, P.nn (dot K a a) := fun a => by
    unfold dot; exact P.nn_add _ _ (P.nn_add _ _ (P.nn_sq _) (P.nn_sq _)) (P.nn_sq _)
  exact le_mul_both K P _ _ _ _ (P.nn_sq _) (P.nn_mul _ _ (nnd v) (nnd w))
    (cauchy_schwarz3 K P u k) (cauchy_schwarz3 K P v w)

end Ring

end TriadBound

#print axioms TriadBound.le_mul_both
#print axioms TriadBound.triad_sq_bound

ns/TriadDom.lean

lean_all/ns/TriadDom.lean

/-
  ns/TriadDom.lean — T37 cycle 272 (goal G14): the domination hypothesis hdom of CNSerrinTriad from termwise triad
  bounds, every ordered field.

  Lean 4 core plus ns/SignedSum (signed_sum, prod_ne, sum_ne), ns/CNSerrinTriad, NSChain (lsum_mul_left).
    triad_dom        T = Σ_p Σ_q t(p,q),  t(p,q)² ≤ (A_p · B_q C_{σ(p,q)})²,  A, B, C ≥ 0, A_p ≠ 0, B_q C_{σ(p,q)} ≠ 0
                     ⇒  T² ≤ (Σ_p A_p Σ_q B_q C_{σ(p,q)})²        (inner and outer signed_sum)
    cn_serrin_triad_terms   CNSerrinTriad.cn_serrin_triad with hdom replaced by the triad expansion of the transfer
                     and the termwise bounds (TriadBound.triad_sq_bound gives them for Fourier triads).
-/
import ns.CNSerrinTriad

namespace TriadDom
open DetChain NSChain LibRing LibCS LibGron LibOF SerrinSum EnstrophyStep CNSerrin TransferBound SignedSum

variable {R : Type} (K : CRing R) {I : Type}

theorem le_refl_sq (P : Pos K) (x : R) : le K P (K.mul x x) (K.mul x x) := by
  unfold le; rw [show sub K (K.mul x x) (K.mul x x) = K.zero by lib_ring]; exact P.nn_zero

/-- **Domination from termwise triad bounds.** -/
theorem triad_dom (P : Pos K) (F : OF K P) (A B C : I → R) (σf : I → I → I) (t : I → I → R) (l : List I) (T : R)
    (hT : T = lsum K (fun p => lsum K (t p) l) l)
    (hA : ∀ p, P.nn (A p)) (hB : ∀ q, P.nn (B q)) (hC : ∀ r, P.nn (C r))
    (hA0 : ∀ p, A p ≠ K.zero) (hBC0 : ∀ p q, K.mul (B q) (C (σf p q)) ≠ K.zero)
    (ht : ∀ p q, le K P (K.mul (t p q) (t p q))
      (K.mul (K.mul (A p) (K.mul (B q) (C (σf p q)))) (K.mul (A p) (K.mul (B q) (C (σf p q)))))) :
    le K P (K.mul T T)
      (K.mul (lsum K (fun p => K.mul (A p) (lsum K (fun q => K.mul (B q) (C (σf p q))) l)) l)
             (lsum K (fun p => K.mul (A p) (lsum K (fun q => K.mul (B q) (C (σf p q))) l)) l)) := by
  rw [hT]
  cases l with
  | nil => exact le_refl_sq K P _
  | cons j l' =>
    -- inner: (Σ_q t(p,q))² ≤ (A_p Σ_q B_q C)²
    have inner : ∀ p, le K P (K.mul (lsum K (t p) (j :: l')) (lsum K (t p) (j :: l')))
        (K.mul (K.mul (A p) (lsum K (fun q => K.mul (B q) (C (σf p q))) (j :: l')))
               (K.mul (A p) (lsum K (fun q => K.mul (B q) (C (σf p q))) (j :: l')))) := by
      intro p
      have s := signed_sum K P F (t p) (fun q => K.mul (A p) (K.mul (B q) (C (σf p q))))
        (fun q => P.nn_mul _ _ (hA p) (P.nn_mul _ _ (hB q) (hC _)))
        (fun q => prod_ne K P F _ _ (hA0 p) (hBC0 p q)) (ht p) (j :: l')
      rw [lsum_mul_left K (A p) (fun q => K.mul (B q) (C (σf p q))) (j :: l')] at s
      exact s
    have hS : ∀ p, P.nn (lsum K (fun q => K.mul (B q) (C (σf p q))) (j :: l')) := by
      intro p
      have g : ∀ l : List I, P.nn (lsum K (fun q => K.mul (B q) (C (σf p q))) l) := by
        intro l; induction l with
        | nil => exact P.nn_zero
        | cons x l ihl => exact P.nn_add _ _ (P.nn_mul _ _ (hB x) (hC _)) ihl
      exact g _
    have hS0 : ∀ p, lsum K (fun q => K.mul (B q) (C (σf p q))) (j :: l') ≠ K.zero := fun p =>
      sum_ne K P F (fun q => K.mul (B q) (C (σf p q))) (fun q => P.nn_mul _ _ (hB q) (hC _)) (hBC0 p) j l'
    exact signed_sum K P F (fun p => lsum K (t p) (j :: l'))
      (fun p => K.mul (A p) (lsum K (fun q => K.mul (B q) (C (σf p q))) (j :: l')))
      (fun p => P.nn_mul _ _ (hA p) (hS p)) (fun p => prod_ne K P F _ _ (hA0 p) (hS0 p)) inner (j :: l')

/-- **Prodi–Serrin for a CN Galerkin run from termwise triad bounds.** -/
theorem cn_serrin_triad_terms (P : Pos K) (F : OF K P) (w k : I → R) (a Nn : Nat → I → R) (X ρ σ : Nat → R) (h ν : R)
    (l : List I) (A B C : Nat → I → R) (σf : Nat → I → I → I) (lt : Nat → List I) (tt : Nat → I → I → R)
    (hw : ∀ i, P.nn (w i)) (hh : P.nn h) (hν : P.nn ν) (hν0 : K.add ν ν ≠ K.zero)
    (h4 : K.add (K.add ν ν) (K.add ν ν) ≠ K.zero)
    (step : ∀ t i, sub K (a (t + 1) i) (a t i)
      = K.mul h (K.add (K.neg (K.mul ν (K.mul (k i) (K.add (a (t + 1) i) (a t i))))) (Nn t i)))
    (hσ : ∀ t, K.mul (σ t) (σ t)
      = lsum K (fun i => K.mul (w i) (K.mul (k i) (K.mul (K.add (a (t + 1) i) (a t i)) (K.add (a (t + 1) i) (a t i))))) l)
    (hρ : ∀ t, K.mul (ρ t) (ρ t) = lsum K (fun i => K.mul (w i) (K.mul (K.add (a (t + 1) i) (a t i)) (K.add (a (t + 1) i) (a t i)))) l)
    (hA : ∀ t i, P.nn (A t i)) (hBn : ∀ t i, P.nn (B t i)) (hCn : ∀ t i, P.nn (C t i))
    (hX : ∀ t, X t = lsum K (A t) (lt t))
    (hB : ∀ t, K.mul (ρ t) (ρ t) = lsum K (fun q => K.mul (B t q) (B t q)) (lt t))
    (hC : ∀ t p, le K P (lsum K (fun q => K.mul (C t (σf t p q)) (C t (σf t p q))) (lt t)) (K.mul (σ t) (σ t)))
    (hXn : ∀ t, P.nn (X t)) (hρn : ∀ t, P.nn (ρ t)) (hσn : ∀ t, P.nn (σ t))
    (h0 : ∀ t, K.mul (K.mul (X t) (ρ t)) (σ t) ≠ K.zero)
    -- the transfer as a triad sum, and the termwise bounds
    (hexp : ∀ t, lsum K (fun i => K.mul (w i) (K.mul (K.add (a (t + 1) i) (a t i)) (Nn t i))) l
      = lsum K (fun p => lsum K (tt t p) (lt t)) (lt t))
    (hA0 : ∀ t p, A t p ≠ K.zero) (hBC0 : ∀ t p q, K.mul (B t q) (C t (σf t p q)) ≠ K.zero)
    (htt : ∀ t p q, le K P (K.mul (tt t p q) (tt t p q))
      (K.mul (K.mul (A t p) (K.mul (B t q) (C t (σf t p q)))) (K.mul (A t p) (K.mul (B t q) (C t (σf t p q))))))
    (hb1 : ∀ t, P.nn (sub K K.one (bk K P F h K.one ν X t)))
    (N : Nat) (hS : P.nn (sub K K.one (sumA K (bk K P F h K.one ν X) N))) :
    le K P (K.mul (Zw K w (a N) l) (K.mul (sub K K.one (sumA K (bk K P F h K.one ν X) N))
      (sub K K.one (sumA K (bk K P F h K.one ν X) N)))) (Zw K w (a 0) l) :=
  CNSerrinTriad.cn_serrin_triad K P F w k a Nn X ρ σ h ν l A B C σf lt hw hh hν hν0 h4 step hσ hρ hA hX hB hC
    hXn hρn hσn h0
    (fun t => triad_dom K P F (A t) (B t) (C t) (σf t) (tt t) (lt t) _ (hexp t) (hA t) (hBn t) (hCn t) (hA0 t)
      (hBC0 t) (htt t))
    hb1 N hS

end TriadDom

#print axioms TriadDom.le_refl_sq
#print axioms TriadDom.triad_dom
#print axioms TriadDom.cn_serrin_triad_terms

ns/TriadDomEps.lean

lean_all/ns/TriadDomEps.lean

/-
  ns/TriadDomEps.lean — T37 cycle 293 (goal G36): TriadDom without the non-vanishing hypotheses, by an ε-shift.

  Lean 4 core plus ns/TriadDom (triad_dom), ns/SignedSum (prod_ne), lib/OField. For every ε ≥ 0, ε ≠ 0 the shifted
  magnitudes A + ε, B + ε, C + ε are non-zero (a + ε = 0 with a, ε ≥ 0 forces ε = 0) and dominate the old ones, so
      t(p,q)² ≤ (A_p B_q C_{σ(p,q)})²   ⇒   T² ≤ (Σ_p (A_p + ε) Σ_q (B_q + ε)(C_{σ(p,q)} + ε))²
  with no condition on zero magnitudes (Fourier modes may vanish). Letting ε → 0 is not available constructively;
  the bound holds for every ε.
    shift_ne, sq_mono, mul_mono, triad_dom_eps
-/
import ns.TriadDom

namespace TriadDomEps
open DetChain NSChain LibRing LibCS LibGron LibOF SignedSum TriadDom

variable {R : Type} (K : CRing R) {I : Type}

theorem shift_ne (P : Pos K) (F : OF K P) (a e : R) (ha : P.nn a) (he : P.nn e) (he0 : e ≠ K.zero) :
    K.add a e ≠ K.zero := by
  intro h
  apply he0
  apply F.antisymm _ he
  rw [show K.neg e = a by
    calc K.neg e = K.add (K.neg e) (K.add a e) := by rw [h]; lib_ring
      _ = a := by lib_ring]
  exact ha

theorem sq_mono (P : Pos K) (x y : R) (hx : P.nn x) (h : le K P x y) : le K P (K.mul x x) (K.mul y y) := by
  unfold le at *
  rw [show sub K (K.mul y y) (K.mul x x) = K.add (K.mul (sub K y x) (sub K y x)) (K.add (K.mul x (sub K y x)) (K.mul x (sub K y x))) by lib_ring]
  exact P.nn_add _ _ (P.nn_sq _) (P.nn_add _ _ (P.nn_mul _ _ hx h) (P.nn_mul _ _ hx h))

theorem mul_mono (P : Pos K) (a b a' b' : R) (ha : P.nn a) (hb' : P.nn b') (h1 : le K P a a') (h2 : le K P b b') :
    le K P (K.mul a b) (K.mul a' b') := by
  unfold le at *
  rw [show sub K (K.mul a' b') (K.mul a b) = K.add (K.mul (sub K a' a) b') (K.mul a (sub K b' b)) by lib_ring]
  exact P.nn_add _ _ (P.nn_mul _ _ h1 hb') (P.nn_mul _ _ ha h2)

theorem le_shift (P : Pos K) (a e : R) (he : P.nn e) : le K P a (K.add a e) := by
  unfold le; rw [show sub K (K.add a e) a = e by lib_ring]; exact he

/-- **Domination for every ε-shift, no non-vanishing hypotheses.** -/
theorem triad_dom_eps (P : Pos K) (F : OF K P) (A B C : I → R) (σf : I → I → I) (t : I → I → R) (l : List I) (T e : R)
    (hT : T = lsum K (fun p => lsum K (t p) l) l)
    (hA : ∀ p, P.nn (A p)) (hB : ∀ q, P.nn (B q)) (hC : ∀ r, P.nn (C r)) (he : P.nn e) (he0 : e ≠ K.zero)
    (ht : ∀ p q, le K P (K.mul (t p q) (t p q))
      (K.mul (K.mul (A p) (K.mul (B q) (C (σf p q)))) (K.mul (A p) (K.mul (B q) (C (σf p q)))))) :
    le K P (K.mul T T)
      (K.mul (lsum K (fun p => K.mul (K.add (A p) e) (lsum K (fun q => K.mul (K.add (B q) e) (K.add (C (σf p q)) e)) l)) l)
             (lsum K (fun p => K.mul (K.add (A p) e) (lsum K (fun q => K.mul (K.add (B q) e) (K.add (C (σf p q)) e)) l)) l)) := by
  apply triad_dom K P F (fun p => K.add (A p) e) (fun q => K.add (B q) e) (fun r => K.add (C r) e) σf t l T hT
    (fun p => P.nn_add _ _ (hA p) he) (fun q => P.nn_add _ _ (hB q) he) (fun r => P.nn_add _ _ (hC r) he)
    (fun p => shift_ne K P F _ _ (hA p) he he0)
    (fun p q => prod_ne K P F _ _ (shift_ne K P F _ _ (hB q) he he0) (shift_ne K P F _ _ (hC _) he he0))
  intro p q
  apply le_trans K P (ht p q)
  apply sq_mono K P _ _ (P.nn_mul _ _ (hA p) (P.nn_mul _ _ (hB q) (hC _)))
  apply mul_mono K P _ _ _ _ (hA p) (P.nn_mul _ _ (P.nn_add _ _ (hB q) he) (P.nn_add _ _ (hC _) he)) (le_shift K P _ _ he)
  exact mul_mono K P _ _ _ _ (hB q) (P.nn_add _ _ (hC _) he) (le_shift K P _ _ he) (le_shift K P _ _ he)

end TriadDomEps

#print axioms TriadDomEps.shift_ne
#print axioms TriadDomEps.sq_mono
#print axioms TriadDomEps.mul_mono
#print axioms TriadDomEps.triad_dom_eps

ns/TriadH2.lean

lean_all/ns/TriadH2.lean

/-
  ns/TriadH2.lean — T37 cycle 111: every triad sum of a truncation bounded by H² × L² × L², constant 52.

  Lean 4 core plus ns/DiscreteYoung (young), ns/Sobolev (sobolev), ns/TriadSum, ns/Riccati.
  With a_p = x_p y_p ≥ 0 (x_p² ≤ ω_p = |k_p|^{-4}, y_p² ≤ β_p = |k_p|⁴|û_p|²), Σω ≤ W (= 52 by shell_weight):
      2 (Σ_p a_p Σ_q b_q c(σ_p q))² ≤ 2 · W · (Σβ) · (Σb²) · C
  i.e. |Σ_{p+q+r=0} |û_p| b_q c_r|² ≤ 52 ‖Δu‖² ‖b‖² ‖c‖² for every truncation.
-/
import ns.DiscreteYoung
import ns.Sobolev

namespace TriadH2
open DetChain NSChain LibRing SumCS TriadSum

variable {R : Type} (K : CRing R) {I : Type}

theorem triad_h2 (P : Cone K) (x y ω β b c : I → R) (σ : I → I → I) (l : List I) (W C : R)
    (hxy : ∀ p, P.nn (K.mul (x p) (y p)))
    (hx : ∀ i, Le K P (sq K (x i)) (ω i)) (hy : ∀ i, Le K P (sq K (y i)) (β i)) (hβ : ∀ i, P.nn (β i))
    (hW : Le K P (lsum K ω l) W) (hC : ∀ p, Le K P (lsum K (fun q => sq K (c (σ p q))) l) C) (hCn : P.nn C) :
    Le K P (K.add (sq K (lsum K (fun p => K.mul (K.mul (x p) (y p)) (lsum K (fun q => K.mul (b q) (c (σ p q))) l)) l))
                  (sq K (lsum K (fun p => K.mul (K.mul (x p) (y p)) (lsum K (fun q => K.mul (b q) (c (σ p q))) l)) l)))
      (K.add (K.mul (K.mul W (lsum K β l)) (K.mul (lsum K (fun q => sq K (b q)) l) C))
             (K.mul (K.mul W (lsum K β l)) (K.mul (lsum K (fun q => sq K (b q)) l) C))) := by
  have y1 := DiscreteYoung.young K P (fun p => K.mul (x p) (y p)) b c σ l C hxy hC
  have s := Sobolev.sobolev K P x y ω β l W hx hy hβ hW
  have bn : P.nn (K.mul (lsum K (fun q => sq K (b q)) l) C) :=
    P.nn_mul _ _ (lsum_nn K P _ (fun q => P.nn_sq _) l) hCn
  have m := Riccati.le_mul_left K P _ _ _ bn s
  rw [K.mul_comm (K.mul (lsum K (fun q => sq K (b q)) l) C),
    K.mul_comm (K.mul (lsum K (fun q => sq K (b q)) l) C)] at m
  exact le_trans' K P _ _ _ y1 (Riccati.le_add K P _ _ _ _ m m)

end TriadH2

#print axioms TriadH2.triad_h2

ns/TriadLinf.lean

lean_all/ns/TriadLinf.lean

/-
  ns/TriadLinf.lean — T37 cycle 203: the stretching bound in L^∞ × H¹ × H² form for any truncation.

  Lean 4 core plus ns/TriadSum (lsum_le, lsum_nn), ns/SumCS (cs_sum, sq), NSChain (lsum, lsum_mul_left), lib/RingTac.
    wdev_eq      Σ_j a_j (S_j − y)² = Q − 2yM + y²A        (A = Σa, M = ΣaS, Q = ΣaS²)
    wcs          a ≥ 0 ⇒ (Σ a S)² ≤ (Σ a)(Σ a S²)          (weighted Cauchy–Schwarz without square roots)
    triad_linf   a ≥ 0, (Σ_q b_q c_{σ(p,q)})² ≤ B for each p ⇒ (Σ_p a_p Σ_q b_q c_{σ(p,q)})² ≤ (Σ a)² B
    stretch      with cs_sum for the inner sum and Σ_q c_{σ(p,q)}² ≤ C:
                 (Σ_p a_p Σ_q b_q c_{σ(p,q)})² ≤ (Σ a)² (Σ b²) C
  With a_p = |û_p| (Σa = Wiener norm ≥ ‖u‖_∞), b_q = |k_q||û_q| (Σb² = Z), c_r = |k_r|²|û_r| (C = P, σ(p,·)
  injective): |stretching| ≤ X·√Z·√P — the bound c X r s of YoungStep / LocalCont with c = 1 (times the vector
  factors of TriadBound), for every truncation, no |l| factor.
-/
import ns.TriadSum

namespace TriadLinf
open DetChain NSChain LibRing SumCS TriadSum

variable {R : Type} (K : CRing R) {I : Type}

theorem wdev_eq (a S : I → R) (y : R) : ∀ l : List I,
    lsum K (fun j => K.mul (a j) (sq K (sub K (S j) y))) l
      = K.add (sub K (lsum K (fun j => K.mul (a j) (sq K (S j))) l)
          (K.mul (K.add y y) (lsum K (fun j => K.mul (a j) (S j)) l)))
          (K.mul (K.mul y y) (lsum K a l))
  | [] => by show K.zero = K.add (sub K K.zero (K.mul (K.add y y) K.zero)) (K.mul (K.mul y y) K.zero); lib_ring
  | j :: l => by
    show K.add (K.mul (a j) (sq K (sub K (S j) y))) (lsum K (fun j => K.mul (a j) (sq K (sub K (S j) y))) l)
      = K.add (sub K (K.add (K.mul (a j) (sq K (S j))) (lsum K (fun j => K.mul (a j) (sq K (S j))) l))
          (K.mul (K.add y y) (K.add (K.mul (a j) (S j)) (lsum K (fun j => K.mul (a j) (S j)) l))))
          (K.mul (K.mul y y) (K.add (a j) (lsum K a l)))
    rw [wdev_eq a S y l]; unfold sq; lib_ring

/-- **Weighted Cauchy–Schwarz.** -/
theorem wcs (P : Cone K) (a S : I → R) (ha : ∀ i, P.nn (a i)) : ∀ l : List I,
    Le K P (sq K (lsum K (fun i => K.mul (a i) (S i)) l))
      (K.mul (lsum K a l) (lsum K (fun i => K.mul (a i) (sq K (S i))) l))
  | [] => ⟨K.zero, P.nn_zero, by show K.add (sq K K.zero) K.zero = K.mul K.zero K.zero; unfold sq; lib_ring⟩
  | j :: l => by
    cases wcs P a S ha l with
    | intro d hd =>
      -- new gap = old gap + a_j · Σ_i a_i (S_i − S_j)²
      have dev := wdev_eq K a S (S j) l
      refine ⟨K.add d (K.mul (a j) (lsum K (fun i => K.mul (a i) (sq K (sub K (S i) (S j)))) l)),
        P.nn_add _ _ hd.1 (P.nn_mul _ _ (ha j) (lsum_nn K P _ (fun i => P.nn_mul _ _ (ha i) (P.nn_sq _)) l)), ?_⟩
      rw [dev]
      show K.add (sq K (K.add (K.mul (a j) (S j)) (lsum K (fun i => K.mul (a i) (S i)) l))) _
        = K.mul (K.add (a j) (lsum K a l)) (K.add (K.mul (a j) (sq K (S j))) (lsum K (fun i => K.mul (a i) (sq K (S i))) l))
      have ed : d = sub K (K.mul (lsum K a l) (lsum K (fun i => K.mul (a i) (sq K (S i))) l))
          (sq K (lsum K (fun i => K.mul (a i) (S i)) l)) := by
        rw [← hd.2]; lib_ring
      rw [ed]
      unfold sq; lib_ring

/-- **Triad sum with an L^∞ factor.** -/
theorem triad_linf (P : Cone K) (a S : I → R) (B : R) (l : List I) (ha : ∀ i, P.nn (a i))
    (hS : ∀ i, Le K P (sq K (S i)) B) :
    Le K P (sq K (lsum K (fun i => K.mul (a i) (S i)) l)) (K.mul (sq K (lsum K a l)) B) := by
  have w := wcs K P a S ha l
  have s := lsum_le K P (fun i => K.mul (a i) (sq K (S i))) (fun i => K.mul B (a i))
    (fun i => by
      have m := Riccati.le_mul_left K P (a i) _ _ (ha i) (hS i)
      rw [show K.mul (a i) B = K.mul B (a i) by lib_ring] at m
      exact m) l
  rw [lsum_mul_left K B a l] at s
  have m := Riccati.le_mul_left K P (lsum K a l) _ _ (lsum_nn K P a ha l) s
  refine le_trans' K P _ _ _ w (le_trans' K P _ _ _ m ⟨K.zero, P.nn_zero, ?_⟩)
  unfold sq; lib_ring

/-- **The stretching bound.** -/
theorem stretch (P : Cone K) (a b c : I → R) (σ : I → I → I) (C : R) (l : List I) (ha : ∀ i, P.nn (a i))
    (hC : ∀ p, Le K P (lsum K (fun q => sq K (c (σ p q))) l) C) :
    Le K P (sq K (lsum K (fun p => K.mul (a p) (lsum K (fun q => K.mul (b q) (c (σ p q))) l)) l))
      (K.mul (sq K (lsum K a l)) (K.mul (lsum K (fun q => sq K (b q)) l) C)) := by
  apply triad_linf K P a _ _ l ha
  intro p
  exact le_trans' K P _ _ _ (cs_sum K P b (fun q => c (σ p q)) l)
    (Riccati.le_mul_left K P _ _ _ (lsum_nn K P _ (fun q => P.nn_sq (b q)) l) (hC p))

end TriadLinf

#print axioms TriadLinf.wdev_eq
#print axioms TriadLinf.wcs
#print axioms TriadLinf.triad_linf
#print axioms TriadLinf.stretch

ns/TriadSparse.lean

lean_all/ns/TriadSparse.lean

/-
  ns/TriadSparse.lean — T37 cycle 961 (goal G624c, last piece): triad sparsity of Fourier modes, the hypotheses
  spA, spB of ForceSob. For modes k : I → ℤ³ that are pairwise distinct on the list l and the triad indicator
  δ(p, q, r) = [k_p + k_q + k_r = 0] ∈ {0, 1}:
    spA   Σ_{q∈l} δ(p, q, r) ≤ 1     (for fixed p, r),
    spB   Σ_{r∈l} δ(p, q, r) ≤ 1     (for fixed p, q),
  over any commutative ring with a nonnegative cone. Generic form at_most_one: an indicator whose true set has a
  single k-value sums to at most 1 over a list with distinct k-values. The cancellation k_p + x + k_r = 0 =
  k_p + y + k_r ⇒ x = y is checked componentwise by lib_int (core Int.add_comm, add_left_cancel use propext).

  Lean 4 core plus ns/ForceBound (lsum_le), ns/RatModel (nn_one'), lib/IntRing (lib_int).
-/
import ns.ForceBound
import ns.RatModel
import lib.IntRing

namespace TriadSparse
open DetChain NSChain LibRing DyadicLarge

abbrev Mode := Int × Int × Int

def tadd (a b : Mode) : Mode := (a.1 + b.1, a.2.1 + b.2.1, a.2.2 + b.2.2)

def zero3 : Mode := (0, 0, 0)

theorem cancel1 (a c x y : Int) (hx : a + x + c = 0) (hy : a + y + c = 0) : x = y := by
  have ex : x = (a + x + c) + (-a) + (-c) := by lib_int
  have ey : y = (a + y + c) + (-a) + (-c) := by lib_int
  rw [ex, ey, hx, hy]

theorem triad_uniq (a c x y : Mode) (hx : tadd (tadd a x) c = zero3) (hy : tadd (tadd a y) c = zero3) : x = y := by
  have h1x : a.1 + x.1 + c.1 = 0 := congrArg Prod.fst hx
  have h1y : a.1 + y.1 + c.1 = 0 := congrArg Prod.fst hy
  have h2x : a.2.1 + x.2.1 + c.2.1 = 0 := congrArg (fun t => t.2.1) hx
  have h2y : a.2.1 + y.2.1 + c.2.1 = 0 := congrArg (fun t => t.2.1) hy
  have h3x : a.2.2 + x.2.2 + c.2.2 = 0 := congrArg (fun t => t.2.2) hx
  have h3y : a.2.2 + y.2.2 + c.2.2 = 0 := congrArg (fun t => t.2.2) hy
  have e1 := cancel1 _ _ _ _ h1x h1y
  have e2 := cancel1 _ _ _ _ h2x h2y
  have e3 := cancel1 _ _ _ _ h3x h3y
  cases x with
  | mk x1 x23 => cases x23 with
    | mk x2 x3 => cases y with
      | mk y1 y23 => cases y23 with
        | mk y2 y3 =>
          have e1' : x1 = y1 := e1
          have e2' : x2 = y2 := e2
          have e3' : x3 = y3 := e3
          rw [e1', e2', e3']

section Gen
variable {R : Type} (K : CRing R) (P : Cone K) {I : Type}

def ind (b : Bool) : R := cond b K.one K.zero

theorem sum_zero (t : I → Bool) : ∀ (l : List I), (∀ q, q ∈ l → t q = false) → lsum K (fun q => ind K (t q)) l = K.zero
  | [], _ => rfl
  | x :: l, h => by
    show K.add (cond (t x) K.one K.zero) (lsum K (fun q => ind K (t q)) l) = K.zero
    rw [h x (List.Mem.head _), sum_zero t l (fun q hq => h q (List.Mem.tail _ hq))]
    show K.add K.zero K.zero = K.zero; lib_ring

/-- an indicator whose true set has a single k-value sums to at most 1 over distinct k-values -/
theorem at_most_one {β : Type} (k : I → β) (t : I → Bool) (uniq : ∀ q q', t q = true → t q' = true → k q = k q') :
    ∀ (l : List I), List.Pairwise (fun a b => k a ≠ k b) l → Le K P (lsum K (fun q => ind K (t q)) l) K.one
  | [], _ => ⟨K.one, RatModel.nn_one' K P, by show K.add K.zero K.one = K.one; lib_ring⟩
  | x :: l, hp => by
    cases hp with
    | cons hx hl =>
      show Le K P (K.add (cond (t x) K.one K.zero) (lsum K (fun q => ind K (t q)) l)) K.one
      cases htx : t x with
      | true =>
        have hz : lsum K (fun q => ind K (t q)) l = K.zero := sum_zero K t l (fun q hq => by
          cases htq : t q with
          | false => rfl
          | true => exact absurd (uniq x q htx htq) (hx q hq))
        rw [hz]
        exact ⟨K.zero, P.nn_zero, by show K.add (K.add K.one K.zero) K.zero = K.one; lib_ring⟩
      | false =>
        have ih := at_most_one k t uniq l hl
        cases ih with
        | intro d hd => exact ⟨d, hd.1, by rw [← hd.2]; show K.add (K.add K.zero _) d = _; lib_ring⟩

/-- the triad indicator [k_p + k_q + k_r = 0] -/
def delta [DecidableEq Mode] (k : I → Mode) (p q r : I) : R :=
  ind K (decide (tadd (tadd (k p) (k q)) (k r) = zero3))

theorem spA (k : I → Mode) (l : List I) (hd : List.Pairwise (fun a b => k a ≠ k b) l) (p r : I) :
    Le K P (lsum K (fun q => delta K k p q r) l) K.one :=
  at_most_one K P k (fun q => decide (tadd (tadd (k p) (k q)) (k r) = zero3))
    (fun _ _ hq hq' => triad_uniq _ _ _ _ (of_decide_eq_true hq) (of_decide_eq_true hq')) l hd

theorem spB (k : I → Mode) (l : List I) (hd : List.Pairwise (fun a b => k a ≠ k b) l) (p q : I) :
    Le K P (lsum K (fun r => delta K k p q r) l) K.one := by
  refine at_most_one K P k (fun r => decide (tadd (tadd (k p) (k q)) (k r) = zero3)) ?_ l hd
  intro r r' hr hr'
  have h1 := of_decide_eq_true hr
  have h2 := of_decide_eq_true hr'
  -- k_p + k_q + x = 0 = k_p + k_q + y, i.e. (k_p + k_q) + x + 0 = 0
  have z : ∀ a x : Mode, tadd a x = zero3 → tadd (tadd a x) zero3 = zero3 := fun a x h => by
    rw [h]; rfl
  exact triad_uniq (tadd (k p) (k q)) zero3 (k r) (k r') (z _ _ h1) (z _ _ h2)

end Gen

end TriadSparse

#print axioms TriadSparse.triad_uniq
#print axioms TriadSparse.at_most_one
#print axioms TriadSparse.spA
#print axioms TriadSparse.spB

ns/TriadSum.lean

lean_all/ns/TriadSum.lean

/-
  ns/TriadSum.lean — T37 cycle 56: the whole 3D transfer of a truncation, bounded with an explicit
  truncation-dependent constant.

  Lean 4 core plus ns/TriadBound, ns/SumCS, ns/Riccati (le_add). For triads t ∈ l with terms
  T_t = (u_t·k_t)(v_t·w_t):
      (Σ_t T_t)² ≤ |l| · Σ_t |u_t|²|k_t|²|v_t|²|w_t|²
  (|l| = number of triads, as Σ 1). The factor |l| grows with the truncation: this is the exact place
  where a uniform bound (R1) must improve on Cauchy–Schwarz (by the dyadic/Littlewood–Paley counting
  of interacting triads).
    lsum_le      termwise ≤ ⇒ sums ≤
    transfer_sq  the bound above
-/
import ns.TriadBound
import ns.SumCS
import ns.Riccati

namespace TriadSum
open DetChain NSChain CFKernel TriadBound SumCS

variable {R : Type} (K : CRing R) {I : Type}

theorem lsum_le (P : Cone K) (f g : I → R) (h : ∀ i, Le K P (f i) (g i)) :
    ∀ l : List I, Le K P (lsum K f l) (lsum K g l)
  | [] => le_refl' K P _
  | i :: l => Riccati.le_add K P _ _ _ _ (h i) (lsum_le P f g h l)

theorem lsum_nn (P : Cone K) (f : I → R) (h : ∀ i, P.nn (f i)) : ∀ l : List I, P.nn (lsum K f l)
  | [] => P.nn_zero
  | i :: l => P.nn_add _ _ (h i) (lsum_nn P f h l)

theorem one_nn (P : Cone K) : P.nn K.one := by
  have e := P.nn_sq K.one
  rw [K.one_mul] at e
  exact e

/-- **Transfer bound**: (Σ T)² ≤ |l| Σ |u|²|k|²|v|²|w|². -/
theorem transfer_sq (P : Cone K) (u k v w : I → V R) (l : List I) :
    Le K P (sq K (lsum K (fun t => K.mul (dot K (u t) (k t)) (dot K (v t) (w t))) l))
      (K.mul (lsum K (fun t => K.mul (K.mul (dot K (u t) (u t)) (dot K (k t) (k t)))
          (K.mul (dot K (v t) (v t)) (dot K (w t) (w t)))) l) (lsum K (fun _ => K.one) l)) := by
  have one1 : ∀ t : I, sq K ((fun _ : I => K.one) t) = K.one := fun _ => K.one_mul _
  have e1 : lsum K (fun t => K.mul (dot K (u t) (k t)) (dot K (v t) (w t))) l
      = lsum K (fun t => K.mul (K.mul (dot K (u t) (k t)) (dot K (v t) (w t))) ((fun _ : I => K.one) t)) l :=
    lsum_ext K _ _ (fun t => by
      show _ = K.mul _ K.one
      rw [K.mul_comm (K.mul _ _) K.one, K.one_mul]) l
  have cs := cs_sum K P (fun t => K.mul (dot K (u t) (k t)) (dot K (v t) (w t))) (fun _ => K.one) l
  rw [← e1, lsum_ext K _ _ one1 l] at cs
  refine le_trans' K P _ _ _ cs ?_
  rw [K.mul_comm _ (lsum K (fun _ => K.one) l), K.mul_comm _ (lsum K (fun _ => K.one) l)]
  apply Riccati.le_mul_left K P _ _ _ (lsum_nn K P _ (fun _ => one_nn K P) l)
  apply lsum_le K P
  intro t
  have b := triad_sq_bound K P (u t) (k t) (v t) (w t)
  have e : sq K (K.mul (dot K (u t) (k t)) (dot K (v t) (w t)))
      = K.mul (K.mul (dot K (u t) (k t)) (dot K (u t) (k t))) (K.mul (dot K (v t) (w t)) (dot K (v t) (w t))) := by
    unfold SumCS.sq
    generalize dot K (u t) (k t) = A
    generalize dot K (v t) (w t) = B
    rw [K.mul_assoc, ← K.mul_assoc B A B, K.mul_comm B A, K.mul_assoc A B B, ← K.mul_assoc]
  rw [e]; exact b

end TriadSum

#print axioms TriadSum.lsum_le
#print axioms TriadSum.lsum_nn
#print axioms TriadSum.one_nn
#print axioms TriadSum.transfer_sq

ns/TruncBound.lean

lean_all/ns/TruncBound.lean

/-
  ns/TruncBound.lean — T37 cycle 1041 (goal G683, piece b): the energy of the truncation error, uniformly in both
  truncations, over an ordered field. Sub-truncation N = {|kz|_∞ ≤ R} of a mode list l (lowB R), a_N, a_H the parts:
    err_bound   Σ_r |N_l(a,a)_r − N_{l∩N}(a,a)_r|² ≤ 2·TS(a_H, a) + 2·ι²A(a_N)·135·Z(a_H)
  (TS = the two-scale bound of ForceTwoScale with free internal cutoffs R', t₀), and the high-mode estimates
    high_Z     (R+1)²·Z(a_H) ≤ A(a_H),      high_A  A(a_H) ≤ A(a),      low_A  A(a_N) ≤ A(a).
  So the error is ≲ ι²A(a)Z(a)·(C₂ + C₁/(R+1)²): small for large R once C₂ is made small by R', t₀.

  Lean 4 core plus ns/TruncError, ns/EnstLocal (sq_mono), ns/ForceTwoScale, ns/WeightSplit (lowB), ns/NormCompare.
-/
import ns.TruncError
import ns.EnstLocal

namespace TruncBound
open DetChain NSChain DyadicSmallGlobal LibRing DyadicLarge FourierGalerkin GalerkinForce ForceBound ForceSob
  ModeCount CubeCount ForceUniform NormCompare EnstGalerkin ForceTwoScale ForceBilin EnstDiff ForceDiff TruncError
  WeightSplit LatticeSum

section Gen
variable {R : Type} (K : CRing R) (O : OrdR K) {I : Type}
variable (inv : R → R) (mul_inv : ∀ y, y ≠ K.zero → K.mul y (inv y) = K.one) (h10 : K.one ≠ K.zero)

theorem part_le (kz : I → Mode) (a : I → V3 R) (p : I) (sel : Bool) :
    Le K O.toCone (K.mul (wt K kz p) (dot3 K (cond sel (a p) (z3 K)) (cond sel (a p) (z3 K))))
      (K.mul (wt K kz p) (dot3 K (a p) (a p))) := by
  cases sel with
  | true => exact le_refl' K O.toCone _
  | false =>
    refine ⟨K.mul (wt K kz p) (dot3 K (a p) (a p)), O.nn_mul _ _ (dot3_nn K O.toCone _) (dot3_nn K O.toCone _), ?_⟩
    show K.add (K.mul (wt K kz p) (K.add (K.add (K.mul K.zero K.zero) (K.mul K.zero K.zero)) (K.mul K.zero K.zero))) _ = _
    lib_ring

theorem high_Z (l : List I) (kz : I → Mode) (Rc : Nat) (a : I → V3 R) :
    Le K O.toCone (K.mul (K.mul (castN K (Rc + 1)) (castN K (Rc + 1)))
        (lsum K (fun p => K.mul (wt K kz p) (dot3 K (hiF K (lowB Rc kz) a p) (hiF K (lowB Rc kz) a p))) l))
      (lsum K (fun p => K.mul (K.mul (wt K kz p) (wt K kz p)) (dot3 K (hiF K (lowB Rc kz) a p) (hiF K (lowB Rc kz) a p))) l) := by
  rw [← lsum_mul_left]
  refine lsum_le K O.toCone _ _ (fun p => ?_) l
  show Le K O.toCone (K.mul _ (K.mul (wt K kz p) (dot3 K (cond (lowB Rc kz p) (z3 K) (a p)) (cond (lowB Rc kz p) (z3 K) (a p)))))
    (K.mul (K.mul (wt K kz p) (wt K kz p)) (dot3 K (cond (lowB Rc kz p) (z3 K) (a p)) (cond (lowB Rc kz p) (z3 K) (a p))))
  cases hb : lowB Rc kz p with
  | true =>
    refine ⟨K.zero, O.nn_zero, ?_⟩
    show K.add (K.mul _ (K.mul (wt K kz p) (K.add (K.add (K.mul K.zero K.zero) (K.mul K.zero K.zero)) (K.mul K.zero K.zero)))) K.zero
      = K.mul (K.mul (wt K kz p) (wt K kz p)) (K.add (K.add (K.mul K.zero K.zero) (K.mul K.zero K.zero)) (K.mul K.zero K.zero))
    lib_ring
  | false =>
    -- R + 1 ≤ nrm, so (R+1)² ≤ nrm² ≤ w
    have hge : Rc + 1 ≤ nrm (kz p) := by
      cases Nat.lt_or_ge Rc (nrm (kz p)) with
      | inl lt => exact lt
      | inr ge => exact absurd (Nat.ble_eq_true_of_le ge) (by
          show ¬ Nat.ble (nrm (kz p)) Rc = true; rw [show Nat.ble (nrm (kz p)) Rc = lowB Rc kz p from rfl, hb]; exact Bool.noConfusion)
    have m := RatModel.cast_mono K O.toCone _ _ hge
    have sq := EnstLocal.sq_mono K O _ _ (RatModel.nn_cast K O.toCone _) (RatModel.nn_cast K O.toCone _) m
    have w1 := le_trans' K O.toCone _ _ _ sq (nrm_le_dot K O.toCone (kz p))
    have m2 := ForceSob.le_mul_right' K O.toCone w1 (O.nn_mul _ _ (dot3_nn K O.toCone (kv K kz p)) (dot3_nn K O.toCone (a p)))
    have e2 : K.mul (dot3 K (kvec K (kz p)) (kvec K (kz p))) (K.mul (dot3 K (kv K kz p) (kv K kz p)) (dot3 K (a p) (a p)))
        = K.mul (K.mul (wt K kz p) (wt K kz p)) (dot3 K (a p) (a p)) := by
      show K.mul (wt K kz p) (K.mul (wt K kz p) (dot3 K (a p) (a p))) = _; lib_ring
    rw [e2] at m2
    exact m2

theorem part_leW (W : R) (hW : O.nn W) (a : I → V3 R) (p : I) (sel : Bool) :
    Le K O.toCone (K.mul W (dot3 K (cond sel (a p) (z3 K)) (cond sel (a p) (z3 K)))) (K.mul W (dot3 K (a p) (a p))) := by
  cases sel with
  | true => exact le_refl' K O.toCone _
  | false =>
    refine ⟨K.mul W (dot3 K (a p) (a p)), O.nn_mul _ _ hW (dot3_nn K O.toCone _), ?_⟩
    show K.add (K.mul W (K.add (K.add (K.mul K.zero K.zero) (K.mul K.zero K.zero)) (K.mul K.zero K.zero))) _ = _
    lib_ring

theorem partH_leW (W : R) (hW : O.nn W) (a : I → V3 R) (p : I) (sel : Bool) :
    Le K O.toCone (K.mul W (dot3 K (cond sel (z3 K) (a p)) (cond sel (z3 K) (a p)))) (K.mul W (dot3 K (a p) (a p))) := by
  cases sel with
  | false => exact le_refl' K O.toCone _
  | true =>
    refine ⟨K.mul W (dot3 K (a p) (a p)), O.nn_mul _ _ hW (dot3_nn K O.toCone _), ?_⟩
    show K.add (K.mul W (K.add (K.add (K.mul K.zero K.zero) (K.mul K.zero K.zero)) (K.mul K.zero K.zero))) _ = _
    lib_ring

include mul_inv h10 in
theorem err_bound (l : List I) (kz : I → Mode) (hd : List.Pairwise (fun a b => kz a ≠ kz b) l)
    (h1 : ∀ p, 1 ≤ nrm (kz p)) (M : Nat) (hM : ∀ p, p ∈ l → nrm (kz p) ≤ M + 1) (Rc R' t0 : Nat) (hRM : R' ≤ M)
    (ι : R) (a : I → V3 R) :
    Le K O.toCone
      (lsum K (fun r => dot3 K (sub3 K (Nl K l (TriadSparse.delta K kz) ι (kv K kz) a a r)
          (Nl K (l.filter (lowB Rc kz)) (TriadSparse.delta K kz) ι (kv K kz) a a r))
        (sub3 K (Nl K l (TriadSparse.delta K kz) ι (kv K kz) a a r)
          (Nl K (l.filter (lowB Rc kz)) (TriadSparse.delta K kz) ι (kv K kz) a a r))) l)
      (K.add
        (K.mul (K.add K.one K.one) (K.add
          (K.mul (K.add K.one K.one) (K.mul (K.mul ι ι) (K.mul (lsum K (fun p => K.mul (wt K kz p) (dot3 K (hiF K (lowB Rc kz) a p) (hiF K (lowB Rc kz) a p))) l)
            (K.mul (K.add (K.mul (castN K (cube R')) K.one) (K.mul (castN K 135) (iv K inv (R' + 1))))
              (lsum K (fun q => K.mul (wt K kz q) (dot3 K (a q) (a q))) l)))))
          (K.mul (K.add K.one K.one) (K.mul (K.mul ι ι) (K.mul (lsum K (fun p => K.mul (K.mul (wt K kz p) (wt K kz p)) (dot3 K (hiF K (lowB Rc kz) a p) (hiF K (lowB Rc kz) a p))) l)
            (K.mul (K.add (K.mul (castN K (cube R')) (K.mul (inv (castN K (t0 + 1))) (inv (castN K (t0 + 1)))))
                (K.mul (castN K 135) (iv K inv (R' + 1))))
              (lsum K (fun q => K.mul (wt K kz q) (dot3 K (a q) (a q))) l)))))))
        (K.mul (K.add K.one K.one) (K.mul (K.mul ι ι) (K.mul (lsum K (fun p => K.mul (K.mul (wt K kz p) (wt K kz p)) (dot3 K (a p) (a p))) l)
          (K.mul (castN K 135) (lsum K (fun q => K.mul (wt K kz q) (dot3 K (hiF K (lowB Rc kz) a q) (hiF K (lowB Rc kz) a q))) l)))))) := by
  rw [lsum_ext K _ _ (fun r => by rw [trunc_err K l (lowB Rc kz) _ ι _ a r]) l]
  have s1 := lsum_le K O.toCone _ _ (fun r => sq2 K O
    (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (hiF K (lowB Rc kz) a) a r)
    (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (restr K (lowB Rc kz) a) (hiF K (lowB Rc kz) a) r)) l
  rw [lsum_add, lsum_mul_left, lsum_mul_left] at s1
  refine le_trans' K O.toCone _ _ _ s1 (le_add' K O.toCone _ _ _ _ ?_ ?_)
  · exact le_mul_nn K O.toCone _ _ _ (O.nn_add _ _ (RatModel.nn_one' K O.toCone) (RatModel.nn_one' K O.toCone))
      (two_scale K O inv mul_inv h10 l kz hd h1 M hM R' t0 hRM ι (hiF K (lowB Rc kz) a) a)
  · have fu := force_uniform K O inv mul_inv h10 l kz hd h1 M hM ι (kv K kz) (restr K (lowB Rc kz) a) (hiF K (lowB Rc kz) a)
    have hH := lsum_le K O.toCone _ _ (fun p => le_trans' K O.toCone _ _ _
      (le_mul_right' K O.toCone (nrm4_le K O.toCone (kz p)) (dot3_nn K O.toCone (restr K (lowB Rc kz) a p)))
      (part_leW K O _ (O.nn_sq _) a p (lowB Rc kz p))) l
    have nZ : O.nn (lsum K (fun q => K.mul (wt K kz q) (dot3 K (hiF K (lowB Rc kz) a q) (hiF K (lowB Rc kz) a q))) l) :=
      lsum_nn K O.toCone _ (fun q => O.nn_mul _ _ (dot3_nn K O.toCone _) (dot3_nn K O.toCone _)) l
    have m1 := le_mul_right' K O.toCone hH (O.nn_mul _ _ (RatModel.nn_cast K O.toCone 135) nZ)
    have m2 := le_mul_nn K O.toCone (K.mul ι ι) _ _ (O.nn_sq ι) m1
    exact le_mul_nn K O.toCone _ _ _ (O.nn_add _ _ (RatModel.nn_one' K O.toCone) (RatModel.nn_one' K O.toCone))
      (le_trans' K O.toCone _ _ _ fu m2)

end Gen

end TruncBound

#print axioms TruncBound.part_le
#print axioms TruncBound.high_Z
#print axioms TruncBound.part_leW
#print axioms TruncBound.partH_leW
#print axioms TruncBound.err_bound

ns/TruncCauchy.lean

lean_all/ns/TruncCauchy.lean

/-
  ns/TruncCauchy.lean — T37 cycle 1047 (goal G684, piece b2): comparison of two Galerkin truncations. A solves the
  implicit-viscosity scheme on the mode list l (force N_l), B on the sub-truncation l_N = l.filter (|k|_∞ ≤ R).
  With d = A|_N − B on l_N, the enstrophy step of d has the forcing (N_{l_N}(a,a) − N_{l_N}(B,B)) + E,
  E = N_l(A,A) − N_{l_N}(A,A), so (ForceDiff, TruncBound, GronwallSource), given the coefficient bounds α_t ≤ α,
  β_t ≤ ν² and the error bound S_t ≤ S,
    trunc_cauchy   ν^T · Q_d(T) ≤ (ν + hα)^T · (Q_d(0) + T·h·S),
  with Q_d = νZ(d) + hν²A(d) on l_N. S is the truncation-error energy, small for large R (TruncBound); Q_d(0) = 0
  when B starts from A|_N. This is the Cauchy estimate in the truncation for the discrete-time Galerkin scheme.

  Lean 4 core plus ns/RestrStep, ns/TruncBound, ns/ForceDiff, ns/GronwallSource.
-/
import ns.RestrStep
import ns.TruncBound
import ns.GronwallSource

namespace TruncCauchy
open DetChain NSChain DyadicSmallGlobal LibRing DyadicLarge FourierGalerkin GalerkinForce ForceBound ForceSob
  ModeCount CubeCount ForceUniform NormCompare EnstStep EnstGlobal EnstGalerkin ForceTwoScale ForceBilin EnstDiff
  ForceDiff TruncError TruncBound RestrStep GronwallSource EnstGronwall WeightSplit LatticeSum

section Gen
variable {R : Type} (K : CRing R) (P : Cone K) {I : Type}

theorem lsum_le_on {f g : I → R} : ∀ (l : List I), (∀ i, i ∈ l → Le K P (f i) (g i)) → Le K P (lsum K f l) (lsum K g l)
  | [], _ => le_refl' K P _
  | i :: l, h => le_add' K P _ _ _ _ (h i (List.Mem.head _)) (lsum_le_on l (fun j hj => h j (List.Mem.tail _ hj)))

/-- enst_step with the step relation required only on the list -/
theorem enst_step_on (l : List I) (h ν : R) (hh : P.nn h) (hν : P.nn ν) (w : I → R) (hw : ∀ r, P.nn (w r))
    (a' a N : I → V3 R) (hs : ∀ r, r ∈ l → Step K h ν (w r) (a' r) (a r) (N r)) :
    Le K P (K.add (K.mul ν (lsum K (fun r => K.mul (w r) (dot3 K (a' r) (a' r))) l))
        (K.mul (K.mul h (K.mul ν ν)) (lsum K (fun r => K.mul (K.mul (w r) (w r)) (dot3 K (a' r) (a' r))) l)))
      (K.add (K.mul ν (lsum K (fun r => K.mul (w r) (dot3 K (a r) (a r))) l))
        (K.mul h (lsum K (fun r => dot3 K (N r) (N r)) l))) := by
  have t := lsum_le_on K P _ (fun r hr => mode_step K P h ν (w r) hh hν (hw r) (a' r) (a r) (N r) (hs r hr))
  rw [lsum_add, lsum_add] at t
  have e1 : lsum K (fun r => K.mul (K.mul ν (w r)) (dot3 K (a' r) (a' r))) l
      = K.mul ν (lsum K (fun r => K.mul (w r) (dot3 K (a' r) (a' r))) l) := by
    rw [← lsum_mul_left]; exact lsum_ext K _ _ (fun r => by lib_ring) l
  have e2 : lsum K (fun r => K.mul (K.mul h (K.mul (K.mul ν ν) (K.mul (w r) (w r)))) (dot3 K (a' r) (a' r))) l
      = K.mul (K.mul h (K.mul ν ν)) (lsum K (fun r => K.mul (K.mul (w r) (w r)) (dot3 K (a' r) (a' r))) l) := by
    rw [← lsum_mul_left]; exact lsum_ext K _ _ (fun r => by lib_ring) l
  have e3 : lsum K (fun r => K.mul (K.mul ν (w r)) (dot3 K (a r) (a r))) l
      = K.mul ν (lsum K (fun r => K.mul (w r) (dot3 K (a r) (a r))) l) := by
    rw [← lsum_mul_left]; exact lsum_ext K _ _ (fun r => by lib_ring) l
  have e4 : lsum K (fun r => K.mul h (dot3 K (N r) (N r))) l = K.mul h (lsum K (fun r => dot3 K (N r) (N r)) l) :=
    lsum_mul_left K h _ l
  rw [e1, e2, e3, e4] at t
  exact t

theorem mem_of_mem_filter (inN : I → Bool) (x : I) : ∀ (l : List I), x ∈ l.filter inN → x ∈ l
  | [], h => nomatch h
  | p :: l, h => by
    have e : List.filter inN (p :: l) = cond (inN p) (p :: List.filter inN l) (List.filter inN l) := by
      show (match inN p with | true => p :: List.filter inN l | false => List.filter inN l) = _
      cases inN p <;> rfl
    rw [e] at h
    cases hp : inN p with
    | true =>
      rw [hp] at h
      cases h with
      | head => exact List.Mem.head _
      | tail _ h' => exact List.Mem.tail _ (mem_of_mem_filter inN x l h')
    | false => rw [hp] at h; exact List.Mem.tail _ (mem_of_mem_filter inN x l h)

theorem pw_filter {β : Type} (kz : I → β) (inN : I → Bool) : ∀ (l : List I),
    List.Pairwise (fun a b => kz a ≠ kz b) l → List.Pairwise (fun a b => kz a ≠ kz b) (l.filter inN)
  | [], _ => List.Pairwise.nil
  | p :: l, hp => by
    cases hp with
    | cons hx hl =>
      have e : List.filter inN (p :: l) = cond (inN p) (p :: List.filter inN l) (List.filter inN l) := by
        show (match inN p with | true => p :: List.filter inN l | false => List.filter inN l) = _
        cases inN p <;> rfl
      rw [e]
      cases inN p with
      | true => exact List.Pairwise.cons (fun y hy => hx y (mem_of_mem_filter inN y l hy)) (pw_filter kz inN l hl)
      | false => exact pw_filter kz inN l hl

/-- the sum over the filtered list of nonnegative terms is at most the sum over the list -/
theorem lsum_filter_le (inN : I → Bool) (f : I → R) (hf : ∀ i, P.nn (f i)) :
    ∀ (l : List I), Le K P (lsum K f (l.filter inN)) (lsum K f l)
  | [] => le_refl' K P _
  | p :: l => by
    have ih := lsum_filter_le inN f hf l
    have e : List.filter inN (p :: l) = cond (inN p) (p :: List.filter inN l) (List.filter inN l) := by
      show (match inN p with | true => p :: List.filter inN l | false => List.filter inN l) = _
      cases inN p <;> rfl
    rw [e]
    cases inN p with
    | true => exact le_add' K P _ _ _ _ (le_refl' K P (f p)) ih
    | false =>
      show Le K P (lsum K f (List.filter inN l)) (K.add (f p) (lsum K f l))
      cases ih with
      | intro d hd => exact ⟨K.add d (f p), P.nn_add _ _ hd.1 (hf p), by rw [← hd.2]; lib_ring⟩

end Gen

section Main
variable {R : Type} (K : CRing R) (O : OrdR K) {I : Type}
variable (inv : R → R) (mul_inv : ∀ y, y ≠ K.zero → K.mul y (inv y) = K.one) (h10 : K.one ≠ K.zero)

def two' : R := K.add K.one K.one
def Zl (kz : I → Mode) (L : List I) (u : I → V3 R) : R := lsum K (fun p => K.mul (wt K kz p) (dot3 K (u p) (u p))) L
def Al (kz : I → Mode) (L : List I) (u : I → V3 R) : R :=
  lsum K (fun p => K.mul (K.mul (wt K kz p) (wt K kz p)) (dot3 K (u p) (u p))) L
def C1' (R' : Nat) : R := K.add (K.mul (castN K (cube R')) K.one) (K.mul (castN K 135) (iv K inv (R' + 1)))
def C2' (R' t0 : Nat) : R := K.add (K.mul (castN K (cube R')) (K.mul (inv (castN K (t0 + 1))) (inv (castN K (t0 + 1)))))
  (K.mul (castN K 135) (iv K inv (R' + 1)))

/-- error energy bound (TruncBound.err_bound right-hand side) -/
def EB (kz : I → Mode) (l : List I) (Rc R' t0 : Nat) (ι : R) (A : I → V3 R) : R :=
  K.add
    (K.mul (two' K) (K.add
      (K.mul (two' K) (K.mul (K.mul ι ι) (K.mul (Zl K kz l (hiF K (lowB Rc kz) A)) (K.mul (C1' K inv R') (Zl K kz l A)))))
      (K.mul (two' K) (K.mul (K.mul ι ι) (K.mul (Al K kz l (hiF K (lowB Rc kz) A)) (K.mul (C2' K inv R' t0) (Zl K kz l A)))))))
    (K.mul (two' K) (K.mul (K.mul ι ι) (K.mul (Al K kz l A) (K.mul (castN K 135) (Zl K kz l (hiF K (lowB Rc kz) A))))))

def alph (kz : I → Mode) (L : List I) (R' : Nat) (ι : R) (a B : I → V3 R) : R :=
  K.add (K.mul (K.mul (two' K) (K.mul (two' K) (two' K))) (K.mul (K.mul ι ι) (K.mul (C1' K inv R') (Zl K kz L a))))
    (K.mul (K.mul (two' K) (two' K)) (K.mul (K.mul ι ι) (K.mul (castN K 135) (Al K kz L B))))
def bet (kz : I → Mode) (L : List I) (R' t0 : Nat) (ι : R) (a : I → V3 R) : R :=
  K.mul (K.mul (two' K) (K.mul (two' K) (two' K))) (K.mul (K.mul ι ι) (K.mul (C2' K inv R' t0) (Zl K kz L a)))

theorem sq2' (G X E : V3 R) (hx : G.x = K.add X.x E.x) (hy : G.y = K.add X.y E.y) (hz : G.z = K.add X.z E.z) :
    Le K O.toCone (dot3 K G G) (K.add (K.mul (two' K) (dot3 K X X)) (K.mul (two' K) (dot3 K E E))) := by
  have e : G = add3 K X E := v3_ext _ _ hx hy hz
  rw [e]; exact sq2 K O X E

set_option maxRecDepth 200000 in
include mul_inv h10 in
theorem trunc_cauchy (l : List I) (kz : I → Mode) (hd : List.Pairwise (fun a b => kz a ≠ kz b) l)
    (h1 : ∀ p, 1 ≤ nrm (kz p)) (M : Nat) (hM : ∀ p, p ∈ l → nrm (kz p) ≤ M + 1) (Rc R' t0 : Nat) (hRM : R' ≤ M)
    (ι h ν α S : R) (hh : O.nn h) (hν : O.nn ν) (hα0 : O.nn α) (hS0 : O.nn S) (A B : Nat → I → V3 R)
    (hA : ∀ t r, Step K h ν (wt K kz r) (A (t + 1) r) (A t r) (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (A t) (A t) r))
    (hB : ∀ t r, r ∈ l.filter (lowB Rc kz) → Step K h ν (wt K kz r) (B (t + 1) r) (B t r)
      (Nl K (l.filter (lowB Rc kz)) (TriadSparse.delta K kz) ι (kv K kz) (B t) (B t) r))
    (hα : ∀ t, Le K O.toCone (alph K inv kz (l.filter (lowB Rc kz)) R' ι (restr K (lowB Rc kz) (A t)) (B t)) α)
    (hβ : ∀ t, Le K O.toCone (bet K inv kz (l.filter (lowB Rc kz)) R' t0 ι (restr K (lowB Rc kz) (A t))) (K.mul ν ν))
    (hS : ∀ t, Le K O.toCone (K.mul (two' K) (EB K inv kz l Rc R' t0 ι (A t))) S) (T : Nat) :
    Le K O.toCone (K.mul (pwr K ν T)
        (Q K ν h (fun t => Zl K kz (l.filter (lowB Rc kz)) (dF K (restr K (lowB Rc kz) (A t)) (B t)))
          (fun t => Al K kz (l.filter (lowB Rc kz)) (dF K (restr K (lowB Rc kz) (A t)) (B t))) T))
      (K.mul (pwr K (K.add ν (K.mul h α)) T)
        (K.add (Q K ν h (fun t => Zl K kz (l.filter (lowB Rc kz)) (dF K (restr K (lowB Rc kz) (A t)) (B t)))
          (fun t => Al K kz (l.filter (lowB Rc kz)) (dF K (restr K (lowB Rc kz) (A t)) (B t))) 0)
          (K.mul (castN K T) (K.mul h S)))) := by
  have hw : ∀ r, O.nn (wt K kz r) := fun r => dot3_nn K O.toCone _
  have hZn : ∀ (L : List I) (u : I → V3 R), O.nn (Zl K kz L u) := fun L u =>
    lsum_nn K O.toCone _ (fun r => O.nn_mul _ _ (hw r) (dot3_nn K O.toCone _)) L
  have hAn : ∀ (L : List I) (u : I → V3 R), O.nn (Al K kz L u) := fun L u =>
    lsum_nn K O.toCone _ (fun r => O.nn_mul _ _ (O.nn_sq _) (dot3_nn K O.toCone _)) L
  have n2 : O.nn (two' K) := O.nn_add _ _ (RatModel.nn_one' K O.toCone) (RatModel.nn_one' K O.toCone)
  have hiv : O.nn (iv K inv (R' + 1)) := iv_nn K O inv mul_inv h10 R'
  have nC1 : O.nn (C1' K inv R') := O.nn_add _ _ (O.nn_mul _ _ (RatModel.nn_cast K O.toCone _) (RatModel.nn_one' K O.toCone))
    (O.nn_mul _ _ (RatModel.nn_cast K O.toCone _) hiv)
  have nC2 : O.nn (C2' K inv R' t0) := O.nn_add _ _ (O.nn_mul _ _ (RatModel.nn_cast K O.toCone _) (O.nn_sq _))
    (O.nn_mul _ _ (RatModel.nn_cast K O.toCone _) hiv)
  have hdN := pw_filter kz (lowB Rc kz) l hd
  have hMN : ∀ p, p ∈ l.filter (lowB Rc kz) → nrm (kz p) ≤ M + 1 := fun p hp => hM p (mem_of_mem_filter (lowB Rc kz) p l hp)
  have hSn : ∀ t, O.nn (K.mul (two' K) (EB K inv kz l Rc R' t0 ι (A t))) := fun t =>
    O.nn_mul _ _ n2 (O.nn_add _ _
      (O.nn_mul _ _ n2 (O.nn_add _ _
        (O.nn_mul _ _ n2 (O.nn_mul _ _ (O.nn_sq ι) (O.nn_mul _ _ (hZn _ _) (O.nn_mul _ _ nC1 (hZn _ _)))))
        (O.nn_mul _ _ n2 (O.nn_mul _ _ (O.nn_sq ι) (O.nn_mul _ _ (hAn _ _) (O.nn_mul _ _ nC2 (hZn _ _)))))))
      (O.nn_mul _ _ n2 (O.nn_mul _ _ (O.nn_sq ι) (O.nn_mul _ _ (hAn _ _) (O.nn_mul _ _ (RatModel.nn_cast K O.toCone 135) (hZn _ _))))))
  refine gronwall_src K O ν h α S _ _ (fun t => alph K inv kz (l.filter (lowB Rc kz)) R' ι (restr K (lowB Rc kz) (A t)) (B t))
    (fun t => bet K inv kz (l.filter (lowB Rc kz)) R' t0 ι (restr K (lowB Rc kz) (A t)))
    (fun t => K.mul (two' K) (EB K inv kz l Rc R' t0 ι (A t))) hν hh hα0 hS0 (fun t => hAn _ _)
    (fun t => O.nn_add _ _ (O.nn_mul _ _ (O.nn_mul _ _ n2 (O.nn_mul _ _ n2 n2)) (O.nn_mul _ _ (O.nn_sq ι) (O.nn_mul _ _ nC1 (hZn _ _))))
      (O.nn_mul _ _ (O.nn_mul _ _ n2 n2) (O.nn_mul _ _ (O.nn_sq ι) (O.nn_mul _ _ (RatModel.nn_cast K O.toCone 135) (hAn _ _)))))
    hα hSn
    hS hβ (fun t => hZn _ _) ?_ T
  intro s
  have hsd : ∀ r, r ∈ l.filter (lowB Rc kz) → Step K h ν (wt K kz r)
      (dF K (restr K (lowB Rc kz) (A (s + 1))) (B (s + 1)) r) (dF K (restr K (lowB Rc kz) (A s)) (B s) r)
      (sub3 K (add3 K (Nl K (l.filter (lowB Rc kz)) (TriadSparse.delta K kz) ι (kv K kz) (restr K (lowB Rc kz) (A s)) (restr K (lowB Rc kz) (A s)) r)
          (sub3 K (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (A s) (A s) r)
            (Nl K (l.filter (lowB Rc kz)) (TriadSparse.delta K kz) ι (kv K kz) (A s) (A s) r)))
        (Nl K (l.filter (lowB Rc kz)) (TriadSparse.delta K kz) ι (kv K kz) (B s) (B s) r)) := fun r hr =>
    step_sub K h ν (wt K kz r) _ _ _ _ _ _
      (restr_step K l (lowB Rc kz) (TriadSparse.delta K kz) ι h ν (wt K kz) (kv K kz) A hA s r
        (mem_filter_true (lowB Rc kz) r l hr)) (hB s r hr)
  have es := enst_step_on K O.toCone (l.filter (lowB Rc kz)) h ν hh hν (wt K kz) hw _ _ _ hsd
  -- |G|² ≤ 2|X|² + 2|E|²
  have hG := lsum_le_on K O.toCone (l.filter (lowB Rc kz)) (fun r _ => sq2' K O
    (sub3 K (add3 K (Nl K (l.filter (lowB Rc kz)) (TriadSparse.delta K kz) ι (kv K kz) (restr K (lowB Rc kz) (A s)) (restr K (lowB Rc kz) (A s)) r)
          (sub3 K (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (A s) (A s) r)
            (Nl K (l.filter (lowB Rc kz)) (TriadSparse.delta K kz) ι (kv K kz) (A s) (A s) r)))
        (Nl K (l.filter (lowB Rc kz)) (TriadSparse.delta K kz) ι (kv K kz) (B s) (B s) r))
    (sub3 K (Nl K (l.filter (lowB Rc kz)) (TriadSparse.delta K kz) ι (kv K kz) (restr K (lowB Rc kz) (A s)) (restr K (lowB Rc kz) (A s)) r)
        (Nl K (l.filter (lowB Rc kz)) (TriadSparse.delta K kz) ι (kv K kz) (B s) (B s) r))
    (sub3 K (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (A s) (A s) r)
        (Nl K (l.filter (lowB Rc kz)) (TriadSparse.delta K kz) ι (kv K kz) (A s) (A s) r))
    (by show K.add (K.add _ (K.add _ (K.neg _))) (K.neg _) = K.add (K.add _ (K.neg _)) (K.add _ (K.neg _)); lib_ring)
    (by show K.add (K.add _ (K.add _ (K.neg _))) (K.neg _) = K.add (K.add _ (K.neg _)) (K.add _ (K.neg _)); lib_ring)
    (by show K.add (K.add _ (K.add _ (K.neg _))) (K.neg _) = K.add (K.add _ (K.neg _)) (K.add _ (K.neg _)); lib_ring))
  rw [lsum_add, lsum_mul_left, lsum_mul_left] at hG
  have fd := force_diff K O inv mul_inv h10 (l.filter (lowB Rc kz)) kz hdN h1 M hMN R' t0 hRM ι (restr K (lowB Rc kz) (A s)) (B s)
  have eb0 := lsum_filter_le K O.toCone (lowB Rc kz)
    (fun r => dot3 K (sub3 K (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (A s) (A s) r)
        (Nl K (l.filter (lowB Rc kz)) (TriadSparse.delta K kz) ι (kv K kz) (A s) (A s) r))
      (sub3 K (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (A s) (A s) r)
        (Nl K (l.filter (lowB Rc kz)) (TriadSparse.delta K kz) ι (kv K kz) (A s) (A s) r))) (fun _ => dot3_nn K O.toCone _) l
  have eb := le_trans' K O.toCone _ _ _ eb0 (err_bound K O inv mul_inv h10 l kz hd h1 M hM Rc R' t0 hRM ι (A s))
  have sum := le_add' K O.toCone _ _ _ _ (le_mul_nn K O.toCone (two' K) _ _ n2 fd) (le_mul_nn K O.toCone (two' K) _ _ n2 eb)
  have hb := le_mul_nn K O.toCone h _ _ hh (le_trans' K O.toCone _ _ _ hG sum)
  have st := le_trans' K O.toCone _ _ _ es (le_add' K O.toCone _ _ _ _ (le_refl' K O.toCone _) hb)
  refine le_trans' K O.toCone _ _ _ st ⟨K.zero, O.nn_zero, ?_⟩
  unfold alph bet EB two' Zl Al C1' C2'
  generalize castN K 135 = c135
  generalize castN K (cube R') = cb
  generalize iv K inv (R' + 1) = ivR
  generalize inv (castN K (t0 + 1)) = it
  generalize lsum K (fun p => K.mul (wt K kz p) (dot3 K (dF K (restr K (lowB Rc kz) (A s)) (B s) p)
    (dF K (restr K (lowB Rc kz) (A s)) (B s) p))) (List.filter (lowB Rc kz) l) = Zd
  generalize lsum K (fun p => K.mul (K.mul (wt K kz p) (wt K kz p)) (dot3 K (dF K (restr K (lowB Rc kz) (A s)) (B s) p)
    (dF K (restr K (lowB Rc kz) (A s)) (B s) p))) (List.filter (lowB Rc kz) l) = Ad
  generalize lsum K (fun q => K.mul (wt K kz q) (dot3 K (restr K (lowB Rc kz) (A s) q) (restr K (lowB Rc kz) (A s) q)))
    (List.filter (lowB Rc kz) l) = Za
  generalize lsum K (fun p => K.mul (K.mul (wt K kz p) (wt K kz p)) (dot3 K (B s p) (B s p))) (List.filter (lowB Rc kz) l) = AB
  generalize lsum K (fun p => K.mul (wt K kz p) (dot3 K (hiF K (lowB Rc kz) (A s) p) (hiF K (lowB Rc kz) (A s) p))) l = Zh
  generalize lsum K (fun p => K.mul (K.mul (wt K kz p) (wt K kz p)) (dot3 K (hiF K (lowB Rc kz) (A s) p) (hiF K (lowB Rc kz) (A s) p))) l = Ah
  generalize lsum K (fun q => K.mul (wt K kz q) (dot3 K (A s q) (A s q))) l = ZA
  generalize lsum K (fun p => K.mul (K.mul (wt K kz p) (wt K kz p)) (dot3 K (A s p) (A s p))) l = AA
  lib_ring

end Main

end TruncCauchy

#print axioms TruncCauchy.lsum_le_on
#print axioms TruncCauchy.enst_step_on
#print axioms TruncCauchy.pw_filter
#print axioms TruncCauchy.lsum_filter_le
#print axioms TruncCauchy.trunc_cauchy

ns/TruncCauchyUpto.lean

lean_all/ns/TruncCauchyUpto.lean

/-
  ns/TruncCauchyUpto.lean — T37 (goal G859c2g, piece b2): the truncation Cauchy estimate on a finite window, over an
  ordered field. Same as TruncCauchy.trunc_cauchy with the coefficient and error bounds asked only for t < n:
    trunc_cauchy_upto   ν^T·Q_d(T) ≤ (ν + hα)^T·(Q_d(0) + T·h·S)   for T ≤ n
  (GronwallUpto.gronwall_upto in place of gronwall_src). The window bounds come from the large-data local bounds.

  Lean 4 core plus ns/TruncCauchy, ns/GronwallUpto.
-/
import ns.TruncCauchy
import ns.GronwallUpto

namespace TruncCauchyUpto
open DetChain NSChain DyadicSmallGlobal LibRing DyadicLarge FourierGalerkin GalerkinForce ForceBound ForceSob
  ModeCount CubeCount ForceUniform NormCompare EnstStep EnstGlobal EnstGalerkin ForceTwoScale ForceBilin EnstDiff
  ForceDiff TruncError TruncBound RestrStep GronwallSource EnstGronwall WeightSplit LatticeSum TruncCauchy

section Main
variable {R : Type} (K : CRing R) (O : OrdR K) {I : Type}
variable (inv : R → R) (mul_inv : ∀ y, y ≠ K.zero → K.mul y (inv y) = K.one) (h10 : K.one ≠ K.zero)

include mul_inv h10 in
theorem trunc_cauchy_upto (l : List I) (kz : I → Mode) (hd : List.Pairwise (fun a b => kz a ≠ kz b) l)
    (h1 : ∀ p, 1 ≤ nrm (kz p)) (M : Nat) (hM : ∀ p, p ∈ l → nrm (kz p) ≤ M + 1) (Rc R' t0 : Nat) (hRM : R' ≤ M)
    (ι h ν α S : R) (hh : O.nn h) (hν : O.nn ν) (hα0 : O.nn α) (hS0 : O.nn S) (A B : Nat → I → V3 R)
    (hA : ∀ t r, Step K h ν (wt K kz r) (A (t + 1) r) (A t r) (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (A t) (A t) r))
    (hB : ∀ t r, r ∈ l.filter (lowB Rc kz) → Step K h ν (wt K kz r) (B (t + 1) r) (B t r)
      (Nl K (l.filter (lowB Rc kz)) (TriadSparse.delta K kz) ι (kv K kz) (B t) (B t) r))
    (n : Nat) (hα : ∀ t, t < n → Le K O.toCone (alph K inv kz (l.filter (lowB Rc kz)) R' ι (restr K (lowB Rc kz) (A t)) (B t)) α)
    (hβ : ∀ t, t < n → Le K O.toCone (bet K inv kz (l.filter (lowB Rc kz)) R' t0 ι (restr K (lowB Rc kz) (A t))) (K.mul ν ν))
    (hS : ∀ t, t < n → Le K O.toCone (K.mul (two' K) (EB K inv kz l Rc R' t0 ι (A t))) S) (T : Nat) (hT : T ≤ n) :
    Le K O.toCone (K.mul (pwr K ν T)
        (Q K ν h (fun t => Zl K kz (l.filter (lowB Rc kz)) (dF K (restr K (lowB Rc kz) (A t)) (B t)))
          (fun t => Al K kz (l.filter (lowB Rc kz)) (dF K (restr K (lowB Rc kz) (A t)) (B t))) T))
      (K.mul (pwr K (K.add ν (K.mul h α)) T)
        (K.add (Q K ν h (fun t => Zl K kz (l.filter (lowB Rc kz)) (dF K (restr K (lowB Rc kz) (A t)) (B t)))
          (fun t => Al K kz (l.filter (lowB Rc kz)) (dF K (restr K (lowB Rc kz) (A t)) (B t))) 0)
          (K.mul (castN K T) (K.mul h S)))) := by
  have hw : ∀ r, O.nn (wt K kz r) := fun r => dot3_nn K O.toCone _
  have hZn : ∀ (L : List I) (u : I → V3 R), O.nn (Zl K kz L u) := fun L u =>
    lsum_nn K O.toCone _ (fun r => O.nn_mul _ _ (hw r) (dot3_nn K O.toCone _)) L
  have hAn : ∀ (L : List I) (u : I → V3 R), O.nn (Al K kz L u) := fun L u =>
    lsum_nn K O.toCone _ (fun r => O.nn_mul _ _ (O.nn_sq _) (dot3_nn K O.toCone _)) L
  have n2 : O.nn (two' K) := O.nn_add _ _ (RatModel.nn_one' K O.toCone) (RatModel.nn_one' K O.toCone)
  have hiv : O.nn (iv K inv (R' + 1)) := iv_nn K O inv mul_inv h10 R'
  have nC1 : O.nn (C1' K inv R') := O.nn_add _ _ (O.nn_mul _ _ (RatModel.nn_cast K O.toCone _) (RatModel.nn_one' K O.toCone))
    (O.nn_mul _ _ (RatModel.nn_cast K O.toCone _) hiv)
  have nC2 : O.nn (C2' K inv R' t0) := O.nn_add _ _ (O.nn_mul _ _ (RatModel.nn_cast K O.toCone _) (O.nn_sq _))
    (O.nn_mul _ _ (RatModel.nn_cast K O.toCone _) hiv)
  have hdN := pw_filter kz (lowB Rc kz) l hd
  have hMN : ∀ p, p ∈ l.filter (lowB Rc kz) → nrm (kz p) ≤ M + 1 := fun p hp => hM p (mem_of_mem_filter (lowB Rc kz) p l hp)
  have hSn : ∀ t, O.nn (K.mul (two' K) (EB K inv kz l Rc R' t0 ι (A t))) := fun t =>
    O.nn_mul _ _ n2 (O.nn_add _ _
      (O.nn_mul _ _ n2 (O.nn_add _ _
        (O.nn_mul _ _ n2 (O.nn_mul _ _ (O.nn_sq ι) (O.nn_mul _ _ (hZn _ _) (O.nn_mul _ _ nC1 (hZn _ _)))))
        (O.nn_mul _ _ n2 (O.nn_mul _ _ (O.nn_sq ι) (O.nn_mul _ _ (hAn _ _) (O.nn_mul _ _ nC2 (hZn _ _)))))))
      (O.nn_mul _ _ n2 (O.nn_mul _ _ (O.nn_sq ι) (O.nn_mul _ _ (hAn _ _) (O.nn_mul _ _ (RatModel.nn_cast K O.toCone 135) (hZn _ _))))))
  refine GronwallUpto.gronwall_upto K O ν h α S _ _ (fun t => alph K inv kz (l.filter (lowB Rc kz)) R' ι (restr K (lowB Rc kz) (A t)) (B t))
    (fun t => bet K inv kz (l.filter (lowB Rc kz)) R' t0 ι (restr K (lowB Rc kz) (A t)))
    (fun t => K.mul (two' K) (EB K inv kz l Rc R' t0 ι (A t))) hν hh hα0 hS0 (fun t => hAn _ _) n
    (fun t _ => O.nn_add _ _ (O.nn_mul _ _ (O.nn_mul _ _ n2 (O.nn_mul _ _ n2 n2)) (O.nn_mul _ _ (O.nn_sq ι) (O.nn_mul _ _ nC1 (hZn _ _))))
      (O.nn_mul _ _ (O.nn_mul _ _ n2 n2) (O.nn_mul _ _ (O.nn_sq ι) (O.nn_mul _ _ (RatModel.nn_cast K O.toCone 135) (hAn _ _)))))
    hα (fun t _ => hSn t)
    hS hβ (fun t => hZn _ _) ?_ T hT
  intro s _
  have hsd : ∀ r, r ∈ l.filter (lowB Rc kz) → Step K h ν (wt K kz r)
      (dF K (restr K (lowB Rc kz) (A (s + 1))) (B (s + 1)) r) (dF K (restr K (lowB Rc kz) (A s)) (B s) r)
      (sub3 K (add3 K (Nl K (l.filter (lowB Rc kz)) (TriadSparse.delta K kz) ι (kv K kz) (restr K (lowB Rc kz) (A s)) (restr K (lowB Rc kz) (A s)) r)
          (sub3 K (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (A s) (A s) r)
            (Nl K (l.filter (lowB Rc kz)) (TriadSparse.delta K kz) ι (kv K kz) (A s) (A s) r)))
        (Nl K (l.filter (lowB Rc kz)) (TriadSparse.delta K kz) ι (kv K kz) (B s) (B s) r)) := fun r hr =>
    step_sub K h ν (wt K kz r) _ _ _ _ _ _
      (restr_step K l (lowB Rc kz) (TriadSparse.delta K kz) ι h ν (wt K kz) (kv K kz) A hA s r
        (mem_filter_true (lowB Rc kz) r l hr)) (hB s r hr)
  have es := enst_step_on K O.toCone (l.filter (lowB Rc kz)) h ν hh hν (wt K kz) hw _ _ _ hsd
  -- |G|² ≤ 2|X|² + 2|E|²
  have hG := lsum_le_on K O.toCone (l.filter (lowB Rc kz)) (fun r _ => sq2' K O
    (sub3 K (add3 K (Nl K (l.filter (lowB Rc kz)) (TriadSparse.delta K kz) ι (kv K kz) (restr K (lowB Rc kz) (A s)) (restr K (lowB Rc kz) (A s)) r)
          (sub3 K (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (A s) (A s) r)
            (Nl K (l.filter (lowB Rc kz)) (TriadSparse.delta K kz) ι (kv K kz) (A s) (A s) r)))
        (Nl K (l.filter (lowB Rc kz)) (TriadSparse.delta K kz) ι (kv K kz) (B s) (B s) r))
    (sub3 K (Nl K (l.filter (lowB Rc kz)) (TriadSparse.delta K kz) ι (kv K kz) (restr K (lowB Rc kz) (A s)) (restr K (lowB Rc kz) (A s)) r)
        (Nl K (l.filter (lowB Rc kz)) (TriadSparse.delta K kz) ι (kv K kz) (B s) (B s) r))
    (sub3 K (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (A s) (A s) r)
        (Nl K (l.filter (lowB Rc kz)) (TriadSparse.delta K kz) ι (kv K kz) (A s) (A s) r))
    (by show K.add (K.add _ (K.add _ (K.neg _))) (K.neg _) = K.add (K.add _ (K.neg _)) (K.add _ (K.neg _)); lib_ring)
    (by show K.add (K.add _ (K.add _ (K.neg _))) (K.neg _) = K.add (K.add _ (K.neg _)) (K.add _ (K.neg _)); lib_ring)
    (by show K.add (K.add _ (K.add _ (K.neg _))) (K.neg _) = K.add (K.add _ (K.neg _)) (K.add _ (K.neg _)); lib_ring))
  rw [lsum_add, lsum_mul_left, lsum_mul_left] at hG
  have fd := force_diff K O inv mul_inv h10 (l.filter (lowB Rc kz)) kz hdN h1 M hMN R' t0 hRM ι (restr K (lowB Rc kz) (A s)) (B s)
  have eb0 := lsum_filter_le K O.toCone (lowB Rc kz)
    (fun r => dot3 K (sub3 K (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (A s) (A s) r)
        (Nl K (l.filter (lowB Rc kz)) (TriadSparse.delta K kz) ι (kv K kz) (A s) (A s) r))
      (sub3 K (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (A s) (A s) r)
        (Nl K (l.filter (lowB Rc kz)) (TriadSparse.delta K kz) ι (kv K kz) (A s) (A s) r))) (fun _ => dot3_nn K O.toCone _) l
  have eb := le_trans' K O.toCone _ _ _ eb0 (err_bound K O inv mul_inv h10 l kz hd h1 M hM Rc R' t0 hRM ι (A s))
  have sum := le_add' K O.toCone _ _ _ _ (le_mul_nn K O.toCone (two' K) _ _ n2 fd) (le_mul_nn K O.toCone (two' K) _ _ n2 eb)
  have hb := le_mul_nn K O.toCone h _ _ hh (le_trans' K O.toCone _ _ _ hG sum)
  have st := le_trans' K O.toCone _ _ _ es (le_add' K O.toCone _ _ _ _ (le_refl' K O.toCone _) hb)
  refine le_trans' K O.toCone _ _ _ st ⟨K.zero, O.nn_zero, ?_⟩
  unfold alph bet EB two' Zl Al C1' C2'
  generalize castN K 135 = c135
  generalize castN K (cube R') = cb
  generalize iv K inv (R' + 1) = ivR
  generalize inv (castN K (t0 + 1)) = it
  generalize lsum K (fun p => K.mul (wt K kz p) (dot3 K (dF K (restr K (lowB Rc kz) (A s)) (B s) p)
    (dF K (restr K (lowB Rc kz) (A s)) (B s) p))) (List.filter (lowB Rc kz) l) = Zd
  generalize lsum K (fun p => K.mul (K.mul (wt K kz p) (wt K kz p)) (dot3 K (dF K (restr K (lowB Rc kz) (A s)) (B s) p)
    (dF K (restr K (lowB Rc kz) (A s)) (B s) p))) (List.filter (lowB Rc kz) l) = Ad
  generalize lsum K (fun q => K.mul (wt K kz q) (dot3 K (restr K (lowB Rc kz) (A s) q) (restr K (lowB Rc kz) (A s) q)))
    (List.filter (lowB Rc kz) l) = Za
  generalize lsum K (fun p => K.mul (K.mul (wt K kz p) (wt K kz p)) (dot3 K (B s p) (B s p))) (List.filter (lowB Rc kz) l) = AB
  generalize lsum K (fun p => K.mul (wt K kz p) (dot3 K (hiF K (lowB Rc kz) (A s) p) (hiF K (lowB Rc kz) (A s) p))) l = Zh
  generalize lsum K (fun p => K.mul (K.mul (wt K kz p) (wt K kz p)) (dot3 K (hiF K (lowB Rc kz) (A s) p) (hiF K (lowB Rc kz) (A s) p))) l = Ah
  generalize lsum K (fun q => K.mul (wt K kz q) (dot3 K (A s q) (A s q))) l = ZA
  generalize lsum K (fun p => K.mul (K.mul (wt K kz p) (wt K kz p)) (dot3 K (A s p) (A s p))) l = AA
  lib_ring

end Main

end TruncCauchyUpto

#print axioms TruncCauchyUpto.trunc_cauchy_upto

ns/TruncCoef.lean

lean_all/ns/TruncCoef.lean

/-
  ns/TruncCoef.lean — T37 cycle 1792 (goal G852m): the truncation Cauchy modulus mode by mode. For a mode r of the
  coarse list L and Q = νZ + hν²A of a difference d:
    coef_le     r ∈ L  ⇒  ν·(w_r·|d_r|²) ≤ Q(d)
    trunc_coef  ν^T·Q(u − v) ≤ ε, ν^T·Q(w − v) ≤ ε  ⇒  ν^T·ν·w_r·|u_r(T) − w_r(T)|² ≤ 4ε
  With TruncEps (ε = 1/(j+1)) and TruncTri: every low Fourier coefficient of the restricted fine truncations is
  Cauchy with an explicit modulus (uniform in the fine truncations), the truncation analogue of DyadicCoef.
  Lean 4 core plus ns/TruncTri, ns/DyadicCoef (term_le).
-/
import ns.TruncTri
import ns.DyadicCoef

namespace TruncCoef
open DetChain NSChain DyadicSmallGlobal LibRing DyadicLarge FourierGalerkin GalerkinForce ForceBound ForceSob
  ModeCount CubeCount ForceUniform NormCompare EnstStep EnstGlobal EnstGalerkin ForceTwoScale ForceBilin EnstDiff
  ForceDiff TruncError TruncBound RestrStep GronwallSource EnstGronwall WeightSplit LatticeSum TruncCauchy

section Gen
variable {R : Type} (K : CRing R) (O : OrdR K) {I : Type}

theorem coef_le (ν h : R) (hν : O.nn ν) (hh : O.nn h) (kz : I → Mode) (L : List I) (d : Nat → I → V3 R) (T : Nat)
    (r : I) (hr : r ∈ L) :
    Le K O.toCone (K.mul ν (K.mul (wt K kz r) (dot3 K (d T r) (d T r))))
      (Q K ν h (fun t => Zl K kz L (d t)) (fun t => Al K kz L (d t)) T) := by
  have hw : ∀ p, O.nn (wt K kz p) := fun p => dot3_nn K O.toCone _
  have t1 := DyadicCoef.term_le K O (fun p => K.mul (wt K kz p) (dot3 K (d T p) (d T p)))
    (fun p => O.nn_mul _ _ (hw p) (dot3_nn K O.toCone _)) L r hr
  have t2 := le_mul_nn K O.toCone ν _ _ hν t1
  refine le_trans' K O.toCone _ _ _ t2 ⟨K.mul (K.mul h (K.mul ν ν)) (Al K kz L (d T)),
    O.nn_mul _ _ (O.nn_mul _ _ hh (O.nn_sq ν))
      (lsum_nn K O.toCone _ (fun p => O.nn_mul _ _ (O.nn_sq _) (dot3_nn K O.toCone _)) L), rfl⟩

theorem trunc_coef (ν h eps : R) (hν : O.nn ν) (hh : O.nn h) (kz : I → Mode) (L : List I) (u v w : Nat → I → V3 R)
    (T : Nat) (r : I) (hr : r ∈ L)
    (h1 : Le K O.toCone (K.mul (pwr K ν T)
      (Q K ν h (fun t => Zl K kz L (dF K (u t) (v t))) (fun t => Al K kz L (dF K (u t) (v t))) T)) eps)
    (h2 : Le K O.toCone (K.mul (pwr K ν T)
      (Q K ν h (fun t => Zl K kz L (dF K (w t) (v t))) (fun t => Al K kz L (dF K (w t) (v t))) T)) eps) :
    Le K O.toCone (K.mul (pwr K ν T) (K.mul ν (K.mul (wt K kz r) (dot3 K (dF K (u T) (w T) r) (dF K (u T) (w T) r)))))
      (K.mul (K.mul (two' K) (two' K)) eps) :=
  le_trans' K O.toCone _ _ _
    (le_mul_nn K O.toCone _ _ _ (EnstGronwall.pwr_nn K O ν hν T)
      (coef_le K O ν h hν hh kz L (fun t => dF K (u t) (w t)) T r hr))
    (TruncTri.tri_eps K O ν h eps hν hh kz L u v w T h1 h2)

end Gen
end TruncCoef

#print axioms TruncCoef.coef_le
#print axioms TruncCoef.trunc_coef

ns/TruncCoefU.lean

lean_all/ns/TruncCoefU.lean

/-
  ns/TruncCoefU.lean — T37 cycle 1822 (goal G857e): the time-uniform mode-by-mode Cauchy bound. For a weight
  c ≥ 0 (c = ν² in TruncUniform) and Q = νZ + hν²A of differences on the coarse list L:
    coef_u   c·Q(u − v)(n) ≤ X and c·Q(w − v)(n) ≤ X  ⇒  c·ν·w_r·|u_r(n) − w_r(n)|² ≤ 4X   (r ∈ L)
  With TruncUniform (X = iR·κ·2Q₂(0), v the coarse R-scheme, u, w restrictions of two fine truncations) every low
  Fourier coefficient of the fine truncations is Cauchy with modulus O(1/R²) uniformly for all times n.
  Lean 4 core plus ns/TruncCoef, ns/TruncTri.
-/
import ns.TruncCoef

namespace TruncCoefU
open DetChain NSChain DyadicSmallGlobal LibRing DyadicLarge FourierGalerkin GalerkinForce ForceBound ForceSob
  ModeCount CubeCount ForceUniform NormCompare EnstStep EnstGlobal EnstGalerkin ForceTwoScale ForceBilin EnstDiff
  ForceDiff TruncError TruncBound RestrStep GronwallSource EnstGronwall WeightSplit LatticeSum TruncCauchy

section Gen
variable {R : Type} (K : CRing R) (O : OrdR K) {I : Type}

theorem coef_u (ν h c X : R) (hν : O.nn ν) (hh : O.nn h) (hc : O.nn c) (kz : I → Mode) (L : List I)
    (u v w : Nat → I → V3 R) (n : Nat) (r : I) (hr : r ∈ L)
    (h1 : Le K O.toCone (K.mul c (Q K ν h (fun t => Zl K kz L (dF K (u t) (v t))) (fun t => Al K kz L (dF K (u t) (v t))) n)) X)
    (h2 : Le K O.toCone (K.mul c (Q K ν h (fun t => Zl K kz L (dF K (w t) (v t))) (fun t => Al K kz L (dF K (w t) (v t))) n)) X) :
    Le K O.toCone (K.mul c (K.mul ν (K.mul (wt K kz r) (dot3 K (dF K (u n) (w n) r) (dF K (u n) (w n) r)))))
      (K.mul (K.mul (two' K) (two' K)) X) := by
  have n2 : O.nn (two' K) := O.nn_add _ _ (RatModel.nn_one' K O.toCone) (RatModel.nn_one' K O.toCone)
  have a := le_mul_nn K O.toCone c _ _ hc
    (TruncCoef.coef_le K O ν h hν hh kz L (fun t => dF K (u t) (w t)) n r hr)
  have b := le_mul_nn K O.toCone c _ _ hc (TruncTri.tri_Q K O ν h hν hh kz L u v w n)
  have s := le_add' K O.toCone _ _ _ _ (le_mul_nn K O.toCone (two' K) _ _ n2 h1) (le_mul_nn K O.toCone (two' K) _ _ n2 h2)
  refine le_trans' K O.toCone _ _ _ a (le_trans' K O.toCone _ _ _ b (le_trans' K O.toCone _ _ _ ⟨K.zero, O.nn_zero, ?_⟩
    (le_trans' K O.toCone _ _ _ s ⟨K.zero, O.nn_zero, ?_⟩)))
  · show K.add (K.mul c (K.add (K.mul (K.add K.one K.one) (Q K ν h (fun t => Zl K kz L (dF K (u t) (v t))) (fun t => Al K kz L (dF K (u t) (v t))) n))
        (K.mul (K.add K.one K.one) (Q K ν h (fun t => Zl K kz L (dF K (w t) (v t))) (fun t => Al K kz L (dF K (w t) (v t))) n)))) K.zero
      = K.add (K.mul (K.add K.one K.one) (K.mul c (Q K ν h (fun t => Zl K kz L (dF K (u t) (v t))) (fun t => Al K kz L (dF K (u t) (v t))) n)))
        (K.mul (K.add K.one K.one) (K.mul c (Q K ν h (fun t => Zl K kz L (dF K (w t) (v t))) (fun t => Al K kz L (dF K (w t) (v t))) n)))
    generalize Q K ν h (fun t => Zl K kz L (dF K (u t) (v t))) (fun t => Al K kz L (dF K (u t) (v t))) n = P
    generalize Q K ν h (fun t => Zl K kz L (dF K (w t) (v t))) (fun t => Al K kz L (dF K (w t) (v t))) n = P'
    lib_ring
  · show K.add (K.add (K.mul (K.add K.one K.one) X) (K.mul (K.add K.one K.one) X)) K.zero
      = K.mul (K.mul (K.add K.one K.one) (K.add K.one K.one)) X
    lib_ring

end Gen
end TruncCoefU

#print axioms TruncCoefU.coef_u

ns/TruncDamp.lean

lean_all/ns/TruncDamp.lean

/-
  ns/TruncDamp.lean — T37 cycle 1810 (goal G857c, piece b): the truncation comparison without exponential growth.
  With TruncStep.trunc_step as the step of DampGronwall.damp_all: if for every t
      α_t + β_t ≤ ν²   (alph, bet of TruncCauchy)   and   2·EB(A_t) ≤ Sb   (TruncBound error),
  then
    trunc_damp   Q_d(n) ≤ Q_d(0) + n·h·Sb      (d = A|_N − B on l_N, Q = νZ + hν²A)
  — linear in time; Z_d ≤ A_d from EnstH2.Z_le_A (w ≥ 1).
  Lean 4 core plus ns/TruncStep, ns/DampGronwall, ns/EnstH2.
-/
import ns.TruncStep
import ns.DampGronwall
import ns.EnstH2

namespace TruncDamp
open DetChain NSChain DyadicSmallGlobal LibRing DyadicLarge FourierGalerkin GalerkinForce ForceBound ForceSob
  ModeCount CubeCount ForceUniform NormCompare EnstStep EnstGlobal EnstGalerkin ForceTwoScale ForceBilin EnstDiff
  ForceDiff TruncError TruncBound RestrStep GronwallSource EnstGronwall WeightSplit LatticeSum TruncCauchy

section Main
variable {R : Type} (K : CRing R) (O : OrdR K) {I : Type}
variable (inv : R → R) (mul_inv : ∀ y, y ≠ K.zero → K.mul y (inv y) = K.one) (h10 : K.one ≠ K.zero)

include mul_inv h10 in
theorem trunc_damp (l : List I) (kz : I → Mode) (hd : List.Pairwise (fun a b => kz a ≠ kz b) l)
    (h1 : ∀ p, 1 ≤ nrm (kz p)) (M : Nat) (hM : ∀ p, p ∈ l → nrm (kz p) ≤ M + 1) (Rc R' t0 : Nat) (hRM : R' ≤ M)
    (ι h ν Sb : R) (hh : O.nn h) (hν : O.nn ν) (A B : Nat → I → V3 R)
    (hA : ∀ t r, Step K h ν (wt K kz r) (A (t + 1) r) (A t r) (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (A t) (A t) r))
    (hB : ∀ t r, r ∈ l.filter (lowB Rc kz) → Step K h ν (wt K kz r) (B (t + 1) r) (B t r)
      (Nl K (l.filter (lowB Rc kz)) (TriadSparse.delta K kz) ι (kv K kz) (B t) (B t) r))
    (hab : ∀ t, Le K O.toCone (K.add (alph K inv kz (l.filter (lowB Rc kz)) R' ι (restr K (lowB Rc kz) (A t)) (B t))
      (bet K inv kz (l.filter (lowB Rc kz)) R' t0 ι (restr K (lowB Rc kz) (A t)))) (K.mul ν ν))
    (hS : ∀ t, Le K O.toCone (K.mul (two' K) (EB K inv kz l Rc R' t0 ι (A t))) Sb) (n : Nat) :
    Le K O.toCone (Q K ν h (fun t => Zl K kz (l.filter (lowB Rc kz)) (dF K (restr K (lowB Rc kz) (A t)) (B t)))
        (fun t => Al K kz (l.filter (lowB Rc kz)) (dF K (restr K (lowB Rc kz) (A t)) (B t))) n)
      (K.add (Q K ν h (fun t => Zl K kz (l.filter (lowB Rc kz)) (dF K (restr K (lowB Rc kz) (A t)) (B t)))
        (fun t => Al K kz (l.filter (lowB Rc kz)) (dF K (restr K (lowB Rc kz) (A t)) (B t))) 0)
        (K.mul (castN K n) (K.mul h Sb))) := by
  have hw : ∀ r, O.nn (wt K kz r) := fun r => dot3_nn K O.toCone _
  have hZn : ∀ (L : List I) (u : I → V3 R), O.nn (Zl K kz L u) := fun L u =>
    lsum_nn K O.toCone _ (fun r => O.nn_mul _ _ (hw r) (dot3_nn K O.toCone _)) L
  have hAn : ∀ (L : List I) (u : I → V3 R), O.nn (Al K kz L u) := fun L u =>
    lsum_nn K O.toCone _ (fun r => O.nn_mul _ _ (O.nn_sq _) (dot3_nn K O.toCone _)) L
  have n2 : O.nn (two' K) := O.nn_add _ _ (RatModel.nn_one' K O.toCone) (RatModel.nn_one' K O.toCone)
  have hiv : O.nn (iv K inv (R' + 1)) := iv_nn K O inv mul_inv h10 R'
  have nC1 : O.nn (C1' K inv R') := O.nn_add _ _ (O.nn_mul _ _ (RatModel.nn_cast K O.toCone _) (RatModel.nn_one' K O.toCone))
    (O.nn_mul _ _ (RatModel.nn_cast K O.toCone _) hiv)
  exact DampGronwall.damp_all K O ν h Sb _ _
    (fun t => alph K inv kz (l.filter (lowB Rc kz)) R' ι (restr K (lowB Rc kz) (A t)) (B t))
    (fun t => bet K inv kz (l.filter (lowB Rc kz)) R' t0 ι (restr K (lowB Rc kz) (A t)))
    (fun t => K.mul (two' K) (EB K inv kz l Rc R' t0 ι (A t))) hh
    (fun t => O.nn_add _ _ (O.nn_mul _ _ (O.nn_mul _ _ n2 (O.nn_mul _ _ n2 n2)) (O.nn_mul _ _ (O.nn_sq ι) (O.nn_mul _ _ nC1 (hZn _ _))))
      (O.nn_mul _ _ (O.nn_mul _ _ n2 n2) (O.nn_mul _ _ (O.nn_sq ι) (O.nn_mul _ _ (RatModel.nn_cast K O.toCone 135) (hAn _ _)))))
    (fun t => hAn _ _) (fun t => EnstH2.Z_le_A K O kz h1 _ _) hab hS
    (fun s => TruncStep.trunc_step K O inv mul_inv h10 l kz hd h1 M hM Rc R' t0 hRM ι h ν hh hν A B hA hB s) n

end Main
end TruncDamp

#print axioms TruncDamp.trunc_damp

ns/TruncDamp0.lean

lean_all/ns/TruncDamp0.lean

/-
  ns/TruncDamp0.lean — T37 cycle 1860 (goal G859b, piece a): TruncDampSum without B_0 = A_0|_N. The coarse scheme B
  starts from any small state (cH·ι²·Q₂(B, 0) ≤ ν³, Q₂(B, 0) ≤ ν·Aa); then
    trunc_damp_sum0   Q_d(n) ≤ Q_d(0) + Σ_{t<n} h·S_t
  (same proof, the initial difference kept). This is the form needed to glue a finite-time estimate on [0, t₂]
  (where B(t₂) ≠ A(t₂)|_N) to the time-uniform estimate after t₂.
  Lean 4 core plus ns/TruncDampSum.
-/
import ns.TruncDampSum

namespace TruncDamp0
open DetChain NSChain DyadicSmallGlobal LibRing DyadicLarge FourierGalerkin GalerkinForce ForceBound ForceSob
  ModeCount CubeCount ForceUniform NormCompare EnstStep EnstGlobal EnstGalerkin ForceTwoScale ForceBilin EnstDiff
  ForceDiff TruncError TruncBound RestrStep GronwallSource EnstGronwall WeightSplit LatticeSum TruncCauchy TruncSmall
  TruncStart TruncRate3 TruncStart3 SchemeTele TruncDampSum

section Gen
variable {R : Type} (K : CRing R) (O : OrdR K) {I : Type}
variable (inv : R → R) (mul_inv : ∀ y, y ≠ K.zero → K.mul y (inv y) = K.one) (h10 : K.one ≠ K.zero)

include mul_inv h10 in
theorem trunc_damp_sum0 (l : List I) (kz : I → Mode) (hd : List.Pairwise (fun a b => kz a ≠ kz b) l)
    (h1 : ∀ p, 1 ≤ nrm (kz p)) (M : Nat) (hM : ∀ p, p ∈ l → nrm (kz p) ≤ M + 1) (Rc R' t0 : Nat) (hRM : R' ≤ M)
    (ι h ν Aa iR : R) (hh : O.nn h) (hν : O.nn ν) (hν0 : ν ≠ K.zero) (hAa : O.nn Aa)
    (hiRn : O.nn iR) (hiR : K.mul (K.mul (castN K (Rc + 1)) (castN K (Rc + 1))) iR = K.one)
    (A B : Nat → I → V3 R)
    (hA : ∀ t r, Step K h ν (wt K kz r) (A (t + 1) r) (A t r) (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (A t) (A t) r))
    (hB : ∀ t r, Step K h ν (wt K kz r) (B (t + 1) r) (B t r)
      (Nl K (l.filter (lowB Rc kz)) (TriadSparse.delta K kz) ι (kv K kz) (B t) (B t) r))
    (smallA : Le K O.toCone (K.mul (K.mul (EnstH2.cH K) (K.mul ι ι)) (QH K ν h kz l A 0)) (K.mul ν (K.mul ν ν)))
    (hQA : Le K O.toCone (QH K ν h kz l A 0) (K.mul ν Aa))
    (smallB : Le K O.toCone (K.mul (K.mul (EnstH2.cH K) (K.mul ι ι)) (QH K ν h kz (l.filter (lowB Rc kz)) B 0)) (K.mul ν (K.mul ν ν)))
    (hQB : Le K O.toCone (QH K ν h kz (l.filter (lowB Rc kz)) B 0) (K.mul ν Aa))
    (hsm : Le K O.toCone (K.add (K.add (K.mul (K.mul (two' K) (K.mul (two' K) (two' K))) (K.mul (K.mul ι ι) (K.mul (C1' K inv R') Aa))) (K.mul (K.mul (two' K) (two' K)) (K.mul (K.mul ι ι) (K.mul (castN K 135) Aa))))
      (K.mul (K.mul (two' K) (K.mul (two' K) (two' K))) (K.mul (K.mul ι ι) (K.mul (C2' K inv R' t0) Aa)))) (K.mul ν ν)) (n : Nat) :
    Le K O.toCone (Q K ν h (fun t => Zl K kz (l.filter (lowB Rc kz)) (dF K (restr K (lowB Rc kz) (A t)) (B t)))
        (fun t => Al K kz (l.filter (lowB Rc kz)) (dF K (restr K (lowB Rc kz) (A t)) (B t))) n)
      (K.add (Q K ν h (fun t => Zl K kz (l.filter (lowB Rc kz)) (dF K (restr K (lowB Rc kz) (A t)) (B t)))
        (fun t => Al K kz (l.filter (lowB Rc kz)) (dF K (restr K (lowB Rc kz) (A t)) (B t))) 0)
        (tsum K (fun t => K.mul h (St K inv kz l R' t0 ι Aa iR A t)) n)) := by
  have two : O.nn (K.add K.one K.one) := O.nn_add _ _ (RatModel.nn_one' K O.toCone) (RatModel.nn_one' K O.toCone)
  have n135 : O.nn (castN K 135) := RatModel.nn_cast K O.toCone 135
  have bA := a_bound K O inv mul_inv h10 l kz hd h1 M hM ι h ν Aa hh hν hν0 A hA smallA hQA
  have bB := a_bound K O inv mul_inv h10 (l.filter (lowB Rc kz)) kz (pw_filter kz (lowB Rc kz) l hd) h1 M
    (fun p hp => hM p (mem_of_mem_filter (lowB Rc kz) p l hp)) ι h ν Aa hh hν hν0 B hB smallB hQB
  have n2 : O.nn (two' K) := two
  have hiv : O.nn (iv K inv (R' + 1)) := iv_nn K O inv mul_inv h10 R'
  have nC1 : O.nn (C1' K inv R') := O.nn_add _ _ (O.nn_mul _ _ (RatModel.nn_cast K O.toCone _) (RatModel.nn_one' K O.toCone))
    (O.nn_mul _ _ (RatModel.nn_cast K O.toCone _) hiv)
  have nC2 : O.nn (C2' K inv R' t0) := O.nn_add _ _ (O.nn_mul _ _ (RatModel.nn_cast K O.toCone _) (O.nn_sq _))
    (O.nn_mul _ _ (RatModel.nn_cast K O.toCone _) hiv)
  have nii : O.nn (K.mul ι ι) := O.nn_sq ι
  have hw : ∀ r, O.nn (wt K kz r) := fun r => dot3_nn K O.toCone _
  have hZn : ∀ (L : List I) (u : I → V3 R), O.nn (Zl K kz L u) := fun L u =>
    lsum_nn K O.toCone _ (fun r => O.nn_mul _ _ (hw r) (dot3_nn K O.toCone _)) L
  have hAn : ∀ (L : List I) (u : I → V3 R), O.nn (Al K kz L u) := fun L u =>
    lsum_nn K O.toCone _ (fun r => O.nn_mul _ _ (O.nn_sq _) (dot3_nn K O.toCone _)) L
  have hZa : ∀ t, Le K O.toCone (Zl K kz (l.filter (lowB Rc kz)) (restr K (lowB Rc kz) (A t))) Aa := fun t =>
    le_trans' K O.toCone _ _ _ (TruncRate.Z_restr_le K O l kz (lowB Rc kz) (A t)) (bA t).2
  have hZh : ∀ t, Le K O.toCone (Zl K kz l (hiF K (lowB Rc kz) (A t))) (K.mul (Al K kz l (A t)) iR) := fun t =>
    TruncRate.Zh_le K O l kz Rc (A t) (Al K kz l (A t)) (le_refl' K O.toCone _) iR hiR hiRn
  have hAh : ∀ t, Le K O.toCone (Al K kz l (hiF K (lowB Rc kz) (A t))) (K.mul (Bl K kz l (A t)) iR) := fun t =>
    Ah_le K O l kz Rc (A t) (Bl K kz l (A t)) (le_refl' K O.toCone _) iR hiR hiRn
  have nZh : ∀ t, O.nn (K.mul (Al K kz l (A t)) iR) := fun t => O.nn_mul _ _ (hAn _ _) hiRn
  have hab : ∀ t, Le K O.toCone (K.add (alph K inv kz (l.filter (lowB Rc kz)) R' ι (restr K (lowB Rc kz) (A t)) (B t))
      (bet K inv kz (l.filter (lowB Rc kz)) R' t0 ι (restr K (lowB Rc kz) (A t)))) (K.mul ν ν) := fun t =>
    le_trans' K O.toCone _ _ _ (le_add' K O.toCone _ _ _ _
      (le_add' K O.toCone _ _ _ _
        (le_mul_nn K O.toCone _ _ _ (O.nn_mul _ _ n2 (O.nn_mul _ _ n2 n2)) (le_mul_nn K O.toCone _ _ _ nii
          (le_mul_nn K O.toCone _ _ _ nC1 (hZa t))))
        (le_mul_nn K O.toCone _ _ _ (O.nn_mul _ _ n2 n2) (le_mul_nn K O.toCone _ _ _ nii
          (le_mul_nn K O.toCone _ _ _ n135 (bB t).1))))
      (le_mul_nn K O.toCone _ _ _ (O.nn_mul _ _ n2 (O.nn_mul _ _ n2 n2))
        (le_mul_nn K O.toCone _ _ _ nii (le_mul_nn K O.toCone _ _ _ nC2 (hZa t))))) hsm
  have hS : ∀ t, Le K O.toCone (K.mul (two' K) (EB K inv kz l Rc R' t0 ι (A t))) (St K inv kz l R' t0 ι Aa iR A t) := by
    intro t
    refine le_mul_nn K O.toCone _ _ _ n2 (le_add' K O.toCone _ _ _ _
      (le_mul_nn K O.toCone _ _ _ n2 (le_add' K O.toCone _ _ _ _
        (le_mul_nn K O.toCone _ _ _ n2 (le_mul_nn K O.toCone _ _ _ nii
          (TruncRate.mm K O _ _ _ _ (hZn _ _) (O.nn_mul _ _ nC1 hAa) (hZh t) (le_mul_nn K O.toCone _ _ _ nC1 (bA t).2))))
        (le_mul_nn K O.toCone _ _ _ n2 (le_mul_nn K O.toCone _ _ _ nii
          (TruncRate.mm K O _ _ _ _ (hAn _ _) (O.nn_mul _ _ nC2 hAa) (hAh t) (le_mul_nn K O.toCone _ _ _ nC2 (bA t).2))))))
      (le_mul_nn K O.toCone _ _ _ n2 (le_mul_nn K O.toCone _ _ _ nii
        (TruncRate.mm K O _ _ _ _ (hAn _ _) (O.nn_mul _ _ n135 (nZh t)) (bA t).1 (le_mul_nn K O.toCone _ _ _ n135 (hZh t))))))
  have t := DampSum.damp_sum K O ν h
    (fun t => Zl K kz (l.filter (lowB Rc kz)) (dF K (restr K (lowB Rc kz) (A t)) (B t)))
    (fun t => Al K kz (l.filter (lowB Rc kz)) (dF K (restr K (lowB Rc kz) (A t)) (B t)))
    (fun t => alph K inv kz (l.filter (lowB Rc kz)) R' ι (restr K (lowB Rc kz) (A t)) (B t))
    (fun t => bet K inv kz (l.filter (lowB Rc kz)) R' t0 ι (restr K (lowB Rc kz) (A t)))
    (fun t => St K inv kz l R' t0 ι Aa iR A t) hh
    (fun t => O.nn_add _ _ (O.nn_mul _ _ (O.nn_mul _ _ n2 (O.nn_mul _ _ n2 n2)) (O.nn_mul _ _ nii (O.nn_mul _ _ nC1 (hZn _ _))))
      (O.nn_mul _ _ (O.nn_mul _ _ n2 n2) (O.nn_mul _ _ nii (O.nn_mul _ _ n135 (hAn _ _)))))
    (fun t => hAn _ _) (fun t => EnstH2.Z_le_A K O kz h1 _ _) hab
    (fun s => le_trans' K O.toCone _ _ _
      (TruncStep.trunc_step K O inv mul_inv h10 l kz hd h1 M hM Rc R' t0 hRM ι h ν hh hν A B hA (fun t r _ => hB t r) s)
      (le_add' K O.toCone _ _ _ _ (le_refl' K O.toCone _) (le_mul_nn K O.toCone h _ _ hh (hS s)))) n
  exact t

end Gen
end TruncDamp0

#print axioms TruncDamp0.trunc_damp_sum0

ns/TruncDamp3.lean

lean_all/ns/TruncDamp3.lean

/-
  ns/TruncDamp3.lean — T37 cycle 1812 (goal G857b): the time-uniform-rate truncation estimate for small data.
  With the hypotheses of TruncStart3 (small H² and H³ data, B_0 = A_0|_N) and the smallness condition
      α* + β* ≤ ν²,   α* = 8ι²C₁Aa + 4·135ι²Aa,   β* = 8ι²C₂Aa
  (which bounds alph + bet at every step), TruncDamp.trunc_damp gives
    trunc_damp3   Q_d(n) ≤ n·h·S,   S = 2[2(2ι²(Aa iR)C₁Aa + 2ι²(Bb iR)C₂Aa) + 2ι²Aa·135·Aa iR],  iR = 1/(R+1)²
  — no factor (ν + hα)^n: on [0, T] (nh = T) the truncation error is T·S = O(T/R²), uniformly in the fine
  truncation l.
  Lean 4 core plus ns/TruncDamp, ns/TruncStart3.
-/
import ns.TruncDamp
import ns.TruncStart3

namespace TruncDamp3
open DetChain NSChain DyadicSmallGlobal LibRing DyadicLarge FourierGalerkin GalerkinForce ForceBound ForceSob
  ModeCount CubeCount ForceUniform NormCompare EnstStep EnstGlobal EnstGalerkin ForceTwoScale ForceBilin EnstDiff
  ForceDiff TruncError TruncBound RestrStep GronwallSource EnstGronwall WeightSplit LatticeSum TruncCauchy TruncSmall
  TruncStart TruncRate3 TruncStart3

section Gen
variable {R : Type} (K : CRing R) (O : OrdR K) {I : Type}
variable (inv : R → R) (mul_inv : ∀ y, y ≠ K.zero → K.mul y (inv y) = K.one) (h10 : K.one ≠ K.zero)

include mul_inv h10 in
theorem trunc_damp3 (l : List I) (kz : I → Mode) (hd : List.Pairwise (fun a b => kz a ≠ kz b) l)
    (h1 : ∀ p, 1 ≤ nrm (kz p)) (M : Nat) (hM : ∀ p, p ∈ l → nrm (kz p) ≤ M + 1) (Rc R' t0 : Nat) (hRM : R' ≤ M)
    (ι h ν Aa Bb iR : R) (hh : O.nn h) (hν : O.nn ν) (hν0 : ν ≠ K.zero) (hAa : O.nn Aa)
    (hiRn : O.nn iR) (hiR : K.mul (K.mul (castN K (Rc + 1)) (castN K (Rc + 1))) iR = K.one)
    (A B : Nat → I → V3 R)
    (hA : ∀ t r, Step K h ν (wt K kz r) (A (t + 1) r) (A t r) (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (A t) (A t) r))
    (hB : ∀ t r, Step K h ν (wt K kz r) (B (t + 1) r) (B t r)
      (Nl K (l.filter (lowB Rc kz)) (TriadSparse.delta K kz) ι (kv K kz) (B t) (B t) r))
    (smallA : Le K O.toCone (K.mul (K.mul (EnstH2.cH K) (K.mul ι ι)) (QH K ν h kz l A 0)) (K.mul ν (K.mul ν ν)))
    (hQA : Le K O.toCone (QH K ν h kz l A 0) (K.mul ν Aa))
    (small3 : Le K O.toCone (K.mul (K.mul (EnstH3.c3 K) (K.mul ι ι)) (QH3 K ν h kz l A 0)) (K.mul ν (K.mul ν ν)))
    (hQ3 : Le K O.toCone (QH3 K ν h kz l A 0) (K.mul ν Bb))
    (hB0 : B 0 = restr K (lowB Rc kz) (A 0))
    (hsm : Le K O.toCone (K.add (K.add (K.mul (K.mul (two' K) (K.mul (two' K) (two' K))) (K.mul (K.mul ι ι) (K.mul (C1' K inv R') Aa))) (K.mul (K.mul (two' K) (two' K)) (K.mul (K.mul ι ι) (K.mul (castN K 135) Aa))))
      (K.mul (K.mul (two' K) (K.mul (two' K) (two' K))) (K.mul (K.mul ι ι) (K.mul (C2' K inv R' t0) Aa)))) (K.mul ν ν)) (n : Nat) :
    Le K O.toCone (Q K ν h (fun t => Zl K kz (l.filter (lowB Rc kz)) (dF K (restr K (lowB Rc kz) (A t)) (B t)))
        (fun t => Al K kz (l.filter (lowB Rc kz)) (dF K (restr K (lowB Rc kz) (A t)) (B t))) n)
      (K.mul (castN K n) (K.mul h (K.mul (two' K) (K.add (K.mul (two' K) (K.add (K.mul (two' K) (K.mul (K.mul ι ι) (K.mul (K.mul Aa iR) (K.mul (C1' K inv R') Aa))))
          (K.mul (two' K) (K.mul (K.mul ι ι) (K.mul (K.mul Bb iR) (K.mul (C2' K inv R' t0) Aa))))))
        (K.mul (two' K) (K.mul (K.mul ι ι) (K.mul Aa (K.mul (castN K 135) (K.mul Aa iR))))))))) := by
  have q := qh_restr K O ν h hν hh kz l (lowB Rc kz) A B hB0
  have two : O.nn (K.add K.one K.one) := O.nn_add _ _ (RatModel.nn_one' K O.toCone) (RatModel.nn_one' K O.toCone)
  have three : O.nn (K.add (K.add K.one K.one) K.one) := O.nn_add _ _ two (RatModel.nn_one' K O.toCone)
  have n135 : O.nn (castN K 135) := RatModel.nn_cast K O.toCone 135
  have ncH : O.nn (EnstH2.cH K) := O.nn_add _ _ (O.nn_mul _ _ two (O.nn_mul _ _ three n135)) (O.nn_mul _ _ two n135)
  have smallB := le_trans' K O.toCone _ _ _ (le_mul_nn K O.toCone _ _ _ (O.nn_mul _ _ ncH (O.nn_sq ι)) q) smallA
  have bA := a_bound K O inv mul_inv h10 l kz hd h1 M hM ι h ν Aa hh hν hν0 A hA smallA hQA
  have bB := a_bound K O inv mul_inv h10 (l.filter (lowB Rc kz)) kz (pw_filter kz (lowB Rc kz) l hd) h1 M
    (fun p hp => hM p (mem_of_mem_filter (lowB Rc kz) p l hp)) ι h ν Aa hh hν hν0 B hB smallB (le_trans' K O.toCone _ _ _ q hQA)
  have b3 := b_bound K O inv mul_inv h10 l kz hd h1 M hM ι h ν Bb hh hν hν0 A hA small3 hQ3
  have n2 : O.nn (two' K) := two
  have hiv : O.nn (iv K inv (R' + 1)) := iv_nn K O inv mul_inv h10 R'
  have nC1 : O.nn (C1' K inv R') := O.nn_add _ _ (O.nn_mul _ _ (RatModel.nn_cast K O.toCone _) (RatModel.nn_one' K O.toCone))
    (O.nn_mul _ _ (RatModel.nn_cast K O.toCone _) hiv)
  have nC2 : O.nn (C2' K inv R' t0) := O.nn_add _ _ (O.nn_mul _ _ (RatModel.nn_cast K O.toCone _) (O.nn_sq _))
    (O.nn_mul _ _ (RatModel.nn_cast K O.toCone _) hiv)
  have nii : O.nn (K.mul ι ι) := O.nn_sq ι
  have hw : ∀ r, O.nn (wt K kz r) := fun r => dot3_nn K O.toCone _
  have hZn : ∀ (L : List I) (u : I → V3 R), O.nn (Zl K kz L u) := fun L u =>
    lsum_nn K O.toCone _ (fun r => O.nn_mul _ _ (hw r) (dot3_nn K O.toCone _)) L
  have hAn : ∀ (L : List I) (u : I → V3 R), O.nn (Al K kz L u) := fun L u =>
    lsum_nn K O.toCone _ (fun r => O.nn_mul _ _ (O.nn_sq _) (dot3_nn K O.toCone _)) L
  have hZa : ∀ t, Le K O.toCone (Zl K kz (l.filter (lowB Rc kz)) (restr K (lowB Rc kz) (A t))) Aa := fun t =>
    le_trans' K O.toCone _ _ _ (TruncRate.Z_restr_le K O l kz (lowB Rc kz) (A t)) (bA t).2
  have hZh : ∀ t, Le K O.toCone (Zl K kz l (hiF K (lowB Rc kz) (A t))) (K.mul Aa iR) := fun t =>
    TruncRate.Zh_le K O l kz Rc (A t) Aa (bA t).1 iR hiR hiRn
  have hAh : ∀ t, Le K O.toCone (Al K kz l (hiF K (lowB Rc kz) (A t))) (K.mul Bb iR) := fun t =>
    Ah_le K O l kz Rc (A t) Bb (b3 t) iR hiR hiRn
  have nZh : O.nn (K.mul Aa iR) := O.nn_mul _ _ hAa hiRn
  have hab : ∀ t, Le K O.toCone (K.add (alph K inv kz (l.filter (lowB Rc kz)) R' ι (restr K (lowB Rc kz) (A t)) (B t))
      (bet K inv kz (l.filter (lowB Rc kz)) R' t0 ι (restr K (lowB Rc kz) (A t)))) (K.mul ν ν) := fun t =>
    le_trans' K O.toCone _ _ _ (le_add' K O.toCone _ _ _ _
      (le_add' K O.toCone _ _ _ _
        (le_mul_nn K O.toCone _ _ _ (O.nn_mul _ _ n2 (O.nn_mul _ _ n2 n2)) (le_mul_nn K O.toCone _ _ _ nii
          (le_mul_nn K O.toCone _ _ _ nC1 (hZa t))))
        (le_mul_nn K O.toCone _ _ _ (O.nn_mul _ _ n2 n2) (le_mul_nn K O.toCone _ _ _ nii
          (le_mul_nn K O.toCone _ _ _ n135 (bB t).1))))
      (le_mul_nn K O.toCone _ _ _ (O.nn_mul _ _ n2 (O.nn_mul _ _ n2 n2))
        (le_mul_nn K O.toCone _ _ _ nii (le_mul_nn K O.toCone _ _ _ nC2 (hZa t))))) hsm
  have hS : ∀ t, Le K O.toCone (K.mul (two' K) (EB K inv kz l Rc R' t0 ι (A t))) (K.mul (two' K) (K.add (K.mul (two' K) (K.add (K.mul (two' K) (K.mul (K.mul ι ι) (K.mul (K.mul Aa iR) (K.mul (C1' K inv R') Aa))))
          (K.mul (two' K) (K.mul (K.mul ι ι) (K.mul (K.mul Bb iR) (K.mul (C2' K inv R' t0) Aa))))))
        (K.mul (two' K) (K.mul (K.mul ι ι) (K.mul Aa (K.mul (castN K 135) (K.mul Aa iR))))))) := by
    intro t
    refine le_mul_nn K O.toCone _ _ _ n2 (le_add' K O.toCone _ _ _ _
      (le_mul_nn K O.toCone _ _ _ n2 (le_add' K O.toCone _ _ _ _
        (le_mul_nn K O.toCone _ _ _ n2 (le_mul_nn K O.toCone _ _ _ nii
          (TruncRate.mm K O _ _ _ _ (hZn _ _) (O.nn_mul _ _ nC1 hAa) (hZh t) (le_mul_nn K O.toCone _ _ _ nC1 (bA t).2))))
        (le_mul_nn K O.toCone _ _ _ n2 (le_mul_nn K O.toCone _ _ _ nii
          (TruncRate.mm K O _ _ _ _ (hAn _ _) (O.nn_mul _ _ nC2 hAa) (hAh t) (le_mul_nn K O.toCone _ _ _ nC2 (bA t).2))))))
      (le_mul_nn K O.toCone _ _ _ n2 (le_mul_nn K O.toCone _ _ _ nii
        (TruncRate.mm K O _ _ _ _ (hAn _ _) (O.nn_mul _ _ n135 nZh) (bA t).1 (le_mul_nn K O.toCone _ _ _ n135 (hZh t))))))
  have t := TruncDamp.trunc_damp K O inv mul_inv h10 l kz hd h1 M hM Rc R' t0 hRM ι h ν _ hh hν A B hA
    (fun t r _ => hB t r) hab hS n
  rw [qd_zero K ν h kz (l.filter (lowB Rc kz)) (lowB Rc kz) A B hB0, zero_add' K] at t
  exact t

end Gen
end TruncDamp3

#print axioms TruncDamp3.trunc_damp3

ns/TruncDampSum.lean

lean_all/ns/TruncDampSum.lean

/-
  ns/TruncDampSum.lean — T37 cycle 1818 (goal G857d, piece 2): the truncation comparison with a time-dependent source.
  As TruncDamp3 but with the high-mode masses bounded by the fine scheme's own norms at time t,
      Z(hi_R A_t) ≤ A(A_t)/(R+1)²,   A(hi_R A_t) ≤ B(A_t)/(R+1)²,
  so the source is S_t = 2[2(2ι²(A(A_t) iR)C₁Aa + 2ι²(B(A_t) iR)C₂Aa) + 2ι²Aa·135·A(A_t) iR] and (DampSum.damp_sum)
    trunc_damp_sum   Q_d(n) ≤ Σ_{t<n} h·S_t
  — every term is iR times a time-integrable norm (Σ_t h·A(A_t), Σ_t h·B(A_t) are bounded by EnstDiss), so the
  bound is uniform in n once those sums are bounded (piece 3). No H³ hypothesis is needed here.
  Lean 4 core plus ns/TruncDamp3, ns/DampSum.
-/
import ns.TruncDamp3
import ns.DampSum

namespace TruncDampSum
open DetChain NSChain DyadicSmallGlobal LibRing DyadicLarge FourierGalerkin GalerkinForce ForceBound ForceSob
  ModeCount CubeCount ForceUniform NormCompare EnstStep EnstGlobal EnstGalerkin ForceTwoScale ForceBilin EnstDiff
  ForceDiff TruncError TruncBound RestrStep GronwallSource EnstGronwall WeightSplit LatticeSum TruncCauchy TruncSmall
  TruncStart TruncRate3 TruncStart3 SchemeTele

section Gen
variable {R : Type} (K : CRing R) (O : OrdR K) {I : Type}
variable (inv : R → R) (mul_inv : ∀ y, y ≠ K.zero → K.mul y (inv y) = K.one) (h10 : K.one ≠ K.zero)

def St (kz : I → Mode) (l : List I) (R' t0 : Nat) (ι Aa iR : R) (A : Nat → I → V3 R) (t : Nat) : R :=
  K.mul (two' K) (K.add (K.mul (two' K) (K.add (K.mul (two' K) (K.mul (K.mul ι ι) (K.mul (K.mul (Al K kz l (A t)) iR) (K.mul (C1' K inv R') Aa))))
      (K.mul (two' K) (K.mul (K.mul ι ι) (K.mul (K.mul (Bl K kz l (A t)) iR) (K.mul (C2' K inv R' t0) Aa))))))
    (K.mul (two' K) (K.mul (K.mul ι ι) (K.mul Aa (K.mul (castN K 135) (K.mul (Al K kz l (A t)) iR))))))

include mul_inv h10 in
theorem trunc_damp_sum (l : List I) (kz : I → Mode) (hd : List.Pairwise (fun a b => kz a ≠ kz b) l)
    (h1 : ∀ p, 1 ≤ nrm (kz p)) (M : Nat) (hM : ∀ p, p ∈ l → nrm (kz p) ≤ M + 1) (Rc R' t0 : Nat) (hRM : R' ≤ M)
    (ι h ν Aa iR : R) (hh : O.nn h) (hν : O.nn ν) (hν0 : ν ≠ K.zero) (hAa : O.nn Aa)
    (hiRn : O.nn iR) (hiR : K.mul (K.mul (castN K (Rc + 1)) (castN K (Rc + 1))) iR = K.one)
    (A B : Nat → I → V3 R)
    (hA : ∀ t r, Step K h ν (wt K kz r) (A (t + 1) r) (A t r) (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (A t) (A t) r))
    (hB : ∀ t r, Step K h ν (wt K kz r) (B (t + 1) r) (B t r)
      (Nl K (l.filter (lowB Rc kz)) (TriadSparse.delta K kz) ι (kv K kz) (B t) (B t) r))
    (smallA : Le K O.toCone (K.mul (K.mul (EnstH2.cH K) (K.mul ι ι)) (QH K ν h kz l A 0)) (K.mul ν (K.mul ν ν)))
    (hQA : Le K O.toCone (QH K ν h kz l A 0) (K.mul ν Aa))
    (hB0 : B 0 = restr K (lowB Rc kz) (A 0))
    (hsm : Le K O.toCone (K.add (K.add (K.mul (K.mul (two' K) (K.mul (two' K) (two' K))) (K.mul (K.mul ι ι) (K.mul (C1' K inv R') Aa))) (K.mul (K.mul (two' K) (two' K)) (K.mul (K.mul ι ι) (K.mul (castN K 135) Aa))))
      (K.mul (K.mul (two' K) (K.mul (two' K) (two' K))) (K.mul (K.mul ι ι) (K.mul (C2' K inv R' t0) Aa)))) (K.mul ν ν)) (n : Nat) :
    Le K O.toCone (Q K ν h (fun t => Zl K kz (l.filter (lowB Rc kz)) (dF K (restr K (lowB Rc kz) (A t)) (B t)))
        (fun t => Al K kz (l.filter (lowB Rc kz)) (dF K (restr K (lowB Rc kz) (A t)) (B t))) n)
      (tsum K (fun t => K.mul h (St K inv kz l R' t0 ι Aa iR A t)) n) := by
  have q := qh_restr K O ν h hν hh kz l (lowB Rc kz) A B hB0
  have two : O.nn (K.add K.one K.one) := O.nn_add _ _ (RatModel.nn_one' K O.toCone) (RatModel.nn_one' K O.toCone)
  have three : O.nn (K.add (K.add K.one K.one) K.one) := O.nn_add _ _ two (RatModel.nn_one' K O.toCone)
  have n135 : O.nn (castN K 135) := RatModel.nn_cast K O.toCone 135
  have ncH : O.nn (EnstH2.cH K) := O.nn_add _ _ (O.nn_mul _ _ two (O.nn_mul _ _ three n135)) (O.nn_mul _ _ two n135)
  have smallB := le_trans' K O.toCone _ _ _ (le_mul_nn K O.toCone _ _ _ (O.nn_mul _ _ ncH (O.nn_sq ι)) q) smallA
  have bA := a_bound K O inv mul_inv h10 l kz hd h1 M hM ι h ν Aa hh hν hν0 A hA smallA hQA
  have bB := a_bound K O inv mul_inv h10 (l.filter (lowB Rc kz)) kz (pw_filter kz (lowB Rc kz) l hd) h1 M
    (fun p hp => hM p (mem_of_mem_filter (lowB Rc kz) p l hp)) ι h ν Aa hh hν hν0 B hB smallB (le_trans' K O.toCone _ _ _ q hQA)
  have n2 : O.nn (two' K) := two
  have hiv : O.nn (iv K inv (R' + 1)) := iv_nn K O inv mul_inv h10 R'
  have nC1 : O.nn (C1' K inv R') := O.nn_add _ _ (O.nn_mul _ _ (RatModel.nn_cast K O.toCone _) (RatModel.nn_one' K O.toCone))
    (O.nn_mul _ _ (RatModel.nn_cast K O.toCone _) hiv)
  have nC2 : O.nn (C2' K inv R' t0) := O.nn_add _ _ (O.nn_mul _ _ (RatModel.nn_cast K O.toCone _) (O.nn_sq _))
    (O.nn_mul _ _ (RatModel.nn_cast K O.toCone _) hiv)
  have nii : O.nn (K.mul ι ι) := O.nn_sq ι
  have hw : ∀ r, O.nn (wt K kz r) := fun r => dot3_nn K O.toCone _
  have hZn : ∀ (L : List I) (u : I → V3 R), O.nn (Zl K kz L u) := fun L u =>
    lsum_nn K O.toCone _ (fun r => O.nn_mul _ _ (hw r) (dot3_nn K O.toCone _)) L
  have hAn : ∀ (L : List I) (u : I → V3 R), O.nn (Al K kz L u) := fun L u =>
    lsum_nn K O.toCone _ (fun r => O.nn_mul _ _ (O.nn_sq _) (dot3_nn K O.toCone _)) L
  have hZa : ∀ t, Le K O.toCone (Zl K kz (l.filter (lowB Rc kz)) (restr K (lowB Rc kz) (A t))) Aa := fun t =>
    le_trans' K O.toCone _ _ _ (TruncRate.Z_restr_le K O l kz (lowB Rc kz) (A t)) (bA t).2
  have hZh : ∀ t, Le K O.toCone (Zl K kz l (hiF K (lowB Rc kz) (A t))) (K.mul (Al K kz l (A t)) iR) := fun t =>
    TruncRate.Zh_le K O l kz Rc (A t) (Al K kz l (A t)) (le_refl' K O.toCone _) iR hiR hiRn
  have hAh : ∀ t, Le K O.toCone (Al K kz l (hiF K (lowB Rc kz) (A t))) (K.mul (Bl K kz l (A t)) iR) := fun t =>
    Ah_le K O l kz Rc (A t) (Bl K kz l (A t)) (le_refl' K O.toCone _) iR hiR hiRn
  have nZh : ∀ t, O.nn (K.mul (Al K kz l (A t)) iR) := fun t => O.nn_mul _ _ (hAn _ _) hiRn
  have hab : ∀ t, Le K O.toCone (K.add (alph K inv kz (l.filter (lowB Rc kz)) R' ι (restr K (lowB Rc kz) (A t)) (B t))
      (bet K inv kz (l.filter (lowB Rc kz)) R' t0 ι (restr K (lowB Rc kz) (A t)))) (K.mul ν ν) := fun t =>
    le_trans' K O.toCone _ _ _ (le_add' K O.toCone _ _ _ _
      (le_add' K O.toCone _ _ _ _
        (le_mul_nn K O.toCone _ _ _ (O.nn_mul _ _ n2 (O.nn_mul _ _ n2 n2)) (le_mul_nn K O.toCone _ _ _ nii
          (le_mul_nn K O.toCone _ _ _ nC1 (hZa t))))
        (le_mul_nn K O.toCone _ _ _ (O.nn_mul _ _ n2 n2) (le_mul_nn K O.toCone _ _ _ nii
          (le_mul_nn K O.toCone _ _ _ n135 (bB t).1))))
      (le_mul_nn K O.toCone _ _ _ (O.nn_mul _ _ n2 (O.nn_mul _ _ n2 n2))
        (le_mul_nn K O.toCone _ _ _ nii (le_mul_nn K O.toCone _ _ _ nC2 (hZa t))))) hsm
  have hS : ∀ t, Le K O.toCone (K.mul (two' K) (EB K inv kz l Rc R' t0 ι (A t))) (St K inv kz l R' t0 ι Aa iR A t) := by
    intro t
    refine le_mul_nn K O.toCone _ _ _ n2 (le_add' K O.toCone _ _ _ _
      (le_mul_nn K O.toCone _ _ _ n2 (le_add' K O.toCone _ _ _ _
        (le_mul_nn K O.toCone _ _ _ n2 (le_mul_nn K O.toCone _ _ _ nii
          (TruncRate.mm K O _ _ _ _ (hZn _ _) (O.nn_mul _ _ nC1 hAa) (hZh t) (le_mul_nn K O.toCone _ _ _ nC1 (bA t).2))))
        (le_mul_nn K O.toCone _ _ _ n2 (le_mul_nn K O.toCone _ _ _ nii
          (TruncRate.mm K O _ _ _ _ (hAn _ _) (O.nn_mul _ _ nC2 hAa) (hAh t) (le_mul_nn K O.toCone _ _ _ nC2 (bA t).2))))))
      (le_mul_nn K O.toCone _ _ _ n2 (le_mul_nn K O.toCone _ _ _ nii
        (TruncRate.mm K O _ _ _ _ (hAn _ _) (O.nn_mul _ _ n135 (nZh t)) (bA t).1 (le_mul_nn K O.toCone _ _ _ n135 (hZh t))))))
  have t := DampSum.damp_sum K O ν h
    (fun t => Zl K kz (l.filter (lowB Rc kz)) (dF K (restr K (lowB Rc kz) (A t)) (B t)))
    (fun t => Al K kz (l.filter (lowB Rc kz)) (dF K (restr K (lowB Rc kz) (A t)) (B t)))
    (fun t => alph K inv kz (l.filter (lowB Rc kz)) R' ι (restr K (lowB Rc kz) (A t)) (B t))
    (fun t => bet K inv kz (l.filter (lowB Rc kz)) R' t0 ι (restr K (lowB Rc kz) (A t)))
    (fun t => St K inv kz l R' t0 ι Aa iR A t) hh
    (fun t => O.nn_add _ _ (O.nn_mul _ _ (O.nn_mul _ _ n2 (O.nn_mul _ _ n2 n2)) (O.nn_mul _ _ nii (O.nn_mul _ _ nC1 (hZn _ _))))
      (O.nn_mul _ _ (O.nn_mul _ _ n2 n2) (O.nn_mul _ _ nii (O.nn_mul _ _ n135 (hAn _ _)))))
    (fun t => hAn _ _) (fun t => EnstH2.Z_le_A K O kz h1 _ _) hab
    (fun s => le_trans' K O.toCone _ _ _
      (TruncStep.trunc_step K O inv mul_inv h10 l kz hd h1 M hM Rc R' t0 hRM ι h ν hh hν A B hA (fun t r _ => hB t r) s)
      (le_add' K O.toCone _ _ _ _ (le_refl' K O.toCone _) (le_mul_nn K O.toCone h _ _ hh (hS s)))) n
  rw [qd_zero K ν h kz (l.filter (lowB Rc kz)) (lowB Rc kz) A B hB0, zero_add' K] at t
  exact t

end Gen
end TruncDampSum

#print axioms TruncDampSum.trunc_damp_sum

ns/TruncDefect.lean

lean_all/ns/TruncDefect.lean

/-
  ns/TruncDefect.lean — T37 cycle 1768 (goal G852g, piece c): a coarse Galerkin scheme a (forces Nl(a_t)) against any
  scheme b on the same modes whose forces N^b_t miss the Galerkin nonlinearity by D_t = Σ_r |Nl(b_t)_r − N^b_t r|²
  (b = the low-mode part of a finer truncation; D_t is its truncation error, TruncBound.err_bound). With the doubled
  EnstLipschitz rates α_t, β_t of ForcedDiffStep, β_t ≤ ν², ν + hα_t ≤ νg, 1 ≤ g and D_t ≤ Dm:
      Q_n(a − b) ≤ gⁿ·(Q_0(a − b) + n·h·2Dm)
  — the error between truncations is controlled by the truncation defect alone (a Cauchy estimate in the truncation
  once Dm → 0). Lean 4 core plus ns/ForcedDiffStep, ns/GronwallSrc, ns/GlobalHolder (tri_sq).
-/
import ns.ForcedDiffStep
import ns.GronwallSrc
import ns.GlobalHolder

namespace TruncDefect
open DetChain NSChain DyadicSmallGlobal LibRing DyadicLarge FourierGalerkin GalerkinForce ForceBound ForceSob
  ModeCount CubeCount ForceUniform NormCompare EnstStep EnstGlobal EnstGalerkin ForceTwoScale ForceBilin EnstDiff
  ForceDiff EnstGronwall LatticeSum EnstLipschitz

section Gen
variable {R : Type} (K : CRing R) (O : OrdR K) {I : Type}
variable (inv : R → R) (mul_inv : ∀ y, y ≠ K.zero → K.mul y (inv y) = K.one) (h10 : K.one ≠ K.zero)

def Dt (l : List I) (kz : I → Mode) (ι : R) (b Nb : Nat → I → V3 R) (t : Nat) : R :=
  lsum K (fun r => dot3 K (EnstDiff.sub3 K (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (b t) (b t) r) (Nb t r))
    (EnstDiff.sub3 K (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (b t) (b t) r) (Nb t r))) l

def alphaT (ι : R) (Rc : Nat) (l : List I) (kz : I → Mode) (a b : Nat → I → V3 R) (t : Nat) : R :=
  K.mul (two K) (K.add (K.mul (two K) (K.mul (K.mul (two K) (K.mul ι ι)) (K.mul (C1 K inv Rc) (Zs K l kz a t))))
    (K.mul (two K) (K.mul (K.mul ι ι) (K.mul (castN K 135) (As K l kz b t)))))

def betaT (ι : R) (Rc t0 : Nat) (l : List I) (kz : I → Mode) (a : Nat → I → V3 R) (t : Nat) : R :=
  K.mul (two K) (K.mul (two K) (K.mul (K.mul (two K) (K.mul ι ι)) (K.mul (C2 K inv Rc t0) (Zs K l kz a t))))

include mul_inv h10 in
theorem trunc_defect (l : List I) (kz : I → Mode) (hd : List.Pairwise (fun a b => kz a ≠ kz b) l)
    (h1 : ∀ p, 1 ≤ nrm (kz p)) (M : Nat) (hM : ∀ p, p ∈ l → nrm (kz p) ≤ M + 1) (Rc t0 : Nat) (hRM : Rc ≤ M)
    (ι h ν g Dm : R) (hh : O.nn h) (hν : O.nn ν) (hν0 : ν ≠ K.zero) (a b Nb : Nat → I → V3 R)
    (ha : ∀ t r, Step K h ν (wt K kz r) (a (t + 1) r) (a t r) (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (a t) (a t) r))
    (hb : ∀ t r, Step K h ν (wt K kz r) (b (t + 1) r) (b t r) (Nb t r))
    (hβ : ∀ t, Le K O.toCone (betaT K inv ι Rc t0 l kz a t) (K.mul ν ν))
    (hg : Le K O.toCone K.one g) (hαg : ∀ t, Le K O.toCone (K.add ν (K.mul h (alphaT K inv ι Rc l kz a b t))) (K.mul ν g))
    (hD : ∀ t, Le K O.toCone (Dt K l kz ι b Nb t) Dm) (hDm : O.nn Dm) :
    ∀ n, Le K O.toCone (Q K ν h (Zs K l kz (dS K a b)) (As K l kz (dS K a b)) n)
      (K.mul (pwr K g n) (K.add (Q K ν h (Zs K l kz (dS K a b)) (As K l kz (dS K a b)) 0)
        (K.mul (castN K n) (K.mul h (K.mul (two K) Dm))))) := by
  have hw : ∀ r, O.nn (wt K kz r) := fun r => dot3_nn K O.toCone _
  have n2 : O.nn (two K) := O.nn_add _ _ (RatModel.nn_one' K O.toCone) (RatModel.nn_one' K O.toCone)
  have hZn : ∀ (u : Nat → I → V3 R) t, O.nn (Zs K l kz u t) := fun u t =>
    lsum_nn K O.toCone _ (fun r => O.nn_mul _ _ (hw r) (dot3_nn K O.toCone _)) l
  have hAn : ∀ (u : Nat → I → V3 R) t, O.nn (As K l kz u t) := fun u t =>
    lsum_nn K O.toCone _ (fun r => O.nn_mul _ _ (O.nn_sq _) (dot3_nn K O.toCone _)) l
  have hiv : O.nn (iv K inv (Rc + 1)) := iv_nn K O inv mul_inv h10 Rc
  have nC1 : O.nn (C1 K inv Rc) := O.nn_add _ _ (O.nn_mul _ _ (RatModel.nn_cast K O.toCone _) (RatModel.nn_one' K O.toCone))
    (O.nn_mul _ _ (RatModel.nn_cast K O.toCone _) hiv)
  have hα : ∀ t, O.nn (alphaT K inv ι Rc l kz a b t) := fun t => O.nn_mul _ _ n2 (O.nn_add _ _
    (O.nn_mul _ _ n2 (O.nn_mul _ _ (O.nn_mul _ _ n2 (O.nn_sq ι)) (O.nn_mul _ _ nC1 (hZn a t))))
    (O.nn_mul _ _ n2 (O.nn_mul _ _ (O.nn_sq ι) (O.nn_mul _ _ (RatModel.nn_cast K O.toCone 135) (hAn b t)))))
  have hN : ∀ t, Le K O.toCone
      (lsum K (fun r => dot3 K (sub3 K (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (a t) (a t) r) (Nb t r))
        (sub3 K (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (a t) (a t) r) (Nb t r))) l)
      (K.add (K.mul (two K) (lsum K (fun r => dot3 K (sub3 K (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (a t) (a t) r)
          (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (b t) (b t) r))
        (sub3 K (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (a t) (a t) r)
          (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (b t) (b t) r))) l)) (K.mul (two K) (Dt K l kz ι b Nb t))) :=
    fun t => GlobalHolder.tri_sq K O l (fun r => Nl K l (TriadSparse.delta K kz) ι (kv K kz) (a t) (a t) r)
      (fun r => Nl K l (TriadSparse.delta K kz) ι (kv K kz) (b t) (b t) r) (Nb t)
  exact GronwallSrc.gronwall_src K O ν h g (K.mul (two K) Dm) _ _ (alphaT K inv ι Rc l kz a b)
    (betaT K inv ι Rc t0 l kz a) (fun t => K.mul (two K) (Dt K l kz ι b Nb t)) hν hν0 hh (hZn _) (hAn _) hα hβ hg hαg
    (fun t => le_mul_nn K O.toCone (two K) _ _ n2 (hD t)) (O.nn_mul _ _ n2 hDm)
    (fun s => ForcedDiffStep.forced_step K O inv mul_inv h10 l kz hd h1 M hM Rc t0 hRM ι h ν hh hν a b
      (fun t r => Nl K l (TriadSparse.delta K kz) ι (kv K kz) (a t) (a t) r) Nb (Dt K l kz ι b Nb) hN ha hb s)

end Gen
end TruncDefect

#print axioms TruncDefect.trunc_defect

ns/TruncEps.lean

lean_all/ns/TruncEps.lean

/-
  ns/TruncEps.lean — T37 cycle 1782 (goal G852k): an explicit truncation rate. In TruncStart3 the source is
  S = iR·S₁ with S₁ independent of R (iR = 1/(R+1)²), so
    eps_le     cm·cj ≤ cR², cR²·iR = 1, cj·ε = 1  ⇒  cm·iR ≤ ε
    sq_ge      m(j+1) ≤ (m(j+1) + 1)²   in the ring
    trunc_eps  if gr^T·T·h·S₁ ≤ m (a natural bound, gr = ν + hα) then the cutoff R = m(j+1) gives
               ν^T·Q_d(T) ≤ 1/(j+1)
  for every fine truncation l: the coarse Galerkin truncations of small H²/H³ data are Cauchy with an explicit
  modulus (no extraction, no choice).
  Lean 4 core plus ns/TruncStart3, ns/LatticeSum (castN_mul).
-/
import ns.TruncStart3

namespace TruncEps
open DetChain NSChain DyadicSmallGlobal LibRing DyadicLarge FourierGalerkin GalerkinForce ForceBound ForceSob
  ModeCount CubeCount ForceUniform NormCompare EnstStep EnstGlobal EnstGalerkin ForceTwoScale ForceBilin EnstDiff
  ForceDiff TruncError TruncBound RestrStep GronwallSource EnstGronwall WeightSplit LatticeSum TruncCauchy TruncSmall
  TruncStart TruncRate3 TruncStart3

section Gen
variable {R : Type} (K : CRing R) (O : OrdR K) {I : Type}
variable (inv : R → R) (mul_inv : ∀ y, y ≠ K.zero → K.mul y (inv y) = K.one) (h10 : K.one ≠ K.zero)

theorem eps_le (cm cj cR iR eps : R) (hiRn : O.nn iR) (hepsn : O.nn eps) (hiR : K.mul (K.mul cR cR) iR = K.one)
    (heps : K.mul cj eps = K.one) (h : Le K O.toCone (K.mul cm cj) (K.mul cR cR)) :
    Le K O.toCone (K.mul cm iR) eps := by
  have m := ForceSob.le_mul_right' K O.toCone h (O.nn_mul _ _ hiRn hepsn)
  have e1 : K.mul (K.mul cm cj) (K.mul iR eps) = K.mul (K.mul cm iR) (K.mul cj eps) := by lib_ring
  have e2 : K.mul (K.mul cR cR) (K.mul iR eps) = K.mul (K.mul (K.mul cR cR) iR) eps := by lib_ring
  rw [e1, heps, e2, hiR] at m
  have e3 : K.mul (K.mul cm iR) K.one = K.mul cm iR := by lib_ring
  have e4 : K.mul K.one eps = eps := by lib_ring
  rw [e3, e4] at m
  exact m

theorem sq_ge (m j : Nat) :
    Le K O.toCone (K.mul (castN K m) (castN K (j + 1)))
      (K.mul (castN K (m * (j + 1) + 1)) (castN K (m * (j + 1) + 1))) := by
  rw [← castN_mul K m (j + 1)]
  generalize m * (j + 1) = x
  show Le K O.toCone (castN K x) (K.mul (K.add K.one (castN K x)) (K.add K.one (castN K x)))
  have hx : O.nn (castN K x) := RatModel.nn_cast K O.toCone x
  refine ⟨K.add (K.mul (castN K x) (castN K x)) (K.add (castN K x) K.one),
    O.nn_add _ _ (O.nn_sq _) (O.nn_add _ _ hx (RatModel.nn_one' K O.toCone)), ?_⟩
  generalize castN K x = X
  lib_ring

include mul_inv h10 in
theorem trunc_eps (l : List I) (kz : I → Mode) (hd : List.Pairwise (fun a b => kz a ≠ kz b) l)
    (h1 : ∀ p, 1 ≤ nrm (kz p)) (M : Nat) (hM : ∀ p, p ∈ l → nrm (kz p) ≤ M + 1) (Rc R' t0 : Nat) (hRM : R' ≤ M)
    (ι h ν Aa Bb iR : R) (hh : O.nn h) (hν : O.nn ν) (hν0 : ν ≠ K.zero) (hAa : O.nn Aa) (hBb : O.nn Bb)
    (hiRn : O.nn iR) (hiR : K.mul (K.mul (castN K (Rc + 1)) (castN K (Rc + 1))) iR = K.one)
    (A B : Nat → I → V3 R)
    (hA : ∀ t r, Step K h ν (wt K kz r) (A (t + 1) r) (A t r) (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (A t) (A t) r))
    (hB : ∀ t r, Step K h ν (wt K kz r) (B (t + 1) r) (B t r)
      (Nl K (l.filter (lowB Rc kz)) (TriadSparse.delta K kz) ι (kv K kz) (B t) (B t) r))
    (smallA : Le K O.toCone (K.mul (K.mul (EnstH2.cH K) (K.mul ι ι)) (QH K ν h kz l A 0)) (K.mul ν (K.mul ν ν)))
    (hQA : Le K O.toCone (QH K ν h kz l A 0) (K.mul ν Aa))
    (small3 : Le K O.toCone (K.mul (K.mul (EnstH3.c3 K) (K.mul ι ι)) (QH3 K ν h kz l A 0)) (K.mul ν (K.mul ν ν)))
    (hQ3 : Le K O.toCone (QH3 K ν h kz l A 0) (K.mul ν Bb))
    (gr S1 : R) (hgr : gr = (K.add ν (K.mul h
          (K.add (K.mul (K.mul (two' K) (K.mul (two' K) (two' K))) (K.mul (K.mul ι ι) (K.mul (C1' K inv R') Aa)))
            (K.mul (K.mul (two' K) (two' K)) (K.mul (K.mul ι ι) (K.mul (castN K 135) Aa)))))))
    (hS1 : S1 = (K.mul (two' K)
            (K.add
              (K.mul (two' K) (K.add
                (K.mul (two' K) (K.mul (K.mul ι ι) (K.mul Aa (K.mul (C1' K inv R') Aa))))
                (K.mul (two' K) (K.mul (K.mul ι ι) (K.mul Bb (K.mul (C2' K inv R' t0) Aa))))))
              (K.mul (two' K) (K.mul (K.mul ι ι) (K.mul Aa (K.mul (castN K 135) Aa)))))))
    (hB0 : B 0 = restr K (lowB Rc kz) (A 0))
    (hβc : Le K O.toCone (K.mul (K.mul (two' K) (K.mul (two' K) (two' K))) (K.mul (K.mul ι ι) (K.mul (C2' K inv R' t0) Aa)))
      (K.mul ν ν)) (T : Nat)
    (m jj : Nat) (eps : R) (hepsn : O.nn eps) (heps : K.mul (castN K (jj + 1)) eps = K.one) (hRc : Rc = m * (jj + 1))
    (hG : Le K O.toCone (K.mul (pwr K gr T) (K.mul (castN K T) (K.mul h S1))) (castN K m)) :
    Le K O.toCone (K.mul (pwr K ν T)
        (Q K ν h (fun t => Zl K kz (l.filter (lowB Rc kz)) (dF K (restr K (lowB Rc kz) (A t)) (B t)))
          (fun t => Al K kz (l.filter (lowB Rc kz)) (dF K (restr K (lowB Rc kz) (A t)) (B t))) T))
      eps := by
  subst hRc
  have t := trunc_start3 K O inv mul_inv h10 l kz hd h1 M hM (m * (jj + 1)) R' t0 hRM ι h ν Aa Bb iR hh hν hν0 hAa hBb
    hiRn hiR A B hA hB smallA hQA small3 hQ3 hB0 hβc T
  have e : (K.mul (pwr K (K.add ν (K.mul h
          (K.add (K.mul (K.mul (two' K) (K.mul (two' K) (two' K))) (K.mul (K.mul ι ι) (K.mul (C1' K inv R') Aa)))
            (K.mul (K.mul (two' K) (two' K)) (K.mul (K.mul ι ι) (K.mul (castN K 135) Aa)))))) T)
        (K.mul (castN K T) (K.mul h (K.mul (two' K)
            (K.add
              (K.mul (two' K) (K.add
                (K.mul (two' K) (K.mul (K.mul ι ι) (K.mul (K.mul Aa iR) (K.mul (C1' K inv R') Aa))))
                (K.mul (two' K) (K.mul (K.mul ι ι) (K.mul (K.mul Bb iR) (K.mul (C2' K inv R' t0) Aa))))))
              (K.mul (two' K) (K.mul (K.mul ι ι) (K.mul Aa (K.mul (castN K 135) (K.mul Aa iR))))))))))
      = K.mul (K.mul (pwr K gr T) (K.mul (castN K T) (K.mul h S1))) iR := by
    rw [hgr, hS1]
    lib_ring
  rw [e] at t
  have g2 := ForceSob.le_mul_right' K O.toCone hG hiRn
  exact le_trans' K O.toCone _ _ _ t (le_trans' K O.toCone _ _ _ g2
    (eps_le K O (castN K m) (castN K (jj + 1)) (castN K (m * (jj + 1) + 1)) iR eps hiRn hepsn hiR heps (sq_ge K O m jj)))

end Gen
end TruncEps

#print axioms TruncEps.eps_le
#print axioms TruncEps.sq_ge
#print axioms TruncEps.trunc_eps

ns/TruncError.lean

lean_all/ns/TruncError.lean

/-
  ns/TruncError.lean — T37 cycle 1039 (goal G683, piece a): the truncation error of the Galerkin force. For a mode list
  lM, a sub-truncation given by a Boolean predicate inN (lN = lM.filter inN) and fields u, v:
    restr_eq   Nl over lN of (u, v) = Nl over lM of (u|_N, v|_N)
    trunc_err  Nl_lM(a, a) − Nl_lN(a, a) = Nl_lM(a|_H, a) + Nl_lM(a|_N, a|_H)      (H = complement of N)
  So the restriction of a solution of the lM-scheme to the modes of lN solves the lN-scheme up to the error
  E = N(a_H, a) + N(a_N, a_H), whose energy ForceTwoScale / ForceUniform bound by the high-mode part a_H.

  Lean 4 core plus ns/ForceDiff (sub3, a_split), ns/ForceBilin (Nl_add_left/right, v3_ext), ns/ForceLip (Nl_congr).
-/
import ns.ForceDiff

namespace TruncError
open DetChain NSChain LibRing DyadicLarge FourierGalerkin GalerkinForce ForceBilin ForceTwoScale EnstDiff ForceDiff

section Gen
variable {R : Type} (K : CRing R) {I : Type}

def restr (inN : I → Bool) (u : I → V3 R) (p : I) : V3 R := cond (inN p) (u p) (z3 K)
def hiF (inN : I → Bool) (u : I → V3 R) (p : I) : V3 R := cond (inN p) (z3 K) (u p)

theorem u_split (inN : I → Bool) (u : I → V3 R) (p : I) : u p = addF K (restr K inN u) (hiF K inN u) p := by
  show u p = add3 K (cond (inN p) (u p) (z3 K)) (cond (inN p) (z3 K) (u p))
  cases inN p with
  | true =>
    apply v3_ext
    · show (u p).x = K.add (u p).x K.zero; lib_ring
    · show (u p).y = K.add (u p).y K.zero; lib_ring
    · show (u p).z = K.add (u p).z K.zero; lib_ring
  | false =>
    apply v3_ext
    · show (u p).x = K.add K.zero (u p).x; lib_ring
    · show (u p).y = K.add K.zero (u p).y; lib_ring
    · show (u p).z = K.add K.zero (u p).z; lib_ring

/-- a sum over l of terms vanishing off the predicate equals the sum over the filtered list -/
theorem lsum_filter (inN : I → Bool) (f : I → R) (hf : ∀ p, inN p = false → f p = K.zero) :
    ∀ (l : List I), lsum K f l = lsum K f (l.filter inN)
  | [] => rfl
  | p :: l => by
    have ih := lsum_filter inN f hf l
    show K.add (f p) (lsum K f l) = lsum K f (List.filter inN (p :: l))
    rw [show List.filter inN (p :: l) = cond (inN p) (p :: List.filter inN l) (List.filter inN l) by
      show (match inN p with | true => p :: List.filter inN l | false => List.filter inN l) = _
      cases inN p <;> rfl]
    cases hp : inN p with
    | true => show K.add (f p) (lsum K f l) = K.add (f p) (lsum K f (List.filter inN l)); rw [ih]
    | false => show K.add (f p) (lsum K f l) = lsum K f (List.filter inN l); rw [hf p hp, ih]; lib_ring

theorem lsum_ext_on {f g : I → R} : ∀ (l : List I), (∀ i, i ∈ l → f i = g i) → lsum K f l = lsum K g l
  | [], _ => rfl
  | p :: l, h => by
    show K.add (f p) (lsum K f l) = K.add (g p) (lsum K g l)
    rw [h p (List.Mem.head _), lsum_ext_on l (fun i hi => h i (List.Mem.tail _ hi))]

theorem mem_filter_true (inN : I → Bool) (x : I) : ∀ (l : List I), x ∈ l.filter inN → inN x = true
  | [], h => nomatch h
  | p :: l, h => by
    have h' : x ∈ cond (inN p) (p :: List.filter inN l) (List.filter inN l) := by
      have e : List.filter inN (p :: l) = cond (inN p) (p :: List.filter inN l) (List.filter inN l) := by
        show (match inN p with | true => p :: List.filter inN l | false => List.filter inN l) = _
        cases inN p <;> rfl
      rw [← e]; exact h
    cases hp : inN p with
    | true =>
      rw [hp] at h'
      cases h' with
      | head => exact hp
      | tail _ h'' => exact mem_filter_true inN x l h''
    | false => rw [hp] at h'; exact mem_filter_true inN x l h'

/-- one component: Σ_p Σ_q coef(u) v_q over the filtered list = the same over lM for the restricted fields -/
theorem comp_restr (l : List I) (inN : I → Bool) (δ : I → I → I → R) (ι : R) (k u v : I → V3 R) (r : I)
    (sel : V3 R → R) (hsel : sel (z3 K) = K.zero) :
    lsum K (fun p => lsum K (fun q => K.mul (coef K δ ι k u p q r) (sel (v q))) (l.filter inN)) (l.filter inN)
      = lsum K (fun p => lsum K (fun q => K.mul (coef K δ ι k (restr K inN u) p q r) (sel (restr K inN v q))) l) l := by
  -- inner sums
  have inner : ∀ p, lsum K (fun q => K.mul (coef K δ ι k (restr K inN u) p q r) (sel (restr K inN v q))) l
      = lsum K (fun q => K.mul (coef K δ ι k (restr K inN u) p q r) (sel (v q))) (l.filter inN) := by
    intro p
    rw [lsum_filter K inN _ (fun q hq => by
      show K.mul _ (sel (cond (inN q) (v q) (z3 K))) = K.zero
      rw [hq]; show K.mul _ (sel (z3 K)) = K.zero; rw [hsel]; lib_ring) l]
    exact lsum_ext_on K _ (fun q hq => by
      show K.mul _ (sel (cond (inN q) (v q) (z3 K))) = _
      rw [mem_filter_true inN q l hq]; rfl)
  rw [lsum_ext K _ _ inner l]
  rw [lsum_filter K inN _ (fun p hp => by
    have hc : ∀ q, coef K δ ι k (restr K inN u) p q r = K.zero := by
      intro q
      show K.mul (δ p q r) (K.mul ι (dot3 K (cond (inN p) (u p) (z3 K)) (k q))) = K.zero
      rw [hp]; show K.mul (δ p q r) (K.mul ι (K.add (K.add (K.mul K.zero (k q).x) (K.mul K.zero (k q).y)) (K.mul K.zero (k q).z))) = K.zero
      lib_ring
    rw [lsum_ext K _ (fun _ => K.zero) (fun q => by rw [hc q]; lib_ring)]
    exact lsum_zero K _) l]
  symm
  exact lsum_ext_on K _ (fun p hp => lsum_ext_on K _ (fun q _ => by
    have hpN := mem_filter_true inN p l hp
    show K.mul (K.mul (δ p q r) (K.mul ι (dot3 K (cond (inN p) (u p) (z3 K)) (k q)))) (sel (v q))
      = K.mul (K.mul (δ p q r) (K.mul ι (dot3 K (u p) (k q)))) (sel (v q))
    rw [hpN]; rfl))

theorem restr_eq (l : List I) (inN : I → Bool) (δ : I → I → I → R) (ι : R) (k u v : I → V3 R) (r : I) :
    Nl K (l.filter inN) δ ι k u v r = Nl K l δ ι k (restr K inN u) (restr K inN v) r := by
  apply v3_ext
  · exact comp_restr K l inN δ ι k u v r (fun w => w.x) rfl
  · exact comp_restr K l inN δ ι k u v r (fun w => w.y) rfl
  · exact comp_restr K l inN δ ι k u v r (fun w => w.z) rfl

theorem trunc_err (l : List I) (inN : I → Bool) (δ : I → I → I → R) (ι : R) (k a : I → V3 R) (r : I) :
    sub3 K (Nl K l δ ι k a a r) (Nl K (l.filter inN) δ ι k a a r)
      = add3 K (Nl K l δ ι k (hiF K inN a) a r) (Nl K l δ ι k (restr K inN a) (hiF K inN a) r) := by
  rw [restr_eq K l inN δ ι k a a r]
  rw [ForceLip.Nl_congr K l δ ι k a (addF K (restr K inN a) (hiF K inN a)) a (addF K (restr K inN a) (hiF K inN a)) r
    (u_split K inN a) (u_split K inN a), Nl_diff]
  rw [ForceLip.Nl_congr K l δ ι k (hiF K inN a) (hiF K inN a) (addF K (restr K inN a) (hiF K inN a)) a r (fun _ => rfl)
    (fun p => (u_split K inN a p).symm)]
  generalize Nl K l δ ι k (restr K inN a) (restr K inN a) r = A
  generalize Nl K l δ ι k (hiF K inN a) a r = B
  generalize Nl K l δ ι k (restr K inN a) (hiF K inN a) r = C
  apply v3_ext
  · show K.add (K.add (K.add A.x B.x) C.x) (K.neg A.x) = K.add B.x C.x; lib_ring
  · show K.add (K.add (K.add A.y B.y) C.y) (K.neg A.y) = K.add B.y C.y; lib_ring
  · show K.add (K.add (K.add A.z B.z) C.z) (K.neg A.z) = K.add B.z C.z; lib_ring

end Gen

end TruncError

#print axioms TruncError.u_split
#print axioms TruncError.lsum_filter
#print axioms TruncError.comp_restr
#print axioms TruncError.restr_eq
#print axioms TruncError.trunc_err

ns/TruncGlue.lean

lean_all/ns/TruncGlue.lean

/-
  ns/TruncGlue.lean — T37 cycle 1866 (goal G859c, conditional gluing): a finite-time truncation estimate on [0, t₂]
  and the time-uniform estimate after t₂ (TruncUniform0.trunc_uniform0_from) combine to an estimate on [0, ∞):
    glue_alg    V·X ≤ V·Y + K, P·Y ≤ Rr  ⇒  P·(V·X) ≤ V·Rr + P·K   (P, V ≥ 0)
    trunc_glue  if ν^{t₂}·Q_d(t₂) ≤ Rr (any finite-time estimate, e.g. TruncRate3.trunc_rate3 at T = t₂) and the
                smallness hypotheses of trunc_uniform0_from hold at t₂, then for every n
                ν^{t₂}·ν²·Q_d(t₂ + n) ≤ ν²·Rr + ν^{t₂}·iR·κ·2Q₂(A, t₂).
  The finite-time bound Rr is a hypothesis: for large data it needs H² bounds on [0, t₂] uniform in the fine
  truncation, which is not proved here.
  Lean 4 core plus ns/TruncUniform0.
-/
import ns.TruncUniform0

namespace TruncGlue
open DetChain NSChain DyadicSmallGlobal LibRing DyadicLarge FourierGalerkin GalerkinForce ForceBound ForceSob
  ModeCount CubeCount ForceUniform NormCompare EnstStep EnstGlobal EnstGalerkin ForceTwoScale ForceBilin EnstDiff
  ForceDiff TruncError TruncBound RestrStep GronwallSource EnstGronwall WeightSplit LatticeSum TruncCauchy TruncSmall
  TruncStart TruncRate3 TruncStart3 SchemeTele TruncDampSum TruncUniform TruncUniform0

section Gen
variable {R : Type} (K : CRing R) (O : OrdR K) {I : Type}
variable (inv : R → R) (mul_inv : ∀ y, y ≠ K.zero → K.mul y (inv y) = K.one) (h10 : K.one ≠ K.zero)

theorem glue_alg (P V X Y Kc Rr : R) (hP : O.nn P) (hV : O.nn V) (u : Le K O.toCone (K.mul V X) (K.add (K.mul V Y) Kc))
    (r : Le K O.toCone (K.mul P Y) Rr) : Le K O.toCone (K.mul P (K.mul V X)) (K.add (K.mul V Rr) (K.mul P Kc)) := by
  refine le_trans' K O.toCone _ _ _ (le_mul_nn K O.toCone P _ _ hP u) (le_trans' K O.toCone _ _ _ ⟨K.zero, O.nn_zero, ?_⟩
    (le_add' K O.toCone _ _ _ _ (le_mul_nn K O.toCone V _ _ hV r) (le_refl' K O.toCone _)))
  lib_ring

include mul_inv h10 in
theorem trunc_glue (l : List I) (kz : I → Mode) (hd : List.Pairwise (fun a b => kz a ≠ kz b) l)
    (h1 : ∀ p, 1 ≤ nrm (kz p)) (M : Nat) (hM : ∀ p, p ∈ l → nrm (kz p) ≤ M + 1) (Rc R' t0 : Nat) (hRM : R' ≤ M)
    (ι h ν Aa iR : R) (hh : O.nn h) (hν : O.nn ν) (hν0 : ν ≠ K.zero) (hAa : O.nn Aa)
    (hiRn : O.nn iR) (hiR : K.mul (K.mul (castN K (Rc + 1)) (castN K (Rc + 1))) iR = K.one)
    (A B : Nat → I → V3 R) (t2 : Nat)
    (hA : ∀ t r, Step K h ν (wt K kz r) (A (t + 1) r) (A t r) (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (A t) (A t) r))
    (hB : ∀ t r, Step K h ν (wt K kz r) (B (t + 1) r) (B t r)
      (Nl K (l.filter (lowB Rc kz)) (TriadSparse.delta K kz) ι (kv K kz) (B t) (B t) r))
    (hQA : Le K O.toCone (QH K ν h kz l (fun t => A (t2 + t)) 0) (K.mul ν Aa))
    (smallB : Le K O.toCone (K.mul (K.mul (EnstH2.cH K) (K.mul ι ι)) (QH K ν h kz (l.filter (lowB Rc kz)) (fun t => B (t2 + t)) 0)) (K.mul ν (K.mul ν ν)))
    (hQB : Le K O.toCone (QH K ν h kz (l.filter (lowB Rc kz)) (fun t => B (t2 + t)) 0) (K.mul ν Aa))
    (hsm : Le K O.toCone (K.add (K.add (K.mul (K.mul (two' K) (K.mul (two' K) (two' K))) (K.mul (K.mul ι ι) (K.mul (C1' K inv R') Aa))) (K.mul (K.mul (two' K) (two' K)) (K.mul (K.mul ι ι) (K.mul (castN K 135) Aa))))
      (K.mul (K.mul (two' K) (K.mul (two' K) (two' K))) (K.mul (K.mul ι ι) (K.mul (C2' K inv R' t0) Aa)))) (K.mul ν ν)) (n : Nat)
    (small2 : Le K O.toCone (K.mul (K.add (K.mul (EnstH2.cH K) (K.mul ι ι)) (K.mul (EnstH2.cH K) (K.mul ι ι))) (QH K ν h kz l (fun t => A (t2 + t)) 0))
      (K.mul ν (K.mul ν ν)))
    (Rr : R) (hfin : Le K O.toCone (K.mul (pwr K ν t2) (Q K ν h (fun t => Zl K kz (l.filter (lowB Rc kz)) (dF K (restr K (lowB Rc kz) (A t)) (B t)))
        (fun t => Al K kz (l.filter (lowB Rc kz)) (dF K (restr K (lowB Rc kz) (A t)) (B t))) t2)) Rr) :
    Le K O.toCone (K.mul (pwr K ν t2) (K.mul (K.mul ν ν) (Q K ν h (fun t => Zl K kz (l.filter (lowB Rc kz)) (dF K (restr K (lowB Rc kz) (A (t2 + t))) (B (t2 + t))))
        (fun t => Al K kz (l.filter (lowB Rc kz)) (dF K (restr K (lowB Rc kz) (A (t2 + t))) (B (t2 + t)))) n)))
      (K.add (K.mul (K.mul ν ν) Rr) (K.mul (pwr K ν t2) (K.mul (K.mul iR (K.mul (two' K) (K.add (K.mul (two' K) (K.add (K.mul (two' K) (K.mul (K.mul ι ι) (K.mul (C1' K inv R') Aa))) (K.mul (two' K) (K.mul (K.mul ι ι) (K.mul (C2' K inv R' t0) Aa))))) (K.mul (two' K) (K.mul (K.mul ι ι) (K.mul Aa (castN K 135))))))) (K.mul (K.add K.one K.one) (QH K ν h kz l (fun t => A (t2 + t)) 0))))) :=
  glue_alg K O (pwr K ν t2) (K.mul ν ν) _ _ _ Rr (EnstGronwall.pwr_nn K O ν hν t2) (O.nn_sq ν)
    (trunc_uniform0_from K O inv mul_inv h10 l kz hd h1 M hM Rc R' t0 hRM ι h ν Aa iR hh hν hν0 hAa hiRn hiR A B t2
      hA hB hQA smallB hQB hsm n small2) hfin

end Gen
end TruncGlue

#print axioms TruncGlue.glue_alg
#print axioms TruncGlue.trunc_glue

ns/TruncNorms.lean

lean_all/ns/TruncNorms.lean

/-
  ns/TruncNorms.lean — T37 cycle 1223 (goal G776, piece a): on a fixed truncation |k|_∞ ≤ M + 1 every weighted energy is
  bounded by the plain energy, over an ordered field (c_M = 3(M+1)², w_r ≤ c_M):
    wpow_le   Σ_r w_r^j·|a_r|² ≤ c_M^j·Σ_r |a_r|²   for every j
  So under the CFL energy bound E ≤ E* (CFLGlobal) all the norms Z, A, B, B₄, B₅ entering the step-size Cauchy
  estimate (StepCauchy, LocalDefect, FDk, SmallAlpha) are bounded by c_M^j·E*: for a fixed truncation the implicit
  scheme converges as h → 0 for every initial datum (finite-dimensional global existence).

  Lean 4 core plus ns/CFLStep (wt_le, lsum_le_mem), ns/EnstGronwall (pwr).
-/
import ns.CFLStep
import ns.EnstGronwall

namespace TruncNorms
open DetChain NSChain DyadicSmallGlobal LibRing DyadicLarge FourierGalerkin GalerkinForce ForceBound ForceSob
  ModeCount CubeCount ForceUniform NormCompare EnstStep EnstGalerkin EnstDiff WeightH2 CFLStep EnstGronwall

section Gen
variable {R : Type} (K : CRing R) (O : OrdR K) {I : Type}

theorem pwr_le (x y : R) (hx : O.nn x) (hxy : Le K O.toCone x y) : ∀ j, Le K O.toCone (pwr K x j) (pwr K y j)
  | 0 => le_refl' K O.toCone _
  | j + 1 => by
    have ih := pwr_le x y hx hxy j
    have hpx : O.nn (pwr K x j) := EnstGronwall.pwr_nn K O x hx j
    have hy : O.nn y := TruncRate.nn_of_le K O _ _ hx hxy
    exact TruncRate.mm K O _ _ _ _ hx (TruncRate.nn_of_le K O _ _ hpx ih) hxy ih

theorem wpow_le (l : List I) (kz : I → Mode) (M : Nat) (hM : ∀ p, p ∈ l → nrm (kz p) ≤ M + 1) (a : I → V3 R) (j : Nat) :
    Le K O.toCone (lsum K (fun r => K.mul (pwr K (wt K kz r) j) (dot3 K (a r) (a r))) l)
      (K.mul (pwr K (cM K M) j) (lsum K (fun r => dot3 K (a r) (a r)) l)) := by
  rw [← lsum_mul_left]
  apply lsum_le_mem K O
  intro r hr
  have hw : O.nn (wt K kz r) := dot3_nn K O.toCone _
  exact ForceSob.le_mul_right' K O.toCone (pwr_le K O _ _ hw (wt_le K O kz M r (hM r hr)) j) (dot3_nn K O.toCone (a r))

end Gen

end TruncNorms

#print axioms TruncNorms.pwr_le
#print axioms TruncNorms.wpow_le

ns/TruncRate.lean

lean_all/ns/TruncRate.lean

/-
  ns/TruncRate.lean — T37 cycle 1053 (goal G695): the truncation Cauchy estimate with explicit constants. If on the time
  interval the large-truncation solution A has Z(A_t) ≤ Zb and A(A_t) ≤ Aa (EnstLocalTime gives both), and the small
  one has A(B_t) ≤ Ab, and the cutoff constant satisfies 8ι²C₂·Zb ≤ ν², then (TruncCauchy.trunc_cauchy)
    ν^T Q_d(T) ≤ (ν + hα)^T (Q_d(0) + T·h·S),
    α = 8ι²C₁Zb + 4ι²·135·Ab,
    S = 2[2(2ι²·Zh·C₁·Zb + 2ι²·Aa·C₂·Zb) + 2ι²·Aa·135·Zh],   Zh = Aa/(R+1)²  (high-mode enstrophy bound).
  As R → ∞ (and C₂ → 0 by the internal cutoffs) the source S → 0: the discrete-time Galerkin family is Cauchy.

  Lean 4 core plus ns/TruncCauchy.
-/
import ns.TruncCauchy

namespace TruncRate
open DetChain NSChain DyadicSmallGlobal LibRing DyadicLarge FourierGalerkin GalerkinForce ForceBound ForceSob
  ModeCount CubeCount ForceUniform NormCompare EnstStep EnstGlobal EnstGalerkin ForceTwoScale ForceBilin EnstDiff
  ForceDiff TruncError TruncBound RestrStep GronwallSource EnstGronwall WeightSplit LatticeSum TruncCauchy

section Gen
variable {R : Type} (K : CRing R) (O : OrdR K) {I : Type}
variable (inv : R → R) (mul_inv : ∀ y, y ≠ K.zero → K.mul y (inv y) = K.one) (h10 : K.one ≠ K.zero)

/-- monotone in the restricted field: Z over the filtered list of A|_N ≤ Z over l of A -/
theorem Z_restr_le (l : List I) (kz : I → Mode) (inN : I → Bool) (A : I → V3 R) :
    Le K O.toCone (Zl K kz (l.filter inN) (restr K inN A)) (Zl K kz l A) := by
  have h1 := lsum_filter_le K O.toCone inN (fun p => K.mul (wt K kz p) (dot3 K (restr K inN A p) (restr K inN A p)))
    (fun p => O.nn_mul _ _ (dot3_nn K O.toCone _) (dot3_nn K O.toCone _)) l
  have h2 := lsum_le K O.toCone _ _ (fun p => part_leW K O (wt K kz p) (dot3_nn K O.toCone _) A p (inN p)) l
  exact le_trans' K O.toCone _ _ _ h1 h2

theorem Zh_le (l : List I) (kz : I → Mode) (Rc : Nat) (A : I → V3 R) (Aa : R)
    (hA : Le K O.toCone (Al K kz l A) Aa) (iR : R)
    (hiR : K.mul (K.mul (castN K (Rc + 1)) (castN K (Rc + 1))) iR = K.one) (hiRn : O.nn iR) :
    Le K O.toCone (Zl K kz l (hiF K (lowB Rc kz) A)) (K.mul Aa iR) := by
  have hz := high_Z K O l kz Rc A
  have ha := lsum_le K O.toCone _ _ (fun p => partH_leW K O (K.mul (wt K kz p) (wt K kz p)) (O.nn_sq _) A p (lowB Rc kz p)) l
  have h3 := le_trans' K O.toCone _ _ _ hz (le_trans' K O.toCone _ _ _ ha hA)
  have h4 := le_mul_nn K O.toCone iR _ _ hiRn h3
  show Le K O.toCone (lsum K (fun p => K.mul (wt K kz p) (dot3 K (hiF K (lowB Rc kz) A p) (hiF K (lowB Rc kz) A p))) l) _
  generalize lsum K (fun p => K.mul (wt K kz p) (dot3 K (hiF K (lowB Rc kz) A p) (hiF K (lowB Rc kz) A p))) l = Zh at h4 ⊢
  generalize castN K (Rc + 1) = c at h4 hiR
  have e : K.mul iR (K.mul (K.mul c c) Zh) = K.mul (K.mul (K.mul c c) iR) Zh := by lib_ring
  rw [e, hiR, K.mul_comm iR] at h4
  rw [show K.mul K.one Zh = Zh by lib_ring] at h4
  exact h4

theorem mm (x x' y y' : R) (hx : O.nn x) (hy' : O.nn y') (h1 : Le K O.toCone x x') (h2 : Le K O.toCone y y') :
    Le K O.toCone (K.mul x y) (K.mul x' y') :=
  le_trans' K O.toCone _ _ _ (le_mul_nn K O.toCone x _ _ hx h2) (ForceSob.le_mul_right' K O.toCone h1 hy')

theorem nn_of_le (x y : R) (hx : O.nn x) (h : Le K O.toCone x y) : O.nn y := by
  cases h with
  | intro d hd => rw [← hd.2]; exact O.nn_add _ _ hx hd.1

include mul_inv h10 in
theorem trunc_rate (l : List I) (kz : I → Mode) (hd : List.Pairwise (fun a b => kz a ≠ kz b) l)
    (h1 : ∀ p, 1 ≤ nrm (kz p)) (M : Nat) (hM : ∀ p, p ∈ l → nrm (kz p) ≤ M + 1) (Rc R' t0 : Nat) (hRM : R' ≤ M)
    (ι h ν Zb Aa Ab iR : R) (hh : O.nn h) (hν : O.nn ν) (hZb : O.nn Zb) (hAa : O.nn Aa) (hAb : O.nn Ab) (hiRn : O.nn iR)
    (hiR : K.mul (K.mul (castN K (Rc + 1)) (castN K (Rc + 1))) iR = K.one)
    (A B : Nat → I → V3 R)
    (hA : ∀ t r, Step K h ν (wt K kz r) (A (t + 1) r) (A t r) (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (A t) (A t) r))
    (hB : ∀ t r, r ∈ l.filter (lowB Rc kz) → Step K h ν (wt K kz r) (B (t + 1) r) (B t r)
      (Nl K (l.filter (lowB Rc kz)) (TriadSparse.delta K kz) ι (kv K kz) (B t) (B t) r))
    (hZA : ∀ t, Le K O.toCone (Zl K kz l (A t)) Zb) (hAA : ∀ t, Le K O.toCone (Al K kz l (A t)) Aa)
    (hAB : ∀ t, Le K O.toCone (Al K kz (l.filter (lowB Rc kz)) (B t)) Ab)
    (hβc : Le K O.toCone (K.mul (K.mul (two' K) (K.mul (two' K) (two' K))) (K.mul (K.mul ι ι) (K.mul (C2' K inv R' t0) Zb)))
      (K.mul ν ν)) (T : Nat) :
    Le K O.toCone (K.mul (pwr K ν T)
        (Q K ν h (fun t => Zl K kz (l.filter (lowB Rc kz)) (dF K (restr K (lowB Rc kz) (A t)) (B t)))
          (fun t => Al K kz (l.filter (lowB Rc kz)) (dF K (restr K (lowB Rc kz) (A t)) (B t))) T))
      (K.mul (pwr K (K.add ν (K.mul h
          (K.add (K.mul (K.mul (two' K) (K.mul (two' K) (two' K))) (K.mul (K.mul ι ι) (K.mul (C1' K inv R') Zb)))
            (K.mul (K.mul (two' K) (two' K)) (K.mul (K.mul ι ι) (K.mul (castN K 135) Ab)))))) T)
        (K.add (Q K ν h (fun t => Zl K kz (l.filter (lowB Rc kz)) (dF K (restr K (lowB Rc kz) (A t)) (B t)))
          (fun t => Al K kz (l.filter (lowB Rc kz)) (dF K (restr K (lowB Rc kz) (A t)) (B t))) 0)
          (K.mul (castN K T) (K.mul h (K.mul (two' K)
            (K.add
              (K.mul (two' K) (K.add
                (K.mul (two' K) (K.mul (K.mul ι ι) (K.mul (K.mul Aa iR) (K.mul (C1' K inv R') Zb))))
                (K.mul (two' K) (K.mul (K.mul ι ι) (K.mul Aa (K.mul (C2' K inv R' t0) Zb))))))
              (K.mul (two' K) (K.mul (K.mul ι ι) (K.mul Aa (K.mul (castN K 135) (K.mul Aa iR))))))))))) := by
  have n2 : O.nn (two' K) := O.nn_add _ _ (RatModel.nn_one' K O.toCone) (RatModel.nn_one' K O.toCone)
  have hiv : O.nn (iv K inv (R' + 1)) := iv_nn K O inv mul_inv h10 R'
  have nC1 : O.nn (C1' K inv R') := O.nn_add _ _ (O.nn_mul _ _ (RatModel.nn_cast K O.toCone _) (RatModel.nn_one' K O.toCone))
    (O.nn_mul _ _ (RatModel.nn_cast K O.toCone _) hiv)
  have nC2 : O.nn (C2' K inv R' t0) := O.nn_add _ _ (O.nn_mul _ _ (RatModel.nn_cast K O.toCone _) (O.nn_sq _))
    (O.nn_mul _ _ (RatModel.nn_cast K O.toCone _) hiv)
  have n135 : O.nn (castN K 135) := RatModel.nn_cast K O.toCone 135
  have nii : O.nn (K.mul ι ι) := O.nn_sq ι
  have hw : ∀ r, O.nn (wt K kz r) := fun r => dot3_nn K O.toCone _
  have hZn : ∀ (L : List I) (u : I → V3 R), O.nn (Zl K kz L u) := fun L u =>
    lsum_nn K O.toCone _ (fun r => O.nn_mul _ _ (hw r) (dot3_nn K O.toCone _)) L
  have hAn : ∀ (L : List I) (u : I → V3 R), O.nn (Al K kz L u) := fun L u =>
    lsum_nn K O.toCone _ (fun r => O.nn_mul _ _ (O.nn_sq _) (dot3_nn K O.toCone _)) L
  have refl := fun (x : R) => le_refl' K O.toCone x
  have hZa : ∀ t, Le K O.toCone (Zl K kz (l.filter (lowB Rc kz)) (restr K (lowB Rc kz) (A t))) Zb := fun t =>
    le_trans' K O.toCone _ _ _ (Z_restr_le K O l kz (lowB Rc kz) (A t)) (hZA t)
  have hZh : ∀ t, Le K O.toCone (Zl K kz l (hiF K (lowB Rc kz) (A t))) (K.mul Aa iR) := fun t =>
    Zh_le K O l kz Rc (A t) Aa (hAA t) iR hiR hiRn
  have hAh : ∀ t, Le K O.toCone (Al K kz l (hiF K (lowB Rc kz) (A t))) Aa := fun t =>
    le_trans' K O.toCone _ _ _ (lsum_le K O.toCone _ _ (fun p => partH_leW K O (K.mul (wt K kz p) (wt K kz p)) (O.nn_sq _) (A t) p (lowB Rc kz p)) l) (hAA t)
  have nZh : O.nn (K.mul Aa iR) := O.nn_mul _ _ hAa hiRn
  refine trunc_cauchy K O inv mul_inv h10 l kz hd h1 M hM Rc R' t0 hRM ι h ν _ _ hh hν
    (O.nn_add _ _ (O.nn_mul _ _ (O.nn_mul _ _ n2 (O.nn_mul _ _ n2 n2)) (O.nn_mul _ _ nii (O.nn_mul _ _ nC1 hZb)))
      (O.nn_mul _ _ (O.nn_mul _ _ n2 n2) (O.nn_mul _ _ nii (O.nn_mul _ _ n135 hAb))))
    (O.nn_mul _ _ n2 (O.nn_add _ _ (O.nn_mul _ _ n2 (O.nn_add _ _
        (O.nn_mul _ _ n2 (O.nn_mul _ _ nii (O.nn_mul _ _ nZh (O.nn_mul _ _ nC1 hZb))))
        (O.nn_mul _ _ n2 (O.nn_mul _ _ nii (O.nn_mul _ _ hAa (O.nn_mul _ _ nC2 hZb))))))
      (O.nn_mul _ _ n2 (O.nn_mul _ _ nii (O.nn_mul _ _ hAa (O.nn_mul _ _ n135 nZh))))))
    A B hA hB ?_ ?_ ?_ T
  · intro t
    exact le_add' K O.toCone _ _ _ _
      (le_mul_nn K O.toCone _ _ _ (O.nn_mul _ _ n2 (O.nn_mul _ _ n2 n2)) (le_mul_nn K O.toCone _ _ _ nii
        (le_mul_nn K O.toCone _ _ _ nC1 (hZa t))))
      (le_mul_nn K O.toCone _ _ _ (O.nn_mul _ _ n2 n2) (le_mul_nn K O.toCone _ _ _ nii
        (le_mul_nn K O.toCone _ _ _ n135 (hAB t))))
  · intro t
    exact le_trans' K O.toCone _ _ _ (le_mul_nn K O.toCone _ _ _ (O.nn_mul _ _ n2 (O.nn_mul _ _ n2 n2))
      (le_mul_nn K O.toCone _ _ _ nii (le_mul_nn K O.toCone _ _ _ nC2 (hZa t)))) hβc
  · intro t
    refine le_mul_nn K O.toCone _ _ _ n2 (le_add' K O.toCone _ _ _ _
      (le_mul_nn K O.toCone _ _ _ n2 (le_add' K O.toCone _ _ _ _
        (le_mul_nn K O.toCone _ _ _ n2 (le_mul_nn K O.toCone _ _ _ nii
          (mm K O _ _ _ _ (hZn _ _) (O.nn_mul _ _ nC1 hZb) (hZh t) (le_mul_nn K O.toCone _ _ _ nC1 (hZA t)))))
        (le_mul_nn K O.toCone _ _ _ n2 (le_mul_nn K O.toCone _ _ _ nii
          (mm K O _ _ _ _ (hAn _ _) (O.nn_mul _ _ nC2 hZb) (hAh t) (le_mul_nn K O.toCone _ _ _ nC2 (hZA t)))))))
      (le_mul_nn K O.toCone _ _ _ n2 (le_mul_nn K O.toCone _ _ _ nii
        (mm K O _ _ _ _ (hAn _ _) (O.nn_mul _ _ n135 nZh) (hAA t) (le_mul_nn K O.toCone _ _ _ n135 (hZh t))))))

end Gen

end TruncRate

#print axioms TruncRate.Z_restr_le
#print axioms TruncRate.Zh_le
#print axioms TruncRate.mm
#print axioms TruncRate.nn_of_le
#print axioms TruncRate.trunc_rate

ns/TruncRate3.lean

lean_all/ns/TruncRate3.lean

/-
  ns/TruncRate3.lean — T37 cycle 1776 (goal G852i, piece a): the truncation Cauchy estimate with a source that decays
  in the cutoff R for fixed internal cutoffs R', t0. TruncRate bounded the high-mode H² mass Σ_{|k|>R} w²|A|² only by
  Aa, which leaves a term 2ι²·Aa·C₂·Zb in the source that does not vanish as R → ∞. With an H³ bound B(A_t) ≤ Bb
  (B = Σ w³|a|², EnstH3.h3_global for small data):
    high_A       (R+1)²·Σ w²|hi_R a|² ≤ Σ w³|hi_R a|²
    Ah_le        Σ w²|hi_R A|² ≤ Bb/(R+1)²
    trunc_rate3  ν^T Q_d(T) ≤ (ν + hα)^T (Q_d(0) + T·h·S),
                 S = 2[2(2ι²·(Aa/(R+1)²)·C₁·Zb + 2ι²·(Bb/(R+1)²)·C₂·Zb) + 2ι²·Aa·135·Aa/(R+1)²],
  so every term of S carries 1/(R+1)² while α does not depend on R: S = O(1/R²) at a fixed growth factor.
  Lean 4 core plus ns/TruncRate.
-/
import ns.TruncRate

namespace TruncRate3
open DetChain NSChain DyadicSmallGlobal LibRing DyadicLarge FourierGalerkin GalerkinForce ForceBound ForceSob
  ModeCount CubeCount ForceUniform NormCompare EnstStep EnstGlobal EnstGalerkin ForceTwoScale ForceBilin EnstDiff
  ForceDiff TruncError TruncBound RestrStep GronwallSource EnstGronwall WeightSplit LatticeSum TruncCauchy TruncRate

section Gen
variable {R : Type} (K : CRing R) (O : OrdR K) {I : Type}
variable (inv : R → R) (mul_inv : ∀ y, y ≠ K.zero → K.mul y (inv y) = K.one) (h10 : K.one ≠ K.zero)

def Bl (kz : I → Mode) (L : List I) (u : I → V3 R) : R :=
  lsum K (fun p => K.mul (K.mul (K.mul (wt K kz p) (wt K kz p)) (wt K kz p)) (dot3 K (u p) (u p))) L

theorem high_A (l : List I) (kz : I → Mode) (Rc : Nat) (a : I → V3 R) :
    Le K O.toCone (K.mul (K.mul (castN K (Rc + 1)) (castN K (Rc + 1)))
        (lsum K (fun p => K.mul (K.mul (wt K kz p) (wt K kz p)) (dot3 K (hiF K (lowB Rc kz) a p) (hiF K (lowB Rc kz) a p))) l))
      (lsum K (fun p => K.mul (K.mul (K.mul (wt K kz p) (wt K kz p)) (wt K kz p))
        (dot3 K (hiF K (lowB Rc kz) a p) (hiF K (lowB Rc kz) a p))) l) := by
  rw [← lsum_mul_left]
  refine lsum_le K O.toCone _ _ (fun p => ?_) l
  show Le K O.toCone (K.mul _ (K.mul (K.mul (wt K kz p) (wt K kz p))
      (dot3 K (cond (lowB Rc kz p) (z3 K) (a p)) (cond (lowB Rc kz p) (z3 K) (a p)))))
    (K.mul (K.mul (K.mul (wt K kz p) (wt K kz p)) (wt K kz p))
      (dot3 K (cond (lowB Rc kz p) (z3 K) (a p)) (cond (lowB Rc kz p) (z3 K) (a p))))
  cases hb : lowB Rc kz p with
  | true =>
    refine ⟨K.zero, O.nn_zero, ?_⟩
    show K.add (K.mul _ (K.mul (K.mul (wt K kz p) (wt K kz p))
        (K.add (K.add (K.mul K.zero K.zero) (K.mul K.zero K.zero)) (K.mul K.zero K.zero)))) K.zero
      = K.mul (K.mul (K.mul (wt K kz p) (wt K kz p)) (wt K kz p))
        (K.add (K.add (K.mul K.zero K.zero) (K.mul K.zero K.zero)) (K.mul K.zero K.zero))
    lib_ring
  | false =>
    have hge : Rc + 1 ≤ nrm (kz p) := by
      cases Nat.lt_or_ge Rc (nrm (kz p)) with
      | inl lt => exact lt
      | inr ge => exact absurd (Nat.ble_eq_true_of_le ge) (by
          show ¬ Nat.ble (nrm (kz p)) Rc = true; rw [show Nat.ble (nrm (kz p)) Rc = lowB Rc kz p from rfl, hb]; exact Bool.noConfusion)
    have m := RatModel.cast_mono K O.toCone _ _ hge
    have sq := EnstLocal.sq_mono K O _ _ (RatModel.nn_cast K O.toCone _) (RatModel.nn_cast K O.toCone _) m
    have w1 := le_trans' K O.toCone _ _ _ sq (nrm_le_dot K O.toCone (kz p))
    have m2 := ForceSob.le_mul_right' K O.toCone w1
      (O.nn_mul _ _ (O.nn_sq (wt K kz p)) (dot3_nn K O.toCone (a p)))
    have e2 : K.mul (dot3 K (kvec K (kz p)) (kvec K (kz p))) (K.mul (K.mul (wt K kz p) (wt K kz p)) (dot3 K (a p) (a p)))
        = K.mul (K.mul (K.mul (wt K kz p) (wt K kz p)) (wt K kz p)) (dot3 K (a p) (a p)) := by
      show K.mul (wt K kz p) (K.mul (K.mul (wt K kz p) (wt K kz p)) (dot3 K (a p) (a p))) = _; lib_ring
    rw [e2] at m2
    exact m2

theorem Ah_le (l : List I) (kz : I → Mode) (Rc : Nat) (A : I → V3 R) (Bb : R)
    (hB : Le K O.toCone (Bl K kz l A) Bb) (iR : R)
    (hiR : K.mul (K.mul (castN K (Rc + 1)) (castN K (Rc + 1))) iR = K.one) (hiRn : O.nn iR) :
    Le K O.toCone (Al K kz l (hiF K (lowB Rc kz) A)) (K.mul Bb iR) := by
  have hz := high_A K O l kz Rc A
  have ha := lsum_le K O.toCone _ _ (fun p => partH_leW K O (K.mul (K.mul (wt K kz p) (wt K kz p)) (wt K kz p))
    (O.nn_mul _ _ (O.nn_sq _) (dot3_nn K O.toCone _)) A p (lowB Rc kz p)) l
  have h3 := le_trans' K O.toCone _ _ _ hz (le_trans' K O.toCone _ _ _ ha hB)
  have h4 := le_mul_nn K O.toCone iR _ _ hiRn h3
  show Le K O.toCone (lsum K (fun p => K.mul (K.mul (wt K kz p) (wt K kz p))
    (dot3 K (hiF K (lowB Rc kz) A p) (hiF K (lowB Rc kz) A p))) l) _
  generalize lsum K (fun p => K.mul (K.mul (wt K kz p) (wt K kz p))
    (dot3 K (hiF K (lowB Rc kz) A p) (hiF K (lowB Rc kz) A p))) l = Ah at h4 ⊢
  generalize castN K (Rc + 1) = c at h4 hiR
  have e : K.mul iR (K.mul (K.mul c c) Ah) = K.mul (K.mul (K.mul c c) iR) Ah := by lib_ring
  rw [e, hiR, K.mul_comm iR] at h4
  rw [show K.mul K.one Ah = Ah by lib_ring] at h4
  exact h4

include mul_inv h10 in
theorem trunc_rate3 (l : List I) (kz : I → Mode) (hd : List.Pairwise (fun a b => kz a ≠ kz b) l)
    (h1 : ∀ p, 1 ≤ nrm (kz p)) (M : Nat) (hM : ∀ p, p ∈ l → nrm (kz p) ≤ M + 1) (Rc R' t0 : Nat) (hRM : R' ≤ M)
    (ι h ν Zb Aa Ab iR : R) (hh : O.nn h) (hν : O.nn ν) (hZb : O.nn Zb) (hAa : O.nn Aa) (hAb : O.nn Ab) (hiRn : O.nn iR)
    (hiR : K.mul (K.mul (castN K (Rc + 1)) (castN K (Rc + 1))) iR = K.one)
    (A B : Nat → I → V3 R)
    (hA : ∀ t r, Step K h ν (wt K kz r) (A (t + 1) r) (A t r) (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (A t) (A t) r))
    (hB : ∀ t r, r ∈ l.filter (lowB Rc kz) → Step K h ν (wt K kz r) (B (t + 1) r) (B t r)
      (Nl K (l.filter (lowB Rc kz)) (TriadSparse.delta K kz) ι (kv K kz) (B t) (B t) r))
    (hZA : ∀ t, Le K O.toCone (Zl K kz l (A t)) Zb) (hAA : ∀ t, Le K O.toCone (Al K kz l (A t)) Aa)
    (hAB : ∀ t, Le K O.toCone (Al K kz (l.filter (lowB Rc kz)) (B t)) Ab)
    (Bb : R) (hBb : O.nn Bb) (hBA : ∀ t, Le K O.toCone (Bl K kz l (A t)) Bb)
    (hβc : Le K O.toCone (K.mul (K.mul (two' K) (K.mul (two' K) (two' K))) (K.mul (K.mul ι ι) (K.mul (C2' K inv R' t0) Zb)))
      (K.mul ν ν)) (T : Nat) :
    Le K O.toCone (K.mul (pwr K ν T)
        (Q K ν h (fun t => Zl K kz (l.filter (lowB Rc kz)) (dF K (restr K (lowB Rc kz) (A t)) (B t)))
          (fun t => Al K kz (l.filter (lowB Rc kz)) (dF K (restr K (lowB Rc kz) (A t)) (B t))) T))
      (K.mul (pwr K (K.add ν (K.mul h
          (K.add (K.mul (K.mul (two' K) (K.mul (two' K) (two' K))) (K.mul (K.mul ι ι) (K.mul (C1' K inv R') Zb)))
            (K.mul (K.mul (two' K) (two' K)) (K.mul (K.mul ι ι) (K.mul (castN K 135) Ab)))))) T)
        (K.add (Q K ν h (fun t => Zl K kz (l.filter (lowB Rc kz)) (dF K (restr K (lowB Rc kz) (A t)) (B t)))
          (fun t => Al K kz (l.filter (lowB Rc kz)) (dF K (restr K (lowB Rc kz) (A t)) (B t))) 0)
          (K.mul (castN K T) (K.mul h (K.mul (two' K)
            (K.add
              (K.mul (two' K) (K.add
                (K.mul (two' K) (K.mul (K.mul ι ι) (K.mul (K.mul Aa iR) (K.mul (C1' K inv R') Zb))))
                (K.mul (two' K) (K.mul (K.mul ι ι) (K.mul (K.mul Bb iR) (K.mul (C2' K inv R' t0) Zb))))))
              (K.mul (two' K) (K.mul (K.mul ι ι) (K.mul Aa (K.mul (castN K 135) (K.mul Aa iR))))))))))) := by
  have n2 : O.nn (two' K) := O.nn_add _ _ (RatModel.nn_one' K O.toCone) (RatModel.nn_one' K O.toCone)
  have hiv : O.nn (iv K inv (R' + 1)) := iv_nn K O inv mul_inv h10 R'
  have nC1 : O.nn (C1' K inv R') := O.nn_add _ _ (O.nn_mul _ _ (RatModel.nn_cast K O.toCone _) (RatModel.nn_one' K O.toCone))
    (O.nn_mul _ _ (RatModel.nn_cast K O.toCone _) hiv)
  have nC2 : O.nn (C2' K inv R' t0) := O.nn_add _ _ (O.nn_mul _ _ (RatModel.nn_cast K O.toCone _) (O.nn_sq _))
    (O.nn_mul _ _ (RatModel.nn_cast K O.toCone _) hiv)
  have n135 : O.nn (castN K 135) := RatModel.nn_cast K O.toCone 135
  have nii : O.nn (K.mul ι ι) := O.nn_sq ι
  have hw : ∀ r, O.nn (wt K kz r) := fun r => dot3_nn K O.toCone _
  have hZn : ∀ (L : List I) (u : I → V3 R), O.nn (Zl K kz L u) := fun L u =>
    lsum_nn K O.toCone _ (fun r => O.nn_mul _ _ (hw r) (dot3_nn K O.toCone _)) L
  have hAn : ∀ (L : List I) (u : I → V3 R), O.nn (Al K kz L u) := fun L u =>
    lsum_nn K O.toCone _ (fun r => O.nn_mul _ _ (O.nn_sq _) (dot3_nn K O.toCone _)) L
  have refl := fun (x : R) => le_refl' K O.toCone x
  have hZa : ∀ t, Le K O.toCone (Zl K kz (l.filter (lowB Rc kz)) (restr K (lowB Rc kz) (A t))) Zb := fun t =>
    le_trans' K O.toCone _ _ _ (Z_restr_le K O l kz (lowB Rc kz) (A t)) (hZA t)
  have hZh : ∀ t, Le K O.toCone (Zl K kz l (hiF K (lowB Rc kz) (A t))) (K.mul Aa iR) := fun t =>
    Zh_le K O l kz Rc (A t) Aa (hAA t) iR hiR hiRn
  have hAh : ∀ t, Le K O.toCone (Al K kz l (hiF K (lowB Rc kz) (A t))) (K.mul Bb iR) := fun t =>
    Ah_le K O l kz Rc (A t) Bb (hBA t) iR hiR hiRn
  have nBh : O.nn (K.mul Bb iR) := O.nn_mul _ _ hBb hiRn
  have nZh : O.nn (K.mul Aa iR) := O.nn_mul _ _ hAa hiRn
  refine TruncCauchy.trunc_cauchy K O inv mul_inv h10 l kz hd h1 M hM Rc R' t0 hRM ι h ν _ _ hh hν
    (O.nn_add _ _ (O.nn_mul _ _ (O.nn_mul _ _ n2 (O.nn_mul _ _ n2 n2)) (O.nn_mul _ _ nii (O.nn_mul _ _ nC1 hZb)))
      (O.nn_mul _ _ (O.nn_mul _ _ n2 n2) (O.nn_mul _ _ nii (O.nn_mul _ _ n135 hAb))))
    (O.nn_mul _ _ n2 (O.nn_add _ _ (O.nn_mul _ _ n2 (O.nn_add _ _
        (O.nn_mul _ _ n2 (O.nn_mul _ _ nii (O.nn_mul _ _ nZh (O.nn_mul _ _ nC1 hZb))))
        (O.nn_mul _ _ n2 (O.nn_mul _ _ nii (O.nn_mul _ _ nBh (O.nn_mul _ _ nC2 hZb))))))
      (O.nn_mul _ _ n2 (O.nn_mul _ _ nii (O.nn_mul _ _ hAa (O.nn_mul _ _ n135 nZh))))))
    A B hA hB ?_ ?_ ?_ T
  · intro t
    exact le_add' K O.toCone _ _ _ _
      (le_mul_nn K O.toCone _ _ _ (O.nn_mul _ _ n2 (O.nn_mul _ _ n2 n2)) (le_mul_nn K O.toCone _ _ _ nii
        (le_mul_nn K O.toCone _ _ _ nC1 (hZa t))))
      (le_mul_nn K O.toCone _ _ _ (O.nn_mul _ _ n2 n2) (le_mul_nn K O.toCone _ _ _ nii
        (le_mul_nn K O.toCone _ _ _ n135 (hAB t))))
  · intro t
    exact le_trans' K O.toCone _ _ _ (le_mul_nn K O.toCone _ _ _ (O.nn_mul _ _ n2 (O.nn_mul _ _ n2 n2))
      (le_mul_nn K O.toCone _ _ _ nii (le_mul_nn K O.toCone _ _ _ nC2 (hZa t)))) hβc
  · intro t
    refine le_mul_nn K O.toCone _ _ _ n2 (le_add' K O.toCone _ _ _ _
      (le_mul_nn K O.toCone _ _ _ n2 (le_add' K O.toCone _ _ _ _
        (le_mul_nn K O.toCone _ _ _ n2 (le_mul_nn K O.toCone _ _ _ nii
          (mm K O _ _ _ _ (hZn _ _) (O.nn_mul _ _ nC1 hZb) (hZh t) (le_mul_nn K O.toCone _ _ _ nC1 (hZA t)))))
        (le_mul_nn K O.toCone _ _ _ n2 (le_mul_nn K O.toCone _ _ _ nii
          (mm K O _ _ _ _ (hAn _ _) (O.nn_mul _ _ nC2 hZb) (hAh t) (le_mul_nn K O.toCone _ _ _ nC2 (hZA t)))))))
      (le_mul_nn K O.toCone _ _ _ n2 (le_mul_nn K O.toCone _ _ _ nii
        (mm K O _ _ _ _ (hAn _ _) (O.nn_mul _ _ n135 nZh) (hAA t) (le_mul_nn K O.toCone _ _ _ n135 (hZh t))))))

end Gen
end TruncRate3

#print axioms TruncRate3.high_A
#print axioms TruncRate3.Ah_le
#print axioms TruncRate3.trunc_rate3

ns/TruncRateUpto.lean

lean_all/ns/TruncRateUpto.lean

/-
  ns/TruncRateUpto.lean — T37 (goal G859c2g, piece b3): the truncation Cauchy estimate with explicit constants on a
  finite window, over an ordered field. TruncRate.trunc_rate with the bounds Z(A_t) ≤ Zb, A(A_t) ≤ Aa, A(B_t) ≤ Ab
  asked only for t < n; the conclusion holds for T ≤ n (TruncCauchyUpto.trunc_cauchy_upto). With H2LargeAll and
  H2ToA these bounds hold on the large-data local-time window, uniformly in the truncation.

  Lean 4 core plus ns/TruncRate, ns/TruncCauchyUpto.
-/
import ns.TruncRate
import ns.TruncCauchyUpto

namespace TruncRateUpto
open DetChain NSChain DyadicSmallGlobal LibRing DyadicLarge FourierGalerkin GalerkinForce ForceBound ForceSob
  ModeCount CubeCount ForceUniform NormCompare EnstStep EnstGlobal EnstGalerkin ForceTwoScale ForceBilin EnstDiff
  ForceDiff TruncError TruncBound RestrStep GronwallSource EnstGronwall WeightSplit LatticeSum TruncCauchy TruncRate

section Gen
variable {R : Type} (K : CRing R) (O : OrdR K) {I : Type}
variable (inv : R → R) (mul_inv : ∀ y, y ≠ K.zero → K.mul y (inv y) = K.one) (h10 : K.one ≠ K.zero)

include mul_inv h10 in
theorem trunc_rate_upto (l : List I) (kz : I → Mode) (hd : List.Pairwise (fun a b => kz a ≠ kz b) l)
    (h1 : ∀ p, 1 ≤ nrm (kz p)) (M : Nat) (hM : ∀ p, p ∈ l → nrm (kz p) ≤ M + 1) (Rc R' t0 : Nat) (hRM : R' ≤ M)
    (ι h ν Zb Aa Ab iR : R) (hh : O.nn h) (hν : O.nn ν) (hZb : O.nn Zb) (hAa : O.nn Aa) (hAb : O.nn Ab) (hiRn : O.nn iR)
    (hiR : K.mul (K.mul (castN K (Rc + 1)) (castN K (Rc + 1))) iR = K.one)
    (A B : Nat → I → V3 R)
    (hA : ∀ t r, Step K h ν (wt K kz r) (A (t + 1) r) (A t r) (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (A t) (A t) r))
    (hB : ∀ t r, r ∈ l.filter (lowB Rc kz) → Step K h ν (wt K kz r) (B (t + 1) r) (B t r)
      (Nl K (l.filter (lowB Rc kz)) (TriadSparse.delta K kz) ι (kv K kz) (B t) (B t) r))
    (n : Nat) (hZA : ∀ t, t < n → Le K O.toCone (Zl K kz l (A t)) Zb) (hAA : ∀ t, t < n → Le K O.toCone (Al K kz l (A t)) Aa)
    (hAB : ∀ t, t < n → Le K O.toCone (Al K kz (l.filter (lowB Rc kz)) (B t)) Ab)
    (hβc : Le K O.toCone (K.mul (K.mul (two' K) (K.mul (two' K) (two' K))) (K.mul (K.mul ι ι) (K.mul (C2' K inv R' t0) Zb)))
      (K.mul ν ν)) (T : Nat) (hT : T ≤ n) :
    Le K O.toCone (K.mul (pwr K ν T)
        (Q K ν h (fun t => Zl K kz (l.filter (lowB Rc kz)) (dF K (restr K (lowB Rc kz) (A t)) (B t)))
          (fun t => Al K kz (l.filter (lowB Rc kz)) (dF K (restr K (lowB Rc kz) (A t)) (B t))) T))
      (K.mul (pwr K (K.add ν (K.mul h
          (K.add (K.mul (K.mul (two' K) (K.mul (two' K) (two' K))) (K.mul (K.mul ι ι) (K.mul (C1' K inv R') Zb)))
            (K.mul (K.mul (two' K) (two' K)) (K.mul (K.mul ι ι) (K.mul (castN K 135) Ab)))))) T)
        (K.add (Q K ν h (fun t => Zl K kz (l.filter (lowB Rc kz)) (dF K (restr K (lowB Rc kz) (A t)) (B t)))
          (fun t => Al K kz (l.filter (lowB Rc kz)) (dF K (restr K (lowB Rc kz) (A t)) (B t))) 0)
          (K.mul (castN K T) (K.mul h (K.mul (two' K)
            (K.add
              (K.mul (two' K) (K.add
                (K.mul (two' K) (K.mul (K.mul ι ι) (K.mul (K.mul Aa iR) (K.mul (C1' K inv R') Zb))))
                (K.mul (two' K) (K.mul (K.mul ι ι) (K.mul Aa (K.mul (C2' K inv R' t0) Zb))))))
              (K.mul (two' K) (K.mul (K.mul ι ι) (K.mul Aa (K.mul (castN K 135) (K.mul Aa iR))))))))))) := by
  have n2 : O.nn (two' K) := O.nn_add _ _ (RatModel.nn_one' K O.toCone) (RatModel.nn_one' K O.toCone)
  have hiv : O.nn (iv K inv (R' + 1)) := iv_nn K O inv mul_inv h10 R'
  have nC1 : O.nn (C1' K inv R') := O.nn_add _ _ (O.nn_mul _ _ (RatModel.nn_cast K O.toCone _) (RatModel.nn_one' K O.toCone))
    (O.nn_mul _ _ (RatModel.nn_cast K O.toCone _) hiv)
  have nC2 : O.nn (C2' K inv R' t0) := O.nn_add _ _ (O.nn_mul _ _ (RatModel.nn_cast K O.toCone _) (O.nn_sq _))
    (O.nn_mul _ _ (RatModel.nn_cast K O.toCone _) hiv)
  have n135 : O.nn (castN K 135) := RatModel.nn_cast K O.toCone 135
  have nii : O.nn (K.mul ι ι) := O.nn_sq ι
  have hw : ∀ r, O.nn (wt K kz r) := fun r => dot3_nn K O.toCone _
  have hZn : ∀ (L : List I) (u : I → V3 R), O.nn (Zl K kz L u) := fun L u =>
    lsum_nn K O.toCone _ (fun r => O.nn_mul _ _ (hw r) (dot3_nn K O.toCone _)) L
  have hAn : ∀ (L : List I) (u : I → V3 R), O.nn (Al K kz L u) := fun L u =>
    lsum_nn K O.toCone _ (fun r => O.nn_mul _ _ (O.nn_sq _) (dot3_nn K O.toCone _)) L
  have refl := fun (x : R) => le_refl' K O.toCone x
  have hZa : ∀ t, t < n → Le K O.toCone (Zl K kz (l.filter (lowB Rc kz)) (restr K (lowB Rc kz) (A t))) Zb := fun t ht =>
    le_trans' K O.toCone _ _ _ (Z_restr_le K O l kz (lowB Rc kz) (A t)) (hZA t ht)
  have hZh : ∀ t, t < n → Le K O.toCone (Zl K kz l (hiF K (lowB Rc kz) (A t))) (K.mul Aa iR) := fun t ht =>
    Zh_le K O l kz Rc (A t) Aa (hAA t ht) iR hiR hiRn
  have hAh : ∀ t, t < n → Le K O.toCone (Al K kz l (hiF K (lowB Rc kz) (A t))) Aa := fun t ht =>
    le_trans' K O.toCone _ _ _ (lsum_le K O.toCone _ _ (fun p => partH_leW K O (K.mul (wt K kz p) (wt K kz p)) (O.nn_sq _) (A t) p (lowB Rc kz p)) l) (hAA t ht)
  have nZh : O.nn (K.mul Aa iR) := O.nn_mul _ _ hAa hiRn
  refine TruncCauchyUpto.trunc_cauchy_upto K O inv mul_inv h10 l kz hd h1 M hM Rc R' t0 hRM ι h ν _ _ hh hν
    (O.nn_add _ _ (O.nn_mul _ _ (O.nn_mul _ _ n2 (O.nn_mul _ _ n2 n2)) (O.nn_mul _ _ nii (O.nn_mul _ _ nC1 hZb)))
      (O.nn_mul _ _ (O.nn_mul _ _ n2 n2) (O.nn_mul _ _ nii (O.nn_mul _ _ n135 hAb))))
    (O.nn_mul _ _ n2 (O.nn_add _ _ (O.nn_mul _ _ n2 (O.nn_add _ _
        (O.nn_mul _ _ n2 (O.nn_mul _ _ nii (O.nn_mul _ _ nZh (O.nn_mul _ _ nC1 hZb))))
        (O.nn_mul _ _ n2 (O.nn_mul _ _ nii (O.nn_mul _ _ hAa (O.nn_mul _ _ nC2 hZb))))))
      (O.nn_mul _ _ n2 (O.nn_mul _ _ nii (O.nn_mul _ _ hAa (O.nn_mul _ _ n135 nZh))))))
    A B hA hB n ?_ ?_ ?_ T hT
  · intro t ht
    exact le_add' K O.toCone _ _ _ _
      (le_mul_nn K O.toCone _ _ _ (O.nn_mul _ _ n2 (O.nn_mul _ _ n2 n2)) (le_mul_nn K O.toCone _ _ _ nii
        (le_mul_nn K O.toCone _ _ _ nC1 (hZa t ht))))
      (le_mul_nn K O.toCone _ _ _ (O.nn_mul _ _ n2 n2) (le_mul_nn K O.toCone _ _ _ nii
        (le_mul_nn K O.toCone _ _ _ n135 (hAB t ht))))
  · intro t ht
    exact le_trans' K O.toCone _ _ _ (le_mul_nn K O.toCone _ _ _ (O.nn_mul _ _ n2 (O.nn_mul _ _ n2 n2))
      (le_mul_nn K O.toCone _ _ _ nii (le_mul_nn K O.toCone _ _ _ nC2 (hZa t ht)))) hβc
  · intro t ht
    refine le_mul_nn K O.toCone _ _ _ n2 (le_add' K O.toCone _ _ _ _
      (le_mul_nn K O.toCone _ _ _ n2 (le_add' K O.toCone _ _ _ _
        (le_mul_nn K O.toCone _ _ _ n2 (le_mul_nn K O.toCone _ _ _ nii
          (mm K O _ _ _ _ (hZn _ _) (O.nn_mul _ _ nC1 hZb) (hZh t ht) (le_mul_nn K O.toCone _ _ _ nC1 (hZA t ht)))))
        (le_mul_nn K O.toCone _ _ _ n2 (le_mul_nn K O.toCone _ _ _ nii
          (mm K O _ _ _ _ (hAn _ _) (O.nn_mul _ _ nC2 hZb) (hAh t ht) (le_mul_nn K O.toCone _ _ _ nC2 (hZA t ht)))))))
      (le_mul_nn K O.toCone _ _ _ n2 (le_mul_nn K O.toCone _ _ _ nii
        (mm K O _ _ _ _ (hAn _ _) (O.nn_mul _ _ n135 nZh) (hAA t ht) (le_mul_nn K O.toCone _ _ _ n135 (hZh t ht))))))

end Gen

end TruncRateUpto

#print axioms TruncRateUpto.trunc_rate_upto

ns/TruncShift.lean

lean_all/ns/TruncShift.lean

/-
  ns/TruncShift.lean — T37 cycle 1858 (goal G859a): the time-uniform truncation estimate started at any time t₂.
    trunc_uniform_from   TruncUniform.trunc_uniform for the shifted fine scheme t ↦ A(t₂ + t) and a coarse scheme B
                         started at B_0 = A(t₂)|_N, with the smallness hypotheses at time t₂:
                         ν²·Qe(A(t₂ + n)|_N, B_n) ≤ iR·κ·2Q₂(A, t₂)  for every n
  For large data EventH2/EventH2K give a truncation-free time t₂ after which the H² energy is small; from there on the
  truncation error is O(1/R²) uniformly in time.
  Lean 4 core plus ns/TruncUniform.
-/
import ns.TruncUniform

namespace TruncShift
open DetChain NSChain DyadicSmallGlobal LibRing DyadicLarge FourierGalerkin GalerkinForce ForceBound ForceSob
  ModeCount CubeCount ForceUniform NormCompare EnstStep EnstGlobal EnstGalerkin ForceTwoScale ForceBilin EnstDiff
  ForceDiff TruncError TruncBound RestrStep GronwallSource EnstGronwall WeightSplit LatticeSum TruncCauchy TruncSmall
  TruncStart TruncRate3 TruncStart3 SchemeTele TruncDampSum TruncUniform

section Gen
variable {R : Type} (K : CRing R) (O : OrdR K) {I : Type}
variable (inv : R → R) (mul_inv : ∀ y, y ≠ K.zero → K.mul y (inv y) = K.one) (h10 : K.one ≠ K.zero)

include mul_inv h10 in
theorem trunc_uniform_from (l : List I) (kz : I → Mode) (hd : List.Pairwise (fun a b => kz a ≠ kz b) l)
    (h1 : ∀ p, 1 ≤ nrm (kz p)) (M : Nat) (hM : ∀ p, p ∈ l → nrm (kz p) ≤ M + 1) (Rc R' t0 : Nat) (hRM : R' ≤ M)
    (ι h ν Aa iR : R) (hh : O.nn h) (hν : O.nn ν) (hν0 : ν ≠ K.zero) (hAa : O.nn Aa)
    (hiRn : O.nn iR) (hiR : K.mul (K.mul (castN K (Rc + 1)) (castN K (Rc + 1))) iR = K.one)
    (A B : Nat → I → V3 R) (t2 : Nat)
    (hA : ∀ t r, Step K h ν (wt K kz r) (A (t + 1) r) (A t r) (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (A t) (A t) r))
    (hB : ∀ t r, Step K h ν (wt K kz r) (B (t + 1) r) (B t r)
      (Nl K (l.filter (lowB Rc kz)) (TriadSparse.delta K kz) ι (kv K kz) (B t) (B t) r))
    (hQA : Le K O.toCone (QH K ν h kz l (fun t => A (t2 + t)) 0) (K.mul ν Aa))
    (hB0 : B 0 = restr K (lowB Rc kz) (A t2))
    (hsm : Le K O.toCone (K.add (K.add (K.mul (K.mul (two' K) (K.mul (two' K) (two' K))) (K.mul (K.mul ι ι) (K.mul (C1' K inv R') Aa))) (K.mul (K.mul (two' K) (two' K)) (K.mul (K.mul ι ι) (K.mul (castN K 135) Aa))))
      (K.mul (K.mul (two' K) (K.mul (two' K) (two' K))) (K.mul (K.mul ι ι) (K.mul (C2' K inv R' t0) Aa)))) (K.mul ν ν)) (n : Nat)
    (small2 : Le K O.toCone (K.mul (K.add (K.mul (EnstH2.cH K) (K.mul ι ι)) (K.mul (EnstH2.cH K) (K.mul ι ι))) (QH K ν h kz l (fun t => A (t2 + t)) 0))
      (K.mul ν (K.mul ν ν))) :
    Le K O.toCone (K.mul (K.mul ν ν) (Q K ν h (fun t => Zl K kz (l.filter (lowB Rc kz)) (dF K (restr K (lowB Rc kz) (A (t2 + t))) (B t)))
        (fun t => Al K kz (l.filter (lowB Rc kz)) (dF K (restr K (lowB Rc kz) (A (t2 + t))) (B t))) n))
      (K.mul (K.mul iR (K.mul (two' K) (K.add (K.mul (two' K) (K.add (K.mul (two' K) (K.mul (K.mul ι ι) (K.mul (C1' K inv R') Aa))) (K.mul (two' K) (K.mul (K.mul ι ι) (K.mul (C2' K inv R' t0) Aa))))) (K.mul (two' K) (K.mul (K.mul ι ι) (K.mul Aa (castN K 135))))))) (K.mul (K.add K.one K.one) (QH K ν h kz l (fun t => A (t2 + t)) 0))) :=
  trunc_uniform K O inv mul_inv h10 l kz hd h1 M hM Rc R' t0 hRM ι h ν Aa iR hh hν hν0 hAa hiRn hiR
    (fun t => A (t2 + t)) B (fun t r => hA (t2 + t) r) hB hQA hB0 hsm n small2

end Gen
end TruncShift

#print axioms TruncShift.trunc_uniform_from

ns/TruncSmall.lean

lean_all/ns/TruncSmall.lean

/-
  ns/TruncSmall.lean — T37 cycle 1772 (goal G852g, piece d): the truncation Cauchy estimate for small H² data, with
  every coefficient hypothesis of TruncRate discharged by the uniform H² bound (EnstH2.h2_global). If the l-scheme A and
  the l_N-scheme B (l_N = modes with |k|_∞ ≤ R) both start small, cH·ι²·Q_H2(0) ≤ ν³, and Q_H2(0) ≤ ν·Aa resp. ν·Ab, then
    a_bound      A(u_t) ≤ Aa and Z(u_t) ≤ Aa for all t   (νA_t ≤ Q_H2(t) ≤ Q_H2(0), Z ≤ A)
    trunc_small  ν^T Q_d(T) ≤ (ν + hα)^T (Q_d(0) + T·h·S)  with Zb = Aa in TruncRate's α and S,
  so S = O(Aa²(C₁/(R+1)² + C₂)) → 0 as R → ∞ (and C₂ → 0 by the internal cutoffs), uniformly in the truncation l.
  Lean 4 core plus ns/TruncRate, ns/EnstH2.
-/
import ns.TruncRate
import ns.EnstH2

namespace TruncSmall
open DetChain NSChain DyadicSmallGlobal LibRing DyadicLarge FourierGalerkin GalerkinForce ForceBound ForceSob
  ModeCount CubeCount ForceUniform NormCompare EnstStep EnstGlobal EnstGalerkin ForceTwoScale ForceBilin EnstDiff
  ForceDiff TruncError TruncBound RestrStep GronwallSource EnstGronwall WeightSplit LatticeSum TruncCauchy

section Gen
variable {R : Type} (K : CRing R) (O : OrdR K) {I : Type}
variable (inv : R → R) (mul_inv : ∀ y, y ≠ K.zero → K.mul y (inv y) = K.one) (h10 : K.one ≠ K.zero)

def QH (ν h : R) (kz : I → Mode) (L : List I) (a : Nat → I → V3 R) (t : Nat) : R :=
  Q K ν h (fun t => lsum K (fun p => K.mul (K.mul (wt K kz p) (wt K kz p)) (dot3 K (a t p) (a t p))) L)
    (fun t => lsum K (fun p => K.mul (K.mul (K.mul (wt K kz p) (wt K kz p)) (wt K kz p)) (dot3 K (a t p) (a t p))) L) t

include mul_inv h10 in
theorem a_bound (L : List I) (kz : I → Mode) (hd : List.Pairwise (fun a b => kz a ≠ kz b) L)
    (h1 : ∀ p, 1 ≤ nrm (kz p)) (M : Nat) (hM : ∀ p, p ∈ L → nrm (kz p) ≤ M + 1)
    (ι h ν Aa : R) (hh : O.nn h) (hν : O.nn ν) (hν0 : ν ≠ K.zero) (a : Nat → I → V3 R)
    (hs : ∀ t r, Step K h ν (wt K kz r) (a (t + 1) r) (a t r) (Nl K L (TriadSparse.delta K kz) ι (kv K kz) (a t) (a t) r))
    (small : Le K O.toCone (K.mul (K.mul (EnstH2.cH K) (K.mul ι ι)) (QH K ν h kz L a 0)) (K.mul ν (K.mul ν ν)))
    (hQ : Le K O.toCone (QH K ν h kz L a 0) (K.mul ν Aa)) (t : Nat) :
    Le K O.toCone (Al K kz L (a t)) Aa ∧ Le K O.toCone (Zl K kz L (a t)) Aa := by
  have g := EnstH2.h2_global K O inv mul_inv h10 L kz hd h1 M hM ι h ν hh hν hν0 a
    (fun t r => Nl K L (TriadSparse.delta K kz) ι (kv K kz) (a t) (a t) r) (fun t => le_refl' K O.toCone _) hs small t
  have hB : O.nn (lsum K (fun p => K.mul (K.mul (K.mul (wt K kz p) (wt K kz p)) (wt K kz p)) (dot3 K (a t p) (a t p))) L) :=
    lsum_nn K O.toCone _ (fun p => O.nn_mul _ _ (O.nn_mul _ _ (O.nn_sq _) (dot3_nn K O.toCone _)) (dot3_nn K O.toCone _)) L
  have e1 : Le K O.toCone (K.mul ν (Al K kz L (a t))) (QH K ν h kz L a t) :=
    ⟨_, O.nn_mul _ _ (O.nn_mul _ _ hh (O.nn_sq ν)) hB, rfl⟩
  have hA : Le K O.toCone (Al K kz L (a t)) Aa :=
    EnstGlobal.le_of_mul_le K O ν _ _ hν hν0 (le_trans' K O.toCone _ _ _ e1 (le_trans' K O.toCone _ _ _ g hQ))
  exact ⟨hA, le_trans' K O.toCone _ _ _ (EnstH2.Z_le_A K O kz h1 L (a t)) hA⟩

include mul_inv h10 in
theorem trunc_small (l : List I) (kz : I → Mode) (hd : List.Pairwise (fun a b => kz a ≠ kz b) l)
    (h1 : ∀ p, 1 ≤ nrm (kz p)) (M : Nat) (hM : ∀ p, p ∈ l → nrm (kz p) ≤ M + 1) (Rc R' t0 : Nat) (hRM : R' ≤ M)
    (ι h ν Aa Ab iR : R) (hh : O.nn h) (hν : O.nn ν) (hν0 : ν ≠ K.zero) (hAa : O.nn Aa) (hAb : O.nn Ab)
    (hiRn : O.nn iR) (hiR : K.mul (K.mul (castN K (Rc + 1)) (castN K (Rc + 1))) iR = K.one)
    (A B : Nat → I → V3 R)
    (hA : ∀ t r, Step K h ν (wt K kz r) (A (t + 1) r) (A t r) (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (A t) (A t) r))
    (hB : ∀ t r, Step K h ν (wt K kz r) (B (t + 1) r) (B t r)
      (Nl K (l.filter (lowB Rc kz)) (TriadSparse.delta K kz) ι (kv K kz) (B t) (B t) r))
    (smallA : Le K O.toCone (K.mul (K.mul (EnstH2.cH K) (K.mul ι ι)) (QH K ν h kz l A 0)) (K.mul ν (K.mul ν ν)))
    (hQA : Le K O.toCone (QH K ν h kz l A 0) (K.mul ν Aa))
    (smallB : Le K O.toCone (K.mul (K.mul (EnstH2.cH K) (K.mul ι ι)) (QH K ν h kz (l.filter (lowB Rc kz)) B 0))
      (K.mul ν (K.mul ν ν)))
    (hQB : Le K O.toCone (QH K ν h kz (l.filter (lowB Rc kz)) B 0) (K.mul ν Ab))
    (hβc : Le K O.toCone (K.mul (K.mul (two' K) (K.mul (two' K) (two' K))) (K.mul (K.mul ι ι) (K.mul (C2' K inv R' t0) Aa)))
      (K.mul ν ν)) (T : Nat) :
    Le K O.toCone (K.mul (pwr K ν T)
        (Q K ν h (fun t => Zl K kz (l.filter (lowB Rc kz)) (dF K (restr K (lowB Rc kz) (A t)) (B t)))
          (fun t => Al K kz (l.filter (lowB Rc kz)) (dF K (restr K (lowB Rc kz) (A t)) (B t))) T))
      (K.mul (pwr K (K.add ν (K.mul h
          (K.add (K.mul (K.mul (two' K) (K.mul (two' K) (two' K))) (K.mul (K.mul ι ι) (K.mul (C1' K inv R') Aa)))
            (K.mul (K.mul (two' K) (two' K)) (K.mul (K.mul ι ι) (K.mul (castN K 135) Ab)))))) T)
        (K.add (Q K ν h (fun t => Zl K kz (l.filter (lowB Rc kz)) (dF K (restr K (lowB Rc kz) (A t)) (B t)))
          (fun t => Al K kz (l.filter (lowB Rc kz)) (dF K (restr K (lowB Rc kz) (A t)) (B t))) 0)
          (K.mul (castN K T) (K.mul h (K.mul (two' K)
            (K.add
              (K.mul (two' K) (K.add
                (K.mul (two' K) (K.mul (K.mul ι ι) (K.mul (K.mul Aa iR) (K.mul (C1' K inv R') Aa))))
                (K.mul (two' K) (K.mul (K.mul ι ι) (K.mul Aa (K.mul (C2' K inv R' t0) Aa))))))
              (K.mul (two' K) (K.mul (K.mul ι ι) (K.mul Aa (K.mul (castN K 135) (K.mul Aa iR))))))))))) := by
  have bA := a_bound K O inv mul_inv h10 l kz hd h1 M hM ι h ν Aa hh hν hν0 A hA smallA hQA
  have bB := a_bound K O inv mul_inv h10 (l.filter (lowB Rc kz)) kz (pw_filter kz (lowB Rc kz) l hd) h1 M
    (fun p hp => hM p (mem_of_mem_filter (lowB Rc kz) p l hp)) ι h ν Ab hh hν hν0 B hB smallB hQB
  exact TruncRate.trunc_rate K O inv mul_inv h10 l kz hd h1 M hM Rc R' t0 hRM ι h ν Aa Aa Ab iR hh hν hAa hAa hAb hiRn hiR
    A B hA (fun t r _ => hB t r) (fun t => (bA t).2) (fun t => (bA t).1) (fun t => (bB t).1) hβc T

end Gen
end TruncSmall

#print axioms TruncSmall.a_bound
#print axioms TruncSmall.trunc_small

ns/TruncStart.lean

lean_all/ns/TruncStart.lean

/-
  ns/TruncStart.lean — T37 cycle 1774 (goal G852h): the coarse scheme started at the restriction of the fine data.
  With B_0 = A_0|_N (l_N = modes with |k|_∞ ≤ R):
    W_restr_le   Σ_{l_N} W|u|_N|² ≤ Σ_l W|u|²   for any weight W ≥ 0
    qh_restr     Q_H2(B, 0) ≤ Q_H2(A, 0)        (so B is small whenever A is, with the same Aa)
    qd_zero      Q_d(0) = 0                      (d = A|_N − B)
    trunc_start  ν^T Q_d(T) ≤ (ν + hα)^T · T·h·S  for small H² data A_0 (TruncSmall with Ab = Aa)
  — the coarse truncations are Cauchy with the explicit source S = O(Aa²(C₁/(R+1)² + C₂)), uniformly in l.
  Lean 4 core plus ns/TruncSmall.
-/
import ns.TruncSmall

namespace TruncStart
open DetChain NSChain DyadicSmallGlobal LibRing DyadicLarge FourierGalerkin GalerkinForce ForceBound ForceSob
  ModeCount CubeCount ForceUniform NormCompare EnstStep EnstGlobal EnstGalerkin ForceTwoScale ForceBilin EnstDiff
  ForceDiff TruncError TruncBound RestrStep GronwallSource EnstGronwall WeightSplit LatticeSum TruncCauchy TruncSmall

section Gen
variable {R : Type} (K : CRing R) (O : OrdR K) {I : Type}
variable (inv : R → R) (mul_inv : ∀ y, y ≠ K.zero → K.mul y (inv y) = K.one) (h10 : K.one ≠ K.zero)

theorem W_restr_le (W : I → R) (hW : ∀ p, O.nn (W p)) (l : List I) (inN : I → Bool) (u : I → V3 R) :
    Le K O.toCone (lsum K (fun p => K.mul (W p) (dot3 K (restr K inN u p) (restr K inN u p))) (l.filter inN))
      (lsum K (fun p => K.mul (W p) (dot3 K (u p) (u p))) l) := by
  have h1 := lsum_filter_le K O.toCone inN (fun p => K.mul (W p) (dot3 K (restr K inN u p) (restr K inN u p)))
    (fun p => O.nn_mul _ _ (hW p) (dot3_nn K O.toCone _)) l
  have h2 := lsum_le K O.toCone _ _ (fun p => part_leW K O (W p) (hW p) u p (inN p)) l
  exact le_trans' K O.toCone _ _ _ h1 h2

theorem qh_restr (ν h : R) (hν : O.nn ν) (hh : O.nn h) (kz : I → Mode) (l : List I) (inN : I → Bool)
    (A B : Nat → I → V3 R) (hB0 : B 0 = restr K inN (A 0)) :
    Le K O.toCone (QH K ν h kz (l.filter inN) B 0) (QH K ν h kz l A 0) := by
  have hw : ∀ p, O.nn (wt K kz p) := fun p => dot3_nn K O.toCone _
  show Le K O.toCone (K.add (K.mul ν (lsum K (fun p => K.mul (K.mul (wt K kz p) (wt K kz p)) (dot3 K (B 0 p) (B 0 p))) (l.filter inN)))
      (K.mul (K.mul h (K.mul ν ν)) (lsum K (fun p => K.mul (K.mul (K.mul (wt K kz p) (wt K kz p)) (wt K kz p)) (dot3 K (B 0 p) (B 0 p))) (l.filter inN))))
    (K.add (K.mul ν (lsum K (fun p => K.mul (K.mul (wt K kz p) (wt K kz p)) (dot3 K (A 0 p) (A 0 p))) l))
      (K.mul (K.mul h (K.mul ν ν)) (lsum K (fun p => K.mul (K.mul (K.mul (wt K kz p) (wt K kz p)) (wt K kz p)) (dot3 K (A 0 p) (A 0 p))) l)))
  rw [hB0]
  exact le_add' K O.toCone _ _ _ _
    (le_mul_nn K O.toCone ν _ _ hν (W_restr_le K O (fun p => K.mul (wt K kz p) (wt K kz p)) (fun p => O.nn_sq _) l inN (A 0)))
    (le_mul_nn K O.toCone _ _ _ (O.nn_mul _ _ hh (O.nn_sq ν))
      (W_restr_le K O (fun p => K.mul (K.mul (wt K kz p) (wt K kz p)) (wt K kz p))
        (fun p => O.nn_mul _ _ (O.nn_sq _) (hw p)) l inN (A 0)))

theorem lsum_self (W : I → R) (x : I → V3 R) :
    ∀ L : List I, lsum K (fun p => K.mul (W p) (dot3 K (dF K x x p) (dF K x x p))) L = K.zero
  | [] => rfl
  | p :: L => by
    show K.add (K.mul (W p) (K.add (K.add
        (K.mul (K.add (x p).x (K.neg (x p).x)) (K.add (x p).x (K.neg (x p).x)))
        (K.mul (K.add (x p).y (K.neg (x p).y)) (K.add (x p).y (K.neg (x p).y))))
        (K.mul (K.add (x p).z (K.neg (x p).z)) (K.add (x p).z (K.neg (x p).z)))))
      (lsum K (fun p => K.mul (W p) (dot3 K (dF K x x p) (dF K x x p))) L) = K.zero
    rw [lsum_self W x L]
    lib_ring

theorem qd_zero (ν h : R) (kz : I → Mode) (L : List I) (inN : I → Bool) (A B : Nat → I → V3 R)
    (hB0 : B 0 = restr K inN (A 0)) :
    Q K ν h (fun t => Zl K kz L (dF K (restr K inN (A t)) (B t)))
      (fun t => Al K kz L (dF K (restr K inN (A t)) (B t))) 0 = K.zero := by
  show K.add (K.mul ν (Zl K kz L (dF K (restr K inN (A 0)) (B 0))))
    (K.mul (K.mul h (K.mul ν ν)) (Al K kz L (dF K (restr K inN (A 0)) (B 0)))) = K.zero
  rw [hB0]
  show K.add (K.mul ν (lsum K (fun p => K.mul (wt K kz p) (dot3 K (dF K (restr K inN (A 0)) (restr K inN (A 0)) p)
      (dF K (restr K inN (A 0)) (restr K inN (A 0)) p))) L))
    (K.mul (K.mul h (K.mul ν ν)) (lsum K (fun p => K.mul (K.mul (wt K kz p) (wt K kz p))
      (dot3 K (dF K (restr K inN (A 0)) (restr K inN (A 0)) p) (dF K (restr K inN (A 0)) (restr K inN (A 0)) p))) L)) = K.zero
  rw [lsum_self K (wt K kz) _ L, lsum_self K (fun p => K.mul (wt K kz p) (wt K kz p)) _ L]
  lib_ring

theorem zero_add' (x : R) : K.add K.zero x = x := by lib_ring

include mul_inv h10 in
theorem trunc_start (l : List I) (kz : I → Mode) (hd : List.Pairwise (fun a b => kz a ≠ kz b) l)
    (h1 : ∀ p, 1 ≤ nrm (kz p)) (M : Nat) (hM : ∀ p, p ∈ l → nrm (kz p) ≤ M + 1) (Rc R' t0 : Nat) (hRM : R' ≤ M)
    (ι h ν Aa iR : R) (hh : O.nn h) (hν : O.nn ν) (hν0 : ν ≠ K.zero) (hAa : O.nn Aa)
    (hiRn : O.nn iR) (hiR : K.mul (K.mul (castN K (Rc + 1)) (castN K (Rc + 1))) iR = K.one)
    (A B : Nat → I → V3 R)
    (hA : ∀ t r, Step K h ν (wt K kz r) (A (t + 1) r) (A t r) (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (A t) (A t) r))
    (hB : ∀ t r, Step K h ν (wt K kz r) (B (t + 1) r) (B t r)
      (Nl K (l.filter (lowB Rc kz)) (TriadSparse.delta K kz) ι (kv K kz) (B t) (B t) r))
    (smallA : Le K O.toCone (K.mul (K.mul (EnstH2.cH K) (K.mul ι ι)) (QH K ν h kz l A 0)) (K.mul ν (K.mul ν ν)))
    (hQA : Le K O.toCone (QH K ν h kz l A 0) (K.mul ν Aa))
    (hB0 : B 0 = restr K (lowB Rc kz) (A 0))
    (hβc : Le K O.toCone (K.mul (K.mul (two' K) (K.mul (two' K) (two' K))) (K.mul (K.mul ι ι) (K.mul (C2' K inv R' t0) Aa)))
      (K.mul ν ν)) (T : Nat) :
    Le K O.toCone (K.mul (pwr K ν T)
        (Q K ν h (fun t => Zl K kz (l.filter (lowB Rc kz)) (dF K (restr K (lowB Rc kz) (A t)) (B t)))
          (fun t => Al K kz (l.filter (lowB Rc kz)) (dF K (restr K (lowB Rc kz) (A t)) (B t))) T))
      (K.mul (pwr K (K.add ν (K.mul h
          (K.add (K.mul (K.mul (two' K) (K.mul (two' K) (two' K))) (K.mul (K.mul ι ι) (K.mul (C1' K inv R') Aa)))
            (K.mul (K.mul (two' K) (two' K)) (K.mul (K.mul ι ι) (K.mul (castN K 135) Aa)))))) T)
        (K.mul (castN K T) (K.mul h (K.mul (two' K)
            (K.add
              (K.mul (two' K) (K.add
                (K.mul (two' K) (K.mul (K.mul ι ι) (K.mul (K.mul Aa iR) (K.mul (C1' K inv R') Aa))))
                (K.mul (two' K) (K.mul (K.mul ι ι) (K.mul Aa (K.mul (C2' K inv R' t0) Aa))))))
              (K.mul (two' K) (K.mul (K.mul ι ι) (K.mul Aa (K.mul (castN K 135) (K.mul Aa iR)))))))))) := by
  have q := qh_restr K O ν h hν hh kz l (lowB Rc kz) A B hB0
  have two : O.nn (K.add K.one K.one) := O.nn_add _ _ (RatModel.nn_one' K O.toCone) (RatModel.nn_one' K O.toCone)
  have three : O.nn (K.add (K.add K.one K.one) K.one) := O.nn_add _ _ two (RatModel.nn_one' K O.toCone)
  have n135 : O.nn (castN K 135) := RatModel.nn_cast K O.toCone 135
  have ncH : O.nn (EnstH2.cH K) := O.nn_add _ _ (O.nn_mul _ _ two (O.nn_mul _ _ three n135)) (O.nn_mul _ _ two n135)
  have smallB := le_trans' K O.toCone _ _ _ (le_mul_nn K O.toCone _ _ _ (O.nn_mul _ _ ncH (O.nn_sq ι)) q) smallA
  have t := trunc_small K O inv mul_inv h10 l kz hd h1 M hM Rc R' t0 hRM ι h ν Aa Aa iR hh hν hν0 hAa hAa hiRn hiR A B
    hA hB smallA hQA smallB (le_trans' K O.toCone _ _ _ q hQA) hβc T
  rw [qd_zero K ν h kz (l.filter (lowB Rc kz)) (lowB Rc kz) A B hB0, zero_add' K] at t
  exact t

end Gen
end TruncStart

#print axioms TruncStart.W_restr_le
#print axioms TruncStart.qh_restr
#print axioms TruncStart.lsum_self
#print axioms TruncStart.qd_zero
#print axioms TruncStart.zero_add'
#print axioms TruncStart.trunc_start

ns/TruncStart3.lean

lean_all/ns/TruncStart3.lean

/-
  ns/TruncStart3.lean — T37 cycle 1780 (goal G852j): TruncRate3 for small H² and H³ data, with the coarse scheme
  started at the restriction B_0 = A_0|_N. Q₃ = νB + hν²B₄ (B = Σw³|a|², B₄ = Σw⁴|a|²):
    b_bound       c₃ι²Q₃(0) ≤ ν³, Q₃(0) ≤ ν·Bb  ⇒  B(A_t) ≤ Bb for all t   (EnstH3.h3_global)
    trunc_start3  with the H² hypotheses of TruncStart as well:
                  ν^T Q_d(T) ≤ (ν + hα)^T · T·h·S,  every term of S carrying 1/(R+1)² (Zb = Ab = Aa),
  uniformly in the fine truncation l; α depends on R', Aa only.
  Lean 4 core plus ns/TruncRate3, ns/TruncStart, ns/EnstH3.
-/
import ns.TruncRate3
import ns.TruncStart
import ns.EnstH3

namespace TruncStart3
open DetChain NSChain DyadicSmallGlobal LibRing DyadicLarge FourierGalerkin GalerkinForce ForceBound ForceSob
  ModeCount CubeCount ForceUniform NormCompare EnstStep EnstGlobal EnstGalerkin ForceTwoScale ForceBilin EnstDiff
  ForceDiff TruncError TruncBound RestrStep GronwallSource EnstGronwall WeightSplit LatticeSum TruncCauchy TruncSmall
  TruncStart TruncRate3

section Gen
variable {R : Type} (K : CRing R) (O : OrdR K) {I : Type}
variable (inv : R → R) (mul_inv : ∀ y, y ≠ K.zero → K.mul y (inv y) = K.one) (h10 : K.one ≠ K.zero)

def QH3 (ν h : R) (kz : I → Mode) (L : List I) (a : Nat → I → V3 R) (t : Nat) : R :=
  Q K ν h (fun t => lsum K (fun p => K.mul (K.mul (wt K kz p) (K.mul (wt K kz p) (wt K kz p))) (dot3 K (a t p) (a t p))) L)
    (fun t => lsum K (fun p => K.mul (K.mul (K.mul (wt K kz p) (wt K kz p)) (K.mul (wt K kz p) (wt K kz p)))
      (dot3 K (a t p) (a t p))) L) t

include mul_inv h10 in
theorem b_bound (L : List I) (kz : I → Mode) (hd : List.Pairwise (fun a b => kz a ≠ kz b) L)
    (h1 : ∀ p, 1 ≤ nrm (kz p)) (M : Nat) (hM : ∀ p, p ∈ L → nrm (kz p) ≤ M + 1)
    (ι h ν Bb : R) (hh : O.nn h) (hν : O.nn ν) (hν0 : ν ≠ K.zero) (a : Nat → I → V3 R)
    (hs : ∀ t r, Step K h ν (wt K kz r) (a (t + 1) r) (a t r) (Nl K L (TriadSparse.delta K kz) ι (kv K kz) (a t) (a t) r))
    (small : Le K O.toCone (K.mul (K.mul (EnstH3.c3 K) (K.mul ι ι)) (QH3 K ν h kz L a 0)) (K.mul ν (K.mul ν ν)))
    (hQ : Le K O.toCone (QH3 K ν h kz L a 0) (K.mul ν Bb)) (t : Nat) :
    Le K O.toCone (Bl K kz L (a t)) Bb := by
  have g := EnstH3.h3_global K O inv mul_inv h10 L kz hd h1 M hM ι h ν hh hν hν0 a
    (fun t r => Nl K L (TriadSparse.delta K kz) ι (kv K kz) (a t) (a t) r) (fun t => le_refl' K O.toCone _) hs small t
  have hw : ∀ r, O.nn (wt K kz r) := fun r => dot3_nn K O.toCone _
  have hB4 : O.nn (lsum K (fun p => K.mul (K.mul (K.mul (wt K kz p) (wt K kz p)) (K.mul (wt K kz p) (wt K kz p)))
      (dot3 K (a t p) (a t p))) L) :=
    lsum_nn K O.toCone _ (fun p => O.nn_mul _ _ (O.nn_mul _ _ (O.nn_sq _) (O.nn_sq _)) (dot3_nn K O.toCone _)) L
  have e1 : Le K O.toCone (K.mul ν (lsum K (fun p => K.mul (K.mul (wt K kz p) (K.mul (wt K kz p) (wt K kz p)))
      (dot3 K (a t p) (a t p))) L)) (QH3 K ν h kz L a t) :=
    ⟨_, O.nn_mul _ _ (O.nn_mul _ _ hh (O.nn_sq ν)) hB4, rfl⟩
  have hB := EnstGlobal.le_of_mul_le K O ν _ _ hν hν0 (le_trans' K O.toCone _ _ _ e1 (le_trans' K O.toCone _ _ _ g hQ))
  show Le K O.toCone (lsum K (fun p => K.mul (K.mul (K.mul (wt K kz p) (wt K kz p)) (wt K kz p)) (dot3 K (a t p) (a t p))) L) Bb
  rw [EnstH3.Bform K kz L (a t)]
  exact hB

include mul_inv h10 in
theorem trunc_start3 (l : List I) (kz : I → Mode) (hd : List.Pairwise (fun a b => kz a ≠ kz b) l)
    (h1 : ∀ p, 1 ≤ nrm (kz p)) (M : Nat) (hM : ∀ p, p ∈ l → nrm (kz p) ≤ M + 1) (Rc R' t0 : Nat) (hRM : R' ≤ M)
    (ι h ν Aa Bb iR : R) (hh : O.nn h) (hν : O.nn ν) (hν0 : ν ≠ K.zero) (hAa : O.nn Aa) (hBb : O.nn Bb)
    (hiRn : O.nn iR) (hiR : K.mul (K.mul (castN K (Rc + 1)) (castN K (Rc + 1))) iR = K.one)
    (A B : Nat → I → V3 R)
    (hA : ∀ t r, Step K h ν (wt K kz r) (A (t + 1) r) (A t r) (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (A t) (A t) r))
    (hB : ∀ t r, Step K h ν (wt K kz r) (B (t + 1) r) (B t r)
      (Nl K (l.filter (lowB Rc kz)) (TriadSparse.delta K kz) ι (kv K kz) (B t) (B t) r))
    (smallA : Le K O.toCone (K.mul (K.mul (EnstH2.cH K) (K.mul ι ι)) (QH K ν h kz l A 0)) (K.mul ν (K.mul ν ν)))
    (hQA : Le K O.toCone (QH K ν h kz l A 0) (K.mul ν Aa))
    (small3 : Le K O.toCone (K.mul (K.mul (EnstH3.c3 K) (K.mul ι ι)) (QH3 K ν h kz l A 0)) (K.mul ν (K.mul ν ν)))
    (hQ3 : Le K O.toCone (QH3 K ν h kz l A 0) (K.mul ν Bb))
    (hB0 : B 0 = restr K (lowB Rc kz) (A 0))
    (hβc : Le K O.toCone (K.mul (K.mul (two' K) (K.mul (two' K) (two' K))) (K.mul (K.mul ι ι) (K.mul (C2' K inv R' t0) Aa)))
      (K.mul ν ν)) (T : Nat) :
    Le K O.toCone (K.mul (pwr K ν T)
        (Q K ν h (fun t => Zl K kz (l.filter (lowB Rc kz)) (dF K (restr K (lowB Rc kz) (A t)) (B t)))
          (fun t => Al K kz (l.filter (lowB Rc kz)) (dF K (restr K (lowB Rc kz) (A t)) (B t))) T))
      (K.mul (pwr K (K.add ν (K.mul h
          (K.add (K.mul (K.mul (two' K) (K.mul (two' K) (two' K))) (K.mul (K.mul ι ι) (K.mul (C1' K inv R') Aa)))
            (K.mul (K.mul (two' K) (two' K)) (K.mul (K.mul ι ι) (K.mul (castN K 135) Aa)))))) T)
        (K.mul (castN K T) (K.mul h (K.mul (two' K)
            (K.add
              (K.mul (two' K) (K.add
                (K.mul (two' K) (K.mul (K.mul ι ι) (K.mul (K.mul Aa iR) (K.mul (C1' K inv R') Aa))))
                (K.mul (two' K) (K.mul (K.mul ι ι) (K.mul (K.mul Bb iR) (K.mul (C2' K inv R' t0) Aa))))))
              (K.mul (two' K) (K.mul (K.mul ι ι) (K.mul Aa (K.mul (castN K 135) (K.mul Aa iR)))))))))) := by
  have q := qh_restr K O ν h hν hh kz l (lowB Rc kz) A B hB0
  have two : O.nn (K.add K.one K.one) := O.nn_add _ _ (RatModel.nn_one' K O.toCone) (RatModel.nn_one' K O.toCone)
  have three : O.nn (K.add (K.add K.one K.one) K.one) := O.nn_add _ _ two (RatModel.nn_one' K O.toCone)
  have n135 : O.nn (castN K 135) := RatModel.nn_cast K O.toCone 135
  have ncH : O.nn (EnstH2.cH K) := O.nn_add _ _ (O.nn_mul _ _ two (O.nn_mul _ _ three n135)) (O.nn_mul _ _ two n135)
  have smallB := le_trans' K O.toCone _ _ _ (le_mul_nn K O.toCone _ _ _ (O.nn_mul _ _ ncH (O.nn_sq ι)) q) smallA
  have bA := a_bound K O inv mul_inv h10 l kz hd h1 M hM ι h ν Aa hh hν hν0 A hA smallA hQA
  have bB := a_bound K O inv mul_inv h10 (l.filter (lowB Rc kz)) kz (pw_filter kz (lowB Rc kz) l hd) h1 M
    (fun p hp => hM p (mem_of_mem_filter (lowB Rc kz) p l hp)) ι h ν Aa hh hν hν0 B hB smallB (le_trans' K O.toCone _ _ _ q hQA)
  have b3 := b_bound K O inv mul_inv h10 l kz hd h1 M hM ι h ν Bb hh hν hν0 A hA small3 hQ3
  have t := trunc_rate3 K O inv mul_inv h10 l kz hd h1 M hM Rc R' t0 hRM ι h ν Aa Aa Aa iR hh hν hAa hAa hAa hiRn hiR
    A B hA (fun t r _ => hB t r) (fun t => (bA t).2) (fun t => (bA t).1) (fun t => (bB t).1) Bb hBb b3 hβc T
  rw [qd_zero K ν h kz (l.filter (lowB Rc kz)) (lowB Rc kz) A B hB0, zero_add' K] at t
  exact t

end Gen
end TruncStart3

#print axioms TruncStart3.b_bound
#print axioms TruncStart3.trunc_start3

ns/TruncStep.lean

lean_all/ns/TruncStep.lean

/-
  ns/TruncStep.lean — T37 cycle 1808 (goal G857c, piece a): the one-step inequality inside TruncCauchy.trunc_cauchy,
  exposed as its own theorem (same proof): for the restriction of the l-scheme A to l_N = l.filter (|k|_∞ ≤ R) against
  the l_N-scheme B, with d = A|_N − B,
    trunc_step   Q_d(s + 1) ≤ νZ_d(s) + h(α_s Z_d(s) + β_s A_d(s)) + h·2·EB(A_s)
  (α_s = alph, β_s = bet, EB the truncation-error bound of TruncBound). TruncCauchy feeds it to the exponential
  Gronwall; DampGronwall uses it without exponential growth when α_s + β_s ≤ ν².
  Lean 4 core plus ns/TruncCauchy.
-/
import ns.TruncCauchy

namespace TruncStep
open DetChain NSChain DyadicSmallGlobal LibRing DyadicLarge FourierGalerkin GalerkinForce ForceBound ForceSob
  ModeCount CubeCount ForceUniform NormCompare EnstStep EnstGlobal EnstGalerkin ForceTwoScale ForceBilin EnstDiff
  ForceDiff TruncError TruncBound RestrStep GronwallSource EnstGronwall WeightSplit LatticeSum TruncCauchy

section Main
variable {R : Type} (K : CRing R) (O : OrdR K) {I : Type}
variable (inv : R → R) (mul_inv : ∀ y, y ≠ K.zero → K.mul y (inv y) = K.one) (h10 : K.one ≠ K.zero)

set_option maxRecDepth 200000 in
include mul_inv h10 in
theorem trunc_step (l : List I) (kz : I → Mode) (hd : List.Pairwise (fun a b => kz a ≠ kz b) l)
    (h1 : ∀ p, 1 ≤ nrm (kz p)) (M : Nat) (hM : ∀ p, p ∈ l → nrm (kz p) ≤ M + 1) (Rc R' t0 : Nat) (hRM : R' ≤ M)
    (ι h ν : R) (hh : O.nn h) (hν : O.nn ν) (A B : Nat → I → V3 R)
    (hA : ∀ t r, Step K h ν (wt K kz r) (A (t + 1) r) (A t r) (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (A t) (A t) r))
    (hB : ∀ t r, r ∈ l.filter (lowB Rc kz) → Step K h ν (wt K kz r) (B (t + 1) r) (B t r)
      (Nl K (l.filter (lowB Rc kz)) (TriadSparse.delta K kz) ι (kv K kz) (B t) (B t) r))
    (s : Nat) :
    Le K O.toCone (Q K ν h (fun t => Zl K kz (l.filter (lowB Rc kz)) (dF K (restr K (lowB Rc kz) (A t)) (B t)))
        (fun t => Al K kz (l.filter (lowB Rc kz)) (dF K (restr K (lowB Rc kz) (A t)) (B t))) (s + 1))
      (K.add (K.add (K.mul ν (Zl K kz (l.filter (lowB Rc kz)) (dF K (restr K (lowB Rc kz) (A s)) (B s))))
        (K.mul h (K.add (K.mul (alph K inv kz (l.filter (lowB Rc kz)) R' ι (restr K (lowB Rc kz) (A s)) (B s)) (Zl K kz (l.filter (lowB Rc kz)) (dF K (restr K (lowB Rc kz) (A s)) (B s))))
          (K.mul (bet K inv kz (l.filter (lowB Rc kz)) R' t0 ι (restr K (lowB Rc kz) (A s))) (Al K kz (l.filter (lowB Rc kz)) (dF K (restr K (lowB Rc kz) (A s)) (B s)))))))
        (K.mul h (K.mul (two' K) (EB K inv kz l Rc R' t0 ι (A s))))) := by
  have hw : ∀ r, O.nn (wt K kz r) := fun r => dot3_nn K O.toCone _
  have hZn : ∀ (L : List I) (u : I → V3 R), O.nn (Zl K kz L u) := fun L u =>
    lsum_nn K O.toCone _ (fun r => O.nn_mul _ _ (hw r) (dot3_nn K O.toCone _)) L
  have hAn : ∀ (L : List I) (u : I → V3 R), O.nn (Al K kz L u) := fun L u =>
    lsum_nn K O.toCone _ (fun r => O.nn_mul _ _ (O.nn_sq _) (dot3_nn K O.toCone _)) L
  have n2 : O.nn (two' K) := O.nn_add _ _ (RatModel.nn_one' K O.toCone) (RatModel.nn_one' K O.toCone)
  have hiv : O.nn (iv K inv (R' + 1)) := iv_nn K O inv mul_inv h10 R'
  have nC1 : O.nn (C1' K inv R') := O.nn_add _ _ (O.nn_mul _ _ (RatModel.nn_cast K O.toCone _) (RatModel.nn_one' K O.toCone))
    (O.nn_mul _ _ (RatModel.nn_cast K O.toCone _) hiv)
  have nC2 : O.nn (C2' K inv R' t0) := O.nn_add _ _ (O.nn_mul _ _ (RatModel.nn_cast K O.toCone _) (O.nn_sq _))
    (O.nn_mul _ _ (RatModel.nn_cast K O.toCone _) hiv)
  have hdN := pw_filter kz (lowB Rc kz) l hd
  have hMN : ∀ p, p ∈ l.filter (lowB Rc kz) → nrm (kz p) ≤ M + 1 := fun p hp => hM p (mem_of_mem_filter (lowB Rc kz) p l hp)
  have hsd : ∀ r, r ∈ l.filter (lowB Rc kz) → Step K h ν (wt K kz r)
      (dF K (restr K (lowB Rc kz) (A (s + 1))) (B (s + 1)) r) (dF K (restr K (lowB Rc kz) (A s)) (B s) r)
      (sub3 K (add3 K (Nl K (l.filter (lowB Rc kz)) (TriadSparse.delta K kz) ι (kv K kz) (restr K (lowB Rc kz) (A s)) (restr K (lowB Rc kz) (A s)) r)
          (sub3 K (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (A s) (A s) r)
            (Nl K (l.filter (lowB Rc kz)) (TriadSparse.delta K kz) ι (kv K kz) (A s) (A s) r)))
        (Nl K (l.filter (lowB Rc kz)) (TriadSparse.delta K kz) ι (kv K kz) (B s) (B s) r)) := fun r hr =>
    step_sub K h ν (wt K kz r) _ _ _ _ _ _
      (restr_step K l (lowB Rc kz) (TriadSparse.delta K kz) ι h ν (wt K kz) (kv K kz) A hA s r
        (mem_filter_true (lowB Rc kz) r l hr)) (hB s r hr)
  have es := enst_step_on K O.toCone (l.filter (lowB Rc kz)) h ν hh hν (wt K kz) hw _ _ _ hsd
  -- |G|² ≤ 2|X|² + 2|E|²
  have hG := lsum_le_on K O.toCone (l.filter (lowB Rc kz)) (fun r _ => sq2' K O
    (sub3 K (add3 K (Nl K (l.filter (lowB Rc kz)) (TriadSparse.delta K kz) ι (kv K kz) (restr K (lowB Rc kz) (A s)) (restr K (lowB Rc kz) (A s)) r)
          (sub3 K (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (A s) (A s) r)
            (Nl K (l.filter (lowB Rc kz)) (TriadSparse.delta K kz) ι (kv K kz) (A s) (A s) r)))
        (Nl K (l.filter (lowB Rc kz)) (TriadSparse.delta K kz) ι (kv K kz) (B s) (B s) r))
    (sub3 K (Nl K (l.filter (lowB Rc kz)) (TriadSparse.delta K kz) ι (kv K kz) (restr K (lowB Rc kz) (A s)) (restr K (lowB Rc kz) (A s)) r)
        (Nl K (l.filter (lowB Rc kz)) (TriadSparse.delta K kz) ι (kv K kz) (B s) (B s) r))
    (sub3 K (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (A s) (A s) r)
        (Nl K (l.filter (lowB Rc kz)) (TriadSparse.delta K kz) ι (kv K kz) (A s) (A s) r))
    (by show K.add (K.add _ (K.add _ (K.neg _))) (K.neg _) = K.add (K.add _ (K.neg _)) (K.add _ (K.neg _)); lib_ring)
    (by show K.add (K.add _ (K.add _ (K.neg _))) (K.neg _) = K.add (K.add _ (K.neg _)) (K.add _ (K.neg _)); lib_ring)
    (by show K.add (K.add _ (K.add _ (K.neg _))) (K.neg _) = K.add (K.add _ (K.neg _)) (K.add _ (K.neg _)); lib_ring))
  rw [lsum_add, lsum_mul_left, lsum_mul_left] at hG
  have fd := force_diff K O inv mul_inv h10 (l.filter (lowB Rc kz)) kz hdN h1 M hMN R' t0 hRM ι (restr K (lowB Rc kz) (A s)) (B s)
  have eb0 := lsum_filter_le K O.toCone (lowB Rc kz)
    (fun r => dot3 K (sub3 K (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (A s) (A s) r)
        (Nl K (l.filter (lowB Rc kz)) (TriadSparse.delta K kz) ι (kv K kz) (A s) (A s) r))
      (sub3 K (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (A s) (A s) r)
        (Nl K (l.filter (lowB Rc kz)) (TriadSparse.delta K kz) ι (kv K kz) (A s) (A s) r))) (fun _ => dot3_nn K O.toCone _) l
  have eb := le_trans' K O.toCone _ _ _ eb0 (err_bound K O inv mul_inv h10 l kz hd h1 M hM Rc R' t0 hRM ι (A s))
  have sum := le_add' K O.toCone _ _ _ _ (le_mul_nn K O.toCone (two' K) _ _ n2 fd) (le_mul_nn K O.toCone (two' K) _ _ n2 eb)
  have hb := le_mul_nn K O.toCone h _ _ hh (le_trans' K O.toCone _ _ _ hG sum)
  have st := le_trans' K O.toCone _ _ _ es (le_add' K O.toCone _ _ _ _ (le_refl' K O.toCone _) hb)
  refine le_trans' K O.toCone _ _ _ st ⟨K.zero, O.nn_zero, ?_⟩
  unfold alph bet EB two' Zl Al C1' C2'
  generalize castN K 135 = c135
  generalize castN K (cube R') = cb
  generalize iv K inv (R' + 1) = ivR
  generalize inv (castN K (t0 + 1)) = it
  generalize lsum K (fun p => K.mul (wt K kz p) (dot3 K (dF K (restr K (lowB Rc kz) (A s)) (B s) p)
    (dF K (restr K (lowB Rc kz) (A s)) (B s) p))) (List.filter (lowB Rc kz) l) = Zd
  generalize lsum K (fun p => K.mul (K.mul (wt K kz p) (wt K kz p)) (dot3 K (dF K (restr K (lowB Rc kz) (A s)) (B s) p)
    (dF K (restr K (lowB Rc kz) (A s)) (B s) p))) (List.filter (lowB Rc kz) l) = Ad
  generalize lsum K (fun q => K.mul (wt K kz q) (dot3 K (restr K (lowB Rc kz) (A s) q) (restr K (lowB Rc kz) (A s) q)))
    (List.filter (lowB Rc kz) l) = Za
  generalize lsum K (fun p => K.mul (K.mul (wt K kz p) (wt K kz p)) (dot3 K (B s p) (B s p))) (List.filter (lowB Rc kz) l) = AB
  generalize lsum K (fun p => K.mul (wt K kz p) (dot3 K (hiF K (lowB Rc kz) (A s) p) (hiF K (lowB Rc kz) (A s) p))) l = Zh
  generalize lsum K (fun p => K.mul (K.mul (wt K kz p) (wt K kz p)) (dot3 K (hiF K (lowB Rc kz) (A s) p) (hiF K (lowB Rc kz) (A s) p))) l = Ah
  generalize lsum K (fun q => K.mul (wt K kz q) (dot3 K (A s q) (A s q))) l = ZA
  generalize lsum K (fun p => K.mul (K.mul (wt K kz p) (wt K kz p)) (dot3 K (A s p) (A s p))) l = AA
  lib_ring

end Main
end TruncStep

#print axioms TruncStep.trunc_step

ns/TruncTri.lean

lean_all/ns/TruncTri.lean

/-
  ns/TruncTri.lean — T37 cycle 1788 (goal G852l): the triangle step for the truncation Cauchy modulus. For fields
  u, v, w on a mode list L and Q(x) = νZ(x) + hν²A(x) of a difference:
    tri3      |u − w|² ≤ 2|u − v|² + 2|w − v|²   (remainder |u − 2v + w|²)
    tri_W     the same for Σ_L W|·|² with any weight W ≥ 0
    tri_Q     Q(u − w) ≤ 2Q(u − v) + 2Q(w − v)
    tri_eps   ν^T·Q(u − v) ≤ ε, ν^T·Q(w − v) ≤ ε  ⇒  ν^T·Q(u − w) ≤ 4ε
  With TruncEps (v = the coarse R-scheme, u, w = restrictions of two fine truncations with the same low modes)
  the restricted fine truncations are within 4/(j+1) of each other: the Cauchy property in the fine truncation.
  Lean 4 core plus ns/TruncEps.
-/
import ns.TruncEps

namespace TruncTri
open DetChain NSChain DyadicSmallGlobal LibRing DyadicLarge FourierGalerkin GalerkinForce ForceBound ForceSob
  ModeCount CubeCount ForceUniform NormCompare EnstStep EnstGlobal EnstGalerkin ForceTwoScale ForceBilin EnstDiff
  ForceDiff TruncError TruncBound RestrStep GronwallSource EnstGronwall WeightSplit LatticeSum TruncCauchy

section Gen
variable {R : Type} (K : CRing R) (O : OrdR K) {I : Type}

theorem tri3 (u v w : V3 R) :
    Le K O.toCone (dot3 K (sub3 K u w) (sub3 K u w))
      (K.add (K.mul (two' K) (dot3 K (sub3 K u v) (sub3 K u v))) (K.mul (two' K) (dot3 K (sub3 K w v) (sub3 K w v)))) := by
  refine ⟨K.add (K.add
      (K.mul (K.add (K.add u.x w.x) (K.neg (K.add v.x v.x))) (K.add (K.add u.x w.x) (K.neg (K.add v.x v.x))))
      (K.mul (K.add (K.add u.y w.y) (K.neg (K.add v.y v.y))) (K.add (K.add u.y w.y) (K.neg (K.add v.y v.y)))))
      (K.mul (K.add (K.add u.z w.z) (K.neg (K.add v.z v.z))) (K.add (K.add u.z w.z) (K.neg (K.add v.z v.z)))),
    O.nn_add _ _ (O.nn_add _ _ (O.nn_sq _) (O.nn_sq _)) (O.nn_sq _), ?_⟩
  show K.add (K.add (K.add (K.mul (K.add u.x (K.neg w.x)) (K.add u.x (K.neg w.x)))
        (K.mul (K.add u.y (K.neg w.y)) (K.add u.y (K.neg w.y)))) (K.mul (K.add u.z (K.neg w.z)) (K.add u.z (K.neg w.z))))
      (K.add (K.add
        (K.mul (K.add (K.add u.x w.x) (K.neg (K.add v.x v.x))) (K.add (K.add u.x w.x) (K.neg (K.add v.x v.x))))
        (K.mul (K.add (K.add u.y w.y) (K.neg (K.add v.y v.y))) (K.add (K.add u.y w.y) (K.neg (K.add v.y v.y)))))
        (K.mul (K.add (K.add u.z w.z) (K.neg (K.add v.z v.z))) (K.add (K.add u.z w.z) (K.neg (K.add v.z v.z)))))
    = K.add (K.mul (K.add K.one K.one) (K.add (K.add (K.mul (K.add u.x (K.neg v.x)) (K.add u.x (K.neg v.x)))
          (K.mul (K.add u.y (K.neg v.y)) (K.add u.y (K.neg v.y)))) (K.mul (K.add u.z (K.neg v.z)) (K.add u.z (K.neg v.z)))))
        (K.mul (K.add K.one K.one) (K.add (K.add (K.mul (K.add w.x (K.neg v.x)) (K.add w.x (K.neg v.x)))
          (K.mul (K.add w.y (K.neg v.y)) (K.add w.y (K.neg v.y)))) (K.mul (K.add w.z (K.neg v.z)) (K.add w.z (K.neg v.z)))))
  lib_ring

theorem tri_W (W : I → R) (hW : ∀ p, O.nn (W p)) (u v w : I → V3 R) (L : List I) :
    Le K O.toCone (lsum K (fun p => K.mul (W p) (dot3 K (dF K u w p) (dF K u w p))) L)
      (K.add (K.mul (two' K) (lsum K (fun p => K.mul (W p) (dot3 K (dF K u v p) (dF K u v p))) L))
        (K.mul (two' K) (lsum K (fun p => K.mul (W p) (dot3 K (dF K w v p) (dF K w v p))) L))) := by
  have s := lsum_le K O.toCone _ _ (fun p => le_trans' K O.toCone _ _ _ (le_mul_nn K O.toCone (W p) _ _ (hW p) (tri3 K O (u p) (v p) (w p)))
    (⟨K.zero, O.nn_zero, (by lib_ring : K.add (K.mul (W p) (K.add (K.mul (two' K) (dot3 K (dF K u v p) (dF K u v p)))
        (K.mul (two' K) (dot3 K (dF K w v p) (dF K w v p))))) K.zero
      = K.add (K.mul (two' K) (K.mul (W p) (dot3 K (dF K u v p) (dF K u v p))))
        (K.mul (two' K) (K.mul (W p) (dot3 K (dF K w v p) (dF K w v p)))))⟩)) L
  rw [lsum_add, lsum_mul_left, lsum_mul_left] at s
  exact s

theorem tri_Q (ν h : R) (hν : O.nn ν) (hh : O.nn h) (kz : I → Mode) (L : List I) (u v w : Nat → I → V3 R) (T : Nat) :
    Le K O.toCone (Q K ν h (fun t => Zl K kz L (dF K (u t) (w t))) (fun t => Al K kz L (dF K (u t) (w t))) T)
      (K.add (K.mul (two' K) (Q K ν h (fun t => Zl K kz L (dF K (u t) (v t))) (fun t => Al K kz L (dF K (u t) (v t))) T))
        (K.mul (two' K) (Q K ν h (fun t => Zl K kz L (dF K (w t) (v t))) (fun t => Al K kz L (dF K (w t) (v t))) T))) := by
  have hw : ∀ p, O.nn (wt K kz p) := fun p => dot3_nn K O.toCone _
  have z := tri_W K O (wt K kz) hw (u T) (v T) (w T) L
  have a := tri_W K O (fun p => K.mul (wt K kz p) (wt K kz p)) (fun p => O.nn_sq _) (u T) (v T) (w T) L
  have c := le_add' K O.toCone _ _ _ _ (le_mul_nn K O.toCone ν _ _ hν z)
    (le_mul_nn K O.toCone (K.mul h (K.mul ν ν)) _ _ (O.nn_mul _ _ hh (O.nn_sq ν)) a)
  refine le_trans' K O.toCone _ _ _ c ⟨K.zero, O.nn_zero, ?_⟩
  show K.add (K.add (K.mul ν (K.add (K.mul (two' K) (Zl K kz L (dF K (u T) (v T)))) (K.mul (two' K) (Zl K kz L (dF K (w T) (v T))))))
      (K.mul (K.mul h (K.mul ν ν)) (K.add (K.mul (two' K) (Al K kz L (dF K (u T) (v T)))) (K.mul (two' K) (Al K kz L (dF K (w T) (v T)))))))
      K.zero
    = K.add (K.mul (two' K) (K.add (K.mul ν (Zl K kz L (dF K (u T) (v T)))) (K.mul (K.mul h (K.mul ν ν)) (Al K kz L (dF K (u T) (v T))))))
      (K.mul (two' K) (K.add (K.mul ν (Zl K kz L (dF K (w T) (v T)))) (K.mul (K.mul h (K.mul ν ν)) (Al K kz L (dF K (w T) (v T))))))
  lib_ring

theorem tri_eps (ν h eps : R) (hν : O.nn ν) (hh : O.nn h) (kz : I → Mode) (L : List I) (u v w : Nat → I → V3 R) (T : Nat)
    (h1 : Le K O.toCone (K.mul (pwr K ν T)
      (Q K ν h (fun t => Zl K kz L (dF K (u t) (v t))) (fun t => Al K kz L (dF K (u t) (v t))) T)) eps)
    (h2 : Le K O.toCone (K.mul (pwr K ν T)
      (Q K ν h (fun t => Zl K kz L (dF K (w t) (v t))) (fun t => Al K kz L (dF K (w t) (v t))) T)) eps) :
    Le K O.toCone (K.mul (pwr K ν T)
        (Q K ν h (fun t => Zl K kz L (dF K (u t) (w t))) (fun t => Al K kz L (dF K (u t) (w t))) T))
      (K.mul (K.mul (two' K) (two' K)) eps) := by
  have n2 : O.nn (two' K) := O.nn_add _ _ (RatModel.nn_one' K O.toCone) (RatModel.nn_one' K O.toCone)
  have t := le_mul_nn K O.toCone (pwr K ν T) _ _ (EnstGronwall.pwr_nn K O ν hν T) (tri_Q K O ν h hν hh kz L u v w T)
  have s := le_add' K O.toCone _ _ _ _ (le_mul_nn K O.toCone (two' K) _ _ n2 h1) (le_mul_nn K O.toCone (two' K) _ _ n2 h2)
  refine le_trans' K O.toCone _ _ _ t (le_trans' K O.toCone _ _ _ ⟨K.zero, O.nn_zero, ?_⟩ (le_trans' K O.toCone _ _ _ s
    ⟨K.zero, O.nn_zero, ?_⟩))
  · generalize pwr K ν T = P
    generalize Q K ν h (fun t => Zl K kz L (dF K (u t) (v t))) (fun t => Al K kz L (dF K (u t) (v t))) T = X
    generalize Q K ν h (fun t => Zl K kz L (dF K (w t) (v t))) (fun t => Al K kz L (dF K (w t) (v t))) T = Y
    show K.add (K.mul P (K.add (K.mul (K.add K.one K.one) X) (K.mul (K.add K.one K.one) Y))) K.zero
      = K.add (K.mul (K.add K.one K.one) (K.mul P X)) (K.mul (K.add K.one K.one) (K.mul P Y))
    lib_ring
  · show K.add (K.add (K.mul (K.add K.one K.one) eps) (K.mul (K.add K.one K.one) eps)) K.zero
      = K.mul (K.mul (K.add K.one K.one) (K.add K.one K.one)) eps
    lib_ring

end Gen
end TruncTri

#print axioms TruncTri.tri3
#print axioms TruncTri.tri_W
#print axioms TruncTri.tri_Q
#print axioms TruncTri.tri_eps

ns/TruncUniform.lean

lean_all/ns/TruncUniform.lean

/-
  ns/TruncUniform.lean — T37 cycle 1820 (goal G857d — done): a truncation estimate uniform in time for small data.
    A_le_B         Σ w²|a|² ≤ Σ w³|a|²   (w ≥ 1)
    tsum_h         Σ_{t<n} h·f_t = h·Σ_{t<n} f_t
    diss_B         2c·Q₂(0) ≤ ν³ (c = cH ι²)  ⇒  hν²·Σ_{t<n} B(A_t) ≤ 2Q₂(0)     (EnstDiss.enst_diss with H2Step.h2_step)
    trunc_uniform  with the hypotheses of TruncDampSum and that smallness:
                   ν²·Q_d(n) ≤ iR·κ·2Q₂(0)  for every n,
                   κ = 2[2(2ι²C₁Aa + 2ι²C₂Aa) + 2ι²Aa·135],  iR = 1/(R+1)²
  (S_t ≤ iR·κ·B(A_t) by A ≤ B). The truncation error is O(1/R²) on the whole time axis, uniformly in the fine
  truncation l: the first time-global constructive convergence statement of the chain.
  Lean 4 core plus ns/TruncDampSum, ns/EnstDiss, ns/H2Step.
-/
import ns.TruncDampSum
import ns.EnstDiss
import ns.H2Step
import ns.IntForce

namespace TruncUniform
open DetChain NSChain DyadicSmallGlobal LibRing DyadicLarge FourierGalerkin GalerkinForce ForceBound ForceSob
  ModeCount CubeCount ForceUniform NormCompare EnstStep EnstGlobal EnstGalerkin ForceTwoScale ForceBilin EnstDiff
  ForceDiff TruncError TruncBound RestrStep GronwallSource EnstGronwall WeightSplit LatticeSum TruncCauchy TruncSmall
  TruncStart TruncRate3 TruncStart3 SchemeTele TruncDampSum

section Gen
variable {R : Type} (K : CRing R) (O : OrdR K) {I : Type}
variable (inv : R → R) (mul_inv : ∀ y, y ≠ K.zero → K.mul y (inv y) = K.one) (h10 : K.one ≠ K.zero)

theorem A_le_B (kz : I → Mode) (h1 : ∀ p, 1 ≤ nrm (kz p)) (l : List I) (a : I → V3 R) :
    Le K O.toCone (Al K kz l a) (Bl K kz l a) := by
  refine lsum_le K O.toCone _ _ (fun p => ?_) l
  have c1 := RatModel.cast_mono K O.toCone 1 _ (h1 p)
  have one1 : castN K 1 = K.one := by show K.add K.one K.zero = K.one; lib_ring
  rw [one1] at c1
  have sq := EnstLocal.sq_mono K O _ _ (RatModel.nn_one' K O.toCone) (RatModel.nn_cast K O.toCone _) c1
  have w1 := le_trans' K O.toCone _ _ _ sq (nrm_le_dot K O.toCone (kz p))
  rw [show K.mul K.one K.one = K.one by lib_ring] at w1
  cases w1 with
  | intro d hd =>
    refine ⟨K.mul (K.mul (K.mul (wt K kz p) (wt K kz p)) d) (dot3 K (a p) (a p)),
      O.nn_mul _ _ (O.nn_mul _ _ (O.nn_sq _) hd.1) (dot3_nn K O.toCone _), ?_⟩
    have hw : wt K kz p = K.add K.one d := hd.2.symm
    show K.add (K.mul (K.mul (wt K kz p) (wt K kz p)) (dot3 K (a p) (a p)))
        (K.mul (K.mul (K.mul (wt K kz p) (wt K kz p)) d) (dot3 K (a p) (a p)))
      = K.mul (K.mul (K.mul (wt K kz p) (wt K kz p)) (wt K kz p)) (dot3 K (a p) (a p))
    rw [hw]; lib_ring

theorem tsum_h (h : R) (f : Nat → R) : ∀ n, tsum K (fun t => K.mul h (f t)) n = K.mul h (sumN K f n)
  | 0 => by show K.zero = K.mul h K.zero; lib_ring
  | n + 1 => by
    show K.add (tsum K (fun t => K.mul h (f t)) n) (K.mul h (f n)) = K.mul h (K.add (sumN K f n) (f n))
    rw [tsum_h h f n]; lib_ring

theorem tsum_hc (h c : R) (f : Nat → R) (n : Nat) :
    tsum K (fun t => K.mul h (K.mul c (f t))) n = K.mul c (K.mul h (sumN K f n)) := by
  have e := tsum_h K h (fun t => K.mul c (f t)) n
  rw [e]
  have m : ∀ m, sumN K (fun t => K.mul c (f t)) m = K.mul c (sumN K f m) := by
    intro m
    induction m with
    | zero => show K.zero = K.mul c K.zero; lib_ring
    | succ m ih =>
      show K.add (sumN K (fun t => K.mul c (f t)) m) (K.mul c (f m)) = K.mul c (K.add (sumN K f m) (f m))
      rw [ih]; lib_ring
  rw [m n]; lib_ring

include mul_inv h10 in
theorem diss_B (l : List I) (kz : I → Mode) (hd : List.Pairwise (fun a b => kz a ≠ kz b) l)
    (h1 : ∀ p, 1 ≤ nrm (kz p)) (M : Nat) (hM : ∀ p, p ∈ l → nrm (kz p) ≤ M + 1)
    (ι h ν : R) (hh : O.nn h) (hν : O.nn ν) (hν0 : ν ≠ K.zero) (a : Nat → I → V3 R)
    (hs : ∀ t r, Step K h ν (wt K kz r) (a (t + 1) r) (a t r) (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (a t) (a t) r))
    (small2 : Le K O.toCone (K.mul (K.add (K.mul (EnstH2.cH K) (K.mul ι ι)) (K.mul (EnstH2.cH K) (K.mul ι ι))) (QH K ν h kz l a 0))
      (K.mul ν (K.mul ν ν))) (n : Nat) :
    Le K O.toCone (K.mul (K.mul h (K.mul ν ν))
        (sumN K (fun t => lsum K (fun p => K.mul (K.mul (K.mul (wt K kz p) (wt K kz p)) (wt K kz p)) (dot3 K (a t p) (a t p))) l) n))
      (K.mul (K.add K.one K.one) (QH K ν h kz l a 0)) := by
  have two : O.nn (K.add K.one K.one) := O.nn_add _ _ (RatModel.nn_one' K O.toCone) (RatModel.nn_one' K O.toCone)
  have three : O.nn (K.add (K.add K.one K.one) K.one) := O.nn_add _ _ two (RatModel.nn_one' K O.toCone)
  have n135 : O.nn (castN K 135) := RatModel.nn_cast K O.toCone 135
  have ncH : O.nn (EnstH2.cH K) := O.nn_add _ _ (O.nn_mul _ _ two (O.nn_mul _ _ three n135)) (O.nn_mul _ _ two n135)
  have hw : ∀ r, O.nn (wt K kz r) := fun r => dot3_nn K O.toCone _
  have e := EnstDiss.enst_diss K O ν h (K.mul (EnstH2.cH K) (K.mul ι ι))
    (fun t => lsum K (fun p => K.mul (K.mul (wt K kz p) (wt K kz p)) (dot3 K (a t p) (a t p))) l)
    (fun t => lsum K (fun p => K.mul (K.mul (K.mul (wt K kz p) (wt K kz p)) (wt K kz p)) (dot3 K (a t p) (a t p))) l)
    hν hν0 hh (O.nn_mul _ _ ncH (O.nn_sq ι))
    (fun t => lsum_nn K O.toCone _ (fun p => O.nn_mul _ _ (O.nn_sq _) (dot3_nn K O.toCone _)) l)
    (fun t => lsum_nn K O.toCone _ (fun p => O.nn_mul _ _ (O.nn_mul _ _ (O.nn_sq _) (hw p)) (dot3_nn K O.toCone _)) l)
    (fun t => le_trans' K O.toCone _ _ _
      (H2Step.h2_step K O inv mul_inv h10 l kz hd h1 M hM ι h ν hh hν a
        (fun t r => Nl K l (TriadSparse.delta K kz) ι (kv K kz) (a t) (a t) r) (fun t => le_refl' K O.toCone _) hs t)
      ⟨K.zero, O.nn_zero, by lib_ring⟩) small2 n
  have hQn : O.nn (Q K ν h (fun t => lsum K (fun p => K.mul (K.mul (wt K kz p) (wt K kz p)) (dot3 K (a t p) (a t p))) l)
      (fun t => lsum K (fun p => K.mul (K.mul (K.mul (wt K kz p) (wt K kz p)) (wt K kz p)) (dot3 K (a t p) (a t p))) l) n) :=
    O.nn_add _ _ (O.nn_mul _ _ hν (lsum_nn K O.toCone _ (fun p => O.nn_mul _ _ (O.nn_sq _) (dot3_nn K O.toCone _)) l))
      (O.nn_mul _ _ (O.nn_mul _ _ hh (O.nn_sq ν))
        (lsum_nn K O.toCone _ (fun p => O.nn_mul _ _ (O.nn_mul _ _ (O.nn_sq _) (hw p)) (dot3_nn K O.toCone _)) l))
  exact le_trans' K O.toCone _ _ _ ⟨_, O.nn_mul _ _ two hQn, by lib_ring⟩ e

include mul_inv h10 in
theorem trunc_uniform (l : List I) (kz : I → Mode) (hd : List.Pairwise (fun a b => kz a ≠ kz b) l)
    (h1 : ∀ p, 1 ≤ nrm (kz p)) (M : Nat) (hM : ∀ p, p ∈ l → nrm (kz p) ≤ M + 1) (Rc R' t0 : Nat) (hRM : R' ≤ M)
    (ι h ν Aa iR : R) (hh : O.nn h) (hν : O.nn ν) (hν0 : ν ≠ K.zero) (hAa : O.nn Aa)
    (hiRn : O.nn iR) (hiR : K.mul (K.mul (castN K (Rc + 1)) (castN K (Rc + 1))) iR = K.one)
    (A B : Nat → I → V3 R)
    (hA : ∀ t r, Step K h ν (wt K kz r) (A (t + 1) r) (A t r) (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (A t) (A t) r))
    (hB : ∀ t r, Step K h ν (wt K kz r) (B (t + 1) r) (B t r)
      (Nl K (l.filter (lowB Rc kz)) (TriadSparse.delta K kz) ι (kv K kz) (B t) (B t) r))
    (hQA : Le K O.toCone (QH K ν h kz l A 0) (K.mul ν Aa))
    (hB0 : B 0 = restr K (lowB Rc kz) (A 0))
    (hsm : Le K O.toCone (K.add (K.add (K.mul (K.mul (two' K) (K.mul (two' K) (two' K))) (K.mul (K.mul ι ι) (K.mul (C1' K inv R') Aa))) (K.mul (K.mul (two' K) (two' K)) (K.mul (K.mul ι ι) (K.mul (castN K 135) Aa))))
      (K.mul (K.mul (two' K) (K.mul (two' K) (two' K))) (K.mul (K.mul ι ι) (K.mul (C2' K inv R' t0) Aa)))) (K.mul ν ν)) (n : Nat)
    (small2 : Le K O.toCone (K.mul (K.add (K.mul (EnstH2.cH K) (K.mul ι ι)) (K.mul (EnstH2.cH K) (K.mul ι ι))) (QH K ν h kz l A 0))
      (K.mul ν (K.mul ν ν))) :
    Le K O.toCone (K.mul (K.mul ν ν) (Q K ν h (fun t => Zl K kz (l.filter (lowB Rc kz)) (dF K (restr K (lowB Rc kz) (A t)) (B t)))
        (fun t => Al K kz (l.filter (lowB Rc kz)) (dF K (restr K (lowB Rc kz) (A t)) (B t))) n))
      (K.mul (K.mul iR (K.mul (two' K) (K.add (K.mul (two' K) (K.add (K.mul (two' K) (K.mul (K.mul ι ι) (K.mul (C1' K inv R') Aa))) (K.mul (two' K) (K.mul (K.mul ι ι) (K.mul (C2' K inv R' t0) Aa))))) (K.mul (two' K) (K.mul (K.mul ι ι) (K.mul Aa (castN K 135))))))) (K.mul (K.add K.one K.one) (QH K ν h kz l A 0))) := by
  have two : O.nn (K.add K.one K.one) := O.nn_add _ _ (RatModel.nn_one' K O.toCone) (RatModel.nn_one' K O.toCone)
  have three : O.nn (K.add (K.add K.one K.one) K.one) := O.nn_add _ _ two (RatModel.nn_one' K O.toCone)
  have n135 : O.nn (castN K 135) := RatModel.nn_cast K O.toCone 135
  have ncH : O.nn (EnstH2.cH K) := O.nn_add _ _ (O.nn_mul _ _ two (O.nn_mul _ _ three n135)) (O.nn_mul _ _ two n135)
  have nc : O.nn (K.mul (EnstH2.cH K) (K.mul ι ι)) := O.nn_mul _ _ ncH (O.nn_sq ι)
  have hw : ∀ r, O.nn (wt K kz r) := fun r => dot3_nn K O.toCone _
  have hQ0 : O.nn (QH K ν h kz l A 0) :=
    O.nn_add _ _ (O.nn_mul _ _ hν (lsum_nn K O.toCone _ (fun p => O.nn_mul _ _ (O.nn_sq _) (dot3_nn K O.toCone _)) l))
      (O.nn_mul _ _ (O.nn_mul _ _ hh (O.nn_sq ν))
        (lsum_nn K O.toCone _ (fun p => O.nn_mul _ _ (O.nn_mul _ _ (O.nn_sq _) (hw p)) (dot3_nn K O.toCone _)) l))
  have smallA : Le K O.toCone (K.mul (K.mul (EnstH2.cH K) (K.mul ι ι)) (QH K ν h kz l A 0)) (K.mul ν (K.mul ν ν)) :=
    le_trans' K O.toCone _ _ _ ⟨K.mul (K.mul (EnstH2.cH K) (K.mul ι ι)) (QH K ν h kz l A 0), O.nn_mul _ _ nc hQ0,
      by lib_ring⟩ small2
  have n2 : O.nn (two' K) := two
  have hiv : O.nn (iv K inv (R' + 1)) := iv_nn K O inv mul_inv h10 R'
  have nC1 : O.nn (C1' K inv R') := O.nn_add _ _ (O.nn_mul _ _ (RatModel.nn_cast K O.toCone _) (RatModel.nn_one' K O.toCone))
    (O.nn_mul _ _ (RatModel.nn_cast K O.toCone _) hiv)
  have nC2 : O.nn (C2' K inv R' t0) := O.nn_add _ _ (O.nn_mul _ _ (RatModel.nn_cast K O.toCone _) (O.nn_sq _))
    (O.nn_mul _ _ (RatModel.nn_cast K O.toCone _) hiv)
  have nii : O.nn (K.mul ι ι) := O.nn_sq ι
  have nkap : O.nn (K.mul (two' K) (K.add (K.mul (two' K) (K.add (K.mul (two' K) (K.mul (K.mul ι ι) (K.mul (C1' K inv R') Aa))) (K.mul (two' K) (K.mul (K.mul ι ι) (K.mul (C2' K inv R' t0) Aa))))) (K.mul (two' K) (K.mul (K.mul ι ι) (K.mul Aa (castN K 135)))))) :=
    O.nn_mul _ _ n2 (O.nn_add _ _ (O.nn_mul _ _ n2 (O.nn_add _ _ (O.nn_mul _ _ n2 (O.nn_mul _ _ nii (O.nn_mul _ _ nC1 hAa)))
      (O.nn_mul _ _ n2 (O.nn_mul _ _ nii (O.nn_mul _ _ nC2 hAa))))) (O.nn_mul _ _ n2 (O.nn_mul _ _ nii (O.nn_mul _ _ hAa n135))))
  have hS : ∀ t, Le K O.toCone (St K inv kz l R' t0 ι Aa iR A t) (K.mul (K.mul iR (K.mul (two' K) (K.add (K.mul (two' K) (K.add (K.mul (two' K) (K.mul (K.mul ι ι) (K.mul (C1' K inv R') Aa))) (K.mul (two' K) (K.mul (K.mul ι ι) (K.mul (C2' K inv R' t0) Aa))))) (K.mul (two' K) (K.mul (K.mul ι ι) (K.mul Aa (castN K 135))))))) (Bl K kz l (A t))) := by
    intro t
    obtain ⟨d, hdn, hde⟩ := A_le_B K O kz h1 l (A t)
    refine ⟨K.mul (K.mul iR (K.add (K.mul (K.mul (two' K) (K.mul (two' K) (two' K))) (K.mul (K.mul ι ι) (K.mul (C1' K inv R') Aa)))
      (K.mul (K.mul (two' K) (two' K)) (K.mul (K.mul ι ι) (K.mul Aa (castN K 135)))))) d,
      O.nn_mul _ _ (O.nn_mul _ _ hiRn (O.nn_add _ _ (O.nn_mul _ _ (O.nn_mul _ _ n2 (O.nn_mul _ _ n2 n2)) (O.nn_mul _ _ nii (O.nn_mul _ _ nC1 hAa)))
        (O.nn_mul _ _ (O.nn_mul _ _ n2 n2) (O.nn_mul _ _ nii (O.nn_mul _ _ hAa n135))))) hdn, ?_⟩
    unfold St
    rw [← hde]
    generalize Al K kz l (A t) = Av
    generalize castN K 135 = c135
    lib_ring
  have t := trunc_damp_sum K O inv mul_inv h10 l kz hd h1 M hM Rc R' t0 hRM ι h ν Aa iR hh hν hν0 hAa hiRn hiR A B hA hB smallA hQA hB0 hsm n
  have hs := IntForce.tsum_le K O h hh _ _ hS n
  rw [tsum_hc K h (K.mul iR (K.mul (two' K) (K.add (K.mul (two' K) (K.add (K.mul (two' K) (K.mul (K.mul ι ι) (K.mul (C1' K inv R') Aa))) (K.mul (two' K) (K.mul (K.mul ι ι) (K.mul (C2' K inv R' t0) Aa))))) (K.mul (two' K) (K.mul (K.mul ι ι) (K.mul Aa (castN K 135))))))) (fun t => Bl K kz l (A t)) n] at hs
  have d : Le K O.toCone (K.mul (K.mul h (K.mul ν ν)) (sumN K (fun t => Bl K kz l (A t)) n))
      (K.mul (K.add K.one K.one) (QH K ν h kz l A 0)) := diss_B K O inv mul_inv h10 l kz hd h1 M hM ι h ν hh hν hν0 A hA small2 n
  have t2 := le_mul_nn K O.toCone (K.mul ν ν) _ _ (O.nn_sq ν) (le_trans' K O.toCone _ _ _ t hs)
  refine le_trans' K O.toCone _ _ _ t2 (le_trans' K O.toCone _ _ _ ⟨K.zero, O.nn_zero, ?_⟩
    (le_mul_nn K O.toCone (K.mul iR (K.mul (two' K) (K.add (K.mul (two' K) (K.add (K.mul (two' K) (K.mul (K.mul ι ι) (K.mul (C1' K inv R') Aa))) (K.mul (two' K) (K.mul (K.mul ι ι) (K.mul (C2' K inv R' t0) Aa))))) (K.mul (two' K) (K.mul (K.mul ι ι) (K.mul Aa (castN K 135))))))) _ _ (O.nn_mul _ _ hiRn nkap) d))
  generalize sumN K (fun t => Bl K kz l (A t)) n = X
  generalize castN K 135 = c135
  lib_ring

end Gen
end TruncUniform

#print axioms TruncUniform.A_le_B
#print axioms TruncUniform.tsum_h
#print axioms TruncUniform.tsum_hc
#print axioms TruncUniform.diss_B
#print axioms TruncUniform.trunc_uniform

ns/TruncUniform0.lean

lean_all/ns/TruncUniform0.lean

/-
  ns/TruncUniform0.lean — T37 cycle 1866 (goal G859b, piece b): TruncUniform without B_0 = A_0|_N.
    trunc_uniform0       the coarse scheme B starts from any small state (cH·ι²·Q₂(B, 0) ≤ ν³, Q₂(B, 0) ≤ ν·Aa):
                         ν²·Q_d(n) ≤ ν²·Q_d(0) + iR·κ·2Q₂(A, 0)  for every n
    trunc_uniform0_from  the same for the schemes shifted by t₂ (A(t₂ + ·), B(t₂ + ·)), so the initial difference
                         Q_d(0) is the difference at time t₂ left by a finite-time estimate on [0, t₂].
  Lean 4 core plus ns/TruncUniform, ns/TruncDamp0.
-/
import ns.TruncUniform
import ns.TruncDamp0

namespace TruncUniform0
open DetChain NSChain DyadicSmallGlobal LibRing DyadicLarge FourierGalerkin GalerkinForce ForceBound ForceSob
  ModeCount CubeCount ForceUniform NormCompare EnstStep EnstGlobal EnstGalerkin ForceTwoScale ForceBilin EnstDiff
  ForceDiff TruncError TruncBound RestrStep GronwallSource EnstGronwall WeightSplit LatticeSum TruncCauchy TruncSmall
  TruncStart TruncRate3 TruncStart3 SchemeTele TruncDampSum TruncUniform

section Gen
variable {R : Type} (K : CRing R) (O : OrdR K) {I : Type}
variable (inv : R → R) (mul_inv : ∀ y, y ≠ K.zero → K.mul y (inv y) = K.one) (h10 : K.one ≠ K.zero)

include mul_inv h10 in
theorem trunc_uniform0 (l : List I) (kz : I → Mode) (hd : List.Pairwise (fun a b => kz a ≠ kz b) l)
    (h1 : ∀ p, 1 ≤ nrm (kz p)) (M : Nat) (hM : ∀ p, p ∈ l → nrm (kz p) ≤ M + 1) (Rc R' t0 : Nat) (hRM : R' ≤ M)
    (ι h ν Aa iR : R) (hh : O.nn h) (hν : O.nn ν) (hν0 : ν ≠ K.zero) (hAa : O.nn Aa)
    (hiRn : O.nn iR) (hiR : K.mul (K.mul (castN K (Rc + 1)) (castN K (Rc + 1))) iR = K.one)
    (A B : Nat → I → V3 R)
    (hA : ∀ t r, Step K h ν (wt K kz r) (A (t + 1) r) (A t r) (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (A t) (A t) r))
    (hB : ∀ t r, Step K h ν (wt K kz r) (B (t + 1) r) (B t r)
      (Nl K (l.filter (lowB Rc kz)) (TriadSparse.delta K kz) ι (kv K kz) (B t) (B t) r))
    (hQA : Le K O.toCone (QH K ν h kz l A 0) (K.mul ν Aa))
    (smallB : Le K O.toCone (K.mul (K.mul (EnstH2.cH K) (K.mul ι ι)) (QH K ν h kz (l.filter (lowB Rc kz)) B 0)) (K.mul ν (K.mul ν ν)))
    (hQB : Le K O.toCone (QH K ν h kz (l.filter (lowB Rc kz)) B 0) (K.mul ν Aa))
    (hsm : Le K O.toCone (K.add (K.add (K.mul (K.mul (two' K) (K.mul (two' K) (two' K))) (K.mul (K.mul ι ι) (K.mul (C1' K inv R') Aa))) (K.mul (K.mul (two' K) (two' K)) (K.mul (K.mul ι ι) (K.mul (castN K 135) Aa))))
      (K.mul (K.mul (two' K) (K.mul (two' K) (two' K))) (K.mul (K.mul ι ι) (K.mul (C2' K inv R' t0) Aa)))) (K.mul ν ν)) (n : Nat)
    (small2 : Le K O.toCone (K.mul (K.add (K.mul (EnstH2.cH K) (K.mul ι ι)) (K.mul (EnstH2.cH K) (K.mul ι ι))) (QH K ν h kz l A 0))
      (K.mul ν (K.mul ν ν))) :
    Le K O.toCone (K.mul (K.mul ν ν) (Q K ν h (fun t => Zl K kz (l.filter (lowB Rc kz)) (dF K (restr K (lowB Rc kz) (A t)) (B t)))
        (fun t => Al K kz (l.filter (lowB Rc kz)) (dF K (restr K (lowB Rc kz) (A t)) (B t))) n))
      (K.add (K.mul (K.mul ν ν) (Q K ν h (fun t => Zl K kz (l.filter (lowB Rc kz)) (dF K (restr K (lowB Rc kz) (A t)) (B t)))
        (fun t => Al K kz (l.filter (lowB Rc kz)) (dF K (restr K (lowB Rc kz) (A t)) (B t))) 0)) (K.mul (K.mul iR (K.mul (two' K) (K.add (K.mul (two' K) (K.add (K.mul (two' K) (K.mul (K.mul ι ι) (K.mul (C1' K inv R') Aa))) (K.mul (two' K) (K.mul (K.mul ι ι) (K.mul (C2' K inv R' t0) Aa))))) (K.mul (two' K) (K.mul (K.mul ι ι) (K.mul Aa (castN K 135))))))) (K.mul (K.add K.one K.one) (QH K ν h kz l A   -- (+11 chars; full line in the Zenodo source)
  have two : O.nn (K.add K.one K.one) := O.nn_add _ _ (RatModel.nn_one' K O.toCone) (RatModel.nn_one' K O.toCone)
  have three : O.nn (K.add (K.add K.one K.one) K.one) := O.nn_add _ _ two (RatModel.nn_one' K O.toCone)
  have n135 : O.nn (castN K 135) := RatModel.nn_cast K O.toCone 135
  have ncH : O.nn (EnstH2.cH K) := O.nn_add _ _ (O.nn_mul _ _ two (O.nn_mul _ _ three n135)) (O.nn_mul _ _ two n135)
  have nc : O.nn (K.mul (EnstH2.cH K) (K.mul ι ι)) := O.nn_mul _ _ ncH (O.nn_sq ι)
  have hw : ∀ r, O.nn (wt K kz r) := fun r => dot3_nn K O.toCone _
  have hQ0 : O.nn (QH K ν h kz l A 0) :=
    O.nn_add _ _ (O.nn_mul _ _ hν (lsum_nn K O.toCone _ (fun p => O.nn_mul _ _ (O.nn_sq _) (dot3_nn K O.toCone _)) l))
      (O.nn_mul _ _ (O.nn_mul _ _ hh (O.nn_sq ν))
        (lsum_nn K O.toCone _ (fun p => O.nn_mul _ _ (O.nn_mul _ _ (O.nn_sq _) (hw p)) (dot3_nn K O.toCone _)) l))
  have smallA : Le K O.toCone (K.mul (K.mul (EnstH2.cH K) (K.mul ι ι)) (QH K ν h kz l A 0)) (K.mul ν (K.mul ν ν)) :=
    le_trans' K O.toCone _ _ _ ⟨K.mul (K.mul (EnstH2.cH K) (K.mul ι ι)) (QH K ν h kz l A 0), O.nn_mul _ _ nc hQ0,
      by lib_ring⟩ small2
  have n2 : O.nn (two' K) := two
  have hiv : O.nn (iv K inv (R' + 1)) := iv_nn K O inv mul_inv h10 R'
  have nC1 : O.nn (C1' K inv R') := O.nn_add _ _ (O.nn_mul _ _ (RatModel.nn_cast K O.toCone _) (RatModel.nn_one' K O.toCone))
    (O.nn_mul _ _ (RatModel.nn_cast K O.toCone _) hiv)
  have nC2 : O.nn (C2' K inv R' t0) := O.nn_add _ _ (O.nn_mul _ _ (RatModel.nn_cast K O.toCone _) (O.nn_sq _))
    (O.nn_mul _ _ (RatModel.nn_cast K O.toCone _) hiv)
  have nii : O.nn (K.mul ι ι) := O.nn_sq ι
  have nkap : O.nn (K.mul (two' K) (K.add (K.mul (two' K) (K.add (K.mul (two' K) (K.mul (K.mul ι ι) (K.mul (C1' K inv R') Aa))) (K.mul (two' K) (K.mul (K.mul ι ι) (K.mul (C2' K inv R' t0) Aa))))) (K.mul (two' K) (K.mul (K.mul ι ι) (K.mul Aa (castN K 135)))))) :=
    O.nn_mul _ _ n2 (O.nn_add _ _ (O.nn_mul _ _ n2 (O.nn_add _ _ (O.nn_mul _ _ n2 (O.nn_mul _ _ nii (O.nn_mul _ _ nC1 hAa)))
      (O.nn_mul _ _ n2 (O.nn_mul _ _ nii (O.nn_mul _ _ nC2 hAa))))) (O.nn_mul _ _ n2 (O.nn_mul _ _ nii (O.nn_mul _ _ hAa n135))))
  have hS : ∀ t, Le K O.toCone (St K inv kz l R' t0 ι Aa iR A t) (K.mul (K.mul iR (K.mul (two' K) (K.add (K.mul (two' K) (K.add (K.mul (two' K) (K.mul (K.mul ι ι) (K.mul (C1' K inv R') Aa))) (K.mul (two' K) (K.mul (K.mul ι ι) (K.mul (C2' K inv R' t0) Aa))))) (K.mul (two' K) (K.mul (K.mul ι ι) (K.mul Aa (castN K 135))))))) (Bl K kz l (A t))) := by
    intro t
    obtain ⟨d, hdn, hde⟩ := A_le_B K O kz h1 l (A t)
    refine ⟨K.mul (K.mul iR (K.add (K.mul (K.mul (two' K) (K.mul (two' K) (two' K))) (K.mul (K.mul ι ι) (K.mul (C1' K inv R') Aa)))
      (K.mul (K.mul (two' K) (two' K)) (K.mul (K.mul ι ι) (K.mul Aa (castN K 135)))))) d,
      O.nn_mul _ _ (O.nn_mul _ _ hiRn (O.nn_add _ _ (O.nn_mul _ _ (O.nn_mul _ _ n2 (O.nn_mul _ _ n2 n2)) (O.nn_mul _ _ nii (O.nn_mul _ _ nC1 hAa)))
        (O.nn_mul _ _ (O.nn_mul _ _ n2 n2) (O.nn_mul _ _ nii (O.nn_mul _ _ hAa n135))))) hdn, ?_⟩
    unfold St
    rw [← hde]
    generalize Al K kz l (A t) = Av
    generalize castN K 135 = c135
    lib_ring
  have t := TruncDamp0.trunc_damp_sum0 K O inv mul_inv h10 l kz hd h1 M hM Rc R' t0 hRM ι h ν Aa iR hh hν hν0 hAa hiRn hiR A B hA hB smallA hQA smallB hQB hsm n
  have hs := IntForce.tsum_le K O h hh _ _ hS n
  rw [tsum_hc K h (K.mul iR (K.mul (two' K) (K.add (K.mul (two' K) (K.add (K.mul (two' K) (K.mul (K.mul ι ι) (K.mul (C1' K inv R') Aa))) (K.mul (two' K) (K.mul (K.mul ι ι) (K.mul (C2' K inv R' t0) Aa))))) (K.mul (two' K) (K.mul (K.mul ι ι) (K.mul Aa (castN K 135))))))) (fun t => Bl K kz l (A t)) n] at hs
  have d : Le K O.toCone (K.mul (K.mul h (K.mul ν ν)) (sumN K (fun t => Bl K kz l (A t)) n))
      (K.mul (K.add K.one K.one) (QH K ν h kz l A 0)) := diss_B K O inv mul_inv h10 l kz hd h1 M hM ι h ν hh hν hν0 A hA small2 n
  have u := le_trans' K O.toCone _ _ _ t (le_add' K O.toCone _ _ _ _ (le_refl' K O.toCone _) hs)
  have t2 := le_mul_nn K O.toCone (K.mul ν ν) _ _ (O.nn_sq ν) u
  refine le_trans' K O.toCone _ _ _ t2 (le_trans' K O.toCone _ _ _ ⟨K.zero, O.nn_zero, ?_⟩
    (le_add' K O.toCone _ _ _ _ (le_refl' K O.toCone _)
    (le_mul_nn K O.toCone (K.mul iR (K.mul (two' K) (K.add (K.mul (two' K) (K.add (K.mul (two' K) (K.mul (K.mul ι ι) (K.mul (C1' K inv R') Aa))) (K.mul (two' K) (K.mul (K.mul ι ι) (K.mul (C2' K inv R' t0) Aa))))) (K.mul (two' K) (K.mul (K.mul ι ι) (K.mul Aa (castN K 135))))))) _ _ (O.nn_mul _ _ hiRn nkap) d)))
  generalize sumN K (fun t => Bl K kz l (A t)) n = X
  generalize castN K 135 = c135
  lib_ring

include mul_inv h10 in
theorem trunc_uniform0_from (l : List I) (kz : I → Mode) (hd : List.Pairwise (fun a b => kz a ≠ kz b) l)
    (h1 : ∀ p, 1 ≤ nrm (kz p)) (M : Nat) (hM : ∀ p, p ∈ l → nrm (kz p) ≤ M + 1) (Rc R' t0 : Nat) (hRM : R' ≤ M)
    (ι h ν Aa iR : R) (hh : O.nn h) (hν : O.nn ν) (hν0 : ν ≠ K.zero) (hAa : O.nn Aa)
    (hiRn : O.nn iR) (hiR : K.mul (K.mul (castN K (Rc + 1)) (castN K (Rc + 1))) iR = K.one)
    (A B : Nat → I → V3 R) (t2 : Nat)
    (hA : ∀ t r, Step K h ν (wt K kz r) (A (t + 1) r) (A t r) (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (A t) (A t) r))
    (hB : ∀ t r, Step K h ν (wt K kz r) (B (t + 1) r) (B t r)
      (Nl K (l.filter (lowB Rc kz)) (TriadSparse.delta K kz) ι (kv K kz) (B t) (B t) r))
    (hQA : Le K O.toCone (QH K ν h kz l (fun t => A (t2 + t)) 0) (K.mul ν Aa))
    (smallB : Le K O.toCone (K.mul (K.mul (EnstH2.cH K) (K.mul ι ι)) (QH K ν h kz (l.filter (lowB Rc kz)) (fun t => B (t2 + t)) 0)) (K.mul ν (K.mul ν ν)))
    (hQB : Le K O.toCone (QH K ν h kz (l.filter (lowB Rc kz)) (fun t => B (t2 + t)) 0) (K.mul ν Aa))
    (hsm : Le K O.toCone (K.add (K.add (K.mul (K.mul (two' K) (K.mul (two' K) (two' K))) (K.mul (K.mul ι ι) (K.mul (C1' K inv R') Aa))) (K.mul (K.mul (two' K) (two' K)) (K.mul (K.mul ι ι) (K.mul (castN K 135) Aa))))
      (K.mul (K.mul (two' K) (K.mul (two' K) (two' K))) (K.mul (K.mul ι ι) (K.mul (C2' K inv R' t0) Aa)))) (K.mul ν ν)) (n : Nat)
    (small2 : Le K O.toCone (K.mul (K.add (K.mul (EnstH2.cH K) (K.mul ι ι)) (K.mul (EnstH2.cH K) (K.mul ι ι))) (QH K ν h kz l (fun t => A (t2 + t)) 0))
      (K.mul ν (K.mul ν ν))) :
    Le K O.toCone (K.mul (K.mul ν ν) (Q K ν h (fun t => Zl K kz (l.filter (lowB Rc kz)) (dF K (restr K (lowB Rc kz) (A (t2 + t))) (B (t2 + t))))
        (fun t => Al K kz (l.filter (lowB Rc kz)) (dF K (restr K (lowB Rc kz) (A (t2 + t))) (B (t2 + t)))) n))
      (K.add (K.mul (K.mul ν ν) (Q K ν h (fun t => Zl K kz (l.filter (lowB Rc kz)) (dF K (restr K (lowB Rc kz) (A (t2 + t))) (B (t2 + t))))
        (fun t => Al K kz (l.filter (lowB Rc kz)) (dF K (restr K (lowB Rc kz) (A (t2 + t))) (B (t2 + t)))) 0))
      (K.mul (K.mul iR (K.mul (two' K) (K.add (K.mul (two' K) (K.add (K.mul (two' K) (K.mul (K.mul ι ι) (K.mul (C1' K inv R') Aa))) (K.mul (two' K) (K.mul (K.mul ι ι) (K.mul (C2' K inv R' t0) Aa))))) (K.mul (two' K) (K.mul (K.mul ι ι) (K.mul Aa (castN K 135))))))) (K.mul (K.add K.one K.one) (QH K ν h kz l (fun t => A (t2 + t)) 0)))) :=
  trunc_uniform0 K O inv mul_inv h10 l kz hd h1 M hM Rc R' t0 hRM ι h ν Aa iR hh hν hν0 hAa hiRn hiR
    (fun t => A (t2 + t)) (fun t => B (t2 + t)) (fun t r => hA (t2 + t) r) (fun t r => hB (t2 + t) r) hQA smallB hQB hsm n small2

end Gen
end TruncUniform0

#print axioms TruncUniform0.trunc_uniform0
#print axioms TruncUniform0.trunc_uniform0_from

ns/TruncWindow.lean

lean_all/ns/TruncWindow.lean

/-
  ns/TruncWindow.lean — T37 cycle 1866 (goal G852e, restated): metastability from a convergence modulus.
  The window form 'for every ε and every F : ℕ → ℕ some window [N, F(N)] of cutoffs is ε-close' needs a rate; with
  the rate of TruncUniform (error ≤ iR·κ·2Q₂(A, 0), iR = 1/(R+1)², κ independent of R) it holds with N explicit:
    inv_anti      a ≤ b, a·ia = 1, b·ib = 1, ia, ib ≥ 0  ⇒  ib ≤ ia
    iR_anti       N ≤ R  ⇒  1/(R+1)² ≤ 1/(N+1)²
    trunc_tail    iN·κ·2Q₂ ≤ ε  ⇒  every cutoff R ≥ N has ν²·Q_d(n) ≤ ε for all times n
    trunc_window  for every F : ℕ → ℕ there is N₀ (= N) with every cutoff R in [N₀, F(N₀)] ε-close at all times
  (small data: the smallness hypotheses of TruncUniform). The window statement without a rate (from compactness
  alone) is the convergence question itself and stays with the main problem.
  Lean 4 core plus ns/TruncUniform.
-/
import ns.TruncUniform

namespace TruncWindow
open DetChain NSChain DyadicSmallGlobal LibRing DyadicLarge FourierGalerkin GalerkinForce ForceBound ForceSob
  ModeCount CubeCount ForceUniform NormCompare EnstStep EnstGlobal EnstGalerkin ForceTwoScale ForceBilin EnstDiff
  ForceDiff TruncError TruncBound RestrStep GronwallSource EnstGronwall WeightSplit LatticeSum TruncCauchy TruncSmall
  TruncStart TruncRate3 TruncStart3 SchemeTele TruncDampSum TruncUniform

section Gen
variable {R : Type} (K : CRing R) (O : OrdR K) {I : Type}
variable (inv : R → R) (mul_inv : ∀ y, y ≠ K.zero → K.mul y (inv y) = K.one) (h10 : K.one ≠ K.zero)

theorem inv_anti (a b ia ib : R) (hia : O.nn ia) (hib : O.nn ib) (hab : Le K O.toCone a b)
    (ha : K.mul a ia = K.one) (hb : K.mul b ib = K.one) : Le K O.toCone ib ia := by
  cases hab with
  | intro d hd =>
    refine ⟨K.mul (K.mul d ia) ib, O.nn_mul _ _ (O.nn_mul _ _ hd.1 hia) hib, ?_⟩
    have e1 : K.add ib (K.mul (K.mul d ia) ib) = K.add (K.mul (K.mul a ia) ib) (K.mul (K.mul d ia) ib) := by
      rw [ha]; lib_ring
    have e2 : K.add (K.mul (K.mul a ia) ib) (K.mul (K.mul d ia) ib) = K.mul (K.mul (K.add a d) ib) ia := by lib_ring
    rw [e1, e2, hd.2, hb]; lib_ring

theorem iR_anti (N Rc : Nat) (hN : N ≤ Rc) (iN iR : R) (hiNn : O.nn iN) (hiRn : O.nn iR)
    (hiN : K.mul (K.mul (castN K (N + 1)) (castN K (N + 1))) iN = K.one)
    (hiR : K.mul (K.mul (castN K (Rc + 1)) (castN K (Rc + 1))) iR = K.one) : Le K O.toCone iR iN :=
  inv_anti K O _ _ iN iR hiNn hiRn
    (EnstLocal.sq_mono K O _ _ (RatModel.nn_cast K O.toCone _) (RatModel.nn_cast K O.toCone _)
      (RatModel.cast_mono K O.toCone _ _ (Nat.succ_le_succ hN))) hiN hiR

include mul_inv h10 in
theorem trunc_tail (l : List I) (kz : I → Mode) (hd : List.Pairwise (fun a b => kz a ≠ kz b) l)
    (h1 : ∀ p, 1 ≤ nrm (kz p)) (M : Nat) (hM : ∀ p, p ∈ l → nrm (kz p) ≤ M + 1) (R' t0 : Nat) (hRM : R' ≤ M)
    (ι h ν Aa : R) (hh : O.nn h) (hν : O.nn ν) (hν0 : ν ≠ K.zero) (hAa : O.nn Aa)
    (A : Nat → I → V3 R)
    (hA : ∀ t r, Step K h ν (wt K kz r) (A (t + 1) r) (A t r) (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (A t) (A t) r))
    (hQA : Le K O.toCone (QH K ν h kz l A 0) (K.mul ν Aa))
    (hsm : Le K O.toCone (K.add (K.add (K.mul (K.mul (two' K) (K.mul (two' K) (two' K))) (K.mul (K.mul ι ι) (K.mul (C1' K inv R') Aa))) (K.mul (K.mul (two' K) (two' K)) (K.mul (K.mul ι ι) (K.mul (castN K 135) Aa))))
      (K.mul (K.mul (two' K) (K.mul (two' K) (two' K))) (K.mul (K.mul ι ι) (K.mul (C2' K inv R' t0) Aa)))) (K.mul ν ν))
    (small2 : Le K O.toCone (K.mul (K.add (K.mul (EnstH2.cH K) (K.mul ι ι)) (K.mul (EnstH2.cH K) (K.mul ι ι))) (QH K ν h kz l A 0))
      (K.mul ν (K.mul ν ν)))
    (N : Nat) (iN eps : R) (hiNn : O.nn iN) (hiN : K.mul (K.mul (castN K (N + 1)) (castN K (N + 1))) iN = K.one)
    (heps : Le K O.toCone (K.mul (K.mul iN (K.mul (two' K) (K.add (K.mul (two' K) (K.add (K.mul (two' K) (K.mul (K.mul ι ι) (K.mul (C1' K inv R') Aa))) (K.mul (two' K) (K.mul (K.mul ι ι) (K.mul (C2' K inv R' t0) Aa))))) (K.mul (two' K) (K.mul (K.mul ι ι) (K.mul Aa (castN K 135))))))) (K.mul (K.add K.one K.one) (QH K ν h kz l A 0))) eps) :
    ∀ Rc, N ≤ Rc → ∀ (iR : R), O.nn iR → K.mul (K.mul (castN K (Rc + 1)) (castN K (Rc + 1))) iR = K.one →
      ∀ (B : Nat → I → V3 R), (∀ t r, Step K h ν (wt K kz r) (B (t + 1) r) (B t r)
        (Nl K (l.filter (lowB Rc kz)) (TriadSparse.delta K kz) ι (kv K kz) (B t) (B t) r)) →
      B 0 = restr K (lowB Rc kz) (A 0) →
      ∀ n, Le K O.toCone (K.mul (K.mul ν ν) (Q K ν h (fun t => Zl K kz (l.filter (lowB Rc kz)) (dF K (restr K (lowB Rc kz) (A t)) (B t)))
        (fun t => Al K kz (l.filter (lowB Rc kz)) (dF K (restr K (lowB Rc kz) (A t)) (B t))) n)) eps := by
  intro Rc hN iR hiRn hiR B hB hB0 n
  have two : O.nn (K.add K.one K.one) := O.nn_add _ _ (RatModel.nn_one' K O.toCone) (RatModel.nn_one' K O.toCone)
  have n135 : O.nn (castN K 135) := RatModel.nn_cast K O.toCone 135
  have hw : ∀ r, O.nn (wt K kz r) := fun r => dot3_nn K O.toCone _
  have hQ0 : O.nn (QH K ν h kz l A 0) :=
    O.nn_add _ _ (O.nn_mul _ _ hν (lsum_nn K O.toCone _ (fun p => O.nn_mul _ _ (O.nn_sq _) (dot3_nn K O.toCone _)) l))
      (O.nn_mul _ _ (O.nn_mul _ _ hh (O.nn_sq ν))
        (lsum_nn K O.toCone _ (fun p => O.nn_mul _ _ (O.nn_mul _ _ (O.nn_sq _) (hw p)) (dot3_nn K O.toCone _)) l))
  have n2 : O.nn (two' K) := two
  have hiv : O.nn (iv K inv (R' + 1)) := iv_nn K O inv mul_inv h10 R'
  have nC1 : O.nn (C1' K inv R') := O.nn_add _ _ (O.nn_mul _ _ (RatModel.nn_cast K O.toCone _) (RatModel.nn_one' K O.toCone))
    (O.nn_mul _ _ (RatModel.nn_cast K O.toCone _) hiv)
  have nC2 : O.nn (C2' K inv R' t0) := O.nn_add _ _ (O.nn_mul _ _ (RatModel.nn_cast K O.toCone _) (O.nn_sq _))
    (O.nn_mul _ _ (RatModel.nn_cast K O.toCone _) hiv)
  have nii : O.nn (K.mul ι ι) := O.nn_sq ι
  have nkap : O.nn (K.mul (two' K) (K.add (K.mul (two' K) (K.add (K.mul (two' K) (K.mul (K.mul ι ι) (K.mul (C1' K inv R') Aa))) (K.mul (two' K) (K.mul (K.mul ι ι) (K.mul (C2' K inv R' t0) Aa))))) (K.mul (two' K) (K.mul (K.mul ι ι) (K.mul Aa (castN K 135)))))) :=
    O.nn_mul _ _ n2 (O.nn_add _ _ (O.nn_mul _ _ n2 (O.nn_add _ _ (O.nn_mul _ _ n2 (O.nn_mul _ _ nii (O.nn_mul _ _ nC1 hAa)))
      (O.nn_mul _ _ n2 (O.nn_mul _ _ nii (O.nn_mul _ _ nC2 hAa))))) (O.nn_mul _ _ n2 (O.nn_mul _ _ nii (O.nn_mul _ _ hAa n135))))
  have u := trunc_uniform K O inv mul_inv h10 l kz hd h1 M hM Rc R' t0 hRM ι h ν Aa iR hh hν hν0 hAa hiRn hiR A B hA hB
    hQA hB0 hsm n small2
  have m := le_mul_right' K O.toCone (le_mul_right' K O.toCone (iR_anti K O N Rc hN iN iR hiNn hiRn hiN hiR) nkap)
    (O.nn_mul _ _ two hQ0)
  exact le_trans' K O.toCone _ _ _ u (le_trans' K O.toCone _ _ _ m heps)

include mul_inv h10 in
theorem trunc_window (l : List I) (kz : I → Mode) (hd : List.Pairwise (fun a b => kz a ≠ kz b) l)
    (h1 : ∀ p, 1 ≤ nrm (kz p)) (M : Nat) (hM : ∀ p, p ∈ l → nrm (kz p) ≤ M + 1) (R' t0 : Nat) (hRM : R' ≤ M)
    (ι h ν Aa : R) (hh : O.nn h) (hν : O.nn ν) (hν0 : ν ≠ K.zero) (hAa : O.nn Aa)
    (A : Nat → I → V3 R)
    (hA : ∀ t r, Step K h ν (wt K kz r) (A (t + 1) r) (A t r) (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (A t) (A t) r))
    (hQA : Le K O.toCone (QH K ν h kz l A 0) (K.mul ν Aa))
    (hsm : Le K O.toCone (K.add (K.add (K.mul (K.mul (two' K) (K.mul (two' K) (two' K))) (K.mul (K.mul ι ι) (K.mul (C1' K inv R') Aa))) (K.mul (K.mul (two' K) (two' K)) (K.mul (K.mul ι ι) (K.mul (castN K 135) Aa))))
      (K.mul (K.mul (two' K) (K.mul (two' K) (two' K))) (K.mul (K.mul ι ι) (K.mul (C2' K inv R' t0) Aa)))) (K.mul ν ν))
    (small2 : Le K O.toCone (K.mul (K.add (K.mul (EnstH2.cH K) (K.mul ι ι)) (K.mul (EnstH2.cH K) (K.mul ι ι))) (QH K ν h kz l A 0))
      (K.mul ν (K.mul ν ν)))
    (N : Nat) (iN eps : R) (hiNn : O.nn iN) (hiN : K.mul (K.mul (castN K (N + 1)) (castN K (N + 1))) iN = K.one)
    (heps : Le K O.toCone (K.mul (K.mul iN (K.mul (two' K) (K.add (K.mul (two' K) (K.add (K.mul (two' K) (K.mul (K.mul ι ι) (K.mul (C1' K inv R') Aa))) (K.mul (two' K) (K.mul (K.mul ι ι) (K.mul (C2' K inv R' t0) Aa))))) (K.mul (two' K) (K.mul (K.mul ι ι) (K.mul Aa (castN K 135))))))) (K.mul (K.add K.one K.one) (QH K ν h kz l A 0))) eps) :
    ∀ F : Nat → Nat, ∃ N0, ∀ Rc, N0 ≤ Rc → Rc ≤ F N0 → ∀ (iR : R), O.nn iR → K.mul (K.mul (castN K (Rc + 1)) (castN K (Rc + 1))) iR = K.one →
      ∀ (B : Nat → I → V3 R), (∀ t r, Step K h ν (wt K kz r) (B (t + 1) r) (B t r)
        (Nl K (l.filter (lowB Rc kz)) (TriadSparse.delta K kz) ι (kv K kz) (B t) (B t) r)) →
      B 0 = restr K (lowB Rc kz) (A 0) →
      ∀ n, Le K O.toCone (K.mul (K.mul ν ν) (Q K ν h (fun t => Zl K kz (l.filter (lowB Rc kz)) (dF K (restr K (lowB Rc kz) (A t)) (B t)))
        (fun t => Al K kz (l.filter (lowB Rc kz)) (dF K (restr K (lowB Rc kz) (A t)) (B t))) n)) eps :=
  fun _ => ⟨N, fun Rc hN _ => trunc_tail K O inv mul_inv h10 l kz hd h1 M hM R' t0 hRM ι h ν Aa hh hν hν0 hAa A hA hQA
    hsm small2 N iN eps hiNn hiN heps Rc hN⟩

end Gen
end TruncWindow

#print axioms TruncWindow.inv_anti
#print axioms TruncWindow.iR_anti
#print axioms TruncWindow.trunc_tail
#print axioms TruncWindow.trunc_window

ns/TwoLevel.lean

lean_all/ns/TwoLevel.lean

/-
  ns/TwoLevel.lean — T37 cycle 833 (goal G543): the time-summed absorption lemma for the semi-implicit low modes,
  over any ordered ring (ℚ included): if for every t
    4E_{t+1} + 7G_{t+1} ≤ 4E_t + G_t + 4R_t
  (from SemiWStep2 + LowMode: the new-state dissipation 2G_{t+1} minus a quarter of G_{t+1} + G_t), then with
  Φ_t = 4E_t + G_t,
    Φ_T + 6·Σ_{t<T} G_{t+1} ≤ Φ_0 + 4·Σ_{t<T} R_t.

  Lean 4 core plus NSChain (sumN), ns/DyadicLarge (le_add'), lib/RingTac.
-/
import ns.DyadicLarge
import lib.RingTac

namespace TwoLevel
open DetChain NSChain LibRing DyadicLarge

section Gen
variable {R : Type} (K : CRing R) (P : Cone K)

def c4 (y : R) : R := K.add (K.add y y) (K.add y y)
def c6 (y : R) : R := K.add (K.add (K.add y y) (K.add y y)) (K.add y y)
def c7 (y : R) : R := K.add (c6 K y) y

theorem two_level (E G Rr : Nat → R)
    (st : ∀ t, Le K P (K.add (c4 K (E (t + 1))) (c7 K (G (t + 1)))) (K.add (K.add (c4 K (E t)) (G t)) (c4 K (Rr t)))) :
    ∀ T, Le K P (K.add (K.add (c4 K (E T)) (G T)) (c6 K (sumN K (fun t => G (t + 1)) T)))
      (K.add (K.add (c4 K (E 0)) (G 0)) (c4 K (sumN K Rr T)))
  | 0 => by
    refine ⟨K.zero, P.nn_zero, ?_⟩
    show K.add (K.add (K.add (c4 K (E 0)) (G 0)) (c6 K K.zero)) K.zero = K.add (K.add (c4 K (E 0)) (G 0)) (c4 K K.zero)
    unfold c4 c6; lib_ring
  | T + 1 => by
    have ih := two_level E G Rr st T
    have s := le_add' K P _ _ _ _ (st T) (le_refl' K P (c6 K (sumN K (fun t => G (t + 1)) T)))
    have s2 := le_add' K P _ _ _ _ ih (le_refl' K P (c4 K (Rr T)))
    have l : K.add (K.add (c4 K (E (T + 1))) (G (T + 1))) (c6 K (K.add (sumN K (fun t => G (t + 1)) T) (G (T + 1))))
        = K.add (K.add (c4 K (E (T + 1))) (c7 K (G (T + 1)))) (c6 K (sumN K (fun t => G (t + 1)) T)) := by
      unfold c7; unfold c6; unfold c4; lib_ring
    have r1 : K.add (K.add (K.add (c4 K (E T)) (G T)) (c4 K (Rr T))) (c6 K (sumN K (fun t => G (t + 1)) T))
        = K.add (K.add (K.add (c4 K (E T)) (G T)) (c6 K (sumN K (fun t => G (t + 1)) T))) (c4 K (Rr T)) := by
      unfold c4 c6; lib_ring
    have r2 : K.add (K.add (K.add (c4 K (E 0)) (G 0)) (c4 K (sumN K Rr T))) (c4 K (Rr T))
        = K.add (K.add (c4 K (E 0)) (G 0)) (c4 K (K.add (sumN K Rr T) (Rr T))) := by
      unfold c4; lib_ring
    show Le K P (K.add (K.add (c4 K (E (T + 1))) (G (T + 1))) (c6 K (K.add (sumN K (fun t => G (t + 1)) T) (G (T + 1)))))
      (K.add (K.add (c4 K (E 0)) (G 0)) (c4 K (K.add (sumN K Rr T) (Rr T))))
    rw [l, ← r2]; rw [r1] at s
    exact le_trans' K P _ _ _ s s2

end Gen

end TwoLevel

#print axioms TwoLevel.two_level

ns/TwoLevelConst.lean

lean_all/ns/TwoLevelConst.lean

/-
  ns/TwoLevelConst.lean — T37 cycle 849 (goal G559): TwoLevel with a uniform remainder bound, over any ring with a
  positive cone (ℚ included): if 4E_{t+1} + 7G_{t+1} ≤ 4E_t + G_t + 4R_t and R_t ≤ ρ for every t, then
    (4E_T + G_T) + 6·Σ_{t<T} G_{t+1} ≤ (4E_0 + G_0) + 4·T·ρ      (T·ρ = castN T · ρ).
  With LowCFL this is the CFL-version weighted bound once ρ bounds 2hB_t + hX_t + 2q_t.

  Lean 4 core plus ns/TwoLevel (two_level), ns/RatModelU (sumN_const_g), ns/DyadicLarge (sum_le), lib/RingTac.
-/
import ns.TwoLevel
import ns.RatModelU

namespace TwoLevelConst
open DetChain NSChain LibRing DyadicLarge TwoLevel RatModelU

section Gen
variable {R : Type} (K : CRing R) (P : Cone K)

theorem two_level_const (E G Rr : Nat → R) (rho : R)
    (st : ∀ t, Le K P (K.add (c4 K (E (t + 1))) (c7 K (G (t + 1)))) (K.add (K.add (c4 K (E t)) (G t)) (c4 K (Rr t))))
    (hR : ∀ t, Le K P (Rr t) rho) (T : Nat) :
    Le K P (K.add (K.add (c4 K (E T)) (G T)) (c6 K (sumN K (fun t => G (t + 1)) T)))
      (K.add (K.add (c4 K (E 0)) (G 0)) (c4 K (K.mul (castN K T) rho))) := by
  have tl := two_level K P E G Rr st T
  have sR := sum_le K P _ _ hR T
  rw [sumN_const_g] at sR
  have c := le_add' K P _ _ _ _ (le_refl' K P (K.add (c4 K (E 0)) (G 0)))
    (show Le K P (c4 K (sumN K Rr T)) (c4 K (K.mul (castN K T) rho)) from by
      unfold c4; exact le_add' K P _ _ _ _ (le_add' K P _ _ _ _ sR sR) (le_add' K P _ _ _ _ sR sR))
  exact le_trans' K P _ _ _ tl c

end Gen

end TwoLevelConst

#print axioms TwoLevelConst.two_level_const

ns/VarChain.lean

lean_all/ns/VarChain.lean

/-
  ns/VarChain.lean — T37 (goal G859c2-s3b, third piece): the window chain for windows of variable lengths n_k ≤ n
  (window starts at good steps, ns/GoodStep): from ν^{n_k} d_{k+1} ≤ u^{n_k}(d_k + s₀) on every window,
      d_K ≤ c^K d_0 + c s₀ Σ_{j<K} c^j,   c = (u·ν⁻¹)^n   (u·ν⁻¹ ≥ 1),
  the same closed form as the fixed-length chain (ns/WindowShift.scale_chain), with the uniform factor of
  ns/PowOne.factor_le.
    mul_right_le   a ≤ b, x ≥ 0 ⇒ a·x ≤ b·x
    var_step       one window of length n_k ≤ n
    var_chain      the chain
  Lean 4 core plus ns/WindowShift, ns/PowOne.
-/
import ns.WindowShift
import ns.PowOne

namespace VarChain
open DetChain NSChain DyadicSmallGlobal LibRing DyadicLarge EnstGronwall BadCount PowOne WindowShift

section Ring
variable {R : Type} (K : CRing R) (O : OrdR K)

theorem mul_right_le (a b x : R) (h : Le K O.toCone a b) (hx : O.nn x) : Le K O.toCone (K.mul a x) (K.mul b x) := by
  have m := le_mul_nn K O.toCone x a b hx h
  refine le_trans' K O.toCone _ _ _ (le_of_eq'' K O.toCone _ _ ?_) (le_trans' K O.toCone _ _ _ m (le_of_eq'' K O.toCone _ _ ?_))
  · lib_ring
  · lib_ring

theorem var_step (ν iν u s0 x y : R) (n nk : Nat) (hνi : K.mul ν iν = K.one) (hiν : O.nn iν)
    (hc1 : Le K O.toCone K.one (K.mul u iν)) (hn : nk ≤ n) (hy : O.nn y) (hs0 : O.nn s0)
    (h : Le K O.toCone (K.mul (pwr K ν nk) x) (K.mul (pwr K u nk) (K.add y s0))) :
    Le K O.toCone x (K.add (K.mul (pwr K (K.mul u iν) n) y) (K.mul (pwr K (K.mul u iν) n) s0)) := by
  have s := scale_step K O ν iν (pwr K u nk) s0 x y nk hνi hiν h
  have f := factor_le K O u iν hc1 nk n hn
  exact le_trans' K O.toCone _ _ _ s
    (le_add' K O.toCone _ _ _ _ (mul_right_le K O _ _ y f hy) (mul_right_le K O _ _ s0 f hs0))

theorem var_chain (ν iν u s0 : R) (n : Nat) (nk : Nat → Nat) (d : Nat → R) (hνi : K.mul ν iν = K.one)
    (hiν : O.nn iν) (hc1 : Le K O.toCone K.one (K.mul u iν)) (hn : ∀ k, nk k ≤ n) (hd : ∀ k, O.nn (d k))
    (hs0 : O.nn s0)
    (hwin : ∀ k, Le K O.toCone (K.mul (pwr K ν (nk k)) (d (k + 1))) (K.mul (pwr K u (nk k)) (K.add (d k) s0))) :
    ∀ K' : Nat, Le K O.toCone (d K')
      (K.add (K.mul (pwr K (pwr K (K.mul u iν) n) K') (d 0))
        (K.mul (K.mul (pwr K (K.mul u iν) n) s0) (sumN K (pwr K (pwr K (K.mul u iν) n)) K'))) := by
  have hc : O.nn (pwr K (K.mul u iν) n) := by
    obtain ⟨e, he, ee⟩ := one_le_pwr K O (K.mul u iν) hc1 n
    rw [← ee]; exact O.nn_add _ _ (RatModel.nn_one' K O.toCone) he
  exact WindowChain.window_chain K O (pwr K (K.mul u iν) n) (K.mul (pwr K (K.mul u iν) n) s0) d hc
    (fun k => var_step K O ν iν u s0 (d (k + 1)) (d k) n (nk k) hνi hiν hc1 (hn k) (hd k) hs0 (hwin k))

end Ring

end VarChain

#print axioms VarChain.mul_right_le
#print axioms VarChain.var_step
#print axioms VarChain.var_chain

ns/VarChainS.lean

lean_all/ns/VarChainS.lean

/-
  ns/VarChainS.lean — T37 (goal G859c2-s3b, fourth piece): the variable-length window chain with the source term of
  ns/WindowAt.large_window_at, n_k·σ on a window of length n_k (σ = h·S): since n_k ≤ n, it is bounded by n·σ, and
  ns/VarChain.var_chain applies with s₀ = n·σ.
    castN_le       a ≤ b ⇒ a ≤ b in the ordered ring
    var_chain_s    ν^{n_k} d_{k+1} ≤ u^{n_k}(d_k + n_k σ) for all k  ⇒  d_K ≤ c^K d_0 + c n σ Σ_{j<K} c^j
  Lean 4 core plus ns/VarChain.
-/
import ns.VarChain

namespace VarChainS
open DetChain NSChain DyadicSmallGlobal LibRing DyadicLarge EnstGronwall BadCount PowOne VarChain

section Ring
variable {R : Type} (K : CRing R) (O : OrdR K)

theorem castN_le_add (a : Nat) : ∀ k, Le K O.toCone (castN K a) (castN K (a + k))
  | 0 => le_refl' K O.toCone _
  | k + 1 => by
    refine le_trans' K O.toCone _ _ _ (castN_le_add a k) ⟨K.one, RatModel.nn_one' K O.toCone, ?_⟩
    show K.add (castN K (a + k)) K.one = K.add K.one (castN K (a + k))
    lib_ring

theorem castN_le (a b : Nat) (h : a ≤ b) : Le K O.toCone (castN K a) (castN K b) := by
  obtain ⟨k, hk⟩ := BadRun.le_split h
  rw [hk]; exact castN_le_add K O a k

theorem var_chain_s (ν iν u σ : R) (n : Nat) (nk : Nat → Nat) (d : Nat → R) (hνi : K.mul ν iν = K.one)
    (hiν : O.nn iν) (hu : O.nn u) (hc1 : Le K O.toCone K.one (K.mul u iν)) (hn : ∀ k, nk k ≤ n)
    (hd : ∀ k, O.nn (d k)) (hσ : O.nn σ)
    (hwin : ∀ k, Le K O.toCone (K.mul (pwr K ν (nk k)) (d (k + 1)))
      (K.mul (pwr K u (nk k)) (K.add (d k) (K.mul (castN K (nk k)) σ)))) :
    ∀ K' : Nat, Le K O.toCone (d K')
      (K.add (K.mul (pwr K (pwr K (K.mul u iν) n) K') (d 0))
        (K.mul (K.mul (pwr K (K.mul u iν) n) (K.mul (castN K n) σ)) (sumN K (pwr K (pwr K (K.mul u iν) n)) K'))) := by
  have hcast : O.nn (castN K n) := RatModel.nn_cast K O.toCone n
  refine var_chain K O ν iν u (K.mul (castN K n) σ) n nk d hνi hiν hc1 hn hd (O.nn_mul _ _ hcast hσ) (fun k => ?_)
  have hs : Le K O.toCone (K.add (d k) (K.mul (castN K (nk k)) σ)) (K.add (d k) (K.mul (castN K n) σ)) :=
    le_add' K O.toCone _ _ _ _ (le_refl' K O.toCone _)
      (mul_right_le K O _ _ σ (castN_le K O (nk k) n (hn k)) hσ)
  exact le_trans' K O.toCone _ _ _ (hwin k) (le_mul_nn K O.toCone _ _ _ (Dyadic.pwr_nn K O u hu (nk k)) hs)

end Ring

end VarChainS

#print axioms VarChainS.castN_le_add
#print axioms VarChainS.castN_le
#print axioms VarChainS.var_chain_s

ns/VarWindows.lean

lean_all/ns/VarWindows.lean

/-
  ns/VarWindows.lean — T37 (goal G859c2-s3b, fifth piece): the conditional large-data truncation Cauchy chain over
  windows of variable lengths n_k ≤ n starting at arbitrary steps s_k (s_{k+1} = s_k + n_k): if every window
  satisfies the local hypotheses of ns/WindowAt.large_window_at (enstrophy ≤ Zb, H² energy bounds), then
      Q_d(s_K) ≤ c^K Q_d(s_0) + c·n·σ·Σ_{j<K} c^j,   c = ((ν + hα)ν⁻¹)^n,
  with the same α, σ as in a single window. The window starts can be the good steps of ns/GoodStep (gaps ≤ L);
  what remains open is that the local hypotheses hold on each window (the local time from a good step ≥ L).
    var_windows
  Lean 4 core plus ns/WindowAt, ns/VarChainS.
-/
import ns.WindowAt
import ns.VarChainS

namespace VarWindows
open DetChain NSChain DyadicSmallGlobal LibRing DyadicLarge FourierGalerkin GalerkinForce ForceBound ForceSob
  ModeCount CubeCount ForceUniform NormCompare EnstStep EnstGlobal EnstGalerkin ForceTwoScale ForceBilin EnstDiff
  ForceDiff TruncError TruncBound RestrStep GronwallSource EnstGronwall WeightSplit LatticeSum TruncCauchy TruncRate
  WindowShift
  WindowShift

section Gen
variable {R : Type} (K : CRing R) (O : OrdR K) {I : Type}
variable (inv : R → R) (mul_inv : ∀ y, y ≠ K.zero → K.mul y (inv y) = K.one) (h10 : K.one ≠ K.zero)

include mul_inv h10 in
/-- **The variable-window truncation Cauchy chain (conditional on the window hypotheses).** -/
theorem var_windows (l : List I) (kz : I → Mode) (hd : List.Pairwise (fun a b => kz a ≠ kz b) l)
    (h1 : ∀ p, 1 ≤ nrm (kz p)) (M : Nat) (hM : ∀ p, p ∈ l → nrm (kz p) ≤ M + 1) (Rc R' t0 : Nat) (hRM : R' ≤ M)
    (ι h ν Zb Aa Ab iR : R) (hh : O.nn h) (hν : O.nn ν) (hZb : O.nn Zb) (hAa : O.nn Aa) (hAb : O.nn Ab) (hiRn : O.nn iR)
    (hiR : K.mul (K.mul (castN K (Rc + 1)) (castN K (Rc + 1))) iR = K.one)
    (A B : Nat → I → V3 R)
    (hA : ∀ t r, Step K h ν (wt K kz r) (A (t + 1) r) (A t r) (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (A t) (A t) r))
    (hB : ∀ t r, r ∈ l.filter (lowB Rc kz) → Step K h ν (wt K kz r) (B (t + 1) r) (B t r)
      (Nl K (l.filter (lowB Rc kz)) (TriadSparse.delta K kz) ι (kv K kz) (B t) (B t) r))
    (n : Nat) (s nk : Nat → Nat) (hs : ∀ k, s (k + 1) = s k + nk k) (hn : ∀ k, nk k ≤ n)
    (hZA : ∀ k t, t < nk k → Le K O.toCone (Zl K kz l (A (s k + t))) Zb)
    (εA εB DA DB : R) (EA EB : Nat → R) (hεA : O.nn εA) (hεB : O.nn εB) (hDA : O.nn DA) (hDB : O.nn DB) (hν0 : ν ≠ K.zero)
    (hEA : ∀ k t, t ≤ nk k → Le K O.toCone (K.add (K.mul ν (Al K kz l (A (s k + t))))
      (K.mul (K.mul h εA) (lsum K (fun p => K.mul (K.mul (K.mul (wt K kz p) (wt K kz p)) (wt K kz p))
        (dot3 K (A (s k + t) p) (A (s k + t) p))) l)))
      (K.add (EA k) (K.mul (castN K t) (K.mul h DA))))
    (hEB : ∀ k t, t ≤ nk k → Le K O.toCone (K.add (K.mul ν (Al K kz (l.filter (lowB Rc kz)) (B (s k + t))))
      (K.mul (K.mul h εB) (lsum K (fun p => K.mul (K.mul (K.mul (wt K kz p) (wt K kz p)) (wt K kz p))
        (dot3 K (B (s k + t) p) (B (s k + t) p))) (l.filter (lowB Rc kz)))))
      (K.add (EB k) (K.mul (castN K t) (K.mul h DB))))
    (hAa_def : ∀ k, Aa = K.mul (K.add (EA k) (K.mul (castN K (nk k)) (K.mul h DA))) (inv ν))
    (hAb_def : ∀ k, Ab = K.mul (K.add (EB k) (K.mul (castN K (nk k)) (K.mul h DB))) (inv ν))
    (hβc : Le K O.toCone (K.mul (K.mul (two' K) (K.mul (two' K) (two' K))) (K.mul (K.mul ι ι) (K.mul (C2' K inv R' t0) Zb)))
      (K.mul ν ν))
    (hiν : O.nn (inv ν)) (hu : O.nn (K.add ν (K.mul h
          (K.add (K.mul (K.mul (two' K) (K.mul (two' K) (two' K))) (K.mul (K.mul ι ι) (K.mul (C1' K inv R') Zb)))
            (K.mul (K.mul (two' K) (two' K)) (K.mul (K.mul ι ι) (K.mul (castN K 135) Ab)))))))
    (hc1 : Le K O.toCone K.one (K.mul (K.add ν (K.mul h
          (K.add (K.mul (K.mul (two' K) (K.mul (two' K) (two' K))) (K.mul (K.mul ι ι) (K.mul (C1' K inv R') Zb)))
            (K.mul (K.mul (two' K) (two' K)) (K.mul (K.mul ι ι) (K.mul (castN K 135) Ab)))))) (inv ν)))
    (hdn : ∀ k, O.nn (Q K ν h (fun t => Zl K kz (l.filter (lowB Rc kz)) (dF K (restr K (lowB Rc kz) (A t)) (B t)))
          (fun t => Al K kz (l.filter (lowB Rc kz)) (dF K (restr K (lowB Rc kz) (A t)) (B t))) (s k)))
    (hσ : O.nn (K.mul h (K.mul (two' K)
            (K.add
              (K.mul (two' K) (K.add
                (K.mul (two' K) (K.mul (K.mul ι ι) (K.mul (K.mul Aa iR) (K.mul (C1' K inv R') Zb))))
                (K.mul (two' K) (K.mul (K.mul ι ι) (K.mul Aa (K.mul (C2' K inv R' t0) Zb))))))
              (K.mul (two' K) (K.mul (K.mul ι ι) (K.mul Aa (K.mul (castN K 135) (K.mul Aa iR))))))))) :
    ∀ K' : Nat, Le K O.toCone (Q K ν h (fun t => Zl K kz (l.filter (lowB Rc kz)) (dF K (restr K (lowB Rc kz) (A t)) (B t)))
          (fun t => Al K kz (l.filter (lowB Rc kz)) (dF K (restr K (lowB Rc kz) (A t)) (B t))) (s K'))
      (K.add (K.mul (pwr K (pwr K (K.mul (K.add ν (K.mul h
          (K.add (K.mul (K.mul (two' K) (K.mul (two' K) (two' K))) (K.mul (K.mul ι ι) (K.mul (C1' K inv R') Zb)))
            (K.mul (K.mul (two' K) (two' K)) (K.mul (K.mul ι ι) (K.mul (castN K 135) Ab)))))) (inv ν)) n) K') (Q K ν h (fun t => Zl K kz (l.filter (lowB Rc kz)) (dF K (restr K (lowB Rc kz) (A t)) (B t)))
          (fun t => Al K kz (l.filter (lowB Rc kz)) (dF K (restr K (lowB Rc kz) (A t)) (B t))) (s 0)))
        (K.mul (K.mul (pwr K (K.mul (K.add ν (K.mul h
          (K.add (K.mul (K.mul (two' K) (K.mul (two' K) (two' K))) (K.mul (K.mul ι ι) (K.mul (C1' K inv R') Zb)))
            (K.mul (K.mul (two' K) (two' K)) (K.mul (K.mul ι ι) (K.mul (castN K 135) Ab)))))) (inv ν)) n) (K.mul (castN K n) (K.mul h (K.mul (two' K)
            (K.add
              (K.mul (two' K) (K.add
                (K.mul (two' K) (K.mul (K.mul ι ι) (K.mul (K.mul Aa iR) (K.mul (C1' K inv R') Zb))))
                (K.mul (two' K) (K.mul (K.mul ι ι) (K.mul Aa (K.mul (C2' K inv R' t0) Zb))))))
              (K.mul (two' K) (K.mul (K.mul ι ι) (K.mul Aa (K.mul (castN K 135) (K.mul Aa iR)))))))))) (sumN K (pwr K (pwr K (K.mul (K.add ν (K.mul h
          (K.add (K.mul (K.mul (two' K) (K.mul (two' K) (two' K))) (K.mul (K.mul ι ι) (K.mul (C1' K inv R') Zb)))
            (K.mul (K.mul (two' K) (two' K)) (K.mul (K.mul ι ι) (K.mul (castN K 135) Ab)))))) (inv ν)) n)) K'))) :=
  VarChainS.var_chain_s K O ν (inv ν) (K.add ν (K.mul h
          (K.add (K.mul (K.mul (two' K) (K.mul (two' K) (two' K))) (K.mul (K.mul ι ι) (K.mul (C1' K inv R') Zb)))
            (K.mul (K.mul (two' K) (two' K)) (K.mul (K.mul ι ι) (K.mul (castN K 135) Ab)))))) (K.mul h (K.mul (two' K)
            (K.add
              (K.mul (two' K) (K.add
                (K.mul (two' K) (K.mul (K.mul ι ι) (K.mul (K.mul Aa iR) (K.mul (C1' K inv R') Zb))))
                (K.mul (two' K) (K.mul (K.mul ι ι) (K.mul Aa (K.mul (C2' K inv R' t0) Zb))))))
              (K.mul (two' K) (K.mul (K.mul ι ι) (K.mul Aa (K.mul (castN K 135) (K.mul Aa iR)))))))) n nk
    (fun k => (Q K ν h (fun t => Zl K kz (l.filter (lowB Rc kz)) (dF K (restr K (lowB Rc kz) (A t)) (B t)))
          (fun t => Al K kz (l.filter (lowB Rc kz)) (dF K (restr K (lowB Rc kz) (A t)) (B t))) (s k))) (mul_inv ν hν0) hiν hu hc1 hn hdn hσ (fun k => by
      have hw := WindowAt.large_window_at K O inv mul_inv h10 l kz hd h1 M hM Rc R' t0 hRM ι h ν Zb Aa Ab iR hh hν hZb hAa hAb
        hiRn hiR A B hA hB (nk k) (s k) (hZA k) εA εB DA DB (EA k) (EB k) hεA hεB hDA hDB hν0 (hEA k) (hEB k)
        (hAa_def k) (hAb_def k) hβc
      rw [← hs k] at hw
      exact hw)

end Gen

end VarWindows

#print axioms VarWindows.var_windows

ns/VecCS.lean

lean_all/ns/VecCS.lean

/-
  ns/VecCS.lean — T37 cycle 938 (goal G624b, piece 1): Cauchy–Schwarz for double sums of dot products of 3-vectors,
  over any commutative ring with a nonnegative cone (ℚ included):
    (Σ_{p∈l} Σ_{q∈l} X_pq · Y_pq)² ≤ (Σ_p Σ_q |X_pq|²) (Σ_p Σ_q |Y_pq|²)
  via lib/CauchySchwarz on the list of all component pairs.

  Lean 4 core plus ns/ForceBound (pos, le_of_cs, sC_app), ns/QuadBound (sA_app, sB_app), lib/CauchySchwarz.
-/
import ns.ForceBound

namespace VecCS
open DetChain NSChain LibRing LibCS FourierGalerkin QuadBound ForceBound

section Gen
variable {R : Type} (K : CRing R) (P : Cone K) {I : Type}

def trip (X Y : V3 R) : List (R × R) := [(X.x, Y.x), (X.y, Y.y), (X.z, Y.z)]

def vrow (X Y : I → I → V3 R) (p : I) : List I → List (R × R)
  | [] => []
  | q :: m => trip (X p q) (Y p q) ++ vrow X Y p m

def vgrid (X Y : I → I → V3 R) (l : List I) : List I → List (R × R)
  | [] => []
  | p :: m => vrow X Y p l ++ vgrid X Y l m

theorem sA_trip (X Y : V3 R) : sA K (trip X Y) = dot3 K X X := by
  show K.add (K.mul X.x X.x) (K.add (K.mul X.y X.y) (K.add (K.mul X.z X.z) K.zero)) = dot3 K X X
  unfold dot3; lib_ring
theorem sB_trip (X Y : V3 R) : sB K (trip X Y) = dot3 K Y Y := by
  show K.add (K.mul Y.x Y.x) (K.add (K.mul Y.y Y.y) (K.add (K.mul Y.z Y.z) K.zero)) = dot3 K Y Y
  unfold dot3; lib_ring
theorem sC_trip (X Y : V3 R) : sC K (trip X Y) = dot3 K X Y := by
  show K.add (K.mul X.x Y.x) (K.add (K.mul X.y Y.y) (K.add (K.mul X.z Y.z) K.zero)) = dot3 K X Y
  unfold dot3; lib_ring

theorem sA_vrow (X Y : I → I → V3 R) (p : I) : ∀ m, sA K (vrow X Y p m) = lsum K (fun q => dot3 K (X p q) (X p q)) m
  | [] => rfl
  | q :: m => by
    show sA K (trip (X p q) (Y p q) ++ vrow X Y p m) = _
    rw [sA_app, sA_trip, sA_vrow X Y p m]; rfl
theorem sB_vrow (X Y : I → I → V3 R) (p : I) : ∀ m, sB K (vrow X Y p m) = lsum K (fun q => dot3 K (Y p q) (Y p q)) m
  | [] => rfl
  | q :: m => by
    show sB K (trip (X p q) (Y p q) ++ vrow X Y p m) = _
    rw [sB_app, sB_trip, sB_vrow X Y p m]; rfl
theorem sC_vrow (X Y : I → I → V3 R) (p : I) : ∀ m, sC K (vrow X Y p m) = lsum K (fun q => dot3 K (X p q) (Y p q)) m
  | [] => rfl
  | q :: m => by
    show sC K (trip (X p q) (Y p q) ++ vrow X Y p m) = _
    rw [sC_app, sC_trip, sC_vrow X Y p m]; rfl

theorem sA_vgrid (X Y : I → I → V3 R) (l : List I) : ∀ m,
    sA K (vgrid X Y l m) = lsum K (fun p => lsum K (fun q => dot3 K (X p q) (X p q)) l) m
  | [] => rfl
  | p :: m => by
    show sA K (vrow X Y p l ++ vgrid X Y l m) = _
    rw [sA_app, sA_vrow, sA_vgrid X Y l m]; rfl
theorem sB_vgrid (X Y : I → I → V3 R) (l : List I) : ∀ m,
    sB K (vgrid X Y l m) = lsum K (fun p => lsum K (fun q => dot3 K (Y p q) (Y p q)) l) m
  | [] => rfl
  | p :: m => by
    show sB K (vrow X Y p l ++ vgrid X Y l m) = _
    rw [sB_app, sB_vrow, sB_vgrid X Y l m]; rfl
theorem sC_vgrid (X Y : I → I → V3 R) (l : List I) : ∀ m,
    sC K (vgrid X Y l m) = lsum K (fun p => lsum K (fun q => dot3 K (X p q) (Y p q)) l) m
  | [] => rfl
  | p :: m => by
    show sC K (vrow X Y p l ++ vgrid X Y l m) = _
    rw [sC_app, sC_vrow, sC_vgrid X Y l m]; rfl

theorem vec_double_cs (X Y : I → I → V3 R) (l : List I) :
    Le K P (K.mul (lsum K (fun p => lsum K (fun q => dot3 K (X p q) (Y p q)) l) l)
        (lsum K (fun p => lsum K (fun q => dot3 K (X p q) (Y p q)) l) l))
      (K.mul (lsum K (fun p => lsum K (fun q => dot3 K (X p q) (X p q)) l) l)
        (lsum K (fun p => lsum K (fun q => dot3 K (Y p q) (Y p q)) l) l)) := by
  have cs := cauchy_schwarz K (pos K P) (vgrid X Y l l)
  rw [sA_vgrid, sB_vgrid, sC_vgrid] at cs
  exact le_of_cs K P _ _ _ cs

end Gen

end VecCS

#print axioms VecCS.sA_vgrid
#print axioms VecCS.sB_vgrid
#print axioms VecCS.sC_vgrid
#print axioms VecCS.vec_double_cs

ns/WCS.lean

lean_all/ns/WCS.lean

/-
  ns/WCS.lean — T37 cycle 1131 (goal G737): weighted Cauchy–Schwarz for telescoping sums, over any commutative ring
  with a nonnegative cone. For weights m_j, im_j ≥ 0 with m_j·im_j = 1:
    cross      Σ_{i<n} m_i m (im_i d_i − im d)² = m·Σ im_i d_i² − 2d·Σ d_i + im d²·Σ m_i     (m·im = 1)
    wcs        (Σ_{j<n} d_j)² ≤ (Σ_{j<n} m_j)·(Σ_{j<n} im_j d_j²)                         (weighted Lagrange)
    wcs_form   Σ_r μ_r|Σ_{j<n} d_{j,r}|² ≤ (Σ_j m_j)·Σ_j im_j·Σ_r μ_r|d_{j,r}|²         (μ_r ≥ 0)
  With m_j = (3/4)^j and step-size errors ≤ C·k₀2^{−j} this sums a dyadic Cauchy sequence without square roots (G736).

  Lean 4 core plus NSChain (sumN), ns/TimeLipN (lsum_sumN, sumN_add3), ns/ForceBound (lsum_le), lib/RingTac.
-/
import ns.TimeLipN
import ns.AbelCount

namespace WCS
open DetChain NSChain LibRing DyadicLarge FourierGalerkin ForceBound EnstDiff TimeLipN

section Gen
variable {R : Type} (K : CRing R) (P : Cone K)

theorem cross (mw imw d : Nat → R) (m im e : R) (hmi : ∀ i, K.mul (mw i) (imw i) = K.one) (hm : K.mul m im = K.one) : ∀ n,
    sumN K (fun i => K.mul (K.mul (mw i) m) (K.mul (K.add (K.mul (imw i) (d i)) (K.neg (K.mul im e)))
        (K.add (K.mul (imw i) (d i)) (K.neg (K.mul im e))))) n
      = K.add (K.add (K.mul m (sumN K (fun i => K.mul (imw i) (K.mul (d i) (d i))) n))
          (K.neg (K.mul (K.add e e) (sumN K d n)))) (K.mul (K.mul im (K.mul e e)) (sumN K mw n))
  | 0 => by
    show K.zero = K.add (K.add (K.mul m K.zero) (K.neg (K.mul (K.add e e) K.zero))) (K.mul (K.mul im (K.mul e e)) K.zero)
    lib_ring
  | n + 1 => by
    show K.add (sumN K (fun i => K.mul (K.mul (mw i) m) (K.mul (K.add (K.mul (imw i) (d i)) (K.neg (K.mul im e)))
        (K.add (K.mul (imw i) (d i)) (K.neg (K.mul im e))))) n)
        (K.mul (K.mul (mw n) m) (K.mul (K.add (K.mul (imw n) (d n)) (K.neg (K.mul im e))) (K.add (K.mul (imw n) (d n)) (K.neg (K.mul im e)))))
      = K.add (K.add (K.mul m (K.add (sumN K (fun i => K.mul (imw i) (K.mul (d i) (d i))) n) (K.mul (imw n) (K.mul (d n) (d n)))))
          (K.neg (K.mul (K.add e e) (K.add (sumN K d n) (d n))))) (K.mul (K.mul im (K.mul e e)) (K.add (sumN K mw n) (mw n)))
    rw [cross mw imw d m im e hmi hm n]
    have h1 := hmi n
    generalize sumN K (fun i => K.mul (imw i) (K.mul (d i) (d i))) n = W at *
    generalize sumN K d n = S at *
    generalize sumN K mw n = M at *
    generalize mw n = a at h1 ⊢
    generalize imw n = ia at h1 ⊢
    generalize d n = x
    calc _ = K.add (K.add (K.add (K.mul m W) (K.neg (K.mul (K.add e e) S))) (K.mul (K.mul im (K.mul e e)) M))
          (K.add (K.add (K.mul (K.mul (K.mul m ia) (K.mul x x)) (K.mul a ia))
            (K.neg (K.mul (K.mul (K.add x x) e) (K.mul (K.mul a ia) (K.mul m im)))))
            (K.mul (K.mul a (K.mul im (K.mul e e))) (K.mul m im))) := by lib_ring
      _ = K.add (K.add (K.add (K.mul m W) (K.neg (K.mul (K.add e e) S))) (K.mul (K.mul im (K.mul e e)) M))
          (K.add (K.add (K.mul (K.mul (K.mul m ia) (K.mul x x)) K.one)
            (K.neg (K.mul (K.mul (K.add x x) e) (K.mul K.one K.one))))
            (K.mul (K.mul a (K.mul im (K.mul e e))) K.one)) := by rw [h1, hm]
      _ = _ := by lib_ring

theorem sumN_nn (f : Nat → R) (h : ∀ i, P.nn (f i)) : ∀ n, P.nn (sumN K f n)
  | 0 => P.nn_zero
  | n + 1 => P.nn_add _ _ (sumN_nn f h n) (h n)

theorem wcs (mw imw d : Nat → R) (hmw : ∀ i, P.nn (mw i)) (hmi : ∀ i, K.mul (mw i) (imw i) = K.one) : ∀ n,
    Le K P (K.mul (sumN K d n) (sumN K d n))
      (K.mul (sumN K mw n) (sumN K (fun i => K.mul (imw i) (K.mul (d i) (d i))) n))
  | 0 => ⟨K.zero, P.nn_zero, by show K.add (K.mul K.zero K.zero) K.zero = K.mul K.zero K.zero; lib_ring⟩
  | n + 1 => by
    have ih := wcs mw imw d hmw hmi n
    have cr := cross K mw imw d (mw n) (imw n) (d n) hmi (hmi n) n
    cases ih with
    | intro D hD =>
      refine ⟨K.add D (sumN K (fun i => K.mul (K.mul (mw i) (mw n)) (K.mul (K.add (K.mul (imw i) (d i)) (K.neg (K.mul (imw n) (d n))))
          (K.add (K.mul (imw i) (d i)) (K.neg (K.mul (imw n) (d n)))))) n),
        P.nn_add _ _ hD.1 (sumN_nn K P _ (fun i => P.nn_mul _ _ (P.nn_mul _ _ (hmw i) (hmw n)) (P.nn_sq _)) n), ?_⟩
      rw [cr]
      show K.add (K.mul (K.add (sumN K d n) (d n)) (K.add (sumN K d n) (d n))) _
        = K.mul (K.add (sumN K mw n) (mw n)) (K.add (sumN K (fun i => K.mul (imw i) (K.mul (d i) (d i))) n) (K.mul (imw n) (K.mul (d n) (d n))))
      have key := hD.2
      have h1 := hmi n
      generalize sumN K (fun i => K.mul (imw i) (K.mul (d i) (d i))) n = W at *
      generalize sumN K d n = S at *
      generalize sumN K mw n = M at *
      generalize mw n = a at h1 ⊢
      generalize imw n = ia at h1 ⊢
      generalize d n = x
      calc _ = K.add (K.add (K.mul S S) D) (K.add (K.add (K.mul K.one (K.mul x x)) (K.mul a W)) (K.mul (K.mul ia (K.mul x x)) M)) := by lib_ring
        _ = K.add (K.mul M W) (K.add (K.add (K.mul (K.mul a ia) (K.mul x x)) (K.mul a W)) (K.mul (K.mul ia (K.mul x x)) M)) := by rw [key, h1]
        _ = _ := by lib_ring

theorem wcs3 (mw imw : Nat → R) (hmw : ∀ i, P.nn (mw i)) (hmi : ∀ i, K.mul (mw i) (imw i) = K.one)
    (d : Nat → V3 R) (n : Nat) :
    Le K P (dot3 K (V3.mk (sumN K (fun j => (d j).x) n) (sumN K (fun j => (d j).y) n) (sumN K (fun j => (d j).z) n))
        (V3.mk (sumN K (fun j => (d j).x) n) (sumN K (fun j => (d j).y) n) (sumN K (fun j => (d j).z) n)))
      (K.mul (sumN K mw n) (sumN K (fun j => K.mul (imw j) (dot3 K (d j) (d j))) n)) := by
  have cx := wcs K P mw imw (fun j => (d j).x) hmw hmi n
  have cy := wcs K P mw imw (fun j => (d j).y) hmw hmi n
  have cz := wcs K P mw imw (fun j => (d j).z) hmw hmi n
  have s := le_add' K P _ _ _ _ (le_add' K P _ _ _ _ cx cy) cz
  refine le_trans' K P _ _ _ s ⟨K.zero, P.nn_zero, ?_⟩
  have e := sumN_add3 K (fun j => K.mul (imw j) (K.mul (d j).x (d j).x)) (fun j => K.mul (imw j) (K.mul (d j).y (d j).y))
    (fun j => K.mul (imw j) (K.mul (d j).z (d j).z)) n
  have e2 : sumN K (fun j => K.mul (imw j) (dot3 K (d j) (d j))) n
      = sumN K (fun j => K.add (K.add (K.mul (imw j) (K.mul (d j).x (d j).x)) (K.mul (imw j) (K.mul (d j).y (d j).y)))
          (K.mul (imw j) (K.mul (d j).z (d j).z))) n := by
    apply AbelCount.sumN_congr; intro j
    show K.mul (imw j) (K.add (K.add (K.mul (d j).x (d j).x) (K.mul (d j).y (d j).y)) (K.mul (d j).z (d j).z)) = _
    lib_ring
  rw [e2, ← e]
  show K.add (K.add (K.add (K.mul (sumN K mw n) _) (K.mul (sumN K mw n) _)) (K.mul (sumN K mw n) _)) K.zero = _
  generalize sumN K mw n = M
  lib_ring

theorem sumN_mul_l (c : R) (f : Nat → R) : ∀ n, K.mul c (sumN K f n) = sumN K (fun j => K.mul c (f j)) n
  | 0 => by show K.mul c K.zero = K.zero; lib_ring
  | n + 1 => by
    show K.mul c (K.add (sumN K f n) (f n)) = K.add (sumN K (fun j => K.mul c (f j)) n) (K.mul c (f n))
    rw [← sumN_mul_l c f n]; lib_ring

/-- the vector sum Σ_{j<n} d_j at mode r -/
def vs {I : Type} (d : Nat → I → V3 R) (n : Nat) (r : I) : V3 R :=
  V3.mk (sumN K (fun j => (d j r).x) n) (sumN K (fun j => (d j r).y) n) (sumN K (fun j => (d j r).z) n)

theorem wcs_form {I : Type} (l : List I) (μ : I → R) (hμ : ∀ r, P.nn (μ r)) (mw imw : Nat → R) (hmw : ∀ i, P.nn (mw i))
    (hmi : ∀ i, K.mul (mw i) (imw i) = K.one) (d : Nat → I → V3 R) (n : Nat) :
    Le K P (lsum K (fun r => K.mul (μ r) (dot3 K (vs K d n r) (vs K d n r))) l)
      (K.mul (sumN K mw n) (sumN K (fun j => K.mul (imw j) (lsum K (fun r => K.mul (μ r) (dot3 K (d j r) (d j r))) l)) n)) := by
  have t := lsum_le K P _ _ (fun r => le_mul_nn K P (μ r) _ _ (hμ r) (wcs3 K P mw imw hmw hmi (fun j => d j r) n)) l
  refine le_trans' K P _ _ _ t ⟨K.zero, P.nn_zero, ?_⟩
  rw [add_zero']
  have e1 : lsum K (fun r => K.mul (μ r) (K.mul (sumN K mw n) (sumN K (fun j => K.mul (imw j) (dot3 K (d j r) (d j r))) n))) l
      = K.mul (sumN K mw n) (lsum K (fun r => sumN K (fun j => K.mul (imw j) (K.mul (μ r) (dot3 K (d j r) (d j r)))) n) l) := by
    rw [← lsum_mul_left]
    refine lsum_ext K _ _ (fun r => ?_) l
    show K.mul (μ r) (K.mul (sumN K mw n) (sumN K (fun j => K.mul (imw j) (dot3 K (d j r) (d j r))) n))
      = K.mul (sumN K mw n) (sumN K (fun j => K.mul (imw j) (K.mul (μ r) (dot3 K (d j r) (d j r)))) n)
    have e := sumN_mul_l K (μ r) (fun j => K.mul (imw j) (dot3 K (d j r) (d j r))) n
    have e' : sumN K (fun j => K.mul (μ r) (K.mul (imw j) (dot3 K (d j r) (d j r)))) n
        = sumN K (fun j => K.mul (imw j) (K.mul (μ r) (dot3 K (d j r) (d j r)))) n :=
      AbelCount.sumN_congr K _ _ (fun j => by lib_ring) n
    rw [← e', ← e]
    generalize sumN K mw n = M
    generalize sumN K (fun j => K.mul (imw j) (dot3 K (d j r) (d j r))) n = W
    lib_ring
  rw [e1, lsum_sumN K (fun r j => K.mul (imw j) (K.mul (μ r) (dot3 K (d j r) (d j r)))) l n]
  have e2 : sumN K (fun j => lsum K (fun r => K.mul (imw j) (K.mul (μ r) (dot3 K (d j r) (d j r)))) l) n
      = sumN K (fun j => K.mul (imw j) (lsum K (fun r => K.mul (μ r) (dot3 K (d j r) (d j r))) l)) n :=
    AbelCount.sumN_congr K _ _ (fun j => by rw [lsum_mul_left]) n
  rw [e2]

end Gen

end WCS

#print axioms WCS.cross
#print axioms WCS.sumN_nn
#print axioms WCS.wcs
#print axioms WCS.wcs3
#print axioms WCS.sumN_mul_l
#print axioms WCS.wcs_form

ns/WeightH2.lean

lean_all/ns/WeightH2.lean

/-
  ns/WeightH2.lean — T37 cycle 1065 (goal G656b, piece iv-a): weights for the H² force bound, over an ordered field.
    dot_le_3nrm   |k|² ≤ 3|k|_∞²   (integer modes cast into the ring)
  With NormCompare.nrm_le_dot (|k|_∞² ≤ |k|²) this sandwiches the Euclidean weight; next: Σ_p |k_p|_∞^{−6}|k_p|² ≤ 3·135.

  Lean 4 core plus ns/NormCompare (kvec, sq_castZ, mx_cases), ns/LatticeBound, ns/EnstLocal (sq_mono).
-/
import ns.NormCompare
import ns.LatticeBound
import ns.EnstLocal

namespace WeightH2
open DetChain NSChain DyadicSmallGlobal LibRing DyadicLarge FourierGalerkin NormCompare ModeCount CubeCount
  LatticeSum LatticeBound

section Gen
variable {R : Type} (K : CRing R) (O : OrdR K)

theorem sq_le_mx (x y : Nat) : Le K O.toCone (K.mul (castN K x) (castN K x)) (K.mul (castN K (mx x y)) (castN K (mx x y))) :=
  EnstLocal.sq_mono K O _ _ (RatModel.nn_cast K O.toCone _) (RatModel.nn_cast K O.toCone _)
    (RatModel.cast_mono K O.toCone _ _ (le_mx_left x y))

theorem sq_le_mx' (x y : Nat) : Le K O.toCone (K.mul (castN K y) (castN K y)) (K.mul (castN K (mx x y)) (castN K (mx x y))) :=
  EnstLocal.sq_mono K O _ _ (RatModel.nn_cast K O.toCone _) (RatModel.nn_cast K O.toCone _)
    (RatModel.cast_mono K O.toCone _ _ (le_mx_right x y))

/-- |k|² ≤ 3|k|_∞² -/
theorem dot_le_3nrm (k : Mode) :
    Le K O.toCone (dot3 K (kvec K k) (kvec K k))
      (K.add (K.add (K.mul (castN K (nrm k)) (castN K (nrm k))) (K.mul (castN K (nrm k)) (castN K (nrm k))))
        (K.mul (castN K (nrm k)) (castN K (nrm k)))) := by
  show Le K O.toCone (K.add (K.add (K.mul (castZ K k.1) (castZ K k.1)) (K.mul (castZ K k.2.1) (castZ K k.2.1)))
    (K.mul (castZ K k.2.2) (castZ K k.2.2))) _
  rw [sq_castZ, sq_castZ, sq_castZ]
  have hn : nrm k = mx k.1.natAbs (mx k.2.1.natAbs k.2.2.natAbs) := rfl
  rw [hn]
  have a1 := sq_le_mx K O k.1.natAbs (mx k.2.1.natAbs k.2.2.natAbs)
  have a2 := le_trans' K O.toCone _ _ _ (sq_le_mx K O k.2.1.natAbs k.2.2.natAbs) (sq_le_mx' K O k.1.natAbs _)
  have a3 := le_trans' K O.toCone _ _ _ (sq_le_mx' K O k.2.1.natAbs k.2.2.natAbs) (sq_le_mx' K O k.1.natAbs _)
  exact le_add' K O.toCone _ _ _ _ (le_add' K O.toCone _ _ _ _ a1 a2) a3

end Gen

end WeightH2

#print axioms WeightH2.sq_le_mx
#print axioms WeightH2.sq_le_mx'
#print axioms WeightH2.dot_le_3nrm

ns/WeightSplit.lean

lean_all/ns/WeightSplit.lean

/-
  ns/WeightSplit.lean — T37 cycle 989 (goal G648, piece b1): weight sums for the two-scale force bound, over an ordered
  field. Modes kz_p ∈ ℤ³, nonzero, distinct on the list l, |kz_p|_∞ ≤ M + 1; cutoff R ≤ M; n_p = |kz_p|_∞.
    low_count   Σ_p [n_p ≤ R] ≤ (2R+1)³
    high_sum    Σ_p [n_p > R] n_p^{−4} ≤ 135/(R+1)
    s_split     Σ_p (if n_p ≤ R then c else n_p^{−4}) ≤ (2R+1)³·c + 135/(R+1)   for any c ≥ 0
  (S1 uses c ≥ n_p^{−2} on low modes, i.e. c = 1; S2 uses c = T^{−2}.)

  Lean 4 core plus ns/LatticeTail, ns/ForceUniform (lsum_norms, mem_map_inv, pw_map).
-/
import ns.LatticeTail
import ns.ForceUniform

namespace WeightSplit
open DetChain NSChain DyadicSmallGlobal LibRing DyadicLarge AbelCount LatticeStep LatticeSum ModeCount CubeCount
  LatticeBound LatticeTail ForceUniform GoodModesQ ForceSob

/-- the modes with |k|_∞ > R -/
def fltH (Rc : Nat) : List Mode → List Mode
  | [] => []
  | k :: l => cond (Nat.ble (nrm k) Rc) (fltH Rc l) (k :: fltH Rc l)

theorem mem_fltH (Rc : Nat) (x : Mode) : ∀ (l : List Mode), x ∈ fltH Rc l → x ∈ l ∧ Rc + 1 ≤ nrm x
  | [], h => nomatch h
  | k :: l, h => by
    have h' : x ∈ cond (Nat.ble (nrm k) Rc) (fltH Rc l) (k :: fltH Rc l) := h
    cases hb : Nat.ble (nrm k) Rc with
    | true =>
      rw [hb] at h'
      have r := mem_fltH Rc x l h'
      exact ⟨List.Mem.tail _ r.1, r.2⟩
    | false =>
      rw [hb] at h'
      have hk : Rc + 1 ≤ nrm k := by
        cases Nat.lt_or_ge Rc (nrm k) with
        | inl lt => exact lt
        | inr ge => exact absurd (Nat.ble_eq_true_of_le ge) (by rw [hb]; exact Bool.noConfusion)
      cases h' with
      | head => exact ⟨List.Mem.head _, hk⟩
      | tail _ h'' =>
        have r := mem_fltH Rc x l h''
        exact ⟨List.Mem.tail _ r.1, r.2⟩

theorem fltH_pw (Rc : Nat) : ∀ (l : List Mode), List.Pairwise (fun a b => a ≠ b) l →
    List.Pairwise (fun a b => a ≠ b) (fltH Rc l)
  | [], _ => List.Pairwise.nil
  | k :: l, hp => by
    cases hp with
    | cons hk hl =>
      show List.Pairwise _ (cond (Nat.ble (nrm k) Rc) (fltH Rc l) (k :: fltH Rc l))
      cases Nat.ble (nrm k) Rc with
      | true => exact fltH_pw Rc l hl
      | false => exact List.Pairwise.cons (fun y hy => hk y (mem_fltH Rc y l hy).1) (fltH_pw Rc l hl)

section Gen
variable {R : Type} (K : CRing R) (O : OrdR K) {I : Type}
variable (inv : R → R) (mul_inv : ∀ y, y ≠ K.zero → K.mul y (inv y) = K.one) (h10 : K.one ≠ K.zero)

def lowB (Rc : Nat) (kz : I → Mode) (p : I) : Bool := Nat.ble (nrm (kz p)) Rc

theorem count_low (Rc : Nat) (kz : I → Mode) :
    ∀ (l : List I), lsum K (fun p => cond (lowB Rc kz p) K.one K.zero) l = castN K (cnt (norms (l.map kz)) Rc)
  | [] => rfl
  | p :: l => by
    show K.add (cond (Nat.ble (nrm (kz p)) Rc) K.one K.zero) (lsum K (fun p => cond (lowB Rc kz p) K.one K.zero) l)
      = castN K (cond (Nat.ble (nrm (kz p)) Rc) (cnt (norms (l.map kz)) Rc + 1) (cnt (norms (l.map kz)) Rc))
    rw [count_low Rc kz l]
    cases Nat.ble (nrm (kz p)) Rc with
    | true => rfl
    | false =>
      show K.add K.zero (castN K (cnt (norms (l.map kz)) Rc)) = castN K (cnt (norms (l.map kz)) Rc)
      lib_ring

theorem high_eq (Rc : Nat) (kz : I → Mode) (g : Nat → R) :
    ∀ (l : List I), lsum K (fun p => cond (lowB Rc kz p) K.zero (g (nrm (kz p)))) l = lsumN K g (norms (fltH Rc (l.map kz)))
  | [] => rfl
  | p :: l => by
    show K.add (cond (Nat.ble (nrm (kz p)) Rc) K.zero (g (nrm (kz p)))) (lsum K (fun p => cond (lowB Rc kz p) K.zero (g (nrm (kz p)))) l)
      = lsumN K g (norms (cond (Nat.ble (nrm (kz p)) Rc) (fltH Rc (l.map kz)) (kz p :: fltH Rc (l.map kz))))
    rw [high_eq Rc kz g l]
    cases Nat.ble (nrm (kz p)) Rc with
    | true =>
      show K.add K.zero (lsumN K g (norms (fltH Rc (l.map kz)))) = lsumN K g (norms (fltH Rc (l.map kz)))
      lib_ring
    | false => rfl

include mul_inv h10 in
/-- Σ_p (if n_p ≤ R then c else n_p^{−4}) ≤ (2R+1)³ c + 135/(R+1) -/
theorem s_split (Rc : Nat) (l : List I) (kz : I → Mode) (hd : List.Pairwise (fun a b => kz a ≠ kz b) l)
    (M : Nat) (hM : ∀ p, p ∈ l → nrm (kz p) ≤ M + 1) (hRM : Rc ≤ M) (c : R) (hc : O.nn c) :
    Le K O.toCone (lsum K (fun p => cond (lowB Rc kz p) c (f4 K inv (nrm (kz p)))) l)
      (K.add (K.mul (castN K ((2 * Rc + 1) * ((2 * Rc + 1) * (2 * Rc + 1)))) c)
        (K.mul (castN K 135) (iv K inv (Rc + 1)))) := by
  have split : lsum K (fun p => cond (lowB Rc kz p) c (f4 K inv (nrm (kz p)))) l
      = K.add (K.mul (lsum K (fun p => cond (lowB Rc kz p) K.one K.zero) l) c)
          (lsum K (fun p => cond (lowB Rc kz p) K.zero (f4 K inv (nrm (kz p)))) l) := by
    rw [K.mul_comm _ c, ← lsum_mul_left, ← lsum_add]
    exact lsum_ext K _ _ (fun p => by
      show cond (lowB Rc kz p) c _ = K.add (K.mul c (cond (lowB Rc kz p) K.one K.zero)) (cond (lowB Rc kz p) K.zero _)
      cases lowB Rc kz p with
      | true => show c = K.add (K.mul c K.one) K.zero; lib_ring
      | false =>
        show f4 K inv (nrm (kz p)) = K.add (K.mul c K.zero) (f4 K inv (nrm (kz p)))
        lib_ring) l
  rw [split, count_low K Rc kz l, high_eq K Rc kz _ l]
  have hlow := RatModel.cast_mono K O.toCone _ _ (cnt_cube (l.map kz) (pw_map kz l hd) Rc)
  have t1 := le_mul_right' K O.toCone hlow hc
  have t2 := lattice_tail K O inv mul_inv h10 Rc (fltH Rc (l.map kz)) (fltH_pw Rc _ (pw_map kz l hd))
    (fun k hk => (mem_fltH Rc k _ hk).2) M
    (fun k hk => by
      have ⟨q, hq, e⟩ := mem_map_inv kz k l (mem_fltH Rc k _ hk).1
      rw [← e]; exact hM q hq) hRM
  exact le_add' K O.toCone _ _ _ _ t1 t2

end Gen

end WeightSplit

#print axioms WeightSplit.fltH_pw
#print axioms WeightSplit.count_low
#print axioms WeightSplit.high_eq
#print axioms WeightSplit.s_split

ns/WeightedCS.lean

lean_all/ns/WeightedCS.lean

/-
  ns/WeightedCS.lean — T37 cycle 59: the weighted form of the transfer bound.

  Lean 4 core plus ns/TriadSum (lsum_le, lsum_nn) and ns/SumCS. Split each triad term as T_t = x_t·y_t
  with x_t² ≤ α_t and y_t² ≤ ω_t. Then
      (Σ_t T_t)² ≤ (Σ_t α_t)(Σ_t ω_t).
  With y_t = |k_t|^{-s}·(…) the weight sum Σω is a lattice sum Σ_k |k|^{-2s}, finite for s > 3/2 in 3D
  and independent of the truncation: this replaces the factor |l| of TriadSum (the Sobolev-type route
  to a truncation-uniform local time for H^s data, s > 3/2; with Riccati).
    le_mul_both'   0 ≤ a ≤ b, 0 ≤ c ≤ d ⇒ ac ≤ bd
    weighted_cs    the bound above
-/
import ns.TriadSum

namespace WeightedCS
open DetChain NSChain SumCS TriadSum

variable {R : Type} (K : CRing R) {I : Type}

theorem le_mul_both' (P : Cone K) (a b c d : R) (ha : P.nn a) (hd : P.nn d)
    (h1 : Le K P a b) (h2 : Le K P c d) : Le K P (K.mul a c) (K.mul b d) :=
  TriadBound.le_mul_both K P a b c d ha hd h1 h2

theorem weighted_cs (P : Cone K) (x y α ω : I → R) (l : List I)
    (hx : ∀ t, Le K P (sq K (x t)) (α t)) (hy : ∀ t, Le K P (sq K (y t)) (ω t))
    (hω : ∀ t, P.nn (ω t)) :
    Le K P (sq K (lsum K (fun t => K.mul (x t) (y t)) l))
      (K.mul (lsum K α l) (lsum K ω l)) :=
  le_trans' K P _ _ _ (cs_sum K P x y l)
    (le_mul_both' K P _ _ _ _ (lsum_nn K P _ (fun t => P.nn_sq (x t)) l) (lsum_nn K P _ hω l)
      (lsum_le K P _ _ hx l) (lsum_le K P _ _ hy l))

end WeightedCS

#print axioms WeightedCS.le_mul_both'
#print axioms WeightedCS.weighted_cs

ns/WeightedJensen.lean

lean_all/ns/WeightedJensen.lean

/-
  ns/WeightedJensen.lean — T37 cycle 108: weighted Cauchy–Schwarz without square roots.

  Lean 4 core plus NSChain (lsum, Cone, Le) and lib/RingTac. For weights a ≥ 0 and values X on a list l:
      Σ_p Σ_q a_p a_q (X_p − X_q)² + 2(ΣaX)² = 2(Σa)(ΣaX²)                     double_identity
      (ΣaX)² ≤ (Σa)(ΣaX²)            (in rings where 2x = 0 ⇒ x = 0 fails, stated doubled)   wcs2
  This is the step that turns a per-p bound X_p² ≤ BC into (Σ_p a_p X_p)² ≤ (Σa)² BC: the discrete
  Young inequality Σ_{p+q+r=0} a_p b_q c_r ≤ ‖a‖₁‖b‖₂‖c‖₂ for the triad sums of a truncation.
-/
import NSChain
import lib.RingTac

namespace WeightedJensen
open DetChain NSChain LibRing

variable {R : Type} (K : CRing R) {I : Type}

theorem lsum_lsum (f g : I → R) (l m : List I) :
    lsum K (fun p => lsum K (fun q => K.mul (f p) (g q)) m) l = K.mul (lsum K f l) (lsum K g m) := by
  have e : ∀ p, lsum K (fun q => K.mul (f p) (g q)) m = K.mul (f p) (lsum K g m) :=
    fun p => lsum_mul_left K (f p) g m
  rw [lsum_ext K _ _ e l]
  induction l with
  | nil => show K.zero = K.mul K.zero _; lib_ring
  | cons x l ih =>
    show K.add (K.mul (f x) (lsum K g m)) (lsum K (fun p => K.mul (f p) (lsum K g m)) l)
      = K.mul (K.add (f x) (lsum K f l)) (lsum K g m)
    rw [ih]; lib_ring

theorem double_identity (a X : I → R) (l : List I) :
    K.add (lsum K (fun p => lsum K (fun q => K.mul (K.mul (a p) (a q))
        (K.mul (sub K (X p) (X q)) (sub K (X p) (X q)))) l) l)
      (K.add (K.mul (lsum K (fun p => K.mul (a p) (X p)) l) (lsum K (fun p => K.mul (a p) (X p)) l))
             (K.mul (lsum K (fun p => K.mul (a p) (X p)) l) (lsum K (fun p => K.mul (a p) (X p)) l)))
      = K.add (K.mul (lsum K a l) (lsum K (fun p => K.mul (a p) (K.mul (X p) (X p))) l))
              (K.mul (lsum K a l) (lsum K (fun p => K.mul (a p) (K.mul (X p) (X p))) l)) := by
  -- expand the inner term into three product sums
  have e : ∀ p q, K.mul (K.mul (a p) (a q)) (K.mul (sub K (X p) (X q)) (sub K (X p) (X q)))
      = K.add (K.add (K.mul (K.mul (a p) (K.mul (X p) (X p))) (a q)) (K.mul (a p) (K.mul (a q) (K.mul (X q) (X q)))))
          (K.neg (K.add (K.mul (K.mul (a p) (X p)) (K.mul (a q) (X q))) (K.mul (K.mul (a p) (X p)) (K.mul (a q) (X q))))) := by
    intro p q; lib_ring
  have inner : ∀ p, lsum K (fun q => K.mul (K.mul (a p) (a q)) (K.mul (sub K (X p) (X q)) (sub K (X p) (X q)))) l
      = K.add (K.add (lsum K (fun q => K.mul (K.mul (a p) (K.mul (X p) (X p))) (a q)) l)
                     (lsum K (fun q => K.mul (a p) (K.mul (a q) (K.mul (X q) (X q)))) l))
          (K.neg (K.add (lsum K (fun q => K.mul (K.mul (a p) (X p)) (K.mul (a q) (X q))) l)
                        (lsum K (fun q => K.mul (K.mul (a p) (X p)) (K.mul (a q) (X q))) l))) := by
    intro p
    rw [lsum_ext K _ _ (e p) l, lsum_add, lsum_add, lsum_neg, lsum_add]
  rw [lsum_ext K _ _ inner l, lsum_add, lsum_add, lsum_neg, lsum_add,
    lsum_lsum K (fun p => K.mul (a p) (K.mul (X p) (X p))) a l l,
    lsum_lsum K a (fun q => K.mul (a q) (K.mul (X q) (X q))) l l,
    lsum_lsum K (fun p => K.mul (a p) (X p)) (fun q => K.mul (a q) (X q)) l l]
  generalize lsum K a l = A
  generalize lsum K (fun p => K.mul (a p) (K.mul (X p) (X p))) l = B
  generalize lsum K (fun p => K.mul (a p) (X p)) l = C
  lib_ring

/-- **Weighted Cauchy–Schwarz, doubled**: 2(ΣaX)² ≤ 2(Σa)(ΣaX²) for a ≥ 0. -/
theorem wcs2 (P : Cone K) (a X : I → R) (l : List I) (ha : ∀ p, P.nn (a p)) :
    Le K P (K.add (K.mul (lsum K (fun p => K.mul (a p) (X p)) l) (lsum K (fun p => K.mul (a p) (X p)) l))
                  (K.mul (lsum K (fun p => K.mul (a p) (X p)) l) (lsum K (fun p => K.mul (a p) (X p)) l)))
      (K.add (K.mul (lsum K a l) (lsum K (fun p => K.mul (a p) (K.mul (X p) (X p))) l))
             (K.mul (lsum K a l) (lsum K (fun p => K.mul (a p) (K.mul (X p) (X p))) l))) := by
  have nn : ∀ m : List I, ∀ f : I → R, (∀ p, P.nn (f p)) → P.nn (lsum K f m) := by
    intro m f hf; induction m with
    | nil => exact P.nn_zero
    | cons x m ih => exact P.nn_add _ _ (hf x) ih
  refine ⟨_, nn l _ (fun p => nn l _ (fun q => P.nn_mul _ _ (P.nn_mul _ _ (ha p) (ha q)) (P.nn_sq (sub K (X p) (X q))))), ?_⟩
  rw [K.add_comm]; exact double_identity K a X l

end WeightedJensen

#print axioms WeightedJensen.lsum_lsum
#print axioms WeightedJensen.double_identity
#print axioms WeightedJensen.wcs2

ns/WindowA.lean

lean_all/ns/WindowA.lean

/-
  ns/WindowA.lean — T37 (goal G859c2g, piece c1): the window H² bound in the form TruncRateUpto needs, over an
  ordered field with inverses. If for every t ≤ n
    νA_t + hεB_t ≤ E_0 + t·h·D       (H2LargeAll; h, D ≥ 0, ε, B_t ≥ 0)
  then for every t ≤ n
    window_a   A_t ≤ (E_0 + n·h·D)·ν⁻¹,
  a single constant Aa on the whole window (t·h·D ≤ n·h·D).

  Lean 4 core plus ns/H2ToA.
-/
import ns.H2ToA

namespace WindowA
open DetChain NSChain DyadicSmallGlobal LibRing DyadicLarge

section Gen
variable {R : Type} (K : CRing R) (O : OrdR K)
variable (inv : R → R) (mul_inv : ∀ y, y ≠ K.zero → K.mul y (inv y) = K.one)

include O mul_inv in
/-- **One H² constant on the window.** -/
theorem window_a (ν h ε D E0 : R) (A B : Nat → R) (h10 : K.one ≠ K.zero) (hν : O.nn ν) (hν0 : ν ≠ K.zero)
    (hh : O.nn h) (hε : O.nn ε) (hD : O.nn D) (hB : ∀ t, O.nn (B t)) (n : Nat)
    (hE : ∀ t, t ≤ n → Le K O.toCone (K.add (K.mul ν (A t)) (K.mul (K.mul h ε) (B t)))
      (K.add E0 (K.mul (castN K t) (K.mul h D)))) :
    ∀ t, t ≤ n → Le K O.toCone (A t) (K.mul (K.add E0 (K.mul (castN K n) (K.mul h D))) (inv ν)) := by
  intro t ht
  have c := RatModel.cast_mono K O.toCone _ _ ht
  have m := ForceSob.le_mul_right' K O.toCone c (O.nn_mul _ _ hh hD)
  have X := le_trans' K O.toCone _ _ _ (hE t ht) (le_add' K O.toCone _ _ _ _ (le_refl' K O.toCone E0) m)
  exact H2ToA.a_of_E K O inv mul_inv ν h ε (A t) (B t) _ h10 hν hν0 hh hε (hB t) X

end Gen

end WindowA

#print axioms WindowA.window_a

ns/WindowAt.lean

lean_all/ns/WindowAt.lean

/-
  ns/WindowAt.lean — T37 (goal G859c2-s3b, first piece): the large-data truncation Cauchy step on a window [m, m + n]
  with an arbitrary start m (ns/WindowShift.large_window has m = n·k): window starts can be placed at good steps
  (ns/GoodStep), with windows of different lengths.
    large_window_at   ν^n Q_d(m + n) ≤ (ν + hα)^n (Q_d(m) + n·h·S) under the local hypotheses on [m, m + n]
  Lean 4 core plus ns/WindowShift.
-/
import ns.WindowShift

namespace WindowAt
open DetChain NSChain DyadicSmallGlobal LibRing DyadicLarge FourierGalerkin GalerkinForce ForceBound ForceSob
  ModeCount CubeCount ForceUniform NormCompare EnstStep EnstGlobal EnstGalerkin ForceTwoScale ForceBilin EnstDiff
  ForceDiff TruncError TruncBound RestrStep GronwallSource EnstGronwall WeightSplit LatticeSum TruncCauchy TruncRate
  WindowShift

section Gen
variable {R : Type} (K : CRing R) (O : OrdR K) {I : Type}
variable (inv : R → R) (mul_inv : ∀ y, y ≠ K.zero → K.mul y (inv y) = K.one) (h10 : K.one ≠ K.zero)

include mul_inv h10 in
/-- **LargeCauchy on the window [m, m + n], any start m** -/
theorem large_window_at (l : List I) (kz : I → Mode) (hd : List.Pairwise (fun a b => kz a ≠ kz b) l)
    (h1 : ∀ p, 1 ≤ nrm (kz p)) (M : Nat) (hM : ∀ p, p ∈ l → nrm (kz p) ≤ M + 1) (Rc R' t0 : Nat) (hRM : R' ≤ M)
    (ι h ν Zb Aa Ab iR : R) (hh : O.nn h) (hν : O.nn ν) (hZb : O.nn Zb) (hAa : O.nn Aa) (hAb : O.nn Ab) (hiRn : O.nn iR)
    (hiR : K.mul (K.mul (castN K (Rc + 1)) (castN K (Rc + 1))) iR = K.one)
    (A B : Nat → I → V3 R)
    (hA : ∀ t r, Step K h ν (wt K kz r) (A (t + 1) r) (A t r) (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (A t) (A t) r))
    (hB : ∀ t r, r ∈ l.filter (lowB Rc kz) → Step K h ν (wt K kz r) (B (t + 1) r) (B t r)
      (Nl K (l.filter (lowB Rc kz)) (TriadSparse.delta K kz) ι (kv K kz) (B t) (B t) r))
    (n m : Nat) (hZA : ∀ t, t < n → Le K O.toCone (Zl K kz l (A (m + t))) Zb)
    (εA εB DA DB EA EB : R) (hεA : O.nn εA) (hεB : O.nn εB) (hDA : O.nn DA) (hDB : O.nn DB) (hν0 : ν ≠ K.zero)
    (hEA : ∀ t, t ≤ n → Le K O.toCone (K.add (K.mul ν (Al K kz l (A (m + t))))
      (K.mul (K.mul h εA) (lsum K (fun p => K.mul (K.mul (K.mul (wt K kz p) (wt K kz p)) (wt K kz p))
        (dot3 K (A (m + t) p) (A (m + t) p))) l)))
      (K.add EA (K.mul (castN K t) (K.mul h DA))))
    (hEB : ∀ t, t ≤ n → Le K O.toCone (K.add (K.mul ν (Al K kz (l.filter (lowB Rc kz)) (B (m + t))))
      (K.mul (K.mul h εB) (lsum K (fun p => K.mul (K.mul (K.mul (wt K kz p) (wt K kz p)) (wt K kz p))
        (dot3 K (B (m + t) p) (B (m + t) p))) (l.filter (lowB Rc kz)))))
      (K.add EB (K.mul (castN K t) (K.mul h DB))))
    (hAa_def : Aa = K.mul (K.add EA (K.mul (castN K n) (K.mul h DA))) (inv ν))
    (hAb_def : Ab = K.mul (K.add EB (K.mul (castN K n) (K.mul h DB))) (inv ν))
    (hβc : Le K O.toCone (K.mul (K.mul (two' K) (K.mul (two' K) (two' K))) (K.mul (K.mul ι ι) (K.mul (C2' K inv R' t0) Zb)))
      (K.mul ν ν)) :
    Le K O.toCone (K.mul (pwr K ν n)
        (Q K ν h (fun t => Zl K kz (l.filter (lowB Rc kz)) (dF K (restr K (lowB Rc kz) (A t)) (B t)))
          (fun t => Al K kz (l.filter (lowB Rc kz)) (dF K (restr K (lowB Rc kz) (A t)) (B t))) (m + n)))
      (K.mul (pwr K (K.add ν (K.mul h
          (K.add (K.mul (K.mul (two' K) (K.mul (two' K) (two' K))) (K.mul (K.mul ι ι) (K.mul (C1' K inv R') Zb)))
            (K.mul (K.mul (two' K) (two' K)) (K.mul (K.mul ι ι) (K.mul (castN K 135) Ab)))))) n)
        (K.add (Q K ν h (fun t => Zl K kz (l.filter (lowB Rc kz)) (dF K (restr K (lowB Rc kz) (A t)) (B t)))
          (fun t => Al K kz (l.filter (lowB Rc kz)) (dF K (restr K (lowB Rc kz) (A t)) (B t))) (m))
          (K.mul (castN K n) (K.mul h (K.mul (two' K)
            (K.add
              (K.mul (two' K) (K.add
                (K.mul (two' K) (K.mul (K.mul ι ι) (K.mul (K.mul Aa iR) (K.mul (C1' K inv R') Zb))))
                (K.mul (two' K) (K.mul (K.mul ι ι) (K.mul Aa (K.mul (C2' K inv R' t0) Zb))))))
              (K.mul (two' K) (K.mul (K.mul ι ι) (K.mul Aa (K.mul (castN K 135) (K.mul Aa iR))))))))))) :=
  LargeCauchy.large_cauchy K O inv mul_inv h10 l kz hd h1 M hM Rc R' t0 hRM ι h ν Zb Aa Ab iR hh hν hZb hAa hAb
    hiRn hiR (fun t => A (m + t)) (fun t => B (m + t)) (fun t r => hA (m + t) r)
    (fun t r hr => hB (m + t) r hr) n hZA εA εB DA DB EA EB hεA hεB hDA hDB hν0 hEA hEB hAa_def hAb_def hβc
    n (Nat.le.refl)

end Gen

end WindowAt

#print axioms WindowAt.large_window_at

ns/WindowBound.lean

lean_all/ns/WindowBound.lean

/-
  ns/WindowBound.lean — T37 (goal G795b, piece d): the chained window bound is monotone in the per-window factor,
  over an ordered ring. For 0 ≤ c ≤ c′ and d₀, s₀ ≥ 0:
    le_mul2      0 ≤ a ≤ a′, 0 ≤ b ≤ b′ ⇒ ab ≤ a′b′,
    pwr_mono     c^K ≤ c′^K,
    chain_mono   c^K d₀ + c s₀ Σ_{j<K} c^j ≤ c′^K d₀ + c′ s₀ Σ_{j<K} c′^j,
    chain_bound  a bound of WindowShift.scale_chain form with factor c passes to any c′ ≥ c,
  so with WindowFactor.window_factor (c ≤ 2^k) the large-data window chain has the explicit factor 2^k per window.

    factor_chain with WindowFactor: a scale_chain bound with c = (ν + hα)^(m·k)(ν⁻¹)^(m·k) holds with c′ = 2^k.

  Lean 4 core plus ns/WindowChain, ns/ForceSob, ns/WindowFactor.
-/
import ns.WindowChain
import ns.ForceSob
import ns.WindowFactor

namespace WindowBound
open DetChain NSChain DyadicSmallGlobal LibRing DyadicLarge EnstGronwall

section Gen
variable {R : Type} (K : CRing R) (O : OrdR K)

/-- 0 ≤ a ≤ a′, 0 ≤ b ≤ b′ ⇒ ab ≤ a′b′ -/
theorem le_mul2 (a a' b b' : R) (ha : O.nn a) (hb' : O.nn b') (hab : Le K O.toCone a a') (hbb : Le K O.toCone b b') :
    Le K O.toCone (K.mul a b) (K.mul a' b') :=
  le_trans' K O.toCone _ _ _ (le_mul_nn K O.toCone a _ _ ha hbb) (ForceSob.le_mul_right' K O.toCone hab hb')

/-- c^K ≤ c′^K -/
theorem pwr_mono (c c' : R) (hc : O.nn c) (hc' : O.nn c') (h : Le K O.toCone c c') :
    ∀ n : Nat, Le K O.toCone (pwr K c n) (pwr K c' n)
  | 0 => le_refl' K O.toCone _
  | n + 1 => le_mul2 K O c c' (pwr K c n) (pwr K c' n) hc (pwr_nn K O c' hc' n) h (pwr_mono c c' hc hc' h n)

theorem sum_pwr_nn (c : R) (hc : O.nn c) : ∀ n : Nat, O.nn (sumN K (pwr K c) n)
  | 0 => O.nn_zero
  | n + 1 => O.nn_add _ _ (sum_pwr_nn c hc n) (pwr_nn K O c hc n)

/-- **the chained bound is monotone in the factor** -/
theorem chain_mono (c c' d0 s0 : R) (hc : O.nn c) (hc' : O.nn c') (h : Le K O.toCone c c') (hd0 : O.nn d0)
    (hs0 : O.nn s0) (n : Nat) :
    Le K O.toCone (K.add (K.mul (pwr K c n) d0) (K.mul (K.mul c s0) (sumN K (pwr K c) n)))
      (K.add (K.mul (pwr K c' n) d0) (K.mul (K.mul c' s0) (sumN K (pwr K c') n))) :=
  le_add' K O.toCone _ _ _ _
    (ForceSob.le_mul_right' K O.toCone (pwr_mono K O c c' hc hc' h n) hd0)
    (le_mul2 K O _ _ _ _ (O.nn_mul _ _ hc hs0) (sum_pwr_nn K O c' hc' n)
      (ForceSob.le_mul_right' K O.toCone h hs0)
      (sum_le K O.toCone _ _ (pwr_mono K O c c' hc hc' h) n))

/-- **pass a chained window bound to a larger factor** -/
theorem chain_bound (c c' s0 : R) (d : Nat → R) (hc : O.nn c) (hc' : O.nn c') (h : Le K O.toCone c c')
    (hd0 : O.nn (d 0)) (hs0 : O.nn s0)
    (hch : ∀ n : Nat, Le K O.toCone (d n) (K.add (K.mul (pwr K c n) (d 0)) (K.mul (K.mul c s0) (sumN K (pwr K c) n))))
    (n : Nat) :
    Le K O.toCone (d n) (K.add (K.mul (pwr K c' n) (d 0)) (K.mul (K.mul c' s0) (sumN K (pwr K c') n))) :=
  le_trans' K O.toCone _ _ _ (hch n) (chain_mono K O c c' (d 0) s0 hc hc' h hd0 hs0 n)

/-- **the window chain with the explicit factor 2^k per window** -/
theorem factor_chain (ν hα iν s0 : R) (m k : Nat) (d : Nat → R) (hν : O.nn ν) (hνi : K.mul ν iν = K.one)
    (hiν : O.nn iν) (ha : O.nn (K.mul hα iν))
    (hm : Le K O.toCone (K.mul (K.add K.one K.one) (K.mul (castN K m) (K.mul hα iν))) K.one)
    (hc : O.nn (K.mul (pwr K (K.add ν hα) (m * k)) (pwr K iν (m * k)))) (hd0 : O.nn (d 0)) (hs0 : O.nn s0)
    (hch : ∀ n : Nat, Le K O.toCone (d n)
      (K.add (K.mul (pwr K (K.mul (pwr K (K.add ν hα) (m * k)) (pwr K iν (m * k))) n) (d 0))
        (K.mul (K.mul (K.mul (pwr K (K.add ν hα) (m * k)) (pwr K iν (m * k))) s0)
          (sumN K (pwr K (K.mul (pwr K (K.add ν hα) (m * k)) (pwr K iν (m * k)))) n))))
    (n : Nat) :
    Le K O.toCone (d n) (K.add (K.mul (pwr K (pwr K (K.add K.one K.one) k) n) (d 0))
      (K.mul (K.mul (pwr K (K.add K.one K.one) k) s0) (sumN K (pwr K (pwr K (K.add K.one K.one) k)) n))) :=
  chain_bound K O _ _ s0 d hc
    (pwr_nn K O _ (O.nn_add _ _ (RatModel.nn_one' K O.toCone) (RatModel.nn_one' K O.toCone)) k)
    (WindowFactor.window_factor K O ν hα iν hν hνi hiν ha m k hm) hd0 hs0 hch n

end Gen

end WindowBound

#print axioms WindowBound.le_mul2
#print axioms WindowBound.pwr_mono
#print axioms WindowBound.sum_pwr_nn
#print axioms WindowBound.chain_mono
#print axioms WindowBound.chain_bound
#print axioms WindowBound.factor_chain

ns/WindowChain.lean

lean_all/ns/WindowChain.lean

/-
  ns/WindowChain.lean — T37 (goal G795b, piece a): chaining window estimates, over an ordered ring. If on consecutive
  windows the truncation difference energy obeys d_{k+1} ≤ c·d_k + s (c, s ≥ 0; LargeCauchy on window k, with the
  restart at the window start), then
    window_chain   d_K ≤ c^K·d_0 + s·Σ_{j<K} c^j.
  With c and s from LargeCauchy/GronwallH (c ≈ 2^k, s → 0 as the cutoff grows) the Cauchy property passes from one
  window to K windows — as long as each window's local-time hypotheses hold (bounded Q at the window starts).

  Lean 4 core plus ns/EnstGronwall (pwr), NSChain (sumN).
-/
import ns.EnstGronwall

namespace WindowChain
open DetChain NSChain DyadicSmallGlobal LibRing DyadicLarge EnstGronwall

section Gen
variable {R : Type} (K : CRing R) (O : OrdR K)

/-- c·Σ_{j<k} c^j + 1 = Σ_{j<k+1} c^j -/
theorem geom_shift (c : R) : ∀ k : Nat,
    K.add (K.mul c (sumN K (pwr K c) k)) K.one = sumN K (pwr K c) (k + 1)
  | 0 => by show K.add (K.mul c K.zero) K.one = K.add K.zero K.one; lib_ring
  | k + 1 => by
    have ih := geom_shift c k
    show K.add (K.mul c (K.add (sumN K (pwr K c) k) (pwr K c k))) K.one
      = K.add (sumN K (pwr K c) (k + 1)) (K.mul c (pwr K c k))
    rw [← ih]; lib_ring

/-- **d_K ≤ c^K d_0 + s Σ_{j<K} c^j** -/
theorem window_chain (c s : R) (d : Nat → R) (hc : O.nn c)
    (hstep : ∀ k, Le K O.toCone (d (k + 1)) (K.add (K.mul c (d k)) s)) :
    ∀ K' : Nat, Le K O.toCone (d K') (K.add (K.mul (pwr K c K') (d 0)) (K.mul s (sumN K (pwr K c) K'))) := by
  intro K'
  induction K' with
  | zero => exact ⟨K.zero, O.nn_zero, by show K.add (d 0) K.zero = K.add (K.mul K.one (d 0)) (K.mul s K.zero); lib_ring⟩
  | succ k ih =>
    have m := le_mul_nn K O.toCone c _ _ hc ih
    have t := le_trans' K O.toCone _ _ _ (hstep k) (le_add' K O.toCone _ _ _ _ m (le_refl' K O.toCone s))
    refine le_trans' K O.toCone _ _ _ t ⟨K.zero, O.nn_zero, ?_⟩
    show K.add (K.add (K.mul c (K.add (K.mul (pwr K c k) (d 0)) (K.mul s (sumN K (pwr K c) k)))) s) K.zero
      = K.add (K.mul (K.mul c (pwr K c k)) (d 0)) (K.mul s (sumN K (pwr K c) (k + 1)))
    rw [← geom_shift K c k]; lib_ring

end Gen

end WindowChain

#print axioms WindowChain.geom_shift
#print axioms WindowChain.window_chain

ns/WindowChainUpto.lean

lean_all/ns/WindowChainUpto.lean

/-
  ns/WindowChainUpto.lean — T37 (goal G795b-i, piece a): WindowChain.window_chain with the step hypothesis only on
  the first K′ windows, over an ordered ring:
    window_chain_upto   d_{k+1} ≤ c·d_k + s for k < K′  ⇒  d_j ≤ c^j d_0 + s·Σ_{i<j} c^i for j ≤ K′,
    lin_upto            E_{k+1} ≤ E_k + D for k < K′  ⇒  E_j ≤ E_0 + j·D for j ≤ K′,
  so on K′ windows the window-start H² energies are bounded by E_0 + K′·D without an extra hypothesis.

    scale_chain_upto    WindowShift.scale_chain on the first K′ windows.

  Lean 4 core plus ns/WindowChain, ns/WindowShift.
-/
import ns.WindowChain
import ns.WindowShift

namespace WindowChainUpto
open DetChain NSChain DyadicSmallGlobal LibRing DyadicLarge EnstGronwall

section Gen
variable {R : Type} (K : CRing R) (O : OrdR K)

/-- **d_j ≤ c^j d_0 + s Σ_{i<j} c^i for j ≤ K′** -/
theorem window_chain_upto (c s : R) (d : Nat → R) (hc : O.nn c) (K' : Nat)
    (hstep : ∀ k, k < K' → Le K O.toCone (d (k + 1)) (K.add (K.mul c (d k)) s)) :
    ∀ j : Nat, j ≤ K' →
      Le K O.toCone (d j) (K.add (K.mul (pwr K c j) (d 0)) (K.mul s (sumN K (pwr K c) j)))
  | 0, _ => ⟨K.zero, O.nn_zero, by show K.add (d 0) K.zero = K.add (K.mul K.one (d 0)) (K.mul s K.zero); lib_ring⟩
  | k + 1, hk => by
    have ih := window_chain_upto c s d hc K' hstep k (Nat.le_of_succ_le hk)
    have m := le_mul_nn K O.toCone c _ _ hc ih
    have t := le_trans' K O.toCone _ _ _ (hstep k hk) (le_add' K O.toCone _ _ _ _ m (le_refl' K O.toCone s))
    refine le_trans' K O.toCone _ _ _ t ⟨K.zero, O.nn_zero, ?_⟩
    show K.add (K.add (K.mul c (K.add (K.mul (pwr K c k) (d 0)) (K.mul s (sumN K (pwr K c) k)))) s) K.zero
      = K.add (K.mul (K.mul c (pwr K c k)) (d 0)) (K.mul s (sumN K (pwr K c) (k + 1)))
    rw [← WindowChain.geom_shift K c k]; lib_ring

/-- **E_j ≤ E_0 + j·D for j ≤ K′** -/
theorem lin_upto (E : Nat → R) (D : R) (K' : Nat) (hstep : ∀ k, k < K' → Le K O.toCone (E (k + 1)) (K.add (E k) D)) :
    ∀ j : Nat, j ≤ K' → Le K O.toCone (E j) (K.add (E 0) (K.mul (castN K j) D))
  | 0, _ => ⟨K.zero, O.nn_zero, by show K.add (E 0) K.zero = K.add (E 0) (K.mul K.zero D); lib_ring⟩
  | k + 1, hk => by
    have ih := lin_upto E D K' hstep k (Nat.le_of_succ_le hk)
    have t := le_trans' K O.toCone _ _ _ (hstep k hk) (le_add' K O.toCone _ _ _ _ ih (le_refl' K O.toCone D))
    refine le_trans' K O.toCone _ _ _ t ⟨K.zero, O.nn_zero, ?_⟩
    show K.add (K.add (K.add (E 0) (K.mul (castN K k) D)) D) K.zero = K.add (E 0) (K.mul (K.add K.one (castN K k)) D)
    lib_ring

/-- **scale_chain on the first K′ windows** -/
theorem scale_chain_upto (ν iν P s0 : R) (n : Nat) (d : Nat → R) (hνi : K.mul ν iν = K.one) (hiν : O.nn iν)
    (hP : O.nn P) (K' : Nat)
    (hwin : ∀ k, k < K' → Le K O.toCone (K.mul (pwr K ν n) (d (k + 1))) (K.mul P (K.add (d k) s0))) :
    ∀ j : Nat, j ≤ K' → Le K O.toCone (d j)
      (K.add (K.mul (pwr K (K.mul P (pwr K iν n)) j) (d 0))
        (K.mul (K.mul (K.mul P (pwr K iν n)) s0) (sumN K (pwr K (K.mul P (pwr K iν n))) j))) :=
  window_chain_upto K O (K.mul P (pwr K iν n)) (K.mul (K.mul P (pwr K iν n)) s0) d
    (O.nn_mul _ _ hP (pwr_nn K O iν hiν n)) K'
    (fun k hk => WindowShift.scale_step K O ν iν P s0 (d (k + 1)) (d k) n hνi hiν (hwin k hk))

end Gen

end WindowChainUpto

#print axioms WindowChainUpto.window_chain_upto
#print axioms WindowChainUpto.lin_upto
#print axioms WindowChainUpto.scale_chain_upto

ns/WindowClosed.lean

lean_all/ns/WindowClosed.lean

/-
  ns/WindowClosed.lean — T37 (goal G795b, piece g): the large-data truncation Cauchy bound over K′ windows in closed
  form. Under the hypotheses of WindowLarge.large_explicit with k ≥ 1 (windows of n = m·k steps):
    large_closed   Q_d(n·K′) ≤ (2^k)^K′ (Q_d(0) + 2^k S₀),
  with S₀ = n·h·S → 0 as the cutoff grows (TruncRate): the truncation difference stays small on any fixed number of
  windows, provided the local-time hypotheses hold at every window start.

  Lean 4 core plus ns/WindowLarge, ns/WindowGeom.
-/
import ns.WindowLarge
import ns.WindowGeom

namespace WindowClosed
open DetChain NSChain DyadicSmallGlobal LibRing DyadicLarge FourierGalerkin GalerkinForce ForceBound ForceSob
  ModeCount CubeCount ForceUniform NormCompare EnstStep EnstGlobal EnstGalerkin ForceTwoScale ForceBilin EnstDiff
  ForceDiff TruncError TruncBound RestrStep GronwallSource EnstGronwall WeightSplit LatticeSum TruncCauchy TruncRate

section Gen
variable {R : Type} (K : CRing R) (O : OrdR K) {I : Type}
variable (inv : R → R) (mul_inv : ∀ y, y ≠ K.zero → K.mul y (inv y) = K.one) (h10 : K.one ≠ K.zero)

include mul_inv h10 in
/-- **Closed-form large-data truncation Cauchy bound over K′ windows.** -/
theorem large_closed (l : List I) (kz : I → Mode) (hd : List.Pairwise (fun a b => kz a ≠ kz b) l)
    (h1 : ∀ p, 1 ≤ nrm (kz p)) (M : Nat) (hM : ∀ p, p ∈ l → nrm (kz p) ≤ M + 1) (Rc R' t0 : Nat) (hRM : R' ≤ M)
    (ι h ν Zb Aa Ab iR : R) (hh : O.nn h) (hν : O.nn ν) (hZb : O.nn Zb) (hAa : O.nn Aa) (hAb : O.nn Ab) (hiRn : O.nn iR)
    (hiR : K.mul (K.mul (castN K (Rc + 1)) (castN K (Rc + 1))) iR = K.one)
    (A B : Nat → I → V3 R)
    (hA : ∀ t r, Step K h ν (wt K kz r) (A (t + 1) r) (A t r) (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (A t) (A t) r))
    (hB : ∀ t r, r ∈ l.filter (lowB Rc kz) → Step K h ν (wt K kz r) (B (t + 1) r) (B t r)
      (Nl K (l.filter (lowB Rc kz)) (TriadSparse.delta K kz) ι (kv K kz) (B t) (B t) r))
    (n : Nat) (hZA : ∀ k t, t < n → Le K O.toCone (Zl K kz l (A (n * k + t))) Zb)
    (εA εB DA DB EA EB : R) (hεA : O.nn εA) (hεB : O.nn εB) (hDA : O.nn DA) (hDB : O.nn DB) (hν0 : ν ≠ K.zero)
    (hEA : ∀ k t, t ≤ n → Le K O.toCone (K.add (K.mul ν (Al K kz l (A (n * k + t))))
      (K.mul (K.mul h εA) (lsum K (fun p => K.mul (K.mul (K.mul (wt K kz p) (wt K kz p)) (wt K kz p))
        (dot3 K (A (n * k + t) p) (A (n * k + t) p))) l)))
      (K.add EA (K.mul (castN K t) (K.mul h DA))))
    (hEB : ∀ k t, t ≤ n → Le K O.toCone (K.add (K.mul ν (Al K kz (l.filter (lowB Rc kz)) (B (n * k + t))))
      (K.mul (K.mul h εB) (lsum K (fun p => K.mul (K.mul (K.mul (wt K kz p) (wt K kz p)) (wt K kz p))
        (dot3 K (B (n * k + t) p) (B (n * k + t) p))) (l.filter (lowB Rc kz)))))
      (K.add EB (K.mul (castN K t) (K.mul h DB))))
    (hAa_def : Aa = K.mul (K.add EA (K.mul (castN K n) (K.mul h DA))) (inv ν))
    (hAb_def : Ab = K.mul (K.add EB (K.mul (castN K n) (K.mul h DB))) (inv ν))
    (hβc : Le K O.toCone (K.mul (K.mul (two' K) (K.mul (two' K) (two' K))) (K.mul (K.mul ι ι) (K.mul (C2' K inv R' t0) Zb)))
      (K.mul ν ν))
    (S0 : R)
    (hS0def : S0 = K.mul (castN K n) (K.mul h (K.mul (two' K)
            (K.add
              (K.mul (two' K) (K.add
                (K.mul (two' K) (K.mul (K.mul ι ι) (K.mul (K.mul Aa iR) (K.mul (C1' K inv R') Zb))))
                (K.mul (two' K) (K.mul (K.mul ι ι) (K.mul Aa (K.mul (C2' K inv R' t0) Zb))))))
              (K.mul (two' K) (K.mul (K.mul ι ι) (K.mul Aa (K.mul (castN K 135) (K.mul Aa iR)))))))))
    (iν : R) (hνi : K.mul ν iν = K.one) (hiν : O.nn iν) (m k : Nat) (hn : n = m * k)
    (ha : O.nn (K.mul (K.mul h (K.add (K.mul (K.mul (two' K) (K.mul (two' K) (two' K))) (K.mul (K.mul ι ι) (K.mul (C1' K inv R') Zb)))
            (K.mul (K.mul (two' K) (two' K)) (K.mul (K.mul ι ι) (K.mul (castN K 135) Ab))))) iν))
    (hm : Le K O.toCone (K.mul (K.add K.one K.one) (K.mul (castN K m) (K.mul (K.mul h (K.add (K.mul (K.mul (two' K) (K.mul (two' K) (two' K))) (K.mul (K.mul ι ι) (K.mul (C1' K inv R') Zb)))
            (K.mul (K.mul (two' K) (two' K)) (K.mul (K.mul ι ι) (K.mul (castN K 135) Ab))))) iν))) K.one)
    (hS0 : O.nn S0)
    (hk : 1 ≤ k) (K' : Nat) :
    Le K O.toCone ((Q K ν h (fun t => Zl K kz (l.filter (lowB Rc kz)) (dF K (restr K (lowB Rc kz) (A t)) (B t)))
          (fun t => Al K kz (l.filter (lowB Rc kz)) (dF K (restr K (lowB Rc kz) (A t)) (B t)))) (n * K'))
      (K.mul (pwr K (pwr K (K.add K.one K.one) k) K') (K.add ((Q K ν h (fun t => Zl K kz (l.filter (lowB Rc kz)) (dF K (restr K (lowB Rc kz) (A t)) (B t)))
          (fun t => Al K kz (l.filter (lowB Rc kz)) (dF K (restr K (lowB Rc kz) (A t)) (B t)))) (n * 0)) (K.mul (pwr K (K.add K.one K.one) k) S0))) := by
  have e := WindowLarge.large_explicit K O inv mul_inv h10 l kz hd h1 M hM Rc R' t0 hRM ι h ν Zb Aa Ab iR hh hν hZb
    hAa hAb hiRn hiR A B hA hB n hZA εA εB DA DB EA EB hεA hεB hDA hDB hν0 hEA hEB hAa_def hAb_def hβc S0 hS0def
    iν hνi hiν m k hn ha hm hS0 K'
  have h2 : Le K O.toCone (K.add K.one K.one) (pwr K (K.add K.one K.one) k) := by
    cases k with
    | zero => exact absurd hk (Nat.not_succ_le_zero 0)
    | succ k' => exact WindowGeom.two_pow_ge K O k'
  have hc : O.nn (pwr K (K.add K.one K.one) k) := pwr_nn K O _ (O.nn_add _ _ (RatModel.nn_one' K O.toCone) (RatModel.nn_one' K O.toCone)) k
  exact le_trans' K O.toCone _ _ _ e (WindowGeom.chain_closed K O _ _ S0 hc h2 hS0 K')

end Gen

end WindowClosed

#print axioms WindowClosed.large_closed

ns/WindowCond.lean

lean_all/ns/WindowCond.lean

/-
  ns/WindowCond.lean — T37 (goal G795b-h, piece d): the closed-form truncation bound over many windows under two
  bounds at the window starts only: if Q(n·j) ≤ Qmax and the H² energy at n·j is at most Emax for every window
  start, with Qmax + n·h·c₁Zb² ≤ ν·Zb and the absorption parameters of H2Large, then
    large_cond   Q_d(n·K′) ≤ (2^k)^K′ (Q_d(0) + 2^k S₀)
  (the small-truncation energy hypothesis hEB is kept). This is the conditional form: bounded Q and H² energy at the
  window starts ⇒ the Galerkin truncations are Cauchy on every finite time interval.

  Lean 4 core plus ns/WindowClosed, ns/WindowHyps.
-/
import ns.WindowClosed
import ns.WindowHyps

namespace WindowCond
open DetChain NSChain DyadicSmallGlobal LibRing DyadicLarge FourierGalerkin GalerkinForce ForceBound ForceSob
  ModeCount CubeCount ForceUniform NormCompare EnstStep EnstGlobal EnstGalerkin ForceTwoScale ForceBilin EnstDiff
  ForceDiff TruncError TruncBound RestrStep GronwallSource EnstGronwall WeightSplit LatticeSum TruncCauchy TruncRate

section Gen
variable {R : Type} (K : CRing R) (O : OrdR K) {I : Type}
variable (inv : R → R) (mul_inv : ∀ y, y ≠ K.zero → K.mul y (inv y) = K.one) (h10 : K.one ≠ K.zero)

include mul_inv h10 in
/-- **Conditional large-data truncation Cauchy bound over many windows.** -/
theorem large_cond (l : List I) (kz : I → Mode) (hd : List.Pairwise (fun a b => kz a ≠ kz b) l)
    (h1 : ∀ p, 1 ≤ nrm (kz p)) (M Mm : Nat) (hMm : M = Mm + 1) (hM : ∀ p, p ∈ l → nrm (kz p) ≤ M + 1) (Rc R' t0 : Nat) (hRM : R' ≤ M)
    (ι h ν Zb Aa Ab iR : R) (hh : O.nn h) (hν : O.nn ν) (hZb : O.nn Zb) (hAa : O.nn Aa) (hAb : O.nn Ab) (hiRn : O.nn iR)
    (hiR : K.mul (K.mul (castN K (Rc + 1)) (castN K (Rc + 1))) iR = K.one)
    (A B : Nat → I → V3 R)
    (hA : ∀ t r, Step K h ν (wt K kz r) (A (t + 1) r) (A t r) (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (A t) (A t) r))
    (hB : ∀ t r, r ∈ l.filter (lowB Rc kz) → Step K h ν (wt K kz r) (B (t + 1) r) (B t r)
      (Nl K (l.filter (lowB Rc kz)) (TriadSparse.delta K kz) ι (kv K kz) (B t) (B t) r))
    (n : Nat)
    (εA εB DA DB EA EB : R) (hεA : O.nn εA) (hεB : O.nn εB) (hDA : O.nn DA) (hDB : O.nn DB) (hν0 : ν ≠ K.zero)
    (Rl t1 : Nat) (hRl : Rl ≤ M)
    (choice : Le K O.toCone (K.mul (EnstLocalGalerkin.cc K ι (EnstLocalGalerkin.C2 K inv Rl t1)) Zb) (K.mul ν ν))
    (Qmax Emax : R)
    (hQ : ∀ j, Le K O.toCone (EnstGlobal.Q K ν h (EnstGalerkin.Zs K l kz A) (EnstGalerkin.As K l kz A) (n * j)) Qmax)
    (hbud : Le K O.toCone (K.add Qmax
      (K.mul (castN K n) (K.mul h (K.mul (EnstLocalGalerkin.cc K ι (EnstLocalGalerkin.C1 K inv Rl)) (K.mul Zb Zb)))))
      (K.mul ν Zb))
    (e D1 D2 D3 : R) (he : O.nn e) (hD1 : O.nn D1) (hD2 : O.nn D2) (hD3 : O.nn D3)
    (H1 : Le K O.toCone (K.mul (AgmonAbsorb.c3 K (castN K 16)) (AgmonAbsorb.q5 K Zb))
      (K.mul (AgmonAbsorb.c3 K (castN K 9)) (K.mul (RootYoung.four K) (K.mul (AgmonAbsorb.c3 K e) D1))))
    (H2 : Le K O.toCone (K.mul (castN K 16) (AgmonAbsorb.q5 K Zb))
      (K.mul (castN K 9) (K.mul (RootYoung.four K) (K.mul (AgmonAbsorb.c3 K e) D2))))
    (H3 : Le K O.toCone (AgmonAbsorb.c3 K Zb) (K.mul (RootYoung.four K) (K.mul e D3)))
    (hε : Le K O.toCone (K.mul (K.mul ι ι) (K.add (K.mul (AgmonCollapse.k1 K (castN K 3) (castN K 9) (castN K 108)) e)
        (K.mul (AgmonCollapse.k2 K (castN K 9) (castN K 108)) (K.add e (K.mul (castN K 2) e))))) (K.mul ν ν))
    (hE : ∀ j, Le K O.toCone (H2Linear.En K ν h (K.mul (K.mul ι ι) (K.add (K.mul (AgmonCollapse.k1 K (castN K 3) (castN K 9) (castN K 108)) e)
        (K.mul (AgmonCollapse.k2 K (castN K 9) (castN K 108)) (K.add e (K.mul (castN K 2) e)))))
        (fun s => ForceAbsorb.As K kz l (A s)) (fun s => ForceAbsorb.Bs K kz l (A s)) (n * j)) Emax)
    (hεAdef : εA = (K.mul (K.mul ι ι) (K.add (K.mul (AgmonCollapse.k1 K (castN K 3) (castN K 9) (castN K 108)) e)
        (K.mul (AgmonCollapse.k2 K (castN K 9) (castN K 108)) (K.add e (K.mul (castN K 2) e)))))) (hDAdef : DA = (K.mul (K.mul ι ι)
          (K.add (K.mul (AgmonCollapse.k1 K (castN K 3) (castN K 9) (castN K 108))
              (K.add D1 (K.mul (AgmonAbsorb.c3 K (castN K 2)) (K.mul Zb Zb))))
            (K.mul (AgmonCollapse.k2 K (castN K 9) (castN K 108)) (K.add D2 (K.mul (castN K 2) D3)))))) (hEAdef : EA = Emax)
    (hEB : ∀ k t, t ≤ n → Le K O.toCone (K.add (K.mul ν (Al K kz (l.filter (lowB Rc kz)) (B (n * k + t))))
      (K.mul (K.mul h εB) (lsum K (fun p => K.mul (K.mul (K.mul (wt K kz p) (wt K kz p)) (wt K kz p))
        (dot3 K (B (n * k + t) p) (B (n * k + t) p))) (l.filter (lowB Rc kz)))))
      (K.add EB (K.mul (castN K t) (K.mul h DB))))
    (hAa_def : Aa = K.mul (K.add EA (K.mul (castN K n) (K.mul h DA))) (inv ν))
    (hAb_def : Ab = K.mul (K.add EB (K.mul (castN K n) (K.mul h DB))) (inv ν))
    (hβc : Le K O.toCone (K.mul (K.mul (two' K) (K.mul (two' K) (two' K))) (K.mul (K.mul ι ι) (K.mul (C2' K inv R' t0) Zb)))
      (K.mul ν ν))
    (S0 : R)
    (hS0def : S0 = K.mul (castN K n) (K.mul h (K.mul (two' K)
            (K.add
              (K.mul (two' K) (K.add
                (K.mul (two' K) (K.mul (K.mul ι ι) (K.mul (K.mul Aa iR) (K.mul (C1' K inv R') Zb))))
                (K.mul (two' K) (K.mul (K.mul ι ι) (K.mul Aa (K.mul (C2' K inv R' t0) Zb))))))
              (K.mul (two' K) (K.mul (K.mul ι ι) (K.mul Aa (K.mul (castN K 135) (K.mul Aa iR)))))))))
    (iν : R) (hνi : K.mul ν iν = K.one) (hiν : O.nn iν) (m k : Nat) (hn : n = m * k)
    (ha : O.nn (K.mul (K.mul h (K.add (K.mul (K.mul (two' K) (K.mul (two' K) (two' K))) (K.mul (K.mul ι ι) (K.mul (C1' K inv R') Zb)))
            (K.mul (K.mul (two' K) (two' K)) (K.mul (K.mul ι ι) (K.mul (castN K 135) Ab))))) iν))
    (hm : Le K O.toCone (K.mul (K.add K.one K.one) (K.mul (castN K m) (K.mul (K.mul h (K.add (K.mul (K.mul (two' K) (K.mul (two' K) (two' K))) (K.mul (K.mul ι ι) (K.mul (C1' K inv R') Zb)))
            (K.mul (K.mul (two' K) (two' K)) (K.mul (K.mul ι ι) (K.mul (castN K 135) Ab))))) iν))) K.one)
    (hS0 : O.nn S0)
    (hk : 1 ≤ k) (K' : Nat) :
    Le K O.toCone ((Q K ν h (fun t => Zl K kz (l.filter (lowB Rc kz)) (dF K (restr K (lowB Rc kz) (A t)) (B t)))
          (fun t => Al K kz (l.filter (lowB Rc kz)) (dF K (restr K (lowB Rc kz) (A t)) (B t)))) (n * K'))
      (K.mul (pwr K (pwr K (K.add K.one K.one) k) K') (K.add ((Q K ν h (fun t => Zl K kz (l.filter (lowB Rc kz)) (dF K (restr K (lowB Rc kz) (A t)) (B t)))
          (fun t => Al K kz (l.filter (lowB Rc kz)) (dF K (restr K (lowB Rc kz) (A t)) (B t)))) (n * 0)) (K.mul (pwr K (K.add K.one K.one) k) S0))) := by
  subst hMm
  subst hεAdef
  subst hDAdef
  subst hEAdef
  have W := fun j t (ht : t ≤ n) => WindowHyps.window_hyps K O inv mul_inv h10 l kz hd h1 Mm hM Rl t1 hRl ι h ν Zb hh hν
    hν0 hZb A (fun t r => Nl K l (TriadSparse.delta K kz) ι (kv K kz) (A t) (A t) r)
    (fun t => le_refl' K O.toCone _) (fun t => le_refl' K O.toCone _) hA choice n j Qmax EA (hQ j) hbud
    e D1 D2 D3 he hD1 hD2 hD3 H1 H2 H3 hε (hE j) t ht
  exact WindowClosed.large_closed K O inv mul_inv h10 l kz hd h1 (Mm + 1) hM Rc R' t0 hRM ι h ν Zb Aa Ab iR hh hν hZb
    hAa hAb hiRn hiR A B hA hB n (fun j t ht => (W j t (Nat.le_of_lt ht)).1) _ εB _ DB _ EB hεA hεB hDA hDB hν0
    (fun j t ht => (W j t ht).2) hEB hAa_def hAb_def hβc S0 hS0def iν hνi hiν m k hn ha hm hS0 hk K'

end Gen

end WindowCond

#print axioms WindowCond.large_cond

ns/WindowCondMem.lean

lean_all/ns/WindowCondMem.lean

/-
  ns/WindowCondMem.lean — T37 (goal G795b-j): WindowCondUpto.large_cond_upto without the small-truncation energy
  hypothesis hEB. The system B on l.filter (lowB Rc) satisfies Step only on that list; extended by 0 off the list
  (StepZeroExt) it gets its window energy bound from a bound Q_B(n·j) ≤ Qmax_B at the window starts, as A does:
    large_cond_mem   d_j ≤ c^j d_0 + c s₀ Σ_{i<j} c^i for j ≤ K′, from Qmax_A and Qmax_B
  (and a Boolean membership test for l.filter (lowB Rc)).
    mem_filter_of, pairwise_filter   the filtered list inherits membership in l and pairwise distinct modes.

  Lean 4 core plus ns/WindowCondUpto, ns/StepZeroExt, ns/WindowHypsMem.
-/
import ns.WindowCondUpto
import ns.StepZeroExt
import ns.WindowHypsMem

namespace WindowCondMem
open DetChain NSChain DyadicSmallGlobal LibRing DyadicLarge FourierGalerkin GalerkinForce ForceBound ForceSob
  ModeCount CubeCount ForceUniform NormCompare EnstStep EnstGlobal EnstGalerkin ForceTwoScale ForceBilin EnstDiff
  ForceDiff TruncError TruncBound RestrStep GronwallSource EnstGronwall WeightSplit LatticeSum TruncCauchy TruncRate

theorem mem_filter_of {I : Type} (f : I → Bool) : ∀ (l : List I) (r : I), r ∈ l.filter f → r ∈ l
  | [], _, h => nomatch h
  | x :: l, r, h => by
    cases e : f x with
    | true =>
      have h' : r ∈ x :: l.filter f := by rw [List.filter, e] at h; exact h
      cases h' with
      | head => exact List.Mem.head _
      | tail _ h2 => exact List.Mem.tail _ (mem_filter_of f l r h2)
    | false =>
      have h' : r ∈ l.filter f := by rw [List.filter, e] at h; exact h
      exact List.Mem.tail _ (mem_filter_of f l r h')

theorem pairwise_filter {I : Type} (P : I → I → Prop) (f : I → Bool) :
    ∀ l : List I, List.Pairwise P l → List.Pairwise P (l.filter f)
  | [], _ => List.Pairwise.nil
  | x :: l, hp => by
    cases hp with
    | cons hx hl =>
      cases e : f x with
      | true =>
        rw [List.filter, e]
        exact List.Pairwise.cons (fun y hy => hx y (mem_filter_of f l y hy)) (pairwise_filter P f l hl)
      | false =>
        rw [List.filter, e]
        exact pairwise_filter P f l hl

section Gen
variable {R : Type} (K : CRing R) (O : OrdR K) {I : Type}
variable (inv : R → R) (mul_inv : ∀ y, y ≠ K.zero → K.mul y (inv y) = K.one) (h10 : K.one ≠ K.zero)

include mul_inv h10 in
/-- **Truncation Cauchy chain over K′ windows from Qmax_A and Qmax_B alone.** -/
theorem large_cond_mem (l : List I) (kz : I → Mode) (hd : List.Pairwise (fun a b => kz a ≠ kz b) l)
    (h1 : ∀ p, 1 ≤ nrm (kz p)) (M Mm : Nat) (hMm : M = Mm + 1) (hM : ∀ p, p ∈ l → nrm (kz p) ≤ M + 1) (Rc R' t0 : Nat) (hRM : R' ≤ M)
    (ι h ν Zb Aa Ab iR : R) (hh : O.nn h) (hν : O.nn ν) (hZb : O.nn Zb) (hAa : O.nn Aa) (hAb : O.nn Ab) (hiRn : O.nn iR)
    (hiR : K.mul (K.mul (castN K (Rc + 1)) (castN K (Rc + 1))) iR = K.one)
    (A B : Nat → I → V3 R)
    (hA : ∀ t r, Step K h ν (wt K kz r) (A (t + 1) r) (A t r) (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (A t) (A t) r))
    (hB : ∀ t r, r ∈ l.filter (lowB Rc kz) → Step K h ν (wt K kz r) (B (t + 1) r) (B t r)
      (Nl K (l.filter (lowB Rc kz)) (TriadSparse.delta K kz) ι (kv K kz) (B t) (B t) r))
    (n K' : Nat)
    (εA εB DA DB EA EB : R) (hεA : O.nn εA) (hεB : O.nn εB) (hDA : O.nn DA) (hDB : O.nn DB) (hν0 : ν ≠ K.zero)
    (Rl t1 : Nat) (hRl : Rl ≤ M)
    (choice : Le K O.toCone (K.mul (EnstLocalGalerkin.cc K ι (EnstLocalGalerkin.C2 K inv Rl t1)) Zb) (K.mul ν ν))
    (Qmax : R)
    (hQ : ∀ j, Le K O.toCone (EnstGlobal.Q K ν h (EnstGalerkin.Zs K l kz A) (EnstGalerkin.As K l kz A) (n * j)) Qmax)
    (hbud : Le K O.toCone (K.add Qmax
      (K.mul (castN K n) (K.mul h (K.mul (EnstLocalGalerkin.cc K ι (EnstLocalGalerkin.C1 K inv Rl)) (K.mul Zb Zb)))))
      (K.mul ν Zb))
    (e D1 D2 D3 : R) (he : O.nn e) (hD1 : O.nn D1) (hD2 : O.nn D2) (hD3 : O.nn D3)
    (H1 : Le K O.toCone (K.mul (AgmonAbsorb.c3 K (castN K 16)) (AgmonAbsorb.q5 K Zb))
      (K.mul (AgmonAbsorb.c3 K (castN K 9)) (K.mul (RootYoung.four K) (K.mul (AgmonAbsorb.c3 K e) D1))))
    (H2 : Le K O.toCone (K.mul (castN K 16) (AgmonAbsorb.q5 K Zb))
      (K.mul (castN K 9) (K.mul (RootYoung.four K) (K.mul (AgmonAbsorb.c3 K e) D2))))
    (H3 : Le K O.toCone (AgmonAbsorb.c3 K Zb) (K.mul (RootYoung.four K) (K.mul e D3)))
    (hε : Le K O.toCone (K.mul (K.mul ι ι) (K.add (K.mul (AgmonCollapse.k1 K (castN K 3) (castN K 9) (castN K 108)) e)
        (K.mul (AgmonCollapse.k2 K (castN K 9) (castN K 108)) (K.add e (K.mul (castN K 2) e))))) (K.mul ν ν))
    (hεAdef : εA = (K.mul (K.mul ι ι) (K.add (K.mul (AgmonCollapse.k1 K (castN K 3) (castN K 9) (castN K 108)) e)
        (K.mul (AgmonCollapse.k2 K (castN K 9) (castN K 108)) (K.add e (K.mul (castN K 2) e)))))) (hDAdef : DA = (K.mul (K.mul ι ι)
          (K.add (K.mul (AgmonCollapse.k1 K (castN K 3) (castN K 9) (castN K 108))
              (K.add D1 (K.mul (AgmonAbsorb.c3 K (castN K 2)) (K.mul Zb Zb))))
            (K.mul (AgmonCollapse.k2 K (castN K 9) (castN K 108)) (K.add D2 (K.mul (castN K 2) D3))))))
    (hEAdef : EA = K.add (H2Linear.En K ν h (K.mul (K.mul ι ι) (K.add (K.mul (AgmonCollapse.k1 K (castN K 3) (castN K 9) (castN K 108)) e)
        (K.mul (AgmonCollapse.k2 K (castN K 9) (castN K 108)) (K.add e (K.mul (castN K 2) e)))))
        (fun s => ForceAbsorb.As K kz l (A s)) (fun s => ForceAbsorb.Bs K kz l (A s)) (n * 0)) (K.mul (castN K K') (K.mul (castN K n) (K.mul h DA))))
    (QmaxB : R)
    (hQB : ∀ j, Le K O.toCone (EnstGlobal.Q K ν h (EnstGalerkin.Zs K (l.filter (lowB Rc kz)) kz B) (EnstGalerkin.As K (l.filter (lowB Rc kz)) kz B) (n * j)) QmaxB)
    (hbudB : Le K O.toCone (K.add QmaxB
      (K.mul (castN K n) (K.mul h (K.mul (EnstLocalGalerkin.cc K ι (EnstLocalGalerkin.C1 K inv Rl)) (K.mul Zb Zb)))))
      (K.mul ν Zb))
    (memb : I → Bool) (hm : ∀ r, r ∈ (l.filter (lowB Rc kz)) → memb r = true) (hm' : ∀ r, memb r = true → r ∈ (l.filter (lowB Rc kz)))
    (hεBdef : εB = (K.mul (K.mul ι ι) (K.add (K.mul (AgmonCollapse.k1 K (castN K 3) (castN K 9) (castN K 108)) e)
        (K.mul (AgmonCollapse.k2 K (castN K 9) (castN K 108)) (K.add e (K.mul (castN K 2) e)))))) (hDBdef : DB = (K.mul (K.mul ι ι)
          (K.add (K.mul (AgmonCollapse.k1 K (castN K 3) (castN K 9) (castN K 108))
              (K.add D1 (K.mul (AgmonAbsorb.c3 K (castN K 2)) (K.mul Zb Zb))))
            (K.mul (AgmonCollapse.k2 K (castN K 9) (castN K 108)) (K.add D2 (K.mul (castN K 2) D3))))))
    (hEBdef : EB = K.add (H2Linear.En K ν h (K.mul (K.mul ι ι) (K.add (K.mul (AgmonCollapse.k1 K (castN K 3) (castN K 9) (castN K 108)) e)
        (K.mul (AgmonCollapse.k2 K (castN K 9) (castN K 108)) (K.add e (K.mul (castN K 2) e)))))
        (fun s => ForceAbsorb.As K kz (l.filter (lowB Rc kz)) (B s)) (fun s => ForceAbsorb.Bs K kz (l.filter (lowB Rc kz)) (B s)) (n * 0)) (K.mul (castN K K') (K.mul (castN K n) (K.mul h DB))))
    (hAa_def : Aa = K.mul (K.add EA (K.mul (castN K n) (K.mul h DA))) (inv ν))
    (hAb_def : Ab = K.mul (K.add EB (K.mul (castN K n) (K.mul h DB))) (inv ν))
    (hβc : Le K O.toCone (K.mul (K.mul (two' K) (K.mul (two' K) (two' K))) (K.mul (K.mul ι ι) (K.mul (C2' K inv R' t0) Zb)))
      (K.mul ν ν))
    (P S0 : R)
    (hPdef : P = pwr K (K.add ν (K.mul h
          (K.add (K.mul (K.mul (two' K) (K.mul (two' K) (two' K))) (K.mul (K.mul ι ι) (K.mul (C1' K inv R') Zb)))
            (K.mul (K.mul (two' K) (two' K)) (K.mul (K.mul ι ι) (K.mul (castN K 135) Ab)))))) n)
    (hS0def : S0 = K.mul (castN K n) (K.mul h (K.mul (two' K)
            (K.add
              (K.mul (two' K) (K.add
                (K.mul (two' K) (K.mul (K.mul ι ι) (K.mul (K.mul Aa iR) (K.mul (C1' K inv R') Zb))))
                (K.mul (two' K) (K.mul (K.mul ι ι) (K.mul Aa (K.mul (C2' K inv R' t0) Zb))))))
              (K.mul (two' K) (K.mul (K.mul ι ι) (K.mul Aa (K.mul (castN K 135) (K.mul Aa iR)))))))))
    (hP : O.nn P) (iν : R) (hνi : K.mul ν iν = K.one) (hiν : O.nn iν) :
    ∀ j : Nat, j ≤ K' → Le K O.toCone
      (Q K ν h (fun t => Zl K kz (l.filter (lowB Rc kz)) (dF K (restr K (lowB Rc kz) (A t)) (B t)))
          (fun t => Al K kz (l.filter (lowB Rc kz)) (dF K (restr K (lowB Rc kz) (A t)) (B t))) (n * j))
      (K.add (K.mul (pwr K (K.mul P (pwr K iν n)) j)
          (Q K ν h (fun t => Zl K kz (l.filter (lowB Rc kz)) (dF K (restr K (lowB Rc kz) (A t)) (B t)))
            (fun t => Al K kz (l.filter (lowB Rc kz)) (dF K (restr K (lowB Rc kz) (A t)) (B t))) (n * 0)))
        (K.mul (K.mul (K.mul P (pwr K iν n)) S0) (sumN K (pwr K (K.mul P (pwr K iν n))) j))) := by
  subst hMm
  subst hεAdef
  subst hDAdef
  subst hEAdef
  subst hDBdef
  subst hεBdef
  subst hEBdef
  have hX : O.nn (K.mul (castN K n) (K.mul h (K.mul (K.mul ι ι)
          (K.add (K.mul (AgmonCollapse.k1 K (castN K 3) (castN K 9) (castN K 108))
              (K.add D1 (K.mul (AgmonAbsorb.c3 K (castN K 2)) (K.mul Zb Zb))))
            (K.mul (AgmonCollapse.k2 K (castN K 9) (castN K 108)) (K.add D2 (K.mul (castN K 2) D3))))))) := O.nn_mul _ _ (RatModel.nn_cast K O.toCone n) (O.nn_mul _ _ hh hDA)
  have hE := fun j (hj : j < K') => le_trans' K O.toCone _ _ _
    (WindowEnergy.energy_upto K O inv mul_inv h10 l kz hd h1 Mm hM Rl t1 hRl ι h ν Zb hh hν hν0 hZb A
      (fun t r => Nl K l (TriadSparse.delta K kz) ι (kv K kz) (A t) (A t) r)
      (fun t => le_refl' K O.toCone _) (fun t => le_refl' K O.toCone _) hA choice n Qmax hQ hbud
      e D1 D2 D3 he hD1 hD2 hD3 H1 H2 H3 hε K' j (Nat.le_of_lt hj))
    (le_add' K O.toCone _ _ _ _ (le_refl' K O.toCone _)
      (ForceSob.le_mul_right' K O.toCone (RatModel.cast_mono K O.toCone j K' (Nat.le_of_lt hj)) hX))
  have W := fun j (hj : j < K') t (ht : t ≤ n) => WindowHyps.window_hyps K O inv mul_inv h10 l kz hd h1 Mm hM Rl t1
    hRl ι h ν Zb hh hν hν0 hZb A (fun t r => Nl K l (TriadSparse.delta K kz) ι (kv K kz) (A t) (A t) r)
    (fun t => le_refl' K O.toCone _) (fun t => le_refl' K O.toCone _) hA choice n j Qmax _ (hQ j) hbud
    e D1 D2 D3 he hD1 hD2 hD3 H1 H2 H3 hε (hE j hj) t ht
  have hdL : List.Pairwise (fun a b => kz a ≠ kz b) (l.filter (lowB Rc kz)) := pairwise_filter _ (lowB Rc kz) l hd
  have hML : ∀ p, p ∈ (l.filter (lowB Rc kz)) → nrm (kz p) ≤ Mm + 1 + 1 := fun p hp => hM p (mem_filter_of (lowB Rc kz) l p hp)
  have hEb := fun j (hj : j < K') => le_trans' K O.toCone _ _ _
    (StepZeroExt.energy_upto_mem K O inv mul_inv h10 (l.filter (lowB Rc kz)) kz hdL h1 Mm hML Rl t1 hRl ι h ν Zb hh hν hν0 hZb B
      (fun t r => Nl K (l.filter (lowB Rc kz)) (TriadSparse.delta K kz) ι (kv K kz) (B t) (B t) r)
      (fun t => le_refl' K O.toCone _) (fun t => le_refl' K O.toCone _) memb hm hm' hB choice n QmaxB hQB hbudB
      e D1 D2 D3 he hD1 hD2 hD3 H1 H2 H3 hε K' j (Nat.le_of_lt hj))
    (le_add' K O.toCone _ _ _ _ (le_refl' K O.toCone _)
      (ForceSob.le_mul_right' K O.toCone (RatModel.cast_mono K O.toCone j K' (Nat.le_of_lt hj)) hX))
  have WB := fun j (hj : j < K') t (ht : t ≤ n) => WindowHypsMem.window_hyps_mem K O inv mul_inv h10 (l.filter (lowB Rc kz)) kz hdL h1 Mm
    hML Rl t1 hRl ι h ν Zb hh hν hν0 hZb B (fun t r => Nl K (l.filter (lowB Rc kz)) (TriadSparse.delta K kz) ι (kv K kz) (B t) (B t) r)
    (fun t => le_refl' K O.toCone _) (fun t => le_refl' K O.toCone _) memb hm hm' hB choice n j QmaxB _ (hQB j) hbudB
    e D1 D2 D3 he hD1 hD2 hD3 H1 H2 H3 hε (hEb j hj) t ht
  exact WindowShiftUpto.large_chain_upto K O inv mul_inv h10 l kz hd h1 (Mm + 1) hM Rc R' t0 hRM ι h ν Zb Aa Ab iR
    hh hν hZb hAa hAb hiRn hiR A B hA hB n K' (fun j hj t ht => (W j hj t (Nat.le_of_lt ht)).1) _ _ _ _ _ _
    hεA hεB hDA hDB hν0 (fun j hj t ht => (W j hj t ht).2) (fun j hj t ht => (WB j hj t ht).2) hAa_def hAb_def hβc P S0 hPdef hS0def hP iν hνi hiν

end Gen

end WindowCondMem

#print axioms WindowCondMem.mem_filter_of
#print axioms WindowCondMem.pairwise_filter
#print axioms WindowCondMem.large_cond_mem

ns/WindowCondUpto.lean

lean_all/ns/WindowCondUpto.lean

/-
  ns/WindowCondUpto.lean — T37 (goal G795b-i, piece d): the truncation Cauchy chain over K′ windows from the one
  bound Q(n·j) ≤ Qmax at the window starts (Qmax + n·h·c₁Zb² ≤ ν·Zb), with the window-start H² energy bound
  E_A = En(0) + K′·n·h·D supplied by WindowEnergy.energy_upto (no separate H² hypothesis):
    large_cond_upto   d_j ≤ c^j d_0 + c s₀ Σ_{i<j} c^i for j ≤ K′.
  The small-truncation energy hypothesis hEB is kept.

  Lean 4 core plus ns/WindowShiftUpto, ns/WindowHyps, ns/WindowEnergy.
-/
import ns.WindowShiftUpto
import ns.WindowHyps
import ns.WindowEnergy

namespace WindowCondUpto
open DetChain NSChain DyadicSmallGlobal LibRing DyadicLarge FourierGalerkin GalerkinForce ForceBound ForceSob
  ModeCount CubeCount ForceUniform NormCompare EnstStep EnstGlobal EnstGalerkin ForceTwoScale ForceBilin EnstDiff
  ForceDiff TruncError TruncBound RestrStep GronwallSource EnstGronwall WeightSplit LatticeSum TruncCauchy TruncRate

section Gen
variable {R : Type} (K : CRing R) (O : OrdR K) {I : Type}
variable (inv : R → R) (mul_inv : ∀ y, y ≠ K.zero → K.mul y (inv y) = K.one) (h10 : K.one ≠ K.zero)

include mul_inv h10 in
/-- **Truncation Cauchy chain over K′ windows from Qmax alone.** -/
theorem large_cond_upto (l : List I) (kz : I → Mode) (hd : List.Pairwise (fun a b => kz a ≠ kz b) l)
    (h1 : ∀ p, 1 ≤ nrm (kz p)) (M Mm : Nat) (hMm : M = Mm + 1) (hM : ∀ p, p ∈ l → nrm (kz p) ≤ M + 1) (Rc R' t0 : Nat) (hRM : R' ≤ M)
    (ι h ν Zb Aa Ab iR : R) (hh : O.nn h) (hν : O.nn ν) (hZb : O.nn Zb) (hAa : O.nn Aa) (hAb : O.nn Ab) (hiRn : O.nn iR)
    (hiR : K.mul (K.mul (castN K (Rc + 1)) (castN K (Rc + 1))) iR = K.one)
    (A B : Nat → I → V3 R)
    (hA : ∀ t r, Step K h ν (wt K kz r) (A (t + 1) r) (A t r) (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (A t) (A t) r))
    (hB : ∀ t r, r ∈ l.filter (lowB Rc kz) → Step K h ν (wt K kz r) (B (t + 1) r) (B t r)
      (Nl K (l.filter (lowB Rc kz)) (TriadSparse.delta K kz) ι (kv K kz) (B t) (B t) r))
    (n K' : Nat)
    (εA εB DA DB EA EB : R) (hεA : O.nn εA) (hεB : O.nn εB) (hDA : O.nn DA) (hDB : O.nn DB) (hν0 : ν ≠ K.zero)
    (Rl t1 : Nat) (hRl : Rl ≤ M)
    (choice : Le K O.toCone (K.mul (EnstLocalGalerkin.cc K ι (EnstLocalGalerkin.C2 K inv Rl t1)) Zb) (K.mul ν ν))
    (Qmax : R)
    (hQ : ∀ j, Le K O.toCone (EnstGlobal.Q K ν h (EnstGalerkin.Zs K l kz A) (EnstGalerkin.As K l kz A) (n * j)) Qmax)
    (hbud : Le K O.toCone (K.add Qmax
      (K.mul (castN K n) (K.mul h (K.mul (EnstLocalGalerkin.cc K ι (EnstLocalGalerkin.C1 K inv Rl)) (K.mul Zb Zb)))))
      (K.mul ν Zb))
    (e D1 D2 D3 : R) (he : O.nn e) (hD1 : O.nn D1) (hD2 : O.nn D2) (hD3 : O.nn D3)
    (H1 : Le K O.toCone (K.mul (AgmonAbsorb.c3 K (castN K 16)) (AgmonAbsorb.q5 K Zb))
      (K.mul (AgmonAbsorb.c3 K (castN K 9)) (K.mul (RootYoung.four K) (K.mul (AgmonAbsorb.c3 K e) D1))))
    (H2 : Le K O.toCone (K.mul (castN K 16) (AgmonAbsorb.q5 K Zb))
      (K.mul (castN K 9) (K.mul (RootYoung.four K) (K.mul (AgmonAbsorb.c3 K e) D2))))
    (H3 : Le K O.toCone (AgmonAbsorb.c3 K Zb) (K.mul (RootYoung.four K) (K.mul e D3)))
    (hε : Le K O.toCone (K.mul (K.mul ι ι) (K.add (K.mul (AgmonCollapse.k1 K (castN K 3) (castN K 9) (castN K 108)) e)
        (K.mul (AgmonCollapse.k2 K (castN K 9) (castN K 108)) (K.add e (K.mul (castN K 2) e))))) (K.mul ν ν))
    (hεAdef : εA = (K.mul (K.mul ι ι) (K.add (K.mul (AgmonCollapse.k1 K (castN K 3) (castN K 9) (castN K 108)) e)
        (K.mul (AgmonCollapse.k2 K (castN K 9) (castN K 108)) (K.add e (K.mul (castN K 2) e)))))) (hDAdef : DA = (K.mul (K.mul ι ι)
          (K.add (K.mul (AgmonCollapse.k1 K (castN K 3) (castN K 9) (castN K 108))
              (K.add D1 (K.mul (AgmonAbsorb.c3 K (castN K 2)) (K.mul Zb Zb))))
            (K.mul (AgmonCollapse.k2 K (castN K 9) (castN K 108)) (K.add D2 (K.mul (castN K 2) D3))))))
    (hEAdef : EA = K.add (H2Linear.En K ν h (K.mul (K.mul ι ι) (K.add (K.mul (AgmonCollapse.k1 K (castN K 3) (castN K 9) (castN K 108)) e)
        (K.mul (AgmonCollapse.k2 K (castN K 9) (castN K 108)) (K.add e (K.mul (castN K 2) e)))))
        (fun s => ForceAbsorb.As K kz l (A s)) (fun s => ForceAbsorb.Bs K kz l (A s)) (n * 0)) (K.mul (castN K K') (K.mul (castN K n) (K.mul h DA))))
    (hEB : ∀ k, k < K' → ∀ t, t ≤ n → Le K O.toCone (K.add (K.mul ν (Al K kz (l.filter (lowB Rc kz)) (B (n * k + t))))
      (K.mul (K.mul h εB) (lsum K (fun p => K.mul (K.mul (K.mul (wt K kz p) (wt K kz p)) (wt K kz p))
        (dot3 K (B (n * k + t) p) (B (n * k + t) p))) (l.filter (lowB Rc kz)))))
      (K.add EB (K.mul (castN K t) (K.mul h DB))))
    (hAa_def : Aa = K.mul (K.add EA (K.mul (castN K n) (K.mul h DA))) (inv ν))
    (hAb_def : Ab = K.mul (K.add EB (K.mul (castN K n) (K.mul h DB))) (inv ν))
    (hβc : Le K O.toCone (K.mul (K.mul (two' K) (K.mul (two' K) (two' K))) (K.mul (K.mul ι ι) (K.mul (C2' K inv R' t0) Zb)))
      (K.mul ν ν))
    (P S0 : R)
    (hPdef : P = pwr K (K.add ν (K.mul h
          (K.add (K.mul (K.mul (two' K) (K.mul (two' K) (two' K))) (K.mul (K.mul ι ι) (K.mul (C1' K inv R') Zb)))
            (K.mul (K.mul (two' K) (two' K)) (K.mul (K.mul ι ι) (K.mul (castN K 135) Ab)))))) n)
    (hS0def : S0 = K.mul (castN K n) (K.mul h (K.mul (two' K)
            (K.add
              (K.mul (two' K) (K.add
                (K.mul (two' K) (K.mul (K.mul ι ι) (K.mul (K.mul Aa iR) (K.mul (C1' K inv R') Zb))))
                (K.mul (two' K) (K.mul (K.mul ι ι) (K.mul Aa (K.mul (C2' K inv R' t0) Zb))))))
              (K.mul (two' K) (K.mul (K.mul ι ι) (K.mul Aa (K.mul (castN K 135) (K.mul Aa iR)))))))))
    (hP : O.nn P) (iν : R) (hνi : K.mul ν iν = K.one) (hiν : O.nn iν) :
    ∀ j : Nat, j ≤ K' → Le K O.toCone
      (Q K ν h (fun t => Zl K kz (l.filter (lowB Rc kz)) (dF K (restr K (lowB Rc kz) (A t)) (B t)))
          (fun t => Al K kz (l.filter (lowB Rc kz)) (dF K (restr K (lowB Rc kz) (A t)) (B t))) (n * j))
      (K.add (K.mul (pwr K (K.mul P (pwr K iν n)) j)
          (Q K ν h (fun t => Zl K kz (l.filter (lowB Rc kz)) (dF K (restr K (lowB Rc kz) (A t)) (B t)))
            (fun t => Al K kz (l.filter (lowB Rc kz)) (dF K (restr K (lowB Rc kz) (A t)) (B t))) (n * 0)))
        (K.mul (K.mul (K.mul P (pwr K iν n)) S0) (sumN K (pwr K (K.mul P (pwr K iν n))) j))) := by
  subst hMm
  subst hεAdef
  subst hDAdef
  subst hEAdef
  have hX : O.nn (K.mul (castN K n) (K.mul h (K.mul (K.mul ι ι)
          (K.add (K.mul (AgmonCollapse.k1 K (castN K 3) (castN K 9) (castN K 108))
              (K.add D1 (K.mul (AgmonAbsorb.c3 K (castN K 2)) (K.mul Zb Zb))))
            (K.mul (AgmonCollapse.k2 K (castN K 9) (castN K 108)) (K.add D2 (K.mul (castN K 2) D3))))))) := O.nn_mul _ _ (RatModel.nn_cast K O.toCone n) (O.nn_mul _ _ hh hDA)
  have hE := fun j (hj : j < K') => le_trans' K O.toCone _ _ _
    (WindowEnergy.energy_upto K O inv mul_inv h10 l kz hd h1 Mm hM Rl t1 hRl ι h ν Zb hh hν hν0 hZb A
      (fun t r => Nl K l (TriadSparse.delta K kz) ι (kv K kz) (A t) (A t) r)
      (fun t => le_refl' K O.toCone _) (fun t => le_refl' K O.toCone _) hA choice n Qmax hQ hbud
      e D1 D2 D3 he hD1 hD2 hD3 H1 H2 H3 hε K' j (Nat.le_of_lt hj))
    (le_add' K O.toCone _ _ _ _ (le_refl' K O.toCone _)
      (ForceSob.le_mul_right' K O.toCone (RatModel.cast_mono K O.toCone j K' (Nat.le_of_lt hj)) hX))
  have W := fun j (hj : j < K') t (ht : t ≤ n) => WindowHyps.window_hyps K O inv mul_inv h10 l kz hd h1 Mm hM Rl t1
    hRl ι h ν Zb hh hν hν0 hZb A (fun t r => Nl K l (TriadSparse.delta K kz) ι (kv K kz) (A t) (A t) r)
    (fun t => le_refl' K O.toCone _) (fun t => le_refl' K O.toCone _) hA choice n j Qmax _ (hQ j) hbud
    e D1 D2 D3 he hD1 hD2 hD3 H1 H2 H3 hε (hE j hj) t ht
  exact WindowShiftUpto.large_chain_upto K O inv mul_inv h10 l kz hd h1 (Mm + 1) hM Rc R' t0 hRM ι h ν Zb Aa Ab iR
    hh hν hZb hAa hAb hiRn hiR A B hA hB n K' (fun j hj t ht => (W j hj t (Nat.le_of_lt ht)).1) _ εB _ DB _ EB
    hεA hεB hDA hDB hν0 (fun j hj t ht => (W j hj t ht).2) hEB hAa_def hAb_def hβc P S0 hPdef hS0def hP iν hνi hiν

end Gen

end WindowCondUpto

#print axioms WindowCondUpto.large_cond_upto

ns/WindowEnergy.lean

lean_all/ns/WindowEnergy.lean

/-
  ns/WindowEnergy.lean — T37 (goal G795b-i, piece c): the H² energy at the window starts grows at most linearly,
  from the one bound Q(n·k) ≤ Qmax at every window start (with Qmax + n·h·c₁Zb² ≤ ν·Zb):
    window_energy   En(n·(k + 1)) ≤ En(n·k) + n·h·D,
    energy_upto     En(n·j) ≤ En(0) + j·n·h·D   (j ≤ K′),
  so on K′ windows the energy hypothesis of WindowHyps holds with Emax = En(0) + K′·n·h·D: no separate H² bound
  at the window starts is needed.

  Lean 4 core plus ns/H2LargeLe, ns/WindowChainUpto.
-/
import ns.H2LargeLe
import ns.WindowChainUpto

namespace WindowEnergy
open DetChain NSChain DyadicSmallGlobal LibRing DyadicLarge FourierGalerkin GalerkinForce ForceBound ForceSob
  ModeCount CubeCount ForceUniform NormCompare EnstGalerkin ForceTwoScale EnstStep

section Gen
variable {R : Type} (K : CRing R) (O : OrdR K) {I : Type}
variable (inv : R → R) (mul_inv : ∀ y, y ≠ K.zero → K.mul y (inv y) = K.one) (h10 : K.one ≠ K.zero)

include mul_inv h10 in
/-- **one window: En(n(k+1)) ≤ En(nk) + n h D** -/
theorem window_energy (l : List I) (kz : I → Mode) (hd : List.Pairwise (fun a b => kz a ≠ kz b) l)
    (h1 : ∀ p, 1 ≤ nrm (kz p)) (Mm : Nat) (hM : ∀ p, p ∈ l → nrm (kz p) ≤ Mm + 2) (Rc t0 : Nat) (hRM : Rc ≤ Mm + 1)
    (ι h ν Zb : R) (hh : O.nn h) (hν : O.nn ν) (hν0 : ν ≠ K.zero) (hZb : O.nn Zb)
    (a N : Nat → I → V3 R)
    (hN0 : ∀ t, Le K O.toCone (lsum K (fun r => dot3 K (N t r) (N t r)) l)
      (lsum K (fun r => dot3 K (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (a t) (a t) r)
        (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (a t) (a t) r)) l))
    (hN : ∀ t, Le K O.toCone (lsum K (fun r => K.mul (wt K kz r) (dot3 K (N t r) (N t r))) l)
      (lsum K (fun r => K.mul (wt K kz r) (dot3 K (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (a t) (a t) r)
        (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (a t) (a t) r))) l))
    (hs : ∀ t r, Step K h ν (wt K kz r) (a (t + 1) r) (a t r) (N t r))
    (choice : Le K O.toCone (K.mul (EnstLocalGalerkin.cc K ι (EnstLocalGalerkin.C2 K inv Rc t0)) Zb) (K.mul ν ν))
    (n : Nat) (Qmax : R)
    (hQ : ∀ k, Le K O.toCone (EnstGlobal.Q K ν h (EnstGalerkin.Zs K l kz a) (EnstGalerkin.As K l kz a) (n * k)) Qmax)
    (hbud : Le K O.toCone (K.add Qmax
      (K.mul (castN K n) (K.mul h (K.mul (EnstLocalGalerkin.cc K ι (EnstLocalGalerkin.C1 K inv Rc)) (K.mul Zb Zb)))))
      (K.mul ν Zb))
    (e D1 D2 D3 : R) (he : O.nn e) (hD1 : O.nn D1) (hD2 : O.nn D2) (hD3 : O.nn D3)
    (H1 : Le K O.toCone (K.mul (AgmonAbsorb.c3 K (castN K 16)) (AgmonAbsorb.q5 K Zb))
      (K.mul (AgmonAbsorb.c3 K (castN K 9)) (K.mul (RootYoung.four K) (K.mul (AgmonAbsorb.c3 K e) D1))))
    (H2 : Le K O.toCone (K.mul (castN K 16) (AgmonAbsorb.q5 K Zb))
      (K.mul (castN K 9) (K.mul (RootYoung.four K) (K.mul (AgmonAbsorb.c3 K e) D2))))
    (H3 : Le K O.toCone (AgmonAbsorb.c3 K Zb) (K.mul (RootYoung.four K) (K.mul e D3)))
    (hε : Le K O.toCone (K.mul (K.mul ι ι) (K.add (K.mul (AgmonCollapse.k1 K (castN K 3) (castN K 9) (castN K 108)) e)
      (K.mul (AgmonCollapse.k2 K (castN K 9) (castN K 108)) (K.add e (K.mul (castN K 2) e))))) (K.mul ν ν))
    (k : Nat) :
    Le K O.toCone (H2Linear.En K ν h (K.mul (K.mul ι ι) (K.add (K.mul (AgmonCollapse.k1 K (castN K 3) (castN K 9) (castN K 108)) e)
        (K.mul (AgmonCollapse.k2 K (castN K 9) (castN K 108)) (K.add e (K.mul (castN K 2) e)))))
        (fun s => ForceAbsorb.As K kz l (a s)) (fun s => ForceAbsorb.Bs K kz l (a s)) (n * (k + 1)))
      (K.add (H2Linear.En K ν h (K.mul (K.mul ι ι) (K.add (K.mul (AgmonCollapse.k1 K (castN K 3) (castN K 9) (castN K 108)) e)
        (K.mul (AgmonCollapse.k2 K (castN K 9) (castN K 108)) (K.add e (K.mul (castN K 2) e)))))
        (fun s => ForceAbsorb.As K kz l (a s)) (fun s => ForceAbsorb.Bs K kz l (a s)) (n * k)) (K.mul (castN K n) (K.mul h (K.mul (K.mul ι ι)
          (K.add (K.mul (AgmonCollapse.k1 K (castN K 3) (castN K 9) (castN K 108))
              (K.add D1 (K.mul (AgmonAbsorb.c3 K (castN K 2)) (K.mul Zb Zb))))
            (K.mul (AgmonCollapse.k2 K (castN K 9) (castN K 108)) (K.add D2 (K.mul (castN K 2) D3)))))))) :=
  (H2LargeLe.h2_large_le K O inv mul_inv h10 l kz hd h1 Mm hM Rc t0 hRM ι h ν Zb hh hν hν0 hZb
    (fun s => a (n * k + s)) (fun s => N (n * k + s)) (fun s => hN0 (n * k + s)) (fun s => hN (n * k + s))
    (fun s r => hs (n * k + s) r) choice n
    (le_trans' K O.toCone _ _ _ (le_add' K O.toCone _ _ _ _ (hQ k) (le_refl' K O.toCone _)) hbud)
    e D1 D2 D3 he hD1 hD2 hD3 H1 H2 H3 hε n (Nat.le_refl n)).2

include mul_inv h10 in
/-- **K′ windows: En(nj) ≤ En(0) + j n h D** -/
theorem energy_upto (l : List I) (kz : I → Mode) (hd : List.Pairwise (fun a b => kz a ≠ kz b) l)
    (h1 : ∀ p, 1 ≤ nrm (kz p)) (Mm : Nat) (hM : ∀ p, p ∈ l → nrm (kz p) ≤ Mm + 2) (Rc t0 : Nat) (hRM : Rc ≤ Mm + 1)
    (ι h ν Zb : R) (hh : O.nn h) (hν : O.nn ν) (hν0 : ν ≠ K.zero) (hZb : O.nn Zb)
    (a N : Nat → I → V3 R)
    (hN0 : ∀ t, Le K O.toCone (lsum K (fun r => dot3 K (N t r) (N t r)) l)
      (lsum K (fun r => dot3 K (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (a t) (a t) r)
        (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (a t) (a t) r)) l))
    (hN : ∀ t, Le K O.toCone (lsum K (fun r => K.mul (wt K kz r) (dot3 K (N t r) (N t r))) l)
      (lsum K (fun r => K.mul (wt K kz r) (dot3 K (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (a t) (a t) r)
        (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (a t) (a t) r))) l))
    (hs : ∀ t r, Step K h ν (wt K kz r) (a (t + 1) r) (a t r) (N t r))
    (choice : Le K O.toCone (K.mul (EnstLocalGalerkin.cc K ι (EnstLocalGalerkin.C2 K inv Rc t0)) Zb) (K.mul ν ν))
    (n : Nat) (Qmax : R)
    (hQ : ∀ k, Le K O.toCone (EnstGlobal.Q K ν h (EnstGalerkin.Zs K l kz a) (EnstGalerkin.As K l kz a) (n * k)) Qmax)
    (hbud : Le K O.toCone (K.add Qmax
      (K.mul (castN K n) (K.mul h (K.mul (EnstLocalGalerkin.cc K ι (EnstLocalGalerkin.C1 K inv Rc)) (K.mul Zb Zb)))))
      (K.mul ν Zb))
    (e D1 D2 D3 : R) (he : O.nn e) (hD1 : O.nn D1) (hD2 : O.nn D2) (hD3 : O.nn D3)
    (H1 : Le K O.toCone (K.mul (AgmonAbsorb.c3 K (castN K 16)) (AgmonAbsorb.q5 K Zb))
      (K.mul (AgmonAbsorb.c3 K (castN K 9)) (K.mul (RootYoung.four K) (K.mul (AgmonAbsorb.c3 K e) D1))))
    (H2 : Le K O.toCone (K.mul (castN K 16) (AgmonAbsorb.q5 K Zb))
      (K.mul (castN K 9) (K.mul (RootYoung.four K) (K.mul (AgmonAbsorb.c3 K e) D2))))
    (H3 : Le K O.toCone (AgmonAbsorb.c3 K Zb) (K.mul (RootYoung.four K) (K.mul e D3)))
    (hε : Le K O.toCone (K.mul (K.mul ι ι) (K.add (K.mul (AgmonCollapse.k1 K (castN K 3) (castN K 9) (castN K 108)) e)
      (K.mul (AgmonCollapse.k2 K (castN K 9) (castN K 108)) (K.add e (K.mul (castN K 2) e))))) (K.mul ν ν))
    (K' : Nat) :
    ∀ j, j ≤ K' → Le K O.toCone (H2Linear.En K ν h (K.mul (K.mul ι ι) (K.add (K.mul (AgmonCollapse.k1 K (castN K 3) (castN K 9) (castN K 108)) e)
        (K.mul (AgmonCollapse.k2 K (castN K 9) (castN K 108)) (K.add e (K.mul (castN K 2) e)))))
        (fun s => ForceAbsorb.As K kz l (a s)) (fun s => ForceAbsorb.Bs K kz l (a s)) (n * j))
      (K.add (H2Linear.En K ν h (K.mul (K.mul ι ι) (K.add (K.mul (AgmonCollapse.k1 K (castN K 3) (castN K 9) (castN K 108)) e)
        (K.mul (AgmonCollapse.k2 K (castN K 9) (castN K 108)) (K.add e (K.mul (castN K 2) e)))))
        (fun s => ForceAbsorb.As K kz l (a s)) (fun s => ForceAbsorb.Bs K kz l (a s)) (n * 0)) (K.mul (castN K j) (K.mul (castN K n) (K.mul h (K.mul (K.mul ι ι)
          (K.add (K.mul (AgmonCollapse.k1 K (castN K 3) (castN K 9) (castN K 108))
              (K.add D1 (K.mul (AgmonAbsorb.c3 K (castN K 2)) (K.mul Zb Zb))))
            (K.mul (AgmonCollapse.k2 K (castN K 9) (castN K 108)) (K.add D2 (K.mul (castN K 2) D3))))))))) :=
  WindowChainUpto.lin_upto K O (fun j => (H2Linear.En K ν h (K.mul (K.mul ι ι) (K.add (K.mul (AgmonCollapse.k1 K (castN K 3) (castN K 9) (castN K 108)) e)
        (K.mul (AgmonCollapse.k2 K (castN K 9) (castN K 108)) (K.add e (K.mul (castN K 2) e)))))
        (fun s => ForceAbsorb.As K kz l (a s)) (fun s => ForceAbsorb.Bs K kz l (a s)) (n * j))) _ K'
    (fun k _ => window_energy K O inv mul_inv h10 l kz hd h1 Mm hM Rc t0 hRM ι h ν Zb hh hν hν0 hZb a N hN0 hN hs
      choice n Qmax hQ hbud e D1 D2 D3 he hD1 hD2 hD3 H1 H2 H3 hε k)

end Gen

end WindowEnergy

#print axioms WindowEnergy.window_energy
#print axioms WindowEnergy.energy_upto

ns/WindowFactor.lean

lean_all/ns/WindowFactor.lean

/-
  ns/WindowFactor.lean — T37 (goal G795b, piece c): the per-window factor of WindowShift.large_chain is bounded by a
  power of two depending only on the window length in time, over an ordered field with inverses. With n = m·k steps,
  ν·ν⁻¹ = 1, hα·ν⁻¹ ≥ 0 and 2m·hαν⁻¹ ≤ 1 (GronwallH):
    window_factor   (ν + hα)^(m·k)·(ν⁻¹)^(m·k) ≤ 2^k,
  so c = P (ν⁻¹)^n in large_chain is at most 2^k with k ≈ 2(n·h)α/ν, uniformly as h → 0.

  Lean 4 core plus ns/GronwallH, ns/WindowShift.
-/
import ns.GronwallH
import ns.WindowShift

namespace WindowFactor
open DetChain NSChain DyadicSmallGlobal LibRing DyadicLarge EnstGronwall

section Gen
variable {R : Type} (K : CRing R) (O : OrdR K)

/-- **(ν + hα)^(m·k)·(ν⁻¹)^(m·k) ≤ 2^k** -/
theorem window_factor (ν hα iν : R) (hν : O.nn ν) (hνi : K.mul ν iν = K.one) (hiν : O.nn iν)
    (ha : O.nn (K.mul hα iν)) (m k : Nat)
    (hm : Le K O.toCone (K.mul (K.add K.one K.one) (K.mul (castN K m) (K.mul hα iν))) K.one) :
    Le K O.toCone (K.mul (pwr K (K.add ν hα) (m * k)) (pwr K iν (m * k))) (pwr K (K.add K.one K.one) k) := by
  have g := GronwallH.gronwall_h K O ν hα iν hν hνi ha m k hm
  have s := le_mul_nn K O.toCone (pwr K iν (m * k)) _ _ (pwr_nn K O iν hiν (m * k)) g
  have e1 : K.mul (pwr K iν (m * k)) (pwr K (K.add ν hα) (m * k))
      = K.mul (pwr K (K.add ν hα) (m * k)) (pwr K iν (m * k)) := by lib_ring
  have e2 : K.mul (pwr K iν (m * k)) (K.mul (pwr K ν (m * k)) (pwr K (K.add K.one K.one) k))
      = pwr K (K.add K.one K.one) k := by
    calc K.mul (pwr K iν (m * k)) (K.mul (pwr K ν (m * k)) (pwr K (K.add K.one K.one) k))
        = K.mul (K.mul (pwr K ν (m * k)) (pwr K iν (m * k))) (pwr K (K.add K.one K.one) k) := by lib_ring
      _ = K.mul K.one (pwr K (K.add K.one K.one) k) := by rw [WindowShift.pwr_inv K ν iν hνi (m * k)]
      _ = pwr K (K.add K.one K.one) k := by lib_ring
  rw [e1, e2] at s
  exact s

end Gen

end WindowFactor

#print axioms WindowFactor.window_factor

ns/WindowGeom.lean

lean_all/ns/WindowGeom.lean

/-
  ns/WindowGeom.lean — T37 (goal G795b, piece f): the geometric sum of the window chain in closed form, over an
  ordered ring. For c ≥ 2:
    geom_le       Σ_{j<K} c^j ≤ c^K,
    chain_closed  c^K d₀ + c s₀ Σ_{j<K} c^j ≤ c^K (d₀ + c s₀)   (s₀ ≥ 0),
    two_pow_ge    2^(k+1) ≥ 2,
  so the large-data window chain (WindowLarge.large_explicit, factor 2^(k+1)) gives d_K ≤ 2^((k+1)K) (d₀ + 2^(k+1) S₀).

  Lean 4 core plus ns/WindowBound.
-/
import ns.WindowBound

namespace WindowGeom
open DetChain NSChain DyadicSmallGlobal LibRing DyadicLarge EnstGronwall

section Gen
variable {R : Type} (K : CRing R) (O : OrdR K)

/-- **Σ_{j<K} c^j ≤ c^K for c ≥ 2** -/
theorem geom_le (c : R) (hc : O.nn c) (hc2 : Le K O.toCone (K.add K.one K.one) c) :
    ∀ n : Nat, Le K O.toCone (sumN K (pwr K c) n) (pwr K c n)
  | 0 => ⟨K.one, RatModel.nn_one' K O.toCone, by show K.add K.zero K.one = K.one; lib_ring⟩
  | n + 1 => by
    have s := le_add' K O.toCone _ _ _ _ (geom_le c hc hc2 n) (le_refl' K O.toCone (pwr K c n))
    have m := ForceSob.le_mul_right' K O.toCone hc2 (pwr_nn K O c hc n)
    refine le_trans' K O.toCone _ _ _ s ?_
    have e : K.add (pwr K c n) (pwr K c n) = K.mul (K.add K.one K.one) (pwr K c n) := by lib_ring
    rw [e]; exact m

/-- **c^K d₀ + c s₀ Σ_{j<K} c^j ≤ c^K (d₀ + c s₀)** -/
theorem chain_closed (c d0 s0 : R) (hc : O.nn c) (hc2 : Le K O.toCone (K.add K.one K.one) c) (hs0 : O.nn s0)
    (n : Nat) :
    Le K O.toCone (K.add (K.mul (pwr K c n) d0) (K.mul (K.mul c s0) (sumN K (pwr K c) n)))
      (K.mul (pwr K c n) (K.add d0 (K.mul c s0))) := by
  have t := le_add' K O.toCone _ _ _ _ (le_refl' K O.toCone (K.mul (pwr K c n) d0))
    (le_mul_nn K O.toCone (K.mul c s0) _ _ (O.nn_mul _ _ hc hs0) (geom_le K O c hc hc2 n))
  refine le_trans' K O.toCone _ _ _ t ⟨K.zero, O.nn_zero, ?_⟩
  lib_ring

theorem one_le_two_pow : ∀ k : Nat, Le K O.toCone K.one (pwr K (K.add K.one K.one) k)
  | 0 => le_refl' K O.toCone _
  | k + 1 => by
    have ih := one_le_two_pow k
    have n1 : O.nn (pwr K (K.add K.one K.one) k) :=
      pwr_nn K O _ (O.nn_add _ _ (RatModel.nn_one' K O.toCone) (RatModel.nn_one' K O.toCone)) k
    refine le_trans' K O.toCone _ _ _ ih ⟨pwr K (K.add K.one K.one) k, n1, ?_⟩
    show K.add (pwr K (K.add K.one K.one) k) (pwr K (K.add K.one K.one) k)
      = K.mul (K.add K.one K.one) (pwr K (K.add K.one K.one) k)
    lib_ring

/-- 2^(k+1) ≥ 2 -/
theorem two_pow_ge (k : Nat) : Le K O.toCone (K.add K.one K.one) (pwr K (K.add K.one K.one) (k + 1)) := by
  have m := le_mul_nn K O.toCone (K.add K.one K.one) _ _
    (O.nn_add _ _ (RatModel.nn_one' K O.toCone) (RatModel.nn_one' K O.toCone)) (one_le_two_pow K O k)
  have e : K.mul (K.add K.one K.one) K.one = K.add K.one K.one := by lib_ring
  rw [e] at m; exact m

end Gen

end WindowGeom

#print axioms WindowGeom.geom_le
#print axioms WindowGeom.chain_closed
#print axioms WindowGeom.one_le_two_pow
#print axioms WindowGeom.two_pow_ge

ns/WindowHyps.lean

lean_all/ns/WindowHyps.lean

/-
  ns/WindowHyps.lean — T37 (goal G795b-h, piece c): the per-window hypotheses of WindowShift.large_chain from two
  bounds at the window starts. If Q(n·k) ≤ Qmax with Qmax + n·h·c₁Zb² ≤ ν·Zb, and the H² energy at the window start
  is at most Emax, then on the window [n·k, n·k + n]
    window_hyps   Z_{n·k+t} ≤ Zb   and   νA_{n·k+t} + hεB_{n·k+t} ≤ Emax + t·h·D   (t ≤ n),
  which are hZA and hEA of large_chain with E_A = Emax (A = Al, B = Σ w³|u|² definitionally).

  Lean 4 core plus ns/H2LargeLe.
-/
import ns.H2LargeLe

namespace WindowHyps
open DetChain NSChain DyadicSmallGlobal LibRing DyadicLarge FourierGalerkin GalerkinForce ForceBound ForceSob
  ModeCount CubeCount ForceUniform NormCompare EnstGalerkin ForceTwoScale EnstStep

section Gen
variable {R : Type} (K : CRing R) (O : OrdR K) {I : Type}
variable (inv : R → R) (mul_inv : ∀ y, y ≠ K.zero → K.mul y (inv y) = K.one) (h10 : K.one ≠ K.zero)

include mul_inv h10 in
/-- **the window hypotheses from Qmax and Emax at the window start** -/
theorem window_hyps (l : List I) (kz : I → Mode) (hd : List.Pairwise (fun a b => kz a ≠ kz b) l)
    (h1 : ∀ p, 1 ≤ nrm (kz p)) (Mm : Nat) (hM : ∀ p, p ∈ l → nrm (kz p) ≤ Mm + 2) (Rc t0 : Nat) (hRM : Rc ≤ Mm + 1)
    (ι h ν Zb : R) (hh : O.nn h) (hν : O.nn ν) (hν0 : ν ≠ K.zero) (hZb : O.nn Zb)
    (a N : Nat → I → V3 R)
    (hN0 : ∀ t, Le K O.toCone (lsum K (fun r => dot3 K (N t r) (N t r)) l)
      (lsum K (fun r => dot3 K (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (a t) (a t) r)
        (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (a t) (a t) r)) l))
    (hN : ∀ t, Le K O.toCone (lsum K (fun r => K.mul (wt K kz r) (dot3 K (N t r) (N t r))) l)
      (lsum K (fun r => K.mul (wt K kz r) (dot3 K (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (a t) (a t) r)
        (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (a t) (a t) r))) l))
    (hs : ∀ t r, Step K h ν (wt K kz r) (a (t + 1) r) (a t r) (N t r))
    (choice : Le K O.toCone (K.mul (EnstLocalGalerkin.cc K ι (EnstLocalGalerkin.C2 K inv Rc t0)) Zb) (K.mul ν ν))
    (n k : Nat) (Qmax Emax : R)
    (hQ : Le K O.toCone (EnstGlobal.Q K ν h (EnstGalerkin.Zs K l kz a) (EnstGalerkin.As K l kz a) (n * k)) Qmax)
    (hbud : Le K O.toCone (K.add Qmax
      (K.mul (castN K n) (K.mul h (K.mul (EnstLocalGalerkin.cc K ι (EnstLocalGalerkin.C1 K inv Rc)) (K.mul Zb Zb)))))
      (K.mul ν Zb))
    (e D1 D2 D3 : R) (he : O.nn e) (hD1 : O.nn D1) (hD2 : O.nn D2) (hD3 : O.nn D3)
    (H1 : Le K O.toCone (K.mul (AgmonAbsorb.c3 K (castN K 16)) (AgmonAbsorb.q5 K Zb))
      (K.mul (AgmonAbsorb.c3 K (castN K 9)) (K.mul (RootYoung.four K) (K.mul (AgmonAbsorb.c3 K e) D1))))
    (H2 : Le K O.toCone (K.mul (castN K 16) (AgmonAbsorb.q5 K Zb))
      (K.mul (castN K 9) (K.mul (RootYoung.four K) (K.mul (AgmonAbsorb.c3 K e) D2))))
    (H3 : Le K O.toCone (AgmonAbsorb.c3 K Zb) (K.mul (RootYoung.four K) (K.mul e D3)))
    (hε : Le K O.toCone (K.mul (K.mul ι ι) (K.add (K.mul (AgmonCollapse.k1 K (castN K 3) (castN K 9) (castN K 108)) e)
      (K.mul (AgmonCollapse.k2 K (castN K 9) (castN K 108)) (K.add e (K.mul (castN K 2) e))))) (K.mul ν ν))
    (hE : Le K O.toCone (H2Linear.En K ν h (K.mul (K.mul ι ι) (K.add (K.mul (AgmonCollapse.k1 K (castN K 3) (castN K 9) (castN K 108)) e)
        (K.mul (AgmonCollapse.k2 K (castN K 9) (castN K 108)) (K.add e (K.mul (castN K 2) e)))))
        (fun s => ForceAbsorb.As K kz l (a s)) (fun s => ForceAbsorb.Bs K kz l (a s)) (n * k)) Emax)
    (t : Nat) (ht : t ≤ n) :
    Le K O.toCone (EnstGalerkin.Zs K l kz a (n * k + t)) Zb ∧
    Le K O.toCone (H2Linear.En K ν h (K.mul (K.mul ι ι) (K.add (K.mul (AgmonCollapse.k1 K (castN K 3) (castN K 9) (castN K 108)) e)
        (K.mul (AgmonCollapse.k2 K (castN K 9) (castN K 108)) (K.add e (K.mul (castN K 2) e)))))
        (fun s => ForceAbsorb.As K kz l (a s)) (fun s => ForceAbsorb.Bs K kz l (a s)) (n * k + t))
      (K.add Emax (K.mul (castN K t) (K.mul h (K.mul (K.mul ι ι)
          (K.add (K.mul (AgmonCollapse.k1 K (castN K 3) (castN K 9) (castN K 108))
              (K.add D1 (K.mul (AgmonAbsorb.c3 K (castN K 2)) (K.mul Zb Zb))))
            (K.mul (AgmonCollapse.k2 K (castN K 9) (castN K 108)) (K.add D2 (K.mul (castN K 2) D3)))))))) := by
  have w := H2LargeLe.h2_large_le K O inv mul_inv h10 l kz hd h1 Mm hM Rc t0 hRM ι h ν Zb hh hν hν0 hZb
    (fun s => a (n * k + s)) (fun s => N (n * k + s)) (fun s => hN0 (n * k + s)) (fun s => hN (n * k + s))
    (fun s r => hs (n * k + s) r) choice n
    (le_trans' K O.toCone _ _ _ (le_add' K O.toCone _ _ _ _ hQ (le_refl' K O.toCone _)) hbud)
    e D1 D2 D3 he hD1 hD2 hD3 H1 H2 H3 hε t ht
  exact ⟨w.1, le_trans' K O.toCone _ _ _ w.2 (le_add' K O.toCone _ _ _ _ hE (le_refl' K O.toCone _))⟩

end Gen

end WindowHyps

#print axioms WindowHyps.window_hyps

ns/WindowHypsMem.lean

lean_all/ns/WindowHypsMem.lean

/-
  ns/WindowHypsMem.lean — T37 (goal G795b-j): WindowHyps.window_hyps with the Step hypothesis only on the modes of
  the list (StepZeroExt.h2_large_le_mem in place of H2LargeLe.h2_large_le), for the small-truncation system B on
  l.filter (lowB Rc).
    window_hyps_mem   Z_{n·k+t} ≤ Zb and νA_{n·k+t} + hεB_{n·k+t} ≤ Emax + t·h·D (t ≤ n)

  Lean 4 core plus ns/StepZeroExt, ns/WindowHyps.
-/
import ns.StepZeroExt
import ns.WindowHyps

namespace WindowHypsMem
open DetChain NSChain DyadicSmallGlobal LibRing DyadicLarge FourierGalerkin GalerkinForce ForceBound ForceSob
  ModeCount CubeCount ForceUniform NormCompare EnstGalerkin ForceTwoScale EnstStep

section Gen
variable {R : Type} (K : CRing R) (O : OrdR K) {I : Type}
variable (inv : R → R) (mul_inv : ∀ y, y ≠ K.zero → K.mul y (inv y) = K.one) (h10 : K.one ≠ K.zero)

include mul_inv h10 in
/-- **the window hypotheses from Qmax and Emax at the window start** -/
theorem window_hyps_mem (l : List I) (kz : I → Mode) (hd : List.Pairwise (fun a b => kz a ≠ kz b) l)
    (h1 : ∀ p, 1 ≤ nrm (kz p)) (Mm : Nat) (hM : ∀ p, p ∈ l → nrm (kz p) ≤ Mm + 2) (Rc t0 : Nat) (hRM : Rc ≤ Mm + 1)
    (ι h ν Zb : R) (hh : O.nn h) (hν : O.nn ν) (hν0 : ν ≠ K.zero) (hZb : O.nn Zb)
    (a N : Nat → I → V3 R)
    (hN0 : ∀ t, Le K O.toCone (lsum K (fun r => dot3 K (N t r) (N t r)) l)
      (lsum K (fun r => dot3 K (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (a t) (a t) r)
        (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (a t) (a t) r)) l))
    (hN : ∀ t, Le K O.toCone (lsum K (fun r => K.mul (wt K kz r) (dot3 K (N t r) (N t r))) l)
      (lsum K (fun r => K.mul (wt K kz r) (dot3 K (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (a t) (a t) r)
        (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (a t) (a t) r))) l))
    (memb : I → Bool) (hm : ∀ r, r ∈ l → memb r = true) (hm' : ∀ r, memb r = true → r ∈ l)
    (hs : ∀ t r, r ∈ l → Step K h ν (wt K kz r) (a (t + 1) r) (a t r) (N t r))
    (choice : Le K O.toCone (K.mul (EnstLocalGalerkin.cc K ι (EnstLocalGalerkin.C2 K inv Rc t0)) Zb) (K.mul ν ν))
    (n k : Nat) (Qmax Emax : R)
    (hQ : Le K O.toCone (EnstGlobal.Q K ν h (EnstGalerkin.Zs K l kz a) (EnstGalerkin.As K l kz a) (n * k)) Qmax)
    (hbud : Le K O.toCone (K.add Qmax
      (K.mul (castN K n) (K.mul h (K.mul (EnstLocalGalerkin.cc K ι (EnstLocalGalerkin.C1 K inv Rc)) (K.mul Zb Zb)))))
      (K.mul ν Zb))
    (e D1 D2 D3 : R) (he : O.nn e) (hD1 : O.nn D1) (hD2 : O.nn D2) (hD3 : O.nn D3)
    (H1 : Le K O.toCone (K.mul (AgmonAbsorb.c3 K (castN K 16)) (AgmonAbsorb.q5 K Zb))
      (K.mul (AgmonAbsorb.c3 K (castN K 9)) (K.mul (RootYoung.four K) (K.mul (AgmonAbsorb.c3 K e) D1))))
    (H2 : Le K O.toCone (K.mul (castN K 16) (AgmonAbsorb.q5 K Zb))
      (K.mul (castN K 9) (K.mul (RootYoung.four K) (K.mul (AgmonAbsorb.c3 K e) D2))))
    (H3 : Le K O.toCone (AgmonAbsorb.c3 K Zb) (K.mul (RootYoung.four K) (K.mul e D3)))
    (hε : Le K O.toCone (K.mul (K.mul ι ι) (K.add (K.mul (AgmonCollapse.k1 K (castN K 3) (castN K 9) (castN K 108)) e)
      (K.mul (AgmonCollapse.k2 K (castN K 9) (castN K 108)) (K.add e (K.mul (castN K 2) e))))) (K.mul ν ν))
    (hE : Le K O.toCone (H2Linear.En K ν h (K.mul (K.mul ι ι) (K.add (K.mul (AgmonCollapse.k1 K (castN K 3) (castN K 9) (castN K 108)) e)
        (K.mul (AgmonCollapse.k2 K (castN K 9) (castN K 108)) (K.add e (K.mul (castN K 2) e)))))
        (fun s => ForceAbsorb.As K kz l (a s)) (fun s => ForceAbsorb.Bs K kz l (a s)) (n * k)) Emax)
    (t : Nat) (ht : t ≤ n) :
    Le K O.toCone (EnstGalerkin.Zs K l kz a (n * k + t)) Zb ∧
    Le K O.toCone (H2Linear.En K ν h (K.mul (K.mul ι ι) (K.add (K.mul (AgmonCollapse.k1 K (castN K 3) (castN K 9) (castN K 108)) e)
        (K.mul (AgmonCollapse.k2 K (castN K 9) (castN K 108)) (K.add e (K.mul (castN K 2) e)))))
        (fun s => ForceAbsorb.As K kz l (a s)) (fun s => ForceAbsorb.Bs K kz l (a s)) (n * k + t))
      (K.add Emax (K.mul (castN K t) (K.mul h (K.mul (K.mul ι ι)
          (K.add (K.mul (AgmonCollapse.k1 K (castN K 3) (castN K 9) (castN K 108))
              (K.add D1 (K.mul (AgmonAbsorb.c3 K (castN K 2)) (K.mul Zb Zb))))
            (K.mul (AgmonCollapse.k2 K (castN K 9) (castN K 108)) (K.add D2 (K.mul (castN K 2) D3)))))))) := by
  have w := StepZeroExt.h2_large_le_mem K O inv mul_inv h10 l kz hd h1 Mm hM Rc t0 hRM ι h ν Zb hh hν hν0 hZb
    (fun s => a (n * k + s)) (fun s => N (n * k + s)) (fun s => hN0 (n * k + s)) (fun s => hN (n * k + s))
    memb hm hm' (fun s r hr => hs (n * k + s) r hr) choice n
    (le_trans' K O.toCone _ _ _ (le_add' K O.toCone _ _ _ _ hQ (le_refl' K O.toCone _)) hbud)
    e D1 D2 D3 he hD1 hD2 hD3 H1 H2 H3 hε t ht
  exact ⟨w.1, le_trans' K O.toCone _ _ _ w.2 (le_add' K O.toCone _ _ _ _ hE (le_refl' K O.toCone _))⟩

end Gen

end WindowHypsMem

#print axioms WindowHypsMem.window_hyps_mem

ns/WindowLarge.lean

lean_all/ns/WindowLarge.lean

/-
  ns/WindowLarge.lean — T37 (goal G795b, piece e): the large-data truncation Cauchy bound across K′ windows with the
  explicit factor 2^k per window, over an ordered field with inverses. Windows have n = m·k steps with
  2m·hα·ν⁻¹ ≤ 1 (α the Gronwall rate of LargeCauchy); with d_j = Q_d(n·j) and S₀ = n·h·S:
    large_explicit   d_K′ ≤ (2^k)^K′ d_0 + 2^k S₀ Σ_{j<K′} (2^k)^j,
  provided every window start satisfies the local-time hypotheses with the same Zb, E_A, E_B
  (WindowShift.large_chain + WindowBound.factor_chain).

  Lean 4 core plus ns/WindowShift, ns/WindowBound.
-/
import ns.WindowShift
import ns.WindowBound

namespace WindowLarge
open DetChain NSChain DyadicSmallGlobal LibRing DyadicLarge FourierGalerkin GalerkinForce ForceBound ForceSob
  ModeCount CubeCount ForceUniform NormCompare EnstStep EnstGlobal EnstGalerkin ForceTwoScale ForceBilin EnstDiff
  ForceDiff TruncError TruncBound RestrStep GronwallSource EnstGronwall WeightSplit LatticeSum TruncCauchy TruncRate

section Gen
variable {R : Type} (K : CRing R) (O : OrdR K) {I : Type}
variable (inv : R → R) (mul_inv : ∀ y, y ≠ K.zero → K.mul y (inv y) = K.one) (h10 : K.one ≠ K.zero)

include mul_inv h10 in
/-- **Large-data truncation Cauchy bound over K′ windows, factor 2^k per window.** -/
theorem large_explicit (l : List I) (kz : I → Mode) (hd : List.Pairwise (fun a b => kz a ≠ kz b) l)
    (h1 : ∀ p, 1 ≤ nrm (kz p)) (M : Nat) (hM : ∀ p, p ∈ l → nrm (kz p) ≤ M + 1) (Rc R' t0 : Nat) (hRM : R' ≤ M)
    (ι h ν Zb Aa Ab iR : R) (hh : O.nn h) (hν : O.nn ν) (hZb : O.nn Zb) (hAa : O.nn Aa) (hAb : O.nn Ab) (hiRn : O.nn iR)
    (hiR : K.mul (K.mul (castN K (Rc + 1)) (castN K (Rc + 1))) iR = K.one)
    (A B : Nat → I → V3 R)
    (hA : ∀ t r, Step K h ν (wt K kz r) (A (t + 1) r) (A t r) (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (A t) (A t) r))
    (hB : ∀ t r, r ∈ l.filter (lowB Rc kz) → Step K h ν (wt K kz r) (B (t + 1) r) (B t r)
      (Nl K (l.filter (lowB Rc kz)) (TriadSparse.delta K kz) ι (kv K kz) (B t) (B t) r))
    (n : Nat) (hZA : ∀ k t, t < n → Le K O.toCone (Zl K kz l (A (n * k + t))) Zb)
    (εA εB DA DB EA EB : R) (hεA : O.nn εA) (hεB : O.nn εB) (hDA : O.nn DA) (hDB : O.nn DB) (hν0 : ν ≠ K.zero)
    (hEA : ∀ k t, t ≤ n → Le K O.toCone (K.add (K.mul ν (Al K kz l (A (n * k + t))))
      (K.mul (K.mul h εA) (lsum K (fun p => K.mul (K.mul (K.mul (wt K kz p) (wt K kz p)) (wt K kz p))
        (dot3 K (A (n * k + t) p) (A (n * k + t) p))) l)))
      (K.add EA (K.mul (castN K t) (K.mul h DA))))
    (hEB : ∀ k t, t ≤ n → Le K O.toCone (K.add (K.mul ν (Al K kz (l.filter (lowB Rc kz)) (B (n * k + t))))
      (K.mul (K.mul h εB) (lsum K (fun p => K.mul (K.mul (K.mul (wt K kz p) (wt K kz p)) (wt K kz p))
        (dot3 K (B (n * k + t) p) (B (n * k + t) p))) (l.filter (lowB Rc kz)))))
      (K.add EB (K.mul (castN K t) (K.mul h DB))))
    (hAa_def : Aa = K.mul (K.add EA (K.mul (castN K n) (K.mul h DA))) (inv ν))
    (hAb_def : Ab = K.mul (K.add EB (K.mul (castN K n) (K.mul h DB))) (inv ν))
    (hβc : Le K O.toCone (K.mul (K.mul (two' K) (K.mul (two' K) (two' K))) (K.mul (K.mul ι ι) (K.mul (C2' K inv R' t0) Zb)))
      (K.mul ν ν))
    (S0 : R)
    (hS0def : S0 = K.mul (castN K n) (K.mul h (K.mul (two' K)
            (K.add
              (K.mul (two' K) (K.add
                (K.mul (two' K) (K.mul (K.mul ι ι) (K.mul (K.mul Aa iR) (K.mul (C1' K inv R') Zb))))
                (K.mul (two' K) (K.mul (K.mul ι ι) (K.mul Aa (K.mul (C2' K inv R' t0) Zb))))))
              (K.mul (two' K) (K.mul (K.mul ι ι) (K.mul Aa (K.mul (castN K 135) (K.mul Aa iR)))))))))
    (iν : R) (hνi : K.mul ν iν = K.one) (hiν : O.nn iν) (m k : Nat) (hn : n = m * k)
    (ha : O.nn (K.mul (K.mul h (K.add (K.mul (K.mul (two' K) (K.mul (two' K) (two' K))) (K.mul (K.mul ι ι) (K.mul (C1' K inv R') Zb)))
            (K.mul (K.mul (two' K) (two' K)) (K.mul (K.mul ι ι) (K.mul (castN K 135) Ab))))) iν))
    (hm : Le K O.toCone (K.mul (K.add K.one K.one) (K.mul (castN K m) (K.mul (K.mul h (K.add (K.mul (K.mul (two' K) (K.mul (two' K) (two' K))) (K.mul (K.mul ι ι) (K.mul (C1' K inv R') Zb)))
            (K.mul (K.mul (two' K) (two' K)) (K.mul (K.mul ι ι) (K.mul (castN K 135) Ab))))) iν))) K.one)
    (hS0 : O.nn S0) :
    ∀ K' : Nat, Le K O.toCone ((Q K ν h (fun t => Zl K kz (l.filter (lowB Rc kz)) (dF K (restr K (lowB Rc kz) (A t)) (B t)))
          (fun t => Al K kz (l.filter (lowB Rc kz)) (dF K (restr K (lowB Rc kz) (A t)) (B t)))) (n * K'))
      (K.add (K.mul (pwr K (pwr K (K.add K.one K.one) k) K') ((Q K ν h (fun t => Zl K kz (l.filter (lowB Rc kz)) (dF K (restr K (lowB Rc kz) (A t)) (B t)))
          (fun t => Al K kz (l.filter (lowB Rc kz)) (dF K (restr K (lowB Rc kz) (A t)) (B t)))) (n * 0)))
        (K.mul (K.mul (pwr K (K.add K.one K.one) k) S0) (sumN K (pwr K (pwr K (K.add K.one K.one) k)) K'))) := by
  have hw : ∀ r, O.nn (wt K kz r) := fun r => dot3_nn K O.toCone _
  have hZn : ∀ (L : List I) (u : I → V3 R), O.nn (Zl K kz L u) := fun L u =>
    lsum_nn K O.toCone _ (fun r => O.nn_mul _ _ (hw r) (dot3_nn K O.toCone _)) L
  have hAn : ∀ (L : List I) (u : I → V3 R), O.nn (Al K kz L u) := fun L u =>
    lsum_nn K O.toCone _ (fun r => O.nn_mul _ _ (O.nn_sq _) (dot3_nn K O.toCone _)) L
  have hd0 : O.nn ((Q K ν h (fun t => Zl K kz (l.filter (lowB Rc kz)) (dF K (restr K (lowB Rc kz) (A t)) (B t)))
          (fun t => Al K kz (l.filter (lowB Rc kz)) (dF K (restr K (lowB Rc kz) (A t)) (B t)))) (n * 0)) :=
    O.nn_add _ _ (O.nn_mul _ _ hν (hZn _ _)) (O.nn_mul _ _ (O.nn_mul _ _ hh (O.nn_sq ν)) (hAn _ _))
  -- 0 ≤ hα: hα = (hα·ν⁻¹)·ν
  have hhα : O.nn (K.mul h (K.add (K.mul (K.mul (two' K) (K.mul (two' K) (two' K))) (K.mul (K.mul ι ι) (K.mul (C1' K inv R') Zb)))
            (K.mul (K.mul (two' K) (two' K)) (K.mul (K.mul ι ι) (K.mul (castN K 135) Ab))))) := by
    have e : K.mul h (K.add (K.mul (K.mul (two' K) (K.mul (two' K) (two' K))) (K.mul (K.mul ι ι) (K.mul (C1' K inv R') Zb)))
            (K.mul (K.mul (two' K) (two' K)) (K.mul (K.mul ι ι) (K.mul (castN K 135) Ab)))) = K.mul (K.mul (K.mul h (K.add (K.mul (K.mul (two' K) (K.mul (two' K) (two' K))) (K.mul (K.mul ι ι) (K.mul (C1' K inv R') Zb)))
            (K.mul (K.mul (two' K) (two' K)) (K.mul (K.mul ι ι) (K.mul (castN K 135) Ab))))) iν) ν := by
      calc K.mul h (K.add (K.mul (K.mul (two' K) (K.mul (two' K) (two' K))) (K.mul (K.mul ι ι) (K.mul (C1' K inv R') Zb)))
            (K.mul (K.mul (two' K) (two' K)) (K.mul (K.mul ι ι) (K.mul (castN K 135) Ab)))) = K.mul (K.mul h (K.add (K.mul (K.mul (two' K) (K.mul (two' K) (two' K))) (K.mul (K.mul ι ι) (K.mul (C1' K inv R') Zb)))
            (K.mul (K.mul (two' K) (two' K)) (K.mul (K.mul ι ι) (K.mul (castN K 135) Ab))))) K.one := by lib_ring
        _ = K.mul (K.mul h (K.add (K.mul (K.mul (two' K) (K.mul (two' K) (two' K))) (K.mul (K.mul ι ι) (K.mul (C1' K inv R') Zb)))
            (K.mul (K.mul (two' K) (two' K)) (K.mul (K.mul ι ι) (K.mul (castN K 135) Ab))))) (K.mul ν iν) := by rw [hνi]
        _ = _ := by lib_ring
    rw [e]; exact O.nn_mul _ _ ha hν
  have hc : O.nn (K.mul (pwr K (K.add ν (K.mul h (K.add (K.mul (K.mul (two' K) (K.mul (two' K) (two' K))) (K.mul (K.mul ι ι) (K.mul (C1' K inv R') Zb)))
            (K.mul (K.mul (two' K) (two' K)) (K.mul (K.mul ι ι) (K.mul (castN K 135) Ab)))))) (m * k)) (pwr K iν (m * k))) :=
    O.nn_mul _ _ (pwr_nn K O _ (O.nn_add _ _ hν hhα) _) (pwr_nn K O iν hiν _)
  have ch := WindowShift.large_chain K O inv mul_inv h10 l kz hd h1 M hM Rc R' t0 hRM ι h ν Zb Aa Ab iR hh hν hZb hAa
    hAb hiRn hiR A B hA hB n hZA εA εB DA DB EA EB hεA hεB hDA hDB hν0 hEA hEB hAa_def hAb_def hβc
    (pwr K (K.add ν (K.mul h (K.add (K.mul (K.mul (two' K) (K.mul (two' K) (two' K))) (K.mul (K.mul ι ι) (K.mul (C1' K inv R') Zb)))
            (K.mul (K.mul (two' K) (two' K)) (K.mul (K.mul ι ι) (K.mul (castN K 135) Ab)))))) n) S0 rfl hS0def
    (pwr_nn K O _ (O.nn_add _ _ hν hhα) _) iν hνi hiν
  intro K'
  subst hn
  exact WindowBound.factor_chain K O ν (K.mul h (K.add (K.mul (K.mul (two' K) (K.mul (two' K) (two' K))) (K.mul (K.mul ι ι) (K.mul (C1' K inv R') Zb)))
            (K.mul (K.mul (two' K) (two' K)) (K.mul (K.mul ι ι) (K.mul (castN K 135) Ab))))) iν S0 m k
    (fun j => (Q K ν h (fun t => Zl K kz (l.filter (lowB Rc kz)) (dF K (restr K (lowB Rc kz) (A t)) (B t)))
          (fun t => Al K kz (l.filter (lowB Rc kz)) (dF K (restr K (lowB Rc kz) (A t)) (B t)))) (m * k * j)) hν hνi hiν ha hm hc hd0 hS0 ch K'

end Gen

end WindowLarge

#print axioms WindowLarge.large_explicit

ns/WindowShift.lean

lean_all/ns/WindowShift.lean

/-
  ns/WindowShift.lean — T37 (goal G795b, piece b): the large-data truncation Cauchy estimate carried across
  consecutive windows of n steps, over an ordered field with inverses.
    pwr_inv        ν^n·(ν⁻¹)^n = 1,
    scale_chain    ν^n d_{k+1} ≤ P (d_k + s₀) for all k  ⇒  d_K ≤ c^K d_0 + c s₀ Σ_{j<K} c^j,  c = P (ν⁻¹)^n,
    large_window   LargeCauchy.large_cauchy restarted at time n·k (the Galerkin step hypotheses hold at every time,
                   so the shifted sequences A(n·k + ·), B(n·k + ·) satisfy them unchanged): with d_k = Q_d(n·k),
                   ν^n d_{k+1} ≤ P (d_k + n·h·S),
    large_chain    the two combined: if every window start satisfies the local-time hypotheses (Z ≤ Zb and the
                   window H² energy bounds with the same E, D), then d_K ≤ c^K d_0 + c·n·h·S·Σ_{j<K} c^j.
  The remaining hypothesis — the same Zb, E at each window start — is the discrete form of "Q bounded on [0, t₄]".

  Lean 4 core plus ns/LargeCauchy, ns/WindowChain.
-/
import ns.LargeCauchy
import ns.WindowChain

namespace WindowShift
open DetChain NSChain DyadicSmallGlobal LibRing DyadicLarge FourierGalerkin GalerkinForce ForceBound ForceSob
  ModeCount CubeCount ForceUniform NormCompare EnstStep EnstGlobal EnstGalerkin ForceTwoScale ForceBilin EnstDiff
  ForceDiff TruncError TruncBound RestrStep GronwallSource EnstGronwall WeightSplit LatticeSum TruncCauchy TruncRate

section Ring
variable {R : Type} (K : CRing R) (O : OrdR K)

/-- ν^n·(ν⁻¹)^n = 1 -/
theorem pwr_inv (ν iν : R) (hνi : K.mul ν iν = K.one) : ∀ n : Nat, K.mul (pwr K ν n) (pwr K iν n) = K.one
  | 0 => by show K.mul K.one K.one = K.one; lib_ring
  | n + 1 => by
    show K.mul (K.mul ν (pwr K ν n)) (K.mul iν (pwr K iν n)) = K.one
    calc K.mul (K.mul ν (pwr K ν n)) (K.mul iν (pwr K iν n))
        = K.mul (K.mul ν iν) (K.mul (pwr K ν n) (pwr K iν n)) := by lib_ring
      _ = K.mul K.one K.one := by rw [hνi, pwr_inv ν iν hνi n]
      _ = K.one := by lib_ring

/-- one window, rescaled: ν^n x ≤ P (y + s₀) ⇒ x ≤ c y + c s₀ with c = P (ν⁻¹)^n -/
theorem scale_step (ν iν P s0 x y : R) (n : Nat) (hνi : K.mul ν iν = K.one) (hiν : O.nn iν)
    (h : Le K O.toCone (K.mul (pwr K ν n) x) (K.mul P (K.add y s0))) :
    Le K O.toCone x (K.add (K.mul (K.mul P (pwr K iν n)) y) (K.mul (K.mul P (pwr K iν n)) s0)) := by
  have m := le_mul_nn K O.toCone (pwr K iν n) _ _ (pwr_nn K O iν hiν n) h
  have e1 : K.mul (pwr K iν n) (K.mul (pwr K ν n) x) = x := by
    calc K.mul (pwr K iν n) (K.mul (pwr K ν n) x) = K.mul (K.mul (pwr K ν n) (pwr K iν n)) x := by lib_ring
      _ = K.mul K.one x := by rw [pwr_inv K ν iν hνi n]
      _ = x := by lib_ring
  have e2 : K.mul (pwr K iν n) (K.mul P (K.add y s0))
      = K.add (K.mul (K.mul P (pwr K iν n)) y) (K.mul (K.mul P (pwr K iν n)) s0) := by lib_ring
  rw [e1, e2] at m
  exact m

/-- **window chain from the unscaled window bound** -/
theorem scale_chain (ν iν P s0 : R) (n : Nat) (d : Nat → R) (hνi : K.mul ν iν = K.one) (hiν : O.nn iν)
    (hP : O.nn P) (hwin : ∀ k, Le K O.toCone (K.mul (pwr K ν n) (d (k + 1))) (K.mul P (K.add (d k) s0))) :
    ∀ K' : Nat, Le K O.toCone (d K')
      (K.add (K.mul (pwr K (K.mul P (pwr K iν n)) K') (d 0))
        (K.mul (K.mul (K.mul P (pwr K iν n)) s0) (sumN K (pwr K (K.mul P (pwr K iν n))) K'))) :=
  WindowChain.window_chain K O (K.mul P (pwr K iν n)) (K.mul (K.mul P (pwr K iν n)) s0) d
    (O.nn_mul _ _ hP (pwr_nn K O iν hiν n))
    (fun k => scale_step K O ν iν P s0 (d (k + 1)) (d k) n hνi hiν (hwin k))

end Ring

section Gen
variable {R : Type} (K : CRing R) (O : OrdR K) {I : Type}
variable (inv : R → R) (mul_inv : ∀ y, y ≠ K.zero → K.mul y (inv y) = K.one) (h10 : K.one ≠ K.zero)

include mul_inv h10 in
/-- **LargeCauchy on the window [n·k, n·(k+1)]** -/
theorem large_window (l : List I) (kz : I → Mode) (hd : List.Pairwise (fun a b => kz a ≠ kz b) l)
    (h1 : ∀ p, 1 ≤ nrm (kz p)) (M : Nat) (hM : ∀ p, p ∈ l → nrm (kz p) ≤ M + 1) (Rc R' t0 : Nat) (hRM : R' ≤ M)
    (ι h ν Zb Aa Ab iR : R) (hh : O.nn h) (hν : O.nn ν) (hZb : O.nn Zb) (hAa : O.nn Aa) (hAb : O.nn Ab) (hiRn : O.nn iR)
    (hiR : K.mul (K.mul (castN K (Rc + 1)) (castN K (Rc + 1))) iR = K.one)
    (A B : Nat → I → V3 R)
    (hA : ∀ t r, Step K h ν (wt K kz r) (A (t + 1) r) (A t r) (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (A t) (A t) r))
    (hB : ∀ t r, r ∈ l.filter (lowB Rc kz) → Step K h ν (wt K kz r) (B (t + 1) r) (B t r)
      (Nl K (l.filter (lowB Rc kz)) (TriadSparse.delta K kz) ι (kv K kz) (B t) (B t) r))
    (n k : Nat) (hZA : ∀ t, t < n → Le K O.toCone (Zl K kz l (A (n * k + t))) Zb)
    (εA εB DA DB EA EB : R) (hεA : O.nn εA) (hεB : O.nn εB) (hDA : O.nn DA) (hDB : O.nn DB) (hν0 : ν ≠ K.zero)
    (hEA : ∀ t, t ≤ n → Le K O.toCone (K.add (K.mul ν (Al K kz l (A (n * k + t))))
      (K.mul (K.mul h εA) (lsum K (fun p => K.mul (K.mul (K.mul (wt K kz p) (wt K kz p)) (wt K kz p))
        (dot3 K (A (n * k + t) p) (A (n * k + t) p))) l)))
      (K.add EA (K.mul (castN K t) (K.mul h DA))))
    (hEB : ∀ t, t ≤ n → Le K O.toCone (K.add (K.mul ν (Al K kz (l.filter (lowB Rc kz)) (B (n * k + t))))
      (K.mul (K.mul h εB) (lsum K (fun p => K.mul (K.mul (K.mul (wt K kz p) (wt K kz p)) (wt K kz p))
        (dot3 K (B (n * k + t) p) (B (n * k + t) p))) (l.filter (lowB Rc kz)))))
      (K.add EB (K.mul (castN K t) (K.mul h DB))))
    (hAa_def : Aa = K.mul (K.add EA (K.mul (castN K n) (K.mul h DA))) (inv ν))
    (hAb_def : Ab = K.mul (K.add EB (K.mul (castN K n) (K.mul h DB))) (inv ν))
    (hβc : Le K O.toCone (K.mul (K.mul (two' K) (K.mul (two' K) (two' K))) (K.mul (K.mul ι ι) (K.mul (C2' K inv R' t0) Zb)))
      (K.mul ν ν)) :
    Le K O.toCone (K.mul (pwr K ν n)
        (Q K ν h (fun t => Zl K kz (l.filter (lowB Rc kz)) (dF K (restr K (lowB Rc kz) (A t)) (B t)))
          (fun t => Al K kz (l.filter (lowB Rc kz)) (dF K (restr K (lowB Rc kz) (A t)) (B t))) (n * (k + 1))))
      (K.mul (pwr K (K.add ν (K.mul h
          (K.add (K.mul (K.mul (two' K) (K.mul (two' K) (two' K))) (K.mul (K.mul ι ι) (K.mul (C1' K inv R') Zb)))
            (K.mul (K.mul (two' K) (two' K)) (K.mul (K.mul ι ι) (K.mul (castN K 135) Ab)))))) n)
        (K.add (Q K ν h (fun t => Zl K kz (l.filter (lowB Rc kz)) (dF K (restr K (lowB Rc kz) (A t)) (B t)))
          (fun t => Al K kz (l.filter (lowB Rc kz)) (dF K (restr K (lowB Rc kz) (A t)) (B t))) (n * k))
          (K.mul (castN K n) (K.mul h (K.mul (two' K)
            (K.add
              (K.mul (two' K) (K.add
                (K.mul (two' K) (K.mul (K.mul ι ι) (K.mul (K.mul Aa iR) (K.mul (C1' K inv R') Zb))))
                (K.mul (two' K) (K.mul (K.mul ι ι) (K.mul Aa (K.mul (C2' K inv R' t0) Zb))))))
              (K.mul (two' K) (K.mul (K.mul ι ι) (K.mul Aa (K.mul (castN K 135) (K.mul Aa iR))))))))))) :=
  LargeCauchy.large_cauchy K O inv mul_inv h10 l kz hd h1 M hM Rc R' t0 hRM ι h ν Zb Aa Ab iR hh hν hZb hAa hAb
    hiRn hiR (fun t => A (n * k + t)) (fun t => B (n * k + t)) (fun t r => hA (n * k + t) r)
    (fun t r hr => hB (n * k + t) r hr) n hZA εA εB DA DB EA EB hεA hεB hDA hDB hν0 hEA hEB hAa_def hAb_def hβc
    n (Nat.le.refl)

include mul_inv h10 in
/-- **Truncation Cauchy bound across K' windows of the large-data problem.** -/
theorem large_chain (l : List I) (kz : I → Mode) (hd : List.Pairwise (fun a b => kz a ≠ kz b) l)
    (h1 : ∀ p, 1 ≤ nrm (kz p)) (M : Nat) (hM : ∀ p, p ∈ l → nrm (kz p) ≤ M + 1) (Rc R' t0 : Nat) (hRM : R' ≤ M)
    (ι h ν Zb Aa Ab iR : R) (hh : O.nn h) (hν : O.nn ν) (hZb : O.nn Zb) (hAa : O.nn Aa) (hAb : O.nn Ab) (hiRn : O.nn iR)
    (hiR : K.mul (K.mul (castN K (Rc + 1)) (castN K (Rc + 1))) iR = K.one)
    (A B : Nat → I → V3 R)
    (hA : ∀ t r, Step K h ν (wt K kz r) (A (t + 1) r) (A t r) (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (A t) (A t) r))
    (hB : ∀ t r, r ∈ l.filter (lowB Rc kz) → Step K h ν (wt K kz r) (B (t + 1) r) (B t r)
      (Nl K (l.filter (lowB Rc kz)) (TriadSparse.delta K kz) ι (kv K kz) (B t) (B t) r))
    (n : Nat) (hZA : ∀ k t, t < n → Le K O.toCone (Zl K kz l (A (n * k + t))) Zb)
    (εA εB DA DB EA EB : R) (hεA : O.nn εA) (hεB : O.nn εB) (hDA : O.nn DA) (hDB : O.nn DB) (hν0 : ν ≠ K.zero)
    (hEA : ∀ k t, t ≤ n → Le K O.toCone (K.add (K.mul ν (Al K kz l (A (n * k + t))))
      (K.mul (K.mul h εA) (lsum K (fun p => K.mul (K.mul (K.mul (wt K kz p) (wt K kz p)) (wt K kz p))
        (dot3 K (A (n * k + t) p) (A (n * k + t) p))) l)))
      (K.add EA (K.mul (castN K t) (K.mul h DA))))
    (hEB : ∀ k t, t ≤ n → Le K O.toCone (K.add (K.mul ν (Al K kz (l.filter (lowB Rc kz)) (B (n * k + t))))
      (K.mul (K.mul h εB) (lsum K (fun p => K.mul (K.mul (K.mul (wt K kz p) (wt K kz p)) (wt K kz p))
        (dot3 K (B (n * k + t) p) (B (n * k + t) p))) (l.filter (lowB Rc kz)))))
      (K.add EB (K.mul (castN K t) (K.mul h DB))))
    (hAa_def : Aa = K.mul (K.add EA (K.mul (castN K n) (K.mul h DA))) (inv ν))
    (hAb_def : Ab = K.mul (K.add EB (K.mul (castN K n) (K.mul h DB))) (inv ν))
    (hβc : Le K O.toCone (K.mul (K.mul (two' K) (K.mul (two' K) (two' K))) (K.mul (K.mul ι ι) (K.mul (C2' K inv R' t0) Zb)))
      (K.mul ν ν))
    (P S0 : R)
    (hPdef : P = pwr K (K.add ν (K.mul h
          (K.add (K.mul (K.mul (two' K) (K.mul (two' K) (two' K))) (K.mul (K.mul ι ι) (K.mul (C1' K inv R') Zb)))
            (K.mul (K.mul (two' K) (two' K)) (K.mul (K.mul ι ι) (K.mul (castN K 135) Ab)))))) n)
    (hS0def : S0 = K.mul (castN K n) (K.mul h (K.mul (two' K)
            (K.add
              (K.mul (two' K) (K.add
                (K.mul (two' K) (K.mul (K.mul ι ι) (K.mul (K.mul Aa iR) (K.mul (C1' K inv R') Zb))))
                (K.mul (two' K) (K.mul (K.mul ι ι) (K.mul Aa (K.mul (C2' K inv R' t0) Zb))))))
              (K.mul (two' K) (K.mul (K.mul ι ι) (K.mul Aa (K.mul (castN K 135) (K.mul Aa iR)))))))))
    (hP : O.nn P) (iν : R) (hνi : K.mul ν iν = K.one) (hiν : O.nn iν) :
    ∀ K' : Nat, Le K O.toCone
      (Q K ν h (fun t => Zl K kz (l.filter (lowB Rc kz)) (dF K (restr K (lowB Rc kz) (A t)) (B t)))
          (fun t => Al K kz (l.filter (lowB Rc kz)) (dF K (restr K (lowB Rc kz) (A t)) (B t))) (n * K'))
      (K.add (K.mul (pwr K (K.mul P (pwr K iν n)) K')
          (Q K ν h (fun t => Zl K kz (l.filter (lowB Rc kz)) (dF K (restr K (lowB Rc kz) (A t)) (B t)))
            (fun t => Al K kz (l.filter (lowB Rc kz)) (dF K (restr K (lowB Rc kz) (A t)) (B t))) (n * 0)))
        (K.mul (K.mul (K.mul P (pwr K iν n)) S0) (sumN K (pwr K (K.mul P (pwr K iν n))) K'))) := by
  subst hPdef
  subst hS0def
  exact scale_chain K O ν iν _ _ n
    (fun k => Q K ν h (fun t => Zl K kz (l.filter (lowB Rc kz)) (dF K (restr K (lowB Rc kz) (A t)) (B t)))
      (fun t => Al K kz (l.filter (lowB Rc kz)) (dF K (restr K (lowB Rc kz) (A t)) (B t))) (n * k))
    hνi hiν hP
    (fun k => large_window K O inv mul_inv h10 l kz hd h1 M hM Rc R' t0 hRM ι h ν Zb Aa Ab iR hh hν hZb hAa hAb
      hiRn hiR A B hA hB n k (hZA k) εA εB DA DB EA EB hεA hεB hDA hDB hν0 (hEA k) (hEB k) hAa_def hAb_def hβc)

end Gen

end WindowShift

#print axioms WindowShift.pwr_inv
#print axioms WindowShift.scale_step
#print axioms WindowShift.scale_chain
#print axioms WindowShift.large_window
#print axioms WindowShift.large_chain

ns/WindowShiftUpto.lean

lean_all/ns/WindowShiftUpto.lean

/-
  ns/WindowShiftUpto.lean — T37 (goal G795b-i, piece b): WindowShift.large_chain with the per-window hypotheses
  only on the first K′ windows:
    large_chain_upto   d_j ≤ c^j d_0 + c s₀ Σ_{i<j} c^i for j ≤ K′  (d_j = Q_d(n·j), c = P (ν⁻¹)^n),
  so the window-start energy bound may depend on K′ (E ≤ E_0 + K′·n·h·D, WindowChainUpto.lin_upto).

  Lean 4 core plus ns/WindowShift, ns/WindowChainUpto.
-/
import ns.WindowShift
import ns.WindowChainUpto

namespace WindowShiftUpto
open DetChain NSChain DyadicSmallGlobal LibRing DyadicLarge FourierGalerkin GalerkinForce ForceBound ForceSob
  ModeCount CubeCount ForceUniform NormCompare EnstStep EnstGlobal EnstGalerkin ForceTwoScale ForceBilin EnstDiff
  ForceDiff TruncError TruncBound RestrStep GronwallSource EnstGronwall WeightSplit LatticeSum TruncCauchy TruncRate

section Gen
variable {R : Type} (K : CRing R) (O : OrdR K) {I : Type}
variable (inv : R → R) (mul_inv : ∀ y, y ≠ K.zero → K.mul y (inv y) = K.one) (h10 : K.one ≠ K.zero)

include mul_inv h10 in
/-- **large_chain on the first K′ windows** -/
theorem large_chain_upto (l : List I) (kz : I → Mode) (hd : List.Pairwise (fun a b => kz a ≠ kz b) l)
    (h1 : ∀ p, 1 ≤ nrm (kz p)) (M : Nat) (hM : ∀ p, p ∈ l → nrm (kz p) ≤ M + 1) (Rc R' t0 : Nat) (hRM : R' ≤ M)
    (ι h ν Zb Aa Ab iR : R) (hh : O.nn h) (hν : O.nn ν) (hZb : O.nn Zb) (hAa : O.nn Aa) (hAb : O.nn Ab) (hiRn : O.nn iR)
    (hiR : K.mul (K.mul (castN K (Rc + 1)) (castN K (Rc + 1))) iR = K.one)
    (A B : Nat → I → V3 R)
    (hA : ∀ t r, Step K h ν (wt K kz r) (A (t + 1) r) (A t r) (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (A t) (A t) r))
    (hB : ∀ t r, r ∈ l.filter (lowB Rc kz) → Step K h ν (wt K kz r) (B (t + 1) r) (B t r)
      (Nl K (l.filter (lowB Rc kz)) (TriadSparse.delta K kz) ι (kv K kz) (B t) (B t) r))
    (n K' : Nat) (hZA : ∀ k, k < K' → ∀ t, t < n → Le K O.toCone (Zl K kz l (A (n * k + t))) Zb)
    (εA εB DA DB EA EB : R) (hεA : O.nn εA) (hεB : O.nn εB) (hDA : O.nn DA) (hDB : O.nn DB) (hν0 : ν ≠ K.zero)
    (hEA : ∀ k, k < K' → ∀ t, t ≤ n → Le K O.toCone (K.add (K.mul ν (Al K kz l (A (n * k + t))))
      (K.mul (K.mul h εA) (lsum K (fun p => K.mul (K.mul (K.mul (wt K kz p) (wt K kz p)) (wt K kz p))
        (dot3 K (A (n * k + t) p) (A (n * k + t) p))) l)))
      (K.add EA (K.mul (castN K t) (K.mul h DA))))
    (hEB : ∀ k, k < K' → ∀ t, t ≤ n → Le K O.toCone (K.add (K.mul ν (Al K kz (l.filter (lowB Rc kz)) (B (n * k + t))))
      (K.mul (K.mul h εB) (lsum K (fun p => K.mul (K.mul (K.mul (wt K kz p) (wt K kz p)) (wt K kz p))
        (dot3 K (B (n * k + t) p) (B (n * k + t) p))) (l.filter (lowB Rc kz)))))
      (K.add EB (K.mul (castN K t) (K.mul h DB))))
    (hAa_def : Aa = K.mul (K.add EA (K.mul (castN K n) (K.mul h DA))) (inv ν))
    (hAb_def : Ab = K.mul (K.add EB (K.mul (castN K n) (K.mul h DB))) (inv ν))
    (hβc : Le K O.toCone (K.mul (K.mul (two' K) (K.mul (two' K) (two' K))) (K.mul (K.mul ι ι) (K.mul (C2' K inv R' t0) Zb)))
      (K.mul ν ν))
    (P S0 : R)
    (hPdef : P = pwr K (K.add ν (K.mul h
          (K.add (K.mul (K.mul (two' K) (K.mul (two' K) (two' K))) (K.mul (K.mul ι ι) (K.mul (C1' K inv R') Zb)))
            (K.mul (K.mul (two' K) (two' K)) (K.mul (K.mul ι ι) (K.mul (castN K 135) Ab)))))) n)
    (hS0def : S0 = K.mul (castN K n) (K.mul h (K.mul (two' K)
            (K.add
              (K.mul (two' K) (K.add
                (K.mul (two' K) (K.mul (K.mul ι ι) (K.mul (K.mul Aa iR) (K.mul (C1' K inv R') Zb))))
                (K.mul (two' K) (K.mul (K.mul ι ι) (K.mul Aa (K.mul (C2' K inv R' t0) Zb))))))
              (K.mul (two' K) (K.mul (K.mul ι ι) (K.mul Aa (K.mul (castN K 135) (K.mul Aa iR)))))))))
    (hP : O.nn P) (iν : R) (hνi : K.mul ν iν = K.one) (hiν : O.nn iν) :
    ∀ j : Nat, j ≤ K' → Le K O.toCone
      (Q K ν h (fun t => Zl K kz (l.filter (lowB Rc kz)) (dF K (restr K (lowB Rc kz) (A t)) (B t)))
          (fun t => Al K kz (l.filter (lowB Rc kz)) (dF K (restr K (lowB Rc kz) (A t)) (B t))) (n * j))
      (K.add (K.mul (pwr K (K.mul P (pwr K iν n)) j)
          (Q K ν h (fun t => Zl K kz (l.filter (lowB Rc kz)) (dF K (restr K (lowB Rc kz) (A t)) (B t)))
            (fun t => Al K kz (l.filter (lowB Rc kz)) (dF K (restr K (lowB Rc kz) (A t)) (B t))) (n * 0)))
        (K.mul (K.mul (K.mul P (pwr K iν n)) S0) (sumN K (pwr K (K.mul P (pwr K iν n))) j))) := by
  subst hPdef
  subst hS0def
  exact WindowChainUpto.scale_chain_upto K O ν iν _ _ n
    (fun k => Q K ν h (fun t => Zl K kz (l.filter (lowB Rc kz)) (dF K (restr K (lowB Rc kz) (A t)) (B t)))
      (fun t => Al K kz (l.filter (lowB Rc kz)) (dF K (restr K (lowB Rc kz) (A t)) (B t))) (n * k))
    hνi hiν hP K'
    (fun k hk => WindowShift.large_window K O inv mul_inv h10 l kz hd h1 M hM Rc R' t0 hRM ι h ν Zb Aa Ab iR hh hν hZb
      hAa hAb hiRn hiR A B hA hB n k (hZA k hk) εA εB DA DB EA EB hεA hεB hDA hDB hν0 (hEA k hk) (hEB k hk)
      hAa_def hAb_def hβc)

end Gen

end WindowShiftUpto

#print axioms WindowShiftUpto.large_chain_upto

ns/Young34.lean

lean_all/ns/Young34.lean

/-
  ns/Young34.lean — T37 cycle 176: the 3D stretching step in Riccati form, by a sum-of-squares Young inequality.

  Lean 4 core plus lib/Gronwall (le), lib/RingTac.
    young34     3A⁴ + B⁴ − 4A³B = (A − B)²(2A² + (A + B)²) ≥ 0            (Young with exponents 4/3, 4)
    step34      Z' ≤ Z + h(κ q s³ − ν s⁴), q⁴ = Z³  ⇒  4ν³ Z' ≤ 4ν³ Z + h κ⁴ Z³
  With the measured stretching bound S ≤ κ Z^{3/4} P^{3/4} (κ ≈ 0.025, cycle 141; q = Z^{3/4}, s = P^{1/4})
  this is z' ≤ z + h (κ⁴/4ν³) z³: the Riccati step whose discrete blow-up time is the 3D local existence time.
-/
import lib.Gronwall

namespace Young34
open DetChain LibRing LibCS LibGron

variable {R : Type} (K : CRing R) (P : Pos K)

def sq (x : R) : R := K.mul x x
def p4 (x : R) : R := sq K (sq K x)

theorem young34 (A B : R) :
    P.nn (sub K (K.add (K.add (p4 K A) (K.add (p4 K A) (p4 K A))) (p4 K B))
      (K.add (K.add (K.mul (K.mul A (sq K A)) B) (K.mul (K.mul A (sq K A)) B))
             (K.add (K.mul (K.mul A (sq K A)) B) (K.mul (K.mul A (sq K A)) B)))) := by
  rw [show sub K (K.add (K.add (p4 K A) (K.add (p4 K A) (p4 K A))) (p4 K B))
      (K.add (K.add (K.mul (K.mul A (sq K A)) B) (K.mul (K.mul A (sq K A)) B))
             (K.add (K.mul (K.mul A (sq K A)) B) (K.mul (K.mul A (sq K A)) B)))
      = K.mul (sq K (sub K A B)) (K.add (K.add (sq K A) (sq K A)) (sq K (K.add A B))) by unfold p4 sq; lib_ring]
  exact P.nn_mul _ _ (P.nn_sq _) (P.nn_add _ _ (P.nn_add _ _ (P.nn_sq _) (P.nn_sq _)) (P.nn_sq _))

def four (x : R) : R := K.add (K.add x x) (K.add x x)

theorem four_nn (ν : R) (hν : P.nn ν) : P.nn (four K (K.mul ν (sq K ν))) := by
  have h3 : P.nn (K.mul ν (sq K ν)) := P.nn_mul _ _ hν (P.nn_sq ν)
  exact P.nn_add _ _ (P.nn_add _ _ h3 h3) (P.nn_add _ _ h3 h3)

/-- **3D stretching step in Riccati form.** -/
theorem step34 (Z Z' h κ q s ν : R) (hh : P.nn h) (hν : P.nn ν) (hq : sq K (sq K q) = K.mul Z (sq K Z))
    (step : le K P Z' (K.add Z (K.mul h (sub K (K.mul (K.mul κ q) (K.mul s (sq K s))) (K.mul ν (p4 K s)))))) :
    le K P (K.mul (four K (K.mul ν (sq K ν))) Z')
      (K.add (K.mul (four K (K.mul ν (sq K ν))) Z) (K.mul h (K.mul (p4 K κ) (K.mul Z (sq K Z))))) := by
  have y := young34 K P (K.mul ν s) (K.mul κ q)
  unfold le at step ⊢
  -- target − 4ν³Z' = 4ν³·(step defect) + h·[Young defect] + h·(νs)⁴
  rw [show K.mul (p4 K κ) (K.mul Z (sq K Z)) = p4 K (K.mul κ q) by rw [← hq]; unfold p4 sq; lib_ring]
  rw [show sub K (K.add (K.mul (four K (K.mul ν (sq K ν))) Z) (K.mul h (p4 K (K.mul κ q))))
        (K.mul (four K (K.mul ν (sq K ν))) Z')
      = K.add (K.add (K.mul (four K (K.mul ν (sq K ν)))
            (sub K (K.add Z (K.mul h (sub K (K.mul (K.mul κ q) (K.mul s (sq K s))) (K.mul ν (p4 K s))))) Z'))
          (K.mul h (sub K (K.add (K.add (p4 K (K.mul ν s)) (K.add (p4 K (K.mul ν s)) (p4 K (K.mul ν s)))) (p4 K (K.mul κ q)))
            (K.add (K.add (K.mul (K.mul (K.mul ν s) (sq K (K.mul ν s))) (K.mul κ q)) (K.mul (K.mul (K.mul ν s) (sq K (K.mul ν s))) (K.mul κ q)))
                   (K.add (K.mul (K.mul (K.mul ν s) (sq K (K.mul ν s))) (K.mul κ q)) (K.mul (K.mul (K.mul ν s) (sq K (K.mul ν s))) (K.mul κ q)))))))
        (K.mul h (p4 K (K.mul ν s))) by unfold four p4 sq; lib_ring]
  exact P.nn_add _ _ (P.nn_add _ _ (P.nn_mul _ _ (four_nn K P ν hν) step) (P.nn_mul _ _ hh y)) (P.nn_mul _ _ hh (P.nn_sq _))

end Young34

#print axioms Young34.young34
#print axioms Young34.four_nn
#print axioms Young34.step34

ns/YoungM.lean

lean_all/ns/YoungM.lean

/-
  ns/YoungM.lean — T37 cycle 776 (goal G487): Young's inequality with a bounded coefficient and no absolute values,
  over any ordered ring without zero divisors (ℚ included):
    M ≥ 0, M ≠ 0, X² ≤ M²  ⇒  2·x·y·X ≤ M·(x² + y²).
  First piece of G484b (the dyadic nonlinearity is one-sided Lipschitz under mode bounds).

  Lean 4 core plus ns/StokesCN (pattern), ns/GoodModesQ, lib/RingTac.
    pos_cancel   M ≥ 0, M ≠ 0, M·Z ≥ 0 ⇒ Z ≥ 0
    young_m      the statement
-/
import ns.StokesCN

namespace YoungM
open DetChain NSChain DyadicSmallGlobal LibRing DyadicLarge GoodModesQ

section Gen
variable {R : Type} (K : CRing R) (O : OrdR K)
include O

theorem pos_cancel (M Z : R) (hM : O.nn M) (hM0 : M ≠ K.zero) (h : O.nn (K.mul M Z)) : O.nn Z := by
  cases O.total K.zero Z with
  | inl l => cases l with
    | intro d hd => rw [← hd.2, K.zero_add]; exact hd.1
  | inr l => cases l with
    | intro d hd =>
      have dZ : Z = K.neg d := by
        have : Z = K.add (K.add Z d) (K.neg d) := by lib_ring
        rw [this, hd.2]; lib_ring
      have q0 : O.nn (K.neg (K.mul M d)) := by
        rw [show K.neg (K.mul M d) = K.mul M Z by rw [dZ]; lib_ring]; exact h
      have q1 : O.nn (K.mul M d) := O.nn_mul _ _ hM hd.1
      have z : K.mul M d = K.zero := O.antisym _ _ ⟨_, q0, by lib_ring⟩ ⟨_, q1, K.zero_add _⟩
      have d0 : d = K.zero := by
        cases O.noZD _ _ z with
        | inl e => exact absurd e hM0
        | inr e => exact e
      rw [dZ, d0, show K.neg K.zero = K.zero by lib_ring]; exact O.nn_zero

theorem young_m (M X x y : R) (hM : O.nn M) (hM0 : M ≠ K.zero)
    (hX : Le K O.toCone (K.mul X X) (K.mul M M)) :
    Le K O.toCone (K.mul (K.add K.one K.one) (K.mul (K.mul x y) X)) (K.mul M (K.add (K.mul x x) (K.mul y y))) := by
  cases hX with
  | intro e he =>
    -- M·(M(x²+y²) − 2xyX) = (Mx − Xy)² + (M² − X²) y² = (Mx − Xy)² + e·y²
    let Z := K.add (K.mul M (K.add (K.mul x x) (K.mul y y))) (K.neg (K.mul (K.add K.one K.one) (K.mul (K.mul x y) X)))
    have key : K.mul M Z = K.add (K.mul (K.add (K.mul M x) (K.neg (K.mul X y))) (K.add (K.mul M x) (K.neg (K.mul X y))))
        (K.mul e (K.mul y y)) := by
      have : e = K.add (K.mul M M) (K.neg (K.mul X X)) := by
        have : e = K.add (K.add (K.mul X X) e) (K.neg (K.mul X X)) := by lib_ring
        rw [this, he.2]
      show K.mul M (K.add (K.mul M (K.add (K.mul x x) (K.mul y y))) (K.neg (K.mul (K.add K.one K.one) (K.mul (K.mul x y) X))))
        = _
      rw [this]; lib_ring
    have hZ : O.nn Z := pos_cancel K O M Z hM hM0 (by rw [key]; exact O.nn_add _ _ (O.nn_sq _) (O.nn_mul _ _ he.1 (O.nn_sq _)))
    exact ⟨Z, hZ, by show K.add _ (K.add _ _) = _; lib_ring⟩

end Gen

end YoungM

#print axioms YoungM.pos_cancel
#print axioms YoungM.young_m

ns/YoungStep.lean

lean_all/ns/YoungStep.lean

/-
  ns/YoungStep.lean — T37 cycle 155: dissipation absorbs the stretching term, Serrin form of the step.

  Lean 4 core plus lib/Gronwall (le), lib/RingTac. With r² = Z = ‖∇u‖², s² = P = ‖Δu‖², X = ‖u‖_∞ and the
  stretching bound c X r s, the enstrophy step
      Z' ≤ Z + h (c X r s − ν s²)
  gives, by (ν s − c X r)² ≥ 0 (Young without division),
      2ν Z' ≤ (2ν + h c² X²) Z ,
  i.e. Z' ≤ (1 + a) Z with a = h c² X² / (2ν): the step of SerrinDiscrete / SerrinSum, a_k ∝ ‖u_k‖²_∞.
-/
import lib.Gronwall

namespace YoungStep
open DetChain LibRing LibCS LibGron

variable {R : Type} (K : CRing R) (P : Pos K)

theorem young_step (Z Z' h c X r s ν : R) (hh : P.nn h) (hν : P.nn ν) (hr : K.mul r r = Z)
    (step : le K P Z' (K.add Z (K.mul h (sub K (K.mul (K.mul (K.mul c X) r) s) (K.mul ν (K.mul s s)))))) :
    le K P (K.mul (K.add ν ν) Z') (K.mul (K.add (K.add ν ν) (K.mul h (K.mul (K.mul c c) (K.mul X X)))) Z) := by
  unfold le at step ⊢
  rw [← hr] at step ⊢
  rw [show sub K (K.mul (K.add (K.add ν ν) (K.mul h (K.mul (K.mul c c) (K.mul X X)))) (K.mul r r)) (K.mul (K.add ν ν) Z')
      = K.add (K.add (K.mul (K.add ν ν)
              (sub K (K.add (K.mul r r) (K.mul h (sub K (K.mul (K.mul (K.mul c X) r) s) (K.mul ν (K.mul s s))))) Z'))
            (K.mul h (K.mul (sub K (K.mul ν s) (K.mul (K.mul c X) r)) (sub K (K.mul ν s) (K.mul (K.mul c X) r)))))
          (K.mul h (K.mul (K.mul ν s) (K.mul ν s))) by lib_ring]
  exact P.nn_add _ _ (P.nn_add _ _ (P.nn_mul _ _ (P.nn_add _ _ hν hν) step) (P.nn_mul _ _ hh (P.nn_sq _)))
    (P.nn_mul _ _ hh (P.nn_sq _))

end YoungStep

#print axioms YoungStep.young_step

ns/ZeroExt.lean

lean_all/ns/ZeroExt.lean

/-
  ns/ZeroExt.lean — T37 (goal G795b-j, piece a): a Galerkin sequence on a sublist L (steps only for r ∈ L) extended
  by zero outside L, with a membership test memb (memb r = true ↔ r ∈ L, a hypothesis, no decidability axiom):
    nl_congr    the nonlinear force on L depends only on the values on L,
    step_ext    the zero extension B′ satisfies the Galerkin step at every mode (outside L: 0 = 0 + h·0),
    zs_ext      Z_L(B′_t) = Z_L(B_t),   as_ext   A_L(B′_t) = A_L(B_t),
  so the results stated for steps at every mode (H2LargeLe, WindowEnergy) apply to the small truncation.

  Lean 4 core plus ns/LargeCauchy.
-/
import ns.LargeCauchy

namespace ZeroExt
open DetChain NSChain DyadicSmallGlobal LibRing DyadicLarge FourierGalerkin GalerkinForce ForceBound ForceSob
  ModeCount CubeCount ForceUniform NormCompare EnstStep EnstGlobal EnstGalerkin ForceTwoScale ForceBilin EnstDiff
  ForceDiff TruncError TruncBound RestrStep GronwallSource EnstGronwall WeightSplit LatticeSum TruncCauchy TruncRate

section Gen
variable {R : Type} (K : CRing R) {I : Type}

def z3 : V3 R := ⟨K.zero, K.zero, K.zero⟩

/-- the zero extension of B outside L -/
def ext (memb : I → Bool) (B : Nat → I → V3 R) : Nat → I → V3 R := fun t r => cond (memb r) (B t r) (z3 K)

theorem comp_congr (L : List I) (δ : I → I → I → R) (ι : R) (k u u' : I → V3 R) (r : I) (g : V3 R → R)
    (h : ∀ x, x ∈ L → u x = u' x) :
    lsum K (fun p => lsum K (fun q => K.mul (coef K δ ι k u p q r) (g (u q))) L) L
      = lsum K (fun p => lsum K (fun q => K.mul (coef K δ ι k u' p q r) (g (u' q))) L) L :=
  lsum_ext_on K L (fun p hp => lsum_ext_on K L (fun q hq => by
    show K.mul (K.mul (δ p q r) (K.mul ι (dot3 K (u p) (k q)))) (g (u q))
      = K.mul (K.mul (δ p q r) (K.mul ι (dot3 K (u' p) (k q)))) (g (u' q))
    rw [h p hp, h q hq]))

/-- **the force on L depends only on the values on L** -/
theorem nl_congr (L : List I) (δ : I → I → I → R) (ι : R) (k u u' : I → V3 R) (r : I)
    (h : ∀ x, x ∈ L → u x = u' x) : Nl K L δ ι k u u r = Nl K L δ ι k u' u' r := by
  have e1 : lsum K (fun p => lsum K (fun q => K.mul (coef K δ ι k u p q r) (u q).x) L) L
      = lsum K (fun p => lsum K (fun q => K.mul (coef K δ ι k u' p q r) (u' q).x) L) L :=
    comp_congr K L δ ι k u u' r (fun w => w.x) h
  have e2 : lsum K (fun p => lsum K (fun q => K.mul (coef K δ ι k u p q r) (u q).y) L) L
      = lsum K (fun p => lsum K (fun q => K.mul (coef K δ ι k u' p q r) (u' q).y) L) L :=
    comp_congr K L δ ι k u u' r (fun w => w.y) h
  have e3 : lsum K (fun p => lsum K (fun q => K.mul (coef K δ ι k u p q r) (u q).z) L) L
      = lsum K (fun p => lsum K (fun q => K.mul (coef K δ ι k u' p q r) (u' q).z) L) L :=
    comp_congr K L δ ι k u u' r (fun w => w.z) h
  show (⟨lsum K (fun p => lsum K (fun q => K.mul (coef K δ ι k u p q r) (u q).x) L) L,
      lsum K (fun p => lsum K (fun q => K.mul (coef K δ ι k u p q r) (u q).y) L) L,
      lsum K (fun p => lsum K (fun q => K.mul (coef K δ ι k u p q r) (u q).z) L) L⟩ : V3 R)
    = ⟨lsum K (fun p => lsum K (fun q => K.mul (coef K δ ι k u' p q r) (u' q).x) L) L,
      lsum K (fun p => lsum K (fun q => K.mul (coef K δ ι k u' p q r) (u' q).y) L) L,
      lsum K (fun p => lsum K (fun q => K.mul (coef K δ ι k u' p q r) (u' q).z) L) L⟩
  rw [e1, e2, e3]

theorem ext_on (memb : I → Bool) (L : List I) (hm : ∀ r, r ∈ L → memb r = true) (B : Nat → I → V3 R) (t : Nat) :
    ∀ x, x ∈ L → ext K memb B t x = B t x := fun x hx => by
  show cond (memb x) (B t x) (z3 K) = B t x
  exact (congrArg (fun b => cond b (B t x) (z3 K)) (hm x hx)).trans rfl

/-- **the zero extension steps at every mode** -/
theorem step_ext (memb : I → Bool) (L : List I) (hm : ∀ r, r ∈ L → memb r = true)
    (hm' : ∀ r, memb r = true → r ∈ L) (kz : I → Mode) (h ν ι : R) (B : Nat → I → V3 R)
    (hB : ∀ t r, r ∈ L → Step K h ν (wt K kz r) (B (t + 1) r) (B t r)
      (Nl K L (TriadSparse.delta K kz) ι (kv K kz) (B t) (B t) r)) (t : Nat) (r : I) :
    Step K h ν (wt K kz r) (ext K memb B (t + 1) r) (ext K memb B t r)
      (cond (memb r) (Nl K L (TriadSparse.delta K kz) ι (kv K kz) (ext K memb B t) (ext K memb B t) r) (z3 K)) := by
  show Step K h ν (wt K kz r) (cond (memb r) (B (t + 1) r) (z3 K)) (cond (memb r) (B t r) (z3 K))
    (cond (memb r) (Nl K L (TriadSparse.delta K kz) ι (kv K kz) (ext K memb B t) (ext K memb B t) r) (z3 K))
  have hN := nl_congr K L (TriadSparse.delta K kz) ι (kv K kz) (ext K memb B t) (B t) r (ext_on K memb L hm B t)
  cases hr : memb r with
  | true =>
    show Step K h ν (wt K kz r) (B (t + 1) r) (B t r)
      (Nl K L (TriadSparse.delta K kz) ι (kv K kz) (ext K memb B t) (ext K memb B t) r)
    rw [hN]
    exact hB t r (hm' r hr)
  | false =>
    exact ⟨by show K.mul (K.add K.one (K.mul (K.mul h ν) (wt K kz r))) K.zero = K.add K.zero (K.mul h K.zero); lib_ring,
      by show K.mul (K.add K.one (K.mul (K.mul h ν) (wt K kz r))) K.zero = K.add K.zero (K.mul h K.zero); lib_ring,
      by show K.mul (K.add K.one (K.mul (K.mul h ν) (wt K kz r))) K.zero = K.add K.zero (K.mul h K.zero); lib_ring⟩

end Gen

end ZeroExt

#print axioms ZeroExt.comp_congr
#print axioms ZeroExt.nl_congr
#print axioms ZeroExt.ext_on
#print axioms ZeroExt.step_ext

ns/ZeroExtB.lean

lean_all/ns/ZeroExtB.lean

/-
  ns/ZeroExtB.lean — T37 (goal G795b-j, piece b): the energies over L of the zero extension equal those of the
  original sequence:
    zl_ext   Z_L(B′_t) = Z_L(B_t),   al_ext   A_L(B′_t) = A_L(B_t),   bl_ext   Σ_L w³|B′_t|² = Σ_L w³|B_t|²,
  so the window hypotheses for the small truncation (hEB of WindowShift.large_chain) can be read off from the
  results for B′ (which steps at every mode, ZeroExt.step_ext).

  Lean 4 core plus ns/ZeroExt.
-/
import ns.ZeroExt

namespace ZeroExtB
open DetChain NSChain DyadicSmallGlobal LibRing DyadicLarge FourierGalerkin GalerkinForce ForceBound ForceSob
  ModeCount CubeCount ForceUniform NormCompare EnstStep EnstGlobal EnstGalerkin ForceTwoScale ForceBilin EnstDiff
  ForceDiff TruncError TruncBound RestrStep GronwallSource EnstGronwall WeightSplit LatticeSum TruncCauchy TruncRate
  ZeroExt

section Gen
variable {R : Type} (K : CRing R) {I : Type}

theorem weighted_ext (memb : I → Bool) (L : List I) (hm : ∀ r, r ∈ L → memb r = true) (B : Nat → I → V3 R)
    (t : Nat) (w : I → R) :
    lsum K (fun p => K.mul (w p) (dot3 K (ext K memb B t p) (ext K memb B t p))) L
      = lsum K (fun p => K.mul (w p) (dot3 K (B t p) (B t p))) L :=
  lsum_ext_on K L (fun p hp => by
    show K.mul (w p) (dot3 K (ext K memb B t p) (ext K memb B t p)) = K.mul (w p) (dot3 K (B t p) (B t p))
    rw [ext_on K memb L hm B t p hp])

theorem zl_ext (memb : I → Bool) (L : List I) (hm : ∀ r, r ∈ L → memb r = true) (B : Nat → I → V3 R)
    (kz : I → Mode) (t : Nat) : Zl K kz L (ext K memb B t) = Zl K kz L (B t) :=
  weighted_ext K memb L hm B t (wt K kz)

theorem al_ext (memb : I → Bool) (L : List I) (hm : ∀ r, r ∈ L → memb r = true) (B : Nat → I → V3 R)
    (kz : I → Mode) (t : Nat) : Al K kz L (ext K memb B t) = Al K kz L (B t) :=
  weighted_ext K memb L hm B t (fun p => K.mul (wt K kz p) (wt K kz p))

theorem bl_ext (memb : I → Bool) (L : List I) (hm : ∀ r, r ∈ L → memb r = true) (B : Nat → I → V3 R)
    (kz : I → Mode) (t : Nat) :
    lsum K (fun p => K.mul (K.mul (K.mul (wt K kz p) (wt K kz p)) (wt K kz p)) (dot3 K (ext K memb B t p) (ext K memb B t p))) L
      = lsum K (fun p => K.mul (K.mul (K.mul (wt K kz p) (wt K kz p)) (wt K kz p)) (dot3 K (B t p) (B t p))) L :=
  weighted_ext K memb L hm B t (fun p => K.mul (K.mul (wt K kz p) (wt K kz p)) (wt K kz p))

end Gen

end ZeroExtB

#print axioms ZeroExtB.weighted_ext
#print axioms ZeroExtB.zl_ext
#print axioms ZeroExtB.al_ext
#print axioms ZeroExtB.bl_ext

tools/RingNorm.lean

lean_all/tools/RingNorm.lean

/-
  tools/RingNorm.lean — a reflective normaliser for commutative-semiring identities over a CRing,
  in Lean 4 core, with its soundness proof (no axioms). Infrastructure for T37/T39/T41 (cycle 36).

  An expression `PE` (variables, 1, +, ·) evaluates in any `CRing` under an assignment ρ. Its normal
  form is a sorted list of sorted monomials (a monomial is a list of variable indices; repetition is
  the coefficient). If two expressions have the same normal form (checked by `rfl`), they evaluate to
  the same element of every commutative ring:
      norm_sound : nf a = nf b → eval ρ a = eval ρ b.
  Identities with subtraction are used in the form A + C = B + D.
-/
import DetChain

namespace RingNorm
open DetChain

inductive PE where
  | var : Nat → PE
  | one : PE
  | add : PE → PE → PE
  | mul : PE → PE → PE

section Ring
variable {R : Type} (K : CRing R)

def eval (ρ : Nat → R) : PE → R
  | .var i => ρ i
  | .one => K.one
  | .add a b => K.add (eval ρ a) (eval ρ b)
  | .mul a b => K.mul (eval ρ a) (eval ρ b)

def evalM (ρ : Nat → R) : List Nat → R
  | [] => K.one
  | i :: m => K.mul (ρ i) (evalM ρ m)

def evalP (ρ : Nat → R) : List (List Nat) → R
  | [] => K.zero
  | m :: p => K.add (evalM K ρ m) (evalP ρ p)

/-! ## Sorting -/

def insN (x : Nat) : List Nat → List Nat
  | [] => [x]
  | y :: l => cond (Nat.ble x y) (x :: y :: l) (y :: insN x l)

def sortN : List Nat → List Nat
  | [] => []
  | x :: l => insN x (sortN l)

def leL : List Nat → List Nat → Bool
  | [], _ => true
  | _ :: _, [] => false
  | x :: l, y :: m => Nat.blt x y || (Nat.beq x y && leL l m)

def insM (x : List Nat) : List (List Nat) → List (List Nat)
  | [] => [x]
  | y :: l => cond (leL x y) (x :: y :: l) (y :: insM x l)

def sortP : List (List Nat) → List (List Nat)
  | [] => []
  | x :: l => insM x (sortP l)

/-! ## Normal form -/

def mulMono (m : List Nat) (q : List (List Nat)) : List (List Nat) := q.map (fun n => sortN (m ++ n))

def mulP : List (List Nat) → List (List Nat) → List (List Nat)
  | [], _ => []
  | m :: p, q => mulMono m q ++ mulP p q

def nf0 : PE → List (List Nat)
  | .var i => [[i]]
  | .one => [[]]
  | .add a b => nf0 a ++ nf0 b
  | .mul a b => mulP (nf0 a) (nf0 b)

def nf (e : PE) : List (List Nat) := sortP (nf0 e)

/-! ## Soundness -/

theorem add_left_comm' (a b c : R) : K.add a (K.add b c) = K.add b (K.add a c) := by
  rw [← K.add_assoc, K.add_comm a b, K.add_assoc]

theorem mul_left_comm' (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]

theorem evalM_insN (ρ : Nat → R) (x : Nat) : ∀ m, evalM K ρ (insN x m) = K.mul (ρ x) (evalM K ρ m)
  | [] => rfl
  | y :: l => by
    unfold insN
    cases Nat.ble x y with
    | true => rfl
    | false =>
      show K.mul (ρ y) (evalM K ρ (insN x l)) = K.mul (ρ x) (K.mul (ρ y) (evalM K ρ l))
      rw [evalM_insN ρ x l, mul_left_comm']

theorem evalM_sortN (ρ : Nat → R) : ∀ m, evalM K ρ (sortN m) = evalM K ρ m
  | [] => rfl
  | x :: l => by
    show evalM K ρ (insN x (sortN l)) = K.mul (ρ x) (evalM K ρ l)
    rw [evalM_insN, evalM_sortN ρ l]

theorem evalM_append (ρ : Nat → R) : ∀ m n, evalM K ρ (m ++ n) = K.mul (evalM K ρ m) (evalM K ρ n)
  | [], n => (K.one_mul _).symm
  | x :: m, n => by
    show K.mul (ρ x) (evalM K ρ (m ++ n)) = K.mul (K.mul (ρ x) (evalM K ρ m)) (evalM K ρ n)
    rw [evalM_append ρ m n, K.mul_assoc]

theorem evalP_append (ρ : Nat → R) : ∀ p q, evalP K ρ (p ++ q) = K.add (evalP K ρ p) (evalP K ρ q)
  | [], q => (K.zero_add _).symm
  | m :: p, q => by
    show K.add (evalM K ρ m) (evalP K ρ (p ++ q)) = K.add (K.add (evalM K ρ m) (evalP K ρ p)) (evalP K ρ q)
    rw [evalP_append ρ p q, K.add_assoc]

theorem evalP_insM (ρ : Nat → R) (x : List Nat) : ∀ p, evalP K ρ (insM x p) = K.add (evalM K ρ x) (evalP K ρ p)
  | [] => rfl
  | y :: l => by
    unfold insM
    cases leL x y with
    | true => rfl
    | false =>
      show K.add (evalM K ρ y) (evalP K ρ (insM x l)) = K.add (evalM K ρ x) (K.add (evalM K ρ y) (evalP K ρ l))
      rw [evalP_insM ρ x l, add_left_comm']

theorem evalP_sortP (ρ : Nat → R) : ∀ p, evalP K ρ (sortP p) = evalP K ρ p
  | [] => rfl
  | x :: l => by
    show evalP K ρ (insM x (sortP l)) = K.add (evalM K ρ x) (evalP K ρ l)
    rw [evalP_insM, evalP_sortP ρ l]

theorem evalP_mulMono (ρ : Nat → R) (m : List Nat) : ∀ q,
    evalP K ρ (mulMono m q) = K.mul (evalM K ρ m) (evalP K ρ q)
  | [] => (mul_zero K _).symm
  | n :: q => by
    show K.add (evalM K ρ (sortN (m ++ n))) (evalP K ρ (mulMono m q))
      = K.mul (evalM K ρ m) (K.add (evalM K ρ n) (evalP K ρ q))
    rw [evalM_sortN, evalM_append, evalP_mulMono ρ m q, K.distrib]

theorem evalP_mulP (ρ : Nat → R) : ∀ p q, evalP K ρ (mulP p q) = K.mul (evalP K ρ p) (evalP K ρ q)
  | [], q => (zero_mul' K _).symm
  | m :: p, q => by
    show evalP K ρ (mulMono m q ++ mulP p q) = K.mul (K.add (evalM K ρ m) (evalP K ρ p)) (evalP K ρ q)
    rw [evalP_append, evalP_mulMono, evalP_mulP ρ p q, right_distrib K]

theorem eval_nf0 (ρ : Nat → R) : ∀ e, eval K ρ e = evalP K ρ (nf0 e)
  | .var i => by show ρ i = K.add (K.mul (ρ i) K.one) K.zero; rw [add_zero', K.mul_comm, K.one_mul]
  | .one => by show K.one = K.add K.one K.zero; rw [add_zero']
  | .add a b => by
    show K.add (eval K ρ a) (eval K ρ b) = evalP K ρ (nf0 a ++ nf0 b)
    rw [evalP_append, eval_nf0 ρ a, eval_nf0 ρ b]
  | .mul a b => by
    show K.mul (eval K ρ a) (eval K ρ b) = evalP K ρ (mulP (nf0 a) (nf0 b))
    rw [evalP_mulP, eval_nf0 ρ a, eval_nf0 ρ b]

/-- **Soundness.** Equal normal forms ⇒ equal values in every commutative ring. -/
theorem norm_sound (ρ : Nat → R) (a b : PE) (h : nf a = nf b) : eval K ρ a = eval K ρ b := by
  rw [eval_nf0, eval_nf0, ← evalP_sortP K ρ (nf0 a), ← evalP_sortP K ρ (nf0 b)]
  show evalP K ρ (nf a) = evalP K ρ (nf b)
  rw [h]

end Ring

/-- Test: (x + y)(x + y) = x x + (x y + x y) + y y. -/
example {R : Type} (K : CRing R) (x y : R) :
    K.mul (K.add x y) (K.add x y) = K.add (K.add (K.mul x x) (K.add (K.mul x y) (K.mul x y))) (K.mul y y) :=
  norm_sound K (fun i => if i = 0 then x else y)
    (.mul (.add (.var 0) (.var 1)) (.add (.var 0) (.var 1)))
    (.add (.add (.mul (.var 0) (.var 0)) (.add (.mul (.var 0) (.var 1)) (.mul (.var 0) (.var 1)))) (.mul (.var 1) (.var 1)))
    rfl

end RingNorm

#print axioms RingNorm.norm_sound

tools/lincert_template.lean

lean_all/tools/lincert_template.lean

/-
  hodge/LinCert@[email protected] — T41 cycle 1868 (goals G861, G769): F_@P@ (m = @M@, the composite member of the 1 + 14k family)
  is indecomposable, by a linear certificate over all units instead of subgroup averages.
  A balanced part A satisfies 2·T_t(A) = |A|·m for every unit t. With integer weights λ_t = lp − ln on 31 units the
  combination Σ λ_t (2⟨t·x⟩ − m) equals L·[x = c_k] − α₁[x = s₁] − α₂[x = s₂] on every element x of c (checked by
  rfl as PT x + Em x = NT x + Ep x), so the counts n_x of A satisfy L·n_k + α₁⁻n₁ + α₂⁻n₂ = α₁⁺n₁ + α₂⁺n₂
  (row_eq). The kernel of the unit matrix is span(1, v) (rank 31 of 33): the counts of the two free elements s₁, s₂
  decide all others, the two mixed cases are impossible and the two pure ones give A = ∅ or B = ∅.
    lin          gs PT A = gs NT A for balanced A
    row_eq       the count relation of each certificate row
    lincert      indec @M@ F@P@ = true
  The weights were found by exact Gaussian elimination (tools/g861_cert.py, computation only).
  Lean 4 core plus hodge/AvgIndec.
-/
import hodge.AvgIndec

namespace LinCert@P@
open GcdHand ModHand OrbitSum APPairGen PermSum FamMod FamArith Indecomp FamBal FamIndec3 Q4Conv QFamPrime AvgIndec

def F@P@ : List Nat := [@C@]

structure Row where
  ck : Nat
  L : Nat
  a1p : Nat
  a1n : Nat
  a2p : Nat
  a2n : Nat
  T : List (Nat × Nat × Nat)

def ind (s x : Nat) : Nat := if Nat.beq x s = true then 1 else 0

def cnt (s : Nat) (A : List Nat) : Nat := gs (ind s) A

def PT (m : Nat) : List (Nat × Nat × Nat) → Nat → Nat
  | [], _ => 0
  | (t, lp, ln) :: T, x => (lp * ((t * x) % m + (t * x) % m) + ln * m) + PT m T x

def NT (m : Nat) : List (Nat × Nat × Nat) → Nat → Nat
  | [], _ => 0
  | (t, lp, ln) :: T, x => (ln * ((t * x) % m + (t * x) % m) + lp * m) + NT m T x

def Em (s1 s2 : Nat) (r : Row) (x : Nat) : Nat := r.a1p * ind s1 x + r.a2p * ind s2 x

def Ep (s1 s2 : Nat) (r : Row) (x : Nat) : Nat := r.L * ind r.ck x + (r.a1n * ind s1 x + r.a2n * ind s2 x)

def rowOK (m s1 s2 : Nat) (c : List Nat) (r : Row) : Bool :=
  r.T.all (fun e => Nat.blt e.1 m && Nat.beq (Nat.gcd e.1 m) 1) &&
  c.all (fun x => Nat.beq (PT m r.T x + Em s1 s2 r x) (NT m r.T x + Ep s1 s2 r x))

def allows (r : Row) (n1 n2 nk : Nat) : Bool :=
  Nat.beq (r.a1p * n1 + r.a2p * n2) (r.L * nk + (r.a1n * n1 + r.a2n * n2))

theorem add_left_cancel' (n a b : Nat) (h : n + a = n + b) : a = b :=
  add_right_cancel' a b n (by rw [Nat.add_comm a n, Nat.add_comm b n]; exact h)

theorem and_tt {a b : Bool} (h : (a && b) = true) : a = true ∧ b = true := by
  cases a with
  | false => exact nomatch h
  | true => exact ⟨rfl, h⟩

theorem gs_lin (lp ln m : Nat) (g : Nat → Nat) : ∀ A : List Nat,
    gs (fun x => lp * (g x + g x) + ln * m) A = lp * (gs g A + gs g A) + ln * (m * A.length)
  | [] => rfl
  | x :: A => by
    show (lp * (g x + g x) + ln * m) + gs (fun x => lp * (g x + g x) + ln * m) A
      = lp * ((g x + gs g A) + (g x + gs g A)) + ln * (m * (A.length + 1))
    rw [gs_lin lp ln m g A]; lib_nat

theorem gs_mul (w : Nat) (g : Nat → Nat) : ∀ A : List Nat, gs (fun x => w * g x) A = w * gs g A
  | [] => rfl
  | x :: A => by
    show w * g x + gs (fun x => w * g x) A = w * (g x + gs g A)
    rw [gs_mul w g A]; lib_nat

theorem gs_congr (f g : Nat → Nat) : ∀ A : List Nat, (∀ x, x ∈ A → f x = g x) → gs f A = gs g A
  | [], _ => rfl
  | x :: A, h => by
    show f x + gs f A = g x + gs g A
    rw [h x (List.Mem.head _), gs_congr f g A (fun y hy => h y (List.Mem.tail _ hy))]

theorem lin (m : Nat) (A : List Nat) : ∀ T : List (Nat × Nat × Nat),
    (∀ e, e ∈ T → tsum m e.1 A + tsum m e.1 A = A.length * m) → gs (PT m T) A = gs (NT m T) A
  | [], _ => by
    show gs (fun _ => 0) A = gs (fun _ => 0) A; rfl
  | (t, lp, ln) :: T, h => by
    have e1 : gs (PT m ((t, lp, ln) :: T)) A
        = gs (fun x => lp * ((t * x) % m + (t * x) % m) + ln * m) A + gs (PT m T) A :=
      gs_addf (fun x => lp * ((t * x) % m + (t * x) % m) + ln * m) (PT m T) A
    have e2 : gs (NT m ((t, lp, ln) :: T)) A
        = gs (fun x => ln * ((t * x) % m + (t * x) % m) + lp * m) A + gs (NT m T) A :=
      gs_addf (fun x => ln * ((t * x) % m + (t * x) % m) + lp * m) (NT m T) A
    have h0 := h (t, lp, ln) (List.Mem.head _)
    have ih := lin m A T (fun e he => h e (List.Mem.tail _ he))
    rw [e1, e2, gs_lin lp ln m (fun x => (t * x) % m) A, gs_lin ln lp m (fun x => (t * x) % m) A, ih]
    rw [tsum_gs] at h0
    show lp * (gs (fun x => (t * x) % m) A + gs (fun x => (t * x) % m) A) + ln * (m * A.length) + gs (NT m T) A
      = ln * (gs (fun x => (t * x) % m) A + gs (fun x => (t * x) % m) A) + lp * (m * A.length) + gs (NT m T) A
    rw [h0]; lib_nat

theorem mem_of_cnt (x : Nat) : ∀ l : List Nat, 0 < cnt x l → x ∈ l
  | [], h => nomatch h
  | y :: l, h => by
    cases hb : Nat.beq y x with
    | true => rw [Nat.eq_of_beq_eq_true hb]; exact List.Mem.head _
    | false =>
      have e : cnt x (y :: l) = cnt x l := by
        show ind x y + cnt x l = cnt x l
        unfold ind; rw [hb]; show 0 + cnt x l = cnt x l; lib_nat
      rw [e] at h
      exact List.Mem.tail _ (mem_of_cnt x l h)

theorem cnt_pos (x : Nat) : ∀ l : List Nat, x ∈ l → 0 < cnt x l
  | [], h => nomatch h
  | y :: l, h => by
    show 0 < ind x y + cnt x l
    cases h with
    | head => unfold ind; rw [beq_refl' x]; exact Nat.lt_of_lt_of_le (Nat.lt_succ_self 0) (Nat.le_add_right 1 _)
    | tail _ h' => exact Nat.lt_of_lt_of_le (cnt_pos x l h') (Nat.le_add_left _ _)

theorem mem_split (c A B : List Nat) (hs : (A, B) ∈ splits c) (x : Nat) (hx : x ∈ A) : x ∈ c := by
  have e := FamIndec3.splits_gs (ind x) c A B hs
  exact mem_of_cnt x c (Nat.lt_of_lt_of_le (cnt_pos x A hx) (by rw [show cnt x c = gs (ind x) A + gs (ind x) B from e.symm]; exact Nat.le_add_right _ _))

theorem mem_split_r (c A B : List Nat) (hs : (A, B) ∈ splits c) (x : Nat) (hx : x ∈ B) : x ∈ c := by
  have e := FamIndec3.splits_gs (ind x) c A B hs
  exact mem_of_cnt x c (Nat.lt_of_lt_of_le (cnt_pos x B hx) (by rw [show cnt x c = gs (ind x) A + gs (ind x) B from e.symm]; exact Nat.le_add_left _ _))

theorem all_mem2 {α : Type} (P : α → Bool) (l : List α) (h : l.all P = true) (r : α) (hr : r ∈ l) : P r = true :=
  all_mem' P l h r hr

theorem row_eq (m s1 s2 : Nat) (c A B : List Nat) (hs : (A, B) ∈ splits c) (hb : bal m A = true) (r : Row)
    (hr : rowOK m s1 s2 c r = true) : allows r (cnt s1 A) (cnt s2 A) (cnt r.ck A) = true := by
  have h2 := and_tt hr
  have hT : ∀ e, e ∈ r.T → tsum m e.1 A + tsum m e.1 A = A.length * m := by
    intro e he
    have u := and_tt (all_mem2 _ r.T h2.1 e he)
    exact bal_unit m e.1 A hb (Nat.le_of_ble_eq_true u.1) (Nat.eq_of_beq_eq_true u.2)
  have hl := lin m A r.T hT
  have hc : ∀ x, x ∈ A → PT m r.T x + Em s1 s2 r x = NT m r.T x + Ep s1 s2 r x := fun x hx =>
    Nat.eq_of_beq_eq_true (all_mem2 _ c h2.2 x (mem_split c A B hs x hx))
  have g := gs_congr _ _ A hc
  rw [gs_addf, gs_addf, hl] at g
  have g2 := add_left_cancel' _ _ _ g
  have eEm : gs (Em s1 s2 r) A = r.a1p * cnt s1 A + r.a2p * cnt s2 A := by
    show gs (fun x => r.a1p * ind s1 x + r.a2p * ind s2 x) A = _
    rw [gs_addf, gs_mul, gs_mul]; rfl
  have eEp : gs (Ep s1 s2 r) A = r.L * cnt r.ck A + (r.a1n * cnt s1 A + r.a2n * cnt s2 A) := by
    show gs (fun x => r.L * ind r.ck x + (r.a1n * ind s1 x + r.a2n * ind s2 x)) A = _
    rw [gs_addf, gs_addf, gs_mul, gs_mul, gs_mul]; rfl
  rw [eEm, eEp] at g2
  unfold allows; rw [g2]; exact beq_refl' _

theorem zero_or_one : ∀ a b : Nat, a + b = 1 → a = 0 ∨ a = 1
  | 0, _, _ => Or.inl rfl
  | 1, _, _ => Or.inr rfl
  | a + 2, b, h => by
    have h' : a + b + 2 = 1 := by rw [← h]; lib_nat
    exact Nat.noConfusion (Nat.succ.inj h')

theorem cnt01 (c A B : List Nat) (hs : (A, B) ∈ splits c) (x : Nat) (h1 : cnt x c = 1) : cnt x A = 0 ∨ cnt x A = 1 :=
  zero_or_one _ (cnt x B) ((FamIndec3.splits_gs (ind x) c A B hs).trans h1)

theorem any_mem {α : Type} (P : α → Bool) : ∀ l : List α, l.any P = true → ∃ r, r ∈ l ∧ P r = true
  | [], h => nomatch h
  | x :: l, h => by
    cases e : P x with
    | true => exact ⟨x, List.Mem.head _, e⟩
    | false =>
      rw [show (x :: l).any P = (P x || l.any P) from rfl, e] at h
      cases any_mem P l h with
      | intro r hr => exact ⟨r, List.Mem.tail _ hr.1, hr.2⟩

theorem mem_app {α : Type} (x : α) : ∀ l l' : List α, x ∈ l ++ l' → x ∈ l ∨ x ∈ l'
  | [], _, h => Or.inr h
  | y :: l, l', h => by
    cases h with
    | head => exact Or.inl (List.Mem.head _)
    | tail _ h' =>
      cases mem_app x l l' h' with
      | inl a => exact Or.inl (List.Mem.tail _ a)
      | inr b => exact Or.inr b

theorem mem_map' {α β : Type} (f : α → β) (y : β) : ∀ l : List α, y ∈ l.map f → ∃ r, r ∈ l ∧ f r = y
  | [], h => nomatch h
  | x :: l, h => by
    cases h with
    | head => exact ⟨x, List.Mem.head _, rfl⟩
    | tail _ h' =>
      cases mem_map' f y l h' with
      | intro r hr => exact ⟨r, List.Mem.tail _ hr.1, hr.2⟩

def rows : List Row := [
@ROWS@]

def S1 : Nat := @S1@
def S2 : Nat := @S2@

theorem rows_ok : rows.all (rowOK @M@ S1 S2 F@P@) = true := rfl
theorem c_split : F@P@ = rows.map (fun r => r.ck) ++ [S1, S2] := rfl
theorem c_once : F@[email protected] (fun x => Nat.beq (cnt x F@P@) 1) = true := rfl
theorem mixed10 : rows.any (fun r => !allows r 1 0 0 && !allows r 1 0 1) = true := rfl
theorem mixed01 : rows.any (fun r => !allows r 0 1 0 && !allows r 0 1 1) = true := rfl
theorem pure00 : rows.all (fun r => !allows r 0 0 1) = true := rfl
theorem pure11 : rows.all (fun r => !allows r 1 1 0) = true := rfl

theorem not_tt {b : Bool} (h : (!b) = true) (h' : b = true) : False := by
  rw [h'] at h; exact nomatch h

theorem mem_app_r {α : Type} (x : α) (l' : List α) (h : x ∈ l') : ∀ l : List α, x ∈ l ++ l'
  | [] => h
  | _ :: l => List.Mem.tail _ (mem_app_r x l' h l)

theorem mem_app_l {α : Type} (x : α) (l' : List α) : ∀ l : List α, x ∈ l → x ∈ l ++ l'
  | [], h => nomatch h
  | _ :: l, h => by
    cases h with
    | head => exact List.Mem.head _
    | tail _ h' => exact List.Mem.tail _ (mem_app_l x l' l h')

theorem map_mem {α β : Type} (f : α → β) (r : α) : ∀ l : List α, r ∈ l → f r ∈ l.map f
  | [], h => nomatch h
  | _ :: l, h => by
    cases h with
    | head => exact List.Mem.head _
    | tail _ h' => exact List.Mem.tail _ (map_mem f r l h')

theorem ne_nil {α : Type} : ∀ l : List α, l.isEmpty = false → ∃ x, x ∈ l
  | [], h => nomatch h
  | x :: _, _ => ⟨x, List.Mem.head _⟩

theorem memS1 : S1 ∈ F@P@ := by rw [c_split]; exact mem_app_r S1 [S1, S2] (List.Mem.head _) _
theorem memS2 : S2 ∈ F@P@ := by rw [c_split]; exact mem_app_r S2 [S1, S2] (List.Mem.tail _ (List.Mem.head _)) _
theorem mem_ck (r : Row) (hr : r ∈ rows) : r.ck ∈ F@P@ := by
  rw [c_split]; exact mem_app_l r.ck [S1, S2] _ (map_mem (fun r => r.ck) r rows hr)

theorem key (x : Nat) (hx : x ∈ F@P@) : (∃ r, r ∈ rows ∧ r.ck = x) ∨ x = S1 ∨ x = S2 := by
  rw [c_split] at hx
  cases mem_app x _ _ hx with
  | inl h => exact Or.inl (mem_map' _ x rows h)
  | inr h =>
    cases h with
    | head => exact Or.inr (Or.inl rfl)
    | tail _ h' =>
      cases h' with
      | head => exact Or.inr (Or.inr rfl)
      | tail _ h'' => exact nomatch h''

theorem no_split (A B : List Nat) (hs : (A, B) ∈ splits F@P@) (hA : bal @M@ A = true)
    (hAne : A.isEmpty = false) (hBne : B.isEmpty = false) : False := by
  have R : ∀ r, r ∈ rows → allows r (cnt S1 A) (cnt S2 A) (cnt r.ck A) = true := fun r hr =>
    row_eq @M@ S1 S2 F@P@ A B hs hA r (all_mem2 _ rows rows_ok r hr)
  have once : ∀ x, x ∈ F@P@ → cnt x F@P@ = 1 := fun x hx => Nat.eq_of_beq_eq_true (all_mem2 _ F@P@ c_once x hx)
  have c01 : ∀ x, x ∈ F@P@ → cnt x A = 0 ∨ cnt x A = 1 := fun x hx => cnt01 F@P@ A B hs x (once x hx)
  cases c01 S1 memS1 with
  | inl a1 =>
    cases c01 S2 memS2 with
    | inl a2 =>
      cases ne_nil A hAne with
      | intro x hxA =>
        have hxF := mem_split F@P@ A B hs x hxA
        have pos := cnt_pos x A hxA
        cases key x hxF with
        | inl hr =>
          cases hr with
          | intro r hr =>
            have q := R r hr.1
            rw [a1, a2, hr.2] at q
            have p00 := all_mem2 _ rows pure00 r hr.1
            cases c01 x hxF with
            | inl z => rw [z] at pos; exact Nat.lt_irrefl 0 pos
            | inr o => rw [o] at q; exact not_tt p00 q
        | inr h =>
          cases h with
          | inl e => rw [e, a1] at pos; exact Nat.lt_irrefl 0 pos
          | inr e => rw [e, a2] at pos; exact Nat.lt_irrefl 0 pos
    | inr b2 =>
      cases any_mem _ rows mixed01 with
      | intro r hr =>
        have q := R r hr.1
        rw [a1, b2] at q
        have hb := and_tt hr.2
        cases c01 r.ck (mem_ck r hr.1) with
        | inl z => rw [z] at q; exact not_tt hb.1 q
        | inr o => rw [o] at q; exact not_tt hb.2 q
  | inr b1 =>
    cases c01 S2 memS2 with
    | inl a2 =>
      cases any_mem _ rows mixed10 with
      | intro r hr =>
        have q := R r hr.1
        rw [b1, a2] at q
        have hb := and_tt hr.2
        cases c01 r.ck (mem_ck r hr.1) with
        | inl z => rw [z] at q; exact not_tt hb.1 q
        | inr o => rw [o] at q; exact not_tt hb.2 q
    | inr b2 =>
      cases ne_nil B hBne with
      | intro y hyB =>
        have hyF := mem_split_r F@P@ A B hs y hyB
        have posB := cnt_pos y B hyB
        have sum : cnt y A + cnt y B = 1 := (FamIndec3.splits_gs (ind y) F@P@ A B hs).trans (once y hyF)
        have hyA1 : cnt y A = 1 := by
          cases key y hyF with
          | inl hr =>
            cases hr with
            | intro r hr =>
              have q := R r hr.1
              rw [b1, b2, hr.2] at q
              cases c01 y hyF with
              | inl z => rw [z] at q; exact (not_tt (all_mem2 _ rows pure11 r hr.1) q).elim
              | inr o => exact o
          | inr h =>
            cases h with
            | inl e => rw [e]; exact b1
            | inr e => rw [e]; exact b2
        rw [hyA1] at sum
        have z : cnt y B = 0 := add_left_cancel' 1 _ 0 (sum.trans rfl : 1 + cnt y B = 1 + 0)
        rw [z] at posB
        exact Nat.lt_irrefl 0 posB

theorem lincert : indec @M@ F@P@ = true := by
  cases hind : indec @M@ F@P@ with
  | true => rfl
  | false =>
    exfalso
    cases all_false _ (splits F@P@) hind with
    | intro sp hsp =>
      have hf : (sp.1.isEmpty || sp.2.isEmpty || !(bal @M@ sp.1 && bal @M@ sp.2)) = false := hsp.2
      cases h1 : sp.1.isEmpty with
      | true => rw [h1] at hf; exact nomatch hf
      | false => cases h2 : sp.2.isEmpty with
        | true => rw [h1, h2] at hf; exact nomatch hf
        | false => cases hA : bal @M@ sp.1 with
          | false => rw [h1, h2, hA] at hf; exact nomatch hf
          | true => exact no_split sp.1 sp.2 hsp.1 hA h1 h2

end LinCert@P@

#print axioms LinCert@[email protected]_lin
#print axioms LinCert@[email protected]
#print axioms LinCert@[email protected]_eq
#print axioms LinCert@[email protected]_ok
#print axioms LinCert@[email protected]_split
#print axioms LinCert@[email protected]

ym/A1.lean

lean_all/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

ym/A1C.lean

lean_all/ym/A1C.lean

/-
  ym/A1C — T45, G50 step 2: the first bond-moving coefficients of c = X·X in autocorrelations.
  With x = co X (length M+1 = L) and R_k = Σ_i x_i x_{i+k}:
    c₁ = 2R₁,  c₂ = 2R₂ + R₀ − x₀²,  c₃ = 2R₃ + 2R₁ − 2x₀x₁,
  via the reflected CG rows Ψ(j,t) (CgWindow.Psi_lo/hi) and A1Ip. Lean 4 core only, no axioms.
-/
import ym.A1Ip

namespace A1C

open YMChain QBinom CgWindow HeatPair A1Ip MigdalCone

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

theorem psi2 (X : List R) : ∀ j, Psi K X j 2 = p2v K (co K X) j
  | 0 => by
    have := Psi_lo K X 0 2; rw [Nat.zero_add] at this; rw [this]
    show K.add K.zero (co K X (2 + 2 * 0)) = co K X 2; rw [show 2 + 2 * 0 = 2 from rfl]; lib_ring
  | 1 => by
    have := Psi_lo K X 1 1; rw [show 1 + 1 = 2 from rfl] at this; rw [this]
    show K.add (K.add K.zero (co K X (1 + 2 * 0))) (co K X (1 + 2 * 1)) = K.add (K.add K.zero (co K X 1)) (co K X 3)
    rfl
  | d + 2 => by
    have := Psi_hi K X 2 d; rw [show 2 + d = d + 2 by lib_nat] at this; rw [this]
    show K.add (K.add (K.add K.zero (co K X (d + 2 * 0))) (co K X (d + 2 * 1))) (co K X (d + 2 * 2))
      = K.add (K.add (co K X d) (co K X (d + 1 + 1))) (co K X (d + 1 + 3))
    rw [show d + 2 * 0 = d by lib_nat, show d + 2 * 1 = d + 1 + 1 by lib_nat, show d + 2 * 2 = d + 1 + 3 by lib_nat]; lib_ring

/-- the reflected row of width 3 -/
def p3v (x : Nat → R) : Nat → R
  | 0 => x 3
  | 1 => K.add (x 2) (x 4)
  | 2 => K.add (K.add (x 1) (x 3)) (x 5)
  | d + 3 => K.add (K.add (K.add (x d) (x (d + 2))) (x (d + 4))) (x (d + 6))

theorem psi3 (X : List R) : ∀ j, Psi K X j 3 = p3v K (co K X) j
  | 0 => by
    have := Psi_lo K X 0 3; rw [Nat.zero_add] at this; rw [this]
    show K.add K.zero (co K X (3 + 2 * 0)) = co K X 3; rw [show 3 + 2 * 0 = 3 from rfl]; lib_ring
  | 1 => by
    have := Psi_lo K X 1 2; rw [show 1 + 2 = 3 from rfl] at this; rw [this]
    show K.add (K.add K.zero (co K X (2 + 2 * 0))) (co K X (2 + 2 * 1)) = K.add (co K X 2) (co K X 4)
    rw [show 2 + 2 * 0 = 2 from rfl, show 2 + 2 * 1 = 4 from rfl]; lib_ring
  | 2 => by
    have := Psi_lo K X 2 1; rw [show 2 + 1 = 3 from rfl] at this; rw [this]
    show K.add (K.add (K.add K.zero (co K X (1 + 2 * 0))) (co K X (1 + 2 * 1))) (co K X (1 + 2 * 2))
      = K.add (K.add (co K X 1) (co K X 3)) (co K X 5)
    rw [show 1 + 2 * 0 = 1 from rfl, show 1 + 2 * 1 = 3 from rfl, show 1 + 2 * 2 = 5 from rfl]; lib_ring
  | d + 3 => by
    have := Psi_hi K X 3 d; rw [show 3 + d = d + 3 by lib_nat] at this; rw [this]
    show K.add (K.add (K.add (K.add K.zero (co K X (d + 2 * 0))) (co K X (d + 2 * 1))) (co K X (d + 2 * 2))) (co K X (d + 2 * 3))
      = K.add (K.add (K.add (co K X d) (co K X (d + 2))) (co K X (d + 4))) (co K X (d + 6))
    rw [show d + 2 * 0 = d by lib_nat, show d + 2 * 1 = d + 2 by lib_nat, show d + 2 * 2 = d + 4 by lib_nat,
      show d + 2 * 3 = d + 6 by lib_nat]; lib_ring

section ceq

variable (x : Nat → R) (L : Nat) (hz : ∀ t, L ≤ t → x t = K.zero)

include hz in
theorem R3sh (k m : Nat) : ip K (fun i => x (i + 3)) (fun i => x (i + 3 + k)) (L + m)
    = K.add (Rk K x L k) (K.add (K.add (K.neg (K.mul (x 0) (x k))) (K.neg (K.mul (x 1) (x (1 + k))))) (K.neg (K.mul (x 2) (x (2 + k))))) := by
  have h := ip_head K (fun i => x (i + 2)) (fun i => x (i + 2 + k)) (L + m)
  rw [show L + m + 1 = L + (m + 1) from rfl, R2sh K x L hz] at h
  have h' : K.add (Rk K x L k) (K.add (K.neg (K.mul (x 0) (x k))) (K.neg (K.mul (x 1) (x (1 + k)))))
      = K.add (K.mul (x 2) (x (2 + k))) (ip K (fun i => x (i + 3)) (fun i => x (i + 3 + k)) (L + m)) := by
    rw [h]
  rw [show ip K (fun i => x (i + 3)) (fun i => x (i + 3 + k)) (L + m)
      = K.add (K.add (Rk K x L k) (K.add (K.neg (K.mul (x 0) (x k))) (K.neg (K.mul (x 1) (x (1 + k)))))) (K.neg (K.mul (x 2) (x (2 + k)))) by
        rw [h']; lib_ring]
  lib_ring

include hz in
theorem ip_p3 : ip K x (p3v K x) (L + 4)
    = 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))))) := by
  show ip K x (p3v K x) (L + 1 + 1 + 1 + 1) = _
  rw [ip_head, ip_head, ip_head]
  rw [ip_ext K (fun i => x (i + 1 + 1 + 1)) (fun i => x (i + 3)) (fun i => p3v K x (i + 1 + 1 + 1))
      (fun d => K.add (K.add (K.add (x d) (x (d + 2))) (x (d + 4))) (x (d + 6))) (fun i => rfl) (fun i => rfl),
    ip_add_r, ip_add_r, ip_add_r]
  have a : ip K (fun i => x (i + 3)) x (L + 1) = Rk K x L 3 := by rw [ip_comm]; exact Rtail K x L hz 3 1
  have b : ip K (fun i => x (i + 3)) (fun i => x (i + 2)) (L + 1)
      = K.add (Rk K x L 1) (K.add (K.neg (K.mul (x 0) (x 1))) (K.neg (K.mul (x 1) (x 2)))) := by
    rw [ip_comm]; exact R2sh K x L hz 1 1
  have c : ip K (fun i => x (i + 3)) (fun i => x (i + 4)) (L + 1)
      = K.add (Rk K x L 1) (K.add (K.add (K.neg (K.mul (x 0) (x 1))) (K.neg (K.mul (x 1) (x 2)))) (K.neg (K.mul (x 2) (x 3)))) :=
    R3sh K x L hz 1 1
  have d : ip K (fun i => x (i + 3)) (fun i => x (i + 6)) (L + 1)
      = K.add (Rk K x L 3) (K.add (K.add (K.neg (K.mul (x 0) (x 3))) (K.neg (K.mul (x 1) (x 4)))) (K.neg (K.mul (x 2) (x 5)))) :=
    R3sh K x L hz 3 1
  rw [a, b, c, d]
  show K.add (K.mul (x 0) (x 3)) (K.add (K.mul (x 1) (K.add (x 2) (x 4))) (K.add (K.mul (x 2) (K.add (K.add (x 1) (x 3)) (x 5))) _)) = _
  lib_ring

end ceq

theorem hzX (X : List R) (M : Nat) (hX : X.length = M + 1) : ∀ t, M + 1 ≤ t → co K X t = K.zero :=
  fun t ht => HeatLR.co_out K X t (by rw [hX]; exact ht)

theorem cgc_ip (X : List R) (M t : Nat) (hX : X.length = M + 1) (v : Nat → R) (hv : ∀ j, Psi K X j t = v j) (d : Nat) :
    cgc K X X t = ip K (co K X) v (M + 1 + d) := by
  rw [OneStep.cgc_sum K X X M t hX, ip_tail K _ _ (M + 1) (fun i hi => by rw [hzX K X M hX i hi]; lib_ring) d]
  exact sumN_ext K _ _ (fun j => by rw [hv]) _

theorem c1eq (X : List R) (M : Nat) (hX : X.length = M + 1) :
    cgc K X X 1 = K.add (Rk K (co K X) (M + 1) 1) (Rk K (co K X) (M + 1) 1) := by
  rw [cgc_ip K X M 1 hX (c1v K (co K X)) (fun j => by
    rw [LC0.psi1]; cases j with
    | zero => show co K X 1 = K.add K.zero (co K X 1); lib_ring
    | succ j => rfl) 3]
  have e := e9 K (co K X) (M + 1) (hzX K X M hX)
  rw [show M + 1 + 4 = M + 1 + 3 + 1 from rfl, ip_zz] at e
  exact e

theorem c2eq (X : List R) (M : Nat) (hX : X.length = M + 1) :
    cgc K X X 2 = K.add (K.add (Rk K (co K X) (M + 1) 2) (Rk K (co K X) (M + 1) 2))
      (K.add (Rk K (co K X) (M + 1) 0) (K.neg (K.mul (co K X 0) (co K X 0)))) := by
  rw [cgc_ip K X M 2 hX (p2v K (co K X)) (psi2 K X) 3]
  have e := e11 K (co K X) (M + 1) (hzX K X M hX)
  rw [show M + 1 + 4 = M + 1 + 3 + 1 from rfl, ip_zz] at e
  exact e

theorem c3eq (X : List R) (M : Nat) (hX : X.length = M + 1) :
    cgc K X X 3 = K.add (K.add (Rk K (co K X) (M + 1) 3) (Rk K (co K X) (M + 1) 3))
      (K.add (K.add (Rk K (co K X) (M + 1) 1) (Rk K (co K X) (M + 1) 1))
        (K.neg (K.add (K.mul (co K X 0) (co K X 1)) (K.mul (co K X 0) (co K X 1))))) := by
  rw [cgc_ip K X M 3 hX (p3v K (co K X)) (psi3 K X) 4]
  exact ip_p3 K (co K X) (M + 1) (hzX K X M hX)

end A1C

#print axioms A1C.psi2
#print axioms A1C.psi3
#print axioms A1C.R3sh
#print axioms A1C.ip_p3
#print axioms A1C.hzX
#print axioms A1C.cgc_ip
#print axioms A1C.c1eq
#print axioms A1C.c2eq
#print axioms A1C.c3eq

ym/A1Ip.lean

lean_all/ym/A1Ip.lean

/-
  ym/A1Ip — T45, G50 (A) at n = 1, step 1: inner products of shifted vectors as autocorrelations.
  For x : ℕ → R vanishing from L on, with R_k = Σ_{i<L+4} x_i x_{i+k}, the inner products over [0, L+4) of the
  vectors S⁻ᵃx (CgWindow.Zs), χ₁x = Zs x + x(·+1) and ψ₂x (the reflected CG row of width 2) reduce to R_0..R_3, x_0, x_1.
  Lean 4 core only, no axioms.
-/
import ym.LC0

namespace A1Ip

open YMChain QBinom CgWindow

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

def ip (f g : Nat → R) (N : Nat) : R := sumN K (fun i => K.mul (f i) (g i)) N

theorem ip_zz (f g : Nat → R) (n : Nat) : ip K (Zs K f) (Zs K g) (n + 1) = ip K f g n := by
  unfold ip; rw [sumN_shift]; show K.add (K.mul K.zero K.zero) _ = _; rw [show K.mul K.zero K.zero = K.zero by lib_ring, K.zero_add]; rfl

theorem ip_z1 (f g : Nat → R) (n : Nat) : ip K (Zs K f) g (n + 1) = ip K f (fun i => g (i + 1)) n := by
  unfold ip; rw [sumN_shift]; show K.add (K.mul K.zero (g 0)) _ = _; rw [show K.mul K.zero (g 0) = K.zero by lib_ring, K.zero_add]; rfl

theorem ip_1z (f g : Nat → R) (n : Nat) : ip K f (Zs K g) (n + 1) = ip K (fun i => f (i + 1)) g n := by
  unfold ip; rw [sumN_shift]; show K.add (K.mul (f 0) K.zero) _ = _; rw [show K.mul (f 0) K.zero = K.zero by lib_ring, K.zero_add]; rfl

theorem ip_head (f g : Nat → R) (n : Nat) : ip K f g (n + 1) = K.add (K.mul (f 0) (g 0)) (ip K (fun i => f (i + 1)) (fun i => g (i + 1)) n) := by
  unfold ip; rw [sumN_shift]

theorem ip_add_l (f f' g : Nat → R) (n : Nat) : ip K (fun i => K.add (f i) (f' i)) g n = K.add (ip K f g n) (ip K f' g n) := by
  unfold ip
  rw [sumN_ext K _ (fun i => K.add (K.mul (f i) (g i)) (K.mul (f' i) (g i)))
    (fun i => by show K.mul (K.add (f i) (f' i)) (g i) = K.add (K.mul (f i) (g i)) (K.mul (f' i) (g i)); lib_ring) n, sumN_add]

theorem ip_add_r (f g g' : Nat → R) (n : Nat) : ip K f (fun i => K.add (g i) (g' i)) n = K.add (ip K f g n) (ip K f g' n) := by
  unfold ip
  rw [sumN_ext K _ (fun i => K.add (K.mul (f i) (g i)) (K.mul (f i) (g' i)))
    (fun i => by show K.mul (f i) (K.add (g i) (g' i)) = K.add (K.mul (f i) (g i)) (K.mul (f i) (g' i)); lib_ring) n, sumN_add]

theorem ip_comm (f g : Nat → R) (n : Nat) : ip K f g n = ip K g f n := by
  unfold ip; exact sumN_ext K _ _ (fun i => by lib_ring) n

theorem ip_ext (f f' g g' : Nat → R) (hf : ∀ i, f i = f' i) (hg : ∀ i, g i = g' i) (n : Nat) : ip K f g n = ip K f' g' n := by
  unfold ip; exact sumN_ext K _ _ (fun i => by rw [hf, hg]) n

/-- a sum whose terms vanish from n on does not change when extended -/
theorem ip_tail (f g : Nat → R) (n : Nat) (h : ∀ i, n ≤ i → K.mul (f i) (g i) = K.zero) (d : Nat) : ip K f g (n + d) = ip K f g n := by
  unfold ip; exact LC0.sum_zero_tail K _ n h d

section closed

variable (x : Nat → R) (L : Nat) (hz : ∀ t, L ≤ t → x t = K.zero)

/-- autocorrelation R_k = Σ_{i<L} x_i x_{i+k} -/
def Rk (k : Nat) : R := ip K x (fun i => x (i + k)) L

include hz in
theorem Rtail (k m : Nat) : ip K x (fun i => x (i + k)) (L + m) = Rk K x L k :=
  ip_tail K _ _ L (fun i hi => by show K.mul (x i) (x (i + k)) = K.zero; rw [hz i hi]; lib_ring) m

include hz in
theorem R1sh (k m : Nat) : ip K (fun i => x (i + 1)) (fun i => x (i + 1 + k)) (L + m)
    = K.add (Rk K x L k) (K.neg (K.mul (x 0) (x k))) := by
  have h := ip_head K x (fun i => x (i + k)) (L + m)
  rw [show L + m + 1 = L + (m + 1) from rfl, Rtail K x L hz] at h
  rw [h, Nat.zero_add]
  lib_ring

def c1v : Nat → R := fun i => K.add (Zs K x i) (x (i + 1))
def p2v : Nat → R
  | 0 => x 2
  | i + 1 => K.add (K.add (Zs K x i) (x (i + 1))) (x (i + 3))

include hz in
theorem e1 : ip K (Zs K (Zs K (Zs K x))) (Zs K x) (L + 4) = Rk K x L 2 := by
  show ip K (Zs K (Zs K (Zs K x))) (Zs K x) (L + 3 + 1) = _
  rw [ip_zz]; show ip K (Zs K (Zs K x)) x (L + 2 + 1) = _
  rw [ip_z1]; show ip K (Zs K x) (fun i => x (i + 1)) (L + 1 + 1) = _
  rw [ip_z1]; exact Rtail K x L hz 2 1

include hz in
theorem e2 : ip K (Zs K (Zs K x)) x (L + 4) = Rk K x L 2 := by
  show ip K (Zs K (Zs K x)) x (L + 3 + 1) = _
  rw [ip_z1]; show ip K (Zs K x) (fun i => x (i + 1)) (L + 2 + 1) = _
  rw [ip_z1]; exact Rtail K x L hz 2 2

include hz in
theorem e3 : ip K (Zs K (Zs K (Zs K x))) x (L + 4) = Rk K x L 3 := by
  show ip K (Zs K (Zs K (Zs K x))) x (L + 3 + 1) = _
  rw [ip_z1]; show ip K (Zs K (Zs K x)) (fun i => x (i + 1)) (L + 2 + 1) = _
  rw [ip_z1]; show ip K (Zs K x) (fun i => x (i + 2)) (L + 1 + 1) = _
  rw [ip_z1]; exact Rtail K x L hz 3 1

include hz in
theorem e4 : ip K (Zs K (Zs K x)) (Zs K x) (L + 4) = Rk K x L 1 := by
  show ip K (Zs K (Zs K x)) (Zs K x) (L + 3 + 1) = _
  rw [ip_zz]; show ip K (Zs K x) x (L + 2 + 1) = _
  rw [ip_z1]; exact Rtail K x L hz 1 2

include hz in
theorem e5 : ip K (Zs K (c1v K x)) x (L + 4)
    = K.add (Rk K x L 2) (K.add (Rk K x L 0) (K.neg (K.mul (x 0) (x 0)))) := by
  show ip K (Zs K (c1v K x)) x (L + 3 + 1) = _
  rw [ip_z1]; unfold c1v
  rw [ip_add_l]
  have a : ip K (Zs K x) (fun i => x (i + 1)) (L + 3) = Rk K x L 2 := by
    show ip K (Zs K x) (fun i => x (i + 1)) (L + 2 + 1) = _; rw [ip_z1]; exact Rtail K x L hz 2 2
  have b := R1sh K x L hz 0 3
  rw [a, show ip K (fun i => x (i + 1)) (fun i => x (i + 1)) (L + 3) = ip K (fun i => x (i + 1)) (fun i => x (i + 1 + 0)) (L + 3) from rfl, b]

include hz in
theorem e6 : ip K (Zs K (c1v K x)) (Zs K x) (L + 4) = K.add (Rk K x L 1) (Rk K x L 1) := by
  show ip K (Zs K (c1v K x)) (Zs K x) (L + 3 + 1) = _
  rw [ip_zz]; unfold c1v; rw [ip_add_l]
  have a : ip K (Zs K x) x (L + 3) = Rk K x L 1 := by
    show ip K (Zs K x) x (L + 2 + 1) = _; rw [ip_z1]; exact Rtail K x L hz 1 2
  have b : ip K (fun i => x (i + 1)) x (L + 3) = Rk K x L 1 := by
    rw [ip_comm]; exact Rtail K x L hz 1 3
  rw [a, b]

include hz in
theorem R2sh (k m : Nat) : ip K (fun i => x (i + 2)) (fun i => x (i + 2 + k)) (L + m)
    = K.add (Rk K x L k) (K.add (K.neg (K.mul (x 0) (x k))) (K.neg (K.mul (x 1) (x (1 + k))))) := by
  have h := ip_head K (fun i => x (i + 1)) (fun i => x (i + 1 + k)) (L + m)
  rw [show L + m + 1 = L + (m + 1) from rfl, R1sh K x L hz] at h
  have h' : K.add (Rk K x L k) (K.neg (K.mul (x 0) (x k)))
      = K.add (K.mul (x 1) (x (1 + k))) (ip K (fun i => x (i + 2)) (fun i => x (i + 2 + k)) (L + m)) := by
    rw [h]
  rw [show ip K (fun i => x (i + 2)) (fun i => x (i + 2 + k)) (L + m)
      = K.add (K.add (Rk K x L k) (K.neg (K.mul (x 0) (x k)))) (K.neg (K.mul (x 1) (x (1 + k)))) by rw [h']; lib_ring]
  lib_ring

include hz in
theorem e7 : ip K (Zs K (Zs K (c1v K x))) x (L + 4)
    = K.add (Rk K x L 3) (K.add (Rk K x L 1) (K.neg (K.mul (x 0) (x 1)))) := by
  show ip K (Zs K (Zs K (c1v K x))) x (L + 3 + 1) = _
  rw [ip_z1]; show ip K (Zs K (c1v K x)) (fun i => x (i + 1)) (L + 2 + 1) = _
  rw [ip_z1]; unfold c1v; rw [ip_add_l]
  have a : ip K (Zs K x) (fun i => x (i + 1 + 1)) (L + 2) = Rk K x L 3 := by
    show ip K (Zs K x) (fun i => x (i + 1 + 1)) (L + 1 + 1) = _; rw [ip_z1]; exact Rtail K x L hz 3 1
  have b : ip K (fun i => x (i + 1)) (fun i => x (i + 1 + 1)) (L + 2) = K.add (Rk K x L 1) (K.neg (K.mul (x 0) (x 1))) :=
    R1sh K x L hz 1 2
  rw [a, b]

include hz in
theorem e8 : ip K (Zs K (Zs K (c1v K x))) (Zs K x) (L + 4)
    = K.add (Rk K x L 2) (K.add (Rk K x L 0) (K.neg (K.mul (x 0) (x 0)))) := by
  show ip K (Zs K (Zs K (c1v K x))) (Zs K x) (L + 3 + 1) = _
  rw [ip_zz]; show ip K (Zs K (c1v K x)) x (L + 2 + 1) = _
  rw [ip_z1]; unfold c1v; rw [ip_add_l]
  have a : ip K (Zs K x) (fun i => x (i + 1)) (L + 2) = Rk K x L 2 := by
    show ip K (Zs K x) (fun i => x (i + 1)) (L + 1 + 1) = _; rw [ip_z1]; exact Rtail K x L hz 2 1
  have b : ip K (fun i => x (i + 1)) (fun i => x (i + 1)) (L + 2) = K.add (Rk K x L 0) (K.neg (K.mul (x 0) (x 0))) :=
    R1sh K x L hz 0 2
  rw [a, b]

include hz in
theorem e9 : ip K (Zs K x) (Zs K (c1v K x)) (L + 4) = K.add (Rk K x L 1) (Rk K x L 1) := by
  show ip K (Zs K x) (Zs K (c1v K x)) (L + 3 + 1) = _
  rw [ip_zz]; unfold c1v; rw [ip_add_r]
  have a : ip K x (Zs K x) (L + 3) = Rk K x L 1 := by
    show ip K x (Zs K x) (L + 2 + 1) = _; rw [ip_1z, ip_comm]; exact Rtail K x L hz 1 2
  rw [a, Rtail K x L hz 1 3]

include hz in
theorem e10 : ip K x (Zs K (p2v K x)) (L + 4)
    = K.add (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))))) := by
  show ip K x (Zs K (p2v K x)) (L + 3 + 1) = _
  rw [ip_1z]; show ip K (fun i => x (i + 1)) (p2v K x) (L + 2 + 1) = _
  rw [ip_head, ip_ext K (fun i => x (i + 1 + 1)) (fun i => x (i + 2)) (fun i => p2v K x (i + 1)) (fun i => K.add (K.add (Zs K x i) (x (i + 1))) (x (i + 3))) (fun i => rfl) (fun i => rfl),
    ip_add_r, ip_add_r]
  have a : ip K (fun i => x (i + 2)) (Zs K x) (L + 2) = Rk K x L 3 := by
    show ip K (fun i => x (i + 2)) (Zs K x) (L + 1 + 1) = _; rw [ip_1z, ip_comm]; exact Rtail K x L hz 3 1
  have b : ip K (fun i => x (i + 2)) (fun i => x (i + 1)) (L + 2) = K.add (Rk K x L 1) (K.neg (K.mul (x 0) (x 1))) := by
    rw [ip_comm]; exact R1sh K x L hz 1 2
  have c : ip K (fun i => x (i + 2)) (fun i => x (i + 3)) (L + 2)
      = K.add (Rk K x L 1) (K.add (K.neg (K.mul (x 0) (x 1))) (K.neg (K.mul (x 1) (x 2)))) := R2sh K x L hz 1 2
  rw [a, b, c]
  show K.add (K.mul (x (0 + 1)) (x 2)) _ = _
  rw [Nat.zero_add]; lib_ring

include hz in
theorem e11 : ip K (Zs K x) (Zs K (p2v K x)) (L + 4)
    = 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)))) := by
  show ip K (Zs K x) (Zs K (p2v K x)) (L + 3 + 1) = _
  rw [ip_zz]; show ip K x (p2v K x) (L + 2 + 1) = _
  rw [ip_head, ip_ext K (fun i => x (i + 1)) (fun i => x (i + 1)) (fun i => p2v K x (i + 1)) (fun i => K.add (K.add (Zs K x i) (x (i + 1))) (x (i + 3))) (fun i => rfl) (fun i => rfl),
    ip_add_r, ip_add_r]
  have a : ip K (fun i => x (i + 1)) (Zs K x) (L + 2) = Rk K x L 2 := by
    show ip K (fun i => x (i + 1)) (Zs K x) (L + 1 + 1) = _; rw [ip_1z, ip_comm]; exact Rtail K x L hz 2 1
  have b : ip K (fun i => x (i + 1)) (fun i => x (i + 1)) (L + 2) = K.add (Rk K x L 0) (K.neg (K.mul (x 0) (x 0))) :=
    R1sh K x L hz 0 2
  have c : ip K (fun i => x (i + 1)) (fun i => x (i + 3)) (L + 2) = K.add (Rk K x L 2) (K.neg (K.mul (x 0) (x 2))) :=
    R1sh K x L hz 2 2
  rw [a, b, c]
  show K.add (K.mul (x 0) (x 2)) _ = _
  lib_ring

end closed

end A1Ip

#print axioms A1Ip.ip_zz
#print axioms A1Ip.ip_z1
#print axioms A1Ip.ip_tail
#print axioms A1Ip.Rtail
#print axioms A1Ip.R1sh
#print axioms A1Ip.e1
#print axioms A1Ip.e5
#print axioms A1Ip.e6
#print axioms A1Ip.R2sh
#print axioms A1Ip.e7
#print axioms A1Ip.e8
#print axioms A1Ip.e9
#print axioms A1Ip.e10
#print axioms A1Ip.e11
#print axioms A1Ip.ip_1z
#print axioms A1Ip.ip_head
#print axioms A1Ip.ip_add_l
#print axioms A1Ip.ip_add_r
#print axioms A1Ip.ip_comm
#print axioms A1Ip.ip_ext
#print axioms A1Ip.e2
#print axioms A1Ip.e3
#print axioms A1Ip.e4

ym/A2.lean

lean_all/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

ym/A2Ip.lean

lean_all/ym/A2Ip.lean

/-
  ym/A2Ip — T45, G53 (A) at n = 2, step 1: general shift lemma and the inner products used by the n = 2 identity.
    Rgen   ⟨x(·+a), x(·+a+k)⟩ = R_k − Σ_{i<a} x_i x_{i+k}   (x vanishing from L on)
  Lean 4 core only, no axioms.
-/
import ym.A1C

namespace A2Ip

open YMChain QBinom CgWindow A1Ip

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

section g

variable (x : Nat → R) (L : Nat) (hz : ∀ t, L ≤ t → x t = K.zero)

/-- boundary sum Σ_{i<a} x_i x_{i+k} -/
def Bs (k a : Nat) : R := sumN K (fun i => K.mul (x i) (x (i + k))) a

include hz in
theorem Rgen (k : Nat) : ∀ a m, ip K (fun i => x (i + a)) (fun i => x (i + a + k)) (L + m)
    = K.add (Rk K x L k) (K.neg (Bs K x k a))
  | 0, m => by
    rw [show (fun i => x (i + 0)) = x from rfl]
    show ip K x (fun i => x (i + 0 + k)) (L + m) = _
    rw [ip_ext K x x (fun i => x (i + 0 + k)) (fun i => x (i + k)) (fun _ => rfl) (fun i => by rw [Nat.add_zero]),
      Rtail K x L hz]
    show _ = K.add (Rk K x L k) (K.neg K.zero); lib_ring
  | a + 1, m => by
    have h := Rgen k a (m + 1)
    rw [show L + (m + 1) = L + m + 1 from rfl, ip_head] at h
    have h' : ip K (fun i => x (i + (a + 1))) (fun i => x (i + (a + 1) + k)) (L + m)
        = ip K (fun i => x (i + 1 + a)) (fun i => x (i + 1 + a + k)) (L + m) :=
      ip_ext K _ _ _ _ (fun i => by rw [show i + (a + 1) = i + 1 + a by lib_nat]) (fun i => by rw [show i + (a + 1) + k = i + 1 + a + k by lib_nat]) _
    rw [h']
    have e : K.add (K.mul (x (0 + a)) (x (0 + a + k))) (ip K (fun i => x (i + 1 + a)) (fun i => x (i + 1 + a + k)) (L + m))
        = K.add (Rk K x L k) (K.neg (Bs K x k a)) := h
    rw [Nat.zero_add] at e
    show _ = K.add (Rk K x L k) (K.neg (K.add (Bs K x k a) (K.mul (x a) (x (a + k)))))
    rw [show ip K (fun i => x (i + 1 + a)) (fun i => x (i + 1 + a + k)) (L + m)
        = K.add (K.add (Rk K x L k) (K.neg (Bs K x k a))) (K.neg (K.mul (x a) (x (a + k)))) by rw [← e]; lib_ring]
    lib_ring

/-- vanishing beyond the support -/
def Van (f : Nat → R) (c : Nat) : Prop := ∀ i, c ≤ i → f i = K.zero

include hz in
theorem van_x : Van K x L := hz

theorem van_zs (f : Nat → R) (c : Nat) (h : Van K f c) : Van K (Zs K f) (c + 1) := fun i hi => by
  cases i with
  | zero => exact absurd hi (Nat.not_succ_le_zero _)
  | succ i => exact h i (Nat.le_of_succ_le_succ hi)

include hz in
theorem van_c1 : Van K (c1v K x) (L + 1) := fun i hi => by
  unfold c1v
  rw [van_zs K x L hz i hi, hz (i + 1) (Nat.le_trans (Nat.le_succ L) (Nat.succ_le_succ (Nat.le_of_succ_le hi)))]
  lib_ring

include hz in
theorem van_p2 : Van K (p2v K x) (L + 2) := fun i hi => by
  cases i with
  | zero => exact absurd hi (Nat.not_succ_le_zero _)
  | succ i =>
    have hi' : L + 1 ≤ i := Nat.le_of_succ_le_succ hi
    show K.add (K.add (Zs K x i) (x (i + 1))) (x (i + 3)) = K.zero
    rw [van_zs K x L hz i hi', hz (i + 1) (Nat.le_trans (Nat.le_succ L) (Nat.le_trans hi' (Nat.le_succ i))),
      hz (i + 3) (Nat.le_trans (Nat.le_succ L) (Nat.le_trans hi' (LibNatLe.le_of_eq_add rfl)))]
    lib_ring

/-- range change: if the product vanishes from L+a on, the sums over L+a and L+a+d agree -/
theorem ip_range (u v : Nat → R) (a d : Nat) (h : ∀ i, L + a ≤ i → K.mul (u i) (v i) = K.zero) :
    ip K u v (L + a + d) = ip K u v (L + a) := ip_tail K u v (L + a) h d

theorem mul_van_l (u v : Nat → R) (c : Nat) (h : Van K u c) : ∀ i, c ≤ i → K.mul (u i) (v i) = K.zero := fun i hi => by
  rw [h i hi]; lib_ring

theorem mul_van_r (u v : Nat → R) (c : Nat) (h : Van K v c) : ∀ i, c ≤ i → K.mul (u i) (v i) = K.zero := fun i hi => by
  rw [h i hi]; lib_ring

include hz in
theorem Rg (k a m : Nat) (f g : Nat → R) (hf : ∀ i, f i = x (i + a)) (hg : ∀ i, g i = x (i + a + k)) :
    ip K f g (L + m) = K.add (Rk K x L k) (K.neg (Bs K x k a)) := by
  rw [ip_ext K f (fun i => x (i + a)) g (fun i => x (i + a + k)) hf hg]; exact Rgen K x L hz k a m

include hz in
theorem Rt (k m : Nat) (g : Nat → R) (hg : ∀ i, g i = x (i + k)) : ip K x g (L + m) = Rk K x L k := by
  rw [ip_ext K x x g (fun i => x (i + k)) (fun _ => rfl) hg]; exact Rtail K x L hz k m

include hz in
theorem f1 : ip K (Zs K (Zs K (Zs K x))) x (L + 6) = Rk K x L 3 := by
  show ip K _ _ (L + 5 + 1) = _; rw [ip_z1]; show ip K _ _ (L + 4 + 1) = _; rw [ip_z1]
  show ip K _ _ (L + 3 + 1) = _; rw [ip_z1]; exact Rt K x L hz 3 3 _ (fun i => rfl)

include hz in
theorem f2 : ip K (Zs K (Zs K x)) (fun i => x (i + 1)) (L + 6) = Rk K x L 3 := by
  show ip K _ _ (L + 5 + 1) = _; rw [ip_z1]; show ip K _ _ (L + 4 + 1) = _; rw [ip_z1]; exact Rt K x L hz 3 4 _ (fun i => rfl)

include hz in
theorem f3 : ip K (Zs K (Zs K (Zs K x))) (fun i => x (i + 1)) (L + 6) = Rk K x L 4 := by
  show ip K _ _ (L + 5 + 1) = _; rw [ip_z1]; show ip K _ _ (L + 4 + 1) = _; rw [ip_z1]
  show ip K _ _ (L + 3 + 1) = _; rw [ip_z1]; exact Rt K x L hz 4 3 _ (fun i => rfl)

include hz in
theorem f4 : ip K (Zs K (Zs K x)) x (L + 6) = Rk K x L 2 := by
  show ip K _ _ (L + 5 + 1) = _; rw [ip_z1]; show ip K _ _ (L + 4 + 1) = _; rw [ip_z1]; exact Rt K x L hz 2 4 _ (fun i => rfl)

include hz in
theorem f5 : ip K (Zs K (c1v K x)) (fun i => x (i + 1)) (L + 6)
    = K.add (Rk K x L 3) (K.add (Rk K x L 1) (K.neg (Bs K x 1 1))) := by
  show ip K _ _ (L + 5 + 1) = _; rw [ip_z1]; unfold c1v; rw [ip_add_l]
  have a : ip K (Zs K x) (fun i => x (i + 1 + 1)) (L + 5) = Rk K x L 3 := by
    show ip K _ _ (L + 4 + 1) = _; rw [ip_z1]; exact Rt K x L hz 3 4 _ (fun i => rfl)
  rw [a, Rg K x L hz 1 1 5 _ _ (fun i => rfl) (fun i => rfl)]

include hz in
theorem f6 : ip K (Zs K (c1v K x)) x (L + 6)
    = K.add (Rk K x L 2) (K.add (Rk K x L 0) (K.neg (Bs K x 0 1))) := by
  show ip K _ _ (L + 5 + 1) = _; rw [ip_z1]; unfold c1v; rw [ip_add_l]
  have a : ip K (Zs K x) (fun i => x (i + 1)) (L + 5) = Rk K x L 2 := by
    show ip K _ _ (L + 4 + 1) = _; rw [ip_z1]; exact Rt K x L hz 2 4 _ (fun i => rfl)
  rw [a, Rg K x L hz 0 1 5 _ _ (fun i => rfl) (fun i => rfl)]

include hz in
theorem f7 : ip K (p2v K x) (fun i => x (i + 1)) (L + 6)
    = K.add (K.mul (x 2) (x 1)) (K.add (Rk K x L 3) (K.add (K.add (Rk K x L 1) (K.neg (Bs K x 1 1))) (K.add (Rk K x L 1) (K.neg (Bs K x 1 2))))) := by
  show ip K _ _ (L + 5 + 1) = _; rw [ip_head]
  rw [ip_ext K (fun i => p2v K x (i + 1)) (fun i => K.add (K.add (Zs K x i) (x (i + 1))) (x (i + 3))) (fun i => x (i + 1 + 1)) (fun i => x (i + 2))
    (fun i => rfl) (fun i => rfl), ip_add_l, ip_add_l]
  have a : ip K (Zs K x) (fun i => x (i + 2)) (L + 5) = Rk K x L 3 := by
    show ip K _ _ (L + 4 + 1) = _; rw [ip_z1]; exact Rt K x L hz 3 4 _ (fun i => rfl)
  have b := Rg K x L hz 1 1 5 (fun i => x (i + 1)) (fun i => x (i + 2)) (fun i => rfl) (fun i => rfl)
  have c : ip K (fun i => x (i + 3)) (fun i => x (i + 2)) (L + 5) = K.add (Rk K x L 1) (K.neg (Bs K x 1 2)) := by
    rw [ip_comm]; exact Rg K x L hz 1 2 5 _ _ (fun i => rfl) (fun i => rfl)
  rw [a, b, c]; show K.add (K.mul (x 2) (x 1)) _ = _; lib_ring

include hz in
theorem f8 : ip K (p2v K x) x (L + 6)
    = K.add (K.mul (x 2) (x 0)) (K.add (Rk K x L 2) (K.add (K.add (Rk K x L 0) (K.neg (Bs K x 0 1))) (K.add (Rk K x L 2) (K.neg (Bs K x 2 1))))) := by
  show ip K _ _ (L + 5 + 1) = _; rw [ip_head]
  rw [ip_ext K (fun i => p2v K x (i + 1)) (fun i => K.add (K.add (Zs K x i) (x (i + 1))) (x (i + 3))) (fun i => x (i + 1)) (fun i => x (i + 1))
    (fun i => rfl) (fun i => rfl), ip_add_l, ip_add_l]
  have a : ip K (Zs K x) (fun i => x (i + 1)) (L + 5) = Rk K x L 2 := by
    show ip K _ _ (L + 4 + 1) = _; rw [ip_z1]; exact Rt K x L hz 2 4 _ (fun i => rfl)
  have b := Rg K x L hz 0 1 5 (fun i => x (i + 1)) (fun i => x (i + 1)) (fun i => rfl) (fun i => rfl)
  have c : ip K (fun i => x (i + 3)) (fun i => x (i + 1)) (L + 5) = K.add (Rk K x L 2) (K.neg (Bs K x 2 1)) := by
    rw [ip_comm]; exact Rg K x L hz 2 1 5 _ _ (fun i => rfl) (fun i => rfl)
  rw [a, b, c]; show K.add (K.mul (x 2) (x 0)) _ = _; lib_ring

include hz in
theorem f9 : ip K (Zs K (Zs K x)) (Zs K (Zs K (A1C.p3v K x))) (L + 6)
    = 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))))) := by
  show ip K _ _ (L + 4 + 1 + 1) = _; rw [ip_zz, ip_zz]; exact A1C.ip_p3 K x L hz

include hz in
theorem xr (v : Nat → R) (a d : Nat) : ip K x v (L + a + d) = ip K x v (L + a) :=
  ip_range K L x v a d (fun i hi => by rw [hz i (Nat.le_trans (Nat.le_add_right L a) hi)]; lib_ring)

include hz in
theorem f10 : ip K (Zs K x) (Zs K (Zs K (p2v K x))) (L + 6)
    = K.add (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))))) := by
  show ip K _ _ (L + 5 + 1) = _; rw [ip_zz, show L + 5 = L + 4 + 1 from rfl, xr K x L hz]; exact e10 K x L hz

include hz in
theorem f11 : ip K (Zs K (Zs K x)) (Zs K (Zs K (p2v K x))) (L + 6)
    = 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)))) := by
  show ip K _ _ (L + 4 + 1 + 1) = _; rw [ip_zz, ip_zz]
  have e := e11 K x L hz
  rw [show L + 4 = L + 3 + 1 from rfl, ip_zz] at e
  rw [show L + 4 = L + 3 + 1 from rfl, xr K x L hz]; exact e

include hz in
theorem f12 : ip K (Zs K x) (Zs K (Zs K (A1C.p3v K x))) (L + 6)
    = K.add (K.add (K.mul (x 1) (x 3)) (K.add (K.mul (x 2) (K.add (x 2) (x 4))) (K.mul (x 3) (K.add (K.add (x 1) (x 3)) (x 5)))))
        (K.add (K.add (Rk K x L 4) (K.add (Rk K x L 2) (K.neg (Bs K x 2 2))))
          (K.add (K.add (Rk K x L 0) (K.neg (Bs K x 0 4))) (K.add (Rk K x L 2) (K.neg (Bs K x 2 4))))) := by
  show ip K _ _ (L + 5 + 1) = _; rw [ip_zz]; show ip K _ _ (L + 4 + 1) = _; rw [ip_1z]
  show ip K _ _ (L + 1 + 1 + 1 + 1) = _; rw [ip_head, ip_head, ip_head]
  rw [ip_ext K (fun i => x (i + 1 + 1 + 1 + 1)) (fun i => x (i + 4)) (fun i => A1C.p3v K x (i + 1 + 1 + 1))
    (fun d => K.add (K.add (K.add (x d) (x (d + 2))) (x (d + 4))) (x (d + 6))) (fun i => rfl) (fun i => rfl),
    ip_add_r, ip_add_r, ip_add_r]
  have a : ip K (fun i => x (i + 4)) x (L + 1) = Rk K x L 4 := by rw [ip_comm]; exact Rt K x L hz 4 1 _ (fun i => rfl)
  have b : ip K (fun i => x (i + 4)) (fun i => x (i + 2)) (L + 1) = K.add (Rk K x L 2) (K.neg (Bs K x 2 2)) := by
    rw [ip_comm]; exact Rg K x L hz 2 2 1 _ _ (fun i => rfl) (fun i => rfl)
  have c := Rg K x L hz 0 4 1 (fun i => x (i + 4)) (fun i => x (i + 4)) (fun i => rfl) (fun i => rfl)
  have d := Rg K x L hz 2 4 1 (fun i => x (i + 4)) (fun i => x (i + 6)) (fun i => rfl) (fun i => rfl)
  rw [a, b, c, d]
  show K.add (K.mul (x 1) (x 3)) (K.add (K.mul (x 2) (K.add (x 2) (x 4))) (K.add (K.mul (x 3) (K.add (K.add (x 1) (x 3)) (x 5))) _)) = _
  lib_ring

include hz in
theorem f13 : ip K (Zs K (Zs K (c1v K x))) x (L + 6)
    = K.add (Rk K x L 3) (K.add (Rk K x L 1) (K.neg (Bs K x 1 1))) := by
  show ip K _ _ (L + 5 + 1) = _; rw [ip_z1]; show ip K _ _ (L + 4 + 1) = _; rw [ip_z1]; unfold c1v; rw [ip_add_l]
  have a : ip K (Zs K x) (fun i => x (i + 1 + 1)) (L + 4) = Rk K x L 3 := by
    show ip K _ _ (L + 3 + 1) = _; rw [ip_z1]; exact Rt K x L hz 3 3 _ (fun i => rfl)
  rw [a, Rg K x L hz 1 1 4 _ _ (fun i => rfl) (fun i => rfl)]

include hz in
theorem f14 : ip K (Zs K (Zs K (c1v K x))) (fun i => x (i + 1)) (L + 6)
    = K.add (Rk K x L 4) (K.add (Rk K x L 2) (K.neg (Bs K x 2 1))) := by
  show ip K _ _ (L + 5 + 1) = _; rw [ip_z1]; show ip K _ _ (L + 4 + 1) = _; rw [ip_z1]; unfold c1v; rw [ip_add_l]
  have a : ip K (Zs K x) (fun i => x (i + 1 + 1 + 1)) (L + 4) = Rk K x L 4 := by
    show ip K _ _ (L + 3 + 1) = _; rw [ip_z1]; exact Rt K x L hz 4 3 _ (fun i => rfl)
  rw [a, Rg K x L hz 2 1 4 _ _ (fun i => rfl) (fun i => rfl)]

include hz in
theorem f16 : ip K x (Zs K (Zs K (p2v K x))) (L + 6)
    = K.add (K.mul (x 2) (x 2)) (K.add (Rk K x L 4) (K.add (K.add (Rk K x L 2) (K.neg (Bs K x 2 1))) (K.add (Rk K x L 0) (K.neg (Bs K x 0 3))))) := by
  show ip K _ _ (L + 5 + 1) = _; rw [ip_1z]; show ip K _ _ (L + 4 + 1) = _; rw [ip_1z]
  show ip K _ _ (L + 3 + 1) = _; rw [ip_head]
  rw [ip_ext K (fun i => x (i + 1 + 1 + 1)) (fun i => x (i + 3)) (fun i => p2v K x (i + 1)) (fun i => K.add (K.add (Zs K x i) (x (i + 1))) (x (i + 3)))
    (fun i => rfl) (fun i => rfl), ip_add_r, ip_add_r]
  have a : ip K (fun i => x (i + 3)) (Zs K x) (L + 3) = Rk K x L 4 := by
    show ip K _ _ (L + 2 + 1) = _; rw [ip_1z, ip_comm]; exact Rt K x L hz 4 2 _ (fun i => rfl)
  have b : ip K (fun i => x (i + 3)) (fun i => x (i + 1)) (L + 3) = K.add (Rk K x L 2) (K.neg (Bs K x 2 1)) := by
    rw [ip_comm]; exact Rg K x L hz 2 1 3 _ _ (fun i => rfl) (fun i => rfl)
  have c := Rg K x L hz 0 3 3 (fun i => x (i + 3)) (fun i => x (i + 3)) (fun i => rfl) (fun i => rfl)
  rw [a, b, c]; show K.add (K.mul (x 2) (x 2)) _ = _; lib_ring

include hz in
theorem f17 : ip K (Zs K x) (Zs K (Zs K (c1v K x))) (L + 6)
    = K.add (Rk K x L 2) (K.add (Rk K x L 0) (K.neg (Bs K x 0 1))) := by
  show ip K _ _ (L + 5 + 1) = _; rw [ip_zz]; show ip K _ _ (L + 4 + 1) = _; rw [ip_1z]; unfold c1v; rw [ip_add_r]
  have a : ip K (fun i => x (i + 1)) (Zs K x) (L + 4) = Rk K x L 2 := by
    show ip K _ _ (L + 3 + 1) = _; rw [ip_1z, ip_comm]; exact Rt K x L hz 2 3 _ (fun i => rfl)
  rw [a, Rg K x L hz 0 1 4 _ _ (fun i => rfl) (fun i => rfl)]

/-- iterated right shift -/
def Zsn : Nat → (Nat → R) → Nat → R
  | 0, f => f
  | a + 1, f => Zs K (Zsn a f)

include hz in
/-- ⟨S⁻ᵃx, Sᵇx⟩ = R_{a+b} (no boundary loss) -/
theorem ipZS : ∀ a b m, ip K (Zsn K a x) (fun i => x (i + b)) (L + a + m) = Rk K x L (a + b)
  | 0, b, m => by rw [Nat.add_zero, Nat.zero_add]; exact Rtail K x L hz b m
  | a + 1, b, m => by
    show ip K (Zs K (Zsn K a x)) (fun i => x (i + b)) (L + (a + 1) + m) = _
    rw [show L + (a + 1) + m = L + a + m + 1 by lib_nat, ip_z1]
    rw [ip_ext K _ (Zsn K a x) _ (fun i => x (i + (b + 1))) (fun _ => rfl) (fun i => by rw [show i + 1 + b = i + (b + 1) by lib_nat]),
      ipZS a (b + 1) m, show a + (b + 1) = a + 1 + b by lib_nat]

/-- the reflected row of width 4 -/
def p4v : Nat → R
  | 0 => x 4
  | 1 => K.add (x 3) (x 5)
  | 2 => K.add (K.add (x 2) (x 4)) (x 6)
  | 3 => K.add (K.add (K.add (x 1) (x 3)) (x 5)) (x 7)
  | d + 4 => K.add (K.add (K.add (K.add (x d) (x (d + 2))) (x (d + 4))) (x (d + 6))) (x (d + 8))

include hz in
theorem ip_p4 : ip K x (p4v K x) (L + 6)
    = K.add (K.add (K.add (K.mul (x 0) (x 4)) (K.mul (x 1) (K.add (x 3) (x 5))))
        (K.add (K.mul (x 2) (K.add (K.add (x 2) (x 4)) (x 6))) (K.mul (x 3) (K.add (K.add (K.add (x 1) (x 3)) (x 5)) (x 7)))))
      (K.add (K.add (Rk K x L 4) (K.add (Rk K x L 2) (K.neg (Bs K x 2 2))))
        (K.add (K.add (Rk K x L 0) (K.neg (Bs K x 0 4))) (K.add (K.add (Rk K x L 2) (K.neg (Bs K x 2 4))) (K.add (Rk K x L 4) (K.neg (Bs K x 4 4)))))) := by
  show ip K _ _ (L + 2 + 1 + 1 + 1 + 1) = _
  rw [ip_head, ip_head, ip_head, ip_head]
  rw [ip_ext K (fun i => x (i + 1 + 1 + 1 + 1)) (fun i => x (i + 4)) (fun i => p4v K x (i + 1 + 1 + 1 + 1))
    (fun d => K.add (K.add (K.add (K.add (x d) (x (d + 2))) (x (d + 4))) (x (d + 6))) (x (d + 8))) (fun i => rfl) (fun i => rfl),
    ip_add_r, ip_add_r, ip_add_r, ip_add_r]
  have a : ip K (fun i => x (i + 4)) x (L + 2) = Rk K x L 4 := by rw [ip_comm]; exact Rt K x L hz 4 2 _ (fun i => rfl)
  have b : ip K (fun i => x (i + 4)) (fun i => x (i + 2)) (L + 2) = K.add (Rk K x L 2) (K.neg (Bs K x 2 2)) := by
    rw [ip_comm]; exact Rg K x L hz 2 2 2 _ _ (fun i => rfl) (fun i => rfl)
  have c := Rg K x L hz 0 4 2 (fun i => x (i + 4)) (fun i => x (i + 4)) (fun i => rfl) (fun i => rfl)
  have d := Rg K x L hz 2 4 2 (fun i => x (i + 4)) (fun i => x (i + 6)) (fun i => rfl) (fun i => rfl)
  have e := Rg K x L hz 4 4 2 (fun i => x (i + 4)) (fun i => x (i + 8)) (fun i => rfl) (fun i => rfl)
  rw [a, b, c, d, e]
  show K.add (K.mul (x 0) (x 4)) (K.add (K.mul (x 1) (K.add (x 3) (x 5))) (K.add (K.mul (x 2) (K.add (K.add (x 2) (x 4)) (x 6)))
    (K.add (K.mul (x 3) (K.add (K.add (K.add (x 1) (x 3)) (x 5)) (x 7))) _))) = _
  lib_ring

end g

theorem psi4 (X : List R) : ∀ j, CgWindow.Psi K X j 4 = p4v K (HeatPair.co K X) j
  | 0 => by
    have := Psi_lo K X 0 4; rw [Nat.zero_add] at this; rw [this]
    show K.add K.zero (HeatPair.co K X (4 + 2 * 0)) = HeatPair.co K X 4; rw [show 4 + 2 * 0 = 4 from rfl]; lib_ring
  | 1 => by
    have := Psi_lo K X 1 3; rw [show 1 + 3 = 4 from rfl] at this; rw [this]
    show K.add (K.add K.zero (HeatPair.co K X (3 + 2 * 0))) (HeatPair.co K X (3 + 2 * 1)) = K.add (HeatPair.co K X 3) (HeatPair.co K X 5)
    rw [show 3 + 2 * 0 = 3 from rfl, show 3 + 2 * 1 = 5 from rfl]; lib_ring
  | 2 => by
    have := Psi_lo K X 2 2; rw [show 2 + 2 = 4 from rfl] at this; rw [this]
    show K.add (K.add (K.add K.zero (HeatPair.co K X (2 + 2 * 0))) (HeatPair.co K X (2 + 2 * 1))) (HeatPair.co K X (2 + 2 * 2))
      = K.add (K.add (HeatPair.co K X 2) (HeatPair.co K X 4)) (HeatPair.co K X 6)
    rw [show 2 + 2 * 0 = 2 from rfl, show 2 + 2 * 1 = 4 from rfl, show 2 + 2 * 2 = 6 from rfl]; lib_ring
  | 3 => by
    have := Psi_lo K X 3 1; rw [show 3 + 1 = 4 from rfl] at this; rw [this]
    show K.add (K.add (K.add (K.add K.zero (HeatPair.co K X (1 + 2 * 0))) (HeatPair.co K X (1 + 2 * 1))) (HeatPair.co K X (1 + 2 * 2))) (HeatPair.co K X (1 + 2 * 3))
      = K.add (K.add (K.add (HeatPair.co K X 1) (HeatPair.co K X 3)) (HeatPair.co K X 5)) (HeatPair.co K X 7)
    rw [show 1 + 2 * 0 = 1 from rfl, show 1 + 2 * 1 = 3 from rfl, show 1 + 2 * 2 = 5 from rfl, show 1 + 2 * 3 = 7 from rfl]; lib_ring
  | d + 4 => by
    have := Psi_hi K X 4 d; rw [show 4 + d = d + 4 by lib_nat] at this; rw [this]
    show K.add (K.add (K.add (K.add (K.add K.zero (HeatPair.co K X (d + 2 * 0))) (HeatPair.co K X (d + 2 * 1))) (HeatPair.co K X (d + 2 * 2)))
        (HeatPair.co K X (d + 2 * 3))) (HeatPair.co K X (d + 2 * 4))
      = K.add (K.add (K.add (K.add (HeatPair.co K X d) (HeatPair.co K X (d + 2))) (HeatPair.co K X (d + 4))) (HeatPair.co K X (d + 6))) (HeatPair.co K X (d + 8))
    rw [show d + 2 * 0 = d by lib_nat, show d + 2 * 1 = d + 2 by lib_nat, show d + 2 * 2 = d + 4 by lib_nat,
      show d + 2 * 3 = d + 6 by lib_nat, show d + 2 * 4 = d + 8 by lib_nat]; lib_ring

end A2Ip

#print axioms A2Ip.Rgen
#print axioms A2Ip.van_c1
#print axioms A2Ip.van_p2
#print axioms A2Ip.ip_range
#print axioms A2Ip.f1
#print axioms A2Ip.f2
#print axioms A2Ip.f3
#print axioms A2Ip.f4
#print axioms A2Ip.f5
#print axioms A2Ip.f6
#print axioms A2Ip.f7
#print axioms A2Ip.f8
#print axioms A2Ip.f9
#print axioms A2Ip.f10
#print axioms A2Ip.f11
#print axioms A2Ip.f12
#print axioms A2Ip.f13
#print axioms A2Ip.f14
#print axioms A2Ip.f16
#print axioms A2Ip.f17
#print axioms A2Ip.ip_p4
#print axioms A2Ip.psi4
#print axioms A2Ip.van_x
#print axioms A2Ip.van_zs
#print axioms A2Ip.mul_van_l
#print axioms A2Ip.mul_van_r
#print axioms A2Ip.Rg
#print axioms A2Ip.Rt
#print axioms A2Ip.xr
#print axioms A2Ip.ipZS

ym/AAll.lean

lean_all/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

ym/AInd.lean

lean_all/ym/AInd.lean

/-
  ym/AInd — T45, G54 (induction route for (A)): the tail of a v-good list is v-good.
  If X = x₀ :: X' is v-good then so is X' (co X' j = co X (j+1); the v-monotonicity conditions shift by one and the
  base condition of X' follows from the first v-condition of X). With T_n(x) ≥ T_{n−1}(Sx) this would give (A) for
  every n by induction from LC0. Lean 4 core only, no axioms.
-/
import ym.A2

namespace AInd

open YMChain QBinom HeatPair

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

theorem co_cons (x0 : R) (X : List R) (j : Nat) : co K (x0 :: X) (j + 1) = co K X j := rfl

/-- **the tail of a v-good list is v-good** -/
theorem good_tail (x0 : R) (X : List R) (hg : L1Full.Good K O (x0 :: X)) : L1Full.Good K O X := by
  have ⟨hb, hpos, hv, h0⟩ := hg
  refine ⟨fun j => hb (j + 1), fun j hj => hpos (j + 1) (Nat.succ_lt_succ hj), fun m => hv (m + 1), ?_⟩
  -- base: X₁² ≤ (X₀ + X₂) X₀ from VV at 0 of x0 :: X:  (x₀ + X₁) X₁ ≤ (X₀ + X₂) X₀
  have v0 := hv 0
  unfold HalfRatio.VV at v0
  have e1 : co K (x0 :: X) 0 = x0 := rfl
  show O.le (K.mul (co K X 1) (co K X 1)) (K.mul (K.add (co K X 0) (co K X 2)) (co K X 0))
  have e : K.mul (K.add (co K (x0 :: X) 0) (co K (x0 :: X) (0 + 2))) (co K (x0 :: X) (0 + 2))
      = K.add (K.mul x0 (co K X 1)) (K.mul (co K X 1) (co K X 1)) := by
    show K.mul (K.add x0 (co K X 1)) (co K X 1) = _; lib_ring
  have e' : K.mul (K.add (co K (x0 :: X) (0 + 1)) (co K (x0 :: X) (0 + 3))) (co K (x0 :: X) (0 + 1))
      = K.mul (K.add (co K X 0) (co K X 2)) (co K X 0) := rfl
  rw [e, e'] at v0
  have p : O.le K.zero (K.mul x0 (co K X 1)) := O.mul_nonneg _ _ (hb 0) (hb 2)
  have := O.add_le_add _ _ _ _ p (O.le_refl (K.mul (co K X 1) (co K X 1)))
  rw [show K.add K.zero (K.mul (co K X 1) (co K X 1)) = K.mul (co K X 1) (co K X 1) by lib_ring] at this
  exact O.le_trans _ _ _ this v0

end AInd

#print axioms AInd.co_cons
#print axioms AInd.good_tail

ym/AIndLC.lean

lean_all/ym/AIndLC.lean

/-
  ym/AIndLC — T45, G54: induction columns. For a v-good list x0 :: Y (Y of length at least 2) and m ≤ 3,
  the Turán expression of the tail Y is nonnegative.
-/
import ym.BondLC3
import ym.AInd
import ym.HeatV

namespace AIndLC

open YMChain QBinom CgWindow MigdalCone HeatPair

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

theorem tail_turan_nn (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)) (M : Nat) (hY : Y.length = M + 2)
    (m : Nat) (hm : m ≤ 3) :
    O.le K.zero (K.add (K.mul (LibRing.castN K ((m + 1) * (m + 3))) (K.mul (cgc K Y Y (m + 1)) (cgc K Y Y (m + 1))))
      (K.neg (K.mul (LibRing.castN K ((m + 2) * (m + 2))) (K.mul (cgc K Y Y m) (cgc K Y Y (m + 2)))))) :=
  HeatV.sub_nn K O _ _ (BondLC3.bond_lc_le3 K O F h1 h10 Y (AInd.good_tail K O x0 Y hg) M hY m hm)

end AIndLC

#print axioms AIndLC.tail_turan_nn

ym/AIndLC4.lean

lean_all/ym/AIndLC4.lean

/-
  ym/AIndLC4 — T45, G54: induction columns up to m = 4. For a v-good list x0 :: Y (Y of length at least 2)
  and m ≤ 4, the Turán expression of the tail Y is nonnegative.
-/
import ym.BondLC4
import ym.AInd
import ym.HeatV

namespace AIndLC4

open YMChain QBinom CgWindow MigdalCone HeatPair

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

theorem tail_turan_nn (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)) (M : Nat) (hY : Y.length = M + 2)
    (m : Nat) (hm : m ≤ 4) :
    O.le K.zero (K.add (K.mul (LibRing.castN K ((m + 1) * (m + 3))) (K.mul (cgc K Y Y (m + 1)) (cgc K Y Y (m + 1))))
      (K.neg (K.mul (LibRing.castN K ((m + 2) * (m + 2))) (K.mul (cgc K Y Y m) (cgc K Y Y (m + 2)))))) :=
  HeatV.sub_nn K O _ _ (BondLC4.bond_lc_le4 K O F h1 h10 Y (AInd.good_tail K O x0 Y hg) M hY m hm)

end AIndLC4

#print axioms AIndLC4.tail_turan_nn

ym/AIndLC6.lean

lean_all/ym/AIndLC6.lean

/-
  ym/AIndLC6 — T45, G54: induction columns up to m = 6. For a v-good list x0 :: Y (Y of length at least 2)
  and m ≤ 6, the Turán expression of the tail Y is nonnegative.
-/
import ym.BondLC6
import ym.AInd
import ym.HeatV

namespace AIndLC6

open YMChain QBinom CgWindow MigdalCone HeatPair

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

theorem tail_turan_nn (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)) (M : Nat) (hY : Y.length = M + 2)
    (m : Nat) (hm : m ≤ 6) :
    O.le K.zero (K.add (K.mul (LibRing.castN K ((m + 1) * (m + 3))) (K.mul (cgc K Y Y (m + 1)) (cgc K Y Y (m + 1))))
      (K.neg (K.mul (LibRing.castN K ((m + 2) * (m + 2))) (K.mul (cgc K Y Y m) (cgc K Y Y (m + 2)))))) :=
  HeatV.sub_nn K O _ _ (BondLC6.bond_lc_le6 K O F h1 h10 Y (AInd.good_tail K O x0 Y hg) M hY m hm)

end AIndLC6

#print axioms AIndLC6.tail_turan_nn

ym/AIndLC7.lean

lean_all/ym/AIndLC7.lean

/-
  ym/AIndLC7 — T45, G54: induction columns up to m = 7. For a v-good list x0 :: Y (Y of length at least 2)
  and m ≤ 7, the Turán expression of the tail Y is nonnegative.
-/
import ym.BondLC7
import ym.AInd
import ym.HeatV

namespace AIndLC7

open YMChain QBinom CgWindow MigdalCone HeatPair

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

theorem tail_turan_nn (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)) (M : Nat) (hY : Y.length = M + 2)
    (m : Nat) (hm : m ≤ 7) :
    O.le K.zero (K.add (K.mul (LibRing.castN K ((m + 1) * (m + 3))) (K.mul (cgc K Y Y (m + 1)) (cgc K Y Y (m + 1))))
      (K.neg (K.mul (LibRing.castN K ((m + 2) * (m + 2))) (K.mul (cgc K Y Y m) (cgc K Y Y (m + 2)))))) :=
  HeatV.sub_nn K O _ _ (BondLC7.bond_lc_le7 K O F h1 h10 Y (AInd.good_tail K O x0 Y hg) M hY m hm)

end AIndLC7

#print axioms AIndLC7.tail_turan_nn

ym/AVs.lean

lean_all/ym/AVs.lean

/-
  ym/AVs — T45, G54 infrastructure: inner products of sums of left shifts of x.
    vsum l i = Σ_{a∈l} x(i+a),   pr a b = ⟨x(·+a), x(·+b)⟩ over [0, L+m) = R_{|a−b|} − Σ_{i<min(a,b)} x_i x_{i+|a−b|}
    ip_vsum: ⟨vsum l₁, vsum l₂⟩ = Σ_{a∈l₁} Σ_{b∈l₂} pr a b.
  Used by generated fixed-n proofs of (A). Lean 4 core only, no axioms.
-/
import ym.A2Ip

namespace AVs

open YMChain QBinom CgWindow A1Ip A2Ip

variable {R : Type} (K : DetChain.CRing R) (x : Nat → R) (L : Nat) (hz : ∀ t, L ≤ t → x t = K.zero)

def vsum : List Nat → Nat → R
  | [], _ => K.zero
  | a :: l, i => K.add (x (i + a)) (vsum l i)

/-- ⟨x(·+a), x(·+a+k)⟩ value -/
def pv (a k : Nat) : R := K.add (Rk K x L k) (K.neg (Bs K x k a))

/-- pair value for an ordered pair given as (min, gap) both ways -/
def pr (a b : Nat) : R := if a ≤ b then pv K x L a (b - a) else pv K x L b (a - b)

def psum (l1 l2 : List Nat) : R :=
  l1.foldr (fun a acc => K.add (l2.foldr (fun b acc2 => K.add (pr K x L a b) acc2) K.zero) acc) K.zero

theorem sub_self_add (a k : Nat) : (a + k) - a = k := LibBinom.sub_add_self a k

include hz in
theorem ip_pair (a b m : Nat) : ip K (fun i => x (i + a)) (fun i => x (i + b)) (L + m) = pr K x L a b := by
  unfold pr
  cases Nat.lt_or_ge b a with
  | inr h =>
    rw [ite_eq_left h]
    have ⟨k, hk⟩ := Nat.le.dest h
    subst hk
    rw [sub_self_add]
    exact Rg K x L hz k a m _ _ (fun i => rfl) (fun i => by rw [show i + (a + k) = i + a + k by lib_nat])
  | inl h =>
    rw [ite_eq_right (Nat.not_le_of_gt h)]
    have ⟨k, hk⟩ := Nat.le.dest h
    subst hk
    rw [show b + 1 + k - b = k + 1 by rw [show b + 1 + k = b + (k + 1) by lib_nat]; exact sub_self_add b (k + 1), ip_comm]
    exact Rg K x L hz (k + 1) b m _ _ (fun i => rfl) (fun i => by rw [show i + (b + 1 + k) = i + b + (k + 1) by lib_nat])

theorem ip_nil_l (g : Nat → R) (n : Nat) : ip K (vsum K x []) g n = K.zero := by
  unfold ip; show sumN K (fun i => K.mul K.zero (g i)) n = K.zero
  induction n with
  | zero => rfl
  | succ n ih => show K.add (sumN K _ n) (K.mul K.zero (g n)) = K.zero; rw [ih]; lib_ring

include hz in
theorem ip_vsum_r (a : Nat) (m : Nat) : ∀ l2, ip K (fun i => x (i + a)) (vsum K x l2) (L + m)
    = l2.foldr (fun b acc2 => K.add (pr K x L a b) acc2) K.zero
  | [] => by rw [ip_comm]; exact ip_nil_l K x _ _
  | b :: l => by
    show ip K (fun i => x (i + a)) (fun i => K.add (x (i + b)) (vsum K x l i)) (L + m) = _
    rw [ip_add_r, ip_pair K x L hz a b m, ip_vsum_r a m l]; rfl

include hz in
theorem ip_vsum (m : Nat) (l2 : List Nat) : ∀ l1, ip K (vsum K x l1) (vsum K x l2) (L + m) = psum K x L l1 l2
  | [] => ip_nil_l K x _ _
  | a :: l => by
    show ip K (fun i => K.add (x (i + a)) (vsum K x l i)) (vsum K x l2) (L + m) = _
    rw [ip_add_l, ip_vsum_r K x L hz a m l2, ip_vsum m l2 l]; rfl

end AVs

#print axioms AVs.ip_pair
#print axioms AVs.ip_vsum

section test
variable {R : Type} (K : DetChain.CRing R) (x : Nat → R) (L : Nat)
example : AVs.psum K x L [1, 3] [0, 2] = K.add (K.add (AVs.pv K x L 0 1) (K.add (AVs.pv K x L 1 1) K.zero))
    (K.add (K.add (AVs.pv K x L 0 3) (K.add (AVs.pv K x L 2 1) K.zero)) K.zero) := rfl
end test

ym/AbelLR.lean

lean_all/ym/AbelLR.lean

/-
  ym/AbelLR — T45, G44-L1 (rectangle reduction): Abel's inequality in one and two variables.
  (1) If every prefix sum of d is ≥ 0 and g is nonincreasing and ≥ 0, then Σ_{j<N} d_j g_j ≥ 0.
  (2) If every rectangle sum Σ_{j<a, k<b} D_{jk} is ≥ 0 (a ≤ A, b ≤ B), then Σ_{j<A} Σ_{k<B} D_{jk} g_j g_k ≥ 0.
  With D = Q·V − P·U this reduces the LR step of bond moving (L1) for X = Y·g to g-free rectangle inequalities.
  Lean 4 core only, no axioms.
-/
import ym.TripleE

namespace AbelLR

open YMChain QBinom

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

theorem abel_aux (d g : Nat → R) (hg : ∀ m, O.le (g (m + 1)) (g m)) :
    ∀ N, (∀ m, m ≤ N → O.le K.zero (sumN K d m)) →
      O.le (K.mul (sumN K d N) (g N)) (sumN K (fun j => K.mul (d j) (g j)) N)
  | 0, _ => by show O.le (K.mul K.zero (g 0)) K.zero; rw [K.mul_comm, DetChain.mul_zero]; exact O.le_refl _
  | N + 1, h => by
    show O.le (K.mul (K.add (sumN K d N) (d N)) (g (N + 1))) (K.add (sumN K (fun j => K.mul (d j) (g j)) N) (K.mul (d N) (g N)))
    have ih := abel_aux d g hg N (fun m hm => h m (Nat.le_trans hm (Nat.le_succ N)))
    have s1 := O.mul_le_mul_right _ _ _ (hg N) (h (N + 1) (Nat.le_refl _))
    have s2 := O.add_le_add _ _ _ _ ih (O.le_refl (K.mul (d N) (g N)))
    have e1 : K.mul (sumN K d (N + 1)) (g (N + 1)) = K.mul (g (N + 1)) (K.add (sumN K d N) (d N)) := by
      show K.mul (K.add (sumN K d N) (d N)) (g (N + 1)) = _; lib_ring
    have e2 : K.mul (g N) (sumN K d (N + 1)) = K.add (K.mul (sumN K d N) (g N)) (K.mul (d N) (g N)) := by
      show K.mul (g N) (K.add (sumN K d N) (d N)) = _; 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 _ _ e2) s2))
    show K.mul (K.add (sumN K d N) (d N)) (g (N + 1)) = K.mul (g (N + 1)) (sumN K d (N + 1))
    show _ = K.mul (g (N + 1)) (K.add (sumN K d N) (d N)); lib_ring

/-- **Abel's inequality** -/
theorem abel (d g : Nat → R) (hg : ∀ m, O.le (g (m + 1)) (g m)) (hg0 : ∀ m, O.le K.zero (g m)) (N : Nat)
    (h : ∀ m, m ≤ N → O.le K.zero (sumN K d m)) : O.le K.zero (sumN K (fun j => K.mul (d j) (g j)) N) :=
  O.le_trans _ _ _ (O.mul_nonneg _ _ (h N (Nat.le_refl N)) (hg0 N)) (abel_aux K O d g hg N h)

theorem sum_swap (D : Nat → Nat → R) (B : Nat) : ∀ A,
    sumN K (fun j => sumN K (fun k => D j k) B) A = sumN K (fun k => sumN K (fun j => D j k) A) B
  | 0 => by
    show K.zero = sumN K (fun k => K.zero) B
    induction B with
    | zero => rfl
    | succ B ih => show K.zero = K.add (sumN K (fun _ => K.zero) B) K.zero; rw [← ih]; lib_ring
  | A + 1 => by
    show K.add (sumN K (fun j => sumN K (fun k => D j k) B) A) (sumN K (fun k => D A k) B) =
      sumN K (fun k => K.add (sumN K (fun j => D j k) A) (D A k)) B
    rw [sum_swap D B A, sumN_add]

/-- **two-variable Abel inequality**: nonnegative rectangle sums give Σ D_{jk} g_j g_k ≥ 0 -/
theorem abel2 (D : Nat → Nat → R) (g : Nat → R) (hg : ∀ m, O.le (g (m + 1)) (g m)) (hg0 : ∀ m, O.le K.zero (g m))
    (A B : Nat) (h : ∀ a b, a ≤ A → b ≤ B → O.le K.zero (sumN K (fun j => sumN K (fun k => D j k) b) a)) :
    O.le K.zero (sumN K (fun j => K.mul (sumN K (fun k => K.mul (D j k) (g k)) B) (g j)) A) := by
  apply abel K O _ g hg hg0 A
  intro a ha
  rw [sum_swap K (fun j k => K.mul (D j k) (g k)) B a]
  have e : ∀ k, sumN K (fun j => K.mul (D j k) (g k)) a = K.mul (sumN K (fun j => D j k) a) (g k) := by
    intro k
    rw [sumN_ext K _ (fun j => K.mul (g k) (D j k)) (fun j => K.mul_comm _ _) a, sumN_mul, K.mul_comm]
  rw [sumN_ext K _ _ e B]
  apply abel K O _ g hg hg0 B
  intro b hb
  rw [← sum_swap K D b a]
  exact h a b ha (Nat.le_trans hb (Nat.le_refl B))

end AbelLR

#print axioms AbelLR.abel_aux
#print axioms AbelLR.abel
#print axioms AbelLR.sum_swap
#print axioms AbelLR.abel2

ym/ActivityDecay.lean

lean_all/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

ym/AltDiff.lean

lean_all/ym/AltDiff.lean

/-
  ym/AltDiff — T45, G78m (β → ∞ cell, size side): the two-sided alternating sum of an even sequence and its central
  second difference. For f on ℕ (read as an even function on ℤ) put
    T_N(f) = f 0 + 2 Σ_{k=1}^{N} (−1)^k f k,     (Df) 0 = 2(f 1 − f 0),  (Df)(k+1) = f k − 2 f(k+1) + f(k+2).
  Then  T_N(Df) + 4 T_N(f) = 2 (−1)^N (f N + f(N+1))           (alt_second)
  so T(f) = −¼ T(Df) + boundary, and j iterations give T(f) = (−¼)^j T(D^j f) + boundary terms (alt_iter).
  Used for ϑ₄(x) = Σ_k (−1)^k x^{k²} (difference of the even and odd half-lattice Gaussian sums of the heat-kernel
  product) without Poisson summation. Lean 4 core only, no axioms.
-/
import ym.AltSum
import ym.Signed

namespace AltDiff

open YMChain

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

/-- (−1)^n -/
def sg : Nat → R
  | 0 => K.one
  | n + 1 => K.neg (sg n)

/-- 2 -/
def two : R := K.add K.one K.one

/-- T_N(f) = f 0 + 2 Σ_{k=1}^{N} (−1)^k f k -/
def tS (f : Nat → R) : Nat → R
  | 0 => f 0
  | N + 1 => K.add (tS f N) (K.mul (two K) (K.mul (sg K (N + 1)) (f (N + 1))))

/-- central second difference of the even extension -/
def dd (f : Nat → R) : Nat → R
  | 0 => K.mul (two K) (K.add (f 1) (K.neg (f 0)))
  | k + 1 => K.add (K.add (f k) (K.neg (K.mul (two K) (f (k + 1))))) (f (k + 2))

/-- **second-difference identity**: T_N(Df) + 4 T_N(f) = 2(−1)^N (f N + f(N+1)) -/
theorem alt_second (f : Nat → R) : ∀ N, K.add (tS K (dd K f) N) (K.mul (K.mul (two K) (two K)) (tS K f N))
    = K.mul (two K) (K.mul (sg K N) (K.add (f N) (f (N + 1))))
  | 0 => by
    show K.add (K.mul (two K) (K.add (f 1) (K.neg (f 0)))) (K.mul (K.mul (two K) (two K)) (f 0))
      = K.mul (two K) (K.mul K.one (K.add (f 0) (f 1)))
    unfold two; lib_ring
  | N + 1 => by
    have ih := alt_second f N
    show K.add (K.add (tS K (dd K f) N) (K.mul (two K) (K.mul (K.neg (sg K N))
          (K.add (K.add (f N) (K.neg (K.mul (two K) (f (N + 1))))) (f (N + 2))))))
        (K.mul (K.mul (two K) (two K)) (K.add (tS K f N) (K.mul (two K) (K.mul (K.neg (sg K N)) (f (N + 1))))))
      = K.mul (two K) (K.mul (K.neg (sg K N)) (K.add (f (N + 1)) (f (N + 2))))
    have e : K.add (K.add (tS K (dd K f) N) (K.mul (two K) (K.mul (K.neg (sg K N))
          (K.add (K.add (f N) (K.neg (K.mul (two K) (f (N + 1))))) (f (N + 2))))))
        (K.mul (K.mul (two K) (two K)) (K.add (tS K f N) (K.mul (two K) (K.mul (K.neg (sg K N)) (f (N + 1))))))
        = K.add (K.add (tS K (dd K f) N) (K.mul (K.mul (two K) (two K)) (tS K f N)))
          (K.mul (two K) (K.mul (K.neg (sg K N)) (K.add (K.add (f N) (K.mul (two K) (f (N + 1)))) (f (N + 2))))) := by
      unfold two; lib_ring
    rw [e, ih]
    generalize sg K N = g; generalize f N = a; generalize f (N + 1) = b; generalize f (N + 2) = c
    show K.add (K.mul (two K) (K.mul g (K.add a b))) (K.mul (two K) (K.mul (K.neg g) (K.add (K.add a (K.mul (two K) b)) c)))
      = K.mul (two K) (K.mul (K.neg g) (K.add b c))
    unfold two; lib_ring

/-- j-fold central second difference -/
def ddj (f : Nat → R) : Nat → Nat → R
  | 0 => f
  | j + 1 => dd K (ddj f j)

/-- boundary of level j: 2(−1)^N (D^j f N + D^j f (N+1)) -/
def bd (f : Nat → R) (N j : Nat) : R := K.mul (two K) (K.mul (sg K N) (K.add (ddj K f j N) (ddj K f j (N + 1))))

/-- **iterated identity** (as a recursion): T(D^j f) = bd_j − 4·T(D^{j}… ) written step by step:
  T_N(D^{j+1} f) + 4 T_N(D^j f) = bd(f, N, j) -/
theorem alt_iter (f : Nat → R) (N j : Nat) :
    K.add (tS K (ddj K f (j + 1)) N) (K.mul (K.mul (two K) (two K)) (tS K (ddj K f j) N)) = bd K f N j :=
  alt_second K (ddj K f j) N

/-- 4^j -/
def p4 : Nat → R
  | 0 => K.one
  | j + 1 => K.mul (K.mul (two K) (two K)) (p4 j)

/-- accumulated boundary: B_0 = 0, B_{j+1} = 4 B_j + (−1)^j bd_j -/
def bs (f : Nat → R) (N : Nat) : Nat → R
  | 0 => K.zero
  | j + 1 => K.add (K.mul (K.mul (two K) (two K)) (bs f N j)) (K.mul (sg K j) (bd K f N j))

/-- **j-fold form**: 4^j T_N(f) = (−1)^j T_N(D^j f) + B_j -/
theorem alt_mul (f : Nat → R) (N : Nat) : ∀ j,
    K.mul (p4 K j) (tS K f N) = K.add (K.mul (sg K j) (tS K (ddj K f j) N)) (bs K f N j)
  | 0 => by
    show K.mul K.one (tS K f N) = K.add (K.mul K.one (tS K f N)) K.zero
    lib_ring
  | j + 1 => by
    have ih := alt_mul f N j
    have it := alt_iter K f N j
    show K.mul (K.mul (K.mul (two K) (two K)) (p4 K j)) (tS K f N)
      = K.add (K.mul (K.neg (sg K j)) (tS K (ddj K f (j + 1)) N))
          (K.add (K.mul (K.mul (two K) (two K)) (bs K f N j)) (K.mul (sg K j) (bd K f N j)))
    rw [← it]
    have e : K.mul (K.mul (K.mul (two K) (two K)) (p4 K j)) (tS K f N)
        = K.mul (K.mul (two K) (two K)) (K.mul (p4 K j) (tS K f N)) := by lib_ring
    rw [e, ih]
    generalize tS K (ddj K f (j + 1)) N = A; generalize tS K (ddj K f j) N = B; generalize bs K f N j = C
    generalize sg K j = g; generalize two K = t
    lib_ring

variable (O : ORing K)

theorem neg_neg' (a : R) : K.neg (K.neg a) = a := by lib_ring

/-- a sign keeps a two-sided bound -/
theorem sg_bnd (u g : R) (h1 : O.le (K.neg u) g) (h2 : O.le g u) :
    ∀ n, O.le (K.neg u) (K.mul (sg K n) g) ∧ O.le (K.mul (sg K n) g) u
  | 0 => by
    have e : K.mul (sg K 0) g = g := by show K.mul K.one g = g; lib_ring
    rw [e]; exact ⟨h1, h2⟩
  | n + 1 => by
    obtain ⟨a, b⟩ := sg_bnd u g h1 h2 n
    have e : K.mul (sg K (n + 1)) g = K.neg (K.mul (sg K n) g) := by show K.mul (K.neg (sg K n)) g = _; lib_ring
    rw [e]
    refine ⟨Signed.neg_le_neg K O _ _ b, ?_⟩
    have c := Signed.neg_le_neg K O _ _ a
    rw [neg_neg'] at c; exact c

/-- sum of absolute bounds U_N = u 0 + 2 Σ_{k=1}^{N} u k -/
def tU (u : Nat → R) : Nat → R
  | 0 => u 0
  | N + 1 => K.add (tU u N) (K.mul (two K) (u (N + 1)))

/-- **|T_N(g)| ≤ U_N** for |g_k| ≤ u_k -/
theorem t_bnd (g u : Nat → R) (h : ∀ k, O.le (K.neg (u k)) (g k) ∧ O.le (g k) (u k)) :
    ∀ N, O.le (K.neg (tU K u N)) (tS K g N) ∧ O.le (tS K g N) (tU K u N)
  | 0 => h 0
  | N + 1 => by
    obtain ⟨a, b⟩ := t_bnd g u h N
    obtain ⟨c, d⟩ := sg_bnd K O (u (N + 1)) (g (N + 1)) (h (N + 1)).1 (h (N + 1)).2 (N + 1)
    have c2 := O.add_le_add _ _ _ _ c c
    have d2 := O.add_le_add _ _ _ _ d d
    have e1 : ∀ x : R, K.add x x = K.mul (two K) x := fun x => by unfold two; lib_ring
    rw [e1, e1] at d2; rw [e1, e1] at c2
    constructor
    · have := O.add_le_add _ _ _ _ a c2
      have e : K.add (K.neg (tU K u N)) (K.mul (two K) (K.neg (u (N + 1))))
          = K.neg (K.add (tU K u N) (K.mul (two K) (u (N + 1)))) := by lib_ring
      rw [e] at this; exact this
    · exact O.add_le_add _ _ _ _ b d2

end AltDiff

#print axioms AltDiff.alt_second
#print axioms AltDiff.alt_iter
#print axioms AltDiff.alt_mul
#print axioms AltDiff.sg_bnd
#print axioms AltDiff.t_bnd

ym/AltSum.lean

lean_all/ym/AltSum.lean

/-
  ym/AltSum — T45, G44 route 7 (discrete, no analysis): alternating sums of monotone sequences.
  For a nonincreasing nonnegative list g₀ ≥ g₁ ≥ … ≥ 0:  0 ≤ g₀ − g₁ + g₂ − … ≤ g₀.
  (Used to compare integer and half-integer Gaussian sums: their difference is an alternating sum on the half grid.)
  Lean 4 core only, no axioms.
-/
import ym.UFlow

namespace AltSum

open YMChain

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

/-- g₀ − g₁ + g₂ − … -/
def altS : List R → R
  | [] => K.zero
  | x :: r => K.add x (K.neg (altS r))

/-- nonincreasing and nonnegative -/
def Dec : List R → Prop
  | [] => True
  | [x] => O.le K.zero x
  | x :: y :: r => O.le y x ∧ Dec (y :: r)

def hd : List R → R
  | [] => K.zero
  | x :: _ => x

theorem le_sub (a b : R) (h : O.le b a) : O.le K.zero (K.add a (K.neg b)) := by
  have := O.add_le_add _ _ _ _ h (O.le_refl (K.neg b))
  rw [show K.add b (K.neg b) = K.zero by lib_ring] at this; exact this

theorem sub_le (a b : R) (h : O.le K.zero b) : O.le (K.add a (K.neg b)) a := by
  have := O.add_le_add _ _ _ _ (O.le_refl (K.add a (K.neg b))) h
  rw [DetChain.add_zero', show K.add (K.add a (K.neg b)) b = a by lib_ring] at this; exact this

/-- **alternating sum of a nonincreasing nonnegative list lies in [0, g₀]** -/
theorem alt_dec : ∀ l : List R, Dec K O l → O.le K.zero (altS K l) ∧ O.le (altS K l) (hd K l)
  | [], _ => ⟨O.le_refl _, O.le_refl _⟩
  | [x], h => by
    show O.le K.zero (K.add x (K.neg K.zero)) ∧ O.le (K.add x (K.neg K.zero)) x
    rw [show K.add x (K.neg K.zero) = x by lib_ring]; exact ⟨h, O.le_refl _⟩
  | x :: y :: r, h => by
    have ih := alt_dec (y :: r) h.2
    have hy : O.le (altS K (y :: r)) x := O.le_trans _ _ _ ih.2 h.1
    exact ⟨le_sub K O x _ hy, sub_le K O x _ ih.1⟩

/-- forward differences g_k − g_{k+1} -/
def dL : List R → List R
  | [] => []
  | [_] => []
  | x :: y :: r => K.add x (K.neg y) :: dL (y :: r)

/-- boundary term (−1)^n g_n (last entry with sign) -/
def bnd : List R → R
  | [] => K.zero
  | [x] => x
  | _ :: y :: r => K.neg (bnd (y :: r))

/-- **summation by parts for alternating sums**: 2·alt(g) = g₀ + (−1)ⁿgₙ + alt(Δg) -/
theorem two_alt : ∀ (x : R) (r : List R), K.add (altS K (x :: r)) (altS K (x :: r))
    = K.add (K.add x (bnd K (x :: r))) (altS K (dL K (x :: r)))
  | x, [] => by show K.add (K.add x (K.neg K.zero)) (K.add x (K.neg K.zero)) = K.add (K.add x x) K.zero; lib_ring
  | x, y :: r => by
    have ih := two_alt y r
    show K.add (K.add x (K.neg (altS K (y :: r)))) (K.add x (K.neg (altS K (y :: r))))
      = K.add (K.add x (K.neg (bnd K (y :: r)))) (K.add (K.add x (K.neg y)) (K.neg (altS K (dL K (y :: r)))))
    have e : K.add (K.add x (K.neg (altS K (y :: r)))) (K.add x (K.neg (altS K (y :: r))))
        = K.add (K.add x x) (K.neg (K.add (altS K (y :: r)) (altS K (y :: r)))) := by lib_ring
    rw [e, ih]
    generalize altS K (dL K (y :: r)) = D; generalize bnd K (y :: r) = B
    lib_ring

end AltSum

#print axioms AltSum.alt_dec
#print axioms AltSum.two_alt

ym/AnimalArea.lean

lean_all/ym/AnimalArea.lean

/-
  ym/AnimalArea — T27, cycle 49 (area law for ℤ₂, lattice-animal count, final): an explicit strong-coupling area-law bound.
  For t ≥ 0, a loop n, plaquettes P with every link in ≤ D plaquettes and every plaquette on ≤ w links, and A_min a lower
  bound for the size of every surface C ⊆ P with ∂C = n (hmin), the Wilson correlator satisfies
      G_W(P) ≤ ( Σ_{A = 0}^{|P|} 2^(D(|supp n| + wA) + 1) · [A ≥ A_min] t^A ) · G_0(P).
  With StrongZ2.wilson_le_surfaces this gives ⟨W⟩ ≤ 2^(D|supp n|+1) Σ_{A ≥ A_min} (2^(Dw) t)^A, volume-independent:
  for 2^(Dw) t < 1 the loop expectation decays exponentially in the minimal area.
    gA, GM_le_gA      a surface term is ≤ [A ≥ A_min] t^A
    sum_mono          pointwise ≤ on a list ⇒ ≤ of sums
    animal_sum_g      the injection with the weight gA
    nsm, sum_map_pair, sum_bindL     evaluation of the code-set sum
    len_masks2, upTo_le             |upTo M| ≤ 2^(M+1)
    nsm_mono, area_bound            the bound
  Lean 4 core only, proofs by hand, no axioms.
-/
import ym.AnimalSum

namespace AnimalArea

open YMChain LinkIntegral GKS StrongZ2 S2cMask S2cComp S2cIter S2cSplit S2cDec S2cIdem S2cFinal AnimalInj AnimalDeg AnimalSupp
  AnimalCode AnimalSum

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

def gA (Amin : Nat) (t : R) (A : Nat) : R := if Amin ≤ A then pwR K t A else K.zero

theorem gA_nn (h1 : O.le K.zero K.one) (Amin : Nat) (t : R) (ht : O.le K.zero t) (A : Nat) : O.le K.zero (gA K Amin t A) := by
  show O.le K.zero (if Amin ≤ A then pwR K t A else K.zero)
  by_cases h : Amin ≤ A
  · rw [FiniteRecChain.ite_pos' _ _ h]; exact pw_nn K O h1 t ht A
  · rw [FiniteRecChain.ite_neg' _ _ h]; exact O.le_refl _

theorem GM_le_gA (h1 : O.le K.zero K.one) (Amin : Nat) (t : R) (ht : O.le K.zero t) (n : List Nat) (P : List (List Nat))
    (C : List Bool) (hm : P.length = C.length) (hmin : evenAll (selS n P C) = true → Amin ≤ cnt C) :
    O.le (GM K t n P C) (gA K Amin t (cnt C)) := by
  rw [GM_eq K t n P C hm]
  show O.le (K.mul (pwR K t (cnt C)) (if evenAll (selS n P C) = true then K.one else K.zero)) _
  by_cases he : evenAll (selS n P C) = true
  · rw [FiniteRecChain.ite_pos' _ _ he, K.mul_comm, K.one_mul]
    show O.le _ (if Amin ≤ cnt C then pwR K t (cnt C) else K.zero)
    rw [FiniteRecChain.ite_pos' _ _ (hmin he)]
    exact O.le_refl _
  · rw [FiniteRecChain.ite_neg' _ _ he, DetChain.mul_zero K]
    exact gA_nn K O h1 Amin t ht _

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

theorem animal_sum_g (h1 : O.le K.zero K.one) (Amin : Nat) (t : R) (ht : O.le K.zero t) (P : List (List Nat)) (n : List Nat)
    (D w : Nat) (hD : allLe (degV P) D) (hw : allQ P w) :
    O.le (sumK K (fun C => gA K Amin t (cnt C)) (filt (conn P n) (masks P.length)))
      (sumK K (fun y => gA K Amin t y.1) (codeSet P n D w)) := by
  have hconn : ∀ C, memL C (filt (conn P n) (masks P.length)) → P.length = C.length ∧ (iter P C n P.length).1 = C := by
    intro C hC
    have r := mem_filt_inv _ C _ hC
    exact ⟨(len_masks _ C r.1).symm, of_decide_eq_true r.2⟩
  refine InjSum.inj_le K O (fun y : Nat × List Bool => gA K Amin t y.1) (fun y => gA_nn K O h1 Amin t ht y.1)
    (fun C => (cnt C, code P C n)) _ _ (distinct_filt _ _ (distinct_masks _)) ?_ ?_
  · intro C hC
    obtain ⟨hm, hc⟩ := hconn C hC
    have hA : cnt C ≤ P.length := by rw [hm]; exact AnimalSum.cnt_le_len C
    exact mem_bindL _ (cnt C, code P C n) (cnt C)
      (mem_map (fun b => (cnt C, b)) (code P C n) _ (mem_upTo _ _ (code_short P C n D w hm hD hw hc)))
      _ (mem_rangeL _ _ hA)
  · intro C C' hC hC' e
    obtain ⟨hm, hc⟩ := hconn C hC
    obtain ⟨hm', hc'⟩ := hconn C' hC'
    exact code_inj P n C C' hm hm' hc hc' (congrArg (fun y : Nat × List Bool => y.2) e)

def nsm : Nat → R → R
  | 0, _ => K.zero
  | k + 1, x => K.add x (nsm k x)

theorem sum_map_pair (g : Nat → R) (A : Nat) : ∀ l : List (List Bool),
    sumK K (fun y : Nat × List Bool => g y.1) (l.map (fun b => (A, b))) = nsm K l.length (g A)
  | [] => rfl
  | _ :: l => by
    show K.add (g A) (sumK K (fun y : Nat × List Bool => g y.1) (l.map (fun b => (A, b)))) = K.add (g A) (nsm K l.length (g A))
    rw [sum_map_pair g A l]

theorem sum_bindL (g : Nat → R) (L : Nat → Nat) : ∀ as : List Nat,
    sumK K (fun y : Nat × List Bool => g y.1) (bindL as (fun A => (upTo (L A)).map (fun b => (A, b)))) =
      sumK K (fun A => nsm K (upTo (L A)).length (g A)) as
  | [] => rfl
  | A :: as => by
    show sumK K (fun y : Nat × List Bool => g y.1) ((upTo (L A)).map (fun b => (A, b)) ++ bindL as (fun A => (upTo (L A)).map (fun b => (A, b)))) = _
    rw [sumK_append, sum_map_pair, sum_bindL g L as]; rfl

theorem nsm_mono (x : R) (hx : O.le K.zero x) : ∀ a b : Nat, a ≤ b → O.le (nsm K a x) (nsm K b x)
  | 0, 0, _ => O.le_refl _
  | 0, b + 1, _ => by
    have := O.add_le_add _ _ _ _ hx (nsm_mono x hx 0 b (Nat.zero_le b))
    rw [K.zero_add] at this; exact this
  | _ + 1, 0, h => nomatch h
  | a + 1, b + 1, h => O.add_le_add _ _ _ _ (O.le_refl x) (nsm_mono x hx a b (Nat.le_of_succ_le_succ h))

end Main

theorem len_map2 {A B : Type} (f : A → B) : ∀ l : List A, (l.map f).length = l.length
  | [] => rfl
  | _ :: l => congrArg Nat.succ (len_map2 f l)

theorem len_masks2 : ∀ k, (masks k).length = 2 ^ k
  | 0 => rfl
  | k + 1 => by
    show ((masks k).map (fun m => false :: m) ++ (masks k).map (fun m => true :: m)).length = 2 ^ k * 2
    rw [AnimalLen.len_app, len_map2, len_map2, len_masks2 k]
    generalize 2 ^ k = u
    lib_nat

theorem upTo_le : ∀ M, (upTo M).length ≤ 2 ^ (M + 1)
  | 0 => Nat.le_succ 1
  | M + 1 => by
    show (masks (M + 1) ++ upTo M).length ≤ 2 ^ (M + 1) * 2
    rw [AnimalLen.len_app, len_masks2]
    have h := Nat.add_le_add_left (upTo_le M) (2 ^ (M + 1))
    have e : 2 ^ (M + 1) + 2 ^ (M + 1) = 2 ^ (M + 1) * 2 := by
      generalize 2 ^ (M + 1) = u
      lib_nat
    rw [← e]; exact h

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

/-- **Strong-coupling area law for ℤ₂ lattice gauge theory, explicit constants, any dimension.** -/
theorem area_bound (h1 : O.le K.zero K.one) (t : R) (ht : O.le K.zero t) (n : List Nat) (P : List (List Nat)) (D w Amin : Nat)
    (hD : allLe (degV P) D) (hw : allQ P w)
    (hmin : ∀ C : List Bool, P.length = C.length → evenAll (selS n P C) = true → Amin ≤ cnt C) :
    O.le (G K t n P)
      (K.mul (sumK K (fun A => nsm K (2 ^ (D * (nz n + w * A) + 1)) (gA K Amin t A)) (rangeL P.length)) (G K t [] P)) := by
  have hG0 : O.le K.zero (G K t [] P) := by
    rw [G_eq K t P []]
    exact InjSum.sum_nn K O _ (fun m => GM_nn' K O h1 t ht [] P m) _
  refine O.le_trans _ _ _ (s2c K O h1 t ht n P) (O.mul_le_mul_right _ _ _ ?_ hG0)
  have s1 : O.le (sumK K (fun C => GM K t n P C) (filt (conn P n) (masks P.length)))
      (sumK K (fun C => gA K Amin t (cnt C)) (filt (conn P n) (masks P.length))) := by
    apply sum_mono K O
    intro C hC
    have hm : P.length = C.length := (len_masks _ C (mem_filt_inv _ C _ hC).1).symm
    exact GM_le_gA K O h1 Amin t ht n P C hm (hmin C hm)
  have s2 := animal_sum_g K O h1 Amin t ht P n D w hD hw
  have s3 : sumK K (fun y => gA K Amin t y.1) (codeSet P n D w) =
      sumK K (fun A => nsm K (upTo (D * (nz n + w * A))).length (gA K Amin t A)) (rangeL P.length) :=
    sum_bindL K (gA K Amin t) (fun A => D * (nz n + w * A)) _
  have s4 := sum_mono K O (fun A => nsm K (upTo (D * (nz n + w * A))).length (gA K Amin t A))
    (fun A => nsm K (2 ^ (D * (nz n + w * A) + 1)) (gA K Amin t A)) (rangeL P.length)
    (fun A _ => nsm_mono K O _ (gA_nn K O h1 Amin t ht A) _ _ (upTo_le _))
  rw [s3] at s2
  exact O.le_trans _ _ _ s1 (O.le_trans _ _ _ s2 s4)

end Final

end AnimalArea

#print axioms AnimalArea.gA
#print axioms AnimalArea.gA_nn
#print axioms AnimalArea.GM_le_gA
#print axioms AnimalArea.sum_mono
#print axioms AnimalArea.animal_sum_g
#print axioms AnimalArea.nsm
#print axioms AnimalArea.sum_map_pair
#print axioms AnimalArea.sum_bindL
#print axioms AnimalArea.nsm_mono
#print axioms AnimalArea.len_map2
#print axioms AnimalArea.len_masks2
#print axioms AnimalArea.upTo_le
#print axioms AnimalArea.area_bound

ym/AnimalCode.lean

lean_all/ym/AnimalCode.lean

/-
  ym/AnimalCode — T27, cycle 47 (area law, lattice-animal count, step 8): the code of a connected mask is short.
    code_short    every link in ≤ D plaquettes, every plaquette on ≤ w links, comp C = C ⇒ |code C| ≤ D·(|supp n| + w·#C)
  Together with code_inj: connected masks C inject into bit strings of length ≤ D·(|supp n| + w·#C).
  Lean 4 core only, proofs by hand, no axioms.
-/
import ym.AnimalSupp

namespace AnimalCode

open YMChain GKS S2cComp S2cIter AnimalInj AnimalLen AnimalTouch AnimalDeg AnimalSupp

/-- **|code C| ≤ D·(|supp n| + w·#C) for a connected mask C.** -/
theorem code_short (P : List (List Nat)) (C : List Bool) (n : List Nat) (D w : Nat) (hm : P.length = C.length)
    (hD : allLe (degV P) D) (hw : allQ P w) (hc : (iter P C n P.length).1 = C) :
    (code P C n).length ≤ D * (nz n + w * cnt C) := by
  rw [code_len P C n hm]
  have he := examined_le P (riter P C n P.length).2.1 (iter P C n P.length).2 D
    ((riter_ok P C n hm P.length).2.2.1).symm (examined_touch P C n hm) hD
  have hs := iter_supp w P C n hm hw P.length
  rw [hc] at hs
  exact Nat.le_trans he (Nat.mul_le_mul_left D hs)

end AnimalCode

#print axioms AnimalCode.code_short

ym/AnimalDec.lean

lean_all/ym/AnimalDec.lean

/-
  ym/AnimalDec — T27, cycle 41 (area law, lattice-animal count, step 2): decoding.
    bitsOf P m c e s        the bits the recording sweep writes (one per first examination: the mask bit)
    dsweep P bits c e s     the same sweep, reading the bits instead of the mask
    dec_ok                  dsweep P (bitsOf P m c e s ++ rest) c e s = (c', e', s', rest), (c', e', s') those of rsweep
  So the swept selection is a function of the bit string alone.
  Lean 4 core only, proofs by hand, no axioms.
-/
import ym.AnimalRec

namespace AnimalDec

open YMChain GKS S2cComp AnimalRec

def bitsOf : List (List Nat) → List Bool → List Bool → List Bool → List Nat → List Bool
  | [], _, _, _, _ => []
  | _ :: _, [], _, _, _ => []
  | _ :: _, _ :: _, [], _, _ => []
  | _ :: _, _ :: _, _ :: _, [], _ => []
  | q :: P, b :: m, _ :: c, y :: e, s =>
    if (!y && touch q s) = true then b :: (if b = true then bitsOf P m c e (zadd s q) else bitsOf P m c e s)
    else bitsOf P m c e s

def dsweep : List (List Nat) → List Bool → List Bool → List Bool → List Nat → List Bool × List Bool × List Nat × List Bool
  | [], bits, c, e, s => (c, e, s, bits)
  | _ :: _, bits, [], e, s => ([], e, s, bits)
  | _ :: _, bits, _ :: _, [], s => ([], [], s, bits)
  | q :: P, bits, x :: c, y :: e, s =>
    if (!y && touch q s) = true then
      if bits.headD false = true then ((true :: (dsweep P bits.tail c e (zadd s q)).1), (true :: (dsweep P bits.tail c e (zadd s q)).2.1), (dsweep P bits.tail c e (zadd s q)).2.2.1, (dsweep P bits.tail c e (zadd s q)).2.2.2) else ((x :: (dsweep P bits.tail c e s).1), (true :: (dsweep P bits.tail c e s).2.1), (dsweep P bits.tail c e s).2.2.1, (dsweep P bits.tail c e s).2.2.2)
    else ((x :: (dsweep P bits c e s).1), (y :: (dsweep P bits c e s).2.1), (dsweep P bits c e s).2.2.1, (dsweep P bits c e s).2.2.2)

theorem ds_unf (q : List Nat) (P : List (List Nat)) (bits : List Bool) (x y : Bool) (c e : List Bool) (s : List Nat) :
    dsweep (q :: P) bits (x :: c) (y :: e) s =
      if (!y && touch q s) = true then (if bits.headD false = true then ((true :: (dsweep P bits.tail c e (zadd s q)).1), (true :: (dsweep P bits.tail c e (zadd s q)).2.1), (dsweep P bits.tail c e (zadd s q)).2.2.1, (dsweep P bits.tail c e (zadd s q)).2.2.2) else ((x :: (dsweep P bits.tail c e s).1), (true :: (dsweep P bits.tail c e s).2.1), (dsweep P bits.tail c e s).2.2.1, (dsweep P bits.tail c   -- (+149 chars; full line in the Zenodo source)

theorem bits_add (q : List Nat) (P : List (List Nat)) (b x y : Bool) (m c e : List Bool) (s : List Nat)
    (ht : (!y && touch q s) = true) (hb : b = true) : bitsOf (q :: P) (b :: m) (x :: c) (y :: e) s = true :: bitsOf P m c e (zadd s q) := by
  show (if (!y && touch q s) = true then b :: (if b = true then bitsOf P m c e (zadd s q) else bitsOf P m c e s)
    else bitsOf P m c e s) = _
  rw [FiniteRecChain.ite_pos' _ _ ht, FiniteRecChain.ite_pos' _ _ hb, hb]

theorem bits_rej (q : List Nat) (P : List (List Nat)) (b x y : Bool) (m c e : List Bool) (s : List Nat)
    (ht : (!y && touch q s) = true) (hb : ¬ b = true) : bitsOf (q :: P) (b :: m) (x :: c) (y :: e) s = false :: bitsOf P m c e s := by
  show (if (!y && touch q s) = true then b :: (if b = true then bitsOf P m c e (zadd s q) else bitsOf P m c e s)
    else bitsOf P m c e s) = _
  rw [FiniteRecChain.ite_pos' _ _ ht, FiniteRecChain.ite_neg' _ _ hb]
  cases b with
  | false => rfl
  | true => exact absurd rfl hb

theorem bits_skip (q : List Nat) (P : List (List Nat)) (b x y : Bool) (m c e : List Bool) (s : List Nat)
    (ht : ¬ (!y && touch q s) = true) : bitsOf (q :: P) (b :: m) (x :: c) (y :: e) s = bitsOf P m c e s := by
  show (if (!y && touch q s) = true then b :: (if b = true then bitsOf P m c e (zadd s q) else bitsOf P m c e s)
    else bitsOf P m c e s) = _
  rw [FiniteRecChain.ite_neg' _ _ ht]

/-- **Decoding the bit string reproduces the recording sweep.** -/
theorem dec_ok : ∀ (P : List (List Nat)) (m c e : List Bool) (s : List Nat) (out rest : List Bool),
    P.length = m.length → m.length = c.length → c.length = e.length →
    dsweep P (bitsOf P m c e s ++ rest) c e s =
      ((rsweep P m c e s out).1, (rsweep P m c e s out).2.1, (rsweep P m c e s out).2.2.1, rest)
  | [], [], [], [], _, _, _, _, _, _ => rfl
  | q :: P, b :: m, x :: c, y :: e, s, out, rest, h1, h2, h3 => by
    have h1' : P.length = m.length := Nat.succ.inj h1
    have h2' : m.length = c.length := Nat.succ.inj h2
    have h3' : c.length = e.length := Nat.succ.inj h3
    by_cases ht : (!y && touch q s) = true
    · by_cases hb : b = true
      · rw [bits_add q P b x y m c e s ht hb, show (true :: bitsOf P m c e (zadd s q)) ++ rest = true :: (bitsOf P m c e (zadd s q) ++ rest) from rfl, rs_add q P b x y m c e s out ht hb, ds_unf, FiniteRecChain.ite_pos' _ _ ht,
          FiniteRecChain.ite_pos' _ _ (show (true :: (bitsOf P m c e (zadd s q) ++ rest)).headD false = true from rfl)]
        rw [show (true :: (bitsOf P m c e (zadd s q) ++ rest)).tail = bitsOf P m c e (zadd s q) ++ rest from rfl, dec_ok P m c e (zadd s q) (out ++ [true]) rest h1' h2' h3']
      · rw [bits_rej q P b x y m c e s ht hb, show (false :: bitsOf P m c e s) ++ rest = false :: (bitsOf P m c e s ++ rest) from rfl, rs_rej q P b x y m c e s out ht hb, ds_unf, FiniteRecChain.ite_pos' _ _ ht,
          FiniteRecChain.ite_neg' _ _ (show ¬ (false :: (bitsOf P m c e s ++ rest)).headD false = true from fun e => Bool.noConfusion e)]
        rw [show (false :: (bitsOf P m c e s ++ rest)).tail = bitsOf P m c e s ++ rest from rfl, dec_ok P m c e s (out ++ [false]) rest h1' h2' h3']
    · rw [bits_skip q P b x y m c e s ht, rs_skip q P b x y m c e s out ht, ds_unf, FiniteRecChain.ite_neg' _ _ ht,
        dec_ok P m c e s out rest h1' h2' h3']

end AnimalDec

#print axioms AnimalDec.bitsOf
#print axioms AnimalDec.dsweep
#print axioms AnimalDec.ds_unf
#print axioms AnimalDec.bits_add
#print axioms AnimalDec.bits_rej
#print axioms AnimalDec.bits_skip
#print axioms AnimalDec.dec_ok

ym/AnimalDeg.lean

lean_all/ym/AnimalDeg.lean

/-
  ym/AnimalDeg — T27, cycle 45 (area law, lattice-animal count, step 6): plaquettes touching a support, counted by link degree.
    nzv, nz       the 0/1 support indicator and its size
    dot, degV     link-wise products; degV P = number of plaquettes of P on each link
    touch_ov      a touching plaquette shares at least one link with S
    dot_zadd, sum_ov   Σ_q (shared links of q and S) = Σ_{links of S} deg
    dot_le        entries ≤ D ⇒ dot a v ≤ D · Σ a
    examined_le   (exT P e S, every link in ≤ D plaquettes) #e ≤ D · |supp S|
  Lean 4 core only, proofs by hand, no axioms.
-/
import ym.AnimalTouch

namespace AnimalDeg

open YMChain GKS S2cComp AnimalTouch

def nzv : List Nat → List Nat
  | [] => []
  | a :: s => (if a = 0 then 0 else 1) :: nzv s

def sumN : List Nat → Nat
  | [] => 0
  | a :: s => a + sumN s

def nz (s : List Nat) : Nat := sumN (nzv s)

def dot : List Nat → List Nat → Nat
  | [], _ => 0
  | _ :: _, [] => 0
  | a :: as, b :: bs => a * b + dot as bs

def degV : List (List Nat) → List Nat
  | [] => []
  | q :: P => zadd (nzv q) (degV P)

def allLe : List Nat → Nat → Prop
  | [], _ => True
  | x :: v, D => x ≤ D ∧ allLe v D

theorem one_of (a : Nat) (h : (a != 0) = true) : (if a = 0 then 0 else 1) = 1 :=
  FiniteRecChain.ite_neg' _ _ (bne_ne a h)

theorem touch_ov : ∀ (q S : List Nat), touch q S = true → 1 ≤ dot (nzv S) (nzv q)
  | [], _, h => absurd h (fun e => Bool.noConfusion e)
  | _ :: _, [], h => absurd h (fun e => Bool.noConfusion e)
  | a :: q, b :: S, h => by
    show 1 ≤ (if b = 0 then 0 else 1) * (if a = 0 then 0 else 1) + dot (nzv S) (nzv q)
    rcases or_cases _ _ h with h' | h'
    · rw [one_of b (and_r _ _ h'), one_of a (and_l _ _ h')]
      exact Nat.le_add_right 1 _
    · exact Nat.le_trans (touch_ov q S h') (Nat.le_add_left _ _)

def sumOv : List (List Nat) → List Nat → Nat
  | [], _ => 0
  | q :: P, S => dot (nzv S) (nzv q) + sumOv P S

theorem cnt_le_ov : ∀ (P : List (List Nat)) (e : List Bool) (S : List Nat), P.length = e.length → exT P e S → cnt e ≤ sumOv P S
  | [], [], _, _, _ => Nat.le_refl 0
  | q :: P, y :: e, S, h, hE => by
    show (if y = true then 1 else 0) + cnt e ≤ dot (nzv S) (nzv q) + sumOv P S
    apply Nat.add_le_add _ (cnt_le_ov P e S (Nat.succ.inj h) hE.2)
    by_cases hy : y = true
    · rw [FiniteRecChain.ite_pos' _ _ hy]; exact touch_ov q S (hE.1 hy)
    · rw [FiniteRecChain.ite_neg' _ _ hy]; exact Nat.zero_le _

theorem dot_nil : ∀ a : List Nat, dot a [] = 0
  | [] => rfl
  | _ :: _ => rfl

theorem dot_zadd : ∀ (a b c : List Nat), dot a (zadd b c) = dot a b + dot a c
  | [], _, _ => rfl
  | _ :: _, [], c => (Nat.zero_add _).symm
  | _ :: _, _ :: _, [] => rfl
  | x :: a, y :: b, z :: c => by
    show x * (y + z) + dot a (zadd b c) = (x * y + dot a b) + (x * z + dot a c)
    rw [dot_zadd a b c]
    generalize dot a b = u
    generalize dot a c = v
    lib_nat

theorem sum_ov : ∀ (P : List (List Nat)) (S : List Nat), sumOv P S = dot (nzv S) (degV P)
  | [], S => (dot_nil _).symm
  | q :: P, S => by
    show dot (nzv S) (nzv q) + sumOv P S = dot (nzv S) (zadd (nzv q) (degV P))
    rw [dot_zadd, sum_ov P S]

theorem dot_le (D : Nat) : ∀ (a v : List Nat), allLe v D → dot a v ≤ D * sumN a
  | [], _, _ => Nat.zero_le _
  | _ :: _, [], _ => Nat.zero_le _
  | x :: a, y :: v, h => by
    show x * y + dot a v ≤ D * (x + sumN a)
    rw [Nat.left_distrib]
    apply Nat.add_le_add _ (dot_le D a v h.2)
    rw [Nat.mul_comm D x]
    exact Nat.mul_le_mul_left x h.1

/-- **Examined plaquettes are at most D · |supp S|.** -/
theorem examined_le (P : List (List Nat)) (e : List Bool) (S : List Nat) (D : Nat)
    (hl : P.length = e.length) (hE : exT P e S) (hD : allLe (degV P) D) : cnt e ≤ D * nz S := by
  have := cnt_le_ov P e S hl hE
  rw [sum_ov] at this
  exact Nat.le_trans this (dot_le D _ _ hD)

end AnimalDeg

#print axioms AnimalDeg.nzv
#print axioms AnimalDeg.sumN
#print axioms AnimalDeg.nz
#print axioms AnimalDeg.dot
#print axioms AnimalDeg.degV
#print axioms AnimalDeg.allLe
#print axioms AnimalDeg.one_of
#print axioms AnimalDeg.touch_ov
#print axioms AnimalDeg.sumOv
#print axioms AnimalDeg.cnt_le_ov
#print axioms AnimalDeg.dot_nil
#print axioms AnimalDeg.dot_zadd
#print axioms AnimalDeg.sum_ov
#print axioms AnimalDeg.dot_le
#print axioms AnimalDeg.examined_le

ym/AnimalInj.lean

lean_all/ym/AnimalInj.lean

/-
  ym/AnimalInj — T27, cycle 42 (area law, lattice-animal count, step 3): connected masks are determined by their bit code.
    rs_inv, rs_len2   the recording sweep keeps the consistency invariant and the length of the examined mask
    riter             |P| recording sweeps from (∅, ∅, loop); riter_ok: it follows S2cIter.iter
    bitsIt / code     the concatenated bits of the sweeps
    diter, dec_iter   iterated decoding reproduces riter from the code alone
    code_inj          two connected masks (comp C = C) with the same code are equal
  Lean 4 core only, proofs by hand, no axioms.
-/
import ym.AnimalDec

namespace AnimalInj

open YMChain GKS S2cComp S2cIter AnimalRec AnimalDec

theorem app_assoc {A : Type} : ∀ (a b c : List A), (a ++ b) ++ c = a ++ (b ++ c)
  | [], _, _ => rfl
  | x :: a, b, c => congrArg (List.cons x) (app_assoc a b c)

theorem app_nil {A : Type} : ∀ (a : List A), a ++ [] = a
  | [] => rfl
  | x :: a => congrArg (List.cons x) (app_nil a)

theorem inv_falses : ∀ m : List Bool, Inv m (falses m.length) (falses m.length)
  | [] => trivial
  | _ :: m => ⟨fun h => Bool.noConfusion h, fun h => Bool.noConfusion h, inv_falses m⟩

theorem rs_inv : ∀ (P : List (List Nat)) (m c e : List Bool) (s : List Nat) (out : List Bool),
    P.length = m.length → m.length = c.length → c.length = e.length → Inv m c e →
    Inv m (rsweep P m c e s out).1 (rsweep P m c e s out).2.1
  | [], [], [], [], _, _, _, _, _, _ => trivial
  | q :: P, b :: m, x :: c, y :: e, s, out, h1, h2, h3, hI => by
    have h1' : P.length = m.length := Nat.succ.inj h1
    have h2' : m.length = c.length := Nat.succ.inj h2
    have h3' : c.length = e.length := Nat.succ.inj h3
    by_cases ht : (!y && touch q s) = true
    · by_cases hb : b = true
      · rw [rs_add q P b x y m c e s out ht hb]
        exact ⟨fun _ => rfl, fun _ h => Bool.noConfusion h, rs_inv P m c e (zadd s q) (out ++ [true]) h1' h2' h3' hI.2.2⟩
      · rw [rs_rej q P b x y m c e s out ht hb]
        have hbf : b = false := by
          cases b with
          | false => rfl
          | true => exact absurd rfl hb
        exact ⟨fun _ => rfl, fun _ _ => hbf, rs_inv P m c e s (out ++ [false]) h1' h2' h3' hI.2.2⟩
    · rw [rs_skip q P b x y m c e s out ht]
      exact ⟨hI.1, hI.2.1, rs_inv P m c e s out h1' h2' h3' hI.2.2⟩

theorem rs_len2 : ∀ (P : List (List Nat)) (m c e : List Bool) (s : List Nat) (out : List Bool),
    P.length = m.length → m.length = c.length → c.length = e.length → (rsweep P m c e s out).2.1.length = e.length
  | [], [], [], [], _, _, _, _, _ => rfl
  | q :: P, b :: m, x :: c, y :: e, s, out, h1, h2, h3 => by
    have h1' : P.length = m.length := Nat.succ.inj h1
    have h2' : m.length = c.length := Nat.succ.inj h2
    have h3' : c.length = e.length := Nat.succ.inj h3
    by_cases ht : (!y && touch q s) = true
    · by_cases hb : b = true
      · rw [rs_add q P b x y m c e s out ht hb]
        exact congrArg Nat.succ (rs_len2 P m c e (zadd s q) (out ++ [true]) h1' h2' h3')
      · rw [rs_rej q P b x y m c e s out ht hb]
        exact congrArg Nat.succ (rs_len2 P m c e s (out ++ [false]) h1' h2' h3')
    · rw [rs_skip q P b x y m c e s out ht]
      exact congrArg Nat.succ (rs_len2 P m c e s out h1' h2' h3')

def riter (P : List (List Nat)) (m : List Bool) (n : List Nat) : Nat → List Bool × List Bool × List Nat
  | 0 => (falses P.length, falses P.length, n)
  | k + 1 => ((rsweep P m (riter P m n k).1 (riter P m n k).2.1 (riter P m n k).2.2 []).1,
      (rsweep P m (riter P m n k).1 (riter P m n k).2.1 (riter P m n k).2.2 []).2.1,
      (rsweep P m (riter P m n k).1 (riter P m n k).2.1 (riter P m n k).2.2 []).2.2.1)

theorem riter_ok (P : List (List Nat)) (m : List Bool) (n : List Nat) (hm : P.length = m.length) : ∀ k,
    (riter P m n k).1 = (iter P m n k).1 ∧ (riter P m n k).2.2 = (iter P m n k).2 ∧
    (riter P m n k).2.1.length = P.length ∧ Inv m (riter P m n k).1 (riter P m n k).2.1
  | 0 => ⟨rfl, rfl, len_falses _, by show Inv m (falses P.length) (falses P.length); rw [hm]; exact inv_falses m⟩
  | k + 1 => by
    obtain ⟨h1, h2, h3, h4⟩ := riter_ok P m n hm k
    have hc : (riter P m n k).1.length = P.length := by rw [h1]; exact iter_len P m n hm k
    have L2 : m.length = (riter P m n k).1.length := by rw [hc, hm]
    have L3 : (riter P m n k).1.length = (riter P m n k).2.1.length := by rw [hc, h3]
    have A := rsweep_agree P m _ _ (riter P m n k).2.2 [] hm L2 L3 h4
    refine ⟨?_, ?_, ?_, rs_inv P m _ _ _ [] hm L2 L3 h4⟩
    · show (rsweep P m (riter P m n k).1 (riter P m n k).2.1 (riter P m n k).2.2 []).1 = (sweep P m (iter P m n k).1 (iter P m n k).2).1
      rw [A.1, h1, h2]
    · show (rsweep P m (riter P m n k).1 (riter P m n k).2.1 (riter P m n k).2.2 []).2.2.1 = (sweep P m (iter P m n k).1 (iter P m n k).2).2
      rw [A.2, h1, h2]
    · show (rsweep P m (riter P m n k).1 (riter P m n k).2.1 (riter P m n k).2.2 []).2.1.length = P.length
      rw [rs_len2 P m _ _ _ [] hm L2 L3, h3]

def bitsIt (P : List (List Nat)) (m : List Bool) (n : List Nat) : Nat → List Bool
  | 0 => []
  | k + 1 => bitsIt P m n k ++ bitsOf P m (riter P m n k).1 (riter P m n k).2.1 (riter P m n k).2.2

def diter (P : List (List Nat)) (bits : List Bool) (n : List Nat) : Nat → List Bool × List Bool × List Nat × List Bool
  | 0 => (falses P.length, falses P.length, n, bits)
  | k + 1 => dsweep P (diter P bits n k).2.2.2 (diter P bits n k).1 (diter P bits n k).2.1 (diter P bits n k).2.2.1

theorem dec_iter (P : List (List Nat)) (m : List Bool) (n : List Nat) (hm : P.length = m.length) : ∀ (k : Nat) (rest : List Bool),
    diter P (bitsIt P m n k ++ rest) n k = ((riter P m n k).1, (riter P m n k).2.1, (riter P m n k).2.2, rest)
  | 0, _ => rfl
  | k + 1, rest => by
    obtain ⟨h1, _, h3, _⟩ := riter_ok P m n hm k
    have hc : (riter P m n k).1.length = P.length := by rw [h1]; exact iter_len P m n hm k
    have L2 : m.length = (riter P m n k).1.length := by rw [hc, hm]
    have L3 : (riter P m n k).1.length = (riter P m n k).2.1.length := by rw [hc, h3]
    show dsweep P (diter P ((bitsIt P m n k ++ bitsOf P m (riter P m n k).1 (riter P m n k).2.1 (riter P m n k).2.2) ++ rest) n k).2.2.2
      (diter P ((bitsIt P m n k ++ bitsOf P m (riter P m n k).1 (riter P m n k).2.1 (riter P m n k).2.2) ++ rest) n k).1
      (diter P ((bitsIt P m n k ++ bitsOf P m (riter P m n k).1 (riter P m n k).2.1 (riter P m n k).2.2) ++ rest) n k).2.1
      (diter P ((bitsIt P m n k ++ bitsOf P m (riter P m n k).1 (riter P m n k).2.1 (riter P m n k).2.2) ++ rest) n k).2.2.1 = _
    rw [app_assoc, dec_iter P m n hm k]
    exact dec_ok P m _ _ _ [] rest hm L2 L3

def code (P : List (List Nat)) (m : List Bool) (n : List Nat) : List Bool := bitsIt P m n P.length

/-- **A connected mask (comp C = C) is determined by its bit code.** -/
theorem code_inj (P : List (List Nat)) (n : List Nat) (m m' : List Bool) (hm : P.length = m.length) (hm' : P.length = m'.length)
    (hc : (iter P m n P.length).1 = m) (hc' : (iter P m' n P.length).1 = m') (e : code P m n = code P m' n) : m = m' := by
  have d := dec_iter P m n hm P.length []
  have d' := dec_iter P m' n hm' P.length []
  rw [app_nil] at d d'
  have f : (riter P m n P.length).1 = (riter P m' n P.length).1 := by
    have g := congrArg (fun y : List Bool × List Bool × List Nat × List Bool => y.1) d
    have g' := congrArg (fun y : List Bool × List Bool × List Nat × List Bool => y.1) d'
    have ee : code P m n = bitsIt P m n P.length := rfl
    have ee' : code P m' n = bitsIt P m' n P.length := rfl
    rw [← ee] at g; rw [← ee', ← e] at g'
    exact g.symm.trans g'
  rw [(riter_ok P m n hm P.length).1, (riter_ok P m' n hm' P.length).1, hc, hc'] at f
  exact f

end AnimalInj

#print axioms AnimalInj.app_assoc
#print axioms AnimalInj.app_nil
#print axioms AnimalInj.inv_falses
#print axioms AnimalInj.rs_inv
#print axioms AnimalInj.rs_len2
#print axioms AnimalInj.riter
#print axioms AnimalInj.riter_ok
#print axioms AnimalInj.bitsIt
#print axioms AnimalInj.diter
#print axioms AnimalInj.dec_iter
#print axioms AnimalInj.code
#print axioms AnimalInj.code_inj

ym/AnimalLen.lean

lean_all/ym/AnimalLen.lean

/-
  ym/AnimalLen — T27, cycle 43 (area law, lattice-animal count, step 4): the code length is the number of examined plaquettes.
    bits_len    |bitsOf P m c e s| + #e = #(examined mask after the sweep)
    code_len    |code P m n| = #(examined mask after |P| sweeps)
  Lean 4 core only, proofs by hand, no axioms.
-/
import ym.AnimalInj

namespace AnimalLen

open YMChain GKS S2cComp S2cIter AnimalRec AnimalDec AnimalInj

theorem bits_len : ∀ (P : List (List Nat)) (m c e : List Bool) (s : List Nat) (out : List Bool),
    P.length = m.length → m.length = c.length → c.length = e.length →
    (bitsOf P m c e s).length + cnt e = cnt (rsweep P m c e s out).2.1
  | [], [], [], [], _, _, _, _, _ => Nat.zero_add _
  | q :: P, b :: m, x :: c, y :: e, s, out, h1, h2, h3 => by
    have h1' : P.length = m.length := Nat.succ.inj h1
    have h2' : m.length = c.length := Nat.succ.inj h2
    have h3' : c.length = e.length := Nat.succ.inj h3
    by_cases ht : (!y && touch q s) = true
    · have hy : y = false := by
        cases y with
        | false => rfl
        | true => exact absurd ht (fun e => Bool.noConfusion e)
      by_cases hb : b = true
      · rw [bits_add q P b x y m c e s ht hb, rs_add q P b x y m c e s out ht hb, hy]
        have ih := bits_len P m c e (zadd s q) (out ++ [true]) h1' h2' h3'
        show ((bitsOf P m c e (zadd s q)).length + 1) + (0 + cnt e) = 1 + cnt (rsweep P m c e (zadd s q) (out ++ [true])).2.1
        rw [← ih]; lib_nat
      · rw [bits_rej q P b x y m c e s ht hb, rs_rej q P b x y m c e s out ht hb, hy]
        have ih := bits_len P m c e s (out ++ [false]) h1' h2' h3'
        show ((bitsOf P m c e s).length + 1) + (0 + cnt e) = 1 + cnt (rsweep P m c e s (out ++ [false])).2.1
        rw [← ih]; lib_nat
    · rw [bits_skip q P b x y m c e s ht, rs_skip q P b x y m c e s out ht]
      have ih := bits_len P m c e s out h1' h2' h3'
      show (bitsOf P m c e s).length + ((if y = true then 1 else 0) + cnt e) = (if y = true then 1 else 0) + cnt (rsweep P m c e s out).2.1
      rw [← ih]
      generalize (if y = true then 1 else 0) = z
      lib_nat

theorem len_app {A : Type} : ∀ (a b : List A), (a ++ b).length = a.length + b.length
  | [], b => (Nat.zero_add _).symm
  | _ :: a, b => by
    show (a ++ b).length + 1 = (a.length + 1) + b.length
    rw [len_app a b]; lib_nat

theorem cnt_falses : ∀ k, cnt (falses k) = 0
  | 0 => rfl
  | k + 1 => by show 0 + cnt (falses k) = 0; rw [cnt_falses k]

theorem bitsIt_len (P : List (List Nat)) (m : List Bool) (n : List Nat) (hm : P.length = m.length) :
    ∀ k, (bitsIt P m n k).length = cnt (riter P m n k).2.1
  | 0 => (cnt_falses _).symm
  | k + 1 => by
    obtain ⟨h1, _, h3, _⟩ := riter_ok P m n hm k
    have hc : (riter P m n k).1.length = P.length := by rw [h1]; exact iter_len P m n hm k
    have L2 : m.length = (riter P m n k).1.length := by rw [hc, hm]
    have L3 : (riter P m n k).1.length = (riter P m n k).2.1.length := by rw [hc, h3]
    show (bitsIt P m n k ++ bitsOf P m (riter P m n k).1 (riter P m n k).2.1 (riter P m n k).2.2).length =
      cnt (rsweep P m (riter P m n k).1 (riter P m n k).2.1 (riter P m n k).2.2 []).2.1
    rw [len_app, bitsIt_len P m n hm k, ← bits_len P m _ _ _ [] hm L2 L3]
    lib_nat

/-- **The code length is the number of examined plaquettes.** -/
theorem code_len (P : List (List Nat)) (m : List Bool) (n : List Nat) (hm : P.length = m.length) :
    (code P m n).length = cnt (riter P m n P.length).2.1 :=
  bitsIt_len P m n hm P.length

end AnimalLen

#print axioms AnimalLen.bits_len
#print axioms AnimalLen.len_app
#print axioms AnimalLen.cnt_falses
#print axioms AnimalLen.bitsIt_len
#print axioms AnimalLen.code_len

ym/AnimalRec.lean

lean_all/ym/AnimalRec.lean

/-
  ym/AnimalRec — T27, cycle 40 (area law, lattice-animal count, step 1): the recording sweep.
  As S2cComp.sweep, but with a mask e of examined plaquettes: a plaquette not yet examined whose links meet the support is
  examined once, its mask bit b is written out, and it is added when b = true.
    rsweep P m c e s        → (c', e', s', bits written)
    rsweep_agree            (c', s') are those of sweep P m c s, provided every selected plaquette is examined (c ≤ e) and
                            every examined unselected plaquette has b = false (consistency)
  Decoding (next cycle) replays the same sweep reading the bits instead of the mask, so the mask restricted to the examined
  plaquettes — the whole connected component — is recovered from the bit string.
  Lean 4 core only, proofs by hand, no axioms.
-/
import ym.S2cFinal

namespace AnimalRec

open YMChain GKS S2cComp

def rsweep : List (List Nat) → List Bool → List Bool → List Bool → List Nat → List Bool → List Bool × List Bool × List Nat × List Bool
  | [], _, c, e, s, out => (c, e, s, out)
  | _ :: _, [], c, e, s, out => (c, e, s, out)
  | _ :: _, _ :: _, [], e, s, out => ([], e, s, out)
  | _ :: _, _ :: _, _ :: _, [], s, out => ([], [], s, out)
  | q :: P, b :: m, x :: c, y :: e, s, out =>
    if (!y && touch q s) = true then
      if b = true then
        ((true :: (rsweep P m c e (zadd s q) (out ++ [true])).1), (true :: (rsweep P m c e (zadd s q) (out ++ [true])).2.1),
          (rsweep P m c e (zadd s q) (out ++ [true])).2.2.1, (rsweep P m c e (zadd s q) (out ++ [true])).2.2.2)
      else
        ((x :: (rsweep P m c e s (out ++ [false])).1), (true :: (rsweep P m c e s (out ++ [false])).2.1),
          (rsweep P m c e s (out ++ [false])).2.2.1, (rsweep P m c e s (out ++ [false])).2.2.2)
    else
      ((x :: (rsweep P m c e s out).1), (y :: (rsweep P m c e s out).2.1), (rsweep P m c e s out).2.2.1, (rsweep P m c e s out).2.2.2)

/-- Consistency of (c, e) with the mask m: selected ⇒ examined, examined and unselected ⇒ not in m. -/
def Inv : List Bool → List Bool → List Bool → Prop
  | [], _, _ => True
  | _ :: _, [], _ => True
  | _ :: _, _ :: _, [] => True
  | b :: m, x :: c, y :: e => (x = true → y = true) ∧ (y = true → x = false → b = false) ∧ Inv m c e

theorem dec_cases (b x y t : Bool) (h1 : x = true → y = true) (h2 : y = true → x = false → b = false) :
    ((!y && t) = true ∧ b = true) ∨ ((!y && t) = true ∧ b = false ∧ x = false) ∨ ((!y && t) = false ∧ (b && !x && t) = false) := by
  cases b <;> cases x <;> cases y <;> cases t <;>
    first
    | exact Or.inl ⟨rfl, rfl⟩
    | exact Or.inr (Or.inl ⟨rfl, rfl, rfl⟩)
    | exact Or.inr (Or.inr ⟨rfl, rfl⟩)
    | exact absurd (h1 rfl) (fun e => Bool.noConfusion e)
    | exact absurd (h2 rfl rfl) (fun e => Bool.noConfusion e)


theorem rs_add (q : List Nat) (P : List (List Nat)) (b x y : Bool) (m c e : List Bool) (s : List Nat) (out : List Bool)
    (ht : (!y && touch q s) = true) (hb : b = true) : rsweep (q :: P) (b :: m) (x :: c) (y :: e) s out = ((true :: (rsweep P m c e (zadd s q) (out ++ [true])).1), (true :: (rsweep P m c e (zadd s q) (out ++ [true])).2.1), (rsweep P m c e (zadd s q) (out ++ [true])).2.2.1, (rsweep P m c e (zadd s q) (out ++ [true])).2.2.2) := by
  show (if (!y && touch q s) = true then (if b = true then ((true :: (rsweep P m c e (zadd s q) (out ++ [true])).1), (true :: (rsweep P m c e (zadd s q) (out ++ [true])).2.1), (rsweep P m c e (zadd s q) (out ++ [true])).2.2.1, (rsweep P m c e (zadd s q) (out ++ [true])).2.2.2) else ((x :: (rsweep P m c e s (out ++ [false])).1), (true :: (rsweep P m c e s (out ++ [false])).2.1), (rsweep P m c e s (  -- (+206 chars; full line in the Zenodo source)
  rw [FiniteRecChain.ite_pos' _ _ ht, FiniteRecChain.ite_pos' _ _ hb]

theorem rs_rej (q : List Nat) (P : List (List Nat)) (b x y : Bool) (m c e : List Bool) (s : List Nat) (out : List Bool)
    (ht : (!y && touch q s) = true) (hb : ¬ b = true) : rsweep (q :: P) (b :: m) (x :: c) (y :: e) s out = ((x :: (rsweep P m c e s (out ++ [false])).1), (true :: (rsweep P m c e s (out ++ [false])).2.1), (rsweep P m c e s (out ++ [false])).2.2.1, (rsweep P m c e s (out ++ [false])).2.2.2) := by
  show (if (!y && touch q s) = true then (if b = true then ((true :: (rsweep P m c e (zadd s q) (out ++ [true])).1), (true :: (rsweep P m c e (zadd s q) (out ++ [true])).2.1), (rsweep P m c e (zadd s q) (out ++ [true])).2.2.1, (rsweep P m c e (zadd s q) (out ++ [true])).2.2.2) else ((x :: (rsweep P m c e s (out ++ [false])).1), (true :: (rsweep P m c e s (out ++ [false])).2.1), (rsweep P m c e s (  -- (+206 chars; full line in the Zenodo source)
  rw [FiniteRecChain.ite_pos' _ _ ht, FiniteRecChain.ite_neg' _ _ hb]

theorem rs_skip (q : List Nat) (P : List (List Nat)) (b x y : Bool) (m c e : List Bool) (s : List Nat) (out : List Bool)
    (ht : ¬ (!y && touch q s) = true) : rsweep (q :: P) (b :: m) (x :: c) (y :: e) s out = ((x :: (rsweep P m c e s out).1), (y :: (rsweep P m c e s out).2.1), (rsweep P m c e s out).2.2.1, (rsweep P m c e s out).2.2.2) := by
  show (if (!y && touch q s) = true then (if b = true then ((true :: (rsweep P m c e (zadd s q) (out ++ [true])).1), (true :: (rsweep P m c e (zadd s q) (out ++ [true])).2.1), (rsweep P m c e (zadd s q) (out ++ [true])).2.2.1, (rsweep P m c e (zadd s q) (out ++ [true])).2.2.2) else ((x :: (rsweep P m c e s (out ++ [false])).1), (true :: (rsweep P m c e s (out ++ [false])).2.1), (rsweep P m c e s (  -- (+206 chars; full line in the Zenodo source)
  rw [FiniteRecChain.ite_neg' _ _ ht]

theorem sw_add (q : List Nat) (P : List (List Nat)) (b x : Bool) (m c : List Bool) (s : List Nat)
    (h : (b && !x && touch q s) = true) : sweep (q :: P) (b :: m) (x :: c) s = ((true :: (sweep P m c (zadd s q)).1), (sweep P m c (zadd s q)).2) := by
  show (if (b && !x && touch q s) = true then ((true :: (sweep P m c (zadd s q)).1), (sweep P m c (zadd s q)).2)
    else ((x :: (sweep P m c s).1), (sweep P m c s).2)) = _
  rw [FiniteRecChain.ite_pos' _ _ h]

theorem sw_skip (q : List Nat) (P : List (List Nat)) (b x : Bool) (m c : List Bool) (s : List Nat)
    (h : ¬ (b && !x && touch q s) = true) : sweep (q :: P) (b :: m) (x :: c) s = ((x :: (sweep P m c s).1), (sweep P m c s).2) := by
  show (if (b && !x && touch q s) = true then ((true :: (sweep P m c (zadd s q)).1), (sweep P m c (zadd s q)).2)
    else ((x :: (sweep P m c s).1), (sweep P m c s).2)) = _
  rw [FiniteRecChain.ite_neg' _ _ h]

/-- **The recording sweep selects exactly what the plain sweep selects.** -/
theorem rsweep_agree : ∀ (P : List (List Nat)) (m c e : List Bool) (s : List Nat) (out : List Bool),
    P.length = m.length → m.length = c.length → c.length = e.length → Inv m c e →
    (rsweep P m c e s out).1 = (sweep P m c s).1 ∧ (rsweep P m c e s out).2.2.1 = (sweep P m c s).2
  | [], [], [], [], _, _, _, _, _, _ => ⟨rfl, rfl⟩
  | q :: P, b :: m, x :: c, y :: e, s, out, h1, h2, h3, hI => by
    have h1' : P.length = m.length := Nat.succ.inj h1
    have h2' : m.length = c.length := Nat.succ.inj h2
    have h3' : c.length = e.length := Nat.succ.inj h3
    rcases dec_cases b x y (touch q s) hI.1 hI.2.1 with ⟨ht, hb⟩ | ⟨ht, hb, hx⟩ | ⟨ht, hs⟩
    · -- examined now, added
      have hx : x = false := by cases x with
        | false => rfl
        | true => exact absurd (hI.1 rfl) (by cases y <;> first | exact fun e => Bool.noConfusion e | exact fun _ => absurd ht (fun e => Bool.noConfusion e))
      have hsw : (b && !x && touch q s) = true := by
        rw [hb, hx]; cases y <;> cases hq : touch q s <;> first | rfl | (rw [hq] at ht; exact absurd ht (fun e => Bool.noConfusion e))
      have ih := rsweep_agree P m c e (zadd s q) (out ++ [true]) h1' h2' h3' hI.2.2
      rw [rs_add q P b x y m c e s out ht hb, sw_add q P b x m c s hsw]
      exact ⟨congrArg (List.cons true) ih.1, ih.2⟩
    · -- examined now, rejected
      have hsw : ¬ (b && !x && touch q s) = true := by rw [hb]; exact fun e => Bool.noConfusion e
      have ih := rsweep_agree P m c e s (out ++ [false]) h1' h2' h3' hI.2.2
      have hb' : ¬ b = true := by rw [hb]; exact fun e => Bool.noConfusion e
      rw [rs_rej q P b x y m c e s out ht hb', sw_skip q P b x m c s hsw]
      exact ⟨congrArg (List.cons x) ih.1, ih.2⟩
    · -- not examined now, not added
      have ht' : ¬ (!y && touch q s) = true := by rw [ht]; exact fun e => Bool.noConfusion e
      have hsw : ¬ (b && !x && touch q s) = true := by rw [hs]; exact fun e => Bool.noConfusion e
      have ih := rsweep_agree P m c e s out h1' h2' h3' hI.2.2
      rw [rs_skip q P b x y m c e s out ht', sw_skip q P b x m c s hsw]
      exact ⟨congrArg (List.cons x) ih.1, ih.2⟩

end AnimalRec

#print axioms AnimalRec.rsweep
#print axioms AnimalRec.Inv
#print axioms AnimalRec.dec_cases
#print axioms AnimalRec.rs_add
#print axioms AnimalRec.rs_rej
#print axioms AnimalRec.rs_skip
#print axioms AnimalRec.sw_add
#print axioms AnimalRec.sw_skip
#print axioms AnimalRec.rsweep_agree

ym/AnimalSum.lean

lean_all/ym/AnimalSum.lean

/-
  ym/AnimalSum — T27, cycle 48 (area law, lattice-animal count, step 9): the sum over connected masks by injection into codes.
    upTo M           all bit strings of length ≤ M;   mem_upTo
    rangeL k         0, …, k;                         mem_rangeL
    bindL            concatenation of a family of lists; mem_bindL
    filt lemmas      mem_filt_inv, distinct_filt
    cnt_le_len       #C ≤ |C|
    animal_sum       Σ_{C connected} t^#C ≤ Σ_{(A, b) ∈ U} t^A,  U = {(A, b) : A ≤ |P|, |b| ≤ D(|supp n| + wA)}
  Lean 4 core only, proofs by hand, no axioms.
-/
import ym.AnimalCode

namespace AnimalSum

open YMChain LinkIntegral GKS StrongZ2 S2cMask S2cComp S2cIter S2cSplit S2cDec S2cIdem S2cFinal AnimalInj AnimalDeg AnimalSupp AnimalCode

theorem le_succ_cases (a M : Nat) (h : a ≤ M + 1) : a = M + 1 ∨ a ≤ M :=
  (Nat.lt_or_eq_of_le h).elim (fun l => Or.inr (Nat.le_of_lt_succ l)) Or.inl

def upTo : Nat → List (List Bool)
  | 0 => masks 0
  | M + 1 => masks (M + 1) ++ upTo M

theorem mem_upTo : ∀ (M : Nat) (b : List Bool), b.length ≤ M → memL b (upTo M)
  | 0, b, h => by
    cases b with
    | nil => exact Or.inl rfl
    | cons _ _ => exact nomatch h
  | M + 1, b, h => by
    rcases le_succ_cases _ _ h with hL | h'
    · have := mem_masks b
      rw [hL] at this
      exact mem_append_l _ _ _ this
    · exact mem_append_r _ _ _ (mem_upTo M b h')

def rangeL : Nat → List Nat
  | 0 => [0]
  | k + 1 => (k + 1) :: rangeL k

theorem mem_rangeL : ∀ (k a : Nat), a ≤ k → memL a (rangeL k)
  | 0, a, h => by
    cases a with
    | zero => exact Or.inl rfl
    | succ _ => exact nomatch h
  | k + 1, a, h => by
    rcases le_succ_cases _ _ h with hA | h'
    · exact Or.inl hA
    · exact Or.inr (mem_rangeL k _ h')

def bindL {A B : Type} : List A → (A → List B) → List B
  | [], _ => []
  | a :: l, f => f a ++ bindL l f

theorem mem_bindL {A B : Type} (f : A → List B) (y : B) (a : A) (hy : memL y (f a)) : ∀ l : List A, memL a l → memL y (bindL l f)
  | [], h => absurd h id
  | x :: l, h => by
    rcases h with e | e
    · rw [← e]; exact mem_append_l _ _ _ hy
    · exact mem_append_r _ _ _ (mem_bindL f y a hy l e)

theorem mem_filt_inv {A : Type} (p : A → Bool) (a : A) : ∀ l : List A, memL a (filt p l) → memL a l ∧ p a = true
  | [], h => absurd h id
  | x :: l, h => by
    by_cases hx : p x = true
    · have h' : memL a (x :: filt p l) := by
        have e : filt p (x :: l) = x :: filt p l := by
          show (if p x = true then x :: filt p l else filt p l) = _
          rw [FiniteRecChain.ite_pos' _ _ hx]
        rw [e] at h; exact h
      rcases h' with e | e
      · exact ⟨Or.inl e, by rw [e]; exact hx⟩
      · have r := mem_filt_inv p a l e
        exact ⟨Or.inr r.1, r.2⟩
    · have h' : memL a (filt p l) := by
        have e : filt p (x :: l) = filt p l := by
          show (if p x = true then x :: filt p l else filt p l) = _
          rw [FiniteRecChain.ite_neg' _ _ hx]
        rw [e] at h; exact h
      have r := mem_filt_inv p a l h'
      exact ⟨Or.inr r.1, r.2⟩

theorem distinct_filt {A : Type} (p : A → Bool) : ∀ l : List A, distinctL l → distinctL (filt p l)
  | [], _ => trivial
  | x :: l, h => by
    by_cases hx : p x = true
    · show distinctL (if p x = true then x :: filt p l else filt p l)
      rw [FiniteRecChain.ite_pos' _ _ hx]
      exact ⟨fun hm => h.1 (mem_filt_inv p x l hm).1, distinct_filt p l h.2⟩
    · show distinctL (if p x = true then x :: filt p l else filt p l)
      rw [FiniteRecChain.ite_neg' _ _ hx]
      exact distinct_filt p l h.2

theorem cnt_le_len : ∀ C : List Bool, cnt C ≤ C.length
  | [] => Nat.le_refl 0
  | x :: C => by
    show (if x = true then 1 else 0) + cnt C ≤ C.length + 1
    rw [Nat.add_comm]
    apply Nat.add_le_add (cnt_le_len C)
    cases x
    · exact Nat.zero_le 1
    · exact Nat.le_refl 1

def codeSet (P : List (List Nat)) (n : List Nat) (D w : Nat) : List (Nat × List Bool) :=
  bindL (rangeL P.length) (fun A => (upTo (D * (nz n + w * A))).map (fun b => (A, b)))

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

/-- **Σ_{C connected to n} t^#C ≤ Σ_{(A, b) ∈ codeSet} t^A.** -/
theorem animal_sum (h1 : O.le K.zero K.one) (t : R) (ht : O.le K.zero t) (P : List (List Nat)) (n : List Nat) (D w : Nat)
    (hD : allLe (degV P) D) (hw : allQ P w) :
    O.le (sumK K (fun C => pwR K t (cnt C)) (filt (conn P n) (masks P.length)))
      (sumK K (fun y => pwR K t y.1) (codeSet P n D w)) := by
  have hconn : ∀ C, memL C (filt (conn P n) (masks P.length)) → P.length = C.length ∧ (iter P C n P.length).1 = C := by
    intro C hC
    have r := mem_filt_inv _ C _ hC
    exact ⟨(len_masks _ C r.1).symm, of_decide_eq_true r.2⟩
  refine InjSum.inj_le K O (fun y : Nat × List Bool => pwR K t y.1) (fun y => pw_nn K O h1 t ht y.1)
    (fun C => (cnt C, code P C n)) _ _ (distinct_filt _ _ (distinct_masks _)) ?_ ?_
  · intro C hC
    obtain ⟨hm, hc⟩ := hconn C hC
    have hA : cnt C ≤ P.length := by rw [hm]; exact cnt_le_len C
    exact mem_bindL _ (cnt C, code P C n) (cnt C)
      (mem_map (fun b => (cnt C, b)) (code P C n) _ (mem_upTo _ _ (code_short P C n D w hm hD hw hc)))
      _ (mem_rangeL _ _ hA)
  · intro C C' hC hC' e
    obtain ⟨hm, hc⟩ := hconn C hC
    obtain ⟨hm', hc'⟩ := hconn C' hC'
    exact code_inj P n C C' hm hm' hc hc' (congrArg (fun y : Nat × List Bool => y.2) e)

end Main

end AnimalSum

#print axioms AnimalSum.le_succ_cases
#print axioms AnimalSum.upTo
#print axioms AnimalSum.mem_upTo
#print axioms AnimalSum.rangeL
#print axioms AnimalSum.mem_rangeL
#print axioms AnimalSum.bindL
#print axioms AnimalSum.mem_bindL
#print axioms AnimalSum.mem_filt_inv
#print axioms AnimalSum.distinct_filt
#print axioms AnimalSum.cnt_le_len
#print axioms AnimalSum.codeSet
#print axioms AnimalSum.animal_sum

ym/AnimalSupp.lean

lean_all/ym/AnimalSupp.lean

/-
  ym/AnimalSupp — T27, cycle 46 (area law, lattice-animal count, step 7): the swept support has size ≤ |supp n| + w·|C|.
    ind_le, nz_zadd     |supp (s + q)| ≤ |supp s| + |supp q|
    allQ                every plaquette has at most w links
    sweep_supp          |supp s'| + w·#c ≤ |supp s| + w·#c' through one sweep
    iter_supp           |supp (iter k).2| ≤ |supp n| + w·#(iter k).1
  Lean 4 core only, proofs by hand, no axioms.
-/
import ym.AnimalDeg

namespace AnimalSupp

open YMChain GKS S2cComp S2cIter AnimalRec AnimalTouch AnimalDeg

theorem ind_le : ∀ a b : Nat, (if a + b = 0 then 0 else 1) ≤ (if a = 0 then 0 else 1) + (if b = 0 then 0 else 1)
  | 0, 0 => Nat.le_refl 0
  | 0, b + 1 => by
    rw [FiniteRecChain.ite_neg' _ _ (fun e => Nat.noConfusion e : ¬ 0 + (b + 1) = 0), FiniteRecChain.ite_pos' _ _ (rfl : (0 : Nat) = 0),
      FiniteRecChain.ite_neg' _ _ (fun e => Nat.noConfusion e : ¬ b + 1 = 0)]
    exact Nat.le_refl 1
  | a + 1, b => by
    rw [FiniteRecChain.ite_neg' _ _ (fun e => Nat.noConfusion (show Nat.succ (a + b) = 0 by rw [← e, Nat.succ_add]) : ¬ a + 1 + b = 0),
      FiniteRecChain.ite_neg' _ _ (fun e => Nat.noConfusion e : ¬ a + 1 = 0)]
    exact Nat.le_add_right 1 _

theorem nz_zadd : ∀ s q : List Nat, nz (zadd s q) ≤ nz s + nz q
  | [], q => Nat.le_of_eq (Nat.zero_add _).symm
  | _ :: _, [] => Nat.le_add_right _ _
  | a :: s, b :: q => by
    show (if a + b = 0 then 0 else 1) + nz (zadd s q) ≤ ((if a = 0 then 0 else 1) + nz s) + ((if b = 0 then 0 else 1) + nz q)
    have h := Nat.add_le_add (ind_le a b) (nz_zadd s q)
    have e : (if a = 0 then 0 else 1) + (if b = 0 then 0 else 1) + (nz s + nz q) =
        ((if a = 0 then 0 else 1) + nz s) + ((if b = 0 then 0 else 1) + nz q) := by
      generalize (if a = 0 then 0 else 1) = x
      generalize (if b = 0 then 0 else 1) = y
      generalize nz s = u
      generalize nz q = v
      lib_nat
    rw [← e]; exact h

theorem le_cancel (a c : Nat) : ∀ b, a + b ≤ c + b → a ≤ c
  | 0, h => h
  | b + 1, h => le_cancel a c b (Nat.le_of_succ_le_succ h)

def allQ : List (List Nat) → Nat → Prop
  | [], _ => True
  | q :: P, w => nz q ≤ w ∧ allQ P w

theorem sweep_supp (w : Nat) : ∀ (P : List (List Nat)) (m c : List Bool) (s : List Nat), P.length = m.length → m.length = c.length →
    allQ P w → nz (sweep P m c s).2 + w * cnt c ≤ nz s + w * cnt (sweep P m c s).1
  | [], [], [], _, _, _, _ => Nat.le_refl _
  | q :: P, b :: m, x :: c, s, h1, h2, hq => by
    have h1' : P.length = m.length := Nat.succ.inj h1
    have h2' : m.length = c.length := Nat.succ.inj h2
    by_cases hsw : (b && !x && touch q s) = true
    · have hx : x = false := by
        have := and_r _ _ (and_l _ _ hsw)
        cases x with
        | false => rfl
        | true => exact absurd this (fun e => Bool.noConfusion e)
      rw [sw_add q P b x m c s hsw]
      have ih := sweep_supp w P m c (zadd s q) h1' h2' hq.2
      have e1 : (if x = true then 1 else 0) = 0 := by rw [hx]; rfl
      show nz (sweep P m c (zadd s q)).2 + w * ((if x = true then 1 else 0) + cnt c) ≤ nz s + w * (1 + cnt (sweep P m c (zadd s q)).1)
      rw [e1, Nat.zero_add]
      have h3 : nz (zadd s q) ≤ nz s + w := Nat.le_trans (nz_zadd s q) (Nat.add_le_add_left hq.1 _)
      have e2 : (nz s + w) + w * cnt (sweep P m c (zadd s q)).1 = nz s + w * (1 + cnt (sweep P m c (zadd s q)).1) := by
        generalize nz s = u
        generalize cnt (sweep P m c (zadd s q)).1 = X
        lib_nat
      rw [← e2]
      exact Nat.le_trans ih (Nat.add_le_add_right h3 _)
    · rw [sw_skip q P b x m c s hsw]
      have ih := sweep_supp w P m c s h1' h2' hq.2
      show nz (sweep P m c s).2 + w * ((if x = true then 1 else 0) + cnt c) ≤ nz s + w * ((if x = true then 1 else 0) + cnt (sweep P m c s).1)
      generalize (if x = true then 1 else 0) = z
      have h := Nat.add_le_add_right ih (w * z)
      have eL : nz (sweep P m c s).2 + w * (z + cnt c) = nz (sweep P m c s).2 + w * cnt c + w * z := by
        generalize nz (sweep P m c s).2 = u
        generalize cnt c = X
        lib_nat
      have eR : nz s + w * (z + cnt (sweep P m c s).1) = nz s + w * cnt (sweep P m c s).1 + w * z := by
        generalize nz s = u
        generalize cnt (sweep P m c s).1 = X
        lib_nat
      rw [eL, eR]; exact h

/-- **The support after k sweeps has size ≤ |supp n| + w · #(selected).** -/
theorem iter_supp (w : Nat) (P : List (List Nat)) (m : List Bool) (n : List Nat) (hm : P.length = m.length) (hq : allQ P w) :
    ∀ k, nz (iter P m n k).2 ≤ nz n + w * cnt (iter P m n k).1
  | 0 => by
    show nz n ≤ nz n + w * cnt (falses P.length)
    rw [AnimalLen.cnt_falses]; exact Nat.le_refl _
  | k + 1 => by
    have ih := iter_supp w P m n hm hq k
    have hs := sweep_supp w P m (iter P m n k).1 (iter P m n k).2 hm (by rw [iter_len P m n hm k, hm]) hq
    show nz (sweep P m (iter P m n k).1 (iter P m n k).2).2 ≤ nz n + w * cnt (sweep P m (iter P m n k).1 (iter P m n k).2).1
    generalize nz (sweep P m (iter P m n k).1 (iter P m n k).2).2 = F at hs ⊢
    generalize cnt (sweep P m (iter P m n k).1 (iter P m n k).2).1 = X at hs ⊢
    generalize w * cnt (iter P m n k).1 = Y at hs ih
    generalize nz (iter P m n k).2 = Z at hs ih
    have h2 : F + Y ≤ (nz n + w * X) + Y := by
      have e : nz n + Y + w * X = (nz n + w * X) + Y := by
        generalize nz n = u
        generalize w * X = v
        lib_nat
      rw [← e]; exact Nat.le_trans hs (Nat.add_le_add_right ih _)
    exact le_cancel F _ Y h2

end AnimalSupp

#print axioms AnimalSupp.ind_le
#print axioms AnimalSupp.nz_zadd
#print axioms AnimalSupp.le_cancel
#print axioms AnimalSupp.allQ
#print axioms AnimalSupp.sweep_supp
#print axioms AnimalSupp.iter_supp

ym/AnimalTouch.lean

lean_all/ym/AnimalTouch.lean

/-
  ym/AnimalTouch — T27, cycle 44 (area law, lattice-animal count, step 5): every examined plaquette touches the final support.
    touch_nil, touch_cov    touching is monotone in the support (covers)
    rs_grow                 the recording sweep's support covers its input support
    exT                     every examined plaquette touches S
    rs_exT, riter_exT       exT is kept by the recording sweeps
    examined_touch          after |P| sweeps every examined plaquette touches the final support (iter P C n |P|).2
  Lean 4 core only, proofs by hand, no axioms.
-/
import ym.AnimalLen

namespace AnimalTouch

open YMChain GKS S2cComp S2cIter S2cCov AnimalRec AnimalInj

theorem touch_nil : ∀ q : List Nat, touch q [] = false
  | [] => rfl
  | _ :: _ => rfl

theorem bne_ne : ∀ b : Nat, (b != 0) = true → b ≠ 0
  | 0, h => absurd h (fun e => Bool.noConfusion e)
  | _ + 1, _ => fun e => Nat.noConfusion e

theorem ne_bne : ∀ b : Nat, b ≠ 0 → (b != 0) = true
  | 0, h => absurd rfl h
  | _ + 1, _ => rfl

theorem or_cases (u v : Bool) (h : (u || v) = true) : u = true ∨ v = true := by
  cases u
  · exact Or.inr h
  · exact Or.inl rfl

theorem and_l (u v : Bool) (h : (u && v) = true) : u = true := by
  cases u
  · exact absurd h (fun e => Bool.noConfusion e)
  · rfl

theorem and_r (u v : Bool) (h : (u && v) = true) : v = true := by
  cases u
  · exact absurd h (fun e => Bool.noConfusion e)
  · exact h

theorem touch_cov : ∀ (q s S : List Nat), covers S s → touch q s = true → touch q S = true
  | [], _, _, _, h => h
  | _ :: _, [], _, _, h => absurd h (fun e => Bool.noConfusion e)
  | a :: q, b :: s, [], hc, h => by
    rcases or_cases _ _ h with h' | h'
    · exact absurd hc.1 (bne_ne b (and_r _ _ h'))
    · have := touch_cov q s [] hc.2 h'
      rw [touch_nil] at this
      exact absurd this (fun e => Bool.noConfusion e)
  | a :: q, b :: s, c :: S, hc, h => by
    show (((a != 0) && (c != 0)) || touch q S) = true
    rcases or_cases _ _ h with h' | h'
    · rw [and_l _ _ h', ne_bne c (hc.1 (bne_ne b (and_r _ _ h')))]; rfl
    · rw [touch_cov q s S hc.2 h']; cases ((a != 0) && (c != 0)) <;> rfl

theorem cov_step (s q : List Nat) : covers (zadd s q) s := cov_mono s q s (cov_refl s)

theorem rs_grow : ∀ (P : List (List Nat)) (m c e : List Bool) (s : List Nat) (out : List Bool),
    covers (rsweep P m c e s out).2.2.1 s
  | [], _, _, _, s, _ => cov_refl s
  | _ :: _, [], _, _, s, _ => cov_refl s
  | _ :: _, _ :: _, [], _, s, _ => cov_refl s
  | _ :: _, _ :: _, _ :: _, [], s, _ => cov_refl s
  | q :: P, b :: m, x :: c, y :: e, s, out => by
    by_cases ht : (!y && touch q s) = true
    · by_cases hb : b = true
      · rw [rs_add q P b x y m c e s out ht hb]
        exact cov_trans _ _ _ (rs_grow P m c e (zadd s q) (out ++ [true])) (cov_step s q)
      · rw [rs_rej q P b x y m c e s out ht hb]
        exact rs_grow P m c e s (out ++ [false])
    · rw [rs_skip q P b x y m c e s out ht]
      exact rs_grow P m c e s out

def exT : List (List Nat) → List Bool → List Nat → Prop
  | [], _, _ => True
  | _ :: _, [], _ => True
  | q :: P, y :: e, S => (y = true → touch q S = true) ∧ exT P e S

theorem rs_exT : ∀ (P : List (List Nat)) (m c e : List Bool) (s : List Nat) (out : List Bool) (S : List Nat),
    covers S (rsweep P m c e s out).2.2.1 → exT P e S → exT P (rsweep P m c e s out).2.1 S
  | [], _, _, _, _, _, _, _, h => h
  | _ :: _, [], _, _, _, _, _, _, h => h
  | _ :: _, _ :: _, [], _, _, _, _, _, h => h
  | _ :: _, _ :: _, _ :: _, [], _, _, _, _, _ => trivial
  | q :: P, b :: m, x :: c, y :: e, s, out, S, hS, hE => by
    by_cases ht : (!y && touch q s) = true
    · have hq : touch q s = true := and_r _ _ ht
      by_cases hb : b = true
      · rw [rs_add q P b x y m c e s out ht hb] at hS ⊢
        have hSs : covers S s := cov_trans _ _ _ hS (cov_trans _ _ _ (rs_grow P m c e (zadd s q) (out ++ [true])) (cov_step s q))
        exact ⟨fun _ => touch_cov q s S hSs hq, rs_exT P m c e (zadd s q) (out ++ [true]) S hS hE.2⟩
      · rw [rs_rej q P b x y m c e s out ht hb] at hS ⊢
        have hSs : covers S s := cov_trans _ _ _ hS (rs_grow P m c e s (out ++ [false]))
        exact ⟨fun _ => touch_cov q s S hSs hq, rs_exT P m c e s (out ++ [false]) S hS hE.2⟩
    · rw [rs_skip q P b x y m c e s out ht] at hS ⊢
      exact ⟨hE.1, rs_exT P m c e s out S hS hE.2⟩

theorem exT_falses : ∀ (P : List (List Nat)) (S : List Nat), exT P (falses P.length) S
  | [], _ => trivial
  | _ :: P, S => ⟨fun h => Bool.noConfusion h, exT_falses P S⟩

theorem riter_exT (P : List (List Nat)) (m : List Bool) (n S : List Nat) :
    ∀ k, covers S (riter P m n k).2.2 → exT P (riter P m n k).2.1 S
  | 0, _ => exT_falses P S
  | k + 1, h => by
    have hS : covers S (rsweep P m (riter P m n k).1 (riter P m n k).2.1 (riter P m n k).2.2 []).2.2.1 := h
    exact rs_exT P m _ _ _ [] S hS (riter_exT P m n S k (cov_trans _ _ _ hS (rs_grow P m _ _ _ [])))

/-- **After |P| sweeps every examined plaquette touches the final support.** -/
theorem examined_touch (P : List (List Nat)) (m : List Bool) (n : List Nat) (hm : P.length = m.length) :
    exT P (riter P m n P.length).2.1 (iter P m n P.length).2 := by
  rw [← (riter_ok P m n hm P.length).2.1]
  exact riter_exT P m n _ P.length (cov_refl _)

end AnimalTouch

#print axioms AnimalTouch.touch_nil
#print axioms AnimalTouch.bne_ne
#print axioms AnimalTouch.ne_bne
#print axioms AnimalTouch.or_cases
#print axioms AnimalTouch.and_l
#print axioms AnimalTouch.and_r
#print axioms AnimalTouch.touch_cov
#print axioms AnimalTouch.cov_step
#print axioms AnimalTouch.rs_grow
#print axioms AnimalTouch.exT
#print axioms AnimalTouch.rs_exT
#print axioms AnimalTouch.exT_falses
#print axioms AnimalTouch.riter_exT
#print axioms AnimalTouch.examined_touch

ym/Approx.lean

lean_all/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/AreaBound.lean

lean_all/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/AreaBox.lean

lean_all/ym/AreaBox.lean

/-
  ym/AreaBox — T27, cycle 72: **the ℤ₂ lattice gauge theory area law on the hypercubic box, no hypotheses but strong coupling**.
  Box n^(k+2) in d ≥ 2 directions with all its plaquettes (PB), R×T loop with corner 0 :: 0 :: r (R, T < n), D = 2(d − 1),
  s = 2^(4D)·t, s + s ≤ 1:
      N_W ≤ 2^(D|supp W|+1) · (s^(RT) + s^(RT)) · Z,   i.e.  ⟨W_{R×T}⟩ ≤ 2^(D|supp W|+2) (2^(8(d−1)) t)^(RT)
  uniformly in the box size n.
  Lean 4 core only, proofs by hand, no axioms.
-/
import ym.BoxDeg

namespace AreaBox

open YMChain LinkIntegral GKS StrongZ2 LatEnc LoopDot AreaRT PlaqSize BoxPlaq BoxDeg AnimalDeg AnimalArea AreaExp

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

/-- **⟨W_{R×T}⟩ ≤ 2^(D|supp W|+2) (2^(4D) t)^(RT) on the full box, D = 2(d − 1).** -/
theorem area_box (h1 : O.le K.zero K.one) (t : R') (ht : O.le K.zero t) (n d k R T D : Nat) (r : List Nat)
    (hR : R + 1 ≤ n) (hT : T + 1 ≤ n) (hr : r.length = k) (hrv : allLt n r) (hd : 2 ≤ d) (hD : D + 2 = d + d)
    (hs : O.le (K.add (K.mul (nsm K (2 ^ (D * 4)) K.one) t) (K.mul (nsm K (2 ^ (D * 4)) K.one) t)) K.one) :
    O.le (N K t (Lt n d k) (Wv n d k R T r) (PB n d k))
      (K.mul (K.mul (nsm K (2 ^ (D * nz (Wv n d k R T r) + 1)) K.one)
        (K.add (pwR K (K.mul (nsm K (2 ^ (D * 4)) K.one) t) (R * T)) (pwR K (K.mul (nsm K (2 ^ (D * 4)) K.one) t) (R * T))))
        (N K t (Lt n d k) (StrongZ2.zl (Lt n d k)) (PB n d k))) :=
  area_RT4 K O h1 t ht n d k R T r hR hT hr hrv hd (PB n d k) (PB_plaq n d k) D
    (box_degV n d k D (Nat.le_trans (Nat.le_succ 1) hd) hD) hs

end Main

end AreaBox

#print axioms AreaBox.area_box

ym/AreaExp.lean

lean_all/ym/AreaExp.lean

/-
  ym/AreaExp — T27, cycle 55: **the ℤ₂ area law as exponential decay in the minimal area**.
  With s = 2^(Dw)·t ≥ 0 and s + s ≤ 1 (strong coupling), under the hypotheses of wilson_area_law:
      N_W ≤ 2^(D|supp W|+1) · (s^(A_min) + s^(A_min)) · Z,   i.e.  ⟨W⟩ ≤ 2^(D|supp W|+2) (2^(Dw) t)^(A_min).
    nsm_mul, nsm_add, nsm_nmul, nsm_pow     the natural-number scalars
    pwR_mul, pwR_base                       exponent and base rules
    term_eq                                 2^(D(c+wA)+1)·[A ≥ m] t^A = 2^(Dc+1)·[A ≥ m] s^A
    wilson_exp                              the bound
  Lean 4 core only, proofs by hand, no axioms.
-/
import ym.AreaGeo

namespace AreaExp

open YMChain LinkIntegral GKS StrongZ2 S2cComp S2cSplit S2cFinal AnimalDeg AnimalSupp AnimalSum AnimalArea WilsonArea AreaGeo

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

theorem nsm_mul (x : R) : ∀ k, nsm K k x = K.mul (nsm K k K.one) x
  | 0 => (DetChain.zero_mul' K x).symm
  | k + 1 => by
    show K.add x (nsm K k x) = K.mul (K.add K.one (nsm K k K.one)) x
    rw [nsm_mul x k, K.mul_comm (K.add _ _), K.distrib, K.mul_comm x K.one, K.one_mul, K.mul_comm x]

theorem nsm_add (x : R) (a : Nat) : ∀ b, nsm K (a + b) x = K.add (nsm K a x) (nsm K b x)
  | 0 => (DetChain.add_zero' K _).symm
  | b + 1 => by
    show K.add x (nsm K (a + b) x) = K.add (nsm K a x) (K.add x (nsm K b x))
    rw [nsm_add x a b, ← K.add_assoc, K.add_comm x, K.add_assoc]

theorem nsm_nmul (a : Nat) : ∀ b, nsm K (a * b) K.one = K.mul (nsm K a K.one) (nsm K b K.one)
  | 0 => (DetChain.mul_zero K _).symm
  | b + 1 => by
    show nsm K (a * b + a) K.one = K.mul (nsm K a K.one) (K.add K.one (nsm K b K.one))
    rw [nsm_add, nsm_nmul a b, K.distrib, K.mul_comm (nsm K a K.one) K.one, K.one_mul, K.add_comm]

theorem nsm_pow (a : Nat) : ∀ n, nsm K (a ^ n) K.one = pwR K (nsm K a K.one) n
  | 0 => DetChain.add_zero' K _
  | n + 1 => by
    show nsm K (a ^ n * a) K.one = K.mul (pwR K (nsm K a K.one) n) (nsm K a K.one)
    rw [nsm_nmul, nsm_pow a n]

theorem pwR_mul (x : R) (a : Nat) : ∀ b, pwR K x (a * b) = pwR K (pwR K x a) b
  | 0 => rfl
  | b + 1 => by
    show pwR K x (a * b + a) = K.mul (pwR K (pwR K x a) b) (pwR K x a)
    rw [pwR_add, pwR_mul x a b]

theorem pwR_base (x y : R) : ∀ n, pwR K (K.mul x y) n = K.mul (pwR K x n) (pwR K y n)
  | 0 => (K.one_mul K.one).symm
  | n + 1 => by
    show K.mul (pwR K (K.mul x y) n) (K.mul x y) = K.mul (K.mul (pwR K x n) x) (K.mul (pwR K y n) y)
    rw [pwR_base x y n]
    generalize pwR K x n = a; generalize pwR K y n = b
    rw [K.mul_assoc, ← K.mul_assoc b, K.mul_comm b x, K.mul_assoc x, ← K.mul_assoc a]

def tw : R := nsm K 2 K.one

theorem nsm_nn (h1 : O.le K.zero K.one) : ∀ k, O.le K.zero (nsm K k K.one)
  | 0 => O.le_refl _
  | k + 1 => by
    have := O.add_le_add _ _ _ _ h1 (nsm_nn h1 k)
    rw [K.zero_add] at this; exact this

theorem term_eq (t : R) (D c w m A : Nat) :
    nsm K (2 ^ (D * (c + w * A) + 1)) (gA K m t A) =
      K.mul (nsm K (2 ^ (D * c + 1)) K.one) (gA K m (K.mul (nsm K (2 ^ (D * w)) K.one) t) A) := by
  rw [nsm_mul]
  show K.mul (nsm K (2 ^ (D * (c + w * A) + 1)) K.one) (if m ≤ A then pwR K t A else K.zero) =
    K.mul (nsm K (2 ^ (D * c + 1)) K.one) (if m ≤ A then pwR K (K.mul (nsm K (2 ^ (D * w)) K.one) t) A else K.zero)
  by_cases h : m ≤ A
  · rw [FiniteRecChain.ite_pos' _ _ h, FiniteRecChain.ite_pos' _ _ h, pwR_base, nsm_pow, nsm_pow, nsm_pow, ← pwR_mul,
      ← K.mul_assoc, ← pwR_add]
    rw [show D * (c + w * A) + 1 = D * c + 1 + D * w * A by
      lib_nat]
  · rw [FiniteRecChain.ite_neg' _ _ h, FiniteRecChain.ite_neg' _ _ h, DetChain.mul_zero, DetChain.mul_zero]

theorem N_nn (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.zero (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]
  exact O.mul_nonneg _ _ (two_pow_nn K O h1 L) (G_nn K O h1 t ht P (zl L))

/-- **ℤ₂ area law, exponential form: ⟨W⟩ ≤ 2^(D|supp W|+1) · 2 (2^(Dw) t)^(A_min) for 2·2^(Dw) t ≤ 1.** -/
theorem wilson_exp (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) (D w Amin : Nat)
    (hD : allLe (degV P) D) (hw : allQ P w)
    (hmin : ∀ C : List Bool, P.length = C.length → evenAll (selS nW P C) = true → Amin ≤ cnt C)
    (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 (N K t L nW P)
      (K.mul (K.mul (nsm K (2 ^ (D * nz nW + 1)) K.one)
        (K.add (pwR K (K.mul (nsm K (2 ^ (D * w)) K.one) t) Amin) (pwR K (K.mul (nsm K (2 ^ (D * w)) K.one) t) Amin)))
        (N K t L (zl L) P)) := by
  have hb := wilson_area_law K O h1 t ht L nW hW P hP D w Amin hD hw hmin
  have e : sumK K (fun A => nsm K (2 ^ (D * (nz nW + w * A) + 1)) (gA K Amin t A)) (rangeL P.length) =
      K.mul (nsm K (2 ^ (D * nz nW + 1)) K.one) (sumK K (gA K Amin (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 nW) w Amin A)
  rw [e] at hb
  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 Amin
  have g2 := O.mul_le_mul_right _ _ _ g (nsm_nn K O h1 (2 ^ (D * nz nW + 1)))
  rw [K.mul_comm (sumK K _ _), K.mul_comm (K.add _ _)] at g2
  exact O.le_trans _ _ _ hb (O.mul_le_mul_right _ _ _ g2 (N_nn K O h1 t ht L P hP))

end Main

end AreaExp

#print axioms AreaExp.nsm_mul
#print axioms AreaExp.nsm_add
#print axioms AreaExp.nsm_nmul
#print axioms AreaExp.nsm_pow
#print axioms AreaExp.pwR_mul
#print axioms AreaExp.pwR_base
#print axioms AreaExp.tw
#print axioms AreaExp.nsm_nn
#print axioms AreaExp.term_eq
#print axioms AreaExp.N_nn
#print axioms AreaExp.wilson_exp

ym/AreaG.lean

lean_all/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/AreaGeo.lean

lean_all/ym/AreaGeo.lean

/-
  ym/AreaGeo — T27, cycle 54: the geometric tail of the area-law sum.
  In an ordered commutative ring, for s ≥ 0 with s + s ≤ 1:
      Σ_{A ∈ {0..M}, A ≥ m} s^A ≤ s^m + s^m      for every M, m.
    key        s^(k+1) + s^(k+1) ≤ s^k
    geo_zero   the sum vanishes when m > M
    geo_main   Σ + 2 s^(M+1) ≤ 2 s^m when m ≤ M + 1
    geo_tail   the bound
  Lean 4 core only, proofs by hand, no axioms.
-/
import ym.MinArea

namespace AreaGeo

open YMChain LinkIntegral GKS S2cFinal AnimalSum AnimalArea

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

theorem two_mul (a s : R) : K.add (K.mul a s) (K.mul a s) = K.mul (K.add s s) a := by
  rw [← K.distrib, K.mul_comm]

theorem key (h1 : O.le K.zero K.one) (s : R) (hs : O.le K.zero s) (h2 : O.le (K.add s s) K.one) (k : Nat) :
    O.le (K.add (pwR K s (k + 1)) (pwR K s (k + 1))) (pwR K s k) := by
  show O.le (K.add (K.mul (pwR K s k) s) (K.mul (pwR K s k) s)) (pwR K s k)
  rw [two_mul]
  have := O.mul_le_mul_right _ _ _ h2 (pw_nn K O h1 s hs k)
  rw [K.one_mul] at this
  exact this

theorem not_le_of_succ (k m : Nat) (h : k + 1 ≤ m) : ¬ m ≤ k := fun h' => Nat.lt_irrefl k (Nat.le_trans h h')

theorem geo_zero (s : R) : ∀ (M m : Nat), M + 1 ≤ m → sumK K (gA K m s) (rangeL M) = K.zero
  | 0, m, h => by
    show K.add (if m ≤ 0 then pwR K s 0 else K.zero) K.zero = K.zero
    rw [FiniteRecChain.ite_neg' _ _ (not_le_of_succ 0 m h), K.zero_add]
  | M + 1, m, h => by
    show K.add (if m ≤ M + 1 then pwR K s (M + 1) else K.zero) (sumK K (gA K m s) (rangeL M)) = K.zero
    rw [FiniteRecChain.ite_neg' _ _ (not_le_of_succ (M + 1) m h), K.zero_add]
    exact geo_zero s M m (Nat.le_of_succ_le h)

theorem geo_main (h1 : O.le K.zero K.one) (s : R) (hs : O.le K.zero s) (h2 : O.le (K.add s s) K.one) :
    ∀ (M m : Nat), m ≤ M + 1 →
      O.le (K.add (sumK K (gA K m s) (rangeL M)) (K.add (pwR K s (M + 1)) (pwR K s (M + 1)))) (K.add (pwR K s m) (pwR K s m))
  | 0, m, h => by
    rcases le_succ_cases m 0 h with e | e
    · rw [e]
      show O.le (K.add (K.add (if 1 ≤ 0 then pwR K s 0 else K.zero) K.zero) _) _
      rw [FiniteRecChain.ite_neg' _ _ (not_le_of_succ 0 1 (Nat.le_refl 1)), K.zero_add, K.zero_add]
      exact O.le_refl _
    · have em : m = 0 := Nat.le_antisymm e (Nat.zero_le m)
      rw [em]
      show O.le (K.add (K.add (if 0 ≤ 0 then pwR K s 0 else K.zero) K.zero) _) _
      rw [FiniteRecChain.ite_pos' _ _ (Nat.le_refl 0), DetChain.add_zero' K]
      exact O.add_le_add _ _ _ _ (O.le_refl _) (key K O h1 s hs h2 0)
  | M + 1, m, h => by
    rcases le_succ_cases m (M + 1) h with e | e
    · rw [e]
      show O.le (K.add (K.add (if M + 2 ≤ M + 1 then pwR K s (M + 1) else K.zero) (sumK K (gA K (M + 2) s) (rangeL M))) _) _
      rw [FiniteRecChain.ite_neg' _ _ (not_le_of_succ (M + 1) (M + 2) (Nat.le_refl _)), K.zero_add,
        geo_zero K s M (M + 2) (Nat.le_succ _), K.zero_add]
      exact O.le_refl _
    · show O.le (K.add (K.add (if m ≤ M + 1 then pwR K s (M + 1) else K.zero) (sumK K (gA K m s) (rangeL M))) _) _
      rw [FiniteRecChain.ite_pos' _ _ e]
      have ih := geo_main h1 s hs h2 M m e
      have k1 := key K O h1 s hs h2 (M + 1)
      generalize sumK K (gA K m s) (rangeL M) = X at ih ⊢
      generalize pwR K s (M + 1) = a at ih k1 ⊢
      generalize K.add (pwR K s (M + 1 + 1)) (pwR K s (M + 1 + 1)) = b at k1 ⊢
      have step := O.add_le_add _ _ _ _ (O.le_refl (K.add a X)) k1
      have e2 : K.add (K.add a X) a = K.add X (K.add a a) := by
        rw [K.add_comm a X, K.add_assoc]
      rw [e2] at step
      exact O.le_trans _ _ _ step ih

/-- **Geometric tail: Σ_{A ≤ M, A ≥ m} s^A ≤ 2 s^m for 0 ≤ s, 2s ≤ 1.** -/
theorem geo_tail (h1 : O.le K.zero K.one) (s : R) (hs : O.le K.zero s) (h2 : O.le (K.add s s) K.one) (M m : Nat) :
    O.le (sumK K (gA K m s) (rangeL M)) (K.add (pwR K s m) (pwR K s m)) := by
  rcases Nat.lt_or_ge M m with hlt | hge
  · rw [geo_zero K s M m hlt]
    have p := pw_nn K O h1 s hs m
    have := O.add_le_add _ _ _ _ p p
    rw [K.zero_add] at this; exact this
  · have hm := geo_main K O h1 s hs h2 M m (Nat.le_succ_of_le hge)
    have p := pw_nn K O h1 s hs (M + 1)
    have p2 := O.add_le_add _ _ _ _ p p
    rw [K.zero_add] at p2
    have := O.add_le_add _ _ _ _ (O.le_refl (sumK K (gA K m s) (rangeL M))) p2
    rw [DetChain.add_zero' K] at this
    exact O.le_trans _ _ _ this hm

end Main

end AreaGeo

#print axioms AreaGeo.two_mul
#print axioms AreaGeo.key
#print axioms AreaGeo.not_le_of_succ
#print axioms AreaGeo.geo_zero
#print axioms AreaGeo.geo_main
#print axioms AreaGeo.geo_tail

ym/AreaH.lean

lean_all/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/AreaLaw2D.lean

lean_all/ym/AreaLaw2D.lean

/-
  ym/AreaLaw2D — T45, cycle 11 (exact 2D ℤ₂): the area law for independent plaquettes.

  In axial gauge the plaquette variables are free coordinates (Planar.plaq_links, links_unique); with the
  Wilson weight w(u) = E_u (E_true = e^β, E_false = e^−β) they are independent. For a plaquette set A
  (the inside of a Wilson loop) and the remaining plaquettes:
    wilson_num    Σ_u Π_{p∈A} w(u_p) s(u_p) Π_{rest} w(u_p) = (E₊ − E₋)^|A| · (E₊ + E₋)^|rest|
    wilson_den    Σ_u Π_all w(u_p)                       = (E₊ + E₋)^(|A| + |rest|)
    area_law      num · (E₊ + E₋)^|A| = den · (E₊ − E₋)^|A|, i.e. ⟨W⟩ = tanh(β)^|A|
  (s(u) = ±1). So the 2D ℤ₂ Wilson loop obeys an exact area law with string tension −ln tanh β > 0 at
  every β: confinement and a gap at all couplings in two dimensions.

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

namespace AreaLaw2D

open YMChain LinkIntegral

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

def w (Ep Em : R) (b : Bool) : R := match b with
  | true => Ep
  | false => Em

def ws (Ep Em : R) (b : Bool) : R := K.mul (w Ep Em b) (sgn K b)

def pwR' (x : R) : Nat → R
  | 0 => K.one
  | n + 1 => K.mul x (pwR' x n)

theorem prodSum_app : ∀ (fs gs : List (Bool → R)), prodSum K (fs ++ gs) = K.mul (prodSum K fs) (prodSum K gs)
  | [], gs => (K.one_mul _).symm
  | f :: fs, gs => by
    show K.mul _ (prodSum K (fs ++ gs)) = K.mul (K.mul _ (prodSum K fs)) (prodSum K gs)
    rw [prodSum_app fs gs, K.mul_assoc]

theorem prodSum_rep (f : Bool → R) : ∀ n, prodSum K (List.replicate n f) = pwR' K (K.add (f true) (f false)) n
  | 0 => rfl
  | n + 1 => by
    show K.mul (K.add (f true) (f false)) (prodSum K (List.replicate n f)) = _
    rw [prodSum_rep f n]
    rfl

theorem ws_sum (Ep Em : R) : K.add (ws K Ep Em true) (ws K Ep Em false) = K.add Ep (K.neg Em) := by
  show K.add (K.mul Ep K.one) (K.mul Em (K.neg K.one)) = K.add Ep (K.neg Em)
  rw [K.mul_comm Ep, K.one_mul, K.mul_comm Em, DetChain.neg_mul_left, K.one_mul]

theorem wilson_num (Ep Em : R) (a b : Nat) :
    sumK K (prodF K (List.replicate a (ws K Ep Em) ++ List.replicate b (w Ep Em)))
      (bools (List.replicate a (ws K Ep Em) ++ List.replicate b (w Ep Em)).length)
    = K.mul (pwR' K (K.add Ep (K.neg Em)) a) (pwR' K (K.add Ep Em) b) := by
  rw [sum_prod_factor, prodSum_app, prodSum_rep, prodSum_rep, ws_sum]
  rfl

theorem wilson_den (Ep Em : R) (n : Nat) :
    sumK K (prodF K (List.replicate n (w Ep Em))) (bools (List.replicate n (w Ep Em)).length)
    = pwR' K (K.add Ep Em) n := by
  rw [sum_prod_factor, prodSum_rep]
  rfl

theorem pw_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 x (pwR' K x (a + b)) = K.mul (pwR' K x a) (K.mul x (pwR' K x b))
    rw [pw_add' x a b, ← K.mul_assoc, K.mul_comm x, K.mul_assoc]

/-- **Area law** (cross-multiplied): `num · (E₊+E₋)^|A| = den · (E₊−E₋)^|A|`. -/
theorem area_law (Ep Em : R) (a b : Nat) :
    K.mul (K.mul (pwR' K (K.add Ep (K.neg Em)) a) (pwR' K (K.add Ep Em) b)) (pwR' K (K.add Ep Em) a)
      = K.mul (pwR' K (K.add Ep Em) (a + b)) (pwR' K (K.add Ep (K.neg Em)) a) := by
  rw [Nat.add_comm a b, pw_add', K.mul_assoc, K.mul_comm (K.mul (pwR' K (K.add Ep Em) b) (pwR' K (K.add Ep Em) a))]

end AreaLaw2D

#print axioms AreaLaw2D.prodSum_app
#print axioms AreaLaw2D.prodSum_rep
#print axioms AreaLaw2D.ws_sum
#print axioms AreaLaw2D.wilson_num
#print axioms AreaLaw2D.wilson_den
#print axioms AreaLaw2D.pw_add'
#print axioms AreaLaw2D.area_law

ym/AreaLower.lean

lean_all/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/AreaRT.lean

lean_all/ym/AreaRT.lean

/-
  ym/AreaRT — T27, cycle 64: **the ℤ₂ area law for every R×T rectangular loop in every hypercubic box, any dimension**.
  Box n^(k+2) (directions < d, d ≥ 2), R×T loop with corner 0 :: 0 :: r (R, T < n), P any list of valid box plaquettes:
    min_RT       every surface C ⊆ P with ∂C = W_{R×T} has ≥ RT plaquettes (min_area with the RT half-space certificates)
    area_RT      with every link in ≤ D plaquettes of P, every plaquette on ≤ w links, s = 2^(Dw)·t, 2s ≤ 1:
                   N_W ≤ 2^(D|supp W|+1) · 2 s^(RT) · Z,   i.e. ⟨W_{R×T}⟩ ≤ 2^(D|supp W|+2) (2^(Dw) t)^(RT)
  Lean 4 core only, proofs by hand, no axioms.
-/
import ym.PlaqDot

namespace AreaRT

open YMChain LinkIntegral GKS StrongZ2 S2cComp S2cSplit LatEnc LatList PlaqCert LoopCert CertIdx LoopDot PlaqDot SparseDot
  S2cFinal AnimalSum AnimalDeg AnimalSupp AnimalArea MinArea AreaExp

theorem len_upto : ∀ R, (upto R).length = R
  | 0 => rfl
  | R + 1 => by
    show (upto R ++ [R]).length = R + 1
    rw [AnimalLen.len_app, len_upto R]; rfl

theorem len_bind_const {B : Type} (F : Nat → List B) (c : Nat) (hF : ∀ a, (F a).length = c) :
    ∀ l : List Nat, (bindL l F).length = l.length * c
  | [] => (Nat.zero_mul c).symm
  | a :: l => by
    show (F a ++ bindL l F).length = (l.length + 1) * c
    rw [AnimalLen.len_app, hF a, len_bind_const F c hF l]
    generalize l.length = m
    lib_nat

theorem len_fsL (n d k R T : Nat) : (fsL n d k R T).length = R * T := by
  show (bindL (upto R) (fun i => (upto T).map (fun j => fv n d k i j))).length = R * T
  rw [len_bind_const _ T (fun i => by rw [AnimalArea.len_map2, len_upto]), len_upto]

theorem mem_fsL (n d k R T : Nat) (f : List Nat) (h : memL f (fsL n d k R T)) :
    ∃ i j, memL i (upto R) ∧ memL j (upto T) ∧ f = fv n d k i j := by
  obtain ⟨i, hi, hf⟩ := mem_bindL_inv _ f _ h
  obtain ⟨j, hj, e⟩ := mem_map_inv _ f _ hf
  exact ⟨i, j, hi, hj, e.symm⟩

def isPlaq (n d k : Nat) (q : List Nat) : Prop := ∃ v μ ν, validP n d k v μ ν ∧ q = plV n d k v μ ν

theorem one_each (n d k R T : Nat) (hR : R + 1 ≤ n) (hT : T + 1 ≤ n) (hd : 2 ≤ d) :
    ∀ P : List (List Nat), (∀ q, memL q P → isPlaq n d k q) → oneEach P (fsL n d k R T)
  | [], _ => trivial
  | q :: P, h => by
    obtain ⟨v, μ, ν, hv, e⟩ := h q (Or.inl rfl)
    refine ⟨?_, one_each n d k R T hR hT hd P (fun q' hq' => h q' (Or.inr hq'))⟩
    rw [e]; exact plaq_one n d k R T v μ ν hv hR hT hd

/-- **Every surface of box plaquettes spanning the R×T loop has at least RT plaquettes.** -/
theorem min_RT (n d k R T : Nat) (r : List Nat) (hR : R + 1 ≤ n) (hT : T + 1 ≤ n) (hr : r.length = k) (hrv : allLt n r)
    (hd : 2 ≤ d) (P : List (List Nat)) (hP : ∀ q, memL q P → isPlaq n d k q) :
    ∀ C : List Bool, P.length = C.length → evenAll (selS (Wv n d k R T r) P C) = true → R * T ≤ cnt C := by
  intro C hl he
  have := min_area (Wv n d k R T r) P (fsL n d k R T)
    (fun f hf => by
      obtain ⟨i, j, hi, hj, e⟩ := mem_fsL n d k R T f hf
      rw [e]; exact dot_loop n d k R T i j r hR hT hr hrv hd (upto_lt R i hi) (upto_lt T j hj))
    (one_each n d k R T hR hT hd P hP) C hl he
  rw [len_fsL] at this; exact this

theorem len_indV : ∀ (L k : Nat) (S : List Nat), (indV k L S).length = L
  | 0, _, _ => rfl
  | L + 1, k, S => congrArg Nat.succ (len_indV L (k + 1) S)

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

/-- **ℤ₂ area law for R×T loops: ⟨W_{R×T}⟩ ≤ 2^(D|supp W|+2) (2^(Dw) t)^(RT), any box, any dimension.** -/
theorem area_RT (h1 : O.le K.zero K.one) (t : R') (ht : O.le K.zero t) (n d k R T : Nat) (r : List Nat)
    (hR : R + 1 ≤ n) (hT : T + 1 ≤ n) (hr : r.length = k) (hrv : allLt n r) (hd : 2 ≤ d)
    (P : List (List Nat)) (hP : ∀ q, memL q P → isPlaq n d k q) (D w : 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 (N K t (Lt n d k) (Wv n d k R T r) P)
      (K.mul (K.mul (nsm K (2 ^ (D * nz (Wv n d k R T r) + 1)) K.one)
        (K.add (pwR K (K.mul (nsm K (2 ^ (D * w)) K.one) t) (R * T)) (pwR K (K.mul (nsm K (2 ^ (D * w)) K.one) t) (R * T))))
        (N K t (Lt n d k) (StrongZ2.zl (Lt n d k)) P)) :=
  wilson_exp K O h1 t ht (Lt n d k) (Wv n d k R T r) (len_indV _ _ _) P
    (fun q hq => by obtain ⟨v, μ, ν, _, e⟩ := hP q hq; rw [e]; exact len_indV _ _ _)
    D w (R * T) hD hw (min_RT n d k R T r hR hT hr hrv hd P hP) hs

end Main

end AreaRT

#print axioms AreaRT.len_upto
#print axioms AreaRT.len_bind_const
#print axioms AreaRT.len_fsL
#print axioms AreaRT.mem_fsL
#print axioms AreaRT.isPlaq
#print axioms AreaRT.one_each
#print axioms AreaRT.min_RT
#print axioms AreaRT.len_indV
#print axioms AreaRT.area_RT

ym/AreaTwo.lean

lean_all/ym/AreaTwo.lean

/-
  ym/AreaTwo — T27, cycle 79: **the two-sided ℤ₂ area law on the full hypercubic box**.
    prs_head          prs (e + 2) = (0, 1) :: X
    PB_split          PB ~ Q ++ S, S the RT flat plaquettes spanning the R×T loop
    lower_box         t^(RT) · Z ≤ N_W              (0 ≤ t ≤ 1)
    two_sided         t^(RT) · Z ≤ N_W ≤ 2^(D|supp W|+1)·2 s^(RT) · Z   (s = 2^(4D) t, 2s ≤ 1, D = 2(d−1))
  so the string tension σ = −lim log⟨W_{R×T}⟩/(RT) lies in [−log(2^(8(d−1)) t), −log t] uniformly in the box.
  Lean 4 core only, proofs by hand, no axioms.
-/
import ym.FlatLen

namespace AreaTwo

open YMChain LinkIntegral GKS StrongZ2 LatEnc LatList PlaqCert LoopCert CertIdx LoopDot PlaqDot AreaRT BoxPlaq BoxDeg PairSum
  FlatCnt FlatPE FlatLen PermTools AreaLower S2cFinal AnimalSum AnimalDeg AnimalArea AreaExp AreaBox

theorem prs_head : ∀ e : Nat, ∃ X, prs (e + 2) = (0, 1) :: X
  | 0 => ⟨[], rfl⟩
  | e + 1 => by
    obtain ⟨X, hX⟩ := prs_head e
    exact ⟨X ++ (upto (e + 2)).map (fun μ => (μ, e + 2)), by
      show prs (e + 2) ++ (upto (e + 2)).map (fun μ => (μ, e + 2)) = _
      rw [hX]; rfl⟩

theorem perm_map {A B : Type} (f : A → B) : ∀ (a b : List A), a.Perm b → (a.map f).Perm (b.map f) := by
  intro a b h
  induction h with
  | nil => exact List.Perm.nil
  | cons x _ ih => exact List.Perm.cons (f x) ih
  | swap x y l => exact List.Perm.swap (f x) (f y) (l.map f)
  | trans _ _ ih1 ih2 => exact ih1.trans ih2

theorem perm_app_r {A : Type} (c : List A) : ∀ (a b : List A), a.Perm b → (a ++ c).Perm (b ++ c) := by
  intro a b h
  induction h with
  | nil => exact List.Perm.refl c
  | cons x _ ih => exact List.Perm.cons x ih
  | swap x y l => exact List.Perm.swap x y (l ++ c)
  | trans _ _ ih1 ih2 => exact ih1.trans ih2

theorem map_app' {A B : Type} (f : A → B) : ∀ a b : List A, (a ++ b).map f = a.map f ++ b.map f
  | [], _ => rfl
  | x :: a, b => congrArg (List.cons (f x)) (map_app' f a b)

def Sf (n d k R T : Nat) (r : List Nat) : List (List Nat) := (FV n k R T r).map (fun v => plV n d k v 0 1)

/-- **PB is a rearrangement of Q ++ S with S the flat surface.** -/
theorem PB_split (n e k R T : Nat) (r : List Nat) : ∃ Q, (PB n (e + 2) k).Perm (Q ++ Sf n (e + 2) k R T r) := by
  obtain ⟨X, hX⟩ := prs_head e
  let F := fun p : Nat × Nat => (inV n k p.1 p.2).map (fun v => plV n (e + 2) k v p.1 p.2)
  have hPB : PB n (e + 2) k = (inV n k 0 1).map (fun v => plV n (e + 2) k v 0 1) ++ bindL X F := by
    show bindL (prs (e + 2)) F = _
    rw [hX]; rfl
  refine ⟨(filt (fun v => !(isF R T r v)) (inV n k 0 1)).map (fun v => plV n (e + 2) k v 0 1) ++ bindL X F, ?_⟩
  rw [hPB]
  have h1 := perm_map (fun v => plV n (e + 2) k v 0 1) _ _ (perm_filt (isF R T r) (inV n k 0 1))
  rw [map_app'] at h1
  refine (perm_app_r _ _ _ h1).trans ?_
  rw [AnimalInj.app_assoc]
  exact perm_app_comm _ _

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

/-- **Lower bound: t^(RT) · Z ≤ N_W on the full box (0 ≤ t ≤ 1).** -/
theorem lower_box (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)
    (n e k R T : Nat) (r : List Nat) (hR : R + 1 ≤ n) (hT : T + 1 ≤ n) (hr : r.length = k) (hrv : allLt n r) :
    O.le (K.mul (pwR K t (R * T)) (N K t (Lt n (e + 2) k) (zl (Lt n (e + 2) k)) (PB n (e + 2) k)))
      (N K t (Lt n (e + 2) k) (Wv n (e + 2) k R T r) (PB n (e + 2) k)) := by
  obtain ⟨Q, hQ⟩ := PB_split n e k R T r
  have hlenPB : ∀ q, memL q (PB n (e + 2) k) → q.length = Lt n (e + 2) k := fun q hq => by
    obtain ⟨v, μ, ν, _, eq⟩ := PB_plaq n (e + 2) k q hq
    rw [eq]; exact len_indV _ _ _
  have hlenQS : ∀ q, memL q (Q ++ Sf n (e + 2) k R T r) → q.length = Lt n (e + 2) k := fun q hq =>
    hlenPB q (perm_mem _ _ hQ.symm q hq)
  have hS : ∀ q, memL q (Sf n (e + 2) k R T r) → q.length = Lt n (e + 2) k := fun q hq =>
    hlenQS q (mem_append_r _ _ _ hq)
  have hQl : ∀ q, memL q Q → q.length = Lt n (e + 2) k := fun q hq => hlenQS q (mem_append_l _ _ _ hq)
  have hW : (Wv n (e + 2) k R T r).length = Lt n (e + 2) k := len_indV _ _ _
  rw [N_perm K t _ _ (len_zl _) _ _ hQ hlenPB, N_perm K t _ _ hW _ _ hQ hlenPB]
  have al := area_lower K O h1 h10 t ht0 ht1 (Lt n (e + 2) k) (Sf n (e + 2) k R T r) hS Q hQl
  have hlenS : (Sf n (e + 2) k R T r).length = R * T := by
    show ((FV n k R T r).map _).length = R * T
    rw [AnimalArea.len_map2, len_FV n k R T r hR hT hr hrv]
  rw [hlenS] at al
  have hn' : (zadd (zl (Lt n (e + 2) k)) (AreaLower.sumL (Sf n (e + 2) k R T r))).length = Lt n (e + 2) k := by
    rw [len_zadd_le _ _ (by rw [len_zl]; exact len_sumL _ _ hS), len_zl]
  have eA := N_eq K t (Q ++ Sf n (e + 2) k R T r) (zadd (zl (Lt n (e + 2) k)) (AreaLower.sumL (Sf n (e + 2) k R T r)))
    (fun q hq => (hlenQS q hq).trans hn'.symm)
  have eB := N_eq K t (Q ++ Sf n (e + 2) k R T r) (Wv n (e + 2) k R T r) (fun q hq => (hlenQS q hq).trans hW.symm)
  rw [hn'] at eA
  rw [hW] at eB
  have pe : PE (zadd (zl (Lt n (e + 2) k)) (AreaLower.sumL (Sf n (e + 2) k R T r))) (Wv n (e + 2) k R T r) :=
    flat_PE n (e + 2) k R T r hR hT hr hrv (Nat.le_add_left 2 e)
  have eq : N K t (Lt n (e + 2) k) (zadd (zl (Lt n (e + 2) k)) (AreaLower.sumL (Sf n (e + 2) k R T r))) (Q ++ Sf n (e + 2) k R T r) =
      N K t (Lt n (e + 2) k) (Wv n (e + 2) k R T r) (Q ++ Sf n (e + 2) k R T r) := by
    rw [eA, eB, G_congr K t _ _ _ pe]
  rw [eq] at al
  exact al

/-- **Two-sided ℤ₂ area law on the full box: t^(RT) Z ≤ N_W ≤ 2^(D|supp W|+1)(s^(RT) + s^(RT)) Z.** -/
theorem two_sided (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)
    (n e k R T D : Nat) (r : List Nat) (hR : R + 1 ≤ n) (hT : T + 1 ≤ n) (hr : r.length = k) (hrv : allLt n r)
    (hD : D + 2 = (e + 2) + (e + 2))
    (hs : O.le (K.add (K.mul (nsm K (2 ^ (D * 4)) K.one) t) (K.mul (nsm K (2 ^ (D * 4)) K.one) t)) K.one) :
    O.le (K.mul (pwR K t (R * T)) (N K t (Lt n (e + 2) k) (zl (Lt n (e + 2) k)) (PB n (e + 2) k)))
      (N K t (Lt n (e + 2) k) (Wv n (e + 2) k R T r) (PB n (e + 2) k)) ∧
    O.le (N K t (Lt n (e + 2) k) (Wv n (e + 2) k R T r) (PB n (e + 2) k))
      (K.mul (K.mul (nsm K (2 ^ (D * nz (Wv n (e + 2) k R T r) + 1)) K.one)
        (K.add (pwR K (K.mul (nsm K (2 ^ (D * 4)) K.one) t) (R * T)) (pwR K (K.mul (nsm K (2 ^ (D * 4)) K.one) t) (R * T))))
        (N K t (Lt n (e + 2) k) (zl (Lt n (e + 2) k)) (PB n (e + 2) k))) :=
  ⟨lower_box K O h1 h10 t ht0 ht1 n e k R T r hR hT hr hrv,
   area_box K O h1 t ht0 n (e + 2) k R T D r hR hT hr hrv (Nat.le_add_left 2 e) hD hs⟩

end Main

end AreaTwo

#print axioms AreaTwo.prs_head
#print axioms AreaTwo.perm_map
#print axioms AreaTwo.perm_app_r
#print axioms AreaTwo.map_app'
#print axioms AreaTwo.Sf
#print axioms AreaTwo.PB_split
#print axioms AreaTwo.lower_box
#print axioms AreaTwo.two_sided

ym/AreaUpper.lean

lean_all/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/AreaZd.lean

lean_all/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/AxCheck.lean

lean_all/ym/AxCheck.lean

/- AxCheck: imports the ladder modules and prints the axioms of every theorem in them (checker only, no definitions). -/
import ym.ActivityDecay
import ym.Approx
import ym.AreaBound
import ym.AreaG
import ym.AreaH
import ym.AreaLower
import ym.AreaUpper
import ym.AreaZd
import ym.AxialAlg
import ym.BallSize
import ym.Block2D
import ym.Bridge2D
import ym.BridgeLogic
import ym.CauchyBox
import ym.CauchyCone8
import ym.CauchyConeJ
import ym.Center
import ym.CenterExp
import ym.CertSound
import ym.CertSoundQ
import ym.CertWilson
import ym.CertWilsonT
import ym.CgSplit
import ym.CharOrthH
import ym.ChiBound
import ym.ChiGram
import ym.ChiGram3
import ym.Closure
import ym.Cluster
import ym.ConeBound
import ym.ConeGap
import ym.Count
import ym.CxRing
import ym.DecLR
import ym.DecVV
import ym.Decimation
import ym.Decrement
import ym.Degree
import ym.DimRow
import ym.DistCount
import ym.Dup
import ym.DupAlg
import ym.DupAlgH
import ym.DupH
import ym.ExpTail
import ym.Expand
import ym.Factor
import ym.FarDisj
import ym.Fischer
import ym.Flow2D
import ym.Flux
import ym.Fusion
import ym.GKS
import ym.GKS2
import ym.GPerm
import ym.Gap
import ym.GapBothBoxD4
import ym.GapBothBoxJ
import ym.GapBothD4
import ym.GapBothGen
import ym.GapBothJ
import ym.GapCone
import ym.GapCone8
import ym.GapConeD
import ym.GapConeJ
import ym.GapConeK
import ym.GapConeKA
import ym.GapFlow
import ym.GapG
import ym.GapH
import ym.GapInputs
import ym.GapInputsK
import ym.GapLimit
import ym.GapZd
import ym.Gauss
import ym.GaussCatalan
import ym.GaussH0
import ym.GaussHermite
import ym.GaussPoly
import ym.GaussPos
import ym.GaussQuat
import ym.GaussRot
import ym.GaussSphere
import ym.GaussSubst
import ym.Geom
import ym.GramChi
import ym.GroupAverage
import ym.HaarFin
import ym.HaarPoly
import ym.HaarStr
import ym.Heat2D
import ym.HeatPair
import ym.HeatTube
import ym.Indep
import ym.InfVol4D
import ym.InjCount
import ym.Join
import ym.L1Full
import ym.L1Zero
import ym.Lad3
import ym.Lad5
import ym.Lad5Cauchy
import ym.LinfDist
import ym.LinkFactor
import ym.LinkIntegral
import ym.MaskMono
import ym.MaskSum
import ym.MeanZero
import ym.Migdal
import ym.MigdalCone
import ym.MigdalDecay
import ym.MigdalStrong
import ym.Monotone
import ym.OField
import ym.OSLimit
import ym.Peel
import ym.PlaqLower
import ym.Poly
import ym.PolyBound
import ym.PosRatio
import ym.QuatTrace
import ym.QuatUnit
import ym.R7
import ym.RPChi
import ym.RPDot
import ym.RPFlow
import ym.RPLimit
import ym.Ratio
import ym.Reach
import ym.RectGeom
import ym.RectInt
import ym.RectLoop
import ym.SU2Char
import ym.SU2CharExp
import ym.SU2Haar
import ym.SU2Strong4D
import ym.SU2Wilson2D
import ym.Sandwich
import ym.SchurAvg
import ym.Seeds
import ym.ShellSum
import ym.Signed
import ym.Sites
import ym.SmallQ
import ym.SpecGap
import ym.Strip2D
import ym.StripAxial
import ym.StrongZ2
import ym.SupportVec
import ym.SymLimit
import ym.TMCertQ
import ym.TMCertT
import ym.TMCertT8
import ym.TMCoverT
import ym.TMCoverT8
import ym.Tail
import ym.TouchCount
import ym.TripleH
import ym.TruncNum
import ym.TruncTail
import ym.UFlow
import ym.Vanish
import ym.VanishH
import ym.VolOrder
import ym.Volume
import ym.VolumeMono
import ym.WeightBound
import ym.WeightPosJ
import ym.WilsonCauchy
import ym.WilsonCauchyD
import ym.WilsonCone
import ym.WilsonGap
import ym.WilsonGapK
import ym.WilsonGram
import ym.WilsonVol
import ym.WilsonVolStep
import ym.Window
import ym.ZdDegree
import ym.ZdDist
import ym.ZdLinks

#print axioms ActivityDecay.four_h
#print axioms ActivityDecay.half_add
#print axioms ActivityDecay.activity_decay
#print axioms Approx.sel_nth
#print axioms Approx.term_dec
#print axioms Approx.len_Xm
#print axioms Approx.off_Xm
#print axioms Approx.disj_of_off
#print axioms Approx.sub_of_le
#print axioms Approx.or_sub
#print axioms Approx.andB_assoc_sub
#print axioms Approx.G_sel_nn
#print axioms Approx.lower_B
#print axioms Approx.and_sub2
#print axioms Approx.upper_P
#print axioms Approx.approx
#print axioms AreaBound.wsum_nil_r
#print axioms AreaBound.wsum_zadd
#print axioms AreaBound.parity_add
#print axioms AreaBound.wsum_even
#print axioms AreaBound.wsum_bd
#print axioms AreaBound.selSum_even
#print axioms AreaBound.odd_exists
#print axioms AreaBound.mem_of_erase
#print axioms AreaBound.nodup_erase
#print axioms AreaBound.len_erase_le
#print axioms AreaBound.surj_count
#print axioms AreaBound.len_selI
#print axioms AreaBound.mem_selI
#print axioms AreaBound.area_bound
#print axioms AreaG.andB_le_r
#print axioms AreaG.pw_exp_mul
#print axioms AreaG.natR_ge_one
#print axioms AreaG.pwN_ge_one
#print axioms AreaG.pw_base_mono
#print axioms AreaG.area_G
#print axioms AreaH.aWH_bnd
#print axioms AreaH.Z0H_ratio
#print axioms AreaH.area_H
#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
#print axioms AreaUpper.len_tr
#print axioms AreaUpper.andB_tr
#print axioms AreaUpper.andB_tr'
#print axioms AreaUpper.cnt_tr
#print axioms AreaUpper.DW_eq_D
#print axioms AreaUpper.area_upper_G
#print axioms AreaUpper.area_upper
#print axioms AreaZd.mem_of_erase'
#print axioms AreaZd.not_mem_erase
#print axioms AreaZd.cnt_erase
#print axioms AreaZd.memB_t
#print axioms AreaZd.memB_f
#print axioms AreaZd.cnt_sub
#print axioms AreaZd.wsum_map_vec
#print axioms AreaZd.cnt4_list
#print axioms AreaZd.ne_snd
#print axioms AreaZd.ne_fst
#print axioms AreaZd.nodup_four
#print axioms AreaZd.linkIn_four
#print axioms AreaZd.wsum_vec_par
#print axioms AreaZd.nthP_map
#print axioms AreaZd.area_Zd
#print axioms AxialAlg.rho_gprod
#print axioms AxialAlg.rho_interleave
#print axioms AxialAlg.It_rinv
#print axioms BallSize.cnt_vAux
#print axioms BallSize.cnt_andB_le
#print axioms BallSize.iter_step_cnt
#print axioms BallSize.pow_ge_one
#print axioms BallSize.ball_size
#print axioms Block2D.conv_schur
#print axioms Block2D.ratio_mul
#print axioms Bridge2D.pow_succ'
#print axioms Bridge2D.bernoulli
#print axioms Bridge2D.steps_bound
#print axioms BridgeLogic.reach_aux
#print axioms BridgeLogic.reach_threshold
#print axioms BridgeLogic.bridge_decay
#print axioms CauchyBox.cauchy_box
#print axioms CauchyCone8.cauchy_of_cone8
#print axioms CauchyConeJ.cauchy_of_coneJ
#print axioms CauchyConeJ.cauchy_flow_j
#print axioms Center.sumK_neg'
#print axioms Center.neg_self_zero
#print axioms Center.odd_mean
#print axioms Center.odd_vanish
#print axioms CenterExp.neg_mul_neg
#print axioms CenterExp.sg_mul
#print axioms CenterExp.sg_mul_l
#print axioms CenterExp.nthN_zadd
#print axioms CenterExp.expand2
#print axioms CenterExp.sign_prod
#print axioms CenterExp.odd_link
#print axioms CenterExp.setG_long
#print axioms CenterExp.sum_zero_mem
#print axioms CenterExp.term_vanish
#print axioms CenterExp.prodW_ext
#print axioms CenterExp.center_area
#print axioms CertSound.dot_bracket
#print axioms CertSound.cgc_bracket
#print axioms CertSound.quot_bracket
#print axioms CertSound.cN_pow
#print axioms CertSound.dec_bracket
#print axioms CertSound.and_true_l
#print axioms CertSound.and_true_r
#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.mkIt_shift
#print axioms CertSound.brk_nn
#print axioms CertSound.chain_sound
#print axioms CertSound.brk_head
#print axioms CertSound.step_bracketT
#print axioms CertSound.chain_soundT
#print axioms CertSoundQ.cone_of_checkQ
#print axioms CertSoundQ.chain_soundTQ
#print axioms CertWilson.fact_succ2
#print axioms CertWilson.cS_exact
#print axioms CertWilson.exact_quot
#print axioms CertWilson.fact_ge
#print axioms CertWilson.pow_ge
#print axioms CertWilson.start_bracket
#print axioms CertWilson.mkIt_add
#print axioms CertWilson.certified_forever
#print axioms CertWilson.certified_foreverT
#print axioms CertWilson.cS_mono
#print axioms CertWilson.sandwich_scale
#print axioms CertWilson.start_bracketI
#print axioms CertWilson.certified_interval
#print axioms CertWilsonT.pad_exact
#print axioms CertWilsonT.start_bracketIT
#print axioms CertWilsonT.certified_intervalT
#print axioms CgSplit.lp_split
#print axioms CgSplit.lp_step
#print axioms CgSplit.cg_split
#print axioms CgSplit.sq_split
#print axioms CgSplit.sq_sum
#print axioms CgSplit.cg_zero
#print axioms CgSplit.cg_one
#print axioms CgSplit.cgc_split
#print axioms CharOrthH.inv_mul_inv
#print axioms CharOrthH.intertw
#print axioms CharOrthH.orth_ineq
#print axioms CharOrthH.trace_M
#print axioms CharOrthH.orth_eq
#print axioms ChiBound.c_rec
#print axioms ChiBound.c0
#print axioms ChiBound.c1
#print axioms ChiBound.Q_inv
#print axioms ChiBound.neg_le
#print axioms ChiBound.sq_le_one
#print axioms ChiBound.le_of_eq_add
#print axioms ChiBound.sub_nonpos
#print axioms ChiBound.step
#print axioms ChiBound.chi_bound
#print axioms ChiBound.weight_near_one
#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 ChiGram3.chi3_gram
#print axioms ChiGram3.chi3_rp
#print axioms Closure.leB_refl
#print axioms Closure.leB_trans
#print axioms Closure.leB_len
#print axioms Closure.cnt_le_len
#print axioms Closure.leB_cnt
#print axioms Closure.leB_cnt_lt
#print axioms Closure.andB_le
#print axioms Closure.le_andB
#print axioms Closure.andB_congr
#print axioms Closure.fz_le
#print axioms Closure.iter_le_S
#print axioms Closure.iter_len
#print axioms Closure.iter_succ_ge
#print axioms Closure.iter_step
#print axioms Closure.cl_fix
#print axioms Closure.cl_le
#print axioms Closure.cl_len
#print axioms Closure.iter_late
#print axioms Closure.iter_chain
#print axioms Closure.split_nat
#print axioms Closure.iter_le_cl
#print axioms Closure.cl_idem
#print axioms Cluster.inj2
#print axioms Cluster.rev_inj
#print axioms Cluster.nth_orB
#print axioms Cluster.nth_notB
#print axioms Cluster.nth_disj
#print axioms Cluster.cl_union
#print axioms Cluster.rest_eq
#print axioms ConeBound.dot_le
#print axioms ConeBound.dot_nn
#print axioms ConeBound.Bd_nn
#print axioms ConeBound.Sm_le
#print axioms ConeBound.Sm_nn
#print axioms ConeBound.Sm_smul
#print axioms ConeBound.Sm_mul_r
#print axioms ConeBound.pw_anti
#print axioms ConeBound.term_le
#print axioms ConeBound.dot_mono
#print axioms ConeBound.prod_bound
#print axioms ConeBound.cN_le_pow2
#print axioms ConeBound.Sm_geom
#print axioms ConeBound.S_le_two
#print axioms ConeBound.cone_product
#print axioms ConeGap.neg_add
#print axioms ConeGap.neg_le
#print axioms ConeGap.dot_abs
#print axioms ConeGap.le_mul_both
#print axioms ConeGap.eps_bound
#print axioms ConeGap.activity
#print axioms ConeGap.activity16
#print axioms Count.ext_nth
#print axioms Count.len_andB
#print axioms Count.cnt_upd_true
#print axioms Count.cnt_upd_false
#print axioms Count.andB_upd
#print axioms Count.cnt_andB_or
#print axioms Count.map_or
#print axioms Count.nth_map
#print axioms Count.cnt_pos
#print axioms Count.cnt_of_nth
#print axioms Count.len_map
#print axioms Count.cnt_fz
#print axioms Count.len_fz
#print axioms Count.split_at
#print axioms Count.D_nn
#print axioms Count.D_rec
#print axioms Count.pw_mono
#print axioms Count.D_base
#print axioms Count.D_bound
#print axioms CxRing.re_mul_conj
#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 DecVV.sq_step
#print axioms DecVV.vv_sq
#print axioms DecVV.sq_mono
#print axioms DecVV.base_sq
#print axioms DecVV.vv_sq'
#print axioms DecVV.cN_shift
#print axioms DecVV.sq_VV
#print axioms DecVV.sq_base
#print axioms DecVV.dec_good
#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
#print axioms Decrement.add_cancel_r
#print axioms Decrement.key_ineq
#print axioms Decrement.quad_decay
#print axioms Degree.touch_cons
#print axioms Degree.cnt_mono_map
#print axioms Degree.linkdeg_shift
#print axioms Degree.map_map'
#print axioms Degree.L_a
#print axioms Degree.L_b
#print axioms Degree.L_c
#print axioms Degree.L_d
#print axioms Degree.L_e
#print axioms Degree.deg_bound
#print axioms Degree.deg_uniform
#print axioms DimRow.arith
#print axioms DimRow.P_congr2
#print axioms DimRow.P_dims
#print axioms DimRow.Psi_dims
#print axioms DimRow.dimsum
#print axioms DimRow.cN_pos
#print axioms DimRow.prod_anoninc
#print axioms DimRow.decM_Yd
#print axioms DimRow.decM_good
#print axioms DistCount.len_map''
#print axioms DistCount.len_app
#print axioms DistCount.len_rng0
#print axioms DistCount.len_win
#print axioms DistCount.le_cancel_l
#print axioms DistCount.mem_win
#print axioms DistCount.len_consAll
#print axioms DistCount.len_winCube
#print axioms DistCount.mem_winCube
#print axioms DistCount.len_withPairs
#print axioms DistCount.len_pairs_le
#print axioms DistCount.mem_winP
#print axioms DistCount.shell_count_dist
#print axioms DistCount.lin_le_pow3
#print axioms DistCount.mul_pow'
#print axioms DistCount.pow_pow_comm
#print axioms DistCount.shell_exp
#print axioms Dup.sum_pairs
#print axioms Dup.dup_factor
#print axioms Dup.dup_swap
#print axioms Dup.two_zero
#print axioms Dup.dup_anti
#print axioms DupAlg.fst_swap
#print axioms DupAlg.snd_swap
#print axioms DupAlg.getO_map
#print axioms DupAlg.dep_fst
#print axioms DupAlg.dep_snd
#print axioms DupAlg.prodW_prod
#print axioms DupAlg.Zdup0
#print axioms DupAlg.ZdupW
#print axioms DupAlgH.Zdup0_H
#print axioms DupAlgH.ZdupW_H
#print axioms DupH.dup_anti_H
#print axioms DupH.vanish_join_H
#print axioms ExpTail.natR_pos
#print axioms ExpTail.halve
#print axioms ExpTail.two_mul
#print axioms ExpTail.tail_geom
#print axioms ExpTail.pw_bnd
#print axioms ExpTail.dsum_bnd
#print axioms ExpTail.series_split
#print axioms ExpTail.exp_cauchy
#print axioms Expand.expand
#print axioms Expand.prodW_or
#print axioms Expand.depOn_mono
#print axioms Expand.depOn_mul
#print axioms Expand.len_uSupp
#print axioms Expand.depOn_const
#print axioms Expand.depOn_prodW
#print axioms Factor.vAux_ge
#print axioms Factor.touch_disj
#print axioms Factor.disj_comm
#print axioms Factor.disj_zadd_left
#print axioms Factor.disj_zadd_right
#print axioms Factor.disj_zl
#print axioms Factor.band3
#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
#print axioms FarDisj.shf_ge
#print axioms FarDisj.shf_le
#print axioms FarDisj.base_near
#print axioms FarDisj.near_of_shared
#print axioms FarDisj.disj_vec
#print axioms FarDisj.far_disj
#print axioms Fischer.pw_mul_conj
#print axioms Fischer.castN_cx
#print axioms Fischer.re_sum
#print axioms Fischer.fischer
#print axioms Flow2D.four_mul
#print axioms Flow2D.blockIt_schur
#print axioms Flow2D.bernoulli
#print axioms Flow2D.flow_reaches
#print axioms Flux.shf_c0
#print axioms Flux.shf_c1
#print axioms Flux.shf0_c0
#print axioms Flux.phi_dir
#print axioms Flux.phi_t
#print axioms Flux.phi_f2
#print axioms Flux.phi_f3
#print axioms Flux.ne21
#print axioms Flux.phi_plaq
#print axioms Fusion.cg_rec
#print axioms Fusion.cg_top
#print axioms Fusion.dl_le_one
#print axioms Fusion.dl_ne
#print axioms Fusion.cg_le_one
#print axioms Fusion.dot_succ
#print axioms Fusion.N_rec
#print axioms Fusion.fusion
#print axioms Fusion.triple
#print axioms Fusion.coef_prod_nn
#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
#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
#print axioms GPerm.G_swap
#print axioms GPerm.G_front
#print axioms GPerm.if_cons
#print axioms GPerm.G_sel
#print axioms GPerm.sel_upd
#print axioms GPerm.memL_app
#print axioms GPerm.memL_sel
#print axioms GPerm.memL_nthL
#print axioms GPerm.one_le_mul
#print axioms GPerm.gksG
#print axioms GPerm.gksG_mask
#print axioms Gap.G_shift'
#print axioms Gap.bd_disj
#print axioms Gap.or_and_self
#print axioms Gap.decouple_mask
#print axioms Gap.mul_le_mul4
#print axioms Gap.gap_upper
#print axioms GapBothBoxD4.both_box_d4
#print axioms GapBothBoxJ.both_box_flow_d4_j
#print axioms GapBothD4.both_d4
#print axioms GapBothGen.both_of_cone8
#print axioms GapBothJ.pow2_le_add
#print axioms GapBothJ.both_of_coneJ
#print axioms GapBothJ.both_flow_d4_j
#print axioms GapCone.gap_cone
#print axioms GapCone8.gap_of_cone8
#print axioms GapConeD.le_pow
#print axioms GapConeD.hsmall_d
#print axioms GapConeD.hxy_d
#print axioms GapConeD.gap_cone_d
#print axioms GapConeJ.gap_of_coneJ
#print axioms GapConeJ.gap_flow_j
#print axioms GapConeK.gap_cone_k
#print axioms GapConeKA.gap_cone_ka
#print axioms GapFlow.adm_ev
#print axioms GapFlow.adm_dot
#print axioms GapFlow.h14
#print axioms GapFlow.gap_of_cone
#print axioms GapG.depOn_add
#print axioms GapG.depOn_neg
#print axioms GapG.depOn_mul'
#print axioms GapG.nzb_add
#print axioms GapG.supp_zadd
#print axioms GapG.prodW_congr
#print axioms GapG.bnd_add
#print axioms GapG.bnd_neg
#print axioms GapG.bnd_unit
#print axioms GapG.wid
#print axioms GapG.gap_G
#print axioms GapH.gap_H
#print axioms GapInputs.bern
#print axioms GapInputs.cN_pow2
#print axioms GapInputs.pow2_h
#print axioms GapInputs.hle_one
#print axioms GapInputs.one_sub_h
#print axioms GapInputs.h_le_sub
#print axioms GapInputs.hxy4
#print axioms GapInputs.k66
#print axioms GapInputs.hsmall4
#print axioms GapInputs.kappa_ineq
#print axioms GapInputs.norm_ok
#print axioms GapInputsK.pw_succ
#print axioms GapInputsK.pow2_hk
#print axioms GapInputsK.dbl
#print axioms GapInputsK.hxyK
#print axioms GapInputsK.kpow
#print axioms GapInputsK.hsmallK
#print axioms GapInputsK.kappa_ineqK
#print axioms GapInputsK.norm_okK
#print axioms GapLimit.bound_limit
#print axioms GapLimit.gap_limit
#print axioms GapZd.gap_Zd
#print axioms Gauss.gm_succ2
#print axioms Gauss.G_ibp
#print axioms Gauss.gm_odd
#print axioms Gauss.gm_values
#print axioms GaussCatalan.addR
#print axioms GaussCatalan.pascal_cp
#print axioms GaussCatalan.dl_ne
#print axioms GaussCatalan.P_far
#print axioms GaussCatalan.ch_mid
#print axioms GaussCatalan.refl
#print axioms GaussCatalan.mom_fact
#print axioms GaussCatalan.fact_double
#print axioms GaussCatalan.catalan_gauss
#print axioms GaussCatalan.catalan_check
#print axioms GaussH0.rot_inv_poly
#print axioms GaussH0.bi_inv_poly
#print axioms GaussH0.sphere_consist
#print axioms GaussHermite.lenP_cre
#print axioms GaussHermite.lenP_creK
#print axioms GaussHermite.lenP_dPk
#print axioms GaussHermite.dPk_comm
#print axioms GaussHermite.gp_comm
#print axioms GaussHermite.adj
#print axioms GaussHermite.rodrigues
#print axioms GaussHermite.herm_dk
#print axioms GaussHermite.comm_row
#print axioms GaussHermite.comm_xi
#print axioms GaussHermite.dP_scale
#print axioms GaussHermite.xi_scale
#print axioms GaussHermite.comm_cre
#print axioms GaussHermite.Gp_xi_unit
#print axioms GaussHermite.eqv_xi
#print axioms GaussHermite.eqv_cre
#print axioms GaussHermite.cre_scale
#print axioms GaussHermite.lenP_He
#print axioms GaussHermite.dP_He
#print axioms GaussHermite.eqv_dPk
#print axioms GaussHermite.dPk_scale
#print axioms GaussHermite.dPk_add
#print axioms GaussHermite.dPk_nil
#print axioms GaussHermite.ff_fact
#print axioms GaussHermite.dPk_He
#print axioms GaussHermite.dPk_He_low
#print axioms GaussHermite.Gp_eqv
#print axioms GaussHermite.herm_sq
#print axioms GaussHermite.split_lt
#print axioms GaussHermite.herm_orth
#print axioms GaussHermite.lenP_Hm
#print axioms GaussHermite.lenP_Dm
#print axioms GaussHermite.rodrigues_m
#print axioms GaussHermite.nth_inc_ne
#print axioms GaussHermite.nth_dec_ne
#print axioms GaussHermite.dec_inc_comm
#print axioms GaussHermite.dec_dec_comm
#print axioms GaussHermite.dP_xi_cross
#print axioms GaussHermite.dP_dP_cross
#print axioms GaussHermite.dP_cre_cross
#print axioms GaussHermite.dP_creK_cross
#print axioms GaussHermite.dPk_creK_cross
#print axioms GaussHermite.Dm_creK
#print axioms GaussHermite.cre_nil
#print axioms GaussHermite.creK_nil
#print axioms GaussHermite.eqv_creK
#print axioms GaussHermite.dP_Hm_low
#print axioms GaussHermite.dP_creK_F
#print axioms GaussHermite.dPk_creK_F
#print axioms GaussHermite.dPk_creK_low
#print axioms GaussHermite.Dm_scale
#print axioms GaussHermite.Dm_nil
#print axioms GaussHermite.eqv_Dm
#print axioms GaussHermite.Gp_creK_pos
#print axioms GaussHermite.split_le
#print axioms GaussHermite.step_le
#print axioms GaussHermite.step_gt
#print axioms GaussHermite.diag_m
#print axioms GaussHermite.off_m
#print axioms GaussHermite.herm_m
#print axioms GaussHermite.inc_inc_comm
#print axioms GaussHermite.xi_xi_comm
#print axioms GaussHermite.xi_cre_cross
#print axioms GaussHermite.xi_creK_cross
#print axioms GaussHermite.eqv_cre_app
#print axioms GaussHermite.eqv_creK_app
#print axioms GaussHermite.eqv_creK_scale
#print axioms GaussHermite.xi_eq_cre
#print axioms GaussHermite.xi_Hm
#print axioms GaussPoly.Gp_app
#print axioms GaussPoly.Gp_ibp
#print axioms GaussPoly.len_decAt
#print axioms GaussPoly.incAt_decAt
#print axioms GaussPoly.sum_decAt
#print axioms GaussPoly.exists_pos
#print axioms GaussPoly.zero_sum
#print axioms GaussPoly.GM_zeros
#print axioms GaussPoly.unique_mono
#print axioms GaussPoly.nth_addE
#print axioms GaussPoly.dec_addE_l
#print axioms GaussPoly.dec_addE_r
#print axioms GaussPoly.Gp_map_mulT
#print axioms GaussPoly.Gp_mulP_cons
#print axioms GaussPoly.leib_TT
#print axioms GaussPoly.Gp_cons'
#print axioms GaussPoly.dP_app
#print axioms GaussPoly.leib_TL
#print axioms GaussPoly.leib
#print axioms GaussPoly.addE_comm
#print axioms GaussPoly.Gp_row
#print axioms GaussPoly.Gp_mulP
#print axioms GaussPoly.Bf_cons_r
#print axioms GaussPoly.row_col
#print axioms GaussPoly.Bf_comm
#print axioms GaussPoly.addE_assoc
#print axioms GaussPoly.row_app
#print axioms GaussPoly.Bf_app_l
#print axioms GaussPoly.row_map
#print axioms GaussPoly.row_mulP
#print axioms GaussPoly.Bf_assoc
#print axioms GaussPoly.lenP_app
#print axioms GaussPoly.len_addE
#print axioms GaussPoly.lenP_map
#print axioms GaussPoly.lenP_mulP
#print axioms GaussPoly.len_incAt
#print axioms GaussPoly.lenP_xi
#print axioms GaussPoly.lenP_dP
#print axioms GaussPoly.wide_of_len
#print axioms GaussPoly.inc_addE
#print axioms GaussPoly.xi_map
#print axioms GaussPoly.xi_app
#print axioms GaussPoly.xi_mulP
#print axioms GaussPoly.dP_pair
#print axioms GaussPoly.eqv_mul_r
#print axioms GaussPoly.eqv_mul_l
#print axioms GaussPoly.eqv_dP
#print axioms GaussPoly.row_single_eq
#print axioms GaussPoly.row_zero_coef
#print axioms GaussPoly.row_coef
#print axioms GaussPoly.leibT_row
#print axioms GaussPoly.Bf_single
#print axioms GaussPoly.leibT_L
#print axioms GaussPoly.leib_eqv
#print axioms GaussPoly.eqv_refl
#print axioms GaussPoly.eqv_symm
#print axioms GaussPoly.eqv_trans
#print axioms GaussPoly.eqv_app
#print axioms GaussPoly.row_scale
#print axioms GaussPoly.Bf_scale
#print axioms GaussPoly.eqv_scale
#print axioms GaussPoly.row_mulT_comm
#print axioms GaussPoly.Bf_map_mulT
#print axioms GaussPoly.Bf_mulP_swap
#print axioms GaussPoly.eqv_mul_comm
#print axioms GaussPoly.mulT_assoc
#print axioms GaussPoly.map_app'
#print axioms GaussPoly.app_assoc'
#print axioms GaussPoly.mulP_app_l
#print axioms GaussPoly.map_mulT_map
#print axioms GaussPoly.mulP_map
#print axioms GaussPoly.mulP_assoc
#print axioms GaussPoly.eqv_mul_assoc
#print axioms GaussPos.lenP_ev
#print axioms GaussPos.lenH_app
#print axioms GaussPos.ev_app
#print axioms GaussPos.lenH_xiH
#print axioms GaussPos.xi_ev
#print axioms GaussPos.Hm_zeros
#print axioms GaussPos.mono_exp
#print axioms GaussPos.poly_exp
#print axioms GaussPos.gp_app_r
#print axioms GaussPos.g_row
#print axioms GaussPos.Gp_ev
#print axioms GaussPos.rowQ_split
#print axioms GaussPos.Qf_split_r
#print axioms GaussPos.Qf_split_l
#print axioms GaussPos.term_zero
#print axioms GaussPos.rowQ_rej_zero
#print axioms GaussPos.rowQ_sel_zero
#print axioms GaussPos.Qf_sel_rej
#print axioms GaussPos.Qf_rej_sel
#print axioms GaussPos.allI_sel
#print axioms GaussPos.rowQ_all
#print axioms GaussPos.Qf_all
#print axioms GaussPos.len_rej
#print axioms GaussPos.one_nn
#print axioms GaussPos.Qf_nn
#print axioms GaussPos.gauss_pos
#print axioms GaussQuat.lt4
#print axioms GaussQuat.sum4
#print axioms GaussQuat.qL_orth
#print axioms GaussQuat.qR_orth
#print axioms GaussQuat.bi_inv
#print axioms GaussRot.sumK_ext
#print axioms GaussRot.sumK_extB
#print axioms GaussRot.sumK_add
#print axioms GaussRot.sumK_mul
#print axioms GaussRot.Gp_scale
#print axioms GaussRot.Gp_eq_Bf
#print axioms GaussRot.Bf_mulP_left
#print axioms GaussRot.addE_zeros_l
#print axioms GaussRot.addE_unit
#print axioms GaussRot.Gp_map_unit
#print axioms GaussRot.Gp_linF
#print axioms GaussRot.mix_chain
#print axioms GaussRot.tgt_low
#print axioms GaussRot.tgt_at
#print axioms GaussRot.subst_inc
#print axioms GaussRot.rot_ibp
#print axioms GaussRot.Gp_substP1
#print axioms GaussRot.Gp_mulP_one
#print axioms GaussRot.Gp_subst_zeros
#print axioms GaussRot.rot_inv
#print axioms GaussSphere.sumN_shift
#print axioms GaussSphere.sum_nth
#print axioms GaussSphere.nth_inc_self
#print axioms GaussSphere.dec_inc
#print axioms GaussSphere.sum_incAt
#print axioms GaussSphere.sum_addE
#print axioms GaussSphere.sum_zeros
#print axioms GaussSphere.GM_sq
#print axioms GaussSphere.addE_sq
#print axioms GaussSphere.hom_app
#print axioms GaussSphere.hom_map
#print axioms GaussSphere.hom_mulP
#print axioms GaussSphere.hom_r2F
#print axioms GaussSphere.hom_pow
#print axioms GaussSphere.sumK_cN
#print axioms GaussSphere.row_r2F
#print axioms GaussSphere.Gp_mul_r2
#print axioms GaussSphere.norm_moment
#print axioms GaussSphere.pow4
#print axioms GaussSphere.haar_sphere
#print axioms GaussSubst.nth_zeros
#print axioms GaussSubst.len_zeros
#print axioms GaussSubst.nth_unit_self
#print axioms GaussSubst.nth_unit_ne
#print axioms GaussSubst.dec_unit_self
#print axioms GaussSubst.app_nil
#print axioms GaussSubst.eqv_zero_term
#print axioms GaussSubst.dP_app'
#print axioms GaussSubst.dP_linF
#print axioms GaussSubst.dP_lin
#print axioms GaussSubst.len_unit
#print axioms GaussSubst.lenP_one
#print axioms GaussSubst.lenP_linF
#print axioms GaussSubst.lenP_pow
#print axioms GaussSubst.lenP_scale
#print axioms GaussSubst.scale_map
#print axioms GaussSubst.scale_app
#print axioms GaussSubst.scale_mulP
#print axioms GaussSubst.eqv_scale_one
#print axioms GaussSubst.eqv_add_scale
#print axioms GaussSubst.dP_one
#print axioms GaussSubst.mulP_nil
#print axioms GaussSubst.dP_pow
#print axioms GaussSubst.lenP_substM
#print axioms GaussSubst.scale_scale
#print axioms GaussSubst.map_mulT_scale
#print axioms GaussSubst.mulP_scale_r
#print axioms GaussSubst.eqv_scale_zero
#print axioms GaussSubst.addE_zeros
#print axioms GaussSubst.mulP_one
#print axioms GaussSubst.chain
#print axioms Geom.natR_add
#print axioms Geom.natR_mul
#print axioms Geom.natR_pow
#print axioms Geom.natR_nn
#print axioms Geom.natR_mono
#print axioms Geom.tail_zero
#print axioms Geom.geom_tail
#print axioms Geom.rng_tail
#print axioms Geom.ccount_zero
#print axioms Geom.shell_bound
#print axioms GramChi.C_pos
#print axioms GramChi.castC_ne
#print axioms GramChi.tr_entry
#print axioms GramChi.snrm_dag
#print axioms GramChi.gram_chi
#print axioms GroupAverage.average_invariant
#print axioms GroupAverage.average_idem
#print axioms GroupAverage.average_zero
#print axioms HaarFin.pwN_mul
#print axioms HaarFin.pwN_one
#print axioms HaarFin.pwN_nn
#print axioms HaarFin.getO_set
#print axioms HaarFin.sum_set
#print axioms HaarFin.memL_join
#print axioms HaarFin.memL_map'
#print axioms HaarFin.mem_cfgs_len
#print axioms HaarFin.setG_ge
#print axioms HaarPoly.adm_prodW
#print axioms HaarPoly.I_sumK
#print axioms HaarPoly.ite_app
#print axioms HaarPoly.adm_ite
#print axioms HaarPoly.I_ite
#print axioms HaarPoly.gen_exp
#print axioms HaarPoly.ZW_exp_H
#print axioms HaarPoly.Z0_exp_H
#print axioms HaarPoly.pair_fact_H
#print axioms HaarPoly.cluster_id_H
#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
#print axioms Heat2D.natMul_succ
#print axioms Heat2D.E_nat
#print axioms Heat2D.heat_strip
#print axioms HeatPair.heat_pair
#print axioms HeatPair.dot_off
#print axioms HeatPair.dot_cauchy
#print axioms HeatPair.corrS_cons
#print axioms HeatPair.anti_ext
#print axioms HeatPair.anti_add
#print axioms HeatPair.anti_diag
#print axioms HeatPair.heat_anti
#print axioms HeatPair.co_addL
#print axioms HeatPair.co_smulL
#print axioms HeatPair.anti_zero
#print axioms HeatPair.co_mulL
#print axioms HeatPair.dot_dl
#print axioms HeatPair.cgc_zero
#print axioms HeatPair.cgc_one
#print axioms HeatTube.len_pw4
#print axioms HeatTube.decM_heatR
#print axioms HeatTube.pw4_ones
#print axioms HeatTube.decM_heat_ones
#print axioms HeatTube.pw4_tube
#print axioms HeatTube.decM_tube
#print axioms Indep.sum_const
#print axioms Indep.dep_tail
#print axioms Indep.dep_free
#print axioms Indep.indep
#print axioms InfVol4D.near_tri
#print axioms InfVol4D.two_pow_ge
#print axioms InfVol4D.near_weaken
#print axioms InfVol4D.near_symm
#print axioms InfVol4D.cauchy_of_rate
#print axioms InfVol4D.inf_vol
#print axioms InfVol4D.gap_inf
#print axioms InjCount.len_erase
#print axioms InjCount.mem_erase
#print axioms InjCount.count_inj
#print axioms Join.disjN_nth
#print axioms Join.prodW_swap
#print axioms Join.len_pairs
#print axioms Join.vanish_join
#print axioms L1Full.pairwise
#print axioms L1Full.P_ge_first
#print axioms L1Full.P_zero_far
#print axioms L1Full.cancel_add
#print axioms L1Full.cancel_le
#print axioms L1Full.Psi_ge
#print axioms L1Full.ne_of_ge
#print axioms L1Full.one_step_eq
#print axioms L1Full.dist_symm
#print axioms L1Full.cancel_add_r
#print axioms L1Full.cg_comm
#print axioms L1Full.cgc_comm
#print axioms L1Full.l1
#print axioms L1Full.cgc_ge
#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.term_le_sum
#print axioms L1Full.cgc_far
#print axioms L1Full.co_prodL
#print axioms L1Full.Good_prod
#print axioms L1Zero.sum_le
#print axioms L1Zero.pre
#print axioms L1Zero.reidx
#print axioms L1Zero.sum_sub
#print axioms L1Zero.l1_zero
#print axioms L1Zero.ex_add
#print axioms L1Zero.heat_l1_zero
#print axioms L1Zero.vv_l1_zero
#print axioms Lad3.rect_char
#print axioms Lad5.inv_unique'
#print axioms Lad5.div_div
#print axioms Lad5.vol_step
#print axioms Lad5.cauchy
#print axioms Lad5Cauchy.sum_rng_map
#print axioms Lad5Cauchy.uniform_cauchy
#print axioms LinfDist.le_mx_l
#print axioms LinfDist.le_mx_r
#print axioms LinfDist.mx_le
#print axioms LinfDist.ad_le1
#print axioms LinfDist.ad_le2
#print axioms LinfDist.le_cancel_r
#print axioms LinfDist.sub_le_of
#print axioms LinfDist.ad_le
#print axioms LinfDist.nthN_hd
#print axioms LinfDist.nthN_tl
#print axioms LinfDist.le_maxL
#print axioms LinfDist.maxL_le
#print axioms LinfDist.dcl_dist
#print axioms LinfDist.dist_le
#print axioms LinfDist.shell_of_dist
#print axioms LinkFactor.sumK_app
#print axioms LinkFactor.sumK_join
#print axioms LinkFactor.sumK_map
#print axioms LinkFactor.cfg_factor
#print axioms LinkFactor.prodSums_zero
#print axioms LinkFactor.cfg_vanish
#print axioms LinkFactor.rep_vanish
#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
#print axioms MaskMono.diff_exists
#print axioms MaskMono.leB_upd
#print axioms MaskMono.G0_ge_one
#print axioms MaskMono.mask_mono
#print axioms MaskSum.sum_zero
#print axioms MaskSum.sumK_app
#print axioms MaskSum.sumK_map
#print axioms MaskSum.ip
#print axioms MaskSum.ineg
#print axioms MaskSum.cons_eq_iff
#print axioms MaskSum.sel
#print axioms MaskSum.sumK_ext_mem
#print axioms MaskSum.len_orB
#print axioms MaskSum.sumK_le
#print axioms MaskSum.inj_bound
#print axioms MaskSum.G_mask
#print axioms MeanZero.split_cfg
#print axioms MeanZero.single_cover
#print axioms MeanZero.mean_zero_transl
#print axioms Migdal.migdal_2d
#print axioms Migdal.pw_pw
#print axioms Migdal.iter4_eq
#print axioms Migdal.total_blockIt
#print axioms Migdal.block_exact
#print axioms MigdalCone.len_lmap
#print axioms MigdalCone.bd_of_fun
#print axioms MigdalCone.dot_head_le
#print axioms MigdalCone.c0_ge_one
#print axioms MigdalCone.four_ge_one
#print axioms MigdalCone.four_le_pow
#print axioms MigdalCone.norm_prod_cone
#print axioms MigdalCone.dec_cone
#print axioms MigdalCone.nprod_head
#print axioms MigdalCone.mk_cone
#print axioms MigdalCone.bd_mono
#print axioms MigdalCone.decM_head
#print axioms MigdalCone.mk_invariant
#print axioms MigdalCone.mk_head
#print axioms MigdalCone.mk_head_nn
#print axioms MigdalCone.mk_forever
#print axioms MigdalCone.mk_invariant16
#print axioms MigdalCone.mk_forever16
#print axioms MigdalDecay.mk_gain
#print axioms MigdalDecay.mk_decay
#print axioms MigdalStrong.gsum_exact
#print axioms MigdalStrong.gsum_nn
#print axioms MigdalStrong.gsum_le_two
#print axioms MigdalStrong.ne_of_one_le
#print axioms MigdalStrong.inv_le_one
#print axioms MigdalStrong.bnd_le
#print axioms MigdalStrong.bS_le
#print axioms MigdalStrong.c0_le_two
#print axioms MigdalStrong.c1_le
#print axioms MigdalStrong.c1_ge
#print axioms MigdalStrong.migdal4_strong
#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
#print axioms OField.inv_ne
#print axioms OField.div_bnd
#print axioms OField.cov_bound
#print axioms OField.step_bound
#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
#print axioms Peel.peel
#print axioms Peel.peelZ
#print axioms Peel.tr_conj
#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
#print axioms Poly.touch_of_nth
#print axioms Poly.uSupp_nth
#print axioms Poly.len_padB
#print axioms Poly.padB_eq
#print axioms Poly.len_map'
#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.le_orB_l
#print axioms Poly.le_orB_r
#print axioms Poly.cond_eq
#print axioms Poly.sub_right
#print axioms Poly.cluster_id
#print axioms PolyBound.cfg_count
#print axioms PolyBound.cnt_orB_le
#print axioms PolyBound.cnt_map_or
#print axioms PolyBound.cnt_map_false
#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
#print axioms PosRatio.prodW_split
#print axioms PosRatio.prodW_ge
#print axioms PosRatio.prodW_nn
#print axioms PosRatio.prodW_le
#print axioms PosRatio.ratio_pos
#print axioms QuatTrace.mul_neg_r
#print axioms QuatTrace.e11
#print axioms QuatTrace.e22
#print axioms QuatTrace.retr_dot
#print axioms QuatUnit.sq_nn
#print axioms QuatUnit.sq_le_one
#print axioms QuatUnit.unit_t
#print axioms R7.two_eq
#print axioms R7.r7_le
#print axioms RPChi.ker_LJ
#print axioms RPChi.ker_LK
#print axioms RPChi.w_nn
#print axioms RPChi.allNN_LJ
#print axioms RPChi.allNN_app'
#print axioms RPChi.allNN_LK
#print axioms RPChi.rp_chi
#print axioms RPDot.Q_add
#print axioms RPDot.Q_smul
#print axioms RPDot.szero
#print axioms RPDot.Q_zero
#print axioms RPDot.rp_dot
#print axioms RPDot.Q_ext
#print axioms RPDot.rp_weight
#print axioms RPFlow.rp_of_cone
#print axioms RPFlow.rp_flow_j
#print axioms RPLimit.rp_approx
#print axioms Ratio.ite_iff
#print axioms Ratio.cons_iff
#print axioms Ratio.head_iff
#print axioms Ratio.decomp_iff
#print axioms Ratio.len_notB
#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.leB_andB_mono
#print axioms Ratio.sub_mono
#print axioms Ratio.ratio
#print axioms Reach.anyT_mono
#print axioms Reach.V_mono
#print axioms Reach.andB_mono2
#print axioms Reach.iter_mono
#print axioms Reach.stable
#print axioms Reach.exists_out
#print axioms Reach.nth_tr
#print axioms Reach.reach
#print axioms Reach.T_tail
#print axioms RectGeom.inv_mul_rev
#print axioms RectGeom.inv_inv'
#print axioms RectGeom.pl_X
#print axioms RectGeom.hp_tp
#print axioms RectGeom.hinv_tinv
#print axioms RectGeom.Wl_zero
#print axioms RectGeom.gprod_Xb
#print axioms RectGeom.peel_id
#print axioms RectInt.vals_hs
#print axioms RectInt.mem_hs
#print axioms RectInt.nodup_hs
#print axioms RectInt.zip_xl
#print axioms RectInt.gprod_xl
#print axioms RectInt.xl_congr
#print axioms RectInt.plaqProd_congr
#print axioms RectInt.plaqProd_congr2
#print axioms RectInt.len_hs
#print axioms RectInt.rgt_congr
#print axioms RectInt.linv_congr
#print axioms RectInt.hinv_congr
#print axioms RectInt.Hf_upd
#print axioms RectInt.Vf_upd
#print axioms RectInt.row_step
#print axioms RectInt.row_stepZ
#print axioms RectInt.indep_const
#print axioms RectInt.rect_W
#print axioms RectInt.rect_Z
#print axioms RectLoop.wsum_ind
#print axioms RectLoop.flux_rect
#print axioms RectLoop.len_app
#print axioms RectLoop.mem_app
#print axioms RectLoop.nodup_app
#print axioms RectLoop.len_rowL
#print axioms RectLoop.len_sqs
#print axioms RectLoop.mem_rowL
#print axioms RectLoop.nodup_rowL
#print axioms RectLoop.mem_sqs
#print axioms RectLoop.nodup_sqs
#print axioms RectLoop.rect_area
#print axioms SU2Char.cN_add
#print axioms SU2Char.ev_addL
#print axioms SU2Char.ev_smulL
#print axioms SU2Char.chi_rec
#print axioms SU2Char.dot_addL
#print axioms SU2Char.dot_smulL
#print axioms SU2Char.dot_shift
#print axioms SU2Char.dot_ext_ge
#print axioms SU2Char.dot_ext
#print axioms SU2Char.dot_fadd
#print axioms SU2Char.dot_fsmul
#print axioms SU2Char.dot_fzero
#print axioms SU2Char.dot_mulL
#print axioms SU2Char.dot_swap
#print axioms SU2Char.walk_chi
#print axioms SU2Char.I_dot
#print axioms SU2Char.invariance
#print axioms SU2Char.I_mul_dot
#print axioms SU2Char.orthogonality
#print axioms SU2Char.dot_combo
#print axioms SU2Char.dl_lt
#print axioms SU2Char.dl_gt
#print axioms SU2Char.dl_self
#print axioms SU2Char.dot_dl_zero
#print axioms SU2Char.diag_ssq
#print axioms SU2Char.norm_sq
#print axioms SU2Char.positivity
#print axioms SU2CharExp.ch_zero_r
#print axioms SU2CharExp.succ_ne
#print axioms SU2CharExp.ch_self
#print axioms SU2CharExp.ch_fact_step
#print axioms SU2CharExp.ch_fact
#print axioms SU2CharExp.ch_pos
#print axioms SU2CharExp.fact_mul_le
#print axioms SU2CharExp.moment_chi
#print axioms SU2CharExp.P_far
#print axioms SU2CharExp.P_le_ch
#print axioms SU2CharExp.key_nat
#print axioms SU2CharExp.cN_nn
#print axioms SU2CharExp.cN_mono
#print axioms SU2CharExp.cN_mul
#print axioms SU2CharExp.cN_one
#print axioms SU2CharExp.fact_pos
#print axioms SU2CharExp.pos_of_ge_one
#print axioms SU2CharExp.inv_uniq
#print axioms SU2CharExp.frac_le
#print axioms SU2CharExp.term_nn
#print axioms SU2CharExp.term_le
#print axioms SU2CharExp.cS_nn
#print axioms SU2CharExp.c_bound
#print axioms SU2CharExp.c0_ge_one
#print axioms SU2CharExp.ratio_bound
#print axioms SU2Haar.dl_symm
#print axioms SU2Haar.last_step
#print axioms SU2Haar.P_symm
#print axioms SU2Haar.sN_map
#print axioms SU2Haar.sN_add
#print axioms SU2Haar.sN_ext
#print axioms SU2Haar.first_return
#print axioms SU2Haar.catalan
#print axioms SU2Haar.mom_values
#print axioms SU2Haar.P_parity
#print axioms SU2Haar.parity_double_succ
#print axioms SU2Haar.odd_zero
#print axioms SU2Haar.Im_add
#print axioms SU2Haar.Im_smul
#print axioms SU2Haar.Im_shift
#print axioms SU2Haar.I_add
#print axioms SU2Haar.I_smul
#print axioms SU2Haar.I_one
#print axioms SU2Strong4D.pw_mono
#print axioms SU2Strong4D.pw_one
#print axioms SU2Strong4D.pw_ge_one
#print axioms SU2Strong4D.small_4d
#print axioms SU2Strong4D.beta_consts
#print axioms SU2Strong4D.gap_4d
#print axioms SU2Strong4D.area_4d
#print axioms SU2Wilson2D.ev_mulL
#print axioms SU2Wilson2D.coeff_chi
#print axioms SU2Wilson2D.ev_chi1
#print axioms SU2Wilson2D.wilson_2d
#print axioms Sandwich.sandwich
#print axioms SchurAvg.commute
#print axioms SchurAvg.schur_avg
#print axioms Seeds.nth_andB
#print axioms Seeds.nth_vAux
#print axioms Seeds.anyT_of
#print axioms Seeds.anyT_ex
#print axioms Seeds.leB_of
#print axioms Seeds.of_leB
#print axioms Seeds.leB_antisymm
#print axioms Seeds.nthB_ge
#print axioms Seeds.nth_upd
#print axioms Seeds.len_upd
#print axioms Seeds.nth_fz
#print axioms Seeds.Vs_ge
#print axioms Seeds.hV
#print axioms Seeds.closed
#print axioms Seeds.iter_nth
#print axioms Seeds.remove_seed
#print axioms Seeds.no_seed
#print axioms ShellSum.tele_var
#print axioms ShellSum.mem_rng
#print axioms ShellSum.sel_rng
#print axioms ShellSum.group_sum
#print axioms ShellSum.len_tI
#print axioms ShellSum.mem_tI
#print axioms ShellSum.shell_count
#print axioms Signed.bij_sum
#print axioms Signed.uniq_neg
#print axioms Signed.neg_le_neg
#print axioms Signed.neg_add'
#print axioms Signed.bnd_sum
#print axioms Signed.neg_mul'
#print axioms Signed.neg_neg'
#print axioms Signed.le_neg_of
#print axioms Signed.bnd_mul
#print axioms Sites.IntS_app
#print axioms Sites.IntS_ext
#print axioms Sites.mu_const
#print axioms Sites.IntS_const
#print axioms Sites.IntS_smul
#print axioms Sites.vals_upd_notin
#print axioms Sites.IntS_row
#print axioms SmallQ.two_pow_h
#print axioms SmallQ.pw_anti
#print axioms SmallQ.ratio
#print axioms SmallQ.tN_nn
#print axioms SmallQ.neg_le
#print axioms SmallQ.tP_nn
#print axioms SmallQ.sum_nn
#print axioms SmallQ.small_q
#print axioms SpecGap.lower_pow
#print axioms SpecGap.pw_ne
#print axioms SpecGap.rate_le
#print axioms SpecGap.eig_gap
#print axioms Strip2D.It_ext
#print axioms Strip2D.It_smul
#print axioms Strip2D.It_add
#print axioms Strip2D.It_zero
#print axioms Strip2D.It_sum
#print axioms Strip2D.mu_sum
#print axioms Strip2D.strip
#print axioms Strip2D.wilson_strip
#print axioms StripAxial.len_xl
#print axioms StripAxial.tele
#print axioms StripAxial.tp_tinv
#print axioms StripAxial.XY_one
#print axioms StripAxial.zip_cancel
#print axioms StripAxial.xl_map_inv
#print axioms StripAxial.len_zipMul
#print axioms StripAxial.len_map_g
#print axioms StripAxial.It_ext_len
#print axioms StripAxial.strip_axial
#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
#print axioms SupportVec.len_map
#print axioms SupportVec.len_vec
#print axioms SupportVec.spP_vec
#print axioms SupportVec.depOn_vec
#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
#print axioms TMCertQ.finalCq_sound
#print axioms TMCertQ.chain_soundQ
#print axioms TMCertT.rep_pad
#print axioms TMCertT.abs_scaled
#print axioms TMCertT.cert_modelT
#print axioms TMCertT8.cert_modelT8
#print axioms TMCoverT.allCP_app
#print axioms TMCoverT.modelCP_of
#print axioms TMCoverT.allCP_one
#print axioms TMCoverT.cover_listT
#print axioms TMCoverT8.allCP8_app
#print axioms TMCoverT8.chainCQ_step
#print axioms TMCoverT8.modelCP8_of
#print axioms TMCoverT8.allCP8_one
#print axioms TMCoverT8.cover_listT8
#print axioms Tail.term_eq
#print axioms Tail.pw_le_one
#print axioms Tail.pw_tail
#print axioms Tail.CW_tail
#print axioms TouchCount.nzb_add_eq
#print axioms TouchCount.or_false'
#print axioms TouchCount.touch_zadd
#print axioms TouchCount.cnt_or
#print axioms TouchCount.sc_le
#print axioms TripleH.ratioP
#print axioms TripleH.condP_up
#print axioms TripleH.tailP
#print axioms TripleH.sg_le
#print axioms TripleH.defect
#print axioms TripleH.E1_le
#print axioms TripleH.big_q
#print axioms TripleH.margin_nn
#print axioms TruncNum.step
#print axioms TruncNum.fs
#print axioms TruncNum.nat4
#print axioms TruncNum.nat_all
#print axioms TruncNum.bnd29
#print axioms TruncNum.trunc29
#print axioms TruncTail.bnd_halve
#print axioms TruncTail.tail
#print axioms UFlow.dot_co_self
#print axioms UFlow.dot_co_up
#print axioms UFlow.dot_co_cons
#print axioms UFlow.dot_co_down
#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 Vanish.prodW_pick
#print axioms Vanish.nth_upd'
#print axioms Vanish.vanish_plaq
#print axioms Vanish.vanish_loop
#print axioms VanishH.vanish_plaq_H
#print axioms VanishH.vanish_loop_H
#print axioms VolOrder.nthL_vec
#print axioms VolOrder.nthP_mem
#print axioms VolOrder.nthP_inj
#print axioms VolOrder.Ms_len
#print axioms VolOrder.Ms_keep
#print axioms VolOrder.Ms_off
#print axioms VolOrder.nodup_snoc
#print axioms VolOrder.mem_map_rng
#print axioms VolOrder.map_snoc
#print axioms VolOrder.nodup_map_rng
#print axioms Volume.upd_upd
#print axioms Volume.nth_upd_self
#print axioms Volume.vol_add
#print axioms Volume.tele
#print axioms VolumeMono.gks_step
#print axioms VolumeMono.mem_app_len
#print axioms VolumeMono.volume_mono
#print axioms WeightBound.weight_bound
#print axioms WeightBound.weight_le_one
#print axioms WeightPosJ.weight_pos_j
#print axioms WilsonCauchy.B_le
#print axioms WilsonCauchy.wilson_cauchy
#print axioms WilsonCauchy.wilson_cauchy_W
#print axioms WilsonCauchyD.wilson_cauchy_d
#print axioms WilsonCone.two_tn_le
#print axioms WilsonCone.two_tn_le_gen
#print axioms WilsonCone.wilson_cone
#print axioms WilsonCone.wilson_forever
#print axioms WilsonCone.wilson_forever16
#print axioms WilsonGap.wilson_gap
#print axioms WilsonGap.wilson_gap16
#print axioms WilsonGapK.wilson_cone_j
#print axioms WilsonGapK.wilson_gap_k
#print axioms WilsonGram.ker_app
#print axioms WilsonGram.ker_scale
#print axioms WilsonGram.ker_one
#print axioms WilsonGram.ker_pow
#print axioms WilsonGram.ker_dot
#print axioms WilsonGram.ker_series
#print axioms WilsonGram.allNN_app
#print axioms WilsonGram.allNN_scale
#print axioms WilsonGram.allNN_one
#print axioms WilsonGram.allNN_pow
#print axioms WilsonGram.allNN_dot
#print axioms WilsonGram.allNN_series
#print axioms WilsonGram.wilson_rp
#print axioms WilsonVol.Z_ge
#print axioms WilsonVol.Z_pos
#print axioms WilsonVol.Ew_ge
#print axioms WilsonVolStep.vol_step_k
#print axioms Window.P_succ
#print axioms Window.P_shift
#print axioms Window.inner_sum
#print axioms Window.Del_zero
#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 ZdDegree.nthN_vec
#print axioms ZdDegree.slot_inj
#print axioms ZdDegree.slot_mem
#print axioms ZdDegree.cnt_map_eq
#print axioms ZdDegree.map_map_g
#print axioms ZdDegree.cnt_false
#print axioms ZdDegree.supp_le4
#print axioms ZdDegree.linkdeg_vec
#print axioms ZdDegree.deg_Zd
#print axioms ZdDist.dcl_symm
#print axioms ZdDist.dcl_trans
#print axioms ZdDist.dcl_mono
#print axioms ZdDist.shf_coord
#print axioms ZdDist.link_point
#print axioms ZdDist.shared_dcl
#print axioms ZdDist.touch_ex
#print axioms ZdDist.touch_shared
#print axioms ZdDist.touch_dcl
#print axioms ZdDist.nthL_map
#print axioms ZdDist.ball_dist
#print axioms ZdDist.far_ok
#print axioms ZdLinks.len_keysAll
#print axioms ZdLinks.mem_keysAll
#print axioms ZdLinks.shf_ne
#print axioms ZdLinks.shf_inj
#print axioms ZdLinks.len_shf
#print axioms ZdLinks.linkIn_cases
#print axioms ZdLinks.side_eq
#print axioms ZdLinks.key_inj
#print axioms ZdLinks.key_mem
#print axioms ZdLinks.linkdeg_Zd

ym/AxialAlg.lean

lean_all/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/BallSize.lean

lean_all/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/BallStep.lean

lean_all/ym/BallStep.lean

/-
  ym/BallStep — T45, G17b: how the ball grows. With N = #P:
    len_ball     (ball σ P r).length = N
    ball_seed    σ (P_i) ⇒ i ∈ ball 1
    ball_step    j ∈ ball r, P_i touches P_j ⇒ i ∈ ball (r + 1)
    ball_mono    i ∈ ball r ⇒ i ∈ ball (r + 1)
  Lean 4 core only, no axioms.
-/
import ym.Reach
import ym.Count

namespace BallStep

open YMChain Closure Factor Seeds Reach AreaUpper Count

theorem len_vAux (r : List Nat → Bool) : ∀ (P : List (List Nat)) (A : List Bool), A.length = P.length →
    (vAux r P A).length = P.length
  | [], _, _ => rfl
  | _ :: _, [], h => absurd h Nat.noConfusion
  | _ :: P, _ :: A, h => congrArg Nat.succ (len_vAux r P A (Nat.succ.inj h))

theorem len_ball (σ : List Nat → Bool) (P : List (List Nat)) : ∀ r, (ball σ P r).length = P.length
  | 0 => len_fz P.length
  | r + 1 => by
    show (andB (tr P.length) (Vs σ P (ball σ P r))).length = P.length
    rw [len_andB _ _ (by rw [len_tr]; exact (len_vAux _ P _ (len_ball σ P r)).symm), len_tr]

theorem nth_ball_succ (σ : List Nat → Bool) (P : List (List Nat)) (r i : Nat) (hi : i < P.length) :
    nthB (ball σ P (r + 1)) i = (nthB (ball σ P r) i || (σ (nthL P i) || anyT P (ball σ P r) (nthL P i))) := by
  show nthB (andB (tr P.length) (Vs σ P (ball σ P r))) i = _
  rw [nth_andB, nth_tr P.length i hi, Bool.true_and]
  exact nth_vAux _ P _ i (len_ball σ P r) hi

theorem ball_mono (σ : List Nat → Bool) (P : List (List Nat)) (r i : Nat) (hi : i < P.length)
    (h : nthB (ball σ P r) i = true) : nthB (ball σ P (r + 1)) i = true := by
  rw [nth_ball_succ σ P r i hi, h]; rfl

theorem ball_seed (σ : List Nat → Bool) (P : List (List Nat)) (i : Nat) (hi : i < P.length)
    (h : σ (nthL P i) = true) : nthB (ball σ P 1) i = true := by
  rw [nth_ball_succ σ P 0 i hi, h]; cases nthB (ball σ P 0) i <;> rfl

theorem ball_step (σ : List Nat → Bool) (P : List (List Nat)) (r i j : Nat) (hi : i < P.length) (hj : j < P.length)
    (hjr : nthB (ball σ P r) j = true) (ht : touch (nthL P i) (nthL P j) = true) : nthB (ball σ P (r + 1)) i = true := by
  rw [nth_ball_succ σ P r i hi, anyT_of P (ball σ P r) (nthL P i) j hjr ht hj]
  cases nthB (ball σ P r) i <;> cases σ (nthL P i) <;> rfl

theorem band_l {a b : Bool} (h : (a && b) = true) : a = true := by cases a <;> cases b <;> first | rfl | exact Bool.noConfusion h
theorem band_r {a b : Bool} (h : (a && b) = true) : b = true := by cases a <;> cases b <;> first | rfl | exact Bool.noConfusion h

/-- a touching path from index j: each next index is in range and touches the previous one -/
def pathOK (P : List (List Nat)) : Nat → List Nat → Bool
  | _, [] => true
  | j, k :: ks => (Nat.blt k P.length && touch (nthL P k) (nthL P j)) && pathOK P k ks

def lastI : Nat → List Nat → Nat
  | j, [] => j
  | _, k :: ks => lastI k ks

/-- **Path lemma:** j ∈ ball r and a touching path of length m from j ⇒ its end ∈ ball (r + m). -/
theorem ball_path (σ : List Nat → Bool) (P : List (List Nat)) :
    ∀ (ks : List Nat) (j r : Nat), nthB (ball σ P r) j = true → j < P.length → pathOK P j ks = true →
      nthB (ball σ P (r + ks.length)) (lastI j ks) = true
  | [], _, _, h, _, _ => h
  | k :: ks, j, r, h, hj, hp => by
    have c := band_l hp
    have hk : k < P.length := Nat.le_of_ble_eq_true (band_l c)
    have st := ball_step σ P r k j hk hj h (band_r c)
    have ih := ball_path σ P ks k (r + 1) st hk (band_r hp)
    show nthB (ball σ P (r + (ks.length + 1))) (lastI k ks) = true
    rw [show r + (ks.length + 1) = r + 1 + ks.length from (Nat.succ_add r ks.length).symm]
    exact ih

end BallStep

#print axioms BallStep.len_ball
#print axioms BallStep.ball_seed
#print axioms BallStep.ball_step
#print axioms BallStep.ball_mono
#print axioms BallStep.ball_path

ym/Bern.lean

lean_all/ym/Bern.lean

/-
  ym/Bern — T45, G44 (exponential beats polynomial, ordered-field form): if 0 ≤ Q, 0 ≤ δ and Q(1+δ) ≤ 1 then
    Q^e · (1 + e·δ) ≤ 1   (Bernoulli),   and  Q^{2e}·(e δ)² ≤ 1.
  Used to realise the smallness conditions of TripleH.big_q. Lean 4 core only, no axioms.
-/
import ym.TripleH

namespace Bern

open YMChain LinkIntegral SU2Char

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

theorem bern (h1 : O.le K.zero K.one) (Q d : R) (hQ : O.le K.zero Q) (hd : O.le K.zero d)
    (h : O.le (K.mul Q (K.add K.one d)) K.one) : ∀ e, O.le (K.mul (pwR K Q e) (K.add K.one (K.mul (cN K e) d))) K.one
  | 0 => by
    show O.le (K.mul K.one (K.add K.one (K.mul K.zero d))) K.one
    rw [show K.mul K.one (K.add K.one (K.mul K.zero d)) = K.one by lib_ring]; exact O.le_refl _
  | e + 1 => by
    have ih := bern h1 Q d hQ hd h e
    have ce := SU2CharExp.cN_nn K O h1 e
    -- Q^{e+1}(1+(e+1)d) ≤ Q^{e+1}(1+ed)(1+d) = [Q^e(1+ed)]·[Q(1+d)] ≤ 1
    have extra : O.le K.zero (K.mul (K.mul (cN K e) d) d) := O.mul_nonneg _ _ (O.mul_nonneg _ _ ce hd) hd
    have pq := O.mul_nonneg _ _ (AreaLower.pw_nn K O Q hQ h1 e) hQ
    have s1 : O.le (K.mul (pwR K Q (e + 1)) (K.add K.one (K.mul (cN K (e + 1)) d)))
        (K.mul (K.mul (pwR K Q e) (K.add K.one (K.mul (cN K e) d))) (K.mul Q (K.add K.one d))) := by
      have := O.mul_le_mul_right _ _ _ (O.add_le_add _ _ _ _ (O.le_refl (K.add K.one (K.mul (cN K (e + 1)) d))) extra) pq
      rw [DetChain.add_zero'] at this
      refine O.le_trans _ _ _ (TripleE.le_of_eq' K O _ _ ?_) (O.le_trans _ _ _ this (TripleE.le_of_eq' K O _ _ ?_))
      · show K.mul (K.mul (pwR K Q e) Q) _ = _; lib_ring
      · rw [cN_add, SU2CharExp.cN_one]; lib_ring
    have n1 : O.le K.zero (K.mul (pwR K Q e) (K.add K.one (K.mul (cN K e) d))) :=
      O.mul_nonneg _ _ (AreaLower.pw_nn K O Q hQ h1 e) (Lambert.one_pos K O h1 _ (O.mul_nonneg _ _ ce hd))
    have s2 := O.mul_le_mul_right _ _ _ ih (O.mul_nonneg _ _ hQ (Lambert.one_pos K O h1 d hd))
    rw [K.one_mul] at s2
    exact O.le_trans _ _ _ s1 (O.le_trans _ _ _ s2 h)

open TripleMom TripleCoef in
/-- **θ₄ side, first bound**: (1−Q)²·P⁻·(−L⁻) ≤ 2N·P⁻ (the inverse in L⁻ cancels against P⁻) -/
theorem PL_le (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) (hQne : Q ≠ K.one) : ∀ N,
    O.le (K.mul (K.mul (K.add K.one (K.neg Q)) (K.add K.one (K.neg Q)))
        (K.mul (Pp K (K.neg K.one) (aQ K Q) N) (K.neg (Lp K O F (K.neg K.one) (aQ K Q) N))))
      (K.mul (K.add (cN K N) (cN K N)) (Pp K (K.neg K.one) (aQ K Q) N)) ∧ O.le K.zero (Pp K (K.neg K.one) (aQ K Q) N)
  | 0 => by
    constructor
    · show O.le (K.mul _ (K.mul K.one (K.neg K.zero))) (K.mul (K.add K.zero K.zero) K.one)
      rw [show K.mul (K.mul (K.add K.one (K.neg Q)) (K.add K.one (K.neg Q))) (K.mul K.one (K.neg K.zero)) = K.zero by lib_ring,
        show K.mul (K.add K.zero K.zero) K.one = K.zero by lib_ring]; exact O.le_refl _
    · exact h1
  | N + 1 => by
    obtain ⟨ih, P0⟩ := PL_le F h1 h10 Q hQ0 hQ1 hQne N
    have a0 : O.le K.zero (aQ K Q N) := AreaLower.pw_nn K O Q hQ0 h1 (2 * N + 1)
    have a1 : O.le (aQ K Q N) K.one := Tail.pw_le_one K O h1 Q hQ0 hQ1 (2 * N + 1)
    have aQ' : O.le (aQ K Q N) Q := by
      have := SmallQ.pw_anti K O h1 Q hQ0 hQ1 1 (2 * N); rw [show 1 + 2 * N = 2 * N + 1 by lib_nat] at this
      exact O.le_trans _ _ _ this (by show O.le (K.mul K.one Q) Q; rw [K.one_mul]; exact O.le_refl _)
    have y0 : O.le K.zero (K.add K.one (K.neg (aQ K Q N))) := QBinomBd.one_sub_nn K O _ a1
    have yne : K.add K.one (K.mul (K.neg K.one) (aQ K Q N)) ≠ K.zero := by
      intro e; apply QBinomBd.pw_ne_one K O h1 Q hQ0 hQ1 hQne (2 * N)
      show aQ K Q N = K.one
      have : aQ K Q N = K.add K.one (K.neg (K.add K.one (K.mul (K.neg K.one) (aQ K Q N)))) := by lib_ring
      rw [this, e]; lib_ring
    have i := F.mul_inv (TripleMom.sq K (K.add K.one (K.mul (K.neg K.one) (aQ K Q N)))) (by
      unfold TripleMom.sq; intro e
      cases O.le_total K.zero (K.add K.one (K.mul (K.neg K.one) (aQ K Q N))) with
      | inl h => exact yne (O.pos_mul_eq_zero _ _ h yne e)
      | inr h => exact yne (O.le_antisymm _ _ h (by rw [show K.mul (K.neg K.one) (aQ K Q N) = K.neg (aQ K Q N) by lib_ring]; exact y0)))
    have P1 : O.le K.zero (Pp K (K.neg K.one) (aQ K Q) (N + 1)) := O.mul_nonneg _ _ P0 (by unfold TripleMom.sq; exact QuatUnit.sq_nn K O _)
    refine ⟨?_, P1⟩
    -- recursion: P' = P y², S' = y² S + 2 a P (with S = P·(−L))
    show O.le (K.mul (K.mul (K.add K.one (K.neg Q)) (K.add K.one (K.neg Q)))
        (K.mul (K.mul (Pp K (K.neg K.one) (aQ K Q) N) (TripleMom.sq K (K.add K.one (K.mul (K.neg K.one) (aQ K Q N)))))
          (K.neg (K.add (Lp K O F (K.neg K.one) (aQ K Q) N) (K.mul (K.add (K.mul (K.neg K.one) (aQ K Q N)) (K.mul (K.neg K.one) (aQ K Q N)))
            (F.inv (TripleMom.sq K (K.add K.one (K.mul (K.neg K.one) (aQ K Q N))))))))))
      (K.mul (K.add (cN K (N + 1)) (cN K (N + 1))) (K.mul (Pp K (K.neg K.one) (aQ K Q) N) (TripleMom.sq K (K.add K.one (K.mul (K.neg K.one) (aQ K Q N))))))
    generalize F.inv (TripleMom.sq K (K.add K.one (K.mul (K.neg K.one) (aQ K Q N)))) = v at i ⊢
    unfold TripleMom.sq at i ⊢
    rw [show K.mul (K.neg K.one) (aQ K Q N) = K.neg (aQ K Q N) by lib_ring] at i ⊢
    generalize Pp K (K.neg K.one) (aQ K Q) N = P at ih P0 ⊢
    generalize Lp K O F (K.neg K.one) (aQ K Q) N = L at ih ⊢
    generalize aQ K Q N = a at a0 a1 aQ' y0 i ⊢
    -- (1−Q)² ≤ (1−a)²
    have dq : O.le (K.mul (K.add K.one (K.neg Q)) (K.add K.one (K.neg Q))) (K.mul (K.add K.one (K.neg a)) (K.add K.one (K.neg a))) := by
      have b : O.le (K.add K.one (K.neg Q)) (K.add K.one (K.neg a)) := by
        have := O.add_le_add _ _ _ _ aQ' (O.le_refl (K.add K.one (K.add (K.neg Q) (K.neg a))))
        rw [show K.add a (K.add K.one (K.add (K.neg Q) (K.neg a))) = K.add K.one (K.neg Q) by lib_ring,
          show K.add Q (K.add K.one (K.add (K.neg Q) (K.neg a))) = K.add K.one (K.neg a) by lib_ring] at this; exact this
      have b0 := QBinomBd.one_sub_nn K O _ hQ1
      exact SU2Strong4D.pw_mono K O h1 _ _ b0 b 2 |> fun h => by
        have e1 : pwR K (K.add K.one (K.neg Q)) 2 = K.mul (K.add K.one (K.neg Q)) (K.add K.one (K.neg Q)) := by
          show K.mul (K.mul K.one _) _ = _; rw [K.one_mul]
        have e2 : pwR K (K.add K.one (K.neg a)) 2 = K.mul (K.add K.one (K.neg a)) (K.add K.one (K.neg a)) := by
          show K.mul (K.mul K.one _) _ = _; rw [K.one_mul]
        rw [e1, e2] at h; exact h
    have yy0 : O.le K.zero (K.mul (K.add K.one (K.neg a)) (K.add K.one (K.neg a))) := O.mul_nonneg _ _ y0 y0
    have q0 : O.le K.zero (K.mul (K.add K.one (K.neg Q)) (K.add K.one (K.neg Q))) := QuatUnit.sq_nn K O _
    -- LHS = y²·[(1−Q)² P (−L)] + (1−Q)²·P·2a·(y² v)
    have eL : K.mul (K.mul (K.add K.one (K.neg Q)) (K.add K.one (K.neg Q)))
        (K.mul (K.mul P (K.mul (K.add K.one (K.neg a)) (K.add K.one (K.neg a)))) (K.neg (K.add L (K.mul (K.add (K.neg a) (K.neg a)) v))))
        = K.add (K.mul (K.mul (K.add K.one (K.neg a)) (K.add K.one (K.neg a))) (K.mul (K.mul (K.add K.one (K.neg Q)) (K.add K.one (K.neg Q))) (K.mul P (K.neg L))))
          (K.mul (K.mul (K.mul (K.add K.one (K.neg Q)) (K.add K.one (K.neg Q))) (K.mul P (K.add a a))) (K.mul (K.mul (K.add K.one (K.neg a)) (K.add K.one (K.neg a))) v)) := by lib_ring
    rw [eL, i, show K.mul (K.mul (K.mul (K.add K.one (K.neg Q)) (K.add K.one (K.neg Q))) (K.mul P (K.add a a))) K.one
        = K.mul (K.mul P (K.add a a)) (K.mul (K.add K.one (K.neg Q)) (K.add K.one (K.neg Q))) by lib_ring]
    have s1 := O.mul_le_mul_right _ _ _ ih yy0
    have aa : O.le (K.mul P (K.add a a)) (K.mul P (K.add K.one K.one)) := by
      have := O.mul_le_mul_right _ _ _ (O.add_le_add _ _ _ _ a1 a1) P0; rw [K.mul_comm _ P, K.mul_comm _ P] at this; exact this
    have s2 := O.mul_le_mul_right _ _ _ aa q0
    have s3 := O.mul_le_mul_right _ _ _ dq (O.mul_nonneg _ _ P0 (Lambert.one_pos K O h1 K.one h1))
    have tot := O.add_le_add _ _ _ _ s1 (O.le_trans _ _ _ s2 (by rw [K.mul_comm (K.mul P (K.add K.one K.one))]; exact s3))
    refine O.le_trans _ _ _ (TripleE.le_of_eq' K O _ _ ?_) (O.le_trans _ _ _ tot (TripleE.le_of_eq' K O _ _ ?_))
    · lib_ring
    · rw [cN_add, SU2CharExp.cN_one]; lib_ring

open TripleMom TripleCoef in
/-- **θ₄ side, N-free bound**: (1−Q)²·P⁻·(−L⁻) ≤ 2·(Σ_{k<N} a_k)·P⁻ -/
theorem PL_le2 (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) (hQne : Q ≠ K.one) : ∀ N,
    O.le (K.mul (K.mul (K.add K.one (K.neg Q)) (K.add K.one (K.neg Q)))
        (K.mul (Pp K (K.neg K.one) (aQ K Q) N) (K.neg (Lp K O F (K.neg K.one) (aQ K Q) N))))
      (K.mul (K.add (QBinom.sumN K (aQ K Q) N) (QBinom.sumN K (aQ K Q) N)) (Pp K (K.neg K.one) (aQ K Q) N)) ∧ O.le K.zero (Pp K (K.neg K.one) (aQ K Q) N)
  | 0 => by
    constructor
    · show O.le (K.mul _ (K.mul K.one (K.neg K.zero))) (K.mul (K.add K.zero K.zero) K.one)
      rw [show K.mul (K.mul (K.add K.one (K.neg Q)) (K.add K.one (K.neg Q))) (K.mul K.one (K.neg K.zero)) = K.zero by lib_ring,
        show K.mul (K.add K.zero K.zero) K.one = K.zero by lib_ring]; exact O.le_refl _
    · exact h1
  | N + 1 => by
    obtain ⟨ih, P0⟩ := PL_le2 F h1 h10 Q hQ0 hQ1 hQne N
    have a0 : O.le K.zero (aQ K Q N) := AreaLower.pw_nn K O Q hQ0 h1 (2 * N + 1)
    have a1 : O.le (aQ K Q N) K.one := Tail.pw_le_one K O h1 Q hQ0 hQ1 (2 * N + 1)
    have aQ' : O.le (aQ K Q N) Q := by
      have := SmallQ.pw_anti K O h1 Q hQ0 hQ1 1 (2 * N); rw [show 1 + 2 * N = 2 * N + 1 by lib_nat] at this
      exact O.le_trans _ _ _ this (by show O.le (K.mul K.one Q) Q; rw [K.one_mul]; exact O.le_refl _)
    have y0 : O.le K.zero (K.add K.one (K.neg (aQ K Q N))) := QBinomBd.one_sub_nn K O _ a1
    have yne : K.add K.one (K.mul (K.neg K.one) (aQ K Q N)) ≠ K.zero := by
      intro e; apply QBinomBd.pw_ne_one K O h1 Q hQ0 hQ1 hQne (2 * N)
      show aQ K Q N = K.one
      have : aQ K Q N = K.add K.one (K.neg (K.add K.one (K.mul (K.neg K.one) (aQ K Q N)))) := by lib_ring
      rw [this, e]; lib_ring
    have i := F.mul_inv (TripleMom.sq K (K.add K.one (K.mul (K.neg K.one) (aQ K Q N)))) (by
      unfold TripleMom.sq; intro e
      cases O.le_total K.zero (K.add K.one (K.mul (K.neg K.one) (aQ K Q N))) with
      | inl h => exact yne (O.pos_mul_eq_zero _ _ h yne e)
      | inr h => exact yne (O.le_antisymm _ _ h (by rw [show K.mul (K.neg K.one) (aQ K Q N) = K.neg (aQ K Q N) by lib_ring]; exact y0)))
    have P1 : O.le K.zero (Pp K (K.neg K.one) (aQ K Q) (N + 1)) := O.mul_nonneg _ _ P0 (by unfold TripleMom.sq; exact QuatUnit.sq_nn K O _)
    refine ⟨?_, P1⟩
    -- recursion: P' = P y², S' = y² S + 2 a P (with S = P·(−L))
    show O.le (K.mul (K.mul (K.add K.one (K.neg Q)) (K.add K.one (K.neg Q)))
        (K.mul (K.mul (Pp K (K.neg K.one) (aQ K Q) N) (TripleMom.sq K (K.add K.one (K.mul (K.neg K.one) (aQ K Q N)))))
          (K.neg (K.add (Lp K O F (K.neg K.one) (aQ K Q) N) (K.mul (K.add (K.mul (K.neg K.one) (aQ K Q N)) (K.mul (K.neg K.one) (aQ K Q N)))
            (F.inv (TripleMom.sq K (K.add K.one (K.mul (K.neg K.one) (aQ K Q N))))))))))
      (K.mul (K.add (K.add (QBinom.sumN K (aQ K Q) N) (aQ K Q N)) (K.add (QBinom.sumN K (aQ K Q) N) (aQ K Q N))) (K.mul (Pp K (K.neg K.one) (aQ K Q) N) (TripleMom.sq K (K.add K.one (K.mul (K.neg K.one) (aQ K Q N))))))
    generalize F.inv (TripleMom.sq K (K.add K.one (K.mul (K.neg K.one) (aQ K Q N)))) = v at i ⊢
    unfold TripleMom.sq at i ⊢
    rw [show K.mul (K.neg K.one) (aQ K Q N) = K.neg (aQ K Q N) by lib_ring] at i ⊢
    generalize Pp K (K.neg K.one) (aQ K Q) N = P at ih P0 ⊢
    generalize Lp K O F (K.neg K.one) (aQ K Q) N = L at ih ⊢
    generalize aQ K Q N = a at a0 a1 aQ' y0 i ⊢
    -- (1−Q)² ≤ (1−a)²
    have dq : O.le (K.mul (K.add K.one (K.neg Q)) (K.add K.one (K.neg Q))) (K.mul (K.add K.one (K.neg a)) (K.add K.one (K.neg a))) := by
      have b : O.le (K.add K.one (K.neg Q)) (K.add K.one (K.neg a)) := by
        have := O.add_le_add _ _ _ _ aQ' (O.le_refl (K.add K.one (K.add (K.neg Q) (K.neg a))))
        rw [show K.add a (K.add K.one (K.add (K.neg Q) (K.neg a))) = K.add K.one (K.neg Q) by lib_ring,
          show K.add Q (K.add K.one (K.add (K.neg Q) (K.neg a))) = K.add K.one (K.neg a) by lib_ring] at this; exact this
      have b0 := QBinomBd.one_sub_nn K O _ hQ1
      exact SU2Strong4D.pw_mono K O h1 _ _ b0 b 2 |> fun h => by
        have e1 : pwR K (K.add K.one (K.neg Q)) 2 = K.mul (K.add K.one (K.neg Q)) (K.add K.one (K.neg Q)) := by
          show K.mul (K.mul K.one _) _ = _; rw [K.one_mul]
        have e2 : pwR K (K.add K.one (K.neg a)) 2 = K.mul (K.add K.one (K.neg a)) (K.add K.one (K.neg a)) := by
          show K.mul (K.mul K.one _) _ = _; rw [K.one_mul]
        rw [e1, e2] at h; exact h
    have yy0 : O.le K.zero (K.mul (K.add K.one (K.neg a)) (K.add K.one (K.neg a))) := O.mul_nonneg _ _ y0 y0
    have q0 : O.le K.zero (K.mul (K.add K.one (K.neg Q)) (K.add K.one (K.neg Q))) := QuatUnit.sq_nn K O _
    -- LHS = y²·[(1−Q)² P (−L)] + (1−Q)²·P·2a·(y² v)
    have eL : K.mul (K.mul (K.add K.one (K.neg Q)) (K.add K.one (K.neg Q)))
        (K.mul (K.mul P (K.mul (K.add K.one (K.neg a)) (K.add K.one (K.neg a)))) (K.neg (K.add L (K.mul (K.add (K.neg a) (K.neg a)) v))))
        = K.add (K.mul (K.mul (K.add K.one (K.neg a)) (K.add K.one (K.neg a))) (K.mul (K.mul (K.add K.one (K.neg Q)) (K.add K.one (K.neg Q))) (K.mul P (K.neg L))))
          (K.mul (K.mul (K.mul (K.add K.one (K.neg Q)) (K.add K.one (K.neg Q))) (K.mul P (K.add a a))) (K.mul (K.mul (K.add K.one (K.neg a)) (K.add K.one (K.neg a))) v)) := by lib_ring
    rw [eL, i, show K.mul (K.mul (K.mul (K.add K.one (K.neg Q)) (K.add K.one (K.neg Q))) (K.mul P (K.add a a))) K.one
        = K.mul (K.mul P (K.add a a)) (K.mul (K.add K.one (K.neg Q)) (K.add K.one (K.neg Q))) by lib_ring]
    have s1 := O.mul_le_mul_right _ _ _ ih yy0
    have s3 := O.mul_le_mul_right _ _ _ dq (O.mul_nonneg _ _ P0 (Lambert.one_pos K O h1 K.one h1 |> fun _ => by
      have := O.add_le_add _ _ _ _ a0 a0; rw [K.zero_add] at this; exact this))
    have s2 : O.le (K.mul (K.mul P (K.add a a)) (K.mul (K.add K.one (K.neg Q)) (K.add K.one (K.neg Q))))
        (K.mul (K.mul (K.add K.one (K.neg a)) (K.add K.one (K.neg a))) (K.mul P (K.add a a))) := by
      rw [K.mul_comm (K.mul P (K.add a a))]; exact s3
    have tot := O.add_le_add _ _ _ _ s1 s2
    refine O.le_trans _ _ _ (TripleE.le_of_eq' K O _ _ ?_) (O.le_trans _ _ _ tot (TripleE.le_of_eq' K O _ _ ?_))
    · lib_ring
    · lib_ring

/-- binomial coefficient (Pascal) -/
def C : Nat → Nat → Nat
  | _, 0 => 1
  | 0, _ + 1 => 0
  | m + 1, d + 1 => C m d + C m (d + 1)

theorem C_zero : ∀ m, C m 0 = 1
  | 0 => rfl
  | _ + 1 => rfl

/-- **binomial lower bound**: C(m,d)·y^d ≤ (1+y)^m for y ≥ 0 -/
theorem binom_lb (h1 : O.le K.zero K.one) (y : R) (hy : O.le K.zero y) : ∀ m d, O.le (K.mul (cN K (C m d)) (pwR K y d)) (pwR K (K.add K.one y) m)
  | m, 0 => by
    rw [C_zero]
    show O.le (K.mul (cN K 1) K.one) _
    rw [SU2CharExp.cN_one, K.one_mul]
    have := SU2Strong4D.pw_mono K O h1 K.one (K.add K.one y) h1
      (by have := O.add_le_add _ _ _ _ (O.le_refl K.one) hy; rw [DetChain.add_zero'] at this; exact this) m
    rw [SU2Strong4D.pw_one] at this; exact this
  | 0, d + 1 => by
    show O.le (K.mul K.zero _) K.one
    rw [K.mul_comm, DetChain.mul_zero]; exact h1
  | m + 1, d + 1 => by
    show O.le (K.mul (cN K (C m d + C m (d + 1))) (K.mul (pwR K y d) y)) (K.mul (pwR K (K.add K.one y) m) (K.add K.one y))
    have a := binom_lb h1 y hy m d
    have b := binom_lb h1 y hy m (d + 1)
    have sa := O.mul_le_mul_right _ _ _ a hy
    have tot := O.add_le_add _ _ _ _ sa b
    refine O.le_trans _ _ _ (TripleE.le_of_eq' K O _ _ ?_) (O.le_trans _ _ _ tot (TripleE.le_of_eq' K O _ _ ?_))
    · rw [cN_add]; show _ = K.add _ (K.mul (cN K (C m (d + 1))) (K.mul (pwR K y d) y)); lib_ring
    · lib_ring

theorem C_diag : ∀ d, C d d = 1
  | 0 => rfl
  | d + 1 => by
    show C d d + C d (d + 1) = 1
    rw [C_diag d, C_top d (d + 1) (Nat.lt_succ_self d)]
where
  C_top : ∀ m d, m < d → C m d = 0
    | 0, 0, h => absurd h (Nat.lt_irrefl 0)
    | 0, _ + 1, _ => rfl
    | _ + 1, 0, h => absurd h (Nat.not_lt_zero _)
    | m + 1, d + 1, h => by
      show C m d + C m (d + 1) = 0
      have h' := Nat.lt_of_succ_lt_succ h
      rw [C_top m d h', C_top m (d + 1) (Nat.lt_trans h' (Nat.lt_succ_self d))]

/-- **binomial term bound**: C(m+d, d)·b^d·a^m ≤ (a+b)^{m+d} for a, b ≥ 0 -/
theorem binom_term (h1 : O.le K.zero K.one) (a b : R) (ha : O.le K.zero a) (hb : O.le K.zero b) (m : Nat) :
    ∀ d, O.le (K.mul (K.mul (cN K (C (m + d) d)) (pwR K b d)) (pwR K a m)) (pwR K (K.add a b) (m + d)) := by
  have hab : O.le K.zero (K.add a b) := by have := O.add_le_add _ _ _ _ ha hb; rw [K.zero_add] at this; exact this
  have aab : O.le a (K.add a b) := by have := O.add_le_add _ _ _ _ (O.le_refl a) hb; rw [DetChain.add_zero'] at this; exact this
  have bab : O.le b (K.add a b) := by have := O.add_le_add _ _ _ _ ha (O.le_refl b); rw [K.zero_add] at this; exact this
  induction m with
  | zero =>
    intro d
    rw [Nat.zero_add, C_diag, SU2CharExp.cN_one, K.one_mul]
    show O.le (K.mul (pwR K b d) K.one) _
    rw [K.mul_comm, K.one_mul]; exact SU2Strong4D.pw_mono K O h1 b _ hb bab d
  | succ m ihm =>
    intro d
    induction d with
    | zero =>
      show O.le (K.mul (K.mul (cN K (C (m + 1 + 0) 0)) K.one) (pwR K a (m + 1))) _
      rw [C_zero, SU2CharExp.cN_one, K.one_mul, K.one_mul]; exact SU2Strong4D.pw_mono K O h1 a _ ha aab (m + 1)
    | succ d ihd =>
      -- C(m+1+d+1, d+1) = C(m+1+d, d) + C(m+1+d, d+1)
      have eC : C (m + 1 + (d + 1)) (d + 1) = C (m + 1 + d) d + C (m + (d + 1)) (d + 1) := by
        rw [show m + 1 + (d + 1) = (m + 1 + d) + 1 by lib_nat, show m + (d + 1) = m + 1 + d by lib_nat]; rfl
      have T1 := O.mul_le_mul_right _ _ _ ihd hb
      have T2 := O.mul_le_mul_right _ _ _ (ihm (d + 1)) ha
      have tot := O.add_le_add _ _ _ _ T1 T2
      refine O.le_trans _ _ _ (TripleE.le_of_eq' K O _ _ ?_) (O.le_trans _ _ _ tot (TripleE.le_of_eq' K O _ _ ?_))
      · rw [eC, cN_add]
        show K.mul (K.mul (K.add (cN K (C (m + 1 + d) d)) (cN K (C (m + (d + 1)) (d + 1)))) (K.mul (pwR K b d) b)) (K.mul (pwR K a m) a)
          = K.add (K.mul (K.mul (K.mul (cN K (C (m + 1 + d) d)) (pwR K b d)) (K.mul (pwR K a m) a)) b)
          (K.mul (K.mul (K.mul (cN K (C (m + (d + 1)) (d + 1))) (K.mul (pwR K b d) b)) (pwR K a m)) a)
        lib_ring
      · rw [show m + 1 + (d + 1) = (m + 1 + d) + 1 by lib_nat, show m + (d + 1) = m + 1 + d by lib_nat]
        show _ = K.mul (pwR K (K.add a b) (m + 1 + d)) (K.add a b)
        lib_ring

open TripleMom TripleCoef in
theorem sq_neg (a : R) : TripleMom.sq K (K.add K.one (K.mul (K.neg K.one) a)) = K.mul (K.add K.one (K.neg a)) (K.add K.one (K.neg a)) := by
  unfold TripleMom.sq; rw [show K.mul (K.neg K.one) a = K.neg a by lib_ring]

open TripleMom TripleCoef in
/-- P⁻ decreases in N -/
theorem Pp_mono (h1 : O.le K.zero K.one) (Q : R) (hQ0 : O.le K.zero Q) (hQ1 : O.le Q K.one) (N : Nat) :
    ∀ j, O.le (Pp K (K.neg K.one) (aQ K Q) (N + j)) (Pp K (K.neg K.one) (aQ K Q) N) ∧ O.le K.zero (Pp K (K.neg K.one) (aQ K Q) (N + j))
  | 0 => ⟨O.le_refl _, by
      have : ∀ M, O.le K.zero (Pp K (K.neg K.one) (aQ K Q) M) := fun M => by
        induction M with
        | zero => exact h1
        | succ M ih => exact O.mul_nonneg _ _ ih (by rw [sq_neg]; exact QuatUnit.sq_nn K O _)
      exact this N⟩
  | j + 1 => by
    obtain ⟨ih, ih0⟩ := Pp_mono h1 Q hQ0 hQ1 N j
    have a1 : O.le (aQ K Q (N + j)) K.one := Tail.pw_le_one K O h1 Q hQ0 hQ1 _
    have a0 : O.le K.zero (aQ K Q (N + j)) := AreaLower.pw_nn K O Q hQ0 h1 _
    have f0 := QBinomBd.one_sub_nn K O _ a1
    have f1 := QBinomBd.one_sub_le K O _ a0
    have ff : O.le (K.mul (K.add K.one (K.neg (aQ K Q (N + j)))) (K.add K.one (K.neg (aQ K Q (N + j))))) K.one := by
      have := O.mul_le_mul_right _ _ _ f1 f0; rw [K.one_mul] at this; exact O.le_trans _ _ _ this f1
    show O.le (K.mul (Pp K (K.neg K.one) (aQ K Q) (N + j)) (TripleMom.sq K (K.add K.one (K.mul (K.neg K.one) (aQ K Q (N + j)))))) _ ∧
      O.le K.zero (K.mul (Pp K (K.neg K.one) (aQ K Q) (N + j)) (TripleMom.sq K (K.add K.one (K.mul (K.neg K.one) (aQ K Q (N + j))))))
    rw [sq_neg]
    refine ⟨?_, O.mul_nonneg _ _ ih0 (O.mul_nonneg _ _ f0 f0)⟩
    have := O.mul_le_mul_right _ _ _ ff ih0
    rw [K.one_mul, K.mul_comm] at this; exact O.le_trans _ _ _ this ih

open TripleMom TripleCoef in
/-- **P⁻ is small**: if x ≤ a_k for k < J then P⁻_J ≤ (1−x)^{2J} -/
theorem Pp_le (h1 : O.le K.zero K.one) (Q x : R) (hQ0 : O.le K.zero Q) (hQ1 : O.le Q K.one) (hx0 : O.le K.zero x) :
    ∀ J, (∀ k, k < J → O.le x (aQ K Q k)) → O.le (Pp K (K.neg K.one) (aQ K Q) J) (pwR K (K.add K.one (K.neg x)) (J + J))
  | 0, _ => O.le_refl _
  | J + 1, h => by
    have ih := Pp_le h1 Q x hQ0 hQ1 hx0 J (fun k hk => h k (Nat.lt_trans hk (Nat.lt_succ_self J)))
    have hJ := h J (Nat.lt_succ_self J)
    have a1 : O.le (aQ K Q J) K.one := Tail.pw_le_one K O h1 Q hQ0 hQ1 _
    have f0 := QBinomBd.one_sub_nn K O _ a1
    have fl : O.le (K.add K.one (K.neg (aQ K Q J))) (K.add K.one (K.neg x)) := by
      have := O.add_le_add _ _ _ _ hJ (O.le_refl (K.add K.one (K.add (K.neg x) (K.neg (aQ K Q J)))))
      rw [show K.add x (K.add K.one (K.add (K.neg x) (K.neg (aQ K Q J)))) = K.add K.one (K.neg (aQ K Q J)) by lib_ring,
        show K.add (aQ K Q J) (K.add K.one (K.add (K.neg x) (K.neg (aQ K Q J)))) = K.add K.one (K.neg x) by lib_ring] at this
      exact this
    have sq := SU2Strong4D.pw_mono K O h1 _ _ f0 fl 2
    have P0 := (Pp_mono K O h1 Q hQ0 hQ1 J 0).2
    show O.le (K.mul (Pp K (K.neg K.one) (aQ K Q) J) (TripleMom.sq K (K.add K.one (K.mul (K.neg K.one) (aQ K Q J))))) _
    rw [sq_neg, show J + 1 + (J + 1) = (J + J) + 2 by lib_nat, GKS.pwR_add]
    have e1 : pwR K (K.add K.one (K.neg (aQ K Q J))) 2 = K.mul (K.add K.one (K.neg (aQ K Q J))) (K.add K.one (K.neg (aQ K Q J))) := by
      show K.mul (K.mul K.one _) _ = _; rw [K.one_mul]
    rw [e1] at sq
    have s1 := O.mul_le_mul_right _ _ _ ih (O.mul_nonneg _ _ f0 f0)
    have s2 := O.mul_le_mul_right _ _ _ sq (AreaLower.pw_nn K O _ (O.le_trans _ _ _ f0 fl) h1 (J + J))
    rw [K.mul_comm (pwR K (K.add K.one (K.neg x)) 2)] at s2
    exact O.le_trans _ _ _ s1 (by rw [K.mul_comm (K.mul _ _) (pwR K _ (J + J))] at s2; exact s2)

open TripleMom TripleCoef in
/-- **θ₄ condition of big_q from a polynomial-free hypothesis** -/
theorem theta4 (F : OField.OF K O) (h1 : O.le K.zero K.one) (h10 : K.one ≠ K.zero) (Q x mg : R) (hQ0 : O.le K.zero Q)
    (hQ1 : O.le Q K.one) (hQne : Q ≠ K.one) (hx0 : O.le K.zero x) (J j : Nat) (hx : ∀ k, k < J → O.le x (aQ K Q k))
    (hP : O.le (K.mul (K.add (K.add K.one K.one) K.one) (K.mul (pwR K (K.add K.one (K.neg x)) (J + J))
        (K.add (K.mul (K.add K.one (K.neg Q)) (K.add K.one (K.neg Q))) (K.mul (K.mul (K.add K.one K.one) (K.add K.one K.one)) (cN K (J + j))))))
      (K.mul mg (K.mul (K.add K.one (K.neg Q)) (K.add K.one (K.neg Q))))) :
    O.le (K.mul (K.add (K.add K.one K.one) K.one) (K.mul (Pp K (K.neg K.one) (aQ K Q) (J + j))
        (K.add K.one (K.neg (K.mul (K.add K.one (K.mul Q Q)) (Lp K O F (K.neg K.one) (aQ K Q) (J + j)))))))
      mg := by
  obtain ⟨pl, P0⟩ := PL_le K O F h1 h10 Q hQ0 hQ1 hQne (J + j)
  have pm := (Pp_mono K O h1 Q hQ0 hQ1 J j).1
  have pj := Pp_le K O h1 Q x hQ0 hQ1 hx0 J hx
  have q2 : 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
  have dpos : O.le K.zero (K.mul (K.add K.one (K.neg Q)) (K.add K.one (K.neg Q))) := QuatUnit.sq_nn K O _
  have dne : K.mul (K.add K.one (K.neg Q)) (K.add K.one (K.neg Q)) ≠ K.zero := by
    intro e
    have y0 := QBinomBd.one_sub_nn K O _ hQ1
    have yne : K.add K.one (K.neg Q) ≠ K.zero := fun h => hQne (by
      have : Q = K.add K.one (K.neg (K.add K.one (K.neg Q))) := by lib_ring
      rw [this, h]; lib_ring)
    exact yne (O.pos_mul_eq_zero _ _ y0 yne e)
  apply GridQ.cancel K O F (K.mul (K.add K.one (K.neg Q)) (K.add K.one (K.neg Q))) _ _ dpos dne
  generalize Pp K (K.neg K.one) (aQ K Q) (J + j) = P at pl P0 pm ⊢
  generalize Lp K O F (K.neg K.one) (aQ K Q) (J + j) = L at pl ⊢
  generalize K.mul (K.add K.one (K.neg Q)) (K.add K.one (K.neg Q)) = D at pl dpos hP ⊢
  generalize pwR K (K.add K.one (K.neg x)) (J + J) = B at pj hP
  generalize Pp K (K.neg K.one) (aQ K Q) J = PJ at pm pj
  -- D·3·P·(1 + (1+q)(−L)) = 3[D P + (1+q)·D P(−L)] ≤ 3[D P + 2·(2(J+j)) P] ≤ 3 B (D + 4(J+j))  ≤ mg D
  have PB := O.le_trans _ _ _ pm pj
  have one_q : O.le (K.add K.one (K.mul Q Q)) (K.add K.one K.one) := O.add_le_add _ _ _ _ (O.le_refl _) q2
  have oq0 : O.le K.zero (K.add K.one (K.mul Q Q)) := Lambert.one_pos K O h1 _ (O.mul_nonneg _ _ hQ0 hQ0)
  have t0 : O.le K.zero (K.add K.one K.one) := Lambert.one_pos K O h1 _ h1
  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 cn := SU2CharExp.cN_nn K O h1 (J + j)
  have NN : O.le K.zero (K.add (cN K (J + j)) (cN K (J + j))) := by have := O.add_le_add _ _ _ _ cn cn; rw [K.zero_add] at this; exact this
  have st1 := O.mul_le_mul_right _ _ _ pl oq0
  have st2 := O.mul_le_mul_right _ _ _ one_q (O.mul_nonneg _ _ NN P0)
  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 DN : O.le K.zero (K.add D (K.mul (K.mul (K.add K.one K.one) (K.add K.one K.one)) (cN K (J + j)))) := by
    have := O.add_le_add _ _ _ _ dpos (O.mul_nonneg _ _ four0 cn); rw [K.zero_add] at this; exact this
  have e1 := O.mul_nonneg _ _ three0 (AltSum.le_sub K O _ _ st1)
  have e2 := O.mul_nonneg _ _ three0 (AltSum.le_sub K O _ _ st2)
  have e3 := O.mul_nonneg _ _ (O.mul_nonneg _ _ three0 DN) (AltSum.le_sub K O _ _ PB)
  have e4 := AltSum.le_sub K O _ _ hP
  have tot := O.add_le_add _ _ _ _ (O.add_le_add _ _ _ _ (O.add_le_add _ _ _ _ e1 e2) e3) e4
  rw [K.zero_add, K.zero_add, K.zero_add] at tot
  have := O.add_le_add _ _ _ _ tot (O.le_refl (K.mul D (K.mul (K.add (K.add K.one K.one) K.one)
    (K.mul P (K.add K.one (K.neg (K.mul (K.add K.one (K.mul Q Q)) L)))))))
  rw [K.zero_add] at this
  refine O.le_trans _ _ _ this (TripleE.le_of_eq' K O _ _ ?_)
  generalize cN K (J + j) = n
  lib_ring

/-- polynomial bound for the weight sum: Σ_{m<n} (2m+3)·tN(m) ≤ n·(2n+1)·4n(n+1) -/
theorem sumB_le (h1 : O.le K.zero K.one) (Q : R) (hQ0 : O.le K.zero Q) (hQ1 : O.le Q K.one) (n : Nat) :
    O.le (QBinom.sumN K (fun m => K.mul (cN K (2 * m + 3)) (SmallQ.tN K Q m)) n) (cN K (n * ((2 * n + 1) * (4 * (n * (n + 1)))))) := by
  have each : ∀ m, m < n → O.le (K.mul (cN K (2 * m + 3)) (SmallQ.tN K Q m)) (cN K ((2 * n + 1) * (4 * (n * (n + 1))))) := by
    intro m hm
    obtain ⟨d, hd⟩ := Nat.le.dest hm
    have c1 : O.le (cN K (2 * m + 3)) (cN K (2 * n + 1)) := SU2CharExp.cN_mono K O h1 _ _ (Nat.le.intro (k := 2 * d) (by rw [← hd]; lib_nat))
    have c2 : O.le (cN K (4 * ((m + 1) * (m + 2)))) (cN K (4 * (n * (n + 1)))) :=
      SU2CharExp.cN_mono K O h1 _ _ (Nat.le.intro (k := 4 * (d * d + 2 * d * m + 3 * d)) (by rw [← hd]; lib_nat))
    have qp := Tail.pw_le_one K O h1 Q hQ0 hQ1 (4 * ((m + 1) * (m + 1)) + 4 * (m + 1) + 1)
    have t1 : O.le (SmallQ.tN K Q m) (cN K (4 * (n * (n + 1)))) := by
      unfold SmallQ.tN
      have := O.mul_le_mul_right _ _ _ qp (SU2CharExp.cN_nn K O h1 (4 * ((m + 1) * (m + 2))))
      rw [K.one_mul, K.mul_comm] at this; exact O.le_trans _ _ _ this c2
    have s1 := O.mul_le_mul_right _ _ _ c1 (SmallQ.tN_nn K O h1 Q hQ0 m)
    have s2 := O.mul_le_mul_right _ _ _ t1 (SU2CharExp.cN_nn K O h1 (2 * n + 1))
    rw [SU2CharExp.cN_mul]
    exact O.le_trans _ _ _ s1 (by rw [K.mul_comm (SmallQ.tN K Q m) (cN K (2 * n + 1)), K.mul_comm (cN K (4 * (n * (n + 1)))) (cN K (2 * n + 1))] at s2; exact s2)
  have := TripleE.sumN_mono_lt K O _ (fun _ => cN K ((2 * n + 1) * (4 * (n * (n + 1))))) n each
  refine O.le_trans _ _ _ this (TripleE.le_of_eq' K O _ _ ?_)
  have : ∀ k, QBinom.sumN K (fun _ => cN K ((2 * n + 1) * (4 * (n * (n + 1))))) k = cN K (k * ((2 * n + 1) * (4 * (n * (n + 1))))) := by
    intro k; induction k with
    | zero => show K.zero = cN K (0 * _); rw [Nat.zero_mul]; rfl
    | succ k ih => show K.add (QBinom.sumN K _ k) _ = _; rw [ih, ← cN_add]; congr 1; lib_nat
  exact this n

open TripleMom TripleCoef in
/-- **big_q with power–polynomial hypotheses only** -/
theorem big_q2 (F : OField.OF K O) (h1 : O.le K.zero K.one) (h10 : K.one ≠ K.zero) (Q x : 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) (hx0 : O.le K.zero x)
    (n r J j : Nat) (hN : 2 * (n + r) + 1 = J + j) (hx : ∀ k, k < J → O.le x (aQ K Q k))
    (hc : TripleH.CondP K O Q n)
    (hP : O.le (K.mul (K.add (K.add K.one K.one) K.one) (K.mul (pwR K (K.add K.one (K.neg x)) (J + J))
        (K.add (K.mul (K.add K.one (K.neg Q)) (K.add K.one (K.neg Q))) (K.mul (K.mul (K.add K.one K.one) (K.add K.one K.one)) (cN K (J + j))))))
      (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.add K.one (K.neg Q)) (K.add K.one (K.neg Q)))))
    (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)) (cN K (n * ((2 * n + 1) * (4 * (n * (n + 1))))))))
      (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 (SmallQ.tP K Q n) (SmallQ.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 (SmallQ.tN K Q) 0 n) (SmallQ.sumF K (SmallQ.tP K Q) 0 n) := by
  have hA := theta4 K O F h1 h10 Q x _ hQ0 hQ1 hQne hx0 J j hx hP
  rw [← hN] at hA
  apply TripleH.big_q K O F h1 h10 Q hQ0 hQ1 hQ hQne hqne n r (TripleH.margin_nn K O F h1 h10 Q hQ0 hQ1) hc hA _ hC
  have sb := sumB_le K O h1 Q hQ0 hQ1 n
  have q0 : O.le K.zero (pwR K (K.mul Q Q) (2 * r)) := AreaLower.pw_nn K O _ (O.mul_nonneg _ _ hQ0 hQ0) h1 _
  have t0 : O.le K.zero (K.add K.one K.one) := Lambert.one_pos K O h1 _ h1
  have c0 : O.le K.zero (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))) :=
    O.mul_nonneg _ _ (by have := O.add_le_add _ _ _ _ t0 h1; rw [K.zero_add] at this; exact this) (O.mul_nonneg _ _ t0 t0)
  have s1 := O.mul_le_mul_right _ _ _ sb q0
  rw [K.mul_comm _ (pwR K (K.mul Q Q) (2 * r)), K.mul_comm (cN K _) (pwR K (K.mul Q Q) (2 * r))] at s1
  have s2 := O.mul_le_mul_right _ _ _ s1 c0
  rw [K.mul_comm _ (K.mul (K.add (K.add K.one K.one) K.one) _), K.mul_comm (K.mul (pwR K (K.mul Q Q) (2 * r)) _) (K.mul (K.add (K.add K.one K.one) K.one) _)] at s2
  exact O.le_trans _ _ _ s2 hB

/-- Bernoulli lower bound: Q^m ≥ 1 − m(1−Q) for 0 ≤ Q ≤ 1 -/
theorem bern_lo (h1 : O.le K.zero K.one) (Q : R) (hQ0 : O.le K.zero Q) (hQ1 : O.le Q K.one) :
    ∀ m, O.le (K.add K.one (K.neg (K.mul (cN K m) (K.add K.one (K.neg Q))))) (pwR K Q m)
  | 0 => by show O.le (K.add K.one (K.neg (K.mul K.zero _))) K.one; rw [show K.add K.one (K.neg (K.mul K.zero (K.add K.one (K.neg Q)))) = K.one by lib_ring]; exact O.le_refl _
  | m + 1 => by
    have ih := bern_lo h1 Q hQ0 hQ1 m
    have d0 := QBinomBd.one_sub_nn K O _ hQ1
    have s1 := O.mul_le_mul_right _ _ _ ih hQ0
    -- (1 − mδ)Q = Q − mδQ ≥ Q − mδ
    have s2 : O.le (K.mul (K.mul (cN K m) (K.add K.one (K.neg Q))) Q) (K.mul (cN K m) (K.add K.one (K.neg Q))) := by
      have := O.mul_le_mul_right _ _ _ hQ1 (O.mul_nonneg _ _ (SU2CharExp.cN_nn K O h1 m) d0)
      rw [K.one_mul, K.mul_comm Q] at this; exact this
    have s3 := O.add_le_add _ _ _ _ (O.le_refl Q) (AltSum.sub_le K O K.zero _ (O.le_refl K.zero) |> fun _ => (show O.le (K.neg (K.mul (cN K m) (K.add K.one (K.neg Q)))) (K.neg (K.mul (K.mul (cN K m) (K.add K.one (K.neg Q))) Q)) from by
      have := O.add_le_add _ _ _ _ s2 (O.le_refl (K.add (K.neg (K.mul (cN K m) (K.add K.one (K.neg Q)))) (K.neg (K.mul (K.mul (cN K m) (K.add K.one (K.neg Q))) Q))))
      rw [show K.add (K.mul (K.mul (cN K m) (K.add K.one (K.neg Q))) Q) (K.add (K.neg (K.mul (cN K m) (K.add K.one (K.neg Q)))) (K.neg (K.mul (K.mul (cN K m) (K.add K.one (K.neg Q))) Q)))
          = K.neg (K.mul (cN K m) (K.add K.one (K.neg Q))) by lib_ring,
        show K.add (K.mul (cN K m) (K.add K.one (K.neg Q))) (K.add (K.neg (K.mul (cN K m) (K.add K.one (K.neg Q)))) (K.neg (K.mul (K.mul (cN K m) (K.add K.one (K.neg Q))) Q)))
          = K.neg (K.mul (K.mul (cN K m) (K.add K.one (K.neg Q))) Q) by lib_ring] at this
      exact this))
    refine O.le_trans _ _ _ (TripleE.le_of_eq' K O _ _ ?_) (O.le_trans _ _ _ s3 (O.le_trans _ _ _ (TripleE.le_of_eq' K O _ _ ?_) s1))
    · rw [cN_add, SU2CharExp.cN_one]; lib_ring
    · lib_ring

open TripleCoef in
/-- (1 − Q²)·Σ_{k<N} Q^{2k+1} = Q − Q^{2N+1} -/
theorem asum (Q : R) : ∀ N, K.mul (K.add K.one (K.neg (K.mul Q Q))) (QBinom.sumN K (aQ K Q) N)
    = K.add Q (K.neg (pwR K Q (2 * N + 1)))
  | 0 => by show K.mul _ K.zero = K.add Q (K.neg (K.mul K.one Q)); lib_ring
  | N + 1 => by
    show K.mul (K.add K.one (K.neg (K.mul Q Q))) (K.add (QBinom.sumN K (aQ K Q) N) (pwR K Q (2 * N + 1))) = K.add Q (K.neg (pwR K Q (2 * (N + 1) + 1)))
    rw [show 2 * (N + 1) + 1 = (2 * N + 1) + 2 by lib_nat, GKS.pwR_add K Q (2 * N + 1) 2,
      show K.mul (K.add K.one (K.neg (K.mul Q Q))) (K.add (QBinom.sumN K (aQ K Q) N) (pwR K Q (2 * N + 1)))
        = K.add (K.mul (K.add K.one (K.neg (K.mul Q Q))) (QBinom.sumN K (aQ K Q) N)) (K.mul (K.add K.one (K.neg (K.mul Q Q))) (pwR K Q (2 * N + 1))) by lib_ring,
      asum Q N, show pwR K Q 2 = K.mul Q Q by show K.mul (K.mul K.one Q) Q = _; rw [K.one_mul]]
    lib_ring

open TripleMom TripleCoef in
/-- **θ₄ condition, N-free**: 3(1−x)^{2J}((1−Q)²(1−Q²) + 4) ≤ m′(1−Q)²(1−Q²) ⇒ 3·P⁻_N X ≤ m′ (any N ≥ J) -/
theorem theta4b (F : OField.OF K O) (h1 : O.le K.zero K.one) (h10 : K.one ≠ K.zero) (Q x mg : R) (hQ0 : O.le K.zero Q)
    (hQ1 : O.le Q K.one) (hQne : Q ≠ K.one) (hqne : K.mul Q Q ≠ K.one) (hx0 : O.le K.zero x) (J j : Nat)
    (hx : ∀ k, k < J → O.le x (aQ K Q k))
    (hP : O.le (K.mul (K.add (K.add K.one K.one) K.one) (K.mul (pwR K (K.add K.one (K.neg x)) (J + J))
        (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 mg (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)))))) :
    O.le (K.mul (K.add (K.add K.one K.one) K.one) (K.mul (Pp K (K.neg K.one) (aQ K Q) (J + j))
        (K.add K.one (K.neg (K.mul (K.add K.one (K.mul Q Q)) (Lp K O F (K.neg K.one) (aQ K Q) (J + j)))))))
      mg := by
  obtain ⟨pl, P0⟩ := PL_le2 K O F h1 h10 Q hQ0 hQ1 hQne (J + j)
  have as := asum K Q (J + j)
  have pm := (Pp_mono K O h1 Q hQ0 hQ1 J j).1
  have pj := Pp_le K O h1 Q x hQ0 hQ1 hx0 J hx
  have q2 : 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
  have E0 : O.le K.zero (K.add K.one (K.neg (K.mul Q Q))) := QBinomBd.one_sub_nn K O _ q2
  have D0 : O.le K.zero (K.mul (K.add K.one (K.neg Q)) (K.add K.one (K.neg Q))) := QuatUnit.sq_nn K O _
  have nz : ∀ y : R, O.le K.zero y → y ≠ K.zero → K.mul y y ≠ K.zero := fun y hy hne e => hne (O.pos_mul_eq_zero _ _ hy hne e)
  have yne : K.add K.one (K.neg Q) ≠ K.zero := fun h => hQne (by
    have : Q = K.add K.one (K.neg (K.add K.one (K.neg Q))) := by lib_ring
    rw [this, h]; lib_ring)
  have Ene : K.add K.one (K.neg (K.mul Q Q)) ≠ K.zero := fun h => hqne (by
    have : K.mul Q Q = K.add K.one (K.neg (K.add K.one (K.neg (K.mul Q Q)))) := by lib_ring
    rw [this, h]; lib_ring)
  have Dne := nz _ (QBinomBd.one_sub_nn K O _ hQ1) yne
  have DEne : 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.zero := by
    intro e; exact Ene (O.pos_mul_eq_zero _ _ D0 Dne e)
  apply GridQ.cancel K O F _ _ _ (O.mul_nonneg _ _ D0 E0) DEne
  have Aq : O.le (K.mul (K.add K.one (K.neg (K.mul Q Q))) (QBinom.sumN K (aQ K Q) (J + j))) K.one := by
    rw [as]; exact O.le_trans _ _ _ (AltSum.sub_le K O Q _ (AreaLower.pw_nn K O Q hQ0 h1 _)) hQ1
  generalize Pp K (K.neg K.one) (aQ K Q) (J + j) = P at pl P0 pm ⊢
  generalize Lp K O F (K.neg K.one) (aQ K Q) (J + j) = L at pl ⊢
  generalize QBinom.sumN K (aQ K Q) (J + j) = A at pl Aq
  generalize K.mul (K.add K.one (K.neg Q)) (K.add K.one (K.neg Q)) = D at pl D0 hP ⊢
  generalize K.add K.one (K.neg (K.mul Q Q)) = E at Aq E0 hP ⊢
  generalize pwR K (K.add K.one (K.neg x)) (J + J) = B at pj hP
  generalize Pp K (K.neg K.one) (aQ K Q) J = PJ at pm pj
  have PB := O.le_trans _ _ _ pm pj
  have t0 : O.le K.zero (K.add K.one K.one) := Lambert.one_pos K O h1 _ h1
  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 oq0 : O.le K.zero (K.add K.one (K.mul Q Q)) := Lambert.one_pos K O h1 _ (O.mul_nonneg _ _ hQ0 hQ0)
  have one_q : O.le (K.add K.one (K.mul Q Q)) (K.add K.one K.one) := O.add_le_add _ _ _ _ (O.le_refl _) q2
  -- E·D·P(−L) ≤ E·2A·P = 2P·(EA) ≤ 2P
  have u1 := O.mul_le_mul_right _ _ _ pl E0
  have u2 := O.mul_le_mul_right _ _ _ Aq (O.mul_nonneg _ _ t0 P0)
  -- (1+q)·(stuff) ≤ 2·stuff
  have W0 : O.le K.zero (K.mul (K.mul (K.add K.one K.one) P) K.one) := O.mul_nonneg _ _ (O.mul_nonneg _ _ t0 P0) h1
  have u3 := O.mul_le_mul_right _ _ _ one_q W0
  have DE0 : O.le K.zero (K.add (K.mul D E) (K.mul (K.add K.one K.one) (K.add K.one K.one))) := by
    have := O.add_le_add _ _ _ _ (O.mul_nonneg _ _ D0 E0) (O.mul_nonneg _ _ t0 t0); rw [K.zero_add] at this; exact this
  have e1 := O.mul_nonneg _ _ (O.mul_nonneg _ _ three0 oq0) (AltSum.le_sub K O _ _ u1)
  have e2 := O.mul_nonneg _ _ (O.mul_nonneg _ _ three0 oq0) (AltSum.le_sub K O _ _ u2)
  have e3 := O.mul_nonneg _ _ three0 (AltSum.le_sub K O _ _ u3)
  have e4 := O.mul_nonneg _ _ (O.mul_nonneg _ _ three0 DE0) (AltSum.le_sub K O _ _ PB)
  have e5 := AltSum.le_sub K O _ _ hP
  have tot := O.add_le_add _ _ _ _ (O.add_le_add _ _ _ _ (O.add_le_add _ _ _ _ (O.add_le_add _ _ _ _ e1 e2) e3) e4) e5
  rw [K.zero_add, K.zero_add, K.zero_add, K.zero_add] at tot
  have := O.add_le_add _ _ _ _ tot (O.le_refl (K.mul (K.mul D E) (K.mul (K.add (K.add K.one K.one) K.one)
    (K.mul P (K.add K.one (K.neg (K.mul (K.add K.one (K.mul Q Q)) L)))))))
  rw [K.zero_add] at this
  refine O.le_trans _ _ _ this (TripleE.le_of_eq' K O _ _ ?_)
  lib_ring

open TripleMom TripleCoef in
/-- **big_q with N-free power–polynomial hypotheses** -/
theorem big_q3 (F : OField.OF K O) (h1 : O.le K.zero K.one) (h10 : K.one ≠ K.zero) (Q x : 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) (hx0 : O.le K.zero x)
    (n r J j : Nat) (hN : 2 * (n + r) + 1 = J + j) (hx : ∀ k, k < J → O.le x (aQ K Q k))
    (hc : TripleH.CondP K O Q n)
    (hP : O.le (K.mul (K.add (K.add K.one K.one) K.one) (K.mul (pwR K (K.add K.one (K.neg x)) (J + J))
        (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))))))
    (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)) (cN K (n * ((2 * n + 1) * (4 * (n * (n + 1))))))))
      (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 (SmallQ.tP K Q n) (SmallQ.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 (SmallQ.tN K Q) 0 n) (SmallQ.sumF K (SmallQ.tP K Q) 0 n) := by
  have hA := theta4b K O F h1 h10 Q x _ hQ0 hQ1 hQne hqne hx0 J j hx hP
  rw [← hN] at hA
  apply TripleH.big_q K O F h1 h10 Q hQ0 hQ1 hQ hQne hqne n r (TripleH.margin_nn K O F h1 h10 Q hQ0 hQ1) hc hA _ hC
  have sb := sumB_le K O h1 Q hQ0 hQ1 n
  have q0 : O.le K.zero (pwR K (K.mul Q Q) (2 * r)) := AreaLower.pw_nn K O _ (O.mul_nonneg _ _ hQ0 hQ0) h1 _
  have t0 : O.le K.zero (K.add K.one K.one) := Lambert.one_pos K O h1 _ h1
  have c0 : O.le K.zero (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))) :=
    O.mul_nonneg _ _ (by have := O.add_le_add _ _ _ _ t0 h1; rw [K.zero_add] at this; exact this) (O.mul_nonneg _ _ t0 t0)
  have s1 := O.mul_le_mul_right _ _ _ sb q0
  rw [K.mul_comm _ (pwR K (K.mul Q Q) (2 * r)), K.mul_comm (cN K _) (pwR K (K.mul Q Q) (2 * r))] at s1
  have s2 := O.mul_le_mul_right _ _ _ s1 c0
  rw [K.mul_comm _ (K.mul (K.add (K.add K.one K.one) K.one) _), K.mul_comm (K.mul (pwR K (K.mul Q Q) (2 * r)) _) (K.mul (K.add (K.add K.one K.one) K.one) _)] at s2
  exact O.le_trans _ _ _ s2 hB

end Bern

#print axioms Bern.bern
#print axioms Bern.PL_le
#print axioms Bern.binom_lb
#print axioms Bern.binom_term
#print axioms Bern.Pp_le
#print axioms Bern.theta4
#print axioms Bern.sumB_le
#print axioms Bern.big_q2
#print axioms Bern.bern_lo
#print axioms Bern.PL_le2
#print axioms Bern.asum
#print axioms Bern.theta4b
#print axioms Bern.big_q3

ym/BigK.lean

lean_all/ym/BigK.lean

/-
  ym/BigK — T45, G44g (integer-parameter realisation, Nat part): exponentials beat the polynomials needed for the
  large-Q cells 1−Q ∈ [1/(4K+1), 1/(4K)]:  30(4K+1)⁴ ≤ 4^K (K ≥ 15),  384 m³ ≤ 16^m (m ≥ 4). Lean 4 core only, no axioms.
  (Core Nat.mul_assoc / Nat.mul_left_comm use propext, so only variable-level lib_nat and closed rfl are used.)
-/
import ym.Bern

namespace BigK

theorem p4_mono {x y : Nat} (h : x ≤ y) : x * x * x * x ≤ y * y * y * y :=
  Nat.mul_le_mul (Nat.mul_le_mul (Nat.mul_le_mul h h) h) h

theorem p3_mono {x y : Nat} (h : x ≤ y) : x * x * x ≤ y * y * y :=
  Nat.mul_le_mul (Nat.mul_le_mul h h) h

theorem sc4 (c x : Nat) : c * x * (c * x) * (c * x) * (c * x) = c * c * c * c * (x * x * x * x) := by lib_nat
theorem sc3 (c x : Nat) : c * x * (c * x) * (c * x) = c * c * c * (x * x * x) := by lib_nat
theorem massoc (a b c : Nat) : a * (b * c) = a * b * c := by lib_nat
theorem mcomm3 (a b c : Nat) : a * (b * c) = b * (a * c) := by lib_nat

theorem stepA (X Y P : Nat) (r4 : 625 * X ≤ 2401 * Y) (ih : 30 * Y ≤ P) : 30 * X ≤ 4 * P := by
  have s : 625 * X ≤ 625 * (4 * Y) := by
    rw [massoc, show 625 * 4 = 2500 from rfl]
    exact Nat.le_trans r4 (Nat.mul_le_mul_right Y (by decide))
  have x4 : X ≤ 4 * Y := Nat.le_of_mul_le_mul_left s (by decide)
  calc 30 * X ≤ 30 * (4 * Y) := Nat.mul_le_mul_left 30 x4
    _ = 4 * (30 * Y) := mcomm3 _ _ _
    _ ≤ 4 * P := Nat.mul_le_mul_left 4 ih

theorem ratio57 (c : Nat) : 5 * (4 * (c + 3 + 1) + 1) ≤ 7 * (4 * (c + 3) + 1) := Nat.le.intro (k := 8 * c + 6) (by lib_nat)

theorem n1 (b : Nat) : 30 * ((4 * (b + 15) + 1) * (4 * (b + 15) + 1) * (4 * (b + 15) + 1) * (4 * (b + 15) + 1)) ≤ 4 ^ (b + 15) := by
  induction b with
  | zero => exact Nat.le_of_ble_eq_true rfl
  | succ b ih =>
    have r : 5 * (4 * (b + 1 + 15) + 1) ≤ 7 * (4 * (b + 15) + 1) := by
      have := ratio57 (b + 12)
      rw [show b + 12 + 3 + 1 = b + 1 + 15 from rfl, show b + 12 + 3 = b + 15 from rfl] at this; exact this
    have r4 := p4_mono r
    rw [sc4, sc4, show 5 * 5 * 5 * 5 = 625 from rfl, show 7 * 7 * 7 * 7 = 2401 from rfl] at r4
    have := stepA _ _ _ r4 ih
    rw [show b + 1 + 15 = (b + 15) + 1 by lib_nat]
    exact Nat.le_trans this (Nat.le_of_eq (Nat.mul_comm _ _))

theorem stepB (X Y P : Nat) (r3 : X ≤ 2 * 2 * 2 * Y) (ih : 384 * Y ≤ P) : 384 * X ≤ 16 * P := by
  have st : X ≤ 16 * Y := Nat.le_trans r3 (Nat.mul_le_mul_right Y (by decide))
  calc 384 * X ≤ 384 * (16 * Y) := Nat.mul_le_mul_left 384 st
    _ = 16 * (384 * Y) := mcomm3 _ _ _
    _ ≤ 16 * P := Nat.mul_le_mul_left 16 ih

theorem n4 : ∀ a, 384 * ((a + 4) * (a + 4) * (a + 4)) ≤ 16 ^ (a + 4)
  | 0 => by decide
  | a + 1 => by
    have ih := n4 a
    have r : a + 1 + 4 ≤ 2 * (a + 4) := Nat.le.intro (k := a + 3) (by lib_nat)
    have r3 := p3_mono r
    rw [sc3] at r3
    have := stepB _ _ _ r3 ih
    rw [show a + 1 + 4 = (a + 4) + 1 by lib_nat]
    exact Nat.le_trans this (Nat.le_of_eq (Nat.mul_comm _ _))

end BigK

#print axioms BigK.n1
#print axioms BigK.n4

ym/BigQK.lean

lean_all/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

ym/Block2D.lean

lean_all/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/BondLC3.lean

lean_all/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

ym/BondLC4.lean

lean_all/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

ym/BondLC5.lean

lean_all/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

ym/BondLC6.lean

lean_all/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

ym/BondLC7.lean

lean_all/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

ym/BondLCBig.lean

lean_all/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

ym/BondLCL11.lean

lean_all/ym/BondLCL11.lean

/-
  ym/BondLCL11 — T45, G54: (A) for v-good lists of length 11 at every index n:
    (n+2)² c_n c_{n+2} ≤ (n+1)(n+3) c_{n+1}²  for c = X·X.
  n ≤ 6: BondLC6 (every length); 7 ≤ n ≤ 19: LCL11n<n>.prod_lc<n> (kernel-checked D·D certificates, common
  denominators cancelled by BondLC3.scale_cancel); n ≥ 20: BondLCBig (both sides vanish). Lean 4 core only, no axioms.
-/
import ym.BondLC6
import ym.BondLC3
import ym.BondLCBig
import ym.LCL11n7
import ym.LCL11n8
import ym.LCL11n9
import ym.LCL11n10
import ym.LCL11n11
import ym.LCL11n12
import ym.LCL11n13
import ym.LCL11n14
import ym.LCL11n15
import ym.LCL11n16
import ym.LCL11n17
import ym.LCL11n18
import ym.LCL11n19

namespace BondLCL11

open YMChain SU2Char HeatPair MigdalCone LinkIntegral

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

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))))

theorem up6 (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) (hX : X.length = 11) (n : Nat) (hn : n ≤ 6) : Stmt K O X n :=
  BondLC6.bond_lc_le6 K O F h1 h10 X hXg 9 hX n hn

theorem up7 (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) (hX : X.length = 11) (n : Nat) (hn : n ≤ 7) : Stmt K O X n := by
  cases Nat.lt_or_ge n 7 with
  | inl h => exact up6 K O F h1 h10 X hXg hX n (Nat.le_of_lt_succ h)
  | inr h =>
    have e : n = 7 := Nat.le_antisymm hn h
    subst e
    have g := LCL11.prod_lc7 K O F X hXg hX
    apply BondLC3.scale_cancel K O h1 h10 16
    have e1 : K.mul (LibRing.castN K (16 + 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 1377) (K.mul (cgc K X X 7) (cgc K X X 9)) := by
      show K.mul (LibRing.castN K 17) (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 (16 + 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 1360) (K.mul (cgc K X X 8) (cgc K X X 8)) := by
      show K.mul (LibRing.castN K 17) (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

theorem up8 (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) (hX : X.length = 11) (n : Nat) (hn : n ≤ 8) : Stmt K O X n := by
  cases Nat.lt_or_ge n 8 with
  | inl h => exact up7 K O F h1 h10 X hXg hX n (Nat.le_of_lt_succ h)
  | inr h =>
    have e : n = 8 := Nat.le_antisymm hn h
    subst e
    have g := LCL11.prod_lc8 K O F X hXg hX
    apply BondLC3.scale_cancel K O h1 h10 14
    have e1 : K.mul (LibRing.castN K (14 + 1)) (K.mul (LibRing.castN K ((8 + 2) * (8 + 2)))
        (K.mul (cgc K X X 8) (cgc K X X (8 + 2))))
        = K.mul (LibRing.castN K 1500) (K.mul (cgc K X X 8) (cgc K X X 10)) := by
      show K.mul (LibRing.castN K 15) (K.mul (LibRing.castN K 100) (K.mul (cgc K X X 8) (cgc K X X 10))) = _
      lib_ring
    have e2 : K.mul (LibRing.castN K (14 + 1)) (K.mul (LibRing.castN K ((8 + 1) * (8 + 3)))
        (K.mul (cgc K X X (8 + 1)) (cgc K X X (8 + 1))))
        = K.mul (LibRing.castN K 1485) (K.mul (cgc K X X 9) (cgc K X X 9)) := by
      show K.mul (LibRing.castN K 15) (K.mul (LibRing.castN K 99) (K.mul (cgc K X X 9) (cgc K X X 9))) = _
      lib_ring
    rw [e1, e2]
    exact g

theorem up9 (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) (hX : X.length = 11) (n : Nat) (hn : n ≤ 9) : Stmt K O X n := by
  cases Nat.lt_or_ge n 9 with
  | inl h => exact up8 K O F h1 h10 X hXg hX n (Nat.le_of_lt_succ h)
  | inr h =>
    have e : n = 9 := Nat.le_antisymm hn h
    subst e
    exact LCL11.prod_lc9 K O F X hXg hX

theorem up10 (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) (hX : X.length = 11) (n : Nat) (hn : n ≤ 10) : Stmt K O X n := by
  cases Nat.lt_or_ge n 10 with
  | inl h => exact up9 K O F h1 h10 X hXg hX n (Nat.le_of_lt_succ h)
  | inr h =>
    have e : n = 10 := Nat.le_antisymm hn h
    subst e
    exact LCL11.prod_lc10 K O F X hXg hX

theorem up11 (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) (hX : X.length = 11) (n : Nat) (hn : n ≤ 11) : Stmt K O X n := by
  cases Nat.lt_or_ge n 11 with
  | inl h => exact up10 K O F h1 h10 X hXg hX n (Nat.le_of_lt_succ h)
  | inr h =>
    have e : n = 11 := Nat.le_antisymm hn h
    subst e
    exact LCL11.prod_lc11 K O F X hXg hX

theorem up12 (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) (hX : X.length = 11) (n : Nat) (hn : n ≤ 12) : Stmt K O X n := by
  cases Nat.lt_or_ge n 12 with
  | inl h => exact up11 K O F h1 h10 X hXg hX n (Nat.le_of_lt_succ h)
  | inr h =>
    have e : n = 12 := Nat.le_antisymm hn h
    subst e
    exact LCL11.prod_lc12 K O F X hXg hX

theorem up13 (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) (hX : X.length = 11) (n : Nat) (hn : n ≤ 13) : Stmt K O X n := by
  cases Nat.lt_or_ge n 13 with
  | inl h => exact up12 K O F h1 h10 X hXg hX n (Nat.le_of_lt_succ h)
  | inr h =>
    have e : n = 13 := Nat.le_antisymm hn h
    subst e
    exact LCL11.prod_lc13 K O F X hXg hX

theorem up14 (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) (hX : X.length = 11) (n : Nat) (hn : n ≤ 14) : Stmt K O X n := by
  cases Nat.lt_or_ge n 14 with
  | inl h => exact up13 K O F h1 h10 X hXg hX n (Nat.le_of_lt_succ h)
  | inr h =>
    have e : n = 14 := Nat.le_antisymm hn h
    subst e
    exact LCL11.prod_lc14 K O F X hXg hX

theorem up15 (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) (hX : X.length = 11) (n : Nat) (hn : n ≤ 15) : Stmt K O X n := by
  cases Nat.lt_or_ge n 15 with
  | inl h => exact up14 K O F h1 h10 X hXg hX n (Nat.le_of_lt_succ h)
  | inr h =>
    have e : n = 15 := Nat.le_antisymm hn h
    subst e
    exact LCL11.prod_lc15 K O F X hXg hX

theorem up16 (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) (hX : X.length = 11) (n : Nat) (hn : n ≤ 16) : Stmt K O X n := by
  cases Nat.lt_or_ge n 16 with
  | inl h => exact up15 K O F h1 h10 X hXg hX n (Nat.le_of_lt_succ h)
  | inr h =>
    have e : n = 16 := Nat.le_antisymm hn h
    subst e
    exact LCL11.prod_lc16 K O F X hXg hX

theorem up17 (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) (hX : X.length = 11) (n : Nat) (hn : n ≤ 17) : Stmt K O X n := by
  cases Nat.lt_or_ge n 17 with
  | inl h => exact up16 K O F h1 h10 X hXg hX n (Nat.le_of_lt_succ h)
  | inr h =>
    have e : n = 17 := Nat.le_antisymm hn h
    subst e
    exact LCL11.prod_lc17 K O F X hXg hX

theorem up18 (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) (hX : X.length = 11) (n : Nat) (hn : n ≤ 18) : Stmt K O X n := by
  cases Nat.lt_or_ge n 18 with
  | inl h => exact up17 K O F h1 h10 X hXg hX n (Nat.le_of_lt_succ h)
  | inr h =>
    have e : n = 18 := Nat.le_antisymm hn h
    subst e
    exact LCL11.prod_lc18 K O F X hXg hX

theorem up19 (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) (hX : X.length = 11) (n : Nat) (hn : n ≤ 19) : Stmt K O X n := by
  cases Nat.lt_or_ge n 19 with
  | inl h => exact up18 K O F h1 h10 X hXg hX n (Nat.le_of_lt_succ h)
  | inr h =>
    have e : n = 19 := Nat.le_antisymm hn h
    subst e
    exact LCL11.prod_lc19 K O F X hXg hX

/-- **(A) for every v-good list of length 11, at every index** -/
theorem bond_lc_len11 (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) (hX : X.length = 11) (n : Nat) : Stmt K O X n := by
  cases Nat.lt_or_ge n 20 with
  | inl h => exact up19 K O F h1 h10 X hXg hX n (Nat.le_of_lt_succ h)
  | inr h =>
    apply BondLCBig.bond_lc_big K O h1 X n
    rw [hX]
    exact Nat.le_trans (by decide : 11 + 11 ≤ 20 + 2 + 1) (Nat.add_le_add_right (Nat.add_le_add_right h 2) 1)

end BondLCL11

#print axioms BondLCL11.up6
#print axioms BondLCL11.up7
#print axioms BondLCL11.up8
#print axioms BondLCL11.up9
#print axioms BondLCL11.up10
#print axioms BondLCL11.up11
#print axioms BondLCL11.up12
#print axioms BondLCL11.up13
#print axioms BondLCL11.up14
#print axioms BondLCL11.up15
#print axioms BondLCL11.up16
#print axioms BondLCL11.up17
#print axioms BondLCL11.up18
#print axioms BondLCL11.up19
#print axioms BondLCL11.bond_lc_len11

ym/BondLCL12.lean

lean_all/ym/BondLCL12.lean

/-
  ym/BondLCL12 — T45, G54: (A) for v-good lists of length 12 at every index n:
    (n+2)² c_n c_{n+2} ≤ (n+1)(n+3) c_{n+1}²  for c = X·X.
  n ≤ 6: BondLC6 (every length); 7 ≤ n ≤ 21: LCL12n<n>.prod_lc<n> (kernel-checked D·D certificates, common
  denominators cancelled by BondLC3.scale_cancel); n ≥ 22: BondLCBig (both sides vanish). Lean 4 core only, no axioms.
-/
import ym.BondLC6
import ym.BondLC3
import ym.BondLCBig
import ym.RingZ
import ym.LCL12n7
import ym.LCL12n8
import ym.LCL12n9
import ym.LCL12n10
import ym.LCL12n11
import ym.LCL12n12
import ym.LCL12n13
import ym.LCL12n14
import ym.LCL12n15
import ym.LCL12n16
import ym.LCL12n17
import ym.LCL12n18
import ym.LCL12n19
import ym.LCL12n20
import ym.LCL12n21

set_option maxHeartbeats 0

namespace BondLCL12

open YMChain SU2Char HeatPair MigdalCone LinkIntegral

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

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))))

theorem up6 (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) (hX : X.length = 12) (n : Nat) (hn : n ≤ 6) : Stmt K O X n :=
  BondLC6.bond_lc_le6 K O F h1 h10 X hXg 10 hX n hn

theorem up7 (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) (hX : X.length = 12) (n : Nat) (hn : n ≤ 7) : Stmt K O X n := by
  cases Nat.lt_or_ge n 7 with
  | inl h => exact up6 K O F h1 h10 X hXg hX n (Nat.le_of_lt_succ h)
  | inr h =>
    have e : n = 7 := Nat.le_antisymm hn h
    subst e
    have g := LCL12.prod_lc7 K O F X hXg hX
    apply BondLC3.scale_cancel K O h1 h10 242
    have e1 : K.mul (LibRing.castN K (242 + 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 19683) (K.mul (cgc K X X 7) (cgc K X X 9)) :=
      (K.mul_assoc _ _ _).symm.trans (congrArg (fun t => K.mul t (K.mul (cgc K X X 7) (cgc K X X (7 + 2))))
        (RingZ.castN_mul K _ _).symm)
    have e2 : K.mul (LibRing.castN K (242 + 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 19440) (K.mul (cgc K X X 8) (cgc K X X 8)) :=
      (K.mul_assoc _ _ _).symm.trans (congrArg (fun t => K.mul t (K.mul (cgc K X X (7 + 1)) (cgc K X X (7 + 1))))
        (RingZ.castN_mul K _ _).symm)
    rw [e1, e2]
    exact g

theorem up8 (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) (hX : X.length = 12) (n : Nat) (hn : n ≤ 8) : Stmt K O X n := by
  cases Nat.lt_or_ge n 8 with
  | inl h => exact up7 K O F h1 h10 X hXg hX n (Nat.le_of_lt_succ h)
  | inr h =>
    have e : n = 8 := Nat.le_antisymm hn h
    subst e
    exact LCL12.prod_lc8 K O F X hXg hX

theorem up9 (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) (hX : X.length = 12) (n : Nat) (hn : n ≤ 9) : Stmt K O X n := by
  cases Nat.lt_or_ge n 9 with
  | inl h => exact up8 K O F h1 h10 X hXg hX n (Nat.le_of_lt_succ h)
  | inr h =>
    have e : n = 9 := Nat.le_antisymm hn h
    subst e
    have g := LCL12.prod_lc9 K O F X hXg hX
    apply BondLC3.scale_cancel K O h1 h10 15
    have e1 : K.mul (LibRing.castN K (15 + 1)) (K.mul (LibRing.castN K ((9 + 2) * (9 + 2)))
        (K.mul (cgc K X X 9) (cgc K X X (9 + 2))))
        = K.mul (LibRing.castN K 1936) (K.mul (cgc K X X 9) (cgc K X X 11)) :=
      (K.mul_assoc _ _ _).symm.trans (congrArg (fun t => K.mul t (K.mul (cgc K X X 9) (cgc K X X (9 + 2))))
        (RingZ.castN_mul K _ _).symm)
    have e2 : K.mul (LibRing.castN K (15 + 1)) (K.mul (LibRing.castN K ((9 + 1) * (9 + 3)))
        (K.mul (cgc K X X (9 + 1)) (cgc K X X (9 + 1))))
        = K.mul (LibRing.castN K 1920) (K.mul (cgc K X X 10) (cgc K X X 10)) :=
      (K.mul_assoc _ _ _).symm.trans (congrArg (fun t => K.mul t (K.mul (cgc K X X (9 + 1)) (cgc K X X (9 + 1))))
        (RingZ.castN_mul K _ _).symm)
    rw [e1, e2]
    exact g

theorem up10 (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) (hX : X.length = 12) (n : Nat) (hn : n ≤ 10) : Stmt K O X n := by
  cases Nat.lt_or_ge n 10 with
  | inl h => exact up9 K O F h1 h10 X hXg hX n (Nat.le_of_lt_succ h)
  | inr h =>
    have e : n = 10 := Nat.le_antisymm hn h
    subst e
    have g := LCL12.prod_lc10 K O F X hXg hX
    apply BondLC3.scale_cancel K O h1 h10 2
    have e1 : K.mul (LibRing.castN K (2 + 1)) (K.mul (LibRing.castN K ((10 + 2) * (10 + 2)))
        (K.mul (cgc K X X 10) (cgc K X X (10 + 2))))
        = K.mul (LibRing.castN K 432) (K.mul (cgc K X X 10) (cgc K X X 12)) :=
      (K.mul_assoc _ _ _).symm.trans (congrArg (fun t => K.mul t (K.mul (cgc K X X 10) (cgc K X X (10 + 2))))
        (RingZ.castN_mul K _ _).symm)
    have e2 : K.mul (LibRing.castN K (2 + 1)) (K.mul (LibRing.castN K ((10 + 1) * (10 + 3)))
        (K.mul (cgc K X X (10 + 1)) (cgc K X X (10 + 1))))
        = K.mul (LibRing.castN K 429) (K.mul (cgc K X X 11) (cgc K X X 11)) :=
      (K.mul_assoc _ _ _).symm.trans (congrArg (fun t => K.mul t (K.mul (cgc K X X (10 + 1)) (cgc K X X (10 + 1))))
        (RingZ.castN_mul K _ _).symm)
    rw [e1, e2]
    exact g

theorem up11 (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) (hX : X.length = 12) (n : Nat) (hn : n ≤ 11) : Stmt K O X n := by
  cases Nat.lt_or_ge n 11 with
  | inl h => exact up10 K O F h1 h10 X hXg hX n (Nat.le_of_lt_succ h)
  | inr h =>
    have e : n = 11 := Nat.le_antisymm hn h
    subst e
    have g := LCL12.prod_lc11 K O F X hXg hX
    apply BondLC3.scale_cancel K O h1 h10 48
    have e1 : K.mul (LibRing.castN K (48 + 1)) (K.mul (LibRing.castN K ((11 + 2) * (11 + 2)))
        (K.mul (cgc K X X 11) (cgc K X X (11 + 2))))
        = K.mul (LibRing.castN K 8281) (K.mul (cgc K X X 11) (cgc K X X 13)) :=
      (K.mul_assoc _ _ _).symm.trans (congrArg (fun t => K.mul t (K.mul (cgc K X X 11) (cgc K X X (11 + 2))))
        (RingZ.castN_mul K _ _).symm)
    have e2 : K.mul (LibRing.castN K (48 + 1)) (K.mul (LibRing.castN K ((11 + 1) * (11 + 3)))
        (K.mul (cgc K X X (11 + 1)) (cgc K X X (11 + 1))))
        = K.mul (LibRing.castN K 8232) (K.mul (cgc K X X 12) (cgc K X X 12)) :=
      (K.mul_assoc _ _ _).symm.trans (congrArg (fun t => K.mul t (K.mul (cgc K X X (11 + 1)) (cgc K X X (11 + 1))))
        (RingZ.castN_mul K _ _).symm)
    rw [e1, e2]
    exact g

theorem up12 (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) (hX : X.length = 12) (n : Nat) (hn : n ≤ 12) : Stmt K O X n := by
  cases Nat.lt_or_ge n 12 with
  | inl h => exact up11 K O F h1 h10 X hXg hX n (Nat.le_of_lt_succ h)
  | inr h =>
    have e : n = 12 := Nat.le_antisymm hn h
    subst e
    exact LCL12.prod_lc12 K O F X hXg hX

theorem up13 (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) (hX : X.length = 12) (n : Nat) (hn : n ≤ 13) : Stmt K O X n := by
  cases Nat.lt_or_ge n 13 with
  | inl h => exact up12 K O F h1 h10 X hXg hX n (Nat.le_of_lt_succ h)
  | inr h =>
    have e : n = 13 := Nat.le_antisymm hn h
    subst e
    exact LCL12.prod_lc13 K O F X hXg hX

theorem up14 (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) (hX : X.length = 12) (n : Nat) (hn : n ≤ 14) : Stmt K O X n := by
  cases Nat.lt_or_ge n 14 with
  | inl h => exact up13 K O F h1 h10 X hXg hX n (Nat.le_of_lt_succ h)
  | inr h =>
    have e : n = 14 := Nat.le_antisymm hn h
    subst e
    exact LCL12.prod_lc14 K O F X hXg hX

theorem up15 (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) (hX : X.length = 12) (n : Nat) (hn : n ≤ 15) : Stmt K O X n := by
  cases Nat.lt_or_ge n 15 with
  | inl h => exact up14 K O F h1 h10 X hXg hX n (Nat.le_of_lt_succ h)
  | inr h =>
    have e : n = 15 := Nat.le_antisymm hn h
    subst e
    exact LCL12.prod_lc15 K O F X hXg hX

theorem up16 (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) (hX : X.length = 12) (n : Nat) (hn : n ≤ 16) : Stmt K O X n := by
  cases Nat.lt_or_ge n 16 with
  | inl h => exact up15 K O F h1 h10 X hXg hX n (Nat.le_of_lt_succ h)
  | inr h =>
    have e : n = 16 := Nat.le_antisymm hn h
    subst e
    exact LCL12.prod_lc16 K O F X hXg hX

theorem up17 (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) (hX : X.length = 12) (n : Nat) (hn : n ≤ 17) : Stmt K O X n := by
  cases Nat.lt_or_ge n 17 with
  | inl h => exact up16 K O F h1 h10 X hXg hX n (Nat.le_of_lt_succ h)
  | inr h =>
    have e : n = 17 := Nat.le_antisymm hn h
    subst e
    exact LCL12.prod_lc17 K O F X hXg hX

theorem up18 (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) (hX : X.length = 12) (n : Nat) (hn : n ≤ 18) : Stmt K O X n := by
  cases Nat.lt_or_ge n 18 with
  | inl h => exact up17 K O F h1 h10 X hXg hX n (Nat.le_of_lt_succ h)
  | inr h =>
    have e : n = 18 := Nat.le_antisymm hn h
    subst e
    exact LCL12.prod_lc18 K O F X hXg hX

theorem up19 (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) (hX : X.length = 12) (n : Nat) (hn : n ≤ 19) : Stmt K O X n := by
  cases Nat.lt_or_ge n 19 with
  | inl h => exact up18 K O F h1 h10 X hXg hX n (Nat.le_of_lt_succ h)
  | inr h =>
    have e : n = 19 := Nat.le_antisymm hn h
    subst e
    exact LCL12.prod_lc19 K O F X hXg hX

theorem up20 (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) (hX : X.length = 12) (n : Nat) (hn : n ≤ 20) : Stmt K O X n := by
  cases Nat.lt_or_ge n 20 with
  | inl h => exact up19 K O F h1 h10 X hXg hX n (Nat.le_of_lt_succ h)
  | inr h =>
    have e : n = 20 := Nat.le_antisymm hn h
    subst e
    exact LCL12.prod_lc20 K O F X hXg hX

theorem up21 (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) (hX : X.length = 12) (n : Nat) (hn : n ≤ 21) : Stmt K O X n := by
  cases Nat.lt_or_ge n 21 with
  | inl h => exact up20 K O F h1 h10 X hXg hX n (Nat.le_of_lt_succ h)
  | inr h =>
    have e : n = 21 := Nat.le_antisymm hn h
    subst e
    exact LCL12.prod_lc21 K O F X hXg hX

/-- **(A) for every v-good list of length 12, at every index** -/
theorem bond_lc_len12 (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) (hX : X.length = 12) (n : Nat) : Stmt K O X n := by
  cases Nat.lt_or_ge n 22 with
  | inl h => exact up21 K O F h1 h10 X hXg hX n (Nat.le_of_lt_succ h)
  | inr h =>
    apply BondLCBig.bond_lc_big K O h1 X n
    rw [hX]
    exact Nat.le_trans (by decide : 12 + 12 ≤ 22 + 2 + 1) (Nat.add_le_add_right (Nat.add_le_add_right h 2) 1)

end BondLCL12

#print axioms BondLCL12.up6
#print axioms BondLCL12.up7
#print axioms BondLCL12.up8
#print axioms BondLCL12.up9
#print axioms BondLCL12.up10
#print axioms BondLCL12.up11
#print axioms BondLCL12.up12
#print axioms BondLCL12.up13
#print axioms BondLCL12.up14
#print axioms BondLCL12.up15
#print axioms BondLCL12.up16
#print axioms BondLCL12.up17
#print axioms BondLCL12.up18
#print axioms BondLCL12.up19
#print axioms BondLCL12.up20
#print axioms BondLCL12.up21
#print axioms BondLCL12.bond_lc_len12

ym/BondLCL13.lean

lean_all/ym/BondLCL13.lean

/-
  ym/BondLCL13 — T45, G54: (A) for v-good lists of length 13 at every index n:
    (n+2)² c_n c_{n+2} ≤ (n+1)(n+3) c_{n+1}²  for c = X·X.
  n ≤ 6: BondLC6 (every length); 7 ≤ n ≤ 23: LCL13n<n>.prod_lc<n> (kernel-checked D·D certificates, common
  denominators cancelled by BondLC3.scale_cancel); n ≥ 24: BondLCBig (both sides vanish). Lean 4 core only, no axioms.
-/
import ym.BondLC6
import ym.BondLC3
import ym.BondLCBig
import ym.RingZ
import ym.LCL13n7
import ym.LCL13n8
import ym.LCL13n9
import ym.LCL13n10
import ym.LCL13n11
import ym.LCL13n12
import ym.LCL13n13
import ym.LCL13n14
import ym.LCL13n15
import ym.LCL13n16
import ym.LCL13n17
import ym.LCL13n18
import ym.LCL13n19
import ym.LCL13n20
import ym.LCL13n21
import ym.LCL13n22
import ym.LCL13n23

set_option maxHeartbeats 0

namespace BondLCL13

open YMChain SU2Char HeatPair MigdalCone LinkIntegral

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

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))))

theorem up6 (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) (hX : X.length = 13) (n : Nat) (hn : n ≤ 6) : Stmt K O X n :=
  BondLC6.bond_lc_le6 K O F h1 h10 X hXg 11 hX n hn

theorem up7 (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) (hX : X.length = 13) (n : Nat) (hn : n ≤ 7) : Stmt K O X n := by
  cases Nat.lt_or_ge n 7 with
  | inl h => exact up6 K O F h1 h10 X hXg hX n (Nat.le_of_lt_succ h)
  | inr h =>
    have e : n = 7 := Nat.le_antisymm hn h
    subst e
    have g := LCL13.prod_lc7 K O F X hXg hX
    apply BondLC3.scale_cancel K O h1 h10 233
    have e1 : K.mul (LibRing.castN K (233 + 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 18954) (K.mul (cgc K X X 7) (cgc K X X 9)) :=
      (K.mul_assoc _ _ _).symm.trans (congrArg (fun t => K.mul t (K.mul (cgc K X X 7) (cgc K X X (7 + 2))))
        (RingZ.castN_mul K _ _).symm)
    have e2 : K.mul (LibRing.castN K (233 + 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 18720) (K.mul (cgc K X X 8) (cgc K X X 8)) :=
      (K.mul_assoc _ _ _).symm.trans (congrArg (fun t => K.mul t (K.mul (cgc K X X (7 + 1)) (cgc K X X (7 + 1))))
        (RingZ.castN_mul K _ _).symm)
    rw [e1, e2]
    exact g

theorem up8 (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) (hX : X.length = 13) (n : Nat) (hn : n ≤ 8) : Stmt K O X n := by
  cases Nat.lt_or_ge n 8 with
  | inl h => exact up7 K O F h1 h10 X hXg hX n (Nat.le_of_lt_succ h)
  | inr h =>
    have e : n = 8 := Nat.le_antisymm hn h
    subst e
    have g := LCL13.prod_lc8 K O F X hXg hX
    apply BondLC3.scale_cancel K O h1 h10 2584
    have e1 : K.mul (LibRing.castN K (2584 + 1)) (K.mul (LibRing.castN K ((8 + 2) * (8 + 2)))
        (K.mul (cgc K X X 8) (cgc K X X (8 + 2))))
        = K.mul (LibRing.castN K 258500) (K.mul (cgc K X X 8) (cgc K X X 10)) :=
      (K.mul_assoc _ _ _).symm.trans (congrArg (fun t => K.mul t (K.mul (cgc K X X 8) (cgc K X X (8 + 2))))
        (RingZ.castN_mul K _ _).symm)
    have e2 : K.mul (LibRing.castN K (2584 + 1)) (K.mul (LibRing.castN K ((8 + 1) * (8 + 3)))
        (K.mul (cgc K X X (8 + 1)) (cgc K X X (8 + 1))))
        = K.mul (LibRing.castN K 255915) (K.mul (cgc K X X 9) (cgc K X X 9)) :=
      (K.mul_assoc _ _ _).symm.trans (congrArg (fun t => K.mul t (K.mul (cgc K X X (8 + 1)) (cgc K X X (8 + 1))))
        (RingZ.castN_mul K _ _).symm)
    rw [e1, e2]
    exact g

theorem up9 (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) (hX : X.length = 13) (n : Nat) (hn : n ≤ 9) : Stmt K O X n := by
  cases Nat.lt_or_ge n 9 with
  | inl h => exact up8 K O F h1 h10 X hXg hX n (Nat.le_of_lt_succ h)
  | inr h =>
    have e : n = 9 := Nat.le_antisymm hn h
    subst e
    have g := LCL13.prod_lc9 K O F X hXg hX
    apply BondLC3.scale_cancel K O h1 h10 24
    have e1 : K.mul (LibRing.castN K (24 + 1)) (K.mul (LibRing.castN K ((9 + 2) * (9 + 2)))
        (K.mul (cgc K X X 9) (cgc K X X (9 + 2))))
        = K.mul (LibRing.castN K 3025) (K.mul (cgc K X X 9) (cgc K X X 11)) :=
      (K.mul_assoc _ _ _).symm.trans (congrArg (fun t => K.mul t (K.mul (cgc K X X 9) (cgc K X X (9 + 2))))
        (RingZ.castN_mul K _ _).symm)
    have e2 : K.mul (LibRing.castN K (24 + 1)) (K.mul (LibRing.castN K ((9 + 1) * (9 + 3)))
        (K.mul (cgc K X X (9 + 1)) (cgc K X X (9 + 1))))
        = K.mul (LibRing.castN K 3000) (K.mul (cgc K X X 10) (cgc K X X 10)) :=
      (K.mul_assoc _ _ _).symm.trans (congrArg (fun t => K.mul t (K.mul (cgc K X X (9 + 1)) (cgc K X X (9 + 1))))
        (RingZ.castN_mul K _ _).symm)
    rw [e1, e2]
    exact g

theorem up10 (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) (hX : X.length = 13) (n : Nat) (hn : n ≤ 10) : Stmt K O X n := by
  cases Nat.lt_or_ge n 10 with
  | inl h => exact up9 K O F h1 h10 X hXg hX n (Nat.le_of_lt_succ h)
  | inr h =>
    have e : n = 10 := Nat.le_antisymm hn h
    subst e
    have g := LCL13.prod_lc10 K O F X hXg hX
    apply BondLC3.scale_cancel K O h1 h10 3
    have e1 : K.mul (LibRing.castN K (3 + 1)) (K.mul (LibRing.castN K ((10 + 2) * (10 + 2)))
        (K.mul (cgc K X X 10) (cgc K X X (10 + 2))))
        = K.mul (LibRing.castN K 576) (K.mul (cgc K X X 10) (cgc K X X 12)) :=
      (K.mul_assoc _ _ _).symm.trans (congrArg (fun t => K.mul t (K.mul (cgc K X X 10) (cgc K X X (10 + 2))))
        (RingZ.castN_mul K _ _).symm)
    have e2 : K.mul (LibRing.castN K (3 + 1)) (K.mul (LibRing.castN K ((10 + 1) * (10 + 3)))
        (K.mul (cgc K X X (10 + 1)) (cgc K X X (10 + 1))))
        = K.mul (LibRing.castN K 572) (K.mul (cgc K X X 11) (cgc K X X 11)) :=
      (K.mul_assoc _ _ _).symm.trans (congrArg (fun t => K.mul t (K.mul (cgc K X X (10 + 1)) (cgc K X X (10 + 1))))
        (RingZ.castN_mul K _ _).symm)
    rw [e1, e2]
    exact g

theorem up11 (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) (hX : X.length = 13) (n : Nat) (hn : n ≤ 11) : Stmt K O X n := by
  cases Nat.lt_or_ge n 11 with
  | inl h => exact up10 K O F h1 h10 X hXg hX n (Nat.le_of_lt_succ h)
  | inr h =>
    have e : n = 11 := Nat.le_antisymm hn h
    subst e
    have g := LCL13.prod_lc11 K O F X hXg hX
    apply BondLC3.scale_cancel K O h1 h10 18
    have e1 : K.mul (LibRing.castN K (18 + 1)) (K.mul (LibRing.castN K ((11 + 2) * (11 + 2)))
        (K.mul (cgc K X X 11) (cgc K X X (11 + 2))))
        = K.mul (LibRing.castN K 3211) (K.mul (cgc K X X 11) (cgc K X X 13)) :=
      (K.mul_assoc _ _ _).symm.trans (congrArg (fun t => K.mul t (K.mul (cgc K X X 11) (cgc K X X (11 + 2))))
        (RingZ.castN_mul K _ _).symm)
    have e2 : K.mul (LibRing.castN K (18 + 1)) (K.mul (LibRing.castN K ((11 + 1) * (11 + 3)))
        (K.mul (cgc K X X (11 + 1)) (cgc K X X (11 + 1))))
        = K.mul (LibRing.castN K 3192) (K.mul (cgc K X X 12) (cgc K X X 12)) :=
      (K.mul_assoc _ _ _).symm.trans (congrArg (fun t => K.mul t (K.mul (cgc K X X (11 + 1)) (cgc K X X (11 + 1))))
        (RingZ.castN_mul K _ _).symm)
    rw [e1, e2]
    exact g

theorem up12 (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) (hX : X.length = 13) (n : Nat) (hn : n ≤ 12) : Stmt K O X n := by
  cases Nat.lt_or_ge n 12 with
  | inl h => exact up11 K O F h1 h10 X hXg hX n (Nat.le_of_lt_succ h)
  | inr h =>
    have e : n = 12 := Nat.le_antisymm hn h
    subst e
    have g := LCL13.prod_lc12 K O F X hXg hX
    apply BondLC3.scale_cancel K O h1 h10 1
    have e1 : K.mul (LibRing.castN K (1 + 1)) (K.mul (LibRing.castN K ((12 + 2) * (12 + 2)))
        (K.mul (cgc K X X 12) (cgc K X X (12 + 2))))
        = K.mul (LibRing.castN K 392) (K.mul (cgc K X X 12) (cgc K X X 14)) :=
      (K.mul_assoc _ _ _).symm.trans (congrArg (fun t => K.mul t (K.mul (cgc K X X 12) (cgc K X X (12 + 2))))
        (RingZ.castN_mul K _ _).symm)
    have e2 : K.mul (LibRing.castN K (1 + 1)) (K.mul (LibRing.castN K ((12 + 1) * (12 + 3)))
        (K.mul (cgc K X X (12 + 1)) (cgc K X X (12 + 1))))
        = K.mul (LibRing.castN K 390) (K.mul (cgc K X X 13) (cgc K X X 13)) :=
      (K.mul_assoc _ _ _).symm.trans (congrArg (fun t => K.mul t (K.mul (cgc K X X (12 + 1)) (cgc K X X (12 + 1))))
        (RingZ.castN_mul K _ _).symm)
    rw [e1, e2]
    exact g

theorem up13 (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) (hX : X.length = 13) (n : Nat) (hn : n ≤ 13) : Stmt K O X n := by
  cases Nat.lt_or_ge n 13 with
  | inl h => exact up12 K O F h1 h10 X hXg hX n (Nat.le_of_lt_succ h)
  | inr h =>
    have e : n = 13 := Nat.le_antisymm hn h
    subst e
    exact LCL13.prod_lc13 K O F X hXg hX

theorem up14 (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) (hX : X.length = 13) (n : Nat) (hn : n ≤ 14) : Stmt K O X n := by
  cases Nat.lt_or_ge n 14 with
  | inl h => exact up13 K O F h1 h10 X hXg hX n (Nat.le_of_lt_succ h)
  | inr h =>
    have e : n = 14 := Nat.le_antisymm hn h
    subst e
    have g := LCL13.prod_lc14 K O F X hXg hX
    apply BondLC3.scale_cancel K O h1 h10 2
    have e1 : K.mul (LibRing.castN K (2 + 1)) (K.mul (LibRing.castN K ((14 + 2) * (14 + 2)))
        (K.mul (cgc K X X 14) (cgc K X X (14 + 2))))
        = K.mul (LibRing.castN K 768) (K.mul (cgc K X X 14) (cgc K X X 16)) :=
      (K.mul_assoc _ _ _).symm.trans (congrArg (fun t => K.mul t (K.mul (cgc K X X 14) (cgc K X X (14 + 2))))
        (RingZ.castN_mul K _ _).symm)
    have e2 : K.mul (LibRing.castN K (2 + 1)) (K.mul (LibRing.castN K ((14 + 1) * (14 + 3)))
        (K.mul (cgc K X X (14 + 1)) (cgc K X X (14 + 1))))
        = K.mul (LibRing.castN K 765) (K.mul (cgc K X X 15) (cgc K X X 15)) :=
      (K.mul_assoc _ _ _).symm.trans (congrArg (fun t => K.mul t (K.mul (cgc K X X (14 + 1)) (cgc K X X (14 + 1))))
        (RingZ.castN_mul K _ _).symm)
    rw [e1, e2]
    exact g

theorem up15 (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) (hX : X.length = 13) (n : Nat) (hn : n ≤ 15) : Stmt K O X n := by
  cases Nat.lt_or_ge n 15 with
  | inl h => exact up14 K O F h1 h10 X hXg hX n (Nat.le_of_lt_succ h)
  | inr h =>
    have e : n = 15 := Nat.le_antisymm hn h
    subst e
    exact LCL13.prod_lc15 K O F X hXg hX

theorem up16 (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) (hX : X.length = 13) (n : Nat) (hn : n ≤ 16) : Stmt K O X n := by
  cases Nat.lt_or_ge n 16 with
  | inl h => exact up15 K O F h1 h10 X hXg hX n (Nat.le_of_lt_succ h)
  | inr h =>
    have e : n = 16 := Nat.le_antisymm hn h
    subst e
    exact LCL13.prod_lc16 K O F X hXg hX

theorem up17 (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) (hX : X.length = 13) (n : Nat) (hn : n ≤ 17) : Stmt K O X n := by
  cases Nat.lt_or_ge n 17 with
  | inl h => exact up16 K O F h1 h10 X hXg hX n (Nat.le_of_lt_succ h)
  | inr h =>
    have e : n = 17 := Nat.le_antisymm hn h
    subst e
    exact LCL13.prod_lc17 K O F X hXg hX

theorem up18 (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) (hX : X.length = 13) (n : Nat) (hn : n ≤ 18) : Stmt K O X n := by
  cases Nat.lt_or_ge n 18 with
  | inl h => exact up17 K O F h1 h10 X hXg hX n (Nat.le_of_lt_succ h)
  | inr h =>
    have e : n = 18 := Nat.le_antisymm hn h
    subst e
    exact LCL13.prod_lc18 K O F X hXg hX

theorem up19 (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) (hX : X.length = 13) (n : Nat) (hn : n ≤ 19) : Stmt K O X n := by
  cases Nat.lt_or_ge n 19 with
  | inl h => exact up18 K O F h1 h10 X hXg hX n (Nat.le_of_lt_succ h)
  | inr h =>
    have e : n = 19 := Nat.le_antisymm hn h
    subst e
    exact LCL13.prod_lc19 K O F X hXg hX

theorem up20 (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) (hX : X.length = 13) (n : Nat) (hn : n ≤ 20) : Stmt K O X n := by
  cases Nat.lt_or_ge n 20 with
  | inl h => exact up19 K O F h1 h10 X hXg hX n (Nat.le_of_lt_succ h)
  | inr h =>
    have e : n = 20 := Nat.le_antisymm hn h
    subst e
    exact LCL13.prod_lc20 K O F X hXg hX

theorem up21 (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) (hX : X.length = 13) (n : Nat) (hn : n ≤ 21) : Stmt K O X n := by
  cases Nat.lt_or_ge n 21 with
  | inl h => exact up20 K O F h1 h10 X hXg hX n (Nat.le_of_lt_succ h)
  | inr h =>
    have e : n = 21 := Nat.le_antisymm hn h
    subst e
    exact LCL13.prod_lc21 K O F X hXg hX

theorem up22 (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) (hX : X.length = 13) (n : Nat) (hn : n ≤ 22) : Stmt K O X n := by
  cases Nat.lt_or_ge n 22 with
  | inl h => exact up21 K O F h1 h10 X hXg hX n (Nat.le_of_lt_succ h)
  | inr h =>
    have e : n = 22 := Nat.le_antisymm hn h
    subst e
    exact LCL13.prod_lc22 K O F X hXg hX

theorem up23 (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) (hX : X.length = 13) (n : Nat) (hn : n ≤ 23) : Stmt K O X n := by
  cases Nat.lt_or_ge n 23 with
  | inl h => exact up22 K O F h1 h10 X hXg hX n (Nat.le_of_lt_succ h)
  | inr h =>
    have e : n = 23 := Nat.le_antisymm hn h
    subst e
    exact LCL13.prod_lc23 K O F X hXg hX

/-- **(A) for every v-good list of length 13, at every index** -/
theorem bond_lc_len13 (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) (hX : X.length = 13) (n : Nat) : Stmt K O X n := by
  cases Nat.lt_or_ge n 24 with
  | inl h => exact up23 K O F h1 h10 X hXg hX n (Nat.le_of_lt_succ h)
  | inr h =>
    apply BondLCBig.bond_lc_big K O h1 X n
    rw [hX]
    exact Nat.le_trans (by decide : 13 + 13 ≤ 24 + 2 + 1) (Nat.add_le_add_right (Nat.add_le_add_right h 2) 1)

end BondLCL13

#print axioms BondLCL13.up6
#print axioms BondLCL13.up7
#print axioms BondLCL13.up8
#print axioms BondLCL13.up9
#print axioms BondLCL13.up10
#print axioms BondLCL13.up11
#print axioms BondLCL13.up12
#print axioms BondLCL13.up13
#print axioms BondLCL13.up14
#print axioms BondLCL13.up15
#print axioms BondLCL13.up16
#print axioms BondLCL13.up17
#print axioms BondLCL13.up18
#print axioms BondLCL13.up19
#print axioms BondLCL13.up20
#print axioms BondLCL13.up21
#print axioms BondLCL13.up22
#print axioms BondLCL13.up23
#print axioms BondLCL13.bond_lc_len13

ym/BondLCL14.lean

lean_all/ym/BondLCL14.lean

/-
  ym/BondLCL14 — T45, G54: (A) for v-good lists of length 14 at every index n:
    (n+2)² c_n c_{n+2} ≤ (n+1)(n+3) c_{n+1}²  for c = X·X.
  n ≤ 7: BondLC7 (every length); 8 ≤ n ≤ 25: LCL14n<n>.prod_lc<n> (kernel-checked D·D certificates, common
  denominators cancelled by BondLC3.scale_cancel); n ≥ 26: BondLCBig (both sides vanish). Lean 4 core only, no axioms.
-/
import ym.BondLC7
import ym.BondLC3
import ym.BondLCBig
import ym.RingZ
import ym.LCL14n8
import ym.LCL14n9
import ym.LCL14n10
import ym.LCL14n11
import ym.LCL14n12
import ym.LCL14n13
import ym.LCL14n14
import ym.LCL14n15
import ym.LCL14n16
import ym.LCL14n17
import ym.LCL14n18
import ym.LCL14n19
import ym.LCL14n20
import ym.LCL14n21
import ym.LCL14n22
import ym.LCL14n23
import ym.LCL14n24
import ym.LCL14n25

set_option maxHeartbeats 0

namespace BondLCL14

open YMChain SU2Char HeatPair MigdalCone LinkIntegral

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

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))))

theorem up7 (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) (hX : X.length = 14) (n : Nat) (hn : n ≤ 7) : Stmt K O X n :=
  BondLC7.bond_lc_le7 K O F h1 h10 X hXg 12 hX n hn

theorem up8 (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) (hX : X.length = 14) (n : Nat) (hn : n ≤ 8) : Stmt K O X n := by
  cases Nat.lt_or_ge n 8 with
  | inl h => exact up7 K O F h1 h10 X hXg hX n (Nat.le_of_lt_succ h)
  | inr h =>
    have e : n = 8 := Nat.le_antisymm hn h
    subst e
    have g := LCL14.prod_lc8 K O F X hXg hX
    apply BondLC3.scale_cancel K O h1 h10 455
    have e1 : K.mul (LibRing.castN K (455 + 1)) (K.mul (LibRing.castN K ((8 + 2) * (8 + 2)))
        (K.mul (cgc K X X 8) (cgc K X X (8 + 2))))
        = K.mul (LibRing.castN K 45600) (K.mul (cgc K X X 8) (cgc K X X 10)) :=
      (K.mul_assoc _ _ _).symm.trans (congrArg (fun t => K.mul t (K.mul (cgc K X X 8) (cgc K X X (8 + 2))))
        (RingZ.castN_mul K _ _).symm)
    have e2 : K.mul (LibRing.castN K (455 + 1)) (K.mul (LibRing.castN K ((8 + 1) * (8 + 3)))
        (K.mul (cgc K X X (8 + 1)) (cgc K X X (8 + 1))))
        = K.mul (LibRing.castN K 45144) (K.mul (cgc K X X 9) (cgc K X X 9)) :=
      (K.mul_assoc _ _ _).symm.trans (congrArg (fun t => K.mul t (K.mul (cgc K X X (8 + 1)) (cgc K X X (8 + 1))))
        (RingZ.castN_mul K _ _).symm)
    rw [e1, e2]
    exact g

theorem up9 (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) (hX : X.length = 14) (n : Nat) (hn : n ≤ 9) : Stmt K O X n := by
  cases Nat.lt_or_ge n 9 with
  | inl h => exact up8 K O F h1 h10 X hXg hX n (Nat.le_of_lt_succ h)
  | inr h =>
    have e : n = 9 := Nat.le_antisymm hn h
    subst e
    have g := LCL14.prod_lc9 K O F X hXg hX
    apply BondLC3.scale_cancel K O h1 h10 2134
    have e1 : K.mul (LibRing.castN K (2134 + 1)) (K.mul (LibRing.castN K ((9 + 2) * (9 + 2)))
        (K.mul (cgc K X X 9) (cgc K X X (9 + 2))))
        = K.mul (LibRing.castN K 258335) (K.mul (cgc K X X 9) (cgc K X X 11)) :=
      (K.mul_assoc _ _ _).symm.trans (congrArg (fun t => K.mul t (K.mul (cgc K X X 9) (cgc K X X (9 + 2))))
        (RingZ.castN_mul K _ _).symm)
    have e2 : K.mul (LibRing.castN K (2134 + 1)) (K.mul (LibRing.castN K ((9 + 1) * (9 + 3)))
        (K.mul (cgc K X X (9 + 1)) (cgc K X X (9 + 1))))
        = K.mul (LibRing.castN K 256200) (K.mul (cgc K X X 10) (cgc K X X 10)) :=
      (K.mul_assoc _ _ _).symm.trans (congrArg (fun t => K.mul t (K.mul (cgc K X X (9 + 1)) (cgc K X X (9 + 1))))
        (RingZ.castN_mul K _ _).symm)
    rw [e1, e2]
    exact g

theorem up10 (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) (hX : X.length = 14) (n : Nat) (hn : n ≤ 10) : Stmt K O X n := by
  cases Nat.lt_or_ge n 10 with
  | inl h => exact up9 K O F h1 h10 X hXg hX n (Nat.le_of_lt_succ h)
  | inr h =>
    have e : n = 10 := Nat.le_antisymm hn h
    subst e
    have g := LCL14.prod_lc10 K O F X hXg hX
    apply BondLC3.scale_cancel K O h1 h10 109927
    have e1 : K.mul (LibRing.castN K (109927 + 1)) (K.mul (LibRing.castN K ((10 + 2) * (10 + 2)))
        (K.mul (cgc K X X 10) (cgc K X X (10 + 2))))
        = K.mul (LibRing.castN K 15829632) (K.mul (cgc K X X 10) (cgc K X X 12)) :=
      (K.mul_assoc _ _ _).symm.trans (congrArg (fun t => K.mul t (K.mul (cgc K X X 10) (cgc K X X (10 + 2))))
        (RingZ.castN_mul K _ _).symm)
    have e2 : K.mul (LibRing.castN K (109927 + 1)) (K.mul (LibRing.castN K ((10 + 1) * (10 + 3)))
        (K.mul (cgc K X X (10 + 1)) (cgc K X X (10 + 1))))
        = K.mul (LibRing.castN K 15719704) (K.mul (cgc K X X 11) (cgc K X X 11)) :=
      (K.mul_assoc _ _ _).symm.trans (congrArg (fun t => K.mul t (K.mul (cgc K X X (10 + 1)) (cgc K X X (10 + 1))))
        (RingZ.castN_mul K _ _).symm)
    rw [e1, e2]
    exact g

theorem up11 (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) (hX : X.length = 14) (n : Nat) (hn : n ≤ 11) : Stmt K O X n := by
  cases Nat.lt_or_ge n 11 with
  | inl h => exact up10 K O F h1 h10 X hXg hX n (Nat.le_of_lt_succ h)
  | inr h =>
    have e : n = 11 := Nat.le_antisymm hn h
    subst e
    have g := LCL14.prod_lc11 K O F X hXg hX
    apply BondLC3.scale_cancel K O h1 h10 483
    have e1 : K.mul (LibRing.castN K (483 + 1)) (K.mul (LibRing.castN K ((11 + 2) * (11 + 2)))
        (K.mul (cgc K X X 11) (cgc K X X (11 + 2))))
        = K.mul (LibRing.castN K 81796) (K.mul (cgc K X X 11) (cgc K X X 13)) :=
      (K.mul_assoc _ _ _).symm.trans (congrArg (fun t => K.mul t (K.mul (cgc K X X 11) (cgc K X X (11 + 2))))
        (RingZ.castN_mul K _ _).symm)
    have e2 : K.mul (LibRing.castN K (483 + 1)) (K.mul (LibRing.castN K ((11 + 1) * (11 + 3)))
        (K.mul (cgc K X X (11 + 1)) (cgc K X X (11 + 1))))
        = K.mul (LibRing.castN K 81312) (K.mul (cgc K X X 12) (cgc K X X 12)) :=
      (K.mul_assoc _ _ _).symm.trans (congrArg (fun t => K.mul t (K.mul (cgc K X X (11 + 1)) (cgc K X X (11 + 1))))
        (RingZ.castN_mul K _ _).symm)
    rw [e1, e2]
    exact g

theorem up12 (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) (hX : X.length = 14) (n : Nat) (hn : n ≤ 12) : Stmt K O X n := by
  cases Nat.lt_or_ge n 12 with
  | inl h => exact up11 K O F h1 h10 X hXg hX n (Nat.le_of_lt_succ h)
  | inr h =>
    have e : n = 12 := Nat.le_antisymm hn h
    subst e
    have g := LCL14.prod_lc12 K O F X hXg hX
    apply BondLC3.scale_cancel K O h1 h10 2
    have e1 : K.mul (LibRing.castN K (2 + 1)) (K.mul (LibRing.castN K ((12 + 2) * (12 + 2)))
        (K.mul (cgc K X X 12) (cgc K X X (12 + 2))))
        = K.mul (LibRing.castN K 588) (K.mul (cgc K X X 12) (cgc K X X 14)) :=
      (K.mul_assoc _ _ _).symm.trans (congrArg (fun t => K.mul t (K.mul (cgc K X X 12) (cgc K X X (12 + 2))))
        (RingZ.castN_mul K _ _).symm)
    have e2 : K.mul (LibRing.castN K (2 + 1)) (K.mul (LibRing.castN K ((12 + 1) * (12 + 3)))
        (K.mul (cgc K X X (12 + 1)) (cgc K X X (12 + 1))))
        = K.mul (LibRing.castN K 585) (K.mul (cgc K X X 13) (cgc K X X 13)) :=
      (K.mul_assoc _ _ _).symm.trans (congrArg (fun t => K.mul t (K.mul (cgc K X X (12 + 1)) (cgc K X X (12 + 1))))
        (RingZ.castN_mul K _ _).symm)
    rw [e1, e2]
    exact g

theorem up13 (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) (hX : X.length = 14) (n : Nat) (hn : n ≤ 13) : Stmt K O X n := by
  cases Nat.lt_or_ge n 13 with
  | inl h => exact up12 K O F h1 h10 X hXg hX n (Nat.le_of_lt_succ h)
  | inr h =>
    have e : n = 13 := Nat.le_antisymm hn h
    subst e
    have g := LCL14.prod_lc13 K O F X hXg hX
    apply BondLC3.scale_cancel K O h1 h10 221
    have e1 : K.mul (LibRing.castN K (221 + 1)) (K.mul (LibRing.castN K ((13 + 2) * (13 + 2)))
        (K.mul (cgc K X X 13) (cgc K X X (13 + 2))))
        = K.mul (LibRing.castN K 49950) (K.mul (cgc K X X 13) (cgc K X X 15)) :=
      (K.mul_assoc _ _ _).symm.trans (congrArg (fun t => K.mul t (K.mul (cgc K X X 13) (cgc K X X (13 + 2))))
        (RingZ.castN_mul K _ _).symm)
    have e2 : K.mul (LibRing.castN K (221 + 1)) (K.mul (LibRing.castN K ((13 + 1) * (13 + 3)))
        (K.mul (cgc K X X (13 + 1)) (cgc K X X (13 + 1))))
        = K.mul (LibRing.castN K 49728) (K.mul (cgc K X X 14) (cgc K X X 14)) :=
      (K.mul_assoc _ _ _).symm.trans (congrArg (fun t => K.mul t (K.mul (cgc K X X (13 + 1)) (cgc K X X (13 + 1))))
        (RingZ.castN_mul K _ _).symm)
    rw [e1, e2]
    exact g

theorem up14 (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) (hX : X.length = 14) (n : Nat) (hn : n ≤ 14) : Stmt K O X n := by
  cases Nat.lt_or_ge n 14 with
  | inl h => exact up13 K O F h1 h10 X hXg hX n (Nat.le_of_lt_succ h)
  | inr h =>
    have e : n = 14 := Nat.le_antisymm hn h
    subst e
    have g := LCL14.prod_lc14 K O F X hXg hX
    apply BondLC3.scale_cancel K O h1 h10 4
    have e1 : K.mul (LibRing.castN K (4 + 1)) (K.mul (LibRing.castN K ((14 + 2) * (14 + 2)))
        (K.mul (cgc K X X 14) (cgc K X X (14 + 2))))
        = K.mul (LibRing.castN K 1280) (K.mul (cgc K X X 14) (cgc K X X 16)) :=
      (K.mul_assoc _ _ _).symm.trans (congrArg (fun t => K.mul t (K.mul (cgc K X X 14) (cgc K X X (14 + 2))))
        (RingZ.castN_mul K _ _).symm)
    have e2 : K.mul (LibRing.castN K (4 + 1)) (K.mul (LibRing.castN K ((14 + 1) * (14 + 3)))
        (K.mul (cgc K X X (14 + 1)) (cgc K X X (14 + 1))))
        = K.mul (LibRing.castN K 1275) (K.mul (cgc K X X 15) (cgc K X X 15)) :=
      (K.mul_assoc _ _ _).symm.trans (congrArg (fun t => K.mul t (K.mul (cgc K X X (14 + 1)) (cgc K X X (14 + 1))))
        (RingZ.castN_mul K _ _).symm)
    rw [e1, e2]
    exact g

theorem up15 (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) (hX : X.length = 14) (n : Nat) (hn : n ≤ 15) : Stmt K O X n := by
  cases Nat.lt_or_ge n 15 with
  | inl h => exact up14 K O F h1 h10 X hXg hX n (Nat.le_of_lt_succ h)
  | inr h =>
    have e : n = 15 := Nat.le_antisymm hn h
    subst e
    exact LCL14.prod_lc15 K O F X hXg hX

theorem up16 (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) (hX : X.length = 14) (n : Nat) (hn : n ≤ 16) : Stmt K O X n := by
  cases Nat.lt_or_ge n 16 with
  | inl h => exact up15 K O F h1 h10 X hXg hX n (Nat.le_of_lt_succ h)
  | inr h =>
    have e : n = 16 := Nat.le_antisymm hn h
    subst e
    exact LCL14.prod_lc16 K O F X hXg hX

theorem up17 (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) (hX : X.length = 14) (n : Nat) (hn : n ≤ 17) : Stmt K O X n := by
  cases Nat.lt_or_ge n 17 with
  | inl h => exact up16 K O F h1 h10 X hXg hX n (Nat.le_of_lt_succ h)
  | inr h =>
    have e : n = 17 := Nat.le_antisymm hn h
    subst e
    have g := LCL14.prod_lc17 K O F X hXg hX
    apply BondLC3.scale_cancel K O h1 h10 2
    have e1 : K.mul (LibRing.castN K (2 + 1)) (K.mul (LibRing.castN K ((17 + 2) * (17 + 2)))
        (K.mul (cgc K X X 17) (cgc K X X (17 + 2))))
        = K.mul (LibRing.castN K 1083) (K.mul (cgc K X X 17) (cgc K X X 19)) :=
      (K.mul_assoc _ _ _).symm.trans (congrArg (fun t => K.mul t (K.mul (cgc K X X 17) (cgc K X X (17 + 2))))
        (RingZ.castN_mul K _ _).symm)
    have e2 : K.mul (LibRing.castN K (2 + 1)) (K.mul (LibRing.castN K ((17 + 1) * (17 + 3)))
        (K.mul (cgc K X X (17 + 1)) (cgc K X X (17 + 1))))
        = K.mul (LibRing.castN K 1080) (K.mul (cgc K X X 18) (cgc K X X 18)) :=
      (K.mul_assoc _ _ _).symm.trans (congrArg (fun t => K.mul t (K.mul (cgc K X X (17 + 1)) (cgc K X X (17 + 1))))
        (RingZ.castN_mul K _ _).symm)
    rw [e1, e2]
    exact g

theorem up18 (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) (hX : X.length = 14) (n : Nat) (hn : n ≤ 18) : Stmt K O X n := by
  cases Nat.lt_or_ge n 18 with
  | inl h => exact up17 K O F h1 h10 X hXg hX n (Nat.le_of_lt_succ h)
  | inr h =>
    have e : n = 18 := Nat.le_antisymm hn h
    subst e
    exact LCL14.prod_lc18 K O F X hXg hX

theorem up19 (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) (hX : X.length = 14) (n : Nat) (hn : n ≤ 19) : Stmt K O X n := by
  cases Nat.lt_or_ge n 19 with
  | inl h => exact up18 K O F h1 h10 X hXg hX n (Nat.le_of_lt_succ h)
  | inr h =>
    have e : n = 19 := Nat.le_antisymm hn h
    subst e
    exact LCL14.prod_lc19 K O F X hXg hX

theorem up20 (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) (hX : X.length = 14) (n : Nat) (hn : n ≤ 20) : Stmt K O X n := by
  cases Nat.lt_or_ge n 20 with
  | inl h => exact up19 K O F h1 h10 X hXg hX n (Nat.le_of_lt_succ h)
  | inr h =>
    have e : n = 20 := Nat.le_antisymm hn h
    subst e
    exact LCL14.prod_lc20 K O F X hXg hX

theorem up21 (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) (hX : X.length = 14) (n : Nat) (hn : n ≤ 21) : Stmt K O X n := by
  cases Nat.lt_or_ge n 21 with
  | inl h => exact up20 K O F h1 h10 X hXg hX n (Nat.le_of_lt_succ h)
  | inr h =>
    have e : n = 21 := Nat.le_antisymm hn h
    subst e
    exact LCL14.prod_lc21 K O F X hXg hX

theorem up22 (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) (hX : X.length = 14) (n : Nat) (hn : n ≤ 22) : Stmt K O X n := by
  cases Nat.lt_or_ge n 22 with
  | inl h => exact up21 K O F h1 h10 X hXg hX n (Nat.le_of_lt_succ h)
  | inr h =>
    have e : n = 22 := Nat.le_antisymm hn h
    subst e
    exact LCL14.prod_lc22 K O F X hXg hX

theorem up23 (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) (hX : X.length = 14) (n : Nat) (hn : n ≤ 23) : Stmt K O X n := by
  cases Nat.lt_or_ge n 23 with
  | inl h => exact up22 K O F h1 h10 X hXg hX n (Nat.le_of_lt_succ h)
  | inr h =>
    have e : n = 23 := Nat.le_antisymm hn h
    subst e
    exact LCL14.prod_lc23 K O F X hXg hX

theorem up24 (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) (hX : X.length = 14) (n : Nat) (hn : n ≤ 24) : Stmt K O X n := by
  cases Nat.lt_or_ge n 24 with
  | inl h => exact up23 K O F h1 h10 X hXg hX n (Nat.le_of_lt_succ h)
  | inr h =>
    have e : n = 24 := Nat.le_antisymm hn h
    subst e
    exact LCL14.prod_lc24 K O F X hXg hX

theorem up25 (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) (hX : X.length = 14) (n : Nat) (hn : n ≤ 25) : Stmt K O X n := by
  cases Nat.lt_or_ge n 25 with
  | inl h => exact up24 K O F h1 h10 X hXg hX n (Nat.le_of_lt_succ h)
  | inr h =>
    have e : n = 25 := Nat.le_antisymm hn h
    subst e
    exact LCL14.prod_lc25 K O F X hXg hX

/-- **(A) for every v-good list of length 14, at every index** -/
theorem bond_lc_len14 (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) (hX : X.length = 14) (n : Nat) : Stmt K O X n := by
  cases Nat.lt_or_ge n 26 with
  | inl h => exact up25 K O F h1 h10 X hXg hX n (Nat.le_of_lt_succ h)
  | inr h =>
    apply BondLCBig.bond_lc_big K O h1 X n
    rw [hX]
    exact Nat.le_trans (by decide : 14 + 14 ≤ 26 + 2 + 1) (Nat.add_le_add_right (Nat.add_le_add_right h 2) 1)

end BondLCL14

#print axioms BondLCL14.up7
#print axioms BondLCL14.up8
#print axioms BondLCL14.up9
#print axioms BondLCL14.up10
#print axioms BondLCL14.up11
#print axioms BondLCL14.up12
#print axioms BondLCL14.up13
#print axioms BondLCL14.up14
#print axioms BondLCL14.up15
#print axioms BondLCL14.up16
#print axioms BondLCL14.up17
#print axioms BondLCL14.up18
#print axioms BondLCL14.up19
#print axioms BondLCL14.up20
#print axioms BondLCL14.up21
#print axioms BondLCL14.up22
#print axioms BondLCL14.up23
#print axioms BondLCL14.up24
#print axioms BondLCL14.up25
#print axioms BondLCL14.bond_lc_len14

ym/BondTube.lean

lean_all/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

ym/BoundaryRel.lean

lean_all/ym/BoundaryRel.lean

/-
  ym/BoundaryRel — T45, G67: one diagonal below the shift-invariant region.
  For X = x0 :: Y with |Y| ≤ t + 2:   c_{t+2}(X) = c_t(Y) + 2 x0 y_{t+1}.
  (TailSq.cgc_cons twice, MidSq.sq_cons2, TopShift.cgc_top on the tail of Y.) On the boundary diagonal
  (|X| = t + 3, n = t + 2 = |X| − 1) this writes (A) for X through the index one below the boundary of Y,
  perturbed by 2 x0 x_top. Lean 4 core only, no axioms.
-/
import ym.MidSq
import ym.TopShift

namespace BoundaryRel

open YMChain SU2Haar SU2Char Fusion MigdalCone HeatPair

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

theorem cgc_step2 (x0 : R) (Y : List R) (t : Nat) (hY : Y.length ≤ t + 2) :
    cgc K (x0 :: Y) (x0 :: Y) (t + 2)
      = K.add (cgc K Y Y t) (K.add (K.mul x0 (co K Y (t + 1))) (K.mul x0 (co K Y (t + 1)))) := by
  rw [TailSq.cgc_cons K x0 Y (t + 2), MidSq.sq_cons2 K x0 Y t]
  cases Y with
  | nil =>
    show K.add K.zero (K.add (K.add (K.mul x0 K.zero) (K.mul x0 K.zero)) K.zero)
      = K.add K.zero (K.add (K.mul x0 K.zero) (K.mul x0 K.zero))
    lib_ring
  | cons y0 Z =>
    have hZ : Z.length ≤ t + 1 := Nat.le_of_succ_le_succ hY
    have c1 : cgc K (y0 :: Z) (y0 :: Z) (t + 2) = cgc K Z Z t := TopShift.cgc_top K [y0] Z t hZ
    rw [c1, TailSq.cgc_cons K y0 Z t]
    lib_ring

end BoundaryRel

#print axioms BoundaryRel.cgc_step2

ym/Box3Area.lean

lean_all/ym/Box3Area.lean

/-
  ym/Box3Area — T27, cycle 52: **a closed instance of the ℤ₂ area law in three dimensions**.
  The 3×3×3 vertex box (54 links, 36 plaquettes, every link in ≤ 4 plaquettes, every plaquette on 4 links) and the 2×2 square
  loop W in the middle plane (8 links). Four half-plane cochains certify that every spanning surface has ≥ 4 plaquettes
  (min_area), and all hypotheses of wilson_area_law are checked by evaluation (Bool checkers proved sound by hand):
      N_W ≤ ( Σ_{A=0}^{36} 2^(4(8 + 4A) + 1) [A ≥ 4] t^A ) · Z     for every t ≥ 0 in every ordered ring.
  Lean 4 core only, proofs by hand (data checked by rfl), no axioms.
-/
import ym.MinArea

namespace Box3Area

open YMChain GKS StrongZ2 S2cComp S2cSplit AnimalDeg AnimalSupp AnimalSum AnimalArea WilsonArea MinArea

def W3 : List Nat := [0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0]

def P3 : List (List Nat) := [
  [1, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
  [1, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
  [0, 1, 1, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
  [0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
  [0, 0, 0, 1, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
  [0, 0, 0, 0, 1, 1, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
  [0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
  [0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
  [0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
  [0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
  [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
  [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
  [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
  [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
  [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
  [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
  [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
  [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
  [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
  [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0],
  [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0],
  [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
  [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0],
  [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0],
  [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0],
  [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
  [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0],
  [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0],
  [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
  [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0],
  [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0],
  [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1],
  [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0],
  [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 1, 0, 0, 0],
  [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 1, 0],
  [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 1]]

def F3 : List (List Nat) := [
  [0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
  [0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
  [0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
  [0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]]

def allLenB (L : Nat) : List (List Nat) → Bool
  | [] => true
  | q :: P => (q.length == L) && allLenB L P

def allLeB (D : Nat) : List Nat → Bool
  | [] => true
  | x :: v => Nat.ble x D && allLeB D v

def allQB (w : Nat) : List (List Nat) → Bool
  | [] => true
  | q :: P => Nat.ble (nz q) w && allQB w P

def oddB (W : List Nat) : List (List Nat) → Bool
  | [] => true
  | f :: fs => (parity (dot W f) == false) && oddB W fs

def oneB (fs : List (List Nat)) : List (List Nat) → Bool
  | [] => true
  | q :: P => Nat.ble (oddCnt q fs) 1 && oneB fs P

theorem beq_nat : ∀ a b : Nat, (a == b) = true → a = b
  | 0, 0, _ => rfl
  | 0, _ + 1, h => absurd h (fun e => Bool.noConfusion e)
  | _ + 1, 0, h => absurd h (fun e => Bool.noConfusion e)
  | a + 1, b + 1, h => congrArg Nat.succ (beq_nat a b h)

theorem ble_le : ∀ a b : Nat, Nat.ble a b = true → a ≤ b
  | 0, b, _ => Nat.zero_le b
  | _ + 1, 0, h => absurd h (fun e => Bool.noConfusion e)
  | a + 1, b + 1, h => Nat.succ_le_succ (ble_le a b h)

theorem allLenB_ok (L : Nat) : ∀ P, allLenB L P = true → ∀ q, memL q P → q.length = L
  | [], _, _, h => absurd h id
  | p :: P, h, q, hq => by
    rcases hq with e | e
    · rw [e]; exact beq_nat _ _ (AnimalTouch.and_l _ _ h)
    · exact allLenB_ok L P (AnimalTouch.and_r _ _ h) q e

theorem allLeB_ok (D : Nat) : ∀ v, allLeB D v = true → allLe v D
  | [], _ => trivial
  | _ :: v, h => ⟨ble_le _ _ (AnimalTouch.and_l _ _ h), allLeB_ok D v (AnimalTouch.and_r _ _ h)⟩

theorem allQB_ok (w : Nat) : ∀ P, allQB w P = true → allQ P w
  | [], _ => trivial
  | _ :: P, h => ⟨ble_le _ _ (AnimalTouch.and_l _ _ h), allQB_ok w P (AnimalTouch.and_r _ _ h)⟩

theorem oddB_ok (W : List Nat) : ∀ fs, oddB W fs = true → ∀ f, memL f fs → parity (dot W f) = false
  | [], _, _, h => absurd h id
  | g :: fs, h, f, hf => by
    rcases hf with e | e
    · rw [e]
      have h' := AnimalTouch.and_l _ _ h
      cases hp : parity (dot W g) with
      | false => rfl
      | true => rw [hp] at h'; exact absurd h' (fun e => Bool.noConfusion e)
    · exact oddB_ok W fs (AnimalTouch.and_r _ _ h) f e

theorem oneB_ok (fs : List (List Nat)) : ∀ P, oneB fs P = true → oneEach P fs
  | [], _ => trivial
  | _ :: P, h => ⟨ble_le _ _ (AnimalTouch.and_l _ _ h), oneB_ok fs P (AnimalTouch.and_r _ _ h)⟩

theorem chk_len : allLenB 54 P3 = true := rfl
theorem chk_W : W3.length = 54 := rfl
theorem chk_deg : allLeB 4 (degV P3) = true := rfl
theorem chk_q : allQB 4 P3 = true := rfl
theorem chk_odd : oddB W3 F3 = true := rfl
theorem chk_one : oneB F3 P3 = true := rfl
theorem chk_F : F3.length = 4 := rfl

/-- Every surface in the box spanning the 2×2 loop has at least 4 plaquettes. -/
theorem box3_min : ∀ C : List Bool, P3.length = C.length → evenAll (selS W3 P3 C) = true → 4 ≤ cnt C := by
  intro C hl he
  have := min_area W3 P3 F3 (oddB_ok W3 F3 chk_odd) (oneB_ok F3 P3 chk_one) C hl he
  rw [chk_F] at this; exact this

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

/-- **⟨W⟩ ≤ Σ_{A ≥ 4} 2^(4(8+4A)+1) t^A for the 2×2 loop in the 3×3×3 box, every t ≥ 0.** -/
theorem box3_area_law (h1 : O.le K.zero K.one) (t : R) (ht : O.le K.zero t) :
    O.le (N K t 54 W3 P3)
      (K.mul (sumK K (fun A => nsm K (2 ^ (4 * (nz W3 + 4 * A) + 1)) (gA K 4 t A)) (rangeL P3.length)) (N K t 54 (zl 54) P3)) :=
  wilson_area_law K O h1 t ht 54 W3 chk_W P3 (allLenB_ok 54 P3 chk_len) 4 4 4
    (allLeB_ok 4 _ chk_deg) (allQB_ok 4 P3 chk_q) box3_min

end Main

end Box3Area

#print axioms Box3Area.beq_nat
#print axioms Box3Area.ble_le
#print axioms Box3Area.allLenB_ok
#print axioms Box3Area.allLeB_ok
#print axioms Box3Area.allQB_ok
#print axioms Box3Area.oddB_ok
#print axioms Box3Area.oneB_ok
#print axioms Box3Area.chk_len
#print axioms Box3Area.chk_deg
#print axioms Box3Area.chk_q
#print axioms Box3Area.chk_odd
#print axioms Box3Area.chk_one
#print axioms Box3Area.box3_min
#print axioms Box3Area.box3_area_law
#print axioms Box3Area.chk_W
#print axioms Box3Area.chk_F

ym/Box4Area.lean

lean_all/ym/Box4Area.lean

/-
  ym/Box4Area — T27, cycle 53: **a closed instance of the ℤ₂ area law in four dimensions**.
  The 3⁴ vertex box (216 links, 216 plaquettes, every link in ≤ 6 = 2(d − 1) plaquettes, every plaquette on 4 links) and the
  2×2 square loop W in the plane z = w = 1 (8 links). Four 3-dimensional half-space cochains (the y-links at row j with x ≤ i,
  all z, w) certify A_min = 4; all hypotheses of wilson_area_law are evaluated by the Box3Area checkers:
      N_W ≤ ( Σ_{A=0}^{216} 2^(6(8 + 4A) + 1) [A ≥ 4] t^A ) · Z     for every t ≥ 0 in every ordered ring.
  Lean 4 core only, proofs by hand (data checked by rfl), no axioms.
-/
import ym.Box3Area

namespace Box4Area

set_option maxHeartbeats 0
set_option maxRecDepth 100000

open YMChain GKS StrongZ2 S2cComp S2cSplit AnimalDeg AnimalSupp AnimalSum AnimalArea WilsonArea MinArea Box3Area

def W4 : List Nat := [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,   -- (+269 chars; full line in the Zenodo source)

def P4 : List (List Nat) := [
  [1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0  -- (+251 chars; full line in the Zenodo source)
  [1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0  -- (+251 chars; full line in the Zenodo source)
  [1, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0  -- (+251 chars; full line in the Zenodo source)
  [0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0  -- (+251 chars; full line in the Zenodo source)
  [0, 1, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0  -- (+251 chars; full line in the Zenodo source)
  [0, 0, 1, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0  -- (+251 chars; full line in the Zenodo source)
  [0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0  -- (+251 chars; full line in the Zenodo source)
  [0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0  -- (+251 chars; full line in the Zenodo source)
  [0, 0, 0, 0, 1, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0  -- (+251 chars; full line in the Zenodo source)
  [0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0  -- (+251 chars; full line in the Zenodo source)
  [0, 0, 0, 0, 0, 1, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0  -- (+251 chars; full line in the Zenodo source)
  [0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0  -- (+251 chars; full line in the Zenodo source)
  [0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0  -- (+251 chars; full line in the Zenodo source)
  [0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0  -- (+251 chars; full line in the Zenodo source)
  [0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0  -- (+251 chars; full line in the Zenodo source)
  [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0  -- (+251 chars; full line in the Zenodo source)
  [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0  -- (+251 chars; full line in the Zenodo source)
  [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0  -- (+251 chars; full line in the Zenodo source)
  [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0  -- (+251 chars; full line in the Zenodo source)
  [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0  -- (+251 chars; full line in the Zenodo source)
  [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0  -- (+251 chars; full line in the Zenodo source)
  [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0  -- (+251 chars; full line in the Zenodo source)
  [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0  -- (+251 chars; full line in the Zenodo source)
  [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0  -- (+251 chars; full line in the Zenodo source)
  [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0  -- (+251 chars; full line in the Zenodo source)
  [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0  -- (+251 chars; full line in the Zenodo source)
  [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0  -- (+251 chars; full line in the Zenodo source)
  [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0  -- (+251 chars; full line in the Zenodo source)
  [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0  -- (+251 chars; full line in the Zenodo source)
  [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0  -- (+251 chars; full line in the Zenodo source)
  [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0  -- (+251 chars; full line in the Zenodo source)
  [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0  -- (+251 chars; full line in the Zenodo source)
  [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0  -- (+251 chars; full line in the Zenodo source)
  [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0  -- (+251 chars; full line in the Zenodo source)
  [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0  -- (+251 chars; full line in the Zenodo source)
  [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0  -- (+251 chars; full line in the Zenodo source)
  [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0  -- (+251 chars; full line in the Zenodo source)
  [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0  -- (+251 chars; full line in the Zenodo source)
  [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0  -- (+251 chars; full line in the Zenodo source)
  [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0  -- (+251 chars; full line in the Zenodo source)
  [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0  -- (+251 chars; full line in the Zenodo source)
  [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0  -- (+251 chars; full line in the Zenodo source)
  [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0  -- (+251 chars; full line in the Zenodo source)
  [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0  -- (+251 chars; full line in the Zenodo source)
  [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0  -- (+251 chars; full line in the Zenodo source)
  [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0  -- (+251 chars; full line in the Zenodo source)
  [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0  -- (+251 chars; full line in the Zenodo source)
  [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0  -- (+251 chars; full line in the Zenodo source)
  [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0  -- (+251 chars; full line in the Zenodo source)
  [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0  -- (+251 chars; full line in the Zenodo source)
  [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0  -- (+251 chars; full line in the Zenodo source)
  [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0  -- (+251 chars; full line in the Zenodo source)
  [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0  -- (+251 chars; full line in the Zenodo source)
  [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0  -- (+251 chars; full line in the Zenodo source)
  [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0  -- (+251 chars; full line in the Zenodo source)
  [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0  -- (+251 chars; full line in the Zenodo source)
  [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0  -- (+251 chars; full line in the Zenodo source)
  [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0  -- (+251 chars; full line in the Zenodo source)
  [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0  -- (+251 chars; full line in the Zenodo source)
  [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0  -- (+251 chars; full line in the Zenodo source)
  [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0  -- (+251 chars; full line in the Zenodo source)
  [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0  -- (+251 chars; full line in the Zenodo source)
  [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0  -- (+251 chars; full line in the Zenodo source)
  [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0  -- (+251 chars; full line in the Zenodo source)
  [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0  -- (+251 chars; full line in the Zenodo source)
  [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1  -- (+251 chars; full line in the Zenodo source)
  [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0  -- (+251 chars; full line in the Zenodo source)
  [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0  -- (+251 chars; full line in the Zenodo source)
  [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0  -- (+251 chars; full line in the Zenodo source)
  [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0  -- (+251 chars; full line in the Zenodo source)
  [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0  -- (+251 chars; full line in the Zenodo source)
  [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0  -- (+251 chars; full line in the Zenodo source)
  [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0  -- (+251 chars; full line in the Zenodo source)
  [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0  -- (+251 chars; full line in the Zenodo source)
  [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0  -- (+251 chars; full line in the Zenodo source)
  [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0  -- (+251 chars; full line in the Zenodo source)
  [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0  -- (+251 chars; full line in the Zenodo source)
  [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0  -- (+251 chars; full line in the Zenodo source)
  [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0  -- (+251 chars; full line in the Zenodo source)
  [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0  -- (+251 chars; full line in the Zenodo source)
  [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0  -- (+251 chars; full line in the Zenodo source)
  [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0  -- (+251 chars; full line in the Zenodo source)
  [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0  -- (+251 chars; full line in the Zenodo source)
  [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0  -- (+251 chars; full line in the Zenodo source)
  [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0  -- (+251 chars; full line in the Zenodo source)
  [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0  -- (+251 chars; full line in the Zenodo source)
  [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0  -- (+251 chars; full line in the Zenodo source)
  [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0  -- (+251 chars; full line in the Zenodo source)
  [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0  -- (+251 chars; full line in the Zenodo source)
  [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0  -- (+251 chars; full line in the Zenodo source)
  [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0  -- (+251 chars; full line in the Zenodo source)
  [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0  -- (+251 chars; full line in the Zenodo source)
  [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0  -- (+251 chars; full line in the Zenodo source)
  [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0  -- (+251 chars; full line in the Zenodo source)
  [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0  -- (+251 chars; full line in the Zenodo source)
  [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0  -- (+251 chars; full line in the Zenodo source)
  [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0  -- (+251 chars; full line in the Zenodo source)
  [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0  -- (+251 chars; full line in the Zenodo source)
  [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0  -- (+251 chars; full line in the Zenodo source)
  [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0  -- (+251 chars; full line in the Zenodo source)
  [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0  -- (+251 chars; full line in the Zenodo source)
  [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0  -- (+251 chars; full line in the Zenodo source)
  [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0  -- (+251 chars; full line in the Zenodo source)
  [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0  -- (+251 chars; full line in the Zenodo source)
  [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0  -- (+251 chars; full line in the Zenodo source)
  [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0  -- (+251 chars; full line in the Zenodo source)
  [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0  -- (+251 chars; full line in the Zenodo source)
  [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0  -- (+251 chars; full line in the Zenodo source)
  [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0  -- (+251 chars; full line in the Zenodo source)
  [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0  -- (+251 chars; full line in the Zenodo source)
  [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0  -- (+251 chars; full line in the Zenodo source)
  [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0  -- (+251 chars; full line in the Zenodo source)
  [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0  -- (+251 chars; full line in the Zenodo source)
  [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0  -- (+251 chars; full line in the Zenodo source)
  [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0  -- (+251 chars; full line in the Zenodo source)
  [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0  -- (+251 chars; full line in the Zenodo source)
  [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0  -- (+251 chars; full line in the Zenodo source)
  [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0  -- (+251 chars; full line in the Zenodo source)
  [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0  -- (+251 chars; full line in the Zenodo source)
  [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1  -- (+251 chars; full line in the Zenodo source)
  [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0  -- (+251 chars; full line in the Zenodo source)
  [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0  -- (+251 chars; full line in the Zenodo source)
  [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0  -- (+251 chars; full line in the Zenodo source)
  [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0  -- (+251 chars; full line in the Zenodo source)
  [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0  -- (+251 chars; full line in the Zenodo source)
  [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0  -- (+251 chars; full line in the Zenodo source)
  [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0  -- (+251 chars; full line in the Zenodo source)
  [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0  -- (+251 chars; full line in the Zenodo source)
  [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0  -- (+251 chars; full line in the Zenodo source)
  [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0  -- (+251 chars; full line in the Zenodo source)
  [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0  -- (+251 chars; full line in the Zenodo source)
  [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0  -- (+251 chars; full line in the Zenodo source)
  [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0  -- (+251 chars; full line in the Zenodo source)
  [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0  -- (+251 chars; full line in the Zenodo source)
  [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0  -- (+251 chars; full line in the Zenodo source)
  [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0  -- (+251 chars; full line in the Zenodo source)
  [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0  -- (+251 chars; full line in the Zenodo source)
  [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0  -- (+251 chars; full line in the Zenodo source)
  [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0  -- (+251 chars; full line in the Zenodo source)
  [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0  -- (+251 chars; full line in the Zenodo source)
  [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0  -- (+251 chars; full line in the Zenodo source)
  [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0  -- (+251 chars; full line in the Zenodo source)
  [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0  -- (+251 chars; full line in the Zenodo source)
  [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0  -- (+251 chars; full line in the Zenodo source)
  [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0  -- (+251 chars; full line in the Zenodo source)
  [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0  -- (+251 chars; full line in the Zenodo source)
  [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 1, 0, 0, 0, 0, 0  -- (+251 chars; full line in the Zenodo source)
  [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 1, 0, 0, 0, 0  -- (+251 chars; full line in the Zenodo source)
  [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0  -- (+251 chars; full line in the Zenodo source)
  [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0  -- (+251 chars; full line in the Zenodo source)
  [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 1, 0, 0  -- (+251 chars; full line in the Zenodo source)
  [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0  -- (+251 chars; full line in the Zenodo source)
  [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 1, 0  -- (+251 chars; full line in the Zenodo source)
  [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 1  -- (+251 chars; full line in the Zenodo source)
  [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0  -- (+251 chars; full line in the Zenodo source)
  [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1  -- (+251 chars; full line in the Zenodo source)
  [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1  -- (+251 chars; full line in the Zenodo source)
  [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0  -- (+251 chars; full line in the Zenodo source)
  [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0  -- (+251 chars; full line in the Zenodo source)
  [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0  -- (+251 chars; full line in the Zenodo source)
  [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0  -- (+251 chars; full line in the Zenodo source)
  [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0  -- (+251 chars; full line in the Zenodo source)
  [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0  -- (+251 chars; full line in the Zenodo source)
  [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0  -- (+251 chars; full line in the Zenodo source)
  [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0  -- (+251 chars; full line in the Zenodo source)
  [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0  -- (+251 chars; full line in the Zenodo source)
  [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0  -- (+251 chars; full line in the Zenodo source)
  [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0  -- (+251 chars; full line in the Zenodo source)
  [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0  -- (+251 chars; full line in the Zenodo source)
  [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0  -- (+251 chars; full line in the Zenodo source)
  [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0  -- (+251 chars; full line in the Zenodo source)
  [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0  -- (+251 chars; full line in the Zenodo source)
  [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0  -- (+251 chars; full line in the Zenodo source)
  [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0  -- (+251 chars; full line in the Zenodo source)
  [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0  -- (+251 chars; full line in the Zenodo source)
  [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0  -- (+251 chars; full line in the Zenodo source)
  [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0  -- (+251 chars; full line in the Zenodo source)
  [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0  -- (+251 chars; full line in the Zenodo source)
  [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0  -- (+251 chars; full line in the Zenodo source)
  [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0  -- (+251 chars; full line in the Zenodo source)
  [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0  -- (+251 chars; full line in the Zenodo source)
  [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0  -- (+251 chars; full line in the Zenodo source)
  [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0  -- (+251 chars; full line in the Zenodo source)
  [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0  -- (+251 chars; full line in the Zenodo source)
  [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0  -- (+251 chars; full line in the Zenodo source)
  [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0  -- (+251 chars; full line in the Zenodo source)
  [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0  -- (+251 chars; full line in the Zenodo source)
  [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0  -- (+251 chars; full line in the Zenodo source)
  [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0  -- (+251 chars; full line in the Zenodo source)
  [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0  -- (+251 chars; full line in the Zenodo source)
  [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0  -- (+251 chars; full line in the Zenodo source)
  [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0  -- (+251 chars; full line in the Zenodo source)
  [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0  -- (+251 chars; full line in the Zenodo source)
  [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0  -- (+251 chars; full line in the Zenodo source)
  [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0  -- (+251 chars; full line in the Zenodo source)
  [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0  -- (+251 chars; full line in the Zenodo source)
  [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0  -- (+251 chars; full line in the Zenodo source)
  [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0  -- (+251 chars; full line in the Zenodo source)
  [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0  -- (+251 chars; full line in the Zenodo source)
  [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0  -- (+251 chars; full line in the Zenodo source)
  [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0  -- (+251 chars; full line in the Zenodo source)
  [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0  -- (+251 chars; full line in the Zenodo source)
  [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0  -- (+251 chars; full line in the Zenodo source)
  [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0  -- (+251 chars; full line in the Zenodo source)
  [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0  -- (+251 chars; full line in the Zenodo source)
  [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0  -- (+251 chars; full line in the Zenodo source)
  [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0  -- (+251 chars; full line in the Zenodo source)
  [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0  -- (+251 chars; full line in the Zenodo source)
  [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0  -- (+251 chars; full line in the Zenodo source)
  [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0  -- (+251 chars; full line in the Zenodo source)
  [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0  -- (+251 chars; full line in the Zenodo source)
  [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0  -- (+251 chars; full line in the Zenodo source)
  [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0  -- (+251 chars; full line in the Zenodo source)
  [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0  -- (+251 chars; full line in the Zenodo source)
  [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0  -- (+251 chars; full line in the Zenodo source)
  [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0  -- (+251 chars; full line in the Zenodo source)

def F4 : List (List Nat) := [
  [0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0  -- (+251 chars; full line in the Zenodo source)
  [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0  -- (+251 chars; full line in the Zenodo source)
  [0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0  -- (+251 chars; full line in the Zenodo source)
  [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0  -- (+251 chars; full line in the Zenodo source)

theorem chk_len : allLenB 216 P4 = true := rfl
theorem chk_W : W4.length = 216 := rfl
theorem chk_deg : allLeB 6 (degV P4) = true := rfl
theorem chk_q : allQB 4 P4 = true := rfl
theorem chk_odd : oddB W4 F4 = true := rfl
theorem chk_one : oneB F4 P4 = true := rfl
theorem chk_F : F4.length = 4 := rfl

theorem box4_min : ∀ C : List Bool, P4.length = C.length → evenAll (selS W4 P4 C) = true → 4 ≤ cnt C := by
  intro C hl he
  have := min_area W4 P4 F4 (oddB_ok W4 F4 chk_odd) (oneB_ok F4 P4 chk_one) C hl he
  rw [chk_F] at this; exact this

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

/-- **⟨W⟩ ≤ Σ_{A ≥ 4} 2^(6(8+4A)+1) t^A for the 2×2 loop in the 3⁴ box, every t ≥ 0.** -/
theorem box4_area_law (h1 : O.le K.zero K.one) (t : R) (ht : O.le K.zero t) :
    O.le (N K t 216 W4 P4)
      (K.mul (sumK K (fun A => nsm K (2 ^ (6 * (nz W4 + 4 * A) + 1)) (gA K 4 t A)) (rangeL P4.length)) (N K t 216 (zl 216) P4)) :=
  wilson_area_law K O h1 t ht 216 W4 chk_W P4 (allLenB_ok 216 P4 chk_len) 6 4 4
    (allLeB_ok 6 _ chk_deg) (allQB_ok 4 P4 chk_q) box4_min

end Main

end Box4Area

#print axioms Box4Area.chk_len
#print axioms Box4Area.chk_W
#print axioms Box4Area.chk_deg
#print axioms Box4Area.chk_q
#print axioms Box4Area.chk_odd
#print axioms Box4Area.chk_one
#print axioms Box4Area.chk_F
#print axioms Box4Area.box4_min
#print axioms Box4Area.box4_area_law

ym/BoxCluster.lean

lean_all/ym/BoxCluster.lean

/-
  ym/BoxCluster — T27, cycle 92: **exponential clustering on the full hypercubic box** (ℤ₂, strong coupling).
  Box n^(k+2), d ≥ 2 directions, all box plaquettes PB, D = 2(d−1), s = 2^(4D)·t with 2s ≤ 1, 0 ≤ t ≤ 1; for box plaquettes
  p = (v, μ, ν), q = (v', μ', ν') with v₀ + 1 + m ≤ v'₀:
      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^(8(d−1)) t)^m — exponential decay in the separation, uniformly in n.
    c0_plL        the links of a plaquette have first coordinate v₀ or v₀ + 1
    lip_box       box plaquettes are Lipschitz for the height hgt
    box_cluster   the bound
  Lean 4 core only, proofs by hand, no axioms.
-/
import ym.BoxHeight

namespace BoxCluster

open YMChain LinkIntegral GKS StrongZ2 LatEnc LatList PlaqCert CertIdx LoopDot PlaqDot AreaRT BoxPlaq BoxDeg PlaqSize DegIdx
  HeightCert BoxHeight Cluster CorrBound AnimalDeg AnimalSupp AnimalArea AreaExp S2cFinal AnimalSum

theorem c0_shift (v : List Nat) (hv : v ≠ []) : ∀ m, c0 v ≤ c0 (shiftC v m) ∧ c0 (shiftC v m) ≤ c0 v + 1
  | 0 => by rw [c0_s0 v hv]; exact ⟨Nat.le_succ _, Nat.le_refl _⟩
  | m + 1 => by rw [c0_s1 m v]; exact ⟨Nat.le_refl _, Nat.le_succ _⟩

theorem c0_plL (v : List Nat) (hv : v ≠ []) (μ ν : Nat) (ℓ : List Nat × Nat) (h : memL ℓ (plL v μ ν)) :
    c0 v ≤ c0 ℓ.1 ∧ c0 ℓ.1 ≤ c0 v + 1 := by
  rcases h with e | e | e | e | e
  · rw [e]; exact ⟨Nat.le_refl _, Nat.le_succ _⟩
  · rw [e]; exact c0_shift v hv ν
  · rw [e]; exact ⟨Nat.le_refl _, Nat.le_succ _⟩
  · rw [e]; exact c0_shift v hv μ
  · exact absurd e id

theorem hgt_plaq (n d k : Nat) (v : List Nat) (μ ν : Nat) (hP : validP n d k v μ ν) (i : Nat)
    (hi : gt (plV n d k v μ ν) i ≠ 0) : c0 v ≤ hgt n d k i ∧ hgt n d k i ≤ c0 v + 1 := by
  obtain ⟨ℓ, hℓ, e⟩ := witness n d k v μ ν i hi
  have hv : v ≠ [] := fun c => by rw [c] at hP; exact nomatch hP.1
  rw [← e, hgt_enc n d k ℓ (plL_valid n d k v μ ν hP ℓ hℓ)]
  exact c0_plL v hv μ ν ℓ hℓ

theorem lip_box (n d k : Nat) : ∀ P : List (List Nat), (∀ q, memL q P → isPlaq n d k q) → lipAll (hgt n d k) P
  | [], _ => trivial
  | q :: P, h => by
    refine ⟨?_, lip_box n d k P (fun q' hq' => h q' (Or.inr hq'))⟩
    obtain ⟨v, μ, ν, hv, e⟩ := h q (Or.inl rfl)
    intro i j hi hj
    rw [e] at hi hj
    have a := hgt_plaq n d k v μ ν hv i hi
    have b := hgt_plaq n d k v μ ν hv j hj
    exact Nat.le_trans b.2 (Nat.succ_le_succ a.1)

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

/-- **Exponential clustering on the full box.** -/
theorem box_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)
    (n d k D m : Nat) (hd : 1 ≤ d) (hD : D + 2 = d + d)
    (v : List Nat) (μ ν : Nat) (hp : validP n d k v μ ν) (v' : List Nat) (μ' ν' : Nat) (hq : validP n d k v' μ' ν')
    (hsep : c0 v + 1 + m ≤ c0 v')
    (hs : O.le (K.add (K.mul (nsm K (2 ^ (D * 4)) K.one) t) (K.mul (nsm K (2 ^ (D * 4)) K.one) t)) K.one) :
    O.le (K.mul (G K t (zadd (plV n d k v μ ν) (plV n d k v' μ' ν')) (PB n d k)) (G K t (zl (Lt n d k)) (PB n d k)))
      (K.add (K.mul (G K t (plV n d k v μ ν) (PB n d k)) (G K t (plV n d k v' μ' ν') (PB n d k)))
        (K.mul (K.mul (G K t (zl (Lt n d k)) (PB n d k)) (G K t (zl (Lt n d k)) (PB n d k)))
          (K.mul (nsm K (2 ^ (D * nz (plV n d k v μ ν) + 1)) K.one)
            (K.add (pwR K (K.mul (nsm K (2 ^ (D * 4)) K.one) t) m) (pwR K (K.mul (nsm K (2 ^ (D * 4)) K.one) t) m))))) := by
  have hPl : ∀ r, memL r (PB n d k) → r.length = Lt n d k := fun r hr => by
    obtain ⟨_, _, _, _, e⟩ := PB_plaq n d k r hr
    rw [e]; exact len_indV _ _ _
  refine cluster K O h1 h10 t ht0 ht1 (Lt n d k) _ _ (len_indV _ _ _) (PB n d k) hPl D 4 m
    (box_degV n d k D hd hD) (allQ_box n d k _ (PB_plaq n d k)) hs ?_
  exact height_dist (hgt n d k) (PB n d k) _ _ (c0 v + 1) m (lip_box n d k _ (PB_plaq n d k))
    (fun i hi => (hgt_plaq n d k v μ ν hp i hi).2)
    (fun i hi => Nat.le_trans hsep (hgt_plaq n d k v' μ' ν' hq i hi).1)

end Main

end BoxCluster

#print axioms BoxCluster.c0_shift
#print axioms BoxCluster.c0_plL
#print axioms BoxCluster.hgt_plaq
#print axioms BoxCluster.lip_box
#print axioms BoxCluster.box_cluster

ym/BoxDeg.lean

lean_all/ym/BoxDeg.lean

/-
  ym/BoxDeg — T27, cycle 71 (link degree, step 6): every link of the box lies in at most 2(d − 1) plaquettes of PB.
    cntQ_eq, cnt_bindP, cntL_filt, cntL_pos, sumP_mono_mem, sumP_double     list counting
    hit_of_mem     (u, λ) ∈ plL v μ ν ⇒ hitB u λ μ ν v
    box_deg        cntQ i (PB n d k) + 2 ≤ d + d        (d ≥ 1)
    box_degV       allLe (degV (PB n d k)) D whenever D + 2 = d + d
  Lean 4 core only, proofs by hand, no axioms.
-/
import ym.BoxPlaq

namespace BoxDeg

open YMChain LatEnc LatList PlaqCert LoopCert CertIdx LoopDot PlaqDot AreaRT SparseDot DegIdx PairSum BoxPlaq VertCnt PlaqThrough
  S2cFinal AnimalSum AnimalDeg

theorem cntQ_eq (i : Nat) : ∀ P : List (List Nat), cntQ i P = cntL (fun q => !(gt q i == 0)) P
  | [] => rfl
  | q :: P => by
    show (if gt q i = 0 then 0 else 1) + cntQ i P = (if (!(gt q i == 0)) = true then 1 else 0) + cntL _ P
    rw [cntQ_eq i P]
    by_cases h : gt q i = 0
    · rw [FiniteRecChain.ite_pos' _ _ h, h]; rfl
    · rw [FiniteRecChain.ite_neg' _ _ h,
        show (gt q i == 0) = false from Bool.of_not_eq_true (fun e => h (Box3Area.beq_nat _ _ e))]; rfl

theorem cnt_bindP {A : Type} (p : A → Bool) (F : Nat × Nat → List A) : ∀ l : List (Nat × Nat),
    cntL p (bindL l F) = sumP (fun x => cntL p (F x)) l
  | [] => rfl
  | x :: l => by
    show cntL p (F x ++ bindL l F) = cntL p (F x) + sumP (fun x => cntL p (F x)) l
    rw [cntL_append, cnt_bindP p F l]

theorem cntL_filt {A : Type} (p f : A → Bool) : ∀ l : List A, cntL p (filt f l) ≤ cntL p l
  | [] => Nat.le_refl 0
  | a :: l => by
    show cntL p (if f a = true then a :: filt f l else filt f l) ≤ (if p a = true then 1 else 0) + cntL p l
    by_cases h : f a = true
    · rw [FiniteRecChain.ite_pos' _ _ h]; exact Nat.add_le_add_left (cntL_filt p f l) _
    · rw [FiniteRecChain.ite_neg' _ _ h]; exact Nat.le_trans (cntL_filt p f l) (Nat.le_add_left _ _)

theorem cntL_pos {A : Type} (p : A → Bool) : ∀ l : List A, cntL p l ≠ 0 → ∃ a, memL a l ∧ p a = true
  | [], h => absurd rfl h
  | a :: l, h => by
    by_cases hp : p a = true
    · exact ⟨a, Or.inl rfl, hp⟩
    · have h' : (if p a = true then 1 else 0) + cntL p l ≠ 0 := h
      rw [FiniteRecChain.ite_neg' _ _ hp, Nat.zero_add] at h'
      obtain ⟨b, hb, pb⟩ := cntL_pos p l h'
      exact ⟨b, Or.inr hb, pb⟩

theorem sumP_mono_mem (f g : Nat × Nat → Nat) : ∀ l, (∀ x, memL x l → f x ≤ g x) → sumP f l ≤ sumP g l
  | [], _ => Nat.le_refl 0
  | x :: l, h => Nat.add_le_add (h x (Or.inl rfl)) (sumP_mono_mem f g l (fun y hy => h y (Or.inr hy)))

theorem sumP_double (lam : Nat) : ∀ l : List (Nat × Nat),
    sumP (fun p => (ib (p.1 == lam) + ib (p.1 == lam)) + (ib (p.2 == lam) + ib (p.2 == lam))) l = sumP (g lam) l + sumP (g lam) l
  | [] => rfl
  | x :: l => by
    show (ib (x.1 == lam) + ib (x.1 == lam)) + (ib (x.2 == lam) + ib (x.2 == lam)) + sumP _ l =
      (ib (x.1 == lam) + ib (x.2 == lam) + sumP (g lam) l) + (ib (x.1 == lam) + ib (x.2 == lam) + sumP (g lam) l)
    rw [sumP_double lam l]
    generalize ib (x.1 == lam) = a; generalize ib (x.2 == lam) = b; generalize sumP (g lam) l = c
    lib_nat

theorem or_l (a b : Bool) (h : a = true) : (a || b) = true := by rw [h]; rfl
theorem or_r (a b : Bool) (h : b = true) : (a || b) = true := by rw [h]; cases a <;> rfl
theorem and_t (a b : Bool) (ha : a = true) (hb : b = true) : (a && b) = true := by rw [ha, hb]; rfl

theorem hit_of_mem (u : List Nat) (lam μ ν : Nat) (v : List Nat) (h : memL (u, lam) (plL v μ ν)) : hitB u lam μ ν v = true := by
  have b1 : ∀ (w : List Nat) (a : Nat), (w, a) = (u, lam) → lbeq w u = true ∧ (a == lam) = true := fun w a e => by
    have e1 : w = u := congrArg Prod.fst e
    have e2 : a = lam := congrArg Prod.snd e
    rw [e1, e2]; exact ⟨lbeq_refl u, beq_self lam⟩
  rcases h with e | e | e | e | e
  · obtain ⟨x, y⟩ := b1 v μ e.symm
    exact or_l _ _ (or_l _ _ (and_t _ _ x y))
  · obtain ⟨x, y⟩ := b1 (shiftC v ν) μ e.symm
    exact or_l _ _ (or_r _ _ (and_t _ _ x y))
  · obtain ⟨x, y⟩ := b1 v ν e.symm
    exact or_r _ _ (or_l _ _ (and_t _ _ x y))
  · obtain ⟨x, y⟩ := b1 (shiftC v μ) ν e.symm
    exact or_r _ _ (or_r _ _ (and_t _ _ x y))
  · exact absurd e id

theorem pair_eq (a b : List Nat × Nat) (h1 : a.1 = b.1) (h2 : a.2 = b.2) : a = b := by
  cases a with
  | mk a1 a2 =>
    cases b with
    | mk b1 b2 =>
      have e1 : a1 = b1 := h1
      have e2 : a2 = b2 := h2
      rw [e1, e2]

/-- **Every link lies in at most 2(d − 1) plaquettes of the canonical box list.** -/
theorem box_deg (n d k : Nat) (hd : 1 ≤ d) (i : Nat) : cntQ i (PB n d k) + 2 ≤ d + d := by
  by_cases h0 : cntQ i (PB n d k) = 0
  · rw [h0]; have h11 : 1 + 1 ≤ d + d := Nat.add_le_add hd hd; exact h11
  · rw [cntQ_eq] at h0 ⊢
    obtain ⟨q0, hq0, hz⟩ := cntL_pos _ _ h0
    obtain ⟨v0, μ0, ν0, hv0, e0⟩ := PB_plaq n d k q0 hq0
    have hnz : gt (plV n d k v0 μ0 ν0) i ≠ 0 := by
      rw [← e0]; intro c; rw [c] at hz; exact absurd hz (fun c => Bool.noConfusion c)
    obtain ⟨ℓ0, hℓ0, eℓ0⟩ := witness n d k v0 μ0 ν0 i hnz
    have vℓ0 := plL_valid n d k v0 μ0 ν0 hv0 ℓ0 hℓ0
    obtain ⟨u, lam⟩ := ℓ0
    have hlam : lam + 1 ≤ d := vℓ0.2.2
    show cntL _ (bindL (prs d) (fun p => (inV n k p.1 p.2).map (fun v => plV n d k v p.1 p.2))) + 2 ≤ d + d
    rw [cnt_bindP]
    have step : ∀ p, memL p (prs d) →
        cntL (fun q => !(gt q i == 0)) ((inV n k p.1 p.2).map (fun v => plV n d k v p.1 p.2)) ≤
          (ib (p.1 == lam) + ib (p.1 == lam)) + (ib (p.2 == lam) + ib (p.2 == lam)) := by
      intro p hp
      rw [cntL_map]
      refine Nat.le_trans (cntL_mono_mem _ (hitB u lam p.1 p.2) _ ?_) ?_
      · intro v hv hq
        have hf := mem_filt_inv _ v _ hv
        have vv := verts_valid n (k + 2) v hf.1
        have hr := mem_prs d p hp
        have hvP : validP n d k v p.1 p.2 := ⟨vv.1, vv.2, allLtB_ok n _ (AnimalTouch.and_l _ _ hf.2),
          allLtB_ok n _ (AnimalTouch.and_r _ _ hf.2), hr.1, hr.2⟩
        have hnz' : gt (plV n d k v p.1 p.2) i ≠ 0 := by
          intro c
          have : (!(gt (plV n d k v p.1 p.2) i == 0)) = true := hq
          rw [c] at this; exact absurd this (fun c => Bool.noConfusion c)
        obtain ⟨ℓ, hℓ, eℓ⟩ := witness n d k v p.1 p.2 i hnz'
        have vℓ := plL_valid n d k v p.1 p.2 hvP ℓ hℓ
        obtain ⟨a1, a2⟩ := encL_inj n d ℓ.1 u ℓ.2 lam (vℓ.1.trans vℓ0.1.symm) vℓ.2.1 vℓ0.2.1 vℓ.2.2 vℓ0.2.2
          (eℓ.trans eℓ0.symm)
        have : ℓ = (u, lam) := pair_eq ℓ (u, lam) a1 a2
        rw [this] at hℓ
        exact hit_of_mem u lam p.1 p.2 v hℓ
      · exact through_le u lam p.1 p.2 _ (fun w => Nat.le_trans (cntL_filt _ _ _) (cnt_verts n (k + 2) w))
    have hs := sumP_mono_mem _ _ (prs d) step
    rw [sumP_double] at hs
    have pc := (pair_cnt lam d).1 hlam
    have : sumP (g lam) (prs d) + sumP (g lam) (prs d) + 2 = d + d := by
      generalize sumP (g lam) (prs d) = X at pc ⊢
      rw [← pc]; lib_nat
    rw [← this]
    exact Nat.add_le_add_right hs 2

/-- **The canonical box list satisfies the degree hypothesis with D = 2(d − 1).** -/
theorem box_degV (n d k D : Nat) (hd : 1 ≤ d) (hD : D + 2 = d + d) : allLe (degV (PB n d k)) D :=
  deg_of_cnt _ D (fun i => AnimalSupp.le_cancel _ D 2 (by rw [hD]; exact box_deg n d k hd i))

end BoxDeg

#print axioms BoxDeg.cntQ_eq
#print axioms BoxDeg.cnt_bindP
#print axioms BoxDeg.cntL_filt
#print axioms BoxDeg.cntL_pos
#print axioms BoxDeg.sumP_mono_mem
#print axioms BoxDeg.sumP_double
#print axioms BoxDeg.or_l
#print axioms BoxDeg.or_r
#print axioms BoxDeg.and_t
#print axioms BoxDeg.hit_of_mem
#print axioms BoxDeg.pair_eq
#print axioms BoxDeg.box_deg
#print axioms BoxDeg.box_degV

ym/BoxHeight.lean

lean_all/ym/BoxHeight.lean

/-
  ym/BoxHeight — T27, cycle 91: the first coordinate as a height on the link indices of the box.
    allL n d k          all links (v, μ) of the box n^(k+2), μ < d
    hgt n d k i         Σ_{ℓ ∈ allL} [index ℓ = i] · v₀(ℓ)
    cnt_allL            each valid link occurs once in allL
    hgt_enc             hgt (index ℓ) = v₀(ℓ) for a valid link ℓ
  Lean 4 core only, proofs by hand, no axioms.
-/
import ym.HeightCert

namespace BoxHeight

open YMChain LatEnc LatList PlaqCert LoopCert CertIdx PlaqDot VertCnt PairSum FlatBd FlatPE S2cFinal AnimalSum

def allL (n d k : Nat) : List (List Nat × Nat) := bindL (verts n (k + 2)) (fun v => (upto d).map (fun μ => (v, μ)))

def sumLk (f : List Nat × Nat → Nat) : List (List Nat × Nat) → Nat
  | [] => 0
  | x :: l => f x + sumLk f l

def hgt (n d k i : Nat) : Nat := sumLk (fun ℓ => if (enc n d ℓ == i) = true then c0 ℓ.1 else 0) (allL n d k)

theorem cnt_bindV {A : Type} (p : A → Bool) (F : List Nat → List A) : ∀ l : List (List Nat),
    cntL p (bindL l F) = sumV (fun v => cntL p (F v)) l
  | [] => rfl
  | v :: l => by
    show cntL p (F v ++ bindL l F) = cntL p (F v) + sumV (fun v => cntL p (F v)) l
    rw [cntL_append, cnt_bindV p F l]

theorem cnt_allL (n d k : Nat) (ℓ : List Nat × Nat) (hℓ : validL n d (k + 2) ℓ) : cntL (fun m => lpeq m ℓ) (allL n d k) = 1 := by
  show cntL _ (bindL (verts n (k + 2)) (fun v => (upto d).map (fun μ => (v, μ)))) = 1
  rw [cnt_bindV]
  have inner : ∀ v, cntL (fun m => lpeq m ℓ) ((upto d).map (fun μ => (v, μ))) = ib (lbeq v ℓ.1) := by
    intro v
    rw [cntL_map]
    show cntL (fun μ => lbeq v ℓ.1 && (μ == ℓ.2)) (upto d) = ib (lbeq v ℓ.1)
    cases lbeq v ℓ.1
    · exact cntL_false _ (fun _ => rfl) _
    · rw [cnt_line.cntL_ext_mem' (fun μ => true && (μ == ℓ.2)) (fun μ => μ == ℓ.2) (upto d) (fun μ => by cases (μ == ℓ.2) <;> rfl),
        cnt_upto_eq ℓ.2 d hℓ.2.2]; rfl
  rw [sumV_ext _ _ inner, sumV_ib]
  have hm := mem_verts n (k + 2) ℓ.1 hℓ.1 hℓ.2.1
  exact Nat.le_antisymm (cnt_verts n (k + 2) ℓ.1) (FlatCnt.cnt_pos_of_mem (fun v => lbeq v ℓ.1) ℓ.1 (lbeq_refl _) _ hm)

theorem allL_valid (n d k : Nat) (m : List Nat × Nat) (hm : memL m (allL n d k)) : validL n d (k + 2) m := by
  obtain ⟨v, hv, hm'⟩ := mem_bindL_inv _ m _ hm
  obtain ⟨μ, hμ, e⟩ := mem_map_inv _ m _ hm'
  rw [← e]
  have r := verts_valid n (k + 2) v hv
  exact ⟨r.1, r.2, upto_lt d μ hμ⟩

theorem sumLk_eq (f : List Nat × Nat → Nat) (ℓ : List Nat × Nat) (c : Nat) :
    ∀ l : List (List Nat × Nat), (∀ m, memL m l → f m = if lpeq m ℓ = true then c else 0) → sumLk f l = c * cntL (fun m => lpeq m ℓ) l
  | [], _ => (Nat.mul_zero c).symm
  | m :: l, h => by
    show f m + sumLk f l = c * ((if lpeq m ℓ = true then 1 else 0) + cntL _ l)
    rw [h m (Or.inl rfl), sumLk_eq f ℓ c l (fun x hx => h x (Or.inr hx))]
    cases lpeq m ℓ
    · show 0 + c * cntL _ l = c * (0 + cntL _ l); rw [Nat.zero_add, Nat.zero_add]
    · show c + c * cntL _ l = c * (1 + cntL _ l); lib_nat

/-- **The height of the index of a valid link is the first coordinate of its vertex.** -/
theorem hgt_enc (n d k : Nat) (ℓ : List Nat × Nat) (hℓ : validL n d (k + 2) ℓ) : hgt n d k (enc n d ℓ) = c0 ℓ.1 := by
  show sumLk _ (allL n d k) = c0 ℓ.1
  rw [sumLk_eq _ ℓ (c0 ℓ.1) _ (fun m hm => by
    rw [conv n d k m ℓ (allL_valid n d k m hm) hℓ]
    cases hl : lpeq m ℓ
    · rfl
    · show c0 m.1 = c0 ℓ.1
      rw [lbeq_eq _ _ (AnimalTouch.and_l _ _ hl)]),
    cnt_allL n d k ℓ hℓ, Nat.mul_one]

end BoxHeight

#print axioms BoxHeight.allL
#print axioms BoxHeight.sumLk
#print axioms BoxHeight.hgt
#print axioms BoxHeight.cnt_bindV
#print axioms BoxHeight.cnt_allL
#print axioms BoxHeight.allL_valid
#print axioms BoxHeight.sumLk_eq
#print axioms BoxHeight.hgt_enc

ym/BoxPath.lean

lean_all/ym/BoxPath.lean

/-
  ym/BoxPath — T45, G17d-4: touching paths inside the box. shN keeps the length and adds k to coordinate c; a line
  of plaquettes in direction c starting in the box and ending inside it lies in boxP, its shared links in boxL, so
  lineB_path applies with all membership hypotheses discharged. Lean 4 core only, no axioms.
-/
import ym.LinePath
import ym.Cube

namespace BoxPath

open YMChain Closure Factor Degree ZdLinks ZdDegree PlaqPath LinePath Cube

theorem len_shN (x : List Nat) (c : Nat) : ∀ k, (shN x c k).length = x.length
  | 0 => rfl
  | k + 1 => by show (shf (shN x c k) c).length = _; rw [Cube.len_shf, len_shN x c k]

theorem nthN_shN (x : List Nat) (c : Nat) (hc : c < x.length) : ∀ k, nthN (shN x c k) c = nthN x c + k
  | 0 => rfl
  | k + 1 => by
    show nthN (shf (shN x c k) c) c = nthN x c + (k + 1)
    rw [nthN_shf_self _ c (by rw [len_shN]; exact hc), nthN_shN x c hc k]; rfl

theorem allLt_shN (n : Nat) (x : List Nat) (c : Nat) (hc : c < x.length) (hx : allLt n x) :
    ∀ k, nthN x c + k < n → allLt n (shN x c k)
  | 0, _ => hx
  | k + 1, h => by
    have ih := allLt_shN n x c hc hx k (Nat.lt_of_succ_lt h)
    show allLt n (shf (shN x c k) c)
    apply allLt_shf n _ c ih
    rw [nthN_shN x c hc k]; exact h

/-- **A line in direction c inside the box is a touching path.** -/
theorem lineB_box (n d : Nat) (x : List Nat) (a c k : Nat) (hx : x.length = d) (hlt : allLt n x) (hac : a < c)
    (hc : c < d) (hk : nthN x c + k < n) :
    pathP (boxL n d) (boxP n d) (x, a, c) (lineB x a c k) ∧ lastP (x, a, c) (lineB x a c k) = (shN x c k, a, c) := by
  apply lineB_path
  intro i hi
  have hcx : c < x.length := by rw [hx]; exact hc
  have hl : allLt n (shN x c (i + 1)) := allLt_shN n x c hcx hlt (i + 1) (Nat.lt_of_le_of_lt (Nat.add_le_add_left hi _) hk)
  have hlen : (shN x c (i + 1)).length = d := by rw [len_shN, hx]
  exact ⟨mem_boxP n d _ a c hlen hl hac hc,
    mem_boxL n d _ a hlen (allLt_mono n (n + 1) (Nat.le_succ n) _ hl) (Nat.lt_trans hac hc)⟩

/-- **Move by k in a third direction c > a inside the box** (start (x, a, b) in the box). -/
theorem moveC_box (n d : Nat) (x : List Nat) (a b c k : Nat) (hx : x.length = d) (hlt : allLt n x)
    (hac : a < c) (hc : c < d) (hab : a < b) (hb : b < d) (hk : nthN x c + k < n) :
    pathP (boxL n d) (boxP n d) (x, a, b) (moveC x a b c k) ∧ lastP (x, a, b) (moveC x a b c k) = (shN x c k, a, b) ∧
      (moveC x a b c k).length = k + 2 := by
  have hcx : c < x.length := by rw [hx]; exact hc
  have hlk : allLt n (shN x c k) := allLt_shN n x c hcx hlt k hk
  have hlenk : (shN x c k).length = d := by rw [len_shN, hx]
  have had : a < d := Nat.lt_trans hac hc
  apply moveC_path
  · exact mem_boxP n d x a c hx hlt hac hc
  · exact mem_boxL n d x a hx (allLt_mono n (n + 1) (Nat.le_succ n) _ hlt) had
  · intro i hi
    have hl : allLt n (shN x c (i + 1)) := allLt_shN n x c hcx hlt (i + 1) (Nat.lt_of_le_of_lt (Nat.add_le_add_left hi _) hk)
    have hlen : (shN x c (i + 1)).length = d := by rw [len_shN, hx]
    exact ⟨mem_boxP n d _ a c hlen hl hac hc, mem_boxL n d _ a hlen (allLt_mono n (n + 1) (Nat.le_succ n) _ hl) had⟩
  · exact mem_boxP n d _ a b hlenk hlk hab hb
  · exact mem_boxL n d _ a hlenk (allLt_mono n (n + 1) (Nat.le_succ n) _ hlk) had

def moveC' (x : List Nat) (a b c k : Nat) : List Pq := [(x, c, a)] ++ lineA x c a k ++ [(shN x c k, a, b)]

/-- (x, a, b) and (x, c, a) share the link (x, a) -/
theorem touch_ca (LL : List Lk) (x : List Nat) (a b c : Nat) (hm : memL (x, a) LL) :
    touch (vec LL (x, c, a)) (vec LL (x, a, b)) = true :=
  TouchLink.touch_vec LL _ _ _ hm (NeighborTouch.linkIn_4 x c a) (NeighborTouch.linkIn_1 x a b)

theorem touch_ac (LL : List Lk) (x : List Nat) (a b c : Nat) (hm : memL (x, a) LL) :
    touch (vec LL (x, a, b)) (vec LL (x, c, a)) = true := by
  rw [touch_symm]; exact touch_ca LL x a b c hm

/-- **Move by k in a direction c < a inside the box.** -/
theorem moveC'_box (n d : Nat) (x : List Nat) (a b c k : Nat) (hx : x.length = d) (hlt : allLt n x)
    (hca : c < a) (had : a < d) (hab : a < b) (hb : b < d) (hk : nthN x c + k < n) :
    pathP (boxL n d) (boxP n d) (x, a, b) (moveC' x a b c k) ∧ lastP (x, a, b) (moveC' x a b c k) = (shN x c k, a, b) ∧
      (moveC' x a b c k).length = k + 2 := by
  have hcd : c < d := Nat.lt_trans hca had
  have hcx : c < x.length := by rw [hx]; exact hcd
  have hlk : allLt n (shN x c k) := allLt_shN n x c hcx hlt k hk
  have hlenk : (shN x c k).length = d := by rw [len_shN, hx]
  have m1 : memL (x, a) (boxL n d) := mem_boxL n d x a hx (allLt_mono n (n + 1) (Nat.le_succ n) _ hlt) had
  have p1 : pathP (boxL n d) (boxP n d) (x, a, b) [(x, c, a)] := ⟨mem_boxP n d x c a hx hlt hca had, touch_ca _ x a b c m1, trivial⟩
  have p2 := lineA_path (boxL n d) (boxP n d) c a k x (fun i hi => by
    have hl : allLt n (shN x c (i + 1)) := allLt_shN n x c hcx hlt (i + 1) (Nat.lt_of_le_of_lt (Nat.add_le_add_left hi _) hk)
    have hlen : (shN x c (i + 1)).length = d := by rw [len_shN, hx]
    exact ⟨mem_boxP n d _ c a hlen hl hca had, mem_boxL n d _ a hlen (allLt_mono n (n + 1) (Nat.le_succ n) _ hl) had⟩)
  have m3 : memL (shN x c k, a) (boxL n d) := mem_boxL n d _ a hlenk (allLt_mono n (n + 1) (Nat.le_succ n) _ hlk) had
  have p3 : pathP (boxL n d) (boxP n d) (shN x c k, c, a) [(shN x c k, a, b)] :=
    ⟨mem_boxP n d _ a b hlenk hlk hab hb, touch_ac _ _ a b c m3, trivial⟩
  have l12 : lastP (x, a, b) ([(x, c, a)] ++ lineA x c a k) = (shN x c k, c, a) := by
    rw [lastP_append]; exact p2.2
  refine ⟨?_, ?_, ?_⟩
  · show pathP _ _ (x, a, b) (([(x, c, a)] ++ lineA x c a k) ++ [(shN x c k, a, b)])
    apply pathP_append
    · exact pathP_append _ _ _ _ _ p1 p2.1
    · rw [l12]; exact p3
  · show lastP (x, a, b) (([(x, c, a)] ++ lineA x c a k) ++ [(shN x c k, a, b)]) = _
    rw [lastP_append, l12]; rfl
  · show (([(x, c, a)] ++ lineA x c a k) ++ [(shN x c k, a, b)]).length = k + 2
    rw [len_append, len_append, len_lineA]
    show 1 + k + 1 = k + 2
    rw [Nat.add_comm 1 k]

/-- **A line in the plaquette's own first direction a inside the box.** -/
theorem lineA_box (n d : Nat) (x : List Nat) (a b k : Nat) (hx : x.length = d) (hlt : allLt n x) (hab : a < b)
    (hb : b < d) (hk : nthN x a + k < n) :
    pathP (boxL n d) (boxP n d) (x, a, b) (lineA x a b k) ∧ lastP (x, a, b) (lineA x a b k) = (shN x a k, a, b) := by
  apply lineA_path
  intro i hi
  have hax : a < x.length := by rw [hx]; exact Nat.lt_trans hab hb
  have hl : allLt n (shN x a (i + 1)) := allLt_shN n x a hax hlt (i + 1) (Nat.lt_of_le_of_lt (Nat.add_le_add_left hi _) hk)
  have hlen : (shN x a (i + 1)).length = d := by rw [len_shN, hx]
  exact ⟨mem_boxP n d _ a b hlen hl hab hb, mem_boxL n d _ b hlen (allLt_mono n (n + 1) (Nat.le_succ n) _ hl) hb⟩

end BoxPath

#print axioms BoxPath.nthN_shN
#print axioms BoxPath.allLt_shN
#print axioms BoxPath.lineB_box
#print axioms BoxPath.moveC_box
#print axioms BoxPath.moveC'_box
#print axioms BoxPath.lineA_box

ym/BoxPlaq.lean

lean_all/ym/BoxPlaq.lean

/-
  ym/BoxPlaq — T27, cycle 70 (link degree, step 5): the canonical plaquette list of the box n^(k+2).
    allLtB / allLtB_ok     Boolean coordinate bound
    inV n k μ ν            vertices v with v + e_μ, v + e_ν inside the box
    PB n d k               ⋃_{μ<ν<d} {plV v μ ν : v ∈ inV n k μ ν}
    mem_prs                pairs of prs d satisfy μ < ν < d
    PB_plaq                every member of PB is a valid box plaquette (isPlaq)
    witness                a nonzero entry i of a plaquette is the index of one of its four links
  Lean 4 core only, proofs by hand, no axioms.
-/
import ym.PairSum

namespace BoxPlaq

open YMChain LatEnc LatList PlaqCert LoopCert CertIdx LoopDot PlaqDot AreaRT SparseDot DegIdx PairSum S2cFinal AnimalSum

def allLtB (n : Nat) : List Nat → Bool
  | [] => true
  | x :: v => Nat.ble (x + 1) n && allLtB n v

theorem allLtB_ok (n : Nat) : ∀ v, allLtB n v = true → allLt n v
  | [], _ => trivial
  | _ :: v, h => ⟨Box3Area.ble_le _ _ (AnimalTouch.and_l _ _ h), allLtB_ok n v (AnimalTouch.and_r _ _ h)⟩

def inV (n k μ ν : Nat) : List (List Nat) :=
  filt (fun v => allLtB n (shiftC v μ) && allLtB n (shiftC v ν)) (verts n (k + 2))

def PB (n d k : Nat) : List (List Nat) := bindL (prs d) (fun p => (inV n k p.1 p.2).map (fun v => plV n d k v p.1 p.2))

theorem mem_prs : ∀ (d : Nat) (p : Nat × Nat), memL p (prs d) → p.1 + 1 ≤ p.2 ∧ p.2 + 1 ≤ d
  | 0, _, h => absurd h id
  | d + 1, p, h => by
    rcases mem_append_inv _ _ _ h with e | e
    · have r := mem_prs d p e
      exact ⟨r.1, Nat.le_succ_of_le r.2⟩
    · obtain ⟨μ, hμ, ep⟩ := mem_map_inv _ p _ e
      rw [← ep]
      exact ⟨upto_lt d μ hμ, Nat.le_refl _⟩

/-- **Every member of the canonical list is a valid box plaquette.** -/
theorem PB_plaq (n d k : Nat) : ∀ q, memL q (PB n d k) → isPlaq n d k q := by
  intro q hq
  obtain ⟨p, hp, hq'⟩ := mem_bindL_inv _ q _ hq
  obtain ⟨v, hv, e⟩ := mem_map_inv _ q _ hq'
  have hr := mem_prs d p hp
  have hf := mem_filt_inv _ v _ hv
  have vv := verts_valid n (k + 2) v hf.1
  exact ⟨v, p.1, p.2, ⟨vv.1, vv.2, allLtB_ok n _ (AnimalTouch.and_l _ _ hf.2), allLtB_ok n _ (AnimalTouch.and_r _ _ hf.2),
    hr.1, hr.2⟩, e.symm⟩

theorem cntN_pos (i : Nat) : ∀ S : List Nat, cntN i S ≠ 0 → memL i S
  | [], h => absurd rfl h
  | s :: S, h => by
    by_cases hs : s = i
    · exact Or.inl hs.symm
    · have h' : (if s = i then 1 else 0) + cntN i S ≠ 0 := h
      rw [FiniteRecChain.ite_neg' _ _ hs, Nat.zero_add] at h'
      exact Or.inr (cntN_pos i S h')

/-- **A nonzero entry of a plaquette vector is the index of one of its four links.** -/
theorem witness (n d k : Nat) (v : List Nat) (μ ν i : Nat) (h : gt (plV n d k v μ ν) i ≠ 0) :
    ∃ ℓ, memL ℓ (plL v μ ν) ∧ enc n d ℓ = i := by
  rcases Nat.lt_or_ge i (Lt n d k) with hi | hi
  · have e := gt_indV (Lt n d k) 0 i ((plL v μ ν).map (enc n d)) hi
    rw [Nat.zero_add] at e
    have h' : gt (indV 0 (Lt n d k) ((plL v μ ν).map (enc n d))) i ≠ 0 := h
    rw [e] at h'
    obtain ⟨ℓ, hℓ, eℓ⟩ := mem_map_inv _ i _ (cntN_pos i _ h')
    exact ⟨ℓ, hℓ, eℓ⟩
  · exact absurd (gt_indV_out (Lt n d k) 0 i _ hi) h

end BoxPlaq

#print axioms BoxPlaq.allLtB
#print axioms BoxPlaq.allLtB_ok
#print axioms BoxPlaq.inV
#print axioms BoxPlaq.PB
#print axioms BoxPlaq.mem_prs
#print axioms BoxPlaq.PB_plaq
#print axioms BoxPlaq.cntN_pos
#print axioms BoxPlaq.witness

ym/Bridge2D.lean

lean_all/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/BridgeLogic.lean

lean_all/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/CBLib.lean

lean_all/ym/CBLib.lean

/-
  ym/CBLib — T45, G69b3: generic tools for the fixed-N CB-M certificates (Cauchy–Binet form).
    Mt s e X = sum_i x_{i+s} x_{i+s+e} (ReduceCBM.Mt) is the tail correlation:  mt_sum, mt_step (peel one term), rr_mt;
    the convolution s_k = sum_{a+b=k} x_a x_b as an explicit recursion cs (conv_cs);
    sums of several Cauchy–Binet values are nonnegative when the summed kernel is (cb_multi_nn);
    sq_nn. Lean 4 core only, no axioms.
-/
import ym.CBM2

namespace CBLib

open YMChain SU2Haar SU2Char Fusion MigdalCone CgSplit HeatPair QBinom

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

/-- the tail correlation as a finite sum -/
theorem mt_sum (s e : Nat) : ∀ (X : List R) (NN : Nat), X.length ≤ NN →
    ReduceCBM.Mt K s e X = sumN K (fun i => K.mul (co K X (i + s)) (co K X (i + s + e))) NN
  | [], NN, _ => by
    rw [sumN_ext K _ (fun _ => K.zero) (fun i => by show K.mul K.zero K.zero = K.zero; lib_ring) NN, CBM2.sum_zero]
    rfl
  | _ :: _, 0, h => absurd h (Nat.not_succ_le_zero _)
  | x0 :: Y, NN + 1, h => by
    have ih := mt_sum s e Y NN (Nat.le_of_succ_le_succ h)
    rw [sumN_shift K]
    have ee : sumN K (fun k => K.mul (co K (x0 :: Y) (k + 1 + s)) (co K (x0 :: Y) (k + 1 + s + e))) NN
        = sumN K (fun i => K.mul (co K Y (i + s)) (co K Y (i + s + e))) NN := by
      apply sumN_ext K
      intro k
      rw [show k + 1 + s = k + s + 1 by lib_nat, show k + s + 1 + e = k + s + e + 1 by lib_nat]
      rfl
    have e0 : K.mul (co K (x0 :: Y) (0 + s)) (co K (x0 :: Y) (0 + s + e))
        = K.mul (co K (x0 :: Y) s) (co K (x0 :: Y) (s + e)) := by rw [Nat.zero_add]
    show K.add (ReduceCBM.Mt K s e Y) (K.mul (co K (x0 :: Y) s) (co K (x0 :: Y) (s + e))) = _
    rw [ee, e0, ← ih]
    lib_ring

/-- peel one term: Mt s e X = x_s x_{s+e} + Mt (s+1) e X -/
theorem mt_step (s e : Nat) (X : List R) :
    ReduceCBM.Mt K s e X = K.add (K.mul (co K X s) (co K X (s + e))) (ReduceCBM.Mt K (s + 1) e X) := by
  rw [mt_sum K s e X (X.length + 1) (Nat.le_succ _), mt_sum K (s + 1) e X X.length (Nat.le_refl _), sumN_shift K]
  have ee : sumN K (fun k => K.mul (co K X (k + 1 + s)) (co K X (k + 1 + s + e))) X.length
      = sumN K (fun i => K.mul (co K X (i + (s + 1))) (co K X (i + (s + 1) + e))) X.length := by
    apply sumN_ext K
    intro k
    try dsimp only
    rw [show k + 1 + s = k + (s + 1) by lib_nat]
  rw [ee, Nat.zero_add]

theorem rr_mt (e : Nat) : ∀ (X : List R), Corr.rr K e X = ReduceCBM.Mt K 0 e X
  | [] => rfl
  | x0 :: Y => by
    show K.add (Corr.rr K e Y) (K.mul x0 (co K (x0 :: Y) e))
      = K.add (ReduceCBM.Mt K 0 e Y) (K.mul (co K (x0 :: Y) 0) (co K (x0 :: Y) (0 + e)))
    rw [rr_mt e Y, Nat.zero_add]
    rfl

/-- explicit convolution recursion -/
def cs (f : Nat → R) : Nat → R
  | 0 => K.mul (f 0) (f 0)
  | 1 => K.add (K.mul (f 0) (f 1)) (K.mul (f 0) (f 1))
  | k + 2 => K.add (K.add (K.mul (f 0) (f (k + 2))) (K.mul (f 0) (f (k + 2)))) (cs (fun i => f (i + 1)) k)

theorem cs_zero : ∀ (k : Nat), cs K (fun _ => K.zero) k = K.zero
  | 0 => by show K.mul K.zero K.zero = K.zero; lib_ring
  | 1 => by show K.add (K.mul K.zero K.zero) (K.mul K.zero K.zero) = K.zero; lib_ring
  | k + 2 => by
    show K.add (K.add (K.mul K.zero K.zero) (K.mul K.zero K.zero)) (cs K (fun _ => K.zero) k) = K.zero
    rw [cs_zero k]; lib_ring

/-- s_k(X) = cs (co X) k -/
theorem conv_cs : ∀ (k : Nat) (X : List R), RhoC.bil K X (fun j l => dl k (j + l)) = cs K (co K X) k
  | 0, [] => by show K.zero = K.mul K.zero K.zero; lib_ring
  | 1, [] => by show K.zero = K.add (K.mul K.zero K.zero) (K.mul K.zero K.zero); lib_ring
  | k + 2, [] => by
    show K.zero = cs K (fun _ => K.zero) (k + 2)
    rw [cs_zero]
  | 0, x0 :: Y => CBM2.conv0 K x0 Y
  | 1, x0 :: Y => CBM2.conv1 K x0 Y
  | k + 2, x0 :: Y => by
    rw [Corr.conv_step K k x0 Y, conv_cs k Y]
    rfl

theorem sq_nn (O : ORing K) (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 := HeatV.sub_nn K O _ _ h
    have e : K.mul a a = K.mul (K.add K.zero (K.neg a)) (K.add K.zero (K.neg a)) := by lib_ring
    rw [e]
    exact O.mul_nonneg _ _ hn hn

/-- **several Cauchy–Binet values**: sum_{j<J} (<f,F_j><g,G_j> - <f,G_j><g,F_j>) is the double sum of
    minor(f,g)(i,k) times the summed kernel sum_{j<J} minor(F_j,G_j)(i,k) -/
def kern (F G : Nat → Nat → R) (J i k : Nat) : R := sumN K (fun j => Lagrange.minor K (F j) (G j) i k) J

def cbv (f g : Nat → R) (F G : Nat → Nat → R) (N J : Nat) : R :=
  sumN K (fun j => K.add (K.mul (CauchyBinet2.ip K f (F j) N) (CauchyBinet2.ip K g (G j) N))
    (K.neg (K.mul (CauchyBinet2.ip K f (G j) N) (CauchyBinet2.ip K g (F j) N)))) J

theorem cb_multi (f g : Nat → R) (F G : Nat → Nat → R) (N : Nat) : ∀ J,
    cbv K f g F G N J = sumN K (fun k => sumN K (fun i => K.mul (Lagrange.minor K f g i k) (kern K F G J i k)) k) N
  | 0 => by
    show K.zero = _
    rw [sumN_ext K _ (fun _ => K.zero) (fun k => by
      show sumN K (fun i => K.mul (Lagrange.minor K f g i k) K.zero) k = K.zero
      rw [sumN_ext K _ (fun _ => K.zero) (fun i => by lib_ring) k, CBM2.sum_zero]) N, CBM2.sum_zero]
  | J + 1 => by
    show K.add (cbv K f g F G N J) _ = _
    try dsimp only
    rw [cb_multi f g F G N J, CauchyBinet2.cauchy_binet K f g (F J) (G J) N]
    unfold CauchyBinet2.mm
    rw [← sumN_add K]
    apply sumN_ext K
    intro k
    try dsimp only
    rw [← sumN_add K]
    apply sumN_ext K
    intro i
    try dsimp only
    show _ = K.mul (Lagrange.minor K f g i k) (K.add (kern K F G J i k) (Lagrange.minor K (F J) (G J) i k))
    lib_ring

theorem cb_multi_nn (O : ORing K) (f g : Nat → R) (F G : Nat → Nat → R) (N J : Nat)
    (h : ∀ i k, i < k → O.le K.zero (K.mul (Lagrange.minor K f g i k) (kern K F G J i k))) :
    O.le K.zero (cbv K f g F G N J) := by
  rw [cb_multi]
  apply CauchyBinet2.sum_nn K O
  intro k
  have t := Lagrange.sum_le_lt K O (fun _ => K.zero) (fun i => K.mul (Lagrange.minor K f g i k) (kern K F G J i k)) k
    (fun i hi => h i k hi)
  rw [CBM2.sum_zero] at t
  exact t

/-- peel the first k terms of a finite sum -/
theorem sumN_peel (f : Nat → R) (k : Nat) : ∀ n, sumN K f (n + k) = K.add (sumN K f k) (sumN K (fun i => f (i + k)) n)
  | 0 => by
    rw [Nat.zero_add]
    show sumN K f k = K.add (sumN K f k) K.zero
    lib_ring
  | n + 1 => by
    rw [show n + 1 + k = n + k + 1 by lib_nat]
    show K.add (sumN K f (n + k)) (f (n + k)) = K.add (sumN K f k) (K.add (sumN K (fun i => f (i + k)) n) (f (n + k)))
    rw [sumN_peel f k n]
    lib_ring

/-- a bundle of tail-correlation summands -/
def lc (X : List R) : List (Nat × Nat) → Nat → R
  | [], _ => K.zero
  | (a, e) :: ps, i => K.add (K.mul (co K X (i + a)) (co K X (i + a + e))) (lc X ps i)

def mts (X : List R) : List (Nat × Nat) → R
  | [] => K.zero
  | (a, e) :: ps => K.add (ReduceCBM.Mt K a e X) (mts X ps)

theorem lc_sum (X : List R) (n : Nat) (hn : X.length ≤ n) : ∀ (ps : List (Nat × Nat)),
    sumN K (lc K X ps) n = mts K X ps
  | [] => CBM2.sum_zero K n
  | (a, e) :: ps => by
    show sumN K (fun i => K.add (K.mul (co K X (i + a)) (co K X (i + a + e))) (lc K X ps i)) n
      = K.add (ReduceCBM.Mt K a e X) (mts K X ps)
    rw [sumN_add K, lc_sum X n hn ps, ← mt_sum K a e X n hn]

end CBLib

#print axioms CBLib.mt_sum
#print axioms CBLib.mt_step
#print axioms CBLib.rr_mt
#print axioms CBLib.cs_zero
#print axioms CBLib.conv_cs
#print axioms CBLib.sq_nn
#print axioms CBLib.cb_multi
#print axioms CBLib.cb_multi_nn
#print axioms CBLib.sumN_peel
#print axioms CBLib.lc_sum

ym/CBM10.lean

lean_all/ym/CBM10.lean

/-
  ym/CBM10 — T45, G69/G68: CB-M at N = 10 for every v-good list of every length (generated by
  work/cert/A/lean/gen_cbm.py). Cauchy–Binet form with bottom-truncated windows; the summed kernel is a
  nonnegative combination of tail minors. With ReduceCBM.a_of_cbm this is (A) at n = 9 for all lengths.
  Lean 4 core only, no axioms.
-/
import ym.CBLib

namespace CBM10

open YMChain SU2Haar SU2Char Fusion MigdalCone CgSplit HeatPair QBinom

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

set_option maxRecDepth 20000
set_option maxHeartbeats 0

def wa10 (X : List R) (u : Nat) : R := (co K X (u + 6))
def wa11 (X : List R) (u : Nat) : R := (co K X (u + 5))
def wa20 (X : List R) : Nat → R
  | 0 => (co K X (7))
  | k + 1 => (K.add (co K X (k + 8)) (co K X (k + 6)))

def wa21 (X : List R) : Nat → R
  | 0 => (co K X (6))
  | k + 1 => (K.add (co K X (k + 7)) (co K X (k + 5)))

def wa30 (X : List R) : Nat → R
  | 0 => (co K X (8))
  | 1 => (K.add (co K X (9)) (co K X (7)))
  | k + 2 => (K.add (K.add (co K X (k + 10)) (co K X (k + 8))) (co K X (k + 6)))

def wa31 (X : List R) : Nat → R
  | 0 => (co K X (7))
  | 1 => (K.add (co K X (8)) (co K X (6)))
  | k + 2 => (K.add (K.add (co K X (k + 9)) (co K X (k + 7))) (co K X (k + 5)))

def wa40 (X : List R) : Nat → R
  | 0 => (co K X (9))
  | 1 => (K.add (co K X (10)) (co K X (8)))
  | 2 => (K.add (K.add (co K X (11)) (co K X (9))) (co K X (7)))
  | k + 3 => (K.add (K.add (K.add (co K X (k + 12)) (co K X (k + 10))) (co K X (k + 8))) (co K X (k + 6)))

def wa41 (X : List R) : Nat → R
  | 0 => (co K X (8))
  | 1 => (K.add (co K X (9)) (co K X (7)))
  | 2 => (K.add (K.add (co K X (10)) (co K X (8))) (co K X (6)))
  | k + 3 => (K.add (K.add (K.add (co K X (k + 11)) (co K X (k + 9))) (co K X (k + 7))) (co K X (k + 5)))

def wa50 (X : List R) : Nat → R
  | 0 => (co K X (10))
  | 1 => (K.add (co K X (11)) (co K X (9)))
  | 2 => (K.add (K.add (co K X (12)) (co K X (10))) (co K X (8)))
  | 3 => (K.add (K.add (K.add (co K X (13)) (co K X (11))) (co K X (9))) (co K X (7)))
  | k + 4 => (K.add (K.add (K.add (K.add (co K X (k + 14)) (co K X (k + 12))) (co K X (k + 10))) (co K X (k + 8))) (co K X (k + 6)))

def wa51 (X : List R) : Nat → R
  | 0 => (co K X (9))
  | 1 => (K.add (co K X (10)) (co K X (8)))
  | 2 => (K.add (K.add (co K X (11)) (co K X (9))) (co K X (7)))
  | 3 => (K.add (K.add (K.add (co K X (12)) (co K X (10))) (co K X (8))) (co K X (6)))
  | k + 4 => (K.add (K.add (K.add (K.add (co K X (k + 13)) (co K X (k + 11))) (co K X (k + 9))) (co K X (k + 7))) (co K X (k + 5)))

def Fa (X : List R) : Nat → Nat → R
  | 0 => wa10 K X
  | 1 => wa20 K X
  | 2 => wa30 K X
  | 3 => wa40 K X
  | 4 => wa50 K X
  | _ + 5 => fun _ => K.zero

def Ga (X : List R) : Nat → Nat → R
  | 0 => wa11 K X
  | 1 => wa21 K X
  | 2 => wa31 K X
  | 3 => wa41 K X
  | 4 => wa51 K X
  | _ + 5 => fun _ => K.zero

variable (O : ORing K)

theorem kna_0_0 (X : List R) (hg : L1Full.Good K O X) :
    O.le K.zero (CBLib.kern K (Fa K X) (Ga K X) 5 (0) (1)) := by
  have kv : CBLib.kern K (Fa K X) (Ga K X) 5 (0) (1) = (K.add (K.add (K.add (K.add (K.add K.zero (K.add (K.mul (co K X (6)) (co K X (6))) (K.neg (K.mul (co K X (7)) (co K X (5)))))) (K.add (K.mul (co K X (7)) (K.add (co K X (7)) (co K X (5)))) (K.neg (K.mul (K.add (co K X (8)) (co K X (6))) (co K X (6)))))) (K.add (K.mul (co K X (8)) (K.add (co K X (8)) (co K X (6)))) (K.neg (K.mul (K.add (co K X   -- (+300 chars; full line in the Zenodo source)
  have t0 : O.le K.zero (VGoodTail.tt K X (10) (11)) := VGoodTail.tail_nn K O X hg (10) 0
  have et0 : (VGoodTail.tt K X (10) (11)) = (K.add (K.mul (co K X (10)) (co K X (10))) (K.neg (K.mul (co K X (9)) (co K X (11))))) := rfl
  have hp : O.le K.zero (VGoodTail.tt K X (10) (11)) := by
    exact t0
  refine O.le_trans _ _ _ hp (TripleE.le_of_eq' K O _ _ ?_)
  rw [kv, et0]
  ring_z

theorem kna_0_1 (X : List R) (hg : L1Full.Good K O X) :
    O.le K.zero (CBLib.kern K (Fa K X) (Ga K X) 5 (0) (2)) := by
  have kv : CBLib.kern K (Fa K X) (Ga K X) 5 (0) (2) = (K.add (K.add (K.add (K.add (K.add K.zero (K.add (K.mul (co K X (6)) (co K X (7))) (K.neg (K.mul (co K X (8)) (co K X (5)))))) (K.add (K.mul (co K X (7)) (K.add (co K X (8)) (co K X (6)))) (K.neg (K.mul (K.add (co K X (9)) (co K X (7))) (co K X (6)))))) (K.add (K.mul (co K X (8)) (K.add (K.add (co K X (9)) (co K X (7))) (co K X (5)))) (K.neg (  -- (+429 chars; full line in the Zenodo source)
  have t0 : O.le K.zero (VGoodTail.tt K X (9) (11)) := VGoodTail.tail_nn K O X hg (9) 1
  have et0 : (VGoodTail.tt K X (9) (11)) = (K.add (K.mul (co K X (9)) (co K X (10))) (K.neg (K.mul (co K X (8)) (co K X (11))))) := rfl
  have t1 : O.le K.zero (VGoodTail.tt K X (10) (12)) := VGoodTail.tail_nn K O X hg (10) 1
  have et1 : (VGoodTail.tt K X (10) (12)) = (K.add (K.mul (co K X (10)) (co K X (11))) (K.neg (K.mul (co K X (9)) (co K X (12))))) := rfl
  have hp : O.le K.zero (K.add (VGoodTail.tt K X (9) (11)) (VGoodTail.tt K X (10) (12))) := by
    exact (TopLC.add_nn K O _ _ t0 t1)
  refine O.le_trans _ _ _ hp (TripleE.le_of_eq' K O _ _ ?_)
  rw [kv, et0, et1]
  ring_z

theorem kna_0_2 (X : List R) (hg : L1Full.Good K O X) :
    O.le K.zero (CBLib.kern K (Fa K X) (Ga K X) 5 (0) (3)) := by
  have kv : CBLib.kern K (Fa K X) (Ga K X) 5 (0) (3) = (K.add (K.add (K.add (K.add (K.add K.zero (K.add (K.mul (co K X (6)) (co K X (8))) (K.neg (K.mul (co K X (9)) (co K X (5)))))) (K.add (K.mul (co K X (7)) (K.add (co K X (9)) (co K X (7)))) (K.neg (K.mul (K.add (co K X (10)) (co K X (8))) (co K X (6)))))) (K.add (K.mul (co K X (8)) (K.add (K.add (co K X (10)) (co K X (8))) (co K X (6)))) (K.neg  -- (+517 chars; full line in the Zenodo source)
  have t0 : O.le K.zero (VGoodTail.tt K X (8) (11)) := VGoodTail.tail_nn K O X hg (8) 2
  have et0 : (VGoodTail.tt K X (8) (11)) = (K.add (K.mul (co K X (8)) (co K X (10))) (K.neg (K.mul (co K X (7)) (co K X (11))))) := rfl
  have t1 : O.le K.zero (VGoodTail.tt K X (9) (12)) := VGoodTail.tail_nn K O X hg (9) 2
  have et1 : (VGoodTail.tt K X (9) (12)) = (K.add (K.mul (co K X (9)) (co K X (11))) (K.neg (K.mul (co K X (8)) (co K X (12))))) := rfl
  have t2 : O.le K.zero (VGoodTail.tt K X (10) (13)) := VGoodTail.tail_nn K O X hg (10) 2
  have et2 : (VGoodTail.tt K X (10) (13)) = (K.add (K.mul (co K X (10)) (co K X (12))) (K.neg (K.mul (co K X (9)) (co K X (13))))) := rfl
  have t3 : O.le K.zero (VGoodTail.tt K X (10) (11)) := VGoodTail.tail_nn K O X hg (10) 0
  have et3 : (VGoodTail.tt K X (10) (11)) = (K.add (K.mul (co K X (10)) (co K X (10))) (K.neg (K.mul (co K X (9)) (co K X (11))))) := rfl
  have hp : O.le K.zero (K.add (K.add (K.add (VGoodTail.tt K X (8) (11)) (VGoodTail.tt K X (9) (12))) (VGoodTail.tt K X (10) (13))) (VGoodTail.tt K X (10) (11))) := by
    exact (TopLC.add_nn K O _ _ (TopLC.add_nn K O _ _ (TopLC.add_nn K O _ _ t0 t1) t2) t3)
  refine O.le_trans _ _ _ hp (TripleE.le_of_eq' K O _ _ ?_)
  rw [kv, et0, et1, et2, et3]
  ring_z

theorem kna_0_3 (X : List R) (hg : L1Full.Good K O X) :
    O.le K.zero (CBLib.kern K (Fa K X) (Ga K X) 5 (0) (4)) := by
  have kv : CBLib.kern K (Fa K X) (Ga K X) 5 (0) (4) = (K.add (K.add (K.add (K.add (K.add K.zero (K.add (K.mul (co K X (6)) (co K X (9))) (K.neg (K.mul (co K X (10)) (co K X (5)))))) (K.add (K.mul (co K X (7)) (K.add (co K X (10)) (co K X (8)))) (K.neg (K.mul (K.add (co K X (11)) (co K X (9))) (co K X (6)))))) (K.add (K.mul (co K X (8)) (K.add (K.add (co K X (11)) (co K X (9))) (co K X (7)))) (K.n  -- (+564 chars; full line in the Zenodo source)
  have t0 : O.le K.zero (VGoodTail.tt K X (7) (11)) := VGoodTail.tail_nn K O X hg (7) 3
  have et0 : (VGoodTail.tt K X (7) (11)) = (K.add (K.mul (co K X (7)) (co K X (10))) (K.neg (K.mul (co K X (6)) (co K X (11))))) := rfl
  have t1 : O.le K.zero (VGoodTail.tt K X (8) (12)) := VGoodTail.tail_nn K O X hg (8) 3
  have et1 : (VGoodTail.tt K X (8) (12)) = (K.add (K.mul (co K X (8)) (co K X (11))) (K.neg (K.mul (co K X (7)) (co K X (12))))) := rfl
  have t2 : O.le K.zero (VGoodTail.tt K X (9) (13)) := VGoodTail.tail_nn K O X hg (9) 3
  have et2 : (VGoodTail.tt K X (9) (13)) = (K.add (K.mul (co K X (9)) (co K X (12))) (K.neg (K.mul (co K X (8)) (co K X (13))))) := rfl
  have t3 : O.le K.zero (VGoodTail.tt K X (9) (11)) := VGoodTail.tail_nn K O X hg (9) 1
  have et3 : (VGoodTail.tt K X (9) (11)) = (K.add (K.mul (co K X (9)) (co K X (10))) (K.neg (K.mul (co K X (8)) (co K X (11))))) := rfl
  have t4 : O.le K.zero (VGoodTail.tt K X (10) (14)) := VGoodTail.tail_nn K O X hg (10) 3
  have et4 : (VGoodTail.tt K X (10) (14)) = (K.add (K.mul (co K X (10)) (co K X (13))) (K.neg (K.mul (co K X (9)) (co K X (14))))) := rfl
  have t5 : O.le K.zero (VGoodTail.tt K X (10) (12)) := VGoodTail.tail_nn K O X hg (10) 1
  have et5 : (VGoodTail.tt K X (10) (12)) = (K.add (K.mul (co K X (10)) (co K X (11))) (K.neg (K.mul (co K X (9)) (co K X (12))))) := rfl
  have hp : O.le K.zero (K.add (K.add (K.add (K.add (K.add (VGoodTail.tt K X (7) (11)) (VGoodTail.tt K X (8) (12))) (VGoodTail.tt K X (9) (13))) (VGoodTail.tt K X (9) (11))) (VGoodTail.tt K X (10) (14))) (VGoodTail.tt K X (10) (12))) := by
    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 _ _ t0 t1) t2) t3) t4) t5)
  refine O.le_trans _ _ _ hp (TripleE.le_of_eq' K O _ _ ?_)
  rw [kv, et0, et1, et2, et3, et4, et5]
  ring_z

theorem kna_0_4 (X : List R) (hg : L1Full.Good K O X) :
    O.le K.zero (CBLib.kern K (Fa K X) (Ga K X) 5 (0) (5)) := by
  have kv : CBLib.kern K (Fa K X) (Ga K X) 5 (0) (5) = (K.add (K.add (K.add (K.add (K.add K.zero (K.add (K.mul (co K X (6)) (co K X (10))) (K.neg (K.mul (co K X (11)) (co K X (5)))))) (K.add (K.mul (co K X (7)) (K.add (co K X (11)) (co K X (9)))) (K.neg (K.mul (K.add (co K X (12)) (co K X (10))) (co K X (6)))))) (K.add (K.mul (co K X (8)) (K.add (K.add (co K X (12)) (co K X (10))) (co K X (8)))) (  -- (+569 chars; full line in the Zenodo source)
  have t0 : O.le K.zero (VGoodTail.tt K X (6) (11)) := VGoodTail.tail_nn K O X hg (6) 4
  have et0 : (VGoodTail.tt K X (6) (11)) = (K.add (K.mul (co K X (6)) (co K X (10))) (K.neg (K.mul (co K X (5)) (co K X (11))))) := rfl
  have t1 : O.le K.zero (VGoodTail.tt K X (7) (12)) := VGoodTail.tail_nn K O X hg (7) 4
  have et1 : (VGoodTail.tt K X (7) (12)) = (K.add (K.mul (co K X (7)) (co K X (11))) (K.neg (K.mul (co K X (6)) (co K X (12))))) := rfl
  have t2 : O.le K.zero (VGoodTail.tt K X (8) (13)) := VGoodTail.tail_nn K O X hg (8) 4
  have et2 : (VGoodTail.tt K X (8) (13)) = (K.add (K.mul (co K X (8)) (co K X (12))) (K.neg (K.mul (co K X (7)) (co K X (13))))) := rfl
  have t3 : O.le K.zero (VGoodTail.tt K X (8) (11)) := VGoodTail.tail_nn K O X hg (8) 2
  have et3 : (VGoodTail.tt K X (8) (11)) = (K.add (K.mul (co K X (8)) (co K X (10))) (K.neg (K.mul (co K X (7)) (co K X (11))))) := rfl
  have t4 : O.le K.zero (VGoodTail.tt K X (9) (14)) := VGoodTail.tail_nn K O X hg (9) 4
  have et4 : (VGoodTail.tt K X (9) (14)) = (K.add (K.mul (co K X (9)) (co K X (13))) (K.neg (K.mul (co K X (8)) (co K X (14))))) := rfl
  have t5 : O.le K.zero (VGoodTail.tt K X (9) (12)) := VGoodTail.tail_nn K O X hg (9) 2
  have et5 : (VGoodTail.tt K X (9) (12)) = (K.add (K.mul (co K X (9)) (co K X (11))) (K.neg (K.mul (co K X (8)) (co K X (12))))) := rfl
  have t6 : O.le K.zero (VGoodTail.tt K X (10) (15)) := VGoodTail.tail_nn K O X hg (10) 4
  have et6 : (VGoodTail.tt K X (10) (15)) = (K.add (K.mul (co K X (10)) (co K X (14))) (K.neg (K.mul (co K X (9)) (co K X (15))))) := rfl
  have t7 : O.le K.zero (VGoodTail.tt K X (10) (13)) := VGoodTail.tail_nn K O X hg (10) 2
  have et7 : (VGoodTail.tt K X (10) (13)) = (K.add (K.mul (co K X (10)) (co K X (12))) (K.neg (K.mul (co K X (9)) (co K X (13))))) := rfl
  have t8 : O.le K.zero (VGoodTail.tt K X (10) (11)) := VGoodTail.tail_nn K O X hg (10) 0
  have et8 : (VGoodTail.tt K X (10) (11)) = (K.add (K.mul (co K X (10)) (co K X (10))) (K.neg (K.mul (co K X (9)) (co K X (11))))) := rfl
  have hp : O.le K.zero (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (VGoodTail.tt K X (6) (11)) (VGoodTail.tt K X (7) (12))) (VGoodTail.tt K X (8) (13))) (VGoodTail.tt K X (8) (11))) (VGoodTail.tt K X (9) (14))) (VGoodTail.tt K X (9) (12))) (VGoodTail.tt K X (10) (15))) (VGoodTail.tt K X (10) (13))) (VGoodTail.tt K X (10) (11))) := by
    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 _ _ (TopLC.add_nn K O _ _ (TopLC.add_nn K O _ _ t0 t1) t2) t3) t4) t5) t6) t7) t8)
  refine O.le_trans _ _ _ hp (TripleE.le_of_eq' K O _ _ ?_)
  rw [kv, et0, et1, et2, et3, et4, et5, et6, et7, et8]
  ring_z

theorem kna_0_5 (X : List R) (hg : L1Full.Good K O X) :
    O.le K.zero (CBLib.kern K (Fa K X) (Ga K X) 5 (0) (6)) := by
  have kv : CBLib.kern K (Fa K X) (Ga K X) 5 (0) (6) = (K.add (K.add (K.add (K.add (K.add K.zero (K.add (K.mul (co K X (6)) (co K X (11))) (K.neg (K.mul (co K X (12)) (co K X (5)))))) (K.add (K.mul (co K X (7)) (K.add (co K X (12)) (co K X (10)))) (K.neg (K.mul (K.add (co K X (13)) (co K X (11))) (co K X (6)))))) (K.add (K.mul (co K X (8)) (K.add (K.add (co K X (13)) (co K X (11))) (co K X (9))))   -- (+573 chars; full line in the Zenodo source)
  have t0 : O.le K.zero (VGoodTail.tt K X (6) (12)) := VGoodTail.tail_nn K O X hg (6) 5
  have et0 : (VGoodTail.tt K X (6) (12)) = (K.add (K.mul (co K X (6)) (co K X (11))) (K.neg (K.mul (co K X (5)) (co K X (12))))) := rfl
  have t1 : O.le K.zero (VGoodTail.tt K X (7) (13)) := VGoodTail.tail_nn K O X hg (7) 5
  have et1 : (VGoodTail.tt K X (7) (13)) = (K.add (K.mul (co K X (7)) (co K X (12))) (K.neg (K.mul (co K X (6)) (co K X (13))))) := rfl
  have t2 : O.le K.zero (VGoodTail.tt K X (7) (11)) := VGoodTail.tail_nn K O X hg (7) 3
  have et2 : (VGoodTail.tt K X (7) (11)) = (K.add (K.mul (co K X (7)) (co K X (10))) (K.neg (K.mul (co K X (6)) (co K X (11))))) := rfl
  have t3 : O.le K.zero (VGoodTail.tt K X (8) (14)) := VGoodTail.tail_nn K O X hg (8) 5
  have et3 : (VGoodTail.tt K X (8) (14)) = (K.add (K.mul (co K X (8)) (co K X (13))) (K.neg (K.mul (co K X (7)) (co K X (14))))) := rfl
  have t4 : O.le K.zero (VGoodTail.tt K X (8) (12)) := VGoodTail.tail_nn K O X hg (8) 3
  have et4 : (VGoodTail.tt K X (8) (12)) = (K.add (K.mul (co K X (8)) (co K X (11))) (K.neg (K.mul (co K X (7)) (co K X (12))))) := rfl
  have t5 : O.le K.zero (VGoodTail.tt K X (9) (15)) := VGoodTail.tail_nn K O X hg (9) 5
  have et5 : (VGoodTail.tt K X (9) (15)) = (K.add (K.mul (co K X (9)) (co K X (14))) (K.neg (K.mul (co K X (8)) (co K X (15))))) := rfl
  have t6 : O.le K.zero (VGoodTail.tt K X (9) (13)) := VGoodTail.tail_nn K O X hg (9) 3
  have et6 : (VGoodTail.tt K X (9) (13)) = (K.add (K.mul (co K X (9)) (co K X (12))) (K.neg (K.mul (co K X (8)) (co K X (13))))) := rfl
  have t7 : O.le K.zero (VGoodTail.tt K X (9) (11)) := VGoodTail.tail_nn K O X hg (9) 1
  have et7 : (VGoodTail.tt K X (9) (11)) = (K.add (K.mul (co K X (9)) (co K X (10))) (K.neg (K.mul (co K X (8)) (co K X (11))))) := rfl
  have t8 : O.le K.zero (VGoodTail.tt K X (10) (16)) := VGoodTail.tail_nn K O X hg (10) 5
  have et8 : (VGoodTail.tt K X (10) (16)) = (K.add (K.mul (co K X (10)) (co K X (15))) (K.neg (K.mul (co K X (9)) (co K X (16))))) := rfl
  have t9 : O.le K.zero (VGoodTail.tt K X (10) (14)) := VGoodTail.tail_nn K O X hg (10) 3
  have et9 : (VGoodTail.tt K X (10) (14)) = (K.add (K.mul (co K X (10)) (co K X (13))) (K.neg (K.mul (co K X (9)) (co K X (14))))) := rfl
  have t10 : O.le K.zero (VGoodTail.tt K X (10) (12)) := VGoodTail.tail_nn K O X hg (10) 1
  have et10 : (VGoodTail.tt K X (10) (12)) = (K.add (K.mul (co K X (10)) (co K X (11))) (K.neg (K.mul (co K X (9)) (co K X (12))))) := rfl
  have hp : O.le K.zero (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (VGoodTail.tt K X (6) (12)) (VGoodTail.tt K X (7) (13))) (VGoodTail.tt K X (7) (11))) (VGoodTail.tt K X (8) (14))) (VGoodTail.tt K X (8) (12))) (VGoodTail.tt K X (9) (15))) (VGoodTail.tt K X (9) (13))) (VGoodTail.tt K X (9) (11))) (VGoodTail.tt K X (10) (16))) (VGoodTail.tt K X (10) (14))) (VGoodTail.tt K  -- (+20 chars; full line in the Zenodo source)
    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 _ _ (TopLC.add_nn K O _ _ (TopLC.add_nn K O _ _ (TopLC.add_nn K O _ _ (TopLC.add_nn K O _ _ t0 t1) t2) t3) t4) t5) t6) t7) t8) t9) t10)
  refine O.le_trans _ _ _ hp (TripleE.le_of_eq' K O _ _ ?_)
  rw [kv, et0, et1, et2, et3, et4, et5, et6, et7, et8, et9, et10]
  ring_z

theorem kna_0_6 (X : List R) (hg : L1Full.Good K O X) :
    O.le K.zero (CBLib.kern K (Fa K X) (Ga K X) 5 (0) (7)) := by
  have kv : CBLib.kern K (Fa K X) (Ga K X) 5 (0) (7) = (K.add (K.add (K.add (K.add (K.add K.zero (K.add (K.mul (co K X (6)) (co K X (12))) (K.neg (K.mul (co K X (13)) (co K X (5)))))) (K.add (K.mul (co K X (7)) (K.add (co K X (13)) (co K X (11)))) (K.neg (K.mul (K.add (co K X (14)) (co K X (12))) (co K X (6)))))) (K.add (K.mul (co K X (8)) (K.add (K.add (co K X (14)) (co K X (12))) (co K X (10))))  -- (+576 chars; full line in the Zenodo source)
  have t0 : O.le K.zero (VGoodTail.tt K X (6) (13)) := VGoodTail.tail_nn K O X hg (6) 6
  have et0 : (VGoodTail.tt K X (6) (13)) = (K.add (K.mul (co K X (6)) (co K X (12))) (K.neg (K.mul (co K X (5)) (co K X (13))))) := rfl
  have t1 : O.le K.zero (VGoodTail.tt K X (7) (14)) := VGoodTail.tail_nn K O X hg (7) 6
  have et1 : (VGoodTail.tt K X (7) (14)) = (K.add (K.mul (co K X (7)) (co K X (13))) (K.neg (K.mul (co K X (6)) (co K X (14))))) := rfl
  have t2 : O.le K.zero (VGoodTail.tt K X (7) (12)) := VGoodTail.tail_nn K O X hg (7) 4
  have et2 : (VGoodTail.tt K X (7) (12)) = (K.add (K.mul (co K X (7)) (co K X (11))) (K.neg (K.mul (co K X (6)) (co K X (12))))) := rfl
  have t3 : O.le K.zero (VGoodTail.tt K X (8) (15)) := VGoodTail.tail_nn K O X hg (8) 6
  have et3 : (VGoodTail.tt K X (8) (15)) = (K.add (K.mul (co K X (8)) (co K X (14))) (K.neg (K.mul (co K X (7)) (co K X (15))))) := rfl
  have t4 : O.le K.zero (VGoodTail.tt K X (8) (13)) := VGoodTail.tail_nn K O X hg (8) 4
  have et4 : (VGoodTail.tt K X (8) (13)) = (K.add (K.mul (co K X (8)) (co K X (12))) (K.neg (K.mul (co K X (7)) (co K X (13))))) := rfl
  have t5 : O.le K.zero (VGoodTail.tt K X (8) (11)) := VGoodTail.tail_nn K O X hg (8) 2
  have et5 : (VGoodTail.tt K X (8) (11)) = (K.add (K.mul (co K X (8)) (co K X (10))) (K.neg (K.mul (co K X (7)) (co K X (11))))) := rfl
  have t6 : O.le K.zero (VGoodTail.tt K X (9) (16)) := VGoodTail.tail_nn K O X hg (9) 6
  have et6 : (VGoodTail.tt K X (9) (16)) = (K.add (K.mul (co K X (9)) (co K X (15))) (K.neg (K.mul (co K X (8)) (co K X (16))))) := rfl
  have t7 : O.le K.zero (VGoodTail.tt K X (9) (14)) := VGoodTail.tail_nn K O X hg (9) 4
  have et7 : (VGoodTail.tt K X (9) (14)) = (K.add (K.mul (co K X (9)) (co K X (13))) (K.neg (K.mul (co K X (8)) (co K X (14))))) := rfl
  have t8 : O.le K.zero (VGoodTail.tt K X (9) (12)) := VGoodTail.tail_nn K O X hg (9) 2
  have et8 : (VGoodTail.tt K X (9) (12)) = (K.add (K.mul (co K X (9)) (co K X (11))) (K.neg (K.mul (co K X (8)) (co K X (12))))) := rfl
  have t9 : O.le K.zero (VGoodTail.tt K X (10) (17)) := VGoodTail.tail_nn K O X hg (10) 6
  have et9 : (VGoodTail.tt K X (10) (17)) = (K.add (K.mul (co K X (10)) (co K X (16))) (K.neg (K.mul (co K X (9)) (co K X (17))))) := rfl
  have t10 : O.le K.zero (VGoodTail.tt K X (10) (15)) := VGoodTail.tail_nn K O X hg (10) 4
  have et10 : (VGoodTail.tt K X (10) (15)) = (K.add (K.mul (co K X (10)) (co K X (14))) (K.neg (K.mul (co K X (9)) (co K X (15))))) := rfl
  have t11 : O.le K.zero (VGoodTail.tt K X (10) (13)) := VGoodTail.tail_nn K O X hg (10) 2
  have et11 : (VGoodTail.tt K X (10) (13)) = (K.add (K.mul (co K X (10)) (co K X (12))) (K.neg (K.mul (co K X (9)) (co K X (13))))) := rfl
  have t12 : O.le K.zero (VGoodTail.tt K X (10) (11)) := VGoodTail.tail_nn K O X hg (10) 0
  have et12 : (VGoodTail.tt K X (10) (11)) = (K.add (K.mul (co K X (10)) (co K X (10))) (K.neg (K.mul (co K X (9)) (co K X (11))))) := rfl
  have hp : 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 (VGoodTail.tt K X (6) (13)) (VGoodTail.tt K X (7) (14))) (VGoodTail.tt K X (7) (12))) (VGoodTail.tt K X (8) (15))) (VGoodTail.tt K X (8) (13))) (VGoodTail.tt K X (8) (11))) (VGoodTail.tt K X (9) (16))) (VGoodTail.tt K X (9) (14))) (VGoodTail.tt K X (9) (12))) (VGoodTail.tt K X (10) (17))) (V  -- (+93 chars; full line in the Zenodo source)
    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 _ _ (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 _ _ t0 t1) t2) t3) t4) t5) t6) t7) t8) t9) t10) t11) t12)
  refine O.le_trans _ _ _ hp (TripleE.le_of_eq' K O _ _ ?_)
  rw [kv, et0, et1, et2, et3, et4, et5, et6, et7, et8, et9, et10, et11, et12]
  ring_z

theorem kna_0_7 (X : List R) (hg : L1Full.Good K O X) :
    O.le K.zero (CBLib.kern K (Fa K X) (Ga K X) 5 (0) (8)) := by
  have kv : CBLib.kern K (Fa K X) (Ga K X) 5 (0) (8) = (K.add (K.add (K.add (K.add (K.add K.zero (K.add (K.mul (co K X (6)) (co K X (13))) (K.neg (K.mul (co K X (14)) (co K X (5)))))) (K.add (K.mul (co K X (7)) (K.add (co K X (14)) (co K X (12)))) (K.neg (K.mul (K.add (co K X (15)) (co K X (13))) (co K X (6)))))) (K.add (K.mul (co K X (8)) (K.add (K.add (co K X (15)) (co K X (13))) (co K X (11))))  -- (+578 chars; full line in the Zenodo source)
  have t0 : O.le K.zero (VGoodTail.tt K X (6) (14)) := VGoodTail.tail_nn K O X hg (6) 7
  have et0 : (VGoodTail.tt K X (6) (14)) = (K.add (K.mul (co K X (6)) (co K X (13))) (K.neg (K.mul (co K X (5)) (co K X (14))))) := rfl
  have t1 : O.le K.zero (VGoodTail.tt K X (7) (15)) := VGoodTail.tail_nn K O X hg (7) 7
  have et1 : (VGoodTail.tt K X (7) (15)) = (K.add (K.mul (co K X (7)) (co K X (14))) (K.neg (K.mul (co K X (6)) (co K X (15))))) := rfl
  have t2 : O.le K.zero (VGoodTail.tt K X (7) (13)) := VGoodTail.tail_nn K O X hg (7) 5
  have et2 : (VGoodTail.tt K X (7) (13)) = (K.add (K.mul (co K X (7)) (co K X (12))) (K.neg (K.mul (co K X (6)) (co K X (13))))) := rfl
  have t3 : O.le K.zero (VGoodTail.tt K X (8) (16)) := VGoodTail.tail_nn K O X hg (8) 7
  have et3 : (VGoodTail.tt K X (8) (16)) = (K.add (K.mul (co K X (8)) (co K X (15))) (K.neg (K.mul (co K X (7)) (co K X (16))))) := rfl
  have t4 : O.le K.zero (VGoodTail.tt K X (8) (14)) := VGoodTail.tail_nn K O X hg (8) 5
  have et4 : (VGoodTail.tt K X (8) (14)) = (K.add (K.mul (co K X (8)) (co K X (13))) (K.neg (K.mul (co K X (7)) (co K X (14))))) := rfl
  have t5 : O.le K.zero (VGoodTail.tt K X (8) (12)) := VGoodTail.tail_nn K O X hg (8) 3
  have et5 : (VGoodTail.tt K X (8) (12)) = (K.add (K.mul (co K X (8)) (co K X (11))) (K.neg (K.mul (co K X (7)) (co K X (12))))) := rfl
  have t6 : O.le K.zero (VGoodTail.tt K X (9) (17)) := VGoodTail.tail_nn K O X hg (9) 7
  have et6 : (VGoodTail.tt K X (9) (17)) = (K.add (K.mul (co K X (9)) (co K X (16))) (K.neg (K.mul (co K X (8)) (co K X (17))))) := rfl
  have t7 : O.le K.zero (VGoodTail.tt K X (9) (15)) := VGoodTail.tail_nn K O X hg (9) 5
  have et7 : (VGoodTail.tt K X (9) (15)) = (K.add (K.mul (co K X (9)) (co K X (14))) (K.neg (K.mul (co K X (8)) (co K X (15))))) := rfl
  have t8 : O.le K.zero (VGoodTail.tt K X (9) (13)) := VGoodTail.tail_nn K O X hg (9) 3
  have et8 : (VGoodTail.tt K X (9) (13)) = (K.add (K.mul (co K X (9)) (co K X (12))) (K.neg (K.mul (co K X (8)) (co K X (13))))) := rfl
  have t9 : O.le K.zero (VGoodTail.tt K X (9) (11)) := VGoodTail.tail_nn K O X hg (9) 1
  have et9 : (VGoodTail.tt K X (9) (11)) = (K.add (K.mul (co K X (9)) (co K X (10))) (K.neg (K.mul (co K X (8)) (co K X (11))))) := rfl
  have t10 : O.le K.zero (VGoodTail.tt K X (10) (18)) := VGoodTail.tail_nn K O X hg (10) 7
  have et10 : (VGoodTail.tt K X (10) (18)) = (K.add (K.mul (co K X (10)) (co K X (17))) (K.neg (K.mul (co K X (9)) (co K X (18))))) := rfl
  have t11 : O.le K.zero (VGoodTail.tt K X (10) (16)) := VGoodTail.tail_nn K O X hg (10) 5
  have et11 : (VGoodTail.tt K X (10) (16)) = (K.add (K.mul (co K X (10)) (co K X (15))) (K.neg (K.mul (co K X (9)) (co K X (16))))) := rfl
  have t12 : O.le K.zero (VGoodTail.tt K X (10) (14)) := VGoodTail.tail_nn K O X hg (10) 3
  have et12 : (VGoodTail.tt K X (10) (14)) = (K.add (K.mul (co K X (10)) (co K X (13))) (K.neg (K.mul (co K X (9)) (co K X (14))))) := rfl
  have t13 : O.le K.zero (VGoodTail.tt K X (10) (12)) := VGoodTail.tail_nn K O X hg (10) 1
  have et13 : (VGoodTail.tt K X (10) (12)) = (K.add (K.mul (co K X (10)) (co K X (11))) (K.neg (K.mul (co K X (9)) (co K X (12))))) := rfl
  have hp : 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 (VGoodTail.tt K X (6) (14)) (VGoodTail.tt K X (7) (15))) (VGoodTail.tt K X (7) (13))) (VGoodTail.tt K X (8) (16))) (VGoodTail.tt K X (8) (14))) (VGoodTail.tt K X (8) (12))) (VGoodTail.tt K X (9) (17))) (VGoodTail.tt K X (9) (15))) (VGoodTail.tt K X (9) (13))) (VGoodTail.tt K X (9) (11  -- (+129 chars; full line in the Zenodo source)
    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 _ _ (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 _ _ (TopLC.add_nn K O _ _ t0 t1) t2) t3) t4) t5) t6) t7) t8) t9) t10) t11) t12) t13)
  refine O.le_trans _ _ _ hp (TripleE.le_of_eq' K O _ _ ?_)
  rw [kv, et0, et1, et2, et3, et4, et5, et6, et7, et8, et9, et10, et11, et12, et13]
  ring_z

theorem kna_0_8 (X : List R) (hg : L1Full.Good K O X) :
    O.le K.zero (CBLib.kern K (Fa K X) (Ga K X) 5 (0) (9)) := by
  have kv : CBLib.kern K (Fa K X) (Ga K X) 5 (0) (9) = (K.add (K.add (K.add (K.add (K.add K.zero (K.add (K.mul (co K X (6)) (co K X (14))) (K.neg (K.mul (co K X (15)) (co K X (5)))))) (K.add (K.mul (co K X (7)) (K.add (co K X (15)) (co K X (13)))) (K.neg (K.mul (K.add (co K X (16)) (co K X (14))) (co K X (6)))))) (K.add (K.mul (co K X (8)) (K.add (K.add (co K X (16)) (co K X (14))) (co K X (12))))  -- (+579 chars; full line in the Zenodo source)
  have t0 : O.le K.zero (VGoodTail.tt K X (6) (15)) := VGoodTail.tail_nn K O X hg (6) 8
  have et0 : (VGoodTail.tt K X (6) (15)) = (K.add (K.mul (co K X (6)) (co K X (14))) (K.neg (K.mul (co K X (5)) (co K X (15))))) := rfl
  have t1 : O.le K.zero (VGoodTail.tt K X (7) (16)) := VGoodTail.tail_nn K O X hg (7) 8
  have et1 : (VGoodTail.tt K X (7) (16)) = (K.add (K.mul (co K X (7)) (co K X (15))) (K.neg (K.mul (co K X (6)) (co K X (16))))) := rfl
  have t2 : O.le K.zero (VGoodTail.tt K X (7) (14)) := VGoodTail.tail_nn K O X hg (7) 6
  have et2 : (VGoodTail.tt K X (7) (14)) = (K.add (K.mul (co K X (7)) (co K X (13))) (K.neg (K.mul (co K X (6)) (co K X (14))))) := rfl
  have t3 : O.le K.zero (VGoodTail.tt K X (8) (17)) := VGoodTail.tail_nn K O X hg (8) 8
  have et3 : (VGoodTail.tt K X (8) (17)) = (K.add (K.mul (co K X (8)) (co K X (16))) (K.neg (K.mul (co K X (7)) (co K X (17))))) := rfl
  have t4 : O.le K.zero (VGoodTail.tt K X (8) (15)) := VGoodTail.tail_nn K O X hg (8) 6
  have et4 : (VGoodTail.tt K X (8) (15)) = (K.add (K.mul (co K X (8)) (co K X (14))) (K.neg (K.mul (co K X (7)) (co K X (15))))) := rfl
  have t5 : O.le K.zero (VGoodTail.tt K X (8) (13)) := VGoodTail.tail_nn K O X hg (8) 4
  have et5 : (VGoodTail.tt K X (8) (13)) = (K.add (K.mul (co K X (8)) (co K X (12))) (K.neg (K.mul (co K X (7)) (co K X (13))))) := rfl
  have t6 : O.le K.zero (VGoodTail.tt K X (9) (18)) := VGoodTail.tail_nn K O X hg (9) 8
  have et6 : (VGoodTail.tt K X (9) (18)) = (K.add (K.mul (co K X (9)) (co K X (17))) (K.neg (K.mul (co K X (8)) (co K X (18))))) := rfl
  have t7 : O.le K.zero (VGoodTail.tt K X (9) (16)) := VGoodTail.tail_nn K O X hg (9) 6
  have et7 : (VGoodTail.tt K X (9) (16)) = (K.add (K.mul (co K X (9)) (co K X (15))) (K.neg (K.mul (co K X (8)) (co K X (16))))) := rfl
  have t8 : O.le K.zero (VGoodTail.tt K X (9) (14)) := VGoodTail.tail_nn K O X hg (9) 4
  have et8 : (VGoodTail.tt K X (9) (14)) = (K.add (K.mul (co K X (9)) (co K X (13))) (K.neg (K.mul (co K X (8)) (co K X (14))))) := rfl
  have t9 : O.le K.zero (VGoodTail.tt K X (9) (12)) := VGoodTail.tail_nn K O X hg (9) 2
  have et9 : (VGoodTail.tt K X (9) (12)) = (K.add (K.mul (co K X (9)) (co K X (11))) (K.neg (K.mul (co K X (8)) (co K X (12))))) := rfl
  have t10 : O.le K.zero (VGoodTail.tt K X (10) (19)) := VGoodTail.tail_nn K O X hg (10) 8
  have et10 : (VGoodTail.tt K X (10) (19)) = (K.add (K.mul (co K X (10)) (co K X (18))) (K.neg (K.mul (co K X (9)) (co K X (19))))) := rfl
  have t11 : O.le K.zero (VGoodTail.tt K X (10) (17)) := VGoodTail.tail_nn K O X hg (10) 6
  have et11 : (VGoodTail.tt K X (10) (17)) = (K.add (K.mul (co K X (10)) (co K X (16))) (K.neg (K.mul (co K X (9)) (co K X (17))))) := rfl
  have t12 : O.le K.zero (VGoodTail.tt K X (10) (15)) := VGoodTail.tail_nn K O X hg (10) 4
  have et12 : (VGoodTail.tt K X (10) (15)) = (K.add (K.mul (co K X (10)) (co K X (14))) (K.neg (K.mul (co K X (9)) (co K X (15))))) := rfl
  have t13 : O.le K.zero (VGoodTail.tt K X (10) (13)) := VGoodTail.tail_nn K O X hg (10) 2
  have et13 : (VGoodTail.tt K X (10) (13)) = (K.add (K.mul (co K X (10)) (co K X (12))) (K.neg (K.mul (co K X (9)) (co K X (13))))) := rfl
  have t14 : O.le K.zero (VGoodTail.tt K X (10) (11)) := VGoodTail.tail_nn K O X hg (10) 0
  have et14 : (VGoodTail.tt K X (10) (11)) = (K.add (K.mul (co K X (10)) (co K X (10))) (K.neg (K.mul (co K X (9)) (co K X (11))))) := rfl
  have hp : 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.add (VGoodTail.tt K X (6) (15)) (VGoodTail.tt K X (7) (16))) (VGoodTail.tt K X (7) (14))) (VGoodTail.tt K X (8) (17))) (VGoodTail.tt K X (8) (15))) (VGoodTail.tt K X (8) (13))) (VGoodTail.tt K X (9) (18))) (VGoodTail.tt K X (9) (16))) (VGoodTail.tt K X (9) (14))) (VGoodTail.tt K X   -- (+166 chars; full line in the Zenodo source)
    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 _ _ (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 _ _ (TopLC.add_nn K O _ _ (TopLC.add_nn K O _ _ t0 t1) t2) t3) t4) t5) t6) t7) t8) t9) t10) t11) t12) t13) t14)
  refine O.le_trans _ _ _ hp (TripleE.le_of_eq' K O _ _ ?_)
  rw [kv, et0, et1, et2, et3, et4, et5, et6, et7, et8, et9, et10, et11, et12, et13, et14]
  ring_z

theorem kna_0_9 (X : List R) (hg : L1Full.Good K O X) :
    O.le K.zero (CBLib.kern K (Fa K X) (Ga K X) 5 (0) (10)) := by
  have kv : CBLib.kern K (Fa K X) (Ga K X) 5 (0) (10) = (K.add (K.add (K.add (K.add (K.add K.zero (K.add (K.mul (co K X (6)) (co K X (15))) (K.neg (K.mul (co K X (16)) (co K X (5)))))) (K.add (K.mul (co K X (7)) (K.add (co K X (16)) (co K X (14)))) (K.neg (K.mul (K.add (co K X (17)) (co K X (15))) (co K X (6)))))) (K.add (K.mul (co K X (8)) (K.add (K.add (co K X (17)) (co K X (15))) (co K X (13)))  -- (+580 chars; full line in the Zenodo source)
  have t0 : O.le K.zero (VGoodTail.tt K X (6) (16)) := VGoodTail.tail_nn K O X hg (6) 9
  have et0 : (VGoodTail.tt K X (6) (16)) = (K.add (K.mul (co K X (6)) (co K X (15))) (K.neg (K.mul (co K X (5)) (co K X (16))))) := rfl
  have t1 : O.le K.zero (VGoodTail.tt K X (7) (17)) := VGoodTail.tail_nn K O X hg (7) 9
  have et1 : (VGoodTail.tt K X (7) (17)) = (K.add (K.mul (co K X (7)) (co K X (16))) (K.neg (K.mul (co K X (6)) (co K X (17))))) := rfl
  have t2 : O.le K.zero (VGoodTail.tt K X (7) (15)) := VGoodTail.tail_nn K O X hg (7) 7
  have et2 : (VGoodTail.tt K X (7) (15)) = (K.add (K.mul (co K X (7)) (co K X (14))) (K.neg (K.mul (co K X (6)) (co K X (15))))) := rfl
  have t3 : O.le K.zero (VGoodTail.tt K X (8) (18)) := VGoodTail.tail_nn K O X hg (8) 9
  have et3 : (VGoodTail.tt K X (8) (18)) = (K.add (K.mul (co K X (8)) (co K X (17))) (K.neg (K.mul (co K X (7)) (co K X (18))))) := rfl
  have t4 : O.le K.zero (VGoodTail.tt K X (8) (16)) := VGoodTail.tail_nn K O X hg (8) 7
  have et4 : (VGoodTail.tt K X (8) (16)) = (K.add (K.mul (co K X (8)) (co K X (15))) (K.neg (K.mul (co K X (7)) (co K X (16))))) := rfl
  have t5 : O.le K.zero (VGoodTail.tt K X (8) (14)) := VGoodTail.tail_nn K O X hg (8) 5
  have et5 : (VGoodTail.tt K X (8) (14)) = (K.add (K.mul (co K X (8)) (co K X (13))) (K.neg (K.mul (co K X (7)) (co K X (14))))) := rfl
  have t6 : O.le K.zero (VGoodTail.tt K X (9) (19)) := VGoodTail.tail_nn K O X hg (9) 9
  have et6 : (VGoodTail.tt K X (9) (19)) = (K.add (K.mul (co K X (9)) (co K X (18))) (K.neg (K.mul (co K X (8)) (co K X (19))))) := rfl
  have t7 : O.le K.zero (VGoodTail.tt K X (9) (17)) := VGoodTail.tail_nn K O X hg (9) 7
  have et7 : (VGoodTail.tt K X (9) (17)) = (K.add (K.mul (co K X (9)) (co K X (16))) (K.neg (K.mul (co K X (8)) (co K X (17))))) := rfl
  have t8 : O.le K.zero (VGoodTail.tt K X (9) (15)) := VGoodTail.tail_nn K O X hg (9) 5
  have et8 : (VGoodTail.tt K X (9) (15)) = (K.add (K.mul (co K X (9)) (co K X (14))) (K.neg (K.mul (co K X (8)) (co K X (15))))) := rfl
  have t9 : O.le K.zero (VGoodTail.tt K X (9) (13)) := VGoodTail.tail_nn K O X hg (9) 3
  have et9 : (VGoodTail.tt K X (9) (13)) = (K.add (K.mul (co K X (9)) (co K X (12))) (K.neg (K.mul (co K X (8)) (co K X (13))))) := rfl
  have t10 : O.le K.zero (VGoodTail.tt K X (10) (20)) := VGoodTail.tail_nn K O X hg (10) 9
  have et10 : (VGoodTail.tt K X (10) (20)) = (K.add (K.mul (co K X (10)) (co K X (19))) (K.neg (K.mul (co K X (9)) (co K X (20))))) := rfl
  have t11 : O.le K.zero (VGoodTail.tt K X (10) (18)) := VGoodTail.tail_nn K O X hg (10) 7
  have et11 : (VGoodTail.tt K X (10) (18)) = (K.add (K.mul (co K X (10)) (co K X (17))) (K.neg (K.mul (co K X (9)) (co K X (18))))) := rfl
  have t12 : O.le K.zero (VGoodTail.tt K X (10) (16)) := VGoodTail.tail_nn K O X hg (10) 5
  have et12 : (VGoodTail.tt K X (10) (16)) = (K.add (K.mul (co K X (10)) (co K X (15))) (K.neg (K.mul (co K X (9)) (co K X (16))))) := rfl
  have t13 : O.le K.zero (VGoodTail.tt K X (10) (14)) := VGoodTail.tail_nn K O X hg (10) 3
  have et13 : (VGoodTail.tt K X (10) (14)) = (K.add (K.mul (co K X (10)) (co K X (13))) (K.neg (K.mul (co K X (9)) (co K X (14))))) := rfl
  have t14 : O.le K.zero (VGoodTail.tt K X (10) (12)) := VGoodTail.tail_nn K O X hg (10) 1
  have et14 : (VGoodTail.tt K X (10) (12)) = (K.add (K.mul (co K X (10)) (co K X (11))) (K.neg (K.mul (co K X (9)) (co K X (12))))) := rfl
  have hp : 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.add (VGoodTail.tt K X (6) (16)) (VGoodTail.tt K X (7) (17))) (VGoodTail.tt K X (7) (15))) (VGoodTail.tt K X (8) (18))) (VGoodTail.tt K X (8) (16))) (VGoodTail.tt K X (8) (14))) (VGoodTail.tt K X (9) (19))) (VGoodTail.tt K X (9) (17))) (VGoodTail.tt K X (9) (15))) (VGoodTail.tt K X   -- (+166 chars; full line in the Zenodo source)
    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 _ _ (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 _ _ (TopLC.add_nn K O _ _ (TopLC.add_nn K O _ _ t0 t1) t2) t3) t4) t5) t6) t7) t8) t9) t10) t11) t12) t13) t14)
  refine O.le_trans _ _ _ hp (TripleE.le_of_eq' K O _ _ ?_)
  rw [kv, et0, et1, et2, et3, et4, et5, et6, et7, et8, et9, et10, et11, et12, et13, et14]
  ring_z

theorem kna_0_10 (X : List R) (hg : L1Full.Good K O X) :
    O.le K.zero (CBLib.kern K (Fa K X) (Ga K X) 5 (0) (11)) := by
  have kv : CBLib.kern K (Fa K X) (Ga K X) 5 (0) (11) = (K.add (K.add (K.add (K.add (K.add K.zero (K.add (K.mul (co K X (6)) (co K X (16))) (K.neg (K.mul (co K X (17)) (co K X (5)))))) (K.add (K.mul (co K X (7)) (K.add (co K X (17)) (co K X (15)))) (K.neg (K.mul (K.add (co K X (18)) (co K X (16))) (co K X (6)))))) (K.add (K.mul (co K X (8)) (K.add (K.add (co K X (18)) (co K X (16))) (co K X (14)))  -- (+580 chars; full line in the Zenodo source)
  have t0 : O.le K.zero (VGoodTail.tt K X (6) (17)) := VGoodTail.tail_nn K O X hg (6) 10
  have et0 : (VGoodTail.tt K X (6) (17)) = (K.add (K.mul (co K X (6)) (co K X (16))) (K.neg (K.mul (co K X (5)) (co K X (17))))) := rfl
  have t1 : O.le K.zero (VGoodTail.tt K X (7) (18)) := VGoodTail.tail_nn K O X hg (7) 10
  have et1 : (VGoodTail.tt K X (7) (18)) = (K.add (K.mul (co K X (7)) (co K X (17))) (K.neg (K.mul (co K X (6)) (co K X (18))))) := rfl
  have t2 : O.le K.zero (VGoodTail.tt K X (7) (16)) := VGoodTail.tail_nn K O X hg (7) 8
  have et2 : (VGoodTail.tt K X (7) (16)) = (K.add (K.mul (co K X (7)) (co K X (15))) (K.neg (K.mul (co K X (6)) (co K X (16))))) := rfl
  have t3 : O.le K.zero (VGoodTail.tt K X (8) (19)) := VGoodTail.tail_nn K O X hg (8) 10
  have et3 : (VGoodTail.tt K X (8) (19)) = (K.add (K.mul (co K X (8)) (co K X (18))) (K.neg (K.mul (co K X (7)) (co K X (19))))) := rfl
  have t4 : O.le K.zero (VGoodTail.tt K X (8) (17)) := VGoodTail.tail_nn K O X hg (8) 8
  have et4 : (VGoodTail.tt K X (8) (17)) = (K.add (K.mul (co K X (8)) (co K X (16))) (K.neg (K.mul (co K X (7)) (co K X (17))))) := rfl
  have t5 : O.le K.zero (VGoodTail.tt K X (8) (15)) := VGoodTail.tail_nn K O X hg (8) 6
  have et5 : (VGoodTail.tt K X (8) (15)) = (K.add (K.mul (co K X (8)) (co K X (14))) (K.neg (K.mul (co K X (7)) (co K X (15))))) := rfl
  have t6 : O.le K.zero (VGoodTail.tt K X (9) (20)) := VGoodTail.tail_nn K O X hg (9) 10
  have et6 : (VGoodTail.tt K X (9) (20)) = (K.add (K.mul (co K X (9)) (co K X (19))) (K.neg (K.mul (co K X (8)) (co K X (20))))) := rfl
  have t7 : O.le K.zero (VGoodTail.tt K X (9) (18)) := VGoodTail.tail_nn K O X hg (9) 8
  have et7 : (VGoodTail.tt K X (9) (18)) = (K.add (K.mul (co K X (9)) (co K X (17))) (K.neg (K.mul (co K X (8)) (co K X (18))))) := rfl
  have t8 : O.le K.zero (VGoodTail.tt K X (9) (16)) := VGoodTail.tail_nn K O X hg (9) 6
  have et8 : (VGoodTail.tt K X (9) (16)) = (K.add (K.mul (co K X (9)) (co K X (15))) (K.neg (K.mul (co K X (8)) (co K X (16))))) := rfl
  have t9 : O.le K.zero (VGoodTail.tt K X (9) (14)) := VGoodTail.tail_nn K O X hg (9) 4
  have et9 : (VGoodTail.tt K X (9) (14)) = (K.add (K.mul (co K X (9)) (co K X (13))) (K.neg (K.mul (co K X (8)) (co K X (14))))) := rfl
  have t10 : O.le K.zero (VGoodTail.tt K X (10) (21)) := VGoodTail.tail_nn K O X hg (10) 10
  have et10 : (VGoodTail.tt K X (10) (21)) = (K.add (K.mul (co K X (10)) (co K X (20))) (K.neg (K.mul (co K X (9)) (co K X (21))))) := rfl
  have t11 : O.le K.zero (VGoodTail.tt K X (10) (19)) := VGoodTail.tail_nn K O X hg (10) 8
  have et11 : (VGoodTail.tt K X (10) (19)) = (K.add (K.mul (co K X (10)) (co K X (18))) (K.neg (K.mul (co K X (9)) (co K X (19))))) := rfl
  have t12 : O.le K.zero (VGoodTail.tt K X (10) (17)) := VGoodTail.tail_nn K O X hg (10) 6
  have et12 : (VGoodTail.tt K X (10) (17)) = (K.add (K.mul (co K X (10)) (co K X (16))) (K.neg (K.mul (co K X (9)) (co K X (17))))) := rfl
  have t13 : O.le K.zero (VGoodTail.tt K X (10) (15)) := VGoodTail.tail_nn K O X hg (10) 4
  have et13 : (VGoodTail.tt K X (10) (15)) = (K.add (K.mul (co K X (10)) (co K X (14))) (K.neg (K.mul (co K X (9)) (co K X (15))))) := rfl
  have t14 : O.le K.zero (VGoodTail.tt K X (10) (13)) := VGoodTail.tail_nn K O X hg (10) 2
  have et14 : (VGoodTail.tt K X (10) (13)) = (K.add (K.mul (co K X (10)) (co K X (12))) (K.neg (K.mul (co K X (9)) (co K X (13))))) := rfl
  have hp : 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.add (VGoodTail.tt K X (6) (17)) (VGoodTail.tt K X (7) (18))) (VGoodTail.tt K X (7) (16))) (VGoodTail.tt K X (8) (19))) (VGoodTail.tt K X (8) (17))) (VGoodTail.tt K X (8) (15))) (VGoodTail.tt K X (9) (20))) (VGoodTail.tt K X (9) (18))) (VGoodTail.tt K X (9) (16))) (VGoodTail.tt K X   -- (+166 chars; full line in the Zenodo source)
    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 _ _ (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 _ _ (TopLC.add_nn K O _ _ (TopLC.add_nn K O _ _ t0 t1) t2) t3) t4) t5) t6) t7) t8) t9) t10) t11) t12) t13) t14)
  refine O.le_trans _ _ _ hp (TripleE.le_of_eq' K O _ _ ?_)
  rw [kv, et0, et1, et2, et3, et4, et5, et6, et7, et8, et9, et10, et11, et12, et13, et14]
  ring_z

theorem kna_0_11 (X : List R) (hg : L1Full.Good K O X) :
    O.le K.zero (CBLib.kern K (Fa K X) (Ga K X) 5 (0) (12)) := by
  have kv : CBLib.kern K (Fa K X) (Ga K X) 5 (0) (12) = (K.add (K.add (K.add (K.add (K.add K.zero (K.add (K.mul (co K X (6)) (co K X (17))) (K.neg (K.mul (co K X (18)) (co K X (5)))))) (K.add (K.mul (co K X (7)) (K.add (co K X (18)) (co K X (16)))) (K.neg (K.mul (K.add (co K X (19)) (co K X (17))) (co K X (6)))))) (K.add (K.mul (co K X (8)) (K.add (K.add (co K X (19)) (co K X (17))) (co K X (15)))  -- (+580 chars; full line in the Zenodo source)
  have t0 : O.le K.zero (VGoodTail.tt K X (6) (18)) := VGoodTail.tail_nn K O X hg (6) 11
  have et0 : (VGoodTail.tt K X (6) (18)) = (K.add (K.mul (co K X (6)) (co K X (17))) (K.neg (K.mul (co K X (5)) (co K X (18))))) := rfl
  have t1 : O.le K.zero (VGoodTail.tt K X (7) (19)) := VGoodTail.tail_nn K O X hg (7) 11
  have et1 : (VGoodTail.tt K X (7) (19)) = (K.add (K.mul (co K X (7)) (co K X (18))) (K.neg (K.mul (co K X (6)) (co K X (19))))) := rfl
  have t2 : O.le K.zero (VGoodTail.tt K X (7) (17)) := VGoodTail.tail_nn K O X hg (7) 9
  have et2 : (VGoodTail.tt K X (7) (17)) = (K.add (K.mul (co K X (7)) (co K X (16))) (K.neg (K.mul (co K X (6)) (co K X (17))))) := rfl
  have t3 : O.le K.zero (VGoodTail.tt K X (8) (20)) := VGoodTail.tail_nn K O X hg (8) 11
  have et3 : (VGoodTail.tt K X (8) (20)) = (K.add (K.mul (co K X (8)) (co K X (19))) (K.neg (K.mul (co K X (7)) (co K X (20))))) := rfl
  have t4 : O.le K.zero (VGoodTail.tt K X (8) (18)) := VGoodTail.tail_nn K O X hg (8) 9
  have et4 : (VGoodTail.tt K X (8) (18)) = (K.add (K.mul (co K X (8)) (co K X (17))) (K.neg (K.mul (co K X (7)) (co K X (18))))) := rfl
  have t5 : O.le K.zero (VGoodTail.tt K X (8) (16)) := VGoodTail.tail_nn K O X hg (8) 7
  have et5 : (VGoodTail.tt K X (8) (16)) = (K.add (K.mul (co K X (8)) (co K X (15))) (K.neg (K.mul (co K X (7)) (co K X (16))))) := rfl
  have t6 : O.le K.zero (VGoodTail.tt K X (9) (21)) := VGoodTail.tail_nn K O X hg (9) 11
  have et6 : (VGoodTail.tt K X (9) (21)) = (K.add (K.mul (co K X (9)) (co K X (20))) (K.neg (K.mul (co K X (8)) (co K X (21))))) := rfl
  have t7 : O.le K.zero (VGoodTail.tt K X (9) (19)) := VGoodTail.tail_nn K O X hg (9) 9
  have et7 : (VGoodTail.tt K X (9) (19)) = (K.add (K.mul (co K X (9)) (co K X (18))) (K.neg (K.mul (co K X (8)) (co K X (19))))) := rfl
  have t8 : O.le K.zero (VGoodTail.tt K X (9) (17)) := VGoodTail.tail_nn K O X hg (9) 7
  have et8 : (VGoodTail.tt K X (9) (17)) = (K.add (K.mul (co K X (9)) (co K X (16))) (K.neg (K.mul (co K X (8)) (co K X (17))))) := rfl
  have t9 : O.le K.zero (VGoodTail.tt K X (9) (15)) := VGoodTail.tail_nn K O X hg (9) 5
  have et9 : (VGoodTail.tt K X (9) (15)) = (K.add (K.mul (co K X (9)) (co K X (14))) (K.neg (K.mul (co K X (8)) (co K X (15))))) := rfl
  have t10 : O.le K.zero (VGoodTail.tt K X (10) (22)) := VGoodTail.tail_nn K O X hg (10) 11
  have et10 : (VGoodTail.tt K X (10) (22)) = (K.add (K.mul (co K X (10)) (co K X (21))) (K.neg (K.mul (co K X (9)) (co K X (22))))) := rfl
  have t11 : O.le K.zero (VGoodTail.tt K X (10) (20)) := VGoodTail.tail_nn K O X hg (10) 9
  have et11 : (VGoodTail.tt K X (10) (20)) = (K.add (K.mul (co K X (10)) (co K X (19))) (K.neg (K.mul (co K X (9)) (co K X (20))))) := rfl
  have t12 : O.le K.zero (VGoodTail.tt K X (10) (18)) := VGoodTail.tail_nn K O X hg (10) 7
  have et12 : (VGoodTail.tt K X (10) (18)) = (K.add (K.mul (co K X (10)) (co K X (17))) (K.neg (K.mul (co K X (9)) (co K X (18))))) := rfl
  have t13 : O.le K.zero (VGoodTail.tt K X (10) (16)) := VGoodTail.tail_nn K O X hg (10) 5
  have et13 : (VGoodTail.tt K X (10) (16)) = (K.add (K.mul (co K X (10)) (co K X (15))) (K.neg (K.mul (co K X (9)) (co K X (16))))) := rfl
  have t14 : O.le K.zero (VGoodTail.tt K X (10) (14)) := VGoodTail.tail_nn K O X hg (10) 3
  have et14 : (VGoodTail.tt K X (10) (14)) = (K.add (K.mul (co K X (10)) (co K X (13))) (K.neg (K.mul (co K X (9)) (co K X (14))))) := rfl
  have hp : 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.add (VGoodTail.tt K X (6) (18)) (VGoodTail.tt K X (7) (19))) (VGoodTail.tt K X (7) (17))) (VGoodTail.tt K X (8) (20))) (VGoodTail.tt K X (8) (18))) (VGoodTail.tt K X (8) (16))) (VGoodTail.tt K X (9) (21))) (VGoodTail.tt K X (9) (19))) (VGoodTail.tt K X (9) (17))) (VGoodTail.tt K X   -- (+166 chars; full line in the Zenodo source)
    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 _ _ (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 _ _ (TopLC.add_nn K O _ _ (TopLC.add_nn K O _ _ t0 t1) t2) t3) t4) t5) t6) t7) t8) t9) t10) t11) t12) t13) t14)
  refine O.le_trans _ _ _ hp (TripleE.le_of_eq' K O _ _ ?_)
  rw [kv, et0, et1, et2, et3, et4, et5, et6, et7, et8, et9, et10, et11, et12, et13, et14]
  ring_z

theorem kna_0_12 (X : List R) (hg : L1Full.Good K O X) :
    O.le K.zero (CBLib.kern K (Fa K X) (Ga K X) 5 (0) (13)) := by
  have kv : CBLib.kern K (Fa K X) (Ga K X) 5 (0) (13) = (K.add (K.add (K.add (K.add (K.add K.zero (K.add (K.mul (co K X (6)) (co K X (18))) (K.neg (K.mul (co K X (19)) (co K X (5)))))) (K.add (K.mul (co K X (7)) (K.add (co K X (19)) (co K X (17)))) (K.neg (K.mul (K.add (co K X (20)) (co K X (18))) (co K X (6)))))) (K.add (K.mul (co K X (8)) (K.add (K.add (co K X (20)) (co K X (18))) (co K X (16)))  -- (+580 chars; full line in the Zenodo source)
  have t0 : O.le K.zero (VGoodTail.tt K X (6) (19)) := VGoodTail.tail_nn K O X hg (6) 12
  have et0 : (VGoodTail.tt K X (6) (19)) = (K.add (K.mul (co K X (6)) (co K X (18))) (K.neg (K.mul (co K X (5)) (co K X (19))))) := rfl
  have t1 : O.le K.zero (VGoodTail.tt K X (7) (20)) := VGoodTail.tail_nn K O X hg (7) 12
  have et1 : (VGoodTail.tt K X (7) (20)) = (K.add (K.mul (co K X (7)) (co K X (19))) (K.neg (K.mul (co K X (6)) (co K X (20))))) := rfl
  have t2 : O.le K.zero (VGoodTail.tt K X (7) (18)) := VGoodTail.tail_nn K O X hg (7) 10
  have et2 : (VGoodTail.tt K X (7) (18)) = (K.add (K.mul (co K X (7)) (co K X (17))) (K.neg (K.mul (co K X (6)) (co K X (18))))) := rfl
  have t3 : O.le K.zero (VGoodTail.tt K X (8) (21)) := VGoodTail.tail_nn K O X hg (8) 12
  have et3 : (VGoodTail.tt K X (8) (21)) = (K.add (K.mul (co K X (8)) (co K X (20))) (K.neg (K.mul (co K X (7)) (co K X (21))))) := rfl
  have t4 : O.le K.zero (VGoodTail.tt K X (8) (19)) := VGoodTail.tail_nn K O X hg (8) 10
  have et4 : (VGoodTail.tt K X (8) (19)) = (K.add (K.mul (co K X (8)) (co K X (18))) (K.neg (K.mul (co K X (7)) (co K X (19))))) := rfl
  have t5 : O.le K.zero (VGoodTail.tt K X (8) (17)) := VGoodTail.tail_nn K O X hg (8) 8
  have et5 : (VGoodTail.tt K X (8) (17)) = (K.add (K.mul (co K X (8)) (co K X (16))) (K.neg (K.mul (co K X (7)) (co K X (17))))) := rfl
  have t6 : O.le K.zero (VGoodTail.tt K X (9) (22)) := VGoodTail.tail_nn K O X hg (9) 12
  have et6 : (VGoodTail.tt K X (9) (22)) = (K.add (K.mul (co K X (9)) (co K X (21))) (K.neg (K.mul (co K X (8)) (co K X (22))))) := rfl
  have t7 : O.le K.zero (VGoodTail.tt K X (9) (20)) := VGoodTail.tail_nn K O X hg (9) 10
  have et7 : (VGoodTail.tt K X (9) (20)) = (K.add (K.mul (co K X (9)) (co K X (19))) (K.neg (K.mul (co K X (8)) (co K X (20))))) := rfl
  have t8 : O.le K.zero (VGoodTail.tt K X (9) (18)) := VGoodTail.tail_nn K O X hg (9) 8
  have et8 : (VGoodTail.tt K X (9) (18)) = (K.add (K.mul (co K X (9)) (co K X (17))) (K.neg (K.mul (co K X (8)) (co K X (18))))) := rfl
  have t9 : O.le K.zero (VGoodTail.tt K X (9) (16)) := VGoodTail.tail_nn K O X hg (9) 6
  have et9 : (VGoodTail.tt K X (9) (16)) = (K.add (K.mul (co K X (9)) (co K X (15))) (K.neg (K.mul (co K X (8)) (co K X (16))))) := rfl
  have t10 : O.le K.zero (VGoodTail.tt K X (10) (23)) := VGoodTail.tail_nn K O X hg (10) 12
  have et10 : (VGoodTail.tt K X (10) (23)) = (K.add (K.mul (co K X (10)) (co K X (22))) (K.neg (K.mul (co K X (9)) (co K X (23))))) := rfl
  have t11 : O.le K.zero (VGoodTail.tt K X (10) (21)) := VGoodTail.tail_nn K O X hg (10) 10
  have et11 : (VGoodTail.tt K X (10) (21)) = (K.add (K.mul (co K X (10)) (co K X (20))) (K.neg (K.mul (co K X (9)) (co K X (21))))) := rfl
  have t12 : O.le K.zero (VGoodTail.tt K X (10) (19)) := VGoodTail.tail_nn K O X hg (10) 8
  have et12 : (VGoodTail.tt K X (10) (19)) = (K.add (K.mul (co K X (10)) (co K X (18))) (K.neg (K.mul (co K X (9)) (co K X (19))))) := rfl
  have t13 : O.le K.zero (VGoodTail.tt K X (10) (17)) := VGoodTail.tail_nn K O X hg (10) 6
  have et13 : (VGoodTail.tt K X (10) (17)) = (K.add (K.mul (co K X (10)) (co K X (16))) (K.neg (K.mul (co K X (9)) (co K X (17))))) := rfl
  have t14 : O.le K.zero (VGoodTail.tt K X (10) (15)) := VGoodTail.tail_nn K O X hg (10) 4
  have et14 : (VGoodTail.tt K X (10) (15)) = (K.add (K.mul (co K X (10)) (co K X (14))) (K.neg (K.mul (co K X (9)) (co K X (15))))) := rfl
  have hp : 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.add (VGoodTail.tt K X (6) (19)) (VGoodTail.tt K X (7) (20))) (VGoodTail.tt K X (7) (18))) (VGoodTail.tt K X (8) (21))) (VGoodTail.tt K X (8) (19))) (VGoodTail.tt K X (8) (17))) (VGoodTail.tt K X (9) (22))) (VGoodTail.tt K X (9) (20))) (VGoodTail.tt K X (9) (18))) (VGoodTail.tt K X   -- (+166 chars; full line in the Zenodo source)
    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 _ _ (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 _ _ (TopLC.add_nn K O _ _ (TopLC.add_nn K O _ _ t0 t1) t2) t3) t4) t5) t6) t7) t8) t9) t10) t11) t12) t13) t14)
  refine O.le_trans _ _ _ hp (TripleE.le_of_eq' K O _ _ ?_)
  rw [kv, et0, et1, et2, et3, et4, et5, et6, et7, et8, et9, et10, et11, et12, et13, et14]
  ring_z

theorem kna_1_0 (X : List R) (hg : L1Full.Good K O X) :
    O.le K.zero (CBLib.kern K (Fa K X) (Ga K X) 5 (1) (2)) := by
  have kv : CBLib.kern K (Fa K X) (Ga K X) 5 (1) (2) = (K.add (K.add (K.add (K.add (K.add K.zero (K.add (K.mul (co K X (7)) (co K X (7))) (K.neg (K.mul (co K X (8)) (co K X (6)))))) (K.add (K.mul (K.add (co K X (8)) (co K X (6))) (K.add (co K X (8)) (co K X (6)))) (K.neg (K.mul (K.add (co K X (9)) (co K X (7))) (K.add (co K X (7)) (co K X (5))))))) (K.add (K.mul (K.add (co K X (9)) (co K X (7))) (  -- (+599 chars; full line in the Zenodo source)
  have t0 : O.le K.zero (VGoodTail.tt K X (9) (10)) := VGoodTail.tail_nn K O X hg (9) 0
  have et0 : (VGoodTail.tt K X (9) (10)) = (K.add (K.mul (co K X (9)) (co K X (9))) (K.neg (K.mul (co K X (8)) (co K X (10))))) := rfl
  have t1 : O.le K.zero (VGoodTail.tt K X (11) (12)) := VGoodTail.tail_nn K O X hg (11) 0
  have et1 : (VGoodTail.tt K X (11) (12)) = (K.add (K.mul (co K X (11)) (co K X (11))) (K.neg (K.mul (co K X (10)) (co K X (12))))) := rfl
  have t2 : O.le K.zero (VGoodTail.tt K X (9) (12)) := VGoodTail.tail_nn K O X hg (9) 2
  have et2 : (VGoodTail.tt K X (9) (12)) = (K.add (K.mul (co K X (9)) (co K X (11))) (K.neg (K.mul (co K X (8)) (co K X (12))))) := rfl
  have hp : O.le K.zero (K.add (K.add (VGoodTail.tt K X (9) (10)) (VGoodTail.tt K X (11) (12))) (VGoodTail.tt K X (9) (12))) := by
    exact (TopLC.add_nn K O _ _ (TopLC.add_nn K O _ _ t0 t1) t2)
  refine O.le_trans _ _ _ hp (TripleE.le_of_eq' K O _ _ ?_)
  rw [kv, et0, et1, et2]
  ring_z

theorem kna_1_1 (X : List R) (hg : L1Full.Good K O X) :
    O.le K.zero (CBLib.kern K (Fa K X) (Ga K X) 5 (1) (3)) := by
  have kv : CBLib.kern K (Fa K X) (Ga K X) 5 (1) (3) = (K.add (K.add (K.add (K.add (K.add K.zero (K.add (K.mul (co K X (7)) (co K X (8))) (K.neg (K.mul (co K X (9)) (co K X (6)))))) (K.add (K.mul (K.add (co K X (8)) (co K X (6))) (K.add (co K X (9)) (co K X (7)))) (K.neg (K.mul (K.add (co K X (10)) (co K X (8))) (K.add (co K X (7)) (co K X (5))))))) (K.add (K.mul (K.add (co K X (9)) (co K X (7)))   -- (+687 chars; full line in the Zenodo source)
  have t0 : O.le K.zero (VGoodTail.tt K X (8) (10)) := VGoodTail.tail_nn K O X hg (8) 1
  have et0 : (VGoodTail.tt K X (8) (10)) = (K.add (K.mul (co K X (8)) (co K X (9))) (K.neg (K.mul (co K X (7)) (co K X (10))))) := rfl
  have t1 : O.le K.zero (VGoodTail.tt K X (9) (11)) := VGoodTail.tail_nn K O X hg (9) 1
  have et1 : (VGoodTail.tt K X (9) (11)) = (K.add (K.mul (co K X (9)) (co K X (10))) (K.neg (K.mul (co K X (8)) (co K X (11))))) := rfl
  have t2 : O.le K.zero (VGoodTail.tt K X (10) (12)) := VGoodTail.tail_nn K O X hg (10) 1
  have et2 : (VGoodTail.tt K X (10) (12)) = (K.add (K.mul (co K X (10)) (co K X (11))) (K.neg (K.mul (co K X (9)) (co K X (12))))) := rfl
  have t3 : O.le K.zero (VGoodTail.tt K X (8) (12)) := VGoodTail.tail_nn K O X hg (8) 3
  have et3 : (VGoodTail.tt K X (8) (12)) = (K.add (K.mul (co K X (8)) (co K X (11))) (K.neg (K.mul (co K X (7)) (co K X (12))))) := rfl
  have t4 : O.le K.zero (VGoodTail.tt K X (11) (13)) := VGoodTail.tail_nn K O X hg (11) 1
  have et4 : (VGoodTail.tt K X (11) (13)) = (K.add (K.mul (co K X (11)) (co K X (12))) (K.neg (K.mul (co K X (10)) (co K X (13))))) := rfl
  have t5 : O.le K.zero (VGoodTail.tt K X (9) (13)) := VGoodTail.tail_nn K O X hg (9) 3
  have et5 : (VGoodTail.tt K X (9) (13)) = (K.add (K.mul (co K X (9)) (co K X (12))) (K.neg (K.mul (co K X (8)) (co K X (13))))) := rfl
  have hp : O.le K.zero (K.add (K.add (K.add (K.add (K.add (VGoodTail.tt K X (8) (10)) (VGoodTail.tt K X (9) (11))) (VGoodTail.tt K X (10) (12))) (VGoodTail.tt K X (8) (12))) (VGoodTail.tt K X (11) (13))) (VGoodTail.tt K X (9) (13))) := by
    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 _ _ t0 t1) t2) t3) t4) t5)
  refine O.le_trans _ _ _ hp (TripleE.le_of_eq' K O _ _ ?_)
  rw [kv, et0, et1, et2, et3, et4, et5]
  ring_z

theorem kna_1_2 (X : List R) (hg : L1Full.Good K O X) :
    O.le K.zero (CBLib.kern K (Fa K X) (Ga K X) 5 (1) (4)) := by
  have kv : CBLib.kern K (Fa K X) (Ga K X) 5 (1) (4) = (K.add (K.add (K.add (K.add (K.add K.zero (K.add (K.mul (co K X (7)) (co K X (9))) (K.neg (K.mul (co K X (10)) (co K X (6)))))) (K.add (K.mul (K.add (co K X (8)) (co K X (6))) (K.add (co K X (10)) (co K X (8)))) (K.neg (K.mul (K.add (co K X (11)) (co K X (9))) (K.add (co K X (7)) (co K X (5))))))) (K.add (K.mul (K.add (co K X (9)) (co K X (7))  -- (+734 chars; full line in the Zenodo source)
  have t0 : O.le K.zero (VGoodTail.tt K X (7) (10)) := VGoodTail.tail_nn K O X hg (7) 2
  have et0 : (VGoodTail.tt K X (7) (10)) = (K.add (K.mul (co K X (7)) (co K X (9))) (K.neg (K.mul (co K X (6)) (co K X (10))))) := rfl
  have t1 : O.le K.zero (VGoodTail.tt K X (8) (11)) := VGoodTail.tail_nn K O X hg (8) 2
  have et1 : (VGoodTail.tt K X (8) (11)) = (K.add (K.mul (co K X (8)) (co K X (10))) (K.neg (K.mul (co K X (7)) (co K X (11))))) := rfl
  have t2 : O.le K.zero (VGoodTail.tt K X (9) (12)) := VGoodTail.tail_nn K O X hg (9) 2
  have et2 : (VGoodTail.tt K X (9) (12)) = (K.add (K.mul (co K X (9)) (co K X (11))) (K.neg (K.mul (co K X (8)) (co K X (12))))) := rfl
  have t3 : O.le K.zero (VGoodTail.tt K X (9) (10)) := VGoodTail.tail_nn K O X hg (9) 0
  have et3 : (VGoodTail.tt K X (9) (10)) = (K.add (K.mul (co K X (9)) (co K X (9))) (K.neg (K.mul (co K X (8)) (co K X (10))))) := rfl
  have t4 : O.le K.zero (VGoodTail.tt K X (7) (12)) := VGoodTail.tail_nn K O X hg (7) 4
  have et4 : (VGoodTail.tt K X (7) (12)) = (K.add (K.mul (co K X (7)) (co K X (11))) (K.neg (K.mul (co K X (6)) (co K X (12))))) := rfl
  have t5 : O.le K.zero (VGoodTail.tt K X (10) (13)) := VGoodTail.tail_nn K O X hg (10) 2
  have et5 : (VGoodTail.tt K X (10) (13)) = (K.add (K.mul (co K X (10)) (co K X (12))) (K.neg (K.mul (co K X (9)) (co K X (13))))) := rfl
  have t6 : O.le K.zero (VGoodTail.tt K X (8) (13)) := VGoodTail.tail_nn K O X hg (8) 4
  have et6 : (VGoodTail.tt K X (8) (13)) = (K.add (K.mul (co K X (8)) (co K X (12))) (K.neg (K.mul (co K X (7)) (co K X (13))))) := rfl
  have t7 : O.le K.zero (VGoodTail.tt K X (11) (14)) := VGoodTail.tail_nn K O X hg (11) 2
  have et7 : (VGoodTail.tt K X (11) (14)) = (K.add (K.mul (co K X (11)) (co K X (13))) (K.neg (K.mul (co K X (10)) (co K X (14))))) := rfl
  have t8 : O.le K.zero (VGoodTail.tt K X (11) (12)) := VGoodTail.tail_nn K O X hg (11) 0
  have et8 : (VGoodTail.tt K X (11) (12)) = (K.add (K.mul (co K X (11)) (co K X (11))) (K.neg (K.mul (co K X (10)) (co K X (12))))) := rfl
  have t9 : O.le K.zero (VGoodTail.tt K X (9) (14)) := VGoodTail.tail_nn K O X hg (9) 4
  have et9 : (VGoodTail.tt K X (9) (14)) = (K.add (K.mul (co K X (9)) (co K X (13))) (K.neg (K.mul (co K X (8)) (co K X (14))))) := rfl
  have hp : O.le K.zero (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (VGoodTail.tt K X (7) (10)) (VGoodTail.tt K X (8) (11))) (VGoodTail.tt K X (9) (12))) (VGoodTail.tt K X (9) (12))) (VGoodTail.tt K X (9) (10))) (VGoodTail.tt K X (7) (12))) (VGoodTail.tt K X (10) (13))) (VGoodTail.tt K X (8) (13))) (VGoodTail.tt K X (11) (14))) (VGoodTail.tt K X (11) (12))) (VGoodTail.tt   -- (+20 chars; full line in the Zenodo source)
    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 _ _ (TopLC.add_nn K O _ _ (TopLC.add_nn K O _ _ (TopLC.add_nn K O _ _ (TopLC.add_nn K O _ _ t0 t1) t2) t2) t3) t4) t5) t6) t7) t8) t9)
  refine O.le_trans _ _ _ hp (TripleE.le_of_eq' K O _ _ ?_)
  rw [kv, et0, et1, et2, et3, et4, et5, et6, et7, et8, et9]
  ring_z

theorem kna_1_3 (X : List R) (hg : L1Full.Good K O X) :
    O.le K.zero (CBLib.kern K (Fa K X) (Ga K X) 5 (1) (5)) := by
  have kv : CBLib.kern K (Fa K X) (Ga K X) 5 (1) (5) = (K.add (K.add (K.add (K.add (K.add K.zero (K.add (K.mul (co K X (7)) (co K X (10))) (K.neg (K.mul (co K X (11)) (co K X (6)))))) (K.add (K.mul (K.add (co K X (8)) (co K X (6))) (K.add (co K X (11)) (co K X (9)))) (K.neg (K.mul (K.add (co K X (12)) (co K X (10))) (K.add (co K X (7)) (co K X (5))))))) (K.add (K.mul (K.add (co K X (9)) (co K X (7  -- (+739 chars; full line in the Zenodo source)
  have t0 : O.le K.zero (VGoodTail.tt K X (7) (11)) := VGoodTail.tail_nn K O X hg (7) 3
  have et0 : (VGoodTail.tt K X (7) (11)) = (K.add (K.mul (co K X (7)) (co K X (10))) (K.neg (K.mul (co K X (6)) (co K X (11))))) := rfl
  have t1 : O.le K.zero (VGoodTail.tt K X (8) (12)) := VGoodTail.tail_nn K O X hg (8) 3
  have et1 : (VGoodTail.tt K X (8) (12)) = (K.add (K.mul (co K X (8)) (co K X (11))) (K.neg (K.mul (co K X (7)) (co K X (12))))) := rfl
  have t2 : O.le K.zero (VGoodTail.tt K X (8) (10)) := VGoodTail.tail_nn K O X hg (8) 1
  have et2 : (VGoodTail.tt K X (8) (10)) = (K.add (K.mul (co K X (8)) (co K X (9))) (K.neg (K.mul (co K X (7)) (co K X (10))))) := rfl
  have t3 : O.le K.zero (VGoodTail.tt K X (6) (12)) := VGoodTail.tail_nn K O X hg (6) 5
  have et3 : (VGoodTail.tt K X (6) (12)) = (K.add (K.mul (co K X (6)) (co K X (11))) (K.neg (K.mul (co K X (5)) (co K X (12))))) := rfl
  have t4 : O.le K.zero (VGoodTail.tt K X (6) (10)) := VGoodTail.tail_nn K O X hg (6) 3
  have et4 : (VGoodTail.tt K X (6) (10)) = (K.add (K.mul (co K X (6)) (co K X (9))) (K.neg (K.mul (co K X (5)) (co K X (10))))) := rfl
  have t5 : O.le K.zero (VGoodTail.tt K X (9) (13)) := VGoodTail.tail_nn K O X hg (9) 3
  have et5 : (VGoodTail.tt K X (9) (13)) = (K.add (K.mul (co K X (9)) (co K X (12))) (K.neg (K.mul (co K X (8)) (co K X (13))))) := rfl
  have t6 : O.le K.zero (VGoodTail.tt K X (9) (11)) := VGoodTail.tail_nn K O X hg (9) 1
  have et6 : (VGoodTail.tt K X (9) (11)) = (K.add (K.mul (co K X (9)) (co K X (10))) (K.neg (K.mul (co K X (8)) (co K X (11))))) := rfl
  have t7 : O.le K.zero (VGoodTail.tt K X (7) (13)) := VGoodTail.tail_nn K O X hg (7) 5
  have et7 : (VGoodTail.tt K X (7) (13)) = (K.add (K.mul (co K X (7)) (co K X (12))) (K.neg (K.mul (co K X (6)) (co K X (13))))) := rfl
  have t8 : O.le K.zero (VGoodTail.tt K X (10) (14)) := VGoodTail.tail_nn K O X hg (10) 3
  have et8 : (VGoodTail.tt K X (10) (14)) = (K.add (K.mul (co K X (10)) (co K X (13))) (K.neg (K.mul (co K X (9)) (co K X (14))))) := rfl
  have t9 : O.le K.zero (VGoodTail.tt K X (10) (12)) := VGoodTail.tail_nn K O X hg (10) 1
  have et9 : (VGoodTail.tt K X (10) (12)) = (K.add (K.mul (co K X (10)) (co K X (11))) (K.neg (K.mul (co K X (9)) (co K X (12))))) := rfl
  have t10 : O.le K.zero (VGoodTail.tt K X (8) (14)) := VGoodTail.tail_nn K O X hg (8) 5
  have et10 : (VGoodTail.tt K X (8) (14)) = (K.add (K.mul (co K X (8)) (co K X (13))) (K.neg (K.mul (co K X (7)) (co K X (14))))) := rfl
  have t11 : O.le K.zero (VGoodTail.tt K X (11) (15)) := VGoodTail.tail_nn K O X hg (11) 3
  have et11 : (VGoodTail.tt K X (11) (15)) = (K.add (K.mul (co K X (11)) (co K X (14))) (K.neg (K.mul (co K X (10)) (co K X (15))))) := rfl
  have t12 : O.le K.zero (VGoodTail.tt K X (11) (13)) := VGoodTail.tail_nn K O X hg (11) 1
  have et12 : (VGoodTail.tt K X (11) (13)) = (K.add (K.mul (co K X (11)) (co K X (12))) (K.neg (K.mul (co K X (10)) (co K X (13))))) := rfl
  have t13 : O.le K.zero (VGoodTail.tt K X (9) (15)) := VGoodTail.tail_nn K O X hg (9) 5
  have et13 : (VGoodTail.tt K X (9) (15)) = (K.add (K.mul (co K X (9)) (co K X (14))) (K.neg (K.mul (co K X (8)) (co K X (15))))) := rfl
  have hp : 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.add (K.add (VGoodTail.tt K X (7) (11)) (VGoodTail.tt K X (8) (12))) (VGoodTail.tt K X (8) (12))) (VGoodTail.tt K X (8) (10))) (VGoodTail.tt K X (6) (12))) (VGoodTail.tt K X (6) (10))) (VGoodTail.tt K X (9) (13))) (VGoodTail.tt K X (9) (13))) (VGoodTail.tt K X (9) (11))) (VGoodTail.  -- (+201 chars; full line in the Zenodo source)
    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 _ _ (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 _ _ (TopLC.add_nn K O _ _ (TopLC.add_nn K O _ _ (TopLC.add_nn K O _ _ t0 t1) t1) t2) t3) t4) t5) t5) t6) t7) t8) t9) t10) t11) t12  -- (+6 chars; full line in the Zenodo source)
  refine O.le_trans _ _ _ hp (TripleE.le_of_eq' K O _ _ ?_)
  rw [kv, et0, et1, et2, et3, et4, et5, et6, et7, et8, et9, et10, et11, et12, et13]
  ring_z

theorem kna_1_4 (X : List R) (hg : L1Full.Good K O X) :
    O.le K.zero (CBLib.kern K (Fa K X) (Ga K X) 5 (1) (6)) := by
  have kv : CBLib.kern K (Fa K X) (Ga K X) 5 (1) (6) = (K.add (K.add (K.add (K.add (K.add K.zero (K.add (K.mul (co K X (7)) (co K X (11))) (K.neg (K.mul (co K X (12)) (co K X (6)))))) (K.add (K.mul (K.add (co K X (8)) (co K X (6))) (K.add (co K X (12)) (co K X (10)))) (K.neg (K.mul (K.add (co K X (13)) (co K X (11))) (K.add (co K X (7)) (co K X (5))))))) (K.add (K.mul (K.add (co K X (9)) (co K X (  -- (+743 chars; full line in the Zenodo source)
  have t0 : O.le K.zero (VGoodTail.tt K X (7) (12)) := VGoodTail.tail_nn K O X hg (7) 4
  have et0 : (VGoodTail.tt K X (7) (12)) = (K.add (K.mul (co K X (7)) (co K X (11))) (K.neg (K.mul (co K X (6)) (co K X (12))))) := rfl
  have t1 : O.le K.zero (VGoodTail.tt K X (8) (13)) := VGoodTail.tail_nn K O X hg (8) 4
  have et1 : (VGoodTail.tt K X (8) (13)) = (K.add (K.mul (co K X (8)) (co K X (12))) (K.neg (K.mul (co K X (7)) (co K X (13))))) := rfl
  have t2 : O.le K.zero (VGoodTail.tt K X (8) (11)) := VGoodTail.tail_nn K O X hg (8) 2
  have et2 : (VGoodTail.tt K X (8) (11)) = (K.add (K.mul (co K X (8)) (co K X (10))) (K.neg (K.mul (co K X (7)) (co K X (11))))) := rfl
  have t3 : O.le K.zero (VGoodTail.tt K X (6) (13)) := VGoodTail.tail_nn K O X hg (6) 6
  have et3 : (VGoodTail.tt K X (6) (13)) = (K.add (K.mul (co K X (6)) (co K X (12))) (K.neg (K.mul (co K X (5)) (co K X (13))))) := rfl
  have t4 : O.le K.zero (VGoodTail.tt K X (6) (11)) := VGoodTail.tail_nn K O X hg (6) 4
  have et4 : (VGoodTail.tt K X (6) (11)) = (K.add (K.mul (co K X (6)) (co K X (10))) (K.neg (K.mul (co K X (5)) (co K X (11))))) := rfl
  have t5 : O.le K.zero (VGoodTail.tt K X (9) (14)) := VGoodTail.tail_nn K O X hg (9) 4
  have et5 : (VGoodTail.tt K X (9) (14)) = (K.add (K.mul (co K X (9)) (co K X (13))) (K.neg (K.mul (co K X (8)) (co K X (14))))) := rfl
  have t6 : O.le K.zero (VGoodTail.tt K X (9) (12)) := VGoodTail.tail_nn K O X hg (9) 2
  have et6 : (VGoodTail.tt K X (9) (12)) = (K.add (K.mul (co K X (9)) (co K X (11))) (K.neg (K.mul (co K X (8)) (co K X (12))))) := rfl
  have t7 : O.le K.zero (VGoodTail.tt K X (9) (10)) := VGoodTail.tail_nn K O X hg (9) 0
  have et7 : (VGoodTail.tt K X (9) (10)) = (K.add (K.mul (co K X (9)) (co K X (9))) (K.neg (K.mul (co K X (8)) (co K X (10))))) := rfl
  have t8 : O.le K.zero (VGoodTail.tt K X (7) (14)) := VGoodTail.tail_nn K O X hg (7) 6
  have et8 : (VGoodTail.tt K X (7) (14)) = (K.add (K.mul (co K X (7)) (co K X (13))) (K.neg (K.mul (co K X (6)) (co K X (14))))) := rfl
  have t9 : O.le K.zero (VGoodTail.tt K X (7) (10)) := VGoodTail.tail_nn K O X hg (7) 2
  have et9 : (VGoodTail.tt K X (7) (10)) = (K.add (K.mul (co K X (7)) (co K X (9))) (K.neg (K.mul (co K X (6)) (co K X (10))))) := rfl
  have t10 : O.le K.zero (VGoodTail.tt K X (10) (15)) := VGoodTail.tail_nn K O X hg (10) 4
  have et10 : (VGoodTail.tt K X (10) (15)) = (K.add (K.mul (co K X (10)) (co K X (14))) (K.neg (K.mul (co K X (9)) (co K X (15))))) := rfl
  have t11 : O.le K.zero (VGoodTail.tt K X (10) (13)) := VGoodTail.tail_nn K O X hg (10) 2
  have et11 : (VGoodTail.tt K X (10) (13)) = (K.add (K.mul (co K X (10)) (co K X (12))) (K.neg (K.mul (co K X (9)) (co K X (13))))) := rfl
  have t12 : O.le K.zero (VGoodTail.tt K X (8) (15)) := VGoodTail.tail_nn K O X hg (8) 6
  have et12 : (VGoodTail.tt K X (8) (15)) = (K.add (K.mul (co K X (8)) (co K X (14))) (K.neg (K.mul (co K X (7)) (co K X (15))))) := rfl
  have t13 : O.le K.zero (VGoodTail.tt K X (11) (16)) := VGoodTail.tail_nn K O X hg (11) 4
  have et13 : (VGoodTail.tt K X (11) (16)) = (K.add (K.mul (co K X (11)) (co K X (15))) (K.neg (K.mul (co K X (10)) (co K X (16))))) := rfl
  have t14 : O.le K.zero (VGoodTail.tt K X (11) (14)) := VGoodTail.tail_nn K O X hg (11) 2
  have et14 : (VGoodTail.tt K X (11) (14)) = (K.add (K.mul (co K X (11)) (co K X (13))) (K.neg (K.mul (co K X (10)) (co K X (14))))) := rfl
  have t15 : O.le K.zero (VGoodTail.tt K X (11) (12)) := VGoodTail.tail_nn K O X hg (11) 0
  have et15 : (VGoodTail.tt K X (11) (12)) = (K.add (K.mul (co K X (11)) (co K X (11))) (K.neg (K.mul (co K X (10)) (co K X (12))))) := rfl
  have t16 : O.le K.zero (VGoodTail.tt K X (9) (16)) := VGoodTail.tail_nn K O X hg (9) 6
  have et16 : (VGoodTail.tt K X (9) (16)) = (K.add (K.mul (co K X (9)) (co K X (15))) (K.neg (K.mul (co K X (8)) (co K X (16))))) := rfl
  have hp : 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.add (K.add (K.add (K.add (K.add (K.add (K.add (VGoodTail.tt K X (7) (12)) (VGoodTail.tt K X (7) (12))) (VGoodTail.tt K X (8) (13))) (VGoodTail.tt K X (8) (13))) (VGoodTail.tt K X (8) (11))) (VGoodTail.tt K X (6) (13))) (VGoodTail.tt K X (6) (11))) (VGoodTail.tt K X (9) (14))) (VGoo  -- (+382 chars; full line in the Zenodo source)
    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 _ _ (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 _ _ (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   -- (+139 chars; full line in the Zenodo source)
  refine O.le_trans _ _ _ hp (TripleE.le_of_eq' K O _ _ ?_)
  rw [kv, et0, et1, et2, et3, et4, et5, et6, et7, et8, et9, et10, et11, et12, et13, et14, et15, et16]
  ring_z

theorem kna_1_5 (X : List R) (hg : L1Full.Good K O X) :
    O.le K.zero (CBLib.kern K (Fa K X) (Ga K X) 5 (1) (7)) := by
  have kv : CBLib.kern K (Fa K X) (Ga K X) 5 (1) (7) = (K.add (K.add (K.add (K.add (K.add K.zero (K.add (K.mul (co K X (7)) (co K X (12))) (K.neg (K.mul (co K X (13)) (co K X (6)))))) (K.add (K.mul (K.add (co K X (8)) (co K X (6))) (K.add (co K X (13)) (co K X (11)))) (K.neg (K.mul (K.add (co K X (14)) (co K X (12))) (K.add (co K X (7)) (co K X (5))))))) (K.add (K.mul (K.add (co K X (9)) (co K X (  -- (+746 chars; full line in the Zenodo source)
  have t0 : O.le K.zero (VGoodTail.tt K X (7) (13)) := VGoodTail.tail_nn K O X hg (7) 5
  have et0 : (VGoodTail.tt K X (7) (13)) = (K.add (K.mul (co K X (7)) (co K X (12))) (K.neg (K.mul (co K X (6)) (co K X (13))))) := rfl
  have t1 : O.le K.zero (VGoodTail.tt K X (8) (14)) := VGoodTail.tail_nn K O X hg (8) 5
  have et1 : (VGoodTail.tt K X (8) (14)) = (K.add (K.mul (co K X (8)) (co K X (13))) (K.neg (K.mul (co K X (7)) (co K X (14))))) := rfl
  have t2 : O.le K.zero (VGoodTail.tt K X (8) (12)) := VGoodTail.tail_nn K O X hg (8) 3
  have et2 : (VGoodTail.tt K X (8) (12)) = (K.add (K.mul (co K X (8)) (co K X (11))) (K.neg (K.mul (co K X (7)) (co K X (12))))) := rfl
  have t3 : O.le K.zero (VGoodTail.tt K X (6) (14)) := VGoodTail.tail_nn K O X hg (6) 7
  have et3 : (VGoodTail.tt K X (6) (14)) = (K.add (K.mul (co K X (6)) (co K X (13))) (K.neg (K.mul (co K X (5)) (co K X (14))))) := rfl
  have t4 : O.le K.zero (VGoodTail.tt K X (6) (12)) := VGoodTail.tail_nn K O X hg (6) 5
  have et4 : (VGoodTail.tt K X (6) (12)) = (K.add (K.mul (co K X (6)) (co K X (11))) (K.neg (K.mul (co K X (5)) (co K X (12))))) := rfl
  have t5 : O.le K.zero (VGoodTail.tt K X (9) (15)) := VGoodTail.tail_nn K O X hg (9) 5
  have et5 : (VGoodTail.tt K X (9) (15)) = (K.add (K.mul (co K X (9)) (co K X (14))) (K.neg (K.mul (co K X (8)) (co K X (15))))) := rfl
  have t6 : O.le K.zero (VGoodTail.tt K X (9) (13)) := VGoodTail.tail_nn K O X hg (9) 3
  have et6 : (VGoodTail.tt K X (9) (13)) = (K.add (K.mul (co K X (9)) (co K X (12))) (K.neg (K.mul (co K X (8)) (co K X (13))))) := rfl
  have t7 : O.le K.zero (VGoodTail.tt K X (9) (11)) := VGoodTail.tail_nn K O X hg (9) 1
  have et7 : (VGoodTail.tt K X (9) (11)) = (K.add (K.mul (co K X (9)) (co K X (10))) (K.neg (K.mul (co K X (8)) (co K X (11))))) := rfl
  have t8 : O.le K.zero (VGoodTail.tt K X (7) (15)) := VGoodTail.tail_nn K O X hg (7) 7
  have et8 : (VGoodTail.tt K X (7) (15)) = (K.add (K.mul (co K X (7)) (co K X (14))) (K.neg (K.mul (co K X (6)) (co K X (15))))) := rfl
  have t9 : O.le K.zero (VGoodTail.tt K X (7) (11)) := VGoodTail.tail_nn K O X hg (7) 3
  have et9 : (VGoodTail.tt K X (7) (11)) = (K.add (K.mul (co K X (7)) (co K X (10))) (K.neg (K.mul (co K X (6)) (co K X (11))))) := rfl
  have t10 : O.le K.zero (VGoodTail.tt K X (10) (16)) := VGoodTail.tail_nn K O X hg (10) 5
  have et10 : (VGoodTail.tt K X (10) (16)) = (K.add (K.mul (co K X (10)) (co K X (15))) (K.neg (K.mul (co K X (9)) (co K X (16))))) := rfl
  have t11 : O.le K.zero (VGoodTail.tt K X (10) (14)) := VGoodTail.tail_nn K O X hg (10) 3
  have et11 : (VGoodTail.tt K X (10) (14)) = (K.add (K.mul (co K X (10)) (co K X (13))) (K.neg (K.mul (co K X (9)) (co K X (14))))) := rfl
  have t12 : O.le K.zero (VGoodTail.tt K X (10) (12)) := VGoodTail.tail_nn K O X hg (10) 1
  have et12 : (VGoodTail.tt K X (10) (12)) = (K.add (K.mul (co K X (10)) (co K X (11))) (K.neg (K.mul (co K X (9)) (co K X (12))))) := rfl
  have t13 : O.le K.zero (VGoodTail.tt K X (8) (16)) := VGoodTail.tail_nn K O X hg (8) 7
  have et13 : (VGoodTail.tt K X (8) (16)) = (K.add (K.mul (co K X (8)) (co K X (15))) (K.neg (K.mul (co K X (7)) (co K X (16))))) := rfl
  have t14 : O.le K.zero (VGoodTail.tt K X (8) (10)) := VGoodTail.tail_nn K O X hg (8) 1
  have et14 : (VGoodTail.tt K X (8) (10)) = (K.add (K.mul (co K X (8)) (co K X (9))) (K.neg (K.mul (co K X (7)) (co K X (10))))) := rfl
  have t15 : O.le K.zero (VGoodTail.tt K X (11) (17)) := VGoodTail.tail_nn K O X hg (11) 5
  have et15 : (VGoodTail.tt K X (11) (17)) = (K.add (K.mul (co K X (11)) (co K X (16))) (K.neg (K.mul (co K X (10)) (co K X (17))))) := rfl
  have t16 : O.le K.zero (VGoodTail.tt K X (11) (15)) := VGoodTail.tail_nn K O X hg (11) 3
  have et16 : (VGoodTail.tt K X (11) (15)) = (K.add (K.mul (co K X (11)) (co K X (14))) (K.neg (K.mul (co K X (10)) (co K X (15))))) := rfl
  have t17 : O.le K.zero (VGoodTail.tt K X (11) (13)) := VGoodTail.tail_nn K O X hg (11) 1
  have et17 : (VGoodTail.tt K X (11) (13)) = (K.add (K.mul (co K X (11)) (co K X (12))) (K.neg (K.mul (co K X (10)) (co K X (13))))) := rfl
  have t18 : O.le K.zero (VGoodTail.tt K X (9) (17)) := VGoodTail.tail_nn K O X hg (9) 7
  have et18 : (VGoodTail.tt K X (9) (17)) = (K.add (K.mul (co K X (9)) (co K X (16))) (K.neg (K.mul (co K X (8)) (co K X (17))))) := rfl
  have hp : 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.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (VGoodTail.tt K X (7) (13)) (VGoodTail.tt K X (7) (13))) (VGoodTail.tt K X (8) (14))) (VGoodTail.tt K X (8) (14))) (VGoodTail.tt K X (8) (12))) (VGoodTail.tt K X (8) (12))) (VGoodTail.tt K X (6) (14))) (VGoodTail.tt  -- (+491 chars; full line in the Zenodo source)
    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 _ _ (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 _ _ (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   -- (+219 chars; full line in the Zenodo source)
  refine O.le_trans _ _ _ hp (TripleE.le_of_eq' K O _ _ ?_)
  rw [kv, et0, et1, et2, et3, et4, et5, et6, et7, et8, et9, et10, et11, et12, et13, et14, et15, et16, et17, et18]
  ring_z

theorem kna_1_6 (X : List R) (hg : L1Full.Good K O X) :
    O.le K.zero (CBLib.kern K (Fa K X) (Ga K X) 5 (1) (8)) := by
  have kv : CBLib.kern K (Fa K X) (Ga K X) 5 (1) (8) = (K.add (K.add (K.add (K.add (K.add K.zero (K.add (K.mul (co K X (7)) (co K X (13))) (K.neg (K.mul (co K X (14)) (co K X (6)))))) (K.add (K.mul (K.add (co K X (8)) (co K X (6))) (K.add (co K X (14)) (co K X (12)))) (K.neg (K.mul (K.add (co K X (15)) (co K X (13))) (K.add (co K X (7)) (co K X (5))))))) (K.add (K.mul (K.add (co K X (9)) (co K X (  -- (+748 chars; full line in the Zenodo source)
  have t0 : O.le K.zero (VGoodTail.tt K X (7) (14)) := VGoodTail.tail_nn K O X hg (7) 6
  have et0 : (VGoodTail.tt K X (7) (14)) = (K.add (K.mul (co K X (7)) (co K X (13))) (K.neg (K.mul (co K X (6)) (co K X (14))))) := rfl
  have t1 : O.le K.zero (VGoodTail.tt K X (8) (15)) := VGoodTail.tail_nn K O X hg (8) 6
  have et1 : (VGoodTail.tt K X (8) (15)) = (K.add (K.mul (co K X (8)) (co K X (14))) (K.neg (K.mul (co K X (7)) (co K X (15))))) := rfl
  have t2 : O.le K.zero (VGoodTail.tt K X (8) (13)) := VGoodTail.tail_nn K O X hg (8) 4
  have et2 : (VGoodTail.tt K X (8) (13)) = (K.add (K.mul (co K X (8)) (co K X (12))) (K.neg (K.mul (co K X (7)) (co K X (13))))) := rfl
  have t3 : O.le K.zero (VGoodTail.tt K X (6) (15)) := VGoodTail.tail_nn K O X hg (6) 8
  have et3 : (VGoodTail.tt K X (6) (15)) = (K.add (K.mul (co K X (6)) (co K X (14))) (K.neg (K.mul (co K X (5)) (co K X (15))))) := rfl
  have t4 : O.le K.zero (VGoodTail.tt K X (6) (13)) := VGoodTail.tail_nn K O X hg (6) 6
  have et4 : (VGoodTail.tt K X (6) (13)) = (K.add (K.mul (co K X (6)) (co K X (12))) (K.neg (K.mul (co K X (5)) (co K X (13))))) := rfl
  have t5 : O.le K.zero (VGoodTail.tt K X (9) (16)) := VGoodTail.tail_nn K O X hg (9) 6
  have et5 : (VGoodTail.tt K X (9) (16)) = (K.add (K.mul (co K X (9)) (co K X (15))) (K.neg (K.mul (co K X (8)) (co K X (16))))) := rfl
  have t6 : O.le K.zero (VGoodTail.tt K X (9) (14)) := VGoodTail.tail_nn K O X hg (9) 4
  have et6 : (VGoodTail.tt K X (9) (14)) = (K.add (K.mul (co K X (9)) (co K X (13))) (K.neg (K.mul (co K X (8)) (co K X (14))))) := rfl
  have t7 : O.le K.zero (VGoodTail.tt K X (9) (12)) := VGoodTail.tail_nn K O X hg (9) 2
  have et7 : (VGoodTail.tt K X (9) (12)) = (K.add (K.mul (co K X (9)) (co K X (11))) (K.neg (K.mul (co K X (8)) (co K X (12))))) := rfl
  have t8 : O.le K.zero (VGoodTail.tt K X (7) (16)) := VGoodTail.tail_nn K O X hg (7) 8
  have et8 : (VGoodTail.tt K X (7) (16)) = (K.add (K.mul (co K X (7)) (co K X (15))) (K.neg (K.mul (co K X (6)) (co K X (16))))) := rfl
  have t9 : O.le K.zero (VGoodTail.tt K X (7) (12)) := VGoodTail.tail_nn K O X hg (7) 4
  have et9 : (VGoodTail.tt K X (7) (12)) = (K.add (K.mul (co K X (7)) (co K X (11))) (K.neg (K.mul (co K X (6)) (co K X (12))))) := rfl
  have t10 : O.le K.zero (VGoodTail.tt K X (10) (17)) := VGoodTail.tail_nn K O X hg (10) 6
  have et10 : (VGoodTail.tt K X (10) (17)) = (K.add (K.mul (co K X (10)) (co K X (16))) (K.neg (K.mul (co K X (9)) (co K X (17))))) := rfl
  have t11 : O.le K.zero (VGoodTail.tt K X (10) (15)) := VGoodTail.tail_nn K O X hg (10) 4
  have et11 : (VGoodTail.tt K X (10) (15)) = (K.add (K.mul (co K X (10)) (co K X (14))) (K.neg (K.mul (co K X (9)) (co K X (15))))) := rfl
  have t12 : O.le K.zero (VGoodTail.tt K X (10) (13)) := VGoodTail.tail_nn K O X hg (10) 2
  have et12 : (VGoodTail.tt K X (10) (13)) = (K.add (K.mul (co K X (10)) (co K X (12))) (K.neg (K.mul (co K X (9)) (co K X (13))))) := rfl
  have t13 : O.le K.zero (VGoodTail.tt K X (8) (17)) := VGoodTail.tail_nn K O X hg (8) 8
  have et13 : (VGoodTail.tt K X (8) (17)) = (K.add (K.mul (co K X (8)) (co K X (16))) (K.neg (K.mul (co K X (7)) (co K X (17))))) := rfl
  have t14 : O.le K.zero (VGoodTail.tt K X (8) (11)) := VGoodTail.tail_nn K O X hg (8) 2
  have et14 : (VGoodTail.tt K X (8) (11)) = (K.add (K.mul (co K X (8)) (co K X (10))) (K.neg (K.mul (co K X (7)) (co K X (11))))) := rfl
  have t15 : O.le K.zero (VGoodTail.tt K X (11) (18)) := VGoodTail.tail_nn K O X hg (11) 6
  have et15 : (VGoodTail.tt K X (11) (18)) = (K.add (K.mul (co K X (11)) (co K X (17))) (K.neg (K.mul (co K X (10)) (co K X (18))))) := rfl
  have t16 : O.le K.zero (VGoodTail.tt K X (11) (16)) := VGoodTail.tail_nn K O X hg (11) 4
  have et16 : (VGoodTail.tt K X (11) (16)) = (K.add (K.mul (co K X (11)) (co K X (15))) (K.neg (K.mul (co K X (10)) (co K X (16))))) := rfl
  have t17 : O.le K.zero (VGoodTail.tt K X (11) (14)) := VGoodTail.tail_nn K O X hg (11) 2
  have et17 : (VGoodTail.tt K X (11) (14)) = (K.add (K.mul (co K X (11)) (co K X (13))) (K.neg (K.mul (co K X (10)) (co K X (14))))) := rfl
  have t18 : O.le K.zero (VGoodTail.tt K X (11) (12)) := VGoodTail.tail_nn K O X hg (11) 0
  have et18 : (VGoodTail.tt K X (11) (12)) = (K.add (K.mul (co K X (11)) (co K X (11))) (K.neg (K.mul (co K X (10)) (co K X (12))))) := rfl
  have t19 : O.le K.zero (VGoodTail.tt K X (9) (18)) := VGoodTail.tail_nn K O X hg (9) 8
  have et19 : (VGoodTail.tt K X (9) (18)) = (K.add (K.mul (co K X (9)) (co K X (17))) (K.neg (K.mul (co K X (8)) (co K X (18))))) := rfl
  have t20 : O.le K.zero (VGoodTail.tt K X (9) (10)) := VGoodTail.tail_nn K O X hg (9) 0
  have et20 : (VGoodTail.tt K X (9) (10)) = (K.add (K.mul (co K X (9)) (co K X (9))) (K.neg (K.mul (co K X (8)) (co K X (10))))) := rfl
  have hp : 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.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 (VGoodTail.tt K X (7) (14)) (VGoodTail.tt K X (7) (14))) (VGoodTail.tt K X (8) (15))) (VGoodTail.tt K X (8) (15))) (VGoodTail.tt K X (8) (13))) (VGoodTail.tt K X (8) (13))) (VGoodTail.tt K X (6)  -- (+600 chars; full line in the Zenodo source)
    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 _ _ (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 _ _ (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   -- (+299 chars; full line in the Zenodo source)
  refine O.le_trans _ _ _ hp (TripleE.le_of_eq' K O _ _ ?_)
  rw [kv, et0, et1, et2, et3, et4, et5, et6, et7, et8, et9, et10, et11, et12, et13, et14, et15, et16, et17, et18, et19, et20]
  ring_z

theorem kna_1_7 (X : List R) (hg : L1Full.Good K O X) :
    O.le K.zero (CBLib.kern K (Fa K X) (Ga K X) 5 (1) (9)) := by
  have kv : CBLib.kern K (Fa K X) (Ga K X) 5 (1) (9) = (K.add (K.add (K.add (K.add (K.add K.zero (K.add (K.mul (co K X (7)) (co K X (14))) (K.neg (K.mul (co K X (15)) (co K X (6)))))) (K.add (K.mul (K.add (co K X (8)) (co K X (6))) (K.add (co K X (15)) (co K X (13)))) (K.neg (K.mul (K.add (co K X (16)) (co K X (14))) (K.add (co K X (7)) (co K X (5))))))) (K.add (K.mul (K.add (co K X (9)) (co K X (  -- (+749 chars; full line in the Zenodo source)
  have t0 : O.le K.zero (VGoodTail.tt K X (7) (15)) := VGoodTail.tail_nn K O X hg (7) 7
  have et0 : (VGoodTail.tt K X (7) (15)) = (K.add (K.mul (co K X (7)) (co K X (14))) (K.neg (K.mul (co K X (6)) (co K X (15))))) := rfl
  have t1 : O.le K.zero (VGoodTail.tt K X (8) (16)) := VGoodTail.tail_nn K O X hg (8) 7
  have et1 : (VGoodTail.tt K X (8) (16)) = (K.add (K.mul (co K X (8)) (co K X (15))) (K.neg (K.mul (co K X (7)) (co K X (16))))) := rfl
  have t2 : O.le K.zero (VGoodTail.tt K X (8) (14)) := VGoodTail.tail_nn K O X hg (8) 5
  have et2 : (VGoodTail.tt K X (8) (14)) = (K.add (K.mul (co K X (8)) (co K X (13))) (K.neg (K.mul (co K X (7)) (co K X (14))))) := rfl
  have t3 : O.le K.zero (VGoodTail.tt K X (6) (16)) := VGoodTail.tail_nn K O X hg (6) 9
  have et3 : (VGoodTail.tt K X (6) (16)) = (K.add (K.mul (co K X (6)) (co K X (15))) (K.neg (K.mul (co K X (5)) (co K X (16))))) := rfl
  have t4 : O.le K.zero (VGoodTail.tt K X (6) (14)) := VGoodTail.tail_nn K O X hg (6) 7
  have et4 : (VGoodTail.tt K X (6) (14)) = (K.add (K.mul (co K X (6)) (co K X (13))) (K.neg (K.mul (co K X (5)) (co K X (14))))) := rfl
  have t5 : O.le K.zero (VGoodTail.tt K X (9) (17)) := VGoodTail.tail_nn K O X hg (9) 7
  have et5 : (VGoodTail.tt K X (9) (17)) = (K.add (K.mul (co K X (9)) (co K X (16))) (K.neg (K.mul (co K X (8)) (co K X (17))))) := rfl
  have t6 : O.le K.zero (VGoodTail.tt K X (9) (15)) := VGoodTail.tail_nn K O X hg (9) 5
  have et6 : (VGoodTail.tt K X (9) (15)) = (K.add (K.mul (co K X (9)) (co K X (14))) (K.neg (K.mul (co K X (8)) (co K X (15))))) := rfl
  have t7 : O.le K.zero (VGoodTail.tt K X (9) (13)) := VGoodTail.tail_nn K O X hg (9) 3
  have et7 : (VGoodTail.tt K X (9) (13)) = (K.add (K.mul (co K X (9)) (co K X (12))) (K.neg (K.mul (co K X (8)) (co K X (13))))) := rfl
  have t8 : O.le K.zero (VGoodTail.tt K X (7) (17)) := VGoodTail.tail_nn K O X hg (7) 9
  have et8 : (VGoodTail.tt K X (7) (17)) = (K.add (K.mul (co K X (7)) (co K X (16))) (K.neg (K.mul (co K X (6)) (co K X (17))))) := rfl
  have t9 : O.le K.zero (VGoodTail.tt K X (7) (13)) := VGoodTail.tail_nn K O X hg (7) 5
  have et9 : (VGoodTail.tt K X (7) (13)) = (K.add (K.mul (co K X (7)) (co K X (12))) (K.neg (K.mul (co K X (6)) (co K X (13))))) := rfl
  have t10 : O.le K.zero (VGoodTail.tt K X (10) (18)) := VGoodTail.tail_nn K O X hg (10) 7
  have et10 : (VGoodTail.tt K X (10) (18)) = (K.add (K.mul (co K X (10)) (co K X (17))) (K.neg (K.mul (co K X (9)) (co K X (18))))) := rfl
  have t11 : O.le K.zero (VGoodTail.tt K X (10) (16)) := VGoodTail.tail_nn K O X hg (10) 5
  have et11 : (VGoodTail.tt K X (10) (16)) = (K.add (K.mul (co K X (10)) (co K X (15))) (K.neg (K.mul (co K X (9)) (co K X (16))))) := rfl
  have t12 : O.le K.zero (VGoodTail.tt K X (10) (14)) := VGoodTail.tail_nn K O X hg (10) 3
  have et12 : (VGoodTail.tt K X (10) (14)) = (K.add (K.mul (co K X (10)) (co K X (13))) (K.neg (K.mul (co K X (9)) (co K X (14))))) := rfl
  have t13 : O.le K.zero (VGoodTail.tt K X (10) (12)) := VGoodTail.tail_nn K O X hg (10) 1
  have et13 : (VGoodTail.tt K X (10) (12)) = (K.add (K.mul (co K X (10)) (co K X (11))) (K.neg (K.mul (co K X (9)) (co K X (12))))) := rfl
  have t14 : O.le K.zero (VGoodTail.tt K X (8) (18)) := VGoodTail.tail_nn K O X hg (8) 9
  have et14 : (VGoodTail.tt K X (8) (18)) = (K.add (K.mul (co K X (8)) (co K X (17))) (K.neg (K.mul (co K X (7)) (co K X (18))))) := rfl
  have t15 : O.le K.zero (VGoodTail.tt K X (8) (12)) := VGoodTail.tail_nn K O X hg (8) 3
  have et15 : (VGoodTail.tt K X (8) (12)) = (K.add (K.mul (co K X (8)) (co K X (11))) (K.neg (K.mul (co K X (7)) (co K X (12))))) := rfl
  have t16 : O.le K.zero (VGoodTail.tt K X (11) (19)) := VGoodTail.tail_nn K O X hg (11) 7
  have et16 : (VGoodTail.tt K X (11) (19)) = (K.add (K.mul (co K X (11)) (co K X (18))) (K.neg (K.mul (co K X (10)) (co K X (19))))) := rfl
  have t17 : O.le K.zero (VGoodTail.tt K X (11) (17)) := VGoodTail.tail_nn K O X hg (11) 5
  have et17 : (VGoodTail.tt K X (11) (17)) = (K.add (K.mul (co K X (11)) (co K X (16))) (K.neg (K.mul (co K X (10)) (co K X (17))))) := rfl
  have t18 : O.le K.zero (VGoodTail.tt K X (11) (15)) := VGoodTail.tail_nn K O X hg (11) 3
  have et18 : (VGoodTail.tt K X (11) (15)) = (K.add (K.mul (co K X (11)) (co K X (14))) (K.neg (K.mul (co K X (10)) (co K X (15))))) := rfl
  have t19 : O.le K.zero (VGoodTail.tt K X (11) (13)) := VGoodTail.tail_nn K O X hg (11) 1
  have et19 : (VGoodTail.tt K X (11) (13)) = (K.add (K.mul (co K X (11)) (co K X (12))) (K.neg (K.mul (co K X (10)) (co K X (13))))) := rfl
  have t20 : O.le K.zero (VGoodTail.tt K X (9) (19)) := VGoodTail.tail_nn K O X hg (9) 9
  have et20 : (VGoodTail.tt K X (9) (19)) = (K.add (K.mul (co K X (9)) (co K X (18))) (K.neg (K.mul (co K X (8)) (co K X (19))))) := rfl
  have t21 : O.le K.zero (VGoodTail.tt K X (9) (11)) := VGoodTail.tail_nn K O X hg (9) 1
  have et21 : (VGoodTail.tt K X (9) (11)) = (K.add (K.mul (co K X (9)) (co K X (10))) (K.neg (K.mul (co K X (8)) (co K X (11))))) := rfl
  have hp : 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.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 (VGoodTail.tt K X (7) (15)) (VGoodTail.tt K X (7) (15))) (VGoodTail.tt K X (8) (16))) (VGoodTail.tt K X (8) (16))) (VGoodTail.tt K X (8) (14))) (VGoodTail.tt K X (8) (14))) (VGoodTail.tt   -- (+637 chars; full line in the Zenodo source)
    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 _ _ (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 _ _ (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   -- (+326 chars; full line in the Zenodo source)
  refine O.le_trans _ _ _ hp (TripleE.le_of_eq' K O _ _ ?_)
  rw [kv, et0, et1, et2, et3, et4, et5, et6, et7, et8, et9, et10, et11, et12, et13, et14, et15, et16, et17, et18, et19, et20, et21]
  ring_z

theorem kna_1_8 (X : List R) (hg : L1Full.Good K O X) :
    O.le K.zero (CBLib.kern K (Fa K X) (Ga K X) 5 (1) (10)) := by
  have kv : CBLib.kern K (Fa K X) (Ga K X) 5 (1) (10) = (K.add (K.add (K.add (K.add (K.add K.zero (K.add (K.mul (co K X (7)) (co K X (15))) (K.neg (K.mul (co K X (16)) (co K X (6)))))) (K.add (K.mul (K.add (co K X (8)) (co K X (6))) (K.add (co K X (16)) (co K X (14)))) (K.neg (K.mul (K.add (co K X (17)) (co K X (15))) (K.add (co K X (7)) (co K X (5))))))) (K.add (K.mul (K.add (co K X (9)) (co K X   -- (+750 chars; full line in the Zenodo source)
  have t0 : O.le K.zero (VGoodTail.tt K X (7) (16)) := VGoodTail.tail_nn K O X hg (7) 8
  have et0 : (VGoodTail.tt K X (7) (16)) = (K.add (K.mul (co K X (7)) (co K X (15))) (K.neg (K.mul (co K X (6)) (co K X (16))))) := rfl
  have t1 : O.le K.zero (VGoodTail.tt K X (8) (17)) := VGoodTail.tail_nn K O X hg (8) 8
  have et1 : (VGoodTail.tt K X (8) (17)) = (K.add (K.mul (co K X (8)) (co K X (16))) (K.neg (K.mul (co K X (7)) (co K X (17))))) := rfl
  have t2 : O.le K.zero (VGoodTail.tt K X (8) (15)) := VGoodTail.tail_nn K O X hg (8) 6
  have et2 : (VGoodTail.tt K X (8) (15)) = (K.add (K.mul (co K X (8)) (co K X (14))) (K.neg (K.mul (co K X (7)) (co K X (15))))) := rfl
  have t3 : O.le K.zero (VGoodTail.tt K X (6) (17)) := VGoodTail.tail_nn K O X hg (6) 10
  have et3 : (VGoodTail.tt K X (6) (17)) = (K.add (K.mul (co K X (6)) (co K X (16))) (K.neg (K.mul (co K X (5)) (co K X (17))))) := rfl
  have t4 : O.le K.zero (VGoodTail.tt K X (6) (15)) := VGoodTail.tail_nn K O X hg (6) 8
  have et4 : (VGoodTail.tt K X (6) (15)) = (K.add (K.mul (co K X (6)) (co K X (14))) (K.neg (K.mul (co K X (5)) (co K X (15))))) := rfl
  have t5 : O.le K.zero (VGoodTail.tt K X (9) (18)) := VGoodTail.tail_nn K O X hg (9) 8
  have et5 : (VGoodTail.tt K X (9) (18)) = (K.add (K.mul (co K X (9)) (co K X (17))) (K.neg (K.mul (co K X (8)) (co K X (18))))) := rfl
  have t6 : O.le K.zero (VGoodTail.tt K X (9) (16)) := VGoodTail.tail_nn K O X hg (9) 6
  have et6 : (VGoodTail.tt K X (9) (16)) = (K.add (K.mul (co K X (9)) (co K X (15))) (K.neg (K.mul (co K X (8)) (co K X (16))))) := rfl
  have t7 : O.le K.zero (VGoodTail.tt K X (9) (14)) := VGoodTail.tail_nn K O X hg (9) 4
  have et7 : (VGoodTail.tt K X (9) (14)) = (K.add (K.mul (co K X (9)) (co K X (13))) (K.neg (K.mul (co K X (8)) (co K X (14))))) := rfl
  have t8 : O.le K.zero (VGoodTail.tt K X (7) (18)) := VGoodTail.tail_nn K O X hg (7) 10
  have et8 : (VGoodTail.tt K X (7) (18)) = (K.add (K.mul (co K X (7)) (co K X (17))) (K.neg (K.mul (co K X (6)) (co K X (18))))) := rfl
  have t9 : O.le K.zero (VGoodTail.tt K X (7) (14)) := VGoodTail.tail_nn K O X hg (7) 6
  have et9 : (VGoodTail.tt K X (7) (14)) = (K.add (K.mul (co K X (7)) (co K X (13))) (K.neg (K.mul (co K X (6)) (co K X (14))))) := rfl
  have t10 : O.le K.zero (VGoodTail.tt K X (10) (19)) := VGoodTail.tail_nn K O X hg (10) 8
  have et10 : (VGoodTail.tt K X (10) (19)) = (K.add (K.mul (co K X (10)) (co K X (18))) (K.neg (K.mul (co K X (9)) (co K X (19))))) := rfl
  have t11 : O.le K.zero (VGoodTail.tt K X (10) (17)) := VGoodTail.tail_nn K O X hg (10) 6
  have et11 : (VGoodTail.tt K X (10) (17)) = (K.add (K.mul (co K X (10)) (co K X (16))) (K.neg (K.mul (co K X (9)) (co K X (17))))) := rfl
  have t12 : O.le K.zero (VGoodTail.tt K X (10) (15)) := VGoodTail.tail_nn K O X hg (10) 4
  have et12 : (VGoodTail.tt K X (10) (15)) = (K.add (K.mul (co K X (10)) (co K X (14))) (K.neg (K.mul (co K X (9)) (co K X (15))))) := rfl
  have t13 : O.le K.zero (VGoodTail.tt K X (10) (13)) := VGoodTail.tail_nn K O X hg (10) 2
  have et13 : (VGoodTail.tt K X (10) (13)) = (K.add (K.mul (co K X (10)) (co K X (12))) (K.neg (K.mul (co K X (9)) (co K X (13))))) := rfl
  have t14 : O.le K.zero (VGoodTail.tt K X (8) (19)) := VGoodTail.tail_nn K O X hg (8) 10
  have et14 : (VGoodTail.tt K X (8) (19)) = (K.add (K.mul (co K X (8)) (co K X (18))) (K.neg (K.mul (co K X (7)) (co K X (19))))) := rfl
  have t15 : O.le K.zero (VGoodTail.tt K X (8) (13)) := VGoodTail.tail_nn K O X hg (8) 4
  have et15 : (VGoodTail.tt K X (8) (13)) = (K.add (K.mul (co K X (8)) (co K X (12))) (K.neg (K.mul (co K X (7)) (co K X (13))))) := rfl
  have t16 : O.le K.zero (VGoodTail.tt K X (11) (20)) := VGoodTail.tail_nn K O X hg (11) 8
  have et16 : (VGoodTail.tt K X (11) (20)) = (K.add (K.mul (co K X (11)) (co K X (19))) (K.neg (K.mul (co K X (10)) (co K X (20))))) := rfl
  have t17 : O.le K.zero (VGoodTail.tt K X (11) (18)) := VGoodTail.tail_nn K O X hg (11) 6
  have et17 : (VGoodTail.tt K X (11) (18)) = (K.add (K.mul (co K X (11)) (co K X (17))) (K.neg (K.mul (co K X (10)) (co K X (18))))) := rfl
  have t18 : O.le K.zero (VGoodTail.tt K X (11) (16)) := VGoodTail.tail_nn K O X hg (11) 4
  have et18 : (VGoodTail.tt K X (11) (16)) = (K.add (K.mul (co K X (11)) (co K X (15))) (K.neg (K.mul (co K X (10)) (co K X (16))))) := rfl
  have t19 : O.le K.zero (VGoodTail.tt K X (11) (14)) := VGoodTail.tail_nn K O X hg (11) 2
  have et19 : (VGoodTail.tt K X (11) (14)) = (K.add (K.mul (co K X (11)) (co K X (13))) (K.neg (K.mul (co K X (10)) (co K X (14))))) := rfl
  have t20 : O.le K.zero (VGoodTail.tt K X (11) (12)) := VGoodTail.tail_nn K O X hg (11) 0
  have et20 : (VGoodTail.tt K X (11) (12)) = (K.add (K.mul (co K X (11)) (co K X (11))) (K.neg (K.mul (co K X (10)) (co K X (12))))) := rfl
  have t21 : O.le K.zero (VGoodTail.tt K X (9) (20)) := VGoodTail.tail_nn K O X hg (9) 10
  have et21 : (VGoodTail.tt K X (9) (20)) = (K.add (K.mul (co K X (9)) (co K X (19))) (K.neg (K.mul (co K X (8)) (co K X (20))))) := rfl
  have t22 : O.le K.zero (VGoodTail.tt K X (9) (12)) := VGoodTail.tail_nn K O X hg (9) 2
  have et22 : (VGoodTail.tt K X (9) (12)) = (K.add (K.mul (co K X (9)) (co K X (11))) (K.neg (K.mul (co K X (8)) (co K X (12))))) := rfl
  have hp : 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.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 (VGoodTail.tt K X (7) (16)) (VGoodTail.tt K X (7) (16))) (VGoodTail.tt K X (8) (17))) (VGoodTail.tt K X (8) (17))) (VGoodTail.tt K X (8) (15))) (VGoodTail.tt K X (8) (15))) (VGoodT  -- (+674 chars; full line in the Zenodo source)
    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 _ _ (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 _ _ (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   -- (+353 chars; full line in the Zenodo source)
  refine O.le_trans _ _ _ hp (TripleE.le_of_eq' K O _ _ ?_)
  rw [kv, et0, et1, et2, et3, et4, et5, et6, et7, et8, et9, et10, et11, et12, et13, et14, et15, et16, et17, et18, et19, et20, et21, et22]
  ring_z

theorem kna_1_9 (X : List R) (hg : L1Full.Good K O X) :
    O.le K.zero (CBLib.kern K (Fa K X) (Ga K X) 5 (1) (11)) := by
  have kv : CBLib.kern K (Fa K X) (Ga K X) 5 (1) (11) = (K.add (K.add (K.add (K.add (K.add K.zero (K.add (K.mul (co K X (7)) (co K X (16))) (K.neg (K.mul (co K X (17)) (co K X (6)))))) (K.add (K.mul (K.add (co K X (8)) (co K X (6))) (K.add (co K X (17)) (co K X (15)))) (K.neg (K.mul (K.add (co K X (18)) (co K X (16))) (K.add (co K X (7)) (co K X (5))))))) (K.add (K.mul (K.add (co K X (9)) (co K X   -- (+750 chars; full line in the Zenodo source)
  have t0 : O.le K.zero (VGoodTail.tt K X (7) (17)) := VGoodTail.tail_nn K O X hg (7) 9
  have et0 : (VGoodTail.tt K X (7) (17)) = (K.add (K.mul (co K X (7)) (co K X (16))) (K.neg (K.mul (co K X (6)) (co K X (17))))) := rfl
  have t1 : O.le K.zero (VGoodTail.tt K X (8) (18)) := VGoodTail.tail_nn K O X hg (8) 9
  have et1 : (VGoodTail.tt K X (8) (18)) = (K.add (K.mul (co K X (8)) (co K X (17))) (K.neg (K.mul (co K X (7)) (co K X (18))))) := rfl
  have t2 : O.le K.zero (VGoodTail.tt K X (8) (16)) := VGoodTail.tail_nn K O X hg (8) 7
  have et2 : (VGoodTail.tt K X (8) (16)) = (K.add (K.mul (co K X (8)) (co K X (15))) (K.neg (K.mul (co K X (7)) (co K X (16))))) := rfl
  have t3 : O.le K.zero (VGoodTail.tt K X (6) (18)) := VGoodTail.tail_nn K O X hg (6) 11
  have et3 : (VGoodTail.tt K X (6) (18)) = (K.add (K.mul (co K X (6)) (co K X (17))) (K.neg (K.mul (co K X (5)) (co K X (18))))) := rfl
  have t4 : O.le K.zero (VGoodTail.tt K X (6) (16)) := VGoodTail.tail_nn K O X hg (6) 9
  have et4 : (VGoodTail.tt K X (6) (16)) = (K.add (K.mul (co K X (6)) (co K X (15))) (K.neg (K.mul (co K X (5)) (co K X (16))))) := rfl
  have t5 : O.le K.zero (VGoodTail.tt K X (9) (19)) := VGoodTail.tail_nn K O X hg (9) 9
  have et5 : (VGoodTail.tt K X (9) (19)) = (K.add (K.mul (co K X (9)) (co K X (18))) (K.neg (K.mul (co K X (8)) (co K X (19))))) := rfl
  have t6 : O.le K.zero (VGoodTail.tt K X (9) (17)) := VGoodTail.tail_nn K O X hg (9) 7
  have et6 : (VGoodTail.tt K X (9) (17)) = (K.add (K.mul (co K X (9)) (co K X (16))) (K.neg (K.mul (co K X (8)) (co K X (17))))) := rfl
  have t7 : O.le K.zero (VGoodTail.tt K X (9) (15)) := VGoodTail.tail_nn K O X hg (9) 5
  have et7 : (VGoodTail.tt K X (9) (15)) = (K.add (K.mul (co K X (9)) (co K X (14))) (K.neg (K.mul (co K X (8)) (co K X (15))))) := rfl
  have t8 : O.le K.zero (VGoodTail.tt K X (7) (19)) := VGoodTail.tail_nn K O X hg (7) 11
  have et8 : (VGoodTail.tt K X (7) (19)) = (K.add (K.mul (co K X (7)) (co K X (18))) (K.neg (K.mul (co K X (6)) (co K X (19))))) := rfl
  have t9 : O.le K.zero (VGoodTail.tt K X (7) (15)) := VGoodTail.tail_nn K O X hg (7) 7
  have et9 : (VGoodTail.tt K X (7) (15)) = (K.add (K.mul (co K X (7)) (co K X (14))) (K.neg (K.mul (co K X (6)) (co K X (15))))) := rfl
  have t10 : O.le K.zero (VGoodTail.tt K X (10) (20)) := VGoodTail.tail_nn K O X hg (10) 9
  have et10 : (VGoodTail.tt K X (10) (20)) = (K.add (K.mul (co K X (10)) (co K X (19))) (K.neg (K.mul (co K X (9)) (co K X (20))))) := rfl
  have t11 : O.le K.zero (VGoodTail.tt K X (10) (18)) := VGoodTail.tail_nn K O X hg (10) 7
  have et11 : (VGoodTail.tt K X (10) (18)) = (K.add (K.mul (co K X (10)) (co K X (17))) (K.neg (K.mul (co K X (9)) (co K X (18))))) := rfl
  have t12 : O.le K.zero (VGoodTail.tt K X (10) (16)) := VGoodTail.tail_nn K O X hg (10) 5
  have et12 : (VGoodTail.tt K X (10) (16)) = (K.add (K.mul (co K X (10)) (co K X (15))) (K.neg (K.mul (co K X (9)) (co K X (16))))) := rfl
  have t13 : O.le K.zero (VGoodTail.tt K X (10) (14)) := VGoodTail.tail_nn K O X hg (10) 3
  have et13 : (VGoodTail.tt K X (10) (14)) = (K.add (K.mul (co K X (10)) (co K X (13))) (K.neg (K.mul (co K X (9)) (co K X (14))))) := rfl
  have t14 : O.le K.zero (VGoodTail.tt K X (8) (20)) := VGoodTail.tail_nn K O X hg (8) 11
  have et14 : (VGoodTail.tt K X (8) (20)) = (K.add (K.mul (co K X (8)) (co K X (19))) (K.neg (K.mul (co K X (7)) (co K X (20))))) := rfl
  have t15 : O.le K.zero (VGoodTail.tt K X (8) (14)) := VGoodTail.tail_nn K O X hg (8) 5
  have et15 : (VGoodTail.tt K X (8) (14)) = (K.add (K.mul (co K X (8)) (co K X (13))) (K.neg (K.mul (co K X (7)) (co K X (14))))) := rfl
  have t16 : O.le K.zero (VGoodTail.tt K X (11) (21)) := VGoodTail.tail_nn K O X hg (11) 9
  have et16 : (VGoodTail.tt K X (11) (21)) = (K.add (K.mul (co K X (11)) (co K X (20))) (K.neg (K.mul (co K X (10)) (co K X (21))))) := rfl
  have t17 : O.le K.zero (VGoodTail.tt K X (11) (19)) := VGoodTail.tail_nn K O X hg (11) 7
  have et17 : (VGoodTail.tt K X (11) (19)) = (K.add (K.mul (co K X (11)) (co K X (18))) (K.neg (K.mul (co K X (10)) (co K X (19))))) := rfl
  have t18 : O.le K.zero (VGoodTail.tt K X (11) (17)) := VGoodTail.tail_nn K O X hg (11) 5
  have et18 : (VGoodTail.tt K X (11) (17)) = (K.add (K.mul (co K X (11)) (co K X (16))) (K.neg (K.mul (co K X (10)) (co K X (17))))) := rfl
  have t19 : O.le K.zero (VGoodTail.tt K X (11) (15)) := VGoodTail.tail_nn K O X hg (11) 3
  have et19 : (VGoodTail.tt K X (11) (15)) = (K.add (K.mul (co K X (11)) (co K X (14))) (K.neg (K.mul (co K X (10)) (co K X (15))))) := rfl
  have t20 : O.le K.zero (VGoodTail.tt K X (11) (13)) := VGoodTail.tail_nn K O X hg (11) 1
  have et20 : (VGoodTail.tt K X (11) (13)) = (K.add (K.mul (co K X (11)) (co K X (12))) (K.neg (K.mul (co K X (10)) (co K X (13))))) := rfl
  have t21 : O.le K.zero (VGoodTail.tt K X (9) (21)) := VGoodTail.tail_nn K O X hg (9) 11
  have et21 : (VGoodTail.tt K X (9) (21)) = (K.add (K.mul (co K X (9)) (co K X (20))) (K.neg (K.mul (co K X (8)) (co K X (21))))) := rfl
  have t22 : O.le K.zero (VGoodTail.tt K X (9) (13)) := VGoodTail.tail_nn K O X hg (9) 3
  have et22 : (VGoodTail.tt K X (9) (13)) = (K.add (K.mul (co K X (9)) (co K X (12))) (K.neg (K.mul (co K X (8)) (co K X (13))))) := rfl
  have hp : 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.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 (VGoodTail.tt K X (7) (17)) (VGoodTail.tt K X (7) (17))) (VGoodTail.tt K X (8) (18))) (VGoodTail.tt K X (8) (18))) (VGoodTail.tt K X (8) (16))) (VGoodTail.tt K X (8) (16))) (VGoodT  -- (+674 chars; full line in the Zenodo source)
    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 _ _ (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 _ _ (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   -- (+353 chars; full line in the Zenodo source)
  refine O.le_trans _ _ _ hp (TripleE.le_of_eq' K O _ _ ?_)
  rw [kv, et0, et1, et2, et3, et4, et5, et6, et7, et8, et9, et10, et11, et12, et13, et14, et15, et16, et17, et18, et19, et20, et21, et22]
  ring_z

theorem kna_1_10 (X : List R) (hg : L1Full.Good K O X) :
    O.le K.zero (CBLib.kern K (Fa K X) (Ga K X) 5 (1) (12)) := by
  have kv : CBLib.kern K (Fa K X) (Ga K X) 5 (1) (12) = (K.add (K.add (K.add (K.add (K.add K.zero (K.add (K.mul (co K X (7)) (co K X (17))) (K.neg (K.mul (co K X (18)) (co K X (6)))))) (K.add (K.mul (K.add (co K X (8)) (co K X (6))) (K.add (co K X (18)) (co K X (16)))) (K.neg (K.mul (K.add (co K X (19)) (co K X (17))) (K.add (co K X (7)) (co K X (5))))))) (K.add (K.mul (K.add (co K X (9)) (co K X   -- (+750 chars; full line in the Zenodo source)
  have t0 : O.le K.zero (VGoodTail.tt K X (7) (18)) := VGoodTail.tail_nn K O X hg (7) 10
  have et0 : (VGoodTail.tt K X (7) (18)) = (K.add (K.mul (co K X (7)) (co K X (17))) (K.neg (K.mul (co K X (6)) (co K X (18))))) := rfl
  have t1 : O.le K.zero (VGoodTail.tt K X (8) (19)) := VGoodTail.tail_nn K O X hg (8) 10
  have et1 : (VGoodTail.tt K X (8) (19)) = (K.add (K.mul (co K X (8)) (co K X (18))) (K.neg (K.mul (co K X (7)) (co K X (19))))) := rfl
  have t2 : O.le K.zero (VGoodTail.tt K X (8) (17)) := VGoodTail.tail_nn K O X hg (8) 8
  have et2 : (VGoodTail.tt K X (8) (17)) = (K.add (K.mul (co K X (8)) (co K X (16))) (K.neg (K.mul (co K X (7)) (co K X (17))))) := rfl
  have t3 : O.le K.zero (VGoodTail.tt K X (6) (19)) := VGoodTail.tail_nn K O X hg (6) 12
  have et3 : (VGoodTail.tt K X (6) (19)) = (K.add (K.mul (co K X (6)) (co K X (18))) (K.neg (K.mul (co K X (5)) (co K X (19))))) := rfl
  have t4 : O.le K.zero (VGoodTail.tt K X (6) (17)) := VGoodTail.tail_nn K O X hg (6) 10
  have et4 : (VGoodTail.tt K X (6) (17)) = (K.add (K.mul (co K X (6)) (co K X (16))) (K.neg (K.mul (co K X (5)) (co K X (17))))) := rfl
  have t5 : O.le K.zero (VGoodTail.tt K X (9) (20)) := VGoodTail.tail_nn K O X hg (9) 10
  have et5 : (VGoodTail.tt K X (9) (20)) = (K.add (K.mul (co K X (9)) (co K X (19))) (K.neg (K.mul (co K X (8)) (co K X (20))))) := rfl
  have t6 : O.le K.zero (VGoodTail.tt K X (9) (18)) := VGoodTail.tail_nn K O X hg (9) 8
  have et6 : (VGoodTail.tt K X (9) (18)) = (K.add (K.mul (co K X (9)) (co K X (17))) (K.neg (K.mul (co K X (8)) (co K X (18))))) := rfl
  have t7 : O.le K.zero (VGoodTail.tt K X (9) (16)) := VGoodTail.tail_nn K O X hg (9) 6
  have et7 : (VGoodTail.tt K X (9) (16)) = (K.add (K.mul (co K X (9)) (co K X (15))) (K.neg (K.mul (co K X (8)) (co K X (16))))) := rfl
  have t8 : O.le K.zero (VGoodTail.tt K X (7) (20)) := VGoodTail.tail_nn K O X hg (7) 12
  have et8 : (VGoodTail.tt K X (7) (20)) = (K.add (K.mul (co K X (7)) (co K X (19))) (K.neg (K.mul (co K X (6)) (co K X (20))))) := rfl
  have t9 : O.le K.zero (VGoodTail.tt K X (7) (16)) := VGoodTail.tail_nn K O X hg (7) 8
  have et9 : (VGoodTail.tt K X (7) (16)) = (K.add (K.mul (co K X (7)) (co K X (15))) (K.neg (K.mul (co K X (6)) (co K X (16))))) := rfl
  have t10 : O.le K.zero (VGoodTail.tt K X (10) (21)) := VGoodTail.tail_nn K O X hg (10) 10
  have et10 : (VGoodTail.tt K X (10) (21)) = (K.add (K.mul (co K X (10)) (co K X (20))) (K.neg (K.mul (co K X (9)) (co K X (21))))) := rfl
  have t11 : O.le K.zero (VGoodTail.tt K X (10) (19)) := VGoodTail.tail_nn K O X hg (10) 8
  have et11 : (VGoodTail.tt K X (10) (19)) = (K.add (K.mul (co K X (10)) (co K X (18))) (K.neg (K.mul (co K X (9)) (co K X (19))))) := rfl
  have t12 : O.le K.zero (VGoodTail.tt K X (10) (17)) := VGoodTail.tail_nn K O X hg (10) 6
  have et12 : (VGoodTail.tt K X (10) (17)) = (K.add (K.mul (co K X (10)) (co K X (16))) (K.neg (K.mul (co K X (9)) (co K X (17))))) := rfl
  have t13 : O.le K.zero (VGoodTail.tt K X (10) (15)) := VGoodTail.tail_nn K O X hg (10) 4
  have et13 : (VGoodTail.tt K X (10) (15)) = (K.add (K.mul (co K X (10)) (co K X (14))) (K.neg (K.mul (co K X (9)) (co K X (15))))) := rfl
  have t14 : O.le K.zero (VGoodTail.tt K X (8) (21)) := VGoodTail.tail_nn K O X hg (8) 12
  have et14 : (VGoodTail.tt K X (8) (21)) = (K.add (K.mul (co K X (8)) (co K X (20))) (K.neg (K.mul (co K X (7)) (co K X (21))))) := rfl
  have t15 : O.le K.zero (VGoodTail.tt K X (8) (15)) := VGoodTail.tail_nn K O X hg (8) 6
  have et15 : (VGoodTail.tt K X (8) (15)) = (K.add (K.mul (co K X (8)) (co K X (14))) (K.neg (K.mul (co K X (7)) (co K X (15))))) := rfl
  have t16 : O.le K.zero (VGoodTail.tt K X (11) (22)) := VGoodTail.tail_nn K O X hg (11) 10
  have et16 : (VGoodTail.tt K X (11) (22)) = (K.add (K.mul (co K X (11)) (co K X (21))) (K.neg (K.mul (co K X (10)) (co K X (22))))) := rfl
  have t17 : O.le K.zero (VGoodTail.tt K X (11) (20)) := VGoodTail.tail_nn K O X hg (11) 8
  have et17 : (VGoodTail.tt K X (11) (20)) = (K.add (K.mul (co K X (11)) (co K X (19))) (K.neg (K.mul (co K X (10)) (co K X (20))))) := rfl
  have t18 : O.le K.zero (VGoodTail.tt K X (11) (18)) := VGoodTail.tail_nn K O X hg (11) 6
  have et18 : (VGoodTail.tt K X (11) (18)) = (K.add (K.mul (co K X (11)) (co K X (17))) (K.neg (K.mul (co K X (10)) (co K X (18))))) := rfl
  have t19 : O.le K.zero (VGoodTail.tt K X (11) (16)) := VGoodTail.tail_nn K O X hg (11) 4
  have et19 : (VGoodTail.tt K X (11) (16)) = (K.add (K.mul (co K X (11)) (co K X (15))) (K.neg (K.mul (co K X (10)) (co K X (16))))) := rfl
  have t20 : O.le K.zero (VGoodTail.tt K X (11) (14)) := VGoodTail.tail_nn K O X hg (11) 2
  have et20 : (VGoodTail.tt K X (11) (14)) = (K.add (K.mul (co K X (11)) (co K X (13))) (K.neg (K.mul (co K X (10)) (co K X (14))))) := rfl
  have t21 : O.le K.zero (VGoodTail.tt K X (9) (22)) := VGoodTail.tail_nn K O X hg (9) 12
  have et21 : (VGoodTail.tt K X (9) (22)) = (K.add (K.mul (co K X (9)) (co K X (21))) (K.neg (K.mul (co K X (8)) (co K X (22))))) := rfl
  have t22 : O.le K.zero (VGoodTail.tt K X (9) (14)) := VGoodTail.tail_nn K O X hg (9) 4
  have et22 : (VGoodTail.tt K X (9) (14)) = (K.add (K.mul (co K X (9)) (co K X (13))) (K.neg (K.mul (co K X (8)) (co K X (14))))) := rfl
  have hp : 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.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 (VGoodTail.tt K X (7) (18)) (VGoodTail.tt K X (7) (18))) (VGoodTail.tt K X (8) (19))) (VGoodTail.tt K X (8) (19))) (VGoodTail.tt K X (8) (17))) (VGoodTail.tt K X (8) (17))) (VGoodT  -- (+674 chars; full line in the Zenodo source)
    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 _ _ (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 _ _ (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   -- (+353 chars; full line in the Zenodo source)
  refine O.le_trans _ _ _ hp (TripleE.le_of_eq' K O _ _ ?_)
  rw [kv, et0, et1, et2, et3, et4, et5, et6, et7, et8, et9, et10, et11, et12, et13, et14, et15, et16, et17, et18, et19, et20, et21, et22]
  ring_z

theorem kna_1_11 (X : List R) (hg : L1Full.Good K O X) :
    O.le K.zero (CBLib.kern K (Fa K X) (Ga K X) 5 (1) (13)) := by
  have kv : CBLib.kern K (Fa K X) (Ga K X) 5 (1) (13) = (K.add (K.add (K.add (K.add (K.add K.zero (K.add (K.mul (co K X (7)) (co K X (18))) (K.neg (K.mul (co K X (19)) (co K X (6)))))) (K.add (K.mul (K.add (co K X (8)) (co K X (6))) (K.add (co K X (19)) (co K X (17)))) (K.neg (K.mul (K.add (co K X (20)) (co K X (18))) (K.add (co K X (7)) (co K X (5))))))) (K.add (K.mul (K.add (co K X (9)) (co K X   -- (+750 chars; full line in the Zenodo source)
  have t0 : O.le K.zero (VGoodTail.tt K X (7) (19)) := VGoodTail.tail_nn K O X hg (7) 11
  have et0 : (VGoodTail.tt K X (7) (19)) = (K.add (K.mul (co K X (7)) (co K X (18))) (K.neg (K.mul (co K X (6)) (co K X (19))))) := rfl
  have t1 : O.le K.zero (VGoodTail.tt K X (8) (20)) := VGoodTail.tail_nn K O X hg (8) 11
  have et1 : (VGoodTail.tt K X (8) (20)) = (K.add (K.mul (co K X (8)) (co K X (19))) (K.neg (K.mul (co K X (7)) (co K X (20))))) := rfl
  have t2 : O.le K.zero (VGoodTail.tt K X (8) (18)) := VGoodTail.tail_nn K O X hg (8) 9
  have et2 : (VGoodTail.tt K X (8) (18)) = (K.add (K.mul (co K X (8)) (co K X (17))) (K.neg (K.mul (co K X (7)) (co K X (18))))) := rfl
  have t3 : O.le K.zero (VGoodTail.tt K X (6) (20)) := VGoodTail.tail_nn K O X hg (6) 13
  have et3 : (VGoodTail.tt K X (6) (20)) = (K.add (K.mul (co K X (6)) (co K X (19))) (K.neg (K.mul (co K X (5)) (co K X (20))))) := rfl
  have t4 : O.le K.zero (VGoodTail.tt K X (6) (18)) := VGoodTail.tail_nn K O X hg (6) 11
  have et4 : (VGoodTail.tt K X (6) (18)) = (K.add (K.mul (co K X (6)) (co K X (17))) (K.neg (K.mul (co K X (5)) (co K X (18))))) := rfl
  have t5 : O.le K.zero (VGoodTail.tt K X (9) (21)) := VGoodTail.tail_nn K O X hg (9) 11
  have et5 : (VGoodTail.tt K X (9) (21)) = (K.add (K.mul (co K X (9)) (co K X (20))) (K.neg (K.mul (co K X (8)) (co K X (21))))) := rfl
  have t6 : O.le K.zero (VGoodTail.tt K X (9) (19)) := VGoodTail.tail_nn K O X hg (9) 9
  have et6 : (VGoodTail.tt K X (9) (19)) = (K.add (K.mul (co K X (9)) (co K X (18))) (K.neg (K.mul (co K X (8)) (co K X (19))))) := rfl
  have t7 : O.le K.zero (VGoodTail.tt K X (9) (17)) := VGoodTail.tail_nn K O X hg (9) 7
  have et7 : (VGoodTail.tt K X (9) (17)) = (K.add (K.mul (co K X (9)) (co K X (16))) (K.neg (K.mul (co K X (8)) (co K X (17))))) := rfl
  have t8 : O.le K.zero (VGoodTail.tt K X (7) (21)) := VGoodTail.tail_nn K O X hg (7) 13
  have et8 : (VGoodTail.tt K X (7) (21)) = (K.add (K.mul (co K X (7)) (co K X (20))) (K.neg (K.mul (co K X (6)) (co K X (21))))) := rfl
  have t9 : O.le K.zero (VGoodTail.tt K X (7) (17)) := VGoodTail.tail_nn K O X hg (7) 9
  have et9 : (VGoodTail.tt K X (7) (17)) = (K.add (K.mul (co K X (7)) (co K X (16))) (K.neg (K.mul (co K X (6)) (co K X (17))))) := rfl
  have t10 : O.le K.zero (VGoodTail.tt K X (10) (22)) := VGoodTail.tail_nn K O X hg (10) 11
  have et10 : (VGoodTail.tt K X (10) (22)) = (K.add (K.mul (co K X (10)) (co K X (21))) (K.neg (K.mul (co K X (9)) (co K X (22))))) := rfl
  have t11 : O.le K.zero (VGoodTail.tt K X (10) (20)) := VGoodTail.tail_nn K O X hg (10) 9
  have et11 : (VGoodTail.tt K X (10) (20)) = (K.add (K.mul (co K X (10)) (co K X (19))) (K.neg (K.mul (co K X (9)) (co K X (20))))) := rfl
  have t12 : O.le K.zero (VGoodTail.tt K X (10) (18)) := VGoodTail.tail_nn K O X hg (10) 7
  have et12 : (VGoodTail.tt K X (10) (18)) = (K.add (K.mul (co K X (10)) (co K X (17))) (K.neg (K.mul (co K X (9)) (co K X (18))))) := rfl
  have t13 : O.le K.zero (VGoodTail.tt K X (10) (16)) := VGoodTail.tail_nn K O X hg (10) 5
  have et13 : (VGoodTail.tt K X (10) (16)) = (K.add (K.mul (co K X (10)) (co K X (15))) (K.neg (K.mul (co K X (9)) (co K X (16))))) := rfl
  have t14 : O.le K.zero (VGoodTail.tt K X (8) (22)) := VGoodTail.tail_nn K O X hg (8) 13
  have et14 : (VGoodTail.tt K X (8) (22)) = (K.add (K.mul (co K X (8)) (co K X (21))) (K.neg (K.mul (co K X (7)) (co K X (22))))) := rfl
  have t15 : O.le K.zero (VGoodTail.tt K X (8) (16)) := VGoodTail.tail_nn K O X hg (8) 7
  have et15 : (VGoodTail.tt K X (8) (16)) = (K.add (K.mul (co K X (8)) (co K X (15))) (K.neg (K.mul (co K X (7)) (co K X (16))))) := rfl
  have t16 : O.le K.zero (VGoodTail.tt K X (11) (23)) := VGoodTail.tail_nn K O X hg (11) 11
  have et16 : (VGoodTail.tt K X (11) (23)) = (K.add (K.mul (co K X (11)) (co K X (22))) (K.neg (K.mul (co K X (10)) (co K X (23))))) := rfl
  have t17 : O.le K.zero (VGoodTail.tt K X (11) (21)) := VGoodTail.tail_nn K O X hg (11) 9
  have et17 : (VGoodTail.tt K X (11) (21)) = (K.add (K.mul (co K X (11)) (co K X (20))) (K.neg (K.mul (co K X (10)) (co K X (21))))) := rfl
  have t18 : O.le K.zero (VGoodTail.tt K X (11) (19)) := VGoodTail.tail_nn K O X hg (11) 7
  have et18 : (VGoodTail.tt K X (11) (19)) = (K.add (K.mul (co K X (11)) (co K X (18))) (K.neg (K.mul (co K X (10)) (co K X (19))))) := rfl
  have t19 : O.le K.zero (VGoodTail.tt K X (11) (17)) := VGoodTail.tail_nn K O X hg (11) 5
  have et19 : (VGoodTail.tt K X (11) (17)) = (K.add (K.mul (co K X (11)) (co K X (16))) (K.neg (K.mul (co K X (10)) (co K X (17))))) := rfl
  have t20 : O.le K.zero (VGoodTail.tt K X (11) (15)) := VGoodTail.tail_nn K O X hg (11) 3
  have et20 : (VGoodTail.tt K X (11) (15)) = (K.add (K.mul (co K X (11)) (co K X (14))) (K.neg (K.mul (co K X (10)) (co K X (15))))) := rfl
  have t21 : O.le K.zero (VGoodTail.tt K X (9) (23)) := VGoodTail.tail_nn K O X hg (9) 13
  have et21 : (VGoodTail.tt K X (9) (23)) = (K.add (K.mul (co K X (9)) (co K X (22))) (K.neg (K.mul (co K X (8)) (co K X (23))))) := rfl
  have t22 : O.le K.zero (VGoodTail.tt K X (9) (15)) := VGoodTail.tail_nn K O X hg (9) 5
  have et22 : (VGoodTail.tt K X (9) (15)) = (K.add (K.mul (co K X (9)) (co K X (14))) (K.neg (K.mul (co K X (8)) (co K X (15))))) := rfl
  have hp : 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.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 (VGoodTail.tt K X (7) (19)) (VGoodTail.tt K X (7) (19))) (VGoodTail.tt K X (8) (20))) (VGoodTail.tt K X (8) (20))) (VGoodTail.tt K X (8) (18))) (VGoodTail.tt K X (8) (18))) (VGoodT  -- (+674 chars; full line in the Zenodo source)
    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 _ _ (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 _ _ (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   -- (+353 chars; full line in the Zenodo source)
  refine O.le_trans _ _ _ hp (TripleE.le_of_eq' K O _ _ ?_)
  rw [kv, et0, et1, et2, et3, et4, et5, et6, et7, et8, et9, et10, et11, et12, et13, et14, et15, et16, et17, et18, et19, et20, et21, et22]
  ring_z

theorem kna_1_12 (X : List R) (hg : L1Full.Good K O X) :
    O.le K.zero (CBLib.kern K (Fa K X) (Ga K X) 5 (1) (14)) := by
  have kv : CBLib.kern K (Fa K X) (Ga K X) 5 (1) (14) = (K.add (K.add (K.add (K.add (K.add K.zero (K.add (K.mul (co K X (7)) (co K X (19))) (K.neg (K.mul (co K X (20)) (co K X (6)))))) (K.add (K.mul (K.add (co K X (8)) (co K X (6))) (K.add (co K X (20)) (co K X (18)))) (K.neg (K.mul (K.add (co K X (21)) (co K X (19))) (K.add (co K X (7)) (co K X (5))))))) (K.add (K.mul (K.add (co K X (9)) (co K X   -- (+750 chars; full line in the Zenodo source)
  have t0 : O.le K.zero (VGoodTail.tt K X (7) (20)) := VGoodTail.tail_nn K O X hg (7) 12
  have et0 : (VGoodTail.tt K X (7) (20)) = (K.add (K.mul (co K X (7)) (co K X (19))) (K.neg (K.mul (co K X (6)) (co K X (20))))) := rfl
  have t1 : O.le K.zero (VGoodTail.tt K X (8) (21)) := VGoodTail.tail_nn K O X hg (8) 12
  have et1 : (VGoodTail.tt K X (8) (21)) = (K.add (K.mul (co K X (8)) (co K X (20))) (K.neg (K.mul (co K X (7)) (co K X (21))))) := rfl
  have t2 : O.le K.zero (VGoodTail.tt K X (8) (19)) := VGoodTail.tail_nn K O X hg (8) 10
  have et2 : (VGoodTail.tt K X (8) (19)) = (K.add (K.mul (co K X (8)) (co K X (18))) (K.neg (K.mul (co K X (7)) (co K X (19))))) := rfl
  have t3 : O.le K.zero (VGoodTail.tt K X (6) (21)) := VGoodTail.tail_nn K O X hg (6) 14
  have et3 : (VGoodTail.tt K X (6) (21)) = (K.add (K.mul (co K X (6)) (co K X (20))) (K.neg (K.mul (co K X (5)) (co K X (21))))) := rfl
  have t4 : O.le K.zero (VGoodTail.tt K X (6) (19)) := VGoodTail.tail_nn K O X hg (6) 12
  have et4 : (VGoodTail.tt K X (6) (19)) = (K.add (K.mul (co K X (6)) (co K X (18))) (K.neg (K.mul (co K X (5)) (co K X (19))))) := rfl
  have t5 : O.le K.zero (VGoodTail.tt K X (9) (22)) := VGoodTail.tail_nn K O X hg (9) 12
  have et5 : (VGoodTail.tt K X (9) (22)) = (K.add (K.mul (co K X (9)) (co K X (21))) (K.neg (K.mul (co K X (8)) (co K X (22))))) := rfl
  have t6 : O.le K.zero (VGoodTail.tt K X (9) (20)) := VGoodTail.tail_nn K O X hg (9) 10
  have et6 : (VGoodTail.tt K X (9) (20)) = (K.add (K.mul (co K X (9)) (co K X (19))) (K.neg (K.mul (co K X (8)) (co K X (20))))) := rfl
  have t7 : O.le K.zero (VGoodTail.tt K X (9) (18)) := VGoodTail.tail_nn K O X hg (9) 8
  have et7 : (VGoodTail.tt K X (9) (18)) = (K.add (K.mul (co K X (9)) (co K X (17))) (K.neg (K.mul (co K X (8)) (co K X (18))))) := rfl
  have t8 : O.le K.zero (VGoodTail.tt K X (7) (22)) := VGoodTail.tail_nn K O X hg (7) 14
  have et8 : (VGoodTail.tt K X (7) (22)) = (K.add (K.mul (co K X (7)) (co K X (21))) (K.neg (K.mul (co K X (6)) (co K X (22))))) := rfl
  have t9 : O.le K.zero (VGoodTail.tt K X (7) (18)) := VGoodTail.tail_nn K O X hg (7) 10
  have et9 : (VGoodTail.tt K X (7) (18)) = (K.add (K.mul (co K X (7)) (co K X (17))) (K.neg (K.mul (co K X (6)) (co K X (18))))) := rfl
  have t10 : O.le K.zero (VGoodTail.tt K X (10) (23)) := VGoodTail.tail_nn K O X hg (10) 12
  have et10 : (VGoodTail.tt K X (10) (23)) = (K.add (K.mul (co K X (10)) (co K X (22))) (K.neg (K.mul (co K X (9)) (co K X (23))))) := rfl
  have t11 : O.le K.zero (VGoodTail.tt K X (10) (21)) := VGoodTail.tail_nn K O X hg (10) 10
  have et11 : (VGoodTail.tt K X (10) (21)) = (K.add (K.mul (co K X (10)) (co K X (20))) (K.neg (K.mul (co K X (9)) (co K X (21))))) := rfl
  have t12 : O.le K.zero (VGoodTail.tt K X (10) (19)) := VGoodTail.tail_nn K O X hg (10) 8
  have et12 : (VGoodTail.tt K X (10) (19)) = (K.add (K.mul (co K X (10)) (co K X (18))) (K.neg (K.mul (co K X (9)) (co K X (19))))) := rfl
  have t13 : O.le K.zero (VGoodTail.tt K X (10) (17)) := VGoodTail.tail_nn K O X hg (10) 6
  have et13 : (VGoodTail.tt K X (10) (17)) = (K.add (K.mul (co K X (10)) (co K X (16))) (K.neg (K.mul (co K X (9)) (co K X (17))))) := rfl
  have t14 : O.le K.zero (VGoodTail.tt K X (8) (23)) := VGoodTail.tail_nn K O X hg (8) 14
  have et14 : (VGoodTail.tt K X (8) (23)) = (K.add (K.mul (co K X (8)) (co K X (22))) (K.neg (K.mul (co K X (7)) (co K X (23))))) := rfl
  have t15 : O.le K.zero (VGoodTail.tt K X (8) (17)) := VGoodTail.tail_nn K O X hg (8) 8
  have et15 : (VGoodTail.tt K X (8) (17)) = (K.add (K.mul (co K X (8)) (co K X (16))) (K.neg (K.mul (co K X (7)) (co K X (17))))) := rfl
  have t16 : O.le K.zero (VGoodTail.tt K X (11) (24)) := VGoodTail.tail_nn K O X hg (11) 12
  have et16 : (VGoodTail.tt K X (11) (24)) = (K.add (K.mul (co K X (11)) (co K X (23))) (K.neg (K.mul (co K X (10)) (co K X (24))))) := rfl
  have t17 : O.le K.zero (VGoodTail.tt K X (11) (22)) := VGoodTail.tail_nn K O X hg (11) 10
  have et17 : (VGoodTail.tt K X (11) (22)) = (K.add (K.mul (co K X (11)) (co K X (21))) (K.neg (K.mul (co K X (10)) (co K X (22))))) := rfl
  have t18 : O.le K.zero (VGoodTail.tt K X (11) (20)) := VGoodTail.tail_nn K O X hg (11) 8
  have et18 : (VGoodTail.tt K X (11) (20)) = (K.add (K.mul (co K X (11)) (co K X (19))) (K.neg (K.mul (co K X (10)) (co K X (20))))) := rfl
  have t19 : O.le K.zero (VGoodTail.tt K X (11) (18)) := VGoodTail.tail_nn K O X hg (11) 6
  have et19 : (VGoodTail.tt K X (11) (18)) = (K.add (K.mul (co K X (11)) (co K X (17))) (K.neg (K.mul (co K X (10)) (co K X (18))))) := rfl
  have t20 : O.le K.zero (VGoodTail.tt K X (11) (16)) := VGoodTail.tail_nn K O X hg (11) 4
  have et20 : (VGoodTail.tt K X (11) (16)) = (K.add (K.mul (co K X (11)) (co K X (15))) (K.neg (K.mul (co K X (10)) (co K X (16))))) := rfl
  have t21 : O.le K.zero (VGoodTail.tt K X (9) (24)) := VGoodTail.tail_nn K O X hg (9) 14
  have et21 : (VGoodTail.tt K X (9) (24)) = (K.add (K.mul (co K X (9)) (co K X (23))) (K.neg (K.mul (co K X (8)) (co K X (24))))) := rfl
  have t22 : O.le K.zero (VGoodTail.tt K X (9) (16)) := VGoodTail.tail_nn K O X hg (9) 6
  have et22 : (VGoodTail.tt K X (9) (16)) = (K.add (K.mul (co K X (9)) (co K X (15))) (K.neg (K.mul (co K X (8)) (co K X (16))))) := rfl
  have hp : 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.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 (VGoodTail.tt K X (7) (20)) (VGoodTail.tt K X (7) (20))) (VGoodTail.tt K X (8) (21))) (VGoodTail.tt K X (8) (21))) (VGoodTail.tt K X (8) (19))) (VGoodTail.tt K X (8) (19))) (VGoodT  -- (+674 chars; full line in the Zenodo source)
    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 _ _ (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 _ _ (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   -- (+353 chars; full line in the Zenodo source)
  refine O.le_trans _ _ _ hp (TripleE.le_of_eq' K O _ _ ?_)
  rw [kv, et0, et1, et2, et3, et4, et5, et6, et7, et8, et9, et10, et11, et12, et13, et14, et15, et16, et17, et18, et19, et20, et21, et22]
  ring_z

theorem kna_2_0 (X : List R) (hg : L1Full.Good K O X) :
    O.le K.zero (CBLib.kern K (Fa K X) (Ga K X) 5 (2) (3)) := by
  have kv : CBLib.kern K (Fa K X) (Ga K X) 5 (2) (3) = (K.add (K.add (K.add (K.add (K.add K.zero (K.add (K.mul (co K X (8)) (co K X (8))) (K.neg (K.mul (co K X (9)) (co K X (7)))))) (K.add (K.mul (K.add (co K X (9)) (co K X (7))) (K.add (co K X (9)) (co K X (7)))) (K.neg (K.mul (K.add (co K X (10)) (co K X (8))) (K.add (co K X (8)) (co K X (6))))))) (K.add (K.mul (K.add (K.add (co K X (10)) (co K   -- (+816 chars; full line in the Zenodo source)
  have t0 : O.le K.zero (VGoodTail.tt K X (8) (9)) := VGoodTail.tail_nn K O X hg (8) 0
  have et0 : (VGoodTail.tt K X (8) (9)) = (K.add (K.mul (co K X (8)) (co K X (8))) (K.neg (K.mul (co K X (7)) (co K X (9))))) := rfl
  have t1 : O.le K.zero (VGoodTail.tt K X (10) (11)) := VGoodTail.tail_nn K O X hg (10) 0
  have et1 : (VGoodTail.tt K X (10) (11)) = (K.add (K.mul (co K X (10)) (co K X (10))) (K.neg (K.mul (co K X (9)) (co K X (11))))) := rfl
  have t2 : O.le K.zero (VGoodTail.tt K X (8) (11)) := VGoodTail.tail_nn K O X hg (8) 2
  have et2 : (VGoodTail.tt K X (8) (11)) = (K.add (K.mul (co K X (8)) (co K X (10))) (K.neg (K.mul (co K X (7)) (co K X (11))))) := rfl
  have t3 : O.le K.zero (VGoodTail.tt K X (12) (13)) := VGoodTail.tail_nn K O X hg (12) 0
  have et3 : (VGoodTail.tt K X (12) (13)) = (K.add (K.mul (co K X (12)) (co K X (12))) (K.neg (K.mul (co K X (11)) (co K X (13))))) := rfl
  have t4 : O.le K.zero (VGoodTail.tt K X (10) (13)) := VGoodTail.tail_nn K O X hg (10) 2
  have et4 : (VGoodTail.tt K X (10) (13)) = (K.add (K.mul (co K X (10)) (co K X (12))) (K.neg (K.mul (co K X (9)) (co K X (13))))) := rfl
  have t5 : O.le K.zero (VGoodTail.tt K X (8) (13)) := VGoodTail.tail_nn K O X hg (8) 4
  have et5 : (VGoodTail.tt K X (8) (13)) = (K.add (K.mul (co K X (8)) (co K X (12))) (K.neg (K.mul (co K X (7)) (co K X (13))))) := rfl
  have hp : O.le K.zero (K.add (K.add (K.add (K.add (K.add (VGoodTail.tt K X (8) (9)) (VGoodTail.tt K X (10) (11))) (VGoodTail.tt K X (8) (11))) (VGoodTail.tt K X (12) (13))) (VGoodTail.tt K X (10) (13))) (VGoodTail.tt K X (8) (13))) := by
    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 _ _ t0 t1) t2) t3) t4) t5)
  refine O.le_trans _ _ _ hp (TripleE.le_of_eq' K O _ _ ?_)
  rw [kv, et0, et1, et2, et3, et4, et5]
  ring_z

theorem kna_2_1 (X : List R) (hg : L1Full.Good K O X) :
    O.le K.zero (CBLib.kern K (Fa K X) (Ga K X) 5 (2) (4)) := by
  have kv : CBLib.kern K (Fa K X) (Ga K X) 5 (2) (4) = (K.add (K.add (K.add (K.add (K.add K.zero (K.add (K.mul (co K X (8)) (co K X (9))) (K.neg (K.mul (co K X (10)) (co K X (7)))))) (K.add (K.mul (K.add (co K X (9)) (co K X (7))) (K.add (co K X (10)) (co K X (8)))) (K.neg (K.mul (K.add (co K X (11)) (co K X (9))) (K.add (co K X (8)) (co K X (6))))))) (K.add (K.mul (K.add (K.add (co K X (10)) (co   -- (+863 chars; full line in the Zenodo source)
  have t0 : O.le K.zero (VGoodTail.tt K X (8) (10)) := VGoodTail.tail_nn K O X hg (8) 1
  have et0 : (VGoodTail.tt K X (8) (10)) = (K.add (K.mul (co K X (8)) (co K X (9))) (K.neg (K.mul (co K X (7)) (co K X (10))))) := rfl
  have t1 : O.le K.zero (VGoodTail.tt K X (9) (11)) := VGoodTail.tail_nn K O X hg (9) 1
  have et1 : (VGoodTail.tt K X (9) (11)) = (K.add (K.mul (co K X (9)) (co K X (10))) (K.neg (K.mul (co K X (8)) (co K X (11))))) := rfl
  have t2 : O.le K.zero (VGoodTail.tt K X (7) (11)) := VGoodTail.tail_nn K O X hg (7) 3
  have et2 : (VGoodTail.tt K X (7) (11)) = (K.add (K.mul (co K X (7)) (co K X (10))) (K.neg (K.mul (co K X (6)) (co K X (11))))) := rfl
  have t3 : O.le K.zero (VGoodTail.tt K X (7) (9)) := VGoodTail.tail_nn K O X hg (7) 1
  have et3 : (VGoodTail.tt K X (7) (9)) = (K.add (K.mul (co K X (7)) (co K X (8))) (K.neg (K.mul (co K X (6)) (co K X (9))))) := rfl
  have t4 : O.le K.zero (VGoodTail.tt K X (10) (12)) := VGoodTail.tail_nn K O X hg (10) 1
  have et4 : (VGoodTail.tt K X (10) (12)) = (K.add (K.mul (co K X (10)) (co K X (11))) (K.neg (K.mul (co K X (9)) (co K X (12))))) := rfl
  have t5 : O.le K.zero (VGoodTail.tt K X (8) (12)) := VGoodTail.tail_nn K O X hg (8) 3
  have et5 : (VGoodTail.tt K X (8) (12)) = (K.add (K.mul (co K X (8)) (co K X (11))) (K.neg (K.mul (co K X (7)) (co K X (12))))) := rfl
  have t6 : O.le K.zero (VGoodTail.tt K X (11) (13)) := VGoodTail.tail_nn K O X hg (11) 1
  have et6 : (VGoodTail.tt K X (11) (13)) = (K.add (K.mul (co K X (11)) (co K X (12))) (K.neg (K.mul (co K X (10)) (co K X (13))))) := rfl
  have t7 : O.le K.zero (VGoodTail.tt K X (9) (13)) := VGoodTail.tail_nn K O X hg (9) 3
  have et7 : (VGoodTail.tt K X (9) (13)) = (K.add (K.mul (co K X (9)) (co K X (12))) (K.neg (K.mul (co K X (8)) (co K X (13))))) := rfl
  have t8 : O.le K.zero (VGoodTail.tt K X (7) (13)) := VGoodTail.tail_nn K O X hg (7) 5
  have et8 : (VGoodTail.tt K X (7) (13)) = (K.add (K.mul (co K X (7)) (co K X (12))) (K.neg (K.mul (co K X (6)) (co K X (13))))) := rfl
  have t9 : O.le K.zero (VGoodTail.tt K X (12) (14)) := VGoodTail.tail_nn K O X hg (12) 1
  have et9 : (VGoodTail.tt K X (12) (14)) = (K.add (K.mul (co K X (12)) (co K X (13))) (K.neg (K.mul (co K X (11)) (co K X (14))))) := rfl
  have t10 : O.le K.zero (VGoodTail.tt K X (10) (14)) := VGoodTail.tail_nn K O X hg (10) 3
  have et10 : (VGoodTail.tt K X (10) (14)) = (K.add (K.mul (co K X (10)) (co K X (13))) (K.neg (K.mul (co K X (9)) (co K X (14))))) := rfl
  have t11 : O.le K.zero (VGoodTail.tt K X (8) (14)) := VGoodTail.tail_nn K O X hg (8) 5
  have et11 : (VGoodTail.tt K X (8) (14)) = (K.add (K.mul (co K X (8)) (co K X (13))) (K.neg (K.mul (co K X (7)) (co K X (14))))) := rfl
  have hp : 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 (VGoodTail.tt K X (8) (10)) (VGoodTail.tt K X (9) (11))) (VGoodTail.tt K X (7) (11))) (VGoodTail.tt K X (7) (9))) (VGoodTail.tt K X (10) (12))) (VGoodTail.tt K X (8) (12))) (VGoodTail.tt K X (11) (13))) (VGoodTail.tt K X (9) (13))) (VGoodTail.tt K X (7) (13))) (VGoodTail.tt K X (12) (14))) (VGoodTa  -- (+56 chars; full line in the Zenodo source)
    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 _ _ (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 _ _ t0 t1) t2) t3) t4) t5) t6) t7) t8) t9) t10) t11)
  refine O.le_trans _ _ _ hp (TripleE.le_of_eq' K O _ _ ?_)
  rw [kv, et0, et1, et2, et3, et4, et5, et6, et7, et8, et9, et10, et11]
  ring_z

theorem kna_2_2 (X : List R) (hg : L1Full.Good K O X) :
    O.le K.zero (CBLib.kern K (Fa K X) (Ga K X) 5 (2) (5)) := by
  have kv : CBLib.kern K (Fa K X) (Ga K X) 5 (2) (5) = (K.add (K.add (K.add (K.add (K.add K.zero (K.add (K.mul (co K X (8)) (co K X (10))) (K.neg (K.mul (co K X (11)) (co K X (7)))))) (K.add (K.mul (K.add (co K X (9)) (co K X (7))) (K.add (co K X (11)) (co K X (9)))) (K.neg (K.mul (K.add (co K X (12)) (co K X (10))) (K.add (co K X (8)) (co K X (6))))))) (K.add (K.mul (K.add (K.add (co K X (10)) (c  -- (+868 chars; full line in the Zenodo source)
  have t0 : O.le K.zero (VGoodTail.tt K X (8) (11)) := VGoodTail.tail_nn K O X hg (8) 2
  have et0 : (VGoodTail.tt K X (8) (11)) = (K.add (K.mul (co K X (8)) (co K X (10))) (K.neg (K.mul (co K X (7)) (co K X (11))))) := rfl
  have t1 : O.le K.zero (VGoodTail.tt K X (9) (12)) := VGoodTail.tail_nn K O X hg (9) 2
  have et1 : (VGoodTail.tt K X (9) (12)) = (K.add (K.mul (co K X (9)) (co K X (11))) (K.neg (K.mul (co K X (8)) (co K X (12))))) := rfl
  have t2 : O.le K.zero (VGoodTail.tt K X (7) (12)) := VGoodTail.tail_nn K O X hg (7) 4
  have et2 : (VGoodTail.tt K X (7) (12)) = (K.add (K.mul (co K X (7)) (co K X (11))) (K.neg (K.mul (co K X (6)) (co K X (12))))) := rfl
  have t3 : O.le K.zero (VGoodTail.tt K X (7) (10)) := VGoodTail.tail_nn K O X hg (7) 2
  have et3 : (VGoodTail.tt K X (7) (10)) = (K.add (K.mul (co K X (7)) (co K X (9))) (K.neg (K.mul (co K X (6)) (co K X (10))))) := rfl
  have t4 : O.le K.zero (VGoodTail.tt K X (10) (13)) := VGoodTail.tail_nn K O X hg (10) 2
  have et4 : (VGoodTail.tt K X (10) (13)) = (K.add (K.mul (co K X (10)) (co K X (12))) (K.neg (K.mul (co K X (9)) (co K X (13))))) := rfl
  have t5 : O.le K.zero (VGoodTail.tt K X (10) (11)) := VGoodTail.tail_nn K O X hg (10) 0
  have et5 : (VGoodTail.tt K X (10) (11)) = (K.add (K.mul (co K X (10)) (co K X (10))) (K.neg (K.mul (co K X (9)) (co K X (11))))) := rfl
  have t6 : O.le K.zero (VGoodTail.tt K X (8) (13)) := VGoodTail.tail_nn K O X hg (8) 4
  have et6 : (VGoodTail.tt K X (8) (13)) = (K.add (K.mul (co K X (8)) (co K X (12))) (K.neg (K.mul (co K X (7)) (co K X (13))))) := rfl
  have t7 : O.le K.zero (VGoodTail.tt K X (8) (9)) := VGoodTail.tail_nn K O X hg (8) 0
  have et7 : (VGoodTail.tt K X (8) (9)) = (K.add (K.mul (co K X (8)) (co K X (8))) (K.neg (K.mul (co K X (7)) (co K X (9))))) := rfl
  have t8 : O.le K.zero (VGoodTail.tt K X (6) (13)) := VGoodTail.tail_nn K O X hg (6) 6
  have et8 : (VGoodTail.tt K X (6) (13)) = (K.add (K.mul (co K X (6)) (co K X (12))) (K.neg (K.mul (co K X (5)) (co K X (13))))) := rfl
  have t9 : O.le K.zero (VGoodTail.tt K X (6) (11)) := VGoodTail.tail_nn K O X hg (6) 4
  have et9 : (VGoodTail.tt K X (6) (11)) = (K.add (K.mul (co K X (6)) (co K X (10))) (K.neg (K.mul (co K X (5)) (co K X (11))))) := rfl
  have t10 : O.le K.zero (VGoodTail.tt K X (6) (9)) := VGoodTail.tail_nn K O X hg (6) 2
  have et10 : (VGoodTail.tt K X (6) (9)) = (K.add (K.mul (co K X (6)) (co K X (8))) (K.neg (K.mul (co K X (5)) (co K X (9))))) := rfl
  have t11 : O.le K.zero (VGoodTail.tt K X (11) (14)) := VGoodTail.tail_nn K O X hg (11) 2
  have et11 : (VGoodTail.tt K X (11) (14)) = (K.add (K.mul (co K X (11)) (co K X (13))) (K.neg (K.mul (co K X (10)) (co K X (14))))) := rfl
  have t12 : O.le K.zero (VGoodTail.tt K X (9) (14)) := VGoodTail.tail_nn K O X hg (9) 4
  have et12 : (VGoodTail.tt K X (9) (14)) = (K.add (K.mul (co K X (9)) (co K X (13))) (K.neg (K.mul (co K X (8)) (co K X (14))))) := rfl
  have t13 : O.le K.zero (VGoodTail.tt K X (7) (14)) := VGoodTail.tail_nn K O X hg (7) 6
  have et13 : (VGoodTail.tt K X (7) (14)) = (K.add (K.mul (co K X (7)) (co K X (13))) (K.neg (K.mul (co K X (6)) (co K X (14))))) := rfl
  have t14 : O.le K.zero (VGoodTail.tt K X (12) (15)) := VGoodTail.tail_nn K O X hg (12) 2
  have et14 : (VGoodTail.tt K X (12) (15)) = (K.add (K.mul (co K X (12)) (co K X (14))) (K.neg (K.mul (co K X (11)) (co K X (15))))) := rfl
  have t15 : O.le K.zero (VGoodTail.tt K X (12) (13)) := VGoodTail.tail_nn K O X hg (12) 0
  have et15 : (VGoodTail.tt K X (12) (13)) = (K.add (K.mul (co K X (12)) (co K X (12))) (K.neg (K.mul (co K X (11)) (co K X (13))))) := rfl
  have t16 : O.le K.zero (VGoodTail.tt K X (10) (15)) := VGoodTail.tail_nn K O X hg (10) 4
  have et16 : (VGoodTail.tt K X (10) (15)) = (K.add (K.mul (co K X (10)) (co K X (14))) (K.neg (K.mul (co K X (9)) (co K X (15))))) := rfl
  have t17 : O.le K.zero (VGoodTail.tt K X (8) (15)) := VGoodTail.tail_nn K O X hg (8) 6
  have et17 : (VGoodTail.tt K X (8) (15)) = (K.add (K.mul (co K X (8)) (co K X (14))) (K.neg (K.mul (co K X (7)) (co K X (15))))) := rfl
  have hp : 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.add (K.add (K.add (K.add (K.add (K.add (K.add (VGoodTail.tt K X (8) (11)) (VGoodTail.tt K X (8) (11))) (VGoodTail.tt K X (9) (12))) (VGoodTail.tt K X (7) (12))) (VGoodTail.tt K X (7) (10))) (VGoodTail.tt K X (10) (13))) (VGoodTail.tt K X (10) (13))) (VGoodTail.tt K X (10) (11))) (V  -- (+382 chars; full line in the Zenodo source)
    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 _ _ (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 _ _ (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   -- (+140 chars; full line in the Zenodo source)
  refine O.le_trans _ _ _ hp (TripleE.le_of_eq' K O _ _ ?_)
  rw [kv, et0, et1, et2, et3, et4, et5, et6, et7, et8, et9, et10, et11, et12, et13, et14, et15, et16, et17]
  ring_z

theorem kna_2_3 (X : List R) (hg : L1Full.Good K O X) :
    O.le K.zero (CBLib.kern K (Fa K X) (Ga K X) 5 (2) (6)) := by
  have kv : CBLib.kern K (Fa K X) (Ga K X) 5 (2) (6) = (K.add (K.add (K.add (K.add (K.add K.zero (K.add (K.mul (co K X (8)) (co K X (11))) (K.neg (K.mul (co K X (12)) (co K X (7)))))) (K.add (K.mul (K.add (co K X (9)) (co K X (7))) (K.add (co K X (12)) (co K X (10)))) (K.neg (K.mul (K.add (co K X (13)) (co K X (11))) (K.add (co K X (8)) (co K X (6))))))) (K.add (K.mul (K.add (K.add (co K X (10)) (  -- (+872 chars; full line in the Zenodo source)
  have t0 : O.le K.zero (VGoodTail.tt K X (8) (12)) := VGoodTail.tail_nn K O X hg (8) 3
  have et0 : (VGoodTail.tt K X (8) (12)) = (K.add (K.mul (co K X (8)) (co K X (11))) (K.neg (K.mul (co K X (7)) (co K X (12))))) := rfl
  have t1 : O.le K.zero (VGoodTail.tt K X (9) (13)) := VGoodTail.tail_nn K O X hg (9) 3
  have et1 : (VGoodTail.tt K X (9) (13)) = (K.add (K.mul (co K X (9)) (co K X (12))) (K.neg (K.mul (co K X (8)) (co K X (13))))) := rfl
  have t2 : O.le K.zero (VGoodTail.tt K X (9) (11)) := VGoodTail.tail_nn K O X hg (9) 1
  have et2 : (VGoodTail.tt K X (9) (11)) = (K.add (K.mul (co K X (9)) (co K X (10))) (K.neg (K.mul (co K X (8)) (co K X (11))))) := rfl
  have t3 : O.le K.zero (VGoodTail.tt K X (7) (13)) := VGoodTail.tail_nn K O X hg (7) 5
  have et3 : (VGoodTail.tt K X (7) (13)) = (K.add (K.mul (co K X (7)) (co K X (12))) (K.neg (K.mul (co K X (6)) (co K X (13))))) := rfl
  have t4 : O.le K.zero (VGoodTail.tt K X (7) (11)) := VGoodTail.tail_nn K O X hg (7) 3
  have et4 : (VGoodTail.tt K X (7) (11)) = (K.add (K.mul (co K X (7)) (co K X (10))) (K.neg (K.mul (co K X (6)) (co K X (11))))) := rfl
  have t5 : O.le K.zero (VGoodTail.tt K X (10) (14)) := VGoodTail.tail_nn K O X hg (10) 3
  have et5 : (VGoodTail.tt K X (10) (14)) = (K.add (K.mul (co K X (10)) (co K X (13))) (K.neg (K.mul (co K X (9)) (co K X (14))))) := rfl
  have t6 : O.le K.zero (VGoodTail.tt K X (10) (12)) := VGoodTail.tail_nn K O X hg (10) 1
  have et6 : (VGoodTail.tt K X (10) (12)) = (K.add (K.mul (co K X (10)) (co K X (11))) (K.neg (K.mul (co K X (9)) (co K X (12))))) := rfl
  have t7 : O.le K.zero (VGoodTail.tt K X (8) (14)) := VGoodTail.tail_nn K O X hg (8) 5
  have et7 : (VGoodTail.tt K X (8) (14)) = (K.add (K.mul (co K X (8)) (co K X (13))) (K.neg (K.mul (co K X (7)) (co K X (14))))) := rfl
  have t8 : O.le K.zero (VGoodTail.tt K X (8) (10)) := VGoodTail.tail_nn K O X hg (8) 1
  have et8 : (VGoodTail.tt K X (8) (10)) = (K.add (K.mul (co K X (8)) (co K X (9))) (K.neg (K.mul (co K X (7)) (co K X (10))))) := rfl
  have t9 : O.le K.zero (VGoodTail.tt K X (6) (14)) := VGoodTail.tail_nn K O X hg (6) 7
  have et9 : (VGoodTail.tt K X (6) (14)) = (K.add (K.mul (co K X (6)) (co K X (13))) (K.neg (K.mul (co K X (5)) (co K X (14))))) := rfl
  have t10 : O.le K.zero (VGoodTail.tt K X (6) (12)) := VGoodTail.tail_nn K O X hg (6) 5
  have et10 : (VGoodTail.tt K X (6) (12)) = (K.add (K.mul (co K X (6)) (co K X (11))) (K.neg (K.mul (co K X (5)) (co K X (12))))) := rfl
  have t11 : O.le K.zero (VGoodTail.tt K X (6) (10)) := VGoodTail.tail_nn K O X hg (6) 3
  have et11 : (VGoodTail.tt K X (6) (10)) = (K.add (K.mul (co K X (6)) (co K X (9))) (K.neg (K.mul (co K X (5)) (co K X (10))))) := rfl
  have t12 : O.le K.zero (VGoodTail.tt K X (11) (15)) := VGoodTail.tail_nn K O X hg (11) 3
  have et12 : (VGoodTail.tt K X (11) (15)) = (K.add (K.mul (co K X (11)) (co K X (14))) (K.neg (K.mul (co K X (10)) (co K X (15))))) := rfl
  have t13 : O.le K.zero (VGoodTail.tt K X (11) (13)) := VGoodTail.tail_nn K O X hg (11) 1
  have et13 : (VGoodTail.tt K X (11) (13)) = (K.add (K.mul (co K X (11)) (co K X (12))) (K.neg (K.mul (co K X (10)) (co K X (13))))) := rfl
  have t14 : O.le K.zero (VGoodTail.tt K X (9) (15)) := VGoodTail.tail_nn K O X hg (9) 5
  have et14 : (VGoodTail.tt K X (9) (15)) = (K.add (K.mul (co K X (9)) (co K X (14))) (K.neg (K.mul (co K X (8)) (co K X (15))))) := rfl
  have t15 : O.le K.zero (VGoodTail.tt K X (7) (15)) := VGoodTail.tail_nn K O X hg (7) 7
  have et15 : (VGoodTail.tt K X (7) (15)) = (K.add (K.mul (co K X (7)) (co K X (14))) (K.neg (K.mul (co K X (6)) (co K X (15))))) := rfl
  have t16 : O.le K.zero (VGoodTail.tt K X (7) (9)) := VGoodTail.tail_nn K O X hg (7) 1
  have et16 : (VGoodTail.tt K X (7) (9)) = (K.add (K.mul (co K X (7)) (co K X (8))) (K.neg (K.mul (co K X (6)) (co K X (9))))) := rfl
  have t17 : O.le K.zero (VGoodTail.tt K X (12) (16)) := VGoodTail.tail_nn K O X hg (12) 3
  have et17 : (VGoodTail.tt K X (12) (16)) = (K.add (K.mul (co K X (12)) (co K X (15))) (K.neg (K.mul (co K X (11)) (co K X (16))))) := rfl
  have t18 : O.le K.zero (VGoodTail.tt K X (12) (14)) := VGoodTail.tail_nn K O X hg (12) 1
  have et18 : (VGoodTail.tt K X (12) (14)) = (K.add (K.mul (co K X (12)) (co K X (13))) (K.neg (K.mul (co K X (11)) (co K X (14))))) := rfl
  have t19 : O.le K.zero (VGoodTail.tt K X (10) (16)) := VGoodTail.tail_nn K O X hg (10) 5
  have et19 : (VGoodTail.tt K X (10) (16)) = (K.add (K.mul (co K X (10)) (co K X (15))) (K.neg (K.mul (co K X (9)) (co K X (16))))) := rfl
  have t20 : O.le K.zero (VGoodTail.tt K X (8) (16)) := VGoodTail.tail_nn K O X hg (8) 7
  have et20 : (VGoodTail.tt K X (8) (16)) = (K.add (K.mul (co K X (8)) (co K X (15))) (K.neg (K.mul (co K X (7)) (co K X (16))))) := rfl
  have hp : 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.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 (VGoodTail.tt K X (8) (12)) (VGoodTail.tt K X (8) (12))) (VGoodTail.tt K X (9) (13))) (VGoodTail.tt K X (9) (13))) (VGoodTail.tt K X (9) (11))) (VGoodTail.tt K X (7) (13))) (VGoodTail.tt K X (7)  -- (+600 chars; full line in the Zenodo source)
    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 _ _ (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 _ _ (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   -- (+299 chars; full line in the Zenodo source)
  refine O.le_trans _ _ _ hp (TripleE.le_of_eq' K O _ _ ?_)
  rw [kv, et0, et1, et2, et3, et4, et5, et6, et7, et8, et9, et10, et11, et12, et13, et14, et15, et16, et17, et18, et19, et20]
  ring_z

theorem kna_2_4 (X : List R) (hg : L1Full.Good K O X) :
    O.le K.zero (CBLib.kern K (Fa K X) (Ga K X) 5 (2) (7)) := by
  have kv : CBLib.kern K (Fa K X) (Ga K X) 5 (2) (7) = (K.add (K.add (K.add (K.add (K.add K.zero (K.add (K.mul (co K X (8)) (co K X (12))) (K.neg (K.mul (co K X (13)) (co K X (7)))))) (K.add (K.mul (K.add (co K X (9)) (co K X (7))) (K.add (co K X (13)) (co K X (11)))) (K.neg (K.mul (K.add (co K X (14)) (co K X (12))) (K.add (co K X (8)) (co K X (6))))))) (K.add (K.mul (K.add (K.add (co K X (10)) (  -- (+875 chars; full line in the Zenodo source)
  have t0 : O.le K.zero (VGoodTail.tt K X (8) (13)) := VGoodTail.tail_nn K O X hg (8) 4
  have et0 : (VGoodTail.tt K X (8) (13)) = (K.add (K.mul (co K X (8)) (co K X (12))) (K.neg (K.mul (co K X (7)) (co K X (13))))) := rfl
  have t1 : O.le K.zero (VGoodTail.tt K X (9) (14)) := VGoodTail.tail_nn K O X hg (9) 4
  have et1 : (VGoodTail.tt K X (9) (14)) = (K.add (K.mul (co K X (9)) (co K X (13))) (K.neg (K.mul (co K X (8)) (co K X (14))))) := rfl
  have t2 : O.le K.zero (VGoodTail.tt K X (9) (12)) := VGoodTail.tail_nn K O X hg (9) 2
  have et2 : (VGoodTail.tt K X (9) (12)) = (K.add (K.mul (co K X (9)) (co K X (11))) (K.neg (K.mul (co K X (8)) (co K X (12))))) := rfl
  have t3 : O.le K.zero (VGoodTail.tt K X (7) (14)) := VGoodTail.tail_nn K O X hg (7) 6
  have et3 : (VGoodTail.tt K X (7) (14)) = (K.add (K.mul (co K X (7)) (co K X (13))) (K.neg (K.mul (co K X (6)) (co K X (14))))) := rfl
  have t4 : O.le K.zero (VGoodTail.tt K X (7) (12)) := VGoodTail.tail_nn K O X hg (7) 4
  have et4 : (VGoodTail.tt K X (7) (12)) = (K.add (K.mul (co K X (7)) (co K X (11))) (K.neg (K.mul (co K X (6)) (co K X (12))))) := rfl
  have t5 : O.le K.zero (VGoodTail.tt K X (10) (15)) := VGoodTail.tail_nn K O X hg (10) 4
  have et5 : (VGoodTail.tt K X (10) (15)) = (K.add (K.mul (co K X (10)) (co K X (14))) (K.neg (K.mul (co K X (9)) (co K X (15))))) := rfl
  have t6 : O.le K.zero (VGoodTail.tt K X (10) (13)) := VGoodTail.tail_nn K O X hg (10) 2
  have et6 : (VGoodTail.tt K X (10) (13)) = (K.add (K.mul (co K X (10)) (co K X (12))) (K.neg (K.mul (co K X (9)) (co K X (13))))) := rfl
  have t7 : O.le K.zero (VGoodTail.tt K X (10) (11)) := VGoodTail.tail_nn K O X hg (10) 0
  have et7 : (VGoodTail.tt K X (10) (11)) = (K.add (K.mul (co K X (10)) (co K X (10))) (K.neg (K.mul (co K X (9)) (co K X (11))))) := rfl
  have t8 : O.le K.zero (VGoodTail.tt K X (8) (15)) := VGoodTail.tail_nn K O X hg (8) 6
  have et8 : (VGoodTail.tt K X (8) (15)) = (K.add (K.mul (co K X (8)) (co K X (14))) (K.neg (K.mul (co K X (7)) (co K X (15))))) := rfl
  have t9 : O.le K.zero (VGoodTail.tt K X (8) (11)) := VGoodTail.tail_nn K O X hg (8) 2
  have et9 : (VGoodTail.tt K X (8) (11)) = (K.add (K.mul (co K X (8)) (co K X (10))) (K.neg (K.mul (co K X (7)) (co K X (11))))) := rfl
  have t10 : O.le K.zero (VGoodTail.tt K X (6) (15)) := VGoodTail.tail_nn K O X hg (6) 8
  have et10 : (VGoodTail.tt K X (6) (15)) = (K.add (K.mul (co K X (6)) (co K X (14))) (K.neg (K.mul (co K X (5)) (co K X (15))))) := rfl
  have t11 : O.le K.zero (VGoodTail.tt K X (6) (13)) := VGoodTail.tail_nn K O X hg (6) 6
  have et11 : (VGoodTail.tt K X (6) (13)) = (K.add (K.mul (co K X (6)) (co K X (12))) (K.neg (K.mul (co K X (5)) (co K X (13))))) := rfl
  have t12 : O.le K.zero (VGoodTail.tt K X (6) (11)) := VGoodTail.tail_nn K O X hg (6) 4
  have et12 : (VGoodTail.tt K X (6) (11)) = (K.add (K.mul (co K X (6)) (co K X (10))) (K.neg (K.mul (co K X (5)) (co K X (11))))) := rfl
  have t13 : O.le K.zero (VGoodTail.tt K X (11) (16)) := VGoodTail.tail_nn K O X hg (11) 4
  have et13 : (VGoodTail.tt K X (11) (16)) = (K.add (K.mul (co K X (11)) (co K X (15))) (K.neg (K.mul (co K X (10)) (co K X (16))))) := rfl
  have t14 : O.le K.zero (VGoodTail.tt K X (11) (14)) := VGoodTail.tail_nn K O X hg (11) 2
  have et14 : (VGoodTail.tt K X (11) (14)) = (K.add (K.mul (co K X (11)) (co K X (13))) (K.neg (K.mul (co K X (10)) (co K X (14))))) := rfl
  have t15 : O.le K.zero (VGoodTail.tt K X (9) (16)) := VGoodTail.tail_nn K O X hg (9) 6
  have et15 : (VGoodTail.tt K X (9) (16)) = (K.add (K.mul (co K X (9)) (co K X (15))) (K.neg (K.mul (co K X (8)) (co K X (16))))) := rfl
  have t16 : O.le K.zero (VGoodTail.tt K X (7) (16)) := VGoodTail.tail_nn K O X hg (7) 8
  have et16 : (VGoodTail.tt K X (7) (16)) = (K.add (K.mul (co K X (7)) (co K X (15))) (K.neg (K.mul (co K X (6)) (co K X (16))))) := rfl
  have t17 : O.le K.zero (VGoodTail.tt K X (7) (10)) := VGoodTail.tail_nn K O X hg (7) 2
  have et17 : (VGoodTail.tt K X (7) (10)) = (K.add (K.mul (co K X (7)) (co K X (9))) (K.neg (K.mul (co K X (6)) (co K X (10))))) := rfl
  have t18 : O.le K.zero (VGoodTail.tt K X (12) (17)) := VGoodTail.tail_nn K O X hg (12) 4
  have et18 : (VGoodTail.tt K X (12) (17)) = (K.add (K.mul (co K X (12)) (co K X (16))) (K.neg (K.mul (co K X (11)) (co K X (17))))) := rfl
  have t19 : O.le K.zero (VGoodTail.tt K X (12) (15)) := VGoodTail.tail_nn K O X hg (12) 2
  have et19 : (VGoodTail.tt K X (12) (15)) = (K.add (K.mul (co K X (12)) (co K X (14))) (K.neg (K.mul (co K X (11)) (co K X (15))))) := rfl
  have t20 : O.le K.zero (VGoodTail.tt K X (12) (13)) := VGoodTail.tail_nn K O X hg (12) 0
  have et20 : (VGoodTail.tt K X (12) (13)) = (K.add (K.mul (co K X (12)) (co K X (12))) (K.neg (K.mul (co K X (11)) (co K X (13))))) := rfl
  have t21 : O.le K.zero (VGoodTail.tt K X (10) (17)) := VGoodTail.tail_nn K O X hg (10) 6
  have et21 : (VGoodTail.tt K X (10) (17)) = (K.add (K.mul (co K X (10)) (co K X (16))) (K.neg (K.mul (co K X (9)) (co K X (17))))) := rfl
  have t22 : O.le K.zero (VGoodTail.tt K X (8) (17)) := VGoodTail.tail_nn K O X hg (8) 8
  have et22 : (VGoodTail.tt K X (8) (17)) = (K.add (K.mul (co K X (8)) (co K X (16))) (K.neg (K.mul (co K X (7)) (co K X (17))))) := rfl
  have t23 : O.le K.zero (VGoodTail.tt K X (8) (9)) := VGoodTail.tail_nn K O X hg (8) 0
  have et23 : (VGoodTail.tt K X (8) (9)) = (K.add (K.mul (co K X (8)) (co K X (8))) (K.neg (K.mul (co K X (7)) (co K X (9))))) := rfl
  have hp : 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.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 (VGoodTail.tt K X (8) (13)) (VGoodTail.tt K X (8) (13))) (VGoodTail.tt K X (8) (13))) (VGoodTail.tt K X (9) (14))) (VGoodTail.tt K X (9) (14))) (VGoodTa  -- (+819 chars; full line in the Zenodo source)
    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 _ _ (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 _ _ (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   -- (+458 chars; full line in the Zenodo source)
  refine O.le_trans _ _ _ hp (TripleE.le_of_eq' K O _ _ ?_)
  rw [kv, et0, et1, et2, et3, et4, et5, et6, et7, et8, et9, et10, et11, et12, et13, et14, et15, et16, et17, et18, et19, et20, et21, et22, et23]
  ring_z

theorem kna_2_5 (X : List R) (hg : L1Full.Good K O X) :
    O.le K.zero (CBLib.kern K (Fa K X) (Ga K X) 5 (2) (8)) := by
  have kv : CBLib.kern K (Fa K X) (Ga K X) 5 (2) (8) = (K.add (K.add (K.add (K.add (K.add K.zero (K.add (K.mul (co K X (8)) (co K X (13))) (K.neg (K.mul (co K X (14)) (co K X (7)))))) (K.add (K.mul (K.add (co K X (9)) (co K X (7))) (K.add (co K X (14)) (co K X (12)))) (K.neg (K.mul (K.add (co K X (15)) (co K X (13))) (K.add (co K X (8)) (co K X (6))))))) (K.add (K.mul (K.add (K.add (co K X (10)) (  -- (+877 chars; full line in the Zenodo source)
  have t0 : O.le K.zero (VGoodTail.tt K X (8) (14)) := VGoodTail.tail_nn K O X hg (8) 5
  have et0 : (VGoodTail.tt K X (8) (14)) = (K.add (K.mul (co K X (8)) (co K X (13))) (K.neg (K.mul (co K X (7)) (co K X (14))))) := rfl
  have t1 : O.le K.zero (VGoodTail.tt K X (9) (15)) := VGoodTail.tail_nn K O X hg (9) 5
  have et1 : (VGoodTail.tt K X (9) (15)) = (K.add (K.mul (co K X (9)) (co K X (14))) (K.neg (K.mul (co K X (8)) (co K X (15))))) := rfl
  have t2 : O.le K.zero (VGoodTail.tt K X (9) (13)) := VGoodTail.tail_nn K O X hg (9) 3
  have et2 : (VGoodTail.tt K X (9) (13)) = (K.add (K.mul (co K X (9)) (co K X (12))) (K.neg (K.mul (co K X (8)) (co K X (13))))) := rfl
  have t3 : O.le K.zero (VGoodTail.tt K X (7) (15)) := VGoodTail.tail_nn K O X hg (7) 7
  have et3 : (VGoodTail.tt K X (7) (15)) = (K.add (K.mul (co K X (7)) (co K X (14))) (K.neg (K.mul (co K X (6)) (co K X (15))))) := rfl
  have t4 : O.le K.zero (VGoodTail.tt K X (7) (13)) := VGoodTail.tail_nn K O X hg (7) 5
  have et4 : (VGoodTail.tt K X (7) (13)) = (K.add (K.mul (co K X (7)) (co K X (12))) (K.neg (K.mul (co K X (6)) (co K X (13))))) := rfl
  have t5 : O.le K.zero (VGoodTail.tt K X (10) (16)) := VGoodTail.tail_nn K O X hg (10) 5
  have et5 : (VGoodTail.tt K X (10) (16)) = (K.add (K.mul (co K X (10)) (co K X (15))) (K.neg (K.mul (co K X (9)) (co K X (16))))) := rfl
  have t6 : O.le K.zero (VGoodTail.tt K X (10) (14)) := VGoodTail.tail_nn K O X hg (10) 3
  have et6 : (VGoodTail.tt K X (10) (14)) = (K.add (K.mul (co K X (10)) (co K X (13))) (K.neg (K.mul (co K X (9)) (co K X (14))))) := rfl
  have t7 : O.le K.zero (VGoodTail.tt K X (10) (12)) := VGoodTail.tail_nn K O X hg (10) 1
  have et7 : (VGoodTail.tt K X (10) (12)) = (K.add (K.mul (co K X (10)) (co K X (11))) (K.neg (K.mul (co K X (9)) (co K X (12))))) := rfl
  have t8 : O.le K.zero (VGoodTail.tt K X (8) (16)) := VGoodTail.tail_nn K O X hg (8) 7
  have et8 : (VGoodTail.tt K X (8) (16)) = (K.add (K.mul (co K X (8)) (co K X (15))) (K.neg (K.mul (co K X (7)) (co K X (16))))) := rfl
  have t9 : O.le K.zero (VGoodTail.tt K X (8) (12)) := VGoodTail.tail_nn K O X hg (8) 3
  have et9 : (VGoodTail.tt K X (8) (12)) = (K.add (K.mul (co K X (8)) (co K X (11))) (K.neg (K.mul (co K X (7)) (co K X (12))))) := rfl
  have t10 : O.le K.zero (VGoodTail.tt K X (6) (16)) := VGoodTail.tail_nn K O X hg (6) 9
  have et10 : (VGoodTail.tt K X (6) (16)) = (K.add (K.mul (co K X (6)) (co K X (15))) (K.neg (K.mul (co K X (5)) (co K X (16))))) := rfl
  have t11 : O.le K.zero (VGoodTail.tt K X (6) (14)) := VGoodTail.tail_nn K O X hg (6) 7
  have et11 : (VGoodTail.tt K X (6) (14)) = (K.add (K.mul (co K X (6)) (co K X (13))) (K.neg (K.mul (co K X (5)) (co K X (14))))) := rfl
  have t12 : O.le K.zero (VGoodTail.tt K X (6) (12)) := VGoodTail.tail_nn K O X hg (6) 5
  have et12 : (VGoodTail.tt K X (6) (12)) = (K.add (K.mul (co K X (6)) (co K X (11))) (K.neg (K.mul (co K X (5)) (co K X (12))))) := rfl
  have t13 : O.le K.zero (VGoodTail.tt K X (11) (17)) := VGoodTail.tail_nn K O X hg (11) 5
  have et13 : (VGoodTail.tt K X (11) (17)) = (K.add (K.mul (co K X (11)) (co K X (16))) (K.neg (K.mul (co K X (10)) (co K X (17))))) := rfl
  have t14 : O.le K.zero (VGoodTail.tt K X (11) (15)) := VGoodTail.tail_nn K O X hg (11) 3
  have et14 : (VGoodTail.tt K X (11) (15)) = (K.add (K.mul (co K X (11)) (co K X (14))) (K.neg (K.mul (co K X (10)) (co K X (15))))) := rfl
  have t15 : O.le K.zero (VGoodTail.tt K X (11) (13)) := VGoodTail.tail_nn K O X hg (11) 1
  have et15 : (VGoodTail.tt K X (11) (13)) = (K.add (K.mul (co K X (11)) (co K X (12))) (K.neg (K.mul (co K X (10)) (co K X (13))))) := rfl
  have t16 : O.le K.zero (VGoodTail.tt K X (9) (17)) := VGoodTail.tail_nn K O X hg (9) 7
  have et16 : (VGoodTail.tt K X (9) (17)) = (K.add (K.mul (co K X (9)) (co K X (16))) (K.neg (K.mul (co K X (8)) (co K X (17))))) := rfl
  have t17 : O.le K.zero (VGoodTail.tt K X (9) (11)) := VGoodTail.tail_nn K O X hg (9) 1
  have et17 : (VGoodTail.tt K X (9) (11)) = (K.add (K.mul (co K X (9)) (co K X (10))) (K.neg (K.mul (co K X (8)) (co K X (11))))) := rfl
  have t18 : O.le K.zero (VGoodTail.tt K X (7) (17)) := VGoodTail.tail_nn K O X hg (7) 9
  have et18 : (VGoodTail.tt K X (7) (17)) = (K.add (K.mul (co K X (7)) (co K X (16))) (K.neg (K.mul (co K X (6)) (co K X (17))))) := rfl
  have t19 : O.le K.zero (VGoodTail.tt K X (7) (11)) := VGoodTail.tail_nn K O X hg (7) 3
  have et19 : (VGoodTail.tt K X (7) (11)) = (K.add (K.mul (co K X (7)) (co K X (10))) (K.neg (K.mul (co K X (6)) (co K X (11))))) := rfl
  have t20 : O.le K.zero (VGoodTail.tt K X (12) (18)) := VGoodTail.tail_nn K O X hg (12) 5
  have et20 : (VGoodTail.tt K X (12) (18)) = (K.add (K.mul (co K X (12)) (co K X (17))) (K.neg (K.mul (co K X (11)) (co K X (18))))) := rfl
  have t21 : O.le K.zero (VGoodTail.tt K X (12) (16)) := VGoodTail.tail_nn K O X hg (12) 3
  have et21 : (VGoodTail.tt K X (12) (16)) = (K.add (K.mul (co K X (12)) (co K X (15))) (K.neg (K.mul (co K X (11)) (co K X (16))))) := rfl
  have t22 : O.le K.zero (VGoodTail.tt K X (12) (14)) := VGoodTail.tail_nn K O X hg (12) 1
  have et22 : (VGoodTail.tt K X (12) (14)) = (K.add (K.mul (co K X (12)) (co K X (13))) (K.neg (K.mul (co K X (11)) (co K X (14))))) := rfl
  have t23 : O.le K.zero (VGoodTail.tt K X (10) (18)) := VGoodTail.tail_nn K O X hg (10) 7
  have et23 : (VGoodTail.tt K X (10) (18)) = (K.add (K.mul (co K X (10)) (co K X (17))) (K.neg (K.mul (co K X (9)) (co K X (18))))) := rfl
  have t24 : O.le K.zero (VGoodTail.tt K X (8) (18)) := VGoodTail.tail_nn K O X hg (8) 9
  have et24 : (VGoodTail.tt K X (8) (18)) = (K.add (K.mul (co K X (8)) (co K X (17))) (K.neg (K.mul (co K X (7)) (co K X (18))))) := rfl
  have t25 : O.le K.zero (VGoodTail.tt K X (8) (10)) := VGoodTail.tail_nn K O X hg (8) 1
  have et25 : (VGoodTail.tt K X (8) (10)) = (K.add (K.mul (co K X (8)) (co K X (9))) (K.neg (K.mul (co K X (7)) (co K X (10))))) := rfl
  have hp : 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.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 (VGoodTail.tt K X (8) (14)) (VGoodTail.tt K X (8) (14))) (VGoodTail.tt K X (8) (14))) (VGoodTail.tt K X (9) (15))) (VGoodTail.tt K   -- (+929 chars; full line in the Zenodo source)
    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 _ _ (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 _ _ (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   -- (+538 chars; full line in the Zenodo source)
  refine O.le_trans _ _ _ hp (TripleE.le_of_eq' K O _ _ ?_)
  rw [kv, et0, et1, et2, et3, et4, et5, et6, et7, et8, et9, et10, et11, et12, et13, et14, et15, et16, et17, et18, et19, et20, et21, et22, et23, et24, et25]
  ring_z

theorem kna_2_6 (X : List R) (hg : L1Full.Good K O X) :
    O.le K.zero (CBLib.kern K (Fa K X) (Ga K X) 5 (2) (9)) := by
  have kv : CBLib.kern K (Fa K X) (Ga K X) 5 (2) (9) = (K.add (K.add (K.add (K.add (K.add K.zero (K.add (K.mul (co K X (8)) (co K X (14))) (K.neg (K.mul (co K X (15)) (co K X (7)))))) (K.add (K.mul (K.add (co K X (9)) (co K X (7))) (K.add (co K X (15)) (co K X (13)))) (K.neg (K.mul (K.add (co K X (16)) (co K X (14))) (K.add (co K X (8)) (co K X (6))))))) (K.add (K.mul (K.add (K.add (co K X (10)) (  -- (+878 chars; full line in the Zenodo source)
  have t0 : O.le K.zero (VGoodTail.tt K X (8) (15)) := VGoodTail.tail_nn K O X hg (8) 6
  have et0 : (VGoodTail.tt K X (8) (15)) = (K.add (K.mul (co K X (8)) (co K X (14))) (K.neg (K.mul (co K X (7)) (co K X (15))))) := rfl
  have t1 : O.le K.zero (VGoodTail.tt K X (9) (16)) := VGoodTail.tail_nn K O X hg (9) 6
  have et1 : (VGoodTail.tt K X (9) (16)) = (K.add (K.mul (co K X (9)) (co K X (15))) (K.neg (K.mul (co K X (8)) (co K X (16))))) := rfl
  have t2 : O.le K.zero (VGoodTail.tt K X (9) (14)) := VGoodTail.tail_nn K O X hg (9) 4
  have et2 : (VGoodTail.tt K X (9) (14)) = (K.add (K.mul (co K X (9)) (co K X (13))) (K.neg (K.mul (co K X (8)) (co K X (14))))) := rfl
  have t3 : O.le K.zero (VGoodTail.tt K X (7) (16)) := VGoodTail.tail_nn K O X hg (7) 8
  have et3 : (VGoodTail.tt K X (7) (16)) = (K.add (K.mul (co K X (7)) (co K X (15))) (K.neg (K.mul (co K X (6)) (co K X (16))))) := rfl
  have t4 : O.le K.zero (VGoodTail.tt K X (7) (14)) := VGoodTail.tail_nn K O X hg (7) 6
  have et4 : (VGoodTail.tt K X (7) (14)) = (K.add (K.mul (co K X (7)) (co K X (13))) (K.neg (K.mul (co K X (6)) (co K X (14))))) := rfl
  have t5 : O.le K.zero (VGoodTail.tt K X (10) (17)) := VGoodTail.tail_nn K O X hg (10) 6
  have et5 : (VGoodTail.tt K X (10) (17)) = (K.add (K.mul (co K X (10)) (co K X (16))) (K.neg (K.mul (co K X (9)) (co K X (17))))) := rfl
  have t6 : O.le K.zero (VGoodTail.tt K X (10) (15)) := VGoodTail.tail_nn K O X hg (10) 4
  have et6 : (VGoodTail.tt K X (10) (15)) = (K.add (K.mul (co K X (10)) (co K X (14))) (K.neg (K.mul (co K X (9)) (co K X (15))))) := rfl
  have t7 : O.le K.zero (VGoodTail.tt K X (10) (13)) := VGoodTail.tail_nn K O X hg (10) 2
  have et7 : (VGoodTail.tt K X (10) (13)) = (K.add (K.mul (co K X (10)) (co K X (12))) (K.neg (K.mul (co K X (9)) (co K X (13))))) := rfl
  have t8 : O.le K.zero (VGoodTail.tt K X (8) (17)) := VGoodTail.tail_nn K O X hg (8) 8
  have et8 : (VGoodTail.tt K X (8) (17)) = (K.add (K.mul (co K X (8)) (co K X (16))) (K.neg (K.mul (co K X (7)) (co K X (17))))) := rfl
  have t9 : O.le K.zero (VGoodTail.tt K X (8) (13)) := VGoodTail.tail_nn K O X hg (8) 4
  have et9 : (VGoodTail.tt K X (8) (13)) = (K.add (K.mul (co K X (8)) (co K X (12))) (K.neg (K.mul (co K X (7)) (co K X (13))))) := rfl
  have t10 : O.le K.zero (VGoodTail.tt K X (6) (17)) := VGoodTail.tail_nn K O X hg (6) 10
  have et10 : (VGoodTail.tt K X (6) (17)) = (K.add (K.mul (co K X (6)) (co K X (16))) (K.neg (K.mul (co K X (5)) (co K X (17))))) := rfl
  have t11 : O.le K.zero (VGoodTail.tt K X (6) (15)) := VGoodTail.tail_nn K O X hg (6) 8
  have et11 : (VGoodTail.tt K X (6) (15)) = (K.add (K.mul (co K X (6)) (co K X (14))) (K.neg (K.mul (co K X (5)) (co K X (15))))) := rfl
  have t12 : O.le K.zero (VGoodTail.tt K X (6) (13)) := VGoodTail.tail_nn K O X hg (6) 6
  have et12 : (VGoodTail.tt K X (6) (13)) = (K.add (K.mul (co K X (6)) (co K X (12))) (K.neg (K.mul (co K X (5)) (co K X (13))))) := rfl
  have t13 : O.le K.zero (VGoodTail.tt K X (11) (18)) := VGoodTail.tail_nn K O X hg (11) 6
  have et13 : (VGoodTail.tt K X (11) (18)) = (K.add (K.mul (co K X (11)) (co K X (17))) (K.neg (K.mul (co K X (10)) (co K X (18))))) := rfl
  have t14 : O.le K.zero (VGoodTail.tt K X (11) (16)) := VGoodTail.tail_nn K O X hg (11) 4
  have et14 : (VGoodTail.tt K X (11) (16)) = (K.add (K.mul (co K X (11)) (co K X (15))) (K.neg (K.mul (co K X (10)) (co K X (16))))) := rfl
  have t15 : O.le K.zero (VGoodTail.tt K X (11) (14)) := VGoodTail.tail_nn K O X hg (11) 2
  have et15 : (VGoodTail.tt K X (11) (14)) = (K.add (K.mul (co K X (11)) (co K X (13))) (K.neg (K.mul (co K X (10)) (co K X (14))))) := rfl
  have t16 : O.le K.zero (VGoodTail.tt K X (9) (18)) := VGoodTail.tail_nn K O X hg (9) 8
  have et16 : (VGoodTail.tt K X (9) (18)) = (K.add (K.mul (co K X (9)) (co K X (17))) (K.neg (K.mul (co K X (8)) (co K X (18))))) := rfl
  have t17 : O.le K.zero (VGoodTail.tt K X (9) (12)) := VGoodTail.tail_nn K O X hg (9) 2
  have et17 : (VGoodTail.tt K X (9) (12)) = (K.add (K.mul (co K X (9)) (co K X (11))) (K.neg (K.mul (co K X (8)) (co K X (12))))) := rfl
  have t18 : O.le K.zero (VGoodTail.tt K X (7) (18)) := VGoodTail.tail_nn K O X hg (7) 10
  have et18 : (VGoodTail.tt K X (7) (18)) = (K.add (K.mul (co K X (7)) (co K X (17))) (K.neg (K.mul (co K X (6)) (co K X (18))))) := rfl
  have t19 : O.le K.zero (VGoodTail.tt K X (7) (12)) := VGoodTail.tail_nn K O X hg (7) 4
  have et19 : (VGoodTail.tt K X (7) (12)) = (K.add (K.mul (co K X (7)) (co K X (11))) (K.neg (K.mul (co K X (6)) (co K X (12))))) := rfl
  have t20 : O.le K.zero (VGoodTail.tt K X (12) (19)) := VGoodTail.tail_nn K O X hg (12) 6
  have et20 : (VGoodTail.tt K X (12) (19)) = (K.add (K.mul (co K X (12)) (co K X (18))) (K.neg (K.mul (co K X (11)) (co K X (19))))) := rfl
  have t21 : O.le K.zero (VGoodTail.tt K X (12) (17)) := VGoodTail.tail_nn K O X hg (12) 4
  have et21 : (VGoodTail.tt K X (12) (17)) = (K.add (K.mul (co K X (12)) (co K X (16))) (K.neg (K.mul (co K X (11)) (co K X (17))))) := rfl
  have t22 : O.le K.zero (VGoodTail.tt K X (12) (15)) := VGoodTail.tail_nn K O X hg (12) 2
  have et22 : (VGoodTail.tt K X (12) (15)) = (K.add (K.mul (co K X (12)) (co K X (14))) (K.neg (K.mul (co K X (11)) (co K X (15))))) := rfl
  have t23 : O.le K.zero (VGoodTail.tt K X (12) (13)) := VGoodTail.tail_nn K O X hg (12) 0
  have et23 : (VGoodTail.tt K X (12) (13)) = (K.add (K.mul (co K X (12)) (co K X (12))) (K.neg (K.mul (co K X (11)) (co K X (13))))) := rfl
  have t24 : O.le K.zero (VGoodTail.tt K X (10) (19)) := VGoodTail.tail_nn K O X hg (10) 8
  have et24 : (VGoodTail.tt K X (10) (19)) = (K.add (K.mul (co K X (10)) (co K X (18))) (K.neg (K.mul (co K X (9)) (co K X (19))))) := rfl
  have t25 : O.le K.zero (VGoodTail.tt K X (10) (11)) := VGoodTail.tail_nn K O X hg (10) 0
  have et25 : (VGoodTail.tt K X (10) (11)) = (K.add (K.mul (co K X (10)) (co K X (10))) (K.neg (K.mul (co K X (9)) (co K X (11))))) := rfl
  have t26 : O.le K.zero (VGoodTail.tt K X (8) (19)) := VGoodTail.tail_nn K O X hg (8) 10
  have et26 : (VGoodTail.tt K X (8) (19)) = (K.add (K.mul (co K X (8)) (co K X (18))) (K.neg (K.mul (co K X (7)) (co K X (19))))) := rfl
  have t27 : O.le K.zero (VGoodTail.tt K X (8) (11)) := VGoodTail.tail_nn K O X hg (8) 2
  have et27 : (VGoodTail.tt K X (8) (11)) = (K.add (K.mul (co K X (8)) (co K X (10))) (K.neg (K.mul (co K X (7)) (co K X (11))))) := rfl
  have hp : 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.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 (VGoodTail.tt K X (8) (15)) (VGoodTail.tt K X (8) (15))) (VGoodTail.tt K X (8) (15))) (VGoodTail.tt K X (9) (1  -- (+1040 chars; full line in the Zenodo source)
    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 _ _ (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 _ _ (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   -- (+618 chars; full line in the Zenodo source)
  refine O.le_trans _ _ _ hp (TripleE.le_of_eq' K O _ _ ?_)
  rw [kv, et0, et1, et2, et3, et4, et5, et6, et7, et8, et9, et10, et11, et12, et13, et14, et15, et16, et17, et18, et19, et20, et21, et22, et23, et24, et25, et26, et27]
  ring_z

theorem kna_2_7 (X : List R) (hg : L1Full.Good K O X) :
    O.le K.zero (CBLib.kern K (Fa K X) (Ga K X) 5 (2) (10)) := by
  have kv : CBLib.kern K (Fa K X) (Ga K X) 5 (2) (10) = (K.add (K.add (K.add (K.add (K.add K.zero (K.add (K.mul (co K X (8)) (co K X (15))) (K.neg (K.mul (co K X (16)) (co K X (7)))))) (K.add (K.mul (K.add (co K X (9)) (co K X (7))) (K.add (co K X (16)) (co K X (14)))) (K.neg (K.mul (K.add (co K X (17)) (co K X (15))) (K.add (co K X (8)) (co K X (6))))))) (K.add (K.mul (K.add (K.add (co K X (10))   -- (+879 chars; full line in the Zenodo source)
  have t0 : O.le K.zero (VGoodTail.tt K X (8) (16)) := VGoodTail.tail_nn K O X hg (8) 7
  have et0 : (VGoodTail.tt K X (8) (16)) = (K.add (K.mul (co K X (8)) (co K X (15))) (K.neg (K.mul (co K X (7)) (co K X (16))))) := rfl
  have t1 : O.le K.zero (VGoodTail.tt K X (9) (17)) := VGoodTail.tail_nn K O X hg (9) 7
  have et1 : (VGoodTail.tt K X (9) (17)) = (K.add (K.mul (co K X (9)) (co K X (16))) (K.neg (K.mul (co K X (8)) (co K X (17))))) := rfl
  have t2 : O.le K.zero (VGoodTail.tt K X (9) (15)) := VGoodTail.tail_nn K O X hg (9) 5
  have et2 : (VGoodTail.tt K X (9) (15)) = (K.add (K.mul (co K X (9)) (co K X (14))) (K.neg (K.mul (co K X (8)) (co K X (15))))) := rfl
  have t3 : O.le K.zero (VGoodTail.tt K X (7) (17)) := VGoodTail.tail_nn K O X hg (7) 9
  have et3 : (VGoodTail.tt K X (7) (17)) = (K.add (K.mul (co K X (7)) (co K X (16))) (K.neg (K.mul (co K X (6)) (co K X (17))))) := rfl
  have t4 : O.le K.zero (VGoodTail.tt K X (7) (15)) := VGoodTail.tail_nn K O X hg (7) 7
  have et4 : (VGoodTail.tt K X (7) (15)) = (K.add (K.mul (co K X (7)) (co K X (14))) (K.neg (K.mul (co K X (6)) (co K X (15))))) := rfl
  have t5 : O.le K.zero (VGoodTail.tt K X (10) (18)) := VGoodTail.tail_nn K O X hg (10) 7
  have et5 : (VGoodTail.tt K X (10) (18)) = (K.add (K.mul (co K X (10)) (co K X (17))) (K.neg (K.mul (co K X (9)) (co K X (18))))) := rfl
  have t6 : O.le K.zero (VGoodTail.tt K X (10) (16)) := VGoodTail.tail_nn K O X hg (10) 5
  have et6 : (VGoodTail.tt K X (10) (16)) = (K.add (K.mul (co K X (10)) (co K X (15))) (K.neg (K.mul (co K X (9)) (co K X (16))))) := rfl
  have t7 : O.le K.zero (VGoodTail.tt K X (10) (14)) := VGoodTail.tail_nn K O X hg (10) 3
  have et7 : (VGoodTail.tt K X (10) (14)) = (K.add (K.mul (co K X (10)) (co K X (13))) (K.neg (K.mul (co K X (9)) (co K X (14))))) := rfl
  have t8 : O.le K.zero (VGoodTail.tt K X (8) (18)) := VGoodTail.tail_nn K O X hg (8) 9
  have et8 : (VGoodTail.tt K X (8) (18)) = (K.add (K.mul (co K X (8)) (co K X (17))) (K.neg (K.mul (co K X (7)) (co K X (18))))) := rfl
  have t9 : O.le K.zero (VGoodTail.tt K X (8) (14)) := VGoodTail.tail_nn K O X hg (8) 5
  have et9 : (VGoodTail.tt K X (8) (14)) = (K.add (K.mul (co K X (8)) (co K X (13))) (K.neg (K.mul (co K X (7)) (co K X (14))))) := rfl
  have t10 : O.le K.zero (VGoodTail.tt K X (6) (18)) := VGoodTail.tail_nn K O X hg (6) 11
  have et10 : (VGoodTail.tt K X (6) (18)) = (K.add (K.mul (co K X (6)) (co K X (17))) (K.neg (K.mul (co K X (5)) (co K X (18))))) := rfl
  have t11 : O.le K.zero (VGoodTail.tt K X (6) (16)) := VGoodTail.tail_nn K O X hg (6) 9
  have et11 : (VGoodTail.tt K X (6) (16)) = (K.add (K.mul (co K X (6)) (co K X (15))) (K.neg (K.mul (co K X (5)) (co K X (16))))) := rfl
  have t12 : O.le K.zero (VGoodTail.tt K X (6) (14)) := VGoodTail.tail_nn K O X hg (6) 7
  have et12 : (VGoodTail.tt K X (6) (14)) = (K.add (K.mul (co K X (6)) (co K X (13))) (K.neg (K.mul (co K X (5)) (co K X (14))))) := rfl
  have t13 : O.le K.zero (VGoodTail.tt K X (11) (19)) := VGoodTail.tail_nn K O X hg (11) 7
  have et13 : (VGoodTail.tt K X (11) (19)) = (K.add (K.mul (co K X (11)) (co K X (18))) (K.neg (K.mul (co K X (10)) (co K X (19))))) := rfl
  have t14 : O.le K.zero (VGoodTail.tt K X (11) (17)) := VGoodTail.tail_nn K O X hg (11) 5
  have et14 : (VGoodTail.tt K X (11) (17)) = (K.add (K.mul (co K X (11)) (co K X (16))) (K.neg (K.mul (co K X (10)) (co K X (17))))) := rfl
  have t15 : O.le K.zero (VGoodTail.tt K X (11) (15)) := VGoodTail.tail_nn K O X hg (11) 3
  have et15 : (VGoodTail.tt K X (11) (15)) = (K.add (K.mul (co K X (11)) (co K X (14))) (K.neg (K.mul (co K X (10)) (co K X (15))))) := rfl
  have t16 : O.le K.zero (VGoodTail.tt K X (11) (13)) := VGoodTail.tail_nn K O X hg (11) 1
  have et16 : (VGoodTail.tt K X (11) (13)) = (K.add (K.mul (co K X (11)) (co K X (12))) (K.neg (K.mul (co K X (10)) (co K X (13))))) := rfl
  have t17 : O.le K.zero (VGoodTail.tt K X (9) (19)) := VGoodTail.tail_nn K O X hg (9) 9
  have et17 : (VGoodTail.tt K X (9) (19)) = (K.add (K.mul (co K X (9)) (co K X (18))) (K.neg (K.mul (co K X (8)) (co K X (19))))) := rfl
  have t18 : O.le K.zero (VGoodTail.tt K X (9) (13)) := VGoodTail.tail_nn K O X hg (9) 3
  have et18 : (VGoodTail.tt K X (9) (13)) = (K.add (K.mul (co K X (9)) (co K X (12))) (K.neg (K.mul (co K X (8)) (co K X (13))))) := rfl
  have t19 : O.le K.zero (VGoodTail.tt K X (7) (19)) := VGoodTail.tail_nn K O X hg (7) 11
  have et19 : (VGoodTail.tt K X (7) (19)) = (K.add (K.mul (co K X (7)) (co K X (18))) (K.neg (K.mul (co K X (6)) (co K X (19))))) := rfl
  have t20 : O.le K.zero (VGoodTail.tt K X (7) (13)) := VGoodTail.tail_nn K O X hg (7) 5
  have et20 : (VGoodTail.tt K X (7) (13)) = (K.add (K.mul (co K X (7)) (co K X (12))) (K.neg (K.mul (co K X (6)) (co K X (13))))) := rfl
  have t21 : O.le K.zero (VGoodTail.tt K X (12) (20)) := VGoodTail.tail_nn K O X hg (12) 7
  have et21 : (VGoodTail.tt K X (12) (20)) = (K.add (K.mul (co K X (12)) (co K X (19))) (K.neg (K.mul (co K X (11)) (co K X (20))))) := rfl
  have t22 : O.le K.zero (VGoodTail.tt K X (12) (18)) := VGoodTail.tail_nn K O X hg (12) 5
  have et22 : (VGoodTail.tt K X (12) (18)) = (K.add (K.mul (co K X (12)) (co K X (17))) (K.neg (K.mul (co K X (11)) (co K X (18))))) := rfl
  have t23 : O.le K.zero (VGoodTail.tt K X (12) (16)) := VGoodTail.tail_nn K O X hg (12) 3
  have et23 : (VGoodTail.tt K X (12) (16)) = (K.add (K.mul (co K X (12)) (co K X (15))) (K.neg (K.mul (co K X (11)) (co K X (16))))) := rfl
  have t24 : O.le K.zero (VGoodTail.tt K X (12) (14)) := VGoodTail.tail_nn K O X hg (12) 1
  have et24 : (VGoodTail.tt K X (12) (14)) = (K.add (K.mul (co K X (12)) (co K X (13))) (K.neg (K.mul (co K X (11)) (co K X (14))))) := rfl
  have t25 : O.le K.zero (VGoodTail.tt K X (10) (20)) := VGoodTail.tail_nn K O X hg (10) 9
  have et25 : (VGoodTail.tt K X (10) (20)) = (K.add (K.mul (co K X (10)) (co K X (19))) (K.neg (K.mul (co K X (9)) (co K X (20))))) := rfl
  have t26 : O.le K.zero (VGoodTail.tt K X (10) (12)) := VGoodTail.tail_nn K O X hg (10) 1
  have et26 : (VGoodTail.tt K X (10) (12)) = (K.add (K.mul (co K X (10)) (co K X (11))) (K.neg (K.mul (co K X (9)) (co K X (12))))) := rfl
  have t27 : O.le K.zero (VGoodTail.tt K X (8) (20)) := VGoodTail.tail_nn K O X hg (8) 11
  have et27 : (VGoodTail.tt K X (8) (20)) = (K.add (K.mul (co K X (8)) (co K X (19))) (K.neg (K.mul (co K X (7)) (co K X (20))))) := rfl
  have t28 : O.le K.zero (VGoodTail.tt K X (8) (12)) := VGoodTail.tail_nn K O X hg (8) 3
  have et28 : (VGoodTail.tt K X (8) (12)) = (K.add (K.mul (co K X (8)) (co K X (11))) (K.neg (K.mul (co K X (7)) (co K X (12))))) := rfl
  have hp : 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.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 (VGoodTail.tt K X (8) (16)) (VGoodTail.tt K X (8) (16))) (VGoodTail.tt K X (8) (16))) (VGoodTail.tt K X  -- (+1077 chars; full line in the Zenodo source)
    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 _ _ (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 _ _ (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   -- (+645 chars; full line in the Zenodo source)
  refine O.le_trans _ _ _ hp (TripleE.le_of_eq' K O _ _ ?_)
  rw [kv, et0, et1, et2, et3, et4, et5, et6, et7, et8, et9, et10, et11, et12, et13, et14, et15, et16, et17, et18, et19, et20, et21, et22, et23, et24, et25, et26, et27, et28]
  ring_z

theorem kna_2_8 (X : List R) (hg : L1Full.Good K O X) :
    O.le K.zero (CBLib.kern K (Fa K X) (Ga K X) 5 (2) (11)) := by
  have kv : CBLib.kern K (Fa K X) (Ga K X) 5 (2) (11) = (K.add (K.add (K.add (K.add (K.add K.zero (K.add (K.mul (co K X (8)) (co K X (16))) (K.neg (K.mul (co K X (17)) (co K X (7)))))) (K.add (K.mul (K.add (co K X (9)) (co K X (7))) (K.add (co K X (17)) (co K X (15)))) (K.neg (K.mul (K.add (co K X (18)) (co K X (16))) (K.add (co K X (8)) (co K X (6))))))) (K.add (K.mul (K.add (K.add (co K X (10))   -- (+879 chars; full line in the Zenodo source)
  have t0 : O.le K.zero (VGoodTail.tt K X (8) (17)) := VGoodTail.tail_nn K O X hg (8) 8
  have et0 : (VGoodTail.tt K X (8) (17)) = (K.add (K.mul (co K X (8)) (co K X (16))) (K.neg (K.mul (co K X (7)) (co K X (17))))) := rfl
  have t1 : O.le K.zero (VGoodTail.tt K X (9) (18)) := VGoodTail.tail_nn K O X hg (9) 8
  have et1 : (VGoodTail.tt K X (9) (18)) = (K.add (K.mul (co K X (9)) (co K X (17))) (K.neg (K.mul (co K X (8)) (co K X (18))))) := rfl
  have t2 : O.le K.zero (VGoodTail.tt K X (9) (16)) := VGoodTail.tail_nn K O X hg (9) 6
  have et2 : (VGoodTail.tt K X (9) (16)) = (K.add (K.mul (co K X (9)) (co K X (15))) (K.neg (K.mul (co K X (8)) (co K X (16))))) := rfl
  have t3 : O.le K.zero (VGoodTail.tt K X (7) (18)) := VGoodTail.tail_nn K O X hg (7) 10
  have et3 : (VGoodTail.tt K X (7) (18)) = (K.add (K.mul (co K X (7)) (co K X (17))) (K.neg (K.mul (co K X (6)) (co K X (18))))) := rfl
  have t4 : O.le K.zero (VGoodTail.tt K X (7) (16)) := VGoodTail.tail_nn K O X hg (7) 8
  have et4 : (VGoodTail.tt K X (7) (16)) = (K.add (K.mul (co K X (7)) (co K X (15))) (K.neg (K.mul (co K X (6)) (co K X (16))))) := rfl
  have t5 : O.le K.zero (VGoodTail.tt K X (10) (19)) := VGoodTail.tail_nn K O X hg (10) 8
  have et5 : (VGoodTail.tt K X (10) (19)) = (K.add (K.mul (co K X (10)) (co K X (18))) (K.neg (K.mul (co K X (9)) (co K X (19))))) := rfl
  have t6 : O.le K.zero (VGoodTail.tt K X (10) (17)) := VGoodTail.tail_nn K O X hg (10) 6
  have et6 : (VGoodTail.tt K X (10) (17)) = (K.add (K.mul (co K X (10)) (co K X (16))) (K.neg (K.mul (co K X (9)) (co K X (17))))) := rfl
  have t7 : O.le K.zero (VGoodTail.tt K X (10) (15)) := VGoodTail.tail_nn K O X hg (10) 4
  have et7 : (VGoodTail.tt K X (10) (15)) = (K.add (K.mul (co K X (10)) (co K X (14))) (K.neg (K.mul (co K X (9)) (co K X (15))))) := rfl
  have t8 : O.le K.zero (VGoodTail.tt K X (8) (19)) := VGoodTail.tail_nn K O X hg (8) 10
  have et8 : (VGoodTail.tt K X (8) (19)) = (K.add (K.mul (co K X (8)) (co K X (18))) (K.neg (K.mul (co K X (7)) (co K X (19))))) := rfl
  have t9 : O.le K.zero (VGoodTail.tt K X (8) (15)) := VGoodTail.tail_nn K O X hg (8) 6
  have et9 : (VGoodTail.tt K X (8) (15)) = (K.add (K.mul (co K X (8)) (co K X (14))) (K.neg (K.mul (co K X (7)) (co K X (15))))) := rfl
  have t10 : O.le K.zero (VGoodTail.tt K X (6) (19)) := VGoodTail.tail_nn K O X hg (6) 12
  have et10 : (VGoodTail.tt K X (6) (19)) = (K.add (K.mul (co K X (6)) (co K X (18))) (K.neg (K.mul (co K X (5)) (co K X (19))))) := rfl
  have t11 : O.le K.zero (VGoodTail.tt K X (6) (17)) := VGoodTail.tail_nn K O X hg (6) 10
  have et11 : (VGoodTail.tt K X (6) (17)) = (K.add (K.mul (co K X (6)) (co K X (16))) (K.neg (K.mul (co K X (5)) (co K X (17))))) := rfl
  have t12 : O.le K.zero (VGoodTail.tt K X (6) (15)) := VGoodTail.tail_nn K O X hg (6) 8
  have et12 : (VGoodTail.tt K X (6) (15)) = (K.add (K.mul (co K X (6)) (co K X (14))) (K.neg (K.mul (co K X (5)) (co K X (15))))) := rfl
  have t13 : O.le K.zero (VGoodTail.tt K X (11) (20)) := VGoodTail.tail_nn K O X hg (11) 8
  have et13 : (VGoodTail.tt K X (11) (20)) = (K.add (K.mul (co K X (11)) (co K X (19))) (K.neg (K.mul (co K X (10)) (co K X (20))))) := rfl
  have t14 : O.le K.zero (VGoodTail.tt K X (11) (18)) := VGoodTail.tail_nn K O X hg (11) 6
  have et14 : (VGoodTail.tt K X (11) (18)) = (K.add (K.mul (co K X (11)) (co K X (17))) (K.neg (K.mul (co K X (10)) (co K X (18))))) := rfl
  have t15 : O.le K.zero (VGoodTail.tt K X (11) (16)) := VGoodTail.tail_nn K O X hg (11) 4
  have et15 : (VGoodTail.tt K X (11) (16)) = (K.add (K.mul (co K X (11)) (co K X (15))) (K.neg (K.mul (co K X (10)) (co K X (16))))) := rfl
  have t16 : O.le K.zero (VGoodTail.tt K X (11) (14)) := VGoodTail.tail_nn K O X hg (11) 2
  have et16 : (VGoodTail.tt K X (11) (14)) = (K.add (K.mul (co K X (11)) (co K X (13))) (K.neg (K.mul (co K X (10)) (co K X (14))))) := rfl
  have t17 : O.le K.zero (VGoodTail.tt K X (9) (20)) := VGoodTail.tail_nn K O X hg (9) 10
  have et17 : (VGoodTail.tt K X (9) (20)) = (K.add (K.mul (co K X (9)) (co K X (19))) (K.neg (K.mul (co K X (8)) (co K X (20))))) := rfl
  have t18 : O.le K.zero (VGoodTail.tt K X (9) (14)) := VGoodTail.tail_nn K O X hg (9) 4
  have et18 : (VGoodTail.tt K X (9) (14)) = (K.add (K.mul (co K X (9)) (co K X (13))) (K.neg (K.mul (co K X (8)) (co K X (14))))) := rfl
  have t19 : O.le K.zero (VGoodTail.tt K X (7) (20)) := VGoodTail.tail_nn K O X hg (7) 12
  have et19 : (VGoodTail.tt K X (7) (20)) = (K.add (K.mul (co K X (7)) (co K X (19))) (K.neg (K.mul (co K X (6)) (co K X (20))))) := rfl
  have t20 : O.le K.zero (VGoodTail.tt K X (7) (14)) := VGoodTail.tail_nn K O X hg (7) 6
  have et20 : (VGoodTail.tt K X (7) (14)) = (K.add (K.mul (co K X (7)) (co K X (13))) (K.neg (K.mul (co K X (6)) (co K X (14))))) := rfl
  have t21 : O.le K.zero (VGoodTail.tt K X (12) (21)) := VGoodTail.tail_nn K O X hg (12) 8
  have et21 : (VGoodTail.tt K X (12) (21)) = (K.add (K.mul (co K X (12)) (co K X (20))) (K.neg (K.mul (co K X (11)) (co K X (21))))) := rfl
  have t22 : O.le K.zero (VGoodTail.tt K X (12) (19)) := VGoodTail.tail_nn K O X hg (12) 6
  have et22 : (VGoodTail.tt K X (12) (19)) = (K.add (K.mul (co K X (12)) (co K X (18))) (K.neg (K.mul (co K X (11)) (co K X (19))))) := rfl
  have t23 : O.le K.zero (VGoodTail.tt K X (12) (17)) := VGoodTail.tail_nn K O X hg (12) 4
  have et23 : (VGoodTail.tt K X (12) (17)) = (K.add (K.mul (co K X (12)) (co K X (16))) (K.neg (K.mul (co K X (11)) (co K X (17))))) := rfl
  have t24 : O.le K.zero (VGoodTail.tt K X (12) (15)) := VGoodTail.tail_nn K O X hg (12) 2
  have et24 : (VGoodTail.tt K X (12) (15)) = (K.add (K.mul (co K X (12)) (co K X (14))) (K.neg (K.mul (co K X (11)) (co K X (15))))) := rfl
  have t25 : O.le K.zero (VGoodTail.tt K X (12) (13)) := VGoodTail.tail_nn K O X hg (12) 0
  have et25 : (VGoodTail.tt K X (12) (13)) = (K.add (K.mul (co K X (12)) (co K X (12))) (K.neg (K.mul (co K X (11)) (co K X (13))))) := rfl
  have t26 : O.le K.zero (VGoodTail.tt K X (10) (21)) := VGoodTail.tail_nn K O X hg (10) 10
  have et26 : (VGoodTail.tt K X (10) (21)) = (K.add (K.mul (co K X (10)) (co K X (20))) (K.neg (K.mul (co K X (9)) (co K X (21))))) := rfl
  have t27 : O.le K.zero (VGoodTail.tt K X (10) (13)) := VGoodTail.tail_nn K O X hg (10) 2
  have et27 : (VGoodTail.tt K X (10) (13)) = (K.add (K.mul (co K X (10)) (co K X (12))) (K.neg (K.mul (co K X (9)) (co K X (13))))) := rfl
  have t28 : O.le K.zero (VGoodTail.tt K X (8) (21)) := VGoodTail.tail_nn K O X hg (8) 12
  have et28 : (VGoodTail.tt K X (8) (21)) = (K.add (K.mul (co K X (8)) (co K X (20))) (K.neg (K.mul (co K X (7)) (co K X (21))))) := rfl
  have t29 : O.le K.zero (VGoodTail.tt K X (8) (13)) := VGoodTail.tail_nn K O X hg (8) 4
  have et29 : (VGoodTail.tt K X (8) (13)) = (K.add (K.mul (co K X (8)) (co K X (12))) (K.neg (K.mul (co K X (7)) (co K X (13))))) := rfl
  have hp : 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.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 (VGoodTail.tt K X (8) (17)) (VGoodTail.tt K X (8) (17))) (VGoodTail.tt K X (8) (17))) (VGoodTail  -- (+1114 chars; full line in the Zenodo source)
    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 _ _ (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 _ _ (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   -- (+672 chars; full line in the Zenodo source)
  refine O.le_trans _ _ _ hp (TripleE.le_of_eq' K O _ _ ?_)
  rw [kv, et0, et1, et2, et3, et4, et5, et6, et7, et8, et9, et10, et11, et12, et13, et14, et15, et16, et17, et18, et19, et20, et21, et22, et23, et24, et25, et26, et27, et28, et29]
  ring_z

theorem kna_2_9 (X : List R) (hg : L1Full.Good K O X) :
    O.le K.zero (CBLib.kern K (Fa K X) (Ga K X) 5 (2) (12)) := by
  have kv : CBLib.kern K (Fa K X) (Ga K X) 5 (2) (12) = (K.add (K.add (K.add (K.add (K.add K.zero (K.add (K.mul (co K X (8)) (co K X (17))) (K.neg (K.mul (co K X (18)) (co K X (7)))))) (K.add (K.mul (K.add (co K X (9)) (co K X (7))) (K.add (co K X (18)) (co K X (16)))) (K.neg (K.mul (K.add (co K X (19)) (co K X (17))) (K.add (co K X (8)) (co K X (6))))))) (K.add (K.mul (K.add (K.add (co K X (10))   -- (+879 chars; full line in the Zenodo source)
  have t0 : O.le K.zero (VGoodTail.tt K X (8) (18)) := VGoodTail.tail_nn K O X hg (8) 9
  have et0 : (VGoodTail.tt K X (8) (18)) = (K.add (K.mul (co K X (8)) (co K X (17))) (K.neg (K.mul (co K X (7)) (co K X (18))))) := rfl
  have t1 : O.le K.zero (VGoodTail.tt K X (9) (19)) := VGoodTail.tail_nn K O X hg (9) 9
  have et1 : (VGoodTail.tt K X (9) (19)) = (K.add (K.mul (co K X (9)) (co K X (18))) (K.neg (K.mul (co K X (8)) (co K X (19))))) := rfl
  have t2 : O.le K.zero (VGoodTail.tt K X (9) (17)) := VGoodTail.tail_nn K O X hg (9) 7
  have et2 : (VGoodTail.tt K X (9) (17)) = (K.add (K.mul (co K X (9)) (co K X (16))) (K.neg (K.mul (co K X (8)) (co K X (17))))) := rfl
  have t3 : O.le K.zero (VGoodTail.tt K X (7) (19)) := VGoodTail.tail_nn K O X hg (7) 11
  have et3 : (VGoodTail.tt K X (7) (19)) = (K.add (K.mul (co K X (7)) (co K X (18))) (K.neg (K.mul (co K X (6)) (co K X (19))))) := rfl
  have t4 : O.le K.zero (VGoodTail.tt K X (7) (17)) := VGoodTail.tail_nn K O X hg (7) 9
  have et4 : (VGoodTail.tt K X (7) (17)) = (K.add (K.mul (co K X (7)) (co K X (16))) (K.neg (K.mul (co K X (6)) (co K X (17))))) := rfl
  have t5 : O.le K.zero (VGoodTail.tt K X (10) (20)) := VGoodTail.tail_nn K O X hg (10) 9
  have et5 : (VGoodTail.tt K X (10) (20)) = (K.add (K.mul (co K X (10)) (co K X (19))) (K.neg (K.mul (co K X (9)) (co K X (20))))) := rfl
  have t6 : O.le K.zero (VGoodTail.tt K X (10) (18)) := VGoodTail.tail_nn K O X hg (10) 7
  have et6 : (VGoodTail.tt K X (10) (18)) = (K.add (K.mul (co K X (10)) (co K X (17))) (K.neg (K.mul (co K X (9)) (co K X (18))))) := rfl
  have t7 : O.le K.zero (VGoodTail.tt K X (10) (16)) := VGoodTail.tail_nn K O X hg (10) 5
  have et7 : (VGoodTail.tt K X (10) (16)) = (K.add (K.mul (co K X (10)) (co K X (15))) (K.neg (K.mul (co K X (9)) (co K X (16))))) := rfl
  have t8 : O.le K.zero (VGoodTail.tt K X (8) (20)) := VGoodTail.tail_nn K O X hg (8) 11
  have et8 : (VGoodTail.tt K X (8) (20)) = (K.add (K.mul (co K X (8)) (co K X (19))) (K.neg (K.mul (co K X (7)) (co K X (20))))) := rfl
  have t9 : O.le K.zero (VGoodTail.tt K X (8) (16)) := VGoodTail.tail_nn K O X hg (8) 7
  have et9 : (VGoodTail.tt K X (8) (16)) = (K.add (K.mul (co K X (8)) (co K X (15))) (K.neg (K.mul (co K X (7)) (co K X (16))))) := rfl
  have t10 : O.le K.zero (VGoodTail.tt K X (6) (20)) := VGoodTail.tail_nn K O X hg (6) 13
  have et10 : (VGoodTail.tt K X (6) (20)) = (K.add (K.mul (co K X (6)) (co K X (19))) (K.neg (K.mul (co K X (5)) (co K X (20))))) := rfl
  have t11 : O.le K.zero (VGoodTail.tt K X (6) (18)) := VGoodTail.tail_nn K O X hg (6) 11
  have et11 : (VGoodTail.tt K X (6) (18)) = (K.add (K.mul (co K X (6)) (co K X (17))) (K.neg (K.mul (co K X (5)) (co K X (18))))) := rfl
  have t12 : O.le K.zero (VGoodTail.tt K X (6) (16)) := VGoodTail.tail_nn K O X hg (6) 9
  have et12 : (VGoodTail.tt K X (6) (16)) = (K.add (K.mul (co K X (6)) (co K X (15))) (K.neg (K.mul (co K X (5)) (co K X (16))))) := rfl
  have t13 : O.le K.zero (VGoodTail.tt K X (11) (21)) := VGoodTail.tail_nn K O X hg (11) 9
  have et13 : (VGoodTail.tt K X (11) (21)) = (K.add (K.mul (co K X (11)) (co K X (20))) (K.neg (K.mul (co K X (10)) (co K X (21))))) := rfl
  have t14 : O.le K.zero (VGoodTail.tt K X (11) (19)) := VGoodTail.tail_nn K O X hg (11) 7
  have et14 : (VGoodTail.tt K X (11) (19)) = (K.add (K.mul (co K X (11)) (co K X (18))) (K.neg (K.mul (co K X (10)) (co K X (19))))) := rfl
  have t15 : O.le K.zero (VGoodTail.tt K X (11) (17)) := VGoodTail.tail_nn K O X hg (11) 5
  have et15 : (VGoodTail.tt K X (11) (17)) = (K.add (K.mul (co K X (11)) (co K X (16))) (K.neg (K.mul (co K X (10)) (co K X (17))))) := rfl
  have t16 : O.le K.zero (VGoodTail.tt K X (11) (15)) := VGoodTail.tail_nn K O X hg (11) 3
  have et16 : (VGoodTail.tt K X (11) (15)) = (K.add (K.mul (co K X (11)) (co K X (14))) (K.neg (K.mul (co K X (10)) (co K X (15))))) := rfl
  have t17 : O.le K.zero (VGoodTail.tt K X (9) (21)) := VGoodTail.tail_nn K O X hg (9) 11
  have et17 : (VGoodTail.tt K X (9) (21)) = (K.add (K.mul (co K X (9)) (co K X (20))) (K.neg (K.mul (co K X (8)) (co K X (21))))) := rfl
  have t18 : O.le K.zero (VGoodTail.tt K X (9) (15)) := VGoodTail.tail_nn K O X hg (9) 5
  have et18 : (VGoodTail.tt K X (9) (15)) = (K.add (K.mul (co K X (9)) (co K X (14))) (K.neg (K.mul (co K X (8)) (co K X (15))))) := rfl
  have t19 : O.le K.zero (VGoodTail.tt K X (7) (21)) := VGoodTail.tail_nn K O X hg (7) 13
  have et19 : (VGoodTail.tt K X (7) (21)) = (K.add (K.mul (co K X (7)) (co K X (20))) (K.neg (K.mul (co K X (6)) (co K X (21))))) := rfl
  have t20 : O.le K.zero (VGoodTail.tt K X (7) (15)) := VGoodTail.tail_nn K O X hg (7) 7
  have et20 : (VGoodTail.tt K X (7) (15)) = (K.add (K.mul (co K X (7)) (co K X (14))) (K.neg (K.mul (co K X (6)) (co K X (15))))) := rfl
  have t21 : O.le K.zero (VGoodTail.tt K X (12) (22)) := VGoodTail.tail_nn K O X hg (12) 9
  have et21 : (VGoodTail.tt K X (12) (22)) = (K.add (K.mul (co K X (12)) (co K X (21))) (K.neg (K.mul (co K X (11)) (co K X (22))))) := rfl
  have t22 : O.le K.zero (VGoodTail.tt K X (12) (20)) := VGoodTail.tail_nn K O X hg (12) 7
  have et22 : (VGoodTail.tt K X (12) (20)) = (K.add (K.mul (co K X (12)) (co K X (19))) (K.neg (K.mul (co K X (11)) (co K X (20))))) := rfl
  have t23 : O.le K.zero (VGoodTail.tt K X (12) (18)) := VGoodTail.tail_nn K O X hg (12) 5
  have et23 : (VGoodTail.tt K X (12) (18)) = (K.add (K.mul (co K X (12)) (co K X (17))) (K.neg (K.mul (co K X (11)) (co K X (18))))) := rfl
  have t24 : O.le K.zero (VGoodTail.tt K X (12) (16)) := VGoodTail.tail_nn K O X hg (12) 3
  have et24 : (VGoodTail.tt K X (12) (16)) = (K.add (K.mul (co K X (12)) (co K X (15))) (K.neg (K.mul (co K X (11)) (co K X (16))))) := rfl
  have t25 : O.le K.zero (VGoodTail.tt K X (12) (14)) := VGoodTail.tail_nn K O X hg (12) 1
  have et25 : (VGoodTail.tt K X (12) (14)) = (K.add (K.mul (co K X (12)) (co K X (13))) (K.neg (K.mul (co K X (11)) (co K X (14))))) := rfl
  have t26 : O.le K.zero (VGoodTail.tt K X (10) (22)) := VGoodTail.tail_nn K O X hg (10) 11
  have et26 : (VGoodTail.tt K X (10) (22)) = (K.add (K.mul (co K X (10)) (co K X (21))) (K.neg (K.mul (co K X (9)) (co K X (22))))) := rfl
  have t27 : O.le K.zero (VGoodTail.tt K X (10) (14)) := VGoodTail.tail_nn K O X hg (10) 3
  have et27 : (VGoodTail.tt K X (10) (14)) = (K.add (K.mul (co K X (10)) (co K X (13))) (K.neg (K.mul (co K X (9)) (co K X (14))))) := rfl
  have t28 : O.le K.zero (VGoodTail.tt K X (8) (22)) := VGoodTail.tail_nn K O X hg (8) 13
  have et28 : (VGoodTail.tt K X (8) (22)) = (K.add (K.mul (co K X (8)) (co K X (21))) (K.neg (K.mul (co K X (7)) (co K X (22))))) := rfl
  have t29 : O.le K.zero (VGoodTail.tt K X (8) (14)) := VGoodTail.tail_nn K O X hg (8) 5
  have et29 : (VGoodTail.tt K X (8) (14)) = (K.add (K.mul (co K X (8)) (co K X (13))) (K.neg (K.mul (co K X (7)) (co K X (14))))) := rfl
  have hp : 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.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 (VGoodTail.tt K X (8) (18)) (VGoodTail.tt K X (8) (18))) (VGoodTail.tt K X (8) (18))) (VGoodTail  -- (+1114 chars; full line in the Zenodo source)
    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 _ _ (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 _ _ (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   -- (+672 chars; full line in the Zenodo source)
  refine O.le_trans _ _ _ hp (TripleE.le_of_eq' K O _ _ ?_)
  rw [kv, et0, et1, et2, et3, et4, et5, et6, et7, et8, et9, et10, et11, et12, et13, et14, et15, et16, et17, et18, et19, et20, et21, et22, et23, et24, et25, et26, et27, et28, et29]
  ring_z

theorem kna_2_10 (X : List R) (hg : L1Full.Good K O X) :
    O.le K.zero (CBLib.kern K (Fa K X) (Ga K X) 5 (2) (13)) := by
  have kv : CBLib.kern K (Fa K X) (Ga K X) 5 (2) (13) = (K.add (K.add (K.add (K.add (K.add K.zero (K.add (K.mul (co K X (8)) (co K X (18))) (K.neg (K.mul (co K X (19)) (co K X (7)))))) (K.add (K.mul (K.add (co K X (9)) (co K X (7))) (K.add (co K X (19)) (co K X (17)))) (K.neg (K.mul (K.add (co K X (20)) (co K X (18))) (K.add (co K X (8)) (co K X (6))))))) (K.add (K.mul (K.add (K.add (co K X (10))   -- (+879 chars; full line in the Zenodo source)
  have t0 : O.le K.zero (VGoodTail.tt K X (8) (19)) := VGoodTail.tail_nn K O X hg (8) 10
  have et0 : (VGoodTail.tt K X (8) (19)) = (K.add (K.mul (co K X (8)) (co K X (18))) (K.neg (K.mul (co K X (7)) (co K X (19))))) := rfl
  have t1 : O.le K.zero (VGoodTail.tt K X (9) (20)) := VGoodTail.tail_nn K O X hg (9) 10
  have et1 : (VGoodTail.tt K X (9) (20)) = (K.add (K.mul (co K X (9)) (co K X (19))) (K.neg (K.mul (co K X (8)) (co K X (20))))) := rfl
  have t2 : O.le K.zero (VGoodTail.tt K X (9) (18)) := VGoodTail.tail_nn K O X hg (9) 8
  have et2 : (VGoodTail.tt K X (9) (18)) = (K.add (K.mul (co K X (9)) (co K X (17))) (K.neg (K.mul (co K X (8)) (co K X (18))))) := rfl
  have t3 : O.le K.zero (VGoodTail.tt K X (7) (20)) := VGoodTail.tail_nn K O X hg (7) 12
  have et3 : (VGoodTail.tt K X (7) (20)) = (K.add (K.mul (co K X (7)) (co K X (19))) (K.neg (K.mul (co K X (6)) (co K X (20))))) := rfl
  have t4 : O.le K.zero (VGoodTail.tt K X (7) (18)) := VGoodTail.tail_nn K O X hg (7) 10
  have et4 : (VGoodTail.tt K X (7) (18)) = (K.add (K.mul (co K X (7)) (co K X (17))) (K.neg (K.mul (co K X (6)) (co K X (18))))) := rfl
  have t5 : O.le K.zero (VGoodTail.tt K X (10) (21)) := VGoodTail.tail_nn K O X hg (10) 10
  have et5 : (VGoodTail.tt K X (10) (21)) = (K.add (K.mul (co K X (10)) (co K X (20))) (K.neg (K.mul (co K X (9)) (co K X (21))))) := rfl
  have t6 : O.le K.zero (VGoodTail.tt K X (10) (19)) := VGoodTail.tail_nn K O X hg (10) 8
  have et6 : (VGoodTail.tt K X (10) (19)) = (K.add (K.mul (co K X (10)) (co K X (18))) (K.neg (K.mul (co K X (9)) (co K X (19))))) := rfl
  have t7 : O.le K.zero (VGoodTail.tt K X (10) (17)) := VGoodTail.tail_nn K O X hg (10) 6
  have et7 : (VGoodTail.tt K X (10) (17)) = (K.add (K.mul (co K X (10)) (co K X (16))) (K.neg (K.mul (co K X (9)) (co K X (17))))) := rfl
  have t8 : O.le K.zero (VGoodTail.tt K X (8) (21)) := VGoodTail.tail_nn K O X hg (8) 12
  have et8 : (VGoodTail.tt K X (8) (21)) = (K.add (K.mul (co K X (8)) (co K X (20))) (K.neg (K.mul (co K X (7)) (co K X (21))))) := rfl
  have t9 : O.le K.zero (VGoodTail.tt K X (8) (17)) := VGoodTail.tail_nn K O X hg (8) 8
  have et9 : (VGoodTail.tt K X (8) (17)) = (K.add (K.mul (co K X (8)) (co K X (16))) (K.neg (K.mul (co K X (7)) (co K X (17))))) := rfl
  have t10 : O.le K.zero (VGoodTail.tt K X (6) (21)) := VGoodTail.tail_nn K O X hg (6) 14
  have et10 : (VGoodTail.tt K X (6) (21)) = (K.add (K.mul (co K X (6)) (co K X (20))) (K.neg (K.mul (co K X (5)) (co K X (21))))) := rfl
  have t11 : O.le K.zero (VGoodTail.tt K X (6) (19)) := VGoodTail.tail_nn K O X hg (6) 12
  have et11 : (VGoodTail.tt K X (6) (19)) = (K.add (K.mul (co K X (6)) (co K X (18))) (K.neg (K.mul (co K X (5)) (co K X (19))))) := rfl
  have t12 : O.le K.zero (VGoodTail.tt K X (6) (17)) := VGoodTail.tail_nn K O X hg (6) 10
  have et12 : (VGoodTail.tt K X (6) (17)) = (K.add (K.mul (co K X (6)) (co K X (16))) (K.neg (K.mul (co K X (5)) (co K X (17))))) := rfl
  have t13 : O.le K.zero (VGoodTail.tt K X (11) (22)) := VGoodTail.tail_nn K O X hg (11) 10
  have et13 : (VGoodTail.tt K X (11) (22)) = (K.add (K.mul (co K X (11)) (co K X (21))) (K.neg (K.mul (co K X (10)) (co K X (22))))) := rfl
  have t14 : O.le K.zero (VGoodTail.tt K X (11) (20)) := VGoodTail.tail_nn K O X hg (11) 8
  have et14 : (VGoodTail.tt K X (11) (20)) = (K.add (K.mul (co K X (11)) (co K X (19))) (K.neg (K.mul (co K X (10)) (co K X (20))))) := rfl
  have t15 : O.le K.zero (VGoodTail.tt K X (11) (18)) := VGoodTail.tail_nn K O X hg (11) 6
  have et15 : (VGoodTail.tt K X (11) (18)) = (K.add (K.mul (co K X (11)) (co K X (17))) (K.neg (K.mul (co K X (10)) (co K X (18))))) := rfl
  have t16 : O.le K.zero (VGoodTail.tt K X (11) (16)) := VGoodTail.tail_nn K O X hg (11) 4
  have et16 : (VGoodTail.tt K X (11) (16)) = (K.add (K.mul (co K X (11)) (co K X (15))) (K.neg (K.mul (co K X (10)) (co K X (16))))) := rfl
  have t17 : O.le K.zero (VGoodTail.tt K X (9) (22)) := VGoodTail.tail_nn K O X hg (9) 12
  have et17 : (VGoodTail.tt K X (9) (22)) = (K.add (K.mul (co K X (9)) (co K X (21))) (K.neg (K.mul (co K X (8)) (co K X (22))))) := rfl
  have t18 : O.le K.zero (VGoodTail.tt K X (9) (16)) := VGoodTail.tail_nn K O X hg (9) 6
  have et18 : (VGoodTail.tt K X (9) (16)) = (K.add (K.mul (co K X (9)) (co K X (15))) (K.neg (K.mul (co K X (8)) (co K X (16))))) := rfl
  have t19 : O.le K.zero (VGoodTail.tt K X (7) (22)) := VGoodTail.tail_nn K O X hg (7) 14
  have et19 : (VGoodTail.tt K X (7) (22)) = (K.add (K.mul (co K X (7)) (co K X (21))) (K.neg (K.mul (co K X (6)) (co K X (22))))) := rfl
  have t20 : O.le K.zero (VGoodTail.tt K X (7) (16)) := VGoodTail.tail_nn K O X hg (7) 8
  have et20 : (VGoodTail.tt K X (7) (16)) = (K.add (K.mul (co K X (7)) (co K X (15))) (K.neg (K.mul (co K X (6)) (co K X (16))))) := rfl
  have t21 : O.le K.zero (VGoodTail.tt K X (12) (23)) := VGoodTail.tail_nn K O X hg (12) 10
  have et21 : (VGoodTail.tt K X (12) (23)) = (K.add (K.mul (co K X (12)) (co K X (22))) (K.neg (K.mul (co K X (11)) (co K X (23))))) := rfl
  have t22 : O.le K.zero (VGoodTail.tt K X (12) (21)) := VGoodTail.tail_nn K O X hg (12) 8
  have et22 : (VGoodTail.tt K X (12) (21)) = (K.add (K.mul (co K X (12)) (co K X (20))) (K.neg (K.mul (co K X (11)) (co K X (21))))) := rfl
  have t23 : O.le K.zero (VGoodTail.tt K X (12) (19)) := VGoodTail.tail_nn K O X hg (12) 6
  have et23 : (VGoodTail.tt K X (12) (19)) = (K.add (K.mul (co K X (12)) (co K X (18))) (K.neg (K.mul (co K X (11)) (co K X (19))))) := rfl
  have t24 : O.le K.zero (VGoodTail.tt K X (12) (17)) := VGoodTail.tail_nn K O X hg (12) 4
  have et24 : (VGoodTail.tt K X (12) (17)) = (K.add (K.mul (co K X (12)) (co K X (16))) (K.neg (K.mul (co K X (11)) (co K X (17))))) := rfl
  have t25 : O.le K.zero (VGoodTail.tt K X (12) (15)) := VGoodTail.tail_nn K O X hg (12) 2
  have et25 : (VGoodTail.tt K X (12) (15)) = (K.add (K.mul (co K X (12)) (co K X (14))) (K.neg (K.mul (co K X (11)) (co K X (15))))) := rfl
  have t26 : O.le K.zero (VGoodTail.tt K X (10) (23)) := VGoodTail.tail_nn K O X hg (10) 12
  have et26 : (VGoodTail.tt K X (10) (23)) = (K.add (K.mul (co K X (10)) (co K X (22))) (K.neg (K.mul (co K X (9)) (co K X (23))))) := rfl
  have t27 : O.le K.zero (VGoodTail.tt K X (10) (15)) := VGoodTail.tail_nn K O X hg (10) 4
  have et27 : (VGoodTail.tt K X (10) (15)) = (K.add (K.mul (co K X (10)) (co K X (14))) (K.neg (K.mul (co K X (9)) (co K X (15))))) := rfl
  have t28 : O.le K.zero (VGoodTail.tt K X (8) (23)) := VGoodTail.tail_nn K O X hg (8) 14
  have et28 : (VGoodTail.tt K X (8) (23)) = (K.add (K.mul (co K X (8)) (co K X (22))) (K.neg (K.mul (co K X (7)) (co K X (23))))) := rfl
  have t29 : O.le K.zero (VGoodTail.tt K X (8) (15)) := VGoodTail.tail_nn K O X hg (8) 6
  have et29 : (VGoodTail.tt K X (8) (15)) = (K.add (K.mul (co K X (8)) (co K X (14))) (K.neg (K.mul (co K X (7)) (co K X (15))))) := rfl
  have hp : 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.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 (VGoodTail.tt K X (8) (19)) (VGoodTail.tt K X (8) (19))) (VGoodTail.tt K X (8) (19))) (VGoodTail  -- (+1114 chars; full line in the Zenodo source)
    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 _ _ (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 _ _ (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   -- (+672 chars; full line in the Zenodo source)
  refine O.le_trans _ _ _ hp (TripleE.le_of_eq' K O _ _ ?_)
  rw [kv, et0, et1, et2, et3, et4, et5, et6, et7, et8, et9, et10, et11, et12, et13, et14, et15, et16, et17, et18, et19, et20, et21, et22, et23, et24, et25, et26, et27, et28, et29]
  ring_z

theorem kna_2_11 (X : List R) (hg : L1Full.Good K O X) :
    O.le K.zero (CBLib.kern K (Fa K X) (Ga K X) 5 (2) (14)) := by
  have kv : CBLib.kern K (Fa K X) (Ga K X) 5 (2) (14) = (K.add (K.add (K.add (K.add (K.add K.zero (K.add (K.mul (co K X (8)) (co K X (19))) (K.neg (K.mul (co K X (20)) (co K X (7)))))) (K.add (K.mul (K.add (co K X (9)) (co K X (7))) (K.add (co K X (20)) (co K X (18)))) (K.neg (K.mul (K.add (co K X (21)) (co K X (19))) (K.add (co K X (8)) (co K X (6))))))) (K.add (K.mul (K.add (K.add (co K X (10))   -- (+879 chars; full line in the Zenodo source)
  have t0 : O.le K.zero (VGoodTail.tt K X (8) (20)) := VGoodTail.tail_nn K O X hg (8) 11
  have et0 : (VGoodTail.tt K X (8) (20)) = (K.add (K.mul (co K X (8)) (co K X (19))) (K.neg (K.mul (co K X (7)) (co K X (20))))) := rfl
  have t1 : O.le K.zero (VGoodTail.tt K X (9) (21)) := VGoodTail.tail_nn K O X hg (9) 11
  have et1 : (VGoodTail.tt K X (9) (21)) = (K.add (K.mul (co K X (9)) (co K X (20))) (K.neg (K.mul (co K X (8)) (co K X (21))))) := rfl
  have t2 : O.le K.zero (VGoodTail.tt K X (9) (19)) := VGoodTail.tail_nn K O X hg (9) 9
  have et2 : (VGoodTail.tt K X (9) (19)) = (K.add (K.mul (co K X (9)) (co K X (18))) (K.neg (K.mul (co K X (8)) (co K X (19))))) := rfl
  have t3 : O.le K.zero (VGoodTail.tt K X (7) (21)) := VGoodTail.tail_nn K O X hg (7) 13
  have et3 : (VGoodTail.tt K X (7) (21)) = (K.add (K.mul (co K X (7)) (co K X (20))) (K.neg (K.mul (co K X (6)) (co K X (21))))) := rfl
  have t4 : O.le K.zero (VGoodTail.tt K X (7) (19)) := VGoodTail.tail_nn K O X hg (7) 11
  have et4 : (VGoodTail.tt K X (7) (19)) = (K.add (K.mul (co K X (7)) (co K X (18))) (K.neg (K.mul (co K X (6)) (co K X (19))))) := rfl
  have t5 : O.le K.zero (VGoodTail.tt K X (10) (22)) := VGoodTail.tail_nn K O X hg (10) 11
  have et5 : (VGoodTail.tt K X (10) (22)) = (K.add (K.mul (co K X (10)) (co K X (21))) (K.neg (K.mul (co K X (9)) (co K X (22))))) := rfl
  have t6 : O.le K.zero (VGoodTail.tt K X (10) (20)) := VGoodTail.tail_nn K O X hg (10) 9
  have et6 : (VGoodTail.tt K X (10) (20)) = (K.add (K.mul (co K X (10)) (co K X (19))) (K.neg (K.mul (co K X (9)) (co K X (20))))) := rfl
  have t7 : O.le K.zero (VGoodTail.tt K X (10) (18)) := VGoodTail.tail_nn K O X hg (10) 7
  have et7 : (VGoodTail.tt K X (10) (18)) = (K.add (K.mul (co K X (10)) (co K X (17))) (K.neg (K.mul (co K X (9)) (co K X (18))))) := rfl
  have t8 : O.le K.zero (VGoodTail.tt K X (8) (22)) := VGoodTail.tail_nn K O X hg (8) 13
  have et8 : (VGoodTail.tt K X (8) (22)) = (K.add (K.mul (co K X (8)) (co K X (21))) (K.neg (K.mul (co K X (7)) (co K X (22))))) := rfl
  have t9 : O.le K.zero (VGoodTail.tt K X (8) (18)) := VGoodTail.tail_nn K O X hg (8) 9
  have et9 : (VGoodTail.tt K X (8) (18)) = (K.add (K.mul (co K X (8)) (co K X (17))) (K.neg (K.mul (co K X (7)) (co K X (18))))) := rfl
  have t10 : O.le K.zero (VGoodTail.tt K X (6) (22)) := VGoodTail.tail_nn K O X hg (6) 15
  have et10 : (VGoodTail.tt K X (6) (22)) = (K.add (K.mul (co K X (6)) (co K X (21))) (K.neg (K.mul (co K X (5)) (co K X (22))))) := rfl
  have t11 : O.le K.zero (VGoodTail.tt K X (6) (20)) := VGoodTail.tail_nn K O X hg (6) 13
  have et11 : (VGoodTail.tt K X (6) (20)) = (K.add (K.mul (co K X (6)) (co K X (19))) (K.neg (K.mul (co K X (5)) (co K X (20))))) := rfl
  have t12 : O.le K.zero (VGoodTail.tt K X (6) (18)) := VGoodTail.tail_nn K O X hg (6) 11
  have et12 : (VGoodTail.tt K X (6) (18)) = (K.add (K.mul (co K X (6)) (co K X (17))) (K.neg (K.mul (co K X (5)) (co K X (18))))) := rfl
  have t13 : O.le K.zero (VGoodTail.tt K X (11) (23)) := VGoodTail.tail_nn K O X hg (11) 11
  have et13 : (VGoodTail.tt K X (11) (23)) = (K.add (K.mul (co K X (11)) (co K X (22))) (K.neg (K.mul (co K X (10)) (co K X (23))))) := rfl
  have t14 : O.le K.zero (VGoodTail.tt K X (11) (21)) := VGoodTail.tail_nn K O X hg (11) 9
  have et14 : (VGoodTail.tt K X (11) (21)) = (K.add (K.mul (co K X (11)) (co K X (20))) (K.neg (K.mul (co K X (10)) (co K X (21))))) := rfl
  have t15 : O.le K.zero (VGoodTail.tt K X (11) (19)) := VGoodTail.tail_nn K O X hg (11) 7
  have et15 : (VGoodTail.tt K X (11) (19)) = (K.add (K.mul (co K X (11)) (co K X (18))) (K.neg (K.mul (co K X (10)) (co K X (19))))) := rfl
  have t16 : O.le K.zero (VGoodTail.tt K X (11) (17)) := VGoodTail.tail_nn K O X hg (11) 5
  have et16 : (VGoodTail.tt K X (11) (17)) = (K.add (K.mul (co K X (11)) (co K X (16))) (K.neg (K.mul (co K X (10)) (co K X (17))))) := rfl
  have t17 : O.le K.zero (VGoodTail.tt K X (9) (23)) := VGoodTail.tail_nn K O X hg (9) 13
  have et17 : (VGoodTail.tt K X (9) (23)) = (K.add (K.mul (co K X (9)) (co K X (22))) (K.neg (K.mul (co K X (8)) (co K X (23))))) := rfl
  have t18 : O.le K.zero (VGoodTail.tt K X (9) (17)) := VGoodTail.tail_nn K O X hg (9) 7
  have et18 : (VGoodTail.tt K X (9) (17)) = (K.add (K.mul (co K X (9)) (co K X (16))) (K.neg (K.mul (co K X (8)) (co K X (17))))) := rfl
  have t19 : O.le K.zero (VGoodTail.tt K X (7) (23)) := VGoodTail.tail_nn K O X hg (7) 15
  have et19 : (VGoodTail.tt K X (7) (23)) = (K.add (K.mul (co K X (7)) (co K X (22))) (K.neg (K.mul (co K X (6)) (co K X (23))))) := rfl
  have t20 : O.le K.zero (VGoodTail.tt K X (7) (17)) := VGoodTail.tail_nn K O X hg (7) 9
  have et20 : (VGoodTail.tt K X (7) (17)) = (K.add (K.mul (co K X (7)) (co K X (16))) (K.neg (K.mul (co K X (6)) (co K X (17))))) := rfl
  have t21 : O.le K.zero (VGoodTail.tt K X (12) (24)) := VGoodTail.tail_nn K O X hg (12) 11
  have et21 : (VGoodTail.tt K X (12) (24)) = (K.add (K.mul (co K X (12)) (co K X (23))) (K.neg (K.mul (co K X (11)) (co K X (24))))) := rfl
  have t22 : O.le K.zero (VGoodTail.tt K X (12) (22)) := VGoodTail.tail_nn K O X hg (12) 9
  have et22 : (VGoodTail.tt K X (12) (22)) = (K.add (K.mul (co K X (12)) (co K X (21))) (K.neg (K.mul (co K X (11)) (co K X (22))))) := rfl
  have t23 : O.le K.zero (VGoodTail.tt K X (12) (20)) := VGoodTail.tail_nn K O X hg (12) 7
  have et23 : (VGoodTail.tt K X (12) (20)) = (K.add (K.mul (co K X (12)) (co K X (19))) (K.neg (K.mul (co K X (11)) (co K X (20))))) := rfl
  have t24 : O.le K.zero (VGoodTail.tt K X (12) (18)) := VGoodTail.tail_nn K O X hg (12) 5
  have et24 : (VGoodTail.tt K X (12) (18)) = (K.add (K.mul (co K X (12)) (co K X (17))) (K.neg (K.mul (co K X (11)) (co K X (18))))) := rfl
  have t25 : O.le K.zero (VGoodTail.tt K X (12) (16)) := VGoodTail.tail_nn K O X hg (12) 3
  have et25 : (VGoodTail.tt K X (12) (16)) = (K.add (K.mul (co K X (12)) (co K X (15))) (K.neg (K.mul (co K X (11)) (co K X (16))))) := rfl
  have t26 : O.le K.zero (VGoodTail.tt K X (10) (24)) := VGoodTail.tail_nn K O X hg (10) 13
  have et26 : (VGoodTail.tt K X (10) (24)) = (K.add (K.mul (co K X (10)) (co K X (23))) (K.neg (K.mul (co K X (9)) (co K X (24))))) := rfl
  have t27 : O.le K.zero (VGoodTail.tt K X (10) (16)) := VGoodTail.tail_nn K O X hg (10) 5
  have et27 : (VGoodTail.tt K X (10) (16)) = (K.add (K.mul (co K X (10)) (co K X (15))) (K.neg (K.mul (co K X (9)) (co K X (16))))) := rfl
  have t28 : O.le K.zero (VGoodTail.tt K X (8) (24)) := VGoodTail.tail_nn K O X hg (8) 15
  have et28 : (VGoodTail.tt K X (8) (24)) = (K.add (K.mul (co K X (8)) (co K X (23))) (K.neg (K.mul (co K X (7)) (co K X (24))))) := rfl
  have t29 : O.le K.zero (VGoodTail.tt K X (8) (16)) := VGoodTail.tail_nn K O X hg (8) 7
  have et29 : (VGoodTail.tt K X (8) (16)) = (K.add (K.mul (co K X (8)) (co K X (15))) (K.neg (K.mul (co K X (7)) (co K X (16))))) := rfl
  have hp : 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.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 (VGoodTail.tt K X (8) (20)) (VGoodTail.tt K X (8) (20))) (VGoodTail.tt K X (8) (20))) (VGoodTail  -- (+1114 chars; full line in the Zenodo source)
    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 _ _ (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 _ _ (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   -- (+672 chars; full line in the Zenodo source)
  refine O.le_trans _ _ _ hp (TripleE.le_of_eq' K O _ _ ?_)
  rw [kv, et0, et1, et2, et3, et4, et5, et6, et7, et8, et9, et10, et11, et12, et13, et14, et15, et16, et17, et18, et19, et20, et21, et22, et23, et24, et25, et26, et27, et28, et29]
  ring_z

theorem kna_2_12 (X : List R) (hg : L1Full.Good K O X) :
    O.le K.zero (CBLib.kern K (Fa K X) (Ga K X) 5 (2) (15)) := by
  have kv : CBLib.kern K (Fa K X) (Ga K X) 5 (2) (15) = (K.add (K.add (K.add (K.add (K.add K.zero (K.add (K.mul (co K X (8)) (co K X (20))) (K.neg (K.mul (co K X (21)) (co K X (7)))))) (K.add (K.mul (K.add (co K X (9)) (co K X (7))) (K.add (co K X (21)) (co K X (19)))) (K.neg (K.mul (K.add (co K X (22)) (co K X (20))) (K.add (co K X (8)) (co K X (6))))))) (K.add (K.mul (K.add (K.add (co K X (10))   -- (+879 chars; full line in the Zenodo source)
  have t0 : O.le K.zero (VGoodTail.tt K X (8) (21)) := VGoodTail.tail_nn K O X hg (8) 12
  have et0 : (VGoodTail.tt K X (8) (21)) = (K.add (K.mul (co K X (8)) (co K X (20))) (K.neg (K.mul (co K X (7)) (co K X (21))))) := rfl
  have t1 : O.le K.zero (VGoodTail.tt K X (9) (22)) := VGoodTail.tail_nn K O X hg (9) 12
  have et1 : (VGoodTail.tt K X (9) (22)) = (K.add (K.mul (co K X (9)) (co K X (21))) (K.neg (K.mul (co K X (8)) (co K X (22))))) := rfl
  have t2 : O.le K.zero (VGoodTail.tt K X (9) (20)) := VGoodTail.tail_nn K O X hg (9) 10
  have et2 : (VGoodTail.tt K X (9) (20)) = (K.add (K.mul (co K X (9)) (co K X (19))) (K.neg (K.mul (co K X (8)) (co K X (20))))) := rfl
  have t3 : O.le K.zero (VGoodTail.tt K X (7) (22)) := VGoodTail.tail_nn K O X hg (7) 14
  have et3 : (VGoodTail.tt K X (7) (22)) = (K.add (K.mul (co K X (7)) (co K X (21))) (K.neg (K.mul (co K X (6)) (co K X (22))))) := rfl
  have t4 : O.le K.zero (VGoodTail.tt K X (7) (20)) := VGoodTail.tail_nn K O X hg (7) 12
  have et4 : (VGoodTail.tt K X (7) (20)) = (K.add (K.mul (co K X (7)) (co K X (19))) (K.neg (K.mul (co K X (6)) (co K X (20))))) := rfl
  have t5 : O.le K.zero (VGoodTail.tt K X (10) (23)) := VGoodTail.tail_nn K O X hg (10) 12
  have et5 : (VGoodTail.tt K X (10) (23)) = (K.add (K.mul (co K X (10)) (co K X (22))) (K.neg (K.mul (co K X (9)) (co K X (23))))) := rfl
  have t6 : O.le K.zero (VGoodTail.tt K X (10) (21)) := VGoodTail.tail_nn K O X hg (10) 10
  have et6 : (VGoodTail.tt K X (10) (21)) = (K.add (K.mul (co K X (10)) (co K X (20))) (K.neg (K.mul (co K X (9)) (co K X (21))))) := rfl
  have t7 : O.le K.zero (VGoodTail.tt K X (10) (19)) := VGoodTail.tail_nn K O X hg (10) 8
  have et7 : (VGoodTail.tt K X (10) (19)) = (K.add (K.mul (co K X (10)) (co K X (18))) (K.neg (K.mul (co K X (9)) (co K X (19))))) := rfl
  have t8 : O.le K.zero (VGoodTail.tt K X (8) (23)) := VGoodTail.tail_nn K O X hg (8) 14
  have et8 : (VGoodTail.tt K X (8) (23)) = (K.add (K.mul (co K X (8)) (co K X (22))) (K.neg (K.mul (co K X (7)) (co K X (23))))) := rfl
  have t9 : O.le K.zero (VGoodTail.tt K X (8) (19)) := VGoodTail.tail_nn K O X hg (8) 10
  have et9 : (VGoodTail.tt K X (8) (19)) = (K.add (K.mul (co K X (8)) (co K X (18))) (K.neg (K.mul (co K X (7)) (co K X (19))))) := rfl
  have t10 : O.le K.zero (VGoodTail.tt K X (6) (23)) := VGoodTail.tail_nn K O X hg (6) 16
  have et10 : (VGoodTail.tt K X (6) (23)) = (K.add (K.mul (co K X (6)) (co K X (22))) (K.neg (K.mul (co K X (5)) (co K X (23))))) := rfl
  have t11 : O.le K.zero (VGoodTail.tt K X (6) (21)) := VGoodTail.tail_nn K O X hg (6) 14
  have et11 : (VGoodTail.tt K X (6) (21)) = (K.add (K.mul (co K X (6)) (co K X (20))) (K.neg (K.mul (co K X (5)) (co K X (21))))) := rfl
  have t12 : O.le K.zero (VGoodTail.tt K X (6) (19)) := VGoodTail.tail_nn K O X hg (6) 12
  have et12 : (VGoodTail.tt K X (6) (19)) = (K.add (K.mul (co K X (6)) (co K X (18))) (K.neg (K.mul (co K X (5)) (co K X (19))))) := rfl
  have t13 : O.le K.zero (VGoodTail.tt K X (11) (24)) := VGoodTail.tail_nn K O X hg (11) 12
  have et13 : (VGoodTail.tt K X (11) (24)) = (K.add (K.mul (co K X (11)) (co K X (23))) (K.neg (K.mul (co K X (10)) (co K X (24))))) := rfl
  have t14 : O.le K.zero (VGoodTail.tt K X (11) (22)) := VGoodTail.tail_nn K O X hg (11) 10
  have et14 : (VGoodTail.tt K X (11) (22)) = (K.add (K.mul (co K X (11)) (co K X (21))) (K.neg (K.mul (co K X (10)) (co K X (22))))) := rfl
  have t15 : O.le K.zero (VGoodTail.tt K X (11) (20)) := VGoodTail.tail_nn K O X hg (11) 8
  have et15 : (VGoodTail.tt K X (11) (20)) = (K.add (K.mul (co K X (11)) (co K X (19))) (K.neg (K.mul (co K X (10)) (co K X (20))))) := rfl
  have t16 : O.le K.zero (VGoodTail.tt K X (11) (18)) := VGoodTail.tail_nn K O X hg (11) 6
  have et16 : (VGoodTail.tt K X (11) (18)) = (K.add (K.mul (co K X (11)) (co K X (17))) (K.neg (K.mul (co K X (10)) (co K X (18))))) := rfl
  have t17 : O.le K.zero (VGoodTail.tt K X (9) (24)) := VGoodTail.tail_nn K O X hg (9) 14
  have et17 : (VGoodTail.tt K X (9) (24)) = (K.add (K.mul (co K X (9)) (co K X (23))) (K.neg (K.mul (co K X (8)) (co K X (24))))) := rfl
  have t18 : O.le K.zero (VGoodTail.tt K X (9) (18)) := VGoodTail.tail_nn K O X hg (9) 8
  have et18 : (VGoodTail.tt K X (9) (18)) = (K.add (K.mul (co K X (9)) (co K X (17))) (K.neg (K.mul (co K X (8)) (co K X (18))))) := rfl
  have t19 : O.le K.zero (VGoodTail.tt K X (7) (24)) := VGoodTail.tail_nn K O X hg (7) 16
  have et19 : (VGoodTail.tt K X (7) (24)) = (K.add (K.mul (co K X (7)) (co K X (23))) (K.neg (K.mul (co K X (6)) (co K X (24))))) := rfl
  have t20 : O.le K.zero (VGoodTail.tt K X (7) (18)) := VGoodTail.tail_nn K O X hg (7) 10
  have et20 : (VGoodTail.tt K X (7) (18)) = (K.add (K.mul (co K X (7)) (co K X (17))) (K.neg (K.mul (co K X (6)) (co K X (18))))) := rfl
  have t21 : O.le K.zero (VGoodTail.tt K X (12) (25)) := VGoodTail.tail_nn K O X hg (12) 12
  have et21 : (VGoodTail.tt K X (12) (25)) = (K.add (K.mul (co K X (12)) (co K X (24))) (K.neg (K.mul (co K X (11)) (co K X (25))))) := rfl
  have t22 : O.le K.zero (VGoodTail.tt K X (12) (23)) := VGoodTail.tail_nn K O X hg (12) 10
  have et22 : (VGoodTail.tt K X (12) (23)) = (K.add (K.mul (co K X (12)) (co K X (22))) (K.neg (K.mul (co K X (11)) (co K X (23))))) := rfl
  have t23 : O.le K.zero (VGoodTail.tt K X (12) (21)) := VGoodTail.tail_nn K O X hg (12) 8
  have et23 : (VGoodTail.tt K X (12) (21)) = (K.add (K.mul (co K X (12)) (co K X (20))) (K.neg (K.mul (co K X (11)) (co K X (21))))) := rfl
  have t24 : O.le K.zero (VGoodTail.tt K X (12) (19)) := VGoodTail.tail_nn K O X hg (12) 6
  have et24 : (VGoodTail.tt K X (12) (19)) = (K.add (K.mul (co K X (12)) (co K X (18))) (K.neg (K.mul (co K X (11)) (co K X (19))))) := rfl
  have t25 : O.le K.zero (VGoodTail.tt K X (12) (17)) := VGoodTail.tail_nn K O X hg (12) 4
  have et25 : (VGoodTail.tt K X (12) (17)) = (K.add (K.mul (co K X (12)) (co K X (16))) (K.neg (K.mul (co K X (11)) (co K X (17))))) := rfl
  have t26 : O.le K.zero (VGoodTail.tt K X (10) (25)) := VGoodTail.tail_nn K O X hg (10) 14
  have et26 : (VGoodTail.tt K X (10) (25)) = (K.add (K.mul (co K X (10)) (co K X (24))) (K.neg (K.mul (co K X (9)) (co K X (25))))) := rfl
  have t27 : O.le K.zero (VGoodTail.tt K X (10) (17)) := VGoodTail.tail_nn K O X hg (10) 6
  have et27 : (VGoodTail.tt K X (10) (17)) = (K.add (K.mul (co K X (10)) (co K X (16))) (K.neg (K.mul (co K X (9)) (co K X (17))))) := rfl
  have t28 : O.le K.zero (VGoodTail.tt K X (8) (25)) := VGoodTail.tail_nn K O X hg (8) 16
  have et28 : (VGoodTail.tt K X (8) (25)) = (K.add (K.mul (co K X (8)) (co K X (24))) (K.neg (K.mul (co K X (7)) (co K X (25))))) := rfl
  have t29 : O.le K.zero (VGoodTail.tt K X (8) (17)) := VGoodTail.tail_nn K O X hg (8) 8
  have et29 : (VGoodTail.tt K X (8) (17)) = (K.add (K.mul (co K X (8)) (co K X (16))) (K.neg (K.mul (co K X (7)) (co K X (17))))) := rfl
  have hp : 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.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 (VGoodTail.tt K X (8) (21)) (VGoodTail.tt K X (8) (21))) (VGoodTail.tt K X (8) (21))) (VGoodTail  -- (+1114 chars; full line in the Zenodo source)
    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 _ _ (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 _ _ (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   -- (+672 chars; full line in the Zenodo source)
  refine O.le_trans _ _ _ hp (TripleE.le_of_eq' K O _ _ ?_)
  rw [kv, et0, et1, et2, et3, et4, et5, et6, et7, et8, et9, et10, et11, et12, et13, et14, et15, et16, et17, et18, et19, et20, et21, et22, et23, et24, et25, et26, et27, et28, et29]
  ring_z

theorem kna_3_0 (X : List R) (hg : L1Full.Good K O X) :
    O.le K.zero (CBLib.kern K (Fa K X) (Ga K X) 5 (3) (4)) := by
  have kv : CBLib.kern K (Fa K X) (Ga K X) 5 (3) (4) = (K.add (K.add (K.add (K.add (K.add K.zero (K.add (K.mul (co K X (9)) (co K X (9))) (K.neg (K.mul (co K X (10)) (co K X (8)))))) (K.add (K.mul (K.add (co K X (10)) (co K X (8))) (K.add (co K X (10)) (co K X (8)))) (K.neg (K.mul (K.add (co K X (11)) (co K X (9))) (K.add (co K X (9)) (co K X (7))))))) (K.add (K.mul (K.add (K.add (co K X (11)) (co  -- (+951 chars; full line in the Zenodo source)
  have t0 : O.le K.zero (VGoodTail.tt K X (9) (10)) := VGoodTail.tail_nn K O X hg (9) 0
  have et0 : (VGoodTail.tt K X (9) (10)) = (K.add (K.mul (co K X (9)) (co K X (9))) (K.neg (K.mul (co K X (8)) (co K X (10))))) := rfl
  have t1 : O.le K.zero (VGoodTail.tt K X (11) (12)) := VGoodTail.tail_nn K O X hg (11) 0
  have et1 : (VGoodTail.tt K X (11) (12)) = (K.add (K.mul (co K X (11)) (co K X (11))) (K.neg (K.mul (co K X (10)) (co K X (12))))) := rfl
  have t2 : O.le K.zero (VGoodTail.tt K X (9) (12)) := VGoodTail.tail_nn K O X hg (9) 2
  have et2 : (VGoodTail.tt K X (9) (12)) = (K.add (K.mul (co K X (9)) (co K X (11))) (K.neg (K.mul (co K X (8)) (co K X (12))))) := rfl
  have t3 : O.le K.zero (VGoodTail.tt K X (7) (12)) := VGoodTail.tail_nn K O X hg (7) 4
  have et3 : (VGoodTail.tt K X (7) (12)) = (K.add (K.mul (co K X (7)) (co K X (11))) (K.neg (K.mul (co K X (6)) (co K X (12))))) := rfl
  have t4 : O.le K.zero (VGoodTail.tt K X (7) (10)) := VGoodTail.tail_nn K O X hg (7) 2
  have et4 : (VGoodTail.tt K X (7) (10)) = (K.add (K.mul (co K X (7)) (co K X (9))) (K.neg (K.mul (co K X (6)) (co K X (10))))) := rfl
  have t5 : O.le K.zero (VGoodTail.tt K X (7) (8)) := VGoodTail.tail_nn K O X hg (7) 0
  have et5 : (VGoodTail.tt K X (7) (8)) = (K.add (K.mul (co K X (7)) (co K X (7))) (K.neg (K.mul (co K X (6)) (co K X (8))))) := rfl
  have t6 : O.le K.zero (VGoodTail.tt K X (13) (14)) := VGoodTail.tail_nn K O X hg (13) 0
  have et6 : (VGoodTail.tt K X (13) (14)) = (K.add (K.mul (co K X (13)) (co K X (13))) (K.neg (K.mul (co K X (12)) (co K X (14))))) := rfl
  have t7 : O.le K.zero (VGoodTail.tt K X (11) (14)) := VGoodTail.tail_nn K O X hg (11) 2
  have et7 : (VGoodTail.tt K X (11) (14)) = (K.add (K.mul (co K X (11)) (co K X (13))) (K.neg (K.mul (co K X (10)) (co K X (14))))) := rfl
  have t8 : O.le K.zero (VGoodTail.tt K X (9) (14)) := VGoodTail.tail_nn K O X hg (9) 4
  have et8 : (VGoodTail.tt K X (9) (14)) = (K.add (K.mul (co K X (9)) (co K X (13))) (K.neg (K.mul (co K X (8)) (co K X (14))))) := rfl
  have t9 : O.le K.zero (VGoodTail.tt K X (7) (14)) := VGoodTail.tail_nn K O X hg (7) 6
  have et9 : (VGoodTail.tt K X (7) (14)) = (K.add (K.mul (co K X (7)) (co K X (13))) (K.neg (K.mul (co K X (6)) (co K X (14))))) := rfl
  have hp : O.le K.zero (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (VGoodTail.tt K X (9) (10)) (VGoodTail.tt K X (11) (12))) (VGoodTail.tt K X (9) (12))) (VGoodTail.tt K X (7) (12))) (VGoodTail.tt K X (7) (10))) (VGoodTail.tt K X (7) (8))) (VGoodTail.tt K X (13) (14))) (VGoodTail.tt K X (11) (14))) (VGoodTail.tt K X (9) (14))) (VGoodTail.tt K X (7) (14))) := by
    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 _ _ (TopLC.add_nn K O _ _ (TopLC.add_nn K O _ _ (TopLC.add_nn K O _ _ t0 t1) t2) t3) t4) t5) t6) t7) t8) t9)
  refine O.le_trans _ _ _ hp (TripleE.le_of_eq' K O _ _ ?_)
  rw [kv, et0, et1, et2, et3, et4, et5, et6, et7, et8, et9]
  ring_z

theorem kna_3_1 (X : List R) (hg : L1Full.Good K O X) :
    O.le K.zero (CBLib.kern K (Fa K X) (Ga K X) 5 (3) (5)) := by
  have kv : CBLib.kern K (Fa K X) (Ga K X) 5 (3) (5) = (K.add (K.add (K.add (K.add (K.add K.zero (K.add (K.mul (co K X (9)) (co K X (10))) (K.neg (K.mul (co K X (11)) (co K X (8)))))) (K.add (K.mul (K.add (co K X (10)) (co K X (8))) (K.add (co K X (11)) (co K X (9)))) (K.neg (K.mul (K.add (co K X (12)) (co K X (10))) (K.add (co K X (9)) (co K X (7))))))) (K.add (K.mul (K.add (K.add (co K X (11)) (  -- (+956 chars; full line in the Zenodo source)
  have t0 : O.le K.zero (VGoodTail.tt K X (9) (11)) := VGoodTail.tail_nn K O X hg (9) 1
  have et0 : (VGoodTail.tt K X (9) (11)) = (K.add (K.mul (co K X (9)) (co K X (10))) (K.neg (K.mul (co K X (8)) (co K X (11))))) := rfl
  have t1 : O.le K.zero (VGoodTail.tt K X (10) (12)) := VGoodTail.tail_nn K O X hg (10) 1
  have et1 : (VGoodTail.tt K X (10) (12)) = (K.add (K.mul (co K X (10)) (co K X (11))) (K.neg (K.mul (co K X (9)) (co K X (12))))) := rfl
  have t2 : O.le K.zero (VGoodTail.tt K X (8) (12)) := VGoodTail.tail_nn K O X hg (8) 3
  have et2 : (VGoodTail.tt K X (8) (12)) = (K.add (K.mul (co K X (8)) (co K X (11))) (K.neg (K.mul (co K X (7)) (co K X (12))))) := rfl
  have t3 : O.le K.zero (VGoodTail.tt K X (8) (10)) := VGoodTail.tail_nn K O X hg (8) 1
  have et3 : (VGoodTail.tt K X (8) (10)) = (K.add (K.mul (co K X (8)) (co K X (9))) (K.neg (K.mul (co K X (7)) (co K X (10))))) := rfl
  have t4 : O.le K.zero (VGoodTail.tt K X (11) (13)) := VGoodTail.tail_nn K O X hg (11) 1
  have et4 : (VGoodTail.tt K X (11) (13)) = (K.add (K.mul (co K X (11)) (co K X (12))) (K.neg (K.mul (co K X (10)) (co K X (13))))) := rfl
  have t5 : O.le K.zero (VGoodTail.tt K X (9) (13)) := VGoodTail.tail_nn K O X hg (9) 3
  have et5 : (VGoodTail.tt K X (9) (13)) = (K.add (K.mul (co K X (9)) (co K X (12))) (K.neg (K.mul (co K X (8)) (co K X (13))))) := rfl
  have t6 : O.le K.zero (VGoodTail.tt K X (7) (13)) := VGoodTail.tail_nn K O X hg (7) 5
  have et6 : (VGoodTail.tt K X (7) (13)) = (K.add (K.mul (co K X (7)) (co K X (12))) (K.neg (K.mul (co K X (6)) (co K X (13))))) := rfl
  have t7 : O.le K.zero (VGoodTail.tt K X (7) (11)) := VGoodTail.tail_nn K O X hg (7) 3
  have et7 : (VGoodTail.tt K X (7) (11)) = (K.add (K.mul (co K X (7)) (co K X (10))) (K.neg (K.mul (co K X (6)) (co K X (11))))) := rfl
  have t8 : O.le K.zero (VGoodTail.tt K X (7) (9)) := VGoodTail.tail_nn K O X hg (7) 1
  have et8 : (VGoodTail.tt K X (7) (9)) = (K.add (K.mul (co K X (7)) (co K X (8))) (K.neg (K.mul (co K X (6)) (co K X (9))))) := rfl
  have t9 : O.le K.zero (VGoodTail.tt K X (12) (14)) := VGoodTail.tail_nn K O X hg (12) 1
  have et9 : (VGoodTail.tt K X (12) (14)) = (K.add (K.mul (co K X (12)) (co K X (13))) (K.neg (K.mul (co K X (11)) (co K X (14))))) := rfl
  have t10 : O.le K.zero (VGoodTail.tt K X (10) (14)) := VGoodTail.tail_nn K O X hg (10) 3
  have et10 : (VGoodTail.tt K X (10) (14)) = (K.add (K.mul (co K X (10)) (co K X (13))) (K.neg (K.mul (co K X (9)) (co K X (14))))) := rfl
  have t11 : O.le K.zero (VGoodTail.tt K X (8) (14)) := VGoodTail.tail_nn K O X hg (8) 5
  have et11 : (VGoodTail.tt K X (8) (14)) = (K.add (K.mul (co K X (8)) (co K X (13))) (K.neg (K.mul (co K X (7)) (co K X (14))))) := rfl
  have t12 : O.le K.zero (VGoodTail.tt K X (6) (14)) := VGoodTail.tail_nn K O X hg (6) 7
  have et12 : (VGoodTail.tt K X (6) (14)) = (K.add (K.mul (co K X (6)) (co K X (13))) (K.neg (K.mul (co K X (5)) (co K X (14))))) := rfl
  have t13 : O.le K.zero (VGoodTail.tt K X (6) (12)) := VGoodTail.tail_nn K O X hg (6) 5
  have et13 : (VGoodTail.tt K X (6) (12)) = (K.add (K.mul (co K X (6)) (co K X (11))) (K.neg (K.mul (co K X (5)) (co K X (12))))) := rfl
  have t14 : O.le K.zero (VGoodTail.tt K X (6) (10)) := VGoodTail.tail_nn K O X hg (6) 3
  have et14 : (VGoodTail.tt K X (6) (10)) = (K.add (K.mul (co K X (6)) (co K X (9))) (K.neg (K.mul (co K X (5)) (co K X (10))))) := rfl
  have t15 : O.le K.zero (VGoodTail.tt K X (6) (8)) := VGoodTail.tail_nn K O X hg (6) 1
  have et15 : (VGoodTail.tt K X (6) (8)) = (K.add (K.mul (co K X (6)) (co K X (7))) (K.neg (K.mul (co K X (5)) (co K X (8))))) := rfl
  have t16 : O.le K.zero (VGoodTail.tt K X (13) (15)) := VGoodTail.tail_nn K O X hg (13) 1
  have et16 : (VGoodTail.tt K X (13) (15)) = (K.add (K.mul (co K X (13)) (co K X (14))) (K.neg (K.mul (co K X (12)) (co K X (15))))) := rfl
  have t17 : O.le K.zero (VGoodTail.tt K X (11) (15)) := VGoodTail.tail_nn K O X hg (11) 3
  have et17 : (VGoodTail.tt K X (11) (15)) = (K.add (K.mul (co K X (11)) (co K X (14))) (K.neg (K.mul (co K X (10)) (co K X (15))))) := rfl
  have t18 : O.le K.zero (VGoodTail.tt K X (9) (15)) := VGoodTail.tail_nn K O X hg (9) 5
  have et18 : (VGoodTail.tt K X (9) (15)) = (K.add (K.mul (co K X (9)) (co K X (14))) (K.neg (K.mul (co K X (8)) (co K X (15))))) := rfl
  have t19 : O.le K.zero (VGoodTail.tt K X (7) (15)) := VGoodTail.tail_nn K O X hg (7) 7
  have et19 : (VGoodTail.tt K X (7) (15)) = (K.add (K.mul (co K X (7)) (co K X (14))) (K.neg (K.mul (co K X (6)) (co K X (15))))) := rfl
  have hp : 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.add (K.add (K.add (K.add (K.add (K.add (VGoodTail.tt K X (9) (11)) (VGoodTail.tt K X (10) (12))) (VGoodTail.tt K X (8) (12))) (VGoodTail.tt K X (8) (10))) (VGoodTail.tt K X (11) (13))) (VGoodTail.tt K X (9) (13))) (VGoodTail.tt K X (7) (13))) (VGoodTail.tt K X (7) (11))) (VGoodTail  -- (+345 chars; full line in the Zenodo source)
    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 _ _ (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 _ _ (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   -- (+116 chars; full line in the Zenodo source)
  refine O.le_trans _ _ _ hp (TripleE.le_of_eq' K O _ _ ?_)
  rw [kv, et0, et1, et2, et3, et4, et5, et6, et7, et8, et9, et10, et11, et12, et13, et14, et15, et16, et17, et18, et19]
  ring_z

theorem kna_3_2 (X : List R) (hg : L1Full.Good K O X) :
    O.le K.zero (CBLib.kern K (Fa K X) (Ga K X) 5 (3) (6)) := by
  have kv : CBLib.kern K (Fa K X) (Ga K X) 5 (3) (6) = (K.add (K.add (K.add (K.add (K.add K.zero (K.add (K.mul (co K X (9)) (co K X (11))) (K.neg (K.mul (co K X (12)) (co K X (8)))))) (K.add (K.mul (K.add (co K X (10)) (co K X (8))) (K.add (co K X (12)) (co K X (10)))) (K.neg (K.mul (K.add (co K X (13)) (co K X (11))) (K.add (co K X (9)) (co K X (7))))))) (K.add (K.mul (K.add (K.add (co K X (11))   -- (+960 chars; full line in the Zenodo source)
  have t0 : O.le K.zero (VGoodTail.tt K X (9) (12)) := VGoodTail.tail_nn K O X hg (9) 2
  have et0 : (VGoodTail.tt K X (9) (12)) = (K.add (K.mul (co K X (9)) (co K X (11))) (K.neg (K.mul (co K X (8)) (co K X (12))))) := rfl
  have t1 : O.le K.zero (VGoodTail.tt K X (10) (13)) := VGoodTail.tail_nn K O X hg (10) 2
  have et1 : (VGoodTail.tt K X (10) (13)) = (K.add (K.mul (co K X (10)) (co K X (12))) (K.neg (K.mul (co K X (9)) (co K X (13))))) := rfl
  have t2 : O.le K.zero (VGoodTail.tt K X (8) (13)) := VGoodTail.tail_nn K O X hg (8) 4
  have et2 : (VGoodTail.tt K X (8) (13)) = (K.add (K.mul (co K X (8)) (co K X (12))) (K.neg (K.mul (co K X (7)) (co K X (13))))) := rfl
  have t3 : O.le K.zero (VGoodTail.tt K X (8) (11)) := VGoodTail.tail_nn K O X hg (8) 2
  have et3 : (VGoodTail.tt K X (8) (11)) = (K.add (K.mul (co K X (8)) (co K X (10))) (K.neg (K.mul (co K X (7)) (co K X (11))))) := rfl
  have t4 : O.le K.zero (VGoodTail.tt K X (11) (14)) := VGoodTail.tail_nn K O X hg (11) 2
  have et4 : (VGoodTail.tt K X (11) (14)) = (K.add (K.mul (co K X (11)) (co K X (13))) (K.neg (K.mul (co K X (10)) (co K X (14))))) := rfl
  have t5 : O.le K.zero (VGoodTail.tt K X (11) (12)) := VGoodTail.tail_nn K O X hg (11) 0
  have et5 : (VGoodTail.tt K X (11) (12)) = (K.add (K.mul (co K X (11)) (co K X (11))) (K.neg (K.mul (co K X (10)) (co K X (12))))) := rfl
  have t6 : O.le K.zero (VGoodTail.tt K X (9) (14)) := VGoodTail.tail_nn K O X hg (9) 4
  have et6 : (VGoodTail.tt K X (9) (14)) = (K.add (K.mul (co K X (9)) (co K X (13))) (K.neg (K.mul (co K X (8)) (co K X (14))))) := rfl
  have t7 : O.le K.zero (VGoodTail.tt K X (9) (10)) := VGoodTail.tail_nn K O X hg (9) 0
  have et7 : (VGoodTail.tt K X (9) (10)) = (K.add (K.mul (co K X (9)) (co K X (9))) (K.neg (K.mul (co K X (8)) (co K X (10))))) := rfl
  have t8 : O.le K.zero (VGoodTail.tt K X (7) (14)) := VGoodTail.tail_nn K O X hg (7) 6
  have et8 : (VGoodTail.tt K X (7) (14)) = (K.add (K.mul (co K X (7)) (co K X (13))) (K.neg (K.mul (co K X (6)) (co K X (14))))) := rfl
  have t9 : O.le K.zero (VGoodTail.tt K X (7) (12)) := VGoodTail.tail_nn K O X hg (7) 4
  have et9 : (VGoodTail.tt K X (7) (12)) = (K.add (K.mul (co K X (7)) (co K X (11))) (K.neg (K.mul (co K X (6)) (co K X (12))))) := rfl
  have t10 : O.le K.zero (VGoodTail.tt K X (7) (10)) := VGoodTail.tail_nn K O X hg (7) 2
  have et10 : (VGoodTail.tt K X (7) (10)) = (K.add (K.mul (co K X (7)) (co K X (9))) (K.neg (K.mul (co K X (6)) (co K X (10))))) := rfl
  have t11 : O.le K.zero (VGoodTail.tt K X (12) (15)) := VGoodTail.tail_nn K O X hg (12) 2
  have et11 : (VGoodTail.tt K X (12) (15)) = (K.add (K.mul (co K X (12)) (co K X (14))) (K.neg (K.mul (co K X (11)) (co K X (15))))) := rfl
  have t12 : O.le K.zero (VGoodTail.tt K X (10) (15)) := VGoodTail.tail_nn K O X hg (10) 4
  have et12 : (VGoodTail.tt K X (10) (15)) = (K.add (K.mul (co K X (10)) (co K X (14))) (K.neg (K.mul (co K X (9)) (co K X (15))))) := rfl
  have t13 : O.le K.zero (VGoodTail.tt K X (8) (15)) := VGoodTail.tail_nn K O X hg (8) 6
  have et13 : (VGoodTail.tt K X (8) (15)) = (K.add (K.mul (co K X (8)) (co K X (14))) (K.neg (K.mul (co K X (7)) (co K X (15))))) := rfl
  have t14 : O.le K.zero (VGoodTail.tt K X (6) (15)) := VGoodTail.tail_nn K O X hg (6) 8
  have et14 : (VGoodTail.tt K X (6) (15)) = (K.add (K.mul (co K X (6)) (co K X (14))) (K.neg (K.mul (co K X (5)) (co K X (15))))) := rfl
  have t15 : O.le K.zero (VGoodTail.tt K X (6) (13)) := VGoodTail.tail_nn K O X hg (6) 6
  have et15 : (VGoodTail.tt K X (6) (13)) = (K.add (K.mul (co K X (6)) (co K X (12))) (K.neg (K.mul (co K X (5)) (co K X (13))))) := rfl
  have t16 : O.le K.zero (VGoodTail.tt K X (6) (11)) := VGoodTail.tail_nn K O X hg (6) 4
  have et16 : (VGoodTail.tt K X (6) (11)) = (K.add (K.mul (co K X (6)) (co K X (10))) (K.neg (K.mul (co K X (5)) (co K X (11))))) := rfl
  have t17 : O.le K.zero (VGoodTail.tt K X (6) (9)) := VGoodTail.tail_nn K O X hg (6) 2
  have et17 : (VGoodTail.tt K X (6) (9)) = (K.add (K.mul (co K X (6)) (co K X (8))) (K.neg (K.mul (co K X (5)) (co K X (9))))) := rfl
  have t18 : O.le K.zero (VGoodTail.tt K X (13) (16)) := VGoodTail.tail_nn K O X hg (13) 2
  have et18 : (VGoodTail.tt K X (13) (16)) = (K.add (K.mul (co K X (13)) (co K X (15))) (K.neg (K.mul (co K X (12)) (co K X (16))))) := rfl
  have t19 : O.le K.zero (VGoodTail.tt K X (13) (14)) := VGoodTail.tail_nn K O X hg (13) 0
  have et19 : (VGoodTail.tt K X (13) (14)) = (K.add (K.mul (co K X (13)) (co K X (13))) (K.neg (K.mul (co K X (12)) (co K X (14))))) := rfl
  have t20 : O.le K.zero (VGoodTail.tt K X (11) (16)) := VGoodTail.tail_nn K O X hg (11) 4
  have et20 : (VGoodTail.tt K X (11) (16)) = (K.add (K.mul (co K X (11)) (co K X (15))) (K.neg (K.mul (co K X (10)) (co K X (16))))) := rfl
  have t21 : O.le K.zero (VGoodTail.tt K X (9) (16)) := VGoodTail.tail_nn K O X hg (9) 6
  have et21 : (VGoodTail.tt K X (9) (16)) = (K.add (K.mul (co K X (9)) (co K X (15))) (K.neg (K.mul (co K X (8)) (co K X (16))))) := rfl
  have t22 : O.le K.zero (VGoodTail.tt K X (7) (16)) := VGoodTail.tail_nn K O X hg (7) 8
  have et22 : (VGoodTail.tt K X (7) (16)) = (K.add (K.mul (co K X (7)) (co K X (15))) (K.neg (K.mul (co K X (6)) (co K X (16))))) := rfl
  have t23 : O.le K.zero (VGoodTail.tt K X (7) (8)) := VGoodTail.tail_nn K O X hg (7) 0
  have et23 : (VGoodTail.tt K X (7) (8)) = (K.add (K.mul (co K X (7)) (co K X (7))) (K.neg (K.mul (co K X (6)) (co K X (8))))) := rfl
  have hp : 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.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 (VGoodTail.tt K X (9) (12)) (VGoodTail.tt K X (9) (12))) (VGoodTail.tt K X (10) (13))) (VGoodTail.tt K X (8) (13))) (VGoodTail.tt K X (8) (11))) (VGoodTail.tt K X (11) (14))  -- (+708 chars; full line in the Zenodo source)
    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 _ _ (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 _ _ (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   -- (+381 chars; full line in the Zenodo source)
  refine O.le_trans _ _ _ hp (TripleE.le_of_eq' K O _ _ ?_)
  rw [kv, et0, et1, et2, et3, et4, et5, et6, et7, et8, et9, et10, et11, et12, et13, et14, et15, et16, et17, et18, et19, et20, et21, et22, et23]
  ring_z

theorem kna_3_3 (X : List R) (hg : L1Full.Good K O X) :
    O.le K.zero (CBLib.kern K (Fa K X) (Ga K X) 5 (3) (7)) := by
  have kv : CBLib.kern K (Fa K X) (Ga K X) 5 (3) (7) = (K.add (K.add (K.add (K.add (K.add K.zero (K.add (K.mul (co K X (9)) (co K X (12))) (K.neg (K.mul (co K X (13)) (co K X (8)))))) (K.add (K.mul (K.add (co K X (10)) (co K X (8))) (K.add (co K X (13)) (co K X (11)))) (K.neg (K.mul (K.add (co K X (14)) (co K X (12))) (K.add (co K X (9)) (co K X (7))))))) (K.add (K.mul (K.add (K.add (co K X (11))   -- (+963 chars; full line in the Zenodo source)
  have t0 : O.le K.zero (VGoodTail.tt K X (9) (13)) := VGoodTail.tail_nn K O X hg (9) 3
  have et0 : (VGoodTail.tt K X (9) (13)) = (K.add (K.mul (co K X (9)) (co K X (12))) (K.neg (K.mul (co K X (8)) (co K X (13))))) := rfl
  have t1 : O.le K.zero (VGoodTail.tt K X (10) (14)) := VGoodTail.tail_nn K O X hg (10) 3
  have et1 : (VGoodTail.tt K X (10) (14)) = (K.add (K.mul (co K X (10)) (co K X (13))) (K.neg (K.mul (co K X (9)) (co K X (14))))) := rfl
  have t2 : O.le K.zero (VGoodTail.tt K X (10) (12)) := VGoodTail.tail_nn K O X hg (10) 1
  have et2 : (VGoodTail.tt K X (10) (12)) = (K.add (K.mul (co K X (10)) (co K X (11))) (K.neg (K.mul (co K X (9)) (co K X (12))))) := rfl
  have t3 : O.le K.zero (VGoodTail.tt K X (8) (14)) := VGoodTail.tail_nn K O X hg (8) 5
  have et3 : (VGoodTail.tt K X (8) (14)) = (K.add (K.mul (co K X (8)) (co K X (13))) (K.neg (K.mul (co K X (7)) (co K X (14))))) := rfl
  have t4 : O.le K.zero (VGoodTail.tt K X (8) (12)) := VGoodTail.tail_nn K O X hg (8) 3
  have et4 : (VGoodTail.tt K X (8) (12)) = (K.add (K.mul (co K X (8)) (co K X (11))) (K.neg (K.mul (co K X (7)) (co K X (12))))) := rfl
  have t5 : O.le K.zero (VGoodTail.tt K X (11) (15)) := VGoodTail.tail_nn K O X hg (11) 3
  have et5 : (VGoodTail.tt K X (11) (15)) = (K.add (K.mul (co K X (11)) (co K X (14))) (K.neg (K.mul (co K X (10)) (co K X (15))))) := rfl
  have t6 : O.le K.zero (VGoodTail.tt K X (11) (13)) := VGoodTail.tail_nn K O X hg (11) 1
  have et6 : (VGoodTail.tt K X (11) (13)) = (K.add (K.mul (co K X (11)) (co K X (12))) (K.neg (K.mul (co K X (10)) (co K X (13))))) := rfl
  have t7 : O.le K.zero (VGoodTail.tt K X (9) (15)) := VGoodTail.tail_nn K O X hg (9) 5
  have et7 : (VGoodTail.tt K X (9) (15)) = (K.add (K.mul (co K X (9)) (co K X (14))) (K.neg (K.mul (co K X (8)) (co K X (15))))) := rfl
  have t8 : O.le K.zero (VGoodTail.tt K X (9) (11)) := VGoodTail.tail_nn K O X hg (9) 1
  have et8 : (VGoodTail.tt K X (9) (11)) = (K.add (K.mul (co K X (9)) (co K X (10))) (K.neg (K.mul (co K X (8)) (co K X (11))))) := rfl
  have t9 : O.le K.zero (VGoodTail.tt K X (7) (15)) := VGoodTail.tail_nn K O X hg (7) 7
  have et9 : (VGoodTail.tt K X (7) (15)) = (K.add (K.mul (co K X (7)) (co K X (14))) (K.neg (K.mul (co K X (6)) (co K X (15))))) := rfl
  have t10 : O.le K.zero (VGoodTail.tt K X (7) (13)) := VGoodTail.tail_nn K O X hg (7) 5
  have et10 : (VGoodTail.tt K X (7) (13)) = (K.add (K.mul (co K X (7)) (co K X (12))) (K.neg (K.mul (co K X (6)) (co K X (13))))) := rfl
  have t11 : O.le K.zero (VGoodTail.tt K X (7) (11)) := VGoodTail.tail_nn K O X hg (7) 3
  have et11 : (VGoodTail.tt K X (7) (11)) = (K.add (K.mul (co K X (7)) (co K X (10))) (K.neg (K.mul (co K X (6)) (co K X (11))))) := rfl
  have t12 : O.le K.zero (VGoodTail.tt K X (12) (16)) := VGoodTail.tail_nn K O X hg (12) 3
  have et12 : (VGoodTail.tt K X (12) (16)) = (K.add (K.mul (co K X (12)) (co K X (15))) (K.neg (K.mul (co K X (11)) (co K X (16))))) := rfl
  have t13 : O.le K.zero (VGoodTail.tt K X (12) (14)) := VGoodTail.tail_nn K O X hg (12) 1
  have et13 : (VGoodTail.tt K X (12) (14)) = (K.add (K.mul (co K X (12)) (co K X (13))) (K.neg (K.mul (co K X (11)) (co K X (14))))) := rfl
  have t14 : O.le K.zero (VGoodTail.tt K X (10) (16)) := VGoodTail.tail_nn K O X hg (10) 5
  have et14 : (VGoodTail.tt K X (10) (16)) = (K.add (K.mul (co K X (10)) (co K X (15))) (K.neg (K.mul (co K X (9)) (co K X (16))))) := rfl
  have t15 : O.le K.zero (VGoodTail.tt K X (8) (16)) := VGoodTail.tail_nn K O X hg (8) 7
  have et15 : (VGoodTail.tt K X (8) (16)) = (K.add (K.mul (co K X (8)) (co K X (15))) (K.neg (K.mul (co K X (7)) (co K X (16))))) := rfl
  have t16 : O.le K.zero (VGoodTail.tt K X (8) (10)) := VGoodTail.tail_nn K O X hg (8) 1
  have et16 : (VGoodTail.tt K X (8) (10)) = (K.add (K.mul (co K X (8)) (co K X (9))) (K.neg (K.mul (co K X (7)) (co K X (10))))) := rfl
  have t17 : O.le K.zero (VGoodTail.tt K X (6) (16)) := VGoodTail.tail_nn K O X hg (6) 9
  have et17 : (VGoodTail.tt K X (6) (16)) = (K.add (K.mul (co K X (6)) (co K X (15))) (K.neg (K.mul (co K X (5)) (co K X (16))))) := rfl
  have t18 : O.le K.zero (VGoodTail.tt K X (6) (14)) := VGoodTail.tail_nn K O X hg (6) 7
  have et18 : (VGoodTail.tt K X (6) (14)) = (K.add (K.mul (co K X (6)) (co K X (13))) (K.neg (K.mul (co K X (5)) (co K X (14))))) := rfl
  have t19 : O.le K.zero (VGoodTail.tt K X (6) (12)) := VGoodTail.tail_nn K O X hg (6) 5
  have et19 : (VGoodTail.tt K X (6) (12)) = (K.add (K.mul (co K X (6)) (co K X (11))) (K.neg (K.mul (co K X (5)) (co K X (12))))) := rfl
  have t20 : O.le K.zero (VGoodTail.tt K X (6) (10)) := VGoodTail.tail_nn K O X hg (6) 3
  have et20 : (VGoodTail.tt K X (6) (10)) = (K.add (K.mul (co K X (6)) (co K X (9))) (K.neg (K.mul (co K X (5)) (co K X (10))))) := rfl
  have t21 : O.le K.zero (VGoodTail.tt K X (13) (17)) := VGoodTail.tail_nn K O X hg (13) 3
  have et21 : (VGoodTail.tt K X (13) (17)) = (K.add (K.mul (co K X (13)) (co K X (16))) (K.neg (K.mul (co K X (12)) (co K X (17))))) := rfl
  have t22 : O.le K.zero (VGoodTail.tt K X (13) (15)) := VGoodTail.tail_nn K O X hg (13) 1
  have et22 : (VGoodTail.tt K X (13) (15)) = (K.add (K.mul (co K X (13)) (co K X (14))) (K.neg (K.mul (co K X (12)) (co K X (15))))) := rfl
  have t23 : O.le K.zero (VGoodTail.tt K X (11) (17)) := VGoodTail.tail_nn K O X hg (11) 5
  have et23 : (VGoodTail.tt K X (11) (17)) = (K.add (K.mul (co K X (11)) (co K X (16))) (K.neg (K.mul (co K X (10)) (co K X (17))))) := rfl
  have t24 : O.le K.zero (VGoodTail.tt K X (9) (17)) := VGoodTail.tail_nn K O X hg (9) 7
  have et24 : (VGoodTail.tt K X (9) (17)) = (K.add (K.mul (co K X (9)) (co K X (16))) (K.neg (K.mul (co K X (8)) (co K X (17))))) := rfl
  have t25 : O.le K.zero (VGoodTail.tt K X (7) (17)) := VGoodTail.tail_nn K O X hg (7) 9
  have et25 : (VGoodTail.tt K X (7) (17)) = (K.add (K.mul (co K X (7)) (co K X (16))) (K.neg (K.mul (co K X (6)) (co K X (17))))) := rfl
  have t26 : O.le K.zero (VGoodTail.tt K X (7) (9)) := VGoodTail.tail_nn K O X hg (7) 1
  have et26 : (VGoodTail.tt K X (7) (9)) = (K.add (K.mul (co K X (7)) (co K X (8))) (K.neg (K.mul (co K X (6)) (co K X (9))))) := rfl
  have hp : 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.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 (VGoodTail.tt K X (9) (13)) (VGoodTail.tt K X (9) (13))) (VGoodTail.tt K X (10) (14))) (VGoodTail.tt K X (10) (14))) (VGoodTail.tt   -- (+928 chars; full line in the Zenodo source)
    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 _ _ (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 _ _ (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   -- (+541 chars; full line in the Zenodo source)
  refine O.le_trans _ _ _ hp (TripleE.le_of_eq' K O _ _ ?_)
  rw [kv, et0, et1, et2, et3, et4, et5, et6, et7, et8, et9, et10, et11, et12, et13, et14, et15, et16, et17, et18, et19, et20, et21, et22, et23, et24, et25, et26]
  ring_z

theorem kna_3_4 (X : List R) (hg : L1Full.Good K O X) :
    O.le K.zero (CBLib.kern K (Fa K X) (Ga K X) 5 (3) (8)) := by
  have kv : CBLib.kern K (Fa K X) (Ga K X) 5 (3) (8) = (K.add (K.add (K.add (K.add (K.add K.zero (K.add (K.mul (co K X (9)) (co K X (13))) (K.neg (K.mul (co K X (14)) (co K X (8)))))) (K.add (K.mul (K.add (co K X (10)) (co K X (8))) (K.add (co K X (14)) (co K X (12)))) (K.neg (K.mul (K.add (co K X (15)) (co K X (13))) (K.add (co K X (9)) (co K X (7))))))) (K.add (K.mul (K.add (K.add (co K X (11))   -- (+965 chars; full line in the Zenodo source)
  have t0 : O.le K.zero (VGoodTail.tt K X (9) (14)) := VGoodTail.tail_nn K O X hg (9) 4
  have et0 : (VGoodTail.tt K X (9) (14)) = (K.add (K.mul (co K X (9)) (co K X (13))) (K.neg (K.mul (co K X (8)) (co K X (14))))) := rfl
  have t1 : O.le K.zero (VGoodTail.tt K X (10) (15)) := VGoodTail.tail_nn K O X hg (10) 4
  have et1 : (VGoodTail.tt K X (10) (15)) = (K.add (K.mul (co K X (10)) (co K X (14))) (K.neg (K.mul (co K X (9)) (co K X (15))))) := rfl
  have t2 : O.le K.zero (VGoodTail.tt K X (10) (13)) := VGoodTail.tail_nn K O X hg (10) 2
  have et2 : (VGoodTail.tt K X (10) (13)) = (K.add (K.mul (co K X (10)) (co K X (12))) (K.neg (K.mul (co K X (9)) (co K X (13))))) := rfl
  have t3 : O.le K.zero (VGoodTail.tt K X (8) (15)) := VGoodTail.tail_nn K O X hg (8) 6
  have et3 : (VGoodTail.tt K X (8) (15)) = (K.add (K.mul (co K X (8)) (co K X (14))) (K.neg (K.mul (co K X (7)) (co K X (15))))) := rfl
  have t4 : O.le K.zero (VGoodTail.tt K X (8) (13)) := VGoodTail.tail_nn K O X hg (8) 4
  have et4 : (VGoodTail.tt K X (8) (13)) = (K.add (K.mul (co K X (8)) (co K X (12))) (K.neg (K.mul (co K X (7)) (co K X (13))))) := rfl
  have t5 : O.le K.zero (VGoodTail.tt K X (11) (16)) := VGoodTail.tail_nn K O X hg (11) 4
  have et5 : (VGoodTail.tt K X (11) (16)) = (K.add (K.mul (co K X (11)) (co K X (15))) (K.neg (K.mul (co K X (10)) (co K X (16))))) := rfl
  have t6 : O.le K.zero (VGoodTail.tt K X (11) (14)) := VGoodTail.tail_nn K O X hg (11) 2
  have et6 : (VGoodTail.tt K X (11) (14)) = (K.add (K.mul (co K X (11)) (co K X (13))) (K.neg (K.mul (co K X (10)) (co K X (14))))) := rfl
  have t7 : O.le K.zero (VGoodTail.tt K X (11) (12)) := VGoodTail.tail_nn K O X hg (11) 0
  have et7 : (VGoodTail.tt K X (11) (12)) = (K.add (K.mul (co K X (11)) (co K X (11))) (K.neg (K.mul (co K X (10)) (co K X (12))))) := rfl
  have t8 : O.le K.zero (VGoodTail.tt K X (9) (16)) := VGoodTail.tail_nn K O X hg (9) 6
  have et8 : (VGoodTail.tt K X (9) (16)) = (K.add (K.mul (co K X (9)) (co K X (15))) (K.neg (K.mul (co K X (8)) (co K X (16))))) := rfl
  have t9 : O.le K.zero (VGoodTail.tt K X (9) (12)) := VGoodTail.tail_nn K O X hg (9) 2
  have et9 : (VGoodTail.tt K X (9) (12)) = (K.add (K.mul (co K X (9)) (co K X (11))) (K.neg (K.mul (co K X (8)) (co K X (12))))) := rfl
  have t10 : O.le K.zero (VGoodTail.tt K X (7) (16)) := VGoodTail.tail_nn K O X hg (7) 8
  have et10 : (VGoodTail.tt K X (7) (16)) = (K.add (K.mul (co K X (7)) (co K X (15))) (K.neg (K.mul (co K X (6)) (co K X (16))))) := rfl
  have t11 : O.le K.zero (VGoodTail.tt K X (7) (14)) := VGoodTail.tail_nn K O X hg (7) 6
  have et11 : (VGoodTail.tt K X (7) (14)) = (K.add (K.mul (co K X (7)) (co K X (13))) (K.neg (K.mul (co K X (6)) (co K X (14))))) := rfl
  have t12 : O.le K.zero (VGoodTail.tt K X (7) (12)) := VGoodTail.tail_nn K O X hg (7) 4
  have et12 : (VGoodTail.tt K X (7) (12)) = (K.add (K.mul (co K X (7)) (co K X (11))) (K.neg (K.mul (co K X (6)) (co K X (12))))) := rfl
  have t13 : O.le K.zero (VGoodTail.tt K X (12) (17)) := VGoodTail.tail_nn K O X hg (12) 4
  have et13 : (VGoodTail.tt K X (12) (17)) = (K.add (K.mul (co K X (12)) (co K X (16))) (K.neg (K.mul (co K X (11)) (co K X (17))))) := rfl
  have t14 : O.le K.zero (VGoodTail.tt K X (12) (15)) := VGoodTail.tail_nn K O X hg (12) 2
  have et14 : (VGoodTail.tt K X (12) (15)) = (K.add (K.mul (co K X (12)) (co K X (14))) (K.neg (K.mul (co K X (11)) (co K X (15))))) := rfl
  have t15 : O.le K.zero (VGoodTail.tt K X (10) (17)) := VGoodTail.tail_nn K O X hg (10) 6
  have et15 : (VGoodTail.tt K X (10) (17)) = (K.add (K.mul (co K X (10)) (co K X (16))) (K.neg (K.mul (co K X (9)) (co K X (17))))) := rfl
  have t16 : O.le K.zero (VGoodTail.tt K X (8) (17)) := VGoodTail.tail_nn K O X hg (8) 8
  have et16 : (VGoodTail.tt K X (8) (17)) = (K.add (K.mul (co K X (8)) (co K X (16))) (K.neg (K.mul (co K X (7)) (co K X (17))))) := rfl
  have t17 : O.le K.zero (VGoodTail.tt K X (8) (11)) := VGoodTail.tail_nn K O X hg (8) 2
  have et17 : (VGoodTail.tt K X (8) (11)) = (K.add (K.mul (co K X (8)) (co K X (10))) (K.neg (K.mul (co K X (7)) (co K X (11))))) := rfl
  have t18 : O.le K.zero (VGoodTail.tt K X (6) (17)) := VGoodTail.tail_nn K O X hg (6) 10
  have et18 : (VGoodTail.tt K X (6) (17)) = (K.add (K.mul (co K X (6)) (co K X (16))) (K.neg (K.mul (co K X (5)) (co K X (17))))) := rfl
  have t19 : O.le K.zero (VGoodTail.tt K X (6) (15)) := VGoodTail.tail_nn K O X hg (6) 8
  have et19 : (VGoodTail.tt K X (6) (15)) = (K.add (K.mul (co K X (6)) (co K X (14))) (K.neg (K.mul (co K X (5)) (co K X (15))))) := rfl
  have t20 : O.le K.zero (VGoodTail.tt K X (6) (13)) := VGoodTail.tail_nn K O X hg (6) 6
  have et20 : (VGoodTail.tt K X (6) (13)) = (K.add (K.mul (co K X (6)) (co K X (12))) (K.neg (K.mul (co K X (5)) (co K X (13))))) := rfl
  have t21 : O.le K.zero (VGoodTail.tt K X (6) (11)) := VGoodTail.tail_nn K O X hg (6) 4
  have et21 : (VGoodTail.tt K X (6) (11)) = (K.add (K.mul (co K X (6)) (co K X (10))) (K.neg (K.mul (co K X (5)) (co K X (11))))) := rfl
  have t22 : O.le K.zero (VGoodTail.tt K X (13) (18)) := VGoodTail.tail_nn K O X hg (13) 4
  have et22 : (VGoodTail.tt K X (13) (18)) = (K.add (K.mul (co K X (13)) (co K X (17))) (K.neg (K.mul (co K X (12)) (co K X (18))))) := rfl
  have t23 : O.le K.zero (VGoodTail.tt K X (13) (16)) := VGoodTail.tail_nn K O X hg (13) 2
  have et23 : (VGoodTail.tt K X (13) (16)) = (K.add (K.mul (co K X (13)) (co K X (15))) (K.neg (K.mul (co K X (12)) (co K X (16))))) := rfl
  have t24 : O.le K.zero (VGoodTail.tt K X (13) (14)) := VGoodTail.tail_nn K O X hg (13) 0
  have et24 : (VGoodTail.tt K X (13) (14)) = (K.add (K.mul (co K X (13)) (co K X (13))) (K.neg (K.mul (co K X (12)) (co K X (14))))) := rfl
  have t25 : O.le K.zero (VGoodTail.tt K X (11) (18)) := VGoodTail.tail_nn K O X hg (11) 6
  have et25 : (VGoodTail.tt K X (11) (18)) = (K.add (K.mul (co K X (11)) (co K X (17))) (K.neg (K.mul (co K X (10)) (co K X (18))))) := rfl
  have t26 : O.le K.zero (VGoodTail.tt K X (9) (18)) := VGoodTail.tail_nn K O X hg (9) 8
  have et26 : (VGoodTail.tt K X (9) (18)) = (K.add (K.mul (co K X (9)) (co K X (17))) (K.neg (K.mul (co K X (8)) (co K X (18))))) := rfl
  have t27 : O.le K.zero (VGoodTail.tt K X (9) (10)) := VGoodTail.tail_nn K O X hg (9) 0
  have et27 : (VGoodTail.tt K X (9) (10)) = (K.add (K.mul (co K X (9)) (co K X (9))) (K.neg (K.mul (co K X (8)) (co K X (10))))) := rfl
  have t28 : O.le K.zero (VGoodTail.tt K X (7) (18)) := VGoodTail.tail_nn K O X hg (7) 10
  have et28 : (VGoodTail.tt K X (7) (18)) = (K.add (K.mul (co K X (7)) (co K X (17))) (K.neg (K.mul (co K X (6)) (co K X (18))))) := rfl
  have t29 : O.le K.zero (VGoodTail.tt K X (7) (10)) := VGoodTail.tail_nn K O X hg (7) 2
  have et29 : (VGoodTail.tt K X (7) (10)) = (K.add (K.mul (co K X (7)) (co K X (9))) (K.neg (K.mul (co K X (6)) (co K X (10))))) := rfl
  have hp : 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.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 (VGoodTail.tt K X (9) (14)) (VGoodTail.tt K X (9) (14))) (VGoodTail.tt K X (9) (14))) (VG  -- (+1148 chars; full line in the Zenodo source)
    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 _ _ (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 _ _ (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   -- (+701 chars; full line in the Zenodo source)
  refine O.le_trans _ _ _ hp (TripleE.le_of_eq' K O _ _ ?_)
  rw [kv, et0, et1, et2, et3, et4, et5, et6, et7, et8, et9, et10, et11, et12, et13, et14, et15, et16, et17, et18, et19, et20, et21, et22, et23, et24, et25, et26, et27, et28, et29]
  ring_z

theorem kna_3_5 (X : List R) (hg : L1Full.Good K O X) :
    O.le K.zero (CBLib.kern K (Fa K X) (Ga K X) 5 (3) (9)) := by
  have kv : CBLib.kern K (Fa K X) (Ga K X) 5 (3) (9) = (K.add (K.add (K.add (K.add (K.add K.zero (K.add (K.mul (co K X (9)) (co K X (14))) (K.neg (K.mul (co K X (15)) (co K X (8)))))) (K.add (K.mul (K.add (co K X (10)) (co K X (8))) (K.add (co K X (15)) (co K X (13)))) (K.neg (K.mul (K.add (co K X (16)) (co K X (14))) (K.add (co K X (9)) (co K X (7))))))) (K.add (K.mul (K.add (K.add (co K X (11))   -- (+966 chars; full line in the Zenodo source)
  have t0 : O.le K.zero (VGoodTail.tt K X (9) (15)) := VGoodTail.tail_nn K O X hg (9) 5
  have et0 : (VGoodTail.tt K X (9) (15)) = (K.add (K.mul (co K X (9)) (co K X (14))) (K.neg (K.mul (co K X (8)) (co K X (15))))) := rfl
  have t1 : O.le K.zero (VGoodTail.tt K X (10) (16)) := VGoodTail.tail_nn K O X hg (10) 5
  have et1 : (VGoodTail.tt K X (10) (16)) = (K.add (K.mul (co K X (10)) (co K X (15))) (K.neg (K.mul (co K X (9)) (co K X (16))))) := rfl
  have t2 : O.le K.zero (VGoodTail.tt K X (10) (14)) := VGoodTail.tail_nn K O X hg (10) 3
  have et2 : (VGoodTail.tt K X (10) (14)) = (K.add (K.mul (co K X (10)) (co K X (13))) (K.neg (K.mul (co K X (9)) (co K X (14))))) := rfl
  have t3 : O.le K.zero (VGoodTail.tt K X (8) (16)) := VGoodTail.tail_nn K O X hg (8) 7
  have et3 : (VGoodTail.tt K X (8) (16)) = (K.add (K.mul (co K X (8)) (co K X (15))) (K.neg (K.mul (co K X (7)) (co K X (16))))) := rfl
  have t4 : O.le K.zero (VGoodTail.tt K X (8) (14)) := VGoodTail.tail_nn K O X hg (8) 5
  have et4 : (VGoodTail.tt K X (8) (14)) = (K.add (K.mul (co K X (8)) (co K X (13))) (K.neg (K.mul (co K X (7)) (co K X (14))))) := rfl
  have t5 : O.le K.zero (VGoodTail.tt K X (11) (17)) := VGoodTail.tail_nn K O X hg (11) 5
  have et5 : (VGoodTail.tt K X (11) (17)) = (K.add (K.mul (co K X (11)) (co K X (16))) (K.neg (K.mul (co K X (10)) (co K X (17))))) := rfl
  have t6 : O.le K.zero (VGoodTail.tt K X (11) (15)) := VGoodTail.tail_nn K O X hg (11) 3
  have et6 : (VGoodTail.tt K X (11) (15)) = (K.add (K.mul (co K X (11)) (co K X (14))) (K.neg (K.mul (co K X (10)) (co K X (15))))) := rfl
  have t7 : O.le K.zero (VGoodTail.tt K X (11) (13)) := VGoodTail.tail_nn K O X hg (11) 1
  have et7 : (VGoodTail.tt K X (11) (13)) = (K.add (K.mul (co K X (11)) (co K X (12))) (K.neg (K.mul (co K X (10)) (co K X (13))))) := rfl
  have t8 : O.le K.zero (VGoodTail.tt K X (9) (17)) := VGoodTail.tail_nn K O X hg (9) 7
  have et8 : (VGoodTail.tt K X (9) (17)) = (K.add (K.mul (co K X (9)) (co K X (16))) (K.neg (K.mul (co K X (8)) (co K X (17))))) := rfl
  have t9 : O.le K.zero (VGoodTail.tt K X (9) (13)) := VGoodTail.tail_nn K O X hg (9) 3
  have et9 : (VGoodTail.tt K X (9) (13)) = (K.add (K.mul (co K X (9)) (co K X (12))) (K.neg (K.mul (co K X (8)) (co K X (13))))) := rfl
  have t10 : O.le K.zero (VGoodTail.tt K X (7) (17)) := VGoodTail.tail_nn K O X hg (7) 9
  have et10 : (VGoodTail.tt K X (7) (17)) = (K.add (K.mul (co K X (7)) (co K X (16))) (K.neg (K.mul (co K X (6)) (co K X (17))))) := rfl
  have t11 : O.le K.zero (VGoodTail.tt K X (7) (15)) := VGoodTail.tail_nn K O X hg (7) 7
  have et11 : (VGoodTail.tt K X (7) (15)) = (K.add (K.mul (co K X (7)) (co K X (14))) (K.neg (K.mul (co K X (6)) (co K X (15))))) := rfl
  have t12 : O.le K.zero (VGoodTail.tt K X (7) (13)) := VGoodTail.tail_nn K O X hg (7) 5
  have et12 : (VGoodTail.tt K X (7) (13)) = (K.add (K.mul (co K X (7)) (co K X (12))) (K.neg (K.mul (co K X (6)) (co K X (13))))) := rfl
  have t13 : O.le K.zero (VGoodTail.tt K X (12) (18)) := VGoodTail.tail_nn K O X hg (12) 5
  have et13 : (VGoodTail.tt K X (12) (18)) = (K.add (K.mul (co K X (12)) (co K X (17))) (K.neg (K.mul (co K X (11)) (co K X (18))))) := rfl
  have t14 : O.le K.zero (VGoodTail.tt K X (12) (16)) := VGoodTail.tail_nn K O X hg (12) 3
  have et14 : (VGoodTail.tt K X (12) (16)) = (K.add (K.mul (co K X (12)) (co K X (15))) (K.neg (K.mul (co K X (11)) (co K X (16))))) := rfl
  have t15 : O.le K.zero (VGoodTail.tt K X (12) (14)) := VGoodTail.tail_nn K O X hg (12) 1
  have et15 : (VGoodTail.tt K X (12) (14)) = (K.add (K.mul (co K X (12)) (co K X (13))) (K.neg (K.mul (co K X (11)) (co K X (14))))) := rfl
  have t16 : O.le K.zero (VGoodTail.tt K X (10) (18)) := VGoodTail.tail_nn K O X hg (10) 7
  have et16 : (VGoodTail.tt K X (10) (18)) = (K.add (K.mul (co K X (10)) (co K X (17))) (K.neg (K.mul (co K X (9)) (co K X (18))))) := rfl
  have t17 : O.le K.zero (VGoodTail.tt K X (10) (12)) := VGoodTail.tail_nn K O X hg (10) 1
  have et17 : (VGoodTail.tt K X (10) (12)) = (K.add (K.mul (co K X (10)) (co K X (11))) (K.neg (K.mul (co K X (9)) (co K X (12))))) := rfl
  have t18 : O.le K.zero (VGoodTail.tt K X (8) (18)) := VGoodTail.tail_nn K O X hg (8) 9
  have et18 : (VGoodTail.tt K X (8) (18)) = (K.add (K.mul (co K X (8)) (co K X (17))) (K.neg (K.mul (co K X (7)) (co K X (18))))) := rfl
  have t19 : O.le K.zero (VGoodTail.tt K X (8) (12)) := VGoodTail.tail_nn K O X hg (8) 3
  have et19 : (VGoodTail.tt K X (8) (12)) = (K.add (K.mul (co K X (8)) (co K X (11))) (K.neg (K.mul (co K X (7)) (co K X (12))))) := rfl
  have t20 : O.le K.zero (VGoodTail.tt K X (6) (18)) := VGoodTail.tail_nn K O X hg (6) 11
  have et20 : (VGoodTail.tt K X (6) (18)) = (K.add (K.mul (co K X (6)) (co K X (17))) (K.neg (K.mul (co K X (5)) (co K X (18))))) := rfl
  have t21 : O.le K.zero (VGoodTail.tt K X (6) (16)) := VGoodTail.tail_nn K O X hg (6) 9
  have et21 : (VGoodTail.tt K X (6) (16)) = (K.add (K.mul (co K X (6)) (co K X (15))) (K.neg (K.mul (co K X (5)) (co K X (16))))) := rfl
  have t22 : O.le K.zero (VGoodTail.tt K X (6) (14)) := VGoodTail.tail_nn K O X hg (6) 7
  have et22 : (VGoodTail.tt K X (6) (14)) = (K.add (K.mul (co K X (6)) (co K X (13))) (K.neg (K.mul (co K X (5)) (co K X (14))))) := rfl
  have t23 : O.le K.zero (VGoodTail.tt K X (6) (12)) := VGoodTail.tail_nn K O X hg (6) 5
  have et23 : (VGoodTail.tt K X (6) (12)) = (K.add (K.mul (co K X (6)) (co K X (11))) (K.neg (K.mul (co K X (5)) (co K X (12))))) := rfl
  have t24 : O.le K.zero (VGoodTail.tt K X (13) (19)) := VGoodTail.tail_nn K O X hg (13) 5
  have et24 : (VGoodTail.tt K X (13) (19)) = (K.add (K.mul (co K X (13)) (co K X (18))) (K.neg (K.mul (co K X (12)) (co K X (19))))) := rfl
  have t25 : O.le K.zero (VGoodTail.tt K X (13) (17)) := VGoodTail.tail_nn K O X hg (13) 3
  have et25 : (VGoodTail.tt K X (13) (17)) = (K.add (K.mul (co K X (13)) (co K X (16))) (K.neg (K.mul (co K X (12)) (co K X (17))))) := rfl
  have t26 : O.le K.zero (VGoodTail.tt K X (13) (15)) := VGoodTail.tail_nn K O X hg (13) 1
  have et26 : (VGoodTail.tt K X (13) (15)) = (K.add (K.mul (co K X (13)) (co K X (14))) (K.neg (K.mul (co K X (12)) (co K X (15))))) := rfl
  have t27 : O.le K.zero (VGoodTail.tt K X (11) (19)) := VGoodTail.tail_nn K O X hg (11) 7
  have et27 : (VGoodTail.tt K X (11) (19)) = (K.add (K.mul (co K X (11)) (co K X (18))) (K.neg (K.mul (co K X (10)) (co K X (19))))) := rfl
  have t28 : O.le K.zero (VGoodTail.tt K X (9) (19)) := VGoodTail.tail_nn K O X hg (9) 9
  have et28 : (VGoodTail.tt K X (9) (19)) = (K.add (K.mul (co K X (9)) (co K X (18))) (K.neg (K.mul (co K X (8)) (co K X (19))))) := rfl
  have t29 : O.le K.zero (VGoodTail.tt K X (9) (11)) := VGoodTail.tail_nn K O X hg (9) 1
  have et29 : (VGoodTail.tt K X (9) (11)) = (K.add (K.mul (co K X (9)) (co K X (10))) (K.neg (K.mul (co K X (8)) (co K X (11))))) := rfl
  have t30 : O.le K.zero (VGoodTail.tt K X (7) (19)) := VGoodTail.tail_nn K O X hg (7) 11
  have et30 : (VGoodTail.tt K X (7) (19)) = (K.add (K.mul (co K X (7)) (co K X (18))) (K.neg (K.mul (co K X (6)) (co K X (19))))) := rfl
  have t31 : O.le K.zero (VGoodTail.tt K X (7) (11)) := VGoodTail.tail_nn K O X hg (7) 3
  have et31 : (VGoodTail.tt K X (7) (11)) = (K.add (K.mul (co K X (7)) (co K X (10))) (K.neg (K.mul (co K X (6)) (co K X (11))))) := rfl
  have hp : 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.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.add (K.add (K.add (VGoodTail.tt K X (9) (15)) (VGoodTail.tt K X (9) (15))) (VGoodTail.  -- (+1259 chars; full line in the Zenodo source)
    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 _ _ (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 _ _ (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   -- (+781 chars; full line in the Zenodo source)
  refine O.le_trans _ _ _ hp (TripleE.le_of_eq' K O _ _ ?_)
  rw [kv, et0, et1, et2, et3, et4, et5, et6, et7, et8, et9, et10, et11, et12, et13, et14, et15, et16, et17, et18, et19, et20, et21, et22, et23, et24, et25, et26, et27, et28, et29, et30, et31]
  ring_z

theorem kna_3_6 (X : List R) (hg : L1Full.Good K O X) :
    O.le K.zero (CBLib.kern K (Fa K X) (Ga K X) 5 (3) (10)) := by
  have kv : CBLib.kern K (Fa K X) (Ga K X) 5 (3) (10) = (K.add (K.add (K.add (K.add (K.add K.zero (K.add (K.mul (co K X (9)) (co K X (15))) (K.neg (K.mul (co K X (16)) (co K X (8)))))) (K.add (K.mul (K.add (co K X (10)) (co K X (8))) (K.add (co K X (16)) (co K X (14)))) (K.neg (K.mul (K.add (co K X (17)) (co K X (15))) (K.add (co K X (9)) (co K X (7))))))) (K.add (K.mul (K.add (K.add (co K X (11))  -- (+967 chars; full line in the Zenodo source)
  have t0 : O.le K.zero (VGoodTail.tt K X (9) (16)) := VGoodTail.tail_nn K O X hg (9) 6
  have et0 : (VGoodTail.tt K X (9) (16)) = (K.add (K.mul (co K X (9)) (co K X (15))) (K.neg (K.mul (co K X (8)) (co K X (16))))) := rfl
  have t1 : O.le K.zero (VGoodTail.tt K X (10) (17)) := VGoodTail.tail_nn K O X hg (10) 6
  have et1 : (VGoodTail.tt K X (10) (17)) = (K.add (K.mul (co K X (10)) (co K X (16))) (K.neg (K.mul (co K X (9)) (co K X (17))))) := rfl
  have t2 : O.le K.zero (VGoodTail.tt K X (10) (15)) := VGoodTail.tail_nn K O X hg (10) 4
  have et2 : (VGoodTail.tt K X (10) (15)) = (K.add (K.mul (co K X (10)) (co K X (14))) (K.neg (K.mul (co K X (9)) (co K X (15))))) := rfl
  have t3 : O.le K.zero (VGoodTail.tt K X (8) (17)) := VGoodTail.tail_nn K O X hg (8) 8
  have et3 : (VGoodTail.tt K X (8) (17)) = (K.add (K.mul (co K X (8)) (co K X (16))) (K.neg (K.mul (co K X (7)) (co K X (17))))) := rfl
  have t4 : O.le K.zero (VGoodTail.tt K X (8) (15)) := VGoodTail.tail_nn K O X hg (8) 6
  have et4 : (VGoodTail.tt K X (8) (15)) = (K.add (K.mul (co K X (8)) (co K X (14))) (K.neg (K.mul (co K X (7)) (co K X (15))))) := rfl
  have t5 : O.le K.zero (VGoodTail.tt K X (11) (18)) := VGoodTail.tail_nn K O X hg (11) 6
  have et5 : (VGoodTail.tt K X (11) (18)) = (K.add (K.mul (co K X (11)) (co K X (17))) (K.neg (K.mul (co K X (10)) (co K X (18))))) := rfl
  have t6 : O.le K.zero (VGoodTail.tt K X (11) (16)) := VGoodTail.tail_nn K O X hg (11) 4
  have et6 : (VGoodTail.tt K X (11) (16)) = (K.add (K.mul (co K X (11)) (co K X (15))) (K.neg (K.mul (co K X (10)) (co K X (16))))) := rfl
  have t7 : O.le K.zero (VGoodTail.tt K X (11) (14)) := VGoodTail.tail_nn K O X hg (11) 2
  have et7 : (VGoodTail.tt K X (11) (14)) = (K.add (K.mul (co K X (11)) (co K X (13))) (K.neg (K.mul (co K X (10)) (co K X (14))))) := rfl
  have t8 : O.le K.zero (VGoodTail.tt K X (9) (18)) := VGoodTail.tail_nn K O X hg (9) 8
  have et8 : (VGoodTail.tt K X (9) (18)) = (K.add (K.mul (co K X (9)) (co K X (17))) (K.neg (K.mul (co K X (8)) (co K X (18))))) := rfl
  have t9 : O.le K.zero (VGoodTail.tt K X (9) (14)) := VGoodTail.tail_nn K O X hg (9) 4
  have et9 : (VGoodTail.tt K X (9) (14)) = (K.add (K.mul (co K X (9)) (co K X (13))) (K.neg (K.mul (co K X (8)) (co K X (14))))) := rfl
  have t10 : O.le K.zero (VGoodTail.tt K X (7) (18)) := VGoodTail.tail_nn K O X hg (7) 10
  have et10 : (VGoodTail.tt K X (7) (18)) = (K.add (K.mul (co K X (7)) (co K X (17))) (K.neg (K.mul (co K X (6)) (co K X (18))))) := rfl
  have t11 : O.le K.zero (VGoodTail.tt K X (7) (16)) := VGoodTail.tail_nn K O X hg (7) 8
  have et11 : (VGoodTail.tt K X (7) (16)) = (K.add (K.mul (co K X (7)) (co K X (15))) (K.neg (K.mul (co K X (6)) (co K X (16))))) := rfl
  have t12 : O.le K.zero (VGoodTail.tt K X (7) (14)) := VGoodTail.tail_nn K O X hg (7) 6
  have et12 : (VGoodTail.tt K X (7) (14)) = (K.add (K.mul (co K X (7)) (co K X (13))) (K.neg (K.mul (co K X (6)) (co K X (14))))) := rfl
  have t13 : O.le K.zero (VGoodTail.tt K X (12) (19)) := VGoodTail.tail_nn K O X hg (12) 6
  have et13 : (VGoodTail.tt K X (12) (19)) = (K.add (K.mul (co K X (12)) (co K X (18))) (K.neg (K.mul (co K X (11)) (co K X (19))))) := rfl
  have t14 : O.le K.zero (VGoodTail.tt K X (12) (17)) := VGoodTail.tail_nn K O X hg (12) 4
  have et14 : (VGoodTail.tt K X (12) (17)) = (K.add (K.mul (co K X (12)) (co K X (16))) (K.neg (K.mul (co K X (11)) (co K X (17))))) := rfl
  have t15 : O.le K.zero (VGoodTail.tt K X (12) (15)) := VGoodTail.tail_nn K O X hg (12) 2
  have et15 : (VGoodTail.tt K X (12) (15)) = (K.add (K.mul (co K X (12)) (co K X (14))) (K.neg (K.mul (co K X (11)) (co K X (15))))) := rfl
  have t16 : O.le K.zero (VGoodTail.tt K X (10) (19)) := VGoodTail.tail_nn K O X hg (10) 8
  have et16 : (VGoodTail.tt K X (10) (19)) = (K.add (K.mul (co K X (10)) (co K X (18))) (K.neg (K.mul (co K X (9)) (co K X (19))))) := rfl
  have t17 : O.le K.zero (VGoodTail.tt K X (10) (13)) := VGoodTail.tail_nn K O X hg (10) 2
  have et17 : (VGoodTail.tt K X (10) (13)) = (K.add (K.mul (co K X (10)) (co K X (12))) (K.neg (K.mul (co K X (9)) (co K X (13))))) := rfl
  have t18 : O.le K.zero (VGoodTail.tt K X (8) (19)) := VGoodTail.tail_nn K O X hg (8) 10
  have et18 : (VGoodTail.tt K X (8) (19)) = (K.add (K.mul (co K X (8)) (co K X (18))) (K.neg (K.mul (co K X (7)) (co K X (19))))) := rfl
  have t19 : O.le K.zero (VGoodTail.tt K X (8) (13)) := VGoodTail.tail_nn K O X hg (8) 4
  have et19 : (VGoodTail.tt K X (8) (13)) = (K.add (K.mul (co K X (8)) (co K X (12))) (K.neg (K.mul (co K X (7)) (co K X (13))))) := rfl
  have t20 : O.le K.zero (VGoodTail.tt K X (6) (19)) := VGoodTail.tail_nn K O X hg (6) 12
  have et20 : (VGoodTail.tt K X (6) (19)) = (K.add (K.mul (co K X (6)) (co K X (18))) (K.neg (K.mul (co K X (5)) (co K X (19))))) := rfl
  have t21 : O.le K.zero (VGoodTail.tt K X (6) (17)) := VGoodTail.tail_nn K O X hg (6) 10
  have et21 : (VGoodTail.tt K X (6) (17)) = (K.add (K.mul (co K X (6)) (co K X (16))) (K.neg (K.mul (co K X (5)) (co K X (17))))) := rfl
  have t22 : O.le K.zero (VGoodTail.tt K X (6) (15)) := VGoodTail.tail_nn K O X hg (6) 8
  have et22 : (VGoodTail.tt K X (6) (15)) = (K.add (K.mul (co K X (6)) (co K X (14))) (K.neg (K.mul (co K X (5)) (co K X (15))))) := rfl
  have t23 : O.le K.zero (VGoodTail.tt K X (6) (13)) := VGoodTail.tail_nn K O X hg (6) 6
  have et23 : (VGoodTail.tt K X (6) (13)) = (K.add (K.mul (co K X (6)) (co K X (12))) (K.neg (K.mul (co K X (5)) (co K X (13))))) := rfl
  have t24 : O.le K.zero (VGoodTail.tt K X (13) (20)) := VGoodTail.tail_nn K O X hg (13) 6
  have et24 : (VGoodTail.tt K X (13) (20)) = (K.add (K.mul (co K X (13)) (co K X (19))) (K.neg (K.mul (co K X (12)) (co K X (20))))) := rfl
  have t25 : O.le K.zero (VGoodTail.tt K X (13) (18)) := VGoodTail.tail_nn K O X hg (13) 4
  have et25 : (VGoodTail.tt K X (13) (18)) = (K.add (K.mul (co K X (13)) (co K X (17))) (K.neg (K.mul (co K X (12)) (co K X (18))))) := rfl
  have t26 : O.le K.zero (VGoodTail.tt K X (13) (16)) := VGoodTail.tail_nn K O X hg (13) 2
  have et26 : (VGoodTail.tt K X (13) (16)) = (K.add (K.mul (co K X (13)) (co K X (15))) (K.neg (K.mul (co K X (12)) (co K X (16))))) := rfl
  have t27 : O.le K.zero (VGoodTail.tt K X (13) (14)) := VGoodTail.tail_nn K O X hg (13) 0
  have et27 : (VGoodTail.tt K X (13) (14)) = (K.add (K.mul (co K X (13)) (co K X (13))) (K.neg (K.mul (co K X (12)) (co K X (14))))) := rfl
  have t28 : O.le K.zero (VGoodTail.tt K X (11) (20)) := VGoodTail.tail_nn K O X hg (11) 8
  have et28 : (VGoodTail.tt K X (11) (20)) = (K.add (K.mul (co K X (11)) (co K X (19))) (K.neg (K.mul (co K X (10)) (co K X (20))))) := rfl
  have t29 : O.le K.zero (VGoodTail.tt K X (11) (12)) := VGoodTail.tail_nn K O X hg (11) 0
  have et29 : (VGoodTail.tt K X (11) (12)) = (K.add (K.mul (co K X (11)) (co K X (11))) (K.neg (K.mul (co K X (10)) (co K X (12))))) := rfl
  have t30 : O.le K.zero (VGoodTail.tt K X (9) (20)) := VGoodTail.tail_nn K O X hg (9) 10
  have et30 : (VGoodTail.tt K X (9) (20)) = (K.add (K.mul (co K X (9)) (co K X (19))) (K.neg (K.mul (co K X (8)) (co K X (20))))) := rfl
  have t31 : O.le K.zero (VGoodTail.tt K X (9) (12)) := VGoodTail.tail_nn K O X hg (9) 2
  have et31 : (VGoodTail.tt K X (9) (12)) = (K.add (K.mul (co K X (9)) (co K X (11))) (K.neg (K.mul (co K X (8)) (co K X (12))))) := rfl
  have t32 : O.le K.zero (VGoodTail.tt K X (7) (20)) := VGoodTail.tail_nn K O X hg (7) 12
  have et32 : (VGoodTail.tt K X (7) (20)) = (K.add (K.mul (co K X (7)) (co K X (19))) (K.neg (K.mul (co K X (6)) (co K X (20))))) := rfl
  have t33 : O.le K.zero (VGoodTail.tt K X (7) (12)) := VGoodTail.tail_nn K O X hg (7) 4
  have et33 : (VGoodTail.tt K X (7) (12)) = (K.add (K.mul (co K X (7)) (co K X (11))) (K.neg (K.mul (co K X (6)) (co K X (12))))) := rfl
  have hp : 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.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.add (K.add (K.add (K.add (K.add (K.add (VGoodTail.tt K X (9) (16)) (VGoodTail.tt K X (  -- (+1370 chars; full line in the Zenodo source)
    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 _ _ (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 _ _ (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   -- (+861 chars; full line in the Zenodo source)
  refine O.le_trans _ _ _ hp (TripleE.le_of_eq' K O _ _ ?_)
  rw [kv, et0, et1, et2, et3, et4, et5, et6, et7, et8, et9, et10, et11, et12, et13, et14, et15, et16, et17, et18, et19, et20, et21, et22, et23, et24, et25, et26, et27, et28, et29, et30, et31, et32, et33]
  ring_z

theorem kna_3_7 (X : List R) (hg : L1Full.Good K O X) :
    O.le K.zero (CBLib.kern K (Fa K X) (Ga K X) 5 (3) (11)) := by
  have kv : CBLib.kern K (Fa K X) (Ga K X) 5 (3) (11) = (K.add (K.add (K.add (K.add (K.add K.zero (K.add (K.mul (co K X (9)) (co K X (16))) (K.neg (K.mul (co K X (17)) (co K X (8)))))) (K.add (K.mul (K.add (co K X (10)) (co K X (8))) (K.add (co K X (17)) (co K X (15)))) (K.neg (K.mul (K.add (co K X (18)) (co K X (16))) (K.add (co K X (9)) (co K X (7))))))) (K.add (K.mul (K.add (K.add (co K X (11))  -- (+967 chars; full line in the Zenodo source)
  have t0 : O.le K.zero (VGoodTail.tt K X (9) (17)) := VGoodTail.tail_nn K O X hg (9) 7
  have et0 : (VGoodTail.tt K X (9) (17)) = (K.add (K.mul (co K X (9)) (co K X (16))) (K.neg (K.mul (co K X (8)) (co K X (17))))) := rfl
  have t1 : O.le K.zero (VGoodTail.tt K X (10) (18)) := VGoodTail.tail_nn K O X hg (10) 7
  have et1 : (VGoodTail.tt K X (10) (18)) = (K.add (K.mul (co K X (10)) (co K X (17))) (K.neg (K.mul (co K X (9)) (co K X (18))))) := rfl
  have t2 : O.le K.zero (VGoodTail.tt K X (10) (16)) := VGoodTail.tail_nn K O X hg (10) 5
  have et2 : (VGoodTail.tt K X (10) (16)) = (K.add (K.mul (co K X (10)) (co K X (15))) (K.neg (K.mul (co K X (9)) (co K X (16))))) := rfl
  have t3 : O.le K.zero (VGoodTail.tt K X (8) (18)) := VGoodTail.tail_nn K O X hg (8) 9
  have et3 : (VGoodTail.tt K X (8) (18)) = (K.add (K.mul (co K X (8)) (co K X (17))) (K.neg (K.mul (co K X (7)) (co K X (18))))) := rfl
  have t4 : O.le K.zero (VGoodTail.tt K X (8) (16)) := VGoodTail.tail_nn K O X hg (8) 7
  have et4 : (VGoodTail.tt K X (8) (16)) = (K.add (K.mul (co K X (8)) (co K X (15))) (K.neg (K.mul (co K X (7)) (co K X (16))))) := rfl
  have t5 : O.le K.zero (VGoodTail.tt K X (11) (19)) := VGoodTail.tail_nn K O X hg (11) 7
  have et5 : (VGoodTail.tt K X (11) (19)) = (K.add (K.mul (co K X (11)) (co K X (18))) (K.neg (K.mul (co K X (10)) (co K X (19))))) := rfl
  have t6 : O.le K.zero (VGoodTail.tt K X (11) (17)) := VGoodTail.tail_nn K O X hg (11) 5
  have et6 : (VGoodTail.tt K X (11) (17)) = (K.add (K.mul (co K X (11)) (co K X (16))) (K.neg (K.mul (co K X (10)) (co K X (17))))) := rfl
  have t7 : O.le K.zero (VGoodTail.tt K X (11) (15)) := VGoodTail.tail_nn K O X hg (11) 3
  have et7 : (VGoodTail.tt K X (11) (15)) = (K.add (K.mul (co K X (11)) (co K X (14))) (K.neg (K.mul (co K X (10)) (co K X (15))))) := rfl
  have t8 : O.le K.zero (VGoodTail.tt K X (9) (19)) := VGoodTail.tail_nn K O X hg (9) 9
  have et8 : (VGoodTail.tt K X (9) (19)) = (K.add (K.mul (co K X (9)) (co K X (18))) (K.neg (K.mul (co K X (8)) (co K X (19))))) := rfl
  have t9 : O.le K.zero (VGoodTail.tt K X (9) (15)) := VGoodTail.tail_nn K O X hg (9) 5
  have et9 : (VGoodTail.tt K X (9) (15)) = (K.add (K.mul (co K X (9)) (co K X (14))) (K.neg (K.mul (co K X (8)) (co K X (15))))) := rfl
  have t10 : O.le K.zero (VGoodTail.tt K X (7) (19)) := VGoodTail.tail_nn K O X hg (7) 11
  have et10 : (VGoodTail.tt K X (7) (19)) = (K.add (K.mul (co K X (7)) (co K X (18))) (K.neg (K.mul (co K X (6)) (co K X (19))))) := rfl
  have t11 : O.le K.zero (VGoodTail.tt K X (7) (17)) := VGoodTail.tail_nn K O X hg (7) 9
  have et11 : (VGoodTail.tt K X (7) (17)) = (K.add (K.mul (co K X (7)) (co K X (16))) (K.neg (K.mul (co K X (6)) (co K X (17))))) := rfl
  have t12 : O.le K.zero (VGoodTail.tt K X (7) (15)) := VGoodTail.tail_nn K O X hg (7) 7
  have et12 : (VGoodTail.tt K X (7) (15)) = (K.add (K.mul (co K X (7)) (co K X (14))) (K.neg (K.mul (co K X (6)) (co K X (15))))) := rfl
  have t13 : O.le K.zero (VGoodTail.tt K X (12) (20)) := VGoodTail.tail_nn K O X hg (12) 7
  have et13 : (VGoodTail.tt K X (12) (20)) = (K.add (K.mul (co K X (12)) (co K X (19))) (K.neg (K.mul (co K X (11)) (co K X (20))))) := rfl
  have t14 : O.le K.zero (VGoodTail.tt K X (12) (18)) := VGoodTail.tail_nn K O X hg (12) 5
  have et14 : (VGoodTail.tt K X (12) (18)) = (K.add (K.mul (co K X (12)) (co K X (17))) (K.neg (K.mul (co K X (11)) (co K X (18))))) := rfl
  have t15 : O.le K.zero (VGoodTail.tt K X (12) (16)) := VGoodTail.tail_nn K O X hg (12) 3
  have et15 : (VGoodTail.tt K X (12) (16)) = (K.add (K.mul (co K X (12)) (co K X (15))) (K.neg (K.mul (co K X (11)) (co K X (16))))) := rfl
  have t16 : O.le K.zero (VGoodTail.tt K X (12) (14)) := VGoodTail.tail_nn K O X hg (12) 1
  have et16 : (VGoodTail.tt K X (12) (14)) = (K.add (K.mul (co K X (12)) (co K X (13))) (K.neg (K.mul (co K X (11)) (co K X (14))))) := rfl
  have t17 : O.le K.zero (VGoodTail.tt K X (10) (20)) := VGoodTail.tail_nn K O X hg (10) 9
  have et17 : (VGoodTail.tt K X (10) (20)) = (K.add (K.mul (co K X (10)) (co K X (19))) (K.neg (K.mul (co K X (9)) (co K X (20))))) := rfl
  have t18 : O.le K.zero (VGoodTail.tt K X (10) (14)) := VGoodTail.tail_nn K O X hg (10) 3
  have et18 : (VGoodTail.tt K X (10) (14)) = (K.add (K.mul (co K X (10)) (co K X (13))) (K.neg (K.mul (co K X (9)) (co K X (14))))) := rfl
  have t19 : O.le K.zero (VGoodTail.tt K X (8) (20)) := VGoodTail.tail_nn K O X hg (8) 11
  have et19 : (VGoodTail.tt K X (8) (20)) = (K.add (K.mul (co K X (8)) (co K X (19))) (K.neg (K.mul (co K X (7)) (co K X (20))))) := rfl
  have t20 : O.le K.zero (VGoodTail.tt K X (8) (14)) := VGoodTail.tail_nn K O X hg (8) 5
  have et20 : (VGoodTail.tt K X (8) (14)) = (K.add (K.mul (co K X (8)) (co K X (13))) (K.neg (K.mul (co K X (7)) (co K X (14))))) := rfl
  have t21 : O.le K.zero (VGoodTail.tt K X (6) (20)) := VGoodTail.tail_nn K O X hg (6) 13
  have et21 : (VGoodTail.tt K X (6) (20)) = (K.add (K.mul (co K X (6)) (co K X (19))) (K.neg (K.mul (co K X (5)) (co K X (20))))) := rfl
  have t22 : O.le K.zero (VGoodTail.tt K X (6) (18)) := VGoodTail.tail_nn K O X hg (6) 11
  have et22 : (VGoodTail.tt K X (6) (18)) = (K.add (K.mul (co K X (6)) (co K X (17))) (K.neg (K.mul (co K X (5)) (co K X (18))))) := rfl
  have t23 : O.le K.zero (VGoodTail.tt K X (6) (16)) := VGoodTail.tail_nn K O X hg (6) 9
  have et23 : (VGoodTail.tt K X (6) (16)) = (K.add (K.mul (co K X (6)) (co K X (15))) (K.neg (K.mul (co K X (5)) (co K X (16))))) := rfl
  have t24 : O.le K.zero (VGoodTail.tt K X (6) (14)) := VGoodTail.tail_nn K O X hg (6) 7
  have et24 : (VGoodTail.tt K X (6) (14)) = (K.add (K.mul (co K X (6)) (co K X (13))) (K.neg (K.mul (co K X (5)) (co K X (14))))) := rfl
  have t25 : O.le K.zero (VGoodTail.tt K X (13) (21)) := VGoodTail.tail_nn K O X hg (13) 7
  have et25 : (VGoodTail.tt K X (13) (21)) = (K.add (K.mul (co K X (13)) (co K X (20))) (K.neg (K.mul (co K X (12)) (co K X (21))))) := rfl
  have t26 : O.le K.zero (VGoodTail.tt K X (13) (19)) := VGoodTail.tail_nn K O X hg (13) 5
  have et26 : (VGoodTail.tt K X (13) (19)) = (K.add (K.mul (co K X (13)) (co K X (18))) (K.neg (K.mul (co K X (12)) (co K X (19))))) := rfl
  have t27 : O.le K.zero (VGoodTail.tt K X (13) (17)) := VGoodTail.tail_nn K O X hg (13) 3
  have et27 : (VGoodTail.tt K X (13) (17)) = (K.add (K.mul (co K X (13)) (co K X (16))) (K.neg (K.mul (co K X (12)) (co K X (17))))) := rfl
  have t28 : O.le K.zero (VGoodTail.tt K X (13) (15)) := VGoodTail.tail_nn K O X hg (13) 1
  have et28 : (VGoodTail.tt K X (13) (15)) = (K.add (K.mul (co K X (13)) (co K X (14))) (K.neg (K.mul (co K X (12)) (co K X (15))))) := rfl
  have t29 : O.le K.zero (VGoodTail.tt K X (11) (21)) := VGoodTail.tail_nn K O X hg (11) 9
  have et29 : (VGoodTail.tt K X (11) (21)) = (K.add (K.mul (co K X (11)) (co K X (20))) (K.neg (K.mul (co K X (10)) (co K X (21))))) := rfl
  have t30 : O.le K.zero (VGoodTail.tt K X (11) (13)) := VGoodTail.tail_nn K O X hg (11) 1
  have et30 : (VGoodTail.tt K X (11) (13)) = (K.add (K.mul (co K X (11)) (co K X (12))) (K.neg (K.mul (co K X (10)) (co K X (13))))) := rfl
  have t31 : O.le K.zero (VGoodTail.tt K X (9) (21)) := VGoodTail.tail_nn K O X hg (9) 11
  have et31 : (VGoodTail.tt K X (9) (21)) = (K.add (K.mul (co K X (9)) (co K X (20))) (K.neg (K.mul (co K X (8)) (co K X (21))))) := rfl
  have t32 : O.le K.zero (VGoodTail.tt K X (9) (13)) := VGoodTail.tail_nn K O X hg (9) 3
  have et32 : (VGoodTail.tt K X (9) (13)) = (K.add (K.mul (co K X (9)) (co K X (12))) (K.neg (K.mul (co K X (8)) (co K X (13))))) := rfl
  have t33 : O.le K.zero (VGoodTail.tt K X (7) (21)) := VGoodTail.tail_nn K O X hg (7) 13
  have et33 : (VGoodTail.tt K X (7) (21)) = (K.add (K.mul (co K X (7)) (co K X (20))) (K.neg (K.mul (co K X (6)) (co K X (21))))) := rfl
  have t34 : O.le K.zero (VGoodTail.tt K X (7) (13)) := VGoodTail.tail_nn K O X hg (7) 5
  have et34 : (VGoodTail.tt K X (7) (13)) = (K.add (K.mul (co K X (7)) (co K X (12))) (K.neg (K.mul (co K X (6)) (co K X (13))))) := rfl
  have hp : 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.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.add (K.add (K.add (K.add (K.add (K.add (K.add (VGoodTail.tt K X (9) (17)) (VGoodTail.t  -- (+1407 chars; full line in the Zenodo source)
    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 _ _ (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 _ _ (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   -- (+888 chars; full line in the Zenodo source)
  refine O.le_trans _ _ _ hp (TripleE.le_of_eq' K O _ _ ?_)
  rw [kv, et0, et1, et2, et3, et4, et5, et6, et7, et8, et9, et10, et11, et12, et13, et14, et15, et16, et17, et18, et19, et20, et21, et22, et23, et24, et25, et26, et27, et28, et29, et30, et31, et32, et33, et34]
  ring_z

theorem kna_3_8 (X : List R) (hg : L1Full.Good K O X) :
    O.le K.zero (CBLib.kern K (Fa K X) (Ga K X) 5 (3) (12)) := by
  have kv : CBLib.kern K (Fa K X) (Ga K X) 5 (3) (12) = (K.add (K.add (K.add (K.add (K.add K.zero (K.add (K.mul (co K X (9)) (co K X (17))) (K.neg (K.mul (co K X (18)) (co K X (8)))))) (K.add (K.mul (K.add (co K X (10)) (co K X (8))) (K.add (co K X (18)) (co K X (16)))) (K.neg (K.mul (K.add (co K X (19)) (co K X (17))) (K.add (co K X (9)) (co K X (7))))))) (K.add (K.mul (K.add (K.add (co K X (11))  -- (+967 chars; full line in the Zenodo source)
  have t0 : O.le K.zero (VGoodTail.tt K X (9) (18)) := VGoodTail.tail_nn K O X hg (9) 8
  have et0 : (VGoodTail.tt K X (9) (18)) = (K.add (K.mul (co K X (9)) (co K X (17))) (K.neg (K.mul (co K X (8)) (co K X (18))))) := rfl
  have t1 : O.le K.zero (VGoodTail.tt K X (10) (19)) := VGoodTail.tail_nn K O X hg (10) 8
  have et1 : (VGoodTail.tt K X (10) (19)) = (K.add (K.mul (co K X (10)) (co K X (18))) (K.neg (K.mul (co K X (9)) (co K X (19))))) := rfl
  have t2 : O.le K.zero (VGoodTail.tt K X (10) (17)) := VGoodTail.tail_nn K O X hg (10) 6
  have et2 : (VGoodTail.tt K X (10) (17)) = (K.add (K.mul (co K X (10)) (co K X (16))) (K.neg (K.mul (co K X (9)) (co K X (17))))) := rfl
  have t3 : O.le K.zero (VGoodTail.tt K X (8) (19)) := VGoodTail.tail_nn K O X hg (8) 10
  have et3 : (VGoodTail.tt K X (8) (19)) = (K.add (K.mul (co K X (8)) (co K X (18))) (K.neg (K.mul (co K X (7)) (co K X (19))))) := rfl
  have t4 : O.le K.zero (VGoodTail.tt K X (8) (17)) := VGoodTail.tail_nn K O X hg (8) 8
  have et4 : (VGoodTail.tt K X (8) (17)) = (K.add (K.mul (co K X (8)) (co K X (16))) (K.neg (K.mul (co K X (7)) (co K X (17))))) := rfl
  have t5 : O.le K.zero (VGoodTail.tt K X (11) (20)) := VGoodTail.tail_nn K O X hg (11) 8
  have et5 : (VGoodTail.tt K X (11) (20)) = (K.add (K.mul (co K X (11)) (co K X (19))) (K.neg (K.mul (co K X (10)) (co K X (20))))) := rfl
  have t6 : O.le K.zero (VGoodTail.tt K X (11) (18)) := VGoodTail.tail_nn K O X hg (11) 6
  have et6 : (VGoodTail.tt K X (11) (18)) = (K.add (K.mul (co K X (11)) (co K X (17))) (K.neg (K.mul (co K X (10)) (co K X (18))))) := rfl
  have t7 : O.le K.zero (VGoodTail.tt K X (11) (16)) := VGoodTail.tail_nn K O X hg (11) 4
  have et7 : (VGoodTail.tt K X (11) (16)) = (K.add (K.mul (co K X (11)) (co K X (15))) (K.neg (K.mul (co K X (10)) (co K X (16))))) := rfl
  have t8 : O.le K.zero (VGoodTail.tt K X (9) (20)) := VGoodTail.tail_nn K O X hg (9) 10
  have et8 : (VGoodTail.tt K X (9) (20)) = (K.add (K.mul (co K X (9)) (co K X (19))) (K.neg (K.mul (co K X (8)) (co K X (20))))) := rfl
  have t9 : O.le K.zero (VGoodTail.tt K X (9) (16)) := VGoodTail.tail_nn K O X hg (9) 6
  have et9 : (VGoodTail.tt K X (9) (16)) = (K.add (K.mul (co K X (9)) (co K X (15))) (K.neg (K.mul (co K X (8)) (co K X (16))))) := rfl
  have t10 : O.le K.zero (VGoodTail.tt K X (7) (20)) := VGoodTail.tail_nn K O X hg (7) 12
  have et10 : (VGoodTail.tt K X (7) (20)) = (K.add (K.mul (co K X (7)) (co K X (19))) (K.neg (K.mul (co K X (6)) (co K X (20))))) := rfl
  have t11 : O.le K.zero (VGoodTail.tt K X (7) (18)) := VGoodTail.tail_nn K O X hg (7) 10
  have et11 : (VGoodTail.tt K X (7) (18)) = (K.add (K.mul (co K X (7)) (co K X (17))) (K.neg (K.mul (co K X (6)) (co K X (18))))) := rfl
  have t12 : O.le K.zero (VGoodTail.tt K X (7) (16)) := VGoodTail.tail_nn K O X hg (7) 8
  have et12 : (VGoodTail.tt K X (7) (16)) = (K.add (K.mul (co K X (7)) (co K X (15))) (K.neg (K.mul (co K X (6)) (co K X (16))))) := rfl
  have t13 : O.le K.zero (VGoodTail.tt K X (12) (21)) := VGoodTail.tail_nn K O X hg (12) 8
  have et13 : (VGoodTail.tt K X (12) (21)) = (K.add (K.mul (co K X (12)) (co K X (20))) (K.neg (K.mul (co K X (11)) (co K X (21))))) := rfl
  have t14 : O.le K.zero (VGoodTail.tt K X (12) (19)) := VGoodTail.tail_nn K O X hg (12) 6
  have et14 : (VGoodTail.tt K X (12) (19)) = (K.add (K.mul (co K X (12)) (co K X (18))) (K.neg (K.mul (co K X (11)) (co K X (19))))) := rfl
  have t15 : O.le K.zero (VGoodTail.tt K X (12) (17)) := VGoodTail.tail_nn K O X hg (12) 4
  have et15 : (VGoodTail.tt K X (12) (17)) = (K.add (K.mul (co K X (12)) (co K X (16))) (K.neg (K.mul (co K X (11)) (co K X (17))))) := rfl
  have t16 : O.le K.zero (VGoodTail.tt K X (12) (15)) := VGoodTail.tail_nn K O X hg (12) 2
  have et16 : (VGoodTail.tt K X (12) (15)) = (K.add (K.mul (co K X (12)) (co K X (14))) (K.neg (K.mul (co K X (11)) (co K X (15))))) := rfl
  have t17 : O.le K.zero (VGoodTail.tt K X (10) (21)) := VGoodTail.tail_nn K O X hg (10) 10
  have et17 : (VGoodTail.tt K X (10) (21)) = (K.add (K.mul (co K X (10)) (co K X (20))) (K.neg (K.mul (co K X (9)) (co K X (21))))) := rfl
  have t18 : O.le K.zero (VGoodTail.tt K X (10) (15)) := VGoodTail.tail_nn K O X hg (10) 4
  have et18 : (VGoodTail.tt K X (10) (15)) = (K.add (K.mul (co K X (10)) (co K X (14))) (K.neg (K.mul (co K X (9)) (co K X (15))))) := rfl
  have t19 : O.le K.zero (VGoodTail.tt K X (8) (21)) := VGoodTail.tail_nn K O X hg (8) 12
  have et19 : (VGoodTail.tt K X (8) (21)) = (K.add (K.mul (co K X (8)) (co K X (20))) (K.neg (K.mul (co K X (7)) (co K X (21))))) := rfl
  have t20 : O.le K.zero (VGoodTail.tt K X (8) (15)) := VGoodTail.tail_nn K O X hg (8) 6
  have et20 : (VGoodTail.tt K X (8) (15)) = (K.add (K.mul (co K X (8)) (co K X (14))) (K.neg (K.mul (co K X (7)) (co K X (15))))) := rfl
  have t21 : O.le K.zero (VGoodTail.tt K X (6) (21)) := VGoodTail.tail_nn K O X hg (6) 14
  have et21 : (VGoodTail.tt K X (6) (21)) = (K.add (K.mul (co K X (6)) (co K X (20))) (K.neg (K.mul (co K X (5)) (co K X (21))))) := rfl
  have t22 : O.le K.zero (VGoodTail.tt K X (6) (19)) := VGoodTail.tail_nn K O X hg (6) 12
  have et22 : (VGoodTail.tt K X (6) (19)) = (K.add (K.mul (co K X (6)) (co K X (18))) (K.neg (K.mul (co K X (5)) (co K X (19))))) := rfl
  have t23 : O.le K.zero (VGoodTail.tt K X (6) (17)) := VGoodTail.tail_nn K O X hg (6) 10
  have et23 : (VGoodTail.tt K X (6) (17)) = (K.add (K.mul (co K X (6)) (co K X (16))) (K.neg (K.mul (co K X (5)) (co K X (17))))) := rfl
  have t24 : O.le K.zero (VGoodTail.tt K X (6) (15)) := VGoodTail.tail_nn K O X hg (6) 8
  have et24 : (VGoodTail.tt K X (6) (15)) = (K.add (K.mul (co K X (6)) (co K X (14))) (K.neg (K.mul (co K X (5)) (co K X (15))))) := rfl
  have t25 : O.le K.zero (VGoodTail.tt K X (13) (22)) := VGoodTail.tail_nn K O X hg (13) 8
  have et25 : (VGoodTail.tt K X (13) (22)) = (K.add (K.mul (co K X (13)) (co K X (21))) (K.neg (K.mul (co K X (12)) (co K X (22))))) := rfl
  have t26 : O.le K.zero (VGoodTail.tt K X (13) (20)) := VGoodTail.tail_nn K O X hg (13) 6
  have et26 : (VGoodTail.tt K X (13) (20)) = (K.add (K.mul (co K X (13)) (co K X (19))) (K.neg (K.mul (co K X (12)) (co K X (20))))) := rfl
  have t27 : O.le K.zero (VGoodTail.tt K X (13) (18)) := VGoodTail.tail_nn K O X hg (13) 4
  have et27 : (VGoodTail.tt K X (13) (18)) = (K.add (K.mul (co K X (13)) (co K X (17))) (K.neg (K.mul (co K X (12)) (co K X (18))))) := rfl
  have t28 : O.le K.zero (VGoodTail.tt K X (13) (16)) := VGoodTail.tail_nn K O X hg (13) 2
  have et28 : (VGoodTail.tt K X (13) (16)) = (K.add (K.mul (co K X (13)) (co K X (15))) (K.neg (K.mul (co K X (12)) (co K X (16))))) := rfl
  have t29 : O.le K.zero (VGoodTail.tt K X (13) (14)) := VGoodTail.tail_nn K O X hg (13) 0
  have et29 : (VGoodTail.tt K X (13) (14)) = (K.add (K.mul (co K X (13)) (co K X (13))) (K.neg (K.mul (co K X (12)) (co K X (14))))) := rfl
  have t30 : O.le K.zero (VGoodTail.tt K X (11) (22)) := VGoodTail.tail_nn K O X hg (11) 10
  have et30 : (VGoodTail.tt K X (11) (22)) = (K.add (K.mul (co K X (11)) (co K X (21))) (K.neg (K.mul (co K X (10)) (co K X (22))))) := rfl
  have t31 : O.le K.zero (VGoodTail.tt K X (11) (14)) := VGoodTail.tail_nn K O X hg (11) 2
  have et31 : (VGoodTail.tt K X (11) (14)) = (K.add (K.mul (co K X (11)) (co K X (13))) (K.neg (K.mul (co K X (10)) (co K X (14))))) := rfl
  have t32 : O.le K.zero (VGoodTail.tt K X (9) (22)) := VGoodTail.tail_nn K O X hg (9) 12
  have et32 : (VGoodTail.tt K X (9) (22)) = (K.add (K.mul (co K X (9)) (co K X (21))) (K.neg (K.mul (co K X (8)) (co K X (22))))) := rfl
  have t33 : O.le K.zero (VGoodTail.tt K X (9) (14)) := VGoodTail.tail_nn K O X hg (9) 4
  have et33 : (VGoodTail.tt K X (9) (14)) = (K.add (K.mul (co K X (9)) (co K X (13))) (K.neg (K.mul (co K X (8)) (co K X (14))))) := rfl
  have t34 : O.le K.zero (VGoodTail.tt K X (7) (22)) := VGoodTail.tail_nn K O X hg (7) 14
  have et34 : (VGoodTail.tt K X (7) (22)) = (K.add (K.mul (co K X (7)) (co K X (21))) (K.neg (K.mul (co K X (6)) (co K X (22))))) := rfl
  have t35 : O.le K.zero (VGoodTail.tt K X (7) (14)) := VGoodTail.tail_nn K O X hg (7) 6
  have et35 : (VGoodTail.tt K X (7) (14)) = (K.add (K.mul (co K X (7)) (co K X (13))) (K.neg (K.mul (co K X (6)) (co K X (14))))) := rfl
  have hp : 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.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.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (VGoodTail.tt K X (9) (18)) (VGoo  -- (+1444 chars; full line in the Zenodo source)
    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 _ _ (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 _ _ (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   -- (+915 chars; full line in the Zenodo source)
  refine O.le_trans _ _ _ hp (TripleE.le_of_eq' K O _ _ ?_)
  rw [kv, et0, et1, et2, et3, et4, et5, et6, et7, et8, et9, et10, et11, et12, et13, et14, et15, et16, et17, et18, et19, et20, et21, et22, et23, et24, et25, et26, et27, et28, et29, et30, et31, et32, et33, et34, et35]
  ring_z

theorem kna_3_9 (X : List R) (hg : L1Full.Good K O X) :
    O.le K.zero (CBLib.kern K (Fa K X) (Ga K X) 5 (3) (13)) := by
  have kv : CBLib.kern K (Fa K X) (Ga K X) 5 (3) (13) = (K.add (K.add (K.add (K.add (K.add K.zero (K.add (K.mul (co K X (9)) (co K X (18))) (K.neg (K.mul (co K X (19)) (co K X (8)))))) (K.add (K.mul (K.add (co K X (10)) (co K X (8))) (K.add (co K X (19)) (co K X (17)))) (K.neg (K.mul (K.add (co K X (20)) (co K X (18))) (K.add (co K X (9)) (co K X (7))))))) (K.add (K.mul (K.add (K.add (co K X (11))  -- (+967 chars; full line in the Zenodo source)
  have t0 : O.le K.zero (VGoodTail.tt K X (9) (19)) := VGoodTail.tail_nn K O X hg (9) 9
  have et0 : (VGoodTail.tt K X (9) (19)) = (K.add (K.mul (co K X (9)) (co K X (18))) (K.neg (K.mul (co K X (8)) (co K X (19))))) := rfl
  have t1 : O.le K.zero (VGoodTail.tt K X (10) (20)) := VGoodTail.tail_nn K O X hg (10) 9
  have et1 : (VGoodTail.tt K X (10) (20)) = (K.add (K.mul (co K X (10)) (co K X (19))) (K.neg (K.mul (co K X (9)) (co K X (20))))) := rfl
  have t2 : O.le K.zero (VGoodTail.tt K X (10) (18)) := VGoodTail.tail_nn K O X hg (10) 7
  have et2 : (VGoodTail.tt K X (10) (18)) = (K.add (K.mul (co K X (10)) (co K X (17))) (K.neg (K.mul (co K X (9)) (co K X (18))))) := rfl
  have t3 : O.le K.zero (VGoodTail.tt K X (8) (20)) := VGoodTail.tail_nn K O X hg (8) 11
  have et3 : (VGoodTail.tt K X (8) (20)) = (K.add (K.mul (co K X (8)) (co K X (19))) (K.neg (K.mul (co K X (7)) (co K X (20))))) := rfl
  have t4 : O.le K.zero (VGoodTail.tt K X (8) (18)) := VGoodTail.tail_nn K O X hg (8) 9
  have et4 : (VGoodTail.tt K X (8) (18)) = (K.add (K.mul (co K X (8)) (co K X (17))) (K.neg (K.mul (co K X (7)) (co K X (18))))) := rfl
  have t5 : O.le K.zero (VGoodTail.tt K X (11) (21)) := VGoodTail.tail_nn K O X hg (11) 9
  have et5 : (VGoodTail.tt K X (11) (21)) = (K.add (K.mul (co K X (11)) (co K X (20))) (K.neg (K.mul (co K X (10)) (co K X (21))))) := rfl
  have t6 : O.le K.zero (VGoodTail.tt K X (11) (19)) := VGoodTail.tail_nn K O X hg (11) 7
  have et6 : (VGoodTail.tt K X (11) (19)) = (K.add (K.mul (co K X (11)) (co K X (18))) (K.neg (K.mul (co K X (10)) (co K X (19))))) := rfl
  have t7 : O.le K.zero (VGoodTail.tt K X (11) (17)) := VGoodTail.tail_nn K O X hg (11) 5
  have et7 : (VGoodTail.tt K X (11) (17)) = (K.add (K.mul (co K X (11)) (co K X (16))) (K.neg (K.mul (co K X (10)) (co K X (17))))) := rfl
  have t8 : O.le K.zero (VGoodTail.tt K X (9) (21)) := VGoodTail.tail_nn K O X hg (9) 11
  have et8 : (VGoodTail.tt K X (9) (21)) = (K.add (K.mul (co K X (9)) (co K X (20))) (K.neg (K.mul (co K X (8)) (co K X (21))))) := rfl
  have t9 : O.le K.zero (VGoodTail.tt K X (9) (17)) := VGoodTail.tail_nn K O X hg (9) 7
  have et9 : (VGoodTail.tt K X (9) (17)) = (K.add (K.mul (co K X (9)) (co K X (16))) (K.neg (K.mul (co K X (8)) (co K X (17))))) := rfl
  have t10 : O.le K.zero (VGoodTail.tt K X (7) (21)) := VGoodTail.tail_nn K O X hg (7) 13
  have et10 : (VGoodTail.tt K X (7) (21)) = (K.add (K.mul (co K X (7)) (co K X (20))) (K.neg (K.mul (co K X (6)) (co K X (21))))) := rfl
  have t11 : O.le K.zero (VGoodTail.tt K X (7) (19)) := VGoodTail.tail_nn K O X hg (7) 11
  have et11 : (VGoodTail.tt K X (7) (19)) = (K.add (K.mul (co K X (7)) (co K X (18))) (K.neg (K.mul (co K X (6)) (co K X (19))))) := rfl
  have t12 : O.le K.zero (VGoodTail.tt K X (7) (17)) := VGoodTail.tail_nn K O X hg (7) 9
  have et12 : (VGoodTail.tt K X (7) (17)) = (K.add (K.mul (co K X (7)) (co K X (16))) (K.neg (K.mul (co K X (6)) (co K X (17))))) := rfl
  have t13 : O.le K.zero (VGoodTail.tt K X (12) (22)) := VGoodTail.tail_nn K O X hg (12) 9
  have et13 : (VGoodTail.tt K X (12) (22)) = (K.add (K.mul (co K X (12)) (co K X (21))) (K.neg (K.mul (co K X (11)) (co K X (22))))) := rfl
  have t14 : O.le K.zero (VGoodTail.tt K X (12) (20)) := VGoodTail.tail_nn K O X hg (12) 7
  have et14 : (VGoodTail.tt K X (12) (20)) = (K.add (K.mul (co K X (12)) (co K X (19))) (K.neg (K.mul (co K X (11)) (co K X (20))))) := rfl
  have t15 : O.le K.zero (VGoodTail.tt K X (12) (18)) := VGoodTail.tail_nn K O X hg (12) 5
  have et15 : (VGoodTail.tt K X (12) (18)) = (K.add (K.mul (co K X (12)) (co K X (17))) (K.neg (K.mul (co K X (11)) (co K X (18))))) := rfl
  have t16 : O.le K.zero (VGoodTail.tt K X (12) (16)) := VGoodTail.tail_nn K O X hg (12) 3
  have et16 : (VGoodTail.tt K X (12) (16)) = (K.add (K.mul (co K X (12)) (co K X (15))) (K.neg (K.mul (co K X (11)) (co K X (16))))) := rfl
  have t17 : O.le K.zero (VGoodTail.tt K X (10) (22)) := VGoodTail.tail_nn K O X hg (10) 11
  have et17 : (VGoodTail.tt K X (10) (22)) = (K.add (K.mul (co K X (10)) (co K X (21))) (K.neg (K.mul (co K X (9)) (co K X (22))))) := rfl
  have t18 : O.le K.zero (VGoodTail.tt K X (10) (16)) := VGoodTail.tail_nn K O X hg (10) 5
  have et18 : (VGoodTail.tt K X (10) (16)) = (K.add (K.mul (co K X (10)) (co K X (15))) (K.neg (K.mul (co K X (9)) (co K X (16))))) := rfl
  have t19 : O.le K.zero (VGoodTail.tt K X (8) (22)) := VGoodTail.tail_nn K O X hg (8) 13
  have et19 : (VGoodTail.tt K X (8) (22)) = (K.add (K.mul (co K X (8)) (co K X (21))) (K.neg (K.mul (co K X (7)) (co K X (22))))) := rfl
  have t20 : O.le K.zero (VGoodTail.tt K X (8) (16)) := VGoodTail.tail_nn K O X hg (8) 7
  have et20 : (VGoodTail.tt K X (8) (16)) = (K.add (K.mul (co K X (8)) (co K X (15))) (K.neg (K.mul (co K X (7)) (co K X (16))))) := rfl
  have t21 : O.le K.zero (VGoodTail.tt K X (6) (22)) := VGoodTail.tail_nn K O X hg (6) 15
  have et21 : (VGoodTail.tt K X (6) (22)) = (K.add (K.mul (co K X (6)) (co K X (21))) (K.neg (K.mul (co K X (5)) (co K X (22))))) := rfl
  have t22 : O.le K.zero (VGoodTail.tt K X (6) (20)) := VGoodTail.tail_nn K O X hg (6) 13
  have et22 : (VGoodTail.tt K X (6) (20)) = (K.add (K.mul (co K X (6)) (co K X (19))) (K.neg (K.mul (co K X (5)) (co K X (20))))) := rfl
  have t23 : O.le K.zero (VGoodTail.tt K X (6) (18)) := VGoodTail.tail_nn K O X hg (6) 11
  have et23 : (VGoodTail.tt K X (6) (18)) = (K.add (K.mul (co K X (6)) (co K X (17))) (K.neg (K.mul (co K X (5)) (co K X (18))))) := rfl
  have t24 : O.le K.zero (VGoodTail.tt K X (6) (16)) := VGoodTail.tail_nn K O X hg (6) 9
  have et24 : (VGoodTail.tt K X (6) (16)) = (K.add (K.mul (co K X (6)) (co K X (15))) (K.neg (K.mul (co K X (5)) (co K X (16))))) := rfl
  have t25 : O.le K.zero (VGoodTail.tt K X (13) (23)) := VGoodTail.tail_nn K O X hg (13) 9
  have et25 : (VGoodTail.tt K X (13) (23)) = (K.add (K.mul (co K X (13)) (co K X (22))) (K.neg (K.mul (co K X (12)) (co K X (23))))) := rfl
  have t26 : O.le K.zero (VGoodTail.tt K X (13) (21)) := VGoodTail.tail_nn K O X hg (13) 7
  have et26 : (VGoodTail.tt K X (13) (21)) = (K.add (K.mul (co K X (13)) (co K X (20))) (K.neg (K.mul (co K X (12)) (co K X (21))))) := rfl
  have t27 : O.le K.zero (VGoodTail.tt K X (13) (19)) := VGoodTail.tail_nn K O X hg (13) 5
  have et27 : (VGoodTail.tt K X (13) (19)) = (K.add (K.mul (co K X (13)) (co K X (18))) (K.neg (K.mul (co K X (12)) (co K X (19))))) := rfl
  have t28 : O.le K.zero (VGoodTail.tt K X (13) (17)) := VGoodTail.tail_nn K O X hg (13) 3
  have et28 : (VGoodTail.tt K X (13) (17)) = (K.add (K.mul (co K X (13)) (co K X (16))) (K.neg (K.mul (co K X (12)) (co K X (17))))) := rfl
  have t29 : O.le K.zero (VGoodTail.tt K X (13) (15)) := VGoodTail.tail_nn K O X hg (13) 1
  have et29 : (VGoodTail.tt K X (13) (15)) = (K.add (K.mul (co K X (13)) (co K X (14))) (K.neg (K.mul (co K X (12)) (co K X (15))))) := rfl
  have t30 : O.le K.zero (VGoodTail.tt K X (11) (23)) := VGoodTail.tail_nn K O X hg (11) 11
  have et30 : (VGoodTail.tt K X (11) (23)) = (K.add (K.mul (co K X (11)) (co K X (22))) (K.neg (K.mul (co K X (10)) (co K X (23))))) := rfl
  have t31 : O.le K.zero (VGoodTail.tt K X (11) (15)) := VGoodTail.tail_nn K O X hg (11) 3
  have et31 : (VGoodTail.tt K X (11) (15)) = (K.add (K.mul (co K X (11)) (co K X (14))) (K.neg (K.mul (co K X (10)) (co K X (15))))) := rfl
  have t32 : O.le K.zero (VGoodTail.tt K X (9) (23)) := VGoodTail.tail_nn K O X hg (9) 13
  have et32 : (VGoodTail.tt K X (9) (23)) = (K.add (K.mul (co K X (9)) (co K X (22))) (K.neg (K.mul (co K X (8)) (co K X (23))))) := rfl
  have t33 : O.le K.zero (VGoodTail.tt K X (9) (15)) := VGoodTail.tail_nn K O X hg (9) 5
  have et33 : (VGoodTail.tt K X (9) (15)) = (K.add (K.mul (co K X (9)) (co K X (14))) (K.neg (K.mul (co K X (8)) (co K X (15))))) := rfl
  have t34 : O.le K.zero (VGoodTail.tt K X (7) (23)) := VGoodTail.tail_nn K O X hg (7) 15
  have et34 : (VGoodTail.tt K X (7) (23)) = (K.add (K.mul (co K X (7)) (co K X (22))) (K.neg (K.mul (co K X (6)) (co K X (23))))) := rfl
  have t35 : O.le K.zero (VGoodTail.tt K X (7) (15)) := VGoodTail.tail_nn K O X hg (7) 7
  have et35 : (VGoodTail.tt K X (7) (15)) = (K.add (K.mul (co K X (7)) (co K X (14))) (K.neg (K.mul (co K X (6)) (co K X (15))))) := rfl
  have hp : 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.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.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (VGoodTail.tt K X (9) (19)) (VGoo  -- (+1444 chars; full line in the Zenodo source)
    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 _ _ (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 _ _ (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   -- (+915 chars; full line in the Zenodo source)
  refine O.le_trans _ _ _ hp (TripleE.le_of_eq' K O _ _ ?_)
  rw [kv, et0, et1, et2, et3, et4, et5, et6, et7, et8, et9, et10, et11, et12, et13, et14, et15, et16, et17, et18, et19, et20, et21, et22, et23, et24, et25, et26, et27, et28, et29, et30, et31, et32, et33, et34, et35]
  ring_z

theorem kna_3_10 (X : List R) (hg : L1Full.Good K O X) :
    O.le K.zero (CBLib.kern K (Fa K X) (Ga K X) 5 (3) (14)) := by
  have kv : CBLib.kern K (Fa K X) (Ga K X) 5 (3) (14) = (K.add (K.add (K.add (K.add (K.add K.zero (K.add (K.mul (co K X (9)) (co K X (19))) (K.neg (K.mul (co K X (20)) (co K X (8)))))) (K.add (K.mul (K.add (co K X (10)) (co K X (8))) (K.add (co K X (20)) (co K X (18)))) (K.neg (K.mul (K.add (co K X (21)) (co K X (19))) (K.add (co K X (9)) (co K X (7))))))) (K.add (K.mul (K.add (K.add (co K X (11))  -- (+967 chars; full line in the Zenodo source)
  have t0 : O.le K.zero (VGoodTail.tt K X (9) (20)) := VGoodTail.tail_nn K O X hg (9) 10
  have et0 : (VGoodTail.tt K X (9) (20)) = (K.add (K.mul (co K X (9)) (co K X (19))) (K.neg (K.mul (co K X (8)) (co K X (20))))) := rfl
  have t1 : O.le K.zero (VGoodTail.tt K X (10) (21)) := VGoodTail.tail_nn K O X hg (10) 10
  have et1 : (VGoodTail.tt K X (10) (21)) = (K.add (K.mul (co K X (10)) (co K X (20))) (K.neg (K.mul (co K X (9)) (co K X (21))))) := rfl
  have t2 : O.le K.zero (VGoodTail.tt K X (10) (19)) := VGoodTail.tail_nn K O X hg (10) 8
  have et2 : (VGoodTail.tt K X (10) (19)) = (K.add (K.mul (co K X (10)) (co K X (18))) (K.neg (K.mul (co K X (9)) (co K X (19))))) := rfl
  have t3 : O.le K.zero (VGoodTail.tt K X (8) (21)) := VGoodTail.tail_nn K O X hg (8) 12
  have et3 : (VGoodTail.tt K X (8) (21)) = (K.add (K.mul (co K X (8)) (co K X (20))) (K.neg (K.mul (co K X (7)) (co K X (21))))) := rfl
  have t4 : O.le K.zero (VGoodTail.tt K X (8) (19)) := VGoodTail.tail_nn K O X hg (8) 10
  have et4 : (VGoodTail.tt K X (8) (19)) = (K.add (K.mul (co K X (8)) (co K X (18))) (K.neg (K.mul (co K X (7)) (co K X (19))))) := rfl
  have t5 : O.le K.zero (VGoodTail.tt K X (11) (22)) := VGoodTail.tail_nn K O X hg (11) 10
  have et5 : (VGoodTail.tt K X (11) (22)) = (K.add (K.mul (co K X (11)) (co K X (21))) (K.neg (K.mul (co K X (10)) (co K X (22))))) := rfl
  have t6 : O.le K.zero (VGoodTail.tt K X (11) (20)) := VGoodTail.tail_nn K O X hg (11) 8
  have et6 : (VGoodTail.tt K X (11) (20)) = (K.add (K.mul (co K X (11)) (co K X (19))) (K.neg (K.mul (co K X (10)) (co K X (20))))) := rfl
  have t7 : O.le K.zero (VGoodTail.tt K X (11) (18)) := VGoodTail.tail_nn K O X hg (11) 6
  have et7 : (VGoodTail.tt K X (11) (18)) = (K.add (K.mul (co K X (11)) (co K X (17))) (K.neg (K.mul (co K X (10)) (co K X (18))))) := rfl
  have t8 : O.le K.zero (VGoodTail.tt K X (9) (22)) := VGoodTail.tail_nn K O X hg (9) 12
  have et8 : (VGoodTail.tt K X (9) (22)) = (K.add (K.mul (co K X (9)) (co K X (21))) (K.neg (K.mul (co K X (8)) (co K X (22))))) := rfl
  have t9 : O.le K.zero (VGoodTail.tt K X (9) (18)) := VGoodTail.tail_nn K O X hg (9) 8
  have et9 : (VGoodTail.tt K X (9) (18)) = (K.add (K.mul (co K X (9)) (co K X (17))) (K.neg (K.mul (co K X (8)) (co K X (18))))) := rfl
  have t10 : O.le K.zero (VGoodTail.tt K X (7) (22)) := VGoodTail.tail_nn K O X hg (7) 14
  have et10 : (VGoodTail.tt K X (7) (22)) = (K.add (K.mul (co K X (7)) (co K X (21))) (K.neg (K.mul (co K X (6)) (co K X (22))))) := rfl
  have t11 : O.le K.zero (VGoodTail.tt K X (7) (20)) := VGoodTail.tail_nn K O X hg (7) 12
  have et11 : (VGoodTail.tt K X (7) (20)) = (K.add (K.mul (co K X (7)) (co K X (19))) (K.neg (K.mul (co K X (6)) (co K X (20))))) := rfl
  have t12 : O.le K.zero (VGoodTail.tt K X (7) (18)) := VGoodTail.tail_nn K O X hg (7) 10
  have et12 : (VGoodTail.tt K X (7) (18)) = (K.add (K.mul (co K X (7)) (co K X (17))) (K.neg (K.mul (co K X (6)) (co K X (18))))) := rfl
  have t13 : O.le K.zero (VGoodTail.tt K X (12) (23)) := VGoodTail.tail_nn K O X hg (12) 10
  have et13 : (VGoodTail.tt K X (12) (23)) = (K.add (K.mul (co K X (12)) (co K X (22))) (K.neg (K.mul (co K X (11)) (co K X (23))))) := rfl
  have t14 : O.le K.zero (VGoodTail.tt K X (12) (21)) := VGoodTail.tail_nn K O X hg (12) 8
  have et14 : (VGoodTail.tt K X (12) (21)) = (K.add (K.mul (co K X (12)) (co K X (20))) (K.neg (K.mul (co K X (11)) (co K X (21))))) := rfl
  have t15 : O.le K.zero (VGoodTail.tt K X (12) (19)) := VGoodTail.tail_nn K O X hg (12) 6
  have et15 : (VGoodTail.tt K X (12) (19)) = (K.add (K.mul (co K X (12)) (co K X (18))) (K.neg (K.mul (co K X (11)) (co K X (19))))) := rfl
  have t16 : O.le K.zero (VGoodTail.tt K X (12) (17)) := VGoodTail.tail_nn K O X hg (12) 4
  have et16 : (VGoodTail.tt K X (12) (17)) = (K.add (K.mul (co K X (12)) (co K X (16))) (K.neg (K.mul (co K X (11)) (co K X (17))))) := rfl
  have t17 : O.le K.zero (VGoodTail.tt K X (10) (23)) := VGoodTail.tail_nn K O X hg (10) 12
  have et17 : (VGoodTail.tt K X (10) (23)) = (K.add (K.mul (co K X (10)) (co K X (22))) (K.neg (K.mul (co K X (9)) (co K X (23))))) := rfl
  have t18 : O.le K.zero (VGoodTail.tt K X (10) (17)) := VGoodTail.tail_nn K O X hg (10) 6
  have et18 : (VGoodTail.tt K X (10) (17)) = (K.add (K.mul (co K X (10)) (co K X (16))) (K.neg (K.mul (co K X (9)) (co K X (17))))) := rfl
  have t19 : O.le K.zero (VGoodTail.tt K X (8) (23)) := VGoodTail.tail_nn K O X hg (8) 14
  have et19 : (VGoodTail.tt K X (8) (23)) = (K.add (K.mul (co K X (8)) (co K X (22))) (K.neg (K.mul (co K X (7)) (co K X (23))))) := rfl
  have t20 : O.le K.zero (VGoodTail.tt K X (8) (17)) := VGoodTail.tail_nn K O X hg (8) 8
  have et20 : (VGoodTail.tt K X (8) (17)) = (K.add (K.mul (co K X (8)) (co K X (16))) (K.neg (K.mul (co K X (7)) (co K X (17))))) := rfl
  have t21 : O.le K.zero (VGoodTail.tt K X (6) (23)) := VGoodTail.tail_nn K O X hg (6) 16
  have et21 : (VGoodTail.tt K X (6) (23)) = (K.add (K.mul (co K X (6)) (co K X (22))) (K.neg (K.mul (co K X (5)) (co K X (23))))) := rfl
  have t22 : O.le K.zero (VGoodTail.tt K X (6) (21)) := VGoodTail.tail_nn K O X hg (6) 14
  have et22 : (VGoodTail.tt K X (6) (21)) = (K.add (K.mul (co K X (6)) (co K X (20))) (K.neg (K.mul (co K X (5)) (co K X (21))))) := rfl
  have t23 : O.le K.zero (VGoodTail.tt K X (6) (19)) := VGoodTail.tail_nn K O X hg (6) 12
  have et23 : (VGoodTail.tt K X (6) (19)) = (K.add (K.mul (co K X (6)) (co K X (18))) (K.neg (K.mul (co K X (5)) (co K X (19))))) := rfl
  have t24 : O.le K.zero (VGoodTail.tt K X (6) (17)) := VGoodTail.tail_nn K O X hg (6) 10
  have et24 : (VGoodTail.tt K X (6) (17)) = (K.add (K.mul (co K X (6)) (co K X (16))) (K.neg (K.mul (co K X (5)) (co K X (17))))) := rfl
  have t25 : O.le K.zero (VGoodTail.tt K X (13) (24)) := VGoodTail.tail_nn K O X hg (13) 10
  have et25 : (VGoodTail.tt K X (13) (24)) = (K.add (K.mul (co K X (13)) (co K X (23))) (K.neg (K.mul (co K X (12)) (co K X (24))))) := rfl
  have t26 : O.le K.zero (VGoodTail.tt K X (13) (22)) := VGoodTail.tail_nn K O X hg (13) 8
  have et26 : (VGoodTail.tt K X (13) (22)) = (K.add (K.mul (co K X (13)) (co K X (21))) (K.neg (K.mul (co K X (12)) (co K X (22))))) := rfl
  have t27 : O.le K.zero (VGoodTail.tt K X (13) (20)) := VGoodTail.tail_nn K O X hg (13) 6
  have et27 : (VGoodTail.tt K X (13) (20)) = (K.add (K.mul (co K X (13)) (co K X (19))) (K.neg (K.mul (co K X (12)) (co K X (20))))) := rfl
  have t28 : O.le K.zero (VGoodTail.tt K X (13) (18)) := VGoodTail.tail_nn K O X hg (13) 4
  have et28 : (VGoodTail.tt K X (13) (18)) = (K.add (K.mul (co K X (13)) (co K X (17))) (K.neg (K.mul (co K X (12)) (co K X (18))))) := rfl
  have t29 : O.le K.zero (VGoodTail.tt K X (13) (16)) := VGoodTail.tail_nn K O X hg (13) 2
  have et29 : (VGoodTail.tt K X (13) (16)) = (K.add (K.mul (co K X (13)) (co K X (15))) (K.neg (K.mul (co K X (12)) (co K X (16))))) := rfl
  have t30 : O.le K.zero (VGoodTail.tt K X (11) (24)) := VGoodTail.tail_nn K O X hg (11) 12
  have et30 : (VGoodTail.tt K X (11) (24)) = (K.add (K.mul (co K X (11)) (co K X (23))) (K.neg (K.mul (co K X (10)) (co K X (24))))) := rfl
  have t31 : O.le K.zero (VGoodTail.tt K X (11) (16)) := VGoodTail.tail_nn K O X hg (11) 4
  have et31 : (VGoodTail.tt K X (11) (16)) = (K.add (K.mul (co K X (11)) (co K X (15))) (K.neg (K.mul (co K X (10)) (co K X (16))))) := rfl
  have t32 : O.le K.zero (VGoodTail.tt K X (9) (24)) := VGoodTail.tail_nn K O X hg (9) 14
  have et32 : (VGoodTail.tt K X (9) (24)) = (K.add (K.mul (co K X (9)) (co K X (23))) (K.neg (K.mul (co K X (8)) (co K X (24))))) := rfl
  have t33 : O.le K.zero (VGoodTail.tt K X (9) (16)) := VGoodTail.tail_nn K O X hg (9) 6
  have et33 : (VGoodTail.tt K X (9) (16)) = (K.add (K.mul (co K X (9)) (co K X (15))) (K.neg (K.mul (co K X (8)) (co K X (16))))) := rfl
  have t34 : O.le K.zero (VGoodTail.tt K X (7) (24)) := VGoodTail.tail_nn K O X hg (7) 16
  have et34 : (VGoodTail.tt K X (7) (24)) = (K.add (K.mul (co K X (7)) (co K X (23))) (K.neg (K.mul (co K X (6)) (co K X (24))))) := rfl
  have t35 : O.le K.zero (VGoodTail.tt K X (7) (16)) := VGoodTail.tail_nn K O X hg (7) 8
  have et35 : (VGoodTail.tt K X (7) (16)) = (K.add (K.mul (co K X (7)) (co K X (15))) (K.neg (K.mul (co K X (6)) (co K X (16))))) := rfl
  have hp : 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.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.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (VGoodTail.tt K X (9) (20)) (VGoo  -- (+1444 chars; full line in the Zenodo source)
    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 _ _ (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 _ _ (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   -- (+915 chars; full line in the Zenodo source)
  refine O.le_trans _ _ _ hp (TripleE.le_of_eq' K O _ _ ?_)
  rw [kv, et0, et1, et2, et3, et4, et5, et6, et7, et8, et9, et10, et11, et12, et13, et14, et15, et16, et17, et18, et19, et20, et21, et22, et23, et24, et25, et26, et27, et28, et29, et30, et31, et32, et33, et34, et35]
  ring_z

theorem kna_3_11 (X : List R) (hg : L1Full.Good K O X) :
    O.le K.zero (CBLib.kern K (Fa K X) (Ga K X) 5 (3) (15)) := by
  have kv : CBLib.kern K (Fa K X) (Ga K X) 5 (3) (15) = (K.add (K.add (K.add (K.add (K.add K.zero (K.add (K.mul (co K X (9)) (co K X (20))) (K.neg (K.mul (co K X (21)) (co K X (8)))))) (K.add (K.mul (K.add (co K X (10)) (co K X (8))) (K.add (co K X (21)) (co K X (19)))) (K.neg (K.mul (K.add (co K X (22)) (co K X (20))) (K.add (co K X (9)) (co K X (7))))))) (K.add (K.mul (K.add (K.add (co K X (11))  -- (+967 chars; full line in the Zenodo source)
  have t0 : O.le K.zero (VGoodTail.tt K X (9) (21)) := VGoodTail.tail_nn K O X hg (9) 11
  have et0 : (VGoodTail.tt K X (9) (21)) = (K.add (K.mul (co K X (9)) (co K X (20))) (K.neg (K.mul (co K X (8)) (co K X (21))))) := rfl
  have t1 : O.le K.zero (VGoodTail.tt K X (10) (22)) := VGoodTail.tail_nn K O X hg (10) 11
  have et1 : (VGoodTail.tt K X (10) (22)) = (K.add (K.mul (co K X (10)) (co K X (21))) (K.neg (K.mul (co K X (9)) (co K X (22))))) := rfl
  have t2 : O.le K.zero (VGoodTail.tt K X (10) (20)) := VGoodTail.tail_nn K O X hg (10) 9
  have et2 : (VGoodTail.tt K X (10) (20)) = (K.add (K.mul (co K X (10)) (co K X (19))) (K.neg (K.mul (co K X (9)) (co K X (20))))) := rfl
  have t3 : O.le K.zero (VGoodTail.tt K X (8) (22)) := VGoodTail.tail_nn K O X hg (8) 13
  have et3 : (VGoodTail.tt K X (8) (22)) = (K.add (K.mul (co K X (8)) (co K X (21))) (K.neg (K.mul (co K X (7)) (co K X (22))))) := rfl
  have t4 : O.le K.zero (VGoodTail.tt K X (8) (20)) := VGoodTail.tail_nn K O X hg (8) 11
  have et4 : (VGoodTail.tt K X (8) (20)) = (K.add (K.mul (co K X (8)) (co K X (19))) (K.neg (K.mul (co K X (7)) (co K X (20))))) := rfl
  have t5 : O.le K.zero (VGoodTail.tt K X (11) (23)) := VGoodTail.tail_nn K O X hg (11) 11
  have et5 : (VGoodTail.tt K X (11) (23)) = (K.add (K.mul (co K X (11)) (co K X (22))) (K.neg (K.mul (co K X (10)) (co K X (23))))) := rfl
  have t6 : O.le K.zero (VGoodTail.tt K X (11) (21)) := VGoodTail.tail_nn K O X hg (11) 9
  have et6 : (VGoodTail.tt K X (11) (21)) = (K.add (K.mul (co K X (11)) (co K X (20))) (K.neg (K.mul (co K X (10)) (co K X (21))))) := rfl
  have t7 : O.le K.zero (VGoodTail.tt K X (11) (19)) := VGoodTail.tail_nn K O X hg (11) 7
  have et7 : (VGoodTail.tt K X (11) (19)) = (K.add (K.mul (co K X (11)) (co K X (18))) (K.neg (K.mul (co K X (10)) (co K X (19))))) := rfl
  have t8 : O.le K.zero (VGoodTail.tt K X (9) (23)) := VGoodTail.tail_nn K O X hg (9) 13
  have et8 : (VGoodTail.tt K X (9) (23)) = (K.add (K.mul (co K X (9)) (co K X (22))) (K.neg (K.mul (co K X (8)) (co K X (23))))) := rfl
  have t9 : O.le K.zero (VGoodTail.tt K X (9) (19)) := VGoodTail.tail_nn K O X hg (9) 9
  have et9 : (VGoodTail.tt K X (9) (19)) = (K.add (K.mul (co K X (9)) (co K X (18))) (K.neg (K.mul (co K X (8)) (co K X (19))))) := rfl
  have t10 : O.le K.zero (VGoodTail.tt K X (7) (23)) := VGoodTail.tail_nn K O X hg (7) 15
  have et10 : (VGoodTail.tt K X (7) (23)) = (K.add (K.mul (co K X (7)) (co K X (22))) (K.neg (K.mul (co K X (6)) (co K X (23))))) := rfl
  have t11 : O.le K.zero (VGoodTail.tt K X (7) (21)) := VGoodTail.tail_nn K O X hg (7) 13
  have et11 : (VGoodTail.tt K X (7) (21)) = (K.add (K.mul (co K X (7)) (co K X (20))) (K.neg (K.mul (co K X (6)) (co K X (21))))) := rfl
  have t12 : O.le K.zero (VGoodTail.tt K X (7) (19)) := VGoodTail.tail_nn K O X hg (7) 11
  have et12 : (VGoodTail.tt K X (7) (19)) = (K.add (K.mul (co K X (7)) (co K X (18))) (K.neg (K.mul (co K X (6)) (co K X (19))))) := rfl
  have t13 : O.le K.zero (VGoodTail.tt K X (12) (24)) := VGoodTail.tail_nn K O X hg (12) 11
  have et13 : (VGoodTail.tt K X (12) (24)) = (K.add (K.mul (co K X (12)) (co K X (23))) (K.neg (K.mul (co K X (11)) (co K X (24))))) := rfl
  have t14 : O.le K.zero (VGoodTail.tt K X (12) (22)) := VGoodTail.tail_nn K O X hg (12) 9
  have et14 : (VGoodTail.tt K X (12) (22)) = (K.add (K.mul (co K X (12)) (co K X (21))) (K.neg (K.mul (co K X (11)) (co K X (22))))) := rfl
  have t15 : O.le K.zero (VGoodTail.tt K X (12) (20)) := VGoodTail.tail_nn K O X hg (12) 7
  have et15 : (VGoodTail.tt K X (12) (20)) = (K.add (K.mul (co K X (12)) (co K X (19))) (K.neg (K.mul (co K X (11)) (co K X (20))))) := rfl
  have t16 : O.le K.zero (VGoodTail.tt K X (12) (18)) := VGoodTail.tail_nn K O X hg (12) 5
  have et16 : (VGoodTail.tt K X (12) (18)) = (K.add (K.mul (co K X (12)) (co K X (17))) (K.neg (K.mul (co K X (11)) (co K X (18))))) := rfl
  have t17 : O.le K.zero (VGoodTail.tt K X (10) (24)) := VGoodTail.tail_nn K O X hg (10) 13
  have et17 : (VGoodTail.tt K X (10) (24)) = (K.add (K.mul (co K X (10)) (co K X (23))) (K.neg (K.mul (co K X (9)) (co K X (24))))) := rfl
  have t18 : O.le K.zero (VGoodTail.tt K X (10) (18)) := VGoodTail.tail_nn K O X hg (10) 7
  have et18 : (VGoodTail.tt K X (10) (18)) = (K.add (K.mul (co K X (10)) (co K X (17))) (K.neg (K.mul (co K X (9)) (co K X (18))))) := rfl
  have t19 : O.le K.zero (VGoodTail.tt K X (8) (24)) := VGoodTail.tail_nn K O X hg (8) 15
  have et19 : (VGoodTail.tt K X (8) (24)) = (K.add (K.mul (co K X (8)) (co K X (23))) (K.neg (K.mul (co K X (7)) (co K X (24))))) := rfl
  have t20 : O.le K.zero (VGoodTail.tt K X (8) (18)) := VGoodTail.tail_nn K O X hg (8) 9
  have et20 : (VGoodTail.tt K X (8) (18)) = (K.add (K.mul (co K X (8)) (co K X (17))) (K.neg (K.mul (co K X (7)) (co K X (18))))) := rfl
  have t21 : O.le K.zero (VGoodTail.tt K X (6) (24)) := VGoodTail.tail_nn K O X hg (6) 17
  have et21 : (VGoodTail.tt K X (6) (24)) = (K.add (K.mul (co K X (6)) (co K X (23))) (K.neg (K.mul (co K X (5)) (co K X (24))))) := rfl
  have t22 : O.le K.zero (VGoodTail.tt K X (6) (22)) := VGoodTail.tail_nn K O X hg (6) 15
  have et22 : (VGoodTail.tt K X (6) (22)) = (K.add (K.mul (co K X (6)) (co K X (21))) (K.neg (K.mul (co K X (5)) (co K X (22))))) := rfl
  have t23 : O.le K.zero (VGoodTail.tt K X (6) (20)) := VGoodTail.tail_nn K O X hg (6) 13
  have et23 : (VGoodTail.tt K X (6) (20)) = (K.add (K.mul (co K X (6)) (co K X (19))) (K.neg (K.mul (co K X (5)) (co K X (20))))) := rfl
  have t24 : O.le K.zero (VGoodTail.tt K X (6) (18)) := VGoodTail.tail_nn K O X hg (6) 11
  have et24 : (VGoodTail.tt K X (6) (18)) = (K.add (K.mul (co K X (6)) (co K X (17))) (K.neg (K.mul (co K X (5)) (co K X (18))))) := rfl
  have t25 : O.le K.zero (VGoodTail.tt K X (13) (25)) := VGoodTail.tail_nn K O X hg (13) 11
  have et25 : (VGoodTail.tt K X (13) (25)) = (K.add (K.mul (co K X (13)) (co K X (24))) (K.neg (K.mul (co K X (12)) (co K X (25))))) := rfl
  have t26 : O.le K.zero (VGoodTail.tt K X (13) (23)) := VGoodTail.tail_nn K O X hg (13) 9
  have et26 : (VGoodTail.tt K X (13) (23)) = (K.add (K.mul (co K X (13)) (co K X (22))) (K.neg (K.mul (co K X (12)) (co K X (23))))) := rfl
  have t27 : O.le K.zero (VGoodTail.tt K X (13) (21)) := VGoodTail.tail_nn K O X hg (13) 7
  have et27 : (VGoodTail.tt K X (13) (21)) = (K.add (K.mul (co K X (13)) (co K X (20))) (K.neg (K.mul (co K X (12)) (co K X (21))))) := rfl
  have t28 : O.le K.zero (VGoodTail.tt K X (13) (19)) := VGoodTail.tail_nn K O X hg (13) 5
  have et28 : (VGoodTail.tt K X (13) (19)) = (K.add (K.mul (co K X (13)) (co K X (18))) (K.neg (K.mul (co K X (12)) (co K X (19))))) := rfl
  have t29 : O.le K.zero (VGoodTail.tt K X (13) (17)) := VGoodTail.tail_nn K O X hg (13) 3
  have et29 : (VGoodTail.tt K X (13) (17)) = (K.add (K.mul (co K X (13)) (co K X (16))) (K.neg (K.mul (co K X (12)) (co K X (17))))) := rfl
  have t30 : O.le K.zero (VGoodTail.tt K X (11) (25)) := VGoodTail.tail_nn K O X hg (11) 13
  have et30 : (VGoodTail.tt K X (11) (25)) = (K.add (K.mul (co K X (11)) (co K X (24))) (K.neg (K.mul (co K X (10)) (co K X (25))))) := rfl
  have t31 : O.le K.zero (VGoodTail.tt K X (11) (17)) := VGoodTail.tail_nn K O X hg (11) 5
  have et31 : (VGoodTail.tt K X (11) (17)) = (K.add (K.mul (co K X (11)) (co K X (16))) (K.neg (K.mul (co K X (10)) (co K X (17))))) := rfl
  have t32 : O.le K.zero (VGoodTail.tt K X (9) (25)) := VGoodTail.tail_nn K O X hg (9) 15
  have et32 : (VGoodTail.tt K X (9) (25)) = (K.add (K.mul (co K X (9)) (co K X (24))) (K.neg (K.mul (co K X (8)) (co K X (25))))) := rfl
  have t33 : O.le K.zero (VGoodTail.tt K X (9) (17)) := VGoodTail.tail_nn K O X hg (9) 7
  have et33 : (VGoodTail.tt K X (9) (17)) = (K.add (K.mul (co K X (9)) (co K X (16))) (K.neg (K.mul (co K X (8)) (co K X (17))))) := rfl
  have t34 : O.le K.zero (VGoodTail.tt K X (7) (25)) := VGoodTail.tail_nn K O X hg (7) 17
  have et34 : (VGoodTail.tt K X (7) (25)) = (K.add (K.mul (co K X (7)) (co K X (24))) (K.neg (K.mul (co K X (6)) (co K X (25))))) := rfl
  have t35 : O.le K.zero (VGoodTail.tt K X (7) (17)) := VGoodTail.tail_nn K O X hg (7) 9
  have et35 : (VGoodTail.tt K X (7) (17)) = (K.add (K.mul (co K X (7)) (co K X (16))) (K.neg (K.mul (co K X (6)) (co K X (17))))) := rfl
  have hp : 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.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.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (VGoodTail.tt K X (9) (21)) (VGoo  -- (+1444 chars; full line in the Zenodo source)
    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 _ _ (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 _ _ (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   -- (+915 chars; full line in the Zenodo source)
  refine O.le_trans _ _ _ hp (TripleE.le_of_eq' K O _ _ ?_)
  rw [kv, et0, et1, et2, et3, et4, et5, et6, et7, et8, et9, et10, et11, et12, et13, et14, et15, et16, et17, et18, et19, et20, et21, et22, et23, et24, et25, et26, et27, et28, et29, et30, et31, et32, et33, et34, et35]
  ring_z

theorem kna_3_12 (X : List R) (hg : L1Full.Good K O X) :
    O.le K.zero (CBLib.kern K (Fa K X) (Ga K X) 5 (3) (16)) := by
  have kv : CBLib.kern K (Fa K X) (Ga K X) 5 (3) (16) = (K.add (K.add (K.add (K.add (K.add K.zero (K.add (K.mul (co K X (9)) (co K X (21))) (K.neg (K.mul (co K X (22)) (co K X (8)))))) (K.add (K.mul (K.add (co K X (10)) (co K X (8))) (K.add (co K X (22)) (co K X (20)))) (K.neg (K.mul (K.add (co K X (23)) (co K X (21))) (K.add (co K X (9)) (co K X (7))))))) (K.add (K.mul (K.add (K.add (co K X (11))  -- (+967 chars; full line in the Zenodo source)
  have t0 : O.le K.zero (VGoodTail.tt K X (9) (22)) := VGoodTail.tail_nn K O X hg (9) 12
  have et0 : (VGoodTail.tt K X (9) (22)) = (K.add (K.mul (co K X (9)) (co K X (21))) (K.neg (K.mul (co K X (8)) (co K X (22))))) := rfl
  have t1 : O.le K.zero (VGoodTail.tt K X (10) (23)) := VGoodTail.tail_nn K O X hg (10) 12
  have et1 : (VGoodTail.tt K X (10) (23)) = (K.add (K.mul (co K X (10)) (co K X (22))) (K.neg (K.mul (co K X (9)) (co K X (23))))) := rfl
  have t2 : O.le K.zero (VGoodTail.tt K X (10) (21)) := VGoodTail.tail_nn K O X hg (10) 10
  have et2 : (VGoodTail.tt K X (10) (21)) = (K.add (K.mul (co K X (10)) (co K X (20))) (K.neg (K.mul (co K X (9)) (co K X (21))))) := rfl
  have t3 : O.le K.zero (VGoodTail.tt K X (8) (23)) := VGoodTail.tail_nn K O X hg (8) 14
  have et3 : (VGoodTail.tt K X (8) (23)) = (K.add (K.mul (co K X (8)) (co K X (22))) (K.neg (K.mul (co K X (7)) (co K X (23))))) := rfl
  have t4 : O.le K.zero (VGoodTail.tt K X (8) (21)) := VGoodTail.tail_nn K O X hg (8) 12
  have et4 : (VGoodTail.tt K X (8) (21)) = (K.add (K.mul (co K X (8)) (co K X (20))) (K.neg (K.mul (co K X (7)) (co K X (21))))) := rfl
  have t5 : O.le K.zero (VGoodTail.tt K X (11) (24)) := VGoodTail.tail_nn K O X hg (11) 12
  have et5 : (VGoodTail.tt K X (11) (24)) = (K.add (K.mul (co K X (11)) (co K X (23))) (K.neg (K.mul (co K X (10)) (co K X (24))))) := rfl
  have t6 : O.le K.zero (VGoodTail.tt K X (11) (22)) := VGoodTail.tail_nn K O X hg (11) 10
  have et6 : (VGoodTail.tt K X (11) (22)) = (K.add (K.mul (co K X (11)) (co K X (21))) (K.neg (K.mul (co K X (10)) (co K X (22))))) := rfl
  have t7 : O.le K.zero (VGoodTail.tt K X (11) (20)) := VGoodTail.tail_nn K O X hg (11) 8
  have et7 : (VGoodTail.tt K X (11) (20)) = (K.add (K.mul (co K X (11)) (co K X (19))) (K.neg (K.mul (co K X (10)) (co K X (20))))) := rfl
  have t8 : O.le K.zero (VGoodTail.tt K X (9) (24)) := VGoodTail.tail_nn K O X hg (9) 14
  have et8 : (VGoodTail.tt K X (9) (24)) = (K.add (K.mul (co K X (9)) (co K X (23))) (K.neg (K.mul (co K X (8)) (co K X (24))))) := rfl
  have t9 : O.le K.zero (VGoodTail.tt K X (9) (20)) := VGoodTail.tail_nn K O X hg (9) 10
  have et9 : (VGoodTail.tt K X (9) (20)) = (K.add (K.mul (co K X (9)) (co K X (19))) (K.neg (K.mul (co K X (8)) (co K X (20))))) := rfl
  have t10 : O.le K.zero (VGoodTail.tt K X (7) (24)) := VGoodTail.tail_nn K O X hg (7) 16
  have et10 : (VGoodTail.tt K X (7) (24)) = (K.add (K.mul (co K X (7)) (co K X (23))) (K.neg (K.mul (co K X (6)) (co K X (24))))) := rfl
  have t11 : O.le K.zero (VGoodTail.tt K X (7) (22)) := VGoodTail.tail_nn K O X hg (7) 14
  have et11 : (VGoodTail.tt K X (7) (22)) = (K.add (K.mul (co K X (7)) (co K X (21))) (K.neg (K.mul (co K X (6)) (co K X (22))))) := rfl
  have t12 : O.le K.zero (VGoodTail.tt K X (7) (20)) := VGoodTail.tail_nn K O X hg (7) 12
  have et12 : (VGoodTail.tt K X (7) (20)) = (K.add (K.mul (co K X (7)) (co K X (19))) (K.neg (K.mul (co K X (6)) (co K X (20))))) := rfl
  have t13 : O.le K.zero (VGoodTail.tt K X (12) (25)) := VGoodTail.tail_nn K O X hg (12) 12
  have et13 : (VGoodTail.tt K X (12) (25)) = (K.add (K.mul (co K X (12)) (co K X (24))) (K.neg (K.mul (co K X (11)) (co K X (25))))) := rfl
  have t14 : O.le K.zero (VGoodTail.tt K X (12) (23)) := VGoodTail.tail_nn K O X hg (12) 10
  have et14 : (VGoodTail.tt K X (12) (23)) = (K.add (K.mul (co K X (12)) (co K X (22))) (K.neg (K.mul (co K X (11)) (co K X (23))))) := rfl
  have t15 : O.le K.zero (VGoodTail.tt K X (12) (21)) := VGoodTail.tail_nn K O X hg (12) 8
  have et15 : (VGoodTail.tt K X (12) (21)) = (K.add (K.mul (co K X (12)) (co K X (20))) (K.neg (K.mul (co K X (11)) (co K X (21))))) := rfl
  have t16 : O.le K.zero (VGoodTail.tt K X (12) (19)) := VGoodTail.tail_nn K O X hg (12) 6
  have et16 : (VGoodTail.tt K X (12) (19)) = (K.add (K.mul (co K X (12)) (co K X (18))) (K.neg (K.mul (co K X (11)) (co K X (19))))) := rfl
  have t17 : O.le K.zero (VGoodTail.tt K X (10) (25)) := VGoodTail.tail_nn K O X hg (10) 14
  have et17 : (VGoodTail.tt K X (10) (25)) = (K.add (K.mul (co K X (10)) (co K X (24))) (K.neg (K.mul (co K X (9)) (co K X (25))))) := rfl
  have t18 : O.le K.zero (VGoodTail.tt K X (10) (19)) := VGoodTail.tail_nn K O X hg (10) 8
  have et18 : (VGoodTail.tt K X (10) (19)) = (K.add (K.mul (co K X (10)) (co K X (18))) (K.neg (K.mul (co K X (9)) (co K X (19))))) := rfl
  have t19 : O.le K.zero (VGoodTail.tt K X (8) (25)) := VGoodTail.tail_nn K O X hg (8) 16
  have et19 : (VGoodTail.tt K X (8) (25)) = (K.add (K.mul (co K X (8)) (co K X (24))) (K.neg (K.mul (co K X (7)) (co K X (25))))) := rfl
  have t20 : O.le K.zero (VGoodTail.tt K X (8) (19)) := VGoodTail.tail_nn K O X hg (8) 10
  have et20 : (VGoodTail.tt K X (8) (19)) = (K.add (K.mul (co K X (8)) (co K X (18))) (K.neg (K.mul (co K X (7)) (co K X (19))))) := rfl
  have t21 : O.le K.zero (VGoodTail.tt K X (6) (25)) := VGoodTail.tail_nn K O X hg (6) 18
  have et21 : (VGoodTail.tt K X (6) (25)) = (K.add (K.mul (co K X (6)) (co K X (24))) (K.neg (K.mul (co K X (5)) (co K X (25))))) := rfl
  have t22 : O.le K.zero (VGoodTail.tt K X (6) (23)) := VGoodTail.tail_nn K O X hg (6) 16
  have et22 : (VGoodTail.tt K X (6) (23)) = (K.add (K.mul (co K X (6)) (co K X (22))) (K.neg (K.mul (co K X (5)) (co K X (23))))) := rfl
  have t23 : O.le K.zero (VGoodTail.tt K X (6) (21)) := VGoodTail.tail_nn K O X hg (6) 14
  have et23 : (VGoodTail.tt K X (6) (21)) = (K.add (K.mul (co K X (6)) (co K X (20))) (K.neg (K.mul (co K X (5)) (co K X (21))))) := rfl
  have t24 : O.le K.zero (VGoodTail.tt K X (6) (19)) := VGoodTail.tail_nn K O X hg (6) 12
  have et24 : (VGoodTail.tt K X (6) (19)) = (K.add (K.mul (co K X (6)) (co K X (18))) (K.neg (K.mul (co K X (5)) (co K X (19))))) := rfl
  have t25 : O.le K.zero (VGoodTail.tt K X (13) (26)) := VGoodTail.tail_nn K O X hg (13) 12
  have et25 : (VGoodTail.tt K X (13) (26)) = (K.add (K.mul (co K X (13)) (co K X (25))) (K.neg (K.mul (co K X (12)) (co K X (26))))) := rfl
  have t26 : O.le K.zero (VGoodTail.tt K X (13) (24)) := VGoodTail.tail_nn K O X hg (13) 10
  have et26 : (VGoodTail.tt K X (13) (24)) = (K.add (K.mul (co K X (13)) (co K X (23))) (K.neg (K.mul (co K X (12)) (co K X (24))))) := rfl
  have t27 : O.le K.zero (VGoodTail.tt K X (13) (22)) := VGoodTail.tail_nn K O X hg (13) 8
  have et27 : (VGoodTail.tt K X (13) (22)) = (K.add (K.mul (co K X (13)) (co K X (21))) (K.neg (K.mul (co K X (12)) (co K X (22))))) := rfl
  have t28 : O.le K.zero (VGoodTail.tt K X (13) (20)) := VGoodTail.tail_nn K O X hg (13) 6
  have et28 : (VGoodTail.tt K X (13) (20)) = (K.add (K.mul (co K X (13)) (co K X (19))) (K.neg (K.mul (co K X (12)) (co K X (20))))) := rfl
  have t29 : O.le K.zero (VGoodTail.tt K X (13) (18)) := VGoodTail.tail_nn K O X hg (13) 4
  have et29 : (VGoodTail.tt K X (13) (18)) = (K.add (K.mul (co K X (13)) (co K X (17))) (K.neg (K.mul (co K X (12)) (co K X (18))))) := rfl
  have t30 : O.le K.zero (VGoodTail.tt K X (11) (26)) := VGoodTail.tail_nn K O X hg (11) 14
  have et30 : (VGoodTail.tt K X (11) (26)) = (K.add (K.mul (co K X (11)) (co K X (25))) (K.neg (K.mul (co K X (10)) (co K X (26))))) := rfl
  have t31 : O.le K.zero (VGoodTail.tt K X (11) (18)) := VGoodTail.tail_nn K O X hg (11) 6
  have et31 : (VGoodTail.tt K X (11) (18)) = (K.add (K.mul (co K X (11)) (co K X (17))) (K.neg (K.mul (co K X (10)) (co K X (18))))) := rfl
  have t32 : O.le K.zero (VGoodTail.tt K X (9) (26)) := VGoodTail.tail_nn K O X hg (9) 16
  have et32 : (VGoodTail.tt K X (9) (26)) = (K.add (K.mul (co K X (9)) (co K X (25))) (K.neg (K.mul (co K X (8)) (co K X (26))))) := rfl
  have t33 : O.le K.zero (VGoodTail.tt K X (9) (18)) := VGoodTail.tail_nn K O X hg (9) 8
  have et33 : (VGoodTail.tt K X (9) (18)) = (K.add (K.mul (co K X (9)) (co K X (17))) (K.neg (K.mul (co K X (8)) (co K X (18))))) := rfl
  have t34 : O.le K.zero (VGoodTail.tt K X (7) (26)) := VGoodTail.tail_nn K O X hg (7) 18
  have et34 : (VGoodTail.tt K X (7) (26)) = (K.add (K.mul (co K X (7)) (co K X (25))) (K.neg (K.mul (co K X (6)) (co K X (26))))) := rfl
  have t35 : O.le K.zero (VGoodTail.tt K X (7) (18)) := VGoodTail.tail_nn K O X hg (7) 10
  have et35 : (VGoodTail.tt K X (7) (18)) = (K.add (K.mul (co K X (7)) (co K X (17))) (K.neg (K.mul (co K X (6)) (co K X (18))))) := rfl
  have hp : 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.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.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (VGoodTail.tt K X (9) (22)) (VGoo  -- (+1444 chars; full line in the Zenodo source)
    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 _ _ (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 _ _ (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   -- (+915 chars; full line in the Zenodo source)
  refine O.le_trans _ _ _ hp (TripleE.le_of_eq' K O _ _ ?_)
  rw [kv, et0, et1, et2, et3, et4, et5, et6, et7, et8, et9, et10, et11, et12, et13, et14, et15, et16, et17, et18, et19, et20, et21, et22, et23, et24, et25, et26, et27, et28, et29, et30, et31, et32, et33, et34, et35]
  ring_z

theorem kna_s_0 (X : List R) (hg : L1Full.Good K O X) (u' : Nat) :
    O.le K.zero (CBLib.kern K (Fa K X) (Ga K X) 5 (u' + 4) (u' + 5)) := by
  have kv : CBLib.kern K (Fa K X) (Ga K X) 5 (u' + 4) (u' + 5) = (K.add (K.add (K.add (K.add (K.add K.zero (K.add (K.mul (co K X (u' + 10)) (co K X (u' + 10))) (K.neg (K.mul (co K X (u' + 11)) (co K X (u' + 9)))))) (K.add (K.mul (K.add (co K X (u' + 11)) (co K X (u' + 9))) (K.add (co K X (u' + 11)) (co K X (u' + 9)))) (K.neg (K.mul (K.add (co K X (u' + 12)) (co K X (u' + 10))) (K.add (co K X (u' +  -- (+1313 chars; full line in the Zenodo source)
  have t0 : O.le K.zero (VGoodTail.tt K X (u' + 10) (u' + 11)) := VGoodTail.tail_nn K O X hg (u' + 10) 0
  have et0 : (VGoodTail.tt K X (u' + 10) (u' + 11)) = (K.add (K.mul (co K X (u' + 10)) (co K X (u' + 10))) (K.neg (K.mul (co K X (u' + 9)) (co K X (u' + 11))))) := rfl
  have t1 : O.le K.zero (VGoodTail.tt K X (u' + 12) (u' + 13)) := VGoodTail.tail_nn K O X hg (u' + 12) 0
  have et1 : (VGoodTail.tt K X (u' + 12) (u' + 13)) = (K.add (K.mul (co K X (u' + 12)) (co K X (u' + 12))) (K.neg (K.mul (co K X (u' + 11)) (co K X (u' + 13))))) := rfl
  have t2 : O.le K.zero (VGoodTail.tt K X (u' + 10) (u' + 13)) := VGoodTail.tail_nn K O X hg (u' + 10) 2
  have et2 : (VGoodTail.tt K X (u' + 10) (u' + 13)) = (K.add (K.mul (co K X (u' + 10)) (co K X (u' + 12))) (K.neg (K.mul (co K X (u' + 9)) (co K X (u' + 13))))) := rfl
  have t3 : O.le K.zero (VGoodTail.tt K X (u' + 8) (u' + 13)) := VGoodTail.tail_nn K O X hg (u' + 8) 4
  have et3 : (VGoodTail.tt K X (u' + 8) (u' + 13)) = (K.add (K.mul (co K X (u' + 8)) (co K X (u' + 12))) (K.neg (K.mul (co K X (u' + 7)) (co K X (u' + 13))))) := rfl
  have t4 : O.le K.zero (VGoodTail.tt K X (u' + 8) (u' + 11)) := VGoodTail.tail_nn K O X hg (u' + 8) 2
  have et4 : (VGoodTail.tt K X (u' + 8) (u' + 11)) = (K.add (K.mul (co K X (u' + 8)) (co K X (u' + 10))) (K.neg (K.mul (co K X (u' + 7)) (co K X (u' + 11))))) := rfl
  have t5 : O.le K.zero (VGoodTail.tt K X (u' + 8) (u' + 9)) := VGoodTail.tail_nn K O X hg (u' + 8) 0
  have et5 : (VGoodTail.tt K X (u' + 8) (u' + 9)) = (K.add (K.mul (co K X (u' + 8)) (co K X (u' + 8))) (K.neg (K.mul (co K X (u' + 7)) (co K X (u' + 9))))) := rfl
  have t6 : O.le K.zero (VGoodTail.tt K X (u' + 14) (u' + 15)) := VGoodTail.tail_nn K O X hg (u' + 14) 0
  have et6 : (VGoodTail.tt K X (u' + 14) (u' + 15)) = (K.add (K.mul (co K X (u' + 14)) (co K X (u' + 14))) (K.neg (K.mul (co K X (u' + 13)) (co K X (u' + 15))))) := rfl
  have t7 : O.le K.zero (VGoodTail.tt K X (u' + 12) (u' + 15)) := VGoodTail.tail_nn K O X hg (u' + 12) 2
  have et7 : (VGoodTail.tt K X (u' + 12) (u' + 15)) = (K.add (K.mul (co K X (u' + 12)) (co K X (u' + 14))) (K.neg (K.mul (co K X (u' + 11)) (co K X (u' + 15))))) := rfl
  have t8 : O.le K.zero (VGoodTail.tt K X (u' + 10) (u' + 15)) := VGoodTail.tail_nn K O X hg (u' + 10) 4
  have et8 : (VGoodTail.tt K X (u' + 10) (u' + 15)) = (K.add (K.mul (co K X (u' + 10)) (co K X (u' + 14))) (K.neg (K.mul (co K X (u' + 9)) (co K X (u' + 15))))) := rfl
  have t9 : O.le K.zero (VGoodTail.tt K X (u' + 8) (u' + 15)) := VGoodTail.tail_nn K O X hg (u' + 8) 6
  have et9 : (VGoodTail.tt K X (u' + 8) (u' + 15)) = (K.add (K.mul (co K X (u' + 8)) (co K X (u' + 14))) (K.neg (K.mul (co K X (u' + 7)) (co K X (u' + 15))))) := rfl
  have t10 : O.le K.zero (VGoodTail.tt K X (u' + 6) (u' + 15)) := VGoodTail.tail_nn K O X hg (u' + 6) 8
  have et10 : (VGoodTail.tt K X (u' + 6) (u' + 15)) = (K.add (K.mul (co K X (u' + 6)) (co K X (u' + 14))) (K.neg (K.mul (co K X (u' + 5)) (co K X (u' + 15))))) := rfl
  have t11 : O.le K.zero (VGoodTail.tt K X (u' + 6) (u' + 13)) := VGoodTail.tail_nn K O X hg (u' + 6) 6
  have et11 : (VGoodTail.tt K X (u' + 6) (u' + 13)) = (K.add (K.mul (co K X (u' + 6)) (co K X (u' + 12))) (K.neg (K.mul (co K X (u' + 5)) (co K X (u' + 13))))) := rfl
  have t12 : O.le K.zero (VGoodTail.tt K X (u' + 6) (u' + 11)) := VGoodTail.tail_nn K O X hg (u' + 6) 4
  have et12 : (VGoodTail.tt K X (u' + 6) (u' + 11)) = (K.add (K.mul (co K X (u' + 6)) (co K X (u' + 10))) (K.neg (K.mul (co K X (u' + 5)) (co K X (u' + 11))))) := rfl
  have t13 : O.le K.zero (VGoodTail.tt K X (u' + 6) (u' + 9)) := VGoodTail.tail_nn K O X hg (u' + 6) 2
  have et13 : (VGoodTail.tt K X (u' + 6) (u' + 9)) = (K.add (K.mul (co K X (u' + 6)) (co K X (u' + 8))) (K.neg (K.mul (co K X (u' + 5)) (co K X (u' + 9))))) := rfl
  have t14 : O.le K.zero (VGoodTail.tt K X (u' + 6) (u' + 7)) := VGoodTail.tail_nn K O X hg (u' + 6) 0
  have et14 : (VGoodTail.tt K X (u' + 6) (u' + 7)) = (K.add (K.mul (co K X (u' + 6)) (co K X (u' + 6))) (K.neg (K.mul (co K X (u' + 5)) (co K X (u' + 7))))) := rfl
  have hp : 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.add (VGoodTail.tt K X (u' + 10) (u' + 11)) (VGoodTail.tt K X (u' + 12) (u' + 13))) (VGoodTail.tt K X (u' + 10) (u' + 13))) (VGoodTail.tt K X (u' + 8) (u' + 13))) (VGoodTail.tt K X (u' + 8) (u' + 11))) (VGoodTail.tt K X (u' + 8) (u' + 9))) (VGoodTail.tt K X (u' + 14) (u' + 15))) (VG  -- (+314 chars; full line in the Zenodo source)
    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 _ _ (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 _ _ (TopLC.add_nn K O _ _ (TopLC.add_nn K O _ _ t0 t1) t2) t3) t4) t5) t6) t7) t8) t9) t10) t11) t12) t13) t14)
  refine O.le_trans _ _ _ hp (TripleE.le_of_eq' K O _ _ ?_)
  rw [kv, et0, et1, et2, et3, et4, et5, et6, et7, et8, et9, et10, et11, et12, et13, et14]
  ring_z

theorem kna_s_1 (X : List R) (hg : L1Full.Good K O X) (u' : Nat) :
    O.le K.zero (CBLib.kern K (Fa K X) (Ga K X) 5 (u' + 4) (u' + 6)) := by
  have kv : CBLib.kern K (Fa K X) (Ga K X) 5 (u' + 4) (u' + 6) = (K.add (K.add (K.add (K.add (K.add K.zero (K.add (K.mul (co K X (u' + 10)) (co K X (u' + 11))) (K.neg (K.mul (co K X (u' + 12)) (co K X (u' + 9)))))) (K.add (K.mul (K.add (co K X (u' + 11)) (co K X (u' + 9))) (K.add (co K X (u' + 12)) (co K X (u' + 10)))) (K.neg (K.mul (K.add (co K X (u' + 13)) (co K X (u' + 11))) (K.add (co K X (u'   -- (+1317 chars; full line in the Zenodo source)
  have t0 : O.le K.zero (VGoodTail.tt K X (u' + 10) (u' + 12)) := VGoodTail.tail_nn K O X hg (u' + 10) 1
  have et0 : (VGoodTail.tt K X (u' + 10) (u' + 12)) = (K.add (K.mul (co K X (u' + 10)) (co K X (u' + 11))) (K.neg (K.mul (co K X (u' + 9)) (co K X (u' + 12))))) := rfl
  have t1 : O.le K.zero (VGoodTail.tt K X (u' + 11) (u' + 13)) := VGoodTail.tail_nn K O X hg (u' + 11) 1
  have et1 : (VGoodTail.tt K X (u' + 11) (u' + 13)) = (K.add (K.mul (co K X (u' + 11)) (co K X (u' + 12))) (K.neg (K.mul (co K X (u' + 10)) (co K X (u' + 13))))) := rfl
  have t2 : O.le K.zero (VGoodTail.tt K X (u' + 9) (u' + 13)) := VGoodTail.tail_nn K O X hg (u' + 9) 3
  have et2 : (VGoodTail.tt K X (u' + 9) (u' + 13)) = (K.add (K.mul (co K X (u' + 9)) (co K X (u' + 12))) (K.neg (K.mul (co K X (u' + 8)) (co K X (u' + 13))))) := rfl
  have t3 : O.le K.zero (VGoodTail.tt K X (u' + 9) (u' + 11)) := VGoodTail.tail_nn K O X hg (u' + 9) 1
  have et3 : (VGoodTail.tt K X (u' + 9) (u' + 11)) = (K.add (K.mul (co K X (u' + 9)) (co K X (u' + 10))) (K.neg (K.mul (co K X (u' + 8)) (co K X (u' + 11))))) := rfl
  have t4 : O.le K.zero (VGoodTail.tt K X (u' + 12) (u' + 14)) := VGoodTail.tail_nn K O X hg (u' + 12) 1
  have et4 : (VGoodTail.tt K X (u' + 12) (u' + 14)) = (K.add (K.mul (co K X (u' + 12)) (co K X (u' + 13))) (K.neg (K.mul (co K X (u' + 11)) (co K X (u' + 14))))) := rfl
  have t5 : O.le K.zero (VGoodTail.tt K X (u' + 10) (u' + 14)) := VGoodTail.tail_nn K O X hg (u' + 10) 3
  have et5 : (VGoodTail.tt K X (u' + 10) (u' + 14)) = (K.add (K.mul (co K X (u' + 10)) (co K X (u' + 13))) (K.neg (K.mul (co K X (u' + 9)) (co K X (u' + 14))))) := rfl
  have t6 : O.le K.zero (VGoodTail.tt K X (u' + 8) (u' + 14)) := VGoodTail.tail_nn K O X hg (u' + 8) 5
  have et6 : (VGoodTail.tt K X (u' + 8) (u' + 14)) = (K.add (K.mul (co K X (u' + 8)) (co K X (u' + 13))) (K.neg (K.mul (co K X (u' + 7)) (co K X (u' + 14))))) := rfl
  have t7 : O.le K.zero (VGoodTail.tt K X (u' + 8) (u' + 12)) := VGoodTail.tail_nn K O X hg (u' + 8) 3
  have et7 : (VGoodTail.tt K X (u' + 8) (u' + 12)) = (K.add (K.mul (co K X (u' + 8)) (co K X (u' + 11))) (K.neg (K.mul (co K X (u' + 7)) (co K X (u' + 12))))) := rfl
  have t8 : O.le K.zero (VGoodTail.tt K X (u' + 8) (u' + 10)) := VGoodTail.tail_nn K O X hg (u' + 8) 1
  have et8 : (VGoodTail.tt K X (u' + 8) (u' + 10)) = (K.add (K.mul (co K X (u' + 8)) (co K X (u' + 9))) (K.neg (K.mul (co K X (u' + 7)) (co K X (u' + 10))))) := rfl
  have t9 : O.le K.zero (VGoodTail.tt K X (u' + 13) (u' + 15)) := VGoodTail.tail_nn K O X hg (u' + 13) 1
  have et9 : (VGoodTail.tt K X (u' + 13) (u' + 15)) = (K.add (K.mul (co K X (u' + 13)) (co K X (u' + 14))) (K.neg (K.mul (co K X (u' + 12)) (co K X (u' + 15))))) := rfl
  have t10 : O.le K.zero (VGoodTail.tt K X (u' + 11) (u' + 15)) := VGoodTail.tail_nn K O X hg (u' + 11) 3
  have et10 : (VGoodTail.tt K X (u' + 11) (u' + 15)) = (K.add (K.mul (co K X (u' + 11)) (co K X (u' + 14))) (K.neg (K.mul (co K X (u' + 10)) (co K X (u' + 15))))) := rfl
  have t11 : O.le K.zero (VGoodTail.tt K X (u' + 9) (u' + 15)) := VGoodTail.tail_nn K O X hg (u' + 9) 5
  have et11 : (VGoodTail.tt K X (u' + 9) (u' + 15)) = (K.add (K.mul (co K X (u' + 9)) (co K X (u' + 14))) (K.neg (K.mul (co K X (u' + 8)) (co K X (u' + 15))))) := rfl
  have t12 : O.le K.zero (VGoodTail.tt K X (u' + 7) (u' + 15)) := VGoodTail.tail_nn K O X hg (u' + 7) 7
  have et12 : (VGoodTail.tt K X (u' + 7) (u' + 15)) = (K.add (K.mul (co K X (u' + 7)) (co K X (u' + 14))) (K.neg (K.mul (co K X (u' + 6)) (co K X (u' + 15))))) := rfl
  have t13 : O.le K.zero (VGoodTail.tt K X (u' + 7) (u' + 13)) := VGoodTail.tail_nn K O X hg (u' + 7) 5
  have et13 : (VGoodTail.tt K X (u' + 7) (u' + 13)) = (K.add (K.mul (co K X (u' + 7)) (co K X (u' + 12))) (K.neg (K.mul (co K X (u' + 6)) (co K X (u' + 13))))) := rfl
  have t14 : O.le K.zero (VGoodTail.tt K X (u' + 7) (u' + 11)) := VGoodTail.tail_nn K O X hg (u' + 7) 3
  have et14 : (VGoodTail.tt K X (u' + 7) (u' + 11)) = (K.add (K.mul (co K X (u' + 7)) (co K X (u' + 10))) (K.neg (K.mul (co K X (u' + 6)) (co K X (u' + 11))))) := rfl
  have t15 : O.le K.zero (VGoodTail.tt K X (u' + 7) (u' + 9)) := VGoodTail.tail_nn K O X hg (u' + 7) 1
  have et15 : (VGoodTail.tt K X (u' + 7) (u' + 9)) = (K.add (K.mul (co K X (u' + 7)) (co K X (u' + 8))) (K.neg (K.mul (co K X (u' + 6)) (co K X (u' + 9))))) := rfl
  have t16 : O.le K.zero (VGoodTail.tt K X (u' + 14) (u' + 16)) := VGoodTail.tail_nn K O X hg (u' + 14) 1
  have et16 : (VGoodTail.tt K X (u' + 14) (u' + 16)) = (K.add (K.mul (co K X (u' + 14)) (co K X (u' + 15))) (K.neg (K.mul (co K X (u' + 13)) (co K X (u' + 16))))) := rfl
  have t17 : O.le K.zero (VGoodTail.tt K X (u' + 12) (u' + 16)) := VGoodTail.tail_nn K O X hg (u' + 12) 3
  have et17 : (VGoodTail.tt K X (u' + 12) (u' + 16)) = (K.add (K.mul (co K X (u' + 12)) (co K X (u' + 15))) (K.neg (K.mul (co K X (u' + 11)) (co K X (u' + 16))))) := rfl
  have t18 : O.le K.zero (VGoodTail.tt K X (u' + 10) (u' + 16)) := VGoodTail.tail_nn K O X hg (u' + 10) 5
  have et18 : (VGoodTail.tt K X (u' + 10) (u' + 16)) = (K.add (K.mul (co K X (u' + 10)) (co K X (u' + 15))) (K.neg (K.mul (co K X (u' + 9)) (co K X (u' + 16))))) := rfl
  have t19 : O.le K.zero (VGoodTail.tt K X (u' + 8) (u' + 16)) := VGoodTail.tail_nn K O X hg (u' + 8) 7
  have et19 : (VGoodTail.tt K X (u' + 8) (u' + 16)) = (K.add (K.mul (co K X (u' + 8)) (co K X (u' + 15))) (K.neg (K.mul (co K X (u' + 7)) (co K X (u' + 16))))) := rfl
  have t20 : O.le K.zero (VGoodTail.tt K X (u' + 6) (u' + 16)) := VGoodTail.tail_nn K O X hg (u' + 6) 9
  have et20 : (VGoodTail.tt K X (u' + 6) (u' + 16)) = (K.add (K.mul (co K X (u' + 6)) (co K X (u' + 15))) (K.neg (K.mul (co K X (u' + 5)) (co K X (u' + 16))))) := rfl
  have t21 : O.le K.zero (VGoodTail.tt K X (u' + 6) (u' + 14)) := VGoodTail.tail_nn K O X hg (u' + 6) 7
  have et21 : (VGoodTail.tt K X (u' + 6) (u' + 14)) = (K.add (K.mul (co K X (u' + 6)) (co K X (u' + 13))) (K.neg (K.mul (co K X (u' + 5)) (co K X (u' + 14))))) := rfl
  have t22 : O.le K.zero (VGoodTail.tt K X (u' + 6) (u' + 12)) := VGoodTail.tail_nn K O X hg (u' + 6) 5
  have et22 : (VGoodTail.tt K X (u' + 6) (u' + 12)) = (K.add (K.mul (co K X (u' + 6)) (co K X (u' + 11))) (K.neg (K.mul (co K X (u' + 5)) (co K X (u' + 12))))) := rfl
  have t23 : O.le K.zero (VGoodTail.tt K X (u' + 6) (u' + 10)) := VGoodTail.tail_nn K O X hg (u' + 6) 3
  have et23 : (VGoodTail.tt K X (u' + 6) (u' + 10)) = (K.add (K.mul (co K X (u' + 6)) (co K X (u' + 9))) (K.neg (K.mul (co K X (u' + 5)) (co K X (u' + 10))))) := rfl
  have t24 : O.le K.zero (VGoodTail.tt K X (u' + 6) (u' + 8)) := VGoodTail.tail_nn K O X hg (u' + 6) 1
  have et24 : (VGoodTail.tt K X (u' + 6) (u' + 8)) = (K.add (K.mul (co K X (u' + 6)) (co K X (u' + 7))) (K.neg (K.mul (co K X (u' + 5)) (co K X (u' + 8))))) := rfl
  have hp : 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.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (VGoodTail.tt K X (u' + 10) (u' + 12)) (VGoodTail.tt K X (u' + 11) (u' + 13))) (VGoodTail.tt K X (u' + 9) (u' + 13))) (VGoodTail.tt K X (u' + 9) (u' + 11))) (VGoodTail.tt K X (u' + 12) (u' + 14))) (VGoodTail.  -- (+778 chars; full line in the Zenodo source)
    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 _ _ (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 _ _ (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   -- (+251 chars; full line in the Zenodo source)
  refine O.le_trans _ _ _ hp (TripleE.le_of_eq' K O _ _ ?_)
  rw [kv, et0, et1, et2, et3, et4, et5, et6, et7, et8, et9, et10, et11, et12, et13, et14, et15, et16, et17, et18, et19, et20, et21, et22, et23, et24]
  ring_z

theorem kna_s_2 (X : List R) (hg : L1Full.Good K O X) (u' : Nat) :
    O.le K.zero (CBLib.kern K (Fa K X) (Ga K X) 5 (u' + 4) (u' + 7)) := by
  have kv : CBLib.kern K (Fa K X) (Ga K X) 5 (u' + 4) (u' + 7) = (K.add (K.add (K.add (K.add (K.add K.zero (K.add (K.mul (co K X (u' + 10)) (co K X (u' + 12))) (K.neg (K.mul (co K X (u' + 13)) (co K X (u' + 9)))))) (K.add (K.mul (K.add (co K X (u' + 11)) (co K X (u' + 9))) (K.add (co K X (u' + 13)) (co K X (u' + 11)))) (K.neg (K.mul (K.add (co K X (u' + 14)) (co K X (u' + 12))) (K.add (co K X (u'   -- (+1320 chars; full line in the Zenodo source)
  have t0 : O.le K.zero (VGoodTail.tt K X (u' + 10) (u' + 13)) := VGoodTail.tail_nn K O X hg (u' + 10) 2
  have et0 : (VGoodTail.tt K X (u' + 10) (u' + 13)) = (K.add (K.mul (co K X (u' + 10)) (co K X (u' + 12))) (K.neg (K.mul (co K X (u' + 9)) (co K X (u' + 13))))) := rfl
  have t1 : O.le K.zero (VGoodTail.tt K X (u' + 11) (u' + 14)) := VGoodTail.tail_nn K O X hg (u' + 11) 2
  have et1 : (VGoodTail.tt K X (u' + 11) (u' + 14)) = (K.add (K.mul (co K X (u' + 11)) (co K X (u' + 13))) (K.neg (K.mul (co K X (u' + 10)) (co K X (u' + 14))))) := rfl
  have t2 : O.le K.zero (VGoodTail.tt K X (u' + 9) (u' + 14)) := VGoodTail.tail_nn K O X hg (u' + 9) 4
  have et2 : (VGoodTail.tt K X (u' + 9) (u' + 14)) = (K.add (K.mul (co K X (u' + 9)) (co K X (u' + 13))) (K.neg (K.mul (co K X (u' + 8)) (co K X (u' + 14))))) := rfl
  have t3 : O.le K.zero (VGoodTail.tt K X (u' + 9) (u' + 12)) := VGoodTail.tail_nn K O X hg (u' + 9) 2
  have et3 : (VGoodTail.tt K X (u' + 9) (u' + 12)) = (K.add (K.mul (co K X (u' + 9)) (co K X (u' + 11))) (K.neg (K.mul (co K X (u' + 8)) (co K X (u' + 12))))) := rfl
  have t4 : O.le K.zero (VGoodTail.tt K X (u' + 12) (u' + 15)) := VGoodTail.tail_nn K O X hg (u' + 12) 2
  have et4 : (VGoodTail.tt K X (u' + 12) (u' + 15)) = (K.add (K.mul (co K X (u' + 12)) (co K X (u' + 14))) (K.neg (K.mul (co K X (u' + 11)) (co K X (u' + 15))))) := rfl
  have t5 : O.le K.zero (VGoodTail.tt K X (u' + 12) (u' + 13)) := VGoodTail.tail_nn K O X hg (u' + 12) 0
  have et5 : (VGoodTail.tt K X (u' + 12) (u' + 13)) = (K.add (K.mul (co K X (u' + 12)) (co K X (u' + 12))) (K.neg (K.mul (co K X (u' + 11)) (co K X (u' + 13))))) := rfl
  have t6 : O.le K.zero (VGoodTail.tt K X (u' + 10) (u' + 15)) := VGoodTail.tail_nn K O X hg (u' + 10) 4
  have et6 : (VGoodTail.tt K X (u' + 10) (u' + 15)) = (K.add (K.mul (co K X (u' + 10)) (co K X (u' + 14))) (K.neg (K.mul (co K X (u' + 9)) (co K X (u' + 15))))) := rfl
  have t7 : O.le K.zero (VGoodTail.tt K X (u' + 10) (u' + 11)) := VGoodTail.tail_nn K O X hg (u' + 10) 0
  have et7 : (VGoodTail.tt K X (u' + 10) (u' + 11)) = (K.add (K.mul (co K X (u' + 10)) (co K X (u' + 10))) (K.neg (K.mul (co K X (u' + 9)) (co K X (u' + 11))))) := rfl
  have t8 : O.le K.zero (VGoodTail.tt K X (u' + 8) (u' + 15)) := VGoodTail.tail_nn K O X hg (u' + 8) 6
  have et8 : (VGoodTail.tt K X (u' + 8) (u' + 15)) = (K.add (K.mul (co K X (u' + 8)) (co K X (u' + 14))) (K.neg (K.mul (co K X (u' + 7)) (co K X (u' + 15))))) := rfl
  have t9 : O.le K.zero (VGoodTail.tt K X (u' + 8) (u' + 13)) := VGoodTail.tail_nn K O X hg (u' + 8) 4
  have et9 : (VGoodTail.tt K X (u' + 8) (u' + 13)) = (K.add (K.mul (co K X (u' + 8)) (co K X (u' + 12))) (K.neg (K.mul (co K X (u' + 7)) (co K X (u' + 13))))) := rfl
  have t10 : O.le K.zero (VGoodTail.tt K X (u' + 8) (u' + 11)) := VGoodTail.tail_nn K O X hg (u' + 8) 2
  have et10 : (VGoodTail.tt K X (u' + 8) (u' + 11)) = (K.add (K.mul (co K X (u' + 8)) (co K X (u' + 10))) (K.neg (K.mul (co K X (u' + 7)) (co K X (u' + 11))))) := rfl
  have t11 : O.le K.zero (VGoodTail.tt K X (u' + 13) (u' + 16)) := VGoodTail.tail_nn K O X hg (u' + 13) 2
  have et11 : (VGoodTail.tt K X (u' + 13) (u' + 16)) = (K.add (K.mul (co K X (u' + 13)) (co K X (u' + 15))) (K.neg (K.mul (co K X (u' + 12)) (co K X (u' + 16))))) := rfl
  have t12 : O.le K.zero (VGoodTail.tt K X (u' + 11) (u' + 16)) := VGoodTail.tail_nn K O X hg (u' + 11) 4
  have et12 : (VGoodTail.tt K X (u' + 11) (u' + 16)) = (K.add (K.mul (co K X (u' + 11)) (co K X (u' + 15))) (K.neg (K.mul (co K X (u' + 10)) (co K X (u' + 16))))) := rfl
  have t13 : O.le K.zero (VGoodTail.tt K X (u' + 9) (u' + 16)) := VGoodTail.tail_nn K O X hg (u' + 9) 6
  have et13 : (VGoodTail.tt K X (u' + 9) (u' + 16)) = (K.add (K.mul (co K X (u' + 9)) (co K X (u' + 15))) (K.neg (K.mul (co K X (u' + 8)) (co K X (u' + 16))))) := rfl
  have t14 : O.le K.zero (VGoodTail.tt K X (u' + 7) (u' + 16)) := VGoodTail.tail_nn K O X hg (u' + 7) 8
  have et14 : (VGoodTail.tt K X (u' + 7) (u' + 16)) = (K.add (K.mul (co K X (u' + 7)) (co K X (u' + 15))) (K.neg (K.mul (co K X (u' + 6)) (co K X (u' + 16))))) := rfl
  have t15 : O.le K.zero (VGoodTail.tt K X (u' + 7) (u' + 14)) := VGoodTail.tail_nn K O X hg (u' + 7) 6
  have et15 : (VGoodTail.tt K X (u' + 7) (u' + 14)) = (K.add (K.mul (co K X (u' + 7)) (co K X (u' + 13))) (K.neg (K.mul (co K X (u' + 6)) (co K X (u' + 14))))) := rfl
  have t16 : O.le K.zero (VGoodTail.tt K X (u' + 7) (u' + 12)) := VGoodTail.tail_nn K O X hg (u' + 7) 4
  have et16 : (VGoodTail.tt K X (u' + 7) (u' + 12)) = (K.add (K.mul (co K X (u' + 7)) (co K X (u' + 11))) (K.neg (K.mul (co K X (u' + 6)) (co K X (u' + 12))))) := rfl
  have t17 : O.le K.zero (VGoodTail.tt K X (u' + 7) (u' + 10)) := VGoodTail.tail_nn K O X hg (u' + 7) 2
  have et17 : (VGoodTail.tt K X (u' + 7) (u' + 10)) = (K.add (K.mul (co K X (u' + 7)) (co K X (u' + 9))) (K.neg (K.mul (co K X (u' + 6)) (co K X (u' + 10))))) := rfl
  have t18 : O.le K.zero (VGoodTail.tt K X (u' + 14) (u' + 17)) := VGoodTail.tail_nn K O X hg (u' + 14) 2
  have et18 : (VGoodTail.tt K X (u' + 14) (u' + 17)) = (K.add (K.mul (co K X (u' + 14)) (co K X (u' + 16))) (K.neg (K.mul (co K X (u' + 13)) (co K X (u' + 17))))) := rfl
  have t19 : O.le K.zero (VGoodTail.tt K X (u' + 14) (u' + 15)) := VGoodTail.tail_nn K O X hg (u' + 14) 0
  have et19 : (VGoodTail.tt K X (u' + 14) (u' + 15)) = (K.add (K.mul (co K X (u' + 14)) (co K X (u' + 14))) (K.neg (K.mul (co K X (u' + 13)) (co K X (u' + 15))))) := rfl
  have t20 : O.le K.zero (VGoodTail.tt K X (u' + 12) (u' + 17)) := VGoodTail.tail_nn K O X hg (u' + 12) 4
  have et20 : (VGoodTail.tt K X (u' + 12) (u' + 17)) = (K.add (K.mul (co K X (u' + 12)) (co K X (u' + 16))) (K.neg (K.mul (co K X (u' + 11)) (co K X (u' + 17))))) := rfl
  have t21 : O.le K.zero (VGoodTail.tt K X (u' + 10) (u' + 17)) := VGoodTail.tail_nn K O X hg (u' + 10) 6
  have et21 : (VGoodTail.tt K X (u' + 10) (u' + 17)) = (K.add (K.mul (co K X (u' + 10)) (co K X (u' + 16))) (K.neg (K.mul (co K X (u' + 9)) (co K X (u' + 17))))) := rfl
  have t22 : O.le K.zero (VGoodTail.tt K X (u' + 8) (u' + 17)) := VGoodTail.tail_nn K O X hg (u' + 8) 8
  have et22 : (VGoodTail.tt K X (u' + 8) (u' + 17)) = (K.add (K.mul (co K X (u' + 8)) (co K X (u' + 16))) (K.neg (K.mul (co K X (u' + 7)) (co K X (u' + 17))))) := rfl
  have t23 : O.le K.zero (VGoodTail.tt K X (u' + 8) (u' + 9)) := VGoodTail.tail_nn K O X hg (u' + 8) 0
  have et23 : (VGoodTail.tt K X (u' + 8) (u' + 9)) = (K.add (K.mul (co K X (u' + 8)) (co K X (u' + 8))) (K.neg (K.mul (co K X (u' + 7)) (co K X (u' + 9))))) := rfl
  have t24 : O.le K.zero (VGoodTail.tt K X (u' + 6) (u' + 17)) := VGoodTail.tail_nn K O X hg (u' + 6) 10
  have et24 : (VGoodTail.tt K X (u' + 6) (u' + 17)) = (K.add (K.mul (co K X (u' + 6)) (co K X (u' + 16))) (K.neg (K.mul (co K X (u' + 5)) (co K X (u' + 17))))) := rfl
  have t25 : O.le K.zero (VGoodTail.tt K X (u' + 6) (u' + 15)) := VGoodTail.tail_nn K O X hg (u' + 6) 8
  have et25 : (VGoodTail.tt K X (u' + 6) (u' + 15)) = (K.add (K.mul (co K X (u' + 6)) (co K X (u' + 14))) (K.neg (K.mul (co K X (u' + 5)) (co K X (u' + 15))))) := rfl
  have t26 : O.le K.zero (VGoodTail.tt K X (u' + 6) (u' + 13)) := VGoodTail.tail_nn K O X hg (u' + 6) 6
  have et26 : (VGoodTail.tt K X (u' + 6) (u' + 13)) = (K.add (K.mul (co K X (u' + 6)) (co K X (u' + 12))) (K.neg (K.mul (co K X (u' + 5)) (co K X (u' + 13))))) := rfl
  have t27 : O.le K.zero (VGoodTail.tt K X (u' + 6) (u' + 11)) := VGoodTail.tail_nn K O X hg (u' + 6) 4
  have et27 : (VGoodTail.tt K X (u' + 6) (u' + 11)) = (K.add (K.mul (co K X (u' + 6)) (co K X (u' + 10))) (K.neg (K.mul (co K X (u' + 5)) (co K X (u' + 11))))) := rfl
  have t28 : O.le K.zero (VGoodTail.tt K X (u' + 6) (u' + 9)) := VGoodTail.tail_nn K O X hg (u' + 6) 2
  have et28 : (VGoodTail.tt K X (u' + 6) (u' + 9)) = (K.add (K.mul (co K X (u' + 6)) (co K X (u' + 8))) (K.neg (K.mul (co K X (u' + 5)) (co K X (u' + 9))))) := rfl
  have hp : 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.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 (VGoodTail.tt K X (u' + 10) (u' + 13)) (VGoodTail.tt K X (u' + 10) (u' + 13))) (VGoodTail.tt K X (u' + 11) (u' + 14))) (VGoodTail.tt K X (  -- (+1244 chars; full line in the Zenodo source)
    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 _ _ (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 _ _ (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   -- (+516 chars; full line in the Zenodo source)
  refine O.le_trans _ _ _ hp (TripleE.le_of_eq' K O _ _ ?_)
  rw [kv, et0, et1, et2, et3, et4, et5, et6, et7, et8, et9, et10, et11, et12, et13, et14, et15, et16, et17, et18, et19, et20, et21, et22, et23, et24, et25, et26, et27, et28]
  ring_z

theorem kna_s_3 (X : List R) (hg : L1Full.Good K O X) (u' : Nat) :
    O.le K.zero (CBLib.kern K (Fa K X) (Ga K X) 5 (u' + 4) (u' + 8)) := by
  have kv : CBLib.kern K (Fa K X) (Ga K X) 5 (u' + 4) (u' + 8) = (K.add (K.add (K.add (K.add (K.add K.zero (K.add (K.mul (co K X (u' + 10)) (co K X (u' + 13))) (K.neg (K.mul (co K X (u' + 14)) (co K X (u' + 9)))))) (K.add (K.mul (K.add (co K X (u' + 11)) (co K X (u' + 9))) (K.add (co K X (u' + 14)) (co K X (u' + 12)))) (K.neg (K.mul (K.add (co K X (u' + 15)) (co K X (u' + 13))) (K.add (co K X (u'   -- (+1322 chars; full line in the Zenodo source)
  have t0 : O.le K.zero (VGoodTail.tt K X (u' + 10) (u' + 14)) := VGoodTail.tail_nn K O X hg (u' + 10) 3
  have et0 : (VGoodTail.tt K X (u' + 10) (u' + 14)) = (K.add (K.mul (co K X (u' + 10)) (co K X (u' + 13))) (K.neg (K.mul (co K X (u' + 9)) (co K X (u' + 14))))) := rfl
  have t1 : O.le K.zero (VGoodTail.tt K X (u' + 11) (u' + 15)) := VGoodTail.tail_nn K O X hg (u' + 11) 3
  have et1 : (VGoodTail.tt K X (u' + 11) (u' + 15)) = (K.add (K.mul (co K X (u' + 11)) (co K X (u' + 14))) (K.neg (K.mul (co K X (u' + 10)) (co K X (u' + 15))))) := rfl
  have t2 : O.le K.zero (VGoodTail.tt K X (u' + 11) (u' + 13)) := VGoodTail.tail_nn K O X hg (u' + 11) 1
  have et2 : (VGoodTail.tt K X (u' + 11) (u' + 13)) = (K.add (K.mul (co K X (u' + 11)) (co K X (u' + 12))) (K.neg (K.mul (co K X (u' + 10)) (co K X (u' + 13))))) := rfl
  have t3 : O.le K.zero (VGoodTail.tt K X (u' + 9) (u' + 15)) := VGoodTail.tail_nn K O X hg (u' + 9) 5
  have et3 : (VGoodTail.tt K X (u' + 9) (u' + 15)) = (K.add (K.mul (co K X (u' + 9)) (co K X (u' + 14))) (K.neg (K.mul (co K X (u' + 8)) (co K X (u' + 15))))) := rfl
  have t4 : O.le K.zero (VGoodTail.tt K X (u' + 9) (u' + 13)) := VGoodTail.tail_nn K O X hg (u' + 9) 3
  have et4 : (VGoodTail.tt K X (u' + 9) (u' + 13)) = (K.add (K.mul (co K X (u' + 9)) (co K X (u' + 12))) (K.neg (K.mul (co K X (u' + 8)) (co K X (u' + 13))))) := rfl
  have t5 : O.le K.zero (VGoodTail.tt K X (u' + 12) (u' + 16)) := VGoodTail.tail_nn K O X hg (u' + 12) 3
  have et5 : (VGoodTail.tt K X (u' + 12) (u' + 16)) = (K.add (K.mul (co K X (u' + 12)) (co K X (u' + 15))) (K.neg (K.mul (co K X (u' + 11)) (co K X (u' + 16))))) := rfl
  have t6 : O.le K.zero (VGoodTail.tt K X (u' + 12) (u' + 14)) := VGoodTail.tail_nn K O X hg (u' + 12) 1
  have et6 : (VGoodTail.tt K X (u' + 12) (u' + 14)) = (K.add (K.mul (co K X (u' + 12)) (co K X (u' + 13))) (K.neg (K.mul (co K X (u' + 11)) (co K X (u' + 14))))) := rfl
  have t7 : O.le K.zero (VGoodTail.tt K X (u' + 10) (u' + 16)) := VGoodTail.tail_nn K O X hg (u' + 10) 5
  have et7 : (VGoodTail.tt K X (u' + 10) (u' + 16)) = (K.add (K.mul (co K X (u' + 10)) (co K X (u' + 15))) (K.neg (K.mul (co K X (u' + 9)) (co K X (u' + 16))))) := rfl
  have t8 : O.le K.zero (VGoodTail.tt K X (u' + 10) (u' + 12)) := VGoodTail.tail_nn K O X hg (u' + 10) 1
  have et8 : (VGoodTail.tt K X (u' + 10) (u' + 12)) = (K.add (K.mul (co K X (u' + 10)) (co K X (u' + 11))) (K.neg (K.mul (co K X (u' + 9)) (co K X (u' + 12))))) := rfl
  have t9 : O.le K.zero (VGoodTail.tt K X (u' + 8) (u' + 16)) := VGoodTail.tail_nn K O X hg (u' + 8) 7
  have et9 : (VGoodTail.tt K X (u' + 8) (u' + 16)) = (K.add (K.mul (co K X (u' + 8)) (co K X (u' + 15))) (K.neg (K.mul (co K X (u' + 7)) (co K X (u' + 16))))) := rfl
  have t10 : O.le K.zero (VGoodTail.tt K X (u' + 8) (u' + 14)) := VGoodTail.tail_nn K O X hg (u' + 8) 5
  have et10 : (VGoodTail.tt K X (u' + 8) (u' + 14)) = (K.add (K.mul (co K X (u' + 8)) (co K X (u' + 13))) (K.neg (K.mul (co K X (u' + 7)) (co K X (u' + 14))))) := rfl
  have t11 : O.le K.zero (VGoodTail.tt K X (u' + 8) (u' + 12)) := VGoodTail.tail_nn K O X hg (u' + 8) 3
  have et11 : (VGoodTail.tt K X (u' + 8) (u' + 12)) = (K.add (K.mul (co K X (u' + 8)) (co K X (u' + 11))) (K.neg (K.mul (co K X (u' + 7)) (co K X (u' + 12))))) := rfl
  have t12 : O.le K.zero (VGoodTail.tt K X (u' + 13) (u' + 17)) := VGoodTail.tail_nn K O X hg (u' + 13) 3
  have et12 : (VGoodTail.tt K X (u' + 13) (u' + 17)) = (K.add (K.mul (co K X (u' + 13)) (co K X (u' + 16))) (K.neg (K.mul (co K X (u' + 12)) (co K X (u' + 17))))) := rfl
  have t13 : O.le K.zero (VGoodTail.tt K X (u' + 13) (u' + 15)) := VGoodTail.tail_nn K O X hg (u' + 13) 1
  have et13 : (VGoodTail.tt K X (u' + 13) (u' + 15)) = (K.add (K.mul (co K X (u' + 13)) (co K X (u' + 14))) (K.neg (K.mul (co K X (u' + 12)) (co K X (u' + 15))))) := rfl
  have t14 : O.le K.zero (VGoodTail.tt K X (u' + 11) (u' + 17)) := VGoodTail.tail_nn K O X hg (u' + 11) 5
  have et14 : (VGoodTail.tt K X (u' + 11) (u' + 17)) = (K.add (K.mul (co K X (u' + 11)) (co K X (u' + 16))) (K.neg (K.mul (co K X (u' + 10)) (co K X (u' + 17))))) := rfl
  have t15 : O.le K.zero (VGoodTail.tt K X (u' + 9) (u' + 17)) := VGoodTail.tail_nn K O X hg (u' + 9) 7
  have et15 : (VGoodTail.tt K X (u' + 9) (u' + 17)) = (K.add (K.mul (co K X (u' + 9)) (co K X (u' + 16))) (K.neg (K.mul (co K X (u' + 8)) (co K X (u' + 17))))) := rfl
  have t16 : O.le K.zero (VGoodTail.tt K X (u' + 9) (u' + 11)) := VGoodTail.tail_nn K O X hg (u' + 9) 1
  have et16 : (VGoodTail.tt K X (u' + 9) (u' + 11)) = (K.add (K.mul (co K X (u' + 9)) (co K X (u' + 10))) (K.neg (K.mul (co K X (u' + 8)) (co K X (u' + 11))))) := rfl
  have t17 : O.le K.zero (VGoodTail.tt K X (u' + 7) (u' + 17)) := VGoodTail.tail_nn K O X hg (u' + 7) 9
  have et17 : (VGoodTail.tt K X (u' + 7) (u' + 17)) = (K.add (K.mul (co K X (u' + 7)) (co K X (u' + 16))) (K.neg (K.mul (co K X (u' + 6)) (co K X (u' + 17))))) := rfl
  have t18 : O.le K.zero (VGoodTail.tt K X (u' + 7) (u' + 15)) := VGoodTail.tail_nn K O X hg (u' + 7) 7
  have et18 : (VGoodTail.tt K X (u' + 7) (u' + 15)) = (K.add (K.mul (co K X (u' + 7)) (co K X (u' + 14))) (K.neg (K.mul (co K X (u' + 6)) (co K X (u' + 15))))) := rfl
  have t19 : O.le K.zero (VGoodTail.tt K X (u' + 7) (u' + 13)) := VGoodTail.tail_nn K O X hg (u' + 7) 5
  have et19 : (VGoodTail.tt K X (u' + 7) (u' + 13)) = (K.add (K.mul (co K X (u' + 7)) (co K X (u' + 12))) (K.neg (K.mul (co K X (u' + 6)) (co K X (u' + 13))))) := rfl
  have t20 : O.le K.zero (VGoodTail.tt K X (u' + 7) (u' + 11)) := VGoodTail.tail_nn K O X hg (u' + 7) 3
  have et20 : (VGoodTail.tt K X (u' + 7) (u' + 11)) = (K.add (K.mul (co K X (u' + 7)) (co K X (u' + 10))) (K.neg (K.mul (co K X (u' + 6)) (co K X (u' + 11))))) := rfl
  have t21 : O.le K.zero (VGoodTail.tt K X (u' + 14) (u' + 18)) := VGoodTail.tail_nn K O X hg (u' + 14) 3
  have et21 : (VGoodTail.tt K X (u' + 14) (u' + 18)) = (K.add (K.mul (co K X (u' + 14)) (co K X (u' + 17))) (K.neg (K.mul (co K X (u' + 13)) (co K X (u' + 18))))) := rfl
  have t22 : O.le K.zero (VGoodTail.tt K X (u' + 14) (u' + 16)) := VGoodTail.tail_nn K O X hg (u' + 14) 1
  have et22 : (VGoodTail.tt K X (u' + 14) (u' + 16)) = (K.add (K.mul (co K X (u' + 14)) (co K X (u' + 15))) (K.neg (K.mul (co K X (u' + 13)) (co K X (u' + 16))))) := rfl
  have t23 : O.le K.zero (VGoodTail.tt K X (u' + 12) (u' + 18)) := VGoodTail.tail_nn K O X hg (u' + 12) 5
  have et23 : (VGoodTail.tt K X (u' + 12) (u' + 18)) = (K.add (K.mul (co K X (u' + 12)) (co K X (u' + 17))) (K.neg (K.mul (co K X (u' + 11)) (co K X (u' + 18))))) := rfl
  have t24 : O.le K.zero (VGoodTail.tt K X (u' + 10) (u' + 18)) := VGoodTail.tail_nn K O X hg (u' + 10) 7
  have et24 : (VGoodTail.tt K X (u' + 10) (u' + 18)) = (K.add (K.mul (co K X (u' + 10)) (co K X (u' + 17))) (K.neg (K.mul (co K X (u' + 9)) (co K X (u' + 18))))) := rfl
  have t25 : O.le K.zero (VGoodTail.tt K X (u' + 8) (u' + 18)) := VGoodTail.tail_nn K O X hg (u' + 8) 9
  have et25 : (VGoodTail.tt K X (u' + 8) (u' + 18)) = (K.add (K.mul (co K X (u' + 8)) (co K X (u' + 17))) (K.neg (K.mul (co K X (u' + 7)) (co K X (u' + 18))))) := rfl
  have t26 : O.le K.zero (VGoodTail.tt K X (u' + 8) (u' + 10)) := VGoodTail.tail_nn K O X hg (u' + 8) 1
  have et26 : (VGoodTail.tt K X (u' + 8) (u' + 10)) = (K.add (K.mul (co K X (u' + 8)) (co K X (u' + 9))) (K.neg (K.mul (co K X (u' + 7)) (co K X (u' + 10))))) := rfl
  have t27 : O.le K.zero (VGoodTail.tt K X (u' + 6) (u' + 18)) := VGoodTail.tail_nn K O X hg (u' + 6) 11
  have et27 : (VGoodTail.tt K X (u' + 6) (u' + 18)) = (K.add (K.mul (co K X (u' + 6)) (co K X (u' + 17))) (K.neg (K.mul (co K X (u' + 5)) (co K X (u' + 18))))) := rfl
  have t28 : O.le K.zero (VGoodTail.tt K X (u' + 6) (u' + 16)) := VGoodTail.tail_nn K O X hg (u' + 6) 9
  have et28 : (VGoodTail.tt K X (u' + 6) (u' + 16)) = (K.add (K.mul (co K X (u' + 6)) (co K X (u' + 15))) (K.neg (K.mul (co K X (u' + 5)) (co K X (u' + 16))))) := rfl
  have t29 : O.le K.zero (VGoodTail.tt K X (u' + 6) (u' + 14)) := VGoodTail.tail_nn K O X hg (u' + 6) 7
  have et29 : (VGoodTail.tt K X (u' + 6) (u' + 14)) = (K.add (K.mul (co K X (u' + 6)) (co K X (u' + 13))) (K.neg (K.mul (co K X (u' + 5)) (co K X (u' + 14))))) := rfl
  have t30 : O.le K.zero (VGoodTail.tt K X (u' + 6) (u' + 12)) := VGoodTail.tail_nn K O X hg (u' + 6) 5
  have et30 : (VGoodTail.tt K X (u' + 6) (u' + 12)) = (K.add (K.mul (co K X (u' + 6)) (co K X (u' + 11))) (K.neg (K.mul (co K X (u' + 5)) (co K X (u' + 12))))) := rfl
  have t31 : O.le K.zero (VGoodTail.tt K X (u' + 6) (u' + 10)) := VGoodTail.tail_nn K O X hg (u' + 6) 3
  have et31 : (VGoodTail.tt K X (u' + 6) (u' + 10)) = (K.add (K.mul (co K X (u' + 6)) (co K X (u' + 9))) (K.neg (K.mul (co K X (u' + 5)) (co K X (u' + 10))))) := rfl
  have hp : 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.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 (VGoodTail.tt K X (u' + 10) (u' + 14)) (VGoodTail.tt K X (u' + 10) (u' + 14))) (VGoodTail.tt K X  -- (+1525 chars; full line in the Zenodo source)
    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 _ _ (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 _ _ (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   -- (+676 chars; full line in the Zenodo source)
  refine O.le_trans _ _ _ hp (TripleE.le_of_eq' K O _ _ ?_)
  rw [kv, et0, et1, et2, et3, et4, et5, et6, et7, et8, et9, et10, et11, et12, et13, et14, et15, et16, et17, et18, et19, et20, et21, et22, et23, et24, et25, et26, et27, et28, et29, et30, et31]
  ring_z

theorem kna_s_4 (X : List R) (hg : L1Full.Good K O X) (u' : Nat) :
    O.le K.zero (CBLib.kern K (Fa K X) (Ga K X) 5 (u' + 4) (u' + 9)) := by
  have kv : CBLib.kern K (Fa K X) (Ga K X) 5 (u' + 4) (u' + 9) = (K.add (K.add (K.add (K.add (K.add K.zero (K.add (K.mul (co K X (u' + 10)) (co K X (u' + 14))) (K.neg (K.mul (co K X (u' + 15)) (co K X (u' + 9)))))) (K.add (K.mul (K.add (co K X (u' + 11)) (co K X (u' + 9))) (K.add (co K X (u' + 15)) (co K X (u' + 13)))) (K.neg (K.mul (K.add (co K X (u' + 16)) (co K X (u' + 14))) (K.add (co K X (u'   -- (+1323 chars; full line in the Zenodo source)
  have t0 : O.le K.zero (VGoodTail.tt K X (u' + 10) (u' + 15)) := VGoodTail.tail_nn K O X hg (u' + 10) 4
  have et0 : (VGoodTail.tt K X (u' + 10) (u' + 15)) = (K.add (K.mul (co K X (u' + 10)) (co K X (u' + 14))) (K.neg (K.mul (co K X (u' + 9)) (co K X (u' + 15))))) := rfl
  have t1 : O.le K.zero (VGoodTail.tt K X (u' + 11) (u' + 16)) := VGoodTail.tail_nn K O X hg (u' + 11) 4
  have et1 : (VGoodTail.tt K X (u' + 11) (u' + 16)) = (K.add (K.mul (co K X (u' + 11)) (co K X (u' + 15))) (K.neg (K.mul (co K X (u' + 10)) (co K X (u' + 16))))) := rfl
  have t2 : O.le K.zero (VGoodTail.tt K X (u' + 11) (u' + 14)) := VGoodTail.tail_nn K O X hg (u' + 11) 2
  have et2 : (VGoodTail.tt K X (u' + 11) (u' + 14)) = (K.add (K.mul (co K X (u' + 11)) (co K X (u' + 13))) (K.neg (K.mul (co K X (u' + 10)) (co K X (u' + 14))))) := rfl
  have t3 : O.le K.zero (VGoodTail.tt K X (u' + 9) (u' + 16)) := VGoodTail.tail_nn K O X hg (u' + 9) 6
  have et3 : (VGoodTail.tt K X (u' + 9) (u' + 16)) = (K.add (K.mul (co K X (u' + 9)) (co K X (u' + 15))) (K.neg (K.mul (co K X (u' + 8)) (co K X (u' + 16))))) := rfl
  have t4 : O.le K.zero (VGoodTail.tt K X (u' + 9) (u' + 14)) := VGoodTail.tail_nn K O X hg (u' + 9) 4
  have et4 : (VGoodTail.tt K X (u' + 9) (u' + 14)) = (K.add (K.mul (co K X (u' + 9)) (co K X (u' + 13))) (K.neg (K.mul (co K X (u' + 8)) (co K X (u' + 14))))) := rfl
  have t5 : O.le K.zero (VGoodTail.tt K X (u' + 12) (u' + 17)) := VGoodTail.tail_nn K O X hg (u' + 12) 4
  have et5 : (VGoodTail.tt K X (u' + 12) (u' + 17)) = (K.add (K.mul (co K X (u' + 12)) (co K X (u' + 16))) (K.neg (K.mul (co K X (u' + 11)) (co K X (u' + 17))))) := rfl
  have t6 : O.le K.zero (VGoodTail.tt K X (u' + 12) (u' + 15)) := VGoodTail.tail_nn K O X hg (u' + 12) 2
  have et6 : (VGoodTail.tt K X (u' + 12) (u' + 15)) = (K.add (K.mul (co K X (u' + 12)) (co K X (u' + 14))) (K.neg (K.mul (co K X (u' + 11)) (co K X (u' + 15))))) := rfl
  have t7 : O.le K.zero (VGoodTail.tt K X (u' + 12) (u' + 13)) := VGoodTail.tail_nn K O X hg (u' + 12) 0
  have et7 : (VGoodTail.tt K X (u' + 12) (u' + 13)) = (K.add (K.mul (co K X (u' + 12)) (co K X (u' + 12))) (K.neg (K.mul (co K X (u' + 11)) (co K X (u' + 13))))) := rfl
  have t8 : O.le K.zero (VGoodTail.tt K X (u' + 10) (u' + 17)) := VGoodTail.tail_nn K O X hg (u' + 10) 6
  have et8 : (VGoodTail.tt K X (u' + 10) (u' + 17)) = (K.add (K.mul (co K X (u' + 10)) (co K X (u' + 16))) (K.neg (K.mul (co K X (u' + 9)) (co K X (u' + 17))))) := rfl
  have t9 : O.le K.zero (VGoodTail.tt K X (u' + 10) (u' + 13)) := VGoodTail.tail_nn K O X hg (u' + 10) 2
  have et9 : (VGoodTail.tt K X (u' + 10) (u' + 13)) = (K.add (K.mul (co K X (u' + 10)) (co K X (u' + 12))) (K.neg (K.mul (co K X (u' + 9)) (co K X (u' + 13))))) := rfl
  have t10 : O.le K.zero (VGoodTail.tt K X (u' + 8) (u' + 17)) := VGoodTail.tail_nn K O X hg (u' + 8) 8
  have et10 : (VGoodTail.tt K X (u' + 8) (u' + 17)) = (K.add (K.mul (co K X (u' + 8)) (co K X (u' + 16))) (K.neg (K.mul (co K X (u' + 7)) (co K X (u' + 17))))) := rfl
  have t11 : O.le K.zero (VGoodTail.tt K X (u' + 8) (u' + 15)) := VGoodTail.tail_nn K O X hg (u' + 8) 6
  have et11 : (VGoodTail.tt K X (u' + 8) (u' + 15)) = (K.add (K.mul (co K X (u' + 8)) (co K X (u' + 14))) (K.neg (K.mul (co K X (u' + 7)) (co K X (u' + 15))))) := rfl
  have t12 : O.le K.zero (VGoodTail.tt K X (u' + 8) (u' + 13)) := VGoodTail.tail_nn K O X hg (u' + 8) 4
  have et12 : (VGoodTail.tt K X (u' + 8) (u' + 13)) = (K.add (K.mul (co K X (u' + 8)) (co K X (u' + 12))) (K.neg (K.mul (co K X (u' + 7)) (co K X (u' + 13))))) := rfl
  have t13 : O.le K.zero (VGoodTail.tt K X (u' + 13) (u' + 18)) := VGoodTail.tail_nn K O X hg (u' + 13) 4
  have et13 : (VGoodTail.tt K X (u' + 13) (u' + 18)) = (K.add (K.mul (co K X (u' + 13)) (co K X (u' + 17))) (K.neg (K.mul (co K X (u' + 12)) (co K X (u' + 18))))) := rfl
  have t14 : O.le K.zero (VGoodTail.tt K X (u' + 13) (u' + 16)) := VGoodTail.tail_nn K O X hg (u' + 13) 2
  have et14 : (VGoodTail.tt K X (u' + 13) (u' + 16)) = (K.add (K.mul (co K X (u' + 13)) (co K X (u' + 15))) (K.neg (K.mul (co K X (u' + 12)) (co K X (u' + 16))))) := rfl
  have t15 : O.le K.zero (VGoodTail.tt K X (u' + 11) (u' + 18)) := VGoodTail.tail_nn K O X hg (u' + 11) 6
  have et15 : (VGoodTail.tt K X (u' + 11) (u' + 18)) = (K.add (K.mul (co K X (u' + 11)) (co K X (u' + 17))) (K.neg (K.mul (co K X (u' + 10)) (co K X (u' + 18))))) := rfl
  have t16 : O.le K.zero (VGoodTail.tt K X (u' + 9) (u' + 18)) := VGoodTail.tail_nn K O X hg (u' + 9) 8
  have et16 : (VGoodTail.tt K X (u' + 9) (u' + 18)) = (K.add (K.mul (co K X (u' + 9)) (co K X (u' + 17))) (K.neg (K.mul (co K X (u' + 8)) (co K X (u' + 18))))) := rfl
  have t17 : O.le K.zero (VGoodTail.tt K X (u' + 9) (u' + 12)) := VGoodTail.tail_nn K O X hg (u' + 9) 2
  have et17 : (VGoodTail.tt K X (u' + 9) (u' + 12)) = (K.add (K.mul (co K X (u' + 9)) (co K X (u' + 11))) (K.neg (K.mul (co K X (u' + 8)) (co K X (u' + 12))))) := rfl
  have t18 : O.le K.zero (VGoodTail.tt K X (u' + 7) (u' + 18)) := VGoodTail.tail_nn K O X hg (u' + 7) 10
  have et18 : (VGoodTail.tt K X (u' + 7) (u' + 18)) = (K.add (K.mul (co K X (u' + 7)) (co K X (u' + 17))) (K.neg (K.mul (co K X (u' + 6)) (co K X (u' + 18))))) := rfl
  have t19 : O.le K.zero (VGoodTail.tt K X (u' + 7) (u' + 16)) := VGoodTail.tail_nn K O X hg (u' + 7) 8
  have et19 : (VGoodTail.tt K X (u' + 7) (u' + 16)) = (K.add (K.mul (co K X (u' + 7)) (co K X (u' + 15))) (K.neg (K.mul (co K X (u' + 6)) (co K X (u' + 16))))) := rfl
  have t20 : O.le K.zero (VGoodTail.tt K X (u' + 7) (u' + 14)) := VGoodTail.tail_nn K O X hg (u' + 7) 6
  have et20 : (VGoodTail.tt K X (u' + 7) (u' + 14)) = (K.add (K.mul (co K X (u' + 7)) (co K X (u' + 13))) (K.neg (K.mul (co K X (u' + 6)) (co K X (u' + 14))))) := rfl
  have t21 : O.le K.zero (VGoodTail.tt K X (u' + 7) (u' + 12)) := VGoodTail.tail_nn K O X hg (u' + 7) 4
  have et21 : (VGoodTail.tt K X (u' + 7) (u' + 12)) = (K.add (K.mul (co K X (u' + 7)) (co K X (u' + 11))) (K.neg (K.mul (co K X (u' + 6)) (co K X (u' + 12))))) := rfl
  have t22 : O.le K.zero (VGoodTail.tt K X (u' + 14) (u' + 19)) := VGoodTail.tail_nn K O X hg (u' + 14) 4
  have et22 : (VGoodTail.tt K X (u' + 14) (u' + 19)) = (K.add (K.mul (co K X (u' + 14)) (co K X (u' + 18))) (K.neg (K.mul (co K X (u' + 13)) (co K X (u' + 19))))) := rfl
  have t23 : O.le K.zero (VGoodTail.tt K X (u' + 14) (u' + 17)) := VGoodTail.tail_nn K O X hg (u' + 14) 2
  have et23 : (VGoodTail.tt K X (u' + 14) (u' + 17)) = (K.add (K.mul (co K X (u' + 14)) (co K X (u' + 16))) (K.neg (K.mul (co K X (u' + 13)) (co K X (u' + 17))))) := rfl
  have t24 : O.le K.zero (VGoodTail.tt K X (u' + 14) (u' + 15)) := VGoodTail.tail_nn K O X hg (u' + 14) 0
  have et24 : (VGoodTail.tt K X (u' + 14) (u' + 15)) = (K.add (K.mul (co K X (u' + 14)) (co K X (u' + 14))) (K.neg (K.mul (co K X (u' + 13)) (co K X (u' + 15))))) := rfl
  have t25 : O.le K.zero (VGoodTail.tt K X (u' + 12) (u' + 19)) := VGoodTail.tail_nn K O X hg (u' + 12) 6
  have et25 : (VGoodTail.tt K X (u' + 12) (u' + 19)) = (K.add (K.mul (co K X (u' + 12)) (co K X (u' + 18))) (K.neg (K.mul (co K X (u' + 11)) (co K X (u' + 19))))) := rfl
  have t26 : O.le K.zero (VGoodTail.tt K X (u' + 10) (u' + 19)) := VGoodTail.tail_nn K O X hg (u' + 10) 8
  have et26 : (VGoodTail.tt K X (u' + 10) (u' + 19)) = (K.add (K.mul (co K X (u' + 10)) (co K X (u' + 18))) (K.neg (K.mul (co K X (u' + 9)) (co K X (u' + 19))))) := rfl
  have t27 : O.le K.zero (VGoodTail.tt K X (u' + 10) (u' + 11)) := VGoodTail.tail_nn K O X hg (u' + 10) 0
  have et27 : (VGoodTail.tt K X (u' + 10) (u' + 11)) = (K.add (K.mul (co K X (u' + 10)) (co K X (u' + 10))) (K.neg (K.mul (co K X (u' + 9)) (co K X (u' + 11))))) := rfl
  have t28 : O.le K.zero (VGoodTail.tt K X (u' + 8) (u' + 19)) := VGoodTail.tail_nn K O X hg (u' + 8) 10
  have et28 : (VGoodTail.tt K X (u' + 8) (u' + 19)) = (K.add (K.mul (co K X (u' + 8)) (co K X (u' + 18))) (K.neg (K.mul (co K X (u' + 7)) (co K X (u' + 19))))) := rfl
  have t29 : O.le K.zero (VGoodTail.tt K X (u' + 8) (u' + 11)) := VGoodTail.tail_nn K O X hg (u' + 8) 2
  have et29 : (VGoodTail.tt K X (u' + 8) (u' + 11)) = (K.add (K.mul (co K X (u' + 8)) (co K X (u' + 10))) (K.neg (K.mul (co K X (u' + 7)) (co K X (u' + 11))))) := rfl
  have t30 : O.le K.zero (VGoodTail.tt K X (u' + 6) (u' + 19)) := VGoodTail.tail_nn K O X hg (u' + 6) 12
  have et30 : (VGoodTail.tt K X (u' + 6) (u' + 19)) = (K.add (K.mul (co K X (u' + 6)) (co K X (u' + 18))) (K.neg (K.mul (co K X (u' + 5)) (co K X (u' + 19))))) := rfl
  have t31 : O.le K.zero (VGoodTail.tt K X (u' + 6) (u' + 17)) := VGoodTail.tail_nn K O X hg (u' + 6) 10
  have et31 : (VGoodTail.tt K X (u' + 6) (u' + 17)) = (K.add (K.mul (co K X (u' + 6)) (co K X (u' + 16))) (K.neg (K.mul (co K X (u' + 5)) (co K X (u' + 17))))) := rfl
  have t32 : O.le K.zero (VGoodTail.tt K X (u' + 6) (u' + 15)) := VGoodTail.tail_nn K O X hg (u' + 6) 8
  have et32 : (VGoodTail.tt K X (u' + 6) (u' + 15)) = (K.add (K.mul (co K X (u' + 6)) (co K X (u' + 14))) (K.neg (K.mul (co K X (u' + 5)) (co K X (u' + 15))))) := rfl
  have t33 : O.le K.zero (VGoodTail.tt K X (u' + 6) (u' + 13)) := VGoodTail.tail_nn K O X hg (u' + 6) 6
  have et33 : (VGoodTail.tt K X (u' + 6) (u' + 13)) = (K.add (K.mul (co K X (u' + 6)) (co K X (u' + 12))) (K.neg (K.mul (co K X (u' + 5)) (co K X (u' + 13))))) := rfl
  have t34 : O.le K.zero (VGoodTail.tt K X (u' + 6) (u' + 11)) := VGoodTail.tail_nn K O X hg (u' + 6) 4
  have et34 : (VGoodTail.tt K X (u' + 6) (u' + 11)) = (K.add (K.mul (co K X (u' + 6)) (co K X (u' + 10))) (K.neg (K.mul (co K X (u' + 5)) (co K X (u' + 11))))) := rfl
  have hp : 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.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.add (K.add (K.add (K.add (K.add (VGoodTail.tt K X (u' + 10) (u' + 15)) (VGoodTail.tt K  -- (+1807 chars; full line in the Zenodo source)
    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 _ _ (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 _ _ (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   -- (+836 chars; full line in the Zenodo source)
  refine O.le_trans _ _ _ hp (TripleE.le_of_eq' K O _ _ ?_)
  rw [kv, et0, et1, et2, et3, et4, et5, et6, et7, et8, et9, et10, et11, et12, et13, et14, et15, et16, et17, et18, et19, et20, et21, et22, et23, et24, et25, et26, et27, et28, et29, et30, et31, et32, et33, et34]
  ring_z

theorem kna_s_5 (X : List R) (hg : L1Full.Good K O X) (u' : Nat) :
    O.le K.zero (CBLib.kern K (Fa K X) (Ga K X) 5 (u' + 4) (u' + 10)) := by
  have kv : CBLib.kern K (Fa K X) (Ga K X) 5 (u' + 4) (u' + 10) = (K.add (K.add (K.add (K.add (K.add K.zero (K.add (K.mul (co K X (u' + 10)) (co K X (u' + 15))) (K.neg (K.mul (co K X (u' + 16)) (co K X (u' + 9)))))) (K.add (K.mul (K.add (co K X (u' + 11)) (co K X (u' + 9))) (K.add (co K X (u' + 16)) (co K X (u' + 14)))) (K.neg (K.mul (K.add (co K X (u' + 17)) (co K X (u' + 15))) (K.add (co K X (u'  -- (+1324 chars; full line in the Zenodo source)
  have t0 : O.le K.zero (VGoodTail.tt K X (u' + 10) (u' + 16)) := VGoodTail.tail_nn K O X hg (u' + 10) 5
  have et0 : (VGoodTail.tt K X (u' + 10) (u' + 16)) = (K.add (K.mul (co K X (u' + 10)) (co K X (u' + 15))) (K.neg (K.mul (co K X (u' + 9)) (co K X (u' + 16))))) := rfl
  have t1 : O.le K.zero (VGoodTail.tt K X (u' + 11) (u' + 17)) := VGoodTail.tail_nn K O X hg (u' + 11) 5
  have et1 : (VGoodTail.tt K X (u' + 11) (u' + 17)) = (K.add (K.mul (co K X (u' + 11)) (co K X (u' + 16))) (K.neg (K.mul (co K X (u' + 10)) (co K X (u' + 17))))) := rfl
  have t2 : O.le K.zero (VGoodTail.tt K X (u' + 11) (u' + 15)) := VGoodTail.tail_nn K O X hg (u' + 11) 3
  have et2 : (VGoodTail.tt K X (u' + 11) (u' + 15)) = (K.add (K.mul (co K X (u' + 11)) (co K X (u' + 14))) (K.neg (K.mul (co K X (u' + 10)) (co K X (u' + 15))))) := rfl
  have t3 : O.le K.zero (VGoodTail.tt K X (u' + 9) (u' + 17)) := VGoodTail.tail_nn K O X hg (u' + 9) 7
  have et3 : (VGoodTail.tt K X (u' + 9) (u' + 17)) = (K.add (K.mul (co K X (u' + 9)) (co K X (u' + 16))) (K.neg (K.mul (co K X (u' + 8)) (co K X (u' + 17))))) := rfl
  have t4 : O.le K.zero (VGoodTail.tt K X (u' + 9) (u' + 15)) := VGoodTail.tail_nn K O X hg (u' + 9) 5
  have et4 : (VGoodTail.tt K X (u' + 9) (u' + 15)) = (K.add (K.mul (co K X (u' + 9)) (co K X (u' + 14))) (K.neg (K.mul (co K X (u' + 8)) (co K X (u' + 15))))) := rfl
  have t5 : O.le K.zero (VGoodTail.tt K X (u' + 12) (u' + 18)) := VGoodTail.tail_nn K O X hg (u' + 12) 5
  have et5 : (VGoodTail.tt K X (u' + 12) (u' + 18)) = (K.add (K.mul (co K X (u' + 12)) (co K X (u' + 17))) (K.neg (K.mul (co K X (u' + 11)) (co K X (u' + 18))))) := rfl
  have t6 : O.le K.zero (VGoodTail.tt K X (u' + 12) (u' + 16)) := VGoodTail.tail_nn K O X hg (u' + 12) 3
  have et6 : (VGoodTail.tt K X (u' + 12) (u' + 16)) = (K.add (K.mul (co K X (u' + 12)) (co K X (u' + 15))) (K.neg (K.mul (co K X (u' + 11)) (co K X (u' + 16))))) := rfl
  have t7 : O.le K.zero (VGoodTail.tt K X (u' + 12) (u' + 14)) := VGoodTail.tail_nn K O X hg (u' + 12) 1
  have et7 : (VGoodTail.tt K X (u' + 12) (u' + 14)) = (K.add (K.mul (co K X (u' + 12)) (co K X (u' + 13))) (K.neg (K.mul (co K X (u' + 11)) (co K X (u' + 14))))) := rfl
  have t8 : O.le K.zero (VGoodTail.tt K X (u' + 10) (u' + 18)) := VGoodTail.tail_nn K O X hg (u' + 10) 7
  have et8 : (VGoodTail.tt K X (u' + 10) (u' + 18)) = (K.add (K.mul (co K X (u' + 10)) (co K X (u' + 17))) (K.neg (K.mul (co K X (u' + 9)) (co K X (u' + 18))))) := rfl
  have t9 : O.le K.zero (VGoodTail.tt K X (u' + 10) (u' + 14)) := VGoodTail.tail_nn K O X hg (u' + 10) 3
  have et9 : (VGoodTail.tt K X (u' + 10) (u' + 14)) = (K.add (K.mul (co K X (u' + 10)) (co K X (u' + 13))) (K.neg (K.mul (co K X (u' + 9)) (co K X (u' + 14))))) := rfl
  have t10 : O.le K.zero (VGoodTail.tt K X (u' + 8) (u' + 18)) := VGoodTail.tail_nn K O X hg (u' + 8) 9
  have et10 : (VGoodTail.tt K X (u' + 8) (u' + 18)) = (K.add (K.mul (co K X (u' + 8)) (co K X (u' + 17))) (K.neg (K.mul (co K X (u' + 7)) (co K X (u' + 18))))) := rfl
  have t11 : O.le K.zero (VGoodTail.tt K X (u' + 8) (u' + 16)) := VGoodTail.tail_nn K O X hg (u' + 8) 7
  have et11 : (VGoodTail.tt K X (u' + 8) (u' + 16)) = (K.add (K.mul (co K X (u' + 8)) (co K X (u' + 15))) (K.neg (K.mul (co K X (u' + 7)) (co K X (u' + 16))))) := rfl
  have t12 : O.le K.zero (VGoodTail.tt K X (u' + 8) (u' + 14)) := VGoodTail.tail_nn K O X hg (u' + 8) 5
  have et12 : (VGoodTail.tt K X (u' + 8) (u' + 14)) = (K.add (K.mul (co K X (u' + 8)) (co K X (u' + 13))) (K.neg (K.mul (co K X (u' + 7)) (co K X (u' + 14))))) := rfl
  have t13 : O.le K.zero (VGoodTail.tt K X (u' + 13) (u' + 19)) := VGoodTail.tail_nn K O X hg (u' + 13) 5
  have et13 : (VGoodTail.tt K X (u' + 13) (u' + 19)) = (K.add (K.mul (co K X (u' + 13)) (co K X (u' + 18))) (K.neg (K.mul (co K X (u' + 12)) (co K X (u' + 19))))) := rfl
  have t14 : O.le K.zero (VGoodTail.tt K X (u' + 13) (u' + 17)) := VGoodTail.tail_nn K O X hg (u' + 13) 3
  have et14 : (VGoodTail.tt K X (u' + 13) (u' + 17)) = (K.add (K.mul (co K X (u' + 13)) (co K X (u' + 16))) (K.neg (K.mul (co K X (u' + 12)) (co K X (u' + 17))))) := rfl
  have t15 : O.le K.zero (VGoodTail.tt K X (u' + 13) (u' + 15)) := VGoodTail.tail_nn K O X hg (u' + 13) 1
  have et15 : (VGoodTail.tt K X (u' + 13) (u' + 15)) = (K.add (K.mul (co K X (u' + 13)) (co K X (u' + 14))) (K.neg (K.mul (co K X (u' + 12)) (co K X (u' + 15))))) := rfl
  have t16 : O.le K.zero (VGoodTail.tt K X (u' + 11) (u' + 19)) := VGoodTail.tail_nn K O X hg (u' + 11) 7
  have et16 : (VGoodTail.tt K X (u' + 11) (u' + 19)) = (K.add (K.mul (co K X (u' + 11)) (co K X (u' + 18))) (K.neg (K.mul (co K X (u' + 10)) (co K X (u' + 19))))) := rfl
  have t17 : O.le K.zero (VGoodTail.tt K X (u' + 11) (u' + 13)) := VGoodTail.tail_nn K O X hg (u' + 11) 1
  have et17 : (VGoodTail.tt K X (u' + 11) (u' + 13)) = (K.add (K.mul (co K X (u' + 11)) (co K X (u' + 12))) (K.neg (K.mul (co K X (u' + 10)) (co K X (u' + 13))))) := rfl
  have t18 : O.le K.zero (VGoodTail.tt K X (u' + 9) (u' + 19)) := VGoodTail.tail_nn K O X hg (u' + 9) 9
  have et18 : (VGoodTail.tt K X (u' + 9) (u' + 19)) = (K.add (K.mul (co K X (u' + 9)) (co K X (u' + 18))) (K.neg (K.mul (co K X (u' + 8)) (co K X (u' + 19))))) := rfl
  have t19 : O.le K.zero (VGoodTail.tt K X (u' + 9) (u' + 13)) := VGoodTail.tail_nn K O X hg (u' + 9) 3
  have et19 : (VGoodTail.tt K X (u' + 9) (u' + 13)) = (K.add (K.mul (co K X (u' + 9)) (co K X (u' + 12))) (K.neg (K.mul (co K X (u' + 8)) (co K X (u' + 13))))) := rfl
  have t20 : O.le K.zero (VGoodTail.tt K X (u' + 7) (u' + 19)) := VGoodTail.tail_nn K O X hg (u' + 7) 11
  have et20 : (VGoodTail.tt K X (u' + 7) (u' + 19)) = (K.add (K.mul (co K X (u' + 7)) (co K X (u' + 18))) (K.neg (K.mul (co K X (u' + 6)) (co K X (u' + 19))))) := rfl
  have t21 : O.le K.zero (VGoodTail.tt K X (u' + 7) (u' + 17)) := VGoodTail.tail_nn K O X hg (u' + 7) 9
  have et21 : (VGoodTail.tt K X (u' + 7) (u' + 17)) = (K.add (K.mul (co K X (u' + 7)) (co K X (u' + 16))) (K.neg (K.mul (co K X (u' + 6)) (co K X (u' + 17))))) := rfl
  have t22 : O.le K.zero (VGoodTail.tt K X (u' + 7) (u' + 15)) := VGoodTail.tail_nn K O X hg (u' + 7) 7
  have et22 : (VGoodTail.tt K X (u' + 7) (u' + 15)) = (K.add (K.mul (co K X (u' + 7)) (co K X (u' + 14))) (K.neg (K.mul (co K X (u' + 6)) (co K X (u' + 15))))) := rfl
  have t23 : O.le K.zero (VGoodTail.tt K X (u' + 7) (u' + 13)) := VGoodTail.tail_nn K O X hg (u' + 7) 5
  have et23 : (VGoodTail.tt K X (u' + 7) (u' + 13)) = (K.add (K.mul (co K X (u' + 7)) (co K X (u' + 12))) (K.neg (K.mul (co K X (u' + 6)) (co K X (u' + 13))))) := rfl
  have t24 : O.le K.zero (VGoodTail.tt K X (u' + 14) (u' + 20)) := VGoodTail.tail_nn K O X hg (u' + 14) 5
  have et24 : (VGoodTail.tt K X (u' + 14) (u' + 20)) = (K.add (K.mul (co K X (u' + 14)) (co K X (u' + 19))) (K.neg (K.mul (co K X (u' + 13)) (co K X (u' + 20))))) := rfl
  have t25 : O.le K.zero (VGoodTail.tt K X (u' + 14) (u' + 18)) := VGoodTail.tail_nn K O X hg (u' + 14) 3
  have et25 : (VGoodTail.tt K X (u' + 14) (u' + 18)) = (K.add (K.mul (co K X (u' + 14)) (co K X (u' + 17))) (K.neg (K.mul (co K X (u' + 13)) (co K X (u' + 18))))) := rfl
  have t26 : O.le K.zero (VGoodTail.tt K X (u' + 14) (u' + 16)) := VGoodTail.tail_nn K O X hg (u' + 14) 1
  have et26 : (VGoodTail.tt K X (u' + 14) (u' + 16)) = (K.add (K.mul (co K X (u' + 14)) (co K X (u' + 15))) (K.neg (K.mul (co K X (u' + 13)) (co K X (u' + 16))))) := rfl
  have t27 : O.le K.zero (VGoodTail.tt K X (u' + 12) (u' + 20)) := VGoodTail.tail_nn K O X hg (u' + 12) 7
  have et27 : (VGoodTail.tt K X (u' + 12) (u' + 20)) = (K.add (K.mul (co K X (u' + 12)) (co K X (u' + 19))) (K.neg (K.mul (co K X (u' + 11)) (co K X (u' + 20))))) := rfl
  have t28 : O.le K.zero (VGoodTail.tt K X (u' + 10) (u' + 20)) := VGoodTail.tail_nn K O X hg (u' + 10) 9
  have et28 : (VGoodTail.tt K X (u' + 10) (u' + 20)) = (K.add (K.mul (co K X (u' + 10)) (co K X (u' + 19))) (K.neg (K.mul (co K X (u' + 9)) (co K X (u' + 20))))) := rfl
  have t29 : O.le K.zero (VGoodTail.tt K X (u' + 10) (u' + 12)) := VGoodTail.tail_nn K O X hg (u' + 10) 1
  have et29 : (VGoodTail.tt K X (u' + 10) (u' + 12)) = (K.add (K.mul (co K X (u' + 10)) (co K X (u' + 11))) (K.neg (K.mul (co K X (u' + 9)) (co K X (u' + 12))))) := rfl
  have t30 : O.le K.zero (VGoodTail.tt K X (u' + 8) (u' + 20)) := VGoodTail.tail_nn K O X hg (u' + 8) 11
  have et30 : (VGoodTail.tt K X (u' + 8) (u' + 20)) = (K.add (K.mul (co K X (u' + 8)) (co K X (u' + 19))) (K.neg (K.mul (co K X (u' + 7)) (co K X (u' + 20))))) := rfl
  have t31 : O.le K.zero (VGoodTail.tt K X (u' + 8) (u' + 12)) := VGoodTail.tail_nn K O X hg (u' + 8) 3
  have et31 : (VGoodTail.tt K X (u' + 8) (u' + 12)) = (K.add (K.mul (co K X (u' + 8)) (co K X (u' + 11))) (K.neg (K.mul (co K X (u' + 7)) (co K X (u' + 12))))) := rfl
  have t32 : O.le K.zero (VGoodTail.tt K X (u' + 6) (u' + 20)) := VGoodTail.tail_nn K O X hg (u' + 6) 13
  have et32 : (VGoodTail.tt K X (u' + 6) (u' + 20)) = (K.add (K.mul (co K X (u' + 6)) (co K X (u' + 19))) (K.neg (K.mul (co K X (u' + 5)) (co K X (u' + 20))))) := rfl
  have t33 : O.le K.zero (VGoodTail.tt K X (u' + 6) (u' + 18)) := VGoodTail.tail_nn K O X hg (u' + 6) 11
  have et33 : (VGoodTail.tt K X (u' + 6) (u' + 18)) = (K.add (K.mul (co K X (u' + 6)) (co K X (u' + 17))) (K.neg (K.mul (co K X (u' + 5)) (co K X (u' + 18))))) := rfl
  have t34 : O.le K.zero (VGoodTail.tt K X (u' + 6) (u' + 16)) := VGoodTail.tail_nn K O X hg (u' + 6) 9
  have et34 : (VGoodTail.tt K X (u' + 6) (u' + 16)) = (K.add (K.mul (co K X (u' + 6)) (co K X (u' + 15))) (K.neg (K.mul (co K X (u' + 5)) (co K X (u' + 16))))) := rfl
  have t35 : O.le K.zero (VGoodTail.tt K X (u' + 6) (u' + 14)) := VGoodTail.tail_nn K O X hg (u' + 6) 7
  have et35 : (VGoodTail.tt K X (u' + 6) (u' + 14)) = (K.add (K.mul (co K X (u' + 6)) (co K X (u' + 13))) (K.neg (K.mul (co K X (u' + 5)) (co K X (u' + 14))))) := rfl
  have t36 : O.le K.zero (VGoodTail.tt K X (u' + 6) (u' + 12)) := VGoodTail.tail_nn K O X hg (u' + 6) 5
  have et36 : (VGoodTail.tt K X (u' + 6) (u' + 12)) = (K.add (K.mul (co K X (u' + 6)) (co K X (u' + 11))) (K.neg (K.mul (co K X (u' + 5)) (co K X (u' + 12))))) := rfl
  have hp : 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.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.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (VGoodTail.tt K X (u' + 10) (u' +  -- (+1948 chars; full line in the Zenodo source)
    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 _ _ (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 _ _ (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   -- (+916 chars; full line in the Zenodo source)
  refine O.le_trans _ _ _ hp (TripleE.le_of_eq' K O _ _ ?_)
  rw [kv, et0, et1, et2, et3, et4, et5, et6, et7, et8, et9, et10, et11, et12, et13, et14, et15, et16, et17, et18, et19, et20, et21, et22, et23, et24, et25, et26, et27, et28, et29, et30, et31, et32, et33, et34, et35, et36]
  ring_z

theorem kna_s_6 (X : List R) (hg : L1Full.Good K O X) (u' : Nat) :
    O.le K.zero (CBLib.kern K (Fa K X) (Ga K X) 5 (u' + 4) (u' + 11)) := by
  have kv : CBLib.kern K (Fa K X) (Ga K X) 5 (u' + 4) (u' + 11) = (K.add (K.add (K.add (K.add (K.add K.zero (K.add (K.mul (co K X (u' + 10)) (co K X (u' + 16))) (K.neg (K.mul (co K X (u' + 17)) (co K X (u' + 9)))))) (K.add (K.mul (K.add (co K X (u' + 11)) (co K X (u' + 9))) (K.add (co K X (u' + 17)) (co K X (u' + 15)))) (K.neg (K.mul (K.add (co K X (u' + 18)) (co K X (u' + 16))) (K.add (co K X (u'  -- (+1324 chars; full line in the Zenodo source)
  have t0 : O.le K.zero (VGoodTail.tt K X (u' + 10) (u' + 17)) := VGoodTail.tail_nn K O X hg (u' + 10) 6
  have et0 : (VGoodTail.tt K X (u' + 10) (u' + 17)) = (K.add (K.mul (co K X (u' + 10)) (co K X (u' + 16))) (K.neg (K.mul (co K X (u' + 9)) (co K X (u' + 17))))) := rfl
  have t1 : O.le K.zero (VGoodTail.tt K X (u' + 11) (u' + 18)) := VGoodTail.tail_nn K O X hg (u' + 11) 6
  have et1 : (VGoodTail.tt K X (u' + 11) (u' + 18)) = (K.add (K.mul (co K X (u' + 11)) (co K X (u' + 17))) (K.neg (K.mul (co K X (u' + 10)) (co K X (u' + 18))))) := rfl
  have t2 : O.le K.zero (VGoodTail.tt K X (u' + 11) (u' + 16)) := VGoodTail.tail_nn K O X hg (u' + 11) 4
  have et2 : (VGoodTail.tt K X (u' + 11) (u' + 16)) = (K.add (K.mul (co K X (u' + 11)) (co K X (u' + 15))) (K.neg (K.mul (co K X (u' + 10)) (co K X (u' + 16))))) := rfl
  have t3 : O.le K.zero (VGoodTail.tt K X (u' + 9) (u' + 18)) := VGoodTail.tail_nn K O X hg (u' + 9) 8
  have et3 : (VGoodTail.tt K X (u' + 9) (u' + 18)) = (K.add (K.mul (co K X (u' + 9)) (co K X (u' + 17))) (K.neg (K.mul (co K X (u' + 8)) (co K X (u' + 18))))) := rfl
  have t4 : O.le K.zero (VGoodTail.tt K X (u' + 9) (u' + 16)) := VGoodTail.tail_nn K O X hg (u' + 9) 6
  have et4 : (VGoodTail.tt K X (u' + 9) (u' + 16)) = (K.add (K.mul (co K X (u' + 9)) (co K X (u' + 15))) (K.neg (K.mul (co K X (u' + 8)) (co K X (u' + 16))))) := rfl
  have t5 : O.le K.zero (VGoodTail.tt K X (u' + 12) (u' + 19)) := VGoodTail.tail_nn K O X hg (u' + 12) 6
  have et5 : (VGoodTail.tt K X (u' + 12) (u' + 19)) = (K.add (K.mul (co K X (u' + 12)) (co K X (u' + 18))) (K.neg (K.mul (co K X (u' + 11)) (co K X (u' + 19))))) := rfl
  have t6 : O.le K.zero (VGoodTail.tt K X (u' + 12) (u' + 17)) := VGoodTail.tail_nn K O X hg (u' + 12) 4
  have et6 : (VGoodTail.tt K X (u' + 12) (u' + 17)) = (K.add (K.mul (co K X (u' + 12)) (co K X (u' + 16))) (K.neg (K.mul (co K X (u' + 11)) (co K X (u' + 17))))) := rfl
  have t7 : O.le K.zero (VGoodTail.tt K X (u' + 12) (u' + 15)) := VGoodTail.tail_nn K O X hg (u' + 12) 2
  have et7 : (VGoodTail.tt K X (u' + 12) (u' + 15)) = (K.add (K.mul (co K X (u' + 12)) (co K X (u' + 14))) (K.neg (K.mul (co K X (u' + 11)) (co K X (u' + 15))))) := rfl
  have t8 : O.le K.zero (VGoodTail.tt K X (u' + 10) (u' + 19)) := VGoodTail.tail_nn K O X hg (u' + 10) 8
  have et8 : (VGoodTail.tt K X (u' + 10) (u' + 19)) = (K.add (K.mul (co K X (u' + 10)) (co K X (u' + 18))) (K.neg (K.mul (co K X (u' + 9)) (co K X (u' + 19))))) := rfl
  have t9 : O.le K.zero (VGoodTail.tt K X (u' + 10) (u' + 15)) := VGoodTail.tail_nn K O X hg (u' + 10) 4
  have et9 : (VGoodTail.tt K X (u' + 10) (u' + 15)) = (K.add (K.mul (co K X (u' + 10)) (co K X (u' + 14))) (K.neg (K.mul (co K X (u' + 9)) (co K X (u' + 15))))) := rfl
  have t10 : O.le K.zero (VGoodTail.tt K X (u' + 8) (u' + 19)) := VGoodTail.tail_nn K O X hg (u' + 8) 10
  have et10 : (VGoodTail.tt K X (u' + 8) (u' + 19)) = (K.add (K.mul (co K X (u' + 8)) (co K X (u' + 18))) (K.neg (K.mul (co K X (u' + 7)) (co K X (u' + 19))))) := rfl
  have t11 : O.le K.zero (VGoodTail.tt K X (u' + 8) (u' + 17)) := VGoodTail.tail_nn K O X hg (u' + 8) 8
  have et11 : (VGoodTail.tt K X (u' + 8) (u' + 17)) = (K.add (K.mul (co K X (u' + 8)) (co K X (u' + 16))) (K.neg (K.mul (co K X (u' + 7)) (co K X (u' + 17))))) := rfl
  have t12 : O.le K.zero (VGoodTail.tt K X (u' + 8) (u' + 15)) := VGoodTail.tail_nn K O X hg (u' + 8) 6
  have et12 : (VGoodTail.tt K X (u' + 8) (u' + 15)) = (K.add (K.mul (co K X (u' + 8)) (co K X (u' + 14))) (K.neg (K.mul (co K X (u' + 7)) (co K X (u' + 15))))) := rfl
  have t13 : O.le K.zero (VGoodTail.tt K X (u' + 13) (u' + 20)) := VGoodTail.tail_nn K O X hg (u' + 13) 6
  have et13 : (VGoodTail.tt K X (u' + 13) (u' + 20)) = (K.add (K.mul (co K X (u' + 13)) (co K X (u' + 19))) (K.neg (K.mul (co K X (u' + 12)) (co K X (u' + 20))))) := rfl
  have t14 : O.le K.zero (VGoodTail.tt K X (u' + 13) (u' + 18)) := VGoodTail.tail_nn K O X hg (u' + 13) 4
  have et14 : (VGoodTail.tt K X (u' + 13) (u' + 18)) = (K.add (K.mul (co K X (u' + 13)) (co K X (u' + 17))) (K.neg (K.mul (co K X (u' + 12)) (co K X (u' + 18))))) := rfl
  have t15 : O.le K.zero (VGoodTail.tt K X (u' + 13) (u' + 16)) := VGoodTail.tail_nn K O X hg (u' + 13) 2
  have et15 : (VGoodTail.tt K X (u' + 13) (u' + 16)) = (K.add (K.mul (co K X (u' + 13)) (co K X (u' + 15))) (K.neg (K.mul (co K X (u' + 12)) (co K X (u' + 16))))) := rfl
  have t16 : O.le K.zero (VGoodTail.tt K X (u' + 11) (u' + 20)) := VGoodTail.tail_nn K O X hg (u' + 11) 8
  have et16 : (VGoodTail.tt K X (u' + 11) (u' + 20)) = (K.add (K.mul (co K X (u' + 11)) (co K X (u' + 19))) (K.neg (K.mul (co K X (u' + 10)) (co K X (u' + 20))))) := rfl
  have t17 : O.le K.zero (VGoodTail.tt K X (u' + 11) (u' + 14)) := VGoodTail.tail_nn K O X hg (u' + 11) 2
  have et17 : (VGoodTail.tt K X (u' + 11) (u' + 14)) = (K.add (K.mul (co K X (u' + 11)) (co K X (u' + 13))) (K.neg (K.mul (co K X (u' + 10)) (co K X (u' + 14))))) := rfl
  have t18 : O.le K.zero (VGoodTail.tt K X (u' + 9) (u' + 20)) := VGoodTail.tail_nn K O X hg (u' + 9) 10
  have et18 : (VGoodTail.tt K X (u' + 9) (u' + 20)) = (K.add (K.mul (co K X (u' + 9)) (co K X (u' + 19))) (K.neg (K.mul (co K X (u' + 8)) (co K X (u' + 20))))) := rfl
  have t19 : O.le K.zero (VGoodTail.tt K X (u' + 9) (u' + 14)) := VGoodTail.tail_nn K O X hg (u' + 9) 4
  have et19 : (VGoodTail.tt K X (u' + 9) (u' + 14)) = (K.add (K.mul (co K X (u' + 9)) (co K X (u' + 13))) (K.neg (K.mul (co K X (u' + 8)) (co K X (u' + 14))))) := rfl
  have t20 : O.le K.zero (VGoodTail.tt K X (u' + 7) (u' + 20)) := VGoodTail.tail_nn K O X hg (u' + 7) 12
  have et20 : (VGoodTail.tt K X (u' + 7) (u' + 20)) = (K.add (K.mul (co K X (u' + 7)) (co K X (u' + 19))) (K.neg (K.mul (co K X (u' + 6)) (co K X (u' + 20))))) := rfl
  have t21 : O.le K.zero (VGoodTail.tt K X (u' + 7) (u' + 18)) := VGoodTail.tail_nn K O X hg (u' + 7) 10
  have et21 : (VGoodTail.tt K X (u' + 7) (u' + 18)) = (K.add (K.mul (co K X (u' + 7)) (co K X (u' + 17))) (K.neg (K.mul (co K X (u' + 6)) (co K X (u' + 18))))) := rfl
  have t22 : O.le K.zero (VGoodTail.tt K X (u' + 7) (u' + 16)) := VGoodTail.tail_nn K O X hg (u' + 7) 8
  have et22 : (VGoodTail.tt K X (u' + 7) (u' + 16)) = (K.add (K.mul (co K X (u' + 7)) (co K X (u' + 15))) (K.neg (K.mul (co K X (u' + 6)) (co K X (u' + 16))))) := rfl
  have t23 : O.le K.zero (VGoodTail.tt K X (u' + 7) (u' + 14)) := VGoodTail.tail_nn K O X hg (u' + 7) 6
  have et23 : (VGoodTail.tt K X (u' + 7) (u' + 14)) = (K.add (K.mul (co K X (u' + 7)) (co K X (u' + 13))) (K.neg (K.mul (co K X (u' + 6)) (co K X (u' + 14))))) := rfl
  have t24 : O.le K.zero (VGoodTail.tt K X (u' + 14) (u' + 21)) := VGoodTail.tail_nn K O X hg (u' + 14) 6
  have et24 : (VGoodTail.tt K X (u' + 14) (u' + 21)) = (K.add (K.mul (co K X (u' + 14)) (co K X (u' + 20))) (K.neg (K.mul (co K X (u' + 13)) (co K X (u' + 21))))) := rfl
  have t25 : O.le K.zero (VGoodTail.tt K X (u' + 14) (u' + 19)) := VGoodTail.tail_nn K O X hg (u' + 14) 4
  have et25 : (VGoodTail.tt K X (u' + 14) (u' + 19)) = (K.add (K.mul (co K X (u' + 14)) (co K X (u' + 18))) (K.neg (K.mul (co K X (u' + 13)) (co K X (u' + 19))))) := rfl
  have t26 : O.le K.zero (VGoodTail.tt K X (u' + 14) (u' + 17)) := VGoodTail.tail_nn K O X hg (u' + 14) 2
  have et26 : (VGoodTail.tt K X (u' + 14) (u' + 17)) = (K.add (K.mul (co K X (u' + 14)) (co K X (u' + 16))) (K.neg (K.mul (co K X (u' + 13)) (co K X (u' + 17))))) := rfl
  have t27 : O.le K.zero (VGoodTail.tt K X (u' + 14) (u' + 15)) := VGoodTail.tail_nn K O X hg (u' + 14) 0
  have et27 : (VGoodTail.tt K X (u' + 14) (u' + 15)) = (K.add (K.mul (co K X (u' + 14)) (co K X (u' + 14))) (K.neg (K.mul (co K X (u' + 13)) (co K X (u' + 15))))) := rfl
  have t28 : O.le K.zero (VGoodTail.tt K X (u' + 12) (u' + 21)) := VGoodTail.tail_nn K O X hg (u' + 12) 8
  have et28 : (VGoodTail.tt K X (u' + 12) (u' + 21)) = (K.add (K.mul (co K X (u' + 12)) (co K X (u' + 20))) (K.neg (K.mul (co K X (u' + 11)) (co K X (u' + 21))))) := rfl
  have t29 : O.le K.zero (VGoodTail.tt K X (u' + 12) (u' + 13)) := VGoodTail.tail_nn K O X hg (u' + 12) 0
  have et29 : (VGoodTail.tt K X (u' + 12) (u' + 13)) = (K.add (K.mul (co K X (u' + 12)) (co K X (u' + 12))) (K.neg (K.mul (co K X (u' + 11)) (co K X (u' + 13))))) := rfl
  have t30 : O.le K.zero (VGoodTail.tt K X (u' + 10) (u' + 21)) := VGoodTail.tail_nn K O X hg (u' + 10) 10
  have et30 : (VGoodTail.tt K X (u' + 10) (u' + 21)) = (K.add (K.mul (co K X (u' + 10)) (co K X (u' + 20))) (K.neg (K.mul (co K X (u' + 9)) (co K X (u' + 21))))) := rfl
  have t31 : O.le K.zero (VGoodTail.tt K X (u' + 10) (u' + 13)) := VGoodTail.tail_nn K O X hg (u' + 10) 2
  have et31 : (VGoodTail.tt K X (u' + 10) (u' + 13)) = (K.add (K.mul (co K X (u' + 10)) (co K X (u' + 12))) (K.neg (K.mul (co K X (u' + 9)) (co K X (u' + 13))))) := rfl
  have t32 : O.le K.zero (VGoodTail.tt K X (u' + 8) (u' + 21)) := VGoodTail.tail_nn K O X hg (u' + 8) 12
  have et32 : (VGoodTail.tt K X (u' + 8) (u' + 21)) = (K.add (K.mul (co K X (u' + 8)) (co K X (u' + 20))) (K.neg (K.mul (co K X (u' + 7)) (co K X (u' + 21))))) := rfl
  have t33 : O.le K.zero (VGoodTail.tt K X (u' + 8) (u' + 13)) := VGoodTail.tail_nn K O X hg (u' + 8) 4
  have et33 : (VGoodTail.tt K X (u' + 8) (u' + 13)) = (K.add (K.mul (co K X (u' + 8)) (co K X (u' + 12))) (K.neg (K.mul (co K X (u' + 7)) (co K X (u' + 13))))) := rfl
  have t34 : O.le K.zero (VGoodTail.tt K X (u' + 6) (u' + 21)) := VGoodTail.tail_nn K O X hg (u' + 6) 14
  have et34 : (VGoodTail.tt K X (u' + 6) (u' + 21)) = (K.add (K.mul (co K X (u' + 6)) (co K X (u' + 20))) (K.neg (K.mul (co K X (u' + 5)) (co K X (u' + 21))))) := rfl
  have t35 : O.le K.zero (VGoodTail.tt K X (u' + 6) (u' + 19)) := VGoodTail.tail_nn K O X hg (u' + 6) 12
  have et35 : (VGoodTail.tt K X (u' + 6) (u' + 19)) = (K.add (K.mul (co K X (u' + 6)) (co K X (u' + 18))) (K.neg (K.mul (co K X (u' + 5)) (co K X (u' + 19))))) := rfl
  have t36 : O.le K.zero (VGoodTail.tt K X (u' + 6) (u' + 17)) := VGoodTail.tail_nn K O X hg (u' + 6) 10
  have et36 : (VGoodTail.tt K X (u' + 6) (u' + 17)) = (K.add (K.mul (co K X (u' + 6)) (co K X (u' + 16))) (K.neg (K.mul (co K X (u' + 5)) (co K X (u' + 17))))) := rfl
  have t37 : O.le K.zero (VGoodTail.tt K X (u' + 6) (u' + 15)) := VGoodTail.tail_nn K O X hg (u' + 6) 8
  have et37 : (VGoodTail.tt K X (u' + 6) (u' + 15)) = (K.add (K.mul (co K X (u' + 6)) (co K X (u' + 14))) (K.neg (K.mul (co K X (u' + 5)) (co K X (u' + 15))))) := rfl
  have t38 : O.le K.zero (VGoodTail.tt K X (u' + 6) (u' + 13)) := VGoodTail.tail_nn K O X hg (u' + 6) 6
  have et38 : (VGoodTail.tt K X (u' + 6) (u' + 13)) = (K.add (K.mul (co K X (u' + 6)) (co K X (u' + 12))) (K.neg (K.mul (co K X (u' + 5)) (co K X (u' + 13))))) := rfl
  have hp : 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.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.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (VGoodTail.t  -- (+2089 chars; full line in the Zenodo source)
    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 _ _ (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 _ _ (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   -- (+996 chars; full line in the Zenodo source)
  refine O.le_trans _ _ _ hp (TripleE.le_of_eq' K O _ _ ?_)
  rw [kv, et0, et1, et2, et3, et4, et5, et6, et7, et8, et9, et10, et11, et12, et13, et14, et15, et16, et17, et18, et19, et20, et21, et22, et23, et24, et25, et26, et27, et28, et29, et30, et31, et32, et33, et34, et35, et36, et37, et38]
  ring_z

theorem kna_s_7 (X : List R) (hg : L1Full.Good K O X) (u' : Nat) :
    O.le K.zero (CBLib.kern K (Fa K X) (Ga K X) 5 (u' + 4) (u' + 12)) := by
  have kv : CBLib.kern K (Fa K X) (Ga K X) 5 (u' + 4) (u' + 12) = (K.add (K.add (K.add (K.add (K.add K.zero (K.add (K.mul (co K X (u' + 10)) (co K X (u' + 17))) (K.neg (K.mul (co K X (u' + 18)) (co K X (u' + 9)))))) (K.add (K.mul (K.add (co K X (u' + 11)) (co K X (u' + 9))) (K.add (co K X (u' + 18)) (co K X (u' + 16)))) (K.neg (K.mul (K.add (co K X (u' + 19)) (co K X (u' + 17))) (K.add (co K X (u'  -- (+1324 chars; full line in the Zenodo source)
  have t0 : O.le K.zero (VGoodTail.tt K X (u' + 10) (u' + 18)) := VGoodTail.tail_nn K O X hg (u' + 10) 7
  have et0 : (VGoodTail.tt K X (u' + 10) (u' + 18)) = (K.add (K.mul (co K X (u' + 10)) (co K X (u' + 17))) (K.neg (K.mul (co K X (u' + 9)) (co K X (u' + 18))))) := rfl
  have t1 : O.le K.zero (VGoodTail.tt K X (u' + 11) (u' + 19)) := VGoodTail.tail_nn K O X hg (u' + 11) 7
  have et1 : (VGoodTail.tt K X (u' + 11) (u' + 19)) = (K.add (K.mul (co K X (u' + 11)) (co K X (u' + 18))) (K.neg (K.mul (co K X (u' + 10)) (co K X (u' + 19))))) := rfl
  have t2 : O.le K.zero (VGoodTail.tt K X (u' + 11) (u' + 17)) := VGoodTail.tail_nn K O X hg (u' + 11) 5
  have et2 : (VGoodTail.tt K X (u' + 11) (u' + 17)) = (K.add (K.mul (co K X (u' + 11)) (co K X (u' + 16))) (K.neg (K.mul (co K X (u' + 10)) (co K X (u' + 17))))) := rfl
  have t3 : O.le K.zero (VGoodTail.tt K X (u' + 9) (u' + 19)) := VGoodTail.tail_nn K O X hg (u' + 9) 9
  have et3 : (VGoodTail.tt K X (u' + 9) (u' + 19)) = (K.add (K.mul (co K X (u' + 9)) (co K X (u' + 18))) (K.neg (K.mul (co K X (u' + 8)) (co K X (u' + 19))))) := rfl
  have t4 : O.le K.zero (VGoodTail.tt K X (u' + 9) (u' + 17)) := VGoodTail.tail_nn K O X hg (u' + 9) 7
  have et4 : (VGoodTail.tt K X (u' + 9) (u' + 17)) = (K.add (K.mul (co K X (u' + 9)) (co K X (u' + 16))) (K.neg (K.mul (co K X (u' + 8)) (co K X (u' + 17))))) := rfl
  have t5 : O.le K.zero (VGoodTail.tt K X (u' + 12) (u' + 20)) := VGoodTail.tail_nn K O X hg (u' + 12) 7
  have et5 : (VGoodTail.tt K X (u' + 12) (u' + 20)) = (K.add (K.mul (co K X (u' + 12)) (co K X (u' + 19))) (K.neg (K.mul (co K X (u' + 11)) (co K X (u' + 20))))) := rfl
  have t6 : O.le K.zero (VGoodTail.tt K X (u' + 12) (u' + 18)) := VGoodTail.tail_nn K O X hg (u' + 12) 5
  have et6 : (VGoodTail.tt K X (u' + 12) (u' + 18)) = (K.add (K.mul (co K X (u' + 12)) (co K X (u' + 17))) (K.neg (K.mul (co K X (u' + 11)) (co K X (u' + 18))))) := rfl
  have t7 : O.le K.zero (VGoodTail.tt K X (u' + 12) (u' + 16)) := VGoodTail.tail_nn K O X hg (u' + 12) 3
  have et7 : (VGoodTail.tt K X (u' + 12) (u' + 16)) = (K.add (K.mul (co K X (u' + 12)) (co K X (u' + 15))) (K.neg (K.mul (co K X (u' + 11)) (co K X (u' + 16))))) := rfl
  have t8 : O.le K.zero (VGoodTail.tt K X (u' + 10) (u' + 20)) := VGoodTail.tail_nn K O X hg (u' + 10) 9
  have et8 : (VGoodTail.tt K X (u' + 10) (u' + 20)) = (K.add (K.mul (co K X (u' + 10)) (co K X (u' + 19))) (K.neg (K.mul (co K X (u' + 9)) (co K X (u' + 20))))) := rfl
  have t9 : O.le K.zero (VGoodTail.tt K X (u' + 10) (u' + 16)) := VGoodTail.tail_nn K O X hg (u' + 10) 5
  have et9 : (VGoodTail.tt K X (u' + 10) (u' + 16)) = (K.add (K.mul (co K X (u' + 10)) (co K X (u' + 15))) (K.neg (K.mul (co K X (u' + 9)) (co K X (u' + 16))))) := rfl
  have t10 : O.le K.zero (VGoodTail.tt K X (u' + 8) (u' + 20)) := VGoodTail.tail_nn K O X hg (u' + 8) 11
  have et10 : (VGoodTail.tt K X (u' + 8) (u' + 20)) = (K.add (K.mul (co K X (u' + 8)) (co K X (u' + 19))) (K.neg (K.mul (co K X (u' + 7)) (co K X (u' + 20))))) := rfl
  have t11 : O.le K.zero (VGoodTail.tt K X (u' + 8) (u' + 18)) := VGoodTail.tail_nn K O X hg (u' + 8) 9
  have et11 : (VGoodTail.tt K X (u' + 8) (u' + 18)) = (K.add (K.mul (co K X (u' + 8)) (co K X (u' + 17))) (K.neg (K.mul (co K X (u' + 7)) (co K X (u' + 18))))) := rfl
  have t12 : O.le K.zero (VGoodTail.tt K X (u' + 8) (u' + 16)) := VGoodTail.tail_nn K O X hg (u' + 8) 7
  have et12 : (VGoodTail.tt K X (u' + 8) (u' + 16)) = (K.add (K.mul (co K X (u' + 8)) (co K X (u' + 15))) (K.neg (K.mul (co K X (u' + 7)) (co K X (u' + 16))))) := rfl
  have t13 : O.le K.zero (VGoodTail.tt K X (u' + 13) (u' + 21)) := VGoodTail.tail_nn K O X hg (u' + 13) 7
  have et13 : (VGoodTail.tt K X (u' + 13) (u' + 21)) = (K.add (K.mul (co K X (u' + 13)) (co K X (u' + 20))) (K.neg (K.mul (co K X (u' + 12)) (co K X (u' + 21))))) := rfl
  have t14 : O.le K.zero (VGoodTail.tt K X (u' + 13) (u' + 19)) := VGoodTail.tail_nn K O X hg (u' + 13) 5
  have et14 : (VGoodTail.tt K X (u' + 13) (u' + 19)) = (K.add (K.mul (co K X (u' + 13)) (co K X (u' + 18))) (K.neg (K.mul (co K X (u' + 12)) (co K X (u' + 19))))) := rfl
  have t15 : O.le K.zero (VGoodTail.tt K X (u' + 13) (u' + 17)) := VGoodTail.tail_nn K O X hg (u' + 13) 3
  have et15 : (VGoodTail.tt K X (u' + 13) (u' + 17)) = (K.add (K.mul (co K X (u' + 13)) (co K X (u' + 16))) (K.neg (K.mul (co K X (u' + 12)) (co K X (u' + 17))))) := rfl
  have t16 : O.le K.zero (VGoodTail.tt K X (u' + 13) (u' + 15)) := VGoodTail.tail_nn K O X hg (u' + 13) 1
  have et16 : (VGoodTail.tt K X (u' + 13) (u' + 15)) = (K.add (K.mul (co K X (u' + 13)) (co K X (u' + 14))) (K.neg (K.mul (co K X (u' + 12)) (co K X (u' + 15))))) := rfl
  have t17 : O.le K.zero (VGoodTail.tt K X (u' + 11) (u' + 21)) := VGoodTail.tail_nn K O X hg (u' + 11) 9
  have et17 : (VGoodTail.tt K X (u' + 11) (u' + 21)) = (K.add (K.mul (co K X (u' + 11)) (co K X (u' + 20))) (K.neg (K.mul (co K X (u' + 10)) (co K X (u' + 21))))) := rfl
  have t18 : O.le K.zero (VGoodTail.tt K X (u' + 11) (u' + 15)) := VGoodTail.tail_nn K O X hg (u' + 11) 3
  have et18 : (VGoodTail.tt K X (u' + 11) (u' + 15)) = (K.add (K.mul (co K X (u' + 11)) (co K X (u' + 14))) (K.neg (K.mul (co K X (u' + 10)) (co K X (u' + 15))))) := rfl
  have t19 : O.le K.zero (VGoodTail.tt K X (u' + 9) (u' + 21)) := VGoodTail.tail_nn K O X hg (u' + 9) 11
  have et19 : (VGoodTail.tt K X (u' + 9) (u' + 21)) = (K.add (K.mul (co K X (u' + 9)) (co K X (u' + 20))) (K.neg (K.mul (co K X (u' + 8)) (co K X (u' + 21))))) := rfl
  have t20 : O.le K.zero (VGoodTail.tt K X (u' + 9) (u' + 15)) := VGoodTail.tail_nn K O X hg (u' + 9) 5
  have et20 : (VGoodTail.tt K X (u' + 9) (u' + 15)) = (K.add (K.mul (co K X (u' + 9)) (co K X (u' + 14))) (K.neg (K.mul (co K X (u' + 8)) (co K X (u' + 15))))) := rfl
  have t21 : O.le K.zero (VGoodTail.tt K X (u' + 7) (u' + 21)) := VGoodTail.tail_nn K O X hg (u' + 7) 13
  have et21 : (VGoodTail.tt K X (u' + 7) (u' + 21)) = (K.add (K.mul (co K X (u' + 7)) (co K X (u' + 20))) (K.neg (K.mul (co K X (u' + 6)) (co K X (u' + 21))))) := rfl
  have t22 : O.le K.zero (VGoodTail.tt K X (u' + 7) (u' + 19)) := VGoodTail.tail_nn K O X hg (u' + 7) 11
  have et22 : (VGoodTail.tt K X (u' + 7) (u' + 19)) = (K.add (K.mul (co K X (u' + 7)) (co K X (u' + 18))) (K.neg (K.mul (co K X (u' + 6)) (co K X (u' + 19))))) := rfl
  have t23 : O.le K.zero (VGoodTail.tt K X (u' + 7) (u' + 17)) := VGoodTail.tail_nn K O X hg (u' + 7) 9
  have et23 : (VGoodTail.tt K X (u' + 7) (u' + 17)) = (K.add (K.mul (co K X (u' + 7)) (co K X (u' + 16))) (K.neg (K.mul (co K X (u' + 6)) (co K X (u' + 17))))) := rfl
  have t24 : O.le K.zero (VGoodTail.tt K X (u' + 7) (u' + 15)) := VGoodTail.tail_nn K O X hg (u' + 7) 7
  have et24 : (VGoodTail.tt K X (u' + 7) (u' + 15)) = (K.add (K.mul (co K X (u' + 7)) (co K X (u' + 14))) (K.neg (K.mul (co K X (u' + 6)) (co K X (u' + 15))))) := rfl
  have t25 : O.le K.zero (VGoodTail.tt K X (u' + 14) (u' + 22)) := VGoodTail.tail_nn K O X hg (u' + 14) 7
  have et25 : (VGoodTail.tt K X (u' + 14) (u' + 22)) = (K.add (K.mul (co K X (u' + 14)) (co K X (u' + 21))) (K.neg (K.mul (co K X (u' + 13)) (co K X (u' + 22))))) := rfl
  have t26 : O.le K.zero (VGoodTail.tt K X (u' + 14) (u' + 20)) := VGoodTail.tail_nn K O X hg (u' + 14) 5
  have et26 : (VGoodTail.tt K X (u' + 14) (u' + 20)) = (K.add (K.mul (co K X (u' + 14)) (co K X (u' + 19))) (K.neg (K.mul (co K X (u' + 13)) (co K X (u' + 20))))) := rfl
  have t27 : O.le K.zero (VGoodTail.tt K X (u' + 14) (u' + 18)) := VGoodTail.tail_nn K O X hg (u' + 14) 3
  have et27 : (VGoodTail.tt K X (u' + 14) (u' + 18)) = (K.add (K.mul (co K X (u' + 14)) (co K X (u' + 17))) (K.neg (K.mul (co K X (u' + 13)) (co K X (u' + 18))))) := rfl
  have t28 : O.le K.zero (VGoodTail.tt K X (u' + 14) (u' + 16)) := VGoodTail.tail_nn K O X hg (u' + 14) 1
  have et28 : (VGoodTail.tt K X (u' + 14) (u' + 16)) = (K.add (K.mul (co K X (u' + 14)) (co K X (u' + 15))) (K.neg (K.mul (co K X (u' + 13)) (co K X (u' + 16))))) := rfl
  have t29 : O.le K.zero (VGoodTail.tt K X (u' + 12) (u' + 22)) := VGoodTail.tail_nn K O X hg (u' + 12) 9
  have et29 : (VGoodTail.tt K X (u' + 12) (u' + 22)) = (K.add (K.mul (co K X (u' + 12)) (co K X (u' + 21))) (K.neg (K.mul (co K X (u' + 11)) (co K X (u' + 22))))) := rfl
  have t30 : O.le K.zero (VGoodTail.tt K X (u' + 12) (u' + 14)) := VGoodTail.tail_nn K O X hg (u' + 12) 1
  have et30 : (VGoodTail.tt K X (u' + 12) (u' + 14)) = (K.add (K.mul (co K X (u' + 12)) (co K X (u' + 13))) (K.neg (K.mul (co K X (u' + 11)) (co K X (u' + 14))))) := rfl
  have t31 : O.le K.zero (VGoodTail.tt K X (u' + 10) (u' + 22)) := VGoodTail.tail_nn K O X hg (u' + 10) 11
  have et31 : (VGoodTail.tt K X (u' + 10) (u' + 22)) = (K.add (K.mul (co K X (u' + 10)) (co K X (u' + 21))) (K.neg (K.mul (co K X (u' + 9)) (co K X (u' + 22))))) := rfl
  have t32 : O.le K.zero (VGoodTail.tt K X (u' + 10) (u' + 14)) := VGoodTail.tail_nn K O X hg (u' + 10) 3
  have et32 : (VGoodTail.tt K X (u' + 10) (u' + 14)) = (K.add (K.mul (co K X (u' + 10)) (co K X (u' + 13))) (K.neg (K.mul (co K X (u' + 9)) (co K X (u' + 14))))) := rfl
  have t33 : O.le K.zero (VGoodTail.tt K X (u' + 8) (u' + 22)) := VGoodTail.tail_nn K O X hg (u' + 8) 13
  have et33 : (VGoodTail.tt K X (u' + 8) (u' + 22)) = (K.add (K.mul (co K X (u' + 8)) (co K X (u' + 21))) (K.neg (K.mul (co K X (u' + 7)) (co K X (u' + 22))))) := rfl
  have t34 : O.le K.zero (VGoodTail.tt K X (u' + 8) (u' + 14)) := VGoodTail.tail_nn K O X hg (u' + 8) 5
  have et34 : (VGoodTail.tt K X (u' + 8) (u' + 14)) = (K.add (K.mul (co K X (u' + 8)) (co K X (u' + 13))) (K.neg (K.mul (co K X (u' + 7)) (co K X (u' + 14))))) := rfl
  have t35 : O.le K.zero (VGoodTail.tt K X (u' + 6) (u' + 22)) := VGoodTail.tail_nn K O X hg (u' + 6) 15
  have et35 : (VGoodTail.tt K X (u' + 6) (u' + 22)) = (K.add (K.mul (co K X (u' + 6)) (co K X (u' + 21))) (K.neg (K.mul (co K X (u' + 5)) (co K X (u' + 22))))) := rfl
  have t36 : O.le K.zero (VGoodTail.tt K X (u' + 6) (u' + 20)) := VGoodTail.tail_nn K O X hg (u' + 6) 13
  have et36 : (VGoodTail.tt K X (u' + 6) (u' + 20)) = (K.add (K.mul (co K X (u' + 6)) (co K X (u' + 19))) (K.neg (K.mul (co K X (u' + 5)) (co K X (u' + 20))))) := rfl
  have t37 : O.le K.zero (VGoodTail.tt K X (u' + 6) (u' + 18)) := VGoodTail.tail_nn K O X hg (u' + 6) 11
  have et37 : (VGoodTail.tt K X (u' + 6) (u' + 18)) = (K.add (K.mul (co K X (u' + 6)) (co K X (u' + 17))) (K.neg (K.mul (co K X (u' + 5)) (co K X (u' + 18))))) := rfl
  have t38 : O.le K.zero (VGoodTail.tt K X (u' + 6) (u' + 16)) := VGoodTail.tail_nn K O X hg (u' + 6) 9
  have et38 : (VGoodTail.tt K X (u' + 6) (u' + 16)) = (K.add (K.mul (co K X (u' + 6)) (co K X (u' + 15))) (K.neg (K.mul (co K X (u' + 5)) (co K X (u' + 16))))) := rfl
  have t39 : O.le K.zero (VGoodTail.tt K X (u' + 6) (u' + 14)) := VGoodTail.tail_nn K O X hg (u' + 6) 7
  have et39 : (VGoodTail.tt K X (u' + 6) (u' + 14)) = (K.add (K.mul (co K X (u' + 6)) (co K X (u' + 13))) (K.neg (K.mul (co K X (u' + 5)) (co K X (u' + 14))))) := rfl
  have hp : 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.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.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (VGoo  -- (+2136 chars; full line in the Zenodo source)
    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 _ _ (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 _ _ (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   -- (+1023 chars; full line in the Zenodo source)
  refine O.le_trans _ _ _ hp (TripleE.le_of_eq' K O _ _ ?_)
  rw [kv, et0, et1, et2, et3, et4, et5, et6, et7, et8, et9, et10, et11, et12, et13, et14, et15, et16, et17, et18, et19, et20, et21, et22, et23, et24, et25, et26, et27, et28, et29, et30, et31, et32, et33, et34, et35, et36, et37, et38, et39]
  ring_z

theorem kna_s_8 (X : List R) (hg : L1Full.Good K O X) (u' : Nat) :
    O.le K.zero (CBLib.kern K (Fa K X) (Ga K X) 5 (u' + 4) (u' + 13)) := by
  have kv : CBLib.kern K (Fa K X) (Ga K X) 5 (u' + 4) (u' + 13) = (K.add (K.add (K.add (K.add (K.add K.zero (K.add (K.mul (co K X (u' + 10)) (co K X (u' + 18))) (K.neg (K.mul (co K X (u' + 19)) (co K X (u' + 9)))))) (K.add (K.mul (K.add (co K X (u' + 11)) (co K X (u' + 9))) (K.add (co K X (u' + 19)) (co K X (u' + 17)))) (K.neg (K.mul (K.add (co K X (u' + 20)) (co K X (u' + 18))) (K.add (co K X (u'  -- (+1324 chars; full line in the Zenodo source)
  have t0 : O.le K.zero (VGoodTail.tt K X (u' + 10) (u' + 19)) := VGoodTail.tail_nn K O X hg (u' + 10) 8
  have et0 : (VGoodTail.tt K X (u' + 10) (u' + 19)) = (K.add (K.mul (co K X (u' + 10)) (co K X (u' + 18))) (K.neg (K.mul (co K X (u' + 9)) (co K X (u' + 19))))) := rfl
  have t1 : O.le K.zero (VGoodTail.tt K X (u' + 11) (u' + 20)) := VGoodTail.tail_nn K O X hg (u' + 11) 8
  have et1 : (VGoodTail.tt K X (u' + 11) (u' + 20)) = (K.add (K.mul (co K X (u' + 11)) (co K X (u' + 19))) (K.neg (K.mul (co K X (u' + 10)) (co K X (u' + 20))))) := rfl
  have t2 : O.le K.zero (VGoodTail.tt K X (u' + 11) (u' + 18)) := VGoodTail.tail_nn K O X hg (u' + 11) 6
  have et2 : (VGoodTail.tt K X (u' + 11) (u' + 18)) = (K.add (K.mul (co K X (u' + 11)) (co K X (u' + 17))) (K.neg (K.mul (co K X (u' + 10)) (co K X (u' + 18))))) := rfl
  have t3 : O.le K.zero (VGoodTail.tt K X (u' + 9) (u' + 20)) := VGoodTail.tail_nn K O X hg (u' + 9) 10
  have et3 : (VGoodTail.tt K X (u' + 9) (u' + 20)) = (K.add (K.mul (co K X (u' + 9)) (co K X (u' + 19))) (K.neg (K.mul (co K X (u' + 8)) (co K X (u' + 20))))) := rfl
  have t4 : O.le K.zero (VGoodTail.tt K X (u' + 9) (u' + 18)) := VGoodTail.tail_nn K O X hg (u' + 9) 8
  have et4 : (VGoodTail.tt K X (u' + 9) (u' + 18)) = (K.add (K.mul (co K X (u' + 9)) (co K X (u' + 17))) (K.neg (K.mul (co K X (u' + 8)) (co K X (u' + 18))))) := rfl
  have t5 : O.le K.zero (VGoodTail.tt K X (u' + 12) (u' + 21)) := VGoodTail.tail_nn K O X hg (u' + 12) 8
  have et5 : (VGoodTail.tt K X (u' + 12) (u' + 21)) = (K.add (K.mul (co K X (u' + 12)) (co K X (u' + 20))) (K.neg (K.mul (co K X (u' + 11)) (co K X (u' + 21))))) := rfl
  have t6 : O.le K.zero (VGoodTail.tt K X (u' + 12) (u' + 19)) := VGoodTail.tail_nn K O X hg (u' + 12) 6
  have et6 : (VGoodTail.tt K X (u' + 12) (u' + 19)) = (K.add (K.mul (co K X (u' + 12)) (co K X (u' + 18))) (K.neg (K.mul (co K X (u' + 11)) (co K X (u' + 19))))) := rfl
  have t7 : O.le K.zero (VGoodTail.tt K X (u' + 12) (u' + 17)) := VGoodTail.tail_nn K O X hg (u' + 12) 4
  have et7 : (VGoodTail.tt K X (u' + 12) (u' + 17)) = (K.add (K.mul (co K X (u' + 12)) (co K X (u' + 16))) (K.neg (K.mul (co K X (u' + 11)) (co K X (u' + 17))))) := rfl
  have t8 : O.le K.zero (VGoodTail.tt K X (u' + 10) (u' + 21)) := VGoodTail.tail_nn K O X hg (u' + 10) 10
  have et8 : (VGoodTail.tt K X (u' + 10) (u' + 21)) = (K.add (K.mul (co K X (u' + 10)) (co K X (u' + 20))) (K.neg (K.mul (co K X (u' + 9)) (co K X (u' + 21))))) := rfl
  have t9 : O.le K.zero (VGoodTail.tt K X (u' + 10) (u' + 17)) := VGoodTail.tail_nn K O X hg (u' + 10) 6
  have et9 : (VGoodTail.tt K X (u' + 10) (u' + 17)) = (K.add (K.mul (co K X (u' + 10)) (co K X (u' + 16))) (K.neg (K.mul (co K X (u' + 9)) (co K X (u' + 17))))) := rfl
  have t10 : O.le K.zero (VGoodTail.tt K X (u' + 8) (u' + 21)) := VGoodTail.tail_nn K O X hg (u' + 8) 12
  have et10 : (VGoodTail.tt K X (u' + 8) (u' + 21)) = (K.add (K.mul (co K X (u' + 8)) (co K X (u' + 20))) (K.neg (K.mul (co K X (u' + 7)) (co K X (u' + 21))))) := rfl
  have t11 : O.le K.zero (VGoodTail.tt K X (u' + 8) (u' + 19)) := VGoodTail.tail_nn K O X hg (u' + 8) 10
  have et11 : (VGoodTail.tt K X (u' + 8) (u' + 19)) = (K.add (K.mul (co K X (u' + 8)) (co K X (u' + 18))) (K.neg (K.mul (co K X (u' + 7)) (co K X (u' + 19))))) := rfl
  have t12 : O.le K.zero (VGoodTail.tt K X (u' + 8) (u' + 17)) := VGoodTail.tail_nn K O X hg (u' + 8) 8
  have et12 : (VGoodTail.tt K X (u' + 8) (u' + 17)) = (K.add (K.mul (co K X (u' + 8)) (co K X (u' + 16))) (K.neg (K.mul (co K X (u' + 7)) (co K X (u' + 17))))) := rfl
  have t13 : O.le K.zero (VGoodTail.tt K X (u' + 13) (u' + 22)) := VGoodTail.tail_nn K O X hg (u' + 13) 8
  have et13 : (VGoodTail.tt K X (u' + 13) (u' + 22)) = (K.add (K.mul (co K X (u' + 13)) (co K X (u' + 21))) (K.neg (K.mul (co K X (u' + 12)) (co K X (u' + 22))))) := rfl
  have t14 : O.le K.zero (VGoodTail.tt K X (u' + 13) (u' + 20)) := VGoodTail.tail_nn K O X hg (u' + 13) 6
  have et14 : (VGoodTail.tt K X (u' + 13) (u' + 20)) = (K.add (K.mul (co K X (u' + 13)) (co K X (u' + 19))) (K.neg (K.mul (co K X (u' + 12)) (co K X (u' + 20))))) := rfl
  have t15 : O.le K.zero (VGoodTail.tt K X (u' + 13) (u' + 18)) := VGoodTail.tail_nn K O X hg (u' + 13) 4
  have et15 : (VGoodTail.tt K X (u' + 13) (u' + 18)) = (K.add (K.mul (co K X (u' + 13)) (co K X (u' + 17))) (K.neg (K.mul (co K X (u' + 12)) (co K X (u' + 18))))) := rfl
  have t16 : O.le K.zero (VGoodTail.tt K X (u' + 13) (u' + 16)) := VGoodTail.tail_nn K O X hg (u' + 13) 2
  have et16 : (VGoodTail.tt K X (u' + 13) (u' + 16)) = (K.add (K.mul (co K X (u' + 13)) (co K X (u' + 15))) (K.neg (K.mul (co K X (u' + 12)) (co K X (u' + 16))))) := rfl
  have t17 : O.le K.zero (VGoodTail.tt K X (u' + 11) (u' + 22)) := VGoodTail.tail_nn K O X hg (u' + 11) 10
  have et17 : (VGoodTail.tt K X (u' + 11) (u' + 22)) = (K.add (K.mul (co K X (u' + 11)) (co K X (u' + 21))) (K.neg (K.mul (co K X (u' + 10)) (co K X (u' + 22))))) := rfl
  have t18 : O.le K.zero (VGoodTail.tt K X (u' + 11) (u' + 16)) := VGoodTail.tail_nn K O X hg (u' + 11) 4
  have et18 : (VGoodTail.tt K X (u' + 11) (u' + 16)) = (K.add (K.mul (co K X (u' + 11)) (co K X (u' + 15))) (K.neg (K.mul (co K X (u' + 10)) (co K X (u' + 16))))) := rfl
  have t19 : O.le K.zero (VGoodTail.tt K X (u' + 9) (u' + 22)) := VGoodTail.tail_nn K O X hg (u' + 9) 12
  have et19 : (VGoodTail.tt K X (u' + 9) (u' + 22)) = (K.add (K.mul (co K X (u' + 9)) (co K X (u' + 21))) (K.neg (K.mul (co K X (u' + 8)) (co K X (u' + 22))))) := rfl
  have t20 : O.le K.zero (VGoodTail.tt K X (u' + 9) (u' + 16)) := VGoodTail.tail_nn K O X hg (u' + 9) 6
  have et20 : (VGoodTail.tt K X (u' + 9) (u' + 16)) = (K.add (K.mul (co K X (u' + 9)) (co K X (u' + 15))) (K.neg (K.mul (co K X (u' + 8)) (co K X (u' + 16))))) := rfl
  have t21 : O.le K.zero (VGoodTail.tt K X (u' + 7) (u' + 22)) := VGoodTail.tail_nn K O X hg (u' + 7) 14
  have et21 : (VGoodTail.tt K X (u' + 7) (u' + 22)) = (K.add (K.mul (co K X (u' + 7)) (co K X (u' + 21))) (K.neg (K.mul (co K X (u' + 6)) (co K X (u' + 22))))) := rfl
  have t22 : O.le K.zero (VGoodTail.tt K X (u' + 7) (u' + 20)) := VGoodTail.tail_nn K O X hg (u' + 7) 12
  have et22 : (VGoodTail.tt K X (u' + 7) (u' + 20)) = (K.add (K.mul (co K X (u' + 7)) (co K X (u' + 19))) (K.neg (K.mul (co K X (u' + 6)) (co K X (u' + 20))))) := rfl
  have t23 : O.le K.zero (VGoodTail.tt K X (u' + 7) (u' + 18)) := VGoodTail.tail_nn K O X hg (u' + 7) 10
  have et23 : (VGoodTail.tt K X (u' + 7) (u' + 18)) = (K.add (K.mul (co K X (u' + 7)) (co K X (u' + 17))) (K.neg (K.mul (co K X (u' + 6)) (co K X (u' + 18))))) := rfl
  have t24 : O.le K.zero (VGoodTail.tt K X (u' + 7) (u' + 16)) := VGoodTail.tail_nn K O X hg (u' + 7) 8
  have et24 : (VGoodTail.tt K X (u' + 7) (u' + 16)) = (K.add (K.mul (co K X (u' + 7)) (co K X (u' + 15))) (K.neg (K.mul (co K X (u' + 6)) (co K X (u' + 16))))) := rfl
  have t25 : O.le K.zero (VGoodTail.tt K X (u' + 14) (u' + 23)) := VGoodTail.tail_nn K O X hg (u' + 14) 8
  have et25 : (VGoodTail.tt K X (u' + 14) (u' + 23)) = (K.add (K.mul (co K X (u' + 14)) (co K X (u' + 22))) (K.neg (K.mul (co K X (u' + 13)) (co K X (u' + 23))))) := rfl
  have t26 : O.le K.zero (VGoodTail.tt K X (u' + 14) (u' + 21)) := VGoodTail.tail_nn K O X hg (u' + 14) 6
  have et26 : (VGoodTail.tt K X (u' + 14) (u' + 21)) = (K.add (K.mul (co K X (u' + 14)) (co K X (u' + 20))) (K.neg (K.mul (co K X (u' + 13)) (co K X (u' + 21))))) := rfl
  have t27 : O.le K.zero (VGoodTail.tt K X (u' + 14) (u' + 19)) := VGoodTail.tail_nn K O X hg (u' + 14) 4
  have et27 : (VGoodTail.tt K X (u' + 14) (u' + 19)) = (K.add (K.mul (co K X (u' + 14)) (co K X (u' + 18))) (K.neg (K.mul (co K X (u' + 13)) (co K X (u' + 19))))) := rfl
  have t28 : O.le K.zero (VGoodTail.tt K X (u' + 14) (u' + 17)) := VGoodTail.tail_nn K O X hg (u' + 14) 2
  have et28 : (VGoodTail.tt K X (u' + 14) (u' + 17)) = (K.add (K.mul (co K X (u' + 14)) (co K X (u' + 16))) (K.neg (K.mul (co K X (u' + 13)) (co K X (u' + 17))))) := rfl
  have t29 : O.le K.zero (VGoodTail.tt K X (u' + 14) (u' + 15)) := VGoodTail.tail_nn K O X hg (u' + 14) 0
  have et29 : (VGoodTail.tt K X (u' + 14) (u' + 15)) = (K.add (K.mul (co K X (u' + 14)) (co K X (u' + 14))) (K.neg (K.mul (co K X (u' + 13)) (co K X (u' + 15))))) := rfl
  have t30 : O.le K.zero (VGoodTail.tt K X (u' + 12) (u' + 23)) := VGoodTail.tail_nn K O X hg (u' + 12) 10
  have et30 : (VGoodTail.tt K X (u' + 12) (u' + 23)) = (K.add (K.mul (co K X (u' + 12)) (co K X (u' + 22))) (K.neg (K.mul (co K X (u' + 11)) (co K X (u' + 23))))) := rfl
  have t31 : O.le K.zero (VGoodTail.tt K X (u' + 12) (u' + 15)) := VGoodTail.tail_nn K O X hg (u' + 12) 2
  have et31 : (VGoodTail.tt K X (u' + 12) (u' + 15)) = (K.add (K.mul (co K X (u' + 12)) (co K X (u' + 14))) (K.neg (K.mul (co K X (u' + 11)) (co K X (u' + 15))))) := rfl
  have t32 : O.le K.zero (VGoodTail.tt K X (u' + 10) (u' + 23)) := VGoodTail.tail_nn K O X hg (u' + 10) 12
  have et32 : (VGoodTail.tt K X (u' + 10) (u' + 23)) = (K.add (K.mul (co K X (u' + 10)) (co K X (u' + 22))) (K.neg (K.mul (co K X (u' + 9)) (co K X (u' + 23))))) := rfl
  have t33 : O.le K.zero (VGoodTail.tt K X (u' + 10) (u' + 15)) := VGoodTail.tail_nn K O X hg (u' + 10) 4
  have et33 : (VGoodTail.tt K X (u' + 10) (u' + 15)) = (K.add (K.mul (co K X (u' + 10)) (co K X (u' + 14))) (K.neg (K.mul (co K X (u' + 9)) (co K X (u' + 15))))) := rfl
  have t34 : O.le K.zero (VGoodTail.tt K X (u' + 8) (u' + 23)) := VGoodTail.tail_nn K O X hg (u' + 8) 14
  have et34 : (VGoodTail.tt K X (u' + 8) (u' + 23)) = (K.add (K.mul (co K X (u' + 8)) (co K X (u' + 22))) (K.neg (K.mul (co K X (u' + 7)) (co K X (u' + 23))))) := rfl
  have t35 : O.le K.zero (VGoodTail.tt K X (u' + 8) (u' + 15)) := VGoodTail.tail_nn K O X hg (u' + 8) 6
  have et35 : (VGoodTail.tt K X (u' + 8) (u' + 15)) = (K.add (K.mul (co K X (u' + 8)) (co K X (u' + 14))) (K.neg (K.mul (co K X (u' + 7)) (co K X (u' + 15))))) := rfl
  have t36 : O.le K.zero (VGoodTail.tt K X (u' + 6) (u' + 23)) := VGoodTail.tail_nn K O X hg (u' + 6) 16
  have et36 : (VGoodTail.tt K X (u' + 6) (u' + 23)) = (K.add (K.mul (co K X (u' + 6)) (co K X (u' + 22))) (K.neg (K.mul (co K X (u' + 5)) (co K X (u' + 23))))) := rfl
  have t37 : O.le K.zero (VGoodTail.tt K X (u' + 6) (u' + 21)) := VGoodTail.tail_nn K O X hg (u' + 6) 14
  have et37 : (VGoodTail.tt K X (u' + 6) (u' + 21)) = (K.add (K.mul (co K X (u' + 6)) (co K X (u' + 20))) (K.neg (K.mul (co K X (u' + 5)) (co K X (u' + 21))))) := rfl
  have t38 : O.le K.zero (VGoodTail.tt K X (u' + 6) (u' + 19)) := VGoodTail.tail_nn K O X hg (u' + 6) 12
  have et38 : (VGoodTail.tt K X (u' + 6) (u' + 19)) = (K.add (K.mul (co K X (u' + 6)) (co K X (u' + 18))) (K.neg (K.mul (co K X (u' + 5)) (co K X (u' + 19))))) := rfl
  have t39 : O.le K.zero (VGoodTail.tt K X (u' + 6) (u' + 17)) := VGoodTail.tail_nn K O X hg (u' + 6) 10
  have et39 : (VGoodTail.tt K X (u' + 6) (u' + 17)) = (K.add (K.mul (co K X (u' + 6)) (co K X (u' + 16))) (K.neg (K.mul (co K X (u' + 5)) (co K X (u' + 17))))) := rfl
  have t40 : O.le K.zero (VGoodTail.tt K X (u' + 6) (u' + 15)) := VGoodTail.tail_nn K O X hg (u' + 6) 8
  have et40 : (VGoodTail.tt K X (u' + 6) (u' + 15)) = (K.add (K.mul (co K X (u' + 6)) (co K X (u' + 14))) (K.neg (K.mul (co K X (u' + 5)) (co K X (u' + 15))))) := rfl
  have hp : 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.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.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.ad  -- (+2183 chars; full line in the Zenodo source)
    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 _ _ (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 _ _ (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   -- (+1050 chars; full line in the Zenodo source)
  refine O.le_trans _ _ _ hp (TripleE.le_of_eq' K O _ _ ?_)
  rw [kv, et0, et1, et2, et3, et4, et5, et6, et7, et8, et9, et10, et11, et12, et13, et14, et15, et16, et17, et18, et19, et20, et21, et22, et23, et24, et25, et26, et27, et28, et29, et30, et31, et32, et33, et34, et35, et36, et37, et38, et39, et40]
  ring_z

theorem kna_s_9 (X : List R) (hg : L1Full.Good K O X) (u' : Nat) :
    O.le K.zero (CBLib.kern K (Fa K X) (Ga K X) 5 (u' + 4) (u' + 14)) := by
  have kv : CBLib.kern K (Fa K X) (Ga K X) 5 (u' + 4) (u' + 14) = (K.add (K.add (K.add (K.add (K.add K.zero (K.add (K.mul (co K X (u' + 10)) (co K X (u' + 19))) (K.neg (K.mul (co K X (u' + 20)) (co K X (u' + 9)))))) (K.add (K.mul (K.add (co K X (u' + 11)) (co K X (u' + 9))) (K.add (co K X (u' + 20)) (co K X (u' + 18)))) (K.neg (K.mul (K.add (co K X (u' + 21)) (co K X (u' + 19))) (K.add (co K X (u'  -- (+1324 chars; full line in the Zenodo source)
  have t0 : O.le K.zero (VGoodTail.tt K X (u' + 10) (u' + 20)) := VGoodTail.tail_nn K O X hg (u' + 10) 9
  have et0 : (VGoodTail.tt K X (u' + 10) (u' + 20)) = (K.add (K.mul (co K X (u' + 10)) (co K X (u' + 19))) (K.neg (K.mul (co K X (u' + 9)) (co K X (u' + 20))))) := rfl
  have t1 : O.le K.zero (VGoodTail.tt K X (u' + 11) (u' + 21)) := VGoodTail.tail_nn K O X hg (u' + 11) 9
  have et1 : (VGoodTail.tt K X (u' + 11) (u' + 21)) = (K.add (K.mul (co K X (u' + 11)) (co K X (u' + 20))) (K.neg (K.mul (co K X (u' + 10)) (co K X (u' + 21))))) := rfl
  have t2 : O.le K.zero (VGoodTail.tt K X (u' + 11) (u' + 19)) := VGoodTail.tail_nn K O X hg (u' + 11) 7
  have et2 : (VGoodTail.tt K X (u' + 11) (u' + 19)) = (K.add (K.mul (co K X (u' + 11)) (co K X (u' + 18))) (K.neg (K.mul (co K X (u' + 10)) (co K X (u' + 19))))) := rfl
  have t3 : O.le K.zero (VGoodTail.tt K X (u' + 9) (u' + 21)) := VGoodTail.tail_nn K O X hg (u' + 9) 11
  have et3 : (VGoodTail.tt K X (u' + 9) (u' + 21)) = (K.add (K.mul (co K X (u' + 9)) (co K X (u' + 20))) (K.neg (K.mul (co K X (u' + 8)) (co K X (u' + 21))))) := rfl
  have t4 : O.le K.zero (VGoodTail.tt K X (u' + 9) (u' + 19)) := VGoodTail.tail_nn K O X hg (u' + 9) 9
  have et4 : (VGoodTail.tt K X (u' + 9) (u' + 19)) = (K.add (K.mul (co K X (u' + 9)) (co K X (u' + 18))) (K.neg (K.mul (co K X (u' + 8)) (co K X (u' + 19))))) := rfl
  have t5 : O.le K.zero (VGoodTail.tt K X (u' + 12) (u' + 22)) := VGoodTail.tail_nn K O X hg (u' + 12) 9
  have et5 : (VGoodTail.tt K X (u' + 12) (u' + 22)) = (K.add (K.mul (co K X (u' + 12)) (co K X (u' + 21))) (K.neg (K.mul (co K X (u' + 11)) (co K X (u' + 22))))) := rfl
  have t6 : O.le K.zero (VGoodTail.tt K X (u' + 12) (u' + 20)) := VGoodTail.tail_nn K O X hg (u' + 12) 7
  have et6 : (VGoodTail.tt K X (u' + 12) (u' + 20)) = (K.add (K.mul (co K X (u' + 12)) (co K X (u' + 19))) (K.neg (K.mul (co K X (u' + 11)) (co K X (u' + 20))))) := rfl
  have t7 : O.le K.zero (VGoodTail.tt K X (u' + 12) (u' + 18)) := VGoodTail.tail_nn K O X hg (u' + 12) 5
  have et7 : (VGoodTail.tt K X (u' + 12) (u' + 18)) = (K.add (K.mul (co K X (u' + 12)) (co K X (u' + 17))) (K.neg (K.mul (co K X (u' + 11)) (co K X (u' + 18))))) := rfl
  have t8 : O.le K.zero (VGoodTail.tt K X (u' + 10) (u' + 22)) := VGoodTail.tail_nn K O X hg (u' + 10) 11
  have et8 : (VGoodTail.tt K X (u' + 10) (u' + 22)) = (K.add (K.mul (co K X (u' + 10)) (co K X (u' + 21))) (K.neg (K.mul (co K X (u' + 9)) (co K X (u' + 22))))) := rfl
  have t9 : O.le K.zero (VGoodTail.tt K X (u' + 10) (u' + 18)) := VGoodTail.tail_nn K O X hg (u' + 10) 7
  have et9 : (VGoodTail.tt K X (u' + 10) (u' + 18)) = (K.add (K.mul (co K X (u' + 10)) (co K X (u' + 17))) (K.neg (K.mul (co K X (u' + 9)) (co K X (u' + 18))))) := rfl
  have t10 : O.le K.zero (VGoodTail.tt K X (u' + 8) (u' + 22)) := VGoodTail.tail_nn K O X hg (u' + 8) 13
  have et10 : (VGoodTail.tt K X (u' + 8) (u' + 22)) = (K.add (K.mul (co K X (u' + 8)) (co K X (u' + 21))) (K.neg (K.mul (co K X (u' + 7)) (co K X (u' + 22))))) := rfl
  have t11 : O.le K.zero (VGoodTail.tt K X (u' + 8) (u' + 20)) := VGoodTail.tail_nn K O X hg (u' + 8) 11
  have et11 : (VGoodTail.tt K X (u' + 8) (u' + 20)) = (K.add (K.mul (co K X (u' + 8)) (co K X (u' + 19))) (K.neg (K.mul (co K X (u' + 7)) (co K X (u' + 20))))) := rfl
  have t12 : O.le K.zero (VGoodTail.tt K X (u' + 8) (u' + 18)) := VGoodTail.tail_nn K O X hg (u' + 8) 9
  have et12 : (VGoodTail.tt K X (u' + 8) (u' + 18)) = (K.add (K.mul (co K X (u' + 8)) (co K X (u' + 17))) (K.neg (K.mul (co K X (u' + 7)) (co K X (u' + 18))))) := rfl
  have t13 : O.le K.zero (VGoodTail.tt K X (u' + 13) (u' + 23)) := VGoodTail.tail_nn K O X hg (u' + 13) 9
  have et13 : (VGoodTail.tt K X (u' + 13) (u' + 23)) = (K.add (K.mul (co K X (u' + 13)) (co K X (u' + 22))) (K.neg (K.mul (co K X (u' + 12)) (co K X (u' + 23))))) := rfl
  have t14 : O.le K.zero (VGoodTail.tt K X (u' + 13) (u' + 21)) := VGoodTail.tail_nn K O X hg (u' + 13) 7
  have et14 : (VGoodTail.tt K X (u' + 13) (u' + 21)) = (K.add (K.mul (co K X (u' + 13)) (co K X (u' + 20))) (K.neg (K.mul (co K X (u' + 12)) (co K X (u' + 21))))) := rfl
  have t15 : O.le K.zero (VGoodTail.tt K X (u' + 13) (u' + 19)) := VGoodTail.tail_nn K O X hg (u' + 13) 5
  have et15 : (VGoodTail.tt K X (u' + 13) (u' + 19)) = (K.add (K.mul (co K X (u' + 13)) (co K X (u' + 18))) (K.neg (K.mul (co K X (u' + 12)) (co K X (u' + 19))))) := rfl
  have t16 : O.le K.zero (VGoodTail.tt K X (u' + 13) (u' + 17)) := VGoodTail.tail_nn K O X hg (u' + 13) 3
  have et16 : (VGoodTail.tt K X (u' + 13) (u' + 17)) = (K.add (K.mul (co K X (u' + 13)) (co K X (u' + 16))) (K.neg (K.mul (co K X (u' + 12)) (co K X (u' + 17))))) := rfl
  have t17 : O.le K.zero (VGoodTail.tt K X (u' + 11) (u' + 23)) := VGoodTail.tail_nn K O X hg (u' + 11) 11
  have et17 : (VGoodTail.tt K X (u' + 11) (u' + 23)) = (K.add (K.mul (co K X (u' + 11)) (co K X (u' + 22))) (K.neg (K.mul (co K X (u' + 10)) (co K X (u' + 23))))) := rfl
  have t18 : O.le K.zero (VGoodTail.tt K X (u' + 11) (u' + 17)) := VGoodTail.tail_nn K O X hg (u' + 11) 5
  have et18 : (VGoodTail.tt K X (u' + 11) (u' + 17)) = (K.add (K.mul (co K X (u' + 11)) (co K X (u' + 16))) (K.neg (K.mul (co K X (u' + 10)) (co K X (u' + 17))))) := rfl
  have t19 : O.le K.zero (VGoodTail.tt K X (u' + 9) (u' + 23)) := VGoodTail.tail_nn K O X hg (u' + 9) 13
  have et19 : (VGoodTail.tt K X (u' + 9) (u' + 23)) = (K.add (K.mul (co K X (u' + 9)) (co K X (u' + 22))) (K.neg (K.mul (co K X (u' + 8)) (co K X (u' + 23))))) := rfl
  have t20 : O.le K.zero (VGoodTail.tt K X (u' + 9) (u' + 17)) := VGoodTail.tail_nn K O X hg (u' + 9) 7
  have et20 : (VGoodTail.tt K X (u' + 9) (u' + 17)) = (K.add (K.mul (co K X (u' + 9)) (co K X (u' + 16))) (K.neg (K.mul (co K X (u' + 8)) (co K X (u' + 17))))) := rfl
  have t21 : O.le K.zero (VGoodTail.tt K X (u' + 7) (u' + 23)) := VGoodTail.tail_nn K O X hg (u' + 7) 15
  have et21 : (VGoodTail.tt K X (u' + 7) (u' + 23)) = (K.add (K.mul (co K X (u' + 7)) (co K X (u' + 22))) (K.neg (K.mul (co K X (u' + 6)) (co K X (u' + 23))))) := rfl
  have t22 : O.le K.zero (VGoodTail.tt K X (u' + 7) (u' + 21)) := VGoodTail.tail_nn K O X hg (u' + 7) 13
  have et22 : (VGoodTail.tt K X (u' + 7) (u' + 21)) = (K.add (K.mul (co K X (u' + 7)) (co K X (u' + 20))) (K.neg (K.mul (co K X (u' + 6)) (co K X (u' + 21))))) := rfl
  have t23 : O.le K.zero (VGoodTail.tt K X (u' + 7) (u' + 19)) := VGoodTail.tail_nn K O X hg (u' + 7) 11
  have et23 : (VGoodTail.tt K X (u' + 7) (u' + 19)) = (K.add (K.mul (co K X (u' + 7)) (co K X (u' + 18))) (K.neg (K.mul (co K X (u' + 6)) (co K X (u' + 19))))) := rfl
  have t24 : O.le K.zero (VGoodTail.tt K X (u' + 7) (u' + 17)) := VGoodTail.tail_nn K O X hg (u' + 7) 9
  have et24 : (VGoodTail.tt K X (u' + 7) (u' + 17)) = (K.add (K.mul (co K X (u' + 7)) (co K X (u' + 16))) (K.neg (K.mul (co K X (u' + 6)) (co K X (u' + 17))))) := rfl
  have t25 : O.le K.zero (VGoodTail.tt K X (u' + 14) (u' + 24)) := VGoodTail.tail_nn K O X hg (u' + 14) 9
  have et25 : (VGoodTail.tt K X (u' + 14) (u' + 24)) = (K.add (K.mul (co K X (u' + 14)) (co K X (u' + 23))) (K.neg (K.mul (co K X (u' + 13)) (co K X (u' + 24))))) := rfl
  have t26 : O.le K.zero (VGoodTail.tt K X (u' + 14) (u' + 22)) := VGoodTail.tail_nn K O X hg (u' + 14) 7
  have et26 : (VGoodTail.tt K X (u' + 14) (u' + 22)) = (K.add (K.mul (co K X (u' + 14)) (co K X (u' + 21))) (K.neg (K.mul (co K X (u' + 13)) (co K X (u' + 22))))) := rfl
  have t27 : O.le K.zero (VGoodTail.tt K X (u' + 14) (u' + 20)) := VGoodTail.tail_nn K O X hg (u' + 14) 5
  have et27 : (VGoodTail.tt K X (u' + 14) (u' + 20)) = (K.add (K.mul (co K X (u' + 14)) (co K X (u' + 19))) (K.neg (K.mul (co K X (u' + 13)) (co K X (u' + 20))))) := rfl
  have t28 : O.le K.zero (VGoodTail.tt K X (u' + 14) (u' + 18)) := VGoodTail.tail_nn K O X hg (u' + 14) 3
  have et28 : (VGoodTail.tt K X (u' + 14) (u' + 18)) = (K.add (K.mul (co K X (u' + 14)) (co K X (u' + 17))) (K.neg (K.mul (co K X (u' + 13)) (co K X (u' + 18))))) := rfl
  have t29 : O.le K.zero (VGoodTail.tt K X (u' + 14) (u' + 16)) := VGoodTail.tail_nn K O X hg (u' + 14) 1
  have et29 : (VGoodTail.tt K X (u' + 14) (u' + 16)) = (K.add (K.mul (co K X (u' + 14)) (co K X (u' + 15))) (K.neg (K.mul (co K X (u' + 13)) (co K X (u' + 16))))) := rfl
  have t30 : O.le K.zero (VGoodTail.tt K X (u' + 12) (u' + 24)) := VGoodTail.tail_nn K O X hg (u' + 12) 11
  have et30 : (VGoodTail.tt K X (u' + 12) (u' + 24)) = (K.add (K.mul (co K X (u' + 12)) (co K X (u' + 23))) (K.neg (K.mul (co K X (u' + 11)) (co K X (u' + 24))))) := rfl
  have t31 : O.le K.zero (VGoodTail.tt K X (u' + 12) (u' + 16)) := VGoodTail.tail_nn K O X hg (u' + 12) 3
  have et31 : (VGoodTail.tt K X (u' + 12) (u' + 16)) = (K.add (K.mul (co K X (u' + 12)) (co K X (u' + 15))) (K.neg (K.mul (co K X (u' + 11)) (co K X (u' + 16))))) := rfl
  have t32 : O.le K.zero (VGoodTail.tt K X (u' + 10) (u' + 24)) := VGoodTail.tail_nn K O X hg (u' + 10) 13
  have et32 : (VGoodTail.tt K X (u' + 10) (u' + 24)) = (K.add (K.mul (co K X (u' + 10)) (co K X (u' + 23))) (K.neg (K.mul (co K X (u' + 9)) (co K X (u' + 24))))) := rfl
  have t33 : O.le K.zero (VGoodTail.tt K X (u' + 10) (u' + 16)) := VGoodTail.tail_nn K O X hg (u' + 10) 5
  have et33 : (VGoodTail.tt K X (u' + 10) (u' + 16)) = (K.add (K.mul (co K X (u' + 10)) (co K X (u' + 15))) (K.neg (K.mul (co K X (u' + 9)) (co K X (u' + 16))))) := rfl
  have t34 : O.le K.zero (VGoodTail.tt K X (u' + 8) (u' + 24)) := VGoodTail.tail_nn K O X hg (u' + 8) 15
  have et34 : (VGoodTail.tt K X (u' + 8) (u' + 24)) = (K.add (K.mul (co K X (u' + 8)) (co K X (u' + 23))) (K.neg (K.mul (co K X (u' + 7)) (co K X (u' + 24))))) := rfl
  have t35 : O.le K.zero (VGoodTail.tt K X (u' + 8) (u' + 16)) := VGoodTail.tail_nn K O X hg (u' + 8) 7
  have et35 : (VGoodTail.tt K X (u' + 8) (u' + 16)) = (K.add (K.mul (co K X (u' + 8)) (co K X (u' + 15))) (K.neg (K.mul (co K X (u' + 7)) (co K X (u' + 16))))) := rfl
  have t36 : O.le K.zero (VGoodTail.tt K X (u' + 6) (u' + 24)) := VGoodTail.tail_nn K O X hg (u' + 6) 17
  have et36 : (VGoodTail.tt K X (u' + 6) (u' + 24)) = (K.add (K.mul (co K X (u' + 6)) (co K X (u' + 23))) (K.neg (K.mul (co K X (u' + 5)) (co K X (u' + 24))))) := rfl
  have t37 : O.le K.zero (VGoodTail.tt K X (u' + 6) (u' + 22)) := VGoodTail.tail_nn K O X hg (u' + 6) 15
  have et37 : (VGoodTail.tt K X (u' + 6) (u' + 22)) = (K.add (K.mul (co K X (u' + 6)) (co K X (u' + 21))) (K.neg (K.mul (co K X (u' + 5)) (co K X (u' + 22))))) := rfl
  have t38 : O.le K.zero (VGoodTail.tt K X (u' + 6) (u' + 20)) := VGoodTail.tail_nn K O X hg (u' + 6) 13
  have et38 : (VGoodTail.tt K X (u' + 6) (u' + 20)) = (K.add (K.mul (co K X (u' + 6)) (co K X (u' + 19))) (K.neg (K.mul (co K X (u' + 5)) (co K X (u' + 20))))) := rfl
  have t39 : O.le K.zero (VGoodTail.tt K X (u' + 6) (u' + 18)) := VGoodTail.tail_nn K O X hg (u' + 6) 11
  have et39 : (VGoodTail.tt K X (u' + 6) (u' + 18)) = (K.add (K.mul (co K X (u' + 6)) (co K X (u' + 17))) (K.neg (K.mul (co K X (u' + 5)) (co K X (u' + 18))))) := rfl
  have t40 : O.le K.zero (VGoodTail.tt K X (u' + 6) (u' + 16)) := VGoodTail.tail_nn K O X hg (u' + 6) 9
  have et40 : (VGoodTail.tt K X (u' + 6) (u' + 16)) = (K.add (K.mul (co K X (u' + 6)) (co K X (u' + 15))) (K.neg (K.mul (co K X (u' + 5)) (co K X (u' + 16))))) := rfl
  have hp : 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.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.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.ad  -- (+2183 chars; full line in the Zenodo source)
    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 _ _ (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 _ _ (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   -- (+1050 chars; full line in the Zenodo source)
  refine O.le_trans _ _ _ hp (TripleE.le_of_eq' K O _ _ ?_)
  rw [kv, et0, et1, et2, et3, et4, et5, et6, et7, et8, et9, et10, et11, et12, et13, et14, et15, et16, et17, et18, et19, et20, et21, et22, et23, et24, et25, et26, et27, et28, et29, et30, et31, et32, et33, et34, et35, et36, et37, et38, et39, et40]
  ring_z

theorem kna_s_10 (X : List R) (hg : L1Full.Good K O X) (u' : Nat) :
    O.le K.zero (CBLib.kern K (Fa K X) (Ga K X) 5 (u' + 4) (u' + 15)) := by
  have kv : CBLib.kern K (Fa K X) (Ga K X) 5 (u' + 4) (u' + 15) = (K.add (K.add (K.add (K.add (K.add K.zero (K.add (K.mul (co K X (u' + 10)) (co K X (u' + 20))) (K.neg (K.mul (co K X (u' + 21)) (co K X (u' + 9)))))) (K.add (K.mul (K.add (co K X (u' + 11)) (co K X (u' + 9))) (K.add (co K X (u' + 21)) (co K X (u' + 19)))) (K.neg (K.mul (K.add (co K X (u' + 22)) (co K X (u' + 20))) (K.add (co K X (u'  -- (+1324 chars; full line in the Zenodo source)
  have t0 : O.le K.zero (VGoodTail.tt K X (u' + 10) (u' + 21)) := VGoodTail.tail_nn K O X hg (u' + 10) 10
  have et0 : (VGoodTail.tt K X (u' + 10) (u' + 21)) = (K.add (K.mul (co K X (u' + 10)) (co K X (u' + 20))) (K.neg (K.mul (co K X (u' + 9)) (co K X (u' + 21))))) := rfl
  have t1 : O.le K.zero (VGoodTail.tt K X (u' + 11) (u' + 22)) := VGoodTail.tail_nn K O X hg (u' + 11) 10
  have et1 : (VGoodTail.tt K X (u' + 11) (u' + 22)) = (K.add (K.mul (co K X (u' + 11)) (co K X (u' + 21))) (K.neg (K.mul (co K X (u' + 10)) (co K X (u' + 22))))) := rfl
  have t2 : O.le K.zero (VGoodTail.tt K X (u' + 11) (u' + 20)) := VGoodTail.tail_nn K O X hg (u' + 11) 8
  have et2 : (VGoodTail.tt K X (u' + 11) (u' + 20)) = (K.add (K.mul (co K X (u' + 11)) (co K X (u' + 19))) (K.neg (K.mul (co K X (u' + 10)) (co K X (u' + 20))))) := rfl
  have t3 : O.le K.zero (VGoodTail.tt K X (u' + 9) (u' + 22)) := VGoodTail.tail_nn K O X hg (u' + 9) 12
  have et3 : (VGoodTail.tt K X (u' + 9) (u' + 22)) = (K.add (K.mul (co K X (u' + 9)) (co K X (u' + 21))) (K.neg (K.mul (co K X (u' + 8)) (co K X (u' + 22))))) := rfl
  have t4 : O.le K.zero (VGoodTail.tt K X (u' + 9) (u' + 20)) := VGoodTail.tail_nn K O X hg (u' + 9) 10
  have et4 : (VGoodTail.tt K X (u' + 9) (u' + 20)) = (K.add (K.mul (co K X (u' + 9)) (co K X (u' + 19))) (K.neg (K.mul (co K X (u' + 8)) (co K X (u' + 20))))) := rfl
  have t5 : O.le K.zero (VGoodTail.tt K X (u' + 12) (u' + 23)) := VGoodTail.tail_nn K O X hg (u' + 12) 10
  have et5 : (VGoodTail.tt K X (u' + 12) (u' + 23)) = (K.add (K.mul (co K X (u' + 12)) (co K X (u' + 22))) (K.neg (K.mul (co K X (u' + 11)) (co K X (u' + 23))))) := rfl
  have t6 : O.le K.zero (VGoodTail.tt K X (u' + 12) (u' + 21)) := VGoodTail.tail_nn K O X hg (u' + 12) 8
  have et6 : (VGoodTail.tt K X (u' + 12) (u' + 21)) = (K.add (K.mul (co K X (u' + 12)) (co K X (u' + 20))) (K.neg (K.mul (co K X (u' + 11)) (co K X (u' + 21))))) := rfl
  have t7 : O.le K.zero (VGoodTail.tt K X (u' + 12) (u' + 19)) := VGoodTail.tail_nn K O X hg (u' + 12) 6
  have et7 : (VGoodTail.tt K X (u' + 12) (u' + 19)) = (K.add (K.mul (co K X (u' + 12)) (co K X (u' + 18))) (K.neg (K.mul (co K X (u' + 11)) (co K X (u' + 19))))) := rfl
  have t8 : O.le K.zero (VGoodTail.tt K X (u' + 10) (u' + 23)) := VGoodTail.tail_nn K O X hg (u' + 10) 12
  have et8 : (VGoodTail.tt K X (u' + 10) (u' + 23)) = (K.add (K.mul (co K X (u' + 10)) (co K X (u' + 22))) (K.neg (K.mul (co K X (u' + 9)) (co K X (u' + 23))))) := rfl
  have t9 : O.le K.zero (VGoodTail.tt K X (u' + 10) (u' + 19)) := VGoodTail.tail_nn K O X hg (u' + 10) 8
  have et9 : (VGoodTail.tt K X (u' + 10) (u' + 19)) = (K.add (K.mul (co K X (u' + 10)) (co K X (u' + 18))) (K.neg (K.mul (co K X (u' + 9)) (co K X (u' + 19))))) := rfl
  have t10 : O.le K.zero (VGoodTail.tt K X (u' + 8) (u' + 23)) := VGoodTail.tail_nn K O X hg (u' + 8) 14
  have et10 : (VGoodTail.tt K X (u' + 8) (u' + 23)) = (K.add (K.mul (co K X (u' + 8)) (co K X (u' + 22))) (K.neg (K.mul (co K X (u' + 7)) (co K X (u' + 23))))) := rfl
  have t11 : O.le K.zero (VGoodTail.tt K X (u' + 8) (u' + 21)) := VGoodTail.tail_nn K O X hg (u' + 8) 12
  have et11 : (VGoodTail.tt K X (u' + 8) (u' + 21)) = (K.add (K.mul (co K X (u' + 8)) (co K X (u' + 20))) (K.neg (K.mul (co K X (u' + 7)) (co K X (u' + 21))))) := rfl
  have t12 : O.le K.zero (VGoodTail.tt K X (u' + 8) (u' + 19)) := VGoodTail.tail_nn K O X hg (u' + 8) 10
  have et12 : (VGoodTail.tt K X (u' + 8) (u' + 19)) = (K.add (K.mul (co K X (u' + 8)) (co K X (u' + 18))) (K.neg (K.mul (co K X (u' + 7)) (co K X (u' + 19))))) := rfl
  have t13 : O.le K.zero (VGoodTail.tt K X (u' + 13) (u' + 24)) := VGoodTail.tail_nn K O X hg (u' + 13) 10
  have et13 : (VGoodTail.tt K X (u' + 13) (u' + 24)) = (K.add (K.mul (co K X (u' + 13)) (co K X (u' + 23))) (K.neg (K.mul (co K X (u' + 12)) (co K X (u' + 24))))) := rfl
  have t14 : O.le K.zero (VGoodTail.tt K X (u' + 13) (u' + 22)) := VGoodTail.tail_nn K O X hg (u' + 13) 8
  have et14 : (VGoodTail.tt K X (u' + 13) (u' + 22)) = (K.add (K.mul (co K X (u' + 13)) (co K X (u' + 21))) (K.neg (K.mul (co K X (u' + 12)) (co K X (u' + 22))))) := rfl
  have t15 : O.le K.zero (VGoodTail.tt K X (u' + 13) (u' + 20)) := VGoodTail.tail_nn K O X hg (u' + 13) 6
  have et15 : (VGoodTail.tt K X (u' + 13) (u' + 20)) = (K.add (K.mul (co K X (u' + 13)) (co K X (u' + 19))) (K.neg (K.mul (co K X (u' + 12)) (co K X (u' + 20))))) := rfl
  have t16 : O.le K.zero (VGoodTail.tt K X (u' + 13) (u' + 18)) := VGoodTail.tail_nn K O X hg (u' + 13) 4
  have et16 : (VGoodTail.tt K X (u' + 13) (u' + 18)) = (K.add (K.mul (co K X (u' + 13)) (co K X (u' + 17))) (K.neg (K.mul (co K X (u' + 12)) (co K X (u' + 18))))) := rfl
  have t17 : O.le K.zero (VGoodTail.tt K X (u' + 11) (u' + 24)) := VGoodTail.tail_nn K O X hg (u' + 11) 12
  have et17 : (VGoodTail.tt K X (u' + 11) (u' + 24)) = (K.add (K.mul (co K X (u' + 11)) (co K X (u' + 23))) (K.neg (K.mul (co K X (u' + 10)) (co K X (u' + 24))))) := rfl
  have t18 : O.le K.zero (VGoodTail.tt K X (u' + 11) (u' + 18)) := VGoodTail.tail_nn K O X hg (u' + 11) 6
  have et18 : (VGoodTail.tt K X (u' + 11) (u' + 18)) = (K.add (K.mul (co K X (u' + 11)) (co K X (u' + 17))) (K.neg (K.mul (co K X (u' + 10)) (co K X (u' + 18))))) := rfl
  have t19 : O.le K.zero (VGoodTail.tt K X (u' + 9) (u' + 24)) := VGoodTail.tail_nn K O X hg (u' + 9) 14
  have et19 : (VGoodTail.tt K X (u' + 9) (u' + 24)) = (K.add (K.mul (co K X (u' + 9)) (co K X (u' + 23))) (K.neg (K.mul (co K X (u' + 8)) (co K X (u' + 24))))) := rfl
  have t20 : O.le K.zero (VGoodTail.tt K X (u' + 9) (u' + 18)) := VGoodTail.tail_nn K O X hg (u' + 9) 8
  have et20 : (VGoodTail.tt K X (u' + 9) (u' + 18)) = (K.add (K.mul (co K X (u' + 9)) (co K X (u' + 17))) (K.neg (K.mul (co K X (u' + 8)) (co K X (u' + 18))))) := rfl
  have t21 : O.le K.zero (VGoodTail.tt K X (u' + 7) (u' + 24)) := VGoodTail.tail_nn K O X hg (u' + 7) 16
  have et21 : (VGoodTail.tt K X (u' + 7) (u' + 24)) = (K.add (K.mul (co K X (u' + 7)) (co K X (u' + 23))) (K.neg (K.mul (co K X (u' + 6)) (co K X (u' + 24))))) := rfl
  have t22 : O.le K.zero (VGoodTail.tt K X (u' + 7) (u' + 22)) := VGoodTail.tail_nn K O X hg (u' + 7) 14
  have et22 : (VGoodTail.tt K X (u' + 7) (u' + 22)) = (K.add (K.mul (co K X (u' + 7)) (co K X (u' + 21))) (K.neg (K.mul (co K X (u' + 6)) (co K X (u' + 22))))) := rfl
  have t23 : O.le K.zero (VGoodTail.tt K X (u' + 7) (u' + 20)) := VGoodTail.tail_nn K O X hg (u' + 7) 12
  have et23 : (VGoodTail.tt K X (u' + 7) (u' + 20)) = (K.add (K.mul (co K X (u' + 7)) (co K X (u' + 19))) (K.neg (K.mul (co K X (u' + 6)) (co K X (u' + 20))))) := rfl
  have t24 : O.le K.zero (VGoodTail.tt K X (u' + 7) (u' + 18)) := VGoodTail.tail_nn K O X hg (u' + 7) 10
  have et24 : (VGoodTail.tt K X (u' + 7) (u' + 18)) = (K.add (K.mul (co K X (u' + 7)) (co K X (u' + 17))) (K.neg (K.mul (co K X (u' + 6)) (co K X (u' + 18))))) := rfl
  have t25 : O.le K.zero (VGoodTail.tt K X (u' + 14) (u' + 25)) := VGoodTail.tail_nn K O X hg (u' + 14) 10
  have et25 : (VGoodTail.tt K X (u' + 14) (u' + 25)) = (K.add (K.mul (co K X (u' + 14)) (co K X (u' + 24))) (K.neg (K.mul (co K X (u' + 13)) (co K X (u' + 25))))) := rfl
  have t26 : O.le K.zero (VGoodTail.tt K X (u' + 14) (u' + 23)) := VGoodTail.tail_nn K O X hg (u' + 14) 8
  have et26 : (VGoodTail.tt K X (u' + 14) (u' + 23)) = (K.add (K.mul (co K X (u' + 14)) (co K X (u' + 22))) (K.neg (K.mul (co K X (u' + 13)) (co K X (u' + 23))))) := rfl
  have t27 : O.le K.zero (VGoodTail.tt K X (u' + 14) (u' + 21)) := VGoodTail.tail_nn K O X hg (u' + 14) 6
  have et27 : (VGoodTail.tt K X (u' + 14) (u' + 21)) = (K.add (K.mul (co K X (u' + 14)) (co K X (u' + 20))) (K.neg (K.mul (co K X (u' + 13)) (co K X (u' + 21))))) := rfl
  have t28 : O.le K.zero (VGoodTail.tt K X (u' + 14) (u' + 19)) := VGoodTail.tail_nn K O X hg (u' + 14) 4
  have et28 : (VGoodTail.tt K X (u' + 14) (u' + 19)) = (K.add (K.mul (co K X (u' + 14)) (co K X (u' + 18))) (K.neg (K.mul (co K X (u' + 13)) (co K X (u' + 19))))) := rfl
  have t29 : O.le K.zero (VGoodTail.tt K X (u' + 14) (u' + 17)) := VGoodTail.tail_nn K O X hg (u' + 14) 2
  have et29 : (VGoodTail.tt K X (u' + 14) (u' + 17)) = (K.add (K.mul (co K X (u' + 14)) (co K X (u' + 16))) (K.neg (K.mul (co K X (u' + 13)) (co K X (u' + 17))))) := rfl
  have t30 : O.le K.zero (VGoodTail.tt K X (u' + 12) (u' + 25)) := VGoodTail.tail_nn K O X hg (u' + 12) 12
  have et30 : (VGoodTail.tt K X (u' + 12) (u' + 25)) = (K.add (K.mul (co K X (u' + 12)) (co K X (u' + 24))) (K.neg (K.mul (co K X (u' + 11)) (co K X (u' + 25))))) := rfl
  have t31 : O.le K.zero (VGoodTail.tt K X (u' + 12) (u' + 17)) := VGoodTail.tail_nn K O X hg (u' + 12) 4
  have et31 : (VGoodTail.tt K X (u' + 12) (u' + 17)) = (K.add (K.mul (co K X (u' + 12)) (co K X (u' + 16))) (K.neg (K.mul (co K X (u' + 11)) (co K X (u' + 17))))) := rfl
  have t32 : O.le K.zero (VGoodTail.tt K X (u' + 10) (u' + 25)) := VGoodTail.tail_nn K O X hg (u' + 10) 14
  have et32 : (VGoodTail.tt K X (u' + 10) (u' + 25)) = (K.add (K.mul (co K X (u' + 10)) (co K X (u' + 24))) (K.neg (K.mul (co K X (u' + 9)) (co K X (u' + 25))))) := rfl
  have t33 : O.le K.zero (VGoodTail.tt K X (u' + 10) (u' + 17)) := VGoodTail.tail_nn K O X hg (u' + 10) 6
  have et33 : (VGoodTail.tt K X (u' + 10) (u' + 17)) = (K.add (K.mul (co K X (u' + 10)) (co K X (u' + 16))) (K.neg (K.mul (co K X (u' + 9)) (co K X (u' + 17))))) := rfl
  have t34 : O.le K.zero (VGoodTail.tt K X (u' + 8) (u' + 25)) := VGoodTail.tail_nn K O X hg (u' + 8) 16
  have et34 : (VGoodTail.tt K X (u' + 8) (u' + 25)) = (K.add (K.mul (co K X (u' + 8)) (co K X (u' + 24))) (K.neg (K.mul (co K X (u' + 7)) (co K X (u' + 25))))) := rfl
  have t35 : O.le K.zero (VGoodTail.tt K X (u' + 8) (u' + 17)) := VGoodTail.tail_nn K O X hg (u' + 8) 8
  have et35 : (VGoodTail.tt K X (u' + 8) (u' + 17)) = (K.add (K.mul (co K X (u' + 8)) (co K X (u' + 16))) (K.neg (K.mul (co K X (u' + 7)) (co K X (u' + 17))))) := rfl
  have t36 : O.le K.zero (VGoodTail.tt K X (u' + 6) (u' + 25)) := VGoodTail.tail_nn K O X hg (u' + 6) 18
  have et36 : (VGoodTail.tt K X (u' + 6) (u' + 25)) = (K.add (K.mul (co K X (u' + 6)) (co K X (u' + 24))) (K.neg (K.mul (co K X (u' + 5)) (co K X (u' + 25))))) := rfl
  have t37 : O.le K.zero (VGoodTail.tt K X (u' + 6) (u' + 23)) := VGoodTail.tail_nn K O X hg (u' + 6) 16
  have et37 : (VGoodTail.tt K X (u' + 6) (u' + 23)) = (K.add (K.mul (co K X (u' + 6)) (co K X (u' + 22))) (K.neg (K.mul (co K X (u' + 5)) (co K X (u' + 23))))) := rfl
  have t38 : O.le K.zero (VGoodTail.tt K X (u' + 6) (u' + 21)) := VGoodTail.tail_nn K O X hg (u' + 6) 14
  have et38 : (VGoodTail.tt K X (u' + 6) (u' + 21)) = (K.add (K.mul (co K X (u' + 6)) (co K X (u' + 20))) (K.neg (K.mul (co K X (u' + 5)) (co K X (u' + 21))))) := rfl
  have t39 : O.le K.zero (VGoodTail.tt K X (u' + 6) (u' + 19)) := VGoodTail.tail_nn K O X hg (u' + 6) 12
  have et39 : (VGoodTail.tt K X (u' + 6) (u' + 19)) = (K.add (K.mul (co K X (u' + 6)) (co K X (u' + 18))) (K.neg (K.mul (co K X (u' + 5)) (co K X (u' + 19))))) := rfl
  have t40 : O.le K.zero (VGoodTail.tt K X (u' + 6) (u' + 17)) := VGoodTail.tail_nn K O X hg (u' + 6) 10
  have et40 : (VGoodTail.tt K X (u' + 6) (u' + 17)) = (K.add (K.mul (co K X (u' + 6)) (co K X (u' + 16))) (K.neg (K.mul (co K X (u' + 5)) (co K X (u' + 17))))) := rfl
  have hp : 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.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.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.ad  -- (+2183 chars; full line in the Zenodo source)
    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 _ _ (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 _ _ (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   -- (+1050 chars; full line in the Zenodo source)
  refine O.le_trans _ _ _ hp (TripleE.le_of_eq' K O _ _ ?_)
  rw [kv, et0, et1, et2, et3, et4, et5, et6, et7, et8, et9, et10, et11, et12, et13, et14, et15, et16, et17, et18, et19, et20, et21, et22, et23, et24, et25, et26, et27, et28, et29, et30, et31, et32, et33, et34, et35, et36, et37, et38, et39, et40]
  ring_z

theorem kna_s_11 (X : List R) (hg : L1Full.Good K O X) (u' : Nat) :
    O.le K.zero (CBLib.kern K (Fa K X) (Ga K X) 5 (u' + 4) (u' + 16)) := by
  have kv : CBLib.kern K (Fa K X) (Ga K X) 5 (u' + 4) (u' + 16) = (K.add (K.add (K.add (K.add (K.add K.zero (K.add (K.mul (co K X (u' + 10)) (co K X (u' + 21))) (K.neg (K.mul (co K X (u' + 22)) (co K X (u' + 9)))))) (K.add (K.mul (K.add (co K X (u' + 11)) (co K X (u' + 9))) (K.add (co K X (u' + 22)) (co K X (u' + 20)))) (K.neg (K.mul (K.add (co K X (u' + 23)) (co K X (u' + 21))) (K.add (co K X (u'  -- (+1324 chars; full line in the Zenodo source)
  have t0 : O.le K.zero (VGoodTail.tt K X (u' + 10) (u' + 22)) := VGoodTail.tail_nn K O X hg (u' + 10) 11
  have et0 : (VGoodTail.tt K X (u' + 10) (u' + 22)) = (K.add (K.mul (co K X (u' + 10)) (co K X (u' + 21))) (K.neg (K.mul (co K X (u' + 9)) (co K X (u' + 22))))) := rfl
  have t1 : O.le K.zero (VGoodTail.tt K X (u' + 11) (u' + 23)) := VGoodTail.tail_nn K O X hg (u' + 11) 11
  have et1 : (VGoodTail.tt K X (u' + 11) (u' + 23)) = (K.add (K.mul (co K X (u' + 11)) (co K X (u' + 22))) (K.neg (K.mul (co K X (u' + 10)) (co K X (u' + 23))))) := rfl
  have t2 : O.le K.zero (VGoodTail.tt K X (u' + 11) (u' + 21)) := VGoodTail.tail_nn K O X hg (u' + 11) 9
  have et2 : (VGoodTail.tt K X (u' + 11) (u' + 21)) = (K.add (K.mul (co K X (u' + 11)) (co K X (u' + 20))) (K.neg (K.mul (co K X (u' + 10)) (co K X (u' + 21))))) := rfl
  have t3 : O.le K.zero (VGoodTail.tt K X (u' + 9) (u' + 23)) := VGoodTail.tail_nn K O X hg (u' + 9) 13
  have et3 : (VGoodTail.tt K X (u' + 9) (u' + 23)) = (K.add (K.mul (co K X (u' + 9)) (co K X (u' + 22))) (K.neg (K.mul (co K X (u' + 8)) (co K X (u' + 23))))) := rfl
  have t4 : O.le K.zero (VGoodTail.tt K X (u' + 9) (u' + 21)) := VGoodTail.tail_nn K O X hg (u' + 9) 11
  have et4 : (VGoodTail.tt K X (u' + 9) (u' + 21)) = (K.add (K.mul (co K X (u' + 9)) (co K X (u' + 20))) (K.neg (K.mul (co K X (u' + 8)) (co K X (u' + 21))))) := rfl
  have t5 : O.le K.zero (VGoodTail.tt K X (u' + 12) (u' + 24)) := VGoodTail.tail_nn K O X hg (u' + 12) 11
  have et5 : (VGoodTail.tt K X (u' + 12) (u' + 24)) = (K.add (K.mul (co K X (u' + 12)) (co K X (u' + 23))) (K.neg (K.mul (co K X (u' + 11)) (co K X (u' + 24))))) := rfl
  have t6 : O.le K.zero (VGoodTail.tt K X (u' + 12) (u' + 22)) := VGoodTail.tail_nn K O X hg (u' + 12) 9
  have et6 : (VGoodTail.tt K X (u' + 12) (u' + 22)) = (K.add (K.mul (co K X (u' + 12)) (co K X (u' + 21))) (K.neg (K.mul (co K X (u' + 11)) (co K X (u' + 22))))) := rfl
  have t7 : O.le K.zero (VGoodTail.tt K X (u' + 12) (u' + 20)) := VGoodTail.tail_nn K O X hg (u' + 12) 7
  have et7 : (VGoodTail.tt K X (u' + 12) (u' + 20)) = (K.add (K.mul (co K X (u' + 12)) (co K X (u' + 19))) (K.neg (K.mul (co K X (u' + 11)) (co K X (u' + 20))))) := rfl
  have t8 : O.le K.zero (VGoodTail.tt K X (u' + 10) (u' + 24)) := VGoodTail.tail_nn K O X hg (u' + 10) 13
  have et8 : (VGoodTail.tt K X (u' + 10) (u' + 24)) = (K.add (K.mul (co K X (u' + 10)) (co K X (u' + 23))) (K.neg (K.mul (co K X (u' + 9)) (co K X (u' + 24))))) := rfl
  have t9 : O.le K.zero (VGoodTail.tt K X (u' + 10) (u' + 20)) := VGoodTail.tail_nn K O X hg (u' + 10) 9
  have et9 : (VGoodTail.tt K X (u' + 10) (u' + 20)) = (K.add (K.mul (co K X (u' + 10)) (co K X (u' + 19))) (K.neg (K.mul (co K X (u' + 9)) (co K X (u' + 20))))) := rfl
  have t10 : O.le K.zero (VGoodTail.tt K X (u' + 8) (u' + 24)) := VGoodTail.tail_nn K O X hg (u' + 8) 15
  have et10 : (VGoodTail.tt K X (u' + 8) (u' + 24)) = (K.add (K.mul (co K X (u' + 8)) (co K X (u' + 23))) (K.neg (K.mul (co K X (u' + 7)) (co K X (u' + 24))))) := rfl
  have t11 : O.le K.zero (VGoodTail.tt K X (u' + 8) (u' + 22)) := VGoodTail.tail_nn K O X hg (u' + 8) 13
  have et11 : (VGoodTail.tt K X (u' + 8) (u' + 22)) = (K.add (K.mul (co K X (u' + 8)) (co K X (u' + 21))) (K.neg (K.mul (co K X (u' + 7)) (co K X (u' + 22))))) := rfl
  have t12 : O.le K.zero (VGoodTail.tt K X (u' + 8) (u' + 20)) := VGoodTail.tail_nn K O X hg (u' + 8) 11
  have et12 : (VGoodTail.tt K X (u' + 8) (u' + 20)) = (K.add (K.mul (co K X (u' + 8)) (co K X (u' + 19))) (K.neg (K.mul (co K X (u' + 7)) (co K X (u' + 20))))) := rfl
  have t13 : O.le K.zero (VGoodTail.tt K X (u' + 13) (u' + 25)) := VGoodTail.tail_nn K O X hg (u' + 13) 11
  have et13 : (VGoodTail.tt K X (u' + 13) (u' + 25)) = (K.add (K.mul (co K X (u' + 13)) (co K X (u' + 24))) (K.neg (K.mul (co K X (u' + 12)) (co K X (u' + 25))))) := rfl
  have t14 : O.le K.zero (VGoodTail.tt K X (u' + 13) (u' + 23)) := VGoodTail.tail_nn K O X hg (u' + 13) 9
  have et14 : (VGoodTail.tt K X (u' + 13) (u' + 23)) = (K.add (K.mul (co K X (u' + 13)) (co K X (u' + 22))) (K.neg (K.mul (co K X (u' + 12)) (co K X (u' + 23))))) := rfl
  have t15 : O.le K.zero (VGoodTail.tt K X (u' + 13) (u' + 21)) := VGoodTail.tail_nn K O X hg (u' + 13) 7
  have et15 : (VGoodTail.tt K X (u' + 13) (u' + 21)) = (K.add (K.mul (co K X (u' + 13)) (co K X (u' + 20))) (K.neg (K.mul (co K X (u' + 12)) (co K X (u' + 21))))) := rfl
  have t16 : O.le K.zero (VGoodTail.tt K X (u' + 13) (u' + 19)) := VGoodTail.tail_nn K O X hg (u' + 13) 5
  have et16 : (VGoodTail.tt K X (u' + 13) (u' + 19)) = (K.add (K.mul (co K X (u' + 13)) (co K X (u' + 18))) (K.neg (K.mul (co K X (u' + 12)) (co K X (u' + 19))))) := rfl
  have t17 : O.le K.zero (VGoodTail.tt K X (u' + 11) (u' + 25)) := VGoodTail.tail_nn K O X hg (u' + 11) 13
  have et17 : (VGoodTail.tt K X (u' + 11) (u' + 25)) = (K.add (K.mul (co K X (u' + 11)) (co K X (u' + 24))) (K.neg (K.mul (co K X (u' + 10)) (co K X (u' + 25))))) := rfl
  have t18 : O.le K.zero (VGoodTail.tt K X (u' + 11) (u' + 19)) := VGoodTail.tail_nn K O X hg (u' + 11) 7
  have et18 : (VGoodTail.tt K X (u' + 11) (u' + 19)) = (K.add (K.mul (co K X (u' + 11)) (co K X (u' + 18))) (K.neg (K.mul (co K X (u' + 10)) (co K X (u' + 19))))) := rfl
  have t19 : O.le K.zero (VGoodTail.tt K X (u' + 9) (u' + 25)) := VGoodTail.tail_nn K O X hg (u' + 9) 15
  have et19 : (VGoodTail.tt K X (u' + 9) (u' + 25)) = (K.add (K.mul (co K X (u' + 9)) (co K X (u' + 24))) (K.neg (K.mul (co K X (u' + 8)) (co K X (u' + 25))))) := rfl
  have t20 : O.le K.zero (VGoodTail.tt K X (u' + 9) (u' + 19)) := VGoodTail.tail_nn K O X hg (u' + 9) 9
  have et20 : (VGoodTail.tt K X (u' + 9) (u' + 19)) = (K.add (K.mul (co K X (u' + 9)) (co K X (u' + 18))) (K.neg (K.mul (co K X (u' + 8)) (co K X (u' + 19))))) := rfl
  have t21 : O.le K.zero (VGoodTail.tt K X (u' + 7) (u' + 25)) := VGoodTail.tail_nn K O X hg (u' + 7) 17
  have et21 : (VGoodTail.tt K X (u' + 7) (u' + 25)) = (K.add (K.mul (co K X (u' + 7)) (co K X (u' + 24))) (K.neg (K.mul (co K X (u' + 6)) (co K X (u' + 25))))) := rfl
  have t22 : O.le K.zero (VGoodTail.tt K X (u' + 7) (u' + 23)) := VGoodTail.tail_nn K O X hg (u' + 7) 15
  have et22 : (VGoodTail.tt K X (u' + 7) (u' + 23)) = (K.add (K.mul (co K X (u' + 7)) (co K X (u' + 22))) (K.neg (K.mul (co K X (u' + 6)) (co K X (u' + 23))))) := rfl
  have t23 : O.le K.zero (VGoodTail.tt K X (u' + 7) (u' + 21)) := VGoodTail.tail_nn K O X hg (u' + 7) 13
  have et23 : (VGoodTail.tt K X (u' + 7) (u' + 21)) = (K.add (K.mul (co K X (u' + 7)) (co K X (u' + 20))) (K.neg (K.mul (co K X (u' + 6)) (co K X (u' + 21))))) := rfl
  have t24 : O.le K.zero (VGoodTail.tt K X (u' + 7) (u' + 19)) := VGoodTail.tail_nn K O X hg (u' + 7) 11
  have et24 : (VGoodTail.tt K X (u' + 7) (u' + 19)) = (K.add (K.mul (co K X (u' + 7)) (co K X (u' + 18))) (K.neg (K.mul (co K X (u' + 6)) (co K X (u' + 19))))) := rfl
  have t25 : O.le K.zero (VGoodTail.tt K X (u' + 14) (u' + 26)) := VGoodTail.tail_nn K O X hg (u' + 14) 11
  have et25 : (VGoodTail.tt K X (u' + 14) (u' + 26)) = (K.add (K.mul (co K X (u' + 14)) (co K X (u' + 25))) (K.neg (K.mul (co K X (u' + 13)) (co K X (u' + 26))))) := rfl
  have t26 : O.le K.zero (VGoodTail.tt K X (u' + 14) (u' + 24)) := VGoodTail.tail_nn K O X hg (u' + 14) 9
  have et26 : (VGoodTail.tt K X (u' + 14) (u' + 24)) = (K.add (K.mul (co K X (u' + 14)) (co K X (u' + 23))) (K.neg (K.mul (co K X (u' + 13)) (co K X (u' + 24))))) := rfl
  have t27 : O.le K.zero (VGoodTail.tt K X (u' + 14) (u' + 22)) := VGoodTail.tail_nn K O X hg (u' + 14) 7
  have et27 : (VGoodTail.tt K X (u' + 14) (u' + 22)) = (K.add (K.mul (co K X (u' + 14)) (co K X (u' + 21))) (K.neg (K.mul (co K X (u' + 13)) (co K X (u' + 22))))) := rfl
  have t28 : O.le K.zero (VGoodTail.tt K X (u' + 14) (u' + 20)) := VGoodTail.tail_nn K O X hg (u' + 14) 5
  have et28 : (VGoodTail.tt K X (u' + 14) (u' + 20)) = (K.add (K.mul (co K X (u' + 14)) (co K X (u' + 19))) (K.neg (K.mul (co K X (u' + 13)) (co K X (u' + 20))))) := rfl
  have t29 : O.le K.zero (VGoodTail.tt K X (u' + 14) (u' + 18)) := VGoodTail.tail_nn K O X hg (u' + 14) 3
  have et29 : (VGoodTail.tt K X (u' + 14) (u' + 18)) = (K.add (K.mul (co K X (u' + 14)) (co K X (u' + 17))) (K.neg (K.mul (co K X (u' + 13)) (co K X (u' + 18))))) := rfl
  have t30 : O.le K.zero (VGoodTail.tt K X (u' + 12) (u' + 26)) := VGoodTail.tail_nn K O X hg (u' + 12) 13
  have et30 : (VGoodTail.tt K X (u' + 12) (u' + 26)) = (K.add (K.mul (co K X (u' + 12)) (co K X (u' + 25))) (K.neg (K.mul (co K X (u' + 11)) (co K X (u' + 26))))) := rfl
  have t31 : O.le K.zero (VGoodTail.tt K X (u' + 12) (u' + 18)) := VGoodTail.tail_nn K O X hg (u' + 12) 5
  have et31 : (VGoodTail.tt K X (u' + 12) (u' + 18)) = (K.add (K.mul (co K X (u' + 12)) (co K X (u' + 17))) (K.neg (K.mul (co K X (u' + 11)) (co K X (u' + 18))))) := rfl
  have t32 : O.le K.zero (VGoodTail.tt K X (u' + 10) (u' + 26)) := VGoodTail.tail_nn K O X hg (u' + 10) 15
  have et32 : (VGoodTail.tt K X (u' + 10) (u' + 26)) = (K.add (K.mul (co K X (u' + 10)) (co K X (u' + 25))) (K.neg (K.mul (co K X (u' + 9)) (co K X (u' + 26))))) := rfl
  have t33 : O.le K.zero (VGoodTail.tt K X (u' + 10) (u' + 18)) := VGoodTail.tail_nn K O X hg (u' + 10) 7
  have et33 : (VGoodTail.tt K X (u' + 10) (u' + 18)) = (K.add (K.mul (co K X (u' + 10)) (co K X (u' + 17))) (K.neg (K.mul (co K X (u' + 9)) (co K X (u' + 18))))) := rfl
  have t34 : O.le K.zero (VGoodTail.tt K X (u' + 8) (u' + 26)) := VGoodTail.tail_nn K O X hg (u' + 8) 17
  have et34 : (VGoodTail.tt K X (u' + 8) (u' + 26)) = (K.add (K.mul (co K X (u' + 8)) (co K X (u' + 25))) (K.neg (K.mul (co K X (u' + 7)) (co K X (u' + 26))))) := rfl
  have t35 : O.le K.zero (VGoodTail.tt K X (u' + 8) (u' + 18)) := VGoodTail.tail_nn K O X hg (u' + 8) 9
  have et35 : (VGoodTail.tt K X (u' + 8) (u' + 18)) = (K.add (K.mul (co K X (u' + 8)) (co K X (u' + 17))) (K.neg (K.mul (co K X (u' + 7)) (co K X (u' + 18))))) := rfl
  have t36 : O.le K.zero (VGoodTail.tt K X (u' + 6) (u' + 26)) := VGoodTail.tail_nn K O X hg (u' + 6) 19
  have et36 : (VGoodTail.tt K X (u' + 6) (u' + 26)) = (K.add (K.mul (co K X (u' + 6)) (co K X (u' + 25))) (K.neg (K.mul (co K X (u' + 5)) (co K X (u' + 26))))) := rfl
  have t37 : O.le K.zero (VGoodTail.tt K X (u' + 6) (u' + 24)) := VGoodTail.tail_nn K O X hg (u' + 6) 17
  have et37 : (VGoodTail.tt K X (u' + 6) (u' + 24)) = (K.add (K.mul (co K X (u' + 6)) (co K X (u' + 23))) (K.neg (K.mul (co K X (u' + 5)) (co K X (u' + 24))))) := rfl
  have t38 : O.le K.zero (VGoodTail.tt K X (u' + 6) (u' + 22)) := VGoodTail.tail_nn K O X hg (u' + 6) 15
  have et38 : (VGoodTail.tt K X (u' + 6) (u' + 22)) = (K.add (K.mul (co K X (u' + 6)) (co K X (u' + 21))) (K.neg (K.mul (co K X (u' + 5)) (co K X (u' + 22))))) := rfl
  have t39 : O.le K.zero (VGoodTail.tt K X (u' + 6) (u' + 20)) := VGoodTail.tail_nn K O X hg (u' + 6) 13
  have et39 : (VGoodTail.tt K X (u' + 6) (u' + 20)) = (K.add (K.mul (co K X (u' + 6)) (co K X (u' + 19))) (K.neg (K.mul (co K X (u' + 5)) (co K X (u' + 20))))) := rfl
  have t40 : O.le K.zero (VGoodTail.tt K X (u' + 6) (u' + 18)) := VGoodTail.tail_nn K O X hg (u' + 6) 11
  have et40 : (VGoodTail.tt K X (u' + 6) (u' + 18)) = (K.add (K.mul (co K X (u' + 6)) (co K X (u' + 17))) (K.neg (K.mul (co K X (u' + 5)) (co K X (u' + 18))))) := rfl
  have hp : 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.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.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.ad  -- (+2183 chars; full line in the Zenodo source)
    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 _ _ (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 _ _ (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   -- (+1050 chars; full line in the Zenodo source)
  refine O.le_trans _ _ _ hp (TripleE.le_of_eq' K O _ _ ?_)
  rw [kv, et0, et1, et2, et3, et4, et5, et6, et7, et8, et9, et10, et11, et12, et13, et14, et15, et16, et17, et18, et19, et20, et21, et22, et23, et24, et25, et26, et27, et28, et29, et30, et31, et32, et33, et34, et35, et36, et37, et38, et39, et40]
  ring_z

theorem kna_s_12 (X : List R) (hg : L1Full.Good K O X) (u' : Nat) :
    O.le K.zero (CBLib.kern K (Fa K X) (Ga K X) 5 (u' + 4) (u' + 17)) := by
  have kv : CBLib.kern K (Fa K X) (Ga K X) 5 (u' + 4) (u' + 17) = (K.add (K.add (K.add (K.add (K.add K.zero (K.add (K.mul (co K X (u' + 10)) (co K X (u' + 22))) (K.neg (K.mul (co K X (u' + 23)) (co K X (u' + 9)))))) (K.add (K.mul (K.add (co K X (u' + 11)) (co K X (u' + 9))) (K.add (co K X (u' + 23)) (co K X (u' + 21)))) (K.neg (K.mul (K.add (co K X (u' + 24)) (co K X (u' + 22))) (K.add (co K X (u'  -- (+1324 chars; full line in the Zenodo source)
  have t0 : O.le K.zero (VGoodTail.tt K X (u' + 10) (u' + 23)) := VGoodTail.tail_nn K O X hg (u' + 10) 12
  have et0 : (VGoodTail.tt K X (u' + 10) (u' + 23)) = (K.add (K.mul (co K X (u' + 10)) (co K X (u' + 22))) (K.neg (K.mul (co K X (u' + 9)) (co K X (u' + 23))))) := rfl
  have t1 : O.le K.zero (VGoodTail.tt K X (u' + 11) (u' + 24)) := VGoodTail.tail_nn K O X hg (u' + 11) 12
  have et1 : (VGoodTail.tt K X (u' + 11) (u' + 24)) = (K.add (K.mul (co K X (u' + 11)) (co K X (u' + 23))) (K.neg (K.mul (co K X (u' + 10)) (co K X (u' + 24))))) := rfl
  have t2 : O.le K.zero (VGoodTail.tt K X (u' + 11) (u' + 22)) := VGoodTail.tail_nn K O X hg (u' + 11) 10
  have et2 : (VGoodTail.tt K X (u' + 11) (u' + 22)) = (K.add (K.mul (co K X (u' + 11)) (co K X (u' + 21))) (K.neg (K.mul (co K X (u' + 10)) (co K X (u' + 22))))) := rfl
  have t3 : O.le K.zero (VGoodTail.tt K X (u' + 9) (u' + 24)) := VGoodTail.tail_nn K O X hg (u' + 9) 14
  have et3 : (VGoodTail.tt K X (u' + 9) (u' + 24)) = (K.add (K.mul (co K X (u' + 9)) (co K X (u' + 23))) (K.neg (K.mul (co K X (u' + 8)) (co K X (u' + 24))))) := rfl
  have t4 : O.le K.zero (VGoodTail.tt K X (u' + 9) (u' + 22)) := VGoodTail.tail_nn K O X hg (u' + 9) 12
  have et4 : (VGoodTail.tt K X (u' + 9) (u' + 22)) = (K.add (K.mul (co K X (u' + 9)) (co K X (u' + 21))) (K.neg (K.mul (co K X (u' + 8)) (co K X (u' + 22))))) := rfl
  have t5 : O.le K.zero (VGoodTail.tt K X (u' + 12) (u' + 25)) := VGoodTail.tail_nn K O X hg (u' + 12) 12
  have et5 : (VGoodTail.tt K X (u' + 12) (u' + 25)) = (K.add (K.mul (co K X (u' + 12)) (co K X (u' + 24))) (K.neg (K.mul (co K X (u' + 11)) (co K X (u' + 25))))) := rfl
  have t6 : O.le K.zero (VGoodTail.tt K X (u' + 12) (u' + 23)) := VGoodTail.tail_nn K O X hg (u' + 12) 10
  have et6 : (VGoodTail.tt K X (u' + 12) (u' + 23)) = (K.add (K.mul (co K X (u' + 12)) (co K X (u' + 22))) (K.neg (K.mul (co K X (u' + 11)) (co K X (u' + 23))))) := rfl
  have t7 : O.le K.zero (VGoodTail.tt K X (u' + 12) (u' + 21)) := VGoodTail.tail_nn K O X hg (u' + 12) 8
  have et7 : (VGoodTail.tt K X (u' + 12) (u' + 21)) = (K.add (K.mul (co K X (u' + 12)) (co K X (u' + 20))) (K.neg (K.mul (co K X (u' + 11)) (co K X (u' + 21))))) := rfl
  have t8 : O.le K.zero (VGoodTail.tt K X (u' + 10) (u' + 25)) := VGoodTail.tail_nn K O X hg (u' + 10) 14
  have et8 : (VGoodTail.tt K X (u' + 10) (u' + 25)) = (K.add (K.mul (co K X (u' + 10)) (co K X (u' + 24))) (K.neg (K.mul (co K X (u' + 9)) (co K X (u' + 25))))) := rfl
  have t9 : O.le K.zero (VGoodTail.tt K X (u' + 10) (u' + 21)) := VGoodTail.tail_nn K O X hg (u' + 10) 10
  have et9 : (VGoodTail.tt K X (u' + 10) (u' + 21)) = (K.add (K.mul (co K X (u' + 10)) (co K X (u' + 20))) (K.neg (K.mul (co K X (u' + 9)) (co K X (u' + 21))))) := rfl
  have t10 : O.le K.zero (VGoodTail.tt K X (u' + 8) (u' + 25)) := VGoodTail.tail_nn K O X hg (u' + 8) 16
  have et10 : (VGoodTail.tt K X (u' + 8) (u' + 25)) = (K.add (K.mul (co K X (u' + 8)) (co K X (u' + 24))) (K.neg (K.mul (co K X (u' + 7)) (co K X (u' + 25))))) := rfl
  have t11 : O.le K.zero (VGoodTail.tt K X (u' + 8) (u' + 23)) := VGoodTail.tail_nn K O X hg (u' + 8) 14
  have et11 : (VGoodTail.tt K X (u' + 8) (u' + 23)) = (K.add (K.mul (co K X (u' + 8)) (co K X (u' + 22))) (K.neg (K.mul (co K X (u' + 7)) (co K X (u' + 23))))) := rfl
  have t12 : O.le K.zero (VGoodTail.tt K X (u' + 8) (u' + 21)) := VGoodTail.tail_nn K O X hg (u' + 8) 12
  have et12 : (VGoodTail.tt K X (u' + 8) (u' + 21)) = (K.add (K.mul (co K X (u' + 8)) (co K X (u' + 20))) (K.neg (K.mul (co K X (u' + 7)) (co K X (u' + 21))))) := rfl
  have t13 : O.le K.zero (VGoodTail.tt K X (u' + 13) (u' + 26)) := VGoodTail.tail_nn K O X hg (u' + 13) 12
  have et13 : (VGoodTail.tt K X (u' + 13) (u' + 26)) = (K.add (K.mul (co K X (u' + 13)) (co K X (u' + 25))) (K.neg (K.mul (co K X (u' + 12)) (co K X (u' + 26))))) := rfl
  have t14 : O.le K.zero (VGoodTail.tt K X (u' + 13) (u' + 24)) := VGoodTail.tail_nn K O X hg (u' + 13) 10
  have et14 : (VGoodTail.tt K X (u' + 13) (u' + 24)) = (K.add (K.mul (co K X (u' + 13)) (co K X (u' + 23))) (K.neg (K.mul (co K X (u' + 12)) (co K X (u' + 24))))) := rfl
  have t15 : O.le K.zero (VGoodTail.tt K X (u' + 13) (u' + 22)) := VGoodTail.tail_nn K O X hg (u' + 13) 8
  have et15 : (VGoodTail.tt K X (u' + 13) (u' + 22)) = (K.add (K.mul (co K X (u' + 13)) (co K X (u' + 21))) (K.neg (K.mul (co K X (u' + 12)) (co K X (u' + 22))))) := rfl
  have t16 : O.le K.zero (VGoodTail.tt K X (u' + 13) (u' + 20)) := VGoodTail.tail_nn K O X hg (u' + 13) 6
  have et16 : (VGoodTail.tt K X (u' + 13) (u' + 20)) = (K.add (K.mul (co K X (u' + 13)) (co K X (u' + 19))) (K.neg (K.mul (co K X (u' + 12)) (co K X (u' + 20))))) := rfl
  have t17 : O.le K.zero (VGoodTail.tt K X (u' + 11) (u' + 26)) := VGoodTail.tail_nn K O X hg (u' + 11) 14
  have et17 : (VGoodTail.tt K X (u' + 11) (u' + 26)) = (K.add (K.mul (co K X (u' + 11)) (co K X (u' + 25))) (K.neg (K.mul (co K X (u' + 10)) (co K X (u' + 26))))) := rfl
  have t18 : O.le K.zero (VGoodTail.tt K X (u' + 11) (u' + 20)) := VGoodTail.tail_nn K O X hg (u' + 11) 8
  have et18 : (VGoodTail.tt K X (u' + 11) (u' + 20)) = (K.add (K.mul (co K X (u' + 11)) (co K X (u' + 19))) (K.neg (K.mul (co K X (u' + 10)) (co K X (u' + 20))))) := rfl
  have t19 : O.le K.zero (VGoodTail.tt K X (u' + 9) (u' + 26)) := VGoodTail.tail_nn K O X hg (u' + 9) 16
  have et19 : (VGoodTail.tt K X (u' + 9) (u' + 26)) = (K.add (K.mul (co K X (u' + 9)) (co K X (u' + 25))) (K.neg (K.mul (co K X (u' + 8)) (co K X (u' + 26))))) := rfl
  have t20 : O.le K.zero (VGoodTail.tt K X (u' + 9) (u' + 20)) := VGoodTail.tail_nn K O X hg (u' + 9) 10
  have et20 : (VGoodTail.tt K X (u' + 9) (u' + 20)) = (K.add (K.mul (co K X (u' + 9)) (co K X (u' + 19))) (K.neg (K.mul (co K X (u' + 8)) (co K X (u' + 20))))) := rfl
  have t21 : O.le K.zero (VGoodTail.tt K X (u' + 7) (u' + 26)) := VGoodTail.tail_nn K O X hg (u' + 7) 18
  have et21 : (VGoodTail.tt K X (u' + 7) (u' + 26)) = (K.add (K.mul (co K X (u' + 7)) (co K X (u' + 25))) (K.neg (K.mul (co K X (u' + 6)) (co K X (u' + 26))))) := rfl
  have t22 : O.le K.zero (VGoodTail.tt K X (u' + 7) (u' + 24)) := VGoodTail.tail_nn K O X hg (u' + 7) 16
  have et22 : (VGoodTail.tt K X (u' + 7) (u' + 24)) = (K.add (K.mul (co K X (u' + 7)) (co K X (u' + 23))) (K.neg (K.mul (co K X (u' + 6)) (co K X (u' + 24))))) := rfl
  have t23 : O.le K.zero (VGoodTail.tt K X (u' + 7) (u' + 22)) := VGoodTail.tail_nn K O X hg (u' + 7) 14
  have et23 : (VGoodTail.tt K X (u' + 7) (u' + 22)) = (K.add (K.mul (co K X (u' + 7)) (co K X (u' + 21))) (K.neg (K.mul (co K X (u' + 6)) (co K X (u' + 22))))) := rfl
  have t24 : O.le K.zero (VGoodTail.tt K X (u' + 7) (u' + 20)) := VGoodTail.tail_nn K O X hg (u' + 7) 12
  have et24 : (VGoodTail.tt K X (u' + 7) (u' + 20)) = (K.add (K.mul (co K X (u' + 7)) (co K X (u' + 19))) (K.neg (K.mul (co K X (u' + 6)) (co K X (u' + 20))))) := rfl
  have t25 : O.le K.zero (VGoodTail.tt K X (u' + 14) (u' + 27)) := VGoodTail.tail_nn K O X hg (u' + 14) 12
  have et25 : (VGoodTail.tt K X (u' + 14) (u' + 27)) = (K.add (K.mul (co K X (u' + 14)) (co K X (u' + 26))) (K.neg (K.mul (co K X (u' + 13)) (co K X (u' + 27))))) := rfl
  have t26 : O.le K.zero (VGoodTail.tt K X (u' + 14) (u' + 25)) := VGoodTail.tail_nn K O X hg (u' + 14) 10
  have et26 : (VGoodTail.tt K X (u' + 14) (u' + 25)) = (K.add (K.mul (co K X (u' + 14)) (co K X (u' + 24))) (K.neg (K.mul (co K X (u' + 13)) (co K X (u' + 25))))) := rfl
  have t27 : O.le K.zero (VGoodTail.tt K X (u' + 14) (u' + 23)) := VGoodTail.tail_nn K O X hg (u' + 14) 8
  have et27 : (VGoodTail.tt K X (u' + 14) (u' + 23)) = (K.add (K.mul (co K X (u' + 14)) (co K X (u' + 22))) (K.neg (K.mul (co K X (u' + 13)) (co K X (u' + 23))))) := rfl
  have t28 : O.le K.zero (VGoodTail.tt K X (u' + 14) (u' + 21)) := VGoodTail.tail_nn K O X hg (u' + 14) 6
  have et28 : (VGoodTail.tt K X (u' + 14) (u' + 21)) = (K.add (K.mul (co K X (u' + 14)) (co K X (u' + 20))) (K.neg (K.mul (co K X (u' + 13)) (co K X (u' + 21))))) := rfl
  have t29 : O.le K.zero (VGoodTail.tt K X (u' + 14) (u' + 19)) := VGoodTail.tail_nn K O X hg (u' + 14) 4
  have et29 : (VGoodTail.tt K X (u' + 14) (u' + 19)) = (K.add (K.mul (co K X (u' + 14)) (co K X (u' + 18))) (K.neg (K.mul (co K X (u' + 13)) (co K X (u' + 19))))) := rfl
  have t30 : O.le K.zero (VGoodTail.tt K X (u' + 12) (u' + 27)) := VGoodTail.tail_nn K O X hg (u' + 12) 14
  have et30 : (VGoodTail.tt K X (u' + 12) (u' + 27)) = (K.add (K.mul (co K X (u' + 12)) (co K X (u' + 26))) (K.neg (K.mul (co K X (u' + 11)) (co K X (u' + 27))))) := rfl
  have t31 : O.le K.zero (VGoodTail.tt K X (u' + 12) (u' + 19)) := VGoodTail.tail_nn K O X hg (u' + 12) 6
  have et31 : (VGoodTail.tt K X (u' + 12) (u' + 19)) = (K.add (K.mul (co K X (u' + 12)) (co K X (u' + 18))) (K.neg (K.mul (co K X (u' + 11)) (co K X (u' + 19))))) := rfl
  have t32 : O.le K.zero (VGoodTail.tt K X (u' + 10) (u' + 27)) := VGoodTail.tail_nn K O X hg (u' + 10) 16
  have et32 : (VGoodTail.tt K X (u' + 10) (u' + 27)) = (K.add (K.mul (co K X (u' + 10)) (co K X (u' + 26))) (K.neg (K.mul (co K X (u' + 9)) (co K X (u' + 27))))) := rfl
  have t33 : O.le K.zero (VGoodTail.tt K X (u' + 10) (u' + 19)) := VGoodTail.tail_nn K O X hg (u' + 10) 8
  have et33 : (VGoodTail.tt K X (u' + 10) (u' + 19)) = (K.add (K.mul (co K X (u' + 10)) (co K X (u' + 18))) (K.neg (K.mul (co K X (u' + 9)) (co K X (u' + 19))))) := rfl
  have t34 : O.le K.zero (VGoodTail.tt K X (u' + 8) (u' + 27)) := VGoodTail.tail_nn K O X hg (u' + 8) 18
  have et34 : (VGoodTail.tt K X (u' + 8) (u' + 27)) = (K.add (K.mul (co K X (u' + 8)) (co K X (u' + 26))) (K.neg (K.mul (co K X (u' + 7)) (co K X (u' + 27))))) := rfl
  have t35 : O.le K.zero (VGoodTail.tt K X (u' + 8) (u' + 19)) := VGoodTail.tail_nn K O X hg (u' + 8) 10
  have et35 : (VGoodTail.tt K X (u' + 8) (u' + 19)) = (K.add (K.mul (co K X (u' + 8)) (co K X (u' + 18))) (K.neg (K.mul (co K X (u' + 7)) (co K X (u' + 19))))) := rfl
  have t36 : O.le K.zero (VGoodTail.tt K X (u' + 6) (u' + 27)) := VGoodTail.tail_nn K O X hg (u' + 6) 20
  have et36 : (VGoodTail.tt K X (u' + 6) (u' + 27)) = (K.add (K.mul (co K X (u' + 6)) (co K X (u' + 26))) (K.neg (K.mul (co K X (u' + 5)) (co K X (u' + 27))))) := rfl
  have t37 : O.le K.zero (VGoodTail.tt K X (u' + 6) (u' + 25)) := VGoodTail.tail_nn K O X hg (u' + 6) 18
  have et37 : (VGoodTail.tt K X (u' + 6) (u' + 25)) = (K.add (K.mul (co K X (u' + 6)) (co K X (u' + 24))) (K.neg (K.mul (co K X (u' + 5)) (co K X (u' + 25))))) := rfl
  have t38 : O.le K.zero (VGoodTail.tt K X (u' + 6) (u' + 23)) := VGoodTail.tail_nn K O X hg (u' + 6) 16
  have et38 : (VGoodTail.tt K X (u' + 6) (u' + 23)) = (K.add (K.mul (co K X (u' + 6)) (co K X (u' + 22))) (K.neg (K.mul (co K X (u' + 5)) (co K X (u' + 23))))) := rfl
  have t39 : O.le K.zero (VGoodTail.tt K X (u' + 6) (u' + 21)) := VGoodTail.tail_nn K O X hg (u' + 6) 14
  have et39 : (VGoodTail.tt K X (u' + 6) (u' + 21)) = (K.add (K.mul (co K X (u' + 6)) (co K X (u' + 20))) (K.neg (K.mul (co K X (u' + 5)) (co K X (u' + 21))))) := rfl
  have t40 : O.le K.zero (VGoodTail.tt K X (u' + 6) (u' + 19)) := VGoodTail.tail_nn K O X hg (u' + 6) 12
  have et40 : (VGoodTail.tt K X (u' + 6) (u' + 19)) = (K.add (K.mul (co K X (u' + 6)) (co K X (u' + 18))) (K.neg (K.mul (co K X (u' + 5)) (co K X (u' + 19))))) := rfl
  have hp : 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.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.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.ad  -- (+2183 chars; full line in the Zenodo source)
    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 _ _ (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 _ _ (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   -- (+1050 chars; full line in the Zenodo source)
  refine O.le_trans _ _ _ hp (TripleE.le_of_eq' K O _ _ ?_)
  rw [kv, et0, et1, et2, et3, et4, et5, et6, et7, et8, et9, et10, et11, et12, et13, et14, et15, et16, et17, et18, et19, et20, et21, et22, et23, et24, et25, et26, et27, et28, et29, et30, et31, et32, et33, et34, et35, et36, et37, et38, et39, et40]
  ring_z

theorem kna_0_s (X : List R) (hg : L1Full.Good K O X) (k' : Nat) :
    O.le K.zero (CBLib.kern K (Fa K X) (Ga K X) 5 (0) (k' + 14)) := by
  have kv : CBLib.kern K (Fa K X) (Ga K X) 5 (0) (k' + 14) = (K.add (K.add (K.add (K.add (K.add K.zero (K.add (K.mul (co K X (6)) (co K X (k' + 19))) (K.neg (K.mul (co K X (k' + 20)) (co K X (5)))))) (K.add (K.mul (co K X (7)) (K.add (co K X (k' + 20)) (co K X (k' + 18)))) (K.neg (K.mul (K.add (co K X (k' + 21)) (co K X (k' + 19))) (co K X (6)))))) (K.add (K.mul (co K X (8)) (K.add (K.add (co K X   -- (+735 chars; full line in the Zenodo source)
  have t0 : O.le K.zero (VGoodTail.tt K X (6) (k' + 20)) := by
    rw [show k' + 20 = 6 + (k' + 13) + 1 by lib_nat]
    exact VGoodTail.tail_nn K O X hg _ _
  have et0 : (VGoodTail.tt K X (6) (k' + 20)) = (K.add (K.mul (co K X (6)) (co K X (k' + 19))) (K.neg (K.mul (co K X (5)) (co K X (k' + 20))))) := rfl
  have t1 : O.le K.zero (VGoodTail.tt K X (7) (k' + 21)) := by
    rw [show k' + 21 = 7 + (k' + 13) + 1 by lib_nat]
    exact VGoodTail.tail_nn K O X hg _ _
  have et1 : (VGoodTail.tt K X (7) (k' + 21)) = (K.add (K.mul (co K X (7)) (co K X (k' + 20))) (K.neg (K.mul (co K X (6)) (co K X (k' + 21))))) := rfl
  have t2 : O.le K.zero (VGoodTail.tt K X (7) (k' + 19)) := by
    rw [show k' + 19 = 7 + (k' + 11) + 1 by lib_nat]
    exact VGoodTail.tail_nn K O X hg _ _
  have et2 : (VGoodTail.tt K X (7) (k' + 19)) = (K.add (K.mul (co K X (7)) (co K X (k' + 18))) (K.neg (K.mul (co K X (6)) (co K X (k' + 19))))) := rfl
  have t3 : O.le K.zero (VGoodTail.tt K X (8) (k' + 22)) := by
    rw [show k' + 22 = 8 + (k' + 13) + 1 by lib_nat]
    exact VGoodTail.tail_nn K O X hg _ _
  have et3 : (VGoodTail.tt K X (8) (k' + 22)) = (K.add (K.mul (co K X (8)) (co K X (k' + 21))) (K.neg (K.mul (co K X (7)) (co K X (k' + 22))))) := rfl
  have t4 : O.le K.zero (VGoodTail.tt K X (8) (k' + 20)) := by
    rw [show k' + 20 = 8 + (k' + 11) + 1 by lib_nat]
    exact VGoodTail.tail_nn K O X hg _ _
  have et4 : (VGoodTail.tt K X (8) (k' + 20)) = (K.add (K.mul (co K X (8)) (co K X (k' + 19))) (K.neg (K.mul (co K X (7)) (co K X (k' + 20))))) := rfl
  have t5 : O.le K.zero (VGoodTail.tt K X (8) (k' + 18)) := by
    rw [show k' + 18 = 8 + (k' + 9) + 1 by lib_nat]
    exact VGoodTail.tail_nn K O X hg _ _
  have et5 : (VGoodTail.tt K X (8) (k' + 18)) = (K.add (K.mul (co K X (8)) (co K X (k' + 17))) (K.neg (K.mul (co K X (7)) (co K X (k' + 18))))) := rfl
  have t6 : O.le K.zero (VGoodTail.tt K X (9) (k' + 23)) := by
    rw [show k' + 23 = 9 + (k' + 13) + 1 by lib_nat]
    exact VGoodTail.tail_nn K O X hg _ _
  have et6 : (VGoodTail.tt K X (9) (k' + 23)) = (K.add (K.mul (co K X (9)) (co K X (k' + 22))) (K.neg (K.mul (co K X (8)) (co K X (k' + 23))))) := rfl
  have t7 : O.le K.zero (VGoodTail.tt K X (9) (k' + 21)) := by
    rw [show k' + 21 = 9 + (k' + 11) + 1 by lib_nat]
    exact VGoodTail.tail_nn K O X hg _ _
  have et7 : (VGoodTail.tt K X (9) (k' + 21)) = (K.add (K.mul (co K X (9)) (co K X (k' + 20))) (K.neg (K.mul (co K X (8)) (co K X (k' + 21))))) := rfl
  have t8 : O.le K.zero (VGoodTail.tt K X (9) (k' + 19)) := by
    rw [show k' + 19 = 9 + (k' + 9) + 1 by lib_nat]
    exact VGoodTail.tail_nn K O X hg _ _
  have et8 : (VGoodTail.tt K X (9) (k' + 19)) = (K.add (K.mul (co K X (9)) (co K X (k' + 18))) (K.neg (K.mul (co K X (8)) (co K X (k' + 19))))) := rfl
  have t9 : O.le K.zero (VGoodTail.tt K X (9) (k' + 17)) := by
    rw [show k' + 17 = 9 + (k' + 7) + 1 by lib_nat]
    exact VGoodTail.tail_nn K O X hg _ _
  have et9 : (VGoodTail.tt K X (9) (k' + 17)) = (K.add (K.mul (co K X (9)) (co K X (k' + 16))) (K.neg (K.mul (co K X (8)) (co K X (k' + 17))))) := rfl
  have t10 : O.le K.zero (VGoodTail.tt K X (10) (k' + 24)) := by
    rw [show k' + 24 = 10 + (k' + 13) + 1 by lib_nat]
    exact VGoodTail.tail_nn K O X hg _ _
  have et10 : (VGoodTail.tt K X (10) (k' + 24)) = (K.add (K.mul (co K X (10)) (co K X (k' + 23))) (K.neg (K.mul (co K X (9)) (co K X (k' + 24))))) := rfl
  have t11 : O.le K.zero (VGoodTail.tt K X (10) (k' + 22)) := by
    rw [show k' + 22 = 10 + (k' + 11) + 1 by lib_nat]
    exact VGoodTail.tail_nn K O X hg _ _
  have et11 : (VGoodTail.tt K X (10) (k' + 22)) = (K.add (K.mul (co K X (10)) (co K X (k' + 21))) (K.neg (K.mul (co K X (9)) (co K X (k' + 22))))) := rfl
  have t12 : O.le K.zero (VGoodTail.tt K X (10) (k' + 20)) := by
    rw [show k' + 20 = 10 + (k' + 9) + 1 by lib_nat]
    exact VGoodTail.tail_nn K O X hg _ _
  have et12 : (VGoodTail.tt K X (10) (k' + 20)) = (K.add (K.mul (co K X (10)) (co K X (k' + 19))) (K.neg (K.mul (co K X (9)) (co K X (k' + 20))))) := rfl
  have t13 : O.le K.zero (VGoodTail.tt K X (10) (k' + 18)) := by
    rw [show k' + 18 = 10 + (k' + 7) + 1 by lib_nat]
    exact VGoodTail.tail_nn K O X hg _ _
  have et13 : (VGoodTail.tt K X (10) (k' + 18)) = (K.add (K.mul (co K X (10)) (co K X (k' + 17))) (K.neg (K.mul (co K X (9)) (co K X (k' + 18))))) := rfl
  have t14 : O.le K.zero (VGoodTail.tt K X (10) (k' + 16)) := by
    rw [show k' + 16 = 10 + (k' + 5) + 1 by lib_nat]
    exact VGoodTail.tail_nn K O X hg _ _
  have et14 : (VGoodTail.tt K X (10) (k' + 16)) = (K.add (K.mul (co K X (10)) (co K X (k' + 15))) (K.neg (K.mul (co K X (9)) (co K X (k' + 16))))) := rfl
  have hp : 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.add (VGoodTail.tt K X (6) (k' + 20)) (VGoodTail.tt K X (7) (k' + 21))) (VGoodTail.tt K X (7) (k' + 19))) (VGoodTail.tt K X (8) (k' + 22))) (VGoodTail.tt K X (8) (k' + 20))) (VGoodTail.tt K X (8) (k' + 18))) (VGoodTail.tt K X (9) (k' + 23))) (VGoodTail.tt K X (9) (k' + 21))) (VGoodT  -- (+241 chars; full line in the Zenodo source)
    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 _ _ (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 _ _ (TopLC.add_nn K O _ _ (TopLC.add_nn K O _ _ t0 t1) t2) t3) t4) t5) t6) t7) t8) t9) t10) t11) t12) t13) t14)
  refine O.le_trans _ _ _ hp (TripleE.le_of_eq' K O _ _ ?_)
  rw [kv, et0, et1, et2, et3, et4, et5, et6, et7, et8, et9, et10, et11, et12, et13, et14]
  ring_z

theorem kna_1_s (X : List R) (hg : L1Full.Good K O X) (k' : Nat) :
    O.le K.zero (CBLib.kern K (Fa K X) (Ga K X) 5 (1) (k' + 15)) := by
  have kv : CBLib.kern K (Fa K X) (Ga K X) 5 (1) (k' + 15) = (K.add (K.add (K.add (K.add (K.add K.zero (K.add (K.mul (co K X (7)) (co K X (k' + 20))) (K.neg (K.mul (co K X (k' + 21)) (co K X (6)))))) (K.add (K.mul (K.add (co K X (8)) (co K X (6))) (K.add (co K X (k' + 21)) (co K X (k' + 19)))) (K.neg (K.mul (K.add (co K X (k' + 22)) (co K X (k' + 20))) (K.add (co K X (7)) (co K X (5))))))) (K.add   -- (+905 chars; full line in the Zenodo source)
  have t0 : O.le K.zero (VGoodTail.tt K X (7) (k' + 21)) := by
    rw [show k' + 21 = 7 + (k' + 13) + 1 by lib_nat]
    exact VGoodTail.tail_nn K O X hg _ _
  have et0 : (VGoodTail.tt K X (7) (k' + 21)) = (K.add (K.mul (co K X (7)) (co K X (k' + 20))) (K.neg (K.mul (co K X (6)) (co K X (k' + 21))))) := rfl
  have t1 : O.le K.zero (VGoodTail.tt K X (8) (k' + 22)) := by
    rw [show k' + 22 = 8 + (k' + 13) + 1 by lib_nat]
    exact VGoodTail.tail_nn K O X hg _ _
  have et1 : (VGoodTail.tt K X (8) (k' + 22)) = (K.add (K.mul (co K X (8)) (co K X (k' + 21))) (K.neg (K.mul (co K X (7)) (co K X (k' + 22))))) := rfl
  have t2 : O.le K.zero (VGoodTail.tt K X (8) (k' + 20)) := by
    rw [show k' + 20 = 8 + (k' + 11) + 1 by lib_nat]
    exact VGoodTail.tail_nn K O X hg _ _
  have et2 : (VGoodTail.tt K X (8) (k' + 20)) = (K.add (K.mul (co K X (8)) (co K X (k' + 19))) (K.neg (K.mul (co K X (7)) (co K X (k' + 20))))) := rfl
  have t3 : O.le K.zero (VGoodTail.tt K X (6) (k' + 22)) := by
    rw [show k' + 22 = 6 + (k' + 15) + 1 by lib_nat]
    exact VGoodTail.tail_nn K O X hg _ _
  have et3 : (VGoodTail.tt K X (6) (k' + 22)) = (K.add (K.mul (co K X (6)) (co K X (k' + 21))) (K.neg (K.mul (co K X (5)) (co K X (k' + 22))))) := rfl
  have t4 : O.le K.zero (VGoodTail.tt K X (6) (k' + 20)) := by
    rw [show k' + 20 = 6 + (k' + 13) + 1 by lib_nat]
    exact VGoodTail.tail_nn K O X hg _ _
  have et4 : (VGoodTail.tt K X (6) (k' + 20)) = (K.add (K.mul (co K X (6)) (co K X (k' + 19))) (K.neg (K.mul (co K X (5)) (co K X (k' + 20))))) := rfl
  have t5 : O.le K.zero (VGoodTail.tt K X (9) (k' + 23)) := by
    rw [show k' + 23 = 9 + (k' + 13) + 1 by lib_nat]
    exact VGoodTail.tail_nn K O X hg _ _
  have et5 : (VGoodTail.tt K X (9) (k' + 23)) = (K.add (K.mul (co K X (9)) (co K X (k' + 22))) (K.neg (K.mul (co K X (8)) (co K X (k' + 23))))) := rfl
  have t6 : O.le K.zero (VGoodTail.tt K X (9) (k' + 21)) := by
    rw [show k' + 21 = 9 + (k' + 11) + 1 by lib_nat]
    exact VGoodTail.tail_nn K O X hg _ _
  have et6 : (VGoodTail.tt K X (9) (k' + 21)) = (K.add (K.mul (co K X (9)) (co K X (k' + 20))) (K.neg (K.mul (co K X (8)) (co K X (k' + 21))))) := rfl
  have t7 : O.le K.zero (VGoodTail.tt K X (9) (k' + 19)) := by
    rw [show k' + 19 = 9 + (k' + 9) + 1 by lib_nat]
    exact VGoodTail.tail_nn K O X hg _ _
  have et7 : (VGoodTail.tt K X (9) (k' + 19)) = (K.add (K.mul (co K X (9)) (co K X (k' + 18))) (K.neg (K.mul (co K X (8)) (co K X (k' + 19))))) := rfl
  have t8 : O.le K.zero (VGoodTail.tt K X (7) (k' + 23)) := by
    rw [show k' + 23 = 7 + (k' + 15) + 1 by lib_nat]
    exact VGoodTail.tail_nn K O X hg _ _
  have et8 : (VGoodTail.tt K X (7) (k' + 23)) = (K.add (K.mul (co K X (7)) (co K X (k' + 22))) (K.neg (K.mul (co K X (6)) (co K X (k' + 23))))) := rfl
  have t9 : O.le K.zero (VGoodTail.tt K X (7) (k' + 19)) := by
    rw [show k' + 19 = 7 + (k' + 11) + 1 by lib_nat]
    exact VGoodTail.tail_nn K O X hg _ _
  have et9 : (VGoodTail.tt K X (7) (k' + 19)) = (K.add (K.mul (co K X (7)) (co K X (k' + 18))) (K.neg (K.mul (co K X (6)) (co K X (k' + 19))))) := rfl
  have t10 : O.le K.zero (VGoodTail.tt K X (10) (k' + 24)) := by
    rw [show k' + 24 = 10 + (k' + 13) + 1 by lib_nat]
    exact VGoodTail.tail_nn K O X hg _ _
  have et10 : (VGoodTail.tt K X (10) (k' + 24)) = (K.add (K.mul (co K X (10)) (co K X (k' + 23))) (K.neg (K.mul (co K X (9)) (co K X (k' + 24))))) := rfl
  have t11 : O.le K.zero (VGoodTail.tt K X (10) (k' + 22)) := by
    rw [show k' + 22 = 10 + (k' + 11) + 1 by lib_nat]
    exact VGoodTail.tail_nn K O X hg _ _
  have et11 : (VGoodTail.tt K X (10) (k' + 22)) = (K.add (K.mul (co K X (10)) (co K X (k' + 21))) (K.neg (K.mul (co K X (9)) (co K X (k' + 22))))) := rfl
  have t12 : O.le K.zero (VGoodTail.tt K X (10) (k' + 20)) := by
    rw [show k' + 20 = 10 + (k' + 9) + 1 by lib_nat]
    exact VGoodTail.tail_nn K O X hg _ _
  have et12 : (VGoodTail.tt K X (10) (k' + 20)) = (K.add (K.mul (co K X (10)) (co K X (k' + 19))) (K.neg (K.mul (co K X (9)) (co K X (k' + 20))))) := rfl
  have t13 : O.le K.zero (VGoodTail.tt K X (10) (k' + 18)) := by
    rw [show k' + 18 = 10 + (k' + 7) + 1 by lib_nat]
    exact VGoodTail.tail_nn K O X hg _ _
  have et13 : (VGoodTail.tt K X (10) (k' + 18)) = (K.add (K.mul (co K X (10)) (co K X (k' + 17))) (K.neg (K.mul (co K X (9)) (co K X (k' + 18))))) := rfl
  have t14 : O.le K.zero (VGoodTail.tt K X (8) (k' + 24)) := by
    rw [show k' + 24 = 8 + (k' + 15) + 1 by lib_nat]
    exact VGoodTail.tail_nn K O X hg _ _
  have et14 : (VGoodTail.tt K X (8) (k' + 24)) = (K.add (K.mul (co K X (8)) (co K X (k' + 23))) (K.neg (K.mul (co K X (7)) (co K X (k' + 24))))) := rfl
  have t15 : O.le K.zero (VGoodTail.tt K X (8) (k' + 18)) := by
    rw [show k' + 18 = 8 + (k' + 9) + 1 by lib_nat]
    exact VGoodTail.tail_nn K O X hg _ _
  have et15 : (VGoodTail.tt K X (8) (k' + 18)) = (K.add (K.mul (co K X (8)) (co K X (k' + 17))) (K.neg (K.mul (co K X (7)) (co K X (k' + 18))))) := rfl
  have t16 : O.le K.zero (VGoodTail.tt K X (11) (k' + 25)) := by
    rw [show k' + 25 = 11 + (k' + 13) + 1 by lib_nat]
    exact VGoodTail.tail_nn K O X hg _ _
  have et16 : (VGoodTail.tt K X (11) (k' + 25)) = (K.add (K.mul (co K X (11)) (co K X (k' + 24))) (K.neg (K.mul (co K X (10)) (co K X (k' + 25))))) := rfl
  have t17 : O.le K.zero (VGoodTail.tt K X (11) (k' + 23)) := by
    rw [show k' + 23 = 11 + (k' + 11) + 1 by lib_nat]
    exact VGoodTail.tail_nn K O X hg _ _
  have et17 : (VGoodTail.tt K X (11) (k' + 23)) = (K.add (K.mul (co K X (11)) (co K X (k' + 22))) (K.neg (K.mul (co K X (10)) (co K X (k' + 23))))) := rfl
  have t18 : O.le K.zero (VGoodTail.tt K X (11) (k' + 21)) := by
    rw [show k' + 21 = 11 + (k' + 9) + 1 by lib_nat]
    exact VGoodTail.tail_nn K O X hg _ _
  have et18 : (VGoodTail.tt K X (11) (k' + 21)) = (K.add (K.mul (co K X (11)) (co K X (k' + 20))) (K.neg (K.mul (co K X (10)) (co K X (k' + 21))))) := rfl
  have t19 : O.le K.zero (VGoodTail.tt K X (11) (k' + 19)) := by
    rw [show k' + 19 = 11 + (k' + 7) + 1 by lib_nat]
    exact VGoodTail.tail_nn K O X hg _ _
  have et19 : (VGoodTail.tt K X (11) (k' + 19)) = (K.add (K.mul (co K X (11)) (co K X (k' + 18))) (K.neg (K.mul (co K X (10)) (co K X (k' + 19))))) := rfl
  have t20 : O.le K.zero (VGoodTail.tt K X (11) (k' + 17)) := by
    rw [show k' + 17 = 11 + (k' + 5) + 1 by lib_nat]
    exact VGoodTail.tail_nn K O X hg _ _
  have et20 : (VGoodTail.tt K X (11) (k' + 17)) = (K.add (K.mul (co K X (11)) (co K X (k' + 16))) (K.neg (K.mul (co K X (10)) (co K X (k' + 17))))) := rfl
  have t21 : O.le K.zero (VGoodTail.tt K X (9) (k' + 25)) := by
    rw [show k' + 25 = 9 + (k' + 15) + 1 by lib_nat]
    exact VGoodTail.tail_nn K O X hg _ _
  have et21 : (VGoodTail.tt K X (9) (k' + 25)) = (K.add (K.mul (co K X (9)) (co K X (k' + 24))) (K.neg (K.mul (co K X (8)) (co K X (k' + 25))))) := rfl
  have t22 : O.le K.zero (VGoodTail.tt K X (9) (k' + 17)) := by
    rw [show k' + 17 = 9 + (k' + 7) + 1 by lib_nat]
    exact VGoodTail.tail_nn K O X hg _ _
  have et22 : (VGoodTail.tt K X (9) (k' + 17)) = (K.add (K.mul (co K X (9)) (co K X (k' + 16))) (K.neg (K.mul (co K X (8)) (co K X (k' + 17))))) := rfl
  have hp : 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.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 (VGoodTail.tt K X (7) (k' + 21)) (VGoodTail.tt K X (7) (k' + 21))) (VGoodTail.tt K X (8) (k' + 22))) (VGoodTail.tt K X (8) (k' + 22))) (VGoodTail.tt K X (8) (k' + 20))) (VGoodTail.  -- (+819 chars; full line in the Zenodo source)
    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 _ _ (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 _ _ (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   -- (+353 chars; full line in the Zenodo source)
  refine O.le_trans _ _ _ hp (TripleE.le_of_eq' K O _ _ ?_)
  rw [kv, et0, et1, et2, et3, et4, et5, et6, et7, et8, et9, et10, et11, et12, et13, et14, et15, et16, et17, et18, et19, et20, et21, et22]
  ring_z

theorem kna_2_s (X : List R) (hg : L1Full.Good K O X) (k' : Nat) :
    O.le K.zero (CBLib.kern K (Fa K X) (Ga K X) 5 (2) (k' + 16)) := by
  have kv : CBLib.kern K (Fa K X) (Ga K X) 5 (2) (k' + 16) = (K.add (K.add (K.add (K.add (K.add K.zero (K.add (K.mul (co K X (8)) (co K X (k' + 21))) (K.neg (K.mul (co K X (k' + 22)) (co K X (7)))))) (K.add (K.mul (K.add (co K X (9)) (co K X (7))) (K.add (co K X (k' + 22)) (co K X (k' + 20)))) (K.neg (K.mul (K.add (co K X (k' + 23)) (co K X (k' + 21))) (K.add (co K X (8)) (co K X (6))))))) (K.add   -- (+1034 chars; full line in the Zenodo source)
  have t0 : O.le K.zero (VGoodTail.tt K X (8) (k' + 22)) := by
    rw [show k' + 22 = 8 + (k' + 13) + 1 by lib_nat]
    exact VGoodTail.tail_nn K O X hg _ _
  have et0 : (VGoodTail.tt K X (8) (k' + 22)) = (K.add (K.mul (co K X (8)) (co K X (k' + 21))) (K.neg (K.mul (co K X (7)) (co K X (k' + 22))))) := rfl
  have t1 : O.le K.zero (VGoodTail.tt K X (9) (k' + 23)) := by
    rw [show k' + 23 = 9 + (k' + 13) + 1 by lib_nat]
    exact VGoodTail.tail_nn K O X hg _ _
  have et1 : (VGoodTail.tt K X (9) (k' + 23)) = (K.add (K.mul (co K X (9)) (co K X (k' + 22))) (K.neg (K.mul (co K X (8)) (co K X (k' + 23))))) := rfl
  have t2 : O.le K.zero (VGoodTail.tt K X (9) (k' + 21)) := by
    rw [show k' + 21 = 9 + (k' + 11) + 1 by lib_nat]
    exact VGoodTail.tail_nn K O X hg _ _
  have et2 : (VGoodTail.tt K X (9) (k' + 21)) = (K.add (K.mul (co K X (9)) (co K X (k' + 20))) (K.neg (K.mul (co K X (8)) (co K X (k' + 21))))) := rfl
  have t3 : O.le K.zero (VGoodTail.tt K X (7) (k' + 23)) := by
    rw [show k' + 23 = 7 + (k' + 15) + 1 by lib_nat]
    exact VGoodTail.tail_nn K O X hg _ _
  have et3 : (VGoodTail.tt K X (7) (k' + 23)) = (K.add (K.mul (co K X (7)) (co K X (k' + 22))) (K.neg (K.mul (co K X (6)) (co K X (k' + 23))))) := rfl
  have t4 : O.le K.zero (VGoodTail.tt K X (7) (k' + 21)) := by
    rw [show k' + 21 = 7 + (k' + 13) + 1 by lib_nat]
    exact VGoodTail.tail_nn K O X hg _ _
  have et4 : (VGoodTail.tt K X (7) (k' + 21)) = (K.add (K.mul (co K X (7)) (co K X (k' + 20))) (K.neg (K.mul (co K X (6)) (co K X (k' + 21))))) := rfl
  have t5 : O.le K.zero (VGoodTail.tt K X (10) (k' + 24)) := by
    rw [show k' + 24 = 10 + (k' + 13) + 1 by lib_nat]
    exact VGoodTail.tail_nn K O X hg _ _
  have et5 : (VGoodTail.tt K X (10) (k' + 24)) = (K.add (K.mul (co K X (10)) (co K X (k' + 23))) (K.neg (K.mul (co K X (9)) (co K X (k' + 24))))) := rfl
  have t6 : O.le K.zero (VGoodTail.tt K X (10) (k' + 22)) := by
    rw [show k' + 22 = 10 + (k' + 11) + 1 by lib_nat]
    exact VGoodTail.tail_nn K O X hg _ _
  have et6 : (VGoodTail.tt K X (10) (k' + 22)) = (K.add (K.mul (co K X (10)) (co K X (k' + 21))) (K.neg (K.mul (co K X (9)) (co K X (k' + 22))))) := rfl
  have t7 : O.le K.zero (VGoodTail.tt K X (10) (k' + 20)) := by
    rw [show k' + 20 = 10 + (k' + 9) + 1 by lib_nat]
    exact VGoodTail.tail_nn K O X hg _ _
  have et7 : (VGoodTail.tt K X (10) (k' + 20)) = (K.add (K.mul (co K X (10)) (co K X (k' + 19))) (K.neg (K.mul (co K X (9)) (co K X (k' + 20))))) := rfl
  have t8 : O.le K.zero (VGoodTail.tt K X (8) (k' + 24)) := by
    rw [show k' + 24 = 8 + (k' + 15) + 1 by lib_nat]
    exact VGoodTail.tail_nn K O X hg _ _
  have et8 : (VGoodTail.tt K X (8) (k' + 24)) = (K.add (K.mul (co K X (8)) (co K X (k' + 23))) (K.neg (K.mul (co K X (7)) (co K X (k' + 24))))) := rfl
  have t9 : O.le K.zero (VGoodTail.tt K X (8) (k' + 20)) := by
    rw [show k' + 20 = 8 + (k' + 11) + 1 by lib_nat]
    exact VGoodTail.tail_nn K O X hg _ _
  have et9 : (VGoodTail.tt K X (8) (k' + 20)) = (K.add (K.mul (co K X (8)) (co K X (k' + 19))) (K.neg (K.mul (co K X (7)) (co K X (k' + 20))))) := rfl
  have t10 : O.le K.zero (VGoodTail.tt K X (6) (k' + 24)) := by
    rw [show k' + 24 = 6 + (k' + 17) + 1 by lib_nat]
    exact VGoodTail.tail_nn K O X hg _ _
  have et10 : (VGoodTail.tt K X (6) (k' + 24)) = (K.add (K.mul (co K X (6)) (co K X (k' + 23))) (K.neg (K.mul (co K X (5)) (co K X (k' + 24))))) := rfl
  have t11 : O.le K.zero (VGoodTail.tt K X (6) (k' + 22)) := by
    rw [show k' + 22 = 6 + (k' + 15) + 1 by lib_nat]
    exact VGoodTail.tail_nn K O X hg _ _
  have et11 : (VGoodTail.tt K X (6) (k' + 22)) = (K.add (K.mul (co K X (6)) (co K X (k' + 21))) (K.neg (K.mul (co K X (5)) (co K X (k' + 22))))) := rfl
  have t12 : O.le K.zero (VGoodTail.tt K X (6) (k' + 20)) := by
    rw [show k' + 20 = 6 + (k' + 13) + 1 by lib_nat]
    exact VGoodTail.tail_nn K O X hg _ _
  have et12 : (VGoodTail.tt K X (6) (k' + 20)) = (K.add (K.mul (co K X (6)) (co K X (k' + 19))) (K.neg (K.mul (co K X (5)) (co K X (k' + 20))))) := rfl
  have t13 : O.le K.zero (VGoodTail.tt K X (11) (k' + 25)) := by
    rw [show k' + 25 = 11 + (k' + 13) + 1 by lib_nat]
    exact VGoodTail.tail_nn K O X hg _ _
  have et13 : (VGoodTail.tt K X (11) (k' + 25)) = (K.add (K.mul (co K X (11)) (co K X (k' + 24))) (K.neg (K.mul (co K X (10)) (co K X (k' + 25))))) := rfl
  have t14 : O.le K.zero (VGoodTail.tt K X (11) (k' + 23)) := by
    rw [show k' + 23 = 11 + (k' + 11) + 1 by lib_nat]
    exact VGoodTail.tail_nn K O X hg _ _
  have et14 : (VGoodTail.tt K X (11) (k' + 23)) = (K.add (K.mul (co K X (11)) (co K X (k' + 22))) (K.neg (K.mul (co K X (10)) (co K X (k' + 23))))) := rfl
  have t15 : O.le K.zero (VGoodTail.tt K X (11) (k' + 21)) := by
    rw [show k' + 21 = 11 + (k' + 9) + 1 by lib_nat]
    exact VGoodTail.tail_nn K O X hg _ _
  have et15 : (VGoodTail.tt K X (11) (k' + 21)) = (K.add (K.mul (co K X (11)) (co K X (k' + 20))) (K.neg (K.mul (co K X (10)) (co K X (k' + 21))))) := rfl
  have t16 : O.le K.zero (VGoodTail.tt K X (11) (k' + 19)) := by
    rw [show k' + 19 = 11 + (k' + 7) + 1 by lib_nat]
    exact VGoodTail.tail_nn K O X hg _ _
  have et16 : (VGoodTail.tt K X (11) (k' + 19)) = (K.add (K.mul (co K X (11)) (co K X (k' + 18))) (K.neg (K.mul (co K X (10)) (co K X (k' + 19))))) := rfl
  have t17 : O.le K.zero (VGoodTail.tt K X (9) (k' + 25)) := by
    rw [show k' + 25 = 9 + (k' + 15) + 1 by lib_nat]
    exact VGoodTail.tail_nn K O X hg _ _
  have et17 : (VGoodTail.tt K X (9) (k' + 25)) = (K.add (K.mul (co K X (9)) (co K X (k' + 24))) (K.neg (K.mul (co K X (8)) (co K X (k' + 25))))) := rfl
  have t18 : O.le K.zero (VGoodTail.tt K X (9) (k' + 19)) := by
    rw [show k' + 19 = 9 + (k' + 9) + 1 by lib_nat]
    exact VGoodTail.tail_nn K O X hg _ _
  have et18 : (VGoodTail.tt K X (9) (k' + 19)) = (K.add (K.mul (co K X (9)) (co K X (k' + 18))) (K.neg (K.mul (co K X (8)) (co K X (k' + 19))))) := rfl
  have t19 : O.le K.zero (VGoodTail.tt K X (7) (k' + 25)) := by
    rw [show k' + 25 = 7 + (k' + 17) + 1 by lib_nat]
    exact VGoodTail.tail_nn K O X hg _ _
  have et19 : (VGoodTail.tt K X (7) (k' + 25)) = (K.add (K.mul (co K X (7)) (co K X (k' + 24))) (K.neg (K.mul (co K X (6)) (co K X (k' + 25))))) := rfl
  have t20 : O.le K.zero (VGoodTail.tt K X (7) (k' + 19)) := by
    rw [show k' + 19 = 7 + (k' + 11) + 1 by lib_nat]
    exact VGoodTail.tail_nn K O X hg _ _
  have et20 : (VGoodTail.tt K X (7) (k' + 19)) = (K.add (K.mul (co K X (7)) (co K X (k' + 18))) (K.neg (K.mul (co K X (6)) (co K X (k' + 19))))) := rfl
  have t21 : O.le K.zero (VGoodTail.tt K X (12) (k' + 26)) := by
    rw [show k' + 26 = 12 + (k' + 13) + 1 by lib_nat]
    exact VGoodTail.tail_nn K O X hg _ _
  have et21 : (VGoodTail.tt K X (12) (k' + 26)) = (K.add (K.mul (co K X (12)) (co K X (k' + 25))) (K.neg (K.mul (co K X (11)) (co K X (k' + 26))))) := rfl
  have t22 : O.le K.zero (VGoodTail.tt K X (12) (k' + 24)) := by
    rw [show k' + 24 = 12 + (k' + 11) + 1 by lib_nat]
    exact VGoodTail.tail_nn K O X hg _ _
  have et22 : (VGoodTail.tt K X (12) (k' + 24)) = (K.add (K.mul (co K X (12)) (co K X (k' + 23))) (K.neg (K.mul (co K X (11)) (co K X (k' + 24))))) := rfl
  have t23 : O.le K.zero (VGoodTail.tt K X (12) (k' + 22)) := by
    rw [show k' + 22 = 12 + (k' + 9) + 1 by lib_nat]
    exact VGoodTail.tail_nn K O X hg _ _
  have et23 : (VGoodTail.tt K X (12) (k' + 22)) = (K.add (K.mul (co K X (12)) (co K X (k' + 21))) (K.neg (K.mul (co K X (11)) (co K X (k' + 22))))) := rfl
  have t24 : O.le K.zero (VGoodTail.tt K X (12) (k' + 20)) := by
    rw [show k' + 20 = 12 + (k' + 7) + 1 by lib_nat]
    exact VGoodTail.tail_nn K O X hg _ _
  have et24 : (VGoodTail.tt K X (12) (k' + 20)) = (K.add (K.mul (co K X (12)) (co K X (k' + 19))) (K.neg (K.mul (co K X (11)) (co K X (k' + 20))))) := rfl
  have t25 : O.le K.zero (VGoodTail.tt K X (12) (k' + 18)) := by
    rw [show k' + 18 = 12 + (k' + 5) + 1 by lib_nat]
    exact VGoodTail.tail_nn K O X hg _ _
  have et25 : (VGoodTail.tt K X (12) (k' + 18)) = (K.add (K.mul (co K X (12)) (co K X (k' + 17))) (K.neg (K.mul (co K X (11)) (co K X (k' + 18))))) := rfl
  have t26 : O.le K.zero (VGoodTail.tt K X (10) (k' + 26)) := by
    rw [show k' + 26 = 10 + (k' + 15) + 1 by lib_nat]
    exact VGoodTail.tail_nn K O X hg _ _
  have et26 : (VGoodTail.tt K X (10) (k' + 26)) = (K.add (K.mul (co K X (10)) (co K X (k' + 25))) (K.neg (K.mul (co K X (9)) (co K X (k' + 26))))) := rfl
  have t27 : O.le K.zero (VGoodTail.tt K X (10) (k' + 18)) := by
    rw [show k' + 18 = 10 + (k' + 7) + 1 by lib_nat]
    exact VGoodTail.tail_nn K O X hg _ _
  have et27 : (VGoodTail.tt K X (10) (k' + 18)) = (K.add (K.mul (co K X (10)) (co K X (k' + 17))) (K.neg (K.mul (co K X (9)) (co K X (k' + 18))))) := rfl
  have t28 : O.le K.zero (VGoodTail.tt K X (8) (k' + 26)) := by
    rw [show k' + 26 = 8 + (k' + 17) + 1 by lib_nat]
    exact VGoodTail.tail_nn K O X hg _ _
  have et28 : (VGoodTail.tt K X (8) (k' + 26)) = (K.add (K.mul (co K X (8)) (co K X (k' + 25))) (K.neg (K.mul (co K X (7)) (co K X (k' + 26))))) := rfl
  have t29 : O.le K.zero (VGoodTail.tt K X (8) (k' + 18)) := by
    rw [show k' + 18 = 8 + (k' + 9) + 1 by lib_nat]
    exact VGoodTail.tail_nn K O X hg _ _
  have et29 : (VGoodTail.tt K X (8) (k' + 18)) = (K.add (K.mul (co K X (8)) (co K X (k' + 17))) (K.neg (K.mul (co K X (7)) (co K X (k' + 18))))) := rfl
  have hp : 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.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 (VGoodTail.tt K X (8) (k' + 22)) (VGoodTail.tt K X (8) (k' + 22))) (VGoodTail.tt K X (8) (k' + 2  -- (+1319 chars; full line in the Zenodo source)
    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 _ _ (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 _ _ (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   -- (+672 chars; full line in the Zenodo source)
  refine O.le_trans _ _ _ hp (TripleE.le_of_eq' K O _ _ ?_)
  rw [kv, et0, et1, et2, et3, et4, et5, et6, et7, et8, et9, et10, et11, et12, et13, et14, et15, et16, et17, et18, et19, et20, et21, et22, et23, et24, et25, et26, et27, et28, et29]
  ring_z

theorem kna_3_s (X : List R) (hg : L1Full.Good K O X) (k' : Nat) :
    O.le K.zero (CBLib.kern K (Fa K X) (Ga K X) 5 (3) (k' + 17)) := by
  have kv : CBLib.kern K (Fa K X) (Ga K X) 5 (3) (k' + 17) = (K.add (K.add (K.add (K.add (K.add K.zero (K.add (K.mul (co K X (9)) (co K X (k' + 22))) (K.neg (K.mul (co K X (k' + 23)) (co K X (8)))))) (K.add (K.mul (K.add (co K X (10)) (co K X (8))) (K.add (co K X (k' + 23)) (co K X (k' + 21)))) (K.neg (K.mul (K.add (co K X (k' + 24)) (co K X (k' + 22))) (K.add (co K X (9)) (co K X (7))))))) (K.add  -- (+1122 chars; full line in the Zenodo source)
  have t0 : O.le K.zero (VGoodTail.tt K X (9) (k' + 23)) := by
    rw [show k' + 23 = 9 + (k' + 13) + 1 by lib_nat]
    exact VGoodTail.tail_nn K O X hg _ _
  have et0 : (VGoodTail.tt K X (9) (k' + 23)) = (K.add (K.mul (co K X (9)) (co K X (k' + 22))) (K.neg (K.mul (co K X (8)) (co K X (k' + 23))))) := rfl
  have t1 : O.le K.zero (VGoodTail.tt K X (10) (k' + 24)) := by
    rw [show k' + 24 = 10 + (k' + 13) + 1 by lib_nat]
    exact VGoodTail.tail_nn K O X hg _ _
  have et1 : (VGoodTail.tt K X (10) (k' + 24)) = (K.add (K.mul (co K X (10)) (co K X (k' + 23))) (K.neg (K.mul (co K X (9)) (co K X (k' + 24))))) := rfl
  have t2 : O.le K.zero (VGoodTail.tt K X (10) (k' + 22)) := by
    rw [show k' + 22 = 10 + (k' + 11) + 1 by lib_nat]
    exact VGoodTail.tail_nn K O X hg _ _
  have et2 : (VGoodTail.tt K X (10) (k' + 22)) = (K.add (K.mul (co K X (10)) (co K X (k' + 21))) (K.neg (K.mul (co K X (9)) (co K X (k' + 22))))) := rfl
  have t3 : O.le K.zero (VGoodTail.tt K X (8) (k' + 24)) := by
    rw [show k' + 24 = 8 + (k' + 15) + 1 by lib_nat]
    exact VGoodTail.tail_nn K O X hg _ _
  have et3 : (VGoodTail.tt K X (8) (k' + 24)) = (K.add (K.mul (co K X (8)) (co K X (k' + 23))) (K.neg (K.mul (co K X (7)) (co K X (k' + 24))))) := rfl
  have t4 : O.le K.zero (VGoodTail.tt K X (8) (k' + 22)) := by
    rw [show k' + 22 = 8 + (k' + 13) + 1 by lib_nat]
    exact VGoodTail.tail_nn K O X hg _ _
  have et4 : (VGoodTail.tt K X (8) (k' + 22)) = (K.add (K.mul (co K X (8)) (co K X (k' + 21))) (K.neg (K.mul (co K X (7)) (co K X (k' + 22))))) := rfl
  have t5 : O.le K.zero (VGoodTail.tt K X (11) (k' + 25)) := by
    rw [show k' + 25 = 11 + (k' + 13) + 1 by lib_nat]
    exact VGoodTail.tail_nn K O X hg _ _
  have et5 : (VGoodTail.tt K X (11) (k' + 25)) = (K.add (K.mul (co K X (11)) (co K X (k' + 24))) (K.neg (K.mul (co K X (10)) (co K X (k' + 25))))) := rfl
  have t6 : O.le K.zero (VGoodTail.tt K X (11) (k' + 23)) := by
    rw [show k' + 23 = 11 + (k' + 11) + 1 by lib_nat]
    exact VGoodTail.tail_nn K O X hg _ _
  have et6 : (VGoodTail.tt K X (11) (k' + 23)) = (K.add (K.mul (co K X (11)) (co K X (k' + 22))) (K.neg (K.mul (co K X (10)) (co K X (k' + 23))))) := rfl
  have t7 : O.le K.zero (VGoodTail.tt K X (11) (k' + 21)) := by
    rw [show k' + 21 = 11 + (k' + 9) + 1 by lib_nat]
    exact VGoodTail.tail_nn K O X hg _ _
  have et7 : (VGoodTail.tt K X (11) (k' + 21)) = (K.add (K.mul (co K X (11)) (co K X (k' + 20))) (K.neg (K.mul (co K X (10)) (co K X (k' + 21))))) := rfl
  have t8 : O.le K.zero (VGoodTail.tt K X (9) (k' + 25)) := by
    rw [show k' + 25 = 9 + (k' + 15) + 1 by lib_nat]
    exact VGoodTail.tail_nn K O X hg _ _
  have et8 : (VGoodTail.tt K X (9) (k' + 25)) = (K.add (K.mul (co K X (9)) (co K X (k' + 24))) (K.neg (K.mul (co K X (8)) (co K X (k' + 25))))) := rfl
  have t9 : O.le K.zero (VGoodTail.tt K X (9) (k' + 21)) := by
    rw [show k' + 21 = 9 + (k' + 11) + 1 by lib_nat]
    exact VGoodTail.tail_nn K O X hg _ _
  have et9 : (VGoodTail.tt K X (9) (k' + 21)) = (K.add (K.mul (co K X (9)) (co K X (k' + 20))) (K.neg (K.mul (co K X (8)) (co K X (k' + 21))))) := rfl
  have t10 : O.le K.zero (VGoodTail.tt K X (7) (k' + 25)) := by
    rw [show k' + 25 = 7 + (k' + 17) + 1 by lib_nat]
    exact VGoodTail.tail_nn K O X hg _ _
  have et10 : (VGoodTail.tt K X (7) (k' + 25)) = (K.add (K.mul (co K X (7)) (co K X (k' + 24))) (K.neg (K.mul (co K X (6)) (co K X (k' + 25))))) := rfl
  have t11 : O.le K.zero (VGoodTail.tt K X (7) (k' + 23)) := by
    rw [show k' + 23 = 7 + (k' + 15) + 1 by lib_nat]
    exact VGoodTail.tail_nn K O X hg _ _
  have et11 : (VGoodTail.tt K X (7) (k' + 23)) = (K.add (K.mul (co K X (7)) (co K X (k' + 22))) (K.neg (K.mul (co K X (6)) (co K X (k' + 23))))) := rfl
  have t12 : O.le K.zero (VGoodTail.tt K X (7) (k' + 21)) := by
    rw [show k' + 21 = 7 + (k' + 13) + 1 by lib_nat]
    exact VGoodTail.tail_nn K O X hg _ _
  have et12 : (VGoodTail.tt K X (7) (k' + 21)) = (K.add (K.mul (co K X (7)) (co K X (k' + 20))) (K.neg (K.mul (co K X (6)) (co K X (k' + 21))))) := rfl
  have t13 : O.le K.zero (VGoodTail.tt K X (12) (k' + 26)) := by
    rw [show k' + 26 = 12 + (k' + 13) + 1 by lib_nat]
    exact VGoodTail.tail_nn K O X hg _ _
  have et13 : (VGoodTail.tt K X (12) (k' + 26)) = (K.add (K.mul (co K X (12)) (co K X (k' + 25))) (K.neg (K.mul (co K X (11)) (co K X (k' + 26))))) := rfl
  have t14 : O.le K.zero (VGoodTail.tt K X (12) (k' + 24)) := by
    rw [show k' + 24 = 12 + (k' + 11) + 1 by lib_nat]
    exact VGoodTail.tail_nn K O X hg _ _
  have et14 : (VGoodTail.tt K X (12) (k' + 24)) = (K.add (K.mul (co K X (12)) (co K X (k' + 23))) (K.neg (K.mul (co K X (11)) (co K X (k' + 24))))) := rfl
  have t15 : O.le K.zero (VGoodTail.tt K X (12) (k' + 22)) := by
    rw [show k' + 22 = 12 + (k' + 9) + 1 by lib_nat]
    exact VGoodTail.tail_nn K O X hg _ _
  have et15 : (VGoodTail.tt K X (12) (k' + 22)) = (K.add (K.mul (co K X (12)) (co K X (k' + 21))) (K.neg (K.mul (co K X (11)) (co K X (k' + 22))))) := rfl
  have t16 : O.le K.zero (VGoodTail.tt K X (12) (k' + 20)) := by
    rw [show k' + 20 = 12 + (k' + 7) + 1 by lib_nat]
    exact VGoodTail.tail_nn K O X hg _ _
  have et16 : (VGoodTail.tt K X (12) (k' + 20)) = (K.add (K.mul (co K X (12)) (co K X (k' + 19))) (K.neg (K.mul (co K X (11)) (co K X (k' + 20))))) := rfl
  have t17 : O.le K.zero (VGoodTail.tt K X (10) (k' + 26)) := by
    rw [show k' + 26 = 10 + (k' + 15) + 1 by lib_nat]
    exact VGoodTail.tail_nn K O X hg _ _
  have et17 : (VGoodTail.tt K X (10) (k' + 26)) = (K.add (K.mul (co K X (10)) (co K X (k' + 25))) (K.neg (K.mul (co K X (9)) (co K X (k' + 26))))) := rfl
  have t18 : O.le K.zero (VGoodTail.tt K X (10) (k' + 20)) := by
    rw [show k' + 20 = 10 + (k' + 9) + 1 by lib_nat]
    exact VGoodTail.tail_nn K O X hg _ _
  have et18 : (VGoodTail.tt K X (10) (k' + 20)) = (K.add (K.mul (co K X (10)) (co K X (k' + 19))) (K.neg (K.mul (co K X (9)) (co K X (k' + 20))))) := rfl
  have t19 : O.le K.zero (VGoodTail.tt K X (8) (k' + 26)) := by
    rw [show k' + 26 = 8 + (k' + 17) + 1 by lib_nat]
    exact VGoodTail.tail_nn K O X hg _ _
  have et19 : (VGoodTail.tt K X (8) (k' + 26)) = (K.add (K.mul (co K X (8)) (co K X (k' + 25))) (K.neg (K.mul (co K X (7)) (co K X (k' + 26))))) := rfl
  have t20 : O.le K.zero (VGoodTail.tt K X (8) (k' + 20)) := by
    rw [show k' + 20 = 8 + (k' + 11) + 1 by lib_nat]
    exact VGoodTail.tail_nn K O X hg _ _
  have et20 : (VGoodTail.tt K X (8) (k' + 20)) = (K.add (K.mul (co K X (8)) (co K X (k' + 19))) (K.neg (K.mul (co K X (7)) (co K X (k' + 20))))) := rfl
  have t21 : O.le K.zero (VGoodTail.tt K X (6) (k' + 26)) := by
    rw [show k' + 26 = 6 + (k' + 19) + 1 by lib_nat]
    exact VGoodTail.tail_nn K O X hg _ _
  have et21 : (VGoodTail.tt K X (6) (k' + 26)) = (K.add (K.mul (co K X (6)) (co K X (k' + 25))) (K.neg (K.mul (co K X (5)) (co K X (k' + 26))))) := rfl
  have t22 : O.le K.zero (VGoodTail.tt K X (6) (k' + 24)) := by
    rw [show k' + 24 = 6 + (k' + 17) + 1 by lib_nat]
    exact VGoodTail.tail_nn K O X hg _ _
  have et22 : (VGoodTail.tt K X (6) (k' + 24)) = (K.add (K.mul (co K X (6)) (co K X (k' + 23))) (K.neg (K.mul (co K X (5)) (co K X (k' + 24))))) := rfl
  have t23 : O.le K.zero (VGoodTail.tt K X (6) (k' + 22)) := by
    rw [show k' + 22 = 6 + (k' + 15) + 1 by lib_nat]
    exact VGoodTail.tail_nn K O X hg _ _
  have et23 : (VGoodTail.tt K X (6) (k' + 22)) = (K.add (K.mul (co K X (6)) (co K X (k' + 21))) (K.neg (K.mul (co K X (5)) (co K X (k' + 22))))) := rfl
  have t24 : O.le K.zero (VGoodTail.tt K X (6) (k' + 20)) := by
    rw [show k' + 20 = 6 + (k' + 13) + 1 by lib_nat]
    exact VGoodTail.tail_nn K O X hg _ _
  have et24 : (VGoodTail.tt K X (6) (k' + 20)) = (K.add (K.mul (co K X (6)) (co K X (k' + 19))) (K.neg (K.mul (co K X (5)) (co K X (k' + 20))))) := rfl
  have t25 : O.le K.zero (VGoodTail.tt K X (13) (k' + 27)) := by
    rw [show k' + 27 = 13 + (k' + 13) + 1 by lib_nat]
    exact VGoodTail.tail_nn K O X hg _ _
  have et25 : (VGoodTail.tt K X (13) (k' + 27)) = (K.add (K.mul (co K X (13)) (co K X (k' + 26))) (K.neg (K.mul (co K X (12)) (co K X (k' + 27))))) := rfl
  have t26 : O.le K.zero (VGoodTail.tt K X (13) (k' + 25)) := by
    rw [show k' + 25 = 13 + (k' + 11) + 1 by lib_nat]
    exact VGoodTail.tail_nn K O X hg _ _
  have et26 : (VGoodTail.tt K X (13) (k' + 25)) = (K.add (K.mul (co K X (13)) (co K X (k' + 24))) (K.neg (K.mul (co K X (12)) (co K X (k' + 25))))) := rfl
  have t27 : O.le K.zero (VGoodTail.tt K X (13) (k' + 23)) := by
    rw [show k' + 23 = 13 + (k' + 9) + 1 by lib_nat]
    exact VGoodTail.tail_nn K O X hg _ _
  have et27 : (VGoodTail.tt K X (13) (k' + 23)) = (K.add (K.mul (co K X (13)) (co K X (k' + 22))) (K.neg (K.mul (co K X (12)) (co K X (k' + 23))))) := rfl
  have t28 : O.le K.zero (VGoodTail.tt K X (13) (k' + 21)) := by
    rw [show k' + 21 = 13 + (k' + 7) + 1 by lib_nat]
    exact VGoodTail.tail_nn K O X hg _ _
  have et28 : (VGoodTail.tt K X (13) (k' + 21)) = (K.add (K.mul (co K X (13)) (co K X (k' + 20))) (K.neg (K.mul (co K X (12)) (co K X (k' + 21))))) := rfl
  have t29 : O.le K.zero (VGoodTail.tt K X (13) (k' + 19)) := by
    rw [show k' + 19 = 13 + (k' + 5) + 1 by lib_nat]
    exact VGoodTail.tail_nn K O X hg _ _
  have et29 : (VGoodTail.tt K X (13) (k' + 19)) = (K.add (K.mul (co K X (13)) (co K X (k' + 18))) (K.neg (K.mul (co K X (12)) (co K X (k' + 19))))) := rfl
  have t30 : O.le K.zero (VGoodTail.tt K X (11) (k' + 27)) := by
    rw [show k' + 27 = 11 + (k' + 15) + 1 by lib_nat]
    exact VGoodTail.tail_nn K O X hg _ _
  have et30 : (VGoodTail.tt K X (11) (k' + 27)) = (K.add (K.mul (co K X (11)) (co K X (k' + 26))) (K.neg (K.mul (co K X (10)) (co K X (k' + 27))))) := rfl
  have t31 : O.le K.zero (VGoodTail.tt K X (11) (k' + 19)) := by
    rw [show k' + 19 = 11 + (k' + 7) + 1 by lib_nat]
    exact VGoodTail.tail_nn K O X hg _ _
  have et31 : (VGoodTail.tt K X (11) (k' + 19)) = (K.add (K.mul (co K X (11)) (co K X (k' + 18))) (K.neg (K.mul (co K X (10)) (co K X (k' + 19))))) := rfl
  have t32 : O.le K.zero (VGoodTail.tt K X (9) (k' + 27)) := by
    rw [show k' + 27 = 9 + (k' + 17) + 1 by lib_nat]
    exact VGoodTail.tail_nn K O X hg _ _
  have et32 : (VGoodTail.tt K X (9) (k' + 27)) = (K.add (K.mul (co K X (9)) (co K X (k' + 26))) (K.neg (K.mul (co K X (8)) (co K X (k' + 27))))) := rfl
  have t33 : O.le K.zero (VGoodTail.tt K X (9) (k' + 19)) := by
    rw [show k' + 19 = 9 + (k' + 9) + 1 by lib_nat]
    exact VGoodTail.tail_nn K O X hg _ _
  have et33 : (VGoodTail.tt K X (9) (k' + 19)) = (K.add (K.mul (co K X (9)) (co K X (k' + 18))) (K.neg (K.mul (co K X (8)) (co K X (k' + 19))))) := rfl
  have t34 : O.le K.zero (VGoodTail.tt K X (7) (k' + 27)) := by
    rw [show k' + 27 = 7 + (k' + 19) + 1 by lib_nat]
    exact VGoodTail.tail_nn K O X hg _ _
  have et34 : (VGoodTail.tt K X (7) (k' + 27)) = (K.add (K.mul (co K X (7)) (co K X (k' + 26))) (K.neg (K.mul (co K X (6)) (co K X (k' + 27))))) := rfl
  have t35 : O.le K.zero (VGoodTail.tt K X (7) (k' + 19)) := by
    rw [show k' + 19 = 7 + (k' + 11) + 1 by lib_nat]
    exact VGoodTail.tail_nn K O X hg _ _
  have et35 : (VGoodTail.tt K X (7) (k' + 19)) = (K.add (K.mul (co K X (7)) (co K X (k' + 18))) (K.neg (K.mul (co K X (6)) (co K X (k' + 19))))) := rfl
  have hp : 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.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.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (VGoodTail.tt K X (9) (k' + 23))   -- (+1694 chars; full line in the Zenodo source)
    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 _ _ (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 _ _ (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   -- (+915 chars; full line in the Zenodo source)
  refine O.le_trans _ _ _ hp (TripleE.le_of_eq' K O _ _ ?_)
  rw [kv, et0, et1, et2, et3, et4, et5, et6, et7, et8, et9, et10, et11, et12, et13, et14, et15, et16, et17, et18, et19, et20, et21, et22, et23, et24, et25, et26, et27, et28, et29, et30, et31, et32, et33, et34, et35]
  ring_z

theorem kna_s_s (X : List R) (hg : L1Full.Good K O X) (u' k' : Nat) :
    O.le K.zero (CBLib.kern K (Fa K X) (Ga K X) 5 (u' + 4) (u' + k' + 18)) := by
  have kv : CBLib.kern K (Fa K X) (Ga K X) 5 (u' + 4) (u' + k' + 18) = (K.add (K.add (K.add (K.add (K.add K.zero (K.add (K.mul (co K X (u' + 10)) (co K X (u' + k' + 23))) (K.neg (K.mul (co K X (u' + k' + 24)) (co K X (u' + 9)))))) (K.add (K.mul (K.add (co K X (u' + 11)) (co K X (u' + 9))) (K.add (co K X (u' + k' + 24)) (co K X (u' + k' + 22)))) (K.neg (K.mul (K.add (co K X (u' + k' + 25)) (co K X   -- (+1479 chars; full line in the Zenodo source)
  have t0 : O.le K.zero (VGoodTail.tt K X (u' + 10) (u' + k' + 24)) := by
    rw [show u' + k' + 24 = u' + 10 + (k' + 13) + 1 by lib_nat]
    exact VGoodTail.tail_nn K O X hg _ _
  have et0 : (VGoodTail.tt K X (u' + 10) (u' + k' + 24)) = (K.add (K.mul (co K X (u' + 10)) (co K X (u' + k' + 23))) (K.neg (K.mul (co K X (u' + 9)) (co K X (u' + k' + 24))))) := rfl
  have t1 : O.le K.zero (VGoodTail.tt K X (u' + 11) (u' + k' + 25)) := by
    rw [show u' + k' + 25 = u' + 11 + (k' + 13) + 1 by lib_nat]
    exact VGoodTail.tail_nn K O X hg _ _
  have et1 : (VGoodTail.tt K X (u' + 11) (u' + k' + 25)) = (K.add (K.mul (co K X (u' + 11)) (co K X (u' + k' + 24))) (K.neg (K.mul (co K X (u' + 10)) (co K X (u' + k' + 25))))) := rfl
  have t2 : O.le K.zero (VGoodTail.tt K X (u' + 11) (u' + k' + 23)) := by
    rw [show u' + k' + 23 = u' + 11 + (k' + 11) + 1 by lib_nat]
    exact VGoodTail.tail_nn K O X hg _ _
  have et2 : (VGoodTail.tt K X (u' + 11) (u' + k' + 23)) = (K.add (K.mul (co K X (u' + 11)) (co K X (u' + k' + 22))) (K.neg (K.mul (co K X (u' + 10)) (co K X (u' + k' + 23))))) := rfl
  have t3 : O.le K.zero (VGoodTail.tt K X (u' + 9) (u' + k' + 25)) := by
    rw [show u' + k' + 25 = u' + 9 + (k' + 15) + 1 by lib_nat]
    exact VGoodTail.tail_nn K O X hg _ _
  have et3 : (VGoodTail.tt K X (u' + 9) (u' + k' + 25)) = (K.add (K.mul (co K X (u' + 9)) (co K X (u' + k' + 24))) (K.neg (K.mul (co K X (u' + 8)) (co K X (u' + k' + 25))))) := rfl
  have t4 : O.le K.zero (VGoodTail.tt K X (u' + 9) (u' + k' + 23)) := by
    rw [show u' + k' + 23 = u' + 9 + (k' + 13) + 1 by lib_nat]
    exact VGoodTail.tail_nn K O X hg _ _
  have et4 : (VGoodTail.tt K X (u' + 9) (u' + k' + 23)) = (K.add (K.mul (co K X (u' + 9)) (co K X (u' + k' + 22))) (K.neg (K.mul (co K X (u' + 8)) (co K X (u' + k' + 23))))) := rfl
  have t5 : O.le K.zero (VGoodTail.tt K X (u' + 12) (u' + k' + 26)) := by
    rw [show u' + k' + 26 = u' + 12 + (k' + 13) + 1 by lib_nat]
    exact VGoodTail.tail_nn K O X hg _ _
  have et5 : (VGoodTail.tt K X (u' + 12) (u' + k' + 26)) = (K.add (K.mul (co K X (u' + 12)) (co K X (u' + k' + 25))) (K.neg (K.mul (co K X (u' + 11)) (co K X (u' + k' + 26))))) := rfl
  have t6 : O.le K.zero (VGoodTail.tt K X (u' + 12) (u' + k' + 24)) := by
    rw [show u' + k' + 24 = u' + 12 + (k' + 11) + 1 by lib_nat]
    exact VGoodTail.tail_nn K O X hg _ _
  have et6 : (VGoodTail.tt K X (u' + 12) (u' + k' + 24)) = (K.add (K.mul (co K X (u' + 12)) (co K X (u' + k' + 23))) (K.neg (K.mul (co K X (u' + 11)) (co K X (u' + k' + 24))))) := rfl
  have t7 : O.le K.zero (VGoodTail.tt K X (u' + 12) (u' + k' + 22)) := by
    rw [show u' + k' + 22 = u' + 12 + (k' + 9) + 1 by lib_nat]
    exact VGoodTail.tail_nn K O X hg _ _
  have et7 : (VGoodTail.tt K X (u' + 12) (u' + k' + 22)) = (K.add (K.mul (co K X (u' + 12)) (co K X (u' + k' + 21))) (K.neg (K.mul (co K X (u' + 11)) (co K X (u' + k' + 22))))) := rfl
  have t8 : O.le K.zero (VGoodTail.tt K X (u' + 10) (u' + k' + 26)) := by
    rw [show u' + k' + 26 = u' + 10 + (k' + 15) + 1 by lib_nat]
    exact VGoodTail.tail_nn K O X hg _ _
  have et8 : (VGoodTail.tt K X (u' + 10) (u' + k' + 26)) = (K.add (K.mul (co K X (u' + 10)) (co K X (u' + k' + 25))) (K.neg (K.mul (co K X (u' + 9)) (co K X (u' + k' + 26))))) := rfl
  have t9 : O.le K.zero (VGoodTail.tt K X (u' + 10) (u' + k' + 22)) := by
    rw [show u' + k' + 22 = u' + 10 + (k' + 11) + 1 by lib_nat]
    exact VGoodTail.tail_nn K O X hg _ _
  have et9 : (VGoodTail.tt K X (u' + 10) (u' + k' + 22)) = (K.add (K.mul (co K X (u' + 10)) (co K X (u' + k' + 21))) (K.neg (K.mul (co K X (u' + 9)) (co K X (u' + k' + 22))))) := rfl
  have t10 : O.le K.zero (VGoodTail.tt K X (u' + 8) (u' + k' + 26)) := by
    rw [show u' + k' + 26 = u' + 8 + (k' + 17) + 1 by lib_nat]
    exact VGoodTail.tail_nn K O X hg _ _
  have et10 : (VGoodTail.tt K X (u' + 8) (u' + k' + 26)) = (K.add (K.mul (co K X (u' + 8)) (co K X (u' + k' + 25))) (K.neg (K.mul (co K X (u' + 7)) (co K X (u' + k' + 26))))) := rfl
  have t11 : O.le K.zero (VGoodTail.tt K X (u' + 8) (u' + k' + 24)) := by
    rw [show u' + k' + 24 = u' + 8 + (k' + 15) + 1 by lib_nat]
    exact VGoodTail.tail_nn K O X hg _ _
  have et11 : (VGoodTail.tt K X (u' + 8) (u' + k' + 24)) = (K.add (K.mul (co K X (u' + 8)) (co K X (u' + k' + 23))) (K.neg (K.mul (co K X (u' + 7)) (co K X (u' + k' + 24))))) := rfl
  have t12 : O.le K.zero (VGoodTail.tt K X (u' + 8) (u' + k' + 22)) := by
    rw [show u' + k' + 22 = u' + 8 + (k' + 13) + 1 by lib_nat]
    exact VGoodTail.tail_nn K O X hg _ _
  have et12 : (VGoodTail.tt K X (u' + 8) (u' + k' + 22)) = (K.add (K.mul (co K X (u' + 8)) (co K X (u' + k' + 21))) (K.neg (K.mul (co K X (u' + 7)) (co K X (u' + k' + 22))))) := rfl
  have t13 : O.le K.zero (VGoodTail.tt K X (u' + 13) (u' + k' + 27)) := by
    rw [show u' + k' + 27 = u' + 13 + (k' + 13) + 1 by lib_nat]
    exact VGoodTail.tail_nn K O X hg _ _
  have et13 : (VGoodTail.tt K X (u' + 13) (u' + k' + 27)) = (K.add (K.mul (co K X (u' + 13)) (co K X (u' + k' + 26))) (K.neg (K.mul (co K X (u' + 12)) (co K X (u' + k' + 27))))) := rfl
  have t14 : O.le K.zero (VGoodTail.tt K X (u' + 13) (u' + k' + 25)) := by
    rw [show u' + k' + 25 = u' + 13 + (k' + 11) + 1 by lib_nat]
    exact VGoodTail.tail_nn K O X hg _ _
  have et14 : (VGoodTail.tt K X (u' + 13) (u' + k' + 25)) = (K.add (K.mul (co K X (u' + 13)) (co K X (u' + k' + 24))) (K.neg (K.mul (co K X (u' + 12)) (co K X (u' + k' + 25))))) := rfl
  have t15 : O.le K.zero (VGoodTail.tt K X (u' + 13) (u' + k' + 23)) := by
    rw [show u' + k' + 23 = u' + 13 + (k' + 9) + 1 by lib_nat]
    exact VGoodTail.tail_nn K O X hg _ _
  have et15 : (VGoodTail.tt K X (u' + 13) (u' + k' + 23)) = (K.add (K.mul (co K X (u' + 13)) (co K X (u' + k' + 22))) (K.neg (K.mul (co K X (u' + 12)) (co K X (u' + k' + 23))))) := rfl
  have t16 : O.le K.zero (VGoodTail.tt K X (u' + 13) (u' + k' + 21)) := by
    rw [show u' + k' + 21 = u' + 13 + (k' + 7) + 1 by lib_nat]
    exact VGoodTail.tail_nn K O X hg _ _
  have et16 : (VGoodTail.tt K X (u' + 13) (u' + k' + 21)) = (K.add (K.mul (co K X (u' + 13)) (co K X (u' + k' + 20))) (K.neg (K.mul (co K X (u' + 12)) (co K X (u' + k' + 21))))) := rfl
  have t17 : O.le K.zero (VGoodTail.tt K X (u' + 11) (u' + k' + 27)) := by
    rw [show u' + k' + 27 = u' + 11 + (k' + 15) + 1 by lib_nat]
    exact VGoodTail.tail_nn K O X hg _ _
  have et17 : (VGoodTail.tt K X (u' + 11) (u' + k' + 27)) = (K.add (K.mul (co K X (u' + 11)) (co K X (u' + k' + 26))) (K.neg (K.mul (co K X (u' + 10)) (co K X (u' + k' + 27))))) := rfl
  have t18 : O.le K.zero (VGoodTail.tt K X (u' + 11) (u' + k' + 21)) := by
    rw [show u' + k' + 21 = u' + 11 + (k' + 9) + 1 by lib_nat]
    exact VGoodTail.tail_nn K O X hg _ _
  have et18 : (VGoodTail.tt K X (u' + 11) (u' + k' + 21)) = (K.add (K.mul (co K X (u' + 11)) (co K X (u' + k' + 20))) (K.neg (K.mul (co K X (u' + 10)) (co K X (u' + k' + 21))))) := rfl
  have t19 : O.le K.zero (VGoodTail.tt K X (u' + 9) (u' + k' + 27)) := by
    rw [show u' + k' + 27 = u' + 9 + (k' + 17) + 1 by lib_nat]
    exact VGoodTail.tail_nn K O X hg _ _
  have et19 : (VGoodTail.tt K X (u' + 9) (u' + k' + 27)) = (K.add (K.mul (co K X (u' + 9)) (co K X (u' + k' + 26))) (K.neg (K.mul (co K X (u' + 8)) (co K X (u' + k' + 27))))) := rfl
  have t20 : O.le K.zero (VGoodTail.tt K X (u' + 9) (u' + k' + 21)) := by
    rw [show u' + k' + 21 = u' + 9 + (k' + 11) + 1 by lib_nat]
    exact VGoodTail.tail_nn K O X hg _ _
  have et20 : (VGoodTail.tt K X (u' + 9) (u' + k' + 21)) = (K.add (K.mul (co K X (u' + 9)) (co K X (u' + k' + 20))) (K.neg (K.mul (co K X (u' + 8)) (co K X (u' + k' + 21))))) := rfl
  have t21 : O.le K.zero (VGoodTail.tt K X (u' + 7) (u' + k' + 27)) := by
    rw [show u' + k' + 27 = u' + 7 + (k' + 19) + 1 by lib_nat]
    exact VGoodTail.tail_nn K O X hg _ _
  have et21 : (VGoodTail.tt K X (u' + 7) (u' + k' + 27)) = (K.add (K.mul (co K X (u' + 7)) (co K X (u' + k' + 26))) (K.neg (K.mul (co K X (u' + 6)) (co K X (u' + k' + 27))))) := rfl
  have t22 : O.le K.zero (VGoodTail.tt K X (u' + 7) (u' + k' + 25)) := by
    rw [show u' + k' + 25 = u' + 7 + (k' + 17) + 1 by lib_nat]
    exact VGoodTail.tail_nn K O X hg _ _
  have et22 : (VGoodTail.tt K X (u' + 7) (u' + k' + 25)) = (K.add (K.mul (co K X (u' + 7)) (co K X (u' + k' + 24))) (K.neg (K.mul (co K X (u' + 6)) (co K X (u' + k' + 25))))) := rfl
  have t23 : O.le K.zero (VGoodTail.tt K X (u' + 7) (u' + k' + 23)) := by
    rw [show u' + k' + 23 = u' + 7 + (k' + 15) + 1 by lib_nat]
    exact VGoodTail.tail_nn K O X hg _ _
  have et23 : (VGoodTail.tt K X (u' + 7) (u' + k' + 23)) = (K.add (K.mul (co K X (u' + 7)) (co K X (u' + k' + 22))) (K.neg (K.mul (co K X (u' + 6)) (co K X (u' + k' + 23))))) := rfl
  have t24 : O.le K.zero (VGoodTail.tt K X (u' + 7) (u' + k' + 21)) := by
    rw [show u' + k' + 21 = u' + 7 + (k' + 13) + 1 by lib_nat]
    exact VGoodTail.tail_nn K O X hg _ _
  have et24 : (VGoodTail.tt K X (u' + 7) (u' + k' + 21)) = (K.add (K.mul (co K X (u' + 7)) (co K X (u' + k' + 20))) (K.neg (K.mul (co K X (u' + 6)) (co K X (u' + k' + 21))))) := rfl
  have t25 : O.le K.zero (VGoodTail.tt K X (u' + 14) (u' + k' + 28)) := by
    rw [show u' + k' + 28 = u' + 14 + (k' + 13) + 1 by lib_nat]
    exact VGoodTail.tail_nn K O X hg _ _
  have et25 : (VGoodTail.tt K X (u' + 14) (u' + k' + 28)) = (K.add (K.mul (co K X (u' + 14)) (co K X (u' + k' + 27))) (K.neg (K.mul (co K X (u' + 13)) (co K X (u' + k' + 28))))) := rfl
  have t26 : O.le K.zero (VGoodTail.tt K X (u' + 14) (u' + k' + 26)) := by
    rw [show u' + k' + 26 = u' + 14 + (k' + 11) + 1 by lib_nat]
    exact VGoodTail.tail_nn K O X hg _ _
  have et26 : (VGoodTail.tt K X (u' + 14) (u' + k' + 26)) = (K.add (K.mul (co K X (u' + 14)) (co K X (u' + k' + 25))) (K.neg (K.mul (co K X (u' + 13)) (co K X (u' + k' + 26))))) := rfl
  have t27 : O.le K.zero (VGoodTail.tt K X (u' + 14) (u' + k' + 24)) := by
    rw [show u' + k' + 24 = u' + 14 + (k' + 9) + 1 by lib_nat]
    exact VGoodTail.tail_nn K O X hg _ _
  have et27 : (VGoodTail.tt K X (u' + 14) (u' + k' + 24)) = (K.add (K.mul (co K X (u' + 14)) (co K X (u' + k' + 23))) (K.neg (K.mul (co K X (u' + 13)) (co K X (u' + k' + 24))))) := rfl
  have t28 : O.le K.zero (VGoodTail.tt K X (u' + 14) (u' + k' + 22)) := by
    rw [show u' + k' + 22 = u' + 14 + (k' + 7) + 1 by lib_nat]
    exact VGoodTail.tail_nn K O X hg _ _
  have et28 : (VGoodTail.tt K X (u' + 14) (u' + k' + 22)) = (K.add (K.mul (co K X (u' + 14)) (co K X (u' + k' + 21))) (K.neg (K.mul (co K X (u' + 13)) (co K X (u' + k' + 22))))) := rfl
  have t29 : O.le K.zero (VGoodTail.tt K X (u' + 14) (u' + k' + 20)) := by
    rw [show u' + k' + 20 = u' + 14 + (k' + 5) + 1 by lib_nat]
    exact VGoodTail.tail_nn K O X hg _ _
  have et29 : (VGoodTail.tt K X (u' + 14) (u' + k' + 20)) = (K.add (K.mul (co K X (u' + 14)) (co K X (u' + k' + 19))) (K.neg (K.mul (co K X (u' + 13)) (co K X (u' + k' + 20))))) := rfl
  have t30 : O.le K.zero (VGoodTail.tt K X (u' + 12) (u' + k' + 28)) := by
    rw [show u' + k' + 28 = u' + 12 + (k' + 15) + 1 by lib_nat]
    exact VGoodTail.tail_nn K O X hg _ _
  have et30 : (VGoodTail.tt K X (u' + 12) (u' + k' + 28)) = (K.add (K.mul (co K X (u' + 12)) (co K X (u' + k' + 27))) (K.neg (K.mul (co K X (u' + 11)) (co K X (u' + k' + 28))))) := rfl
  have t31 : O.le K.zero (VGoodTail.tt K X (u' + 12) (u' + k' + 20)) := by
    rw [show u' + k' + 20 = u' + 12 + (k' + 7) + 1 by lib_nat]
    exact VGoodTail.tail_nn K O X hg _ _
  have et31 : (VGoodTail.tt K X (u' + 12) (u' + k' + 20)) = (K.add (K.mul (co K X (u' + 12)) (co K X (u' + k' + 19))) (K.neg (K.mul (co K X (u' + 11)) (co K X (u' + k' + 20))))) := rfl
  have t32 : O.le K.zero (VGoodTail.tt K X (u' + 10) (u' + k' + 28)) := by
    rw [show u' + k' + 28 = u' + 10 + (k' + 17) + 1 by lib_nat]
    exact VGoodTail.tail_nn K O X hg _ _
  have et32 : (VGoodTail.tt K X (u' + 10) (u' + k' + 28)) = (K.add (K.mul (co K X (u' + 10)) (co K X (u' + k' + 27))) (K.neg (K.mul (co K X (u' + 9)) (co K X (u' + k' + 28))))) := rfl
  have t33 : O.le K.zero (VGoodTail.tt K X (u' + 10) (u' + k' + 20)) := by
    rw [show u' + k' + 20 = u' + 10 + (k' + 9) + 1 by lib_nat]
    exact VGoodTail.tail_nn K O X hg _ _
  have et33 : (VGoodTail.tt K X (u' + 10) (u' + k' + 20)) = (K.add (K.mul (co K X (u' + 10)) (co K X (u' + k' + 19))) (K.neg (K.mul (co K X (u' + 9)) (co K X (u' + k' + 20))))) := rfl
  have t34 : O.le K.zero (VGoodTail.tt K X (u' + 8) (u' + k' + 28)) := by
    rw [show u' + k' + 28 = u' + 8 + (k' + 19) + 1 by lib_nat]
    exact VGoodTail.tail_nn K O X hg _ _
  have et34 : (VGoodTail.tt K X (u' + 8) (u' + k' + 28)) = (K.add (K.mul (co K X (u' + 8)) (co K X (u' + k' + 27))) (K.neg (K.mul (co K X (u' + 7)) (co K X (u' + k' + 28))))) := rfl
  have t35 : O.le K.zero (VGoodTail.tt K X (u' + 8) (u' + k' + 20)) := by
    rw [show u' + k' + 20 = u' + 8 + (k' + 11) + 1 by lib_nat]
    exact VGoodTail.tail_nn K O X hg _ _
  have et35 : (VGoodTail.tt K X (u' + 8) (u' + k' + 20)) = (K.add (K.mul (co K X (u' + 8)) (co K X (u' + k' + 19))) (K.neg (K.mul (co K X (u' + 7)) (co K X (u' + k' + 20))))) := rfl
  have t36 : O.le K.zero (VGoodTail.tt K X (u' + 6) (u' + k' + 28)) := by
    rw [show u' + k' + 28 = u' + 6 + (k' + 21) + 1 by lib_nat]
    exact VGoodTail.tail_nn K O X hg _ _
  have et36 : (VGoodTail.tt K X (u' + 6) (u' + k' + 28)) = (K.add (K.mul (co K X (u' + 6)) (co K X (u' + k' + 27))) (K.neg (K.mul (co K X (u' + 5)) (co K X (u' + k' + 28))))) := rfl
  have t37 : O.le K.zero (VGoodTail.tt K X (u' + 6) (u' + k' + 26)) := by
    rw [show u' + k' + 26 = u' + 6 + (k' + 19) + 1 by lib_nat]
    exact VGoodTail.tail_nn K O X hg _ _
  have et37 : (VGoodTail.tt K X (u' + 6) (u' + k' + 26)) = (K.add (K.mul (co K X (u' + 6)) (co K X (u' + k' + 25))) (K.neg (K.mul (co K X (u' + 5)) (co K X (u' + k' + 26))))) := rfl
  have t38 : O.le K.zero (VGoodTail.tt K X (u' + 6) (u' + k' + 24)) := by
    rw [show u' + k' + 24 = u' + 6 + (k' + 17) + 1 by lib_nat]
    exact VGoodTail.tail_nn K O X hg _ _
  have et38 : (VGoodTail.tt K X (u' + 6) (u' + k' + 24)) = (K.add (K.mul (co K X (u' + 6)) (co K X (u' + k' + 23))) (K.neg (K.mul (co K X (u' + 5)) (co K X (u' + k' + 24))))) := rfl
  have t39 : O.le K.zero (VGoodTail.tt K X (u' + 6) (u' + k' + 22)) := by
    rw [show u' + k' + 22 = u' + 6 + (k' + 15) + 1 by lib_nat]
    exact VGoodTail.tail_nn K O X hg _ _
  have et39 : (VGoodTail.tt K X (u' + 6) (u' + k' + 22)) = (K.add (K.mul (co K X (u' + 6)) (co K X (u' + k' + 21))) (K.neg (K.mul (co K X (u' + 5)) (co K X (u' + k' + 22))))) := rfl
  have t40 : O.le K.zero (VGoodTail.tt K X (u' + 6) (u' + k' + 20)) := by
    rw [show u' + k' + 20 = u' + 6 + (k' + 13) + 1 by lib_nat]
    exact VGoodTail.tail_nn K O X hg _ _
  have et40 : (VGoodTail.tt K X (u' + 6) (u' + k' + 20)) = (K.add (K.mul (co K X (u' + 6)) (co K X (u' + k' + 19))) (K.neg (K.mul (co K X (u' + 5)) (co K X (u' + k' + 20))))) := rfl
  have hp : 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.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.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.ad  -- (+2458 chars; full line in the Zenodo source)
    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 _ _ (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 _ _ (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   -- (+1050 chars; full line in the Zenodo source)
  refine O.le_trans _ _ _ hp (TripleE.le_of_eq' K O _ _ ?_)
  rw [kv, et0, et1, et2, et3, et4, et5, et6, et7, et8, et9, et10, et11, et12, et13, et14, et15, et16, et17, et18, et19, et20, et21, et22, et23, et24, et25, et26, et27, et28, et29, et30, et31, et32, et33, et34, et35, et36, et37, et38, et39, et40]
  ring_z

theorem knna (X : List R) (hg : L1Full.Good K O X) : ∀ (u k : Nat),
    O.le K.zero (CBLib.kern K (Fa K X) (Ga K X) 5 u (u + k + 1))
  | 0, 0 => kna_0_0 K O X hg
  | 0, 1 => kna_0_1 K O X hg
  | 0, 2 => kna_0_2 K O X hg
  | 0, 3 => kna_0_3 K O X hg
  | 0, 4 => kna_0_4 K O X hg
  | 0, 5 => kna_0_5 K O X hg
  | 0, 6 => kna_0_6 K O X hg
  | 0, 7 => kna_0_7 K O X hg
  | 0, 8 => kna_0_8 K O X hg
  | 0, 9 => kna_0_9 K O X hg
  | 0, 10 => kna_0_10 K O X hg
  | 0, 11 => kna_0_11 K O X hg
  | 0, 12 => kna_0_12 K O X hg
  | 0, k' + 13 => by
    rw [show 0 + (k' + 13) + 1 = k' + 14 by lib_nat]
    exact kna_0_s K O X hg k'
  | 1, 0 => kna_1_0 K O X hg
  | 1, 1 => kna_1_1 K O X hg
  | 1, 2 => kna_1_2 K O X hg
  | 1, 3 => kna_1_3 K O X hg
  | 1, 4 => kna_1_4 K O X hg
  | 1, 5 => kna_1_5 K O X hg
  | 1, 6 => kna_1_6 K O X hg
  | 1, 7 => kna_1_7 K O X hg
  | 1, 8 => kna_1_8 K O X hg
  | 1, 9 => kna_1_9 K O X hg
  | 1, 10 => kna_1_10 K O X hg
  | 1, 11 => kna_1_11 K O X hg
  | 1, 12 => kna_1_12 K O X hg
  | 1, k' + 13 => by
    rw [show 1 + (k' + 13) + 1 = k' + 15 by lib_nat]
    exact kna_1_s K O X hg k'
  | 2, 0 => kna_2_0 K O X hg
  | 2, 1 => kna_2_1 K O X hg
  | 2, 2 => kna_2_2 K O X hg
  | 2, 3 => kna_2_3 K O X hg
  | 2, 4 => kna_2_4 K O X hg
  | 2, 5 => kna_2_5 K O X hg
  | 2, 6 => kna_2_6 K O X hg
  | 2, 7 => kna_2_7 K O X hg
  | 2, 8 => kna_2_8 K O X hg
  | 2, 9 => kna_2_9 K O X hg
  | 2, 10 => kna_2_10 K O X hg
  | 2, 11 => kna_2_11 K O X hg
  | 2, 12 => kna_2_12 K O X hg
  | 2, k' + 13 => by
    rw [show 2 + (k' + 13) + 1 = k' + 16 by lib_nat]
    exact kna_2_s K O X hg k'
  | 3, 0 => kna_3_0 K O X hg
  | 3, 1 => kna_3_1 K O X hg
  | 3, 2 => kna_3_2 K O X hg
  | 3, 3 => kna_3_3 K O X hg
  | 3, 4 => kna_3_4 K O X hg
  | 3, 5 => kna_3_5 K O X hg
  | 3, 6 => kna_3_6 K O X hg
  | 3, 7 => kna_3_7 K O X hg
  | 3, 8 => kna_3_8 K O X hg
  | 3, 9 => kna_3_9 K O X hg
  | 3, 10 => kna_3_10 K O X hg
  | 3, 11 => kna_3_11 K O X hg
  | 3, 12 => kna_3_12 K O X hg
  | 3, k' + 13 => by
    rw [show 3 + (k' + 13) + 1 = k' + 17 by lib_nat]
    exact kna_3_s K O X hg k'
  | u' + 4, 0 => kna_s_0 K O X hg u'
  | u' + 4, 1 => kna_s_1 K O X hg u'
  | u' + 4, 2 => kna_s_2 K O X hg u'
  | u' + 4, 3 => kna_s_3 K O X hg u'
  | u' + 4, 4 => kna_s_4 K O X hg u'
  | u' + 4, 5 => kna_s_5 K O X hg u'
  | u' + 4, 6 => kna_s_6 K O X hg u'
  | u' + 4, 7 => kna_s_7 K O X hg u'
  | u' + 4, 8 => kna_s_8 K O X hg u'
  | u' + 4, 9 => kna_s_9 K O X hg u'
  | u' + 4, 10 => kna_s_10 K O X hg u'
  | u' + 4, 11 => kna_s_11 K O X hg u'
  | u' + 4, 12 => kna_s_12 K O X hg u'
  | u' + 4, k' + 13 => by
    rw [show u' + 4 + (k' + 13) + 1 = u' + k' + 18 by lib_nat]
    exact kna_s_s K O X hg u' k'

theorem cbnna (X : List R) (hg : L1Full.Good K O X) (NN : Nat) :
    O.le K.zero (CBLib.cbv K (co K X) (VGoodPair.yv K X) (Fa K X) (Ga K X) NN 5) := by
  apply CBLib.cb_multi_nn K O
  intro i k hik
  obtain ⟨m, hm⟩ := CBM2.lt_dest i k hik
  subst hm
  exact O.mul_nonneg _ _ (VGoodTail.pair_all K O X hg i m) (knna K O X hg i m)

theorem cbm10 (X : List R) (hg : L1Full.Good K O X) :
    O.le (K.mul (ReduceCBM.Mt K 5 0 X) (ReduceCBM.Mt K 5 0 X))
      (LibRing.sub K (K.mul (cgc K X X 10) (cgc K X X 10)) (K.mul (cgc K X X 9) (cgc K X X 11))) := by
  have pa := cbnna K O X hg (X.length + 2 + 4)
  have cva : CBLib.cbv K (co K X) (VGoodPair.yv K X) (Fa K X) (Ga K X) (X.length + 2 + 4) 5 = (K.add (K.add (K.add (K.add (K.add K.zero (K.add (K.mul (CauchyBinet2.ip K (co K X) (wa10 K X) (X.length + 2 + 4)) (CauchyBinet2.ip K (VGoodPair.yv K X) (wa11 K X) (X.length + 2 + 4))) (K.neg (K.mul (CauchyBinet2.ip K (co K X) (wa11 K X) (X.length + 2 + 4)) (CauchyBinet2.ip K (VGoodPair.yv K X) (wa10 K X)  -- (+1187 chars; full line in the Zenodo source)
  have ipa10x : CauchyBinet2.ip K (co K X) (wa10 K X) (X.length + 2 + 4) = K.add (K.add (K.add (K.add (K.add K.zero (K.mul (co K X (0)) (co K X (6)))) (K.mul (co K X (1)) (co K X (7)))) (K.mul (co K X (2)) (co K X (8)))) (K.mul (co K X (3)) (co K X (9)))) (K.add (ReduceCBM.Mt K 4 6 X) K.zero) := by
    have h1 := CBLib.sumN_peel K (fun i => K.mul ((co K X) i) ((wa10 K X) i)) 4 (X.length + 2)
    have hb : sumN K (fun i => K.mul ((co K X) i) ((wa10 K X) i)) 4 = (K.add (K.add (K.add (K.add K.zero (K.mul (co K X (0)) (co K X (6)))) (K.mul (co K X (1)) (co K X (7)))) (K.mul (co K X (2)) (co K X (8)))) (K.mul (co K X (3)) (co K X (9)))) := rfl
    rw [hb] at h1
    have ht : sumN K (fun i => K.mul ((co K X) (i + 4)) ((wa10 K X) (i + 4))) (X.length + 2) = (K.add (ReduceCBM.Mt K 4 6 X) K.zero) := by
      rw [sumN_ext K _ (CBLib.lc K X [(4, 6)]) (fun i => by
        show (K.mul (co K X (i + 4)) (co K X (i + 10))) = (K.add (K.mul (co K X (i + 4)) (co K X (i + 10))) K.zero)
        ring_z)]
      exact CBLib.lc_sum K X _ (Nat.le_add_right _ 2) [(4, 6)]
    exact h1.trans (congrArg (K.add (K.add (K.add (K.add (K.add K.zero (K.mul (co K X (0)) (co K X (6)))) (K.mul (co K X (1)) (co K X (7)))) (K.mul (co K X (2)) (co K X (8)))) (K.mul (co K X (3)) (co K X (9))))) ht)
  have ipa10y : CauchyBinet2.ip K (VGoodPair.yv K X) (wa10 K X) (X.length + 2 + 4) = K.add (K.add (K.add (K.add (K.add K.zero (K.mul (co K X (1)) (co K X (6)))) (K.mul (K.add (co K X (0)) (co K X (2))) (co K X (7)))) (K.mul (K.add (co K X (1)) (co K X (3))) (co K X (8)))) (K.mul (K.add (co K X (2)) (co K X (4))) (co K X (9)))) (K.add (ReduceCBM.Mt K 3 7 X) (K.add (ReduceCBM.Mt K 5 5 X) K.zero)) :=  -- (+3 chars; full line in the Zenodo source)
    have h1 := CBLib.sumN_peel K (fun i => K.mul ((VGoodPair.yv K X) i) ((wa10 K X) i)) 4 (X.length + 2)
    have hb : sumN K (fun i => K.mul ((VGoodPair.yv K X) i) ((wa10 K X) i)) 4 = (K.add (K.add (K.add (K.add K.zero (K.mul (co K X (1)) (co K X (6)))) (K.mul (K.add (co K X (0)) (co K X (2))) (co K X (7)))) (K.mul (K.add (co K X (1)) (co K X (3))) (co K X (8)))) (K.mul (K.add (co K X (2)) (co K X (4))) (co K X (9)))) := rfl
    rw [hb] at h1
    have ht : sumN K (fun i => K.mul ((VGoodPair.yv K X) (i + 4)) ((wa10 K X) (i + 4))) (X.length + 2) = (K.add (ReduceCBM.Mt K 3 7 X) (K.add (ReduceCBM.Mt K 5 5 X) K.zero)) := by
      rw [sumN_ext K _ (CBLib.lc K X [(3, 7), (5, 5)]) (fun i => by
        show (K.mul (K.add (co K X (i + 3)) (co K X (i + 5))) (co K X (i + 10))) = (K.add (K.mul (co K X (i + 3)) (co K X (i + 10))) (K.add (K.mul (co K X (i + 5)) (co K X (i + 10))) K.zero))
        ring_z)]
      exact CBLib.lc_sum K X _ (Nat.le_add_right _ 2) [(3, 7), (5, 5)]
    exact h1.trans (congrArg (K.add (K.add (K.add (K.add (K.add K.zero (K.mul (co K X (1)) (co K X (6)))) (K.mul (K.add (co K X (0)) (co K X (2))) (co K X (7)))) (K.mul (K.add (co K X (1)) (co K X (3))) (co K X (8)))) (K.mul (K.add (co K X (2)) (co K X (4))) (co K X (9))))) ht)
  have ipa11x : CauchyBinet2.ip K (co K X) (wa11 K X) (X.length + 2 + 4) = K.add (K.add (K.add (K.add (K.add K.zero (K.mul (co K X (0)) (co K X (5)))) (K.mul (co K X (1)) (co K X (6)))) (K.mul (co K X (2)) (co K X (7)))) (K.mul (co K X (3)) (co K X (8)))) (K.add (ReduceCBM.Mt K 4 5 X) K.zero) := by
    have h1 := CBLib.sumN_peel K (fun i => K.mul ((co K X) i) ((wa11 K X) i)) 4 (X.length + 2)
    have hb : sumN K (fun i => K.mul ((co K X) i) ((wa11 K X) i)) 4 = (K.add (K.add (K.add (K.add K.zero (K.mul (co K X (0)) (co K X (5)))) (K.mul (co K X (1)) (co K X (6)))) (K.mul (co K X (2)) (co K X (7)))) (K.mul (co K X (3)) (co K X (8)))) := rfl
    rw [hb] at h1
    have ht : sumN K (fun i => K.mul ((co K X) (i + 4)) ((wa11 K X) (i + 4))) (X.length + 2) = (K.add (ReduceCBM.Mt K 4 5 X) K.zero) := by
      rw [sumN_ext K _ (CBLib.lc K X [(4, 5)]) (fun i => by
        show (K.mul (co K X (i + 4)) (co K X (i + 9))) = (K.add (K.mul (co K X (i + 4)) (co K X (i + 9))) K.zero)
        ring_z)]
      exact CBLib.lc_sum K X _ (Nat.le_add_right _ 2) [(4, 5)]
    exact h1.trans (congrArg (K.add (K.add (K.add (K.add (K.add K.zero (K.mul (co K X (0)) (co K X (5)))) (K.mul (co K X (1)) (co K X (6)))) (K.mul (co K X (2)) (co K X (7)))) (K.mul (co K X (3)) (co K X (8))))) ht)
  have ipa11y : CauchyBinet2.ip K (VGoodPair.yv K X) (wa11 K X) (X.length + 2 + 4) = K.add (K.add (K.add (K.add (K.add K.zero (K.mul (co K X (1)) (co K X (5)))) (K.mul (K.add (co K X (0)) (co K X (2))) (co K X (6)))) (K.mul (K.add (co K X (1)) (co K X (3))) (co K X (7)))) (K.mul (K.add (co K X (2)) (co K X (4))) (co K X (8)))) (K.add (ReduceCBM.Mt K 3 6 X) (K.add (ReduceCBM.Mt K 5 4 X) K.zero)) :=  -- (+3 chars; full line in the Zenodo source)
    have h1 := CBLib.sumN_peel K (fun i => K.mul ((VGoodPair.yv K X) i) ((wa11 K X) i)) 4 (X.length + 2)
    have hb : sumN K (fun i => K.mul ((VGoodPair.yv K X) i) ((wa11 K X) i)) 4 = (K.add (K.add (K.add (K.add K.zero (K.mul (co K X (1)) (co K X (5)))) (K.mul (K.add (co K X (0)) (co K X (2))) (co K X (6)))) (K.mul (K.add (co K X (1)) (co K X (3))) (co K X (7)))) (K.mul (K.add (co K X (2)) (co K X (4))) (co K X (8)))) := rfl
    rw [hb] at h1
    have ht : sumN K (fun i => K.mul ((VGoodPair.yv K X) (i + 4)) ((wa11 K X) (i + 4))) (X.length + 2) = (K.add (ReduceCBM.Mt K 3 6 X) (K.add (ReduceCBM.Mt K 5 4 X) K.zero)) := by
      rw [sumN_ext K _ (CBLib.lc K X [(3, 6), (5, 4)]) (fun i => by
        show (K.mul (K.add (co K X (i + 3)) (co K X (i + 5))) (co K X (i + 9))) = (K.add (K.mul (co K X (i + 3)) (co K X (i + 9))) (K.add (K.mul (co K X (i + 5)) (co K X (i + 9))) K.zero))
        ring_z)]
      exact CBLib.lc_sum K X _ (Nat.le_add_right _ 2) [(3, 6), (5, 4)]
    exact h1.trans (congrArg (K.add (K.add (K.add (K.add (K.add K.zero (K.mul (co K X (1)) (co K X (5)))) (K.mul (K.add (co K X (0)) (co K X (2))) (co K X (6)))) (K.mul (K.add (co K X (1)) (co K X (3))) (co K X (7)))) (K.mul (K.add (co K X (2)) (co K X (4))) (co K X (8))))) ht)
  have ipa20x : CauchyBinet2.ip K (co K X) (wa20 K X) (X.length + 2 + 4) = K.add (K.add (K.add (K.add (K.add K.zero (K.mul (co K X (0)) (co K X (7)))) (K.mul (co K X (1)) (K.add (co K X (8)) (co K X (6))))) (K.mul (co K X (2)) (K.add (co K X (9)) (co K X (7))))) (K.mul (co K X (3)) (K.add (co K X (10)) (co K X (8))))) (K.add (ReduceCBM.Mt K 4 7 X) (K.add (ReduceCBM.Mt K 4 5 X) K.zero)) := by
    have h1 := CBLib.sumN_peel K (fun i => K.mul ((co K X) i) ((wa20 K X) i)) 4 (X.length + 2)
    have hb : sumN K (fun i => K.mul ((co K X) i) ((wa20 K X) i)) 4 = (K.add (K.add (K.add (K.add K.zero (K.mul (co K X (0)) (co K X (7)))) (K.mul (co K X (1)) (K.add (co K X (8)) (co K X (6))))) (K.mul (co K X (2)) (K.add (co K X (9)) (co K X (7))))) (K.mul (co K X (3)) (K.add (co K X (10)) (co K X (8))))) := rfl
    rw [hb] at h1
    have ht : sumN K (fun i => K.mul ((co K X) (i + 4)) ((wa20 K X) (i + 4))) (X.length + 2) = (K.add (ReduceCBM.Mt K 4 7 X) (K.add (ReduceCBM.Mt K 4 5 X) K.zero)) := by
      rw [sumN_ext K _ (CBLib.lc K X [(4, 7), (4, 5)]) (fun i => by
        show (K.mul (co K X (i + 4)) (K.add (co K X (i + 11)) (co K X (i + 9)))) = (K.add (K.mul (co K X (i + 4)) (co K X (i + 11))) (K.add (K.mul (co K X (i + 4)) (co K X (i + 9))) K.zero))
        ring_z)]
      exact CBLib.lc_sum K X _ (Nat.le_add_right _ 2) [(4, 7), (4, 5)]
    exact h1.trans (congrArg (K.add (K.add (K.add (K.add (K.add K.zero (K.mul (co K X (0)) (co K X (7)))) (K.mul (co K X (1)) (K.add (co K X (8)) (co K X (6))))) (K.mul (co K X (2)) (K.add (co K X (9)) (co K X (7))))) (K.mul (co K X (3)) (K.add (co K X (10)) (co K X (8)))))) ht)
  have ipa20y : CauchyBinet2.ip K (VGoodPair.yv K X) (wa20 K X) (X.length + 2 + 4) = K.add (K.add (K.add (K.add (K.add K.zero (K.mul (co K X (1)) (co K X (7)))) (K.mul (K.add (co K X (0)) (co K X (2))) (K.add (co K X (8)) (co K X (6))))) (K.mul (K.add (co K X (1)) (co K X (3))) (K.add (co K X (9)) (co K X (7))))) (K.mul (K.add (co K X (2)) (co K X (4))) (K.add (co K X (10)) (co K X (8))))) (K.add   -- (+129 chars; full line in the Zenodo source)
    have h1 := CBLib.sumN_peel K (fun i => K.mul ((VGoodPair.yv K X) i) ((wa20 K X) i)) 4 (X.length + 2)
    have hb : sumN K (fun i => K.mul ((VGoodPair.yv K X) i) ((wa20 K X) i)) 4 = (K.add (K.add (K.add (K.add K.zero (K.mul (co K X (1)) (co K X (7)))) (K.mul (K.add (co K X (0)) (co K X (2))) (K.add (co K X (8)) (co K X (6))))) (K.mul (K.add (co K X (1)) (co K X (3))) (K.add (co K X (9)) (co K X (7))))) (K.mul (K.add (co K X (2)) (co K X (4))) (K.add (co K X (10)) (co K X (8))))) := rfl
    rw [hb] at h1
    have ht : sumN K (fun i => K.mul ((VGoodPair.yv K X) (i + 4)) ((wa20 K X) (i + 4))) (X.length + 2) = (K.add (ReduceCBM.Mt K 3 8 X) (K.add (ReduceCBM.Mt K 3 6 X) (K.add (ReduceCBM.Mt K 5 6 X) (K.add (ReduceCBM.Mt K 5 4 X) K.zero)))) := by
      rw [sumN_ext K _ (CBLib.lc K X [(3, 8), (3, 6), (5, 6), (5, 4)]) (fun i => by
        show (K.mul (K.add (co K X (i + 3)) (co K X (i + 5))) (K.add (co K X (i + 11)) (co K X (i + 9)))) = (K.add (K.mul (co K X (i + 3)) (co K X (i + 11))) (K.add (K.mul (co K X (i + 3)) (co K X (i + 9))) (K.add (K.mul (co K X (i + 5)) (co K X (i + 11))) (K.add (K.mul (co K X (i + 5)) (co K X (i + 9))) K.zero))))
        ring_z)]
      exact CBLib.lc_sum K X _ (Nat.le_add_right _ 2) [(3, 8), (3, 6), (5, 6), (5, 4)]
    exact h1.trans (congrArg (K.add (K.add (K.add (K.add (K.add K.zero (K.mul (co K X (1)) (co K X (7)))) (K.mul (K.add (co K X (0)) (co K X (2))) (K.add (co K X (8)) (co K X (6))))) (K.mul (K.add (co K X (1)) (co K X (3))) (K.add (co K X (9)) (co K X (7))))) (K.mul (K.add (co K X (2)) (co K X (4))) (K.add (co K X (10)) (co K X (8)))))) ht)
  have ipa21x : CauchyBinet2.ip K (co K X) (wa21 K X) (X.length + 2 + 4) = K.add (K.add (K.add (K.add (K.add K.zero (K.mul (co K X (0)) (co K X (6)))) (K.mul (co K X (1)) (K.add (co K X (7)) (co K X (5))))) (K.mul (co K X (2)) (K.add (co K X (8)) (co K X (6))))) (K.mul (co K X (3)) (K.add (co K X (9)) (co K X (7))))) (K.add (ReduceCBM.Mt K 4 6 X) (K.add (ReduceCBM.Mt K 4 4 X) K.zero)) := by
    have h1 := CBLib.sumN_peel K (fun i => K.mul ((co K X) i) ((wa21 K X) i)) 4 (X.length + 2)
    have hb : sumN K (fun i => K.mul ((co K X) i) ((wa21 K X) i)) 4 = (K.add (K.add (K.add (K.add K.zero (K.mul (co K X (0)) (co K X (6)))) (K.mul (co K X (1)) (K.add (co K X (7)) (co K X (5))))) (K.mul (co K X (2)) (K.add (co K X (8)) (co K X (6))))) (K.mul (co K X (3)) (K.add (co K X (9)) (co K X (7))))) := rfl
    rw [hb] at h1
    have ht : sumN K (fun i => K.mul ((co K X) (i + 4)) ((wa21 K X) (i + 4))) (X.length + 2) = (K.add (ReduceCBM.Mt K 4 6 X) (K.add (ReduceCBM.Mt K 4 4 X) K.zero)) := by
      rw [sumN_ext K _ (CBLib.lc K X [(4, 6), (4, 4)]) (fun i => by
        show (K.mul (co K X (i + 4)) (K.add (co K X (i + 10)) (co K X (i + 8)))) = (K.add (K.mul (co K X (i + 4)) (co K X (i + 10))) (K.add (K.mul (co K X (i + 4)) (co K X (i + 8))) K.zero))
        ring_z)]
      exact CBLib.lc_sum K X _ (Nat.le_add_right _ 2) [(4, 6), (4, 4)]
    exact h1.trans (congrArg (K.add (K.add (K.add (K.add (K.add K.zero (K.mul (co K X (0)) (co K X (6)))) (K.mul (co K X (1)) (K.add (co K X (7)) (co K X (5))))) (K.mul (co K X (2)) (K.add (co K X (8)) (co K X (6))))) (K.mul (co K X (3)) (K.add (co K X (9)) (co K X (7)))))) ht)
  have ipa21y : CauchyBinet2.ip K (VGoodPair.yv K X) (wa21 K X) (X.length + 2 + 4) = K.add (K.add (K.add (K.add (K.add K.zero (K.mul (co K X (1)) (co K X (6)))) (K.mul (K.add (co K X (0)) (co K X (2))) (K.add (co K X (7)) (co K X (5))))) (K.mul (K.add (co K X (1)) (co K X (3))) (K.add (co K X (8)) (co K X (6))))) (K.mul (K.add (co K X (2)) (co K X (4))) (K.add (co K X (9)) (co K X (7))))) (K.add (  -- (+128 chars; full line in the Zenodo source)
    have h1 := CBLib.sumN_peel K (fun i => K.mul ((VGoodPair.yv K X) i) ((wa21 K X) i)) 4 (X.length + 2)
    have hb : sumN K (fun i => K.mul ((VGoodPair.yv K X) i) ((wa21 K X) i)) 4 = (K.add (K.add (K.add (K.add K.zero (K.mul (co K X (1)) (co K X (6)))) (K.mul (K.add (co K X (0)) (co K X (2))) (K.add (co K X (7)) (co K X (5))))) (K.mul (K.add (co K X (1)) (co K X (3))) (K.add (co K X (8)) (co K X (6))))) (K.mul (K.add (co K X (2)) (co K X (4))) (K.add (co K X (9)) (co K X (7))))) := rfl
    rw [hb] at h1
    have ht : sumN K (fun i => K.mul ((VGoodPair.yv K X) (i + 4)) ((wa21 K X) (i + 4))) (X.length + 2) = (K.add (ReduceCBM.Mt K 3 7 X) (K.add (ReduceCBM.Mt K 3 5 X) (K.add (ReduceCBM.Mt K 5 5 X) (K.add (ReduceCBM.Mt K 5 3 X) K.zero)))) := by
      rw [sumN_ext K _ (CBLib.lc K X [(3, 7), (3, 5), (5, 5), (5, 3)]) (fun i => by
        show (K.mul (K.add (co K X (i + 3)) (co K X (i + 5))) (K.add (co K X (i + 10)) (co K X (i + 8)))) = (K.add (K.mul (co K X (i + 3)) (co K X (i + 10))) (K.add (K.mul (co K X (i + 3)) (co K X (i + 8))) (K.add (K.mul (co K X (i + 5)) (co K X (i + 10))) (K.add (K.mul (co K X (i + 5)) (co K X (i + 8))) K.zero))))
        ring_z)]
      exact CBLib.lc_sum K X _ (Nat.le_add_right _ 2) [(3, 7), (3, 5), (5, 5), (5, 3)]
    exact h1.trans (congrArg (K.add (K.add (K.add (K.add (K.add K.zero (K.mul (co K X (1)) (co K X (6)))) (K.mul (K.add (co K X (0)) (co K X (2))) (K.add (co K X (7)) (co K X (5))))) (K.mul (K.add (co K X (1)) (co K X (3))) (K.add (co K X (8)) (co K X (6))))) (K.mul (K.add (co K X (2)) (co K X (4))) (K.add (co K X (9)) (co K X (7)))))) ht)
  have ipa30x : CauchyBinet2.ip K (co K X) (wa30 K X) (X.length + 2 + 4) = K.add (K.add (K.add (K.add (K.add K.zero (K.mul (co K X (0)) (co K X (8)))) (K.mul (co K X (1)) (K.add (co K X (9)) (co K X (7))))) (K.mul (co K X (2)) (K.add (K.add (co K X (10)) (co K X (8))) (co K X (6))))) (K.mul (co K X (3)) (K.add (K.add (co K X (11)) (co K X (9))) (co K X (7))))) (K.add (ReduceCBM.Mt K 4 8 X) (K.add   -- (+68 chars; full line in the Zenodo source)
    have h1 := CBLib.sumN_peel K (fun i => K.mul ((co K X) i) ((wa30 K X) i)) 4 (X.length + 2)
    have hb : sumN K (fun i => K.mul ((co K X) i) ((wa30 K X) i)) 4 = (K.add (K.add (K.add (K.add K.zero (K.mul (co K X (0)) (co K X (8)))) (K.mul (co K X (1)) (K.add (co K X (9)) (co K X (7))))) (K.mul (co K X (2)) (K.add (K.add (co K X (10)) (co K X (8))) (co K X (6))))) (K.mul (co K X (3)) (K.add (K.add (co K X (11)) (co K X (9))) (co K X (7))))) := rfl
    rw [hb] at h1
    have ht : sumN K (fun i => K.mul ((co K X) (i + 4)) ((wa30 K X) (i + 4))) (X.length + 2) = (K.add (ReduceCBM.Mt K 4 8 X) (K.add (ReduceCBM.Mt K 4 6 X) (K.add (ReduceCBM.Mt K 4 4 X) K.zero))) := by
      rw [sumN_ext K _ (CBLib.lc K X [(4, 8), (4, 6), (4, 4)]) (fun i => by
        show (K.mul (co K X (i + 4)) (K.add (K.add (co K X (i + 12)) (co K X (i + 10))) (co K X (i + 8)))) = (K.add (K.mul (co K X (i + 4)) (co K X (i + 12))) (K.add (K.mul (co K X (i + 4)) (co K X (i + 10))) (K.add (K.mul (co K X (i + 4)) (co K X (i + 8))) K.zero)))
        ring_z)]
      exact CBLib.lc_sum K X _ (Nat.le_add_right _ 2) [(4, 8), (4, 6), (4, 4)]
    exact h1.trans (congrArg (K.add (K.add (K.add (K.add (K.add K.zero (K.mul (co K X (0)) (co K X (8)))) (K.mul (co K X (1)) (K.add (co K X (9)) (co K X (7))))) (K.mul (co K X (2)) (K.add (K.add (co K X (10)) (co K X (8))) (co K X (6))))) (K.mul (co K X (3)) (K.add (K.add (co K X (11)) (co K X (9))) (co K X (7)))))) ht)
  have ipa30y : CauchyBinet2.ip K (VGoodPair.yv K X) (wa30 K X) (X.length + 2 + 4) = K.add (K.add (K.add (K.add (K.add K.zero (K.mul (co K X (1)) (co K X (8)))) (K.mul (K.add (co K X (0)) (co K X (2))) (K.add (co K X (9)) (co K X (7))))) (K.mul (K.add (co K X (1)) (co K X (3))) (K.add (K.add (co K X (10)) (co K X (8))) (co K X (6))))) (K.mul (K.add (co K X (2)) (co K X (4))) (K.add (K.add (co K X   -- (+234 chars; full line in the Zenodo source)
    have h1 := CBLib.sumN_peel K (fun i => K.mul ((VGoodPair.yv K X) i) ((wa30 K X) i)) 4 (X.length + 2)
    have hb : sumN K (fun i => K.mul ((VGoodPair.yv K X) i) ((wa30 K X) i)) 4 = (K.add (K.add (K.add (K.add K.zero (K.mul (co K X (1)) (co K X (8)))) (K.mul (K.add (co K X (0)) (co K X (2))) (K.add (co K X (9)) (co K X (7))))) (K.mul (K.add (co K X (1)) (co K X (3))) (K.add (K.add (co K X (10)) (co K X (8))) (co K X (6))))) (K.mul (K.add (co K X (2)) (co K X (4))) (K.add (K.add (co K X (11)) (co K  -- (+31 chars; full line in the Zenodo source)
    rw [hb] at h1
    have ht : sumN K (fun i => K.mul ((VGoodPair.yv K X) (i + 4)) ((wa30 K X) (i + 4))) (X.length + 2) = (K.add (ReduceCBM.Mt K 3 9 X) (K.add (ReduceCBM.Mt K 3 7 X) (K.add (ReduceCBM.Mt K 3 5 X) (K.add (ReduceCBM.Mt K 5 7 X) (K.add (ReduceCBM.Mt K 5 5 X) (K.add (ReduceCBM.Mt K 5 3 X) K.zero)))))) := by
      rw [sumN_ext K _ (CBLib.lc K X [(3, 9), (3, 7), (3, 5), (5, 7), (5, 5), (5, 3)]) (fun i => by
        show (K.mul (K.add (co K X (i + 3)) (co K X (i + 5))) (K.add (K.add (co K X (i + 12)) (co K X (i + 10))) (co K X (i + 8)))) = (K.add (K.mul (co K X (i + 3)) (co K X (i + 12))) (K.add (K.mul (co K X (i + 3)) (co K X (i + 10))) (K.add (K.mul (co K X (i + 3)) (co K X (i + 8))) (K.add (K.mul (co K X (i + 5)) (co K X (i + 12))) (K.add (K.mul (co K X (i + 5)) (co K X (i + 10))) (K.add (K.mul (co  -- (+44 chars; full line in the Zenodo source)
        ring_z)]
      exact CBLib.lc_sum K X _ (Nat.le_add_right _ 2) [(3, 9), (3, 7), (3, 5), (5, 7), (5, 5), (5, 3)]
    exact h1.trans (congrArg (K.add (K.add (K.add (K.add (K.add K.zero (K.mul (co K X (1)) (co K X (8)))) (K.mul (K.add (co K X (0)) (co K X (2))) (K.add (co K X (9)) (co K X (7))))) (K.mul (K.add (co K X (1)) (co K X (3))) (K.add (K.add (co K X (10)) (co K X (8))) (co K X (6))))) (K.mul (K.add (co K X (2)) (co K X (4))) (K.add (K.add (co K X (11)) (co K X (9))) (co K X (7)))))) ht)
  have ipa31x : CauchyBinet2.ip K (co K X) (wa31 K X) (X.length + 2 + 4) = K.add (K.add (K.add (K.add (K.add K.zero (K.mul (co K X (0)) (co K X (7)))) (K.mul (co K X (1)) (K.add (co K X (8)) (co K X (6))))) (K.mul (co K X (2)) (K.add (K.add (co K X (9)) (co K X (7))) (co K X (5))))) (K.mul (co K X (3)) (K.add (K.add (co K X (10)) (co K X (8))) (co K X (6))))) (K.add (ReduceCBM.Mt K 4 7 X) (K.add (  -- (+67 chars; full line in the Zenodo source)
    have h1 := CBLib.sumN_peel K (fun i => K.mul ((co K X) i) ((wa31 K X) i)) 4 (X.length + 2)
    have hb : sumN K (fun i => K.mul ((co K X) i) ((wa31 K X) i)) 4 = (K.add (K.add (K.add (K.add K.zero (K.mul (co K X (0)) (co K X (7)))) (K.mul (co K X (1)) (K.add (co K X (8)) (co K X (6))))) (K.mul (co K X (2)) (K.add (K.add (co K X (9)) (co K X (7))) (co K X (5))))) (K.mul (co K X (3)) (K.add (K.add (co K X (10)) (co K X (8))) (co K X (6))))) := rfl
    rw [hb] at h1
    have ht : sumN K (fun i => K.mul ((co K X) (i + 4)) ((wa31 K X) (i + 4))) (X.length + 2) = (K.add (ReduceCBM.Mt K 4 7 X) (K.add (ReduceCBM.Mt K 4 5 X) (K.add (ReduceCBM.Mt K 4 3 X) K.zero))) := by
      rw [sumN_ext K _ (CBLib.lc K X [(4, 7), (4, 5), (4, 3)]) (fun i => by
        show (K.mul (co K X (i + 4)) (K.add (K.add (co K X (i + 11)) (co K X (i + 9))) (co K X (i + 7)))) = (K.add (K.mul (co K X (i + 4)) (co K X (i + 11))) (K.add (K.mul (co K X (i + 4)) (co K X (i + 9))) (K.add (K.mul (co K X (i + 4)) (co K X (i + 7))) K.zero)))
        ring_z)]
      exact CBLib.lc_sum K X _ (Nat.le_add_right _ 2) [(4, 7), (4, 5), (4, 3)]
    exact h1.trans (congrArg (K.add (K.add (K.add (K.add (K.add K.zero (K.mul (co K X (0)) (co K X (7)))) (K.mul (co K X (1)) (K.add (co K X (8)) (co K X (6))))) (K.mul (co K X (2)) (K.add (K.add (co K X (9)) (co K X (7))) (co K X (5))))) (K.mul (co K X (3)) (K.add (K.add (co K X (10)) (co K X (8))) (co K X (6)))))) ht)
  have ipa31y : CauchyBinet2.ip K (VGoodPair.yv K X) (wa31 K X) (X.length + 2 + 4) = K.add (K.add (K.add (K.add (K.add K.zero (K.mul (co K X (1)) (co K X (7)))) (K.mul (K.add (co K X (0)) (co K X (2))) (K.add (co K X (8)) (co K X (6))))) (K.mul (K.add (co K X (1)) (co K X (3))) (K.add (K.add (co K X (9)) (co K X (7))) (co K X (5))))) (K.mul (K.add (co K X (2)) (co K X (4))) (K.add (K.add (co K X (  -- (+233 chars; full line in the Zenodo source)
    have h1 := CBLib.sumN_peel K (fun i => K.mul ((VGoodPair.yv K X) i) ((wa31 K X) i)) 4 (X.length + 2)
    have hb : sumN K (fun i => K.mul ((VGoodPair.yv K X) i) ((wa31 K X) i)) 4 = (K.add (K.add (K.add (K.add K.zero (K.mul (co K X (1)) (co K X (7)))) (K.mul (K.add (co K X (0)) (co K X (2))) (K.add (co K X (8)) (co K X (6))))) (K.mul (K.add (co K X (1)) (co K X (3))) (K.add (K.add (co K X (9)) (co K X (7))) (co K X (5))))) (K.mul (K.add (co K X (2)) (co K X (4))) (K.add (K.add (co K X (10)) (co K   -- (+30 chars; full line in the Zenodo source)
    rw [hb] at h1
    have ht : sumN K (fun i => K.mul ((VGoodPair.yv K X) (i + 4)) ((wa31 K X) (i + 4))) (X.length + 2) = (K.add (ReduceCBM.Mt K 3 8 X) (K.add (ReduceCBM.Mt K 3 6 X) (K.add (ReduceCBM.Mt K 3 4 X) (K.add (ReduceCBM.Mt K 5 6 X) (K.add (ReduceCBM.Mt K 5 4 X) (K.add (ReduceCBM.Mt K 5 2 X) K.zero)))))) := by
      rw [sumN_ext K _ (CBLib.lc K X [(3, 8), (3, 6), (3, 4), (5, 6), (5, 4), (5, 2)]) (fun i => by
        show (K.mul (K.add (co K X (i + 3)) (co K X (i + 5))) (K.add (K.add (co K X (i + 11)) (co K X (i + 9))) (co K X (i + 7)))) = (K.add (K.mul (co K X (i + 3)) (co K X (i + 11))) (K.add (K.mul (co K X (i + 3)) (co K X (i + 9))) (K.add (K.mul (co K X (i + 3)) (co K X (i + 7))) (K.add (K.mul (co K X (i + 5)) (co K X (i + 11))) (K.add (K.mul (co K X (i + 5)) (co K X (i + 9))) (K.add (K.mul (co K   -- (+41 chars; full line in the Zenodo source)
        ring_z)]
      exact CBLib.lc_sum K X _ (Nat.le_add_right _ 2) [(3, 8), (3, 6), (3, 4), (5, 6), (5, 4), (5, 2)]
    exact h1.trans (congrArg (K.add (K.add (K.add (K.add (K.add K.zero (K.mul (co K X (1)) (co K X (7)))) (K.mul (K.add (co K X (0)) (co K X (2))) (K.add (co K X (8)) (co K X (6))))) (K.mul (K.add (co K X (1)) (co K X (3))) (K.add (K.add (co K X (9)) (co K X (7))) (co K X (5))))) (K.mul (K.add (co K X (2)) (co K X (4))) (K.add (K.add (co K X (10)) (co K X (8))) (co K X (6)))))) ht)
  have ipa40x : CauchyBinet2.ip K (co K X) (wa40 K X) (X.length + 2 + 4) = K.add (K.add (K.add (K.add (K.add K.zero (K.mul (co K X (0)) (co K X (9)))) (K.mul (co K X (1)) (K.add (co K X (10)) (co K X (8))))) (K.mul (co K X (2)) (K.add (K.add (co K X (11)) (co K X (9))) (co K X (7))))) (K.mul (co K X (3)) (K.add (K.add (K.add (co K X (12)) (co K X (10))) (co K X (8))) (co K X (6))))) (K.add (Reduce  -- (+122 chars; full line in the Zenodo source)
    have h1 := CBLib.sumN_peel K (fun i => K.mul ((co K X) i) ((wa40 K X) i)) 4 (X.length + 2)
    have hb : sumN K (fun i => K.mul ((co K X) i) ((wa40 K X) i)) 4 = (K.add (K.add (K.add (K.add K.zero (K.mul (co K X (0)) (co K X (9)))) (K.mul (co K X (1)) (K.add (co K X (10)) (co K X (8))))) (K.mul (co K X (2)) (K.add (K.add (co K X (11)) (co K X (9))) (co K X (7))))) (K.mul (co K X (3)) (K.add (K.add (K.add (co K X (12)) (co K X (10))) (co K X (8))) (co K X (6))))) := rfl
    rw [hb] at h1
    have ht : sumN K (fun i => K.mul ((co K X) (i + 4)) ((wa40 K X) (i + 4))) (X.length + 2) = (K.add (ReduceCBM.Mt K 4 9 X) (K.add (ReduceCBM.Mt K 4 7 X) (K.add (ReduceCBM.Mt K 4 5 X) (K.add (ReduceCBM.Mt K 4 3 X) K.zero)))) := by
      rw [sumN_ext K _ (CBLib.lc K X [(4, 9), (4, 7), (4, 5), (4, 3)]) (fun i => by
        show (K.mul (co K X (i + 4)) (K.add (K.add (K.add (co K X (i + 13)) (co K X (i + 11))) (co K X (i + 9))) (co K X (i + 7)))) = (K.add (K.mul (co K X (i + 4)) (co K X (i + 13))) (K.add (K.mul (co K X (i + 4)) (co K X (i + 11))) (K.add (K.mul (co K X (i + 4)) (co K X (i + 9))) (K.add (K.mul (co K X (i + 4)) (co K X (i + 7))) K.zero))))
        ring_z)]
      exact CBLib.lc_sum K X _ (Nat.le_add_right _ 2) [(4, 9), (4, 7), (4, 5), (4, 3)]
    exact h1.trans (congrArg (K.add (K.add (K.add (K.add (K.add K.zero (K.mul (co K X (0)) (co K X (9)))) (K.mul (co K X (1)) (K.add (co K X (10)) (co K X (8))))) (K.mul (co K X (2)) (K.add (K.add (co K X (11)) (co K X (9))) (co K X (7))))) (K.mul (co K X (3)) (K.add (K.add (K.add (co K X (12)) (co K X (10))) (co K X (8))) (co K X (6)))))) ht)
  have ipa40y : CauchyBinet2.ip K (VGoodPair.yv K X) (wa40 K X) (X.length + 2 + 4) = K.add (K.add (K.add (K.add (K.add K.zero (K.mul (co K X (1)) (co K X (9)))) (K.mul (K.add (co K X (0)) (co K X (2))) (K.add (co K X (10)) (co K X (8))))) (K.mul (K.add (co K X (1)) (co K X (3))) (K.add (K.add (co K X (11)) (co K X (9))) (co K X (7))))) (K.mul (K.add (co K X (2)) (co K X (4))) (K.add (K.add (K.add   -- (+320 chars; full line in the Zenodo source)
    have h1 := CBLib.sumN_peel K (fun i => K.mul ((VGoodPair.yv K X) i) ((wa40 K X) i)) 4 (X.length + 2)
    have hb : sumN K (fun i => K.mul ((VGoodPair.yv K X) i) ((wa40 K X) i)) 4 = (K.add (K.add (K.add (K.add K.zero (K.mul (co K X (1)) (co K X (9)))) (K.mul (K.add (co K X (0)) (co K X (2))) (K.add (co K X (10)) (co K X (8))))) (K.mul (K.add (co K X (1)) (co K X (3))) (K.add (K.add (co K X (11)) (co K X (9))) (co K X (7))))) (K.mul (K.add (co K X (2)) (co K X (4))) (K.add (K.add (K.add (co K X (12  -- (+54 chars; full line in the Zenodo source)
    rw [hb] at h1
    have ht : sumN K (fun i => K.mul ((VGoodPair.yv K X) (i + 4)) ((wa40 K X) (i + 4))) (X.length + 2) = (K.add (ReduceCBM.Mt K 3 10 X) (K.add (ReduceCBM.Mt K 3 8 X) (K.add (ReduceCBM.Mt K 3 6 X) (K.add (ReduceCBM.Mt K 3 4 X) (K.add (ReduceCBM.Mt K 5 8 X) (K.add (ReduceCBM.Mt K 5 6 X) (K.add (ReduceCBM.Mt K 5 4 X) (K.add (ReduceCBM.Mt K 5 2 X) K.zero)))))))) := by
      rw [sumN_ext K _ (CBLib.lc K X [(3, 10), (3, 8), (3, 6), (3, 4), (5, 8), (5, 6), (5, 4), (5, 2)]) (fun i => by
        show (K.mul (K.add (co K X (i + 3)) (co K X (i + 5))) (K.add (K.add (K.add (co K X (i + 13)) (co K X (i + 11))) (co K X (i + 9))) (co K X (i + 7)))) = (K.add (K.mul (co K X (i + 3)) (co K X (i + 13))) (K.add (K.mul (co K X (i + 3)) (co K X (i + 11))) (K.add (K.mul (co K X (i + 3)) (co K X (i + 9))) (K.add (K.mul (co K X (i + 3)) (co K X (i + 7))) (K.add (K.mul (co K X (i + 5)) (co K X (i +  -- (+169 chars; full line in the Zenodo source)
        ring_z)]
      exact CBLib.lc_sum K X _ (Nat.le_add_right _ 2) [(3, 10), (3, 8), (3, 6), (3, 4), (5, 8), (5, 6), (5, 4), (5, 2)]
    exact h1.trans (congrArg (K.add (K.add (K.add (K.add (K.add K.zero (K.mul (co K X (1)) (co K X (9)))) (K.mul (K.add (co K X (0)) (co K X (2))) (K.add (co K X (10)) (co K X (8))))) (K.mul (K.add (co K X (1)) (co K X (3))) (K.add (K.add (co K X (11)) (co K X (9))) (co K X (7))))) (K.mul (K.add (co K X (2)) (co K X (4))) (K.add (K.add (K.add (co K X (12)) (co K X (10))) (co K X (8))) (co K X (6))  -- (+8 chars; full line in the Zenodo source)
  have ipa41x : CauchyBinet2.ip K (co K X) (wa41 K X) (X.length + 2 + 4) = K.add (K.add (K.add (K.add (K.add K.zero (K.mul (co K X (0)) (co K X (8)))) (K.mul (co K X (1)) (K.add (co K X (9)) (co K X (7))))) (K.mul (co K X (2)) (K.add (K.add (co K X (10)) (co K X (8))) (co K X (6))))) (K.mul (co K X (3)) (K.add (K.add (K.add (co K X (11)) (co K X (9))) (co K X (7))) (co K X (5))))) (K.add (ReduceCB  -- (+120 chars; full line in the Zenodo source)
    have h1 := CBLib.sumN_peel K (fun i => K.mul ((co K X) i) ((wa41 K X) i)) 4 (X.length + 2)
    have hb : sumN K (fun i => K.mul ((co K X) i) ((wa41 K X) i)) 4 = (K.add (K.add (K.add (K.add K.zero (K.mul (co K X (0)) (co K X (8)))) (K.mul (co K X (1)) (K.add (co K X (9)) (co K X (7))))) (K.mul (co K X (2)) (K.add (K.add (co K X (10)) (co K X (8))) (co K X (6))))) (K.mul (co K X (3)) (K.add (K.add (K.add (co K X (11)) (co K X (9))) (co K X (7))) (co K X (5))))) := rfl
    rw [hb] at h1
    have ht : sumN K (fun i => K.mul ((co K X) (i + 4)) ((wa41 K X) (i + 4))) (X.length + 2) = (K.add (ReduceCBM.Mt K 4 8 X) (K.add (ReduceCBM.Mt K 4 6 X) (K.add (ReduceCBM.Mt K 4 4 X) (K.add (ReduceCBM.Mt K 4 2 X) K.zero)))) := by
      rw [sumN_ext K _ (CBLib.lc K X [(4, 8), (4, 6), (4, 4), (4, 2)]) (fun i => by
        show (K.mul (co K X (i + 4)) (K.add (K.add (K.add (co K X (i + 12)) (co K X (i + 10))) (co K X (i + 8))) (co K X (i + 6)))) = (K.add (K.mul (co K X (i + 4)) (co K X (i + 12))) (K.add (K.mul (co K X (i + 4)) (co K X (i + 10))) (K.add (K.mul (co K X (i + 4)) (co K X (i + 8))) (K.add (K.mul (co K X (i + 4)) (co K X (i + 6))) K.zero))))
        ring_z)]
      exact CBLib.lc_sum K X _ (Nat.le_add_right _ 2) [(4, 8), (4, 6), (4, 4), (4, 2)]
    exact h1.trans (congrArg (K.add (K.add (K.add (K.add (K.add K.zero (K.mul (co K X (0)) (co K X (8)))) (K.mul (co K X (1)) (K.add (co K X (9)) (co K X (7))))) (K.mul (co K X (2)) (K.add (K.add (co K X (10)) (co K X (8))) (co K X (6))))) (K.mul (co K X (3)) (K.add (K.add (K.add (co K X (11)) (co K X (9))) (co K X (7))) (co K X (5)))))) ht)
  have ipa41y : CauchyBinet2.ip K (VGoodPair.yv K X) (wa41 K X) (X.length + 2 + 4) = K.add (K.add (K.add (K.add (K.add K.zero (K.mul (co K X (1)) (co K X (8)))) (K.mul (K.add (co K X (0)) (co K X (2))) (K.add (co K X (9)) (co K X (7))))) (K.mul (K.add (co K X (1)) (co K X (3))) (K.add (K.add (co K X (10)) (co K X (8))) (co K X (6))))) (K.mul (K.add (co K X (2)) (co K X (4))) (K.add (K.add (K.add (  -- (+317 chars; full line in the Zenodo source)
    have h1 := CBLib.sumN_peel K (fun i => K.mul ((VGoodPair.yv K X) i) ((wa41 K X) i)) 4 (X.length + 2)
    have hb : sumN K (fun i => K.mul ((VGoodPair.yv K X) i) ((wa41 K X) i)) 4 = (K.add (K.add (K.add (K.add K.zero (K.mul (co K X (1)) (co K X (8)))) (K.mul (K.add (co K X (0)) (co K X (2))) (K.add (co K X (9)) (co K X (7))))) (K.mul (K.add (co K X (1)) (co K X (3))) (K.add (K.add (co K X (10)) (co K X (8))) (co K X (6))))) (K.mul (K.add (co K X (2)) (co K X (4))) (K.add (K.add (K.add (co K X (11)  -- (+52 chars; full line in the Zenodo source)
    rw [hb] at h1
    have ht : sumN K (fun i => K.mul ((VGoodPair.yv K X) (i + 4)) ((wa41 K X) (i + 4))) (X.length + 2) = (K.add (ReduceCBM.Mt K 3 9 X) (K.add (ReduceCBM.Mt K 3 7 X) (K.add (ReduceCBM.Mt K 3 5 X) (K.add (ReduceCBM.Mt K 3 3 X) (K.add (ReduceCBM.Mt K 5 7 X) (K.add (ReduceCBM.Mt K 5 5 X) (K.add (ReduceCBM.Mt K 5 3 X) (K.add (ReduceCBM.Mt K 5 1 X) K.zero)))))))) := by
      rw [sumN_ext K _ (CBLib.lc K X [(3, 9), (3, 7), (3, 5), (3, 3), (5, 7), (5, 5), (5, 3), (5, 1)]) (fun i => by
        show (K.mul (K.add (co K X (i + 3)) (co K X (i + 5))) (K.add (K.add (K.add (co K X (i + 12)) (co K X (i + 10))) (co K X (i + 8))) (co K X (i + 6)))) = (K.add (K.mul (co K X (i + 3)) (co K X (i + 12))) (K.add (K.mul (co K X (i + 3)) (co K X (i + 10))) (K.add (K.mul (co K X (i + 3)) (co K X (i + 8))) (K.add (K.mul (co K X (i + 3)) (co K X (i + 6))) (K.add (K.mul (co K X (i + 5)) (co K X (i +  -- (+169 chars; full line in the Zenodo source)
        ring_z)]
      exact CBLib.lc_sum K X _ (Nat.le_add_right _ 2) [(3, 9), (3, 7), (3, 5), (3, 3), (5, 7), (5, 5), (5, 3), (5, 1)]
    exact h1.trans (congrArg (K.add (K.add (K.add (K.add (K.add K.zero (K.mul (co K X (1)) (co K X (8)))) (K.mul (K.add (co K X (0)) (co K X (2))) (K.add (co K X (9)) (co K X (7))))) (K.mul (K.add (co K X (1)) (co K X (3))) (K.add (K.add (co K X (10)) (co K X (8))) (co K X (6))))) (K.mul (K.add (co K X (2)) (co K X (4))) (K.add (K.add (K.add (co K X (11)) (co K X (9))) (co K X (7))) (co K X (5))))  -- (+6 chars; full line in the Zenodo source)
  have ipa50x : CauchyBinet2.ip K (co K X) (wa50 K X) (X.length + 2 + 4) = K.add (K.add (K.add (K.add (K.add K.zero (K.mul (co K X (0)) (co K X (10)))) (K.mul (co K X (1)) (K.add (co K X (11)) (co K X (9))))) (K.mul (co K X (2)) (K.add (K.add (co K X (12)) (co K X (10))) (co K X (8))))) (K.mul (co K X (3)) (K.add (K.add (K.add (co K X (13)) (co K X (11))) (co K X (9))) (co K X (7))))) (K.add (Redu  -- (+156 chars; full line in the Zenodo source)
    have h1 := CBLib.sumN_peel K (fun i => K.mul ((co K X) i) ((wa50 K X) i)) 4 (X.length + 2)
    have hb : sumN K (fun i => K.mul ((co K X) i) ((wa50 K X) i)) 4 = (K.add (K.add (K.add (K.add K.zero (K.mul (co K X (0)) (co K X (10)))) (K.mul (co K X (1)) (K.add (co K X (11)) (co K X (9))))) (K.mul (co K X (2)) (K.add (K.add (co K X (12)) (co K X (10))) (co K X (8))))) (K.mul (co K X (3)) (K.add (K.add (K.add (co K X (13)) (co K X (11))) (co K X (9))) (co K X (7))))) := rfl
    rw [hb] at h1
    have ht : sumN K (fun i => K.mul ((co K X) (i + 4)) ((wa50 K X) (i + 4))) (X.length + 2) = (K.add (ReduceCBM.Mt K 4 10 X) (K.add (ReduceCBM.Mt K 4 8 X) (K.add (ReduceCBM.Mt K 4 6 X) (K.add (ReduceCBM.Mt K 4 4 X) (K.add (ReduceCBM.Mt K 4 2 X) K.zero))))) := by
      rw [sumN_ext K _ (CBLib.lc K X [(4, 10), (4, 8), (4, 6), (4, 4), (4, 2)]) (fun i => by
        show (K.mul (co K X (i + 4)) (K.add (K.add (K.add (K.add (co K X (i + 14)) (co K X (i + 12))) (co K X (i + 10))) (co K X (i + 8))) (co K X (i + 6)))) = (K.add (K.mul (co K X (i + 4)) (co K X (i + 14))) (K.add (K.mul (co K X (i + 4)) (co K X (i + 12))) (K.add (K.mul (co K X (i + 4)) (co K X (i + 10))) (K.add (K.mul (co K X (i + 4)) (co K X (i + 8))) (K.add (K.mul (co K X (i + 4)) (co K X (i  -- (+19 chars; full line in the Zenodo source)
        ring_z)]
      exact CBLib.lc_sum K X _ (Nat.le_add_right _ 2) [(4, 10), (4, 8), (4, 6), (4, 4), (4, 2)]
    exact h1.trans (congrArg (K.add (K.add (K.add (K.add (K.add K.zero (K.mul (co K X (0)) (co K X (10)))) (K.mul (co K X (1)) (K.add (co K X (11)) (co K X (9))))) (K.mul (co K X (2)) (K.add (K.add (co K X (12)) (co K X (10))) (co K X (8))))) (K.mul (co K X (3)) (K.add (K.add (K.add (co K X (13)) (co K X (11))) (co K X (9))) (co K X (7)))))) ht)
  have ipa50y : CauchyBinet2.ip K (VGoodPair.yv K X) (wa50 K X) (X.length + 2 + 4) = K.add (K.add (K.add (K.add (K.add K.zero (K.mul (co K X (1)) (co K X (10)))) (K.mul (K.add (co K X (0)) (co K X (2))) (K.add (co K X (11)) (co K X (9))))) (K.mul (K.add (co K X (1)) (co K X (3))) (K.add (K.add (co K X (12)) (co K X (10))) (co K X (8))))) (K.mul (K.add (co K X (2)) (co K X (4))) (K.add (K.add (K.ad  -- (+384 chars; full line in the Zenodo source)
    have h1 := CBLib.sumN_peel K (fun i => K.mul ((VGoodPair.yv K X) i) ((wa50 K X) i)) 4 (X.length + 2)
    have hb : sumN K (fun i => K.mul ((VGoodPair.yv K X) i) ((wa50 K X) i)) 4 = (K.add (K.add (K.add (K.add K.zero (K.mul (co K X (1)) (co K X (10)))) (K.mul (K.add (co K X (0)) (co K X (2))) (K.add (co K X (11)) (co K X (9))))) (K.mul (K.add (co K X (1)) (co K X (3))) (K.add (K.add (co K X (12)) (co K X (10))) (co K X (8))))) (K.mul (K.add (co K X (2)) (co K X (4))) (K.add (K.add (K.add (co K X (  -- (+56 chars; full line in the Zenodo source)
    rw [hb] at h1
    have ht : sumN K (fun i => K.mul ((VGoodPair.yv K X) (i + 4)) ((wa50 K X) (i + 4))) (X.length + 2) = (K.add (ReduceCBM.Mt K 3 11 X) (K.add (ReduceCBM.Mt K 3 9 X) (K.add (ReduceCBM.Mt K 3 7 X) (K.add (ReduceCBM.Mt K 3 5 X) (K.add (ReduceCBM.Mt K 3 3 X) (K.add (ReduceCBM.Mt K 5 9 X) (K.add (ReduceCBM.Mt K 5 7 X) (K.add (ReduceCBM.Mt K 5 5 X) (K.add (ReduceCBM.Mt K 5 3 X) (K.add (ReduceCBM.Mt K 5  -- (+28 chars; full line in the Zenodo source)
      rw [sumN_ext K _ (CBLib.lc K X [(3, 11), (3, 9), (3, 7), (3, 5), (3, 3), (5, 9), (5, 7), (5, 5), (5, 3), (5, 1)]) (fun i => by
        show (K.mul (K.add (co K X (i + 3)) (co K X (i + 5))) (K.add (K.add (K.add (K.add (co K X (i + 14)) (co K X (i + 12))) (co K X (i + 10))) (co K X (i + 8))) (co K X (i + 6)))) = (K.add (K.mul (co K X (i + 3)) (co K X (i + 14))) (K.add (K.mul (co K X (i + 3)) (co K X (i + 12))) (K.add (K.mul (co K X (i + 3)) (co K X (i + 10))) (K.add (K.mul (co K X (i + 3)) (co K X (i + 8))) (K.add (K.mul (c  -- (+297 chars; full line in the Zenodo source)
        ring_z)]
      exact CBLib.lc_sum K X _ (Nat.le_add_right _ 2) [(3, 11), (3, 9), (3, 7), (3, 5), (3, 3), (5, 9), (5, 7), (5, 5), (5, 3), (5, 1)]
    exact h1.trans (congrArg (K.add (K.add (K.add (K.add (K.add K.zero (K.mul (co K X (1)) (co K X (10)))) (K.mul (K.add (co K X (0)) (co K X (2))) (K.add (co K X (11)) (co K X (9))))) (K.mul (K.add (co K X (1)) (co K X (3))) (K.add (K.add (co K X (12)) (co K X (10))) (co K X (8))))) (K.mul (K.add (co K X (2)) (co K X (4))) (K.add (K.add (K.add (co K X (13)) (co K X (11))) (co K X (9))) (co K X (7  -- (+10 chars; full line in the Zenodo source)
  have ipa51x : CauchyBinet2.ip K (co K X) (wa51 K X) (X.length + 2 + 4) = K.add (K.add (K.add (K.add (K.add K.zero (K.mul (co K X (0)) (co K X (9)))) (K.mul (co K X (1)) (K.add (co K X (10)) (co K X (8))))) (K.mul (co K X (2)) (K.add (K.add (co K X (11)) (co K X (9))) (co K X (7))))) (K.mul (co K X (3)) (K.add (K.add (K.add (co K X (12)) (co K X (10))) (co K X (8))) (co K X (6))))) (K.add (Reduce  -- (+153 chars; full line in the Zenodo source)
    have h1 := CBLib.sumN_peel K (fun i => K.mul ((co K X) i) ((wa51 K X) i)) 4 (X.length + 2)
    have hb : sumN K (fun i => K.mul ((co K X) i) ((wa51 K X) i)) 4 = (K.add (K.add (K.add (K.add K.zero (K.mul (co K X (0)) (co K X (9)))) (K.mul (co K X (1)) (K.add (co K X (10)) (co K X (8))))) (K.mul (co K X (2)) (K.add (K.add (co K X (11)) (co K X (9))) (co K X (7))))) (K.mul (co K X (3)) (K.add (K.add (K.add (co K X (12)) (co K X (10))) (co K X (8))) (co K X (6))))) := rfl
    rw [hb] at h1
    have ht : sumN K (fun i => K.mul ((co K X) (i + 4)) ((wa51 K X) (i + 4))) (X.length + 2) = (K.add (ReduceCBM.Mt K 4 9 X) (K.add (ReduceCBM.Mt K 4 7 X) (K.add (ReduceCBM.Mt K 4 5 X) (K.add (ReduceCBM.Mt K 4 3 X) (K.add (ReduceCBM.Mt K 4 1 X) K.zero))))) := by
      rw [sumN_ext K _ (CBLib.lc K X [(4, 9), (4, 7), (4, 5), (4, 3), (4, 1)]) (fun i => by
        show (K.mul (co K X (i + 4)) (K.add (K.add (K.add (K.add (co K X (i + 13)) (co K X (i + 11))) (co K X (i + 9))) (co K X (i + 7))) (co K X (i + 5)))) = (K.add (K.mul (co K X (i + 4)) (co K X (i + 13))) (K.add (K.mul (co K X (i + 4)) (co K X (i + 11))) (K.add (K.mul (co K X (i + 4)) (co K X (i + 9))) (K.add (K.mul (co K X (i + 4)) (co K X (i + 7))) (K.add (K.mul (co K X (i + 4)) (co K X (i +  -- (+17 chars; full line in the Zenodo source)
        ring_z)]
      exact CBLib.lc_sum K X _ (Nat.le_add_right _ 2) [(4, 9), (4, 7), (4, 5), (4, 3), (4, 1)]
    exact h1.trans (congrArg (K.add (K.add (K.add (K.add (K.add K.zero (K.mul (co K X (0)) (co K X (9)))) (K.mul (co K X (1)) (K.add (co K X (10)) (co K X (8))))) (K.mul (co K X (2)) (K.add (K.add (co K X (11)) (co K X (9))) (co K X (7))))) (K.mul (co K X (3)) (K.add (K.add (K.add (co K X (12)) (co K X (10))) (co K X (8))) (co K X (6)))))) ht)
  have ipa51y : CauchyBinet2.ip K (VGoodPair.yv K X) (wa51 K X) (X.length + 2 + 4) = K.add (K.add (K.add (K.add (K.add K.zero (K.mul (co K X (1)) (co K X (9)))) (K.mul (K.add (co K X (0)) (co K X (2))) (K.add (co K X (10)) (co K X (8))))) (K.mul (K.add (co K X (1)) (co K X (3))) (K.add (K.add (co K X (11)) (co K X (9))) (co K X (7))))) (K.mul (K.add (co K X (2)) (co K X (4))) (K.add (K.add (K.add   -- (+382 chars; full line in the Zenodo source)
    have h1 := CBLib.sumN_peel K (fun i => K.mul ((VGoodPair.yv K X) i) ((wa51 K X) i)) 4 (X.length + 2)
    have hb : sumN K (fun i => K.mul ((VGoodPair.yv K X) i) ((wa51 K X) i)) 4 = (K.add (K.add (K.add (K.add K.zero (K.mul (co K X (1)) (co K X (9)))) (K.mul (K.add (co K X (0)) (co K X (2))) (K.add (co K X (10)) (co K X (8))))) (K.mul (K.add (co K X (1)) (co K X (3))) (K.add (K.add (co K X (11)) (co K X (9))) (co K X (7))))) (K.mul (K.add (co K X (2)) (co K X (4))) (K.add (K.add (K.add (co K X (12  -- (+54 chars; full line in the Zenodo source)
    rw [hb] at h1
    have ht : sumN K (fun i => K.mul ((VGoodPair.yv K X) (i + 4)) ((wa51 K X) (i + 4))) (X.length + 2) = (K.add (ReduceCBM.Mt K 3 10 X) (K.add (ReduceCBM.Mt K 3 8 X) (K.add (ReduceCBM.Mt K 3 6 X) (K.add (ReduceCBM.Mt K 3 4 X) (K.add (ReduceCBM.Mt K 3 2 X) (K.add (ReduceCBM.Mt K 5 8 X) (K.add (ReduceCBM.Mt K 5 6 X) (K.add (ReduceCBM.Mt K 5 4 X) (K.add (ReduceCBM.Mt K 5 2 X) (K.add (ReduceCBM.Mt K 5  -- (+28 chars; full line in the Zenodo source)
      rw [sumN_ext K _ (CBLib.lc K X [(3, 10), (3, 8), (3, 6), (3, 4), (3, 2), (5, 8), (5, 6), (5, 4), (5, 2), (5, 0)]) (fun i => by
        show (K.mul (K.add (co K X (i + 3)) (co K X (i + 5))) (K.add (K.add (K.add (K.add (co K X (i + 13)) (co K X (i + 11))) (co K X (i + 9))) (co K X (i + 7))) (co K X (i + 5)))) = (K.add (K.mul (co K X (i + 3)) (co K X (i + 13))) (K.add (K.mul (co K X (i + 3)) (co K X (i + 11))) (K.add (K.mul (co K X (i + 3)) (co K X (i + 9))) (K.add (K.mul (co K X (i + 3)) (co K X (i + 7))) (K.add (K.mul (co   -- (+294 chars; full line in the Zenodo source)
        ring_z)]
      exact CBLib.lc_sum K X _ (Nat.le_add_right _ 2) [(3, 10), (3, 8), (3, 6), (3, 4), (3, 2), (5, 8), (5, 6), (5, 4), (5, 2), (5, 0)]
    exact h1.trans (congrArg (K.add (K.add (K.add (K.add (K.add K.zero (K.mul (co K X (1)) (co K X (9)))) (K.mul (K.add (co K X (0)) (co K X (2))) (K.add (co K X (10)) (co K X (8))))) (K.mul (K.add (co K X (1)) (co K X (3))) (K.add (K.add (co K X (11)) (co K X (9))) (co K X (7))))) (K.mul (K.add (co K X (2)) (co K X (4))) (K.add (K.add (K.add (co K X (12)) (co K X (10))) (co K X (8))) (co K X (6))  -- (+8 chars; full line in the Zenodo source)
  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)
  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)
  have hc2 : cgc K X X 2 = K.add (K.add (cgc K X X 0) (K.add (Corr.rr K 2 X) (Corr.rr K 2 X))) (K.neg (K.mul (co K X (0)) (co K X (0)))) := by
    have st : K.add (cgc K X X 2) (RhoC.bil K X (fun j k => dl 0 (j + k))) = K.add (cgc K X X 0) (RhoC.bil K X (fun j k => dl 2 (dist j k))) := RhoC.cgc_step K X 0
    have dd : RhoC.bil K X (fun j k => dl 2 (dist j k)) = K.add (Corr.rr K 2 X) (Corr.rr K 2 X) := Corr.dist_bil K 1 X
    have cc : RhoC.bil K X (fun j k => dl 0 (j + k)) = (K.mul (co K X (0)) (co K X (0))) := CBLib.conv_cs K 0 X
    rw [dd, cc] at st
    exact CBM2.eq_of_add K _ _ _ st
  have hc3 : cgc K X X 3 = K.add (K.add (cgc K X X 1) (K.add (Corr.rr K 3 X) (Corr.rr K 3 X))) (K.neg (K.add (K.mul (co K X (0)) (co K X (1))) (K.mul (co K X (0)) (co K X (1))))) := by
    have st : K.add (cgc K X X 3) (RhoC.bil K X (fun j k => dl 1 (j + k))) = K.add (cgc K X X 1) (RhoC.bil K X (fun j k => dl 3 (dist j k))) := RhoC.cgc_step K X 1
    have dd : RhoC.bil K X (fun j k => dl 3 (dist j k)) = K.add (Corr.rr K 3 X) (Corr.rr K 3 X) := Corr.dist_bil K 2 X
    have cc : RhoC.bil K X (fun j k => dl 1 (j + k)) = (K.add (K.mul (co K X (0)) (co K X (1))) (K.mul (co K X (0)) (co K X (1)))) := CBLib.conv_cs K 1 X
    rw [dd, cc] at st
    exact CBM2.eq_of_add K _ _ _ st
  have hc4 : cgc K X X 4 = K.add (K.add (cgc K X X 2) (K.add (Corr.rr K 4 X) (Corr.rr K 4 X))) (K.neg (K.add (K.add (K.mul (co K X (0)) (co K X (2))) (K.mul (co K X (0)) (co K X (2)))) (K.mul (co K X (1)) (co K X (1))))) := by
    have st : K.add (cgc K X X 4) (RhoC.bil K X (fun j k => dl 2 (j + k))) = K.add (cgc K X X 2) (RhoC.bil K X (fun j k => dl 4 (dist j k))) := RhoC.cgc_step K X 2
    have dd : RhoC.bil K X (fun j k => dl 4 (dist j k)) = K.add (Corr.rr K 4 X) (Corr.rr K 4 X) := Corr.dist_bil K 3 X
    have cc : RhoC.bil K X (fun j k => dl 2 (j + k)) = (K.add (K.add (K.mul (co K X (0)) (co K X (2))) (K.mul (co K X (0)) (co K X (2)))) (K.mul (co K X (1)) (co K X (1)))) := CBLib.conv_cs K 2 X
    rw [dd, cc] at st
    exact CBM2.eq_of_add K _ _ _ st
  have hc5 : cgc K X X 5 = K.add (K.add (cgc K X X 3) (K.add (Corr.rr K 5 X) (Corr.rr K 5 X))) (K.neg (K.add (K.add (K.mul (co K X (0)) (co K X (3))) (K.mul (co K X (0)) (co K X (3)))) (K.add (K.mul (co K X (1)) (co K X (2))) (K.mul (co K X (1)) (co K X (2)))))) := by
    have st : K.add (cgc K X X 5) (RhoC.bil K X (fun j k => dl 3 (j + k))) = K.add (cgc K X X 3) (RhoC.bil K X (fun j k => dl 5 (dist j k))) := RhoC.cgc_step K X 3
    have dd : RhoC.bil K X (fun j k => dl 5 (dist j k)) = K.add (Corr.rr K 5 X) (Corr.rr K 5 X) := Corr.dist_bil K 4 X
    have cc : RhoC.bil K X (fun j k => dl 3 (j + k)) = (K.add (K.add (K.mul (co K X (0)) (co K X (3))) (K.mul (co K X (0)) (co K X (3)))) (K.add (K.mul (co K X (1)) (co K X (2))) (K.mul (co K X (1)) (co K X (2))))) := CBLib.conv_cs K 3 X
    rw [dd, cc] at st
    exact CBM2.eq_of_add K _ _ _ st
  have hc6 : cgc K X X 6 = K.add (K.add (cgc K X X 4) (K.add (Corr.rr K 6 X) (Corr.rr K 6 X))) (K.neg (K.add (K.add (K.mul (co K X (0)) (co K X (4))) (K.mul (co K X (0)) (co K X (4)))) (K.add (K.add (K.mul (co K X (1)) (co K X (3))) (K.mul (co K X (1)) (co K X (3)))) (K.mul (co K X (2)) (co K X (2)))))) := by
    have st : K.add (cgc K X X 6) (RhoC.bil K X (fun j k => dl 4 (j + k))) = K.add (cgc K X X 4) (RhoC.bil K X (fun j k => dl 6 (dist j k))) := RhoC.cgc_step K X 4
    have dd : RhoC.bil K X (fun j k => dl 6 (dist j k)) = K.add (Corr.rr K 6 X) (Corr.rr K 6 X) := Corr.dist_bil K 5 X
    have cc : RhoC.bil K X (fun j k => dl 4 (j + k)) = (K.add (K.add (K.mul (co K X (0)) (co K X (4))) (K.mul (co K X (0)) (co K X (4)))) (K.add (K.add (K.mul (co K X (1)) (co K X (3))) (K.mul (co K X (1)) (co K X (3)))) (K.mul (co K X (2)) (co K X (2))))) := CBLib.conv_cs K 4 X
    rw [dd, cc] at st
    exact CBM2.eq_of_add K _ _ _ st
  have hc7 : cgc K X X 7 = K.add (K.add (cgc K X X 5) (K.add (Corr.rr K 7 X) (Corr.rr K 7 X))) (K.neg (K.add (K.add (K.mul (co K X (0)) (co K X (5))) (K.mul (co K X (0)) (co K X (5)))) (K.add (K.add (K.mul (co K X (1)) (co K X (4))) (K.mul (co K X (1)) (co K X (4)))) (K.add (K.mul (co K X (2)) (co K X (3))) (K.mul (co K X (2)) (co K X (3))))))) := by
    have st : K.add (cgc K X X 7) (RhoC.bil K X (fun j k => dl 5 (j + k))) = K.add (cgc K X X 5) (RhoC.bil K X (fun j k => dl 7 (dist j k))) := RhoC.cgc_step K X 5
    have dd : RhoC.bil K X (fun j k => dl 7 (dist j k)) = K.add (Corr.rr K 7 X) (Corr.rr K 7 X) := Corr.dist_bil K 6 X
    have cc : RhoC.bil K X (fun j k => dl 5 (j + k)) = (K.add (K.add (K.mul (co K X (0)) (co K X (5))) (K.mul (co K X (0)) (co K X (5)))) (K.add (K.add (K.mul (co K X (1)) (co K X (4))) (K.mul (co K X (1)) (co K X (4)))) (K.add (K.mul (co K X (2)) (co K X (3))) (K.mul (co K X (2)) (co K X (3)))))) := CBLib.conv_cs K 5 X
    rw [dd, cc] at st
    exact CBM2.eq_of_add K _ _ _ st
  have hc8 : cgc K X X 8 = K.add (K.add (cgc K X X 6) (K.add (Corr.rr K 8 X) (Corr.rr K 8 X))) (K.neg (K.add (K.add (K.mul (co K X (0)) (co K X (6))) (K.mul (co K X (0)) (co K X (6)))) (K.add (K.add (K.mul (co K X (1)) (co K X (5))) (K.mul (co K X (1)) (co K X (5)))) (K.add (K.add (K.mul (co K X (2)) (co K X (4))) (K.mul (co K X (2)) (co K X (4)))) (K.mul (co K X (3)) (co K X (3))))))) := by
    have st : K.add (cgc K X X 8) (RhoC.bil K X (fun j k => dl 6 (j + k))) = K.add (cgc K X X 6) (RhoC.bil K X (fun j k => dl 8 (dist j k))) := RhoC.cgc_step K X 6
    have dd : RhoC.bil K X (fun j k => dl 8 (dist j k)) = K.add (Corr.rr K 8 X) (Corr.rr K 8 X) := Corr.dist_bil K 7 X
    have cc : RhoC.bil K X (fun j k => dl 6 (j + k)) = (K.add (K.add (K.mul (co K X (0)) (co K X (6))) (K.mul (co K X (0)) (co K X (6)))) (K.add (K.add (K.mul (co K X (1)) (co K X (5))) (K.mul (co K X (1)) (co K X (5)))) (K.add (K.add (K.mul (co K X (2)) (co K X (4))) (K.mul (co K X (2)) (co K X (4)))) (K.mul (co K X (3)) (co K X (3)))))) := CBLib.conv_cs K 6 X
    rw [dd, cc] at st
    exact CBM2.eq_of_add K _ _ _ st
  have hc9 : cgc K X X 9 = K.add (K.add (cgc K X X 7) (K.add (Corr.rr K 9 X) (Corr.rr K 9 X))) (K.neg (K.add (K.add (K.mul (co K X (0)) (co K X (7))) (K.mul (co K X (0)) (co K X (7)))) (K.add (K.add (K.mul (co K X (1)) (co K X (6))) (K.mul (co K X (1)) (co K X (6)))) (K.add (K.add (K.mul (co K X (2)) (co K X (5))) (K.mul (co K X (2)) (co K X (5)))) (K.add (K.mul (co K X (3)) (co K X (4))) (K.mul (  -- (+36 chars; full line in the Zenodo source)
    have st : K.add (cgc K X X 9) (RhoC.bil K X (fun j k => dl 7 (j + k))) = K.add (cgc K X X 7) (RhoC.bil K X (fun j k => dl 9 (dist j k))) := RhoC.cgc_step K X 7
    have dd : RhoC.bil K X (fun j k => dl 9 (dist j k)) = K.add (Corr.rr K 9 X) (Corr.rr K 9 X) := Corr.dist_bil K 8 X
    have cc : RhoC.bil K X (fun j k => dl 7 (j + k)) = (K.add (K.add (K.mul (co K X (0)) (co K X (7))) (K.mul (co K X (0)) (co K X (7)))) (K.add (K.add (K.mul (co K X (1)) (co K X (6))) (K.mul (co K X (1)) (co K X (6)))) (K.add (K.add (K.mul (co K X (2)) (co K X (5))) (K.mul (co K X (2)) (co K X (5)))) (K.add (K.mul (co K X (3)) (co K X (4))) (K.mul (co K X (3)) (co K X (4))))))) := CBLib.conv_cs   -- (+5 chars; full line in the Zenodo source)
    rw [dd, cc] at st
    exact CBM2.eq_of_add K _ _ _ st
  have hc10 : cgc K X X 10 = K.add (K.add (cgc K X X 8) (K.add (Corr.rr K 10 X) (Corr.rr K 10 X))) (K.neg (K.add (K.add (K.mul (co K X (0)) (co K X (8))) (K.mul (co K X (0)) (co K X (8)))) (K.add (K.add (K.mul (co K X (1)) (co K X (7))) (K.mul (co K X (1)) (co K X (7)))) (K.add (K.add (K.mul (co K X (2)) (co K X (6))) (K.mul (co K X (2)) (co K X (6)))) (K.add (K.add (K.mul (co K X (3)) (co K X (5)  -- (+82 chars; full line in the Zenodo source)
    have st : K.add (cgc K X X 10) (RhoC.bil K X (fun j k => dl 8 (j + k))) = K.add (cgc K X X 8) (RhoC.bil K X (fun j k => dl 10 (dist j k))) := RhoC.cgc_step K X 8
    have dd : RhoC.bil K X (fun j k => dl 10 (dist j k)) = K.add (Corr.rr K 10 X) (Corr.rr K 10 X) := Corr.dist_bil K 9 X
    have cc : RhoC.bil K X (fun j k => dl 8 (j + k)) = (K.add (K.add (K.mul (co K X (0)) (co K X (8))) (K.mul (co K X (0)) (co K X (8)))) (K.add (K.add (K.mul (co K X (1)) (co K X (7))) (K.mul (co K X (1)) (co K X (7)))) (K.add (K.add (K.mul (co K X (2)) (co K X (6))) (K.mul (co K X (2)) (co K X (6)))) (K.add (K.add (K.mul (co K X (3)) (co K X (5))) (K.mul (co K X (3)) (co K X (5)))) (K.mul (co K   -- (+47 chars; full line in the Zenodo source)
    rw [dd, cc] at st
    exact CBM2.eq_of_add K _ _ _ st
  have hc11 : cgc K X X 11 = K.add (K.add (cgc K X X 9) (K.add (Corr.rr K 11 X) (Corr.rr K 11 X))) (K.neg (K.add (K.add (K.mul (co K X (0)) (co K X (9))) (K.mul (co K X (0)) (co K X (9)))) (K.add (K.add (K.mul (co K X (1)) (co K X (8))) (K.mul (co K X (1)) (co K X (8)))) (K.add (K.add (K.mul (co K X (2)) (co K X (7))) (K.mul (co K X (2)) (co K X (7)))) (K.add (K.add (K.mul (co K X (3)) (co K X (6)  -- (+124 chars; full line in the Zenodo source)
    have st : K.add (cgc K X X 11) (RhoC.bil K X (fun j k => dl 9 (j + k))) = K.add (cgc K X X 9) (RhoC.bil K X (fun j k => dl 11 (dist j k))) := RhoC.cgc_step K X 9
    have dd : RhoC.bil K X (fun j k => dl 11 (dist j k)) = K.add (Corr.rr K 11 X) (Corr.rr K 11 X) := Corr.dist_bil K 10 X
    have cc : RhoC.bil K X (fun j k => dl 9 (j + k)) = (K.add (K.add (K.mul (co K X (0)) (co K X (9))) (K.mul (co K X (0)) (co K X (9)))) (K.add (K.add (K.mul (co K X (1)) (co K X (8))) (K.mul (co K X (1)) (co K X (8)))) (K.add (K.add (K.mul (co K X (2)) (co K X (7))) (K.mul (co K X (2)) (co K X (7)))) (K.add (K.add (K.mul (co K X (3)) (co K X (6))) (K.mul (co K X (3)) (co K X (6)))) (K.add (K.mul  -- (+89 chars; full line in the Zenodo source)
    rw [dd, cc] at st
    exact CBM2.eq_of_add K _ _ _ st
  have st0_0 : ReduceCBM.Mt K 0 0 X = K.add (K.mul (co K X (0)) (co K X (0))) (ReduceCBM.Mt K 1 0 X) := CBLib.mt_step K 0 0 X
  have st1_0 : ReduceCBM.Mt K 1 0 X = K.add (K.mul (co K X (1)) (co K X (1))) (ReduceCBM.Mt K 2 0 X) := CBLib.mt_step K 1 0 X
  have st2_0 : ReduceCBM.Mt K 2 0 X = K.add (K.mul (co K X (2)) (co K X (2))) (ReduceCBM.Mt K 3 0 X) := CBLib.mt_step K 2 0 X
  have st3_0 : ReduceCBM.Mt K 3 0 X = K.add (K.mul (co K X (3)) (co K X (3))) (ReduceCBM.Mt K 4 0 X) := CBLib.mt_step K 3 0 X
  have st4_0 : ReduceCBM.Mt K 4 0 X = K.add (K.mul (co K X (4)) (co K X (4))) (ReduceCBM.Mt K 5 0 X) := CBLib.mt_step K 4 0 X
  have st5_0 : ReduceCBM.Mt K 5 0 X = K.add (K.mul (co K X (5)) (co K X (5))) (ReduceCBM.Mt K 6 0 X) := CBLib.mt_step K 5 0 X
  have st0_1 : ReduceCBM.Mt K 0 1 X = K.add (K.mul (co K X (0)) (co K X (1))) (ReduceCBM.Mt K 1 1 X) := CBLib.mt_step K 0 1 X
  have st1_1 : ReduceCBM.Mt K 1 1 X = K.add (K.mul (co K X (1)) (co K X (2))) (ReduceCBM.Mt K 2 1 X) := CBLib.mt_step K 1 1 X
  have st2_1 : ReduceCBM.Mt K 2 1 X = K.add (K.mul (co K X (2)) (co K X (3))) (ReduceCBM.Mt K 3 1 X) := CBLib.mt_step K 2 1 X
  have st3_1 : ReduceCBM.Mt K 3 1 X = K.add (K.mul (co K X (3)) (co K X (4))) (ReduceCBM.Mt K 4 1 X) := CBLib.mt_step K 3 1 X
  have st4_1 : ReduceCBM.Mt K 4 1 X = K.add (K.mul (co K X (4)) (co K X (5))) (ReduceCBM.Mt K 5 1 X) := CBLib.mt_step K 4 1 X
  have st5_1 : ReduceCBM.Mt K 5 1 X = K.add (K.mul (co K X (5)) (co K X (6))) (ReduceCBM.Mt K 6 1 X) := CBLib.mt_step K 5 1 X
  have st0_2 : ReduceCBM.Mt K 0 2 X = K.add (K.mul (co K X (0)) (co K X (2))) (ReduceCBM.Mt K 1 2 X) := CBLib.mt_step K 0 2 X
  have st1_2 : ReduceCBM.Mt K 1 2 X = K.add (K.mul (co K X (1)) (co K X (3))) (ReduceCBM.Mt K 2 2 X) := CBLib.mt_step K 1 2 X
  have st2_2 : ReduceCBM.Mt K 2 2 X = K.add (K.mul (co K X (2)) (co K X (4))) (ReduceCBM.Mt K 3 2 X) := CBLib.mt_step K 2 2 X
  have st3_2 : ReduceCBM.Mt K 3 2 X = K.add (K.mul (co K X (3)) (co K X (5))) (ReduceCBM.Mt K 4 2 X) := CBLib.mt_step K 3 2 X
  have st4_2 : ReduceCBM.Mt K 4 2 X = K.add (K.mul (co K X (4)) (co K X (6))) (ReduceCBM.Mt K 5 2 X) := CBLib.mt_step K 4 2 X
  have st5_2 : ReduceCBM.Mt K 5 2 X = K.add (K.mul (co K X (5)) (co K X (7))) (ReduceCBM.Mt K 6 2 X) := CBLib.mt_step K 5 2 X
  have st0_3 : ReduceCBM.Mt K 0 3 X = K.add (K.mul (co K X (0)) (co K X (3))) (ReduceCBM.Mt K 1 3 X) := CBLib.mt_step K 0 3 X
  have st1_3 : ReduceCBM.Mt K 1 3 X = K.add (K.mul (co K X (1)) (co K X (4))) (ReduceCBM.Mt K 2 3 X) := CBLib.mt_step K 1 3 X
  have st2_3 : ReduceCBM.Mt K 2 3 X = K.add (K.mul (co K X (2)) (co K X (5))) (ReduceCBM.Mt K 3 3 X) := CBLib.mt_step K 2 3 X
  have st3_3 : ReduceCBM.Mt K 3 3 X = K.add (K.mul (co K X (3)) (co K X (6))) (ReduceCBM.Mt K 4 3 X) := CBLib.mt_step K 3 3 X
  have st4_3 : ReduceCBM.Mt K 4 3 X = K.add (K.mul (co K X (4)) (co K X (7))) (ReduceCBM.Mt K 5 3 X) := CBLib.mt_step K 4 3 X
  have st5_3 : ReduceCBM.Mt K 5 3 X = K.add (K.mul (co K X (5)) (co K X (8))) (ReduceCBM.Mt K 6 3 X) := CBLib.mt_step K 5 3 X
  have st0_4 : ReduceCBM.Mt K 0 4 X = K.add (K.mul (co K X (0)) (co K X (4))) (ReduceCBM.Mt K 1 4 X) := CBLib.mt_step K 0 4 X
  have st1_4 : ReduceCBM.Mt K 1 4 X = K.add (K.mul (co K X (1)) (co K X (5))) (ReduceCBM.Mt K 2 4 X) := CBLib.mt_step K 1 4 X
  have st2_4 : ReduceCBM.Mt K 2 4 X = K.add (K.mul (co K X (2)) (co K X (6))) (ReduceCBM.Mt K 3 4 X) := CBLib.mt_step K 2 4 X
  have st3_4 : ReduceCBM.Mt K 3 4 X = K.add (K.mul (co K X (3)) (co K X (7))) (ReduceCBM.Mt K 4 4 X) := CBLib.mt_step K 3 4 X
  have st4_4 : ReduceCBM.Mt K 4 4 X = K.add (K.mul (co K X (4)) (co K X (8))) (ReduceCBM.Mt K 5 4 X) := CBLib.mt_step K 4 4 X
  have st5_4 : ReduceCBM.Mt K 5 4 X = K.add (K.mul (co K X (5)) (co K X (9))) (ReduceCBM.Mt K 6 4 X) := CBLib.mt_step K 5 4 X
  have st0_5 : ReduceCBM.Mt K 0 5 X = K.add (K.mul (co K X (0)) (co K X (5))) (ReduceCBM.Mt K 1 5 X) := CBLib.mt_step K 0 5 X
  have st1_5 : ReduceCBM.Mt K 1 5 X = K.add (K.mul (co K X (1)) (co K X (6))) (ReduceCBM.Mt K 2 5 X) := CBLib.mt_step K 1 5 X
  have st2_5 : ReduceCBM.Mt K 2 5 X = K.add (K.mul (co K X (2)) (co K X (7))) (ReduceCBM.Mt K 3 5 X) := CBLib.mt_step K 2 5 X
  have st3_5 : ReduceCBM.Mt K 3 5 X = K.add (K.mul (co K X (3)) (co K X (8))) (ReduceCBM.Mt K 4 5 X) := CBLib.mt_step K 3 5 X
  have st4_5 : ReduceCBM.Mt K 4 5 X = K.add (K.mul (co K X (4)) (co K X (9))) (ReduceCBM.Mt K 5 5 X) := CBLib.mt_step K 4 5 X
  have st5_5 : ReduceCBM.Mt K 5 5 X = K.add (K.mul (co K X (5)) (co K X (10))) (ReduceCBM.Mt K 6 5 X) := CBLib.mt_step K 5 5 X
  have st0_6 : ReduceCBM.Mt K 0 6 X = K.add (K.mul (co K X (0)) (co K X (6))) (ReduceCBM.Mt K 1 6 X) := CBLib.mt_step K 0 6 X
  have st1_6 : ReduceCBM.Mt K 1 6 X = K.add (K.mul (co K X (1)) (co K X (7))) (ReduceCBM.Mt K 2 6 X) := CBLib.mt_step K 1 6 X
  have st2_6 : ReduceCBM.Mt K 2 6 X = K.add (K.mul (co K X (2)) (co K X (8))) (ReduceCBM.Mt K 3 6 X) := CBLib.mt_step K 2 6 X
  have st3_6 : ReduceCBM.Mt K 3 6 X = K.add (K.mul (co K X (3)) (co K X (9))) (ReduceCBM.Mt K 4 6 X) := CBLib.mt_step K 3 6 X
  have st4_6 : ReduceCBM.Mt K 4 6 X = K.add (K.mul (co K X (4)) (co K X (10))) (ReduceCBM.Mt K 5 6 X) := CBLib.mt_step K 4 6 X
  have st5_6 : ReduceCBM.Mt K 5 6 X = K.add (K.mul (co K X (5)) (co K X (11))) (ReduceCBM.Mt K 6 6 X) := CBLib.mt_step K 5 6 X
  have st0_7 : ReduceCBM.Mt K 0 7 X = K.add (K.mul (co K X (0)) (co K X (7))) (ReduceCBM.Mt K 1 7 X) := CBLib.mt_step K 0 7 X
  have st1_7 : ReduceCBM.Mt K 1 7 X = K.add (K.mul (co K X (1)) (co K X (8))) (ReduceCBM.Mt K 2 7 X) := CBLib.mt_step K 1 7 X
  have st2_7 : ReduceCBM.Mt K 2 7 X = K.add (K.mul (co K X (2)) (co K X (9))) (ReduceCBM.Mt K 3 7 X) := CBLib.mt_step K 2 7 X
  have st3_7 : ReduceCBM.Mt K 3 7 X = K.add (K.mul (co K X (3)) (co K X (10))) (ReduceCBM.Mt K 4 7 X) := CBLib.mt_step K 3 7 X
  have st4_7 : ReduceCBM.Mt K 4 7 X = K.add (K.mul (co K X (4)) (co K X (11))) (ReduceCBM.Mt K 5 7 X) := CBLib.mt_step K 4 7 X
  have st5_7 : ReduceCBM.Mt K 5 7 X = K.add (K.mul (co K X (5)) (co K X (12))) (ReduceCBM.Mt K 6 7 X) := CBLib.mt_step K 5 7 X
  have st0_8 : ReduceCBM.Mt K 0 8 X = K.add (K.mul (co K X (0)) (co K X (8))) (ReduceCBM.Mt K 1 8 X) := CBLib.mt_step K 0 8 X
  have st1_8 : ReduceCBM.Mt K 1 8 X = K.add (K.mul (co K X (1)) (co K X (9))) (ReduceCBM.Mt K 2 8 X) := CBLib.mt_step K 1 8 X
  have st2_8 : ReduceCBM.Mt K 2 8 X = K.add (K.mul (co K X (2)) (co K X (10))) (ReduceCBM.Mt K 3 8 X) := CBLib.mt_step K 2 8 X
  have st3_8 : ReduceCBM.Mt K 3 8 X = K.add (K.mul (co K X (3)) (co K X (11))) (ReduceCBM.Mt K 4 8 X) := CBLib.mt_step K 3 8 X
  have st4_8 : ReduceCBM.Mt K 4 8 X = K.add (K.mul (co K X (4)) (co K X (12))) (ReduceCBM.Mt K 5 8 X) := CBLib.mt_step K 4 8 X
  have st5_8 : ReduceCBM.Mt K 5 8 X = K.add (K.mul (co K X (5)) (co K X (13))) (ReduceCBM.Mt K 6 8 X) := CBLib.mt_step K 5 8 X
  have st0_9 : ReduceCBM.Mt K 0 9 X = K.add (K.mul (co K X (0)) (co K X (9))) (ReduceCBM.Mt K 1 9 X) := CBLib.mt_step K 0 9 X
  have st1_9 : ReduceCBM.Mt K 1 9 X = K.add (K.mul (co K X (1)) (co K X (10))) (ReduceCBM.Mt K 2 9 X) := CBLib.mt_step K 1 9 X
  have st2_9 : ReduceCBM.Mt K 2 9 X = K.add (K.mul (co K X (2)) (co K X (11))) (ReduceCBM.Mt K 3 9 X) := CBLib.mt_step K 2 9 X
  have st3_9 : ReduceCBM.Mt K 3 9 X = K.add (K.mul (co K X (3)) (co K X (12))) (ReduceCBM.Mt K 4 9 X) := CBLib.mt_step K 3 9 X
  have st4_9 : ReduceCBM.Mt K 4 9 X = K.add (K.mul (co K X (4)) (co K X (13))) (ReduceCBM.Mt K 5 9 X) := CBLib.mt_step K 4 9 X
  have st5_9 : ReduceCBM.Mt K 5 9 X = K.add (K.mul (co K X (5)) (co K X (14))) (ReduceCBM.Mt K 6 9 X) := CBLib.mt_step K 5 9 X
  have st0_10 : ReduceCBM.Mt K 0 10 X = K.add (K.mul (co K X (0)) (co K X (10))) (ReduceCBM.Mt K 1 10 X) := CBLib.mt_step K 0 10 X
  have st1_10 : ReduceCBM.Mt K 1 10 X = K.add (K.mul (co K X (1)) (co K X (11))) (ReduceCBM.Mt K 2 10 X) := CBLib.mt_step K 1 10 X
  have st2_10 : ReduceCBM.Mt K 2 10 X = K.add (K.mul (co K X (2)) (co K X (12))) (ReduceCBM.Mt K 3 10 X) := CBLib.mt_step K 2 10 X
  have st3_10 : ReduceCBM.Mt K 3 10 X = K.add (K.mul (co K X (3)) (co K X (13))) (ReduceCBM.Mt K 4 10 X) := CBLib.mt_step K 3 10 X
  have st4_10 : ReduceCBM.Mt K 4 10 X = K.add (K.mul (co K X (4)) (co K X (14))) (ReduceCBM.Mt K 5 10 X) := CBLib.mt_step K 4 10 X
  have st5_10 : ReduceCBM.Mt K 5 10 X = K.add (K.mul (co K X (5)) (co K X (15))) (ReduceCBM.Mt K 6 10 X) := CBLib.mt_step K 5 10 X
  have st0_11 : ReduceCBM.Mt K 0 11 X = K.add (K.mul (co K X (0)) (co K X (11))) (ReduceCBM.Mt K 1 11 X) := CBLib.mt_step K 0 11 X
  have st1_11 : ReduceCBM.Mt K 1 11 X = K.add (K.mul (co K X (1)) (co K X (12))) (ReduceCBM.Mt K 2 11 X) := CBLib.mt_step K 1 11 X
  have st2_11 : ReduceCBM.Mt K 2 11 X = K.add (K.mul (co K X (2)) (co K X (13))) (ReduceCBM.Mt K 3 11 X) := CBLib.mt_step K 2 11 X
  have st3_11 : ReduceCBM.Mt K 3 11 X = K.add (K.mul (co K X (3)) (co K X (14))) (ReduceCBM.Mt K 4 11 X) := CBLib.mt_step K 3 11 X
  have st4_11 : ReduceCBM.Mt K 4 11 X = K.add (K.mul (co K X (4)) (co K X (15))) (ReduceCBM.Mt K 5 11 X) := CBLib.mt_step K 4 11 X
  have st5_11 : ReduceCBM.Mt K 5 11 X = K.add (K.mul (co K X (5)) (co K X (16))) (ReduceCBM.Mt K 6 11 X) := CBLib.mt_step K 5 11 X
  have mx0_0 : ReduceCBM.Mt K 0 0 X = (K.add (K.mul (co K X (0)) (co K X (0))) (K.add (K.mul (co K X (1)) (co K X (1))) (K.add (K.mul (co K X (2)) (co K X (2))) (K.add (K.mul (co K X (3)) (co K X (3))) (K.add (K.mul (co K X (4)) (co K X (4))) (K.add (K.mul (co K X (5)) (co K X (5))) (ReduceCBM.Mt K 6 0 X))))))) := by rw [st0_0, st1_0, st2_0, st3_0, st4_0, st5_0]
  have mx0_1 : ReduceCBM.Mt K 0 1 X = (K.add (K.mul (co K X (0)) (co K X (1))) (K.add (K.mul (co K X (1)) (co K X (2))) (K.add (K.mul (co K X (2)) (co K X (3))) (K.add (K.mul (co K X (3)) (co K X (4))) (K.add (K.mul (co K X (4)) (co K X (5))) (K.add (K.mul (co K X (5)) (co K X (6))) (ReduceCBM.Mt K 6 1 X))))))) := by rw [st0_1, st1_1, st2_1, st3_1, st4_1, st5_1]
  have mx0_2 : ReduceCBM.Mt K 0 2 X = (K.add (K.mul (co K X (0)) (co K X (2))) (K.add (K.mul (co K X (1)) (co K X (3))) (K.add (K.mul (co K X (2)) (co K X (4))) (K.add (K.mul (co K X (3)) (co K X (5))) (K.add (K.mul (co K X (4)) (co K X (6))) (K.add (K.mul (co K X (5)) (co K X (7))) (ReduceCBM.Mt K 6 2 X))))))) := by rw [st0_2, st1_2, st2_2, st3_2, st4_2, st5_2]
  have mx0_3 : ReduceCBM.Mt K 0 3 X = (K.add (K.mul (co K X (0)) (co K X (3))) (K.add (K.mul (co K X (1)) (co K X (4))) (K.add (K.mul (co K X (2)) (co K X (5))) (K.add (K.mul (co K X (3)) (co K X (6))) (K.add (K.mul (co K X (4)) (co K X (7))) (K.add (K.mul (co K X (5)) (co K X (8))) (ReduceCBM.Mt K 6 3 X))))))) := by rw [st0_3, st1_3, st2_3, st3_3, st4_3, st5_3]
  have mx0_4 : ReduceCBM.Mt K 0 4 X = (K.add (K.mul (co K X (0)) (co K X (4))) (K.add (K.mul (co K X (1)) (co K X (5))) (K.add (K.mul (co K X (2)) (co K X (6))) (K.add (K.mul (co K X (3)) (co K X (7))) (K.add (K.mul (co K X (4)) (co K X (8))) (K.add (K.mul (co K X (5)) (co K X (9))) (ReduceCBM.Mt K 6 4 X))))))) := by rw [st0_4, st1_4, st2_4, st3_4, st4_4, st5_4]
  have mx0_5 : ReduceCBM.Mt K 0 5 X = (K.add (K.mul (co K X (0)) (co K X (5))) (K.add (K.mul (co K X (1)) (co K X (6))) (K.add (K.mul (co K X (2)) (co K X (7))) (K.add (K.mul (co K X (3)) (co K X (8))) (K.add (K.mul (co K X (4)) (co K X (9))) (K.add (K.mul (co K X (5)) (co K X (10))) (ReduceCBM.Mt K 6 5 X))))))) := by rw [st0_5, st1_5, st2_5, st3_5, st4_5, st5_5]
  have mx0_6 : ReduceCBM.Mt K 0 6 X = (K.add (K.mul (co K X (0)) (co K X (6))) (K.add (K.mul (co K X (1)) (co K X (7))) (K.add (K.mul (co K X (2)) (co K X (8))) (K.add (K.mul (co K X (3)) (co K X (9))) (K.add (K.mul (co K X (4)) (co K X (10))) (K.add (K.mul (co K X (5)) (co K X (11))) (ReduceCBM.Mt K 6 6 X))))))) := by rw [st0_6, st1_6, st2_6, st3_6, st4_6, st5_6]
  have mx0_7 : ReduceCBM.Mt K 0 7 X = (K.add (K.mul (co K X (0)) (co K X (7))) (K.add (K.mul (co K X (1)) (co K X (8))) (K.add (K.mul (co K X (2)) (co K X (9))) (K.add (K.mul (co K X (3)) (co K X (10))) (K.add (K.mul (co K X (4)) (co K X (11))) (K.add (K.mul (co K X (5)) (co K X (12))) (ReduceCBM.Mt K 6 7 X))))))) := by rw [st0_7, st1_7, st2_7, st3_7, st4_7, st5_7]
  have mx0_8 : ReduceCBM.Mt K 0 8 X = (K.add (K.mul (co K X (0)) (co K X (8))) (K.add (K.mul (co K X (1)) (co K X (9))) (K.add (K.mul (co K X (2)) (co K X (10))) (K.add (K.mul (co K X (3)) (co K X (11))) (K.add (K.mul (co K X (4)) (co K X (12))) (K.add (K.mul (co K X (5)) (co K X (13))) (ReduceCBM.Mt K 6 8 X))))))) := by rw [st0_8, st1_8, st2_8, st3_8, st4_8, st5_8]
  have mx0_9 : ReduceCBM.Mt K 0 9 X = (K.add (K.mul (co K X (0)) (co K X (9))) (K.add (K.mul (co K X (1)) (co K X (10))) (K.add (K.mul (co K X (2)) (co K X (11))) (K.add (K.mul (co K X (3)) (co K X (12))) (K.add (K.mul (co K X (4)) (co K X (13))) (K.add (K.mul (co K X (5)) (co K X (14))) (ReduceCBM.Mt K 6 9 X))))))) := by rw [st0_9, st1_9, st2_9, st3_9, st4_9, st5_9]
  have mx0_10 : ReduceCBM.Mt K 0 10 X = (K.add (K.mul (co K X (0)) (co K X (10))) (K.add (K.mul (co K X (1)) (co K X (11))) (K.add (K.mul (co K X (2)) (co K X (12))) (K.add (K.mul (co K X (3)) (co K X (13))) (K.add (K.mul (co K X (4)) (co K X (14))) (K.add (K.mul (co K X (5)) (co K X (15))) (ReduceCBM.Mt K 6 10 X))))))) := by rw [st0_10, st1_10, st2_10, st3_10, st4_10, st5_10]
  have mx0_11 : ReduceCBM.Mt K 0 11 X = (K.add (K.mul (co K X (0)) (co K X (11))) (K.add (K.mul (co K X (1)) (co K X (12))) (K.add (K.mul (co K X (2)) (co K X (13))) (K.add (K.mul (co K X (3)) (co K X (14))) (K.add (K.mul (co K X (4)) (co K X (15))) (K.add (K.mul (co K X (5)) (co K X (16))) (ReduceCBM.Mt K 6 11 X))))))) := by rw [st0_11, st1_11, st2_11, st3_11, st4_11, st5_11]
  have mx3_2 : ReduceCBM.Mt K 3 2 X = (K.add (K.mul (co K X (3)) (co K X (5))) (K.add (K.mul (co K X (4)) (co K X (6))) (K.add (K.mul (co K X (5)) (co K X (7))) (ReduceCBM.Mt K 6 2 X)))) := by rw [st3_2, st4_2, st5_2]
  have mx3_3 : ReduceCBM.Mt K 3 3 X = (K.add (K.mul (co K X (3)) (co K X (6))) (K.add (K.mul (co K X (4)) (co K X (7))) (K.add (K.mul (co K X (5)) (co K X (8))) (ReduceCBM.Mt K 6 3 X)))) := by rw [st3_3, st4_3, st5_3]
  have mx3_4 : ReduceCBM.Mt K 3 4 X = (K.add (K.mul (co K X (3)) (co K X (7))) (K.add (K.mul (co K X (4)) (co K X (8))) (K.add (K.mul (co K X (5)) (co K X (9))) (ReduceCBM.Mt K 6 4 X)))) := by rw [st3_4, st4_4, st5_4]
  have mx3_5 : ReduceCBM.Mt K 3 5 X = (K.add (K.mul (co K X (3)) (co K X (8))) (K.add (K.mul (co K X (4)) (co K X (9))) (K.add (K.mul (co K X (5)) (co K X (10))) (ReduceCBM.Mt K 6 5 X)))) := by rw [st3_5, st4_5, st5_5]
  have mx3_6 : ReduceCBM.Mt K 3 6 X = (K.add (K.mul (co K X (3)) (co K X (9))) (K.add (K.mul (co K X (4)) (co K X (10))) (K.add (K.mul (co K X (5)) (co K X (11))) (ReduceCBM.Mt K 6 6 X)))) := by rw [st3_6, st4_6, st5_6]
  have mx3_7 : ReduceCBM.Mt K 3 7 X = (K.add (K.mul (co K X (3)) (co K X (10))) (K.add (K.mul (co K X (4)) (co K X (11))) (K.add (K.mul (co K X (5)) (co K X (12))) (ReduceCBM.Mt K 6 7 X)))) := by rw [st3_7, st4_7, st5_7]
  have mx3_8 : ReduceCBM.Mt K 3 8 X = (K.add (K.mul (co K X (3)) (co K X (11))) (K.add (K.mul (co K X (4)) (co K X (12))) (K.add (K.mul (co K X (5)) (co K X (13))) (ReduceCBM.Mt K 6 8 X)))) := by rw [st3_8, st4_8, st5_8]
  have mx3_9 : ReduceCBM.Mt K 3 9 X = (K.add (K.mul (co K X (3)) (co K X (12))) (K.add (K.mul (co K X (4)) (co K X (13))) (K.add (K.mul (co K X (5)) (co K X (14))) (ReduceCBM.Mt K 6 9 X)))) := by rw [st3_9, st4_9, st5_9]
  have mx3_10 : ReduceCBM.Mt K 3 10 X = (K.add (K.mul (co K X (3)) (co K X (13))) (K.add (K.mul (co K X (4)) (co K X (14))) (K.add (K.mul (co K X (5)) (co K X (15))) (ReduceCBM.Mt K 6 10 X)))) := by rw [st3_10, st4_10, st5_10]
  have mx3_11 : ReduceCBM.Mt K 3 11 X = (K.add (K.mul (co K X (3)) (co K X (14))) (K.add (K.mul (co K X (4)) (co K X (15))) (K.add (K.mul (co K X (5)) (co K X (16))) (ReduceCBM.Mt K 6 11 X)))) := by rw [st3_11, st4_11, st5_11]
  have mx4_1 : ReduceCBM.Mt K 4 1 X = (K.add (K.mul (co K X (4)) (co K X (5))) (K.add (K.mul (co K X (5)) (co K X (6))) (ReduceCBM.Mt K 6 1 X))) := by rw [st4_1, st5_1]
  have mx4_2 : ReduceCBM.Mt K 4 2 X = (K.add (K.mul (co K X (4)) (co K X (6))) (K.add (K.mul (co K X (5)) (co K X (7))) (ReduceCBM.Mt K 6 2 X))) := by rw [st4_2, st5_2]
  have mx4_3 : ReduceCBM.Mt K 4 3 X = (K.add (K.mul (co K X (4)) (co K X (7))) (K.add (K.mul (co K X (5)) (co K X (8))) (ReduceCBM.Mt K 6 3 X))) := by rw [st4_3, st5_3]
  have mx4_4 : ReduceCBM.Mt K 4 4 X = (K.add (K.mul (co K X (4)) (co K X (8))) (K.add (K.mul (co K X (5)) (co K X (9))) (ReduceCBM.Mt K 6 4 X))) := by rw [st4_4, st5_4]
  have mx4_5 : ReduceCBM.Mt K 4 5 X = (K.add (K.mul (co K X (4)) (co K X (9))) (K.add (K.mul (co K X (5)) (co K X (10))) (ReduceCBM.Mt K 6 5 X))) := by rw [st4_5, st5_5]
  have mx4_6 : ReduceCBM.Mt K 4 6 X = (K.add (K.mul (co K X (4)) (co K X (10))) (K.add (K.mul (co K X (5)) (co K X (11))) (ReduceCBM.Mt K 6 6 X))) := by rw [st4_6, st5_6]
  have mx4_7 : ReduceCBM.Mt K 4 7 X = (K.add (K.mul (co K X (4)) (co K X (11))) (K.add (K.mul (co K X (5)) (co K X (12))) (ReduceCBM.Mt K 6 7 X))) := by rw [st4_7, st5_7]
  have mx4_8 : ReduceCBM.Mt K 4 8 X = (K.add (K.mul (co K X (4)) (co K X (12))) (K.add (K.mul (co K X (5)) (co K X (13))) (ReduceCBM.Mt K 6 8 X))) := by rw [st4_8, st5_8]
  have mx4_9 : ReduceCBM.Mt K 4 9 X = (K.add (K.mul (co K X (4)) (co K X (13))) (K.add (K.mul (co K X (5)) (co K X (14))) (ReduceCBM.Mt K 6 9 X))) := by rw [st4_9, st5_9]
  have mx4_10 : ReduceCBM.Mt K 4 10 X = (K.add (K.mul (co K X (4)) (co K X (14))) (K.add (K.mul (co K X (5)) (co K X (15))) (ReduceCBM.Mt K 6 10 X))) := by rw [st4_10, st5_10]
  have mx5_0 : ReduceCBM.Mt K 5 0 X = (K.add (K.mul (co K X (5)) (co K X (5))) (ReduceCBM.Mt K 6 0 X)) := by rw [st5_0]
  have mx5_1 : ReduceCBM.Mt K 5 1 X = (K.add (K.mul (co K X (5)) (co K X (6))) (ReduceCBM.Mt K 6 1 X)) := by rw [st5_1]
  have mx5_2 : ReduceCBM.Mt K 5 2 X = (K.add (K.mul (co K X (5)) (co K X (7))) (ReduceCBM.Mt K 6 2 X)) := by rw [st5_2]
  have mx5_3 : ReduceCBM.Mt K 5 3 X = (K.add (K.mul (co K X (5)) (co K X (8))) (ReduceCBM.Mt K 6 3 X)) := by rw [st5_3]
  have mx5_4 : ReduceCBM.Mt K 5 4 X = (K.add (K.mul (co K X (5)) (co K X (9))) (ReduceCBM.Mt K 6 4 X)) := by rw [st5_4]
  have mx5_5 : ReduceCBM.Mt K 5 5 X = (K.add (K.mul (co K X (5)) (co K X (10))) (ReduceCBM.Mt K 6 5 X)) := by rw [st5_5]
  have mx5_6 : ReduceCBM.Mt K 5 6 X = (K.add (K.mul (co K X (5)) (co K X (11))) (ReduceCBM.Mt K 6 6 X)) := by rw [st5_6]
  have mx5_7 : ReduceCBM.Mt K 5 7 X = (K.add (K.mul (co K X (5)) (co K X (12))) (ReduceCBM.Mt K 6 7 X)) := by rw [st5_7]
  have mx5_8 : ReduceCBM.Mt K 5 8 X = (K.add (K.mul (co K X (5)) (co K X (13))) (ReduceCBM.Mt K 6 8 X)) := by rw [st5_8]
  have mx5_9 : ReduceCBM.Mt K 5 9 X = (K.add (K.mul (co K X (5)) (co K X (14))) (ReduceCBM.Mt K 6 9 X)) := by rw [st5_9]
  have key : LibRing.sub K (K.mul (cgc K X X 10) (cgc K X X 10)) (K.mul (cgc K X X 9) (cgc K X X 11))
      = K.add (K.mul (ReduceCBM.Mt K 5 0 X) (ReduceCBM.Mt K 5 0 X)) (K.add (K.add (CBLib.cbv K (co K X) (VGoodPair.yv K X) (Fa K X) (Ga K X) (X.length + 2 + 4) 5) (CBLib.cbv K (co K X) (VGoodPair.yv K X) (Fa K X) (Ga K X) (X.length + 2 + 4) 5)) (K.add (CBLib.cbv K (co K X) (VGoodPair.yv K X) (Fa K X) (Ga K X) (X.length + 2 + 4) 5) (CBLib.cbv K (co K X) (VGoodPair.yv K X) (Fa K X) (Ga K X) (X.lengt  -- (+21 chars; full line in the Zenodo source)
    rw [cva, ipa10x, ipa10y, ipa11x, ipa11y, ipa20x, ipa20y, ipa21x, ipa21y, ipa30x, ipa30y, ipa31x, ipa31y, ipa40x, ipa40y, ipa41x, ipa41y, ipa50x, ipa50y, ipa51x, ipa51y, hc11, hc10, hc9, hc8, hc7, hc6, hc5, hc4, hc3, hc2, hc1, hc0, CBLib.rr_mt K 0 X, CBLib.rr_mt K 1 X, CBLib.rr_mt K 2 X, CBLib.rr_mt K 3 X, CBLib.rr_mt K 4 X, CBLib.rr_mt K 5 X, CBLib.rr_mt K 6 X, CBLib.rr_mt K 7 X, CBLib.rr_mt K  -- (+363 chars; full line in the Zenodo source)
    unfold LibRing.sub
    ring_z
  rw [key]
  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)
  ring_z

/-- **(A) at n = 9** for every v-good list of every length -/
theorem a9 (X : List R) (hg : L1Full.Good K O X) :
    O.le (K.mul (LibRing.castN K ((9 + 2) * (9 + 2))) (K.mul (cgc K X X 9) (cgc K X X (9 + 2))))
      (K.mul (LibRing.castN K ((9 + 1) * (9 + 3))) (K.mul (cgc K X X (9 + 1)) (cgc K X X (9 + 1)))) :=
  ReduceCBM.a_of_cbm K O X hg 9 5 0 (by decide) rfl (cbm10 K O X hg)

end CBM10

#print axioms CBM10.kna_0_0
#print axioms CBM10.kna_0_1
#print axioms CBM10.kna_0_2
#print axioms CBM10.kna_0_3
#print axioms CBM10.kna_0_4
#print axioms CBM10.kna_0_5
#print axioms CBM10.kna_0_6
#print axioms CBM10.kna_0_7
#print axioms CBM10.kna_0_8
#print axioms CBM10.kna_0_9
#print axioms CBM10.kna_0_10
#print axioms CBM10.kna_0_11
#print axioms CBM10.kna_0_12
#print axioms CBM10.kna_1_0
#print axioms CBM10.kna_1_1
#print axioms CBM10.kna_1_2
#print axioms CBM10.kna_1_3
#print axioms CBM10.kna_1_4
#print axioms CBM10.kna_1_5
#print axioms CBM10.kna_1_6
#print axioms CBM10.kna_1_7
#print axioms CBM10.kna_1_8
#print axioms CBM10.kna_1_9
#print axioms CBM10.kna_1_10
#print axioms CBM10.kna_1_11
#print axioms CBM10.kna_1_12
#print axioms CBM10.kna_2_0
#print axioms CBM10.kna_2_1
#print axioms CBM10.kna_2_2
#print axioms CBM10.kna_2_3
#print axioms CBM10.kna_2_4
#print axioms CBM10.kna_2_5
#print axioms CBM10.kna_2_6
#print axioms CBM10.kna_2_7
#print axioms CBM10.kna_2_8
#print axioms CBM10.kna_2_9
#print axioms CBM10.kna_2_10
#print axioms CBM10.kna_2_11
#print axioms CBM10.kna_2_12
#print axioms CBM10.kna_3_0
#print axioms CBM10.kna_3_1
#print axioms CBM10.kna_3_2
#print axioms CBM10.kna_3_3
#print axioms CBM10.kna_3_4
#print axioms CBM10.kna_3_5
#print axioms CBM10.kna_3_6
#print axioms CBM10.kna_3_7
#print axioms CBM10.kna_3_8
#print axioms CBM10.kna_3_9
#print axioms CBM10.kna_3_10
#print axioms CBM10.kna_3_11
#print axioms CBM10.kna_3_12
#print axioms CBM10.kna_s_0
#print axioms CBM10.kna_s_1
#print axioms CBM10.kna_s_2
#print axioms CBM10.kna_s_3
#print axioms CBM10.kna_s_4
#print axioms CBM10.kna_s_5
#print axioms CBM10.kna_s_6
#print axioms CBM10.kna_s_7
#print axioms CBM10.kna_s_8
#print axioms CBM10.kna_s_9
#print axioms CBM10.kna_s_10
#print axioms CBM10.kna_s_11
#print axioms CBM10.kna_s_12
#print axioms CBM10.kna_0_s
#print axioms CBM10.kna_1_s
#print axioms CBM10.kna_2_s
#print axioms CBM10.kna_3_s
#print axioms CBM10.kna_s_s
#print axioms CBM10.knna
#print axioms CBM10.cbnna
#print axioms CBM10.cbm10
#print axioms CBM10.a9