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

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 O(1)O(1) amortized and O(log⁡n)O(\log n) worst-case time, and crazy good if moreover every leaf of weight ww has depth O(log⁡W/w)O(\log W/w), where WW 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.

Main result · Bose et al. 2012a
Vanilla pop-tarts are crazy good when pushed weights dominate

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 w≥Ww \ge W onto a valid stack of total weight WW 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 WW, a leaf of weight w>0w > 0 at depth dd satisfies

d  ≤  1+log⁡2Ww.d \;\le\; 1 + \log_2 \frac{W}{w}.

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 WW. 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
theoremPopTart.Vanilla.Results.push_validpropext · Classical.choice · Quot.soundStructures/PopTart/Vanilla/Abstract.lean:12
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 dominates
theoremPopTart.Vanilla.Results.pop_validpropext · Classical.choice · Quot.soundStructures/PopTart/Vanilla/Abstract.lean:20
theorem 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 exposed
theoremPopTart.Vanilla.Results.operationpropext · Classical.choice · Quot.soundStructures/PopTart/Vanilla/Abstract.lean:59
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 cs
theoremPopTart.Vanilla.Results.depth_logpropext · Classical.choice · Quot.soundStructures/PopTart/Vanilla/Abstract.lean:36
theorem 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 slotPositive
18 definitions in 3 chapters · reading list
Main result · Bose et al. 2012a
Cherry pop-tarts are good, and crazy good under increasing weights

Start a Cherry pop-tart from the empty stack, and apply any sequence of nn 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 10n10n. A stack with LL leaves has every leaf at depth at most 4log⁡2L4\log_2 L. Its next push costs at most 10log⁡2L10\log_2 L instructions, and its next pop at most 6log⁡2L6\log_2 L. In an ordinary pop-tart, whose leaves are empty, with nn nodes, every node has depth less than 4log⁡2(n+1)4\log_2(n+1). 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 10n10n, and in the final stack of total weight WW every leaf of weight w>0w > 0 has depth

d  ≤  3+4log⁡2Ww.d \;\le\; 3 + 4\log_2 \frac{W}{w}.

This is Lemma 3 of the paper. The lemma is stated with O(⋅)O(\cdot); its proof ends with 4log⁡2(W/w)4\log_2(W/w), which drops an additive constant: the heaviest leaf is at depth 11, while log⁡2(W/w)\log_2(W/w) can be 00. The Lean bound adds 33 for the spine nodes. The costs are those of Program.cost, and empty_sequence and weighted_sequence write the cost bound as n+cost≤11nn + \text{cost} \le 11n, which is cost≤10n\text{cost} \le 10n.

The Lean statements
theoremPopTart.Cherry.Results.empty_sequencepropext · Classical.choice · Quot.soundStructures/PopTart/Cherry/Abstract.lean:38
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 bottom
theoremPopTart.Cherry.Results.worst_casepropext · Classical.choice · Quot.soundStructures/PopTart/Cherry/Abstract.lean:67
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 base
theoremPopTart.Cherry.Results.depth_logpropext · Classical.choice · Quot.soundStructures/PopTart/Cherry/Abstract.lean:53
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 slot
theoremPopTart.Cherry.Results.key_depthpropext · Classical.choice · Quot.soundStructures/PopTart/Cherry/Abstract.lean:60
theorem 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 inside
theoremPopTart.Cherry.Results.weighted_sequencepropext · Classical.choice · Quot.soundStructures/PopTart/Cherry/Abstract.lean:130
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 increasing
36 definitions in 3 chapters · reading list

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

Do you need this section?

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 sjs_j 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 11 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 ww hypothesis
Vanilla 00 instructions ≤1+log⁡2(W/w)\le 1 + \log_2(W/w) each push dominates the mass below
Cherry ≤10\le 10 amortized; ≤10log⁡2L\le 10\log_2 L (push), ≤6log⁡2L\le 6\log_2 L (pop) worst case ≤4log⁡2L\le 4\log_2 L one-slot pushes
Cherry, weighted ≤10\le 10 amortized ≤3+4log⁡2(W/w)\le 3 + 4\log_2(W/w) each push weighs at least every current slot

