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

Chapter 35 · Simulating fingers · Section 35.2Finger hands

The multi-finger model of Chapter 10 keeps several persistent fingers in one BST. Each instruction moves one finger to a neighbouring node or rotates at it, and every other finger stays on its key. The pointer machine of Chapter 7 has a single cursor, which starts at the root for every request. A finger hand lets the single cursor simulate the fingers. It shapes the BST so that every finger is a constant number of steps from the root, for a fixed number of fingers, and it maintains that shape as the fingers move and rotate.

The library uses the hand with a single finger. Theorem 6 of the lazy finger paper (Bose et al. 2016) compares dynamic BSTs with static trees searched by a lazy finger. Its formal counterpart, Results.optimal in Chapter 16, says that from any initial tree on nn keys, some pointer-machine execution serves the whole request sequence at cost at most 16n+464 OPT16n + 464\,\mathrm{OPT}, where OPT\mathrm{OPT} is the cost of the best static tree searched with a lazy finger. The execution is an offline witness: it is chosen knowing the sequence. The proof runs the lazy finger as a one-finger execution of the multi-finger model, and simulates that execution with a hand. The central proof-file theorem is Hand.one_finger_execution (in Structures/FingerHand/Packed/Simulation/Execution.lean):

Let tt be a nonempty tree, and suppose a multi-finger execution with kk fingers, all starting at the root of tt, serves a request sequence at cost CC. Then the pointer machine serves the same sequence from the same tree tt, at cost at most (344+60(k+1)) C(344 + 60(k+1))\,C.

Here CC is the multi-finger cost: one unit per request plus one per instruction. With k=1k = 1 the factor is 464464, the constant of Results.same_tree. The overhead grows linearly with the number of fingers. That is enough for the lazy finger, but it is weaker than an overhead independent of kk.

Do you need this section?

No theorem statement of the reading interface mentions a hand, and no headline result has these definitions on its reading path. They are shown so that the proof of Results.optimal can be followed in the source. A reader who only wants to check Theorem 6 can stop at its statement in Chapter 16. The building blocks, BST deques and tendons, are in Section 35.1. Everything here lives in Structures/FingerHand. The theorems quoted by name in plain code are internal to the library.

35.2.1 Overview

Three trees are involved:

  • The reference tree is the tree of the multi-finger execution, Multifinger.State.tree. Its fingers are inorder ranks.
  • The skeleton is a small labelled tree of reference keys: the fingers, the reference root, and the branching vertices between them, with every other vertex contracted away. It has at most 2(k+1)2(k+1) vertices.
  • The physical tree is the tree that the pointer machine actually holds. It has the same keys in the same inorder as the reference tree, but a different shape.

The physical tree is built from the skeleton. Each skeleton vertex, a pseudofinger, is a real node. A skeleton edge stands for a reference path, possibly very long, and that path is stored as a tendon, which puts the lower endpoint at physical depth at most four. The subtrees that contain no skeleton vertex, the knuckles, are kept exactly as they are in the reference tree. A pseudofinger is therefore at physical depth less than 55 times the number of skeleton vertices, and the cursor reaches any finger and returns to the root in fewer than 20(k+1)20(k+1) instructions.

A multi-finger instruction changes the skeleton by a bounded amount. A move adds the destination to the skeleton and may make the source obsolete. A rotation acts on two skeleton vertices. The simulation of one instruction has three phases:

  1. Exposure. Make the destination a pseudofinger, at most one new vertex. If it was inside a tendon, take it out with a tendon operation. If it was the root of a knuckle, split the knuckle.
  2. The change itself. A move needs nothing more. A rotation also exposes the parent and rotates the two pseudofingers, a constant number of physical instructions, since both are exposed.
  3. Suppression. Remove the vertex that is no longer a finger and no longer branches, by pushing it into a neighbouring tendon or merging it into a knuckle.

The tendon potentials pay for the amortized deque operations. Each phase costs a constant plus the travel through the hand, which is O(k)O(k).

35.2.2 The compressed skeleton

The skeleton is computed from a set of marks, the reference ranks to keep:

defFingerHand.compresspropext · Classical.choice · Quot.soundStructures/FingerHand/Definitions/Abstract.lean:11
/-- Compress all unmarked unary vertices of the rooted Steiner tree. Markedvertices and branching vertices remain. Insert the original root into `marks`when a root-anchored hand is required. -/def compress (marks : Finset Nat) : Tree → Nat → LabelledTree Nat  | .nil, _ => .nil  | .node l r, lo =>      let left := compress marks l lo      let right := compress marks r (lo + l.size + 1)      if lo + l.size ∈ marks then .node left (lo + l.size) right      else match left, right with        | .nil, _ => right        | _, .nil => left        | _, _ => .node left (lo + l.size) right

compress marks t lo reads t with inorder ranks starting at lo. A vertex is kept if it is marked, or if both of its compressed subtrees are nonempty. An unmarked vertex with marks on only one side is replaced by that side’s compressed subtree, and a subtree without marks disappears. The result is the subtree of t spanned by the marks, with its unmarked unary vertices contracted. It keeps the inorder order and the ancestor relation of the surviving keys (compress_sublist, marked_mem), and its labels are global ranks. The vertex above the topmost branching point is not kept unless it is marked, which is why the root is added as a mark below.

defFingerHand.markedCountpropext · Classical.choice · Quot.soundStructures/FingerHand/Definitions/Abstract.lean:25
/-- Number of marked keys actually present in the reference interval. -/def markedCount (marks : Finset Nat) : Tree → Nat → Nat  | .nil, _ => 0  | .node l r, lo =>      markedCount marks l lo + (if lo + l.size ∈ marks then 1 else 0) +        markedCount marks r (lo + l.size + 1)

markedCount marks t lo counts the marks that are actually ranks of t. Marks outside the interval do not count. The library proves that a nonempty compressed skeleton has fewer than 2⋅2\cdotmarkedCount vertices, the usual bound for a tree whose leaves and unary vertices are all marked (compress_size_invariant).

defFingerHand.rootRankno axiomsStructures/FingerHand/Definitions/Abstract.lean:32
/-- The original root rank. The empty-tree value is only a totalization. -/def rootRank : Tree → Nat  | .nil => 0  | .node l _ => l.size
defFingerHand.stateMarkspropext · Classical.choice · Quot.soundStructures/FingerHand/Definitions/Abstract.lean:37
/-- Actual finger keys, with the original root included as an anchor. Duplicatefingers create a single mark. -/def stateMarks {k : Nat} (s : Multifinger.State k) : Finset Nat :=  insert (rootRank s.tree) (Finset.univ.image s.position)
defFingerHand.ofStatepropext · Classical.choice · Quot.soundStructures/FingerHand/Definitions/Abstract.lean:42
def ofState {k : Nat} (s : Multifinger.State k) : LabelledTree Nat := compress (stateMarks s) s.tree 0

rootRank t is the rank of the root, l.size. On the empty tree it is 00, a totalized value. The marks of a multi-finger state are the finger positions together with the root, so fingers on the same key contribute a single mark. ofState s is the skeleton of the state. It has at most 2(k+1)2(k+1) vertices, whatever the size of the tree (ofState_size), and it contains every finger of a valid state (ofState_finger). Including the root as a permanent anchor means that the cursor, which starts at the root, enters the skeleton at its top.

defFingerHand.lowerPathno axiomsStructures/FingerHand/Definitions/Abstract.lean:44
/-- The keys below the endpoint, in their order on its root-to-key path. -/def lowerPath (t : Tree) (lo x : Nat) : List Nat := (t.searchPath lo x).filter (· < x)
defFingerHand.upperPathno axiomsStructures/FingerHand/Definitions/Abstract.lean:47
/-- The keys above the endpoint, in their order on its root-to-key path. -/def upperPath (t : Tree) (lo x : Nat) : List Nat := (t.searchPath lo x).filter (x < ·)

lowerPath t lo x lists the keys smaller than x on the search path to x (Tree.searchPath), from the root down, and upperPath the larger ones. These are the two monotone sequences that a tendon stores, described by keys. The proofs work with the zipper versions Tendon.lowerFringe and Tendon.upperFringe instead, and no theorem currently uses lowerPath or upperPath.

35.2.3 Packed hands

The physical structure mirrors the skeleton:

inductiveFingerHand.Handno axiomsStructures/FingerHand/Packed/Definitions/Abstract.lean:9
/-- A whole reference tree represented by pseudofinger vertices, incomingcompressed tendons, and untouched knuckle subtrees. Every `node` is onepseudofinger; its incoming path may be empty. -/inductive Hand where  | knuckle (tree : Tree)  | node (path : List Pointer.Frame) (tendon : Tendon.State) (left right : Hand)

