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

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 xx, delete the path from the root down to xx. What is left is a forest of subtrees hanging off that path, together with xx 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 xx to yy, the forest changes by a sequence of local merges and splits along the path from xx to yy, 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.

Do you need this section?

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.

defGeometric.GreedyASS.Reference.leavesno axiomsAlgorithms/GreedyASS/Reference/Definitions/Abstract.lean:8
/-- 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 r

leaves t is the number of leaves. Under this reading a tree with kk internal nodes has k+1k + 1 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 Component is a pair: the rank first of its first leaf and its shape. It covers the half-open interval of leaves [first,last)[\mathit{first}, \mathit{last}), 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.

Containment is two comparisons, so it is decidable.

defGeometric.GreedyASS.Reference.forestno axiomsAlgorithms/GreedyASS/Reference/Definitions/Abstract.lean:29
/-- 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) searched

forest 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 d+1d + 1 components, where dd is the depth of the searched leaf: one per edge of the search path, plus the singleton.

For example, take the reference tree t=node(L,R)t = \mathrm{node}(L, R) with L=node(nil,nil)L = \mathrm{node}(\mathrm{nil}, \mathrm{nil}) and R=node(nil,node(nil,nil))R = \mathrm{node}(\mathrm{nil}, \mathrm{node}(\mathrm{nil}, \mathrm{nil})). Its leaves are 0,…,40, \dots, 4, with LL holding 0,10, 1 and RR holding 2,3,42, 3, 4. The search for leaf 33 goes right, right, left, and

forest t 0 3=[⟨0,L⟩, ⟨2,nil⟩, ⟨3,nil⟩, ⟨4,nil⟩].\mathtt{forest}\ t\ 0\ 3 = [\langle 0, L\rangle,\ \langle 2, \mathrm{nil}\rangle,\ \langle 3, \mathrm{nil}\rangle,\ \langle 4, \mathrm{nil}\rangle].

The search for leaf 44 removes the same three internal nodes and gives the same forest. Only the searched singleton differs, ⟨4,nil⟩\langle 4, \mathrm{nil}\rangle instead of ⟨3,nil⟩\langle 3, \mathrm{nil}\rangle. 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.

theoremGeometric.GreedyASS.Reference.Results.layoutpropext · Classical.choice · Quot.soundAlgorithms/GreedyASS/Reference/Abstract.lean:9
/-- 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 lo≤first<last≤lo+leaves t\mathit{lo} \le \mathit{first} < \mathit{last} \le \mathit{lo} + \mathtt{leaves}\ t. These hold even for an invalid searched.

Theorem (partition). Every leaf of the tree lies in exactly one component of the forest.

theoremGeometric.GreedyASS.Reference.Results.partitionpropext · Classical.choice · Quot.soundAlgorithms/GreedyASS/Reference/Abstract.lean:19
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 valid

Theorem (the searched singleton). After a search for a valid leaf xx, the singleton ⟨x,nil⟩\langle x, \mathrm{nil}\rangle belongs to the forest and is the only component that contains xx.

theoremGeometric.GreedyASS.Reference.Results.searchedpropext · Classical.choice · Quot.soundAlgorithms/GreedyASS/Reference/Abstract.lean:23
/-- 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.

defGeometric.GreedyASS.Reference.Distance.depthno axiomsAlgorithms/GreedyASS/Reference/Distance/Definitions/Abstract.lean:8
/-- 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
defGeometric.GreedyASS.Reference.Distance.lcaDepthno axiomsAlgorithms/GreedyASS/Reference/Distance/Definitions/Abstract.lean:15
/-- 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
defGeometric.GreedyASS.Reference.Distance.lcano axiomsAlgorithms/GreedyASS/Reference/Distance/Definitions/Abstract.lean:24
/-- 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 xx: the number of internal nodes above it. lcaDepth t lo a b is the edge depth of the lowest common ancestor of the leaves aa and bb, 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 a=ba = b the lowest common ancestor is the leaf itself.

Positions in the tree are described by an inductive predicate:

