Design decisions

22. Core syntax to 0.3 (2026-11-21)

  • 22.1 where is the 0.3 guard keyword alongside if for loops and let conditions. for x in s where c:, [e for x in s where c], if let x = e where c:, while let x = e where c:, and multi-binding if let a = x, b = y where c: / while let a = x, b = y where c: all parse into the existing Cond::Let / Cond::Guard / filtered-For AST nodes. for x in s if c: remains accepted so the existing 235 corpus ports stay green without churn. Reversal: remove the at_ident("where") branches in crates/cpl-syntax/src/parser/{stmt,expr}.rs (or deprecate for ... if once ports migrate).
  • 22.2 Product array syntax [(A, B)]T and table syntax [(A, B)] (a, b) => ... desugar to [A, B]T. Both [(A, B)]T (0.3) and [A, B]T (0.2.2) produce the same flat multi-dimensional TypeKind::Array, and both g[(a, b)] and g[a, b] index it, matching 21.1 without breaking existing single-bracket grid diagnostics or ports. Works for domain products ([(R, C)]Int) and counted dimensions ([(n, m)]Int). Reversal: remove the tuple-flattening branch in crates/cpl-syntax/src/parser/{types,expr}.rs.
  • 22.3 Optional arithmetic and bitwise lifting covers + - * / % ** & | ^ << >> and unary -, ~ (?T). Previously only +, -, and unary - lifted over ?Int/?F64. Per 23-optionals.md, + - * / lift over ?Int/?F64, % ** & | ^ << >> and unary ~ lift over ?Int, and ordering (< <= > >=) still does not lift. Implemented via the existing tropical lowering in cpl-ir and matching rules in cpl-check. Reversal: narrow the BinOp/UnOp match arms in crates/cpl-check/src/check/expr.rs and crates/cpl-ir/src/lower/expr.rs.
  • 22.4 pairs() and with_prev() are ordinary yields library functions in stdlib/seq.cp. Per 22-sequences.md, xs.pairs() yields adjacent (prev, x) and xs.with_prev() yields (?T, T) starting with (none, first), with no special loop syntax or compiler privilege. Written as yields generators over List<T> (which also works on [D]T arrays via existing coercion) so they fuse into loops in C++ and keep stdlib/prelude.cp at its 80-name cap (imported via use std.seq.{pairs, with_prev}). Reversal: remove pairs and with_prev from stdlib/seq.cp (or add them to stdlib/prelude.cp if prelude slots open).
  • 22.5 One-line colon structs (struct Edge: to: Int, w: Int) and dotted range-bound types (Self.min..=Self.max). struct_decl now accepts struct Name: f1: T1, f2: T2 (including shared-type x, y: Int and defaults), producing the same StructDecl AST node as indented/paren structs. range_follows in cpl-syntax now skips .ident member chains so Self.min..=Self.max and D.first..=D.last parse as TypeKind::Range. Reversal: remove the one-line colon branch in crates/cpl-syntax/src/parser/item.rs and restore 1-token range_follows in crates/cpl-syntax/src/parser/types.rs.
  • 22.6 Range-checked read::<lo..=hi>() in expression position and homogeneous multi-binding (var a, b, c: Z = (0, 0, 0), let n, m: 1..=20 = read()). read::<lo..=hi>() in cpl-ir now lowers a TypeKind::Range type argument to Spec::Int { lo, hi, text }, enforcing range bounds identically to read(lo..=hi) and let x: 1..=3 = read() while keeping ::<> turbofish for generic functions. Homogeneous multi-binding (var a, b, c: T = ... / let n, m: T = ...) desugars in let_stmt to a tuple pattern with repeated tuple type (T, T, ...), so var ones, one_twos, ans: Z = (0, 0, 0) and let n, m: 1..=20 = read() both work while scalar broadcast (var a, b: Int = 0) remains rejected as a tuple/scalar mismatch. Reversal: remove the TypeKind::Range branch in crates/cpl-ir/src/lower/call.rs and remove shared_count in crates/cpl-syntax/src/parser/stmt.rs.
  • 22.7 Parenthesized supertraits (trait Ord(Eq)), &mut T parameter syntax, opaque structs (struct List<T>), and multi-trait impl A + B for T. trait_decl accepts (Super1, Super2) alongside : Super1 + Super2; param desugars x: &mut T to Param { mutable: true, ty: Some(T) } with no new AST node; struct_decl allows bodyless opaque declarations (struct List<T>, struct Str); impl_decl desugars impl A + B for T: into one StmtKind::Impl per trait. Reversal: revert the four branches in crates/cpl-syntax/src/parser/item.rs.
  • 22.8 One-sided range types (0.., 2.., ..=8, ..n) and associated types in trait/impl scope. TypeKind::Range stores lo: Option<Box<Expr>> and hi: Option<Box<Expr>>, matching the optional bounds already used by cpl-check::Ty::Range and cpl-ir::Spec::Int. Both lower-bounded (0.., 2..) and upper-bounded (..n, ..=8) ranges now parse, type-check, and emit runtime range checks on read() and read::<2..>(). StmtKind::Trait and StmtKind::Impl also bring associated type Name declarations into their body generic scope so trait Unwrap: type Out; fn unwrap(self) -> Out checks cleanly. Reversal: make lo/hi non-optional again in TypeKind::Range and remove the TypeAlias loops in crates/cpl-check/src/check/stmt.rs.
  • 22.9 Struct type-parameter defaults (struct Pair<A, B = Int>, struct Graph<V: Index, W = ()>). StructDecl stores generic_defaults: Vec<Option<Type>> and Def::Struct stores resolved defaults: Vec<Option<Ty>>. Omitting trailing type arguments fills them from defaults (substituting earlier parameters) in both cpl-check::def_type and cpl-ir::instantiate_struct before memoization, so Pair<Int> and Pair<Int, Int> are identical types and share one monomorphized struct. Removed the planned::TYPE_PARAM_DEFAULT stub. Reversal: restore planned::TYPE_PARAM_DEFAULT in crates/cpl-syntax/src/planned.rs and remove generic_defaults/defaults.
  • 22.10 Dream ergonomics batch 1: additive const type args (Mod<1e9 + 7>), multi-clause for, guard let, and top-level return <unit>. Auditing all 29 dream files (proto/ports/*/dream/*.cp) showed these four forms recurring across 13 dreams:
    1. type_args parses additive value expressions (1e9 + 7, n - 1) as TypeKind::Value when the token after an int/ident/minus is ,, >, >>, +, -, *, or **.
    2. for_stmt parses comma-separated pat in iter [where cond] clauses and folds them right-to-left into nested StmtKind::For.
    3. guard let ... else: delegates directly to let_stmt().
    4. StmtKind::Return(Some(e)) is accepted at top level (and in Unit-returning fns) when e has type Unit (e.g. return print(ans)), lowering to Stmt::Expr(e); Stmt::Return(None) while still rejecting non-unit top-level return values. Reversal: revert the four parser/check/IR hunks in types.rs, stmt.rs, check/stmt.rs, and lower/stmt.rs.
  • 22.11 Dream ergonomics batch 2: typed streaming reads read<[n]T>(), x with field: v record update, scalar-fill multi-binding var a, b: T = 0, and typed lambda params.
    1. names_a_type in cpl-syntax now accepts TypeKind::Range, index_ty treats arithmetic index expressions ([V.count - 1]) as counted arrays 0..expr, cpl-fmt recognizes lowercase generic calls f<...>(...) via closes_before_lparen, and cpl-ir lowers zero-arg read<T>() / read::<T>() via self.resolve(ty, true) + self.read(spec, span) — one general path for ranges, counted arrays, grids, tuples, and structs.
    2. ExprKind::With { base, updates } is now lowered in cpl-ir for structs and records via a Do block binding base once and reconstructing the struct/record tuple with updated fields, removing the E0501 with stub.
    3. var ones, open, ans: Z = 0 and var ways, next: [1..=m]Z = 0 desugar to N separate StmtKind::Let declarations when the initializer is not a tuple or read(), while keeping tuple/read destructuring intact.
    4. Lambda parameter lists accept optional : Type annotations ((d: Int, l: Low) => ...). This brings proto/ports/ecnerwala/dream/cf2069c.cp (exemplar #1 in design/EXEMPLARS-PORTS.md) to 0 errors in cpl check, and proto/ports/ecnerwala/cf2069c.cp now matches its dream exemplar. Reversal: revert the parser/IR/fmt hunks in expr.rs, types.rs, stmt.rs, call.rs, lower/expr.rs, and spacing.rs.