import Std/-! Integer identities for the greatest convex minorant of a binomial tail.The functions use only Pascal's recurrence and include every natural n,k. -/namespace BinomialTaildef choose : Nat → Nat → Nat| 0, 0 => 1| 0, _ + 1 => 0| _ + 1, 0 => 1| n + 1, k + 1 => choose n k + choose n (k + 1)def sumThrough (n : Nat) : Nat → Nat| 0 => choose n 0| k + 1 => sumThrough n k + choose n (k + 1)theorem choose_zero (n : Nat) : choose n 0 = 1 := bycases n <;> rfl/-- After dividing by 2^(n+1), this is f(n) - f(n+1) = C(n,k)/2^(n+1). -/theorem first_difference (n k : Nat) :2 * sumThrough n k = sumThrough (n + 1) k + choose n k := byinduction k with| zero => simp [sumThrough, choose_zero]| succ k ih =>simp only [sumThrough, choose]omega/-- Division by natural factors is avoided, including at the endpoints. -/theorem successive_binomials (n k : Nat) :(n + 1) * choose n k = (n + 1 - k) * choose (n + 1) k := byinduction n generalizing k with| zero =>cases k with| zero => decide| succ k => simp [choose]| succ n ih =>cases k with| zero => simp [choose_zero]| succ k =>by_cases hk : k ≤ n· have h1 := ih khave h2 := ih (k + 1)have hd1 : n + 1 - k = n - k + 1 := by omegahave hd2 : n + 1 - (k + 1) = n - k := by omegahave hd3 : n + 1 + 1 - (k + 1) = n - k + 1 := by omegarw [hd1] at h1rw [hd2] at h2rw [hd3]simp only [choose] at h2 ⊢grind· have hd : n + 1 + 1 - (k + 1) = 0 := by omegahave h2 := ih (k + 1)have hd2 : n + 1 - (k + 1) = 0 := by omegarw [hd2, Nat.zero_mul] at h2have hz : choose n (k + 1) = 0 := byrcases Nat.mul_eq_zero.mp h2 with h | h· omega· exact hby_cases hkn : k = n + 1· subst khave h1 := ih (n + 1)simp at h1have hzero : choose n (n + 1) = 0 := byrcases Nat.mul_eq_zero.mp h1 with h | h· omega· exact hsimp [choose, hz, hzero]· have h1 := ih khave hd1 : n + 1 - k = 0 := by omegarw [hd1, Nat.zero_mul] at h1have hzero : choose n k = 0 := byrcases Nat.mul_eq_zero.mp h1 with h | h· omega· exact hsimp [choose, hz, hzero, hd]#print axioms first_difference#print axioms successive_binomialsend BinomialTail