Documentation

CslibFrontier.Computability.Machines.Turing.MultiTape.Complexity.PolyTime.BitString

Polynomial-time bit-list operations #

Raw bit strings and framed lists share the list and iteration calculus. Captured traversals, Boolean folds, pointwise operations, majority and selected mask sums have compositional certificates. Runtime dimensions are computed in unary. Enumeration additionally requires a polynomial bound on the complete encoded output. The operations are total on empty and short lists.

Main results #

References #

Adapted from Computability/Probabilistic/BitString.lean and Computability/Probabilistic/UniformNat.lean and Crypto/Computational/GoldreichLevin/WordDecoder.lean on the cslib crypto branch at 5ff69ca93402e7c947208dc56396d6d86203ccd8, using the tidy machine calculus.

Reversing a most-significant-first word gives its little-endian binary decoding.

The encoding charges every bit and every delimiter in the enumerated words.

Frame the existing input encoding, charging every delimiter.

Saturating binary decoding to unary is polynomial time on arbitrary input words.

Decode computed bits under a computed unary cap, fixing both intermediate encodings.

theorem Turing.MultiTapeTM.ComputableTidilyInPolyTime.binary_to_unary_bounded {α : Type} {input : α ↪ List Bool} {value bound : α → ℕ} (hvalue : ComputableTidilyInPolyTime value input encNat) (hbound : ComputableTidilyInPolyTime bound input encUnary) (hle : ∀ (a : α), value a ≤ bound a) :

Convert a binary value to unary under a computed unary bound.

Convert a raw bit string to the framed list encoding.

theorem Turing.MultiTapeTM.ComputableTidilyInPolyTime.bitsGetElem? {α : Type} {input : α ↪ List Bool} {list : α → List Bool} {index : α → ℕ} (hlist : ComputableTidilyInPolyTime list input encBits) (hindex : ComputableTidilyInPolyTime index input encUnary) :
ComputableTidilyInPolyTime (fun (x : α) => (list x)[index x]?) input (encOption encBool)

Optional lookup in a raw bit string, fixing the representation of the returned bit.

Repeat a fixed bit a unary number of times.

theorem Turing.MultiTapeTM.ComputableTidilyInPolyTime.bitsFlatten {α : Type} {input : α ↪ List Bool} {words : α → List (List Bool)} (hwords : ComputableTidilyInPolyTime words input (encList encBits)) :
ComputableTidilyInPolyTime (fun (a : α) => (words a).flatten) input encBits

Concatenate computed raw words, fixing the intermediate list encoding before proof search.

theorem Turing.MultiTapeTM.ComputableTidilyInPolyTime.bitsPadWord {α : Type} {input : α ↪ List Bool} {bound : α → ℕ} {word : α → List Bool} (hbound : ComputableTidilyInPolyTime bound input encUnary) (hword : ComputableTidilyInPolyTime word input encBits) :
ComputableTidilyInPolyTime (fun (a : α) => Cslib.BitString.padWord (bound a) (word a)) input encBits

Self-delimiting padding uses ordinary framing and unary arithmetic.

theorem Turing.MultiTapeTM.ComputableTidilyInPolyTime.mapToBitsWithInput {α : Type} {input : α ↪ List Bool} {β : Type} {enc : β ↪ List Bool} {list : α → List β} {f : α → β → Bool} (hlist : ComputableTidilyInPolyTime list input (encList enc)) (hf : ComputableTidilyInPolyTime (fun (p : α × β) => f p.1 p.2) (encPair input enc) encBool) :
ComputableTidilyInPolyTime (fun (a : α) => List.map (f a) (list a)) input encBits

Map a captured Boolean-valued function over a framed list, emitting a raw bit string.

theorem Turing.MultiTapeTM.ComputableTidilyInPolyTime.flatMapToBitsWithInput {α : Type} {input : α ↪ List Bool} {β : Type} {enc : β ↪ List Bool} {list : α → List β} {f : α → β → List Bool} (hlist : ComputableTidilyInPolyTime list input (encList enc)) (hf : ComputableTidilyInPolyTime (fun (p : α × β) => f p.1 p.2) (encPair input enc) encBits) :
ComputableTidilyInPolyTime (fun (a : α) => List.flatMap (f a) (list a)) input encBits

Concatenate captured raw bit strings over a framed list. The complete intermediate words and their framing are charged before concatenation. Apply this rule explicitly: registering both raw and framed flatMap outputs makes existing enumeration proof search exceed its limit.

theorem Turing.MultiTapeTM.ComputableTidilyInPolyTime.bitsLow {α : Type} {input : α ↪ List Bool} {width value : α → ℕ} (hwidth : ComputableTidilyInPolyTime width input encUnary) (hvalue : ComputableTidilyInPolyTime value input encNat) :
ComputableTidilyInPolyTime (fun (a : α) => Cslib.BitString.lowBits (width a) (value a)) input encBits

Emit a computed unary number of low-order digits of a computed binary natural. The width charges every output bit, including leading zero digits.

theorem Turing.MultiTapeTM.ComputableTidilyInPolyTime.bitsMapWithInput {α : Type} {input : α ↪ List Bool} {list : α → List Bool} {f : α → Bool → Bool} (hlist : ComputableTidilyInPolyTime list input encBits) (hf : ComputableTidilyInPolyTime (fun (p : α × Bool) => f p.1 p.2) (encPair input encBool) encBool) :
ComputableTidilyInPolyTime (fun (a : α) => List.map (f a) (list a)) input encBits

Map a captured Boolean operation over a raw bit string.

theorem Turing.MultiTapeTM.ComputableTidilyInPolyTime.bitsFilterWithInput {α : Type} {input : α ↪ List Bool} {list : α → List Bool} {test : α → Bool → Bool} (hlist : ComputableTidilyInPolyTime list input encBits) (htest : ComputableTidilyInPolyTime (fun (p : α × Bool) => test p.1 p.2) (encPair input encBool) encBool) :
ComputableTidilyInPolyTime (fun (a : α) => List.filter (test a) (list a)) input encBits

Filter a raw bit string with a predicate that may capture its original input.

theorem Turing.MultiTapeTM.ComputableTidilyInPolyTime.bitsCountWithInput {α : Type} {input : α ↪ List Bool} {list : α → List Bool} {value : α → Bool} (hlist : ComputableTidilyInPolyTime list input encBits) (hvalue : ComputableTidilyInPolyTime value input encBool) :
ComputableTidilyInPolyTime (fun (x : α) => List.count (value x) (list x)) input encUnary

Count occurrences of a computed bit in a raw bit string.

theorem Turing.MultiTapeTM.ComputableTidilyInPolyTime.bitsAnyWithInput {α : Type} {input : α ↪ List Bool} {list : α → List Bool} {test : α → Bool → Bool} (hlist : ComputableTidilyInPolyTime list input encBits) (htest : ComputableTidilyInPolyTime (fun (p : α × Bool) => test p.1 p.2) (encPair input encBool) encBool) :
ComputableTidilyInPolyTime (fun (a : α) => (list a).any (test a)) input encBool

Test whether any bit satisfies a captured predicate.

theorem Turing.MultiTapeTM.ComputableTidilyInPolyTime.bitsFoldlWithInput {α : Type} {input : α ↪ List Bool} (op : Bool → Bool → Bool) {list : α → List Bool} {init : α → Bool} (hlist : ComputableTidilyInPolyTime list input encBits) (hinit : ComputableTidilyInPolyTime init input encBool) :
ComputableTidilyInPolyTime (fun (a : α) => List.foldl op (init a) (list a)) input encBool

A Boolean fold has constant-sized state, so no separate state bound is needed.

Apply a fixed Boolean operation pointwise, stopping at the shorter word.

theorem Turing.MultiTapeTM.ComputableTidilyInPolyTime.bitsZipWithInput {α : Type} {input : α ↪ List Bool} (op : Bool → Bool → Bool) {left right : α → List Bool} (hleft : ComputableTidilyInPolyTime left input encBits) (hright : ComputableTidilyInPolyTime right input encBits) :
ComputableTidilyInPolyTime (fun (a : α) => List.zipWith op (left a) (right a)) input encBits

Pointwise operations on computed words, with both intermediate encodings fixed.

Strict majority of computed votes fixes the intermediate raw-word encoding.

Generate one coordinate mask from its unary width and index.

Sum selected rows with a shrinking selector and an accumulator that never grows.

theorem Turing.MultiTapeTM.ComputableTidilyInPolyTime.bitsEnumList {α : Type} {input : α ↪ List Bool} {count : α → ℕ} (hcount : ComputableTidilyInPolyTime count input encUnary) {size : ℕ → ℕ} (hsize : Cslib.PolynomiallyBounded size) (hbudget : ∀ (a : α), 2 ^ count a * (2 * count a + 1) ≤ size (input a).length) :

Enumerate words only when their complete encoded size is polynomially bounded. This does not claim polynomial time in the number of enumerated bits.

