Skip to content

feat(Tactic/ComputablePolynomial): computable univariate polynomials and poly_decide - #41282

Closed
mkaratarakis wants to merge 8 commits into
leanprover-community:masterfrom
mkaratarakis:univariate-poly-decide
Closed

mkaratarakis wants to merge 8 commits into
leanprover-community:masterfrom
mkaratarakis:univariate-poly-decide

Conversation

@mkaratarakis

@mkaratarakis mkaratarakis commented Jul 2, 2026 •

Copy link
Copy Markdown
Contributor

@erdOne @Multramate @b-mehta
This PR adds a computable, kernel-reducible representation of univariate polynomials
(SparsePoly R, over any CommRing R with DecidableEq R) together with reflection tactics
that prove statements about Mathlib's noncomputable Polynomial R:

  • poly_decide / poly_compute — decide equalities and inequalities of Polynomial R by
    reflecting onto normal-form coefficient lists and closing with kernel decide +kernel;
  • poly_eval — evaluate a polynomial in the kernel;
  • poly_dvd — kernel divisibility;
  • poly_dvd_cert — divisibility of Polynomial K by a symbolic certificate (the quotient is
    searched uncertified in MetaM, then verified with ring): works over any field, for any monic
    divisor over a CommRing, and for unit leading coefficients.

SparsePoly R is proved to be a CommRing and is given an AlgEquiv to Polynomial R, so the
tactics are sound by transport along toPoly.

Everything is axiom-free: the proofs depend only on [propext, Classical.choice, Quot.sound]
— in particular there is no native_decide / Lean.ofReduceBool. The #print axioms trust
checks live in MathlibTest/ComputablePolynomial.lean.

