Skip to content

engine: SUM(pop * 2) in an arrayed equation fails with a cryptic error #1051

Open

Description

@bpowers

In an apply-to-all equation, a reducer over an expression such as x[region] = SUM(pop * 2) is refused with an internal-sounding error, while SUM(pop) * 2 compiles.
The error should tell the modeler what to write instead.

Example

pop[region] a stock with values 100, 200, 300; x[region] = SUM(pop * 2):

variable 'x' failed to compile: codegen rejected the lowered expressions:
an array operand here must be a variable, a subscripted array or an array temp,
but it is an arithmetic or comparison expression

The same refusal hits SUM(pop[region] * 2), SUM(-pop), SUM(pop * w), SUM(IF pop > 150 THEN pop ELSE 0), the other reducers (MIN, MAX, STDDEV, SIZE; single-argument MEAN compiles), and a scalar equation x = SUM(s * 2) with s scalar.
SUM(pop[*] * 2) compiles and gives 1200 for every element.

The engine's rule is that a bare pop inside an apply-to-all equation means the current element pop[region], even inside a reducer, so SUM(pop) is just pop[e].
Under that rule SUM(pop * 2) is a sum of one value, 200 400 600.
Before #1040 these spellings ran and returned that value times 3 (the size of region), silently wrong; the refusal replaced it.

Why it matters

Seen only in constructed models; no corpus model has the shape.
A modeler who writes SUM(pop * 2) in an arrayed equation almost certainly means the whole-array sum (1200), not 200 400 600.
Quietly returning the one-value sum would surprise them, so a clear refusal is better.

Fix

Keep the refusal and make it actionable: say that inside an apply-to-all equation pop means the current element, so the argument is a single value, and suggest pop[*] for the whole array.
The check belongs where the reducer's argument turns out to have no array shape, not in codegen's generic fallback.

Alternative: compile it as a sum of one value by materializing the scalar argument into a one-element temporary array (remove the dims.is_empty() early return in materialize_view_operand).
Moderate cost, for a number the modeler probably did not mean.

Pointers

  • src/simlin-engine/src/compiler/array_operand.rs: materialize_view_operand
  • src/simlin-engine/src/compiler/codegen.rs: the fallback arm that formats "an array operand here must be ..."
  • src/simlin-engine/src/array_operand_materialization_tests.rs: a_reducer_over_a_zero_d_operand_in_an_apply_to_all_body_is_refused_loudly pins today's text
  • engine: SUM(pop[coast]) error blames pop, not the subrange coast #1052: same kind of reducer error, should suggest the * spelling the same way

Activity

  1. changed the title [-]engine: reducer over a scalar-shaped expression is refused (SUM(pop * 2) in an apply-to-all body, SUM(s * 2)) where SUM(pop) * 2 compiles -- the materializer declines a 0-d operand; origin/main computes it x|D|[/-] [+]engine: SUM(pop * 2) in an arrayed equation fails with a cryptic error[/+] on Oct 12, 2026
  2. added
    engine
    enhancement
    and removed on Oct 12, 2026
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

    engineenhancement

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions