# 16. Standard Library ## 1. Contract The standard library SHOULD make CLI programs, storage components, network services and structured concurrent programs possible without external source dependencies. Scope and sequencing are tracked in `docs/plan/standard-library.md`; that roadmap does not imply that unimplemented APIs are available. Standard APIs MUST expose allocation, authority, effects, ownership transfer and recoverable failure in their public contracts. Safe library source MUST pass the ordinary verifier. Unsafe or foreign implementations MUST use an explicit audited trusted boundary under spec 13; they MUST NOT masquerade as verified source. The library MUST NOT obtain ambient host authority. APIs SHOULD offer one canonical operation for a common need, small documentation requirements, bounded memory behavior and stable repair guidance (spec 14 §8). Resource cleanup remains explicit. Pure byte/path transformations do not grant authority over files or sockets. ## 2. Bootstrap module registry The built-in registry contains `std.math` and `std.bytes`. Both are implemented in safe `.aji` with `effects []`. Spec 15 §13 defines imports; the reference API tables are in `reference/std.md`. All operations are deterministic in every profile, have no concurrency effects and allocate no heap memory. Ordinary interpreter fuel limits apply. ### 2.1 `std.math` - `min_u64(a: u64, b: u64) -> u64` returns the smaller operand. - `max_u64(a: u64, b: u64) -> u64` returns the larger operand. - `clamp_u64(value: u64, lower: u64, upper: u64) -> u64` returns `lower` below the range, `upper` above it, or `value` inside it. The inclusive range MUST satisfy `lower <= upper`; violation traps `T0002` before returning a value. These are monomorphic functions, not a generic arithmetic facility. Checked/wrapping operators retain their existing semantics (spec 02); importing this module does not alter them. ### 2.2 `std.bytes` Inputs are byte slices, not encoded text. No UTF-8 validation or locale interpretation occurs. Read-only parameters are `slice`. - `equal(a, b) -> bool`: true exactly when lengths and all bytes agree. - `compare(a, b) -> i8`: unsigned-byte lexicographic comparison; returns exactly `-1`, `0` or `1`. The shorter of two matching prefixes sorts first. - `find(data, byte: u8) -> usize`: index of the first occurrence; `data.len` when absent. A found index is valid only for the searched slice and exactly when the result is less than its length. Indexing retains the ordinary bounds-check rules (`T0008` for an absent result used as an index). - `starts_with(data, prefix) -> bool`, `ends_with(data, suffix) -> bool`: true for matching prefixes/suffixes, including empty ones; false when the candidate is longer than `data`. - `fill(dst: slice_mut, byte: u8) -> void`: writes every byte of `dst`, including zero writes for an empty slice. The library MUST NOT read or write outside a passed slice and MUST NOT retain or return any borrow. The verifier enforces existing shared/exclusive borrowing, lifetime and bounds rules; there is no library privilege or new transfer function. Read-only functions only read their slices; `fill` writes only its exclusive slice. None consumes an owner or resource. These operations MUST NOT trap for valid byte slices and scalar operands, apart from the execution environment's fuel limit. ### 2.3 Endian reads and writes in `std.bytes` The library provides `read_u16_le`, `read_u16_be`, `read_u32_le`, `read_u32_be`, `read_u64_le`, `read_u64_be`, and the corresponding `write_` functions. Width/order are part of the function name; no native-endian default exists. `le` is least-significant byte first; `be` is most-significant byte first. These functions operate on the beginning of the passed slice. To use an offset, pass an explicit sub-slice. There is no cursor state or alignment requirement. Public result enums: ```aji enum ReadU16 { ok(u16), short_input } enum ReadU32 { ok(u32), short_input } enum ReadU64 { ok(u64), short_input } enum WriteResult { ok, short_output } ``` These declarations belong to `std.bytes` and are named with that prefix (spec 15 §13). For width `W` and byte count `N = W / 8`: - `read_uW_ORDER(src: slice) -> std.bytes.ReadUW` MUST return `short_input` when `src.len < N`, without reading elements. Otherwise it MUST return `ok(value)` decoded from exactly the first N bytes. Extra bytes MUST NOT affect the result. `value = Σ src[i] × 2^(8i)` for LE and `Σ src[i] × 2^(8(N-1-i))` for BE, for `0 <= i < N`. - `write_uW_ORDER(dst: slice_mut, value: uW) -> std.bytes.WriteResult` MUST return `short_output` when `dst.len < N` and leave every destination byte unchanged. Otherwise it MUST write exactly the first N bytes in the selected order, leave the remaining bytes unchanged and return `ok`. Short slices are recoverable enum results, not traps. A successful zero value is distinct from a short input. These APIs declare `effects []`, allocate no heap, retain no borrow and consume no owner/resource. They have no concurrency or authority requirements beyond the ordinary slice borrow contract. All capacity checks precede all accesses/mutations. Valid slices and values MUST NOT produce bounds, arithmetic, cast or shift traps; interpreter fuel exhaustion remains an environment outcome. The verifier uses the existing enum payload, call, shared/exclusive borrow, lifetime and bounds rules. There is no new transfer function, runtime primitive or AIR feature. Ordinary enum construction and exhaustive `enum.switch` lower the result handling. Existing `E7002`, `E7004`, `E7005`, `E7006`, `E2101` cover invalid names/matches/calls and aliasing. The behavior is identical in deterministic and hosted profiles. Existing AIR and the existing nine library operations retain their semantics; imported source receives additional declarations pinned to the compiler version.