Skip to content

Optimize ConwayRefinement private imports and algebra proofs - #809

Open
Vilin97 wants to merge 1 commit into
mainfrom
codex/daily-optimize-20261113
Open

Vilin97 wants to merge 1 commit into
mainfrom
codex/daily-optimize-20261113

Conversation

@Vilin97

@Vilin97 Vilin97 commented Nov 13, 2026 •

Copy link
Copy Markdown
Collaborator

ConwayRefinement's 775-module aggregate took about 24 minutes to build in the controlled baseline. This pass narrows private dependencies and replaces expensive general proof automation with direct mathematical arguments. The final paired clean build took 20:39.19 versus 24:19.11, an observed 15.07% reduction in wall time on the VM.

The target is an existing completed project with about 150,000 lines and a large import closure. A separate baseline native profile attributed 4.36 seconds to generic congruence in the monomial Leibniz proof, providing a concrete proof-elaboration hotspot alongside the import work.

  • Reduce private import directives from 964 to 759, retaining all 3,231 original public import directives, their order, and their normal/meta/all flags. Keep macro quotation dependencies explicitly and protect imports used by anonymous examples with lake shake annotations.
  • Replace 101 uses of ring for pure associativity/commutativity with ac_rfl.
  • Prove the monomial Leibniz identity with an index-equality split and targeted associativity rewrites; use explicit witnesses for support/image identities.
  • Share finite-sum endpoint extraction, use direct order lemmas in the power-remainder argument, and transport units through the coefficient ring homomorphism instead of repeating integer-unit classifications.
  • Use integer-cast injectivity, direct composition of the primality equivalences, and a coefficient-function equality instead of pointwise rewriting.

The change is confined to 225 existing Lean files in ConwayRefinement. Existing mathematical statements, definitions, instances, notation, namespaces and exported declarations are preserved. Original proof steps are retained where changing them would disturb generated private declaration names or types.

Base: d1019ceec2f68c6ff2eaa59a413bc20b3fceac0b.
Head: 2371d531db7fc6b3bd6ecfef0444cf4bbb000994.
origin/main was refetched immediately before commit and still equals the measured base.

Before editing, the base source hashes and compiled environment were saved. The invariant audit walks each module's own ModuleData.constants rather than looking names up in the merged environment, so colliding names in different modules are checked independently. It compares structural expression DAGs, including binder names/annotations, ordered universe parameters, declaration kind, safety, reducibility, all non-theorem values, and inductive/constructor/recursor metadata and computation rules. Only theorem proof bodies and expression source metadata are omitted.

Result: all 15,483 pre-existing declaration records match exactly; none are removed or changed. Two supporting declarations are added: the private finite-sum helper and the generated intCoeffRingHom.congr_simp lemma. Independent source audits also find no changed signatures, preserve 104 non-theorem source blocks in proof-edited files, and confirm the public import/export closure is unchanged.

Clean build Wall time User + system CPU seconds GNU time peak process RSS (KiB)
Initial baseline 24:06.74 2772.94 1,637,364
Repeated baseline 24:19.11 2840.69 1,637,152
Optimized 20:39.19 2679.54 1,622,692

The primary comparison is the consecutive repeated-baseline/optimized pair: wall time decreases 15.07%, CPU time 5.67%, and process peak RSS 0.88%. Both builds compile all 775 project modules freshly. Only this project's build products are removed; dependencies and the compiler linter plugin are prebuilt. Both use the unchanged pin from the measured main snapshot: Lean 4.35.0, Mathlib 56a005855ea17e0b0958f73ec801ae140b0a1679.

/usr/bin/time -v taskset -c 8-11 env LEAN_NUM_THREADS=4 \
  lake build LeanPool.ConwayRefinement.Imports

ARTIFACTS=/data/codex/scratch/lean4lean4/optimization-20261113
mapfile -t selected_modules < "$ARTIFACTS/modules.txt"
lake env lean --run scripts/ci/lint-project.lean "${selected_modules[@]}"
lake exe lint-style LeanPool.ConwayRefinement.Imports
lake env lean --run "$ARTIFACTS/ManifestDAG.lean" \
  "$ARTIFACTS/manifest-head-final.jsonl" "${selected_modules[@]}"
python3 "$ARTIFACTS/compare_dag_manifests.py" \
  "$ARTIFACTS/manifest-base-complete.jsonl" \
  "$ARTIFACTS/manifest-head-final.jsonl" "$ARTIFACTS/manifest-final-diff.json"
