Skip to content

refactor: make the CPolynomial namespace open-friendly - #279

Open
mitschabaude-bot wants to merge 3 commits into
Verified-zkEVM:mainfrom
mitschabaude-bot:protect-ring-axiom-lemmas
Open

refactor: make the CPolynomial namespace open-friendly#279
mitschabaude-bot wants to merge 3 commits into
Verified-zkEVM:mainfrom
mitschabaude-bot:protect-ring-axiom-lemmas

Conversation

@mitschabaude-bot

@mitschabaude-bot mitschabaude-bot commented Jul 30, 2026

Copy link
Copy Markdown

Marks the ring- and module-axiom lemmas in CPolynomial as protected, so that open CompPoly.CPolynomial no longer shadows the root-level lemmas of the same name.

Why

These lemmas exist to discharge the Semiring/CommRing/Module instance fields. Once the instances are built, downstream code reaches them through the algebraic hierarchy — nobody should be citing CPolynomial.mul_comm by name. But because they are unprotected, open CompPoly.CPolynomial rebinds mul_comm, add_zero, one_mul, smul_add and 15 more to the CPolynomial-specific versions, and every unrelated ring-style step in the opening file has to be written _root_.mul_comm.

Mathlib's Polynomial is open-friendly for exactly this reason: it never restates its ring axioms, so open Polynomial costs nothing.

For scale, a downstream development that moved its polynomial layer from Polynomial to CPolynomial accumulated 75 _root_. qualifications purely from this, dominated by mul_comm (32), add_zero (19), mul_assoc (16), one_mul (13) and mul_one (12). All of them disappear with this change.

What changed

protected on 19 declarations in Univariate/Basic.lean:

add_assoc  add_comm  add_mul  add_smul  add_zero  div_zero  instLawfulBEq
mul_add  mul_assoc  mul_comm  mul_one  neg_add_cancel  nsmul_zero  one_mul
one_smul  smul_add  smul_zero  zero_add  zero_smul

These are precisely the members of CompPoly.CPolynomial that collide with an existing root-level name; I enumerated them from the environment rather than by eye.

protected requires qualification inside the declaring namespace too, so six internal call sites are now written CPolynomial.add_comm and the like. All six are in Basic.lean and all but two are instance fields consuming the lemma they are named after — AddCommSemigroup.add_assoc/add_comm, CommSemiring.mul_comm, and the five Module fields. The other two are rw [add_comm] steps inside X_mul_divX_add and induction_on.

Those two are worth a note: they did not fail with an unknown identifier but with an ambiguity between _root_.add_comm and Raw.add_comm. CPolynomial.Raw is a sub-namespace, so its own unprotected copy of the axiom was previously masked by the outer one; removing the outer one exposes it.

Nothing about instance resolution changes, and every lemma remains accessible as CPolynomial.foo.

Verified with a full lake build of the package: 2605 jobs, no errors. Basic.lean is the only file that needed touching.

Not included

CPolynomial.Raw and QuotientCPolynomial carry the same axiom lemmas and have the same issue — the ambiguity above is a symptom. They are sub-namespaces that are rarely opened, so I left them alone; happy to extend this if you'd like.

These lemmas exist to discharge the `Semiring`/`CommRing`/`Module` instance
fields; downstream code reaches them through the algebraic hierarchy. Leaving
them unprotected means `open CompPoly.CPolynomial` rebinds `mul_comm`,
`add_zero`, `one_mul` and 16 more, so every unrelated ring step in the opening
file has to be written `_root_.mul_comm`.

Mathlib's `Polynomial` is open-friendly because it never restates its ring
axioms. This makes `CPolynomial` behave the same way. The lemmas stay available
as `CPolynomial.mul_comm` and instance resolution is unaffected.
@github-actions

Copy link
Copy Markdown

🤖 PR Summary

⚠️ PR title does not follow conventional commit format type[(scope)]: subject. Got: Make the CPolynomial namespace open-friendly

Refactoring

  • Marked 19 declarations in CompPoly/Univariate/Basic.lean as protected to prevent shadowing of root-level lemmas when open CompPoly.CPolynomial is used. The affected declarations are: add_assoc, add_comm, add_mul, add_smul, add_zero, div_zero, instLawfulBEq, mul_add, mul_assoc, mul_comm, mul_one, neg_add_cancel, nsmul_zero, one_mul, one_smul, smul_add, smul_zero, zero_add, zero_smul.
  • All internal call sites within the same file that previously referred to these lemmas unqualified have been updated to use the qualified CPolynomial. prefix (e.g., CPolynomial.add_comm in AddCommSemigroup instance, CPolynomial.mul_comm in X_mul_divX_add, etc.).
  • The change is concentrated in a single file; no other files are modified.
  • The CPolynomial.Raw and QuotientCPolynomial sub-namespaces are intentionally left unchanged, as they are rarely opened.
  • No sorry or admit placeholders are introduced. Instance resolution and external accessibility (CPolynomial.foo) remain unaffected.

