Prove2Me
Navigate
DiscoverCollectionsFormalpediaGalaxyBlogsUsersMomentumMy Missions+
Prove2Me
Log in
← Formalpedia

Lemma 5 Huffman Decoder Construction from a Strongly Acyclic Synchronizing Automaton

Definition
SyncPrefix_Huffman_Lemma5Construction

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

automatahuffman-codessynchronizing-words

Given a complete deterministic automaton A=(Q,Σ,δ)A = (Q, \Sigma, \delta)A=(Q,Σ,δ) with nnn states and kkk letters, huffmanOfStronglyAcyclic A constructs the automaton AHA_HAH​ over k+2k + 2k+2 letters from the proof of Lemma 5 (Ryzhikov–Szykuła 2018, p. 6). When n≥2n \ge 2n≥2 and k≥1k \ge 1k≥1, let f=sinkOf(A)f = \mathrm{sinkOf}(A)f=sinkOf(A) be the sink state of AAA and S=sourcesOf(A,f)S = \mathrm{sourcesOf}(A, f)S=sourcesOf(A,f) be the list of non-sink states with in-degree 000 in AAA. The automaton AHA_HAH​ has 2n2n2n states (the nnn original states of AAA together with nnn auxiliary tree states) and k+2k + 2k+2 letters (Σ∪{b1,b2}\Sigma \cup \{b_1, b_2\}Σ∪{b1​,b2​}): original letters act as δ\deltaδ on QQQ and map all auxiliary states to fff; on Q∖{f}Q \setminus \{f\}Q∖{f}, the new letters b1,b2b_1, b_2b1​,b2​ act identically to letter 0∈Σ0 \in \Sigma0∈Σ; from fff, b1b_1b1​ and b2b_2b2​ enter a delay stem of n−∣S∣n - |S|n−∣S∣ auxiliary states followed by ∣S∣|S|∣S∣ caterpillar tree states whose leaves under b1b_1b1​ are the source states in SSS.

Definition code
import Definitions.Def_SyncPrefix_Huffman_Automata

namespace SyncPrefix.Huffman

/-- First sink state of `A` in `Fin A.n` (with `h : 0 < A.n`), or `0` if none exists. -/
def sinkOf (A : Automaton) (h : 0 < A.n) : Fin A.n :=
  match ((List.finRange A.n).filter fun q =>
      (List.finRange A.k).all fun a => decide (A.δ q a = q)).head? with
  | some q => q
  | none => ⟨0, h⟩

/-- List of non-sink source states of `A` (states `q ≠ f` with no incoming transitions in `A`). -/
def sourcesOf (A : Automaton) (f : Fin A.n) : List (Fin A.n) :=
  (List.finRange A.n).filter fun q =>
    decide (q ≠ f) &&
      (List.finRange A.n).all fun p =>
        (List.finRange A.k).all fun a => decide (A.δ p a ≠ q)

/-- Construction for Lemma 5 (p. 6): converts a synchronizing strongly acyclic automaton `A`
into a Huffman decoder over `A.k + 2` letters with the same shortest synchronizing word length
and between `A.n` and `2 * A.n` states. -/
def huffmanOfStronglyAcyclic (A : Automaton) : Automaton :=
  if h : 2 ≤ A.n ∧ 1 ≤ A.k then
    let hn : 0 < A.n := by omega
    let hk : 0 < A.k := by omega
    let f : Fin A.n := sinkOf A hn
    let S : List (Fin A.n) := sourcesOf A f
    let d : ℕ := A.n - S.length
    { n := 2 * A.n
      k := A.k + 2
      δ := fun q a =>
        if hq : q.val < A.n then
          let q₀ : Fin A.n := ⟨q.val, hq⟩
          if ha : a.val < A.k then
            let q' := A.δ q₀ ⟨a.val, ha⟩
            ⟨q'.val, by omega⟩
          else if q₀ = f then
            ⟨A.n, by omega⟩
          else
            let q' := A.δ q₀ ⟨0, hk⟩
            ⟨q'.val, by omega⟩
        else
          let i : ℕ := q.val - A.n
          if _ha : a.val < A.k then
            ⟨f.val, by omega⟩
          else if i < d then
            if hnext : i + 1 < A.n then
              ⟨A.n + (i + 1), by omega⟩
            else
              ⟨f.val, by omega⟩
          else
            let j : ℕ := i - d
            let s_j : Fin A.n := S.getD j f
            if a.val = A.k then
              ⟨s_j.val, by omega⟩
            else if hnext : i + 1 < A.n then
              ⟨A.n + (i + 1), by omega⟩
            else
              ⟨s_j.val, by omega⟩ }
  else
    { n := A.n
      k := A.k + 2
      δ := fun q _ => q }

@[simp]
theorem huffmanOfStronglyAcyclic_k (A : Automaton) :
    (huffmanOfStronglyAcyclic A).k = A.k + 2 := by
  unfold huffmanOfStronglyAcyclic
  split_ifs <;> rfl

theorem le_huffmanOfStronglyAcyclic_n (A : Automaton) :
    A.n ≤ (huffmanOfStronglyAcyclic A).n := by
  unfold huffmanOfStronglyAcyclic
  split_ifs <;> dsimp <;> omega

theorem huffmanOfStronglyAcyclic_n_le (A : Automaton) :
    (huffmanOfStronglyAcyclic A).n ≤ 2 * A.n := by
  unfold huffmanOfStronglyAcyclic
  split_ifs <;> dsimp <;> omega

end SyncPrefix.Huffman
Source
Ryzhikov–Szykuła 2018, Synchronizing Prefix Codes and Huffman’s Algorithm, 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