Documentation

CslibFrontier.DataStructure.Memory.Triple

Correctness-and-cost triples for memory programs #

Memory.Triple w s p c Q says that the memory program p, run from the state s at word size w, issues only admissible operations, costs at most c memory units (Memory.charge), and ends with an answer and a state satisfying Q. It is StateTransitionSpec.CostHoare at the single initial state s; Memory.triple_iff unfolds it along the run of p.

The rules evaluate a program from its first operation on. Triple.read_bind and Triple.write_bind discharge one operation's admissibility and charge, and leave the continuation at the next state; Triple.bind splits a program at a subprogram with its own triple; Triple.pure ends the run. Each rule peels one unit off a cost written c + 1, so a straight-line program is checked by applying the rules in order. A recursive program is checked by induction on its recursion measure, with the induction hypothesis applied at the recursive call.

The word RAM's initial memory is zero past the input (zeroAbove_init), so a structure that needs zeroed storage can take it there with a dirty allocation, at the cost of one unit, instead of a zeroed one, whose charge grows with its size.

Main definitions #

Main results #

@[reducible, inline]
abbrev Cslib.DataStructure.Memory.Triple {α : Type} (w : ℕ) (s : State) (p : Prog α) (c : ℕ) (Q : α → State → Prop) :

Run from s at word size w, the program p issues only admissible operations, costs at most c memory units, and ends with an answer and a state satisfying Q.

theorem Cslib.DataStructure.Memory.triple_iff {w : ℕ} {α : Type} {s : State} {p : Prog α} {c : ℕ} {Q : α → State → Prop} :
Triple w s p c Q ↔ (spec w).ValidHistory s ((spec w).clientHistory s p) ∧ (spec w).totalCost charge s ((spec w).clientHistory s p) ≤ c ∧ Q ((spec w).clientResult s p) ((spec w).clientState s p)

A triple is a statement about the one run of the program.

theorem Cslib.DataStructure.Memory.Triple.mono {w : ℕ} {α : Type} {s : State} {c c' : ℕ} {Q Q' : α → State → Prop} {p : Prog α} (h : Triple w s p c Q) (hc : c ≤ c') (hQ : ∀ (a : α) (s' : State), Q a s' → Q' a s') :
Triple w s p c' Q'

Weaken the cost and the postcondition.

theorem Cslib.DataStructure.Memory.Triple.pure {w : ℕ} {α : Type} {s : State} {c : ℕ} {Q : α → State → Prop} {a : α} (h : Q a s) :
Triple w s (Pure.pure a) c Q

Returning a value costs nothing.

theorem Cslib.DataStructure.Memory.Triple.read_bind {w : ℕ} {α : Type} {s : State} {c : ℕ} {Q : α → State → Prop} {a : ℕ} {k : ℕ → Prog α} (ha : a < s.frontier) (h : Triple w s (k (s.mem a)) c Q) :
Triple w s (Memory.read a >>= k) (c + 1) Q

Read an allocated cell, then continue with its contents.

theorem Cslib.DataStructure.Memory.Triple.read {w : ℕ} {s : State} {Q : ℕ → State → Prop} {a : ℕ} (ha : a < s.frontier) (h : Q (s.mem a) s) :
Triple w s (Memory.read a) 1 Q

Read an allocated cell as a standalone program.

theorem Cslib.DataStructure.Memory.Triple.write_bind {w : ℕ} {α : Type} {s : State} {c : ℕ} {Q : α → State → Prop} {a v : ℕ} {k : Unit → Prog α} (ha : a < s.frontier) (hv : WordRAM.Fits w v) (h : Triple w { mem := Function.update s.mem a v, frontier := s.frontier } (k ()) c Q) :
Triple w s (Memory.write a v >>= k) (c + 1) Q

Write a word to an allocated cell, then continue.

theorem Cslib.DataStructure.Memory.Triple.write {w : ℕ} {s : State} {Q : Unit → State → Prop} {a v : ℕ} (ha : a < s.frontier) (hv : WordRAM.Fits w v) (h : Q () { mem := Function.update s.mem a v, frontier := s.frontier }) :
Triple w s (Memory.write a v) 1 Q

Write a word to an allocated cell as a standalone program.

theorem Cslib.DataStructure.Memory.Triple.alloc_bind {w : ℕ} {α : Type} {s : State} {c : ℕ} {Q : α → State → Prop} {k : ℕ} {next : ℕ → Prog α} (hk : WordRAM.Fits w (s.frontier + k)) (h : Triple w { mem := WordRAM.clear s.mem s.frontier k, frontier := s.frontier + k } (next s.frontier) c Q) :
Triple w s (Memory.alloc k >>= next) (c + (k + 1)) Q

Allocate k zeroed cells at the frontier, then continue with the base address.

theorem Cslib.DataStructure.Memory.Triple.alloc {w : ℕ} {s : State} {Q : ℕ → State → Prop} {k : ℕ} (hk : WordRAM.Fits w (s.frontier + k)) (h : Q s.frontier { mem := WordRAM.clear s.mem s.frontier k, frontier := s.frontier + k }) :
Triple w s (Memory.alloc k) (k + 1) Q

Allocate k zeroed cells as a standalone program.

theorem Cslib.DataStructure.Memory.Triple.allocDirty_bind {w : ℕ} {α : Type} {s : State} {c : ℕ} {Q : α → State → Prop} {k : ℕ} {next : ℕ → Prog α} (hk : WordRAM.Fits w (s.frontier + k)) (h : Triple w { mem := s.mem, frontier := s.frontier + k } (next s.frontier) c Q) :
Triple w s (Memory.allocDirty k >>= next) (c + 1) Q

Allocate k dirty cells at the frontier, then continue with the base address.

theorem Cslib.DataStructure.Memory.Triple.allocDirty {w : ℕ} {s : State} {Q : ℕ → State → Prop} {k : ℕ} (hk : WordRAM.Fits w (s.frontier + k)) (h : Q s.frontier { mem := s.mem, frontier := s.frontier + k }) :

Allocate k dirty cells as a standalone program.

theorem Cslib.DataStructure.Memory.Triple.allocBlock_bind {w : ℕ} {α : Type} {s : State} {c : ℕ} {Q : α → State → Prop} {k : ℕ} {next : WordRAM.Block → Prog α} (hk : WordRAM.Fits w (s.frontier + k)) (h : Triple w { mem := WordRAM.clear s.mem s.frontier k, frontier := s.frontier + k } (next { base := s.frontier, length := k }) c Q) :
Triple w s (Memory.allocBlock k >>= next) (c + (k + 1)) Q

Allocate a zeroed block of k cells, then continue with the block handle.

theorem Cslib.DataStructure.Memory.Triple.allocBlock {w : ℕ} {s : State} {Q : WordRAM.Block → State → Prop} {k : ℕ} (hk : WordRAM.Fits w (s.frontier + k)) (h : Q { base := s.frontier, length := k } { mem := WordRAM.clear s.mem s.frontier k, frontier := s.frontier + k }) :
Triple w s (Memory.allocBlock k) (k + 1) Q

Allocate a zeroed block of k cells as a standalone program.

theorem Cslib.DataStructure.Memory.Triple.allocBlockDirty_bind {w : ℕ} {α : Type} {s : State} {c : ℕ} {Q : α → State → Prop} {k : ℕ} {next : WordRAM.Block → Prog α} (hk : WordRAM.Fits w (s.frontier + k)) (h : Triple w { mem := s.mem, frontier := s.frontier + k } (next { base := s.frontier, length := k }) c Q) :
Triple w s (Memory.allocBlockDirty k >>= next) (c + 1) Q

Allocate a field of k cells without clearing it, then continue with the field at the frontier.

theorem Cslib.DataStructure.Memory.Triple.allocBlockDirty {w : ℕ} {s : State} {Q : WordRAM.Block → State → Prop} {k : ℕ} (hk : WordRAM.Fits w (s.frontier + k)) (h : Q { base := s.frontier, length := k } { mem := s.mem, frontier := s.frontier + k }) :

Allocate a dirty block of k cells as a standalone program.

theorem Cslib.DataStructure.Memory.Triple.get_bind {w : ℕ} {α : Type} {s : State} {c : ℕ} {Q : α → State → Prop} {b : WordRAM.Block} {i : ℕ} {k : ℕ → Prog α} (hb : b.Allocated s) (hi : i < b.length) (h : Triple w s (k (s.mem (b.base + i))) c Q) :
Triple w s (b.get i >>= k) (c + 1) Q

Read entry i of an allocated block, then continue with its contents.

theorem Cslib.DataStructure.Memory.Triple.get {w : ℕ} {s : State} {Q : ℕ → State → Prop} {b : WordRAM.Block} {i : ℕ} (hb : b.Allocated s) (hi : i < b.length) (h : Q (s.mem (b.base + i)) s) :
Triple w s (b.get i) 1 Q

Read entry i of an allocated block as a standalone program.

theorem Cslib.DataStructure.Memory.Triple.set_bind {w : ℕ} {α : Type} {s : State} {c : ℕ} {Q : α → State → Prop} {b : WordRAM.Block} {i v : ℕ} {k : Unit → Prog α} (hb : b.Allocated s) (hi : i < b.length) (hv : WordRAM.Fits w v) (h : Triple w { mem := Function.update s.mem (b.base + i) v, frontier := s.frontier } (k ()) c Q) :
Triple w s (b.set i v >>= k) (c + 1) Q

Write v to entry i of an allocated block, then continue.

