Documentation

CslibFrontier.Algorithms.WordRAM.BurrowsWheeler

Burrows–Wheeler Inversion on the Word-RAM #

Linear-time implementation of Algorithm D of [BW94] (Sections 2 and 4.2) on the standard-basis Word-RAM (Basis.standard) for words over a bounded integer alphabet [0, σ):

  1. buildTablesCode clears σ buckets at cBase via zeroFill, scans the last column L once to populate both the rank array P[0..|L|) (rankTable L) at pBase and per-symbol frequencies in cBase, and runs an exclusive prefix-sum scan over [0, σ) so that C[b] = L.countP (· < b), in 11 * σ + 11 * |L| + 13 machine instructions.
  2. lfDecodeCode executes up to |L| table-driven LF steps r ↦ P[r] + C[L[r]] from row I, writing decoded symbols backwards into an output buffer so that halting returns inverseBwt (L, I) as a length-prefixed array without a post-copy pass, in 14 * |L| + 9 machine instructions.
  3. decodeCode / decodeProgram unpacks an encoded input inp = ⟨L, I, σ⟩ ([I, σ] ++ L), places C, P, and the output buffer in disjoint regions after the input, and returns inverseBwt (L, I) in at most 25 * |L| + 11 * σ + 34 instructions at any word width w with Fits w (3 * |L| + σ + 4) (and InputFits w inp.encode in decodeProgram_computesListInTime to bound an arbitrary encoded starting row I). On bwt u (with ∀ c ∈ u, c < σ), decodeProgram_bwt discharges InputFits automatically from bwtIndex u ≤ |u| and recovers u in O(|u| + σ) instructions.

Main definitions #

Main results #

References #

Input encoding and pure count identities #

Input to the integer-alphabet Burrows–Wheeler decoder: the last column last, the starting row index index, and the strict alphabet upper bound alphabetSize.

  • last : List ℕ

    The last column L.

  • index : ℕ

    The starting row index I.

  • alphabetSize : ℕ

    Strict upper bound σ on symbols appearing in last.

