Documentation

CslibFrontier.DataStructure.Retroactive.Basic

Timeline editing by reset and replay #

This is the elementary rebuilding transformation motivated by retroactive data structures Demaine, Iacono, and Langerman. A timeline edit is followed by an explicit reset and replay of the edited history. It is not an efficient rollback algorithm, and Retroactive.spec has no query operation, historical or present: every edit returns Unit. The correctness of Retroactive.implementation therefore says only that each edit issues admissible backend calls and preserves the representation invariant: the backend stores the edited timeline, and its ordinary state is that timeline replayed from the initial state. Editing an admissible timeline can make later operations invalid, so admissibility checks the entire edited history.

Main definitions #

Main results #

The edit primitive includes storing the timeline and exposing the edited list to its caller. A backend must certify this primitive's resource weight; list copying is not assumed constant time. Like all StateTransitionSpec.Client results, the resource claims count explicit backend operations and leave pure Lean controller computation uncharged. There is no fixed Word-RAM compiler here.

References #

An edit position names a gap for insertion and an existing entry for deletion.

Edit the operation sequence. Valid excludes out-of-range positions.

Insertion allows the final gap; deletion requires an existing entry.

theorem Cslib.DataStructure.TimelineEdit.length_insert {Op : Type} (os : List Op) (i : ℕ) (o : Op) (hi : i ≤ os.length) :
((insert i o).apply os).length = os.length + 1

A valid insertion increases the timeline length by one.

theorem Cslib.DataStructure.TimelineEdit.length_erase {Op : Type} (os : List Op) (i : ℕ) (hi : i < os.length) :
((erase i).apply os).length + 1 = os.length

A valid deletion removes exactly one operation.

@[reducible, inline]
abbrev Cslib.DataStructure.Retroactive.spec (D : StateTransitionSpec) {stepFn : D.State → (o : D.Op) → D.State × D.Result o} [D.HasStepFn stepFn] (initial : D.State) :

A dynamic specification of admissible timeline edits.

Primitive operations required by rebuilding; editing returns the entire new timeline.

@[reducible, inline]

Return types for BackendOp D.Op.

@[reducible, inline]
abbrev Cslib.DataStructure.Retroactive.backendStep (D : StateTransitionSpec) {stepFn : D.State → (o : D.Op) → D.State × D.Result o} [D.HasStepFn stepFn] (initial : D.State) (s : List D.Op × D.State) (o : BackendOp D.Op) :

Pure transition function of the rebuilding backend.

@[reducible, inline]
abbrev Cslib.DataStructure.Retroactive.backendValid (D : StateTransitionSpec) {stepFn : D.State → (o : D.Op) → D.State × D.Result o} [D.HasStepFn stepFn] (initial : D.State) (s : List D.Op × D.State) :

Admissibility predicate for BackendOp D.Op.

@[reducible, inline]
abbrev Cslib.DataStructure.Retroactive.backend (D : StateTransitionSpec) {stepFn : D.State → (o : D.Op) → D.State × D.Result o} [D.HasStepFn stepFn] (initial : D.State) :

The rebuilding backend tracks the stored timeline and current ordinary state separately.

def Cslib.DataStructure.Retroactive.replay (D : StateTransitionSpec) {stepFn : D.State → (o : D.Op) → D.State × D.Result o} [D.HasStepFn stepFn] (initial : D.State) :
List D.Op → (backend D initial).Client Unit

Replay ordinary operations through explicitly charged backend calls.

def Cslib.DataStructure.Retroactive.methods (D : StateTransitionSpec) {stepFn : D.State → (o : D.Op) → D.State × D.Result o} [D.HasStepFn stepFn] (initial : D.State) :
Methods (spec D initial) (backend D initial)

Editing, reset, and replay are distinct calls in the interface client.

theorem Cslib.DataStructure.Retroactive.replay_run (D : StateTransitionSpec) {stepFn : D.State → (o : D.Op) → D.State × D.Result o} [D.HasStepFn stepFn] (initial : D.State) (timeline os : List D.Op) (s : D.State) :
(backend D initial).runClient (timeline, s) (replay D initial os) = ((), (timeline, (D.run s os).1), List.map BackendOp.execute os)

Replay preserves the stored timeline and has precisely the original operation trace.