The representation was started by Mario Carneiro at the Hausdorff Institute (June 2024) with design
notes and an original Lean prototype by James Davenport
(https://github.com/JamesHDavenport/Dagstuhl23401, verify-irred/VerifyIrred).
The definitions and statements were written by Mario and most of the proofs and tactic were written with help from Claude.

Opened as a draft to invite discussion first. Happy to start a Zulip thread and/or split this into
smaller PRs if that is preferred

mkaratarakis and others added 2 commits July 2, 2026 00:25
…+ poly_decide

Add `SparsePoly R`, a computable, canonical (sorted, nonzero-coefficient) representation of
univariate polynomials over a commutative ring `R` with `DecidableEq R`, and axiom-free tactics
built on it by reflecting `Polynomial R` onto `SparsePoly R`:
* `poly_decide`/`poly_compute` — decide equalities/inequalities of `Polynomial R` via kernel
  `decide +kernel` (no `native_decide`);
* `poly_eval`, `poly_dvd` — evaluation and kernel-division divisibility;
* `poly_dvd_cert` — divisibility over any field (monic/unit cases over any comm ring) by MetaM
  pseudo-division certified with `ring`.

Started by Mario Carneiro at the Hausdorff Institute (June 2024), with design notes by
James Davenport.

Co-Authored-By: Claude Opus 4.8 <[email protected]>
Rename to UpperCamelCase and relocate the computable univariate polynomial
library and reflection tactics from `Mathlib/NumberTheory/Transcendental/` to
`Mathlib/Tactic/ComputablePolynomial/`:

* `poly.lean`            -> `SparsePoly.lean`
* `polyReflectKernel.lean` -> `Reflect.lean`
* `polyDvdCert.lean`     -> `DvdCert.lean`
* `polyTactics.lean`     -> `Tactics.lean`

Replace the blanket `import Mathlib.Tactic` with specific tactic imports (no
longer possible under `Mathlib/Tactic/` and preferred anyway), move all `#eval`
/ `#print axioms` demonstrations into `MathlibTest/ComputablePolynomial.lean`,
and cite James Davenport's original prototype in the provenance note.

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
@github-actions github-actions Bot added the t-meta Tactics, attributes or user commands label Jul 2, 2026
@github-actions

github-actions Bot commented Jul 2, 2026 •

Copy link
Copy Markdown

PR summary 7fe43d41b3

Import changes for modified files

No significant changes to the import graph

Import changes for all files
Files Import difference
Mathlib.Tactic 4
Mathlib.Tactic.ComputablePolynomial.DvdCert (new file) 1148
Mathlib.Tactic.ComputablePolynomial.SparsePoly (new file) 1150
Mathlib.Tactic.ComputablePolynomial.Reflect (new file) 1159
Mathlib.Tactic.ComputablePolynomial.Tactics (new file) 1160

Declarations diff (regex)

+ C
+ CHom
+ IsExactDiv
+ SparsePoly
+ SparsePoly.Reflect.reflectKWithProof
+ TL
+ X
+ addCore
+ addCore_degLt
+ addCore_sorted
+ big_dvd
+ bridgeSimpK
+ coeff
+ coeffCore
+ coeffCore_eq_toPolyCore_coeff
+ coeff_smul
+ coeff_toPoly
+ coeff_toPolyCore_eq_zero_of_forall_lt
+ coeff_toPolyCore_head
+ coeff_toPolyCore_listCoeff
+ coeff_toPolyCore_of_degLt
+ content
+ content_dvd_coeff_final
+ dedupList
+ dedupList_degLt
+ dedupList_sorted
+ degLt
+ degLt_of_sorted_cons
+ deg_pseudo_rem_a
+ deg_pseudo_rem_b
+ degree
+ degree_eq_head
+ degree_eq_natDegree_toPoly
+ degree_eq_poly_degree
+ degree_mul
+ degree_sub_leading_term_lt
+ divMod
+ divModK
+ divRem
+ divRem_eq
+ divRem_spec
+ dvd_demo
+ dvd_of_divMod
+ eq_of_core
+ eval
+ evalCore
+ evalCore_eq
+ eval_eq
+ eval_of_core
+ foldl_gcd_dvd_acc
+ foldl_gcd_dvd_mem
+ gcd
+ gcdPrim
+ insertK
+ instance : Add (SparsePoly R)
+ instance : Algebra R (SparsePoly R)
+ instance : CommRing (SparsePoly R)
+ instance : DecidableEq (SparsePoly R) := fun a b =>
+ instance : IsExactDiv ℤ
+ instance : Mul (SparsePoly R)
+ instance : Neg (SparsePoly R)
+ instance : One (SparsePoly R)
+ instance : Zero (SparsePoly R)
+ instance [CommRing R] [Lean.ToFormat R] : Lean.ToFormat (SparsePoly R)
+ instance [Div R] : Div (SparsePoly R)
+ instance {R} [CommGroupWithZero R] : IsExactDiv R
+ isNatLit
+ kConv
+ kZip
+ listCoeff
+ natDegree_toPolyCore_lt
+ natLit?
+ ne_of_core
+ ofList
+ ofPoly_X
+ ofSortedList
+ ofSortedList_of_nonzero
+ polyNeBool
+ primitivePart
+ pseudoQuotient
+ root_demo
+ sq_expand
+ toPoly
+ toPolyCore
+ toPolyCore_C_coeffs
+ toPolyCore_X_coeffs
+ toPolyCore_addCore
+ toPolyCore_append
+ toPolyCore_cons
+ toPolyCore_cons_split
+ toPolyCore_dedupList
+ toPolyCore_divMod
+ toPolyCore_divModK
+ toPolyCore_filter_nonzero
+ toPolyCore_injective_of_sorted
+ toPolyCore_insertK
+ toPolyCore_kAdd
+ toPolyCore_kMul
+ toPolyCore_kNeg
+ toPolyCore_kPow
+ toPolyCore_kSub
+ toPolyCore_map_smul
+ toPolyCore_mergeSort
+ toPolyCore_mul_x
+ toPolyCore_mul_y
+ toPolyCore_one_coeffs
+ toPolyCore_perm
+ toPolyCore_zero_coeffs
+ toPolyEquiv
+ toPoly_C
+ toPoly_X
+ toPoly_X_fixed
+ toPoly_add
+ toPoly_injective
+ toPoly_mul
+ toPoly_neg
+ toPoly_ofList
+ toPoly_ofPoly
+ toPoly_one
+ toPoly_pow
+ toPoly_pseudo_rem_a
+ toPoly_pseudo_rem_b
+ toPoly_smul
+ toPoly_smul_X_pow
+ toPoly_smul_X_pow_mul
+ toPoly_zero
++ kAdd
++ kMul
++ kNeg
++ kPow
++ kSub

You can run this locally as follows
## from your `mathlib4` directory:
git clone https://github.com/leanprover-community/mathlib-ci.git ../mathlib-ci

## summary with just the declaration names:
../mathlib-ci/scripts/pr_summary/declarations_diff.sh <optional_commit>

## more verbose report:
../mathlib-ci/scripts/pr_summary/declarations_diff.sh long <optional_commit>

The doc-module for scripts/pr_summary/declarations_diff.sh in the mathlib-ci repository contains some details about this script.

Declarations diff (Lean)

✅ Lean-aware diff — post-build, computed from the Lean environment (commit 7fe43d4).

  • +163 new declarations
  • −0 removed declarations
+Polynomial.DvdCert.tacticPoly_dvd_cert
+SparsePoly
+SparsePoly.C
+SparsePoly.CHom
+SparsePoly.IsExactDiv
+SparsePoly.IsExactDiv.casesOn
+SparsePoly.IsExactDiv.mk
+SparsePoly.IsExactDiv.mul_div_cancel
+SparsePoly.IsExactDiv.rec
+SparsePoly.IsExactDiv.recOn
+SparsePoly.Kernel.TL
+SparsePoly.Kernel.bridgeSimpK
+SparsePoly.Kernel.coeff_toPolyCore_listCoeff
+SparsePoly.Kernel.divMod
+SparsePoly.Kernel.divModK
+SparsePoly.Kernel.divModK.eq_def
+SparsePoly.Kernel.dvd_of_divMod
+SparsePoly.Kernel.eq_of_core
+SparsePoly.Kernel.insertK
+SparsePoly.Kernel.insertK.eq_def
+SparsePoly.Kernel.kAdd
+SparsePoly.Kernel.kMul
+SparsePoly.Kernel.kNeg
+SparsePoly.Kernel.kPow
+SparsePoly.Kernel.kPow.eq_def
+SparsePoly.Kernel.kSub
+SparsePoly.Kernel.listCoeff
+SparsePoly.Kernel.ne_of_core
+SparsePoly.Kernel.polyNeBool
+SparsePoly.Kernel.reifyK
+SparsePoly.Kernel.toPolyCore_C_coeffs
+SparsePoly.Kernel.toPolyCore_X_coeffs
+SparsePoly.Kernel.toPolyCore_cons
+SparsePoly.Kernel.toPolyCore_cons_split
+SparsePoly.Kernel.toPolyCore_divMod
+SparsePoly.Kernel.toPolyCore_divModK
+SparsePoly.Kernel.toPolyCore_insertK
+SparsePoly.Kernel.toPolyCore_kAdd
+SparsePoly.Kernel.toPolyCore_kMul
+SparsePoly.Kernel.toPolyCore_kNeg
+SparsePoly.Kernel.toPolyCore_kPow
+SparsePoly.Kernel.toPolyCore_kSub
+SparsePoly.Kernel.toPolyCore_one_coeffs
+SparsePoly.Kernel.toPolyCore_zero_coeffs
+SparsePoly.Reflect.reflectKWithProof
+SparsePoly.X
+SparsePoly.addCore
+SparsePoly.addCore.eq_def
+SparsePoly.addCore_degLt
+SparsePoly.addCore_sorted
+SparsePoly.casesOn
+SparsePoly.coeff
+SparsePoly.coeffCore
+SparsePoly.coeffCore.eq_def
+SparsePoly.coeffCore_eq_toPolyCore_coeff
+SparsePoly.coeff_smul
+SparsePoly.coeff_toPoly
+SparsePoly.coeff_toPolyCore_eq_zero_of_forall_lt
+SparsePoly.coeff_toPolyCore_head
+SparsePoly.coeff_toPolyCore_of_degLt
+SparsePoly.coeffs
+SparsePoly.content
+SparsePoly.content_dvd_coeff_final
+SparsePoly.ctorIdx
+SparsePoly.dedupList
+SparsePoly.dedupList.eq_def
+SparsePoly.dedupList_degLt
+SparsePoly.dedupList_sorted
+SparsePoly.degLt
+SparsePoly.degLt_of_sorted_cons
+SparsePoly.deg_pseudo_rem_a
+SparsePoly.deg_pseudo_rem_b
+SparsePoly.degree
+SparsePoly.degree_eq_head
+SparsePoly.degree_eq_natDegree_toPoly
+SparsePoly.degree_eq_poly_degree
+SparsePoly.degree_mul
+SparsePoly.degree_sub_leading_term_lt
+SparsePoly.divRem
+SparsePoly.divRem.eq_def
+SparsePoly.divRem.induct
+SparsePoly.divRem.match_3.congr_eq_1
+SparsePoly.divRem.match_3.congr_eq_2
+SparsePoly.divRem_eq
+SparsePoly.divRem_spec
+SparsePoly.eval
+SparsePoly.evalCore
+SparsePoly.evalCore.eq_def
+SparsePoly.evalCore_eq
+SparsePoly.eval_eq
+SparsePoly.eval_of_core
+SparsePoly.ext
+SparsePoly.ext_iff
+SparsePoly.foldl_gcd_dvd_acc
+SparsePoly.foldl_gcd_dvd_mem
+SparsePoly.gcd
+SparsePoly.gcdPrim
+SparsePoly.gcdPrim.eq_def
+SparsePoly.instAdd
+SparsePoly.instAlgebra
+SparsePoly.instCommRing
+SparsePoly.instDecidableEq
+SparsePoly.instDiv
+SparsePoly.instIsExactDiv
+SparsePoly.instIsExactDivInt
+SparsePoly.instMul
+SparsePoly.instNeg
+SparsePoly.instOne
+SparsePoly.instToFormat
+SparsePoly.instZero
+SparsePoly.mk
+SparsePoly.mk.congr_simp
+SparsePoly.mk.inj
+SparsePoly.mk.injEq
+SparsePoly.mk.noConfusion
+SparsePoly.mk.sizeOf_spec
+SparsePoly.natDegree_toPolyCore_lt
+SparsePoly.noConfusion
+SparsePoly.noConfusionType
+SparsePoly.nonzero
+SparsePoly.ofList
+SparsePoly.ofPoly_X
+SparsePoly.ofSortedList
+SparsePoly.ofSortedList_of_nonzero
+SparsePoly.primitivePart
+SparsePoly.rec
+SparsePoly.recOn
+SparsePoly.sorted
+SparsePoly.toPoly
+SparsePoly.toPolyCore
+SparsePoly.toPolyCore.eq_def
+SparsePoly.toPolyCore_addCore
+SparsePoly.toPolyCore_append
+SparsePoly.toPolyCore_dedupList
+SparsePoly.toPolyCore_filter_nonzero
+SparsePoly.toPolyCore_injective_of_sorted
+SparsePoly.toPolyCore_map_smul
+SparsePoly.toPolyCore_mergeSort
+SparsePoly.toPolyCore_mul_x
+SparsePoly.toPolyCore_mul_y
+SparsePoly.toPolyCore_perm
+SparsePoly.toPolyEquiv
+SparsePoly.toPoly_C
+SparsePoly.toPoly_X
+SparsePoly.toPoly_X_fixed
+SparsePoly.toPoly_add
+SparsePoly.toPoly_injective
+SparsePoly.toPoly_mul
+SparsePoly.toPoly_neg
+SparsePoly.toPoly_ofList
+SparsePoly.toPoly_ofPoly
+SparsePoly.toPoly_one
+SparsePoly.toPoly_pow
+SparsePoly.toPoly_pseudo_rem_a
+SparsePoly.toPoly_pseudo_rem_b
+SparsePoly.toPoly_smul
+SparsePoly.toPoly_smul_X_pow
+SparsePoly.toPoly_smul_X_pow_mul
+SparsePoly.toPoly_zero
+tacticPoly_compute
+tacticPoly_decide
+tacticPoly_dvd
+tacticPoly_eval

No changes to strong technical debt.

Increase in weak tech debt: (relative, absolute) = (3.00, 0.00)
Current number Change Type (weak)
4998 3 exposed public sections

Current commit 7fe43d41b3
Reference commit 29fdc2c22d

This script lives in the mathlib-ci repository. To run it locally, from your mathlib4 directory:

git clone https://github.com/leanprover-community/mathlib-ci.git ../mathlib-ci
../mathlib-ci/scripts/reporting/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).

