Lean / Reader
Lean readerColourRestrictions.lean
Lean 4 Web has Mathlib, so it can check files that import only Mathlib. This one also imports ProgressionColours, which it does not have: you can read it there, but it will not check. Build the project itself to check it.
import ProgressionColoursimport Mathlib.Algebra.Order.BigOperators.Group.Finsetnamespace ProgressionColours/-- Every length `k+2` progression in the interval uses a colour outside any given pair. -/def Valid (N k : ℕ) {α : Type*} (c : ℕ → α) : Prop :=∀ a d, 0 < d → a+(k+1)*d < N → ∀ u v, ∃ x ∈ progression a d (k+1), c x ≠ u ∧ c x ≠ vdef Mono {α : Type*} (c : ℕ → α) (P : Finset ℕ) : Prop := ∃ u, ∀ x ∈ P, c x = u/-- No valid natural-number colouring has a monochromatic length `k+1` progression in `S`. -/def Universal (N k : ℕ) (S : Finset ℕ) : Prop :=∀ (c : ℕ → ℕ), Valid N k c → ∀ a d, 0 < d → progression a d k ⊆ S → ¬ Mono c (progression a d k)theorem term_mem {a d k j : ℕ} (hj : j≤k) : a+j*d ∈ progression a d k :=Finset.mem_image.mpr ⟨j, Finset.mem_range.mpr (by omega),rfl⟩theorem card_progression (a k : ℕ) {d : ℕ} (hd : 0<d) : (progression a d k).card=k+1 := byhave h : Function.Injective (fun j : ℕ => a+j*d) := byintro i j h; exact Nat.eq_of_mul_eq_mul_right hd (Nat.add_left_cancel h)simp [progression,Finset.card_image_of_injective _ h]theorem mono_blocked {α : Type*} {N k a d : ℕ} {c : ℕ → α} (hv : Valid N k c)(hd : 0<d) (hin : a+k*d<N) (hm : Mono c (progression a d k)) : Blocked N k a d := byobtain ⟨u,hu⟩ := hmrefine ⟨hd,hin,?_,?_⟩· by_contra! hobtain ⟨x,hx,h1,h2⟩ := hv (a-d) d hd (by rw [Nat.add_mul,Nat.one_mul];omega) u (c (a-d))obtain ⟨j,hj,rfl⟩ := Finset.mem_image.mp hxcases j with| zero => simp at h2| succ j =>have hj' : j≤k := by have := Finset.mem_range.mp hj;omegahave he : a-d+(j+1)*d=a+j*d := by rw [Nat.add_mul,Nat.one_mul];omegaexact h1 (he ▸ hu _ (term_mem hj'))· by_contra! hobtain ⟨x,hx,h1,h2⟩ := hv a d hd h u (c (a+(k+1)*d))obtain ⟨j,hj,rfl⟩ := Finset.mem_image.mp hxby_cases hj' : j≤k· exact h1 (hu _ (term_mem hj'))· have he : j=k+1 := by have := Finset.mem_range.mp hj; omegaexact h2 (by rw [he])/-- When at least three equally spaced terms fit in a progression one term longer, itscommon difference must be the same and it must be an end segment. -/theorem no_larger_progression {N k a d b e : ℕ} (hk : 2≤k) (hb : Blocked N k a d)(he : 0<e) (hend : b+(k+1)*e<N) : ¬ progression a d k ⊆ progression b e (k+1) := byintro subobtain ⟨hpos,_,hleft,hright⟩ := hbhave hmem (j : ℕ) (hj : j≤k) := sub (term_mem (a:=a) (d:=d) hj)obtain ⟨i,hi,hi'⟩ := Finset.mem_image.mp (hmem 0 (by omega))obtain ⟨j,hj,hj'⟩ := Finset.mem_image.mp (hmem 1 (by omega))obtain ⟨l,hl,hl'⟩ := Finset.mem_image.mp (hmem k le_rfl)simp only [Nat.zero_mul,Nat.add_zero,Nat.one_mul] at hi' hj'have hlbound := Finset.mem_range.mp hlhave ij : i<j := by by_contra! h; have := Nat.mul_le_mul_right e h; omegahave delta : d=(j-i)*e := by rw [Nat.sub_mul];omegahave il : l=i+k*(j-i) := byapply Nat.eq_of_mul_eq_mul_right herw [Nat.add_mul,Nat.mul_assoc,←delta];omegahave gap : j-i=1 := byby_contra! hhave big := Nat.mul_le_mul_left k (by omega : 2≤j-i);omegahave de : d=e := by simpa [gap] using deltarw [gap,Nat.mul_one] at ilhave hi01 : i=0 ∨ i=1 := by omegarw [de] at hleft hrightrcases hi01 with rfl | rfl <;> simp only [Nat.zero_mul,Nat.one_mul,Nat.add_zero] at hi'· subst a; omega· omegadef single_class (P : Finset ℕ) (x : ℕ) : ℕ := if x ∈ P then 0 else x+1/-- A blocked progression can be made the only colour class of size greater than one. -/theorem blocked_realizable {N k a d : ℕ} (hk : 2≤k) (hb : Blocked N k a d) :Valid N k (single_class (progression a d k)) ∧Mono (single_class (progression a d k)) (progression a d k) := bylet P := progression a d krefine ⟨?_,0,fun _ h => by simp [single_class,h]⟩intro b e he hend u vlet Q := progression b e (k+1)by_contra! hhave covers (x : ℕ) (hx : x∈Q) : single_class P x=u ∨ single_class P x=v := byby_cases h' : single_class P x=u· exact Or.inl h'· exact Or.inr (h x hx h')have sizeQ := card_progression b (k+1) hehave sizeP := card_progression a k hb.1change Q.card=_ at sizeQchange P.card=_ at sizePby_cases hit : ∃ x ∈ Q, x∈P· obtain ⟨x,hx,hp⟩ := hithave zero := covers x hxsimp only [single_class,if_pos hp] at zerohave sub (w : ℕ) (hw : ∀ y ∈ Q, single_class P y=0 ∨ single_class P y=w) : P⊆Q := byhave sq : Q ⊆ insert (w-1) P := byintro y hyby_cases hy' : y∈P· exact Finset.mem_insert_of_mem hy'· have hh := hw y hy; simp [single_class,hy'] at hhexact Finset.mem_insert.mpr (Or.inl (by omega))have eq := Finset.eq_of_subset_of_card_le sq (by have := Finset.card_insert_le (w-1) P;omega)rw [eq]; exact Finset.subset_insert ..apply no_larger_progression hk hb he hendrcases zero with rfl | rfl· exact sub v covers· exact sub u (fun y hy => (covers y hy).symm)· have small : Q⊆{u-1,v-1} := byintro y hyhave hp : y∉P := fun hmem => hit ⟨y,hy,hmem⟩have hh := covers y hy; simp only [single_class,if_neg hp] at hhsimp only [Finset.mem_insert,Finset.mem_singleton];omegahave := Finset.card_le_card smallhave := Finset.card_insert_le (u-1) ({v-1} : Finset ℕ)simp only [Finset.card_singleton] at *;omegatheorem universal_iff {N k : ℕ} (hk : 2≤k) (S : Finset ℕ) (hs : S ⊆ Finset.range N) :Universal N k S ↔ Hits N k (Finset.range N \ S) := byconstructor· intro hu a d hbby_contra! hhave sub : progression a d k ⊆ S := byintro x hxobtain ⟨j,hj,hje⟩ := Finset.mem_image.mp hxhave hj' : j≤k := by have := Finset.mem_range.mp hj;omegahave hxN : x ∈ Finset.range N := Finset.mem_range.mpr (by have := Nat.mul_le_mul_right d hj';have := hb.2.1;omega)by_contra hnexact h x (Finset.mem_sdiff.mpr ⟨hxN,hn⟩) hxobtain ⟨hv,hm⟩ := blocked_realizable hk hbexact hu _ hv a d hb.1 sub hm· intro hit c hv a d hd sub hmhave hin := Finset.mem_range.mp (hs (sub (term_mem (j:=k) le_rfl)))obtain ⟨x,hx,hm'⟩ := hit a d (mono_blocked hv hd hin hm)exact (Finset.mem_sdiff.mp hx).2 (sub hm')theorem strip_in_range {k q r : ℕ} (hr : r≤k) : strip k q r ⊆ Finset.range ((k+1)*q+r) := byintro x hxobtain ⟨i,hi,rfl⟩ := Finset.mem_image.mp hxhave him := Finset.mem_range.mp hihave hcount : count k q r ≤ q := max_le (Nat.sub_le ..) (Nat.sub_le ..)have := Nat.mul_le_mul_right q hrapply Finset.mem_range.mprsimp only [Nat.add_mul,Nat.mul_add,Nat.mul_one,Nat.one_mul] at *;omega/-- The maximum size of a fixed domain on which every valid colouring has no monochromaticlength-`L` progression. The cover and the disjoint progressions witnessing optimality work forarbitrary interval size. -/theorem sharp_universal_domain (N L : ℕ) (hL : 3≤L) :∃ S ⊆ Finset.range N, Universal N (L-1) S ∧S.card=N-count (L-1) (N/L) (N%L) ∧∀ A ⊆ Finset.range N, Universal N (L-1) A → A.card ≤ S.card := bylet k := L-1let q := N/Llet r := N%Lhave hk : k+1=L := by dsimp [k];omegahave hk2 : 2≤k := by dsimp [k];omegahave hr : r≤k := by have := Nat.mod_lt N (by omega : 0<L); dsimp [r,k];omegahave hn : (k+1)*q+r=N := by rw [hk];exact Nat.div_add_mod N Llet T := strip k q rhave ht : T ⊆ Finset.range N := hn ▸ strip_in_range hrhave hits : Hits N k T := hn ▸ strip_hits hrlet S := Finset.range N \ Thave sub : S ⊆ Finset.range N := Finset.sdiff_subsethave heq : Finset.range N \ S = T := Finset.sdiff_sdiff_eq_self hthave card : S.card=N-count k q r := by simp [S,Finset.card_sdiff_of_subset ht,T,card_strip]refine ⟨S,sub,(universal_iff hk2 S sub).2 (heq ▸ hits),card,?_⟩intro A ha huahave cover := (universal_iff hk2 A ha).1 huahave bound := exact_cover_lower hr (Finset.range N \ A) (by rwa [hn])rw [Finset.card_sdiff_of_subset ha,Finset.card_range] at boundhave := Finset.card_le_card hasimp only [Finset.card_range] at *;omegatheorem sharp_for_erdos160 (N : ℕ) (hN : 2≤N) :∃ S ⊆ Finset.range N, Universal N 2 S ∧ S.card=2*((N+2)/3) ∧∀ A ⊆ Finset.range N, Universal N 2 A → A.card ≤ S.card := byobtain ⟨S,hs,hu,hc,hm⟩ := sharp_universal_domain N 3 (by decide)refine ⟨S,hs,hu,?_,hm⟩rw [hc]; simp only [count];omega/-- Hunter's reduction on a prefix. It suffices that every three-term progression can beextended to the right in the original interval. -/theorem prefix_no_mono {α : Type*} {N M a d : ℕ} {c : ℕ → α} (hv : Valid N 2 c)(hNM : 3*M≤2*N+2) (hd : 0<d) (hin : a+2*d<M) : ¬ Mono c (progression a d 2) := byintro hmhave hb := mono_blocked hv hd (by omega) hmhave := hb.2.2.2;omega/-- Any numerical bound for a 3-AP-free subset of the prefix transfers to a lower boundon the number of colours. -/theorem transfer_three_free_bound {N M K R : ℕ} (c : ℕ → Fin K) (hv : Valid N 2 c)(hNM : 3*M≤2*N+2)(hR : ∀ A ⊆ Finset.range M,(∀ a d, 0<d → ¬ progression a d 2 ⊆ A) → A.card≤R) : M ≤ R*K := byclassicalhave ht : ∀ x ∈ Finset.range M, c x ∈ (Finset.univ : Finset (Fin K)) := by simphave h := Finset.card_le_mul_card_image_of_maps_to ht R (byintro col _apply hR _ (Finset.filter_subset ..)intro a d hd subhave H (j : ℕ) (hj : j≤2) := Finset.mem_filter.mp (sub (term_mem (a:=a) (d:=d) hj))apply prefix_no_mono hv hNM hd (Finset.mem_range.mp (H 2 (by decide)).1)exact ⟨col,fun x hx => (Finset.mem_filter.mp (sub hx)).2⟩)simpa using hend ProgressionColours