Documentation

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

Loops whose body is any function of stored registers and the input #

Turing.MultiTapeTM.ComputableInSpace charges the value of a function but not its read-only input. A function of stored registers r and a read-only context a is therefore a function on pairs, with the registers first, within a budget that charges only the registers:

ComputableInSpace f (encPair encR encA) encB fun p => (encR p.1).length + S p.2.

This module calls that the context form. The registers come first because encPair frames its first component: the encoded pair is the framed registers followed by the context verbatim.

The main result runs such a function with the context on the input tape and the registers in the state of a query machine, repeatedly: Turing.MultiTapeTM.ComputableInSpace.loopWith is a loop whose body step : ρ × α → Option ρ is any certified function of the pair, in context form or within any other budget, with no restriction on how it reads the input. The loop machine simulates the machine of the body, in the representation of Turing.VirtualWord.Native.machine, on a virtual input: a read inside the framed registers is served from the state, and a read past them is a read of the real input, shifted. The output of the body is collected in the state and becomes the registers of the next round. So the space is that of the body, of the registers and of a position counter, whatever the number of rounds; the constant does not depend on it either. Composition by recomputation (Turing.MultiTapeTM.ComputableInTimeAndSpace.comp_space), by contrast, multiplies the bound by a constant for every nested stage.

The trajectory of the registers is a hypothesis: the caller names the registers r a j of every round, shows that the body maps each to the next and stops at the last, and bounds the budget of the body on them. Nothing is inferred about the loop, and the budget of the body is not used on registers that the loop does not hold.

Main results #

Implementation notes #

The loop machine, its state and its invariant are private to this module.

The rounds of the loop #

The invariant #

theorem Turing.MultiTapeTM.log2_length_encPair_le {α ρ : Type} (encR : ρ ↪ List Bool) (encA : α ↪ List Bool) (p : ρ × α) :
((encPair encR encA) p).length.log2 ≤ 2 * (encR p.1).length + (encA p.2).length.log2 + 2

The logarithm of the length of an encoded pair is within the length of its first component plus the logarithm of the length of the second.

theorem Turing.MultiTapeTM.ComputableInSpace.comp_logSpace_context {α ρ β γ : Type} {encA : α ↪ List Bool} {encR : ρ ↪ List Bool} {encB : β ↪ List Bool} {encC : γ ↪ List Bool} {g : β → γ} {f : ρ × α → β} {Sg : β → ℕ} {S : α → ℕ} (hg : ComputableInSpace g encB encC Sg) (hf : ComputableInLogSpace f (encPair encR encA) encB) (hS : BigOLe (fun (p : ρ × α) => Sg (f p)) fun (p : ρ × α) => (encR p.1).length + S p.2) (hlog : ∀ (a : α), (encA a).length.log2 ≤ S a) :
ComputableInSpace (fun (p : ρ × α) => g (f p)) (encPair encR encA) encC fun (p : ρ × α) => (encR p.1).length + S p.2

A budgeted function after a logspace rearrangement of registers and context. If g is computable within the budget Sg, f is a logspace function of stored registers and a context, and the budget of g at f p is within the registers plus a budget S of the context that is at least the logarithm of its length, then g ∘ f is in context form with the budget S: the logarithm of the length of the encoded pair, which composition by recomputation (Turing.MultiTapeTM.ComputableInSpace.comp_logSpace) adds, is within the registers plus the logarithm of the length of the context. This substitutes the registers or the context of a function in context form.

theorem Turing.MultiTapeTM.ComputableInLogSpace.inSpace_context {α ρ β : Type} {encA : α ↪ List Bool} {encR : ρ ↪ List Bool} {encB : β ↪ List Bool} {f : ρ × α → β} (h : ComputableInLogSpace f (encPair encR encA) encB) (hlen : BigOLe (fun (p : ρ × α) => (encB (f p)).length) fun (p : ρ × α) => (encR p.1).length + (encA p.2).length.log2) :
ComputableInSpace f (encPair encR encA) encB fun (p : ρ × α) => (encR p.1).length + (encA p.2).length.log2

A logspace function of stored registers and the input, with a short value, in context form. Logarithmic space in the length of the encoded pair is within the registers plus the logarithm of the length of the input, so the function is computable within that budget once its value fits it. This turns the rules of Turing.MultiTapeTM.ComputableInLogSpace, such as projections and composition with a function of the input alone, into bodies of Turing.MultiTapeTM.ComputableInSpace.loopWith.

theorem Turing.MultiTapeTM.ComputableInSpace.loopWith {α ρ : Type} {encA : α ↪ List Bool} {encR : ρ ↪ List Bool} {step : ρ × α → Option ρ} {init : α → ρ} {S B : α → ℕ} {T : ρ × α → ℕ} (r : α → ℕ → ρ) (n : α → ℕ) (hinit : ComputableInSpace init encA encR S) (hstep : ComputableInSpace step (encPair encR encA) (encOption encR) T) (h0 : ∀ (a : α), r a 0 = init a) (hsucc : ∀ (a : α), ∀ j < n a, step (r a j, a) = some (r a (j + 1))) (hstop : ∀ (a : α), step (r a (n a), a) = none) (hT : ∀ (a : α), ∀ j ≤ n a, T (r a j, a) ≤ B a) :
ComputableInSpace (fun (a : α) => r a (n a)) encA encR fun (a : α) => S a + B a + (encA a).length.log2

A loop whose body is any function of stored registers and the input. The body step : ρ × α → Option ρ takes the registers and the input, and returns the next registers or none to stop. Let it be computable within any budget T of the registers and the input, where the registers are charged and the input is not, and let the registers of the loop from init a be r a 0, …, r a (n a), the body stopping at the last, with T (r a j, a) ≤ B a in every round. Then the final registers are computable within space O(S + B + log n) on inputs of length n: the body is simulated on the virtual input encPair encR encA (r, a), whose framed first component is stored and whose second component is the input itself. The budget of the body is only used on the registers of the run, so it may grow with registers that the loop never holds; the registers themselves are short because the value of the body is charged to its budget.

theorem Turing.MultiTapeTM.ComputableInSpace.loopWith_of_invariant {α ρ : Type} {encA : α ↪ List Bool} {encR : ρ ↪ List Bool} {step : ρ × α → Option ρ} {init f : α → ρ} {S B : α → ℕ} {T : ρ × α → ℕ} (Inv : α → ℕ → ρ → Prop) (hinit : ComputableInSpace init encA encR S) (hstep : ComputableInSpace step (encPair encR encA) (encOption encR) T) (h0 : ∀ (a : α), ∃ (m : ℕ), Inv a m (init a)) (hround : ∀ (a : α) (m : ℕ) (r r' : ρ), Inv a m r → step (r, a) = some r' → ∃ m' < m, Inv a m' r') (hstop : ∀ (a : α) (m : ℕ) (r : ρ), Inv a m r → step (r, a) = none → r = f a) (hT : ∀ (a : α) (m : ℕ) (r : ρ), Inv a m r → T (r, a) ≤ B a) :
ComputableInSpace f encA encR fun (a : α) => S a + B a + (encA a).length.log2

A loop over stored registers and the input, from an invariant with a variant. As Turing.MultiTapeTM.ComputableInSpace.loopWith, with the registers of every round replaced by an invariant Inv a m r of the registers r on the input a, indexed by a variant m: it holds at the start, a round keeps it with a smaller variant, it bounds the budget of the body, and where the body stops it determines the registers as f a. Then f is computable within space O(S + B + log n).