✓
Passing This code compiles and runs correctly.
Code
// Test: `koruc explain` reports index routing — the SQL-EXPLAIN namesake
// rung. A `std/indexes:store(players, key)` declaration shows as an
// `index = key` row witnessed by the declaration's own site hash, and each
// query site gets the plan the transform's own fold decides: a `! first`
// whose `when` names the indexed column is an `index lookup`, a `! query`
// is a `sweep`, and a `! first` on a non-indexed column is a `sweep` that
// stops at first match. A `[tree]` store plans its queries like any other
// — the fold reports the transform's own column surface, so a guard on a
// real column plans a sweep and a guard on the invisible `parent` would
// refuse with the transform's own words, never a guessed plan. Sites
// inside continuations count too — the two `| row`-chained inserts and
// queries nested under `|>` pipelines are folded, not just flow heads,
// and a site nested four invocations deep still carries its witness.
// The program itself still runs — the plans it reports are the routes it
// executes.
import std/io
import std/store
import std/indexes
import std/explain
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/io:print.ln("seeded")
|> std/store:query(players)
! first p when p.key == 30 |> std/io:print.ln("nested {{ p.val:d }}")
| none |> std/io:print.ln("nested none")
// routed: the guard's equality is on the indexed column
std/store:query(players)
! first p when p.key == 30 |> std/io:print.ln("first {{ p.val:d }}")
| none |> std/io:print.ln("first none")
// sweep: visits every row
std/store:query(players)
! query q |> std/io:print.ln("row {{ q.key:d }}")
// scan: the predicate names a non-indexed column — same `! first`
// spelling, no index to route through
std/store:query(players)
! first p when p.val == 100 |> std/io:print.ln("scan {{ p.val:d }}")
| none |> std/io:print.ln("scan none")
// a [tree] store: its `parent` self-FK is synthesized by the annotation
// and invisible to the query surface — plans for it read the same column
// list the transform does
[tree]std/store:new(tree, capacity: 4) { val: i64 }
std/store:insert(tree) { val: 10 }
| row r |> std/store:insert(tree) { val: 20 }
|> std/store:query(tree)
! first n when n.val == 20 |> std/io:print.ln("tree nested {{ n.val:d }}")
| none |> std/io:print.ln("tree none")
std/store:query(tree)
! first n when n.val == 10 |> std/io:print.ln("root {{ n.val:d }}")
| none |> std/io:print.ln("no root")
Actual
seeded
nested 300
first 300
row 10
row 20
row 30
scan 100
tree nested 20
root 10
Expected output
seeded
nested 300
first 300
row 10
row 20
row 30
scan 100
tree nested 20
root 10
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)
flow ~new click a branch to expand · @labels scroll to their anchor
new (tree, capacity: 4, source: val: i64)
flow ~insert click a branch to expand · @labels scroll to their anchor
insert (tree, source: val: 10)
flow ~query click a branch to expand · @labels scroll to their anchor
query (tree)
Test Configuration
MUST_RUN
Post-validation Script:
#!/bin/bash
# `koruc explain` on an indexed store: the `index` row names the declared
# column and carries the declaration site's witness hash, and each query
# site reports the plan the transform's fold routes it to — index lookup,
# sweep, or early-exit sweep — with refusals in the transform's own words.
set -e
echo "=== koruc explain (text) ==="
TEXT=$(koruc "$KORU_INPUT" explain 2>&1)
echo "$TEXT"
echo "$TEXT" | grep -q "📖 std/store" || { echo "FAIL: store report missing"; exit 1; }
echo "$TEXT" | grep -qE "index = key \[[0-9a-z]+\]" || { echo "FAIL: index row missing"; exit 1; }
echo "$TEXT" | grep -qE "inserts = 3 \[[0-9a-z]+\] \[[0-9a-z]+\] \[[0-9a-z]+\]" || { echo "FAIL: insert count/witnesses missing"; exit 1; }
echo "$TEXT" | grep -qE "queries = 4( \[[0-9a-z]+\]){4}" || { echo "FAIL: query count/witnesses missing"; exit 1; }
test "$(echo "$TEXT" | grep -c "index lookup on key")" = "2" || { echo "FAIL: nested routed plan missing"; exit 1; }
echo "$TEXT" | grep -q "index lookup on key" || { echo "FAIL: routed plan missing"; exit 1; }
echo "$TEXT" | grep -q "! query q — sweep" || { echo "FAIL: sweep plan missing"; exit 1; }
echo "$TEXT" | grep -q "sweep, stops at first match" || { echo "FAIL: scan plan missing"; exit 1; }
echo "$TEXT" | grep -q "input:tree" || { echo "FAIL: tree section missing"; exit 1; }
echo "$TEXT" | grep -qE "queries = 2 \[[0-9a-z]+\] \[[0-9a-z]+\]" || { echo "FAIL: nested tree query unwitnessed"; exit 1; }
test "$(echo "$TEXT" | grep -c "! first n — sweep, stops at first match")" = "2" || { echo "FAIL: tree plan missing"; exit 1; }
! echo "$TEXT" | grep -q "refused:" || { echo "FAIL: a plan row refused"; exit 1; }
echo "=== second insert witness resolves to the chained insert ==="
IHASH=$(echo "$TEXT" | grep -oE "inserts = 3 \[[0-9a-z]+\] \[[0-9a-z]+\]" | grep -oE "\[[0-9a-z]+\]" | tail -1 | tr -d '[]')
IAT=$(koruc "$KORU_INPUT" at "$IHASH" 2>&1)
echo "$IAT"
echo "$IAT" | grep -q "std.store:insert" || { echo "FAIL: at missed the chained insert"; exit 1; }
echo "$IAT" | grep -q "input.k:25" || { echo "FAIL: arm head not on its own line"; exit 1; }
echo "=== deep-nested query witness resolves and drifts gracefully ==="
QHASH=$(echo "$TEXT" | grep -oE "query\[0\] = ! first p — index lookup on key \[[0-9a-z]+\]" | grep -oE "\[[0-9a-z]+\]" | tail -1 | tr -d '[]')
QAT=$(koruc "$KORU_INPUT" at "$QHASH" 2>&1)
echo "$QAT"
echo "$QAT" | grep -q "std.store:query" || { echo "FAIL: at missed the nested query"; exit 1; }
echo "$QAT" | grep -q "std.store:query.*input.k:28" || { echo "FAIL: |> query link not on line 28"; exit 1; }
echo "$QAT" | grep -q "std.io:print.ln.*input.k:29" || { echo "FAIL: ! first arm not on line 29"; exit 1; }
echo "$QAT" | grep -q "std.io:print.ln.*input.k:30" || { echo "FAIL: | none arm not on line 30"; exit 1; }
koruc "$KORU_INPUT" at "${QHASH}aa" 2>&1 | grep -q "tail drifted" || { echo "FAIL: deep descent did not drift"; exit 1; }
echo "=== index witness resolves to the declaration site ==="
HASH=$(echo "$TEXT" | grep -oE "index = key \[[0-9a-z]+\]" | grep -oE "\[[0-9a-z]+\]" | tr -d '[]')
AT=$(koruc "$KORU_INPUT" at "$HASH" 2>&1)
echo "$AT"
echo "$AT" | grep -q "std.indexes:store" || { echo "FAIL: at missed the index decl"; exit 1; }
echo "$AT" | grep -q "input.k" || { echo "FAIL: at lost the file"; exit 1; }
echo "$AT" | grep -q "input.k:22" || { echo "FAIL: decl line double-subtracted"; exit 1; }
echo "=== koruc explain json (typed) ==="
JSON=$(koruc "$KORU_INPUT" explain json 2>&1)
echo "$JSON"
echo "$JSON" | grep -q '"index":"key"' || { echo "FAIL: index not in json"; exit 1; }
echo "=== PASS: explain reports the index plan ==="