Chapter 29 · Combining search trees · Section 29.2Interruptible workers
ComboBST, the combined search tree of Section 29.1, runs two online BST algorithms and in alternating rounds. A round has a fixed budget of operations. When the budget runs out, the round usually stops in the middle of a request. The combined tree then restores the initial tree, switches to the other algorithm, and re-runs the interrupted request from scratch on that side. To describe this precisely, an input algorithm cannot be a black box that serves one whole request per call. It must be presented so that it can be run one charged unit at a time, stopped at any unit, and either resumed or abandoned. The library calls such a presentation a worker.
This section collects what the library knows about workers as inputs:
- the facts that hold for every worker, whatever algorithm it implements (determinism of a request, budgets and closed rounds);
- the replay adapter, which turns an ordinary online algorithm into a worker, together with its pointer interpretation and the proof that its requests are served correctly;
- the pair construction, which puts two workers with different state types behind one state type, as the controller requires.
The controller that schedules the rounds is the subject of Section 29.3, and the single online pointer execution built from it is the subject of Section 29.4. Both take a worker as their input and assume the hypotheses discussed here. A reader interested only in Theorem 3 can read the statement in Section 29.1 and return here for one question: what must an input algorithm satisfy, and which of those requirements are already proved for ordinary online algorithms? The box below answers the second half.
Let be two online algorithms of the rotation model (Chapter 5) and let be a tree on keys. There is one interruptible replay worker for the pair, fixed before any charges, overhead or constants are chosen, with the following property. Take any overhead , any constants and , and any nonnegative charges (the charge of the -th request of on side ) such that, for every sequence of keys below ,
and, for every contiguous segment ,
Then the worker satisfies the operational well-behavedness contract Analysis.WellBehaved with these parameters. Every step of the worker is either one legal pointer instruction or local bookkeeping, and every valid request it completes from a reachable request boundary moves the cursor over the requested key (Visits.Correct for Replay.model).
The cost on the left of the first inequality is Algorithm.totalCost: one unit per request plus rotations. The factor is the price of the translation to the pointer machine. The single-request bound of well-behavedness ( per access from any reachable state) is not a hypothesis here: the worker caps every request at units.
The Lean statements
theorem algorithms_well_behaved (a : Bool → RotationBST.Algorithm) (saved : Tree) : ∃ planner : Planner (List Nat × Pointer.Lazy.Online.State) saved, ∀ (overhead C₀ C₁ : Nat) (charges : Analysis.Charges), saved.size ≤ overhead → 20 ≤ C₁ → (∀ side xs i, 0 ≤ charges side xs i) → (∀ side xs, (∀ x ∈ xs, x < saved.size) → (64 : ℝ) * (a side).totalCost saved xs ≤ Analysis.total charges side xs + overhead) → (∀ side xs, (∀ x ∈ xs, x < saved.size) → ∀ lo hi, lo ≤ hi → hi ≤ xs.length → Analysis.total charges side (Analysis.segment xs lo hi) + overhead ≤ C₀ * Analysis.mass charges side xs lo hi + C₁ * overhead) → Analysis.WellBehaved (worker planner) saved.size overhead C₀ C₁ charges := … RotationBST.Combo.Worker.Replay.algorithms_well_behaved a savedModels.Rotation, Models.Online, Models.Pointer.Lazy.Online.Definitions, Algorithms.Combination.Analysis.Definitions, Models.Pointer.Machine, Models.Multifinger.Program.Definitions, Models.Multifinger.Program.Replay.Definitions, Models.Labelled.Cursor.Definitions, Models.Labelled.Cursor.Map.Definitions, Models.Labelled.Metadata.Definitions, Models.Labelled.Intervals.Definitions, Structures.TreeBuffer.Nodes.Definitions, Structures.TreeBuffer.Shared.Definitions, Algorithms.Combination.Controller.Definitions, Algorithms.Combination.Worker.Definitions, Algorithms.Combination.Refinement.Definitions, Algorithms.Combination.Refinement.Visits.Definitions, Algorithms.Combination.Worker.Replay.Definitions, Algorithms.Combination.Worker.Replay.Modeltheorem correct : Refinement.Visits.Correct (model planner) := … RotationBST.Combo.Worker.Replay.correct plannerModels.Rotation, Models.Online, Models.Pointer.Lazy.Online.Definitions, Algorithms.Combination.Analysis.Definitions, Models.Pointer.Machine, Models.Multifinger.Program.Definitions, Models.Multifinger.Program.Replay.Definitions, Models.Labelled.Cursor.Definitions, Models.Labelled.Cursor.Map.Definitions, Models.Labelled.Metadata.Definitions, Models.Labelled.Intervals.Definitions, Structures.TreeBuffer.Nodes.Definitions, Structures.TreeBuffer.Shared.Definitions, Algorithms.Combination.Controller.Definitions, Algorithms.Combination.Worker.Definitions, Algorithms.Combination.Refinement.Definitions, Algorithms.Combination.Refinement.Visits.Definitions, Algorithms.Combination.Worker.Replay.Definitions, Algorithms.Combination.Worker.Replay.ModelSo the three hypotheses that Theorem 3 places on its input workers (a pointer interpretation, request service, and well-behavedness) reduce, for an ordinary rotation-model algorithm, to the paper’s own assumption: an amortized bound that dominates the algorithm’s cost (here, times its total cost) and is stable on contiguous segments. That reduction is the content of the box, proved in Section 29.2.3. The GreedyASS and Tango sources of Chapter 31 enter the combination as replay workers of this kind.
The Lean sources are in Algorithms/Combination/Worker/: Definitions/Abstract.lean and Abstract.lean for the general facts, Replay/ for the adapter, and Pair/ for the pairing.
29.2.1 What a worker is
The worker structure itself, Controller.Worker, is shown with the controller in Section 29.3. It has two fields:
restart side : σ, the worker state right after the initial tree has been restored, for sideside;step side s x : σ × Bool, one charged unit of work on the current requestxfrom states, returning the new state and a completion bit.
A worker is indexed by a side side : Bool. It is really two workers, one for (false) and one for (true), sharing one state type σ. A step sees only the current state and the current request, never the future, so a worker is causal by construction. Nothing in the structure says what a unit is; that is the job of a separate pointer interpretation (Refinement.WorkerModel, Section 29.4), which says that each step performs at most one pointer instruction.
The controller runs a worker on one request with a fuel budget, using Controller.attempt, or its relational form Controller.AttemptRun. An attempt calls step until the completion bit is set or the fuel is exhausted, and returns an Controller.Attempt: the state reached, the fuel left, and whether the request completed. A failed attempt keeps its partial state. The controller then discards it by restoring the initial tree, which is why the interface has restart rather than any way to roll back.
A round on side , with budget and requests , looks like this:
side μ, budget B: | x₁ ✓ | x₂ ✓ | x₃ ✓ | x₄ · · · · · (budget exhausted: x₄ interrupted)
restore the initial tree, switch sides
side 1−μ, budget B: | x₄ ✓ | x₅ ✓ | x₆ · · · · · ·
When the budget suffices for any single request from a fresh restart, as Section 29.3 assumes, each request is completed by exactly one round, and the interrupted request is shared by two consecutive rounds. (If the second attempt also fails, the controller reports a failure; it never counts the request as served.) The library’s convention, used throughout, is that the closing round includes its interrupted request, even when its budget was already zero when the request arrived.
29.2.1.1 What is proved and what is assumed
This is the most important distinction in the section.
| Property | Status |
|---|---|
A completed request is deterministic: its final state and cost do not depend on the fuel (completed_unique) |
proved for every worker |
A failed attempt had less fuel than the request needs (failed_lt_complete) |
proved for every worker |
Each completed request costs at least one unit (length_le_cost) |
proved for every worker |
A closed round’s budget is less than the cost of completing its requests (lower_bound) |
proved for every worker |
A pointer interpretation: each step is one legal instruction or none (Refinement.WorkerModel) |
hypothesis of Theorem 3; proved for replay workers (Replay.model) |
Request service: a completed request visits its key (Visits.Correct) |
hypothesis of Theorem 3; proved for replay workers (correct) |
Well-behaved charges: nonnegativity, single-request bound, complete-run bound, segment stability (Analysis.WellBehaved) |
hypothesis of Theorem 3; for replay workers of rotation-model algorithms, reduced to a cost bound and segment stability of the charges (algorithms_well_behaved) |
Pairing preserves interpretation, service and well-behavedness (correct, well_behaved) |
proved |
The segment stability of the charges is never proved by this section. It is a property of the bound being combined, such as the lazy finger bound or Wilber’s interleave bound, and is established for those bounds where they are used (Section 29.1, Chapter 31).
29.2.2 Facts true of every worker
These results are about an arbitrary Controller.Worker and use nothing but its two fields. They are the worker-level half of the paper’s counting argument for Theorem 13: every round except the last runs for its whole budget, so the requests it completes would cost the input algorithm more than the budget.
29.2.2.1 Whole requests
Definition (access, request sequence). An access of x from state s with cost work is a successful attempt run with fuel exactly work: it completes, ends in state t, and has no fuel left. A request sequence is a chain of accesses, with costs added.
/-- A complete request stops at the first completion bit, using exactly itswork as fuel. This is the worker's execution, without controller resets. -/def Access {σ : Type} (worker : Controller.Worker σ) (side : Bool) (s : σ) (x : Nat) (t : σ) (work : Nat) : Prop := Controller.AttemptRun worker side x work s ⟨t, 0, true⟩ workinductive Requests {σ : Type} (worker : Controller.Worker σ) (side : Bool) : σ → List Nat → σ → Nat → Prop where | nil {s : σ} : Requests worker side s [] s 0 | cons {s t u : σ} {x : Nat} {xs : List Nat} {before after : Nat} : Access worker side s x t before → Requests worker side t xs u after → Requests worker side s (x :: xs) u (before + after)Worker.Access is the cost of a request when the worker is run to completion with no controller interference. It is the quantity that well-behavedness bounds. Fixing the fuel to the work makes it a single number; the next theorem shows that any other completing fuel gives the same answer.
Theorem (determinism of a request). Two attempts of the same request from the same state that both complete reach the same state with the same work, whatever their fuels. An attempt that fails had strictly less fuel than the work of any completing attempt. A completing attempt with any fuel is an access with the same work.
theorem completed_unique {σ : Type} {worker : Worker σ} {side : Bool} {x fuel work otherFuel otherWork : Nat} {s : σ} {result other : Attempt σ} (first : AttemptRun worker side x fuel s result work) (finished : result.done = true) (second : AttemptRun worker side x otherFuel s other otherWork) (alsoFinished : other.done = true) : result.state = other.state ∧ work = otherWork := RotationBST.Combo.Controller.AttemptRun.completed_unique first finished second alsoFinishedtheorem failed_lt_complete {σ : Type} {worker : Worker σ} {side : Bool} {x fuel work otherFuel otherWork : Nat} {s : σ} {result other : Attempt σ} (first : AttemptRun worker side x fuel s result work) (failed : result.done = false) (second : AttemptRun worker side x otherFuel s other otherWork) (finished : other.done = true) : fuel < otherWork := RotationBST.Combo.Controller.AttemptRun.failed_lt_complete first failed second finishedtheorem to_access {σ : Type} {worker : Worker σ} {side : Bool} {x fuel work : Nat} {s : σ} {result : Attempt σ} (run : AttemptRun worker side x fuel s result work) (finished : result.done = true) : Worker.Access worker side s x result.state work := RotationBST.Combo.Controller.AttemptRun.to_access run finishedAll three follow by induction on the attempt: step is a function, and the first completion bit stops the run.
Theorem (request sequences). Request sequences compose under concatenation, are unique given the start state and the requests, and cost at least one unit per request.
theorem Requests.append {σ : Type} {worker : Controller.Worker σ} {side : Bool} {s t u : σ} {before after : Nat} {earlier later : List Nat} (first : Requests worker side s earlier t before) (second : Requests worker side t later u after) : Requests worker side s (earlier ++ later) u (before + after) := RotationBST.Combo.Worker.Requests.append first secondtheorem Requests.unique {σ : Type} {worker : Controller.Worker σ} {side : Bool} {s t u : σ} {cost otherCost : Nat} {xs : List Nat} (first : Requests worker side s xs t cost) (second : Requests worker side s xs u otherCost) : t = u ∧ cost = otherCost := RotationBST.Combo.Worker.Requests.unique first secondtheorem Requests.length_le_cost {σ : Type} {worker : Controller.Worker σ} {side : Bool} {s t : σ} {cost : Nat} {xs : List Nat} (run : Requests worker side s xs t cost) : xs.length ≤ cost := RotationBST.Combo.Worker.Requests.length_le_cost run29.2.2.2 Closed rounds
Definition (closed round). A closed round from state s with budget fuel on a list of requests completes every request but the last, each one with the fuel left by the previous ones, and then fails on the last request with the fuel that remains.
/-- An exhausted round completes all but its last request, whose attemptconsumes the remaining budget without completion. The last request is includedeven when zero budget remains, matching the round-span coverage convention. -/inductive ClosedRound {σ : Type} (worker : Controller.Worker σ) (side : Bool) : σ → Nat → List Nat → Prop where | failed {s : σ} {x fuel work : Nat} {result : Controller.Attempt σ} : Controller.AttemptRun worker side x fuel s result work → result.done = false → ClosedRound worker side s fuel [x] | cons {s : σ} {x fuel work : Nat} {result : Controller.Attempt σ} {xs : List Nat} : Controller.AttemptRun worker side x fuel s result work → result.done = true → ClosedRound worker side result.state result.left xs → ClosedRound worker side s fuel (x :: xs)The last request is always in the list, even when it receives zero fuel: the failed case allows an exhausted attempt with fuel = 0. This matches the coverage convention of the round partition in Section 29.3, where a closing round contains its interrupted request. No worker step is invented for that request; it merely belongs to the round.
Theorem (closed rounds are expensive). If a closed round with budget covers requests , and the worker can complete all of from the same state at cost , then . A closed round may also be extended at the front by completed requests, adding their cost to its budget.
theorem ClosedRound.lower_bound {σ : Type} {worker : Controller.Worker σ} {side : Bool} {s t : σ} {budget cost : Nat} {xs : List Nat} (round : ClosedRound worker side s budget xs) (complete : Requests worker side s xs t cost) : budget < cost := RotationBST.Combo.Worker.ClosedRound.lower_bound round completetheorem ClosedRound.prepend {σ : Type} {worker : Controller.Worker σ} {side : Bool} {s t : σ} {before remaining : Nat} {earlier suffix : List Nat} (complete : Requests worker side s earlier t before) (round : ClosedRound worker side t remaining suffix) : ClosedRound worker side s (before + remaining) (earlier ++ suffix) := RotationBST.Combo.Worker.ClosedRound.prepend complete roundThe lower bound is the strict form of the paper’s inequality for every round but the last.
Theorem (the first switch closes a round). Run the controller of Section 29.3 from a state whose active side is , whose worker state is and whose remaining budget is . If the first switch happens at request index boundary (the first entry of Rounds.cuts, counted from offset), then the requests up to and including that index form a closed round of side from with budget . Consequently, completing them from costs more than .
theorem first_closed {σ : Type} (worker : Controller.Worker σ) (budget restore : Nat) (s : Controller.State σ) (offset : Nat) (xs : List Nat) (boundary : Nat) (later : List Nat) (cut : Rounds.cuts worker budget restore s offset xs = boundary :: later) : ClosedRound worker s.side s.workerState s.left (xs.take (boundary - offset + 1)) := RotationBST.Combo.Worker.first_closed worker budget restore s offset xs boundary later cuttheorem first_lower_bound {σ : Type} (worker : Controller.Worker σ) (budget restore : Nat) (s : Controller.State σ) (offset : Nat) (xs : List Nat) (boundary : Nat) (later : List Nat) (cut : Rounds.cuts worker budget restore s offset xs = boundary :: later) (final : σ) (cost : Nat) (complete : Requests worker s.side s.workerState (xs.take (boundary - offset + 1)) final cost) : s.left < cost := RotationBST.Combo.Worker.first_lower_bound worker budget restore s offset xs boundary later cut final cost completeThis links the controller’s actual behaviour to the worker-level notion. Section 29.3 applies it round by round, where each fresh round starts from restart with the full budget, and turns it into round certificates.
29.2.3 From an online algorithm to a worker: replay
An online BST algorithm serves one whole request per call. To make it interruptible, the replay adapter asks a planner for the complete list of pointer instructions of the current request, then plays them back one per unit. If the round is interrupted, the half-played list is simply abandoned, together with the tree it was building. If the round continues, the adapter finishes the list and moves to the planner’s next state.
This is a different decomposition from the paper’s, which emulates the input BST operation by operation inside the combined structure. The planning happens in one unit, and the retained list of instructions is abstract state. Both are allowed by the abstract BST model, which charges pointer instructions and does not bound private computation or storage. The real-world storage clause of Theorem 3 is not claimed.
29.2.3.1 Planners
A planner works on the pointer machine of Chapter 7. A Pointer.Config is a tree with a cursor, and an instruction is one of the six Multifinger.Opcodes of Chapter 10, executed by Multifinger.replay.
Definition (visit). An instruction list visits key x from a cursor if some prefix of it, possibly empty, leads to a cursor on x.
/-- A visit on a recorded instruction path, including its initial cursor. -/def Visits (x : Nat) (ops : List Opcode) (cursor : Pointer.Config) : Prop := ∃ before after visited, ops = before ++ after ∧ replay before cursor = some visited ∧ visited.key? = some xThe empty prefix counts, so a request for the root key is visited before any movement.
Definition (planner). A planner with state type σ for the initial tree saved has, for each side, an initial state, a tree represented by each state, and a planning function. Given a state and a request, plan returns the next state and the instructions that serve the request. The three conditions say: the initial state represents saved; on a tree of the right size and a valid key, the instructions start with the cursor at the root of the current tree and end with the cursor at the root of the next state’s tree; and they visit the requested key on the way.
/-- A causal pointer planner. The current state and current request determineits output; no future request list is supplied. Planning and the retained logare permitted in the abstract BST model, not claimed as finite-word storage. -/structure Planner (σ : Type) (saved : Tree) where initial : Bool → σ tree : Bool → σ → Tree plan : Bool → σ → Nat → σ × List Opcode initial_tree : ∀ side, tree side (initial side) = saved legal : ∀ side state x, (tree side state).size = saved.size → x < saved.size → replay (plan side state x).2 ⟨tree side state, []⟩ = some ⟨tree side (plan side state x).1, []⟩ visits : ∀ side state x, (tree side state).size = saved.size → x < saved.size → Visits x (plan side state x).2 ⟨tree side state, []⟩initial_treeAlgorithms/Combination/Worker/Replay/Definitions/Abstract.lean and its importslegalAlgorithms/Combination/Worker/Replay/Definitions/Abstract.lean and its importsvisitsAlgorithms/Combination/Worker/Replay/Definitions/Abstract.lean and its importsA planner is causal: plan sees the side, the current state and the current request, and nothing else. The obligations are imposed only on states whose tree has keys, and only for keys below ; the invariant Ready below ensures that only such states are reached. Ending each request at the root is a normalization. The pointer machine’s own requests (Pointer.Serves) leave the cursor anywhere, and the next request starts again at the root. The planner pays explicitly for the walk back, which Results.rooted below bounds.
29.2.3.2 The replay worker
Definition (replay worker). A replay state is either at a request boundary, holding a planner state, or running, holding the planner’s next state, the current cursor and the instructions still to play. One step from a boundary calls the planner and keeps the cursor at the root. Each later step plays one instruction. When none remain, one more step reports completion and moves to the boundary of the next state.
inductive State (σ : Type) where | boundary (state : σ) | running (next : σ) (cursor : Pointer.Config) (remaining : List Opcode)/-- One unit starts planning, each subsequent opcode takes one unit, and onefinal unit reports completion. A failed opcode never reports success. -/def step {σ : Type} {saved : Tree} (planner : Planner σ saved) (side : Bool) : State σ → Nat → State σ × Bool | .boundary state, x => let planned := planner.plan side state x (.running planned.1 ⟨planner.tree side state, []⟩ planned.2, false) | .running next _ [], _ => (.boundary next, true) | .running next cursor (op :: ops), _ => match perform op cursor with | none => (.running next cursor (op :: ops), false) | some moved => (.running next moved ops, false)def worker {σ : Type} {saved : Tree} (planner : Planner σ saved) : Controller.Worker (State σ) := ⟨fun side => .boundary (planner.initial side), step planner⟩A request with a plan of length therefore costs exactly units: one to plan, to play, one to finish. If an instruction were illegal, the step would leave the state unchanged and never report completion, so the attempt would only burn its budget. The pointer invariant below shows that this never happens on reachable states.
For comparisons with the planner, the same computation is also written request by request, without interruption. run threads the planner state through a sequence and adds per request. policyRun does the same for an arbitrary causal rule that returns a next state and a cost, which is how a source algorithm is described before it is turned into a planner.
/-- Whole requests use the same planner, allowing exact cost/state comparisonwith the interruptible worker. Empty input does not invoke planning. -/def run {σ : Type} {saved : Tree} (planner : Planner σ saved) (side : Bool) : σ → List Nat → σ × Nat | state, [] => (state, 0) | state, x :: xs => let planned := planner.plan side state x let rest := run planner side planned.1 xs (rest.1, planned.2.length + 2 + rest.2)/-- Direct execution of a source policy's request rule, before trace expansion. -/def policyRun {σ : Type} (next : Bool → σ → Nat → σ × Nat) (side : Bool) : σ → List Nat → σ × Nat | state, [] => (state, 0) | state, x :: xs => let first := next side state x let rest := policyRun next side first.1 xs (rest.1, first.2 + rest.2)Theorem (replay serves whole requests at the planner’s cost). From a boundary state whose tree has keys, a valid request is an Worker.Access that ends at the boundary of the planner’s next state, at cost plan length plus . A sequence of valid requests is a Worker.Requests whose final state and cost are those of run.
theorem access (side : Bool) (state : σ) (x : Nat) (size : (planner.tree side state).size = saved.size) (valid : x < saved.size) : Worker.Access (worker planner) side (.boundary state) x (.boundary (planner.plan side state x).1) ((planner.plan side state x).2.length + 2) := RotationBST.Combo.Worker.Replay.access planner side state x size validModels.Rotation, Models.Online, Models.Pointer.Lazy.Online.Definitions, Algorithms.Combination.Analysis.Definitions, Models.Pointer.Machine, Models.Multifinger.Program.Definitions, Models.Multifinger.Program.Replay.Definitions, Models.Labelled.Cursor.Definitions, Models.Labelled.Cursor.Map.Definitions, Models.Labelled.Metadata.Definitions, Models.Labelled.Intervals.Definitions, Structures.TreeBuffer.Nodes.Definitions, Structures.TreeBuffer.Shared.Definitions, Algorithms.Combination.Controller.Definitions, Algorithms.Combination.Worker.Definitions, Algorithms.Combination.Refinement.Definitions, Algorithms.Combination.Refinement.Visits.Definitions, Algorithms.Combination.Worker.Replay.Definitions, Algorithms.Combination.Worker.Replay.Modeltheorem requests (side : Bool) (state : σ) (xs : List Nat) (size : (planner.tree side state).size = saved.size) (valid : ∀ x ∈ xs, x < saved.size) : Worker.Requests (worker planner) side (.boundary state) xs (.boundary (run planner side state xs).1) (run planner side state xs).2 := RotationBST.Combo.Worker.Replay.requests planner side state xs size validModels.Rotation, Models.Online, Models.Pointer.Lazy.Online.Definitions, Algorithms.Combination.Analysis.Definitions, Models.Pointer.Machine, Models.Multifinger.Program.Definitions, Models.Multifinger.Program.Replay.Definitions, Models.Labelled.Cursor.Definitions, Models.Labelled.Cursor.Map.Definitions, Models.Labelled.Metadata.Definitions, Models.Labelled.Intervals.Definitions, Structures.TreeBuffer.Nodes.Definitions, Structures.TreeBuffer.Shared.Definitions, Algorithms.Combination.Controller.Definitions, Algorithms.Combination.Worker.Definitions, Algorithms.Combination.Refinement.Definitions, Algorithms.Combination.Refinement.Visits.Definitions, Algorithms.Combination.Worker.Replay.Definitions, Algorithms.Combination.Worker.Replay.Model29.2.3.3 The pointer interpretation
The controller treats a worker as a black box of units. Section 29.4 needs more: each unit must correspond to at most one physical pointer instruction, so that the units of the controller become the instructions of one pointer execution. This is the contract Refinement.WorkerModel. For a replay worker, the cursor is the root of the planner’s tree at a boundary and the current cursor while running, and the instruction of a step is the next one in the list.
def cursor {σ : Type} {saved : Tree} (planner : Planner σ saved) (side : Bool) : State σ → Pointer.Config | .boundary state => ⟨planner.tree side state, []⟩ | .running _ current _ => currentdef Ready {σ : Type} {saved : Tree} (planner : Planner σ saved) (side : Bool) : State σ → Prop | .boundary state => (planner.tree side state).size = saved.size | .running next current ops => (planner.tree side next).size = saved.size ∧ replay ops current = some ⟨planner.tree side next, []⟩def instruction {σ : Type} : State σ → Option Opcode | .running _ _ (op :: _) => some op | _ => noneReady is the invariant: at a boundary, the tree has keys; while running, the next tree has keys and the remaining instructions really lead from the current cursor to its root. Ready holds at restart by initial_tree, and the planner’s legal field makes it hold again after the planning step.
Theorem (the invariant is maintained). A ready state’s cursor tree has keys. A step on a valid key keeps the state ready. A step either leaves the cursor where it is (planning and completion) or performs exactly the reported instruction.
theorem model_ready_size (side : Bool) (state : State σ) (ready : Ready planner side state) : (cursor planner side state).tree.size = saved.size := ready_size planner side state readyModels.Rotation, Models.Pointer.Machine, Models.Multifinger.Program.Definitions, Models.Multifinger.Program.Replay.Definitions, Models.Labelled.Cursor.Definitions, Models.Labelled.Cursor.Map.Definitions, Models.Labelled.Metadata.Definitions, Models.Labelled.Intervals.Definitions, Structures.TreeBuffer.Nodes.Definitions, Structures.TreeBuffer.Shared.Definitions, Algorithms.Combination.Controller.Definitions, Algorithms.Combination.Worker.Definitions, Algorithms.Combination.Refinement.Definitions, Algorithms.Combination.Refinement.Visits.Definitions, Algorithms.Combination.Worker.Replay.Definitionstheorem model_step_ready (side : Bool) (state : State σ) (x : Nat) (ready : Ready planner side state) (valid : x < saved.size) : Ready planner side ((worker planner).step side state x).1 := step_ready planner side state x ready validModels.Rotation, Models.Pointer.Machine, Models.Multifinger.Program.Definitions, Models.Multifinger.Program.Replay.Definitions, Models.Labelled.Cursor.Definitions, Models.Labelled.Cursor.Map.Definitions, Models.Labelled.Metadata.Definitions, Models.Labelled.Intervals.Definitions, Structures.TreeBuffer.Nodes.Definitions, Structures.TreeBuffer.Shared.Definitions, Algorithms.Combination.Controller.Definitions, Algorithms.Combination.Worker.Definitions, Algorithms.Combination.Refinement.Definitions, Algorithms.Combination.Refinement.Visits.Definitions, Algorithms.Combination.Worker.Replay.Definitionstheorem model_step_cursor (side : Bool) (state : State σ) (x : Nat) (ready : Ready planner side state) : match instruction state with | none => cursor planner side ((worker planner).step side state x).1 = cursor planner side state | some op => perform op (cursor planner side state) = some (cursor planner side ((worker planner).step side state x).1) := step_cursor planner side state x readyModels.Rotation, Models.Pointer.Machine, Models.Multifinger.Program.Definitions, Models.Multifinger.Program.Replay.Definitions, Models.Labelled.Cursor.Definitions, Models.Labelled.Cursor.Map.Definitions, Models.Labelled.Metadata.Definitions, Models.Labelled.Intervals.Definitions, Structures.TreeBuffer.Nodes.Definitions, Structures.TreeBuffer.Shared.Definitions, Algorithms.Combination.Controller.Definitions, Algorithms.Combination.Worker.Definitions, Algorithms.Combination.Refinement.Definitions, Algorithms.Combination.Refinement.Visits.Definitions, Algorithms.Combination.Worker.Replay.DefinitionsThese are the obligations of Refinement.WorkerModel, packaged into one model:
/-- A causal single-opcode interpretation of the interruptible trace worker.The pending log is abstract state; no real-world space bound is asserted. -/def model : Refinement.WorkerModel (worker planner) saved where cursor := cursor planner Ready := Ready planner instruction := fun _ state _ => instruction state restart_ready := fun side => congrArg Tree.size (planner.initial_tree side) restart_cursor := fun side => congrArg (fun t => Pointer.Config.mk t []) (planner.initial_tree side) size := model_ready_size planner step_ready := model_step_ready planner step_cursor := fun side state x ready _ => model_step_cursor planner side state x readyThe log of pending instructions is abstract state. No bound on its size, or on the planner state, is asserted.
Theorem (replay workers serve their requests). Every request boundary that the worker reaches from restart by completing valid requests is a boundary state whose tree has keys. From such a state, every completed valid request moves the cursor over the requested key during its consumed steps.
theorem request_boundary {side : Bool} {state : State σ} (boundary : Refinement.Visits.Boundary (worker planner) saved.size side state) : ∃ original, state = .boundary original ∧ (planner.tree side original).size = saved.size := RotationBST.Combo.Worker.Replay.request_boundary planner boundaryModels.Rotation, Models.Online, Models.Pointer.Lazy.Online.Definitions, Algorithms.Combination.Analysis.Definitions, Models.Pointer.Machine, Models.Multifinger.Program.Definitions, Models.Multifinger.Program.Replay.Definitions, Models.Labelled.Cursor.Definitions, Models.Labelled.Cursor.Map.Definitions, Models.Labelled.Metadata.Definitions, Models.Labelled.Intervals.Definitions, Structures.TreeBuffer.Nodes.Definitions, Structures.TreeBuffer.Shared.Definitions, Algorithms.Combination.Controller.Definitions, Algorithms.Combination.Worker.Definitions, Algorithms.Combination.Refinement.Definitions, Algorithms.Combination.Refinement.Visits.Definitions, Algorithms.Combination.Worker.Replay.Definitions, Algorithms.Combination.Worker.Replay.Modeltheorem correct : Refinement.Visits.Correct (model planner) := RotationBST.Combo.Worker.Replay.correct plannerModels.Rotation, Models.Online, Models.Pointer.Lazy.Online.Definitions, Algorithms.Combination.Analysis.Definitions, Models.Pointer.Machine, Models.Multifinger.Program.Definitions, Models.Multifinger.Program.Replay.Definitions, Models.Labelled.Cursor.Definitions, Models.Labelled.Cursor.Map.Definitions, Models.Labelled.Metadata.Definitions, Models.Labelled.Intervals.Definitions, Structures.TreeBuffer.Nodes.Definitions, Structures.TreeBuffer.Shared.Definitions, Algorithms.Combination.Controller.Definitions, Algorithms.Combination.Worker.Definitions, Algorithms.Combination.Refinement.Definitions, Algorithms.Combination.Refinement.Visits.Definitions, Algorithms.Combination.Worker.Replay.Definitions, Algorithms.Combination.Worker.Replay.ModelThe second statement is the request-service contract Visits.Correct for every planner, with no hypothesis. It is a contract on the input worker only: what the combined execution does after an interruption is analysed in Section 29.4.
29.2.3.4 Building planners from pointer algorithms
The planner interface is close to that of an ordinary online pointer algorithm, except that a request must return to the root.
Theorem (returning to the root). If the pointer machine serves x from tree s to tree t at cost c (including its request unit), then some instruction list leads from the root of s to the root of t, visits x, and has length at most .
theorem rooted {s t : Tree} {x cost : Nat} (served : Pointer.Serves s t x cost) : ∃ ops, replay ops ⟨s, []⟩ = some ⟨t, []⟩ ∧ Visits x ops ⟨s, []⟩ ∧ ops.length + 2 ≤ 2 * cost := RotationBST.Combo.Worker.Replay.rooted servedModels.Rotation, Models.Online, Models.Pointer.Lazy.Online.Definitions, Algorithms.Combination.Analysis.Definitions, Models.Pointer.Machine, Models.Multifinger.Program.Definitions, Models.Multifinger.Program.Replay.Definitions, Models.Labelled.Cursor.Definitions, Models.Labelled.Cursor.Map.Definitions, Models.Labelled.Metadata.Definitions, Models.Labelled.Intervals.Definitions, Structures.TreeBuffer.Nodes.Definitions, Structures.TreeBuffer.Shared.Definitions, Algorithms.Combination.Controller.Definitions, Algorithms.Combination.Worker.Definitions, Algorithms.Combination.Refinement.Definitions, Algorithms.Combination.Refinement.Visits.Definitions, Algorithms.Combination.Worker.Replay.Definitions, Algorithms.Combination.Worker.Replay.ModelThe walk back to the root is no longer than the work already done, because each instruction changes the cursor’s depth by at most one.
Theorem (pointer algorithms are planners). Let next be a causal rule that, from each state whose tree has keys and each valid request, serves the request on the pointer machine and returns a new state and a cost. Then there is a planner with the same initial states and trees whose successor states are those of next and whose plans cost at most twice the rule’s cost, per request and over any sequence.
theorem planner_exists {σ : Type} {saved : Tree} (initial : Bool → σ) (tree : Bool → σ → Tree) (next : Bool → σ → Nat → σ × Nat) (initialTree : ∀ side, tree side (initial side) = saved) (correct : ∀ side state x, (tree side state).size = saved.size → x < saved.size → Pointer.Serves (tree side state) (tree side (next side state x).1) x (next side state x).2) : ∃ planner : Planner σ saved, planner.initial = initial ∧ planner.tree = tree ∧ ∀ side state x, (tree side state).size = saved.size → x < saved.size → (planner.plan side state x).1 = (next side state x).1 ∧ (planner.plan side state x).2.length + 2 ≤ 2 * (next side state x).2 := RotationBST.Combo.Worker.Replay.planner_exists initial tree next initialTree correctModels.Rotation, Models.Online, Models.Pointer.Lazy.Online.Definitions, Algorithms.Combination.Analysis.Definitions, Models.Pointer.Machine, Models.Multifinger.Program.Definitions, Models.Multifinger.Program.Replay.Definitions, Models.Labelled.Cursor.Definitions, Models.Labelled.Cursor.Map.Definitions, Models.Labelled.Metadata.Definitions, Models.Labelled.Intervals.Definitions, Structures.TreeBuffer.Nodes.Definitions, Structures.TreeBuffer.Shared.Definitions, Algorithms.Combination.Controller.Definitions, Algorithms.Combination.Worker.Definitions, Algorithms.Combination.Refinement.Definitions, Algorithms.Combination.Refinement.Visits.Definitions, Algorithms.Combination.Worker.Replay.Definitions, Algorithms.Combination.Worker.Replay.Modeltheorem run_bound {σ : Type} {saved : Tree} (planner : Planner σ saved) (next : Bool → σ → Nat → σ × Nat) (expansion : ∀ side state x, (planner.tree side state).size = saved.size → x < saved.size → (planner.plan side state x).1 = (next side state x).1 ∧ (planner.plan side state x).2.length + 2 ≤ 2 * (next side state x).2) (side : Bool) (state : σ) (xs : List Nat) (size : (planner.tree side state).size = saved.size) (valid : ∀ x ∈ xs, x < saved.size) : (run planner side state xs).1 = (policyRun next side state xs).1 ∧ (run planner side state xs).2 ≤ 2 * (policyRun next side state xs).2 := RotationBST.Combo.Worker.Replay.run_bound planner next expansion side state xs size validModels.Rotation, Models.Online, Models.Pointer.Lazy.Online.Definitions, Algorithms.Combination.Analysis.Definitions, Models.Pointer.Machine, Models.Multifinger.Program.Definitions, Models.Multifinger.Program.Replay.Definitions, Models.Labelled.Cursor.Definitions, Models.Labelled.Cursor.Map.Definitions, Models.Labelled.Metadata.Definitions, Models.Labelled.Intervals.Definitions, Structures.TreeBuffer.Nodes.Definitions, Structures.TreeBuffer.Shared.Definitions, Algorithms.Combination.Controller.Definitions, Algorithms.Combination.Worker.Definitions, Algorithms.Combination.Refinement.Definitions, Algorithms.Combination.Refinement.Visits.Definitions, Algorithms.Combination.Worker.Replay.Definitions, Algorithms.Combination.Worker.Replay.ModelThe planner is obtained by choice, so it is a mathematical object, not a program; the abstract model allows this.
29.2.3.5 Capping a request at
Well-behavedness asks that every single access, from every reachable state, cost at most . In the paper this is part of the assumption on the input algorithms. For replay workers it comes for free, because a request can always be served in linear time while still producing the planner’s intended next tree.
Theorem (a short rooted program always exists). For any two trees and of the same size and any key , some instruction list leads from the root of to the root of , visits , and has length plus at most .
theorem short_rooted (s t : Tree) (x : Nat) (same : s.size = t.size) (valid : x < s.size) : ∃ ops, replay ops ⟨s, []⟩ = some ⟨t, []⟩ ∧ Visits x ops ⟨s, []⟩ ∧ ops.length + 2 ≤ 20 * s.size := RotationBST.Combo.Worker.Replay.short_rooted s t x same validModels.Rotation, Models.Online, Models.Pointer.Lazy.Online.Definitions, Algorithms.Combination.Analysis.Definitions, Models.Pointer.Machine, Models.Multifinger.Program.Definitions, Models.Multifinger.Program.Replay.Definitions, Models.Labelled.Cursor.Definitions, Models.Labelled.Cursor.Map.Definitions, Models.Labelled.Metadata.Definitions, Models.Labelled.Intervals.Definitions, Structures.TreeBuffer.Nodes.Definitions, Structures.TreeBuffer.Shared.Definitions, Algorithms.Combination.Controller.Definitions, Algorithms.Combination.Worker.Definitions, Algorithms.Combination.Refinement.Definitions, Algorithms.Combination.Refinement.Visits.Definitions, Algorithms.Combination.Worker.Replay.Definitions, Algorithms.Combination.Worker.Replay.ModelIt searches for from the root and returns, then reshapes into with the linear pointer reshaping of Chapter 7.
Theorem (capped planners). Every planner has a capped version with the same initial states, trees and successor states, whose plans are never longer than the original’s and cost at most each (plan length plus ). Over any sequence, the capped planner ends in the same state and costs no more.
theorem bounded_planner {σ : Type} {saved : Tree} (planner : Planner σ saved) : ∃ bounded : Planner σ saved, bounded.initial = planner.initial ∧ bounded.tree = planner.tree ∧ (∀ side state x, (bounded.plan side state x).1 = (planner.plan side state x).1) ∧ ∀ side state x, (planner.tree side state).size = saved.size → x < saved.size → (bounded.plan side state x).2.length ≤ (planner.plan side state x).2.length ∧ (bounded.plan side state x).2.length + 2 ≤ 20 * saved.size := RotationBST.Combo.Worker.Replay.bounded_planner plannerModels.Rotation, Models.Online, Models.Pointer.Lazy.Online.Definitions, Algorithms.Combination.Analysis.Definitions, Models.Pointer.Machine, Models.Multifinger.Program.Definitions, Models.Multifinger.Program.Replay.Definitions, Models.Labelled.Cursor.Definitions, Models.Labelled.Cursor.Map.Definitions, Models.Labelled.Metadata.Definitions, Models.Labelled.Intervals.Definitions, Structures.TreeBuffer.Nodes.Definitions, Structures.TreeBuffer.Shared.Definitions, Algorithms.Combination.Controller.Definitions, Algorithms.Combination.Worker.Definitions, Algorithms.Combination.Refinement.Definitions, Algorithms.Combination.Refinement.Visits.Definitions, Algorithms.Combination.Worker.Replay.Definitions, Algorithms.Combination.Worker.Replay.Modeltheorem refined_run {σ : Type} {saved : Tree} (planner bounded : Planner σ saved) (sameNext : ∀ side state x, (bounded.plan side state x).1 = (planner.plan side state x).1) (shorter : ∀ side state x, (planner.tree side state).size = saved.size → x < saved.size → (bounded.plan side state x).2.length ≤ (planner.plan side state x).2.length) (side : Bool) (state : σ) (xs : List Nat) (size : (planner.tree side state).size = saved.size) (valid : ∀ x ∈ xs, x < saved.size) : (run bounded side state xs).1 = (run planner side state xs).1 ∧ (run bounded side state xs).2 ≤ (run planner side state xs).2 := RotationBST.Combo.Worker.Replay.refined_run planner bounded sameNext shorter side state xs size validModels.Rotation, Models.Online, Models.Pointer.Lazy.Online.Definitions, Algorithms.Combination.Analysis.Definitions, Models.Pointer.Machine, Models.Multifinger.Program.Definitions, Models.Multifinger.Program.Replay.Definitions, Models.Labelled.Cursor.Definitions, Models.Labelled.Cursor.Map.Definitions, Models.Labelled.Metadata.Definitions, Models.Labelled.Intervals.Definitions, Structures.TreeBuffer.Nodes.Definitions, Structures.TreeBuffer.Shared.Definitions, Algorithms.Combination.Controller.Definitions, Algorithms.Combination.Worker.Definitions, Algorithms.Combination.Refinement.Definitions, Algorithms.Combination.Refinement.Visits.Definitions, Algorithms.Combination.Worker.Replay.Definitions, Algorithms.Combination.Worker.Replay.ModelBecause the successor states are unchanged, the capped worker makes the same decisions as the original on every later request. Only the instructions of an expensive request are replaced.
Theorem (capped replay workers are well behaved). Let a capped planner be derived from a planner as above. Suppose , , the charges are nonnegative, the original planner’s cost on every valid sequence from its initial state is at most the total charge plus , and the charges are stable on contiguous segments with constants . Then the capped replay worker satisfies Analysis.WellBehaved with , , , and these charges.
theorem bounded_well_behaved {σ : Type} {saved : Tree} (planner bounded : Planner σ saved) (sameInitial : bounded.initial = planner.initial) (sameTree : bounded.tree = planner.tree) (sameNext : ∀ side state x, (bounded.plan side state x).1 = (planner.plan side state x).1) (shorter : ∀ side state x, (planner.tree side state).size = saved.size → x < saved.size → (bounded.plan side state x).2.length ≤ (planner.plan side state x).2.length) (single : ∀ side state x, (planner.tree side state).size = saved.size → x < saved.size → (bounded.plan side state x).2.length + 2 ≤ 20 * saved.size) (overhead C₀ C₁ : Nat) (charges : Analysis.Charges) (size : saved.size ≤ overhead) (constant : 20 ≤ C₁) (nonneg : ∀ side xs i, 0 ≤ charges side xs i) (upper : ∀ side xs, (∀ x ∈ xs, x < saved.size) → ((run planner side (planner.initial side) xs).2 : ℝ) ≤ Analysis.total charges side xs + overhead) (stable : ∀ side xs, (∀ x ∈ xs, x < saved.size) → ∀ lo hi, lo ≤ hi → hi ≤ xs.length → Analysis.total charges side (Analysis.segment xs lo hi) + overhead ≤ C₀ * Analysis.mass charges side xs lo hi + C₁ * overhead) : Analysis.WellBehaved (worker bounded) saved.size overhead C₀ C₁ charges := RotationBST.Combo.Worker.Replay.bounded_well_behaved planner bounded sameInitial sameTree sameNext shorter single overhead C₀ C₁ charges size constant nonneg upper stableModels.Rotation, Models.Online, Models.Pointer.Lazy.Online.Definitions, Algorithms.Combination.Analysis.Definitions, Models.Pointer.Machine, Models.Multifinger.Program.Definitions, Models.Multifinger.Program.Replay.Definitions, Models.Labelled.Cursor.Definitions, Models.Labelled.Cursor.Map.Definitions, Models.Labelled.Metadata.Definitions, Models.Labelled.Intervals.Definitions, Structures.TreeBuffer.Nodes.Definitions, Structures.TreeBuffer.Shared.Definitions, Algorithms.Combination.Controller.Definitions, Algorithms.Combination.Worker.Definitions, Algorithms.Combination.Refinement.Definitions, Algorithms.Combination.Refinement.Visits.Definitions, Algorithms.Combination.Worker.Replay.Definitions, Algorithms.Combination.Worker.Replay.ModelOf the four fields of well-behavedness, nonnegativity and segment stability are copied from the hypotheses, which concern the charges alone. The complete-run bound follows from the original planner’s bound and Results.refined_run. The single-access bound follows from the cap: .
29.2.3.6 Rotation-model algorithms
Combining the pieces gives a worker for any pair of online algorithms of the rotation model. The rotation-model algorithm is first run on the pointer machine by the online lazy simulation of Chapter 7 (Online.State), which costs at most times its total cost. Returning to the root doubles this, and capping adds the single-access bound.
Theorem (a planner for every pair of algorithms). For any two rotation-model online algorithms and any initial tree on keys, there is a planner whose state is the list of past requests together with a lazy-simulation state, whose plans cost at most per request, and whose run on any valid sequence from its initial state on side costs at most times the total cost of on from .
theorem algorithm_planner (a : Bool → RotationBST.Algorithm) (saved : Tree) : ∃ planner : Planner (List Nat × Pointer.Lazy.Online.State) saved, (∀ side state x, (planner.tree side state).size = saved.size → x < saved.size → (planner.plan side state x).2.length + 2 ≤ 20 * saved.size) ∧ ∀ side xs, (∀ x ∈ xs, x < saved.size) → (run planner side (planner.initial side) xs).2 ≤ 64 * (a side).totalCost saved xs := RotationBST.Combo.Worker.Replay.algorithm_planner a savedModels.Rotation, Models.Online, Models.Pointer.Lazy.Online.Definitions, Algorithms.Combination.Analysis.Definitions, Models.Pointer.Machine, Models.Multifinger.Program.Definitions, Models.Multifinger.Program.Replay.Definitions, Models.Labelled.Cursor.Definitions, Models.Labelled.Cursor.Map.Definitions, Models.Labelled.Metadata.Definitions, Models.Labelled.Intervals.Definitions, Structures.TreeBuffer.Nodes.Definitions, Structures.TreeBuffer.Shared.Definitions, Algorithms.Combination.Controller.Definitions, Algorithms.Combination.Worker.Definitions, Algorithms.Combination.Refinement.Definitions, Algorithms.Combination.Refinement.Visits.Definitions, Algorithms.Combination.Worker.Replay.Definitions, Algorithms.Combination.Worker.Replay.ModelTheorem (the replay worker is well behaved). For the same data, one planner can be chosen with the following property. Take any overhead , any , any , and any nonnegative charges such that for every valid and both sides, and such that is stable on contiguous segments with constants . Then the replay worker of that planner satisfies Analysis.WellBehaved with , , , and . This is the statement of the box at the top of the section.
theorem algorithms_well_behaved (a : Bool → RotationBST.Algorithm) (saved : Tree) : ∃ planner : Planner (List Nat × Pointer.Lazy.Online.State) saved, ∀ (overhead C₀ C₁ : Nat) (charges : Analysis.Charges), saved.size ≤ overhead → 20 ≤ C₁ → (∀ side xs i, 0 ≤ charges side xs i) → (∀ side xs, (∀ x ∈ xs, x < saved.size) → (64 : ℝ) * (a side).totalCost saved xs ≤ Analysis.total charges side xs + overhead) → (∀ side xs, (∀ x ∈ xs, x < saved.size) → ∀ lo hi, lo ≤ hi → hi ≤ xs.length → Analysis.total charges side (Analysis.segment xs lo hi) + overhead ≤ C₀ * Analysis.mass charges side xs lo hi + C₁ * overhead) → Analysis.WellBehaved (worker planner) saved.size overhead C₀ C₁ charges := RotationBST.Combo.Worker.Replay.algorithms_well_behaved a savedModels.Rotation, Models.Online, Models.Pointer.Lazy.Online.Definitions, Algorithms.Combination.Analysis.Definitions, Models.Pointer.Machine, Models.Multifinger.Program.Definitions, Models.Multifinger.Program.Replay.Definitions, Models.Labelled.Cursor.Definitions, Models.Labelled.Cursor.Map.Definitions, Models.Labelled.Metadata.Definitions, Models.Labelled.Intervals.Definitions, Structures.TreeBuffer.Nodes.Definitions, Structures.TreeBuffer.Shared.Definitions, Algorithms.Combination.Controller.Definitions, Algorithms.Combination.Worker.Definitions, Algorithms.Combination.Refinement.Definitions, Algorithms.Combination.Refinement.Visits.Definitions, Algorithms.Combination.Worker.Replay.Definitions, Algorithms.Combination.Worker.Replay.ModelThe order of quantifiers matters: the planner exists before the charges. This is used in Chapter 31, where the charges of the GreedyASS side depend on a reference tree chosen after the algorithm, and the same worker must serve for all of them.
29.2.3.7 The emitted program
The deamortization of Chapter 30 also starts from a planner, but uses it differently. It does not interrupt requests; it needs the concatenated stream of instructions, to feed into the bounded-depth finger simulation of Section 38.4.
Definition (program). The program of a planner on a sequence is the concatenation of its plans, threading the successor states.
/-- The planner's actual concatenated opcodes, with successor states retained.This omits the two bookkeeping steps charged by the interruptible worker. -/def program {σ : Type} {saved : Tree} (planner : Planner σ saved) (side : Bool) : σ → List Nat → List Multifinger.Opcode | _, [] => [] | state, x :: xs => (planner.plan side state x).2 ++ program planner side (planner.plan side state x).1 xsTheorem (program accounting). run splits over concatenated sequences. The program has length exactly the cost of run minus two units per request: the planning and completion units of the interruptible worker, which are bookkeeping, not instructions.
theorem run_append {σ : Type} {saved : Tree} (planner : Planner σ saved) (side : Bool) (state : σ) (xs ys : List Nat) : run planner side state (xs ++ ys) = let first := run planner side state xs let last := run planner side first.1 ys (last.1, first.2 + last.2) := RotationBST.Combo.Worker.Replay.Requests.run_append planner side state xs ysModels.Rotation, Models.Online, Models.Pointer.Lazy.Online.Definitions, Algorithms.Combination.Analysis.Definitions, Models.Pointer.Machine, Models.Multifinger.Program.Definitions, Models.Multifinger.Program.Replay.Definitions, Models.Labelled.Cursor.Definitions, Models.Labelled.Cursor.Map.Definitions, Models.Labelled.Metadata.Definitions, Models.Labelled.Intervals.Definitions, Structures.TreeBuffer.Nodes.Definitions, Structures.TreeBuffer.Shared.Definitions, Algorithms.Combination.Controller.Definitions, Algorithms.Combination.Worker.Definitions, Algorithms.Combination.Refinement.Definitions, Algorithms.Combination.Refinement.Visits.Definitions, Algorithms.Combination.Worker.Replay.Definitions, Algorithms.Combination.Worker.Replay.Model, Models.Pointer.Mirror.Definitions, Models.Pointer.Lazy.Skeleton, Models.Labelled.Ordering.Definitions, Structures.PopTart.Definitions, Structures.PopTart.Mirror.Definitions, Structures.PopTart.Chocolate.Definitions, Structures.PopTart.Located.Definitions, Structures.HeavyPath.Definitions, Structures.HeavyPath.Paired.Definitions, Structures.HeavyPath.Paired.Substitution.Definitions, Structures.HeavyPath.Recursive.Definitions, Structures.HeavyPath.Finger.Definitions, Structures.PopTart.Chocolate.Top.Definitions, Structures.HeavyPath.Paired.Exposure.Definitions, Structures.HeavyPath.Recursive.History.Definitions, Structures.HeavyPath.Finger.Transfer.Definitions, Structures.HeavyPath.Finger.History.Definitions, Structures.HeavyPath.Finger.History.Simulation.Definitions, Structures.HeavyPath.Finger.History.Simulation.Requests.Definitions, Bounds.StaticTree.Definitions, Algorithms.Combination.Worker.Replay.Requests.Definitionstheorem program_cost {σ : Type} {saved : Tree} (planner : Planner σ saved) (side : Bool) (state : σ) (xs : List Nat) : (program planner side state xs).length + 2 * xs.length = (run planner side state xs).2 := RotationBST.Combo.Worker.Replay.Requests.program_cost planner side state xsModels.Rotation, Models.Online, Models.Pointer.Lazy.Online.Definitions, Algorithms.Combination.Analysis.Definitions, Models.Pointer.Machine, Models.Multifinger.Program.Definitions, Models.Multifinger.Program.Replay.Definitions, Models.Labelled.Cursor.Definitions, Models.Labelled.Cursor.Map.Definitions, Models.Labelled.Metadata.Definitions, Models.Labelled.Intervals.Definitions, Structures.TreeBuffer.Nodes.Definitions, Structures.TreeBuffer.Shared.Definitions, Algorithms.Combination.Controller.Definitions, Algorithms.Combination.Worker.Definitions, Algorithms.Combination.Refinement.Definitions, Algorithms.Combination.Refinement.Visits.Definitions, Algorithms.Combination.Worker.Replay.Definitions, Algorithms.Combination.Worker.Replay.Model, Models.Pointer.Mirror.Definitions, Models.Pointer.Lazy.Skeleton, Models.Labelled.Ordering.Definitions, Structures.PopTart.Definitions, Structures.PopTart.Mirror.Definitions, Structures.PopTart.Chocolate.Definitions, Structures.PopTart.Located.Definitions, Structures.HeavyPath.Definitions, Structures.HeavyPath.Paired.Definitions, Structures.HeavyPath.Paired.Substitution.Definitions, Structures.HeavyPath.Recursive.Definitions, Structures.HeavyPath.Finger.Definitions, Structures.PopTart.Chocolate.Top.Definitions, Structures.HeavyPath.Paired.Exposure.Definitions, Structures.HeavyPath.Recursive.History.Definitions, Structures.HeavyPath.Finger.Transfer.Definitions, Structures.HeavyPath.Finger.History.Definitions, Structures.HeavyPath.Finger.History.Simulation.Definitions, Structures.HeavyPath.Finger.History.Simulation.Requests.Definitions, Bounds.StaticTree.Definitions, Algorithms.Combination.Worker.Replay.Requests.DefinitionsTheorem (planners are source algorithms of the simulation). An instruction list that is legal on the erasure of a labelled cursor (LabelledCursor.Config) and visits a key also runs on the labelled cursor, with the same erasure, and serves the key in the sense of Requests.Serves. Consequently, every planner and side yield a source algorithm of the simulation (Requests.Algorithm) that, after any valid history and from a cursor at the root of the planner’s current tree, emits exactly the planner’s program, ends at the root of the planner’s next tree, and has the same cost accounting.
theorem source_lift {α : Type} (c : LabelledCursor.Config α) (ops : List Multifinger.Opcode) (final : Pointer.Config) (key : Nat) (legal : Multifinger.replay ops c.erase = some final) (visit : Visits key ops c.erase) : ∃ result : LabelledCursor.Config α, Simulation.sourceRun c ops = some result ∧ result.erase = final ∧ Simulation.Requests.Serves c ops key result := RotationBST.Combo.Worker.Replay.Requests.source_lift c ops final key legal visitModels.Rotation, Models.Online, Models.Pointer.Lazy.Online.Definitions, Algorithms.Combination.Analysis.Definitions, Models.Pointer.Machine, Models.Multifinger.Program.Definitions, Models.Multifinger.Program.Replay.Definitions, Models.Labelled.Cursor.Definitions, Models.Labelled.Cursor.Map.Definitions, Models.Labelled.Metadata.Definitions, Models.Labelled.Intervals.Definitions, Structures.TreeBuffer.Nodes.Definitions, Structures.TreeBuffer.Shared.Definitions, Algorithms.Combination.Controller.Definitions, Algorithms.Combination.Worker.Definitions, Algorithms.Combination.Refinement.Definitions, Algorithms.Combination.Refinement.Visits.Definitions, Algorithms.Combination.Worker.Replay.Definitions, Algorithms.Combination.Worker.Replay.Model, Models.Pointer.Mirror.Definitions, Models.Pointer.Lazy.Skeleton, Models.Labelled.Ordering.Definitions, Structures.PopTart.Definitions, Structures.PopTart.Mirror.Definitions, Structures.PopTart.Chocolate.Definitions, Structures.PopTart.Located.Definitions, Structures.HeavyPath.Definitions, Structures.HeavyPath.Paired.Definitions, Structures.HeavyPath.Paired.Substitution.Definitions, Structures.HeavyPath.Recursive.Definitions, Structures.HeavyPath.Finger.Definitions, Structures.PopTart.Chocolate.Top.Definitions, Structures.HeavyPath.Paired.Exposure.Definitions, Structures.HeavyPath.Recursive.History.Definitions, Structures.HeavyPath.Finger.Transfer.Definitions, Structures.HeavyPath.Finger.History.Definitions, Structures.HeavyPath.Finger.History.Simulation.Definitions, Structures.HeavyPath.Finger.History.Simulation.Requests.Definitions, Bounds.StaticTree.Definitions, Algorithms.Combination.Worker.Replay.Requests.Definitionstheorem planner_source {α σ : Type} {saved : Tree} (planner : Planner σ saved) (side : Bool) : ∃ a : Simulation.Requests.Algorithm α, ∀ (past : List Nat) (c : LabelledCursor.Config α) (xs : List Nat), (∀ x ∈ past, x < saved.size) → c.erase = ⟨planner.tree side (run planner side (planner.initial side) past).1, []⟩ → (∀ x ∈ xs, x < saved.size) → (a.runFrom past c xs).1 = program planner side (run planner side (planner.initial side) past).1 xs ∧ (a.runFrom past c xs).2.erase = ⟨planner.tree side (run planner side (run planner side (planner.initial side) past).1 xs).1, []⟩ ∧ (a.runFrom past c xs).1.length + 2 * xs.length = (run planner side (run planner side (planner.initial side) past).1 xs).2 := RotationBST.Combo.Worker.Replay.Requests.planner_source planner sideModels.Rotation, Models.Online, Models.Pointer.Lazy.Online.Definitions, Algorithms.Combination.Analysis.Definitions, Models.Pointer.Machine, Models.Multifinger.Program.Definitions, Models.Multifinger.Program.Replay.Definitions, Models.Labelled.Cursor.Definitions, Models.Labelled.Cursor.Map.Definitions, Models.Labelled.Metadata.Definitions, Models.Labelled.Intervals.Definitions, Structures.TreeBuffer.Nodes.Definitions, Structures.TreeBuffer.Shared.Definitions, Algorithms.Combination.Controller.Definitions, Algorithms.Combination.Worker.Definitions, Algorithms.Combination.Refinement.Definitions, Algorithms.Combination.Refinement.Visits.Definitions, Algorithms.Combination.Worker.Replay.Definitions, Algorithms.Combination.Worker.Replay.Model, Models.Pointer.Mirror.Definitions, Models.Pointer.Lazy.Skeleton, Models.Labelled.Ordering.Definitions, Structures.PopTart.Definitions, Structures.PopTart.Mirror.Definitions, Structures.PopTart.Chocolate.Definitions, Structures.PopTart.Located.Definitions, Structures.HeavyPath.Definitions, Structures.HeavyPath.Paired.Definitions, Structures.HeavyPath.Paired.Substitution.Definitions, Structures.HeavyPath.Recursive.Definitions, Structures.HeavyPath.Finger.Definitions, Structures.PopTart.Chocolate.Top.Definitions, Structures.HeavyPath.Paired.Exposure.Definitions, Structures.HeavyPath.Recursive.History.Definitions, Structures.HeavyPath.Finger.Transfer.Definitions, Structures.HeavyPath.Finger.History.Definitions, Structures.HeavyPath.Finger.History.Simulation.Definitions, Structures.HeavyPath.Finger.History.Simulation.Requests.Definitions, Bounds.StaticTree.Definitions, Algorithms.Combination.Worker.Replay.Requests.DefinitionsThe constructed source algorithm recomputes the planner state from the history it is given. This is allowed by the abstract model, in which an online algorithm sees its past requests.
29.2.4 Two state types: pairs
The controller of Section 29.3 takes one Controller.Worker, with one state type for both sides. Two independently built workers may have different state types. This happens in the finite families of Section 29.1, where combined workers are themselves paired again. Even when the state types agree, the pair is how two separately built workers become the two sides of one worker, as for the GreedyASS and Tango planners of Chapter 31. The pair construction puts the two inputs, for the same initial tree, behind the sum type α ⊕ β.
Definition (paired worker). Given a left worker on α, a right worker on β, and a chosen side of each (leftSide, rightSide), the paired worker restarts side false as the left worker’s chosen side and side true as the right worker’s chosen side, tagging the state. A step on side false of a left-tagged state is a step of the left worker, and symmetrically on the right. A step with mismatched side and tag does nothing and never completes. The paired charges and pointer model take each side from the selected input.
/-- Tag only the active input worker. The selected source sides are fixedwhen the pair is built, and a step never runs the other input. -/def worker {α β : Type} (left : Controller.Worker α) (right : Controller.Worker β) (leftSide rightSide : Bool) : Controller.Worker (Sum α β) where restart := fun side => if side then .inr (right.restart rightSide) else .inl (left.restart leftSide) step := fun side state x => match side, state with | false, .inl s => let next := left.step leftSide s x; (.inl next.1, next.2) | true, .inr s => let next := right.step rightSide s x; (.inr next.1, next.2) | _, s => (s, false)/-- Each paired side retains the selected input's original charge function. -/def charges (left right : Analysis.Charges) (leftSide rightSide : Bool) : Analysis.Charges := fun side => if side then right rightSide else left leftSide/-- The tagged worker has exactly its selected source cursor and invariant.Mismatched tags are not ready and cannot be reached from a paired restart. -/def model {α β : Type} {left : Controller.Worker α} {right : Controller.Worker β} {saved : Tree} (leftModel : Refinement.WorkerModel left saved) (rightModel : Refinement.WorkerModel right saved) (leftSide rightSide : Bool) : Refinement.WorkerModel (worker left right leftSide rightSide) saved where cursor := fun _ state => match state with | .inl s => leftModel.cursor leftSide s | .inr s => rightModel.cursor rightSide s Ready := fun side state => match side, state with | false, .inl s => leftModel.Ready leftSide s | true, .inr s => rightModel.Ready rightSide s | _, _ => False instruction := fun _ state x => match state with | .inl s => leftModel.instruction leftSide s x | .inr s => rightModel.instruction rightSide s x restart_ready := by intro side; cases side <;> exact (by first | exact leftModel.restart_ready _ | exact rightModel.restart_ready _) restart_cursor := by intro side; cases side <;> exact (by first | exact leftModel.restart_cursor _ | exact rightModel.restart_cursor _) size := by intro side state ready cases side <;> cases state <;> first | contradiction | exact leftModel.size _ _ ready | exact rightModel.size _ _ ready step_ready := by intro side state x ready valid cases side <;> cases state <;> first | contradiction | exact leftModel.step_ready _ _ _ ready valid | exact rightModel.step_ready _ _ _ ready valid step_cursor := by intro side state x ready valid cases side <;> cases state <;> first | contradiction | exact leftModel.step_cursor _ _ _ ready valid | exact rightModel.step_cursor _ _ _ ready validMismatched states are never reached from a restart, and the paired model declares them not ready, so they never matter. The chosen sides let the pair take one side of each input. In Chapter 31, for example, both inputs are replay workers used on their side false.
Theorem (pairing is transparent). An attempt, an access or a request sequence of the paired worker on a tagged state is exactly the tagged image of the same object for the selected input worker, with the same fuel, work and cost.
theorem attempt_left {α β : Type} (left : Controller.Worker α) (right : Controller.Worker β) (leftSide rightSide : Bool) (x fuel : Nat) (state : α) : Controller.attempt (worker left right leftSide rightSide) false x fuel (.inl state) = let result := Controller.attempt left leftSide x fuel state ⟨.inl result.state, result.left, result.done⟩ := RotationBST.Combo.Worker.Pair.attempt_left left right leftSide rightSide x fuel stateModels.Rotation, Models.Pointer.Machine, Models.Multifinger.Program.Definitions, Models.Labelled.Cursor.Definitions, Models.Labelled.Cursor.Map.Definitions, Models.Labelled.Metadata.Definitions, Models.Labelled.Intervals.Definitions, Structures.TreeBuffer.Nodes.Definitions, Structures.TreeBuffer.Shared.Definitions, Algorithms.Combination.Controller.Definitions, Algorithms.Combination.Worker.Definitions, Algorithms.Combination.Rounds.Definitions, Algorithms.Combination.Analysis.Definitions, Algorithms.Combination.Refinement.Definitions, Algorithms.Combination.Refinement.Visits.Definitions, Algorithms.Combination.Worker.Pair.Definitionstheorem attempt_right {α β : Type} (left : Controller.Worker α) (right : Controller.Worker β) (leftSide rightSide : Bool) (x fuel : Nat) (state : β) : Controller.attempt (worker left right leftSide rightSide) true x fuel (.inr state) = let result := Controller.attempt right rightSide x fuel state ⟨.inr result.state, result.left, result.done⟩ := RotationBST.Combo.Worker.Pair.attempt_right left right leftSide rightSide x fuel stateModels.Rotation, Models.Pointer.Machine, Models.Multifinger.Program.Definitions, Models.Labelled.Cursor.Definitions, Models.Labelled.Cursor.Map.Definitions, Models.Labelled.Metadata.Definitions, Models.Labelled.Intervals.Definitions, Structures.TreeBuffer.Nodes.Definitions, Structures.TreeBuffer.Shared.Definitions, Algorithms.Combination.Controller.Definitions, Algorithms.Combination.Worker.Definitions, Algorithms.Combination.Rounds.Definitions, Algorithms.Combination.Analysis.Definitions, Algorithms.Combination.Refinement.Definitions, Algorithms.Combination.Refinement.Visits.Definitions, Algorithms.Combination.Worker.Pair.Definitionstheorem access_left {α β : Type} (left : Controller.Worker α) (right : Controller.Worker β) (leftSide rightSide : Bool) {state : α} {final : Sum α β} {x cost : Nat} : Worker.Access (worker left right leftSide rightSide) false (.inl state) x final cost ↔ ∃ next, final = .inl next ∧ Worker.Access left leftSide state x next cost := RotationBST.Combo.Worker.Pair.access_left left right leftSide rightSideModels.Rotation, Models.Pointer.Machine, Models.Multifinger.Program.Definitions, Models.Labelled.Cursor.Definitions, Models.Labelled.Cursor.Map.Definitions, Models.Labelled.Metadata.Definitions, Models.Labelled.Intervals.Definitions, Structures.TreeBuffer.Nodes.Definitions, Structures.TreeBuffer.Shared.Definitions, Algorithms.Combination.Controller.Definitions, Algorithms.Combination.Worker.Definitions, Algorithms.Combination.Rounds.Definitions, Algorithms.Combination.Analysis.Definitions, Algorithms.Combination.Refinement.Definitions, Algorithms.Combination.Refinement.Visits.Definitions, Algorithms.Combination.Worker.Pair.Definitionstheorem access_right {α β : Type} (left : Controller.Worker α) (right : Controller.Worker β) (leftSide rightSide : Bool) {state : β} {final : Sum α β} {x cost : Nat} : Worker.Access (worker left right leftSide rightSide) true (.inr state) x final cost ↔ ∃ next, final = .inr next ∧ Worker.Access right rightSide state x next cost := RotationBST.Combo.Worker.Pair.access_right left right leftSide rightSideModels.Rotation, Models.Pointer.Machine, Models.Multifinger.Program.Definitions, Models.Labelled.Cursor.Definitions, Models.Labelled.Cursor.Map.Definitions, Models.Labelled.Metadata.Definitions, Models.Labelled.Intervals.Definitions, Structures.TreeBuffer.Nodes.Definitions, Structures.TreeBuffer.Shared.Definitions, Algorithms.Combination.Controller.Definitions, Algorithms.Combination.Worker.Definitions, Algorithms.Combination.Rounds.Definitions, Algorithms.Combination.Analysis.Definitions, Algorithms.Combination.Refinement.Definitions, Algorithms.Combination.Refinement.Visits.Definitions, Algorithms.Combination.Worker.Pair.Definitionstheorem requests_left {α β : Type} (left : Controller.Worker α) (right : Controller.Worker β) (leftSide rightSide : Bool) {state : α} {final : Sum α β} {xs : List Nat} {cost : Nat} : Worker.Requests (worker left right leftSide rightSide) false (.inl state) xs final cost ↔ ∃ next, final = .inl next ∧ Worker.Requests left leftSide state xs next cost := RotationBST.Combo.Worker.Pair.requests_left left right leftSide rightSideModels.Rotation, Models.Pointer.Machine, Models.Multifinger.Program.Definitions, Models.Labelled.Cursor.Definitions, Models.Labelled.Cursor.Map.Definitions, Models.Labelled.Metadata.Definitions, Models.Labelled.Intervals.Definitions, Structures.TreeBuffer.Nodes.Definitions, Structures.TreeBuffer.Shared.Definitions, Algorithms.Combination.Controller.Definitions, Algorithms.Combination.Worker.Definitions, Algorithms.Combination.Rounds.Definitions, Algorithms.Combination.Analysis.Definitions, Algorithms.Combination.Refinement.Definitions, Algorithms.Combination.Refinement.Visits.Definitions, Algorithms.Combination.Worker.Pair.Definitionstheorem requests_right {α β : Type} (left : Controller.Worker α) (right : Controller.Worker β) (leftSide rightSide : Bool) {state : β} {final : Sum α β} {xs : List Nat} {cost : Nat} : Worker.Requests (worker left right leftSide rightSide) true (.inr state) xs final cost ↔ ∃ next, final = .inr next ∧ Worker.Requests right rightSide state xs next cost := RotationBST.Combo.Worker.Pair.requests_right left right leftSide rightSideModels.Rotation, Models.Pointer.Machine, Models.Multifinger.Program.Definitions, Models.Labelled.Cursor.Definitions, Models.Labelled.Cursor.Map.Definitions, Models.Labelled.Metadata.Definitions, Models.Labelled.Intervals.Definitions, Structures.TreeBuffer.Nodes.Definitions, Structures.TreeBuffer.Shared.Definitions, Algorithms.Combination.Controller.Definitions, Algorithms.Combination.Worker.Definitions, Algorithms.Combination.Rounds.Definitions, Algorithms.Combination.Analysis.Definitions, Algorithms.Combination.Refinement.Definitions, Algorithms.Combination.Refinement.Visits.Definitions, Algorithms.Combination.Worker.Pair.DefinitionsTheorem (pairing preserves the hypotheses). If both input models serve their requests, so does the paired model. If both inputs are well behaved with the same key count , overhead and constants , then the paired worker is well behaved for the paired charges with the same parameters.
theorem correct {α β : Type} {left : Controller.Worker α} {right : Controller.Worker β} {saved : Tree} (leftModel : Refinement.WorkerModel left saved) (rightModel : Refinement.WorkerModel right saved) (leftSide rightSide : Bool) (leftCorrect : Refinement.Visits.Correct leftModel) (rightCorrect : Refinement.Visits.Correct rightModel) : Refinement.Visits.Correct (model leftModel rightModel leftSide rightSide) := RotationBST.Combo.Worker.Pair.correct leftModel rightModel leftSide rightSide leftCorrect rightCorrectModels.Rotation, Models.Pointer.Machine, Models.Multifinger.Program.Definitions, Models.Labelled.Cursor.Definitions, Models.Labelled.Cursor.Map.Definitions, Models.Labelled.Metadata.Definitions, Models.Labelled.Intervals.Definitions, Structures.TreeBuffer.Nodes.Definitions, Structures.TreeBuffer.Shared.Definitions, Algorithms.Combination.Controller.Definitions, Algorithms.Combination.Worker.Definitions, Algorithms.Combination.Rounds.Definitions, Algorithms.Combination.Analysis.Definitions, Algorithms.Combination.Refinement.Definitions, Algorithms.Combination.Refinement.Visits.Definitions, Algorithms.Combination.Worker.Pair.Definitionstheorem well_behaved {α β : Type} {left : Controller.Worker α} {right : Controller.Worker β} {n overhead C₀ C₁ : Nat} {leftCharges rightCharges : Analysis.Charges} (leftWell : Analysis.WellBehaved left n overhead C₀ C₁ leftCharges) (rightWell : Analysis.WellBehaved right n overhead C₀ C₁ rightCharges) (leftSide rightSide : Bool) : Analysis.WellBehaved (worker left right leftSide rightSide) n overhead C₀ C₁ (charges leftCharges rightCharges leftSide rightSide) := RotationBST.Combo.Worker.Pair.well_behaved leftWell rightWell leftSide rightSideModels.Rotation, Models.Pointer.Machine, Models.Multifinger.Program.Definitions, Models.Labelled.Cursor.Definitions, Models.Labelled.Cursor.Map.Definitions, Models.Labelled.Metadata.Definitions, Models.Labelled.Intervals.Definitions, Structures.TreeBuffer.Nodes.Definitions, Structures.TreeBuffer.Shared.Definitions, Algorithms.Combination.Controller.Definitions, Algorithms.Combination.Worker.Definitions, Algorithms.Combination.Rounds.Definitions, Algorithms.Combination.Analysis.Definitions, Algorithms.Combination.Refinement.Definitions, Algorithms.Combination.Refinement.Visits.Definitions, Algorithms.Combination.Worker.Pair.DefinitionsThe well-behavedness transfer asks for a common , , and . Inputs with different constants must first be brought to common ones; the pairing theorem does not do this. Each side keeps its own original charges, so a later bound in terms of compares the combined tree with each input’s own bound.