agent: .aji Quick Reference (~3.6k tokens)

First entry for the condition Aji/source: skeleton, types, statements,
borrows and moves, outcome matches, authority/allocation/I/O table,
tasks, cancellation, channels, select, mutex, diagnostics-to-repair
table, index into the full reference. Consistent with spec 15 (one
fix there: send of a copyable value takes no move).
This commit is contained in:
dev committed 2026-10-05 10:37:37 +03:00
1 parent 65b10e3fb5
commit 8582346214
3 files changed
+233 -1

No files matched your search

+228
View File
@@ -0,0 +1,228 @@
# Aji Quick Reference (`.aji` 0.1)
Read this first; it covers most programs. The full rules live in the reference index at the end. `aji check file.aji` verifies, `aji run file.aji` verifies and runs, `aji lower file.aji` prints the AIR it becomes. Diagnostics are JSON lines; fix the first one first.
Aji makes every significant action explicit — moves, borrows, allocation, closing, authority, effects, tasks, channel transfers, locking, cancellation, every failure outcome — and checks them all before running. The compiler writes the mechanics: SSA, control flow, borrow ends, frame storage, the feature header.
## 1. Skeleton
```aji
module demo.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;
}
}
```
- `main`'s parameters are capabilities, granted by name: `aji run --grant input=io.read --grant out=io.write --stdin 'hi' main.aji`.
- Every function lists its effects after the signature. A missing effect is `E3101`, whose `suggested_effects` field is the full list to write.
- Statements end with `;`; `if`/`while`/`for`/`loop`/`match`/`select`/`scope`/`shield`/`lock` blocks do not.
## 2. Types
| Type | Notes |
|---|---|
| `u8 … u128`, `usize`, `i8 … i128`, `isize`, `bool` | no implicit conversion |
| `[T; N]` | array; `[e; N]` repeats `e` |
| `type P = struct { a: u8, b: u8 }` | `P { a: 1, b: 2 }`, `p.a` |
| `type E = enum { A, B(u64), C(u8, u8) }` | `E.B(4)`; `match` arms `A`, `B(x)`, `C(x, y)`, `_` |
| `ref<T>`, `mut<T>`, `slice<T>`, `slice_mut<T>` | borrows (§4) |
| `own<T>`, `own_array<T>`, `region` | heap and regions (§6) |
| `cap<K>` | authority; kinds below |
| `file<read>`, `file<write>`, `file<append>`, `listener`, `stream` | linear resources |
| `task<T>`, `sender<T>`, `receiver<T>`, `mutex<T>` | concurrency (§7) |
| `io_error`, `alloc_error`, `cap_error` | built-in error enums |
**Linear** values (resources, `own`, `region`, `task`, endpoints, `mutex`) are consumed exactly once. Everything else that is a plain value is copyable.
## 3. Statements and expressions
```aji
let x = a + b; // immutable
var i: usize = 0; // variable; reassign with i = i + 1;
var buf: [u8; 16] = zero; // zero-initialized storage
if c { … } else if d { … } else { … }
while i < n { … }
for i in 0..n { … } // 0 ≤ i < n
loop { … break; … continue; … }
return v; trap; assert(c);
let y = if c { 1 } else { 2 }; // value-producing if/match: tail expression without `;`
```
| Operation | Meaning |
|---|---|
| `+ - * / %` | checked: overflow traps `T0003`, division by zero `T0004` |
| `+% -% *%` | wrapping |
| `& \| ^ << >>` | bitwise; shift ≥ width traps `T0005` |
| `== != < <= > >=`, `and or not` | comparisons, booleans |
| `x as T` | lossless widening only (else `E1002`) |
| `x as! T` | checked conversion, traps `T0006` |
| `x as% T` | wrapping conversion |
| `42`, `42u8`, `0xff`, `1_000` | literals; type from context or suffix (else `E7007`) |
| `b"text\n"` | byte string, `[u8; N]` |
There is one spelling per construct: no `+=`, no alternative syntax.
## 4. Borrows and moves
```aji
var data: [u64; 8] = zero;
let s = mut data; // exclusive borrow (here: slice_mut<u64>)
let e = mut s[i]; // element borrow; out of range traps T0008
*e = 7; // store through mut
let v = *e; // load
let n = s.len;
let (l, r) = s.split_at(4); // two disjoint halves
copy(dst, ref src); // element-wise copy, same element type
let first = ref p.a; // field borrow; r.f reads a field through a borrow
```
Rules (checked by the verifier):
- **A borrow lives until the end of the block where it was `let`-bound.** A borrow written directly as an argument lives until the end of the statement. To end one early, put it in an inner `{ … }` block.
- **Exclusive XOR shared.** While a `mut` borrow of a place is live, no other borrow of the same place, and no direct use of it, is allowed (`E2101`). Disjoint fields and `split_at` halves are independent.
- A borrow cannot outlive what it borrows from (`E2102`). A function returns a borrow only with `-> ref<T> from param`.
- **Moves are explicit:** a named linear value is consumed only as `move x` (otherwise `E7003`). A fresh temporary needs no `move`: `consume(make())`. Copyable values are passed plainly; `move` on them is `E7006`.
- Use after move is `E2001`, use after close/free/take `E3001`, a second consumption `E2002`, and a linear value never consumed `E3002`. Every path must consume it, including early `return`s.
- Index proofs: inside `if i < s.len { … }` the access `s[i]` needs no runtime check.
## 5. Failure: every outcome is a `match` arm
```aji
match fs.open_read(c, b"/data/in.txt") {
ok(f) => { … use f … ; fs.close(move f) or trap; }
err(e) => { return 1u8; }
}
let n = net.recv(conn, mut buf) or trap; // any non-ok outcome traps
```
- Arms are named after the outcomes and the `match` must cover all of them (`E7004`).
- `or trap` is the only shorthand: it takes the `ok` value and traps on anything else.
- Matching an `err(e)` of type `io_error`: `match e { not_found => …, _ => … }`.
- Closing consumes the resource on **both** outcomes.
## 6. Authority, allocation, I/O
Capabilities are only ever received (as `main` parameters, passed down as arguments) or narrowed. They are copyable.
| Kind (`cap<K>`) | Effect | Operations (all fallible unless noted) |
|---|---|---|
| `io.read` / `io.write` | same | `io.stdin_read(c, mut buf) → ok(n)`; `io.stdout_write(c, ref bytes)`, `io.stderr_write(c, ref bytes)` |
| `fs.read` / `fs.write` | same | `fs.open_read(c, path) → ok(f)`, `fs.create(c, path)`, `fs.open_append(c, path)`; `fs.read(f, mut buf) → ok(n)`; `fs.write_all(f, ref bytes)`; `fs.sync(f)`; `fs.close(move f)` |
| `net.listen` / `net.connect` | same; `net.recv` needs `net.read`, `net.send_all` needs `net.write` | `net.listen(c, b"127.0.0.1:7") → ok(l)`; `net.accept(l) → ok(s)`; `net.connect(c, addr) → ok(s)`; `net.recv(s, mut buf) → ok(n)` (0 = closed); `net.send_all(s, ref bytes)`; `net.close_stream(move s)`; `net.close_listener(move l)` |
| `clock.read`, `random.read` | same | `clock.monotonic(c)` (infallible, `u64`); `random.fill(c, mut buf)` (infallible) |
| `mem.alloc` | `mem.alloc` | `own.new(heap, v) → ok(o) / err(e)`; `own.array(heap, n, v)`; `own.take(move o)`, `own.free(move o)`; `region.create(heap) → ok(r)`; `region.new(r, v) → ok(p)`; `region.array(r, n, v)`; `region.reset(r)`; `region.destroy(move r)` |
- **Narrowing:** `cap.narrow.prefix(c, b"/data")` and `cap.narrow.budget(heap, 4096)`, each giving `ok(c2) / err(e)`.
- Resource operands (`f`, `s`, `l`, `r`, endpoints) are borrowed automatically for an operation; data borrows (`mut buf`, `ref bytes`) are always written.
- Memory budgets count live bytes, so freeing returns them. In the deterministic profile `mem.alloc` needs a grant with a budget: `--grant heap=mem.alloc:4096`.
## 7. Concurrency
```aji
let d = mut data; // data: [u64; 8]
scope s { // every task spawned in s must be joined before the block ends (E4301)
let (l, r) = d.split_at(4);
let a = task.spawn(s, square, l); // borrows are lent to the task until its join
let b = task.spawn(s, square, r);
let x = task.join(move a);
let y = task.join(move b);
}
```
- **Spawning:**
- effect `task.spawn`;
- a value moved into a task must be `Send`, and data shared by `ref` must be `Share` (`E4001`, `E4002`);
- two live tasks may not hold overlapping `mut` loans (`E4101`);
- while lent, a borrow cannot be ended or used conflictingly (`E4302`, `E4101`).
- **Cancellation:**
- `task.cancel(t)` only requests it;
- `task.cancelled()` reads the flag;
- `match task.check_cancel() { ok => …, cancelled => … }`;
- `match task.join_cancel(move t) { ok(v) => …, cancelled(t) => { task.cancel(t); let v = task.join(move t); … } }`;
- `shield { … }` defers cancellation.
- **Waiting** requires the effect `task.wait`. A wait completes immediately when it can; it takes `cancelled` only if it would otherwise wait.
- **Channels** (bounded; capacity 0 = rendezvous; several senders, one receiver):
```aji
match channel.make<u64>(2, heap) { // effect mem.alloc
ok(tx, rx) => {
let tx2 = channel.dup(tx);
match channel.send(tx, v) { ok => {} closed(v) => { … } cancelled(v) => { … } } // a linear v: `move v`
match channel.recv(rx) { ok(v) => { … } closed => { … } cancelled => { … } }
channel.close_sender(move tx);
match channel.close_receiver(move rx) { ok => {} pending(rx) => { … receive until closed, close again … } }
}
err(e) => { trap; }
}
```
- A value is the sender's again on `closed(v)` and `cancelled(v)`.
- The receiver sees `closed` only after the buffer is empty and every sender is closed.
- `close_receiver` with buffered values hands the receiver back as `pending(rx)`: drain it, then close again.
- **Select:**
```aji
select {
recv(rx_a) { ok(x) => { … } closed => { … } }
send(tx_b, move job) { ok => { … } closed(job) => { … } }
cancelled(job) => { … }
}
```
Among ready arms, the scheduler's seeded choice decides. Arms where `job` was not sent bind it back.
- **Mutex:**
```aji
let m = mutex.make(0u64);
lock ref m as count { // count: mut<u64>; unlocked at every exit of the block
*count = *count + 1;
} cancelled { … }
let total = mutex.take(move m); // needs no live borrow of m
```
Inside a `lock` block no operation may wait or spawn, and no function may be called whose effects include `task.wait` or `task.spawn` (`E4304`). One lock at a time, so lock-order deadlocks cannot happen.
## 8. Common diagnostics → repair
| Code | Meaning | Usual repair |
|---|---|---|
| E7001 | syntax | check `;`, braces, `=>` in `match` |
| E7002 | unknown name | spelling; declare the variable or function |
| E7003 | linear value consumed without `move` | write `move x` (or borrow it with `ref`/`mut` if it must stay) |
| E7004 | `match` not exhaustive | add the missing outcome/variant arm or `_` |
| E7005, E1001 | type mismatch | convert with `as`, `as!`, `as%`; check borrow vs value |
| E7007 | ambiguous literal | add a suffix (`0u64`) or a type annotation |
| E2101 | conflicting borrows | end the first borrow earlier with an inner `{ }` block |
| E2102 | borrow outlives its source | borrow later, or return `from param` |
| E2001, E3001, E2002 | used after move / after consume / twice | restructure so each linear value is consumed once on every path |
| E3002 | linear value leaked | consume it (close, free, take, join, send) on every path, including `return` |
| E3101 | effect not declared | copy `suggested_effects` into `effects [...]` |
| E3201 | wrong capability kind | pass the right `cap<K>` |
| E4001, E4002 | not Send / not Share | move owned data into tasks; share only `Share` data by `ref` |
| E4101 | data race with a task | lend disjoint parts (`split_at`, fields) or join first |
| E4301 | task not joined at scope end | `task.join(move t)` before the `scope` block ends |
| E4304 | wait inside `lock` | move the wait out of the `lock` block |
| T0001–T0009 | runtime trap | see `kind` and `source_location` |
## 9. Index into the full reference
| Topic | Section |
|---|---|
| source language, every construct | spec 15 |
| types, Send/Share | spec 02 §6; spec 04 §35 |
| borrows, frame storage | spec 08 §11; spec 15 §5, §9 |
| heap, regions, budgets | spec 03 §18–§20 |
| effects, capabilities, narrowing | spec 05 §9–§11; `reference/capabilities.md` |
| resources, I/O errors | spec 06 §6–§7; `reference/intrinsics.md` |
| tasks, loans, scheduler | spec 04 §33–§36 |
| waits, cancellation, channels, select, mutex | spec 04 §37–§41 |
| determinism, run records, profiles | spec 11 §7–§8 |
| all codes | `reference/error-codes.md` |