(cd python && uv run python "$ARTIFACTS/check_project_quality.py")
(cd python && uv run python -m lean_pool.build_time_limits check \
  --log "$ARTIFACTS/optimized-repaired-build.log" \
  --base d1019ceec2f68c6ff2eaa59a413bc20b3fceac0b --root ..)
python3 "$ARTIFACTS/audit_statements.py"
python3 "$ARTIFACTS/audit_source_bodies.py"
python3 "$ARTIFACTS/audit_public_directives.py"
python3 "$ARTIFACTS/audit_public_imports.py"
git diff --check

Validation passed: warning-free complete project build; slow declaration linters for all 775 modules; Mathlib style; selected-project header, forbidden-text, size, reachability, metadata/card and generated-index checks; compiled axiom and option-backdoor audits; main-declaration checks; source/API audits; and git diff --check. Every changed module has a fresh recorded build time and passes the unchanged 600-second limit; the largest changed-module time is 40 seconds. The pool discovery root, project aggregate and repository configuration match the base.

The audit harness, raw manifests, measurements, source fingerprints and logs are retained on the VM in /data/codex/scratch/lean4lean4/optimization-20261113/. The manifest comparison uses ManifestDAG.lean and compare_dag_manifests.py; the final result is manifest-final-diff.json. Base manifest SHA-256: 1985df9bd49e5ee396dc4b8c38b58e85ce6f5c24ec7afc77d701b198d3a8b66a. Head manifest SHA-256: f74a47ca431e281aed7728b4df70974ebc175810c38c0a461353438d4827bc66.

These are local measurements on a shared VM, with fixed CPU affinity but no exclusive reservation. Hosted CI and independent review still need to pass on the required revisions. This PR is for review; this cycle does not request queue entry or auto-merge.

@greptile-apps

greptile-apps Bot commented Nov 13, 2026 •

Copy link
Copy Markdown
Contributor

Advisory cross-file review against Lean Pool's supervisory rubric. This is not by itself a significance, source-verification, or merge verdict.

RetriggerView in Greptile

[Medium impact] No actionable defect was found in the reviewed changes.

Summary

Narrows private imports and replaces broad proof automation in ConwayRefinement, while retaining the existing mathematical statements.

  • ConwayRefinement builds faster with fewer private imports and more direct proofs.

Reviews (1) · Last reviewed commit: "Optimize ConwayRefinement private import..." · Reviewed by Greptile

@github-actions

Copy link
Copy Markdown
Contributor

Proof profile (new / modified Lean files)

Statement changes

✅ No statements changed — all 3,510 tracked declarations across 225 modified files kept their type; only proofs / definition bodies changed.

Also: 1 declaration added, 0 removed. A reorganization moves declarations between files, which shows up here as an add in one file and a remove in another.

➕ Declarations added (1)
  • Berarducci.sum_eq_add_sum_erase_of_eq_zero — LeanPool/ConwayRefinement/ConwayRefinement/HahnSeries/OrdinalValue/PowerRemainder.lean

Statements are compared textually against the merge base (comments and whitespace ignored; by proof terms embedded in a type are treated as proof-irrelevant), the same method as physlib's /check-golf. Anonymous instances and examples are not tracked.

Compile cost of modified files — base → head

Heartbeats: 22,218,617 → 22,108,615 (-110,001, -0.5%). Commands: 8,863 → 8,863.

Per-file wall sum: 3119.8 s → 3597.3 s (+15.3%). Wall clock is measured under parallel load and is noisy; heartbeats are deterministic and are the regression signal.

Files: 221 compared — 5 cheaper, 9 costlier, 207 within noise (threshold: |Δ heartbeats| ≥ 1,000 and ≥ 1%).

Both sides are elaborated against the merge-base build of the pool — sound when only proofs and definition bodies change; a file whose statements changed may fail there and is flagged ⚠. Heartbeats come from disjoint root command traces with trace.profiler.useHeartbeats and synchronous elaboration, converted to maxHeartbeats units. Failed measurements are not classified as cheaper or costlier. Δ lines counts added/deleted lines from this PR diff.

