πŸ›οΈ Atlasβœ’οΈFormalizations.Combinatorics.Caballero.AngularUpperTail view on GitHub β†—20 theorems
/-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