doc-next-gen

Init.Data.Repr

Module docstring

{}

Repr structure
(α : Type u)
Full source
/--
A typeclass that specifies the standard way of turning values of some type into `Format`.

When rendered this `Format` should be as close as possible to something that can be parsed as the
input value.
-/
class Repr (α : Type u) where
  /--
  Turn a value of type `α` into `Format` at a given precedence. The precedence value can be used
  to avoid parentheses if they are not necessary.
  -/
  reprPrec : α → NatFormat
Value Representation Format
Informal description
A structure that specifies the standard way of converting values of a type `α` into a `Format` representation, which should closely resemble a parsable input form of the value.
repr abbrev
[Repr α] (a : α) : Format
Full source
/--
Turn `a` into `Format` using its `Repr` instance. The precedence level is initially set to 0.
-/
abbrev repr [Repr α] (a : α) : Format :=
  reprPrec a 0
Format Representation of a Value
Informal description
Given a type $\alpha$ with a representation format `[Repr α]`, the function `repr` takes an element $a : \alpha$ and returns its formatted representation as a `Format` object.
reprStr abbrev
[Repr α] (a : α) : String
Full source
abbrev reprStr [Repr α] (a : α) : String :=
  reprPrec a 0 |>.pretty
String Representation of a Value
Informal description
Given a type $\alpha$ with a representation format `[Repr α]`, the function `reprStr` takes an element $a : \alpha$ and returns its string representation.
reprArg abbrev
[Repr α] (a : α) : Format
Full source
abbrev reprArg [Repr α] (a : α) : Format :=
  reprPrec a max_prec
Format Representation of an Argument
Informal description
Given a type $\alpha$ with a representation format `[Repr α]`, the function `reprArg` takes an element $a : \alpha$ and returns its formatted representation as a `Format` object.
ReprAtom structure
(α : Type u) -- This instance is needed because `id` is not reducible
Full source
/-- Auxiliary class for marking types that should be considered atomic by `Repr` methods.
   We use it at `Repr (List α)` to decide whether `bracketFill` should be used or not. -/
class ReprAtom (α : Type u)
Atomic type marker for representation methods
Informal description
A structure used to mark types that should be treated as atomic by representation methods. This is used, for example, in `Repr (List α)` to determine whether `bracketFill` should be applied or not.
instReprId instance
[Repr α] : Repr (id α)
Full source
instance [Repr α] : Repr (id α) :=
  inferInstanceAs (Repr α)
Representation Format for Identity Function
Informal description
For any type $\alpha$ with a representation format, the identity function $\mathrm{id} \alpha$ also has a representation format.
instReprId_1 instance
[Repr α] : Repr (Id α)
Full source
instance [Repr α] : Repr (Id α) :=
  inferInstanceAs (Repr α)
Representation Format for Identity Types
Informal description
For any type $\alpha$ with a representation format, the identity type $\mathrm{Id}\,\alpha$ also has a representation format.
instReprEmpty instance
: Repr Empty
Full source
instance : Repr Empty where
  reprPrec := nofun
Representation Format for the Empty Type
Informal description
The empty type `Empty` has a standard representation format.
instReprBool instance
: Repr Bool
Full source
instance : Repr Bool where
  reprPrec
    | true, _  => "true"
    | false, _ => "false"
Representation Format for Boolean Values
Informal description
The Boolean type `Bool` has a standard representation format for its values `true` and `false`.
Repr.addAppParen definition
(f : Format) (prec : Nat) : Format
Full source
def Repr.addAppParen (f : Format) (prec : Nat) : Format :=
  if prec >= max_prec then
    Format.paren f
  else
    f
Conditional Parentheses for Formatting Based on Precedence
Informal description
The function `Repr.addAppParen` takes a format object `f` and a natural number `prec` (representing precedence), and returns `f` enclosed in parentheses if `prec` is greater than or equal to `max_prec`, otherwise it returns `f` unchanged. This is used to control the formatting of expressions based on their precedence levels to ensure proper parsing and readability.
instReprDecidable instance
: Repr (Decidable p)
Full source
instance : Repr (Decidable p) where
  reprPrec
    | Decidable.isTrue _, prec  => Repr.addAppParen "isTrue _" prec
    | Decidable.isFalse _, prec => Repr.addAppParen "isFalse _" prec
Standard Representation of Decidable Propositions
Informal description
For any proposition `p`, there is a standard representation format for the type `Decidable p` that displays its values (either `isTrue` or `isFalse`) in a parsable form.
instReprPUnit instance
: Repr PUnit.{u + 1}
Full source
instance : Repr PUnitPUnit.{u+1} where
  reprPrec _ _ := "PUnit.unit"
