How inputs are given to algorithms

Lax185574.Encodings · concepts/Lax185574/Encodings.lean · lax-185574

definition

Loading review…

Sign in with ORCID

Community review

Flags

Each flag is tied to a public ORCID identity and explains why this concept may be incorrect.

No flags have been submitted.

    Community review

    Flag this concept

    State precisely what appears incorrect. This explanation will be public under your ORCID name.

    No source line selected.

    Natural Language Statement

    Definition

    The encodings of all values that algorithms in this submission read or write. Except for lists, functions on FinnFin n, finite sets, graphs and the values of ℕ∞, they are given by the rule of Lax185574.EncodingLax185574.Encoding. Encodings of further types, such as partial graphs and type trees, are given next to their definitions.

    type value encoding
    BoolBool falsefalse, truetrue 00, 11
    FinnFin n ii ii
    α×βα × β (a,b)(a, b) encoding of aa, then of bb
    α⊕βα ⊕ β inlainl a, inrbinr b 00, encoding of aa, and 11, encoding of bb
    OptionαOption α nonenone, someasome a 00, and 11, encoding of aa
    Zℤ m≥0m ≥ 0, −(m+1)-(m + 1) 0,m0, m, and 1,m1, m
    Qℚ p/qp / q in lowest terms, q>0q > 0 pp as an integer, then qq
    N∞ℕ∞ dd, ∞∞ d+1d + 1, and 00
    EmptyEmpty (no values)
    ListαList α [a1,…,am][a₁, …, aₘ] mm, then the encodings of a1a₁, …, amaₘ
    Finn→αFin n → α ff the list [f0,…,f(n−1)][f 0, …, f (n - 1)]
    FinsetαFinset α SS the list of the encodings of the elements of SS, in increasing order

    The encodings of the elements of a finite set are lists of numbers, and they are compared lexicographically. As a list of lists, the encoding of a set gives the length of each element's encoding in front of it.

    A first-order formula is written in prefix (Polish) notation, with one number per connective.

    formula encoding
    xᵢ adjacent to xⱼ 0,i,j0, i, j
    xᵢ = xⱼ 1,i,j1, i, j
    ¬φ 22, encoding of φ
    φ ∧ ψ 33, encoding of φ, encoding of ψ
    ∃xₙ φ 44, encoding of φ
    xᵢ has color c 55, encoding of cc, ii

    The first-order formulas Lax3.FirstOrder.FOLax3.FirstOrder.FO of the archive, which have no colors, are encoded in the same way, 0,i,j0, i, j for adjacency, 1,i,j1, i, j for equality, and 22, 33, 44 for negation, conjunction and the quantifier.

    A graph with n vertices is encoded by the family of the neighborhoods of its vertices 0, 1, …, n − 1, each a finite set of vertices. So the encoding starts with n, followed for each vertex by its degree and its neighbors in increasing order, each neighbor preceded by 11, the length of its encoding. A graph with n vertices and m edges has an encoding of length n + 4m + 1, whose numbers are at most n. A graph of mathlib on the vertices 0, 1, …, n − 1, a SimpleGraph(Finn)SimpleGraph (Fin n), is encoded in the same way.

    Concept map
    5 concepts
    100%
    DefinitionThis conceptRelated conceptA → B: B builds on ADescendants are omitted for concepts with more than 10 descendants.

    Lean source view on GitHub

    1import Lax185574.Encoding
    2import Lax185574.FirstOrderLogic
    3import Lax3.FirstOrder
    4import Mathlib.Data.Finset.Sort
    5import Mathlib.Data.List.Lex
    6import Mathlib.Data.Rat.Defs
    7import Mathlib.Data.ENat.Basic
    8
    9/-!
    10---
    11title: How inputs are given to algorithms
    12type: definition
    13---
    14The encodings of all values that algorithms in this submission read or
    15write. Except for lists, functions on `Fin n`, finite sets, graphs and
    16the values of ℕ∞, they are given by the rule of `Lax185574.Encoding`.
    17Encodings of further types, such as partial graphs and type trees, are
    18given next to their definitions.
    19
    20| type | value | encoding |
    21| --- | --- | --- |
    22| `Bool` | `false`, `true` | `0`, `1` |
    23| `Fin n` | `i` | `i` |
    24| `α × β` | `(a, b)` | encoding of `a`, then of `b` |
    25| `α ⊕ β` | `inl a`, `inr b` | `0`, encoding of `a`, and `1`, encoding of `b` |
    26| `Option α` | `none`, `some a` | `0`, and `1`, encoding of `a` |
    27| `ℤ` | `m ≥ 0`, `-(m + 1)` | `0, m`, and `1, m` |
    28| `ℚ` | `p / q` in lowest terms, `q > 0` | `p` as an integer, then `q` |
    29| `ℕ∞` | `d`, `∞` | `d + 1`, and `0` |
    30| `Empty` | (no values) | |
    31| `List α` | `[a₁, …, aₘ]` | `m`, then the encodings of `a₁`, …, `aₘ` |
    32| `Fin n → α` | `f` | the list `[f 0, …, f (n - 1)]` |
    33| `Finset α` | `S` | the list of the encodings of the elements of `S`, in increasing order |
    34
    35The encodings of the elements of a finite set are lists of numbers, and
    36they are compared lexicographically. As a list of lists, the encoding
    37of a set gives the length of each element's encoding in front of it.
    38
    39A first-order formula is written in prefix (Polish) notation, with one
    40number per connective.
    41
    42| formula | encoding |
    43| --- | --- |
    44| xᵢ adjacent to xⱼ | `0, i, j` |
    45| xᵢ = xⱼ | `1, i, j` |
    46| ¬φ | `2`, encoding of φ |
    47| φ ∧ ψ | `3`, encoding of φ, encoding of ψ |
    48| ∃xₙ φ | `4`, encoding of φ |
    49| xᵢ has color c | `5`, encoding of `c`, `i` |
    50
    51The first-order formulas `Lax3.FirstOrder.FO` of the archive, which
    52have no colors, are encoded in the same way, `0, i, j` for adjacency,
    53`1, i, j` for equality, and `2`, `3`, `4` for negation, conjunction and
    54the quantifier.
    55
    56A graph with n vertices is encoded by the family of the neighborhoods of
    57its vertices 0, 1, …, n − 1, each a finite set of vertices. So the
    58encoding starts with n, followed for each vertex by its degree and its
    59neighbors in increasing order, each neighbor preceded by `1`, the length
    60of its encoding. A graph with n vertices and m edges has an encoding of
    61length n + 4m + 1, whose numbers are at most n. A graph of mathlib on
    62the vertices 0, 1, …, n − 1, a `SimpleGraph (Fin n)`, is encoded in the
    63same way.
    64
    65# Formalization notes
    66
    67The tables are not part of the definition. They spell out what the
    68instances below and `deriving Encode` write for these declarations.
    69
    70The adjacency of a graph is a relation, so the encoding of a graph is not
    71derived. A number or ∞ takes one entry, so that a matrix of distances
    72has entries of constant size. Its derived encoding, as an optional
    73number, would take two entries for a finite value. Every finite set has exactly one encoding, so every graph has
    74exactly one encoding. Its length is at least the number of vertices, so
    75an algorithm whose running time is measured in the number of vertices
    76can read its whole input.
    77-/
    78
    79namespace Lax185574.Encodings
    80
    81open Lax185574.Encoding Lax185574.Graphs Lax185574.FirstOrderLogic
    82
    83universe u
    84
    85deriving instance Encode for Bool, Fin, Prod, Sum, Option, Int, Rat, Empty, Formula,
    86 Lax3.FirstOrder.FO
    87
    88/-- A list is encoded by its length, followed by the encodings of its
    89entries. -/
    90instance {α : Type u} [Encode α] : Encode (List α) where
    91 encode l := l.length :: (l.map encode).flatten
    92
    93/-- A family indexed by `Fin n` is encoded as the list of its members. -/
    94instance {α : Type u} {n : ℕ} [Encode α] : Encode (Fin n → α) where
    95 encode f := encode (List.ofFn f)
    96
    97/-- A finite set is encoded by the list of the encodings of its elements,
    98in increasing lexicographic order. -/
    99instance {α : Type u} [Encode α] : Encode (Finset α) where
    100 encode S := encode ((S.image encode).sort (· ≤ ·))
    101
    102/-- A number `d` or `∞` is encoded as one number, `d + 1` for `d` and
    103`0` for `∞`. -/
    104instance : Encode ℕ∞ where
    105 encode d := [if d = ⊤ then 0 else d.toNat + 1]
    106
    107open Classical in
    108/-- A graph of mathlib on `Fin n` is encoded by the family of the
    109neighborhoods of its vertices. -/
    110noncomputable instance {n : ℕ} : Encode (SimpleGraph (Fin n)) where
    111 encode H := encode fun v : Fin n => Finset.univ.filter (H.Adj v)
    112
    113open Classical in
    114/-- A graph is encoded by the family of the neighborhoods of its
    115vertices. -/
    116noncomputable instance : Encode Graph where
    117 encode G := encode fun v : Fin G.n => ({u | G.Adj v u} : Finset (Fin G.n))
    118
    119end Lax185574.Encodings
    120
    Formalization notes

    The tables are not part of the definition. They spell out what the instances below and derivingEncodederiving Encode write for these declarations.

    The adjacency of a graph is a relation, so the encoding of a graph is not derived. A number or ∞ takes one entry, so that a matrix of distances has entries of constant size. Its derived encoding, as an optional number, would take two entries for a finite value. Every finite set has exactly one encoding, so every graph has exactly one encoding. Its length is at least the number of vertices, so an algorithm whose running time is measured in the number of vertices can read its whole input.

    Discussion

    Ask a question or add context. Endorsements and structured flags are kept in the review panel above.

    Loading discussion…