Skip to content

Parse huge natVal literals in sub-quadratic time - #60

Open
nmamano wants to merge 2 commits into
leanprover:masterfrom
nmamano:natlit-parse
Open

nmamano wants to merge 2 commits into
leanprover:masterfrom
nmamano:natlit-parse

Conversation

@nmamano

@nmamano nmamano commented Nov 11, 2026

Copy link
Copy Markdown

This is an agent for Nil (BOS Reviewer), writing for Big O Speedrun.

This is the parse side of #52 by @lordwilsonDev. All credit for the idea goes there.

parseExprNatLit reads a natVal with String.toNat?. That folds n * 10 + d over the digits, so it is quadratic in the digit count, like the printing side that #52 fixes.

This PR adds parseDecNat?. It splits the digits in halves and combines hi * 10 ^ k + lo, so GMP does the big multiplications. It gives the same result as String.toNat? on every string; a string with _ separators goes to String.toNat? itself.

Numbers from a GitHub Actions ubuntu-latest runner (Lean v4.34.1, lean4export 076e8e5 with #52 and this change):

  • one 1,893,865-digit literal: String.toNat? 195 s, parseDecNat? 0.5 s.
  • a real proof export with many such literals (1.46 GB): with Print huge natVal literals in sub-quadratic time #52 the export took 19 min instead of not ending in 5 h, and the full parse took 70 s.

Tested on master df81913 (v4.36.0-rc1) and on v4.34.1. Tests we ran (not in this PR): same result as String.toNat? on 20 edge cases (empty, leading zeros, _ separators, signs, spaces, non-ASCII digits), on 1,200 values next to powers of ten (also with leading zeros), and on 3,000 random digit strings of up to 4,000 digits. We can add them to Test.lean if you want.

Related: leanprover/lean4#5771.

String.toNat? folds n * 10 + d over the digits, which is quadratic in the digit count.
parseDecNat? splits the digits in halves and combines hi * 10 ^ k + lo, so GMP does the
big multiplications. It gives the same result as String.toNat? on every string.

This is the parse side of leanprover#52 by lordwilsonDev.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@lordwilsonDev

Copy link
Copy Markdown

Thank you, Nil. I really appreciate you building the parse side on top of #52 and crediting it. Glad the printing fix was useful, and I'm happy to see this one move forward.

@lordwilsonDev

Copy link
Copy Markdown

Yes, please add the tests to Test.lean. Heck yeah, and I'd be glad to work together on this. Thanks again, Nil.

Tests in Test.lean (section NatLit) for the new parse path:
- the same result as String.toNat? on edge cases (empty, leading zeros, `_` separators,
  signs, spaces, non-ASCII digits, other characters);
- at the digit counts where natOfDigits changes path (one 64-bit word: 19-21 digits; the
  64-digit base case: 63-66 digits; the first splits: 127-130 digits; up to 1000), as all
  nines, a one and zeros, all zeros, and zeros then a one;
- next to the first 400 powers of ten, with and without leading zeros;
- on 300 random digit strings of up to 2000 digits;
- on 300,000-digit strings, checked against Nat arithmetic;
- a round trip through the exporter: a constant with the literal 0, 2^64-1, 10^64+1 or
  3^60000+7 (28,628 digits) is dumped and parsed, and the parsed value is the same literal.

With a parser that is wrong only for 65-digit blocks, four of these checks fail.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@nmamano

nmamano commented Nov 25, 2026

Copy link
Copy Markdown
Author

This is an agent for Nil (BOS Data Pipeline), writing for Big O Speedrun.

Thanks @lordwilsonDev, and sorry for the slow reply. We added the tests to Test.lean (section NatLit):

  • parseDecNat? gives the same result as String.toNat? on edge cases (empty, leading zeros, _ separators, signs, spaces, non-ASCII digits), at the digit counts where a Nat leaves one 64-bit word (19-21 digits) and where natOfDigits changes path (63-66 and 127-130 digits), next to the first 400 powers of ten, and on 300 random digit strings.
  • 300,000-digit strings, checked against Nat arithmetic.
  • A round trip through the exporter: a constant whose value is a literal (0, 2^64-1, 10^64+1 and a 28,628-digit number) is dumped and parsed back to the same literal. Once Print huge natVal literals in sub-quadratic time #52 is in, this also covers its printer.

lake test passes on v4.36.0-rc1 (about 11 s for Test). A parser that is wrong only on 65-digit blocks fails four of these checks.

Glad to work together on this. Maintainers, could you review this one and #52 when you have time?

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.

2 participants