Standard Representation of the Universe-Polymorphic Unit Type
Informal description
The universe-polymorphic unit type `PUnit` has a standard representation format that displays its single element in a parsable form.
instReprULift instance
[Repr α] : Repr (ULift.{v} α)
Full source
instance [Repr α] : Repr (ULift.{v} α) where
  reprPrec v prec :=
    Repr.addAppParen ("ULift.up " ++ reprArg v.1) prec
Representation of Universe-Lifted Types
Informal description
For any type $\alpha$ with a representation format, the universe-lifted type $\mathrm{ULift}\,\alpha$ also has a representation format, where the representation of a lifted value is the same as the representation of the original value.
instReprUnit instance
: Repr Unit
Full source
instance : Repr Unit where
  reprPrec _ _ := "()"
Standard Representation of the Unit Type
Informal description
The type `Unit` has a standard representation format that displays its single element `()` in a parsable form.
Option.repr definition
[Repr α] : Option α → Nat → Format
Full source
/--
Returns a representation of an optional value that should be able to be parsed as an equivalent
optional value.

This function is typically accessed through the `Repr (Option α)` instance.
-/
protected def Option.repr [Repr α] : Option α → NatFormat
  | none,    _   => "none"
  | some a, prec => Repr.addAppParen ("some " ++ reprArg a) prec
Representation of Optional Values
Informal description
Given a type $\alpha$ with a representation format `[Repr α]`, the function `Option.repr` takes an optional value of type `Option α` and a precedence level `prec`, and returns a formatted representation of the optional value. Specifically: - If the optional value is `none`, it returns the string `"none"`. - If the optional value is `some a`, it returns the string `"some "` followed by the formatted representation of `a`, enclosed in parentheses if the precedence level `prec` is sufficiently high. This function ensures that the representation can be parsed back into an equivalent optional value.
instReprOption instance
[Repr α] : Repr (Option α)
Full source
instance [Repr α] : Repr (Option α) where
  reprPrec := Option.repr
Representation Format for Optional Values
Informal description
For any type $\alpha$ with a representation format, the type `Option α` of optional values also has a representation format. The representation of `none` is the string `"none"`, and the representation of `some a` is the string `"some "` followed by the representation of `a`.
Sum.repr definition
[Repr α] [Repr β] : Sum α β → Nat → Format
Full source
protected def Sum.repr [Repr α] [Repr β] : Sum α β → NatFormat
  | Sum.inl a, prec => Repr.addAppParen ("Sum.inl " ++ reprArg a) prec
  | Sum.inr b, prec => Repr.addAppParen ("Sum.inr " ++ reprArg b) prec
Format representation of a sum type element
Informal description
The function `Sum.repr` takes a disjoint union (sum type) element of $\alpha \oplus \beta$ and a precedence level `prec`, and returns its formatted representation as a `Format` object. If the element is `inl a`, it formats as `"Sum.inl "` followed by the formatted representation of `a`, and if the element is `inr b`, it formats as `"Sum.inr "` followed by the formatted representation of `b`. The result is enclosed in parentheses if the precedence level `prec` is sufficiently high.
instReprSum instance
[Repr α] [Repr β] : Repr (Sum α β)
Full source
instance [Repr α] [Repr β] : Repr (Sum α β) where
  reprPrec := Sum.repr
Representation Format for Sum Types
Informal description
For any types $\alpha$ and $\beta$ with representation formats, the sum type $\alpha \oplus \beta$ also has a representation format. The representation of an element `inl a` is formatted as `"Sum.inl "` followed by the representation of `a`, and the representation of an element `inr b` is formatted as `"Sum.inr "` followed by the representation of `b`.
ReprTuple structure
(α : Type u)
Full source
class ReprTuple (α : Type u) where
  reprTuple : α → List FormatList Format
Tuple representation formatter
Informal description
The structure `ReprTuple` represents a type `α` that can be formatted as a tuple for pretty-printing purposes. It is used to define how elements of type `α` should be displayed when printed, particularly in the context of nested structures like products or sums.
instReprTupleOfRepr instance
[Repr α] : ReprTuple α
Full source
instance [Repr α] : ReprTuple α where
  reprTuple a xs := reprrepr a :: xs
Representation Format Implies Tuple Representation Format
Informal description
For any type $\alpha$ with a representation format `[Repr α]`, there exists a tuple representation format for $\alpha$.
instReprTupleProdOfRepr instance
[Repr α] [ReprTuple β] : ReprTuple (α × β)
Full source
instance [Repr α] [ReprTuple β] : ReprTuple (α × β) where
  reprTuple | (a, b), xs => reprTuple b (repr a :: xs)
