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

Closed
Command-Master opened this issue Nov 24, 2024 · 4 comments · Fixed by #6204
Closed

RFC: Visual Separators in Number Literals #6199

Command-Master opened this issue Nov 24, 2024 · 4 comments · Fixed by #6204
Labels
RFC accepted RFC is waiting for a corresponding PR (external or internal) 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
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.

@Kha
Copy link
Member

Kha commented Dec 6, 2024

RFC accepted!

@Kha Kha added the RFC accepted RFC is waiting for a corresponding PR (external or internal) label Dec 6, 2024
github-merge-queue bot pushed a commit that referenced this issue Dec 8, 2024
This PR lets `_` be used in numeric literals as a separator. For
example, `1_000_000`, `0xff_ff` or `0b_10_11_01_00`. New lexical syntax:
```text
numeral10 : [0-9]+ ("_"+ [0-9]+)*
numeral2  : "0" [bB] ("_"* [0-1]+)+
numeral8  : "0" [oO] ("_"* [0-7]+)+
numeral16 : "0" [xX] ("_"* hex_char+)+
float     : numeral10 "." numeral10? [eE[+-]numeral10]
```

Closes #6199
JovanGerb pushed a commit to JovanGerb/lean4 that referenced this issue Jan 21, 2025
This PR lets `_` be used in numeric literals as a separator. For
example, `1_000_000`, `0xff_ff` or `0b_10_11_01_00`. New lexical syntax:
```text
numeral10 : [0-9]+ ("_"+ [0-9]+)*
numeral2  : "0" [bB] ("_"* [0-1]+)+
numeral8  : "0" [oO] ("_"* [0-7]+)+
numeral16 : "0" [xX] ("_"* hex_char+)+
float     : numeral10 "." numeral10? [eE[+-]numeral10]
```

Closes leanprover#6199
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
RFC accepted RFC is waiting for a corresponding PR (external or internal) RFC Request for comments
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants