/-Copyright (c) 2026 Carles MarΓn. All rights reserved.Released under Apache 2.0 license as described in the file LICENSE.Authors: Carles MarΓn, Claude (AI assistant)-/module public import Formalizations.Combinatorics.Caballero.AngularGeneralApublic import Formalizations.Combinatorics.Caballero.AngularWindows /-!# The upper tail of the row multiset determines the window sums (Caballero, Part IV, Lemma 8) Lemma 8 of Part IV, on the cell vectors and the row multiset Z^r of the base module(CaballeroGeneralA). For an integer E >= 1 and two non-decreasing integer vectors v, v' with entriesin [-1, b] and spreads at most E, if v_a <= b - E and Z^r(v) = Z^r(v'), then the window sums S_k =sum_{h=k-a}^{k} v_h agree for 0 <= k <= a. The proof is a counting version of the paper'sgenerating-function argument: for each row, the values above m(E-1), m = a+1, form an arithmeticprogression of step m, so the difference of the multiplicities at x and x - m, for x > mE, equalsminus the number of rows whose top value m(b+1) - S_k equals x. The multiset of these top values isthen the same for v and v', and monotonicity in k identifies them termwise. Stated for all a, b (thepaper fixes a >= 2, b >= 1; the argument does not use it). The vectors are Fin (2a+1) -> Z with thecoordinate h stored at index h + a; the hypotheses on entries, monotonicity and spread are stated onthe vector, and the margin v_a <= b - E uses coord v a. The margin is required only for v, as in thepaper; v' has only the entry and spread bounds. The positivity of Lambda, theta (realisability) isnot needed here and not assumed. What is not formalised: Lemma 9 and the central injectivity theorembuilt from them. ## Main results * `CaballeroGeneralA.upper_tail`: Lemma 8: v_a <= b - E, spreads <= E, entries in [-1, b] and Z^r(v) = Z^r(v') imply S_k(v) = S_k(v') for 0 <= k <= a* `CaballeroGeneralA.windowSum`: the window sum S_k(v) = sum_{h=k-a}^{k} v_h* `CaballeroGeneralA.count_diff`: for x > m E the multiplicity of x minus that of x - m in Z^r(u) is minus the number of rows with top value m(b+1) - S_k equal to x* `CaballeroGeneralA.R_hi`: R_i(j) = (a+1) j - S_k for j at least the largest entry of the window (the cap b+1 never acts) ## References * C. MarΓn, How Many Dyck Words Do the Divisors of an Integer Make? IV. The Primary Phase of the Ambiguity Profile for General a, draft (2 December 2026), unpublished.* C. MarΓn, How Many Dyck Words Do the Divisors of an Integer Make? Exact Counts and Quasipolynomial Growth (Part I), Zenodo (2026), doi:10.5281/zenodo.24536372 (concept DOI).-/ @[expose] public section namespace CaballeroGeneralA open Finset /-- The window sum `S_k(v) = β_{h=k-a}^{k} v_h` (the window `W_k = {k-a, β¦, k}`). -/def windowSum {a : β} (v : Vec a) (k : β) : β€ := β h β Icc ((k : β€) - a) (k : β€), coord v h /-- The hypotheses of Lemma 8 on one vector, read through `coord`. -/structure Good (a b : β) (E : β€) (u : Vec a) : Prop where bd : β h : β€, -(a : β€) β€ h β h β€ a β -1 β€ coord u h β§ coord u h β€ (b : β€) mono : β h h' : β€, -(a : β€) β€ h β h β€ h' β h' β€ a β coord u h β€ coord u h' sp : β h h' : β€, -(a : β€) β€ h β h β€ a β -(a : β€) β€ h' β h' β€ a β coord u h - coord u h' β€ E theorem coord_eq {a : β} (u : Vec a) (h : β€) (h1 : -(a : β€) β€ h) (h2 : h β€ a) : coord u h = u β¨(h + a).toNat, by omegaβ© := by unfold coord have hh : -(a : β€) β€ h β§ h β€ a := β¨h1, h2β© simp [hh] theorem good_of {a b : β} {E : β€} {u : Vec a} (hm : Monotone u) (hb : β k, -1 β€ u k β§ u k β€ (b : β€)) (hs : spread u β€ E) : Good a b E u where bd := fun h h1 h2 => by rw [coord_eq u h h1 h2]; exact hb _ mono := fun h h' h1 h2 h3 => by rw [coord_eq u h h1 (by omega), coord_eq u h' (by omega) h3] apply hm rw [Fin.mk_le_mk] omega sp := fun h h' h1 h2 h1' h2' => by rw [coord_eq u h h1 h2, coord_eq u h' h1' h2'] have e1 := Finset.le_sup' u (mem_univ (β¨(h + a).toNat, by omegaβ© : Fin (2 * a + 1))) have e2 := Finset.inf'_le u (mem_univ (β¨(h' + a).toNat, by omegaβ© : Fin (2 * a + 1))) unfold spread at hs linarith section Rows variable {a b : β} {E : β€} {u : Vec a} theorem windowSum_row (i : β) (hi : i β€ a) : windowSum u (a - i) = β h β Icc (-(i : β€)) ((a : β€) - i), coord u h := by unfold windowSum have e : Icc (((a - i : β) : β€) - a) ((a - i : β) : β€) = Icc (-(i : β€)) ((a : β€) - i) := by congr 1 <;> omega rw [e] theorem card_row (i : β) (hi : i β€ a) : (Icc (-(i : β€)) ((a : β€) - i)).card = a + 1 := by simp only [Int.card_Icc] omega theorem R_sum (G : Good a b E u) (i j : β) (hi : i β€ a) (hj : j β€ b) : R a b u i j = β h β Icc (-(i : β€)) ((a : β€) - i), max 0 ((j : β€) - coord u h) := by unfold R apply sum_congr rfl intro h hh rw [mem_Icc] at hh have := G.bd h (by omega) (by omega) omega theorem R_hi (G : Good a b E u) (i j : β) (hi : i β€ a) (hj : j β€ b) (hM : coord u ((a : β€) - i) β€ j) : R a b u i j = ((a : β€) + 1) * j - windowSum u (a - i) := by rw [R_sum G i j hi hj, windowSum_row i hi] have : β h β Icc (-(i : β€)) ((a : β€) - i), max 0 ((j : β€) - coord u h) = (j : β€) - coord u h := by intro h hh rw [mem_Icc] at hh have := G.mono h ((a : β€) - i) (by omega) hh.2 (by omega) omega rw [sum_congr rfl this, sum_sub_distrib, sum_const, card_row i hi] simp theorem R_lo (G : Good a b E u) (hE : 1 β€ E) (i j : β) (hi : i β€ a) (hj : j β€ b) (hM : (j : β€) < coord u ((a : β€) - i)) : R a b u i j β€ ((a : β€) + 1) * (E - 1) := by rw [R_sum G i j hi hj] have h1 : β h β Icc (-(i : β€)) ((a : β€) - i), max 0 ((j : β€) - coord u h) β€ (Icc (-(i : β€)) ((a : β€) - i)).card β’ (E - 1) := by apply sum_le_card_nsmul intro h hh rw [mem_Icc] at hh have := G.sp ((a : β€) - i) h (by omega) (by omega) (by omega) (by omega) omega rw [card_row i hi] at h1 simpa [nsmul_eq_mul] using h1 theorem S_lower (G : Good a b E u) (hE : 1 β€ E) (i : β) (hi : i β€ a) : ((a : β€) + 1) * (max (coord u ((a : β€) - i)) 0 - E) β€ windowSum u (a - i) := by rw [windowSum_row i hi] have h1 : (Icc (-(i : β€)) ((a : β€) - i)).card β’ (max (coord u ((a : β€) - i)) 0 - E) β€ β h β Icc (-(i : β€)) ((a : β€) - i), coord u h := by apply card_nsmul_le_sum intro h hh rw [mem_Icc] at hh have := G.sp ((a : β€) - i) h (by omega) (by omega) (by omega) (by omega) have := G.bd h (by omega) (by omega) omega rw [card_row i hi] at h1 simpa [nsmul_eq_mul] using h1 theorem S_upper (G : Good a b E u) (i : β) (hi : i β€ a) : windowSum u (a - i) β€ ((a : β€) + 1) * coord u a := by rw [windowSum_row i hi] have h1 : β h β Icc (-(i : β€)) ((a : β€) - i), coord u h β€ (Icc (-(i : β€)) ((a : β€) - i)).card β’ coord u a := by apply sum_le_card_nsmul intro h hh rw [mem_Icc] at hh exact G.mono h a (by omega) (by omega) le_rfl rw [card_row i hi] at h1 simpa [nsmul_eq_mul] using h1 theorem windowSum_mono (G : Good a b E u) : β k k' : β, k β€ k' β k' β€ a β windowSum u k β€ windowSum u k' := by intro k k' hk induction k', hk using Nat.le_induction with | base => intro _; exact le_rfl | succ n hn ih => intro h have := ih (by omega) have st := CaballeroWindows.window_step (fun h => coord u h) a n have e1 : windowSum u (n + 1) - windowSum u n = coord u ((n : β€) + 1) - coord u ((n : β€) - a) := st have := G.mono ((n : β€) - a) ((n : β€) + 1) (by omega) (by omega) (by omega) linarith end Rows /-! ### Counting -/ theorem count_map_eq_sum {Ξ± : Type*} (s : Finset Ξ±) (F : Ξ± β β€) (x : β€) : Multiset.count x (s.val.map F) = β p β s, if F p = x then 1 else 0 := by classical rw [Multiset.count_map] have : (Multiset.filter (fun p => x = F p) s.val) = (s.filter fun p => x = F p).val := rfl rw [this] show (s.filter fun p => x = F p).card = _ rw [card_filter] apply sum_congr rfl intro p _ simp only [eq_comm] theorem count_Zr {a b : β} (u : Vec a) (x : β€) : (Multiset.count x (Zr a b u) : β€) = β i β range (a + 1), (((range (b + 1)).filter fun j : β => R a b u i j = x).card : β€) := by classical unfold Zr rw [count_map_eq_sum, sum_product] push_cast apply sum_congr rfl intro i _ rw [card_filter] push_cast rfl theorem count_M {n : β} (F : β β β€) (x : β€) : (Multiset.count x ((range n).val.map F) : β€) = β i β range n, if F i = x then (1 : β€) else 0 := by rw [count_map_eq_sum] push_cast rfl /-- Number of elements of an arithmetic progression of a row, in the case where `y + S = m q`. -/theorem ap_card_of {b : β} {m lo S y q : β€} (hm : 0 < m) (hlo : 0 β€ lo) (hq : y + S = m * q) : (((range (b + 1)).filter fun j : β => lo β€ (j : β€) β§ m * j - S = y).card : β€) = if lo β€ q β§ q β€ b then 1 else 0 := by have key : β j : β, m * j - S = y β (j : β€) = q := by intro j constructor Β· intro h exact mul_left_cancelβ (ne_of_gt hm) (by linarith) Β· intro h rw [h] linarith split_ifs with hc Β· have : ((range (b + 1)).filter fun j : β => lo β€ (j : β€) β§ m * j - S = y) = {q.toNat} := by ext j simp only [mem_filter, mem_range, mem_singleton, key] omega rw [this] simp Β· have : ((range (b + 1)).filter fun j : β => lo β€ (j : β€) β§ m * j - S = y) = β := by rw [filter_eq_empty_iff] intro j hj rw [mem_range] at hj rw [key] omega rw [this] simp theorem ap_card_none {b : β} {m lo S y : β€} (hno : β q : β€, y + S β m * q) : (((range (b + 1)).filter fun j : β => lo β€ (j : β€) β§ m * j - S = y).card : β€) = 0 := by have : ((range (b + 1)).filter fun j : β => lo β€ (j : β€) β§ m * j - S = y) = β := by rw [filter_eq_empty_iff] intro j _ h exact hno j (by linarith [h.2]) rw [this] simp section Rows2 variable {a b : β} {E : β€} {u : Vec a} /-- For `y > m(E-1)`, row `i` hits `y` exactly on the progression `m j - S`, `j β₯ max M 0`. -/theorem row_count (G : Good a b E u) (hE : 1 β€ E) (i : β) (hi : i β€ a) (y : β€) (hy : ((a : β€) + 1) * (E - 1) < y) : ((range (b + 1)).filter fun j : β => R a b u i j = y) = (range (b + 1)).filter fun j : β => max (coord u ((a : β€) - i)) 0 β€ (j : β€) β§ ((a : β€) + 1) * j - windowSum u (a - i) = y := by apply filter_congr intro j hj rw [mem_range] at hj by_cases hM : coord u ((a : β€) - i) β€ j Β· rw [R_hi G i j hi (by omega) hM] constructor Β· intro h exact β¨by omega, hβ© Β· intro h exact h.2 Β· have := R_lo G hE i j hi (by omega) (by omega) constructor Β· intro h omega Β· intro h omega theorem row_diff (G : Good a b E u) (hE : 1 β€ E) (i : β) (hi : i β€ a) (x : β€) (hx : ((a : β€) + 1) * E < x) : (((range (b + 1)).filter fun j : β => R a b u i j = x).card : β€) - (((range (b + 1)).filter fun j : β => R a b u i j = x - ((a : β€) + 1)).card : β€) = - (if ((a : β€) + 1) * ((b : β€) + 1) - windowSum u (a - i) = x then (1 : β€) else 0) := by have hm : (0 : β€) < (a : β€) + 1 := by omega rw [row_count G hE i hi x (by nlinarith), row_count G hE i hi _ (by nlinarith)] have hS := S_lower G hE i hi set S := windowSum u (a - i) with hSdef set lo := max (coord u ((a : β€) - i)) 0 with hlo by_cases hd : β q : β€, x + S = ((a : β€) + 1) * q Β· obtain β¨q, hqβ© := hd have hq' : x - ((a : β€) + 1) + S = ((a : β€) + 1) * (q - 1) := by linarith rw [ap_card_of hm (le_max_right _ _) hq, ap_card_of hm (le_max_right _ _) hq'] have hlq : lo < q := by by_contra hc push Not at hc have : ((a : β€) + 1) * q β€ ((a : β€) + 1) * lo := mul_le_mul_of_nonneg_left hc hm.le nlinarith have hiff : ((a : β€) + 1) * ((b : β€) + 1) - S = x β q = (b : β€) + 1 := by constructor Β· intro h exact mul_left_cancelβ (ne_of_gt hm) (by linarith) Β· intro h rw [h] at hq linarith simp only [hiff] split_ifs <;> omega Β· push Not at hd rw [ap_card_none hd, ap_card_none (fun q h => hd (q + 1) (by linarith))] have hn : Β¬ (((a : β€) + 1) * ((b : β€) + 1) - S = x) := fun h => hd ((b : β€) + 1) (by linarith) simp [hn] theorem count_diff (G : Good a b E u) (hE : 1 β€ E) (x : β€) (hx : ((a : β€) + 1) * E < x) : (Multiset.count x (Zr a b u) : β€) - Multiset.count (x - ((a : β€) + 1)) (Zr a b u) = - β i β range (a + 1), if ((a : β€) + 1) * ((b : β€) + 1) - windowSum u (a - i) = x then (1 : β€) else 0 := by rw [count_Zr, count_Zr, β sum_sub_distrib, β sum_neg_distrib] apply sum_congr rfl intro i hi rw [mem_range] at hi exact row_diff G hE i (by omega) x hx end Rows2 theorem map_range_eq_of_mono (n : β) (f g : β β β€) (hf : β i j, i β€ j β j < n β f i β€ f j) (hg : β i j, i β€ j β j < n β g i β€ g j) (h : (range n).val.map f = (range n).val.map g) : β i < n, f i = g i := by have hp : List.Perm ((List.range n).map f) ((List.range n).map g) := Multiset.coe_eq_coe.mp h have pf : ((List.range n).map f).Pairwise (Β· β€ Β·) := by rw [List.pairwise_map] exact (List.pairwise_lt_range (n := n)).imp_of_mem fun {i j} hi hj hij => hf i j hij.le (List.mem_range.mp hj) have pg : ((List.range n).map g).Pairwise (Β· β€ Β·) := by rw [List.pairwise_map] exact (List.pairwise_lt_range (n := n)).imp_of_mem fun {i j} hi hj hij => hg i j hij.le (List.mem_range.mp hj) have e := hp.eq_of_pairwise' pf pg intro i hi have := congrArg (fun l => l[i]?) e simpa [List.getElem?_map, hi] using this /-- Lemma 8 (upper tail). Let `E β₯ 1` and `v, v'` non-decreasing integer vectors with entries in`[-1, b]` and spreads at most `E`. If `v_a β€ b - E` and `ZΚ³(v) = ZΚ³(v')`, then`S_k(v) = S_k(v')` for `0 β€ k β€ a`. -/theorem upper_tail (a b : β) (E : β€) (hE : 1 β€ E) (v v' : Vec a) (hmv : Monotone v) (hmv' : Monotone v') (hv : β k, -1 β€ v k β§ v k β€ (b : β€)) (hv' : β k, -1 β€ v' k β§ v' k β€ (b : β€)) (hsv : spread v β€ E) (hsv' : spread v' β€ E) (hva : coord v a β€ (b : β€) - E) (hr : Zr a b v = Zr a b v') : β k : β, k β€ a β windowSum v k = windowSum v' k := by classical have G := good_of (b := b) hmv hv hsv have G' := good_of (b := b) hmv' hv' hsv' have hm : (0 : β€) < (a : β€) + 1 := by omega set e : Vec a β β β β€ := fun u i => ((a : β€) + 1) * ((b : β€) + 1) - windowSum u (a - i) with he -- the counts of the exponents above `m E` agree have hcount : β x : β€, ((a : β€) + 1) * E < x β Multiset.count x ((range (a + 1)).val.map (e v)) = Multiset.count x ((range (a + 1)).val.map (e v')) := by intro x hx have d1 := count_diff G hE x hx have d2 := count_diff G' hE x hx rw [hr] at d1 have : (Multiset.count x ((range (a + 1)).val.map (e v)) : β€) = Multiset.count x ((range (a + 1)).val.map (e v')) := by rw [count_M, count_M] simp only [he] linarith exact_mod_cast this -- all exponents of `v` exceed `m E` have hbig : β y β (range (a + 1)).val.map (e v), ((a : β€) + 1) * E < y := by intro y hy rw [Multiset.mem_map] at hy obtain β¨i, hi, rflβ© := hy have hi' : i β€ a := by have := (mem_range.mp hi) omega have := S_upper G i hi' simp only [he] nlinarith set p : β€ β Prop := fun y => ((a : β€) + 1) * E < y with hp have hfilt : Multiset.filter p ((range (a + 1)).val.map (e v)) = Multiset.filter p ((range (a + 1)).val.map (e v')) := by ext x rw [Multiset.count_filter, Multiset.count_filter] by_cases hx : p x Β· simp only [hx, βreduceIte] exact hcount x hx Β· simp only [hx, βreduceIte] have hv_self : Multiset.filter p ((range (a + 1)).val.map (e v)) = (range (a + 1)).val.map (e v) := Multiset.filter_eq_self.mpr hbig have hcard : Multiset.card (Multiset.filter p ((range (a + 1)).val.map (e v'))) = Multiset.card ((range (a + 1)).val.map (e v')) := by rw [β hfilt, hv_self] simp have hall : β y β (range (a + 1)).val.map (e v'), p y := by have h0 := Multiset.filter_add_not p ((range (a + 1)).val.map (e v')) have h1 := congrArg Multiset.card h0 rw [Multiset.card_add, hcard] at h1 have h2 : Multiset.card (Multiset.filter (fun a => Β¬ p a) ((range (a + 1)).val.map (e v'))) = 0 := by omega rw [Multiset.card_eq_zero, Multiset.filter_eq_nil] at h2 intro y hy by_contra hc exact h2 y hy hc have hmeq : (range (a + 1)).val.map (e v) = (range (a + 1)).val.map (e v') := by rw [β hv_self, hfilt, Multiset.filter_eq_self.mpr hall] have hmon : β (u : Vec a), Good a b E u β β i j, i β€ j β j < a + 1 β e u i β€ e u j := by intro u Gu i j hij hj simp only [he] have := windowSum_mono Gu (a - j) (a - i) (by omega) (by omega) linarith have hfin := map_range_eq_of_mono (a + 1) (e v) (e v') (hmon v G) (hmon v' G') hmeq intro k hk have := hfin (a - k) (by omega) simp only [he] at this have e1 : a - (a - k) = k := by omega rw [e1] at this linarith end CaballeroGeneralA