Skip to content

Commit bdb997c

Browse files
authored
feat: precise spec for natural numbers and scientific notation literals (#596)
This PR makes the lexical specification of natural numbers and scientific notation literals more precise.
1 parent c7a5a43 commit bdb997c

File tree

1 file changed

+19
-3
lines changed

1 file changed

+19
-3
lines changed

Manual/Terms.lean

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -913,7 +913,7 @@ end
913913
```
914914
::::
915915

916-
# Literals
916+
# Numeric Literals
917917

918918
There are two kinds of numeric literal: natural number literals and {deftech}[scientific literals].
919919
Both are overloaded via {tech (key := "type class")}[type classes].
@@ -928,6 +928,17 @@ section
928928
variable {n : Nat}
929929
```
930930

931+
Natural numbers can be specified in several forms:
932+
933+
- A sequence of digits 0 through 9 is a decimal literal
934+
- `0b` or `0B` followed by a sequence of one or more 0s and 1s is a binary literal
935+
- `0o` or `0O` followed by a sequence of one or more digits 0 through 7 is an octal literal
936+
- `0x` or `0X` followed by a sequence of one or more hex digits (0 through 9 and A through F, case-insensitive) is a hexadecimal literal
937+
938+
All numeric literals can also contain internal underscores, except for between the first two characters in a binary, octal, or hexadecimal literal.
939+
These are intended to help groups of digits in natural ways, for instance {lean}`1_000_000` or {lean}`0x_c0de_cafe`.
940+
(While it is possible to write the number 123 as {lean}`1_2__3`, this is not recommended.)
941+
931942
When Lean encounters a natural number literal {lean}`n`, it interprets it via the overloaded method {lean}`OfNat.ofNat n`.
932943
A {tech}[default instance] of {lean}`OfNat Nat n` ensures that the type {lean}`Nat` can be inferred when no other type information is present.
933944

@@ -961,15 +972,20 @@ instance : OfNat NatInterval n where
961972
```leanOutput eval8Interval
962973
{ low := 8, high := 8, low_le_high := _ }
963974
```
975+
```lean (name := eval7Interval)
976+
#eval (0b111 : NatInterval)
977+
```
978+
```leanOutput eval7Interval
979+
{ low := 7, high := 7, low_le_high := _ }
980+
```
964981
:::
965982

966983
There are no separate integer literals.
967984
Terms such as {lean}`-5` consist of a prefix negation (which can be overloaded via the {name}`Neg` type class) applied to a natural number literal.
968985

969986
## Scientific Numbers
970987

971-
Scientific number literals consist of a sequence of digits followed by an optional period and decimal part and an optional exponent.
972-
If no period or exponent is present, then the term is instead a natural number literal.
988+
Scientific number literals consist of a sequence of decimal digits followed (without intervening whitespace) by an optional decimal part (a period followed by zero or more decimal digits) and an optional exponent part (the letter `e` followed by an optional `+` or `-` and then followed by one or more decimal digits).
973989
Scientific numbers are overloaded via the {name}`OfScientific` type class.
974990

975991
{docstring OfScientific}

0 commit comments

Comments
 (0)