Documentation

CslibFrontier.Computability.WordRAM.Realization.BackendFrame

Framed backends: frames, products over one memory, and allocation #

A backend that implements a framed refinement (Memory.Framed) inherits its frame. A native operation leaves its method's final memory (Backend.exec_memoryState), so it writes only the refinement's region, its footprint and the room reserved above the frontier (Backend.modifies). An assertion on other cells survives it (Backend.frame_heap), and so does every framed structure kept apart from it (Backend.frame). Backends of two framed refinements run their product over one memory (Implements.prod_framed). The product is framed again by the union of the footprints and the sum of the rooms, so products iterate, and Memory.Framed.amortized_prod adds their costs for Backend.amortized.

Allocation. An allocator (Memory.Allocator) sets a structure up in memory with a program that returns a handle, such as a layout of named fields. A Backend.Allocation names the tag whose body runs that program, and reads the handle off the body's result. After uncharged argument installation, the body runs from bounded data with the allocator's precondition and room within the certified bound. It returns the handle, writes only the room it takes above the frontier, and leaves data that the handle's backend represents at the allocator's initial state (Allocation.exec). The bound is the allocator's memory cost and the body's local work. When allocation reserves memory without clearing it (Memory.allocDirty), as the array stack does, both are constant.

Base-relative layouts. A family F : A.Handle → Backend D C uses one fixed finite certificate family C, whose bodies, register layout and exchange factor precede the runtime handle. The handle supplies arguments to those bodies. The selected tag (F h).tag o may depend on h. If the tag is handle-independent for each operation, that operation uses the same body across layouts. The general guarantee is a fixed finite code family; uniform local-work bounds must also hold across handles.

The frame is that of the memory-level refinement (Memory.Framed.region).

Main definitions #

Main results #

The frame of a native operation #

theorem Cslib.WordRAM.Realization.Backend.clientRun_exec {B : Basis} {ι : Type} {C : ι → Certificate B} {D : DataStructure.StateTransitionSpec} {w : ℕ} {S : Backend D C} {M : DataStructure.Memory.Framed D w} [Fintype ι] (hS : S.Implements M.toRefinement) {s : D.State} {x y : Data (registers C)} {o : D.Op} {a : D.Result o} {n : ℕ} (hr : (implementation hS).rep s x) (hv : D.valid s o) (he : S.exec w x o a y n) :

A native operation runs its method from the memory it holds to the memory it leaves.

theorem Cslib.WordRAM.Realization.Backend.modifies {B : Basis} {ι : Type} {C : ι → Certificate B} {D : DataStructure.StateTransitionSpec} {w : ℕ} {S : Backend D C} {M : DataStructure.Memory.Framed D w} [Fintype ι] (hS : S.Implements M.toRefinement) {s : D.State} {x y : Data (registers C)} {o : D.Op} {a : D.Result o} {n : ℕ} (hr : (implementation hS).rep s x) (hv : D.valid s o) (he : S.exec w x o a y n) :

The frame of a native operation. An operation of a backend that implements a framed refinement writes only the refinement's region: its footprint, and the room reserved above the frontier. Without room (Memory.Framed.region_eq_footprint) it writes only the footprint.

theorem Cslib.WordRAM.Realization.Backend.frame_heap {B : Basis} {ι : Type} {C : ι → Certificate B} {D : DataStructure.StateTransitionSpec} {w : ℕ} {S : Backend D C} {M : DataStructure.Memory.Framed D w} [Fintype ι] (hS : S.Implements M.toRefinement) {s : D.State} {x y : Data (registers C)} {o : D.Op} {a : D.Result o} {n : ℕ} (hr : (implementation hS).rep s x) (hv : D.valid s o) (he : S.exec w x o a y n) {Q : HeapAssertion} (hQ : Q.holds x.mem) (hd : Disjoint Q.footprint (M.region (memoryState (frontier C) x))) :

A native operation keeps every assertion on cells outside its structure's region.

theorem Cslib.WordRAM.Realization.Backend.frame {B : Basis} {ι : Type} {C : ι → Certificate B} {D : DataStructure.StateTransitionSpec} {w : ℕ} {S : Backend D C} {M : DataStructure.Memory.Framed D w} [Fintype ι] (hS : S.Implements M.toRefinement) {D' : DataStructure.StateTransitionSpec} (N : DataStructure.Memory.Framed D' w) {s : D.State} {x y : Data (registers C)} {o : D.Op} {a : D.Result o} {n : ℕ} (hr : (implementation hS).rep s x) (hv : D.valid s o) (he : S.exec w x o a y n) (hN : N.inv (memoryState (frontier C) x)) (hJ : M.Apart N (memoryState (frontier C) x)) :

A native operation keeps a structure apart from its own: the other structure's abstraction and invariant are unchanged, and the two stay apart, with disjoint footprints and room for both.

Products over one memory #

Products over one memory. Backends of two framed refinements run their product (Memory.Framed.prod), whose frame is the union of the footprints and the sum of the rooms.

theorem Cslib.WordRAM.Realization.Backend.modifies_inl {B : Basis} {ι : Type} {C : ι → Certificate B} {D : DataStructure.StateTransitionSpec} {w : ℕ} {S : Backend D C} {M : DataStructure.Memory.Framed D w} {ι' : Type} {C' : ι' → Certificate B} {D' : DataStructure.StateTransitionSpec} {S' : Backend D' C'} {M' : DataStructure.Memory.Framed D' w} [Fintype ι] [Fintype ι'] (hS : S.Implements M.toRefinement) (hS' : S'.Implements M'.toRefinement) {s : (DataStructure.product D D').State} {x y : Data (registers (Sum.elim C C'))} {o : D.Op} {a : (DataStructure.product D D').Result (Sum.inl o)} {n : ℕ} (hr : (implementation ⋯).rep s x) (hv : (DataStructure.product D D').valid s (Sum.inl o)) (he : (S.sum S').exec w x (Sum.inl o) a y n) :

Each side keeps its own frame. An operation of the left structure of a product writes only that structure's region.

theorem Cslib.WordRAM.Realization.Backend.modifies_inr {B : Basis} {ι : Type} {C : ι → Certificate B} {D : DataStructure.StateTransitionSpec} {w : ℕ} {S : Backend D C} {M : DataStructure.Memory.Framed D w} {ι' : Type} {C' : ι' → Certificate B} {D' : DataStructure.StateTransitionSpec} {S' : Backend D' C'} {M' : DataStructure.Memory.Framed D' w} [Fintype ι] [Fintype ι'] (hS : S.Implements M.toRefinement) (hS' : S'.Implements M'.toRefinement) {s : (DataStructure.product D D').State} {x y : Data (registers (Sum.elim C C'))} {o : D'.Op} {a : (DataStructure.product D D').Result (Sum.inr o)} {n : ℕ} (hr : (implementation ⋯).rep s x) (hv : (DataStructure.product D D').valid s (Sum.inr o)) (he : (S.sum S').exec w x (Sum.inr o) a y n) :

An operation of the right structure of a product writes only that structure's region.

Allocation #

A realized allocation for the allocator A: the body of tag tag, on arguments arg, runs the allocation program, and handle reads the allocated handle off its result. From memory the allocator can allocate from, the arguments fit words and the tag's domain holds.

theorem Cslib.WordRAM.Realization.Backend.Allocation.exec {B : Basis} {ι : Type} {C : ι → Certificate B} {D : DataStructure.StateTransitionSpec} {w : ℕ} [Fintype ι] {A : DataStructure.Memory.Allocator D w} (I : Allocation A C) {F : A.Handle → Backend D C} (hF : ∀ (s : DataStructure.Memory.State) (hs : A.Pre s) (hroom : Fits w (s.frontier + A.room)), (F ((DataStructure.Memory.spec w).clientResult s A.alloc)).Implements (A.framed s hs hroom).toRefinement) {x : Data (registers C)} (hx : Data.Bounded w x) (hs : A.Pre (memoryState (frontier C) x)) (hroom : Fits w ((memoryState (frontier C) x).frontier + A.room)) :
∃ (y : Data (registers C)) (n : ℕ) (b : (C I.tag).Res), Exec w (code C I.tag) (install C I.tag I.arg x) y n ∧ n ≤ factor C * (A.cost + (C I.tag).work I.arg (memoryState (frontier C) x) + 1) ∧ (∀ (j : Fin (C I.tag).l), y.regs (tmp C I.tag (Fin.castAdd ((C I.tag).realizes.scratchSize + 1) j)) = (C I.tag).output b j) ∧ I.handle b = (DataStructure.Memory.spec w).clientResult (memoryState (frontier C) x) A.alloc ∧ Modifies (Finset.Ico (memoryState (frontier C) x).frontier ((memoryState (frontier C) x).frontier + A.room)) x.mem y.mem ∧ (implementation ⋯).rep A.init y

Allocation from arbitrary memory. From bounded data whose memory the allocator can allocate from, with its room fitting a word, the allocation body runs within factor C * (A.cost + work + 1) instructions, where work is its local work. It returns the allocated handle in its result registers, writes only the room it takes above the frontier, and leaves data that the handle's backend represents at the allocator's initial state.

Execution starts after the uncharged install has supplied the argument registers. A raw-data initializer must separately pay native argument installation and any allocation-cursor setup.