Design decisions

137. Indices: position and cut words, empty domains (2026-11-21)

  • Context. Round 1 of the API design (spec/core/04-indices.md): domains have positions and cuts (D.bounds), and each needs neighbour and conversion words; empty domains needed a rule for first/last.
  • Decision.
    • .l/.r switch kind, .prev/.next keep it. Position v: v.prev, v.next (?V); its cuts v.l, v.r. Cut c: c.prev, c.next (?V.bounds); the positions beside it c.l, c.r (?V). Range r: its end cuts r.l, r.r. v.l..v.r is v. The receiver's kind is static, so reusing the word is a type error when misused, never silent.
    • v.next(k), v.prev(k), c.next(k), c.prev(k) step k at once (optional). General rule: an @get method whose parameters all have defaults may be read without parentheses, so .next is .next(1); one method, no overloading (127). Only @get methods (O(1) by contract) read bare; a plain method always needs ().
    • An integer literal where a position or cut of an integer domain is expected is that position (var mex: P.bounds = 0, [d]P.bounds = 0). Non-literal Ints still convert explicitly (V(i)).
    • r.halve() returns ?(r.l..r.mid, r.mid..r.r), both non-empty, none when r.len <= 1 so let … else is the base case.
    • Ranges keep 124: a range's elements are the kind of its ends. j..=i over cuts iterates cuts j through i; j..i iterates cuts j to i-1 and is also the piece where a Range<V> is expected. The piece is literal typing, not a conversion: only a half-open a..b literal with cut ends builds it, a stored cut range never converts, and there is no implicit Into (implicit conversions stay closed: T to ?T, range widening, literal typing). Considered and rejected: every range is positions with cut ends, plus r.cuts (cleaner model, but for j in lo..i: dp[i] min= dp[j] + cost(j..i) stops type-checking and a..=b over cuts stops meaning cuts).
    • Rejected: .n/.p (n reads as a count, p as parent or prime).
    • Rejected: r.split(at: c); with cuts as values, r.l..c and c..r.r are as short and also express "all but v" (r.l..v.l, v.r..r.r).
    • D.bounds is renamed D.cuts (type Bounds<D> is now Cut<D>): one word for the idea in spec, errors and docs. The old spelling still parses so unmigrated ports keep checking; remove it once none use it.
    • Int(c) is the number of positions before the cut, 0..=n for any base, so both C++ prefix conventions fall out (0..n: v.r is v+1; 1..=n: v.r is v). Back: V.cuts(k) traps, V.cuts.at(k) is optional.
    • Domains may be empty. D.first/D.last return ?D; write D.first! when you know. A derived NonEmpty bound (narrowed by guards, asserted as domain E: NonEmpty = …) was accepted, then dropped as more machinery than ! saves.
    • r.mid is the middle cut (⌊len/2⌋ elements before it), always present: r.l when empty. Element-shaped D&C writes if r.is_empty: return then r.mid.r!; lint flags .mid.r without the guard, since on an empty range it is a position outside r. r.mid: ?V (safe on empty) was tried and dropped: the cut is what halve and the element shape share. Reverse: make it ?V again.
    • v + 1 is an Int; re-enter the domain with v.next or V(x).
    • Int to position: V(x) traps outside V, V.at(x) returns ?V (the a[i] / a.get(i) split), and the pattern V(j) is V.at in pattern position (if let V(j) = i - 10:). V.try rejected.
    • r.first_true(ok) (false→true) and r.last_true(ok) (true→false) return ? the element; r.bsearch(ok, last: false) returns the flip cut, always present: first_true is bsearch(ok).r, last_true is bsearch(ok, last: true).l. Replace bsearch_first/bsearch_last.
  • Rejected. start/end plus before/after (four words where one rule does; typed often in prefix DPs); cut_before/pos_before (long); always-V with a trap (the n = 1 bug only shows if a test hits it).
  • Why. .l/.r are short where they are typed most (prefix DPs), already the range-end words in spec/std/seq.cp, and ?V from first puts the empty edge case where an end is asked for, at the cost of one !.
  • Reverse. Rename via cpl fix; bring back NonEmpty (checker-derived from D.count's lower bound) to make first plain where proven.
  • Status. Accepted in the spec; not implemented.