doc-next-gen

Init.Data.Format.Basic

Module docstring

{}

Std.Format.FlattenBehavior inductive
Full source
/-- Determines how groups should have linebreaks inserted when the
text would overfill its remaining space.

- `allOrNone` will make a linebreak on every `Format.line` in the group or none of them.
  ```
  [1,
   2,
   3]
  ```
- `fill` will only make linebreaks on as few `Format.line`s as possible:
  ```
  [1, 2,
   3]
  ```
-/
inductive Format.FlattenBehavior where
  | allOrNone
  | fill
  deriving Inhabited, BEq
Text Flattening Behavior for Line Breaks
Informal description
An inductive type that determines how line breaks should be inserted in a group of text when it would exceed the available space. It has two modes: 1. `allOrNone`: Either breaks on every line in the group or none of them, producing output like: ``` [1, 2, 3] ``` 2. `fill`: Only breaks on as few lines as necessary to fit the content, producing output like: ``` [1, 2, 3] ```
Std.Format.instInhabitedFlattenBehavior instance
: Inhabited✝ (@Std.Format.FlattenBehavior)
Full source
Inhabited, BEq
Default Flattening Behavior Exists
Informal description
The type `FlattenBehavior` is inhabited, meaning it has a default element.
Std.Format.instBEqFlattenBehavior instance
: BEq✝ (@Std.Format.FlattenBehavior✝)
Full source
BEq
Boolean Equality for Flattening Behavior
Informal description
The type `FlattenBehavior` has a boolean equality relation `==` that determines whether two flattening behaviors are considered equal.
Std.Format inductive
Full source
/-- A string with pretty-printing information for rendering in a column-width-aware way.

