Documentation

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

Polynomial-time arithmetic on binary integers #

Polynomial-time certificates for arithmetic on the binary sign-magnitude integers Turing.MultiTapeTM.encIntBin. The sign bit and the magnitude are read off the code directly. Every other operation is an ordinary functional program over the sign and the magnitude, by cases on the signs, built from the binary natural arithmetic of Complexity/Computability/Machines/Turing/MultiTape/Complexity/PolyTime/Nat/Binary.lean; no new machine is involved. A result is assembled from a sign and a magnitude by Turing.MultiTapeTM.ComputableTidilyInPolyTime.intBin_signMagnitude, which clears the sign of a zero magnitude. Integers of equal sign add their magnitudes; otherwise the smaller magnitude is subtracted from the larger one, which keeps its sign. A product multiplies the magnitudes, and is negative when exactly one factor is. Lists are summed and powers taken by left folds, with the binary length bounds of Int.size_natAbs_add_le and Int.size_natAbs_mul_le.

Main results #

The magnitude of a sum is at most one binary digit longer than the longer magnitude of the summands.

The magnitude of a difference is at most one binary digit longer than the longer magnitude of the operands.

The magnitude of a product is at most as long as the magnitudes of the factors together.

theorem Int.size_natAbs_pow_le (a : ℤ) (k : ℕ) :
(a ^ k).natAbs.size ≤ k * a.natAbs.size + 1

The magnitude of a power is at most the exponent times the magnitude of the base, plus one for the zeroth power.

theorem Int.size_natAbs_sum_le {l : List ℤ} {b : ℕ} (h : ∀ z ∈ l, z.natAbs.size ≤ b) :

A sum of integers whose magnitudes have binary length at most b has a magnitude of binary length at most b plus the binary length of the number of summands.

theorem Int.size_natAbs_prod_le (l : List ℤ) :
l.prod.natAbs.size ≤ (List.map (fun (z : ℤ) => z.natAbs.size) l).sum + 1

A product of integers has a magnitude of binary length at most the sum of the binary lengths of the magnitudes of the factors, plus one for the empty product.

The sign of a binary integer: whether it is negative.

Assemble a binary integer from a sign and a binary magnitude. A negative sign on a zero magnitude is dropped, so the code stays unique.

Negation of binary integers flips the sign of a nonzero magnitude.

Addition of binary integers, by cases on the signs of the summands.

Multiplication of binary integers: the magnitudes multiply, and the product is negative when exactly one factor is.

Subtraction of binary integers, as addition of the negation.

Test whether a binary integer is zero: its magnitude has no digits.

Test whether a binary integer is positive.

Strict comparison of binary integers, through the sign of their difference.

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

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

The product of a list of binary integers, by a left fold of binary multiplications.

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

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

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

Subtract computed binary integers, fixing their intermediate encodings.

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

Multiply computed binary integers, fixing their intermediate encodings.

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

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

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

Strictly compare computed binary integers, fixing their intermediate encodings.

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

Compare computed binary integers, fixing their intermediate encodings.

Multiply a uniformly computed family of binary integers over a computed unary range.

theorem Turing.MultiTapeTM.ComputableInPolyTime.intBin_mul' {α : Type} {input : α ↪ List Bool} {left right : α → ℤ} (hleft : ComputableInPolyTime left input encIntBin) (hright : ComputableInPolyTime right input encIntBin) :
ComputableInPolyTime (fun (a : α) => left a * right a) input encIntBin

Multiply computed binary integers, preserving the complete input encoding.

The cast of a binary natural number to a binary integer.