-- Thue's lemma by pigeonhole: for 0 < p and m with m² > p and m ≤ p, every -- a has a nonzero pair (x, y) of integers of absolute value below m with -- a x ≡ y modulo p; with the least such m, and in a natural form with a -- positive x. -- Part of Cumulus, the Grace17 mathematics library. -- Copyright (c) 2026 Grant Passmore. -- Released under the Apache License 2.0. See LICENSE in the repository root. -- Authors: Grant Passmore module Grace.Cumulus.NumberTheory.Thue import Grace.Cumulus.NumberTheory.IntegerPrime import Grace.Cumulus.Combinatorics.Pigeonhole import Grace.Cumulus.Nat.Least -- The least m with p < m² -- The naturals m with p < m², a nonempty set since p < (p + 1)². def squareBound.family (p : Nat) : Set := { m ∈ ℕ | p < mul(m, m) } theorem squareBound.inhabited (p : Nat) shows Nonempty(squareBound.family(p)) proof have expand : (p + 1) * (p + 1) = p * p + 2 * p + 1 by ring have bound : p < (p + 1) * (p + 1) rw expand arith have successorNat : p + 1 ∈ ℕ by typecheck witness p + 1 exact squareBound.family.membership.backward with ⟨successorNat, bound⟩ theorem squareBound.naturals (p : Nat) shows squareBound.family(p) ⊆ ℕ proof intros m hm have both := squareBound.family.membership.forward with hm exact both.left -- The least m with p < m², at least 1 since 0² = 0. def squareBound (p : Nat) : Nat := leastNatural(squareBound.family(p)) proof unfold squareBound have selected := leastNatural.selected at squareBound.family(p) with (squareBound.inhabited at p), (squareBound.naturals at p) exact nat.omega.backward with selected.left theorem squareBound.above (p : Nat) shows p < squareBound(p) * squareBound(p) proof have selected := leastNatural.selected at squareBound.family(p) with (squareBound.inhabited at p), (squareBound.naturals at p) have member := selected.right.left have both := squareBound.family.membership.forward with member unfold squareBound exact both.right theorem squareBound.least (p k : Nat) requires below : k < squareBound(p) shows k * k ≤ p proof by_contra notBelow have above := Less.of_not_LessEqual with notBelow have kNat : k ∈ ℕ by typecheck have member := squareBound.family.membership.backward with ⟨kNat, above⟩ have least := leastNatural.least at squareBound.family(p), k with (squareBound.inhabited at p), (squareBound.naturals at p), member unfold squareBound at below arith theorem squareBound.positive (p : Nat) shows 0 < squareBound(p) proof have above := squareBound.above at p by_contra notPositive have atMost := LessEqual.of_not_Less with notPositive have squares := LessEqual.mul with atMost, atMost have zero : 0 * 0 = 0 by compute rw zero at squares arith -- p < p² for 1 < p, so the least bound is at most p. theorem squareBound.le (p : Nat) requires aboveOne : 1 < p shows squareBound(p) ≤ p proof have twoLe : 2 ≤ p by arith obtain ⟨k, kNat, shape⟩ := LessEqual.exists_add with twoLe have expand : p * p = p + (2 + 3 * k + k * k) by ring [shape] have bound : p < p * p rw expand arith have pNat : p ∈ ℕ by typecheck have member := squareBound.family.membership.backward with ⟨pNat, bound⟩ have least := leastNatural.least at squareBound.family(p), p with (squareBound.inhabited at p), (squareBound.naturals at p), member unfold squareBound exact least -- The pigeonhole map -- (x, y) ↦ (a x − y) mod p on m × m, with −y written as p − y for y < p. defgraph thueMap (a p m : Nat) (q ∈ m × m) : p := mod(add(mul(a, fst(q)), sub(p, snd(q))), p) requires positive : 0 < p proof have parts := product.membership.forward with member obtain ⟨x, hx, y, hy, shape⟩ := parts.right rw [shape, fst.pair, snd.pair] have nonzero := Less.ne_zero_of_positive with positive have less := mod.less at a * x + (p - y), p with nonzero exact membership.of_Less at (a * x + (p - y)) % p, p with *, less -- m² > p boxes: two distinct pairs share a value. theorem thueMap.collision (a p m : Nat) requires positive : 0 < p requires bound : p < m * m shows ∃ q ∈ m × m, ∃ r ∈ m × m, q ≠ r ∧ thueMap(a, p, m)(q) = thueMap(a, p, m)(r) proof have function := thueMap.function at a, p, m with positive have finite := Finite.natural at p have finiteM := Finite.natural at m have count := cardinality.product_count at m, m with finiteM, finiteM have countP := cardinality.natural at p have countM := cardinality.natural at m have more : cardinality(p) < cardinality(m × m) rw [count, countP, countM] exact bound exact pigeonhole.collision with function, finite, more -- The value at a pair of naturals below m. theorem thueMap.pair_value (a p m x y : Nat) requires positive : 0 < p requires hx : x < m requires hy : y < m shows thueMap(a, p, m)((x, y)) = (a * x + (p - y)) % p proof have xMember := membership.of_Less at x, m with *, hx have yMember := membership.of_Less at y, m with *, hy have member := product.pair at x, y, m, m with xMember, yMember have value := thueMap.value at a, p, m, (x, y) with positive, member rw [fst.pair, snd.pair] at value exact value -- Thue's lemma -- Two colliding pairs satisfy a x₁ + y₂ ≡ a x₂ + y₁: add y₁ + y₂ to both -- values and drop the p. theorem thueMap.cross (a p m x1 y1 x2 y2 : Nat) requires positive : 0 < p requires below : m ≤ p requires hx1 : x1 < m requires hy1 : y1 < m requires hx2 : x2 < m requires hy2 : y2 < m requires same : thueMap(a, p, m)((x1, y1)) = thueMap(a, p, m)((x2, y2)) shows (a * x1 + y2) % p = (a * x2 + y1) % p proof have nonzero := Less.ne_zero_of_positive with positive have leftValue := thueMap.pair_value at a, p, m, x1, y1 with positive, hx1, hy1 have rightValue := thueMap.pair_value at a, p, m, x2, y2 with positive, hx2, hy2 rw [leftValue, rightValue] at same have y1Le : y1 ≤ p by arith have y2Le : y2 ≤ p by arith obtain ⟨d1, d1Nat, split1⟩ := LessEqual.exists_add with y1Le obtain ⟨d2, d2Nat, split2⟩ := LessEqual.exists_add with y2Le have d1Sum : d1 + y1 = p by ring [split1] have d2Sum : d2 + y2 = p by ring [split2] have d1Value := sub.of_add_eq with d1Sum have d2Value := sub.of_add_eq with d2Sum rw [d1Value, d2Value] at same have leftSplit := mod.add at p, a * x1 + d1, y1 + y2 with nonzero have rightSplit := mod.add at p, a * x2 + d2, y1 + y2 with nonzero have shifted : (a * x1 + d1 + (y1 + y2)) % p = (a * x2 + d2 + (y1 + y2)) % p rw [leftSplit, rightSplit, same] rfl have leftShape : a * x1 + d1 + (y1 + y2) = a * x1 + y2 + p by ring [split1] have rightShape : a * x2 + d2 + (y1 + y2) = a * x2 + y1 + p by ring [split2] rw [leftShape, rightShape] at shifted have leftDrop := mod.add_self at p, a * x1 + y2 with nonzero have rightDrop := mod.add_self at p, a * x2 + y1 with nonzero rw [leftDrop, rightDrop] at shifted exact shifted -- From a x₁ + y₂ ≡ a x₂ + y₁ with x₂ < x₁: the differences x = x₁ − x₂ and -- y = |y₁ − y₂| satisfy a x ≡ y or a x + y ≡ 0 by the sign of y₁ − y₂. theorem thue.of_cross (a p m x1 y1 x2 y2 : Nat) requires positive : 0 < p requires order : x2 < x1 requires hx1 : x1 < m requires hy1 : y1 < m requires hy2 : y2 < m requires cross : (a * x1 + y2) % p = (a * x2 + y1) % p shows ∃ x, Nat(x) ∧ ∃ y, Nat(y) ∧ 0 < x ∧ x < m ∧ y < m ∧ ((a * x) % p = y % p ∨ (a * x + y) % p = 0) proof have xLe : x2 ≤ x1 by arith obtain ⟨x, xNat, xSplit⟩ := LessEqual.exists_add with xLe have total := LessEqual.total at y2, y1 cases total with below | above . obtain ⟨y, yNat, ySplit⟩ := LessEqual.exists_add with below refine ⟨x, xNat, y, yNat, ?_, ?_, ?_, ?_⟩ . arith . arith . arith . left have congruent : CongruentMod(a * x2 + y2 + a * x, a * x2 + y2 + y, p) unfold CongruentMod have leftShape : a * x2 + y2 + a * x = a * x1 + y2 by ring [xSplit] have rightShape : a * x2 + y2 + y = a * x2 + y1 by ring [ySplit] rw [leftShape, rightShape] exact cross have cancelled := CongruentMod.add_left_cancel with congruent unfold CongruentMod at cancelled exact cancelled . obtain ⟨y, yNat, ySplit⟩ := LessEqual.exists_add with above refine ⟨x, xNat, y, yNat, ?_, ?_, ?_, ?_⟩ . arith . arith . arith . right have congruent : CongruentMod(a * x2 + y1 + (a * x + y), a * x2 + y1 + 0, p) unfold CongruentMod have leftShape : a * x2 + y1 + (a * x + y) = a * x1 + y2 ring [xSplit, ySplit] have rightShape : a * x2 + y1 + 0 = a * x2 + y1 by ring rw [leftShape, rightShape] exact cross have cancelled := CongruentMod.add_left_cancel with congruent unfold CongruentMod at cancelled have zero := mod.zero_left at p rw zero at cancelled exact cancelled -- The natural form: the two colliding pairs differ in x, since equal x -- would force y₁ ≡ y₂ with y₁, y₂ < m ≤ p, and then the pairs would be the -- same; order them by x and take the differences. theorem thue.natural (a p m : Nat) requires positive : 0 < p requires bound : p < m * m requires below : m ≤ p shows ∃ x, Nat(x) ∧ ∃ y, Nat(y) ∧ 0 < x ∧ x < m ∧ y < m ∧ ((a * x) % p = y % p ∨ (a * x + y) % p = 0) proof obtain ⟨q, hq, r, hr, distinct, same⟩ := thueMap.collision at a, p, m with positive, bound have qParts := product.membership.forward with hq obtain ⟨x1, hx1, y1, hy1, qShape⟩ := qParts.right have rParts := product.membership.forward with hr obtain ⟨x2, hx2, y2, hy2, rShape⟩ := rParts.right have x1Nat := natural.member at m with hx1 have x2Nat := natural.member at m with hx2 have x1Less := Less.of_membership at x1, m with hx1 have y1Less := Less.of_membership at y1, m with hy1 have x2Less := Less.of_membership at x2, m with hx2 have y2Less := Less.of_membership at y2, m with hy2 rw [qShape, rShape] at same rw [qShape, rShape] at distinct have cross := thueMap.cross with positive, below, x1Less, y1Less, x2Less, y2Less, same have order := Less.trichotomy at x1, x2 with x1Nat, x2Nat rcases order with less | equal | greater . symm at cross exact thue.of_cross with positive, less, x2Less, y2Less, y1Less, cross . exfalso rw equal at cross have congruent : CongruentMod(a * x2 + y2, a * x2 + y1, p) unfold CongruentMod exact cross have cancelled := CongruentMod.add_left_cancel with congruent unfold CongruentMod at cancelled have y1Below : y1 < p by arith have y2Below : y2 < p by arith have y1Mod := mod.of_less with y1Below have y2Mod := mod.of_less with y2Below rw [y1Mod, y2Mod] at cancelled apply distinct rw [equal, cancelled] rfl . exact thue.of_cross with positive, greater, x1Less, y1Less, y2Less, cross -- The integer form embeds the natural one: x = x, y = ±y. theorem thue.lemma (a p m : Nat) requires positive : 0 < p requires bound : p < m * m requires below : m ≤ p shows ∃ x ∈ ℤ, ∃ y ∈ ℤ, ¬(x = 0 ∧ y = 0) ∧ integerNatAbs(x) < m ∧ integerNatAbs(y) < m ∧ IntegerCongruent(integerOfNat(p), integerOfNat(a) * x, y) proof obtain ⟨x, xNat, y, yNat, xPositive, xLess, yLess, congruent⟩ := thue.natural at a, p, m with positive, bound, below have xAbs := integerNatAbs.ofNat at x have yAbs := integerNatAbs.ofNat at y have xNonzero : integerOfNat(x) ≠ 0 intro zero have characterization := integerOfNat.eq_zero_iff at x have xZero := characterization.forward with zero arith have xInteger : integerOfNat(x) ∈ ℤ by typecheck have yInteger : integerOfNat(y) ∈ ℤ by typecheck have product := integerOfNat.mul at a, x cases congruent with plus | minus . have natural : CongruentMod(a * x, y, p) unfold CongruentMod exact plus have bridge := IntegerCongruent.ofNat_iff at p, a * x, y have embedded := bridge.backward with natural refine ⟨integerOfNat(x), xInteger, integerOfNat(y), yInteger, ?_, ?_, ?_, ?_⟩ . intro both exact xNonzero with both.left . rw xAbs exact xLess . rw yAbs exact yLess . rw ← product exact embedded . have natural : CongruentMod(a * x + y, 0, p) unfold CongruentMod rw mod.zero_left exact minus have bridge := IntegerCongruent.ofNat_iff at p, a * x + y, 0 have embedded := bridge.backward with natural have negInteger : -integerOfNat(y) ∈ ℤ by typecheck have negAbs := integerNatAbs.neg at integerOfNat(y) refine ⟨integerOfNat(x), xInteger, -integerOfNat(y), negInteger, ?_, ?_, ?_, ?_⟩ . intro both exact xNonzero with both.left . rw xAbs exact xLess . rw [negAbs, yAbs] exact yLess . unfold IntegerCongruent unfold IntegerCongruent at embedded have shape : integerOfNat(a) * integerOfNat(x) - (-integerOfNat(y)) = integerOfNat(a * x + y) - integerOfNat(0) simp [integerOfNat.add, integerOfNat.mul, integerOfNat.zero] ring rw shape exact embedded