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

Chapter 30Deamortization

Most of the good BST bounds are amortized. Splaying costs O(log⁡n)O(\log n) per access on average over a sequence, but a single access can cost Θ(n)\Theta(n). Balanced trees guarantee O(log⁡n)O(\log n) per access, but none of the adaptive bounds. Bose, Collette, Fagerberg and Langerman (2012a) showed that this trade-off is not necessary. Any BST algorithm can be transformed into one that has the same total cost on every sequence, up to a constant factor, and in addition serves every single access with O(log⁡n)O(\log n) operations. Applied to splay trees, this gives one BST with all of splaying’s amortized bounds and logarithmic worst-case accesses.

The library formalizes the abstract BST-operation part of the paper: the counterpart of its Theorem 6 for every online rotation-model algorithm, and its application to splaying. Like the paper’s Theorem 6, it counts BST operations only; the private computation and storage that choose them are not bounded (Section 30.11).

Key result · Bose et al. 2012a
Every online BST algorithm can be deamortized

This is the abstract BST-operation counterpart of Theorem 6 of the paper. Let AA be any online BST algorithm in the rotation model, and TT any nonempty initial tree on nn keys. Then there is a single deamortized program, fixed from AA and TT before any request is known, made of two parts.

  1. Setup. A pointer-machine trace of at most 16n16n instructions turns TT, cursor at the root, into a tree T′′T'', cursor at the root.
  2. Responses. For every sequence X=x1,…,xmX = x_1, \dots, x_m of keys in {0,…,n−1}\{0, \dots, n-1\}, the program emits one stream of pointer instructions for a single persistent finger, starting at the root of T′′T''. The stream is cut into mm consecutive segments, one per request. During segment jj the finger stands on xjx_j at least once, and response jj costs cj=1+(length of segment j)c_j = 1 + (\text{length of segment } j). Every response is cheap, and the total is within a constant factor of AA:
cj  <  251+195log⁡2nfor every j,∑j=1mcj  ≤  79360 (m+RA(X)),c_j \;<\; 251 + 195\log_2 n \quad\text{for every } j, \qquad \sum_{j=1}^m c_j \;\le\; 79360\,\bigl(m + R_A(X)\bigr),

where RA(X)R_A(X) is the number of rotations AA performs on XX from TT. With the setup included, the total is at most 16n+79360 (m+RA(X))16n + 79360\,(m + R_A(X)), and any upper bound BB on m+RA(X)m + R_A(X) gives the total bound 16n+79360 B16n + 79360\,B.

The program is online: if XX is a prefix of X′X', then the instruction stream and the list of response costs for XX are prefixes of those for X′X'.

In Lean, AA is an Algorithm with cost Algorithm.totalCost, and the program is a Algorithm.Implementation. implementation_exists constructs one; its fields carry the setup, the correctness and cost guarantees, and causality. total_bound adds the setup, and transfer replaces m+RA(X)m + R_A(X) by any real upper bound.

The Lean statements
theoremDeamortization.Algorithm.Results.implementation_existspropext · Classical.choice · Quot.soundAlgorithms/Deamortization/Algorithm/Abstract.lean:13
theorem implementation_exists (source : RotationBST.Algorithm) (saved : Tree) (nonempty : saved ≠ .nil) :    Nonempty (Implementation source saved) := …  RotationBST.Deamortization.Algorithm.implementation_exists source saved nonempty
theoremDeamortization.Algorithm.Results.total_boundpropext · Classical.choice · Quot.soundAlgorithms/Deamortization/Algorithm/Abstract.lean:17
theorem total_bound {source : RotationBST.Algorithm} {saved : Tree}    (d : Implementation source saved) {keys : List Nat} (valid : ∀ x ∈ keys, x < saved.size)    {out : Scheduler.Output} (completed : d.program keys = some out) :    d.startup + out.costs.sum ≤ 16 * saved.size + 79360 * source.totalCost saved keys := …  RotationBST.Deamortization.Algorithm.Implementation.total_bound d valid completed
theoremDeamortization.Algorithm.Results.transferpropext · Classical.choice · Quot.soundAlgorithms/Deamortization/Algorithm/Abstract.lean:23
theorem transfer {source : RotationBST.Algorithm} {saved : Tree}    (d : Implementation source saved) {keys : List Nat} (valid : ∀ x ∈ keys, x < saved.size)    {out : Scheduler.Output} (completed : d.program keys = some out) {bound : ℝ}    (sourceBound : (source.totalCost saved keys : ℝ) ≤ bound) :    (d.startup + out.costs.sum : ℝ) ≤ 16 * saved.size + 79360 * bound := …  RotationBST.Deamortization.Algorithm.Implementation.transfer d valid completed sourceBound
30 definitions in 8 chapters · reading list
Key result · Bose et al. 2012a
A deamortized splay tree keeps splaying’s bounds

For every nonempty initial tree on nn keys there is a deamortized splay program as in the box above, with splaying as the source algorithm AA. Every response costs less than 251+195log⁡2n251 + 195\log_2 n. For every such program and every sequence XX of mm keys in {0,…,n−1}\{0, \dots, n-1\}, let T(X)T(X) be the setup cost plus the sum of the response costs. Then all of the following hold, for the same program and the same run:

  • Scanning. For X=0,1,…,n−1X = 0, 1, \dots, n-1: T(X)≤1111056 nT(X) \le 1111056\,n.
  • Balance. T(X)≤16n+79360 (m(2+3log⁡2n)+nlog⁡2n)T(X) \le 16n + 79360\,\bigl(m(2 + 3\log_2 n) + n\log_2 n\bigr).
  • Working set. T(X)≤16n+79360 (11∑j(1+⌊log⁡2wj⌋)+6n(1+⌊log⁡2n⌋))T(X) \le 16n + 79360\,\bigl(11\sum_j (1 + \lfloor\log_2 w_j\rfloor) + 6n(1 + \lfloor\log_2 n\rfloor)\bigr), where wjw_j is the working-set number of request jj.
  • Static optimality, if XX requests every key: T(X)≤16n+952320∑j(1+depthu(xj))T(X) \le 16n + 952320\sum_j \bigl(1 + \mathrm{depth}_u(x_j)\bigr) for every BST uu on the nn keys.
  • Static finger, if XX requests every key: T(X)≤16n+952320∑j(1+log⁡2(∣xj−f∣+1))T(X) \le 16n + 952320\sum_j \bigl(1 + \log_2(|x_j - f| + 1)\bigr) for every natural number ff.
  • Without the coverage hypothesis, there are constants CC and DD, the same for all nn, all programs and all sequences, such that T(X)≤16n+C(m+∑iqilog⁡2(m/qi))+D n(1+⌊log⁡2n⌋)T(X) \le 16n + C\bigl(m + \sum_i q_i\log_2(m/q_i)\bigr) + D\,n(1 + \lfloor\log_2 n\rfloor), where key ii is requested qiq_i times. Likewise, with constants of its own, T(X)≤16n+C∑j(1+log⁡2(∣xj−f∣+1))+D n(1+⌊log⁡2n⌋)T(X) \le 16n + C\sum_j \bigl(1 + \log_2(|x_j - f| + 1)\bigr) + D\,n(1 + \lfloor\log_2 n\rfloor) for every finger f<nf < n.

In Lean, splaying is Splay.algorithm, T(X)T(X) is d.startup + out.costs.sum, and the bounds are workingSetSum, StaticTree.cost, Bounds.staticCost and Bounds.fingerCost of Chapter 14.

The Lean statements
theoremSplay.Deamortization.Results.implementation_existspropext · Classical.choice · Quot.soundAlgorithms/Splay/Deamortization/Abstract.lean:14
theorem implementation_exists (saved : Tree) (nonempty : saved ≠ .nil) :    Nonempty (Algorithm.Implementation Splay.algorithm saved) := …  RotationBST.Splay.Deamortization.implementation_exists saved nonempty
theoremSplay.Deamortization.Results.scanningpropext · Classical.choice · Quot.soundAlgorithms/Splay/Deamortization/Abstract.lean:18
theorem scanning {saved : Tree} (d : Algorithm.Implementation Splay.algorithm saved)    {out : Scheduler.Output} (completed : d.program (List.range saved.size) = some out) :    d.startup + out.costs.sum ≤ 1111056 * saved.size := …  RotationBST.Splay.Deamortization.scanning d completed
theoremSplay.Deamortization.Results.balancepropext · Classical.choice · Quot.soundAlgorithms/Splay/Deamortization/Abstract.lean:23
theorem balance {saved : Tree} (d : Algorithm.Implementation Splay.algorithm saved)    {xs : List Nat} (valid : ∀ x ∈ xs, x < saved.size)    {out : Scheduler.Output} (completed : d.program xs = some out) :    (d.startup + out.costs.sum : ℝ) ≤ 16 * saved.size + 79360 *      (xs.length * (2 + 3 * Real.logb 2 saved.size) + saved.size * Real.logb 2 saved.size) := …  RotationBST.Splay.Deamortization.balance d valid completed
theoremSplay.Deamortization.Results.working_setpropext · Classical.choice · Quot.soundAlgorithms/Splay/Deamortization/Abstract.lean:30
theorem working_set {saved : Tree} (d : Algorithm.Implementation Splay.algorithm saved)    {xs : List Nat} (valid : ∀ x ∈ xs, x < saved.size)    {out : Scheduler.Output} (completed : d.program xs = some out) :    d.startup + out.costs.sum ≤ 16 * saved.size + 79360 *      (11 * workingSetSum saved.size [] xs + 6 * saved.size * (1 + saved.size.log2)) := …  RotationBST.Splay.Deamortization.working_set d valid completed
theoremSplay.Deamortization.Results.static_optimalitypropext · Classical.choice · Quot.soundAlgorithms/Splay/Deamortization/Abstract.lean:37
theorem static_optimality {saved : Tree} (d : Algorithm.Implementation Splay.algorithm saved)    (reference : Tree) (same : saved.size = reference.size) {xs : List Nat}    (valid : ∀ x ∈ xs, x < saved.size) (covers : ∀ x < saved.size, x ∈ xs)    {out : Scheduler.Output} (completed : d.program xs = some out) :    (d.startup + out.costs.sum : ℝ) ≤ 16 * saved.size + 952320 * (StaticTree.cost reference xs : ℝ) := …  RotationBST.Splay.Deamortization.static_optimality d reference same valid covers completed
theoremSplay.Deamortization.Results.static_fingerpropext · Classical.choice · Quot.soundAlgorithms/Splay/Deamortization/Abstract.lean:44
theorem static_finger {saved : Tree} (d : Algorithm.Implementation Splay.algorithm saved)    (finger : Nat) {xs : List Nat} (valid : ∀ x ∈ xs, x < saved.size)    (covers : ∀ x < saved.size, x ∈ xs) {out : Scheduler.Output} (completed : d.program xs = some out) :    (d.startup + out.costs.sum : ℝ) ≤ 16 * saved.size + 952320 * Bounds.fingerCost finger xs := …  RotationBST.Splay.Deamortization.static_finger d finger valid covers completed
theoremSplay.Deamortization.Results.static_optimality_generalpropext · Classical.choice · Quot.soundAlgorithms/Splay/Deamortization/Abstract.lean:50
theorem static_optimality_general :    ∃ C D : Nat, ∀ saved : Tree, ∀ d : Algorithm.Implementation Splay.algorithm saved,      ∀ xs : List Nat, (∀ x ∈ xs, x < saved.size) → ∀ out,        d.program xs = some out →        (d.startup + out.costs.sum : ℝ) ≤ 16 * saved.size +          C * Bounds.staticCost saved.size xs + D * saved.size * (1 + (saved.size.log2 : ℝ)) := …  RotationBST.Splay.Deamortization.static_optimality_general
theoremSplay.Deamortization.Results.static_finger_generalpropext · Classical.choice · Quot.soundAlgorithms/Splay/Deamortization/Abstract.lean:58
theorem static_finger_general :    ∃ C D : Nat, ∀ saved : Tree, ∀ d : Algorithm.Implementation Splay.algorithm saved,      ∀ (xs : List Nat) (finger : Nat), finger < saved.size →        (∀ x ∈ xs, x < saved.size) → ∀ out, d.program xs = some out →        (d.startup + out.costs.sum : ℝ) ≤ 16 * saved.size +          C * Bounds.fingerCost finger xs + D * saved.size * (1 + (saved.size.log2 : ℝ)) := …  RotationBST.Splay.Deamortization.static_finger_general
40 definitions in 10 chapters · reading list

The two boxes are all a reader needs to confirm the results. Their statements use the rotation model, the online algorithms of Chapter 5, the pointer instructions of Chapter 7 and Chapter 10, the replay function of Chapter 36, and the three definitions of Section 30.2. None of the machinery that builds the program appears in them.

The rest of the chapter has two layers. Section 30.1 is a bird’s-eye view of the construction, with its constants. Section 30.2 to Section 30.4 state the transformation theorem and its splay consequences again, in English and in Lean, with what each hypothesis means. Section 30.5 to Section 30.10 open the construction: the bounded-depth simulation, the scheduler, and how they are composed. The structures they rely on are built in Part V. A reader who trusts the construction can skip those sections; a reader who wants to see where each constant comes from should read them. Section 30.11 lists what is not formalized. The definitions are in Algorithms/Deamortization/{Scheduler,Finger,Algorithm}/Definitions/Abstract.lean, the theorems in the Abstract.lean files of Algorithms/Deamortization/{Weighted,Window,Scheduler,Finger,Replay,Algorithm} and Algorithms/Splay/Deamortization.

30.1 The plan

The paper’s idea fits in two sentences. First, simulate the algorithm AA in a tree whose depth stays O(log⁡n)O(\log n) at all times, at a constant factor in total cost (the paper’s Theorem 5). Second, run that simulation, and whenever it has spent clog⁡nc\log n operations without reaching the next requested key, walk to that key and back, which costs another O(log⁡n)O(\log n) because the tree is shallow (Theorem 6). Every request is then served within O(log⁡n)O(\log n) operations, and the detours at most triple the total.

The second step needs a shallow tree at every intermediate moment, not just between requests, because a detour can interrupt the simulation anywhere. This is what the first step provides, and it is where most of the work is. The library’s chain has five links:

weighted stacks        pop-tarts: BSTs that implement a stack whose top is shallow
      │                (Chapter 37)
      ▼
heavy paths            the source tree, stored as pairs of pop-tarts along heavy paths,
      │                rebuilt locally after each source instruction (Sections 38.1–38.3)
      ▼
bounded-depth          every source instruction becomes O(1) amortized physical
simulation             instructions, and every intermediate tree has depth
      │                ≤ 49 + 39 log₂ n (Section 38.4; Theorem 5)
      ▼
scheduled responses    each request consumes at most a budget of pending simulation
      │                work, then detours to the key; the simulation may lag behind
      ▼                (this chapter; Theorem 6)
applications           any rotation-model algorithm, splaying, and the combined
                       finger bound of Chapter 31

Weighted stacks. A pop-tart (Section 37.1) is a BST that implements a stack of nodes, pushed in decreasing key order at the root, with O(1)O(1) amortized BST operations per push and pop. Its leaves may carry weights. The chocolate pop-tart of the paper’s Section 2 (Section 37.2) keeps every leaf of weight ww at depth O(log⁡(W/w))O(\log(W/w)), where WW is the total leaf weight, whatever the weights of the pushed leaves.

Heavy paths. The simulation keeps the source algorithm’s tree, but stores it differently (Section 38.1 and Section 38.2). Each heavy path is split at its bottom node into its nodes smaller and larger than that node. These two monotone sequences are stored as two pop-tarts below the bottom node, and the subtrees hanging off the path are leaves of the pop-tarts, represented recursively. Depths telescope along the dotted edges to O(log⁡(W/w))O(\log(W/w)). The source’s finger is represented the same way, by the path from the root to the finger turned upside down (Section 38.3), so the simulated finger is always at the physical root.

Bounded-depth simulation. Section 38.4 packages this into a policy: a function that receives one source instruction (a move or a rotation of the source’s finger) and emits a physical instruction stream that updates the representation. With unit weights it costs at most 6262 amortized physical instructions per source instruction, and at most 34+24log⁡2n34 + 24\log_2 n in the worst case. Every intermediate physical tree, at every instruction boundary, has all depths at most 49+39log⁡2n49 + 39\log_2 n (Results.response_uniform). Measured from the cheapest representation of the starting tree, the factor is 248248.

Scheduled responses. The scheduler of this chapter fixes an integer budget bb with 49+39log⁡2n≤b<50+39log⁡2n49 + 39\log_2 n \le b < 50 + 39\log_2 n. For each request it executes at most bb instructions of the pending simulated work. If the simulation reaches the requested key within these, the response ends there. Otherwise the finger walks to the key and back, which is cheap because the tree it walks in has depth at most bb. The library bounds each response by five times the work it consumed, so it emits at most 5b5b instructions and costs at most 1+5b<251+195log⁡2n1 + 5b < 251 + 195\log_2 n. The work that was not executed stays pending, so the simulation lags behind the requests. Each emitted instruction is charged to executed simulation work at most five times over.

Applications. A rotation-model algorithm is first turned into a pointer program whose recorded work is at most 6464 times its total cost (the replay planners of Section 29.2, built on the lazy online simulation of Chapter 7). The constants then multiply:

Step Constant
Building the initial representation setup ≤16n\le 16n
Rotation algorithm → pointer program 64×64 \times total cost
Pointer program → bounded-depth simulation 248×248 \times instructions
Simulation → scheduled responses 5×5 \times executed work
Altogether 64⋅248⋅5=7936064 \cdot 248 \cdot 5 = 79360
Per response 1+5b<251+195log⁡2n1 + 5b < 251 + 195\log_2 n

These are explicit witnesses, not optimized constants. The splay applications multiply further by splaying’s own constants: 952320=12⋅79360952320 = 12 \cdot 79360 and 1111056=16+14⋅793601111056 = 16 + 14 \cdot 79360.

30.2 Physical programs and their responses

The deamortized program works in the BST model of the paper: one finger that persists between requests, moves along edges and rotates at the node it stands on, each instruction at unit cost. This is the one-finger case of the model of Chapter 10. Unlike the root-restarting pointer machine of Chapter 7, returning to the root is not free. Instructions are Multifinger.Opcodes, executed on a single cursor by Multifinger.replay (Chapter 36), which returns none at the first illegal instruction.

A program’s output for a request sequence is one instruction stream. To speak of the cost of each response, the library records how the stream is cut:

inductiveDeamortization.Scheduler.ServedStreampropextAlgorithms/Deamortization/Scheduler/Definitions/Abstract.lean:72
/-- Each charged request has its own contiguous physical instruction segment,with a recorded visit and the exact segment length. Adjacent segments shareone actual cursor; concatenation is the emitted program. -/inductive ServedStream : Pointer.Config → List Nat → Pointer.Config →    List Multifinger.Opcode → List Nat → Prop where  | nil {cursor : Pointer.Config} : ServedStream cursor [] cursor [] []  | cons {a visited b c : Pointer.Config} {key : Nat} {keys costs : List Nat}      {before after rest : List Multifinger.Opcode} :      Multifinger.replay before a = some visited → visited.key? = some key →      Multifinger.replay after visited = some b → ServedStream b keys c rest costs →      ServedStream a (key :: keys) c ((before ++ after) ++ rest)        ((1 + (before ++ after).length) :: costs)

ServedStream a keys c ops costs says that the stream ops leads from cursor a to cursor c and serves keys, cut into one segment before ++ after per key. Within its segment, the cursor reaches a configuration visited whose key is the request, after the before part. The segments are contiguous: each starts at the cursor where the previous one ended. The cost of a response is 1+∣before∣+∣after∣1 + |\mathit{before}| + |\mathit{after}|, the request unit plus every instruction, exactly as in Multifinger.Serves. So the cost list has one entry per request, and sums to the number of requests plus the length of the stream.

The relation fixes the segmentation, which is what makes a per-response bound meaningful: without it, a total cost could be split into small “responses” arbitrarily. A visit may occur at the very start of the segment, with before = [], if the cursor already stands on the key.

defDeamortization.Scheduler.cursorStateno axiomsAlgorithms/Deamortization/Scheduler/Definitions/Abstract.lean:98
/-- Existing persistent single-finger semantics, with the actual cursor key. -/def cursorState (c : Pointer.Config) : Multifinger.State 1 :=  ⟨c.tree, fun _ => c.key?.getD 0⟩

cursorState c reads a cursor as a one-finger state of Chapter 10: the tree of c, and one finger on the key under the cursor. The key is totalized to 00 on an empty focus, which never occurs in the theorems below, since their cursors stand on actual nodes. Through it, the output is also stated as an ordinary Multifinger.Execution.

An Output is what the scheduler returns for a whole request sequence: its final internal state (Section 30.6), the emitted instructions, the number of source instructions it consumed, and the list of response costs. Only instructions and costs matter for the main theorems; the state.cursor field is the physical cursor at the end.

The deamortized program itself is a structure:

structureDeamortization.Algorithm.Implementationno axiomsAlgorithms/Deamortization/Algorithm/Definitions/Abstract.lean:10
/-- One causal deamortized physical program for a rotation-model algorithm.The source and saved tree are fixed before requests and cost benchmarks.Setup is a separate linear trace; computation and storage are abstract. -/structure Implementation (source : RotationBST.Algorithm) (saved : Tree) where  initial : Tree  startup : Nat  program : List Nat → Option Scheduler.Output  setup : Pointer.Trace ⟨saved, []⟩ ⟨initial, []⟩ startup  setup_bound : startup ≤ 16 * saved.size  correct : ∀ keys, (∀ x ∈ keys, x < saved.size) →    ∃ out, program keys = some out ∧      Multifinger.replay out.instructions ⟨initial, []⟩ = some out.state.cursor ∧      Scheduler.ServedStream ⟨initial, []⟩ keys out.state.cursor out.instructions out.costs ∧      Multifinger.Execution (Scheduler.cursorState ⟨initial, []⟩) keys        (Scheduler.cursorState out.state.cursor) (keys.length + out.instructions.length) ∧      out.costs.length = keys.length ∧ out.costs.sum = keys.length + out.instructions.length ∧      out.costs.sum ≤ 79360 * source.totalCost saved keys ∧      ∀ cost ∈ out.costs, (cost : ℝ) < 251 + 195 * Real.logb 2 saved.size  causal : ∀ xs ys out, program (xs ++ ys) = some out →    ∃ first, program xs = some first ∧ first.instructions <+: out.instructions ∧      first.costs <+: out.costs

An Implementation source saved fixes the source algorithm and the initial tree saved as parameters. Everything else is chosen once, before any request sequence and before any cost bound that one might compare with:

  • initial and startup: the physical tree on which the responses begin, and the cost of reaching it. setup is a Pointer.Trace from saved to initial, both with the cursor at the root, of exactly startup instructions, and setup_bound says startup≤16n\mathtt{startup} \le 16n. The setup is not part of any response. It restructures the arbitrary initial tree into the shallow representation, as the paper also requires when TT is unbalanced.
  • program: a function from request sequences to outputs. Its value on a sequence is what the program does on that sequence.
  • correct: for every sequence keys of valid ranks, the program produces an output out such that
    1. its instructions replay legally from initial to the final cursor;
    2. they form a ServedStream of keys with the response costs out.costs;
    3. the same run is a one-finger Multifinger.Execution of keys from the root of initial, of cost keys.length + out.instructions.length;
    4. there is one cost per request, and the costs sum to keys.length + out.instructions.length;
    5. the sum is at most 79360 * source.totalCost saved keys, where Algorithm.totalCost counts one unit per request plus the source’s rotations, from the original tree saved; and
    6. every response cost is below 251+195log⁡2n251 + 195\log_2 n, with nn = saved.size and the real logarithm.
  • causal: if program (xs ++ ys) returns an output, then program xs returns one whose instructions and costs are prefixes of it.

causal is the paper’s definition of an online algorithm, “A(S)A(S) is a prefix of A(S′)A(S') whenever SS is a prefix of S′S'”. Because the program is a function of the whole sequence, causality is what makes it online. Prefix preservation of the instructions alone would allow the response to xjx_j to be split differently once later requests are known. The cost prefix fixes the segmentation as well: the first jj responses, as segments of the stream with their visits and costs, are the same for every continuation. Nothing done for x1,…,xjx_1, \dots, x_j depends on xj+1,…x_{j+1}, \dots. The comparison benchmarks of the next section are applied to this one program afterwards, so a single deamortized execution meets all of them at once.

Reading the Lean
  • Two trees. The source cost is measured from saved, the tree the source starts from. The responses start from initial, after the setup. The final physical tree is not required to be the source’s final tree: work that the scheduler has not yet executed when the sequence ends is never executed, and it is not needed.
  • Validity. correct says nothing about sequences with a key outside {0,…,n−1}\{0, \dots, n-1\}; the program may fail on them. Nonemptiness of saved is a hypothesis of the existence theorems, not a convention.
  • What is counted. The fields count BST instructions only. program is an arbitrary Lean function: the computation that chooses the instructions, and whatever it remembers, are free, as in the paper’s BST model. Section 30.11 comes back to this.

30.3 The transformation theorem

Theorem (deamortization). For every online rotation-model algorithm and every nonempty initial tree, a deamortized implementation exists.

All guarantees are in the fields of Algorithm.Implementation, so this one statement is the counterpart of Theorem 6. It is stated as Nonempty: the implementation is shown to exist, by choices made inside the proof (Section 30.10), and is not claimed to be computable.

The next two theorems apply to every implementation, in particular the one just constructed. They add the setup to the response costs.

Theorem (total cost). For every implementation and every valid sequence XX, the setup plus all response costs is at most 16n+79360 (m+RA(X))16n + 79360\,(m + R_A(X)).

Theorem (transfer of bounds). If the source’s total cost on XX is at most a real number BB, then the setup plus all response costs is at most 16n+79360 B16n + 79360\,B.

transfer is the tool for applications: any proved bound on the source, such as an entropy or working-set bound, becomes a bound on the deamortized program with the extra 16n16n. The splay theorems of the next section are proved this way.

The paper’s abstract also states a consequence for dynamic optimality: if a dynamically optimal BST algorithm exists, then one exists that answers every search in O(log⁡n)O(\log n) operations. The library proves this implication.

Theorem (dynamic optimality is preserved). Suppose an online rotation-model algorithm AA is dynamically optimal for total cost: there are constants such that, from every tree, its total cost is at most C(m+k)+DnC(m + k) + Dn for every offline rotation-model execution of the same requests with kk rotations. Then there are constants C′C' and D′D' such that for every nonempty tree TT there is a deamortized implementation of AA from TT whose setup plus response costs, on every sequence, is at most C′(m+k)+D′nC'(m + k) + D'n for every offline execution of the sequence from TT with kk rotations.

The hypothesis is TotallyDynamicallyOptimal of Chapter 5, and the comparator is any Execution, so kk ranges over the offline optimum and everything above it. The proof is transfer with C′=79360 CC' = 79360\,C and D′=16+79360 DD' = 16 + 79360\,D. The constants do not depend on TT; the implementation does.

Caveat

This theorem is conditional. It preserves dynamic optimality of an algorithm that is supplied with a proof of it. It asserts nothing about splaying, or about any other specific algorithm: no algorithm is known to satisfy the hypothesis.

30.4 Deamortized splay trees

Splaying is the paper’s first application, and its abstract’s first claim: a BST with the same asymptotic cost as splay trees on every sequence, and O(log⁡n)O(\log n) worst-case cost per search. With Splay.algorithm as the source, the transformation theorem gives the program.

Theorem (a deamortized splay program). For every nonempty initial tree there is a deamortized implementation of splaying.

Each of the following theorems holds for every implementation d of splaying and every completed run, so they hold simultaneously for one program on one run. They are the bounds of Section 24.2 and Section 24.3, passed through transfer, with the setup cost 16n16n included. In all of them, T(X)T(X) = d.startup + out.costs.sum.

Theorem (scanning). Requesting all keys in increasing order costs at most 1111056 n1111056\,n, setup included.

The sequence is List.range saved.size, so validity is automatic. The constant is 16+79360⋅1416 + 79360 \cdot 14, from splaying’s sequential-access bound of 14n14n (Results.scanning), which holds from every initial tree.

Theorem (balance). T(X)≤16n+79360 (m(2+3log⁡2n)+nlog⁡2n)T(X) \le 16n + 79360\,\bigl(m(2 + 3\log_2 n) + n\log_2 n\bigr).

Theorem (working set). T(X)≤16n+79360 (11 W(X)+6n(1+⌊log⁡2n⌋))T(X) \le 16n + 79360\,\bigl(11\,W(X) + 6n(1 + \lfloor\log_2 n\rfloor)\bigr), where W(X)=∑j(1+⌊log⁡2wj⌋)W(X) = \sum_j (1 + \lfloor\log_2 w_j\rfloor) is workingSetSum.

Splaying’s working-set theorem bounds rotations by 10 W(X)+6n(1+⌊log⁡2n⌋)10\,W(X) + 6n(1 + \lfloor\log_2 n\rfloor) (Results.workingSet). The eleventh copy of W(X)W(X) pays for the mm request units, since W(X)≥mW(X) \ge m. The startup term 6n(1+⌊log⁡2n⌋)6n(1 + \lfloor\log_2 n\rfloor) is part of the statement, multiplied like the rest.

The static bounds come in two versions, depending on whether the sequence requests every key. This coverage premise matters because unused keys still sit in the initial tree. Splaying’s static-optimality proof uses the weights qiq_i, and its potential drops by at most ∑ilog⁡2(m/qi)\sum_i \log_2(m/q_i) over the sequence. When every key is requested, every qi≥1q_i \ge 1, and this is at most ∑iqilog⁡2(m/qi)\sum_i q_i\log_2(m/q_i), a part of the bound itself (Section 24.2). A key that is never requested has no term to pay for it, and an additive startup term is needed.

Theorem (static optimality, every key requested). If XX requests every key, then for every BST uu on the same nn keys, T(X)≤16n+952320 ∑j(1+depthu(xj))T(X) \le 16n + 952320\,\sum_j (1 + \mathrm{depth}_u(x_j)).

The right side is StaticTree.cost reference xs, the cost of serving XX by root searches in the fixed tree uu. The constant is 12⋅7936012 \cdot 79360, from Results.staticOptimality_fixedTree. Apart from the 16n16n of the setup, there is no startup term.

Theorem (static finger, every key requested). If XX requests every key, then for every natural number ff, T(X)≤16n+952320 ∑j(1+log⁡2(∣xj−f∣+1))T(X) \le 16n + 952320\,\sum_j \bigl(1 + \log_2(|x_j - f| + 1)\bigr).

As in Results.staticFinger_all_accessed, the finger ff is not required to be a key.

Theorem (static optimality and static finger, general). There are constants CC and DD such that for every tree on nn keys, every implementation and every valid sequence, T(X)≤16n+C(m+∑iqilog⁡2(m/qi))+D n(1+⌊log⁡2n⌋)T(X) \le 16n + C\bigl(m + \sum_i q_i\log_2(m/q_i)\bigr) + D\,n(1 + \lfloor\log_2 n\rfloor). Likewise, with possibly other constants, T(X)≤16n+C∑j(1+log⁡2(∣xj−f∣+1))+D n(1+⌊log⁡2n⌋)T(X) \le 16n + C\sum_j \bigl(1 + \log_2(|x_j - f| + 1)\bigr) + D\,n(1 + \lfloor\log_2 n\rfloor) for every finger f<nf < n.

theoremSplay.Deamortization.Results.static_optimality_generalpropext · Classical.choice · Quot.soundAlgorithms/Splay/Deamortization/Abstract.lean:50
theorem static_optimality_general :    ∃ C D : Nat, ∀ saved : Tree, ∀ d : Algorithm.Implementation Splay.algorithm saved,      ∀ xs : List Nat, (∀ x ∈ xs, x < saved.size) → ∀ out,        d.program xs = some out →        (d.startup + out.costs.sum : ℝ) ≤ 16 * saved.size +          C * Bounds.staticCost saved.size xs + D * saved.size * (1 + (saved.size.log2 : ℝ)) :=  RotationBST.Splay.Deamortization.static_optimality_general
theoremSplay.Deamortization.Results.static_finger_generalpropext · Classical.choice · Quot.soundAlgorithms/Splay/Deamortization/Abstract.lean:58
theorem static_finger_general :    ∃ C D : Nat, ∀ saved : Tree, ∀ d : Algorithm.Implementation Splay.algorithm saved,      ∀ (xs : List Nat) (finger : Nat), finger < saved.size →        (∀ x ∈ xs, x < saved.size) → ∀ out, d.program xs = some out →        (d.startup + out.costs.sum : ℝ) ≤ 16 * saved.size +          C * Bounds.fingerCost finger xs + D * saved.size * (1 + (saved.size.log2 : ℝ)) :=  RotationBST.Splay.Deamortization.static_finger_general

These versions drop the coverage premise. In exchange they keep the startup term n(1+⌊log⁡2n⌋)n(1 + \lfloor\log_2 n\rfloor), and the constants are existential, inherited from Results.staticOptimality and Results.staticFinger. They are uniform: CC and DD are chosen before the tree, the implementation and the sequence. The bracket m+∑iqilog⁡2(m/qi)m + \sum_i q_i\log_2(m/q_i) is Bounds.staticCost, which is at most 33 times the cost of every static tree, and the finger sum is Bounds.fingerCost.

Other applications in the book

The same transformation is applied, through the replay planners of Section 30.10, to the combination of GreedyASS and Tango in Chapter 31. That chapter obtains one BST with the weighted dynamic finger bound, O(log⁡log⁡n)O(\log\log n)-competitiveness and O(log⁡n)O(\log n) worst-case responses (Results.combined).

30.5 Bounded-depth simulation

The rest of the chapter opens the construction. This section is the library’s form of the paper’s Theorem 5, in the weighted version. It is a self-contained statement about the simulation of Section 38.4, and the scheduler below uses its unit-weight form.

The source algorithm here is more general than a rotation-model algorithm. It is a Requests.Algorithm: an online rule that, given the past requests, its current labelled cursor (LabelledCursor.Config, a tree whose nodes carry labels in some type α\alpha, with a persistent cursor) and the next request, emits a list of source instructions that legally visits the requested key, and may keep working afterwards. Its cost is the number of instructions it emits, (a.runFrom past c keys).1.length. The labels carry the weights: ww is a function on labels, and WW is the total weight HeavyPath.mass of the source tree. HeavyPath.Positive asks for positive weights on the tree’s labels.

Theorem (bounded-depth simulation). Let cc be a labelled source cursor on a nonempty tree, with positive weights. There are a fixed simulation policy, an initial representation and a setup trace of at most 16n16n instructions such that, for every source algorithm and every valid sequence, simulating the algorithm request by request:

  1. produces the source’s own final cursor, and a valid representation of it;
  2. is a legal physical program from the root of the representation to its root, and a Pointer.Execution of the requests of cost mm plus its length;
  3. costs at most 248248 physical instructions per source instruction;
  4. leaves the source finger’s key at the physical root; and
  5. at every cut of the emitted stream, the intermediate physical tree has every key xx at depth at most 336+240log⁡2(W/wx)336 + 240\log_2(W/w_x).
theoremDeamortization.Weighted.Results.simulationpropext · Classical.choice · Quot.soundAlgorithms/Deamortization/Weighted/Abstract.lean:10
theorem simulation {α : Type} (weight : α → ℝ) (c : LabelledCursor.Config α)    (live : c.focus ≠ .nil) (positive : HeavyPath.Positive weight c.tree) :    ∃ (p : Simulation.Policy α) (start : State α) (startup : Nat),      Simulation.PolicyWithin weight p ∧ Simulation.ConfigValid weight c start ∧      Pointer.Trace ⟨c.tree.erase, []⟩ ⟨start.pair.tree, []⟩ startup ∧      startup ≤ 16 * c.tree.erase.size ∧      ∀ (a : Simulation.Requests.Algorithm α) (past keys : List Nat),        (∀ x ∈ keys, x < c.tree.erase.size) →        ∃ out, Simulation.Requests.run p a past c start keys = some out ∧          out.source = (a.runFrom past c keys).2 ∧          Simulation.ConfigValid weight out.source out.state ∧          Multifinger.replay out.instructions ⟨start.pair.tree, []⟩ = some ⟨out.state.pair.tree, []⟩ ∧          Pointer.Execution start.pair.tree keys out.state.pair.tree (keys.length + out.cost) ∧          out.cost ≤ 248 * (a.runFrom past c keys).1.length ∧          (Pointer.Config.mk out.state.pair.tree []).key? = out.source.erase.key? ∧          ∀ cut, ∃ middle,            Multifinger.replay (out.instructions.take cut) ⟨start.pair.tree, []⟩ = some middle ∧            Multifinger.replay (out.instructions.drop cut) middle = some ⟨out.state.pair.tree, []⟩ ∧            ∀ rank : Fin c.tree.inorder.length,              (Tree.depth middle.tree rank.val : ℝ) ≤                336 + 240 * Real.logb 2 (HeavyPath.mass weight c.tree /                  weight (c.tree.inorder[rank.val]'rank.isLt)) :=  RotationBST.Deamortization.Weighted.simulation weight c live positive

Item 5 is the strong form of Theorem 5’s depth claim: it holds not only between source instructions, but at every physical instruction boundary of the emitted program. Item 4 is the paper’s “the finger is always at the root of T′T'”. The policy and the initial representation are chosen before the source algorithm, and they are Simulation.PolicyWithin, the bounded policies of Section 38.4 with a structural depth certificate.

Theorem (the simulation is online). If simulating on xs ++ ys succeeds, then simulating on xs succeeds with a prefix of the instructions.

theoremDeamortization.Weighted.Results.online_prefixpropext · Classical.choice · Quot.soundAlgorithms/Deamortization/Weighted/Abstract.lean:34
theorem online_prefix {α : Type} (p : Simulation.Policy α)    (a : Simulation.Requests.Algorithm α) (past : List Nat) (c : LabelledCursor.Config α)    (start : State α) (xs ys : List Nat) {out : Simulation.Output α}    (completed : Simulation.Requests.run p a past c start (xs ++ ys) = some out) :    ∃ first, Simulation.Requests.run p a past c start xs = some first ∧      first.instructions <+: out.instructions :=  RotationBST.Deamortization.Weighted.online_prefix p a past c start xs ys completed

Theorem (the simulation program, Theorem 5). For a family of source algorithms, one for each request sequence, there is one simulation program with the guarantees above, for the source chosen by the sequence. If the family is constant, the program is online.

theoremDeamortization.Weighted.Results.transformpropext · Classical.choice · Quot.soundAlgorithms/Deamortization/Weighted/Abstract.lean:42
theorem transform {α : Type} (family : List Nat → Simulation.Requests.Algorithm α)    (weight : α → ℝ) (c : LabelledCursor.Config α) (live : c.focus ≠ .nil)    (positive : HeavyPath.Positive weight c.tree) :    ∃ (program : List Nat → Option (Simulation.Output α)) (start : State α) (startup : Nat),      Pointer.Trace ⟨c.tree.erase, []⟩ ⟨start.pair.tree, []⟩ startup ∧      startup ≤ 16 * c.tree.erase.size ∧      (∀ keys, (∀ x ∈ keys, x < c.tree.erase.size) →        ∃ out, program keys = some out ∧          out.source = ((family keys).runFrom [] c keys).2 ∧          Simulation.ConfigValid weight out.source out.state ∧          Multifinger.replay out.instructions ⟨start.pair.tree, []⟩ = some ⟨out.state.pair.tree, []⟩ ∧          Pointer.Execution start.pair.tree keys out.state.pair.tree (keys.length + out.cost) ∧          out.cost ≤ 248 * ((family keys).runFrom [] c keys).1.length ∧          (Pointer.Config.mk out.state.pair.tree []).key? = out.source.erase.key? ∧          ∀ cut, ∃ middle,            Multifinger.replay (out.instructions.take cut) ⟨start.pair.tree, []⟩ = some middle ∧            Multifinger.replay (out.instructions.drop cut) middle = some ⟨out.state.pair.tree, []⟩ ∧            ∀ rank : Fin c.tree.inorder.length,              (Tree.depth middle.tree rank.val : ℝ) ≤                336 + 240 * Real.logb 2 (HeavyPath.mass weight c.tree /                  weight (c.tree.inorder[rank.val]'rank.isLt))) ∧      ((∃ a, ∀ keys, family keys = a) →        ∀ xs ys out, program (xs ++ ys) = some out →          ∃ first, program xs = some first ∧ first.instructions <+: out.instructions) :=  RotationBST.Deamortization.Weighted.transform family weight c live positive

The family lets the source depend on the whole sequence, which covers offline source algorithms, as in the paper’s statement “for any BST algorithm AA … if AA is online, so is A′A'”. The emitted instructions are prefix-preserving only when the family is constant, that is, when the source is one online algorithm.

Reading the Lean

The weighted results are not used by the deamortization theorems below, which need only unit weights. Those use the sharper unit-weight depth bound 49+39log⁡2n49 + 39\log_2 n of Section 38.4, proved directly for the same kind of policies.

30.6 Pending work and lag

The scheduler turns a stream of prepared request programs into responses. It knows nothing about heavy paths or pop-tarts. Its input is, for each request, the physical instruction stream that the simulation would execute for it, with two guarantees: the stream visits the requested key, and every intermediate tree along it is shallow.

defDeamortization.Scheduler.HeightpropextAlgorithms/Deamortization/Scheduler/Definitions/Abstract.lean:9
/-- A pending program keeps every intermediate physical tree within the budget. -/def Height (budget : Nat) (start : Pointer.Config) (ops : List Multifinger.Opcode) : Prop :=  ∀ cut middle, Multifinger.replay (ops.take cut) start = some middle →    ∀ x < middle.tree.size, Tree.depth middle.tree x ≤ budget

Height budget start ops says that every prefix of ops that replays from start reaches a tree in which every key has depth at most budget. This is the property a detour needs: from any point of the pending work, any key can be reached and left in O(budget)O(\text{budget}) instructions.

structureDeamortization.Scheduler.Requestno axiomsAlgorithms/Deamortization/Scheduler/Definitions/Abstract.lean:14
/-- The original algorithm supplies only the next request's physical program. -/structure Request where  target : Nat  instructions : List Multifinger.Opcode
defDeamortization.Scheduler.PreparedpropextAlgorithms/Deamortization/Scheduler/Definitions/Abstract.lean:19
/-- A prepared original request contains an actual visit in its recorded code. -/def Prepared (budget : Nat) (start : Pointer.Config) (request : Request) (finish : Pointer.Config) : Prop :=  Multifinger.replay request.instructions start = some finish ∧  (∃ cut visited, cut ≤ request.instructions.length ∧    Multifinger.replay (request.instructions.take cut) start = some visited ∧ visited.key? = some request.target) ∧  Height budget start request.instructions
inductiveDeamortization.Scheduler.PreparedStreampropextAlgorithms/Deamortization/Scheduler/Definitions/Abstract.lean:65
/-- Preparation follows the original cursor, independently of scheduler lag. -/inductive PreparedStream (budget : Nat) : Pointer.Config → List Request → Pointer.Config → Prop where  | nil {cursor : Pointer.Config} : PreparedStream budget cursor [] cursor  | cons {a b c : Pointer.Config} {request : Request} {requests : List Request} :      Prepared budget a request b → PreparedStream budget b requests c →      PreparedStream budget a (request :: requests) c

A Request is the target key with its physical program. Prepared budget start request finish says that the program replays from start to finish, that some prefix of it reaches a cursor on the target (so the program contains an actual visit), and that it satisfies Height. A PreparedStream chains prepared requests, each starting where the previous one’s program ends. This chain follows the source’s cursor, independently of what the scheduler has executed so far.

structureDeamortization.Scheduler.Stateno axiomsAlgorithms/Deamortization/Scheduler/Definitions/Abstract.lean:26
/-- The actual cursor can lag behind the planned endpoint by exactly pending.No original operation is lost when a new request is appended. -/structure State where  cursor : Pointer.Config  pending : List Multifinger.Opcode  planned : Pointer.Config
defDeamortization.Scheduler.ValidpropextAlgorithms/Deamortization/Scheduler/Definitions/Abstract.lean:33
def Valid (budget : Nat) (s : State) : Prop :=  s.cursor.focus ≠ .nil ∧ Multifinger.replay s.pending s.cursor = some s.planned ∧    Height budget s.cursor s.pending
defDeamortization.Scheduler.initialno axiomsAlgorithms/Deamortization/Scheduler/Definitions/Abstract.lean:37
def initial (cursor : Pointer.Config) : State := ⟨cursor, [], cursor⟩

The scheduler’s state has three parts:

      executed                    pending
 ─────────────────────|─────────────────────────────|
                    cursor                       planned

cursor is the actual physical cursor. pending is the simulation work not yet executed, and planned is where the simulation will be once it has all been executed. Valid says that the cursor is on an actual node, that pending replays from cursor to planned, and that it satisfies Height. initial c has nothing pending.

The gap between cursor and planned is the lag of the paper’s proof: the deamortized algorithm can be ahead of the simulation in the input sequence. When a new request arrives, its program is appended after pending and starts at planned. No source instruction is ever dropped while requests keep arriving.

Theorem (initial validity). An initial state is valid if the cursor is on an actual node and every key of its tree has depth at most the budget.

30.7 Serving one request

The core of a response is a statement about one window of pending work.

Theorem (one window). Let ops be a legal program from start to finish that visits the target and keeps every intermediate depth at most budget, with start on an actual node. Then there is a number consumed of instructions to execute, at most the budget, and a physical response before ++ after such that:

  1. executing the first consumed instructions of ops reaches a cursor middle, and the rest of ops still leads from middle to finish;
  2. before leads from start to a cursor on the target, and after from there to middle;
  3. the response has at most 5⋅consumed5 \cdot \mathtt{consumed} instructions, and at most 5⋅budget5 \cdot \mathtt{budget}; and
  4. either all of the budget was consumed, or middle itself is on the target.
theoremDeamortization.Window.Results.servepropext · Classical.choice · Quot.soundAlgorithms/Deamortization/Window/Abstract.lean:10
theorem serve {start finish : Config} {ops : List Multifinger.Opcode}    (executed : Multifinger.replay ops start = some finish) (live : start.focus ≠ .nil)    {target budget : Nat} (valid : target < start.tree.size)    (visits : ∃ cut visited, cut ≤ ops.length ∧      Multifinger.replay (ops.take cut) start = some visited ∧ visited.key? = some target)    (height : ∀ cut middle, Multifinger.replay (ops.take cut) start = some middle →      ∀ x < middle.tree.size, Tree.depth middle.tree x ≤ budget) :    ∃ (consumed : Nat) (middle visited : Config) (before after : List Multifinger.Opcode),      consumed ≤ budget ∧ consumed ≤ ops.length ∧      Multifinger.replay (ops.take consumed) start = some middle ∧      Multifinger.replay (ops.drop consumed) middle = some finish ∧      Multifinger.replay before start = some visited ∧ visited.key? = some target ∧      Multifinger.replay after visited = some middle ∧      before.length + after.length ≤ 5 * consumed ∧      1 + before.length + after.length ≤ 1 + 5 * budget ∧      (consumed = budget ∨ middle.key? = some target) :=  RotationBST.Deamortization.Window.serve executed live valid visits height

This is the paper’s rule, read for one request. Execute the source work; if the target is reached within the budget, stop there. Otherwise stop after the budget, and walk to the target and back. The walk stays within the tree reached after consumed instructions, whose depths are at most the budget, so it costs O(budget)O(\text{budget}), and the library bounds the whole response by five times what it consumed. After the response, the physical cursor is exactly where the source work stands after consumed instructions: the detour returns precisely to it, so the remaining work can continue unchanged.

A Response records the new state, the visited cursor, the two parts of the physical response and the number of pending instructions consumed. Its instructions are before ++ after. Correct budget s request out lists what a response must satisfy:

  1. the new state is valid;
  2. the planned endpoint advances by the request’s whole program;
  3. at most budget instructions are consumed, and no more than are pending, counting the new request’s program;
  4. the new pending work is what remains after them;
  5. executing the consumed instructions leads from the old cursor to the new one;
  6. before leads from the old cursor to a cursor on the target, and after from there to the new cursor; and
  7. the response has at most 5⋅consumed5 \cdot \mathtt{consumed} instructions, and its cost 1+∣instructions∣1 + |\mathtt{instructions}| is at most 1+5⋅budget1 + 5 \cdot \mathtt{budget}.

Three consequences are used repeatedly.

Theorem (a correct response). A correct response replays from the old cursor to the new one. It conserves work: pending plus consumed after the response equals pending before it plus the request’s program. And, from a valid state, it is a one-finger Multifinger.Serves of the target at cost 1+∣instructions∣1 + |\mathtt{instructions}|.

theoremDeamortization.Scheduler.Results.response_replaypropext · Quot.soundAlgorithms/Deamortization/Scheduler/Abstract.lean:17
theorem response_replay {budget : Nat} {s : State} {request : Request} {out : Response}    (correct : Correct budget s request out) :    Multifinger.replay out.instructions s.cursor = some out.state.cursor :=  RotationBST.Deamortization.Scheduler.response_replay correct
theoremDeamortization.Scheduler.Results.response_accountpropext · Quot.soundAlgorithms/Deamortization/Scheduler/Abstract.lean:22
theorem response_account {budget : Nat} {s : State} {request : Request} {out : Response}    (correct : Correct budget s request out) :    out.state.pending.length + out.consumed = s.pending.length + request.instructions.length :=  RotationBST.Deamortization.Scheduler.response_account correct
theoremDeamortization.Scheduler.Results.response_servespropext · Quot.soundAlgorithms/Deamortization/Scheduler/Abstract.lean:27
theorem response_serves {budget : Nat} {s : State} {request : Request} {out : Response}    (valid : Valid budget s) (correct : Correct budget s request out) :    Multifinger.Serves (cursorState s.cursor) (cursorState out.state.cursor) request.target      (1 + out.instructions.length) :=  RotationBST.Deamortization.Scheduler.response_serves valid correct

30.8 Running the scheduler

defDeamortization.Scheduler.Policyno axiomsAlgorithms/Deamortization/Scheduler/Definitions/Abstract.lean:59
abbrev Policy := State → Request → Option Response
defDeamortization.Scheduler.PolicyValidpropextAlgorithms/Deamortization/Scheduler/Definitions/Abstract.lean:61
def PolicyValid (budget : Nat) (p : Policy) : Prop :=  ∀ s request, Valid budget s → (∃ finish, Prepared budget s.planned request finish) →    ∃ out, p s request = some out ∧ Correct budget s request out

A scheduling Policy is a function of the current state and the current request only, returning a response or none. It never sees later requests. PolicyValid budget p asks that, from every valid state, for every request prepared from the planned endpoint, the policy returns a correct response.

Theorem (a valid policy exists). For every budget there is a valid scheduling policy.

The proof applies the window theorem to the pending work followed by the new request’s program, which is legal, visits the target and has bounded height, and chooses the result.

defDeamortization.Scheduler.runno axiomsAlgorithms/Deamortization/Scheduler/Definitions/Abstract.lean:91
def run (p : Policy) (s : State) : List Request → Option Output  | [] => some ⟨s, [], 0, []⟩  | request :: requests => (p s request).bind fun first =>      (run p first.state requests).map fun last =>        ⟨last.state, first.instructions ++ last.instructions,          first.consumed + last.consumed, (1 + first.instructions.length) :: last.costs⟩

run p s requests threads the state through the requests. It concatenates the response instructions, adds the consumed counts, and records the cost 1+∣instructions∣1 + |\mathtt{instructions}| of each response.

Theorem (scheduled run). Let the policy be valid, the state valid, and the requests a prepared stream from its planned endpoint. Then the run succeeds, and:

  1. it ends planned at the end of the stream, in a valid state;
  2. its instructions replay from the cursor, and form a one-finger execution of the targets of cost mm plus their length;
  3. pending plus consumed equals the initial pending plus all request programs;
  4. it emits at most 55 times what it consumed, and consumes at most the budget times the number of requests; and
  5. it has one cost per request, summing to mm plus the number of instructions, each at most 1+5⋅budget1 + 5 \cdot \mathtt{budget}.
theoremDeamortization.Scheduler.Results.run_specpropext · Quot.soundAlgorithms/Deamortization/Scheduler/Abstract.lean:33
theorem run_spec {budget : Nat} {p : Policy} (policyValid : PolicyValid budget p)    {s : State} {requests : List Request} {finish : Pointer.Config}    (valid : Valid budget s) (ready : PreparedStream budget s.planned requests finish) :    ∃ out, run p s requests = some out ∧ out.state.planned = finish ∧ Valid budget out.state ∧      Multifinger.replay out.instructions s.cursor = some out.state.cursor ∧      Multifinger.Execution (cursorState s.cursor) (requests.map Request.target)        (cursorState out.state.cursor) (requests.length + out.instructions.length) ∧      out.state.pending.length + out.consumed =        s.pending.length + (requests.map (fun request => request.instructions.length)).sum ∧      out.instructions.length ≤ 5 * out.consumed ∧      out.costs.length = requests.length ∧ out.costs.sum = requests.length + out.instructions.length ∧      (∀ cost ∈ out.costs, cost ≤ 1 + 5 * budget) ∧ out.consumed ≤ budget * requests.length :=  RotationBST.Deamortization.Scheduler.run_spec policyValid valid ready

Theorem (scheduled run, served). Under the same hypotheses, the emitted instructions and the costs form a served stream of the targets.

Theorem (prefixes). Running on xs ++ ys is running on xs and continuing from its final state on ys, with outputs concatenated. Hence a run on xs ++ ys that succeeds extends a successful run on xs, in both instructions and costs.

run_prefix holds for every policy, valid or not: causality comes from the shape of run alone, because a policy sees only its state and the current request. This is where the causality of the final program comes from, once the preparation of the requests is causal as well.

30.9 Preparing the source

This section composes the simulation of Section 38.4 with the scheduler, for a source algorithm with labelled cursors, as in Section 30.5, and unit weights.

structureDeamortization.Finger.PreparedOutputpropext · Classical.choice · Quot.soundAlgorithms/Deamortization/Finger/Definitions/Abstract.lean:10
/-- The original source simulation plans request programs causally. Thescheduler may still be executing a prefix of the accumulated programs. -/structure PreparedOutput (α : Type) where  source : LabelledCursor.Config α  history : State α  requests : List Scheduler.Request
defDeamortization.Finger.preparepropext · Classical.choice · Quot.soundAlgorithms/Deamortization/Finger/Definitions/Abstract.lean:17
def prepare {α : Type} (p : Simulation.Policy α) (a : Simulation.Requests.Algorithm α)    (past : List Nat) (c : LabelledCursor.Config α) (s : State α) : List Nat → Option (PreparedOutput α)  | [] => some ⟨c, s, []⟩  | key :: keys => (Simulation.run p c s (a.step past c key).1).bind fun first =>      (prepare p a (past ++ [key]) first.source first.state keys).map fun last =>        ⟨last.source, last.history, ⟨key, first.instructions⟩ :: last.requests⟩
defDeamortization.Finger.transformpropext · Classical.choice · Quot.soundAlgorithms/Deamortization/Finger/Definitions/Abstract.lean:24
/-- Both policies are fixed before the input sequence. Planning time followsthe abstract BST model; the output counts every executed pointer instruction. -/def transform {α : Type} (sourcePolicy : Simulation.Policy α) (scheduler : Scheduler.Policy)    (a : Simulation.Requests.Algorithm α) (past : List Nat) (c : LabelledCursor.Config α)    (s : State α) (keys : List Nat) : Option Scheduler.Output :=  (prepare sourcePolicy a past c s keys).bind fun ready =>    Scheduler.run scheduler (Scheduler.initial ⟨s.pair.tree, []⟩) ready.requests

prepare runs the source algorithm on the keys, request by request. For each key it takes the source’s instructions for that key, simulates them with the policy Simulation.run, and records the resulting physical program as a Scheduler.Request for that key. It returns the final source cursor, the final representation and the list of requests. transform prepares the requests and schedules them, starting with the physical cursor at the root of the initial representation. The scheduler executes a prefix of the accumulated programs, so it may lag behind the preparation. Both policies are fixed before the keys, and the preparation sees each key only when it is requested.

The first group of results describes the preparation. The budget must be at least 49+39log⁡2n49 + 39\log_2 n, the unit-weight depth bound of the simulation; ss is a valid representation of the source cursor cc, and the simulation policy is Simulation.PolicyBounded.

Theorem (preparation). The preparation succeeds on valid keys. It ends at the source’s own final cursor, with a valid representation, one request per key, and a prepared stream from the root of the initial representation. Its total physical work plus the final potential is at most 6262 times the source’s instructions plus the initial potential. Measured by the excess over the cheapest representation of the same source cursor (Minimum.excess), the factor is 248248.

theoremDeamortization.Finger.Results.prepare_specpropext · Classical.choice · Quot.soundAlgorithms/Deamortization/Finger/Abstract.lean:17
theorem prepare_spec {α : Type} {p : Simulation.Policy α}    (bounded : Simulation.PolicyBounded (fun _ => 1) p) (a : Simulation.Requests.Algorithm α)    (past : List Nat) {c : LabelledCursor.Config α} {s : State α}    (valid : Simulation.ConfigValid (fun _ => 1) c s) {budget : Nat}    (budgetBound : 49 + 39 * Real.logb 2 s.pair.tree.size ≤ budget)    (keys : List Nat) (validKeys : ∀ x ∈ keys, x < c.tree.erase.size) :    ∃ out, prepare p a past c s keys = some out ∧ out.source = (a.runFrom past c keys).2 ∧      Simulation.ConfigValid (fun _ => 1) out.source out.history ∧ out.requests.map Scheduler.Request.target = keys ∧      Scheduler.PreparedStream budget ⟨s.pair.tree, []⟩ out.requests ⟨out.history.pair.tree, []⟩ ∧      (out.requests.map (fun request => request.instructions.length)).sum + out.history.potential ≤        62 * (a.runFrom past c keys).1.length + s.potential :=  RotationBST.Deamortization.Finger.prepare_spec bounded a past valid budgetBound keys validKeys
theoremDeamortization.Finger.Results.prepare_excesspropext · Classical.choice · Quot.soundAlgorithms/Deamortization/Finger/Abstract.lean:90
theorem prepare_excess {α : Type} {p : Simulation.Policy α}    (bounded : Simulation.PolicyBounded (fun _ => 1) p) (a : Simulation.Requests.Algorithm α)    (past : List Nat) {c : LabelledCursor.Config α} {s : State α}    (valid : Simulation.ConfigValid (fun _ => 1) c s) {budget : Nat}    (budgetBound : 49 + 39 * Real.logb 2 s.pair.tree.size ≤ budget)    (keys : List Nat) (validKeys : ∀ x ∈ keys, x < c.tree.erase.size) :    ∃ out, prepare p a past c s keys = some out ∧ out.source = (a.runFrom past c keys).2 ∧      Simulation.ConfigValid (fun _ => 1) out.source out.history ∧ out.requests.map Scheduler.Request.target = keys ∧      Scheduler.PreparedStream budget ⟨s.pair.tree, []⟩ out.requests ⟨out.history.pair.tree, []⟩ ∧      (out.requests.map (fun request => request.instructions.length)).sum + Simulation.Minimum.excess (fun _ => 1) out.source out.history ≤        248 * (a.runFrom past c keys).1.length + Simulation.Minimum.excess (fun _ => 1) c s :=  RotationBST.Deamortization.Finger.prepare_excess bounded a past valid budgetBound keys validKeys

Theorem (preparation is causal). Preparing xs ++ ys is preparing xs and continuing with history past ++ xs. Its requests extend those of xs.

theoremDeamortization.Finger.Results.prepare_appendpropext · Classical.choice · Quot.soundAlgorithms/Deamortization/Finger/Abstract.lean:30
theorem prepare_append {α : Type} (p : Simulation.Policy α) (a : Simulation.Requests.Algorithm α)    (past : List Nat) (c : LabelledCursor.Config α) (s : State α) (xs ys : List Nat) :    prepare p a past c s (xs ++ ys) = (prepare p a past c s xs).bind fun first =>      (prepare p a (past ++ xs) first.source first.history ys).map fun last =>        ⟨last.source, last.history, first.requests ++ last.requests⟩ :=  RotationBST.Deamortization.Finger.prepare_append p a past c s xs ys
theoremDeamortization.Finger.Results.prepare_prefixpropext · Classical.choice · Quot.soundAlgorithms/Deamortization/Finger/Abstract.lean:37
theorem prepare_prefix {α : Type} (p : Simulation.Policy α) (a : Simulation.Requests.Algorithm α)    (past : List Nat) (c : LabelledCursor.Config α) (s : State α) (xs ys : List Nat)    {out : PreparedOutput α} (completed : prepare p a past c s (xs ++ ys) = some out) :    ∃ first, prepare p a past c s xs = some first ∧ first.requests <+: out.requests :=  RotationBST.Deamortization.Finger.prepare_prefix p a past c s xs ys completed

The second group describes the composed transform.

Theorem (initial validity). The scheduler’s initial state at the root of a valid representation is valid for every budget of at least 49+39log⁡2n49 + 39\log_2 n.

theoremDeamortization.Finger.Results.initial_validpropext · Classical.choice · Quot.soundAlgorithms/Deamortization/Finger/Abstract.lean:10
theorem initial_valid {α : Type} {p : Simulation.Policy α}    (bounded : Simulation.PolicyBounded (fun _ => 1) p)    {c : LabelledCursor.Config α} {s : State α} (valid : Simulation.ConfigValid (fun _ => 1) c s)    {budget : Nat} (budgetBound : 49 + 39 * Real.logb 2 s.pair.tree.size ≤ budget) :    Scheduler.Valid budget (Scheduler.initial ⟨s.pair.tree, []⟩) :=  RotationBST.Deamortization.Finger.initial_valid bounded valid budgetBound

Theorem (transform). With a bounded simulation policy and a valid scheduling policy, the transform succeeds on valid keys. Its instructions are legal, serve the keys as a one-finger execution and, with the excess version, as a served stream. The responses cost at most 1+5⋅budget1 + 5 \cdot \mathtt{budget} each. The physical instructions, plus 55 times the pending work and the final potential, are at most 310310 times the source’s instructions plus 55 times the initial potential. Measured by excess, the factor is 12401240.

theoremDeamortization.Finger.Results.transform_specpropext · Classical.choice · Quot.soundAlgorithms/Deamortization/Finger/Abstract.lean:51
theorem transform_spec {α : Type} {sourcePolicy : Simulation.Policy α} {scheduler : Scheduler.Policy}    (bounded : Simulation.PolicyBounded (fun _ => 1) sourcePolicy) {budget : Nat}    (schedulerValid : Scheduler.PolicyValid budget scheduler) (a : Simulation.Requests.Algorithm α)    (past : List Nat) {c : LabelledCursor.Config α} {s : State α}    (valid : Simulation.ConfigValid (fun _ => 1) c s)    (budgetBound : 49 + 39 * Real.logb 2 s.pair.tree.size ≤ budget)    (keys : List Nat) (validKeys : ∀ x ∈ keys, x < c.tree.erase.size) :    ∃ (out : Scheduler.Output) (planned : PreparedOutput α),      prepare sourcePolicy a past c s keys = some planned ∧      transform sourcePolicy scheduler a past c s keys = some out ∧      out.state.planned = ⟨planned.history.pair.tree, []⟩ ∧ Scheduler.Valid budget out.state ∧      Multifinger.replay out.instructions ⟨s.pair.tree, []⟩ = some out.state.cursor ∧      Multifinger.Execution (Scheduler.cursorState ⟨s.pair.tree, []⟩) keys        (Scheduler.cursorState out.state.cursor) (keys.length + out.instructions.length) ∧      out.instructions.length + 5 * out.state.pending.length + 5 * planned.history.potential ≤        310 * (a.runFrom past c keys).1.length + 5 * s.potential ∧      out.costs.length = keys.length ∧ out.costs.sum = keys.length + out.instructions.length ∧      ∀ cost ∈ out.costs, cost ≤ 1 + 5 * budget :=  RotationBST.Deamortization.Finger.transform_spec bounded schedulerValid a past valid budgetBound keys validKeys
theoremDeamortization.Finger.Results.transform_excesspropext · Classical.choice · Quot.soundAlgorithms/Deamortization/Finger/Abstract.lean:103
theorem transform_excess {α : Type} {sourcePolicy : Simulation.Policy α} {scheduler : Scheduler.Policy}    (bounded : Simulation.PolicyBounded (fun _ => 1) sourcePolicy) {budget : Nat}    (schedulerValid : Scheduler.PolicyValid budget scheduler) (a : Simulation.Requests.Algorithm α)    (past : List Nat) {c : LabelledCursor.Config α} {s : State α}    (valid : Simulation.ConfigValid (fun _ => 1) c s)    (budgetBound : 49 + 39 * Real.logb 2 s.pair.tree.size ≤ budget)    (keys : List Nat) (validKeys : ∀ x ∈ keys, x < c.tree.erase.size) :    ∃ (out : Scheduler.Output) (planned : PreparedOutput α),      prepare sourcePolicy a past c s keys = some planned ∧      transform sourcePolicy scheduler a past c s keys = some out ∧      out.state.planned = ⟨planned.history.pair.tree, []⟩ ∧ Scheduler.Valid budget out.state ∧      Multifinger.replay out.instructions ⟨s.pair.tree, []⟩ = some out.state.cursor ∧      Scheduler.ServedStream ⟨s.pair.tree, []⟩ keys out.state.cursor out.instructions out.costs ∧      Multifinger.Execution (Scheduler.cursorState ⟨s.pair.tree, []⟩) keys        (Scheduler.cursorState out.state.cursor) (keys.length + out.instructions.length) ∧      out.instructions.length + 5 * out.state.pending.length + 5 * Simulation.Minimum.excess (fun _ => 1) planned.source planned.history ≤        1240 * (a.runFrom past c keys).1.length + 5 * Simulation.Minimum.excess (fun _ => 1) c s ∧      out.costs.length = keys.length ∧ out.costs.sum = keys.length + out.instructions.length ∧      ∀ cost ∈ out.costs, cost ≤ 1 + 5 * budget :=  RotationBST.Deamortization.Finger.transform_excess bounded schedulerValid a past valid budgetBound keys validKeys

Theorem (transform is causal). A successful transform on xs ++ ys extends a successful transform on xs, in instructions and costs.

theoremDeamortization.Finger.Results.transform_prefixpropext · Classical.choice · Quot.soundAlgorithms/Deamortization/Finger/Abstract.lean:43
theorem transform_prefix {α : Type} (sourcePolicy : Simulation.Policy α) (scheduler : Scheduler.Policy)    (a : Simulation.Requests.Algorithm α) (past : List Nat) (c : LabelledCursor.Config α)    (s : State α) (xs ys : List Nat) {out : Scheduler.Output}    (completed : transform sourcePolicy scheduler a past c s (xs ++ ys) = some out) :    ∃ first, transform sourcePolicy scheduler a past c s xs = some first ∧      first.instructions <+: out.instructions ∧ first.costs <+: out.costs :=  RotationBST.Deamortization.Finger.transform_prefix sourcePolicy scheduler a past c s xs ys completed

The last group chooses the policies, the budget and the initial representation. Two versions differ in the starting representation.

Theorem (an initialized transform). For a source algorithm and a source cursor on a nonempty tree, there are a bounded simulation policy, a valid scheduling policy, a budget below 50+39log⁡2n50 + 39\log_2 n, a valid representation and a setup trace of at most 16n16n instructions to reach it, such that on every valid sequence, setup and responses together cost at most 131n+m+310⋅(source instructions)131n + m + 310 \cdot (\text{source instructions}), and every response costs less than 251+195log⁡2n251 + 195\log_2 n.

Theorem (a minimized transform). As above, but the representation is the cheapest one, with zero excess, and everything is chosen before the source algorithm. Then for every source algorithm the physical instructions are at most 12401240 times the source’s, with a served stream, and every response costs less than 251+195log⁡2n251 + 195\log_2 n.

theoremDeamortization.Finger.Results.initialized_transformpropext · Classical.choice · Quot.soundAlgorithms/Deamortization/Finger/Abstract.lean:71
theorem initialized_transform {α : Type} (a : Simulation.Requests.Algorithm α)    (c : LabelledCursor.Config α) (live : c.focus ≠ .nil) :    ∃ (sourcePolicy : Simulation.Policy α) (scheduler : Scheduler.Policy) (budget : Nat)      (s : State α) (startup : Nat),      Simulation.PolicyBounded (fun _ => 1) sourcePolicy ∧ Scheduler.PolicyValid budget scheduler ∧      Simulation.ConfigValid (fun _ => 1) c s ∧      Pointer.Trace ⟨c.tree.erase, []⟩ ⟨s.pair.tree, []⟩ startup ∧ startup ≤ 16 * c.tree.erase.size ∧      (budget : ℝ) < 50 + 39 * Real.logb 2 c.tree.erase.size ∧      ∀ (past keys : List Nat), (∀ x ∈ keys, x < c.tree.erase.size) →        ∃ out, transform sourcePolicy scheduler a past c s keys = some out ∧          Multifinger.replay out.instructions ⟨s.pair.tree, []⟩ = some out.state.cursor ∧          Multifinger.Execution (Scheduler.cursorState ⟨s.pair.tree, []⟩) keys            (Scheduler.cursorState out.state.cursor) (keys.length + out.instructions.length) ∧          startup + keys.length + out.instructions.length ≤            131 * c.tree.erase.size + keys.length + 310 * (a.runFrom past c keys).1.length ∧          out.costs.length = keys.length ∧ out.costs.sum = keys.length + out.instructions.length ∧          ∀ cost ∈ out.costs, (cost : ℝ) < 251 + 195 * Real.logb 2 c.tree.erase.size :=  RotationBST.Deamortization.Finger.initialized_transform a c live
theoremDeamortization.Finger.Results.minimized_transformpropext · Classical.choice · Quot.soundAlgorithms/Deamortization/Finger/Abstract.lean:124
theorem minimized_transform {α : Type}    (c : LabelledCursor.Config α) (live : c.focus ≠ .nil) :    ∃ (sourcePolicy : Simulation.Policy α) (scheduler : Scheduler.Policy) (budget : Nat)      (s : State α) (startup : Nat),      Simulation.PolicyBounded (fun _ => 1) sourcePolicy ∧ Scheduler.PolicyValid budget scheduler ∧      Simulation.ConfigValid (fun _ => 1) c s ∧      Simulation.Minimum.excess (fun _ => 1) c s = 0 ∧      Pointer.Trace ⟨c.tree.erase, []⟩ ⟨s.pair.tree, []⟩ startup ∧ startup ≤ 16 * c.tree.erase.size ∧      (budget : ℝ) < 50 + 39 * Real.logb 2 c.tree.erase.size ∧      ∀ (a : Simulation.Requests.Algorithm α) (past keys : List Nat), (∀ x ∈ keys, x < c.tree.erase.size) →        ∃ out, transform sourcePolicy scheduler a past c s keys = some out ∧          Multifinger.replay out.instructions ⟨s.pair.tree, []⟩ = some out.state.cursor ∧          Scheduler.ServedStream ⟨s.pair.tree, []⟩ keys out.state.cursor out.instructions out.costs ∧          Multifinger.Execution (Scheduler.cursorState ⟨s.pair.tree, []⟩) keys            (Scheduler.cursorState out.state.cursor) (keys.length + out.instructions.length) ∧          out.instructions.length ≤ 1240 * (a.runFrom past c keys).1.length ∧          out.costs.length = keys.length ∧ out.costs.sum = keys.length + out.instructions.length ∧          ∀ cost ∈ out.costs, (cost : ℝ) < 251 + 195 * Real.logb 2 c.tree.erase.size :=  RotationBST.Deamortization.Finger.minimized_transform c live

The first version pays the setup and the initial potential, which is linear, in the 131n131n term. The second avoids any additive term by starting from a representation of least potential; the setup still pays 16n16n to build it. In minimized_transform, the policies, the budget and the representation are fixed before the source algorithm is named, so the physical machinery is the same for all sources.

Theorem (abstract deamortization). For a family of source algorithms, one per request sequence, and a source cursor on a nonempty tree, there are an initial representation, a setup trace of at most 16n16n instructions and one program such that on every valid sequence the program serves it as a one-finger execution and a served stream, emits at most 12401240 times the instructions of the source chosen for that sequence, and costs less than 251+195log⁡2n251 + 195\log_2 n per response. If the family is constant, the program is causal in instructions and costs.

theoremDeamortization.Finger.Results.deamortizepropext · Classical.choice · Quot.soundAlgorithms/Deamortization/Finger/Abstract.lean:144
theorem deamortize {α : Type} (family : List Nat → Simulation.Requests.Algorithm α)    (c : LabelledCursor.Config α) (live : c.focus ≠ .nil) :    ∃ (s : State α) (startup : Nat) (program : List Nat → Option Scheduler.Output),      Simulation.ConfigValid (fun _ => 1) c s ∧      Pointer.Trace ⟨c.tree.erase, []⟩ ⟨s.pair.tree, []⟩ startup ∧ startup ≤ 16 * c.tree.erase.size ∧      (∀ keys, (∀ x ∈ keys, x < c.tree.erase.size) →        ∃ out, program keys = some out ∧          Multifinger.replay out.instructions ⟨s.pair.tree, []⟩ = some out.state.cursor ∧          Scheduler.ServedStream ⟨s.pair.tree, []⟩ keys out.state.cursor out.instructions out.costs ∧          Multifinger.Execution (Scheduler.cursorState ⟨s.pair.tree, []⟩) keys            (Scheduler.cursorState out.state.cursor) (keys.length + out.instructions.length) ∧          out.instructions.length ≤ 1240 * ((family keys).runFrom [] c keys).1.length ∧          out.costs.length = keys.length ∧ out.costs.sum = keys.length + out.instructions.length ∧          ∀ cost ∈ out.costs, (cost : ℝ) < 251 + 195 * Real.logb 2 c.tree.erase.size) ∧      ((∃ a, ∀ keys, family keys = a) →        ∀ xs ys out, program (xs ++ ys) = some out →          ∃ first, program xs = some first ∧ first.instructions <+: out.instructions ∧            first.costs <+: out.costs) :=  RotationBST.Deamortization.Finger.deamortize family c live

This is the library’s most general form of Theorem 6. As in Weighted.Results.transform, the family covers sources that depend on the whole sequence, and causality is claimed only for a single online source, matching the paper’s “if AA is online, so is A′′A''”.

30.10 From rotation algorithms to a deamortized program

The last step connects the abstract theorem to the algorithms of the book, which are rotation-model algorithms or root-restarting pointer programs. The bridge is the replay planner of Section 29.2, Replay.Planner: a causal rule that, for each request, plans a pointer program from the root of its current tree back to the root, visiting the key. Replay.run counts its recorded work, the length of each plan plus 22 per request. A planner has two sides (a Bool), because the combination of Section 29.1 runs two of them; here one side is used.

Theorem (deamortizing a planner). For every planner on a nonempty tree, and either side, there are an initial tree, a setup trace of at most 16n16n instructions and one causal program such that on every valid sequence the program serves it as a served stream and a one-finger execution, its costs sum to at most 12401240 times the planner’s recorded work, and each response costs less than 251+195log⁡2n251 + 195\log_2 n.

theoremDeamortization.Replay.Results.deamortizepropext · Classical.choice · Quot.soundAlgorithms/Deamortization/Replay/Abstract.lean:9
theorem deamortize {σ : Type} {saved : Tree} (planner : Combo.Worker.Replay.Planner σ saved)    (side : Bool) (nonempty : saved ≠ .nil) :    ∃ (initial : Tree) (startup : Nat) (program : List Nat → Option Scheduler.Output),      Pointer.Trace ⟨saved, []⟩ ⟨initial, []⟩ startup ∧ startup ≤ 16 * saved.size ∧      (∀ keys, (∀ x ∈ keys, x < saved.size) →        ∃ out, program keys = some out ∧          Multifinger.replay out.instructions ⟨initial, []⟩ = some out.state.cursor ∧          Scheduler.ServedStream ⟨initial, []⟩ keys out.state.cursor out.instructions out.costs ∧          Multifinger.Execution (Scheduler.cursorState ⟨initial, []⟩) keys            (Scheduler.cursorState out.state.cursor) (keys.length + out.instructions.length) ∧          out.costs.length = keys.length ∧ out.costs.sum = keys.length + out.instructions.length ∧          out.costs.sum ≤ 1240 * (Combo.Worker.Replay.run planner side (planner.initial side) keys).2 ∧          ∀ cost ∈ out.costs, (cost : ℝ) < 251 + 195 * Real.logb 2 saved.size) ∧      (∀ xs ys out, program (xs ++ ys) = some out →        ∃ first, program xs = some first ∧ first.instructions <+: out.instructions ∧          first.costs <+: out.costs) :=  RotationBST.Deamortization.Replay.deamortize planner side nonempty
Statement reads only Models.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, Bounds.StaticTree.Definitions, Structures.HeavyPath.Finger.History.Simulation.Requests.Definitions, Models.Multifinger.Definitions, Algorithms.Deamortization.Scheduler.Definitions, Algorithms.Deamortization.Finger.Definitions, Structures.HeavyPath.Finger.History.Simulation.Minimum.Definitions

The proof labels the tree by its ranks, reads the planner as a source algorithm with a persistent cursor, and applies Finger.Results.deamortize with a constant family, which gives causality. Each plan starts and ends at the root, so consecutive plans chain into one program of a persistent cursor.

implementation_exists then takes the planner of Results.algorithm_planner, which runs a rotation-model algorithm through the lazy online simulation of Chapter 7 and records at most 6464 times its total cost. Composing gives 1240⋅64=793601240 \cdot 64 = 79360, the constant of Algorithm.Implementation. Chapter 31 uses Replay.Results.deamortize directly, with its own planner.

30.11 What is not formalized

The library proves the abstract BST-operation result of Theorem 6, its unit-weight and weighted simulations (Theorem 5), and the Splay bounds above. The following parts of the paper are not formalized.

  • Theorem 7. The paper’s Theorem 7 strengthens Theorem 6 for algorithms with an amortized bound O(nf(n)+kf(n))O(nf(n) + kf(n)), f(n)=Ω(log⁡n)f(n) = \Omega(\log n): the deamortized algorithm works in the pointer machine model with O(1)O(1) words per node and O(1)O(1) global variables, and computes each access in time proportional to its BST operations. None of these clauses is formalized: the bounds on private computation, on auxiliary storage (the paper’s queue of pending keys, stored in the tree nodes, and its overflow argument), and execution on a finite-word machine. In the library, program is an arbitrary function, and the pending work of the scheduler is an unbounded list. The machine model for these clauses is defined in Chapter 13, with a compiler to the pointer machine; the deamortization is not yet given in it.
  • Cole’s dynamic finger theorem for splaying. The paper deamortizes all of splaying’s known bounds, including the dynamic finger bound (Cole et al. 2000; Cole 2000). The library has no proof of the dynamic finger theorem for splay trees, so it does not state it for the deamortized program. (The weighted dynamic finger bound for a different deamortized BST is the subject of Chapter 31.)
  • Key-independent optimality of splay trees (Iacono 2005), and therefore its deamortized form, is not formalized.
  • Multi-splay trees. The paper’s application to multi-splay trees (Wang et al. 2006), a deamortized BST that is O(log⁡log⁡n)O(\log\log n)-competitive and has the scanning, static optimality, static finger and working-set bounds, is not formalized, since the library has no multi-splay trees. Chapter 31 obtains a deamortized O(log⁡log⁡n)O(\log\log n)-competitive BST by a different route, combining GreedyASS with Tango.
  • Dynamic optimality. Results.dynamic_optimal is an implication. No dynamically optimal algorithm is known, and none is claimed.