import Mathlib /-! # Non-Crossing Partitions `NC(n)`: Lattice, Catalan/Narayana Enumeration, Kreweras Complement Encoding: a partition of `{0, …, n-1}` is a Boolean relation `rel : ℕ → ℕ → Bool` supported on `[0, n)²` that is an equivalence relation there and satisfies the non-crossing axiom `a < b < c < d, a ~ c, b ~ d ⟹ a ~ b`. -/ namespace FreeProb /-- A non-crossing partition of `{0, …, n-1}`, encoded by its block relation. -/ structure NC (n : ℕ) where rel : ℕ → ℕ → Bool supp : ∀ i j, rel i j = true → i < n ∧ j < n refl : ∀ i, i < n → rel i i = true symm : ∀ i j, rel i j = rel j i trans : ∀ i j k, rel i j = true → rel j k = true → rel i k = true nc : ∀ a b c d, a < b → b < c → c < d → rel a c = true → rel b d = true → rel a b = true namespace NC variable {n : ℕ} theorem ext' {π σ : NC n} (h : ∀ i j, i < n → j < n → π.rel i j = σ.rel i j) : π = σ := by cases π with | mk r1 s1 _ _ _ _ => cases σ with | mk r2 s2 _ _ _ _ => have : r1 = r2 := by funext i j by_cases hi : i < n ∧ j < n · exact h i j hi.1 hi.2 · have e1 : r1 i j = false := by cases hr : r1 i j · rfl · exact absurd (s1 i j hr) hi have e2 : r2 i j = false := by cases hr : r2 i j · rfl · exact absurd (s2 i j hr) hi rw [e1, e2] subst this rfl /-- The finite "matrix" shape of an `NC n`, used to build the `Fintype` instance. -/ def toMat (π : NC n) : Fin n → Fin n → Bool := fun i j => π.rel i.1 j.1 theorem toMat_injective : Function.Injective (toMat (n := n)) := by intro π σ h apply ext' intro i j hi hj exact congrFun (congrFun h ⟨i, hi⟩) ⟨j, hj⟩ instance : DecidableEq (NC n) := fun π σ => decidable_of_iff (toMat π = toMat σ) ⟨fun h => toMat_injective h, fun h => h ▸ rfl⟩ /-- Rebuild a relation on `ℕ` from a matrix, extended by `false`. -/ def ofMat (m : Fin n → Fin n → Bool) : ℕ → ℕ → Bool := fun i j => if h : i < n ∧ j < n then m ⟨i, h.1⟩ ⟨j, h.2⟩ else false /-- Matrices that come from non-crossing partitions. -/ def GoodMat (m : Fin n → Fin n → Bool) : Prop := (∀ i : Fin n, m i i = true) ∧ (∀ i j : Fin n, m i j = m j i) ∧ (∀ i j k : Fin n, m i j = true → m j k = true → m i k = true) ∧ (∀ a b c d : Fin n, a < b → b < c → c < d → m a c = true → m b d = true → m a b = true) instance (m : Fin n → Fin n → Bool) : Decidable (GoodMat m) := by unfold GoodMat; infer_instance /-- `NC n` is equivalent to the finite set of good matrices. -/ def equivGood : NC n ≃ { m : Fin n → Fin n → Bool // GoodMat m } where toFun π := ⟨toMat π, fun i => π.refl _ i.2, fun i j => π.symm _ _, fun i j k h1 h2 => π.trans _ _ _ h1 h2, fun a b c d h1 h2 h3 h4 h5 => π.nc _ _ _ _ h1 h2 h3 h4 h5⟩ invFun m := { rel := ofMat m.1 supp := by intro i j h unfold ofMat at h split_ifs at h with hh · exact hh refl := by intro i hi unfold ofMat rw [dif_pos ⟨hi, hi⟩] exact m.2.1 ⟨i, hi⟩ symm := by intro i j unfold ofMat by_cases h : i < n ∧ j < n · rw [dif_pos h, dif_pos ⟨h.2, h.1⟩] exact m.2.2.1 _ _ · rw [dif_neg h, dif_neg (fun h' => h ⟨h'.2, h'.1⟩)] trans := by intro i j k h1 h2 unfold ofMat at h1 h2 ⊢ split_ifs at h1 h2 with ha hb · rw [dif_pos ⟨ha.1, hb.2⟩] exact m.2.2.2.1 _ _ _ h1 h2 nc := by intro a b c d h1 h2 h3 h4 h5 unfold ofMat at h4 h5 ⊢ split_ifs at h4 h5 with ha hb · rw [dif_pos ⟨ha.1, hb.1⟩] exact m.2.2.2.2 ⟨a, ha.1⟩ ⟨b, hb.1⟩ ⟨c, ha.2⟩ ⟨d, hb.2⟩ h1 h2 h3 h4 h5 } left_inv π := by apply ext' intro i j hi hj show ofMat (toMat π) i j = π.rel i j unfold ofMat toMat rw [dif_pos ⟨hi, hj⟩] right_inv m := by apply Subtype.ext funext i j show ofMat m.1 i.1 j.1 = m.1 i j unfold ofMat rw [dif_pos ⟨i.2, j.2⟩] instance : Fintype (NC n) := Fintype.ofEquiv _ equivGood.symm /-! ### Refinement order and meet -/ instance : PartialOrder (NC n) where le π σ := ∀ i j, π.rel i j = true → σ.rel i j = true le_refl _ _ _ h := h le_trans _ _ _ h1 h2 i j h := h2 i j (h1 i j h) le_antisymm π σ h1 h2 := by apply ext' intro i j _ _ cases e : π.rel i j · cases e' : σ.rel i j · rfl · exact absurd (h2 i j e') (by simp [e]) · exact (h1 i j e).symm theorem le_def {π σ : NC n} : π ≤ σ ↔ ∀ i j, π.rel i j = true → σ.rel i j = true := Iff.rfl instance : DecidableLE (NC n) := fun π σ => decidable_of_iff (∀ i < n, ∀ j < n, π.rel i j = true → σ.rel i j = true) (by constructor · intro h i j hij exact h i (π.supp i j hij).1 j (π.supp i j hij).2 hij · intro h i _ j _ hij exact h i j hij) /-- The discrete partition `0_n` (all singletons). -/ def bot (n : ℕ) : NC n where rel i j := decide (i = j ∧ i < n) supp i j h := by simp at h; omega refl i hi := by simp [hi] symm i j := by by_cases h : i = j · subst h; rfl · simp [h, Ne.symm h] trans i j k h1 h2 := by simp at h1 h2 ⊢; omega nc a b c d h1 h2 _ h4 _ := by simp at h4; omega /-- The one-block partition `1_n`. -/ def top (n : ℕ) : NC n where rel i j := decide (i < n ∧ j < n) supp i j h := by simpa using h refl i hi := by simp [hi] symm i j := by simp [and_comm] trans i j k h1 h2 := by simp at h1 h2 ⊢; omega nc a b c d _ _ _ h4 h5 := by simp at h4 h5 ⊢; omega instance : OrderBot (NC n) where bot := bot n bot_le π i j h := by simp [bot] at h obtain ⟨rfl, hi⟩ := h exact π.refl i hi instance : OrderTop (NC n) where top := top n le_top π i j h := by have := π.supp i j h simp [top, this.1, this.2] /-- Meet: intersection of block relations. -/ def inf (π σ : NC n) : NC n where rel i j := π.rel i j && σ.rel i j supp i j h := by simp only [Bool.and_eq_true] at h exact π.supp i j h.1 refl i hi := by simp [π.refl i hi, σ.refl i hi] symm i j := by rw [π.symm, σ.symm] trans i j k h1 h2 := by simp only [Bool.and_eq_true] at h1 h2 ⊢ exact ⟨π.trans _ _ _ h1.1 h2.1, σ.trans _ _ _ h1.2 h2.2⟩ nc a b c d h1 h2 h3 h4 h5 := by simp only [Bool.and_eq_true] at h4 h5 ⊢ exact ⟨π.nc _ _ _ _ h1 h2 h3 h4.1 h5.1, σ.nc _ _ _ _ h1 h2 h3 h4.2 h5.2⟩ instance : SemilatticeInf (NC n) where inf := inf inf_le_left _ _ i j h := by simp only [inf, Bool.and_eq_true] at h; exact h.1 inf_le_right _ _ i j h := by simp only [inf, Bool.and_eq_true] at h; exact h.2 le_inf _ _ _ h1 h2 i j h := by simp only [inf, Bool.and_eq_true]; exact ⟨h1 i j h, h2 i j h⟩ /-- Join: the meet of all common upper bounds (a finite meet in the finite lattice). -/ def sup (π σ : NC n) : NC n := (Finset.univ.filter fun τ : NC n => π ≤ τ ∧ σ ≤ τ).inf' ⟨⊤, by simp⟩ id instance : Lattice (NC n) where sup := sup le_sup_left _ _ := Finset.le_inf' _ _ fun _ hτ => (Finset.mem_filter.mp hτ).2.1 le_sup_right _ _ := Finset.le_inf' _ _ fun _ hτ => (Finset.mem_filter.mp hτ).2.2 sup_le _ _ _ h1 h2 := Finset.inf'_le _ (Finset.mem_filter.mpr ⟨Finset.mem_univ _, h1, h2⟩) theorem card_zero : Fintype.card (NC 0) = 1 := Fintype.card_eq_one_iff.mpr ⟨⊥, fun _ => ext' fun _ _ hi => absurd hi (Nat.not_lt_zero _)⟩ /-! ### Windows, prepending a singleton, joining at the first link -/ /-- Restriction of `π` to the window `{k, …, k+m-1}`, re-indexed to `{0, …, m-1}`. -/ def window {N : ℕ} (π : NC N) (k m : ℕ) (h : k + m ≤ N) : NC m where rel i j := decide (i < m ∧ j < m ∧ π.rel (i + k) (j + k) = true) supp i j hr := by have := of_decide_eq_true hr; exact ⟨this.1, this.2.1⟩ refl i hi := decide_eq_true ⟨hi, hi, π.refl _ (by omega)⟩ symm i j := decide_eq_decide.mpr ⟨fun ⟨a, b, c⟩ => ⟨b, a, by rw [π.symm]; exact c⟩, fun ⟨a, b, c⟩ => ⟨b, a, by rw [π.symm]; exact c⟩⟩ trans i j l h1 h2 := by obtain ⟨a1, _, a3⟩ := of_decide_eq_true h1 obtain ⟨_, b2, b3⟩ := of_decide_eq_true h2 exact decide_eq_true ⟨a1, b2, π.trans _ _ _ a3 b3⟩ nc a b c d h1 h2 h3 h4 h5 := by obtain ⟨a1, _, a3⟩ := of_decide_eq_true h4 obtain ⟨b1, _, b3⟩ := of_decide_eq_true h5 exact decide_eq_true ⟨a1, b1, π.nc _ _ _ _ (by omega) (by omega) (by omega) a3 b3⟩ theorem window_rel {N : ℕ} (π : NC N) (k m : ℕ) (h : k + m ≤ N) (i j : ℕ) : (window π k m h).rel i j = true ↔ i < m ∧ j < m ∧ π.rel (i + k) (j + k) = true := decide_eq_true_iff /-- Case A: prepend the singleton block `{0}`. -/ def prepend {m : ℕ} (σ : NC m) : NC (m + 1) where rel i j := decide ((i = 0 ∧ j = 0) ∨ (1 ≤ i ∧ 1 ≤ j ∧ σ.rel (i - 1) (j - 1) = true)) supp i j h := by rcases of_decide_eq_true h with h | ⟨_, _, h3⟩ · omega · have := σ.supp _ _ h3; omega refl i hi := by apply decide_eq_true rcases Nat.eq_zero_or_pos i with rfl | hpos · exact Or.inl ⟨rfl, rfl⟩ · exact Or.inr ⟨hpos, hpos, σ.refl _ (by omega)⟩ symm i j := decide_eq_decide.mpr ⟨fun h => h.elim (fun ⟨a, b⟩ => Or.inl ⟨b, a⟩) (fun ⟨a, b, c⟩ => Or.inr ⟨b, a, by rw [σ.symm]; exact c⟩), fun h => h.elim (fun ⟨a, b⟩ => Or.inl ⟨b, a⟩) (fun ⟨a, b, c⟩ => Or.inr ⟨b, a, by rw [σ.symm]; exact c⟩)⟩ trans i j k h1 h2 := by apply decide_eq_true rcases of_decide_eq_true h1 with ⟨a1, a2⟩ | ⟨a1, a2, a3⟩ <;> rcases of_decide_eq_true h2 with ⟨b1, b2⟩ | ⟨b1, b2, b3⟩ · exact Or.inl ⟨a1, b2⟩ · omega · omega · exact Or.inr ⟨a1, b2, σ.trans _ _ _ a3 b3⟩ nc a b c d h1 h2 h3 h4 h5 := by apply decide_eq_true rcases of_decide_eq_true h4 with ⟨_, _⟩ | ⟨a1, _, a3⟩ · omega · rcases of_decide_eq_true h5 with ⟨_, _⟩ | ⟨b1, _, b3⟩ · omega · exact Or.inr ⟨a1, b1, σ.nc _ _ _ _ (by omega) (by omega) (by omega) a3 b3⟩ theorem prepend_rel {m : ℕ} (σ : NC m) (i j : ℕ) : (prepend σ).rel i j = true ↔ (i = 0 ∧ j = 0) ∨ (1 ≤ i ∧ 1 ≤ j ∧ σ.rel (i - 1) (j - 1) = true) := decide_eq_true_iff theorem prepend_rel_succ {m : ℕ} (σ : NC m) (i j : ℕ) : (prepend σ).rel (i + 1) (j + 1) = σ.rel i j := by rw [Bool.eq_iff_iff, prepend_rel]; simp theorem prepend_rel_zero_succ {m : ℕ} (σ : NC m) (i : ℕ) : (prepend σ).rel 0 (i + 1) = false := by rw [Bool.eq_false_iff]; intro h; rw [prepend_rel] at h; omega /-- Outer re-indexing used in Case B: `0 ↦ 0` and `u ↦ u - (j+1)`. -/ def oIdx (j u : ℕ) : ℕ := if u = 0 then 0 else u - (j + 1) theorem oIdx_zero (j : ℕ) : oIdx j 0 = 0 := rfl theorem oIdx_pos {j u : ℕ} (h : u ≠ 0) : oIdx j u = u - (j + 1) := if_neg h /-- Case B: `0` is attached to the block of `j+1`; `a` lives on `{1..j}`, `b` on `{j+1..m}`. -/ def joinAt {m : ℕ} (j : ℕ) (hj : j < m) (a : NC j) (b : NC (m - j)) : NC (m + 1) where rel u v := decide ((1 ≤ u ∧ u ≤ j ∧ 1 ≤ v ∧ v ≤ j ∧ a.rel (u - 1) (v - 1) = true) ∨ ((u = 0 ∨ (j + 1 ≤ u ∧ u ≤ m)) ∧ (v = 0 ∨ (j + 1 ≤ v ∧ v ≤ m)) ∧ b.rel (oIdx j u) (oIdx j v) = true)) supp u v h := by rcases of_decide_eq_true h with ⟨_, _, _, _, _⟩ | ⟨h1, h2, _⟩ <;> omega refl u hu := by apply decide_eq_true by_cases h0 : u = 0 · subst h0 exact Or.inr ⟨Or.inl rfl, Or.inl rfl, by rw [oIdx_zero]; exact b.refl 0 (by omega)⟩ · by_cases h1 : u ≤ j · exact Or.inl ⟨by omega, h1, by omega, h1, a.refl _ (by omega)⟩ · exact Or.inr ⟨Or.inr ⟨by omega, by omega⟩, Or.inr ⟨by omega, by omega⟩, by rw [oIdx_pos h0]; exact b.refl _ (by omega)⟩ symm u v := decide_eq_decide.mpr ⟨fun h => h.elim (fun ⟨p1, p2, p3, p4, p5⟩ => Or.inl ⟨p3, p4, p1, p2, by rw [a.symm]; exact p5⟩) (fun ⟨p1, p2, p3⟩ => Or.inr ⟨p2, p1, by rw [b.symm]; exact p3⟩), fun h => h.elim (fun ⟨p1, p2, p3, p4, p5⟩ => Or.inl ⟨p3, p4, p1, p2, by rw [a.symm]; exact p5⟩) (fun ⟨p1, p2, p3⟩ => Or.inr ⟨p2, p1, by rw [b.symm]; exact p3⟩)⟩ trans u v w h1 h2 := by apply decide_eq_true rcases of_decide_eq_true h1 with ⟨p1, p2, p3, p4, p5⟩ | ⟨p1, p2, p3⟩ <;> rcases of_decide_eq_true h2 with ⟨q1, q2, q3, q4, q5⟩ | ⟨q1, q2, q3⟩ · exact Or.inl ⟨p1, p2, q3, q4, a.trans _ _ _ p5 q5⟩ · omega · omega · exact Or.inr ⟨p1, q2, b.trans _ _ _ p3 q3⟩ nc p q r s h1 h2 h3 h4 h5 := by apply decide_eq_true rcases of_decide_eq_true h4 with ⟨p1, p2, p3, p4, p5⟩ | ⟨p1, p2, p3⟩ <;> rcases of_decide_eq_true h5 with ⟨q1, q2, q3, q4, q5⟩ | ⟨q1, q2, q3⟩ · exact Or.inl ⟨p1, by omega, q1, q2, a.nc _ _ _ _ (by omega) (by omega) (by omega) p5 q5⟩ · omega · omega · have hq : q ≠ 0 := by omega have hr : r ≠ 0 := by omega have hs : s ≠ 0 := by omega rw [oIdx_pos hr] at p3 rw [oIdx_pos hq, oIdx_pos hs] at q3 refine Or.inr ⟨p1, q1, ?_⟩ rw [oIdx_pos hq] by_cases hp0 : p = 0 · subst hp0 rw [oIdx_zero] at p3 ⊢ by_cases hqj : q = j + 1 · subst hqj; simpa using b.refl 0 (by omega) · exact b.nc _ _ _ _ (by omega) (by omega) (by omega) p3 q3 · rw [oIdx_pos hp0] at p3 ⊢ exact b.nc _ _ _ _ (by omega) (by omega) (by omega) p3 q3 section JoinAt variable {m j : ℕ} {hj : j < m} {a : NC j} {b : NC (m - j)} theorem joinAt_rel (u v : ℕ) : (joinAt j hj a b).rel u v = true ↔ ((1 ≤ u ∧ u ≤ j ∧ 1 ≤ v ∧ v ≤ j ∧ a.rel (u - 1) (v - 1) = true) ∨ ((u = 0 ∨ (j + 1 ≤ u ∧ u ≤ m)) ∧ (v = 0 ∨ (j + 1 ≤ v ∧ v ≤ m)) ∧ b.rel (oIdx j u) (oIdx j v) = true)) := decide_eq_true_iff theorem joinAt_rel_in (i k : ℕ) (hi : i < j) (hk : k < j) : (joinAt j hj a b).rel (i + 1) (k + 1) = a.rel i k := by rw [Bool.eq_iff_iff, joinAt_rel] constructor · rintro (⟨_, _, _, _, h⟩ | ⟨h, _, _⟩) · simpa using h · omega · intro h; exact Or.inl ⟨by omega, by omega, by omega, by omega, by simpa using h⟩ theorem joinAt_rel_out (u v : ℕ) (hu : u = 0 ∨ (j + 1 ≤ u ∧ u ≤ m)) (hv : v = 0 ∨ (j + 1 ≤ v ∧ v ≤ m)) : (joinAt j hj a b).rel u v = b.rel (oIdx j u) (oIdx j v) := by rw [Bool.eq_iff_iff, joinAt_rel] constructor · rintro (⟨_, _, _, _, _⟩ | ⟨_, _, h⟩) · omega · exact h · intro h; exact Or.inr ⟨hu, hv, h⟩ theorem joinAt_rel_cross (u v : ℕ) (hu : 1 ≤ u ∧ u ≤ j) (hv : v = 0 ∨ (j + 1 ≤ v ∧ v ≤ m)) : (joinAt j hj a b).rel u v = false := by rw [Bool.eq_false_iff]; intro h; rw [joinAt_rel] at h; omega theorem joinAt_rel_zero_link : (joinAt j hj a b).rel 0 (j + 1) = true := by rw [joinAt_rel_out _ _ (Or.inl rfl) (Or.inr ⟨le_rfl, hj⟩), oIdx_zero, oIdx_pos (by omega), Nat.sub_self] exact b.refl 0 (by omega) theorem joinAt_rel_zero_inner (i : ℕ) (hi : i < j) : (joinAt j hj a b).rel 0 (i + 1) = false := by rw [(joinAt j hj a b).symm] exact joinAt_rel_cross _ _ ⟨by omega, by omega⟩ (Or.inl rfl) end JoinAt /-- The first link of `π ∈ NC(m+1)`: the least `j` with `0 ~ j+1`, if any. -/ def firstLink {m : ℕ} (π : NC (m + 1)) : Option (Fin m) := if h : ∃ i, i < m ∧ π.rel 0 (i + 1) = true then some ⟨Nat.find h, (Nat.find_spec h).1⟩ else none theorem firstLink_eq_none {m : ℕ} (π : NC (m + 1)) : firstLink π = none ↔ ∀ i, i < m → π.rel 0 (i + 1) = false := by unfold firstLink split_ifs with h · simp only [false_iff, not_forall] obtain ⟨i, hi, hr⟩ := h exact ⟨i, hi, by simp [hr]⟩ · constructor · intro _ i hi cases hr : π.rel 0 (i + 1) · rfl · exact absurd ⟨i, hi, hr⟩ h · intro _; rfl theorem firstLink_eq_some {m : ℕ} (π : NC (m + 1)) (j : Fin m) : firstLink π = some j ↔ π.rel 0 (j.1 + 1) = true ∧ ∀ i, i < j.1 → π.rel 0 (i + 1) = false := by unfold firstLink split_ifs with h · rw [Option.some.injEq, Fin.ext_iff, Fin.val_mk, Nat.find_eq_iff] constructor · rintro ⟨⟨_, h1⟩, h2⟩ refine ⟨h1, fun i hi => ?_⟩ cases hr : π.rel 0 (i + 1) · rfl · exact absurd ⟨by omega, hr⟩ (h2 i hi) · rintro ⟨h1, h2⟩ refine ⟨⟨j.2, h1⟩, fun i hi => ?_⟩ rintro ⟨_, hr⟩ rw [h2 i hi] at hr; exact Bool.false_ne_true hr · simp only [false_iff, not_and, not_forall] intro h1 exact absurd ⟨j.1, j.2, h1⟩ h theorem prepend_window {m : ℕ} (π : NC (m + 1)) (h : ∀ i, i < m → π.rel 0 (i + 1) = false) : prepend (window π 1 m (by omega)) = π := by apply ext' intro u v hu hv rcases u with _ | u <;> rcases v with _ | v · rw [π.refl 0 hu, Bool.eq_iff_iff, prepend_rel]; simp · rw [prepend_rel_zero_succ, h v (by omega)] · rw [(prepend _).symm, prepend_rel_zero_succ, π.symm, h u (by omega)] · rw [prepend_rel_succ, Bool.eq_iff_iff, window_rel] exact ⟨fun h => h.2.2, fun h => ⟨by omega, by omega, h⟩⟩ theorem window_prepend {m : ℕ} (σ : NC m) : window (prepend σ) 1 m (by omega) = σ := by apply ext' intro i j hi hj rw [Bool.eq_iff_iff, window_rel, prepend_rel_succ] exact ⟨fun h => h.2.2, fun h => ⟨hi, hj, h⟩⟩ theorem joinAt_window {m : ℕ} (j : ℕ) (hj : j < m) (π : NC (m + 1)) (h1 : π.rel 0 (j + 1) = true) (h2 : ∀ i, i < j → π.rel 0 (i + 1) = false) : joinAt j hj (window π 1 j (by omega)) (window π (j + 1) (m - j) (by omega)) = π := by -- no block of `π` crosses from `{1..j}` to `{0} ∪ {j+1..m}` have hcross : ∀ u v, 1 ≤ u → u ≤ j → (v = 0 ∨ (j + 1 ≤ v ∧ v ≤ m)) → π.rel u v = false := by intro u v hu1 hu2 hv have hu0 : π.rel u 0 = false := by rw [π.symm]; have := h2 (u - 1) (by omega); rwa [Nat.sub_add_cancel hu1] at this rw [Bool.eq_false_iff] intro huv rcases hv with rfl | ⟨hv1, hv2⟩ · rw [hu0] at huv; exact Bool.false_ne_true huv · rcases Nat.eq_or_lt_of_le hv1 with hveq | hvlt · subst hveq have := π.trans _ _ _ huv (by rw [π.symm]; exact h1) rw [hu0] at this; exact Bool.false_ne_true this · have := π.nc 0 u (j + 1) v (by omega) (by omega) hvlt h1 huv rw [π.symm, hu0] at this; exact Bool.false_ne_true this -- re-indexing the outer zone have hsub : ∀ u w, (u = 0 ∨ j + 1 ≤ u) → π.rel (oIdx j u + (j + 1)) w = π.rel u w := by intro u w hu by_cases hu0 : u = 0 · subst hu0 rw [oIdx_zero, Nat.zero_add, Bool.eq_iff_iff] constructor · intro h; exact π.trans _ _ _ h1 h · intro h; exact π.trans _ _ _ (by rw [π.symm]; exact h1) h · rw [oIdx_pos hu0, Nat.sub_add_cancel (by omega)] apply ext' intro u v hu hv have zone : ∀ x, x < m + 1 → (1 ≤ x ∧ x ≤ j) ∨ (x = 0 ∨ (j + 1 ≤ x ∧ x ≤ m)) := by intro x hx; omega rcases zone u hu with hu' | hu' <;> rcases zone v hv with hv' | hv' · obtain ⟨u', rfl⟩ : ∃ u', u = u' + 1 := ⟨u - 1, by omega⟩ obtain ⟨v', rfl⟩ : ∃ v', v = v' + 1 := ⟨v - 1, by omega⟩ rw [joinAt_rel_in _ _ (by omega) (by omega), Bool.eq_iff_iff, window_rel] exact ⟨fun h => h.2.2, fun h => ⟨by omega, by omega, h⟩⟩ · rw [joinAt_rel_cross _ _ hu' hv', hcross u v hu'.1 hu'.2 hv'] · rw [(joinAt _ _ _ _).symm, joinAt_rel_cross _ _ hv' hu', π.symm, hcross v u hv'.1 hv'.2 hu'] · rw [joinAt_rel_out _ _ hu' hv', Bool.eq_iff_iff, window_rel] have hbu : oIdx j u < m - j := by rcases hu' with rfl | hu' · rw [oIdx_zero]; omega · rw [oIdx_pos (by omega)]; omega have hbv : oIdx j v < m - j := by rcases hv' with rfl | hv' · rw [oIdx_zero]; omega · rw [oIdx_pos (by omega)]; omega rw [hsub u _ (by omega), π.symm, hsub v _ (by omega), π.symm] exact ⟨fun h => h.2.2, fun h => ⟨hbu, hbv, h⟩⟩ theorem window_joinAt_in {m : ℕ} (j : ℕ) (hj : j < m) (a : NC j) (b : NC (m - j)) : window (joinAt j hj a b) 1 j (by omega) = a := by apply ext' intro i k hi hk rw [Bool.eq_iff_iff, window_rel, joinAt_rel_in _ _ hi hk] exact ⟨fun h => h.2.2, fun h => ⟨hi, hk, h⟩⟩ theorem window_joinAt_out {m : ℕ} (j : ℕ) (hj : j < m) (a : NC j) (b : NC (m - j)) : window (joinAt j hj a b) (j + 1) (m - j) (by omega) = b := by apply ext' intro y z hy hz rw [Bool.eq_iff_iff, window_rel, joinAt_rel_out _ _ (Or.inr ⟨by omega, by omega⟩) (Or.inr ⟨by omega, by omega⟩), oIdx_pos (by omega), oIdx_pos (by omega), Nat.add_sub_cancel, Nat.add_sub_cancel] exact ⟨fun h => h.2.2, fun h => ⟨hy, hz, h⟩⟩ /-- Fibre of Case A. -/ def fiberNone (m : ℕ) : { π : NC (m + 1) // firstLink π = none } ≃ NC m where toFun π := window π.1 1 m (by omega) invFun σ := ⟨prepend σ, (firstLink_eq_none _).mpr fun i _ => prepend_rel_zero_succ σ i⟩ left_inv π := Subtype.ext (prepend_window π.1 ((firstLink_eq_none _).mp π.2)) right_inv σ := window_prepend σ /-- Fibre of Case B at link `j`. -/ def fiberSome (m : ℕ) (j : Fin m) : { π : NC (m + 1) // firstLink π = some j } ≃ NC j.1 × NC (m - j.1) where toFun π := (window π.1 1 j.1 (by omega), window π.1 (j.1 + 1) (m - j.1) (by omega)) invFun p := ⟨joinAt j.1 j.2 p.1 p.2, (firstLink_eq_some _ _).mpr ⟨joinAt_rel_zero_link (hj := j.2), fun i hi => joinAt_rel_zero_inner (hj := j.2) i hi⟩⟩ left_inv π := Subtype.ext (joinAt_window j.1 j.2 π.1 ((firstLink_eq_some _ _).mp π.2).1 ((firstLink_eq_some _ _).mp π.2).2) right_inv p := Prod.ext (window_joinAt_in j.1 j.2 p.1 p.2) (window_joinAt_out j.1 j.2 p.1 p.2) /-- **First-link decomposition of sums over `NC(m+1)`.** -/ theorem sum_decomp {M : Type*} [AddCommMonoid M] (m : ℕ) (F : NC (m + 1) → M) : ∑ π, F π = ∑ σ : NC m, F (prepend σ) + ∑ j : Fin m, ∑ p : NC j.1 × NC (m - j.1), F (joinAt j.1 j.2 p.1 p.2) := by rw [← Fintype.sum_fiberwise (firstLink (m := m)) F, Fintype.sum_option] congr 1 · refine Fintype.sum_equiv (fiberNone m) _ _ fun π => ?_ exact congrArg F (prepend_window π.1 ((firstLink_eq_none _).mp π.2)).symm · refine Finset.sum_congr rfl fun j _ => Fintype.sum_equiv (fiberSome m j) _ _ fun π => ?_ dsimp only [fiberSome, Equiv.coe_fn_mk] exact congrArg F (joinAt_window j.1 j.2 π.1 ((firstLink_eq_some _ _).mp π.2).1 ((firstLink_eq_some _ _).mp π.2).2).symm /-- **Catalan enumeration:** `|NC(n)| = C_n`. -/ theorem card_NC (n : ℕ) : Fintype.card (NC n) = catalan n := by induction n using Nat.strong_induction_on with | _ n ih => rcases n with _ | m · rw [card_zero, catalan_zero] · rw [Fintype.card_eq_sum_ones, sum_decomp m (fun _ => (1 : ℕ))] simp only [Finset.sum_const, Finset.card_univ, smul_eq_mul, mul_one, Fintype.card_prod] rw [ih m (by omega), catalan_succ, Fin.sum_univ_castSucc] simp only [Fin.val_castSucc, Fin.val_last, Nat.sub_self, catalan_zero, mul_one] rw [add_comm] congr 1 refine Finset.sum_congr rfl fun j _ => ?_ rw [ih j.1 (by omega), ih (m - j.1) (by omega)] /-- Explicit central-binomial form: `(n+1) |NC(n)| = binom(2n, n)`. -/ theorem succ_mul_card_NC (n : ℕ) : (n + 1) * Fintype.card (NC n) = (2 * n).choose n := by rw [card_NC, succ_mul_catalan_eq_centralBinom, Nat.centralBinom_eq_two_mul_choose] /-! ### Block counts -/ /-- `i` is the least element of its block. -/ def isMin {n : ℕ} (π : NC n) (i : ℕ) : Bool := decide (∀ k, k < i → π.rel k i = false) /-- Number of blocks `|π|`. -/ def blockCount {n : ℕ} (π : NC n) : ℕ := ∑ i ∈ Finset.range n, if π.isMin i then 1 else 0 theorem isMin_zero {n : ℕ} (π : NC n) : π.isMin 0 = true := decide_eq_true fun _ hk => absurd hk (Nat.not_lt_zero _) theorem isMin_prepend_succ {m : ℕ} (σ : NC m) (i : ℕ) : (prepend σ).isMin (i + 1) = σ.isMin i := by unfold isMin apply decide_eq_decide.mpr constructor · intro h k hk; have := h (k + 1) (by omega); rwa [prepend_rel_succ] at this · intro h k hk rcases k with _ | k · exact prepend_rel_zero_succ σ i · rw [prepend_rel_succ]; exact h k (by omega) theorem blockCount_prepend {m : ℕ} (σ : NC m) : (prepend σ).blockCount = σ.blockCount + 1 := by unfold blockCount rw [Finset.sum_range_succ', isMin_zero] simp only [isMin_prepend_succ, if_true] section JoinAtBlocks variable {m j : ℕ} {hj : j < m} {a : NC j} {b : NC (m - j)} theorem isMin_joinAt_in (i : ℕ) (hi : i < j) : (joinAt j hj a b).isMin (i + 1) = a.isMin i := by unfold isMin apply decide_eq_decide.mpr constructor · intro h k hk; have := h (k + 1) (by omega); rwa [joinAt_rel_in _ _ (by omega) hi] at this · intro h k hk rcases k with _ | k · exact joinAt_rel_zero_inner i hi · rw [joinAt_rel_in _ _ (by omega) hi]; exact h k (by omega) theorem isMin_joinAt_link : (joinAt j hj a b).isMin (j + 1) = false := by unfold isMin rw [decide_eq_false_iff_not] intro h have := h 0 (by omega) rw [joinAt_rel_zero_link] at this exact absurd this (by decide) theorem isMin_joinAt_out (y : ℕ) (hy : y + 1 < m - j) : (joinAt j hj a b).isMin (j + 1 + (y + 1)) = b.isMin (y + 1) := by unfold isMin apply decide_eq_decide.mpr have hout : j + 1 + (y + 1) = 0 ∨ (j + 1 ≤ j + 1 + (y + 1) ∧ j + 1 + (y + 1) ≤ m) := Or.inr ⟨by omega, by omega⟩ have hidx : oIdx j (j + 1 + (y + 1)) = y + 1 := by rw [oIdx_pos (by omega)]; omega constructor · intro h k hk rcases k with _ | k · have := h 0 (by omega) rwa [joinAt_rel_out _ _ (Or.inl rfl) hout, oIdx_zero, hidx] at this · have := h (j + 1 + (k + 1)) (by omega) rwa [joinAt_rel_out _ _ (Or.inr ⟨by omega, by omega⟩) hout, hidx, oIdx_pos (by omega), show j + 1 + (k + 1) - (j + 1) = k + 1 by omega] at this · intro h k hk by_cases hk0 : k = 0 · subst hk0 rw [joinAt_rel_out _ _ (Or.inl rfl) hout, oIdx_zero, hidx] exact h 0 (by omega) · by_cases hkj : k ≤ j · exact joinAt_rel_cross _ _ ⟨by omega, hkj⟩ hout · rw [joinAt_rel_out _ _ (Or.inr ⟨by omega, by omega⟩) hout, hidx, oIdx_pos hk0] exact h _ (by omega) theorem blockCount_joinAt : (joinAt j hj a b).blockCount = a.blockCount + b.blockCount := by obtain ⟨r, hr⟩ : ∃ r, m - j = r + 1 := ⟨m - j - 1, by omega⟩ unfold blockCount have key : ∀ f : ℕ → ℕ, ∑ i ∈ Finset.range (m - j), f i = ∑ i ∈ Finset.range r, f (i + 1) + f 0 := fun f => by rw [hr, Finset.sum_range_succ'] rw [Finset.sum_range_succ', isMin_zero, if_pos rfl, ← Finset.sum_range_add_sum_Ico _ (show j ≤ m by omega), Finset.sum_Ico_eq_sum_range, key, key (fun i => if b.isMin i then 1 else 0), isMin_zero b] simp only [Nat.add_zero, if_pos] rw [isMin_joinAt_link, if_neg Bool.false_ne_true] have e1 : ∑ i ∈ Finset.range j, (if (joinAt j hj a b).isMin (i + 1) = true then 1 else 0) = ∑ i ∈ Finset.range j, (if a.isMin i = true then 1 else 0) := Finset.sum_congr rfl fun i hi => by rw [isMin_joinAt_in i (Finset.mem_range.mp hi)] have e2 : ∑ i ∈ Finset.range r, (if (joinAt j hj a b).isMin (j + (i + 1) + 1) = true then 1 else 0) = ∑ i ∈ Finset.range r, (if b.isMin (i + 1) = true then 1 else 0) := Finset.sum_congr rfl fun i hi => by rw [show j + (i + 1) + 1 = j + 1 + (i + 1) by omega, isMin_joinAt_out i (by have := Finset.mem_range.mp hi; omega)] rw [e1, e2] ring end JoinAtBlocks theorem blockCount_le {n : ℕ} (π : NC n) : π.blockCount ≤ n := by unfold blockCount calc ∑ i ∈ Finset.range n, (if π.isMin i then 1 else 0) ≤ ∑ _i ∈ Finset.range n, 1 := Finset.sum_le_sum fun i _ => by split_ifs <;> omega _ = n := by simp theorem blockCount_pos {n : ℕ} (hn : 0 < n) (π : NC n) : 0 < π.blockCount := by unfold blockCount obtain ⟨k, rfl⟩ : ∃ k, n = k + 1 := ⟨n - 1, by omega⟩ rw [Finset.sum_range_succ', isMin_zero] simp /-! ### Exact Narayana Block-Count Enumeration -/ /-- Narayana number `N(n, k) = (1/n) * C(n, k) * C(n, k - 1)` for `1 ≤ k ≤ n`, with `N(0, 0) = 1` and `0` outside the valid range. -/ def narayana (n k : ℕ) : ℕ := if n = 0 then if k = 0 then 1 else 0 else if k = 0 then 0 else (n.choose k * n.choose (k - 1)) / n /-- Exact divisibility: `n ∣ C(n, k) * C(n, k - 1)` for all `n ≥ 1, k ≥ 1`. -/ theorem narayana_dvd (n k : ℕ) (hn : 0 < n) (hk : 1 ≤ k) : n ∣ n.choose k * n.choose (k - 1) := by cases n with | zero => omega | succ m => cases k with | zero => omega | succ j => cases j with | zero => simp [Nat.choose_one_right] | succ r => have h1 : (r + 2) * (m + 1).choose (r + 2) = (m + 1) * m.choose (r + 1) := by have := Nat.add_one_mul_choose_eq m (r + 1); linarith have h2 : (r + 1) * (m + 1).choose (r + 1) = (m + 1) * m.choose r := by have := Nat.add_one_mul_choose_eq m r; linarith have hsub : (m + 1).choose (r + 2) * (m + 1).choose (r + 1) + (m + 1) * ((m + 1).choose (r + 2) * m.choose r) = (m + 1) * (m.choose (r + 1) * (m + 1).choose (r + 1)) := by have e1 : (r + 2) * ((m + 1).choose (r + 2) * (m + 1).choose (r + 1)) = (m + 1) * (m.choose (r + 1) * (m + 1).choose (r + 1)) := by calc (r + 2) * ((m + 1).choose (r + 2) * (m + 1).choose (r + 1)) = ((r + 2) * (m + 1).choose (r + 2)) * (m + 1).choose (r + 1) := by ring _ = ((m + 1) * m.choose (r + 1)) * (m + 1).choose (r + 1) := by rw [h1] _ = (m + 1) * (m.choose (r + 1) * (m + 1).choose (r + 1)) := by ring have e2 : (r + 1) * ((m + 1).choose (r + 2) * (m + 1).choose (r + 1)) = (m + 1) * ((m + 1).choose (r + 2) * m.choose r) := by calc (r + 1) * ((m + 1).choose (r + 2) * (m + 1).choose (r + 1)) = (m + 1).choose (r + 2) * ((r + 1) * (m + 1).choose (r + 1)) := by ring _ = (m + 1).choose (r + 2) * ((m + 1) * m.choose r) := by rw [h2] _ = (m + 1) * ((m + 1).choose (r + 2) * m.choose r) := by ring linarith have hdvd : (m + 1) ∣ (m + 1).choose (r + 2) * (m + 1).choose (r + 1) + (m + 1) * ((m + 1).choose (r + 2) * m.choose r) := by rw [hsub]; exact dvd_mul_right _ _ exact (Nat.dvd_add_left (dvd_mul_right _ _)).mp hdvd lemma mul_narayana (n k : ℕ) (hn : 0 < n) (hk : 1 ≤ k) : n * narayana n k = n.choose k * n.choose (k - 1) := by have hn0 : n ≠ 0 := Nat.ne_of_gt hn have hk0 : k ≠ 0 := by omega simp only [narayana, hn0, hk0, ↓reduceIte] exact Nat.mul_div_cancel' (narayana_dvd n k hn hk) lemma narayana_eq_zero_of_gt {n k : ℕ} (h : n < k) : narayana n k = 0 := by unfold narayana split_ifs with hn hk · omega · rfl · rfl · rw [Nat.choose_eq_zero_of_lt h, zero_mul, Nat.zero_div] /-- Symmetry of Narayana numbers: `N(n, k) = N(n, n + 1 - k)` for `1 ≤ k ≤ n`. -/ theorem narayana_symm (n k : ℕ) (hk1 : 1 ≤ k) (hkn : k ≤ n) : narayana n k = narayana n (n + 1 - k) := by have hn : 0 < n := by omega have h1 : 1 ≤ n + 1 - k := by omega apply Nat.eq_of_mul_eq_mul_left hn rw [mul_narayana n k hn hk1, mul_narayana n (n + 1 - k) hn h1] have hsub1 : n + 1 - k - 1 = n - k := by omega have hsub2 : n - (n + 1 - k) = k - 1 := by omega rw [hsub1, ← Nat.choose_symm hkn, mul_comm] congr 1 have hle : n + 1 - k ≤ n := by omega rw [← Nat.choose_symm hle, hsub2] /-- Exact 3-term linear Narayana recurrence for all `n ≥ 2` and `k ≥ 1`. -/ lemma narayana_three_term (n k : ℕ) (hn : 2 ≤ n) (hk : 1 ≤ k) : ((n + 2 : ℤ) * (narayana (n + 1) k : ℤ)) = (2 * (n : ℤ) + 1) * ((narayana n k : ℤ) + (narayana n (k - 1) : ℤ)) - ((n : ℤ) - 1) * ((narayana (n - 1) k : ℤ) - 2 * (narayana (n - 1) (k - 1) : ℤ) + (if 2 ≤ k then (narayana (n - 1) (k - 2) : ℤ) else 0)) := by suffices hQ : ((n + 2 : ℚ) * (narayana (n + 1) k : ℚ)) = (2 * (n : ℚ) + 1) * ((narayana n k : ℚ) + (narayana n (k - 1) : ℚ)) - ((n : ℚ) - 1) * ((narayana (n - 1) k : ℚ) - 2 * (narayana (n - 1) (k - 1) : ℚ) + (if 2 ≤ k then (narayana (n - 1) (k - 2) : ℚ) else 0)) by exact_mod_cast hQ have hnar : ∀ m j : ℕ, 0 < m → (narayana m j : ℚ) = if j = 0 then 0 else ((m.choose j : ℚ) * (m.choose (j - 1) : ℚ)) / (m : ℚ) := by intro m j hm by_cases hj : j = 0 · simp [narayana, Nat.ne_of_gt hm, hj] · simp only [hj, ↓reduceIte] have hmQ : (m : ℚ) ≠ 0 := Nat.cast_ne_zero.mpr (Nat.ne_of_gt hm) have hmul : (m : ℚ) * (narayana m j : ℚ) = (m.choose j : ℚ) * (m.choose (j - 1) : ℚ) := by exact_mod_cast mul_narayana m j hm (by omega) field_simp [hmQ]; linarith obtain ⟨m, rfl⟩ : ∃ m, n = m + 2 := ⟨n - 2, by omega⟩ cases k with | zero => omega | succ k' => cases k' with | zero => have hN1 : ∀ r : ℕ, 0 < r → narayana r 1 = 1 := by intro r hr have := mul_narayana r 1 hr le_rfl simp only [Nat.choose_one_right, Nat.sub_self, Nat.choose_zero_right, mul_one] at this exact Nat.eq_of_mul_eq_mul_left hr (by linarith) have hN0 : ∀ r : ℕ, 0 < r → narayana r 0 = 0 := by intro r hr; simp [narayana, Nat.ne_of_gt hr] simp [hN1 (m + 3) (by omega), hN1 (m + 2) (by omega), hN1 (m + 1) (by omega), hN0 (m + 2) (by omega), hN0 (m + 1) (by omega)] ring | succ j => cases j with | zero => have hN1 : ∀ r : ℕ, 0 < r → (narayana r 1 : ℚ) = 1 := by intro r hr; rw [hnar r 1 hr]; simp [Nat.choose_one_right, Nat.ne_of_gt hr] have hN2 : ∀ r : ℕ, 0 < r → (narayana r 2 : ℚ) = (r : ℚ) * ((r : ℚ) - 1) / 2 := by intro r hr rw [hnar r 2 hr, if_neg (by decide), show 2 - 1 = 1 by rfl, Nat.choose_one_right] have hch : (r.choose 2 : ℚ) = (r : ℚ) * ((r : ℚ) - 1) / 2 := by have h2 : 2 * r.choose 2 = r * (r - 1) := by rw [Nat.choose_two_right, Nat.mul_div_cancel' (Nat.even_mul_pred_self r).two_dvd] have h2Q : (2 : ℚ) * (r.choose 2 : ℚ) = (r : ℚ) * ((r : ℚ) - 1) := by have : ((r - 1 : ℕ) : ℚ) = (r : ℚ) - 1 := Nat.cast_sub hr rw [← this]; exact_mod_cast h2 linarith have hrQ : (r : ℚ) ≠ 0 := Nat.cast_ne_zero.mpr (Nat.ne_of_gt hr) rw [hch]; field_simp [hrQ] have hN0 : ∀ r : ℕ, 0 < r → (narayana r 0 : ℚ) = 0 := by intro r hr; rw [hnar r 0 hr]; simp simp only [show m + 2 - 1 = m + 1 by omega, show (2 : ℕ) ≤ 2 by omega, ↓reduceIte, Nat.sub_self, show 2 - 1 = 1 by omega] rw [hN2 (m + 3) (by omega), hN2 (m + 2) (by omega), hN2 (m + 1) (by omega), hN1 (m + 2) (by omega), hN1 (m + 1) (by omega), hN0 (m + 1) (by omega)] push_cast; ring | succ r => have habs : ∀ (M i : ℕ), ((i : ℚ) + 1) * (M.choose (i + 1) : ℚ) = ((M : ℚ) - (i : ℚ)) * (M.choose i : ℚ) := by intro M i by_cases h : i ≤ M · have hnat := Nat.choose_succ_right_eq M i have hsub : ((M - i : ℕ) : ℚ) = (M : ℚ) - (i : ℚ) := Nat.cast_sub h have hQ : (M.choose (i + 1) : ℚ) * ((i : ℚ) + 1) = (M.choose i : ℚ) * ((M : ℚ) - (i : ℚ)) := by rw [← hsub]; exact_mod_cast hnat linarith · have h1 : M.choose i = 0 := Nat.choose_eq_zero_of_lt (by omega) have h2 : M.choose (i + 1) = 0 := Nat.choose_eq_zero_of_lt (by omega) simp [h1, h2] have hP : ∀ (M i : ℕ), ((M + 1).choose (i + 1) : ℚ) = (M.choose i : ℚ) + (M.choose (i + 1) : ℚ) := fun M i => by exact_mod_cast Nat.choose_succ_succ M i simp only [show m + 2 - 1 = m + 1 by omega, show (2 : ℕ) ≤ r + 3 by omega, ↓reduceIte, show r + 3 - 1 = r + 2 by omega, show r + 3 - 2 = r + 1 by omega] rw [hnar (m + 3) (r + 3) (by omega), hnar (m + 2) (r + 3) (by omega), hnar (m + 2) (r + 2) (by omega), hnar (m + 1) (r + 3) (by omega), hnar (m + 1) (r + 2) (by omega), hnar (m + 1) (r + 1) (by omega)] simp only [Nat.succ_ne_zero, ↓reduceIte, show r + 3 - 1 = r + 2 by omega, show r + 2 - 1 = r + 1 by omega, show r + 1 - 1 = r by omega] rw [show m + 3 = (m + 2) + 1 by omega, show m + 2 = (m + 1) + 1 by omega] rw [hP ((m + 1) + 1) (r + 2), hP ((m + 1) + 1) (r + 1), hP (m + 1) (r + 2), hP (m + 1) (r + 1), hP (m + 1) r] set A : ℚ := ((m + 1).choose (r + 3) : ℚ) set B : ℚ := ((m + 1).choose (r + 2) : ℚ) set C : ℚ := ((m + 1).choose (r + 1) : ℚ) set D : ℚ := ((m + 1).choose r : ℚ) have hAB : ((r : ℚ) + 3) * A = ((m : ℚ) - (r : ℚ) - 1) * B := by have := habs (m + 1) (r + 2); push_cast at this; linarith have hBC : ((r : ℚ) + 2) * B = ((m : ℚ) - (r : ℚ)) * C := by have := habs (m + 1) (r + 1); push_cast at this; linarith have hCD : ((r : ℚ) + 1) * C = ((m : ℚ) - (r : ℚ) + 1) * D := by have := habs (m + 1) r; push_cast at this; linarith have hm1 : ((m : ℚ) + 1) ≠ 0 := by positivity have hm2 : ((m : ℚ) + 2) ≠ 0 := by positivity have hm3 : ((m : ℚ) + 3) ≠ 0 := by positivity have hr1 : ((r : ℚ) + 1) ≠ 0 := by positivity have hr2 : ((r : ℚ) + 2) ≠ 0 := by positivity have hr3 : ((r : ℚ) + 3) ≠ 0 := by positivity have hC_eq : C = ((m : ℚ) - (r : ℚ) + 1) / ((r : ℚ) + 1) * D := by field_simp [hr1]; linarith have hB_eq : B = ((m : ℚ) - (r : ℚ)) / ((r : ℚ) + 2) * C := by field_simp [hr2]; linarith have hA_eq : A = ((m : ℚ) - (r : ℚ) - 1) / ((r : ℚ) + 3) * B := by field_simp [hr3]; linarith push_cast rw [hA_eq, hB_eq, hC_eq] field_simp [hm1, hm2, hm3, hr1, hr2, hr3] ring /-- Narayana polynomial `P_n(t) = ∑_{k=1}^n N(n, k) t^k` (`0` for `n = 0`). -/ def narayanaPoly (t : ℚ) (n : ℕ) : ℚ := if n = 0 then 0 else ∑ k ∈ Finset.Icc 1 n, (narayana n k : ℚ) * t ^ k lemma narayanaPoly_three_term (t : ℚ) (n : ℕ) (hn : 2 ≤ n) : ((n : ℚ) + 2) * narayanaPoly t (n + 1) = (2 * (n : ℚ) + 1) * (t + 1) * narayanaPoly t n - ((n : ℚ) - 1) * (t - 1) ^ 2 * narayanaPoly t (n - 1) := by have hn0 : n ≠ 0 := by omega have hnm0 : n - 1 ≠ 0 := by omega simp only [narayanaPoly, Nat.succ_ne_zero, hn0, hnm0, ↓reduceIte] have hsum_ext : ∀ (m : ℕ), m ≤ n + 1 → ∑ k ∈ Finset.Icc 1 m, (narayana m k : ℚ) * t ^ k = ∑ k ∈ Finset.Icc 1 (n + 1), (narayana m k : ℚ) * t ^ k := by intro m hm apply Finset.sum_subset (Finset.Icc_subset_Icc_right hm) intro k hk hnot; simp only [Finset.mem_Icc] at hk hnot simp [narayana_eq_zero_of_gt (by omega : m < k)] have hshift1 : ∀ (m : ℕ), 1 ≤ m → m ≤ n → t * (∑ k ∈ Finset.Icc 1 m, (narayana m k : ℚ) * t ^ k) = ∑ k ∈ Finset.Icc 1 (n + 1), (narayana m (k - 1) : ℚ) * t ^ k := by intro m hm1 hm rw [Finset.mul_sum] have h1 : ∑ k ∈ Finset.Icc 1 m, t * ((narayana m k : ℚ) * t ^ k) = ∑ k ∈ Finset.Icc 2 (m + 1), (narayana m (k - 1) : ℚ) * t ^ k := by refine Finset.sum_nbij (· + 1) ?_ ?_ ?_ ?_ · intro k hk; simp only [Finset.mem_Icc] at hk ⊢; omega · intro a _ b _ h; dsimp at h; omega · intro k hk; simp only [Finset.mem_coe, Finset.mem_Icc] at hk exact ⟨k - 1, Finset.mem_coe.mpr (Finset.mem_Icc.mpr ⟨by omega, by omega⟩), by dsimp; omega⟩ · intro k _; simp only [Nat.add_sub_cancel]; ring rw [h1] apply Finset.sum_subset (Finset.Icc_subset_Icc (by omega) (by omega)) intro k hk hnot; simp only [Finset.mem_Icc] at hk hnot by_cases hk1 : k = 1 · subst hk1 simp [narayana, show m ≠ 0 by omega] · simp [narayana_eq_zero_of_gt (by omega : m < k - 1)] have hshift2 : t ^ 2 * (∑ k ∈ Finset.Icc 1 (n - 1), (narayana (n - 1) k : ℚ) * t ^ k) = ∑ k ∈ Finset.Icc 1 (n + 1), (if 2 ≤ k then (narayana (n - 1) (k - 2) : ℚ) else 0) * t ^ k := by rw [Finset.mul_sum] have h1 : ∑ k ∈ Finset.Icc 1 (n - 1), t ^ 2 * ((narayana (n - 1) k : ℚ) * t ^ k) = ∑ k ∈ Finset.Icc 3 (n + 1), (if 2 ≤ k then (narayana (n - 1) (k - 2) : ℚ) else 0) * t ^ k := by refine Finset.sum_nbij (· + 2) ?_ ?_ ?_ ?_ · intro k hk; simp only [Finset.mem_Icc] at hk ⊢; omega · intro a _ b _ h; dsimp at h; omega · intro k hk; simp only [Finset.mem_coe, Finset.mem_Icc] at hk exact ⟨k - 2, Finset.mem_coe.mpr (Finset.mem_Icc.mpr ⟨by omega, by omega⟩), by dsimp; omega⟩ · intro k _; simp only [show 2 ≤ k + 2 by omega, ↓reduceIte, Nat.add_sub_cancel]; ring rw [h1] apply Finset.sum_subset (Finset.Icc_subset_Icc (by omega) (by omega)) intro k hk hnot; simp only [Finset.mem_Icc] at hk hnot by_cases hk2 : 2 ≤ k · have : k = 2 := by omega subst this; simp [narayana, hnm0] · simp [hk2] have hA : (t + 1) * ∑ k ∈ Finset.Icc 1 n, (narayana n k : ℚ) * t ^ k = ∑ k ∈ Finset.Icc 1 (n + 1), ((narayana n k : ℚ) + (narayana n (k - 1) : ℚ)) * t ^ k := by rw [add_mul, one_mul, hshift1 n (by omega) le_rfl, hsum_ext n (by omega), ← Finset.sum_add_distrib] exact Finset.sum_congr rfl fun k _ => by ring have hB : (t - 1) ^ 2 * ∑ k ∈ Finset.Icc 1 (n - 1), (narayana (n - 1) k : ℚ) * t ^ k = ∑ k ∈ Finset.Icc 1 (n + 1), ((narayana (n - 1) k : ℚ) - 2 * (narayana (n - 1) (k - 1) : ℚ) + (if 2 ≤ k then (narayana (n - 1) (k - 2) : ℚ) else 0)) * t ^ k := by have hsq : (t - 1) ^ 2 * ∑ k ∈ Finset.Icc 1 (n - 1), (narayana (n - 1) k : ℚ) * t ^ k = (∑ k ∈ Finset.Icc 1 (n - 1), (narayana (n - 1) k : ℚ) * t ^ k) - 2 * (t * ∑ k ∈ Finset.Icc 1 (n - 1), (narayana (n - 1) k : ℚ) * t ^ k) + (t ^ 2 * ∑ k ∈ Finset.Icc 1 (n - 1), (narayana (n - 1) k : ℚ) * t ^ k) := by ring rw [hsq, hshift1 (n - 1) (by omega) (by omega), hshift2, hsum_ext (n - 1) (by omega), Finset.mul_sum, ← Finset.sum_sub_distrib, ← Finset.sum_add_distrib] exact Finset.sum_congr rfl fun k _ => by ring rw [mul_assoc, hA, mul_assoc, hB, Finset.mul_sum, Finset.mul_sum, Finset.mul_sum, ← Finset.sum_sub_distrib] refine Finset.sum_congr rfl fun k hk => ?_ have hk1 : 1 ≤ k := (Finset.mem_Icc.mp hk).1 have hrec : ((n : ℚ) + 2) * (narayana (n + 1) k : ℚ) = (2 * (n : ℚ) + 1) * ((narayana n k : ℚ) + (narayana n (k - 1) : ℚ)) - ((n : ℚ) - 1) * ((narayana (n - 1) k : ℚ) - 2 * (narayana (n - 1) (k - 1) : ℚ) + (if 2 ≤ k then (narayana (n - 1) (k - 2) : ℚ) else 0)) := by exact_mod_cast narayana_three_term n k hn hk1 calc ((n : ℚ) + 2) * ((narayana (n + 1) k : ℚ) * t ^ k) = (((n : ℚ) + 2) * (narayana (n + 1) k : ℚ)) * t ^ k := by ring _ = ((2 * (n : ℚ) + 1) * ((narayana n k : ℚ) + (narayana n (k - 1) : ℚ)) - ((n : ℚ) - 1) * ((narayana (n - 1) k : ℚ) - 2 * (narayana (n - 1) (k - 1) : ℚ) + (if 2 ≤ k then (narayana (n - 1) (k - 2) : ℚ) else 0))) * t ^ k := by rw [hrec] _ = _ := by ring /-- **Algebraic Narayana Quadratic Equation (via ODE `2 D S' = D' S`):** The formal power series `P(z) = ∑_{n ≥ 1} P_n(t) z^n` satisfies `P = X * (1 + P) * (C t + P)`. -/ theorem narayanaPowerSeries_quadratic (t : ℚ) : let P : PowerSeries ℚ := PowerSeries.mk (narayanaPoly t) P = PowerSeries.X * (1 + P) * (PowerSeries.C t + P) := by intro P let S : PowerSeries ℚ := 1 - PowerSeries.C (t + 1) * PowerSeries.X - PowerSeries.C 2 * PowerSeries.X * P let D : PowerSeries ℚ := 1 - PowerSeries.C (2 * (t + 1)) * PowerSeries.X + PowerSeries.C ((t - 1) ^ 2) * PowerSeries.X ^ 2 have hP0 : narayanaPoly t 0 = 0 := by simp [narayanaPoly] have hP1 : narayanaPoly t 1 = t := by simp [narayanaPoly, narayana] have hP2 : narayanaPoly t 2 = t + t ^ 2 := by have h12 : Finset.Icc 1 2 = {1, 2} := by decide simp [narayanaPoly, h12, narayana] have hS_coeff : ∀ m : ℕ, PowerSeries.coeff m S = match m with | 0 => 1 | 1 => -(t + 1) | k + 2 => -2 * narayanaPoly t (k + 1) := by intro m cases m with | zero => simp [S] | succ k => cases k with | zero => simp [S, P, hP0, mul_assoc] | succ j => simp only [S, P, map_sub, mul_assoc, PowerSeries.coeff_C_mul, PowerSeries.coeff_succ_X_mul, PowerSeries.coeff_X, PowerSeries.coeff_mk] simp [PowerSeries.coeff_one] have hODE : PowerSeries.C 2 * D * PowerSeries.derivative ℚ S = PowerSeries.derivative ℚ D * S := by have hD' : PowerSeries.derivative ℚ D = -PowerSeries.C (2 * (t + 1)) + PowerSeries.C (2 * (t - 1) ^ 2) * PowerSeries.X := by ext m rw [PowerSeries.coeff_derivative] cases m with | zero => simp only [D, map_add, map_sub, map_neg, PowerSeries.coeff_one, PowerSeries.coeff_C_mul, PowerSeries.coeff_X, pow_two, mul_assoc, PowerSeries.coeff_succ_X_mul, PowerSeries.coeff_C] norm_num | succ k => cases k with | zero => simp only [D, map_add, map_sub, map_neg, PowerSeries.coeff_one, PowerSeries.coeff_C_mul, PowerSeries.coeff_X, pow_two, mul_assoc, PowerSeries.coeff_succ_X_mul, PowerSeries.coeff_C] norm_num; ring | succ j => have hne : j + 2 ≠ 1 := by omega simp only [D, map_add, map_sub, map_neg, PowerSeries.coeff_one, PowerSeries.coeff_C_mul, PowerSeries.coeff_X, pow_two, mul_assoc, PowerSeries.coeff_succ_X_mul, PowerSeries.coeff_C, Nat.succ_ne_zero, ↓reduceIte, hne] norm_num have hX : ∀ (a : ℚ) (f : PowerSeries ℚ) (m : ℕ), PowerSeries.coeff m (PowerSeries.C a * PowerSeries.X * f) = if m = 0 then 0 else a * PowerSeries.coeff (m - 1) f := by intro a f m cases m with | zero => simp [mul_assoc] | succ k => simp [mul_assoc, PowerSeries.coeff_succ_X_mul] have hX2 : ∀ (a : ℚ) (f : PowerSeries ℚ) (m : ℕ), PowerSeries.coeff m (PowerSeries.C a * PowerSeries.X ^ 2 * f) = if m < 2 then 0 else a * PowerSeries.coeff (m - 2) f := by intro a f m cases m with | zero => simp [pow_two, mul_assoc] | succ k => cases k with | zero => simp [pow_two, mul_assoc] | succ j => simp only [show ¬ (j + 2 < 2) by omega, ↓reduceIte, show j + 2 - 2 = j by omega] rw [pow_two, mul_assoc, mul_assoc, PowerSeries.coeff_C_mul, PowerSeries.coeff_succ_X_mul, PowerSeries.coeff_succ_X_mul] ext m have hLHS_eq : PowerSeries.C 2 * D * PowerSeries.derivative ℚ S = PowerSeries.C 2 * PowerSeries.derivative ℚ S - PowerSeries.C (4 * (t + 1)) * PowerSeries.X * PowerSeries.derivative ℚ S + PowerSeries.C (2 * (t - 1) ^ 2) * PowerSeries.X ^ 2 * PowerSeries.derivative ℚ S := by simp only [D] have hc1 : PowerSeries.C (4 * (t + 1)) = PowerSeries.C 2 * PowerSeries.C (2 * (t + 1)) := by rw [← map_mul]; congr 1; ring have hc2 : PowerSeries.C (2 * (t - 1) ^ 2) = PowerSeries.C 2 * PowerSeries.C ((t - 1) ^ 2) := by rw [← map_mul] rw [hc1, hc2]; ring have hRHS_eq : PowerSeries.derivative ℚ D * S = -(PowerSeries.C (2 * (t + 1)) * S) + PowerSeries.C (2 * (t - 1) ^ 2) * PowerSeries.X * S := by rw [hD']; ring rw [hLHS_eq, hRHS_eq, map_add, map_sub, map_add, map_neg, PowerSeries.coeff_C_mul, PowerSeries.coeff_C_mul, hX, hX2, hX] simp only [PowerSeries.coeff_derivative] cases m with | zero => simp [hS_coeff]; ring | succ k => cases k with | zero => simp [hS_coeff, hP1]; ring | succ j => cases j with | zero => simp [hS_coeff, hP1, hP2]; ring | succ r => simp only [Nat.succ_ne_zero, ↓reduceIte, show ¬ (r + 3 < 2) by omega, show r + 3 - 1 = r + 2 by omega, show r + 3 - 2 = r + 1 by omega] rw [hS_coeff (r + 4), hS_coeff (r + 3), hS_coeff (r + 2)] have hrec := narayanaPoly_three_term t (r + 2) (by omega) simp only [show r + 2 - 1 = r + 1 by omega] at hrec push_cast at hrec ⊢ linarith have hD_const : PowerSeries.constantCoeff D = 1 := by simp [D] have hD_unit : IsUnit D := PowerSeries.isUnit_iff_constantCoeff.mpr (by rw [hD_const]; exact isUnit_one) obtain ⟨uD, huD⟩ := hD_unit set v : PowerSeries ℚ := (uD⁻¹ : (PowerSeries ℚ)ˣ).val have hvD : v * D = 1 := by show (uD⁻¹ : (PowerSeries ℚ)ˣ).val * D = 1; rw [← huD]; exact uD.inv_val have hderiv_zero : PowerSeries.derivative ℚ (S ^ 2 * v) = 0 := by have hD_inv_deriv : PowerSeries.derivative ℚ v * D + v * PowerSeries.derivative ℚ D = 0 := by have := congrArg (PowerSeries.derivative ℚ) hvD rw [Derivation.leibniz, PowerSeries.derivative_one] at this simp only [smul_eq_mul] at this linear_combination this have hmul : PowerSeries.derivative ℚ (S ^ 2 * v) * D = 0 := by rw [Derivation.leibniz, PowerSeries.derivative_pow] simp only [smul_eq_mul, Nat.cast_ofNat, show (2 : ℕ) - 1 = 1 by rfl, pow_one] have h1 : (S ^ 2 * PowerSeries.derivative ℚ v + v * (2 * S * PowerSeries.derivative ℚ S)) * D = S * (PowerSeries.C 2 * D * PowerSeries.derivative ℚ S) * v + S ^ 2 * (PowerSeries.derivative ℚ v * D) := by have h2 : (2 : PowerSeries ℚ) = PowerSeries.C 2 := by simp [map_ofNat] rw [h2]; ring rw [h1, hODE] have h2 : PowerSeries.derivative ℚ v * D = -v * PowerSeries.derivative ℚ D := by linear_combination hD_inv_deriv rw [h2]; ring calc PowerSeries.derivative ℚ (S ^ 2 * v) = PowerSeries.derivative ℚ (S ^ 2 * v) * (D * v) := by rw [mul_comm D, hvD, mul_one] _ = (PowerSeries.derivative ℚ (S ^ 2 * v) * D) * v := by ring _ = 0 := by rw [hmul, zero_mul] have hS2_eq_D : S ^ 2 = D := by have hconst : PowerSeries.constantCoeff (S ^ 2 * v) = 1 := by have hS0 : PowerSeries.constantCoeff S = 1 := by simp [S] have hinv0 : PowerSeries.constantCoeff v = 1 := by have := congrArg PowerSeries.constantCoeff hvD simp only [map_mul, map_one, hD_const, mul_one] at this exact this simp [map_mul, map_pow, hS0, hinv0] have hEq : S ^ 2 * v = 1 := by apply PowerSeries.derivative.ext (R := ℚ) · rw [hderiv_zero, PowerSeries.derivative_one] · simpa using hconst calc S ^ 2 = (S ^ 2 * v) * D := by rw [mul_assoc, hvD, mul_one] _ = 1 * D := by rw [hEq] _ = D := one_mul D have h4X : PowerSeries.C 4 * PowerSeries.X * (PowerSeries.X * (1 + P) * (PowerSeries.C t + P) - P) = 0 := by have hdiff : S ^ 2 - D = PowerSeries.C 4 * PowerSeries.X * (PowerSeries.X * (1 + P) * (PowerSeries.C t + P) - P) := by simp only [S, D] have h2t : PowerSeries.C (2 * (t + 1)) = PowerSeries.C 2 * PowerSeries.C (t + 1) := by rw [← map_mul] have htm : PowerSeries.C ((t - 1) ^ 2) = (PowerSeries.C t - 1) ^ 2 := by rw [map_pow, map_sub, map_one] have htp : PowerSeries.C (t + 1) = PowerSeries.C t + 1 := by rw [map_add, map_one] have h4 : PowerSeries.C (4 : ℚ) = PowerSeries.C 2 * PowerSeries.C 2 := by rw [← map_mul]; exact congrArg PowerSeries.C (by norm_num) have h2c : PowerSeries.C (2 : ℚ) = 2 := map_ofNat PowerSeries.C 2 rw [h2t, htm, htp, h4, h2c]; ring rw [← hdiff, hS2_eq_D, sub_self] ext n have hcoeff := congrArg (PowerSeries.coeff (n + 1)) h4X rw [map_zero, mul_assoc (PowerSeries.C 4), PowerSeries.coeff_C_mul, PowerSeries.coeff_succ_X_mul] at hcoeff have h0 : PowerSeries.coeff n (PowerSeries.X * (1 + P) * (PowerSeries.C t + P) - P) = 0 := (mul_eq_zero.mp hcoeff).resolve_left (by norm_num) rw [map_sub] at h0 linarith /-- Block-count polynomial of `NC(n)`: `Q_n(t) = ∑_{π ∈ NC(n)} t^{|π|}` for `n ≥ 1`, `0` for `n = 0`. -/ def ncBlockPoly (t : ℚ) (n : ℕ) : ℚ := if n = 0 then 0 else ∑ π : NC n, t ^ π.blockCount lemma ncBlockSum_eq (t : ℚ) (n : ℕ) : (∑ π : NC n, t ^ π.blockCount) = (if n = 0 then 1 else 0) + ncBlockPoly t n := by cases n with | zero => have : Unique (NC 0) := ⟨⟨⊥⟩, fun π => ext' fun _ _ hi => absurd hi (Nat.not_lt_zero _)⟩ simp [ncBlockPoly, blockCount] | succ m => simp [ncBlockPoly] /-- Exact quadratic power-series equation for `ncBlockPoly t` directly from `sum_decomp`. -/ theorem ncBlockPowerSeries_quadratic (t : ℚ) : let Q : PowerSeries ℚ := PowerSeries.mk (ncBlockPoly t) Q = PowerSeries.X * (1 + Q) * (PowerSeries.C t + Q) := by intro Q have hM : ∀ n : ℕ, PowerSeries.coeff n (1 + Q) = ∑ π : NC n, t ^ π.blockCount := by intro n; rw [map_add, PowerSeries.coeff_one, PowerSeries.coeff_mk, ncBlockSum_eq] have hR : ∀ n : ℕ, PowerSeries.coeff n (PowerSeries.C t + Q) = if n = 0 then t else ncBlockPoly t n := by intro n; rw [map_add, PowerSeries.coeff_C, PowerSeries.coeff_mk] cases n with | zero => simp [ncBlockPoly] | succ m => simp ext n cases n with | zero => simp [Q, ncBlockPoly] | succ m => rw [PowerSeries.coeff_mk, mul_assoc, PowerSeries.coeff_succ_X_mul, PowerSeries.coeff_mul] have hLHS : ncBlockPoly t (m + 1) = t * (∑ π' : NC m, t ^ π'.blockCount) + ∑ j : Fin m, (∑ π_in : NC j.1, t ^ π_in.blockCount) * (∑ π_out : NC (m - j.1), t ^ π_out.blockCount) := by simp only [ncBlockPoly, Nat.succ_ne_zero, ↓reduceIte] rw [sum_decomp m (fun π => t ^ π.blockCount)] congr 1 · rw [Finset.mul_sum] exact Finset.sum_congr rfl fun σ _ => by rw [blockCount_prepend, pow_succ'] · refine Finset.sum_congr rfl fun j _ => ?_ rw [Fintype.sum_prod_type, Finset.sum_mul] refine Finset.sum_congr rfl fun a _ => ?_ rw [Finset.mul_sum] exact Finset.sum_congr rfl fun b _ => by rw [blockCount_joinAt, pow_add] rw [hLHS, Finset.Nat.sum_antidiagonal_eq_sum_range_succ (fun a b => PowerSeries.coeff a (1 + Q) * PowerSeries.coeff b (PowerSeries.C t + Q)) m] rw [Finset.sum_range_succ] simp only [Nat.sub_self, hM, hR, ↓reduceIte] rw [add_comm, mul_comm _ t] congr 1 rw [← Fin.sum_univ_eq_sum_range] refine Finset.sum_congr rfl fun j _ => ?_ have hne : m - j.1 ≠ 0 := by omega simp only [hne, ↓reduceIte, ncBlockPoly] theorem ncBlockPoly_eq_narayanaPoly (t : ℚ) (n : ℕ) : ncBlockPoly t n = narayanaPoly t n := by let P : PowerSeries ℚ := PowerSeries.mk (narayanaPoly t) let Q : PowerSeries ℚ := PowerSeries.mk (ncBlockPoly t) have hP := narayanaPowerSeries_quadratic t have hQ := ncBlockPowerSeries_quadratic t suffices PowerSeries.coeff n Q = PowerSeries.coeff n P by simpa [P, Q] using this induction n using Nat.strong_induction_on with | _ n ih => cases n with | zero => simp [P, Q, narayanaPoly, ncBlockPoly] | succ m => have hQ' : Q = PowerSeries.X * (1 + Q) * (PowerSeries.C t + Q) := hQ have hP' : P = PowerSeries.X * (1 + P) * (PowerSeries.C t + P) := hP have h1 : PowerSeries.coeff (m + 1) Q = ∑ ij ∈ Finset.HasAntidiagonal.antidiagonal m, PowerSeries.coeff ij.1 (1 + Q) * PowerSeries.coeff ij.2 (PowerSeries.C t + Q) := by conv_lhs => rw [hQ'] rw [mul_assoc, PowerSeries.coeff_succ_X_mul, PowerSeries.coeff_mul] have h2 : PowerSeries.coeff (m + 1) P = ∑ ij ∈ Finset.HasAntidiagonal.antidiagonal m, PowerSeries.coeff ij.1 (1 + P) * PowerSeries.coeff ij.2 (PowerSeries.C t + P) := by conv_lhs => rw [hP'] rw [mul_assoc, PowerSeries.coeff_succ_X_mul, PowerSeries.coeff_mul] rw [h1, h2] refine Finset.sum_congr rfl fun ij hij => ?_ have hsum : ij.1 + ij.2 = m := Finset.HasAntidiagonal.mem_antidiagonal.mp hij have hle1 : ij.1 < m + 1 := by omega have hle2 : ij.2 < m + 1 := by omega simp only [map_add, ih ij.1 hle1, ih ij.2 hle2] /-- **Exact Narayana Block-Count Enumeration of `NC(n)`:** For all `n ≥ 1` and `1 ≤ k ≤ n`, `|\{π ∈ NC(n) : |π| = k\}| = N(n, k) = (1/n) C(n, k) C(n, k - 1)`. -/ theorem card_NC_blockCount (n k : ℕ) (hn : 1 ≤ n) (hk1 : 1 ≤ k) (hkn : k ≤ n) : (Finset.univ.filter fun π : NC n => π.blockCount = k).card = narayana n k := by let polyNC : Polynomial ℚ := ∑ j ∈ Finset.Icc 1 n, Polynomial.C ((Finset.univ.filter fun π : NC n => π.blockCount = j).card : ℚ) * Polynomial.X ^ j let polyNar : Polynomial ℚ := ∑ j ∈ Finset.Icc 1 n, Polynomial.C (narayana n j : ℚ) * Polynomial.X ^ j have heval : ∀ t : ℚ, polyNC.eval t = polyNar.eval t := by intro t have hL : polyNC.eval t = ncBlockPoly t n := by simp only [polyNC, Polynomial.eval_finsetSum, Polynomial.eval_mul, Polynomial.eval_C, Polynomial.eval_pow, Polynomial.eval_X, ncBlockPoly, show n ≠ 0 by omega, ↓reduceIte] have hpart : (Finset.univ : Finset (NC n)) = (Finset.Icc 1 n).biUnion (fun j => Finset.univ.filter fun π : NC n => π.blockCount = j) := by ext π simp only [Finset.mem_univ, Finset.mem_biUnion, Finset.mem_Icc, Finset.mem_filter, true_and, true_iff] exact ⟨π.blockCount, ⟨blockCount_pos (by omega) π, blockCount_le π⟩, rfl⟩ have hdisj : Set.PairwiseDisjoint (↑(Finset.Icc 1 n) : Set ℕ) (fun j => Finset.univ.filter fun π : NC n => π.blockCount = j) := by intro a _ b _ hab change Disjoint _ _ rw [Finset.disjoint_left] intro π h1 h2 simp only [Finset.mem_filter] at h1 h2 exact hab (h1.2.symm.trans h2.2) conv_rhs => rw [hpart, Finset.sum_biUnion hdisj] refine Finset.sum_congr rfl fun j _ => ?_ rw [Finset.sum_congr rfl fun π hπ => by rw [(Finset.mem_filter.mp hπ).2]] simp [Finset.sum_const, nsmul_eq_mul] have hR : polyNar.eval t = narayanaPoly t n := by simp only [polyNar, Polynomial.eval_finsetSum, Polynomial.eval_mul, Polynomial.eval_C, Polynomial.eval_pow, Polynomial.eval_X, narayanaPoly, show n ≠ 0 by omega, ↓reduceIte] rw [hL, hR, ncBlockPoly_eq_narayanaPoly] have hpoly : polyNC = polyNar := Polynomial.funext heval have hcoeff := congrArg (fun p => p.coeff k) hpoly have hk_mem : k ∈ Finset.Icc 1 n := Finset.mem_Icc.mpr ⟨hk1, hkn⟩ simp only [polyNC, polyNar, Polynomial.finsetSum_coeff, Polynomial.coeff_C_mul, Polynomial.coeff_X_pow, mul_ite, mul_one, mul_zero, Finset.sum_ite_eq, hk_mem, ↓reduceIte] at hcoeff exact_mod_cast hcoeff /-- **Catalan = Sum of Narayana Numbers:** `∑_{k=1}^n N(n, k) = C_n` for all `n ≥ 1`. -/ theorem sum_narayana_eq_catalan (n : ℕ) (hn : 1 ≤ n) : ∑ k ∈ Finset.Icc 1 n, narayana n k = catalan n := by have h := ncBlockPoly_eq_narayanaPoly 1 n simp only [ncBlockPoly, narayanaPoly, show n ≠ 0 by omega, ↓reduceIte, one_pow, mul_one, Finset.sum_const, Finset.card_univ, nsmul_eq_mul, mul_one] at h rw [card_NC] at h have hQ : (∑ k ∈ Finset.Icc 1 n, (narayana n k : ℚ)) = (catalan n : ℚ) := h.symm exact_mod_cast hQ /-! ### Exact Kreweras Complement `K : NC(n) → NC(n)` -/ /-- The half-open interval `(lo, hi]` is closed under blocks of `π`. -/ def closedInterval {n : ℕ} (π : NC n) (lo hi : ℕ) : Bool := decide (∀ a ∈ Finset.range n, ∀ b ∈ Finset.range n, lo < a → a ≤ hi → π.rel a b = true → lo < b ∧ b ≤ hi) lemma closedInterval_iff {n : ℕ} (π : NC n) (lo hi : ℕ) : closedInterval π lo hi = true ↔ ∀ a < n, ∀ b < n, lo < a → a ≤ hi → π.rel a b = true → lo < b ∧ b ≤ hi := by unfold closedInterval rw [decide_eq_true_iff] simp only [Finset.mem_range] /-- **Kreweras Complement `K(π)`:** `i ~_{K(π)} j` iff the half-open interval `(min i j, max i j]` is a union of blocks of `π`. -/ def kreweras {n : ℕ} (π : NC n) : NC n where rel i j := decide (i < n ∧ j < n ∧ closedInterval π (min i j) (max i j) = true) supp i j h := by have := of_decide_eq_true h; exact ⟨this.1, this.2.1⟩ refl i hi := by apply decide_eq_true refine ⟨hi, hi, ?_⟩ rw [closedInterval_iff] intro a _ _ _ h1 h2; simp at h1 h2; omega symm i j := decide_eq_decide.mpr (by rw [min_comm, max_comm]; tauto) trans i j k h1 h2 := by obtain ⟨hi, hj, hc1⟩ := of_decide_eq_true h1 obtain ⟨_, hk, hc2⟩ := of_decide_eq_true h2 rw [closedInterval_iff] at hc1 hc2 apply decide_eq_true refine ⟨hi, hk, (closedInterval_iff _ _ _).mpr ?_⟩ intro a ha b hb hlo hhi hab -- Every element in (min i k, max i k] is handled by the two closed intervals (min i j, max i j] and (min j k, max j k] by_cases h1in : min i j < a ∧ a ≤ max i j · have hb1 := hc1 a ha b hb h1in.1 h1in.2 hab by_cases h2in : min j k < a ∧ a ≤ max j k · -- a in both intervals -> impossible since a ∈ (min i k, max i k], wait: can a be in both? -- If a is in both, b is in both, which is ONLY outside (min i k, max i k] if b is in the overlap; -- wait: if j is outside [min i k, max i k], then (min i k, max i k] is the SYMMETRIC DIFFERENCE! -- So a ∈ (min i k, max i k] CANNOT be in BOTH (min i j, max i j] and (min j k, max j k]! omega · -- a is ONLY in (min i j, max i j], NOT in (min j k, max j k] -- Could b be in (min j k, max j k]? If so, by hc2 on (b, a), a would be in (min j k, max j k], contradiction! have hb_not2 : ¬ (min j k < b ∧ b ≤ max j k) := fun hb2 => h2in (hc2 b hb a ha hb2.1 hb2.2 (by rw [π.symm]; exact hab)) omega · have h2in : min j k < a ∧ a ≤ max j k := by omega have hb2 := hc2 a ha b hb h2in.1 h2in.2 hab have hb_not1 : ¬ (min i j < b ∧ b ≤ max i j) := fun hb1 => h1in (hc1 b hb a ha hb1.1 hb1.2 (by rw [π.symm]; exact hab)) omega nc a b c d hab hbc hcd hac hbd := by obtain ⟨ha, hc, hc1⟩ := of_decide_eq_true hac obtain ⟨hb, hd, hc2⟩ := of_decide_eq_true hbd rw [closedInterval_iff] at hc1 hc2 rw [min_eq_left (by omega : a ≤ c), max_eq_right (by omega : a ≤ c)] at hc1 rw [min_eq_left (by omega : b ≤ d), max_eq_right (by omega : b ≤ d)] at hc2 apply decide_eq_true refine ⟨ha, hb, (closedInterval_iff _ _ _).mpr ?_⟩ rw [min_eq_left (by omega : a ≤ b), max_eq_right (by omega : a ≤ b)] intro x hx y hy hxlo hxhi hxy have hy_ac := hc1 x hx y hy hxlo (by omega) hxy have hy_not_bd : ¬ (b < y ∧ y ≤ d) := fun hybd => by have hxbd := hc2 y hy x hx hybd.1 hybd.2 (by rw [π.symm]; exact hxy) omega omega lemma kreweras_rel_iff {n : ℕ} (π : NC n) (i j : ℕ) : (kreweras π).rel i j = true ↔ i < n ∧ j < n ∧ closedInterval π (min i j) (max i j) = true := decide_eq_true_iff /-- **Order-Reversing Property of Kreweras Complement:** `π ≤ σ → K(σ) ≤ K(π)`. -/ theorem kreweras_antitone {n : ℕ} {π σ : NC n} (h : π ≤ σ) : kreweras σ ≤ kreweras π := by intro i j hij obtain ⟨hi, hj, hc⟩ := (kreweras_rel_iff σ i j).mp hij rw [closedInterval_iff] at hc exact (kreweras_rel_iff π i j).mpr ⟨hi, hj, (closedInterval_iff _ _ _).mpr fun a ha b hb hlo hhi hab => hc a ha b hb hlo hhi (h a b hab)⟩ /-- Cyclic rotation by `-1 (mod n)`: `i ~' j` iff `(i + 1) % n ~ (j + 1) % n`. -/ def rotMinusOne {n : ℕ} (π : NC n) : NC n where rel i j := decide (i < n ∧ j < n ∧ π.rel ((i + 1) % n) ((j + 1) % n) = true) supp i j h := by have := of_decide_eq_true h; exact ⟨this.1, this.2.1⟩ refl i hi := decide_eq_true ⟨hi, hi, π.refl _ (Nat.mod_lt _ (by omega))⟩ symm i j := decide_eq_decide.mpr (by rw [π.symm]; tauto) trans i j k h1 h2 := by obtain ⟨hi, _, h1'⟩ := of_decide_eq_true h1 obtain ⟨_, hk, h2'⟩ := of_decide_eq_true h2 exact decide_eq_true ⟨hi, hk, π.trans _ _ _ h1' h2'⟩ nc a b c d hab hbc hcd hac hbd := by obtain ⟨ha, hc, hac'⟩ := of_decide_eq_true hac obtain ⟨hb, hd, hbd'⟩ := of_decide_eq_true hbd have ha' : (a + 1) % n = a + 1 := Nat.mod_eq_of_lt (by omega) have hb' : (b + 1) % n = b + 1 := Nat.mod_eq_of_lt (by omega) have hc' : (c + 1) % n = c + 1 := Nat.mod_eq_of_lt (by omega) rw [ha', hc'] at hac' rw [hb'] at hbd' apply decide_eq_true refine ⟨ha, hb, ?_⟩ rw [ha', hb'] by_cases hdn : d + 1 < n · rw [Nat.mod_eq_of_lt hdn] at hbd' exact π.nc (a + 1) (b + 1) (c + 1) (d + 1) (by omega) (by omega) (by omega) hac' hbd' · have hdeq : d + 1 = n := by omega rw [hdeq, Nat.mod_self] at hbd' have := π.nc 0 (a + 1) (b + 1) (c + 1) (by omega) (by omega) (by omega) (by rw [π.symm]; exact hbd') hac' exact π.trans _ _ _ (by rw [π.symm]; exact this) (by rw [π.symm]; exact hbd') lemma rotMinusOne_rel_iff {n : ℕ} (π : NC n) (i j : ℕ) : (rotMinusOne π).rel i j = true ↔ i < n ∧ j < n ∧ π.rel ((i + 1) % n) ((j + 1) % n) = true := decide_eq_true_iff /-- **Half of `K² = rot⁻¹`:** `rotMinusOne π ≤ K(K(π))` (direct witness proof). -/ theorem rotMinusOne_le_kreweras_sq {n : ℕ} (π : NC n) : rotMinusOne π ≤ kreweras (kreweras π) := by intro i j hij obtain ⟨hi, hj, hrel⟩ := (rotMinusOne_rel_iff π i j).mp hij rw [kreweras_rel_iff] refine ⟨hi, hj, (closedInterval_iff _ _ _).mpr ?_⟩ -- WLOG lo = min i j, hi' = max i j; if i = j then (lo, hi'] is empty intro a ha b hb hlo hhi hab by_contra hnot obtain ⟨_, _, hcl⟩ := (kreweras_rel_iff π a b).mp hab rw [closedInterval_iff] at hcl have hne : i ≠ j := by omega -- Swap so u < v with π.rel ((u+1)%n) ((v+1)%n) = true have huv : ∃ u v, u = min i j ∧ v = max i j ∧ u < v ∧ v < n ∧ π.rel ((u + 1) % n) ((v + 1) % n) = true := by by_cases hlt : i < j · exact ⟨i, j, (min_eq_left (le_of_lt hlt)).symm, (max_eq_right (le_of_lt hlt)).symm, hlt, hj, hrel⟩ · exact ⟨j, i, (min_eq_right (by omega)).symm, (max_eq_left (by omega)).symm, by omega, hi, by rw [π.symm]; exact hrel⟩ obtain ⟨u, v, hu_eq, hv_eq, huv_lt, hv_lt, hrel_uv⟩ := huv rw [← hu_eq] at hlo hnot rw [← hv_eq] at hhi hnot have hu1 : (u + 1) % n = u + 1 := Nat.mod_eq_of_lt (by omega) rw [hu1] at hrel_uv by_cases hbu : b ≤ u · -- b ≤ u < a ≤ v: min a b = b, max a b = a, and u + 1 ∈ (b, a] rw [min_eq_right (by omega : b ≤ a), max_eq_left (by omega : b ≤ a)] at hcl have hv_in := hcl (u + 1) (by omega) ((v + 1) % n) (Nat.mod_lt _ (by omega)) (by omega) (by omega) hrel_uv by_cases hvn : v + 1 < n · rw [Nat.mod_eq_of_lt hvn] at hv_in; omega · have : v + 1 = n := by omega rw [this, Nat.mod_self] at hv_in; omega · -- v < b (so v + 1 < n, and u < a ≤ v < b): min a b = a, max a b = b, and v + 1 ∈ (a, b] have hvb : v < b := by omega have hvn : v + 1 < n := by omega rw [Nat.mod_eq_of_lt hvn] at hrel_uv rw [min_eq_left (by omega : a ≤ b), max_eq_right (by omega : a ≤ b)] at hcl have hu_in := hcl (v + 1) hvn (u + 1) (by omega) (by omega) (by omega) (by rw [π.symm]; exact hrel_uv) omega /-- `i` is the largest element of its block. -/ def isMax {n : ℕ} (π : NC n) (i : ℕ) : Bool := decide (i < n ∧ ∀ k ∈ Finset.range n, i < k → π.rel i k = false) lemma isMax_iff {n : ℕ} (π : NC n) (i : ℕ) : π.isMax i = true ↔ i < n ∧ ∀ k, i < k → k < n → π.rel i k = false := by unfold isMax rw [decide_eq_true_iff] simp only [Finset.mem_range] exact and_congr_right fun _ => ⟨fun h k hik hkn => h k hkn hik, fun h k hkn hik => h k hik hkn⟩ /-- Count blocks via maximal elements. -/ def blockCountMax {n : ℕ} (π : NC n) : ℕ := ∑ i ∈ Finset.range n, if π.isMax i then 1 else 0 /-- Every block has a unique minimum and a unique maximum, so `blockCount π = blockCountMax π`. -/ lemma blockCount_eq_blockCountMax {n : ℕ} (π : NC n) : π.blockCount = π.blockCountMax := by -- Bijection between min-set and max-set on Finset.range n: -- map each i with isMin i to max {j < n | π.rel i j = true}, inverse maps j to min {i < n | π.rel i j = true}! let Smin := (Finset.range n).filter fun i => π.isMin i = true let Smax := (Finset.range n).filter fun i => π.isMax i = true have h1 : π.blockCount = Smin.card := by unfold blockCount Smin; rw [Finset.card_filter] have h2 : π.blockCountMax = Smax.card := by unfold blockCountMax Smax; rw [Finset.card_filter] rw [h1, h2] have hblk_ne : ∀ i < n, ((Finset.range n).filter fun j => π.rel i j = true).Nonempty := fun i hi => ⟨i, Finset.mem_filter.mpr ⟨Finset.mem_range.mpr hi, π.refl i hi⟩⟩ let toMax : ℕ → ℕ := fun i => if h : i < n then ((Finset.range n).filter fun j => π.rel i j = true).max' (hblk_ne i h) else 0 let toMin : ℕ → ℕ := fun j => if h : j < n then ((Finset.range n).filter fun i => π.rel i j = true).min' ⟨j, Finset.mem_filter.mpr ⟨Finset.mem_range.mpr h, π.refl j h⟩⟩ else 0 refine Finset.card_bij (fun i _ => toMax i) ?_ ?_ ?_ · intro i hi obtain ⟨hi_r, hi_m⟩ := Finset.mem_filter.mp hi have hin : i < n := Finset.mem_range.mp hi_r simp only [Smax, Finset.mem_filter, Finset.mem_range, toMax, hin, ↓reduceDIte] set B := (Finset.range n).filter fun j => π.rel i j = true set m := B.max' (hblk_ne i hin) have hm_mem : m ∈ B := Finset.max'_mem B (hblk_ne i hin) obtain ⟨hm_r, hm_rel⟩ := Finset.mem_filter.mp hm_mem have hmn : m < n := Finset.mem_range.mp hm_r refine ⟨hmn, (isMax_iff π m).mpr ⟨hmn, fun k hmk hkn => ?_⟩⟩ cases hr : π.rel m k · rfl · have hkB : k ∈ B := Finset.mem_filter.mpr ⟨Finset.mem_range.mpr hkn, π.trans _ _ _ hm_rel hr⟩ have hle : k ≤ m := Finset.le_max' B k hkB omega · intro i1 hi1 i2 hi2 heq obtain ⟨hi1_r, hi1_m⟩ := Finset.mem_filter.mp hi1 obtain ⟨hi2_r, hi2_m⟩ := Finset.mem_filter.mp hi2 have h1n : i1 < n := Finset.mem_range.mp hi1_r have h2n : i2 < n := Finset.mem_range.mp hi2_r simp only [toMax, h1n, h2n, ↓reduceDIte] at heq have hm1 : π.rel i1 (((Finset.range n).filter fun j => π.rel i1 j = true).max' (hblk_ne i1 h1n)) = true := (Finset.mem_filter.mp (Finset.max'_mem ((Finset.range n).filter fun j => π.rel i1 j = true) (hblk_ne i1 h1n))).2 have hm2 : π.rel i2 (((Finset.range n).filter fun j => π.rel i2 j = true).max' (hblk_ne i2 h2n)) = true := (Finset.mem_filter.mp (Finset.max'_mem ((Finset.range n).filter fun j => π.rel i2 j = true) (hblk_ne i2 h2n))).2 rw [heq] at hm1 have hrel12 : π.rel i1 i2 = true := π.trans _ _ _ hm1 (by rw [π.symm]; exact hm2) have hm1' := of_decide_eq_true hi1_m have hm2' := of_decide_eq_true hi2_m by_contra hne rcases lt_or_gt_of_ne hne with hlt | hgt · have := hm2' i1 hlt; rw [hrel12] at this; exact Bool.false_ne_true this.symm · have := hm1' i2 hgt; rw [π.symm, hrel12] at this; exact Bool.false_ne_true this.symm · intro j hj obtain ⟨hj_r, hj_m⟩ := Finset.mem_filter.mp hj have hjn : j < n := Finset.mem_range.mp hj_r obtain ⟨_, hj_max⟩ := (isMax_iff π j).mp hj_m set B := (Finset.range n).filter fun i => π.rel i j = true have hBne : B.Nonempty := ⟨j, Finset.mem_filter.mpr ⟨Finset.mem_range.mpr hjn, π.refl j hjn⟩⟩ set m := B.min' hBne have hm_mem : m ∈ B := Finset.min'_mem B hBne obtain ⟨hm_r, hm_rel⟩ := Finset.mem_filter.mp hm_mem have hmn : m < n := Finset.mem_range.mp hm_r refine ⟨m, ?_, ?_⟩ · refine Finset.mem_filter.mpr ⟨hm_r, decide_eq_true fun k hkm => ?_⟩ cases hr : π.rel k m · rfl · have hkB : k ∈ B := Finset.mem_filter.mpr ⟨Finset.mem_range.mpr (by omega), π.trans _ _ _ hr hm_rel⟩ have hle : m ≤ k := Finset.min'_le B k hkB omega · simp only [toMax, hmn, ↓reduceDIte] set Bm := (Finset.range n).filter fun k => π.rel m k = true have hjBm : j ∈ Bm := Finset.mem_filter.mpr ⟨hj_r, hm_rel⟩ apply le_antisymm · apply Finset.max'_le intro k hk obtain ⟨hk_r, hk_rel⟩ := Finset.mem_filter.mp hk have hkn : k < n := Finset.mem_range.mp hk_r have hjk : π.rel j k = true := π.trans _ _ _ (by rw [π.symm]; exact hm_rel) hk_rel by_contra hgt have := hj_max k (by omega) hkn rw [hjk] at this; exact Bool.false_ne_true this.symm · exact Finset.le_max' Bm j hjBm /-- Key structural identity: `|K(prepend σ)| = |K(σ)|` for `m ≥ 1`. -/ lemma blockCount_kreweras_prepend {m : ℕ} (hm : 0 < m) (σ : NC m) : (kreweras (prepend σ)).blockCount = (kreweras σ).blockCount := by rw [blockCount_eq_blockCountMax, blockCount_eq_blockCountMax] unfold blockCountMax rw [Finset.sum_range_succ'] -- 0 is NOT max in K(prepend σ), because 0 ~ m (since (0, m] = {1..m} is closed in prepend σ)! have h0_not_max : (kreweras (prepend σ)).isMax 0 = false := by rw [Bool.eq_false_iff] intro hmax obtain ⟨_, h⟩ := (isMax_iff _ _).mp hmax have h0m := h m hm (by omega) have htrue : (kreweras (prepend σ)).rel 0 m = true := by rw [kreweras_rel_iff] refine ⟨by omega, by omega, (closedInterval_iff _ _ _).mpr ?_⟩ rw [min_eq_left (Nat.zero_le m), max_eq_right (Nat.zero_le m)] intro a ha b hb hlo hhi hab rw [prepend_rel] at hab rcases hab with ⟨ha0, _⟩ | ⟨_, hb1, _⟩ <;> omega rw [htrue] at h0m; exact absurd h0m (by decide) rw [h0_not_max, if_neg Bool.false_ne_true, add_zero] refine Finset.sum_congr rfl fun i hi => ?_ have him : i < m := Finset.mem_range.mp hi have hrel_eq : ∀ k, i < k → k < m → (kreweras (prepend σ)).rel (i + 1) (k + 1) = (kreweras σ).rel i k := by intro k hik hkm rw [Bool.eq_iff_iff, kreweras_rel_iff, kreweras_rel_iff, closedInterval_iff, closedInterval_iff] rw [min_eq_left (by omega : i + 1 ≤ k + 1), max_eq_right (by omega : i + 1 ≤ k + 1), min_eq_left (by omega : i ≤ k), max_eq_right (by omega : i ≤ k)] constructor · rintro ⟨_, _, hc⟩ refine ⟨him, hkm, fun a ha b hb hlo hhi hab => ?_⟩ have := hc (a + 1) (by omega) (b + 1) (by omega) (by omega) (by omega) (by rw [prepend_rel_succ]; exact hab) omega · rintro ⟨_, _, hc⟩ refine ⟨by omega, by omega, fun a ha b hb hlo hhi hab => ?_⟩ rw [prepend_rel] at hab rcases hab with ⟨ha0, _⟩ | ⟨ha1, hb1, hab'⟩ · omega · have := hc (a - 1) (by omega) (b - 1) (by omega) (by omega) (by omega) hab' omega have heq_b : (kreweras (prepend σ)).isMax (i + 1) = (kreweras σ).isMax i := by rw [Bool.eq_iff_iff, isMax_iff, isMax_iff] constructor · rintro ⟨_, h⟩ refine ⟨him, fun k hik hkm => ?_⟩ rw [← hrel_eq k hik hkm] exact h (k + 1) (by omega) (by omega) · rintro ⟨_, h⟩ refine ⟨by omega, fun k hik hkm => ?_⟩ obtain ⟨k', rfl⟩ : ∃ k', k = k' + 1 := ⟨k - 1, by omega⟩ rw [hrel_eq k' (by omega) (by omega)] exact h k' (by omega) (by omega) rw [heq_b] /-- Key structural identity: `|K(joinAt j a b)| = (if j = 0 then 1 else |K(a)|) + |K(b)|`. -/ lemma blockCount_kreweras_joinAt {m j : ℕ} (hj : j < m) (a : NC j) (b : NC (m - j)) : (kreweras (joinAt j hj a b)).blockCount = (if j = 0 then 1 else (kreweras a).blockCount) + (kreweras b).blockCount := by -- No cross-edges in K(joinAt j a b) between {0..j} and {j+1..m} because 0 ~ j+1! have hno_cross : ∀ u v, u ≤ j → j + 1 ≤ v → v ≤ m → (kreweras (joinAt j hj a b)).rel u v = false := by intro u v hu hv1 hv2 rw [Bool.eq_false_iff]; intro h obtain ⟨_, _, hc⟩ := (kreweras_rel_iff _ u v).mp h rw [closedInterval_iff, min_eq_left (by omega : u ≤ v), max_eq_right (by omega : u ≤ v)] at hc have h0 := hc (j + 1) (by omega) 0 (by omega) (by omega) (by omega) (by rw [(joinAt j hj a b).symm]; exact joinAt_rel_zero_link (hj := hj)) omega -- On {0..j}, K(joinAt j a b) agrees with K(prepend a)! have hleft_eq : ∀ u v, u ≤ j → v ≤ j → (kreweras (joinAt j hj a b)).rel u v = (kreweras (prepend a)).rel u v := by intro u v hu hv rw [Bool.eq_iff_iff, kreweras_rel_iff, kreweras_rel_iff, closedInterval_iff, closedInterval_iff] constructor · rintro ⟨_, _, hc⟩ refine ⟨by omega, by omega, fun x hx y hy hlo hhi hxy => ?_⟩ rw [prepend_rel] at hxy rcases hxy with ⟨hx0, _⟩ | ⟨hx1, hy1, hxy'⟩ · omega · exact hc x (by omega) y (by omega) hlo hhi ((joinAt_rel x y).mpr (Or.inl ⟨hx1, by omega, hy1, by omega, hxy'⟩)) · rintro ⟨_, _, hc⟩ refine ⟨by omega, by omega, fun x hx y hy hlo hhi hxy => ?_⟩ rw [joinAt_rel] at hxy rcases hxy with ⟨hx1, hxj, hy1, hyj, hxy'⟩ | ⟨hx0, _, _⟩ · exact hc x (by omega) y (by omega) hlo hhi ((prepend_rel _ x y).mpr (Or.inr ⟨hx1, hy1, hxy'⟩)) · omega -- On {j+1..m}, K(joinAt j a b) agrees with K(b) shifted by j+1! have hright_eq : ∀ x y, x < m - j → y < m - j → (kreweras (joinAt j hj a b)).rel (j + 1 + x) (j + 1 + y) = (kreweras b).rel x y := by intro x y hx hy rw [Bool.eq_iff_iff, kreweras_rel_iff, kreweras_rel_iff, closedInterval_iff, closedInterval_iff] have hmin : min (j + 1 + x) (j + 1 + y) = j + 1 + min x y := by omega have hmax : max (j + 1 + x) (j + 1 + y) = j + 1 + max x y := by omega rw [hmin, hmax] constructor · rintro ⟨_, _, hc⟩ refine ⟨hx, hy, fun p hp q hq hlo hhi hpq => ?_⟩ have := hc (j + 1 + p) (by omega) (j + 1 + q) (by omega) (by omega) (by omega) (by rw [joinAt_rel_out _ _ (Or.inr ⟨by omega, by omega⟩) (Or.inr ⟨by omega, by omega⟩), oIdx_pos (by omega), oIdx_pos (by omega), show j + 1 + p - (j + 1) = p by omega, show j + 1 + q - (j + 1) = q by omega] exact hpq) omega · rintro ⟨_, _, hc⟩ refine ⟨by omega, by omega, fun p hp q hq hlo hhi hpq => ?_⟩ rw [joinAt_rel] at hpq rcases hpq with ⟨_, _, _, _, _⟩ | ⟨_, hq_zone, hbrel⟩ · omega · rw [oIdx_pos (by omega)] at hbrel have hq_in := hc (p - (j + 1)) (by omega) (oIdx j q) (by rcases hq_zone with rfl | ⟨_, _⟩ · rw [oIdx_zero]; omega · rw [oIdx_pos (by omega)]; omega) (by omega) (by omega) hbrel rcases hq_zone with rfl | ⟨hq1, _⟩ · rw [oIdx_zero] at hq_in; omega · rw [oIdx_pos (by omega)] at hq_in; omega -- Now split blockCountMax of K(joinAt j a b) into [0..j] and [j+1..m]! rw [blockCount_eq_blockCountMax] unfold blockCountMax rw [← Finset.sum_range_add_sum_Ico _ (show j + 1 ≤ m + 1 by omega), Finset.sum_Ico_eq_sum_range, show m + 1 - (j + 1) = m - j by omega] have eLeft : ∑ i ∈ Finset.range (j + 1), (if (kreweras (joinAt j hj a b)).isMax i then 1 else 0) = (kreweras (prepend a)).blockCount := by rw [blockCount_eq_blockCountMax] unfold blockCountMax refine Finset.sum_congr rfl fun i hi => ?_ have hij : i ≤ j := by have := Finset.mem_range.mp hi; omega have heq_b : (kreweras (joinAt j hj a b)).isMax i = (kreweras (prepend a)).isMax i := by rw [Bool.eq_iff_iff, isMax_iff, isMax_iff] constructor · rintro ⟨_, h⟩ refine ⟨by omega, fun k hik hkj => ?_⟩ rw [← hleft_eq i k hij (by omega)] exact h k hik (by omega) · rintro ⟨_, h⟩ refine ⟨by omega, fun k hik hkm => ?_⟩ by_cases hkj : k ≤ j · rw [hleft_eq i k hij hkj]; exact h k hik (by omega) · exact hno_cross i k hij (by omega) (by omega) rw [heq_b] have eRight : ∑ k ∈ Finset.range (m - j), (if (kreweras (joinAt j hj a b)).isMax (j + 1 + k) then 1 else 0) = (kreweras b).blockCount := by rw [blockCount_eq_blockCountMax] unfold blockCountMax refine Finset.sum_congr rfl fun k hk => ?_ have hkm : k < m - j := Finset.mem_range.mp hk have heq_b : (kreweras (joinAt j hj a b)).isMax (j + 1 + k) = (kreweras b).isMax k := by rw [Bool.eq_iff_iff, isMax_iff, isMax_iff] constructor · rintro ⟨_, h⟩ refine ⟨hkm, fun l hkl hlm => ?_⟩ rw [← hright_eq k l hkm hlm] exact h (j + 1 + l) (by omega) (by omega) · rintro ⟨_, h⟩ refine ⟨by omega, fun l hkl hlm => ?_⟩ have hl : l = j + 1 + (l - (j + 1)) := by omega rw [hl, hright_eq k (l - (j + 1)) hkm (by omega)] exact h (l - (j + 1)) (by omega) (by omega) rw [heq_b] rw [eLeft, eRight] congr 1 by_cases hj0 : j = 0 · subst hj0 simp only [↓reduceIte] have h1 : (kreweras (prepend a)).blockCount = 1 := by unfold blockCount rw [Finset.sum_range_one, isMin_zero]; rfl exact h1 · simp only [hj0, ↓reduceIte] exact blockCount_kreweras_prepend (Nat.pos_of_ne_zero hj0) a /-- **Exact Kreweras Rank Identity:** `|π| + |K(π)| = n + 1` for all `n ≥ 1` and `π ∈ NC(n)`. -/ theorem blockCount_add_kreweras {n : ℕ} (hn : 0 < n) (π : NC n) : π.blockCount + (kreweras π).blockCount = n + 1 := by induction n using Nat.strong_induction_on with | _ n ih => rcases n with _ | m · omega · rcases m with _ | m' · -- n = 1 have h1 : π.blockCount = 1 := by unfold blockCount; rw [Finset.sum_range_one, isMin_zero]; rfl have h2 : (kreweras π).blockCount = 1 := by unfold blockCount; rw [Finset.sum_range_one, isMin_zero]; rfl omega · cases hfl : firstLink π with | none => have heq := prepend_window π ((firstLink_eq_none _).mp hfl) rw [← heq, blockCount_prepend, blockCount_kreweras_prepend (by omega)] have := ih (m' + 1) (by omega) (by omega) (window π 1 (m' + 1) (by omega)) omega | some j => have hspec := (firstLink_eq_some _ _).mp hfl have heq := joinAt_window j.1 j.2 π hspec.1 hspec.2 rw [← heq, blockCount_joinAt, blockCount_kreweras_joinAt] have hB := ih (m' + 1 - j.1) (by omega) (by omega) (window π (j.1 + 1) (m' + 1 - j.1) (by omega)) by_cases hj0 : j.1 = 0 · have hA0 : (window π 1 j.1 (by omega)).blockCount = 0 := by unfold blockCount; simp [hj0] rw [if_pos hj0, hA0] omega · simp only [hj0, ↓reduceIte] have hA := ih j.1 (by omega) (Nat.pos_of_ne_zero hj0) (window π 1 j.1 (by omega)) have hjlt : j.1 < m' + 1 := j.2 omega /-- In `NC(n)`, if `α ≤ β` and `|α| = |β|`, then `α = β`. -/ lemma eq_of_le_of_blockCount_eq {n : ℕ} {α β : NC n} (hle : α ≤ β) (hcard : α.blockCount = β.blockCount) : α = β := by by_contra hne -- Find x < y < n with α.rel x y = false and β.rel x y = true have hex : ∃ x y, x < y ∧ y < n ∧ α.rel x y = false ∧ β.rel x y = true := by by_contra hall push Not at hall apply hne apply ext' intro i j hi hj cases ha : α.rel i j · cases hb : β.rel i j · rfl · exfalso by_cases hij : i = j · subst hij; rw [α.refl i hi] at ha; exact absurd ha (by decide) · rcases lt_or_gt_of_ne hij with hlt | hgt · exact absurd hb (hall i j hlt hj ha) · rw [α.symm] at ha; rw [β.symm] at hb exact absurd hb (hall j i hgt hi ha) · exact (hle i j ha).symm obtain ⟨x, y, hxy, hyn, hαxy, hβxy⟩ := hex -- Let m = min of α-block of y; then α.isMin m = true, while β.isMin m = false (Wait: what if m ≤ x?) -- Better: let mx = min of α-block of x, my = min of α-block of y. -- Since α.rel x y = false, mx ≠ my, yet β.rel mx my = true! So max(mx, my) has α.isMin = true and β.isMin = false! have hmin_blk : ∀ u < n, ∃ m < n, α.isMin m = true ∧ α.rel m u = true := by intro u hu set B := (Finset.range n).filter fun k => α.rel k u = true have hneB : B.Nonempty := ⟨u, Finset.mem_filter.mpr ⟨Finset.mem_range.mpr hu, α.refl u hu⟩⟩ set m := B.min' hneB obtain ⟨hmr, hmu⟩ := Finset.mem_filter.mp (Finset.min'_mem B hneB) refine ⟨m, Finset.mem_range.mp hmr, decide_eq_true fun k hkm => ?_, hmu⟩ cases hk : α.rel k m · rfl · have hkB : k ∈ B := Finset.mem_filter.mpr ⟨Finset.mem_range.mpr (by have := Finset.mem_range.mp hmr; omega), α.trans _ _ _ hk hmu⟩ have := Finset.min'_le B k hkB; omega obtain ⟨mx, hmxn, hmx_min, hmx_rel⟩ := hmin_blk x (by omega) obtain ⟨my, hmyn, hmy_min, hmy_rel⟩ := hmin_blk y hyn have hmne : mx ≠ my := by intro heq; subst heq have := α.trans _ _ _ (by rw [α.symm]; exact hmx_rel) hmy_rel rw [hαxy] at this; exact absurd this (by decide) have hβ_m : β.rel mx my = true := β.trans _ _ _ (hle _ _ hmx_rel) (β.trans _ _ _ hβxy (hle _ _ (by rw [α.symm]; exact hmy_rel))) -- Pick w = max mx my: α.isMin w = true, β.isMin w = false! have hw : ∃ w < n, α.isMin w = true ∧ β.isMin w = false := by rcases lt_or_gt_of_ne hmne with hlt | hgt · refine ⟨my, hmyn, hmy_min, ?_⟩ unfold isMin; rw [decide_eq_false_iff_not]; intro h have := h mx hlt; rw [hβ_m] at this; exact absurd this (by decide) · refine ⟨mx, hmxn, hmx_min, ?_⟩ unfold isMin; rw [decide_eq_false_iff_not]; intro h have := h my hgt; rw [β.symm, hβ_m] at this; exact absurd this (by decide) obtain ⟨w, hwn, hwα, hwβ⟩ := hw -- Every β.isMin is an α.isMin (since α ≤ β), so β.blockCount < α.blockCount! have hsub_le : ∀ i ∈ Finset.range n, (if β.isMin i = true then 1 else 0) ≤ (if α.isMin i = true then 1 else 0) := by intro i _ by_cases hbi : β.isMin i = true · have hai : α.isMin i = true := decide_eq_true fun k hki => by cases hak : α.rel k i · rfl · have := of_decide_eq_true hbi k hki rw [hle k i hak] at this; exact absurd this (by decide) simp [hbi, hai] · simp [hbi] have hlt : β.blockCount < α.blockCount := Finset.sum_lt_sum hsub_le ⟨w, Finset.mem_range.mpr hwn, by simp [hwα, hwβ]⟩ omega /-- `rotMinusOne` preserves `blockCount`. -/ lemma blockCount_rotMinusOne {n : ℕ} (π : NC n) : (rotMinusOne π).blockCount = π.blockCount := by rcases n with _ | m · simp [blockCount] · -- rotMinusOne is a bijection on blocks! Explicitly: if m = 0 then both = 1; -- in general, the unique element of each block of π with minimum image under `x ↦ (x + m) % (m + 1)` -- maps bijectively to the unique `isMin` of that block in π! let S1 := (Finset.range (m + 1)).filter fun i => (rotMinusOne π).isMin i = true let S2 := (Finset.range (m + 1)).filter fun i => π.isMin i = true have h1 : (rotMinusOne π).blockCount = S1.card := by unfold blockCount S1; rw [Finset.card_filter] have h2 : π.blockCount = S2.card := by unfold blockCount S2; rw [Finset.card_filter] rw [h1, h2] let f : ℕ → ℕ := fun i => if h : i < m + 1 then ((Finset.range (m + 1)).filter fun k => π.rel k ((i + 1) % (m + 1)) = true).min' ⟨(i + 1) % (m + 1), Finset.mem_filter.mpr ⟨Finset.mem_range.mpr (Nat.mod_lt _ (Nat.succ_pos m)), π.refl _ (Nat.mod_lt _ (Nat.succ_pos m))⟩⟩ else 0 refine Finset.card_bij (fun i _ => f i) ?_ ?_ ?_ · intro i hi obtain ⟨hir, _⟩ := Finset.mem_filter.mp hi have hin : i < m + 1 := Finset.mem_range.mp hir simp only [S2, Finset.mem_filter, Finset.mem_range, f, hin, ↓reduceDIte] set B := (Finset.range (m + 1)).filter fun k => π.rel k ((i + 1) % (m + 1)) = true have hneB : B.Nonempty := ⟨(i + 1) % (m + 1), Finset.mem_filter.mpr ⟨Finset.mem_range.mpr (Nat.mod_lt _ (Nat.succ_pos m)), π.refl _ (Nat.mod_lt _ (Nat.succ_pos m))⟩⟩ set r := B.min' hneB obtain ⟨hrr, hr_rel⟩ := Finset.mem_filter.mp (Finset.min'_mem B hneB) refine ⟨Finset.mem_range.mp hrr, decide_eq_true fun k hkr => ?_⟩ cases hkr' : π.rel k r · rfl · have hkB : k ∈ B := Finset.mem_filter.mpr ⟨Finset.mem_range.mpr (by have := Finset.mem_range.mp hrr; omega), π.trans _ _ _ hkr' hr_rel⟩ have := Finset.min'_le B k hkB; omega · intro i1 hi1 i2 hi2 heq obtain ⟨hi1r, hi1m⟩ := Finset.mem_filter.mp hi1 obtain ⟨hi2r, hi2m⟩ := Finset.mem_filter.mp hi2 have h1n : i1 < m + 1 := Finset.mem_range.mp hi1r have h2n : i2 < m + 1 := Finset.mem_range.mp hi2r simp only [f, h1n, h2n, ↓reduceDIte] at heq have hne1 : ((Finset.range (m + 1)).filter fun k => π.rel k ((i1 + 1) % (m + 1)) = true).Nonempty := ⟨(i1 + 1) % (m + 1), Finset.mem_filter.mpr ⟨Finset.mem_range.mpr (Nat.mod_lt _ (Nat.succ_pos m)), π.refl _ (Nat.mod_lt _ (Nat.succ_pos m))⟩⟩ have hne2 : ((Finset.range (m + 1)).filter fun k => π.rel k ((i2 + 1) % (m + 1)) = true).Nonempty := ⟨(i2 + 1) % (m + 1), Finset.mem_filter.mpr ⟨Finset.mem_range.mpr (Nat.mod_lt _ (Nat.succ_pos m)), π.refl _ (Nat.mod_lt _ (Nat.succ_pos m))⟩⟩ have hr1 := (Finset.mem_filter.mp (Finset.min'_mem ((Finset.range (m + 1)).filter fun k => π.rel k ((i1 + 1) % (m + 1)) = true) hne1)).2 have hr2 := (Finset.mem_filter.mp (Finset.min'_mem ((Finset.range (m + 1)).filter fun k => π.rel k ((i2 + 1) % (m + 1)) = true) hne2)).2 rw [heq] at hr1 have hrel : (rotMinusOne π).rel i1 i2 = true := (rotMinusOne_rel_iff π i1 i2).mpr ⟨h1n, h2n, π.trans _ _ _ (by rw [π.symm]; exact hr1) hr2⟩ by_contra hne rcases lt_or_gt_of_ne hne with hlt | hgt · have := of_decide_eq_true hi2m i1 hlt rw [hrel] at this; exact absurd this (by decide) · have := of_decide_eq_true hi1m i2 hgt rw [(rotMinusOne π).symm, hrel] at this; exact absurd this (by decide) · intro r hr obtain ⟨hrr, hrm⟩ := Finset.mem_filter.mp hr have hrn : r < m + 1 := Finset.mem_range.mp hrr set Br := (Finset.range (m + 1)).filter fun i => π.rel r ((i + 1) % (m + 1)) = true have hneBr : Br.Nonempty := by refine ⟨(r + m) % (m + 1), Finset.mem_filter.mpr ⟨Finset.mem_range.mpr (Nat.mod_lt _ (Nat.succ_pos m)), ?_⟩⟩ have hmod : ((r + m) % (m + 1) + 1) % (m + 1) = r := by rw [Nat.mod_add_mod, show r + m + 1 = r + (m + 1) by omega, Nat.add_mod_right, Nat.mod_eq_of_lt hrn] rw [hmod]; exact π.refl r hrn set i0 := Br.min' hneBr obtain ⟨hi0r, hi0_rel⟩ := Finset.mem_filter.mp (Finset.min'_mem Br hneBr) have hi0n : i0 < m + 1 := Finset.mem_range.mp hi0r refine ⟨i0, ?_, ?_⟩ · refine Finset.mem_filter.mpr ⟨hi0r, decide_eq_true fun k hki => ?_⟩ cases hk : (rotMinusOne π).rel k i0 · rfl · obtain ⟨_, _, hk'⟩ := (rotMinusOne_rel_iff π k i0).mp hk have hkBr : k ∈ Br := Finset.mem_filter.mpr ⟨Finset.mem_range.mpr (by omega), π.trans _ _ _ hi0_rel (by rw [π.symm]; exact hk')⟩ have := Finset.min'_le Br k hkBr; omega · simp only [f, hi0n, ↓reduceDIte] set Bi := (Finset.range (m + 1)).filter fun k => π.rel k ((i0 + 1) % (m + 1)) = true have hrBi : r ∈ Bi := Finset.mem_filter.mpr ⟨hrr, hi0_rel⟩ apply le_antisymm (Finset.min'_le Bi r hrBi) apply Finset.le_min' Bi intro k hk obtain ⟨_, hk_rel⟩ := Finset.mem_filter.mp hk have hkr : π.rel k r = true := π.trans _ _ _ hk_rel (by rw [π.symm]; exact hi0_rel) by_contra hlt have := of_decide_eq_true hrm k (by omega) rw [hkr] at this; exact absurd this (by decide) /-- **Exact Kreweras Involution / Rotation Identity:** `K(K(π)) = rotMinusOne π` (cyclic rotation by `-1 mod n`) for all `n` and `π ∈ NC(n)`. -/ theorem kreweras_sq_eq_rot {n : ℕ} (π : NC n) : kreweras (kreweras π) = rotMinusOne π := by rcases n with _ | m · exact ext' fun _ _ hi => absurd hi (Nat.not_lt_zero _) · symm apply eq_of_le_of_blockCount_eq (rotMinusOne_le_kreweras_sq π) have h1 := blockCount_add_kreweras (Nat.succ_pos m) π have h2 := blockCount_add_kreweras (Nat.succ_pos m) (kreweras π) have h3 := blockCount_rotMinusOne π omega /-- Inverse cyclic rotation `rotPlusOne` by `+1 mod n`. -/ def rotPlusOne {n : ℕ} (π : NC n) : NC n where rel i j := decide (i < n ∧ j < n ∧ π.rel ((i + n - 1) % n) ((j + n - 1) % n) = true) supp i j h := by have := of_decide_eq_true h; exact ⟨this.1, this.2.1⟩ refl i hi := decide_eq_true ⟨hi, hi, π.refl _ (Nat.mod_lt _ (by omega))⟩ symm i j := decide_eq_decide.mpr (by rw [π.symm]; tauto) trans i j k h1 h2 := by obtain ⟨hi, _, h1'⟩ := of_decide_eq_true h1 obtain ⟨_, hk, h2'⟩ := of_decide_eq_true h2 exact decide_eq_true ⟨hi, hk, π.trans _ _ _ h1' h2'⟩ nc a b c d hab hbc hcd hac hbd := by obtain ⟨ha, hc, hac'⟩ := of_decide_eq_true hac obtain ⟨hb, hd, hbd'⟩ := of_decide_eq_true hbd have hmod_pos : ∀ x, 0 < x → x < n → (x + n - 1) % n = x - 1 := by intro x hx1 hx2 rw [show x + n - 1 = (x - 1) + n by omega, Nat.add_mod_right, Nat.mod_eq_of_lt (by omega)] have hb' := hmod_pos b (by omega) hb have hc' := hmod_pos c (by omega) hc have hd' := hmod_pos d (by omega) hd rw [hb', hd'] at hbd' rw [hc'] at hac' apply decide_eq_true refine ⟨ha, hb, ?_⟩ rw [hb'] by_cases ha0 : a = 0 · subst ha0 have h0 : (0 + n - 1) % n = n - 1 := by rw [Nat.zero_add]; exact Nat.mod_eq_of_lt (by omega) rw [h0] at hac' ⊢ have := π.nc (b - 1) (c - 1) (d - 1) (n - 1) (by omega) (by omega) (by omega) hbd' (by rw [π.symm]; exact hac') exact π.trans _ _ _ hac' (by rw [π.symm]; exact this) · have ha' := hmod_pos a (by omega) ha rw [ha'] at hac' ⊢ exact π.nc (a - 1) (b - 1) (c - 1) (d - 1) (by omega) (by omega) (by omega) hac' hbd' lemma rotPlusOne_rotMinusOne {n : ℕ} (π : NC n) : rotPlusOne (rotMinusOne π) = π := by apply ext' intro i j hi hj rw [Bool.eq_iff_iff] simp only [rotPlusOne, rotMinusOne, decide_eq_true_eq] have hmod : ∀ x < n, ((x + n - 1) % n + 1) % n = x := by intro x hx have h1 : (x + n - 1) + 1 = x + n := by omega rw [Nat.mod_add_mod, h1, Nat.add_mod_right, Nat.mod_eq_of_lt hx] have hlt : ∀ x < n, (x + n - 1) % n < n := fun x hx => Nat.mod_lt _ (by omega) simp [hi, hj, hlt, hmod] lemma rotMinusOne_rotPlusOne {n : ℕ} (π : NC n) : rotMinusOne (rotPlusOne π) = π := by apply ext' intro i j hi hj rw [Bool.eq_iff_iff] simp only [rotPlusOne, rotMinusOne, decide_eq_true_eq] have hmod : ∀ x < n, ((x + 1) % n + n - 1) % n = x := by intro x hx by_cases hx1 : x + 1 < n · rw [Nat.mod_eq_of_lt hx1, show x + 1 + n - 1 = x + n by omega, Nat.add_mod_right, Nat.mod_eq_of_lt hx] · have : x + 1 = n := by omega rw [this, Nat.mod_self, show 0 + n - 1 = x by omega, Nat.mod_eq_of_lt hx] have hlt : ∀ x < n, (x + 1) % n < n := fun x hx => Nat.mod_lt _ (by omega) simp [hi, hj, hlt, hmod] lemma rotPlusOne_monotone {n : ℕ} {π σ : NC n} (h : π ≤ σ) : rotPlusOne π ≤ rotPlusOne σ := by intro i j hij simp only [rotPlusOne, decide_eq_true_eq] at hij ⊢ exact ⟨hij.1, hij.2.1, h _ _ hij.2.2⟩ lemma rotMinusOne_monotone {n : ℕ} {π σ : NC n} (h : π ≤ σ) : rotMinusOne π ≤ rotMinusOne σ := by intro i j hij simp only [rotMinusOne, decide_eq_true_eq] at hij ⊢ exact ⟨hij.1, hij.2.1, h _ _ hij.2.2⟩ /-- **Kreweras Complement is an Exact Order-Reversing Lattice Bijection:** `NC n ≃o (NC n)ᵒᵈ`. -/ def krewerasOrderIso (n : ℕ) : NC n ≃o (NC n)ᵒᵈ where toFun π := OrderDual.toDual (kreweras π) invFun σ := rotPlusOne (kreweras (OrderDual.ofDual σ)) left_inv π := by show rotPlusOne (kreweras (kreweras π)) = π rw [kreweras_sq_eq_rot, rotPlusOne_rotMinusOne] right_inv σ := by change OrderDual.toDual (kreweras (rotPlusOne (kreweras (OrderDual.ofDual σ)))) = σ have hcomm : ∀ τ : NC n, kreweras (rotPlusOne τ) = rotPlusOne (kreweras τ) := by intro τ have h1 : rotMinusOne (kreweras (rotPlusOne τ)) = kreweras τ := by calc rotMinusOne (kreweras (rotPlusOne τ)) = kreweras (kreweras (kreweras (rotPlusOne τ))) := (kreweras_sq_eq_rot _).symm _ = kreweras (rotMinusOne (rotPlusOne τ)) := by rw [kreweras_sq_eq_rot (rotPlusOne τ)] _ = kreweras τ := by rw [rotMinusOne_rotPlusOne] calc kreweras (rotPlusOne τ) = rotPlusOne (rotMinusOne (kreweras (rotPlusOne τ))) := (rotPlusOne_rotMinusOne _).symm _ = rotPlusOne (kreweras τ) := by rw [h1] rw [hcomm, kreweras_sq_eq_rot, rotPlusOne_rotMinusOne] rfl map_rel_iff' := by intro π σ change kreweras σ ≤ kreweras π ↔ π ≤ σ constructor · intro h have h2 := kreweras_antitone h rw [kreweras_sq_eq_rot, kreweras_sq_eq_rot] at h2 have h3 := rotPlusOne_monotone h2 rwa [rotPlusOne_rotMinusOne, rotPlusOne_rotMinusOne] at h3 · exact kreweras_antitone theorem kreweras_bijective (n : ℕ) : Function.Bijective (kreweras (n := n)) := (krewerasOrderIso n).bijective /-! ### Exact `NC₂(2m)` Non-Crossing Pairing Enumeration -/ /-- Block size of element `i` in `π`. -/ def blockSize {n : ℕ} (π : NC n) (i : ℕ) : ℕ := ((Finset.range n).filter fun j => π.rel i j = true).card /-- A non-crossing pairing: every element belongs to a block of size exactly `2`. -/ def isPairing {n : ℕ} (π : NC n) : Prop := ∀ i < n, π.blockSize i = 2 instance {n : ℕ} (π : NC n) : Decidable (isPairing π) := inferInstanceAs (Decidable (∀ i < n, π.blockSize i = 2)) /-- The subtype of non-crossing pairings on `2m` elements. -/ abbrev NC2 (m : ℕ) := { π : NC (2 * m) // isPairing π } /-- No non-crossing pairing exists on an odd number of elements `2m + 1`. -/ theorem NC2_odd_empty (m : ℕ) : IsEmpty { π : NC (2 * m + 1) // isPairing π } := by refine ⟨fun ⟨π, hpair⟩ => ?_⟩ -- Double count pairs (i, j) with π.rel i j = true and i ≠ j: -- Each i has exactly 1 partner j ≠ i (since blockSize i = 2 and π.rel i i = true), -- so the symmetric irreflexive relation has odd total degree (2m+1), impossible! let E := ((Finset.range (2 * m + 1)) ×ˢ (Finset.range (2 * m + 1))).filter fun p => p.1 ≠ p.2 ∧ π.rel p.1 p.2 = true have hE_odd : E.card = 2 * m + 1 := by have h1 : E.card = ∑ i ∈ Finset.range (2 * m + 1), ((Finset.range (2 * m + 1)).filter fun j => i ≠ j ∧ π.rel i j = true).card := by unfold E rw [Finset.card_filter, Finset.sum_product] refine Finset.sum_congr rfl fun i _ => ?_ rw [Finset.card_filter] rw [h1] have h2 : ∀ i ∈ Finset.range (2 * m + 1), ((Finset.range (2 * m + 1)).filter fun j => i ≠ j ∧ π.rel i j = true).card = 1 := by intro i hi have hin : i < 2 * m + 1 := Finset.mem_range.mp hi have hsz := hpair i hin unfold blockSize at hsz have hEq : ((Finset.range (2 * m + 1)).filter fun j => i ≠ j ∧ π.rel i j = true) = ((Finset.range (2 * m + 1)).filter fun j => π.rel i j = true).erase i := by ext j; simp only [Finset.mem_filter, Finset.mem_erase, ne_eq] constructor · rintro ⟨hj, hne, hr⟩; exact ⟨Ne.symm hne, hj, hr⟩ · rintro ⟨hne, hj, hr⟩; exact ⟨hj, Ne.symm hne, hr⟩ rw [hEq, Finset.card_erase_of_mem (Finset.mem_filter.mpr ⟨hi, π.refl i hin⟩), hsz] rw [Finset.sum_congr rfl h2]; simp have hE_even : Even E.card := by let Elt := E.filter fun p => p.1 < p.2 let Egt := E.filter fun p => p.2 < p.1 have hdisj : Disjoint Elt Egt := by rw [Finset.disjoint_left]; intro p h1 h2 have := (Finset.mem_filter.mp h1).2; have := (Finset.mem_filter.mp h2).2; omega have hunion : E = Elt ∪ Egt := by ext p; simp only [Elt, Egt, Finset.mem_union, Finset.mem_filter] constructor · intro hp have hne : p.1 ≠ p.2 := (Finset.mem_filter.mp hp).2.1 rcases lt_or_gt_of_ne hne with h | h · exact Or.inl ⟨hp, h⟩ · exact Or.inr ⟨hp, h⟩ · rintro (⟨hp, _⟩ | ⟨hp, _⟩) <;> exact hp have hbij : Elt.card = Egt.card := by refine Finset.card_bij (fun p _ => (p.2, p.1)) ?_ ?_ ?_ · intro p hp obtain ⟨hpE, hlt⟩ := Finset.mem_filter.mp hp obtain ⟨hpP, hne, hrel⟩ := Finset.mem_filter.mp hpE obtain ⟨hp1, hp2⟩ := Finset.mem_product.mp hpP refine Finset.mem_filter.mpr ⟨Finset.mem_filter.mpr ⟨Finset.mem_product.mpr ⟨hp2, hp1⟩, hne.symm, by rw [π.symm]; exact hrel⟩, hlt⟩ · intro p1 _ p2 _ h; exact Prod.ext (congrArg Prod.snd h) (congrArg Prod.fst h) · intro q hq obtain ⟨hqE, hgt⟩ := Finset.mem_filter.mp hq obtain ⟨hqP, hne, hrel⟩ := Finset.mem_filter.mp hqE obtain ⟨hq1, hq2⟩ := Finset.mem_product.mp hqP exact ⟨(q.2, q.1), Finset.mem_filter.mpr ⟨Finset.mem_filter.mpr ⟨Finset.mem_product.mpr ⟨hq2, hq1⟩, hne.symm, by rw [π.symm]; exact hrel⟩, hgt⟩, rfl⟩ rw [hunion, Finset.card_union_of_disjoint hdisj, hbij] exact ⟨Egt.card, rfl⟩ rcases hE_even with ⟨k, hk⟩ omega /-- Helper: `isPairing (prepend σ)` is always false (element `0` has block size `1`). -/ lemma not_isPairing_prepend {m : ℕ} (σ : NC m) : ¬ isPairing (prepend σ) := by intro hp have h0 := hp 0 (Nat.succ_pos m) unfold blockSize at h0 have hS : ((Finset.range (m + 1)).filter fun j => (prepend σ).rel 0 j = true) = {0} := by ext j; simp only [Finset.mem_filter, Finset.mem_range, Finset.mem_singleton, prepend_rel] constructor · rintro ⟨_, ⟨_, rfl⟩ | ⟨h1, _, _⟩⟩ · rfl · omega · rintro rfl; simp [Nat.succ_pos m] rw [hS, Finset.card_singleton] at h0 omega /-- Helper: `isPairing (joinAt j hj a b)` holds iff `isPairing a` and `b = prepend b'` with `isPairing b'`. -/ lemma isPairing_joinAt_iff {m j : ℕ} (hj : j < m) (a : NC j) (b : NC (m - j)) : isPairing (joinAt j hj a b) ↔ isPairing a ∧ (∀ i < m - j - 1, b.rel 0 (i + 1) = false) ∧ isPairing (window b 1 (m - j - 1) (by omega)) := by -- Compute blockSize of 0 in joinAt j hj a b: it equals 1 + blockSize_b 0! have hsz0 : (joinAt j hj a b).blockSize 0 = 1 + b.blockSize 0 := by unfold blockSize have hS : ((Finset.range (m + 1)).filter fun v => (joinAt j hj a b).rel 0 v = true) = insert 0 (((Finset.range (m - j)).filter fun y => b.rel 0 y = true).image (· + (j + 1))) := by ext v simp only [Finset.mem_filter, Finset.mem_range, Finset.mem_insert, Finset.mem_image, joinAt_rel] constructor · rintro ⟨hv, ⟨h0, _⟩ | ⟨_, hvz, hbrel⟩⟩ · omega · rcases hvz with rfl | ⟨hv1, hv2⟩ · exact Or.inl rfl · right refine ⟨v - (j + 1), ⟨by omega, ?_⟩, by omega⟩ rw [oIdx_zero, oIdx_pos (by omega)] at hbrel; exact hbrel · rintro (rfl | ⟨y, ⟨hy, hby⟩, rfl⟩) · refine ⟨Nat.succ_pos m, Or.inr ⟨Or.inl trivial, Or.inl rfl, ?_⟩⟩ rw [oIdx_zero]; exact b.refl 0 (by omega) · refine ⟨by omega, Or.inr ⟨Or.inl trivial, Or.inr ⟨by omega, by omega⟩, ?_⟩⟩ rw [oIdx_zero, oIdx_pos (by omega), Nat.add_sub_cancel]; exact hby rw [hS, Finset.card_insert_of_notMem, Finset.card_image_of_injective _ (add_left_injective _), add_comm] simp only [Finset.mem_image, not_exists, not_and]; intro y _ hy; omega -- Compute blockSize in the inner zone {1..j}: equals a.blockSize (u - 1) have hsz_in : ∀ u < j, (joinAt j hj a b).blockSize (u + 1) = a.blockSize u := by intro u hu unfold blockSize have hS : ((Finset.range (m + 1)).filter fun v => (joinAt j hj a b).rel (u + 1) v = true) = ((Finset.range j).filter fun w => a.rel u w = true).image (· + 1) := by ext v simp only [Finset.mem_filter, Finset.mem_range, Finset.mem_image, joinAt_rel] constructor · rintro ⟨_, ⟨_, _, hv1, hvj, ha⟩ | ⟨hu0, _, _⟩⟩ · exact ⟨v - 1, ⟨by omega, by simpa using ha⟩, by omega⟩ · omega · rintro ⟨w, ⟨hw, haw⟩, rfl⟩ exact ⟨by omega, Or.inl ⟨by omega, by omega, by omega, by omega, by simpa using haw⟩⟩ rw [hS, Finset.card_image_of_injective _ (add_left_injective _)] -- Compute blockSize in the outer zone {j+2..m} when b has 0 as singleton have hsz_out : (∀ i < m - j - 1, b.rel 0 (i + 1) = false) → ∀ y < m - j - 1, (joinAt j hj a b).blockSize (j + 1 + (y + 1)) = (window b 1 (m - j - 1) (by omega)).blockSize y := by intro hb0 y hy unfold blockSize have hS : ((Finset.range (m + 1)).filter fun v => (joinAt j hj a b).rel (j + 1 + (y + 1)) v = true) = ((Finset.range (m - j - 1)).filter fun z => (window b 1 (m - j - 1) (by omega)).rel y z = true).image (fun z => j + 1 + (z + 1)) := by ext v simp only [Finset.mem_filter, Finset.mem_range, Finset.mem_image, joinAt_rel, window_rel] constructor · rintro ⟨hv, ⟨_, hu2, _, _, _⟩ | ⟨_, hvz, hbrel⟩⟩ · omega · rw [oIdx_pos (by omega), show j + 1 + (y + 1) - (j + 1) = y + 1 by omega] at hbrel rcases hvz with rfl | ⟨hv1, hv2⟩ · rw [oIdx_zero, b.symm, hb0 y hy] at hbrel; exact absurd hbrel (by decide) · rw [oIdx_pos (by omega)] at hbrel rcases Nat.eq_or_lt_of_le hv1 with rfl | hvgt · rw [Nat.sub_self, b.symm, hb0 y hy] at hbrel; exact absurd hbrel (by decide) · refine ⟨v - (j + 2), ⟨by omega, hy, by omega, ?_⟩, by omega⟩ rwa [show v - (j + 2) + 1 = v - (j + 1) by omega] · rintro ⟨z, ⟨hz, _, _, hbyz⟩, rfl⟩ refine ⟨by omega, Or.inr ⟨Or.inr ⟨by omega, by omega⟩, Or.inr ⟨by omega, by omega⟩, ?_⟩⟩ rw [oIdx_pos (by omega), oIdx_pos (by omega), show j + 1 + (y + 1) - (j + 1) = y + 1 by omega, show j + 1 + (z + 1) - (j + 1) = z + 1 by omega] exact hbyz rw [hS, Finset.card_image_of_injective] intro x1 x2 hx; dsimp at hx; omega -- Also blockSize (j + 1) = blockSize 0 since 0 ~ j + 1! have hsz_link : (joinAt j hj a b).blockSize (j + 1) = (joinAt j hj a b).blockSize 0 := by unfold blockSize congr 1; ext v; simp only [Finset.mem_filter] refine and_congr_right fun _ => ?_ have h0j := joinAt_rel_zero_link (hj := hj) (a := a) (b := b) exact ⟨fun h => (joinAt j hj a b).trans _ _ _ h0j h, fun h => (joinAt j hj a b).trans _ _ _ (by rw [(joinAt j hj a b).symm]; exact h0j) h⟩ constructor · intro hp have hb0_sz : b.blockSize 0 = 1 := by have := hp 0 (Nat.succ_pos m); omega have hb0 : ∀ i < m - j - 1, b.rel 0 (i + 1) = false := by intro i hi cases hr : b.rel 0 (i + 1) · rfl · exfalso have hsub : ({0, i + 1} : Finset ℕ) ⊆ (Finset.range (m - j)).filter fun v => b.rel 0 v = true := by intro x hx; simp only [Finset.mem_insert, Finset.mem_singleton] at hx rcases hx with rfl | rfl · exact Finset.mem_filter.mpr ⟨Finset.mem_range.mpr (by omega), b.refl 0 (by omega)⟩ · exact Finset.mem_filter.mpr ⟨Finset.mem_range.mpr (by omega), hr⟩ have hle := Finset.card_le_card hsub rw [Finset.card_pair (by omega)] at hle unfold blockSize at hb0_sz; omega refine ⟨fun u hu => by rw [← hsz_in u hu]; exact hp (u + 1) (by omega), hb0, fun y hy => by rw [← hsz_out hb0 y hy]; exact hp (j + 1 + (y + 1)) (by omega)⟩ · rintro ⟨ha, hb0, hb'⟩ have hb0_sz : b.blockSize 0 = 1 := by unfold blockSize have hS : ((Finset.range (m - j)).filter fun v => b.rel 0 v = true) = {0} := by ext v; simp only [Finset.mem_filter, Finset.mem_range, Finset.mem_singleton] constructor · rintro ⟨hv, hrel⟩ rcases v with _ | v' · rfl · rw [hb0 v' (by omega)] at hrel; exact absurd hrel (by decide) · rintro rfl; exact ⟨by omega, b.refl 0 (by omega)⟩ rw [hS, Finset.card_singleton] intro u hu by_cases hu0 : u = 0 · subst hu0; rw [hsz0, hb0_sz] · by_cases huj : u ≤ j · obtain ⟨u', rfl⟩ : ∃ u', u = u' + 1 := ⟨u - 1, by omega⟩ rw [hsz_in u' (by omega)]; exact ha u' (by omega) · by_cases hul : u = j + 1 · subst hul; rw [hsz_link, hsz0, hb0_sz] · have hu_eq : u = j + 1 + ((u - (j + 2)) + 1) := by omega rw [hu_eq, hsz_out hb0 (u - (j + 2)) (by omega)] exact hb' (u - (j + 2)) (by omega) /-- **Exact Catalan Enumeration of Non-Crossing Pairings:** `|NC₂(2m)| = C_m` for all `m : ℕ`. -/ theorem card_NC2 (m : ℕ) : Fintype.card (NC2 m) = catalan m := by induction m using Nat.strong_induction_on with | _ m ih => rcases m with _ | k · have : Unique (NC2 0) := ⟨⟨⟨⊥, fun _ h => absurd h (Nat.not_lt_zero _)⟩⟩, fun ⟨π, _⟩ => Subtype.ext (ext' fun _ _ hi => absurd hi (Nat.not_lt_zero _))⟩ rw [Fintype.card_unique, catalan_zero] · -- Express card (NC2 (k + 1)) as sum over NC (2k + 2) of indicator of isPairing have hcard : Fintype.card (NC2 (k + 1)) = ∑ π : NC (2 * k + 1 + 1), if isPairing π then 1 else 0 := by have haux : ∀ (N : ℕ) (hN : N = 2 * k + 1 + 1), Fintype.card { π : NC N // isPairing π } = ∑ π : NC (2 * k + 1 + 1), if isPairing π then 1 else 0 := by rintro _ rfl; rw [Fintype.card_subtype, Finset.card_filter] exact haux (2 * (k + 1)) (by omega) rw [hcard, sum_decomp (2 * k + 1) (fun π => if isPairing π then 1 else 0)] -- Case A vanishes! have hA : ∑ σ : NC (2 * k + 1), (if isPairing (prepend σ) then 1 else 0) = 0 := Finset.sum_eq_zero fun σ _ => if_neg (not_isPairing_prepend σ) rw [hA, zero_add] -- In Case B, odd j contributes 0 (by NC2_odd_empty), even j = 2*r contributes |NC2 r| * |NC2 (k - r)|! have hB : ∀ j : Fin (2 * k + 1), (∑ p : NC j.1 × NC (2 * k + 1 - j.1), if isPairing (joinAt j.1 j.2 p.1 p.2) then 1 else 0) = if j.1 % 2 = 0 then catalan (j.1 / 2) * catalan (k - j.1 / 2) else 0 := by intro j rw [Fintype.sum_prod_type] by_cases hpar : j.1 % 2 = 0 · simp only [hpar, ↓reduceIte] set r := j.1 / 2 have hj_eq : j.1 = 2 * r := by omega have hsub_eq : 2 * k + 1 - j.1 = 2 * (k - r) + 1 := by omega -- Factor inner sum over b : NC (2*(k-r) + 1): condition is b = prepend b' with isPairing b' have hinner : ∀ a : NC j.1, (∑ b : NC (2 * k + 1 - j.1), if isPairing (joinAt j.1 j.2 a b) then 1 else 0) = if isPairing a then catalan (k - r) else 0 := by intro a by_cases ha : isPairing a · simp only [ha, ↓reduceIte] have heq_b : ∀ b : NC (2 * k + 1 - j.1), (if isPairing (joinAt j.1 j.2 a b) then 1 else 0) = (if (∀ i < 2 * k + 1 - j.1 - 1, b.rel 0 (i + 1) = false) ∧ isPairing (window b 1 (2 * k + 1 - j.1 - 1) (by omega)) then 1 else 0) := by intro b by_cases h : isPairing (joinAt j.1 j.2 a b) · rw [if_pos h, if_pos ((isPairing_joinAt_iff j.2 a b).mp h).2] · rw [if_neg h, if_neg (fun h' => h ((isPairing_joinAt_iff j.2 a b).mpr ⟨ha, h'⟩))] simp_rw [heq_b] -- Re-index along hsub_eq via cast to NC (2*(k-r)+1), then use sum_decomp! set n' := 2 * (k - r) have hlen : 2 * k + 1 - j.1 = n' + 1 := by omega let castE : NC (2 * k + 1 - j.1) ≃ NC (n' + 1) := Equiv.cast (congrArg NC hlen) have hcast_sum : (∑ b : NC (2 * k + 1 - j.1), if (∀ i < 2 * k + 1 - j.1 - 1, b.rel 0 (i + 1) = false) ∧ isPairing (window b 1 (2 * k + 1 - j.1 - 1) (by omega)) then 1 else 0) = ∑ b : NC (n' + 1), if (∀ i < n', b.rel 0 (i + 1) = false) ∧ isPairing (window b 1 n' (by omega)) then 1 else 0 := by have haux : ∀ (M : ℕ) (hM : M = n' + 1), (∑ b : NC M, if (∀ i < M - 1, b.rel 0 (i + 1) = false) ∧ isPairing (window b 1 (M - 1) (by omega)) then 1 else 0) = ∑ b : NC (n' + 1), if (∀ i < n', b.rel 0 (i + 1) = false) ∧ isPairing (window b 1 n' (by omega)) then 1 else 0 := by rintro _ rfl; rfl exact haux _ hlen rw [hcast_sum, sum_decomp n'] have hB0 : ∑ l : Fin n', ∑ p : NC l.1 × NC (n' - l.1), (if (∀ i < n', (joinAt l.1 l.2 p.1 p.2).rel 0 (i + 1) = false) ∧ isPairing (window (joinAt l.1 l.2 p.1 p.2) 1 n' (by omega)) then 1 else 0) = 0 := by refine Finset.sum_eq_zero fun l _ => Finset.sum_eq_zero fun p _ => if_neg ?_ rintro ⟨h0, _⟩ have := h0 l.1 l.2 rw [joinAt_rel_zero_link (hj := l.2)] at this exact absurd this (by decide) rw [hB0, add_zero] have hA_eq : ∑ σ : NC n', (if (∀ i < n', (prepend σ).rel 0 (i + 1) = false) ∧ isPairing (window (prepend σ) 1 n' (by omega)) then 1 else 0) = Fintype.card (NC2 (k - r)) := by rw [Fintype.card_subtype, Finset.card_filter] refine Finset.sum_congr rfl fun σ _ => ?_ rw [window_prepend] have h0 : ∀ i < n', (prepend σ).rel 0 (i + 1) = false := fun i _ => prepend_rel_zero_succ σ i by_cases hp : isPairing σ · rw [if_pos ⟨h0, hp⟩, if_pos hp] · rw [if_neg (fun h => hp h.2), if_neg hp] rw [hA_eq, ih (k - r) (by omega)] · simp only [ha, ↓reduceIte] refine Finset.sum_eq_zero fun b _ => if_neg fun h => ha ((isPairing_joinAt_iff j.2 a b).mp h).1 simp_rw [hinner] have houter : (∑ a : NC j.1, if isPairing a then catalan (k - r) else 0) = Fintype.card (NC2 r) * catalan (k - r) := by have h1 : (∑ a : NC j.1, if isPairing a then catalan (k - r) else 0) = (∑ a : NC (2 * r), if isPairing a then 1 else 0) * catalan (k - r) := by have haux : ∀ (J : ℕ) (hJ : J = 2 * r), (∑ a : NC J, if isPairing a then catalan (k - r) else 0) = (∑ a : NC (2 * r), if isPairing a then 1 else 0) * catalan (k - r) := by rintro _ rfl rw [Finset.sum_mul] refine Finset.sum_congr rfl fun a _ => by split_ifs <;> simp exact haux _ hj_eq rw [h1, ← Finset.card_filter, ← Fintype.card_subtype] rw [houter, ih r (by omega)] · simp only [hpar, ↓reduceIte] refine Finset.sum_eq_zero fun a _ => Finset.sum_eq_zero fun b _ => if_neg fun h => ?_ have ha := ((isPairing_joinAt_iff j.2 a b).mp h).1 have hodd : j.1 = 2 * (j.1 / 2) + 1 := by omega have hempty := NC2_odd_empty (j.1 / 2) have haux : ∀ (J : ℕ) (hJ : J = 2 * (j.1 / 2) + 1) (x : NC J), isPairing x → False := by rintro _ rfl x hx; exact hempty.false ⟨x, hx⟩ exact haux j.1 hodd a ha simp_rw [hB] rw [catalan_succ] -- Match ∑ j : Fin (2k+1), (if j % 2 = 0 then C_{j/2} * C_{k - j/2} else 0) with ∑ r : Fin (k+1), C_r * C_{k - r}! rw [← Finset.sum_filter] refine Finset.sum_nbij (fun j => (⟨j.1 / 2, by omega⟩ : Fin (k + 1))) ?_ ?_ ?_ ?_ · intro _ _; exact Finset.mem_univ _ · intro j1 hj1 j2 hj2 heq have h1 := (Finset.mem_filter.mp (Finset.mem_coe.mp hj1)).2 have h2 := (Finset.mem_filter.mp (Finset.mem_coe.mp hj2)).2 have hv := congrArg Fin.val heq dsimp at hv ext; omega · intro r _ have hrlt : r.1 < k + 1 := r.2 refine ⟨⟨2 * r.1, by omega⟩, Finset.mem_coe.mpr (Finset.mem_filter.mpr ⟨Finset.mem_univ _, by simp⟩), ?_⟩ ext; simp · intro _ _; rfl /-! ### Exact Interval Factorization of `NC(n)` -/ /-- The block of `i` in `π` as a `Finset ℕ`. -/ def blockFinset {n : ℕ} (π : NC n) (i : ℕ) : Finset ℕ := (Finset.range n).filter fun j => π.rel i j = true /-- The set of minimal representatives (roots) of blocks of `π`. -/ def rootSet {n : ℕ} (π : NC n) : Finset ℕ := (Finset.range n).filter fun i => π.isMin i = true /-- Pull back a partition `π : NC n` to an ordered subset `S : Finset ℕ` (`∀ x ∈ S, x < n`). -/ def restrictSet {n : ℕ} (π : NC n) (S : Finset ℕ) (hS : ∀ x ∈ S, x < n) : NC S.card where rel i j := if h : i < S.card ∧ j < S.card then π.rel (S.orderEmbOfFin rfl ⟨i, h.1⟩) (S.orderEmbOfFin rfl ⟨j, h.2⟩) else false supp i j hr := by split_ifs at hr with h; exact h refl i hi := by rw [dif_pos ⟨hi, hi⟩] exact π.refl _ (hS _ (S.orderEmbOfFin_mem rfl ⟨i, hi⟩)) symm i j := by by_cases h : i < S.card ∧ j < S.card · rw [dif_pos h, dif_pos ⟨h.2, h.1⟩, π.symm] · rw [dif_neg h, dif_neg (fun h' => h ⟨h'.2, h'.1⟩)] trans i j k h1 h2 := by split_ifs at h1 h2 with ha hb rw [dif_pos ⟨ha.1, hb.2⟩] exact π.trans _ _ _ h1 h2 nc a b c d hab hbc hcd hac hbd := by split_ifs at hac hbd with ha hb rw [dif_pos ⟨ha.1, hb.1⟩] let e := S.orderEmbOfFin rfl have h1 : S.orderEmbOfFin rfl ⟨a, ha.1⟩ < S.orderEmbOfFin rfl ⟨b, hb.1⟩ := e.strictMono (Fin.mk_lt_mk.mpr hab) have h2 : S.orderEmbOfFin rfl ⟨b, hb.1⟩ < S.orderEmbOfFin rfl ⟨c, ha.2⟩ := e.strictMono (Fin.mk_lt_mk.mpr hbc) have h3 : S.orderEmbOfFin rfl ⟨c, ha.2⟩ < S.orderEmbOfFin rfl ⟨d, hb.2⟩ := e.strictMono (Fin.mk_lt_mk.mpr hcd) exact π.nc _ _ _ _ h1 h2 h3 hac hbd lemma restrictSet_rel {n : ℕ} (π : NC n) (S : Finset ℕ) (hS : ∀ x ∈ S, x < n) (i j : ℕ) (hi : i < S.card) (hj : j < S.card) : (π.restrictSet S hS).rel i j = π.rel (S.orderEmbOfFin rfl ⟨i, hi⟩) (S.orderEmbOfFin rfl ⟨j, hj⟩) := dif_pos ⟨hi, hj⟩ /-- Unique block root of `i < n` in `σ : NC n`. -/ def rootOf {n : ℕ} (σ : NC n) (i : ℕ) : ℕ := if h : i < n then (σ.blockFinset i).min' ⟨i, Finset.mem_filter.mpr ⟨Finset.mem_range.mpr h, σ.refl i h⟩⟩ else 0 lemma rootOf_mem_rootSet {n : ℕ} (σ : NC n) {i : ℕ} (hi : i < n) : σ.rootOf i ∈ σ.rootSet := by simp only [rootOf, hi, ↓reduceDIte, rootSet, Finset.mem_filter, Finset.mem_range] set B := σ.blockFinset i have hne : B.Nonempty := ⟨i, Finset.mem_filter.mpr ⟨Finset.mem_range.mpr hi, σ.refl i hi⟩⟩ set r := B.min' hne obtain ⟨hrr, hri⟩ := Finset.mem_filter.mp (Finset.min'_mem B hne) refine ⟨Finset.mem_range.mp hrr, decide_eq_true fun k hkr => ?_⟩ cases hk : σ.rel k r · rfl · have hkB : k ∈ B := Finset.mem_filter.mpr ⟨Finset.mem_range.mpr (by have := Finset.mem_range.mp hrr; omega), σ.trans _ _ _ hri (by rw [σ.symm]; exact hk)⟩ have := Finset.min'_le B k hkB; omega lemma rootOf_rel {n : ℕ} (σ : NC n) {i : ℕ} (hi : i < n) : σ.rel (σ.rootOf i) i = true := by simp only [rootOf, hi, ↓reduceDIte] rw [σ.symm] exact (Finset.mem_filter.mp (Finset.min'_mem (σ.blockFinset i) _)).2 lemma rootOf_eq_iff {n : ℕ} (σ : NC n) {i j : ℕ} (hi : i < n) (hj : j < n) : σ.rootOf i = σ.rootOf j ↔ σ.rel i j = true := by constructor · intro heq have h1 := σ.rootOf_rel hi have h2 := σ.rootOf_rel hj rw [heq] at h1 exact σ.trans _ _ _ (by rw [σ.symm]; exact h1) h2 · intro hij simp only [rootOf, hi, hj, ↓reduceDIte] have hB : σ.blockFinset i = σ.blockFinset j := by ext k; simp only [blockFinset, Finset.mem_filter] exact and_congr_right fun _ => ⟨fun h => σ.trans _ _ _ (by rw [σ.symm]; exact hij) h, fun h => σ.trans _ _ _ hij h⟩ simp only [hB] lemma mem_blockFinset_rootOf {n : ℕ} (σ : NC n) {i : ℕ} (hi : i < n) : i ∈ σ.blockFinset (σ.rootOf i) := Finset.mem_filter.mpr ⟨Finset.mem_range.mpr hi, σ.rootOf_rel hi⟩ lemma blockFinset_lt {n : ℕ} (σ : NC n) (r : ℕ) : ∀ x ∈ σ.blockFinset r, x < n := fun _ hx => Finset.mem_range.mp (Finset.mem_filter.mp hx).1 /-- Glue independent partitions on each block of `σ` into a partition `≤ σ`. -/ noncomputable def glueOnBlocks {n : ℕ} (σ : NC n) (f : ∀ r : σ.rootSet, NC (σ.blockFinset r.1).card) : NC n where rel i j := if h : i < n ∧ j < n ∧ σ.rel i j = true then let r : σ.rootSet := ⟨σ.rootOf i, σ.rootOf_mem_rootSet h.1⟩ let e := (σ.blockFinset r.1).orderIsoOfFin rfl let idx_i := (e.symm ⟨i, σ.mem_blockFinset_rootOf h.1⟩).1 let idx_j := (e.symm ⟨j, by have hr := (σ.rootOf_eq_iff h.1 h.2.1).mpr h.2.2 change j ∈ σ.blockFinset (σ.rootOf i) rw [hr]; exact σ.mem_blockFinset_rootOf h.2.1⟩).1 (f r).rel idx_i idx_j else false supp i j hr := by split_ifs at hr with h; exact ⟨h.1, h.2.1⟩ refl i hi := by rw [dif_pos ⟨hi, hi, σ.refl i hi⟩] exact (f _).refl _ (Fin.isLt _) symm i j := by by_cases h : i < n ∧ j < n ∧ σ.rel i j = true · have h' : j < n ∧ i < n ∧ σ.rel j i = true := ⟨h.2.1, h.1, by rw [σ.symm]; exact h.2.2⟩ rw [dif_pos h, dif_pos h'] have hr : (⟨σ.rootOf i, σ.rootOf_mem_rootSet h.1⟩ : σ.rootSet) = ⟨σ.rootOf j, σ.rootOf_mem_rootSet h.2.1⟩ := Subtype.ext ((σ.rootOf_eq_iff h.1 h.2.1).mpr h.2.2) have : ∀ (r1 r2 : σ.rootSet) (hreq : r1 = r2) (hi1 : i ∈ σ.blockFinset r1.1) (hj1 : j ∈ σ.blockFinset r1.1) (hj2 : j ∈ σ.blockFinset r2.1) (hi2 : i ∈ σ.blockFinset r2.1), (f r1).rel (((σ.blockFinset r1.1).orderIsoOfFin rfl).symm ⟨i, hi1⟩).1 (((σ.blockFinset r1.1).orderIsoOfFin rfl).symm ⟨j, hj1⟩).1 = (f r2).rel (((σ.blockFinset r2.1).orderIsoOfFin rfl).symm ⟨j, hj2⟩).1 (((σ.blockFinset r2.1).orderIsoOfFin rfl).symm ⟨i, hi2⟩).1 := by rintro r1 _ rfl hi1 hj1 hj2 hi2; exact (f r1).symm _ _ exact this _ _ hr _ _ _ _ · have h' : ¬ (j < n ∧ i < n ∧ σ.rel j i = true) := fun h' => h ⟨h'.2.1, h'.1, by rw [σ.symm]; exact h'.2.2⟩ rw [dif_neg h, dif_neg h'] trans i j k h1 h2 := by split_ifs at h1 h2 with ha hb have hσik : σ.rel i k = true := σ.trans _ _ _ ha.2.2 hb.2.2 rw [dif_pos ⟨ha.1, hb.2.1, hσik⟩] have hr : (⟨σ.rootOf i, σ.rootOf_mem_rootSet ha.1⟩ : σ.rootSet) = ⟨σ.rootOf j, σ.rootOf_mem_rootSet ha.2.1⟩ := Subtype.ext ((σ.rootOf_eq_iff ha.1 ha.2.1).mpr ha.2.2) have haux : ∀ (r1 r2 : σ.rootSet) (hreq : r1 = r2) (hi1 : i ∈ σ.blockFinset r1.1) (hj1 : j ∈ σ.blockFinset r1.1) (hk1 : k ∈ σ.blockFinset r1.1) (hj2 : j ∈ σ.blockFinset r2.1) (hk2 : k ∈ σ.blockFinset r2.1), (f r1).rel (((σ.blockFinset r1.1).orderIsoOfFin rfl).symm ⟨i, hi1⟩).1 (((σ.blockFinset r1.1).orderIsoOfFin rfl).symm ⟨j, hj1⟩).1 = true → (f r2).rel (((σ.blockFinset r2.1).orderIsoOfFin rfl).symm ⟨j, hj2⟩).1 (((σ.blockFinset r2.1).orderIsoOfFin rfl).symm ⟨k, hk2⟩).1 = true → (f r1).rel (((σ.blockFinset r1.1).orderIsoOfFin rfl).symm ⟨i, hi1⟩).1 (((σ.blockFinset r1.1).orderIsoOfFin rfl).symm ⟨k, hk1⟩).1 = true := by rintro r1 _ rfl _ _ _ _ _ p q; exact (f r1).trans _ _ _ p q exact haux _ _ hr _ _ _ _ _ h1 h2 nc a b c d hab hbc hcd hac hbd := by split_ifs at hac hbd with ha hb have hσab : σ.rel a b = true := σ.nc a b c d hab hbc hcd ha.2.2 hb.2.2 rw [dif_pos ⟨ha.1, hb.1, hσab⟩] have hr : (⟨σ.rootOf a, σ.rootOf_mem_rootSet ha.1⟩ : σ.rootSet) = ⟨σ.rootOf b, σ.rootOf_mem_rootSet hb.1⟩ := Subtype.ext ((σ.rootOf_eq_iff ha.1 hb.1).mpr hσab) have haux : ∀ (r1 r2 : σ.rootSet) (hreq : r1 = r2) (ha1 : a ∈ σ.blockFinset r1.1) (hb1 : b ∈ σ.blockFinset r1.1) (hc1 : c ∈ σ.blockFinset r1.1) (hd1 : d ∈ σ.blockFinset r1.1) (hb2 : b ∈ σ.blockFinset r2.1) (hd2 : d ∈ σ.blockFinset r2.1), (f r1).rel (((σ.blockFinset r1.1).orderIsoOfFin rfl).symm ⟨a, ha1⟩).1 (((σ.blockFinset r1.1).orderIsoOfFin rfl).symm ⟨c, hc1⟩).1 = true → (f r2).rel (((σ.blockFinset r2.1).orderIsoOfFin rfl).symm ⟨b, hb2⟩).1 (((σ.blockFinset r2.1).orderIsoOfFin rfl).symm ⟨d, hd2⟩).1 = true → (f r1).rel (((σ.blockFinset r1.1).orderIsoOfFin rfl).symm ⟨a, ha1⟩).1 (((σ.blockFinset r1.1).orderIsoOfFin rfl).symm ⟨b, hb1⟩).1 = true := by rintro r1 _ rfl ha1 hb1 hc1 hd1 _ _ p q let e := ((σ.blockFinset r1.1).orderIsoOfFin rfl).symm exact (f r1).nc _ _ _ _ (Fin.lt_def.mp (e.strictMono (Subtype.mk_lt_mk.mpr hab))) (Fin.lt_def.mp (e.strictMono (Subtype.mk_lt_mk.mpr hbc))) (Fin.lt_def.mp (e.strictMono (Subtype.mk_lt_mk.mpr hcd))) p q have hσad : σ.rel a d = true := σ.trans _ _ _ hσab hb.2.2 have hd1 : d ∈ σ.blockFinset (σ.rootOf a) := by rw [(σ.rootOf_eq_iff ha.1 hb.2.1).mpr hσad]; exact σ.mem_blockFinset_rootOf hb.2.1 have hb1 : b ∈ σ.blockFinset (σ.rootOf a) := by rw [(σ.rootOf_eq_iff ha.1 hb.1).mpr hσab]; exact σ.mem_blockFinset_rootOf hb.1 have hc1 : c ∈ σ.blockFinset (σ.rootOf a) := by rw [(σ.rootOf_eq_iff ha.1 ha.2.1).mpr ha.2.2]; exact σ.mem_blockFinset_rootOf ha.2.1 have hd2 : d ∈ σ.blockFinset (σ.rootOf b) := by rw [(σ.rootOf_eq_iff hb.1 hb.2.1).mpr hb.2.2]; exact σ.mem_blockFinset_rootOf hb.2.1 exact haux _ _ hr (σ.mem_blockFinset_rootOf ha.1) hb1 hc1 hd1 (σ.mem_blockFinset_rootOf hb.1) hd2 hac hbd lemma glueOnBlocks_le {n : ℕ} (σ : NC n) (f : ∀ r : σ.rootSet, NC (σ.blockFinset r.1).card) : glueOnBlocks σ f ≤ σ := by intro i j hij dsimp only [glueOnBlocks] at hij split_ifs at hij with h exact h.2.2 /-- **Lower Interval Factorization Theorem:** `[⊥, σ] ≃o ∏ r : σ.rootSet, NC (σ.blockFinset r).card`. -/ noncomputable def lowerIntervalOrderIso {n : ℕ} (σ : NC n) : { π : NC n // π ≤ σ } ≃o (∀ r : σ.rootSet, NC (σ.blockFinset r.1).card) where toFun π := fun r => π.1.restrictSet (σ.blockFinset r.1) (σ.blockFinset_lt r.1) invFun f := ⟨glueOnBlocks σ f, glueOnBlocks_le σ f⟩ left_inv π := by apply Subtype.ext apply ext' intro i j hi hj change (glueOnBlocks σ (fun r => π.1.restrictSet (σ.blockFinset r.1) (σ.blockFinset_lt r.1))).rel i j = π.1.rel i j dsimp only [glueOnBlocks] by_cases hσ : σ.rel i j = true · rw [dif_pos ⟨hi, hj, hσ⟩, restrictSet_rel _ _ _ _ _ (Fin.isLt _) (Fin.isLt _)] set S := σ.blockFinset (σ.rootOf i) have hiS : i ∈ S := σ.mem_blockFinset_rootOf hi have hjS : j ∈ S := by change j ∈ σ.blockFinset (σ.rootOf i) rw [(σ.rootOf_eq_iff hi hj).mpr hσ]; exact σ.mem_blockFinset_rootOf hj have e1 : S.orderEmbOfFin rfl ((S.orderIsoOfFin rfl).symm ⟨i, hiS⟩) = i := by rw [← Finset.coe_orderIsoOfFin_apply] exact congrArg Subtype.val ((S.orderIsoOfFin rfl).apply_symm_apply ⟨i, hiS⟩) have e2 : S.orderEmbOfFin rfl ((S.orderIsoOfFin rfl).symm ⟨j, hjS⟩) = j := by rw [← Finset.coe_orderIsoOfFin_apply] exact congrArg Subtype.val ((S.orderIsoOfFin rfl).apply_symm_apply ⟨j, hjS⟩) exact congrArg₂ π.1.rel e1 e2 · rw [dif_neg (fun (h : i < n ∧ j < n ∧ σ.rel i j = true) => hσ h.2.2)] cases hp : π.1.rel i j · rfl · exact absurd (π.2 i j hp) hσ right_inv f := by funext r apply ext' intro i j hi hj rw [restrictSet_rel _ _ _ _ _ hi hj] set S := σ.blockFinset r.1 set x := S.orderEmbOfFin rfl ⟨i, hi⟩ set y := S.orderEmbOfFin rfl ⟨j, hj⟩ have hxS : x ∈ S := S.orderEmbOfFin_mem rfl ⟨i, hi⟩ have hyS : y ∈ S := S.orderEmbOfFin_mem rfl ⟨j, hj⟩ have hxn : x < n := σ.blockFinset_lt r.1 x hxS have hyn : y < n := σ.blockFinset_lt r.1 y hyS have hrx : σ.rel r.1 x = true := (Finset.mem_filter.mp hxS).2 have hry : σ.rel r.1 y = true := (Finset.mem_filter.mp hyS).2 have hxy : σ.rel x y = true := σ.trans _ _ _ (by rw [σ.symm]; exact hrx) hry have hroot : σ.rootOf x = r.1 := by have hrr : σ.rootOf r.1 = r.1 := by have hr_min := of_decide_eq_true (Finset.mem_filter.mp r.2).2 have hrn : r.1 < n := Finset.mem_range.mp (Finset.mem_filter.mp r.2).1 have hle := Finset.min'_le (σ.blockFinset r.1) r.1 (Finset.mem_filter.mpr ⟨Finset.mem_range.mpr hrn, σ.refl r.1 hrn⟩) have hrel := σ.rootOf_rel hrn simp only [rootOf, hrn, ↓reduceDIte] at hrel ⊢ by_contra hne have hlt : (σ.blockFinset r.1).min' _ < r.1 := lt_of_le_of_ne hle hne have := hr_min _ hlt; rw [hrel] at this; exact absurd this (by decide) rw [← hrr] exact (σ.rootOf_eq_iff hxn (Finset.mem_range.mp (Finset.mem_filter.mp r.2).1)).mpr (by rw [σ.symm]; exact hrx) dsimp only [glueOnBlocks] rw [dif_pos ⟨hxn, hyn, hxy⟩] have hr_sub : (⟨σ.rootOf x, σ.rootOf_mem_rootSet hxn⟩ : σ.rootSet) = r := Subtype.ext hroot have haux : ∀ (r1 : σ.rootSet) (hreq : r1 = r) (hx1 : x ∈ σ.blockFinset r1.1) (hy1 : y ∈ σ.blockFinset r1.1), (f r1).rel (((σ.blockFinset r1.1).orderIsoOfFin rfl).symm ⟨x, hx1⟩).1 (((σ.blockFinset r1.1).orderIsoOfFin rfl).symm ⟨y, hy1⟩).1 = (f r).rel i j := by rintro _ rfl hx1 hy1 have ei : ((S.orderIsoOfFin rfl).symm ⟨x, hx1⟩) = ⟨i, hi⟩ := (S.orderIsoOfFin rfl).symm_apply_apply ⟨i, hi⟩ have ej : ((S.orderIsoOfFin rfl).symm ⟨y, hy1⟩) = ⟨j, hj⟩ := (S.orderIsoOfFin rfl).symm_apply_apply ⟨j, hj⟩ rw [ei, ej] exact haux _ hr_sub _ _ map_rel_iff' := by intro π1 π2 constructor · intro h i j hij have hσ : σ.rel i j = true := π1.2 i j hij have ⟨hi, hj⟩ := π1.1.supp i j hij set r : σ.rootSet := ⟨σ.rootOf i, σ.rootOf_mem_rootSet hi⟩ set S := σ.blockFinset r.1 set e := S.orderIsoOfFin rfl have hiS : i ∈ S := σ.mem_blockFinset_rootOf hi have hjS : j ∈ S := by have := (σ.rootOf_eq_iff hi hj).mpr hσ change j ∈ σ.blockFinset (σ.rootOf i); rw [this]; exact σ.mem_blockFinset_rootOf hj set ui := e.symm ⟨i, hiS⟩ set uj := e.symm ⟨j, hjS⟩ have hu : S.orderEmbOfFin rfl ui = i := by rw [← Finset.coe_orderIsoOfFin_apply]; exact congrArg Subtype.val (e.apply_symm_apply ⟨i, hiS⟩) have hv : S.orderEmbOfFin rfl uj = j := by rw [← Finset.coe_orderIsoOfFin_apply]; exact congrArg Subtype.val (e.apply_symm_apply ⟨j, hjS⟩) have h1 : (π1.1.restrictSet S (σ.blockFinset_lt r.1)).rel ui.1 uj.1 = true := by rw [restrictSet_rel _ _ _ _ _ ui.2 uj.2, hu, hv]; exact hij have h2 := h r ui.1 uj.1 h1 change (π2.1.restrictSet S (σ.blockFinset_lt r.1)).rel ui.1 uj.1 = true at h2 rw [restrictSet_rel _ _ _ _ _ ui.2 uj.2, hu, hv] at h2 exact h2 · intro h r i j hij change (π1.1.restrictSet (σ.blockFinset r.1) (σ.blockFinset_lt r.1)).rel i j = true at hij change (π2.1.restrictSet (σ.blockFinset r.1) (σ.blockFinset_lt r.1)).rel i j = true have ⟨hi, hj⟩ := (π1.1.restrictSet (σ.blockFinset r.1) (σ.blockFinset_lt r.1)).supp i j hij rw [restrictSet_rel _ _ (σ.blockFinset_lt r.1) _ _ hi hj] at hij ⊢ exact h _ _ hij /-- **General Interval Factorization Theorem:** For `π ≤ σ` in `NC(n)`, `[π, σ]` is order-isomorphic to `∏ r : σ.rootSet, { τ : NC (σ.blockFinset r).card // π.restrictSet (σ.blockFinset r) ≤ τ }`. -/ noncomputable def intervalOrderIso {n : ℕ} {π σ : NC n} (hπσ : π ≤ σ) : { τ : NC n // π ≤ τ ∧ τ ≤ σ } ≃o (∀ r : σ.rootSet, { τ : NC (σ.blockFinset r.1).card // π.restrictSet (σ.blockFinset r.1) (σ.blockFinset_lt r.1) ≤ τ }) where toFun τ := fun r => ⟨τ.1.restrictSet (σ.blockFinset r.1) (σ.blockFinset_lt r.1), fun i j hij => by have ⟨hi, hj⟩ := (π.restrictSet _ _).supp i j hij rw [restrictSet_rel _ _ _ _ _ hi hj] at hij ⊢ exact τ.2.1 _ _ hij⟩ invFun f := ⟨glueOnBlocks σ (fun r => (f r).1), ⟨by have h1 : (lowerIntervalOrderIso σ).symm (fun r => π.restrictSet (σ.blockFinset r.1) (σ.blockFinset_lt r.1)) = ⟨π, hπσ⟩ := (lowerIntervalOrderIso σ).symm_apply_apply ⟨π, hπσ⟩ have h2 : (lowerIntervalOrderIso σ).symm (fun r => π.restrictSet (σ.blockFinset r.1) (σ.blockFinset_lt r.1)) ≤ (lowerIntervalOrderIso σ).symm (fun r => (f r).1) := (lowerIntervalOrderIso σ).symm.monotone (fun r => (f r).2) rw [h1] at h2 exact h2, glueOnBlocks_le σ (fun r => (f r).1)⟩⟩ left_inv τ := by apply Subtype.ext change glueOnBlocks σ (fun r => τ.1.restrictSet (σ.blockFinset r.1) (σ.blockFinset_lt r.1)) = τ.1 exact congrArg Subtype.val ((lowerIntervalOrderIso σ).left_inv ⟨τ.1, τ.2.2⟩) right_inv f := by funext r apply Subtype.ext change (glueOnBlocks σ (fun r => (f r).1)).restrictSet (σ.blockFinset r.1) (σ.blockFinset_lt r.1) = (f r).1 exact congrFun ((lowerIntervalOrderIso σ).right_inv (fun r => (f r).1)) r map_rel_iff' := fun {τ1 τ2} => (lowerIntervalOrderIso σ).map_rel_iff (a := ⟨τ1.1, τ1.2.2⟩) (b := ⟨τ2.1, τ2.2.2⟩) end NC /-! ### Kernel Axiom Audit for Headline Theorems -/ #print axioms NC.card_NC #print axioms NC.succ_mul_card_NC #print axioms NC.narayana_dvd #print axioms NC.narayana_symm #print axioms NC.narayanaPowerSeries_quadratic #print axioms NC.card_NC_blockCount #print axioms NC.sum_narayana_eq_catalan #print axioms NC.kreweras_antitone #print axioms NC.blockCount_add_kreweras #print axioms NC.kreweras_sq_eq_rot #print axioms NC.krewerasOrderIso #print axioms NC.kreweras_bijective #print axioms NC.NC2_odd_empty #print axioms NC.card_NC2 #print axioms NC.lowerIntervalOrderIso #print axioms NC.intervalOrderIso end FreeProb