Tuple Representation for Product Types
Informal description
For any types $\alpha$ and $\beta$ where $\alpha$ has a representation format and $\beta$ can be formatted as a tuple, the product type $\alpha \times \beta$ can also be formatted as a tuple.
Prod.repr definition
[Repr α] [ReprTuple β] : α × β → Nat → Format
Full source
protected def Prod.repr [Repr α] [ReprTuple β] : α × βNatFormat
  | (a, b), _ => Format.bracket "(" (Format.joinSep (reprTuple b [repr a]).reverse ("," ++ Format.line)) ")"
Tuple representation formatter for ordered pairs
Informal description
Given types $\alpha$ and $\beta$ with representation formats `[Repr α]` and `[ReprTuple β]`, the function `Prod.repr` formats an ordered pair $(a, b) \in \alpha \times \beta$ as a string representation. The representation is constructed by first formatting the first component $a$ using its `repr` function, then formatting the second component $b$ using its `reprTuple` function, and combining them into a parenthesized tuple separated by a comma and a line break.
instReprProdOfReprTuple instance
[Repr α] [ReprTuple β] : Repr (α × β)
Full source
instance [Repr α] [ReprTuple β] : Repr (α × β) where
  reprPrec := Prod.repr
Representation Format for Product Types
Informal description
For any types $\alpha$ and $\beta$ where $\alpha$ has a standard representation format and $\beta$ can be formatted as a tuple, the product type $\alpha \times \beta$ has a canonical representation format.
instReprSigma instance
{β : α → Type v} [Repr α] [(x : α) → Repr (β x)] : Repr (Sigma β)
Full source
instance {β : α → Type v} [Repr α] [(x : α) → Repr (β x)] : Repr (Sigma β) where
  reprPrec | ⟨a, b⟩, _ => Format.bracket "⟨" (repr a ++ ", " ++ repr b) "⟩"
Representation Format for Dependent Sum Types
Informal description
For any type $\alpha$ with a representation format `[Repr α]` and a type family $\beta : \alpha \to \text{Type}$ where each $\beta x$ has a representation format `[Repr (β x)]`, the dependent sum type $\Sigma a : \alpha, \beta a$ has a canonical representation format.
instReprSubtype instance
{p : α → Prop} [Repr α] : Repr (Subtype p)
Full source
instance {p : α → Prop} [Repr α] : Repr (Subtype p) where
  reprPrec s prec := reprPrec s.val prec
Representation of Subtypes
Informal description
For any type $\alpha$ with a predicate $p : \alpha \to \text{Prop}$ and a representation instance for $\alpha$, there is a representation instance for the subtype $\{x : \alpha \mid p x\}$. This representation displays elements of the subtype in the same format as their underlying elements in $\alpha$.
Nat.digitChar definition
(n : Nat) : Char
Full source
/--
Returns a single digit representation of `n`, which is assumed to be in a base less than or equal to
`16`. Returns `'*'` if `n > 15`.

Examples:
 * `Nat.digitChar 5 = '5'`
 * `Nat.digitChar 12 = 'c'`
 * `Nat.digitChar 15 = 'f'`
 * `Nat.digitChar 16 = '*'`
 * `Nat.digitChar 85 = '*'`
-/
def digitChar (n : Nat) : Char :=
  if n = 0 then '0' else
  if n = 1 then '1' else
  if n = 2 then '2' else
  if n = 3 then '3' else
  if n = 4 then '4' else
  if n = 5 then '5' else
  if n = 6 then '6' else
  if n = 7 then '7' else
  if n = 8 then '8' else
  if n = 9 then '9' else
  if n = 0xa then 'a' else
  if n = 0xb then 'b' else
  if n = 0xc then 'c' else
  if n = 0xd then 'd' else
  if n = 0xe then 'e' else
  if n = 0xf then 'f' else
  '*'
Single-digit character representation of a natural number (base ≤ 16)
Informal description
The function converts a natural number \( n \) (assumed to be in a base ≤ 16) to its corresponding single-digit character representation. For \( 0 \leq n \leq 9 \), it returns the corresponding digit character. For \( 10 \leq n \leq 15 \), it returns the lowercase letters 'a' through 'f'. For \( n > 15 \), it returns the asterisk character '*'.
Nat.toDigitsCore definition
(base : Nat) : Nat → Nat → List Char → List Char
Full source
def toDigitsCore (base : Nat) : NatNatList CharList Char
  | 0,      _, ds => ds
  | fuel+1, n, ds =>
    let d  := digitChar <| n % base;
    let n' := n / base;
    if n' = 0 then d::ds
    else toDigitsCore base fuel n' (d::ds)
