Repository navigation
Description
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_operandsrc/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_loudlypins 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
engine: one ordinal fallback for undeclared dimension pairs
[-]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[/+]