You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Add sum(range) builtin that lowers to closed-form arithmetic series #475
Flow's for n in 0..limit step step construct is a loop primitive. For arithmetic progressions, the sum has a closed form: step * k * (k + 1) / 2 where k = (limit - 1) / step. The compiler could compute this at compile time or generate the closed-form C expression instead of emitting a loop.
compile to a constant or a handful of arithmetic operations rather than iterating 333 + 200 + 66 = 599 times.
Current state
sum exists in lib/stdlib/array.flow but only for array<f32>. There is no sum overload that accepts a range expression. The for n in start..end step step construct lowers to a C for loop with an i32 counter.
Proposal
Add a sum(range) builtin (or range method) that:
Accepts start..end step step as input.
When start, end, and step are all compile-time constants, folds to a constant.
When they are runtime values, generates the closed-form arithmetic series expression: step * k * (k + 1) / 2 where k = (end - 1 - start) / step (adjusted for direction).
Falls back to iterative summation for non-arithmetic ranges (if such ranges ever exist).
This fits Flow's philosophy that "the model is the program." The mathematical identity behind the sum is the program; the compiler should not require the programmer to manually encode it as a loop.
Use case
Project Euler problem 001 (sum of multiples of 3 or 5 below 1000) currently requires a helper function with an explicit loop. With this builtin, the solution would be a single expression:
Cleanup/design pass note: this currently has a hidden prerequisite. Flow's start..end step step is represented as part of for syntax, not as a general first-class expression/value that can be passed to sum or assigned to a variable. So this should not be implemented as a one-off parser special case for sum(...). The clean implementation order is: (1) introduce an arithmetic-range expression/value carrying start/end/step + end-exclusivity/direction semantics; (2) type/lower it consistently in C and MLIR; (3) implement sum(range) reduction, with constant folding for constant bounds and overflow-safe closed-form lowering for runtime bounds; (4) test positive/negative step, nonzero start, empty ranges, exact/non-exact endpoints, and integer overflow behavior. #476 should build on the same representation.
Activity
godofecht commentedon Aug 17, 2026
Cleanup/design pass note: this currently has a hidden prerequisite. Flow's
start..end step stepis represented as part offorsyntax, not as a general first-class expression/value that can be passed tosumor assigned to a variable. So this should not be implemented as a one-off parser special case forsum(...). The clean implementation order is: (1) introduce an arithmetic-range expression/value carrying start/end/step + end-exclusivity/direction semantics; (2) type/lower it consistently in C and MLIR; (3) implementsum(range)reduction, with constant folding for constant bounds and overflow-safe closed-form lowering for runtime bounds; (4) test positive/negative step, nonzero start, empty ranges, exact/non-exact endpoints, and integer overflow behavior. #476 should build on the same representation.