146. Graph traits: Next with arc capabilities (2026-11-21)
- Decision (agreed direction; stubs not yet rewritten). Algorithms take
a trait,
Graph<V, E>is one struct implementing it.trait Next<V>: type Arc: Target<V>; fn out(self, u: V) -> yields Arc. Implemented byGraph(arcs(to, e, ...)), any fnV -> Seq, and anyIndexed<V, S>of sequences. Arc capabilities are traits onArc:Target<V>(.to; a plain vertex is its own target),Weighted<W>(.w),Edged<E>(.e). Trait aliases name bundles:WeightedNext<V, W> = Next<V> where Self.Arc: Weighted<W>andEdgedNext<V, E>.dijkstra<V, W, G: WeightedNext<V, W>>(g: G, ...). Whole-graph algorithms boundV: Domain. Bridges, cut vertices, blocks and two-edge components needEdgedNext(skip the same edge, not the same vertex, so multi-edges are right; results are[E]); undirectedness is a debug-checked precondition. Edge-list algorithms (Kruskal, Euler, offline connectivity) take aSeqof(u, v, ...)records. Flows stay their own mutable struct. - New powers this needs. Associated types (
type Arc,G.Arc) and trait aliases, both open to user libraries. - Weights (agreed). No algorithm takes
w:.g.weighted(f)on anyNextis an adapter (likeFilter) whose arcs read.wasf(a)and forward.to/.e, so it is aWeightedNextand keepsEdged. On a weighted graph it reweights.fruns per arc read; no graph is built..weighted((u, a) => ...)also passes the source vertex (Johnson). - Parameters (agreed). Every parameter asks for the weakest capability
it uses (as §142 for sequences):
from:is anySeq<(V, W)>, noSourcetrait. Inline bounds are core:g: WeightedNext<V, W>in a parameter's type is a fresh hidden type parameter (static, per caller); name it with<G: ...>when two places must share it. Open to users. - Still open. Weight algebra: Ryan leans to a monoid-like argument
(minimax, bottleneck, reliability) over fixed
Ord + Add; later. - Storage (agreed).
Graph<V, A>is immutable CSR over an arc recordA, built in O(n + m) keeping input order; arcs carryto,eand every other edge field by default;keep:narrows what arcs store (droppingemakes them notEdged). Noadd_edge: a growing graph is[V]List<Arc>, which is already aNext. Widths come from domains. Rejected for now:ein a side table (e: .table), rarely worth it. keep:(agreed, trial). A fn from the default arc to the stored arc,keep: a => (to: a.to, w: a.w), run once per stored arc at build (2m times undirected), so a computed field (fwd: a.to == edges[a.e].v) is a stored value. No bare field-list syntax: it would be core syntax for one fn. The stub spells itkeep: ?(EdgeArc<V, E, R> -> B) = none:spec-stub.pyreads a lambda default (= a => a) as a body.- Records as edges and arcs (agreed, trial). Row bounds
R: (u: V, v: V, ..)and..R without (u, v)letdirected/undirectedtake any named edge record;EdgeArc<V, E, R> = (to: V, e: E, ..R without (u, v))is the default arc. Anonymous records satisfy field traits (all members@get:Target,Weighted,Edged) by their fields; named structs opt in with a bodilessimpl. Record update(..a, to: u)buildstranspose. Rules that keep rows from leaking: carried fields are plain (a heap field is an error pointing atkeep:); no record subtyping, rows only bound type parameters; builds need named records (unnamed tuples get a hint with the namingmap); a clash withto/eis an error pointing atkeep:; algorithms bound on field traits, rows only build records (lint);..Rkeeps R's field order; record update moves heap fields under R1/R2; errors name the missing field, never the row; a generic body sees only the named fields. Ryan: "ok it's fine we can try this out for now". - Floyd-Warshall (agreed).
floyd_warshall(mut d: [V, V]?W) -> Bool, in place,falseon a negative cycle;g.matrix()buildsdin O(n^2 + m). Saves an n^2 copy and works for anyW: Ord + Add. - DFS (agreed in §147; was proposed here).
step.prune()afterEnter(the stub said steer only withbreak); events carry the arc,Enter(v, via: ?Arc); anAgain(from, via)event for arcs into visited vertices, so custom low-link fits; optional sharedseen: mut [V]Bool; freereachable(g, from:)with noskip:(a filter lambda does it);g.dfs_treereturnsRootedplus[E]Booltree edges, one tree type. - Done.
spec/std/graph.cpuses these traits and signatures; the parser now closes a type parameter list on half of>>(G: Next<V>>(), andspec-stub.pyskipswhere-clause colons. - Reverse rows. Per-shape builders (
_of_pairs,_of_triples) and a fixed(to, e)arc with fields read throughedges[a.e]. - Why. Ryan asked whether
WeightedNextcould bundle the arc bounds and called the long signature "a mouthful"; he acceptedEdgedonce its reason (multi-edge bridges,[E]results) was shown. - Reverse. Back to
Neighbourswithw: A -> Won each algorithm.