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 allocation program, which returns a handle such as a layout of named fields;
- the methods, given the handle;
- the abstract state the allocation sets up, the room it needs, and a bound on its cost;
- for each memory with that room, the framed refinement it sets up (
Allocator.framed), with the proof that the allocation sets it up (Memory.Allocates).
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.
Allocator.prodallocates two structures one after the other, and sets up their product over one memory (Memory.Framed.prod). Its room is the sum of the rooms, so it reserves both structures' growth. Iterated,A₁.prod (A₂.prod A₃)allocates the three structures of a client such as Dijkstra's algorithm, which runs overproduct PQ (product Dict Adj).Allocator.compputs a refinement on top, such as a queue over the pair of its stacks, withMemory.Framed.comp.
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 #
Memory.Allocates: what an allocation program establishes for the structure it sets up.Memory.Allocator: an allocation program, the methods it sets up, and their framed refinement.Memory.Allocator.run: allocate, then run a client.Memory.Allocator.prod,Memory.Allocator.comp: products and refinements on top.Memory.Allocator.ProdPre: the precondition of a product.Memory.Allocator.resident: a structure already in memory.Memory.Allocator.Amortized: an amortized cost certificate of an allocator.
Main results #
Memory.Allocator.run_correct: the run is admissible and answers as the specification.Memory.Allocator.run_cost,Memory.Allocator.run_cost_le: its memory cost.Memory.Allocator.run_cost_isBigO: the cost asO(m)for constant per-operation charges.Memory.Allocator.Amortized.prod,Memory.Allocator.Amortized.comp,Memory.Allocator.Amortized.resident: certificates combine, and carry over to structures already in memory.
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.
The allocation's operations are admissible.
The allocation costs at most
cost.The allocation changes no allocated cell.
The structure runs the methods of the returned handle.
The structure holds after the allocation.
It represents
init.- of_mem_footprint (a : ℕ) : a ∈ M.footprint ((spec w).clientState s alloc) → a ∈ owned ∨ s.frontier ≤ a
Its footprint lies in the cells it takes over and in fresh cells.
- room_le : ((spec w).clientState s alloc).frontier + M.room ((spec w).clientState s alloc) ≤ s.frontier + room
The allocation and the structure's reserved room take at most
roomcells.
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.
- Handle : Type
What the allocation returns, such as a layout of named fields.
The allocation program.
The structure's methods, given the handle.
- init : D.State
The abstract state the allocation sets up.
- room : ℕ
The cells the allocation takes and the room it reserves for growth.
- cost : ℕ
A bound on the memory cost of the allocation.
What the allocation needs of the memory besides room, such as the structure it takes over.
The allocated cells that the structure takes over instead of allocating them.
The cells taken over are allocated.
- pre_fresh {s s' : State} : self.Pre s → (∀ a < s.frontier, s'.mem a = s.mem a) → (∀ (a : ℕ), s'.frontier ≤ a → s'.mem a = s.mem a) → s.frontier ≤ s'.frontier → WordRAM.Fits w (s'.frontier + self.room) → self.Pre s'
Runs that keep every allocated cell and every cell beyond the new frontier and leave the room fitting, such as other allocations, keep the precondition.
- owned_fresh {s s' : State} : self.Pre s → (∀ a < s.frontier, s'.mem a = s.mem a) → (∀ (a : ℕ), s'.frontier ≤ a → s'.mem a = s.mem a) → s.frontier ≤ s'.frontier → WordRAM.Fits w (s'.frontier + self.room) → self.owned s' = self.owned s
Such runs keep the cells taken over.
The framed refinement that allocating from
ssets up.- allocates (s : State) (hs : self.Pre s) (hroom : WordRAM.Fits w (s.frontier + self.room)) : Allocates self.alloc self.methods self.init (self.owned s) self.room self.cost s (self.framed s hs hroom)
Allocating from
ssets it up.
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.
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 #
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.
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.
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.
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.
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 #
Allocate both structures, one after the other.
Both allocations' histories, one after the other.
The memory after both allocations.
Both handles.
After allocating the first structure, room for both leaves room for the second.
After allocating the first structure, the second one's precondition still holds.
After allocating the first structure, the second one takes over the same cells.
The precondition of a product of allocators: both preconditions, and the cells that the two structures take over are disjoint.
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.
The product's methods run each side's methods with its own handle.
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.
Its allocation costs nothing.
Its precondition: M holds, represents init and has at most room cells of room.
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.