cli: add safe owning deque standard module

Embed std.deque with eleven generic operations implemented entirely in
safe Aji over initialized Option<T> buffer slots. Bounded pushes and
moving pops work at both ends; full pushes, out-of-bounds replacements
and nonempty free return ownership to the caller. Explicit reserve
preserves order and all state on allocation failure, charges the old
and new slot capacities at the allocation peak, and rotates whole
values after successful growth. No AIR, verifier or runtime change.

Specify the visible representation invariant, tag-inclusive allocation
accounting and empty-only cleanup. Add RFC 0025, a runnable owning
example, source/CLI conformance and CI coverage. Complete STD-6c;
HashMap, HashSet and BinaryHeap remain STD-6d.

Validation: unit tests and 785 full conformance cases; 28 deque fixtures;
focused collection after every instruction and 12 schedule seeds over
four quanta; 136 source/package/hosted CLI checks; an independent Python
deque oracle for 128 operations and 71 small growth configurations;
93/93 source coverage; Zig formatting, staged whitespace and bundles.

Reference cost: full source excluding AIR grows 52,384 -> 53,993 cl100k
tokens (+3.1%); Quick Reference 7,667 -> 7,747 (+1.0%). AIR stays at
175,736 bytes. Construction/growth still incur interpreter prefix-copy
overhead; no native performance or empirical MCC benefit is claimed.
This commit is contained in:
dev committed 2026-10-07 00:08:42 +03:00
1 parent 4138a52af6
commit 93405f69ce
46 files changed
+1789 -7

No files matched your search