theorem Cslib.DataStructure.Memory.Triple.set {w : ℕ} {s : State} {Q : Unit → State → Prop} {b : WordRAM.Block} {i v : ℕ} (hb : b.Allocated s) (hi : i < b.length) (hv : WordRAM.Fits w v) (h : Q () { mem := Function.update s.mem (b.base + i) v, frontier := s.frontier }) :
Triple w s (b.set i v) 1 Q

Write v to entry i of an allocated block as a standalone program.

theorem Cslib.DataStructure.Memory.Triple.bind {w : ℕ} {α β : Type} {s : State} {p : Prog α} {k : α → Prog β} {c₁ c₂ : ℕ} {Q : β → State → Prop} (h : Triple w s p c₁ fun (a : α) (s' : State) => Triple w s' (k a) c₂ Q) :
Triple w s (p >>= k) (c₁ + c₂) Q

Sequential composition. Run p within c₁, and from each of its outcomes run the continuation within c₂.

theorem Cslib.DataStructure.Memory.Triple.ite {w : ℕ} {α : Type} {s : State} {c : ℕ} {Q : α → State → Prop} {cond : Prop} [Decidable cond] {p₁ p₂ : Prog α} (h₁ : cond → Triple w s p₁ c Q) (h₂ : ¬cond → Triple w s p₂ c Q) :
Triple w s (if cond then p₁ else p₂) c Q

Branching rule for if cond then p₁ else p₂.

theorem Cslib.DataStructure.Memory.Triple.dite {w : ℕ} {α : Type} {s : State} {c : ℕ} {Q : α → State → Prop} {cond : Prop} [Decidable cond] {p₁ : cond → Prog α} {p₂ : ¬cond → Prog α} (h₁ : ∀ (h : cond), Triple w s (p₁ h) c Q) (h₂ : ∀ (h : ¬cond), Triple w s (p₂ h) c Q) :
Triple w s (if h : cond then p₁ h else p₂ h) c Q

Dependent branching rule for if h : cond then p₁ h else p₂ h.

theorem Cslib.DataStructure.Memory.Triple.map {w : ℕ} {α β : Type} {s : State} {c : ℕ} (f : α → β) {p : Prog α} {Q : β → State → Prop} (h : Triple w s p c fun (a : α) (s' : State) => Q (f a) s') :
Triple w s (f <$> p) c Q

Map the result of a memory program without additional cost.

theorem Cslib.DataStructure.Memory.Triple.wellFormed {w : ℕ} {α : Type} {s : State} {c : ℕ} {Q : α → State → Prop} {p : Prog α} (hwf : WellFormed w s) (h : Triple w s p c Q) :
Triple w s p c fun (a : α) (s' : State) => Q a s' ∧ WellFormed w s'

Any admissible Triple run preserves WellFormed w.

Memory that is zero above the frontier #

Every cell at or above the frontier holds 0, as in the word RAM's initial memory past the input. A dirty allocation from such a state hands out a zeroed block at the cost of one unit.

The input convention is zero above the frontier.

theorem Cslib.DataStructure.Memory.ZeroAbove.allocDirty {s : State} (h : ZeroAbove s) (k : ℕ) :
ZeroAbove { mem := s.mem, frontier := s.frontier + k }

A dirty allocation keeps memory zero above the new frontier.

theorem Cslib.DataStructure.Memory.ZeroAbove.write {s : State} (h : ZeroAbove s) {a v : ℕ} (ha : a < s.frontier) :
ZeroAbove { mem := Function.update s.mem a v, frontier := s.frontier }

Writing below the frontier preserves ZeroAbove.

theorem Cslib.DataStructure.Memory.ZeroAbove.alloc {s : State} (h : ZeroAbove s) (k : ℕ) :
ZeroAbove { mem := WordRAM.clear s.mem s.frontier k, frontier := s.frontier + k }

A zeroed allocation preserves ZeroAbove.

theorem Cslib.DataStructure.Memory.ZeroAbove.step {w : ℕ} {s : State} (h : ZeroAbove s) {o : Op} (hv : valid w s o) :

Every admissible single operation preserves ZeroAbove.

theorem Cslib.DataStructure.Memory.ZeroAbove.run {w : ℕ} {s : State} (h : ZeroAbove s) {os : List Op} (hv : (spec w).ValidHistory s os) :
ZeroAbove ((spec w).run s os).1

Every admissible history preserves ZeroAbove.

theorem Cslib.DataStructure.Memory.Triple.zeroAbove {w : ℕ} {α : Type} {s : State} {p : Prog α} {c : ℕ} {Q : α → State → Prop} (hz : ZeroAbove s) (h : Triple w s p c Q) :
Triple w s p c fun (a : α) (s' : State) => Q a s' ∧ ZeroAbove s'

Any Triple run preserves ZeroAbove.