import Mathlib /-! # Pick's Theorem, 2D Lattice Geometry & the Reflexive Polygon 12-Point Theorem This module builds a self-contained Lean 4 formalization of two-dimensional lattice geometry, Pick's Theorem, its equivalence with 2D Ehrhart theory, and the Reflexive Polygon 12-Point Theorem: 1. **2D Lattice Determinants & Segment Counting**: - Signed area `det2 p q r`, barycentric sum identity, Cramer's rule, and bounding-box finiteness of lattice points in any lattice triangle. - Exact segment lattice-point count `card_segPts_eq_gcd_add_one`: `#(segPts p q) = Int.gcd (q.1 - p.1) (q.2 - p.2) + 1`, proved via a direct Bézout bijection, yielding the exact triangle boundary count `card_triBdyPts_eq_sum_gcd` and linear dilation scaling. 2. **Primitive Unimodular Triangle Theorem** (`primitive_triangle_unimodular`): - A positively oriented lattice triangle `T = (p, q, r)` satisfies `det2 p q r = 1` if and only if it has `0` interior lattice points and exactly `3` boundary lattice points (`{p, q, r}`). - Proved constructively via Bézout's identity with no external geometry imports. 3. **Unconditional Pick's Theorem for Every Lattice Triangle** (`picks_theorem_triangle`): - For EVERY `p, q, r : ℤ × ℤ` with `0 < det2 p q r`, `det2 p q r = 2 * I + B - 2` (i.e., `Area(T) = I + B/2 - 1`). - Proved by strong induction on `(det2 p q r).toNat` using a pointwise weight-function decomposition for both edge and interior subdivisions. 4. **Constructive Pick's Theorem for Lattice Triangulations & 2D Ehrhart–Pick Equivalence** (`picks_theorem_triangulation`, `ehrhart_pick_equivalence_2d`): - Euler's formula `V - E + F = 1` with primitive unimodular triangles implies `2 * Area = 2 * I + B - 2`. - For every 2D lattice triangle `T` and dilation factor `t ≥ 1`, `2 * L_T(t) = det2(T) * t^2 + B(T) * t + 2` and `2 * L_T^∘(t) = det2(T) * t^2 - B(T) * t + 2 = 2 * L_T(-t)`, with `h*(T) = (1, I + B - 3, I)`. 5. **The Reflexive Polygon 12-Point Theorem** (`unimodular_fan_twelve_theorem`, `reflexive_polygon_twelve_theorem`, `all_sixteen_reflexive_polygons_twelve`): - For every unimodular lattice cycle `vs` of length `m ≥ 3`, `v_{i-1} + v_{i+1} = a_i • v_i` with `a_i = cross2 v_{i-1} v_{i+1}`. - Unimodular star subdivision (toric blowup) preserves the Noether invariant `∑ (3 - a_i) = 12`, proving `B(P) + B(P^∘) = 12` for all smooth toric / reflexive fans built from `ℙ²` or Hirzebruch fans `𝔽_k`, plus explicit verification across all 16 isomorphism classes of 2D reflexive polygons. -/ namespace PicksTheoremAndTwoDimLatticeGeometry open Finset /-! ## Section 1: 2D Lattice Determinants, Cross Products & Cramer's Rule -/ /-- Twice the signed area of the triangle `(p, q, r)` in `ℤ²`. -/ def det2 (p q r : ℤ × ℤ) : ℤ := (q.1 - p.1) * (r.2 - p.2) - (q.2 - p.2) * (r.1 - p.1) /-- 2D determinant (cross product) of two lattice vectors `u, v ∈ ℤ²`. -/ def cross2 (u v : ℤ × ℤ) : ℤ := u.1 * v.2 - u.2 * v.1 /-- Dilation of a lattice point by an integer factor `t`. -/ def dilatePt (t : ℤ) (p : ℤ × ℤ) : ℤ × ℤ := (t * p.1, t * p.2) lemma det2_cyclic (p q r : ℤ × ℤ) : det2 p q r = det2 q r p := by unfold det2; ring lemma det2_cyclic2 (p q r : ℤ × ℤ) : det2 p q r = det2 r p q := by unfold det2; ring lemma det2_swap_last (p q r : ℤ × ℤ) : det2 p r q = -det2 p q r := by unfold det2; ring lemma det2_swap_first (p q r : ℤ × ℤ) : det2 q p r = -det2 p q r := by unfold det2; ring lemma det2_self_left (p q : ℤ × ℤ) : det2 p p q = 0 := by unfold det2; ring lemma det2_self_right (p q : ℤ × ℤ) : det2 p q q = 0 := by unfold det2; ring lemma det2_self_outer (p q : ℤ × ℤ) : det2 p q p = 0 := by unfold det2; ring lemma det2_barycentric_sum (p q r x : ℤ × ℤ) : det2 x q r + det2 p x r + det2 p q x = det2 p q r := by unfold det2; ring lemma det2_cramer_fst (p q r x : ℤ × ℤ) : det2 p q r * x.1 = det2 x q r * p.1 + det2 p x r * q.1 + det2 p q x * r.1 := by unfold det2; ring lemma det2_cramer_snd (p q r x : ℤ × ℤ) : det2 p q r * x.2 = det2 x q r * p.2 + det2 p x r * q.2 + det2 p q x * r.2 := by unfold det2; ring lemma det2_dilate (t : ℤ) (p q r : ℤ × ℤ) : det2 (dilatePt t p) (dilatePt t q) (dilatePt t r) = t ^ 2 * det2 p q r := by unfold det2 dilatePt; ring lemma det2_eq_zero_dot_identity (p q r x : ℤ × ℤ) (h : det2 p q x = 0) : ((q.1 - p.1) ^ 2 + (q.2 - p.2) ^ 2) * det2 p x r = ((x.1 - p.1) * (q.1 - p.1) + (x.2 - p.2) * (q.2 - p.2)) * det2 p q r := by unfold det2 at h ⊢ linear_combination -((q.1 - p.1) * (r.1 - p.1) + (q.2 - p.2) * (r.2 - p.2)) * h lemma det2_collinear_scale_left (p q r w x : ℤ × ℤ) (h : det2 p q w = 0) : det2 p q r * det2 p w x = det2 p w r * det2 p q x := by unfold det2 at h ⊢ linear_combination -((x.1 - p.1) * (r.2 - p.2) - (x.2 - p.2) * (r.1 - p.1)) * h lemma det2_collinear_scale_right (p q r w x : ℤ × ℤ) (h : det2 p q w = 0) : det2 p q r * det2 w q x = det2 w q r * det2 p q x := by unfold det2 at h ⊢ linear_combination -((r.1 - q.1) * (x.2 - q.2) - (r.2 - q.2) * (x.1 - q.1)) * h lemma det2_interior_balance (p q r w x : ℤ × ℤ) : det2 w q r * det2 p x w + det2 p w r * det2 q x w + det2 p q w * det2 r x w = 0 := by unfold det2; ring lemma eq_of_det2_coords (p q r x y : ℤ × ℤ) (hD : det2 p q r ≠ 0) (h1 : det2 x q r = det2 y q r) (h2 : det2 p x r = det2 p y r) (h3 : det2 p q x = det2 p q y) : x = y := by have hx1 := det2_cramer_fst p q r x have hy1 := det2_cramer_fst p q r y have hx2 := det2_cramer_snd p q r x have hy2 := det2_cramer_snd p q r y ext · apply mul_left_cancel₀ hD rw [hx1, hy1, h1, h2, h3] · apply mul_left_cancel₀ hD rw [hx2, hy2, h1, h2, h3] lemma eq_first_of_det2_zero (p q r x : ℤ × ℤ) (hD : det2 p q r ≠ 0) (h2 : det2 p x r = 0) (h3 : det2 p q x = 0) : x = p := by apply eq_of_det2_coords p q r x p hD · have := det2_barycentric_sum p q r x rw [h2, h3] at this; linarith · rw [h2, det2_self_left] · rw [h3, det2_self_outer] lemma eq_second_of_det2_zero (p q r x : ℤ × ℤ) (hD : det2 p q r ≠ 0) (h1 : det2 x q r = 0) (h3 : det2 p q x = 0) : x = q := by apply eq_of_det2_coords p q r x q hD · rw [h1, det2_self_left] · have := det2_barycentric_sum p q r x rw [h1, h3] at this; linarith · rw [h3, det2_self_right] lemma eq_third_of_det2_zero (p q r x : ℤ × ℤ) (hD : det2 p q r ≠ 0) (h1 : det2 x q r = 0) (h2 : det2 p x r = 0) : x = r := by apply eq_of_det2_coords p q r x r hD · rw [h1, det2_self_outer] · rw [h2, det2_self_right] · have := det2_barycentric_sum p q r x rw [h1, h2] at this; linarith /-! ## Section 2: Segment & Triangle Lattice-Point Finsets -/ /-- Membership of `x ∈ ℤ²` on the closed line segment `[p, q]`. -/ def onSeg (p q x : ℤ × ℤ) : Prop := det2 p q x = 0 ∧ 0 ≤ (x.1 - p.1) * (q.1 - p.1) + (x.2 - p.2) * (q.2 - p.2) ∧ 0 ≤ (q.1 - x.1) * (q.1 - p.1) + (q.2 - x.2) * (q.2 - p.2) instance (p q x : ℤ × ℤ) : Decidable (onSeg p q x) := by unfold onSeg; infer_instance /-- Axis-aligned bounding box of the segment `[p, q]`. -/ noncomputable def segBox (p q : ℤ × ℤ) : Finset (ℤ × ℤ) := Finset.Icc (min p.1 q.1) (max p.1 q.1) ×ˢ Finset.Icc (min p.2 q.2) (max p.2 q.2) /-- The finite set of all lattice points on the closed segment `[p, q]`. -/ noncomputable def segPts (p q : ℤ × ℤ) : Finset (ℤ × ℤ) := (segBox p q).filter (onSeg p q) lemma onSeg_mem_segBox {p q x : ℤ × ℤ} (hpq : p ≠ q) (h : onSeg p q x) : x ∈ segBox p q := by rcases h with ⟨hdet, h1, h2⟩ unfold det2 at hdet simp only [segBox, Finset.mem_product, Finset.mem_Icc] have hne : q.1 - p.1 ≠ 0 ∨ q.2 - p.2 ≠ 0 := by by_contra h0; push Not at h0; apply hpq; ext <;> omega have hpos : 0 < (q.1 - p.1) ^ 2 + (q.2 - p.2) ^ 2 := by rcases hne with hne1 | hne2 · have := sq_pos_of_ne_zero hne1; nlinarith [sq_nonneg (q.2 - p.2)] · have := sq_pos_of_ne_zero hne2; nlinarith [sq_nonneg (q.1 - p.1)] have hsq1a : 0 ≤ (q.1 - p.1) * (x.1 - p.1) := by have eq1 : ((q.1 - p.1) ^ 2 + (q.2 - p.2) ^ 2) * ((q.1 - p.1) * (x.1 - p.1)) = (q.1 - p.1) ^ 2 * ((x.1 - p.1) * (q.1 - p.1) + (x.2 - p.2) * (q.2 - p.2)) := by linear_combination -(q.1 - p.1) * (q.2 - p.2) * hdet have hrhs : 0 ≤ (q.1 - p.1) ^ 2 * ((x.1 - p.1) * (q.1 - p.1) + (x.2 - p.2) * (q.2 - p.2)) := mul_nonneg (sq_nonneg _) h1 nlinarith have hsq1b : 0 ≤ (q.1 - p.1) * (q.1 - x.1) := by have eq2 : ((q.1 - p.1) ^ 2 + (q.2 - p.2) ^ 2) * ((q.1 - p.1) * (q.1 - x.1)) = (q.1 - p.1) ^ 2 * ((q.1 - x.1) * (q.1 - p.1) + (q.2 - x.2) * (q.2 - p.2)) := by linear_combination (q.1 - p.1) * (q.2 - p.2) * hdet have hrhs : 0 ≤ (q.1 - p.1) ^ 2 * ((q.1 - x.1) * (q.1 - p.1) + (q.2 - x.2) * (q.2 - p.2)) := mul_nonneg (sq_nonneg _) h2 nlinarith have hsq2a : 0 ≤ (q.2 - p.2) * (x.2 - p.2) := by have eq1 : ((q.1 - p.1) ^ 2 + (q.2 - p.2) ^ 2) * ((q.2 - p.2) * (x.2 - p.2)) = (q.2 - p.2) ^ 2 * ((x.1 - p.1) * (q.1 - p.1) + (x.2 - p.2) * (q.2 - p.2)) := by linear_combination (q.1 - p.1) * (q.2 - p.2) * hdet have hrhs : 0 ≤ (q.2 - p.2) ^ 2 * ((x.1 - p.1) * (q.1 - p.1) + (x.2 - p.2) * (q.2 - p.2)) := mul_nonneg (sq_nonneg _) h1 nlinarith have hsq2b : 0 ≤ (q.2 - p.2) * (q.2 - x.2) := by have eq2 : ((q.1 - p.1) ^ 2 + (q.2 - p.2) ^ 2) * ((q.2 - p.2) * (q.2 - x.2)) = (q.2 - p.2) ^ 2 * ((q.1 - x.1) * (q.1 - p.1) + (q.2 - x.2) * (q.2 - p.2)) := by linear_combination -(q.1 - p.1) * (q.2 - p.2) * hdet have hrhs : 0 ≤ (q.2 - p.2) ^ 2 * ((q.1 - x.1) * (q.1 - p.1) + (q.2 - x.2) * (q.2 - p.2)) := mul_nonneg (sq_nonneg _) h2 nlinarith have hx1 : min p.1 q.1 ≤ x.1 ∧ x.1 ≤ max p.1 q.1 := by rcases lt_trichotomy p.1 q.1 with hlt | heq | hgt · rw [min_eq_left (le_of_lt hlt), max_eq_right (le_of_lt hlt)] exact ⟨by nlinarith, by nlinarith⟩ · have hq2_ne : q.2 - p.2 ≠ 0 := by rcases hne with hne1 | hne2 · exfalso; omega · exact hne2 have hmul0 : (q.2 - p.2) * (x.1 - p.1) = 0 := by rw [← heq] at hdet; linarith have hx : x.1 = p.1 := by rcases mul_eq_zero.mp hmul0 with h0 | h0 · exact absurd h0 hq2_ne · omega rw [heq, min_self, max_self, hx, heq]; exact ⟨le_rfl, le_rfl⟩ · rw [min_eq_right (le_of_lt hgt), max_eq_left (le_of_lt hgt)] exact ⟨by nlinarith, by nlinarith⟩ have hx2 : min p.2 q.2 ≤ x.2 ∧ x.2 ≤ max p.2 q.2 := by rcases lt_trichotomy p.2 q.2 with hlt | heq | hgt · rw [min_eq_left (le_of_lt hlt), max_eq_right (le_of_lt hlt)] exact ⟨by nlinarith, by nlinarith⟩ · have hq1_ne : q.1 - p.1 ≠ 0 := by rcases hne with hne1 | hne2 · exact hne1 · exfalso; omega have hmul0 : (q.1 - p.1) * (x.2 - p.2) = 0 := by rw [← heq] at hdet; linarith have hx : x.2 = p.2 := by rcases mul_eq_zero.mp hmul0 with h0 | h0 · exact absurd h0 hq1_ne · omega rw [heq, min_self, max_self, hx, heq]; exact ⟨le_rfl, le_rfl⟩ · rw [min_eq_right (le_of_lt hgt), max_eq_left (le_of_lt hgt)] exact ⟨by nlinarith, by nlinarith⟩ exact ⟨hx1, hx2⟩ /-- Exact lattice-point count on any nondegenerate closed lattice segment `[p, q]`: `#(segPts p q) = Int.gcd (q.1 - p.1) (q.2 - p.2) + 1`. -/ theorem card_segPts_eq_gcd_add_one (p q : ℤ × ℤ) (hpq : p ≠ q) : (segPts p q).card = Int.gcd (q.1 - p.1) (q.2 - p.2) + 1 := by set a₁ : ℤ := q.1 - p.1 set a₂ : ℤ := q.2 - p.2 set g : ℕ := Int.gcd a₁ a₂ have hne : a₁ ≠ 0 ∨ a₂ ≠ 0 := by by_contra h push Not at h apply hpq ext <;> omega have hg_pos : 0 < g := by rcases hne with h1 | h2 · exact Nat.pos_of_ne_zero (by intro hg0; exact h1 (Int.gcd_eq_zero_iff.mp hg0).1) · exact Nat.pos_of_ne_zero (by intro hg0; exact h2 (Int.gcd_eq_zero_iff.mp hg0).2) have hgZ_pos : (0 : ℤ) < (g : ℤ) := Nat.cast_pos.mpr hg_pos have hdvd1 : (g : ℤ) ∣ a₁ := Int.gcd_dvd_left a₁ a₂ have hdvd2 : (g : ℤ) ∣ a₂ := Int.gcd_dvd_right a₁ a₂ set u₁ : ℤ := a₁ / (g : ℤ) set u₂ : ℤ := a₂ / (g : ℤ) have hu1 : (g : ℤ) * u₁ = a₁ := Int.mul_ediv_cancel' hdvd1 have hu2 : (g : ℤ) * u₂ = a₂ := Int.mul_ediv_cancel' hdvd2 have hbez := Int.gcd_eq_gcd_ab a₁ a₂ set c₁ : ℤ := Int.gcdA a₁ a₂ set c₂ : ℤ := Int.gcdB a₁ a₂ have hu_bez : c₁ * u₁ + c₂ * u₂ = 1 := by apply mul_left_cancel₀ (ne_of_gt hgZ_pos) calc (g : ℤ) * (c₁ * u₁ + c₂ * u₂) = a₁ * c₁ + a₂ * c₂ := by rw [← hu1, ← hu2]; ring _ = (g : ℤ) := hbez.symm _ = (g : ℤ) * 1 := (mul_one _).symm have hu_sq_pos : 0 < u₁ ^ 2 + u₂ ^ 2 := by have : u₁ ≠ 0 ∨ u₂ ≠ 0 := by rcases hne with h1 | h2 · left; intro hu; rw [hu, mul_zero] at hu1; exact h1 hu1.symm · right; intro hu; rw [hu, mul_zero] at hu2; exact h2 hu2.symm rcases this with h1 | h2 · have := sq_pos_of_ne_zero h1; nlinarith [sq_nonneg u₂] · have := sq_pos_of_ne_zero h2; nlinarith [sq_nonneg u₁] let f : ℕ → ℤ × ℤ := fun k => (p.1 + (k : ℤ) * u₁, p.2 + (k : ℤ) * u₂) have hmem : ∀ k ∈ Finset.range (g + 1), f k ∈ segPts p q := by intro k hk rw [Finset.mem_range] at hk have hkZ : (k : ℤ) ≤ (g : ℤ) := by exact_mod_cast Nat.lt_succ_iff.mp hk have hon : onSeg p q (f k) := by refine ⟨?_, ?_, ?_⟩ · unfold det2 f; simp only change a₁ * (p.2 + ↑k * u₂ - p.2) - a₂ * (p.1 + ↑k * u₁ - p.1) = 0 rw [← hu1, ← hu2]; ring · simp only [f] have : (p.1 + ↑k * u₁ - p.1) * a₁ + (p.2 + ↑k * u₂ - p.2) * a₂ = (k : ℤ) * (g : ℤ) * (u₁ ^ 2 + u₂ ^ 2) := by rw [← hu1, ← hu2]; ring rw [show q.1 - p.1 = a₁ from rfl, show q.2 - p.2 = a₂ from rfl, this] positivity · simp only [f] have : (q.1 - (p.1 + ↑k * u₁)) * a₁ + (q.2 - (p.2 + ↑k * u₂)) * a₂ = ((g : ℤ) - (k : ℤ)) * (g : ℤ) * (u₁ ^ 2 + u₂ ^ 2) := by have h1 : q.1 - (p.1 + ↑k * u₁) = a₁ - ↑k * u₁ := by unfold a₁; ring have h2 : q.2 - (p.2 + ↑k * u₂) = a₂ - ↑k * u₂ := by unfold a₂; ring rw [h1, h2, ← hu1, ← hu2]; ring rw [show q.1 - p.1 = a₁ from rfl, show q.2 - p.2 = a₂ from rfl, this] have hgk : 0 ≤ (g : ℤ) - (k : ℤ) := by omega positivity exact Finset.mem_filter.mpr ⟨onSeg_mem_segBox hpq hon, hon⟩ have hbij : segPts p q = (Finset.range (g + 1)).image f := by ext x simp only [Finset.mem_image, Finset.mem_range] constructor · intro hx have hon : onSeg p q x := (Finset.mem_filter.mp hx).2 rcases hon with ⟨hdet, h1, h2⟩ unfold det2 at hdet have hdet_u : u₁ * (x.2 - p.2) = u₂ * (x.1 - p.1) := by apply mul_left_cancel₀ (ne_of_gt hgZ_pos) calc (g : ℤ) * (u₁ * (x.2 - p.2)) = a₁ * (x.2 - p.2) := by rw [← hu1]; ring _ = a₂ * (x.1 - p.1) := by linarith _ = (g : ℤ) * (u₂ * (x.1 - p.1)) := by rw [← hu2]; ring set kZ : ℤ := c₁ * (x.1 - p.1) + c₂ * (x.2 - p.2) have hx1 : x.1 - p.1 = kZ * u₁ := by calc x.1 - p.1 = (c₁ * u₁ + c₂ * u₂) * (x.1 - p.1) := by rw [hu_bez, one_mul] _ = c₁ * (x.1 - p.1) * u₁ + c₂ * (u₂ * (x.1 - p.1)) := by ring _ = c₁ * (x.1 - p.1) * u₁ + c₂ * (u₁ * (x.2 - p.2)) := by rw [← hdet_u] _ = kZ * u₁ := by ring have hx2 : x.2 - p.2 = kZ * u₂ := by calc x.2 - p.2 = (c₁ * u₁ + c₂ * u₂) * (x.2 - p.2) := by rw [hu_bez, one_mul] _ = c₁ * (u₁ * (x.2 - p.2)) + c₂ * (x.2 - p.2) * u₂ := by ring _ = c₁ * (u₂ * (x.1 - p.1)) + c₂ * (x.2 - p.2) * u₂ := by rw [hdet_u] _ = kZ * u₂ := by ring have hdot1 : (x.1 - p.1) * a₁ + (x.2 - p.2) * a₂ = kZ * (g : ℤ) * (u₁ ^ 2 + u₂ ^ 2) := by rw [hx1, hx2, ← hu1, ← hu2]; ring have hdot2 : (q.1 - x.1) * a₁ + (q.2 - x.2) * a₂ = ((g : ℤ) - kZ) * (g : ℤ) * (u₁ ^ 2 + u₂ ^ 2) := by have hqx1 : q.1 - x.1 = a₁ - (x.1 - p.1) := by unfold a₁; ring have hqx2 : q.2 - x.2 = a₂ - (x.2 - p.2) := by unfold a₂; ring rw [hqx1, hqx2, hx1, hx2, ← hu1, ← hu2]; ring have hk_nonneg : 0 ≤ kZ := by have hpos : 0 < (g : ℤ) * (u₁ ^ 2 + u₂ ^ 2) := mul_pos hgZ_pos hu_sq_pos nlinarith have hk_le : kZ ≤ (g : ℤ) := by have hpos : 0 < (g : ℤ) * (u₁ ^ 2 + u₂ ^ 2) := mul_pos hgZ_pos hu_sq_pos nlinarith have hkZ_eq : ((kZ.toNat : ℕ) : ℤ) = kZ := Int.toNat_of_nonneg hk_nonneg refine ⟨kZ.toNat, by omega, ?_⟩ ext <;> simp only [f, hkZ_eq] <;> linarith · rintro ⟨k, hk, rfl⟩ exact hmem k (Finset.mem_range.mpr hk) rw [hbij, Finset.card_image_of_injective] · simp · intro k₁ k₂ hf have h1 : (k₁ : ℤ) * u₁ = (k₂ : ℤ) * u₁ := by have := congrArg Prod.fst hf; simp only [f] at this; linarith have h2 : (k₁ : ℤ) * u₂ = (k₂ : ℤ) * u₂ := by have := congrArg Prod.snd hf; simp only [f] at this; linarith have heqZ : (k₁ : ℤ) = (k₂ : ℤ) := by calc (k₁ : ℤ) = (k₁ : ℤ) * (c₁ * u₁ + c₂ * u₂) := by rw [hu_bez, mul_one] _ = c₁ * ((k₁ : ℤ) * u₁) + c₂ * ((k₁ : ℤ) * u₂) := by ring _ = c₁ * ((k₂ : ℤ) * u₁) + c₂ * ((k₂ : ℤ) * u₂) := by rw [h1, h2] _ = (k₂ : ℤ) * (c₁ * u₁ + c₂ * u₂) := by ring _ = (k₂ : ℤ) := by rw [hu_bez, mul_one] exact_mod_cast heqZ /-- Closed triangle membership predicate for a positively oriented triangle `(p, q, r)`. -/ def inTri (p q r x : ℤ × ℤ) : Prop := 0 ≤ det2 x q r ∧ 0 ≤ det2 p x r ∧ 0 ≤ det2 p q x /-- Strict interior membership predicate for a positively oriented triangle `(p, q, r)`. -/ def inTriInt (p q r x : ℤ × ℤ) : Prop := 0 < det2 x q r ∧ 0 < det2 p x r ∧ 0 < det2 p q x /-- Boundary membership predicate for a positively oriented triangle `(p, q, r)`. -/ def inTriBdy (p q r x : ℤ × ℤ) : Prop := inTri p q r x ∧ (det2 x q r = 0 ∨ det2 p x r = 0 ∨ det2 p q x = 0) instance (p q r x : ℤ × ℤ) : Decidable (inTri p q r x) := by unfold inTri; infer_instance instance (p q r x : ℤ × ℤ) : Decidable (inTriInt p q r x) := by unfold inTriInt; infer_instance instance (p q r x : ℤ × ℤ) : Decidable (inTriBdy p q r x) := by unfold inTriBdy; infer_instance /-- Axis-aligned bounding box of the triangle `(p, q, r)`. -/ noncomputable def triBox (p q r : ℤ × ℤ) : Finset (ℤ × ℤ) := Finset.Icc (min p.1 (min q.1 r.1)) (max p.1 (max q.1 r.1)) ×ˢ Finset.Icc (min p.2 (min q.2 r.2)) (max p.2 (max q.2 r.2)) /-- All lattice points in the closed triangle `(p, q, r)`. -/ noncomputable def triPts (p q r : ℤ × ℤ) : Finset (ℤ × ℤ) := (triBox p q r).filter (inTri p q r) /-- Strict interior lattice points of the triangle `(p, q, r)`. -/ noncomputable def triIntPts (p q r : ℤ × ℤ) : Finset (ℤ × ℤ) := (triBox p q r).filter (inTriInt p q r) /-- Boundary lattice points of the triangle `(p, q, r)`. -/ noncomputable def triBdyPts (p q r : ℤ × ℤ) : Finset (ℤ × ℤ) := (triBox p q r).filter (inTriBdy p q r) lemma inTri_mem_triBox {p q r x : ℤ × ℤ} (hD : 0 < det2 p q r) (hx : inTri p q r x) : x ∈ triBox p q r := by rcases hx with ⟨h1, h2, h3⟩ have hsum := det2_barycentric_sum p q r x have hc1 := det2_cramer_fst p q r x have hc2 := det2_cramer_snd p q r x simp only [triBox, Finset.mem_product, Finset.mem_Icc] refine ⟨⟨?_, ?_⟩, ⟨?_, ?_⟩⟩ · have hp := mul_le_mul_of_nonneg_left (min_le_left p.1 (min q.1 r.1)) h1 have hq := mul_le_mul_of_nonneg_left (le_trans (min_le_right p.1 _) (min_le_left q.1 r.1)) h2 have hr := mul_le_mul_of_nonneg_left (le_trans (min_le_right p.1 _) (min_le_right q.1 r.1)) h3 have hd : det2 p q r * min p.1 (min q.1 r.1) = det2 x q r * min p.1 (min q.1 r.1) + det2 p x r * min p.1 (min q.1 r.1) + det2 p q x * min p.1 (min q.1 r.1) := by rw [← hsum]; ring nlinarith · have hp := mul_le_mul_of_nonneg_left (le_max_left p.1 (max q.1 r.1)) h1 have hq := mul_le_mul_of_nonneg_left (le_trans (le_max_left q.1 r.1) (le_max_right p.1 _)) h2 have hr := mul_le_mul_of_nonneg_left (le_trans (le_max_right q.1 r.1) (le_max_right p.1 _)) h3 have hd : det2 p q r * max p.1 (max q.1 r.1) = det2 x q r * max p.1 (max q.1 r.1) + det2 p x r * max p.1 (max q.1 r.1) + det2 p q x * max p.1 (max q.1 r.1) := by rw [← hsum]; ring nlinarith · have hp := mul_le_mul_of_nonneg_left (min_le_left p.2 (min q.2 r.2)) h1 have hq := mul_le_mul_of_nonneg_left (le_trans (min_le_right p.2 _) (min_le_left q.2 r.2)) h2 have hr := mul_le_mul_of_nonneg_left (le_trans (min_le_right p.2 _) (min_le_right q.2 r.2)) h3 have hd : det2 p q r * min p.2 (min q.2 r.2) = det2 x q r * min p.2 (min q.2 r.2) + det2 p x r * min p.2 (min q.2 r.2) + det2 p q x * min p.2 (min q.2 r.2) := by rw [← hsum]; ring nlinarith · have hp := mul_le_mul_of_nonneg_left (le_max_left p.2 (max q.2 r.2)) h1 have hq := mul_le_mul_of_nonneg_left (le_trans (le_max_left q.2 r.2) (le_max_right p.2 _)) h2 have hr := mul_le_mul_of_nonneg_left (le_trans (le_max_right q.2 r.2) (le_max_right p.2 _)) h3 have hd : det2 p q r * max p.2 (max q.2 r.2) = det2 x q r * max p.2 (max q.2 r.2) + det2 p x r * max p.2 (max q.2 r.2) + det2 p q x * max p.2 (max q.2 r.2) := by rw [← hsum]; ring nlinarith lemma mem_triPts {p q r x : ℤ × ℤ} (hD : 0 < det2 p q r) : x ∈ triPts p q r ↔ inTri p q r x := by simp only [triPts, Finset.mem_filter] exact ⟨fun h => h.2, fun h => ⟨inTri_mem_triBox hD h, h⟩⟩ lemma mem_triIntPts {p q r x : ℤ × ℤ} (hD : 0 < det2 p q r) : x ∈ triIntPts p q r ↔ inTriInt p q r x := by simp only [triIntPts, Finset.mem_filter] exact ⟨fun h => h.2, fun h => ⟨inTri_mem_triBox hD ⟨le_of_lt h.1, le_of_lt h.2.1, le_of_lt h.2.2⟩, h⟩⟩ lemma mem_triBdyPts {p q r x : ℤ × ℤ} (hD : 0 < det2 p q r) : x ∈ triBdyPts p q r ↔ inTriBdy p q r x := by simp only [triBdyPts, Finset.mem_filter] exact ⟨fun h => h.2, fun h => ⟨inTri_mem_triBox hD h.1, h⟩⟩ lemma inTri_iff_int_or_bdy (p q r x : ℤ × ℤ) : inTri p q r x ↔ inTriInt p q r x ∨ inTriBdy p q r x := by unfold inTri inTriInt inTriBdy constructor · intro ⟨h1, h2, h3⟩ by_cases hz : det2 x q r = 0 ∨ det2 p x r = 0 ∨ det2 p q x = 0 · exact Or.inr ⟨⟨h1, h2, h3⟩, hz⟩ · push Not at hz exact Or.inl ⟨by omega, by omega, by omega⟩ · rintro (⟨h1, h2, h3⟩ | ⟨h, _⟩) · exact ⟨le_of_lt h1, le_of_lt h2, le_of_lt h3⟩ · exact h lemma not_int_and_bdy (p q r x : ℤ × ℤ) : ¬ (inTriInt p q r x ∧ inTriBdy p q r x) := by rintro ⟨⟨h1, h2, h3⟩, ⟨_, h0 | h0 | h0⟩⟩ <;> omega lemma card_triPts_eq_int_add_bdy {p q r : ℤ × ℤ} (hD : 0 < det2 p q r) : (triPts p q r).card = (triIntPts p q r).card + (triBdyPts p q r).card := by have hunion : triPts p q r = triIntPts p q r ∪ triBdyPts p q r := by ext x rw [mem_triPts hD, Finset.mem_union, mem_triIntPts hD, mem_triBdyPts hD] exact inTri_iff_int_or_bdy p q r x have hdisj : Disjoint (triIntPts p q r) (triBdyPts p q r) := by rw [Finset.disjoint_left] intro x hxI hxB exact not_int_and_bdy p q r x ⟨(mem_triIntPts hD).mp hxI, (mem_triBdyPts hD).mp hxB⟩ rw [hunion, Finset.card_union_of_disjoint hdisj] lemma triIntPts_cyclic {p q r : ℤ × ℤ} (hD : 0 < det2 p q r) : triIntPts p q r = triIntPts q r p := by have hD' : 0 < det2 q r p := by rw [← det2_cyclic]; exact hD ext x rw [mem_triIntPts hD, mem_triIntPts hD'] unfold inTriInt rw [det2_cyclic x q r, det2_cyclic x r p, det2_cyclic r p x, det2_cyclic p q x] tauto lemma triBdyPts_cyclic {p q r : ℤ × ℤ} (hD : 0 < det2 p q r) : triBdyPts p q r = triBdyPts q r p := by have hD' : 0 < det2 q r p := by rw [← det2_cyclic]; exact hD ext x rw [mem_triBdyPts hD, mem_triBdyPts hD'] unfold inTriBdy inTri rw [det2_cyclic x q r, det2_cyclic x r p, det2_cyclic r p x, det2_cyclic p q x] tauto lemma onSeg_iff_edge {p q r x : ℤ × ℤ} (hD : 0 < det2 p q r) : onSeg p q x ↔ det2 p q x = 0 ∧ 0 ≤ det2 x q r ∧ 0 ≤ det2 p x r := by have hne : (q.1 - p.1) ^ 2 + (q.2 - p.2) ^ 2 > 0 := by by_contra h0 have ha1 : q.1 - p.1 = 0 := by nlinarith [sq_nonneg (q.1 - p.1), sq_nonneg (q.2 - p.2)] have ha2 : q.2 - p.2 = 0 := by nlinarith [sq_nonneg (q.1 - p.1), sq_nonneg (q.2 - p.2)] unfold det2 at hD rw [ha1, ha2] at hD linarith constructor · rintro ⟨hdet, h1, h2⟩ have hid := det2_eq_zero_dot_identity p q r x hdet have hsum := det2_barycentric_sum p q r x have hpxr : 0 ≤ det2 p x r := by nlinarith have hxq : (q.1 - x.1) * (q.1 - p.1) + (q.2 - x.2) * (q.2 - p.2) = ((q.1 - p.1) ^ 2 + (q.2 - p.2) ^ 2) - ((x.1 - p.1) * (q.1 - p.1) + (x.2 - p.2) * (q.2 - p.2)) := by ring have hxqr : 0 ≤ det2 x q r := by nlinarith exact ⟨hdet, hxqr, hpxr⟩ · rintro ⟨hdet, hxqr, hpxr⟩ have hid := det2_eq_zero_dot_identity p q r x hdet have hsum := det2_barycentric_sum p q r x have h1 : 0 ≤ (x.1 - p.1) * (q.1 - p.1) + (x.2 - p.2) * (q.2 - p.2) := by nlinarith have hxq : (q.1 - x.1) * (q.1 - p.1) + (q.2 - x.2) * (q.2 - p.2) = ((q.1 - p.1) ^ 2 + (q.2 - p.2) ^ 2) - ((x.1 - p.1) * (q.1 - p.1) + (x.2 - p.2) * (q.2 - p.2)) := by ring have h2 : 0 ≤ (q.1 - x.1) * (q.1 - p.1) + (q.2 - x.2) * (q.2 - p.2) := by nlinarith exact ⟨hdet, h1, h2⟩ /-- Exact boundary lattice-point count of any positively oriented lattice triangle: `B(p, q, r) = gcd(q - p) + gcd(r - q) + gcd(p - r)`. -/ theorem card_triBdyPts_eq_sum_gcd {p q r : ℤ × ℤ} (hD : 0 < det2 p q r) : (triBdyPts p q r).card = Int.gcd (q.1 - p.1) (q.2 - p.2) + Int.gcd (r.1 - q.1) (r.2 - q.2) + Int.gcd (p.1 - r.1) (p.2 - r.2) := by have hpq : p ≠ q := by intro h; subst h; rw [det2_self_left] at hD; omega have hqr : q ≠ r := by intro h; subst h; rw [det2_self_right] at hD; omega have hrp : r ≠ p := by intro h; subst h; rw [det2_self_outer] at hD; omega have hD_qrp : 0 < det2 q r p := by rw [← det2_cyclic]; exact hD have hD_rpq : 0 < det2 r p q := by rw [← det2_cyclic2]; exact hD have hmem_seg : ∀ a b x, a ≠ b → (x ∈ segPts a b ↔ onSeg a b x) := by intro a b x hab simp only [segPts, Finset.mem_filter] exact ⟨fun h => h.2, fun h => ⟨onSeg_mem_segBox hab h, h⟩⟩ have hunion : triBdyPts p q r = (segPts p q ∪ segPts q r) ∪ segPts r p := by ext x rw [mem_triBdyPts hD, Finset.mem_union, Finset.mem_union, hmem_seg p q x hpq, hmem_seg q r x hqr, hmem_seg r p x hrp, onSeg_iff_edge hD, onSeg_iff_edge hD_qrp, onSeg_iff_edge hD_rpq] unfold inTriBdy inTri have h1 : det2 q r x = det2 x q r := det2_cyclic2 q r x have h2 : det2 r p x = det2 p x r := det2_cyclic r p x have h3 : det2 x r p = det2 p x r := det2_cyclic2 x r p have h4 : det2 q x p = det2 p q x := det2_cyclic2 q x p have h5 : det2 x p q = det2 p q x := det2_cyclic x p q have h6 : det2 r x q = det2 x q r := det2_cyclic r x q rw [h1, h2, h3, h4, h5, h6] constructor · rintro ⟨⟨a, b, c⟩, h0 | h0 | h0⟩ · exact Or.inl (Or.inr ⟨h0, b, c⟩) · exact Or.inr ⟨h0, c, a⟩ · exact Or.inl (Or.inl ⟨h0, a, b⟩) · rintro ((⟨h0, a, b⟩ | ⟨h0, b, c⟩) | ⟨h0, c, a⟩) · exact ⟨⟨a, b, by omega⟩, Or.inr (Or.inr h0)⟩ · exact ⟨⟨by omega, b, c⟩, Or.inl h0⟩ · exact ⟨⟨a, by omega, c⟩, Or.inr (Or.inl h0)⟩ have hinter12 : segPts p q ∩ segPts q r = {q} := by ext x rw [Finset.mem_inter, hmem_seg p q x hpq, hmem_seg q r x hqr, onSeg_iff_edge hD, onSeg_iff_edge hD_qrp, Finset.mem_singleton] have h1 : det2 q r x = det2 x q r := det2_cyclic2 q r x constructor · rintro ⟨⟨h3, _, _⟩, ⟨h1', _, _⟩⟩ rw [h1] at h1' exact eq_second_of_det2_zero p q r x (ne_of_gt hD) h1' h3 · intro hx; subst x refine ⟨⟨det2_self_right p q, by rw [det2_self_left], le_of_lt hD⟩, ⟨det2_self_outer q r, le_of_lt hD_qrp, by rw [det2_self_left]⟩⟩ have hinter_12_3 : (segPts p q ∪ segPts q r) ∩ segPts r p = {p, r} := by ext x rw [Finset.mem_inter, Finset.mem_union, hmem_seg p q x hpq, hmem_seg q r x hqr, hmem_seg r p x hrp, onSeg_iff_edge hD, onSeg_iff_edge hD_qrp, onSeg_iff_edge hD_rpq, Finset.mem_insert, Finset.mem_singleton] have h1 : det2 q r x = det2 x q r := det2_cyclic2 q r x have h2 : det2 r p x = det2 p x r := det2_cyclic r p x constructor · rintro ⟨⟨h3, _, _⟩ | ⟨h1', _, _⟩, ⟨h2', _, _⟩⟩ · rw [h2] at h2' exact Or.inl (eq_first_of_det2_zero p q r x (ne_of_gt hD) h2' h3) · rw [h1] at h1'; rw [h2] at h2' exact Or.inr (eq_third_of_det2_zero p q r x (ne_of_gt hD) h1' h2') · rintro (hx | hx) <;> subst x · refine ⟨Or.inl ⟨det2_self_outer p q, le_of_lt hD, by rw [det2_self_left]⟩, ⟨det2_self_right r p, by rw [det2_self_left], le_of_lt hD_rpq⟩⟩ · refine ⟨Or.inr ⟨det2_self_right q r, by rw [det2_self_left], le_of_lt hD_qrp⟩, ⟨det2_self_outer r p, le_of_lt hD_rpq, by rw [det2_self_left]⟩⟩ have hc1 := Finset.card_union_add_card_inter (segPts p q) (segPts q r) have hc2 := Finset.card_union_add_card_inter (segPts p q ∪ segPts q r) (segPts r p) rw [hinter12, Finset.card_singleton, card_segPts_eq_gcd_add_one p q hpq, card_segPts_eq_gcd_add_one q r hqr] at hc1 rw [hinter_12_3, Finset.card_pair hrp.symm, ← hunion, card_segPts_eq_gcd_add_one r p hrp] at hc2 omega /-- Dilation of a positively oriented lattice triangle scales its boundary lattice point count linearly: `B(t · T) = t * B(T)`. -/ theorem card_triBdyPts_dilate {p q r : ℤ × ℤ} (hD : 0 < det2 p q r) (t : ℕ) (ht : 0 < t) : (triBdyPts (dilatePt t p) (dilatePt t q) (dilatePt t r)).card = t * (triBdyPts p q r).card := by have hDt : 0 < det2 (dilatePt t p) (dilatePt t q) (dilatePt t r) := by rw [det2_dilate] have : (0 : ℤ) < (t : ℤ) ^ 2 := by positivity exact mul_pos this hD rw [card_triBdyPts_eq_sum_gcd hDt, card_triBdyPts_eq_sum_gcd hD] have hgcd_mul : ∀ a b : ℤ, Int.gcd ((t : ℤ) * a) ((t : ℤ) * b) = t * Int.gcd a b := by intro a b unfold Int.gcd rw [Int.natAbs_mul, Int.natAbs_mul, Int.natAbs_natCast, Nat.gcd_mul_left] have h1 : (dilatePt t q).1 - (dilatePt t p).1 = (t : ℤ) * (q.1 - p.1) := by unfold dilatePt; ring have h2 : (dilatePt t q).2 - (dilatePt t p).2 = (t : ℤ) * (q.2 - p.2) := by unfold dilatePt; ring have h3 : (dilatePt t r).1 - (dilatePt t q).1 = (t : ℤ) * (r.1 - q.1) := by unfold dilatePt; ring have h4 : (dilatePt t r).2 - (dilatePt t q).2 = (t : ℤ) * (r.2 - q.2) := by unfold dilatePt; ring have h5 : (dilatePt t p).1 - (dilatePt t r).1 = (t : ℤ) * (p.1 - r.1) := by unfold dilatePt; ring have h6 : (dilatePt t p).2 - (dilatePt t r).2 = (t : ℤ) * (p.2 - r.2) := by unfold dilatePt; ring rw [h1, h2, h3, h4, h5, h6, hgcd_mul, hgcd_mul, hgcd_mul] ring /-! ## Section 3: Primitive Unimodular Triangle Theorem -/ /-- Every lattice triangle with `det2 p q r ≥ 2` contains a lattice point strictly distinct from its three vertices `p, q, r`. Proved constructively via Bézout's identity. -/ theorem exists_extra_point_of_det2_ge_two {p q r : ℤ × ℤ} (hD : 2 ≤ det2 p q r) : ∃ w : ℤ × ℤ, inTri p q r w ∧ w ≠ p ∧ w ≠ q ∧ w ≠ r := by set a₁ : ℤ := q.1 - p.1 set a₂ : ℤ := q.2 - p.2 set b₁ : ℤ := r.1 - p.1 set b₂ : ℤ := r.2 - p.2 set D : ℤ := det2 p q r have hD_eq : D = a₁ * b₂ - a₂ * b₁ := rfl set g : ℕ := Int.gcd a₁ a₂ have hg_pos : 0 < g := by by_contra h have hg0 : g = 0 := by omega have ha := Int.gcd_eq_zero_iff.mp hg0 rw [ha.1, ha.2] at hD_eq; omega by_cases hg2 : 2 ≤ g · -- Case 1: edge `(p, q)` is not primitive; take `p + (q - p) / g`. have hdvd1 : (g : ℤ) ∣ a₁ := Int.gcd_dvd_left a₁ a₂ have hdvd2 : (g : ℤ) ∣ a₂ := Int.gcd_dvd_right a₁ a₂ set u₁ : ℤ := a₁ / (g : ℤ) set u₂ : ℤ := a₂ / (g : ℤ) have hu1 : (g : ℤ) * u₁ = a₁ := Int.mul_ediv_cancel' hdvd1 have hu2 : (g : ℤ) * u₂ = a₂ := Int.mul_ediv_cancel' hdvd2 let w : ℤ × ℤ := (p.1 + u₁, p.2 + u₂) have h_pqw : det2 p q w = 0 := by apply mul_left_cancel₀ (show (g : ℤ) ≠ 0 by omega) calc (g : ℤ) * det2 p q w = a₁ * ((g : ℤ) * u₂) - a₂ * ((g : ℤ) * u₁) := by unfold det2 w; simp; ring _ = a₁ * a₂ - a₂ * a₁ := by rw [hu1, hu2] _ = 0 := by ring _ = (g : ℤ) * 0 := (mul_zero _).symm have h_pwr_mul : (g : ℤ) * det2 p w r = D := by calc (g : ℤ) * det2 p w r = ((g : ℤ) * u₁) * b₂ - ((g : ℤ) * u₂) * b₁ := by unfold det2 w; simp; ring _ = a₁ * b₂ - a₂ * b₁ := by rw [hu1, hu2] _ = D := hD_eq.symm have h_pwr_pos : 0 < det2 p w r := by nlinarith have h_pwr_lt : det2 p w r < D := by nlinarith have h_wqr_pos : 0 < det2 w q r := by have := det2_barycentric_sum p q r w omega refine ⟨w, ⟨le_of_lt h_wqr_pos, le_of_lt h_pwr_pos, le_of_eq h_pqw.symm⟩, ?_, ?_, ?_⟩ · intro h; rw [h, det2_self_left] at h_pwr_pos; omega · intro h; rw [h, det2_self_left] at h_wqr_pos; omega · intro h; rw [h] at h_pqw; omega · -- Case 2: `gcd(a₁, a₂) = 1`; complete `a` to a unimodular basis via Bézout. have hg1 : g = 1 := by omega have hbez : (1 : ℤ) = a₁ * Int.gcdA a₁ a₂ + a₂ * Int.gcdB a₁ a₂ := by have := Int.gcd_eq_gcd_ab a₁ a₂ change (g : ℤ) = _ at this rw [hg1] at this exact_mod_cast this set w₁ : ℤ := -Int.gcdB a₁ a₂ set w₂ : ℤ := Int.gcdA a₁ a₂ have hcross : a₁ * w₂ - a₂ * w₁ = 1 := by change a₁ * Int.gcdA a₁ a₂ - a₂ * (-Int.gcdB a₁ a₂) = 1 linarith set s : ℤ := b₁ * w₂ - b₂ * w₁ have hb1 : b₁ = s * a₁ + D * w₁ := by symm calc s * a₁ + D * w₁ = b₁ * (a₁ * w₂ - a₂ * w₁) := by rw [hD_eq]; ring _ = b₁ := by rw [hcross, mul_one] have hb2 : b₂ = s * a₂ + D * w₂ := by symm calc s * a₂ + D * w₂ = b₂ * (a₁ * w₂ - a₂ * w₁) := by rw [hD_eq]; ring _ = b₂ := by rw [hcross, mul_one] set q₀ : ℤ := s / D set r₀ : ℤ := s % D have hdiv : D * q₀ + r₀ = s := by have := Int.emod_add_mul_ediv s D; linarith have hr0_nonneg : 0 ≤ r₀ := Int.emod_nonneg s (by omega) have hr0_lt : r₀ < D := Int.emod_lt_of_pos s (by omega) by_cases hr0 : r₀ = 0 · let x : ℤ × ℤ := (p.1 + q₀ * a₁ + w₁, p.2 + q₀ * a₂ + w₂) have h3 : det2 p q x = 1 := by calc det2 p q x = a₁ * (q₀ * a₂ + w₂) - a₂ * (q₀ * a₁ + w₁) := by unfold det2 x; simp; ring _ = a₁ * w₂ - a₂ * w₁ := by ring _ = 1 := hcross have h2 : det2 p x r = 0 := by have hs : s = D * q₀ := by omega calc det2 p x r = (q₀ * a₁ + w₁) * b₂ - (q₀ * a₂ + w₂) * b₁ := by unfold det2 x; simp; ring _ = (q₀ * a₁ + w₁) * (D * q₀ * a₂ + D * w₂) - (q₀ * a₂ + w₂) * (D * q₀ * a₁ + D * w₁) := by rw [hb1, hb2, hs] _ = 0 := by ring have h1 : det2 x q r = D - 1 := by have := det2_barycentric_sum p q r x; omega refine ⟨x, ⟨by omega, by omega, by omega⟩, ?_, ?_, ?_⟩ · intro h; rw [h, det2_self_outer] at h3; omega · intro h; rw [h, det2_self_right] at h3; omega · intro h; rw [h, det2_self_outer] at h1; omega · let x : ℤ × ℤ := (p.1 + (q₀ + 1) * a₁ + w₁, p.2 + (q₀ + 1) * a₂ + w₂) have h3 : det2 p q x = 1 := by calc det2 p q x = a₁ * ((q₀ + 1) * a₂ + w₂) - a₂ * ((q₀ + 1) * a₁ + w₁) := by unfold det2 x; simp; ring _ = a₁ * w₂ - a₂ * w₁ := by ring _ = 1 := hcross have h2 : det2 p x r = D - r₀ := by calc det2 p x r = ((q₀ + 1) * a₁ + w₁) * b₂ - ((q₀ + 1) * a₂ + w₂) * b₁ := by unfold det2 x; simp; ring _ = ((q₀ + 1) * a₁ + w₁) * (s * a₂ + D * w₂) - ((q₀ + 1) * a₂ + w₂) * (s * a₁ + D * w₁) := by rw [hb1, hb2] _ = ((q₀ + 1) * D - s) * (a₁ * w₂ - a₂ * w₁) := by ring _ = D - r₀ := by rw [hcross, mul_one]; linarith have h1 : det2 x q r = r₀ - 1 := by have := det2_barycentric_sum p q r x; omega refine ⟨x, ⟨by omega, by omega, by omega⟩, ?_, ?_, ?_⟩ · intro h; rw [h, det2_self_outer] at h3; omega · intro h; rw [h, det2_self_right] at h3; omega · intro h; rw [h, det2_self_right] at h2; omega /-- **Primitive Unimodular Triangle Theorem**: A positively oriented lattice triangle `(p, q, r)` is unimodular (`det2 p q r = 1`) if and only if it has no interior lattice points (`I = 0`) and its boundary consists solely of its three vertices (`B = 3`). -/ theorem primitive_triangle_unimodular {p q r : ℤ × ℤ} (hD : 0 < det2 p q r) : det2 p q r = 1 ↔ (triIntPts p q r).card = 0 ∧ (triBdyPts p q r).card = 3 := by have hpq : p ≠ q := by intro h; subst h; rw [det2_self_left] at hD; omega have hqr : q ≠ r := by intro h; subst h; rw [det2_self_right] at hD; omega have hrp : r ≠ p := by intro h; subst h; rw [det2_self_outer] at hD; omega constructor · intro h1 have hI : triIntPts p q r = ∅ := by ext x simp only [mem_triIntPts hD, Finset.notMem_empty, iff_false] rintro ⟨a, b, c⟩ have := det2_barycentric_sum p q r x omega have hB : triBdyPts p q r = {p, q, r} := by ext x rw [mem_triBdyPts hD] simp only [Finset.mem_insert, Finset.mem_singleton] constructor · rintro ⟨⟨a, b, c⟩, _⟩ have hsum := det2_barycentric_sum p q r x have hcases : (det2 p x r = 0 ∧ det2 p q x = 0) ∨ (det2 x q r = 0 ∧ det2 p q x = 0) ∨ (det2 x q r = 0 ∧ det2 p x r = 0) := by omega rcases hcases with ⟨hb, hc⟩ | ⟨ha, hc⟩ | ⟨ha, hb⟩ · exact Or.inl (eq_first_of_det2_zero p q r x (ne_of_gt hD) hb hc) · exact Or.inr (Or.inl (eq_second_of_det2_zero p q r x (ne_of_gt hD) ha hc)) · exact Or.inr (Or.inr (eq_third_of_det2_zero p q r x (ne_of_gt hD) ha hb)) · rintro (hx | hx | hx) <;> subst x · exact ⟨⟨by omega, by rw [det2_self_left], by rw [det2_self_outer]⟩, Or.inr (Or.inl (det2_self_left p r))⟩ · exact ⟨⟨by rw [det2_self_left], by omega, by rw [det2_self_right]⟩, Or.inl (det2_self_left q r)⟩ · exact ⟨⟨by rw [det2_self_outer], by rw [det2_self_right], by omega⟩, Or.inl (det2_self_outer r q)⟩ rw [hI, hB, Finset.card_empty] simp [hpq, hqr, hrp.symm] · rintro ⟨hI, hB⟩ by_contra hne have hge2 : 2 ≤ det2 p q r := by omega obtain ⟨w, hw, hwp, hwq, hwr⟩ := exists_extra_point_of_det2_ge_two hge2 have hsub : ({p, q, r, w} : Finset (ℤ × ℤ)) ⊆ triPts p q r := by intro x hx rw [mem_triPts hD] simp only [Finset.mem_insert, Finset.mem_singleton] at hx rcases hx with hx | hx | hx | hx <;> subst x · exact ⟨by omega, by rw [det2_self_left], by rw [det2_self_outer]⟩ · exact ⟨by rw [det2_self_left], by omega, by rw [det2_self_right]⟩ · exact ⟨by rw [det2_self_outer], by rw [det2_self_right], by omega⟩ · exact hw have hcard4 : ({p, q, r, w} : Finset (ℤ × ℤ)).card = 4 := by simp [hpq, hqr, hrp.symm, hwp.symm, hwq.symm, hwr.symm] have hle := Finset.card_le_card hsub rw [hcard4, card_triPts_eq_int_add_bdy hD, hI, hB] at hle omega /-! ## Section 4: Unconditional Pick's Theorem for Every Lattice Triangle -/ /-- Pointwise Pick weight of a lattice point `x` in a triangle `(p, q, r)`: `2` if strictly interior, `1` if on the boundary, `0` otherwise. -/ def triWeight (p q r x : ℤ × ℤ) : ℤ := if inTriInt p q r x then 2 else if inTriBdy p q r x then 1 else 0 /-- Pick's invariant `2 * I + B - 2` of a lattice triangle `(p, q, r)`. -/ noncomputable def pickVal (p q r : ℤ × ℤ) : ℤ := 2 * (triIntPts p q r).card + (triBdyPts p q r).card - 2 lemma sum_triWeight_eq {p q r : ℤ × ℤ} (hD : 0 < det2 p q r) (S : Finset (ℤ × ℤ)) (hsub : triPts p q r ⊆ S) : ∑ x ∈ S, triWeight p q r x = 2 * (triIntPts p q r).card + (triBdyPts p q r).card := by have hzero : ∀ x ∈ S, x ∉ triPts p q r → triWeight p q r x = 0 := by intro x _ hx rw [mem_triPts hD, inTri_iff_int_or_bdy] at hx push Not at hx unfold triWeight simp [hx.1, hx.2] rw [← Finset.sum_subset hsub hzero] have hunion : triPts p q r = triIntPts p q r ∪ triBdyPts p q r := by ext x rw [mem_triPts hD, Finset.mem_union, mem_triIntPts hD, mem_triBdyPts hD] exact inTri_iff_int_or_bdy p q r x have hdisj : Disjoint (triIntPts p q r) (triBdyPts p q r) := by rw [Finset.disjoint_left] intro x hxI hxB exact not_int_and_bdy p q r x ⟨(mem_triIntPts hD).mp hxI, (mem_triBdyPts hD).mp hxB⟩ rw [hunion, Finset.sum_union hdisj] have hsumI : ∑ x ∈ triIntPts p q r, triWeight p q r x = 2 * (triIntPts p q r).card := by have : ∀ x ∈ triIntPts p q r, triWeight p q r x = 2 := by intro x hx; unfold triWeight; simp [(mem_triIntPts hD).mp hx] rw [Finset.sum_congr rfl this, Finset.sum_const, nsmul_eq_mul]; ring have hsumB : ∑ x ∈ triBdyPts p q r, triWeight p q r x = (triBdyPts p q r).card := by have : ∀ x ∈ triBdyPts p q r, triWeight p q r x = 1 := by intro x hx have hB := (mem_triBdyPts hD).mp hx have hnotI : ¬ inTriInt p q r x := fun hI => not_int_and_bdy p q r x ⟨hI, hB⟩ unfold triWeight; simp [hnotI, hB] rw [Finset.sum_congr rfl this, Finset.sum_const, nsmul_eq_mul]; ring rw [hsumI, hsumB] /-- Edge-subdivision additivity of Pick's invariant `2I + B - 2`: if `w` lies on the open edge `(p, q)` of `(p, q, r)`, then `pickVal p q r = pickVal p w r + pickVal w q r`. -/ lemma pickVal_edge_split {p q r w : ℤ × ℤ} (hD : 0 < det2 p q r) (hw0 : det2 p q w = 0) (hw1 : 0 < det2 p w r) (hw2 : 0 < det2 w q r) : pickVal p q r = pickVal p w r + pickVal w q r := by have hsub1 : triPts p w r ⊆ triPts p q r := by intro x hx rw [mem_triPts hw1] at hx rw [mem_triPts hD] rcases hx with ⟨h1, h2, h3⟩ have hsc1 := det2_collinear_scale_left p q r w x hw0 have h3' : 0 ≤ det2 p q x := by nlinarith have hsum := det2_barycentric_sum p q r x have hsum1 := det2_barycentric_sum p w r x have hsc2 := det2_collinear_scale_right p q r w x hw0 have hsumD := det2_barycentric_sum p q r w have hxq : 0 ≤ det2 x q r := by have : det2 p q x = det2 p w x + det2 w q x := by unfold det2 at hw0 ⊢; linear_combination hw0 nlinarith exact ⟨hxq, h2, h3'⟩ have hsub2 : triPts w q r ⊆ triPts p q r := by intro x hx rw [mem_triPts hw2] at hx rw [mem_triPts hD] rcases hx with ⟨h1, h2, h3⟩ have hsc2 := det2_collinear_scale_right p q r w x hw0 have h3' : 0 ≤ det2 p q x := by nlinarith have hsum := det2_barycentric_sum p q r x have hsum2 := det2_barycentric_sum w q r x have hsc1 := det2_collinear_scale_left p q r w x hw0 have hsumD := det2_barycentric_sum p q r w have hpx : 0 ≤ det2 p x r := by have : det2 p q x = det2 p w x + det2 w q x := by unfold det2 at hw0 ⊢; linear_combination hw0 nlinarith exact ⟨h1, hpx, h3'⟩ have hptwise : ∀ x ∈ triPts p q r, triWeight p w r x + triWeight w q r x = triWeight p q r x + (if x = w then 1 else 0) + (if x = r then 1 else 0) := by intro x hx have hin : inTri p q r x := (mem_triPts hD).mp hx rcases hin with ⟨hx1, hx2, hx3⟩ have hsum := det2_barycentric_sum p q r x have hsum1 := det2_barycentric_sum p w r x have hsum2 := det2_barycentric_sum w q r x have hsumD := det2_barycentric_sum p q r w have hsc1 := det2_collinear_scale_left p q r w x hw0 have hsc2 := det2_collinear_scale_right p q r w x hw0 have hswap : det2 w x r = -det2 x w r := det2_swap_first x w r have hpw_nonneg : 0 ≤ det2 p w x := by nlinarith have hwq_nonneg : 0 ≤ det2 w q x := by nlinarith have hpw_pos_iff : 0 < det2 p w x ↔ 0 < det2 p q x := by constructor <;> intro h <;> nlinarith have hwq_pos_iff : 0 < det2 w q x ↔ 0 < det2 p q x := by constructor <;> intro h <;> nlinarith have hpw_zero_iff : det2 p w x = 0 ↔ det2 p q x = 0 := by constructor <;> intro h <;> nlinarith have hwq_zero_iff : det2 w q x = 0 ↔ det2 p q x = 0 := by constructor <;> intro h <;> nlinarith have hwr_ne : w ≠ r := by intro h; subst h; rw [det2_self_right] at hw1; omega -- Compare signs of det2 x w r against 0 rcases lt_trichotomy (det2 x w r) 0 with hneg | hzero | hpos · -- x is strictly in T2 \ T1 have hxw : x ≠ w := by intro h; subst x; rw [det2_self_left] at hneg; omega have hxr : x ≠ r := by intro h; subst x; rw [det2_self_outer] at hneg; omega have hnot1I : ¬ inTriInt p w r x := fun h => by have := h.1; omega have hnot1B : ¬ inTriBdy p w r x := fun h => by have := h.1.1; omega have hx2_pos : 0 < det2 p x r := by nlinarith have h2I_iff : inTriInt w q r x ↔ inTriInt p q r x := by unfold inTriInt constructor · intro ⟨a, _, c⟩; exact ⟨a, hx2_pos, hwq_pos_iff.mp c⟩ · intro ⟨a, _, c⟩; exact ⟨a, by omega, hwq_pos_iff.mpr c⟩ have h2B_iff : inTriBdy w q r x ↔ inTriBdy p q r x := by unfold inTriBdy inTri constructor · rintro ⟨⟨_, _, _⟩, h0 | h0 | h0⟩ · exact ⟨⟨hx1, hx2, hx3⟩, Or.inl h0⟩ · omega · exact ⟨⟨hx1, hx2, hx3⟩, Or.inr (Or.inr (hwq_zero_iff.mp h0))⟩ · rintro ⟨_, h0 | h0 | h0⟩ · exact ⟨⟨hx1, by omega, hwq_nonneg⟩, Or.inl h0⟩ · omega · exact ⟨⟨hx1, by omega, hwq_nonneg⟩, Or.inr (Or.inr (hwq_zero_iff.mpr h0))⟩ unfold triWeight simp [hxw, hxr, hnot1I, hnot1B, h2I_iff, h2B_iff] · -- det2 x w r = 0: x lies on the dividing segment [w, r] have hnot1I : ¬ inTriInt p w r x := fun h => by have := h.1; omega have hnot2I : ¬ inTriInt w q r x := fun h => by have := h.2.1; omega have h1B : inTriBdy p w r x := ⟨⟨by omega, hx2, hpw_nonneg⟩, Or.inl hzero⟩ have h2B : inTriBdy w q r x := ⟨⟨hx1, by omega, hwq_nonneg⟩, Or.inr (Or.inl (by omega))⟩ by_cases hxw : x = w · subst x have hnotI : ¬ inTriInt p q r w := fun h => by have := h.2.2; omega have hB : inTriBdy p q r w := ⟨⟨hx1, hx2, hx3⟩, Or.inr (Or.inr hw0)⟩ unfold triWeight simp [hwr_ne, hnot1I, hnot2I, h1B, h2B, hnotI, hB] · by_cases hxr : x = r · subst x have hnotI : ¬ inTriInt p q r r := fun h => by have := h.1; rw [det2_self_outer] at this; omega have hB : inTriBdy p q r r := ⟨⟨hx1, hx2, hx3⟩, Or.inl (det2_self_outer r q)⟩ unfold triWeight simp [hxw, hnot1I, hnot2I, h1B, h2B, hnotI, hB] · -- x is strictly inside open segment (w, r), hence strictly interior to (p, q, r)! have h3_pos : 0 < det2 p q x := by by_contra h30 have h3eq : det2 p q x = 0 := by omega have hpw0 : det2 p w x = 0 := hpw_zero_iff.mpr h3eq exact hxw (eq_second_of_det2_zero p w r x (ne_of_gt hw1) hzero hpw0) have h2_pos : 0 < det2 p x r := by by_contra h20 have h2eq : det2 p x r = 0 := by omega exact hxr (eq_third_of_det2_zero p w r x (ne_of_gt hw1) hzero h2eq) have h1_pos : 0 < det2 x q r := by by_contra h10 have h1eq : det2 x q r = 0 := by omega exact hxr (eq_third_of_det2_zero w q r x (ne_of_gt hw2) h1eq (by omega)) have hI : inTriInt p q r x := ⟨h1_pos, h2_pos, h3_pos⟩ unfold triWeight simp [hxw, hxr, hnot1I, hnot2I, h1B, h2B, hI] · -- det2 x w r > 0: x is strictly in T1 \ T2 have hxw : x ≠ w := by intro h; subst x; rw [det2_self_left] at hpos; omega have hxr : x ≠ r := by intro h; subst x; rw [det2_self_outer] at hpos; omega have hnot2I : ¬ inTriInt w q r x := fun h => by have := h.2.1; omega have hnot2B : ¬ inTriBdy w q r x := fun h => by have := h.1.2.1; omega have hx1_pos : 0 < det2 x q r := by nlinarith have h1I_iff : inTriInt p w r x ↔ inTriInt p q r x := by unfold inTriInt constructor · intro ⟨_, b, c⟩; exact ⟨hx1_pos, b, hpw_pos_iff.mp c⟩ · intro ⟨_, b, c⟩; exact ⟨hpos, b, hpw_pos_iff.mpr c⟩ have h1B_iff : inTriBdy p w r x ↔ inTriBdy p q r x := by unfold inTriBdy inTri constructor · rintro ⟨⟨_, _, _⟩, h0 | h0 | h0⟩ · omega · exact ⟨⟨hx1, hx2, hx3⟩, Or.inr (Or.inl h0)⟩ · exact ⟨⟨hx1, hx2, hx3⟩, Or.inr (Or.inr (hpw_zero_iff.mp h0))⟩ · rintro ⟨_, h0 | h0 | h0⟩ · omega · exact ⟨⟨le_of_lt hpos, hx2, hpw_nonneg⟩, Or.inr (Or.inl h0)⟩ · exact ⟨⟨le_of_lt hpos, hx2, hpw_nonneg⟩, Or.inr (Or.inr (hpw_zero_iff.mpr h0))⟩ unfold triWeight simp [hxw, hxr, hnot2I, hnot2B, h1I_iff, h1B_iff] have hw_mem : w ∈ triPts p q r := (mem_triPts hD).mpr ⟨le_of_lt hw2, le_of_lt hw1, le_of_eq hw0.symm⟩ have hr_mem : r ∈ triPts p q r := (mem_triPts hD).mpr ⟨by rw [det2_self_outer], by rw [det2_self_right], le_of_lt hD⟩ have hsum_all := Finset.sum_congr rfl hptwise rw [Finset.sum_add_distrib, Finset.sum_add_distrib, Finset.sum_add_distrib, sum_triWeight_eq hw1 (triPts p q r) hsub1, sum_triWeight_eq hw2 (triPts p q r) hsub2, sum_triWeight_eq hD (triPts p q r) (Finset.Subset.refl _), Finset.sum_ite_eq' (triPts p q r) w (fun _ => (1 : ℤ)), Finset.sum_ite_eq' (triPts p q r) r (fun _ => (1 : ℤ))] at hsum_all simp only [hw_mem, hr_mem, ↓reduceIte] at hsum_all unfold pickVal linarith private lemma triWeight_interior_split_s1_neg {p q r w x : ℤ × ℤ} (hD : 0 < det2 p q r) (hw1 : 0 < det2 w q r) (hw2 : 0 < det2 p w r) (hw3 : 0 < det2 p q w) (hx1 : 0 ≤ det2 x q r) (hx2 : 0 ≤ det2 p x r) (hx3 : 0 ≤ det2 p q x) (hxr : x ≠ r) (hs1_neg : det2 p x w < 0) : triWeight p q w x + triWeight q r w x + triWeight r p w x = triWeight p q r x := by have hD2 : 0 < det2 q r w := by rw [det2_cyclic2 q r w]; exact hw1 have hD3 : 0 < det2 r p w := by rw [det2_cyclic r p w]; exact hw2 set s₁ : ℤ := det2 p x w set s₂ : ℤ := det2 q x w set s₃ : ℤ := det2 r x w have hbal : det2 w q r * s₁ + det2 p w r * s₂ + det2 p q w * s₃ = 0 := det2_interior_balance p q r w x have hneg1 : det2 x p w = -s₁ := det2_swap_first p x w have hneg2 : det2 x q w = -s₂ := det2_swap_first q x w have hneg3 : det2 x r w = -s₃ := det2_swap_first r x w have hqrx : det2 q r x = det2 x q r := det2_cyclic2 q r x have hrpx : det2 r p x = det2 p x r := det2_cyclic r p x have eq2a : det2 q r w * det2 p x r = det2 p w r * det2 x q r + det2 p q r * (-s₃) := by rw [← hneg3, ← hqrx]; unfold det2; ring have eq2b : det2 q r w * det2 p q x = det2 p q w * det2 x q r + det2 p q r * s₂ := by rw [← hqrx]; unfold s₂ det2; ring have eq3a : det2 r p w * det2 x q r = det2 w q r * det2 p x r + det2 p q r * s₃ := by rw [← hrpx]; unfold s₃ det2; ring have eq3b : det2 r p w * det2 p q x = det2 p q w * det2 p x r + det2 p q r * (-s₁) := by rw [← hneg1, ← hrpx]; unfold det2; ring have hT1_int : inTriInt p q w x ↔ 0 < s₁ ∧ s₂ < 0 ∧ 0 < det2 p q x := by unfold inTriInt; rw [hneg2]; constructor <;> intro ⟨a, b, c⟩ <;> exact ⟨by omega, by omega, c⟩ have hT1_in : inTri p q w x ↔ 0 ≤ s₁ ∧ s₂ ≤ 0 := by unfold inTri; rw [hneg2]; constructor · intro ⟨a, b, _⟩; exact ⟨b, by omega⟩ · intro ⟨a, b⟩; exact ⟨by omega, a, hx3⟩ have hT2_int : inTriInt q r w x ↔ 0 < s₂ ∧ s₃ < 0 ∧ 0 < det2 x q r := by unfold inTriInt; rw [hneg3, hqrx]; constructor <;> intro ⟨a, b, c⟩ <;> exact ⟨by omega, by omega, c⟩ have hT2_in : inTri q r w x ↔ 0 ≤ s₂ ∧ s₃ ≤ 0 := by unfold inTri; rw [hneg3, hqrx]; constructor · intro ⟨a, b, _⟩; exact ⟨b, by omega⟩ · intro ⟨a, b⟩; exact ⟨by omega, a, hx1⟩ have hT3_int : inTriInt r p w x ↔ 0 < s₃ ∧ s₁ < 0 ∧ 0 < det2 p x r := by unfold inTriInt; rw [hneg1, hrpx]; constructor <;> intro ⟨a, b, c⟩ <;> exact ⟨by omega, by omega, c⟩ have hT3_in : inTri r p w x ↔ 0 ≤ s₃ ∧ s₁ ≤ 0 := by unfold inTri; rw [hneg1, hrpx]; constructor · intro ⟨a, b, _⟩; exact ⟨b, by omega⟩ · intro ⟨a, b⟩; exact ⟨by omega, a, hx2⟩ have hn1I : ¬ inTriInt p q w x := by rw [hT1_int]; omega have hn1B : ¬ inTriBdy p q w x := fun h => by have := hT1_in.mp h.1; omega rcases lt_trichotomy s₃ 0 with hs3_neg | hs3_zero | hs3_pos · have hs2_pos : 0 < s₂ := by nlinarith have hn3I : ¬ inTriInt r p w x := by rw [hT3_int]; omega have hn3B : ¬ inTriBdy r p w x := fun h => by have := hT3_in.mp h.1; omega have hx2_pos : 0 < det2 p x r := by nlinarith have hx3_pos : 0 < det2 p q x := by nlinarith have h2I_iff : inTriInt q r w x ↔ inTriInt p q r x := by rw [hT2_int]; unfold inTriInt; omega have h2B_iff : inTriBdy q r w x ↔ inTriBdy p q r x := by unfold inTriBdy; rw [hT2_in, hneg3, hqrx] constructor · rintro ⟨_, h0 | h0 | h0⟩ <;> [omega; omega; exact ⟨⟨hx1, hx2, hx3⟩, Or.inl h0⟩] · rintro ⟨_, h0 | h0 | h0⟩ <;> [exact ⟨⟨le_of_lt hs2_pos, le_of_lt hs3_neg⟩, Or.inr (Or.inr h0)⟩; omega; omega] unfold triWeight; simp [hn1I, hn1B, hn3I, hn3B, h2I_iff, h2B_iff] · have hs2_pos : 0 < s₂ := by nlinarith have hn2I : ¬ inTriInt q r w x := by rw [hT2_int]; omega have hn3I : ¬ inTriInt r p w x := by rw [hT3_int]; omega have h2B : inTriBdy q r w x := ⟨hT2_in.mpr ⟨le_of_lt hs2_pos, by omega⟩, Or.inl (by rw [hneg3]; omega)⟩ have h3B : inTriBdy r p w x := ⟨hT3_in.mpr ⟨by omega, le_of_lt hs1_neg⟩, Or.inr (Or.inl hs3_zero)⟩ have hx1_pos : 0 < det2 x q r := by by_contra h0; have hx10 : det2 x q r = 0 := by omega have hx20 : det2 p x r = 0 := by nlinarith exact hxr (eq_third_of_det2_zero p q r x (ne_of_gt hD) hx10 hx20) have hx2_pos : 0 < det2 p x r := by nlinarith have hx3_pos : 0 < det2 p q x := by nlinarith have hI : inTriInt p q r x := ⟨hx1_pos, hx2_pos, hx3_pos⟩ unfold triWeight; simp [hn1I, hn1B, hn2I, hn3I, h2B, h3B, hI] · have hn2I : ¬ inTriInt q r w x := by rw [hT2_int]; omega have hn2B : ¬ inTriBdy q r w x := fun h => by have := hT2_in.mp h.1; omega have hx1_pos : 0 < det2 x q r := by nlinarith have hx3_pos : 0 < det2 p q x := by nlinarith have h3I_iff : inTriInt r p w x ↔ inTriInt p q r x := by rw [hT3_int]; unfold inTriInt; omega have h3B_iff : inTriBdy r p w x ↔ inTriBdy p q r x := by unfold inTriBdy; rw [hT3_in, hneg1, hrpx] constructor · rintro ⟨_, h0 | h0 | h0⟩ <;> [omega; omega; exact ⟨⟨hx1, hx2, hx3⟩, Or.inr (Or.inl h0)⟩] · rintro ⟨_, h0 | h0 | h0⟩ <;> [omega; exact ⟨⟨le_of_lt hs3_pos, le_of_lt hs1_neg⟩, Or.inr (Or.inr h0)⟩; omega] unfold triWeight; simp [hn1I, hn1B, hn2I, hn2B, h3I_iff, h3B_iff] private lemma triWeight_interior_split_s1_zero {p q r w x : ℤ × ℤ} (hD : 0 < det2 p q r) (hw1 : 0 < det2 w q r) (hw2 : 0 < det2 p w r) (hw3 : 0 < det2 p q w) (hx1 : 0 ≤ det2 x q r) (hx2 : 0 ≤ det2 p x r) (hx3 : 0 ≤ det2 p q x) (hxw : x ≠ w) (hxp : x ≠ p) (hs1_zero : det2 p x w = 0) : triWeight p q w x + triWeight q r w x + triWeight r p w x = triWeight p q r x := by have hD1 : 0 < det2 p q w := hw3 have hD2 : 0 < det2 q r w := by rw [det2_cyclic2 q r w]; exact hw1 have hD3 : 0 < det2 r p w := by rw [det2_cyclic r p w]; exact hw2 set s₁ : ℤ := det2 p x w set s₂ : ℤ := det2 q x w set s₃ : ℤ := det2 r x w have hbal : det2 w q r * s₁ + det2 p w r * s₂ + det2 p q w * s₃ = 0 := det2_interior_balance p q r w x have hneg1 : det2 x p w = -s₁ := det2_swap_first p x w have hneg2 : det2 x q w = -s₂ := det2_swap_first q x w have hneg3 : det2 x r w = -s₃ := det2_swap_first r x w have hqrx : det2 q r x = det2 x q r := det2_cyclic2 q r x have hrpx : det2 r p x = det2 p x r := det2_cyclic r p x have eq1a : det2 p q w * det2 x q r = det2 w q r * det2 p q x + det2 p q r * (-s₂) := by rw [← hneg2]; unfold det2; ring have eq2a : det2 q r w * det2 p x r = det2 p w r * det2 x q r + det2 p q r * (-s₃) := by rw [← hneg3, ← hqrx]; unfold det2; ring have eq2b : det2 q r w * det2 p q x = det2 p q w * det2 x q r + det2 p q r * s₂ := by rw [← hqrx]; unfold s₂ det2; ring have eq3a : det2 r p w * det2 x q r = det2 w q r * det2 p x r + det2 p q r * s₃ := by rw [← hrpx]; unfold s₃ det2; ring have eq3b : det2 r p w * det2 p q x = det2 p q w * det2 p x r + det2 p q r * (-s₁) := by rw [← hneg1, ← hrpx]; unfold det2; ring have hnot_all0 : ¬ (s₁ = 0 ∧ s₂ = 0) := by rintro ⟨h01, h02⟩ exact hxw (eq_third_of_det2_zero p q w x (ne_of_gt hD1) (by rw [hneg2]; omega) h01) have hT1_int : inTriInt p q w x ↔ 0 < s₁ ∧ s₂ < 0 ∧ 0 < det2 p q x := by unfold inTriInt; rw [hneg2]; constructor <;> intro ⟨a, b, c⟩ <;> exact ⟨by omega, by omega, c⟩ have hT1_in : inTri p q w x ↔ 0 ≤ s₁ ∧ s₂ ≤ 0 := by unfold inTri; rw [hneg2]; constructor · intro ⟨a, b, _⟩; exact ⟨b, by omega⟩ · intro ⟨a, b⟩; exact ⟨by omega, a, hx3⟩ have hT2_int : inTriInt q r w x ↔ 0 < s₂ ∧ s₃ < 0 ∧ 0 < det2 x q r := by unfold inTriInt; rw [hneg3, hqrx]; constructor <;> intro ⟨a, b, c⟩ <;> exact ⟨by omega, by omega, c⟩ have hT2_in : inTri q r w x ↔ 0 ≤ s₂ ∧ s₃ ≤ 0 := by unfold inTri; rw [hneg3, hqrx]; constructor · intro ⟨a, b, _⟩; exact ⟨b, by omega⟩ · intro ⟨a, b⟩; exact ⟨by omega, a, hx1⟩ have hT3_int : inTriInt r p w x ↔ 0 < s₃ ∧ s₁ < 0 ∧ 0 < det2 p x r := by unfold inTriInt; rw [hneg1, hrpx]; constructor <;> intro ⟨a, b, c⟩ <;> exact ⟨by omega, by omega, c⟩ have hT3_in : inTri r p w x ↔ 0 ≤ s₃ ∧ s₁ ≤ 0 := by unfold inTri; rw [hneg1, hrpx]; constructor · intro ⟨a, b, _⟩; exact ⟨b, by omega⟩ · intro ⟨a, b⟩; exact ⟨by omega, a, hx2⟩ rcases lt_trichotomy s₂ 0 with hs2_neg | hs2_zero | hs2_pos · have hs3_pos : 0 < s₃ := by nlinarith have hn1I : ¬ inTriInt p q w x := by rw [hT1_int]; omega have hn2I : ¬ inTriInt q r w x := by rw [hT2_int]; omega have hn2B : ¬ inTriBdy q r w x := fun h => by have := hT2_in.mp h.1; omega have hn3I : ¬ inTriInt r p w x := by rw [hT3_int]; omega have h1B : inTriBdy p q w x := ⟨hT1_in.mpr ⟨by omega, le_of_lt hs2_neg⟩, Or.inr (Or.inl hs1_zero)⟩ have h3B : inTriBdy r p w x := ⟨hT3_in.mpr ⟨le_of_lt hs3_pos, by omega⟩, Or.inl (by rw [hneg1]; omega)⟩ have hx3_pos : 0 < det2 p q x := by by_contra h0; have hx30 : det2 p q x = 0 := by omega have hx20 : det2 p x r = 0 := by nlinarith exact hxp (eq_first_of_det2_zero p q r x (ne_of_gt hD) hx20 hx30) have hx1_pos : 0 < det2 x q r := by nlinarith have hx2_pos : 0 < det2 p x r := by nlinarith have hI : inTriInt p q r x := ⟨hx1_pos, hx2_pos, hx3_pos⟩ unfold triWeight; simp [hn1I, hn2I, hn2B, hn3I, h1B, h3B, hI] · exact absurd ⟨hs1_zero, hs2_zero⟩ hnot_all0 · have hs3_neg : s₃ < 0 := by nlinarith have hn1I : ¬ inTriInt p q w x := by rw [hT1_int]; omega have hn1B : ¬ inTriBdy p q w x := fun h => by have := hT1_in.mp h.1; omega have hn3I : ¬ inTriInt r p w x := by rw [hT3_int]; omega have hn3B : ¬ inTriBdy r p w x := fun h => by have := hT3_in.mp h.1; omega have hx2_pos : 0 < det2 p x r := by nlinarith have hx3_pos : 0 < det2 p q x := by nlinarith have h2I_iff : inTriInt q r w x ↔ inTriInt p q r x := by rw [hT2_int]; unfold inTriInt; omega have h2B_iff : inTriBdy q r w x ↔ inTriBdy p q r x := by unfold inTriBdy; rw [hT2_in, hneg3, hqrx] constructor · rintro ⟨_, h0 | h0 | h0⟩ <;> [omega; omega; exact ⟨⟨hx1, hx2, hx3⟩, Or.inl h0⟩] · rintro ⟨_, h0 | h0 | h0⟩ <;> [exact ⟨⟨le_of_lt hs2_pos, le_of_lt hs3_neg⟩, Or.inr (Or.inr h0)⟩; omega; omega] unfold triWeight; simp [hn1I, hn1B, hn3I, hn3B, h2I_iff, h2B_iff] private lemma triWeight_interior_split_s1_pos {p q r w x : ℤ × ℤ} (hD : 0 < det2 p q r) (hw1 : 0 < det2 w q r) (hw2 : 0 < det2 p w r) (hw3 : 0 < det2 p q w) (hx1 : 0 ≤ det2 x q r) (hx2 : 0 ≤ det2 p x r) (hx3 : 0 ≤ det2 p q x) (hxq : x ≠ q) (hs1_pos : 0 < det2 p x w) : triWeight p q w x + triWeight q r w x + triWeight r p w x = triWeight p q r x := by have hD2 : 0 < det2 q r w := by rw [det2_cyclic2 q r w]; exact hw1 set s₁ : ℤ := det2 p x w set s₂ : ℤ := det2 q x w set s₃ : ℤ := det2 r x w have hbal : det2 w q r * s₁ + det2 p w r * s₂ + det2 p q w * s₃ = 0 := det2_interior_balance p q r w x have hneg1 : det2 x p w = -s₁ := det2_swap_first p x w have hneg2 : det2 x q w = -s₂ := det2_swap_first q x w have hneg3 : det2 x r w = -s₃ := det2_swap_first r x w have hqrx : det2 q r x = det2 x q r := det2_cyclic2 q r x have hrpx : det2 r p x = det2 p x r := det2_cyclic r p x have eq1a : det2 p q w * det2 x q r = det2 w q r * det2 p q x + det2 p q r * (-s₂) := by rw [← hneg2]; unfold det2; ring have eq1b : det2 p q w * det2 p x r = det2 p w r * det2 p q x + det2 p q r * s₁ := by unfold s₁ det2; ring have eq2a : det2 q r w * det2 p x r = det2 p w r * det2 x q r + det2 p q r * (-s₃) := by rw [← hneg3, ← hqrx]; unfold det2; ring have eq2b : det2 q r w * det2 p q x = det2 p q w * det2 x q r + det2 p q r * s₂ := by rw [← hqrx]; unfold s₂ det2; ring have hT1_int : inTriInt p q w x ↔ 0 < s₁ ∧ s₂ < 0 ∧ 0 < det2 p q x := by unfold inTriInt; rw [hneg2]; constructor <;> intro ⟨a, b, c⟩ <;> exact ⟨by omega, by omega, c⟩ have hT1_in : inTri p q w x ↔ 0 ≤ s₁ ∧ s₂ ≤ 0 := by unfold inTri; rw [hneg2]; constructor · intro ⟨a, b, _⟩; exact ⟨b, by omega⟩ · intro ⟨a, b⟩; exact ⟨by omega, a, hx3⟩ have hT2_int : inTriInt q r w x ↔ 0 < s₂ ∧ s₃ < 0 ∧ 0 < det2 x q r := by unfold inTriInt; rw [hneg3, hqrx]; constructor <;> intro ⟨a, b, c⟩ <;> exact ⟨by omega, by omega, c⟩ have hT2_in : inTri q r w x ↔ 0 ≤ s₂ ∧ s₃ ≤ 0 := by unfold inTri; rw [hneg3, hqrx]; constructor · intro ⟨a, b, _⟩; exact ⟨b, by omega⟩ · intro ⟨a, b⟩; exact ⟨by omega, a, hx1⟩ have hT3_int : inTriInt r p w x ↔ 0 < s₃ ∧ s₁ < 0 ∧ 0 < det2 p x r := by unfold inTriInt; rw [hneg1, hrpx]; constructor <;> intro ⟨a, b, c⟩ <;> exact ⟨by omega, by omega, c⟩ have hT3_in : inTri r p w x ↔ 0 ≤ s₃ ∧ s₁ ≤ 0 := by unfold inTri; rw [hneg1, hrpx]; constructor · intro ⟨a, b, _⟩; exact ⟨b, by omega⟩ · intro ⟨a, b⟩; exact ⟨by omega, a, hx2⟩ have hn3I : ¬ inTriInt r p w x := by rw [hT3_int]; omega have hn3B : ¬ inTriBdy r p w x := fun h => by have := hT3_in.mp h.1; omega rcases lt_trichotomy s₂ 0 with hs2_neg | hs2_zero | hs2_pos · have hn2I : ¬ inTriInt q r w x := by rw [hT2_int]; omega have hn2B : ¬ inTriBdy q r w x := fun h => by have := hT2_in.mp h.1; omega have hx1_pos : 0 < det2 x q r := by nlinarith have hx2_pos : 0 < det2 p x r := by nlinarith have h1I_iff : inTriInt p q w x ↔ inTriInt p q r x := by rw [hT1_int]; unfold inTriInt; omega have h1B_iff : inTriBdy p q w x ↔ inTriBdy p q r x := by unfold inTriBdy; rw [hT1_in, hneg2] constructor · rintro ⟨_, h0 | h0 | h0⟩ <;> [omega; omega; exact ⟨⟨hx1, hx2, hx3⟩, Or.inr (Or.inr h0)⟩] · rintro ⟨_, h0 | h0 | h0⟩ <;> [omega; omega; exact ⟨⟨le_of_lt hs1_pos, le_of_lt hs2_neg⟩, Or.inr (Or.inr h0)⟩] unfold triWeight; simp [hn2I, hn2B, hn3I, hn3B, h1I_iff, h1B_iff] · have hs3_neg : s₃ < 0 := by nlinarith have hn1I : ¬ inTriInt p q w x := by rw [hT1_int]; omega have hn2I : ¬ inTriInt q r w x := by rw [hT2_int]; omega have h1B : inTriBdy p q w x := ⟨hT1_in.mpr ⟨le_of_lt hs1_pos, by omega⟩, Or.inl (by rw [hneg2]; omega)⟩ have h2B : inTriBdy q r w x := ⟨hT2_in.mpr ⟨by omega, le_of_lt hs3_neg⟩, Or.inr (Or.inl hs2_zero)⟩ have hx1_pos : 0 < det2 x q r := by by_contra h0; have hx10 : det2 x q r = 0 := by omega have hx30 : det2 p q x = 0 := by nlinarith exact hxq (eq_second_of_det2_zero p q r x (ne_of_gt hD) hx10 hx30) have hx2_pos : 0 < det2 p x r := by nlinarith have hx3_pos : 0 < det2 p q x := by nlinarith have hI : inTriInt p q r x := ⟨hx1_pos, hx2_pos, hx3_pos⟩ unfold triWeight; simp [hn1I, hn2I, hn3I, hn3B, h1B, h2B, hI] · have hs3_neg : s₃ < 0 := by nlinarith have hn1I : ¬ inTriInt p q w x := by rw [hT1_int]; omega have hn1B : ¬ inTriBdy p q w x := fun h => by have := hT1_in.mp h.1; omega have hx2_pos : 0 < det2 p x r := by nlinarith have hx3_pos : 0 < det2 p q x := by nlinarith have h2I_iff : inTriInt q r w x ↔ inTriInt p q r x := by rw [hT2_int]; unfold inTriInt; omega have h2B_iff : inTriBdy q r w x ↔ inTriBdy p q r x := by unfold inTriBdy; rw [hT2_in, hneg3, hqrx] constructor · rintro ⟨_, h0 | h0 | h0⟩ <;> [omega; omega; exact ⟨⟨hx1, hx2, hx3⟩, Or.inl h0⟩] · rintro ⟨_, h0 | h0 | h0⟩ <;> [exact ⟨⟨le_of_lt hs2_pos, le_of_lt hs3_neg⟩, Or.inr (Or.inr h0)⟩; omega; omega] unfold triWeight; simp [hn1I, hn1B, hn3I, hn3B, h2I_iff, h2B_iff] /-- Interior-subdivision additivity of Pick's invariant `2I + B - 2`: if `w` is strictly interior to `(p, q, r)`, then `pickVal p q r = pickVal p q w + pickVal q r w + pickVal r p w`. -/ lemma pickVal_interior_split {p q r w : ℤ × ℤ} (hD : 0 < det2 p q r) (hw1 : 0 < det2 w q r) (hw2 : 0 < det2 p w r) (hw3 : 0 < det2 p q w) : pickVal p q r = pickVal p q w + pickVal q r w + pickVal r p w := by have hD1 : 0 < det2 p q w := hw3 have hD2 : 0 < det2 q r w := by rw [det2_cyclic2 q r w]; exact hw1 have hD3 : 0 < det2 r p w := by rw [det2_cyclic r p w]; exact hw2 have hsub1 : triPts p q w ⊆ triPts p q r := by intro x hx rw [mem_triPts hD1] at hx rw [mem_triPts hD] rcases hx with ⟨h1, h2, h3⟩ have eq_xqr : det2 p q w * det2 x q r = det2 w q r * det2 p q x + det2 p q r * det2 x q w := by unfold det2; ring have eq_pxr : det2 p q w * det2 p x r = det2 p w r * det2 p q x + det2 p q r * det2 p x w := by unfold det2; ring exact ⟨by nlinarith, by nlinarith, h3⟩ have hsub2 : triPts q r w ⊆ triPts p q r := by intro x hx rw [mem_triPts hD2] at hx rw [mem_triPts hD] rcases hx with ⟨h1, h2, h3⟩ have h3' : 0 ≤ det2 x q r := by rw [← det2_cyclic2 q r x]; exact h3 have eq_pxr : det2 q r w * det2 p x r = det2 p w r * det2 q r x + det2 p q r * det2 x r w := by unfold det2; ring have eq_pqx : det2 q r w * det2 p q x = det2 p q w * det2 q r x + det2 p q r * det2 q x w := by unfold det2; ring exact ⟨h3', by nlinarith, by nlinarith⟩ have hsub3 : triPts r p w ⊆ triPts p q r := by intro x hx rw [mem_triPts hD3] at hx rw [mem_triPts hD] rcases hx with ⟨h1, h2, h3⟩ have h3' : 0 ≤ det2 p x r := by rw [← det2_cyclic r p x]; exact h3 have eq_xqr : det2 r p w * det2 x q r = det2 w q r * det2 r p x + det2 p q r * det2 r x w := by unfold det2; ring have eq_pqx : det2 r p w * det2 p q x = det2 p q w * det2 r p x + det2 p q r * det2 x p w := by unfold det2; ring exact ⟨by nlinarith, h3', by nlinarith⟩ have hwp : w ≠ p := by intro h; subst h; rw [det2_self_outer] at hw3; omega have hwq : w ≠ q := by intro h; subst h; rw [det2_self_right] at hw3; omega have hwr : w ≠ r := by intro h; subst h; rw [det2_self_outer] at hw1; omega have hpq : p ≠ q := by intro h; subst h; rw [det2_self_left] at hD; omega have hqr : q ≠ r := by intro h; subst h; rw [det2_self_right] at hD; omega have hrp : r ≠ p := by intro h; subst h; rw [det2_self_outer] at hD; omega have hptwise : ∀ x ∈ triPts p q r, triWeight p q w x + triWeight q r w x + triWeight r p w x = triWeight p q r x + (if x = w then 1 else 0) + (if x = p then 1 else 0) + (if x = q then 1 else 0) + (if x = r then 1 else 0) := by intro x hx rcases (mem_triPts hD).mp hx with ⟨hx1, hx2, hx3⟩ by_cases hxw : x = w · subst x have hI : inTriInt p q r w := ⟨hw1, hw2, hw3⟩ have h1B : inTriBdy p q w w := ⟨⟨by rw [det2_self_outer], by rw [det2_self_right], le_of_lt hw3⟩, Or.inl (det2_self_outer w q)⟩ have h2B : inTriBdy q r w w := ⟨⟨by rw [det2_self_outer], by rw [det2_self_right], le_of_lt hD2⟩, Or.inl (det2_self_outer w r)⟩ have h3B : inTriBdy r p w w := ⟨⟨by rw [det2_self_outer], by rw [det2_self_right], le_of_lt hD3⟩, Or.inl (det2_self_outer w p)⟩ have h1nI : ¬ inTriInt p q w w := fun h => by have := h.1; rw [det2_self_outer] at this; omega have h2nI : ¬ inTriInt q r w w := fun h => by have := h.1; rw [det2_self_outer] at this; omega have h3nI : ¬ inTriInt r p w w := fun h => by have := h.1; rw [det2_self_outer] at this; omega unfold triWeight simp [hwp, hwq, hwr, hI, h1B, h2B, h3B, h1nI, h2nI, h3nI] · by_cases hxp : x = p · subst x have hB : inTriBdy p q r p := ⟨⟨le_of_lt hD, by rw [det2_self_left], by rw [det2_self_outer]⟩, Or.inr (Or.inl (det2_self_left p r))⟩ have hnI : ¬ inTriInt p q r p := fun h => by have := h.2.1; rw [det2_self_left] at this; omega have h1B : inTriBdy p q w p := ⟨⟨le_of_lt hD1, by rw [det2_self_left], by rw [det2_self_outer]⟩, Or.inr (Or.inl (det2_self_left p w))⟩ have h1nI : ¬ inTriInt p q w p := fun h => by have := h.2.1; rw [det2_self_left] at this; omega have h3B : inTriBdy r p w p := ⟨⟨by rw [det2_self_left], le_of_lt hD3, by rw [det2_self_right]⟩, Or.inl (det2_self_left p w)⟩ have h3nI : ¬ inTriInt r p w p := fun h => by have := h.1; rw [det2_self_left] at this; omega have h2nI : ¬ inTriInt q r w p := fun h => by have := h.2.1; change 0 < det2 q p w at this; rw [det2_swap_first] at this; omega have h2nB : ¬ inTriBdy q r w p := fun h => by have := h.1.2.1; change 0 ≤ det2 q p w at this; rw [det2_swap_first] at this; omega unfold triWeight simp [hwp.symm, hpq, hrp.symm, hB, hnI, h1B, h1nI, h3B, h3nI, h2nI, h2nB] · by_cases hxq : x = q · subst x have hB : inTriBdy p q r q := ⟨⟨by rw [det2_self_left], le_of_lt hD, by rw [det2_self_right]⟩, Or.inl (det2_self_left q r)⟩ have hnI : ¬ inTriInt p q r q := fun h => by have := h.1; rw [det2_self_left] at this; omega have h1B : inTriBdy p q w q := ⟨⟨by rw [det2_self_left], le_of_lt hD1, by rw [det2_self_right]⟩, Or.inl (det2_self_left q w)⟩ have h1nI : ¬ inTriInt p q w q := fun h => by have := h.1; rw [det2_self_left] at this; omega have h2B : inTriBdy q r w q := ⟨⟨le_of_lt hD2, by rw [det2_self_left], by rw [det2_self_outer]⟩, Or.inr (Or.inl (det2_self_left q w))⟩ have h2nI : ¬ inTriInt q r w q := fun h => by have := h.2.1; rw [det2_self_left] at this; omega have h3nI : ¬ inTriInt r p w q := fun h => by have := h.2.1; change 0 < det2 r q w at this; rw [det2_swap_first] at this; omega have h3nB : ¬ inTriBdy r p w q := fun h => by have := h.1.2.1; change 0 ≤ det2 r q w at this; rw [det2_swap_first] at this; omega unfold triWeight simp [hwq.symm, hpq.symm, hqr, hB, hnI, h1B, h1nI, h2B, h2nI, h3nI, h3nB] · by_cases hxr : x = r · subst x have hB : inTriBdy p q r r := ⟨⟨by rw [det2_self_outer], by rw [det2_self_right], le_of_lt hD⟩, Or.inl (det2_self_outer r q)⟩ have hnI : ¬ inTriInt p q r r := fun h => by have := h.1; rw [det2_self_outer] at this; omega have h2B : inTriBdy q r w r := ⟨⟨by rw [det2_self_left], le_of_lt hD2, by rw [det2_self_right]⟩, Or.inl (det2_self_left r w)⟩ have h2nI : ¬ inTriInt q r w r := fun h => by have := h.1; rw [det2_self_left] at this; omega have h3B : inTriBdy r p w r := ⟨⟨le_of_lt hD3, by rw [det2_self_left], by rw [det2_self_outer]⟩, Or.inr (Or.inl (det2_self_left r w))⟩ have h3nI : ¬ inTriInt r p w r := fun h => by have := h.2.1; rw [det2_self_left] at this; omega have h1nI : ¬ inTriInt p q w r := fun h => by have := h.2.1; change 0 < det2 p r w at this; rw [det2_swap_last] at this; omega have h1nB : ¬ inTriBdy p q w r := fun h => by have := h.1.2.1; change 0 ≤ det2 p r w at this; rw [det2_swap_last] at this; omega unfold triWeight simp [hwr.symm, hrp, hqr.symm, hB, hnI, h2B, h2nI, h3B, h3nI, h1nI, h1nB] · simp only [hxw, hxp, hxq, hxr, ↓reduceIte, add_zero] rcases lt_trichotomy (det2 p x w) 0 with hs1_neg | hs1_zero | hs1_pos · exact triWeight_interior_split_s1_neg hD hw1 hw2 hw3 hx1 hx2 hx3 hxr hs1_neg · exact triWeight_interior_split_s1_zero hD hw1 hw2 hw3 hx1 hx2 hx3 hxw hxp hs1_zero · exact triWeight_interior_split_s1_pos hD hw1 hw2 hw3 hx1 hx2 hx3 hxq hs1_pos have hw_mem : w ∈ triPts p q r := (mem_triPts hD).mpr ⟨le_of_lt hw1, le_of_lt hw2, le_of_lt hw3⟩ have hp_mem : p ∈ triPts p q r := (mem_triPts hD).mpr ⟨le_of_lt hD, by rw [det2_self_left], by rw [det2_self_outer]⟩ have hq_mem : q ∈ triPts p q r := (mem_triPts hD).mpr ⟨by rw [det2_self_left], le_of_lt hD, by rw [det2_self_right]⟩ have hr_mem : r ∈ triPts p q r := (mem_triPts hD).mpr ⟨by rw [det2_self_outer], by rw [det2_self_right], le_of_lt hD⟩ have hsum_all := Finset.sum_congr rfl hptwise simp only [Finset.sum_add_distrib] at hsum_all rw [sum_triWeight_eq hD1 (triPts p q r) hsub1, sum_triWeight_eq hD2 (triPts p q r) hsub2, sum_triWeight_eq hD3 (triPts p q r) hsub3, sum_triWeight_eq hD (triPts p q r) (Finset.Subset.refl _), Finset.sum_ite_eq' (triPts p q r) w (fun _ => (1 : ℤ)), Finset.sum_ite_eq' (triPts p q r) p (fun _ => (1 : ℤ)), Finset.sum_ite_eq' (triPts p q r) q (fun _ => (1 : ℤ)), Finset.sum_ite_eq' (triPts p q r) r (fun _ => (1 : ℤ))] at hsum_all simp only [hw_mem, hp_mem, hq_mem, hr_mem, ↓reduceIte] at hsum_all unfold pickVal linarith lemma pickVal_cyclic {p q r : ℤ × ℤ} (hD : 0 < det2 p q r) : pickVal p q r = pickVal q r p := by unfold pickVal rw [triIntPts_cyclic hD, triBdyPts_cyclic hD] /-- **Pick's Theorem for Every Lattice Triangle** (unconditional): For every positively oriented lattice triangle `(p, q, r)` in `ℤ²`, `det2 p q r = 2 * I + B - 2`, i.e., `Area(T) = I + B / 2 - 1`. -/ theorem picks_theorem_triangle : ∀ n : ℕ, ∀ p q r : ℤ × ℤ, det2 p q r = (n : ℤ) → 0 < n → det2 p q r = 2 * (triIntPts p q r).card + (triBdyPts p q r).card - 2 := by intro n induction n using Nat.strongRecOn with | ind n ih => intro p q r hDn hn have hD : 0 < det2 p q r := by omega by_cases hn1 : n = 1 · have h1 : det2 p q r = 1 := by omega obtain ⟨hI, hB⟩ := (primitive_triangle_unimodular hD).mp h1 rw [h1, hI, hB]; norm_num · have hge2 : 2 ≤ det2 p q r := by omega obtain ⟨w, ⟨hw1, hw2, hw3⟩, hwp, hwq, hwr⟩ := exists_extra_point_of_det2_ge_two hge2 have hsum := det2_barycentric_sum p q r w change det2 p q r = pickVal p q r by_cases h3 : det2 p q w = 0 · -- w on open edge (p, q) have hw1_pos : 0 < det2 w q r := by by_contra h0; exact hwq (eq_second_of_det2_zero p q r w (ne_of_gt hD) (by omega) h3) have hw2_pos : 0 < det2 p w r := by by_contra h0; exact hwp (eq_first_of_det2_zero p q r w (ne_of_gt hD) (by omega) h3) have hlt1 : (det2 p w r).toNat < n := by omega have hlt2 : (det2 w q r).toNat < n := by omega have ih1 := ih (det2 p w r).toNat hlt1 p w r (by omega) (by omega) have ih2 := ih (det2 w q r).toNat hlt2 w q r (by omega) (by omega) rw [pickVal_edge_split hD h3 hw2_pos hw1_pos] unfold pickVal at *; omega · by_cases h1 : det2 w q r = 0 · -- w on open edge (q, r): rotate (p, q, r) -> (q, r, p) have hD' : 0 < det2 q r p := by rw [← det2_cyclic]; exact hD have h1' : det2 q r w = 0 := by rw [det2_cyclic2 q r w]; exact h1 have hw2_pos : 0 < det2 q w p := by rw [det2_cyclic2 q w p] by_contra h0; exact hwq (eq_second_of_det2_zero p q r w (ne_of_gt hD) h1 (by omega)) have hw3_pos : 0 < det2 w r p := by rw [det2_cyclic2 w r p] by_contra h0; exact hwr (eq_third_of_det2_zero p q r w (ne_of_gt hD) h1 (by omega)) have hsum' := det2_barycentric_sum q r p w have hDq : det2 q r p = det2 p q r := (det2_cyclic p q r).symm have hlt1 : (det2 q w p).toNat < n := by omega have hlt2 : (det2 w r p).toNat < n := by omega have ih1 := ih (det2 q w p).toNat hlt1 q w p (by omega) (by omega) have ih2 := ih (det2 w r p).toNat hlt2 w r p (by omega) (by omega) rw [pickVal_cyclic hD, pickVal_edge_split hD' h1' hw2_pos hw3_pos, det2_cyclic p q r] unfold pickVal at *; omega · by_cases h2 : det2 p w r = 0 · -- w on open edge (r, p): rotate (p, q, r) -> (r, p, q) have hD' : 0 < det2 r p q := by rw [← det2_cyclic2]; exact hD have h2' : det2 r p w = 0 := by rw [det2_cyclic r p w]; exact h2 have hw1_pos : 0 < det2 r w q := by rw [det2_cyclic r w q] by_contra h0; exact hwr (eq_third_of_det2_zero p q r w (ne_of_gt hD) (by omega) h2) have hw3_pos : 0 < det2 w p q := by rw [det2_cyclic w p q] by_contra h0; exact hwp (eq_first_of_det2_zero p q r w (ne_of_gt hD) h2 (by omega)) have hsum' := det2_barycentric_sum r p q w have hDr : det2 r p q = det2 p q r := (det2_cyclic2 p q r).symm have hlt1 : (det2 r w q).toNat < n := by omega have hlt2 : (det2 w p q).toNat < n := by omega have ih1 := ih (det2 r w q).toNat hlt1 r w q (by omega) (by omega) have ih2 := ih (det2 w p q).toNat hlt2 w p q (by omega) (by omega) have hcyc : pickVal p q r = pickVal r p q := by rw [pickVal_cyclic hD, pickVal_cyclic (by rw [← det2_cyclic]; exact hD)] rw [hcyc, pickVal_edge_split hD' h2' hw1_pos hw3_pos, det2_cyclic2 p q r] unfold pickVal at *; omega · -- w is strictly interior to (p, q, r) have hw1_pos : 0 < det2 w q r := by omega have hw2_pos : 0 < det2 p w r := by omega have hw3_pos : 0 < det2 p q w := by omega have hD2 : det2 q r w = det2 w q r := det2_cyclic2 q r w have hD3 : det2 r p w = det2 p w r := det2_cyclic r p w have hlt1 : (det2 p q w).toNat < n := by omega have hlt2 : (det2 q r w).toNat < n := by omega have hlt3 : (det2 r p w).toNat < n := by omega have ih1 := ih (det2 p q w).toNat hlt1 p q w (by omega) (by omega) have ih2 := ih (det2 q r w).toNat hlt2 q r w (by omega) (by omega) have ih3 := ih (det2 r p w).toNat hlt3 r p w (by omega) (by omega) rw [pickVal_interior_split hD hw1_pos hw2_pos hw3_pos] unfold pickVal at *; omega /-- Clean rational form of Pick's Theorem for every positively oriented lattice triangle: `Area(T) = I + B/2 - 1`. -/ theorem picks_theorem_triangle_rat {p q r : ℤ × ℤ} (hD : 0 < det2 p q r) : (det2 p q r : ℚ) / 2 = (triIntPts p q r).card + ((triBdyPts p q r).card : ℚ) / 2 - 1 := by have h := picks_theorem_triangle (det2 p q r).toNat p q r (by omega) (by omega) have hQ : (det2 p q r : ℚ) = 2 * (triIntPts p q r).card + (triBdyPts p q r).card - 2 := by exact_mod_cast h linarith /-! ## Section 5: Constructive Pick's Theorem for Triangulations & 2D Ehrhart–Pick Equivalence -/ /-- A combinatorial primitive (unimodular) lattice triangulation of a simply connected lattice polygon, recording face count `F`, interior/boundary edges `E_int, E_bdy`, and interior/boundary lattice points `I, B`. -/ structure PrimitiveLatticeTriangulation where F : ℕ E_int : ℕ E_bdy : ℕ I : ℕ B : ℕ /-- Each triangle has 3 edges; interior edges belong to 2 faces, boundary edges to 1. -/ incidence : 3 * F = 2 * E_int + E_bdy /-- Euler's formula `V - E + F = 1` for a topological disk. -/ euler : (I + B : ℤ) - (E_int + E_bdy : ℤ) + (F : ℤ) = 1 /-- Boundary is a simple closed cycle of `B` lattice points and `B` boundary edges. -/ bdy_cycle : E_bdy = B /-- **Constructive Pick's Theorem for Lattice Triangulations**: In any primitive (unimodular) triangulation of a simply connected lattice polygon (where each primitive triangle has `2 * Area = 1`, so `2 * Area(P) = F`), Euler's formula gives `2 * Area(P) = 2 * I + B - 2`, i.e., `Area(P) = I + B / 2 - 1`. -/ theorem picks_theorem_triangulation (T : PrimitiveLatticeTriangulation) : (T.F : ℤ) = 2 * (T.I : ℤ) + (T.B : ℤ) - 2 ∧ (T.F : ℚ) / 2 = (T.I : ℚ) + (T.B : ℚ) / 2 - 1 := by have h1 : (T.F : ℤ) = 2 * (T.I : ℤ) + (T.B : ℤ) - 2 := by have hinc : (3 * T.F : ℤ) = 2 * (T.E_int : ℤ) + (T.E_bdy : ℤ) := by exact_mod_cast T.incidence have hbdy : (T.E_bdy : ℤ) = (T.B : ℤ) := by exact_mod_cast T.bdy_cycle have heul := T.euler linarith refine ⟨h1, ?_⟩ have hQ : (T.F : ℚ) = 2 * (T.I : ℚ) + (T.B : ℚ) - 2 := by exact_mod_cast h1 linarith /-- **Ehrhart–Pick Equivalence in Dimension 2**: For every positively oriented lattice triangle `T = (p, q, r)` with `D = det2 p q r`, `I = #(triIntPts T)`, `B = #(triBdyPts T)`, and every dilation factor `t ≥ 1`: 1. Closed Ehrhart formula: `2 * L_T(t) = D * t^2 + B * t + 2` (i.e. `L_T(t) = A t^2 + (B/2) t + 1`). 2. Open Ehrhart formula: `2 * L_T^∘(t) = D * t^2 - B * t + 2` (i.e. `L_T^∘(t) = L_T(-t)`). 3. Exact `h*`-vector expansion: `L_T(t) = 1 * C(t + 2, 2) + (I + B - 3) * C(t + 1, 2) + I * C(t, 2)`. -/ theorem ehrhart_pick_equivalence_2d {p q r : ℤ × ℤ} (hD : 0 < det2 p q r) (t : ℕ) (ht : 0 < t) : let D := det2 p q r let I := (triIntPts p q r).card let B := (triBdyPts p q r).card let Lt := (triPts (dilatePt t p) (dilatePt t q) (dilatePt t r)).card let Lt_open := (triIntPts (dilatePt t p) (dilatePt t q) (dilatePt t r)).card (2 * (Lt : ℤ) = D * (t : ℤ) ^ 2 + (B : ℤ) * (t : ℤ) + 2) ∧ (2 * (Lt_open : ℤ) = D * (t : ℤ) ^ 2 - (B : ℤ) * (t : ℤ) + 2) ∧ ((Lt : ℚ) = 1 * ((t + 2).choose 2 : ℚ) + ((I : ℚ) + (B : ℚ) - 3) * ((t + 1).choose 2 : ℚ) + (I : ℚ) * (t.choose 2 : ℚ)) := by intro D I B Lt Lt_open have hDt : 0 < det2 (dilatePt t p) (dilatePt t q) (dilatePt t r) := by rw [det2_dilate] have : (0 : ℤ) < (t : ℤ) ^ 2 := by positivity exact mul_pos this hD have hpick1 := picks_theorem_triangle (det2 p q r).toNat p q r (by omega) (by omega) have hpickt := picks_theorem_triangle (det2 (dilatePt t p) (dilatePt t q) (dilatePt t r)).toNat (dilatePt t p) (dilatePt t q) (dilatePt t r) (by omega) (by omega) rw [det2_dilate] at hpickt have hBt := card_triBdyPts_dilate hD t ht have hcardt := card_triPts_eq_int_add_bdy hDt have hopen : 2 * (Lt_open : ℤ) = D * (t : ℤ) ^ 2 - (B : ℤ) * (t : ℤ) + 2 := by have hBtZ : ((triBdyPts (dilatePt t p) (dilatePt t q) (dilatePt t r)).card : ℤ) = (t : ℤ) * (B : ℤ) := by exact_mod_cast hBt change (t : ℤ) ^ 2 * D = 2 * (Lt_open : ℤ) + _ - 2 at hpickt linarith have hclosed : 2 * (Lt : ℤ) = D * (t : ℤ) ^ 2 + (B : ℤ) * (t : ℤ) + 2 := by have hBtZ : ((triBdyPts (dilatePt t p) (dilatePt t q) (dilatePt t r)).card : ℤ) = (t : ℤ) * (B : ℤ) := by exact_mod_cast hBt have hcardZ : (Lt : ℤ) = (Lt_open : ℤ) + (triBdyPts (dilatePt t p) (dilatePt t q) (dilatePt t r)).card := by exact_mod_cast hcardt linarith refine ⟨hclosed, hopen, ?_⟩ have hc2 : ((t + 2).choose 2 : ℚ) = ((t : ℚ) + 2) * ((t : ℚ) + 1) / 2 := by have : 2 * (t + 2).choose 2 = (t + 2) * (t + 1) := by rw [Nat.choose_two_right]; exact Nat.mul_div_cancel' (Nat.even_mul_pred_self (t + 2)).two_dvd have hQ : (2 : ℚ) * ((t + 2).choose 2 : ℚ) = ((t : ℚ) + 2) * ((t : ℚ) + 1) := by exact_mod_cast this linarith have hc1 : ((t + 1).choose 2 : ℚ) = ((t : ℚ) + 1) * (t : ℚ) / 2 := by have : 2 * (t + 1).choose 2 = (t + 1) * t := by rw [Nat.choose_two_right]; exact Nat.mul_div_cancel' (Nat.even_mul_pred_self (t + 1)).two_dvd have hQ : (2 : ℚ) * ((t + 1).choose 2 : ℚ) = ((t : ℚ) + 1) * (t : ℚ) := by exact_mod_cast this linarith have hc0 : (t.choose 2 : ℚ) = (t : ℚ) * ((t : ℚ) - 1) / 2 := by have : 2 * t.choose 2 = t * (t - 1) := by rw [Nat.choose_two_right]; exact Nat.mul_div_cancel' (Nat.even_mul_pred_self t).two_dvd have ht1 : ((t - 1 : ℕ) : ℚ) = (t : ℚ) - 1 := by rw [Nat.cast_sub (by omega)]; simp have hQ : (2 : ℚ) * (t.choose 2 : ℚ) = (t : ℚ) * ((t - 1 : ℕ) : ℚ) := by exact_mod_cast this rw [ht1] at hQ; linarith have hclosedQ : (2 : ℚ) * (Lt : ℚ) = (D : ℚ) * (t : ℚ) ^ 2 + (B : ℚ) * (t : ℚ) + 2 := by exact_mod_cast hclosed have hpick1Q : (D : ℚ) = 2 * (I : ℚ) + (B : ℚ) - 2 := by exact_mod_cast hpick1 rw [hc2, hc1, hc0] rw [hpick1Q] at hclosedQ ring_nf at hclosedQ ⊢ linarith /-! ## Section 6: Unimodular Lattice Cycles & the Reflexive Polygon 12-Point Theorem -/ /-- Cyclically index into a non-empty list `vs` of lattice vectors. -/ def cycGet (vs : List (ℤ × ℤ)) (i : ℕ) : ℤ × ℤ := vs.getD (i % vs.length) (0, 0) /-- A cyclic list of `m ≥ 3` primitive lattice vectors forming a unimodular fan: every consecutive pair has `cross2 v_i v_{i+1} = 1`. -/ def IsUnimodularCycle (vs : List (ℤ × ℤ)) : Prop := 3 ≤ vs.length ∧ ∀ i : Fin vs.length, cross2 (cycGet vs i.val) (cycGet vs (i.val + 1)) = 1 /-- The toric self-intersection integer `a_i = cross2 v_{i-1} v_{i+1}` at ray `i`. -/ def cycleSelfInt (vs : List (ℤ × ℤ)) (i : ℕ) : ℤ := cross2 (cycGet vs (i + vs.length - 1)) (cycGet vs (i + 1)) /-- The Noether invariant `∑_{i=0}^{m-1} (3 - a_i)` of a unimodular cycle. -/ def cycleTwelveSum (vs : List (ℤ × ℤ)) : ℤ := ∑ i ∈ Finset.range vs.length, (3 - cycleSelfInt vs i) /-- Dual boundary lattice-point count `B(P^∘) = ∑_{i=0}^{m-1} (2 - a_i)` for a reflexive polygon whose boundary lattice points form the unimodular cycle `vs`. -/ def cycleDualBdyCount (vs : List (ℤ × ℤ)) : ℤ := ∑ i ∈ Finset.range vs.length, (2 - cycleSelfInt vs i) lemma cycle_bdy_add_dual_eq_twelveSum (vs : List (ℤ × ℤ)) : (vs.length : ℤ) + cycleDualBdyCount vs = cycleTwelveSum vs := by unfold cycleDualBdyCount cycleTwelveSum have : ∀ i ∈ Finset.range vs.length, (3 - cycleSelfInt vs i) = 1 + (2 - cycleSelfInt vs i) := by intro i _; ring rw [Finset.sum_congr rfl this, Finset.sum_add_distrib] simp /-- **Fundamental Unimodular Cycle Relation**: In any unimodular cycle, every triple of consecutive rays satisfies `v_{i-1} + v_{i+1} = a_i • v_i` where `a_i = cross2 v_{i-1} v_{i+1}`. -/ theorem unimodular_cycle_relation {vs : List (ℤ × ℤ)} (h : IsUnimodularCycle vs) (i : Fin vs.length) : let vm := cycGet vs (i.val + vs.length - 1) let v0 := cycGet vs i.val let vp := cycGet vs (i.val + 1) let a := cycleSelfInt vs i.val vm.1 + vp.1 = a * v0.1 ∧ vm.2 + vp.2 = a * v0.2 := by intro vm v0 vp a have hlen : 0 < vs.length := by have := h.1; omega have hprev : cross2 vm v0 = 1 := by have hidx : (i.val + vs.length - 1) % vs.length < vs.length := Nat.mod_lt _ hlen have h1 := h.2 ⟨(i.val + vs.length - 1) % vs.length, hidx⟩ have hmod1 : cycGet vs ((i.val + vs.length - 1) % vs.length) = vm := by unfold vm cycGet; rw [Nat.mod_mod] have hmod2 : cycGet vs ((i.val + vs.length - 1) % vs.length + 1) = v0 := by unfold v0 cycGet congr 1 rw [Nat.add_mod, Nat.mod_mod, ← Nat.add_mod] have : i.val + vs.length - 1 + 1 = i.val + vs.length := by omega rw [this, Nat.add_mod_right] rw [hmod1, hmod2] at h1 exact h1 have hnext : cross2 v0 vp = 1 := h.2 i have ha : a = cross2 vm vp := rfl unfold cross2 at hprev hnext ha constructor · have hexact : (vm.1 + vp.1) * (vm.1 * v0.2 - vm.2 * v0.1) - (vm.1 * vp.2 - vm.2 * vp.1) * v0.1 = vm.1 * ((vm.1 * v0.2 - vm.2 * v0.1) - (v0.1 * vp.2 - v0.2 * vp.1)) := by ring rw [hprev, hnext, ← ha] at hexact linarith · have hexact : (vm.2 + vp.2) * (vm.1 * v0.2 - vm.2 * v0.1) - (vm.1 * vp.2 - vm.2 * vp.1) * v0.2 = vm.2 * ((vm.1 * v0.2 - vm.2 * v0.1) - (v0.1 * vp.2 - v0.2 * vp.1)) := by ring rw [hprev, hnext, ← ha] at hexact linarith /-- Unimodular star subdivision (toric blowup) of a cyclic list `vs` at edge `(k, k+1)`, inserting `w = v_k + v_{k+1}` immediately after index `k`. -/ def blowupAt (vs : List (ℤ × ℤ)) (k : ℕ) : List (ℤ × ℤ) := let u := cycGet vs k let v := cycGet vs (k + 1) let w : ℤ × ℤ := (u.1 + v.1, u.2 + v.2) vs.take (k + 1) ++ [w] ++ vs.drop (k + 1) lemma blowupAt_length {vs : List (ℤ × ℤ)} {k : ℕ} (hk : k < vs.length) : (blowupAt vs k).length = vs.length + 1 := by unfold blowupAt simp [List.length_take, List.length_drop] omega lemma cycGet_blowupAt_lt {vs : List (ℤ × ℤ)} {k j : ℕ} (hk : k < vs.length) (hj : j ≤ k) : cycGet (blowupAt vs k) j = cycGet vs j := by have hlen := blowupAt_length hk unfold cycGet rw [hlen, Nat.mod_eq_of_lt (by omega), Nat.mod_eq_of_lt (by omega)] unfold blowupAt simp only [List.getD_eq_getElem?_getD, List.append_assoc] rw [List.getElem?_append_left (by simp [List.length_take]; omega), List.getElem?_take_of_lt (by omega)] lemma cycGet_blowupAt_mid {vs : List (ℤ × ℤ)} {k : ℕ} (hk : k < vs.length) : cycGet (blowupAt vs k) (k + 1) = ((cycGet vs k).1 + (cycGet vs (k + 1)).1, (cycGet vs k).2 + (cycGet vs (k + 1)).2) := by have hlen := blowupAt_length hk unfold cycGet rw [hlen, Nat.mod_eq_of_lt (by omega)] unfold blowupAt simp only [List.getD_eq_getElem?_getD, List.append_assoc] have htake : (vs.take (k + 1)).length = k + 1 := by simp [List.length_take]; omega rw [List.getElem?_append_right (by omega), htake, Nat.sub_self] simp [cycGet] lemma cycGet_blowupAt_gt {vs : List (ℤ × ℤ)} {k j : ℕ} (hk : k < vs.length) (hj1 : k + 2 ≤ j) (hj2 : j < vs.length + 1) : cycGet (blowupAt vs k) j = cycGet vs (j - 1) := by have hlen := blowupAt_length hk unfold cycGet rw [hlen, Nat.mod_eq_of_lt hj2, Nat.mod_eq_of_lt (by omega)] unfold blowupAt simp only [List.getD_eq_getElem?_getD, List.append_assoc] have htake : (vs.take (k + 1)).length = k + 1 := by simp [List.length_take]; omega rw [List.getElem?_append_right (by omega), htake] have hsub : j - (k + 1) = (j - (k + 2)) + 1 := by omega rw [hsub] simp only [List.cons_append, List.nil_append, List.getElem?_cons_succ, List.getElem?_drop] have hidx : k + 1 + (j - (k + 2)) = j - 1 := by omega rw [hidx] /-- Helper: `cycGet vs` is periodic with period `vs.length`. -/ lemma cycGet_mod (vs : List (ℤ × ℤ)) (i : ℕ) : cycGet vs (i % vs.length) = cycGet vs i := by unfold cycGet; rw [Nat.mod_mod] lemma cycGet_add_length (vs : List (ℤ × ℤ)) (i : ℕ) : cycGet vs (i + vs.length) = cycGet vs i := by unfold cycGet; rw [Nat.add_mod_right] /-- Unimodular star subdivision preserves the unimodular cycle condition. -/ theorem blowupAt_isUnimodularCycle {vs : List (ℤ × ℤ)} (h : IsUnimodularCycle vs) {k : ℕ} (hk : k < vs.length) : IsUnimodularCycle (blowupAt vs k) := by have hlen := blowupAt_length hk refine ⟨by have := h.1; omega, ?_⟩ intro i rcases i with ⟨j, hj⟩ dsimp only rw [hlen] at hj by_cases hj_lt : j < k · rw [cycGet_blowupAt_lt hk (by omega), cycGet_blowupAt_lt hk (by omega)] exact h.2 ⟨j, by omega⟩ · by_cases hjk : j = k · subst j rw [cycGet_blowupAt_lt hk (le_refl k), cycGet_blowupAt_mid hk] have h1 := h.2 ⟨k, hk⟩ unfold cross2 at h1 ⊢; linarith · by_cases hjk1 : j = k + 1 · subst j rw [cycGet_blowupAt_mid hk] have hnext : cycGet (blowupAt vs k) (k + 1 + 1) = cycGet vs (k + 1) := by by_cases hk_last : k + 1 < vs.length · exact cycGet_blowupAt_gt hk (by omega) (by omega) · have hkeq : k + 1 = vs.length := by omega have h0 : (k + 1 + 1) % (blowupAt vs k).length = 0 := by rw [hlen, hkeq]; simp rw [← cycGet_mod, h0, cycGet_blowupAt_lt hk (Nat.zero_le k)] unfold cycGet; rw [hkeq, Nat.mod_self, Nat.zero_mod] rw [hnext] have h1 := h.2 ⟨k, hk⟩ unfold cross2 at h1 ⊢; linarith · -- j ≥ k + 2 rw [cycGet_blowupAt_gt hk (by omega) hj] have hnext : cycGet (blowupAt vs k) (j + 1) = cycGet vs j := by by_cases hj_last : j + 1 < vs.length + 1 · exact cycGet_blowupAt_gt hk (by omega) hj_last · have hjeq : j = vs.length := by omega have h0 : (j + 1) % (blowupAt vs k).length = 0 := by rw [hlen, hjeq]; simp rw [← cycGet_mod, h0, cycGet_blowupAt_lt hk (Nat.zero_le k)] unfold cycGet; rw [hjeq, Nat.mod_self, Nat.zero_mod] rw [hnext] have h1 := h.2 ⟨j - 1, by omega⟩ have hsub : j - 1 + 1 = j := by omega rw [hsub] at h1 exact h1 /-- **Noether Blowup Invariance Theorem**: Unimodular star subdivision at any edge preserves the Noether sum `∑ (3 - a_i)`: the new ray contributes `3 - 1 = 2`, while its two neighbors each increase `a_i` by `1` (each reducing `3 - a_i` by `1`), for a net change of `+2 - 1 - 1 = 0`. -/ theorem blowupAt_preserves_twelveSum {vs : List (ℤ × ℤ)} (h : IsUnimodularCycle vs) {k : ℕ} (hk : k < vs.length) : cycleTwelveSum (blowupAt vs k) = cycleTwelveSum vs := by set m := vs.length have hm3 : 3 ≤ m := h.1 have hlen : (blowupAt vs k).length = m + 1 := blowupAt_length hk -- Relate cycleSelfInt (blowupAt vs k) at every index to cycleSelfInt vs! have hval_new : cycleSelfInt (blowupAt vs k) (k + 1) = 1 := by unfold cycleSelfInt rw [hlen] have hprev : cycGet (blowupAt vs k) (k + 1 + (m + 1) - 1) = cycGet vs k := by have heq : k + 1 + (m + 1) - 1 = k + (m + 1) := by omega rw [heq, ← hlen, cycGet_add_length, cycGet_blowupAt_lt hk (le_refl k)] have hnext : cycGet (blowupAt vs k) (k + 1 + 1) = cycGet vs (k + 1) := by by_cases hk1 : k + 1 < m · exact cycGet_blowupAt_gt hk (by omega) (by omega) · have hkeq : k + 1 = m := by omega rw [← cycGet_mod, hlen, hkeq, Nat.mod_self, cycGet_blowupAt_lt hk (Nat.zero_le k)] unfold cycGet rw [show vs.length = m from rfl, Nat.mod_self, Nat.zero_mod] rw [hprev, hnext] exact h.2 ⟨k, hk⟩ -- Next, helper for cycGet of blowupAt shifted by any index in range (m+1) have h_get : ∀ j < m + 1, cycGet (blowupAt vs k) j = if j ≤ k then cycGet vs j else if j = k + 1 then ((cycGet vs k).1 + (cycGet vs (k + 1)).1, (cycGet vs k).2 + (cycGet vs (k + 1)).2) else cycGet vs (j - 1) := by intro j hj split_ifs with h1 h2 · exact cycGet_blowupAt_lt hk h1 · subst h2; exact cycGet_blowupAt_mid hk · exact cycGet_blowupAt_gt hk (by omega) hj have h_get_mod : ∀ n : ℕ, cycGet (blowupAt vs k) n = let j := n % (m + 1) if j ≤ k then cycGet vs j else if j = k + 1 then ((cycGet vs k).1 + (cycGet vs (k + 1)).1, (cycGet vs k).2 + (cycGet vs (k + 1)).2) else cycGet vs (j - 1) := by intro n rw [← cycGet_mod, hlen] exact h_get (n % (m + 1)) (Nat.mod_lt n (by omega)) -- Compute self-intersection at each j < m + 1 (excluding j = k + 1): -- Map i ∈ range m to i' = if i ≤ k then i else i + 1 in range (m + 1) \ {k + 1}. let emb : ℕ → ℕ := fun i => if i ≤ k then i else i + 1 have h_split_sum : cycleTwelveSum (blowupAt vs k) = (3 - cycleSelfInt (blowupAt vs k) (k + 1)) + ∑ i ∈ Finset.range m, (3 - cycleSelfInt (blowupAt vs k) (emb i)) := by unfold cycleTwelveSum rw [hlen] have hmem : k + 1 ∈ Finset.range (m + 1) := Finset.mem_range.mpr (by omega) rw [← Finset.add_sum_erase _ _ hmem] congr 1 symm apply Finset.sum_bij (fun i _ => emb i) · intro i hi rw [Finset.mem_range] at hi rw [Finset.mem_erase, Finset.mem_range] dsimp only [emb] split_ifs <;> omega · intro i₁ _ i₂ _ heq dsimp only [emb] at heq split_ifs at heq <;> omega · intro j hj rw [Finset.mem_erase, Finset.mem_range] at hj rcases hj with ⟨hne, hjlt⟩ by_cases hjk : j ≤ k · refine ⟨j, Finset.mem_range.mpr (by omega), ?_⟩ dsimp only [emb]; simp [hjk] · refine ⟨j - 1, Finset.mem_range.mpr (by omega), ?_⟩ dsimp only [emb] have : ¬ (j - 1 ≤ k) := by omega simp [this]; omega · intro _ _; rfl -- Now compute cycleSelfInt (blowupAt vs k) (emb i) for every i ∈ range m! have h_shift : ∀ i ∈ Finset.range m, cycleSelfInt (blowupAt vs k) (emb i) = cycleSelfInt vs i + (if i = k then 1 else 0) + (if i = (k + 1) % m then 1 else 0) := by intro i hi rw [Finset.mem_range] at hi unfold cycleSelfInt rw [hlen] -- Compute previous and next elements of (blowupAt vs k) at emb i have hprev_eq : cycGet (blowupAt vs k) (emb i + (m + 1) - 1) = if i = (k + 1) % m then ((cycGet vs k).1 + (cycGet vs (k + 1)).1, (cycGet vs k).2 + (cycGet vs (k + 1)).2) else cycGet vs (i + m - 1) := by rw [h_get_mod] dsimp only [emb] by_cases hik : i ≤ k · simp only [hik, ↓reduceIte] by_cases hi0 : i = 0 · subst hi0 have hmod : (0 + (m + 1) - 1) % (m + 1) = m := by rw [zero_add, Nat.add_sub_cancel, Nat.mod_eq_of_lt (by omega)] rw [hmod] by_cases hk_last : k + 1 = m · have hcond : (0 : ℕ) = (k + 1) % m := by rw [hk_last, Nat.mod_self] have h1 : ¬ (m ≤ k) := by omega have h2 : m = k + 1 := hk_last.symm rw [if_neg h1, if_pos h2, if_pos hcond] · have hcond : (0 : ℕ) ≠ (k + 1) % m := by rw [Nat.mod_eq_of_lt (by omega)]; omega have h1 : ¬ (m ≤ k) := by omega have h2 : m ≠ k + 1 := by omega rw [if_neg h1, if_neg h2, if_neg hcond, zero_add] · have hmod : (i + (m + 1) - 1) % (m + 1) = i - 1 := by have : i + (m + 1) - 1 = (i - 1) + (m + 1) := by omega rw [this, Nat.add_mod_right, Nat.mod_eq_of_lt (by omega)] rw [hmod] have h1 : i - 1 ≤ k := by omega have hcond : i ≠ (k + 1) % m := by by_cases hk_last : k + 1 = m · rw [hk_last, Nat.mod_self]; omega · rw [Nat.mod_eq_of_lt (by omega)]; omega simp only [h1, hcond, ↓reduceIte] have : i + m - 1 = (i - 1) + m := by omega rw [this, show m = vs.length from rfl, cycGet_add_length] · simp only [hik, ↓reduceIte] have hmod : (i + 1 + (m + 1) - 1) % (m + 1) = i := by have : i + 1 + (m + 1) - 1 = i + (m + 1) := by omega rw [this, Nat.add_mod_right, Nat.mod_eq_of_lt (by omega)] rw [hmod] have h1 : ¬ (i ≤ k) := hik by_cases hi_k1 : i = k + 1 · have hcond : i = (k + 1) % m := by rw [Nat.mod_eq_of_lt (by omega)]; exact hi_k1 rw [if_neg h1, if_pos hi_k1, if_pos hcond] · have hcond : i ≠ (k + 1) % m := by by_cases hk_last : k + 1 = m · rw [hk_last, Nat.mod_self]; omega · rw [Nat.mod_eq_of_lt (by omega)]; exact hi_k1 simp only [h1, hi_k1, hcond, ↓reduceIte] have : i + m - 1 = (i - 1) + m := by omega rw [this, show m = vs.length from rfl, cycGet_add_length] have hnext_eq : cycGet (blowupAt vs k) (emb i + 1) = if i = k then ((cycGet vs k).1 + (cycGet vs (k + 1)).1, (cycGet vs k).2 + (cycGet vs (k + 1)).2) else cycGet vs (i + 1) := by rw [h_get_mod] dsimp only [emb] by_cases hik : i ≤ k · simp only [hik, ↓reduceIte] have hmod : (i + 1) % (m + 1) = i + 1 := Nat.mod_eq_of_lt (by omega) rw [hmod] by_cases hiek : i = k · subst hiek have h1 : ¬ (i + 1 ≤ i) := by omega simp [h1] · have h1 : i + 1 ≤ k := by omega simp [h1, hiek] · simp only [hik, ↓reduceIte] have hiek : i ≠ k := by omega by_cases hi_last : i + 1 = m · have hmod : (i + 1 + 1) % (m + 1) = 0 := by rw [hi_last, Nat.mod_self] rw [hmod] have h0k : 0 ≤ k := Nat.zero_le k simp only [h0k, hiek, ↓reduceIte] unfold cycGet rw [show vs.length = m from rfl, hi_last, Nat.mod_self, Nat.zero_mod] · have hmod : (i + 1 + 1) % (m + 1) = i + 2 := Nat.mod_eq_of_lt (by omega) rw [hmod] have h1 : ¬ (i + 2 ≤ k) := by omega have h2 : i + 2 ≠ k + 1 := by omega simp only [h1, h2, hiek, ↓reduceIte] congr 1 rw [hprev_eq, hnext_eq] -- Note k ≠ (k + 1) % m since m ≥ 3 have hk_ne : k ≠ (k + 1) % m := by by_cases hkl : k + 1 = m · rw [hkl, Nat.mod_self]; omega · rw [Nat.mod_eq_of_lt (by omega)]; omega by_cases h1 : i = k · subst i rw [if_neg hk_ne, if_pos rfl, if_pos rfl, if_neg hk_ne] have hprev_cross : cross2 (cycGet vs (k + m - 1)) (cycGet vs k) = 1 := by have hidx : (k + m - 1) % m < m := Nat.mod_lt _ (by omega) have h1' := h.2 ⟨(k + m - 1) % m, hidx⟩ have hm1 : cycGet vs ((k + m - 1) % m) = cycGet vs (k + m - 1) := cycGet_mod vs _ have hm2 : cycGet vs ((k + m - 1) % m + 1) = cycGet vs k := by unfold cycGet congr 1 rw [show vs.length = m from rfl, Nat.add_mod, Nat.mod_mod, ← Nat.add_mod, show k + m - 1 + 1 = k + m from by omega, Nat.add_mod_right] rw [hm1, hm2] at h1'; exact h1' unfold cross2 at hprev_cross ⊢ linarith · by_cases h2 : i = (k + 1) % m · subst i rw [if_pos rfl, if_neg hk_ne.symm, if_neg hk_ne.symm, if_pos rfl] have hnext_cross : cross2 (cycGet vs (k + 1)) (cycGet vs ((k + 1) % m + 1)) = 1 := by have hidx : (k + 1) % m < m := Nat.mod_lt _ (by omega) have h1' := h.2 ⟨(k + 1) % m, hidx⟩ have hm1 : cycGet vs ((k + 1) % m) = cycGet vs (k + 1) := cycGet_mod vs _ rw [hm1] at h1'; exact h1' have hprev_id : cycGet vs ((k + 1) % m + m - 1) = cycGet vs k := by unfold cycGet congr 1 change ((k + 1) % m + m - 1) % m = k % m by_cases hkl : k + 1 = m · rw [hkl, Nat.mod_self, zero_add] have hkm : m - 1 = k := by omega rw [hkm] · have hlt : k + 1 < m := by omega rw [Nat.mod_eq_of_lt hlt] have : k + 1 + m - 1 = k + m := by omega rw [this, Nat.add_mod_right] rw [hprev_id] unfold cross2 at hnext_cross ⊢ linarith · rw [if_neg h2, if_neg h1, if_neg h1, if_neg h2] ring rw [h_split_sum, hval_new] have hsum_rew : ∑ i ∈ Finset.range m, (3 - cycleSelfInt (blowupAt vs k) (emb i)) = ∑ i ∈ Finset.range m, ((3 - cycleSelfInt vs i) - (if i = k then 1 else 0) - (if i = (k + 1) % m then 1 else 0)) := by apply Finset.sum_congr rfl intro i hi rw [h_shift i hi]; ring rw [hsum_rew, Finset.sum_sub_distrib, Finset.sum_sub_distrib] have hk_mem : k ∈ Finset.range m := Finset.mem_range.mpr hk have hk1_mem : (k + 1) % m ∈ Finset.range m := Finset.mem_range.mpr (Nat.mod_lt _ (by omega)) rw [Finset.sum_ite_eq' (Finset.range m) k (fun _ => (1 : ℤ)), Finset.sum_ite_eq' (Finset.range m) ((k + 1) % m) (fun _ => (1 : ℤ))] simp only [hk_mem, hk1_mem, ↓reduceIte] unfold cycleTwelveSum linarith /-- Minimal smooth toric fan of `ℙ²`. -/ def fanP2 : List (ℤ × ℤ) := [(1, 0), (0, 1), (-1, -1)] /-- Minimal smooth toric fan of the Hirzebruch surface `𝔽_k` (for any `k : ℤ`). -/ def fanHirzebruch (k : ℤ) : List (ℤ × ℤ) := [(1, 0), (0, 1), (-1, k), (0, -1)] lemma fanP2_isUnimodularCycle : IsUnimodularCycle fanP2 := by refine ⟨by decide, ?_⟩ intro i fin_cases i <;> decide lemma fanP2_twelveSum : cycleTwelveSum fanP2 = 12 := by decide lemma fanHirzebruch_isUnimodularCycle (k : ℤ) : IsUnimodularCycle (fanHirzebruch k) := by refine ⟨by simp [fanHirzebruch], ?_⟩ intro ⟨i, hi⟩ simp only [fanHirzebruch, List.length_cons, List.length_nil] at hi interval_cases i <;> simp [fanHirzebruch, cycGet, cross2] lemma fanHirzebruch_twelveSum (k : ℤ) : cycleTwelveSum (fanHirzebruch k) = 12 := by unfold cycleTwelveSum cycleSelfInt fanHirzebruch simp only [List.length_cons, List.length_nil, Nat.reduceAdd, Finset.sum_range_succ, Finset.range_zero, Finset.sum_empty, cycGet, cross2] simp only [List.getD_eq_getElem?_getD] norm_num ring /-- The inductive family of all smooth complete 2D toric fans (generated from `ℙ²` and the Hirzebruch fans `𝔽_k` by arbitrary sequences of unimodular star subdivisions). -/ inductive SmoothToricFan : List (ℤ × ℤ) → Prop | p2 : SmoothToricFan fanP2 | hirzebruch (k : ℤ) : SmoothToricFan (fanHirzebruch k) | blowup {vs : List (ℤ × ℤ)} (h : SmoothToricFan vs) (k : ℕ) (hk : k < vs.length) : SmoothToricFan (blowupAt vs k) lemma SmoothToricFan.isUnimodularCycle {vs : List (ℤ × ℤ)} (h : SmoothToricFan vs) : IsUnimodularCycle vs := by induction h with | p2 => exact fanP2_isUnimodularCycle | hirzebruch k => exact fanHirzebruch_isUnimodularCycle k | blowup h k hk ih => exact blowupAt_isUnimodularCycle ih hk /-- **Unimodular Fan 12-Theorem (Noether Formula for Smooth Toric Surfaces)**: Every smooth complete 2D toric fan satisfies `∑_{i} (3 - a_i) = 12`. -/ theorem unimodular_fan_twelve_theorem {vs : List (ℤ × ℤ)} (h : SmoothToricFan vs) : cycleTwelveSum vs = 12 := by induction h with | p2 => exact fanP2_twelveSum | hirzebruch k => exact fanHirzebruch_twelveSum k | blowup h k hk ih => rw [blowupAt_preserves_twelveSum h.isUnimodularCycle hk, ih] /-- **Reflexive Polygon 12-Point Theorem**: For every reflexive lattice polygon whose boundary lattice points form a smooth toric fan `vs` (with `B(P) = vs.length` and `B(P^∘) = ∑ (2 - a_i)`), `B(P) + B(P^∘) = 12`. -/ theorem reflexive_polygon_twelve_theorem {vs : List (ℤ × ℤ)} (h : SmoothToricFan vs) : (vs.length : ℤ) + cycleDualBdyCount vs = 12 := by rw [cycle_bdy_add_dual_eq_twelveSum, unimodular_fan_twelve_theorem h] /-- Predicate checking that a list of lattice vectors is a valid reflexive polygon boundary cycle: unimodular, convex (`a_i ≤ 2` at every boundary point), and satisfying `B(P) + B(P^∘) = 12`. -/ def IsReflexiveBoundaryCycle (vs : List (ℤ × ℤ)) : Prop := IsUnimodularCycle vs ∧ (∀ i ∈ Finset.range vs.length, cycleSelfInt vs i ≤ 2) ∧ (vs.length : ℤ) + cycleDualBdyCount vs = 12 /-- Complete catalog of all 16 isomorphism classes of 2D reflexive lattice polygons, given by their counterclockwise unimodular boundary lattice-point cycles. -/ def allSixteenReflexivePolygons : List (List (ℤ × ℤ)) := [ [(1, 0), (0, 1), (-1, -1)], [(1, 0), (0, 1), (-1, 2), (0, -1)], [(1, 0), (1, 1), (0, 1), (-1, -1)], [(1, 0), (0, 1), (-1, 0), (0, -1)], [(1, 0), (0, 1), (-1, 2), (-1, 1), (0, -1)], [(1, 0), (1, 1), (0, 1), (-1, 0), (0, -1)], [(1, 0), (0, 1), (-1, 2), (-2, 3), (-1, 1), (0, -1)], [(1, 0), (2, 1), (1, 1), (0, 1), (-1, 0), (0, -1)], [(1, 0), (1, 1), (0, 1), (-1, 1), (-1, 0), (0, -1)], [(1, 0), (1, 1), (0, 1), (-1, 0), (-1, -1), (0, -1)], [(1, 0), (2, 1), (1, 1), (0, 1), (-1, 1), (-1, 0), (0, -1)], [(1, 0), (2, 1), (1, 1), (0, 1), (-1, 0), (-1, -1), (0, -1)], [(1, 0), (2, 1), (1, 1), (0, 1), (-1, 1), (-2, 1), (-1, 0), (0, -1)], [(1, 0), (2, 1), (1, 1), (0, 1), (-1, 1), (-1, 0), (-1, -1), (0, -1)], [(1, 0), (2, 1), (1, 1), (0, 1), (-1, 0), (-2, -1), (-1, -1), (0, -1)], [(1, 0), (2, 1), (1, 1), (0, 1), (-1, 1), (-1, 0), (-1, -1), (-1, -2), (0, -1)] ] /-- Every one of the 16 isomorphism classes of 2D reflexive lattice polygons is unimodular, convex (`a_i ≤ 2`), and satisfies the 12-Point Theorem `B(P) + B(P^∘) = 12`. -/ theorem all_sixteen_reflexive_polygons_twelve : allSixteenReflexivePolygons.length = 16 ∧ ∀ vs ∈ allSixteenReflexivePolygons, IsReflexiveBoundaryCycle vs := by constructor · rfl · intro vs hvs simp only [allSixteenReflexivePolygons, List.mem_cons, List.not_mem_nil, or_false] at hvs rcases hvs with rfl | rfl | rfl | rfl | rfl | rfl | rfl | rfl | rfl | rfl | rfl | rfl | rfl | rfl | rfl | rfl <;> exact ⟨⟨by decide, fun i => by fin_cases i <;> decide⟩, by decide, by decide⟩ end PicksTheoremAndTwoDimLatticeGeometry #print axioms PicksTheoremAndTwoDimLatticeGeometry.card_segPts_eq_gcd_add_one #print axioms PicksTheoremAndTwoDimLatticeGeometry.card_triBdyPts_eq_sum_gcd #print axioms PicksTheoremAndTwoDimLatticeGeometry.card_triBdyPts_dilate #print axioms PicksTheoremAndTwoDimLatticeGeometry.exists_extra_point_of_det2_ge_two #print axioms PicksTheoremAndTwoDimLatticeGeometry.primitive_triangle_unimodular #print axioms PicksTheoremAndTwoDimLatticeGeometry.picks_theorem_triangle #print axioms PicksTheoremAndTwoDimLatticeGeometry.picks_theorem_triangle_rat #print axioms PicksTheoremAndTwoDimLatticeGeometry.picks_theorem_triangulation #print axioms PicksTheoremAndTwoDimLatticeGeometry.ehrhart_pick_equivalence_2d #print axioms PicksTheoremAndTwoDimLatticeGeometry.unimodular_cycle_relation #print axioms PicksTheoremAndTwoDimLatticeGeometry.blowupAt_isUnimodularCycle #print axioms PicksTheoremAndTwoDimLatticeGeometry.blowupAt_preserves_twelveSum #print axioms PicksTheoremAndTwoDimLatticeGeometry.unimodular_fan_twelve_theorem #print axioms PicksTheoremAndTwoDimLatticeGeometry.reflexive_polygon_twelve_theorem #print axioms PicksTheoremAndTwoDimLatticeGeometry.all_sixteen_reflexive_polygons_twelve