import Mathlib /-! # Type-A_n Triangulated Polygon, Ptolemy Cluster Algebras, Associahedra & Tamari Lattices This module is completely self-contained (importing only `Mathlib`) and formalizes: 1. **Type-`A_n` Triangulated `(n + 3)`-Gon Model & Universal Snake-Graph Positivity**: - Diagonals, crossing relation, boundary edges, and maximal fan triangulation `fanDiags n` (`fanDiags_card`, `fanDiags_pairwise_noncrossing`, `fanDiags_maximal`); - Exact $2 \times m$ matrix Ptolemy exchange identity over ANY commutative ring (`ptolemy_matrix_realization`); - **Universal Type-`A_n` Positive Laurent & Snake-Graph Perfect-Matching Theorem**: for EVERY `n : ℕ` and EVERY diagonal `(i, j)` of the `(n + 3)`-gon, the explicit fan snake-graph matching polynomial `snakeMatchPoly n i j ∈ ℤ[x₁^±1, ..., x_n^±1]` is strictly positive (`typeA_snake_graph_positivity`), counts exact snake-graph matchings (`typeA_snake_graph_matching_count`), and satisfies **ALL Ptolemy cluster exchange identities** `L_{a,c} * L_{b,d} = L_{a,b} * L_{c,d} + L_{a,d} * L_{b,c}` (`typeA_ptolemy_exchange_all`)! 2. **Full Binary Trees, Catalan Enumeration & Stasheff Associahedron Flip Graph**: - Exact Catalan bijection `card_binTreeN_eq_catalan` (`Fintype.card (BinTreeN n) = catalanNat n`); - Right rotations, flip adjacency, and **Exact `(n - 1)`-Regularity of Every Stasheff Associahedron `K_{n+2}`** (`associahedron_degree_exact`) for all `n ≥ 1`; - Exact right-comb reachability `tamari_dist_to_rightComb` (`n - rightSpineLen t` flips), unique top `rightComb n` (`tamari_le_rightComb`) and bottom `leftComb n` (`leftComb_tamari_le`)! 3. **Tamari Lattice & Loday–Ronco / Björner–Wachs Weak-Bruhat Quotient Theorem**: - Strict left-weight potential (`leftWeight_lt_of_rightRot`) and antisymmetry (`tamari_antisymm`); - **Huang–Tamari Bracket-Vector Theorem (`tamari_iff_rVec_le`)**: `TamariLE t₁ t₂` holds **IF AND ONLY IF** `rVec t₁ ≤ rVec t₂` componentwise! - **Full Tamari Lattice Theorem (`tamari_meet_spec`, `tamari_join_spec`)**: explicit meet & join constructions satisfying exact universal lattice properties for EVERY `n : ℕ`! - **Loday–Ronco / Björner–Wachs Theorem (`weakBruhat_implies_tamari`, `permToBinTree_surjective`, `tamari_strict_quotient_of_weak`)**: right-to-left BST insertion maps Right Weak Bruhat Order on `S_n` surjectively and order-preservingly onto the Tamari lattice for EVERY `n : ℕ`, strictly contracting for `n ≥ 3` (`|S_3| = 6 > 5 = C_3`)! -/ namespace TypeAPolygonPtolemyAndTamari /-! ## 1. Type-`A_n` Triangulated `(n + 3)`-Gon, Crossing, Fan Triangulation & Ptolemy Identity -/ /-- A diagonal (or boundary chord) of a convex `m`-gon is an ordered pair `a < b` in `Fin m`. -/ structure Diag (m : ℕ) where a : Fin m b : Fin m hab : a < b deriving DecidableEq /-- A chord `(a, b)` of an `m`-gon is a boundary edge if `b = a + 1` or `(a, b) = (0, m - 1)`. -/ def Diag.IsBoundary {m : ℕ} (d : Diag m) : Prop := d.b.1 = d.a.1 + 1 ∨ (d.a.1 = 0 ∧ d.b.1 + 1 = m) /-- Two chords of an `m`-gon *cross* iff their endpoints strictly interleave around the polygon. -/ def Crosses {m : ℕ} (d₁ d₂ : Diag m) : Prop := (d₁.a < d₂.a ∧ d₂.a < d₁.b ∧ d₁.b < d₂.b) ∨ (d₂.a < d₁.a ∧ d₁.a < d₂.b ∧ d₂.b < d₁.b) lemma crosses_symm {m : ℕ} {d₁ d₂ : Diag m} : Crosses d₁ d₂ → Crosses d₂ d₁ := fun h => h.symm lemma crosses_irrefl {m : ℕ} (d : Diag m) : ¬ Crosses d d := by unfold Crosses rintro (⟨h, _⟩ | ⟨h, _⟩) <;> exact lt_irrefl _ h lemma not_crosses_boundary {m : ℕ} {d₁ d₂ : Diag m} (hb : d₁.IsBoundary) : ¬ Crosses d₁ d₂ := by rcases hb with h1 | ⟨h0, hm⟩ · rintro (⟨h1a, h2a, _⟩ | ⟨h1a, h2a, h3a⟩) · have := d₁.hab; have := d₂.hab; omega · have := d₁.hab; have := d₂.hab; omega · rintro (⟨h1a, _, h3a⟩ | ⟨h1a, _, _⟩) · have := d₂.b.2; omega · omega /-- The canonical *fan triangulation* of the `(n + 3)`-gon from vertex `0`: the `n` internal diagonals `(0, k + 2)` for `k : Fin n`. -/ def fanDiag (n : ℕ) (k : Fin n) : Diag (n + 3) := ⟨⟨0, by omega⟩, ⟨k.1 + 2, by omega⟩, by show (0 : ℕ) < k.1 + 2; omega⟩ def fanDiags (n : ℕ) : Finset (Diag (n + 3)) := Finset.univ.image (fanDiag n) /-- **THEOREM 1A — Exact Size, Non-Crossing & Maximality of the Type-`A_n` Fan Triangulation**: -/ theorem fanDiags_card (n : ℕ) : (fanDiags n).card = n := by unfold fanDiags rw [Finset.card_image_of_injective _ (by intro i j h have := congrArg (fun d : Diag (n + 3) => d.b.1) h dsimp [fanDiag] at this exact Fin.ext (by omega))] simp theorem fanDiags_pairwise_noncrossing (n : ℕ) : ∀ d₁ ∈ fanDiags n, ∀ d₂ ∈ fanDiags n, ¬ Crosses d₁ d₂ := by intro d₁ h₁ d₂ h₂ rcases Finset.mem_image.mp h₁ with ⟨i, _, rfl⟩ rcases Finset.mem_image.mp h₂ with ⟨j, _, rfl⟩ unfold Crosses fanDiag rintro (⟨h, _⟩ | ⟨h, _⟩) <;> exact lt_irrefl _ h theorem fanDiags_maximal (n : ℕ) (d : Diag (n + 3)) (hnb : ¬ d.IsBoundary) (hnm : d ∉ fanDiags n) : ∃ f ∈ fanDiags n, Crosses d f := by rcases d with ⟨a, b, hab⟩ have ha0 : a.1 ≠ 0 := by intro h0 apply hnm have hb2 : 2 ≤ b.1 := by have : a.1 < b.1 := hab by_contra hlt apply hnb; left; change b.1 = a.1 + 1; omega have hbn : b.1 < n + 2 := by have hbm : b.1 < n + 3 := b.2 by_contra hge apply hnb; right; exact ⟨h0, by change b.1 + 1 = n + 3; omega⟩ refine Finset.mem_image.mpr ⟨⟨b.1 - 2, by omega⟩, Finset.mem_univ _, ?_⟩ simp only [fanDiag, Diag.mk.injEq] exact ⟨Fin.ext (by dsimp; omega), Fin.ext (by dsimp; omega)⟩ have hgap : a.1 + 2 ≤ b.1 := by have : a.1 < b.1 := hab by_contra hlt apply hnb; left; change b.1 = a.1 + 1; omega have hka : a.1 - 1 < n := by have hbm : b.1 < n + 3 := b.2; omega refine ⟨fanDiag n ⟨a.1 - 1, hka⟩, Finset.mem_image_of_mem _ (Finset.mem_univ _), Or.inr ?_⟩ refine ⟨?_, ?_, ?_⟩ · show (0 : ℕ) < a.1; omega · show a.1 < (a.1 - 1) + 2; omega · show (a.1 - 1) + 2 < b.1; omega /-- **THEOREM 1B — Exact Universal Matrix Ptolemy Identity (`ptolemy_matrix_realization`)**: For ANY `2 × m` matrix `M` over ANY commutative ring `R`, the `2 × 2` Plücker minors `x_{i, j} = M 0 i * M 1 j - M 1 i * M 0 j` satisfy the exact Ptolemy exchange identity: `x_{a, c} * x_{b, d} = x_{a, b} * x_{c, d} + x_{a, d} * x_{b, c}`! -/ theorem ptolemy_matrix_realization {R : Type*} [CommRing R] {m : ℕ} (M : Matrix (Fin 2) (Fin m) R) (a b c d : Fin m) : let x := fun i j => M 0 i * M 1 j - M 1 i * M 0 j x a c * x b d = x a b * x c d + x a d * x b c := by intro x dsimp only [x] ring /-! ### 1A. Universal Type-`A_n` Positive Laurent & Snake-Graph Perfect-Matching Theorem -/ /-- The `n`-variable Laurent polynomial ring `ℤ[x_0^±1, ..., x_{n-1}^±1]`. -/ abbrev LaurentA (n : ℕ) := AddMonoidAlgebra ℤ (Fin n → ℤ) /-- Single monomial `c * ∏_r x_r^{e r}` in `LaurentA n`. -/ noncomputable def laMon {n : ℕ} (e : Fin n → ℤ) (c : ℤ) : LaurentA n := AddMonoidAlgebra.single e c lemma laMon_mul {n : ℕ} (e₁ e₂ : Fin n → ℤ) (c₁ c₂ : ℤ) : laMon e₁ c₁ * laMon e₂ c₂ = laMon (e₁ + e₂) (c₁ * c₂) := AddMonoidAlgebra.single_mul_single e₁ e₂ c₁ c₂ lemma laMon_one {n : ℕ} : (1 : LaurentA n) = laMon 0 1 := by rw [AddMonoidAlgebra.one_def]; rfl /-- Positivity predicate in `LaurentA n`: every coefficient is nonnegative and `f ≠ 0`. -/ def IsPosLaurentA {n : ℕ} (f : LaurentA n) : Prop := (∀ e : Fin n → ℤ, 0 ≤ f.coeff e) ∧ f ≠ 0 lemma isPosLaurentA_mon {n : ℕ} (e : Fin n → ℤ) {c : ℤ} (hc : 0 < c) : IsPosLaurentA (laMon e c) := by constructor · intro e' unfold laMon rw [AddMonoidAlgebra.coeff_single, Finsupp.single_apply] split_ifs · exact le_of_lt hc · exact le_rfl · intro h0 have := congrArg (fun g : LaurentA n => g.coeff e) h0 simp [laMon] at this omega lemma isPosLaurentA_add {n : ℕ} {f g : LaurentA n} (hf : IsPosLaurentA f) (hg : IsPosLaurentA g) : IsPosLaurentA (f + g) := by constructor · intro e rw [AddMonoidAlgebra.coeff_add] exact add_nonneg (hf.1 e) (hg.1 e) · intro h0 apply hf.2 ext e have hfp := hf.1 e have hgp := hg.1 e have hsum : f.coeff e + g.coeff e = 0 := by have := congrArg (fun h : LaurentA n => h.coeff e) h0 simpa [AddMonoidAlgebra.coeff_add] using this change f.coeff e = (0 : LaurentA n).coeff e rw [AddMonoidAlgebra.coeff_zero, Finsupp.zero_apply] omega lemma isPosLaurentA_sum {n : ℕ} {ι : Type*} (s : Finset ι) (hs : s.Nonempty) (f : ι → LaurentA n) (hf : ∀ i ∈ s, IsPosLaurentA (f i)) : IsPosLaurentA (∑ i ∈ s, f i) := by classical induction s using Finset.induction_on with | empty => exact absurd rfl hs.ne_empty | insert a s ha ih => rw [Finset.sum_insert ha] by_cases hse : s.Nonempty · exact isPosLaurentA_add (hf a (Finset.mem_insert_self a s)) (ih hse (fun i hi => hf i (Finset.mem_insert_of_mem hi))) · rw [Finset.not_nonempty_iff_eq_empty.mp hse, Finset.sum_empty, add_zero] exact hf a (Finset.mem_insert_self a s) /-- Exponent vector of the fan cluster variable `x_{0, k}` (`k : Fin (n + 3)`): `0` on boundary chords `k = 0, 1, n + 2`, and `e_{k - 2}` for internal fan diagonals `2 ≤ k ≤ n + 1`. -/ def fanExp (n : ℕ) (k : Fin (n + 3)) : Fin n → ℤ := if 2 ≤ k.1 ∧ k.1 < n + 2 then fun r => if r.1 = k.1 - 2 then 1 else 0 else 0 /-- The fan cluster variable `x_{0, k}` in `LaurentA n` and its inverse `x_{0, k}⁻¹`. -/ noncomputable def xFan (n : ℕ) (k : Fin (n + 3)) : LaurentA n := laMon (fanExp n k) 1 noncomputable def xFanInv (n : ℕ) (k : Fin (n + 3)) : LaurentA n := laMon (-(fanExp n k)) 1 lemma xFan_mul_xFanInv (n : ℕ) (k : Fin (n + 3)) : xFan n k * xFanInv n k = 1 := by unfold xFan xFanInv rw [laMon_mul, add_neg_cancel, mul_one, ← laMon_one] /-- Exact *fan snake-graph matching polynomial* `snakeMatchPoly n i j : LaurentA n` for every ordered chord `0 ≤ i < j ≤ n + 2`: - If `i = 0`, it is the fan variable `x_{0, j}`; - If `1 ≤ i < j`, it is the exact sum over all `j - i` perfect matchings of the fan snake graph `G_{i, j}`: `L_{i, j} = ∑_{k ∈ [i, j)} x_{0, i} * x_{0, j} * x_{0, k}⁻¹ * x_{0, k + 1}⁻¹`! -/ noncomputable def snakeMatchPoly (n : ℕ) (i j : Fin (n + 3)) : LaurentA n := if i.1 = 0 then if j.1 = 0 then 0 else xFan n j else ∑ k ∈ Finset.Ico i.1 j.1, if hk : k + 1 < n + 3 then laMon (fanExp n i + fanExp n j - fanExp n ⟨k, by omega⟩ - fanExp n ⟨k + 1, hk⟩) 1 else 0 /-- Boundary values: `L_{0, 1} = 1`, `L_{0, n + 2} = 1`, and `L_{i, i + 1} = 1`! -/ theorem snakeMatchPoly_boundary_zero (n : ℕ) : snakeMatchPoly n ⟨0, by omega⟩ ⟨1, by omega⟩ = 1 ∧ snakeMatchPoly n ⟨0, by omega⟩ ⟨n + 2, by omega⟩ = 1 := by constructor <;> simp [snakeMatchPoly, xFan, fanExp, laMon_one] theorem snakeMatchPoly_boundary_adj (n : ℕ) (i : ℕ) (hi1 : 1 ≤ i) (hi2 : i + 1 < n + 3) : snakeMatchPoly n ⟨i, by omega⟩ ⟨i + 1, hi2⟩ = 1 := by unfold snakeMatchPoly have hi0 : i ≠ 0 := by omega simp only [hi0, ↓reduceIte, Nat.Ico_succ_singleton, Finset.sum_singleton] rw [dif_pos hi2] have hzero : fanExp n ⟨i, by omega⟩ + fanExp n ⟨i + 1, hi2⟩ - fanExp n ⟨i, by omega⟩ - fanExp n ⟨i + 1, hi2⟩ = 0 := by abel rw [hzero, ← laMon_one] /-- **THEOREM 2 — Universal Type-`A_n` Snake-Graph Positivity (`typeA_snake_graph_positivity`)**: For **EVERY** `n : ℕ` and **EVERY** chord `i < j` of the `(n + 3)`-gon, the cluster variable `snakeMatchPoly n i j ∈ ℤ[x₁^±1, ..., x_n^±1]` is a **STRICTLY POSITIVE LAURENT POLYNOMIAL**! -/ theorem typeA_snake_graph_positivity (n : ℕ) (i j : Fin (n + 3)) (hij : i < j) : IsPosLaurentA (snakeMatchPoly n i j) := by unfold snakeMatchPoly by_cases hi0 : i.1 = 0 · have hj0 : j.1 ≠ 0 := by have : i.1 < j.1 := hij; omega simp only [hi0, hj0, ↓reduceIte] exact isPosLaurentA_mon _ (by omega) · simp only [hi0, ↓reduceIte] have hne : (Finset.Ico i.1 j.1).Nonempty := ⟨i.1, Finset.mem_Ico.mpr ⟨le_rfl, hij⟩⟩ refine isPosLaurentA_sum _ hne _ fun k hk => ?_ have hk1 : k + 1 < n + 3 := by have hkj : k < j.1 := (Finset.mem_Ico.mp hk).2 have hjm := j.2; omega rw [dif_pos hk1] exact isPosLaurentA_mon _ (by omega) /-- **THEOREM 3 — Exact Snake-Graph Perfect-Matching Count (`typeA_snake_graph_matching_count`)**: Evaluating `snakeMatchPoly n i j` at `(1, ..., 1)` (the sum of all Laurent coefficients) equals `j - i` for every `1 ≤ i < j` — the EXACT number of perfect matchings of the fan snake graph `G_{i, j}`! -/ theorem typeA_snake_graph_matching_count (n : ℕ) (i j : Fin (n + 3)) (hi : 1 ≤ i.1) (_hij : i < j) : (snakeMatchPoly n i j).coeff.sum (fun _ c => c) = ((j.1 - i.1 : ℕ) : ℤ) := by unfold snakeMatchPoly have hi0 : i.1 ≠ 0 := by omega simp only [hi0, ↓reduceIte] let aug : LaurentA n →+ ℤ := { toFun := fun g => g.coeff.sum (fun _ c => c) map_zero' := by simp [AddMonoidAlgebra.coeff_zero] map_add' := fun g₁ g₂ => by rw [AddMonoidAlgebra.coeff_add, Finsupp.sum_add_index (fun _ _ => rfl) (fun _ _ _ _ => rfl)] } change aug (∑ k ∈ Finset.Ico i.1 j.1, _) = _ rw [map_sum] have hterm : ∀ k ∈ Finset.Ico i.1 j.1, aug (if hk : k + 1 < n + 3 then laMon (fanExp n i + fanExp n j - fanExp n ⟨k, by omega⟩ - fanExp n ⟨k + 1, hk⟩) 1 else 0) = 1 := by intro k hk have hk1 : k + 1 < n + 3 := by have := (Finset.mem_Ico.mp hk).2; have := j.2; omega rw [dif_pos hk1] change (laMon _ 1).coeff.sum (fun _ c => c) = 1 unfold laMon rw [AddMonoidAlgebra.coeff_single, Finsupp.sum_single_index rfl] rw [Finset.sum_congr rfl hterm, Finset.sum_const, Nat.card_Ico, nsmul_eq_mul, mul_one] /-- Helper normalized telescoping sum `φ(i, j) = ∑_{k ∈ [i, j)} x_{0,k}⁻¹ * x_{0,k+1}⁻¹`. -/ noncomputable def fanPhi (n : ℕ) (i j : ℕ) : LaurentA n := ∑ k ∈ Finset.Ico i j, if hk : k + 1 < n + 3 then xFanInv n ⟨k, by omega⟩ * xFanInv n ⟨k + 1, hk⟩ else 0 lemma fanPhi_add (n : ℕ) {a b c : ℕ} (hab : a ≤ b) (hbc : b ≤ c) : fanPhi n a c = fanPhi n a b + fanPhi n b c := by unfold fanPhi exact (Finset.sum_Ico_consecutive _ hab hbc).symm lemma snakeMatchPoly_eq_xFan_mul_fanPhi (n : ℕ) (i j : Fin (n + 3)) (hi : 1 ≤ i.1) : snakeMatchPoly n i j = xFan n i * xFan n j * fanPhi n i.1 j.1 := by unfold snakeMatchPoly fanPhi have hi0 : i.1 ≠ 0 := by omega simp only [hi0, ↓reduceIte, Finset.mul_sum] refine Finset.sum_congr rfl fun k _ => ?_ split_ifs with hk · unfold xFan xFanInv rw [laMon_mul, laMon_mul, laMon_mul] have hexp : fanExp n i + fanExp n j + (-(fanExp n ⟨k, by omega⟩) + -(fanExp n ⟨k + 1, hk⟩)) = fanExp n i + fanExp n j - fanExp n ⟨k, by omega⟩ - fanExp n ⟨k + 1, hk⟩ := by abel rw [hexp]; simp · simp /-- **THEOREM 4 — Universal Type-`A_n` Ptolemy Cluster Exchange Theorem (`typeA_ptolemy_exchange_all`)**: For **EVERY** `n : ℕ` and **EVERY** quadruple `0 ≤ a < b < c < d ≤ n + 2`, the positive snake-graph Laurent polynomials satisfy the EXACT Ptolemy cluster exchange relation in `LaurentA n`: `L_{a, c} * L_{b, d} = L_{a, b} * L_{c, d} + L_{a, d} * L_{b, c}`! -/ theorem typeA_ptolemy_exchange_all (n : ℕ) (a b c d : Fin (n + 3)) (hab : a < b) (hbc : b < c) (hcd : c < d) : snakeMatchPoly n a c * snakeMatchPoly n b d = snakeMatchPoly n a b * snakeMatchPoly n c d + snakeMatchPoly n a d * snakeMatchPoly n b c := by have hb1 : 1 ≤ b.1 := by have : a.1 < b.1 := hab; omega have hc1 : 1 ≤ c.1 := by have : b.1 < c.1 := hbc; omega have hd1 : 1 ≤ d.1 := by have : c.1 < d.1 := hcd; omega by_cases ha0 : a.1 = 0 · have hb0 : b.1 ≠ 0 := by omega have hc0 : c.1 ≠ 0 := by omega have hd0 : d.1 ≠ 0 := by omega have hL_ab : snakeMatchPoly n a b = xFan n b := by unfold snakeMatchPoly; rw [if_pos ha0, if_neg hb0] have hL_ac : snakeMatchPoly n a c = xFan n c := by unfold snakeMatchPoly; rw [if_pos ha0, if_neg hc0] have hL_ad : snakeMatchPoly n a d = xFan n d := by unfold snakeMatchPoly; rw [if_pos ha0, if_neg hd0] rw [hL_ab, hL_ac, hL_ad, snakeMatchPoly_eq_xFan_mul_fanPhi n b d hb1, snakeMatchPoly_eq_xFan_mul_fanPhi n c d hc1, snakeMatchPoly_eq_xFan_mul_fanPhi n b c hb1, fanPhi_add n (le_of_lt hbc) (le_of_lt hcd)] ring · have ha1 : 1 ≤ a.1 := by omega rw [snakeMatchPoly_eq_xFan_mul_fanPhi n a c ha1, snakeMatchPoly_eq_xFan_mul_fanPhi n b d hb1, snakeMatchPoly_eq_xFan_mul_fanPhi n a b ha1, snakeMatchPoly_eq_xFan_mul_fanPhi n c d hc1, snakeMatchPoly_eq_xFan_mul_fanPhi n a d ha1, snakeMatchPoly_eq_xFan_mul_fanPhi n b c hb1] have h1 : fanPhi n a.1 c.1 = fanPhi n a.1 b.1 + fanPhi n b.1 c.1 := fanPhi_add n (le_of_lt hab) (le_of_lt hbc) have h2 : fanPhi n b.1 d.1 = fanPhi n b.1 c.1 + fanPhi n c.1 d.1 := fanPhi_add n (le_of_lt hbc) (le_of_lt hcd) have h3 : fanPhi n a.1 d.1 = fanPhi n a.1 b.1 + fanPhi n b.1 c.1 + fanPhi n c.1 d.1 := by rw [fanPhi_add n (le_of_lt hab) (le_of_lt (lt_trans hbc hcd)), h2, add_assoc] rw [h1, h2, h3] ring /-! ## 2. Full Binary Trees, Catalan Enumeration & Stasheff Associahedron Flip Graph -/ /-- Full binary trees (`leaf` or `node l r`). -/ inductive BinTree where | leaf : BinTree | node : BinTree → BinTree → BinTree deriving DecidableEq, Repr /-- Number of internal nodes of a full binary tree. -/ def BinTree.size : BinTree → ℕ | .leaf => 0 | .node l r => 1 + l.size + r.size /-- The subtype of full binary trees with exactly `n` internal nodes. -/ abbrev BinTreeN (n : ℕ) := {t : BinTree // t.size = n} /-- Classical Catalan numbers `C_0 = 1`, `C_{n+1} = ∑_{i=0}^n C_i * C_{n - i}`. -/ def catalanNat : ℕ → ℕ | 0 => 1 | n + 1 => ((List.finRange (n + 1)).map fun i => catalanNat i.1 * catalanNat (n - i.1)).sum /-- Explicit finite list of ALL full binary trees of size `n` (with no duplicates!). -/ def allBinTrees : ℕ → List BinTree | 0 => [.leaf] | n + 1 => (List.finRange (n + 1)).flatMap fun i => (allBinTrees i.1).flatMap fun l => (allBinTrees (n - i.1)).map fun r => BinTree.node l r lemma mem_allBinTrees_iff : ∀ (n : ℕ) (t : BinTree), t ∈ allBinTrees n ↔ t.size = n := by intro n induction n using Nat.strong_induction_on with | h n ih => intro t cases n with | zero => cases t <;> simp [allBinTrees, BinTree.size] | succ n => cases t with | leaf => simp [allBinTrees, BinTree.size] | node l r => simp only [allBinTrees, List.mem_flatMap, List.mem_map, List.mem_finRange, true_and] constructor · rintro ⟨i, l', hl', r', hr', heq⟩ injection heq with h1 h2; subst h1 h2 have hsl := (ih i.1 (by omega) l').mp hl' have hsr := (ih (n - i.1) (by omega) r').mp hr' simp [BinTree.size, hsl, hsr]; omega · intro hsz simp only [BinTree.size] at hsz refine ⟨⟨l.size, by omega⟩, l, (ih l.size (by omega) l).mpr rfl, r, (ih (n - l.size) (by omega) r).mpr (by omega), rfl⟩ lemma allBinTrees_nodup : ∀ n : ℕ, (allBinTrees n).Nodup := by intro n induction n using Nat.strong_induction_on with | h n ih => cases n with | zero => simp [allBinTrees] | succ n => unfold allBinTrees rw [List.nodup_flatMap] constructor · intro i _ rw [List.nodup_flatMap] constructor · intro l _ exact (ih (n - i.1) (by omega)).map (fun r₁ r₂ h => by injection h) · exact (ih i.1 (by omega)).imp fun {l₁ l₂} hne t ht₁ ht₂ => by rcases List.mem_map.mp ht₁ with ⟨r₁, _, rfl⟩ rcases List.mem_map.mp ht₂ with ⟨r₂, _, heq⟩ injection heq with hl _ exact hne hl.symm · exact (List.nodup_finRange (n + 1)).imp fun {i j} hij t ht₁ ht₂ => by rcases List.mem_flatMap.mp ht₁ with ⟨l₁, hl₁, ht₁'⟩ rcases List.mem_map.mp ht₁' with ⟨r₁, _, rfl⟩ rcases List.mem_flatMap.mp ht₂ with ⟨l₂, hl₂, ht₂'⟩ rcases List.mem_map.mp ht₂' with ⟨r₂, _, heq⟩ injection heq with hl _ have hs1 := (mem_allBinTrees_iff i.1 l₁).mp hl₁ have hs2 := (mem_allBinTrees_iff j.1 l₂).mp hl₂ subst hl exact hij (Fin.ext (hs1.symm.trans hs2)) lemma allBinTrees_length : ∀ n : ℕ, (allBinTrees n).length = catalanNat n := by intro n induction n using Nat.strong_induction_on with | h n ih => cases n with | zero => simp [allBinTrees, catalanNat] | succ n => unfold allBinTrees catalanNat rw [List.length_flatMap] congr 1 refine List.map_congr_left fun i _ => ?_ rw [List.length_flatMap] simp only [List.length_map, List.map_const', List.sum_replicate, smul_eq_mul, ih i.1 (by omega), ih (n - i.1) (by omega)] noncomputable instance fintypeBinTreeN (n : ℕ) : Fintype (BinTreeN n) where elems := (allBinTrees n).toFinset.attach.map ⟨fun (x : {t : BinTree // t ∈ (allBinTrees n).toFinset}) => ⟨x.1, (mem_allBinTrees_iff n x.1).mp (List.mem_toFinset.mp x.2)⟩, fun x y h => Subtype.ext (congrArg (fun z : BinTreeN n => z.1) h)⟩ complete := fun ⟨t, ht⟩ => by simp only [Finset.mem_map, Finset.mem_attach, true_and, Subtype.exists, List.mem_toFinset] exact ⟨t, (mem_allBinTrees_iff n t).mpr ht, rfl⟩ /-- **THEOREM 5 — Exact Catalan Enumeration of Full Binary Trees (`card_binTreeN_eq_catalan`)**: For EVERY `n : ℕ`, `Fintype.card (BinTreeN n) = catalanNat n` (and `C_0=1, C_1=1, C_2=2, C_3=5, C_4=14, C_5=42`)! -/ theorem card_binTreeN_eq_catalan (n : ℕ) : Fintype.card (BinTreeN n) = catalanNat n := by change ((allBinTrees n).toFinset.attach.map _).card = catalanNat n rw [Finset.card_map, Finset.card_attach, List.toFinset_card_of_nodup (allBinTrees_nodup n), allBinTrees_length] lemma catalanNat_0 : catalanNat 0 = 1 := by rw [catalanNat] lemma catalanNat_1 : catalanNat 1 = 1 := by rw [catalanNat]; simp [List.finRange, catalanNat_0] lemma catalanNat_2 : catalanNat 2 = 2 := by rw [catalanNat]; simp [List.finRange, catalanNat_0, catalanNat_1] lemma catalanNat_3 : catalanNat 3 = 5 := by rw [catalanNat]; simp [List.finRange, catalanNat_0, catalanNat_1, catalanNat_2] lemma catalanNat_4 : catalanNat 4 = 14 := by rw [catalanNat]; simp [List.finRange, catalanNat_0, catalanNat_1, catalanNat_2, catalanNat_3] lemma catalanNat_5 : catalanNat 5 = 42 := by rw [catalanNat] simp [List.finRange, catalanNat_0, catalanNat_1, catalanNat_2, catalanNat_3, catalanNat_4] theorem catalan_small_values : catalanNat 0 = 1 ∧ catalanNat 1 = 1 ∧ catalanNat 2 = 2 ∧ catalanNat 3 = 5 ∧ catalanNat 4 = 14 ∧ catalanNat 5 = 42 := ⟨catalanNat_0, catalanNat_1, catalanNat_2, catalanNat_3, catalanNat_4, catalanNat_5⟩ /-! ### 2A. Right Rotations, Left-Weight Potential & Stasheff Associahedron `(n - 1)`-Regularity -/ /-- Single Tamari right rotation `((a, b), c) ↦ (a, (b, c))` at any internal node. -/ inductive RightRot : BinTree → BinTree → Prop | root (a b c : BinTree) : RightRot (.node (.node a b) c) (.node a (.node b c)) | left {l l' : BinTree} (r : BinTree) : RightRot l l' → RightRot (.node l r) (.node l' r) | right (l : BinTree) {r r' : BinTree} : RightRot r r' → RightRot (.node l r) (.node l r') lemma rightRot_size {t₁ t₂ : BinTree} (h : RightRot t₁ t₂) : t₁.size = t₂.size := by induction h with | root a b c => simp [BinTree.size]; omega | left _ _ ih => simp [BinTree.size, ih] | right _ _ ih => simp [BinTree.size, ih] /-- Left-weight potential `lw(node l r) = l.size + lw(l) + lw(r)`: strictly decreases on EVERY right rotation! -/ def leftWeight : BinTree → ℕ | .leaf => 0 | .node l r => l.size + leftWeight l + leftWeight r /-- **THEOREM 6A — Strict Monotonicity of Left-Weight under Right Rotation**: -/ theorem leftWeight_lt_of_rightRot {t₁ t₂ : BinTree} (h : RightRot t₁ t₂) : leftWeight t₂ < leftWeight t₁ := by induction h with | root a b c => simp only [leftWeight, BinTree.size] omega | left r h' ih => have hsz := rightRot_size h' simp only [leftWeight, hsz] omega | right l _ ih => simp only [leftWeight] omega /-- Undirected flip adjacency in the Stasheff associahedron `K_{n+2}`. -/ def FlipAdj (t₁ t₂ : BinTree) : Prop := RightRot t₁ t₂ ∨ RightRot t₂ t₁ /-- Explicit list of all right-rotation neighbors of a binary tree. -/ def rightRotList : BinTree → List BinTree | .leaf => [] | .node l r => (match l with | .leaf => [] | .node a b => [.node a (.node b r)]) ++ (rightRotList l).map (fun l' => .node l' r) ++ (rightRotList r).map (fun r' => .node l r') /-- Explicit list of all left-rotation neighbors of a binary tree. -/ def leftRotList : BinTree → List BinTree | .leaf => [] | .node l r => (match r with | .leaf => [] | .node b c => [.node (.node l b) c]) ++ (leftRotList l).map (fun l' => .node l' r) ++ (leftRotList r).map (fun r' => .node l r') lemma mem_rightRotList_iff (t u : BinTree) : u ∈ rightRotList t ↔ RightRot t u := by induction t generalizing u with | leaf => simp only [rightRotList, List.not_mem_nil, false_iff] intro h; cases h | node l r ihl ihr => simp only [rightRotList, List.mem_append, List.mem_map] constructor · rintro ((hroot | ⟨l', hl', rfl⟩) | ⟨r', hr', rfl⟩) · cases l with | leaf => simp at hroot | node a b => simp only [List.mem_singleton] at hroot subst hroot; exact RightRot.root a b r · exact RightRot.left r ((ihl l').mp hl') · exact RightRot.right l ((ihr r').mp hr') · intro h cases h with | root a b c => left; left; simp | left _ hl => left; right; exact ⟨_, (ihl _).mpr hl, rfl⟩ | right _ hr => right; exact ⟨_, (ihr _).mpr hr, rfl⟩ lemma mem_leftRotList_iff (t u : BinTree) : u ∈ leftRotList t ↔ RightRot u t := by induction t generalizing u with | leaf => simp only [leftRotList, List.not_mem_nil, false_iff] intro h; cases h | node l r ihl ihr => simp only [leftRotList, List.mem_append, List.mem_map] constructor · rintro ((hroot | ⟨l', hl', rfl⟩) | ⟨r', hr', rfl⟩) · cases r with | leaf => simp at hroot | node b c => simp only [List.mem_singleton] at hroot subst hroot; exact RightRot.root l b c · exact RightRot.left r ((ihl l').mp hl') · exact RightRot.right l ((ihr r').mp hr') · intro h cases h with | root a b c => left; left; simp | left _ hl => left; right; exact ⟨_, (ihl _).mpr hl, rfl⟩ | right _ hr => right; exact ⟨_, (ihr _).mpr hr, rfl⟩ lemma rightRotList_nodup (t : BinTree) : (rightRotList t).Nodup := by induction t with | leaf => simp [rightRotList] | node l r ihl ihr => unfold rightRotList rw [List.nodup_append, List.nodup_append] refine ⟨⟨?_, ihl.map (fun _ _ h => by injection h), ?_⟩, ihr.map (fun _ _ h => by injection h), ?_⟩ · cases l <;> simp · intro u hu₁ v hv₂ heq subst heq cases l with | leaf => simp at hu₁ | node a b => simp only [List.mem_singleton] at hu₁; subst hu₁ rcases List.mem_map.mp hv₂ with ⟨l', _, heq⟩ injection heq with _ hr have hsz := congrArg BinTree.size hr simp [BinTree.size] at hsz · intro u hu₁ v hv₂ heq subst heq rcases List.mem_map.mp hv₂ with ⟨r', hr', rfl⟩ rcases List.mem_append.mp hu₁ with hroot | hleft · cases l with | leaf => simp at hroot | node a b => simp only [List.mem_singleton] at hroot injection hroot with hl _ have hsz := congrArg BinTree.size hl simp [BinTree.size] at hsz; omega · rcases List.mem_map.mp hleft with ⟨l', hl', heq⟩ injection heq with hl hr have hlt := leftWeight_lt_of_rightRot ((mem_rightRotList_iff l l').mp hl') rw [hl] at hlt exact lt_irrefl _ hlt lemma leftRotList_nodup (t : BinTree) : (leftRotList t).Nodup := by induction t with | leaf => simp [leftRotList] | node l r ihl ihr => unfold leftRotList rw [List.nodup_append, List.nodup_append] refine ⟨⟨?_, ihl.map (fun _ _ h => by injection h), ?_⟩, ihr.map (fun _ _ h => by injection h), ?_⟩ · cases r <;> simp · intro u hu₁ v hv₂ heq subst heq cases r with | leaf => simp at hu₁ | node b c => simp only [List.mem_singleton] at hu₁; subst hu₁ rcases List.mem_map.mp hv₂ with ⟨l', _, heq⟩ injection heq with _ hr have hsz := congrArg BinTree.size hr simp [BinTree.size] at hsz · intro u hu₁ v hv₂ heq subst heq rcases List.mem_map.mp hv₂ with ⟨r', hr', rfl⟩ rcases List.mem_append.mp hu₁ with hroot | hleft · cases r with | leaf => simp at hroot | node b c => simp only [List.mem_singleton] at hroot injection hroot with hl _ have hsz := congrArg BinTree.size hl simp [BinTree.size] at hsz; omega · rcases List.mem_map.mp hleft with ⟨l', hl', heq⟩ injection heq with hl hr have hlt := leftWeight_lt_of_rightRot ((mem_leftRotList_iff l l').mp hl') rw [hl] at hlt exact lt_irrefl _ hlt lemma rot_count_sum (t : BinTree) (ht : 1 ≤ t.size) : (rightRotList t).length + (leftRotList t).length = t.size - 1 := by induction t with | leaf => simp [BinTree.size] at ht | node l r ihl ihr => simp only [rightRotList, leftRotList, List.length_append, List.length_map, BinTree.size] cases l with | leaf => cases r with | leaf => simp [rightRotList, leftRotList, BinTree.size] | node c d => have hr := ihr (by simp [BinTree.size]; omega) simp only [rightRotList, leftRotList, BinTree.size, List.length_nil, List.length_singleton] at hr ⊢ omega | node a b => have hl := ihl (by simp [BinTree.size]; omega) cases r with | leaf => simp only [rightRotList, leftRotList, BinTree.size, List.length_nil, List.length_singleton] at hl ⊢ omega | node c d => have hr := ihr (by simp [BinTree.size]; omega) simp only [BinTree.size, List.length_singleton] at hl hr ⊢ omega /-- **THEOREM 6B — Exact `(n - 1)`-Regularity of Every Stasheff Associahedron `K_{n+2}` (`associahedron_degree_exact`)**: For EVERY `n ≥ 1` and EVERY full binary tree `t` with `n` internal nodes, the finite set of all associahedron flip neighbors `{u | FlipAdj t u}` has cardinality **EXACTLY `n - 1`**! -/ theorem associahedron_degree_exact {n : ℕ} (hn : 1 ≤ n) (t : BinTree) (ht : t.size = n) : ((rightRotList t ++ leftRotList t).toFinset).card = n - 1 ∧ (∀ u : BinTree, u ∈ (rightRotList t ++ leftRotList t).toFinset ↔ FlipAdj t u) := by have hnodup : (rightRotList t ++ leftRotList t).Nodup := by rw [List.nodup_append] refine ⟨rightRotList_nodup t, leftRotList_nodup t, fun u hu₁ v hv₂ heq => ?_⟩ subst heq have h1 := leftWeight_lt_of_rightRot ((mem_rightRotList_iff t u).mp hu₁) have h2 := leftWeight_lt_of_rightRot ((mem_leftRotList_iff t u).mp hv₂) omega constructor · rw [List.toFinset_card_of_nodup hnodup, List.length_append, rot_count_sum t (by omega), ht] · intro u simp only [List.mem_toFinset, List.mem_append, mem_rightRotList_iff, mem_leftRotList_iff, FlipAdj] /-! ## 3. Tamari Lattice, Huang–Tamari Bracket Vectors & Weak-Bruhat Quotient Theorem -/ /-- The Tamari lattice order on full binary trees: reflexive-transitive closure of `RightRot`. -/ def TamariLE (t₁ t₂ : BinTree) : Prop := Relation.ReflTransGen RightRot t₁ t₂ lemma tamariLE_size {t₁ t₂ : BinTree} (h : TamariLE t₁ t₂) : t₁.size = t₂.size := by induction h with | refl => rfl | tail _ hstep ih => exact ih.trans (rightRot_size hstep) lemma leftWeight_le_of_tamari {t₁ t₂ : BinTree} (h : TamariLE t₁ t₂) : leftWeight t₂ ≤ leftWeight t₁ := by induction h with | refl => exact le_rfl | tail _ hstep ih => exact le_trans (le_of_lt (leftWeight_lt_of_rightRot hstep)) ih /-- **THEOREM 7 — Antisymmetry of the Tamari Partial Order (`tamari_antisymm`)**: -/ theorem tamari_antisymm {t₁ t₂ : BinTree} (h12 : TamariLE t₁ t₂) (h21 : TamariLE t₂ t₁) : t₁ = t₂ := by cases h12 with | refl => rfl | tail h1m hstep => have hlt := leftWeight_lt_of_rightRot hstep have hle1 := leftWeight_le_of_tamari h1m have hle2 := leftWeight_le_of_tamari h21 omega /-- The right comb (unique maximum) and left comb (unique minimum) of size `n`. -/ def rightComb : ℕ → BinTree | 0 => .leaf | n + 1 => .node .leaf (rightComb n) def leftComb : ℕ → BinTree | 0 => .leaf | n + 1 => .node (leftComb n) .leaf lemma rightComb_size (n : ℕ) : (rightComb n).size = n := by induction n with | zero => rfl | succ n ih => simp [rightComb, BinTree.size, ih]; omega lemma leftComb_size (n : ℕ) : (leftComb n).size = n := by induction n with | zero => rfl | succ n ih => simp [leftComb, BinTree.size, ih]; omega /-- Left-right mirror involution on binary trees. -/ def binMirror : BinTree → BinTree | .leaf => .leaf | .node l r => .node (binMirror r) (binMirror l) lemma binMirror_inv (u : BinTree) : binMirror (binMirror u) = u := by induction u with | leaf => rfl | node l r ihl ihr => simp [binMirror, ihl, ihr] lemma binMirror_size (u : BinTree) : (binMirror u).size = u.size := by induction u with | leaf => rfl | node l r ihl ihr => simp [binMirror, BinTree.size, ihl, ihr]; omega lemma binMirror_rightComb (m : ℕ) : binMirror (rightComb m) = leftComb m := by induction m with | zero => rfl | succ m ih => simp [rightComb, leftComb, binMirror, ih] lemma binMirror_rightRot {u v : BinTree} (h : RightRot u v) : RightRot (binMirror v) (binMirror u) := by induction h with | root a b c => exact RightRot.root (binMirror c) (binMirror b) (binMirror a) | left r _ ih => exact RightRot.right (binMirror r) ih | right l _ ih => exact RightRot.left (binMirror l) ih lemma binMirror_tamari {u v : BinTree} (h : TamariLE u v) : TamariLE (binMirror v) (binMirror u) := by induction h with | refl => exact Relation.ReflTransGen.refl | tail _ hstep ih => exact Relation.ReflTransGen.head (binMirror_rightRot hstep) ih /-- Exact flip distance in right rotations. -/ def TamariDist (k : ℕ) (t₁ t₂ : BinTree) : Prop := match k with | 0 => t₁ = t₂ | k + 1 => ∃ tm, RightRot t₁ tm ∧ TamariDist k tm t₂ lemma tamariDist_implies_tamariLE {k : ℕ} {t₁ t₂ : BinTree} (h : TamariDist k t₁ t₂) : TamariLE t₁ t₂ := by induction k generalizing t₁ with | zero => subst h; exact Relation.ReflTransGen.refl | succ k ih => rcases h with ⟨tm, hr, hd⟩ exact Relation.ReflTransGen.head hr (ih hd) /-- Right spine length of a binary tree (`0` for `leaf`, `1 + rightSpineLen r` for `node l r`). -/ def rightSpineLen : BinTree → ℕ | .leaf => 0 | .node _ r => 1 + rightSpineLen r lemma rightSpineLen_le_size (t : BinTree) : rightSpineLen t ≤ t.size := by induction t with | leaf => exact le_rfl | node l r _ ihr => simp [rightSpineLen, BinTree.size]; omega /-- **THEOREM 8 — Exact Right-Comb Reachability, Unique Top `rightComb n` & Unique Bottom `leftComb n`**: Every binary tree `t` of size `n` reaches `rightComb n` in EXACTLY `n - rightSpineLen t` right rotations (`tamari_dist_to_rightComb`), proving `t ≤_Tam rightComb n` (`tamari_le_rightComb`) and `leftComb n ≤_Tam t` (`leftComb_tamari_le`)! -/ theorem tamari_dist_to_rightComb (t : BinTree) : TamariDist (t.size - rightSpineLen t) t (rightComb t.size) := by generalize hk : t.size - rightSpineLen t = k induction k generalizing t with | zero => have hrc : ∀ u : BinTree, u.size ≤ rightSpineLen u → u = rightComb u.size := by intro u hu induction u with | leaf => rfl | node l r _ ihr => simp only [BinTree.size, rightSpineLen] at hu ⊢ have hl0 : l.size = 0 := by have := rightSpineLen_le_size r; omega have hl : l = .leaf := by cases l <;> simp [BinTree.size] at hl0 ⊢ subst hl have hr_eq := ihr (by omega) simp only [BinTree.size, add_zero] rw [show 1 + r.size = r.size + 1 by omega, rightComb, ← hr_eq] exact hrc t (by have := rightSpineLen_le_size t; omega) | succ k ih => have hstep : ∀ u : BinTree, rightSpineLen u < u.size → ∃ u', RightRot u u' ∧ rightSpineLen u' = rightSpineLen u + 1 := by intro u hu induction u with | leaf => simp [BinTree.size, rightSpineLen] at hu | node l r _ ihr => cases l with | leaf => simp only [BinTree.size, rightSpineLen] at hu rcases ihr (by omega) with ⟨r', hr', hsl⟩ exact ⟨.node .leaf r', RightRot.right .leaf hr', by simp [rightSpineLen, hsl]; omega⟩ | node a b => exact ⟨.node a (.node b r), RightRot.root a b r, by simp [rightSpineLen]; omega⟩ rcases hstep t (by omega) with ⟨t', hrot, hsl⟩ have hsz := rightRot_size hrot refine ⟨t', hrot, ?_⟩ rw [hsz] exact ih t' (by omega) theorem tamari_le_rightComb (t : BinTree) : TamariLE t (rightComb t.size) := tamariDist_implies_tamariLE (tamari_dist_to_rightComb t) theorem leftComb_tamari_le (t : BinTree) : TamariLE (leftComb t.size) t := by have h := binMirror_tamari (tamari_le_rightComb (binMirror t)) rw [binMirror_size, binMirror_rightComb, binMirror_inv] at h exact h /-! ### 3A. Huang–Tamari Bracket-Vector Characterization & Full Lattice Structure -/ /-- The Huang–Tamari bracket vector `rVec t : List ℕ` of length `t.size`: for each inorder internal node `i`, `rVec t[i]` is `1 + (right subtree size at node i)`. -/ def rVec : BinTree → List ℕ | .leaf => [] | .node l r => rVec l ++ [r.size + 1] ++ rVec r lemma rVec_length (t : BinTree) : (rVec t).length = t.size := by induction t with | leaf => rfl | node l r ihl ihr => simp [rVec, BinTree.size, ihl, ihr]; omega lemma rVec_node_get_lt (l r : BinTree) (k : ℕ) (hk : k < l.size) : (rVec (.node l r))[k]'(by rw [rVec_length]; simp [BinTree.size]; omega) = (rVec l)[k]'(by rw [rVec_length]; exact hk) := by have h1 : rVec (.node l r) = rVec l ++ [r.size + 1] ++ rVec r := rfl simp only [h1] rw [List.getElem_append_left (by simp [rVec_length]; omega), List.getElem_append_left (by rw [rVec_length]; exact hk)] lemma rVec_node_get_eq (l r : BinTree) : (rVec (.node l r))[l.size]'(by rw [rVec_length]; simp [BinTree.size]; omega) = r.size + 1 := by have h1 : rVec (.node l r) = rVec l ++ [r.size + 1] ++ rVec r := rfl simp only [h1] rw [List.getElem_append_left (by simp [rVec_length]), List.getElem_append_right (by rw [rVec_length])] simp [rVec_length] lemma rVec_node_get_gt (l r : BinTree) (k : ℕ) (hk1 : l.size < k) (hk2 : k < (BinTree.node l r).size) : (rVec (.node l r))[k]'(by rw [rVec_length]; exact hk2) = (rVec r)[k - (l.size + 1)]'(by rw [rVec_length]; simp [BinTree.size] at hk2; omega) := by have h1 : rVec (.node l r) = rVec l ++ [r.size + 1] ++ rVec r := rfl simp only [h1] rw [List.getElem_append_right (by simp [rVec_length]; omega)] simp only [List.length_append, List.length_singleton, rVec_length] /-- A list `v : List ℕ` is a *Tamari bracket vector* iff: 1. `1 ≤ v[i]` and `i + v[i] ≤ v.length` for all `i < v.length`; 2. Nesting (non-crossing): if `i < j` and `j < i + v[i]`, then `j + v[j] ≤ i + v[i]`. -/ def IsTamariVec (v : List ℕ) : Prop := (∀ i (hi : i < v.length), 1 ≤ v[i] ∧ i + v[i] ≤ v.length) ∧ (∀ i j (hi : i < v.length) (hj : j < v.length), i < j → j < i + v[i] → j + v[j] ≤ i + v[i]) lemma rVec_bound (t : BinTree) (i : ℕ) (hi : i < t.size) : 1 ≤ (rVec t)[i]'(by rw [rVec_length]; exact hi) ∧ i + (rVec t)[i]'(by rw [rVec_length]; exact hi) ≤ t.size := by induction t generalizing i with | leaf => simp [BinTree.size] at hi | node l r ihl ihr => by_cases h1 : i < l.size · rw [rVec_node_get_lt l r i h1] have := ihl i h1; simp [BinTree.size]; omega · by_cases h2 : i = l.size · subst h2; rw [rVec_node_get_eq l r]; simp [BinTree.size]; omega · rw [rVec_node_get_gt l r i (by omega) hi] have := ihr (i - (l.size + 1)) (by simp [BinTree.size] at hi; omega) simp [BinTree.size]; omega lemma rVec_isTamariVec (t : BinTree) : IsTamariVec (rVec t) := by induction t with | leaf => exact ⟨fun i hi => by simp [rVec] at hi, fun i _ hi _ _ _ => by simp [rVec] at hi⟩ | node l r ihl ihr => constructor · intro i hi have hsz : i < (BinTree.node l r).size := by rwa [← rVec_length] simpa [rVec_length] using rVec_bound (.node l r) i hsz · intro i j hi hj hij hji have hisz : i < (BinTree.node l r).size := by rwa [← rVec_length] have hjsz : j < (BinTree.node l r).size := by rwa [← rVec_length] by_cases hi1 : i < l.size · rw [rVec_node_get_lt l r i hi1] at hji ⊢ have hbi := rVec_bound l i hi1 have hj1 : j < l.size := by omega rw [rVec_node_get_lt l r j hj1] exact ihl.2 i j (by rw [rVec_length]; exact hi1) (by rw [rVec_length]; exact hj1) hij hji · by_cases hi2 : i = l.size · subst hi2 rw [rVec_node_get_eq l r] at hji ⊢ rw [rVec_node_get_gt l r j hij hjsz] have hbj := rVec_bound r (j - (l.size + 1)) (by simp [BinTree.size] at hjsz; omega) omega · have hi_gt : l.size < i := by omega have hj_gt : l.size < j := by omega rw [rVec_node_get_gt l r i hi_gt hisz] at hji ⊢ rw [rVec_node_get_gt l r j hj_gt hjsz] have h := ihr.2 (i - (l.size + 1)) (j - (l.size + 1)) (by rw [rVec_length]; simp [BinTree.size] at hisz; omega) (by rw [rVec_length]; simp [BinTree.size] at hjsz; omega) (by omega) (by omega) omega /-- Every `BinTree` is uniquely determined by its bracket vector `rVec` (`rVec_injective`)! -/ theorem rVec_injective : Function.Injective rVec := by intro t₁ induction t₁ with | leaf => intro t₂ h have hsz : t₂.size = 0 := by rw [← rVec_length, ← h]; rfl cases t₂ <;> simp [BinTree.size] at hsz ⊢ | node l₁ r₁ ihl ihr => intro t₂ h cases t₂ with | leaf => have := congrArg List.length h simp [rVec, rVec_length] at this | node l₂ r₂ => have hsz_eq : (BinTree.node l₁ r₁).size = (BinTree.node l₂ r₂).size := by rw [← rVec_length, ← rVec_length, h] have hl_eq : l₁.size = l₂.size := by by_contra hne rcases lt_or_gt_of_ne hne with hlt | hgt · have h1 := rVec_node_get_eq l₁ r₁ have h2 := rVec_node_get_lt l₂ r₂ l₁.size hlt have hb := rVec_bound l₂ l₁.size hlt have hget : (rVec (.node l₁ r₁))[l₁.size]'(by rw [rVec_length]; simp [BinTree.size]; omega) = (rVec (.node l₂ r₂))[l₁.size]'(by rw [rVec_length]; simp [BinTree.size] at hsz_eq ⊢; omega) := by congr 1 rw [h1, h2] at hget simp [BinTree.size] at hsz_eq; omega · have h1 := rVec_node_get_eq l₂ r₂ have h2 := rVec_node_get_lt l₁ r₁ l₂.size hgt have hb := rVec_bound l₁ l₂.size hgt have hget : (rVec (.node l₁ r₁))[l₂.size]'(by rw [rVec_length]; simp [BinTree.size]; omega) = (rVec (.node l₂ r₂))[l₂.size]'(by rw [rVec_length]; simp [BinTree.size] at hsz_eq ⊢; omega) := by congr 1 rw [h1, h2] at hget simp [BinTree.size] at hsz_eq; omega have hl_vec : rVec l₁ = rVec l₂ := by have ht := congrArg (List.take l₁.size) h have e1 : rVec (.node l₁ r₁) = rVec l₁ ++ ([r₁.size + 1] ++ rVec r₁) := by simp [rVec, List.append_assoc] have e2 : rVec (.node l₂ r₂) = rVec l₂ ++ ([r₂.size + 1] ++ rVec r₂) := by simp [rVec, List.append_assoc] rw [e1, e2, List.take_left' (rVec_length l₁), List.take_left' (by rw [rVec_length, hl_eq])] at ht exact ht have hr_vec : rVec r₁ = rVec r₂ := by have hd := congrArg (List.drop (l₁.size + 1)) h have e1 : rVec (.node l₁ r₁) = (rVec l₁ ++ [r₁.size + 1]) ++ rVec r₁ := rfl have e2 : rVec (.node l₂ r₂) = (rVec l₂ ++ [r₂.size + 1]) ++ rVec r₂ := rfl rw [e1, e2, List.drop_left' (by simp [rVec_length]), List.drop_left' (by simp [rVec_length, hl_eq])] at hd exact hd rw [ihl hl_vec, ihr hr_vec] /-- Every Tamari bracket vector `v` is realized by a `BinTree` (`exists_tree_of_isTamariVec`)! -/ theorem exists_tree_of_isTamariVec (v : List ℕ) (hv : IsTamariVec v) : ∃ t : BinTree, rVec t = v := by generalize hn : v.length = n induction n using Nat.strong_induction_on generalizing v with | h n ih => cases n with | zero => have hv0 : v = [] := List.eq_nil_of_length_eq_zero hn exact ⟨.leaf, by rw [hv0]; rfl⟩ | succ m => have hex : ∃ p : ℕ, ∃ hp : p < v.length, p + v[p] = v.length := by refine ⟨m, by omega, ?_⟩ have := hv.1 m (by omega); omega classical let p := Nat.find hex have hp_spec : ∃ hp : p < v.length, p + v[p] = v.length := Nat.find_spec hex rcases hp_spec with ⟨hp_lt, hp_eq⟩ have hp_min : ∀ k (hk : k < p), k + v[k]'(by omega) < v.length := by intro k hk have hnot : ¬ (∃ h : k < v.length, k + v[k] = v.length) := Nat.find_min hex hk have hle := (hv.1 k (by omega)).2 by_contra hge exact hnot ⟨by omega, by omega⟩ have hleft_bound : ∀ k (hk : k < p), k + v[k]'(by omega) ≤ p := by intro k hk by_contra hgt have hnest := hv.2 k p (by omega) hp_lt hk (by omega) have hlt := hp_min k hk omega let vL := v.take p let vR := v.drop (p + 1) have hvL_len : vL.length = p := List.length_take_of_le (by omega) have hvR_len : vR.length = m - p := by simp [vR, hn] have hvL_tam : IsTamariVec vL := by constructor · intro i hi rw [hvL_len] at hi simp only [vL, List.getElem_take, hvL_len] exact ⟨(hv.1 i (by omega)).1, hleft_bound i hi⟩ · intro i j hi hj hij hji rw [hvL_len] at hi hj simp only [vL, List.getElem_take] at hji ⊢ exact hv.2 i j (by omega) (by omega) hij hji have hvR_tam : IsTamariVec vR := by constructor · intro i hi rw [hvR_len] at hi simp only [vR, List.getElem_drop, hvR_len] have h1 := hv.1 (p + 1 + i) (by omega) omega · intro i j hi hj hij hji rw [hvR_len] at hi hj simp only [vR, List.getElem_drop] at hji ⊢ have h2 := hv.2 (p + 1 + i) (p + 1 + j) (by omega) (by omega) (by omega) (by omega) omega rcases ih p (by omega) vL hvL_tam hvL_len with ⟨tL, htL⟩ rcases ih (m - p) (by omega) vR hvR_tam hvR_len with ⟨tR, htR⟩ refine ⟨.node tL tR, ?_⟩ have hrn : rVec (.node tL tR) = rVec tL ++ [tR.size + 1] ++ rVec tR := rfl rw [hrn, htL, htR] have htR_sz : tR.size = m - p := by rw [← rVec_length, htR, hvR_len] have hvp : v[p]'hp_lt = tR.size + 1 := by omega rw [← hvp] apply List.ext_getElem · simp [vL, vR, hn] · intro k hk1 hk2 by_cases h1 : k < p · rw [List.getElem_append_left (by simp [hvL_len]; omega), List.getElem_append_left (by rw [hvL_len]; exact h1)] simp [vL] · by_cases h2 : k = p · rw [List.getElem_append_left (by simp [hvL_len]; omega), List.getElem_append_right (by rw [hvL_len]; omega)] simp [hvL_len, h2] · rw [List.getElem_append_right (by simp [hvL_len]; omega)] simp [vR, hvL_len, show p + 1 + (k - (p + 1)) = k by omega] /-- Componentwise order on bracket vectors: `rVecLE t₁ t₂`. -/ def rVecLE (t₁ t₂ : BinTree) : Prop := t₁.size = t₂.size ∧ ∀ i (hi1 : i < t₁.size) (hi2 : i < t₂.size), (rVec t₁)[i]'(by rw [rVec_length]; exact hi1) ≤ (rVec t₂)[i]'(by rw [rVec_length]; exact hi2) lemma rVec_root_rot_get (a b c : BinTree) (i : ℕ) (hi : i < (BinTree.node (.node a b) c).size) : (i = a.size ∧ (rVec (.node (.node a b) c))[i]'(by rw [rVec_length]; exact hi) = b.size + 1 ∧ (rVec (.node a (.node b c)))[i]'(by rw [rVec_length]; simp [BinTree.size] at hi ⊢; omega) = b.size + c.size + 2) ∨ (i ≠ a.size ∧ (rVec (.node a (.node b c)))[i]'(by rw [rVec_length]; simp [BinTree.size] at hi ⊢; omega) = (rVec (.node (.node a b) c))[i]'(by rw [rVec_length]; exact hi)) := by by_cases h1 : i < a.size · right; refine ⟨by omega, ?_⟩ rw [rVec_node_get_lt a (.node b c) i h1, rVec_node_get_lt (.node a b) c i (by simp [BinTree.size]; omega), rVec_node_get_lt a b i h1] · by_cases h2 : i = a.size · left; subst h2; refine ⟨rfl, ?_, ?_⟩ · rw [rVec_node_get_lt (.node a b) c a.size (by simp [BinTree.size]; omega), rVec_node_get_eq a b] · rw [rVec_node_get_eq a (.node b c)]; simp [BinTree.size]; omega · right; refine ⟨h2, ?_⟩ rw [rVec_node_get_gt a (.node b c) i (by omega) (by simp [BinTree.size] at hi ⊢; omega)] by_cases h3 : i < (BinTree.node a b).size · rw [rVec_node_get_lt (.node a b) c i h3, rVec_node_get_gt a b i (by omega) h3, rVec_node_get_lt b c (i - (a.size + 1)) (by simp [BinTree.size] at h3; omega)] · by_cases h4 : i = (BinTree.node a b).size · subst h4 rw [rVec_node_get_eq (.node a b) c] have heq : (BinTree.node a b).size - (a.size + 1) = b.size := by simp [BinTree.size]; omega simp only [heq, rVec_node_get_eq b c] · rw [rVec_node_get_gt (.node a b) c i (by omega) hi, rVec_node_get_gt b c (i - (a.size + 1)) (by simp [BinTree.size] at h3 h4; omega) (by simp [BinTree.size] at hi ⊢; omega)] congr 1; simp [BinTree.size]; omega lemma rVecLE_of_rightRot {t₁ t₂ : BinTree} (h : RightRot t₁ t₂) : rVecLE t₁ t₂ := by have hsz := rightRot_size h refine ⟨hsz, ?_⟩ induction h with | root a b c => intro i hi1 _ rcases rVec_root_rot_get a b c i hi1 with ⟨_, h1, h2⟩ | ⟨_, heq⟩ · rw [h1, h2]; omega · rw [heq] | @left l l' r hl ih => intro i hi1 hi2 have hsl := rightRot_size hl by_cases h1 : i < l.size · rw [rVec_node_get_lt l r i h1, rVec_node_get_lt l' r i (by omega)] exact ih hsl i h1 (by omega) · by_cases h2 : i = l.size · have hL : (rVec (.node l r))[i]'(by rw [rVec_length]; exact hi1) = r.size + 1 := by simpa only [h2] using rVec_node_get_eq l r have hR : (rVec (.node l' r))[i]'(by rw [rVec_length]; exact hi2) = r.size + 1 := by have hi' : i = l'.size := by omega simpa only [hi'] using rVec_node_get_eq l' r rw [hL, hR] · rw [rVec_node_get_gt l r i (by omega) hi1, rVec_node_get_gt l' r i (by omega) hi2] simp only [hsl, le_rfl] | @right l r r' hr ih => intro i hi1 hi2 have hsr := rightRot_size hr by_cases h1 : i < l.size · rw [rVec_node_get_lt l r i h1, rVec_node_get_lt l r' i h1] · by_cases h2 : i = l.size · subst h2; rw [rVec_node_get_eq, rVec_node_get_eq]; omega · rw [rVec_node_get_gt l r i (by omega) hi1, rVec_node_get_gt l r' i (by omega) hi2] exact ih hsr (i - (l.size + 1)) (by simp [BinTree.size] at hi1; omega) (by simp [BinTree.size] at hi2; omega) /-- Key converse step for Huang–Tamari: if `rVec t₁ ≤ rVec t₂` componentwise and `t₁ ≠ t₂`, there exists a SINGLE right rotation `RightRot t₁ t'` with `rVec t' ≤ rVec t₂`! -/ lemma exists_rightRot_of_rVecLE_ne {t₁ t₂ : BinTree} (hle : rVecLE t₁ t₂) (hne : t₁ ≠ t₂) : ∃ t', RightRot t₁ t' ∧ rVecLE t' t₂ := by induction t₁ generalizing t₂ with | leaf => cases t₂ · exact absurd rfl hne · have := hle.1; simp [BinTree.size] at this; omega | node l₁ r₁ ihl ihr => rcases hle with ⟨hsz, hcomp⟩ cases t₂ with | leaf => simp [BinTree.size] at hsz | node l₂ r₂ => have hl_ge : l₂.size ≤ l₁.size := by by_contra hlt have hle_at := hcomp l₁.size (by simp [BinTree.size]; omega) (by simp [BinTree.size] at hsz ⊢; omega) rw [rVec_node_get_eq l₁ r₁, rVec_node_get_lt l₂ r₂ l₁.size (by omega)] at hle_at have hb := rVec_bound l₂ l₁.size (by omega) simp [BinTree.size] at hsz; omega by_cases hleq : l₁.size = l₂.size · have hreq : r₁.size = r₂.size := by simp [BinTree.size] at hsz; omega have hl_le : rVecLE l₁ l₂ := by refine ⟨hleq, fun i hi1 hi2 => ?_⟩ have h := hcomp i (by simp [BinTree.size]; omega) (by simp [BinTree.size]; omega) rwa [rVec_node_get_lt l₁ r₁ i hi1, rVec_node_get_lt l₂ r₂ i hi2] at h have hr_le : rVecLE r₁ r₂ := by refine ⟨hreq, fun i hi1 hi2 => ?_⟩ have h := hcomp (l₁.size + 1 + i) (by simp [BinTree.size]; omega) (by simp [BinTree.size]; omega) rw [rVec_node_get_gt l₁ r₁ (l₁.size + 1 + i) (by omega) (by simp [BinTree.size]; omega), rVec_node_get_gt l₂ r₂ (l₁.size + 1 + i) (by omega) (by simp [BinTree.size]; omega)] at h simpa [hleq] using h by_cases hll : l₁ = l₂ · subst hll have hrr : r₁ ≠ r₂ := fun h => hne (by rw [h]) rcases ihr hr_le hrr with ⟨r', hrot, hle'⟩ have hsr' := rightRot_size hrot refine ⟨.node l₁ r', RightRot.right l₁ hrot, by simp [BinTree.size, ← hsr', hreq], fun i hi1 hi2 => ?_⟩ by_cases h1 : i < l₁.size · rw [rVec_node_get_lt l₁ r' i h1, rVec_node_get_lt l₁ r₂ i h1] · by_cases h2 : i = l₁.size · subst h2; rw [rVec_node_get_eq, rVec_node_get_eq]; omega · rw [rVec_node_get_gt l₁ r' i (by omega) hi1, rVec_node_get_gt l₁ r₂ i (by omega) hi2] exact hle'.2 (i - (l₁.size + 1)) (by simp [BinTree.size] at hi1; omega) (by simp [BinTree.size] at hi2; omega) · rcases ihl hl_le hll with ⟨l', hrot, hle'⟩ have hsl' := rightRot_size hrot refine ⟨.node l' r₁, RightRot.left r₁ hrot, by simp [BinTree.size, ← hsl', hleq, hreq], fun i hi1 hi2 => ?_⟩ by_cases h1 : i < l'.size · rw [rVec_node_get_lt l' r₁ i h1, rVec_node_get_lt l₂ r₂ i (by omega)] exact hle'.2 i h1 (by omega) · by_cases h2 : i = l'.size · have hL : (rVec (.node l' r₁))[i]'(by rw [rVec_length]; exact hi1) = r₁.size + 1 := by simpa only [h2] using rVec_node_get_eq l' r₁ have hR : (rVec (.node l₂ r₂))[i]'(by rw [rVec_length]; exact hi2) = r₂.size + 1 := by have h2' : i = l₂.size := by omega simpa only [h2'] using rVec_node_get_eq l₂ r₂ rw [hL, hR]; omega · rw [rVec_node_get_gt l' r₁ i (by omega) hi1, rVec_node_get_gt l₂ r₂ i (by omega) hi2] have := hr_le.2 (i - (l'.size + 1)) (by simp [BinTree.size] at hi1; omega) (by simp [BinTree.size] at hi2; omega) simpa [← hsl', hleq] using this · cases l₁ with | leaf => simp [BinTree.size] at hl_ge hleq; omega | node a b => have ha_ge : l₂.size ≤ a.size := by by_contra hlt have hle_at := hcomp a.size (by simp [BinTree.size]; omega) (by simp [BinTree.size] at hsz ⊢; omega) rw [rVec_node_get_lt (.node a b) r₁ a.size (by simp [BinTree.size]; omega), rVec_node_get_eq a b, rVec_node_get_lt l₂ r₂ a.size (by omega)] at hle_at have hb := rVec_bound l₂ a.size (by omega) simp [BinTree.size] at hl_ge hleq; omega by_cases haeq : a.size = l₂.size · refine ⟨.node a (.node b r₁), RightRot.root a b r₁, by rw [← rightRot_size (RightRot.root a b r₁)]; exact hsz, fun i hi1 hi2 => ?_⟩ rcases rVec_root_rot_get a b r₁ i (by rw [rightRot_size (RightRot.root a b r₁)]; exact hi1) with ⟨rfl, _, hR⟩ | ⟨_, heq⟩ · rw [hR] have h2 : (rVec (.node l₂ r₂))[a.size]'(by rw [rVec_length]; exact hi2) = r₂.size + 1 := by simpa only [haeq] using rVec_node_get_eq l₂ r₂ rw [h2]; simp [BinTree.size] at hsz; omega · rw [heq] exact hcomp i (by rw [rightRot_size (RightRot.root a b r₁)]; exact hi1) hi2 · set m := (BinTree.node a b).size have hm_lt : m < (BinTree.node l₂ r₂).size := by have h1 : (BinTree.node (.node a b) r₁).size = 1 + m + r₁.size := rfl omega let vT : List ℕ := (List.range m).map fun k => if hk : k < (BinTree.node l₂ r₂).size then min ((rVec (.node l₂ r₂))[k]'(by rw [rVec_length]; exact hk)) (m - k) else 0 have hvT_len : vT.length = m := by simp [vT] have hvT_get : ∀ k (hk : k < m), vT[k]'(by omega) = min ((rVec (.node l₂ r₂))[k]'(by rw [rVec_length]; omega)) (m - k) := by intro k hk simp only [vT, List.getElem_map, List.getElem_range] rw [dif_pos (by omega)] have hvT_tam : IsTamariVec vT := by constructor · intro i hi rw [hvT_len] at hi rw [hvT_get i hi, hvT_len] have h1 := rVec_bound (.node l₂ r₂) i (by omega) omega · intro i j hi hj hij hji rw [hvT_len] at hi hj rw [hvT_get i hi] at hji rw [hvT_get i hi, hvT_get j hj] have hnest := (rVec_isTamariVec (.node l₂ r₂)).2 i j (by rw [rVec_length]; omega) (by rw [rVec_length]; omega) hij (by omega) omega rcases exists_tree_of_isTamariVec vT hvT_tam with ⟨tT, htT⟩ have htT_sz : tT.size = m := by rw [← rVec_length, htT, hvT_len] have hle_T : rVecLE (.node a b) tT := by refine ⟨htT_sz.symm, fun i hi1 hi2 => ?_⟩ have hi_m : i < m := hi1 have hi_ab : i < (BinTree.node (.node a b) r₁).size := by have : (BinTree.node (.node a b) r₁).size = 1 + m + r₁.size := rfl omega have hle1 := hcomp i hi_ab (by omega) rw [rVec_node_get_lt (.node a b) r₁ i hi1] at hle1 have hbnd := rVec_bound (.node a b) i hi1 have hT : (rVec tT)[i]'(by rw [rVec_length]; exact hi2) = vT[i]'(by omega) := by simp only [htT] rw [hT, hvT_get i hi_m] omega have hne_T : BinTree.node a b ≠ tT := by intro heq; subst heq have hba := rVec_bound a l₂.size (by omega) have hl2_m : l₂.size < m := by simp [m, BinTree.size]; omega have hT : (rVec (.node a b))[l₂.size]'(by rw [rVec_length]; exact hl2_m) = vT[l₂.size]'(by omega) := by simp only [htT] rw [rVec_node_get_lt a b l₂.size (by omega), hvT_get l₂.size hl2_m, rVec_node_get_eq l₂ r₂] at hT simp [BinTree.size, m] at hsz hT ⊢; omega rcases ihl hle_T hne_T with ⟨l', hrot, hle'⟩ have hsl' := rightRot_size hrot have hsz_out : (BinTree.node l' r₁).size = (BinTree.node l₂ r₂).size := by have h1 : (BinTree.node (.node a b) r₁).size = 1 + (BinTree.node a b).size + r₁.size := rfl have h2 : (BinTree.node l' r₁).size = 1 + l'.size + r₁.size := rfl omega refine ⟨.node l' r₁, RightRot.left r₁ hrot, hsz_out, fun i hi1 hi2 => ?_⟩ by_cases h1 : i < l'.size · rw [rVec_node_get_lt l' r₁ i h1] have hleT := hle'.2 i h1 (by omega) have hT : (rVec tT)[i]'(by rw [rVec_length]; omega) = vT[i]'(by omega) := by simp only [htT] rw [hT, hvT_get i (by omega)] at hleT exact le_trans hleT (min_le_left _ _) · have hi_orig : i < (BinTree.node (.node a b) r₁).size := by omega have hsame : (rVec (.node l' r₁))[i]'(by rw [rVec_length]; exact hi1) = (rVec (.node (.node a b) r₁))[i]'(by rw [rVec_length]; exact hi_orig) := by by_cases h2 : i = l'.size · have hL : (rVec (.node l' r₁))[i]'(by rw [rVec_length]; exact hi1) = r₁.size + 1 := by simpa only [h2] using rVec_node_get_eq l' r₁ have hR : (rVec (.node (.node a b) r₁))[i]'(by rw [rVec_length]; exact hi_orig) = r₁.size + 1 := by have h2' : i = (BinTree.node a b).size := by omega simpa only [h2'] using rVec_node_get_eq (.node a b) r₁ rw [hL, hR] · rw [rVec_node_get_gt l' r₁ i (by omega) hi1, rVec_node_get_gt (.node a b) r₁ i (by omega) hi_orig] simp only [← hsl'] rw [hsame] exact hcomp i hi_orig hi2 /-- **THEOREM 9 — Huang–Tamari Bracket-Vector Equivalence (`tamari_iff_rVec_le`)**: For **EVERY** pair of full binary trees `t₁, t₂`, `TamariLE t₁ t₂` holds **IF AND ONLY IF** `rVecLE t₁ t₂` (same size and componentwise `rVec t₁ ≤ rVec t₂`)! -/ theorem tamari_iff_rVec_le (t₁ t₂ : BinTree) : TamariLE t₁ t₂ ↔ rVecLE t₁ t₂ := by constructor · intro h induction h with | refl => exact ⟨rfl, fun _ _ _ => le_rfl⟩ | @tail u v _ hstep ih => have hs := rVecLE_of_rightRot hstep refine ⟨ih.1.trans hs.1, fun i hi1 hi2 => ?_⟩ have hiu : i < u.size := by rw [← ih.1]; exact hi1 exact le_trans (ih.2 i hi1 hiu) (hs.2 i hiu hi2) · intro hle generalize hw : leftWeight t₁ = w induction w using Nat.strong_induction_on generalizing t₁ with | h w ih => by_cases heq : t₁ = t₂ · subst heq; exact Relation.ReflTransGen.refl · rcases exists_rightRot_of_rVecLE_ne hle heq with ⟨t', hrot, hle'⟩ have hlt := leftWeight_lt_of_rightRot hrot exact Relation.ReflTransGen.head hrot (ih (leftWeight t') (by omega) t' hle' rfl) /-- Componentwise minimum of two Tamari bracket vectors is ALWAYS a Tamari bracket vector! -/ lemma min_isTamariVec {v₁ v₂ : List ℕ} (h1 : IsTamariVec v₁) (h2 : IsTamariVec v₂) (hlen : v₁.length = v₂.length) : IsTamariVec (List.zipWith min v₁ v₂) := by have hl : (List.zipWith min v₁ v₂).length = v₁.length := by simp [hlen] constructor · intro i hi rw [hl] at hi rw [List.getElem_zipWith, hl] have a := h1.1 i hi have b := h2.1 i (by omega) omega · intro i j hi hj hij hji rw [hl] at hi hj rw [List.getElem_zipWith] at hji rw [List.getElem_zipWith, List.getElem_zipWith] have a := h1.2 i j hi hj hij (by omega) have b := h2.2 i j (by omega) (by omega) hij (by omega) omega lemma tamariMeet_exists {n : ℕ} (t₁ t₂ : BinTreeN n) : ∃ t : BinTree, rVec t = List.zipWith min (rVec t₁.1) (rVec t₂.1) := exists_tree_of_isTamariVec _ (min_isTamariVec (rVec_isTamariVec t₁.1) (rVec_isTamariVec t₂.1) (by rw [rVec_length, rVec_length, t₁.2, t₂.2])) /-- Explicit Tamari meet of two trees of the same size `n`. -/ noncomputable def tamariMeet {n : ℕ} (t₁ t₂ : BinTreeN n) : BinTreeN n := ⟨Classical.choose (tamariMeet_exists t₁ t₂), by rw [← rVec_length, Classical.choose_spec (tamariMeet_exists t₁ t₂)] simp [rVec_length, t₁.2, t₂.2]⟩ lemma rVec_tamariMeet {n : ℕ} (t₁ t₂ : BinTreeN n) : rVec (tamariMeet t₁ t₂).1 = List.zipWith min (rVec t₁.1) (rVec t₂.1) := Classical.choose_spec (tamariMeet_exists t₁ t₂) /-- **THEOREM 10A — Exact Tamari Lattice Meet (`tamari_meet_spec`)**: For EVERY `n : ℕ` and EVERY `t₁, t₂, u : BinTreeN n`, `TamariLE u.1 (tamariMeet t₁ t₂).1 ↔ TamariLE u.1 t₁.1 ∧ TamariLE u.1 t₂.1`! -/ theorem tamari_meet_spec {n : ℕ} (t₁ t₂ u : BinTreeN n) : TamariLE u.1 (tamariMeet t₁ t₂).1 ↔ TamariLE u.1 t₁.1 ∧ TamariLE u.1 t₂.1 := by rw [tamari_iff_rVec_le, tamari_iff_rVec_le, tamari_iff_rVec_le] have hvec := rVec_tamariMeet t₁ t₂ have hsz1 : u.1.size = (tamariMeet t₁ t₂).1.size := u.2.trans (tamariMeet t₁ t₂).2.symm have hsz2 : u.1.size = t₁.1.size := u.2.trans t₁.2.symm have hsz3 : u.1.size = t₂.1.size := u.2.trans t₂.2.symm have hget : ∀ i (hi : i < (tamariMeet t₁ t₂).1.size), (rVec (tamariMeet t₁ t₂).1)[i]'(by rw [rVec_length]; exact hi) = min ((rVec t₁.1)[i]'(by rw [rVec_length]; omega)) ((rVec t₂.1)[i]'(by rw [rVec_length]; omega)) := by intro i hi simp only [hvec, List.getElem_zipWith] constructor · intro ⟨_, h⟩ refine ⟨⟨hsz2, fun i hi1 hi2 => ?_⟩, ⟨hsz3, fun i hi1 hi2 => ?_⟩⟩ · have hm := h i hi1 (by omega); rw [hget i (by omega)] at hm; exact le_trans hm (min_le_left _ _) · have hm := h i hi1 (by omega); rw [hget i (by omega)] at hm; exact le_trans hm (min_le_right _ _) · rintro ⟨⟨_, h1⟩, ⟨_, h2⟩⟩ refine ⟨hsz1, fun i hi1 hi2 => ?_⟩ rw [hget i hi2] exact le_min (h1 i hi1 (by omega)) (h2 i hi1 (by omega)) /-- Explicit Tamari join of two trees `t₁, t₂ : BinTreeN n` (meet of all common upper bounds in the finite lattice with top element `rightComb n`)! -/ noncomputable def tamariJoin {n : ℕ} (t₁ t₂ : BinTreeN n) : BinTreeN n := letI : DecidablePred (fun t : BinTreeN n => TamariLE t₁.1 t.1 ∧ TamariLE t₂.1 t.1) := Classical.decPred _ let U : List (BinTreeN n) := (allBinTrees n).attach.filterMap fun x => let t : BinTreeN n := ⟨x.1, (mem_allBinTrees_iff n x.1).mp x.2⟩ if TamariLE t₁.1 t.1 ∧ TamariLE t₂.1 t.1 then some t else none U.foldl tamariMeet ⟨rightComb n, rightComb_size n⟩ /-- **THEOREM 10B — Exact Tamari Lattice Join (`tamari_join_spec`)**: For EVERY `n : ℕ` and EVERY `t₁, t₂, u : BinTreeN n`, `TamariLE (tamariJoin t₁ t₂).1 u.1 ↔ TamariLE t₁.1 u.1 ∧ TamariLE t₂.1 u.1`! -/ theorem tamari_join_spec {n : ℕ} (t₁ t₂ u : BinTreeN n) : TamariLE (tamariJoin t₁ t₂).1 u.1 ↔ TamariLE t₁.1 u.1 ∧ TamariLE t₂.1 u.1 := by have hfold_lb : ∀ (L : List (BinTreeN n)) (init x : BinTreeN n), TamariLE x.1 (L.foldl tamariMeet init).1 ↔ TamariLE x.1 init.1 ∧ ∀ y ∈ L, TamariLE x.1 y.1 := by intro L induction L with | nil => intro init x; simp | cons a L ih => intro init x rw [List.foldl_cons, ih, tamari_meet_spec] simp [and_assoc] have hfold_le_mem : ∀ (L : List (BinTreeN n)) (init y : BinTreeN n), y ∈ L → TamariLE (L.foldl tamariMeet init).1 y.1 := by intro L init y hy exact ((hfold_lb L init (L.foldl tamariMeet init)).mp Relation.ReflTransGen.refl).2 y hy let _inst : DecidablePred (fun t : BinTreeN n => TamariLE t₁.1 t.1 ∧ TamariLE t₂.1 t.1) := Classical.decPred _ set U : List (BinTreeN n) := (allBinTrees n).attach.filterMap fun x => let t : BinTreeN n := ⟨x.1, (mem_allBinTrees_iff n x.1).mp x.2⟩ if TamariLE t₁.1 t.1 ∧ TamariLE t₂.1 t.1 then some t else none have hmemU : ∀ y : BinTreeN n, y ∈ U ↔ TamariLE t₁.1 y.1 ∧ TamariLE t₂.1 y.1 := by intro y simp only [U, List.mem_filterMap, List.mem_attach, true_and, Subtype.exists] constructor · rintro ⟨x, hx, hif⟩ split_ifs at hif with hcond · simp only [Option.some.injEq] at hif rw [← hif]; exact hcond · intro hy refine ⟨y.1, (mem_allBinTrees_iff n y.1).mpr y.2, ?_⟩ rw [if_pos hy] have htop1 : TamariLE t₁.1 (rightComb n) := by have h := tamari_le_rightComb t₁.1; rwa [t₁.2] at h have htop2 : TamariLE t₂.1 (rightComb n) := by have h := tamari_le_rightComb t₂.1; rwa [t₂.2] at h have hj1 : TamariLE t₁.1 (tamariJoin t₁ t₂).1 := by change TamariLE t₁.1 (U.foldl tamariMeet ⟨rightComb n, rightComb_size n⟩).1 rw [hfold_lb] exact ⟨htop1, fun y hy => ((hmemU y).mp hy).1⟩ have hj2 : TamariLE t₂.1 (tamariJoin t₁ t₂).1 := by change TamariLE t₂.1 (U.foldl tamariMeet ⟨rightComb n, rightComb_size n⟩).1 rw [hfold_lb] exact ⟨htop2, fun y hy => ((hmemU y).mp hy).2⟩ constructor · intro h exact ⟨Relation.ReflTransGen.trans hj1 h, Relation.ReflTransGen.trans hj2 h⟩ · intro hu exact hfold_le_mem U ⟨rightComb n, rightComb_size n⟩ u ((hmemU u).mpr hu) /-! ### 3B. Loday–Ronco / Björner–Wachs Weak-Bruhat to Tamari Quotient Theorem -/ /-- Labeled binary search tree (for right-to-left BST insertion of permutations). -/ inductive BST where | empty : BST | node : ℕ → BST → BST → BST deriving DecidableEq def bstInsert (x : ℕ) : BST → BST | .empty => .node x .empty .empty | .node v l r => if x < v then .node v (bstInsert x l) r else .node v l (bstInsert x r) def bstShape : BST → BinTree | .empty => .leaf | .node _ l r => .node (bstShape l) (bstShape r) lemma bstShape_insert_size (x : ℕ) (t : BST) : (bstShape (bstInsert x t)).size = (bstShape t).size + 1 := by induction t with | empty => rfl | node v l r ihl ihr => unfold bstInsert split_ifs <;> simp [bstShape, BinTree.size, ihl, ihr] <;> omega /-- Right-to-left BST insertion of a list of natural numbers. -/ def listToBST : List ℕ → BST | [] => .empty | x :: xs => bstInsert x (listToBST xs) lemma listToBST_size (xs : List ℕ) : (bstShape (listToBST xs)).size = xs.length := by induction xs with | nil => rfl | cons x xs ih => simp [listToBST, bstShape_insert_size, ih] /-- Labeled BST right rotation preserving search-tree ordering (`a < b`). -/ inductive BSTRightRot : BST → BST → Prop | root (a b : ℕ) (hab : a < b) (l m r : BST) : BSTRightRot (.node b (.node a l m) r) (.node a l (.node b m r)) | left (v : ℕ) {l l' : BST} (r : BST) : BSTRightRot l l' → BSTRightRot (.node v l r) (.node v l' r) | right (v : ℕ) (l : BST) {r r' : BST} : BSTRightRot r r' → BSTRightRot (.node v l r) (.node v l r') lemma bstRightRot_shape {t₁ t₂ : BST} (h : BSTRightRot t₁ t₂) : RightRot (bstShape t₁) (bstShape t₂) := by induction h with | root a b _ l m r => exact RightRot.root (bstShape l) (bstShape m) (bstShape r) | left _ _ _ ih => exact RightRot.left _ ih | right _ _ _ ih => exact RightRot.right _ ih lemma bstInsert_preserves_rightRot (z : ℕ) {t₁ t₂ : BST} (h : BSTRightRot t₁ t₂) : BSTRightRot (bstInsert z t₁) (bstInsert z t₂) := by induction h with | root a b hab l m r => by_cases hza : z < a · have hzb : z < b := lt_trans hza hab simp only [bstInsert, hza, hzb, ↓reduceIte] exact BSTRightRot.root a b hab (bstInsert z l) m r · by_cases hzb : z < b · simp only [bstInsert, hza, hzb, ↓reduceIte] exact BSTRightRot.root a b hab l (bstInsert z m) r · simp only [bstInsert, hza, hzb, ↓reduceIte] exact BSTRightRot.root a b hab l m (bstInsert z r) | left v r hl ih => unfold bstInsert split_ifs · exact BSTRightRot.left v r ih · exact BSTRightRot.left v (bstInsert z r) hl | right v l hr ih => unfold bstInsert split_ifs · exact BSTRightRot.right v (bstInsert z l) hr · exact BSTRightRot.right v l ih lemma bstInsert_swap_le {a b : ℕ} (hab : a < b) (t : BST) : bstInsert a (bstInsert b t) = bstInsert b (bstInsert a t) ∨ BSTRightRot (bstInsert a (bstInsert b t)) (bstInsert b (bstInsert a t)) := by induction t with | empty => right have hba : ¬ (b < a) := not_lt_of_gt hab simp only [bstInsert, hab, hba, ↓reduceIte] exact BSTRightRot.root a b hab .empty .empty .empty | node v l r ihl ihr => by_cases hbv : b < v · have hav : a < v := lt_trans hab hbv simp only [bstInsert, hav, hbv, ↓reduceIte] rcases ihl with heq | hrot · left; rw [heq] · right; exact BSTRightRot.left v r hrot · by_cases hav : a < v · left; simp only [bstInsert, hav, hbv, ↓reduceIte] · simp only [bstInsert, hav, hbv, ↓reduceIte] rcases ihr with heq | hrot · left; rw [heq] · right; exact BSTRightRot.right v l hrot /-- Canonical Loday–Ronco / Björner–Wachs map `permToBinTree : Equiv.Perm (Fin n) → BinTreeN n` defined by right-to-left BST insertion of `[w 0, w 1, ..., w (n - 1)]`. -/ def permToBinTree {n : ℕ} (w : Equiv.Perm (Fin n)) : BinTreeN n := ⟨bstShape (listToBST ((List.finRange n).map fun i => (w i).1)), by rw [listToBST_size, List.length_map, List.length_finRange]⟩ /-- Right Weak Bruhat cover relation on `S_n`: `v = u * s_i` with `u i < u (i + 1)`. -/ def RightWeakStep {n : ℕ} (u v : Equiv.Perm (Fin n)) : Prop := ∃ i : ℕ, ∃ hi : i + 1 < n, u ⟨i, by omega⟩ < u ⟨i + 1, hi⟩ ∧ v = u * Equiv.swap ⟨i, by omega⟩ ⟨i + 1, hi⟩ /-- Right Weak Bruhat order on `S_n` as reflexive-transitive closure of `RightWeakStep`. -/ def RightWeakOrder {n : ℕ} (u v : Equiv.Perm (Fin n)) : Prop := Relation.ReflTransGen RightWeakStep u v /-- **THEOREM 11A — Loday–Ronco / Björner–Wachs Monotonicity (`weakBruhat_implies_tamari`)**: For **EVERY** `n : ℕ` and **EVERY** `u, v : Equiv.Perm (Fin n)`, Right Weak Bruhat Order maps order-preservingly onto the Tamari Lattice: `RightWeakOrder u v → TamariLE (permToBinTree u).1 (permToBinTree v).1`! -/ theorem weakBruhat_implies_tamari {n : ℕ} {u v : Equiv.Perm (Fin n)} (h : RightWeakOrder u v) : TamariLE (permToBinTree u).1 (permToBinTree v).1 := by have hlist_swap : ∀ (pre suf : List ℕ) (a b : ℕ), a < b → TamariLE (bstShape (listToBST (pre ++ a :: b :: suf))) (bstShape (listToBST (pre ++ b :: a :: suf))) := by intro pre suf a b hab have hbase := bstInsert_swap_le hab (listToBST suf) have hpre : ∀ (p : List ℕ) {t₁ t₂ : BST}, (t₁ = t₂ ∨ BSTRightRot t₁ t₂) → (p.foldr bstInsert t₁ = p.foldr bstInsert t₂ ∨ BSTRightRot (p.foldr bstInsert t₁) (p.foldr bstInsert t₂)) := by intro p; induction p with | nil => intro _ _ h; exact h | cons z zs ih => intro t₁ t₂ h rcases ih h with heq | hrot · left; simp [heq] · right; exact bstInsert_preserves_rightRot z hrot have hfold : ∀ (p q : List ℕ), listToBST (p ++ q) = p.foldr bstInsert (listToBST q) := by intro p q; induction p with | nil => rfl | cons z zs ih => simp [listToBST, ih] rw [hfold, hfold] simp only [listToBST] rcases hpre pre hbase with heq | hrot · rw [heq]; exact Relation.ReflTransGen.refl · exact Relation.ReflTransGen.single (bstRightRot_shape hrot) induction h with | refl => exact Relation.ReflTransGen.refl | @tail u' v' _ hstep ih => apply Relation.ReflTransGen.trans ih rcases hstep with ⟨i, hi, hlt, rfl⟩ let i0 : Fin n := ⟨i, by omega⟩ let i1 : Fin n := ⟨i + 1, hi⟩ set lu := (List.finRange n).map fun j => (u' j).1 set lv := (List.finRange n).map fun j => ((u' * Equiv.swap i0 i1) j).1 have hsplit_u : lu = lu.take i ++ (u' i0).1 :: (u' i1).1 :: lu.drop (i + 2) := by have hlen : lu.length = n := by simp [lu] apply List.ext_getElem · simp [hlen]; omega · intro k hk1 hk2 by_cases h1 : k < i · rw [List.getElem_append_left (by simp [hlen]; omega), List.getElem_take] · rw [List.getElem_append_right (by simp [hlen]; omega)] simp only [List.length_take, hlen, min_eq_left (by omega : i ≤ n)] by_cases h2 : k = i · subst h2; simp [lu, i0] · by_cases h3 : k = i + 1 · subst h3; simp [lu, i1, show i + 1 - i = 1 by omega] · have hk2' : k - i = (k - (i + 2)) + 2 := by omega simp only [hk2', List.getElem_cons_succ, List.getElem_drop, show i + 2 + (k - (i + 2)) = k by omega] have hsplit_v : lv = lu.take i ++ (u' i1).1 :: (u' i0).1 :: lu.drop (i + 2) := by have hlen_u : lu.length = n := by simp [lu] have hlen_v : lv.length = n := by simp [lv] apply List.ext_getElem · simp [hlen_u, hlen_v]; omega · intro k hk1 hk2 by_cases h1 : k < i · rw [List.getElem_append_left (by simp [hlen_u]; omega), List.getElem_take] simp only [lu, lv, List.getElem_map, List.getElem_finRange, Fin.cast_mk, Equiv.Perm.mul_apply] have hne1 : (⟨k, by omega⟩ : Fin n) ≠ i0 := by intro h; simp [i0] at h; omega have hne2 : (⟨k, by omega⟩ : Fin n) ≠ i1 := by intro h; simp [i1] at h; omega rw [Equiv.swap_apply_of_ne_of_ne hne1 hne2] · rw [List.getElem_append_right (by simp [hlen_u]; omega)] simp only [List.length_take, hlen_u, min_eq_left (by omega : i ≤ n)] by_cases h2 : k = i · subst h2 simp only [lv, List.getElem_map, List.getElem_finRange, Fin.cast_mk, Equiv.Perm.mul_apply, Nat.sub_self, List.getElem_cons_zero] change (u' (Equiv.swap i0 i1 i0)).1 = (u' i1).1 rw [Equiv.swap_apply_left] · by_cases h3 : k = i + 1 · subst h3 simp only [lv, List.getElem_map, List.getElem_finRange, Fin.cast_mk, Equiv.Perm.mul_apply, show i + 1 - i = 1 by omega, List.getElem_cons_succ, List.getElem_cons_zero] change (u' (Equiv.swap i0 i1 i1)).1 = (u' i0).1 rw [Equiv.swap_apply_right] · have hk2' : k - i = (k - (i + 2)) + 2 := by omega simp only [hk2', List.getElem_cons_succ, List.getElem_drop, show i + 2 + (k - (i + 2)) = k by omega, lu, lv, List.getElem_map, List.getElem_finRange, Fin.cast_mk, Equiv.Perm.mul_apply] have hne1 : (⟨k, by omega⟩ : Fin n) ≠ i0 := by intro h; simp [i0] at h; omega have hne2 : (⟨k, by omega⟩ : Fin n) ≠ i1 := by intro h; simp [i1] at h; omega rw [Equiv.swap_apply_of_ne_of_ne hne1 hne2] change TamariLE (bstShape (listToBST lu)) (bstShape (listToBST lv)) rw [hsplit_u, hsplit_v] exact hlist_swap (lu.take i) (lu.drop (i + 2)) _ _ hlt /-- Postorder labeling of a binary tree with consecutive integers `[base, base + t.size)`. -/ def postOrder (base : ℕ) : BinTree → List ℕ | .leaf => [] | .node l r => postOrder base l ++ postOrder (base + l.size + 1) r ++ [base + l.size] lemma postOrder_length (base : ℕ) (t : BinTree) : (postOrder base t).length = t.size := by induction t generalizing base with | leaf => rfl | node l r ihl ihr => simp [postOrder, BinTree.size, ihl, ihr]; omega lemma postOrder_mem (base : ℕ) (t : BinTree) (x : ℕ) : x ∈ postOrder base t ↔ base ≤ x ∧ x < base + t.size := by induction t generalizing base with | leaf => simp [postOrder, BinTree.size] | node l r ihl ihr => simp only [postOrder, List.mem_append, List.mem_singleton, ihl, ihr, BinTree.size] omega lemma postOrder_nodup (base : ℕ) (t : BinTree) : (postOrder base t).Nodup := by induction t generalizing base with | leaf => simp [postOrder] | node l r ihl ihr => unfold postOrder rw [List.nodup_append, List.nodup_append] refine ⟨⟨ihl base, ihr (base + l.size + 1), fun x hx1 y hy2 heq => ?_⟩, List.nodup_singleton _, fun x hx1 y hy2 heq => ?_⟩ · subst heq have h1 := (postOrder_mem _ _ _).mp hx1 have h2 := (postOrder_mem _ _ _).mp hy2; omega · subst heq simp only [List.mem_singleton] at hy2; subst hy2 rcases List.mem_append.mp hx1 with h1 | h2 · have := (postOrder_mem _ _ _).mp h1; omega · have := (postOrder_mem _ _ _).mp h2; omega /-- **THEOREM 11B — Surjectivity of `permToBinTree` (`permToBinTree_surjective`)**: For EVERY `n : ℕ` and EVERY full binary tree `t : BinTreeN n`, there exists a permutation `w : Equiv.Perm (Fin n)` with `permToBinTree w = t`! -/ theorem permToBinTree_surjective (n : ℕ) : Function.Surjective (@permToBinTree n) := by have hfold_left : ∀ (xs : List ℕ) (v : ℕ) (l r : BST), (∀ x ∈ xs, x < v) → xs.foldr bstInsert (.node v l r) = .node v (xs.foldr bstInsert l) r := by intro xs v l r hlt induction xs with | nil => rfl | cons x xs ih => simp only [List.foldr_cons] rw [ih (fun y hy => hlt y (List.mem_cons_of_mem x hy))] conv_lhs => unfold bstInsert rw [if_pos (hlt x (List.mem_cons_self ..))] have hfold_right : ∀ (xs : List ℕ) (v : ℕ) (l r : BST), (∀ x ∈ xs, v ≤ x) → xs.foldr bstInsert (.node v l r) = .node v l (xs.foldr bstInsert r) := by intro xs v l r hge induction xs with | nil => rfl | cons x xs ih => simp only [List.foldr_cons] rw [ih (fun y hy => hge y (List.mem_cons_of_mem x hy))] conv_lhs => unfold bstInsert rw [if_neg (not_lt_of_ge (hge x (List.mem_cons_self ..)))] have hlist_fold : ∀ p q, listToBST (p ++ q) = p.foldr bstInsert (listToBST q) := by intro p q; induction p with | nil => rfl | cons x xs ih => simp [listToBST, ih] have hpost_shape : ∀ base t, bstShape (listToBST (postOrder base t)) = t := by intro base t; induction t generalizing base with | leaf => rfl | node l r ihl ihr => have hp : postOrder base (.node l r) = postOrder base l ++ (postOrder (base + l.size + 1) r ++ [base + l.size]) := by simp [postOrder, List.append_assoc] rw [hp, hlist_fold, hlist_fold] change bstShape ((postOrder base l).foldr bstInsert ((postOrder (base + l.size + 1) r).foldr bstInsert (bstInsert (base + l.size) .empty))) = .node l r simp only [bstInsert] rw [hfold_right _ _ _ _ (fun x hx => by have := (postOrder_mem _ _ _).mp hx; omega)] rw [hfold_left _ _ _ _ (fun x hx => by have := (postOrder_mem _ _ _).mp hx; omega)] have hl_eq : (postOrder base l).foldr bstInsert .empty = listToBST (postOrder base l) := by simpa [listToBST] using (hlist_fold (postOrder base l) []).symm have hr_eq : (postOrder (base + l.size + 1) r).foldr bstInsert .empty = listToBST (postOrder (base + l.size + 1) r) := by simpa [listToBST] using (hlist_fold (postOrder (base + l.size + 1) r) []).symm rw [hl_eq, hr_eq, bstShape, ihl, ihr] intro ⟨t, ht⟩ let xs := postOrder 0 t have hlen : xs.length = n := by rw [postOrder_length, ht] have hnodup : xs.Nodup := postOrder_nodup 0 t let f : Fin n → Fin n := fun i => ⟨xs[i.1]'(by omega), by have hmem : xs[i.1]'(by omega) ∈ xs := List.getElem_mem _ have := (postOrder_mem 0 t _).mp hmem; omega⟩ have hfinj : Function.Injective f := by intro i j hij have heq : xs[i.1]'(by omega) = xs[j.1]'(by omega) := congrArg Fin.val hij have hidx : (⟨i.1, by omega⟩ : Fin xs.length) = ⟨j.1, by omega⟩ := (List.nodup_iff_injective_getElem.mp hnodup) heq exact Fin.ext (by simpa using congrArg Fin.val hidx) let w : Equiv.Perm (Fin n) := Equiv.ofBijective f (Finite.injective_iff_bijective.mp hfinj) refine ⟨w, Subtype.ext ?_⟩ change bstShape (listToBST ((List.finRange n).map fun i => (w i).1)) = t have hmap_eq : ((List.finRange n).map fun i => (w i).1) = xs := by apply List.ext_getElem · simp [hlen] · intro k hk1 hk2; simp [w, f] rw [hmap_eq] exact hpost_shape 0 t /-- **THEOREM 11C — Strict Contraction of Weak Bruhat Order onto Tamari Lattice for `n = 3` (`tamari_strict_quotient_of_weak`)**: `|S_3| = 6 > 5 = C_3`, with explicit distinct permutations `u = [0, 2, 1]` and `v = [2, 0, 1]` mapping to the EXACT SAME Tamari binary tree `node (node leaf leaf) (node leaf leaf)`! -/ theorem tamari_strict_quotient_of_weak : Fintype.card (Equiv.Perm (Fin 3)) = 6 ∧ Fintype.card (BinTreeN 3) = 5 ∧ ∃ u v : Equiv.Perm (Fin 3), u ≠ v ∧ permToBinTree u = permToBinTree v := by refine ⟨by decide, by rw [card_binTreeN_eq_catalan, catalanNat_3], ?_⟩ let u : Equiv.Perm (Fin 3) := Equiv.swap 1 2 let v : Equiv.Perm (Fin 3) := Equiv.swap 0 1 * Equiv.swap 0 2 refine ⟨u, v, by decide, ?_⟩ apply Subtype.ext decide /-! ## 4. Axiom Verification -/ #print axioms fanDiags_card #print axioms fanDiags_pairwise_noncrossing #print axioms fanDiags_maximal #print axioms ptolemy_matrix_realization #print axioms snakeMatchPoly_boundary_zero #print axioms snakeMatchPoly_boundary_adj #print axioms typeA_snake_graph_positivity #print axioms typeA_snake_graph_matching_count #print axioms typeA_ptolemy_exchange_all #print axioms card_binTreeN_eq_catalan #print axioms catalan_small_values #print axioms leftWeight_lt_of_rightRot #print axioms associahedron_degree_exact #print axioms tamari_antisymm #print axioms tamari_dist_to_rightComb #print axioms tamari_le_rightComb #print axioms leftComb_tamari_le #print axioms rVec_injective #print axioms exists_tree_of_isTamariVec #print axioms tamari_iff_rVec_le #print axioms tamari_meet_spec #print axioms tamari_join_spec #print axioms weakBruhat_implies_tamari #print axioms permToBinTree_surjective #print axioms tamari_strict_quotient_of_weak end TypeAPolygonPtolemyAndTamari