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

+21 -2
View File
@@ -1,8 +1,8 @@
# Standard library API
Normative contracts: spec 16. Import rules: spec 15 §13. These modules are bundled with the compiler, with no external source dependencies. Allocation authority is explicit for std.vec new/reserve.
Normative contracts: spec 16. Import rules: spec 15 §13. These modules are bundled with the compiler, with no external source dependencies. Allocation authority is explicit for std.vec/std.deque new/reserve.
Except std.vec new/reserve, functions below declare `effects []`, allocate no heap memory and retain no borrows. Pass arrays/slices using explicit `ref` or `mut`. Names are fully qualified; no alias or short-name import exists.
Except std.vec/std.deque new/reserve, functions below declare `effects []`, allocate no heap memory and retain no borrows. Pass arrays/slices using explicit `ref` or `mut`. Names are fully qualified; no alias or short-name import exists.
| Module | Function signature | Result/failure |
|---|---|---|
@@ -244,3 +244,22 @@ Import std.vec; every function supplies explicit `::<T>`. Vec<T> contains a buff
| replace<T> | value: mut<Vec<T>>, index: usize, item: T | ReplaceResult<T>: ok(old_value: T) / out_of_bounds(original_item: T) |
Growth has an old+new capacity allocation peak. Failure preserves capacity/length/elements; full push and out-of-bounds replacement return the exact input. Nonempty free returns the vector; pop/consume all elements before retrying. get returns none out of bounds, replace returns the old complete element on success. Generic moves compose Copy and linear types. Qualified variants require direct imports (including std.option for pop/get). [Executable owning vector](../../examples/std/vector.aji) returns 42 with heap=mem.alloc:256. Views and nested element borrows cannot escape or overlap shape changes. Normative contracts: spec 16 §2.10 and spec 03 §26.
## Owning deques
Import std.deque; every operation supplies `::<T>`. Deque<T> is always linear and has bounded ring capacity. Pop transfers complete values. No operation grows except explicit reserve. New/reserve declare mem.alloc and take explicit authority; all others declare effects [].
| Function | Parameters | Result |
|---|---|---|
| new<T> | capacity: usize, heap: cap<mem.alloc> | NewResult<T>: ok(Deque<T>) / err(alloc_error) |
| len<T> / capacity<T> | value: ref<Deque<T>> | usize |
| reserve<T> | value: mut<Deque<T>>, capacity: usize, heap: cap<mem.alloc> | ReserveResult: ok / err(alloc_error) |
| push_front<T> / push_back<T> | value: mut<Deque<T>>, item: T | PushResult<T>: ok / full(original_item: T) |
| pop_front<T> / pop_back<T> | value: mut<Deque<T>> | std.option.Option<T>: some(T) / none |
| free<T> | value: Deque<T> | FreeResult<T>: ok / not_empty(Deque<T>) |
| get<T: Copy> | value: ref<Deque<T>>, index: usize | std.option.Option<T>: some(T) / none |
| replace<T> | value: mut<Deque<T>>, index: usize, item: T | ReplaceResult<T>: ok(old_item: T) / out_of_bounds(original_item: T) |
Indices count from the front. Full pushes, out-of-bounds replaces and nonempty free preserve the queue and return ownership. Allocation failure also preserves physical slots/head/length. Successful growth preserves logical order, resets head and charges the old+new capacities at the peak; no shrink occurs. Every slot is an Option<T> (tag plus payload), including free slots: capacity 3 of own<u64> charges 27 bytes. Empty free explicitly drains internal none slots; it never destroys a user's T. Callers pop and consume resources before freeing; match move on linear result values and import std.option to name pop/get variants.
End operations and indexing use constant slot work; construction/growth/free use O(capacity) work. Interpreter representation costs remain those of buffer primitives. Deque<T> fields are ordinarily visible; direct representation writes/construction must preserve spec 16 §2.11's ring invariant. Library calls maintain it. [Executable owning deque](../../examples/std/deque.aji) returns 42 with heap=mem.alloc:1024. There is no iterator, returned borrow, trait hierarchy or implicit allocation/cleanup. Existing alias, task-loan and Copy/Send/Share checks apply.