Core digit conversion function for natural numbers
Informal description
The function `Nat.toDigitsCore` converts a natural number `n` into a list of characters representing its digits in a given base. It takes three arguments: 1. `base`: The base for the digit representation (must be ≤ 16) 2. `n`: The natural number to convert 3. `ds`: An accumulator list of characters for the digits processed so far The function works recursively: 1. If the fuel (recursion counter) reaches 0, it returns the accumulated digits 2. Otherwise, it: - Computes the current digit using `n % base` - Converts it to a character using `digitChar` - Computes the remaining number `n' = n / base` - If `n'` is zero, prepends the digit to the accumulator - Otherwise, continues recursively with the remaining number and prepended digit
Nat.toDigits definition
(base : Nat) (n : Nat) : List Char
Full source
/--
Returns the decimal representation of a natural number as a list of digit characters in the given
base. If the base is greater than `16` then `'*'` is returned for digits greater than `0xf`.

Examples:
* `Nat.toDigits 10 0xff = ['2', '5', '5']`
* `Nat.toDigits 8 0xc = ['1', '4']`
* `Nat.toDigits 16 0xcafe = ['c', 'a', 'f', 'e']`
* `Nat.toDigits 80 200 = ['2', '*']`
-/
def toDigits (base : Nat) (n : Nat) : List Char :=
  toDigitsCore base (n+1) n []
Natural number to digit list conversion
Informal description
The function converts a natural number \( n \) into a list of characters representing its digits in the given base. For bases greater than 16, digits beyond 15 are represented by the character `'*'`. The function uses an auxiliary function `toDigitsCore` with an initial accumulator of an empty list. **Examples:** - For base 10 and \( n = 255 \), returns `['2', '5', '5']` - For base 8 and \( n = 12 \), returns `['1', '4']` - For base 16 and \( n = 51966 \), returns `['c', 'a', 'f', 'e']` - For base 80 and \( n = 200 \), returns `['2', '*']`
USize.repr definition
(n : @& USize) : String
Full source
/--
Converts a word-sized unsigned integer into a decimal string.

This function is overridden at runtime with an efficient implementation.

Examples:
 * `USize.repr 0 = "0"`
 * `USize.repr 28 = "28"`
 * `USize.repr 307 = "307"`
-/
@[extern "lean_string_of_usize"]
protected def _root_.USize.repr (n : @& USize) : String :=
  (toDigits 10 n.toNat).asString
Decimal string representation of a word-sized unsigned integer
Informal description
The function converts a word-sized unsigned integer $n$ into its decimal string representation by first converting the natural number value of $n$ to a list of digit characters (using base 10) and then converting this list to a string. **Examples:** - $\text{USize.repr}\ 0 = \texttt{"0"}$ - $\text{USize.repr}\ 28 = \texttt{"28"}$ - $\text{USize.repr}\ 307 = \texttt{"307"}$
Nat.repr definition
(n : Nat) : String
Full source
/--
Converts a natural number to its decimal string representation.
-/
@[implemented_by reprFast]
protected def repr (n : Nat) : String :=
  (toDigits 10 n).asString
Decimal string representation of a natural number
Informal description
The function converts a natural number \( n \) to its decimal string representation by first converting \( n \) to a list of digit characters (using base 10) and then converting this list to a string.
Nat.superDigitChar definition
(n : Nat) : Char
Full source
/--
Converts a natural number less than `10` to the corresponding Unicode superscript digit character.
Returns `'*'` for other numbers.

Examples:
* `Nat.superDigitChar 3 = '³'`
* `Nat.superDigitChar 7 = '⁷'`
* `Nat.superDigitChar 10 = '*'`
-/
def superDigitChar (n : Nat) : Char :=
  if n = 0 then '⁰' else
  if n = 1 then '¹' else
  if n = 2 then '²' else
  if n = 3 then '³' else
  if n = 4 then '⁴' else
  if n = 5 then '⁵' else
  if n = 6 then '⁶' else
  if n = 7 then '⁷' else
  if n = 8 then '⁸' else
  if n = 9 then '⁹' else
  '*'
Superscript digit character from natural number
Informal description
The function converts a natural number \( n \) less than 10 to its corresponding Unicode superscript digit character. For numbers 0 through 9, it returns the characters '⁰' to '⁹' respectively. For any other natural number, it returns the asterisk character '*'. Examples: - \( \text{Nat.superDigitChar}\ 3 = \text{`³'} \) - \( \text{Nat.superDigitChar}\ 7 = \text{`⁷'} \) - \( \text{Nat.superDigitChar}\ 10 = \text{`*'} \)
Nat.toSuperDigitsAux definition
: Nat → List Char → List Char
Full source
partial def toSuperDigitsAux : NatList CharList Char
  | n, ds =>
    let d  := superDigitChar <| n % 10;
    let n' := n / 10;
    if n' = 0 then d::ds
    else toSuperDigitsAux n' (d::ds)