Here WW is the total weight of the slots and LL their number.

37.1.2 Weighted trees

37.1.2.1 The type

inductivePopTart.WeightedTreepropext · Classical.choice · Quot.soundStructures/PopTart/Definitions/Abstract.lean:10
/-- 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.

skeleton 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 kk keys contributes kk keys to tree and none to the skeleton’s size.

defPopTart.WeightedTree.masspropext · Classical.choice · Quot.soundStructures/PopTart/Definitions/Abstract.lean:25
/-- Total weight of fringe slots; active separator nodes are not included. -/def mass : WeightedTree → ℝ  | .leaf _ w => w  | .node l r => l.mass + r.mass
defPopTart.WeightedTree.Nonnegativepropext · Classical.choice · Quot.soundStructures/PopTart/Definitions/Abstract.lean:30
def Nonnegative : WeightedTree → Prop  | .leaf _ w => 0 ≤ w  | .node l r => l.Nonnegative ∧ r.Nonnegative
defPopTart.WeightedTree.leafCountpropext · Classical.choice · Quot.soundStructures/PopTart/Definitions/Abstract.lean:34
/-- Number of outer fringe slots, independent of their contents and weights. -/def leafCount : WeightedTree → Nat  | .leaf _ _ => 1  | .node l r => leafCount l + leafCount r
defPopTart.WeightedTree.fringepropext · Classical.choice · Quot.soundStructures/PopTart/Definitions/Abstract.lean:39
/-- 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.fringe

mass is the total weight WW of the slots. Nonnegative says that every slot weight is nonnegative. leafCount is the number LL 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.

defPopTart.WeightedTree.heightpropext · Classical.choice · Quot.soundStructures/PopTart/Definitions/Abstract.lean:55
/-- 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
defPopTart.WeightedTree.EmptyFringepropext · Classical.choice · Quot.soundStructures/PopTart/Definitions/Abstract.lean:60
/-- 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.EmptyFringe

height counts separator levels only: a slot is a leaf of height 00, 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

inductivePopTart.WeightedTree.SlotAtpropext · Classical.choice · Quot.soundStructures/PopTart/Definitions/Abstract.lean:65
/-- 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:

theoremPopTart.WeightedTree.Results.slot_contextpropext · Classical.choice · Quot.soundStructures/PopTart/Abstract.lean:17
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 slot
theoremPopTart.WeightedTree.Results.slot_tracepropext · Classical.choice · Quot.soundStructures/PopTart/Abstract.lean:22
theorem 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 nonempty
theoremPopTart.WeightedTree.Results.slot_splitpropext · Classical.choice · Quot.soundStructures/PopTart/Abstract.lean:58
theorem 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 slot
theoremPopTart.WeightedTree.Results.slot_le_masspropext · Classical.choice · Quot.soundStructures/PopTart/Abstract.lean:12
theorem 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 nonnegative

slot_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 0≤w≤W0 \le w \le W in a nonnegative tree.

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

theoremPopTart.WeightedTree.Results.fringe_lengthpropext · Classical.choice · Quot.soundStructures/PopTart/Abstract.lean:48
theorem fringe_length (t : WeightedTree) : t.fringe.length = t.leafCount :=  RotationBST.PopTart.WeightedTree.fringe_length t
theoremPopTart.WeightedTree.Results.fringe_masspropext · Classical.choice · Quot.soundStructures/PopTart/Abstract.lean:51
theorem fringe_mass (t : WeightedTree) : (t.fringe.map Prod.snd).sum = t.mass :=  RotationBST.PopTart.WeightedTree.fringe_mass t
theoremPopTart.WeightedTree.Results.fringe_nonnegativepropext · Classical.choice · Quot.soundStructures/PopTart/Abstract.lean:54
theorem fringe_nonnegative (t : WeightedTree) :    t.Nonnegative ↔ ∀ p ∈ t.fringe, 0 ≤ p.2 :=  RotationBST.PopTart.WeightedTree.fringe_nonnegative t
theoremPopTart.WeightedTree.Results.mass_nonnegativepropext · Classical.choice · Quot.soundStructures/PopTart/Abstract.lean:9
theorem mass_nonnegative {t : WeightedTree} (nonnegative : t.Nonnegative) : 0 ≤ t.mass :=  RotationBST.PopTart.WeightedTree.mass_nonnegative nonnegative