+25 -2
View File
@@ -10,7 +10,7 @@ APIs SHOULD offer one canonical operation for a common need, small documentation
## 2. Bootstrap module registry
The built-in registry contains `std.bytes`, `std.encoding.base64`, `std.encoding.hex`, `std.fmt`, `std.math`, `std.option`, `std.result`, `std.utf8` and `std.vec`. They are implemented in safe `.aji`; their stable source paths are `std/bytes.aji`, `std/encoding/base64.aji`, `std/encoding/hex.aji`, `std/fmt.aji`, `std/math.aji`, `std/option.aji`, `std/result.aji`, `std/utf8.aji` and `std/vec.aji`, respectively. Spec 15 §13 defines imports; the reference API tables are in `reference/std.md`. All operations are deterministic in every profile and have no concurrency effects. Modules other than std.vec declare effects [] and allocate no heap memory. std.vec new/reserve declare mem.alloc and require explicit allocation authority (§2.10); its other functions allocate no heap. Ordinary interpreter fuel limits apply.
The built-in registry contains `std.bytes`, `std.encoding.base64`, `std.encoding.hex`, `std.fmt`, `std.math`, `std.option`, `std.result`, `std.utf8`, `std.vec` and `std.deque`. They are implemented in safe `.aji`; their stable source paths are `std/bytes.aji`, `std/encoding/base64.aji`, `std/encoding/hex.aji`, `std/fmt.aji`, `std/math.aji`, `std/option.aji`, `std/result.aji`, `std/utf8.aji`, `std/vec.aji` and `std/deque.aji`, respectively. Spec 15 §13 defines imports; the reference API tables are in `reference/std.md`. All operations are deterministic in every profile and have no concurrency effects. Modules other than std.vec/std.deque declare effects [] and allocate no heap memory. std.vec/std.deque new/reserve declare mem.alloc and require explicit allocation authority (§2.10–§2.11); their other functions allocate no heap. Ordinary interpreter fuel limits apply.
### 2.1 `std.math`
@@ -160,4 +160,27 @@ Public functions use explicit `::<T>` specialization: new<T>(capacity:usize,heap
These are safe `.aji` wrappers over spec 03 §26. new chooses exact initial capacity, reserve chooses a minimum capacity without shrinking, and push never grows implicitly. len/capacity observe initialized length/backing capacity. pop removes the last complete value, returning none for empty. A full push returns the original item. get copies an in-bounds Copy item or returns none. replace exchanges an in-bounds initialized element and returns the previous item; out_of_bounds returns the original supplied item and preserves the vector. free releases only an empty vector; otherwise it returns the same vector. Returned owning result specializations need exhaustive match move; Copy results use ordinary matching. Callers drain and explicitly consume resource payloads before freeing.
Capacity/allocation failure, alias/task exclusion, deterministic profiles and diagnostics are those of buffer primitives and ordinary enum/call rules. Valid complete values include owners/files/endpoints; no collection bypasses Send/Share or channel_transfer gates. Imports grant no authority. Deque/HashMap/HashSet/BinaryHeap, owning strings and general borrowed function returns remain separate increments of STD-6.
Capacity/allocation failure, alias/task exclusion, deterministic profiles and diagnostics are those of buffer primitives and ordinary enum/call rules. Valid complete values include owners/files/endpoints; no collection bypasses Send/Share or channel_transfer gates. Imports grant no authority. HashMap/HashSet/BinaryHeap, owning strings and general borrowed function returns remain separate increments of STD-6.
### 2.11 Explicit owning deques
The compiler-pinned registry MUST include safe `std.deque` (`std/deque.aji`). Its ordinary type is `Deque<T> { storage: buffer<std.option.Option<T>>, head: usize, length: usize }`, always linear. It exports `NewResult<T> { ok(Deque<T>), err(alloc_error) }`, `PushResult<T> { ok, full(T) }`, `ReserveResult { ok, err(alloc_error) }`, `FreeResult<T> { ok, not_empty(Deque<T>) }` and `ReplaceResult<T> { ok(T), out_of_bounds(T) }`.
For a valid deque of capacity C, the backing buffer MUST have C initialized Option<T> slots. For C=0, head=length=0; otherwise head<C and length≤C, with head=0 when empty. Logical element i (0≤i<length) occupies `(head+i) mod C`; precisely those slots contain some(T), every other slot contains none. Each linear element is owned exactly once. Algorithms MUST compute indices without intermediate usize overflow. Fields follow ordinary source visibility: callers constructing or modifying the representation MUST preserve this invariant. Library operations preserve it; ordinary traps/bounds/ownership checks still apply to invalid caller representations, with no unsafe exemption or promise to validate the entire invariant on every call.
Every function supplies explicit `::<T>` specialization:
| Function | Parameters | Result / contract |
|---|---|---|
| new<T> | capacity: usize, heap: cap<mem.alloc> | NewResult<T>; exact capacity, empty queue |
| len<T> / capacity<T> | value: ref<Deque<T>> | usize; logical length / capacity |
| reserve<T> | value: mut<Deque<T>>, capacity: usize, heap: cap<mem.alloc> | ReserveResult; no shrink; successful growth preserves logical order and sets head=0 |
| push_front<T> / push_back<T> | value: mut<Deque<T>>, item: T | PushResult<T>; insert at selected end, or full(original_item) with entire deque unchanged |
| pop_front<T> / pop_back<T> | value: mut<Deque<T>> | std.option.Option<T>; remove/move selected end, or none with empty deque unchanged |
| free<T> | value: Deque<T> | FreeResult<T>; empty frees all backing storage; nonempty returns same deque unchanged |
| get<T: Copy> | value: ref<Deque<T>>, index: usize | std.option.Option<T>; copy logical element, or none out of bounds |
| replace<T> | value: mut<Deque<T>>, index: usize, item: T | ReplaceResult<T>; return old logical element after complete exchange, or out_of_bounds(original_item) without mutation |
Only new/reserve declare effects [mem.alloc] and require cap<mem.alloc>. Each slot charges accounted_size(Option<T>) (the enum tag plus its largest payload), including free none slots. Growth uses buffer.reserve's old+new allocation peak; an allocation error MUST preserve head, length, capacity, every slot and ownership. After successful allocation the library initializes new slots to none and rotates the old ring using complete value.swap operations, with no second allocation. Requests≤C succeed without mutation. New/growth perform O(capacity) slot work; end operations, get/replace and metadata use constant slot work; freeing an empty deque drains its none slots in O(capacity). These are library slot-operation counts; the reference interpreter currently copies the initialized prefix in buffer.push, so construction/growth can have higher host cost. No native backend performance guarantee is added. Fuel exhaustion/traps are abnormal execution outcomes, outside returned-error preservation guarantees.
A full push and out-of-bounds replace return the exact original item. Callers MUST consume returned linear payloads. A nonempty free MUST NOT pop, close or destroy user elements; callers pop/consume all elements first. Empty free explicitly consumes only internal none slots before freeing its buffer. Ownership never escapes through a borrowed result. Valid calls add no program trap beyond the environment's execution limits. Existing linear availability, borrow/task-loan, bounds, generic Copy/Send/Share, effects and allocation rules verify the library as ordinary source; no new verifier state, AIR feature/opcode, runtime primitive, diagnostic, effect or authority is added. AIR format 0 and existing APIs retain meaning. Owners, files and channel endpoints follow the same rules as Vec; map/set/heap collections remain separate work.