Binary Search Trees in Lean Check a result Declarations Bibliography Lean source

Chapter 29 · Combining search trees · Section 29.3Alternating rounds

Combo-BST serves one request sequence with two BST algorithms A0A_0 and A1A_1, 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 TT, for a fixed number of steps C3 f(n)C_3\,f(n). When the budget runs out in the middle of a request xix_i, the tree is restored to TT, the other algorithm becomes active, and the new round starts again at xix_i. 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 C3 f(n)C_3\,f(n). 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.
Do you need this section?

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 2k+12k+1 and 3f(n)3f(n) 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 B=5B = 5. Worker A0A_0 completes x0x_0 and x1x_1 at cost 22 each, then runs out of budget during x2x_2. The tree is restored, and A1A_1 starts from scratch at x2x_2.

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 22 belongs to round 1, whose attempt at x2x_2 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 A0A_0 from the initial tree on x5,x6,…x_5, x_6, \dots. 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 f(n)f(n) is known in advance, so every round of the paper has the same budget C3 f(n)C_3\,f(n), 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 =(C1+1) F= (C_1 + 1)\,F, that is, C3=C1+1C_3 = C_1 + 1, where F≥nF \ge n is the overhead and C1C_1 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 O(n)O(n) for LoadState. The analysis takes restore to be a multiple of FF, 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 A0A_0 and true for A1A_1.

structureCombo.Controller.Workerno axiomsAlgorithms/Combination/Controller/Definitions/Abstract.lean:8
/-- 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 → σ × Bool

restart side is the state of algorithm side immediately after the tree has been restored to the initial tree TT. 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:

structureCombo.Controller.Attemptno axiomsAlgorithms/Combination/Controller/Definitions/Abstract.lean:16
structure Attempt (σ : Type) where  state : σ  left : Nat  done : Bool  deriving DecidableEq
defCombo.Controller.attemptno axiomsAlgorithms/Combination/Controller/Definitions/Abstract.lean:22
/-- 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.1

attempt 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:

inductiveCombo.Controller.AttemptRunno axiomsAlgorithms/Combination/Controller/Definitions/Abstract.lean:58
/-- 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 00), one step that completes (done, work 11), 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:

theoremCombo.Controller.Results.attempt_runpropext · Classical.choice · Quot.soundAlgorithms/Combination/Controller/Abstract.lean:9
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 s
theoremCombo.Controller.Results.AttemptRun.failedpropext · Quot.soundAlgorithms/Combination/Controller/Abstract.lean:14
theorem 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 failed
theoremCombo.Controller.Results.attempt_extrapropextAlgorithms/Combination/Controller/Abstract.lean:19
theorem 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 done

attempt_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 C3 f(n)C_3\,f(n) 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:

initial worker budget starts with A0A_0 (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.

defCombo.Controller.serveno axiomsAlgorithms/Combination/Controller/Definitions/Abstract.lean:46
/-- 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:

  1. Attempt x with the active worker, from its current state, with the round’s remaining fuel s.left. If it completes, stay in the same round, with the state and fuel the attempt left. The work is the fuel consumed.
  2. Otherwise switch. Flip the side, start the other worker in its restart state, and attempt x with a fresh, full budget. The work is the fuel the first attempt consumed (all of s.left, by AttemptRun.failed), plus restore, 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:

inductiveCombo.Controller.Serviceno axiomsAlgorithms/Combination/Controller/Definitions/Abstract.lean:69
/-- 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:

theoremCombo.Controller.Results.serve_servicepropext · Classical.choice · Quot.soundAlgorithms/Combination/Controller/Abstract.lean:25
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 x
theoremCombo.Controller.Results.serve_leftpropext · Classical.choice · Quot.soundAlgorithms/Combination/Controller/Abstract.lean:34
theorem 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 limit
theoremCombo.Controller.Results.serve_alternatespropextAlgorithms/Combination/Controller/Abstract.lean:38
theorem 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 x

serve_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 C3C_3 “suitably large” so that every round completes at least one access. The library states this as a hypothesis on the worker:

defCombo.Controller.FreshBoundno axiomsAlgorithms/Combination/Controller/Definitions/Abstract.lean:82
/-- 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 = true

FreshBound worker n bound says that, from its restart state, either algorithm completes any single valid request x<nx < n 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 =C1F= C_1 F, from Analysis.WellBehaved. The budget (C1+1)F(C_1+1)F is strictly larger.

theoremCombo.Controller.Results.serve_donepropextAlgorithms/Combination/Controller/Abstract.lean:29
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 valid
theoremCombo.Controller.Results.fresh_completespropext · Quot.soundAlgorithms/Combination/Controller/Abstract.lean:43
theorem 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 valid

If 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.

defCombo.Controller.runno axiomsAlgorithms/Combination/Controller/Definitions/Abstract.lean:87
/-- 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
inductiveCombo.Controller.Executionno axiomsAlgorithms/Combination/Controller/Definitions/Abstract.lean:96
/-- 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:

theoremCombo.Controller.Results.completepropext · Classical.choice · Quot.soundAlgorithms/Combination/Controller/Abstract.lean:49
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 valid

From 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 (2⋅budget+restore)(2\cdot\mathit{budget} + \mathit{restore}) per request. The sharper bound, relative to the cheaper algorithm, needs the rounds below.

The central accounting identity is exact:

theoremCombo.Controller.Results.Execution.accountpropext · Classical.choice · Quot.soundAlgorithms/Combination/Controller/Abstract.lean:57
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 execution
cost+t.left  =  s.left+switches⋅(budget+restore).\mathit{cost} + t.\mathit{left} \;=\; s.\mathit{left} + \mathit{switches}\cdot(\mathit{budget} + \mathit{restore}).

The 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), O(k f(n)+k n)O(k\,f(n) + k\,n) for 2k+12k+1 rounds, follows from it by counting switches.