mkaratarakis and others added 6 commits July 2, 2026 11:38
Files under `Mathlib/Tactic/` must be `module`s (the `Mathlib.Tactic`
aggregator uses `public import` and cannot import a non-`module`). Add the
`module` header and `public import`s, wrap the kernel-reducible defs in
`@[expose] public section` (needed so `decide +kernel` still reduces `toPoly*`
/`evalCore` downstream) and the elaborators in `public meta section`. Make
`coeffCore` and the `k*` list ops public (an exposed body may not reference a
private helper).

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
…msgs

CI runs `lake --iofail test`, which fails on any uncaptured command output.
Wrap each `#print axioms` in `#guard_msgs in`, which both silences the `info:`
message and asserts the axiom set is exactly `[propext, Classical.choice,
Quot.sound]` (no `native_decide` / `Lean.ofReduceBool`).

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
@mkaratarakis
mkaratarakis marked this pull request as ready for review July 3, 2026 08:59
@joneugster joneugster added the LLM-generated PRs with substantial input from LLMs - review accordingly label Jul 3, 2026
@joneugster
joneugster marked this pull request as draft July 3, 2026 09:47
@mathlib-bors

mathlib-bors Bot commented Jul 3, 2026

Copy link
Copy Markdown
Contributor

This pull request is now in draft mode. No active bors state needed cleanup.