inductiveGeometric.GreedyASS.Reference.Distance.Occursno axiomsAlgorithms/GreedyASS/Reference/Distance/Definitions/Abstract.lean:33
/-- 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 00, 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:

defGeometric.GreedyASS.Reference.Distance.toLCAno axiomsAlgorithms/GreedyASS/Reference/Distance/Definitions/Abstract.lean:42
/-- 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
defGeometric.GreedyASS.Reference.Distance.toRootno axiomsAlgorithms/GreedyASS/Reference/Distance/Definitions/Abstract.lean:46
/-- Node-counted distance from a contained leaf to the component root. -/def toRoot (c : Component) (x : Nat) : Nat := depth c.shape c.first x + 1
  • toLCA t lo x a b = depth x - lcaDepth a b + 1 is, when a≤x≤ba \le x \le b, the number of nodes on the path from the leaf xx up to the lowest common ancestor of aa and bb, both ends included. The hypothesis a≤x≤ba \le x \le b ensures that this ancestor is an ancestor of xx. Outside it the formula is a totalized natural-number subtraction.
  • toRoot c x = depth c.shape c.first x + 1 is the number of nodes on the path from the leaf xx up to the root of the component c, both ends included. For a singleton it is 11.

Theorem (lowest common ancestor). For valid leaves aa and bb, 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.

theoremGeometric.GreedyASS.Reference.Distance.Results.lowest_common_ancestorpropext · Classical.choice · Quot.soundAlgorithms/GreedyASS/Reference/Distance/Abstract.lean:9
/-- 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 a≤x≤ba \le x \le b are valid leaves, then toLCA x a b is the node count from xx to the root of the subtree lca a b.

theoremGeometric.GreedyASS.Reference.Distance.Results.pathpropext · Classical.choice · Quot.soundAlgorithms/GreedyASS/Reference/Distance/Abstract.lean:19
/-- 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 validB

So the arithmetic formula of toLCA measures what it should. The next two theorems say that widening the interval [a,b][a, b] 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 w≤x≤y≤zw \le x \le y \le z, the distance from xx to the lowest common ancestor of ww and yy is at most that to the lowest common ancestor of ww and zz, and the distance from yy to the lowest common ancestor of xx and zz is at most that to the lowest common ancestor of ww and zz.

theoremGeometric.GreedyASS.Reference.Distance.Results.orderedpropext · Classical.choice · Quot.soundAlgorithms/GreedyASS/Reference/Distance/Abstract.lean:24
/-- 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 yz

Theorem (enclosing intervals). If [b,c]⊆[a,d][b, c] \subseteq [a, d], then toLCA x b c ≤ toLCA x a d for every xx.

theoremGeometric.GreedyASS.Reference.Distance.Results.intervalpropext · Classical.choice · Quot.soundAlgorithms/GreedyASS/Reference/Distance/Abstract.lean:30
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 cd

Neither statement needs valid ranks: the inequalities hold for the totalized formula. They are meaningful as path lengths only for valid leaves with xx between the endpoints.

Distances can be computed inside any subtree that contains the leaves in question:

Theorem (subtrees). If the component cc occurs at depth kk and contains xx, aa and bb, then the depth of xx is kk plus its depth in cc, and toLCA x a b is the same whether computed in the whole tree or in cc.

theoremGeometric.GreedyASS.Reference.Distance.Results.subtreepropext · Classical.choice · Quot.soundAlgorithms/GreedyASS/Reference/Distance/Abstract.lean:33
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.

The next theorem is the bridge between the two kinds of distance, and the reason why they are compared at all. Take a forest component cc that contains xx, and leaves a≤x≤ba \le x \le b. Going up from xx, the path reaches the lowest common ancestor of aa and bb no later than the root of cc exactly when that ancestor is inside cc, that is, exactly when cc contains aa and bb.

Theorem (component comparison). Let cc be a component of the forest of some search and let x∈cx \in c. For valid leaves a≤x≤ba \le x \le b, toLCA x a b ≤ toRoot c x holds if and only if cc contains both aa and bb, and the strict reverse inequality holds if and only if cc misses one of them.

theoremGeometric.GreedyASS.Reference.Distance.Results.componentpropext · Classical.choice · Quot.soundAlgorithms/GreedyASS/Reference/Distance/Abstract.lean:44
/-- 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, aa and bb 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.

theoremGeometric.GreedyASS.Reference.Distance.Results.mergepropextAlgorithms/GreedyASS/Reference/Distance/Abstract.lean:55
/-- 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 xx to leaf yy, the forest of xx must become the forest of yy. Below the lowest common ancestor of xx and yy, the path to xx has to be put back and the path to yy 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.

An event split first left right replaces the component ⟨first,node(left,right)⟩\langle \mathit{first}, \mathrm{node}(\mathit{left}, \mathit{right})\rangle 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.

defGeometric.GreedyASS.Reference.Transitions.Event.introducedno axiomsAlgorithms/GreedyASS/Reference/Transitions/Definitions/Abstract.lean:20
/-- 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 _ _ _ => 1
defGeometric.GreedyASS.Reference.Transitions.Event.isMergeno axiomsAlgorithms/GreedyASS/Reference/Transitions/Definitions/Abstract.lean:26
def Event.isMerge : Event → Bool  | .split _ _ _ => false  | .merge _ _ _ => true
defGeometric.GreedyASS.Reference.Transitions.mergesno axiomsAlgorithms/GreedyASS/Reference/Transitions/Definitions/Abstract.lean:30
/-- Count merge events for the reserved item-potential budget. -/def merges (events : List Event) : Nat := events.countP Event.isMerge
defGeometric.GreedyASS.Reference.Transitions.introductionsno axiomsAlgorithms/GreedyASS/Reference/Transitions/Definitions/Abstract.lean:33
/-- Total root introductions along an event trace, counting repeated births. -/def introductions (events : List Event) : Nat := (events.map Event.introduced).sum

Two 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:

inductiveGeometric.GreedyASS.Reference.Transitions.Stepno axiomsAlgorithms/GreedyASS/Reference/Transitions/Definitions/Abstract.lean:47
/-- 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)
inductiveGeometric.GreedyASS.Reference.Transitions.Traceno axiomsAlgorithms/GreedyASS/Reference/Transitions/Definitions/Abstract.lean:57
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) c

Step 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 ⟨0,L⟩\langle 0, L\rangle and ⟨2,nil⟩\langle 2, \mathrm{nil}\rangle can always be merged into ⟨0,node(L,nil)⟩\langle 0, \mathrm{node}(L, \mathrm{nil})\rangle 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:

defGeometric.GreedyASS.Reference.Transitions.Admissibleno axiomsAlgorithms/GreedyASS/Reference/Transitions/Definitions/Abstract.lean:36
/-- 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
structureGeometric.GreedyASS.Reference.Transitions.Cutno axiomsAlgorithms/GreedyASS/Reference/Transitions/Definitions/Abstract.lean:41
/-- 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 x

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

defGeometric.GreedyASS.Reference.Transitions.descendno axiomsAlgorithms/GreedyASS/Reference/Transitions/Definitions/Abstract.lean:62
/-- 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) searched
defGeometric.GreedyASS.Reference.Transitions.ascendno axiomsAlgorithms/GreedyASS/Reference/Transitions/Definitions/Abstract.lean:70
def ascend (t : Tree) (lo searched : Nat) : List Event :=  (descend t lo searched).reverse.map Event.inverse
defGeometric.GreedyASS.Reference.Transitions.planno axiomsAlgorithms/GreedyASS/Reference/Transitions/Definitions/Abstract.lean:73
/-- 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 new

descend 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 [⟨lo,t⟩][\langle \mathit{lo}, t\rangle] 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 vv it ascends from old up to vv, merging vv’s subtree back together, and then descends from vv 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 00.

defGeometric.GreedyASS.Reference.Transitions.edgesno axiomsAlgorithms/GreedyASS/Reference/Transitions/Definitions/Abstract.lean:82
/-- 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 new

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