theoremCombo.Controller.Results.Execution.switchespropext · Quot.soundAlgorithms/Combination/Controller/Abstract.lean:63
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 execution
theoremCombo.Controller.Results.Execution.left_lepropext · Classical.choice · Quot.soundAlgorithms/Combination/Controller/Abstract.lean:74
theorem 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 limit
theoremCombo.Controller.Results.run_appendpropext · Quot.soundAlgorithms/Combination/Controller/Abstract.lean:68
theorem 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 ys
theoremCombo.Controller.Results.Execution.splitpropext · Quot.soundAlgorithms/Combination/Controller/Abstract.lean:79
theorem 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 execution
theoremCombo.Controller.Results.Execution.windowpropext · Quot.soundAlgorithms/Combination/Controller/Abstract.lean:88
theorem 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 ordered

These 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 lo≤hilo \le hi 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 lolo, 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.

structureCombo.Rounds.Spanno axiomsAlgorithms/Combination/Rounds/Definitions/Abstract.lean:10
/-- 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 DecidableEq
defCombo.Rounds.Span.Containsno axiomsAlgorithms/Combination/Rounds/Definitions/Abstract.lean:18
def Span.Contains (span : Span) (index : Nat) : Prop := span.start ≤ index ∧ index < span.stop

A 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 bb stops at b+1b + 1, so that it includes the interrupted request, and the next span starts at bb. spans applies both to an input prefix whose first position is offset.

defCombo.Rounds.cutsno axiomsAlgorithms/Combination/Rounds/Definitions/Abstract.lean:20
/-- 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 []
defCombo.Rounds.partitionno axiomsAlgorithms/Combination/Rounds/Definitions/Abstract.lean:32
/-- 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
defCombo.Rounds.spansno axiomsAlgorithms/Combination/Rounds/Definitions/Abstract.lean:39
/-- 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.

defCombo.Rounds.Separatedno axiomsAlgorithms/Combination/Rounds/Definitions/Abstract.lean:45
/-- 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.start
defCombo.Rounds.countno axiomsAlgorithms/Combination/Rounds/Definitions/Abstract.lean:49
def count (side : Bool) (rounds : List Span) : Nat := (rounds.filter fun span => span.side == side).length

The structural facts about rounds:

theoremCombo.Rounds.Results.cuts_orderedpropext · Classical.choice · Quot.soundAlgorithms/Combination/Rounds/Abstract.lean:9
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 xs
theoremCombo.Rounds.Results.cuts_lengthpropextAlgorithms/Combination/Rounds/Abstract.lean:15
theorem 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 offset
theoremCombo.Rounds.Results.spans_boundspropext · Classical.choice · Quot.soundAlgorithms/Combination/Rounds/Abstract.lean:21
theorem 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 xs
theoremCombo.Rounds.Results.spans_separatedpropext · Classical.choice · Quot.soundAlgorithms/Combination/Rounds/Abstract.lean:27
theorem 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 xs
theoremCombo.Rounds.Results.spans_lengthpropextAlgorithms/Combination/Rounds/Abstract.lean:32
theorem 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 offset
theoremCombo.Rounds.Results.rounds_countpropext · Classical.choice · Quot.soundAlgorithms/Combination/Rounds/Abstract.lean:38
theorem 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 selected

The 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 switches+1\mathit{switches} + 1 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 2k+12k+1 rounds, kk of them performed by Amin⁡A_{\min}”: for either side, the total number of rounds is at most 2⋅count+12\cdot\mathit{count} + 1, 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:

defCombo.Rounds.Span.chargepropext · Classical.choice · Quot.soundAlgorithms/Combination/Rounds/Definitions/Abstract.lean:51
/-- 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 i
defCombo.Rounds.chargepropext · Classical.choice · Quot.soundAlgorithms/Combination/Rounds/Definitions/Abstract.lean:55
def charge (selected : Bool) (weights : Nat → ℝ) (rounds : List Span) : ℝ :=  ((rounds.filter fun span => span.side == selected).map (Span.charge weights)).sum

