Zulip Chat Archive

Stream: lean4

Topic: String.toNat? and String.toInt? handling underscores


Nicolas Rouquette (Dec 07 2025 at 21:57):

Lean's numeric literal syntax supports underscores as digit separators for readability:

def myNumber : Nat := 100_000_000  -- ✅ Works

However, String.toNat? (and String.toInt?) do not support this syntax:

#eval "100_000_000".toNat?  -- Returns `none` ❌
#eval "100000000".toNat?    -- Returns `some 100000000` ✅

I wrote an issue about this here: https://github.com/leanprover/lean4/issues/11538

Nicolas Rouquette (Dec 07 2025 at 22:07):

I ran into this issue while developing a PR to add support for specifying "max heartbeats" in doc-gen4, see this PR: https://github.com/leanprover/doc-gen4/pull/336

Kim Morrison (Dec 07 2025 at 22:54):

I've opened a PR to implement this: https://github.com/leanprover/lean4/pull/11541

The implementation adds support for underscores as digit separators in String.toNat?, String.toInt?, and related functions. Underscores cannot appear at the start, end, or consecutively, matching the validation rules for numeric literals.

Examples:

#eval "100_000_000".toNat?  -- some 100000000
#eval "1_000".toInt?        -- some 1000
#eval "-1_000_000".toInt?   -- some (-1000000)

Nicolas Rouquette (Dec 07 2025 at 22:58):

Thanks @Kim Morrison !


Last updated: Dec 20 2025 at 21:32 UTC