/-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 Mathlib.Algebra.BigOperators.Ring.Finsetpublic import Mathlib.Algebra.Order.BigOperators.Group.Finsetpublic import Mathlib.Algebra.Order.Floor.Ringpublic import Mathlib.Data.Int.Intervalpublic import Mathlib.Algebra.Order.Archimedean.Real.Basic /-!# Window sums determine a floor form (Caballero, Part IV, Lemma E2) Lemma 9 of Part IV: for real alpha, alpha', theta, theta' and a natural number a, the vectors v_h =floor(alpha + h theta), v'_h = floor(alpha' + h theta') (-a <= h <= a, stored as Fin (2a+1) -> Z asin the base module of Part IV) with equal window sums S_k = sum_{h=k-a}^{k} v_h for 0 <= k <= a areequal. No sign condition on theta and no clipping is assumed (the paper's statement says realparameters). The proof follows the paper: the difference delta is a-periodic with period a+1 on thefirst window, has zero sum, and an affine function cannot change sign as +,-,+ or -,+,-. Vec andcoord are copies of CaballeroGeneralA.Vec/coord (same body); a = 0 is allowed. ## Main results * `CaballeroWindows.windowSums_determine_floorVec`: Lemma 9: equal window sums of two floor forms force the forms to be equal* `CaballeroWindows.core`: abstract core: periodic, zero-sum differences without sign pattern +,-,+ or -,+,- vanish on [-a,a] ## 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.-/ @[expose] public section namespace CaballeroWindows open Finset /-- A cell vector `c = (c_h)_{h=-a}^{a}`; the coordinate `c_h` is stored at index `h + a`(same body as `CaballeroGeneralA.Vec`). -/abbrev Vec (a : ℕ) := Fin (2 * a + 1) → ℤ /-- The coordinate `c_h` for `h : ℤ` (same body as `CaballeroGeneralA.coord`). -/def coord {a : ℕ} (c : Vec a) (h : ℤ) : ℤ := if hh : -(a : ℤ) ≤ h ∧ h ≤ a then c ⟨(h + a).toNat, by omega⟩ else 0 /-- The floor form `v_h = ⌊α + hθ⌋`, `-a ≤ h ≤ a` (no clipping, no sign condition on `θ`). -/noncomputable def floorVec (a : ℕ) (α θ : ℝ) : Vec a := fun k => ⌊α + ((((k : ℕ) : ℤ) - a : ℤ) : ℝ) * θ⌋ /-- The window sum `S_k(v) = ∑_{h=k-a}^{k} v_h`. -/def winSum {a : ℕ} (v : Vec a) (k : ℕ) : ℤ := ∑ h ∈ Icc ((k : ℤ) - a) (k : ℤ), coord v h theorem coord_floorVec (a : ℕ) (α θ : ℝ) (h : ℤ) (h1 : -(a : ℤ) ≤ h) (h2 : h ≤ a) : coord (floorVec a α θ) h = ⌊α + (h : ℝ) * θ⌋ := by unfold coord floorVec have e : (((h + a).toNat : ℕ) : ℤ) - a = h := by omega split_ifs with hh · simp only [e] · exact absurd ⟨h1, h2⟩ hh theorem sum_Icc_eq_range (g : ℤ → ℤ) (a k : ℕ) : ∑ h ∈ Icc ((k : ℤ) - a) (k : ℤ), g h = ∑ i ∈ range (a + 1), g ((k : ℤ) - a + i) := by symm apply sum_nbij' (fun i : ℕ => (k : ℤ) - a + i) (fun h : ℤ => (h - ((k : ℤ) - a)).toNat) · intro i hi simp only [mem_range, mem_Icc] at hi ⊢ omega · intro h hh simp only [mem_Icc, mem_range] at hh ⊢ omega · intro i _ omega · intro h hh simp only [mem_Icc] at hh omega · intro i _ rfl theorem window_step (δ : ℤ → ℤ) (a k : ℕ) : (∑ h ∈ Icc (((k + 1 : ℕ) : ℤ) - a) ((k + 1 : ℕ) : ℤ), δ h) - ∑ h ∈ Icc ((k : ℤ) - a) (k : ℤ), δ h = δ ((k : ℤ) + 1) - δ ((k : ℤ) - a) := by rw [sum_Icc_eq_range, sum_Icc_eq_range, sum_range_succ, sum_range_succ'] have e1 : ∑ i ∈ range a, δ (((k + 1 : ℕ) : ℤ) - a + i) = ∑ i ∈ range a, δ ((k : ℤ) - a + ((i + 1 : ℕ) : ℤ)) := sum_congr rfl (fun i _ => by congr 1; push_cast; ring) have e2 : δ (((k + 1 : ℕ) : ℤ) - a + (a : ℕ)) = δ ((k : ℤ) + 1) := by congr 1; push_cast; ring have e3 : δ ((k : ℤ) - a + ((0 : ℕ) : ℤ)) = δ ((k : ℤ) - a) := by congr 1; push_cast; ring rw [e1, e2, e3] ring /-- Sign of a difference of floors. -/theorem lt_of_floor_lt {x y : ℝ} (h : ⌊x⌋ < ⌊y⌋) : x < y := by have h1 : (⌊x⌋ : ℝ) + 1 ≤ ⌊y⌋ := by exact_mod_cast h have h2 := Int.lt_floor_add_one x have h3 := Int.floor_le y linarith /-- An affine function cannot have the sign pattern `+, -, +`. -/theorem affine_no_pmp (c d : ℝ) (x y z : ℤ) (hxy : x < y) (hyz : y < z) (h1 : 0 < c + x * d) (h2 : c + y * d < 0) (h3 : 0 < c + z * d) : False := by have hxy' : (x : ℝ) < y := by exact_mod_cast hxy have hyz' : (y : ℝ) < z := by exact_mod_cast hyz have e : ((z : ℝ) - x) * (c + y * d) = ((z : ℝ) - y) * (c + x * d) + ((y : ℝ) - x) * (c + z * d) := by ring have a1 : 0 < ((z : ℝ) - y) * (c + x * d) := mul_pos (by linarith) h1 have a2 : 0 < ((y : ℝ) - x) * (c + z * d) := mul_pos (by linarith) h3 have a3 : ((z : ℝ) - x) * (c + y * d) < 0 := mul_neg_of_pos_of_neg (by linarith) h2 linarith /-- The abstract core: a difference `δ` with periodicity, zero sum and no sign pattern`+,-,+` has no positive value on `[-a, -1]`. -/theorem no_pos (a : ℕ) (δ : ℤ → ℤ) (hper : ∀ h, -(a : ℤ) ≤ h → h ≤ -1 → δ (h + a + 1) = δ h) (hsum : ∑ h ∈ Icc (-(a : ℤ)) 0, δ h = 0) (hp : ∀ x y z, x < y → y < z → 0 < δ x → δ y < 0 → 0 < δ z → False) (hm : ∀ x y z, x < y → y < z → δ x < 0 → 0 < δ y → δ z < 0 → False) : ∀ h, -(a : ℤ) ≤ h → h ≤ -1 → ¬ 0 < δ h := by intro h h1 h2 hh have hex : ∃ r ∈ Icc (-(a : ℤ)) 0, δ r < 0 := by by_contra hr push Not at hr have := single_le_sum (f := δ) (s := Icc (-(a : ℤ)) 0) (fun i hi => hr i hi) (mem_Icc.mpr ⟨h1, by omega⟩) omega obtain ⟨r, hr, hrneg⟩ := hex rw [mem_Icc] at hr rcases lt_trichotomy r h with hrh | hrh | hrh · have hp' := hper r hr.1 (by omega) exact hm r h (r + a + 1) hrh (by omega) hrneg hh (by omega) · subst hrh; omega · have hp' := hper h h1 h2 exact hp h r (h + a + 1) hrh (by omega) hh hrneg (by omega) theorem core (a : ℕ) (δ : ℤ → ℤ) (hper : ∀ h, -(a : ℤ) ≤ h → h ≤ -1 → δ (h + a + 1) = δ h) (hsum : ∑ h ∈ Icc (-(a : ℤ)) 0, δ h = 0) (hp : ∀ x y z, x < y → y < z → 0 < δ x → δ y < 0 → 0 < δ z → False) (hm : ∀ x y z, x < y → y < z → δ x < 0 → 0 < δ y → δ z < 0 → False) : ∀ h, -(a : ℤ) ≤ h → h ≤ a → δ h = 0 := by have hpos := no_pos a δ hper hsum hp hm have hneg := no_pos a (fun h => - δ h) (fun h h1 h2 => by simp only [hper h h1 h2]) (by simp only [sum_neg_distrib, hsum, neg_zero]) (fun x y z hxy hyz a1 a2 a3 => hm x y z hxy hyz (by omega) (by omega) (by omega)) (fun x y z hxy hyz a1 a2 a3 => hp x y z hxy hyz (by omega) (by omega) (by omega)) have hz : ∀ h, -(a : ℤ) ≤ h → h ≤ -1 → δ h = 0 := by intro h h1 h2 have := hpos h h1 h2 have := hneg h h1 h2 omega intro h h1 h2 by_cases hh : h ≤ -1 · exact hz h h1 hh · by_cases h0 : h = 0 · subst h0 have : ∑ x ∈ Icc (-(a : ℤ)) 0, δ x = δ 0 := by apply sum_eq_single 0 · intro x hx hx0 rw [mem_Icc] at hx exact hz x hx.1 (by omega) · intro hn exact absurd (mem_Icc.mpr ⟨by omega, le_refl _⟩) hn omega · have := hper (h - a - 1) (by omega) (by omega) have e : h - a - 1 + a + 1 = h := by ring rw [e] at this rw [this] exact hz _ (by omega) (by omega) theorem windowSums_determine_floorVec (a : ℕ) (α α' θ θ' : ℝ) (h : ∀ k : ℕ, k ≤ a → winSum (floorVec a α θ) k = winSum (floorVec a α' θ') k) : floorVec a α θ = floorVec a α' θ' := by set δ : ℤ → ℤ := fun h => ⌊α' + (h : ℝ) * θ'⌋ - ⌊α + (h : ℝ) * θ⌋ with hδ have hW : ∀ k : ℕ, k ≤ a → ∑ x ∈ Icc ((k : ℤ) - a) (k : ℤ), δ x = 0 := by intro k hk have := h k hk unfold winSum at this rw [sum_congr rfl (fun x hx => coord_floorVec a α θ x (by rw [mem_Icc] at hx; omega) (by rw [mem_Icc] at hx; omega)), sum_congr rfl (fun x hx => coord_floorVec a α' θ' x (by rw [mem_Icc] at hx; omega) (by rw [mem_Icc] at hx; omega))] at this simp only [hδ, sum_sub_distrib] omega have hper : ∀ x, -(a : ℤ) ≤ x → x ≤ -1 → δ (x + a + 1) = δ x := by intro x h1 h2 have hk := window_step δ a (x + a).toNat have e1 : (((x + a).toNat : ℕ) : ℤ) = x + a := by omega rw [hW ((x + a).toNat + 1) (by omega), hW (x + a).toNat (by omega)] at hk rw [e1] at hk have e2 : x + (a : ℤ) + 1 = (x + a) + 1 := by ring have e3 : x + (a : ℤ) - a = x := by ring rw [e3] at hk omega have hsum : ∑ x ∈ Icc (-(a : ℤ)) 0, δ x = 0 := by have := hW 0 (by omega) simpa using this have hp : ∀ x y z, x < y → y < z → 0 < δ x → δ y < 0 → 0 < δ z → False := by intro x y z hxy hyz h1 h2 h3 have l1 := lt_of_floor_lt (x := α + (x : ℝ) * θ) (y := α' + (x : ℝ) * θ') (by simp only [hδ] at h1; omega) have l2 := lt_of_floor_lt (x := α' + (y : ℝ) * θ') (y := α + (y : ℝ) * θ) (by simp only [hδ] at h2; omega) have l3 := lt_of_floor_lt (x := α + (z : ℝ) * θ) (y := α' + (z : ℝ) * θ') (by simp only [hδ] at h3; omega) exact affine_no_pmp (α' - α) (θ' - θ) x y z hxy hyz (by linarith) (by linarith) (by linarith) have hm : ∀ x y z, x < y → y < z → δ x < 0 → 0 < δ y → δ z < 0 → False := by intro x y z hxy hyz h1 h2 h3 have l1 := lt_of_floor_lt (x := α' + (x : ℝ) * θ') (y := α + (x : ℝ) * θ) (by simp only [hδ] at h1; omega) have l2 := lt_of_floor_lt (x := α + (y : ℝ) * θ) (y := α' + (y : ℝ) * θ') (by simp only [hδ] at h2; omega) have l3 := lt_of_floor_lt (x := α' + (z : ℝ) * θ') (y := α + (z : ℝ) * θ) (by simp only [hδ] at h3; omega) exact affine_no_pmp (α - α') (θ - θ') x y z hxy hyz (by linarith) (by linarith) (by linarith) have hc := core a δ hper hsum hp hm funext k have h0 := hc (((k : ℕ) : ℤ) - a) (by omega) (by omega) simp only [hδ] at h0 unfold floorVec omega end CaballeroWindows