In the analysis the weights are the amortized charges TX′(Aμ,T,X)T_{X'}(A_\mu, T, X) of the paper, read at their positions in the original sequence XX. 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:

theoremCombo.Rounds.Results.work_by_roundspropext · Classical.choice · Quot.soundAlgorithms/Combination/Rounds/Abstract.lean:44
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 selected
theoremCombo.Rounds.Results.charge_le_intervalpropext · Classical.choice · Quot.soundAlgorithms/Combination/Rounds/Abstract.lean:51
theorem 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 nonneg

work_by_rounds bounds the work of an execution that starts within the budget by the number of rounds of either side: cost≤(2⋅count+1)(budget+restore)\mathit{cost} \le (2\cdot\mathit{count} + 1)(\mathit{budget} + \mathit{restore}). 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 Xj′X'_j are disjoint subsets of X′′X''”. 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 kk. To bound kk, the paper shows that each closed round of Amin⁡A_{\min} is expensive: “T(Amin⁡,T,Xj′)≥C3 f(n)T(A_{\min}, T, X'_j) \ge C_3\,f(n) for all j<kj < k”. 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.

structureCombo.Rounds.Certificates.Recordno axiomsAlgorithms/Combination/Rounds/Certificates/Definitions/Abstract.lean:9
/-- 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
defCombo.Rounds.Certificates.Activeno axiomsAlgorithms/Combination/Rounds/Certificates/Definitions/Abstract.lean:16
/-- 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
defCombo.Rounds.Certificates.recordsno axiomsAlgorithms/Combination/Rounds/Certificates/Definitions/Abstract.lean:23
/-- 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.

theoremCombo.Rounds.Certificates.Results.initial_activepropextAlgorithms/Combination/Rounds/Certificates/Abstract.lean:17
theorem initial_active {σ : Type} (worker : Controller.Worker σ) (budget : Nat) :    Active worker budget (Controller.initial worker budget) [] :=  RotationBST.Combo.Rounds.Certificates.initial_active worker budget
theoremCombo.Rounds.Certificates.Results.records_closedpropext · Classical.choice · Quot.soundAlgorithms/Combination/Rounds/Certificates/Abstract.lean:9
theorem 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 active
theoremCombo.Rounds.Certificates.Results.records_spanspropextAlgorithms/Combination/Rounds/Certificates/Abstract.lean:21
theorem 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 xs
theoremCombo.Rounds.Certificates.Results.records_boundspropext · Classical.choice · Quot.soundAlgorithms/Combination/Rounds/Certificates/Abstract.lean:27
theorem 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 before
theoremCombo.Rounds.Certificates.Results.records_slicepropext · Classical.choice · Quot.soundAlgorithms/Combination/Rounds/Certificates/Abstract.lean:33
theorem 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 aligned
theoremCombo.Rounds.Certificates.Results.records_tail_closedpropext · Classical.choice · Quot.soundAlgorithms/Combination/Rounds/Certificates/Abstract.lean:56
theorem 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 earlier

The 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:

theoremCombo.Rounds.Certificates.Results.closed_spanspropext · Classical.choice · Quot.soundAlgorithms/Combination/Rounds/Certificates/Abstract.lean:39
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 execution
theoremCombo.Rounds.Certificates.Results.span_lower_boundpropext · Classical.choice · Quot.soundAlgorithms/Combination/Rounds/Certificates/Abstract.lean:47
theorem 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 complete
theoremCombo.Rounds.Certificates.Results.interior_spanspropext · Classical.choice · Quot.soundAlgorithms/Combination/Rounds/Certificates/Abstract.lean:64
theorem 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 execution

For 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 ww, then budget<w\mathit{budget} < w. 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 B=(C1+1)FB = (C_1 + 1)F and restore cost rFrF. Fix either side μ\mu, for example the one with the smaller total charge, and let kk be its number of rounds.

  1. Each closed round is charged at least FF. Let Xj′X'_j be the requests of a closed round of μ\mu. By well-behavedness (Results.segment_bound), running μ\mu from the restart state on the standalone piece Xj′X'_j costs at most C0 TXj′(Aμ,T,X)+C1FC_0\,T_{X'_j}(A_\mu, T, X) + C_1 F. By span_lower_bound this cost exceeds (C1+1)F(C_1 + 1)F. Hence F≤C0 TXj′(Aμ,T,X)F \le C_0\,T_{X'_j}(A_\mu, T, X): this is Results.closed_span_charge.
  2. Closed rounds are few. Summing over the at least k−1k - 1 closed rounds of μ\mu, and using charge_le_interval, gives (k−1) F≤C0 T(Aμ)(k-1)\,F \le C_0\,T(A_\mu), where T(Aμ)T(A_\mu) is the total charge of μ\mu on the prefix (Results.closed_count_bound).
  3. Work is proportional to rounds. By work_by_rounds,
cost  ≤  (2k+1) (C1+1+r) F  ≤  (C1+1+r)(2 C0 T(Aμ)+3F).\mathit{cost} \;\le\; (2k+1)\,(C_1 + 1 + r)\,F \;\le\; (C_1 + 1 + r)\bigl(2\,C_0\,T(A_\mu) + 3F\bigr).

Since μ\mu 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 5F5F where the prefix bounds have 3F3F.

What this layer does not provide

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 O(n)O(n), 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.