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

Chapter 25 · GreedyASS · Section 25.5Items, colors and potential

Iacono and Langerman (2016) prove their Theorem 3.1 with a potential function. GreedyASS runs on its own, and a lazy finger moves in a fixed leaf-oriented reference tree TT alongside it. The potential is chosen so that every point GreedyASS adds is paid for, up to a constant, by a drop in the potential or by the finger’s movement. This section defines that potential and proves its local properties. Section 25.6 does the bookkeeping, request by request, and concludes the theorem.

The potential has two parts, both read off a snapshot of the execution.

  • Item potentials. Every key is an item. Once GreedyASS has touched it, its potential depends on where its most recent access sits in the picture of the points so far, and on distances in TT. Each item gets one of four colors, red, green, blue or purple. The color selects one of four candidate values, each a node count along a path of TT.
  • Virgin potential. Each component of the reference forest (Section 25.4) that has not been touched since it became a root carries 1212 units.

The analysis of one request has two phases. In Phase I the finger moves from the previous request to the new one, and the reference forest changes by merges and splits. In Phase II GreedyASS adds its row. Most results below say what one of these phases can do to one part of the potential.

Do you need this section?

Not to check Theorem 3.1. Its statement, Results.cost, mentions only the GreedyASS sweep, its point count and the reference cost. Nothing defined here appears in it, so these definitions need not be trusted: whatever they say, Lean has checked the proof that uses them. Read this section to follow the proof, or to see how closely the formal potential follows the paper.

Two results here stand out as milestones of the proof. The first is a geometric barrier: once a key is accessed, as long as all requests are on one side of it, no access happens on the other side (Lemma 3.4 of the paper). The second is its consequence for the reference forest: each GreedyASS row touches at most two components that were already accessed earlier and have remained roots since (Lemma 3.7). This is why virgin potential pays for most of the touched components (Lemma 3.8).

Each GreedyASS row touches at most two old roots

Let TT be a leaf-oriented reference tree whose ℓ\ell leaves are the keys lo,…,lo+ℓ−1lo, \dots, lo + \ell - 1, and run GreedyASS from the empty point set on a sequence of requests from that range. Before the row for the current request rr is added, call a component of the reference forest of rr old if, at some earlier time, GreedyASS touched one of its keys while it was a root, and it has remained a root at every search since then, up to and including rr. Call the other components virgin. Then the row GreedyASS adds for rr touches at most two old components.

Give each virgin component 1212 units of potential. If the row touches kk components in all, at least k−2k - 2 of them are virgin, so the virgin potential drops from VV to some V′V' with

V′+12 k  ≤  V+24.V' + 12\,k \;\le\; V + 24 .

These are the paper’s Lemmas 3.7 and 3.8. The second is stated additively, so that it is meaningful when k<2k < 2.

The Lean statements
theoremGeometric.GreedyASS.Virgin.Results.countpropext · Classical.choice · Quot.soundAlgorithms/GreedyASS/Virgin/Abstract.lean:29
/-- Lemma 3.7, including searched singleton components. -/theorem count (t : Tree) (lo : Nat) (history : List Nat) (request : Nat)    (valid : ∀ x ∈ history ++ [request], lo ≤ x ∧ x < lo + leaves t) :    (oldTouched t lo history request).card ≤ 2 := … Virgin.count t lo history request valid
theoremGeometric.GreedyASS.Virgin.Results.decreasepropext · Classical.choice · Quot.soundAlgorithms/GreedyASS/Virgin/Abstract.lean:44
/-- Lemma 3.8, stated without truncated subtraction. -/theorem decrease (t : Tree) (lo : Nat) (history : List Nat) (request : Nat)    (valid : ∀ x ∈ history ++ [request], lo ≤ x ∧ x < lo + leaves t) :    after t lo history request + 12 * (touched t lo history request).card ≤      before t lo history request + 24 := … Virgin.decrease t lo history request valid
25 definitions in 5 chapters, 4 more in internal modules · reading list

The definitions are in Algorithms/GreedyASS/Items/Definitions/Abstract.lean, Barrier/Definitions/Abstract.lean, Virgin/Definitions/Abstract.lean and Potential/Definitions/Abstract.lean. The theorems are in the Abstract.lean file next to each.

25.5.1 Overview

Here is the whole construction in brief. The details follow in the same order.

Picture the GreedyASS points as in Chapter 9, keys left to right and time upward. For each touched key xx, its most recent access is the highest point in column xx. It lies on some row, the item’s access row. Its neighbors are the nearest keys to its left and to its right that were touched in that same row. The item is classified by what happened around it since that row:

  • it is external if it was the leftmost or rightmost point of its row;
  • otherwise it is glancing if every later point lies strictly on one side of its column;
  • otherwise it is internal: later points lie on both sides.

The external items are the points that are visible from far to the left or far to the right: their upper-left or upper-right quadrant is empty. They form two staircases.

The four candidate values compare the item with the reference tree TT. All distances count nodes, both endpoints included.

color value used for
red distance from xx to the lowest common ancestor of its two neighbors internal and glancing items
green one more than the distance from xx to the lowest common ancestor of the nearest external items on either side external items
blue one more than the distance from xx to the root of its component in the current reference forest external items
purple the largest blue value of xx since its most recent access glancing items

An internal item is red. A glancing item takes the smaller of red and purple, and an external item the smaller of green and blue. An external item touched by the latest row, a top corner, is always blue. The item’s potential is the value of its color. The total potential adds up the item potentials and 1212 for each virgin root.

25.5.2 Items in the geometric view

All definitions in this section take an arbitrary finite point set P, a snapshot, not necessarily produced by GreedyASS. Theorems add the hypotheses they need: that P is arborally satisfied (Geometric.ASS), or that it is a Geometric.History, with all points strictly before the time of the next row.

25.5.2.1 Most recent accesses

