Owner decisions D1-D7 and the boundary rule (source may hide representation mechanics, never authority, allocation, ownership transfer, synchronization, external effects or recoverable failure). spec 15 normative: items, types, statements, expressions, places and borrows, explicit move (named values), outcome matches and 'or trap', scope/shield/lock blocks (lock lowering defined exactly), select, channels, allocation, frame storage, lexical borrow scopes, inferred features, one canonical spelling per construct. grammar.ebnf replaced; E7001-E7007; E3101 gains suggested_effects; spec 01 examples updated.
365 lines
19 KiB
Markdown
365 lines
19 KiB
Markdown
# RFC 0013: The `.aji` Source Language, Phase 6A
|
||
|
||
Status: Accepted (owner decisions D1–D7 and the boundary rule; normative text in spec 15)
|
||
Integrated into: spec 15 (new); `reference/grammar.ebnf`; `reference/error-codes.md` (E7001–E7007); spec 05 / verifier (`E3101` `suggested_effects`); spec README
|
||
Scope: source language `.aji` 0.1 for the semantics of AIR format 0 features of Phases 0–3; no change to AIR, the verifier or the runtime
|
||
Language version: 0.1
|
||
Supersedes: the grammar sketch in `reference/grammar.ebnf`
|
||
|
||
Decision context: the native smoke (`docs/evaluation/results/2026-10-05-native-smoke.md`) found that agents writing Aji pay mostly for **representation**, not semantics:
|
||
|
||
- SSA and CFG diagnostics outnumbered safety diagnostics about 3:1 for the weaker agent;
|
||
- strong agents produced no safety diagnostics at all;
|
||
- AIR solutions were about 3× the size of Rust;
|
||
- the 33k-token reference was read in full.
|
||
|
||
The owner brought Phase 6 forward as Phase 6A.
|
||
|
||
## Motivation
|
||
|
||
AIR is the normative, machine-facing format: SSA values, block parameters, explicit terminators, explicit `borrow.end`, a `features` header. Each of these is mechanical bookkeeping that a compiler does better than a model. None of them carries a safety decision. Writing them by hand costs agents tokens, repair cycles and output length, and it hides whether Aji's semantics help.
|
||
|
||
Phase 6A gives agents a source language whose only job is to remove that mechanical work:
|
||
|
||
> `.aji` removes mechanical work but never hides a semantically significant action.
|
||
|
||
The frontend lowers `.aji` to AIR, and the existing verifier checks that AIR. The safety guarantees are therefore exactly those of AIR format 0. The frontend is outside the trusted computing base (spec 01 §8, spec 00).
|
||
|
||
## Invariants
|
||
|
||
1. **Same semantics.** Every `.aji` program means exactly the AIR it lowers to. There is no source-only runtime behavior, no source-only type rule that admits a program the verifier would reject, and no feature beyond the published AIR features of Phases 0–3.
|
||
2. **Visible significant actions.** These stay written in the source at the point where they happen:
|
||
- move of a linear value;
|
||
- shared and exclusive borrow;
|
||
- heap and region allocation;
|
||
- resource consumption (close, free, take);
|
||
- capability use;
|
||
- the effect list;
|
||
- task spawn and join;
|
||
- channel send and receive;
|
||
- locking;
|
||
- cancellation points and shields;
|
||
- every fallible outcome.
|
||
3. **Invisible mechanics.** The frontend generates the rest:
|
||
- SSA values, block parameters and the CFG;
|
||
- the end of a borrow at the end of its lexical scope;
|
||
- frame storage for variables that are borrowed;
|
||
- the `features [...]` header (the closure of what is used);
|
||
- array and byte literals;
|
||
- intermediate values.
|
||
4. **Determinism.** Lowering is a pure function: the same source gives byte-identical canonical AIR.
|
||
5. **Traceability.** Every AIR instruction carries the source span it came from. Every verifier diagnostic on lowered AIR is reported at a source span, with the same code.
|
||
|
||
## 1. Lexical structure and items
|
||
|
||
The surface deliberately follows Rust and Zig where their meaning matches Aji. Agents bring strong priors for that syntax, and the source reference stays small. Where the meaning differs, Aji uses a keyword rather than a familiar symbol with an unfamiliar meaning.
|
||
|
||
```aji
|
||
module echo
|
||
|
||
type Pair = struct { a: u8, b: u8 }
|
||
type Shape = enum { Empty, Square(u64), Rect(u64, u64) }
|
||
|
||
fn main(input: cap<io.read>, out: cap<io.write>) -> void
|
||
effects [io.read, io.write]
|
||
{
|
||
...
|
||
}
|
||
```
|
||
|
||
- One module per file, `module <path>`. There are no imports in 6A.
|
||
- Line comments start with `//`.
|
||
- Types use the AIR spellings: `u8`…`i128`, `usize`, `isize`, `bool`, `void`, `never`, `ref<T>`, `mut<T>`, `slice<T>`, `slice_mut<T>`, `own<T>`, `own_array<T>`, `region`, `cap<K>`, `file<read>` and so on, `task<T>`, `sender<T>`, `receiver<T>`, `mutex<T>`, built-in enums. Two exceptions:
|
||
- arrays are written `[T; N]`;
|
||
- a function-bound type (`scope`, `shield`, `guard<T>`) is never written; it exists only through the block forms of §5.
|
||
- `effects [...]` is written exactly as in AIR (decision D5).
|
||
|
||
## 2. Values, variables and control flow
|
||
|
||
```aji
|
||
let n = a + b // immutable binding: an SSA value
|
||
var i: usize = 0 // a variable: SSA with block parameters, or frame storage if borrowed (§3)
|
||
i = i + 1
|
||
if i < n { ... } else { ... }
|
||
while i < n { ... }
|
||
loop { ... break ... continue ... }
|
||
match shape { Empty => ..., Square(s) => ..., Rect(w, h) => ... } // enum.switch, exhaustive
|
||
match code { 0 => ..., 1 => ..., _ => ... } // switch.<int>
|
||
return x
|
||
trap
|
||
assert(c)
|
||
```
|
||
|
||
Lowering: `if`, `while`, `loop` and `match` become blocks, `cbr`, `br`, `enum.switch` and `switch`. A `var` that is assigned on several paths becomes a block parameter at the join. The agent never writes SSA, a block or a terminator.
|
||
|
||
Arithmetic keeps AIR's explicit modes:
|
||
|
||
| Source | AIR |
|
||
|---|---|
|
||
| `a + b`, `a - b`, `a * b`, `a / b`, `a % b` | `.checked` (traps `T0003`/`T0004`) |
|
||
| `a +% b`, `a -% b`, `a *% b` | `.wrap` |
|
||
| `&`, `\|`, `^`, `<<`, `>>`, `==`, `!=`, `<`, `<=`, `>`, `>=`, `and`, `or`, `not` | bit, shift, compare and boolean operations |
|
||
| `x as T` | `cast.widen` (accepted only where widen is lossless) |
|
||
| `x as! T` | `cast.checked` (traps `T0006`) |
|
||
| `x as% T` | `cast.wrap` |
|
||
|
||
Structs and enums:
|
||
|
||
- `Pair { a: 1, b: 2 }`, `Shape.Square(4)`;
|
||
- `p.a` on a value is `struct.get`.
|
||
|
||
Integer literals take their type from context, or from a suffix (`1u8`).
|
||
|
||
## 3. Borrows and frame storage
|
||
|
||
```aji
|
||
var buf: [u8; 64] = zero // frame storage (local.alloc + local.zero)
|
||
let s = mut buf // exclusive borrow: borrow.mut, slice.make where a slice is needed
|
||
let r = ref buf // shared borrow
|
||
let x = *r // load
|
||
*s = v // store (through mut<T>)
|
||
let e = mut s[i] // borrow.index (checked, traps T0008), or .proven under a dominating `if i < s.len`
|
||
let (l, r) = s.split_at(k) // slice.split
|
||
let part = s[a..b] // slice.sub
|
||
copy(dst, src) // slice.copy
|
||
s.len // slice.len
|
||
let f = mut p.field // borrow.field
|
||
```
|
||
|
||
- **Borrows are created explicitly** with `ref` and `mut`, the same keywords as the borrow types.
|
||
- **A borrow ends at the end of the lexical block that binds it**, on every path out of the block: fall-through, `break`, `continue`, `return`. A temporary borrow inside an expression ends at the end of its statement. The frontend emits `borrow.end` in reverse order of creation. A borrow that must end earlier is put in an inner block `{ … }` (decision D2).
|
||
- **Frame storage is chosen by the frontend.** A `var` whose address is taken by `ref` or `mut` becomes `local<T>` storage in the entry block; all other variables are SSA (decision D4).
|
||
- **Returning a borrow** keeps the AIR rule: `fn first(s: slice<u8>) -> ref<u8> from s { … }`.
|
||
|
||
The verifier still checks every borrow. The frontend only removes the bookkeeping: an agent who writes a conflicting borrow gets `E2101` at the source span of the second borrow.
|
||
|
||
## 4. Ownership, resources, allocation and fallible operations
|
||
|
||
**Moves are explicit.** A linear value is consumed only by writing `move x` (decision D3):
|
||
|
||
```aji
|
||
close(move f)
|
||
let v = take(move o)
|
||
spawn(s, worker, move job)
|
||
```
|
||
|
||
Using a linear value without `move` is a source diagnostic. Using it after a move is `E2001`, after it was consumed `E3001`, a second consumption `E2002`, and never consuming it `E3002`. These are the AIR codes, reported at source spans.
|
||
|
||
**Fallible operations are `match` expressions over their AIR outcomes.** The arm names are the AIR edge roles, and the match must be exhaustive:
|
||
|
||
```aji
|
||
match io.stdin_read(input, mut buf) {
|
||
ok(n) => { ... }
|
||
err(e) => { trap }
|
||
}
|
||
|
||
let o = match own.new(heap, Pair { a: 1, b: 2 }) {
|
||
ok(o) => o,
|
||
err(e) => return Status.OutOfMemory,
|
||
}
|
||
|
||
let n = io.stdin_read(input, mut buf) or trap // shorthand: any non-ok outcome traps
|
||
```
|
||
|
||
`or trap` is the only shorthand, and it is explicit. 6A has no `?`: AIR format 0 has no `Result` type to propagate (open question O1).
|
||
|
||
**Intrinsics and allocation** keep their AIR names and operand order: `fs.open_read(c, path)`, `net.accept(mut l)`, `own.new(heap, v)`, `region.create(heap)`, `region.new(ref r, v)`, `cap.narrow.prefix(c, path)`.
|
||
|
||
**Byte literals.** `b"127.0.0.1:7"` is a `[u8; 11]` value. Used where a `slice<u8>` is expected, the frontend materializes it in frame storage. This removes byte-by-byte construction (the AIR TCP echo spends about 60 lines on an address).
|
||
|
||
## 5. Tasks, cancellation, channels, select and mutex
|
||
|
||
Block forms make the function-bound values of RFC 0011 and RFC 0012 lexical. Their structure is visible in the source, and the frontend emits the begin and end instructions:
|
||
|
||
```aji
|
||
scope s { // task.scope.begin … task.scope.end
|
||
let a = spawn(s, square, mut left) // task.spawn; lends `left` until the join
|
||
let b = spawn(s, square, mut right)
|
||
let x = join(move a) // task.join
|
||
let y = join(move b)
|
||
} // a task not joined here is E4301
|
||
|
||
shield { ... } // task.shield.begin … task.shield.end (nesting by construction)
|
||
|
||
match check_cancel() { ok => ..., cancelled => ... }
|
||
match join_cancel(move t) { ok(v) => ..., cancelled(t) => { cancel(t); let v = join(move t) } }
|
||
|
||
match channel.make<u64>(2, heap) { ok(tx, rx) => ..., err(e) => ... }
|
||
let tx2 = dup(ref tx)
|
||
match send(mut tx, move v) { ok => ..., closed(v) => ..., cancelled(v) => ... }
|
||
match recv(mut rx) { ok(v) => ..., closed => ..., cancelled => ... }
|
||
close_sender(move tx)
|
||
match close_receiver(move rx) { ok => ..., pending(rx) => ... }
|
||
|
||
select {
|
||
recv(mut a) { ok(x) => ..., closed => ... }
|
||
send(mut b, move job) { ok => ..., closed(job) => ... }
|
||
cancelled(job) => ...
|
||
}
|
||
|
||
let m = mutex(0u64) // mutex.make
|
||
lock ref m as counter { // mutex.lock; `counter: mut<u64>` (mutex.borrow)
|
||
*counter = *counter + 1
|
||
} cancelled { ... } // the lock's cancelled edge
|
||
let v = take_mutex(move m) // mutex.take
|
||
```
|
||
|
||
- **Values coming back on an edge are bound by name**, as in AIR, where they are block parameters. `closed(v)` rebinds the returned `v`. In `select`, the arms bind the send values that were not sent, in the order of their first appearance.
|
||
- **`lock … as x { … }`** unlocks at the end of the block on every path out of it (decision D1). The critical-section rule (`E4304`) is unchanged: a waiting operation inside the block is rejected at its source span.
|
||
- **`scope` and `shield` blocks** end their scope or shield at the end of the block. A task that is not joined is still `E4301`.
|
||
|
||
## 6. The `features` header and effects
|
||
|
||
The frontend computes the `features [...]` header: the dependency closure of the constructs a module uses. It always declares `effects`, so effect lists are checked. `E0006` therefore cannot arise from `.aji` source.
|
||
|
||
Effects stay declared by the programmer (decision D5). A missing effect is `E3101` at the source span of the operation. Its fields name the effect and the canonical effect list the function needs.
|
||
|
||
## 7. Diagnostics
|
||
|
||
| Diagnostic | Source |
|
||
|---|---|
|
||
| verifier diagnostics (`E1xxx`–`E6xxx`) | the lowered AIR, reported with the same code at the source span of the instruction, with an `air` field pointing at the AIR location |
|
||
| trap records | function, block and instruction plus the source span |
|
||
|
||
Source-only problems get codes in a new range, `E7xxx` (decision D7):
|
||
|
||
| Code | Kind | Meaning |
|
||
|---|---|---|
|
||
| E7001 | source.syntax | malformed `.aji` source |
|
||
| E7002 | source.unknown_name | undefined variable, function, type, field or variant |
|
||
| E7003 | source.missing_move | linear value used without `move` |
|
||
| E7004 | source.not_exhaustive | `match` over an outcome or enum misses an arm |
|
||
| E7005 | source.type_mismatch | an expression has the wrong type in source position (reported before lowering) |
|
||
|
||
## 8. Tooling
|
||
|
||
| Command | Does |
|
||
|---|---|
|
||
| `aji check file.aji` | parse, lower, verify; diagnostics at source spans |
|
||
| `aji run file.aji` | the same, then run, exactly like `aji run file.air` |
|
||
| `aji lower file.aji` | print the canonical AIR, for inspection and as the bridge to every AIR tool |
|
||
|
||
## 9. Agent Reference Bundle
|
||
|
||
The two conditions get separate bundles:
|
||
|
||
- **`Aji/source`** gets a source bundle: the `.aji` reference (this RFC's §1–§8 as normative text), the semantic rules agents must reason about (borrows, linearity, resources, effects and capabilities, tasks, channels, mutex, cancellation) stated in source terms, and the diagnostics table. It contains no AIR syntax.
|
||
- **`Aji/AIR`** keeps the current bundle.
|
||
|
||
Both start from a quick reference of 5–8k tokens with an index into the full material.
|
||
|
||
## Valid example
|
||
|
||
The echo program of the dev task, 13 lines against about 35 lines of AIR:
|
||
|
||
```aji
|
||
module echo
|
||
|
||
fn main(input: cap<io.read>, out: cap<io.write>) -> void
|
||
effects [io.read, io.write]
|
||
{
|
||
var buf: [u8; 64] = zero
|
||
loop {
|
||
let n = io.stdin_read(input, mut buf) or trap
|
||
if n == 0 { return }
|
||
io.stdout_write(out, ref buf[0..n]) or trap
|
||
}
|
||
}
|
||
```
|
||
|
||
## Rejected examples
|
||
|
||
```aji
|
||
let f = fs.open_read(c, b"log") or trap
|
||
read_all(f) // E7003: `f: file<read>` is linear; write `move f` or borrow it with `mut f`
|
||
```
|
||
|
||
```aji
|
||
lock ref m as v {
|
||
match recv(mut rx) { ... } // E4304 at this span: waiting while the lock is held
|
||
}
|
||
```
|
||
|
||
```aji
|
||
let r = mut buf
|
||
let s = ref buf // E2101 at this span: `buf` is exclusively borrowed by `r` (line …)
|
||
```
|
||
|
||
## Agent complexity impact
|
||
|
||
- **New concepts:** none in semantics. The source spellings of existing operations, lexical borrow scopes, block forms, outcome `match`, and `move`.
|
||
- **New verifier state:** none. The frontend has its own state: names, types, liveness, scopes.
|
||
- **Repairability:**
|
||
- SSA, CFG, block-arity and header diagnostics disappear;
|
||
- borrow-end bookkeeping (`E2105`, `E2106`) largely disappears;
|
||
- the remaining diagnostics are the semantic ones, at source spans.
|
||
- **Bundle:** a separate source bundle for the condition `Aji/source`. Target: about half of the AIR bundle, with a 5–8k quick reference.
|
||
- **Targets, measured on dev tasks:**
|
||
|
||
| Quantity | Target |
|
||
|---|---|
|
||
| source size | ≤ 1.5× Rust |
|
||
| output tokens | ≤ 2× Rust |
|
||
| hand-written SSA/CFG diagnostics | ≈ 0 |
|
||
| `E0006` | 0 |
|
||
|
||
## Compatibility
|
||
|
||
There is no change to AIR format 0, the verifier, the runtime or any published feature. `.aji` 0.1 is new. Spec 00 allows source syntax to change between 0.x releases.
|
||
|
||
## Spec integration (on acceptance)
|
||
|
||
- a new spec file `15-source-language.md` (normative for `.aji` 0.1);
|
||
- `reference/grammar.ebnf` replaced by the 6A grammar;
|
||
- `reference/error-codes.md` gains `E7001`–`E7005`;
|
||
- spec 01 §3–§5 examples updated to 6A syntax;
|
||
- `docs/agent/` gains the source bundle and the quick reference.
|
||
|
||
## Implementation (after acceptance)
|
||
|
||
The frontend lives in `src/frontend/`: `lex`, `parse`, `resolve`, `check`, `lower`, `spans`. Conformance gains a `.aji` twin for every Phase 0–3 run and reject test. A twin must give the same outcome or code, and the twins together are the exit criterion. Reference programs and examples are ported to `.aji`. Dev tasks M and C serve as the `Aji/AIR` vs `Aji/source` regression.
|
||
|
||
## Alternatives considered
|
||
|
||
- **Smarter AIR tooling, e.g. auto-inserted `borrow.end` or SSA repair:** keeps the agent in a compiler IR. The measured cost is the IR form itself.
|
||
- **Inferring moves and borrows Rust-style:** less text, but the significant actions become invisible, which invariant 2 forbids. Explicit `move`, `ref` and `mut` cost a few tokens and are where the reasoning is.
|
||
- **Inferring effects:** violates "effects are part of the public contract" (AGENTS.md).
|
||
- **A new surface unlike any known language:** wastes the agents' priors and makes the bundle bigger.
|
||
|
||
## Decisions (owner)
|
||
|
||
All seven accepted, with these refinements (normative in spec 15):
|
||
|
||
- **Boundary rule.** Source syntax may hide representation mechanics:
|
||
- SSA;
|
||
- the CFG and block arguments;
|
||
- `borrow.end`;
|
||
- `local.alloc`;
|
||
- `features [...]`.
|
||
|
||
It must not hide authority, allocation, ownership transfer, synchronization, external effects or recoverable failure.
|
||
- **D1 — lock block, accepted.** `lock ref m as x { … } cancelled { … }` is a syntactic lowering:
|
||
1. `mutex.lock`;
|
||
2. `mutex.borrow` bound as `x: mut<T>`;
|
||
3. the body;
|
||
4. `borrow.end` and `mutex.unlock` at every normal exit, including `return`, `break` and `continue` that leave the block.
|
||
|
||
Nothing happens on a trap, because there is no unwinding. The guard is never a source value.
|
||
- **D2 — lexical borrows, accepted.** A borrow ends at the end of its block. An earlier end needs an inner block. There is no last-use inference: lexical source lifetime maps to an exact explicit lifetime in AIR. NLL may be discussed separately if benchmarks show its cost.
|
||
- **D3 — explicit `move`, accepted.** It is required when consuming a **named** linear value. A temporary needs none: `consume(make_value())`.
|
||
- **D4 — compiler-managed frame storage, accepted.** There is no hidden heap allocation, and the frame-size limit and address stability are preserved.
|
||
- **D5 — explicit effects, accepted.** "Explicit effects, assisted bookkeeping": `E3101` carries `suggested_effects`, the complete sorted list the function needs.
|
||
- **D6 — Rust/Zig familiarity, accepted.** A familiar operation gets a familiar form, and different semantics get their own word or operator. Every construct has **one canonical spelling**, with a minimum of alternatives. 6A has no compound assignment (`+=`): `x = x + 1;` is the only form.
|
||
- **D7 — `E7xxx` for frontend diagnostics only, accepted.** It covers syntax, names, lowering, invalid and ambiguous constructs (`E7001`–`E7007`). Verifier findings keep their AIR codes, with `source_location` (primary) and `air_location` (metadata).
|
||
|
||
Further spec-15 choices made while writing the normative text:
|
||
|
||
- built-in operations are called by their AIR names (`channel.send`, `own.new`, `task.spawn`), a one-to-one map to AIR;
|
||
- resource and endpoint operands are borrowed for the duration of an operation, while data borrows are always written;
|
||
- a mutex is borrowed explicitly (`lock ref m`).
|
||
|
||
## Open questions
|
||
|
||
- **O1.** `?`-style propagation needs a result type in AIR (a later feature). 6A offers `match` and `or trap` only.
|
||
- **O2.** Methods and generics, for example `s.len` and `channel.make<u64>`, are spelled as fixed built-in forms in 6A; user-defined generics belong to the full Phase 6.
|