Design decisions

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 by Graph (arcs (to, e, ...)), any fn V -> Seq, and any Indexed<V, S> of sequences. Arc capabilities are traits on Arc: 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> and EdgedNext<V, E>. dijkstra<V, W, G: WeightedNext<V, W>>(g: G, ...). Whole-graph algorithms bound V: Domain. Bridges, cut vertices, blocks and two-edge components need EdgedNext (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 a Seq of (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 any Next is an adapter (like Filter) whose arcs read .w as f(a) and forward .to/.e, so it is a WeightedNext and keeps Edged. On a weighted graph it reweights. f runs 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 any Seq<(V, W)>, no Source trait. 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 record A, built in O(n + m) keeping input order; arcs carry to, e and every other edge field by default; keep: narrows what arcs store (dropping e makes them not Edged). No add_edge: a growing graph is [V]List<Arc>, which is already a Next. Widths come from domains. Rejected for now: e in 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 it keep: ?(EdgeArc<V, E, R> -> B) = none: spec-stub.py reads 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) let directed/undirected take 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 bodiless impl. Record update (..a, to: u) builds transpose. Rules that keep rows from leaking: carried fields are plain (a heap field is an error pointing at keep:); no record subtyping, rows only bound type parameters; builds need named records (unnamed tuples get a hint with the naming map); a clash with to/e is an error pointing at keep:; algorithms bound on field traits, rows only build records (lint); ..R keeps 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, false on a negative cycle; g.matrix() builds d in O(n^2 + m). Saves an n^2 copy and works for any W: Ord + Add.
  • DFS (agreed in §147; was proposed here). step.prune() after Enter (the stub said steer only with break); events carry the arc, Enter(v, via: ?Arc); an Again(from, via) event for arcs into visited vertices, so custom low-link fits; optional shared seen: mut [V]Bool; free reachable(g, from:) with no skip: (a filter lambda does it); g.dfs_tree returns Rooted plus [E]Bool tree edges, one tree type.
  • Done. spec/std/graph.cp uses these traits and signatures; the parser now closes a type parameter list on half of >> (G: Next<V>>(), and spec-stub.py skips where-clause colons.
  • Reverse rows. Per-shape builders (_of_pairs, _of_triples) and a fixed (to, e) arc with fields read through edges[a.e].
  • Why. Ryan asked whether WeightedNext could bundle the arc bounds and called the long signature "a mouthful"; he accepted Edged once its reason (multi-edge bridges, [E] results) was shown.
  • Reverse. Back to Neighbours with w: A -> W on each algorithm.