theorem Turing.MultiTapeTM.ComputableTidilyInPolyTime.bitsRowWithInput {α : Type} {input : α ↪ List Bool} {width row : α → ℕ} {word : α → List Bool} (hwidth : ComputableTidilyInPolyTime width input encUnary) (hrow : ComputableTidilyInPolyTime row input encUnary) (hword : ComputableTidilyInPolyTime word input encBits) :
ComputableTidilyInPolyTime (fun (a : α) => Cslib.BitString.listRow (width a) (row a) (word a)) input encBits

Read a row of a computed word, including the cost of padding a short word.

theorem Turing.MultiTapeTM.ComputableTidilyInPolyTime.bitsRowsWithInput {α : Type} {input : α ↪ List Bool} {count width : α → ℕ} {word : α → List Bool} (hcount : ComputableTidilyInPolyTime count input encUnary) (hwidth : ComputableTidilyInPolyTime width input encUnary) (hword : ComputableTidilyInPolyTime word input encBits) :
ComputableTidilyInPolyTime (fun (a : α) => Cslib.BitString.listRows (count a) (width a) (word a)) input (encList encBits)

Extract rows from a word with polynomial-time unary dimensions.

theorem Turing.MultiTapeTM.ComputableTidilyInPolyTime.bitsXorWithInput {α : Type} {input : α ↪ List Bool} {width : α → ℕ} {words : α → List (List Bool)} (hwidth : ComputableTidilyInPolyTime width input encUnary) (hwords : ComputableTidilyInPolyTime words input (encList encBits)) :
ComputableTidilyInPolyTime (fun (a : α) => Cslib.BitString.listXor (width a) (words a)) input encBits

Variable-width XOR uses runtime lookup and ordinary Boolean folds.

Lexicographic comparison of equal-length words is polynomial-time.

Plain polynomial-time automation #

theorem Turing.MultiTapeTM.ComputableInPolyTime.bitsAnyWithInput {α : Type} {input : α ↪ List Bool} {list : α → List Bool} {test : α → Bool → Bool} (hlist : ComputableInPolyTime list input encBits) (htest : ComputableInPolyTime (fun (p : α × Bool) => test p.1 p.2) (encPair input encBool) encBool) :
ComputableInPolyTime (fun (a : α) => (list a).any (test a)) input encBool

Test whether any bit satisfies a captured predicate.

theorem Turing.MultiTapeTM.ComputableInPolyTime.bitsPadWord {α : Type} {input : α ↪ List Bool} {bound : α → ℕ} {word : α → List Bool} (hbound : ComputableInPolyTime bound input encUnary) (hword : ComputableInPolyTime word input encBits) :
ComputableInPolyTime (fun (a : α) => Cslib.BitString.padWord (bound a) (word a)) input encBits

Self-delimiting padding uses ordinary framing and unary arithmetic.

Repeat a fixed bit a unary number of times, including zero repetitions.

theorem Turing.MultiTapeTM.ComputableInPolyTime.bitsZipWithInput (op : Bool → Bool → Bool) {α : Type} {input : α ↪ List Bool} {left right : α → List Bool} (hleft : ComputableInPolyTime left input encBits) (hright : ComputableInPolyTime right input encBits) :
ComputableInPolyTime (fun (a : α) => List.zipWith op (left a) (right a)) input encBits

Apply a fixed Boolean operation pointwise to two computed raw words. The result stops at whichever word ends first; the two certificates retain their raw bit-string encoding.

theorem Turing.MultiTapeTM.ComputableInPolyTime.bitsRowWithInput {α : Type} {input : α ↪ List Bool} {width row : α → ℕ} {word : α → List Bool} (hwidth : ComputableInPolyTime width input encUnary) (hrow : ComputableInPolyTime row input encUnary) (hword : ComputableInPolyTime word input encBits) :
ComputableInPolyTime (fun (a : α) => Cslib.BitString.listRow (width a) (row a) (word a)) input encBits

Read a row of a computed word, charging padding and both computed unary indices.

Frame the existing input encoding, charging every delimiter.

Saturating binary decoding to unary is polynomial time on arbitrary input words.

theorem Turing.MultiTapeTM.ComputableInPolyTime.bitsBoundedBinaryValueWithInput {α : Type} {input : α ↪ List Bool} {cap : α → ℕ} {word : α → List Bool} (hcap : ComputableInPolyTime cap input encUnary) (hword : ComputableInPolyTime word input encBits) :
ComputableInPolyTime (fun (a : α) => Cslib.BitString.boundedBinaryValue (cap a) (word a)) input encUnary

Decode computed bits under a computed unary cap, fixing both intermediate encodings.