Auxiliary function for converting natural numbers to superscript digits
Informal description
The auxiliary function `Nat.toSuperDigitsAux` takes a natural number `n` and a list of characters `ds`, and recursively converts `n` into a list of superscript digit characters. At each step, it takes the last digit of `n` (using modulo 10), converts it to a superscript character, and prepends it to the list `ds`. The process continues with the remaining digits of `n` (obtained by integer division by 10) until all digits are processed. More precisely: 1. For a given `n` and `ds`, compute `d` as the superscript character corresponding to `n % 10`. 2. Compute `n'` as `n / 10`. 3. If `n'` is zero, return the list `d::ds`. 4. Otherwise, recursively process `n'` with `d::ds` as the new accumulator.
Nat.toSuperDigits definition
(n : Nat) : List Char
Full source
/--
Converts a natural number to the list of Unicode superscript digit characters that corresponds to
its decimal representation.

Examples:
 * `Nat.toSuperDigits 0 = ['⁰']`
 * `Nat.toSuperDigits 35 = ['³', '⁵']`
-/
def toSuperDigits (n : Nat) : List Char :=
  toSuperDigitsAux n []
Conversion of natural numbers to superscript digits
Informal description
The function converts a natural number $n$ to a list of Unicode superscript digit characters corresponding to its decimal representation. For example: - $0$ is converted to `['⁰']` - $35$ is converted to `['³', '⁵']`
Nat.toSuperscriptString definition
(n : Nat) : String
Full source
/--
Converts a natural number to a string that contains the its decimal representation as Unicode
superscript digit characters.

Examples:
 * `Nat.toSuperscriptString 0 = "⁰"`
 * `Nat.toSuperscriptString 35 = "³⁵"`
-/
def toSuperscriptString (n : Nat) : String :=
  (toSuperDigits n).asString
Natural number to superscript string conversion
Informal description
The function converts a natural number $n$ to a string where each digit of $n$ is represented as a Unicode superscript character. For example: - $0$ is converted to `"⁰"` - $35$ is converted to `"³⁵"`
Nat.subDigitChar definition
(n : Nat) : Char
Full source
/--
Converts a natural number less than `10` to the corresponding Unicode subscript digit character.
Returns `'*'` for other numbers.

Examples:
* `Nat.subDigitChar 3 = '₃'`
* `Nat.subDigitChar 7 = '₇'`
* `Nat.subDigitChar 10 = '*'`
-/
def subDigitChar (n : Nat) : Char :=
  if n = 0 then '₀' else
  if n = 1 then '₁' else
  if n = 2 then '₂' else
  if n = 3 then '₃' else
  if n = 4 then '₄' else
  if n = 5 then '₅' else
  if n = 6 then '₆' else
  if n = 7 then '₇' else
  if n = 8 then '₈' else
  if n = 9 then '₉' else
  '*'
Subscript digit character from natural number
Informal description
The function converts a natural number \( n \) less than 10 to its corresponding Unicode subscript digit character. Specifically: - \( 0 \) maps to `'₀'` - \( 1 \) maps to `'₁'` - \( 2 \) maps to `'₂'` - \( 3 \) maps to `'₃'` - \( 4 \) maps to `'₄'` - \( 5 \) maps to `'₅'` - \( 6 \) maps to `'₆'` - \( 7 \) maps to `'₇'` - \( 8 \) maps to `'₈'` - \( 9 \) maps to `'₉'` For any other natural number \( n \), the function returns the asterisk character `'*'`.
Nat.toSubDigitsAux definition
: Nat → List Char → List Char
Full source
partial def toSubDigitsAux : NatList CharList Char
  | n, ds =>
    let d  := subDigitChar <| n % 10;
    let n' := n / 10;
    if n' = 0 then d::ds
    else toSubDigitsAux n' (d::ds)
Auxiliary function for converting natural numbers to subscript digits
Informal description
The auxiliary function `Nat.toSubDigitsAux` takes a natural number `n` and a list of characters `ds`, and returns a list of characters representing the digits of `n` in subscript notation, appended to `ds`. Specifically, it processes `n` digit by digit from least significant to most significant, converting each digit to its corresponding subscript character and prepending it to the accumulating list `ds`. The recursion stops when the remaining part of `n` becomes zero.
Nat.toSubDigits definition
(n : Nat) : List Char
Full source
/--
Converts a natural number to the list of Unicode subscript digit characters that corresponds to
its decimal representation.

