-- The half range 1..h, the Gauss set of k in it whose residue a k mod p -- exceeds h, the folding of a residue list onto 1..h, the count of large -- residues in a list, and the bridge between that list count and the set -- cardinality. -- 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.GaussCount import Grace.Cumulus.NumberTheory.Wilson import Grace.Cumulus.NumberTheory.ResidueLists import Grace.Cumulus.Cardinal -- The half range {1, …, h}: a finite set of naturals with h members. def halfRange (h : Nat) : Set := { k ∈ succ(h) | 0 < k } theorem halfRange.mem_iff (h k : Nat) shows k ∈ halfRange(h) ↔ 0 < k ∧ k ≤ h proof constructor . intro member have parts := halfRange.membership.forward with member have below := Less.of_membership at k, succ(h) with parts.left have bound : k ≤ h by arith exact ⟨parts.right, bound⟩ . intro both obtain ⟨positive, bound⟩ := both have below : k < succ(h) by arith have inSucc := membership.of_Less at k, succ(h) with *, *, below exact halfRange.membership.backward with ⟨inSucc, positive⟩ -- A member of the half range is a natural. theorem halfRange.member_nat (h : Nat) (k : Set) requires member : k ∈ halfRange(h) shows Nat(k) proof have parts := halfRange.membership.forward with member exact natural.member at succ(h) with parts.left theorem halfRange.finite (h : Nat) shows Finite(halfRange(h)) proof have inside : halfRange(h) ⊆ succ(h) intros k member have parts := halfRange.membership.forward with member exact parts.left exact Finite.subset with (Finite.natural at succ(h)), inside theorem halfRange.succ (h : Nat) shows halfRange(succ(h)) = insert(succ(h), halfRange(h)) proof ext k constructor . intro member have parts := halfRange.membership.forward with member have kNat := natural.member at succ(succ(h)) with parts.left have both := (halfRange.mem_iff at succ(h), k).forward with member obtain ⟨positive, atMost⟩ := both have split := (LessEqual.iff_Less_or_eq at k, succ(h)).forward with atMost apply insert.membership.backward cases split with below | same . right have bound : k ≤ h by arith exact (halfRange.mem_iff at h, k).backward with ⟨positive, bound⟩ . left exact same . intro member have split := insert.membership.forward with member cases split with same | inRange . rw same have positive : 0 < succ(h) by arith have bound : succ(h) ≤ succ(h) by arith exact (halfRange.mem_iff at succ(h), succ(h)).backward with ⟨positive, bound⟩ . have parts := halfRange.membership.forward with inRange have kNat := natural.member at succ(h) with parts.left have both := (halfRange.mem_iff at h, k).forward with inRange obtain ⟨positive, atMost⟩ := both have bound : k ≤ succ(h) by arith exact (halfRange.mem_iff at succ(h), k).backward with ⟨positive, bound⟩ theorem halfRange.cardinality (h : Nat) shows cardinality(halfRange(h)) = h proof induction h | zero => have empty : halfRange(0) = ∅ ext k constructor . intro member exfalso have parts := halfRange.membership.forward with member have kNat := natural.member at succ(0) with parts.left have both := (halfRange.mem_iff at 0, k).forward with member obtain ⟨positive, bound⟩ := both have impossible : ¬k ≤ 0 by arith exact impossible with bound . intro member exfalso exact set.empty with member rw [empty, cardinality.empty] rfl | succ(j) using ih => have finite := halfRange.finite at j have fresh : ¬succ(j) ∈ halfRange(j) intro member have both := (halfRange.mem_iff at j, succ(j)).forward with member have impossible : ¬succ(j) ≤ j by arith exact impossible with both.right have count := cardinality.insert_add_one with finite, fresh rw [halfRange.succ, count, ih] exact add.right_one at j -- The Gauss set: the k in 1..h whose least residue a k mod p lies above h. def gaussSet (a p h : Nat) : Set := { k ∈ succ(h) | 0 < k ∧ h < mod(mul(a, k), p) } theorem gaussSet.mem_iff (a p h k : Nat) shows k ∈ gaussSet(a, p, h) ↔ 0 < k ∧ k ≤ h ∧ h < (a * k) % p proof constructor . intro member have parts := gaussSet.membership.forward with member have below := Less.of_membership at k, succ(h) with parts.left have bound : k ≤ h by arith exact ⟨parts.right.left, bound, parts.right.right⟩ . intro all obtain ⟨positive, bound, large⟩ := all have below : k < succ(h) by arith have inSucc := membership.of_Less at k, succ(h) with *, *, below exact gaussSet.membership.backward with ⟨inSucc, positive, large⟩ theorem gaussSet.subset (a p h : Nat) shows gaussSet(a, p, h) ⊆ halfRange(h) proof intros k member have parts := gaussSet.membership.forward with member exact halfRange.membership.backward with ⟨parts.left, parts.right.left⟩ theorem gaussSet.finite (a p h : Nat) shows Finite(gaussSet(a, p, h)) proof exact Finite.subset with (halfRange.finite at h), (gaussSet.subset at a, p, h) theorem gaussSet.cardinality_le (a p h : Nat) shows cardinality(gaussSet(a, p, h)) ≤ h proof have bound := cardinality.monotone with (halfRange.finite at h), (gaussSet.subset at a, p, h) rwa halfRange.cardinality at bound -- The count of entries of a list that exceed h. An entry r is large when -- r − h is nonzero, which the Boolean test isZero(r − h) decides. def largeCount (h : Nat, xs : List(ℕ)) → Nat | h, [] => 0 | h, r :: rest => chooseNat(isZero(sub(r, h)), largeCount(h, rest), succ(largeCount(h, rest))) theorem largeCount.nil (h : Nat) shows largeCount(h, []) = 0 proof simp [largeCount] theorem isZero.sub_of_le (h r : Nat) requires small : r ≤ h shows isZero(r - h) = Bool.true proof have zero := sub.eq_zero_of_le with small rw zero simp [isZero] theorem isZero.sub_of_Less (h r : Nat) requires large : h < r shows isZero(r - h) = Bool.false proof have positive := sub.positive_of_Less with large have shape := natural.zero_or_successor at r - h cases shape with zero | successor . exfalso rw zero at positive exact Less.irreflexive at 0 with *, positive . obtain ⟨k, kNat, form⟩ := successor rw form simp [isZero] theorem largeCount.cons_small (h r : Nat) (rest : List(ℕ)) requires small : r ≤ h shows largeCount(h, r :: rest) = largeCount(h, rest) proof have test := isZero.sub_of_le with small simp [largeCount, test, chooseNat] theorem largeCount.cons_large (h r : Nat) (rest : List(ℕ)) requires large : h < r shows largeCount(h, r :: rest) = succ(largeCount(h, rest)) proof have test := isZero.sub_of_Less with large simp [largeCount, test, chooseNat] theorem largeCount.natural (h : Nat) (xs : List(ℕ)) shows Nat(largeCount(h, xs)) proof induction xs | List.nil => rw largeCount.nil typecheck | List.cons(r, rest) using ih => have split := Less.or_LessEqual at h, r cases split with large | small . rw largeCount.cons_large typecheck . rw largeCount.cons_small exact ih -- The folding of a list onto 1..h: an entry r stays when r ≤ h and becomes -- p − r otherwise. def foldedList (p h : Nat, xs : List(ℕ)) → List(ℕ) | p, h, [] => [] | p, h, r :: rest => chooseNat(isZero(sub(r, h)), r, sub(p, r)) :: foldedList(p, h, rest) theorem foldedList.nil (p h : Nat) shows foldedList(p, h, []) = [] proof simp [foldedList] theorem foldedList.cons_small (p h r : Nat) (rest : List(ℕ)) requires small : r ≤ h shows foldedList(p, h, r :: rest) = r :: foldedList(p, h, rest) proof have test := isZero.sub_of_le with small simp [foldedList, test, chooseNat] theorem foldedList.cons_large (p h r : Nat) (rest : List(ℕ)) requires large : h < r shows foldedList(p, h, r :: rest) = (p - r) :: foldedList(p, h, rest) proof have test := isZero.sub_of_Less with large simp [foldedList, test, chooseNat] theorem foldedList.list (p h : Nat) (xs : List(ℕ)) shows List(ℕ, foldedList(p, h, xs)) proof induction xs | List.nil => rw foldedList.nil typecheck | List.cons(r, rest) using ih => have split := Less.or_LessEqual at h, r cases split with large | small . rw foldedList.cons_large typecheck . rw foldedList.cons_small typecheck theorem foldedList.length (p h : Nat) (xs : List(ℕ)) shows length{ℕ}(foldedList(p, h, xs)) = length{ℕ}(xs) proof induction xs | List.nil => rw foldedList.nil rfl | List.cons(r, rest) using ih => have right := length.cons at ℕ, r, rest with * have split := Less.or_LessEqual at h, r cases split with large | small . have left := length.cons at ℕ, p - r, foldedList(p, h, rest) with * rw [foldedList.cons_large, left, right, ih] rfl . have left := length.cons at ℕ, r, foldedList(p, h, rest) with * rw [foldedList.cons_small, left, right, ih] rfl -- Folding changes an entry below p by a sign, so the product of a list and -- the product of its folding agree modulo p when the large count is even -- and sum to 0 modulo p when it is odd. Both parities are carried through -- the induction at once, since folding one more entry trades them: a small -- entry keeps the count and the relation, a large entry p − r turns an -- agreement modulo p into a vanishing sum and back. theorem foldedList.product_sign (p h : Nat) (xs : List(ℕ)) shows (∀ r, Nat(r) → 0 < multiplicity(r, xs) → r < p) → (Even(largeCount(h, xs)) → (∏ xs) % p = (∏ foldedList(p, h, xs)) % p) ∧ (Odd(largeCount(h, xs)) → (∏ xs + ∏ foldedList(p, h, xs)) % p = 0) proof induction xs | List.nil => intro entries rw [foldedList.nil, largeCount.nil] constructor . intro even rfl . intro odd exfalso have even := Even.zero exact Even.not_odd at 0 with even, odd | List.cons(x, rest) using ih => intro entries have xPositive : 0 < multiplicity(x, x :: rest) have self := multiplicity.cons_self at x, rest with * rw self arith have xNat : Nat(x) by typecheck have xBelow := entries with x, xNat, xPositive have nonzero : p ≠ 0 intro zero rw zero at xBelow have impossible : ¬x < 0 by arith exact impossible with xBelow have restEntries : ∀ y, Nat(y) → 0 < multiplicity(y, rest) → y < p intros y yNat positive have lifted := multiplicity.positive_cons at y, x, rest with positive exact entries with y, yNat, lifted have previous := ih with restEntries have expandXs := listProduct.cons at x, rest with * have split := Less.or_LessEqual at h, x cases split with large | small . rw [largeCount.cons_large, foldedList.cons_large, expandXs] have expandFolded := listProduct.cons at p - x, foldedList(p, h, rest) with * rw expandFolded have xLe : x ≤ p by arith have splitP := mul.split_sub at p, x, ∏ foldedList(p, h, rest) with xLe constructor . intro even have shifted := (Even.succ_iff at largeCount(h, rest)).forward with even have restOdd := previous.right with shifted -- x R ≡ x R + p F = x (R + F) + (p − x) F, and x (R + F) ≡ 0. have identity : x * ∏ rest + p * ∏ foldedList(p, h, rest) = x * (∏ rest + ∏ foldedList(p, h, rest)) + (p - x) * ∏ foldedList(p, h, rest) rw splitP ring have multiple := mod.add_multiple at p, x * ∏ rest, ∏ foldedList(p, h, rest) with nonzero have modSplit := mod.add at p, x * (∏ rest + ∏ foldedList(p, h, rest)), (p - x) * ∏ foldedList(p, h, rest) with nonzero have step := mod.mul_right at p, x, ∏ rest + ∏ foldedList(p, h, rest) with nonzero have zero : x * 0 = 0 by ring have selfMod := mod.mod at p, (p - x) * ∏ foldedList(p, h, rest) with nonzero have zeroAdd : 0 + ((p - x) * ∏ foldedList(p, h, rest)) % p = ((p - x) * ∏ foldedList(p, h, rest)) % p by ring rw [← multiple, identity, modSplit, ← step, restOdd, zero, mod.zero_left, zeroAdd, selfMod] rfl . intro odd have shifted := (Odd.succ_iff at largeCount(h, rest)).forward with odd have restEven := previous.left with shifted -- x R + (p − x) F ≡ x F + (p − x) F = p F ≡ 0. have modSplit := mod.add at p, x * ∏ rest, (p - x) * ∏ foldedList(p, h, rest) with nonzero have left := mod.mul_right at p, x, ∏ rest with nonzero have right := mod.mul_right at p, x, ∏ foldedList(p, h, rest) with nonzero have merge := mod.add at p, x * ∏ foldedList(p, h, rest), (p - x) * ∏ foldedList(p, h, rest) with nonzero have total : x * ∏ foldedList(p, h, rest) + (p - x) * ∏ foldedList(p, h, rest) = 0 + p * ∏ foldedList(p, h, rest) rw splitP ring have multiple := mod.add_multiple at p, 0, ∏ foldedList(p, h, rest) with nonzero rw [modSplit, ← left, restEven, right, ← merge, total, multiple] exact mod.zero_left at p . rw [largeCount.cons_small, foldedList.cons_small, expandXs] have expandFolded := listProduct.cons at x, foldedList(p, h, rest) with * rw expandFolded constructor . intro even have restEven := previous.left with even have left := mod.mul_right at p, x, ∏ rest with nonzero have right := mod.mul_right at p, x, ∏ foldedList(p, h, rest) with nonzero rw [← left, restEven, right] rfl . intro odd have restOdd := previous.right with odd have factor : x * ∏ rest + x * ∏ foldedList(p, h, rest) = x * (∏ rest + ∏ foldedList(p, h, rest)) by ring have step := mod.mul_right at p, x, ∏ rest + ∏ foldedList(p, h, rest) with nonzero have zero : x * 0 = 0 by ring rw [factor, ← step, restOdd, zero] exact mod.zero_left at p theorem foldedList.product_even (p h : Nat) (xs : List(ℕ)) requires entries : ∀ r, Nat(r) → 0 < multiplicity(r, xs) → r < p requires even : Even(largeCount(h, xs)) shows (∏ xs) % p = (∏ foldedList(p, h, xs)) % p proof have both := foldedList.product_sign at p, h, xs with entries exact both.left with even theorem foldedList.product_odd (p h : Nat) (xs : List(ℕ)) requires entries : ∀ r, Nat(r) → 0 < multiplicity(r, xs) → r < p requires odd : Odd(largeCount(h, xs)) shows (∏ xs + ∏ foldedList(p, h, xs)) % p = 0 proof have both := foldedList.product_sign at p, h, xs with entries exact both.right with odd -- The residue list of 1..h scaled by a: the entries (a k) % p for -- k = h, …, 1, a list of length h. def gaussList (a p h : Nat) : Set := scaledList(a, p, factorialList(h)) theorem gaussList.value (a p h : Nat) shows gaussList(a, p, h) = scaledList(a, p, factorialList(h)) proof unfold gaussList rfl theorem gaussList.list (a p h : Nat) shows List(ℕ, gaussList(a, p, h)) proof rw gaussList.value exact scaledList.list at a, p, factorialList(h) with (factorialList.list at h) theorem gaussList.length (a p h : Nat) shows length{ℕ}(gaussList(a, p, h)) = h proof have list := factorialList.list at h rw [gaussList.value, scaledList.length, factorialList.length] rfl -- The bridge from lists to sets: the large entries of the residue list of -- 1..h are counted by the Gauss set. The induction grows the range 1..m -- while the threshold t stays, so the Gauss set is first taken with the -- two kept apart, and the threshold h is set to the range at the end. def gaussSetBelow (a p t m : Nat) : Set := { k ∈ succ(m) | 0 < k ∧ t < mod(mul(a, k), p) } theorem gaussSetBelow.mem_iff (a p t m k : Nat) shows k ∈ gaussSetBelow(a, p, t, m) ↔ 0 < k ∧ k ≤ m ∧ t < (a * k) % p proof constructor . intro member have parts := gaussSetBelow.membership.forward with member have below := Less.of_membership at k, succ(m) with parts.left have bound : k ≤ m by arith exact ⟨parts.right.left, bound, parts.right.right⟩ . intro all obtain ⟨positive, bound, large⟩ := all have below : k < succ(m) by arith have inSucc := membership.of_Less at k, succ(m) with *, *, below exact gaussSetBelow.membership.backward with ⟨inSucc, positive, large⟩ theorem gaussSetBelow.finite (a p t m : Nat) shows Finite(gaussSetBelow(a, p, t, m)) proof have inside : gaussSetBelow(a, p, t, m) ⊆ succ(m) intros k member have parts := gaussSetBelow.membership.forward with member exact parts.left exact Finite.subset with (Finite.natural at succ(m)), inside theorem gaussSetBelow.zero (a p t : Nat) shows gaussSetBelow(a, p, t, 0) = ∅ proof ext k constructor . intro member exfalso have parts := gaussSetBelow.membership.forward with member have kNat := natural.member at succ(0) with parts.left have all := (gaussSetBelow.mem_iff at a, p, t, 0, k).forward with member obtain ⟨positive, bound, large⟩ := all have impossible : ¬k ≤ 0 by arith exact impossible with bound . intro member exfalso exact set.empty with member theorem gaussSetBelow.fresh (a p t m : Nat) shows ¬succ(m) ∈ gaussSetBelow(a, p, t, m) proof intro member have all := (gaussSetBelow.mem_iff at a, p, t, m, succ(m)).forward with member obtain ⟨positive, bound, large⟩ := all have impossible : ¬succ(m) ≤ m by arith exact impossible with bound theorem gaussSetBelow.succ_large (a p t m : Nat) requires large : t < (a * succ(m)) % p shows gaussSetBelow(a, p, t, succ(m)) = insert(succ(m), gaussSetBelow(a, p, t, m)) proof ext k constructor . intro member have parts := gaussSetBelow.membership.forward with member have kNat := natural.member at succ(succ(m)) with parts.left have all := (gaussSetBelow.mem_iff at a, p, t, succ(m), k).forward with member obtain ⟨positive, atMost, largeK⟩ := all have split := (LessEqual.iff_Less_or_eq at k, succ(m)).forward with atMost apply insert.membership.backward cases split with below | same . right have bound : k ≤ m by arith exact (gaussSetBelow.mem_iff at a, p, t, m, k).backward with ⟨positive, bound, largeK⟩ . left exact same . intro member have split := insert.membership.forward with member cases split with same | inSet . rw same have positive : 0 < succ(m) by arith have bound : succ(m) ≤ succ(m) by arith exact (gaussSetBelow.mem_iff at a, p, t, succ(m), succ(m)).backward with ⟨positive, bound, large⟩ . have parts := gaussSetBelow.membership.forward with inSet have kNat := natural.member at succ(m) with parts.left have all := (gaussSetBelow.mem_iff at a, p, t, m, k).forward with inSet obtain ⟨positive, atMost, largeK⟩ := all have bound : k ≤ succ(m) by arith exact (gaussSetBelow.mem_iff at a, p, t, succ(m), k).backward with ⟨positive, bound, largeK⟩ theorem gaussSetBelow.succ_small (a p t m : Nat) requires small : (a * succ(m)) % p ≤ t shows gaussSetBelow(a, p, t, succ(m)) = gaussSetBelow(a, p, t, m) proof ext k constructor . intro member have parts := gaussSetBelow.membership.forward with member have kNat := natural.member at succ(succ(m)) with parts.left have all := (gaussSetBelow.mem_iff at a, p, t, succ(m), k).forward with member obtain ⟨positive, atMost, largeK⟩ := all have split := (LessEqual.iff_Less_or_eq at k, succ(m)).forward with atMost cases split with below | same . have bound : k ≤ m by arith exact (gaussSetBelow.mem_iff at a, p, t, m, k).backward with ⟨positive, bound, largeK⟩ . exfalso rw same at largeK have impossible : ¬t < (a * succ(m)) % p by arith exact impossible with largeK . intro member have parts := gaussSetBelow.membership.forward with member have kNat := natural.member at succ(m) with parts.left have all := (gaussSetBelow.mem_iff at a, p, t, m, k).forward with member obtain ⟨positive, atMost, largeK⟩ := all have bound : k ≤ succ(m) by arith exact (gaussSetBelow.mem_iff at a, p, t, succ(m), k).backward with ⟨positive, bound, largeK⟩ theorem largeCount.gaussSetBelow (a p t m : Nat) shows largeCount(t, scaledList(a, p, factorialList(m))) = cardinality(gaussSetBelow(a, p, t, m)) proof induction m | zero => rw [factorialList.zero, scaledList.nil, largeCount.nil, gaussSetBelow.zero, cardinality.empty] rfl | succ(j) using ih => have restList := factorialList.list at j have scaledRest := scaledList.list at a, p, factorialList(j) with restList have finite := gaussSetBelow.finite at a, p, t, j have fresh := gaussSetBelow.fresh at a, p, t, j rw [factorialList.succ, scaledList.cons] have split := Less.or_LessEqual at t, (a * succ(j)) % p cases split with large | small . rw [largeCount.cons_large, gaussSetBelow.succ_large] have count := cardinality.insert_add_one with finite, fresh have one := add.right_one at cardinality(gaussSetBelow(a, p, t, j)) rw [count, ih, one] rfl . rw [largeCount.cons_small, gaussSetBelow.succ_small] exact ih theorem largeCount.gaussSet (a p h : Nat) shows largeCount(h, gaussList(a, p, h)) = cardinality(gaussSet(a, p, h)) proof have same : gaussSet(a, p, h) = gaussSetBelow(a, p, h, h) ext k constructor . intro member have parts := gaussSet.membership.forward with member exact gaussSetBelow.membership.backward with parts . intro member have parts := gaussSetBelow.membership.forward with member exact gaussSet.membership.backward with parts rw [gaussList.value, same] exact largeCount.gaussSetBelow at a, p, h, h