File Δ lines HB base HB head Δ HB Δ HB % Wall base (s) Wall head (s)
LeanPool/ConwayRefinement/ConwayRefinement/Surreal/OmnificInteger/Primality/IrreducibleOmnificIntegers.lean +6/-23 410,615 357,233 -53,382 -13.0% 43.58 43.84
LeanPool/ConwayRefinement/ConwayRefinement/Surreal/HahnSeries/NormalFormBasic.lean +4/-1 74,342 31,074 -43,268 -58.2% 11.49 7.72
LeanPool/ConwayRefinement/CombinatorialGames/Surreal/Pow.lean +6/-1 232,640 203,024 -29,617 -12.7% 36.73 35.48
LeanPool/ConwayRefinement/ConwayRefinement/Algebra/Divisibility/PrimalPreimage.lean +47/-47 253,163 271,869 +18,705 +7.4% 27.64 33.69
LeanPool/ConwayRefinement/ConwayRefinement/Algebra/LoweringDerivation/Mu.lean +1/-2 204,403 187,729 -16,674 -8.2% 32.48 28.81
LeanPool/ConwayRefinement/ConwayRefinement/HahnSeries/FiniteSupportScalarExtension.lean +3/-9 623,347 608,903 -14,444 -2.3% 89.23 119.91
LeanPool/ConwayRefinement/ConwayRefinement/HahnSeries/Factorization/InfiniteSupport.lean +2/-2 241,160 250,348 +9,188 +3.8% 32.88 27.08
LeanPool/ConwayRefinement/ConwayRefinement/HahnSeries/OrdinalValue/Tests/PrincipalSubringFiniteSupport.lean +2/-2 172,103 177,477 +5,374 +3.1% 26.34 21.39
LeanPool/ConwayRefinement/ConwayRefinement/HahnSeries/Factorization/SeriesMaximalMultiplicativity.lean +1/-3 257,072 262,339 +5,267 +2.0% 37.02 40.04
LeanPool/ConwayRefinement/ConwayRefinement/HahnSeries/Factorization/DegreeTwo/TranslatedSpanFactorization.lean +2/-4 717,458 713,036 -4,423 -0.6% 145.26 146.08
LeanPool/ConwayRefinement/ConwayRefinement/HahnSeries/OrdinalValue/PrincipalSubringFractionAlgebraic.lean +2/-3 505,858 508,753 +2,895 +0.6% 38.43 54.91
LeanPool/ConwayRefinement/ConwayRefinement/HahnSeries/Factorization/Random/NonPrincipalIrreducible.lean +1/-5 102,677 105,330 +2,654 +2.6% 12.29 23.31
LeanPool/ConwayRefinement/ConwayRefinement/HahnSeries/OrdinalValue/Statements/MainLemma.lean +3/-2 341,894 339,383 -2,511 -0.7% 69.26 58.09
LeanPool/ConwayRefinement/ConwayRefinement/RingTheory/LocalizationUFM.lean +4/-4 19,502 21,759 +2,258 +11.6% 4.61 3.99
LeanPool/ConwayRefinement/ConwayRefinement/Algebra/LoweringDerivation/Correction.lean +1/-2 396,443 397,977 +1,534 +0.4% 31.00 48.81
LeanPool/ConwayRefinement/ConwayRefinement/Surreal/HahnSeries/NormalFormMul.lean +2/-2 99,367 100,822 +1,455 +1.5% 18.81 19.61
LeanPool/ConwayRefinement/ConwayRefinement/Algebra/MvPolynomial/OrdinalDerivation.lean +1/-1 50,239 51,595 +1,357 +2.7% 9.40 9.13
LeanPool/ConwayRefinement/ConwayRefinement/Algebra/DirectSum/GermSuccessorStep.lean +1/-1 126,662 127,986 +1,324 +1.0% 18.51 18.17
LeanPool/ConwayRefinement/ConwayRefinement/HahnSeries/OrdinalValue/AlgebraicIndependence/TruncationExpansion.lean +1/-1 306,545 307,784 +1,239 +0.4% 24.90 32.69
LeanPool/ConwayRefinement/ConwayRefinement/HahnSeries/Germ/AlgebraicIndependence/TruncationExpansion.lean +1/-1 835,359 836,593 +1,235 +0.1% 46.62 81.83
LeanPool/ConwayRefinement/ConwayRefinement/HahnSeries/OrdinalValue/ConvolutionRemainder.lean +2/-3 260,422 259,477 -944 -0.4% 37.15 56.99
LeanPool/ConwayRefinement/ConwayRefinement/Algebra/Divisibility/DenominatorIdeal.lean +6/-6 19,974 20,843 +869 +4.4% 5.61 6.79
LeanPool/ConwayRefinement/ConwayRefinement/Algebra/LoweringDerivation/IdealGEGraded.lean +2/-1 121,829 121,126 -703 -0.6% 14.74 25.25
LeanPool/ConwayRefinement/ConwayRefinement/LinearAlgebra/TensorProduct/Content.lean +1/-1 57,853 58,317 +464 +0.8% 10.08 10.18
LeanPool/ConwayRefinement/ConwayRefinement/HahnSeries/OrdinalValue/PrincipalSubringLocalization.lean +3/-2 573,946 573,554 -392 -0.1% 86.25 93.48
LeanPool/ConwayRefinement/ConwayRefinement/HahnSeries/Factorization/Random/GradedIrreducible.lean +1/-3 333,356 333,663 +307 +0.1% 46.80 50.20
LeanPool/ConwayRefinement/ConwayRefinement/HahnSeries/Germ/AlgebraicIndependence/Power.lean +3/-3 70,875 71,175 +300 +0.4% 13.57 16.36
LeanPool/ConwayRefinement/ConwayRefinement/HahnSeries/OrdinalValue/Tests/Convolution.lean +2/-3 1,079 1,368 +289 +26.8% 2.15 3.10
LeanPool/ConwayRefinement/ConwayRefinement/HahnSeries/OrdinalValue/ConvolutionList.lean +3/-2 60,667 60,492 -175 -0.3% 7.47 11.22
LeanPool/ConwayRefinement/ConwayRefinement/Surreal/HahnSeries/Full.lean +1/-1 11,385 11,551 +166 +1.5% 3.27 4.36
LeanPool/ConwayRefinement/ConwayRefinement/HahnSeries/Factorization/Random/RandomBlocks.lean +1/-4 22,734 22,569 -164 -0.7% 6.61 6.45
LeanPool/ConwayRefinement/ConwayRefinement/HahnSeries/Factorization/Random/SupportRandom.lean +1/-1 46,179 46,036 -143 -0.3% 4.46 6.68
LeanPool/ConwayRefinement/ConwayRefinement/HahnSeries/OrdinalValue/AlgebraicIndependence/Pieces.lean +1/-1 17,611 17,743 +132 +0.7% 5.51 5.01
LeanPool/ConwayRefinement/ConwayRefinement/Algebra/MonoidAlgebra/LatticeFunctional.lean +7/-3 13,277 13,396 +120 +0.9% 2.87 3.44
LeanPool/ConwayRefinement/ConwayRefinement/SetTheory/Ordinal/NaturalPrincipal.lean +2/-2 10,513 10,402 -111 -1.1% 4.21 3.79
LeanPool/ConwayRefinement/ConwayRefinement/HahnSeries/OrdinalValue/PurePowerRemainder.lean +1/-1 100,359 100,453 +94 +0.1% 16.70 22.12
LeanPool/ConwayRefinement/ConwayRefinement/HahnSeries/OrdinalValue/Tests/AlgebraicIndependence/PrincipalIdealGE.lean +2/-1 14,437 14,356 -80 -0.6% 3.68 3.36
LeanPool/ConwayRefinement/ConwayRefinement/HahnSeries/Germ/AlgebraicIndependence/CantorBendixsonValueMultiplicativity.lean +2/-2 95,334 95,262 -72 -0.1% 17.94 18.81
LeanPool/ConwayRefinement/ConwayRefinement/HahnSeries/OrdinalValue/ResidualPointOrderType.lean +2/-1 38,962 38,916 -47 -0.1% 9.38 9.58
LeanPool/ConwayRefinement/ConwayRefinement/HahnSeries/OrdinalValue/Statements/ProductValue.lean +4/-4 51,799 51,845 +46 +0.1% 9.90 11.34
LeanPool/ConwayRefinement/ConwayRefinement/HahnSeries/OrdinalValue/CriticalPoint.lean +2/-2 71,495 71,452 -43 -0.1% 11.09 17.75
LeanPool/ConwayRefinement/ConwayRefinement/HahnSeries/Factorization/DegreeTwo/FactorizationClassification.lean +2/-4 50,156 50,113 -43 -0.1% 7.68 10.11
LeanPool/ConwayRefinement/ConwayRefinement/Order/Archimedean.lean +2/-2 2,013 1,983 -29 -1.5% 2.06 2.43
LeanPool/ConwayRefinement/ConwayRefinement/FieldTheory/MonicFactorCoefficients.lean +1/-1 5,268 5,295 +27 +0.5% 3.25 4.19
LeanPool/ConwayRefinement/ConwayRefinement/HahnSeries/OrdinalValue/Convolution.lean +2/-1 66,485 66,458 -27 -0.0% 16.87 14.52
LeanPool/ConwayRefinement/ConwayRefinement/HahnSeries/OrdinalValue/PrincipalComponentTensor.lean +1/-2 550,403 550,426 +23 +0.0% 66.55 73.39
LeanPool/ConwayRefinement/ConwayRefinement/Topology/Order/PWOSumset.lean +1/-1 36,518 36,504 -14 -0.0% 5.67 8.13
LeanPool/ConwayRefinement/ConwayRefinement/Order/Filter/FunAtZeroMinus/Pointwise.lean +2/-1 4,274 4,265 -9 -0.2% 3.00 2.22
LeanPool/ConwayRefinement/ConwayRefinement/Algebra/Valuation/Tests/DegreeAssociatedGraded.lean +5/-1 2,023 2,017 -6 -0.3% 2.92 2.79
LeanPool/ConwayRefinement/ConwayRefinement/Standalone/Mathlib/Support/HahnSeriesGCDProof.lean +1/-1 21,048 21,047 -0 -0.0% 5.10 6.23
… +171 more files, each with a smaller move (≤ 0 HB); full table in the run's step summary
Total +266/-434 22,218,617 22,108,615 -110,001 -0.5% 3119.8 3597.3

