Documentation

CslibFrontier.Algorithms.Recursion.StackScheme

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 #

Main results #

References #

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 none when 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.

  • node : X → Prop

    Every node evaluated satisfies node.

  • frame : X → A → Prop

    Every pushed frame satisfies frame.

  • value : Y → Prop

    Every returned value satisfies value.

@[reducible, inline]

A state of the stack machine: the open calls with their accumulators, innermost first, a pending call and a returned value.

@[reducible, inline]

The returned-value register of an explicit-stack state.

def Cslib.Algorithms.StackRecursion.Scheme.resume {X A Y : Type} (R : Scheme X A Y) (x : X) (a : A) (st : List (X × A)) :
State X A Y

Continue the call x with accumulator a above the stack st: push its frame and call its next child, or return its value when it has none.

def Cslib.Algorithms.StackRecursion.Scheme.step {X A Y : Type} (R : Scheme X A Y) :
State X A Y → State X A Y

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.

theorem Cslib.Algorithms.StackRecursion.Scheme.resume_of_next_none {X A Y : Type} (R : Scheme X A Y) {x : X} {a : A} (st : List (X × A)) (h : R.next x a = none) :
R.resume x a st = (st, none, some (R.finish x a))
theorem Cslib.Algorithms.StackRecursion.Scheme.resume_of_next_some {X A Y : Type} (R : Scheme X A Y) {x c : X} {a : A} (st : List (X × A)) (h : R.next x a = some c) :
R.resume x a st = ((x, a) :: st, some c, none)
theorem Cslib.Algorithms.StackRecursion.Scheme.step_leaf {X A Y : Type} (R : Scheme X A Y) {x : X} (st : List (X × A)) (r : Option Y) (h : R.isLeaf x = true) :
R.step (st, some x, r) = (st, none, some (R.leaf x))
theorem Cslib.Algorithms.StackRecursion.Scheme.step_call {X A Y : Type} (R : Scheme X A Y) {x : X} (st : List (X × A)) (r : Option Y) (h : R.isLeaf x = false) :
R.step (st, some x, r) = R.resume x (R.start x) st
theorem Cslib.Algorithms.StackRecursion.Scheme.step_return {X A Y : Type} (R : Scheme X A Y) (x : X) (a : A) (st : List (X × A)) (y : Y) :
R.step ((x, a) :: st, none, some y) = R.resume x (R.absorb x a y) st
@[simp]

A value returned with no open call is final.

inductive Cslib.Algorithms.StackRecursion.Scheme.Loop {X A Y : Type} (R : Scheme X A Y) (I : Invariant X A Y) :
X → A → A → ℕ → ℕ → Prop

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.

def Cslib.Algorithms.StackRecursion.Scheme.Eval {X A Y : Type} (R : Scheme X A Y) (I : Invariant X A Y) (x : X) (y : Y) (n h : ℕ) :

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.

theorem Cslib.Algorithms.StackRecursion.Scheme.Eval.leaf {X A Y : Type} {R : Scheme X A Y} {I : Invariant X A Y} {x : X} (hx : I.node x) (hl : R.isLeaf x = true) (hv : I.value (R.leaf x)) :
R.Eval I x (R.leaf x) 1 0

A leaf is evaluated by leaf.

theorem Cslib.Algorithms.StackRecursion.Scheme.Eval.call {X A Y : Type} {R : Scheme X A Y} {I : Invariant X A Y} {x : X} {b : A} {n h : ℕ} (hx : I.node x) (hl : R.isLeaf x = false) (hloop : R.Loop I x (R.start x) b n h) (hv : I.value (R.finish x b)) :
R.Eval I x (R.finish x b) (n + 1) (h + 1)

A call is evaluated by its loop, from start, and finish.

theorem Cslib.Algorithms.StackRecursion.Scheme.Loop.child {X A Y : Type} {R : Scheme X A Y} {I : Invariant X A Y} {x c : X} {a a' : A} {y : Y} {n₁ h₁ n₂ h₂ : ℕ} (hf : I.frame x a) (hn : R.next x a = some c) (hc : R.Eval I c y n₁ h₁) (hrest : R.Loop I x (R.absorb x a y) a' n₂ h₂) :
R.Loop I x a a' (n₁ + n₂) (max h₁ h₂)

