Documentation

CslibFrontier.DataStructure.StateTransition.WP

Weakest preconditions with cost #

D.wp charge p Q s is the weakest precondition of the client p of a deterministic specification D, under a postcondition Q that sees the answer, the final state and the total charge of the run. It holds when every operation of the run from s is admissible and Q holds of the outcome (StateTransitionSpec.wp_iff). It is computed operation by operation (wp_pure, wp_lift_bind), so a program's effect and cost follow by simp, and it composes (wp_bind, wp_mono).

A postcondition that bounds the charge expresses an amortized inequality directly: Q a s' t may say t + potential s' ≤ budget + potential s. A CostHoare triple, whose budget is a function of the initial state, does not.

With the charge fun _ _ => 1 the cost is the number of operations (wp_one_iff). Over Memory.spec w with Memory.charge it is the memory cost.

Main definitions #

Main results #

def Cslib.DataStructure.StateTransitionSpec.wp (D : StateTransitionSpec) {f : D.State → (o : D.Op) → D.State × D.Result o} [D.HasStepFn f] (charge : D.State → D.Op → ℕ) {α : Type y} :
D.Client α → (α → D.State → ℕ → Prop) → D.State → Prop

The weakest precondition of the client p under Q, with charges charge: every operation of the run from s is admissible, and Q holds of the answer, the final state and the total charge.

@[simp]
theorem Cslib.DataStructure.StateTransitionSpec.wp_pure {D : StateTransitionSpec} {f : D.State → (o : D.Op) → D.State × D.Result o} [D.HasStepFn f] {charge : D.State → D.Op → ℕ} {α : Type y} (a : α) (Q : α → D.State → ℕ → Prop) (s : D.State) :
D.wp charge (pure a) Q s ↔ Q a s 0

A pure client is admissible and costs nothing.

@[simp]
theorem Cslib.DataStructure.StateTransitionSpec.wp_lift_bind {D : StateTransitionSpec} {f : D.State → (o : D.Op) → D.State × D.Result o} [D.HasStepFn f] {charge : D.State → D.Op → ℕ} {α : Type w} (o : D.Op) (k : D.Result o → D.Client α) (Q : α → D.State → ℕ → Prop) (s : D.State) :
D.wp charge (PFunctor.FreeM.lift o >>= k) Q s ↔ D.valid s o ∧ D.wp charge (k (D.stepFn s o).2) (fun (a : α) (s' : D.State) (t : ℕ) => Q a s' (t + charge s o)) (D.stepFn s o).1

An operation followed by a continuation: the operation is admissible, and the continuation runs from the next state with the operation's charge added.

@[simp]
theorem Cslib.DataStructure.StateTransitionSpec.wp_lift {D : StateTransitionSpec} {f : D.State → (o : D.Op) → D.State × D.Result o} [D.HasStepFn f] {charge : D.State → D.Op → ℕ} (o : D.Op) (Q : D.Result o → D.State → ℕ → Prop) (s : D.State) :
D.wp charge (PFunctor.FreeM.lift o) Q s ↔ D.valid s o ∧ Q (D.stepFn s o).2 (D.stepFn s o).1 (charge s o)

A single operation.

theorem Cslib.DataStructure.StateTransitionSpec.wp_mono {D : StateTransitionSpec} {f : D.State → (o : D.Op) → D.State × D.Result o} [D.HasStepFn f] {charge : D.State → D.Op → ℕ} {α : Type y} {p : D.Client α} {Q Q' : α → D.State → ℕ → Prop} (h : ∀ (a : α) (s : D.State) (t : ℕ), Q a s t → Q' a s t) {s : D.State} (hp : D.wp charge p Q s) :
D.wp charge p Q' s

Consequence: a weaker postcondition.

theorem Cslib.DataStructure.StateTransitionSpec.wp_bind {D : StateTransitionSpec} {f : D.State → (o : D.Op) → D.State × D.Result o} [D.HasStepFn f] {charge : D.State → D.Op → ℕ} {α β : Type y} (p : D.Client α) (k : α → D.Client β) (Q : β → D.State → ℕ → Prop) (s : D.State) :
D.wp charge (p >>= k) Q s ↔ D.wp charge p (fun (a : α) (s' : D.State) (t : ℕ) => D.wp charge (k a) (fun (b : β) (s'' : D.State) (t' : ℕ) => Q b s'' (t + t')) s') s

Sequencing: the charge of p >>= k is the charge of p plus the charge of k.

theorem Cslib.DataStructure.StateTransitionSpec.wp_iff {D : StateTransitionSpec} {f : D.State → (o : D.Op) → D.State × D.Result o} [D.HasStepFn f] {charge : D.State → D.Op → ℕ} {α : Type y} (p : D.Client α) (Q : α → D.State → ℕ → Prop) (s : D.State) :
D.wp charge p Q s ↔ D.ValidHistory s (D.clientHistory s p) ∧ Q (D.clientResult s p) (D.clientState s p) (D.totalCost charge s (D.clientHistory s p))

The weakest precondition holds exactly when the run is admissible and Q holds of its answer, final state and total charge.

theorem Cslib.DataStructure.StateTransitionSpec.wp_one_iff {D : StateTransitionSpec} {f : D.State → (o : D.Op) → D.State × D.Result o} [D.HasStepFn f] {α : Type y} (p : D.Client α) (Q : α → D.State → ℕ → Prop) (s : D.State) :
D.wp (fun (x : D.State) (x_1 : D.Op) => 1) p Q s ↔ D.ValidHistory s (D.clientHistory s p) ∧ Q (D.clientResult s p) (D.clientState s p) (D.clientHistory s p).length

With a unit charge, the weakest precondition counts operations.