✓
Passing This code compiles and runs correctly.
Code
// PINS: first-class std/index — `std/indexes:store(players, key)` is a pure
// AST-data declaration (kernel:shape's `[norun]` pattern): it claims the
// site, never executes, and std/store's transforms discover it on the
// whole-program walk and emit what an indexed store needs — a key→handle
// map maintained inside insert/take/stored, and a `! first` whose `when`
// predicate on the indexed column routes to a map lookup instead of a sweep.
//
// Three oracles, one declaration:
// 1. `! first p when p.key == k` answers — the same spelling that scans
// an unindexed store. The index is an organ decision, not a surface.
// 2. Swap-remove correctness: take the middle row and the row MOVED into
// its slot must still resolve by key. An index kept from outside could
// never see the move (no event fires for the displaced row); the fixup
// has to live inside take's own emit.
// 3. Taken-key honesty: `! first` on the TAKEN row's key answers `| none`
// — never a stale handle, never a generational trap.
// Plus the floor: a predicate on a NON-indexed column still scans and
// answers — declaring an index must not change what unindexed `when` means.
import std/store
import std/indexes
import std/io
std/store:new(players, capacity: 8) { key: i64, val: i64 }
std/indexes:store(players, key)
std/store:insert(players) { key: 10, val: 100 }
| row a |> std/store:insert(players) { key: 20, val: 200 }
| row b |> std/store:insert(players) { key: 30, val: 300 }
|> std/store:take(players[b])
| item _ |> std/io:print.ln("took")
| empty |> std/io:print.ln("take-empty")
// the moved row still resolves by key
std/store:query(players)
! first p when p.key == 30 |> std/io:print.ln("moved {{ p.val:d }}")
| none |> std/io:print.ln("moved LOST")
// the taken key is gone — none, never a stale deref
std/store:query(players)
! first p when p.key == 20 |> std/io:print.ln("taken {{ p.val:d }}")
| none |> std/io:print.ln("taken none")
// a non-indexed predicate still scans
std/store:query(players)
! first p when p.val == 100 |> std/io:print.ln("scan {{ p.val:d }}")
| none |> std/io:print.ln("scan lost")
Supporting Files
Actual
took
moved 300
taken none
scan 100
Expected output
took
moved 300
taken none
scan 100
Flows
flow ~new click a branch to expand · @labels scroll to their anchor
new (players, capacity: 8, source: key: i64, val: i64)
flow ~store click a branch to expand · @labels scroll to their anchor
store (expr: players, key, source: <implicit_source>)
flow ~insert click a branch to expand · @labels scroll to their anchor
insert (players, source: key: 10, val: 100)
flow ~query click a branch to expand · @labels scroll to their anchor
query (players)
flow ~query click a branch to expand · @labels scroll to their anchor
query (players)
flow ~query click a branch to expand · @labels scroll to their anchor
query (players)
Test Configuration
MUST_RUN
Post-validation Script:
#!/bin/bash
# Emitted-code oracle: the index must be REAL, not a scan wearing the
# declaration (the honesty gap this test's TODO named). An `std/indexes`
# store emits a key→member-bucket map; `! first` on the indexed column
# emits a bucket get + a per-member row resolve (dead members — taken
# rows, never ejected — resolve to no row and are skipped); insert
# appends; the predicate on the NON-indexed column still emits the sweep.
set -u
if [ ! -f output_emitted.zig ]; then
echo "FAIL: no output_emitted.zig"
exit 1
fi
if ! grep -q '__index_key' output_emitted.zig; then
echo "FAIL: std/indexes:store(players, key) emitted no __index_key map"
exit 1
fi
if ! grep -q '__index_key.get(30)' output_emitted.zig; then
echo "FAIL: '! first p when p.key == 30' did not route to the bucket"
exit 1
fi
if ! grep -q '__index_key.get(20)' output_emitted.zig; then
echo "FAIL: '! first p when p.key == 20' did not route to the bucket"
exit 1
fi
if ! grep -q '__koru_row_of(__koru_bh)' output_emitted.zig; then
echo "FAIL: routed lookups do not resolve members - a taken key would answer"
exit 1
fi
if ! grep -q 'for (0..main_module.__koru_store_players.len)' output_emitted.zig; then
echo "FAIL: the non-indexed predicate (p.val) lost its scan"
exit 1
fi
echo "PASS: declared index emits a real bucket map, resolved lookups, and the unindexed scan survives"
exit 0