Documentation

CslibFrontier.DataStructure.Memory.Alloc

Allocating data structures over the memory oracle #

A program that uses data structures over Memory.spec w first allocates their storage, then runs a client of their specification through their methods. A Memory.Allocator D w packages the first half for a specification D:

The structure's footprint lies in fresh cells, which the allocation and later operations take from the frontier, and in the allocated cells Allocator.owned that it takes over. A structure allocated from scratch takes over none. One that the input already holds, such as a graph given as adjacency arrays, takes over the cells holding it, and its precondition Allocator.Pre says that they do. Allocator.resident is the allocator of a framed structure already in memory, whose allocation does nothing.

Allocator.run allocates and then runs a client. Allocator.run_correct and Allocator.run_cost hold for every allocator and every client: a client admissible for the specification issues only admissible memory operations, answers as the specification does, and costs at most the allocation's cost, the potential once allocated, and the upper charges of its operations. A certificate Allocator.Amortized carries the cost side. Allocator.run_cost_isBigO exports a constant per-operation charge as O(m) in the number m of operations, for one allocator and start memory; the constant depends on the allocation's cost and the potential.

Allocators combine as their structures do.

Certificates combine along with them (Allocator.Amortized.prod, Allocator.Amortized.comp, Allocator.Amortized.resident). An allocation changes no allocated cell, and being admissible, no cell beyond its new frontier either (Memory.mem_clientState_of_frontier_le). So it keeps the other structures' preconditions, such as that the memory beyond the frontier is zero, and the cells they take over (Allocator.pre_fresh, Allocator.owned_fresh). The product's precondition (Allocator.ProdPre) asks in addition that the cells the two structures take over be disjoint. So A₁.prod (A₂.prod A₃) may combine structures allocated from scratch with structures that the input holds.

Main definitions #

Main results #

structure Cslib.DataStructure.Memory.Allocates {w : ℕ} {D : StateTransitionSpec} {H : Type} (alloc : Prog H) (methods : H → Methods D (spec w)) (init : D.State) (owned : Finset ℕ) (room cost : ℕ) (s : State) (M : Framed D w) :

What running the allocation program alloc from the memory s establishes for the framed refinement M: its operations are admissible and cost at most cost, it changes no allocated cell, and it sets up M with the methods of the handle it returns, holding at the abstract state init. The footprint lies in the allocated cells owned that the structure takes over and in fresh cells, and the frontier plus M's room is at most room beyond the old frontier.

Allocators. A memory program alloc that allocates a structure implementing D, and the structure's methods, given the handle that alloc returns. From a memory satisfying Pre in which room cells beyond the frontier fit a word, the allocation sets up the framed refinement framed s, which represents init, at a memory cost of at most cost. Its footprint lies in fresh cells and in the allocated cells owned s that it takes over, such as an input that holds the structure already.

Runs #

Allocate the structure, then run a client of its specification through its methods.

The history of a run: the allocation's, then the linked client's.

The answer of a run is the linked client's.

A run ends where the linked client ends.

theorem Cslib.DataStructure.Memory.Allocator.run_correct {w : ℕ} {D : StateTransitionSpec} (A : Allocator D w) {fD : D.State → (o : D.Op) → D.State × D.Result o} [D.HasStepFn fD] {β : Type} (p : D.Client β) {s : State} (hs : A.Pre s) (hroom : WordRAM.Fits w (s.frontier + A.room)) (hv : D.ValidHistory A.init (D.clientHistory A.init p)) :
(spec w).ValidHistory s ((spec w).clientHistory s (A.run p)) ∧ (spec w).clientResult s (A.run p) = D.clientResult A.init p ∧ (A.framed s hs hroom).inv ((spec w).clientState s (A.run p)) ∧ (A.framed s hs hroom).abs ((spec w).clientState s (A.run p)) = D.clientState A.init p

Correctness of a run. From a memory with the allocator's precondition and room, a client admissible for the specification issues only admissible memory operations and answers as the specification does from A.init; the structure then holds at the specified final state.

Cost certificates #

def Cslib.DataStructure.Memory.Allocator.Amortized {w : ℕ} {D : StateTransitionSpec} (A : Allocator D w) (upper : D.State → D.Op → ℕ) (Φ₀ : ℕ) :

An amortized cost certificate of an allocator. From every memory it can allocate from, the structure it sets up is amortized against upper with a potential that reads only its footprint and is at most Φ₀ once allocated.

theorem Cslib.DataStructure.Memory.Allocator.run_cost {w : ℕ} {D : StateTransitionSpec} {A : Allocator D w} {fD : D.State → (o : D.Op) → D.State × D.Result o} [D.HasStepFn fD] {upper : D.State → D.Op → ℕ} {Φ₀ : ℕ} (hA : A.Amortized upper Φ₀) {β : Type} (p : D.Client β) {s : State} (hs : A.Pre s) (hroom : WordRAM.Fits w (s.frontier + A.room)) (hv : D.ValidHistory A.init (D.clientHistory A.init p)) :
(spec w).totalCost charge s ((spec w).clientHistory s (A.run p)) ≤ A.cost + Φ₀ + D.totalCost upper A.init (D.clientHistory A.init p)

The cost of a run. A client admissible for the specification costs at most the allocation's cost, the potential once allocated, and the upper charges of its operations.

theorem Cslib.DataStructure.Memory.Allocator.run_cost_le {w : ℕ} {D : StateTransitionSpec} {A : Allocator D w} {fD : D.State → (o : D.Op) → D.State × D.Result o} [D.HasStepFn fD] {K Φ₀ : ℕ} (hA : A.Amortized (fun (x : D.State) (x_1 : D.Op) => K) Φ₀) {β : Type} (p : D.Client β) {s : State} (hs : A.Pre s) (hroom : WordRAM.Fits w (s.frontier + A.room)) (hv : D.ValidHistory A.init (D.clientHistory A.init p)) :
(spec w).totalCost charge s ((spec w).clientHistory s (A.run p)) ≤ A.cost + Φ₀ + K * (D.clientHistory A.init p).length

The cost of a run for a constant charge K per operation: at most the allocation's cost, the potential once allocated, and K per operation.

