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 forfirst/last. - Decision.
.l/.rswitch kind,.prev/.nextkeep it. Positionv:v.prev,v.next(?V); its cutsv.l,v.r. Cutc:c.prev,c.next(?V.bounds); the positions beside itc.l,c.r(?V). Ranger: its end cutsr.l,r.r.v.l..v.risv. 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@getmethod whose parameters all have defaults may be read without parentheses, so.nextis.next(1); one method, no overloading (127). Only@getmethods (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,nonewhenr.len <= 1solet … elseis the base case.- Ranges keep 124: a range's elements are the kind of its ends.
j..=iover cuts iterates cuts j through i;j..iiterates cuts j to i-1 and is also the piece where aRange<V>is expected. The piece is literal typing, not a conversion: only a half-opena..bliteral with cut ends builds it, a stored cut range never converts, and there is no implicitInto(implicit conversions stay closed:Tto?T, range widening, literal typing). Considered and rejected: every range is positions with cut ends, plusr.cuts(cleaner model, butfor j in lo..i: dp[i] min= dp[j] + cost(j..i)stops type-checking anda..=bover cuts stops meaning cuts). - Rejected:
.n/.p(nreads as a count,pas parent or prime). - Rejected:
r.split(at: c); with cuts as values,r.l..candc..r.rare as short and also express "all butv" (r.l..v.l,v.r..r.r). D.boundsis renamedD.cuts(typeBounds<D>is nowCut<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.ris v+1;1..=n:v.ris v). Back:V.cuts(k)traps,V.cuts.at(k)is optional.- Domains may be empty.
D.first/D.lastreturn?D; writeD.first!when you know. A derivedNonEmptybound (narrowed by guards, asserted asdomain E: NonEmpty = …) was accepted, then dropped as more machinery than!saves. r.midis the middle cut (⌊len/2⌋ elements before it), always present:r.lwhen empty. Element-shaped D&C writesif r.is_empty: returnthenr.mid.r!; lint flags.mid.rwithout the guard, since on an empty range it is a position outsider.r.mid: ?V(safe on empty) was tried and dropped: the cut is whathalveand the element shape share. Reverse: make it?Vagain.v + 1is anInt; re-enter the domain withv.nextorV(x).- Int to position:
V(x)traps outsideV,V.at(x)returns?V(thea[i]/a.get(i)split), and the patternV(j)isV.atin pattern position (if let V(j) = i - 10:).V.tryrejected. r.first_true(ok)(false→true) andr.last_true(ok)(true→false) return?the element;r.bsearch(ok, last: false)returns the flip cut, always present:first_trueisbsearch(ok).r,last_trueisbsearch(ok, last: true).l. Replacebsearch_first/bsearch_last.
- Rejected.
start/endplusbefore/after(four words where one rule does; typed often in prefix DPs);cut_before/pos_before(long); always-Vwith a trap (the n = 1 bug only shows if a test hits it). - Why.
.l/.rare short where they are typed most (prefix DPs), already the range-end words inspec/std/seq.cp, and?Vfromfirstputs the empty edge case where an end is asked for, at the cost of one!. - Reverse. Rename via
cpl fix; bring backNonEmpty(checker-derived fromD.count's lower bound) to makefirstplain where proven. - Status. Accepted in the spec; not implemented.