theorem Turing.MultiTapeTM.ComputableInPolyTime.binary_to_unary_bounded {α : Type} {input : α ↪ List Bool} {value bound : α → ℕ} (hvalue : ComputableInPolyTime value input encNat) (hbound : ComputableInPolyTime bound input encUnary) (hle : ∀ (a : α), value a ≤ bound a) :

Convert a binary value to unary under a computed unary bound.

Convert a raw bit string to the framed list encoding.

theorem Turing.MultiTapeTM.ComputableInPolyTime.bitsGetElem? {α : Type} {input : α ↪ List Bool} {list : α → List Bool} {index : α → ℕ} (hlist : ComputableInPolyTime list input encBits) (hindex : ComputableInPolyTime index input encUnary) :
ComputableInPolyTime (fun (x : α) => (list x)[index x]?) input (encOption encBool)

Optional lookup in a raw bit string, fixing the representation of the returned bit.

theorem Turing.MultiTapeTM.ComputableInPolyTime.bitsFlatten {α : Type} {input : α ↪ List Bool} {words : α → List (List Bool)} (hwords : ComputableInPolyTime words input (encList encBits)) :
ComputableInPolyTime (fun (a : α) => (words a).flatten) input encBits

Concatenate computed raw words, fixing the intermediate list encoding before proof search.

theorem Turing.MultiTapeTM.ComputableInPolyTime.mapToBitsWithInput {α : Type} {input : α ↪ List Bool} {β : Type} {enc : β ↪ List Bool} {list : α → List β} {f : α → β → Bool} (hlist : ComputableInPolyTime list input (encList enc)) (hf : ComputableInPolyTime (fun (p : α × β) => f p.1 p.2) (encPair input enc) encBool) :
ComputableInPolyTime (fun (a : α) => List.map (f a) (list a)) input encBits

Map a captured Boolean-valued function over a framed list, emitting a raw bit string.

theorem Turing.MultiTapeTM.ComputableInPolyTime.flatMapToBitsWithInput {α : Type} {input : α ↪ List Bool} {β : Type} {enc : β ↪ List Bool} {list : α → List β} {f : α → β → List Bool} (hlist : ComputableInPolyTime list input (encList enc)) (hf : ComputableInPolyTime (fun (p : α × β) => f p.1 p.2) (encPair input enc) encBits) :
ComputableInPolyTime (fun (a : α) => List.flatMap (f a) (list a)) input encBits

Concatenate captured raw bit strings over a framed list. The complete intermediate words and their framing are charged before concatenation. Apply this rule explicitly: registering both raw and framed flatMap outputs makes existing enumeration proof search exceed its limit.

theorem Turing.MultiTapeTM.ComputableInPolyTime.bitsLow {α : Type} {input : α ↪ List Bool} {width value : α → ℕ} (hwidth : ComputableInPolyTime width input encUnary) (hvalue : ComputableInPolyTime value input encNat) :
ComputableInPolyTime (fun (a : α) => Cslib.BitString.lowBits (width a) (value a)) input encBits

Emit a computed unary number of low-order digits of a computed binary natural. The width charges every output bit, including leading zero digits.

theorem Turing.MultiTapeTM.ComputableInPolyTime.bitsMapWithInput {α : Type} {input : α ↪ List Bool} {list : α → List Bool} {f : α → Bool → Bool} (hlist : ComputableInPolyTime list input encBits) (hf : ComputableInPolyTime (fun (p : α × Bool) => f p.1 p.2) (encPair input encBool) encBool) :
ComputableInPolyTime (fun (a : α) => List.map (f a) (list a)) input encBits

Map a captured Boolean operation over a raw bit string.

theorem Turing.MultiTapeTM.ComputableInPolyTime.bitsFilterWithInput {α : Type} {input : α ↪ List Bool} {list : α → List Bool} {test : α → Bool → Bool} (hlist : ComputableInPolyTime list input encBits) (htest : ComputableInPolyTime (fun (p : α × Bool) => test p.1 p.2) (encPair input encBool) encBool) :
ComputableInPolyTime (fun (a : α) => List.filter (test a) (list a)) input encBits

Filter a raw bit string with a predicate that may capture its original input.

theorem Turing.MultiTapeTM.ComputableInPolyTime.bitsCountPWithInput {α : Type} {input : α ↪ List Bool} {list : α → List Bool} {test : α → Bool → Bool} (hlist : ComputableInPolyTime list input encBits) (htest : ComputableInPolyTime (fun (p : α × Bool) => test p.1 p.2) (encPair input encBool) encBool) :
ComputableInPolyTime (fun (x : α) => List.countP (test x) (list x)) input encUnary

Count predicate matches in a raw bit string, including a captured input.

