Design decisions

94. xs.distinct() and consistent empty-sequence rule for fold(f) / min() / max() (2026-11-21)

  • Decision.
    1. Add distinct<S: Seq, T>(self: S) -> List<T> to std.seq and std.prelude: returns distinct values in first-seen order in O(n) expected time and O(n) space via HashSet, across every Seq (List, ListView, [D]T, Str, StrView, Range). Keep dedup(mut a) / a.deduped() for adjacent-only O(n) deduplication (no hashing, e.g. after sort()); no parallel unique name.
    2. Extend xs.fold(f) across every Seq to accept both monoids (+, *, ^, |, &, gcd, min, max, Monoid<T>, which return their identity when empty) and arbitrary binary functions (T, T) -> T, obeying the exact same empty-sequence rule as xs.min() / xs.max(): bare xs.fold(f) returns T and traps when empty, while xs.fold(f)! or xs.fold(f) ?? d asks for ?T (none when empty). Also include ListView and StrView in expr_maybe_opt_reduce so view.min() ?? d / view.max() ?? d / view.fold(f) ?? d behave identically to owned sequences.
  • Reason. Closes research/frictions/cf2115a.md (a.fold(gcd) and a.distinct() on [D]T), eliminates the gap between monoid folds and arbitrary binary folds without inventing a second empty-sequence convention beside min/max, and keeps adjacent dedup/deduped cleanly separated from hashing distinct.
  • Reverse. Remove distinct from stdlib/seq.cp and stdlib/prelude.cp, and remove fold_seq/reduce_fold_fn from crates/cpl-ir/src/lower/call.rs.