import Mathlib.Tacticnamespace DecayProductsdef OneColor {V L : Type*} (c : V → V → L) (p : L) (s : Finset V) : Prop :=∀ x ∈ s, ∀ y ∈ s, x ≠ y → c x y = pdef BoundedCliques {V L : Type*} (c : V → V → L) (k : ℕ) : Prop :=∀ p s, OneColor c p s → s.card ≤ kdef lexSame {U V L : Type*} [DecidableEq U] (c : U → U → L) (d : V → V → L)(x y : U × V) : L := if x.1 = y.1 then d x.2 y.2 else c x.1 y.1def lexApart {U V L H : Type*} [DecidableEq U] (c : U → U → L) (d : V → V → H)(x y : U × V) : L ⊕ H := if x.1 = y.1 then Sum.inr (d x.2 y.2) else Sum.inl (c x.1 y.1)section Finitevariable {U V L H : Type*} [DecidableEq U] [DecidableEq V] [Fintype V]def fiber (s : Finset (U × V)) (u : U) : Finset V := Finset.univ.filter fun v => (u,v) ∈ stheorem count_fibers (s : Finset (U × V)) (p q : ℕ)(first : (s.image Prod.fst).card ≤ p)(rest : ∀ u ∈ s.image Prod.fst, (fiber s u).card ≤ q) : s.card ≤ p*q := byclassicalcalcs.card ≤ ((s.image Prod.fst).sigma (fiber s)).card := byapply Finset.card_le_card_of_injOn (fun z => (⟨z.1,z.2⟩ : Σ _ : U, V))· intro z hzexact Finset.mem_sigma.mpr ⟨Finset.mem_image_of_mem _ hz,by simpa [fiber] using hz⟩· intro a _ b _ heqexact congrArg (fun v : Σ _ : U, V => (v.1,v.2)) heq_ = ∑ u ∈ s.image Prod.fst, (fiber s u).card := Finset.card_sigma _ __ ≤ ∑ _u ∈ s.image Prod.fst, q := Finset.sum_le_sum rest_ ≤ p*q := by simpa using Nat.mul_le_mul_right q firsttheorem lex_same_clique (c : U → U → L) (d : V → V → L) {p q : ℕ}(hc : BoundedCliques c p) (hd : BoundedCliques d q) :BoundedCliques (lexSame c d) (p*q) := byintro col s hmonoapply count_fibers s p q· apply hc colintro u hu v hv neobtain ⟨x,hx,rfl⟩ := Finset.mem_image.mp huobtain ⟨y,hy,rfl⟩ := Finset.mem_image.mp hvhave neq : x ≠ y := fun eq => ne (congrArg Prod.fst eq)simpa [lexSame,ne] using hmono x hx y hy neq· intro u _apply hd colintro v hv w hw nehave hx : (u,v) ∈ s := by simpa [fiber] using hvhave hy : (u,w) ∈ s := by simpa [fiber] using hwsimpa [lexSame] using hmono (u,v) hx (u,w) hy (fun eq => ne (congrArg Prod.snd eq))theorem lex_apart_clique (c : U → U → L) (d : V → V → H) {p q : ℕ}(hc : BoundedCliques c p) (hd : BoundedCliques d q) :BoundedCliques (lexApart c d) (max p q) := byintro col s hmonocases col with| inl col =>apply le_trans _ (le_max_left p q)rw [← Nat.mul_one p]apply count_fibers· apply hc colintro u hu v hv neobtain ⟨x,hx,rfl⟩ := Finset.mem_image.mp huobtain ⟨y,hy,rfl⟩ := Finset.mem_image.mp hvhave neq : x ≠ y := fun eq => ne (congrArg Prod.fst eq)simpa [lexApart,ne] using hmono x hx y hy neq· intro u _apply Finset.card_le_one.mprintro v hv w hwby_contra nehave hx : (u,v) ∈ s := by simpa [fiber] using hvhave hy : (u,w) ∈ s := by simpa [fiber] using hwhave bad := hmono (u,v) hx (u,w) hy (fun eq => ne (congrArg Prod.snd eq))simp [lexApart] at bad| inr col =>apply le_trans _ (le_max_right p q)rw [← Nat.one_mul q]apply count_fibers· apply Finset.card_le_one.mprintro u hu v hvby_contra neobtain ⟨x,hx,rfl⟩ := Finset.mem_image.mp huobtain ⟨y,hy,rfl⟩ := Finset.mem_image.mp hvhave bad := hmono x hx y hy (fun eq => ne (congrArg Prod.fst eq))simp [lexApart,ne] at bad· intro u _apply hd colintro v hv w hw nehave hx : (u,v) ∈ s := by simpa [fiber] using hvhave hy : (u,w) ∈ s := by simpa [fiber] using hwsimpa [lexApart] using hmono (u,v) hx (u,w) hy (fun eq => ne (congrArg Prod.snd eq))/-- The fraction of edges internal to P equal fibers is at most 1/P. -/theorem equal_fibers (P m : ℕ) (hP : 0 < P) : P*(P * m.choose 2) ≤ (P*m).choose 2 := byhave h1 : m.choose 2 * 2 = m*(m-1) := by simpa using Nat.choose_succ_right_eq m 1have h2 : (P*m).choose 2 * 2 = (P*m)*(P*m-1) := bysimpa using Nat.choose_succ_right_eq (P*m) 1have le : P*(m-1) ≤ P*m-1 := by rw [Nat.mul_sub, Nat.mul_one]; omegahave big := Nat.mul_le_mul_left (P*m) lehave replace : P*(P*(m.choose 2))*2 = (P*m)*(P*(m-1)) := bycalc_ = (P*P)*(m.choose 2 * 2) := by ring_ = _ := by rw [h1]; ringomegaend Finiteend DecayProducts