The 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

inductivePopTart.WeightedTree.Perfectpropext · Classical.choice · Quot.soundStructures/PopTart/Definitions/Abstract.lean:49
/-- 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 ii, an ii-crumb in the paper, is a perfect weighted tree of height ii. Its slots are arbitrary: Cherry fills them with single leaves, and Chocolate may fill them with frozen subtrees.

theoremPopTart.WeightedTree.Results.perfect_countpropext · Classical.choice · Quot.soundStructures/PopTart/Abstract.lean:28
theorem perfect_count {level : Nat} {crumb : WeightedTree} (perfect : Perfect level crumb) :    leafCount crumb = 2 ^ level :=  RotationBST.PopTart.WeightedTree.perfect_count perfect
theoremPopTart.WeightedTree.Results.perfect_depthpropext · Classical.choice · Quot.soundStructures/PopTart/Abstract.lean:32
theorem 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 slot

An ii-crumb has 2i2^i slots, all at depth ii.

37.1.2.4 Ordered fringes

defPopTart.WeightedTree.Orderedpropext · Classical.choice · Quot.soundStructures/PopTart/Definitions/Abstract.lean:44
/-- 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.

theoremPopTart.WeightedTree.Results.ordered_appendpropext · Classical.choice · Quot.soundStructures/PopTart/Abstract.lean:63
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 split
theoremPopTart.WeightedTree.Results.ordered_prefix_masspropext · Classical.choice · Quot.soundStructures/PopTart/Abstract.lean:68
theorem 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 split

ordered_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 ww is the kk-th entry of an ordered fringe, the kk entries up to it each weigh at least ww, so

k w  ≤  W.k\,w \;\le\; W .

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.

inductivePopTart.Vanilla.Validpropext · Classical.choice · Quot.soundStructures/PopTart/Vanilla/Definitions/Abstract.lean:8
/-- 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 ww onto a valid stack tail is valid when W(tail)≤wW(\mathit{tail}) \le w, that is, when the new slot weighs at least as much as all the others together. Equality is allowed, as in Lemma 1.

defPopTart.Vanilla.pushpropext · Classical.choice · Quot.soundStructures/PopTart/Vanilla/Definitions/Abstract.lean:15
def push (t : Tree) (w : ℝ) (stack : WeightedTree) : WeightedTree :=  .node (.leaf t w) stack
defPopTart.Vanilla.poppropext · Classical.choice · Quot.soundStructures/PopTart/Vanilla/Definitions/Abstract.lean:18
/-- 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)  | _ => none

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

inductivePopTart.Vanilla.Operationpropext · Classical.choice · Quot.soundStructures/PopTart/Vanilla/Definitions/Abstract.lean:24
/-- 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 tail
defPopTart.Vanilla.Operation.inputpropext · Classical.choice · Quot.soundStructures/PopTart/Vanilla/Definitions/Abstract.lean:33
def input {stack tail : WeightedTree} : Operation stack tail → Tree  | .push t _ stack => .node t stack.tree  | .pop stack _ _ _ _ => stack.tree
defPopTart.Vanilla.Operation.outputpropext · Classical.choice · Quot.soundStructures/PopTart/Vanilla/Definitions/Abstract.lean:37
def output {stack tail : WeightedTree} : Operation stack tail → Tree  | .push _ _ _ => tail.tree  | .pop _ tail t _ _ => .node t tail.tree
defPopTart.Vanilla.Operation.Realizespropext · Classical.choice · Quot.soundStructures/PopTart/Vanilla/Definitions/Abstract.lean:41
def Realizes {stack tail : WeightedTree} (op : Operation stack tail) (cost : Nat) : Prop :=  Pointer.Trace ⟨op.input, []⟩ ⟨op.output, []⟩ cost

