66. Dream domain constructs: subsets, enum arrays, sequence ops, and Item/Cut ranges (0.3, 2026-11-21)
- 66.1 Domain-wide subsets (
V.all,Bitset<V>,adjacency(edges, into: Bitset)).Bitset<D>adds non-mutating.without(v),.find(pred),.any(pred),@get fn empty, andimpl Show. Any struct providingeach(self)(includingBitset<D>) is directly iterable infor v in s:via one generalIter::Seqlowering rule incpl-irandelement_tylookup incpl-check.V.allcontinues to default toRange<V>(preserving existingList<V>/Range<V>callers), and lifts cleanly toBitset<V>.full()wherever aBitset<V>is expected, inV.all - side/&/|/^, and inV.all.without(v).adjacency(edges, into: Bitset)is a general graph builder returning[V]Bitset<V>(withinto: Listor default returning[V]List<V>), inferringVfrom the edge record/tuple endpoints. Majorization check forV.allcontextual lift:V.allstaysRange<V>(two ints, O(1), zero allocation) in all non-Bitset contexts (for v in V.all,V.all.len, etc.) and lifts toBitset<V>.full()only where the static context explicitly requiresBitset<V>(aBitset<V>parameter/annotation,V.all - side/&/|/^, orV.all.without(v)). In those positionsBitset<V>is the only valid representation and the generated code is identical to writingBitset<V>.full()explicitly (same O(n/64) time and space, never allocating a bitset outside Bitset contexts), so the contextual lift majorizes explicitBitset<V>.full()without hidden cost. Reason: eliminates manualBitset<V>.full()/.each()/ odd-adjacency boilerplate (cf2147h, IOI sketches). Reverse: remove theeach-basedelement_ty/iterpath,Range -> Bitsetcoercion, andadjacencylowering.
- 66.2 Fieldless enums as first-class array dimensions (
[Sign]T,[Sign] x => ..., explicit variant values). Fieldless enum variants accept optional constant integer discriminants (enum Sign: Neg = -1, Zero = 0, Pos = 1, read viaInt(s)). Any fieldless enumEis a valid array dimensionDim::Enum(E)/DimSpec::Enum(E)of static lengthE.variants.len, supports[E] x => exprtable comprehensions andfor x in E:iteration, and indexes withCast::EnumOrd(E)so distinct enums cannot index each other's tables. Reason: enables[Sign]PathDP/segment-tree tags (design/EXEMPLARS-PORTS.md) without manual ordinal arrays or losing enum type safety. Reverse: removeDim::Enum/DimSpec::EnumandCast::{EnumOrd, EnumVal, OrdToEnum}across IR, interp, and codegen.
- 66.3 Domain-level sequence operations (
Seq/Indexedparity:partition,sort(by:),group(by:),at,first(p),filter).Ty::Domain(D)satisfiesSeqandIndexedviaTy::seq_elem() == Some(Ty::Pos(D)), inheriting.at(i)and.get(i)directly fromIndexed. Every domainD(and any sequenceList<T>,[D]T,Range<T>,Set<T>) supports.partition(keep)returning(List<D>, List<D>)(matchingstdlib/seq.cp),.group(by: key)returningList<List<D>>in deterministic first-seen key order (§60by:option),.first(pred)returning?D(matchingxs.first(p)),.filter(pred)returningList<D>, andD.sort(by: ...)returningList<D>(matchingxs.sort(by:), whereby:accepts a lambda, function name, or[D]Karray).cpl fix(L0510by_twin+L0511domain_spelling) rewritesD.sorted(by:),xs.first(where: p), andxs.group_by(k). Reason: aligns domain sequence ops with the one-spelling rules (§53, §60) andSeq/Indexedtraits. Reverse: removeTy::DomainfromTy::seq_elem(), the sequence-op arms incpl-check/cpl-ir, andL0511.
- 66.4 Typed
Item/Cutpairs withminmaxandchoose2.minmax(a, b)preserves the operand's domain/position type ((Cut, Cut)forCutinputs), solet (lo, hi) = minmax(p[i], s[i]); low.apply(lo..hi, d)passes a typedRange<Cut>directly toLazySegTree<Cut, _>.applywithout converting throughIntor conflatingItemandCut.choose2(n)lowers directly ton * (n - 1) / 2. Reason: eliminatesmin/max+Cut(lo)..Cut(hi)boilerplate incf2147fwhile keepingItemandCutdistinct. Reverse: removeminmax/choose2fromcpl-check/src/prelude.rs,cpl-check/src/check/call.rs, andcpl-ir/src/lower/call.rs. Update (builtin floor, Q1 default):choose2is no longer a compiler builtin. The prelude brings instd.math.choose2, whose body is the one definition; the lowering arm gavechoose2(-3) == 6, against the documented0. Reverse: restore the three arms and dropchoose2fromspec/std/prelude.cp.minmaxfollowed:pub fn minmax<T: Ord>(a: T, b: T) -> (T, T)inspec/std/algebra.cp, in the prelude; the generic keeps(Cut, Cut)forCutoperands. Reverse: restore its arms inknown.rs,prelude.rsandlower/call.rsand drop it fromspec/std/prelude.cp.
- 61.5 Destructured fresh domains (
let (domain C, comp) = f(...)) are type names for the rest of their scope, nestedfns included (clarifies 33.1/33.2 and 58). A domain bound by destructuringlet (domain C, comp) = f(...)has the same scoping asdomain C = ...: top-level bindings pre-register theirDomainIdincollect_alland reuse that slot at theletsite so sibling/nestedfns can nameCin parameter/return types and read the declaration's hidden slot directly; local bindings already resolve throughouter_cxs. The escape rule (E0313) is unchanged. Reason: enablesfn force(c: C, val: Int)afterlet (domain C, comp) = two_edge_components(g)(cf2135c). Reverse: removeregister_pat_domainsincrates/cpl-ir/src/lower/mod.rsand the top-level slot reuse incrates/cpl-ir/src/lower/stmt.rs.
- 66.6
D.boundsas a first-class index domain inSegTree,LazySegTree, andFenwick. Generic type parameters bound toTy::Bound(d)(D = V.bounds) act as full index domains incpl-ir:D.K/D.len/D.countlower toDomLen(d) + 1,for i in D:iterates0..DomLen(d) + 1asTy::Bound(d),[D]Tlowers toDimSpec::Bound(d), andD.nth(k)/Range<D>.{lo,hi}preserveTy::Bound(d). Incpl-checkandcpl-ir, when a call argument expectsRange<D.bounds>, boundary ranges (..=lo.l,a..=b,a..b,a..) keep.boundsand permit..=rather than stripping toRange<D>(while non-bounds contexts likea[l.l..r.r]keep stripping toRange<D>). Partial generic constructor inference (LazySegTree<V.bounds, Int>(AddMax, fill: 0)) binds the supplied prefix of type parameters and infers the remaining ones (F) from arguments. Reason: eliminates shadow0..=ndomains andPos(lo.to_idx)casts (cf2207f). Reverse: remove theGenArg::Ty(Ty::Bound(_))arms incpl-ir/src/lower/{mod,expr,call}.rsandexpected_bounds_rangeincpl-check.