Examples:
 * `Nat.toSubDigits 0 = ['₀']`
 * `Nat.toSubDigits 35 = ['₃', '₅']`
-/
def toSubDigits (n : Nat) : List Char :=
  toSubDigitsAux n []
Natural number to subscript digits conversion
Informal description
The function converts a natural number $n$ to a list of Unicode subscript digit characters corresponding to its decimal representation. For example, $0$ is converted to `['₀']` and $35$ is converted to `['₃', '₅']`.
Nat.toSubscriptString definition
(n : Nat) : String
Full source
/--
Converts a natural number to a string that contains the its decimal representation as Unicode
subscript digit characters.

Examples:
 * `Nat.toSubscriptString 0 = "₀"`
 * `Nat.toSubscriptString 35 = "₃₅"`
-/
def toSubscriptString (n : Nat) : String :=
  (toSubDigits n).asString
Natural number to subscript string conversion
Informal description
The function converts a natural number $n$ to a string containing its decimal representation using Unicode subscript digit characters. For example, $0$ is converted to `"₀"` and $35$ is converted to `"₃₅"`.
instReprNat instance
: Repr Nat
Full source
instance : Repr Nat where
  reprPrec n _ := Nat.repr n
Decimal Representation of Natural Numbers
Informal description
The natural numbers $\mathbb{N}$ have a standard string representation in decimal format.
Int.repr definition
: Int → String
Full source
/--
Returns the decimal string representation of an integer.
-/
protected def Int.repr : IntString
    | ofNat m   => Nat.repr m
    | negSucc m => "-" ++ Nat.repr (succ m)
Decimal string representation of an integer
Informal description
The function converts an integer to its decimal string representation. For non-negative integers, it returns the same string as `Nat.repr`. For negative integers, it prepends a minus sign to the string representation of the absolute value.
hexDigitRepr definition
(n : Nat) : String
Full source
def hexDigitRepr (n : Nat) : String :=
  String.singleton <| Nat.digitChar n
Single-digit hexadecimal string representation of a natural number
Informal description
The function converts a natural number \( n \) to its single-digit hexadecimal string representation. For \( 0 \leq n \leq 15 \), it returns the corresponding hexadecimal digit character (0-9, a-f). For \( n > 15 \), it returns the asterisk character '*'.
Char.quoteCore definition
(c : Char) : String
Full source
def Char.quoteCore (c : Char) : String :=
  if       c = '\n' then "\\n"
  else if  c = '\t' then "\\t"
  else if  c = '\\' then "\\\\"
  else if  c = '\"' then "\\\""
  else if  c.toNat <= 31 ∨ c = '\x7f' then "\\x" ++ smallCharToHex c
  else String.singleton c
where
  smallCharToHex (c : Char) : String :=
    let n  := Char.toNat c;
    let d2 := n / 16;
    let d1 := n % 16;
    hexDigitRepr d2 ++ hexDigitRepr d1
Escaped string representation of a Unicode character
Informal description
The function converts a Unicode character \( c \) into its escaped string representation. It handles special characters as follows: - Newline `'\n'` becomes `"\\n"` - Tab `'\t'` becomes `"\\t"` - Backslash `'\\'` becomes `"\\\\"` - Double quote `'\"'` becomes `"\\\""` - Control characters (Unicode code point ≤ 31 or equal to `'\x7f'`) are converted to hexadecimal escape sequences `"\\x"` followed by their two-digit hexadecimal representation - All other characters are represented as themselves in a singleton string.
Char.quote definition
(c : Char) : String
Full source
/--
Quotes the character to its representation as a character literal, surrounded by single quotes and
escaped as necessary.

Examples:
 * `'L'.quote = "'L'"`
 * `'"'.quote = "'\\\"'"`
-/
def Char.quote (c : Char) : String :=
  "'" ++ Char.quoteCore c ++ "'"
Character literal quotation
Informal description
The function converts a Unicode character \( c \) into its string representation as a character literal, surrounded by single quotes and with necessary escape sequences. For example: - The character `'L'` becomes `"'L'"` - The double quote character `'"'` becomes `"'\\\"'"`
instReprChar instance
: Repr Char
Full source
instance : Repr Char where
  reprPrec c _ := c.quote
Standard Representation of Unicode Characters as Lean Literals
Informal description
Every Unicode character has a standard representation as a Lean character literal, where special characters are properly escaped and the character is enclosed in single quotes.
Char.repr definition
(c : Char) : String
Full source
protected def Char.repr (c : Char) : String :=
  c.quote
