Documentation

CslibFrontier.Algorithms.Recursion.StackRecursionTree

Derivations of stack recursions from their recursion trees #

A derivation Cslib.Algorithms.StackRecursion.Scheme.Eval of a recursion scheme records the value of a node with its number of nodes and its stack depth. This file builds derivations from a description of the recursion tree, and reads off what a clocked run returns, without reference to any machine model.

Counted children (Scheme.Loop.counted). A call whose accumulators acc c ask for the children child c for c < bound, and then stop, runs its loop through the children's derivations.

The recursion tree (Scheme.exists_eval_of_counted). Let value assign values to the nodes satisfying I.node, such that a leaf has its leaf value, and a call at depth d < D has at most b d counted children, all at depth d + 1 and smaller in a measure, with its value the finish of the accumulated children's values. Then every node at depth d ≤ D has a derivation of its value, with at most Nat.recursionBound b (D - d) d nodes and at most D - d stack levels. This is the induction "from the leaves" of a recursive algorithm, with its node count; without the count, Scheme.exists_eval_of_measure is the induction alone.

What a run returns (Scheme.value_of_run_eq_some). Every value the clocked run returns, for any fuel, satisfies the invariant of a derivation of the root. If the values carry a tag that only the root's value has, a returned tagged value is the root's value, even when the fuel is too small to vouch for the run.

Main results #

References #

theorem Cslib.Algorithms.StackRecursion.Scheme.Loop.counted {X A Y : Type} {R : Scheme X A Y} {I : Invariant X A Y} {x : X} {acc : ℕ → A} {child : ℕ → X} {val : ℕ → Y} {nodes height : ℕ → ℕ} (bound H : ℕ) (hnext : ∀ c < bound, R.next x (acc c) = some (child c)) (hdone : R.next x (acc bound) = none) (habsorb : ∀ c < bound, R.absorb x (acc c) (val c) = acc (c + 1)) (heval : ∀ c < bound, R.Eval I (child c) (val c) (nodes c) (height c)) (hframe : ∀ c < bound, I.frame x (acc c)) (hH : ∀ c < bound, height c ≤ H) :
∃ h ≤ H, R.Loop I x (acc 0) (acc bound) (∑ c ∈ Finset.range bound, nodes c) h

A call with counted children. If, from the accumulators acc c, the call x asks for the children child c for c < bound and then stops, and the children evaluate to val c, then its loop from acc 0 ends at acc bound, with the children's nodes and at most H levels.

theorem Cslib.Algorithms.StackRecursion.Scheme.exists_eval_of_measure {X A Y : Type} {R : Scheme X A Y} {I : Invariant X A Y} (μ : X → ℕ) (value : X → Y) (hleaf : ∀ (x : X), I.node x → R.isLeaf x = true → value x = R.leaf x) (hcall : ∀ (x : X), I.node x → R.isLeaf x = false → ∃ (cnt : ℕ) (acc : ℕ → A) (child : ℕ → X), acc 0 = R.start x ∧ (∀ c < cnt, R.next x (acc c) = some (child c)) ∧ R.next x (acc cnt) = none ∧ (∀ c < cnt, R.absorb x (acc c) (value (child c)) = acc (c + 1)) ∧ (∀ c < cnt, I.frame x (acc c) ∧ I.node (child c) ∧ μ (child c) < μ x) ∧ value x = R.finish x (acc cnt)) (hvalue : ∀ (x : X), I.node x → I.value (value x)) (x : X) (hx : I.node x) :
∃ (n : ℕ) (h : ℕ), R.Eval I x (value x) n h

Derivations by well-founded recursion. Let value assign values to the nodes satisfying I.node, all satisfying I.value, such that a leaf has its leaf value, and a call has counted children: from the accumulators acc c, starting at start, it asks for child c for c < cnt and then stops, absorbing the children's values; the frames satisfy I.frame, the children satisfies I.node and are smaller in the measure μ; and its value is the finish of the last accumulator. Then every node has a derivation of its value. exists_eval_of_counted adds the node count.

theorem Cslib.Algorithms.StackRecursion.Scheme.exists_eval_of_counted {X A Y : Type} {R : Scheme X A Y} {I : Invariant X A Y} (μ depth : X → ℕ) (value : X → Y) (b : ℕ → ℕ) (D : ℕ) (hleaf : ∀ (x : X), I.node x → R.isLeaf x = true → value x = R.leaf x) (hcall : ∀ (x : X), I.node x → R.isLeaf x = false → depth x < D ∧ ∃ (cnt : ℕ) (acc : ℕ → A) (child : ℕ → X), cnt ≤ b (depth x) ∧ acc 0 = R.start x ∧ (∀ c < cnt, R.next x (acc c) = some (child c)) ∧ R.next x (acc cnt) = none ∧ (∀ c < cnt, R.absorb x (acc c) (value (child c)) = acc (c + 1)) ∧ (∀ c < cnt, I.frame x (acc c) ∧ I.node (child c) ∧ μ (child c) < μ x ∧ depth (child c) = depth x + 1) ∧ value x = R.finish x (acc cnt)) (hvalue : ∀ (x : X), I.node x → I.value (value x)) (x : X) (hx : I.node x) (hxD : depth x ≤ D) :
∃ (n : ℕ) (h : ℕ), R.Eval I x (value x) n h ∧ n ≤ Nat.recursionBound b (D - depth x) (depth x) ∧ h ≤ D - depth x

Derivations from the recursion tree. Let value assign values to the nodes satisfying I.node, all satisfying I.value, such that

  • a leaf has its leaf value;
  • a call at depth d has d < D and at most b d children, counted: from the accumulators acc c, starting at start, it asks for child c for c < cnt and then stops, absorbing the children's values; the frames satisfy I.frame, and the children satisfy I.node, are smaller in the measure μ and lie at depth d + 1; and its value is the finish of the last accumulator.

Then every node at depth d ≤ D has a derivation of its value with at most Nat.recursionBound b (D - d) d nodes and at most D - d stack levels.

theorem Cslib.Algorithms.StackRecursion.Scheme.exists_eval_of_height {X A Y : Type} {R : Scheme X A Y} {I : Invariant X A Y} (μ depth : X → ℕ) (value : X → Y) (D : ℕ) (hleaf : ∀ (x : X), I.node x → R.isLeaf x = true → value x = R.leaf x) (hcall : ∀ (x : X), I.node x → R.isLeaf x = false → depth x < D ∧ ∃ (cnt : ℕ) (acc : ℕ → A) (child : ℕ → X), acc 0 = R.start x ∧ (∀ c < cnt, R.next x (acc c) = some (child c)) ∧ R.next x (acc cnt) = none ∧ (∀ c < cnt, R.absorb x (acc c) (value (child c)) = acc (c + 1)) ∧ (∀ c < cnt, I.frame x (acc c) ∧ I.node (child c) ∧ μ (child c) < μ x ∧ depth (child c) = depth x + 1) ∧ value x = R.finish x (acc cnt)) (hvalue : ∀ (x : X), I.node x → I.value (value x)) (x : X) (hx : I.node x) (hxD : depth x ≤ D) :
∃ (n : ℕ) (h : ℕ), R.Eval I x (value x) n h ∧ h ≤ D - depth x

Derivations with their height. Let value assign values to the nodes satisfying I.node, all satisfying I.value, such that a leaf has its leaf value, and a call at depth d has d < D and counted children, as in exists_eval_of_counted, at depth d + 1. Then every node at depth d ≤ D has a derivation of its value with at most D - d stack levels. This is exists_eval_of_counted without a bound on the number of children.

theorem Cslib.Algorithms.StackRecursion.Scheme.value_of_run_eq_some {X A Y : Type} {R : Scheme X A Y} {I : Invariant X A Y} {J : Type} {fuel : J → ℕ} {root : J → X} {a : J} {y y' : Y} {N H : ℕ} (he : R.Eval I (root a) y N H) (hrun : R.run fuel root a = some y') :
I.value y'

What a clocked run returns. For any fuel, a value returned by the run satisfies the invariant of a derivation of the root.