Design decisions

123. Mutable slice views (cpl::MutView<T>) via existing range-lending rules (2026-11-21)

  • Decision. A range slice xs[l..r] (or xs[l..=r], a[D(l)..D(r)], cuts[c1..c2]) of a mutable List<T> or 1D [D]T (nominal, range, or cut domain) is an O(1) fixed-length mutable view (Ty::MutView<T> in IR, cpl::MutView<T> holding T* ptr + size_t n in C++17, Value::MutView holding (target: Part, lo, len) in the interpreter). It implements IndexedMut (at, set_at, swap_idx) so every in-place sequence mutator (sort, reverse, rotate_left, rotate_right, scan_in_place, suffix_scan_in_place, shuffle, next_permutation, prev_permutation, nth_element, fill, element assignment v[i] = x) works through one shared mechanism with zero copies and no per-method special cases. Length-changing List methods (push, pop, clear, insert, remove, resize, extend, etc.) are rejected with E0401.
  • Lending. Reuses the existing lending rules (cpl-check/src/check/lends.rs) with no core change of character: a slice let v = xs[l..r] that is mutated (or passed mut) lends mut over Step::Slice(l..r) of xs for v's live range, so any aliasing read or write of xs (or an overlapping mutable slice) while v is live is a compile error (E0602), while statically disjoint slices (xs[0..2] and xs[2..4]) both work. Read-only slices of immutable (let) bases or Str remain read-only (E0401 on mutation). cpl explain reports mutable slice view -> cpl::MutView<T> (T* + len), O(1), fixed-length mut lend.
  • Evidence. Interp/C++17 parity test crates/cpl-cli/tests/programs/seq_mut_slice.cp (covering List<Int>, [D]Int, [D.bounds]Int, [0..6]Int, nested and disjoint slices); checker compile-fail tests in crates/cpl-check/src/tests.rs (mutable_slice_views_lend_and_reject_aliasing); cpl explain test in crates/cpl-cli/src/lib.rs; matched benchmark bench/seq/slice_sort.cp (xs[l..r].sort() vs std::sort(xs.data() + l, xs.data() + r)): 1.05x (213.9 ms vs 202.9 ms, 9,393 B emitted C++); port migrations: cf2207h3.cp (678 → 658 tokens, -20, gate.sh PASS) and cf2207h1.cp (1,007 → 813 tokens, -194, gate.sh PASS). Closes research/frictions/cf2207h3.md #1b.
  • Reverse. Restore note_readonly_view for list/array slices in cpl-check/src/check/expr.rs, remove Ty::MutView / ExprKind::MutSlice / cpl::MutView, and revert cf2207h3.cp / cf2207h1.cp.