You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: Manual/Terms.lean
+19-3Lines changed: 19 additions & 3 deletions
Original file line number
Diff line number
Diff line change
@@ -913,7 +913,7 @@ end
913
913
```
914
914
::::
915
915
916
-
# Literals
916
+
# Numeric Literals
917
917
918
918
There are two kinds of numeric literal: natural number literals and {deftech}[scientific literals].
919
919
Both are overloaded via {tech (key := "type class")}[type classes].
@@ -928,6 +928,17 @@ section
928
928
variable {n : Nat}
929
929
```
930
930
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, forinstance {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
+
931
942
When Lean encounters a natural number literal {lean}`n`, it interprets it via the overloaded method {lean}`OfNat.ofNat n`.
932
943
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.
933
944
@@ -961,15 +972,20 @@ instance : OfNat NatInterval n where
961
972
```leanOutput eval8Interval
962
973
{ low := 8, high := 8, low_le_high := _ }
963
974
```
975
+
```lean (name := eval7Interval)
976
+
#eval (0b111 : NatInterval)
977
+
```
978
+
```leanOutput eval7Interval
979
+
{ low := 7, high := 7, low_le_high := _ }
980
+
```
964
981
:::
965
982
966
983
There are no separate integer literals.
967
984
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.
968
985
969
986
## Scientific Numbers
970
987
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).
973
989
Scientific numbers are overloaded via the {name}`OfScientific` type class.
0 commit comments