defGeometric.GreedyASS.Items.greatestpropext · Classical.choice · Quot.soundAlgorithms/GreedyASS/Items/Definitions/Abstract.lean:9
def greatest (s : Finset Nat) : Option Nat :=  if h : s.Nonempty then some (s.max' h) else none
defGeometric.GreedyASS.Items.leastpropext · Classical.choice · Quot.soundAlgorithms/GreedyASS/Items/Definitions/Abstract.lean:12
def least (s : Finset Nat) : Option Nat :=  if h : s.Nonempty then some (s.min' h) else none

greatest and least return the maximum and minimum of a finite set of naturals, or none for the empty set. They are written with Option so that a missing neighbor or an unseen key is a value, not a hypothesis.

defGeometric.GreedyASS.Items.timespropext · Classical.choice · Quot.soundAlgorithms/GreedyASS/Items/Definitions/Abstract.lean:15
/-- Access times in one column, without an artificial time for unseen keys. -/def times (P : Finset Point) (key : Nat) : Finset Nat :=  (P.filter fun p => p.1 = key).image Prod.snd
defGeometric.GreedyASS.Items.latestpropext · Classical.choice · Quot.soundAlgorithms/GreedyASS/Items/Definitions/Abstract.lean:19
/-- Time coordinate of the most recent access, or none for an unseen item. -/def latest (P : Finset Point) (key : Nat) : Option Nat := greatest (times P key)

times P key is the set of times at which column key has a point, and latest P key is the latest of them, the time of the most recent access. A key that was never touched has latest = none. There is no artificial access at time −1-1 or at the initial tree.

defGeometric.GreedyASS.Items.Activepropext · Classical.choice · Quot.soundAlgorithms/GreedyASS/Items/Definitions/Abstract.lean:29
def Active (P : Finset Point) (q : Point) : Prop :=  q ∈ P ∧ ∀ p ∈ P, p.1 = q.1 → p.2 ≤ q.2

A point q is active in P if it belongs to P and is the highest point of its column. The active points are exactly the most recent accesses, one per touched key:

25.5.2.2 Neighbors on the access row

defGeometric.GreedyASS.Items.leftNeighborpropext · Classical.choice · Quot.soundAlgorithms/GreedyASS/Items/Definitions/Abstract.lean:22
/-- Nearest same-row accesses on either side of an active point. -/def leftNeighbor (P : Finset Point) (q : Point) : Option Nat :=  greatest ((keysAt P q.2).filter fun x => x < q.1)
defGeometric.GreedyASS.Items.rightNeighborpropext · Classical.choice · Quot.soundAlgorithms/GreedyASS/Items/Definitions/Abstract.lean:26
def rightNeighbor (P : Finset Point) (q : Point) : Option Nat :=  least ((keysAt P q.2).filter fun x => q.1 < x)

The neighbors of a point q are the nearest keys touched at the same time q.2, strictly to its left and strictly to its right. Geometric.keysAt P q.2 is the set of keys of that row. These are the paper’s L-IntL\text{-}\mathrm{Int} and R-IntR\text{-}\mathrm{Int}, taken at the item’s most recent access. Their characterizations are what one expects:

theoremGeometric.GreedyASS.Items.Results.left_neighborpropext · Classical.choice · Quot.soundAlgorithms/GreedyASS/Items/Abstract.lean:12
theorem left_neighbor (P : Finset Point) (q : Point) (key : Nat) :    leftNeighbor P q = some key ↔      (key, q.2) ∈ P ∧ key < q.1 ∧ ∀ x, (x, q.2) ∈ P → x < q.1 → x ≤ key :=  left_spec P q key
theoremGeometric.GreedyASS.Items.Results.right_neighborpropext · Classical.choice · Quot.soundAlgorithms/GreedyASS/Items/Abstract.lean:17
theorem right_neighbor (P : Finset Point) (q : Point) (key : Nat) :    rightNeighbor P q = some key ↔      (key, q.2) ∈ P ∧ q.1 < key ∧ ∀ x, (x, q.2) ∈ P → q.1 < x → key ≤ x :=  right_spec P q key

25.5.2.3 External, glancing and internal

defGeometric.GreedyASS.Items.LeftExternalpropext · Classical.choice · Quot.soundAlgorithms/GreedyASS/Items/Definitions/Abstract.lean:40
def LeftExternal (P : Finset Point) (q : Point) : Prop :=  Active P q ∧ ∀ p ∈ P, p.2 = q.2 → q.1 ≤ p.1
defGeometric.GreedyASS.Items.RightExternalpropext · Classical.choice · Quot.soundAlgorithms/GreedyASS/Items/Definitions/Abstract.lean:43
def RightExternal (P : Finset Point) (q : Point) : Prop :=  Active P q ∧ ∀ p ∈ P, p.2 = q.2 → p.1 ≤ q.1
defGeometric.GreedyASS.Items.Externalpropext · Classical.choice · Quot.soundAlgorithms/GreedyASS/Items/Definitions/Abstract.lean:46
def External (P : Finset Point) (q : Point) : Prop :=  LeftExternal P q ∨ RightExternal P q

An active point is left external if no point of its own row is to its left, and right external if none is to its right. A point alone on its row is both. The definition looks only at the row, not at the rest of the picture. In an arborally satisfied set this is the same as an empty quadrant, closed on both sides: nothing at or to the left of q is at or above it, except q itself.

theoremGeometric.GreedyASS.Items.Results.quadrantspropext · Classical.choice · Quot.soundAlgorithms/GreedyASS/Items/Abstract.lean:31
/-- External points are precisely the exposed upper quadrant corners in an ASS. -/theorem quadrants {P : Finset Point} {q : Point} (satisfied : ASS P) (active : Active P q) :    (LeftExternal P q ↔ ∀ p ∈ P, p.1 ≤ q.1 → q.2 ≤ p.2 → p = q) ∧      (RightExternal P q ↔ ∀ p ∈ P, q.1 ≤ p.1 → q.2 ≤ p.2 → p = q) :=  ⟨left_external_iff satisfied active, right_external_iff satisfied active⟩

So the left-external points are the corners of the staircase seen from the far left: a request to the left of every key would see exactly these points. Going from left to right along this staircase, the points rise. The right-external staircase is the mirror image. The two staircases do not cross:

theoremGeometric.GreedyASS.Items.Results.external_chainspropext · Classical.choice · Quot.soundAlgorithms/GreedyASS/Items/Abstract.lean:86
/-- The two external chains are ordered and cannot cross. -/theorem external_chains {P : Finset Point} (satisfied : ASS P) {p q : Point}    (left : LeftExternal P p) (right : RightExternal P q) : p.1 ≤ q.1 :=  external_order satisfied left right

Every left-external key is at most every right-external key. Otherwise one of the two points would lie in the other’s empty quadrant.

defGeometric.GreedyASS.Items.LeftGlancingpropext · Classical.choice · Quot.soundAlgorithms/GreedyASS/Items/Definitions/Abstract.lean:49
def LeftGlancing (P : Finset Point) (q : Point) : Prop :=  Active P q ∧ ¬ External P q ∧ ∀ p ∈ P, q.2 < p.2 → q.1 < p.1
defGeometric.GreedyASS.Items.RightGlancingpropext · Classical.choice · Quot.soundAlgorithms/GreedyASS/Items/Definitions/Abstract.lean:52
def RightGlancing (P : Finset Point) (q : Point) : Prop :=  Active P q ∧ ¬ External P q ∧ ∀ p ∈ P, q.2 < p.2 → p.1 < q.1
defGeometric.GreedyASS.Items.Glancingpropext · Classical.choice · Quot.soundAlgorithms/GreedyASS/Items/Definitions/Abstract.lean:55
def Glancing (P : Finset Point) (q : Point) : Prop :=  LeftGlancing P q ∨ RightGlancing P q

A nonexternal active point is left glancing if every point that is strictly later lies strictly to its right. Since the access, everything has happened on the right, and the left part of the picture has frozen. This is the paper’s condition that the quadrant strictly above and at or to the left of the most recent access is empty. Right glancing is symmetric. A point whose row is the latest row of the snapshot has no later points, so if it is not external, it is both left and right glancing.

defGeometric.GreedyASS.Items.Internalpropext · Classical.choice · Quot.soundAlgorithms/GreedyASS/Items/Definitions/Abstract.lean:58
def Internal (P : Finset Point) (q : Point) : Prop :=  Active P q ∧ ¬ External P q ∧ ¬ Glancing P q

An internal point is active and neither external nor glancing: some later point lies strictly to its left, and some strictly to its right. (No later point is in its own column, since it is active.)

instanceGeometric.GreedyASS.Items.instDecidableActivepropext · Classical.choice · Quot.soundAlgorithms/GreedyASS/Items/Definitions/Abstract.lean:61
instance (P : Finset Point) (q : Point) : Decidable (Active P q) :=  inferInstanceAs (Decidable (_ ∧ ∀ p ∈ P, _ → _))
instanceGeometric.GreedyASS.Items.instDecidableLeftExternalpropext · Classical.choice · Quot.soundAlgorithms/GreedyASS/Items/Definitions/Abstract.lean:64
instance (P : Finset Point) (q : Point) : Decidable (LeftExternal P q) :=  inferInstanceAs (Decidable (Active P q ∧ ∀ p ∈ P, _ → _))
instanceGeometric.GreedyASS.Items.instDecidableRightExternalpropext · Classical.choice · Quot.soundAlgorithms/GreedyASS/Items/Definitions/Abstract.lean:67
instance (P : Finset Point) (q : Point) : Decidable (RightExternal P q) :=  inferInstanceAs (Decidable (Active P q ∧ ∀ p ∈ P, _ → _))
instanceGeometric.GreedyASS.Items.instDecidableExternalpropext · Classical.choice · Quot.soundAlgorithms/GreedyASS/Items/Definitions/Abstract.lean:70
instance (P : Finset Point) (q : Point) : Decidable (External P q) :=  inferInstanceAs (Decidable (LeftExternal P q ∨ RightExternal P q))
instanceGeometric.GreedyASS.Items.instDecidableLeftGlancingpropext · Classical.choice · Quot.soundAlgorithms/GreedyASS/Items/Definitions/Abstract.lean:73
instance (P : Finset Point) (q : Point) : Decidable (LeftGlancing P q) :=  inferInstanceAs (Decidable (Active P q ∧ ¬ External P q ∧ ∀ p ∈ P, _ → _))
instanceGeometric.GreedyASS.Items.instDecidableRightGlancingpropext · Classical.choice · Quot.soundAlgorithms/GreedyASS/Items/Definitions/Abstract.lean:76
instance (P : Finset Point) (q : Point) : Decidable (RightGlancing P q) :=  inferInstanceAs (Decidable (Active P q ∧ ¬ External P q ∧ ∀ p ∈ P, _ → _))
instanceGeometric.GreedyASS.Items.instDecidableGlancingpropext · Classical.choice · Quot.soundAlgorithms/GreedyASS/Items/Definitions/Abstract.lean:79
instance (P : Finset Point) (q : Point) : Decidable (Glancing P q) :=  inferInstanceAs (Decidable (LeftGlancing P q ∨ RightGlancing P q))
instanceGeometric.GreedyASS.Items.instDecidableInternalpropext · Classical.choice · Quot.soundAlgorithms/GreedyASS/Items/Definitions/Abstract.lean:82
instance (P : Finset Point) (q : Point) : Decidable (Internal P q) :=  inferInstanceAs (Decidable (Active P q ∧ ¬ External P q ∧ ¬ Glancing P q))

All these classes quantify over the finite set P, so they are decidable, and the coloring below can branch on them.

Theorem (three classes). Every active point is external, glancing or internal, and exactly one of the three.

theoremGeometric.GreedyASS.Items.Results.classespropext · Classical.choice · Quot.soundAlgorithms/GreedyASS/Items/Abstract.lean:37
theorem classes {P : Finset Point} {q : Point} (active : Active P q) :    (External P q ∨ Glancing P q ∨ Internal P q) ∧      ¬ (External P q ∧ Glancing P q) ∧      ¬ (External P q ∧ Internal P q) ∧ ¬ (Glancing P q ∧ Internal P q) := classification active

A nonexternal point has both neighbors, and the neighbors are missing exactly on the external sides:

25.5.2.4 An example

Run GreedyASS on the five keys 0,…,40, \dots, 4 with requests 0,2,1,0,3,40, 2, 1, 0, 3, 4. The sweep of Section 25.1 adds 1212 points:

time 5   request 4   ·  ·  ·  ●  ●
time 4   request 3   ●  ·  ●  ●  ·
time 3   request 0   ●  ·  ·  ·  ·
time 2   request 1   ●  ●  ●  ·  ·
time 1   request 2   ●  ·  ●  ·  ·
time 0   request 0   ●  ·  ·  ·  ·
          key        0  1  2  3  4

The active points are (0,4)(0,4), (1,2)(1,2), (2,4)(2,4), (3,5)(3,5) and (4,5)(4,5).

  • (0,4)(0,4) is the leftmost point of row 44, so it is left external.
  • (1,2)(1,2) has neighbors 00 and 22 on row 22. Later, (0,3)(0,3) lies to its left and (2,4)(2,4) to its right, so it is internal.
  • (2,4)(2,4) has neighbors 00 and 33 on row 44. The only later points, (3,5)(3,5) and (4,5)(4,5), are to its right, so it is left glancing.
  • (3,5)(3,5) and (4,5)(4,5) are the left-external and right-external points of the latest row.

The left-external staircase is (0,4),(3,5)(0,4), (3,5). It rises from left to right, and its upper-left quadrants are empty. The right-external staircase is just (4,5)(4,5).

25.5.2.5 The latest row and its corners

The newest row plays a special role: its two extreme points are the top corners, which the coloring always makes blue.

defGeometric.GreedyASS.Items.LatestRowpropext · Classical.choice · Quot.soundAlgorithms/GreedyASS/Items/Definitions/Abstract.lean:32
/-- Whether a row is at least as recent as every point in this snapshot.This depends on the point set, including during a reference-forest update. -/def LatestRow (P : Finset Point) (stamp : Nat) : Prop :=  ∀ p ∈ P, p.2 ≤ stamp
instanceGeometric.GreedyASS.Items.instDecidableLatestRowpropext · Classical.choice · Quot.soundAlgorithms/GreedyASS/Items/Definitions/Abstract.lean:37
instance (P : Finset Point) (stamp : Nat) : Decidable (LatestRow P stamp) :=  inferInstanceAs (Decidable (∀ p ∈ P, p.2 ≤ stamp))

LatestRow P stamp says that no point of P is later than stamp; like the classes above, it is decidable. It is a property of the snapshot, not of a separate clock. The distinction matters in Phase I, where the reference forest has already moved to the new request but the old point set is still in place. The old latest row then still counts as the latest row.

defGeometric.GreedyASS.Items.cornerspropext · Classical.choice · Quot.soundAlgorithms/GreedyASS/Items/Definitions/Abstract.lean:85
/-- External items accessed on the new row, the paper's top corners. -/def corners (P : Finset Point) (time : Nat) (keys : Finset Nat) : Finset Nat :=  keys.filter fun x => External (P ∪ row time keys) (x, time)

corners P time keys is the set of keys of a new row keys, at time time, that are external once the row is added. These are the paper’s top corners.

theoremGeometric.GreedyASS.Items.Results.latest_cornerspropext · Classical.choice · Quot.soundAlgorithms/GreedyASS/Items/Abstract.lean:91
/-- Both corners exist on the actual latest row of every nonempty snapshot. -/theorem latest_corners {P : Finset Point} (nonempty : P.Nonempty) :    ∃ p q, LeftExternal P p ∧ RightExternal P q ∧ LatestRow P p.2 ∧ q.2 = p.2 :=  Items.latest_corners nonempty
theoremGeometric.GreedyASS.Items.Results.top_cornerspropext · Classical.choice · Quot.soundAlgorithms/GreedyASS/Items/Abstract.lean:70
theorem top_corners {n time : Nat} {P : Finset Point} (keys : Finset Nat)    (history : History n time P) : (corners P time keys).card ≤ 2 := corners_bound keys history

Every nonempty snapshot has a left-external and a right-external point on its latest row: the extreme points of that row. A new row has at most two top corners, its leftmost and rightmost keys. Once the row is completed, “on the latest row” and “at the new time” agree, provided the row is nonempty:

theoremGeometric.GreedyASS.Items.Results.latest_rowpropext · Classical.choice · Quot.soundAlgorithms/GreedyASS/Items/Abstract.lean:73
/-- The latest row belongs to the point-set snapshot, not a separatesearch-history clock. On a completed nonempty row the tests agree. -/theorem latest_row {n time : Nat} {P : Finset Point} (keys : Finset Nat)    (history : History n time P) (nonempty : keys.Nonempty) {q : Point}    (member : q ∈ P ∪ row time keys) : LatestRow (P ∪ row time keys) q.2 ↔ q.2 = time :=  latest_row_iff keys history nonempty member

Finally, no active point strictly to the left of a left-external point, or strictly to the right of a right-external point, is on the latest row:

theoremGeometric.GreedyASS.Items.Results.noncornerpropext · Classical.choice · Quot.soundAlgorithms/GreedyASS/Items/Abstract.lean:80
theorem noncorner {P : Finset Point} (satisfied : ASS P) {p q r : Point}    (active : Active P q) :    (LeftExternal P r → q.1 < r.1 → ¬ LatestRow P q.2) ∧      (RightExternal P p → p.1 < q.1 → ¬ LatestRow P q.2) :=  ⟨left_not_latest satisfied active, right_not_latest satisfied active⟩

25.5.2.6 What a new row changes

In Phase II GreedyASS adds a row row time keys at a time later than every old point. The theorems of this subsection hold for any such row, not only for the one GreedyASS chooses. For a column that the row does not touch, very little changes:

Theorem (untouched items, Fact 3.3). Let P be a history before time time, and let q be active in P with q.1 not among the new keys. After the row is added, q is still the most recent access of its column, it has the same two neighbors, and it is external if and only if it was before.

theoremGeometric.GreedyASS.Items.Results.unchangedpropext · Classical.choice · Quot.soundAlgorithms/GreedyASS/Items/Abstract.lean:42
/-- Fact 3.3 and the corresponding latest-access and externality invariants. -/theorem unchanged {n time : Nat} {P : Finset Point} (keys : Finset Nat)    (history : History n time P) {q : Point} (active : Active P q) (untouched : q.1 ∉ keys) :    latest (P ∪ row time keys) q.1 = latest P q.1 ∧ Active (P ∪ row time keys) q ∧      leftNeighbor (P ∪ row time keys) q = leftNeighbor P q ∧      rightNeighbor (P ∪ row time keys) q = rightNeighbor P q ∧      (External (P ∪ row time keys) q ↔ External P q) :=  ⟨latest_unchanged P time keys q.1 untouched,    (active_unchanged P time keys q untouched).mpr active,    (neighbors_unchanged keys history active.1).1, (neighbors_unchanged keys history active.1).2,    external_unchanged keys history active untouched⟩

The paper’s Fact 3.3 is the statement about neighbors. The rest is immediate from the definitions: all of these depend only on q’s own row and column, and the new row is neither.

Glancing status can only be lost. A left-glancing item stays left glancing exactly when the whole new row is strictly to its right. An internal item stays internal:

theoremGeometric.GreedyASS.Items.Results.glancing_updatepropext · Classical.choice · Quot.soundAlgorithms/GreedyASS/Items/Abstract.lean:54
theorem glancing_update {n time : Nat} {P : Finset Point} (keys : Finset Nat)    (history : History n time P) {q : Point} (active : Active P q) (untouched : q.1 ∉ keys) :    (LeftGlancing (P ∪ row time keys) q ↔ LeftGlancing P q ∧ ∀ x ∈ keys, q.1 < x) ∧      (RightGlancing (P ∪ row time keys) q ↔ RightGlancing P q ∧ ∀ x ∈ keys, x < q.1) :=  ⟨left_glancing_update keys history active untouched, right_glancing_update keys history active untouched⟩
theoremGeometric.GreedyASS.Items.Results.internalpropext · Classical.choice · Quot.soundAlgorithms/GreedyASS/Items/Abstract.lean:60
theorem internal {n time : Nat} {P : Finset Point} (keys : Finset Nat)    (history : History n time P) {q : Point} (old : Internal P q) (untouched : q.1 ∉ keys) :    Internal (P ∪ row time keys) q := internal_unchanged keys history old untouched

The keys of the new row itself have nothing above them, so none of them is internal:

theoremGeometric.GreedyASS.Items.Results.accessedpropext · Classical.choice · Quot.soundAlgorithms/GreedyASS/Items/Abstract.lean:64
/-- Every new access is external or glancing, never internal. -/theorem accessed {n time : Nat} {P : Finset Point} (keys : Finset Nat)    (history : History n time P) {x : Nat} (member : x ∈ keys) :    (External (P ∪ row time keys) (x, time) ∨ Glancing (P ∪ row time keys) (x, time)) ∧      ¬ Internal (P ∪ row time keys) (x, time) := top_classification keys history member

25.5.3 The barrier

Lemma 3.4 of the paper is a geometric fact about GreedyASS alone. Suppose key kk is accessed at time ss, and every request after time ss is at least kk. Then GreedyASS never accesses a key smaller than kk after time ss. The point (k,s)(k, s) is a barrier: every old point to its left spans, with any later request at or to its right, a rectangle containing (k,s)(k, s), so no such old point is visible again.

The library first isolates the invariant that makes this work:

defGeometric.GreedyASS.Barrier.Leftpropext · Classical.choice · Quot.soundAlgorithms/GreedyASS/Barrier/Definitions/Abstract.lean:9
/-- The point at `(key, stamp)` blocks old points strictly to its left:none of those points is more recent than the barrier. -/def Left (P : Finset Point) (key stamp : Nat) : Prop :=  (key, stamp) ∈ P ∧ ∀ p ∈ P, p.1 < key → p.2 ≤ stamp
defGeometric.GreedyASS.Barrier.Rightpropext · Classical.choice · Quot.soundAlgorithms/GreedyASS/Barrier/Definitions/Abstract.lean:14
/-- Symmetric barrier for points strictly to the right. -/def Right (P : Finset Point) (key stamp : Nat) : Prop :=  (key, stamp) ∈ P ∧ ∀ p ∈ P, key < p.1 → p.2 ≤ stamp

Barrier.Left P key stamp says that (key, stamp) is a point of P and that no point strictly to its left is later than stamp. It does not require stamp to be the latest time. One sweep cannot cross a barrier, and a stream of sweeps preserves it:

theoremGeometric.GreedyASS.Barrier.Results.left_steppropext · Classical.choice · Quot.soundAlgorithms/GreedyASS/Barrier/Abstract.lean:9
/-- The next sweep cannot access the far side of a left barrier. -/theorem left_step {P : Finset Point} {key stamp time request : Nat}    (barrier : Left P key stamp) (later : stamp ≤ time) (side : key ≤ request) :    ∀ x ∈ greedySweep time request P, key ≤ x := sweep_left barrier later side
theoremGeometric.GreedyASS.Barrier.Results.right_steppropext · Classical.choice · Quot.soundAlgorithms/GreedyASS/Barrier/Abstract.lean:14
theorem right_step {P : Finset Point} {key stamp time request : Nat}    (barrier : Right P key stamp) (later : stamp ≤ time) (side : request ≤ key) :    ∀ x ∈ greedySweep time request P, x ≤ key := sweep_right barrier later side
theoremGeometric.GreedyASS.Barrier.Results.left_streampropext · Classical.choice · Quot.soundAlgorithms/GreedyASS/Barrier/Abstract.lean:18
/-- Preserve the barrier and every old far-side point over a whole stream. -/theorem left_stream {P : Finset Point} {key stamp time : Nat} (xs : List Nat)    (barrier : Left P key stamp) (later : stamp ≤ time)    (side : ∀ x ∈ xs, key ≤ x) :    Left (fill greedySweep time P xs) key stamp ∧      ∀ p : Point, p.1 < key → (p ∈ fill greedySweep time P xs ↔ p ∈ P) :=  fill_left xs barrier later side
theoremGeometric.GreedyASS.Barrier.Results.right_streampropext · Classical.choice · Quot.soundAlgorithms/GreedyASS/Barrier/Abstract.lean:26
theorem right_stream {P : Finset Point} {key stamp time : Nat} (xs : List Nat)    (barrier : Right P key stamp) (later : stamp ≤ time)    (side : ∀ x ∈ xs, x ≤ key) :    Right (fill greedySweep time P xs) key stamp ∧      ∀ p : Point, key < p.1 → (p ∈ fill greedySweep time P xs ↔ p ∈ P) :=  fill_right xs barrier later side

left_step concerns one Geometric.greedySweep at any time time ≥ stamp, for a request request ≥ key: every key of the row is at least key. left_stream runs the sweep with Geometric.fill on a whole list of requests that are all at least key. The barrier is still a barrier at the end, and the points strictly to the left of key are exactly the old ones. Neither statement needs P to be arborally satisfied.

Right after (key, stamp) is accessed, row stamp is the latest row, so the barrier condition holds automatically. This gives the paper’s lemma:

Theorem (Lemma 3.4). Let P be a history before time stamp + 1 containing (key, stamp). Run GreedyASS on requests that are all at least key, from time stamp + 1. Then every point added after time stamp has key at least key. Symmetrically, for requests that are all at most key.

theoremGeometric.GreedyASS.Barrier.Results.no_leftpropext · Classical.choice · Quot.soundAlgorithms/GreedyASS/Barrier/Abstract.lean:33
/-- Lemma 3.4 of arXiv:1810.01785, also allowing subsequent requests equal tothe barrier key. No future access can cross to the opposite side. -/theorem no_left {n key stamp : Nat} {P : Finset Point} (xs : List Nat)    (history : History n (stamp + 1) P) (accessed : (key, stamp) ∈ P)    (side : ∀ x ∈ xs, key ≤ x) :    ∀ p ∈ fill greedySweep (stamp + 1) P xs, stamp < p.2 → key ≤ p.1 :=  no_left_access xs history accessed side
theoremGeometric.GreedyASS.Barrier.Results.no_rightpropext · Classical.choice · Quot.soundAlgorithms/GreedyASS/Barrier/Abstract.lean:41
theorem no_right {n key stamp : Nat} {P : Finset Point} (xs : List Nat)    (history : History n (stamp + 1) P) (accessed : (key, stamp) ∈ P)    (side : ∀ x ∈ xs, x ≤ key) :    ∀ p ∈ fill greedySweep (stamp + 1) P xs, stamp < p.2 → p.1 ≤ key :=  no_right_access xs history accessed side

The paper assumes the later requests are strictly greater than kk; the library allows them to equal kk. Two accessed endpoints on the same row confine all later accesses to the interval between them, as long as the requests stay in that interval:

theoremGeometric.GreedyASS.Barrier.Results.intervalpropext · Classical.choice · Quot.soundAlgorithms/GreedyASS/Barrier/Abstract.lean:47
/-- Two accessed endpoints confine all later accesses while searches remainbetween them. This includes either endpoint and singleton intervals. -/theorem interval {n lo hi stamp : Nat} {P : Finset Point} (xs : List Nat)    (history : History n (stamp + 1) P)    (left : (lo, stamp) ∈ P) (right : (hi, stamp) ∈ P)    (inside : ∀ x ∈ xs, lo ≤ x ∧ x ≤ hi) :    ∀ p ∈ fill greedySweep (stamp + 1) P xs, stamp < p.2 → lo ≤ p.1 ∧ p.1 ≤ hi := … by  intro p hp recent  exact ⟨no_left xs history left (fun x hx => (inside x hx).1) p hp recent,    no_right xs history right (fun x hx => (inside x hx).2) p hp recent⟩

Section 25.4 combines the barrier with the reference forest (Results.barrier): if a component has stayed a root since one of its keys was accessed, all later searches are on one side of that key, and so are all later accesses.

25.5.4 Virgin roots

The second part of the potential lives on the reference forest. After a search for xx, Reference.forest lists the components hanging off the path to xx, together with xx itself as a one-leaf component. A component is virgin while none of its keys has been accessed since it became a root. Each virgin root holds 1212 units. When a row touches a virgin root, the root stops being virgin, and its 1212 units pay for accesses. When the finger moves and the forest changes, new roots are born virgin, and the lazy finger pays for them.

Unlike the item definitions, these definitions run GreedyASS itself, from the empty point set at time 00:

defGeometric.GreedyASS.Virgin.pointspropext · Classical.choice · Quot.soundAlgorithms/GreedyASS/Virgin/Definitions/Abstract.lean:11
/-- The actual points produced by GreedyASS from the empty history. -/def points (history : List Nat) : Finset Point := fill greedySweep 0 ∅ history
defGeometric.GreedyASS.Virgin.keyspropext · Classical.choice · Quot.soundAlgorithms/GreedyASS/Virgin/Definitions/Abstract.lean:14
def keys (history : List Nat) (request : Nat) : Finset Nat :=  greedySweep history.length request (points history)

points history is the set of GreedyASS points after serving history, and keys history request is the row GreedyASS adds for the next request, at time history.length.

defGeometric.GreedyASS.Virgin.NonVirginpropext · Classical.choice · Quot.soundAlgorithms/GreedyASS/Virgin/Definitions/Abstract.lean:17
/-- An earlier access occurred while this component was a root, and it hasremained a root at every intervening search, including the current one.This is a proof observation, not extra state required by GreedyASS. -/def NonVirgin (t : Tree) (lo : Nat) (history : List Nat) (request : Nat)    (c : Component) : Prop :=  ∃ before first after accessed,    history = before ++ (first :: after) ∧    accessed ∈ keys before first ∧ c.Contains accessed ∧    ∀ x ∈ first :: (after ++ [request]), c ∈ forest t lo x

NonVirgin t lo history request c describes the moment just before the row for request is added. It holds when the history splits as before ++ first :: after such that:

  • the row GreedyASS added for first touched a key of c; and
  • c was a root of the reference forest at the search for first, at every later search in the history, and at the current request.

So c was accessed while it was a root, and has stayed a root without interruption. The definition is a function of the history and the reference tree. It is an observation of the proof, not state that GreedyASS keeps. The paper’s definition is the same, read the other way: a root that has been a root continuously since time kk is ii-virgin if none of its keys was accessed at times kk to ii. The paper’s count includes the current row ii. The library’s NonVirgin stops just before it, and update below adds the current row.

defGeometric.GreedyASS.Virgin.touchedpropext · Classical.choice · Quot.soundAlgorithms/GreedyASS/Virgin/Definitions/Abstract.lean:27
/-- Components touched by the current row, independently of their age. -/noncomputable def touched (t : Tree) (lo : Nat) (history : List Nat) (request : Nat) :    Finset Component := by  classical  exact (forest t lo request).toFinset.filter fun c => ∃ x ∈ keys history request, c.Contains x
defGeometric.GreedyASS.Virgin.oldTouchedpropext · Classical.choice · Quot.soundAlgorithms/GreedyASS/Virgin/Definitions/Abstract.lean:33
noncomputable def oldTouched (t : Tree) (lo : Nat) (history : List Nat) (request : Nat) :    Finset Component := by  classical  exact (touched t lo history request).filter (NonVirgin t lo history request)
defGeometric.GreedyASS.Virgin.rootspropext · Classical.choice · Quot.soundAlgorithms/GreedyASS/Virgin/Definitions/Abstract.lean:38
/-- Virgin roots immediately before adding the current access row. -/noncomputable def roots (t : Tree) (lo : Nat) (history : List Nat) (request : Nat) :    Finset Component := by  classical  exact (forest t lo request).toFinset.filter fun c => ¬ NonVirgin t lo history request c

touched is the set of roots of the current forest that contain a key of the new row; the paper’s tit_i is their number. oldTouched is the set of those that are not virgin. roots is the set of virgin roots before the row is added.

defGeometric.GreedyASS.Virgin.beforepropext · Classical.choice · Quot.soundAlgorithms/GreedyASS/Virgin/Definitions/Abstract.lean:44
/-- Phase-II virgin potential; each untouched virgin root contributes 12. -/noncomputable def before (t : Tree) (lo : Nat) (history : List Nat) (request : Nat) : Nat :=  12 * (roots t lo history request).card
defGeometric.GreedyASS.Virgin.afterpropext · Classical.choice · Quot.soundAlgorithms/GreedyASS/Virgin/Definitions/Abstract.lean:48
noncomputable def after (t : Tree) (lo : Nat) (history : List Nat) (request : Nat) : Nat :=  12 * (roots t lo history request \ touched t lo history request).card

before is the virgin potential before the new row, 1212 per virgin root. after is the virgin potential after it: only the virgin roots the row does not touch keep their 1212 units.

25.5.4.1 How virgin status evolves

Nothing is old before the first request:

Theorem (one step of history). After request is served, a component is old at the next request next exactly when it is a root of next’s forest and either it was already old at request, or it was a root at request and the row for request touched it.

theoremGeometric.GreedyASS.Virgin.Results.lifetimepropext · Classical.choice · Quot.soundAlgorithms/GreedyASS/Virgin/Abstract.lean:13
/-- Exact lifetime update, including loss of old status when a root disappears. -/theorem lifetime (t : Tree) (lo : Nat) (history : List Nat) (request next : Nat) (c : Component) :    NonVirgin t lo (history ++ [request]) next c ↔      c ∈ forest t lo next ∧        (NonVirgin t lo history request c ∨          (c ∈ forest t lo request ∧ ∃ x ∈ keys history request, c.Contains x)) :=  advance t lo history request next c

The “root of next’s forest” condition is what makes a component lose its old status when the finger’s movement merges it away or splits it. If it reappears later, it is reborn virgin. Two consequences follow. Taking next = request, the virgin roots after the row are those before it minus the touched ones, so after is really the virgin potential of the completed history. And a component that is a root at both the old and the new request has the same status for both:

theoremGeometric.GreedyASS.Virgin.Results.updatepropext · Classical.choice · Quot.soundAlgorithms/GreedyASS/Virgin/Abstract.lean:34
/-- The remaining virgin roots agree with the actual completed history. -/theorem update (t : Tree) (lo : Nat) (history : List Nat) (request : Nat) :    roots t lo (history ++ [request]) request =      roots t lo history request \ touched t lo history request := roots_after t lo history request
theoremGeometric.GreedyASS.Virgin.Results.survivalpropext · Classical.choice · Quot.soundAlgorithms/GreedyASS/Virgin/Abstract.lean:51
/-- Surviving roots preserve their completed-row non-virgin status. -/theorem survival (t : Tree) (lo : Nat) (history : List Nat) (old new : Nat) {c : Component}    (oldRoot : c ∈ forest t lo old) (newRoot : c ∈ forest t lo new) :    NonVirgin t lo (history ++ [old]) new c ↔ NonVirgin t lo (history ++ [old]) old c :=  survives t lo history old new oldRoot newRoot

25.5.4.2 Phase II: at most two old roots

An old root constrains the next row. It contains a key that was accessed while it was a root, and since then every search has been on one side of that key (Results.point_side). By the barrier, every access is then on that side too, including the current row:

theoremGeometric.GreedyASS.Virgin.Results.barrierpropext · Classical.choice · Quot.soundAlgorithms/GreedyASS/Virgin/Abstract.lean:21
/-- An earlier access constrains the actual next GreedyASS row. -/theorem barrier {t : Tree} {lo request : Nat} {history : List Nat} {c : Component}    (valid : ∀ x ∈ history ++ [request], lo ≤ x ∧ x < lo + leaves t)    (old : NonVirgin t lo history request c) :    ∃ accessed, c.Contains accessed ∧      ((∀ x ∈ keys history request, x ≤ accessed) ∨        (∀ x ∈ keys history request, accessed ≤ x)) := old_barrier valid old

This plays the role of the paper’s Lemma 3.6. The hypothesis valid says that every request, past and current, is a leaf of t.

Now the components of the forest are disjoint intervals in key order. If an old component is touched by the row, the whole row lies on one side of one of its keys. So, among the touched components, an old one can only be the leftmost or the rightmost.

Theorem (Lemma 3.7). For valid requests, the row GreedyASS adds for the current request touches at most two old roots of the current forest.

theoremGeometric.GreedyASS.Virgin.Results.countpropext · Classical.choice · Quot.soundAlgorithms/GreedyASS/Virgin/Abstract.lean:29
/-- Lemma 3.7, including searched singleton components. -/theorem count (t : Tree) (lo : Nat) (history : List Nat) (request : Nat)    (valid : ∀ x ∈ history ++ [request], lo ≤ x ∧ x < lo + leaves t) :    (oldTouched t lo history request).card ≤ 2 := Virgin.count t lo history request valid

The count includes the searched one-leaf component, which may be old too. Every touched virgin root gives up its 1212 units, which is an exact identity:

Combined with the count, this gives the paper’s Lemma 3.8, which says that at least 12(ti−2)12(t_i - 2) units of virgin potential are released in Phase II. The library states it without subtraction, so it is also correct when fewer than two roots are touched:

Theorem (Lemma 3.8). For valid requests, after+12⋅∣touched∣≤before+24\mathit{after} + 12 \cdot |\mathit{touched}| \le \mathit{before} + 24.

theoremGeometric.GreedyASS.Virgin.Results.decreasepropext · Classical.choice · Quot.soundAlgorithms/GreedyASS/Virgin/Abstract.lean:44
/-- Lemma 3.8, stated without truncated subtraction. -/theorem decrease (t : Tree) (lo : Nat) (history : List Nat) (request : Nat)    (valid : ∀ x ∈ history ++ [request], lo ≤ x ∧ x < lo + leaves t) :    after t lo history request + 12 * (touched t lo history request).card ≤      before t lo history request + 24 := Virgin.decrease t lo history request valid

25.5.4.3 Phase I: new roots are born virgin

When the finger moves from old to new, the forest changes by a sequence of merges and splits (Transitions.Trace, Section 25.4). A split creates two roots and a merge creates one (Transitions.introductions). Each new root may be virgin, so the virgin potential can grow by 1212 per root created. Old roots that survive keep their status, by survival.

theoremGeometric.GreedyASS.Virgin.Results.transitionpropext · Classical.choice · Quot.soundAlgorithms/GreedyASS/Virgin/Abstract.lean:57
/-- Virgin potential increases by at most twelve per root introduction along a trace. -/theorem transition (t : Tree) (lo : Nat) (history : List Nat) (old new : Nat)    {events : List Transitions.Event}    (trace : Transitions.Trace (forest t lo old) events (forest t lo new)) :    before t lo (history ++ [old]) new ≤ after t lo history old + 12 * Transitions.introductions events :=  transition_bound t lo history old new trace
theoremGeometric.GreedyASS.Virgin.Results.forest_changepropext · Classical.choice · Quot.soundAlgorithms/GreedyASS/Virgin/Abstract.lean:64
/-- The concrete LCA-routed forest change has the root-introduction bound. -/theorem forest_change (t : Tree) (lo : Nat) (history : List Nat) (old new : Nat) :    before t lo (history ++ [old]) new ≤ after t lo history old +      12 * Transitions.introductions (Transitions.plan t lo old new) :=  plan_bound t lo history old new

The left side, before t lo (history ++ [old]) new, is the virgin potential in the new forest after old was served: the virgin part of the paper’s intermediate potential Φi′\Phi'_i. The right side starts from after t lo history old, the potential after old’s row. transition holds for every trace between the two forests. forest_change applies it to Transitions.plan, the concrete route that merges up from old to the lowest common ancestor and then splits down to new.

The paper’s Lemma 3.2 allows 2424 units for each merge or split, item and virgin potential together. The virgin part uses at most 2424 for a split and 1212 for a merge, so it leaves 1212 units per merge for the item potentials:

theoremGeometric.GreedyASS.Virgin.Results.phase_one_budgetpropext · Classical.choice · Quot.soundAlgorithms/GreedyASS/Virgin/Abstract.lean:70
/-- Virgin potential leaves twelve units per merge in the 24-per-edge budget; item changes are separate. -/theorem phase_one_budget (t : Tree) (lo : Nat) (history : List Nat) (old new : Nat) :    before t lo (history ++ [old]) new +        12 * Transitions.merges (Transitions.plan t lo old new) ≤      after t lo history old + 24 * Transitions.edges t lo old new :=  merge_budget t lo history old new

Here Transitions.edges is the number of reference edges between old and new, and the plan has one event per edge (Results.transition). Using the reserved 1212 units per merge for the item potentials is part of Section 25.6.

25.5.4.4 The example: virgin roots

Take the reference tree TT = node (node nil nil) (node nil (node nil nil)), with leaves 0,…,40, \dots, 4:

           •
         /   \
        •     •
       / \   / \
      0   1 2   •
               / \
              3   4

The searches for 33 and for 44 give the same forest: the subtree on {0,1}\{0,1\} and the single leaves {2}\{2\}, {3}\{3\}, {4}\{4\}. Just before the last row, the old roots are {0,1}\{0,1\}, {2}\{2\} and {3}\{3\}, which the row at time 44 touched. Only {4}\{4\} is virgin, so the virgin potential is 1212. The last row {3,4}\{3,4\} touches two roots, one old and one virgin. Afterwards no root is virgin, so the virgin potential is 00. Lemma 3.8 reads 0+12⋅2≤12+240 + 12 \cdot 2 \le 12 + 24.

25.5.5 Four candidate values

Every value is a node count in the reference tree, computed with the distances of Section 25.4: Distance.toLCA t lo x a b is the number of nodes on the path from leaf xx up to the lowest common ancestor of leaves aa and bb, and Distance.toRoot c x is the number of nodes from xx up to the root of a component cc. A value that the paper calls infinite is ⊤ in WithTop Nat.

25.5.5.1 Red: the access row’s neighbors

defGeometric.GreedyASS.Potential.redpropext · Classical.choice · Quot.soundAlgorithms/GreedyASS/Potential/Definitions/Abstract.lean:24
/-- Red is finite precisely when both same-row neighbors exist. Infinitymarks an undefined candidate; active nonexternal points have both neighbors. -/def red (t : Tree) (lo : Nat) (P : Finset Point) (q : Point) : WithTop Nat :=  match leftNeighbor P q, rightNeighbor P q with  | some a, some b => (Distance.toLCA t lo q.1 a b : Nat)  | _, _ => ⊤

red t lo P q is the node distance from q.1 to the lowest common ancestor of its two neighbors on its access row, or ⊤ if a neighbor is missing. This is the paper’s Redi(x)=d(x,Lca(L-Int,R-Int))\mathrm{Red}_i(x) = d(x, \mathrm{Lca}(L\text{-}\mathrm{Int}, R\text{-}\mathrm{Int})), where dd counts nodes. The value only makes sense when the neighbors surround q.1. For nonexternal points they do, and red is finite:

A row that does not touch q’s column changes neither its neighbors nor its red value (Lemma 3.14). The library proves this for every untouched active point, external or not:

theoremGeometric.GreedyASS.Potential.Results.red_stablepropext · Classical.choice · Quot.soundAlgorithms/GreedyASS/Potential/Abstract.lean:27
/-- Lemma 3.14 at the retained active point of an unaccessed column. -/theorem red_stable (t : Tree) (lo : Nat) {n time : Nat} {P : Finset Point}    (keys : Finset Nat) (history : History n time P) {q : Point}    (active : Active P q) (untouched : q.1 ∉ keys) :    Active (P ∪ row time keys) q ∧ red t lo (P ∪ row time keys) q = red t lo P q :=  ⟨(active_unchanged P time keys q untouched).mpr active, red_unchanged t lo keys history active.1⟩

25.5.5.2 Green: the nearest external items

defGeometric.GreedyASS.Potential.externalKeyspropext · Classical.choice · Quot.soundAlgorithms/GreedyASS/Potential/Definitions/Abstract.lean:14
/-- Columns of actual active external points, not all previously accessed keys. -/def externalKeys (P : Finset Point) : Finset Nat :=  (P.filter fun q => External P q).image Prod.fst
defGeometric.GreedyASS.Potential.leftExternalpropext · Classical.choice · Quot.soundAlgorithms/GreedyASS/Potential/Definitions/Abstract.lean:18
def leftExternal (P : Finset Point) (x : Nat) : Option Nat :=  greatest ((externalKeys P).filter fun y => y < x)
defGeometric.GreedyASS.Potential.rightExternalpropext · Classical.choice · Quot.soundAlgorithms/GreedyASS/Potential/Definitions/Abstract.lean:21
def rightExternal (P : Finset Point) (x : Nat) : Option Nat :=  least ((externalKeys P).filter fun y => x < y)

externalKeys P is the set of columns whose active point is external. leftExternal P x and rightExternal P x are the nearest such columns strictly to the left and right of x, the paper’s L-ExtL\text{-}\mathrm{Ext} and R-ExtR\text{-}\mathrm{Ext}:

theoremGeometric.GreedyASS.Potential.Results.externalspropext · Classical.choice · Quot.soundAlgorithms/GreedyASS/Potential/Abstract.lean:10
theorem externals (P : Finset Point) (x : Nat) :    x ∈ externalKeys P ↔ ∃ stamp, External P (x, stamp) := mem_external P x
theoremGeometric.GreedyASS.Potential.Results.left_neighborpropext · Classical.choice · Quot.soundAlgorithms/GreedyASS/Potential/Abstract.lean:13
theorem left_neighbor (P : Finset Point) (x a : Nat) :    leftExternal P x = some a ↔ a ∈ externalKeys P ∧ a < x ∧      ∀ y ∈ externalKeys P, y < x → y ≤ a := left_external_spec P x a
theoremGeometric.GreedyASS.Potential.Results.right_neighborpropext · Classical.choice · Quot.soundAlgorithms/GreedyASS/Potential/Abstract.lean:17
theorem right_neighbor (P : Finset Point) (x b : Nat) :    rightExternal P x = some b ↔ b ∈ externalKeys P ∧ x < b ∧      ∀ y ∈ externalKeys P, x < y → b ≤ y := right_external_spec P x b
defGeometric.GreedyASS.Potential.greenpropext · Classical.choice · Quot.soundAlgorithms/GreedyASS/Potential/Definitions/Abstract.lean:31
/-- Missing either external neighbor gives the paper's infinite green value. -/def green (t : Tree) (lo : Nat) (P : Finset Point) (x : Nat) : WithTop Nat :=  match leftExternal P x, rightExternal P x with  | some a, some b => (Distance.toLCA t lo x a b + 1 : Nat)  | _, _ => ⊤

green t lo P x is one more than the node distance from x to the lowest common ancestor of its external neighbors, and ⊤ if either is missing. In particular the leftmost and the rightmost external items have infinite green value, as in the paper:

25.5.5.3 Blue: the current forest

defGeometric.GreedyASS.Potential.blueno axiomsAlgorithms/GreedyASS/Potential/Definitions/Abstract.lean:37
/-- Compute the blue value in the actual forest by following the search pathuntil it separates from the item's path. Distances count nodes, then add one. -/def blue : Tree → Nat → Nat → Nat → Nat  | .nil, _, _, _ => 2  | .node l r, lo, searched, x =>      if searched < lo + leaves l then        if x < lo + leaves l then blue l lo searched x        else Distance.depth r (lo + leaves l) x + 2      else        if x < lo + leaves l then Distance.depth l lo x + 2        else blue r (lo + leaves l) searched x

blue t lo searched x follows the search path to searched from the root. At the first node where the path to x leaves it, x is in the sibling subtree, which is a component of the forest. The value is then the edge depth of x in that component plus 22. If the two paths never separate, x is the searched leaf, and the value is 22. In terms of Section 25.4, blue is the paper’s Bluei(x)=d(x,ri(x))+1\mathrm{Blue}_i(x) = d(x, r_i(x)) + 1, with ri(x)r_i(x) the root of x’s component:

The blue value is always at least 22. blue is defined by the same recursion for every search, valid or not; blue_root describes it whenever x lies in a component of the searched forest.

defGeometric.GreedyASS.Potential.currentBlueno axiomsAlgorithms/GreedyASS/Potential/Definitions/Abstract.lean:49
/-- Empty histories have no current forest contribution. -/def currentBlue (t : Tree) (lo : Nat) (searches : List Nat) (x : Nat) : Nat :=  match searches.getLast? with  | none => 0  | some searched => blue t lo searched x

currentBlue t lo searches x is the blue value for the last search of searches, the current position of the finger. Before any search it is 00. The list searches is the request history. Its entry with index ii is the request that produced the row at time ii.

The comparison behind the paper’s Fact 3.2 is that green is at most blue exactly when both external neighbors lie in the item’s component:

theoremGeometric.GreedyASS.Potential.Results.green_comparisonpropext · Classical.choice · Quot.soundAlgorithms/GreedyASS/Potential/Abstract.lean:43
/-- Candidate-value comparison; the color rule additionally handles top corners. -/theorem green_comparison (t : Tree) (lo searched : Nat) (P : Finset Point) (x a b : Nat)    {c : Component} (member : c ∈ forest t lo searched) (inside : c.Contains x)    (left : leftExternal P x = some a) (right : rightExternal P x = some b)    (valid : ∀ q ∈ P, lo ≤ q.1 ∧ q.1 < lo + leaves t) :    green t lo P x ≤ (blue t lo searched x : WithTop Nat) ↔ c.Contains a ∧ c.Contains b :=  green_blue t lo searched P x a b member inside left right valid

25.5.5.4 Purple: the largest blue since the last access

defGeometric.GreedyASS.Potential.purplepropext · Classical.choice · Quot.soundAlgorithms/GreedyASS/Potential/Definitions/Abstract.lean:55
/-- Inclusive maximum from the most recent access row through the currentsearch. Row zero corresponds to the first entry in `searches`. -/def purple (t : Tree) (lo : Nat) (searches : List Nat) (stamp x : Nat) : Nat :=  (searches.drop stamp).toFinset.sup fun searched => blue t lo searched x

purple t lo searches stamp x is the maximum of the blue values of x over the searches at index stamp and later, through the current search. For an item whose most recent access is at time stamp, this is the highest blue value since that access, the paper’s Purplei(x)\mathrm{Purple}_i(x). The maximum is a Finset.sup, so it is 00 if stamp is past the end of the list. The update rule below assumes stamp ≤ searches.length.

Appending a search raises purple to the new blue value if that is larger. Purple measured from the newest search alone is the new blue value:

theoremGeometric.GreedyASS.Potential.Results.purple_updatepropext · Classical.choice · Quot.soundAlgorithms/GreedyASS/Potential/Abstract.lean:51
theorem purple_update (t : Tree) (lo : Nat) (searches : List Nat) (searched stamp x : Nat)    (old : stamp ≤ searches.length) :    purple t lo (searches ++ [searched]) stamp x =        max (purple t lo searches stamp x) (blue t lo searched x) ∧      blue t lo searched x ≤ purple t lo (searches ++ [searched]) stamp x ∧      purple t lo (searches ++ [searched]) searches.length x = blue t lo searched x :=  ⟨purple_step t lo searches searched stamp x old, purple_ge_blue t lo searches searched stamp x old,    purple_reset t lo searches searched x⟩

25.5.5.5 The example: values

In the example the current search is 44. The forest is {0,1},{2},{3},{4}\{0,1\}, \{2\}, \{3\}, \{4\}. The external keys are 00, 33 and 44. A dash marks a candidate that the item’s class does not use.

item class red green blue purple color value
00 left external — ∞\infty 33 — blue 33
11 internal 33 — — — red 33
22 left glancing 33 — — 22 purple 22
33 top corner — 55 22 — blue 22
44 top corner — ∞\infty 22 — blue 22

Item 11 is red: its neighbors 00 and 22 meet at the root, and the path from 11 to the root has three nodes. Item 22 has red value 33 as well, through neighbors 00 and 33. Since its access at time 44, it has been a one-leaf component, with blue value 22, so its purple value is 22, and purple wins. Item 00 has no external item to its left, so its green value is infinite, and it is blue: two nodes up to the root of {0,1}\{0,1\}, plus one. The item potentials add up to 1212, and with no virgin roots left, that is the whole potential.

25.5.6 The coloring

defGeometric.GreedyASS.Potential.colorpropext · Classical.choice · Quot.soundAlgorithms/GreedyASS/Potential/Definitions/Abstract.lean:64
/-- Snapshot coloring. Appending the current search while keeping the oldpoint set computes the paper's intermediate Phase I colors. Top-cornerstatus uses the latest row of that point set, not the search-history length. -/def color (t : Tree) (lo : Nat) (searches : List Nat) (P : Finset Point) (q : Point) : Option Color :=  if ¬ Active P q then none  else if External P q then    if LatestRow P q.2 then some .blue    else if green t lo P q.1 ≤ (currentBlue t lo searches q.1 : WithTop Nat) then some .green    else some .blue  else if Glancing P q then    if red t lo P q < (purple t lo searches q.2 q.1 : WithTop Nat) then some .red    else some .purple  else some .red

color t lo searches P q is none for a point that is not active. For an active point, it follows the classification:

  • External. A top corner, on the latest row of P, is blue. Any other external point is green if its green value is at most its current blue value, and blue otherwise. Ties go to green.
  • Glancing. Red if its red value is strictly less than its purple value, and purple otherwise. Ties go to purple.
  • Internal. Always red.

These are the paper’s rules, ties included.

defGeometric.GreedyASS.Potential.valuepropext · Classical.choice · Quot.soundAlgorithms/GreedyASS/Potential/Definitions/Abstract.lean:78
/-- The selected item potential, zero for inactive points. -/def value (t : Tree) (lo : Nat) (searches : List Nat) (P : Finset Point) (q : Point) : WithTop Nat :=  match color t lo searches P q with  | none => ((0 : Nat) : WithTop Nat)  | some .red => red t lo P q  | some .green => green t lo P q.1  | some .blue => (currentBlue t lo searches q.1 : Nat)  | some .purple => (purple t lo searches q.2 q.1 : Nat)

The value of a point is the candidate that its color selects. An inactive point has value 00. For glancing points, and for external points other than top corners, the rule amounts to taking the smaller of the two candidates:

theoremGeometric.GreedyASS.Potential.Results.internalpropext · Classical.choice · Quot.soundAlgorithms/GreedyASS/Potential/Abstract.lean:60
theorem internal (t : Tree) (lo : Nat) (searches : List Nat) {P : Finset Point} {q : Point}    (inside : Internal P q) :    color t lo searches P q = some .red ∧ value t lo searches P q = red t lo P q :=  internal_spec t lo searches inside
theoremGeometric.GreedyASS.Potential.Results.glancingpropext · Classical.choice · Quot.soundAlgorithms/GreedyASS/Potential/Abstract.lean:65
theorem glancing (t : Tree) (lo : Nat) (searches : List Nat) {P : Finset Point} {q : Point}    (glance : Glancing P q) :    color t lo searches P q =        (if red t lo P q < (purple t lo searches q.2 q.1 : WithTop Nat) then some .red else some .purple) ∧      value t lo searches P q = min (red t lo P q) (purple t lo searches q.2 q.1 : WithTop Nat) :=  glancing_spec t lo searches glance
theoremGeometric.GreedyASS.Potential.Results.externalpropext · Classical.choice · Quot.soundAlgorithms/GreedyASS/Potential/Abstract.lean:72
theorem external (t : Tree) (lo : Nat) (searches : List Nat) {P : Finset Point} {q : Point}    (outside : External P q) :    color t lo searches P q =        (if LatestRow P q.2 then some .blue         else if green t lo P q.1 ≤ (currentBlue t lo searches q.1 : WithTop Nat)           then some .green else some .blue) ∧      value t lo searches P q =        (if LatestRow P q.2 then (currentBlue t lo searches q.1 : WithTop Nat)         else min (green t lo P q.1) (currentBlue t lo searches q.1 : WithTop Nat)) :=  external_spec t lo searches outside
theoremGeometric.GreedyASS.Potential.Results.cornerpropext · Classical.choice · Quot.soundAlgorithms/GreedyASS/Potential/Abstract.lean:83
/-- Top corners remain blue when only the reference history changes. -/theorem corner (t : Tree) (lo : Nat) (searches : List Nat) {P : Finset Point} {q : Point}    (external : External P q) (latest : LatestRow P q.2) :    color t lo searches P q = some .blue ∧      value t lo searches P q = WithTop.some (currentBlue t lo searches q.1) :=  corner_spec t lo searches external latest
theoremGeometric.GreedyASS.Potential.Results.green_locationpropext · Classical.choice · Quot.soundAlgorithms/GreedyASS/Potential/Abstract.lean:90
theorem green_location (t : Tree) (lo : Nat) (searches : List Nat) {P : Finset Point} {q : Point}    (colored : color t lo searches P q = some .green) :    External P q ∧ ¬ LatestRow P q.2 ∧      green t lo P q.1 ≤ WithTop.some (currentBlue t lo searches q.1) ∧      value t lo searches P q = green t lo P q.1 :=  green_spec t lo searches colored

A green item is external, not a top corner, and its green value is at most its blue value. In particular, a green value that is used is finite. In fact every value is finite: nonexternal points have both neighbors, and external points can always fall back on blue.

Two independent arguments

color takes the request history searches and the point set P as separate arguments, and nothing in the definition ties them together. The potential of Section 25.6 uses the actual GreedyASS points of the history. The paper’s intermediate Phase I potential uses the old point set with the new search appended. Top-corner status always comes from the latest row of P. So in that intermediate snapshot, the corners of the old latest row are still top corners and stay blue, although the finger has moved: corner holds for any searches.

Untouched internal and glancing points keep their values when a row is added. The glancing case needs the point to be still glancing afterwards:

theoremGeometric.GreedyASS.Potential.Results.internal_stablepropext · Classical.choice · Quot.soundAlgorithms/GreedyASS/Potential/Abstract.lean:100
theorem internal_stable (t : Tree) (lo : Nat) (searches : List Nat)    {n time : Nat} {P : Finset Point} (keys : Finset Nat) (history : History n time P)    {q : Point} (inside : Internal P q) (untouched : q.1 ∉ keys) :    value t lo searches (P ∪ row time keys) q = value t lo searches P q :=  Potential.internal_unchanged t lo searches keys history inside untouched
theoremGeometric.GreedyASS.Potential.Results.glancing_stablepropext · Classical.choice · Quot.soundAlgorithms/GreedyASS/Potential/Abstract.lean:106
theorem glancing_stable (t : Tree) (lo : Nat) (searches : List Nat)    {n time : Nat} {P : Finset Point} (keys : Finset Nat) (history : History n time P)    {q : Point} (old : Glancing P q) (new : Glancing (P ∪ row time keys) q) :    color t lo searches (P ∪ row time keys) q = color t lo searches P q ∧      value t lo searches (P ∪ row time keys) q = value t lo searches P q :=  glancing_unchanged t lo searches keys history old new

Glancing items that become internal, and external items, can change value. Section 25.6 shows that, for untouched items, these changes are never increases (Lemmas 3.13 to 3.17).

25.5.7 Amounts and totals

The potential is a sum over keys, so the last definitions move from points to keys.

defGeometric.GreedyASS.Potential.itemColorpropext · Classical.choice · Quot.soundAlgorithms/GreedyASS/Potential/Definitions/Abstract.lean:87
/-- Item-level queries use the actual most recent access in each column.This also applies to intermediate colors computed from the old point set. -/def itemColor (t : Tree) (lo : Nat) (searches : List Nat) (P : Finset Point) (x : Nat) : Option Color :=  match latest P x with  | none => none  | some stamp => color t lo searches P (x, stamp)
defGeometric.GreedyASS.Potential.itemValuepropext · Classical.choice · Quot.soundAlgorithms/GreedyASS/Potential/Definitions/Abstract.lean:94
def itemValue (t : Tree) (lo : Nat) (searches : List Nat) (P : Finset Point) (x : Nat) : WithTop Nat :=  match latest P x with  | none => ((0 : Nat) : WithTop Nat)  | some stamp => value t lo searches P (x, stamp)

itemColor and itemValue evaluate color and value at the most recent access of the column. A key that was never touched has no color and value 00. This is also how the intermediate Phase I colors are computed, from the old point set.

defGeometric.GreedyASS.Potential.itemAmountpropext · Classical.choice · Quot.soundAlgorithms/GreedyASS/Potential/Definitions/Abstract.lean:99
/-- Natural-valued item potential. The selected value is always finite;the proof interface certifies that this conversion loses no information. -/def itemAmount (t : Tree) (lo : Nat) (searches : List Nat) (P : Finset Point) (x : Nat) : Nat :=  (itemValue t lo searches P x).untopD 0
defGeometric.GreedyASS.Potential.totalAmountpropext · Classical.choice · Quot.soundAlgorithms/GreedyASS/Potential/Definitions/Abstract.lean:104
def totalAmount (t : Tree) (lo : Nat) (searches : List Nat) (P : Finset Point)    (keys : Finset Nat) : Nat := ∑ x ∈ keys, itemAmount t lo searches P x

itemAmount converts the value to a natural number, with untopD 0. By finite no information is lost. totalAmount sums the amounts over a set of keys. The potential of Section 25.6, Amortized.potential, is totalAmount over the keys below lo + leaves t, at the GreedyASS points of the history, plus the virgin potential before at the last request. In the example it is 12+0=1212 + 0 = 12.

For counting, the keys of a row are sorted by their colors:

defGeometric.GreedyASS.Potential.coloredKeyspropext · Classical.choice · Quot.soundAlgorithms/GreedyASS/Potential/Definitions/Abstract.lean:107
def coloredKeys (t : Tree) (lo : Nat) (searches : List Nat) (P : Finset Point)    (keys : Finset Nat) (c : Color) : Finset Nat :=  keys.filter fun x => itemColor t lo searches P x = some c
defGeometric.GreedyASS.Potential.unseenKeyspropext · Classical.choice · Quot.soundAlgorithms/GreedyASS/Potential/Definitions/Abstract.lean:111
def unseenKeys (P : Finset Point) (keys : Finset Nat) : Finset Nat :=  keys.filter fun x => latest P x = none

coloredKeys … keys c is the set of keys in keys whose item has color c, and unseenKeys P keys is the set of keys never touched in P. Before the new row is added, the keys of the row are either unseen or have one of the four colors. After the row is added, every key of the row is active, so it has a color:

theoremGeometric.GreedyASS.Potential.Results.partitionpropext · Classical.choice · Quot.soundAlgorithms/GreedyASS/Potential/Abstract.lean:119
/-- Before adding the row, previously unseen keys have no old color. -/theorem partition (t : Tree) (lo : Nat) (searches : List Nat) (P : Finset Point) (keys : Finset Nat) :    keys.card = (unseenKeys P keys).card +      (coloredKeys t lo searches P keys .red).card + (coloredKeys t lo searches P keys .green).card +      (coloredKeys t lo searches P keys .blue).card + (coloredKeys t lo searches P keys .purple).card :=  Potential.partition t lo searches P keys
theoremGeometric.GreedyASS.Potential.Results.rowpropext · Classical.choice · Quot.soundAlgorithms/GreedyASS/Potential/Abstract.lean:126
theorem row (t : Tree) (lo : Nat) (searches : List Nat) {n time : Nat}    {P : Finset Point} (keys : Finset Nat) (history : History n time P) :    keys.card =      (coloredKeys t lo searches (P ∪ row time keys) keys .red).card +      (coloredKeys t lo searches (P ∪ row time keys) keys .green).card +      (coloredKeys t lo searches (P ∪ row time keys) keys .blue).card +      (coloredKeys t lo searches (P ∪ row time keys) keys .purple).card :=  row_partition t lo searches keys history

The unseen keys need an extra allowance in the library’s accounting: an unseen key has no old potential to pay for its first access. The library pays for first accesses with a separate, one-time budget: there are at most nn of them over the whole run.

defGeometric.GreedyASS.Potential.firstAccessespropext · Classical.choice · Quot.soundAlgorithms/GreedyASS/Potential/Definitions/Abstract.lean:114
/-- Count keys with no old color along the actual GreedyASS execution. -/def firstAccesses : Nat → Finset Point → List Nat → Nat  | _, _, [] => 0  | time, P, x :: xs =>      (unseenKeys P (greedySweep time x P)).card +        firstAccesses (time + 1) (P ∪ row time (greedySweep time x P)) xs

firstAccesses time P xs runs the GreedyASS sweep from snapshot P at time time on the requests xs, and counts, row by row, the keys that are touched for the first time.

theoremGeometric.GreedyASS.Potential.Results.first_accessespropext · Classical.choice · Quot.soundAlgorithms/GreedyASS/Potential/Abstract.lean:135
theorem first_accesses (time : Nat) (P : Finset Point) (xs : List Nat) :    (P.image Prod.fst).card + firstAccesses time P xs =      ((fill greedySweep time P xs).image Prod.fst).card := first_access_exact time P xs
theoremGeometric.GreedyASS.Potential.Results.startuppropext · Classical.choice · Quot.soundAlgorithms/GreedyASS/Potential/Abstract.lean:139
/-- Unseen keys in intermediate row partitions require only a one-timeuniverse-size allowance along the actual executable sweep. -/theorem startup {n time : Nat} {P : Finset Point} (xs : List Nat)    (history : History n time P) (satisfied : ASS P) (valid : ∀ x ∈ xs, x < n) :    (P.image Prod.fst).card + firstAccesses time P xs ≤ n :=  first_access_bound xs history satisfied valid

first_accesses is exact: the number of columns touched at the start, plus the first accesses, is the number of columns touched at the end. startup bounds this by nn when the history is arborally satisfied and it and all requests use keys below nn. This one-time charge is part of the term linear in the number of reference leaves in Theorem 3.1 (Section 25.6).