Character literal representation
Informal description
The function converts a Unicode character \( c \) into its string representation as a character literal, surrounded by single quotes and with necessary escape sequences. For example: - The character `'L'` becomes `"'L'"` - The double quote character `'"'` becomes `"'\\\"'"`
String.quote definition
(s : String) : String
Full source
/--
Converts a string to its corresponding Lean string literal syntax. Double quotes are added to each
end, and internal characters are escaped as needed.

Examples:
* `"abc".quote = "\"abc\""`
* `"\"".quote = "\"\\\"\""`
-/
def String.quote (s : String) : String :=
  if s.isEmpty then "\"\""
  else s.foldl (fun s c => s ++ c.quoteCore) "\"" ++ "\""
String literal conversion
Informal description
The function converts a string $s$ into its Lean string literal syntax by adding double quotes at both ends and properly escaping any internal characters. Specifically: - If $s$ is empty, it returns `""""` (two double quotes) - Otherwise, it prepends a double quote, then processes each character by escaping it appropriately (using `Char.quoteCore`), and finally appends another double quote
instReprString instance
: Repr String
Full source
instance : Repr String where
  reprPrec s _ := s.quote
String Representation Format
Informal description
The type `String` has a standard representation format that converts strings into a format resembling their Lean string literal syntax, including proper escaping of special characters and surrounding double quotes.
instReprPos instance
: Repr String.Pos
Full source
instance : Repr String.Pos where
  reprPrec p _ := "{ byteIdx := " ++ repr p.byteIdx ++ " }"
Representation of String Positions
Informal description
The type `String.Pos`, representing byte positions in a UTF-8 encoded string, has a standard representation format.
instReprSubstring instance
: Repr Substring
Full source
instance : Repr Substring where
  reprPrec s _ := Format.text <| String.quote s.toString ++ ".toSubstring"
Standard Representation for Substrings
Informal description
The type `Substring` has a standard representation format that converts its values into a parsable string format.
instReprIterator instance
: Repr String.Iterator
Full source
instance : Repr String.Iterator where
  reprPrec | ⟨s, pos⟩, prec => Repr.addAppParen ("String.Iterator.mk " ++ reprArg s ++ " " ++ reprArg pos) prec
Standard Representation for String Iterators
Informal description
The type `String.Iterator`, which represents an iterator over a string, has a standard representation format that converts its values into a parsable string format.
instReprFin instance
(n : Nat) : Repr (Fin n)
Full source
instance (n : Nat) : Repr (Fin n) where
  reprPrec f _ := repr f.val
Representation Format for Finite Types
Informal description
For any natural number $n$, the finite type $\mathrm{Fin}\,n$ has a standard representation format that displays its elements as natural numbers less than $n$.
instReprUInt8 instance
: Repr UInt8
Full source
instance : Repr UInt8 where
  reprPrec n _ := repr n.toNat
Representation of Unsigned 8-bit Integers
Informal description
The type of unsigned 8-bit integers has a standard representation format.
instReprUInt16 instance
: Repr UInt16
Full source
instance : Repr UInt16 where
  reprPrec n _ := repr n.toNat
Decimal Representation of Unsigned 16-bit Integers
Informal description
The unsigned 16-bit integers have a standard string representation.
instReprUInt32 instance
: Repr UInt32
Full source
instance : Repr UInt32 where
  reprPrec n _ := repr n.toNat
Decimal Representation of 32-bit Unsigned Integers
Informal description
The unsigned 32-bit integers have a standard string representation in decimal format.
instReprUInt64 instance
: Repr UInt64
Full source
instance : Repr UInt64 where
  reprPrec n _ := repr n.toNat
Decimal Representation of Unsigned 64-bit Integers
Informal description
The unsigned 64-bit integers have a standard string representation.
instReprUSize instance
: Repr USize
Full source
instance : Repr USize where
  reprPrec n _ := repr n.toNat
Representation Format for Platform-Dependent Unsigned Integers
Informal description
The type `USize` of platform-dependent unsigned word-size integers has a standard string representation format.
List.repr definition
[Repr α] (a : List α) (n : Nat) : Format
Full source
protected def List.repr [Repr α] (a : List α) (n : Nat) : Format :=
  let _ : ToFormat α := ⟨repr⟩
  match a, n with
  | [], _ => "[]"
  | as, _ => Format.bracket "[" (Format.joinSep as ("," ++ Format.line)) "]"
List representation formatter
Informal description
The function `List.repr` takes a list `a` of elements of type `α` (where `α` has a representation format `[Repr α]`) and a natural number `n`, and returns a formatted string representation of the list. The representation is either `"[]"` for an empty list or a bracketed sequence of elements separated by commas and line breaks for non-empty lists.
instReprList instance
[Repr α] : Repr (List α)
Full source
instance [Repr α] : Repr (List α) where
  reprPrec := List.repr
