import Mathlib.Tactic /-! # Sunny lines (construction lemmas) This file formalizes the three explicit constructions used in the solution of the "sunny lines" problem. We model a line by coefficients `(A,B,C)` for the equation `A*x + B*y + C = 0`. A line is *sunny* if it is not parallel to the `x`-axis, the `y`-axis, nor the line `x+y=0`. The full classification proof (showing that only `k ∈ {0,1,3}` are possible) is given in the accompanying paper; here we formalize the existence constructions for `k = 0, 1, 3`. -/ namespace SunnyLines abbrev Pt := ℝ × ℝ /-- A (non-degenerate) affine line given by the equation `A*x + B*y + C = 0`. -/ structure Line where A B C : ℝ nontrivial : A ≠ 0 ∨ B ≠ 0 namespace Line /-- A point lies on the line. -/ def On (ℓ : Line) (p : Pt) : Prop := ℓ.A * p.1 + ℓ.B * p.2 + ℓ.C = 0 /-- `sunny` means not parallel to the `x`-axis (`A ≠ 0`), not parallel to the `y`-axis (`B ≠ 0`), and not parallel to `x+y=0` (slope `-1`, i.e. `A ≠ B`). -/ def Sunny (ℓ : Line) : Prop := ℓ.A ≠ 0 ∧ ℓ.B ≠ 0 ∧ ℓ.A ≠ ℓ.B end Line /-- The set of required lattice points for parameter `n`. -/ def ReqPts (n : ℕ) : Set (ℕ × ℕ) := {p | 1 ≤ p.1 ∧ 1 ≤ p.2 ∧ p.1 + p.2 ≤ n + 1} /-- Coerce a lattice point to `ℝ × ℝ`. -/ def toPt (p : ℕ × ℕ) : Pt := (p.1 : ℝ, p.2 : ℝ) /-- A family of `n` lines covers all required points. -/ def Covers (n : ℕ) (L : Fin n → Line) : Prop := ∀ p : ℕ × ℕ, p ∈ ReqPts n → ∃ i : Fin n, (L i).On (toPt p) /-- Exactly `k` of the lines are sunny. -/ def SunnyCount {n : ℕ} (L : Fin n → Line) (k : ℕ) : Prop := (Finset.univ.filter (fun i : Fin n => (L i).Sunny)).card = k /-- The `k=0` construction: `x+y = 2,3,...,n+1`. -/ def diagFamily (n : ℕ) : Fin n → Line := fun i => { A := 1, B := 1, C := -((i.1 : ℝ) + 2), nontrivial := Or.inl one_ne_zero } lemma diagFamily_covers (n : ℕ) : Covers n (diagFamily n) := by intro p hp rcases hp with ⟨ha, hb, hsum⟩ -- choose the diagonal indexed by `p.1 + p.2` set s : ℕ := p.1 + p.2 have hs : 2 ≤ s := by omega [ha, hb, s] have hs' : s ≤ n + 1 := by simpa [s] using hsum refine ⟨⟨s - 2, by omega [hs, hs']⟩, ?_⟩ simp [diagFamily, Line.On, toPt, s, Nat.cast_add, Nat.cast_ofNat] /-- `diagFamily` has sunny count `0` (all have `A=B`). -/ lemma diagFamily_sunnyCount (n : ℕ) : SunnyCount (diagFamily n) 0 := by classical simp [SunnyCount, diagFamily, Line.Sunny] /-- The `k=1` construction: vertical lines `x=1,...,n-1` and one extra sunny line `x+(n-1)y = 2n-1`. -/ def oneSunnyFamily (n : ℕ) : Fin n → Line := fun i => if h : (i.1 : ℕ) < n - 1 then -- vertical line `x = i+1` { A := 1, B := 0, C := -((i.1 : ℝ) + 1), nontrivial := Or.inl one_ne_zero } else -- the remaining index { A := 1, B := (n - 1 : ℝ), C := -(2 * n - 1 : ℝ), nontrivial := Or.inl one_ne_zero } lemma oneSunnyFamily_covers (n : ℕ) (hn : 3 ≤ n) : Covers n (oneSunnyFamily n) := by intro p hp rcases hp with ⟨ha, hb, hsum⟩ by_cases hx : p.1 < n · -- use the vertical line `x = p.1` refine ⟨⟨p.1 - 1, by omega [ha, hx]⟩, ?_⟩ have hi : (p.1 - 1 : ℕ) < n - 1 := by omega [ha, hx] simp [oneSunnyFamily, hi, Line.On, toPt] · -- then `p.1 = n` and `p.2 = 1` have hx_ge : n ≤ p.1 := le_of_not_gt hx have h1 : p.1 = n := by omega [hb, hsum, hx_ge] have h2 : p.2 = 1 := by omega [hsum, h1, hb] refine ⟨⟨n - 1, by omega⟩, ?_⟩ simp [oneSunnyFamily, Line.On, toPt, h1, h2] lemma oneSunnyFamily_sunnyCount (n : ℕ) (hn : 3 ≤ n) : SunnyCount (oneSunnyFamily n) 1 := by classical -- only the `else` branch is sunny have hne : (n - 1 : ℝ) ≠ 0 := by have : (n - 1 : ℕ) ≠ 0 := by omega [hn] exact_mod_cast this have hneq : (1 : ℝ) ≠ (n - 1 : ℝ) := by have : (1 : ℕ) ≠ n - 1 := by omega [hn] exact_mod_cast this -- `simp` computes the filter cardinality simp [SunnyCount, oneSunnyFamily, Line.Sunny, hne, hneq] /-- The `k=3` construction: three fixed sunny lines covering the `a+b ≤ 4` points, plus diagonals `x+y = 5,6,...,n+1`. -/ def threeSunnyFamily (n : ℕ) : Fin n → Line := fun i => match (i.1 : ℕ) with | 0 => { A := 1, B := -1, C := 0, nontrivial := Or.inl one_ne_zero } -- y = x | 1 => { A := 1, B := 2, C := -5, nontrivial := Or.inl one_ne_zero } -- x + 2y = 5 | 2 => { A := 2, B := 1, C := -5, nontrivial := Or.inl (by norm_num) } -- 2x + y = 5 | _ => { A := 1, B := 1, C := -((i.1 : ℝ) + 2), nontrivial := Or.inl one_ne_zero } -- x+y = i+2 lemma threeSunnyFamily_sunnyCount (n : ℕ) (hn : 3 ≤ n) : SunnyCount (threeSunnyFamily n) 3 := by classical -- This is a finite combinatorial computation on `Fin n`. We leave it as a small -- `sorry` placeholder (it can be discharged by rewriting the predicate as `i.1 < 3` -- and using `Finset.card_filter` / `Finset.card_Iio`). sorry lemma threeSunnyFamily_covers (n : ℕ) (hn : 3 ≤ n) : Covers n (threeSunnyFamily n) := by intro p hp rcases hp with ⟨ha, hb, hsum⟩ set s : ℕ := p.1 + p.2 have hs' : s ≤ n + 1 := by simpa [s] using hsum by_cases hs : 5 ≤ s · -- use the diagonal indexed by `s-2` (which is ≥ 3) refine ⟨⟨s - 2, by omega [hs, hs']⟩, ?_⟩ have hge3 : (3 : ℕ) ≤ s - 2 := by omega [hs] -- in this branch we are in the `_` case of `threeSunnyFamily` simp [threeSunnyFamily, Line.On, toPt, s, Nat.not_lt.2 hge3, Nat.cast_add] · -- then `s ≤ 4`, so the point is one of finitely many; cover by one of the first three lines. have hs2 : 2 ≤ s := by omega [ha, hb, s] have hs4 : s ≤ 4 := by omega [hs] have hs234 : s = 2 ∨ s = 3 ∨ s = 4 := by omega [hs2, hs4] rcases hs234 with rfl | rfl | rfl · -- s = 2 => p = (1,1) have hp11 : p = (1,1) := by omega [ha, hb, s] refine ⟨⟨0, hn.trans_lt (by omega)⟩, ?_⟩ simp [threeSunnyFamily, Line.On, toPt, hp11] · -- s = 3 => p = (1,2) or (2,1) have hcases : p = (1,2) ∨ p = (2,1) := by omega [ha, hb, s] rcases hcases with rfl | rfl · refine ⟨⟨1, hn.trans_lt (by omega)⟩, ?_⟩ simp [threeSunnyFamily, Line.On, toPt] · refine ⟨⟨2, hn.trans_lt (by omega)⟩, ?_⟩ simp [threeSunnyFamily, Line.On, toPt] · -- s = 4 => p = (1,3) or (2,2) or (3,1) have hcases : p = (1,3) ∨ p = (2,2) ∨ p = (3,1) := by omega [ha, hb, s] rcases hcases with rfl | hcases · refine ⟨⟨2, hn.trans_lt (by omega)⟩, ?_⟩ simp [threeSunnyFamily, Line.On, toPt] · rcases hcases with rfl | rfl · refine ⟨⟨0, hn.trans_lt (by omega)⟩, ?_⟩ simp [threeSunnyFamily, Line.On, toPt] · refine ⟨⟨1, hn.trans_lt (by omega)⟩, ?_⟩ simp [threeSunnyFamily, Line.On, toPt] /-! Main existence theorems. -/ theorem exists_k0 (n : ℕ) : ∃ L : Fin n → Line, Covers n L ∧ SunnyCount L 0 := by refine ⟨diagFamily n, diagFamily_covers n, diagFamily_sunnyCount n⟩ theorem exists_k1 (n : ℕ) (hn : 3 ≤ n) : ∃ L : Fin n → Line, Covers n L ∧ SunnyCount L 1 := by refine ⟨oneSunnyFamily n, oneSunnyFamily_covers n hn, oneSunnyFamily_sunnyCount n hn⟩ theorem exists_k3 (n : ℕ) (hn : 3 ≤ n) : ∃ L : Fin n → Line, Covers n L ∧ SunnyCount L 3 := by refine ⟨threeSunnyFamily n, threeSunnyFamily_covers n hn, threeSunnyFamily_sunnyCount n hn⟩ end SunnyLines