theorem Cslib.DataStructure.Retroactive.replay_valid (D : StateTransitionSpec) {stepFn : D.State → (o : D.Op) → D.State × D.Result o} [D.HasStepFn stepFn] (initial : D.State) (timeline os : List D.Op) (s : D.State) (h : D.ValidHistory s os) :

Every admissible ordinary history is admissible when executed through the backend.

def Cslib.DataStructure.Retroactive.implementation (D : StateTransitionSpec) {stepFn : D.State → (o : D.Op) → D.State × D.Result o} [D.HasStepFn stepFn] (initial : D.State) :
Module (spec D initial) (backend D initial)

Rebuilding correctness is independent of the implementation of reset, edit, and execution.

def Cslib.DataStructure.Retroactive.primitiveCharge (D : StateTransitionSpec) {stepFn : D.State → (o : D.Op) → D.State × D.Result o} [D.HasStepFn stepFn] (initial : D.State) (editCharge : List D.Op → TimelineEdit D.Op → ℕ) (resetCharge : ℕ) (ordinaryCharge : D.State → D.Op → ℕ) (s : (backend D initial).State) :
(backend D initial).Op → ℕ

Separate charges for timeline editing, resetting, and ordinary operations.

theorem Cslib.DataStructure.Retroactive.operation_charge (D : StateTransitionSpec) {stepFn : D.State → (o : D.Op) → D.State × D.Result o} [D.HasStepFn stepFn] (initial : D.State) (editCharge : List D.Op → TimelineEdit D.Op → ℕ) (resetCharge : ℕ) (ordinaryCharge : D.State → D.Op → ℕ) (timeline : List D.Op) (state : D.State) (e : TimelineEdit D.Op) :
(backend D initial).totalCost (primitiveCharge D initial editCharge resetCharge ordinaryCharge) (timeline, state) ((backend D initial).runClient (timeline, state) (methods D initial e)).2.2 = editCharge timeline e + resetCharge + D.totalCost ordinaryCharge initial (e.apply timeline)

The full operation charge retains both editing and resetting overhead.

theorem Cslib.DataStructure.Retroactive.realized_bound (D : StateTransitionSpec) {stepFn : D.State → (o : D.Op) → D.State × D.Result o} [D.HasStepFn stepFn] (initial : D.State) {C Trace : Type} {exec : Execution (backend D initial).signature C Trace} (R : StateTransitionImpl (backend D initial) exec) (weight : Trace → ℕ) (editCharge : List D.Op → TimelineEdit D.Op → ℕ) (resetCharge : ℕ) (ordinaryCharge : D.State → D.Op → ℕ) (B : Bound R weight (primitiveCharge D initial editCharge resetCharge ordinaryCharge)) :
Bound ((implementation D initial).realize R) (total weight) fun (os : (spec D initial).State) (e : (spec D initial).Op) => editCharge os e + resetCharge + D.totalCost ordinaryCharge initial (TimelineEdit.apply e os)

Resource bounds transport to any realization of the backend interface. This certificate then composes across arbitrary admissible timeline edits via Bound.client.

theorem Cslib.DataStructure.Retroactive.realized_linear_bound (D : StateTransitionSpec) {stepFn : D.State → (o : D.Op) → D.State × D.Result o} [D.HasStepFn stepFn] (initial : D.State) {C Trace : Type} {exec : Execution (backend D initial).signature C Trace} (R : StateTransitionImpl (backend D initial) exec) (weight : Trace → ℕ) (editCharge : List D.Op → TimelineEdit D.Op → ℕ) (resetCharge : ℕ) (ordinaryCharge : D.State → D.Op → ℕ) (B : Bound R weight (primitiveCharge D initial editCharge resetCharge ordinaryCharge)) (T : ℕ) (hT : ∀ (s : D.State) (o : D.Op), D.valid s o → ordinaryCharge s o ≤ T) :
Bound ((implementation D initial).realize R) (total weight) fun (os : (spec D initial).State) (e : (spec D initial).Op) => editCharge os e + resetCharge + (TimelineEdit.apply e os).length * T

A uniform ordinary-operation bound gives linear replay overhead. Editing and resetting remain explicit, and the resulting certificate applies to every concrete operation execution.