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) /-- The lines in the family are pairwise distinct (modeled as `Injective`). -/ def Distinct {n : ℕ} (L : Fin n → Line) : Prop := Function.Injective L /-- 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] lemma diagFamily_distinct (n : ℕ) : Distinct (diagFamily n) := by intro i j hij apply Fin.ext -- compare `C` have hC : (diagFamily n i).C = (diagFamily n j).C := congrArg Line.C hij -- remove the leading minus sign have hC' : (i.1 : ℝ) + 2 = (j.1 : ℝ) + 2 := by simpa [diagFamily] using congrArg (fun t : ℝ => -t) hC have hij' : (i.1 : ℝ) = (j.1 : ℝ) := by linarith exact_mod_cast hij' /-- 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] lemma oneSunnyFamily_distinct (n : ℕ) (hn : 3 ≤ n) : Distinct (oneSunnyFamily n) := by classical intro i j hij by_cases hi : (i.1 : ℕ) < n - 1 · by_cases hj : (j.1 : ℕ) < n - 1 · -- both vertical lines; compare `C` apply Fin.ext have hC : (oneSunnyFamily n i).C = (oneSunnyFamily n j).C := congrArg Line.C hij have hC' : (i.1 : ℝ) + 1 = (j.1 : ℝ) + 1 := by simp [oneSunnyFamily, hi, hj] at hC simpa using congrArg (fun t : ℝ => -t) hC have hij' : (i.1 : ℝ) = (j.1 : ℝ) := by linarith exact_mod_cast hij' · -- `i` vertical but `j` is the final sunny line; compare `B` exfalso have hB : (oneSunnyFamily n i).B = (oneSunnyFamily n j).B := congrArg Line.B hij have : (0 : ℝ) = (n - 1 : ℝ) := by simpa [oneSunnyFamily, hi, hj] using hB have hn' : (n - 1 : ℝ) ≠ 0 := by have : (n - 1 : ℕ) ≠ 0 := by omega [hn] exact_mod_cast this exact hn' (by simpa [this] ) · by_cases hj : (j.1 : ℕ) < n - 1 · -- symmetric to previous case exfalso have hB : (oneSunnyFamily n i).B = (oneSunnyFamily n j).B := congrArg Line.B hij have : (n - 1 : ℝ) = (0 : ℝ) := by simpa [oneSunnyFamily, hi, hj] using hB have hn' : (n - 1 : ℝ) ≠ 0 := by have : (n - 1 : ℕ) ≠ 0 := by omega [hn] exact_mod_cast this exact hn' (by simpa [this] ) · -- both are the final line, so equal indices -- show both must have value `n-1` apply Fin.ext have hi' : i.1 = n - 1 := by -- in `Fin n`, if `¬ i.1 < n-1` then `i.1 = n-1` have : i.1 ≤ n - 1 := Nat.le_pred_of_lt i.2 exact le_antisymm this (le_of_not_gt hi) have hj' : j.1 = n - 1 := by have : j.1 ≤ n - 1 := Nat.le_pred_of_lt j.2 exact le_antisymm this (le_of_not_gt hj) simpa [hi', hj'] /-- 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 -- define the three sunny indices let i0 : Fin n := ⟨0, by omega [hn]⟩ let i1 : Fin n := ⟨1, by omega [hn]⟩ let i2 : Fin n := ⟨2, by omega [hn]⟩ -- characterize which indices are sunny have hsunny_iff : ∀ i : Fin n, (threeSunnyFamily n i).Sunny ↔ i = i0 ∨ i = i1 ∨ i = i2 := by intro i rcases i with ⟨v, hv⟩ cases v with | zero => have hs : (threeSunnyFamily n ⟨0, hv⟩).Sunny := by simp [threeSunnyFamily, Line.Sunny] constructor · intro _ left apply Fin.ext simp [i0] · intro h rcases h with h | h · simpa [h, i0] using hs · rcases h with h | h <;> cases (Fin.ext_iff.mp h) | succ v1 => cases v1 with | zero => have hs : (threeSunnyFamily n ⟨1, hv⟩).Sunny := by simp [threeSunnyFamily, Line.Sunny] constructor · intro _ right; left apply Fin.ext simp [i1] · intro h rcases h with h | h · cases (Fin.ext_iff.mp h) · rcases h with h | h · simpa [h, i1] using hs · cases (Fin.ext_iff.mp h) | succ v2 => cases v2 with | zero => have hs : (threeSunnyFamily n ⟨2, hv⟩).Sunny := by simp [threeSunnyFamily, Line.Sunny] constructor · intro _ right; right apply Fin.ext simp [i2] · intro h rcases h with h | h · cases (Fin.ext_iff.mp h) · rcases h with h | h · cases (Fin.ext_iff.mp h) · simpa [h, i2] using hs | succ v3 => have hns : ¬(threeSunnyFamily n ⟨Nat.succ (Nat.succ (Nat.succ v3)), hv⟩).Sunny := by simp [threeSunnyFamily, Line.Sunny] constructor · intro hs exact False.elim (hns hs) · intro h rcases h with h | h · cases (Fin.ext_iff.mp h) · rcases h with h | h <;> cases (Fin.ext_iff.mp h) -- compute the card of the filter have hfilter : (Finset.univ.filter (fun i : Fin n => (threeSunnyFamily n i).Sunny)) = {i0, i1, i2} := by ext i simp [Finset.mem_filter, hsunny_iff] have h01 : i0 ≠ i1 := by intro h exact (by simpa [i0, i1] using congrArg Fin.val h) have h02 : i0 ≠ i2 := by intro h exact (by simpa [i0, i2] using congrArg Fin.val h) have h12 : i1 ≠ i2 := by intro h exact (by simpa [i1, i2] using congrArg Fin.val h) have hcard : ({i0, i1, i2} : Finset (Fin n)).card = 3 := by simp [Finset.card_insert, Finset.mem_insert, Finset.mem_singleton, h01, h02, h12] -- assemble simpa [SunnyCount, hfilter, hcard] 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] lemma threeSunnyFamily_distinct (n : ℕ) (hn : 3 ≤ n) : Distinct (threeSunnyFamily n) := by classical intro i j hij -- compare on values; split on `i.1` and `j.1` rcases i with ⟨iv, hiv⟩ rcases j with ⟨jv, hjv⟩ -- Use `C`/`B` differences to force equality. -- We do a case split on `iv < 3` and `jv < 3`. by_cases hi3 : iv < 3 · have : iv = 0 ∨ iv = 1 ∨ iv = 2 := by omega rcases this with rfl | rfl | rfl · -- iv = 0 by_cases hj3 : jv < 3 · have : jv = 0 ∨ jv = 1 ∨ jv = 2 := by omega rcases this with rfl | rfl | rfl · rfl · exfalso have hB : (threeSunnyFamily n ⟨0, hiv⟩).B = (threeSunnyFamily n ⟨1, hjv⟩).B := congrArg Line.B hij simpa [threeSunnyFamily] using hB · exfalso have hA : (threeSunnyFamily n ⟨0, hiv⟩).A = (threeSunnyFamily n ⟨2, hjv⟩).A := congrArg Line.A hij simpa [threeSunnyFamily] using hA · -- jv ≥ 3 => diagonal has `A=B=1`, contradict `B=-1` exfalso have hB : (threeSunnyFamily n ⟨0, hiv⟩).B = (threeSunnyFamily n ⟨jv, hjv⟩).B := congrArg Line.B hij have : (-1 : ℝ) = 1 := by simpa [threeSunnyFamily, hj3] using hB norm_num at this · -- iv = 1 by_cases hj3 : jv < 3 · have : jv = 0 ∨ jv = 1 ∨ jv = 2 := by omega rcases this with rfl | rfl | rfl · exfalso have hB : (threeSunnyFamily n ⟨1, hiv⟩).B = (threeSunnyFamily n ⟨0, hjv⟩).B := congrArg Line.B hij simpa [threeSunnyFamily] using hB · rfl · exfalso have hA : (threeSunnyFamily n ⟨1, hiv⟩).A = (threeSunnyFamily n ⟨2, hjv⟩).A := congrArg Line.A hij simpa [threeSunnyFamily] using hA · exfalso have hB : (threeSunnyFamily n ⟨1, hiv⟩).B = (threeSunnyFamily n ⟨jv, hjv⟩).B := congrArg Line.B hij have : (2 : ℝ) = 1 := by simpa [threeSunnyFamily, hj3] using hB norm_num at this · -- iv = 2 by_cases hj3 : jv < 3 · have : jv = 0 ∨ jv = 1 ∨ jv = 2 := by omega rcases this with rfl | rfl | rfl · exfalso have hA : (threeSunnyFamily n ⟨2, hiv⟩).A = (threeSunnyFamily n ⟨0, hjv⟩).A := congrArg Line.A hij simpa [threeSunnyFamily] using hA · exfalso have hA : (threeSunnyFamily n ⟨2, hiv⟩).A = (threeSunnyFamily n ⟨1, hjv⟩).A := congrArg Line.A hij simpa [threeSunnyFamily] using hA · rfl · exfalso have hA : (threeSunnyFamily n ⟨2, hiv⟩).A = (threeSunnyFamily n ⟨jv, hjv⟩).A := congrArg Line.A hij have : (2 : ℝ) = 1 := by simpa [threeSunnyFamily, hj3] using hA norm_num at this · -- iv ≥ 3 by_cases hj3 : jv < 3 · -- symmetric contradiction exfalso have hA : (threeSunnyFamily n ⟨iv, hiv⟩).A = (threeSunnyFamily n ⟨jv, hjv⟩).A := congrArg Line.A hij have : (1 : ℝ) = (threeSunnyFamily n ⟨jv, hjv⟩).A := by simpa [threeSunnyFamily, hi3] using hA -- but for jv=0,1,2 the `A` is 1,1,2, so only possible is jv=0 or 1, still `B` differs. -- We can just compare `B`. have hB : (threeSunnyFamily n ⟨iv, hiv⟩).B = (threeSunnyFamily n ⟨jv, hjv⟩).B := congrArg Line.B hij have : (1 : ℝ) = (threeSunnyFamily n ⟨jv, hjv⟩).B := by simpa [threeSunnyFamily, hi3] using hB -- for jv<3, `B` is -1,2,1, never 1 except jv=2. have : jv = 2 := by omega -- but then compare A again gives 1=2. have hA' : (threeSunnyFamily n ⟨iv, hiv⟩).A = (threeSunnyFamily n ⟨2, by omega [hjv]⟩).A := congrArg Line.A (by simpa [this] using hij) have : (1 : ℝ) = 2 := by simpa [threeSunnyFamily, hi3] using hA' norm_num at this · -- both diagonal: compare `C` apply Fin.ext have hC : (threeSunnyFamily n ⟨iv, hiv⟩).C = (threeSunnyFamily n ⟨jv, hjv⟩).C := congrArg Line.C hij have hC' : (iv : ℝ) + 2 = (jv : ℝ) + 2 := by simpa [threeSunnyFamily, hi3, hj3] using congrArg (fun t : ℝ => -t) hC have hij' : (iv : ℝ) = (jv : ℝ) := by linarith exact_mod_cast hij' /-! Main existence theorems. -/ theorem exists_k0 (n : ℕ) : ∃ L : Fin n → Line, Distinct L ∧ Covers n L ∧ SunnyCount L 0 := by refine ⟨diagFamily n, diagFamily_distinct n, diagFamily_covers n, diagFamily_sunnyCount n⟩ theorem exists_k1 (n : ℕ) (hn : 3 ≤ n) : ∃ L : Fin n → Line, Distinct L ∧ Covers n L ∧ SunnyCount L 1 := by refine ⟨oneSunnyFamily n, oneSunnyFamily_distinct n hn, oneSunnyFamily_covers n hn, oneSunnyFamily_sunnyCount n hn⟩ theorem exists_k3 (n : ℕ) (hn : 3 ≤ n) : ∃ L : Fin n → Line, Distinct L ∧ Covers n L ∧ SunnyCount L 3 := by refine ⟨threeSunnyFamily n, threeSunnyFamily_distinct n hn, threeSunnyFamily_covers n hn, threeSunnyFamily_sunnyCount n hn⟩ end SunnyLines