theorem Turing.MultiTapeTM.ComputableInPolyTime.bitsCountWithInput {α : Type} {input : α ↪ List Bool} {list : α → List Bool} {value : α → Bool} (hlist : ComputableInPolyTime list input encBits) (hvalue : ComputableInPolyTime value input encBool) :
ComputableInPolyTime (fun (x : α) => List.count (value x) (list x)) input encUnary

Count occurrences of a computed bit in a raw bit string.

theorem Turing.MultiTapeTM.ComputableInPolyTime.bitsAllWithInput {α : Type} {input : α ↪ List Bool} {list : α → List Bool} {test : α → Bool → Bool} (hlist : ComputableInPolyTime list input encBits) (htest : ComputableInPolyTime (fun (p : α × Bool) => test p.1 p.2) (encPair input encBool) encBool) :
ComputableInPolyTime (fun (a : α) => (list a).all (test a)) input encBool

Test whether every bit satisfies a captured predicate.

theorem Turing.MultiTapeTM.ComputableInPolyTime.bitsFoldlWithInput {α : Type} {input : α ↪ List Bool} (op : Bool → Bool → Bool) {list : α → List Bool} {init : α → Bool} (hlist : ComputableInPolyTime list input encBits) (hinit : ComputableInPolyTime init input encBool) :
ComputableInPolyTime (fun (a : α) => List.foldl op (init a) (list a)) input encBool

A Boolean fold has constant-sized state, so no separate state bound is needed.

Apply a fixed Boolean operation pointwise, stopping at the shorter word.

Strict majority uses filtering, length, unary multiplication and comparison.

Strict majority of computed votes fixes the intermediate raw-word encoding.

theorem Turing.MultiTapeTM.ComputableInPolyTime.bitsNonpositiveMarginWithInput {α : Type} {input : α ↪ List Bool} {votes : α → List Bool} {truth : α → Bool} (hvotes : ComputableInPolyTime votes input encBits) (htruth : ComputableInPolyTime truth input encBool) :
ComputableInPolyTime (fun (a : α) => Cslib.BitString.listNonpositiveMargin (votes a) (truth a)) input encBool

The training test is polynomial time in the vote list and its supplied label.

Generate one coordinate mask from its unary width and index.

Generate all coordinate masks, charging for their quadratic total size.

Sum selected rows with a shrinking selector and an accumulator that never grows.

theorem Turing.MultiTapeTM.ComputableInPolyTime.bitsEnumList {α : Type} {input : α ↪ List Bool} {count : α → ℕ} (hcount : ComputableInPolyTime count input encUnary) {size : ℕ → ℕ} (hsize : Cslib.PolynomiallyBounded size) (hbudget : ∀ (a : α), 2 ^ count a * (2 * count a + 1) ≤ size (input a).length) :
ComputableInPolyTime (fun (a : α) => Cslib.BitString.enumList (count a)) input (encList encBits)

Enumerate words only when their complete encoded size is polynomially bounded. This does not claim polynomial time in the number of enumerated bits.

theorem Turing.MultiTapeTM.ComputableInPolyTime.bitsRowsWithInput {α : Type} {input : α ↪ List Bool} {count width : α → ℕ} {word : α → List Bool} (hcount : ComputableInPolyTime count input encUnary) (hwidth : ComputableInPolyTime width input encUnary) (hword : ComputableInPolyTime word input encBits) :
ComputableInPolyTime (fun (a : α) => Cslib.BitString.listRows (count a) (width a) (word a)) input (encList encBits)

Extract rows from a word with polynomial-time unary dimensions.

theorem Turing.MultiTapeTM.ComputableInPolyTime.bitsXorWithInput {α : Type} {input : α ↪ List Bool} {width : α → ℕ} {words : α → List (List Bool)} (hwidth : ComputableInPolyTime width input encUnary) (hwords : ComputableInPolyTime words input (encList encBits)) :
ComputableInPolyTime (fun (a : α) => Cslib.BitString.listXor (width a) (words a)) input encBits

Variable-width XOR uses runtime lookup and ordinary Boolean folds.

theorem Turing.MultiTapeTM.ComputableInPolyTime.bitsWordLt {α : Type} {input : α ↪ List Bool} {x y : α → List Bool} (hx : ComputableInPolyTime x input encBits) (hy : ComputableInPolyTime y input encBits) :
ComputableInPolyTime (fun (c : α) => Cslib.BitString.wordLt (x c) (y c)) input encBool

Lexicographic comparison of equal-length words is polynomial-time.

Decode arbitrary most-significant-first bit words to binary naturals.