New files — absolute profile

Total heartbeats: 1,135,734 maxHeartbeats units across 1 valid file (33 added LOC).

Sum of lean --profile: — across 0 valid files. Import-excluded time: —.

Count-heartbeats wall-clock total: 113.35 s. Repeated import cost inside lean --profile: —.

Heartbeat values sum disjoint root command traces with trace.profiler.useHeartbeats and synchronous elaboration, converted from raw allocations to maxHeartbeats units by dividing by 1000. They include command elaboration and kernel checking, exclude imports, and include profiler overhead; they are not comparable to the deprecated linter's totals. Per-file wall clocks are measured under parallel load and are noisier than heartbeats.

LOC counts added lines in the profiled Lean files from this PR diff.

File LOC Heartbeats (maxHB) Count wall (s) lean --profile (s) Without import (s) Import (s) Commands Errors
LeanPool/ConwayRefinement/ConwayRefinement/HahnSeries/Germ/AlgebraicIndependence/LowerTruncationDegree.lean 5 1,135,734 113.35 — — — 94 0
LeanPool/ConwayRefinement/ConwayRefinement/HahnSeries/OrdinalValue/PowerRemainder.lean 21 — — — — — 24 1
LeanPool/ConwayRefinement/ConwayRefinement/HahnSeries/PolynomialAlgebra/PolynomialPresentation.lean 5 — — — — — 55 1
LeanPool/ConwayRefinement/ConwayRefinement/HahnSeries/TruncationIntegerPartPrimal.lean 2 — — — — — 18 1
Total 33 1,135,734 113.35 — — — 191 3

