Documentation

CslibFrontier.Computability.Machines.Turing.MultiTape.Complexity.InSpace.QueryLoop

Loops that read their input, within space #

Turing.MultiTapeTM.ComputableInSpace does not charge the read-only input, but the loops of Turing.MultiTapeTM.ComputableInSpace.iterate and Turing.MultiTapeTM.ComputableInSpace.iterateUntil take a step function of the state alone, so a round cannot look at the input. The loop of this module can: a round reads the input bit at a position computed from the state (Turing.MultiTapeTM.queryRound), and the loop runs until a test on the state holds. It is computable within the space of the start state and of the states it passes through, plus the logarithm of the length of the input for the position counter of the underlying query machine (Turing.QueryMachine.computableInSpaceFrom), which the caller no longer declares.

The rule comes in two forms. queryLoop asks for the number of rounds and a bound on the iterates, which suits loops with a closed form. queryLoop_of_invariant asks for an invariant of the states indexed by a variant that decreases in every round, in the style of a total-correctness rule: the invariant bounds the states and determines the result at the first state where the test holds.

The result is the final state and is charged to the budget, so the rule composes with the other budget rules of ComputableInSpace and with the space tactic. A budget logarithmic in the length of the input gives logarithmic space (Turing.MultiTapeTM.ComputableInSpace.computableInLogSpace).

Main definitions #

Main results #

def Turing.MultiTapeTM.queryRound {σ : Type} (query : σ → ℕ) (next : σ × Option Bool → σ) (w : List Bool) (s : σ) :
σ

A round of a loop that reads its input w: read the bit at position query s, none past the end of w, and move to next.

theorem Turing.MultiTapeTM.ComputableInSpace.computableInLogSpace {α β : Type} {enc : α ↪ List Bool} {encB : β ↪ List Bool} {f : α → β} (h : ComputableInSpace f enc encB fun (a : α) => (enc a).length.log2) :

A budget logarithmic in the length of the encoded input gives logarithmic space: the same machine, without the bound on the length of the value.

theorem Turing.MultiTapeTM.ComputableInSpace.queryLoop {α σ : Type} {enc : α ↪ List Bool} {encS : σ ↪ List Bool} {start : α → σ} {query : σ → ℕ} {next : σ × Option Bool → σ} {done : σ → Bool} {n S : α → ℕ} (hstart : ComputableInSpace start enc encS S) (hquery : ComputableInSpace query encS encNat fun (s : σ) => (encS s).length) (hnext : ComputableInSpace next (QueryMachine.encStateRead encS) encS fun (p : σ × Option Bool) => ((QueryMachine.encStateRead encS) p).length) (hdone : ComputableInSpace done encS encBool fun (s : σ) => (encS s).length) (hn : ∀ (a : α), done ((queryRound query next (enc a))^[n a] (start a)) = true) (hlt : ∀ (a : α), ∀ j < n a, done ((queryRound query next (enc a))^[j] (start a)) = false) (hsize : ∃ (c : ℕ), ∀ (a : α), ∀ j ≤ n a, (encS ((queryRound query next (enc a))^[j] (start a))).length ≤ c * (S a + (enc a).length.log2 + 1)) :
ComputableInSpace (fun (a : α) => (queryRound query next (enc a))^[n a] (start a)) enc encS fun (a : α) => S a + (enc a).length.log2

Iterating a round that reads the input until a test holds. From the state start a, a round reads the input bit at the position query s and moves to next, until done. If the start state is computable within the budget S, the parts of a round within space linear in the encoded state, and the states of the loop have encodings of length O(S + log n) for inputs of length n, then the final state is computable within space O(S + log n). The number of rounds n a, the first at which the test holds, need not be computable, and the input is not charged.

theorem Turing.MultiTapeTM.ComputableInSpace.queryLoop_of_invariant {α σ : Type} {enc : α ↪ List Bool} {encS : σ ↪ List Bool} {start : α → σ} {query : σ → ℕ} {next : σ × Option Bool → σ} {done : σ → Bool} {S : α → ℕ} {f : α → σ} (Inv : α → ℕ → σ → Prop) (hstart : ComputableInSpace start enc encS S) (hquery : ComputableInSpace query encS encNat fun (s : σ) => (encS s).length) (hnext : ComputableInSpace next (QueryMachine.encStateRead encS) encS fun (p : σ × Option Bool) => ((QueryMachine.encStateRead encS) p).length) (hdone : ComputableInSpace done encS encBool fun (s : σ) => (encS s).length) (hinit : ∀ (a : α), ∃ (m : ℕ), Inv a m (start a)) (hround : ∀ (a : α) (m : ℕ) (s : σ), Inv a m s → done s = false → ∃ m' < m, Inv a m' (queryRound query next (enc a) s)) (hf : ∀ (a : α) (m : ℕ) (s : σ), Inv a m s → done s = true → s = f a) (hsize : ∃ (c : ℕ), ∀ (a : α) (m : ℕ) (s : σ), Inv a m s → (encS s).length ≤ c * (S a + (enc a).length.log2 + 1)) :
ComputableInSpace f enc encS fun (a : α) => S a + (enc a).length.log2

A loop that reads the input, from an invariant with a variant. As Turing.MultiTapeTM.ComputableInSpace.queryLoop, with the number of rounds and the bound on the iterates replaced by an invariant Inv a m s of the states s on the input a, indexed by a variant m: it holds at the start, a round from a state where the test fails keeps it with a smaller variant, it bounds the encoded state by O(S + log n), and where the test holds it determines the state as f a. Then f is computable within space O(S + log n).