Repository navigation
Description
Problem
A module-function call inside an apply-to-all body (target[State] = SMTH1(x[State], 1)) hoists its computed argument out of the body into a per-element scalar helper aux. Because a scalar helper has no active axis of its own for lowering to resolve a dimension name against, builtins_visitor::substitute_dimension_refs rewrites the argument to the active element before the hoist. That rewrite is a second place a cross-dimension reference is resolved, and in two shapes it gives a different answer from the plain equation target[State] = x[State] -- so the same reference reads a different element depending only on whether it is wrapped in a module-function call.
Both are silent wrong numbers (the model compiles and runs), except where the wrong index runs off the end of the source, which is refused.
1. A subscript naming the iterated dimension is read by ordinal (ITERATED_ELEMENT_BY_ORDINAL)
x is declared over Region; State carries an explicit element map onto Region; the target iterates State.
- The plain equation
target[State] = x[State]resolves, since compiler-unification Phase 6b (5a50a404), throughDimensionsContext::resolve_mapped_read(src/simlin-engine/src/dimensions.rs): the element's own name on the source axis first, then the declared map, then a mapped parent. - The hoisted argument goes through
substitute_dimension_refs' active-dimension arm (src/simlin-engine/src/builtins_visitor.rs, theVararm's loop overself.dimension_names), which rewritesx[State]to the qualified elementx[State·Steel].ast/expr1.rs::constify_dimensionsthen foldsState·Steelto that element's ordinal (viascope.dimensions.lookup), so the helper indexesxpositionally.
Measured on the fixture in src/simlin-engine/src/mapped_reference_semantics_tests.rs (source Region{Ruby=10, Rose=20, Reed=30} unless noted; target reads listed in the target's declared order):
| Element map | Plain x[State] |
Hoisted SMTH1(x[State], 1) |
|---|---|---|
Positional (maps_to, no element list) |
[10, 20, 30] |
[10, 20, 30] (agree -- the control) |
Permuted: Steel->Reed, Slate->Ruby, Stone->Rose |
[30, 10, 20] |
[10, 20, 30] |
Many-to-one (3 State onto Region{Ruby, Rose}): Steel->Ruby, Slate->Rose, Stone->Ruby |
[10, 20, 10] |
refused (NotSimulatable through the assembly channel: ordinal 2 is off the end of a 2-element source) |
Reverse cardinality (State{Steel, Slate} onto 3 Region): Steel->Reed, Slate->Ruby |
[30, 10] |
[10, 20] |
Shared element names (Region{Ann=10, Bob=20, Cal=30}, State{Cal, Ann, Bob}, map Cal->Bob, Ann->Cal, Bob->Ann) |
[30, 10, 20] (name identity first) |
[10, 20, 30] |
The substitution cannot close this on its own: it does not know the source variable's own axis (the parse deliberately withholds the model's variable shapes for incrementality), and a qualified element is the only spelling a scalar helper can carry. Its foreign-dimension arm already routes through resolve_mapped_read; it is specifically the iterated-dimension arm that folds to an ordinal.
2. A target that repeats a dimension substitutes both positions to the first axis (REPEATED_DIMENSION_FIRST_AXIS)
State{r1, r2}, pop[State,State] with pop[r1,r1]=11, pop[r1,r2]=12, pop[r2,r1]=21, pop[r2,r2]=22, target target[State,State].
- Plain
target[State,State] = pop[State,State](since 6b) pairs subscript positions with active axes one to one: element(r1,r2)readspop[r1,r2]. Reads, in(r1,r1), (r1,r2), (r2,r1), (r2,r2)order:[11, 12, 21, 22]. - Hoisted
target[State,State] = SMTH1(pop[State,State], 1):substitute_dimension_refsmatches each subscript index by dimension name againstdimension_namesand takes the first hit, so both positions resolve to the first active axis's element --(r1,r2)readspop[r1,r1]. Reads:[11, 11, 22, 22].
Same class as #974 / #986: a dimension name is not an axis identity. The Subscript arm hands each index to substitute_dimension_refs independently, so the rewrite is position-blind.
Why it matters
- Silent wrong numbers under any non-positional element map or any self-repeating dimension, triggered only by wrapping an expression in
SMTH1/DELAY/PREVIOUS-style calls. A modeller who refactorstarget[State] = x[State]intotarget[State] = SMTH1(x[State], 1)changes which element is read, with no diagnostic. - Phase 6b made the plain equation follow the map (the engine: permuted element maps were read by position, giving wrong values #756 / engine: a many-to-one element map reference failed to compile #753 / engine: subrange dimension reference reads the parent array positionally (out[Sub] = src[Sub] is silently wrong) #1029 family); the hoisted path is now the one remaining place that resolves the iterated-dimension spelling positionally, so the two paths disagree exactly where the map is non-trivial.
- Both divergences are currently pinned as recorded facts, not endorsed:
mapped_reference_semantics_tests::a_hoisted_argument_reads_what_the_plain_equation_reads(thehoisted_divergencetable and theITERATED_ELEMENT_BY_ORDINAL/REPEATED_DIMENSION_FIRST_AXISkinds) and the "Phase 7.3 semantic divergences" paragraph ofdocs/design-plans/2026-08-25-compiler-unification.md. Fixing either means flipping the corresponding rows toNone(must agree) and deleting the kind.
Components affected
src/simlin-engine/src/builtins_visitor.rs--substitute_dimension_refs(the active-dimensionVararm; the position-blindSubscriptarm).src/simlin-engine/src/ast/expr1.rs--constify_dimensions(folds a qualified element to its ordinal regardless of which axis it is used against).src/simlin-engine/src/dimensions.rs--resolve_mapped_read(the rule the plain equation uses and the hoisted path should share).src/simlin-engine/src/mapped_reference_semantics_tests.rs-- the hoisted-argument column that pins both.
Possible approaches
For (1) -- a decision, not just a chunk. The principled fix is compiler-side: when a qualified element Dim·Elem is used as a subscript of a variable whose axis at that position is NOT Dim, resolve it through resolve_mapped_read(source_axis, Dim, Elem) instead of folding to the ordinal in constify_dimensions. That is the same rule the compiler applies to the bare dimension name, so the hoisted helper (which can only carry the qualified spelling) would read what the plain equation reads. It also changes the semantics of a hand-written plain target[State] = x[State·Steel] (today: ordinal; after: follows the map), which is why it needs an explicit decision rather than being folded into a compiler-unification chunk. Alternatives worth weighing against it: (a) a shaped (arrayed) helper that keeps the bare x[State] and resolves at lowering like the parent -- the direction Phase 7.5 already leans for the HELPER_REFUSED_IN_LOWERING divergence; (b) refusing non-positional maps under a hoisted iterated-dimension read loudly until (a) or the compiler rule lands, so the disagreement is at least not silent.
For (2) -- contained. Substitute by axis position rather than by name: when the active dimensions repeat a name, the k-th subscript position naming D takes the k-th active axis named D (walk the Subscript args positionally against self.dimensions, matching the compiler's one-to-one pairing -- verify the exact pairing rule against the 6b compiler before pinning it). This is independent of (1) and can land on its own.
Either way, the gate is the existing column: every cell of a_hoisted_argument_reads_what_the_plain_equation_reads must read what the plain equation reads, with the fixed kind removed from DIVERGENCE_KINDS.
Context
Surfaced while rebasing the compiler-unification Phase 7.3 work onto Phase 6b (5a50a404, branch compiler-unification-v2, engine crate). Pre-existing in the sense that the hoisted path has always resolved by ordinal; it became a divergence when 6b taught the plain equation to follow the map. Related but distinct: #756 / #753 / #1029 (the plain-equation positional family that 6b addresses), #974 / #986 (LTM-side "dimension name is not an axis identity" instances), #1034 (a different hoisted-helper defect: phase-less synthetic_ident).
Activity
engine: implicit modules and hoisted args as parsed data
engine: element-scoped helpers; structural captures
engine: implicit modules and hoisted args as parsed data
engine: element-scoped helpers; structural captures