An 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

theoremPopTart.Vanilla.Results.push_validpropext · Classical.choice · Quot.soundStructures/PopTart/Vanilla/Abstract.lean:12
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 dominates
theoremPopTart.Vanilla.Results.pop_validpropext · Classical.choice · Quot.soundStructures/PopTart/Vanilla/Abstract.lean:20
theorem 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 exposed

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

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

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

The context cs places the pop-tart anywhere inside a larger tree. Since input and output coincide, the empty trace works. The paper’s “O(1)O(1) 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 WW, a slot of weight ww at depth dd satisfies 2dw≤2W2^d w \le 2W. If w>0w > 0, then d≤1+log⁡2(W/w)d \le 1 + \log_2(W/w).

theoremPopTart.Vanilla.Results.depth_masspropext · Classical.choice · Quot.soundStructures/PopTart/Vanilla/Abstract.lean:30
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 slot
theoremPopTart.Vanilla.Results.depth_logpropext · Classical.choice · Quot.soundStructures/PopTart/Vanilla/Abstract.lean:36
theorem 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 slotPositive

The proof is the paper’s induction. The top slot is at depth 11 and weighs at least W/2W/2. Every other slot lies in the tail, whose mass is at most W/2W/2, one level deeper. The base case, a single slot at depth 00, gives w≤2Ww \le 2W.

theoremPopTart.Vanilla.Results.access_slotpropext · Classical.choice · Quot.soundStructures/PopTart/Vanilla/Abstract.lean:42
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 nonempty

access_slot combines this bound with Results.slot_trace: a slot with a nonempty payload is reached from the root by exactly dd pointer moves, with d≤1+log⁡2(W/w)d \le 1 + \log_2(W/w). 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 ii holds one to three ii-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 00-crumb to layer 00. 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 00-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

inductivePopTart.Cherry.Layerspropext · Classical.choice · Quot.soundStructures/PopTart/Cherry/Definitions/Abstract.lean:9
/-- 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)
defPopTart.Cherry.Layers.decodepropext · Classical.choice · Quot.soundStructures/PopTart/Cherry/Definitions/Abstract.lean:18
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)))
defPopTart.Cherry.Layers.heightpropext · Classical.choice · Quot.soundStructures/PopTart/Cherry/Definitions/Abstract.lean:24
def height : Layers → Nat  | .nil => 0  | .one _ tail | .two _ _ tail | .three _ _ _ tail => tail.height + 1

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

inductivePopTart.Cherry.Validpropext · Classical.choice · Quot.soundStructures/PopTart/Cherry/Definitions/Abstract.lean:64
/-- 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 00 with a single-slot bottom, Perfect 0 bottom. The general level is needed for the recursion: a carry from layer ii is a push at level i+1i+1.

37.1.4.2 Push and pop

defPopTart.Cherry.Layers.pushpropext · Classical.choice · Quot.soundStructures/PopTart/Cherry/Definitions/Abstract.lean:35
/-- 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))
defPopTart.Cherry.Layers.poppropext · Classical.choice · Quot.soundStructures/PopTart/Cherry/Definitions/Abstract.lean:42
/-- 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.

defPopTart.Cherry.Layers.pushCostpropext · Classical.choice · Quot.soundStructures/PopTart/Cherry/Definitions/Abstract.lean:53
def pushCost : Layers → Nat  | .three _ _ _ tail => 10 + tail.pushCost  | _ => 0
defPopTart.Cherry.Layers.popCostpropext · Classical.choice · Quot.soundStructures/PopTart/Cherry/Definitions/Abstract.lean:57
def popCost : Layers → Nat  | .one _ .nil => 0  | .one _ tail => 6 + tail.popCost  | _ => 0
defPopTart.Cherry.Layers.potentialpropext · Classical.choice · Quot.soundStructures/PopTart/Cherry/Definitions/Abstract.lean:28
/-- 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.potential

pushCost charges 1010 instructions for each carry, and popCost charges 66 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: 1010 for each layer with one or three crumbs, 00 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 1010 units that pay for it.

37.1.4.3 Operations and programs

inductivePopTart.Cherry.Operationpropext · Classical.choice · Quot.soundStructures/PopTart/Cherry/Definitions/Abstract.lean:76
/-- 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 rest
defPopTart.Cherry.Operation.costpropext · Classical.choice · Quot.soundStructures/PopTart/Cherry/Definitions/Abstract.lean:86
def cost {level : Nat} {layers rest : Layers} : Operation level layers rest → Nat  | .push _ _ _ => layers.pushCost  | .pop _ _ _ _ => layers.popCost
defPopTart.Cherry.Operation.inputpropext · Classical.choice · Quot.soundStructures/PopTart/Cherry/Definitions/Abstract.lean:90
def 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).tree
defPopTart.Cherry.Operation.outputpropext · Classical.choice · Quot.soundStructures/PopTart/Cherry/Definitions/Abstract.lean:94
def 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).tree

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

inductivePopTart.Cherry.Programpropext · Classical.choice · Quot.soundStructures/PopTart/Cherry/Definitions/Abstract.lean:109
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)
defPopTart.Cherry.Program.costpropext · Classical.choice · Quot.soundStructures/PopTart/Cherry/Definitions/Abstract.lean:117
def cost {level : Nat} {layers rest : Layers} {n : Nat} : Program level layers rest n → Nat  | .nil _ => 0  | .cons op tail => op.cost + tail.cost
inductivePopTart.Cherry.Program.Realizespropext · Classical.choice · Quot.soundStructures/PopTart/Cherry/Definitions/Abstract.lean:126
/-- 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 nn 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 1010. 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 1010.

theoremPopTart.Cherry.Results.pushpropext · Classical.choice · Quot.soundStructures/PopTart/Cherry/Abstract.lean:10
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 bottom
theoremPopTart.Cherry.Results.poppropext · Classical.choice · Quot.soundStructures/PopTart/Cherry/Abstract.lean:19
theorem 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 bottom

Amortized cost means actual cost plus the potential after, which is at most 1010 plus the potential before.

Each 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 nn operations from valid layers ends in valid layers, is realized, and costs at most 10n10n plus the initial potential minus the final one. From the empty stack, the total cost is at most 10n10n.

theoremPopTart.Cherry.Results.sequencepropext · Classical.choice · Quot.soundStructures/PopTart/Cherry/Abstract.lean:33
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 bottom
theoremPopTart.Cherry.Results.empty_sequencepropext · Classical.choice · Quot.soundStructures/PopTart/Cherry/Abstract.lean:38
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 bottom

empty_sequence writes the bound as n+cost≤11nn + \text{cost} \le 11n, that is, cost≤10n\text{cost} \le 10n. 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 jj holds at least one jj-crumb, with 2j2^j slots, so there are at most log⁡2L\log_2 L layers.

layer_population is the counting statement at any level, 2ℓ+h+1≤L+2ℓ2^{\ell + h} + 1 \le L + 2^{\ell} for hh layers starting at level ℓ\ell. height_log specializes it to level 00: h≤log⁡2Lh \le \log_2 L.

Theorem (depth). In a Cherry pop-tart with LL slots, every slot has depth at most 4log⁡2L4\log_2 L. In an ordinary Cherry pop-tart with nn nodes, every node has depth less than 4log⁡2(n+1)4\log_2(n+1).