Representation Format for Lists
Informal description
For any type $\alpha$ with a representation format, the type `List α` of lists of elements of type $\alpha$ also has a representation format. The representation of a list is either `"[]"` for an empty list or a bracketed sequence of comma-separated elements for non-empty lists.
List.repr' definition
[Repr α] [ReprAtom α] (a : List α) (n : Nat) : Format
Full source
protected def List.repr' [Repr α] [ReprAtom α] (a : List α) (n : Nat) : Format :=
  let _ : ToFormat α := ⟨repr⟩
  match a, n with
  | [], _ => "[]"
  | as, _ => Format.bracketFill "[" (Format.joinSep as ("," ++ Format.line)) "]"
Formatted representation of atomic lists
Informal description
Given a type $\alpha$ with a representation format `[Repr α]` and marked as atomic `[ReprAtom α]`, the function `List.repr'` takes a list $a : \text{List } \alpha$ and a natural number $n$ (representing the nesting level), and returns a formatted string representation of the list. Empty lists are represented as `"[]"`, while non-empty lists are represented with square brackets enclosing the comma-separated elements, with proper line breaks for nested structures.
instReprListOfReprAtom instance
[Repr α] [ReprAtom α] : Repr (List α)
Full source
instance [Repr α] [ReprAtom α] : Repr (List α) where
  reprPrec := List.repr'
Representation Format for Lists of Atomic Types
Informal description
For any type $\alpha$ with a representation format and marked as atomic, the list type $\text{List } \alpha$ also has a representation format where lists are displayed in square brackets with comma-separated elements.
instReprAtomBool instance
: ReprAtom Bool
Full source
instance : ReprAtom Bool   := ⟨⟩
Boolean Type as Atomic Representation Type
Informal description
The Boolean type `Bool` is marked as an atomic type for representation methods.
instReprAtomNat instance
: ReprAtom Nat
Full source
instance : ReprAtom Nat    := ⟨⟩
Natural Numbers as Atomic Representation Type
Informal description
The type of natural numbers is marked as an atomic type for representation methods.
instReprAtomInt instance
: ReprAtom Int
Full source
instance : ReprAtom Int    := ⟨⟩
Integers as Atomic Representation Type
Informal description
The type of integers is marked as an atomic type for representation methods, meaning it should be treated as a single unit when generating string representations.
instReprAtomChar instance
: ReprAtom Char
Full source
instance : ReprAtom Char   := ⟨⟩
Unicode Characters as Atomic Representation Type
Informal description
The type of Unicode characters is marked as an atomic type for representation methods.
instReprAtomString instance
: ReprAtom String
Full source
instance : ReprAtom String := ⟨⟩
String as Atomic Representation Type
Informal description
The type `String` is marked as an atomic type for representation methods, meaning it should be treated as a single unit when generating string representations.
instReprAtomUInt8 instance
: ReprAtom UInt8
Full source
instance : ReprAtom UInt8  := ⟨⟩
Atomic Representation Marker for 8-bit Unsigned Integers
Informal description
The type `UInt8` of unsigned 8-bit integers is marked as an atomic type for representation methods.
instReprAtomUInt16 instance
: ReprAtom UInt16
Full source
instance : ReprAtom UInt16 := ⟨⟩
Atomic Representation Marker for 16-bit Unsigned Integers
Informal description
The type `UInt16` of unsigned 16-bit integers is marked as an atomic type for representation methods.
instReprAtomUInt32 instance
: ReprAtom UInt32
Full source
instance : ReprAtom UInt32 := ⟨⟩
Atomic Representation Marker for 32-bit Unsigned Integers
Informal description
The type `UInt32` of unsigned 32-bit integers is marked as an atomic type for representation methods.
instReprAtomUInt64 instance
: ReprAtom UInt64
Full source
instance : ReprAtom UInt64 := ⟨⟩
Atomic Representation Marker for 64-bit Unsigned Integers
Informal description
The type `UInt64` of unsigned 64-bit integers is marked as an atomic type for representation methods.
instReprAtomUSize instance
: ReprAtom USize
Full source
instance : ReprAtom USize  := ⟨⟩
Atomic Representation Marker for Platform-Dependent Unsigned Integers
Informal description
The type `USize` of platform-dependent unsigned integers is marked as an atomic type for representation methods.
instReprSourceInfo instance
: Repr✝ (@Lean.SourceInfo✝)
Full source
Repr for Lean.SourceInfo
Standard Representation for Source Information
Informal description
The type `Lean.SourceInfo` has a standard representation format for its values.