A hand is a knuckle, an untouched reference subtree stored as is, or a node, one pseudofinger. A node records path, the reference path from the pseudofinger up to its skeleton parent, as zipper frames, innermost first. That path consists of the contracted vertices, each with its hanging subtree. The node also records tendon, the deque representation of that path, and its two child hands. The path may be empty, when the skeleton parent is the reference parent.

defFingerHand.Hand.countno axiomsStructures/FingerHand/Packed/Definitions/Abstract.lean:18
def count : Hand → Nat  | .knuckle _ => 0  | .node _ _ l r => l.count + 1 + r.count
defFingerHand.Hand.referenceno axiomsStructures/FingerHand/Packed/Definitions/Abstract.lean:22
def reference : Hand → Tree  | .knuckle t => t  | .node cs _ l r => Pointer.plug cs (.node l.reference r.reference)
defFingerHand.Hand.treeno axiomsStructures/FingerHand/Packed/Definitions/Abstract.lean:26
def tree : Hand → Tree  | .knuckle t => t  | .node _ d l r => d.tree (.node l.tree r.tree)
defFingerHand.Hand.labelsno axiomsStructures/FingerHand/Packed/Definitions/Abstract.lean:30
/-- The pseudofinger skeleton, using global ranks from the reference tree. -/def labels : Hand → Nat → LabelledTree Nat  | .knuckle _, _ => .nil  | .node cs _ l r, lo =>      let base := lo + Pointer.offset cs      .node (l.labels base) (base + l.reference.size) (r.labels (base + l.reference.size + 1))

These four readings of a hand are what the proofs relate:

  • count is the number of pseudofingers.
  • reference is the reference subtree the hand stands for. It plugs the node’s own subtree back into its path, so it uses only path, never tendon.
  • tree is the physical tree. It uses only tendon: the node’s own subtree becomes the bottom of the tendon’s tree (State.tree). The path is bookkeeping for the proofs; the physical tree does not depend on it.
  • labels lo is the skeleton as a labelled tree, with global reference ranks. A pseudofinger’s rank is lo, plus the offset of its path, plus the size of its left reference subtree. Knuckles contribute nothing.

Valid asks that every tendon represent its path, in the sense of State.Represents. On a valid hand the physical and reference trees have the same size and the same inorder ranks (tree_size). The potential of a hand is the sum of the potentials of its tendons.

35.2.3.1 Building a hand

defFingerHand.Hand.extendno axiomsStructures/FingerHand/Packed/Definitions/Abstract.lean:45
/-- Extend an incoming reference path during canonical construction. The newlyformed tendon is balanced; incremental updates use the tendon operations. -/def extend (f : Pointer.Frame) : Hand → Hand  | .knuckle t => .knuckle (Pointer.plug [f] t)  | .node cs _ l r => .node (cs ++ [f]) (Tendon.State.ofPath (cs ++ [f])) l r
defFingerHand.Hand.buildpropext · Classical.choice · Quot.soundStructures/FingerHand/Packed/Definitions/Abstract.lean:51
/-- Extract precisely the marked/branching skeleton used by `compress`, keepingits suppressed paths as concrete two-deque tendons and its knuckles intact. -/def build (marks : Finset Nat) : Tree → Nat → Hand  | .nil, _ => .knuckle .nil  | .node l r, lo =>      let left := build marks l lo      let right := build marks r (lo + l.size + 1)      if lo + l.size ∈ marks then .node [] (Tendon.State.ofPath []) left right      else match left, right with        | .knuckle _, .knuckle _ => .knuckle (.node l r)        | .knuckle _, q => q.extend (.right l)        | q, .knuckle _ => q.extend (.left r)        | q, s => .node [] (Tendon.State.ofPath []) q s
defFingerHand.Hand.forStatepropext · Classical.choice · Quot.soundStructures/FingerHand/Packed/Definitions/Abstract.lean:65
/-- Pack the fingers of a multifinger state, including its permanent root anchor. -/def forState {k : Nat} (s : Multifinger.State k) : Hand := build (stateMarks s) s.tree 0

build marks t lo follows compress case by case. A marked vertex becomes a pseudofinger with an empty path. An unmarked vertex whose subtrees are both knuckles stays inside one larger knuckle. If exactly one side contains marks, the vertex is pushed onto the incoming path of that side with extend. If both sides contain marks, the vertex branches and becomes a pseudofinger. extend rebuilds the tendon as a balanced State.ofPath each time, which is fine for a specification but is not how the simulation maintains it. forState s packs a multi-finger state.

