Documentation

CslibFrontier.Algorithms.Lean.LempelZiv.LZ78

Lempel–Ziv incremental parsing (LZ78) #

The incremental parsing of [ZL78] cuts a word into phrases from left to right: each phrase is the shortest prefix of the remaining input that is not an earlier phrase. When the input runs out first, the last phrase is the whole (nonempty) remainder, which may repeat an earlier phrase. For example ([CT06], Ch. 13), ABBABBABBBAABABAA parses as A, B, BA, BB, AB, BBA, ABA, BAA.

Every phrase is an earlier phrase (or []) followed by one symbol, so the parse is encoded by (pointer, symbol) pairs: the pair (j, a) stands for phrase j (with phrase 0 being []) followed by a. The example above becomes (0,A) (0,B) (2,A) (2,B) (1,B) (4,A) (5,A) (3,A).

This file defines the parse, the pair encoder and decoder, and proves that the code is lossless. The number of phrases is bounded in CslibFrontier.Algorithms.Lean.LempelZiv.LZ78Count, and the bit-level code and its length bound are in CslibFrontier.Algorithms.Lean.LempelZiv.LZ78Bits. The functions are reference implementations with quadratic dictionary lookups; no running time is claimed.

Main definitions #

Main results #

References #

Parsing #

def Cslib.Algorithms.Lean.LempelZiv.LZ78.splitPhraseAux {α : Type u_1} [DecidableEq α] (dict : List (List α)) :
List α → List α → List α × List α

Extend the current phrase acc symbol by symbol while it is in the dictionary. Returns the phrase and the remaining input.

def Cslib.Algorithms.Lean.LempelZiv.LZ78.splitPhrase {α : Type u_1} [DecidableEq α] (dict : List (List α)) (x : List α) :
List α × List α

The next LZ78 phrase of x against the dictionary dict of earlier phrases, and the remaining input: the shortest prefix of x not in dict, or all of x if every nonempty prefix of x is in dict.

theorem Cslib.Algorithms.Lean.LempelZiv.LZ78.splitPhrase_append {α : Type u_1} [DecidableEq α] (dict : List (List α)) (x : List α) :
(splitPhrase dict x).1 ++ (splitPhrase dict x).2 = x

The phrase and the remaining input concatenate to the input.

theorem Cslib.Algorithms.Lean.LempelZiv.LZ78.length_splitPhrase_snd_lt {α : Type u_1} [DecidableEq α] (dict : List (List α)) (a : α) (x : List α) :
(splitPhrase dict (a :: x)).2.length < (a :: x).length

On nonempty input, the phrase consumes at least one symbol.

theorem Cslib.Algorithms.Lean.LempelZiv.LZ78.splitPhrase_notMem {α : Type u_1} [DecidableEq α] (dict : List (List α)) (x : List α) (h : (splitPhrase dict x).2 ≠ []) :
(splitPhrase dict x).1 ∉ dict

If input remains after the phrase, the phrase is new.

theorem Cslib.Algorithms.Lean.LempelZiv.LZ78.splitPhrase_prefix {α : Type u_1} [DecidableEq α] (dict : List (List α)) (x q : List α) :
q <+: (splitPhrase dict x).1 → q ≠ [] → q ≠ (splitPhrase dict x).1 → q ∈ dict

Every nonempty proper prefix of the phrase is in the dictionary, so the phrase is the shortest prefix of the input that is not.

LZ78 parsing of x against the dictionary dict of earlier phrases, with fuel steps. Each step consumes at least one symbol, so fuel = x.length suffices.

LZ78 incremental parsing ([ZL78]): cut x into phrases, each the shortest prefix of the remaining input that is not an earlier phrase (the last phrase may repeat an earlier one).

@[simp]

The LZ78 phrases concatenate to the input.

Every LZ78 phrase is nonempty.

The number of phrases is at most the input length.

theorem Cslib.Algorithms.Lean.LempelZiv.LZ78.prefix_mem_take_parse {α : Type u_1} [DecidableEq α] (x : List α) (i : ℕ) (hi : i < (parse x).length) (q : List α) (hq : q <+: (parse x)[i]) (hne : q ≠ []) (hne' : q ≠ (parse x)[i]) :

Prefix closure. Every nonempty proper prefix of an LZ78 phrase is an earlier phrase.

theorem Cslib.Algorithms.Lean.LempelZiv.LZ78.getElem_parse_notMem_take {α : Type u_1} [DecidableEq α] (x : List α) (i : ℕ) (hi : i + 1 < (parse x).length) :
(parse x)[i] ∉ List.take i (parse x)

Novelty. Every LZ78 phrase except the last is different from all earlier phrases.

The LZ78 phrases before the last are pairwise distinct.

All LZ78 phrases are pairwise distinct when the last phrase is new.

A phrase sequence is tree structured over the dictionary dict if every phrase is an earlier phrase, a dictionary entry, or [], followed by one symbol. LZ78 parses are tree structured over [] ([CT06], Ch. 13).

@[simp]
theorem Cslib.Algorithms.Lean.LempelZiv.LZ78.isTreeStructured_cons {α : Type u_1} (dict : List (List α)) (w : List α) (ws : List (List α)) :
IsTreeStructured dict (w :: ws) ↔ (∃ q ∈ [] :: dict, ∃ (a : α), w = q ++ [a]) ∧ IsTreeStructured (dict ++ [w]) ws

The LZ78 parse is tree structured.

The (pointer, symbol) code #

Encode each nonempty phrase w as the index of w.dropLast in [] :: dict (the dictionary grows by every encoded phrase) and the last symbol of w. Empty phrases are skipped.

LZ78 encoder ([ZL78]): the (pointer, symbol) pairs of the LZ78 parse. Pointer 0 is the empty phrase and pointer j ≥ 1 the j-th phrase.

Decode (pointer, symbol) pairs against the dictionary dict of earlier phrases. An out-of-range pointer is read as the empty phrase.

LZ78 decoder: rebuild the phrases from the pairs and concatenate them.

Decoding inverts encoding for a phrase sequence that is tree structured over dict.

@[simp]

The LZ78 code is lossless.

A tree-structured phrase sequence is encoded with one pair per phrase.

@[simp]

There is one pair per phrase.