Skip to content

feat(NumberTheory/ArithmeticFunction): wrap Nat.totient as an ArithmeticFunction - #28676

Closed
sun123zxy wants to merge 11 commits into
leanprover-community:masterfrom
sun123zxy:arithfunc-totient
Closed

sun123zxy wants to merge 11 commits into
leanprover-community:masterfrom
sun123zxy:arithfunc-totient

Conversation

@sun123zxy

Copy link
Copy Markdown
Contributor

This wraps the Euler's totient function Nat.totient into a new ArithmeticFunction ϕ, with some basic identities such as ϕ * ζ = id and μ * id = ϕ.


We use the notation ϕ to distinguish from Nat.totient's notation φ, however this might be controversial. Suggestions are welcome!

Open in Gitpod

@github-actions github-actions Bot added new-contributor This PR was made by a contributor with at most 5 merged PRs. Welcome to the community! t-number-theory Number theory (also use t-algebra or t-analysis to specialize) large-import Automatically added label for PRs with a significant increase in transitive imports labels Aug 20, 2025
@github-actions

github-actions Bot commented Aug 20, 2025 •

Copy link
Copy Markdown

PR summary c9e5385874

Import changes exceeding 2%

% File
+15.69% Mathlib.NumberTheory.ArithmeticFunction

Import changes for modified files

Dependency changes

File Base Count Head Count Change
Mathlib.NumberTheory.ArithmeticFunction 969 1121 +152 (+15.69%)
Import changes for all files
Files Import difference
Mathlib.NumberTheory.VonMangoldt 20
Mathlib.NumberTheory.EulerProduct.Basic Mathlib.NumberTheory.TsumDivsorsAntidiagonal 31
Mathlib.Algebra.Order.Antidiag.Nat 146
Mathlib.NumberTheory.SelbergSieve 151
Mathlib.NumberTheory.ArithmeticFunction 152

Declarations diff

+ coe_id_mul_moebius_eq_coe_totient
+ isMultiplicative_totient
+ moebius_mul_coe_id_eq_coe_totient
+ totient
+ totient_apply
+ totient_mul_zeta
+ zeta_mul_totient

You can run this locally as follows
## summary with just the declaration names:
./scripts/declarations_diff.sh <optional_commit>

## more verbose report:
./scripts/declarations_diff.sh long <optional_commit>

The doc-module for script/declarations_diff.sh contains some details about this script.


No changes to technical debt.

You can run this locally as

./scripts/technical-debt-metrics.sh pr_summary
  • The relative value is the weighted sum of the differences with weight given by the inverse of the current value of the statistic.
  • The absolute value is the relative value divided by the total sum of the inverses of the current values (i.e. the weighted average of the differences).

@sun123zxy
sun123zxy marked this pull request as ready for review August 20, 2025 04:34

@b-mehta b-mehta left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In my opinion, we shouldn't make two versions of this function, instead we should re-define Nat.totient to be an ArithmeticFunction. But I'd like to hear what others think about this too, since that could be an intrusive change. @eric-wieser, perhaps?

Comment on lines +1300 to +1301
def totient : ArithmeticFunction ℕ :=
⟨fun n => n.totient, by simp⟩

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

May as well skip simp here:

Suggested change
def totient : ArithmeticFunction ℕ :=
⟨fun n => n.totient, by simp⟩
@[simps]
def totient : ArithmeticFunction ℕ where
toFun := Nat.totient
map_zero' := Nat.totient_zero

@sun123zxy sun123zxy Aug 20, 2025 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure! I'll change the definition as above, except tagging it with [@simp]. See comments below.

Comment on lines +1309 to +1312
/-- Fall back to `Nat.totient`.
Added as a `simp` lemma so that `simp` can make use of the existing lemmas for `Nat.totient`. -/
@[simp]
theorem totient_apply {n : ℕ} : ϕ n = n.totient := rfl

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This can be deleted with my @[simps] above

@sun123zxy sun123zxy Aug 20, 2025 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Apologize that I'm not so familiar with @[simps], but seemingly this approach changes the behavior of simp? Try this minimal example:

import Mathlib.NumberTheory.ArithmeticFunction
import Mathlib.Data.Nat.Totient

namespace ArithmeticFunction

open Nat

@[simps]
def totient : ArithmeticFunction ℕ where
  toFun := Nat.totient
  map_zero' := Nat.totient_zero

#check totient_apply

theorem totient_one : totient 1 = 1 := by
  simp -- `simp` made no progress

end ArithmeticFunction

On my side only simp [totient_apply 1] works. This is not desired, right? Previously a single simp closes the goal.

Not sure why this happens, but having an explicit [@simp] xxx_apply is a common practice in ArithmeticFunction.lean. I would suggest following the original style for the time being? (maybe replacing these [@simp] xxx_apply with @[simps] in another pull request)

@eric-wieser

eric-wieser commented Aug 20, 2025 •

Copy link
Copy Markdown
Member