The pretty-printing algorithm is based on Wadler's paper
[_A Prettier Printer_](https://homepages.inf.ed.ac.uk/wadler/papers/prettier/prettier.pdf). -/
inductive Format where
  /-- The empty format. -/
  | nil                 : Format
  /-- A position where a newline may be inserted
  if the current group does not fit within the allotted column width. -/
  | line                : Format
  /-- `align` tells the formatter to pad with spaces to the current indent,
  or else add a newline if we are already at or past the indent. For example:
  ```
  nest 2 <| "." ++ align ++ "a" ++ line ++ "b"
  ```
  results in:
  ```
  . a
    b
  ```
  If `force` is true, then it will pad to the indent even if it is in a flattened group.
  -/
  | align (force : Bool) : Format
  /-- A node containing a plain string. -/
  | text                : String → Format
  /-- `nest n f` tells the formatter that `f` is nested inside something with length `n`
  so that it is pretty-printed with the correct indentation on a line break.
  For example, we can define a formatter for list `l : List Format` as:

  ```
  let f := join <| l.intersperse <| ", " ++ Format.line
  group (nest 1 <| "[" ++ f ++ "]")
  ```

  This will be written all on one line, but if the text is too large,
  the formatter will put in linebreaks after the commas and indent later lines by 1.
  -/
  | nest (indent : Int) : Format → Format
  /-- Concatenation of two Formats. -/
  | append              : Format → Format → Format
  /-- Creates a new flattening group for the given inner format.  -/
  | group               : Format → (behavior : FlattenBehavior := FlattenBehavior.allOrNone) → Format
  /-- Used for associating auxiliary information (e.g. `Expr`s) with `Format` objects. -/
  | tag                 : Nat → Format → Format
  deriving Inhabited
Pretty-printing format
Informal description
The inductive type `Std.Format` represents a string with pretty-printing information that can be rendered in a column-width-aware manner. The pretty-printing algorithm follows Wadler's approach described in "A Prettier Printer".
Std.instInhabitedFormat instance
: Inhabited✝ (@Std.Format)
Full source
Inhabited
Default Element for Pretty-Printing Format
Informal description
The type `Std.Format` has a default element.
Std.Format.isEmpty definition
: Format → Bool
Full source
/-- Check whether the given format contains no characters. -/
def isEmpty : FormatBool
  | nil          => true
  | line         => false
  | align _      => true
  | text msg     => msg == ""
  | nest _ f     => f.isEmpty
  | append f₁ f₂ => f₁.isEmpty && f₂.isEmpty
  | group f _    => f.isEmpty
  | tag _ f      => f.isEmpty
Check for empty pretty-printing format
Informal description
The function checks whether a pretty-printing format is empty. A format is considered empty if it is `nil`, an empty text, or recursively if all its components are empty. Specifically: - `nil` is empty - `line` is not empty - `align` is empty - `text` is empty if its message is the empty string - `nest` is empty if its nested format is empty - `append` is empty if both its components are empty - `group` is empty if its nested format is empty - `tag` is empty if its nested format is empty
Std.Format.fill definition
(f : Format) : Format
Full source
/-- Alias for a group with `FlattenBehavior` set to `fill`. -/
def fill (f : Format) : Format :=
  group f (behavior := FlattenBehavior.fill)
Fill-style grouped pretty-printing format
Informal description
The function `Std.Format.fill` takes a pretty-printing format `f` and returns a grouped version of `f` with the flattening behavior set to "fill". This affects how the pretty-printer will attempt to fit the content within available width constraints.
Std.Format.instAppend instance
: Append Format
Full source
instance : Append Format     := ⟨Format.append⟩
Append Operation for Pretty-Printing Formats
Informal description
The pretty-printing format type `Std.Format` has a canonical append operation `++` that combines two formats into one.
Std.Format.instCoeString instance
: Coe String Format
Full source
instance : Coe String Format := ⟨text⟩
String to Pretty-Printing Format Coercion
Informal description
There is a canonical way to convert a string into a pretty-printing format.
Std.Format.join definition
(xs : List Format) : Format
Full source
def join (xs : List Format) : Format :=
  xs.foldl (·++·) ""
Concatenation of pretty-printing formats
Informal description
The function `Std.Format.join` takes a list of pretty-printing formats and combines them into a single format by concatenating them from left to right using the append operation `++`, starting with the empty format `""`. In other words, given a list of formats `[f₁, f₂, ..., fₙ]`, `join [f₁, f₂, ..., fₙ]` returns the format `f₁ ++ f₂ ++ ... ++ fₙ`.
Std.Format.isNil definition
: Format → Bool
Full source
def isNil : FormatBool
  | nil => true
  | _   => false
Check for empty pretty-printing format
Informal description
The function `Std.Format.isNil` checks whether a given pretty-printing format is the empty format `nil`. It returns `true` if the input is `nil` and `false` otherwise.
Std.Format.instInhabitedSpaceResult instance
: Inhabited✝ (@_private.0.Std.Format.SpaceResult)
Full source
Inhabited
Inhabitedness of SpaceResult Type
Informal description
The type `_private.0.Std.Format.SpaceResult` is inhabited, meaning it has a designated default element.
Std.Format.MonadPrettyFormat structure
(m : Type → Type)
Full source
/-- A monad in which we can pretty-print `Format` objects. -/
class MonadPrettyFormat (m : Type → Type) where
  pushOutput (s : String)    : m Unit
  pushNewline (indent : Nat) : m Unit
  currColumn                 : m Nat
  /-- Start a scope tagged with `n`. -/
  startTag                   : Nat → m Unit
  /-- Exit the scope of `n`-many opened tags. -/
  endTags                    : Nat → m Unit
Pretty-printing monad for `Format` objects
Informal description
A monad structure that enables pretty-printing of `Format` objects within the monadic context.
Std.Format.bracket definition
(l : String) (f : Format) (r : String) : Format
Full source
/-- Create a format `l ++ f ++ r` with a flatten group.
FlattenBehaviour is `allOrNone`; for `fill` use `bracketFill`. -/
@[inline] def bracket (l : String) (f : Format) (r : String) : Format :=
  group (nest l.length $ l ++ f ++ r)
Bracketed format with nesting
Informal description
The function creates a pretty-printing format by surrounding a format `f` with left and right delimiters `l` and `r`, respectively, and grouping them together with a nesting level equal to the length of `l`. The resulting format is `l ++ f ++ r` with flatten behavior set to `allOrNone`.
Std.Format.paren definition
(f : Format) : Format
Full source
/-- Creates the format `"(" ++ f ++ ")"` with a flattening group.-/
@[inline] def paren (f : Format) : Format :=
  bracket "(" f ")"
Parenthesized pretty-printing format
Informal description
The function creates a pretty-printing format by surrounding a given format `f` with parentheses, i.e., it produces the format `"(" ++ f ++ ")"` with a flattening group.
Std.Format.sbracket definition
(f : Format) : Format
Full source
/-- Creates the format `"[" ++ f ++ "]"` with a flattening group.-/
@[inline] def sbracket (f : Format) : Format :=
  bracket "[" f "]"
Square-bracketed pretty-printing format
Informal description
The function creates a pretty-printing format by surrounding a given format `f` with square brackets, i.e., it produces the format `"[" ++ f ++ "]"` with a flattening group.
Std.Format.bracketFill definition
(l : String) (f : Format) (r : String) : Format
Full source
/-- Same as `bracket` except uses the `fill` flattening behaviour. -/
@[inline] def bracketFill (l : String) (f : Format) (r : String) : Format :=
  fill (nest l.length $ l ++ f ++ r)
Fill-style bracketed pretty-printing format
Informal description
The function `Std.Format.bracketFill` takes a left delimiter string `l`, a pretty-printing format `f`, and a right delimiter string `r`, and returns a grouped version of the format where the content is nested inside the delimiters with "fill" flattening behavior. This means the pretty-printer will attempt to fit the content within the available width constraints while maintaining the delimiters.
Std.Format.defIndent definition
Full source
/-- Default indentation. -/
def defIndent  := 2
Default indentation width
Informal description
The default indentation width is set to 2 spaces.
Std.Format.defUnicode definition
Full source
def defUnicode := true
Unicode formatting flag
Informal description
The definition `Std.Format.defUnicode` is set to the Boolean value `true`.
Std.Format.defWidth definition
Full source
/-- Default width of the targeted output pane. -/
def defWidth   := 120
Default output pane width
Informal description
The default width of the targeted output pane is set to 120.
Std.Format.nestD definition
(f : Format) : Format
Full source
/-- Nest with the default indentation amount.-/
def nestD (f : Format) : Format :=
  nest defIndent f
Default nesting of pretty-printing format
Informal description
The function `Std.Format.nestD` takes a pretty-printing format `f` and nests it with the default indentation amount (2 spaces).
Std.Format.indentD definition
(f : Format) : Format
Full source
/-- Insert a newline and then `f`, all nested by the default indent amount. -/
def indentD (f : Format) : Format :=
  nestD (Format.line ++ f)
Default indentation of pretty-printing format
Informal description
The function `Std.Format.indentD` takes a pretty-printing format `f` and inserts a newline followed by `f`, with the entire content nested by the default indentation amount (2 spaces).
Std.Format.instMonadPrettyFormatStateMState instance
: MonadPrettyFormat (StateM State)
Full source
instance : MonadPrettyFormat (StateM State) where
  -- We avoid a structure instance update, and write these functions using pattern matching because of issue #316
  pushOutput s       := modify fun ⟨out, col⟩ => ⟨out ++ s, col + s.length⟩
  pushNewline indent := modify fun ⟨out, _⟩ => ⟨out ++ "\n".pushn ' ' indent, indent⟩
  currColumn         := return (← get).column
  startTag _         := return ()
  endTags _          := return ()
Pretty-Printing in the State Monad
Informal description
The state monad transformer `StateM State` is equipped with a canonical structure for pretty-printing `Format` objects within its monadic context.
Std.ToFormat structure
(α : Type u)
Full source
/-- Class for converting a given type α to a `Format` object for pretty-printing.
See also `Repr`, which also outputs a `Format` object. -/
class ToFormat (α : Type u) where
  format : α → Format
Pretty-printing format conversion class
Informal description
A class that provides a method to convert elements of a type `α` into a `Format` object for pretty-printing purposes. This is similar to the `Repr` class, which also produces `Format` objects, but focuses specifically on pretty-printing.
Std.instToFormatFormat instance
: ToFormat Format
Full source
instance : ToFormat Format where
  format f := f
Pretty-printing Format Conversion for Format Type
Informal description
The type `Format` has a canonical way to convert its elements into pretty-printing format objects.
Std.instToFormatString instance
: ToFormat String
Full source
instance : ToFormat String where
  format s := Format.text s
Pretty-printing Format Conversion for Strings
Informal description
The type `String` has a canonical way to convert its elements into pretty-printing format objects.
Std.Format.joinSep definition
{α : Type u} [ToFormat α] : List α → Format → Format
Full source
/-- Intersperse the given list (each item printed with `format`) with the given `sep` format. -/
def Format.joinSep {α : Type u} [ToFormat α] : List α → FormatFormat
  | [],    _   => nil
  | [a],   _   => format a
  | a::as, sep => as.foldl (· ++ sep ++ format ·) (format a)
Intersperse list elements with separator in pretty-printing format
Informal description
Given a list of elements (each convertible to a pretty-printing format) and a separator format, this function produces a new format where the elements are interspersed with the separator. Specifically: - For an empty list, it returns an empty format - For a singleton list `[a]`, it returns the format of `a` - For a list `a::as`, it folds the list by appending the separator and each subsequent element's format to the accumulated result, starting with the format of `a` Example: `joinSep [1, 2, 3] ", "` would produce the format equivalent to `"1, 2, 3"`.
Std.Format.prefixJoin definition
{α : Type u} [ToFormat α] (pre : Format) : List α → Format
Full source
/-- Format each item in `items` and prepend prefix `pre`. -/
def Format.prefixJoin {α : Type u} [ToFormat α] (pre : Format) : List α → Format
  | []    => nil
  | a::as => as.foldl (· ++ pre ++ format ·) (pre ++ format a)
Prefix-joined pretty-printing format
Informal description
Given a pretty-printing format `pre` and a list `items` of elements of type `α` (where each element can be converted to a pretty-printing format), the function `prefixJoin` formats each item in `items` and prepends `pre` to each item, then joins them together. For example: - `prefixJoin "• " [1, 2, 3]` produces `"• 1• 2• 3"` - `prefixJoin " " ["a", "b", "c"]` produces `" a b c"` The function works by folding the list, starting with the first element prepended with `pre`, and then appending `pre` followed by each subsequent element's format.
Std.Format.joinSuffix definition
{α : Type u} [ToFormat α] : List α → Format → Format
Full source
/-- Format each item in `items` and append `suffix`. -/
def Format.joinSuffix {α : Type u} [ToFormat α] : List α → FormatFormat
  | [],    _      => nil
  | a::as, suffix => as.foldl (· ++ format · ++ suffix) (format a ++ suffix)
Format list with suffix after each element
Informal description
Given a list of elements of type `α` (where each element can be converted to a pretty-printing format) and a suffix format, this function formats each element in the list and appends the suffix after each element. For an empty list, it returns the empty format. For a non-empty list `[a₁, a₂, ..., aₙ]`, it produces the format `(format a₁ ++ suffix) ++ (format a₂ ++ suffix) ++ ... ++ (format aₙ ++ suffix)`.