Statistics

Metric Count
📝 Files Changed 1
Lines Added 29
Lines Removed 29

Lean Declarations

✏️ Affected: 18 declaration(s) (line number changed)
  • protected lemma add_mul [Semiring R] [BEq R] [LawfulBEq R] in CompPoly/Univariate/Basic.lean moved from L945 to L945
  • protected lemma add_smul [Semiring R] [BEq R] [LawfulBEq R] in CompPoly/Univariate/Basic.lean moved from L1324 to L1324
  • protected lemma mul_add [Semiring R] [BEq R] [LawfulBEq R] in CompPoly/Univariate/Basic.lean moved from L940 to L940
  • protected lemma mul_assoc [Semiring R] [BEq R] [LawfulBEq R] in CompPoly/Univariate/Basic.lean moved from L927 to L927
  • protected lemma mul_comm [CommSemiring R] [BEq R] [LawfulBEq R] (p q : CPolynomial R) : p * q = q * p in CompPoly/Univariate/Basic.lean moved from L1084 to L1084
  • protected lemma mul_one [Semiring R] [BEq R] [LawfulBEq R] [Nontrivial R] in CompPoly/Univariate/Basic.lean moved from L920 to L920
  • protected lemma one_mul [Semiring R] [BEq R] [LawfulBEq R] [Nontrivial R] in CompPoly/Univariate/Basic.lean moved from L913 to L913
  • protected lemma one_smul [Semiring R] [BEq R] [LawfulBEq R] (p : CPolynomial R) : (1 : R) • p = p in CompPoly/Univariate/Basic.lean moved from L1338 to L1338
  • protected lemma smul_add [Semiring R] [BEq R] [LawfulBEq R] in CompPoly/Univariate/Basic.lean moved from L1312 to L1312
  • protected lemma smul_zero [Semiring R] [BEq R] [LawfulBEq R] (r : R) : r • (0 : CPolynomial R) = 0 in CompPoly/Univariate/Basic.lean moved from L1300 to L1300
  • protected lemma zero_smul [Semiring R] [BEq R] [LawfulBEq R] (p : CPolynomial R) : (0 : R) • p = 0 in CompPoly/Univariate/Basic.lean moved from L1331 to L1331
  • protected theorem add_assoc [Semiring R] [BEq R] [LawfulBEq R] in CompPoly/Univariate/Basic.lean moved from L111 to L111
  • protected theorem add_comm [Semiring R] [BEq R] [LawfulBEq R] (p q : CPolynomial R) : p + q = q + p in CompPoly/Univariate/Basic.lean moved from L106 to L106
  • protected theorem add_zero [Semiring R] [BEq R] [LawfulBEq R] (p : CPolynomial R) : p + 0 = p in CompPoly/Univariate/Basic.lean moved from L122 to L122
  • protected theorem div_zero [Field R] [BEq R] [LawfulBEq R] (p : CPolynomial R) : p.div 0 = 0 in CompPoly/Univariate/Basic.lean moved from L872 to L872
  • protected theorem neg_add_cancel [Ring R] [BEq R] [LawfulBEq R] (p : CPolynomial R) : -p + p = 0 in CompPoly/Univariate/Basic.lean moved from L1233 to L1233
  • protected theorem nsmul_zero [Semiring R] [BEq R] [LawfulBEq R] (p : CPolynomial R) : nsmul 0 p = 0 in CompPoly/Univariate/Basic.lean moved from L133 to L133
  • protected theorem zero_add [Semiring R] [BEq R] [LawfulBEq R] (p : CPolynomial R) : 0 + p = p in CompPoly/Univariate/Basic.lean moved from L117 to L117

sorry Tracking

  • No sorrys were added, removed, or affected.

📋 **Additional Analysis**

Differences from the contribution guidelines. See table below.


📄 **Per-File Summaries**
  • CompPoly/Univariate/Basic.lean: Changed several theorem and instance declarations in CompPoly/Univariate/Basic.lean from public to protected: add_comm, add_assoc, zero_add, add_zero, nsmul_zero, div_zero, one_mul, mul_one, mul_assoc, mul_add, add_mul, mul_comm, neg_add_cancel, smul_zero, smul_add, add_smul, zero_smul, one_smul, and the LawfulBEq instance. Updated the references to these lemmas in the AddCommSemigroup, CommSemiring, and Module instances to use their fully qualified CPolynomial. names (e.g., CPolynomial.add_comm in AddCommSemigroup and X_mul_divX_add and induction_on proofs; CPolynomial.add_assoc, CPolynomial.mul_comm, CPolynomial.one_smul, CPolynomial.smul_zero, CPolynomial.smul_add, CPolynomial.add_smul, CPolynomial.zero_smul in the respective instances).

Last updated: 2026-07-30 13:48 UTC.

@mitschabaude-bot mitschabaude-bot changed the title Make the CPolynomial namespace open-friendly refactor: make the CPolynomial namespace open-friendly Jul 30, 2026
@mitschabaude