But I'd like to hear what others think about this too, since that could be an intrusive change. @eric-wieser, perhaps?

My take is usually that the definition should be unbundled, and then the bundled versions should unfold to it; meaning the approach in this PR seems reasonable.

@b-mehta

b-mehta commented Aug 20, 2025

Copy link
Copy Markdown
Contributor

My take is usually that the definition should be unbundled, and then the bundled versions should unfold to it; meaning the approach in this PR seems reasonable.

I think I'm not seeing the advantage of having the unbundled version around at all here, since they both take the same assumptions (none)

@MichaelStollBayreuth

Copy link
Copy Markdown
Contributor

@eric-wieser @b-mehta Can you get to an agreement here?

@MichaelStollBayreuth

Copy link
Copy Markdown
Contributor

I think it definitely makes sense to have the totient as an ArithmeticFunction, so I would like to move this along.
Somebody can later refactor to remove Nat.totient if so desired.

@SnirBroshi

Copy link
Copy Markdown
Collaborator

Fun fact: the identities ϕ * ζ = id and μ * id = ϕ just appeared on Numberphile - https://youtu.be/XhA5U9pFXuU

@MichaelStollBayreuth

Copy link
Copy Markdown
Contributor

maintainer merge

@github-actions

Copy link
Copy Markdown

🚀 Pull request has been placed on the maintainer queue by MichaelStollBayreuth.

@ghost ghost added the maintainer-merge A reviewer has approved the changed; awaiting maintainer approval. label Oct 11, 2025
@riccardobrasca

Copy link
Copy Markdown
Member

How bad would be, in terms of imports, removing Nat.totient entirely?

@bryangingechen bryangingechen added the awaiting-author Reply -awaiting-author to remove the label on your PR once you have addressed all comments. label Oct 12, 2025
@sun123zxy

Copy link
Copy Markdown
Contributor Author

How bad would be, in terms of imports, removing Nat.totient entirely?

I might not have sufficient knowledge to give a firm answer. Nevertheless, currently Mathlib.Data.Nat.Totient is directly imported by

But around 30+ files are calling Nat.totient without an explicit import. Most of them are in number theory and ring theory, like cyclotomic polynomials, L-series and finite groups.

@riccardobrasca

riccardobrasca commented Oct 13, 2025 •

Copy link
Copy Markdown
Member

How bad would be, in terms of imports, removing Nat.totient entirely?

I might not have sufficient knowledge to give a firm answer. Nevertheless, currently Mathlib.Data.Nat.Totient is directly imported by

* [Mathlib.NumberTheory.PrimeCounting](https://leanprover-community.github.io/mathlib4_docs/Mathlib/NumberTheory/PrimeCounting.html)

* [Mathlib.Topology.Instances.AddCircle.Defs](https://leanprover-community.github.io/mathlib4_docs/Mathlib/Topology/Instances/AddCircle/Defs.html)

* [Mathlib.GroupTheory.SpecificGroups.Cyclic](https://leanprover-community.github.io/mathlib4_docs/Mathlib/GroupTheory/SpecificGroups/Cyclic.html)

But around 30+ files are calling Nat.totient without an explicit import. Most of them are in number theory and ring theory, like cyclotomic polynomials, L-series and finite groups.

Those are not a real problems, but currently ArithmeticFunction import a bunch of stuff. I propose first of all to split ArithmeticFunction in Defs and Basic (it's a long file so this is surely a good idea) so that the definition of Totient as an ArithmeticFunction would be very light in terms of imports and we can have just that one.

@mathlib4-merge-conflict-bot mathlib4-merge-conflict-bot added the merge-conflict The PR has a merge conflict with master, and needs manual merging. (this label is managed by a bot) label Oct 17, 2025
@mathlib4-merge-conflict-bot

Copy link
Copy Markdown

This pull request has conflicts, please merge master and resolve them.

@sgouezel sgouezel removed the maintainer-merge A reviewer has approved the changed; awaiting maintainer approval. label Oct 28, 2025
@xroblot

xroblot commented Nov 17, 2026

Copy link
Copy Markdown
Collaborator

Hi @sun123zxy, I've opened #46097 on the same topic without realising this PR existed. Are you still working on this PR? If so, I'm happy to close mine, but otherwise, would you mind if continued with #46097?

@sun123zxy

sun123zxy commented Nov 17, 2026 •

Copy link
Copy Markdown
Contributor Author

Hi @xroblot, Glad to hear that someone is working on this again! This PR is really outdated now, will close it right away.

@sun123zxy sun123zxy closed this Nov 17, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

awaiting-author Reply -awaiting-author to remove the label on your PR once you have addressed all comments. large-import Automatically added label for PRs with a significant increase in transitive imports merge-conflict The PR has a merge conflict with master, and needs manual merging. (this label is managed by a bot) new-contributor This PR was made by a contributor with at most 5 merged PRs. Welcome to the community! t-number-theory Number theory (also use t-algebra or t-analysis to specialize)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

10 participants