Documentation

CslibFrontier.Algorithms.Lean.LempelZiv.LZ77

Lempel–Ziv sliding-window parsing (LZ77) and the exhaustive history (LZ76) #

A phrase u is reproducible from the text h already written, within window W, if it can be written by copying symbols one at a time from d positions back, for some 1 ≤ d ≤ W with d ≤ |h|. The copy may run into u itself: aaaa is reproducible from a with d = 1. A history of x is a factorization x = u₁ ⋯ u_c into nonempty phrases each of which is reproducible from u₁ ⋯ u_{i-1} except possibly for its last symbol ([LZ76], where the window is unbounded; [ZL77] with a finite window).

The greedy (exhaustive) parse takes as each phrase the longest reproducible prefix of the rest of the input followed by one more symbol (or the whole rest, if it is reproducible). For example, with an unbounded window, ABBABBABBBAABABAA parses as A, B, BA, BBABBB, AA, BABA, A, and aaaaaaaa as a, aaaaaaa (an overlapping copy). Each phrase is encoded as a Phrase codeword ⟨d, ℓ, a⟩: copy ℓ symbols from distance d, then write a.

Scope: the window bounds only how far back a copy may start. As in [LZ76], a copy may be arbitrarily long; the lookahead buffer of [ZL77], which also bounds the phrase length, and its fixed-width codewords are not modelled. The functions are list-based reference implementations, and no running time is claimed.

Main definitions #

Main results #

References #

The decoder #

An LZ77 codeword (offset, length, next): copy length symbols starting offset positions before the end of the text written so far (0 when length = 0), then append next.

  • offset : ℕ

    Distance backward in the window where the match starts (0 when length = 0).

  • length : ℕ

    Number of symbols copied before the trailing symbol next.

  • next : α

    Trailing symbol appended after the copied prefix.

Instances For
    theorem Cslib.Algorithms.Lean.LempelZiv.LZ77.Phrase.ext {α : Type u_2} {x y : Phrase α} (offset : x.offset = y.offset) (length : x.length = y.length) (next : x.next = y.next) :
    x = y
    @[instance_reducible]
    def Cslib.Algorithms.Lean.LempelZiv.LZ77.instDecidableEqPhrase.decEq {α✝ : Type u_2} [DecidableEq α✝] (x✝ x✝¹ : Phrase α✝) :
    Decidable (x✝ = x✝¹)
    @[instance_reducible]
    @[instance_reducible]

    Append the symbol d positions before the end (nothing if there is none).

    Decode one codeword: copy t.length symbols from distance t.offset, then write t.next.

    Decode a sequence of LZ77 Phrase codewords.

    theorem Cslib.Algorithms.Lean.LempelZiv.LZ77.iterate_copyStep {α : Type u_1} {d : ℕ} (hd : 0 < d) (h v : List α) :
    d ≤ h.length → v <+: List.drop (h.length - d) (h ++ v) → (copyStep d)^[v.length] h = h ++ v

    Copying |v| symbols from distance d writes v, if v is a copy from that distance.

    Reproduction and histories #

    u is reproducible from the text h within window W: it is empty, or it is a copy that starts d symbols back, for some 1 ≤ d ≤ W with d ≤ |h|; the copy may run into u itself. This is reproduction h → h u of [LZ76] with the copy's start restricted to the last W positions of h.

    Instances For

      A history of the text h ++ ws.flatten from h, within window W: every phrase is nonempty and reproducible from the text before it except for its last symbol (production ⇒ in [LZ76]).

      @[simp]
      theorem Cslib.Algorithms.Lean.LempelZiv.LZ77.isHistory_cons {α : Type u_1} (W : ℕ) (h u : List α) (ws : List (List α)) :
      IsHistory W h (u :: ws) ↔ u ≠ [] ∧ Reproducible W h u.dropLast ∧ IsHistory W (h ++ u) ws

      A history is exhaustive if no phrase but the last is reproducible from the text before it ([LZ76]).

      @[simp]
      theorem Cslib.Algorithms.Lean.LempelZiv.LZ77.isExhaustive_cons {α : Type u_1} (W : ℕ) (h u : List α) (ws : List (List α)) :
      IsExhaustive W h (u :: ws) ↔ (ws ≠ [] → ¬Reproducible W h u) ∧ IsExhaustive W (h ++ u) ws

      The empty phrase is reproducible.

      theorem Cslib.Algorithms.Lean.LempelZiv.LZ77.Reproducible.of_prefix {α : Type u_1} {W : ℕ} {h u v : List α} (hu : Reproducible W h u) (hvu : v <+: u) :

      Reproducibility is closed under prefixes.

      theorem Cslib.Algorithms.Lean.LempelZiv.LZ77.Reproducible.drop {α : Type u_1} {W : ℕ} {h v : List α} (hv : Reproducible W h v) (k : ℕ) :

      Moving the first k symbols of a reproducible phrase into the text keeps the rest reproducible.

      theorem Cslib.Algorithms.Lean.LempelZiv.LZ77.reproducible_of_infix {α : Type u_1} {W : ℕ} {h u : List α} (hhW : h.length ≤ W) (hu : u <:+: h) :

      Every infix of the text is reproducible when the window covers the whole text.

      The greedy parse #

      The length of the longest reproducible prefix of r.

      def Cslib.Algorithms.Lean.LempelZiv.LZ77.parseAux {α : Type u_1} [DecidableEq α] (W : ℕ) :
      ℕ → List α → List α → List (List α)

      The greedy parse of r after the text h: the next phrase is the longest reproducible prefix of r followed by one more symbol (all of r if r is reproducible).

      The greedy (exhaustive) parse of x with window W.

      The Lempel–Ziv complexity of x ([LZ76]): the number of phrases of its exhaustive history with an unbounded window. Copies never start before the beginning of the text, so the window |x| is unbounded for x.

      The longest reproducible prefix of r is no longer than r.

      The prefix of length copyLen is reproducible.

      theorem Cslib.Algorithms.Lean.LempelZiv.LZ77.le_copyLen {α : Type u_1} [DecidableEq α] {W : ℕ} {h r u : List α} (hu : Reproducible W h u) (hur : u <+: r) :

      Every reproducible prefix of r is at most copyLen long.

      @[simp]

      The phrases of the greedy parse concatenate to the input.

      The greedy parse is a history of its input.

      The greedy parse is exhaustive: no phrase but the last is reproducible.

      Optimality #

      theorem Cslib.Algorithms.Lean.LempelZiv.LZ77.length_add_length_le {α : Type u_1} [DecidableEq α] {W : ℕ} {h h' r u s : List α} (heq : h ++ r = h' ++ (u ++ s)) (hh : h'.length ≤ h.length) (hrep : Reproducible W h' u.dropLast) :
      h'.length + u.length ≤ h.length + min (copyLen W h r + 1) r.length

      One step of the exchange argument: a phrase u of a history that starts no later than the greedy phrase (after h) also ends no later.

      theorem Cslib.Algorithms.Lean.LempelZiv.LZ77.length_parse_le_of_isHistory {α : Type u_1} [DecidableEq α] {W : ℕ} {x : List α} {ws : List (List α)} (hx : ws.flatten = x) (hws : IsHistory W [] ws) :

      Optimality of the exhaustive history ([LZ76]): the greedy parse of x has no more phrases than any history of x with the same window.

      The Lempel–Ziv complexity is the least number of phrases of a history.

      theorem Cslib.Algorithms.Lean.LempelZiv.LZ77.parse_eq_of_isExhaustive {α : Type u_1} [DecidableEq α] {W : ℕ} {x : List α} {ws : List (List α)} (hx : ws.flatten = x) (hws : IsHistory W [] ws) (hex : IsExhaustive W [] ws) :
      parse W x = ws

      Uniqueness of the exhaustive history ([LZ76]): an exhaustive history of x is its greedy parse.

      The (distance, length, symbol) code #

      The smallest distance from which u is a copy, within window W; 0 if u is empty or there is no such distance.

      theorem Cslib.Algorithms.Lean.LempelZiv.LZ77.bestDist_spec {α : Type u_1} [DecidableEq α] {W : ℕ} {h u : List α} {d : ℕ} (h0 : 0 < d) (hW : d ≤ W) (hh : d ≤ h.length) (hu : u <+: List.drop (h.length - d) (h ++ u)) :
      u = [] ∨ 0 < bestDist W h u ∧ bestDist W h u ≤ h.length ∧ u <+: List.drop (h.length - bestDist W h u) (h ++ u)

      If u is a copy from some distance within the window, then bestDist is such a distance (or u is empty).

      Encode each nonempty phrase u after the text h as ⟨bestDist W h u.dropLast, |u| - 1, a⟩, where a is the last symbol of u. Empty phrases are skipped.

      The LZ77 code of x with window W.

      theorem Cslib.Algorithms.Lean.LempelZiv.LZ77.decode_encodeAux {α : Type u_1} [DecidableEq α] {W : ℕ} {ws : List (List α)} (hws : IsHistory W [] ws) :

      Decoding a history's code gives the text back.

      The LZ77 code is lossless. The decoder does not need the window.

      theorem Cslib.Algorithms.Lean.LempelZiv.LZ77.ne_nil_of_mem_parse {α : Type u_1} [DecidableEq α] {W : ℕ} {x u : List α} (hu : u ∈ parse W x) :

      The phrases of the greedy parse are nonempty.

      The code has one triple per phrase.