While this PR remains draft, bors will ignore commands on this PR. Mark it ready for review before using commands like bors r+ or bors try.

@mkaratarakis

Copy link
Copy Markdown
Contributor Author

@alainchmt

@mkaratarakis
mkaratarakis marked this pull request as ready for review July 3, 2026 10:21
@mkaratarakis
mkaratarakis marked this pull request as draft July 3, 2026 10:23
@mathlib-bors

mathlib-bors Bot commented Jul 3, 2026

Copy link
Copy Markdown
Contributor

This pull request is now in draft mode. No active bors state needed cleanup.

While this PR remains draft, bors will ignore commands on this PR. Mark it ready for review before using commands like bors r+ or bors try.

@mkaratarakis

Copy link
Copy Markdown
Contributor Author

Per review feedback, this PR has been split into a series of ≤300-line PRs for easier review:

SparsePoly series (stacked): #41339 → #41340 → #41341 → #41342 → #41343 → #41344, with the tactic line #41345 → #41346 branching off #41341.
Independent: #41347 (poly_dvd_cert, no dependence on the series).

Closing this one in favour of the series.

@JovanGerb

Copy link
Copy Markdown
Contributor

Hi @mkaratarakis, I have a question about this series of PRs:

There are various polynomial related tactics for which we need to figure out how to best implement then in mathlib. This includes things like deciding equality between polynomials, computing the degree of a polynomial, checking that a polynomial is monic. These operations could be decided in the kernel by using your SparsePoly, or they could be done an a meta level similar to what is done in the ring tactic.

