Chapter 29 · Combining search trees · Section 29.3Alternating rounds
Combo-BST serves one request sequence with two BST algorithms and , but it holds only one tree. Demaine et al. (2013) arrange this, in Section 4 of their paper, in rounds. A round runs one algorithm, from the fixed initial tree , for a fixed number of steps . When the budget runs out in the middle of a request , the tree is restored to , the other algorithm becomes active, and the new round starts again at . The analysis in Section 5 of the paper charges each round to the algorithm that is cheaper on the whole sequence. Every round of that algorithm except its last one used up its budget, so it served a piece of the sequence that costs it at least . Well-behavedness turns this into a lower bound on its amortized charge for that piece.
This section formalizes that bookkeeping in three layers, all under Algorithms/Combination:
Controller: the executable controller. It runs bounded attempts of one worker, switches workers when an attempt is exhausted, and follows a finite input prefix. The exact work accounting is proved here.Rounds: the rounds of a run, as intervals of request positions. They are computed from the controller’s switch positions; the controller does not store them. The theorems count the rounds and add up charges over them.Rounds.Certificates: closed-round certificates. Every round except the last one is a complete record of an exhausted round started from the restart state, so rerunning its requests from that state costs more than the budget.
The combination theorem of Section 29.1, and the combined finger theorem of Chapter 31 built on it, are stated for an online policy that runs Controller.serve under the fresh-round hypothesis Controller.FreshBound. These two definitions are on the reading path of both results, together with what they use: the worker, attempts, controller states and results. Section 29.3.2 and Section 29.3.3 explain them. The rest of the section is proof machinery: the rounds, their certificates, and the counting behind Section 5 of the paper. Read it if you want to see where the factors and of the final bound come from. Section 29.3.7 sketches how the analysis of Section 29.1 assembles them.
29.3.1 Overview
Here is a run with budget . Worker completes and at cost each, then runs out of budget during . The tree is restored, and starts from scratch at .
positions 0 1 2 3 4 5 6
round 1 (A0) [ x0 x1 x2 ] budget exhausted in x2
restore
round 2 (A1) [ x2 x3 x4 x5 ] budget exhausted in x5
restore
round 3 (A0) [ x5 x6 still open
Three features of this picture recur throughout.
- Rounds share their interrupted request. Position belongs to round 1, whose attempt at failed, and to round 2, which served it. The library keeps the failed request in the closing round, so rounds of different workers overlap in one position. Rounds of the same worker never overlap: the paper’s “no access gets executed by the same BST data structure in more than one round”.
- Every round restarts. Round 3 runs from the initial tree on . It does not resume where round 1 stopped. Each round is thus a standalone run of one algorithm on a contiguous piece of the sequence, which is why well-behaved bounds are stated for contiguous subsequences.
- Only the last round is open. Rounds 1 and 2 are closed: their budgets were spent exactly. Round 3 may still have budget left when the prefix ends.
The budget is fixed. The overhead is known in advance, so every round of the paper has the same budget , and the library does the same. The controller takes one budget for all rounds. The analysis of Section 29.1 instantiates it as Analysis.budget , that is, , where is the overhead and is the additive constant of well-behavedness. No budget doubling takes place. The cost of restoring the tree is likewise a fixed number restore, charged at every switch. The paper pays for LoadState. The analysis takes restore to be a multiple of , and the physical restoration is in Section 29.4.
What a worker is here. At this level a worker is not yet a BST. It is a pure state machine whose steps each cost one unit of work and which reports when the current request has been served. Section 29.2 defines what it means for such a worker to serve whole request sequences, and how two algorithms with different state types become one worker. Section 29.4 connects the steps to physical pointer operations on a shared tree.
29.3.2 Workers and bounded attempts
A controller worker packages both algorithms over one common state type σ. The Boolean side selects the algorithm: false for and true for .
/-- Two causal workers with a common state type. One call performs one chargedunit and reports whether the current request has completed. `restart` is theworker state after restoring the fixed initial tree. Physical implementationof these operations is a separate refinement obligation. -/structure Worker (σ : Type) where restart : Bool → σ step : Bool → σ → Nat → σ × Boolrestart side is the state of algorithm side immediately after the tree has been restored to the initial tree . It is the state in which every round starts. step side s x performs one charged unit of work of algorithm side, in state s, on the current request key x. It returns the new state and a completion bit, which is true when the step finishes request x. The step sees only the current state and request, never future requests, so any worker is online by construction. Several algorithms with different state types are combined into this common format in Section 29.2.
An attempt runs one worker on the current request with a limited amount of fuel:
structure Attempt (σ : Type) where state : σ left : Nat done : Bool deriving DecidableEq/-- Run only the current request, stopping immediately on completion or budgetexhaustion. A failed attempt retains its partial worker state for restoration. -/def attempt {σ : Type} (worker : Worker σ) (side : Bool) (x : Nat) : Nat → σ → Attempt σ | 0, s => ⟨s, 0, false⟩ | fuel + 1, s => let next := worker.step side s x if next.2 then ⟨next.1, fuel, true⟩ else attempt worker side x fuel next.1attempt worker side x fuel s steps until the completion bit appears or the fuel runs out, whichever comes first. The result records the state reached, the fuel left, and whether the request was completed (done). With zero fuel no step is taken and the attempt fails at once. A failed attempt keeps its partial state, but the controller discards it, since it restores the tree instead.
The same process as an inductive trace, with an explicit work count:
/-- Operational trace of one attempted request. The budget decreases once perworker step, and a completion bit stops the attempt immediately. -/inductive AttemptRun {σ : Type} (worker : Worker σ) (side : Bool) (x : Nat) : Nat → σ → Attempt σ → Nat → Prop where | exhausted {s : σ} : AttemptRun worker side x 0 s ⟨s, 0, false⟩ 0 | done {fuel : Nat} {s t : σ} : worker.step side s x = (t, true) → AttemptRun worker side x (fuel + 1) s ⟨t, fuel, true⟩ 1 | more {fuel work : Nat} {s t : σ} {result : Attempt σ} : worker.step side s x = (t, false) → AttemptRun worker side x fuel t result work → AttemptRun worker side x (fuel + 1) s result (1 + work)The three constructors are an attempt with no fuel (exhausted, work ), one step that completes (done, work ), and one step that does not complete followed by the rest of the attempt (more). Each step costs exactly one unit of fuel. The function and the trace agree, and the work is the fuel consumed:
theorem attempt_run {σ : Type} (worker : Worker σ) (side : Bool) (x fuel : Nat) (s : σ) : AttemptRun worker side x fuel s (attempt worker side x fuel s) (fuel - (attempt worker side x fuel s).left) := RotationBST.Combo.Controller.attempt_run worker side x fuel sAlgorithms.Combination.Controller.Definitionstheorem AttemptRun.failed {σ : Type} {worker : Worker σ} {side : Bool} {x fuel work : Nat} {s : σ} {result : Attempt σ} (run : AttemptRun worker side x fuel s result work) (failed : result.done = false) : result.left = 0 ∧ work = fuel := RotationBST.Combo.Controller.AttemptRun.failed run failedAlgorithms.Combination.Controller.Definitionstheorem attempt_extra {σ : Type} (worker : Worker σ) (side : Bool) (x fuel extra : Nat) (s : σ) (done : (attempt worker side x fuel s).done = true) : attempt worker side x (fuel + extra) s = { attempt worker side x fuel s with left := (attempt worker side x fuel s).left + extra } := RotationBST.Combo.Controller.attempt_extra worker side x fuel extra s doneAlgorithms.Combination.Controller.Definitionsattempt_run says that attempt produces an AttemptRun whose work is fuel - left. AttemptRun.failed says that a failed attempt has consumed all of its fuel: left = 0 and work = fuel. This is the formal “each round but the last one runs for steps exactly”. attempt_extra says that more fuel does not change a completed attempt: the same state is reached and the extra fuel is left over. A worker that finishes within some bound therefore finishes within any larger budget.
29.3.3 Serving one request
The controller state records the active side, the active worker’s state, and the budget left in the current round:
structure State (σ : Type) where side : Bool workerState : σ left : Nat deriving DecidableEqdef initial {σ : Type} (worker : Worker σ) (budget : Nat) : State σ := ⟨false, worker.restart false, budget⟩structure Result (σ : Type) where state : State σ done : Bool switched : Bool work : Nat deriving DecidableEqinitial worker budget starts with (side false) in its restart state and a full budget. A Result reports the new state, whether the request was served (done), whether a switch occurred (switched), and the work charged for this request.
/-- At most one switch per request. Both branches use only the current inputand state. The supplied restore charge remains explicit. A second failedattempt is reported as failure, never silently accepted as a served request. -/def serve {σ : Type} (worker : Worker σ) (budget restore : Nat) (s : State σ) (x : Nat) : Result σ := let first := attempt worker s.side x s.left s.workerState if first.done then ⟨⟨s.side, first.state, first.left⟩, true, false, s.left - first.left⟩ else let second := attempt worker (!s.side) x budget (worker.restart (!s.side)) ⟨⟨!s.side, second.state, second.left⟩, second.done, true, s.left - first.left + restore + (budget - second.left)⟩serve worker budget restore s x is the controller’s response to one request:
- Attempt
xwith the active worker, from its current state, with the round’s remaining fuels.left. If it completes, stay in the same round, with the state and fuel the attempt left. The work is the fuel consumed. - Otherwise switch. Flip the side, start the other worker in its restart state, and attempt
xwith a fresh, full budget. The work is the fuel the first attempt consumed (all ofs.left, byAttemptRun.failed), plusrestore, plus the fuel the second attempt consumed.
So there is at most one switch per request. If the second attempt fails too, serve reports done = false. The request is never silently counted as served. Under the fresh-round hypothesis below this case does not happen. The relational version spells out the two cases with their traces:
/-- Trace of the controller's one or two attempts. Restoring the original stateis an explicit charged boundary between them, not parallel worker execution. -/inductive Service {σ : Type} (worker : Worker σ) (budget restore : Nat) (s : State σ) (x : Nat) : Result σ → Prop where | finish {first : Attempt σ} {work : Nat} : AttemptRun worker s.side x s.left s.workerState first work → first.done = true → Service worker budget restore s x ⟨⟨s.side, first.state, first.left⟩, true, false, work⟩ | switch {first second : Attempt σ} {before after : Nat} : AttemptRun worker s.side x s.left s.workerState first before → first.done = false → AttemptRun worker (!s.side) x budget (worker.restart (!s.side)) second after → Service worker budget restore s x ⟨⟨!s.side, second.state, second.left⟩, second.done, true, before + restore + after⟩In the switch case the restore cost restore sits between the two attempts. The two workers run one after the other, never in parallel. Three facts about one request:
theorem serve_service {σ : Type} (worker : Worker σ) (budget restore : Nat) (s : State σ) (x : Nat) : Service worker budget restore s x (serve worker budget restore s x) := RotationBST.Combo.Controller.serve_service worker budget restore s xAlgorithms.Combination.Controller.Definitionstheorem serve_left {σ : Type} (worker : Worker σ) (budget restore : Nat) (s : State σ) (x : Nat) (limit : s.left ≤ budget) : (serve worker budget restore s x).state.left ≤ budget := RotationBST.Combo.Controller.serve_left worker budget restore s x limitAlgorithms.Combination.Controller.Definitionstheorem serve_alternates {σ : Type} (worker : Worker σ) (budget restore : Nat) (s : State σ) (x : Nat) : (serve worker budget restore s x).state.side = if (serve worker budget restore s x).switched then !s.side else s.side := RotationBST.Combo.Controller.serve_alternates worker budget restore s xAlgorithms.Combination.Controller.Definitionsserve_service says that serve satisfies Service. serve_left says that the remaining fuel never exceeds the budget. serve_alternates says that the side changes exactly when switched is set.
Fresh rounds always make progress. The paper takes “suitably large” so that every round completes at least one access. The library states this as a hypothesis on the worker:
/-- The paper's single-access hypothesis at a fresh round, restricted to validkeys. Larger round budgets will inherit this guarantee. -/def FreshBound {σ : Type} (worker : Worker σ) (n bound : Nat) : Prop := ∀ side x, x < n → (attempt worker side x bound (worker.restart side)).done = trueFreshBound worker n bound says that, from its restart state, either algorithm completes any single valid request within bound units of work. This is the single-access part of well-behavedness, applied to the first request of a round. Results.fresh derives it, with bound , from Analysis.WellBehaved. The budget is strictly larger.
theorem serve_done {σ : Type} (worker : Worker σ) (n bound budget restore : Nat) (bounded : FreshBound worker n bound) (enough : bound ≤ budget) (s : State σ) (x : Nat) (valid : x < n) : (serve worker budget restore s x).done = true := RotationBST.Combo.Controller.serve_done worker n bound budget restore bounded enough s x validAlgorithms.Combination.Controller.Definitionstheorem fresh_completes {σ : Type} (worker : Worker σ) (n bound budget restore : Nat) (bounded : FreshBound worker n bound) (enough : bound ≤ budget) (side : Bool) (x : Nat) (valid : x < n) : (serve worker budget restore ⟨side, worker.restart side, budget⟩ x).switched = false ∧ (serve worker budget restore ⟨side, worker.restart side, budget⟩ x).work ≤ bound := RotationBST.Combo.Controller.fresh_completes worker n bound budget restore bounded enough side x validAlgorithms.Combination.Controller.DefinitionsIf bound ≤ budget, then serve_done says that every valid request is served, whatever the controller state. In the worst case, the retry runs from a fresh restart with a full budget, and FreshBound guarantees that it completes. fresh_completes says that a request served from a fresh round state (restart state, full budget) does not switch and costs at most bound.
29.3.4 Running a prefix
run feeds a list of requests to serve, adds up the work, and stops with none at the first failure. Execution is the corresponding trace. It also counts the switches, a quantity used only in proofs: the controller does not store it.
/-- Execute a finite online input prefix. A reported failure stops the run. -/def run {σ : Type} (worker : Worker σ) (budget restore : Nat) : State σ → List Nat → Option (State σ × Nat) | s, [] => some (s, 0) | s, x :: xs => let next := serve worker budget restore s x if next.done then (run worker budget restore next.state xs).map fun result => (result.1, next.work + result.2) else none/-- Completed request executions. The switch count is proof accounting, notadditional unbounded runtime history retained by the controller. -/inductive Execution {σ : Type} (worker : Worker σ) (budget restore : Nat) : State σ → List Nat → State σ → Nat → Nat → Prop where | nil {s : State σ} : Execution worker budget restore s [] s 0 0 | cons {s t : State σ} {x : Nat} {xs : List Nat} {cost switches : Nat} : (serve worker budget restore s x).done = true → Execution worker budget restore (serve worker budget restore s x).state xs t cost switches → Execution worker budget restore s (x :: xs) t ((serve worker budget restore s x).work + cost) ((if (serve worker budget restore s x).switched then 1 else 0) + switches)The first theorem says that under FreshBound the controller never fails, with a crude per-request bound:
theorem complete {σ : Type} (worker : Worker σ) (n bound budget restore : Nat) (bounded : FreshBound worker n bound) (enough : bound ≤ budget) (s : State σ) (limit : s.left ≤ budget) (xs : List Nat) (valid : ∀ x ∈ xs, x < n) : ∃ final cost switches, Execution worker budget restore s xs final cost switches ∧ run worker budget restore s xs = some (final, cost) ∧ final.left ≤ budget ∧ cost ≤ (2 * budget + restore) * xs.length := RotationBST.Combo.Controller.complete worker n bound budget restore bounded enough s limit xs validAlgorithms.Combination.Controller.DefinitionsFrom any state whose fuel is within the budget, and for any list of valid keys, run returns some (final, cost), an Execution with the same final state and cost exists, the final fuel is still within the budget, and the cost is at most per request. The sharper bound, relative to the cheaper algorithm, needs the rounds below.
The central accounting identity is exact:
theorem Execution.account {σ : Type} {worker : Worker σ} {budget restore : Nat} {s t : State σ} {xs : List Nat} {cost switches : Nat} (execution : Execution worker budget restore s xs t cost switches) : cost + t.left = s.left + switches * (budget + restore) := RotationBST.Combo.Controller.Execution.account executionAlgorithms.Combination.Controller.DefinitionsThe work of a run is the fuel available at the start, plus a full budget and one restore per switch, minus the fuel still unspent at the end. Since AttemptRun.failed makes every exhausted round spend all of its fuel, the identity holds with equality. The paper’s Equation (1), for rounds, follows from it by counting switches.
theorem Execution.switches {σ : Type} {worker : Worker σ} {budget restore : Nat} {s t : State σ} {xs : List Nat} {cost switches : Nat} (execution : Execution worker budget restore s xs t cost switches) : switches ≤ xs.length := RotationBST.Combo.Controller.Execution.switches executionAlgorithms.Combination.Controller.Definitionstheorem Execution.left_le {σ : Type} {worker : Worker σ} {budget restore : Nat} {s t : State σ} {xs : List Nat} {cost switches : Nat} (execution : Execution worker budget restore s xs t cost switches) (limit : s.left ≤ budget) : t.left ≤ budget := RotationBST.Combo.Controller.Execution.left_le execution limitAlgorithms.Combination.Controller.Definitionstheorem run_append {σ : Type} (worker : Worker σ) (budget restore : Nat) (s : State σ) (xs ys : List Nat) : run worker budget restore s (xs ++ ys) = (run worker budget restore s xs).bind fun first => (run worker budget restore first.1 ys).map fun last => (last.1, first.2 + last.2) := RotationBST.Combo.Controller.run_append worker budget restore s xs ysAlgorithms.Combination.Controller.Definitionstheorem Execution.split {σ : Type} {worker : Worker σ} {budget restore : Nat} {s t : State σ} {xs ys : List Nat} {cost switches : Nat} (execution : Execution worker budget restore s (xs ++ ys) t cost switches) : ∃ middle beforeCost afterCost beforeSwitches afterSwitches, Execution worker budget restore s xs middle beforeCost beforeSwitches ∧ Execution worker budget restore middle ys t afterCost afterSwitches ∧ cost = beforeCost + afterCost ∧ switches = beforeSwitches + afterSwitches := RotationBST.Combo.Controller.Execution.split executionAlgorithms.Combination.Controller.Definitionstheorem Execution.window {σ : Type} {worker : Worker σ} {budget restore : Nat} {s t : State σ} {xs : List Nat} {cost switches lo hi : Nat} (execution : Execution worker budget restore s xs t cost switches) (ordered : lo ≤ hi) : ∃ beforeState afterState beforeCost windowCost afterCost beforeSwitches windowSwitches afterSwitches, Execution worker budget restore s (xs.take lo) beforeState beforeCost beforeSwitches ∧ Execution worker budget restore beforeState ((xs.drop lo).take (hi - lo)) afterState windowCost windowSwitches ∧ Execution worker budget restore afterState (xs.drop hi) t afterCost afterSwitches ∧ cost = beforeCost + windowCost + afterCost ∧ switches = beforeSwitches + windowSwitches + afterSwitches := RotationBST.Combo.Controller.Execution.window execution orderedAlgorithms.Combination.Controller.DefinitionsThese are the composition lemmas. There is at most one switch per request (Execution.switches), and the fuel stays within the budget along a whole run (Execution.left_le). Running xs ++ ys is running xs and then ys from the state reached (run_append), and an execution of xs ++ ys splits into two executions whose costs and switch counts add up (Execution.split). Execution.window splits a run at positions into a prefix, a window and a suffix, with costs and switches adding up exactly. The window part starts from whatever state the prefix left. It is the controller’s own state at position , not a fresh restart. The window theorems of Section 29.1 and Section 29.4 bound this actual middle segment of the actual run.
29.3.5 Rounds as spans
A round is recorded as a span of request positions, not of key values: equal keys at different positions are different requests and are charged separately.
/-- Request positions, not key values: equal requests at different indices arecharged separately. `stop` is exclusive. -/structure Span where side : Bool start : Nat stop : Nat deriving DecidableEqdef Span.Contains (span : Span) (index : Nat) : Prop := span.start ≤ index ∧ index < span.stopA Span has the side that ran it and the half-open interval [start, stop) of positions it covers.
The spans of a run are computed in two steps. cuts replays the controller and lists the positions at which a switch happened. partition turns a list of switch positions into spans of alternating sides. A span that ends at switch position stops at , so that it includes the interrupted request, and the next span starts at . spans applies both to an input prefix whose first position is offset.
/-- Proof accounting for switch positions in the actual executable controller.A failed request ends the prefix, exactly as in `Controller.run`. -/def cuts {σ : Type} (worker : Controller.Worker σ) (budget restore : Nat) : Controller.State σ → Nat → List Nat → List Nat | _, _, [] => [] | s, offset, x :: xs => let next := Controller.serve worker budget restore s x if next.done then let rest := cuts worker budget restore next.state (offset + 1) xs if next.switched then offset :: rest else rest else []/-- Consecutive rounds share at most their interrupted request. The closinground includes that index even if its budget was already zero, which is aconservative coverage convention; no extra worker instruction is inferred. -/def partition (side : Bool) (start stop : Nat) : List Nat → List Span | [] => [⟨side, start, stop⟩] | boundary :: rest => ⟨side, start, boundary + 1⟩ :: partition (!side) boundary stop rest/-- Round coverage of an executed input prefix. The list is a proof witness;it is not stored by the online controller. -/def spans {σ : Type} (worker : Controller.Worker σ) (budget restore : Nat) (s : Controller.State σ) (offset : Nat) (xs : List Nat) : List Span := partition s.side offset (offset + xs.length) (cuts worker budget restore s offset xs)The convention that the closing round includes its interrupted request is conservative. If the round’s fuel was already zero when the request arrived, the round did no work on it, but the request still belongs to the round. No extra worker step is inferred. The convention makes every closed round end with a failed attempt, which is the shape the certificates below need. In the overview example, cuts is [2, 5] and spans is [⟨A₀, 0, 3⟩, ⟨A₁, 2, 6⟩, ⟨A₀, 5, 7⟩].
Two more definitions give the vocabulary of the counting argument. Separated says that, in a list of spans, an earlier span and a later span of the same side do not overlap. count side is the number of spans of that side.
/-- Earlier and later rounds of the same worker have disjoint request ranges. -/def Separated (rounds : List Span) : Prop := rounds.Pairwise fun a b => a.side = b.side → a.stop ≤ b.startdef count (side : Bool) (rounds : List Span) : Nat := (rounds.filter fun span => span.side == side).lengthThe structural facts about rounds:
theorem cuts_ordered {σ : Type} (worker : Controller.Worker σ) (budget restore : Nat) (s : Controller.State σ) (offset : Nat) (xs : List Nat) : (cuts worker budget restore s offset xs).Pairwise (· < ·) ∧ ∀ i ∈ cuts worker budget restore s offset xs, offset ≤ i ∧ i < offset + xs.length := RotationBST.Combo.Rounds.cuts_ordered worker budget restore s offset xsAlgorithms.Combination.Controller.Definitions, Algorithms.Combination.Rounds.Definitionstheorem cuts_length {σ : Type} {worker : Controller.Worker σ} {budget restore : Nat} {s t : Controller.State σ} {xs : List Nat} {cost switches : Nat} (execution : Controller.Execution worker budget restore s xs t cost switches) (offset : Nat) : (cuts worker budget restore s offset xs).length = switches := RotationBST.Combo.Rounds.cuts_length execution offsetAlgorithms.Combination.Controller.Definitions, Algorithms.Combination.Rounds.Definitionstheorem spans_bounds {σ : Type} (worker : Controller.Worker σ) (budget restore : Nat) (s : Controller.State σ) (offset : Nat) (xs : List Nat) : ∀ span ∈ spans worker budget restore s offset xs, offset ≤ span.start ∧ span.start ≤ span.stop ∧ span.stop ≤ offset + xs.length := RotationBST.Combo.Rounds.spans_bounds worker budget restore s offset xsAlgorithms.Combination.Controller.Definitions, Algorithms.Combination.Rounds.Definitionstheorem spans_separated {σ : Type} (worker : Controller.Worker σ) (budget restore : Nat) (s : Controller.State σ) (offset : Nat) (xs : List Nat) : Separated (spans worker budget restore s offset xs) := RotationBST.Combo.Rounds.spans_separated worker budget restore s offset xsAlgorithms.Combination.Controller.Definitions, Algorithms.Combination.Rounds.Definitionstheorem spans_length {σ : Type} {worker : Controller.Worker σ} {budget restore : Nat} {s t : Controller.State σ} {xs : List Nat} {cost switches : Nat} (execution : Controller.Execution worker budget restore s xs t cost switches) (offset : Nat) : (spans worker budget restore s offset xs).length = switches + 1 := RotationBST.Combo.Rounds.spans_length execution offsetAlgorithms.Combination.Controller.Definitions, Algorithms.Combination.Rounds.Definitionstheorem rounds_count {σ : Type} {worker : Controller.Worker σ} {budget restore : Nat} {s t : Controller.State σ} {xs : List Nat} {cost switches : Nat} (execution : Controller.Execution worker budget restore s xs t cost switches) (offset : Nat) (selected : Bool) : switches + 1 ≤ 2 * count selected (spans worker budget restore s offset xs) + 1 := RotationBST.Combo.Rounds.rounds_count execution offset selectedAlgorithms.Combination.Controller.Definitions, Algorithms.Combination.Rounds.DefinitionsThe switch positions are strictly increasing and lie inside the prefix (cuts_ordered). There is one per switch of the execution (cuts_length), so there are spans (spans_length). Each span lies inside the prefix and has start ≤ stop (spans_bounds). Same-side spans are separated (spans_separated). The proof uses that switch positions increase strictly, so the next same-side round begins at or after the next switch. rounds_count is the paper’s “at most rounds, of them performed by ”: for either side, the total number of rounds is at most , since the sides alternate. The final round may be empty, for example on the empty prefix.
Charges are attached to positions of the original sequence by a weight function. A span’s charge is the sum of the weights of its positions, and a side’s charge is the sum over that side’s spans:
/-- The original sequence's amortized charges over this contiguous span. -/def Span.charge (weights : Nat → ℝ) (span : Span) : ℝ := ∑ i ∈ Finset.Ico span.start span.stop, weights idef charge (selected : Bool) (weights : Nat → ℝ) (rounds : List Span) : ℝ := ((rounds.filter fun span => span.side == selected).map (Span.charge weights)).sumIn the analysis the weights are the amortized charges of the paper, read at their positions in the original sequence . Rounds are thus charged with the history of the whole sequence, not as if they were the start of a fresh sequence. The two quantitative consequences:
theorem work_by_rounds {σ : Type} {worker : Controller.Worker σ} {budget restore : Nat} {s t : Controller.State σ} {xs : List Nat} {cost switches : Nat} (execution : Controller.Execution worker budget restore s xs t cost switches) (limit : s.left ≤ budget) (offset : Nat) (selected : Bool) : cost ≤ (2 * count selected (spans worker budget restore s offset xs) + 1) * (budget + restore) := RotationBST.Combo.Rounds.work_by_rounds execution limit offset selectedAlgorithms.Combination.Controller.Definitions, Algorithms.Combination.Rounds.Definitionstheorem charge_le_interval {σ : Type} (worker : Controller.Worker σ) (budget restore : Nat) (s : Controller.State σ) (offset : Nat) (xs : List Nat) (selected : Bool) (weights : Nat → ℝ) (nonneg : ∀ i, 0 ≤ weights i) : charge selected weights (spans worker budget restore s offset xs) ≤ ∑ i ∈ Finset.Ico offset (offset + xs.length), weights i := RotationBST.Combo.Rounds.charge_le_interval worker budget restore s offset xs selected weights nonnegAlgorithms.Combination.Controller.Definitions, Algorithms.Combination.Rounds.Definitionswork_by_rounds bounds the work of an execution that starts within the budget by the number of rounds of either side: . It combines Execution.account, cuts_length and rounds_count. charge_le_interval says that, for nonnegative weights, one side’s rounds together carry at most the total weight of the prefix. This is the paper’s “the are disjoint subsets of ”. It follows from separation. Without the nonnegativity hypothesis it could fail, and the weights of the two sides cannot be added: their rounds overlap in the interrupted positions.
29.3.6 Closed-round certificates
Counting rounds only bounds the work in terms of . To bound , the paper shows that each closed round of is expensive: “ for all ”. The certificates make “closed” precise. They use two relations from Section 29.2: Worker.Requests, a worker serving a list of requests to completion, and Worker.ClosedRound, a round in which every request but the last completes and the last attempt is exhausted, starting from a given state with a given fuel.
/-- A ghost record of one exhausted round: its global request interval andits actual request values. No record list is stored by the controller. -/structure Record where span : Span requests : List Nat deriving DecidableEq/-- The current round began at the fixed restart state, completed `earlier`,and has exactly the unspent part of its original budget remaining. -/def Active {σ : Type} (worker : Controller.Worker σ) (budget : Nat) (s : Controller.State σ) (earlier : List Nat) : Prop := ∃ spent, Worker.Requests worker s.side (worker.restart s.side) earlier s.workerState spent ∧ spent + s.left = budget/-- Extract completed-round proof records while following the actualcontroller. The successful retry belongs to the next round's completed prefix. -/def records {σ : Type} (worker : Controller.Worker σ) (budget restore : Nat) : Controller.State σ → Nat → Nat → List Nat → List Nat → List Record | _, _, _, _, [] => [] | s, start, offset, earlier, x :: xs => let next := Controller.serve worker budget restore s x if next.done then if next.switched then ⟨⟨s.side, start, offset + 1⟩, earlier ++ [x]⟩ :: records worker budget restore next.state offset (offset + 1) [x] xs else records worker budget restore next.state start (offset + 1) (earlier ++ [x]) xs else []A Record is a span together with the request values it covers. It is a ghost: the controller never builds a list of records. Active worker budget s earlier is the invariant of the current round. The round began at restart s.side, its worker has served the requests earlier to completion, and the work spent on them plus the fuel left is exactly budget. records follows the controller and emits a record each time a round closes. The interrupted request ends the closing record, and the same request, now served by the retry, begins the earlier list of the next round.
theorem initial_active {σ : Type} (worker : Controller.Worker σ) (budget : Nat) : Active worker budget (Controller.initial worker budget) [] := RotationBST.Combo.Rounds.Certificates.initial_active worker budgettheorem records_closed {σ : Type} {worker : Controller.Worker σ} {budget restore : Nat} {s t : Controller.State σ} {xs : List Nat} {cost switches : Nat} (execution : Controller.Execution worker budget restore s xs t cost switches) (start offset : Nat) (earlier : List Nat) (active : Active worker budget s earlier) : ∀ record ∈ records worker budget restore s start offset earlier xs, Worker.ClosedRound worker record.span.side (worker.restart record.span.side) budget record.requests := RotationBST.Combo.Rounds.Certificates.records_closed execution start offset earlier activetheorem records_spans {σ : Type} (worker : Controller.Worker σ) (budget restore : Nat) (s : Controller.State σ) (start offset : Nat) (earlier xs : List Nat) : (records worker budget restore s start offset earlier xs).map Record.span = (partition s.side start (offset + xs.length) (cuts worker budget restore s offset xs)).dropLast := RotationBST.Combo.Rounds.Certificates.records_spans worker budget restore s start offset earlier xstheorem records_bounds {σ : Type} (worker : Controller.Worker σ) (budget restore : Nat) (s : Controller.State σ) (start offset : Nat) (earlier xs : List Nat) (before : start ≤ offset) : ∀ record ∈ records worker budget restore s start offset earlier xs, start ≤ record.span.start ∧ record.span.start ≤ record.span.stop ∧ record.span.stop ≤ offset + xs.length := RotationBST.Combo.Rounds.Certificates.records_bounds worker budget restore s start offset earlier xs beforetheorem records_slice {σ : Type} (worker : Controller.Worker σ) (budget restore : Nat) (s : Controller.State σ) (start offset : Nat) (earlier xs : List Nat) (aligned : start + earlier.length = offset) : ∀ record ∈ records worker budget restore s start offset earlier xs, record.requests = ((earlier ++ xs).drop (record.span.start - start)).take (record.span.stop - record.span.start) := RotationBST.Combo.Rounds.Certificates.records_slice worker budget restore s start offset earlier xs alignedtheorem records_tail_closed {σ : Type} {worker : Controller.Worker σ} {budget restore : Nat} {s t : Controller.State σ} {xs : List Nat} {cost switches : Nat} (execution : Controller.Execution worker budget restore s xs t cost switches) (start offset : Nat) (earlier : List Nat) : ∀ record ∈ (records worker budget restore s start offset earlier xs).tail, Worker.ClosedRound worker record.span.side (worker.restart record.span.side) budget record.requests := RotationBST.Combo.Rounds.Certificates.records_tail_closed execution start offset earlierThe controller’s initial state is active with nothing served (initial_active). If the run starts in an active state, every record is a ClosedRound of its side, from that side’s restart state, with fuel exactly budget (records_closed). The records’ spans are the round spans with the last, open, one removed (records_spans). They lie in the prefix (records_bounds), and their request lists are the corresponding slices of the input (records_slice). Without the Active hypothesis, every record except the first is still closed (records_tail_closed): any round that began at a switch began from a restart with a full budget.
Read in terms of spans, these give the three statements that the analysis uses:
theorem closed_spans {σ : Type} {worker : Controller.Worker σ} {budget restore : Nat} {final : Controller.State σ} {xs : List Nat} {cost switches : Nat} (execution : Controller.Execution worker budget restore (Controller.initial worker budget) xs final cost switches) : ∀ span ∈ (spans worker budget restore (Controller.initial worker budget) 0 xs).dropLast, Worker.ClosedRound worker span.side (worker.restart span.side) budget ((xs.drop span.start).take (span.stop - span.start)) := RotationBST.Combo.Rounds.Certificates.closed_spans executiontheorem span_lower_bound {σ : Type} {worker : Controller.Worker σ} {budget restore : Nat} {final : Controller.State σ} {xs : List Nat} {cost switches : Nat} (execution : Controller.Execution worker budget restore (Controller.initial worker budget) xs final cost switches) (span : Span) (member : span ∈ (spans worker budget restore (Controller.initial worker budget) 0 xs).dropLast) (endState : σ) (work : Nat) (complete : Worker.Requests worker span.side (worker.restart span.side) ((xs.drop span.start).take (span.stop - span.start)) endState work) : budget < work := RotationBST.Combo.Rounds.Certificates.span_lower_bound execution span member endState work completetheorem interior_spans {σ : Type} {worker : Controller.Worker σ} {budget restore : Nat} {s t : Controller.State σ} {xs : List Nat} {cost switches : Nat} (execution : Controller.Execution worker budget restore s xs t cost switches) : ∀ span ∈ (spans worker budget restore s 0 xs).dropLast.tail, Worker.ClosedRound worker span.side (worker.restart span.side) budget ((xs.drop span.start).take (span.stop - span.start)) := RotationBST.Combo.Rounds.Certificates.interior_spans executionFor an execution from Controller.initial, every span except the last is a closed round, from the restart state with the full budget, on its slice of the input (closed_spans). span_lower_bound is the paper’s inequality, in strict form: if the worker of such a span serves all of its requests from its restart state at total work , then . The proof is ClosedRound.lower_bound. The exhausted last attempt shows that the full run needs more than the fuel the round had. For an execution from an arbitrary state, the first round may have begun before the prefix, with less than a full budget. interior_spans then certifies every span except the first and the last.
29.3.7 How the pieces give the bound
The analysis in Section 29.1 (Results.prefix_bound and its relatives) puts these facts together, with budget and restore cost . Fix either side , for example the one with the smaller total charge, and let be its number of rounds.
- Each closed round is charged at least . Let be the requests of a closed round of . By well-behavedness (
Results.segment_bound), running from the restart state on the standalone piece costs at most . Byspan_lower_boundthis cost exceeds . Hence : this isResults.closed_span_charge. - Closed rounds are few. Summing over the at least closed rounds of , and using
charge_le_interval, gives , where is the total charge of on the prefix (Results.closed_count_bound). - Work is proportional to rounds. By
work_by_rounds,
Since is arbitrary, the cost is bounded by the minimum over both sides. This is Results.minimum_bound. For a window of an ongoing run, Execution.window isolates the middle segment and interior_spans certifies all of its rounds except the first and the last. Two uncertified rounds instead of one is why the window bounds of Section 29.1 have where the prefix bounds have .
Everything here is about an abstract worker, whose steps cost one unit each. Restoring the tree is the number restore, charged once per switch. The fact that a worker step is a legal pointer operation on a real tree, and that restoring the initial tree costs , are separate obligations, discharged by the refinement and snapshot machinery of Section 29.4 and Section 39.2. The hypothesis Controller.FreshBound is an assumption on the worker, and it is derived from well-behavedness, never proved for arbitrary algorithms. The switch counts, spans and records exist only in proofs. The controller itself stores a side, a worker state and a fuel counter.