theoremPopTart.Cherry.Results.depth_logpropext · Classical.choice · Quot.soundStructures/PopTart/Cherry/Abstract.lean:53
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 slot
theoremPopTart.Cherry.Results.key_depthpropext · Classical.choice · Quot.soundStructures/PopTart/Cherry/Abstract.lean:60
theorem 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 inside

For the proof: a slot in layer jj lies below at most 3(j+1)3(j+1) spine nodes and jj crumb levels, and j<h≤log⁡2Lj < h \le \log_2 L. The key version uses Results.empty_count: an ordinary pop-tart with nn keys has n+1n+1 slots.

Theorem (worst case). In a Cherry pop-tart with LL slots, the next push costs at most 10log⁡2L10\log_2 L instructions and the next pop at most 6log⁡2L6\log_2 L.

theoremPopTart.Cherry.Results.worst_casepropext · Classical.choice · Quot.soundStructures/PopTart/Cherry/Abstract.lean:67
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 base

A carry or borrow can ripple through every layer, but there are at most log⁡2L\log_2 L 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.

defPopTart.Cherry.Operation.Increasingpropext · Classical.choice · Quot.soundStructures/PopTart/Cherry/Definitions/Abstract.lean:98
/-- 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 _ _ _ _ => True
defPopTart.Cherry.Program.Increasingpropext · Classical.choice · Quot.soundStructures/PopTart/Cherry/Definitions/Abstract.lean:121
def 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 bottom

A 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 00 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.

theoremPopTart.Cherry.Results.push_orderedpropext · Classical.choice · Quot.soundStructures/PopTart/Cherry/Abstract.lean:81
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 dominates
theoremPopTart.Cherry.Results.push_leaf_orderedpropext · Classical.choice · Quot.soundStructures/PopTart/Cherry/Abstract.lean:87
theorem 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 dominates
theoremPopTart.Cherry.Results.pop_orderedpropext · Classical.choice · Quot.soundStructures/PopTart/Cherry/Abstract.lean:94
theorem 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 popped
theoremPopTart.Cherry.Results.operation_orderedpropext · Classical.choice · Quot.soundStructures/PopTart/Cherry/Abstract.lean:99
theorem 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 increasing
theoremPopTart.Cherry.Results.sequence_orderedpropext · Classical.choice · Quot.soundStructures/PopTart/Cherry/Abstract.lean:105
theorem 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 increasing

These 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 kk-th slot of the fringe has depth at most 3+4log⁡2k3 + 4\log_2 k.

theoremPopTart.Cherry.Results.depth_rankpropext · Classical.choice · Quot.soundStructures/PopTart/Cherry/Abstract.lean:73
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 slot

The first slots of the fringe are in the first layers. The kk-th slot is in a layer of index at most log⁡2k\log_2 k, 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 w>0w > 0 at depth dd satisfies d≤3+4log⁡2(W/w)d \le 3 + 4\log_2(W/w). If its payload is nonempty, the pointer machine reaches it from the root in exactly dd instructions.

theoremPopTart.Cherry.Results.weighted_depthpropext · Classical.choice · Quot.soundStructures/PopTart/Cherry/Abstract.lean:111
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 positive
theoremPopTart.Cherry.Results.weighted_accesspropext · Classical.choice · Quot.soundStructures/PopTart/Cherry/Abstract.lean:119
theorem 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 nonempty

This combines depth_rank with Results.ordered_prefix_mass: the kk-th slot of an ordered fringe has k≤W/wk \le W/w.

Theorem (Lemma 3). Start from the empty Cherry pop-tart, with a single bottom slot of nonnegative weight. After any increasing program of nn single-slot operations, the stack is valid and ordered, every operation was realized, the total cost is at most 10n10n, and every slot of weight w>0w > 0 has depth at most 3+4log⁡2(W/w)3 + 4\log_2(W/w).

theoremPopTart.Cherry.Results.weighted_sequencepropext · Classical.choice · Quot.soundStructures/PopTart/Cherry/Abstract.lean:130
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 increasing

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

