Skip to content

Add sum(range) builtin that lowers to closed-form arithmetic series #475

Closed

Description

@godofecht

Motivation

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.

This would make code like:

println(sum(0..1000 step 3) + sum(0..1000 step 5) - sum(0..1000 step 15))

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:

  1. Accepts start..end step step as input.
  2. When start, end, and step are all compile-time constants, folds to a constant.
  3. 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).
  4. 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:

println(sum(0..1000 step 3) + sum(0..1000 step 5) - sum(0..1000 step 15))

Activity

  1. added
    enhancement
    on Aug 16, 2026
  2. godofecht commented on Aug 17, 2026

    @godofecht
    ContributorAuthor

    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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancement

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions