Documentation

Cslib.Computability.Encoding.Basic

Default encodings #

Provides default encodings and encoding combinators for basic Lean types such as natural numbers, finite types, products, dependent pairs, sums, options and lists into Boolean words.

Since the complexity of a function depends on the exact encoding used, and thus the encoding is part of the definition of the function, we do not use a class but instead provide encodings merely as a convenience.

The delimit function is used as the basic building block here: It creates a self-delimiting, prefix-free code by replacing each bit b of a word with [true, b] and appending [false].

The product encoding delimits the first component and appends the second, and lists are encoded by delimiting each element. Dependent pairs are encoded like products, with the second component encoded under the encoding selected by the first one. Sums prefix the code of the component with a tag bit, and options are encoded as the sum Unit ⊕ α. Booleans are their single bit, natural numbers use the little-endian binary code Nat.bits, and Fin n uses a fixed-width binary code of Nat.clog 2 n bits, which encFintype transfers to any finite type via Fintype.equivFin. Subsingletons, including propositions, are encoded as the empty word by encSubsingleton.

Products delimit only their first component because the second one extends to the end of the code word, so a delimiter would add nothing for parsing; this keeps the second component undoubled and the size of right-nested products linear. Lists delimit every element since their number is not known in advance, and sums use a tag bit rather than a delimiter since it has a fixed length.

In general, a code word followed by further data cannot be split again; for example, encList enc [a] is a prefix of encList enc [a, b]. Wrap an encoding with encDelimited (as encList does for its elements) when its end has to be recognizable, see delimit_append_inj.

We do not use Mathlib's Computability.Encoding combinators since they change the alphabet (e.g. encodingProd encodes into Γ₁ ⊕ Γ₂), while here every encoding targets List Bool.

def Cslib.Encoding.encDelimited {α : Type u_1} (enc : α ↪ List Bool) :

Wraps every code word of an encoding with delimit, so that it can be followed by further data.

@[simp]
theorem Cslib.Encoding.encDelimited_apply {α : Type u_1} (enc : α ↪ List Bool) (a : α) :
(encDelimited enc) a = delimit (enc a)

Basic types #

A Boolean is encoded by its single bit.

Instances For
    @[simp]
    theorem Cslib.Encoding.encBool_apply (x✝ : Bool) :
    encBool x✝ = [x✝]

    Boolean words already are codes.

    Nat.bits is injective. Candidate for upstreaming to Mathlib.Data.Nat.Bits.

    @[simp]
    theorem Nat.bits_inj {m n : ℕ} :
    m.bits = n.bits ↔ m = n
    @[simp]
    theorem Nat.bits_eq_nil {n : ℕ} :
    n.bits = [] ↔ n = 0

    A natural number is encoded in binary, least significant bit first, as Nat.bits.

    @[simp]
    theorem Cslib.Encoding.encNat_apply (a✝ : ℕ) :
    encNat a✝ = a✝.bits

    Finite types #

    Fin n is encoded in binary as a fixed-width code of Nat.clog 2 n bits, least significant bit first.

    theorem Cslib.Encoding.encFin_apply (n : ℕ) (a : Fin n) :
    (encFin n) a = List.map (fun (x : ℕ) => (↑a).testBit x) (List.range (Nat.clog 2 n))
    @[simp]
    theorem Cslib.Encoding.length_encFin (n : ℕ) (a : Fin n) :
    ((encFin n) a).length = Nat.clog 2 n
    @[simp]
    theorem Cslib.Encoding.getElem_encFin (n : ℕ) (a : Fin n) (i : ℕ) (h : i < ((encFin n) a).length) :
    ((encFin n) a)[i] = (↑a).testBit i

    The i-th bit of the code is the i-th bit of the number.

    @[simp]
    theorem Cslib.Encoding.encFin_eq_nil {n : ℕ} (h : n ≤ 1) (a : Fin n) :
    (encFin n) a = []

    Types with at most one element are encoded as the empty word.

    noncomputable def Cslib.Encoding.encFintype (α : Type u_3) [Fintype α] :

    A finite type is encoded with the fixed width Nat.clog 2 (Fintype.card α) through Fintype.equivFin. The enumeration is not computable and depends on the Fintype instance, so this encoding serves existence results where only the code length matters. Concrete machines should instead compose an explicit embedding α ↪ Fin n with encFin n.

    theorem Cslib.Encoding.encFintype_apply {α : Type u_1} [Fintype α] (a : α) :
    @[simp]
    theorem Cslib.Encoding.length_encFintype {α : Type u_1} [Fintype α] (a : α) :
    @[simp]
    theorem Cslib.Encoding.encFintype_eq_nil {α : Type u_1} [Fintype α] [Subsingleton α] (a : α) :
    (encFintype α) a = []

    Subsingletons are encoded as the empty word via encFintype.

    A type with at most one element, such as a proposition, is encoded as the empty word. Unlike encFintype, this is computable and needs no Fintype instance.

    @[simp]
    theorem Cslib.Encoding.encSubsingleton_apply (α : Sort u_3) [Subsingleton α] (x✝ : α) :
    (encSubsingleton α) x✝ = []

    Combinators #

    def Cslib.Encoding.encProd {α : Type u_1} {β : Type u_2} (enc₁ : α ↪ List Bool) (enc₂ : β ↪ List Bool) :
    α × β ↪ List Bool

    A product is encoded as the delimited first component followed by the second component. Only the first component is delimited since the second one extends to the end of the code word; this keeps the second component undoubled and right-nested products linear in size.

    Instances For
      @[simp]
      theorem Cslib.Encoding.encProd_apply {α : Type u_1} {β : Type u_2} (enc₁ : α ↪ List Bool) (enc₂ : β ↪ List Bool) (p : α × β) :
      (encProd enc₁ enc₂) p = delimit (enc₁ p.1) ++ enc₂ p.2
      def Cslib.Encoding.encSigma {α : Type u_1} {β : α → Type u_3} (enc₁ : α ↪ List Bool) (enc₂ : (a : α) → β a ↪ List Bool) :
      (a : α) × β a ↪ List Bool

      A dependent pair is encoded as the delimited first component followed by the second component, encoded under the encoding selected by the first component. As for encProd, only the first component is delimited, which is exactly what is needed to know the encoding of the second one.

      @[simp]
      theorem Cslib.Encoding.encSigma_apply {α : Type u_1} {β : α → Type u_3} (enc₁ : α ↪ List Bool) (enc₂ : (a : α) → β a ↪ List Bool) (x : (a : α) × β a) :
      (encSigma enc₁ enc₂) x = delimit (enc₁ x.fst) ++ (enc₂ x.fst) x.snd
      theorem Cslib.Encoding.encProd_eq_encSigma {α : Type u_1} {β : Type u_2} (enc₁ : α ↪ List Bool) (enc₂ : β ↪ List Bool) (p : α × β) :
      (encProd enc₁ enc₂) p = (encSigma enc₁ fun (x : α) => enc₂) ⟨p.1, p.2⟩

      A product is a dependent pair with a constant second type.

      def Cslib.Encoding.encSum {α : Type u_1} {β : Type u_2} (enc₁ : α ↪ List Bool) (enc₂ : β ↪ List Bool) :

      A sum is encoded by a tag bit, false for inl and true for inr, followed by the code of the component. A tag bit is used instead of a delimiter since it has a fixed length and does not double the component.

      def Cslib.Encoding.encOption {α : Type u_1} (enc : α ↪ List Bool) :

      An option is encoded as the sum Unit ⊕ α: none is encoded as [false] and some a as true followed by the code of a.

      def Cslib.Encoding.encList {α : Type u_1} (enc : α ↪ List Bool) :

      A list is encoded as the concatenation of the delimited codes of its elements.

      Instances For
        @[simp]
        theorem Cslib.Encoding.encSum_inl {α : Type u_1} {β : Type u_2} (enc₁ : α ↪ List Bool) (enc₂ : β ↪ List Bool) (a : α) :
        (encSum enc₁ enc₂) (Sum.inl a) = false :: enc₁ a
        @[simp]
        theorem Cslib.Encoding.encSum_inr {α : Type u_1} {β : Type u_2} (enc₁ : α ↪ List Bool) (enc₂ : β ↪ List Bool) (b : β) :
        (encSum enc₁ enc₂) (Sum.inr b) = true :: enc₂ b
        @[simp]
        theorem Cslib.Encoding.encOption_none {α : Type u_1} (enc : α ↪ List Bool) :
        @[simp]
        theorem Cslib.Encoding.encOption_some {α : Type u_1} (enc : α ↪ List Bool) (a : α) :
        (encOption enc) (some a) = true :: enc a
        @[simp]
        theorem Cslib.Encoding.encList_nil {α : Type u_1} (enc : α ↪ List Bool) :
        (encList enc) [] = []
        @[simp]
        theorem Cslib.Encoding.encList_cons {α : Type u_1} (enc : α ↪ List Bool) (a : α) (l : List α) :
        (encList enc) (a :: l) = delimit (enc a) ++ (encList enc) l
        theorem Cslib.Encoding.encList_singleton {α : Type u_1} (enc : α ↪ List Bool) (a : α) :
        (encList enc) [a] = delimit (enc a)
        @[simp]
        theorem Cslib.Encoding.encList_append {α : Type u_1} (enc : α ↪ List Bool) (l l' : List α) :
        (encList enc) (l ++ l') = (encList enc) l ++ (encList enc) l'

        Lengths #

        theorem Cslib.Encoding.length_encProd {α : Type u_1} {β : Type u_2} (enc₁ : α ↪ List Bool) (enc₂ : β ↪ List Bool) (p : α × β) :
        ((encProd enc₁ enc₂) p).length = 2 * (enc₁ p.1).length + 1 + (enc₂ p.2).length
        theorem Cslib.Encoding.length_encSigma {α : Type u_1} {β : α → Type u_3} (enc₁ : α ↪ List Bool) (enc₂ : (a : α) → β a ↪ List Bool) (x : (a : α) × β a) :
        ((encSigma enc₁ enc₂) x).length = 2 * (enc₁ x.fst).length + 1 + ((enc₂ x.fst) x.snd).length
        theorem Cslib.Encoding.length_encSum {α : Type u_1} {β : Type u_2} (enc₁ : α ↪ List Bool) (enc₂ : β ↪ List Bool) (x : α ⊕ β) :
        ((encSum enc₁ enc₂) x).length = Sum.elim (fun (a : α) => (enc₁ a).length + 1) (fun (b : β) => (enc₂ b).length + 1) x
        theorem Cslib.Encoding.length_encOption {α : Type u_1} (enc : α ↪ List Bool) (o : Option α) :
        ((encOption enc) o).length = o.elim 1 fun (a : α) => (enc a).length + 1
        theorem Cslib.Encoding.length_encOption_some {α : Type u_1} (enc : α ↪ List Bool) (a : α) :
        ((encOption enc) (some a)).length = (enc a).length + 1
        @[simp]
        theorem Cslib.Encoding.length_encList {α : Type u_1} (enc : α ↪ List Bool) (l : List α) :
        ((encList enc) l).length = 2 * (List.map (fun (a : α) => (enc a).length) l).sum + l.length

        Each element contributes twice the length of its code plus one delimiter bit.

        theorem Cslib.Encoding.length_le_length_encList {α : Type u_1} (enc : α ↪ List Bool) (l : List α) :
        l.length ≤ ((encList enc) l).length

        Delimiters account for at least one bit per list element, even for empty element codes.