An issues with the kernel approach (which this PR is taking) is that it will only work with polynomials over a coefficient ring that evaluates well in the kernel. Your test file shows that this works with Int. However, in mathlib we want everything to work as generally as possible, which means that it should also work for e.g. Rat or Real. As far as I know we don't have a way to efficiently do Rat computations in the kernel, and of course Real cannot be evaluated in the kernel.

What do you think about this?

Of course, the fact that this was generated with the help of an LLM, and that there are so many lines of code (2000), makes this PR a very significant effort to fully review.

@mkaratarakis

Copy link
Copy Markdown
Contributor Author

Hi @JovanGerb, I agree that the kernel approach only works well for rings that compute, and for the rationals and reals the meta approach is the right one. Actually the series has both: poly_dvd_cert (#41347) is a meta-level tactic in the style of ring, and it works over any field. The kernel tactics are for the computable case, where they can also decide disequalities and ideal membership, which normalization can't do.

But the tactics were not part of my original motivation for this PR. My plan was to PR only the computable polynomials API. But then after talking with Bhavik and others at the Lean and LMFDB workshop a few weeks ago, he advised that the way to get this into mathlib is to also include the corresponding tactics. I am happy to adapt the tactics to whatever design the community prefers or let someone else do it who knows this stuff better than I do.

On the LLM point: as noted in the PR description, the design and definitions are Mario's. Claude was used for the proofs and tactic files. After splitting this PR, the open PRs are around 150-350 LOC each, most of them 200-300.

@JovanGerb

Copy link
Copy Markdown
Contributor

Oh, I hadn't realized that #41347 was entirely independent from the rest. So that one can be reviewed separately. Was there a reason to include it in the same PR?

The kernel tactics are for the computable case, where they can also decide disequalities and ideal membership, which normalization can't do.

In a meta-level tactic, it should also be possible to prove e.g. that two polynomials are disequal, by normalizing their coefficients using norm_num, like the ring tactic does.

@mkaratarakis

Copy link
Copy Markdown
Contributor Author

I agree. #41347 was just a polynomial related tactic, but yeah, I will update the name of that PR since it's not related to the rest of computable polys API

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

Labels

LLM-generated PRs with substantial input from LLMs - review accordingly t-meta Tactics, attributes or user commands

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants