Chapter 37 · Pop-tarts · Section 37.2Chocolate pop-tarts
A pop-tart (Section 37.1) is a binary search tree that implements a stack. A new node is pushed as the parent of the current root, with the old stack as its right child, and a pop removes the root’s left fringe slot. Between operations the structure may rebalance itself with pointer moves and rotations, and the finger must end at the root again. The Vanilla pop-tart does nothing, and keeps a slot of weight near the root only if every new slot outweighs all older ones. The Cherry pop-tart is a 2–4 tree turned sideways, and handles unit weights or weights that increase along the stack.
The Chocolate pop-tart of Bose, Collette, Fagerberg and Langerman (2012a) handles arbitrary weights. It is the building block of their deamortization: each heavy path of the simulated tree is stored as two Chocolate pop-tarts, whose slots are the subtrees hanging off the path, weighted by their sizes. Its Lemma 4 says that the structure is crazy good: amortized and worst-case time per push and pop, and every slot of weight at depth , where is the total weight. This section formalizes that lemma with explicit constants, and two refinements that the next chapter needs: operations that can be interrupted after any instruction with bounded intermediate depths, and in-place replacement of the top slot.
Start a Chocolate pop-tart from a single bottom slot of nonnegative weight. Run any sequence of operations, each either a push of one new fringe slot (an arbitrary payload subtree with an arbitrary nonnegative weight) or a pop, which is available whenever the stack holds at least two slots. Then:
- Stack semantics. Every state is valid. A push prepends the new slot to the inorder sequence of weighted slots, and a pop exposes the first slot under a separator at the root and leaves the rest.
- Local pointer operations. The rebalancing of each operation is a pointer-machine trace of exactly moves and rotations, from the root of the stack back to its root, and it exists below any surrounding context: the stack may sit anywhere inside a larger tree.
- Amortized cost. for a potential , and from the initial single slot, .
- Worst-case cost. Each operation on a stack with fringe slots has .
- Weighted depth. After the sequence, every slot of weight is at depth at most
where is the total weight of the slots.
This is Lemma 4 of the paper. The paper’s depth bound is ; the Lean constant is one larger. The cost of an operation counts only the rebalancing: placing the caller’s new separator on a push, and removing the exposed one on a pop, belong to the caller, which is why the bounds add per operation. Weights live on the fringe slots only; separators weigh nothing. A slot of weight is allowed but gets no depth bound.
The Lean statements
theorem push {stack : Layer} {level : Nat} (valid : stack.Valid level) {new : WeightedTree} (perfect : Perfect level new) (nonnegative : new.Nonnegative) (cs : List Pointer.Frame) : (stack.push new).Valid level ∧ (stack.push new).decode.fringe = new.fringe ++ stack.decode.fringe ∧ (stack.push new).decode.mass = new.mass + stack.decode.mass ∧ Pointer.Trace ⟨.node new.tree stack.decode.tree, cs⟩ ⟨(stack.push new).decode.tree, cs⟩ stack.pushCost ∧ stack.pushCost + (stack.push new).potential ≤ 20 + stack.potential := … RotationBST.PopTart.Chocolate.push valid perfect nonnegative cstheorem pop {stack : Layer} {level : Nat} (valid : stack.Valid level) (top : stack.HasTop) (cs : List Pointer.Frame) : ∃ crumb rest, stack.pop = some (crumb, rest) ∧ Perfect level crumb ∧ crumb.Nonnegative ∧ rest.Valid level ∧ stack.decode.fringe = crumb.fringe ++ rest.decode.fringe ∧ stack.decode.mass = crumb.mass + rest.decode.mass ∧ Pointer.Trace ⟨stack.decode.tree, cs⟩ ⟨.node crumb.tree rest.decode.tree, cs⟩ stack.popCost ∧ stack.popCost + rest.potential ≤ 20 + stack.potential := … RotationBST.PopTart.Chocolate.pop valid top cstheorem hasTop_of_fringe {stack : Layer} (valid : stack.Valid 0) {subtree : Tree} {weight : ℝ} {suffix : List (Tree × ℝ)} (fringe : stack.decode.fringe = (subtree, weight) :: suffix) (nonempty : suffix ≠ []) : stack.HasTop := … RotationBST.PopTart.Chocolate.hasTop_of_fringe valid fringe nonemptytheorem weighted_sequence {bottom : WeightedTree} {rest : Layer} {n : Nat} (program : Program 0 (.last .zero (.base bottom)) rest n) (base : Perfect 0 bottom) (nonnegative : bottom.Nonnegative) : rest.Valid 0 ∧ program.Realizes ∧ n + program.cost ≤ 21 * n ∧ ∀ subtree weight depth, SlotAt rest.decode subtree weight depth → 0 < weight → (depth : ℝ) ≤ 7 + 7 * Real.logb 2 (rest.decode.mass / weight) := … RotationBST.PopTart.Chocolate.weighted_sequence program base nonnegativetheorem operation_worst_case {stack rest : Layer} (valid : stack.Valid 0) (op : Operation 0 stack rest) : (1 + op.cost : ℝ) ≤ 7 + 14 * Real.logb 2 stack.decode.leafCount := … RotationBST.PopTart.Chocolate.operation_worst_case valid opOnly if you want to follow the deamortization proof into its data structures. The headline results of Chapter 30 and Chapter 31 state costs of online algorithms, and no pop-tart appears in their statements. The heavy-path representation of Section 38.1 to Section 38.4 uses the operations and bounds of this section, mostly through Section 37.2.8 and Section 37.2.9. The definitions are in Structures/PopTart/Chocolate/Definitions/Abstract.lean and Top/Definitions/Abstract.lean; the theorems are in Chocolate/Abstract.lean, Interruption/Abstract.lean and Top/Abstract.lean.
37.2.1 Overview
The pop-tarts of this book work on a PopTart.WeightedTree (Section 37.1). Its nodes are the separators that the pop-tart rearranges. Its leafs are fringe slots, each carrying a payload subtree and a real weight. The pop-tart never looks inside a payload. In the paper a slot is an empty leaf. In the heavy-path application it is an entire subtree hanging off the path, moved as a block. The mass of a weighted tree is the total weight of its slots, and a crumb of level is a WeightedTree.Perfect tree of height , with slots.
Like the Cherry pop-tart, the Chocolate pop-tart cuts the right spine into layers, numbered from the root. Layer of the paper consists of
- one to three regular nodes, each with an -crumb as its left child;
- then a next node, whose left child is layer and whose right child is the icing of layer .
The last layer has no next node: zero to three regular nodes, followed directly by its icing, and zero regular nodes only if the icing is a single -crumb.
layer i: r ─ r ─ r ─ next ─ icing ─ … ─ (bottom crumb)
│ │ │ │ │
c c c layer frozen
i+1 blocks
Here every horizontal edge is a right child, every vertical edge a left child, and c is an -crumb. The icing is a Vanilla pop-tart, a plain right spine, whose slots are frozen subtrees of the Chocolate pop-tart. Two invariants hold:
- Structural invariant: the layer shapes just described.
- Thick-icing invariant: the mass of layer is strictly smaller than the mass of the icing of layer .
A push adds a regular node to layer . If a layer now has four regular nodes, its last two crumbs are joined by a rotation into an -crumb, which is carried into layer , as in a binary counter. Before the carry, the algorithm compares masses. If layer with the carried crumb would weigh at least as much as the icing of layer , that layer is frozen instead: it is pushed, together with the next node above it, as one block onto the icing of layer , without further rebalancing, and layer becomes the last layer. Since the block weighs at least as much as the icing below it, the icing stays a well-behaved Vanilla pop-tart. A pop removes the first regular node of layer . If a layer runs out of regular nodes, it borrows an -crumb from the layer below and splits it into two; a last layer instead thaws the top block of its icing.
The depth bound comes from the thick-icing invariant. Layer weighs less than its icing, so less than half of layer : weight halves at every active layer. A frozen block sits in a Vanilla stack, so it is at depth , and the layer frozen inside it was, when frozen, light relative to the block. An induction on the layers gives the term.
37.2.2 Layers and icing
/-- The regular right-spine nodes of one Chocolate layer. -/inductive Regulars where | zero | one (a : WeightedTree) | two (a b : WeightedTree) | three (a b c : WeightedTree)def attach (tail : WeightedTree) : Regulars → WeightedTree | .zero => tail | .one a => .node a tail | .two a b => .node a (.node b tail) | .three a b c => .node a (.node b (.node c tail))def Valid (level : Nat) : Regulars → Prop | .zero => True | .one a => Perfect level a ∧ a.Nonnegative | .two a b => (Perfect level a ∧ a.Nonnegative) ∧ (Perfect level b ∧ b.Nonnegative) | .three a b c => (Perfect level a ∧ a.Nonnegative) ∧ (Perfect level b ∧ b.Nonnegative) ∧ (Perfect level c ∧ c.Nonnegative)Chocolate.Regulars holds the zero to three regular crumbs of one layer, top first. attach tail hangs them on a right spine above tail, each crumb as a left child. Valid level asks that each crumb be perfect of the given level, with nonnegative weights.
/-- Active layers retain a distinct next node with its child on the left andicing on the right. A last layer has no next node. -/inductive Layer where | last (regulars : Regulars) (icing : Icing) | next (regulars : Regulars) (child : Layer) (icing : Icing)/-- The icing keeps enough frozen metadata for subsequent thawing. `solid`is a frozen perfect crumb; `frozen` remembers the carried crumb and the oldnext layer as separate pieces of the unchanged frozen subtree. -/inductive Icing where | base (crumb : WeightedTree) | solid (crumb : WeightedTree) (tail : Icing) | frozen (carry : WeightedTree) (old : Layer) (tail : Icing)The two types are mutually inductive. A Chocolate.Layer is either last regulars icing, the last layer, or next regulars child icing, an active layer whose next node points to child, the next layer. An Chocolate.Icing is a Vanilla stack read from the top:
base crumbis the bottom of the icing. For the whole pop-tart, the base of the last icing of level is the bottom slot of the initial empty stack.solid crumb tailis a frozen -crumb on top oftail. It arises when the carried crumb alone already outweighs the icing, so there is no layer to freeze, only the crumb.frozen carry old tailis a frozen layer. It remembers the carried -crumbcarryand the old next layeroldseparately, rather than as one layer with four regular nodes. This is exactly the state before the carry, and thawing restores it.
def Layer.decode : Layer → WeightedTree | .last rs icing => rs.attach icing.decode | .next rs child icing => rs.attach (.node child.decode icing.decode)def Icing.decode : Icing → WeightedTree | .base crumb => crumb | .solid crumb tail => .node crumb tail.decode | .frozen carry old tail => .node (WeightedTree.node carry old.decode) tail.decodedecode is the physical tree as a weighted tree, so stack.decode.tree is the BST the pointer machine holds. A next node is .node child.decode icing.decode. A frozen block is the separator .node carry old.decode, and it hangs on the icing spine under the old next node of the frozen layer, as in the paper.
/-- Structural validity, nonnegative weights, and the strict thick-icinginvariant. An empty regular list is permitted only for a single base crumb. -/def Layer.Valid (level : Nat) : Layer → Prop | .last rs icing => rs.Valid level ∧ icing.Valid level ∧ (rs = .zero → ∃ crumb, icing = .base crumb) | .next rs child icing => rs.Valid level ∧ child.Valid (level + 1) ∧ icing.Valid level ∧ rs ≠ .zero ∧ child.decode.mass < icing.decode.mass/-- Frozen pending carries retain both the old strict mass inequality and thenew domination inequality. These are the certificates needed when thawing andwhen bounding paths inside a frozen next layer. -/def Icing.Valid (level : Nat) : Icing → Prop | .base crumb => Perfect level crumb ∧ crumb.Nonnegative | .solid crumb tail => Perfect (level + 1) crumb ∧ crumb.Nonnegative ∧ tail.Valid level ∧ tail.decode.mass ≤ crumb.mass | .frozen carry old tail => Perfect (level + 1) carry ∧ carry.Nonnegative ∧ old.Valid (level + 1) ∧ tail.Valid level ∧ old.decode.mass < tail.decode.mass ∧ tail.decode.mass ≤ carry.mass + old.decode.massValid level is the invariant at layer number level:
- A last layer has valid regulars and icing, and it has no regulars only if its icing is a single
basecrumb. - An active layer has one to three regulars (
rs ≠ .zero), a valid next layer at levellevel + 1, a valid icing, and the thick-icing inequalitychild.decode.mass < icing.decode.mass. - A
basecrumb is perfect of the layer’s level; asolidcrumb is perfect one level higher and dominates the rest of the icing,tail.decode.mass ≤ crumb.mass. - A
frozenblock keeps two certificates: the strict thick-icing inequality that held when the frozen layer was still active,old.decode.mass < tail.decode.mass, and the dominationtail.decode.mass ≤ carry.mass + old.decode.massthat made it freeze. The first is needed when the layer is thawed, and both bound depths inside the block.
All weights are nonnegative. The whole pop-tart is valid at level , where crumbs are single slots.
/-- The same icing viewed as a Vanilla stack of frozen physical subtrees.Each frozen block carries its actual aggregate fringe mass. `decode` retainsthe full interior; this view is used only for the outer access bound. -/def Icing.outer : Icing → WeightedTree | .base crumb => .leaf crumb.tree crumb.mass | .solid crumb tail => .node (.leaf crumb.tree crumb.mass) tail.outer | .frozen carry old tail => .node (.leaf (WeightedTree.node carry old.decode).tree (WeightedTree.node carry old.decode).mass) tail.outerouter views the icing as the Vanilla pop-tart of the paper: each frozen block, or solid crumb, becomes one slot of a PopTart.WeightedTree whose payload is the block’s whole physical subtree and whose weight is its total mass. The physical tree and the mass are unchanged (Section 37.2.6). The view exists only to apply the Vanilla depth bound to the icing.
/-- Number of next-layer transitions before reaching the last active layer.Frozen icing interiors do not participate in a recursive carry. -/def activeDepth : Layer → Nat | .last _ _ => 0 | .next _ child _ => child.activeDepth + 1activeDepth is the number of active layers below the top one: the length of the chain of next nodes. Layers frozen inside icing do not count, since a push or pop never recurses into them.
37.2.3 Push and pop
/-- A push compares only the mass of the proposed next layer with its icing.When that mass reaches the icing mass it freezes immediately, before furtherrebalancing of the saved next layer. -/noncomputable def push (new : WeightedTree) : Layer → Layer := by classical exact fun stack => match stack with | .last .zero icing => .last (.one new) icing | .last (.one a) icing => .last (.two new a) icing | .last (.two a b) icing => .last (.three new a b) icing | .last (.three a b c) icing => if (.node b c : WeightedTree).mass < icing.decode.mass then .next (.two new a) (.last .zero (.base (.node b c))) icing else .last (.two new a) (.solid (.node b c) icing) | .next .zero child icing => .next (.one new) child icing | .next (.one a) child icing => .next (.two new a) child icing | .next (.two a b) child icing => .next (.three new a b) child icing | .next (.three a b c) child icing => if (.node b c : WeightedTree).mass + child.decode.mass < icing.decode.mass then .next (.two new a) (child.push (.node b c)) icing else .last (.two new a) (.frozen (.node b c) child icing)/-- Exact pointer cost of the rebalancing trace, including returning to theouter root between the local rotation and recursive carry phases. -/noncomputable def pushCost : Layer → Nat := by classical exact fun stack => match stack with | .last (.three _ _ _) _ => 6 | .next (.three _ b c) child icing => if (.node b c : WeightedTree).mass + child.decode.mass < icing.decode.mass then 14 + child.pushCost else 8 | _ => 0push new follows the paper. With fewer than three regulars the new crumb is simply added. With three, the layer now has four, new a b c, and b and c are joined into the carried crumb .node b c:
- In a last layer, if
(.node b c).mass < icing.decode.mass, the layer becomes active with a new next layer.last .zero (.base (.node b c)), holding the carried crumb as its icing and nothing else. Otherwise the crumb is frozen assolidonto the icing. - In an active layer, if the next layer with the crumb would still be lighter than the icing,
(.node b c).mass + child.decode.mass < icing.decode.mass, the crumb is pushed recursively intochild. Otherwise the layer freezesfrozen (.node b c) child icing, and the layer becomeslast.
After an overflow, in every case, two regulars new a remain, so the layer’s potential drops to (Section 37.2.4). The comparison uses only masses that are already available. It is decided by classical, which is why push is noncomputable; nothing else is chosen classically.
pushCost is the exact number of pointer instructions of the rebalancing: for an overflow of a last layer, for a freeze, and plus the cost of the recursive push otherwise, which includes going back up to the root between the local rotations and the recursive carry. A push that does not overflow costs : placing the new separator above the old root is the caller’s step, not part of the pop-tart’s work.
/-- Refill an empty regular list from the icing. Frozen pending carries retaintheir saved next layer; only the carried perfect crumb is split. -/def thaw : Icing → Option Layer | .base crumb => some (.last .zero (.base crumb)) | .solid (.node b c) tail => some (.last (.two b c) tail) | .frozen (.node b c) old tail => some (.next (.two b c) old tail) | _ => nonedef thawCost : Icing → Nat | .base _ => 0 | .solid _ _ => 2 | .frozen _ _ _ => 4/-- Thaw cost inside the right child of an exposed popped separator. -/def popCost : Icing → Nat | .base _ => 0 | .solid _ _ => 4 | .frozen _ _ _ => 6thaw refills a last layer that has lost its regulars. A base crumb becomes the layer with no regulars. A solid crumb .node b c is split into two regulars b c. A frozen block is reactivated: its carry is split into two regulars, and the saved old layer becomes the next layer again. The physical change is one right rotation for solid and two for frozen, and thawCost (2 and 4) is the cost of thawing at the icing’s own root. popCost (4 and 6) is the cost of the same thaw seen from the exposed separator of a pop, two moves more. The paper’s thaw turns the frosted subtree into one regular node and a next node; the Lean version splits the carried crumb back into two regulars instead. No theorem of the reading interface mentions thawCost; the pop theorems use popCost.
def HasTop : Layer → Prop | .last .zero _ | .next .zero _ _ => False | _ => True/-- A next layer consisting of only its initial crumb is borrowed directly,without trying to pop a nonexistent separator from that layer. -/def baseCrumb : Layer → Option WeightedTree | .last .zero (.base crumb) => some crumb | _ => nonedef pop : Layer → Option (WeightedTree × Layer) | .last .zero _ => none | .last (.one a) icing => icing.thaw.map (fun rest => (a, rest)) | .last (.two a b) icing => some (a, .last (.one b) icing) | .last (.three a b c) icing => some (a, .last (.two b c) icing) | .next .zero _ _ => none | .next (.one a) child icing => match child.baseCrumb with | some (.node b c) => some (a, .last (.two b c) icing) | some (.leaf _ _) => none | none => match child.pop with | some (.node b c, rest) => some (a, .next (.two b c) rest icing) | _ => none | .next (.two a b) child icing => some (a, .next (.one b) child icing) | .next (.three a b c) child icing => some (a, .next (.two b c) child icing)def popCost : Layer → Nat | .last (.one _) icing => icing.popCost | .next (.one _) child _ => match child.baseCrumb with | some _ => 4 | none => 10 + child.popCost | _ => 0HasTop says that the top layer has a regular crumb, so that there is something to pop. pop removes the first regular crumb and repairs an emptied layer:
- a last layer with one regular thaws its icing;
- an active layer with one regular borrows from its next layer. If that layer is only a single base crumb (
baseCrumb), the crumb is split into two regulars and the layer becomes last. Otherwise the next layer is popped recursively and the returned -crumb is split into two regulars.
Like the other pop-tart operations, pop is totalized by none on shapes that the invariant excludes, such as a borrowed crumb that is a single slot. The pop theorem below shows that it succeeds on every valid stack with a top. popCost is unless the layer had a single regular, and plus the recursive cost for a recursive borrow.
37.2.4 Potential and operations
/-- Empty base layers and two-crumb layers need no repair credit. -/def potential : Regulars → Nat | .zero | .two _ _ => 0 | .one _ | .three _ _ _ => 20/-- Credits in frozen layers remain stored, so freezing does not discard thepotential that will be needed when those layers are thawed. -/def Layer.potential : Layer → Nat | .last rs icing => rs.potential + icing.potential | .next rs child icing => rs.potential + child.potential + icing.potentialdef Icing.potential : Icing → Nat | .base _ => 0 | .solid _ tail => tail.potential | .frozen _ old tail => old.potential + tail.potentialThe paper charges to every layer whose regular count is not . The library charges , enough for one rebalancing round trip in pointer instructions, to layers with one or three regulars, and nothing to layers with zero or two. A last layer with zero regulars is a single base crumb, which no operation repairs. Credits of frozen layers stay in the potential, through Icing.potential. When a frozen layer is thawed, its credit is therefore still there.
/-- Concrete push histories retain the chosen weights and every intermediatelayer state. The general mixed-operation interface appears below. -/inductive Pushes (level : Nat) : Layer → Layer → Nat → Type where | nil (stack : Layer) : Pushes level stack stack 0 | cons {stack rest : Layer} {n : Nat} (new : WeightedTree) (perfect : Perfect level new) (nonnegative : new.Nonnegative) (tail : Pushes level (stack.push new) rest n) : Pushes level stack rest (n + 1)noncomputable def cost {level : Nat} {stack rest : Layer} {n : Nat} : Pushes level stack rest n → Nat | .nil _ => 0 | .cons _ _ _ tail => stack.pushCost + tail.cost/-- Every rebalancing boundary is realized; caller-supplied separators areaccounted for separately, as in the other pop-tart structures. -/inductive Realizes {level : Nat} : {stack rest : Layer} → {n : Nat} → Pushes level stack rest n → Prop where | nil (stack : Layer) : Realizes (.nil stack) | cons {stack rest : Layer} {n : Nat} {new : WeightedTree} {perfect : Perfect level new} {nonnegative : new.Nonnegative} {tail : Pushes level (stack.push new) rest n} (first : Pointer.Trace ⟨.node new.tree stack.decode.tree, []⟩ ⟨(stack.push new).decode.tree, []⟩ stack.pushCost) (later : Realizes tail) : Realizes (.cons new perfect nonnegative tail)Chocolate.Pushes level stack rest n is a history of n pushes from stack to rest, each of a perfect, nonnegative crumb of the given level, with every intermediate state determined by push. Its cost is the sum of the push costs. Realizes asks that each push be witnessed by a Pointer.Trace (Chapter 7) from the tree with the new separator on top to the rebalanced tree, at exactly its pushCost, with the cursor at the root at both ends. The theorems prove Realizes, rather than assuming it.
/-- Legal operations preserve the caller-supplied/exposed-separator boundary. -/inductive Operation (level : Nat) : Layer → Layer → Type where | push (stack : Layer) (new : WeightedTree) (perfect : Perfect level new) (nonnegative : new.Nonnegative) : Operation level stack (stack.push new) | pop (stack rest : Layer) (crumb : WeightedTree) (popped : stack.pop = some (crumb, rest)) : Operation level stack restnoncomputable def cost {level : Nat} {stack rest : Layer} : Operation level stack rest → Nat | .push _ _ _ _ => stack.pushCost | .pop _ _ _ _ => stack.popCostdef input {level : Nat} {stack rest : Layer} : Operation level stack rest → Tree | .push _ new _ _ => .node new.tree stack.decode.tree | .pop _ _ _ _ => stack.decode.treedef output {level : Nat} {stack rest : Layer} : Operation level stack rest → Tree | .push _ _ _ _ => rest.decode.tree | .pop _ _ crumb _ => .node crumb.tree rest.decode.treeAn Chocolate.Operation is one push, of a perfect nonnegative crumb, or one pop, with a proof that pop succeeded. input and output are the trees at the operation’s boundaries. A push starts from .node new.tree stack.decode.tree, the caller’s separator above the old stack, and ends at the new stack. A pop starts at the stack and ends at .node crumb.tree rest.decode.tree, the popped crumb exposed as the left child of a root separator that the caller then takes away.
inductive Program (level : Nat) : Layer → Layer → Nat → Type where | nil (stack : Layer) : Program level stack stack 0 | cons {stack middle rest : Layer} {n : Nat} (op : Operation level stack middle) (tail : Program level middle rest n) : Program level stack rest (n + 1)noncomputable def cost {level : Nat} {stack rest : Layer} {n : Nat} : Program level stack rest n → Nat | .nil _ => 0 | .cons op tail => op.cost + tail.costinductive Realizes {level : Nat} : {stack rest : Layer} → {n : Nat} → Program level stack rest n → Prop where | nil (stack : Layer) : Realizes (.nil stack) | cons {stack middle rest : Layer} {n : Nat} {op : Operation level stack middle} {tail : Program level middle rest n} (first : Pointer.Trace ⟨op.input, []⟩ ⟨op.output, []⟩ op.cost) (later : Realizes tail) : Realizes (.cons op tail)A Chocolate.Program is a sequence of operations, chained by their states; Realizes asks for a trace of each operation, from its input to its output at its cost.
37.2.5 Amortized cost
The single-operation theorems state everything about one push or pop.
Theorem (push). Let stack be valid at some level, and push a perfect, nonnegative crumb of the same level. Then the result is valid, its fringe is the new crumb’s fringe followed by the old one, and its mass is the sum. Below any context cs, a trace of exactly pushCost instructions turns the tree with the new separator on top into the result. Finally, pushCost .
theorem push {stack : Layer} {level : Nat} (valid : stack.Valid level) {new : WeightedTree} (perfect : Perfect level new) (nonnegative : new.Nonnegative) (cs : List Pointer.Frame) : (stack.push new).Valid level ∧ (stack.push new).decode.fringe = new.fringe ++ stack.decode.fringe ∧ (stack.push new).decode.mass = new.mass + stack.decode.mass ∧ Pointer.Trace ⟨.node new.tree stack.decode.tree, cs⟩ ⟨(stack.push new).decode.tree, cs⟩ stack.pushCost ∧ stack.pushCost + (stack.push new).potential ≤ 20 + stack.potential := RotationBST.PopTart.Chocolate.push valid perfect nonnegative csTheorem (pop). Let stack be valid with a top. Then pop succeeds, with a perfect, nonnegative crumb of the stack’s level and a valid rest. The stack’s fringe is the crumb’s followed by the rest’s, its mass is their sum, and below any context cs a trace of exactly popCost instructions exposes the crumb. Finally, popCost .
theorem pop {stack : Layer} {level : Nat} (valid : stack.Valid level) (top : stack.HasTop) (cs : List Pointer.Frame) : ∃ crumb rest, stack.pop = some (crumb, rest) ∧ Perfect level crumb ∧ crumb.Nonnegative ∧ rest.Valid level ∧ stack.decode.fringe = crumb.fringe ++ rest.decode.fringe ∧ stack.decode.mass = crumb.mass + rest.decode.mass ∧ Pointer.Trace ⟨stack.decode.tree, cs⟩ ⟨.node crumb.tree rest.decode.tree, cs⟩ stack.popCost ∧ stack.popCost + rest.potential ≤ 20 + stack.potential := RotationBST.PopTart.Chocolate.pop valid top csThe context cs in both theorems is what makes the operations local: the trace starts and ends with the cursor at the stack’s root, wherever that root is in a larger tree, and the ancestors above it are the same at both ends. The heavy-path structure runs pop-tart operations deep inside the simulated tree this way.
theorem push_trace (stack : Layer) (new : WeightedTree) : Pointer.Trace ⟨.node new.tree stack.decode.tree, []⟩ ⟨(stack.push new).decode.tree, []⟩ stack.pushCost := RotationBST.PopTart.Chocolate.push_trace stack newtheorem pop_trace {stack rest : Layer} {crumb : WeightedTree} (popped : stack.pop = some (crumb, rest)) : Pointer.Trace ⟨stack.decode.tree, []⟩ ⟨.node crumb.tree rest.decode.tree, []⟩ stack.popCost := RotationBST.PopTart.Chocolate.pop_trace poppedtheorem layer_nonnegative {stack : Layer} {level : Nat} (valid : stack.Valid level) : stack.decode.Nonnegative := RotationBST.PopTart.Chocolate.layer_nonnegative validtheorem icing_nonnegative {icing : Icing} {level : Nat} (valid : icing.Valid level) : icing.decode.Nonnegative := RotationBST.PopTart.Chocolate.icing_nonnegative validThe trace exists for every push, valid or not (push_trace), and for every successful pop (pop_trace): the rebalancing rotations are legal on any shape that push or pop accepts. Validity is needed only for the fringe, mass and potential parts. A valid layer or icing has nonnegative weights.
theorem operation_valid {level : Nat} {stack rest : Layer} (valid : stack.Valid level) (op : Operation level stack rest) : rest.Valid level := RotationBST.PopTart.Chocolate.operation_valid valid optheorem operation_trace {level : Nat} {stack rest : Layer} (op : Operation level stack rest) (cs : List Pointer.Frame) : Pointer.Trace ⟨op.input, cs⟩ ⟨op.output, cs⟩ op.cost := RotationBST.PopTart.Chocolate.operation_trace op cstheorem operation_amortized {level : Nat} {stack rest : Layer} (op : Operation level stack rest) : op.cost + rest.potential ≤ 20 + stack.potential := RotationBST.PopTart.Chocolate.operation_amortized opThe same three facts for an Chocolate.Operation: it preserves validity, it has a trace below any context, and its amortized cost is at most .
Theorem (sequences). A program of operations from a valid state ends in a valid state, is realized by traces, and costs at most . From a single base crumb, where , the cost plus one unit per operation is at most .
theorem push_sequence {level : Nat} {stack rest : Layer} {n : Nat} (program : Pushes level stack rest n) (valid : stack.Valid level) : rest.Valid level ∧ program.Realizes ∧ program.cost + rest.potential ≤ 20 * n + stack.potential := RotationBST.PopTart.Chocolate.push_sequence program validtheorem empty_push_sequence {level : Nat} {bottom : WeightedTree} {rest : Layer} {n : Nat} (program : Pushes level (.last .zero (.base bottom)) rest n) (base : Perfect level bottom) (nonnegative : bottom.Nonnegative) : rest.Valid level ∧ program.Realizes ∧ n + program.cost ≤ 21 * n := RotationBST.PopTart.Chocolate.empty_push_sequence program base nonnegativetheorem sequence {level : Nat} {stack rest : Layer} {n : Nat} (program : Program level stack rest n) (valid : stack.Valid level) : rest.Valid level ∧ program.Realizes ∧ program.cost + rest.potential ≤ 20 * n + stack.potential := RotationBST.PopTart.Chocolate.sequence program validtheorem empty_sequence {level : Nat} {bottom : WeightedTree} {rest : Layer} {n : Nat} (program : Program level (.last .zero (.base bottom)) rest n) (base : Perfect level bottom) (nonnegative : bottom.Nonnegative) : rest.Valid level ∧ program.Realizes ∧ n + program.cost ≤ 21 * n := RotationBST.PopTart.Chocolate.empty_sequence program base nonnegativeThe push_ versions are the special case of push-only histories. The added on the left of empty_sequence is the caller’s unit per operation; the pop-tart’s own cost is at most .
theorem layer_potential_bound (stack : Layer) : stack.potential + 20 ≤ 20 * stack.decode.leafCount := RotationBST.PopTart.Chocolate.layer_potential_bound stacktheorem icing_potential_bound (icing : Icing) : icing.potential + 20 ≤ 20 * icing.decode.leafCount := RotationBST.PopTart.Chocolate.icing_potential_bound icingThe potential is at most for a stack with fringe slots. So the credit stored in a pop-tart is linear in its size, and a structure that builds or discards pop-tarts can account for their potential explicitly.
37.2.6 Depth
37.2.6.1 The icing is a Vanilla pop-tart
theorem outer_tree (icing : Icing) : icing.outer.tree = icing.decode.tree := RotationBST.PopTart.Chocolate.outer_tree icingtheorem outer_mass (icing : Icing) : icing.outer.mass = icing.decode.mass := RotationBST.PopTart.Chocolate.outer_mass icingtheorem icing_vanilla {icing : Icing} {level : Nat} (valid : icing.Valid level) : Vanilla.Valid icing.outer := RotationBST.PopTart.Chocolate.icing_vanilla validThe Vanilla view of an icing has the same physical tree and the same mass, and it satisfies Vanilla.Valid: each block weighs at least the rest of the icing below it. This is where the two domination inequalities of Icing.Valid are used.
theorem icing_depth {icing : Icing} {level : Nat} (valid : icing.Valid level) {subtree : Tree} {weight : ℝ} {depth : Nat} (slot : SlotAt icing.outer subtree weight depth) (positive : 0 < weight) : (depth : ℝ) ≤ 1 + Real.logb 2 (icing.decode.mass / weight) := RotationBST.PopTart.Chocolate.icing_depth valid slot positivetheorem icing_access {icing : Icing} {level : Nat} (valid : icing.Valid level) {subtree : Tree} {weight : ℝ} {depth : Nat} (slot : SlotAt icing.outer subtree weight depth) (positive : 0 < weight) (nonempty : subtree ≠ .nil) : ∃ cs : List Pointer.Frame, cs.length = depth ∧ Pointer.plug cs subtree = icing.decode.tree ∧ Pointer.Trace ⟨icing.decode.tree, []⟩ ⟨subtree, cs⟩ depth ∧ (depth : ℝ) ≤ 1 + Real.logb 2 (icing.decode.mass / weight) := RotationBST.PopTart.Chocolate.icing_access valid slot positive nonemptyHence a block of weight is at depth at most in the icing, being the icing’s mass, as in Lemma 1 of the paper. icing_access also produces the pointer path: a trace of exactly that many moves from the icing’s root to the block, with the matching context frames. The depth depth of a WeightedTree.SlotAt counts edges from the root of the weighted tree to the slot.
37.2.6.2 Every slot of a layer
Theorem (layer depth). In a stack valid at level , every fringe slot of weight is at depth at most , where is the stack’s mass. In an icing valid at level , every slot of the full physical icing, including the slots inside frozen blocks, is at depth at most .
theorem layer_depth {stack : Layer} {level : Nat} (valid : stack.Valid level) {subtree : Tree} {weight : ℝ} {depth : Nat} (slot : SlotAt stack.decode subtree weight depth) (positive : 0 < weight) : (depth : ℝ) ≤ (level : ℝ) + 7 + 7 * Real.logb 2 (stack.decode.mass / weight) := RotationBST.PopTart.Chocolate.layer_depth valid slot positivetheorem icing_leaf_depth {icing : Icing} {level : Nat} (valid : icing.Valid level) {subtree : Tree} {weight : ℝ} {depth : Nat} (slot : SlotAt icing.decode subtree weight depth) (positive : 0 < weight) : (depth : ℝ) ≤ (level : ℝ) + 3 + 7 * Real.logb 2 (icing.decode.mass / weight) := RotationBST.PopTart.Chocolate.icing_leaf_depth valid slot positiveThe two are proved together, by induction on the mutual structure, following the paper’s three cases: slots in regular crumbs, slots in the next layer, and slots in the icing, where a frozen layer’s old certificate old.decode.mass < tail.decode.mass plays the role of the paper’s “otherwise the layer would have been frosted earlier”. The paper’s hypothesis is ; the Lean induction carries .
Theorem (weighted depth). In a stack valid at level , every slot of weight is at depth at most , and a pointer trace of exactly that length reaches it from the root.
theorem weighted_depth {stack : Layer} (valid : stack.Valid 0) {subtree : Tree} {weight : ℝ} {depth : Nat} (slot : SlotAt stack.decode subtree weight depth) (positive : 0 < weight) : (depth : ℝ) ≤ 7 + 7 * Real.logb 2 (stack.decode.mass / weight) := RotationBST.PopTart.Chocolate.weighted_depth valid slot positivetheorem weighted_access {stack : Layer} (valid : stack.Valid 0) {subtree : Tree} {weight : ℝ} {depth : Nat} (slot : SlotAt stack.decode subtree weight depth) (positive : 0 < weight) (nonempty : subtree ≠ .nil) : ∃ cs : List Pointer.Frame, cs.length = depth ∧ Pointer.plug cs subtree = stack.decode.tree ∧ Pointer.Trace ⟨stack.decode.tree, []⟩ ⟨subtree, cs⟩ depth ∧ (depth : ℝ) ≤ 7 + 7 * Real.logb 2 (stack.decode.mass / weight) := RotationBST.PopTart.Chocolate.weighted_access valid slot positive nonemptytheorem weighted_push_sequence {bottom : WeightedTree} {rest : Layer} {n : Nat} (program : Pushes 0 (.last .zero (.base bottom)) rest n) (base : Perfect 0 bottom) (nonnegative : bottom.Nonnegative) : rest.Valid 0 ∧ program.Realizes ∧ n + program.cost ≤ 21 * n ∧ ∀ subtree weight depth, SlotAt rest.decode subtree weight depth → 0 < weight → (depth : ℝ) ≤ 7 + 7 * Real.logb 2 (rest.decode.mass / weight) := RotationBST.PopTart.Chocolate.weighted_push_sequence program base nonnegativetheorem weighted_sequence {bottom : WeightedTree} {rest : Layer} {n : Nat} (program : Program 0 (.last .zero (.base bottom)) rest n) (base : Perfect 0 bottom) (nonnegative : bottom.Nonnegative) : rest.Valid 0 ∧ program.Realizes ∧ n + program.cost ≤ 21 * n ∧ ∀ subtree weight depth, SlotAt rest.decode subtree weight depth → 0 < weight → (depth : ℝ) ≤ 7 + 7 * Real.logb 2 (rest.decode.mass / weight) := RotationBST.PopTart.Chocolate.weighted_sequence program base nonnegativeweighted_sequence is the combination used in the headline: a program of operations at level from a single base slot ends in a valid state, is realized, costs at most , and leaves every slot of positive weight at depth . Every prefix of a program is a program, so the depth bound holds after every operation.
- Weights on slots only. The paper gives every internal node weight . Here
PopTart.WeightedTreeweighs only fringe slots, and separators count nothing. The heavy-path representation adds the weight of each path node to the weight of the slot beside it. - Zero weights. Weights must be nonnegative. A slot of weight is allowed, and the heavy-path stacks start from a bottom slot of weight , but the depth bounds assume
0 < weight: Mathlib’s of would be . - Constants. The depth bound is rather than the paper’s . Amortized cost is pointer instructions per operation. The constants are not optimized.
- Cost model. Costs are exact instruction counts of a single-cursor
Pointer.Tracefrom root to root, not bounds. The caller’s separator step is not included.
37.2.7 Worst-case cost
The crumbs of layer have slots each, so the number of active layers is logarithmic in the number of slots, whatever the weights.
theorem icing_population {icing : Icing} {level : Nat} (valid : icing.Valid level) : 2 ^ level ≤ icing.decode.leafCount := RotationBST.PopTart.Chocolate.icing_population validtheorem active_population {stack : Layer} {level : Nat} (valid : stack.Valid level) : 2 ^ (level + stack.activeDepth) ≤ stack.decode.leafCount := RotationBST.PopTart.Chocolate.active_population validtheorem push_cost_active_depth (stack : Layer) : stack.pushCost ≤ 14 * stack.activeDepth + 6 := RotationBST.PopTart.Chocolate.push_cost_active_depth stackA valid icing of level has at least slots, since its base is an -crumb. A valid stack at level with activeDepth has at least slots. And a push costs at most , since it recurses at most once per active layer.
Theorem (worst case). On a stack valid at level with fringe slots, a push costs and a pop .
theorem push_worst_case {stack : Layer} (valid : stack.Valid 0) : (1 + stack.pushCost : ℝ) ≤ 7 + 14 * Real.logb 2 (stack.decode.leafCount) := RotationBST.PopTart.Chocolate.push_worst_case validtheorem pop_worst_case {stack : Layer} (valid : stack.Valid 0) : (1 + stack.popCost : ℝ) ≤ 7 + 10 * Real.logb 2 (stack.decode.leafCount) := RotationBST.PopTart.Chocolate.pop_worst_case validtheorem operation_worst_case {stack rest : Layer} (valid : stack.Valid 0) (op : Operation 0 stack rest) : (1 + op.cost : ℝ) ≤ 7 + 14 * Real.logb 2 stack.decode.leafCount := RotationBST.PopTart.Chocolate.operation_worst_case valid op is WeightedTree.leafCount of the stack before the operation, which counts slots, not nodes: at level it is one more than the number of separators. The bound for a pop is stated without assuming the pop succeeds, since popCost is defined anyway. operation_worst_case takes the larger constant for both kinds.
theorem uniform_height {stack : Layer} (valid : stack.Valid 0) (unitWeights : ∀ subtree weight depth, SlotAt stack.decode subtree weight depth → weight = 1) : (stack.decode.height : ℝ) ≤ 7 + 7 * Real.logb 2 (stack.decode.leafCount) := RotationBST.PopTart.Chocolate.uniform_height valid unitWeightstheorem uniform_key_depth {stack : Layer} (valid : stack.Valid 0) (unitWeights : ∀ subtree weight depth, SlotAt stack.decode subtree weight depth → weight = 1) (empty : stack.decode.EmptyFringe) (x : Nat) (inside : x < stack.decode.tree.size) : (Tree.depth stack.decode.tree x : ℝ) < 7 + 7 * Real.logb 2 (((stack.decode.tree.size + 1 : Nat) : ℝ)) := RotationBST.PopTart.Chocolate.uniform_key_depth valid unitWeights empty x insideWith unit weights the depth bound becomes a height bound, . If moreover every slot is an empty leaf, the pop-tart is an ordinary BST on its separators, and every key has depth less than . This is the unweighted case of the paper, the Cherry guarantee of Section 37.1 recovered by the weighted structure.
37.2.8 Interrupted operations
The deamortized algorithm of Chapter 30 cannot afford to run a whole push or pop at once whenever the simulation needs it. Its scheduler performs a bounded number of instructions per access, so a pop-tart operation is often stopped half-way, the tree is searched in that intermediate state, and the operation resumes later. Searches in the intermediate tree must still be fast. The theorems of this section make this precise. Each turns the trace of an operation into a concrete list of Multifinger.Opcodes, replayed with Multifinger.replay on one cursor (Chapter 36), and bounds the depth of every key at every cut point.
Each theorem has the same shape. There is a list ops of exactly cost opcodes whose replay performs the operation, and for every cut the prefix ops.take cut replays to some intermediate configuration middle, from which the suffix ops.drop cut replays to the final one. The theorems differ in the depth bound for middle.tree, the whole tree in the intermediate state. Here Tree.depth is the depth of a key, given by its inorder rank, in the whole tree.
Theorem (additive interruption). On a stack valid at level with slots, below any context, the depth of every key at any cut point exceeds its depth before the operation by at most for a push and for a pop.
theorem push_prefix {stack : Layer} (valid : stack.Valid 0) (new : WeightedTree) (cs : List Pointer.Frame) : ∃ ops : List Multifinger.Opcode, ops.length = stack.pushCost ∧ Multifinger.replay ops ⟨.node new.tree stack.decode.tree, cs⟩ = some ⟨(stack.push new).decode.tree, cs⟩ ∧ ∀ cut, ∃ middle, Multifinger.replay (ops.take cut) ⟨.node new.tree stack.decode.tree, cs⟩ = some middle ∧ Multifinger.replay (ops.drop cut) middle = some ⟨(stack.push new).decode.tree, cs⟩ ∧ ∀ x, (Tree.depth middle.tree x : ℝ) ≤ Tree.depth (Pointer.plug cs (.node new.tree stack.decode.tree)) x + 6 + 14 * Real.logb 2 (stack.decode.leafCount) := RotationBST.PopTart.Chocolate.Interruption.push_prefix valid new csModels.Rotation, Models.Pointer.Machine, Models.Pointer.Lazy.Skeleton, Models.Multifinger.Program.Definitions, Models.Multifinger.Program.Replay.Definitions, Bounds.StaticTree.Definitions, Structures.PopTart.Definitions, Structures.PopTart.Chocolate.Definitions, Structures.PopTart.Located.Definitions, Models.Pointer.Invariant.Definitionstheorem pop_prefix {stack rest : Layer} {crumb : WeightedTree} (valid : stack.Valid 0) (popped : stack.pop = some (crumb, rest)) (cs : List Pointer.Frame) : ∃ ops : List Multifinger.Opcode, ops.length = stack.popCost ∧ Multifinger.replay ops ⟨stack.decode.tree, cs⟩ = some ⟨.node crumb.tree rest.decode.tree, cs⟩ ∧ ∀ cut, ∃ middle, Multifinger.replay (ops.take cut) ⟨stack.decode.tree, cs⟩ = some middle ∧ Multifinger.replay (ops.drop cut) middle = some ⟨.node crumb.tree rest.decode.tree, cs⟩ ∧ ∀ x, (Tree.depth middle.tree x : ℝ) ≤ Tree.depth (Pointer.plug cs stack.decode.tree) x + 6 + 10 * Real.logb 2 (stack.decode.leafCount) := RotationBST.PopTart.Chocolate.Interruption.pop_prefix valid popped csModels.Rotation, Models.Pointer.Machine, Models.Pointer.Lazy.Skeleton, Models.Multifinger.Program.Definitions, Models.Multifinger.Program.Replay.Definitions, Bounds.StaticTree.Definitions, Structures.PopTart.Definitions, Structures.PopTart.Chocolate.Definitions, Structures.PopTart.Located.Definitions, Models.Pointer.Invariant.Definitionspush_prefix makes no assumption on new: the rebalancing of push does not depend on the pushed crumb’s shape. The quantifier ∀ x ranges over all naturals; for ranks outside the tree, Tree.depth is totalized to the length of the right spine, and the bound holds for those too.
Theorem (multiplicative interruption). Without any validity assumption, at every cut point every key has depth at most , where is its depth before the operation.
theorem push_depth_prefix (stack : Layer) (new : WeightedTree) (cs : List Pointer.Frame) : ∃ ops : List Multifinger.Opcode, ops.length = stack.pushCost ∧ Multifinger.replay ops ⟨.node new.tree stack.decode.tree, cs⟩ = some ⟨(stack.push new).decode.tree, cs⟩ ∧ ∀ cut, ∃ middle, Multifinger.replay (ops.take cut) ⟨.node new.tree stack.decode.tree, cs⟩ = some middle ∧ Multifinger.replay (ops.drop cut) middle = some ⟨(stack.push new).decode.tree, cs⟩ ∧ ∀ x, Tree.depth middle.tree x ≤ 4 * Tree.depth (Pointer.plug cs (.node new.tree stack.decode.tree)) x + 8 := RotationBST.PopTart.Chocolate.Interruption.push_depth_prefix stack new csModels.Rotation, Models.Pointer.Machine, Models.Pointer.Lazy.Skeleton, Models.Multifinger.Program.Definitions, Models.Multifinger.Program.Replay.Definitions, Bounds.StaticTree.Definitions, Structures.PopTart.Definitions, Structures.PopTart.Chocolate.Definitions, Structures.PopTart.Located.Definitions, Models.Pointer.Invariant.Definitionstheorem pop_depth_prefix {stack rest : Layer} {crumb : WeightedTree} (popped : stack.pop = some (crumb, rest)) (cs : List Pointer.Frame) : ∃ ops : List Multifinger.Opcode, ops.length = stack.popCost ∧ Multifinger.replay ops ⟨stack.decode.tree, cs⟩ = some ⟨.node crumb.tree rest.decode.tree, cs⟩ ∧ ∀ cut, ∃ middle, Multifinger.replay (ops.take cut) ⟨stack.decode.tree, cs⟩ = some middle ∧ Multifinger.replay (ops.drop cut) middle = some ⟨.node crumb.tree rest.decode.tree, cs⟩ ∧ ∀ x, Tree.depth middle.tree x ≤ 4 * Tree.depth (Pointer.plug cs stack.decode.tree) x + 8 := RotationBST.PopTart.Chocolate.Interruption.pop_depth_prefix popped csModels.Rotation, Models.Pointer.Machine, Models.Pointer.Lazy.Skeleton, Models.Multifinger.Program.Definitions, Models.Multifinger.Program.Replay.Definitions, Bounds.StaticTree.Definitions, Structures.PopTart.Definitions, Structures.PopTart.Chocolate.Definitions, Structures.PopTart.Located.Definitions, Models.Pointer.Invariant.Definitionstheorem push_within (stack : Layer) (new : WeightedTree) : Pointer.TraceWithin (fun t => t.size = (Tree.node new.tree stack.decode.tree).size ∧ ∀ x, Tree.depth t x ≤ 4 * Tree.depth (.node new.tree stack.decode.tree) x + 8) ⟨.node new.tree stack.decode.tree, []⟩ ⟨(stack.push new).decode.tree, []⟩ stack.pushCost := RotationBST.PopTart.Chocolate.Interruption.push_within stack newModels.Rotation, Models.Pointer.Machine, Models.Pointer.Lazy.Skeleton, Models.Multifinger.Program.Definitions, Models.Multifinger.Program.Replay.Definitions, Bounds.StaticTree.Definitions, Structures.PopTart.Definitions, Structures.PopTart.Chocolate.Definitions, Structures.PopTart.Located.Definitions, Models.Pointer.Invariant.Definitionstheorem pop_within {stack rest : Layer} {crumb : WeightedTree} (popped : stack.pop = some (crumb, rest)) : Pointer.TraceWithin (fun t => t.size = stack.decode.tree.size ∧ ∀ x, Tree.depth t x ≤ 4 * Tree.depth stack.decode.tree x + 8) ⟨stack.decode.tree, []⟩ ⟨.node crumb.tree rest.decode.tree, []⟩ stack.popCost := RotationBST.PopTart.Chocolate.Interruption.pop_within poppedModels.Rotation, Models.Pointer.Machine, Models.Pointer.Lazy.Skeleton, Models.Multifinger.Program.Definitions, Models.Multifinger.Program.Replay.Definitions, Bounds.StaticTree.Definitions, Structures.PopTart.Definitions, Structures.PopTart.Chocolate.Definitions, Structures.PopTart.Located.Definitions, Models.Pointer.Invariant.DefinitionsThe within forms state the same invariant, together with preservation of the size, as a Pointer.TraceWithin: a trace whose every intermediate tree satisfies a predicate. They are stated at the stack’s root (empty context), in the form that Results.replaceTop_within lifts below a top slot (Section 37.2.9). The heavy-path interruption proofs use these.
Theorem (payload depth). At level , let a payload subtree occupy a slot of weight at inorder offset offset of the stack, and let be a key of the payload at depth inside it. At every cut point of a pop, the key is at depth at most . For a push the key’s rank is shifted by the new crumb and its separator, and the bound is .
theorem pop_payload_prefix {stack rest : Layer} {crumb : WeightedTree} (valid : stack.Valid 0) (popped : stack.pop = some (crumb, rest)) : ∃ ops : List Multifinger.Opcode, ops.length = stack.popCost ∧ Multifinger.replay ops ⟨stack.decode.tree, []⟩ = some ⟨.node crumb.tree rest.decode.tree, []⟩ ∧ ∀ cut, ∃ middle, Multifinger.replay (ops.take cut) ⟨stack.decode.tree, []⟩ = some middle ∧ Multifinger.replay (ops.drop cut) middle = some ⟨.node crumb.tree rest.decode.tree, []⟩ ∧ ∀ subtree weight offset slotDepth, LocatedSlot stack.decode subtree weight offset slotDepth → 0 < weight → ∀ x, x < subtree.size → (Tree.depth middle.tree (offset + x) : ℝ) ≤ Tree.depth subtree x + 13 + 7 * Real.logb 2 (stack.decode.mass / weight) + 10 * Real.logb 2 (stack.decode.leafCount) := RotationBST.PopTart.Chocolate.Interruption.pop_payload_prefix valid poppedModels.Rotation, Models.Pointer.Machine, Models.Pointer.Lazy.Skeleton, Models.Multifinger.Program.Definitions, Models.Multifinger.Program.Replay.Definitions, Bounds.StaticTree.Definitions, Structures.PopTart.Definitions, Structures.PopTart.Chocolate.Definitions, Structures.PopTart.Located.Definitions, Models.Pointer.Invariant.Definitionstheorem push_payload_prefix {stack : Layer} (valid : stack.Valid 0) (new : WeightedTree) : ∃ ops : List Multifinger.Opcode, ops.length = stack.pushCost ∧ Multifinger.replay ops ⟨.node new.tree stack.decode.tree, []⟩ = some ⟨(stack.push new).decode.tree, []⟩ ∧ ∀ cut, ∃ middle, Multifinger.replay (ops.take cut) ⟨.node new.tree stack.decode.tree, []⟩ = some middle ∧ Multifinger.replay (ops.drop cut) middle = some ⟨(stack.push new).decode.tree, []⟩ ∧ ∀ subtree weight offset slotDepth, LocatedSlot stack.decode subtree weight offset slotDepth → 0 < weight → ∀ x, x < subtree.size → (Tree.depth middle.tree (offset + new.tree.size + 1 + x) : ℝ) ≤ Tree.depth subtree x + 14 + 7 * Real.logb 2 (stack.decode.mass / weight) + 14 * Real.logb 2 (stack.decode.leafCount) := RotationBST.PopTart.Chocolate.Interruption.push_payload_prefix valid newModels.Rotation, Models.Pointer.Machine, Models.Pointer.Lazy.Skeleton, Models.Multifinger.Program.Definitions, Models.Multifinger.Program.Replay.Definitions, Bounds.StaticTree.Definitions, Structures.PopTart.Definitions, Structures.PopTart.Chocolate.Definitions, Structures.PopTart.Located.Definitions, Models.Pointer.Invariant.DefinitionsThe slot is described by a WeightedTree.LocatedSlot (Section 37.1), which records both its outer depth and the inorder rank of its first key. The traces here start at the stack’s root, with no outer context.
Theorem (weighted interruption). With the same data, at every cut point the key is at depth at most for a pop, and for a push.
theorem pop_weighted_prefix {stack rest : Layer} {crumb : WeightedTree} (valid : stack.Valid 0) (popped : stack.pop = some (crumb, rest)) : ∃ ops : List Multifinger.Opcode, ops.length = stack.popCost ∧ Multifinger.replay ops ⟨stack.decode.tree, []⟩ = some ⟨.node crumb.tree rest.decode.tree, []⟩ ∧ ∀ cut, ∃ middle, Multifinger.replay (ops.take cut) ⟨stack.decode.tree, []⟩ = some middle ∧ Multifinger.replay (ops.drop cut) middle = some ⟨.node crumb.tree rest.decode.tree, []⟩ ∧ ∀ subtree weight offset slotDepth, LocatedSlot stack.decode subtree weight offset slotDepth → 0 < weight → ∀ x, x < subtree.size → (Tree.depth middle.tree (offset + x) : ℝ) ≤ 4 * Tree.depth subtree x + 36 + 28 * Real.logb 2 (stack.decode.mass / weight) := RotationBST.PopTart.Chocolate.Interruption.pop_weighted_prefix valid poppedModels.Rotation, Models.Pointer.Machine, Models.Pointer.Lazy.Skeleton, Models.Multifinger.Program.Definitions, Models.Multifinger.Program.Replay.Definitions, Bounds.StaticTree.Definitions, Structures.PopTart.Definitions, Structures.PopTart.Chocolate.Definitions, Structures.PopTart.Located.Definitions, Models.Pointer.Invariant.Definitionstheorem push_weighted_prefix {stack : Layer} (valid : stack.Valid 0) (new : WeightedTree) : ∃ ops : List Multifinger.Opcode, ops.length = stack.pushCost ∧ Multifinger.replay ops ⟨.node new.tree stack.decode.tree, []⟩ = some ⟨(stack.push new).decode.tree, []⟩ ∧ ∀ cut, ∃ middle, Multifinger.replay (ops.take cut) ⟨.node new.tree stack.decode.tree, []⟩ = some middle ∧ Multifinger.replay (ops.drop cut) middle = some ⟨(stack.push new).decode.tree, []⟩ ∧ ∀ subtree weight offset slotDepth, LocatedSlot stack.decode subtree weight offset slotDepth → 0 < weight → ∀ x, x < subtree.size → (Tree.depth middle.tree (offset + new.tree.size + 1 + x) : ℝ) ≤ 4 * Tree.depth subtree x + 40 + 28 * Real.logb 2 (stack.decode.mass / weight) := RotationBST.PopTart.Chocolate.Interruption.push_weighted_prefix valid newModels.Rotation, Models.Pointer.Machine, Models.Pointer.Lazy.Skeleton, Models.Multifinger.Program.Definitions, Models.Multifinger.Program.Replay.Definitions, Bounds.StaticTree.Definitions, Structures.PopTart.Definitions, Structures.PopTart.Chocolate.Definitions, Structures.PopTart.Located.Definitions, Models.Pointer.Invariant.DefinitionsThese are the purely weighted bounds that a heavy path needs, with no term: a key inside a heavy subtree stays close to the root even in the middle of a rebalancing, independently of how many light slots the stack holds. The price is the multiplicative factor on the depth inside the payload.
Theorem (uniform interruption). With unit weights and empty slots, the ordinary unweighted pop-tart, every key at every cut point has depth less than during a pop and less than during the push of a new empty slot of weight .
theorem uniform_pop_prefix {stack rest : Layer} {crumb : WeightedTree} (valid : stack.Valid 0) (popped : stack.pop = some (crumb, rest)) (unitWeights : ∀ subtree weight depth, SlotAt stack.decode subtree weight depth → weight = 1) (empty : stack.decode.EmptyFringe) : ∃ ops : List Multifinger.Opcode, ops.length = stack.popCost ∧ Multifinger.replay ops ⟨stack.decode.tree, []⟩ = some ⟨.node crumb.tree rest.decode.tree, []⟩ ∧ ∀ cut, ∃ middle, Multifinger.replay (ops.take cut) ⟨stack.decode.tree, []⟩ = some middle ∧ Multifinger.replay (ops.drop cut) middle = some ⟨.node crumb.tree rest.decode.tree, []⟩ ∧ ∀ x, x < stack.decode.tree.size → (Tree.depth middle.tree x : ℝ) < 13 + 17 * Real.logb 2 (stack.decode.leafCount) := RotationBST.PopTart.Chocolate.Interruption.uniform_pop_prefix valid popped unitWeights emptyModels.Rotation, Models.Pointer.Machine, Models.Pointer.Lazy.Skeleton, Models.Multifinger.Program.Definitions, Models.Multifinger.Program.Replay.Definitions, Bounds.StaticTree.Definitions, Structures.PopTart.Definitions, Structures.PopTart.Chocolate.Definitions, Structures.PopTart.Located.Definitions, Models.Pointer.Invariant.Definitionstheorem uniform_push_prefix {stack : Layer} (valid : stack.Valid 0) (unitWeights : ∀ subtree weight depth, SlotAt stack.decode subtree weight depth → weight = 1) (empty : stack.decode.EmptyFringe) : ∃ ops : List Multifinger.Opcode, ops.length = stack.pushCost ∧ Multifinger.replay ops ⟨.node .nil stack.decode.tree, []⟩ = some ⟨(stack.push (.leaf .nil 1)).decode.tree, []⟩ ∧ ∀ cut, ∃ middle, Multifinger.replay (ops.take cut) ⟨.node .nil stack.decode.tree, []⟩ = some middle ∧ Multifinger.replay (ops.drop cut) middle = some ⟨(stack.push (.leaf .nil 1)).decode.tree, []⟩ ∧ ∀ x, x < stack.decode.tree.size + 1 → (Tree.depth middle.tree x : ℝ) < 14 + 21 * Real.logb 2 (stack.decode.leafCount) := RotationBST.PopTart.Chocolate.Interruption.uniform_push_prefix valid unitWeights emptyModels.Rotation, Models.Pointer.Machine, Models.Pointer.Lazy.Skeleton, Models.Multifinger.Program.Definitions, Models.Multifinger.Program.Replay.Definitions, Bounds.StaticTree.Definitions, Structures.PopTart.Definitions, Structures.PopTart.Chocolate.Definitions, Structures.PopTart.Located.Definitions, Models.Pointer.Invariant.Definitions37.2.9 The top slot
In the heavy-path representation, the slot at the top of a pop-tart is the subtree next to the finger. The simulated algorithm changes that subtree, and its weight, without pushing or popping. The Top module supports this.
/-- The first outer crumb. At level zero it is one opaque payload. -/def top : Layer → WeightedTree | .last .zero icing => icing.decode | .next .zero child icing => .node child.decode icing.decode | .last (.one a) _ | .last (.two a _) _ | .last (.three a _ _) _ => a | .next (.one a) _ _ | .next (.two a _) _ _ | .next (.three a _ _) _ _ => a/-- The unchanged suffix beyond the first separator, if that separator exists. -/def topTail : Layer → Option WeightedTree | .last .zero _ | .next .zero _ _ => none | .last (.one _) icing => some icing.decode | .last (.two _ b) icing => some (.node b icing.decode) | .last (.three _ b c) icing => some (.node b (.node c icing.decode)) | .next (.one _) child icing => some (.node child.decode icing.decode) | .next (.two _ b) child icing => some (.node b (.node child.decode icing.decode)) | .next (.three _ b c) child icing => some (.node b (.node c (.node child.decode icing.decode)))/-- Update the opaque first payload without touching the suffix metadata.This is a representation operation, not a free physical change of subtree. -/def replaceTop (new : WeightedTree) : Layer → Layer | .last .zero _ | .next .zero _ _ => .last .zero (.base new) | .last (.one _) icing => .last (.one new) icing | .last (.two _ b) icing => .last (.two new b) icing | .last (.three _ b c) icing => .last (.three new b c) icing | .next (.one _) child icing => .next (.one new) child icing | .next (.two _ b) child icing => .next (.two new b) child icing | .next (.three _ b c) child icing => .next (.three new b c) child icingtop is the first outer crumb: the first regular crumb, or, when the top layer has no regulars, the whole stack, which is then a single base crumb on a valid stack. topTail is the rest of the stack beyond the first separator, or none if there is no separator. replaceTop new replaces the top crumb and keeps every other piece of the representation. It is a change of representation only. The physical change of the subtree must be paid for by a trace, as replaceTop_lift below does.
theorem decode_top (stack : Layer) : stack.decode = stack.topTail.elim stack.top (WeightedTree.node stack.top) := RotationBST.PopTart.Chocolate.decode_top stacktheorem replaceTop_decode (stack : Layer) (new : WeightedTree) : (stack.replaceTop new).decode = stack.topTail.elim new (WeightedTree.node new) := RotationBST.PopTart.Chocolate.replaceTop_decode stack newtheorem replaceTop_top (stack : Layer) (new : WeightedTree) : (stack.replaceTop new).top = new := RotationBST.PopTart.Chocolate.replaceTop_top stack newtheorem replaceTop_tail (stack : Layer) (new : WeightedTree) : (stack.replaceTop new).topTail = stack.topTail := RotationBST.PopTart.Chocolate.replaceTop_tail stack newtheorem replaceTop_replaceTop (stack : Layer) (first second : WeightedTree) : (stack.replaceTop first).replaceTop second = stack.replaceTop second := RotationBST.PopTart.Chocolate.replaceTop_replaceTop stack first secondThe physical stack is top alone or .node top tail. After replaceTop new it is the same with new in place of top. The tail is unchanged, and a second replacement overrides the first.
theorem top_valid {stack : Layer} {level : Nat} (valid : stack.Valid level) : Perfect level stack.top ∧ stack.top.Nonnegative := RotationBST.PopTart.Chocolate.top_valid validtheorem replaceTop_valid {stack : Layer} {level : Nat} (valid : stack.Valid level) {new : WeightedTree} (perfect : Perfect level new) (nonnegative : new.Nonnegative) : (stack.replaceTop new).Valid level := RotationBST.PopTart.Chocolate.replaceTop_valid valid perfect nonnegativetheorem replaceTop_potential {stack : Layer} {level : Nat} (valid : stack.Valid level) (new : WeightedTree) : (stack.replaceTop new).potential = stack.potential := RotationBST.PopTart.Chocolate.replaceTop_potential valid newtheorem replaceTop_mass (stack : Layer) (new : WeightedTree) : (stack.replaceTop new).decode.mass + stack.top.mass = stack.decode.mass + new.mass := RotationBST.PopTart.Chocolate.replaceTop_mass stack newtheorem replaceTop_size (stack : Layer) (new : WeightedTree) : (stack.replaceTop new).decode.tree.size + stack.top.tree.size = stack.decode.tree.size + new.tree.size := RotationBST.PopTart.Chocolate.replaceTop_size stack newThe top crumb of a valid stack is perfect and nonnegative. Conversely, replacing it by any perfect, nonnegative crumb of the same level keeps the stack valid and does not change the potential. At level , any single slot qualifies, whatever its payload and weight. This works because the top crumb sits under a regular node, and no inequality of Valid involves the masses of regular crumbs. So the top slot’s weight can rise or fall arbitrarily with no rebalancing, and the depth bounds then hold for the new masses. The mass and the size change by the difference between the two crumbs.
Theorem (lifting a top change). Let the old and new top trees be nonempty, and let a trace of cost turn the old top tree into the new one. Then a trace of cost at most turns the whole stack into the stack after replaceTop. If every intermediate tree of the given trace satisfies the size-and-depth invariant (with ) relative to the old top, then the lifted trace satisfies the same invariant relative to the whole stack.
theorem replaceTop_lift (stack : Layer) (new : WeightedTree) {cost : Nat} (oldNonempty : stack.top.tree ≠ .nil) (newNonempty : new.tree ≠ .nil) (trace : Pointer.Trace ⟨stack.top.tree, []⟩ ⟨new.tree, []⟩ cost) : ∃ total, total ≤ cost + 2 ∧ Pointer.Trace ⟨stack.decode.tree, []⟩ ⟨(stack.replaceTop new).decode.tree, []⟩ total := RotationBST.PopTart.Chocolate.replaceTop_lift stack new oldNonempty newNonempty tracetheorem replaceTop_within (stack : Layer) (new : WeightedTree) {cost factor constant : Nat} (factorPositive : 1 ≤ factor) (oldNonempty : stack.top.tree ≠ .nil) (newNonempty : new.tree ≠ .nil) (trace : Pointer.TraceWithin (fun t => t.size = stack.top.tree.size ∧ ∀ x, Tree.depth t x ≤ factor * Tree.depth stack.top.tree x + constant) ⟨stack.top.tree, []⟩ ⟨new.tree, []⟩ cost) : ∃ total, total ≤ cost + 2 ∧ Pointer.TraceWithin (fun t => t.size = stack.decode.tree.size ∧ ∀ x, Tree.depth t x ≤ factor * Tree.depth stack.decode.tree x + constant) ⟨stack.decode.tree, []⟩ ⟨(stack.replaceTop new).decode.tree, []⟩ total := RotationBST.PopTart.Chocolate.replaceTop_within stack new factorPositive oldNonempty newNonempty traceThe is one move down to the top crumb and one move back up, needed only when there is a tail. The trees must be nonempty because the cursor must stand on a node.
theorem hasTop_of_fringe {stack : Layer} (valid : stack.Valid 0) {subtree : Tree} {weight : ℝ} {suffix : List (Tree × ℝ)} (fringe : stack.decode.fringe = (subtree, weight) :: suffix) (nonempty : suffix ≠ []) : stack.HasTop := RotationBST.PopTart.Chocolate.hasTop_of_fringe valid fringe nonemptytheorem pop_front {stack : Layer} (valid : stack.Valid 0) {subtree : Tree} {weight : ℝ} {suffix : List (Tree × ℝ)} (fringe : stack.decode.fringe = (subtree, weight) :: suffix) (nonempty : suffix ≠ []) : ∃ rest, stack.pop = some (.leaf subtree weight, rest) ∧ rest.Valid 0 ∧ rest.decode.fringe = suffix ∧ stack.decode.mass = weight + rest.decode.mass ∧ stack.popCost + rest.potential ≤ 20 + stack.potential := RotationBST.PopTart.Chocolate.pop_front valid fringe nonemptyAt level , a valid stack with at least two fringe slots has a top, so a pop is available whenever the stack holds more than its bottom slot. pop_front reads the pop off the fringe: if the fringe is (subtree, weight) :: suffix with suffix nonempty, then pop returns exactly that slot and a valid rest whose fringe is suffix, with amortized cost at most . This is the form in which the heavy-path histories pop the slot next to the finger.