Documentation

CslibFrontier.Computability.Machines.Turing.MultiTape.Complexity.BigO.Constructible

Lengths, comparisons and unary conversion within given bounds #

Routines that compute resource bounds from an input, with computability up to constants (Turing.MultiTapeTM.ComputableInO): the length of a bit word in binary, the comparison of two unary numbers, and conversions between unary and binary. They are the building blocks of constructible resource bounds, of padding arguments and of the diagonal machines of the hierarchy theorems.

The conversions are linear. From unary to binary, a counter is halved once per round and the parity of each nonzero value is emitted; the rounds cost n + n / 2 + ⋯ = O(n). From binary to unary, the digits are read least significant first while a unary place value doubles, and the place value is emitted at each digit one; round i costs O(2 ^ i). The number of binary digits of m is logarithmic in m, so every power of it is O(m + 1); this absorbs binary counters into bounds linear in the counted value.

Main results #

The binary digits of m are logarithmic: 2 ^ (encNat m).length ≤ 2 * (m + 1).

theorem Turing.MultiTapeTM.exists_pow_length_encNat_le (d : ℕ) :
∃ (C : ℕ), ∀ (m : ℕ), ((encNat m).length + 1) ^ d ≤ C * (m + 1)

Every power of the number of binary digits of m is O(m + 1).

Conversions between unary and binary, in linear time #

theorem Turing.MultiTapeTM.ComputableInO.unary_halvings {emit finish : ℕ → List Bool} (hemit : ComputableInO emit encUnary encBits (fun (v : ℕ) => v) fun (v : ℕ) => v) (hfinish : ComputableInO finish encUnary encBits (fun (v : ℕ) => v) fun (v : ℕ) => v) :
ComputableInO (fun (x : ℕ × ℕ) => (List.map (fun (i : ℕ) => emit (x.1 / 2 ^ i)) (List.range x.2)).flatten ++ finish (x.1 / 2 ^ x.2)) (encPair encUnary encUnary) encBits (fun (x : ℕ × ℕ) => x.1 + x.2) fun (x : ℕ × ℕ) => x.1 + x.2

Repeated halving with emission. From (n, m) in unary, round i < m emits emit (n / 2 ^ i) and the last value n / 2 ^ m is finished by finish, within time and space O(n + m) when emit and finish are linear in the value: the rounds cost n + n / 2 + ⋯ ≤ 2 n (Nat.sum_div_two_pow_le).

theorem Turing.MultiTapeTM.ComputableInO.unaryToNat :
ComputableInO (fun (n : ℕ) => n) encUnary encNat (fun (n : ℕ) => n) fun (n : ℕ) => n

From unary to binary, by repeated halving, within linear time and space. Round i halves the counter n / 2 ^ i and emits its parity while it is positive (Nat.flatten_map_range_halfBit), so the rounds cost O(n) in total.

The length of a bit word in binary, within linear time and space: the word is measured in unary and converted by Turing.MultiTapeTM.ComputableInO.unaryToNat.

The comparison of unary numbers, by counting down, within quadratic time and linear space.

theorem Turing.MultiTapeTM.ComputableInO.natToUnary :
ComputableInO (fun (n : ℕ) => n) encNat encUnary (fun (n : ℕ) => n) fun (n : ℕ) => n

From binary to unary, within linear time and space. The digits are read least significant first while a unary place value 2 ^ i doubles, and the place value is emitted at every digit one. Round i costs O(2 ^ i), so the rounds cost O(n + 1) in total.