Documentation

Complexity.Computability.Machines.Turing.MultiTape.Complexity.PolyTime.Nat.Binary

Polynomial-time arithmetic on binary naturals #

Polynomial-time certificates for arithmetic on the binary encoding Turing.MultiTapeTM.encNat (digits least significant first), the binary counterparts of the unary certificates in Complexity/Computability/Machines/Turing/MultiTape/Complexity/PolyTime/Nat.lean. Addition and multiplication are Christian Reitwiessner's degree-bounded machines (Turing.MultiTapeTM.computableTidilyInDegree_add, Turing.MultiTapeTM.computableTidilyInDegree_mul). A list is summed by a left fold whose accumulator grows by at most one digit more than the length of the element added. Powers of two are computed from a unary exponent by repeated doubling. Strict comparison is the linear-time zip transducer of Turing.MultiTapeTM.computableInTimeAndSpace_natLt. The verifiers and reductions of the number problems (Cslib.Complexity.SubsetSum and its relatives) are built from these.

Truncated subtraction is schoolbook subtraction: a left fold over the digit pairs of both operands keeps a borrow and appends one difference digit per position. A final borrow means that the difference is negative; otherwise the digits are read back as a natural (Turing.MultiTapeTM.ofBits, certified by Turing.MultiTapeTM.Decoder.nat), dropping trailing zeros. General powers to a unary exponent are computed by repeated multiplication.

Main definitions #

Main results #

theorem Nat.size_add_le (m n : ℕ) :
(m + n).size ≤ m.size + n.size + 1

The binary length of a sum is at most the binary lengths of the summands together, plus one.

theorem Nat.size_add_le_max (m n : ℕ) :
(m + n).size ≤ max m.size n.size + 1

The binary length of a sum is at most one more than the longer binary length of the summands.

theorem Nat.size_sum_le {l : List ℕ} {b : ℕ} (h : ∀ n ∈ l, n.size ≤ b) :

A sum of naturals of binary length at most b has binary length at most b plus the binary length of the number of summands.

theorem Nat.size_le_self (n : ℕ) :
n.size ≤ n

A binary length is at most the number itself.

Decode arbitrary little-endian bit words to binary naturals.

The binary digits of a natural number, as a raw bit string.

theorem Turing.MultiTapeTM.ComputableTidilyInPolyTime.binary_add' {α : Type} {input : α ↪ List Bool} {left right : α → ℕ} (hleft : ComputableTidilyInPolyTime left input encNat) (hright : ComputableTidilyInPolyTime right input encNat) :
ComputableTidilyInPolyTime (fun (a : α) => left a + right a) input encNat

Add computed binary values, fixing their intermediate encodings for proof search.

The sum of a list of binary naturals, by a left fold of binary additions.

2 ^ k in binary, from k in unary, by doubling.

Strict comparison of binary naturals, from the linear zip-transducer certificate.

theorem Turing.MultiTapeTM.ComputableTidilyInPolyTime.binary_lt' {α : Type} {input : α ↪ List Bool} {left right : α → ℕ} (hleft : ComputableTidilyInPolyTime left input encNat) (hright : ComputableTidilyInPolyTime right input encNat) :
ComputableTidilyInPolyTime (fun (a : α) => decide (left a < right a)) input encBool

Strictly compare computed binary values, fixing their intermediate encodings.

theorem Turing.MultiTapeTM.ComputableTidilyInPolyTime.binary_le' {α : Type} {input : α ↪ List Bool} {left right : α → ℕ} (hleft : ComputableTidilyInPolyTime left input encNat) (hright : ComputableTidilyInPolyTime right input encNat) :
ComputableTidilyInPolyTime (fun (a : α) => decide (left a ≤ right a)) input encBool

Compare computed binary values, fixing their intermediate encodings.

theorem Turing.MultiTapeTM.ComputableTidilyInPolyTime.binary_mul' {α : Type} {input : α ↪ List Bool} {left right : α → ℕ} (hleft : ComputableTidilyInPolyTime left input encNat) (hright : ComputableTidilyInPolyTime right input encNat) :
ComputableTidilyInPolyTime (fun (a : α) => left a * right a) input encNat

Multiply computed binary values, fixing their intermediate encodings for proof search.

A power of a binary natural to a unary exponent, by repeated multiplication.

