Module docstring
{}
{}
ToString
structure
(α : Type u)
/--
Types that can be converted into a string for display.
There is no expectation that the resulting string can be parsed back to the original data (see
`Repr` for a similar class with this expectation).
-/
class ToString (α : Type u) where
/-- Converts a value into a string. -/
toString : α → String
instToStringId
instance
{α} [ToString α] : ToString (id α)
instToStringId_1
instance
{α} [ToString α] : ToString (Id α)
instToStringString
instance
: ToString String
instance : ToString String :=
⟨fun s => s⟩
instToStringSubstring
instance
: ToString Substring
instance : ToString Substring :=
⟨fun s => s.toString⟩
instToStringIterator
instance
: ToString String.Iterator
instance : ToString String.Iterator :=
⟨fun it => it.remainingToString⟩
instToStringBool
instance
: ToString Bool
instance : ToString Bool :=
⟨fun b => cond b "true" "false"⟩
instToStringDecidable
instance
{p : Prop} : ToString (Decidable p)
instance {p : Prop} : ToString (Decidable p) := ⟨fun h =>
match h with
| Decidable.isTrue _ => "true"
| Decidable.isFalse _ => "false"⟩
List.toString
definition
[ToString α] : List α → String
/--
Converts a list into a string, using `ToString.toString` to convert its elements.
The resulting string resembles list literal syntax, with the elements separated by `", "` and
enclosed in square brackets.
The resulting string may not be valid Lean syntax, because there's no such expectation for
`ToString` instances.
Examples:
* `[1, 2, 3].toString = "[1, 2, 3]"`
* `["cat", "dog"].toString = "[cat, dog]"`
* `["cat", "dog", ""].toString = "[cat, dog, ]"`
-/
protected def List.toString [ToString α] : List α → String
| [] => "[]"
| [x] => "[" ++ toString x ++ "]"
| x::xs => xs.foldl (· ++ ", " ++ toString ·) ("[" ++ toString x) |>.push ']'
instToStringList
instance
{α : Type u} [ToString α] : ToString (List α)
instance {α : Type u} [ToString α] : ToString (List α) :=
⟨List.toString⟩
instToStringPUnit
instance
: ToString PUnit.{u + 1}
instance : ToString PUnitPUnit.{u+1} :=
⟨fun _ => "()"⟩
instToStringULift
instance
{α : Type u} [ToString α] : ToString (ULift.{v} α)
instance {α : Type u} [ToString α] : ToString (ULift.{v} α) :=
⟨fun v => toString v.1⟩
instToStringUnit
instance
: ToString Unit
instance : ToString Unit :=
⟨fun _ => "()"⟩
instToStringNat
instance
: ToString Nat
instance : ToString Nat :=
⟨fun n => Nat.repr n⟩
instToStringPos
instance
: ToString String.Pos
instance : ToString String.Pos :=
⟨fun p => Nat.repr p.byteIdx⟩
instToStringInt
instance
: ToString Int
instance : ToString Int where
toString
| Int.ofNat m => toString m
| Int.negSucc m => "-" ++ toString (succ m)
instToStringChar
instance
: ToString Char
instance : ToString Char :=
⟨fun c => c.toString⟩
instToStringFin
instance
(n : Nat) : ToString (Fin n)
instance (n : Nat) : ToString (Fin n) :=
⟨fun f => toString (Fin.val f)⟩
instToStringUInt8
instance
: ToString UInt8
instance : ToString UInt8 :=
⟨fun n => toString n.toNat⟩
instToStringUInt16
instance
: ToString UInt16
instance : ToString UInt16 :=
⟨fun n => toString n.toNat⟩
instToStringUInt32
instance
: ToString UInt32
instance : ToString UInt32 :=
⟨fun n => toString n.toNat⟩
instToStringUInt64
instance
: ToString UInt64
instance : ToString UInt64 :=
⟨fun n => toString n.toNat⟩
instToStringUSize
instance
: ToString USize
instance : ToString USize :=
⟨fun n => toString n.toNat⟩
instToStringFormat
instance
: ToString Format
addParenHeuristic
definition
(s : String) : String
instToStringOption
instance
{α : Type u} [ToString α] : ToString (Option α)
instance {α : Type u} [ToString α] : ToString (Option α) := ⟨fun
| none => "none"
| (some a) => "(some " ++ addParenHeuristic (toString a) ++ ")"⟩
instToStringSum
instance
{α : Type u} {β : Type v} [ToString α] [ToString β] : ToString (Sum α β)
instToStringProd
instance
{α : Type u} {β : Type v} [ToString α] [ToString β] : ToString (α × β)
instToStringSigma
instance
{α : Type u} {β : α → Type v} [ToString α] [∀ x, ToString (β x)] : ToString (Sigma β)
instToStringSubtype
instance
{α : Type u} {p : α → Prop} [ToString α] : ToString (Subtype p)
instance {α : Type u} {p : α → Prop} [ToString α] : ToString (Subtype p) := ⟨fun s =>
toString (val s)⟩
String.toInt?
definition
(s : String) : Option Int
/--
Interprets a string as the decimal representation of an integer, returning it. Returns `none` if the
string does not contain a decimal integer.
A string can be interpreted as a decimal integer if it is not empty, its first character is either
`'-'` or a digit, and all remaining characters are digits.
Use `String.isInt` to check whether `String.toInt?` would return `some`. `String.toInt!` is an
alternative that panics instead of returning `none` when the string is not an integer.
Examples:
* `"".toInt? = none`
* `"0".toInt? = some 0`
* `"5".toInt? = some 5`
* `"-5".toInt? = some (-5)`
* `"587".toInt? = some 587`
* `"-587".toInt? = some (-587)`
* `" 5".toInt? = none`
* `"2-3".toInt? = none`
* `"0xff".toInt? = none`
-/
def String.toInt? (s : String) : Option Int := do
if s.get 0 = '-' then do
let v ← (s.toSubstring.drop 1).toNat?;
pure <| - Int.ofNat v
else
Int.ofNat <$> s.toNat?
String.isInt
definition
(s : String) : Bool
/--
Checks whether the string can be interpreted as the decimal representation of an integer.
A string can be interpreted as a decimal integer if it is not empty, its first character is
`'-'` or a digit, and all subsequent characters are digits. Leading `+` characters are not allowed.
Use `String.toInt?` or `String.toInt!` to convert such a string to an integer.
Examples:
* `"".isInt = false`
* `"0".isInt = true`
* `"-0".isInt = true`
* `"5".isInt = true`
* `"587".isInt = true`
* `"-587".isInt = true`
* `"+587".isInt = false`
* `" 5".isInt = false`
* `"2-3".isInt = false`
* `"0xff".isInt = false`
-/
def String.isInt (s : String) : Bool :=
if s.get 0 = '-' then
(s.toSubstring.drop 1).isNat
else
s.isNat
String.toInt!
definition
(s : String) : Int
/--
Interprets a string as the decimal representation of an integer, returning it. Panics if the string
does not contain a decimal integer.
A string can be interpreted as a decimal integer if it is not empty, its first character is `'-'` or
a digit, and all remaining characters are digits.
Use `String.isInt` to check whether `String.toInt!` would return a value. `String.toInt?` is a safer
alternative that returns `none` instead of panicking when the string is not an integer.
Examples:
* `"0".toInt! = 0`
* `"5".toInt! = 5`
* `"587".toInt! = 587`
* `"-587".toInt! = -587`
-/
def String.toInt! (s : String) : Int :=
match s.toInt? with
| somesome v => v
| none => panic "Int expected"
instToStringExcept
instance
[ToString ε] [ToString α] : ToString (Except ε α)
instReprExcept
instance
[Repr ε] [Repr α] : Repr (Except ε α)