mitschabaude commented Jul 30, 2026

Copy link
Copy Markdown
Contributor

for context, this was motivated by zcash/ironwood#146, which made CPolynomial the default polynomial type used in all of ironwood. previously the project used open Polynomial consistently, and open CPolynomial doesn't work quite as well because stuff like mul_zero is shadowed.

@dhsorens can you review or suggest a reviewer?

@dhsorens
dhsorens self-requested a review July 30, 2026 15:17
@dhsorens

Copy link
Copy Markdown
Collaborator

thanks @mitschabaude - I will review this asap!

@dhsorens dhsorens left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

@mitschabaude you're right, this is a good idea. merging

@dhsorens

Copy link
Copy Markdown
Collaborator

@mitschabaude would you mind fixing the linting errors? I'll merge this when it passes (I would do it myself but I can't push to your fork)

Adding `protected ` took seven headers past the style limit; each is wrapped
after its instance binders, matching the file's own style.
@github-actions

github-actions Bot commented Aug 4, 2026

Copy link
Copy Markdown

Caution

This summary did not complete normally. One or more AI calls failed for a spend, quota, or authentication reason, or the per-run budget was exhausted. The summary below is PARTIAL — see the Actions log for details.

🤖 PR Summary

Failed to generate AI summary. Please check the per-file summaries and statistics below.


Statistics

Metric Count
📝 Files Changed 1
Lines Added 36
Lines Removed 29

Lean Declarations

✏️ Affected: 18 declaration(s) (line number changed)
  • protected lemma add_mul [Semiring R] [BEq R] [LawfulBEq R] in CompPoly/Univariate/Basic.lean moved from L949 to L951
  • protected lemma add_smul [Semiring R] [BEq R] [LawfulBEq R] in CompPoly/Univariate/Basic.lean moved from L1328 to L1333
  • protected lemma mul_add [Semiring R] [BEq R] [LawfulBEq R] in CompPoly/Univariate/Basic.lean moved from L944 to L946
  • protected lemma mul_assoc [Semiring R] [BEq R] [LawfulBEq R] in CompPoly/Univariate/Basic.lean moved from L931 to L933
  • protected lemma mul_comm [CommSemiring R] [BEq R] [LawfulBEq R] in CompPoly/Univariate/Basic.lean moved from L1088 to L1090
  • protected lemma mul_one [Semiring R] [BEq R] [LawfulBEq R] [Nontrivial R] in CompPoly/Univariate/Basic.lean moved from L924 to L926
  • protected lemma one_mul [Semiring R] [BEq R] [LawfulBEq R] [Nontrivial R] in CompPoly/Univariate/Basic.lean moved from L917 to L919
  • protected lemma one_smul [Semiring R] [BEq R] [LawfulBEq R] in CompPoly/Univariate/Basic.lean moved from L1342 to L1348
  • protected lemma smul_add [Semiring R] [BEq R] [LawfulBEq R] in CompPoly/Univariate/Basic.lean moved from L1316 to L1321
  • protected lemma smul_zero [Semiring R] [BEq R] [LawfulBEq R] in CompPoly/Univariate/Basic.lean moved from L1304 to L1308
  • protected lemma zero_smul [Semiring R] [BEq R] [LawfulBEq R] in CompPoly/Univariate/Basic.lean moved from L1335 to L1340
  • protected theorem add_assoc [Semiring R] [BEq R] [LawfulBEq R] in CompPoly/Univariate/Basic.lean moved from L115 to L116
  • protected theorem add_comm [Semiring R] [BEq R] [LawfulBEq R] in CompPoly/Univariate/Basic.lean moved from L110 to L110
  • protected theorem add_zero [Semiring R] [BEq R] [LawfulBEq R] (p : CPolynomial R) : p + 0 = p in CompPoly/Univariate/Basic.lean moved from L126 to L127
  • protected theorem div_zero [Field R] [BEq R] [LawfulBEq R] (p : CPolynomial R) : p.div 0 = 0 in CompPoly/Univariate/Basic.lean moved from L876 to L878
  • protected theorem neg_add_cancel [Ring R] [BEq R] [LawfulBEq R] in CompPoly/Univariate/Basic.lean moved from L1237 to L1240
  • protected theorem nsmul_zero [Semiring R] [BEq R] [LawfulBEq R] in CompPoly/Univariate/Basic.lean moved from L137 to L138
  • protected theorem zero_add [Semiring R] [BEq R] [LawfulBEq R] (p : CPolynomial R) : 0 + p = p in CompPoly/Univariate/Basic.lean moved from L121 to L122

sorry Tracking

  • No sorrys were added, removed, or affected.

📄 **Per-File Summaries**
  • CompPoly/Univariate/Basic.lean: Summary unavailable — AI generation failed.

Last updated: 2026-08-04 14:35 UTC.

@mitschabaude

Copy link
Copy Markdown
Contributor

@dhsorens fixed up the warnings!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants