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 #
Turing.MultiTapeTM.ComputableInSpace.loopWith: a loop whose body is any function of stored registers and the input, within the space of the body and of the registers.Turing.MultiTapeTM.ComputableInSpace.loopWith_of_invariant: the same from an invariant with a decreasing variant, which gives termination, the bound on the body and the result.Turing.MultiTapeTM.ComputableInSpace.comp_logSpace_context: a budgeted function after a logspace rearrangement of registers and context, in context form.Turing.MultiTapeTM.ComputableInLogSpace.inSpace_context: a logspace function of registers and the input with a short value, in context form.
Implementation notes #
The loop machine, its state and its invariant are private to this module.
The rounds of the loop #
The invariant #
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.
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.
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.
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).