Files
aji/docs/spec/16-standard-library.md
T

3.3 KiB

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<u8>.

  • 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<u8>, 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.