Aggregate phase totals

Phase Time
Unavailable —

Advisory only — never blocks merge. Full log uploaded as the proof-profile artifact.

@Vilin97

Vilin97 commented Nov 13, 2026

Copy link
Copy Markdown
Collaborator Author

🤖 LLM review — refactor (gpt-6.1-sol)

Reviewed head: 2371d531db7fc6b3bd6ecfef0444cf4bbb000994

Verdict: ✅ approve

This narrows private imports and replaces general automation with explicit witnesses, targeted rewrites, unit transport, and a shared finite-sum helper. Proof structure and public imports are preserved. The supplied profile reports 0.5% fewer heartbeats across 221 comparable files, despite modest local increases. Targeted compilation checks passed; the full-project build and claimed wall-time improvement were not independently reproduced.

Aspect Value
Scope private import pruning and proof rewrites
Introduces tech debt ✅ no
Maintainability ✅ improved
Brittleness ✅ more_robust
Risk ✅ low

The refactor makes dependencies narrower and mathematical arguments more explicit without losing reusable structure or introducing brittle automation.


Tokens: 5,144,402 in / 16,418 out · Tier: codex-subscription · Effort: xhigh · Billing: Codex subscription quota (no API credits) · Estimated cost: $20.8239 (Standard API equivalent; uncached input) (long-context rate)
Automated review against .github/REFACTOR_REVIEW_RULES.md. Disagree? Reply on the PR; rules can be updated in a PR of their own.

This branch has not been deployed

No deployments
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.

1 participant