Instances For

    Every symbol of inp.last lies in [0, inp.alphabetSize).

    Word-array encoding [I, σ] ++ L: Data.init places |L| + 2 at address 0, I at 1, σ at 2, and L[0..|L|) at 3..|L|+2.

    Register layout (Fin 16, Basis.standard) #

    @[reducible, inline]

    Base address of input last column L.

    @[reducible, inline]

    Length n = |L|.

    @[reducible, inline]

    Alphabet upper bound σ.

    @[reducible, inline]

    Starting / current row index r.

    @[reducible, inline]

    Base address of cumulative count table C[0..σ).

    @[reducible, inline]

    Base address of rank table P[0..n).

    @[reducible, inline]

    Base address of output buffer [buf..buf+n].

    @[reducible, inline]

    Output list header address returned at halt.

    Word-RAM structured code #

    Build both C[0..sigma) at cBase and P[0..len) at pBase in O(len + sigma) time.

    Run up to len LF steps from row using C at cBase and P at pBase, storing the length-prefixed decoded word in [buf..buf+len] and leaving its header address in out.

    Unpack Input.encode ([I, σ] ++ L at Data.init), allocate C, P, and buf after the input, build the tables, and run LF decoding.

    Flat Word-RAM program computing inverseBwt (inp.last, inp.index) from inp.encode.

    Correctness and running time of buildTablesCode #

    theorem Cslib.Algorithms.WordRAM.BurrowsWheeler.buildTablesCode_exec {w : ℕ} (x : WordRAM.Data 16) (L : List ℕ) (h1 : WordRAM.Fits w 1) (hlen_eq : x.regs Reg.len = L.length) (hs : WordRAM.Fits w (x.regs Reg.src + L.length)) (hc : WordRAM.Fits w (x.regs Reg.cBase + x.regs Reg.sigma)) (hp : WordRAM.Fits w (x.regs Reg.pBase + L.length)) (hdisj_sc : x.regs Reg.src + L.length ≤ x.regs Reg.cBase) (hdisj_cp : x.regs Reg.cBase + x.regs Reg.sigma ≤ x.regs Reg.pBase) (hvalid : ∀ c ∈ L, c < x.regs Reg.sigma) (hinput : ∀ (i : ℕ) (hi : i < L.length), x.mem (x.regs Reg.src + i) = L[i]) :
    ∃ (y : WordRAM.Data 16), WordRAM.ExecWithin w buildTablesCode x y (11 * x.regs Reg.sigma + 11 * L.length + 13) ∧ (∀ b < x.regs Reg.sigma, y.mem (x.regs Reg.cBase + b) = List.countP (fun (x : ℕ) => decide (x < b)) L) ∧ WordRAM.Block.HoldsList (x.regs Reg.pBase) (Lean.BurrowsWheeler.rankTable L) y.mem ∧ (∀ (a : ℕ), ¬(x.regs Reg.cBase ≤ a ∧ a < x.regs Reg.cBase + x.regs Reg.sigma) → ¬(x.regs Reg.pBase ≤ a ∧ a < x.regs Reg.pBase + L.length) → y.mem a = x.mem a) ∧ ∀ (i : Fin 16), ↑i ≤ 6 → y.regs i = x.regs i

    buildTablesCode builds C[0..sigma) at cBase and rankTable L at pBase within 11 * sigma + 11 * |L| + 13 instructions, preserving all other memory cells and the parameter registers 0..6.

    Correctness and running time of lfDecodeCode #

    theorem Cslib.Algorithms.WordRAM.BurrowsWheeler.lfDecodeCode_exec {w : ℕ} (x : WordRAM.Data 16) (L : List ℕ) (σ : ℕ) (h1 : WordRAM.Fits w 1) (hvalid : ∀ c ∈ L, c < σ) (hlen_eq : x.regs Reg.len = L.length) (hf_buf : WordRAM.Fits w (x.regs Reg.buf + L.length)) (hf_src : WordRAM.Fits w (x.regs Reg.src + L.length)) (hf_cb : WordRAM.Fits w (x.regs Reg.cBase + σ)) (hf_pb : WordRAM.Fits w (x.regs Reg.pBase + L.length)) (hd_sb : x.regs Reg.src + L.length ≤ x.regs Reg.buf) (hd_cb : x.regs Reg.cBase + σ ≤ x.regs Reg.buf) (hd_pb : x.regs Reg.pBase + L.length ≤ x.regs Reg.buf) (hL : ∀ (i : ℕ) (hi : i < L.length), x.mem (x.regs Reg.src + i) = L[i]) (hC : ∀ b < σ, x.mem (x.regs Reg.cBase + b) = List.countP (fun (x : ℕ) => decide (x < b)) L) (hP : ∀ (i : ℕ) (hi : i < L.length), x.mem (x.regs Reg.pBase + i) = List.count L[i] (List.take i L)) :

    lfDecodeCode computes inverseBwt (L, row) as a length-prefixed array inside [buf..buf+|L|] within 14 * |L| + 9 instructions.

    End-to-end Word-RAM BWT decoder #

    decodeCode inverts inp = ⟨L, I, σ⟩ from Data.init inp.encode within 25 * |L| + 11 * σ + 33 instructions at any fitting width Fits w (3 * |L| + σ + 4).

    theorem Cslib.Algorithms.WordRAM.BurrowsWheeler.decodeProgram_computesListInTime :
    decodeProgram.ComputesListInTime (fun (x : ℕ) (inp : Input) => inp.encode) (fun (inp : Input) (x : ℕ) => 25 * inp.last.length + 11 * inp.alphabetSize + 34) (fun (inp : Input) (w : ℕ) => inp.Valid ∧ WordRAM.Fits w (3 * inp.last.length + inp.alphabetSize + 4)) fun (inp : Input) => Lean.BurrowsWheeler.inverseBwt (inp.last, inp.index)

    decodeProgram computes inverseBwt (inp.last, inp.index) in 25 * |L| + 11 * σ + 34 instructions at every fitting width with inp.Valid, Fits w (3 * |L| + σ + 4), and InputFits w inp.encode (the ComputesListInTime input-fit hypothesis, which bounds an arbitrary encoded starting row I).

    theorem Cslib.Algorithms.WordRAM.BurrowsWheeler.decodeProgram_bwt (u : List ℕ) (σ w : ℕ) (hu : ∀ c ∈ u, c < σ) (hw : WordRAM.Fits w (3 * u.length + σ + 4)) :
    ∃ (t : ℕ), WordRAM.ExecutesList w decodeProgram (WordRAM.State.init { last := Lean.BurrowsWheeler.bwtLast u, index := Lean.BurrowsWheeler.bwtIndex u, alphabetSize := σ }.encode) u t ∧ t ≤ 25 * u.length + 11 * σ + 34

    On bwt u over an integer alphabet < σ, decodeProgram recovers u within 25 * |u| + 11 * σ + 34 instructions at any word width w with Fits w (3 * |u| + σ + 4).