Chapter 37 · Pop-tarts · Section 37.1Vanilla and Cherry pop-tarts
The deamortization of Bose, Collette, Fagerberg and Langerman (2012a), in Chapter 30, stores every heavy path of the simulated tree as two stacks, and each stack is itself a binary search tree. Section 2 of their paper calls such a BST a pop-tart. Nodes are pushed in decreasing key order: the new node arrives as the parent of the current root, with the old stack as its right subtree and a leaf as its left subtree. A pop removes the root, whose left child is a leaf. Between operations the structure may rebalance with finger moves and rotations, and it must end with the finger back at the root.
Leaves carry weights. In the paper’s terms, a pop-tart is good if push and pop take amortized and worst-case time, and crazy good if moreover every leaf of weight has depth , where is the total weight of the leaves. The paper describes three pop-tarts. Vanilla never rebalances; it is crazy good when each new leaf outweighs the rest of the stack. Cherry is a 2-4 tree laid out along the right spine; it is crazy good for unit weights, and for weights that only increase. Chocolate adds frozen icing layers, built as Vanilla stacks, and is crazy good for arbitrary weights. Chocolate is the one the deamortization uses. It is the subject of Section 37.2.
This section covers the common vocabulary, Vanilla and Cherry, and two variants used by the heavy-path chapter: slots located by their physical rank, and mirrored pop-tarts that push in increasing key order.
A Vanilla pop-tart is a right spine whose left children are its weighted leaves, and whose last right child is the leaf of the initial empty stack. It is valid if the bottom leaf has nonnegative weight and every pushed leaf has weight at least the total weight of the leaves below it on the spine. Pushing a leaf of weight onto a valid stack of total weight gives a valid stack, and popping a valid stack gives a valid stack. Each push and pop is realized by a pointer trace of zero instructions, root to root, under any context. In a valid stack of total weight , a leaf of weight at depth satisfies
This is Lemma 1 of the paper, with its constant. Depth counts edges from the root, and the weights of internal nodes are not part of . A leaf may hold a whole subtree, frozen as one fringe slot of a PopTart.WeightedTree. The decreasing key order of pushes is built into the shape of the operations and is not a hypothesis.
The Lean statements
theorem push_valid {stack : WeightedTree} (valid : Valid stack) (t : Tree) (w : ℝ) (dominates : stack.mass ≤ w) : Valid (push t w stack) := … RotationBST.PopTart.Vanilla.push_valid valid t w dominatestheorem pop_valid {stack tail : WeightedTree} {t : Tree} {w : ℝ} (valid : Valid stack) (exposed : pop stack = some ((t, w), tail)) : Valid tail ∧ 0 ≤ w ∧ tail.mass ≤ w := … RotationBST.PopTart.Vanilla.pop_valid valid exposedtheorem operation {stack tail : WeightedTree} (op : Operation stack tail) (cs : List Pointer.Frame) : Pointer.Trace ⟨op.input, cs⟩ ⟨op.output, cs⟩ 0 := … RotationBST.PopTart.Vanilla.operation op cstheorem depth_log {stack : WeightedTree} (valid : Valid stack) {subtree : Tree} {weight : ℝ} {depth : Nat} (slot : WeightedTree.SlotAt stack subtree weight depth) (slotPositive : 0 < weight) : (depth : ℝ) ≤ 1 + Real.logb 2 (stack.mass / weight) := … RotationBST.PopTart.Vanilla.depth_log valid slot slotPositiveStart a Cherry pop-tart from the empty stack, and apply any sequence of pushes and pops of single leaves. Then every operation is realized by a pointer trace, root to root, and the total number of instructions is at most . A stack with leaves has every leaf at depth at most . Its next push costs at most instructions, and its next pop at most . In an ordinary pop-tart, whose leaves are empty, with nodes, every node has depth less than . This is Lemma 2 of the paper, with explicit constants.
Suppose in addition that the bottom leaf has nonnegative weight and that each pushed leaf weighs at least as much as every leaf currently in the stack. Then the leaves stay sorted by nonincreasing weight in inorder, the total cost is still at most , and in the final stack of total weight every leaf of weight has depth
This is Lemma 3 of the paper. The lemma is stated with ; its proof ends with , which drops an additive constant: the heaviest leaf is at depth , while can be . The Lean bound adds for the spine nodes. The costs are those of Program.cost, and empty_sequence and weighted_sequence write the cost bound as , which is .
The Lean statements
theorem empty_sequence {level : Nat} {rest : Layers} {n : Nat} (program : Program level .nil rest n) (bottom : WeightedTree) : Valid level rest ∧ program.Realizes bottom ∧ n + program.cost ≤ 11 * n := … RotationBST.PopTart.Cherry.empty_sequence program bottomtheorem worst_case {layers : Layers} (valid : Valid 0 layers) {bottom : WeightedTree} (base : Perfect 0 bottom) : (layers.pushCost : ℝ) ≤ 10 * Real.logb 2 (leafCount (layers.decode bottom)) ∧ (layers.popCost : ℝ) ≤ 6 * Real.logb 2 (leafCount (layers.decode bottom)) := … RotationBST.PopTart.Cherry.worst_case valid basetheorem depth_log {layers : Layers} (valid : Valid 0 layers) {bottom : WeightedTree} (base : Perfect 0 bottom) {subtree : Tree} {weight : ℝ} {depth : Nat} (slot : SlotAt (layers.decode bottom) subtree weight depth) : (depth : ℝ) ≤ 4 * Real.logb 2 (leafCount (layers.decode bottom)) := … RotationBST.PopTart.Cherry.depth_log valid base slottheorem key_depth {layers : Layers} (valid : Valid 0 layers) {bottom : WeightedTree} (base : Perfect 0 bottom) (empty : (layers.decode bottom).EmptyFringe) (x : Nat) (inside : x < (layers.decode bottom).tree.size) : (Tree.depth (layers.decode bottom).tree x : ℝ) < 4 * Real.logb 2 (((layers.decode bottom).tree.size + 1 : Nat) : ℝ) := … RotationBST.PopTart.Cherry.key_depth valid base empty x insidetheorem weighted_sequence {rest : Layers} {n : Nat} (program : Program 0 .nil rest n) {bottom : WeightedTree} (base : Perfect 0 bottom) (bottomNonnegative : bottom.Nonnegative) (increasing : program.Increasing bottom) : Valid 0 rest ∧ (rest.decode bottom).Ordered ∧ program.Realizes bottom ∧ n + program.cost ≤ 11 * n ∧ ∀ subtree weight depth, SlotAt (rest.decode bottom) subtree weight depth → 0 < weight → (depth : ℝ) ≤ 3 + 4 * Real.logb 2 ((rest.decode bottom).mass / weight) := … RotationBST.PopTart.Cherry.weighted_sequence program base bottomNonnegative increasingThe two boxes state what the library proves of the paper’s Lemmas 1–3. The rest of the section gives the definitions in reading order and the theorems again, each with its Lean statement. Section 37.1.1 compares the paper’s conventions with the library’s, and summarizes the bounds.
These results support others. Outside this section, Vanilla appears only in one theorem of the next section, and Cherry nowhere. Chocolate (Section 37.2) builds on the weighted trees, slots and crumbs of this section, and proves its icing bound by viewing each icing as a Vanilla stack (Results.icing_vanilla). The paired stacks of heavy paths (Section 38.1) use mirrored pop-tarts for the ancestors larger than the endpoint, and located slots to translate slot depths into key depths. Cherry pop-tarts are used nowhere else in the library, as in the paper, where they are a warm-up for Chocolate. Read the sections on weighted trees and Vanilla before Section 37.2, and the sections on located slots and mirrored pop-tarts before Section 38.1. The Cherry section can be skipped.
The definitions are in Structures/PopTart/Definitions/Abstract.lean and the Definitions/Abstract.lean files of Vanilla, Cherry, Located and Mirror below it. The theorems are in the Abstract.lean files of the same directories.
37.1.1 Overview
All pop-tarts of the library are values of one type, PopTart.WeightedTree: a binary tree whose internal nodes are the stack’s nodes and whose leaves are fringe slots. A slot holds a weight and an arbitrary Tree, its payload. An ordinary pop-tart has empty payloads. The Chocolate pop-tart and the heavy paths need nonempty ones: Chocolate freezes whole crumbs inside its icing, and a heavy-path stack stores each off-path subtree as one slot. The physical BST is the weighted tree with every slot replaced by its payload.
A Cherry pop-tart, drawn with its layers, looks like this, with c the crumbs and ⊥ the bottom slot of the empty stack:
s₁ ─ s₂ ─ s₃ ─ s₄ ─ s₅ ─ … ─ ⊥ the right spine; s₁ is the root
│ │ │ │ │
c c c C C c = 0-crumb (one slot), C = 1-crumb (two slots)
└─ layer 0 ┘ └ layer 1 ┘
Each spine node is a separator whose left subtree is its crumb. A Vanilla pop-tart is the same picture with only one-slot crumbs and no layers.
The paper’s model and the library’s differ in a few conventions:
| paper | library |
|---|---|
| leaves, possibly empty pointers | fringe slots of a PopTart.WeightedTree, each with a payload subtree and a real weight |
| internal nodes have weight | internal nodes carry no weight; WeightedTree.mass sums slot weights only |
| depth of a leaf | outer depth of a slot, WeightedTree.SlotAt, the number of separator nodes above it |
| keys pushed in decreasing order | structural: a push receives the tree node t stack, with the old stack on the right |
| the finger returns to the root | costs are Pointer.Traces from root to root (Chapter 7) |
| time | pointer instructions, one unit each for moves and rotations |
| inserting or removing the root | the caller’s business, outside the operation’s cost |
The last line matters. A push starts from the tree in which the new node is already the parent of the stack, and a pop ends with the popped node as the root above the rest. Moving the node between the stack and its caller is charged by whoever uses the pop-tart, such as the heavy-path stacks of Section 38.1.
The bounds proved in this section are:
| structure | cost per operation | depth of a slot of weight | hypothesis |
|---|---|---|---|
| Vanilla | instructions | each push dominates the mass below | |
| Cherry | amortized; (push), (pop) worst case | one-slot pushes | |
| Cherry, weighted | amortized | each push weighs at least every current slot |
Here is the total weight of the slots and their number.
37.1.2 Weighted trees
37.1.2.1 The type
/-- A pop-tart's active nodes and weighted fringe occurrences. A fringe slotcan hold an entire frozen subtree. Equal-shaped slots may have differentweights, and the outer structure does not inspect their contents. -/inductive WeightedTree where | leaf (subtree : Tree) (weight : ℝ) | node (left right : WeightedTree)A weighted tree is either a slot leaf subtree weight or a separator node node left right. The separator nodes are the pop-tart’s active nodes. Equal payloads may occur in several slots with different weights, and the type never inspects a payload.
def skeleton : WeightedTree → Pointer.Lazy.Skeleton | .leaf t _ => .hole t | .node l r => .node l.skeleton r.skeletondef tree (t : WeightedTree) : Tree := t.skeleton.treeskeleton forgets the weights and gives a Lazy.Skeleton of Chapter 7: the separators become the skeleton’s nodes and the slots become holes. tree fills the holes in, and is the physical BST in which the pointer machine runs. So a slot whose payload has keys contributes keys to tree and none to the skeleton’s size.
/-- Total weight of fringe slots; active separator nodes are not included. -/def mass : WeightedTree → ℝ | .leaf _ w => w | .node l r => l.mass + r.massdef Nonnegative : WeightedTree → Prop | .leaf _ w => 0 ≤ w | .node l r => l.Nonnegative ∧ r.Nonnegative/-- Number of outer fringe slots, independent of their contents and weights. -/def leafCount : WeightedTree → Nat | .leaf _ _ => 1 | .node l r => leafCount l + leafCount r/-- Inorder weighted fringe, retaining distinct weights on equal-shaped slots. -/def fringe : WeightedTree → List (Tree × ℝ) | .leaf t w => [(t, w)] | .node l r => l.fringe ++ r.fringemass is the total weight of the slots. Nonnegative says that every slot weight is nonnegative. leafCount is the number of slots, independent of payloads. fringe lists the slots in inorder as pairs (payload, weight). Equal pairs remain distinct list entries, so the fringe records occurrences, not a set.
/-- Outer height treats each frozen fringe as one external leaf. -/def height : WeightedTree → Nat | .leaf _ _ => 0 | .node l r => max l.height r.height + 1/-- In an ordinary pop-tart every fringe is an empty external leaf. -/def EmptyFringe : WeightedTree → Prop | .leaf t _ => t = .nil | .node l r => l.EmptyFringe ∧ r.EmptyFringeheight counts separator levels only: a slot is a leaf of height , whatever its payload. EmptyFringe says that every payload is nil. That is the ordinary pop-tart of the paper, in which the slots are the external leaves of the physical tree.
37.1.2.2 Slots
/-- A particular weighted fringe occurrence at a given outer depth. Therelation retains its weight even if the same subtree occurs elsewhere. -/inductive SlotAt : WeightedTree → Tree → ℝ → Nat → Prop where | leaf (t : Tree) (w : ℝ) : SlotAt (.leaf t w) t w 0 | left {l r : WeightedTree} {t : Tree} {w : ℝ} {depth : Nat} : SlotAt l t w depth → SlotAt (.node l r) t w (depth + 1) | right {l r : WeightedTree} {t : Tree} {w : ℝ} {depth : Nat} : SlotAt r t w depth → SlotAt (.node l r) t w (depth + 1)SlotAt t subtree weight depth says that t has a slot with this payload and weight at outer depth depth. It is a path from the root, so two equal slots at different places are two different witnesses. The depth bounds of this section and the next are stated for SlotAt witnesses, or for the located slots below.
The basic facts say that a slot’s depth is the cost of reaching it, that slots are fringe entries, and that weights are bounded by the mass:
theorem slot_context {t : WeightedTree} {subtree : Tree} {weight : ℝ} {depth : Nat} (slot : SlotAt t subtree weight depth) : ∃ cs : List Pointer.Frame, cs.length = depth ∧ Pointer.plug cs subtree = t.tree := RotationBST.PopTart.WeightedTree.slot_context slottheorem slot_trace {t : WeightedTree} {subtree : Tree} {weight : ℝ} {depth : Nat} (slot : SlotAt t subtree weight depth) (nonempty : subtree ≠ .nil) : ∃ cs : List Pointer.Frame, cs.length = depth ∧ Pointer.plug cs subtree = t.tree ∧ Pointer.Trace ⟨t.tree, []⟩ ⟨subtree, cs⟩ depth := RotationBST.PopTart.WeightedTree.slot_trace slot nonemptytheorem slot_split {t : WeightedTree} {subtree : Tree} {weight : ℝ} {depth : Nat} (slot : SlotAt t subtree weight depth) : ∃ before after, t.fringe = before ++ (subtree, weight) :: after := RotationBST.PopTart.WeightedTree.slot_split slottheorem slot_le_mass {t : WeightedTree} {subtree : Tree} {weight : ℝ} {depth : Nat} (slot : SlotAt t subtree weight depth) (nonnegative : t.Nonnegative) : 0 ≤ weight ∧ weight ≤ t.mass := RotationBST.PopTart.WeightedTree.slot_le_mass slot nonnegativeslot_context gives the zipper path (Pointer.Frames) of the slot: it has exactly depth frames, and plugging the payload back in gives t.tree. slot_trace adds that, when the payload is nonempty, the pointer machine walks from the root to the payload’s root in exactly depth instructions. The payload must be nonempty because the cursor never moves onto nil. slot_split places the slot in the fringe list, and slot_le_mass bounds its weight by in a nonnegative tree.
theorem height_slot (t : WeightedTree) : ∃ subtree weight depth, SlotAt t subtree weight depth ∧ depth = t.height := RotationBST.PopTart.WeightedTree.height_slot ttheorem empty_count {t : WeightedTree} (empty : t.EmptyFringe) : leafCount t = t.tree.size + 1 := RotationBST.PopTart.WeightedTree.empty_count emptytheorem key_depth_lt_height {t : WeightedTree} (empty : t.EmptyFringe) (x : Nat) (valid : x < t.tree.size) : Tree.depth t.tree x < t.height := RotationBST.PopTart.WeightedTree.key_depth_lt_height empty x validSome slot lies at depth exactly height, so a bound on all slot depths bounds the height. When every payload is empty, there is one slot more than keys, and every key is strictly shallower than the height. These two facts turn slot bounds into the key bounds of an ordinary pop-tart, as in Results.key_depth.
theorem fringe_length (t : WeightedTree) : t.fringe.length = t.leafCount := RotationBST.PopTart.WeightedTree.fringe_length ttheorem fringe_mass (t : WeightedTree) : (t.fringe.map Prod.snd).sum = t.mass := RotationBST.PopTart.WeightedTree.fringe_mass ttheorem fringe_nonnegative (t : WeightedTree) : t.Nonnegative ↔ ∀ p ∈ t.fringe, 0 ≤ p.2 := RotationBST.PopTart.WeightedTree.fringe_nonnegative ttheorem mass_nonnegative {t : WeightedTree} (nonnegative : t.Nonnegative) : 0 ≤ t.mass := RotationBST.PopTart.WeightedTree.mass_nonnegative nonnegativeThe fringe has leafCount entries, its weights sum to mass, and the tree is nonnegative exactly when every fringe weight is. Hence a nonnegative tree has nonnegative mass.
37.1.2.3 Crumbs
/-- A complete crumb of a given height, with arbitrary weighted fringe slots. -/inductive Perfect : Nat → WeightedTree → Prop where | leaf (t : Tree) (w : ℝ) : Perfect 0 (.leaf t w) | node {level : Nat} {l r : WeightedTree} : Perfect level l → Perfect level r → Perfect (level + 1) (.node l r)A crumb of level , an -crumb in the paper, is a perfect weighted tree of height . Its slots are arbitrary: Cherry fills them with single leaves, and Chocolate may fill them with frozen subtrees.
theorem perfect_count {level : Nat} {crumb : WeightedTree} (perfect : Perfect level crumb) : leafCount crumb = 2 ^ level := RotationBST.PopTart.WeightedTree.perfect_count perfecttheorem perfect_depth {level : Nat} {crumb : WeightedTree} (perfect : Perfect level crumb) {subtree : Tree} {weight : ℝ} {depth : Nat} (slot : WeightedTree.SlotAt crumb subtree weight depth) : depth = level := RotationBST.PopTart.WeightedTree.perfect_depth perfect slotAn -crumb has slots, all at depth .
37.1.2.4 Ordered fringes
/-- Nonnegative fringe weights in nonincreasing inorder order. Equal weightsare allowed; the order concerns occurrences, including the bottom leaf. -/def Ordered (t : WeightedTree) : Prop := t.Nonnegative ∧ t.fringe.Pairwise (fun a b => b.2 ≤ a.2)Ordered t says that t is nonnegative and that its fringe weights are nonincreasing in inorder, ties allowed. The condition concerns all occurrences, including the bottom slot. It is the invariant behind the weighted Cherry bound: the most recently pushed slots are the heaviest and come first.
theorem ordered_append {t a b : WeightedTree} (split : t.fringe = a.fringe ++ b.fringe) : t.Ordered ↔ a.Ordered ∧ b.Ordered ∧ ∀ x ∈ a.fringe, ∀ y ∈ b.fringe, y.2 ≤ x.2 := RotationBST.PopTart.WeightedTree.ordered_append splittheorem ordered_prefix_mass {t : WeightedTree} (ordered : t.Ordered) {before after : List (Tree × ℝ)} {subtree : Tree} {weight : ℝ} (split : t.fringe = before ++ (subtree, weight) :: after) : ((before.length + 1 : Nat) : ℝ) * weight ≤ t.mass := RotationBST.PopTart.WeightedTree.ordered_prefix_mass ordered splitordered_append says that a fringe split into two parts is ordered exactly when both parts are ordered and every weight of the first part is at least every weight of the second. ordered_prefix_mass is the step from ranks to weights in the paper’s proof of Lemma 3. If the slot of weight is the -th entry of an ordered fringe, the entries up to it each weigh at least , so
37.1.3 Vanilla pop-tarts
A Vanilla pop-tart performs no rebalancing at all. The stack is a right spine: each push adds a node above the root, with its slot on the left.
/-- A right spine whose newly pushed fringe slot dominates the older mass.The leaf case is an empty active stack with one nonnegative-weight fringe slot. -/inductive Valid : WeightedTree → Prop where | leaf (t : Tree) (w : ℝ) (nonnegative : 0 ≤ w) : Valid (.leaf t w) | push {tail : WeightedTree} (t : Tree) (w : ℝ) (valid : Valid tail) (dominates : tail.mass ≤ w) : Valid (.node (.leaf t w) tail)Valid is the paper’s weight condition. The empty stack is one slot of nonnegative weight. A push of slot weight onto a valid stack tail is valid when , that is, when the new slot weighs at least as much as all the others together. Equality is allowed, as in Lemma 1.
def push (t : Tree) (w : ℝ) (stack : WeightedTree) : WeightedTree := .node (.leaf t w) stack/-- Expose the top separator and its weighted fringe slot. The caller removesthat separator; the realizing pointer trace itself preserves the entire tree. -/def pop : WeightedTree → Option ((Tree × ℝ) × WeightedTree) | .node (.leaf t w) tail => some ((t, w), tail) | _ => nonepush t w stack is the node whose left slot holds t with weight w and whose right child is stack. pop exposes the top slot and the stack below. It fails on a single slot, and on a node whose left child is not a slot, which no valid stack has.
/-- The caller supplies a new top separator on push and takes the exposed topseparator on pop. Only rebalancing between those boundaries is charged here. -/inductive Operation : WeightedTree → WeightedTree → Type where | push (t : Tree) (w : ℝ) (stack : WeightedTree) : Operation stack (push t w stack) | pop (stack tail : WeightedTree) (t : Tree) (w : ℝ) (exposed : pop stack = some ((t, w), tail)) : Operation stack taildef input {stack tail : WeightedTree} : Operation stack tail → Tree | .push t _ stack => .node t stack.tree | .pop stack _ _ _ _ => stack.treedef output {stack tail : WeightedTree} : Operation stack tail → Tree | .push _ _ _ => tail.tree | .pop _ tail t _ _ => .node t tail.treedef Realizes {stack tail : WeightedTree} (op : Operation stack tail) (cost : Nat) : Prop := Pointer.Trace ⟨op.input, []⟩ ⟨op.output, []⟩ costAn Operation stack tail is a push, or a pop that is known to succeed. input and output are the physical trees at the operation’s boundaries. A push starts from node t stack.tree, the new node already above the old stack, and ends at the pushed stack. A pop starts from the stack and ends with the top node above the rest, node t tail.tree. Realizes op cost asks for a pointer trace from input to output, root to root, with cost instructions. In both cases input and output are the same tree, which is why Vanilla costs nothing.
37.1.3.1 Validity
theorem push_valid {stack : WeightedTree} (valid : Valid stack) (t : Tree) (w : ℝ) (dominates : stack.mass ≤ w) : Valid (push t w stack) := RotationBST.PopTart.Vanilla.push_valid valid t w dominatestheorem pop_valid {stack tail : WeightedTree} {t : Tree} {w : ℝ} (valid : Valid stack) (exposed : pop stack = some ((t, w), tail)) : Valid tail ∧ 0 ≤ w ∧ tail.mass ≤ w := RotationBST.PopTart.Vanilla.pop_valid valid exposedPushing a dominating slot preserves validity, and popping a valid stack returns a valid stack together with the popped slot, which is nonnegative and dominates the rest.
theorem pop_push (t : Tree) (w : ℝ) (stack : WeightedTree) : pop (push t w stack) = some ((t, w), stack) := RotationBST.PopTart.Vanilla.pop_push t w stacktheorem cases_valid {stack : WeightedTree} (valid : Valid stack) : (∃ t w, stack = .leaf t w ∧ 0 ≤ w) ∨ ∃ t w tail, stack = push t w tail ∧ Valid tail ∧ tail.mass ≤ w := RotationBST.PopTart.Vanilla.cases_valid validtheorem nonnegative {stack : WeightedTree} (valid : Valid stack) : stack.Nonnegative := RotationBST.PopTart.Vanilla.nonnegative validA pop undoes a push. A valid stack is either a single nonnegative slot or a dominating push onto a valid stack. Valid stacks are nonnegative.
theorem push_fringe (t : Tree) (w : ℝ) (stack : WeightedTree) : (push t w stack).skeleton.fringe = t :: stack.skeleton.fringe := RotationBST.PopTart.Vanilla.push_fringe t w stacktheorem pop_fringe {stack tail : WeightedTree} {t : Tree} {w : ℝ} (exposed : pop stack = some ((t, w), tail)) : stack.skeleton.fringe = t :: tail.skeleton.fringe := RotationBST.PopTart.Vanilla.pop_fringe exposedA push adds its payload at the front of the fringe, and a pop removes it. So the fringe of a Vanilla stack is the list of payloads from the most recent to the oldest, ending with the bottom.
37.1.3.2 Cost
Theorem (Vanilla operations). Every Vanilla push and pop is realized by a pointer trace of zero instructions, under any context.
theorem operation {stack tail : WeightedTree} (op : Operation stack tail) (cs : List Pointer.Frame) : Pointer.Trace ⟨op.input, cs⟩ ⟨op.output, cs⟩ 0 := RotationBST.PopTart.Vanilla.operation op csThe context cs places the pop-tart anywhere inside a larger tree. Since input and output coincide, the empty trace works. The paper’s “ time per operation” is, in this accounting, the caller’s transfer of the node into or out of the stack.
37.1.3.3 Depth
Theorem (Lemma 1, depth). In a valid Vanilla stack of mass , a slot of weight at depth satisfies . If , then .
theorem depth_mass {stack : WeightedTree} (valid : Valid stack) {subtree : Tree} {weight : ℝ} {depth : Nat} (slot : WeightedTree.SlotAt stack subtree weight depth) : (2 : ℝ) ^ depth * weight ≤ 2 * stack.mass := RotationBST.PopTart.Vanilla.depth_mass valid slottheorem depth_log {stack : WeightedTree} (valid : Valid stack) {subtree : Tree} {weight : ℝ} {depth : Nat} (slot : WeightedTree.SlotAt stack subtree weight depth) (slotPositive : 0 < weight) : (depth : ℝ) ≤ 1 + Real.logb 2 (stack.mass / weight) := RotationBST.PopTart.Vanilla.depth_log valid slot slotPositiveThe proof is the paper’s induction. The top slot is at depth and weighs at least . Every other slot lies in the tail, whose mass is at most , one level deeper. The base case, a single slot at depth , gives .
theorem access_slot {stack : WeightedTree} (valid : Valid stack) {subtree : Tree} {weight : ℝ} {depth : Nat} (slot : WeightedTree.SlotAt stack subtree weight depth) (slotPositive : 0 < weight) (nonempty : subtree ≠ .nil) : ∃ cs : List Pointer.Frame, cs.length = depth ∧ Pointer.plug cs subtree = stack.tree ∧ Pointer.Trace ⟨stack.tree, []⟩ ⟨subtree, cs⟩ depth ∧ (depth : ℝ) ≤ 1 + Real.logb 2 (stack.mass / weight) := RotationBST.PopTart.Vanilla.access_slot valid slot slotPositive nonemptyaccess_slot combines this bound with Results.slot_trace: a slot with a nonempty payload is reached from the root by exactly pointer moves, with . Chocolate’s icing access lemma, Results.icing_access, is this theorem applied to the icing viewed as a Vanilla stack.
37.1.4 Cherry pop-tarts
The Cherry pop-tart is a 2-4 tree laid along the right spine. The spine is cut into layers of consecutive nodes. Layer holds one to three -crumbs as the left subtrees of its nodes, and the last node of a layer has the first node of the next layer as its right child. The last layer ends in the bottom slot of the empty stack. A push adds a -crumb to layer . When a layer would hold four crumbs, one rotation joins its last two crumbs into a crumb of the next level, which is pushed onto the next layer. A pop removes the first -crumb; when a layer becomes empty, one rotation splits the first crumb of the next layer into two. These carries and borrows behave like the digits of a redundant binary counter.
37.1.4.1 Layers
/-- Redundant binary digits store one to three crumbs at each layer. -/inductive Layers where | nil | one (a : WeightedTree) (tail : Layers) | two (a b : WeightedTree) (tail : Layers) | three (a b c : WeightedTree) (tail : Layers)def decode (bottom : WeightedTree) : Layers → WeightedTree | .nil => bottom | .one a tail => .node a (tail.decode bottom) | .two a b tail => .node a (.node b (tail.decode bottom)) | .three a b c tail => .node a (.node b (.node c (tail.decode bottom)))def height : Layers → Nat | .nil => 0 | .one _ tail | .two _ _ tail | .three _ _ _ tail => tail.height + 1Layers is a list of layers, each with one, two or three crumbs. decode bottom layers is the weighted tree: the crumbs of all layers, in order, as the left children of the spine, with bottom at the end. height is the number of layers, not the height of the tree.
/-- Each layer contains perfect crumbs of its level; the next level is onehigher. Empty layers terminate the structure. -/inductive Valid : Nat → Layers → Prop where | nil (level : Nat) : Valid level .nil | one {level : Nat} {a : WeightedTree} {tail : Layers} : Perfect level a → Valid (level + 1) tail → Valid level (.one a tail) | two {level : Nat} {a b : WeightedTree} {tail : Layers} : Perfect level a → Perfect level b → Valid (level + 1) tail → Valid level (.two a b tail) | three {level : Nat} {a b c : WeightedTree} {tail : Layers} : Perfect level a → Perfect level b → Perfect level c → Valid (level + 1) tail → Valid level (.three a b c tail)Valid level layers says that the crumbs of the first layer are perfect of height level, those of the next layer of height level + 1, and so on. A Cherry pop-tart is a valid Layers at level with a single-slot bottom, Perfect 0 bottom. The general level is needed for the recursion: a carry from layer is a push at level .
37.1.4.2 Push and pop
/-- Overflow retains the first two crumbs and carries the joined last two. -/def push (new : WeightedTree) : Layers → Layers | .nil => .one new .nil | .one a tail => .two new a tail | .two a b tail => .three new a b tail | .three a b c tail => .two new a (tail.push (.node b c))/-- Underflow borrows one next-level crumb and splits it into its two children.Invalid layers are totalized by failure; the invariant proves success. -/def pop : Layers → Option (WeightedTree × Layers) | .nil => none | .one a .nil => some (a, .nil) | .one a tail => match tail.pop with | some (.node b c, rest) => some (a, .two b c rest) | _ => none | .two a b tail => some (a, .one b tail) | .three a b c tail => some (a, .two b c tail)push new adds a crumb in front of the first layer. A full layer three a b c becomes two new a, and the crumb node b c is pushed recursively onto the next layer. pop takes the first crumb. A layer one a with a nonempty tail borrows: it pops a crumb node b c from the next layer and becomes two b c. The pop is totalized by none on shapes the invariant excludes, for example a borrowed crumb that is a single slot.
def pushCost : Layers → Nat | .three _ _ _ tail => 10 + tail.pushCost | _ => 0def popCost : Layers → Nat | .one _ .nil => 0 | .one _ tail => 6 + tail.popCost | _ => 0/-- Ten units on a one- or three-crumb layer pay for a full pointer round tripwhen that layer must carry or borrow. -/def potential : Layers → Nat | .nil => 0 | .one _ tail | .three _ _ _ tail => 10 + tail.potential | .two _ _ tail => tail.potentialpushCost charges instructions for each carry, and popCost charges for each borrow. These are the exact lengths of the traces built in the proofs: walk down the spine to the layer, rotate, and walk back. The potential is the paper’s, scaled to the cost: for each layer with one or three crumbs, for a layer with two. A carry turns a three-crumb layer into a two-crumb layer, and a borrow turns a one-crumb layer into a two-crumb layer, so each releases exactly the units that pay for it.
37.1.4.3 Operations and programs
/-- Legal stack operations at one crumb level. The enclosing algorithm suppliesor receives the top separator at the stated operation boundary. -/inductive Operation (level : Nat) : Layers → Layers → Type where | push (layers : Layers) (crumb : WeightedTree) (perfect : Perfect level crumb) : Operation level layers (layers.push crumb) | pop (layers rest : Layers) (crumb : WeightedTree) (popped : layers.pop = some (crumb, rest)) : Operation level layers restdef cost {level : Nat} {layers rest : Layers} : Operation level layers rest → Nat | .push _ _ _ => layers.pushCost | .pop _ _ _ _ => layers.popCostdef input {level : Nat} {layers rest : Layers} (bottom : WeightedTree) : Operation level layers rest → Tree | .push _ crumb _ => .node crumb.tree (layers.decode bottom).tree | .pop _ _ _ _ => (layers.decode bottom).treedef output {level : Nat} {layers rest : Layers} (bottom : WeightedTree) : Operation level layers rest → Tree | .push _ _ _ => (rest.decode bottom).tree | .pop _ _ crumb _ => .node crumb.tree (rest.decode bottom).treeAn Operation level layers rest is a push of a perfect crumb of the given level, or a pop that is known to succeed. Its cost is pushCost or popCost of the layers before it. input bottom and output bottom are the physical trees at the boundaries, with the same conventions as for Vanilla: a push starts with the new node above the decoded stack, and a pop ends with the popped node above the rest.
inductive Program (level : Nat) : Layers → Layers → Nat → Type where | nil (layers : Layers) : Program level layers layers 0 | cons {layers middle rest : Layers} {n : Nat} (op : Operation level layers middle) (tail : Program level middle rest n) : Program level layers rest (n + 1)def cost {level : Nat} {layers rest : Layers} {n : Nat} : Program level layers rest n → Nat | .nil _ => 0 | .cons op tail => op.cost + tail.cost/-- Each operation's actual pointer trace, with all caller transfers betweenoperation boundaries left explicit. The bottom fringe slot stays fixed. -/inductive Realizes (bottom : WeightedTree) {level : Nat} : {layers rest : Layers} → {n : Nat} → Program level layers rest n → Prop where | nil (layers : Layers) : Realizes bottom (.nil layers) | cons {layers middle rest : Layers} {n : Nat} {op : Operation level layers middle} {tail : Program level middle rest n} (first : Pointer.Trace ⟨op.input bottom, []⟩ ⟨op.output bottom, []⟩ op.cost) (later : Realizes bottom tail) : Realizes bottom (.cons op tail)A Program level layers rest n is a sequence of operations from layers to rest, and its cost is the sum. Realizes bottom program says that each operation is realized by a pointer trace from its input to its output, root to root, of exactly its cost. The bottom slot stays fixed, and any transfer between operations is left to the caller.
37.1.4.4 Lemma 2: amortized cost, worst case and height
Theorem (one push, one pop). Let layers be valid at some level. Pushing a perfect crumb of that level gives valid layers, puts the crumb’s slots in front of the fringe, is realized by a trace of pushCost instructions, and has amortized cost at most . If layers is nonempty, a pop succeeds and returns a perfect crumb of that level and valid layers, removes the crumb’s slots from the front of the fringe, is realized by a trace of popCost instructions, and has amortized cost at most .
theorem push {level : Nat} {layers : Layers} (valid : Valid level layers) {crumb : WeightedTree} (perfect : Perfect level crumb) (bottom : WeightedTree) : Valid level (layers.push crumb) ∧ ((layers.push crumb).decode bottom).fringe = crumb.fringe ++ (layers.decode bottom).fringe ∧ Pointer.Trace ⟨.node crumb.tree (layers.decode bottom).tree, []⟩ ⟨((layers.push crumb).decode bottom).tree, []⟩ layers.pushCost ∧ layers.pushCost + (layers.push crumb).potential ≤ 10 + layers.potential := RotationBST.PopTart.Cherry.push valid perfect bottomtheorem pop {level : Nat} {layers : Layers} (valid : Valid level layers) (nonempty : layers ≠ .nil) (bottom : WeightedTree) : ∃ crumb rest, layers.pop = some (crumb, rest) ∧ Perfect level crumb ∧ Valid level rest ∧ (layers.decode bottom).fringe = crumb.fringe ++ (rest.decode bottom).fringe ∧ Pointer.Trace ⟨(layers.decode bottom).tree, []⟩ ⟨.node crumb.tree (rest.decode bottom).tree, []⟩ layers.popCost ∧ layers.popCost + rest.potential ≤ 10 + layers.potential := RotationBST.PopTart.Cherry.pop valid nonempty bottomAmortized cost means actual cost plus the potential after, which is at most plus the potential before.
theorem operation_trace {level : Nat} {layers rest : Layers} (op : Operation level layers rest) (bottom : WeightedTree) (cs : List Pointer.Frame) : Pointer.Trace ⟨op.input bottom, cs⟩ ⟨op.output bottom, cs⟩ op.cost := RotationBST.PopTart.Cherry.operation_trace op bottom csEach operation’s trace can be run under any context, so a Cherry pop-tart may sit anywhere in a larger tree.
Theorem (sequences). A program of operations from valid layers ends in valid layers, is realized, and costs at most plus the initial potential minus the final one. From the empty stack, the total cost is at most .
theorem sequence {level : Nat} {layers rest : Layers} {n : Nat} (program : Program level layers rest n) (valid : Valid level layers) (bottom : WeightedTree) : Valid level rest ∧ program.Realizes bottom ∧ program.cost + rest.potential ≤ 10 * n + layers.potential := RotationBST.PopTart.Cherry.sequence program valid bottomtheorem empty_sequence {level : Nat} {rest : Layers} {n : Nat} (program : Program level .nil rest n) (bottom : WeightedTree) : Valid level rest ∧ program.Realizes bottom ∧ n + program.cost ≤ 11 * n := RotationBST.PopTart.Cherry.empty_sequence program bottomempty_sequence writes the bound as , that is, . It holds at every level and for every bottom slot. This is the “good” half of Lemma 2.
The height bound counts slots. A layer at index holds at least one -crumb, with slots, so there are at most layers.
theorem layer_population {level : Nat} {layers : Layers} (valid : Valid level layers) {bottom : WeightedTree} (base : Perfect 0 bottom) : 2 ^ (level + layers.height) + 1 ≤ leafCount (layers.decode bottom) + 2 ^ level := RotationBST.PopTart.Cherry.layer_population valid basetheorem height_log {layers : Layers} (valid : Valid 0 layers) {bottom : WeightedTree} (base : Perfect 0 bottom) : (layers.height : ℝ) ≤ Real.logb 2 (leafCount (layers.decode bottom)) := RotationBST.PopTart.Cherry.height_log valid baselayer_population is the counting statement at any level, for layers starting at level . height_log specializes it to level : .
Theorem (depth). In a Cherry pop-tart with slots, every slot has depth at most . In an ordinary Cherry pop-tart with nodes, every node has depth less than .
theorem depth_log {layers : Layers} (valid : Valid 0 layers) {bottom : WeightedTree} (base : Perfect 0 bottom) {subtree : Tree} {weight : ℝ} {depth : Nat} (slot : SlotAt (layers.decode bottom) subtree weight depth) : (depth : ℝ) ≤ 4 * Real.logb 2 (leafCount (layers.decode bottom)) := RotationBST.PopTart.Cherry.depth_log valid base slottheorem key_depth {layers : Layers} (valid : Valid 0 layers) {bottom : WeightedTree} (base : Perfect 0 bottom) (empty : (layers.decode bottom).EmptyFringe) (x : Nat) (inside : x < (layers.decode bottom).tree.size) : (Tree.depth (layers.decode bottom).tree x : ℝ) < 4 * Real.logb 2 (((layers.decode bottom).tree.size + 1 : Nat) : ℝ) := RotationBST.PopTart.Cherry.key_depth valid base empty x insideFor the proof: a slot in layer lies below at most spine nodes and crumb levels, and . The key version uses Results.empty_count: an ordinary pop-tart with keys has slots.
Theorem (worst case). In a Cherry pop-tart with slots, the next push costs at most instructions and the next pop at most .
theorem worst_case {layers : Layers} (valid : Valid 0 layers) {bottom : WeightedTree} (base : Perfect 0 bottom) : (layers.pushCost : ℝ) ≤ 10 * Real.logb 2 (leafCount (layers.decode bottom)) ∧ (layers.popCost : ℝ) ≤ 6 * Real.logb 2 (leafCount (layers.decode bottom)) := RotationBST.PopTart.Cherry.worst_case valid baseA carry or borrow can ripple through every layer, but there are at most of them. The bound refers to the stack before the operation, and to its number of slots, not to weights or payload sizes.
37.1.4.5 Lemma 3: increasing weights
For the weighted version, each push must weigh at least as much as everything already in the stack.
/-- A pushed crumb is itself ordered and no lighter than any old fringeoccurrence. At level zero it consists of exactly one newly pushed leaf.Pop operations impose no extra weight restriction. -/def Increasing {level : Nat} {layers rest : Layers} (bottom : WeightedTree) : Operation level layers rest → Prop | .push _ crumb _ => crumb.Ordered ∧ ∀ a ∈ crumb.fringe, ∀ b ∈ (layers.decode bottom).fringe, b.2 ≤ a.2 | .pop _ _ _ _ => Truedef Increasing (bottom : WeightedTree) {level : Nat} : {layers rest : Layers} → {n : Nat} → Program level layers rest n → Prop | _, _, _, .nil _ => True | _, _, _, .cons op tail => op.Increasing bottom ∧ tail.Increasing bottomA push is Increasing when its crumb is ordered and each of its slots weighs at least as much as each slot of the current stack, the bottom included. At level the crumb is a single slot, so the condition says that the new weight is at least the current maximum. Pops are unrestricted. The comparison is with the slots present at the time of the push, not with all slots ever pushed. The paper’s condition, “new leaves are added with increasing weights”, implies it, provided the bottom slot is no heavier than the first push.
theorem push_ordered {layers : Layers} {crumb bottom : WeightedTree} (ordered : (layers.decode bottom).Ordered) (newOrdered : crumb.Ordered) (dominates : ∀ a ∈ crumb.fringe, ∀ b ∈ (layers.decode bottom).fringe, b.2 ≤ a.2) : ((layers.push crumb).decode bottom).Ordered := RotationBST.PopTart.Cherry.push_ordered ordered newOrdered dominatestheorem push_leaf_ordered {layers : Layers} {bottom : WeightedTree} (ordered : (layers.decode bottom).Ordered) (subtree : Tree) (weight : ℝ) (nonnegative : 0 ≤ weight) (dominates : ∀ b ∈ (layers.decode bottom).fringe, b.2 ≤ weight) : ((layers.push (.leaf subtree weight)).decode bottom).Ordered := RotationBST.PopTart.Cherry.push_leaf_ordered ordered subtree weight nonnegative dominatestheorem pop_ordered {layers rest : Layers} {crumb bottom : WeightedTree} (ordered : (layers.decode bottom).Ordered) (popped : layers.pop = some (crumb, rest)) : crumb.Ordered ∧ (rest.decode bottom).Ordered := RotationBST.PopTart.Cherry.pop_ordered ordered poppedtheorem operation_ordered {level : Nat} {layers rest : Layers} (op : Operation level layers rest) {bottom : WeightedTree} (ordered : (layers.decode bottom).Ordered) (increasing : op.Increasing bottom) : (rest.decode bottom).Ordered := RotationBST.PopTart.Cherry.operation_ordered op ordered increasingtheorem sequence_ordered {level : Nat} {layers rest : Layers} {n : Nat} (program : Program level layers rest n) {bottom : WeightedTree} (ordered : (layers.decode bottom).Ordered) (increasing : program.Increasing bottom) : (rest.decode bottom).Ordered := RotationBST.PopTart.Cherry.sequence_ordered program ordered increasingThese lemmas show that the ordered invariant survives. Pushing an ordered, dominating crumb onto an ordered stack gives an ordered stack, and push_leaf_ordered is the single-slot case. A pop leaves an ordered crumb and an ordered rest. operation_ordered and sequence_ordered lift this to increasing operations and programs. Carries and borrows never reorder slots: they only regroup the fringe, as Results.push and Results.pop state.
Theorem (depth by rank). In a Cherry pop-tart, the -th slot of the fringe has depth at most .
theorem depth_rank {layers : Layers} (valid : Valid 0 layers) {bottom : WeightedTree} (base : Perfect 0 bottom) {subtree : Tree} {weight : ℝ} {depth : Nat} (slot : SlotAt (layers.decode bottom) subtree weight depth) : ∃ before after, (layers.decode bottom).fringe = before ++ (subtree, weight) :: after ∧ (depth : ℝ) ≤ 3 + 4 * Real.logb 2 ((before.length + 1 : Nat) : ℝ) := RotationBST.PopTart.Cherry.depth_rank valid base slotThe first slots of the fringe are in the first layers. The -th slot is in a layer of index at most , which gives the bound as in depth_log. This needs no weights at all.
Theorem (Lemma 3, depth). In a Cherry pop-tart whose fringe is ordered, a slot of weight at depth satisfies . If its payload is nonempty, the pointer machine reaches it from the root in exactly instructions.
theorem weighted_depth {layers : Layers} (valid : Valid 0 layers) {bottom : WeightedTree} (base : Perfect 0 bottom) (ordered : (layers.decode bottom).Ordered) {subtree : Tree} {weight : ℝ} {depth : Nat} (slot : SlotAt (layers.decode bottom) subtree weight depth) (positive : 0 < weight) : (depth : ℝ) ≤ 3 + 4 * Real.logb 2 ((layers.decode bottom).mass / weight) := RotationBST.PopTart.Cherry.weighted_depth valid base ordered slot positivetheorem weighted_access {layers : Layers} (valid : Valid 0 layers) {bottom : WeightedTree} (base : Perfect 0 bottom) (ordered : (layers.decode bottom).Ordered) {subtree : Tree} {weight : ℝ} {depth : Nat} (slot : SlotAt (layers.decode bottom) subtree weight depth) (positive : 0 < weight) (nonempty : subtree ≠ .nil) : ∃ cs : List Pointer.Frame, cs.length = depth ∧ Pointer.plug cs subtree = (layers.decode bottom).tree ∧ Pointer.Trace ⟨(layers.decode bottom).tree, []⟩ ⟨subtree, cs⟩ depth ∧ (depth : ℝ) ≤ 3 + 4 * Real.logb 2 ((layers.decode bottom).mass / weight) := RotationBST.PopTart.Cherry.weighted_access valid base ordered slot positive nonemptyThis combines depth_rank with Results.ordered_prefix_mass: the -th slot of an ordered fringe has .
Theorem (Lemma 3). Start from the empty Cherry pop-tart, with a single bottom slot of nonnegative weight. After any increasing program of single-slot operations, the stack is valid and ordered, every operation was realized, the total cost is at most , and every slot of weight has depth at most .
theorem weighted_sequence {rest : Layers} {n : Nat} (program : Program 0 .nil rest n) {bottom : WeightedTree} (base : Perfect 0 bottom) (bottomNonnegative : bottom.Nonnegative) (increasing : program.Increasing bottom) : Valid 0 rest ∧ (rest.decode bottom).Ordered ∧ program.Realizes bottom ∧ n + program.cost ≤ 11 * n ∧ ∀ subtree weight depth, SlotAt (rest.decode bottom) subtree weight depth → 0 < weight → (depth : ℝ) ≤ 3 + 4 * Real.logb 2 ((rest.decode bottom).mass / weight) := RotationBST.PopTart.Cherry.weighted_sequence program base bottomNonnegative increasingThe depth bound is stated for the final stack. It holds at every intermediate moment as well, since the intermediate stacks are again valid and ordered and weighted_depth applies to them. Lemma 3 does not claim a worst-case bound; worst_case above gives one anyway, in terms of the number of slots.
37.1.5 Located slots
A slot’s depth bounds the cost of reaching its payload. The heavy-path structures also need the rank of the payload’s keys in the physical tree, to say how deep a given key is. A located slot records it.
/-- Each preceding payload contributes its keys and the separator followingit. The final fringe has no following separator in the physical tree. -/def fringeSpan (slots : List (Tree × ℝ)) : Nat := (slots.map (fun p => p.1.size + 1)).sum/-- An outer fringe occurrence with both its physical inorder offset and itsouter edge depth. Equal-shaped, equal-weight payloads remain distinct by rank. -/inductive LocatedSlot : WeightedTree → Tree → ℝ → Nat → Nat → Prop where | leaf (t : Tree) (w : ℝ) : LocatedSlot (.leaf t w) t w 0 0 | left {l r : WeightedTree} {t : Tree} {w : ℝ} {offset depth : Nat} : LocatedSlot l t w offset depth → LocatedSlot (.node l r) t w offset (depth + 1) | right {l r : WeightedTree} {t : Tree} {w : ℝ} {offset depth : Nat} : LocatedSlot r t w offset depth → LocatedSlot (.node l r) t w (offset + l.tree.size + 1) (depth + 1)fringeSpan slots counts the physical keys spanned by a list of slots: each payload’s keys plus the separator that follows it. LocatedSlot t subtree weight offset depth is a SlotAt witness that also records offset, the number of keys of t.tree before the payload in inorder. Going right past a separator adds the left subtree’s keys and the separator itself, as in Pointer.offset.
theorem fringeSpan_append (xs ys : List (Tree × ℝ)) : fringeSpan (xs ++ ys) = fringeSpan xs + fringeSpan ys := RotationBST.PopTart.WeightedTree.fringeSpan_append xs ystheorem fringeSpan_fringe (t : WeightedTree) : fringeSpan t.fringe = t.tree.size + 1 := RotationBST.PopTart.WeightedTree.fringeSpan_fringe tThe span is additive, and the span of a whole fringe is one more than the number of keys, since the last slot has no separator after it.
theorem forget {t : WeightedTree} {subtree : Tree} {weight : ℝ} {offset depth : Nat} (slot : LocatedSlot t subtree weight offset depth) : SlotAt t subtree weight depth := RotationBST.PopTart.WeightedTree.LocatedSlot.forget slottheorem fits {t : WeightedTree} {subtree : Tree} {weight : ℝ} {offset depth : Nat} (slot : LocatedSlot t subtree weight offset depth) : offset + subtree.size ≤ t.tree.size := RotationBST.PopTart.WeightedTree.LocatedSlot.fits slottheorem split {t : WeightedTree} {subtree : Tree} {weight : ℝ} {offset depth : Nat} (slot : LocatedSlot t subtree weight offset depth) : ∃ before after, t.fringe = before ++ (subtree, weight) :: after ∧ fringeSpan before = offset := RotationBST.PopTart.WeightedTree.LocatedSlot.split slottheorem locate_split {t : WeightedTree} {before after : List (Tree × ℝ)} {subtree : Tree} {weight : ℝ} (split : t.fringe = before ++ (subtree, weight) :: after) : ∃ depth, LocatedSlot t subtree weight (fringeSpan before) depth := RotationBST.PopTart.WeightedTree.locate_split splittheorem context {t : WeightedTree} {subtree : Tree} {weight : ℝ} {offset depth : Nat} (slot : LocatedSlot t subtree weight offset depth) : ∃ cs : List Pointer.Frame, cs.length = depth ∧ Pointer.offset cs = offset ∧ Pointer.plug cs subtree = t.tree := RotationBST.PopTart.WeightedTree.LocatedSlot.context slotEvery located slot is a slot (forget), and its payload fits inside the tree (fits). Its offset is the span of the fringe entries before it (split), and conversely every entry of the fringe is a located slot with that offset (locate_split). context is the zipper path to the slot, of length depth and with Pointer.offset equal to offset.
Theorem (key depths in a located slot). If a located slot holds subtree at depth and offset , the key of the physical tree, for a key of subtree, has depth . The keys just after and just before the payload, when they exist, have depth less than .
theorem key_depth {t : WeightedTree} {subtree : Tree} {weight : ℝ} {offset depth : Nat} (slot : LocatedSlot t subtree weight offset depth) (x : Nat) (inside : x < subtree.size) : Tree.depth t.tree (offset + x) = depth + Tree.depth subtree x := RotationBST.PopTart.WeightedTree.LocatedSlot.key_depth slot x insidetheorem successor_depth {t : WeightedTree} {subtree : Tree} {weight : ℝ} {offset depth : Nat} (slot : LocatedSlot t subtree weight offset depth) (existsKey : offset + subtree.size < t.tree.size) : Tree.depth t.tree (offset + subtree.size) < depth := RotationBST.PopTart.WeightedTree.LocatedSlot.successor_depth slot existsKeytheorem predecessor_depth {t : WeightedTree} {subtree : Tree} {weight : ℝ} {offset depth : Nat} (slot : LocatedSlot t subtree weight offset depth) (existsKey : 0 < offset) : Tree.depth t.tree (offset - 1) < depth := RotationBST.PopTart.WeightedTree.LocatedSlot.predecessor_depth slot existsKeyThe keys at and are the separators adjacent to the slot, which are ancestors of it. In a heavy-path stack these separators are the path nodes themselves, and the payloads are the off-path subtrees. The bounds Results.lower_key_bound and Results.lower_separator_bound on the paired stacks of Section 38.1 are these lemmas combined with Chocolate’s weighted depth bound. Chocolate’s interruption lemmas (Section 37.2) use located slots in the same way, to bound key depths in the middle of an operation.
37.1.6 Mirrored pop-tarts
The paper notes that every pop-tart can be flipped to accept pushes in increasing key order. In a heavy path, the ancestors larger than the endpoint arrive in increasing order. The library does not define a second family of pop-tarts. It stores such a stack as an ordinary one, in reversed coordinates, and reflects it when it is decoded.
/-- Reflection reverses the active skeleton and the physical fringe payloads.To retain a payload's original orientation on the right stack, store itsreflection before reflecting the entire stack. -/def mirror : WeightedTree → WeightedTree | .leaf t w => .leaf t.mirror w | .node l r => .node r.mirror l.mirrormirror swaps the two children of every separator and reflects every payload with Tree.mirror, keeping the weights. The physical tree of the mirror is the mirror image of the physical tree. To keep a payload in its original orientation in the reflected stack, the caller stores its reflection: Stacks.push pushes r.erase.mirror onto the upper stack, and decoding the stack mirrors it back.
theorem involution (t : WeightedTree) : t.mirror.mirror = t := mirror_mirror ttheorem tree (t : WeightedTree) : t.mirror.tree = t.tree.mirror := mirror_tree ttheorem mass (t : WeightedTree) : t.mirror.mass = t.mass := mirror_mass ttheorem nonnegative {t : WeightedTree} (valid : t.Nonnegative) : t.mirror.Nonnegative := mirror_nonnegative validtheorem fringe (t : WeightedTree) : t.mirror.fringe = (t.fringe.map (fun p => (p.1.mirror, p.2))).reverse := mirror_fringe ttheorem slot {t : WeightedTree} {subtree : Tree} {weight : ℝ} {depth : Nat} (h : SlotAt t subtree weight depth) : SlotAt t.mirror subtree.mirror weight depth := h.mirrorMirroring is an involution and commutes with taking the physical tree. It preserves mass and nonnegativity. It reverses the fringe and reflects each payload. It maps a slot to a slot with the reflected payload, the same weight and the same depth. Every depth bound of a pop-tart therefore holds for its mirror image, with the same constants. This is how the paired stacks obtain their bounds for the upper stack, such as Results.upper_depth. Pointer traces are transferred the same way: a mirrored trace is a trace of the same length (Results.trace, Chapter 36), and the paired stacks obtain their upper pops from their lower ones by it.