theoremGeometric.GreedyASS.Reference.Transitions.Results.descendingpropext · Quot.soundAlgorithms/GreedyASS/Reference/Transitions/Abstract.lean:9
/-- 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⟩
theoremGeometric.GreedyASS.Reference.Transitions.Results.ascendingpropext · Quot.soundAlgorithms/GreedyASS/Reference/Transitions/Abstract.lean:16
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.

theoremGeometric.GreedyASS.Reference.Transitions.Results.transitionpropext · Quot.soundAlgorithms/GreedyASS/Reference/Transitions/Abstract.lean:22
/-- 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 xx and yy, the plan’s length plus 22 is the sum of the node counts from xx and from yy up to their lowest common ancestor.

theoremGeometric.GreedyASS.Reference.Transitions.Results.pathpropext · Classical.choice · Quot.soundAlgorithms/GreedyASS/Reference/Transitions/Abstract.lean:30
/-- 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 validY

Theorem (repeated search). A repeated search needs no events.

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

theoremGeometric.GreedyASS.Reference.Transitions.Results.preservationpropext · Classical.choice · Quot.soundAlgorithms/GreedyASS/Reference/Transitions/Abstract.lean:39
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
theoremGeometric.GreedyASS.Reference.Transitions.Results.intermediatepropext · Classical.choice · Quot.soundAlgorithms/GreedyASS/Reference/Transitions/Abstract.lean:43
/-- 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 events
theoremGeometric.GreedyASS.Reference.Transitions.Results.ownershippropext · Quot.soundAlgorithms/GreedyASS/Reference/Transitions/Abstract.lean:51
theorem 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 valid

Each 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 a∈a \in roots and b∈b \in next both contain the leaf xx. After a split, xx’s node count to its root is the same or one smaller. After a merge, it is the same or one larger.

theoremGeometric.GreedyASS.Reference.Transitions.Results.local_distancepropext · Classical.choice · Quot.soundAlgorithms/GreedyASS/Reference/Transitions/Abstract.lean:55
/-- 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 cb

