Recursion over stored frames and the input, within the space of the deepest chain #
A recursive procedure is given by its body, a function of a frame, of the value that a call has just returned to the frame, if any, and of the read-only input. The body answers with a call, the frame to resume when the call returns together with the frame of the callee, or with the value that the frame returns:
body : (φ × Option β) × α → (φ × φ) ⊕ β.
Turing.MultiTapeTM.ComputableInSpace.recWith runs such a body on a stack of frames. The space is
that of the frames saved along one chain of calls, of the body and of a position counter, with a
constant that does not depend on the depth of the recursion: the stack is the registers of one
loop of Turing.MultiTapeTM.ComputableInSpace.loopWith, and a round runs the body once on the
frame in control. Composition by recomputation, by contrast, multiplies the bound by a constant
for every level.
Nothing is inferred about the recursion. The caller gives the value val that every reachable
frame returns, a rank that decreases from a frame to its callee and to its own resumption, and a
potential need: a frame needs at least the budget of the body on it, and a frame that calls
needs its saved frame, one more unit and the need of the callee. With frames of F bits and
depth D the potential (depth + 1) * F gives space O(D * F); a potential that sums frame
sizes decreasing along a chain gives the sum, which may be far smaller.
Main results #
Turing.MultiTapeTM.ComputableInSpace.recWith: the value of a recursion whose body is any function of a frame, a returned value and the input, within the space of the deepest chain of saved frames.
Implementation notes #
The stack, the round of the loop and its invariant are private to this module.
The invariant #
Recursion over stored frames and the input. The body takes a frame, the value that a call
has just returned to it, if any, and the input. It answers .inl (saved, child) to call the frame
child and resume the frame saved with the value of the call, or .inr v to return v. Let
the body be computable within a budget T, and on every input let
Reachhold of the frames that occur, starting from(init a, none);valbe the value that a reachable frame returns: the value of its resumption after a call, and the value that the body returns;rankdecrease from a calling frame to its callee and to its resumption;needbe at least the budget of the body on a reachable frame, and for a calling frame at least its saved frame, one more unit and the need of the callee, and at least the need of its resumption.
Then the value of the frame init a is computable within space O(S + B + log n) on inputs of
length n, where B a bounds the need of the first frame. The constant does not depend on the
depth of the recursion.