theorem Cslib.DataStructure.Memory.Allocator.run_cost_isBigO {w : ℕ} {D : StateTransitionSpec} {A : Allocator D w} {fD : D.State → (o : D.Op) → D.State × D.Result o} [D.HasStepFn fD] {K Φ₀ : ℕ} (hA : A.Amortized (fun (x : D.State) (x_1 : D.Op) => K) Φ₀) {β : Type} {s : State} (hs : A.Pre s) (hroom : WordRAM.Fits w (s.frontier + A.room)) :
CostIsBigO Filter.atTop (fun (m : ℕ) => ↑m) (fun (p : { p : D.Client β // D.ValidHistory A.init (D.clientHistory A.init p) }) => (D.clientHistory A.init ↑p).length) fun (p : { p : D.Client β // D.ValidHistory A.init (D.clientHistory A.init p) }) => (spec w).totalCost charge s ((spec w).clientHistory s (A.run ↑p))

The exported form. For a constant charge per operation, the memory cost of a run is O(m) in the number m of operations, over admissible clients of one allocator A from one memory s. The bound is A.cost + Φ₀ + K * m (run_cost_le), so the constant depends on A.cost and Φ₀.

Refinements on top #

A refinement on top of an allocated structure. The same allocation sets up the composite Memory.Framed.comp R M, whose methods link R's methods through the structure's, provided R's invariant holds of the allocated abstract state.

theorem Cslib.DataStructure.Memory.Allocator.Amortized.comp {w : ℕ} {D E : StateTransitionSpec} {R : Refinement D E} {A : Allocator E w} {hinit : R.inv A.init} {middle : E.State → E.Op → ℕ} {upper : D.State → D.Op → ℕ} {Φ : E.State → ℕ} {Ψ₀ : ℕ} (hR : R.Amortized middle upper Φ) (hA : A.Amortized middle Ψ₀) :
(Allocator.comp R A hinit).Amortized upper (Φ A.init + Ψ₀)

Potentials stack through a refinement on top. A certificate of R against the middle charges and one of the allocator against them give a certificate of the composite, with R's potential at the allocated state added.

Products #

def Cslib.DataStructure.Memory.Allocator.allocPair {w : ℕ} {D₁ D₂ : StateTransitionSpec} (A₁ : Allocator D₁ w) (A₂ : Allocator D₂ w) :
Prog (A₁.Handle × A₂.Handle)

Allocate both structures, one after the other.

theorem Cslib.DataStructure.Memory.Allocator.clientHistory_allocPair {w : ℕ} {D₁ D₂ : StateTransitionSpec} (A₁ : Allocator D₁ w) (A₂ : Allocator D₂ w) (s : State) :
(spec w).clientHistory s (A₁.allocPair A₂) = (spec w).clientHistory s A₁.alloc ++ (spec w).clientHistory ((spec w).clientState s A₁.alloc) A₂.alloc

Both allocations' histories, one after the other.

theorem Cslib.DataStructure.Memory.Allocator.clientState_allocPair {w : ℕ} {D₁ D₂ : StateTransitionSpec} (A₁ : Allocator D₁ w) (A₂ : Allocator D₂ w) (s : State) :
(spec w).clientState s (A₁.allocPair A₂) = (spec w).clientState ((spec w).clientState s A₁.alloc) A₂.alloc

The memory after both allocations.

theorem Cslib.DataStructure.Memory.Allocator.clientResult_allocPair {w : ℕ} {D₁ D₂ : StateTransitionSpec} (A₁ : Allocator D₁ w) (A₂ : Allocator D₂ w) (s : State) :
(spec w).clientResult s (A₁.allocPair A₂) = ((spec w).clientResult s A₁.alloc, (spec w).clientResult ((spec w).clientState s A₁.alloc) A₂.alloc)

Both handles.

theorem Cslib.DataStructure.Memory.Allocator.room_after {w : ℕ} {D₁ : StateTransitionSpec} {A₁ : Allocator D₁ w} {s : State} (hs₁ : A₁.Pre s) {r : ℕ} (hroom : WordRAM.Fits w (s.frontier + (A₁.room + r))) :

After allocating the first structure, room for both leaves room for the second.

theorem Cslib.DataStructure.Memory.Allocator.pre_after {w : ℕ} {D₁ D₂ : StateTransitionSpec} {A₁ : Allocator D₁ w} (A₂ : Allocator D₂ w) {s : State} (hs₁ : A₁.Pre s) (hroom : WordRAM.Fits w (s.frontier + (A₁.room + A₂.room))) (hs₂ : A₂.Pre s) :
A₂.Pre ((spec w).clientState s A₁.alloc)

After allocating the first structure, the second one's precondition still holds.

theorem Cslib.DataStructure.Memory.Allocator.owned_after {w : ℕ} {D₁ D₂ : StateTransitionSpec} {A₁ : Allocator D₁ w} (A₂ : Allocator D₂ w) {s : State} (hs₁ : A₁.Pre s) (hroom : WordRAM.Fits w (s.frontier + (A₁.room + A₂.room))) (hs₂ : A₂.Pre s) :
A₂.owned ((spec w).clientState s A₁.alloc) = A₂.owned s

After allocating the first structure, the second one takes over the same cells.

structure Cslib.DataStructure.Memory.Allocator.ProdPre {w : ℕ} {D₁ D₂ : StateTransitionSpec} (A₁ : Allocator D₁ w) (A₂ : Allocator D₂ w) (s : State) :

The precondition of a product of allocators: both preconditions, and the cells that the two structures take over are disjoint.

  • left : A₁.Pre s

    The first allocator's precondition.

  • right : A₂.Pre s

    The second allocator's precondition.

  • disjoint : Disjoint (A₁.owned s) (A₂.owned s)

    The two structures take over disjoint cells.

def Cslib.DataStructure.Memory.Allocator.prod {w : ℕ} {D₁ D₂ : StateTransitionSpec} (A₁ : Allocator D₁ w) (A₂ : Allocator D₂ w) :
Allocator (product D₁ D₂) w

Products of allocators. Allocate the two structures one after the other, and set up their product over one memory. The room and the cost add, and the product takes over the cells that either structure takes over.

@[simp]
theorem Cslib.DataStructure.Memory.Allocator.prod_methods {w : ℕ} {D₁ D₂ : StateTransitionSpec} (A₁ : Allocator D₁ w) (A₂ : Allocator D₂ w) (h₁ : A₁.Handle) (h₂ : A₂.Handle) :
(A₁.prod A₂).methods (h₁, h₂) = (A₁.methods h₁).prod (A₂.methods h₂)

The product's methods run each side's methods with its own handle.

@[simp]
theorem Cslib.DataStructure.Memory.Allocator.prod_init {w : ℕ} {D₁ D₂ : StateTransitionSpec} (A₁ : Allocator D₁ w) (A₂ : Allocator D₂ w) :
(A₁.prod A₂).init = (A₁.init, A₂.init)

The product sets up both abstract states.

@[simp]
theorem Cslib.DataStructure.Memory.Allocator.prod_room {w : ℕ} {D₁ D₂ : StateTransitionSpec} (A₁ : Allocator D₁ w) (A₂ : Allocator D₂ w) :
(A₁.prod A₂).room = A₁.room + A₂.room

The product's room is the sum of the rooms.

@[simp]
theorem Cslib.DataStructure.Memory.Allocator.prod_cost {w : ℕ} {D₁ D₂ : StateTransitionSpec} (A₁ : Allocator D₁ w) (A₂ : Allocator D₂ w) :
(A₁.prod A₂).cost = A₁.cost + A₂.cost

The product's allocation costs at most the sum of the costs.

@[simp]
theorem Cslib.DataStructure.Memory.Allocator.prod_owned {w : ℕ} {D₁ D₂ : StateTransitionSpec} (A₁ : Allocator D₁ w) (A₂ : Allocator D₂ w) (s : State) :
(A₁.prod A₂).owned s = A₁.owned s ∪ A₂.owned s

The product takes over the cells that either structure takes over.

theorem Cslib.DataStructure.Memory.Allocator.prod_pre_iff {w : ℕ} {D₁ D₂ : StateTransitionSpec} (A₁ : Allocator D₁ w) (A₂ : Allocator D₂ w) (s : State) :
(A₁.prod A₂).Pre s ↔ A₁.ProdPre A₂ s

The product's precondition.

theorem Cslib.DataStructure.Memory.Allocator.Amortized.prod {w : ℕ} {D₁ D₂ : StateTransitionSpec} {A₁ : Allocator D₁ w} {A₂ : Allocator D₂ w} {upper₁ : D₁.State → D₁.Op → ℕ} {upper₂ : D₂.State → D₂.Op → ℕ} {Φ₁ Φ₂ : ℕ} (hA₁ : A₁.Amortized upper₁ Φ₁) (hA₂ : A₂.Amortized upper₂ Φ₂) :
(A₁.prod A₂).Amortized (productCharge upper₁ upper₂) (Φ₁ + Φ₂)

Certificates add across a product of allocators: the charges componentwise, the potentials once allocated by sum.

Structures already in memory #

A structure already in memory, such as a graph that the input holds as adjacency arrays. Its allocation does nothing. It takes over the footprint of M, and needs M to hold, to represent init and to have at most room cells of room.

@[simp]
theorem Cslib.DataStructure.Memory.Allocator.resident_methods {w : ℕ} {D : StateTransitionSpec} (M : Framed D w) (init : D.State) (room : ℕ) (h : Unit) :
(resident M init room).methods h = M.methods

A structure already in memory runs M's methods.

@[simp]
theorem Cslib.DataStructure.Memory.Allocator.resident_init {w : ℕ} {D : StateTransitionSpec} (M : Framed D w) (init : D.State) (room : ℕ) :
(resident M init room).init = init

It represents init.

@[simp]
theorem Cslib.DataStructure.Memory.Allocator.resident_owned {w : ℕ} {D : StateTransitionSpec} (M : Framed D w) (init : D.State) (room : ℕ) (s : State) :
(resident M init room).owned s = M.footprint s

It takes over M's footprint.

@[simp]
theorem Cslib.DataStructure.Memory.Allocator.resident_cost {w : ℕ} {D : StateTransitionSpec} (M : Framed D w) (init : D.State) (room : ℕ) :
(resident M init room).cost = 0

Its allocation costs nothing.

theorem Cslib.DataStructure.Memory.Allocator.resident_pre_iff {w : ℕ} {D : StateTransitionSpec} (M : Framed D w) (init : D.State) (room : ℕ) (s : State) :
(resident M init room).Pre s ↔ M.inv s ∧ M.abs s = init ∧ M.room s ≤ room

Its precondition: M holds, represents init and has at most room cells of room.

theorem Cslib.DataStructure.Memory.Allocator.Amortized.resident {w : ℕ} {D : StateTransitionSpec} {M : Framed D w} {init : D.State} {room : ℕ} {upper : D.State → D.Op → ℕ} {Φ : State → ℕ} {Φ₀ : ℕ} (hc : M.Amortized charge upper Φ) (hl : M.Local Φ) (hΦ : ∀ (s : (spec w).State), M.inv s → M.abs s = init → Φ s ≤ Φ₀) :
(Allocator.resident M init room).Amortized upper Φ₀

The certificate of a structure already in memory is the structure's own: a potential that reads only its footprint and is at most Φ₀ wherever the structure represents init.