/-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.Data.Rat.Floorpublic import Mathlib.Data.Set.Cardpublic import Mathlib.Data.Int.Intervalpublic import Mathlib.Algebra.BigOperators.Group.Finset.Basicpublic import Mathlib.Data.Nat.ModEqpublic import Mathlib.Data.Nat.Prime.Basicpublic import Mathlib.Algebra.BigOperators.Ring.Finset /-!# Rows of a slope-2/q vector (Caballero, Part IV) For q >= a+1 odd, k >= 2a and T = qb + 2a - k - 1 < q(b+1), the positive part of the row multisetZ^r(g) of the clipped floor vector g_h = clip floor((k+2h)/q) is the multiset {{Phi(T - v) : v inSigma, v <= T}}, where Sigma = {2t + qs : 0 <= t <= a, s >= 0} and Phi(u) = #(Sigma cap [0,u])(Phi(u) = 0 for u < 0). The key step is the uniqueness of the representation 2t + qs (q odd, t <= a< q), proved from Nat.ModEq.cancel_left_of_coprime. Conventions: a, b, q are natural numbers and kan integer; the floor is Int.floor over Q; the right-hand multiset runs over the finite set Sigmacap [0,T] once per element. The statement holds for every a, b (the paper fixes a >= 2, b >= 1). Thedefinitions (clip, g, rows R_i(j), Z^r, Sigma, Phi, T) are local to this module and are not yetlinked to the base definitions of Part IV. ## Main results * `CaballeroSlopeTwo.rows_slopeTwo`: Part IV, Lemma 15: positive part of Z^r(g) = {{Phi(T-v) : v in Sigma, v <= T}}* `CaballeroSlopeTwo.slope2_repr_unique`: uniqueness of 2t + qs with t <= a < q, q odd* `CaballeroSlopeTwo.slope2_row_Phi`: R_i(j) = Phi(T - v) with v = 2(a-i) + q(b-j)* `CaballeroSlopeTwo.slope2_count`: [j - floor(w/q)]_+ = #{s >= 0 : qs + w < qj} ## 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 CaballeroSlopeTwo /-- `clip(v) = min(b, max(-b-1, v))` (Part IV, Notation). -/def slope2_clip (b : ℕ) (v : ℤ) : ℤ := min (b : ℤ) (max (-(b : ℤ) - 1) v) /-- The vector of slope `2/q`: `g_h = clip ⌊(k+2h)/q⌋`. -/def slope2_g (b q : ℕ) (k : ℤ) (h : ℤ) : ℤ := slope2_clip b ⌊((k + 2 * h : ℤ) : ℚ) / (q : ℚ)⌋ /-- The rows `R_i(j) = ∑_{h=-i}^{a-i} min(b+1, max(0, j - c_h))`. -/def slope2_row (a b : ℕ) (c : ℤ → ℤ) (i j : ℕ) : ℤ := ∑ h ∈ Finset.Icc (-(i : ℤ)) ((a : ℤ) - i), min ((b : ℤ) + 1) (max 0 ((j : ℤ) - c h)) /-- `Z^r(c)`: the multiset of the `K = (a+1)(b+1)` values `R_i(j)`, `0 ≤ i ≤ a`, `0 ≤ j ≤ b`. -/def slope2_Zr (a b : ℕ) (c : ℤ → ℤ) : Multiset ℤ := ((Finset.range (a + 1)) ×ˢ (Finset.range (b + 1))).val.map (fun p => slope2_row a b c p.1 p.2) /-- `Σ = {2t + qs : 0 ≤ t ≤ a, s ≥ 0}`. -/def slope2_Sigma (a q : ℕ) : Set ℕ := {v | ∃ t s : ℕ, t ≤ a ∧ v = 2 * t + q * s} /-- `Φ(u) = #(Σ ∩ [0,u])` for `u ≥ 0`, `Φ(u) = 0` for `u < 0`. -/noncomputable def slope2_Phi (a q : ℕ) (u : ℤ) : ℕ := if 0 ≤ u then {v : ℕ | v ∈ slope2_Sigma a q ∧ (v : ℤ) ≤ u}.ncard else 0 /-- `T = qb + 2a - k - 1`. -/def slope2_T (a b q : ℕ) (k : ℤ) : ℤ := (q : ℤ) * b + 2 * a - k - 1 /-- Uniqueness of the representation `2t + qs` with `0 ≤ t ≤ a < q`, `q` odd. -/theorem slope2_repr_unique {a q t t' s s' : ℕ} (hq : Odd q) (hqa : a + 1 ≤ q) (ht : t ≤ a) (ht' : t' ≤ a) (h : 2 * t + q * s = 2 * t' + q * s') : t = t' ∧ s = s' := by have hcop : Nat.gcd q 2 = 1 := Nat.coprime_two_right.mpr hq have hmod : 2 * t ≡ 2 * t' [MOD q] := by have h1 : (2 * t + q * s) % q = (2 * t' + q * s') % q := by rw [h] simpa [Nat.ModEq, Nat.add_mul_mod_self_left] using h1 have htt : t = t' := Nat.ModEq.eq_of_lt_of_lt (Nat.ModEq.cancel_left_of_coprime hcop hmod) (by omega) (by omega) subst htt refine ⟨rfl, ?_⟩ have hqs : q * s = q * s' := by omega exact Nat.eq_of_mul_eq_mul_left hq.pos hqs /-- The floor over `ℚ` is integer division. -/theorem slope2_g_eq (b q : ℕ) (k h : ℤ) : slope2_g b q k h = slope2_clip b ((k + 2 * h) / (q : ℤ)) := by simp only [slope2_g] rw [Rat.floor_intCast_div_natCast] /-- For `k + 2h ≥ 0` and `j ≤ b` neither the clip nor the cap `b+1` acts. -/theorem slope2_term (b q : ℕ) (k h j : ℤ) (hj : j ≤ b) (hkh : 0 ≤ k + 2 * h) : min ((b : ℤ) + 1) (max 0 (j - slope2_g b q k h)) = max 0 (j - (k + 2 * h) / (q : ℤ)) := by rw [slope2_g_eq] have hF : 0 ≤ (k + 2 * h) / (q : ℤ) := Int.ediv_nonneg hkh (by positivity) generalize (k + 2 * h) / (q : ℤ) = F at * simp only [slope2_clip] omega /-- `[j - ⌊w/q⌋]_+ = #{s ≥ 0 : qs + w < qj}`. -/theorem slope2_count (q : ℕ) (hq : 0 < q) (j w : ℤ) (M : ℕ) (hM : j - w / (q : ℤ) ≤ M) : (((Finset.range M).filter (fun s : ℕ => (q : ℤ) * s + w < q * j)).card : ℤ) = max 0 (j - w / (q : ℤ)) := by have hq' : (0 : ℤ) < q := by exact_mod_cast hq have key : ∀ s : ℕ, ((q : ℤ) * s + w < q * j ↔ (s : ℤ) < j - w / q) := by intro s have e1 : (s : ℤ) < j - w / q ↔ w / q < j - s := by constructor <;> intro h <;> linarith rw [e1, Int.ediv_lt_iff_lt_mul hq', show (j - (s : ℤ)) * q = q * j - q * s by ring] constructor <;> intro h <;> linarith have hfil : (Finset.range M).filter (fun s : ℕ => (q : ℤ) * s + w < q * j) = Finset.range (j - w / (q : ℤ)).toNat := by ext s simp only [Finset.mem_filter, Finset.mem_range, key] omega rw [hfil, Finset.card_range] omega /-- The row `R_i(j)` of `g` counts the pairs `(t,s)`, `t ≤ a`, `s ≤ b`, with`2t + qs ≤ qj + 2i - k - 1`. -/theorem slope2_row_eq (a b q : ℕ) (k : ℤ) (hq : 0 < q) (hk : 2 * (a : ℤ) ≤ k) (i j : ℕ) (hi : i ≤ a) (hj : j ≤ b) : slope2_row a b (slope2_g b q k) i j = ((((Finset.range (a + 1)) ×ˢ (Finset.range (b + 1))).filter (fun p : ℕ × ℕ => (2 * p.1 + q * p.2 : ℤ) ≤ q * j + 2 * i - k - 1)).card : ℤ) := by unfold slope2_row have hre : ∑ h ∈ Finset.Icc (-(i : ℤ)) ((a : ℤ) - i), min ((b : ℤ) + 1) (max 0 ((j : ℤ) - slope2_g b q k h)) = ∑ t ∈ Finset.range (a + 1), max 0 ((j : ℤ) - (k + 2 * ((t : ℤ) - i)) / (q : ℤ)) := by apply Finset.sum_nbij' (fun h : ℤ => (h + i).toNat) (fun t : ℕ => (t : ℤ) - i) · intro h hh simp only [Finset.mem_Icc, Finset.mem_range] at hh ⊢ omega · intro t ht simp only [Finset.mem_Icc, Finset.mem_range] at ht ⊢ omega · intro h hh simp only [Finset.mem_Icc] at hh omega · intro t ht simp · intro h hh simp only [Finset.mem_Icc] at hh have e : (((h + i).toNat : ℕ) : ℤ) - i = h := by omega rw [e] exact slope2_term b q k h j (by exact_mod_cast hj) (by omega) rw [hre] have hterm : ∀ t ∈ Finset.range (a + 1), max 0 ((j : ℤ) - (k + 2 * ((t : ℤ) - i)) / (q : ℤ)) = (((Finset.range (b + 1)).filter (fun s : ℕ => (2 * t + q * s : ℤ) ≤ q * j + 2 * i - k - 1)).card : ℤ) := by intro t ht have hnn : 0 ≤ (k + 2 * ((t : ℤ) - i)) / (q : ℤ) := Int.ediv_nonneg (by omega) (by positivity) have hc := slope2_count q hq j (k + 2 * ((t : ℤ) - i)) (b + 1) (by push_cast; omega) rw [← hc] have hf : (Finset.range (b + 1)).filter (fun s : ℕ => (q : ℤ) * s + (k + 2 * ((t : ℤ) - i)) < q * j) = (Finset.range (b + 1)).filter (fun s : ℕ => (2 * t + q * s : ℤ) ≤ q * j + 2 * i - k - 1) := by apply Finset.filter_congr intro s _ omega rw [hf] rw [Finset.sum_congr rfl hterm, ← Nat.cast_sum] congr 1 rw [Finset.card_filter, Finset.sum_product] refine Finset.sum_congr rfl ?_ intro t _ rw [Finset.card_filter] /-- `Φ(u)` counts the same pairs, when `u < q(b+1)`. -/theorem slope2_Phi_eq (a b q : ℕ) (hq : Odd q) (hqa : a + 1 ≤ q) (u : ℤ) (hu : u < (q : ℤ) * (b + 1)) : slope2_Phi a q u = (((Finset.range (a + 1)) ×ˢ (Finset.range (b + 1))).filter (fun p : ℕ × ℕ => (2 * p.1 + q * p.2 : ℤ) ≤ u)).card := by have hq0 : 0 < q := hq.pos have hq0' : (0 : ℤ) ≤ q := by positivity unfold slope2_Phi split_ifs with h0 · have hset : {v : ℕ | v ∈ slope2_Sigma a q ∧ (v : ℤ) ≤ u} = ↑((((Finset.range (a + 1)) ×ˢ (Finset.range (b + 1))).filter (fun p : ℕ × ℕ => (2 * p.1 + q * p.2 : ℤ) ≤ u)).image (fun p : ℕ × ℕ => 2 * p.1 + q * p.2)) := by ext v simp only [slope2_Sigma, Finset.coe_image, Finset.coe_filter, Set.mem_image, Finset.mem_product, Finset.mem_range, Prod.exists] constructor · rintro ⟨⟨t, s, ht, rfl⟩, hv⟩ have hs : (s : ℤ) < b + 1 := by by_contra hc have h1 := mul_le_mul_of_nonneg_left (not_lt.mp hc) hq0' push_cast at hv nlinarith refine ⟨t, s, ⟨⟨by omega, by omega⟩, ?_⟩, rfl⟩ push_cast at hv ⊢ exact hv · rintro ⟨t, s, ⟨⟨ht, hs⟩, hv⟩, rfl⟩ refine ⟨⟨t, s, by omega, rfl⟩, ?_⟩ push_cast exact hv rw [hset, Set.ncard_coe_finset, Finset.card_image_of_injOn] rintro ⟨t, s⟩ hp ⟨t', s'⟩ hp' he simp only [Finset.coe_filter, Finset.mem_product, Finset.mem_range, Set.mem_ofPred_eq] at hp hp' simp only at he obtain ⟨h1, h2⟩ := slope2_repr_unique hq hqa (by omega) (by omega) he rw [h1, h2] · symm rw [Finset.card_eq_zero, Finset.filter_eq_empty_iff] rintro ⟨t, s⟩ _ have : (0 : ℤ) ≤ (q : ℤ) * s := by positivity push_cast omega /-- The row of `(i,j)` is `Φ(T - v)` with `v = 2(a-i) + q(b-j)`. -/theorem slope2_row_Phi (a b q : ℕ) (k : ℤ) (hq : Odd q) (hqa : a + 1 ≤ q) (hk : 2 * (a : ℤ) ≤ k) (hT : slope2_T a b q k < (q : ℤ) * (b + 1)) (i j : ℕ) (hi : i ≤ a) (hj : j ≤ b) : slope2_row a b (slope2_g b q k) i j = (slope2_Phi a q (slope2_T a b q k - ((2 * (a - i) + q * (b - j) : ℕ) : ℤ)) : ℤ) := by have hu : (q : ℤ) * j + 2 * i - k - 1 = slope2_T a b q k - ((2 * (a - i) + q * (b - j) : ℕ) : ℤ) := by push_cast [Nat.cast_sub hi, Nat.cast_sub hj] unfold slope2_T ring have hq0' : (0 : ℤ) ≤ q := by positivity have hv : (0 : ℤ) ≤ ((2 * (a - i) + q * (b - j) : ℕ) : ℤ) := by positivity rw [slope2_row_eq a b q k hq.pos hk i j hi hj, hu, slope2_Phi_eq a b q hq hqa _ (by linarith)] /-- `Φ(u) > 0` exactly when `u ≥ 0`. -/theorem slope2_Phi_pos (a q : ℕ) (u : ℤ) : 0 < (slope2_Phi a q u : ℤ) ↔ 0 ≤ u := by unfold slope2_Phi split_ifs with h0 · simp only [h0, iff_true, Nat.cast_pos] have hfin : {v : ℕ | v ∈ slope2_Sigma a q ∧ (v : ℤ) ≤ u}.Finite := (Set.finite_le_nat u.toNat).subset (fun v hv => by simp only [Set.mem_ofPred_eq] at hv show v ≤ u.toNat omega) apply (Set.ncard_pos hfin).mpr exact ⟨0, ⟨0, 0, Nat.zero_le _, by simp⟩, by simpa using h0⟩ · simp [h0] open Classical in/-- Part IV, Lemma 15 (rows of a slope-`2/q` vector): if `q ≥ a+1` is odd, `k ≥ 2a`and `T < q(b+1)`, the positive part of `Z^r(g)` is the multiset `{{Φ(T-v) : v ∈ Σ, v ≤ T}}`. -/theorem rows_slopeTwo (a b q : ℕ) (k : ℤ) (hq : Odd q) (hqa : a + 1 ≤ q) (hk : 2 * (a : ℤ) ≤ k) (hT : slope2_T a b q k < (q : ℤ) * (b + 1)) : (slope2_Zr a b (slope2_g b q k)).filter (fun x => 0 < x) = ((Finset.range ((slope2_T a b q k).toNat + 1)).filter (fun v : ℕ => v ∈ slope2_Sigma a q ∧ (v : ℤ) ≤ slope2_T a b q k)).val.map (fun v : ℕ => ((slope2_Phi a q (slope2_T a b q k - (v : ℤ)) : ℕ) : ℤ)) := by have hq0 : 0 < q := hq.pos have hq0' : (0 : ℤ) ≤ q := by positivity -- the index map `(i,j) ↦ v = 2(a-i) + q(b-j)` have hS : (Finset.range ((slope2_T a b q k).toNat + 1)).filter (fun v : ℕ => v ∈ slope2_Sigma a q ∧ (v : ℤ) ≤ slope2_T a b q k) = (((Finset.range (a + 1)) ×ˢ (Finset.range (b + 1))).filter (fun p : ℕ × ℕ => ((2 * (a - p.1) + q * (b - p.2) : ℕ) : ℤ) ≤ slope2_T a b q k)).image (fun p : ℕ × ℕ => 2 * (a - p.1) + q * (b - p.2)) := by ext v rw [Finset.mem_filter, Finset.mem_image] simp only [Finset.mem_filter, Finset.mem_range, Finset.mem_product, Prod.exists] simp only [slope2_Sigma, Set.mem_ofPred_eq] constructor · rintro ⟨_, ⟨t, s, ht, rfl⟩, hv⟩ have hs : (s : ℤ) < b + 1 := by by_contra hc have h1 := mul_le_mul_of_nonneg_left (not_lt.mp hc) hq0' push_cast at hv nlinarith refine ⟨a - t, b - s, ⟨⟨by omega, by omega⟩, ?_⟩, ?_⟩ · rw [Nat.sub_sub_self ht, Nat.sub_sub_self (by omega)] exact hv · rw [Nat.sub_sub_self ht, Nat.sub_sub_self (by omega)] · rintro ⟨i, j, ⟨⟨hi, hj⟩, hv⟩, rfl⟩ refine ⟨by omega, ⟨a - i, b - j, Nat.sub_le _ _, rfl⟩, hv⟩ have hinj : Set.InjOn (fun p : ℕ × ℕ => 2 * (a - p.1) + q * (b - p.2)) ↑(((Finset.range (a + 1)) ×ˢ (Finset.range (b + 1))).filter (fun p : ℕ × ℕ => ((2 * (a - p.1) + q * (b - p.2) : ℕ) : ℤ) ≤ slope2_T a b q k)) := by rintro ⟨i, j⟩ hp ⟨i', j'⟩ hp' he simp only [Finset.coe_filter, Finset.mem_product, Finset.mem_range, Set.mem_ofPred_eq] at hp hp' simp only at he obtain ⟨h1, h2⟩ := slope2_repr_unique hq hqa (Nat.sub_le a i) (Nat.sub_le a i') he simp only [Prod.mk.injEq] omega rw [hS, Finset.image_val_of_injOn hinj, Multiset.map_map, slope2_Zr, Multiset.filter_map, Finset.filter_val] rw [Multiset.filter_congr (q := fun p : ℕ × ℕ => ((2 * (a - p.1) + q * (b - p.2) : ℕ) : ℤ) ≤ slope2_T a b q k)] · apply Multiset.map_congr rfl intro p hp have hp' := Multiset.mem_of_mem_filter hp simp only [Finset.mem_val, Finset.mem_product, Finset.mem_range] at hp' simp only [Function.comp] exact slope2_row_Phi a b q k hq hqa hk hT p.1 p.2 (by omega) (by omega) · intro p hp simp only [Finset.mem_val, Finset.mem_product, Finset.mem_range] at hp simp only [Function.comp] rw [slope2_row_Phi a b q k hq hqa hk hT p.1 p.2 (by omega) (by omega), slope2_Phi_pos] constructor <;> intro h <;> linarith end CaballeroSlopeTwo