Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RFC: Visual Separators in Number Literals #6199

Open
Command-Master opened this issue Nov 24, 2024 · 3 comments · May be fixed by #6204
Open

RFC: Visual Separators in Number Literals #6199

Command-Master opened this issue Nov 24, 2024 · 3 comments · May be fixed by #6204
Labels
RFC Request for comments

Comments

@Command-Master
Copy link
Contributor

Command-Master commented Nov 24, 2024

Proposal

Many languages (Java, Python, Perl, Ruby, Julia, Ada, JavaScript, PHP, D, Elixir, C#, C++, Rust, Haskell etc.) support using some character as a visual separator in number literals, mostly _, except C++ which uses '. This suggestion is for this behavior in Lean.

Community Feedback

Zulip discussion

Impact

Add 👍 to issues you consider important. If others benefit from the changes in this proposal being added, please ask them to add 👍 to it.

@Command-Master Command-Master added the RFC Request for comments label Nov 24, 2024
@kmill
Copy link
Collaborator

kmill commented Nov 24, 2024

I looked at a few languages for their lexical syntaxes, and there are broadly two conventions that use _. First, the "Ada convention" (used by Ada, Python, and Javascript) is that the sequence digits in the literal have the following lexical syntax:

numeral := digit many("_"? digit)

Second, the "Rust convention" (used by Rust, Java, and C#):

numeral := digit many(digit <|> "_")

(For other bases, Rust allows _ at the beginning, like 0b_11_00.) Java and C# require that the numeral end in a digit.

I don't have much of a preference over whether it should be allowed to have sequences of _'s, but I think we should adopt a lexical syntax that satisfies the following properties:

  • numerals should not be allowed to end with _
  • numerals with a base prefix should be allowed to begin with _ (like 0b_11_00).

kmill added a commit to kmill/lean4 that referenced this issue Nov 25, 2024
This PR allows `_` in numeric literals as a separator. For example, `1_000_000`, `0xff_ff` or `0b_10_11_01_00`.

Closes leanprover#6199
@kmill kmill linked a pull request Nov 25, 2024 that will close this issue
@kmill
Copy link
Collaborator

kmill commented Nov 25, 2024

I made a reference implementation of the RFC at #6204 and I'll leave it to the triage team to decide whether to accept it.

@edwardzcn-decade
Copy link

Add lexical structure information for similar proof tools.

Coq: Uses "Rust conventions" and allows continuing underscores.
Isabelle: I couldn’t find a similar lexical allowance for separators. This requires further verification.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
RFC Request for comments
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants