9.0 KiB
RFC 0016: Standard library bootstrap and built-in source imports
Status: Proposed
Scope: .aji source composition and safe pure standard modules; AIR format 0 unchanged
Language version: 0.1
Supersedes: —
This branch contains the proposed spec text, reference implementation and tests for review. Only the owner changes the RFC to Accepted; implementation on a work branch does not constitute acceptance.
Motivation
Reusable byte operations reduce repeated loop/borrow code and the reasoning burden of basic system software. The current frontend has no modules/import resolution or user generics, while linear aggregates are reserved. Beginning with safe non-owning byte functions makes progress without pretending that resource-owning collections already work.
The proposal improves implementation simplicity, deterministic behavior, auditability and machine repairability. Its value for Model Capability Compression is a hypothesis to measure, not a result inferred from smoke tasks. The full roadmap is docs/plan/standard-library.md.
Precise invariant
Imported standard source is ordinary code under the same verifier: immutable slices only read, exclusive slices only write, no borrow escapes, no resource is consumed, no authority is created, no hidden heap allocation occurs. Every source call lowers to a normal AIR call with a collision-free identifier.
Normative rule changes
The complete proposed text is integrated into spec 15 §2/§13, spec 16 and reference/grammar.ebnf/reference/std.md in this branch. It defines a closed compiler-pinned module registry, contextual import std.bytes;, fully qualified calls, lexicographic composition, collision-free name mangling, library source provenance and nine pure API contracts.
This proposal covers only std.bytes and std.math. It does not make general package imports, aliases, imported types or generic collections available. The whole imported module is lowered, so unused declarations also contribute to the feature closure. Dependency pruning is deferred until there is evidence that the simple strategy becomes costly.
Verifier implications
No new state, transfer functions, diagnostics or trusted exemptions. Calls, slices, alias checks, frame lifetime, bounds and initialization use the existing rules. Module/function limits apply to the combined module, including all standard declarations. Imported source cannot bypass verification.
Interpreter / runtime implications
No new runtime rule. Ordinary function calls execute pure operations under fuel. fill writes only caller-provided storage. No initialization on import, hidden cleanup, allocation or task creation. Invalid clamp bounds trap T0002 at the assertion in std/math.aji; valid byte slices do not trap due to library arithmetic or bounds.
AIR changes
None. There is no new feature registry entry or opcode: the source import is composition, not an AIR construct. Dots are not allowed in format-0 global identifiers; standard functions therefore use deterministic collision-free __aji_std_bytes_equal-style names, with numeric suffixes when needed. The compiler emits the existing feature closure. Existing AIR constructs, types and effects retain their frozen semantics.
Valid example
module example
import std.bytes;
fn main() -> u8
effects []
{
var buf: [u8; 4] = zero;
std.bytes.fill(mut buf, 65);
assert(std.bytes.equal(ref buf, b"AAAA"));
return 0;
}
Rejected example
module example
import std.bytes;
fn main() -> u8
effects []
{
var buf: [u8; 4] = zero;
ref saved = buf;
std.bytes.fill(mut buf, 65);
return 0;
}
The exclusive borrow conflicts with saved: E2101. Current verification reports it at both the borrowed argument and the call, as captured by the conformance test; no new rule or code is introduced. A wrong slice type/arity is E7005, an unknown import/member or absent import is E7002, a duplicate import is E7006, malformed import/type-argument syntax is E7001.
Diagnostics impact
No new code. Library diagnostics and traps use the stable bundled source path and original positions; AIR locations remain available. User diagnostics retain their existing file and source mapping. Source-mapping deduplication compares the file as well as code/line/column, so separate files cannot silently suppress one another.
Spec integration
- Spec 15 §2/§13: imports, resolution, lowering and source provenance.
- Spec 16: standard library contract and bootstrap modules.
- Reference grammar: import syntax; API reference: signatures/failure table.
- Source Reference Bundle: import rules in
language/syntax.md, APIs in the on-demandlibrary/std.md, navigation and a checked Quick Reference example.
The AIR bundle is unchanged; the functions need only existing AIR contracts. The spec remains authoritative after owner acceptance.
Compatibility and migration
AIR format 0 is unchanged, with no new instruction, type, verifier/runtime rule or interpretation. Programs without imports keep identical canonical AIR; the contextual word import still works as an ordinary local/function name. User functions/types with generated-name collisions are preserved and standard symbols get suffixes. A local binding named std does not change fully qualified standard resolution. No migration required.
General user imports, public types and generics need their own source specifications. Resource-owning collections require the independent vertical linear_aggregates RFC; this proposal cannot be used as permission to weaken ownership.
Agent complexity impact
- New concepts: compiler-bundled module import and fully qualified library call; nine named pure operations.
- New verifier state: none.
- New syntax forms: one import statement; qualified calls reuse existing dotted call expressions. No new AIR instruction.
- Interactions: existing call typing, frame/slice borrows, bounds, function naming, feature closure and source spans. No capability or effect is inferred away.
- Repairability: reuse checked functions instead of rewriting loops; absent import/type/alias mistakes reuse stable diagnostics.
finduses a length sentinel that callers must check. No benefit is claimed until measured. - Existing primitives suffice for every algorithm; source composition removes the need for manual source concatenation.
- Public generics are not invented; the three integer functions explicitly say
u64. The source-side budget includes this temporary monomorphic surface. - New source diagnostics: none; existing codes gain import/member cases.
Measured with zig build agent-reference and HF_HUB_OFFLINE=1 uv run --with tiktoken --with tokenizers tools/agent_bundle_tokens.py --source on the bootstrap branch:
| Group | cl100k before → after | o200k before → after | Qwen2.5 before → after |
|---|---|---|---|
Quick Reference (QUICK.md, includes navigation) |
5,511 → 5,777 (+4.8%) | 5,529 → 5,797 (+4.8%) | 5,664 → 5,948 (+5.0%) |
| Other source docs | 28,470 → 30,461 (+7.0%) | 28,552 → 30,546 (+7.0%) | 29,238 → 31,307 (+7.1%) |
| Full source docs without AIR | 33,981 → 36,238 (+6.6%) | 34,081 → 36,343 (+6.6%) | 34,902 → 37,255 (+6.7%) |
| AIR docs in the source bundle | 42,454 → 42,454 | 42,602 → 42,602 | 43,827 → 43,827 |
The new on-demand API page costs 1,346/1,347/1,406 tokens. Standalone AIR bundle size stays 160,797 bytes, 48 sections, 19 files. Growth is below the 10% warning threshold. Nine APIs replace repeated user loops without adding verifier state; later additions must remeasure rather than assuming this budget scales.
Branch checks: build, unit tests, Quick Reference extraction, full conformance (456 passed, zero failed/pending), formatting and source coverage (69/69) pass. Hosted file/stdin/TCP smoke tests pass. Evaluation harness self-tests pass (22 tests; one environment-dependent skip). The source run tests execute twice for determinism, and one std test compares against hand-written AIR. Boundary tests cover empty slices, embedded zero/high bytes, length ordering, repeated/absent search results, every u8 fill value, sub-slice mutation, u64 extremes and inverted clamp bounds. Collision tests exercise both user type/function names and the local std name; mapping and CLI record tests preserve library provenance.
Alternatives considered
Manual copying/concatenation makes library source and diagnostics hard to track. Hidden std intrinsics enlarge the trusted surface for ordinary algorithms. Implementing general packages and generics first delays pure reusable code; the closed two-module registry provides a bounded first increment while keeping those prerequisites explicit. Implicit prelude and aliases increase name-resolution choices and are omitted.
Unresolved questions
Q-026 records the import/distribution decision. Q-027 records the existing discrepancy between the spec and frontend for empty byte literals; tests pass zero-length slices with the existing ref storage[0..0] form. Typed schemas, crypto backend choice, TLS trust configuration, cancellation-safe container cleanup and extended runtime capabilities are separate roadmap gates.