Theorem (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.

theoremGeometric.GreedyASS.Reference.Transitions.Results.sequence_distancepropext · Classical.choice · Quot.soundAlgorithms/GreedyASS/Reference/Transitions/Abstract.lean:68
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 cb

Finally, 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.

theoremGeometric.GreedyASS.Reference.Transitions.Results.local_rootspropext · Classical.choice · Quot.soundAlgorithms/GreedyASS/Reference/Transitions/Abstract.lean:78
/-- 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
theoremGeometric.GreedyASS.Reference.Transitions.Results.new_rootspropext · Classical.choice · Quot.soundAlgorithms/GreedyASS/Reference/Transitions/Abstract.lean:82
/-- 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.introductions

Theorem (budget). For any list of events, root introductions plus merges equal exactly twice the number of events.

A split contributes 2+02 + 0 and a merge 1+11 + 1. This identity is where the constant 2424 of Theorem 3.1 comes from: Section 25.6 pays 1212 units for each new virgin root and at most 1212 units of item potential for each merge, so a transition with ee events costs at most 12⋅2e=24e12 \cdot 2e = 24e, and ee is the number of reference edges between consecutive searches.

25.4.5 Components that persist

Suppose a component cc belongs to the forests of a whole stretch of consecutive searches. Then no search in the stretch lands inside cc, except possibly at its own singleton leaf, because a search inside cc would remove cc’s root. So the searches stay on one side of cc. This section proves that, and then draws the geometric consequence for GreedyASS: a key accessed in cc 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 cc belongs to the forests of two valid searches xx and yy, and is neither searched singleton, then xx and yy are both to its left or both to its right.

theoremGeometric.GreedyASS.Reference.Results.sidepropext · Classical.choice · Quot.soundAlgorithms/GreedyASS/Reference/Abstract.lean:29
/-- 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 ny

Theorem (one side for a stream). The same holds for a whole nonempty sequence of searches: if cc belongs to all their forests and is none of their singletons, then all the searches lie to the left of cc, or all lie to its right.

theoremGeometric.GreedyASS.Reference.Results.streampropext · Classical.choice · Quot.soundAlgorithms/GreedyASS/Reference/Abstract.lean:38
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 other

Without the singleton exclusion the statement would be false: in the example tree, the singleton ⟨3,nil⟩\langle 3, \mathrm{nil}\rangle belongs to the forests of both 33 and 44, but the search for 33 is inside it. The weak form below covers this case, relative to a single key of the component:

Theorem (weak side). If cc belongs to the forests of all searches in a sequence and contains the leaf kk, then all searches are ≤k\le k, or all are ≥k\ge k.

theoremGeometric.GreedyASS.Reference.Results.point_sidepropext · Classical.choice · Quot.soundAlgorithms/GreedyASS/Reference/Abstract.lean:60
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 member

The 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 (accessed,stamp)(\mathit{accessed}, \mathit{stamp}) was added, with accessed\mathit{accessed} in cc, and that cc then persists. The searches stay on one side of cc, 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 accessed\mathit{accessed}, and the latest point of that column blocks every such rectangle.

Theorem (access barrier). Let PP be a point history with all times at most stamp and containing (accessed,stamp)(\mathit{accessed}, \mathit{stamp}), and let cc contain accessed. Let first :: xs be a sequence of valid searches whose forests all contain cc, none of them at cc’s singleton. Then either all searches lie left of cc and every point that GreedyASS adds on xs after time stamp has key at most accessed, or all lie right of cc and every such point has key at least accessed.

theoremGeometric.GreedyASS.Reference.Results.accesspropext · Classical.choice · Quot.soundAlgorithms/GreedyASS/Reference/Abstract.lean:45
/-- 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 other

In 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 PP is only Geometric.History, so the theorem does not assume that PP 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 cc, then either all searches and all new points are at most accessed, or all are at least accessed. Searches of cc’s singleton are allowed.

theoremGeometric.GreedyASS.Reference.Results.barrierpropext · Classical.choice · Quot.soundAlgorithms/GreedyASS/Reference/Abstract.lean:66
/-- 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 member

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

defGeometric.GreedyASS.Reference.Conversion.convertno axiomsAlgorithms/GreedyASS/Reference/Conversion/Definitions/Abstract.lean:8
/-- 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 kk, becomes a small gadget whose leaves are the leaves of the converted left subtree, then the leaf kk, 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 kk from the right part;
  • with only one child, a single internal node separating kk from that child;
  • with no children, the leaf kk alone.

Missing children are contracted rather than turned into leaves, so the converted tree has exactly one leaf per key, in inorder: leaf rank xx is key rank xx. 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 nn keys converts to a reference tree with nn leaves.

Theorem (depth). The leaf of key xx has edge depth at most 2 d(x)+22\,d(x) + 2 in the converted tree, where d(x)d(x) is the depth of xx in the original tree, with the root at depth 00.

Theorem (distance). For keys x,yx, y of tt, the number of reference edges between their leaves is at most 2 δt(x,y)+42\,\delta_t(x, y) + 4, where δt\delta_t is the edge distance LazyFinger.distance in tt.

Summing 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 tt from the previous request, starting from the key pp, and LazyFinger.cost starts at the root.

Theorem (benchmark comparison). For a BST tt, a valid start key pp and valid requests XX,

referenceCost(convert t,0,X)≤5 LFt(p,X),\mathtt{referenceCost}(\mathtt{convert}\ t, 0, X) \le 5\,\mathrm{LF}_t(p, X),

and the same holds for the root-start cost LFt(X)\mathrm{LF}_t(X) when tt is nonempty.

Each request contributes 1+e1 + e on the left, with e≤2δ+4e \le 2\delta + 4 reference edges, and 1+δ1 + \delta on the right, so the ratio is at most 55. The first request contributes 11 on the left and at least 11 on the right. So the bound holds for every valid start key pp: the leaf-oriented benchmark does not charge the walk to the first request at all.

Reading the Lean
  • Offsets. The conversion statements use offset 0 for the reference cost and shift ranks by lo in depth and distance, so that the converted tree can be placed anywhere.
  • The empty tree. population and cost assume 0 < t.size. In cost_from the hypothesis p < t.size already forces the tree to be nonempty.