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 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 . 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 .
- Virgin potential. Each component of the reference forest (Section 25.4) that has not been touched since it became a root carries 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.
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).
Let be a leaf-oriented reference tree whose leaves are the keys , and run GreedyASS from the empty point set on a sequence of requests from that range. Before the row for the current request is added, call a component of the reference forest of 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 . Call the other components virgin. Then the row GreedyASS adds for touches at most two old components.
Give each virgin component units of potential. If the row touches components in all, at least of them are virgin, so the virgin potential drops from to some with
These are the paper’s Lemmas 3.7 and 3.8. The second is stated additively, so that it is meaningful when .
The Lean statements
/-- 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 validModels.Rotation, Models.Geometric.Definitions, Models.Geometric.Online, Algorithms.GreedyASS.Algorithm, Algorithms.GreedyASS.Reference.Definitions, Algorithms.GreedyASS.Reference.Distance.Definitions, Algorithms.GreedyASS.Reference.Transitions.Definitions, Algorithms.GreedyASS.Virgin.Definitions/-- 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 validModels.Rotation, Models.Geometric.Definitions, Models.Geometric.Online, Algorithms.GreedyASS.Algorithm, Algorithms.GreedyASS.Reference.Definitions, Algorithms.GreedyASS.Reference.Distance.Definitions, Algorithms.GreedyASS.Reference.Transitions.Definitions, Algorithms.GreedyASS.Virgin.DefinitionsThe 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 , its most recent access is the highest point in column . 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 . All distances count nodes, both endpoints included.
| color | value | used for |
|---|---|---|
| red | distance from to the lowest common ancestor of its two neighbors | internal and glancing items |
| green | one more than the distance from to the lowest common ancestor of the nearest external items on either side | external items |
| blue | one more than the distance from to the root of its component in the current reference forest | external items |
| purple | the largest blue value of 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 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
def greatest (s : Finset Nat) : Option Nat := if h : s.Nonempty then some (s.max' h) else nonedef least (s : Finset Nat) : Option Nat := if h : s.Nonempty then some (s.min' h) else nonegreatest 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.
/-- 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/-- 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 or at the initial tree.
def Active (P : Finset Point) (q : Point) : Prop := q ∈ P ∧ ∀ p ∈ P, p.1 = q.1 → p.2 ≤ q.2A 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:
theorem most_recent (P : Finset Point) (key stamp : Nat) : latest P key = some stamp ↔ Active P (key, stamp) := latest_spec P key stamp25.5.2.2 Neighbors on the access row
/-- 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)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 and , taken at the item’s most recent access. Their characterizations are what one expects:
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 keytheorem 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 key25.5.2.3 External, glancing and internal
def LeftExternal (P : Finset Point) (q : Point) : Prop := Active P q ∧ ∀ p ∈ P, p.2 = q.2 → q.1 ≤ p.1def RightExternal (P : Finset Point) (q : Point) : Prop := Active P q ∧ ∀ p ∈ P, p.2 = q.2 → p.1 ≤ q.1def External (P : Finset Point) (q : Point) : Prop := LeftExternal P q ∨ RightExternal P qAn 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.
/-- 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:
/-- 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 rightEvery left-external key is at most every right-external key. Otherwise one of the two points would lie in the other’s empty quadrant.
def LeftGlancing (P : Finset Point) (q : Point) : Prop := Active P q ∧ ¬ External P q ∧ ∀ p ∈ P, q.2 < p.2 → q.1 < p.1def RightGlancing (P : Finset Point) (q : Point) : Prop := Active P q ∧ ¬ External P q ∧ ∀ p ∈ P, q.2 < p.2 → p.1 < q.1def Glancing (P : Finset Point) (q : Point) : Prop := LeftGlancing P q ∨ RightGlancing P qA 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.
def Internal (P : Finset Point) (q : Point) : Prop := Active P q ∧ ¬ External P q ∧ ¬ Glancing P qAn 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.)
instance (P : Finset Point) (q : Point) : Decidable (Active P q) := inferInstanceAs (Decidable (_ ∧ ∀ p ∈ P, _ → _))instance (P : Finset Point) (q : Point) : Decidable (LeftExternal P q) := inferInstanceAs (Decidable (Active P q ∧ ∀ p ∈ P, _ → _))instance (P : Finset Point) (q : Point) : Decidable (RightExternal P q) := inferInstanceAs (Decidable (Active P q ∧ ∀ p ∈ P, _ → _))instance (P : Finset Point) (q : Point) : Decidable (External P q) := inferInstanceAs (Decidable (LeftExternal P q ∨ RightExternal P q))instance (P : Finset Point) (q : Point) : Decidable (LeftGlancing P q) := inferInstanceAs (Decidable (Active P q ∧ ¬ External P q ∧ ∀ p ∈ P, _ → _))instance (P : Finset Point) (q : Point) : Decidable (RightGlancing P q) := inferInstanceAs (Decidable (Active P q ∧ ¬ External P q ∧ ∀ p ∈ P, _ → _))instance (P : Finset Point) (q : Point) : Decidable (Glancing P q) := inferInstanceAs (Decidable (LeftGlancing P q ∨ RightGlancing P q))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.
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 activeA nonexternal point has both neighbors, and the neighbors are missing exactly on the external sides:
theorem missing_neighbors {P : Finset Point} {q : Point} (active : Active P q) : (leftNeighbor P q = none ↔ LeftExternal P q) ∧ (rightNeighbor P q = none ↔ RightExternal P q) := ⟨left_none active, right_none active⟩theorem neighbors {P : Finset Point} {q : Point} (active : Active P q) (notExternal : ¬ External P q) : ∃ l r, leftNeighbor P q = some l ∧ rightNeighbor P q = some r := neighbors_exist active notExternal25.5.2.4 An example
Run GreedyASS on the five keys with requests . The sweep of Section 25.1 adds 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 , , , and .
- is the leftmost point of row , so it is left external.
- has neighbors and on row . Later, lies to its left and to its right, so it is internal.
- has neighbors and on row . The only later points, and , are to its right, so it is left glancing.
- and are the left-external and right-external points of the latest row.
The left-external staircase is . It rises from left to right, and its upper-left quadrants are empty. The right-external staircase is just .
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.
/-- 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 ≤ stampinstance (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.
/-- 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.
/-- 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 nonemptytheorem 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 historyEvery 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:
/-- 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 memberFinally, 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:
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.
/-- 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:
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⟩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 untouchedThe keys of the new row itself have nothing above them, so none of them is internal:
/-- 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 member25.5.3 The barrier
Lemma 3.4 of the paper is a geometric fact about GreedyASS alone. Suppose key is accessed at time , and every request after time is at least . Then GreedyASS never accesses a key smaller than after time . The point is a barrier: every old point to its left spans, with any later request at or to its right, a rectangle containing , so no such old point is visible again.
The library first isolates the invariant that makes this work:
/-- 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/-- 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 ≤ stampBarrier.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:
/-- 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 sidetheorem 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/-- 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 sidetheorem 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 sideleft_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.
/-- 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 sidetheorem 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 sideThe paper assumes the later requests are strictly greater than ; the library allows them to equal . 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:
/-- 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 , Reference.forest lists the components hanging off the path to , together with 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 units. When a row touches a virgin root, the root stops being virgin, and its 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 :
/-- The actual points produced by GreedyASS from the empty history. -/def points (history : List Nat) : Finset Point := fill greedySweep 0 ∅ historydef 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.
/-- 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 xNonVirgin 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
firsttouched a key ofc; and cwas a root of the reference forest at the search forfirst, at every later search in the history, and at the currentrequest.
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 is -virgin if none of its keys was accessed at times to . The paper’s count includes the current row . The library’s NonVirgin stops just before it, and update below adds the current row.
/-- 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 xnoncomputable 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)/-- 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 ctouched is the set of roots of the current forest that contain a key of the new row; the paper’s 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.
/-- 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).cardnoncomputable def after (t : Tree) (lo : Nat) (history : List Nat) (request : Nat) : Nat := 12 * (roots t lo history request \ touched t lo history request).cardbefore is the virgin potential before the new row, per virgin root. after is the virgin potential after it: only the virgin roots the row does not touch keep their units.
25.5.4.1 How virgin status evolves
Nothing is old before the first request:
theorem initial (t : Tree) (lo request : Nat) (c : Component) : ¬ NonVirgin t lo [] request c := initially_virgin t lo request cModels.Rotation, Models.Geometric.Definitions, Models.Geometric.Online, Algorithms.GreedyASS.Algorithm, Algorithms.GreedyASS.Reference.Definitions, Algorithms.GreedyASS.Reference.Distance.Definitions, Algorithms.GreedyASS.Reference.Transitions.Definitions, Algorithms.GreedyASS.Virgin.DefinitionsTheorem (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.
/-- 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 cModels.Rotation, Models.Geometric.Definitions, Models.Geometric.Online, Algorithms.GreedyASS.Algorithm, Algorithms.GreedyASS.Reference.Definitions, Algorithms.GreedyASS.Reference.Distance.Definitions, Algorithms.GreedyASS.Reference.Transitions.Definitions, Algorithms.GreedyASS.Virgin.DefinitionsThe “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:
/-- 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 requestModels.Rotation, Models.Geometric.Definitions, Models.Geometric.Online, Algorithms.GreedyASS.Algorithm, Algorithms.GreedyASS.Reference.Definitions, Algorithms.GreedyASS.Reference.Distance.Definitions, Algorithms.GreedyASS.Reference.Transitions.Definitions, Algorithms.GreedyASS.Virgin.Definitions/-- 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 newRootModels.Rotation, Models.Geometric.Definitions, Models.Geometric.Online, Algorithms.GreedyASS.Algorithm, Algorithms.GreedyASS.Reference.Definitions, Algorithms.GreedyASS.Reference.Distance.Definitions, Algorithms.GreedyASS.Reference.Transitions.Definitions, Algorithms.GreedyASS.Virgin.Definitions25.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:
/-- 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 oldModels.Rotation, Models.Geometric.Definitions, Models.Geometric.Online, Algorithms.GreedyASS.Algorithm, Algorithms.GreedyASS.Reference.Definitions, Algorithms.GreedyASS.Reference.Distance.Definitions, Algorithms.GreedyASS.Reference.Transitions.Definitions, Algorithms.GreedyASS.Virgin.DefinitionsThis 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.
/-- 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 validModels.Rotation, Models.Geometric.Definitions, Models.Geometric.Online, Algorithms.GreedyASS.Algorithm, Algorithms.GreedyASS.Reference.Definitions, Algorithms.GreedyASS.Reference.Distance.Definitions, Algorithms.GreedyASS.Reference.Transitions.Definitions, Algorithms.GreedyASS.Virgin.DefinitionsThe count includes the searched one-leaf component, which may be old too. Every touched virgin root gives up its units, which is an exact identity:
theorem exact (t : Tree) (lo : Nat) (history : List Nat) (request : Nat) : before t lo history request = after t lo history request + 12 * (roots t lo history request ∩ touched t lo history request).card := exact_drop t lo history requestModels.Rotation, Models.Geometric.Definitions, Models.Geometric.Online, Algorithms.GreedyASS.Algorithm, Algorithms.GreedyASS.Reference.Definitions, Algorithms.GreedyASS.Reference.Distance.Definitions, Algorithms.GreedyASS.Reference.Transitions.Definitions, Algorithms.GreedyASS.Virgin.DefinitionsCombined with the count, this gives the paper’s Lemma 3.8, which says that at least 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, .
/-- 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 validModels.Rotation, Models.Geometric.Definitions, Models.Geometric.Online, Algorithms.GreedyASS.Algorithm, Algorithms.GreedyASS.Reference.Definitions, Algorithms.GreedyASS.Reference.Distance.Definitions, Algorithms.GreedyASS.Reference.Transitions.Definitions, Algorithms.GreedyASS.Virgin.Definitions25.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 per root created. Old roots that survive keep their status, by survival.
/-- 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 traceModels.Rotation, Models.Geometric.Definitions, Models.Geometric.Online, Algorithms.GreedyASS.Algorithm, Algorithms.GreedyASS.Reference.Definitions, Algorithms.GreedyASS.Reference.Distance.Definitions, Algorithms.GreedyASS.Reference.Transitions.Definitions, Algorithms.GreedyASS.Virgin.Definitions/-- 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 newModels.Rotation, Models.Geometric.Definitions, Models.Geometric.Online, Algorithms.GreedyASS.Algorithm, Algorithms.GreedyASS.Reference.Definitions, Algorithms.GreedyASS.Reference.Distance.Definitions, Algorithms.GreedyASS.Reference.Transitions.Definitions, Algorithms.GreedyASS.Virgin.DefinitionsThe 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 . 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 units for each merge or split, item and virgin potential together. The virgin part uses at most for a split and for a merge, so it leaves units per merge for the item potentials:
/-- 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 newModels.Rotation, Models.Geometric.Definitions, Models.Geometric.Online, Algorithms.GreedyASS.Algorithm, Algorithms.GreedyASS.Reference.Definitions, Algorithms.GreedyASS.Reference.Distance.Definitions, Algorithms.GreedyASS.Reference.Transitions.Definitions, Algorithms.GreedyASS.Virgin.DefinitionsHere 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 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 = node (node nil nil) (node nil (node nil nil)), with leaves :
•
/ \
• •
/ \ / \
0 1 2 •
/ \
3 4
The searches for and for give the same forest: the subtree on and the single leaves , , . Just before the last row, the old roots are , and , which the row at time touched. Only is virgin, so the virgin potential is . The last row touches two roots, one old and one virgin. Afterwards no root is virgin, so the virgin potential is . Lemma 3.8 reads .
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 up to the lowest common ancestor of leaves and , and Distance.toRoot c x is the number of nodes from up to the root of a component . A value that the paper calls infinite is ⊤ in WithTop Nat.
25.5.5.1 Red: the access row’s neighbors
/-- 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 , where counts nodes. The value only makes sense when the neighbors surround q.1. For nonexternal points they do, and red is finite:
theorem red_defined (t : Tree) (lo : Nat) {P : Finset Point} {q : Point} (active : Active P q) (nonexternal : ¬ External P q) : ∃ a b, leftNeighbor P q = some a ∧ rightNeighbor P q = some b ∧ a < q.1 ∧ q.1 < b ∧ red t lo P q = (Distance.toLCA t lo q.1 a b : Nat) := red_finite t lo active nonexternalModels.Rotation, Models.Geometric.Definitions, Models.Geometric.Online, Models.Geometric.Offline, Algorithms.GreedyASS.Algorithm, Algorithms.GreedyASS.Items.Definitions, Algorithms.GreedyASS.Reference.Definitions, Algorithms.GreedyASS.Reference.Distance.Definitions, Algorithms.GreedyASS.Potential.DefinitionsA 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:
/-- 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⟩Models.Rotation, Models.Geometric.Definitions, Models.Geometric.Online, Models.Geometric.Offline, Algorithms.GreedyASS.Algorithm, Algorithms.GreedyASS.Items.Definitions, Algorithms.GreedyASS.Reference.Definitions, Algorithms.GreedyASS.Reference.Distance.Definitions, Algorithms.GreedyASS.Potential.Definitions25.5.5.2 Green: the nearest external items
/-- 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.fstdef leftExternal (P : Finset Point) (x : Nat) : Option Nat := greatest ((externalKeys P).filter fun y => y < x)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 and :
theorem externals (P : Finset Point) (x : Nat) : x ∈ externalKeys P ↔ ∃ stamp, External P (x, stamp) := mem_external P xModels.Rotation, Models.Geometric.Definitions, Models.Geometric.Online, Models.Geometric.Offline, Algorithms.GreedyASS.Algorithm, Algorithms.GreedyASS.Items.Definitions, Algorithms.GreedyASS.Reference.Definitions, Algorithms.GreedyASS.Reference.Distance.Definitions, Algorithms.GreedyASS.Potential.Definitionstheorem 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 aModels.Rotation, Models.Geometric.Definitions, Models.Geometric.Online, Models.Geometric.Offline, Algorithms.GreedyASS.Algorithm, Algorithms.GreedyASS.Items.Definitions, Algorithms.GreedyASS.Reference.Definitions, Algorithms.GreedyASS.Reference.Distance.Definitions, Algorithms.GreedyASS.Potential.Definitionstheorem 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 bModels.Rotation, Models.Geometric.Definitions, Models.Geometric.Online, Models.Geometric.Offline, Algorithms.GreedyASS.Algorithm, Algorithms.GreedyASS.Items.Definitions, Algorithms.GreedyASS.Reference.Definitions, Algorithms.GreedyASS.Reference.Distance.Definitions, Algorithms.GreedyASS.Potential.Definitions/-- 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:
theorem green_infinite (t : Tree) (lo : Nat) (P : Finset Point) (x : Nat) (missing : leftExternal P x = none ∨ rightExternal P x = none) : green t lo P x = ⊤ := green_missing t lo P x missingModels.Rotation, Models.Geometric.Definitions, Models.Geometric.Online, Models.Geometric.Offline, Algorithms.GreedyASS.Algorithm, Algorithms.GreedyASS.Items.Definitions, Algorithms.GreedyASS.Reference.Definitions, Algorithms.GreedyASS.Reference.Distance.Definitions, Algorithms.GreedyASS.Potential.Definitions25.5.5.3 Blue: the current forest
/-- 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 xblue 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 . If the two paths never separate, x is the searched leaf, and the value is . In terms of Section 25.4, blue is the paper’s , with the root of x’s component:
theorem blue_root (t : Tree) (lo searched x : Nat) {c : Component} (member : c ∈ forest t lo searched) (inside : c.Contains x) : 2 ≤ blue t lo searched x ∧ blue t lo searched x = Distance.toRoot c x + 1 := ⟨blue_ge_two t lo searched x, blue_component t lo searched x member inside⟩Models.Rotation, Models.Geometric.Definitions, Models.Geometric.Online, Models.Geometric.Offline, Algorithms.GreedyASS.Algorithm, Algorithms.GreedyASS.Items.Definitions, Algorithms.GreedyASS.Reference.Definitions, Algorithms.GreedyASS.Reference.Distance.Definitions, Algorithms.GreedyASS.Potential.DefinitionsThe blue value is always at least . 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.
/-- 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 xcurrentBlue 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 . The list searches is the request history. Its entry with index is the request that produced the row at time .
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:
/-- 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 validModels.Rotation, Models.Geometric.Definitions, Models.Geometric.Online, Models.Geometric.Offline, Algorithms.GreedyASS.Algorithm, Algorithms.GreedyASS.Items.Definitions, Algorithms.GreedyASS.Reference.Definitions, Algorithms.GreedyASS.Reference.Distance.Definitions, Algorithms.GreedyASS.Potential.Definitions25.5.5.4 Purple: the largest blue since the last access
/-- 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 xpurple 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 . The maximum is a Finset.sup, so it is 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:
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⟩Models.Rotation, Models.Geometric.Definitions, Models.Geometric.Online, Models.Geometric.Offline, Algorithms.GreedyASS.Algorithm, Algorithms.GreedyASS.Items.Definitions, Algorithms.GreedyASS.Reference.Definitions, Algorithms.GreedyASS.Reference.Distance.Definitions, Algorithms.GreedyASS.Potential.Definitions25.5.5.5 The example: values
In the example the current search is . The forest is . The external keys are , and . A dash marks a candidate that the item’s class does not use.
| item | class | red | green | blue | purple | color | value |
|---|---|---|---|---|---|---|---|
| left external | — | — | blue | ||||
| internal | — | — | — | red | |||
| left glancing | — | — | purple | ||||
| top corner | — | — | blue | ||||
| top corner | — | — | blue |
Item is red: its neighbors and meet at the root, and the path from to the root has three nodes. Item has red value as well, through neighbors and . Since its access at time , it has been a one-leaf component, with blue value , so its purple value is , and purple wins. Item has no external item to its left, so its green value is infinite, and it is blue: two nodes up to the root of , plus one. The item potentials add up to , and with no virgin roots left, that is the whole potential.
25.5.6 The coloring
inductive Color where | red | green | blue | purple deriving DecidableEq, Repr/-- 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 .redcolor 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.
/-- 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 . For glancing points, and for external points other than top corners, the rule amounts to taking the smaller of the two candidates:
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 insideModels.Rotation, Models.Geometric.Definitions, Models.Geometric.Online, Models.Geometric.Offline, Algorithms.GreedyASS.Algorithm, Algorithms.GreedyASS.Items.Definitions, Algorithms.GreedyASS.Reference.Definitions, Algorithms.GreedyASS.Reference.Distance.Definitions, Algorithms.GreedyASS.Potential.Definitionstheorem 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 glanceModels.Rotation, Models.Geometric.Definitions, Models.Geometric.Online, Models.Geometric.Offline, Algorithms.GreedyASS.Algorithm, Algorithms.GreedyASS.Items.Definitions, Algorithms.GreedyASS.Reference.Definitions, Algorithms.GreedyASS.Reference.Distance.Definitions, Algorithms.GreedyASS.Potential.Definitionstheorem 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 outsideModels.Rotation, Models.Geometric.Definitions, Models.Geometric.Online, Models.Geometric.Offline, Algorithms.GreedyASS.Algorithm, Algorithms.GreedyASS.Items.Definitions, Algorithms.GreedyASS.Reference.Definitions, Algorithms.GreedyASS.Reference.Distance.Definitions, Algorithms.GreedyASS.Potential.Definitions/-- 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 latestModels.Rotation, Models.Geometric.Definitions, Models.Geometric.Online, Models.Geometric.Offline, Algorithms.GreedyASS.Algorithm, Algorithms.GreedyASS.Items.Definitions, Algorithms.GreedyASS.Reference.Definitions, Algorithms.GreedyASS.Reference.Distance.Definitions, Algorithms.GreedyASS.Potential.Definitionstheorem 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 coloredModels.Rotation, Models.Geometric.Definitions, Models.Geometric.Online, Models.Geometric.Offline, Algorithms.GreedyASS.Algorithm, Algorithms.GreedyASS.Items.Definitions, Algorithms.GreedyASS.Reference.Definitions, Algorithms.GreedyASS.Reference.Distance.Definitions, Algorithms.GreedyASS.Potential.DefinitionsA 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.
theorem finite (t : Tree) (lo : Nat) (searches : List Nat) (P : Finset Point) (q : Point) : ∃ v : Nat, value t lo searches P q = v := Potential.finite t lo searches P qModels.Rotation, Models.Geometric.Definitions, Models.Geometric.Online, Models.Geometric.Offline, Algorithms.GreedyASS.Algorithm, Algorithms.GreedyASS.Items.Definitions, Algorithms.GreedyASS.Reference.Definitions, Algorithms.GreedyASS.Reference.Distance.Definitions, Algorithms.GreedyASS.Potential.Definitionscolor 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:
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 untouchedModels.Rotation, Models.Geometric.Definitions, Models.Geometric.Online, Models.Geometric.Offline, Algorithms.GreedyASS.Algorithm, Algorithms.GreedyASS.Items.Definitions, Algorithms.GreedyASS.Reference.Definitions, Algorithms.GreedyASS.Reference.Distance.Definitions, Algorithms.GreedyASS.Potential.Definitionstheorem 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 newModels.Rotation, Models.Geometric.Definitions, Models.Geometric.Online, Models.Geometric.Offline, Algorithms.GreedyASS.Algorithm, Algorithms.GreedyASS.Items.Definitions, Algorithms.GreedyASS.Reference.Definitions, Algorithms.GreedyASS.Reference.Distance.Definitions, Algorithms.GreedyASS.Potential.DefinitionsGlancing 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.
/-- 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)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 . This is also how the intermediate Phase I colors are computed, from the old point set.
theorem item (t : Tree) (lo : Nat) (searches : List Nat) {P : Finset Point} {x stamp : Nat} (active : Active P (x, stamp)) : itemColor t lo searches P x = color t lo searches P (x, stamp) ∧ itemValue t lo searches P x = value t lo searches P (x, stamp) := item_spec t lo searches activeModels.Rotation, Models.Geometric.Definitions, Models.Geometric.Online, Models.Geometric.Offline, Algorithms.GreedyASS.Algorithm, Algorithms.GreedyASS.Items.Definitions, Algorithms.GreedyASS.Reference.Definitions, Algorithms.GreedyASS.Reference.Distance.Definitions, Algorithms.GreedyASS.Potential.Definitions/-- 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 0def totalAmount (t : Tree) (lo : Nat) (searches : List Nat) (P : Finset Point) (keys : Finset Nat) : Nat := ∑ x ∈ keys, itemAmount t lo searches P xitemAmount 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 .
For counting, the keys of a row are sorted by their colors:
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 cdef unseenKeys (P : Finset Point) (keys : Finset Nat) : Finset Nat := keys.filter fun x => latest P x = nonecoloredKeys … 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:
/-- 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 keysModels.Rotation, Models.Geometric.Definitions, Models.Geometric.Online, Models.Geometric.Offline, Algorithms.GreedyASS.Algorithm, Algorithms.GreedyASS.Items.Definitions, Algorithms.GreedyASS.Reference.Definitions, Algorithms.GreedyASS.Reference.Distance.Definitions, Algorithms.GreedyASS.Potential.Definitionstheorem 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 historyModels.Rotation, Models.Geometric.Definitions, Models.Geometric.Online, Models.Geometric.Offline, Algorithms.GreedyASS.Algorithm, Algorithms.GreedyASS.Items.Definitions, Algorithms.GreedyASS.Reference.Definitions, Algorithms.GreedyASS.Reference.Distance.Definitions, Algorithms.GreedyASS.Potential.DefinitionsThe 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 of them over the whole run.
/-- 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)) xsfirstAccesses 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.
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 xsModels.Rotation, Models.Geometric.Definitions, Models.Geometric.Online, Models.Geometric.Offline, Algorithms.GreedyASS.Algorithm, Algorithms.GreedyASS.Items.Definitions, Algorithms.GreedyASS.Reference.Definitions, Algorithms.GreedyASS.Reference.Distance.Definitions, Algorithms.GreedyASS.Potential.Definitions/-- 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 validModels.Rotation, Models.Geometric.Definitions, Models.Geometric.Online, Models.Geometric.Offline, Algorithms.GreedyASS.Algorithm, Algorithms.GreedyASS.Items.Definitions, Algorithms.GreedyASS.Reference.Definitions, Algorithms.GreedyASS.Reference.Distance.Definitions, Algorithms.GreedyASS.Potential.Definitionsfirst_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 when the history is arborally satisfied and it and all requests use keys below . This one-time charge is part of the term linear in the number of reference leaves in Theorem 3.1 (Section 25.6).