Print huge natVal literals in sub-quadratic time - #52
Open
lordwilsonDev wants to merge 1 commit into
Open
lordwilsonDev wants to merge 1 commit into
lordwilsonDev wants to merge 1 commit into
Conversation
`Nat.repr` (used by s!"{i}") converts a Nat to decimal one digit at a time,
each step a division of the whole number: quadratic in the digit count. A proof
term containing a literal with millions of digits stalls the exporter at 100% CPU
inside Nat.reprFast/toDigitsCore/gmpn_divrem_1 with no output.
natToDecStr splits by 10^(18*2^k) so GMP's sub-quadratic division does the work.
Output is identical to Nat.repr.
Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
lordwilsonDev
pushed a commit
to lordwilsonDev/fcve
that referenced
this pull request
Sep 19, 2026
…ependent checks - skills/verifying-lean-proofs: snapshot of the upgraded skill (LESSONS.md, patches/, hardened scripts, BUG-002..005) - independent-checks/skill-run2: 3 formerly BLOCKED theorems now PASS with the fast-literal tools - HANDOFF: upstream PRs leanprover/lean4export#52 and ammkrn/nanoda_lib#36 Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
Contributor
|
Related issue: leanprover/lean4#5771 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
natValliterals are printed withs!"{i}", i.e.Nat.repr, which converts one digit at a time with a division of the whole number: quadratic in the number of digits. A proof term that contains a literal with millions of digits makes the exporter sit at 100% CPU with no output (sampleshowsNat.reprFast→toDigitsCore→__gmpz_tdiv_q/_r→__gmpn_divrem_1).Real example:
results_eliahou_theorem_1_1in tangentstorm/eliahou-collatz-bounds @db804ce6305ea99a817f067869607f8b677d895a(Lean v4.28.0,lake env lean4export Results -- results_eliahou_theorem_1_1). The closure contains literals of up to 25,628,966 digits (81 literals over 50 digits). Unpatched: the output stops at 71 MB and then makes no progress for 8+ minutes of CPU (I killed it; I have not shown it never finishes). It looks like a hang, and I first misdiagnosed it as a memoization problem until I sampled it.Change
natToDecStrsplits the number by 10^(18·2^k) recursively, so GMP's sub-quadratic division does the work, and pads the low parts to a fixed width. The output is identical toNat.repr. Only thenatValcase indumpExprAuxchanges (+26/−1 lines,Export.leanonly).Evidence
natToDecStr n == toString non ~3,000 pseudo-random numbers (up to 40th powers of 64-bit values) plus edge cases (0, 10^18−1, 10^18, 10^36±1, 2^200, 2^5000, 10^100−1): 0 mismatches.What I did not test
I built and tested this on Lean v4.28.0 and v4.34.0.
masteris on v4.35.0-rc2, which I could not install locally (disk); the patch applies cleanly tomasterand uses onlyArray,String.pushn,Natdivision/modulo andId.run, but CI is the first real build on that toolchain. If a newer Lean'sNat.repris already sub-quadratic for big numbers, this may be unnecessary — I have not checked.🤖 Generated with Claude Code