The loop absorbs an evaluated child.

theorem Cslib.Algorithms.StackRecursion.Scheme.Eval.one_le {X A Y : Type} {R : Scheme X A Y} {I : Invariant X A Y} {x : X} {y : Y} {n h : ℕ} (he : R.Eval I x y n h) :
1 ≤ n

Every derivation has at least one node.

The machine follows the derivation #

theorem Cslib.Algorithms.StackRecursion.Scheme.Loop.run {X A Y : Type} {R : Scheme X A Y} {I : Invariant X A Y} {x : X} {a a' : A} {n h : ℕ} (hl : R.Loop I x a a' n h) (st : List (X × A)) :
R.step^[2 * n] (R.resume x a st) = (st, none, some (R.finish x a'))

A loop is run in two steps per node, ending with the value of the call returned to the stack below it.

theorem Cslib.Algorithms.StackRecursion.Scheme.Eval.run {X A Y : Type} {R : Scheme X A Y} {I : Invariant X A Y} {x : X} {y : Y} {n h : ℕ} (he : R.Eval I x y n h) (st : List (X × A)) :
R.step^[2 * n - 1] (st, some x, none) = (st, none, some y)

A node with n nodes is evaluated in 2 * n - 1 steps, above any stack.

The states met #

def Cslib.Algorithms.StackRecursion.Scheme.Good {X A Y : Type} (I : Invariant X A Y) (H : ℕ) (σ : State X A Y) :

A state is good for I and a depth H when at most H calls are open, every open frame satisfies I, and the register holds either a pending node or a returned value satisfying I.

theorem Cslib.Algorithms.StackRecursion.Scheme.Good.mono {X A Y : Type} {I : Invariant X A Y} {H H' : ℕ} {σ : State X A Y} (hσ : Good I H σ) (h : H ≤ H') :
Good I H' σ
theorem Cslib.Algorithms.StackRecursion.Scheme.Loop.good {X A Y : Type} {R : Scheme X A Y} {I : Invariant X A Y} {x : X} {a a' : A} {n h : ℕ} (hl : R.Loop I x a a' n h) {st : List (X × A)} (hst : ∀ f ∈ st, I.frame f.1 f.2) (j : ℕ) :
j < 2 * n → Good I (st.length + h + 1) (R.step^[j] (R.resume x a st))

The states of a loop are good, strictly before it returns.

theorem Cslib.Algorithms.StackRecursion.Scheme.Eval.good {X A Y : Type} {R : Scheme X A Y} {I : Invariant X A Y} {x : X} {y : Y} {n h : ℕ} (he : R.Eval I x y n h) {st : List (X × A)} (hst : ∀ f ∈ st, I.frame f.1 f.2) (j : ℕ) :
j ≤ 2 * n - 1 → Good I (st.length + h) (R.step^[j] (st, some x, none))

The states of an evaluation are good, up to and including its returned value.

theorem Cslib.Algorithms.StackRecursion.Scheme.Eval.good_iterate {X A Y : Type} {R : Scheme X A Y} {I : Invariant X A Y} {x : X} {y : Y} {n h : ℕ} (he : R.Eval I x y n h) (j : ℕ) :

Every state of a run from an empty stack is good, including the final state, which no step changes.

def Cslib.Algorithms.StackRecursion.Scheme.run {X A Y : Type} (R : Scheme X A Y) {I : Type} (fuel : I → ℕ) (root : I → X) (a : I) :

The clocked run: the register after fuel a steps from the root root a on an empty stack.

theorem Cslib.Algorithms.StackRecursion.Scheme.run_eq_some {X A Y : Type} {R : Scheme X A Y} {I : Type} {fuel : I → ℕ} {root : I → X} {Inv : Invariant X A Y} {a : I} {y : Y} {N H : ℕ} (he : R.Eval Inv (root a) y N H) (hfuel : 2 * N - 1 ≤ fuel a) :
R.run fuel root a = some y

Enough fuel gives the value.