Chapter 38 · Heavy paths · Section 38.1One heavy path
The deamortization of Chapter 30 runs an arbitrary source BST algorithm inside a tree that it keeps balanced in a weighted sense: whatever shape the source tree takes, every key stays at depth in the tree that is actually stored, where is the key’s weight and the total weight. This is Theorem 5 of Bose, Collette, Fagerberg and Langerman (2012a), and its formal counterpart is Results.simulation. The simulation stores the source tree through its heavy-path decomposition. Each heavy path is kept as a pair of pop-tarts, the weighted stacks of Section 37.1 and Section 37.2. The subtrees hanging off a path are stored inside the pop-tarts as weighted leaves, and they are represented in the same way, recursively.
This section is the first of four on that representation. It covers the decomposition itself and the representation of a single heavy path by a pair of pop-tarts, with its off-path subtrees treated as opaque payloads.
Let be a tree whose nodes carry weights, and let be its total weight. Each node has a solid edge to its heavier child (the left child on a tie); its other child edge is light.
- If all weights are nonnegative, every node with is reached from the root along a path with at most light edges.
- If all weights are positive and is nonempty, the solid path from the root ends at a leaf . Let be its number of edges. Starting at the root, the pointer machine turns into a tree with at the root, a valid Chocolate pop-tart of the path’s nodes smaller than as its left subtree, and a mirrored one of the larger nodes as its right subtree. Each path node enters its pop-tart together with its off-path subtree, as one leaf of weight equal to the node’s weight plus the subtree’s. The cost of the trace plus the potential of the two pop-tarts is at most , and , where is the total leaf weight of . The rank at the root of is the rank of in .
- In every valid pair of leaf weight , a key inside a leaf of weight has depth at most
where is its depth inside the leaf’s subtree.
Item 1 is path_exists with light_bound, item 2 is extract_build and item 3 is located_key_bound. Depths count edges, and ranks are inorder positions from .
The Lean statements
theorem path_exists {α : Type} (weight : α → ℝ) {t : LabelledTree α} {x : α} (mem : x ∈ t.inorder) : ∃ lights depth, Path weight t x lights depth := … RotationBST.HeavyPath.path_exists weight memtheorem light_bound {α : Type} {weight : α → ℝ} {t : LabelledTree α} {x : α} {lights depth : Nat} (path : Path weight t x lights depth) (nonnegative : Nonnegative weight t) (positive : 0 < weight x) : (lights : ℝ) ≤ Real.logb 2 (mass weight t / weight x) := … RotationBST.HeavyPath.light_bound path nonnegative positivetheorem extract_build {α : Type} {weight : α → ℝ} {t : LabelledTree α} (positive : Positive weight t) (nonempty : t ≠ .nil) : let q := … extract weight t let p := Stacks.build weight q.context Stacks.empty ∃ x cost, q.focus = .node .nil x .nil ∧ p.Valid ∧ weight x + p.mass = HeavyPath.mass weight t ∧ Pointer.Trace ⟨t.erase, []⟩ ⟨p.tree, []⟩ cost ∧ cost + p.potential ≤ 24 * q.context.length ∧ (Pointer.Config.mk p.tree []).key? = q.erase.key? := RotationBST.HeavyPath.Paired.extract_build positive nonemptyModels.Rotation, Models.Pointer.Machine, Models.Pointer.Mirror.Definitions, Models.Pointer.Lazy.Skeleton, Models.Labelled.Cursor.Definitions, Models.Labelled.Ordering.Definitions, Structures.PopTart.Definitions, Structures.PopTart.Mirror.Definitions, Structures.PopTart.Chocolate.Definitions, Structures.HeavyPath.Definitions, Structures.HeavyPath.Paired.Definitions, Structures.PopTart.Located.Definitions, Bounds.StaticTree.Definitionstheorem located_key_bound {p : Stacks} (valid : p.Valid) {subtree : Tree} {weight : ℝ} {offset depth : Nat} (slot : LocatedSlot (.node p.lower.decode p.upper.decode.mirror) subtree weight offset depth) (positive : 0 < weight) (x : Nat) (inside : x < subtree.size) : (Tree.depth p.tree (offset + x) : ℝ) ≤ 8 + 7 * Real.logb 2 (p.mass / weight) + Tree.depth subtree x := … RotationBST.HeavyPath.Paired.located_key_bound valid slot positive x insideModels.Rotation, Models.Pointer.Machine, Models.Pointer.Mirror.Definitions, Models.Pointer.Lazy.Skeleton, Models.Labelled.Cursor.Definitions, Models.Labelled.Ordering.Definitions, Structures.PopTart.Definitions, Structures.PopTart.Mirror.Definitions, Structures.PopTart.Chocolate.Definitions, Structures.HeavyPath.Definitions, Structures.HeavyPath.Paired.Definitions, Structures.PopTart.Located.Definitions, Bounds.StaticTree.DefinitionsThe headline is the step that the paper iterates. Item 3 bounds the depth of a key inside one off-path subtree by its depth in that subtree plus . Applied once per heavy path along the way to a key, the logarithms telescope to . That recursion is Section 38.2.
Only to follow the proof of the deamortization. The intermediate simulation theorem Results.simulation uses HeavyPath.mass and HeavyPath.Positive from this section, together with the policies of Section 38.4. A reader who only wants to understand that statement needs the section on the decomposition below and can skip the rest. The definitions are in Structures/HeavyPath/Definitions/Abstract.lean and Structures/HeavyPath/Paired/**/Definitions/Abstract.lean, the theorems in the Abstract.lean files next to them.
38.1.1 The heavy-path decomposition
The source tree is a LabelledTree, whose nodes carry labels of an arbitrary type α (in the deamortization, the labels are inorder ranks). A weight function weight : α → ℝ gives each label a weight. The library never stores weights in the tree, and introduces no new tree type. Positions in the tree are described with the labelled zippers of Chapter 36: a LabelledCursor.Frame is one ancestor, .left x r when the focus is the left child of the node labelled x with right subtree r, and .right l x symmetrically. A LabelledCursor.Config is a focus subtree with its list of frames, innermost first.
38.1.1.1 Weights and mass
/-- Analysis weights belong to labelled nodes; no additional tree shape isintroduced. Inorder ranks can be used as the labels of an ordinary BST. -/def mass {α : Type} (weight : α → ℝ) : LabelledTree α → ℝ | .nil => 0 | .node l x r => mass weight l + weight x + mass weight rdef Nonnegative {α : Type} (weight : α → ℝ) : LabelledTree α → Prop | .nil => True | .node l x r => Nonnegative weight l ∧ 0 ≤ weight x ∧ Nonnegative weight rdef Positive {α : Type} (weight : α → ℝ) : LabelledTree α → Prop | .nil => True | .node l x r => Positive weight l ∧ 0 < weight x ∧ Positive weight rmass weight t is the total weight of the nodes of t. The paper writes for the mass of the whole tree. Nonnegative and Positive ask that every node of t have nonnegative, or positive, weight. Positivity is the standing hypothesis of the decomposition theorems below. The deamortization assumes it for the source tree, and with unit weights it holds trivially.
theorem mass_nonnegative {α : Type} {weight : α → ℝ} {t : LabelledTree α} (nonnegative : Nonnegative weight t) : 0 ≤ mass weight t := RotationBST.HeavyPath.mass_nonnegative nonnegativetheorem mass_positive {α : Type} {weight : α → ℝ} {t : LabelledTree α} (positive : Positive weight t) (nonempty : t ≠ .nil) : 0 < mass weight t := RotationBST.HeavyPath.mass_positive positive nonemptytheorem unit_positive {α : Type} (t : LabelledTree α) : Positive (fun _ => 1) t := RotationBST.HeavyPath.unit_positive ttheorem unit_mass {α : Type} (t : LabelledTree α) : mass (fun _ => 1) t = (t.erase.size : ℝ) := RotationBST.HeavyPath.unit_mass tNonnegative weights give a nonnegative mass, and positive weights give a nonempty tree a positive mass. Unit weights are positive, and with unit weights the mass is the number of nodes, t.erase.size, where LabelledTree.erase forgets the labels.
38.1.1.2 Solid and light edges
/-- A node's solid edge goes to its heavier child; ties go left. -/def LeftHeavy {α : Type} (weight : α → ℝ) (l r : LabelledTree α) : Prop := mass weight r ≤ mass weight lLeftHeavy weight l r says that the solid edge of a node with subtrees l and r goes left: the left subtree is at least as heavy as the right one. Ties go left, as in the paper. The relation compares masses, so with zero weights an empty child can be “heavy”.
theorem right_light_half {α : Type} {weight : α → ℝ} {l r : LabelledTree α} {x : α} (nonnegative : Nonnegative weight (.node l x r)) (heavy : LeftHeavy weight l r) : 2 * mass weight r ≤ mass weight (.node l x r) := RotationBST.HeavyPath.right_light_half nonnegative heavytheorem left_light_half {α : Type} {weight : α → ℝ} {l r : LabelledTree α} {x : α} (nonnegative : Nonnegative weight (.node l x r)) (heavy : ¬ LeftHeavy weight l r) : 2 * mass weight l ≤ mass weight (.node l x r) := RotationBST.HeavyPath.left_light_half nonnegative heavyThese two lemmas are the reason heavy paths work. If the solid edge goes left, the right subtree has at most half the mass of the node’s whole subtree, and symmetrically. So each light edge halves the mass, at least.
/-- A particular original node occurrence, with its total edge depth andnumber of dotted (non-heavy) edges recorded separately. -/inductive Path {α : Type} (weight : α → ℝ) : LabelledTree α → α → Nat → Nat → Prop where | root (l r : LabelledTree α) (x : α) : Path weight (.node l x r) x 0 0 | leftSolid {l r : LabelledTree α} {x y : α} {lights depth : Nat} (found : Path weight l y lights depth) (heavy : LeftHeavy weight l r) : Path weight (.node l x r) y lights (depth + 1) | leftLight {l r : LabelledTree α} {x y : α} {lights depth : Nat} (found : Path weight l y lights depth) (heavy : ¬ LeftHeavy weight l r) : Path weight (.node l x r) y (lights + 1) (depth + 1) | rightSolid {l r : LabelledTree α} {x y : α} {lights depth : Nat} (found : Path weight r y lights depth) (heavy : ¬ LeftHeavy weight l r) : Path weight (.node l x r) y lights (depth + 1) | rightLight {l r : LabelledTree α} {x y : α} {lights depth : Nat} (found : Path weight r y lights depth) (heavy : LeftHeavy weight l r) : Path weight (.node l x r) y (lights + 1) (depth + 1)Path weight t y lights depth records one root-to-node path in t, ending at a node labelled y. depth counts its edges and lights counts its light edges. Each constructor takes one step down and records whether that step followed the solid edge (leftSolid, rightSolid) or not (leftLight, rightLight). The relation follows a particular occurrence of the label, so repeated labels cause no ambiguity.
theorem path_exists {α : Type} (weight : α → ℝ) {t : LabelledTree α} {x : α} (mem : x ∈ t.inorder) : ∃ lights depth, Path weight t x lights depth := RotationBST.HeavyPath.path_exists weight memtheorem lights_le_depth {α : Type} {weight : α → ℝ} {t : LabelledTree α} {x : α} {lights depth : Nat} (path : Path weight t x lights depth) : lights ≤ depth := RotationBST.HeavyPath.lights_le_depth paththeorem path_mass {α : Type} {weight : α → ℝ} {t : LabelledTree α} {x : α} {lights depth : Nat} (path : Path weight t x lights depth) (nonnegative : Nonnegative weight t) : (2 : ℝ) ^ lights * weight x ≤ mass weight t := RotationBST.HeavyPath.path_mass path nonnegativetheorem light_bound {α : Type} {weight : α → ℝ} {t : LabelledTree α} {x : α} {lights depth : Nat} (path : Path weight t x lights depth) (nonnegative : Nonnegative weight t) (positive : 0 < weight x) : (lights : ℝ) ≤ Real.logb 2 (mass weight t / weight x) := RotationBST.HeavyPath.light_bound path nonnegative positivetheorem unit_light_bound {α : Type} {t : LabelledTree α} {x : α} {lights depth : Nat} (path : Path (fun _ => 1) t x lights depth) : (lights : ℝ) ≤ Real.logb 2 t.erase.size := RotationBST.HeavyPath.unit_light_bound pathTheorem (light edges). With nonnegative weights, if is reached by a path with light edges, then . Hence, if , then . With unit weights, .
Every label occurring in the tree has such a path (path_exists). path_mass is the halving argument iterated, and light_bound takes its logarithm. This is the paper’s remark that “any path from the root to a node of weight uses at most dotted edges”.
theorem path_context {α : Type} {weight : α → ℝ} {t : LabelledTree α} {x : α} {lights depth : Nat} (path : Path weight t x lights depth) : ∃ l r cs, plug cs (.node l x r) = t ∧ cs.length = depth := RotationBST.HeavyPath.path_context paththeorem path_access {α : Type} {weight : α → ℝ} {t : LabelledTree α} {x : α} {lights depth : Nat} (path : Path weight t x lights depth) (nonnegative : Nonnegative weight t) (positive : 0 < weight x) : ∃ l r cs, plug cs (.node l x r) = t ∧ cs.length = depth ∧ Pointer.Trace ⟨t.erase, []⟩ ⟨.node l.erase r.erase, cs.map Frame.erase⟩ depth ∧ (lights : ℝ) ≤ Real.logb 2 (mass weight t / weight x) := RotationBST.HeavyPath.path_access path nonnegative positiveThese connect a Path to the zippers and to the pointer machine. A path of depth to y gives a context cs of frames that plugs the subtree rooted at y back into t. path_access adds a Pointer.Trace of exactly instructions, the descent from the root of the erased tree to that node, together with the light-edge bound.
38.1.1.3 The heavy path from the root
/-- Frames run from the endpoint back toward the root, as in the existinglabelled cursor. Every selected edge must obey the canonical heavy choice. -/def SolidContext {α : Type} (weight : α → ℝ) : LabelledTree α → List (Frame α) → Prop | _, [] => True | t, .left x r :: cs => LeftHeavy weight t r ∧ SolidContext weight (.node t x r) cs | t, .right l x :: cs => ¬ LeftHeavy weight l t ∧ SolidContext weight (.node l x t) csSolidContext weight focus cs says that every frame of cs follows the solid edge: going up from focus, each ancestor’s other subtree is no heavier (for a .left frame) or strictly lighter (for a .right frame). These are exactly the LeftHeavy conventions, with the tie broken to the left.
/-- Weight removed from the active path by a frame: its separator node plusits off-path subtree. These charges partition the original node mass. -/def frameMass {α : Type} (weight : α → ℝ) : Frame α → ℝ | .left x r => weight x + mass weight r | .right l x => mass weight l + weight xdef frameKey {α : Type} : Frame α → α | .left x _ => x | .right _ x => xA frame of a heavy path is the unit that goes into a pop-tart. frameKey is its separator, the ancestor’s label. frameMass is its charge: the separator’s weight plus the mass of its off-path subtree. This is the weight of the pop-tart leaf that will hold the frame. The charges of the frames along a path, plus the mass of the focus, add up to the mass of the whole tree, so each node’s weight is charged exactly once.
/-- Path separators smaller than the endpoint, in endpoint-to-root order. -/def lowerKeys {α : Type} : List (Frame α) → List α | [] => [] | .left _ _ :: cs => lowerKeys cs | .right _ x :: cs => x :: lowerKeys cs/-- Path separators larger than the endpoint, in endpoint-to-root order. -/def upperKeys {α : Type} : List (Frame α) → List α | [] => [] | .left x _ :: cs => x :: upperKeys cs | .right _ _ :: cs => upperKeys csAlong a heavy path from down to its endpoint , the paper splits the nodes into , those smaller than , and , those larger. A .right l x frame has its separator to the left of the focus, so it belongs to . A .left x r frame belongs to . lowerKeys and upperKeys list these separators in frame order, from the endpoint up.
def extend {α : Type} (cursor : Config α) (frame : Frame α) : Config α := ⟨cursor.focus, cursor.context ++ [frame]⟩/-- Extract one maximal solid path. If the selected child is empty, thecurrent node is its endpoint. Positive node weights make that endpoint aleaf; zero-weight off-path subtrees are retained without being discarded. -/noncomputable def extract {α : Type} (weight : α → ℝ) : LabelledTree α → Config α := by classical exact fun t => match t with | .nil => ⟨.nil, []⟩ | .node l x r => if LeftHeavy weight l r then if l = .nil then ⟨.node l x r, []⟩ else extend (extract weight l) (.left x r) else if r = .nil then ⟨.node l x r, []⟩ else extend (extract weight r) (.right l x)extract weight t follows solid edges from the root of t and returns the endpoint as a cursor configuration: its focus is the endpoint’s subtree and its context the frames of the path, innermost first. extend appends one more frame at the outer end, which is how the recursion builds the context. The walk stops when the heavy child is empty. With positive weights the heavy child of a node with a nonempty subtree is nonempty, so the endpoint is a leaf. With zero weights it can stop earlier, at a node whose only nonempty subtree has mass , and that subtree is kept in the focus. extract is noncomputable only because it decides LeftHeavy on real numbers classically.
theorem extract_tree {α : Type} (weight : α → ℝ) (t : LabelledTree α) : (extract weight t).tree = t := RotationBST.HeavyPath.extract_tree weight ttheorem extract_nonempty {α : Type} (weight : α → ℝ) {t : LabelledTree α} (nonempty : t ≠ .nil) : (extract weight t).focus ≠ .nil := RotationBST.HeavyPath.extract_nonempty weight nonemptytheorem extract_solid {α : Type} (weight : α → ℝ) (t : LabelledTree α) : SolidContext weight (extract weight t).focus (extract weight t).context := RotationBST.HeavyPath.extract_solid weight ttheorem extract_mass {α : Type} (weight : α → ℝ) (t : LabelledTree α) : mass weight t = mass weight (extract weight t).focus + ((extract weight t).context.map (frameMass weight)).sum := RotationBST.HeavyPath.extract_mass weight ttheorem extract_trace {α : Type} (weight : α → ℝ) {t : LabelledTree α} (nonempty : t ≠ .nil) : Pointer.Trace ⟨t.erase, []⟩ (extract weight t).erase (extract weight t).context.length := RotationBST.HeavyPath.extract_trace weight nonemptytheorem extract_order {α : Type} [LT α] (weight : α → ℝ) {t : LabelledTree α} (ordered : t.Ordered) : (lowerKeys (extract weight t).context).Pairwise (fun a b => b < a) ∧ (upperKeys (extract weight t).context).Pairwise (fun a b => a < b) ∧ ∀ y ∈ (extract weight t).focus.inorder, (∀ x ∈ lowerKeys (extract weight t).context, x < y) ∧ (∀ x ∈ upperKeys (extract weight t).context, y < x) := RotationBST.HeavyPath.extract_order weight orderedtheorem extract_positive {α : Type} {weight : α → ℝ} {t : LabelledTree α} (positive : Positive weight t) : Positive weight (extract weight t).focus ∧ ∀ frame ∈ (extract weight t).context, 0 < frameMass weight frame := RotationBST.HeavyPath.extract_positive positivetheorem extract_terminal {α : Type} {weight : α → ℝ} {t : LabelledTree α} (positive : Positive weight t) (nonempty : t ≠ .nil) : ∃ x, (extract weight t).focus = .node .nil x .nil := RotationBST.HeavyPath.extract_terminal positive nonemptytheorem extract_path {α : Type} {weight : α → ℝ} {t : LabelledTree α} (positive : Positive weight t) (nonempty : t ≠ .nil) : ∃ x, (extract weight t).focus = .node .nil x .nil ∧ Path weight t x 0 (extract weight t).context.length := RotationBST.HeavyPath.extract_path positive nonemptyWhat extract returns, for any weights:
extract_tree: plugging the focus back into the context givest. Nothing is lost or reordered.extract_nonempty: a nonempty tree has a nonempty endpoint subtree.extract_solid: the context is solid.extract_mass: the mass oftis the mass of the focus plus the sum of the frame charges.extract_trace: the pointer machine walks from the root to the endpoint in exactly as many instructions as there are frames.extract_order: in an ordered tree,lowerKeysis decreasing,upperKeysis increasing, and every key of the focus lies strictly between the two lists. Reading from the endpoint up, the paper’s grows downward and grows upward, which is why the two halves need stacks of opposite orientations.
And with positive weights:
extract_positive: the focus is positive and every frame has a positive charge.extract_terminal: the endpoint is a leaf.node .nil x .nil.extract_path: that leaf is reached by aPathwith no light edge, whose depth is the number of frames.
theorem decomposition {α : Type} {weight : α → ℝ} {t : LabelledTree α} (positive : Positive weight t) (nonempty : t ≠ .nil) : ∃ x, (extract weight t).focus = .node .nil x .nil ∧ (extract weight t).tree = t ∧ SolidContext weight (extract weight t).focus (extract weight t).context ∧ mass weight t = weight x + ((extract weight t).context.map (frameMass weight)).sum ∧ Path weight t x 0 (extract weight t).context.length ∧ Pointer.Trace ⟨t.erase, []⟩ (extract weight t).erase (extract weight t).context.length := RotationBST.HeavyPath.decomposition positive nonemptydecomposition bundles the facts used later: the endpoint is a leaf , the configuration is t itself, the context is solid, charges, the path has no light edge, and the descent has an exact trace.
38.1.2 A heavy path as a pair of stacks
Following the paper (its Figure 4), a heavy path ending at is stored with at the root, the separators of in a pop-tart as its left subtree, and those of in another pop-tart as its right subtree. Each separator brings its off-path subtree along, as one opaque leaf of the pop-tart:
source tree paired representation
y1 x
/ \ / \
A1 y2 pop-tart L pop-tart R
/ \ A1 y1 A3 y3 . . y2 A2
y3 A2
/ \
A3 x
Here in key order, so and . Under each pop-tart its inorder is listed: the separators are its internal nodes, the are leaves holding whole subtrees, and . is the empty leaf next to . The physical tree has the same inorder as the source; only the shape differs.
In the library this is paired: a Paired.Stacks is the pair of Chocolate pop-tarts, and the path endpoint is the root between them.
38.1.2.1 The pair
/-- The endpoint is the root between the two stacks. The second stack isstored in reversed coordinates and reflected when decoded physically. -/structure Stacks where lower : Layer upper : Layerlower stores and upper stores , each as a Chocolate Chocolate.Layer. A Chocolate pop-tart is efficient when its leaves are pushed in decreasing key order. Moving up from the endpoint, receives its separators in decreasing order, as required, but receives them in increasing order. So upper is kept in reflected coordinates, and it is mirrored when it is laid out physically.
def tree (p : Stacks) : Tree := .node p.lower.decode.tree p.upper.decode.tree.mirrordef mass (p : Stacks) : ℝ := p.lower.decode.mass + p.upper.decode.massdef potential (p : Stacks) : Nat := p.lower.potential + p.upper.potentialdef Valid (p : Stacks) : Prop := p.lower.Valid 0 ∧ p.upper.Valid 0def empty : Stacks := ⟨.last .zero (.base (.leaf .nil 0)), .last .zero (.base (.leaf .nil 0))⟩treeis the physical tree: a root (the endpoint) with the decoded lower pop-tart on the left and the mirrored decoded upper pop-tart (Tree.mirror) on the right. Its leaves are the stored payload subtrees.massis the total leaf weight of both pop-tarts. The endpoint’s own weight is not included. Thus in the headline.potentialis the sum of the two Chocolate potentials, natural numbers.Validasks both layers to be valid Chocolate pop-tarts at level (Layer.Valid).emptyis a path consisting of its endpoint only: both stacks hold just one empty leaf of weight . That empty leaf stays at the bottom of each stack throughout. It is the endpoint’s empty child, and it is why the fringes below always end with(.nil, 0).
def lowerSlots {α : Type} (weight : α → ℝ) : List (Frame α) → List (Tree × ℝ) | [] => [] | .left _ _ :: cs => lowerSlots weight cs | .right l x :: cs => (l.erase, HeavyPath.mass weight l + weight x) :: lowerSlots weight csdef upperSlots {α : Type} (weight : α → ℝ) : List (Frame α) → List (Tree × ℝ) | [] => [] | .left x r :: cs => (r.erase, weight x + HeavyPath.mass weight r) :: upperSlots weight cs | .right _ _ :: cs => upperSlots weight csThe leaves a list of frames contributes: each .right l x frame gives the lower slot (l.erase, mass l + w(x)), and each .left x r frame gives the upper slot (r.erase, w(x) + mass r). The weight of a slot is the frame’s frameMass.
38.1.2.2 Pushing the next ancestor
/-- Insert the next ancestor into the appropriate stack. The off-path subtreeis opaque; an upper subtree is reflected on entry so physical decoding restoresits original orientation. Node weights are included in the fringe charges. -/noncomputable def push {α : Type} (weight : α → ℝ) (frame : Frame α) (p : Stacks) : Stacks := match frame with | .left x r => ⟨p.lower, p.upper.push (.leaf r.erase.mirror (weight x + HeavyPath.mass weight r))⟩ | .right l x => ⟨p.lower.push (.leaf l.erase (HeavyPath.mass weight l + weight x)), p.upper⟩/-- One rotation raises the endpoint over the next ancestor; entering andleaving the updated stack adds two moves to its rebalancing cost. -/noncomputable def pushCost {α : Type} (frame : Frame α) (p : Stacks) : Nat := match frame with | .left _ _ => p.upper.pushCost + 3 | .right _ _ => p.lower.pushCost + 3push weight frame p extends the path by one more ancestor. The frame goes into the stack on its side, as one leaf: the erased off-path subtree (mirrored first for the upper stack, so that decoding restores it) with the frame’s charge. Physically, the endpoint sits just below the new ancestor. One rotation lifts it over that ancestor; the cursor then enters the selected pop-tart, the pop-tart’s push rebalances it, and the cursor comes back. pushCost counts this exactly: the Chocolate Layer.pushCost plus three.
noncomputable def build {α : Type} (weight : α → ℝ) : List (Frame α) → Stacks → Stacks | [], p => p | frame :: cs, p => build weight cs (p.push weight frame)noncomputable def buildCost {α : Type} (weight : α → ℝ) : List (Frame α) → Stacks → Nat | [], _ => 0 | frame :: cs, p => p.pushCost frame + buildCost weight cs (p.push weight frame)build pushes a list of frames, innermost first, and buildCost adds up the push costs.
theorem empty_valid : Stacks.empty.Valid := RotationBST.HeavyPath.Paired.empty_validModels.Rotation, Models.Pointer.Machine, Models.Pointer.Mirror.Definitions, Models.Pointer.Lazy.Skeleton, Models.Labelled.Cursor.Definitions, Models.Labelled.Ordering.Definitions, Structures.PopTart.Definitions, Structures.PopTart.Mirror.Definitions, Structures.PopTart.Chocolate.Definitions, Structures.HeavyPath.Definitions, Structures.HeavyPath.Paired.Definitions, Structures.PopTart.Located.Definitions, Bounds.StaticTree.Definitionstheorem push_trace {α : Type} (weight : α → ℝ) (frame : Frame α) (p : Stacks) (cs : List Pointer.Frame) : Pointer.Trace ⟨p.tree, frame.erase :: cs⟩ ⟨(p.push weight frame).tree, cs⟩ (p.pushCost frame) := RotationBST.HeavyPath.Paired.push_trace weight frame p csModels.Rotation, Models.Pointer.Machine, Models.Pointer.Mirror.Definitions, Models.Pointer.Lazy.Skeleton, Models.Labelled.Cursor.Definitions, Models.Labelled.Ordering.Definitions, Structures.PopTart.Definitions, Structures.PopTart.Mirror.Definitions, Structures.PopTart.Chocolate.Definitions, Structures.HeavyPath.Definitions, Structures.HeavyPath.Paired.Definitions, Structures.PopTart.Located.Definitions, Bounds.StaticTree.Definitionstheorem push_valid {α : Type} {weight : α → ℝ} {frame : Frame α} {p : Stacks} (valid : p.Valid) (nonnegative : 0 ≤ frameMass weight frame) : (p.push weight frame).Valid := RotationBST.HeavyPath.Paired.push_valid valid nonnegativeModels.Rotation, Models.Pointer.Machine, Models.Pointer.Mirror.Definitions, Models.Pointer.Lazy.Skeleton, Models.Labelled.Cursor.Definitions, Models.Labelled.Ordering.Definitions, Structures.PopTart.Definitions, Structures.PopTart.Mirror.Definitions, Structures.PopTart.Chocolate.Definitions, Structures.HeavyPath.Definitions, Structures.HeavyPath.Paired.Definitions, Structures.PopTart.Located.Definitions, Bounds.StaticTree.Definitionstheorem push_amortized {α : Type} {weight : α → ℝ} {frame : Frame α} {p : Stacks} (valid : p.Valid) (nonnegative : 0 ≤ frameMass weight frame) : p.pushCost frame + (p.push weight frame).potential ≤ 23 + p.potential := RotationBST.HeavyPath.Paired.push_amortized valid nonnegativeModels.Rotation, Models.Pointer.Machine, Models.Pointer.Mirror.Definitions, Models.Pointer.Lazy.Skeleton, Models.Labelled.Cursor.Definitions, Models.Labelled.Ordering.Definitions, Structures.PopTart.Definitions, Structures.PopTart.Mirror.Definitions, Structures.PopTart.Chocolate.Definitions, Structures.HeavyPath.Definitions, Structures.HeavyPath.Paired.Definitions, Structures.PopTart.Located.Definitions, Bounds.StaticTree.Definitionstheorem push_mass {α : Type} {weight : α → ℝ} {frame : Frame α} {p : Stacks} (valid : p.Valid) (nonnegative : 0 ≤ frameMass weight frame) : (p.push weight frame).mass = p.mass + frameMass weight frame := RotationBST.HeavyPath.Paired.push_mass valid nonnegativeModels.Rotation, Models.Pointer.Machine, Models.Pointer.Mirror.Definitions, Models.Pointer.Lazy.Skeleton, Models.Labelled.Cursor.Definitions, Models.Labelled.Ordering.Definitions, Structures.PopTart.Definitions, Structures.PopTart.Mirror.Definitions, Structures.PopTart.Chocolate.Definitions, Structures.HeavyPath.Definitions, Structures.HeavyPath.Paired.Definitions, Structures.PopTart.Located.Definitions, Bounds.StaticTree.Definitionstheorem push_lower_size {α : Type} (weight : α → ℝ) (frame : Frame α) (p : Stacks) : (p.push weight frame).lower.decode.tree.size = p.lower.decode.tree.size + Pointer.offset [frame.erase] := RotationBST.HeavyPath.Paired.push_lower_size weight frame pModels.Rotation, Models.Pointer.Machine, Models.Pointer.Mirror.Definitions, Models.Pointer.Lazy.Skeleton, Models.Labelled.Cursor.Definitions, Models.Labelled.Ordering.Definitions, Structures.PopTart.Definitions, Structures.PopTart.Mirror.Definitions, Structures.PopTart.Chocolate.Definitions, Structures.HeavyPath.Definitions, Structures.HeavyPath.Paired.Definitions, Structures.PopTart.Located.Definitions, Bounds.StaticTree.DefinitionsTheorem (push). Let be a valid pair, physically the child of the ancestor in frame, inside any further context cs. With the cursor at the root of , there is a pointer trace of exactly pushCost instructions that ends with the cursor at the root of the pushed pair, inside cs. If the frame’s charge is nonnegative, the result is valid, its mass grows by the charge, and
The empty pair is valid. Only nonnegative charges are needed, not positive ones. The trace in push_trace exists for every pair, valid or not, because it describes the physical rebalancing. push_lower_size tracks the rank of the endpoint: the lower pop-tart grows by l.size + 1 for a .right l x frame and by nothing for a .left frame, which is the Pointer.offset of the frame. Since the endpoint’s rank is the size of the lower pop-tart, the root of the pair always has the endpoint’s source rank.
theorem build_trace {α : Type} (weight : α → ℝ) (cs : List (Frame α)) (p : Stacks) (ds : List Pointer.Frame) : Pointer.Trace ⟨p.tree, cs.map Frame.erase ++ ds⟩ ⟨(Stacks.build weight cs p).tree, ds⟩ (Stacks.buildCost weight cs p) := RotationBST.HeavyPath.Paired.build_trace weight cs p dsModels.Rotation, Models.Pointer.Machine, Models.Pointer.Mirror.Definitions, Models.Pointer.Lazy.Skeleton, Models.Labelled.Cursor.Definitions, Models.Labelled.Ordering.Definitions, Structures.PopTart.Definitions, Structures.PopTart.Mirror.Definitions, Structures.PopTart.Chocolate.Definitions, Structures.HeavyPath.Definitions, Structures.HeavyPath.Paired.Definitions, Structures.PopTart.Located.Definitions, Bounds.StaticTree.Definitionstheorem build_valid {α : Type} {weight : α → ℝ} (cs : List (Frame α)) {p : Stacks} (valid : p.Valid) (nonnegative : ∀ frame ∈ cs, 0 ≤ frameMass weight frame) : (Stacks.build weight cs p).Valid := RotationBST.HeavyPath.Paired.build_valid cs valid nonnegativeModels.Rotation, Models.Pointer.Machine, Models.Pointer.Mirror.Definitions, Models.Pointer.Lazy.Skeleton, Models.Labelled.Cursor.Definitions, Models.Labelled.Ordering.Definitions, Structures.PopTart.Definitions, Structures.PopTart.Mirror.Definitions, Structures.PopTart.Chocolate.Definitions, Structures.HeavyPath.Definitions, Structures.HeavyPath.Paired.Definitions, Structures.PopTart.Located.Definitions, Bounds.StaticTree.Definitionstheorem build_amortized {α : Type} {weight : α → ℝ} (cs : List (Frame α)) {p : Stacks} (valid : p.Valid) (nonnegative : ∀ frame ∈ cs, 0 ≤ frameMass weight frame) : Stacks.buildCost weight cs p + (Stacks.build weight cs p).potential ≤ 23 * cs.length + p.potential := RotationBST.HeavyPath.Paired.build_amortized cs valid nonnegativeModels.Rotation, Models.Pointer.Machine, Models.Pointer.Mirror.Definitions, Models.Pointer.Lazy.Skeleton, Models.Labelled.Cursor.Definitions, Models.Labelled.Ordering.Definitions, Structures.PopTart.Definitions, Structures.PopTart.Mirror.Definitions, Structures.PopTart.Chocolate.Definitions, Structures.HeavyPath.Definitions, Structures.HeavyPath.Paired.Definitions, Structures.PopTart.Located.Definitions, Bounds.StaticTree.Definitionstheorem build_mass {α : Type} {weight : α → ℝ} (cs : List (Frame α)) {p : Stacks} (valid : p.Valid) (nonnegative : ∀ frame ∈ cs, 0 ≤ frameMass weight frame) : (Stacks.build weight cs p).mass = p.mass + (cs.map (frameMass weight)).sum := RotationBST.HeavyPath.Paired.build_mass cs valid nonnegativeModels.Rotation, Models.Pointer.Machine, Models.Pointer.Mirror.Definitions, Models.Pointer.Lazy.Skeleton, Models.Labelled.Cursor.Definitions, Models.Labelled.Ordering.Definitions, Structures.PopTart.Definitions, Structures.PopTart.Mirror.Definitions, Structures.PopTart.Chocolate.Definitions, Structures.HeavyPath.Definitions, Structures.HeavyPath.Paired.Definitions, Structures.PopTart.Located.Definitions, Bounds.StaticTree.Definitionstheorem build_lower_size {α : Type} (weight : α → ℝ) (cs : List (Frame α)) (p : Stacks) : (Stacks.build weight cs p).lower.decode.tree.size = p.lower.decode.tree.size + Pointer.offset (cs.map Frame.erase) := RotationBST.HeavyPath.Paired.build_lower_size weight cs pModels.Rotation, Models.Pointer.Machine, Models.Pointer.Mirror.Definitions, Models.Pointer.Lazy.Skeleton, Models.Labelled.Cursor.Definitions, Models.Labelled.Ordering.Definitions, Structures.PopTart.Definitions, Structures.PopTart.Mirror.Definitions, Structures.PopTart.Chocolate.Definitions, Structures.HeavyPath.Definitions, Structures.HeavyPath.Paired.Definitions, Structures.PopTart.Located.Definitions, Bounds.StaticTree.Definitionstheorem build_fringe {α : Type} {weight : α → ℝ} (cs : List (Frame α)) {p : Stacks} (valid : p.Valid) (nonnegative : ∀ frame ∈ cs, 0 ≤ frameMass weight frame) : (Stacks.build weight cs p).lower.decode.fringe = (lowerSlots weight cs).reverse ++ p.lower.decode.fringe ∧ (Stacks.build weight cs p).upper.decode.mirror.fringe = p.upper.decode.mirror.fringe ++ upperSlots weight cs := RotationBST.HeavyPath.Paired.build_fringe cs valid nonnegativeModels.Rotation, Models.Pointer.Machine, Models.Pointer.Mirror.Definitions, Models.Pointer.Lazy.Skeleton, Models.Labelled.Cursor.Definitions, Models.Labelled.Ordering.Definitions, Structures.PopTart.Definitions, Structures.PopTart.Mirror.Definitions, Structures.PopTart.Chocolate.Definitions, Structures.HeavyPath.Definitions, Structures.HeavyPath.Paired.Definitions, Structures.PopTart.Located.Definitions, Bounds.StaticTree.Definitionstheorem empty_build_fringe {α : Type} {weight : α → ℝ} (cs : List (Frame α)) (nonnegative : ∀ frame ∈ cs, 0 ≤ frameMass weight frame) : (Stacks.build weight cs Stacks.empty).lower.decode.fringe = (lowerSlots weight cs).reverse ++ [(.nil, 0)] ∧ (Stacks.build weight cs Stacks.empty).upper.decode.mirror.fringe = [(.nil, 0)] ++ upperSlots weight cs := RotationBST.HeavyPath.Paired.empty_build_fringe cs nonnegativeModels.Rotation, Models.Pointer.Machine, Models.Pointer.Mirror.Definitions, Models.Pointer.Lazy.Skeleton, Models.Labelled.Cursor.Definitions, Models.Labelled.Ordering.Definitions, Structures.PopTart.Definitions, Structures.PopTart.Mirror.Definitions, Structures.PopTart.Chocolate.Definitions, Structures.HeavyPath.Definitions, Structures.HeavyPath.Paired.Definitions, Structures.PopTart.Located.Definitions, Bounds.StaticTree.DefinitionsThe same facts for build, by induction. Building frames costs at most amortized. The fringe lemmas say exactly which leaves the two pop-tarts hold, in physical inorder. The lower fringe is lowerSlots reversed, outermost first, followed by the old fringe. The mirrored upper fringe is the old one followed by upperSlots, innermost first. From the empty pair, each fringe has the empty leaf on the endpoint’s side. So the physical tree has the source inorder, payload by payload and separator by separator.
38.1.2.3 From the source tree to the pair
Theorem (pairing the heavy path from the root). Let be a nonempty tree with positive weights, let be the endpoint of its heavy path from the root, and let be the path’s number of edges. Build the pair from the path’s frames, starting from empty. Then is valid and . With the cursor at the root of , a pointer trace of some cost turns into the tree of with the cursor back at the root, and . The root of holds the endpoint’s rank.
theorem extract_build {α : Type} {weight : α → ℝ} {t : LabelledTree α} (positive : Positive weight t) (nonempty : t ≠ .nil) : let q := extract weight t let p := Stacks.build weight q.context Stacks.empty ∃ x cost, q.focus = .node .nil x .nil ∧ p.Valid ∧ weight x + p.mass = HeavyPath.mass weight t ∧ Pointer.Trace ⟨t.erase, []⟩ ⟨p.tree, []⟩ cost ∧ cost + p.potential ≤ 24 * q.context.length ∧ (Pointer.Config.mk p.tree []).key? = q.erase.key? := RotationBST.HeavyPath.Paired.extract_build positive nonemptyModels.Rotation, Models.Pointer.Machine, Models.Pointer.Mirror.Definitions, Models.Pointer.Lazy.Skeleton, Models.Labelled.Cursor.Definitions, Models.Labelled.Ordering.Definitions, Structures.PopTart.Definitions, Structures.PopTart.Mirror.Definitions, Structures.PopTart.Chocolate.Definitions, Structures.HeavyPath.Definitions, Structures.HeavyPath.Paired.Definitions, Structures.PopTart.Located.Definitions, Bounds.StaticTree.DefinitionsThe trace walks down the path, instructions, and then pushes the frames back up, at amortized cost each, from potential . The last conjunct compares Config.key? of the two configurations: the physical root is the rank of the source endpoint.
theorem extract_build_labelled {α : Type} [Preorder α] {weight : α → ℝ} {t : LabelledTree α} (positive : Positive weight t) (nonempty : t ≠ .nil) (ordered : t.Ordered) : let p := Stacks.build weight (extract weight t).context Stacks.empty ∃ u : LabelledTree α, ∃ rotations cost, u.erase = p.tree ∧ u.inorder = t.inorder ∧ u.Ordered ∧ LabelledTree.Rotations t u rotations ∧ rotations ≤ cost ∧ Pointer.Trace ⟨t.erase, []⟩ ⟨p.tree, []⟩ cost ∧ cost + p.potential ≤ 24 * (extract weight t).context.length := RotationBST.HeavyPath.Paired.extract_build_labelled positive nonempty orderedModels.Rotation, Models.Pointer.Machine, Models.Pointer.Mirror.Definitions, Models.Pointer.Lazy.Skeleton, Models.Labelled.Cursor.Definitions, Models.Labelled.Ordering.Definitions, Structures.PopTart.Definitions, Structures.PopTart.Mirror.Definitions, Structures.PopTart.Chocolate.Definitions, Structures.HeavyPath.Definitions, Structures.HeavyPath.Paired.Definitions, Structures.PopTart.Located.Definitions, Bounds.StaticTree.DefinitionsThe labelled version, for an ordered labelled tree t. Some labelled tree u, with the shape of the pair, the same inorder labels as t, and still ordered, is reached from t by a sequence of labelled rotations (LabelledTree.Rotations). The rotation count is at most the trace cost. So the transformation is an ordinary sequence of BST rotations that carries each label with its node. The off-path subtrees are left untouched: each is erased and stored as is.
38.1.2.4 Depth inside the pair
The pair inherits the weighted depth bound of the Chocolate pop-tart (Results.weighted_depth). That bound places a leaf of weight at depth at most in its stack . The pair adds one edge for its root and uses the mass of both stacks, which is at least .
theorem lower_depth {p : Stacks} (valid : p.Valid) {subtree : Tree} {weight : ℝ} {depth : Nat} (slot : WeightedTree.SlotAt p.lower.decode subtree weight depth) (positive : 0 < weight) : ((depth + 1 : Nat) : ℝ) ≤ 8 + 7 * Real.logb 2 (p.mass / weight) := RotationBST.HeavyPath.Paired.lower_depth valid slot positiveModels.Rotation, Models.Pointer.Machine, Models.Pointer.Mirror.Definitions, Models.Pointer.Lazy.Skeleton, Models.Labelled.Cursor.Definitions, Models.Labelled.Ordering.Definitions, Structures.PopTart.Definitions, Structures.PopTart.Mirror.Definitions, Structures.PopTart.Chocolate.Definitions, Structures.HeavyPath.Definitions, Structures.HeavyPath.Paired.Definitions, Structures.PopTart.Located.Definitions, Bounds.StaticTree.Definitionstheorem upper_depth {p : Stacks} (valid : p.Valid) {subtree : Tree} {weight : ℝ} {depth : Nat} (slot : WeightedTree.SlotAt p.upper.decode.mirror subtree weight depth) (positive : 0 < weight) : ((depth + 1 : Nat) : ℝ) ≤ 8 + 7 * Real.logb 2 (p.mass / weight) := RotationBST.HeavyPath.Paired.upper_depth valid slot positiveModels.Rotation, Models.Pointer.Machine, Models.Pointer.Mirror.Definitions, Models.Pointer.Lazy.Skeleton, Models.Labelled.Cursor.Definitions, Models.Labelled.Ordering.Definitions, Structures.PopTart.Definitions, Structures.PopTart.Mirror.Definitions, Structures.PopTart.Chocolate.Definitions, Structures.HeavyPath.Definitions, Structures.HeavyPath.Paired.Definitions, Structures.PopTart.Located.Definitions, Bounds.StaticTree.Definitionstheorem lower_access {p : Stacks} (valid : p.Valid) {subtree : Tree} {weight : ℝ} {depth : Nat} (slot : WeightedTree.SlotAt p.lower.decode subtree weight depth) (positive : 0 < weight) (nonempty : subtree ≠ .nil) : ∃ cs : List Pointer.Frame, cs.length = depth + 1 ∧ Pointer.plug cs subtree = p.tree ∧ Pointer.Trace ⟨p.tree, []⟩ ⟨subtree, cs⟩ (depth + 1) ∧ ((depth + 1 : Nat) : ℝ) ≤ 8 + 7 * Real.logb 2 (p.mass / weight) := RotationBST.HeavyPath.Paired.lower_access valid slot positive nonemptyModels.Rotation, Models.Pointer.Machine, Models.Pointer.Mirror.Definitions, Models.Pointer.Lazy.Skeleton, Models.Labelled.Cursor.Definitions, Models.Labelled.Ordering.Definitions, Structures.PopTart.Definitions, Structures.PopTart.Mirror.Definitions, Structures.PopTart.Chocolate.Definitions, Structures.HeavyPath.Definitions, Structures.HeavyPath.Paired.Definitions, Structures.PopTart.Located.Definitions, Bounds.StaticTree.Definitionstheorem upper_access {p : Stacks} (valid : p.Valid) {subtree : Tree} {weight : ℝ} {depth : Nat} (slot : WeightedTree.SlotAt p.upper.decode.mirror subtree weight depth) (positive : 0 < weight) (nonempty : subtree ≠ .nil) : ∃ cs : List Pointer.Frame, cs.length = depth + 1 ∧ Pointer.plug cs subtree = p.tree ∧ Pointer.Trace ⟨p.tree, []⟩ ⟨subtree, cs⟩ (depth + 1) ∧ ((depth + 1 : Nat) : ℝ) ≤ 8 + 7 * Real.logb 2 (p.mass / weight) := RotationBST.HeavyPath.Paired.upper_access valid slot positive nonemptyModels.Rotation, Models.Pointer.Machine, Models.Pointer.Mirror.Definitions, Models.Pointer.Lazy.Skeleton, Models.Labelled.Cursor.Definitions, Models.Labelled.Ordering.Definitions, Structures.PopTart.Definitions, Structures.PopTart.Mirror.Definitions, Structures.PopTart.Chocolate.Definitions, Structures.HeavyPath.Definitions, Structures.HeavyPath.Paired.Definitions, Structures.PopTart.Located.Definitions, Bounds.StaticTree.DefinitionsA leaf occurrence (WeightedTree.SlotAt) at depth in the lower pop-tart, or in the physically oriented upper one, with weight , satisfies . The access versions add the navigation: if the payload is nonempty, the cursor reaches its root from the root of the pair in exactly instructions, through a context that plugs it back into the pair.
theorem lower_key_depth {p : Stacks} {subtree : Tree} {weight : ℝ} {offset depth : Nat} (slot : LocatedSlot p.lower.decode subtree weight offset depth) (x : Nat) (inside : x < subtree.size) : Tree.depth p.tree (offset + x) = depth + 1 + Tree.depth subtree x := RotationBST.HeavyPath.Paired.lower_key_depth slot x insideModels.Rotation, Models.Pointer.Machine, Models.Pointer.Mirror.Definitions, Models.Pointer.Lazy.Skeleton, Models.Labelled.Cursor.Definitions, Models.Labelled.Ordering.Definitions, Structures.PopTart.Definitions, Structures.PopTart.Mirror.Definitions, Structures.PopTart.Chocolate.Definitions, Structures.HeavyPath.Definitions, Structures.HeavyPath.Paired.Definitions, Structures.PopTart.Located.Definitions, Bounds.StaticTree.Definitionstheorem upper_key_depth {p : Stacks} {subtree : Tree} {weight : ℝ} {offset depth : Nat} (slot : LocatedSlot p.upper.decode.mirror subtree weight offset depth) (x : Nat) (inside : x < subtree.size) : Tree.depth p.tree (p.lower.decode.tree.size + 1 + offset + x) = depth + 1 + Tree.depth subtree x := RotationBST.HeavyPath.Paired.upper_key_depth slot x insideModels.Rotation, Models.Pointer.Machine, Models.Pointer.Mirror.Definitions, Models.Pointer.Lazy.Skeleton, Models.Labelled.Cursor.Definitions, Models.Labelled.Ordering.Definitions, Structures.PopTart.Definitions, Structures.PopTart.Mirror.Definitions, Structures.PopTart.Chocolate.Definitions, Structures.HeavyPath.Definitions, Structures.HeavyPath.Paired.Definitions, Structures.PopTart.Located.Definitions, Bounds.StaticTree.Definitionstheorem lower_key_bound {p : Stacks} (valid : p.Valid) {subtree : Tree} {weight : ℝ} {offset depth : Nat} (slot : LocatedSlot p.lower.decode subtree weight offset depth) (positive : 0 < weight) (x : Nat) (inside : x < subtree.size) : (Tree.depth p.tree (offset + x) : ℝ) ≤ 8 + 7 * Real.logb 2 (p.mass / weight) + Tree.depth subtree x := RotationBST.HeavyPath.Paired.lower_key_bound valid slot positive x insideModels.Rotation, Models.Pointer.Machine, Models.Pointer.Mirror.Definitions, Models.Pointer.Lazy.Skeleton, Models.Labelled.Cursor.Definitions, Models.Labelled.Ordering.Definitions, Structures.PopTart.Definitions, Structures.PopTart.Mirror.Definitions, Structures.PopTart.Chocolate.Definitions, Structures.HeavyPath.Definitions, Structures.HeavyPath.Paired.Definitions, Structures.PopTart.Located.Definitions, Bounds.StaticTree.Definitionstheorem upper_key_bound {p : Stacks} (valid : p.Valid) {subtree : Tree} {weight : ℝ} {offset depth : Nat} (slot : LocatedSlot p.upper.decode.mirror subtree weight offset depth) (positive : 0 < weight) (x : Nat) (inside : x < subtree.size) : (Tree.depth p.tree (p.lower.decode.tree.size + 1 + offset + x) : ℝ) ≤ 8 + 7 * Real.logb 2 (p.mass / weight) + Tree.depth subtree x := RotationBST.HeavyPath.Paired.upper_key_bound valid slot positive x insideModels.Rotation, Models.Pointer.Machine, Models.Pointer.Mirror.Definitions, Models.Pointer.Lazy.Skeleton, Models.Labelled.Cursor.Definitions, Models.Labelled.Ordering.Definitions, Structures.PopTart.Definitions, Structures.PopTart.Mirror.Definitions, Structures.PopTart.Chocolate.Definitions, Structures.HeavyPath.Definitions, Structures.HeavyPath.Paired.Definitions, Structures.PopTart.Located.Definitions, Bounds.StaticTree.Definitionstheorem lower_separator_bound {p : Stacks} (valid : p.Valid) {subtree : Tree} {weight : ℝ} {offset depth : Nat} (slot : LocatedSlot p.lower.decode subtree weight offset depth) (positive : 0 < weight) : (Tree.depth p.tree (offset + subtree.size) : ℝ) ≤ 7 + 7 * Real.logb 2 (p.mass / weight) := RotationBST.HeavyPath.Paired.lower_separator_bound valid slot positiveModels.Rotation, Models.Pointer.Machine, Models.Pointer.Mirror.Definitions, Models.Pointer.Lazy.Skeleton, Models.Labelled.Cursor.Definitions, Models.Labelled.Ordering.Definitions, Structures.PopTart.Definitions, Structures.PopTart.Mirror.Definitions, Structures.PopTart.Chocolate.Definitions, Structures.HeavyPath.Definitions, Structures.HeavyPath.Paired.Definitions, Structures.PopTart.Located.Definitions, Bounds.StaticTree.Definitionstheorem upper_separator_bound {p : Stacks} (valid : p.Valid) {subtree : Tree} {weight : ℝ} {offset depth : Nat} (slot : LocatedSlot p.upper.decode.mirror subtree weight offset depth) (positive : 0 < weight) : (Tree.depth p.tree (p.lower.decode.tree.size + offset) : ℝ) ≤ 7 + 7 * Real.logb 2 (p.mass / weight) := RotationBST.HeavyPath.Paired.upper_separator_bound valid slot positiveModels.Rotation, Models.Pointer.Machine, Models.Pointer.Mirror.Definitions, Models.Pointer.Lazy.Skeleton, Models.Labelled.Cursor.Definitions, Models.Labelled.Ordering.Definitions, Structures.PopTart.Definitions, Structures.PopTart.Mirror.Definitions, Structures.PopTart.Chocolate.Definitions, Structures.HeavyPath.Definitions, Structures.HeavyPath.Paired.Definitions, Structures.PopTart.Located.Definitions, Bounds.StaticTree.DefinitionsThe same, by key. A WeightedTree.LocatedSlot also records the payload’s inorder offset in its pop-tart. A key of rank x inside the payload then has rank offset + x in the pair, or lower size + 1 + offset + x on the upper side, past the endpoint. Its Tree.depth in the pair is exactly the slot’s depth plus one plus its depth inside the payload (*_key_depth), hence at most plus its depth inside the payload (*_key_bound). The separator that comes with the payload, just after it on the lower side and just before it on the upper side, is at depth at most .
theorem located_depth {p : Stacks} (valid : p.Valid) {subtree : Tree} {weight : ℝ} {offset depth : Nat} (slot : LocatedSlot (.node p.lower.decode p.upper.decode.mirror) subtree weight offset depth) (positive : 0 < weight) : (depth : ℝ) ≤ 8 + 7 * Real.logb 2 (p.mass / weight) := RotationBST.HeavyPath.Paired.located_depth valid slot positiveModels.Rotation, Models.Pointer.Machine, Models.Pointer.Mirror.Definitions, Models.Pointer.Lazy.Skeleton, Models.Labelled.Cursor.Definitions, Models.Labelled.Ordering.Definitions, Structures.PopTart.Definitions, Structures.PopTart.Mirror.Definitions, Structures.PopTart.Chocolate.Definitions, Structures.HeavyPath.Definitions, Structures.HeavyPath.Paired.Definitions, Structures.PopTart.Located.Definitions, Bounds.StaticTree.Definitionstheorem located_mass {p : Stacks} (valid : p.Valid) {subtree : Tree} {weight : ℝ} {offset depth : Nat} (slot : LocatedSlot (.node p.lower.decode p.upper.decode.mirror) subtree weight offset depth) : 0 ≤ weight ∧ weight ≤ p.mass := RotationBST.HeavyPath.Paired.located_mass valid slotModels.Rotation, Models.Pointer.Machine, Models.Pointer.Mirror.Definitions, Models.Pointer.Lazy.Skeleton, Models.Labelled.Cursor.Definitions, Models.Labelled.Ordering.Definitions, Structures.PopTart.Definitions, Structures.PopTart.Mirror.Definitions, Structures.PopTart.Chocolate.Definitions, Structures.HeavyPath.Definitions, Structures.HeavyPath.Paired.Definitions, Structures.PopTart.Located.Definitions, Bounds.StaticTree.Definitionstheorem located_key_bound {p : Stacks} (valid : p.Valid) {subtree : Tree} {weight : ℝ} {offset depth : Nat} (slot : LocatedSlot (.node p.lower.decode p.upper.decode.mirror) subtree weight offset depth) (positive : 0 < weight) (x : Nat) (inside : x < subtree.size) : (Tree.depth p.tree (offset + x) : ℝ) ≤ 8 + 7 * Real.logb 2 (p.mass / weight) + Tree.depth subtree x := RotationBST.HeavyPath.Paired.located_key_bound valid slot positive x insideModels.Rotation, Models.Pointer.Machine, Models.Pointer.Mirror.Definitions, Models.Pointer.Lazy.Skeleton, Models.Labelled.Cursor.Definitions, Models.Labelled.Ordering.Definitions, Structures.PopTart.Definitions, Structures.PopTart.Mirror.Definitions, Structures.PopTart.Chocolate.Definitions, Structures.HeavyPath.Definitions, Structures.HeavyPath.Paired.Definitions, Structures.PopTart.Located.Definitions, Bounds.StaticTree.Definitionstheorem located_successor_bound {p : Stacks} (valid : p.Valid) {subtree : Tree} {weight : ℝ} {offset depth : Nat} (slot : LocatedSlot (.node p.lower.decode p.upper.decode.mirror) subtree weight offset depth) (positive : 0 < weight) (existsKey : offset + subtree.size < p.tree.size) : (Tree.depth p.tree (offset + subtree.size) : ℝ) ≤ 7 + 7 * Real.logb 2 (p.mass / weight) := RotationBST.HeavyPath.Paired.located_successor_bound valid slot positive existsKeyModels.Rotation, Models.Pointer.Machine, Models.Pointer.Mirror.Definitions, Models.Pointer.Lazy.Skeleton, Models.Labelled.Cursor.Definitions, Models.Labelled.Ordering.Definitions, Structures.PopTart.Definitions, Structures.PopTart.Mirror.Definitions, Structures.PopTart.Chocolate.Definitions, Structures.HeavyPath.Definitions, Structures.HeavyPath.Paired.Definitions, Structures.PopTart.Located.Definitions, Bounds.StaticTree.Definitionstheorem located_predecessor_bound {p : Stacks} (valid : p.Valid) {subtree : Tree} {weight : ℝ} {offset depth : Nat} (slot : LocatedSlot (.node p.lower.decode p.upper.decode.mirror) subtree weight offset depth) (positive : 0 < weight) (existsKey : 0 < offset) : (Tree.depth p.tree (offset - 1) : ℝ) ≤ 7 + 7 * Real.logb 2 (p.mass / weight) := RotationBST.HeavyPath.Paired.located_predecessor_bound valid slot positive existsKeyModels.Rotation, Models.Pointer.Machine, Models.Pointer.Mirror.Definitions, Models.Pointer.Lazy.Skeleton, Models.Labelled.Cursor.Definitions, Models.Labelled.Ordering.Definitions, Structures.PopTart.Definitions, Structures.PopTart.Mirror.Definitions, Structures.PopTart.Chocolate.Definitions, Structures.HeavyPath.Definitions, Structures.HeavyPath.Paired.Definitions, Structures.PopTart.Located.Definitions, Bounds.StaticTree.DefinitionsTheorem (depth in a pair). View a valid pair as a single weighted tree: the endpoint at the root, the lower pop-tart on the left, and the physically oriented upper pop-tart on the right. Let a payload with charge sit in it at depth and inorder offset . Then:
- , and ;
- a key of rank , the -th key of the payload, has depth at most plus its depth in the payload;
- the keys just after and just before the payload, when they exist, have depth at most .
These are the forms used later. The recursion of Section 38.2 applies located_key_bound once per heavy path and telescopes. The successor and predecessor bounds let a search that ends between two payloads, or at a separator, be charged to the payload next to it.
38.1.3 Substitution: payloads other than the source subtree
So far the off-path subtrees go into the pop-tarts unchanged. In the recursive representation, each of them is itself restructured before it is stored, and later sections even keep different physical layouts for equal source subtrees. Substitution generalizes the pair to an arbitrary physical payload function, while keeping the charges of the source.
def framePayload {α : Type} : Frame α → LabelledTree α | .left _ r => r | .right l _ => l/-- Replace the physical off-path subtree, retaining the side of the path. -/def replaceFrame {α : Type} (payload : LabelledTree α → Tree) : Frame α → Pointer.Frame | .left _ r => .left (payload r) | .right l _ => .right (payload l)framePayload is a frame’s off-path source subtree. replaceFrame payload frame is the physical Pointer.Frame on the same side, with payload applied to that subtree.
/-- Analysis charges come from the original labelled tree, even when thephysical payload has already been recursively restructured. -/noncomputable def pushWith {α : Type} (weight : α → ℝ) (payload : LabelledTree α → Tree) (frame : Frame α) (p : Stacks) : Stacks := match frame with | .left x r => ⟨p.lower, p.upper.push (.leaf (payload r).mirror (weight x + HeavyPath.mass weight r))⟩ | .right l x => ⟨p.lower.push (.leaf (payload l) (HeavyPath.mass weight l + weight x)), p.upper⟩noncomputable def buildWith {α : Type} (weight : α → ℝ) (payload : LabelledTree α → Tree) : List (Frame α) → Stacks → Stacks | [], p => p | frame :: cs, p => buildWith weight payload cs (p.pushWith weight payload frame)noncomputable def buildWithCost {α : Type} (weight : α → ℝ) (payload : LabelledTree α → Tree) : List (Frame α) → Stacks → Nat | [], _ => 0 | frame :: cs, p => p.pushCost frame + buildWithCost weight payload cs (p.pushWith weight payload frame)def lowerSlotsWith {α : Type} (weight : α → ℝ) (payload : LabelledTree α → Tree) : List (Frame α) → List (Tree × ℝ) | [] => [] | .left _ _ :: cs => lowerSlotsWith weight payload cs | .right l x :: cs => (payload l, HeavyPath.mass weight l + weight x) :: lowerSlotsWith weight payload csdef upperSlotsWith {α : Type} (weight : α → ℝ) (payload : LabelledTree α → Tree) : List (Frame α) → List (Tree × ℝ) | [] => [] | .left x r :: cs => (payload r, weight x + HeavyPath.mass weight r) :: upperSlotsWith weight payload cs | .right _ _ :: cs => upperSlotsWith weight payload cspushWith weight payload stores payload l (or the mirror of payload r) instead of the erased subtree. The charge is still the frame’s frameMass, computed from the source subtree. The physical layout of a payload therefore never changes the weights that the pop-tarts see. buildWith, buildWithCost and the *SlotsWith lists are the corresponding generalizations. The cost of each push is the same pushCost, which does not depend on the payload.
theorem pushWith_trace {α : Type} (weight : α → ℝ) (payload : LabelledTree α → Tree) (frame : Frame α) (p : Stacks) (cs : List Pointer.Frame) : Pointer.Trace ⟨p.tree, replaceFrame payload frame :: cs⟩ ⟨(p.pushWith weight payload frame).tree, cs⟩ (p.pushCost frame) := RotationBST.HeavyPath.Paired.pushWith_trace weight payload frame p csModels.Rotation, Models.Pointer.Machine, Models.Pointer.Mirror.Definitions, Models.Pointer.Lazy.Skeleton, Models.Labelled.Cursor.Definitions, Models.Labelled.Ordering.Definitions, Structures.PopTart.Definitions, Structures.PopTart.Mirror.Definitions, Structures.PopTart.Chocolate.Definitions, Structures.HeavyPath.Definitions, Structures.HeavyPath.Paired.Definitions, Structures.HeavyPath.Paired.Substitution.Definitionstheorem pushWith_valid {α : Type} {weight : α → ℝ} {frame : Frame α} {p : Stacks} (payload : LabelledTree α → Tree) (valid : p.Valid) (nonnegative : 0 ≤ frameMass weight frame) : (p.pushWith weight payload frame).Valid := RotationBST.HeavyPath.Paired.pushWith_valid payload valid nonnegativeModels.Rotation, Models.Pointer.Machine, Models.Pointer.Mirror.Definitions, Models.Pointer.Lazy.Skeleton, Models.Labelled.Cursor.Definitions, Models.Labelled.Ordering.Definitions, Structures.PopTart.Definitions, Structures.PopTart.Mirror.Definitions, Structures.PopTart.Chocolate.Definitions, Structures.HeavyPath.Definitions, Structures.HeavyPath.Paired.Definitions, Structures.HeavyPath.Paired.Substitution.Definitionstheorem pushWith_amortized {α : Type} {weight : α → ℝ} {frame : Frame α} {p : Stacks} (payload : LabelledTree α → Tree) (valid : p.Valid) (nonnegative : 0 ≤ frameMass weight frame) : p.pushCost frame + (p.pushWith weight payload frame).potential ≤ 23 + p.potential := RotationBST.HeavyPath.Paired.pushWith_amortized payload valid nonnegativeModels.Rotation, Models.Pointer.Machine, Models.Pointer.Mirror.Definitions, Models.Pointer.Lazy.Skeleton, Models.Labelled.Cursor.Definitions, Models.Labelled.Ordering.Definitions, Structures.PopTart.Definitions, Structures.PopTart.Mirror.Definitions, Structures.PopTart.Chocolate.Definitions, Structures.HeavyPath.Definitions, Structures.HeavyPath.Paired.Definitions, Structures.HeavyPath.Paired.Substitution.Definitionstheorem pushWith_mass {α : Type} {weight : α → ℝ} {frame : Frame α} {p : Stacks} (payload : LabelledTree α → Tree) (valid : p.Valid) (nonnegative : 0 ≤ frameMass weight frame) : (p.pushWith weight payload frame).mass = p.mass + frameMass weight frame := RotationBST.HeavyPath.Paired.pushWith_mass payload valid nonnegativeModels.Rotation, Models.Pointer.Machine, Models.Pointer.Mirror.Definitions, Models.Pointer.Lazy.Skeleton, Models.Labelled.Cursor.Definitions, Models.Labelled.Ordering.Definitions, Structures.PopTart.Definitions, Structures.PopTart.Mirror.Definitions, Structures.PopTart.Chocolate.Definitions, Structures.HeavyPath.Definitions, Structures.HeavyPath.Paired.Definitions, Structures.HeavyPath.Paired.Substitution.Definitionstheorem pushWith_lower_size {α : Type} (weight : α → ℝ) (payload : LabelledTree α → Tree) (frame : Frame α) (p : Stacks) : (p.pushWith weight payload frame).lower.decode.tree.size = p.lower.decode.tree.size + Pointer.offset [replaceFrame payload frame] := RotationBST.HeavyPath.Paired.pushWith_lower_size weight payload frame pModels.Rotation, Models.Pointer.Machine, Models.Pointer.Mirror.Definitions, Models.Pointer.Lazy.Skeleton, Models.Labelled.Cursor.Definitions, Models.Labelled.Ordering.Definitions, Structures.PopTart.Definitions, Structures.PopTart.Mirror.Definitions, Structures.PopTart.Chocolate.Definitions, Structures.HeavyPath.Definitions, Structures.HeavyPath.Paired.Definitions, Structures.HeavyPath.Paired.Substitution.Definitionstheorem buildWith_trace {α : Type} (weight : α → ℝ) (payload : LabelledTree α → Tree) (cs : List (Frame α)) (p : Stacks) (ds : List Pointer.Frame) : Pointer.Trace ⟨p.tree, cs.map (replaceFrame payload) ++ ds⟩ ⟨(Stacks.buildWith weight payload cs p).tree, ds⟩ (Stacks.buildWithCost weight payload cs p) := RotationBST.HeavyPath.Paired.buildWith_trace weight payload cs p dsModels.Rotation, Models.Pointer.Machine, Models.Pointer.Mirror.Definitions, Models.Pointer.Lazy.Skeleton, Models.Labelled.Cursor.Definitions, Models.Labelled.Ordering.Definitions, Structures.PopTart.Definitions, Structures.PopTart.Mirror.Definitions, Structures.PopTart.Chocolate.Definitions, Structures.HeavyPath.Definitions, Structures.HeavyPath.Paired.Definitions, Structures.HeavyPath.Paired.Substitution.Definitionstheorem buildWith_valid {α : Type} {weight : α → ℝ} (payload : LabelledTree α → Tree) (cs : List (Frame α)) {p : Stacks} (valid : p.Valid) (nonnegative : ∀ frame ∈ cs, 0 ≤ frameMass weight frame) : (Stacks.buildWith weight payload cs p).Valid := RotationBST.HeavyPath.Paired.buildWith_valid payload cs valid nonnegativeModels.Rotation, Models.Pointer.Machine, Models.Pointer.Mirror.Definitions, Models.Pointer.Lazy.Skeleton, Models.Labelled.Cursor.Definitions, Models.Labelled.Ordering.Definitions, Structures.PopTart.Definitions, Structures.PopTart.Mirror.Definitions, Structures.PopTart.Chocolate.Definitions, Structures.HeavyPath.Definitions, Structures.HeavyPath.Paired.Definitions, Structures.HeavyPath.Paired.Substitution.Definitionstheorem buildWith_amortized {α : Type} {weight : α → ℝ} (payload : LabelledTree α → Tree) (cs : List (Frame α)) {p : Stacks} (valid : p.Valid) (nonnegative : ∀ frame ∈ cs, 0 ≤ frameMass weight frame) : Stacks.buildWithCost weight payload cs p + (Stacks.buildWith weight payload cs p).potential ≤ 23 * cs.length + p.potential := RotationBST.HeavyPath.Paired.buildWith_amortized payload cs valid nonnegativeModels.Rotation, Models.Pointer.Machine, Models.Pointer.Mirror.Definitions, Models.Pointer.Lazy.Skeleton, Models.Labelled.Cursor.Definitions, Models.Labelled.Ordering.Definitions, Structures.PopTart.Definitions, Structures.PopTart.Mirror.Definitions, Structures.PopTart.Chocolate.Definitions, Structures.HeavyPath.Definitions, Structures.HeavyPath.Paired.Definitions, Structures.HeavyPath.Paired.Substitution.Definitionstheorem buildWith_mass {α : Type} {weight : α → ℝ} (payload : LabelledTree α → Tree) (cs : List (Frame α)) {p : Stacks} (valid : p.Valid) (nonnegative : ∀ frame ∈ cs, 0 ≤ frameMass weight frame) : (Stacks.buildWith weight payload cs p).mass = p.mass + (cs.map (frameMass weight)).sum := RotationBST.HeavyPath.Paired.buildWith_mass payload cs valid nonnegativeModels.Rotation, Models.Pointer.Machine, Models.Pointer.Mirror.Definitions, Models.Pointer.Lazy.Skeleton, Models.Labelled.Cursor.Definitions, Models.Labelled.Ordering.Definitions, Structures.PopTart.Definitions, Structures.PopTart.Mirror.Definitions, Structures.PopTart.Chocolate.Definitions, Structures.HeavyPath.Definitions, Structures.HeavyPath.Paired.Definitions, Structures.HeavyPath.Paired.Substitution.Definitionstheorem buildWith_lower_size {α : Type} (weight : α → ℝ) (payload : LabelledTree α → Tree) (cs : List (Frame α)) (p : Stacks) : (Stacks.buildWith weight payload cs p).lower.decode.tree.size = p.lower.decode.tree.size + Pointer.offset (cs.map (replaceFrame payload)) := RotationBST.HeavyPath.Paired.buildWith_lower_size weight payload cs pModels.Rotation, Models.Pointer.Machine, Models.Pointer.Mirror.Definitions, Models.Pointer.Lazy.Skeleton, Models.Labelled.Cursor.Definitions, Models.Labelled.Ordering.Definitions, Structures.PopTart.Definitions, Structures.PopTart.Mirror.Definitions, Structures.PopTart.Chocolate.Definitions, Structures.HeavyPath.Definitions, Structures.HeavyPath.Paired.Definitions, Structures.HeavyPath.Paired.Substitution.Definitionstheorem buildWith_fringe {α : Type} {weight : α → ℝ} (payload : LabelledTree α → Tree) (cs : List (Frame α)) {p : Stacks} (valid : p.Valid) (nonnegative : ∀ frame ∈ cs, 0 ≤ frameMass weight frame) : (Stacks.buildWith weight payload cs p).lower.decode.fringe = (lowerSlotsWith weight payload cs).reverse ++ p.lower.decode.fringe ∧ (Stacks.buildWith weight payload cs p).upper.decode.mirror.fringe = p.upper.decode.mirror.fringe ++ upperSlotsWith weight payload cs := RotationBST.HeavyPath.Paired.buildWith_fringe payload cs valid nonnegativeModels.Rotation, Models.Pointer.Machine, Models.Pointer.Mirror.Definitions, Models.Pointer.Lazy.Skeleton, Models.Labelled.Cursor.Definitions, Models.Labelled.Ordering.Definitions, Structures.PopTart.Definitions, Structures.PopTart.Mirror.Definitions, Structures.PopTart.Chocolate.Definitions, Structures.HeavyPath.Definitions, Structures.HeavyPath.Paired.Definitions, Structures.HeavyPath.Paired.Substitution.DefinitionsThe push and build theorems of the previous section, with payloads: the same exact traces, now starting under the substituted frames; the same validity condition; the same amortized bound of per push; the same masses; and the same fringe description with *SlotsWith.
theorem buildWith_erase {α : Type} (weight : α → ℝ) (cs : List (Frame α)) (p : Stacks) : Stacks.buildWith weight LabelledTree.erase cs p = Stacks.build weight cs p := RotationBST.HeavyPath.Paired.buildWith_erase weight cs pModels.Rotation, Models.Pointer.Machine, Models.Pointer.Mirror.Definitions, Models.Pointer.Lazy.Skeleton, Models.Labelled.Cursor.Definitions, Models.Labelled.Ordering.Definitions, Structures.PopTart.Definitions, Structures.PopTart.Mirror.Definitions, Structures.PopTart.Chocolate.Definitions, Structures.HeavyPath.Definitions, Structures.HeavyPath.Paired.Definitions, Structures.HeavyPath.Paired.Substitution.Definitionstheorem pushWith_congr {α : Type} (weight : α → ℝ) (frame : Frame α) (p : Stacks) {first second : LabelledTree α → Tree} (same : first (framePayload frame) = second (framePayload frame)) : p.pushWith weight first frame = p.pushWith weight second frame := RotationBST.HeavyPath.Paired.pushWith_congr weight frame p sameModels.Rotation, Models.Pointer.Machine, Models.Pointer.Mirror.Definitions, Models.Pointer.Lazy.Skeleton, Models.Labelled.Cursor.Definitions, Models.Labelled.Ordering.Definitions, Structures.PopTart.Definitions, Structures.PopTart.Mirror.Definitions, Structures.PopTart.Chocolate.Definitions, Structures.HeavyPath.Definitions, Structures.HeavyPath.Paired.Definitions, Structures.HeavyPath.Paired.Substitution.Definitionstheorem buildWith_congr {α : Type} (weight : α → ℝ) (cs : List (Frame α)) (p : Stacks) {first second : LabelledTree α → Tree} (same : ∀ frame ∈ cs, first (framePayload frame) = second (framePayload frame)) : Stacks.buildWith weight first cs p = Stacks.buildWith weight second cs p := RotationBST.HeavyPath.Paired.buildWith_congr weight cs p sameModels.Rotation, Models.Pointer.Machine, Models.Pointer.Mirror.Definitions, Models.Pointer.Lazy.Skeleton, Models.Labelled.Cursor.Definitions, Models.Labelled.Ordering.Definitions, Structures.PopTart.Definitions, Structures.PopTart.Mirror.Definitions, Structures.PopTart.Chocolate.Definitions, Structures.HeavyPath.Definitions, Structures.HeavyPath.Paired.Definitions, Structures.HeavyPath.Paired.Substitution.DefinitionsWith payload = erase, buildWith is build. The two congruence lemmas say that only the payload’s values on the frames’ own subtrees matter, so the payload function may be changed elsewhere, for instance when the fuel of a recursive construction changes.
theorem replaced_plug_size {α : Type} (payload : LabelledTree α → Tree) (cs : List (Frame α)) (t : LabelledTree α) (focus : Tree) (focusSize : focus.size = t.erase.size) (same : ∀ frame ∈ cs, (payload (framePayload frame)).size = (framePayload frame).erase.size) : (Pointer.plug (cs.map (replaceFrame payload)) focus).size = (plug cs t).erase.size := RotationBST.HeavyPath.Paired.replaced_plug_size payload cs t focus focusSize sameModels.Rotation, Models.Pointer.Machine, Models.Pointer.Mirror.Definitions, Models.Pointer.Lazy.Skeleton, Models.Labelled.Cursor.Definitions, Models.Labelled.Ordering.Definitions, Structures.PopTart.Definitions, Structures.PopTart.Mirror.Definitions, Structures.PopTart.Chocolate.Definitions, Structures.HeavyPath.Definitions, Structures.HeavyPath.Paired.Definitions, Structures.HeavyPath.Paired.Substitution.Definitionstheorem buildWith_size {α : Type} (weight : α → ℝ) (payload : LabelledTree α → Tree) (cs : List (Frame α)) (p : Stacks) (t : LabelledTree α) (focusSize : p.tree.size = t.erase.size) (same : ∀ frame ∈ cs, (payload (framePayload frame)).size = (framePayload frame).erase.size) : (Stacks.buildWith weight payload cs p).tree.size = (plug cs t).erase.size := RotationBST.HeavyPath.Paired.buildWith_size weight payload cs p t focusSize sameModels.Rotation, Models.Pointer.Machine, Models.Pointer.Mirror.Definitions, Models.Pointer.Lazy.Skeleton, Models.Labelled.Cursor.Definitions, Models.Labelled.Ordering.Definitions, Structures.PopTart.Definitions, Structures.PopTart.Mirror.Definitions, Structures.PopTart.Chocolate.Definitions, Structures.HeavyPath.Definitions, Structures.HeavyPath.Paired.Definitions, Structures.HeavyPath.Paired.Substitution.Definitionstheorem replaced_offset {α : Type} (payload : LabelledTree α → Tree) (cs : List (Frame α)) (same : ∀ frame ∈ cs, (payload (framePayload frame)).size = (framePayload frame).erase.size) : Pointer.offset (cs.map (replaceFrame payload)) = Pointer.offset (cs.map Frame.erase) := RotationBST.HeavyPath.Paired.replaced_offset payload cs sameModels.Rotation, Models.Pointer.Machine, Models.Pointer.Mirror.Definitions, Models.Pointer.Lazy.Skeleton, Models.Labelled.Cursor.Definitions, Models.Labelled.Ordering.Definitions, Structures.PopTart.Definitions, Structures.PopTart.Mirror.Definitions, Structures.PopTart.Chocolate.Definitions, Structures.HeavyPath.Definitions, Structures.HeavyPath.Paired.Definitions, Structures.HeavyPath.Paired.Substitution.DefinitionsIf every payload has the size of its source subtree, nothing moves in rank. The substituted context plugs to a tree of the source size, the built pair has the size of the source tree, and the substituted frames have the same inorder offset as the erased ones. This is how ranks in the physical tree are translated back to the source.
38.1.4 Exposure: popping the topmost ancestor
A push makes the heavy path one node longer at the top. The opposite operation removes the topmost ancestor, the one farthest from the endpoint, and makes it the physical root again, with its payload on its side and the rest of the path, still paired, below it. The topmost frame of the heavy path from the root holds the source root itself, so exposing it makes the source root the physical root, with its off-path payload on its side. Section 38.2 uses this to expose the source root and join it back.
/-- A path representation permits any valid Chocolate history with thespecified payload fringe. In particular, popping need not recover the shapechosen by the original sequence of pushes. -/def Represents {α : Type} (weight : α → ℝ) (payload : LabelledTree α → Tree) (cs : List (Frame α)) (p : Stacks) : Prop := p.Valid ∧ p.lower.decode.fringe = (lowerSlotsWith weight payload cs).reverse ++ [(.nil, 0)] ∧ p.upper.decode.fringe = (upperSlotsWith weight (fun t => (payload t).mirror) cs).reverse ++ [(.nil, 0)]/-- Enter the selected stack, perform its pop, return to the endpoint,then descend and rotate to make the exposed ancestor the physical root. -/def exposeCost {α : Type} (p : Stacks) : Frame α → Nat | .left _ _ => p.upper.popCost + 4 | .right _ _ => p.lower.popCost + 4p.Represents weight payload cs says that p is a valid pair that stores exactly the frames cs with payloads payload. Both fringes, read in stored coordinates, are the expected slot lists followed by the empty leaf. The pop-tart shapes are not fixed. Any valid Chocolate history with the right fringe is allowed, and a pop need not undo the shape of the push it reverses. exposeCost is the cost of the exposure: the cursor enters the stack on the frame’s side, the stack pops (Layer.popCost), and four more instructions return to the endpoint, descend and rotate the popped separator to the root.
theorem lower_trace {p : Stacks} {crumb : WeightedTree} {rest : Layer} (popped : p.lower.pop = some (crumb, rest)) : Pointer.Trace ⟨p.tree, []⟩ ⟨.node crumb.tree (Stacks.tree ⟨rest, p.upper⟩), []⟩ (p.lower.popCost + 4) := RotationBST.HeavyPath.Paired.Exposure.lower_trace poppedModels.Rotation, Models.Pointer.Machine, Models.Pointer.Mirror.Definitions, Models.Pointer.Lazy.Skeleton, Models.Labelled.Cursor.Definitions, Structures.PopTart.Definitions, Structures.PopTart.Mirror.Definitions, Structures.PopTart.Chocolate.Definitions, Structures.HeavyPath.Definitions, Structures.HeavyPath.Paired.Definitions, Structures.HeavyPath.Paired.Substitution.Definitions, Structures.HeavyPath.Paired.Exposure.Definitionstheorem upper_trace {p : Stacks} {crumb : WeightedTree} {rest : Layer} (popped : p.upper.pop = some (crumb, rest)) : Pointer.Trace ⟨p.tree, []⟩ ⟨.node (Stacks.tree ⟨p.lower, rest⟩) crumb.tree.mirror, []⟩ (p.upper.popCost + 4) := RotationBST.HeavyPath.Paired.Exposure.upper_trace poppedModels.Rotation, Models.Pointer.Machine, Models.Pointer.Mirror.Definitions, Models.Pointer.Lazy.Skeleton, Models.Labelled.Cursor.Definitions, Structures.PopTart.Definitions, Structures.PopTart.Mirror.Definitions, Structures.PopTart.Chocolate.Definitions, Structures.HeavyPath.Definitions, Structures.HeavyPath.Paired.Definitions, Structures.HeavyPath.Paired.Substitution.Definitions, Structures.HeavyPath.Paired.Exposure.DefinitionsThe physical part. If the lower pop-tart pops a leaf, the crumb crumb, leaving rest (Layer.pop), then a trace of popCost + 4 instructions turns the pair into a root with crumb’s tree on the left and the remaining pair on the right; the root is the separator. The upper case is the mirror image.
theorem empty_represents {α : Type} (weight : α → ℝ) (payload : LabelledTree α → Tree) : Stacks.empty.Represents weight payload [] := RotationBST.HeavyPath.Paired.Exposure.empty_represents weight payloadModels.Rotation, Models.Pointer.Machine, Models.Pointer.Mirror.Definitions, Models.Pointer.Lazy.Skeleton, Models.Labelled.Cursor.Definitions, Structures.PopTart.Definitions, Structures.PopTart.Mirror.Definitions, Structures.PopTart.Chocolate.Definitions, Structures.HeavyPath.Definitions, Structures.HeavyPath.Paired.Definitions, Structures.HeavyPath.Paired.Substitution.Definitions, Structures.HeavyPath.Paired.Exposure.Definitionstheorem push_represents {α : Type} {weight : α → ℝ} {payload : LabelledTree α → Tree} {cs : List (Frame α)} {p : Stacks} (represented : p.Represents weight payload cs) (frame : Frame α) (nonnegative : 0 ≤ frameMass weight frame) : (p.pushWith weight payload frame).Represents weight payload (cs ++ [frame]) := RotationBST.HeavyPath.Paired.Exposure.push_represents represented frame nonnegativeModels.Rotation, Models.Pointer.Machine, Models.Pointer.Mirror.Definitions, Models.Pointer.Lazy.Skeleton, Models.Labelled.Cursor.Definitions, Structures.PopTart.Definitions, Structures.PopTart.Mirror.Definitions, Structures.PopTart.Chocolate.Definitions, Structures.HeavyPath.Definitions, Structures.HeavyPath.Paired.Definitions, Structures.HeavyPath.Paired.Substitution.Definitions, Structures.HeavyPath.Paired.Exposure.Definitionstheorem build_represents {α : Type} {weight : α → ℝ} (payload : LabelledTree α → Tree) (cs : List (Frame α)) (nonnegative : ∀ frame ∈ cs, 0 ≤ frameMass weight frame) : (Stacks.buildWith weight payload cs Stacks.empty).Represents weight payload cs := RotationBST.HeavyPath.Paired.Exposure.build_represents payload cs nonnegativeModels.Rotation, Models.Pointer.Machine, Models.Pointer.Mirror.Definitions, Models.Pointer.Lazy.Skeleton, Models.Labelled.Cursor.Definitions, Structures.PopTart.Definitions, Structures.PopTart.Mirror.Definitions, Structures.PopTart.Chocolate.Definitions, Structures.HeavyPath.Definitions, Structures.HeavyPath.Paired.Definitions, Structures.HeavyPath.Paired.Substitution.Definitions, Structures.HeavyPath.Paired.Exposure.Definitionstheorem represents_sizes {α : Type} {weight : α → ℝ} {payload : LabelledTree α → Tree} {cs : List (Frame α)} {p q : Stacks} (first : p.Represents weight payload cs) (second : q.Represents weight payload cs) : p.lower.decode.tree.size = q.lower.decode.tree.size ∧ p.upper.decode.tree.size = q.upper.decode.tree.size ∧ p.tree.size = q.tree.size := RotationBST.HeavyPath.Paired.Exposure.represents_sizes first secondModels.Rotation, Models.Pointer.Machine, Models.Pointer.Mirror.Definitions, Models.Pointer.Lazy.Skeleton, Models.Labelled.Cursor.Definitions, Structures.PopTart.Definitions, Structures.PopTart.Mirror.Definitions, Structures.PopTart.Chocolate.Definitions, Structures.HeavyPath.Definitions, Structures.HeavyPath.Paired.Definitions, Structures.HeavyPath.Paired.Substitution.Definitions, Structures.HeavyPath.Paired.Exposure.Definitionsempty represents the empty path, a push with a nonnegative charge appends its frame, and so buildWith represents its frames. Two representations of the same frames have the same lower, upper and total sizes, even if their shapes differ. The rank of the endpoint therefore depends only on the path, not on the pop-tart history.
theorem expose_frame {α : Type} {weight : α → ℝ} {payload : LabelledTree α → Tree} {cs : List (Frame α)} {frame : Frame α} {p : Stacks} (represented : p.Represents weight payload (cs ++ [frame])) : ∃ rest : Stacks, rest.Represents weight payload cs ∧ Pointer.Trace ⟨p.tree, []⟩ ⟨Pointer.plug [replaceFrame payload frame] rest.tree, []⟩ (p.exposeCost frame) ∧ p.exposeCost frame + rest.potential ≤ 24 + p.potential ∧ p.mass = rest.mass + frameMass weight frame := RotationBST.HeavyPath.Paired.Exposure.expose_frame representedModels.Rotation, Models.Pointer.Machine, Models.Pointer.Mirror.Definitions, Models.Pointer.Lazy.Skeleton, Models.Labelled.Cursor.Definitions, Structures.PopTart.Definitions, Structures.PopTart.Mirror.Definitions, Structures.PopTart.Chocolate.Definitions, Structures.HeavyPath.Definitions, Structures.HeavyPath.Paired.Definitions, Structures.HeavyPath.Paired.Substitution.Definitions, Structures.HeavyPath.Paired.Exposure.DefinitionsTheorem (exposure). If represents the frames cs ++ [frame], where frame is the topmost ancestor, then some pair rest represents cs, and a trace of exactly exposeCost frame instructions turns the tree of into the topmost separator at the root, its substituted payload on its side and rest as its other child. Moreover
Together with push, this makes the top of the path a stack with constant amortized cost per operation: frames enter by pushWith and leave by expose_frame.
38.1.5 Population: worst-case costs
The deamortization needs more than amortized bounds: every single response must cost . The Chocolate pop-tart bounds the cost of one push and one pop by the logarithm of its number of leaves (Results.push_worst_case, Results.pop_worst_case). In a pair, the number of leaves of each stack, its population, is at most the number of nodes of the pair.
theorem leaf_count_size (t : WeightedTree) : 1 ≤ t.leafCount ∧ t.leafCount ≤ t.tree.size + 1 := RotationBST.HeavyPath.Paired.Population.leaf_count_size tModels.Rotation, Models.Pointer.Machine, Models.Pointer.Mirror.Definitions, Models.Pointer.Lazy.Skeleton, Models.Labelled.Cursor.Definitions, Structures.PopTart.Definitions, Structures.PopTart.Chocolate.Definitions, Structures.PopTart.Mirror.Definitions, Structures.HeavyPath.Definitions, Structures.HeavyPath.Paired.Definitions, Structures.HeavyPath.Paired.Substitution.Definitions, Structures.HeavyPath.Paired.Exposure.Definitionstheorem pair_population (p : Stacks) : 1 ≤ p.lower.decode.leafCount ∧ p.lower.decode.leafCount ≤ p.tree.size ∧ 1 ≤ p.upper.decode.leafCount ∧ p.upper.decode.leafCount ≤ p.tree.size := RotationBST.HeavyPath.Paired.Population.pair_population pModels.Rotation, Models.Pointer.Machine, Models.Pointer.Mirror.Definitions, Models.Pointer.Lazy.Skeleton, Models.Labelled.Cursor.Definitions, Structures.PopTart.Definitions, Structures.PopTart.Chocolate.Definitions, Structures.PopTart.Mirror.Definitions, Structures.HeavyPath.Definitions, Structures.HeavyPath.Paired.Definitions, Structures.HeavyPath.Paired.Substitution.Definitions, Structures.HeavyPath.Paired.Exposure.DefinitionsA weighted tree has between and leaves, where counts its internal nodes. In a pair, each stack has between and leaves, since the endpoint is a node outside both stacks.
theorem push_cost_bound {α : Type} {p : Stacks} (valid : p.Valid) (frame : Frame α) {n : Nat} (size : p.tree.size ≤ n) : (p.pushCost frame + 1 : ℝ) ≤ 10 + 14 * Real.logb 2 n := RotationBST.HeavyPath.Paired.Population.push_cost_bound valid frame sizeModels.Rotation, Models.Pointer.Machine, Models.Pointer.Mirror.Definitions, Models.Pointer.Lazy.Skeleton, Models.Labelled.Cursor.Definitions, Structures.PopTart.Definitions, Structures.PopTart.Chocolate.Definitions, Structures.PopTart.Mirror.Definitions, Structures.HeavyPath.Definitions, Structures.HeavyPath.Paired.Definitions, Structures.HeavyPath.Paired.Substitution.Definitions, Structures.HeavyPath.Paired.Exposure.Definitionstheorem expose_cost_bound {α : Type} {p : Stacks} (valid : p.Valid) (frame : Frame α) {n : Nat} (size : p.tree.size ≤ n) : (p.exposeCost frame : ℝ) ≤ 10 + 10 * Real.logb 2 n := RotationBST.HeavyPath.Paired.Population.expose_cost_bound valid frame sizeModels.Rotation, Models.Pointer.Machine, Models.Pointer.Mirror.Definitions, Models.Pointer.Lazy.Skeleton, Models.Labelled.Cursor.Definitions, Structures.PopTart.Definitions, Structures.PopTart.Chocolate.Definitions, Structures.PopTart.Mirror.Definitions, Structures.HeavyPath.Definitions, Structures.HeavyPath.Paired.Definitions, Structures.HeavyPath.Paired.Substitution.Definitions, Structures.HeavyPath.Paired.Exposure.DefinitionsTheorem (worst-case push and exposure). For a valid pair with at most nodes, one push, with the instruction that enters it from the ancestor, costs at most , and one exposure costs at most .
These bounds hold for each operation separately, without potential, and for any frame. They count the nodes of the pair, payloads included, so can be taken to be the size of the whole tree.
38.1.6 Interruption: depth at every instruction
The deamortized algorithm does not run the simulation in whole operations. It interleaves source work with the service of requests, and it may stop a push or a pop between any two instructions and serve a request from the tree as it stands. Depth bounds that hold only before and after a push are not enough. The interruption lemmas bound the depth of every key at every instruction boundary, relative to its depth at the start, using Pointer.TraceWithin traces, which check an invariant on every intermediate tree.
theorem lower_pop {p : Stacks} {crumb : WeightedTree} {rest : Layer} (popped : p.lower.pop = some (crumb, rest)) : Pointer.TraceWithin (fun t => t.size = p.tree.size ∧ ∀ x, depth t x ≤ 4 * depth p.tree x + 10) ⟨p.tree, []⟩ ⟨.node crumb.tree (Stacks.tree ⟨rest, p.upper⟩), []⟩ (p.lower.popCost + 4) := RotationBST.HeavyPath.Paired.Interruption.lower_pop poppedModels.Rotation, Models.Pointer.Machine, Models.Pointer.Mirror.Definitions, Models.Pointer.Lazy.Skeleton, Models.Labelled.Cursor.Definitions, Structures.PopTart.Definitions, Structures.PopTart.Mirror.Definitions, Structures.PopTart.Chocolate.Definitions, Structures.HeavyPath.Definitions, Structures.HeavyPath.Paired.Definitions, Structures.HeavyPath.Paired.Substitution.Definitions, Structures.HeavyPath.Paired.Exposure.Definitions, Bounds.StaticTree.Definitions, Models.Pointer.Invariant.Definitionstheorem upper_pop {p : Stacks} {crumb : WeightedTree} {rest : Layer} (popped : p.upper.pop = some (crumb, rest)) : Pointer.TraceWithin (fun t => t.size = p.tree.size ∧ ∀ x, depth t x ≤ 4 * depth p.tree x + 10) ⟨p.tree, []⟩ ⟨.node (Stacks.tree ⟨p.lower, rest⟩) crumb.tree.mirror, []⟩ (p.upper.popCost + 4) := RotationBST.HeavyPath.Paired.Interruption.upper_pop poppedModels.Rotation, Models.Pointer.Machine, Models.Pointer.Mirror.Definitions, Models.Pointer.Lazy.Skeleton, Models.Labelled.Cursor.Definitions, Structures.PopTart.Definitions, Structures.PopTart.Mirror.Definitions, Structures.PopTart.Chocolate.Definitions, Structures.HeavyPath.Definitions, Structures.HeavyPath.Paired.Definitions, Structures.HeavyPath.Paired.Substitution.Definitions, Structures.HeavyPath.Paired.Exposure.Definitions, Bounds.StaticTree.Definitions, Models.Pointer.Invariant.Definitionstheorem lower_push (p : Stacks) (payload : Tree) (weight : ℝ) : Pointer.TraceWithin (fun t => t.size = (Tree.node payload p.tree).size ∧ ∀ x, depth t x ≤ 4 * depth (.node payload p.tree) x + 16) ⟨.node payload p.tree, []⟩ ⟨Stacks.tree ⟨p.lower.push (.leaf payload weight), p.upper⟩, []⟩ (p.lower.pushCost + 4) := RotationBST.HeavyPath.Paired.Interruption.lower_push p payload weightModels.Rotation, Models.Pointer.Machine, Models.Pointer.Mirror.Definitions, Models.Pointer.Lazy.Skeleton, Models.Labelled.Cursor.Definitions, Structures.PopTart.Definitions, Structures.PopTart.Mirror.Definitions, Structures.PopTart.Chocolate.Definitions, Structures.HeavyPath.Definitions, Structures.HeavyPath.Paired.Definitions, Structures.HeavyPath.Paired.Substitution.Definitions, Structures.HeavyPath.Paired.Exposure.Definitions, Bounds.StaticTree.Definitions, Models.Pointer.Invariant.Definitionstheorem upper_push (p : Stacks) (payload : Tree) (weight : ℝ) : Pointer.TraceWithin (fun t => t.size = (Tree.node p.tree payload).size ∧ ∀ x, depth t x ≤ 4 * depth (.node p.tree payload) x + 16) ⟨.node p.tree payload, []⟩ ⟨Stacks.tree ⟨p.lower, p.upper.push (.leaf payload.mirror weight)⟩, []⟩ (p.upper.pushCost + 4) := RotationBST.HeavyPath.Paired.Interruption.upper_push p payload weightModels.Rotation, Models.Pointer.Machine, Models.Pointer.Mirror.Definitions, Models.Pointer.Lazy.Skeleton, Models.Labelled.Cursor.Definitions, Structures.PopTart.Definitions, Structures.PopTart.Mirror.Definitions, Structures.PopTart.Chocolate.Definitions, Structures.HeavyPath.Definitions, Structures.HeavyPath.Paired.Definitions, Structures.HeavyPath.Paired.Substitution.Definitions, Structures.HeavyPath.Paired.Exposure.Definitions, Bounds.StaticTree.Definitions, Models.Pointer.Invariant.Definitionstheorem push_frame {α : Type} (weight : α → ℝ) (payload : LabelledTree α → Tree) (frame : LabelledCursor.Frame α) (p : Stacks) : Pointer.TraceWithin (fun t => t.size = (Pointer.plug [replaceFrame payload frame] p.tree).size ∧ ∀ x, depth t x ≤ 4 * depth (Pointer.plug [replaceFrame payload frame] p.tree) x + 16) ⟨Pointer.plug [replaceFrame payload frame] p.tree, []⟩ ⟨(p.pushWith weight payload frame).tree, []⟩ (p.pushCost frame + 1) := RotationBST.HeavyPath.Paired.Interruption.push_frame weight payload frame pModels.Rotation, Models.Pointer.Machine, Models.Pointer.Mirror.Definitions, Models.Pointer.Lazy.Skeleton, Models.Labelled.Cursor.Definitions, Structures.PopTart.Definitions, Structures.PopTart.Mirror.Definitions, Structures.PopTart.Chocolate.Definitions, Structures.HeavyPath.Definitions, Structures.HeavyPath.Paired.Definitions, Structures.HeavyPath.Paired.Substitution.Definitions, Structures.HeavyPath.Paired.Exposure.Definitions, Bounds.StaticTree.Definitions, Models.Pointer.Invariant.DefinitionsTheorem (interruptible pushes and pops). The exposure traces of lower_trace and upper_trace can be taken so that every intermediate tree has the size of the starting tree, and every rank has depth at most , where is its depth in the starting tree. Likewise for a push that starts from the tree in which the new ancestor is the physical root, with its payload on one side and the pair on the other, and the cursor at that root. It takes pushCost + 1 instructions, the stack’s push cost plus four, and can be done so that every intermediate depth is at most .
lower_push and upper_push state the push for a raw payload and charge. push_frame states it for a source frame with a substituted payload, ending at pushWith. The bounds are pointwise and linear in the starting depth, so they compose with the logarithmic depth bounds of the starting tree. The predicate quantifies over all natural numbers x, including ranks outside the tree, where Tree.depth is a totalized value. The bound holds for those as well, and is used only for actual keys. The policies of Section 38.4 carry certificates of this kind for each emitted program (their PolicyWithin condition). That is how the depth bound at every cut in Results.simulation is obtained.