Native implementations of refinements, one realized body per operation constructor #
MemoryBackend runs each memory operation as the static body of a primitive realization, chosen
by the operation's constructor. Backend does the same for any specification D refined to the
memory oracle by a Refinement D (Memory.spec w). One supplied realization witness per
constructor tag supplies the body; the refinement supplies correctness and memory charges.
A Certificate bundles a Realizes.Witness with its program, encodings, domain and local work.
Resource and code projections retain those supplied data. Certificate.ofRealizes supports
existing existential clients using classical choice; it does not identify the chosen resources
with a particular realization derivation. A
family C : ι → Certificate B over a finite type of tags fixes one register file, one body per
tag and one exchange factor (Backend.registers, Backend.code, Backend.factor), before every
width, refinement, runtime layout and argument. A Backend D C says which tag each operation of
D selects (tag), which arguments the tag's program receives (arg, which may include the
structure's runtime layout), and how the program's result gives the specified answer (answer).
The result encoding belongs to the tag, so an operation's arguments are never encoded in its
result; the native code never dispatches on the operation.
Backend.Implements connects a backend to a refinement: each method runs the selected program
and maps its result by answer, and from an admissible state the arguments are words and the
program's domain holds. Then Backend.implementation is a StateTransitionImpl D with a
functional representation: the memory satisfies the refinement's invariant and abstracts to the
state, and every register and cell holds a word. Instruction counts follow from the refinement's
amortized memory charges (Backend.amortized), and are constant per operation when those charges
are constant and need no potential (Backend.bound). Backends of two specifications run their
product side by side (Backend.sum), with tags in a sum type: the operation's Sum constructor
selects the family, and no native code dispatches on it.
As in MemoryBackend, the selected tag's arguments are installed in registers before its body
runs, uncharged, as a calling convention; decoding the answer executes no instruction. Bounds
count the body's instructions only. The fields arg and answer are unrestricted: arg is any
function of the operation and answer any function of the operation and the program's result.
Nothing requires them to be computable or charges for evaluating them, and the answer need not be
held in a register: Backend.exec requires it to be answer applied to a result whose encoding
the result registers hold.
Memory charges do not depend on the word width (totalCost_width, memoryCost_eq_totalCost),
so memoryCost, which is taken at width zero, is the charge that refinements state at width w.
Main definitions #
Certificate: a realization certificate with its program, encodings, domain and work.Backend.registers,Backend.frontier,Backend.code,Backend.factor: the register file, the body of each tag and one exchange factor.Backend.data: the native data holding a memory state.Backend.install: a tag's arguments installed in its argument registers.Backend: the tag, arguments and answer of each operation.Backend.Implements: a backend runs the methods of a refinement.Backend.sum: two backends side by side, for a product specification.Backend.extend: a backend with further tags, such as an allocation's.Backend.exec,Backend.implementation: native execution, and the native implementation.
Main results #
totalCost_width,clientHistory_width,memoryCost_eq_totalCost: memory charges do not depend on the word width.fits_appendWords: appended argument words fit when both parts do.Backend.exists_exec: the body of one tag runs its program on installed arguments.Backend.exists_exec_frame,Backend.exists_exec_live: retain the register frame and run directly from arguments already live in the source bank.Backend.install_eq_self: installation is the identity on actual live arguments.Backend.Implements.prod: side-by-side backends run a product of refinements.Backend.exec_iff,Backend.rep_iff: the execution relation and representation, unfolded.Backend.rep_data: the data holding an invariant state represents its abstraction.Backend.exec_memoryState: an operation leaves its method's final memory.Backend.amortized,Backend.bound: amortized and worst-case instruction bounds.
Memory charges and the word width #
Memory charges along a history do not depend on the word width.
The memory history of a client does not depend on the word width.
memoryCost is the memory charge of the client's history at every word width.
Certificates and backends #
A uniform realization certificate, bundled with the program it realizes, its argument and result encodings, its entry domain and its local work.
- Arg : Type
The argument type.
- Res : Type
The result type.
- k : ℕ
The number of argument words.
- l : ℕ
The number of result words.
The realized memory program.
The faithful argument encoding.
The faithful result encoding.
The entry domain.
The certified local work.
The supplied witness. Resource and code projections retain these data directly.
Package an existential realization using its classically selected witness. This fallback retains correctness, but supplies no concrete bounds on its selected scratch or factor.
How the operations of D run on the bodies of C: each operation selects a tag, passes
arguments to the tag's program and reads its answer from that program's result. Only the tag
selects code. The maps arg and answer are unrestricted: they need not be computable, and
evaluating them is not charged.
- tag : D.Op → ι
The tag of each operation, typically its constructor.
The arguments of the selected program, including any runtime layout.
The specified answer, read off the selected program's result.
Registers for the longest argument vector.
Registers for the largest result and scratch bank.
One register count, chosen before every width, refinement, layout and argument.
Register zero holds the allocation frontier.
The argument registers of tag i, from register one.
The result and scratch registers of tag i, after all argument registers.
Distinct argument coordinates use distinct source registers.
Distinct result/scratch coordinates use distinct registers.
Result/scratch coordinates at the same position agree across tags.
Native result/scratch writes never use the frontier register.
The static body of tag i.
One exchange factor for every tag.
Each tag's exchange factor is at most the common one.
The native data holding the memory state s: its cells, the frontier in its register, and
zero in every other register. A calling-convention state, not an allocation.
The data holding s abstracts to s.
The data holding a well-formed memory state is word-bounded.
Install arguments d of tag i in its argument registers. Installation is uncharged and
leaves memory and the frontier unchanged.
Installation leaves the memory and the frontier.
Installing arguments already present in their source registers changes nothing. This identity requires actual live words; it supplies no native argument-staging code.
Installing fitted argument words preserves bounded data.
The selected body's complete execution guarantee, including its register frame. Only the frontier and its result/scratch bank may change relative to installed arguments.
The body of one tag. From bounded data, with arguments that fit words, inside the tag's
domain and with an admissible memory history, the body of tag i runs its program on the
installed arguments within the certified bound at the common exchange factor. It leaves the
program's final memory and encoded result, and every register and cell a word.
Run the same fixed body directly from bounded data whose input words are already live. No installation or loader is executed. Safety and valid history remain explicit, and the register frame is relative to the actual initial data.
A backend runs the methods of R: each method is the selected program with its result
mapped by answer, and from an admissible state the arguments are words and the program's
domain holds.
- run (o : D.Op) (s : (DataStructure.Memory.spec w).State) : (DataStructure.Memory.spec w).runClient s (R.methods o) = Prod.map (S.answer o) id ((DataStructure.Memory.spec w).runClient s ((C (S.tag o)).prog (S.arg o)))
Each method runs the selected program and answers through
answer. - entry {e : DataStructure.Memory.State} (o : D.Op) : DataStructure.Memory.WellFormed w e → R.inv e → D.valid (R.abs e) o → (∀ (j : Fin (C (S.tag o)).k), Fits w ((C (S.tag o)).input (S.arg o) j)) ∧ (C (S.tag o)).safe w (S.arg o) e
From an admissible state, the arguments fit words and the program's domain holds.
Products #
Two backends side by side run the product specification: a left operation selects a left tag and a right operation a right tag.
Side-by-side backends run the product of two non-interfering refinements, whatever their
joint invariant J.
The backend with the tags of a second family added, such as a realized allocation's
(Backend.Allocation). Every operation selects the body it selects in S.
An extended backend runs the methods its original runs.
Native execution #
Install the selected program's arguments in its argument registers. Installation is uncharged and leaves memory and the frontier unchanged.
An operation runs the body of its tag from the prepared data, in the counted number of instructions, and its answer is read off the encoded result.
The execution relation, unfolded.
The native implementation: represented data holds a memory that satisfies the refinement's invariant and abstracts to the state, and every register and cell holds a word.
The representation, unfolded.
The data holding a well-formed memory state that satisfies the invariant represents its abstraction.
An operation leaves the memory and frontier of its method.
Amortized instructions. Memory charges with a potential, plus local work at most c,
give native instruction counts with the potential scaled by the exchange factor.
Worst-case instructions. Memory charges at most upper without a potential, plus local
work at most c, bound every operation's instructions by factor C * (upper + c + 1). With a
constant upper this is one constant for every width and refinement.