cli: add safe owning numeric-key maps
Register std.map with bounded U64Map<V>, exact key equality and explicit allocation/growth. Preserve returned ownership, old slots on reserve failure and original-authority refunds; support Copy values, owners and resources. Cover collision/tombstone/wrap behavior, independent dictionary traces, quota/cleanup/borrow/task rules, source/AIR parity and hosted/package CLI. Publish the API, roadmap and measured documentation cost. General key hashing/equality remains Q-037; numeric maps do not satisfy that gate.
This commit is contained in:
1 parent
fc23f1b23a
commit
57603860df
46 files changed
+2430
-15
No files matched your search
@@ -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.hash`, `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/hash.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.
|
||||
The built-in registry contains `std.bytes`, `std.encoding.base64`, `std.encoding.hex`, `std.fmt`, `std.hash`, `std.map`, `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/hash.aji`, `std/map.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/std.map declare effects [] and allocate no heap memory. std.vec/std.deque/std.map new/reserve declare mem.alloc and require explicit allocation authority (§2.10–§2.11, §2.13); their other functions allocate no heap. Ordinary interpreter fuel limits apply.
|
||||
|
||||
### 2.1 `std.math`
|
||||
|
||||
@@ -201,3 +201,30 @@ FNV MUST process each byte in slice order by XOR with its zero-extended value, t
|
||||
Empty input returns the supplied state/checksum; a fresh empty hash is the FNV offset basis or zero CRC. For any bytes A/B, extending hash(A) with B MUST equal hashing their concatenation. Any u64/u32 continuation value is valid; it need not come from a previous call. Input bytes including zero MUST all participate. No UTF-8 validation, terminator, implicit padding or field separator is introduced. The caller MUST encode field boundaries and scalar byte order when hashing structured data.
|
||||
|
||||
These operations MUST NOT allocate heap memory, mutate input, retain a borrow, obtain authority or consume an owner. They use ordinary slice bounds, immutable borrows and explicit wrapping arithmetic; no new verifier/runtime rule, diagnostic, AIR feature or unsafe boundary is introduced. Conflicting mutable access rejects under existing E2101; argument type errors use E7005 and missing imports/members E7002. Valid calls do not trap except ordinary execution limits. Time is O(data.len), auxiliary state O(1); the initial CRC implementation MAY use eight bit steps per byte rather than a lookup table. Deterministic and hosted profiles MUST return the same values, and existing APIs/AIR retain meaning. Hashes/checksums provide neither cryptographic authentication nor adversarial collision resistance and MUST NOT be used as key equality in a map.
|
||||
|
||||
### 2.13 `std.map`: bounded owning numeric-key map
|
||||
|
||||
The registry MUST include std.map, ordinary safe source. U64Map<V> stores exact u64 keys and complete V values; V MAY be Copy, an owner, file or endpoint subject to existing member/Send/Share rules. It does not implement general HashMap<K,V> hashing/equality (Q-037). Entry<V> has key: u64 and value: V. Ten operations use explicit `::<V>` specialization:
|
||||
|
||||
| Function | Result |
|
||||
|---|---|
|
||||
| `new<V>(capacity: usize, heap: cap<mem.alloc>)` | NewResult<V>: ok(U64Map<V>), err(alloc_error) |
|
||||
| `len<V>(map: ref<U64Map<V>>) -> usize` | live entries |
|
||||
| `capacity<V>(map: ref<U64Map<V>>) -> usize` | maximum live entries |
|
||||
| `reserve<V>(map: mut<U64Map<V>>, capacity: usize, heap: cap<mem.alloc>)` | ReserveResult: ok, err(alloc_error) |
|
||||
| `insert<V>(map: mut<U64Map<V>>, key: u64, value: V)` | InsertResult<V>: inserted, replaced(V), full(V) |
|
||||
| `contains<V>(map: ref<U64Map<V>>, key: u64) -> bool` | exact presence |
|
||||
| `get<V: Copy>(map: ref<U64Map<V>>, key: u64) -> std.option.Option<V>` | copy of value, or none |
|
||||
| `remove<V>(map: mut<U64Map<V>>, key: u64) -> std.option.Option<V>` | moving extraction, or none |
|
||||
| `pop<V>(map: mut<U64Map<V>>) -> std.option.Option<Entry<V>>` | first occupied physical slot, or none |
|
||||
| `free<V>(map: U64Map<V>)` | FreeResult<V>: ok, not_empty(U64Map<V>) |
|
||||
|
||||
Representation: U64Map<V> has storage: buffer<Slot<V>> and length: usize. Slot<V> has key: u64, state: SlotState and value: std.option.Option<V>; SlotState is the Copy enum empty/deleted/occupied. A logical capacity N uses exactly 2N initialized physical slots. buffer.len(storage) equals buffer.capacity(storage); length≤N equals occupied count; occupied slots contain some(V), other slots none; occupied keys are unique and reachable by the probing rule. Copy key/state fields are observable through ref without copying V. Ordinary visible fields have no special access protection; manual construction/mutation MUST preserve this invariant. Invalid representations retain ordinary ownership/bounds safety but need not preserve map behavior.
|
||||
|
||||
New creates all-empty slots. Only new/reserve declare effects [mem.alloc] and require cap<mem.alloc>; every other operation is effects [] and allocates no heap. Multiplication/charge overflow MUST return alloc_error.budget_exceeded before mutation. Each slot charges the key, SlotState tag and Option<V> tag plus maximum payload, including vacancies/tombstones; separately allocated V owners retain their own charges. Zero capacity succeeds with zero slots, no modulo; new-key insertion returns full(value). A key's hash is FNV-1a of exactly eight little-endian key bytes; the starting slot is that u64 hash modulo physical count. Probing visits at most the physical count and wraps without overflowing. Lookup passes deleted and stops at empty/end. Hash equality MUST NOT substitute for exact u64 equality; 0 and maximum u64 are valid keys.
|
||||
|
||||
Insert searches for an existing key before choosing its first deleted/empty slot. Existing-key replacement MUST succeed even at full logical capacity, returning the old complete V. A new key when full MUST return the exact input V without mutation. Deletion/pop exchange a complete Option<V> for none, mark deleted and return ownership; missing removal preserves all state. Tombstones need no implicit compaction. Tables with no empty slots MUST still terminate probes and reuse deleted slots without duplicate keys. pop order is physical-index order, deterministic for the complete history; successful rehash MAY change that order. No borrowed value or cursor escapes.
|
||||
|
||||
Reserve≤N is an unchanged no-op, retaining its explicit effect/capability/mut contract. Growth MUST allocate/initialize the entire new table before changing the original. Returned failure MUST preserve every old slot, metadata, key/value association and pop order. Peak quota includes both whole tables and existing user-owned allocations; old storage is refunded to its recorded authority even if a different capability funds growth. After successful allocation, transfer complete entries in old physical-index order, clear tombstones and explicitly free old internal vacant storage. No subsequent recoverable error is permitted; fuel/traps are abnormal outcomes outside returned-error preservation. Nonempty free MUST return the entire map; empty free MUST consume all internal none values and free the storage, never normally discarding an occupied/owning payload, including with corrupted caller metadata. Callers MUST consume returned linear replacements/full/remove/pop/not_empty results; no hidden destructor closes user resources.
|
||||
|
||||
Existing aggregate/enum moves, value.swap, slice bounds, borrow/task-loan exclusion, Copy/Send/Share, effects and allocation accounting verify this library without map-specific rules, diagnostics, AIR instructions/features or runtime behavior. Preflight size failures construct alloc_error values through error_values (spec 05 §14); map adds no further semantic feature. Bad moves/leaks/aliases/tasks/Copy uses retain E7003/E2002/E3002/E2101/E4101/E7005 and Share restrictions E4002. Valid operations do not trap except execution limits. Deterministic/hosted profiles agree; existing source/AIR/API meanings remain unchanged. Lookup/insert/remove are O(N) worst-case slot work, pop/free O(N), growth O(N²) worst-case collision work. Initialized-prefix copying may increase interpreter host cost; no native throughput guarantee or adversarial collision bound is added.
|
||||
Reference in new issue
Block a user