Phase 4: islands, frozen graphs and arc through every layer (RFC 0014)

AIR: types island<T>, frozen<T>, arc<T>, iref<T>; 16 instructions
(island.create/alloc/root/borrow/freeze/destroy, frozen.root/borrow/
release, arc.from_frozen/new/clone/release/root/borrow, iref.eq); features
islands (depends on heap) and arc (depends on islands) published.
Verifier: IslandSafe/ArcSafe (E2111 at the owner type), container child
borrows (one mut access per island), Send/Share rows (island not Share),
linear owners, mem.alloc effect, element rules (recursion through iref).
Interpreter: graphs with never-reused identities, T0010 for a foreign
iref, O(1) freeze/conversion/transfer, arc counts in a trace hook
(Options.arc_events), budgets per the quota table, collector support.
.aji layer 9: types, operations, island.at/frozen.at/arc.at as borrow
places with explicit container borrows, iref ==/!=.
Frontend fix: joins carry only variables the construct assigns (and that
are read again), so an unchanged linear value is never a block argument
while borrowed.

Tests: 10 AIR (phase4), 6 .aji run + 12 rejects (layer9), the reference
program island-pipeline in .aji, equivalent to an independently
hand-written AIR program (36 on 30 schedule seeds); unit tests for the
arc count trace and O(1) transfer. Quick Reference §7 graphs (checked
excerpt of quickref/graphs.aji); coverage 61/61. Bundle growth: AIR
+7.0%, Quick Reference +0.6k, source docs +4.2k (recorded).
Spec: quota rows match the header-reserved count; idioms and iref
recursion in spec 03 §22; P4-11 (air-text.ebnf gaps) recorded.
This commit is contained in:
dev committed 2026-10-05 22:14:05 +03:00
1 parent ca84ab4348
commit c699529fec
58 files changed
+2189 -98

No files matched your search

@@ -13,24 +13,24 @@ while_2(%i_4: u64):
cbr %cmp_5, @body_6(), @after_3()
after_3:
channel.close_sender %tx
%c_17 = const.u8 0
ret %c_17
%c_16 = const.u8 0
ret %c_16
body_6:
%tx_7 = borrow.mut %tx
channel.send %tx_7, %i_4, ok -> @ok_8, closed -> @closed_9, cancelled -> @cancelled_11
ok_8:
borrow.end %tx_7
br @endmatch_13(%i_4)
br @endmatch_13()
closed_9(%closed_10: u64):
borrow.end %tx_7
br @after_3()
cancelled_11(%cancelled_12: u64):
borrow.end %tx_7
br @after_3()
endmatch_13(%i_14: u64):
%c_15 = const.u64 1
%t_16 = add.checked.u64 %i_14, %c_15
br @while_2(%t_16)
endmatch_13:
%c_14 = const.u64 1
%t_15 = add.checked.u64 %i_4, %c_14
br @while_2(%t_15)
fn @main(%heap: cap<mem.alloc>) -> u64
effects [mem.alloc, task.spawn, task.wait]
@@ -49,32 +49,32 @@ loop_11(%acc_13: u64, %r_14: receiver<u64>):
%r_16 = borrow.mut %r_14
channel.recv %r_16, ok -> @ok_17, closed -> @closed_19, cancelled -> @cancelled_20
after_12(%acc_15: u64):
%r_37 = task.join %t_10
%r_35 = task.join %t_10
task.scope.end %sc_8
br @endmatch_38(%acc_15)
br @endmatch_36(%acc_15)
ok_17(%ok_18: u64):
borrow.end %r_16
%t_21 = add.checked.u64 %acc_13, %ok_18
br @endmatch_34(%t_21, %r_14)
br @endmatch_32(%t_21, %r_14)
closed_19:
borrow.end %r_16
channel.close_receiver %r_14, ok -> @ok_22, pending -> @pending_23
cancelled_20:
borrow.end %r_16
channel.close_receiver %r_14, ok -> @ok_28, pending -> @pending_29
channel.close_receiver %r_14, ok -> @ok_27, pending -> @pending_28
ok_22:
br @after_12(%acc_13)
pending_23(%pending_24: receiver<u64>):
br @endmatch_25(%acc_13, %pending_24)
endmatch_25(%acc_26: u64, %r_27: receiver<u64>):
br @endmatch_34(%acc_26, %r_27)
ok_28:
br @endmatch_25(%pending_24)
endmatch_25(%r_26: receiver<u64>):
br @endmatch_32(%acc_13, %r_26)
ok_27:
br @after_12(%acc_13)
pending_29(%pending_30: receiver<u64>):
br @endmatch_31(%acc_13, %pending_30)
endmatch_31(%acc_32: u64, %r_33: receiver<u64>):
br @endmatch_34(%acc_32, %r_33)
endmatch_34(%acc_35: u64, %r_36: receiver<u64>):
br @loop_11(%acc_35, %r_36)
endmatch_38(%match_39: u64):
ret %match_39
pending_28(%pending_29: receiver<u64>):
br @endmatch_30(%pending_29)
endmatch_30(%r_31: receiver<u64>):
br @endmatch_32(%acc_13, %r_31)
endmatch_32(%acc_33: u64, %r_34: receiver<u64>):
br @loop_11(%acc_33, %r_34)
endmatch_36(%match_37: u64):
ret %match_37
@@ -13,8 +13,8 @@ while_2(%i_4: u64):
%cmp_6 = ne.u64 %i_4, %c_5
cbr %cmp_6, @body_7(), @after_3()
after_3:
%c_20 = const.u8 0
ret %c_20
%c_19 = const.u8 0
ret %c_19
body_7:
mutex.lock %mr, ok -> @locked_8, cancelled -> @lock_cancelled_10
locked_8(%guard_9: guard<u64>):
@@ -25,14 +25,14 @@ locked_8(%guard_9: guard<u64>):
store %count_11, %t_14
borrow.end %count_11
mutex.unlock %guard_9
br @endlock_16(%i_4)
br @endlock_16()
lock_cancelled_10:
%c_15 = const.u8 0
ret %c_15
endlock_16(%i_17: u64):
%c_18 = const.u64 1
%t_19 = add.checked.u64 %i_17, %c_18
br @while_2(%t_19)
endlock_16:
%c_17 = const.u64 1
%t_18 = add.checked.u64 %i_4, %c_17
br @while_2(%t_18)
fn @main() -> u64
effects [task.spawn, task.wait]
@@ -0,0 +1,57 @@
// Arc handles: the frozen graph becomes shared; each worker gets its own handle through
// a channel and releases it; the last release frees the graph.
// expect: run
// grant: heap=mem.alloc:512
// schedules: 20
// result: 20
// phase: 6
// spec: docs/spec/03-memory-model.md
module demo.arcshared
fn reader(rx: receiver<arc<u64>>) -> u64
effects [task.wait]
{
var sum: u64 = 0;
loop {
match channel.recv(rx) {
ok(a) => {
{
ref ar = a;
let r = arc.root(ar);
ref v = arc.at(ar, r);
sum = sum + *v;
}
arc.release(move a);
}
closed => { break; }
cancelled => { break; }
}
}
channel.close_receiver(move rx) or trap;
sum
}
fn main(heap: cap<mem.alloc>) -> u64
effects [mem.alloc, task.spawn, task.wait]
{
let isl = island.create(10u64, heap) or trap;
let a = arc.from_frozen(island.freeze(move isl));
match channel.make<arc<u64>>(2, heap) {
ok(tx, rx) => {
var r: u64 = 0;
scope sc {
let t = task.spawn(sc, reader, move rx);
let b = arc.clone(a);
channel.send(tx, move b) or trap;
channel.send(tx, move a) or trap;
channel.close_sender(move tx);
r = task.join(move t);
}
r
}
err(e) => {
arc.release(move a);
trap;
}
}
}
@@ -0,0 +1,31 @@
// A graph charges the authority it was created with; island.alloc fails with alloc_error
// when the budget is spent, and destroy returns the bytes.
// expect: run
// grant: heap=mem.alloc:64
// result: 2
// phase: 6
// spec: docs/spec/03-memory-model.md
module demo.islandbudget
fn main(heap: cap<mem.alloc>) -> u64
effects [mem.alloc]
{
let isl = island.create([0u64; 4], heap) or trap;
var outcome: u64 = 0;
{
mut g = isl;
match island.alloc(g, [0u64; 4]) {
ok(r) => { outcome = 1; }
err(e) => {
outcome = match e {
alloc_error::budget_exceeded => 2,
alloc_error::out_of_memory => 3,
};
}
}
}
island.destroy(move isl);
let again = island.create([0u64; 4], heap) or trap;
island.destroy(move again);
outcome
}
@@ -0,0 +1,28 @@
// An iref of one island used with another traps T0010; equality across islands is false.
// expect: run
// grant: heap=mem.alloc:256
// trap: T0010
// phase: 6
// spec: docs/spec/03-memory-model.md
module demo.foreigniref
fn main(heap: cap<mem.alloc>) -> u64
effects [mem.alloc]
{
let a = island.create(1u64, heap) or trap;
let b = island.create(2u64, heap) or trap;
var out: u64 = 0;
{
ref ga = a;
ref gb = b;
let ra = island.root(ga);
let rb = island.root(gb);
if ra != rb {
ref wrong = island.at(gb, ra);
out = *wrong;
}
}
island.destroy(move a);
island.destroy(move b);
out
}
@@ -0,0 +1,34 @@
// A frozen graph is Share: two tasks read it through the same ref at once.
// expect: run
// grant: heap=mem.alloc:256
// schedules: 20
// result: 84
// phase: 6
// spec: docs/spec/03-memory-model.md
module demo.frozenreaders
fn read(f: ref<frozen<u64>>) -> u64
effects []
{
let r = frozen.root(f);
ref v = frozen.at(f, r);
*v
}
fn main(heap: cap<mem.alloc>) -> u64
effects [mem.alloc, task.spawn]
{
let isl = island.create(42u64, heap) or trap;
let f = island.freeze(move isl);
var sum: u64 = 0;
{
ref fr = f;
scope sc {
let a = task.spawn(sc, read, fr);
let b = task.spawn(sc, read, fr);
sum = task.join(move a) + task.join(move b);
}
}
frozen.release(move f);
sum
}
@@ -0,0 +1,13 @@
// An arc holds only ArcSafe values: a mutex is Share but not ArcSafe.
// expect: reject E2111
// phase: 6
// spec: docs/spec/03-memory-model.md
module demo.arc_mutex
fn main(heap: cap<mem.alloc>) -> u64
effects [mem.alloc]
{
let a = arc.new(mutex.make(0u64), heap) or trap;
arc.release(move a);
0
}
@@ -0,0 +1,19 @@
// A graph access is a borrow: it is bound with ref/mut, not used as a value.
// expect: reject E7006
// phase: 6
// spec: docs/spec/03-memory-model.md
module demo.at_as_value
fn main(heap: cap<mem.alloc>) -> u64
effects [mem.alloc]
{
let isl = island.create(1u64, heap) or trap;
var v: u64 = 0;
{
ref g = isl;
let r = island.root(g);
let x = island.at(g, r);
}
island.destroy(move isl);
v
}
@@ -0,0 +1,14 @@
// An island holds only IslandSafe values: an owner inside is E2111.
// expect: reject E2111
// phase: 6
// spec: docs/spec/03-memory-model.md
module demo.content_owner
fn main(heap: cap<mem.alloc>) -> u64
effects [mem.alloc]
{
let o = own.new(1u64, heap) or trap;
let isl = island.create(move o, heap) or trap;
island.destroy(move isl);
0
}
@@ -0,0 +1,14 @@
// Destroying an island while a borrow of it is live.
// expect: reject E2101
// phase: 6
// spec: docs/spec/03-memory-model.md
module demo.destroy_while_borrowed
fn main(heap: cap<mem.alloc>) -> u64
effects [mem.alloc]
{
let isl = island.create(1u64, heap) or trap;
ref g = isl;
island.destroy(move isl);
0
}
@@ -0,0 +1,19 @@
// A frozen graph gives ref access only.
// expect: reject E7005
// phase: 6
// spec: docs/spec/03-memory-model.md
module demo.frozen_mut
fn main(heap: cap<mem.alloc>) -> u64
effects [mem.alloc]
{
let f = island.freeze(island.create(1u64, heap) or trap);
{
ref g = f;
let r = frozen.root(g);
mut x = frozen.at(g, r);
*x = 2;
}
frozen.release(move f);
0
}
@@ -0,0 +1,20 @@
// The container borrow is bound first; written inline it would end before the access.
// expect: reject E7006
// phase: 6
// spec: docs/spec/03-memory-model.md
module demo.inline_container_borrow
fn main(heap: cap<mem.alloc>) -> u64
effects [mem.alloc]
{
let isl = island.create(1u64, heap) or trap;
var v: u64 = 0;
{
ref g = isl;
let r = island.root(g);
mut x = island.at(mut isl, r);
v = *x;
}
island.destroy(move isl);
v
}
@@ -0,0 +1,19 @@
// iref handles are compared with == and != only; they have no order.
// expect: reject E7005
// phase: 6
// spec: docs/spec/03-memory-model.md
module demo.iref_order
fn main(heap: cap<mem.alloc>) -> u64
effects [mem.alloc]
{
let isl = island.create(1u64, heap) or trap;
var v: u64 = 0;
{
ref g = isl;
let a = island.root(g);
if a < a { v = 1; }
}
island.destroy(move isl);
v
}
@@ -0,0 +1,12 @@
// A graph owner is consumed exactly once: an island never destroyed, frozen or moved leaks.
// expect: reject E3002
// phase: 6
// spec: docs/spec/03-memory-model.md
module demo.leak
fn main(heap: cap<mem.alloc>) -> u64
effects [mem.alloc]
{
let isl = island.create(1u64, heap) or trap;
0
}
@@ -0,0 +1,29 @@
// An island is not Share: its owner may mutate it. Freeze it to share it with tasks.
// expect: reject E4002
// phase: 6
// spec: docs/spec/03-memory-model.md
module demo.lend_island
fn read(g: ref<island<u64>>) -> u64
effects []
{
let r = island.root(g);
ref v = island.at(g, r);
*v
}
fn main(heap: cap<mem.alloc>) -> u64
effects [mem.alloc, task.spawn]
{
let isl = island.create(1u64, heap) or trap;
var v: u64 = 0;
{
ref g = isl;
scope sc {
let t = task.spawn(sc, read, g);
v = task.join(move t);
}
}
island.destroy(move isl);
v
}
@@ -0,0 +1,14 @@
// island.alloc takes the island's borrow binding, not the owner.
// expect: reject E7005
// phase: 6
// spec: docs/spec/03-memory-model.md
module demo.owner_not_borrow
fn main(heap: cap<mem.alloc>) -> u64
effects [mem.alloc]
{
let isl = island.create(1u64, heap) or trap;
let r = island.alloc(isl, 2u64) or trap;
island.destroy(move isl);
0
}
@@ -0,0 +1,20 @@
// One mutable access per island at a time, even through different irefs.
// expect: reject E2101
// phase: 6
// spec: docs/spec/03-memory-model.md
module demo.two_mut_access
fn main(heap: cap<mem.alloc>) -> u64
effects [mem.alloc]
{
let isl = island.create(1u64, heap) or trap;
{
mut g = isl;
let a = island.root(g);
let b = island.alloc(g, 2u64) or trap;
mut x = island.at(g, a);
mut y = island.at(g, b);
}
island.destroy(move isl);
0
}
@@ -0,0 +1,26 @@
// After an island is sent, the sender holds nothing that reaches it.
// expect: reject E2001
// phase: 6
// spec: docs/spec/03-memory-model.md
module demo.use_after_send
fn main(heap: cap<mem.alloc>) -> u64
effects [mem.alloc, task.wait]
{
match channel.make<island<u64>>(1, heap) {
ok(tx, rx) => {
let isl = island.create(1u64, heap) or trap;
channel.send(tx, move isl) or trap;
{
ref g = isl;
let r = island.root(g);
}
channel.close_sender(move tx);
let back = channel.recv(rx) or trap;
island.destroy(move back);
channel.close_receiver(move rx) or trap;
0
}
err(e) => { trap; }
}
}
+73
View File
@@ -0,0 +1,73 @@
// A cyclic ring of three nodes built in an island with iref edges, frozen in O(1),
// walked through a ref of the frozen graph, turned into an arc, cloned and released.
// expect: run
// grant: heap=mem.alloc:256
// result: 13
// phase: 6
// spec: docs/spec/03-memory-model.md
module demo.ring
struct Node {
value: u64,
next: Link,
}
enum Link {
End,
To(iref<Node>),
}
fn ring(heap: cap<mem.alloc>) -> island<Node>
effects [mem.alloc]
{
let isl = island.create(Node { value: 1, next: Link::End }, heap) or trap;
{
mut g = isl;
let a = island.root(g);
let b = island.alloc(g, Node { value: 2, next: Link::To(a) }) or trap;
let c = island.alloc(g, Node { value: 3, next: Link::To(b) }) or trap;
mut first = island.at(g, a);
first.next = Link::To(c);
}
move isl
}
fn walk(f: ref<frozen<Node>>, steps: u64) -> u64
effects []
{
var sum: u64 = 0;
var at: iref<Node> = frozen.root(f);
for i in 0..steps {
ref node = frozen.at(f, at);
sum = sum + node.value;
at = match node.next {
Link::End => at,
Link::To(n) => n,
};
}
sum
}
fn main(heap: cap<mem.alloc>) -> u64
effects [mem.alloc]
{
let isl = ring(heap);
let f = island.freeze(move isl);
var total: u64 = 0;
{
ref fr = f;
total = walk(fr, 7);
}
let a = arc.from_frozen(move f);
let b = arc.clone(a);
{
ref ar = b;
let root = arc.root(ar);
if root == arc.root(ar) {
total = total + 0;
}
}
arc.release(move a);
arc.release(move b);
total
}
@@ -0,0 +1,107 @@
module demo.ring
air 0
lang 0.1
features [arc, capabilities, effects, heap, islands, ownership, resources]
type @Node = struct { value: u64, next: @Link }
type @Link = enum { End, To(iref<@Node>) }
fn @ring(%heap: cap<mem.alloc>) -> island<@Node>
effects [mem.alloc]
entry:
%c_1 = const.u64 1
%e_2 = enum.make @Link.End()
%s_3 = struct.make @Node(%c_1, %e_2)
island.create %s_3, %heap, ok -> @ok_4, err -> @err_6
ok_4(%ok_5: island<@Node>):
%isl_8 = borrow.mut %ok_5
%root_9 = island.root %isl_8
%c_10 = const.u64 2
%e_11 = enum.make @Link.To(%root_9)
%s_12 = struct.make @Node(%c_10, %e_11)
island.alloc %isl_8, %s_12, ok -> @ok_13, err -> @err_15
err_6(%err_7: alloc_error):
trap
ok_13(%ok_14: iref<@Node>):
%c_17 = const.u64 3
%e_18 = enum.make @Link.To(%ok_14)
%s_19 = struct.make @Node(%c_17, %e_18)
island.alloc %isl_8, %s_19, ok -> @ok_20, err -> @err_22
err_15(%err_16: alloc_error):
trap
ok_20(%ok_21: iref<@Node>):
%first_24 = island.borrow %isl_8, %root_9
%e_25 = enum.make @Link.To(%ok_21)
%next_26 = borrow.field @Node.next, %first_24
store %next_26, %e_25
borrow.end %next_26
borrow.end %first_24
borrow.end %isl_8
ret %ok_5
err_22(%err_23: alloc_error):
trap
fn @walk(%f: ref<frozen<@Node>>, %steps: u64) -> u64
effects []
entry:
%c_1 = const.u64 0
%root_2 = frozen.root %f
%c_3 = const.u64 0
br @while_4(%c_1, %root_2, %c_3)
while_4(%sum_7: u64, %at_8: iref<@Node>, %i_9: u64):
%cmp_14 = lt.u64 %i_9, %steps
cbr %cmp_14, @body_15(), @after_5(%sum_7)
after_5(%sum_10: u64):
ret %sum_10
step_6(%sum_11: u64, %at_12: iref<@Node>, %i_13: u64):
%one_27 = const.u64 1
%i_28 = add.checked.u64 %i_13, %one_27
br @while_4(%sum_11, %at_12, %i_28)
body_15:
%node_16 = frozen.borrow %f, %at_8
%value_17 = borrow.field @Node.value, %node_16
%value_18 = load %value_17
borrow.end %value_17
%t_19 = add.checked.u64 %sum_7, %value_18
%next_20 = borrow.field @Node.next, %node_16
%next_21 = load %next_20
borrow.end %next_20
enum.switch @Link %next_21, End -> @case_End_22, To -> @case_To_23
case_End_22:
br @endmatch_25(%at_8)
case_To_23(%n_24: iref<@Node>):
br @endmatch_25(%n_24)
endmatch_25(%match_26: iref<@Node>):
borrow.end %node_16
br @step_6(%t_19, %match_26, %i_9)
fn @main(%heap: cap<mem.alloc>) -> u64
effects [mem.alloc]
entry:
%ring_1 = call @ring(%heap)
%frozen_2 = island.freeze %ring_1
%c_3 = const.u64 0
%f_4 = borrow.ref %frozen_2
%c_5 = const.u64 7
%walk_6 = call @walk(%f_4, %c_5)
borrow.end %f_4
%arc_7 = arc.from_frozen %frozen_2
%a_8 = borrow.ref %arc_7
%arc_9 = arc.clone %a_8
borrow.end %a_8
%b_10 = borrow.ref %arc_9
%root_11 = arc.root %b_10
%root_12 = arc.root %b_10
%cmp_13 = iref.eq %root_11, %root_12
cbr %cmp_13, @then_14(), @else_15()
then_14:
%c_16 = const.u64 0
%t_17 = add.checked.u64 %walk_6, %c_16
br @endif_18(%t_17)
else_15:
br @endif_18(%walk_6)
endif_18(%total_19: u64):
borrow.end %b_10
arc.release %arc_7
arc.release %arc_9
ret %total_19
@@ -0,0 +1,78 @@
// An island moves to a worker task through a channel in O(1); the worker sums the graph
// and destroys it. The sender holds nothing that reaches it after the send.
// expect: run
// grant: heap=mem.alloc:512
// schedules: 20
// quantum: 3
// result: 6
// phase: 6
// spec: docs/spec/03-memory-model.md
module demo.islandtransfer
struct Node {
value: u64,
next: Link,
}
enum Link {
End,
To(iref<Node>),
}
fn worker(rx: receiver<island<Node>>) -> u64
effects [task.wait]
{
var sum: u64 = 0;
match channel.recv(rx) {
ok(isl) => {
{
ref g = isl;
var at: iref<Node> = island.root(g);
loop {
ref node = island.at(g, at);
sum = sum + node.value;
match node.next {
Link::End => { break; }
Link::To(n) => { at = n; }
}
}
}
island.destroy(move isl);
}
closed => {}
cancelled => {}
}
channel.close_receiver(move rx) or trap;
sum
}
fn main(heap: cap<mem.alloc>) -> u64
effects [mem.alloc, task.spawn, task.wait]
{
match channel.make<island<Node>>(1, heap) {
ok(tx, rx) => {
var r: u64 = 0;
scope sc {
let t = task.spawn(sc, worker, move rx);
let isl = island.create(Node { value: 1, next: Link::End }, heap) or trap;
{
mut g = isl;
let a = island.root(g);
let b = island.alloc(g, Node { value: 2, next: Link::End }) or trap;
let c = island.alloc(g, Node { value: 3, next: Link::To(b) }) or trap;
mut first = island.at(g, a);
first.next = Link::To(c);
}
match channel.send(tx, move isl) {
ok => {}
closed(back) => { island.destroy(move back); }
cancelled(back) => { island.destroy(move back); }
}
channel.close_sender(move tx);
r = task.join(move t);
}
r
}
err(e) => { trap; }
}
}
@@ -0,0 +1,141 @@
// Reference program (Phase 4 exit): island pipeline. A producer task builds a cyclic
// graph in an island and moves it through a channel (O(1), no copy). The consumer
// freezes it, lends the frozen graph to two readers at once, turns it into an arc,
// sends a clone to a late reader, and releases every handle; the last release frees
// the graph. Each reader walks the ring 6 steps: 2 × (1+2+3+1+2+3) + 12 = 36.
// expect: run
// grant: heap=mem.alloc:1024
// schedules: 30
// quantum: 3
// result: 36
// equivalent: phase4/run/program_island_pipeline.air
// phase: 6
// spec: docs/spec/03-memory-model.md
module demo.islandpipeline
struct Node {
value: u64,
next: Link,
}
enum Link {
End,
To(iref<Node>),
}
// Builds the ring 1 → 2 → 3 → 1 and moves it into the channel.
fn produce(tx: sender<island<Node>>, heap: cap<mem.alloc>) -> u8
effects [mem.alloc, task.wait]
{
let isl = island.create(Node { value: 1, next: Link::End }, heap) or trap;
{
mut g = isl;
let a = island.root(g);
let c = island.alloc(g, Node { value: 3, next: Link::To(a) }) or trap;
let b = island.alloc(g, Node { value: 2, next: Link::To(c) }) or trap;
mut first = island.at(g, a);
first.next = Link::To(b);
}
match channel.send(tx, move isl) {
ok => {}
closed(back) => { island.destroy(move back); }
cancelled(back) => { island.destroy(move back); }
}
channel.close_sender(move tx);
0
}
// Sums `steps` nodes along the ring, through a shared borrow of the frozen graph.
fn walk(f: ref<frozen<Node>>, steps: u64) -> u64
effects []
{
var sum: u64 = 0;
var at: iref<Node> = frozen.root(f);
for i in 0..steps {
ref node = frozen.at(f, at);
sum = sum + node.value;
at = match node.next {
Link::End => at,
Link::To(n) => n,
};
}
sum
}
// A late reader that owns its own arc handle and releases it.
fn late(rx: receiver<arc<Node>>) -> u64
effects [task.wait]
{
var sum: u64 = 0;
match channel.recv(rx) {
ok(a) => {
{
ref ar = a;
var at: iref<Node> = arc.root(ar);
for i in 0..6u64 {
ref node = arc.at(ar, at);
sum = sum + node.value * 2;
at = match node.next {
Link::End => at,
Link::To(n) => n,
};
}
}
arc.release(move a);
}
closed => {}
cancelled => {}
}
channel.close_receiver(move rx) or trap;
sum / 2
}
fn main(heap: cap<mem.alloc>) -> u64
effects [mem.alloc, task.spawn, task.wait]
{
var total: u64 = 0;
match channel.make<island<Node>>(1, heap) {
ok(tx, rx) => {
scope sc {
let p = task.spawn(sc, produce, move tx, heap);
match channel.recv(rx) {
ok(isl) => {
let f = island.freeze(move isl);
{
ref fr = f;
scope readers {
let r1 = task.spawn(readers, walk, fr, 6);
let r2 = task.spawn(readers, walk, fr, 6);
total = task.join(move r1) + task.join(move r2);
}
}
let a = arc.from_frozen(move f);
match channel.make<arc<Node>>(1, heap) {
ok(atx, arx) => {
scope lates {
let l = task.spawn(lates, late, move arx);
let b = arc.clone(a);
match channel.send(atx, move b) {
ok => {}
closed(back) => { arc.release(move back); }
cancelled(back) => { arc.release(move back); }
}
channel.close_sender(move atx);
total = total + task.join(move l);
}
}
err(e) => {}
}
arc.release(move a);
}
closed => {}
cancelled => {}
}
channel.close_receiver(move rx) or trap;
task.join(move p);
}
}
err(e) => { trap; }
}
total
}
@@ -0,0 +1,59 @@
// Quick Reference: graphs (island → frozen → arc).
// expect: run
// grant: heap=mem.alloc:256
// schedules: 10
// result: 8
// phase: 6
// spec: docs/agent/quick-reference.md
module demo.qrgraphs
struct Node {
value: u64,
next: Link,
}
enum Link {
End,
To(iref<Node>),
}
fn read(fr: ref<frozen<Node>>, a: iref<Node>) -> u64
effects []
{
ref node = frozen.at(fr, a); // ref access only
node.value
}
fn main(heap: cap<mem.alloc>) -> u64
effects [mem.alloc, task.spawn]
{
let isl = island.create(Node { value: 1, next: Link::End }, heap) or trap; // effect mem.alloc
{
mut g = isl; // borrow the island; it outlives the accesses
let a = island.root(g);
let b = island.alloc(g, Node { value: 2, next: Link::To(a) }) or trap;
mut first = island.at(g, a); // mut<Node>; one mutable access at a time
first.next = Link::To(b); // edges may form cycles
}
let f = island.freeze(move isl); // O(1), irreversible; irefs stay valid
var sum: u64 = 0;
{
ref fr = f; // frozen graphs are Share: lend fr to tasks
let a = frozen.root(fr);
scope s {
let t = task.spawn(s, read, fr, a);
sum = task.join(move t);
}
}
let shared = arc.from_frozen(move f); // O(1); the same graph
let other = arc.clone(shared); // count + 1
arc.release(move other); // the last release frees the graph
{
ref ar = shared;
let a = arc.root(ar);
ref node = arc.at(ar, a);
sum = sum + node.value * 7;
}
arc.release(move shared);
sum
}