Grace/Cumulus/NumberTheory/Thue.g17
Imports
Grace.Cumulus.NumberTheory.IntegerPrime · Grace.Cumulus.Combinatorics.Pigeonhole · Grace.Cumulus.Nat.Least
Declarations
- squareBound.family def
- squareBound.inhabited theorem derivation
- squareBound.naturals theorem derivation
- squareBound def
- squareBound.above theorem derivation
- squareBound.least theorem derivation
- squareBound.positive theorem derivation
- squareBound.le theorem derivation
- thueMap defgraph
- thueMap.collision theorem derivation
- thueMap.pair_value theorem derivation
- thueMap.cross theorem derivation
- thue.of_cross theorem derivation
- thue.natural theorem derivation
- thue.lemma theorem derivation
Source
1-- Thue's lemma by pigeonhole: for 0 < p and m with m² > p and m ≤ p, every2-- a has a nonzero pair (x, y) of integers of absolute value below m with3-- a x ≡ y modulo p; with the least such m, and in a natural form with a4-- positive x.5-- Part of Cumulus, the Grace17 mathematics library.6-- Copyright (c) 2026 Grant Passmore.7-- Released under the Apache License 2.0. See LICENSE in the repository root.8-- Authors: Grant Passmore910module Grace.Cumulus.NumberTheory.Thue11import Grace.Cumulus.NumberTheory.IntegerPrime12import Grace.Cumulus.Combinatorics.Pigeonhole13import Grace.Cumulus.Nat.Least1415-- The least m with p < m²1617-- The naturals m with p < m², a nonempty set since p < (p + 1)².18def squareBound.family (p : Nat) : Set := { m ∈ ℕ | p < mul(m, m) }1920theorem squareBound.inhabited (p : Nat)21 shows Nonempty(squareBound.family(p))22proof23 have expand : (p + 1) * (p + 1) = p * p + 2 * p + 1 by ring24 have bound : p < (p + 1) * (p + 1)25 rw expand26 arith27 have successorNat : p + 1 ∈ ℕ by typecheck28 witness p + 129 exact squareBound.family.membership.backward with ⟨successorNat, bound⟩3031theorem squareBound.naturals (p : Nat)32 shows squareBound.family(p) ⊆ ℕ33proof34 intros m hm35 have both := squareBound.family.membership.forward with hm36 exact both.left3738-- The least m with p < m², at least 1 since 0² = 0.39def squareBound (p : Nat) : Nat := leastNatural(squareBound.family(p))40proof41 unfold squareBound42 have selected := leastNatural.selected at squareBound.family(p) with43 (squareBound.inhabited at p), (squareBound.naturals at p)44 exact nat.omega.backward with selected.left4546theorem squareBound.above (p : Nat)47 shows p < squareBound(p) * squareBound(p)48proof49 have selected := leastNatural.selected at squareBound.family(p) with50 (squareBound.inhabited at p), (squareBound.naturals at p)51 have member := selected.right.left52 have both := squareBound.family.membership.forward with member53 unfold squareBound54 exact both.right5556theorem squareBound.least (p k : Nat)57 requires below : k < squareBound(p)58 shows k * k ≤ p59proof60 by_contra notBelow61 have above := Less.of_not_LessEqual with notBelow62 have kNat : k ∈ ℕ by typecheck63 have member := squareBound.family.membership.backward with ⟨kNat, above⟩64 have least := leastNatural.least at squareBound.family(p), k with65 (squareBound.inhabited at p), (squareBound.naturals at p), member66 unfold squareBound at below67 arith6869theorem squareBound.positive (p : Nat)70 shows 0 < squareBound(p)71proof72 have above := squareBound.above at p73 by_contra notPositive74 have atMost := LessEqual.of_not_Less with notPositive75 have squares := LessEqual.mul with atMost, atMost76 have zero : 0 * 0 = 0 by compute77 rw zero at squares78 arith7980-- p < p² for 1 < p, so the least bound is at most p.81theorem squareBound.le (p : Nat)82 requires aboveOne : 1 < p83 shows squareBound(p) ≤ p84proof85 have twoLe : 2 ≤ p by arith86 obtain ⟨k, kNat, shape⟩ := LessEqual.exists_add with twoLe87 have expand : p * p = p + (2 + 3 * k + k * k) by ring [shape]88 have bound : p < p * p89 rw expand90 arith91 have pNat : p ∈ ℕ by typecheck92 have member := squareBound.family.membership.backward with ⟨pNat, bound⟩93 have least := leastNatural.least at squareBound.family(p), p with94 (squareBound.inhabited at p), (squareBound.naturals at p), member95 unfold squareBound96 exact least9798-- The pigeonhole map99100-- (x, y) ↦ (a x − y) mod p on m × m, with −y written as p − y for y < p.101defgraph thueMap (a p m : Nat) (q ∈ m × m) : p :=102 mod(add(mul(a, fst(q)), sub(p, snd(q))), p)103requires positive : 0 < p104proof105 have parts := product.membership.forward with member106 obtain ⟨x, hx, y, hy, shape⟩ := parts.right107 rw [shape, fst.pair, snd.pair]108 have nonzero := Less.ne_zero_of_positive with positive109 have less := mod.less at a * x + (p - y), p with nonzero110 exact membership.of_Less at (a * x + (p - y)) % p, p with *, less111112-- m² > p boxes: two distinct pairs share a value.113theorem thueMap.collision (a p m : Nat)114 requires positive : 0 < p115 requires bound : p < m * m116 shows ∃ q ∈ m × m, ∃ r ∈ m × m, q ≠ r ∧117 thueMap(a, p, m)(q) = thueMap(a, p, m)(r)118proof119 have function := thueMap.function at a, p, m with positive120 have finite := Finite.natural at p121 have finiteM := Finite.natural at m122 have count := cardinality.product_count at m, m with finiteM, finiteM123 have countP := cardinality.natural at p124 have countM := cardinality.natural at m125 have more : cardinality(p) < cardinality(m × m)126 rw [count, countP, countM]127 exact bound128 exact pigeonhole.collision with function, finite, more129130-- The value at a pair of naturals below m.131theorem thueMap.pair_value (a p m x y : Nat)132 requires positive : 0 < p133 requires hx : x < m134 requires hy : y < m135 shows thueMap(a, p, m)((x, y)) = (a * x + (p - y)) % p136proof137 have xMember := membership.of_Less at x, m with *, hx138 have yMember := membership.of_Less at y, m with *, hy139 have member := product.pair at x, y, m, m with xMember, yMember140 have value := thueMap.value at a, p, m, (x, y) with positive, member141 rw [fst.pair, snd.pair] at value142 exact value143144-- Thue's lemma145146-- Two colliding pairs satisfy a x₁ + y₂ ≡ a x₂ + y₁: add y₁ + y₂ to both147-- values and drop the p.148theorem thueMap.cross (a p m x1 y1 x2 y2 : Nat)149 requires positive : 0 < p150 requires below : m ≤ p151 requires hx1 : x1 < m152 requires hy1 : y1 < m153 requires hx2 : x2 < m154 requires hy2 : y2 < m155 requires same : thueMap(a, p, m)((x1, y1)) = thueMap(a, p, m)((x2, y2))156 shows (a * x1 + y2) % p = (a * x2 + y1) % p157proof158 have nonzero := Less.ne_zero_of_positive with positive159 have leftValue := thueMap.pair_value at a, p, m, x1, y1 with160 positive, hx1, hy1161 have rightValue := thueMap.pair_value at a, p, m, x2, y2 with162 positive, hx2, hy2163 rw [leftValue, rightValue] at same164 have y1Le : y1 ≤ p by arith165 have y2Le : y2 ≤ p by arith166 obtain ⟨d1, d1Nat, split1⟩ := LessEqual.exists_add with y1Le167 obtain ⟨d2, d2Nat, split2⟩ := LessEqual.exists_add with y2Le168 have d1Sum : d1 + y1 = p by ring [split1]169 have d2Sum : d2 + y2 = p by ring [split2]170 have d1Value := sub.of_add_eq with d1Sum171 have d2Value := sub.of_add_eq with d2Sum172 rw [d1Value, d2Value] at same173 have leftSplit := mod.add at p, a * x1 + d1, y1 + y2 with nonzero174 have rightSplit := mod.add at p, a * x2 + d2, y1 + y2 with nonzero175 have shifted : (a * x1 + d1 + (y1 + y2)) % p = (a * x2 + d2 + (y1 + y2)) % p176 rw [leftSplit, rightSplit, same]177 rfl178 have leftShape : a * x1 + d1 + (y1 + y2) = a * x1 + y2 + p by ring [split1]179 have rightShape : a * x2 + d2 + (y1 + y2) = a * x2 + y1 + p by ring [split2]180 rw [leftShape, rightShape] at shifted181 have leftDrop := mod.add_self at p, a * x1 + y2 with nonzero182 have rightDrop := mod.add_self at p, a * x2 + y1 with nonzero183 rw [leftDrop, rightDrop] at shifted184 exact shifted185186-- From a x₁ + y₂ ≡ a x₂ + y₁ with x₂ < x₁: the differences x = x₁ − x₂ and187-- y = |y₁ − y₂| satisfy a x ≡ y or a x + y ≡ 0 by the sign of y₁ − y₂.188theorem thue.of_cross (a p m x1 y1 x2 y2 : Nat)189 requires positive : 0 < p190 requires order : x2 < x1191 requires hx1 : x1 < m192 requires hy1 : y1 < m193 requires hy2 : y2 < m194 requires cross : (a * x1 + y2) % p = (a * x2 + y1) % p195 shows ∃ x, Nat(x) ∧ ∃ y, Nat(y) ∧ 0 < x ∧ x < m ∧ y < m ∧196 ((a * x) % p = y % p ∨ (a * x + y) % p = 0)197proof198 have xLe : x2 ≤ x1 by arith199 obtain ⟨x, xNat, xSplit⟩ := LessEqual.exists_add with xLe200 have total := LessEqual.total at y2, y1201 cases total with below | above202 . obtain ⟨y, yNat, ySplit⟩ := LessEqual.exists_add with below203 refine ⟨x, xNat, y, yNat, ?_, ?_, ?_, ?_⟩204 . arith205 . arith206 . arith207 . left208 have congruent : CongruentMod(a * x2 + y2 + a * x, a * x2 + y2 + y, p)209 unfold CongruentMod210 have leftShape : a * x2 + y2 + a * x = a * x1 + y2 by ring [xSplit]211 have rightShape : a * x2 + y2 + y = a * x2 + y1 by ring [ySplit]212 rw [leftShape, rightShape]213 exact cross214 have cancelled := CongruentMod.add_left_cancel with congruent215 unfold CongruentMod at cancelled216 exact cancelled217 . obtain ⟨y, yNat, ySplit⟩ := LessEqual.exists_add with above218 refine ⟨x, xNat, y, yNat, ?_, ?_, ?_, ?_⟩219 . arith220 . arith221 . arith222 . right223 have congruent :224 CongruentMod(a * x2 + y1 + (a * x + y), a * x2 + y1 + 0, p)225 unfold CongruentMod226 have leftShape : a * x2 + y1 + (a * x + y) = a * x1 + y2227 ring [xSplit, ySplit]228 have rightShape : a * x2 + y1 + 0 = a * x2 + y1 by ring229 rw [leftShape, rightShape]230 exact cross231 have cancelled := CongruentMod.add_left_cancel with congruent232 unfold CongruentMod at cancelled233 have zero := mod.zero_left at p234 rw zero at cancelled235 exact cancelled236237-- The natural form: the two colliding pairs differ in x, since equal x238-- would force y₁ ≡ y₂ with y₁, y₂ < m ≤ p, and then the pairs would be the239-- same; order them by x and take the differences.240theorem thue.natural (a p m : Nat)241 requires positive : 0 < p242 requires bound : p < m * m243 requires below : m ≤ p244 shows ∃ x, Nat(x) ∧ ∃ y, Nat(y) ∧ 0 < x ∧ x < m ∧ y < m ∧245 ((a * x) % p = y % p ∨ (a * x + y) % p = 0)246proof247 obtain ⟨q, hq, r, hr, distinct, same⟩ :=248 thueMap.collision at a, p, m with positive, bound249 have qParts := product.membership.forward with hq250 obtain ⟨x1, hx1, y1, hy1, qShape⟩ := qParts.right251 have rParts := product.membership.forward with hr252 obtain ⟨x2, hx2, y2, hy2, rShape⟩ := rParts.right253 have x1Nat := natural.member at m with hx1254 have x2Nat := natural.member at m with hx2255 have x1Less := Less.of_membership at x1, m with hx1256 have y1Less := Less.of_membership at y1, m with hy1257 have x2Less := Less.of_membership at x2, m with hx2258 have y2Less := Less.of_membership at y2, m with hy2259 rw [qShape, rShape] at same260 rw [qShape, rShape] at distinct261 have cross := thueMap.cross with262 positive, below, x1Less, y1Less, x2Less, y2Less, same263 have order := Less.trichotomy at x1, x2 with x1Nat, x2Nat264 rcases order with less | equal | greater265 . symm at cross266 exact thue.of_cross with positive, less, x2Less, y2Less, y1Less, cross267 . exfalso268 rw equal at cross269 have congruent : CongruentMod(a * x2 + y2, a * x2 + y1, p)270 unfold CongruentMod271 exact cross272 have cancelled := CongruentMod.add_left_cancel with congruent273 unfold CongruentMod at cancelled274 have y1Below : y1 < p by arith275 have y2Below : y2 < p by arith276 have y1Mod := mod.of_less with y1Below277 have y2Mod := mod.of_less with y2Below278 rw [y1Mod, y2Mod] at cancelled279 apply distinct280 rw [equal, cancelled]281 rfl282 . exact thue.of_cross with positive, greater, x1Less, y1Less, y2Less, cross283284-- The integer form embeds the natural one: x = x, y = ±y.285theorem thue.lemma (a p m : Nat)286 requires positive : 0 < p287 requires bound : p < m * m288 requires below : m ≤ p289 shows ∃ x ∈ ℤ, ∃ y ∈ ℤ, ¬(x = 0 ∧ y = 0) ∧290 integerNatAbs(x) < m ∧ integerNatAbs(y) < m ∧291 IntegerCongruent(integerOfNat(p), integerOfNat(a) * x, y)292proof293 obtain ⟨x, xNat, y, yNat, xPositive, xLess, yLess, congruent⟩ :=294 thue.natural at a, p, m with positive, bound, below295 have xAbs := integerNatAbs.ofNat at x296 have yAbs := integerNatAbs.ofNat at y297 have xNonzero : integerOfNat(x) ≠ 0298 intro zero299 have characterization := integerOfNat.eq_zero_iff at x300 have xZero := characterization.forward with zero301 arith302 have xInteger : integerOfNat(x) ∈ ℤ by typecheck303 have yInteger : integerOfNat(y) ∈ ℤ by typecheck304 have product := integerOfNat.mul at a, x305 cases congruent with plus | minus306 . have natural : CongruentMod(a * x, y, p)307 unfold CongruentMod308 exact plus309 have bridge := IntegerCongruent.ofNat_iff at p, a * x, y310 have embedded := bridge.backward with natural311 refine ⟨integerOfNat(x), xInteger, integerOfNat(y), yInteger,312 ?_, ?_, ?_, ?_⟩313 . intro both314 exact xNonzero with both.left315 . rw xAbs316 exact xLess317 . rw yAbs318 exact yLess319 . rw ← product320 exact embedded321 . have natural : CongruentMod(a * x + y, 0, p)322 unfold CongruentMod323 rw mod.zero_left324 exact minus325 have bridge := IntegerCongruent.ofNat_iff at p, a * x + y, 0326 have embedded := bridge.backward with natural327 have negInteger : -integerOfNat(y) ∈ ℤ by typecheck328 have negAbs := integerNatAbs.neg at integerOfNat(y)329 refine ⟨integerOfNat(x), xInteger, -integerOfNat(y), negInteger,330 ?_, ?_, ?_, ?_⟩331 . intro both332 exact xNonzero with both.left333 . rw xAbs334 exact xLess335 . rw [negAbs, yAbs]336 exact yLess337 . unfold IntegerCongruent338 unfold IntegerCongruent at embedded339 have shape : integerOfNat(a) * integerOfNat(x) - (-integerOfNat(y)) =340 integerOfNat(a * x + y) - integerOfNat(0)341 simp [integerOfNat.add, integerOfNat.mul, integerOfNat.zero]342 ring343 rw shape344 exact embedded345