theorem Turing.MultiTapeTM.ComputableTidilyInPolyTime.binary_pow' {α : Type} {input : α ↪ List Bool} {base exponent : α → ℕ} (hbase : ComputableTidilyInPolyTime base input encNat) (hexponent : ComputableTidilyInPolyTime exponent input encUnary) :
ComputableTidilyInPolyTime (fun (a : α) => base a ^ exponent a) input encNat

Raise a computed binary value to a computed unary exponent, fixing the intermediate encodings.

Truncated subtraction of binary naturals, reusing the shared plain schoolbook certificate.

theorem Turing.MultiTapeTM.ComputableTidilyInPolyTime.binary_sub' {α : Type} {input : α ↪ List Bool} {left right : α → ℕ} (hleft : ComputableTidilyInPolyTime left input encNat) (hright : ComputableTidilyInPolyTime right input encNat) :
ComputableTidilyInPolyTime (fun (a : α) => left a - right a) input encNat

Subtract computed binary values, truncating at zero and fixing their intermediate encodings.

Read the bit of a computed binary number at a computed unary position.

Any zero-preserving bitwise operation on binary naturals is polynomial-time.

theorem Turing.MultiTapeTM.ComputableInPolyTime.binary_and' {α : Type} {input : α ↪ List Bool} {left right : α → ℕ} (hleft : ComputableInPolyTime left input encNat) (hright : ComputableInPolyTime right input encNat) :
ComputableInPolyTime (fun (a : α) => left a &&& right a) input encNat

Bitwise AND of computed binary naturals.

theorem Turing.MultiTapeTM.ComputableInPolyTime.binary_or' {α : Type} {input : α ↪ List Bool} {left right : α → ℕ} (hleft : ComputableInPolyTime left input encNat) (hright : ComputableInPolyTime right input encNat) :
ComputableInPolyTime (fun (a : α) => left a ||| right a) input encNat

Bitwise OR of computed binary naturals.

theorem Turing.MultiTapeTM.ComputableInPolyTime.binary_xor' {α : Type} {input : α ↪ List Bool} {left right : α → ℕ} (hleft : ComputableInPolyTime left input encNat) (hright : ComputableInPolyTime right input encNat) :
ComputableInPolyTime (fun (a : α) => left a ^^^ right a) input encNat

Bitwise XOR of computed binary naturals.

The smaller of a unary natural and a binary natural, returned in unary by saturating binary decoding.

theorem Turing.MultiTapeTM.ComputableInPolyTime.binary_min_unary' {α : Type} {input : α ↪ List Bool} {left right : α → ℕ} (hleft : ComputableInPolyTime left input encUnary) (hright : ComputableInPolyTime right input encNat) :
ComputableInPolyTime (fun (a : α) => min (left a) (right a)) input encUnary

Cap a computed binary natural by a computed unary bound, returning the result in unary.

Right shift of binary naturals, by dropping at most the dividend's bit length of low-order digits.

theorem Turing.MultiTapeTM.ComputableInPolyTime.binary_shiftRight' {α : Type} {input : α ↪ List Bool} {left right : α → ℕ} (hleft : ComputableInPolyTime left input encNat) (hright : ComputableInPolyTime right input encNat) :
ComputableInPolyTime (fun (a : α) => left a >>> right a) input encNat

Right shift of computed binary naturals.

theorem Turing.MultiTapeTM.ComputableInPolyTime.binary_lt' {α : Type} {input : α ↪ List Bool} {left right : α → ℕ} (hleft : ComputableInPolyTime left input encNat) (hright : ComputableInPolyTime right input encNat) :
ComputableInPolyTime (fun (a : α) => decide (left a < right a)) input encBool

The strict order of two computed numbers in binary.

Plain polynomial-time automation #

2 ^ k in binary, from k in unary, by doubling.

theorem Turing.MultiTapeTM.ComputableInPolyTime.binary_sub' {α : Type} {input : α ↪ List Bool} {left right : α → ℕ} (hleft : ComputableInPolyTime left input encNat) (hright : ComputableInPolyTime right input encNat) :
ComputableInPolyTime (fun (a : α) => left a - right a) input encNat

Subtract computed binary values using the shared plain certificate.

theorem Turing.MultiTapeTM.ComputableInPolyTime.binary_pow' {α : Type} {input : α ↪ List Bool} {base exponent : α → ℕ} (hbase : ComputableInPolyTime base input encNat) (hexponent : ComputableInPolyTime exponent input encUnary) :
ComputableInPolyTime (fun (a : α) => base a ^ exponent a) input encNat

Turing.MultiTapeTM.ComputableTidilyInPolyTime.binary_pow' for plain certificates.