-
Notifications
You must be signed in to change notification settings - Fork 18
Description
Problem
Array-producing builtins (VECTOR ELM MAP, VECTOR SORT ORDER, ALLOCATE AVAILABLE) produce incorrect results in the interpreter when nested inside other BuiltinFn calls. For example:
MAX(VECTOR ELM MAP(source[*], offsets[*]), 15)returns 15 for element 0 instead of the correct value 30 (when VEM should produce 30 for that element).SUM(VECTOR ELM MAP(...))returns NaN.
The root cause appears to be how array-producing builtins interact with the per-element evaluation context when wrapped inside another BuiltinFn. The outer builtin evaluates the inner array-producing builtin in a context where the per-element iteration state is not correctly propagated, causing the inner builtin to return wrong values or NaN rather than the correctly mapped element.
This is a pre-existing limitation, not introduced by PR #361 (VM vector operations).
Why it matters
- Correctness: Models that compose array-producing builtins inside scalar builtins (a common Vensim/Stella pattern) will silently produce wrong simulation results.
- Developer experience: The failure mode is silent -- wrong values rather than errors -- making it difficult to diagnose.
- Model compatibility: Real-world SD models frequently nest these operations (e.g.,
MAX(VEM(...), threshold),SUM(VEM(...)),MIN(ALLOCATE_AVAILABLE(...), cap)).
Component(s) affected
src/simlin-engine/src/interpreter.rs(per-element evaluation context for array-producing builtins)- Potentially also relevant to VM implementation once engine: vector operations and ALLOCATE AVAILABLE are interpreter-only, not in VM #355 is resolved, since the VM will need to handle the same composition patterns.
Possible approaches
- Ensure array-producing builtins fully materialize their result array before the outer builtin evaluates, so the outer builtin sees the correct per-element value rather than an incomplete or context-dependent intermediate.
- Propagate the per-element iteration index through nested builtin calls so inner array-producing builtins know which element they should compute.
- Review how the interpreter's
calc_eval/ element iteration interacts withBuiltinFn::Vem,BuiltinFn::Vso, andBuiltinFn::AllocateAvailablewhen they appear as arguments to anotherBuiltinFn.
Context
Identified during code review of PR #361 (VM vector operations). The issue is pre-existing in the interpreter and was surfaced by testing nested compositions of array-producing builtins.