defPopTart.WeightedTree.fringeSpanpropext · Classical.choice · Quot.soundStructures/PopTart/Located/Definitions/Abstract.lean:8
/-- 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
inductivePopTart.WeightedTree.LocatedSlotpropext · Classical.choice · Quot.soundStructures/PopTart/Located/Definitions/Abstract.lean:13
/-- 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.

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

theoremPopTart.Located.Results.forgetpropext · Classical.choice · Quot.soundStructures/PopTart/Located/Abstract.lean:17
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 slot
theoremPopTart.Located.Results.fitspropext · Classical.choice · Quot.soundStructures/PopTart/Located/Abstract.lean:21
theorem 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 slot
theoremPopTart.Located.Results.splitpropext · Classical.choice · Quot.soundStructures/PopTart/Located/Abstract.lean:25
theorem 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 slot
theoremPopTart.Located.Results.locate_splitpropext · Classical.choice · Quot.soundStructures/PopTart/Located/Abstract.lean:30
theorem 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 split
theoremPopTart.Located.Results.contextpropext · Classical.choice · Quot.soundStructures/PopTart/Located/Abstract.lean:50
theorem 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 slot

Every 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 dd and offset oo, the key o+xo + x of the physical tree, for xx a key of subtree, has depth d+depthsubtree(x)d + \mathrm{depth}_{\mathit{subtree}}(x). The keys just after and just before the payload, when they exist, have depth less than dd.

theoremPopTart.Located.Results.key_depthpropext · Classical.choice · Quot.soundStructures/PopTart/Located/Abstract.lean:35
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 inside
theoremPopTart.Located.Results.successor_depthpropext · Classical.choice · Quot.soundStructures/PopTart/Located/Abstract.lean:40
theorem 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 existsKey
theoremPopTart.Located.Results.predecessor_depthpropext · Classical.choice · Quot.soundStructures/PopTart/Located/Abstract.lean:45
theorem 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 existsKey

The keys at o+∣subtree∣o + |\mathit{subtree}| and o−1o - 1 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.

defPopTart.WeightedTree.mirrorpropext · Classical.choice · Quot.soundStructures/PopTart/Mirror/Definitions/Abstract.lean:9
/-- 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.mirror

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

theoremPopTart.Mirror.Results.involutionpropext · Classical.choice · Quot.soundStructures/PopTart/Mirror/Abstract.lean:10
theorem involution (t : WeightedTree) : t.mirror.mirror = t := mirror_mirror t
theoremPopTart.Mirror.Results.treepropext · Classical.choice · Quot.soundStructures/PopTart/Mirror/Abstract.lean:11
theorem tree (t : WeightedTree) : t.mirror.tree = t.tree.mirror := mirror_tree t
theoremPopTart.Mirror.Results.masspropext · Classical.choice · Quot.soundStructures/PopTart/Mirror/Abstract.lean:12
theorem mass (t : WeightedTree) : t.mirror.mass = t.mass := mirror_mass t
theoremPopTart.Mirror.Results.nonnegativepropext · Classical.choice · Quot.soundStructures/PopTart/Mirror/Abstract.lean:13
theorem nonnegative {t : WeightedTree} (valid : t.Nonnegative) : t.mirror.Nonnegative :=  mirror_nonnegative valid
theoremPopTart.Mirror.Results.fringepropext · Classical.choice · Quot.soundStructures/PopTart/Mirror/Abstract.lean:15
theorem fringe (t : WeightedTree) :    t.mirror.fringe = (t.fringe.map (fun p => (p.1.mirror, p.2))).reverse := mirror_fringe t
theoremPopTart.Mirror.Results.slotpropext · Classical.choice · Quot.soundStructures/PopTart/Mirror/Abstract.lean:17
theorem slot {t : WeightedTree} {subtree : Tree} {weight : ℝ} {depth : Nat}    (h : SlotAt t subtree weight depth) : SlotAt t.mirror subtree.mirror weight depth := h.mirror

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