Depth-first recursion with an explicit stack #
A recursion scheme (Cslib.Algorithms.StackRecursion.Scheme) evaluates node descriptions x : X
to values y : Y without reference to any machine model. A node is either a leaf, evaluated by
leaf, or a call, which keeps an accumulator a : A: starting from start x, it asks next x a
for its next child, folds the child's value into the accumulator with absorb, and when next
returns none produces its value with finish. Children may depend on the values of earlier
children through the accumulator.
The explicit-stack transition Scheme.step keeps the open calls with their accumulators, innermost
first, together with a register holding either a pending call or a returned value (State). Each
step performs one call or one return. A derivation Scheme.Eval I x y n h, with n nodes and at
most h open calls, reaches y in 2 * n - 1 steps (Scheme.Eval.run), through states with at
most h open calls whose nodes, frames and values satisfy the invariant I
(Scheme.Eval.good_iterate). When 2 * n - 1 ≤ fuel a, the clocked run Scheme.run returns
some y (Scheme.run_eq_some).
Multi-tape Turing machine polynomial-time step certificates and native workspace/time bounds for
this scheme live in CslibFrontier.Computability.GenericRAM.Native.StackRecursion.
Main definitions #
Cslib.Algorithms.StackRecursion.Scheme: an explicit-stack recursion scheme.Cslib.Algorithms.StackRecursion.Invariant: predicates on the nodes, frames and values met.Cslib.Algorithms.StackRecursion.State,Cslib.Algorithms.StackRecursion.State.returned: the explicit-stack state and its returned-value register.Cslib.Algorithms.StackRecursion.Scheme.Loop,Cslib.Algorithms.StackRecursion.Scheme.Eval,Cslib.Algorithms.StackRecursion.Scheme.Good: derivations of the loop of a call and of the value of a node, with their node counts and stack depths, and the state invariant.Cslib.Algorithms.StackRecursion.Scheme.resume,Cslib.Algorithms.StackRecursion.Scheme.step,Cslib.Algorithms.StackRecursion.Scheme.run: the stack transition and its clocked run.
Main results #
Cslib.Algorithms.StackRecursion.Scheme.Loop.child: a call's loop absorbs an evaluated child.Cslib.Algorithms.StackRecursion.Scheme.Loop.run,Cslib.Algorithms.StackRecursion.Scheme.Eval.run: a loop withnchild nodes runs in2 * nsteps, and a derivation withnnodes reaches its value in2 * n - 1steps.Cslib.Algorithms.StackRecursion.Scheme.Loop.good,Cslib.Algorithms.StackRecursion.Scheme.Eval.good,Cslib.Algorithms.StackRecursion.Scheme.Eval.good_iterate: every state reached along a run satisfies the stack-depth and node/frame/value invariant.Cslib.Algorithms.StackRecursion.Scheme.run_eq_some: enough fuel returnssome y.
References #
- OpenAI, The Exponential Time Hypothesis is False, Appendix D.4.7 (p. 64).
A recursion scheme. A node x is a leaf when isLeaf x, with value leaf x. Otherwise
it is a call: its accumulator starts at start x; while next x a returns a child, the child is
evaluated and its value folded into the accumulator by absorb; when next x a is none the
call returns finish x a.
- isLeaf : X → Bool
Whether a node is a leaf.
- leaf : X → Y
The value of a leaf.
- start : X → A
The initial accumulator of a call.
- next : X → A → Option X
The next child of a call with the given accumulator, or
nonewhen it is done. - absorb : X → A → Y → A
Fold the value of the current child into the accumulator.
- finish : X → A → Y
The value of a call from its final accumulator.
Predicates on what a derivation meets: the nodes evaluated, the frames pushed (a call with its accumulator while one of its children is evaluated) and the values returned.
A state of the stack machine: the open calls with their accumulators, innermost first, a pending call and a returned value.
The returned-value register of an explicit-stack state.
One step of the stack machine. A pending leaf is evaluated; a pending call is started; a returned value is absorbed by the innermost open call, which then continues. A value returned with no open call is final.
The loop of a call. R.Loop I x a a' n h: the call x, from accumulator a, evaluates
its remaining children with n nodes in total and ends with accumulator a', using at most h
stack levels above its own frame. Every frame pushed, child evaluated and value returned
satisfies I.
- done {X A Y : Type} {R : Scheme X A Y} {I : Invariant X A Y} {x : X} {a : A} : R.next x a = none → R.Loop I x a a 0 0
The call has no further child.
- leaf {X A Y : Type} {R : Scheme X A Y} {I : Invariant X A Y} {x c : X} {a a' : A} {n h : ℕ} : I.frame x a →
R.next x a = some c →
I.node c →
R.isLeaf c = true → I.value (R.leaf c) → R.Loop I x (R.absorb x a (R.leaf c)) a' n h → R.Loop I x a a' (n + 1) h
The next child is a leaf.
- call {X A Y : Type} {R : Scheme X A Y} {I : Invariant X A Y} {x c : X} {a b a' : A} {n₁ h₁ n₂ h₂ : ℕ} : I.frame x a →
R.next x a = some c →
I.node c →
R.isLeaf c = false →
R.Loop I c (R.start c) b n₁ h₁ →
I.value (R.finish c b) →
R.Loop I x (R.absorb x a (R.finish c b)) a' n₂ h₂ → R.Loop I x a a' (n₁ + 1 + n₂) (max (h₁ + 1) h₂)
The next child is a call, whose own loop ends with accumulator
b.
A derivation of the value of a node. R.Eval I x y n h: the node x has value y,
computed with n nodes and at most h open calls, and every node, frame and value met satisfies
I. A leaf has one node and no open call; a call has one more node and one more level than its
loop.
The loop absorbs an evaluated child.
The machine follows the derivation #
The states met #
The states of an evaluation are good, up to and including its returned value.