rfc: 0014 accepted (revision 2) and integrated into the spec
Owner review: D1-D4, D7-D9 accepted; D5 frozen.release, no thaw; D6 arc.from_frozen (island -> frozen -> arc, one graph identity), ArcSafe (not Share alone; no arc<mutex>, no nested arc); D7 island Send, not Share; D10 islands -> heap, arc -> islands; added: never-reused graph identity, IslandSafe, the quota lifecycle, iref equality. Integrated: spec 02 §6 (IslandSafe, ArcSafe), 03 §12-§14 and new §22-§24 (instruction contracts, quota), 04 §13/§35/§38.1, 07a (features islands, arc), 15 §8 (layer 9 syntax, explicit container borrows), error codes (T0010, registered in src/diag.zig), type-properties. Phase 4 plan: P4-0, P4-1 done.
This commit is contained in:
1 parent
7d8f147d1d
commit
ca84ab4348
10 files changed
+346
-224
No files matched your search
@@ -123,7 +123,9 @@ The compiler derives:
|
||||
- `Send`: may be moved to another concurrent execution context;
|
||||
- `Share`: may be observed concurrently through immutable access;
|
||||
- `Freeze`: can be converted to deeply immutable state;
|
||||
- `Local`: tied to its current execution context.
|
||||
- `Local`: tied to its current execution context;
|
||||
- `IslandSafe`: may be stored in an island graph (spec 03 §22). Derived structurally: integers, `bool`, built-in enums and `iref<U>` are `IslandSafe`, and so are arrays, structs and enums of `IslandSafe` values. Borrows, owners, regions, graph owners (`island`, `frozen`, `arc`), resources, endpoints, `task`, `scope`, `shield`, `guard`, `mutex` and `cap<K>` are not;
|
||||
- `ArcSafe`: may be held by an `arc` (spec 03 §24). The last release destroys it by freeing memory only. In format 0 it is exactly `IslandSafe`.
|
||||
|
||||
These are semantic properties, not user-extensible traits in v0.1. Their derivation for the types of AIR format 0 is the table of spec 04 §35.
|
||||
|
||||
|
||||
@@ -193,69 +193,26 @@ For arrays/buffers the verifier may track initialized prefixes or ranges.
|
||||
|
||||
## 12. Island<T>
|
||||
|
||||
An `Island<T>` is a uniquely owned, closed, transferable graph of mutable memory.
|
||||
|
||||
An island consists of:
|
||||
|
||||
- a root object of type `T`;
|
||||
- one or more private allocation regions;
|
||||
- internal references between island objects.
|
||||
|
||||
Properties:
|
||||
|
||||
- move-only ownership;
|
||||
- mutable by the current owner;
|
||||
- no external mutable aliases;
|
||||
- no references from the island into ordinary foreign mutable memory, except explicitly modeled transferable capabilities;
|
||||
- transferable between tasks without deep copy.
|
||||
|
||||
### 12.1 Internal references
|
||||
|
||||
Island graphs use verifier-recognized `iref<T>` handles for graph edges.
|
||||
|
||||
`iref<T>`:
|
||||
|
||||
- may point only inside the same island;
|
||||
- may represent cycles;
|
||||
- cannot be dereferenced without access to the owning island;
|
||||
- cannot escape as a standalone safe reference.
|
||||
|
||||
### 12.2 Transfer
|
||||
|
||||
Moving an island into a channel or task transfers graph ownership.
|
||||
|
||||
After successful transfer the sender cannot access the island.
|
||||
|
||||
The operation is intended to be O(1) with respect to graph size.
|
||||
An `island<T>` is a uniquely owned, closed, mutable graph whose root has type `T`. Its objects are addressed by opaque `iref<U>` handles, which may form cycles. It holds only `IslandSafe` values (spec 02 §6), so it never points into mutable memory outside itself and carries no authority. It moves between tasks in O(1), and after the move the sender holds nothing that reaches it. In AIR: §22.
|
||||
|
||||
## 13. Frozen<T>
|
||||
|
||||
`Frozen<T>` is a deeply immutable graph.
|
||||
|
||||
An island may be irreversibly frozen:
|
||||
|
||||
```aji
|
||||
let immutable = island.freeze(move mutable_graph);
|
||||
```
|
||||
|
||||
After freeze, no mutable reference may ever be obtained through safe Aji.
|
||||
|
||||
`Frozen<T>` is `Share` and `Send` when contained external resources permit sharing.
|
||||
`island.freeze` turns an island into a `frozen<T>`. It is irreversible and O(1), and keeps the graph's identity. A `frozen<T>` is deeply immutable, has one owner, and is `Send` and `Share`, so it is lent to tasks by `ref`. No safe operation yields a mutable borrow of it. In AIR: §23.
|
||||
|
||||
## 14. Arc<T>
|
||||
|
||||
`Arc<T>` provides explicit shared ownership for `Share` values.
|
||||
`arc<T>` is explicitly counted shared ownership of an immutable graph. It is made from a frozen graph (`arc.from_frozen`) or from one `ArcSafe` value (`arc.new`). The count changes only at `arc.clone` and `arc.release`, and the last release frees memory and nothing else. No operation silently changes a count. Cycles belong inside a graph (`iref` edges), not between arcs. In AIR: §24.
|
||||
|
||||
Reference-count manipulation is explicit:
|
||||
The three modes of one graph, chosen by intent:
|
||||
|
||||
```aji
|
||||
let another = arc.clone(config);
|
||||
```text
|
||||
island<T> mutable, unique change it
|
||||
│ island.freeze O(1)
|
||||
frozen<T> immutable, unique owner, Share read it in parallel
|
||||
│ arc.from_frozen O(1)
|
||||
arc<T> immutable, shared ownership several independent owners
|
||||
```
|
||||
|
||||
No operation silently increments a reference count.
|
||||
|
||||
Aji does not recommend `Arc` for arbitrary cyclic object graphs. Cycles should normally be represented inside an `Island<T>` or through stable handles/indices.
|
||||
|
||||
## 15. Stable addresses and pinning
|
||||
|
||||
Region and island allocations are address-stable until reset/destruction.
|
||||
@@ -403,3 +360,70 @@ Allocations cannot contain borrows (borrows are second-class), so linked structu
|
||||
- Authority objects for `mem.alloc` carry `limit`, `used` and `parent`. Narrowing appends a child. Charges walk the parent chain.
|
||||
- `own<T>` is a cell holding a value; `own_array<T>` a cell holding an array of `n` values. A region keeps a generation counter and the bytes charged by its current generation.
|
||||
- In the `deterministic` profile memory is bounded by the mandatory budgets, so the interpreter's own memory use is bounded by a function of the module and its grants.
|
||||
|
||||
## 22. Islands in AIR (feature `islands`)
|
||||
|
||||
Decision record: RFC 0014. Depends on `heap` and its closure.
|
||||
|
||||
**Types.**
|
||||
|
||||
- `island<T>`: a graph owner; linear; `Send`, not `Share`.
|
||||
- `iref<T>`: an opaque `{graph identity, slot}`; copyable, `IslandSafe`, `Send`, `Share`.
|
||||
|
||||
Graph owners may be parameters, returns, block arguments, channel elements and mutex contents. They may not be aggregate members (`linear_aggregates` stays reserved).
|
||||
|
||||
**Identity.** A graph's identity comes from a per-execution counter and is never reused. A handle cannot be built from, cast to or inspected as an integer.
|
||||
|
||||
| Instruction | Operands → result | Ownership and memory | Effect | Verifier | Runtime / trap |
|
||||
|---|---|---|---|---|---|
|
||||
| `island.create %v, %mem, ok -> @ok, err -> @err` (terminator) | `T` (consumed), `cap<mem.alloc>`; `@ok(island<T>)`, `@err(alloc_error)` | `%v` is the root object; charges its size and the graph header | `mem.alloc` | `T: IslandSafe` (`E2111`, field `found`) | a new identity; over budget → `@err` |
|
||||
| `island.alloc %ib, %v, ok -> @ok, err -> @err` (terminator) | `mut<island<T>>`, `U` (consumed); `@ok(iref<U>)`, `@err(alloc_error)` | a new object; charges the graph's authority | `mem.alloc` | `U: IslandSafe` (`E2111`) | — |
|
||||
| `%r = island.root %ib` | `ref`/`mut<island<T>>` → `iref<T>` | — | — | — | the root object |
|
||||
| `%p = island.borrow %ib, %r` | `ref<island<T>>` → `ref<U>`; `mut<island<T>>` → `mut<U>`; `%r: iref<U>` | child borrow of `%ib` | — | ends before `%ib` (`E2102`); a `mut` child is exclusive: any other access through `%ib` while it lives is `E2101` | `T0010` if `%r` belongs to another graph |
|
||||
| `%f = island.freeze %i` | `island<T>` (consumed) → `frozen<T>` | same storage, identity and quota | — | — | O(1) |
|
||||
| `island.destroy %i` | `island<T>` (consumed) | frees the graph; returns its bytes | — | — | — |
|
||||
| `%c = iref.eq %a, %b` | `iref<U>`, `iref<U>` → `bool` | — | — | — | equal identity and slot; handles of different graphs are unequal, never a trap |
|
||||
|
||||
Objects are never freed one by one. Two irefs naming different objects are not treated as disjoint: a mutable access is exclusive for the whole island.
|
||||
|
||||
## 23. Frozen graphs in AIR (feature `islands`)
|
||||
|
||||
`frozen<T>`: a graph owner; linear; `Send`, `Share`. There is no thaw.
|
||||
|
||||
| Instruction | Operands → result | Ownership and memory | Verifier | Runtime / trap |
|
||||
|---|---|---|---|---|
|
||||
| `%r = frozen.root %fb` | `ref<frozen<T>>` → `iref<T>` | — | — | — |
|
||||
| `%p = frozen.borrow %fb, %r` | `ref<frozen<T>>`, `iref<U>` → `ref<U>` | child borrow | `E2102` | `T0010` |
|
||||
| `frozen.release %f` | `frozen<T>` (consumed) | frees the graph; returns its bytes | — | — |
|
||||
|
||||
## 24. Arc in AIR (feature `arc`)
|
||||
|
||||
Decision record: RFC 0014. Depends on `islands`.
|
||||
|
||||
`arc<T>`: a handle to a shared, immutable graph with root type `T`. Each handle is linear; the type is `Send` and `Share`.
|
||||
|
||||
| Instruction | Operands → result | Ownership and memory | Effect | Verifier | Runtime |
|
||||
|---|---|---|---|---|---|
|
||||
| `%a = arc.from_frozen %f` | `frozen<T>` (consumed) → `arc<T>` | same storage and identity; count 1; charges the count | — | — | O(1) |
|
||||
| `arc.new %v, %mem, ok -> @ok, err -> @err` (terminator) | `T` (consumed), `cap<mem.alloc>`; `@ok(arc<T>)`, `@err(alloc_error)` | a one-object graph; count 1 | `mem.alloc` | `T: ArcSafe` (`E2111`) | — |
|
||||
| `%a2 = arc.clone %ar` | `ref<arc<T>>` → `arc<T>` | count + 1 | — | — | synchronized by the runtime |
|
||||
| `arc.release %a` | `arc<T>` (consumed) | count − 1; the last release frees the graph and the count | — | — | synchronized; never fails |
|
||||
| `%r = arc.root %ar` | `ref<arc<T>>` → `iref<T>` | — | — | — | — |
|
||||
| `%p = arc.borrow %ar, %r` | `ref<arc<T>>`, `iref<U>` → `ref<U>` | child borrow | — | `E2102` | `T0010` |
|
||||
|
||||
The count is not a user atomic: no memory ordering is chosen by the program. Each count change appears in the execution trace (spec 11 §2).
|
||||
|
||||
**Quota (all three modes).** A graph charges the authority it was created with. The ownership mode does not change the budget.
|
||||
|
||||
| Operation | Live bytes |
|
||||
|---|---|
|
||||
| `island.create`, `island.alloc` | + the object |
|
||||
| `island.freeze` | unchanged |
|
||||
| `arc.new` | + the value and the count |
|
||||
| `arc.from_frozen` | + the count |
|
||||
| `arc.clone` | unchanged |
|
||||
| `arc.release`, not the last | unchanged |
|
||||
| `arc.release`, the last | − the graph and the count |
|
||||
| `frozen.release` | − the graph |
|
||||
| `island.destroy` | − the graph |
|
||||
|
||||
@@ -143,9 +143,7 @@ AIR realization: §38 (separate `sender<T>`/`receiver<T>` endpoints, MPSC).
|
||||
|
||||
## 13. Island messaging
|
||||
|
||||
`Island<T>` is the preferred representation for large mutable messages and object graphs.
|
||||
|
||||
Sending an island transfers ownership without exposing shared mutation.
|
||||
`island<T>` is the preferred representation for large mutable messages and object graphs. Sending an island transfers ownership in O(1) without exposing shared mutation. An immutable graph is shared as `frozen<T>` (lent by `ref`) or `arc<T>` (several owners): spec 03 §12–§14, §22–§24.
|
||||
|
||||
## 14. select
|
||||
|
||||
@@ -432,6 +430,9 @@ Example: two workers square the two halves of one buffer in parallel; both loans
|
||||
| `task<T>`, `scope`, `shield`, `guard<T>` | no (`Local`) | no |
|
||||
| `sender<T>`, `receiver<T>` | yes | no |
|
||||
| `mutex<T>` | if `T` is | if `T` is `Send` (the lock synchronizes access) |
|
||||
| `island<T>` | yes | no — its owner may mutate it; freeze to share |
|
||||
| `frozen<T>`, `arc<T>` | yes | yes |
|
||||
| `iref<T>` | yes | yes |
|
||||
| struct, enum, array | if every field/payload/element is | if every field/payload/element is |
|
||||
|
||||
So an aggregate holding capabilities is `Send` and `Share` exactly when its other members are (Q-014).
|
||||
@@ -581,7 +582,7 @@ Endpoints may be block arguments, function parameters and return values, and may
|
||||
- `T` contains no borrow, `scope`, `task<…>`, `shield` or `guard<…>` (`E1001`);
|
||||
- `T` contains no `sender<…>` or `receiver<…>`: reserved feature `channel_transfer` (`E0006`).
|
||||
|
||||
Linear elements (`own<T>`, `file<…>`, `stream`, …) are allowed.
|
||||
Linear elements (`own<T>`, `file<…>`, `stream`, `island<T>`, `frozen<T>`, `arc<T>`, …) are allowed.
|
||||
|
||||
### 38.2 Instructions
|
||||
|
||||
|
||||
@@ -269,6 +269,12 @@ Types `scope` and `task<T>` (linear, function-bound), instructions `task.scope.b
|
||||
|
||||
Every terminator of these features follows spec 07 §8.2: each named target receives exactly the parameters its contract lists.
|
||||
|
||||
## Features `islands`, `arc`
|
||||
|
||||
`islands`: types `island<T>`, `frozen<T>`, `iref<T>`, instructions `island.create`, `island.alloc`, `island.root`, `island.borrow`, `island.freeze`, `island.destroy`, `frozen.root`, `frozen.borrow`, `frozen.release`, `iref.eq`; `IslandSafe`, identity and `T0010`: spec 03 §22–§23.
|
||||
|
||||
`arc`: type `arc<T>`, instructions `arc.from_frozen`, `arc.new`, `arc.clone`, `arc.release`, `arc.root`, `arc.borrow`; `ArcSafe` and the quota lifecycle: spec 03 §24.
|
||||
|
||||
## Run result
|
||||
|
||||
`aji run` executes the function `@main`, whose parameters MUST all be capabilities, bound by name at instantiation (spec 05 §10.2); without the `capabilities` feature it takes no parameters. On return it reports `{"status":"returned","value":V}` where `V` renders the returned value as JSON:
|
||||
|
||||
@@ -342,6 +342,20 @@ region.reset(r);
|
||||
|
||||
Passing the region itself is `E7005`; the frontend never creates a temporary region borrow. `region.reset` and `region.destroy` end no borrow: a live allocation borrow at a reset or destroy is the verifier's `E2104`. The programmer narrows the scope.
|
||||
|
||||
**Islands, frozen graphs and arcs** (spec 03 §22–§24; layer 9). Types `island<T>`, `frozen<T>`, `arc<T>`, `iref<T>`.
|
||||
|
||||
| Source | AIR |
|
||||
|---|---|
|
||||
| `island.create(v, mem)`, `island.alloc(g, v)`, `arc.new(v, mem)` | outcome matches or `or trap` (`ok(…)`, `err(e: alloc_error)`) |
|
||||
| `island.root(g)`, `frozen.root(g)`, `arc.root(g)` | `island.root`, `frozen.root`, `arc.root` |
|
||||
| `ref x = island.at(g, r);` / `mut x = island.at(g, r);` | `island.borrow` (`mut` needs `g: mut<island<T>>`) |
|
||||
| `ref x = frozen.at(g, r);`, `ref x = arc.at(g, r);` | `frozen.borrow`, `arc.borrow` (`mut` is `E7005`) |
|
||||
| `island.freeze(move i)`, `island.destroy(move i)`, `frozen.release(move f)`, `arc.from_frozen(move f)`, `arc.release(move a)` | the same |
|
||||
| `arc.clone(a)` | `arc.clone` on a shared borrow of `a`, made for the operation |
|
||||
| `a == b`, `a != b` for `iref<T>` | `iref.eq` (then `not`) |
|
||||
|
||||
**Container borrows are explicit, as for regions.** `.root`, `.at` and `island.alloc` take a named borrow binding of the graph's owner (`mut g = isl;`), which outlives the borrows derived from it. Passing the owner itself is `E7005`, with a hint. A container borrow written inline is `E7006`. `island.at`, `frozen.at` and `arc.at` appear only as the place of a `ref`/`mut` binding (`E7006` elsewhere).
|
||||
|
||||
**Linear aggregates.** A struct or enum with a linear field (`own<T>`, `region`, a resource) needs the reserved feature `linear_aggregates`. The frontend lowers it as written and the AIR is rejected (`E0006`) until that feature is published.
|
||||
|
||||
A byte string or array used where a `slice<u8>` is expected is materialized in frame storage and borrowed for the operation.
|
||||
|
||||
Reference in new issue
Block a user