Two facts make the canonical hand a good starting point. Its skeleton is exactly compress (build_labels). And when every finger is at the root, as in Multifinger.initial, the only mark is the root, the hand is one pseudofinger with two knuckles, its physical tree is the reference tree itself, and its potential is 00 (initial_tree). So no startup reshaping is needed.

35.2.3.2 The invariant

defFingerHand.Hand.Representspropext · Classical.choice · Quot.soundStructures/FingerHand/Packed/Definitions/Abstract.lean:68
/-- The maintained representation invariant. Tendons may retain arbitrary validdeque states; the reference tree and pseudofinger skeleton must match themultifinger state exactly. -/def Represents {k : Nat} (h : Hand) (s : Multifinger.State k) : Prop :=  h.Valid ∧ h.reference = s.tree ∧ h.labels 0 = FingerHand.ofState s

h.Represents s is the invariant of the simulation:

  1. every tendon represents its reference path (Valid),
  2. the reference tree is exactly the multi-finger tree s.tree, and
  3. the skeleton is exactly the compressed skeleton ofState s, labels included.

The deque states inside the tendons are not fixed: any valid state is allowed, and the potential accounts for their imbalance. Conditions 2 and 3 tie the hand to the simulated state precisely. All fingers are pseudofingers, and there are no extra pseudofingers, so the hand has at most 2(k+1)2(k+1) of them (Represents.count). Each pseudofinger costs at most five physical frames, one edge from its skeleton parent and at most four inside its tendon (location). The cursor therefore reaches finger ii and comes back in fewer than 10(k+1)10(k+1) instructions each way, by navigation alone (Represents.visit). This holds for every maintained hand, not only the canonical one.

35.2.4 Editing inside a hand

The updates are local. A zipper over the hand locates the region being edited:

inductiveFingerHand.Hand.ContextFrameno axiomsStructures/FingerHand/Packed/Updates/Definitions/Abstract.lean:36
/-- An enclosing pseudofinger and the sibling region not being edited. -/inductive ContextFrame where  | left (path : List Pointer.Frame) (tendon : Tendon.State) (right : Hand)  | right (path : List Pointer.Frame) (tendon : Tendon.State) (left : Hand)

A context frame is an enclosing pseudofinger, with its path, its tendon and the sibling hand that is not being edited. As for Pointer.Frame, the constructor names the side on which the edited region lies.

defFingerHand.Hand.Context.plugno axiomsStructures/FingerHand/Packed/Updates/Definitions/Abstract.lean:43
def plug : List ContextFrame → Hand → Hand  | [], h => h  | .left cs d r :: fs, h => plug fs (.node cs d h r)  | .right cs d l :: fs, h => plug fs (.node cs d l h)
defFingerHand.Hand.Context.Validno axiomsStructures/FingerHand/Packed/Updates/Definitions/Abstract.lean:48
def Valid : List ContextFrame → Prop  | [] => True  | .left cs d r :: fs => d.Represents cs ∧ r.Valid ∧ Valid fs  | .right cs d l :: fs => d.Represents cs ∧ l.Valid ∧ Valid fs
defFingerHand.Hand.Context.pointerno axiomsStructures/FingerHand/Packed/Updates/Definitions/Abstract.lean:53
/-- The actual pointer zipper of the edited region within the represented BST. -/def pointer : List ContextFrame → List Pointer.Frame  | [] => []  | .left _ d r :: fs => (.left r.tree :: d.context) ++ pointer fs  | .right _ d l :: fs => (.right l.tree :: d.context) ++ pointer fs
defFingerHand.Hand.Context.referencePathno axiomsStructures/FingerHand/Packed/Updates/Definitions/Abstract.lean:59
/-- The same enclosing region in the uncompressed reference tree. -/def referencePath : List ContextFrame → List Pointer.Frame  | [] => []  | .left cs _ r :: fs => (.left r.reference :: cs) ++ referencePath fs  | .right cs _ l :: fs => (.right l.reference :: cs) ++ referencePath fs
defFingerHand.Hand.Context.potentialno axiomsStructures/FingerHand/Packed/Updates/Definitions/Abstract.lean:65
def potential : List ContextFrame → Nat  | [] => 0  | .left _ d r :: fs => d.potential + r.potential + potential fs  | .right _ d l :: fs => d.potential + l.potential + potential fs
defFingerHand.Hand.Context.countno axiomsStructures/FingerHand/Packed/Updates/Definitions/Abstract.lean:70
def count : List ContextFrame → Nat  | [] => 0  | .left _ _ r :: fs => 1 + r.count + count fs  | .right _ _ l :: fs => 1 + l.count + count fs

plug rebuilds the whole hand, and Valid checks the tendons and siblings along the way. The two paths relate the edited region to the two trees. pointer is the physical zipper: for each enclosing pseudofinger, one frame for the edge into the region, then the at most four frames of its tendon’s context. referencePath is the same position in the reference tree, through the full reference path of each enclosing vertex. potential and count add up what the enclosing frames hold. The proof lemma Context.lift_trace turns a local bound into a global one. A local trace with amortized cost CC lifts to the whole hand at cost C+10⋅C + 10 \cdot(number of frames), because the cursor travels down and back. In a maintained hand this is at most C+20(k+1)C + 20(k+1) (lift_in_state).

defFingerHand.Hand.graftno axiomsStructures/FingerHand/Packed/Updates/Definitions/Abstract.lean:8
/-- Insert a pseudofinger above `child`, keeping its off-path sibling as aknuckle and its incoming tendon explicit. -/def graft (cs : List Pointer.Frame) (d : Tendon.State) (f : Pointer.Frame) (child : Hand) : Hand :=  match f with  | .left g => .node cs d child (.knuckle g)  | .right g => .node cs d (.knuckle g) child
defFingerHand.Hand.forkno axiomsStructures/FingerHand/Packed/Updates/Definitions/Abstract.lean:15
/-- An exposed pseudofinger with no incoming tendon. -/def fork (left right : Hand) : Hand := .node [] (Tendon.State.ofPath []) left right

graft cs d f child puts a new pseudofinger above child, with incoming path cs, tendon d, and the off-path sibling of frame f kept as a knuckle. fork l r is a pseudofinger with an empty incoming path and the canonical empty tendon. It is an exposed vertex, physically the parent of its children’s trees.

inductiveFingerHand.Hand.LocalRotationno axiomsStructures/FingerHand/Packed/Updates/Definitions/Abstract.lean:18
/-- Rotate two exposed neighboring pseudofingers. Their child regions and theincoming tendon of the rotated region stay intact. -/inductive LocalRotation : Hand → Hand → Prop where  | right (cs : List Pointer.Frame) (d : Tendon.State) (a b c : Hand) :      LocalRotation (.node cs d (fork a b) c) (.node cs d a (fork b c))  | left (cs : List Pointer.Frame) (d : Tendon.State) (a b c : Hand) :      LocalRotation (.node cs d a (fork b c)) (.node cs d (fork a b) c)

A local rotation rotates two adjacent exposed pseudofingers, both with empty incoming paths, inside the region of an enclosing node with path cs and tendon d. The tendon and the three subhands a, b, c are unchanged. Physically this is a single rotation just below the bottom of tendon d, at depth at most four. It takes two instructions plus at most eight to go there and back (LocalRotation.trace). The labelled skeleton undergoes the same rotation (LocalRotation.labels), so all pseudofingers keep their ranks.

structureFingerHand.Hand.Expansionno axiomsStructures/FingerHand/Packed/Updates/Definitions/Abstract.lean:26
/-- A local expansion retains the reference tree and every existingpseudofinger, adding exactly one pseudofinger. It is a representation change,not an insertion into the underlying BST's key universe. -/structure Expansion (before after : Hand) : Prop where  before_valid : before.Valid  after_valid : after.Valid  reference : after.reference = before.reference  count : after.count = before.count + 1  keys : ∀ lo, (before.labels lo).inorder.Sublist (after.labels lo).inorder

An expansion is a representation change that adds exactly one pseudofinger. Both hands are valid, the reference tree is the same, the count goes up by one, and, for every offset lo, the old skeleton’s inorder list is a sublist of the new one. It is not an insertion into the BST: no key is added to the tree, only to the skeleton. Its five fields, before_valid, after_valid, reference, count and keys, are exactly these conditions. The proof files show that the new vertex is exactly one key (Expansion.key_set). Marking that key makes the expanded skeleton canonical for the enlarged mark set (Expansion.mark_added).

35.2.5 Exposing a key

structureFingerHand.Hand.Exposureno axiomsStructures/FingerHand/Packed/Neighbors/Definitions/Abstract.lean:8
/-- Make a specified reference key available as a pseudofinger while retainingall existing pseudofingers. At most one new hand vertex is introduced. -/structure Exposure (before after : Hand) (key : Nat) : Prop where  valid : after.Valid  reference : after.reference = before.reference  keys : ∀ lo, (before.labels lo).inorder.Sublist (after.labels lo).inorder  count : after.count ≤ before.count + 1  retained : key ∈ (after.labels 0).inorder

Exposure before after key is the contract of phase 1. The new hand is valid and stands for the same reference tree. It keeps every old pseudofinger, has at most one more, and has key among its pseudofingers. Unlike an expansion, an exposure may add nothing, when key already is a pseudofinger. The field names valid, reference, keys, count and retained are the five conditions.

The library proves exposure lemmas for each neighbour of a pseudofinger (in Packed/Neighbors):

  • expose_parent: if the incoming path is nonempty, the parent is the innermost path node, and the tendon’s removeBottom operation takes it out, at amortized cost at most 180180 plus navigation. If the path is empty, the parent already is the enclosing pseudofinger, at no cost.
  • expose_left, expose_right: a child is the root of a knuckle, or the top of the child hand’s path, or already a pseudofinger. The first case splits the knuckle into a pseudofinger and two smaller knuckles, which changes no pointer. The second uses removeTop on the child’s tendon (expose_root).
  • expose_instruction: for any legal multi-finger instruction, the destination of the moving finger can be exposed.

35.2.6 Pruning the skeleton

When a finger leaves a key, the hand must lose the vertices that are no longer needed. These definitions describe, on labelled trees, which vertices go:

defFingerHand.joinpropext · Classical.choice · Quot.soundStructures/FingerHand/Pruning/Definitions/Abstract.lean:8
/-- Retain a marked root or a branch; contract an unmarked unary root. -/def join (marks : Finset Nat) (l : LabelledTree Nat) (x : Nat) (r : LabelledTree Nat) : LabelledTree Nat :=  if x ∈ marks then .node l x r  else match l, r with    | .nil, _ => r    | _, .nil => l    | _, _ => .node l x r
defFingerHand.prunepropext · Classical.choice · Quot.soundStructures/FingerHand/Pruning/Definitions/Abstract.lean:16
/-- Recompute a marked skeleton using its existing labels, without relabellingthe surviving vertices by consecutive ranks. -/def prune (marks : Finset Nat) : LabelledTree Nat → LabelledTree Nat  | .nil => .nil  | .node l x r => join marks (prune marks l) x (prune marks r)

join is the step of compress for one vertex. It keeps a marked vertex or a branching one, and contracts an unmarked vertex with an empty side. prune marks t applies it bottom up to a labelled tree, keeping its labels. It recomputes a skeleton for fewer marks from an existing skeleton, with no access to the reference tree. Two results justify this. If a⊆ba \subseteq b then prune a (prune b t) = prune a t (prune_prune). And if a hand’s skeleton contains every mark of its reference interval, then pruning it gives exactly the compressed skeleton of the reference tree (prune_labels).

inductiveFingerHand.Contractsno axiomsStructures/FingerHand/Pruning/Definitions/Abstract.lean:33
/-- A skeleton obtained by discarding subtrees and contracting unary paths.Surviving vertices keep their labels and ancestor relationships. -/inductive Contracts : LabelledTree Nat → LabelledTree Nat → Prop where  | empty (t : LabelledTree Nat) : Contracts t .nil  | node {l r a b : LabelledTree Nat} (x : Nat) :      Contracts l a → Contracts r b → Contracts (.node l x r) (.node a x b)  | left {l r a : LabelledTree Nat} (x : Nat) : Contracts l a → Contracts (.node l x r) a  | right {l r b : LabelledTree Nat} (x : Nat) : Contracts r b → Contracts (.node l x r) b

Contracts s t says that t arises from s by discarding subtrees and contracting paths. Surviving vertices keep their labels and their ancestor relations. Every hand’s skeleton is a contraction of its labelled reference tree (labels_contracts). When the labels are distinct, any contraction that retains all the marks determines the exact compressed skeleton (Contracts.project). Distinctness matters here: without it, another occurrence of the same label could falsely witness that a mark was retained.

defFingerHand.lostMarkspropext · Classical.choice · Quot.soundStructures/FingerHand/Pruning/Definitions/Abstract.lean:22
/-- Count occurrences of marks that disappear. On a distinctly labelled tree,erasing one key contributes at most one to this quantity. -/def lostMarks (before after : Finset Nat) : LabelledTree Nat → Nat  | .nil => 0  | .node l x r => lostMarks before after l +      (if x ∈ before ∧ x ∉ after then 1 else 0) + lostMarks before after r
defFingerHand.skeletonMassno axiomsStructures/FingerHand/Pruning/Definitions/Abstract.lean:29
/-- Counting a nonempty skeleton with an extra root unit makes the loss ofbranch vertices telescope when terminals are removed. -/def skeletonMass (t : LabelledTree Nat) : Nat := t.erase.size + if t = .nil then 0 else 1

These two quantities bound how much a skeleton shrinks. lostMarks before after t counts the vertices of t labelled by a mark of before that is not in after. skeletonMass t is the number of vertices plus one if t is nonempty; the extra unit pays for a component that disappears entirely. For a⊆ba \subseteq b, removing the marks of b∖ab \setminus a loses at most two units of mass per lost mark (prune_mass_loss). On distinctly labelled trees, removing one mark removes at most two skeleton vertices, however deep the reference tree is (erase_size).

defFingerHand.stationaryMarkspropext · Classical.choice · Quot.soundStructures/FingerHand/Pruning/Definitions/Abstract.lean:42
/-- The root anchor and every finger except the one being moved. A coincidentinactive finger keeps its key in this set. -/def stationaryMarks {k : Nat} (s : Multifinger.State k) (i : Fin k) : Finset Nat :=  insert (rootRank s.tree) ((Finset.univ.erase i).image s.position)

stationaryMarks s i is the root anchor together with every finger except finger ii. When finger ii moves without changing the tree, the new marks are these stationary marks plus its new position (step_stationary_marks). A coincident finger, or the root anchor, at the old position keeps that key marked. The proof lemma update_common_skeleton then shows that the skeletons before and after the move are both prunings of one joint skeleton, marked with the old marks plus the new position. That joint skeleton has at most two more vertices than either of them.

inductiveFingerHand.MarkedRotationpropext · Classical.choice · Quot.soundStructures/FingerHand/Pruning/Definitions/Abstract.lean:47
/-- A reference rotation whose two participating keys are retained as marks.The enclosing vertices need not be marked. -/inductive MarkedRotation (marks : Finset Nat) : LabelledTree Nat → LabelledTree Nat → Prop where  | right {a b c : LabelledTree Nat} {x y : Nat} : x ∈ marks → y ∈ marks →      MarkedRotation marks (.node (.node a x b) y c) (.node a x (.node b y c))  | left {a b c : LabelledTree Nat} {x y : Nat} : x ∈ marks → y ∈ marks →      MarkedRotation marks (.node a x (.node b y c)) (.node (.node a x b) y c)  | inLeft {a b c : LabelledTree Nat} {x : Nat} : MarkedRotation marks a b →      MarkedRotation marks (.node a x c) (.node b x c)  | inRight {a b c : LabelledTree Nat} {x : Nat} : MarkedRotation marks a b →      MarkedRotation marks (.node c x a) (.node c x b)

MarkedRotation marks s t is a rotation of the labelled reference tree, anywhere inside it, whose two participating keys are both marked. The enclosing vertices need not be marked. Compression commutes with such a rotation: the skeleton of the rotated tree is the rotated skeleton (MarkedRotation.project). This is why a finger rotation first marks its parent temporarily: once both endpoints are pseudofingers, the reference rotation is a Hand.LocalRotation of the hand.

35.2.7 Suppression

Pruning says which vertices go. Suppression removes them one at a time, by steps the pointer machine can perform:

defFingerHand.Reducedpropext · Classical.choice · Quot.soundStructures/FingerHand/Pruning/Suppression/Definitions/Abstract.lean:8
/-- Every retained vertex is marked or branches into two nonempty retainedregions. This is the local normal form of a compressed skeleton. -/def Reduced (marks : Finset Nat) : LabelledTree Nat → Prop  | .nil => True  | .node l x r => Reduced marks l ∧ Reduced marks r ∧ (x ∈ marks ∨ l ≠ .nil ∧ r ≠ .nil)

Reduced marks t is the normal form of a skeleton: every vertex is marked or has two nonempty children. Compressed skeletons are in this form.

inductiveFingerHand.Suppresspropext · Classical.choice · Quot.soundStructures/FingerHand/Pruning/Suppression/Definitions/Abstract.lean:14
/-- Suppress one obsolete vertex without creating another obsolete branch.A unary vertex can always be contracted. Removing a leaf requires its parentto remain marked. Enclosing vertices are retained. -/inductive Suppress (marks : Finset Nat) (x : Nat) : LabelledTree Nat → LabelledTree Nat → Prop where  | unaryLeft {t : LabelledTree Nat} : t ≠ .nil → Suppress marks x (.node t x .nil) t  | unaryRight {t : LabelledTree Nat} : t ≠ .nil → Suppress marks x (.node .nil x t) t  | leafLeft {r : LabelledTree Nat} {y : Nat} : y ∈ marks →      Suppress marks x (.node (.node .nil x .nil) y r) (.node .nil y r)  | leafRight {l : LabelledTree Nat} {y : Nat} : y ∈ marks →      Suppress marks x (.node l y (.node .nil x .nil)) (.node l y .nil)  | inLeft {a b r : LabelledTree Nat} {y : Nat} : Suppress marks x a b →      Suppress marks x (.node a y r) (.node b y r)  | inRight {a b l : LabelledTree Nat} {y : Nat} : Suppress marks x a b →      Suppress marks x (.node l y a) (.node l y b)

Suppress marks x s t removes the single obsolete vertex x from s, without making any other vertex obsolete:

  • unaryLeft, unaryRight: x has one nonempty child. Contract it. Physically, x joins a tendon, at the bottom of its own incoming tendon (suppress_bottom_under, using Tendon.State.addBottom) or at the top of its child’s tendon (suppress_top_under, using addTop).
  • leafLeft, leafRight: x is a leaf whose parent y is marked. Delete it. Physically, its two knuckles and the node itself merge into one knuckle, which changes no pointer and no potential (demote_left). The parent must be marked: an unmarked parent would become unary, and therefore obsolete as well.
  • inLeft, inRight: the removal happens inside a subtree, and the enclosing vertices are kept.

Suppress is exactly the pruning it is meant to implement. If s is reduced for the marks plus x, x is not a mark, and the labels are distinct, then prune marks s = t (Suppress.prune_eq), and t is again reduced (Suppress.reduced).

35.2.8 Simulating one instruction

The pieces combine as follows (in Packed/Simulation). Here kk is the number of fingers, and every bound is amortized: instructions plus the new potential, at most a constant plus the old potential.

  • A move of finger ii from xx to a neighbour yy (simulate_move). prepare_move exposes yy, giving a hand whose skeleton is canonical for the old marks plus yy. If xx is still needed (it is another finger, the root anchor, or a branching vertex), the hand already represents the new state (move_retained). Otherwise cleanup_obsolete suppresses xx. The physical step required for this is located from the instruction that moved the finger: going to a parent uses suppress_up, and going to a child uses suppress_down_left and its mirror image. The amortized cost is at most 324+40(k+1)324 + 40(k+1).
  • A rotation at finger ii (simulate_rotate_right, simulate_rotate_left). The parent pp is exposed as a temporary mark. The two pseudofingers are rotated as a local rotation (rotate_right_prepared), which keeps the skeleton canonical because both keys are marked. Then finish_rotation drops the temporary mark on pp, suppressing pp if it has become obsolete. At the root the anchor moves with the rotation, since the rotating finger becomes the new root (rotation_marks_right). The amortized cost is at most 344+60(k+1)344 + 60(k+1).

simulate_step takes the larger bound for all six instructions, and simulate_trace adds it up along a trace, with the potentials telescoping. For a request, simulate_serves simulates the instructions before the visit, navigates to the visiting finger and back in fewer than 10(k+1)10(k+1) instructions each way, and simulates the instructions after it. The request unit pays for that navigation. Over a whole execution, simulate_execution gives a Pointer.Execution whose cost plus the final potential is at most (344+60(k+1)) C(344 + 60(k+1))\,C plus the initial potential. The initial potential is 00 when all fingers start at the root, which is one_finger_execution.

Reading the Lean
  • The simulated execution starts from the same physical tree as the multi-finger one, and its final tree is the physical tree of the final hand. That tree has the same keys as the final multi-finger tree, but usually not the same shape.
  • The hand is not rebalanced. The skeleton is laid out as it is, and its depth can be as large as its size, so navigating it costs O(k)O(k). This is where the term 60(k+1)60(k+1) comes from.
  • Planning is free, as everywhere in the pointer model. The bounds count pointer instructions and request units, and the simulation reads the multi-finger execution to decide what to do. The hand’s reference paths and labels are bookkeeping and cost nothing.