Chapter 25 · GreedyASS · Section 25.4The reference forest
The analysis of GreedyASS in Section 25.3 compares it with a lazy finger moving in a fixed reference tree. That comparison has to be made one request at a time. It is made against a snapshot of the reference tree: after a search for the leaf , delete the path from the root down to . What is left is a forest of subtrees hanging off that path, together with itself as a one-leaf tree. Iacono and Langerman measure every GreedyASS item against the component of this forest that contains it. When the next search goes from to , the forest changes by a sequence of local merges and splits along the path from to , one per reference edge on that path. This is where the lazy-finger cost enters the analysis.
This section builds the reference tree, the forest, the distances that the potential of Section 25.5 is made of, and the merges and splits between consecutive forests. It ends with the conversion from an ordinary BST to a reference tree, which is how Section 25.3 turns the leaf-oriented Theorem 3.1 into the bound against ordinary lazy-finger trees. Nothing here mentions GreedyASS, except Section 25.4.5, which shows that a persistent component acts as a barrier for the sweep.
To check the headline results of Section 25.3, you do not: their statements use only ordinary trees and the lazy-finger and weighted-finger costs of Chapter 15. To check the statement of Theorem 3.1 (Section 25.6), you need only Reference.leaves from Section 25.4.1 and Transitions.edges from Section 25.4.4. The conversion of Section 25.4.6 is how the ordinary bounds follow from it. Everything else is proof machinery for Section 25.5 and Section 25.6, to be read when following the potential argument.
The definitions are in Algorithms/GreedyASS/Reference/Definitions/Abstract.lean and in the Definitions/Abstract.lean files of its submodules Distance, Transitions and Conversion; the theorems are in the corresponding Abstract.lean files.
25.4.1 Leaf-oriented trees
The library’s Tree is a bare shape whose internal nodes carry the keys, in inorder (Chapter 1). The paper’s reference tree is leaf-oriented: the keys sit at the leaves, in left-to-right order, and internal nodes only route searches. The library reuses the same type and reads it differently: Tree.nil is a leaf, and node l r is an internal node whose leaves are those of l followed by those of r.
/-- The reference tree is leaf-oriented: `Tree.nil` is one labelled leaf,and a node joins two nonempty leaf ranges. Ordinary BST node ranks are notused for this reference tree. -/def leaves : Tree → Nat | .nil => 1 | .node l r => leaves l + leaves rleaves t is the number of leaves. Under this reading a tree with internal nodes has leaves, so Tree.nil is a one-key tree rather than the empty tree. Leaves are numbered by rank. Every function of this section takes an offset lo, the rank of the tree’s first leaf, so that a subtree can be addressed inside a larger tree: the leaves of node l r at offset lo are those of l at offset lo and those of r at offset lo + leaves l.
The analysis works with subtrees of the reference tree, and different subtrees may have the same shape. So a subtree is identified by its shape and its position:
/-- A subtree occurrence, identified by its shape and first leaf rank. -/structure Component where first : Nat shape : Tree deriving DecidableEq, Reprdef Component.last (c : Component) : Nat := c.first + leaves c.shapedef Component.Contains (c : Component) (key : Nat) : Prop := c.first ≤ key ∧ key < c.lastA Component is a pair: the rank first of its first leaf and its shape. It covers the half-open interval of leaves , where last = first + leaves shape, and Contains c key says that the leaf key lies in that interval. Two components with the same shape at different offsets are different. A Component is only a pair of data: nothing in the type says that it occurs in a given reference tree. That fact is the predicate Distance.Occurs below, and the theorems state it where it is needed.
instance (c : Component) (key : Nat) : Decidable (c.Contains key) := inferInstanceAs (Decidable (c.first ≤ key ∧ key < c.last))Containment is two comparisons, so it is decidable.
25.4.2 The forest of a search
/-- Remove the proper ancestors of the searched leaf. The resulting forestis listed in increasing leaf order and includes the searched singleton.Invalid searches are totalized; search-side theorems require a valid rank. -/def forest : Tree → Nat → Nat → List Component | .nil, lo, _ => [⟨lo, .nil⟩] | .node l r, lo, searched => if searched < lo + leaves l then forest l lo searched ++ [⟨lo + leaves l, r⟩] else ⟨lo, l⟩ :: forest r (lo + leaves l) searchedforest t lo searched walks from the root towards the leaf searched. At each internal node it keeps the child that the search does not enter as a whole component, and recurses into the other one. At the leaf it keeps the singleton ⟨searched, nil⟩. The proper ancestors of the searched leaf, the internal nodes on the search path, are exactly what is removed. The children that branch off to the left come before the recursive part and those that branch off to the right after it, so the list is in increasing leaf order. The forest has components, where is the depth of the searched leaf: one per edge of the search path, plus the singleton.
For example, take the reference tree with and . Its leaves are , with holding and holding . The search for leaf goes right, right, left, and
The search for leaf removes the same three internal nodes and gives the same forest. Only the searched singleton differs, instead of . This is why several theorems below exclude the searched singleton by an explicit hypothesis.
The function is total. A rank beyond the last leaf is treated like a search for the last leaf, and a rank below lo like a search for the first. The theorems that concern the searched leaf require a valid rank.
Theorem (layout). A tree has one more leaf than internal nodes. The components of every forest are listed left to right with disjoint intervals, and each is a nonempty interval inside the tree’s range.
/-- The leaf interpretation has one more key than internal nodes. Its forestis ordered, with nonempty component intervals inside the original range. -/theorem layout (t : Tree) (lo searched : Nat) : leaves t = t.size + 1 ∧ (forest t lo searched).Pairwise (fun a b => a.last ≤ b.first) ∧ ∀ c ∈ forest t lo searched, lo ≤ c.first ∧ c.first < c.last ∧ c.last ≤ lo + leaves t := ⟨leaves_eq t, ordered t lo searched, fun c hc => ⟨(bounds t lo searched hc).1, component_nonempty c, (bounds t lo searched hc).2⟩⟩The first conjunct, leaves t = t.size + 1, is the dictionary between the two readings of a tree: Tree.size counts internal nodes. The second says that each component ends before the next one starts, and the third that . These hold even for an invalid searched.
Theorem (partition). Every leaf of the tree lies in exactly one component of the forest.
theorem partition (t : Tree) (lo searched key : Nat) (valid : lo ≤ key ∧ key < lo + leaves t) : ∃! c, c ∈ forest t lo searched ∧ c.Contains key := Reference.partition t lo searched key validTheorem (the searched singleton). After a search for a valid leaf , the singleton belongs to the forest and is the only component that contains .
/-- Exactly the searched singleton contains the current searched key. -/theorem searched (t : Tree) (lo key : Nat) (valid : lo ≤ key ∧ key < lo + leaves t) : (⟨key, .nil⟩ : Component) ∈ forest t lo key ∧ ∀ c ∈ forest t lo key, c.Contains key → c = ⟨key, .nil⟩ := ⟨selected t lo key valid, fun _ member contains => contains_searched t lo key valid member contains⟩25.4.3 Distances in the reference tree
The potential of Section 25.5 assigns each GreedyASS item a value built from the length of a path in the reference tree: from the item’s leaf up to the root of its forest component, or up to the lowest common ancestor of two other leaves. This section defines those lengths and proves the comparisons the potential needs.
/-- Edge depth of a leaf in a reference subtree whose first rank is `lo`. -/def depth : Tree → Nat → Nat → Nat | .nil, _, _ => 0 | .node l r, lo, x => if x < lo + leaves l then depth l lo x + 1 else depth r (lo + leaves l) x + 1/-- Edge depth of the lowest common ancestor of two reference leaves. -/def lcaDepth : Tree → Nat → Nat → Nat → Nat | .nil, _, _, _ => 0 | .node l r, lo, a, b => if a < lo + leaves l ∧ b < lo + leaves l then lcaDepth l lo a b + 1 else if lo + leaves l ≤ a ∧ lo + leaves l ≤ b then lcaDepth r (lo + leaves l) a b + 1 else 0/-- The actual subtree occurrence rooted at the lowest common ancestor. -/def lca : Tree → Nat → Nat → Nat → Component | .nil, lo, _, _ => ⟨lo, .nil⟩ | .node l r, lo, a, b => if a < lo + leaves l ∧ b < lo + leaves l then lca l lo a b else if lo + leaves l ≤ a ∧ lo + leaves l ≤ b then lca r (lo + leaves l) a b else ⟨lo, .node l r⟩depth t lo x is the edge depth of the leaf : the number of internal nodes above it. lcaDepth t lo a b is the edge depth of the lowest common ancestor of the leaves and , and lca t lo a b is that ancestor’s subtree, as a Reference.Component. All three descend together while both leaves are on the same side, and stop at the first node that separates them. For the lowest common ancestor is the leaf itself.
Positions in the tree are described by an inductive predicate:
/-- Subtree occurrences retain their offset and edge depth. Equal shapes atdifferent positions are distinct occurrences. -/inductive Occurs : Tree → Nat → Component → Nat → Prop | root (t : Tree) (lo : Nat) : Occurs t lo ⟨lo, t⟩ 0 | left {l r : Tree} {lo : Nat} {c : Component} {k : Nat} : Occurs l lo c k → Occurs (.node l r) lo c (k + 1) | right {l r : Tree} {lo : Nat} {c : Component} {k : Nat} : Occurs r (lo + leaves l) c k → Occurs (.node l r) lo c (k + 1)Occurs t lo c k says that the component c is the subtree of t (at offset lo) rooted at depth k. The root occurs at depth , and the occurrences of each child, shifted to its offset, occur one level deeper. This is what makes a component an actual subtree of the reference tree, rather than a pair of a shape and an offset.
The two lengths used by the potential count nodes, not edges:
/-- Node-counted distance to the LCA of enclosing leaves. The geometricinterpretation requires `a ≤ x ≤ b` and valid ranks. -/def toLCA (t : Tree) (lo x a b : Nat) : Nat := depth t lo x - lcaDepth t lo a b + 1/-- Node-counted distance from a contained leaf to the component root. -/def toRoot (c : Component) (x : Nat) : Nat := depth c.shape c.first x + 1toLCA t lo x a b = depth x - lcaDepth a b + 1is, when , the number of nodes on the path from the leaf up to the lowest common ancestor of and , both ends included. The hypothesis ensures that this ancestor is an ancestor of . Outside it the formula is a totalized natural-number subtraction.toRoot c x = depth c.shape c.first x + 1is the number of nodes on the path from the leaf up to the root of the componentc, both ends included. For a singleton it is .
Theorem (lowest common ancestor). For valid leaves and , the subtree lca a b occurs at depth lcaDepth a b and contains both leaves, and every subtree that contains both occurs at that depth or above.
/-- The computed LCA is a common ancestor at the stated depth, and no commonancestor occurrence has greater depth. Ranks use the reference leaf order. -/theorem lowest_common_ancestor (t : Tree) (lo a b : Nat) (validA : lo ≤ a ∧ a < lo + leaves t) (validB : lo ≤ b ∧ b < lo + leaves t) : Occurs t lo (lca t lo a b) (lcaDepth t lo a b) ∧ (lca t lo a b).Contains a ∧ (lca t lo a b).Contains b ∧ ∀ c k, Occurs t lo c k → c.Contains a → c.Contains b → k ≤ lcaDepth t lo a b := ⟨lca_occurs t lo a b, (lca_contains t lo a b validA validB).1, (lca_contains t lo a b validA validB).2, fun _ _ occurs ha hb => lca_lowest occurs ha hb⟩Theorem (path). If are valid leaves, then toLCA x a b is the node count from to the root of the subtree lca a b.
/-- The formula counts nodes on the upward path inside the actual LCA subtree. -/theorem path (t : Tree) (lo a x b : Nat) (ax : a ≤ x) (xb : x ≤ b) (validA : lo ≤ a ∧ a < lo + leaves t) (validB : lo ≤ b ∧ b < lo + leaves t) : toLCA t lo x a b = toRoot (lca t lo a b) x := toLCA_eq t lo a x b ax xb validA validBSo the arithmetic formula of toLCA measures what it should. The next two theorems say that widening the interval moves its lowest common ancestor up, and so lengthens the path. The first is the paper’s Fact 3.1, the second is its general form.
Theorem (Fact 3.1). For leaves , the distance from to the lowest common ancestor of and is at most that to the lowest common ancestor of and , and the distance from to the lowest common ancestor of and is at most that to the lowest common ancestor of and .
/-- Fact 3.1. The arithmetic inequalities also hold for totalized ranks;their reference-path interpretation applies to valid leaves. -/theorem ordered (t : Tree) (lo w x y z : Nat) (wx : w ≤ x) (xy : x ≤ y) (yz : y ≤ z) : toLCA t lo x w y ≤ toLCA t lo x w z ∧ toLCA t lo y x z ≤ toLCA t lo y w z := four_leaves t lo w x y z wx xy yzTheorem (enclosing intervals). If , then toLCA x b c ≤ toLCA x a d for every .
theorem interval (t : Tree) (lo x a b c d : Nat) (ab : a ≤ b) (bc : b ≤ c) (cd : c ≤ d) : toLCA t lo x b c ≤ toLCA t lo x a d := enclosing t lo x a b c d ab bc cdNeither statement needs valid ranks: the inequalities hold for the totalized formula. They are meaningful as path lengths only for valid leaves with between the endpoints.
Distances can be computed inside any subtree that contains the leaves in question:
Theorem (subtrees). If the component occurs at depth and contains , and , then the depth of is plus its depth in , and toLCA x a b is the same whether computed in the whole tree or in .
theorem subtree {t : Tree} {lo k a x b : Nat} {c : Component} (occurs : Occurs t lo c k) (insideX : c.Contains x) (insideA : c.Contains a) (insideB : c.Contains b) : depth t lo x = k + depth c.shape c.first x ∧ toLCA t lo x a b = toLCA c.shape c.first x a b := ⟨occurs.depth_eq insideX, occurs.toLCA_eq insideX insideA insideB⟩Theorem (forest components occur). Every component of a search forest is an actual subtree of the reference tree.
theorem forest (t : Tree) (lo searched : Nat) {c : Component} (member : c ∈ Reference.forest t lo searched) : ∃ k, Occurs t lo c k := forest_occurs t lo searched memberThe next theorem is the bridge between the two kinds of distance, and the reason why they are compared at all. Take a forest component that contains , and leaves . Going up from , the path reaches the lowest common ancestor of and no later than the root of exactly when that ancestor is inside , that is, exactly when contains and .
Theorem (component comparison). Let be a component of the forest of some search and let . For valid leaves , toLCA x a b ≤ toRoot c x holds if and only if contains both and , and the strict reverse inequality holds if and only if misses one of them.
/-- The distance comparison underlying Fact 3.2. Color assignment, missingexternal neighbors, and the top-corner exception are separate obligations. -/theorem component (t : Tree) (lo searched a x b : Nat) {c : Component} (member : c ∈ Reference.forest t lo searched) (inside : c.Contains x) (ax : a ≤ x) (xb : x ≤ b) (validA : lo ≤ a ∧ a < lo + leaves t) (validB : lo ≤ b ∧ b < lo + leaves t) : (toLCA t lo x a b ≤ toRoot c x ↔ c.Contains a ∧ c.Contains b) ∧ (toRoot c x < toLCA t lo x a b ↔ ¬ c.Contains a ∨ ¬ c.Contains b) := ⟨forest_compare t lo searched a x b member inside ax xb validA validB, forest_outside t lo searched a x b member inside ax xb validA validB⟩In Section 25.5, and are the neighbors of an item in the geometric point set, and this comparison decides which of its candidate values is selected. The docstring calls it “the distance comparison underlying Fact 3.2”: the color assignments themselves are proved there.
Theorem (one level up). The root of node l r is one node farther from each leaf than the root of the child containing it.
/-- Each parent merge increases the node-counted distance by exactly one. -/theorem merge (l r : Tree) (lo x : Nat) : (lo ≤ x ∧ x < lo + leaves l → toRoot ⟨lo, .node l r⟩ x = toRoot ⟨lo, l⟩ x + 1) ∧ (lo + leaves l ≤ x ∧ x < lo + leaves l + leaves r → toRoot ⟨lo, .node l r⟩ x = toRoot ⟨lo + leaves l, r⟩ x + 1) := ⟨merge_left l r lo x, merge_right l r lo x⟩25.4.4 From one forest to the next
When the search moves from leaf to leaf , the forest of must become the forest of . Below the lowest common ancestor of and , the path to has to be put back and the path to removed. The library describes this as a sequence of elementary events, each of which merges two sibling components into their parent or splits a parent into its two children.
inductive Event where | split (first : Nat) (left right : Tree) | merge (first : Nat) (left right : Tree) deriving DecidableEq, Reprdef Event.inverse : Event → Event | .split lo l r => .merge lo l r | .merge lo l r => .split lo l rdef Event.parent : Event → Component | .split lo l r | .merge lo l r => ⟨lo, .node l r⟩An event split first left right replaces the component by its two children, and merge first left right does the opposite. parent is the larger of the two sides, and inverse swaps splits and merges.
/-- Number of component roots introduced by a local rewrite. Removedroots may carry credit, so this bounds the increase in virgin-root count. -/def Event.introduced : Event → Nat | .split _ _ _ => 2 | .merge _ _ _ => 1def Event.isMerge : Event → Bool | .split _ _ _ => false | .merge _ _ _ => true/-- Count merge events for the reserved item-potential budget. -/def merges (events : List Event) : Nat := events.countP Event.isMerge/-- Total root introductions along an event trace, counting repeated births. -/def introductions (events : List Event) : Nat := (events.map Event.introduced).sumTwo counts serve the potential argument of Section 25.6. A split creates two new roots and a merge one: this is introduced, and introductions sums it along a list of events. merges counts the merges. New roots matter because a newly created virgin component carries a fixed credit (Section 25.5). Merges matter because a merge may raise the potential of the items in the merged component.
The events act on lists of components:
/-- A local forest rewrite. Reference-tree membership is certified separatelyby `Admissible`; the rewrite alone only checks shapes and offsets. -/inductive Step : List Component → Event → List Component → Prop | split (before after : List Component) (lo : Nat) (l r : Tree) : Step (before ++ [⟨lo, .node l r⟩] ++ after) (.split lo l r) (before ++ [⟨lo, l⟩, ⟨lo + leaves l, r⟩] ++ after) | merge (before after : List Component) (lo : Nat) (l r : Tree) : Step (before ++ [⟨lo, l⟩, ⟨lo + leaves l, r⟩] ++ after) (.merge lo l r) (before ++ [⟨lo, .node l r⟩] ++ after)inductive Trace : List Component → List Event → List Component → Prop | nil (roots : List Component) : Trace roots [] roots | cons {a b c : List Component} {event : Event} {events : List Event} : Step a event b → Trace b events c → Trace a (event :: events) cStep roots event next says that next is roots with one parent replaced by its two adjacent children (a split), or two adjacent children replaced by their parent (a merge). The rest of the list, before and after, is untouched. Trace chains steps. A step checks only shapes and offsets. Two adjacent components and can always be merged into as far as Step is concerned, even though this subtree does not exist in the example tree above. Membership in the reference tree is a separate condition:
/-- The event's parent must occur in the fixed reference tree. Merely havingadjacent intervals does not make two components siblings in that tree. -/def Admissible (t : Tree) (lo : Nat) (event : Event) : Prop := ∃ k, Distance.Occurs t lo event.parent k/-- An ordered partition of the reference leaves by actual subtree roots. -/structure Cut (t : Tree) (lo : Nat) (roots : List Component) : Prop where ordered : roots.Pairwise (fun a b => a.last ≤ b.first) occurs : ∀ c ∈ roots, ∃ k, Distance.Occurs t lo c k covers : ∀ x, lo ≤ x ∧ x < lo + leaves t → ∃ c ∈ roots, c.Contains xAn event is Admissible when its parent occurs in the reference tree. A Cut is the general shape of a forest: a left-to-right list of actual subtrees of the reference tree whose intervals cover every leaf. Its three fields are ordered, occurs and covers. Every search forest is a cut, and so is every intermediate list during a transition.
The transition itself is computed by three functions:
/-- Split the root and continue only along the searched leaf's path. -/def descend : Tree → Nat → Nat → List Event | .nil, _, _ => [] | .node l r, lo, searched => .split lo l r :: if searched < lo + leaves l then descend l lo searched else descend r (lo + leaves l) searcheddef ascend (t : Tree) (lo searched : Nat) : List Event := (descend t lo searched).reverse.map Event.inverse/-- Common ancestors need no changes. At the LCA, first merge toward itsroot, then split along the new search path. -/def plan : Tree → Nat → Nat → Nat → List Event | .nil, _, _, _ => [] | .node l r, lo, old, new => if old < lo + leaves l ∧ new < lo + leaves l then plan l lo old new else if lo + leaves l ≤ old ∧ lo + leaves l ≤ new then plan r (lo + leaves l) old new else ascend (.node l r) lo old ++ descend (.node l r) lo newdescend t lo searched splits the root, then the child on the searched leaf’s side, and so on down to the leaf: it turns the one-component list into the forest of searched. ascend is its reverse, made of merges. plan t lo old new descends through the common ancestors of old and new, which stay removed in both forests and need no change. At their lowest common ancestor it ascends from old up to , merging ’s subtree back together, and then descends from to new. In the example tree, plan t 0 3 0 has five events: three merges up to the root, then two splits down to leaf .
/-- Number of reference edges on the leaf-to-leaf path. The paper'snode-counted distance is one larger, even for repeated searches. -/def edges : Tree → Nat → Nat → Nat → Nat | .nil, _, _, _ => 0 | .node l r, lo, old, new => if old < lo + leaves l ∧ new < lo + leaves l then edges l lo old new else if lo + leaves l ≤ old ∧ lo + leaves l ≤ new then edges r (lo + leaves l) old new else Distance.depth (.node l r) lo old + Distance.depth (.node l r) lo newedges t lo old new is the number of edges on the path between two leaves: the sum of their depths below the lowest common ancestor. It is for a repeated search. This is the reference tree’s lazy-finger distance, and the benchmark of Theorem 3.1 adds it up over consecutive requests (Amortized.referenceCost, Section 25.6). The paper counts the nodes on the path instead, which is one more, even for a repeated search; the library charges that unit separately, as one per request.
Theorem (descending and ascending). Descending to a leaf is a trace from the whole tree to the leaf’s forest, with as many events as the leaf’s depth, all admissible. Ascending is the reverse trace, with the same length.
/-- Splitting down one leaf path constructs its reference forest. -/theorem descending (t : Tree) (lo searched : Nat) : Trace [⟨lo, t⟩] (descend t lo searched) (forest t lo searched) ∧ (descend t lo searched).length = Distance.depth t lo searched ∧ ∀ event ∈ descend t lo searched, Admissible t lo event := ⟨descend_trace t lo searched, descend_length t lo searched, descend_admissible t lo searched⟩theorem ascending (t : Tree) (lo searched : Nat) : Trace (forest t lo searched) (ascend t lo searched) [⟨lo, t⟩] ∧ (ascend t lo searched).length = Distance.depth t lo searched ∧ ∀ event ∈ ascend t lo searched, Admissible t lo event := ⟨ascend_trace t lo searched, ascend_length t lo searched, ascend_admissible t lo searched⟩Theorem (transition). For any two leaves old and new, the plan is a trace from the forest of old to the forest of new, its length is the number of reference edges between the two leaves, and every event in it is admissible.
/-- Merge to the LCA, then split toward the new search. Common ancestorsabove the LCA are untouched. Every event belongs to the original tree. -/theorem transition (t : Tree) (lo old new : Nat) : Trace (forest t lo old) (plan t lo old new) (forest t lo new) ∧ (plan t lo old new).length = edges t lo old new ∧ ∀ event ∈ plan t lo old new, Admissible t lo event := ⟨plan_trace t lo old new, plan_length t lo old new, plan_admissible t lo old new⟩This is the central fact of the section: one event per edge of the lazy finger’s path. The same length in node counts:
Theorem (path length). For valid leaves and , the plan’s length plus is the sum of the node counts from and from up to their lowest common ancestor.
/-- Exact edge accounting, expressed using the node-counted upward paths. -/theorem path (t : Tree) (lo x y : Nat) (validX : lo ≤ x ∧ x < lo + leaves t) (validY : lo ≤ y ∧ y < lo + leaves t) : (plan t lo x y).length + 2 = Distance.toRoot (Distance.lca t lo x y) x + Distance.toRoot (Distance.lca t lo x y) y := path_length t lo x y validX validYTheorem (repeated search). A repeated search needs no events.
theorem repeated (t : Tree) (lo x : Nat) : plan t lo x x = [] := plan_repeated t lo xThe potential argument looks at the forest after every single event, not only at the two ends. The next theorems say that each intermediate list is again a partition of the leaves by actual subtrees.
Theorem (preservation). An admissible trace turns a cut into a cut.
Theorem (intermediate cuts). Split the plan from old to new anywhere into a completed part and a remaining part. There is a cut that the completed part reaches from the forest of old, and from which the remaining part reaches the forest of new.
Theorem (ownership). In a cut, every leaf lies in exactly one component.
theorem preservation {t : Tree} {lo : Nat} {a b : List Component} {events : List Event} (trace : Trace a events b) (admissible : ∀ e ∈ events, Admissible t lo e) (cut : Cut t lo a) : Cut t lo b := trace.cut admissible cut/-- Every event boundary has an actual intermediate partition and tracesrealizing both the completed and remaining portions of the transition. -/theorem intermediate (t : Tree) (lo old new : Nat) (initial remaining : List Event) (events : plan t lo old new = initial ++ remaining) : ∃ roots, Trace (forest t lo old) initial roots ∧ Trace roots remaining (forest t lo new) ∧ Cut t lo roots := plan_prefix t lo old new initial remaining eventstheorem ownership {t : Tree} {lo : Nat} {roots : List Component} (cut : Cut t lo roots) {x : Nat} (valid : lo ≤ x ∧ x < lo + leaves t) : ∃! c, c ∈ roots ∧ c.Contains x := cut.partition validEach event moves the root of a leaf’s component by at most one level, which is what makes the change in potential per event bounded:
Theorem (local distance). Let a single step go from an ordered list roots to next, and let roots and next both contain the leaf . After a split, ’s node count to its root is the same or one smaller. After a merge, it is the same or one larger.
/-- Splits weakly decrease root distance; merges weakly increase it.In either direction, the change is at most one. -/theorem local_distance {roots next : List Component} {event : Event} (step : Step roots event next) (ordered : roots.Pairwise (fun c d => c.last ≤ d.first)) {a b : Component} {x : Nat} (ha : a ∈ roots) (hb : b ∈ next) (ca : a.Contains x) (cb : b.Contains x) : match event with | .split _ _ _ => Distance.toRoot b x ≤ Distance.toRoot a x ∧ Distance.toRoot a x ≤ Distance.toRoot b x + 1 | .merge _ _ _ => Distance.toRoot a x ≤ Distance.toRoot b x ∧ Distance.toRoot b x ≤ Distance.toRoot a x + 1 := step.distance ordered ha hb ca cbTheorem (distance along a trace). Along an admissible trace starting from a cut, the node count from a valid leaf to the root of its component changes by at most the number of events.
theorem sequence_distance {t : Tree} {lo x : Nat} {roots next : List Component} {events : List Event} (trace : Trace roots events next) (admissible : ∀ e ∈ events, Admissible t lo e) (cut : Cut t lo roots) (valid : lo ≤ x ∧ x < lo + leaves t) {a b : Component} (ha : a ∈ roots) (hb : b ∈ next) (ca : a.Contains x) (cb : b.Contains x) : Distance.toRoot b x ≤ Distance.toRoot a x + events.length ∧ Distance.toRoot a x ≤ Distance.toRoot b x + events.length := trace.distance_bound admissible cut valid ha hb ca cbFinally, the counts of new roots and merges:
Theorem (new roots). A single step introduces at most event.introduced components that were not there before. Along a trace, the components of the final list that were not in the initial list are at most introductions events.
/-- A split introduces at most two roots, and a merge at most one. -/theorem local_roots {roots next : List Component} {event : Event} (step : Step roots event next) : (next.toFinset \ roots.toFinset).card ≤ event.introduced := step.introductions/-- Endpoint roots absent initially are paid for by births along the trace. -/theorem new_roots {roots next : List Component} {events : List Event} (trace : Trace roots events next) : (next.toFinset \ roots.toFinset).card ≤ introductions events := trace.introductionsTheorem (budget). For any list of events, root introductions plus merges equal exactly twice the number of events.
/-- Exact sharing of the per-event budget between root births and merges. -/theorem birth_budget (events : List Event) : introductions events + merges events = 2 * events.length := introductions_merges eventsA split contributes and a merge . This identity is where the constant of Theorem 3.1 comes from: Section 25.6 pays units for each new virgin root and at most units of item potential for each merge, so a transition with events costs at most , and is the number of reference edges between consecutive searches.
25.4.5 Components that persist
Suppose a component belongs to the forests of a whole stretch of consecutive searches. Then no search in the stretch lands inside , except possibly at its own singleton leaf, because a search inside would remove ’s root. So the searches stay on one side of . This section proves that, and then draws the geometric consequence for GreedyASS: a key accessed in blocks the sweep, so that no point of the stretch is added beyond it on the far side. Section 25.5 uses this to show that each GreedyASS row touches at most two non-virgin components, those that were already accessed while they were roots (the paper’s Lemma 3.7).
Theorem (one side). If a component belongs to the forests of two valid searches and , and is neither searched singleton, then and are both to its left or both to its right.
/-- Persistent components other than the searched singletons see bothsearches on the same side of their interval. -/theorem side (t : Tree) (lo x y : Nat) {c : Component} (vx : lo ≤ x ∧ x < lo + leaves t) (vy : lo ≤ y ∧ y < lo + leaves t) (mx : c ∈ forest t lo x) (my : c ∈ forest t lo y) (nx : c ≠ ⟨x, .nil⟩) (ny : c ≠ ⟨y, .nil⟩) : (x < c.first ∧ y < c.first) ∨ (c.last ≤ x ∧ c.last ≤ y) := persistent_side t lo x y vx vy mx my nx nyTheorem (one side for a stream). The same holds for a whole nonempty sequence of searches: if belongs to all their forests and is none of their singletons, then all the searches lie to the left of , or all lie to its right.
theorem stream (t : Tree) (lo first : Nat) (xs : List Nat) {c : Component} (valid : ∀ x ∈ first :: xs, lo ≤ x ∧ x < lo + leaves t) (member : ∀ x ∈ first :: xs, c ∈ forest t lo x) (other : ∀ x ∈ first :: xs, c ≠ ⟨x, .nil⟩) : (∀ x ∈ first :: xs, x < c.first) ∨ (∀ x ∈ first :: xs, c.last ≤ x) := stream_side t lo first xs valid member otherWithout the singleton exclusion the statement would be false: in the example tree, the singleton belongs to the forests of both and , but the search for is inside it. The weak form below covers this case, relative to a single key of the component:
Theorem (weak side). If belongs to the forests of all searches in a sequence and contains the leaf , then all searches are , or all are .
theorem point_side (t : Tree) (lo : Nat) (xs : List Nat) {c : Component} {key : Nat} (within : c.Contains key) (valid : ∀ x ∈ xs, lo ≤ x ∧ x < lo + leaves t) (member : ∀ x ∈ xs, c ∈ forest t lo x) : (∀ x ∈ xs, x ≤ key) ∨ (∀ x ∈ xs, key ≤ x) := Reference.point_side t lo xs within valid memberThe geometric consequence uses the sweep Geometric.greedySweep of Section 25.1 and the run Geometric.fill of Chapter 9, which adds its rows one time step at a time. Suppose that at time stamp the point was added, with in , and that then persists. The searches stay on one side of , say the left. Intuitively, the old point acts as a wall: a later point to its right would have to be visible from a request to its left, across the column of , and the latest point of that column blocks every such rectangle.
Theorem (access barrier). Let be a point history with all times at most stamp and containing , and let contain accessed. Let first :: xs be a sequence of valid searches whose forests all contain , none of them at ’s singleton. Then either all searches lie left of and every point that GreedyASS adds on xs after time stamp has key at most accessed, or all lie right of and every such point has key at least accessed.
/-- Connect reference-forest persistence to actual GreedyASS accesses afteran earlier component access. The searched-singleton exception stays explicit. -/theorem access (t : Tree) (lo first : Nat) (xs : List Nat) {c : Component} {n stamp accessed : Nat} {P : Finset Point} (history : History n (stamp + 1) P) (point : (accessed, stamp) ∈ P) (within : c.Contains accessed) (valid : ∀ x ∈ first :: xs, lo ≤ x ∧ x < lo + leaves t) (member : ∀ x ∈ first :: xs, c ∈ forest t lo x) (other : ∀ x ∈ first :: xs, c ≠ ⟨x, .nil⟩) : ((∀ x ∈ first :: xs, x < c.first) ∧ ∀ p ∈ fill greedySweep (stamp + 1) P xs, stamp < p.2 → p.1 ≤ accessed) ∨ ((∀ x ∈ first :: xs, c.last ≤ x) ∧ ∀ p ∈ fill greedySweep (stamp + 1) P xs, stamp < p.2 → accessed ≤ p.1) := access_barrier t lo first xs history point within valid member otherIn the intended use, first is the search of time stamp, whose row contained accessed, and xs are the following searches. The sweep runs on xs from the history P, starting at time stamp + 1. The hypothesis on is only Geometric.History, so the theorem does not assume that was produced by GreedyASS, or even that it is arborally satisfied.
Theorem (weak barrier). The same with the weak sides of point_side: if all forests of xs contain , then either all searches and all new points are at most accessed, or all are at least accessed. Searches of ’s singleton are allowed.
/-- The point-side barrier includes visits to a searched singleton. -/theorem barrier (t : Tree) (lo : Nat) (xs : List Nat) {c : Component} {n stamp accessed : Nat} {P : Finset Point} (history : History n (stamp + 1) P) (point : (accessed, stamp) ∈ P) (within : c.Contains accessed) (valid : ∀ x ∈ xs, lo ≤ x ∧ x < lo + leaves t) (member : ∀ x ∈ xs, c ∈ forest t lo x) : ((∀ x ∈ xs, x ≤ accessed) ∧ ∀ p ∈ fill greedySweep (stamp + 1) P xs, stamp < p.2 → p.1 ≤ accessed) ∨ ((∀ x ∈ xs, accessed ≤ x) ∧ ∀ p ∈ fill greedySweep (stamp + 1) P xs, stamp < p.2 → accessed ≤ p.1) := persistent_barrier t lo xs history point within valid member25.4.6 From an ordinary BST to a reference tree
Theorem 3.1 is stated for leaf-oriented reference trees, while the lazy-finger cost of Chapter 15 is defined for ordinary BSTs, whose internal nodes carry the keys. The paper bridges the two with a simple conversion that keeps each key’s position in the inorder and at most doubles depths.
/-- Replace each ordinary BST key by a leaf, retaining inorder ranks andcontracting missing children. The empty input is totalized to one leaf;population and performance theorems require a nonempty input. -/def convert : Tree → Tree | .nil => .nil | .node l r => if l.size = 0 then if r.size = 0 then .nil else .node .nil (convert r) else if r.size = 0 then .node (convert l) .nil else .node (convert l) (.node .nil (convert r))Each node of the ordinary tree, with key , becomes a small gadget whose leaves are the leaves of the converted left subtree, then the leaf , then the leaves of the converted right subtree:
- with two children,
node (convert l) (node nil (convert r)): an internal node for the left part, and below it on the right a second internal node separating the leaf from the right part; - with only one child, a single internal node separating from that child;
- with no children, the leaf alone.
Missing children are contracted rather than turned into leaves, so the converted tree has exactly one leaf per key, in inorder: leaf rank is key rank . Descending from a node to its left child costs one reference edge, and to its right child two. The empty tree has no keys but is sent to nil, which has one leaf. This is a totalized value, and the theorems that need a key count assume a nonempty tree.
Theorem (population). A nonempty tree with keys converts to a reference tree with leaves.
theorem population (t : Tree) (nonempty : 0 < t.size) : leaves (convert t) = t.size := Conversion.population t nonemptyModels.Rotation, Bounds.StaticTree.Definitions, Bounds.LazyFinger.Definitions, Algorithms.GreedyASS.Reference.Definitions, Algorithms.GreedyASS.Reference.Distance.Definitions, Algorithms.GreedyASS.Reference.Transitions.Definitions, Algorithms.GreedyASS.Reference.Conversion.Definitions, Algorithms.GreedyASS.Potential.Amortized.DefinitionsTheorem (depth). The leaf of key has edge depth at most in the converted tree, where is the depth of in the original tree, with the root at depth .
theorem depth (t : Tree) (lo x : Nat) (valid : x < t.size) : Distance.depth (convert t) lo (lo + x) ≤ 2 * Tree.depth t x + 2 := Conversion.depth t lo x validModels.Rotation, Bounds.StaticTree.Definitions, Bounds.LazyFinger.Definitions, Algorithms.GreedyASS.Reference.Definitions, Algorithms.GreedyASS.Reference.Distance.Definitions, Algorithms.GreedyASS.Reference.Transitions.Definitions, Algorithms.GreedyASS.Reference.Conversion.Definitions, Algorithms.GreedyASS.Potential.Amortized.DefinitionsTheorem (distance). For keys of , the number of reference edges between their leaves is at most , where is the edge distance LazyFinger.distance in .
theorem distance (t : Tree) (lo x y : Nat) (hx : x < t.size) (hy : y < t.size) : Transitions.edges (convert t) lo (lo + x) (lo + y) ≤ 2 * LazyFinger.distance t x y + 4 := Conversion.distance t lo x y hx hyModels.Rotation, Bounds.StaticTree.Definitions, Bounds.LazyFinger.Definitions, Algorithms.GreedyASS.Reference.Definitions, Algorithms.GreedyASS.Reference.Distance.Definitions, Algorithms.GreedyASS.Reference.Transitions.Definitions, Algorithms.GreedyASS.Reference.Conversion.Definitions, Algorithms.GreedyASS.Potential.Amortized.DefinitionsSumming over a request sequence gives the comparison of the two benchmarks. Recall that the leaf-oriented benchmark Amortized.referenceCost charges one unit per request plus the reference edges between consecutive requests, with no initial traversal. The lazy-finger cost LazyFinger.costFrom t p xs charges one unit per request plus the distance in from the previous request, starting from the key , and LazyFinger.cost starts at the root.
Theorem (benchmark comparison). For a BST , a valid start key and valid requests ,
and the same holds for the root-start cost when is nonempty.
theorem cost_from (t : Tree) (p : Nat) (xs : List Nat) (hp : p < t.size) (valid : ∀ x ∈ xs, x < t.size) : referenceCost (convert t) 0 xs ≤ 5 * LazyFinger.costFrom t p xs := Conversion.cost_from t p xs hp validModels.Rotation, Bounds.StaticTree.Definitions, Bounds.LazyFinger.Definitions, Algorithms.GreedyASS.Reference.Definitions, Algorithms.GreedyASS.Reference.Distance.Definitions, Algorithms.GreedyASS.Reference.Transitions.Definitions, Algorithms.GreedyASS.Reference.Conversion.Definitions, Algorithms.GreedyASS.Potential.Amortized.Definitionstheorem cost (t : Tree) (xs : List Nat) (nonempty : 0 < t.size) (valid : ∀ x ∈ xs, x < t.size) : referenceCost (convert t) 0 xs ≤ 5 * LazyFinger.cost t xs := Conversion.cost t xs nonempty validModels.Rotation, Bounds.StaticTree.Definitions, Bounds.LazyFinger.Definitions, Algorithms.GreedyASS.Reference.Definitions, Algorithms.GreedyASS.Reference.Distance.Definitions, Algorithms.GreedyASS.Reference.Transitions.Definitions, Algorithms.GreedyASS.Reference.Conversion.Definitions, Algorithms.GreedyASS.Potential.Amortized.DefinitionsEach request contributes on the left, with reference edges, and on the right, so the ratio is at most . The first request contributes on the left and at least on the right. So the bound holds for every valid start key : the leaf-oriented benchmark does not charge the walk to the first request at all.
- Offsets. The conversion statements use offset
0for the reference cost and shift ranks byloindepthanddistance, so that the converted tree can be placed anywhere. - The empty tree.
populationandcostassume0 < t.size. Incost_fromthe hypothesisp < t.sizealready forces the tree to be nonempty.