Chapter 13Real-world implementations
The models of this part count BST operations: moves, rotations and requests. Nothing else is charged. An online algorithm of Chapter 5 receives the whole request history at every step, and may compute anything from it for free. That is the right model for competitive analysis, but it says nothing about the memory and the time that an implementation spends to choose its operations.
Bose et al. (2012a) observe that almost all BST implementations in the literature have three more properties: they work in the pointer machine model, they use no more space than the tree itself plus words of balance information per node and working variables, and they generate their BST operations in time proportional to the BST cost. Demaine et al. (2013) define a real-world BST, crediting Bose et al. (2012a) for the term, by the space conditions alone: one that can be implemented with a constant number of registers of bits and bits of data at each node. Both papers prove transformations that preserve these properties: the deamortization (Theorem 7 of Bose et al. (2012a), Chapter 30) preserves all three, and ComboBST and the one-finger simulation of several fingers (Theorems 3 and 5 of Demaine et al. (2013), Chapter 29) preserve the space conditions.
This chapter makes the notion precise. A real-world machine is a finite program with a fixed number of word registers, run on a tree whose nodes hold a fixed number of words, with one persistent finger. Every instruction costs one step of running time, and only moves and rotations count as BST operations. The second half of the chapter compiles every such program into a single program for the pointer machine of Tarjan (1979), with a constant factor in running time.
Fix a real-world machine . If runs from state to state in steps, with of them moves or rotations, then those instructions form a trace of the single-cursor machine of Chapter 7 from the tree and cursor of to those of , with the node data forgotten, and . The machine receives one request at a time, and two executions of the same requests from the same state end in the same state with the same costs.
In Lean, machines are RealWorld.Machine, their states RealWorld.State, runs RealWorld.Run, and traces Pointer.Trace.
The Lean statements
theorem physical {w f r l : Nat} {p : Machine f r l} {s t : State w f r l} {time cost : Nat} (h : Run p s t time cost) : Pointer.Trace s.cursor.erase t.cursor.erase cost := … run_physical htheorem bst_cost {w f r l : Nat} {p : Machine f r l} {s t : State w f r l} {time cost : Nat} (h : Run p s t time cost) : cost ≤ time := … cost_le_time htheorem causal {w f r l : Nat} {p : Machine f r l} {s t u : State w f r l} {xs : List (BitVec w)} {a b c d : Nat} (h : Execution p s xs t a b) (k : Execution p s xs u c d) : t = u ∧ a = c ∧ b = d := … execution_unique h kStore the tree in records with three links (left, right, parent), the key and the node’s data words. Every move and rotation of the finger is then a fixed program of at most 19 pointer-machine instructions, which keeps the whole representation and allocates nothing. A real-world machine with instructions compiles to one pointer-machine program of instructions. Start the compiled program from a state that stores the source state, a relation that requires a nonempty tree, and let the word width be at least one bit. If the machine serves requests in steps in total, the compiled program responds to the same requests in at most instructions, ends in a state that stores the source’s final state, and keeps the number of records.
In Lean, the pointer machine is Cslib.PointerMachine from CSLib Frontier, the representation is Linked.Realizes, the relation between the two machines is Compiler.Image, and the compiler is Compiler.compile.
The Lean statements
/-- Every legal abstract pointer operation has a bounded-time concreteimplementation preserving the entire tree, all node data, and exact storage. -/theorem primitive {w e regs : Nat} {s : State w e regs} {c d : Cursor w e} {op : Multifinger.Opcode} (rep : Realizes s c) (legal : LabelledCursor.perform op c = some d) : ∃ t time, time ≤ 19 ∧ Cslib.PointerMachine.Executes (program op) (start s) t time ∧ Realizes t d ∧ t.heap.length = s.heap.length := … Linked.primitive rep legalModels.RealWorld.Linked.Definitions, Models.RealWorld.PointerMachine.Definitions, Models.Labelled.Cursor.Definitions, Models.Multifinger.Program.Definitions, Models.Rotation, Models.Labelled.Ordering.Definitions, CslibFrontier.Computability.PointerMachine.Defs/-- Compile every successful source request into one fixed pointer-machineprogram, with charged dispatch, request setup and halt. -/theorem response {w f r l : Nat} (p : Machine f r l) {s u : State w f r l} {t : Target w f r} {key : BitVec w} {time cost : Nat} (width : 0 < w) (image : Image s t) (source : RealWorld.Responds p s key u time cost) : ∃ v actual, actual ≤ 19 * time + 3 ∧ Responds p t key v actual ∧ Image u v := … response_simulation p width image sourceModels.RealWorld.Compiler.Definitions, Models.RealWorld.Definitions, Models.RealWorld.Linked.Definitions, Models.RealWorld.PointerMachine.Definitions, Models.Labelled.Cursor.Definitions, Models.Labelled.Cursor.Map.Definitions, Models.Multifinger.Program.Definitions, Models.Rotation, Models.Labelled.Ordering.Definitions, CslibFrontier.Computability.PointerMachine.Defs/-- The compiled code is reused unchanged across the whole online sequence. -/theorem execution {w f r l : Nat} (p : Machine f r l) {s u : State w f r l} {t : Target w f r} {xs : List (BitVec w)} {time cost : Nat} (width : 0 < w) (image : Image s t) (source : RealWorld.Execution p s xs u time cost) : ∃ v actual, actual ≤ 19 * time + 3 * xs.length ∧ Execution p t xs v actual ∧ Image u v := … execution_simulation p width image sourceModels.RealWorld.Compiler.Definitions, Models.RealWorld.Definitions, Models.RealWorld.Linked.Definitions, Models.RealWorld.PointerMachine.Definitions, Models.Labelled.Cursor.Definitions, Models.Labelled.Cursor.Map.Definitions, Models.Multifinger.Program.Definitions, Models.Rotation, Models.Labelled.Ordering.Definitions, CslibFrontier.Computability.PointerMachine.Defs/-- No execution of compiled code allocates or removes a record. -/theorem storage {w f r l : Nat} (p : Machine f r l) {s t : Target w f r} {n : Nat} (run : Cslib.PointerMachine.Runs (compile p) s t n) : t.heap.length = s.heap.length := … storage_preserved p runModels.RealWorld.Compiler.Definitions, Models.RealWorld.Definitions, Models.RealWorld.Linked.Definitions, Models.RealWorld.PointerMachine.Definitions, Models.Labelled.Cursor.Definitions, Models.Labelled.Cursor.Map.Definitions, Models.Multifinger.Program.Definitions, Models.Rotation, Models.Labelled.Ordering.Definitions, CslibFrontier.Computability.PointerMachine.Defs/-- Code size is fixed by the source controller, independent of tree size. -/theorem code_size {w f r l : Nat} (p : Machine f r l) : (compile (w := … w) p).length = 24 * l + 4 := compile_length pModels.RealWorld.Compiler.Definitions, Models.RealWorld.Definitions, Models.RealWorld.Linked.Definitions, Models.RealWorld.PointerMachine.Definitions, Models.Labelled.Cursor.Definitions, Models.Labelled.Cursor.Map.Definitions, Models.Multifinger.Program.Definitions, Models.Rotation, Models.Labelled.Ordering.Definitions, CslibFrontier.Computability.PointerMachine.Defs13.1 The machine
/-- Immutable key plus a fixed number of mutable words. -/structure Node (width fields : Nat) where key : BitVec width data : Fin fields → BitVec width/-- Finite code, independent of tree size and word width. No instructioncontains a Lean function for computing its result. Arithmetic is modular. -/inductive Instruction (fields registers labels : Nat) where | literal (dst : Fin registers) (value : Nat) (next : Fin labels) | copy (dst src : Fin registers) (next : Fin labels) | add (dst a b : Fin registers) (next : Fin labels) | sub (dst a b : Fin registers) (next : Fin labels) | branch (a b : Fin registers) (yes no : Fin labels) | load (dst : Fin registers) (field : Fin fields) (next : Fin labels) | store (field : Fin fields) (src : Fin registers) (next : Fin labels) | key (dst : Fin registers) (next : Fin labels) | query (dst : Fin registers) (next : Fin labels) | bst (op : Multifinger.Opcode) (next : Fin labels) | mark (next : Fin labels) | finishstructure Machine (fields registers labels : Nat) where code : Fin labels → Instruction fields registers labels entry : Fin labelsA node holds an immutable key and fields mutable words. Words are BitVec width: addition and subtraction wrap around modulo , and comparisons are unsigned. A machine is a table of labels instructions and an entry label. Each instruction names its successor labels, so the control is finite: the numbers of fields, registers and labels are fixed, independently of the tree, the requests and the word width. Constants in instructions are fixed natural numbers. No instruction contains a Lean function, so a machine cannot hide a computation in an instruction.
The instructions are of three kinds.
- Register instructions: a constant, a copy, addition, subtraction, a branch on
a < b, and reading the current request into a register. - Instructions at the finger: read the finger’s key, and read or write one of its data words. No instruction reads another node.
- BST instructions: one of the six moves and rotations of
Multifinger.Opcode(Chapter 10), applied at the finger.
Two more instructions close a request. mark succeeds only if the finger’s key equals the request, and records that the request was visited. finish succeeds only after a mark, and ends the request.
/-- A semantic presentation of a linked tree and one persistent finger.The zipper describes the finger; it is not an additional runtime stack.Node links and the finger are represented by addresses in a concrete lowering.There is no request history, pending output list, or arbitrary private state. -/structure State (width fields registers labels : Nat) where cursor : LabelledCursor.Config (Node width fields) regs : Fin registers → BitVec width request : BitVec width seen : Bool pc : Option (Fin labels)/-- Scalar instructions and local metadata operations are charged even thoughthey emit no BST opcode. A request can finish only after an explicit visit. -/def eval {w f r l : Nat} (i : Instruction f r l) (s : State w f r l) : Option (State w f r l × Option Multifinger.Opcode) := do match i with | .literal dst value next => return ({ s with regs := Function.update s.regs dst (BitVec.ofNat w value), pc := some next }, none) | .copy dst src next => return ({ s with regs := Function.update s.regs dst (s.regs src), pc := some next }, none) | .add dst a b next => return ({ s with regs := Function.update s.regs dst (s.regs a + s.regs b), pc := some next }, none) | .sub dst a b next => return ({ s with regs := Function.update s.regs dst (s.regs a - s.regs b), pc := some next }, none) | .branch a b yes no => return ({ s with pc := some (if (s.regs a).toNat < (s.regs b).toNat then yes else no) }, none) | .load dst field next => let node ← s.cursor.read return ({ s with regs := Function.update s.regs dst (node.data field), pc := some next }, none) | .store field src next => let node ← s.cursor.read let cursor ← s.cursor.write { node with data := Function.update node.data field (s.regs src) } return ({ s with cursor, pc := some next }, none) | .key dst next => let node ← s.cursor.read return ({ s with regs := Function.update s.regs dst node.key, pc := some next }, none) | .query dst next => return ({ s with regs := Function.update s.regs dst s.request, pc := some next }, none) | .bst op next => let cursor ← LabelledCursor.perform op s.cursor return ({ s with cursor, pc := some next }, some op) | .mark next => let node ← s.cursor.read if node.key = s.request then return ({ s with seen := true, pc := some next }, none) else none | .finish => if s.seen then return ({ s with pc := none }, none) else nonedef tick {w f r l : Nat} (p : Machine f r l) (s : State w f r l) := s.pc.bind fun pc => eval (p.code pc) sA state holds the tree with its finger, as a labelled cursor (LabelledCursor.Config, from Chapter 36), the registers, the current request, the visited flag, and the label of the next instruction. When there is no next label, the machine is between two requests, at a response boundary. The cursor is a zipper, the path from the finger to the root. This is how the mathematics describes a linked tree with a pointer into it; it is not a stack that the machine stores, and the second half of the chapter stores the tree with parent links instead.
eval executes one instruction and returns the new state, together with the BST instruction it performed, if any. It fails on an illegal move or rotation (LabelledCursor.perform), on an access to an empty finger, on a mark at the wrong key, and on a finish before a mark. A failed step has no successor, so a machine that fails or loops never completes its request. tick runs the instruction at the current label.
13.2 Requests and runs
/-- Input is available only at a response boundary, one key at a time. -/def accept {w f r l : Nat} (p : Machine f r l) (x : BitVec w) (s : State w f r l) : Option (State w f r l) := if s.pc = none then some { s with request := x, seen := false, pc := some p.entry } else noneinductive Event (width : Nat) where | input (key : BitVec width) | compute (output : Option Multifinger.Opcode)/-- CSLib supplies the transition-system vocabulary, not the instruction set. -/def lts {w f r l : Nat} (p : Machine f r l) : Cslib.LTS (State w f r l) (Event w) where Tr s event t := match event with | .input x => accept p x s = some t | .compute out => tick p s = some (t, out)accept gives the machine its next request, and only at a response boundary. It stores the key, clears the visited flag and jumps to the entry label. The state has no field for past requests. A machine that wants to remember something about them must keep it in its registers or in the data words of the nodes. The machine is also a labelled transition system in the sense of CSLib, whose events are inputs and computation steps.
/-- Proof-only execution log. Each constructor charges one machine instruction;only emitted pointer instructions contribute to BST cost. -/inductive Run {w f r l : Nat} (p : Machine f r l) : State w f r l → State w f r l → Nat → Nat → Prop where | nil {s} : Run p s s 0 0 | cons {s t u time cost out} : tick p s = some (t, out) → Run p t u time cost → Run p s u (time + 1) (cost + out.toList.length)/-- One completed request. The initial state is a boundary state. -/def Responds {w f r l : Nat} (p : Machine f r l) (s : State w f r l) (x : BitVec w) (t : State w f r l) (time cost : Nat) : Prop := ∃ start, accept p x s = some start ∧ Run p start t time cost ∧ t.pc = none/-- Completed online accesses. The request list is specification data; it isnever passed to an instruction or stored in a machine state. -/inductive Execution {w f r l : Nat} (p : Machine f r l) : State w f r l → List (BitVec w) → State w f r l → Nat → Nat → Prop where | nil {s} : Execution p s [] s 0 0 | cons {s t u x xs time cost moreTime moreCost} : Responds p s x t time cost → Execution p t xs u moreTime moreCost → Execution p s (x :: xs) u (time + moreTime) (cost + moreCost)Run p s t time cost says that time steps lead from s to t, and that cost of them are BST instructions. Responds is one request: accept it, run, and stop at a boundary. Execution serves a list of requests in order and adds up both costs. The list of requests is part of the statement, not of the state: no instruction can read it.
Theorem (runs). The BST instructions of a run form a Pointer.Trace between the cursors of its two states with the node data forgotten (Config.erase), with exactly cost instructions, and cost ≤ time. Two executions of the same requests from the same state reach the same state with the same costs, and executions compose.
theorem physical {w f r l : Nat} {p : Machine f r l} {s t : State w f r l} {time cost : Nat} (h : Run p s t time cost) : Pointer.Trace s.cursor.erase t.cursor.erase cost := run_physical htheorem bst_cost {w f r l : Nat} {p : Machine f r l} {s t : State w f r l} {time cost : Nat} (h : Run p s t time cost) : cost ≤ time := cost_le_time htheorem causal {w f r l : Nat} {p : Machine f r l} {s t u : State w f r l} {xs : List (BitVec w)} {a b c d : Nat} (h : Execution p s xs t a b) (k : Execution p s xs u c d) : t = u ∧ a = c ∧ b = d := execution_unique h ktheorem append {w f r l : Nat} {p : Machine f r l} {s t u : State w f r l} {xs ys : List (BitVec w)} {a b c d : Nat} (h : Execution p s xs t a b) (k : Execution p t ys u c d) : Execution p s (xs ++ ys) u (a + c) (b + d) := execution_append h kSince a request enters only at a boundary, the machine cannot look ahead, and determinism makes the response to a prefix of the requests independent of what follows. This is the prefix property that Chapter 30 takes as the definition of an online program, here for a machine with bounded state. The trace starts where the previous request left the finger, so it is a trace of one persistent finger, as in Chapter 10, not a Pointer.Execution, which restarts at the root.
13.3 Real-world implementations
A machine is an implementation of a real-world BST if it works for every tree size , with the same code, and meets the conditions of the papers.
/-- Word-size discipline for a uniform family. Enough bits for addresses andkeys; at most logarithmically many bits. Constants do not depend on n. -/def WordSize (width : Nat → Nat) (constant : Nat) : Prop := ∀ n, n < 2 ^ width n ∧ width n ≤ constant * (Nat.log2 (n + 1) + 1)/-- Ordered immutable keys 0,...,n-1 at initialization. The addressabilitycondition in `WordSize` prevents truncation of these keys. -/def Initial {w f r l : Nat} (s : State w f r l) (n : Nat) : Prop := s.pc = none ∧ s.cursor.context = [] ∧ s.cursor.tree.inorder.map Node.key = List.ofFn (fun i : Fin n => BitVec.ofNat w i.val)/-- Total service is a separate obligation: finite code may otherwise fail orloop. Quantification is over every completed history and every existing key.`initial` is supplied separately; this does not give initialization free runtime. -/def Total {w f r l : Nat} (p : Machine f r l) (initial : State w f r l) (nodes : Nat) : Prop := ∀ (xs : List (BitVec w)) (s : State w f r l) (time cost : Nat), Execution p initial xs s time cost → ∀ x : BitVec w, x.toNat < nodes → ∃ t moreTime moreCost, Responds p s x t moreTime moreCost/-- Runtime bound restricted to reachable response boundaries. -/def EfficientFrom {w f r l : Nat} (p : Machine f r l) (initial : State w f r l) (constant : Nat) : Prop := ∀ (xs : List (BitVec w)) (s t : State w f r l) (a b time cost : Nat) (x : BitVec w), Execution p initial xs s a b → Responds p s x t time cost → time ≤ constant * (cost + 1)/-- Additional efficiency obligation, not a consequence of bounded storage.The additive request unit handles accesses already at the finger. -/def ProportionalTime {f r l : Nat} (p : Machine f r l) (constant : Nat) : Prop := ∀ (w : Nat) (s t : State w f r l) (x : BitVec w) (time cost : Nat), Responds p s x t time cost → time ≤ constant * (cost + 1)WordSize width Csays that words have enough bits to store the keys , , and at most bits. A word is therefore bits, as in the definition of Demaine et al. (2013).Initial s nsays thatsis at a boundary, with the finger at the root, and that the keys in inorder are .Total p s nsays that after every completed execution froms, the machine completes a request for every key . It is a separate obligation, because a finite program may fail or loop.EfficientFrom p s Cbounds the running time by the BST cost: every request served from a state reachable fromstakes at most steps if it performs BST instructions. The pays for a request whose key is already under the finger.ProportionalTimeis the same bound for every state and every word width, not only the reachable ones.
/-- Uniform implementation contract. Fixed code and register/field counts areoutside the size quantifier. Initialization is a specification, not freeexecutable preprocessing; a construction must separately account for setup. -/structure Implementation (fields registers labels : Nat) where machine : Machine fields registers labels width : Nat → Nat wordConstant : Nat wordSize : WordSize width wordConstant initial : (n : Nat) → State (width n) fields registers labels initialized : ∀ n, Initial (initial n) n total : ∀ n, Total machine (initial n) n timeConstant : Nat efficient : ∀ n, EfficientFrom machine (initial n) timeConstant/-- Three links, one immutable key, and f metadata words per node; r scalarregisters, root/finger/query registers and a flag. Finite control is separate. -/def storageWords (fields registers nodes : Nat) : Nat := (fields + 4) * nodes + registers + 4An Implementation packages one machine, a word width for every with WordSize, an initial state for every with Initial, and the certificates Total and EfficientFrom. Its constants are chosen before . Its numbers of data words per node and of registers are type parameters, also fixed before . storageWords f r n records the storage convention: three links, the key and data words per node, the registers, and four more words for the root, the finger, the request and the flag. It is a definition only, and no theorem refers to it; the compiled program below uses more registers (seven pointer registers and data registers), still a constant number. The space of the compiled program is controlled directly: its records are exactly the nodes of the tree, and no instruction changes their number (theorems below).
The initial states are given, not computed. Building the initial tree, and the time this takes, is not part of the definition.
- No algorithm is implemented yet. No algorithm of the book, not even move-to-root or splaying, is given as an
Implementation. - From machines to algorithms. An execution of an
Implementationis not yet turned into an online algorithm of Chapter 5 or a one-finger execution of Chapter 10, so the bounds proved for those do not yet apply to it. - Compiled implementations. The compiler of the next sections moves single runs; it does not yet turn an
Implementation, withTotalandEfficientFrom, into a pointer-machine program that serves every request in proportional time. - Initialization. Building the initial tree on the pointer machine, and charging its time, and the empty tree.
- The real-world clauses of the papers. That the deamortization preserves real-world implementations (Theorem 7 of Bose et al. (2012a)), and that ComboBST and the one-finger simulation do (Theorems 3 and 5 of Demaine et al. (2013)).
Roadmap entry 10.
13.4 Tarjan’s pointer machine
The machine above reads and writes the tree through its finger, but it treats the tree as a mathematical object. The papers count time on a pointer machine (Tarjan 1979): a fixed number of registers and a growing pool of records, all with the same fields. Registers and fields hold either pointers or data. Memory is reached only by following a pointer held in a register, and no instruction computes on pointers, so there is no address arithmetic. Every instruction costs one unit. The library uses the formalization of this machine in CSLib Frontier, Cslib.PointerMachine, which also allocates records; the programs of this chapter never do. Where Tarjan’s machine stops on an undefined value, this formalization reads a default value instead.
/-- The scalar instruction vocabulary is independent of word width. -/inductive WordOp where | literal (n : Nat) | add | subinductive WordTest where | eq | lt/-- Modular words, with no conversion from a word to a pointer. -/abbrev words (w : Nat) : Cslib.PointerMachine.Model where Value := BitVec w undefined := 0 Op := WordOp Test := WordTest evalOp | .literal n, _, _ => BitVec.ofNat w n | .add, x, y => x + y | .sub, x, y => x - y evalTest | .eq, x, y => decide (x = y) | .lt, x, y => decide (x.toNat < y.toNat)The data are the words of the first half, with constants, addition and subtraction, and the tests and . There is no conversion from a word to a pointer.
13.4.1 A first example: rotation at the root
The simplest representation of a tree has two links per record and no parent links. It is enough to move down and to rotate at the root, and it shows what the general representation must check.
/-- Three pointer registers (cursor and two temporaries), two child fields,one key field and `extra` metadata words. This initial representation has noparent links: upward navigation needs a later representation extension. -/abbrev layout (extra : Nat) : Cslib.PointerMachine.Layout := ⟨3, 2, 2, extra + 1⟩abbrev State (w extra : Nat) := Cslib.PointerMachine.State (words w) (layout extra)abbrev Record (w extra : Nat) := Cslib.PointerMachine.Record (words w) (layout extra)abbrev Instruction (w extra : Nat) := Cslib.PointerMachine.Instr (words w) (layout extra)abbrev Tree (w : Nat) := LabelledTree (Nat × BitVec w)The layout has three pointer registers (the cursor and two temporaries), two data registers, two links per record, the key and extra data words. A tree is labelled with pairs (address, key). The addresses are ghost labels: they relate the mathematical tree to the heap, and no program can read them.
/-- Addresses in this specification are ghost labels, never scalar machine data. -/def root {w : Nat} : Tree w → Cslib.PointerMachine.Pointer | .nil => none | .node _ x _ => some x.1def addresses {w : Nat} (t : Tree w) : List Nat := t.inorder.map Prod.fstdef keys {w : Nat} : Tree w → LabelledTree Nat | .nil => .nil | .node l x r => .node (keys l) x.2.toNat (keys r)/-- Recursive representation in an actual upstream heap. Metadata is unrestricted. -/def Represents {w e : Nat} (heap : List (Record w e)) : Tree w → Prop | .nil => True | .node l x r => (∃ record, heap[x.1]? = some record ∧ record.ptr 0 = root l ∧ record.ptr 1 = root r ∧ record.data 0 = x.2) ∧ Represents heap l ∧ Represents heap r/-- At a boundary all allocated records are exactly the distinct tree nodes.There is no separate unaccounted work heap. -/structure Boundary {w e : Nat} (s : State w e) (t : Tree w) : Prop where valid : s.Valid represents : Represents s.heap t distinct : (addresses t).Nodup covers : ∀ a, a < s.heap.length ↔ a ∈ addresses t ordered : (keys t).Ordered cursor : s.ptr 0 = root tRepresents heap t says that every node of t is a record at its address, with the links to its children and its key. Boundary s t asks more: valid pointers, distinct addresses, keys in order, the cursor on the root, and a heap that consists of the tree’s records and nothing else. So no extra memory hides next to the tree.
/-- Accepting the next key changes only its designated register and the entrylabel. A suspended computation and all retained history must reside in `s`. -/def accept {w e : Nat} (entry : Nat) (key : BitVec w) (s : State w e) : State w e := { s.setData 0 key with pc := entry }/-- Fixed code and a restart label. Halting is the request boundary. -/structure Protocol (w e : Nat) where code : List (Instruction w e) entry : Nat/-- A response includes an actual visit along the execution, and counts thehalting instruction. The next key is unavailable until this response ends. -/def Responds {w e : Nat} (p : Protocol w e) (s : State w e) (key : BitVec w) (t : State w e) (time : Nat) : Prop := p.code[s.pc]? = some .halt ∧ Cslib.PointerMachine.Executes p.code (accept p.entry key s) t time ∧ ∃ u a b, Cslib.PointerMachine.Runs p.code (accept p.entry key s) u a ∧ Cslib.PointerMachine.Runs p.code u t b ∧ (∃ record, u.deref 0 = some record ∧ record.data 0 = key) ∧ time = a + b + 1/-- Histories are proof objects; they are not passed to the machine. -/inductive Execution {w e : Nat} (p : Protocol w e) : State w e → List (BitVec w) → State w e → Nat → Prop where | nil (s) : Execution p s [] s 0 | cons {s t u key rest a b} : Responds p s key t a → Execution p t rest u b → Execution p s (key :: rest) u (a + b)A protocol is a program with an entry label. Responds starts from a state stopped at a halt instruction. accept puts the key into data register and restarts at the entry, and the program runs to the next halt, which is counted. Somewhere along the way the cursor must visit a record holding the key. Executions chain responses.
/-- Downward navigation is exactly one pointer load. -/def down {w e : Nat} (side : Fin 2) : Instruction w e := .loadPtr 0 side 0/-- Root right rotation: load the left child and its right subtree, rewireboth child links, then put the new root in the cursor. No allocation. -/def rotateRight {w e : Nat} : List (Instruction w e) := [.loadPtr 1 0 0, .loadPtr 2 1 1, .storePtr 0 0 2, .storePtr 1 1 0, .movePtr 0 1]/-- Straight-line effects, used only to name the endpoints of these programs.The execution theorem separately establishes the actual charged `Cslib.PointerMachine.Runs`. -/def effects {w e : Nat} : List (Instruction w e) → State w e → State w e | [], s => s | i :: rest, s => effects rest { i.effect s with pc := s.pc + 1 }A move down is one load. A right rotation at the root is five instructions: load the left child, load its right subtree, relink the old root’s left child, relink the child’s right child, and move the cursor to the new root. effects evaluates a straight-line program; it only names the final state, and the theorems below prove that the machine reaches it.
Theorem (two-link representation). Executions of a protocol are deterministic. A move down is one instruction, and a move to the left child reaches the represented left subtree. A root rotation is five instructions, keeps the boundary invariant and the number of records, and makes the same change as a move down-left followed by a rotation of the finger over its parent in LabelledCursor.perform.
/-- Completed request prefixes uniquely determine the actual machine state and time. -/theorem causal {p : Protocol w e} {s t u : State w e} {xs : List (BitVec w)} {a b : Nat} (h : Execution p s xs t a) (k : Execution p s xs u b) : t = u ∧ a = b := execution_unique h kModels.RealWorld.PointerMachine.Definitions, Models.Labelled.Cursor.Definitions, Models.Multifinger.Program.Definitions, Models.Rotation, Models.Labelled.Ordering.Definitions, CslibFrontier.Computability.PointerMachine.Defs/-- A child-pointer load is one charged upstream instruction. -/theorem navigation_time (s : State w e) (side : Fin 2) (pc : s.pc = 0) : Cslib.PointerMachine.Runs [down side, .halt] s (effects [down side] s) 1 := down_runs s side pcModels.RealWorld.PointerMachine.Definitions, Models.Labelled.Cursor.Definitions, Models.Multifinger.Program.Definitions, Models.Rotation, Models.Labelled.Ordering.Definitions, CslibFrontier.Computability.PointerMachine.Defs/-- Navigation reaches the represented left subtree without changing its storage. -/theorem navigation {s : State w e} {l r : Tree w} {x : Nat × BitVec w} (h : Represents s.heap (.node l x r)) (cursor : s.ptr 0 = some x.1) : (effects [down 0] s).ptr 0 = root l ∧ Represents (effects [down 0] s).heap l := down_left_refines h cursorModels.RealWorld.PointerMachine.Definitions, Models.Labelled.Cursor.Definitions, Models.Multifinger.Program.Definitions, Models.Rotation, Models.Labelled.Ordering.Definitions, CslibFrontier.Computability.PointerMachine.Defs/-- A root right rotation takes five upstream instructions before halt. -/theorem rotation_time (s : State w e) (pc : s.pc = 0) : Cslib.PointerMachine.Runs (rotateRight ++ [.halt]) s (effects rotateRight s) 5 := rotateRight_runs s pcModels.RealWorld.PointerMachine.Definitions, Models.Labelled.Cursor.Definitions, Models.Multifinger.Program.Definitions, Models.Rotation, Models.Labelled.Ordering.Definitions, CslibFrontier.Computability.PointerMachine.Defs/-- A root rotation preserves the ordered BST, its exact node storage, and validity. -/theorem rotation {s : State w e} {a b c : Tree w} {x y : Nat × BitVec w} (h : Boundary s (.node (.node a x b) y c)) (pc : s.pc = 0) : Boundary (effects rotateRight s) (.node a x (.node b y c)) ∧ (effects rotateRight s).heap.length = s.heap.length := rotateRight_boundary h pcModels.RealWorld.PointerMachine.Definitions, Models.Labelled.Cursor.Definitions, Models.Multifinger.Program.Definitions, Models.Rotation, Models.Labelled.Ordering.Definitions, CslibFrontier.Computability.PointerMachine.Defs/-- The same change is down-left followed by the existing child rotation. -/theorem rotation_bst (a b c : Tree w) (x y : Nat × BitVec w) : (LabelledCursor.perform .downLeft ⟨.node (.node a x b) y c, []⟩).bind (LabelledCursor.perform .rotateLeftChild) = some ⟨.node a x (.node b y c), []⟩ := rotateRight_cursor a b c x yModels.RealWorld.PointerMachine.Definitions, Models.Labelled.Cursor.Definitions, Models.Multifinger.Program.Definitions, Models.Rotation, Models.Labelled.Ordering.Definitions, CslibFrontier.Computability.PointerMachine.Defs13.4.2 Parent links and all six instructions
Moving up and rotating below the root need parent links.
/-- Cursor, root, parent, grandparent, middle subtree, null and comparisonregister; a fixed scalar register file; left, right and parent fields. -/abbrev layout (extra regs : Nat) : Cslib.PointerMachine.Layout := ⟨7, regs, 3, extra + 1⟩abbrev State (w e regs : Nat) := Cslib.PointerMachine.State (PointerMachine.words w) (layout e regs)abbrev Record (w e regs : Nat) := Cslib.PointerMachine.Record (PointerMachine.words w) (layout e regs)abbrev Instruction (w e regs : Nat) := Cslib.PointerMachine.Instr (PointerMachine.words w) (layout e regs)/-- Ghost labels retain every data field, so representation preservation alsopreserves metadata. The address is not available to scalar instructions. -/abbrev Label (w e : Nat) := Nat × (Fin (e + 1) → BitVec w)abbrev Tree (w e : Nat) := LabelledTree (Label w e)abbrev Cursor (w e : Nat) := LabelledCursor.Config (Label w e)The layout has seven pointer registers (the cursor, the root, the parent, the grandparent, the middle subtree, a null pointer and a temporary), regs data registers, and three links per record: left, right and parent. A label is an address together with all the data fields of the node, its key first. So a theorem that keeps the labels keeps the data too.
def root {w e : Nat} : Tree w e → Pointer | .nil => none | .node _ x _ => some x.1def parent {w e : Nat} : List (LabelledCursor.Frame (Label w e)) → Pointer | [] => none | .left x _ :: _ | .right _ x :: _ => some x.1def node {w e : Nat} (l r p : Pointer) (x : Label w e) : Record w e regs := ⟨fun i => if i = 0 then l else if i = 1 then r else p, x.2⟩def Represents {w e : Nat} (h : List (Record w e regs)) : Pointer → Tree w e → Prop | _, .nil => True | p, .node l x r => h[x.1]? = some (node (root l) (root r) p x) ∧ Represents h (some x.1) l ∧ Represents h (some x.1) r/-- Representation of ancestors and siblings, parameterized by the link tothe focused subtree. This is a proof-only zipper, not a stored stack. -/def Context {w e : Nat} (h : List (Record w e regs)) : Pointer → List (LabelledCursor.Frame (Label w e)) → Prop | _, [] => True | p, .left x r :: cs => h[x.1]? = some (node p (root r) (parent cs) x) ∧ Represents h (some x.1) r ∧ Context h (some x.1) cs | p, .right l x :: cs => h[x.1]? = some (node (root l) p (parent cs) x) ∧ Represents h (some x.1) l ∧ Context h (some x.1) csdef keys {w e : Nat} : Tree w e → LabelledTree Nat | .nil => .nil | .node l x r => .node (keys l) (x.2 0).toNat (keys r)def addresses {w e : Nat} (t : Tree w e) := t.inorder.map Prod.fst/-- A complete representation at any nonempty cursor, with no extra records. -/structure Realizes {w e : Nat} (s : State w e regs) (c : Cursor w e) : Prop where valid : s.Valid focus : Represents s.heap (parent c.context) c.focus context : Context s.heap (root c.focus) c.context distinct : (addresses c.tree).Nodup covers : ∀ a, a < s.heap.length ↔ a ∈ addresses c.tree ordered : (keys c.tree).Ordered cursor : s.ptr 0 = root c.focus rootReg : s.ptr 1 = root c.tree nonempty : c.focus ≠ .nilRepresents heap p t says that t is stored below the parent pointer p, with both directions of every edge. Context does the same for the path from the finger to the root: each ancestor, its other subtree, and its own parent. Realizes s c says that the machine state stores the cursor c, at any depth: valid pointers, the focus and the context represented, distinct addresses, ordered keys, a heap with exactly the tree’s records, the cursor register on the finger, the root register on the root, and a nonempty focus.
/-- Both upward opcodes follow the same parent field. Their side conditionsare supplied by the legal abstract step. -/def move {w e : Nat} (field : Fin 3) : List (Instruction w e regs) := [.loadPtr 0 field 0, .halt]/-- Promote the cursor above its parent. `side` is the cursor's side in itsparent and `other` its opposite. Branches inspect the grandparent to repairthe appropriate link, or update the root register when the parent was root.Null middle subtrees require no special branch: stores through null are no-ops. -/def rotate {w e : Nat} (side other : Fin 3) : List (Instruction w e regs) := [ .loadPtr 2 2 0, -- 0: parent .loadPtr 3 2 2, -- 1: grandparent .loadPtr 4 other 0, -- 2: middle subtree .storePtr side 2 4, -- 3: parent's child = middle .storePtr 2 4 2, -- 4: middle's parent = parent .storePtr other 0 2, -- 5: cursor's child = parent .storePtr 2 2 0, -- 6: parent's parent = cursor .storePtr 2 0 3, -- 7: cursor's parent = grandparent .null 5, -- 8 .ifPtrEq 3 5 16, -- 9: root case .loadPtr 6 0 3, -- 10 .ifPtrEq 6 2 14, -- 11: parent was grandparent's left child .storePtr 1 3 0, -- 12: right link .goto 17, -- 13 .storePtr 0 3 0, -- 14: left link .goto 17, -- 15 .movePtr 1 0, -- 16: root register .halt ]def program {w e : Nat} : Multifinger.Opcode → List (Instruction w e regs) | .downLeft => move 0 | .downRight => move 1 | .upLeft | .upRight => move 2 | .rotateLeftChild => rotate 0 1 | .rotateRightChild => rotate 1 0/-- Each invocation starts at its entry. Registers and heap persist. -/def start {w e : Nat} (s : State w e regs) : State w e regs := { s with pc := 0 }/-- A finite evaluator used to name endpoints, not an extra machine primitive. -/def run {w e : Nat} (code : List (Instruction w e regs)) : Nat → State w e regs → State w e regs | 0, s => s | n + 1, s => match Cslib.PointerMachine.step code s with | none => s | some t => run code n tA move is one load and a halt; both moves up follow the parent link. A rotation promotes the finger over its parent and leaves the finger on it. It relinks the middle subtree, the two nodes and the grandparent’s child on the correct side, or the root register if there is no grandparent. A store through the null pointer does nothing, so an empty middle subtree needs no special case. program gives the code of each of the six instructions, start sets the program counter to , and run evaluates a program for a given number of steps.
Theorem (the six instructions). If a state realizes a cursor and an instruction is legal at , its program runs in at most instructions, halt included, to a state that realizes the cursor after the instruction, with the same number of records.
/-- Every legal abstract pointer operation has a bounded-time concreteimplementation preserving the entire tree, all node data, and exact storage. -/theorem primitive {w e regs : Nat} {s : State w e regs} {c d : Cursor w e} {op : Multifinger.Opcode} (rep : Realizes s c) (legal : LabelledCursor.perform op c = some d) : ∃ t time, time ≤ 19 ∧ Cslib.PointerMachine.Executes (program op) (start s) t time ∧ Realizes t d ∧ t.heap.length = s.heap.length := Linked.primitive rep legalModels.RealWorld.Linked.Definitions, Models.RealWorld.PointerMachine.Definitions, Models.Labelled.Cursor.Definitions, Models.Multifinger.Program.Definitions, Models.Rotation, Models.Labelled.Ordering.Definitions, CslibFrontier.Computability.PointerMachine.DefsThe bound is a uniform bound for all six programs; the moves take two instructions.
13.5 Compiling a machine
The last step translates a whole real-world machine into one pointer-machine program. There is no outside dispatcher: the compiled program chooses its next block of code itself, and every jump is an instruction that costs one unit.
abbrev Target (w f r : Nat) := Linked.State w f (r + 4)abbrev Opcode (w f r : Nat) := Linked.Instruction w f (r + 4)/-- User registers occupy an initial segment; four private scalar registershold the current request, visited flag, key temporary and zero temporary. -/def userReg {r : Nat} (i : Fin r) : Fin (r + 4) := ⟨i, by omega⟩def queryReg (r : Nat) : Fin (r + 4) := ⟨r, by omega⟩def seenReg (r : Nat) : Fin (r + 4) := ⟨r + 1, by omega⟩def tempReg (r : Nat) : Fin (r + 4) := ⟨r + 2, by omega⟩def zeroReg (r : Nat) : Fin (r + 4) := ⟨r + 3, by omega⟩def field {f : Nat} (i : Fin f) : Fin (f + 1) := ⟨i + 1, by omega⟩The target is the parent-linked machine with data registers: the registers of the source machine, then the request, the visited flag, a temporary for keys and a temporary for zero. The data field of a node is field of its record, after the key.
def stride : Nat := 24def address {l : Nat} (pc : Fin l) : Nat := stride * pc.valdef boundary (l : Nat) : Nat := stride * ldef trap (l : Nat) : Nat := boundary l + 1def entry (l : Nat) : Nat := boundary l + 2/-- Relocate a primitive into a block and replace its halt by a chargedcontinuation jump. There is only one actual halt in the compiled program. -/def relocate {w f r : Nat} (base exit : Nat) : Opcode w f r → Opcode w f r | .goto k => .goto (base + k) | .ifPtrEq a b k => .ifPtrEq a b (base + k) | .ifTest p a b k => .ifTest p a b (base + k) | .halt => .goto exit | i => i/-- Machine code for each semantic instruction. All bookkeeping is charged. -/def block {w f r l : Nat} (base : Nat) : Instruction f r l → List (Opcode w f r) | .literal dst value next => [.op (.literal value) (userReg dst) (zeroReg r) (zeroReg r), .goto (address next)] | .copy dst src next => [.moveData (userReg dst) (userReg src), .goto (address next)] | .add dst a b next => [.op .add (userReg dst) (userReg a) (userReg b), .goto (address next)] | .sub dst a b next => [.op .sub (userReg dst) (userReg a) (userReg b), .goto (address next)] | .branch a b yes no => [.ifTest .lt (userReg a) (userReg b) (address yes), .goto (address no)] | .load dst i next => [.loadData (userReg dst) (field i) 0, .goto (address next)] | .store i src next => [.storeData (field i) 0 (userReg src), .goto (address next)] | .key dst next => [.loadData (userReg dst) 0 0, .goto (address next)] | .query dst next => [.moveData (userReg dst) (queryReg r), .goto (address next)] | .bst op next => (Linked.program op).map (relocate base (address next)) | .mark next => [.loadData (tempReg r) 0 0, .ifTest .eq (tempReg r) (queryReg r) (base + 3), .goto (trap l), .op (.literal 1) (seenReg r) (zeroReg r) (zeroReg r), .goto (address next)] | .finish => [.op (.literal 0) (zeroReg r) (zeroReg r) (zeroReg r), .ifTest .eq (seenReg r) (zeroReg r) (trap l), .goto (boundary l)]/-- The whole controller is one finite program. Padding and failed checks goto a nonhalting trap. Entry resets the visited flag using a machine instruction. -/def compile {w f r l : Nat} (p : Machine f r l) : List (Opcode w f r) := List.ofFn fun i : Fin (boundary l + 4) => if h : i.val < boundary l then let pc : Fin l := ⟨i.val / stride, (Nat.div_lt_iff_lt_mul (by decide)).mpr (by simpa [boundary, Nat.mul_comm] using h)⟩ ((block (address pc) (p.code pc))[i.val % stride]?).getD (.goto (trap l)) else if i.val = boundary l then .halt else if i.val = entry l then .op (.literal 0) (seenReg r) (zeroReg r) (zeroReg r) else if i.val = entry l + 1 then .goto (address p.entry) else .goto (trap l)Each source instruction gets a block of slots. Register and finger instructions become a few pointer-machine instructions and a jump. A BST instruction becomes its program from the previous section, relocated into the block, with its halt replaced by a jump to the block of the next label. After the blocks come four slots: the only halt, which is the response boundary, a trap that loops forever, and the two instructions that start a request. Unused slots, a failed mark and an early finish jump to the trap. The theorems below are about successful source runs only; they say nothing about the compiled program after an illegal move.
/-- Supplying a request is permitted only at the unique successful halt.It preserves the heap and all registers except the designated input register.Resetting the visited flag and jumping to source entry are charged code. -/def accept {w f r l : Nat} (key : BitVec w) (s : Target w f r) : Option (Target w f r) := if s.pc = boundary l then some { s.setData (queryReg r) key with pc := entry l } else none/-- A completed request in the one compiled program, including its halt. -/def Responds {w f r l : Nat} (p : Machine f r l) (s : Target w f r) (key : BitVec w) (t : Target w f r) (time : Nat) : Prop := ∃ input, accept (l := l) key s = some input ∧ Cslib.PointerMachine.Executes (compile p) input t time/-- The same code is reused for the whole request sequence. -/inductive Execution {w f r l : Nat} (p : Machine f r l) : Target w f r → List (BitVec w) → Target w f r → Nat → Prop where | nil (s) : Execution p s [] s 0 | cons {s t u key rest a b} : Responds p s key t a → Execution p t rest u b → Execution p s (key :: rest) u (a + b)A request is accepted only at the halt. It writes the key into the request register and enters the start code, which clears the visited flag and jumps to the source entry; both instructions are charged. Registers and records persist from one request to the next.
def decode (x : Linked.Label w f) : Node w f := ⟨x.2 0, fun i => x.2 (field i)⟩def flag (w : Nat) (seen : Bool) : BitVec w := BitVec.ofNat w (if seen then 1 else 0)def location {l : Nat} : Option (Fin l) → Nat | none => boundary l | some pc => address pc/-- The full compiler invariant: linked representation, user registers,current input, visited flag and control label. The address-labelled cursoris ghost state; only the fixed-size machine state is available at runtime. -/def Image (s : State w f r l) (t : Target w f r) : Prop := ∃ c, Linked.Realizes t c ∧ s.cursor = c.map decode ∧ (∀ i, t.data (userReg i) = s.regs i) ∧ t.data (queryReg r) = s.request ∧ t.data (seenReg r) = flag w s.seen ∧ t.pc = location s.pcImage s t relates a source state s to a target state t: some cursor with addresses is realized by t and decodes to the cursor of s, the registers and the request agree, the flag is stored as or , and the program counter of t is the start of the block of the next source instruction, or the halt at a boundary.
Theorem (compilation). Assume a word width of at least one bit, and a source state and a target state related by Image.
- If the source machine serves a request in steps, the compiled program responds to it in at most instructions, in a state related to the source’s final state. The compiled response is only accept and run to the halt; that it visits the key follows from the source run, through the relation. For requests in steps in total, the bound is .
- If the source serves the request in at most steps, where is its BST cost, the compiled program takes at most instructions.
- No run of the compiled program changes the number of records.
- The program has exactly instructions for a source machine with labels.
- A request served by the compiled program ends in a unique state, with a unique instruction count.
/-- Compile every successful source request into one fixed pointer-machineprogram, with charged dispatch, request setup and halt. -/theorem response {w f r l : Nat} (p : Machine f r l) {s u : State w f r l} {t : Target w f r} {key : BitVec w} {time cost : Nat} (width : 0 < w) (image : Image s t) (source : RealWorld.Responds p s key u time cost) : ∃ v actual, actual ≤ 19 * time + 3 ∧ Responds p t key v actual ∧ Image u v := response_simulation p width image sourceModels.RealWorld.Compiler.Definitions, Models.RealWorld.Definitions, Models.RealWorld.Linked.Definitions, Models.RealWorld.PointerMachine.Definitions, Models.Labelled.Cursor.Definitions, Models.Labelled.Cursor.Map.Definitions, Models.Multifinger.Program.Definitions, Models.Rotation, Models.Labelled.Ordering.Definitions, CslibFrontier.Computability.PointerMachine.Defs/-- The compiled code is reused unchanged across the whole online sequence. -/theorem execution {w f r l : Nat} (p : Machine f r l) {s u : State w f r l} {t : Target w f r} {xs : List (BitVec w)} {time cost : Nat} (width : 0 < w) (image : Image s t) (source : RealWorld.Execution p s xs u time cost) : ∃ v actual, actual ≤ 19 * time + 3 * xs.length ∧ Execution p t xs v actual ∧ Image u v := execution_simulation p width image sourceModels.RealWorld.Compiler.Definitions, Models.RealWorld.Definitions, Models.RealWorld.Linked.Definitions, Models.RealWorld.PointerMachine.Definitions, Models.Labelled.Cursor.Definitions, Models.Labelled.Cursor.Map.Definitions, Models.Multifinger.Program.Definitions, Models.Rotation, Models.Labelled.Ordering.Definitions, CslibFrontier.Computability.PointerMachine.Defs/-- Source time proportional to BST work transfers with explicit constants. -/theorem proportional_time {w f r l : Nat} (p : Machine f r l) {s u : State w f r l} {t : Target w f r} {key : BitVec w} {time cost constant : Nat} (width : 0 < w) (image : Image s t) (source : RealWorld.Responds p s key u time cost) (bound : time ≤ constant * (cost + 1)) : ∃ v actual, actual ≤ (19 * constant + 3) * (cost + 1) ∧ Responds p t key v actual ∧ Image u v := proportional_response p width image source boundModels.RealWorld.Compiler.Definitions, Models.RealWorld.Definitions, Models.RealWorld.Linked.Definitions, Models.RealWorld.PointerMachine.Definitions, Models.Labelled.Cursor.Definitions, Models.Labelled.Cursor.Map.Definitions, Models.Multifinger.Program.Definitions, Models.Rotation, Models.Labelled.Ordering.Definitions, CslibFrontier.Computability.PointerMachine.Defs/-- No execution of compiled code allocates or removes a record. -/theorem storage {w f r l : Nat} (p : Machine f r l) {s t : Target w f r} {n : Nat} (run : Cslib.PointerMachine.Runs (compile p) s t n) : t.heap.length = s.heap.length := storage_preserved p runModels.RealWorld.Compiler.Definitions, Models.RealWorld.Definitions, Models.RealWorld.Linked.Definitions, Models.RealWorld.PointerMachine.Definitions, Models.Labelled.Cursor.Definitions, Models.Labelled.Cursor.Map.Definitions, Models.Multifinger.Program.Definitions, Models.Rotation, Models.Labelled.Ordering.Definitions, CslibFrontier.Computability.PointerMachine.Defs/-- Code size is fixed by the source controller, independent of tree size. -/theorem code_size {w f r l : Nat} (p : Machine f r l) : (compile (w := w) p).length = 24 * l + 4 := compile_length pModels.RealWorld.Compiler.Definitions, Models.RealWorld.Definitions, Models.RealWorld.Linked.Definitions, Models.RealWorld.PointerMachine.Definitions, Models.Labelled.Cursor.Definitions, Models.Labelled.Cursor.Map.Definitions, Models.Multifinger.Program.Definitions, Models.Rotation, Models.Labelled.Ordering.Definitions, CslibFrontier.Computability.PointerMachine.Defs/-- Concrete successful requests have unique states and exact instruction counts. -/theorem deterministic {w f r l : Nat} {p : Machine f r l} {s t u : Target w f r} {key : BitVec w} {a b : Nat} (h : Responds p s key t a) (k : Responds p s key u b) : t = u ∧ a = b := response_unique h kModels.RealWorld.Compiler.Definitions, Models.RealWorld.Definitions, Models.RealWorld.Linked.Definitions, Models.RealWorld.PointerMachine.Definitions, Models.Labelled.Cursor.Definitions, Models.Labelled.Cursor.Map.Definitions, Models.Multifinger.Program.Definitions, Models.Rotation, Models.Labelled.Ordering.Definitions, CslibFrontier.Computability.PointerMachine.DefsThe per request pays for the two start instructions and the halt. The width must be positive so that the flag values and differ. The compiler moves a running time certificate from the source to the target; it does not produce one. During a rotation, the records may not represent a tree for a few instructions; the invariant holds again at the end of each source instruction.
The compiler simulates successful requests forward, from a given related pair of states with a nonempty tree. It does not build the initial records, handle the empty tree, or prove that a source machine terminates. Termination is the certificate Total of each implementation; the rest is listed in the box above.