Skip to content

engine: MDL writer silently rebinds user variables named like Vensim control/builtin keywords (Time Step, Time, PI, ...) #853

Closed

Description

@bpowers

Summary

The MDL writer silently rebinds references to user variables whose names collide with Vensim control variables or bare builtin keywords (Time Step, Time, Initial Time, Final Time, SAVEPER). The definition line is emitted with the user's name, but every reference to the variable is printed as the builtin keyword, so re-importing the exported MDL silently changes simulation results.

Repro

An XMILE model with:

  • aux Time Step = 5
  • aux if = Time_Step * 2

exports to MDL where:

  • the definition line reads Time Step = 5 (user variable, kept), but
  • the reference in if's equation is routed through mdl_bare_keyword (src/simlin-engine/src/mdl/writer.rs:181, used in the expression printer's zero-arg App branch around line 648) and printed as the builtin TIME STEP.

On re-import, the equation reads the simulation dt (DT * 2) instead of the user's variable -- silently changing results. The exported file also contains two conflicting TIME STEP definitions: the user's and the .Control section's.

The same class of corruption applies to user variables named Time, Initial Time, Final Time, and SAVEPER.

Related import-direction sibling: PI

A user variable literally named PI (e.g. test/metasd/industrial-dynamics/IDch15/IDch15d.mdl has PI=3.14159) has its references rewritten to the engine builtin pi() on import, which then exports as unparseable PI(). Same root confusion between user-variable names and zero-arity builtin/keyword names, in the opposite direction.

Root cause

The MDL expression printer is a context-free printer today: it has no access to the model's variable-name set, so it cannot distinguish "zero-arg builtin call that should print as a bare keyword" from "reference to a user variable that happens to share the keyword's canonical name". (Upstream, the XMILE-side parse likely resolves the colliding name as the zero-arity builtin in the first place, so the corruption may begin before the writer -- see #681 for the same shadowing class with inf.)

Why it matters

  • Severity: high -- silent semantic corruption. Simulation results change on an export/import round trip with no diagnostic.
  • Exported MDL can also be structurally invalid (duplicate TIME STEP definitions; unparseable PI()).

Components affected

  • src/simlin-engine/src/mdl/writer.rs (mdl_bare_keyword, expression printer walk)
  • XMILE-side equation parsing / builtin resolution (zero-arity builtin shadowing of user variables)
  • MDL import (PI direction)

Possible approaches

  • Thread the model's variable-name set into the MDL expression printer so keyword-vs-variable is decided per name; quote or rename colliding user variables on export (and keep the reference spelling consistent with the definition line).
  • On the import/parse side, prefer a declared user variable over a zero-arity builtin when a name collides (mirroring whatever resolution engine: bare builtin name silently shadows a same-named variable #681 lands on for inf).
  • Add round-trip tests for user variables named Time Step, Time, Initial Time, Final Time, SAVEPER, and PI.

Related issues

Discovery context

Identified while auditing MDL export fidelity: the writer's bare-keyword mapping was observed rebinding references without consulting the model's variable names.

Activity

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

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions