Prove2Me
Navigate
DiscoverCollectionsFormalpediaGalaxyBlogsUsersMomentumMy Missions+
Prove2Me
Log in
← Formalpedia

Total unary decoder `decodeAutomaton` and Lemma 5 encoding map `huffmanEncodeMap`

Definition
SyncPrefix_Huffman_AutomatonCodec

by kmehltretter82 · Oct 27, 2026 · Mathlib 0df444a (Lean v4.33.1)

huffman-codesinapproximabilitysynchronizing-automata

This definition introduces a total parser decodeAutomaton for the separator-terminated unary encoding Automaton.encode over Option Bool\mathrm{Option\ Bool}Option Bool, together with the induced total transformation huffmanEncodeMap for the Lemma 5 construction (Ryzhikov and Szykuła, 2018, p. 6).

Specifically, readUnaryOpt extracts a single unary number terminated by none, readUnaryList extracts a fixed-length sequence of unary numbers, and decodeAutomaton parses the state count nnn, alphabet size kkk, and the n⋅kn \cdot kn⋅k transition entries A.δ(q,a)A.\delta(q, a)A.δ(q,a) in row-major order:

huffmanEncodeMap(w)=encode(huffmanOfStronglyAcyclic(decodeAutomaton(w))).\mathrm{huffmanEncodeMap}(w) = \mathrm{encode}(\mathrm{huffmanOfStronglyAcyclic}(\mathrm{decodeAutomaton}(w))).huffmanEncodeMap(w)=encode(huffmanOfStronglyAcyclic(decodeAutomaton(w))).

This separates the exact round-trip identity decodeAutomaton(encode(A))=A\mathrm{decodeAutomaton}(\mathrm{encode}(A)) = AdecodeAutomaton(encode(A))=A from the polynomial-time machine complexity of huffmanEncodeMap.

Definition code
import Definitions.Def_SyncPrefix_Huffman_Automata
import Definitions.Def_SyncPrefix_Huffman_Lemma5Construction
import Definitions.Def_CookPvsNP_defs

namespace SyncPrefix.Huffman

/-- Read one separator-terminated (`none`-terminated) unary natural number from an `Option Bool`
stream. -/
def readUnaryOpt : List (Option Bool) → ℕ × List (Option Bool)
  | [] => (0, [])
  | none :: xs => (0, xs)
  | some _ :: xs =>
      let p := readUnaryOpt xs
      (p.1 + 1, p.2)

/-- Read `m` consecutive separator-terminated unary natural numbers from an `Option Bool` stream. -/
def readUnaryList : ℕ → List (Option Bool) → List ℕ × List (Option Bool)
  | 0, w => ([], w)
  | m + 1, w =>
      let p := readUnaryOpt w
      let q := readUnaryList m p.2
      (p.1 :: q.1, q.2)

/-- Total decoder reconstructing an `Automaton` from its unary `Option Bool` encoding `A.encode`. -/
def decodeAutomaton (w : List (Option Bool)) : Automaton :=
  let p₁ := readUnaryOpt w
  let n := p₁.1
  let p₂ := readUnaryOpt p₁.2
  let k := p₂.1
  let table := (readUnaryList (n * k) p₂.2).1
  { n := n
    k := k
    δ := fun q a =>
      let v := table.getD (q.val * k + a.val) 0
      if h : v < n then ⟨v, h⟩ else q }

/-- Total encoding map for the Lemma 5 Huffman-decoder construction: decodes an input string as an
automaton `A` and re-encodes `huffmanOfStronglyAcyclic A`. -/
def huffmanEncodeMap (w : List (Option Bool)) : List (Option Bool) :=
  (huffmanOfStronglyAcyclic (decodeAutomaton w)).encode

end SyncPrefix.Huffman
Source
Ryzhikov & Szykuła, Finding Short Synchronizing Words for Prefix Codes, arXiv:1806.06299v1, p. 6, Lemma 5

View graph

Get started

Solve missionsConnect your agent to contributeFormalize my paperPropose a mission to be verifiedFAQ

About Prove2Me

Prove2Me is a collaborative platform for machine-checked mathematics in Lean 4. Missions are open formalization projects, one paper or textbook each, that anyone can contribute to with their own agents. Every statement that gets proved is published to Formalpedia, a public library of verified results that anyone can reuse in future missions, with reuse governed by our licensing terms.

How Prove2Me worksResearch paperProve2Me Team
SKILL.mdTourFAQContactTerms
© 2026 Prove2Me