Grace/Cumulus/NumberTheory/GaussParity.g17
Imports
Grace.Cumulus.NumberTheory.GaussLemma · Grace.Cumulus.Cardinal · Grace.Cumulus.Nat.Division
Declarations
- floorWeights defgraph
- scaledWeights defgraph
- residueWeights defgraph
- foldedWeights defgraph
- identityWeights defgraph
- floorWeights.natural theorem derivation
- scaledWeights.natural theorem derivation
- residueWeights.natural theorem derivation
- foldedWeights.natural theorem derivation
- identityWeights.natural theorem derivation
- foldedWeights.small theorem derivation
- foldedWeights.large theorem derivation
- natSum.division_split theorem derivation
- natSum.scaled theorem derivation
- natSum.fold_split theorem derivation
- foldedWeights.member theorem derivation
- foldedWeights.self_map theorem derivation
- foldedWeights.same theorem derivation
- foldedWeights.injective theorem derivation
- foldedWeights.bijective theorem derivation
- natSum.folded_permutation theorem derivation
- gauss.parity_arithmetic theorem derivation
- gauss.parity theorem derivation
Source
1-- Eisenstein's parity form of Gauss's lemma: for an odd prime p = 2h + 12-- and an odd a not divisible by p, the Gauss count of a has the parity of3-- the floor sum Σ ⌊a k / p⌋ over k = 1..h.4-- Part of Cumulus, the Grace17 mathematics library.5-- Copyright (c) 2026 Grant Passmore.6-- Released under the Apache License 2.0. See LICENSE in the repository root.7-- Authors: Grant Passmore89module Grace.Cumulus.NumberTheory.GaussParity10import Grace.Cumulus.NumberTheory.GaussLemma11import Grace.Cumulus.Cardinal12import Grace.Cumulus.Nat.Division1314-- The weights on the half range are graphs on it with natural values:15-- ⌊a k / p⌋, a k, the residue (a k) % p, the residue folded onto 1..h,16-- and k itself.17defgraph floorWeights (a p h : Nat) (k ∈ halfRange(h)) : ℕ :=18 quotientNat(mul(a, k), p)19proof20 have kNat := halfRange.member_nat with member21 have natural := quotientNat.natural at p, a * k22 exact nat.omega.forward with natural2324defgraph scaledWeights (a h : Nat) (k ∈ halfRange(h)) : ℕ := mul(a, k)25proof26 have kNat := halfRange.member_nat with member27 have natural : Nat(a * k) by typecheck28 exact nat.omega.forward with natural2930defgraph residueWeights (a p h : Nat) (k ∈ halfRange(h)) : ℕ :=31 mod(mul(a, k), p)32proof33 have kNat := halfRange.member_nat with member34 have natural : Nat((a * k) % p) by typecheck35 exact nat.omega.forward with natural3637defgraph foldedWeights (a p h : Nat) (k ∈ halfRange(h)) : ℕ :=38 chooseNat(isZero(sub(mod(mul(a, k), p), h)), mod(mul(a, k), p),39 sub(p, mod(mul(a, k), p)))40proof41 have kNat := halfRange.member_nat with member42 have natural := chooseNat.natural at isZero((a * k) % p - h), (a * k) % p,43 p - (a * k) % p with *44 exact nat.omega.forward with natural4546defgraph identityWeights (h : Nat) (k ∈ halfRange(h)) : ℕ := k47proof48 have kNat := halfRange.member_nat with member49 exact nat.omega.forward with kNat5051theorem floorWeights.natural (a p h : Nat)52 shows ∀ v ∈ halfRange(h), floorWeights(a, p, h)(v) ∈ ℕ53proof54 intros v hv55 have vNat := halfRange.member_nat with hv56 rw floorWeights.value with hv57 have natural := quotientNat.natural at p, a * v58 exact nat.omega.forward with natural5960theorem scaledWeights.natural (a h : Nat)61 shows ∀ v ∈ halfRange(h), scaledWeights(a, h)(v) ∈ ℕ62proof63 intros v hv64 have vNat := halfRange.member_nat with hv65 rw scaledWeights.value with hv66 have natural : Nat(a * v) by typecheck67 exact nat.omega.forward with natural6869theorem residueWeights.natural (a p h : Nat)70 shows ∀ v ∈ halfRange(h), residueWeights(a, p, h)(v) ∈ ℕ71proof72 intros v hv73 have vNat := halfRange.member_nat with hv74 rw residueWeights.value with hv75 have natural : Nat((a * v) % p) by typecheck76 exact nat.omega.forward with natural7778theorem foldedWeights.natural (a p h : Nat)79 shows ∀ v ∈ halfRange(h), foldedWeights(a, p, h)(v) ∈ ℕ80proof81 intros v hv82 have vNat := halfRange.member_nat with hv83 rw foldedWeights.value with hv84 have natural := chooseNat.natural at isZero((a * v) % p - h), (a * v) % p,85 p - (a * v) % p with *86 exact nat.omega.forward with natural8788theorem identityWeights.natural (h : Nat)89 shows ∀ v ∈ halfRange(h), identityWeights(h)(v) ∈ ℕ90proof91 intros v hv92 have vNat := halfRange.member_nat with hv93 rw identityWeights.value with hv94 exact nat.omega.forward with vNat9596-- The folded weight of a small residue is the residue, of a large one its97-- complement p − r.98theorem foldedWeights.small (a p h k : Nat)99 requires member : k ∈ halfRange(h)100 requires small : (a * k) % p ≤ h101 shows foldedWeights(a, p, h)(k) = (a * k) % p102proof103 have test := isZero.sub_of_le with small104 rw [foldedWeights.value with member, test]105 simp [chooseNat]106107theorem foldedWeights.large (a p h k : Nat)108 requires member : k ∈ halfRange(h)109 requires large : h < (a * k) % p110 shows foldedWeights(a, p, h)(k) = p - (a * k) % p111proof112 have test := isZero.sub_of_Less with large113 rw [foldedWeights.value with member, test]114 simp [chooseNat]115116-- The three sum identities over 1..h, each an instance of natSum.linear117-- with the indicator of ∅ as the zero weight and the indicator of the118-- range itself as the constant weight 1. Division: Σ a k = p Σ ⌊a k / p⌋ +119-- Σ (a k) % p, from quotientNat.equation pointwise.120theorem natSum.division_split (a p h : Nat)121 requires positive : 0 < p122 shows natSum(halfRange(h), scaledWeights(a, h)) =123 add(mul(p, natSum(halfRange(h), floorWeights(a, p, h))),124 natSum(halfRange(h), residueWeights(a, p, h)))125proof126 have finite := halfRange.finite at h127 have zeroWeights := indicatorMap.natural at ∅, halfRange(h)128 have agree : ∀ v ∈ halfRange(h), add(scaledWeights(a, h)(v),129 indicatorMap(∅, halfRange(h))(v)) =130 add(mul(p, floorWeights(a, p, h)(v)), residueWeights(a, p, h)(v))131 intros v hv132 have vNat := halfRange.member_nat with hv133 have absent : ¬v ∈ ∅134 intro inside135 exact set.empty with inside136 rw [scaledWeights.value with hv,137 (indicatorMap.value at ∅, halfRange(h), v with hv),138 floorWeights.value with hv, residueWeights.value with hv,139 indicator.absent with absent]140 have equation := quotientNat.equation at p, a * v141 have unit : a * v + 0 = a * v by ring142 rw unit143 exact equation144 have law := natSum.linear with finite, (scaledWeights.natural at a, h),145 zeroWeights, (floorWeights.natural at a, p, h),146 (residueWeights.natural at a, p, h), agree147 have sumNat := natSum.natural with finite, (scaledWeights.natural at a, h)148 rw [natSum.empty_indicator, add.right_unit] at law149 exact law150151-- Scaling: Σ a k = a Σ k.152theorem natSum.scaled (a h : Nat)153 shows natSum(halfRange(h), scaledWeights(a, h)) =154 mul(a, natSum(halfRange(h), identityWeights(h)))155proof156 have finite := halfRange.finite at h157 have zeroWeights := indicatorMap.natural at ∅, halfRange(h)158 have agree : ∀ v ∈ halfRange(h), add(scaledWeights(a, h)(v),159 indicatorMap(∅, halfRange(h))(v)) =160 add(mul(a, identityWeights(h)(v)), indicatorMap(∅, halfRange(h))(v))161 intros v hv162 rw [scaledWeights.value with hv, identityWeights.value with hv]163 rfl164 have law := natSum.linear with finite, (scaledWeights.natural at a, h),165 zeroWeights, (identityWeights.natural at h), zeroWeights, agree166 have sumNat := natSum.natural with finite, (scaledWeights.natural at a, h)167 have identityNat := natSum.natural with finite, (identityWeights.natural at h)168 rw [natSum.empty_indicator, add.right_unit, add.right_unit] at law169 exact law170171-- Folding: over the Gauss set the residue is p minus the folded value,172-- elsewhere the two agree, so Σ r_k + 2 Σ_{Gauss} r'_k = Σ r'_k + μ p.173theorem natSum.fold_split (a p h : Nat)174 requires positive : 0 < p175 shows add(natSum(halfRange(h), residueWeights(a, p, h)),176 mul(2, natSum(gaussSet(a, p, h), foldedWeights(a, p, h)))) =177 add(natSum(halfRange(h), foldedWeights(a, p, h)),178 mul(cardinality(gaussSet(a, p, h)), p))179proof180 have nonzero : p ≠ 0 by arith181 have finite := halfRange.finite at h182 have gaussFinite := gaussSet.finite at a, p, h183 have subset := gaussSet.subset at a, p, h184 have restInside := difference.subset_left at halfRange(h), gaussSet(a, p, h)185 have restFinite := Finite.subset with finite, restInside186 have disjoint := Disjoint.difference_right at halfRange(h), gaussSet(a, p, h)187 have cover := difference.union2_of_subset with subset188 have residueNat := residueWeights.natural at a, p, h189 have foldedNat := foldedWeights.natural at a, p, h190 have residueGauss : ∀ v ∈ gaussSet(a, p, h),191 residueWeights(a, p, h)(v) ∈ ℕ192 intros v hv193 exact residueNat with v, (subset with v, hv)194 have foldedGauss : ∀ v ∈ gaussSet(a, p, h),195 foldedWeights(a, p, h)(v) ∈ ℕ196 intros v hv197 exact foldedNat with v, (subset with v, hv)198 have residueRest : ∀ v ∈ halfRange(h) ∖ gaussSet(a, p, h),199 residueWeights(a, p, h)(v) ∈ ℕ200 intros v hv201 exact residueNat with v, (restInside with v, hv)202 have foldedRest : ∀ v ∈ halfRange(h) ∖ gaussSet(a, p, h),203 foldedWeights(a, p, h)(v) ∈ ℕ204 intros v hv205 exact foldedNat with v, (restInside with v, hv)206 -- Both sums split over the Gauss set and its complement in 1..h.207 have splitResidue := natSum.union_disjoint with gaussFinite, restFinite,208 residueGauss, residueRest, disjoint209 have splitFolded := natSum.union_disjoint with gaussFinite, restFinite,210 foldedGauss, foldedRest, disjoint211 rw cover at splitResidue212 rw cover at splitFolded213 -- Off the Gauss set the residue is small and folding keeps it.214 have agreeRest : ∀ v ∈ halfRange(h) ∖ gaussSet(a, p, h),215 residueWeights(a, p, h)(v) = foldedWeights(a, p, h)(v)216 intros v hv217 have parts := difference.membership.forward with hv218 have inRange := parts.left219 have vNat := halfRange.member_nat with inRange220 have bounds := (halfRange.mem_iff at h, v).forward with inRange221 obtain ⟨vPositive, vBound⟩ := bounds222 have small : (a * v) % p ≤ h223 have cases := Less.or_LessEqual at h, (a * v) % p224 cases cases with large | small225 . exfalso226 have inGauss := (gaussSet.mem_iff at a, p, h, v).backward227 with ⟨vPositive, vBound, large⟩228 exact parts.right with inGauss229 . exact small230 rw [residueWeights.value with inRange,231 (foldedWeights.small with inRange, small)]232 rfl233 have sameRest := natSum.congr with agreeRest234 -- On the Gauss set residue and folded value add to p, so the two sums235 -- there add to p μ.236 have agreeGauss : ∀ v ∈ gaussSet(a, p, h), add(residueWeights(a, p, h)(v),237 foldedWeights(a, p, h)(v)) =238 add(mul(p, indicatorMap(gaussSet(a, p, h), gaussSet(a, p, h))(v)),239 indicatorMap(∅, gaussSet(a, p, h))(v))240 intros v hv241 have inRange := subset with v, hv242 have vNat := halfRange.member_nat with inRange243 have all := (gaussSet.mem_iff at a, p, h, v).forward with hv244 obtain ⟨vPositive, vBound, large⟩ := all245 have absent : ¬v ∈ ∅246 intro inside247 exact set.empty with inside248 rw [residueWeights.value with inRange,249 (foldedWeights.large with inRange, large),250 (indicatorMap.value at gaussSet(a, p, h), gaussSet(a, p, h), v with hv),251 (indicatorMap.value at ∅, gaussSet(a, p, h), v with hv),252 indicator.member with hv, indicator.absent with absent]253 have residueBelow := mod.less at a * v, p with nonzero254 have residueLe : (a * v) % p ≤ p by arith255 have pieces := sub.add_cancel with residueLe256 have total : (a * v) % p + (p - (a * v) % p) = p by arith257 rw total258 ring259 have law := natSum.linear with gaussFinite, residueGauss, foldedGauss,260 (indicatorMap.natural at gaussSet(a, p, h), gaussSet(a, p, h)),261 (indicatorMap.natural at ∅, gaussSet(a, p, h)), agreeGauss262 rw [natSum.self_indicator with gaussFinite, natSum.empty_indicator] at law263 have residueGaussNat := natSum.natural with gaussFinite, residueGauss264 have foldedGaussNat := natSum.natural with gaussFinite, foldedGauss265 have foldedRestNat := natSum.natural with restFinite, foldedRest266 rw [splitResidue, splitFolded, sameRest]267 have commute : mul(cardinality(gaussSet(a, p, h)), p) =268 mul(p, cardinality(gaussSet(a, p, h))) by ring269 rw commute270 rw add.right_unit at law271 rw ← law272 arith273274-- The folding permutes 1..h: the folded residue of k ∈ 1..h lies in 1..h,275-- and two residues folding to the same value come from the same k.276theorem foldedWeights.member (a p h : Nat)277 requires prime : Prime(p)278 requires odd : p = 2 * h + 1279 requires notDivides : ¬(p ∣ a)280 shows ∀ k ∈ halfRange(h), foldedWeights(a, p, h)(k) ∈ halfRange(h)281proof282 intros k hk283 have nonzero := Prime.nonzero with prime284 have kNat := halfRange.member_nat with hk285 have bounds := (halfRange.mem_iff at h, k).forward with hk286 obtain ⟨kPositive, kBound⟩ := bounds287 have coprime := Prime.coprime_of_not_divides_gcd with prime, notDivides288 have residueBelow := mod.less at a * k, p with nonzero289 -- The residue is not 0, since p ∤ a k.290 have residuePositive : 0 < (a * k) % p291 have notZero : (a * k) % p ≠ 0292 intro zero293 have divides := (mod.zero_iff at p, a * k with nonzero).forward with zero294 have cancelled := coprime.cancel with coprime, divides295 have kNonzero : k ≠ 0 by arith296 have le := divides.le with cancelled, kNonzero297 have impossible : ¬p ≤ k by arith298 exact impossible with le299 arith300 have cases := Less.or_LessEqual at h, (a * k) % p301 cases cases with large | small302 . rw foldedWeights.large with hk, large303 have residueLe : (a * k) % p ≤ p by arith304 have pieces := sub.add_cancel with residueLe305 have lower : 0 < p - (a * k) % p by arith306 have upper : p - (a * k) % p ≤ h by arith307 exact (halfRange.mem_iff at h, p - (a * k) % p).backward308 with ⟨lower, upper⟩309 . rw foldedWeights.small with hk, small310 exact (halfRange.mem_iff at h, (a * k) % p).backward311 with ⟨residuePositive, small⟩312313theorem foldedWeights.self_map (a p h : Nat)314 requires prime : Prime(p)315 requires odd : p = 2 * h + 1316 requires notDivides : ¬(p ∣ a)317 shows foldedWeights(a, p, h) : halfRange(h) → halfRange(h)318proof319 have function := foldedWeights.function at a, p, h320 have values := foldedWeights.member with prime, odd, notDivides321 exact FunctionBetween.codomain_of_values with function, values322323-- Equal folded values mean a k ≡ ±a j, so k ≡ ±j with p ∤ a; the sum324-- 0 < k + j ≤ 2h < p rules out the sign −, and k, j < p give k = j.325theorem foldedWeights.same (a p h k j : Nat)326 requires prime : Prime(p)327 requires odd : p = 2 * h + 1328 requires notDivides : ¬(p ∣ a)329 requires hk : k ∈ halfRange(h)330 requires hj : j ∈ halfRange(h)331 requires same : foldedWeights(a, p, h)(k) = foldedWeights(a, p, h)(j)332 shows k = j333proof334 have nonzero := Prime.nonzero with prime335 have kBounds := (halfRange.mem_iff at h, k).forward with hk336 obtain ⟨kPositive, kBound⟩ := kBounds337 have jBounds := (halfRange.mem_iff at h, j).forward with hj338 obtain ⟨jPositive, jBound⟩ := jBounds339 have coprime := Prime.coprime_of_not_divides_gcd with prime, notDivides340 have kBelow : k < p by arith341 have jBelow : j < p by arith342 have kSelf := mod.of_less with kBelow343 have jSelf := mod.of_less with jBelow344 have kResidue := mod.less at a * k, p with nonzero345 have jResidue := mod.less at a * j, p with nonzero346 have kLe : (a * k) % p ≤ p by arith347 have jLe : (a * j) % p ≤ p by arith348 have kPieces := sub.add_cancel with kLe349 have jPieces := sub.add_cancel with jLe350 have ofEqual : (a * k) % p = (a * j) % p → k = j351 intro equal352 have cancelled := coprime.mul_cancel_mod with nonzero, coprime, equal353 rwa [kSelf, jSelf] at cancelled354 have ofSum : (a * k) % p + (a * j) % p = p → k = j355 intro total356 exfalso357 have split := mod.add at p, a * k, a * j with nonzero358 have vanish := mod.self at p359 rw [total, vanish] at split360 have factor : a * k + a * j = a * (k + j) by ring361 rw factor at split362 have divides := (mod.zero_iff at p, a * (k + j) with nonzero).forward363 with split364 have cancelled := coprime.cancel with coprime, divides365 have sumNonzero : k + j ≠ 0 by arith366 have le := divides.le with cancelled, sumNonzero367 have impossible : ¬p ≤ k + j by arith368 exact impossible with le369 have kCases := Less.or_LessEqual at h, (a * k) % p370 have jCases := Less.or_LessEqual at h, (a * j) % p371 cases kCases with kLarge | kSmall372 . rw foldedWeights.large with hk, kLarge at same373 cases jCases with jLarge | jSmall374 . rw foldedWeights.large with hj, jLarge at same375 apply ofEqual376 arith377 . rw foldedWeights.small with hj, jSmall at same378 apply ofSum379 arith380 . rw foldedWeights.small with hk, kSmall at same381 cases jCases with jLarge | jSmall382 . rw foldedWeights.large with hj, jLarge at same383 apply ofSum384 arith385 . rw foldedWeights.small with hj, jSmall at same386 exact ofEqual with same387388theorem foldedWeights.injective (a p h : Nat)389 requires prime : Prime(p)390 requires odd : p = 2 * h + 1391 requires notDivides : ¬(p ∣ a)392 shows InjectiveBetween(foldedWeights(a, p, h), halfRange(h), halfRange(h))393proof394 have function := foldedWeights.self_map with prime, odd, notDivides395 constructor396 . exact function397 . intros x hx y hy z hz hxz hyz398 have xValue := FunctionBetween.apply_value with function, hx, hxz399 have yValue := FunctionBetween.apply_value with function, hy, hyz400 have xNat := halfRange.member_nat with hx401 have yNat := halfRange.member_nat with hy402 have same : foldedWeights(a, p, h)(x) = foldedWeights(a, p, h)(y)403 rw [xValue, yValue]404 rfl405 exact foldedWeights.same with prime, odd, notDivides, hx, hy, same406407-- An injection of the finite set 1..h into itself is onto.408theorem foldedWeights.bijective (a p h : Nat)409 requires prime : Prime(p)410 requires odd : p = 2 * h + 1411 requires notDivides : ¬(p ∣ a)412 shows BijectiveBetween(foldedWeights(a, p, h), halfRange(h), halfRange(h))413proof414 have function := foldedWeights.self_map with prime, odd, notDivides415 have injective := foldedWeights.injective with prime, odd, notDivides416 have finite := halfRange.finite at h417 have reflexive : halfRange(h) ⊆ halfRange(h)418 intros y hy419 exact hy420 have imageInside := functionImage.subset at foldedWeights(a, p, h),421 halfRange(h), halfRange(h)422 have count := cardinality.injective_image with injective, reflexive, finite423 have onto := cardinality.eq_subset with finite, imageInside, count424 have surjective := functionImage.surjective with function425 rw onto at surjective426 exact ⟨injective, surjective⟩427428-- The folded residues permute 1..h, so their sum is Σ k: reindex the sum429-- along the folding bijection.430theorem natSum.folded_permutation (a p h : Nat)431 requires prime : Prime(p)432 requires odd : p = 2 * h + 1433 requires notDivides : ¬(p ∣ a)434 shows natSum(halfRange(h), foldedWeights(a, p, h)) =435 natSum(halfRange(h), identityWeights(h))436proof437 have finite := halfRange.finite at h438 have bijective := foldedWeights.bijective with prime, odd, notDivides439 have members := foldedWeights.member with prime, odd, notDivides440 have values : ∀ s ∈ halfRange(h), foldedWeights(a, p, h)(s) =441 identityWeights(h)(foldedWeights(a, p, h)(s))442 intros s hs443 have inside := members with s, hs444 rw identityWeights.value with inside445 rfl446 have reindexed := natSum.reindex with finite, bijective,447 (identityWeights.natural at h), values448 symm449 exact reindexed450451-- Eisenstein's parity. Combining the three identities,452-- (a − 1) Σ k = p (Σ ⌊a k / p⌋ + μ) − 2 Σ_{Gauss} r'_k; with a odd the453-- left side is even and p is odd, so μ + Σ ⌊a k / p⌋ is even. The454-- bookkeeping is done on naturals by canceling Σ r_k and Σ k from both455-- sides of the combined identity.456theorem gauss.parity_arithmetic (p m F count I R G : Nat)457 requires combined : (2 * m + 1) * I + (R + 2 * G) =458 (p * F + R) + (I + count * p)459 shows p * (F + count) = 2 * (m * I + G)460proof461 have left : p * (F + count) + (R + I) = (p * F + R) + (I + count * p)462 by ring463 have right : 2 * (m * I + G) + (R + I) = (2 * m + 1) * I + (R + 2 * G)464 by ring465 rw [← combined, ← right] at left466 arith467468theorem gauss.parity (a p h : Nat)469 requires prime : Prime(p)470 requires odd : p = 2 * h + 1471 requires oddA : Odd(a)472 requires notDivides : ¬(p ∣ a)473 shows Even(add(cardinality(gaussSet(a, p, h)),474 natSum(halfRange(h), floorWeights(a, p, h))))475proof476 have positive : 0 < p by arith477 have finite := halfRange.finite at h478 have gaussFinite := gaussSet.finite at a, p, h479 have subset := gaussSet.subset at a, p, h480 have foldedGauss : ∀ v ∈ gaussSet(a, p, h),481 foldedWeights(a, p, h)(v) ∈ ℕ482 intros v hv483 exact foldedWeights.natural at a, p, h with v, (subset with v, hv)484 have floorNat := natSum.natural with finite, (floorWeights.natural at a, p, h)485 have residueNat := natSum.natural with finite,486 (residueWeights.natural at a, p, h)487 have identityNat := natSum.natural with finite, (identityWeights.natural at h)488 have foldedGaussNat := natSum.natural with gaussFinite, foldedGauss489 -- The three identities, with Σ a k written as a Σ k and the folded sum490 -- as Σ k.491 have division := natSum.division_split at a, p, h with positive492 have scaled := natSum.scaled at a, h493 have fold := natSum.fold_split at a, p, h with positive494 have permutation := natSum.folded_permutation with prime, odd, notDivides495 rw [scaled] at division496 rw [permutation] at fold497 have oddForm := (Odd.iff_two_mul_add_one at a).forward with oddA498 obtain ⟨m, mNat, aForm⟩ := oddForm499 have scaledA : mul(a, natSum(halfRange(h), identityWeights(h))) =500 mul(2 * m + 1, natSum(halfRange(h), identityWeights(h)))501 rw aForm502 rfl503 have combined :504 add(mul(2 * m + 1, natSum(halfRange(h), identityWeights(h))),505 add(natSum(halfRange(h), residueWeights(a, p, h)),506 mul(2, natSum(gaussSet(a, p, h), foldedWeights(a, p, h))))) =507 add(add(mul(p, natSum(halfRange(h), floorWeights(a, p, h))),508 natSum(halfRange(h), residueWeights(a, p, h))),509 add(natSum(halfRange(h), identityWeights(h)),510 mul(cardinality(gaussSet(a, p, h)), p)))511 rw [← scaledA, division, fold]512 rfl513 have equation : mul(p, add(natSum(halfRange(h), floorWeights(a, p, h)),514 cardinality(gaussSet(a, p, h)))) =515 mul(2, add(mul(m, natSum(halfRange(h), identityWeights(h))),516 natSum(gaussSet(a, p, h), foldedWeights(a, p, h))))517 exact gauss.parity_arithmetic at p, m,518 natSum(halfRange(h), floorWeights(a, p, h)),519 cardinality(gaussSet(a, p, h)), natSum(halfRange(h), identityWeights(h)),520 natSum(halfRange(h), residueWeights(a, p, h)),521 natSum(gaussSet(a, p, h), foldedWeights(a, p, h)) with combined522 -- p times the sum is even and p is odd.523 have even : Even(mul(p, add(natSum(halfRange(h), floorWeights(a, p, h)),524 cardinality(gaussSet(a, p, h)))))525 rw equation526 exact Even.two_mul at add(mul(m, natSum(halfRange(h), identityWeights(h))),527 natSum(gaussSet(a, p, h), foldedWeights(a, p, h)))528 have split := Even.of_mul with even529 cases split with evenP | evenSum530 . exfalso531 have oddP : Odd(p)532 rw odd533 have hNat : Nat(h) by typecheck534 have form : 2 * h + 1 = 2 * h + 1 by rfl535 exact (Odd.iff_two_mul_add_one at 2 * h + 1).backward536 with ⟨h, hNat, form⟩537 exact Odd.not_even with oddP, evenP538 . have commute : add(cardinality(gaussSet(a, p, h)),539 natSum(halfRange(h), floorWeights(a, p, h))) =540 add(natSum(halfRange(h), floorWeights(a, p, h)),541 cardinality(gaussSet(a, p, h))) by ring542 rw commute543 exact evenSum544