Repository navigation
Description
Problem
mdl::writer::equation_to_mdl falls back to emitting the raw XMILE equation text (with _ -> ) whenever Expr0::new cannot parse it:
// src/simlin-engine/src/mdl/writer.rs:1282
match Expr0::new(xmile_eqn, LexerType::Equation) {
Ok(Some(ast)) => expr0_to_mdl_ctx(&ast, ctx),
// Fallback for unparseable equations; best-effort space conversion.
_ => underbar_to_space(xmile_eqn),
}XMILE syntax is not MDL syntax. The fallback therefore writes a .mdl file whose equation means something different when re-imported, with no warning and no error. The builtin-name table (xmile_to_mdl_function_name, which maps int -> INTEGER, smth1 -> SMOOTH, safediv -> ZIDZ, ...) is exactly what gets skipped, and Vensim has no INT, SMTH1, SAFEDIV, ... builtins -- so an unmapped call re-parses as a lookup invocation of an undefined variable.
Reachable from a real .mdl file
The trigger is a stacked unary negative in the datamodel equation, which the engine's own equation parser rejects (#863). #863 states this is "effectively unreachable from real models today" because "every stored datamodel equation originated from the parser, which never produces stacked --". That is not true of the MDL importer: Vensim accepts a space-separated double unary minus, and the importer's xmile_compat formatter collapses it to --.
Verified at e1c1cca9 (branch conveyor-engine), so this is pre-existing:
MDL source `x = - -3` -> imported datamodel equation "--3" -> Expr0::new REJECTS it
Feed that back through the writer and the corruption is silent and compounding. Starting from a legal .mdl:
input x = --(0 ^ INTEGER(0)) (Vensim: -(-(0 ^ INTEGER(0))))
import datamodel eqn = "--(0 ^ INT(0))" <- unparseable XMILE (INTEGER renamed to INT)
write x = --(0 ^ INT(0)) <- raw fallback: XMILE text leaked into MDL
import datamodel eqn = "--(0 ^ LOOKUP(INT, 0))" <- INT is not a Vensim builtin: read as a lookup call
write x = --(0 ^ LOOKUP(INT, 0))
Integer truncation silently became a lookup of a nonexistent table int.
Two independently bad outcomes here:
- Import: a legal Vensim model becomes an uncompilable project (the stored equation
--3does not parse; the variable never compiles). This is engine: print_eqn emits stacked unary negatives (--x) that the equation parser rejects #863's asymmetry, but reached through the importer rather thanprint_eqn. - Export: the writer emits text it cannot vouch for, and the meaning changes on the next import. That is this issue.
How it was discovered
The proptest mdl::writer::proptest_tests::equation_write_reparse_is_fixpoint (src/simlin-engine/src/mdl/writer_proptest.rs:460) fails on the seed below; found during unrelated conveyor work, reproduces at e1c1cca9.
src/simlin-engine/proptest-regressions/mdl/writer_proptest.txt:
cc 250a03c2936a26621a2bd89e4ff19d2ccc03f3bf6c1e2c0b64a22f0db71515a9 # shrinks to expr = Op1(Negative, Op2(Exp, Op1(Negative, Const("0", 0.0, ..)), App(UntypedBuiltinFn("int", [Const("0", 0.0, ..)]))))
The property's prop_filter (xmile_equation_roundtrips(print_eqn(e))) only guards the first write's input, so the case survives: print_eqn yields -(-0 ^ int(0)), which parses; mdl1 is correct (--(0 ^ INTEGER(0))); but the round-trip through parse_mdl produces the stacked--- datamodel equation, and the test compares mdl2 vs mdl3 -- which is where the fallback fires.
Note this is not a missing INT -> INTEGER mapping: xmile_to_mdl_function_name already has that arm (writer.rs:627). The mapping is simply never reached on the fallback path.
Why it matters
Silent semantic corruption of exported models. project_to_mdl_with_warnings (#856) exists precisely so degraded exports are visible to callers; this path degrades and says nothing. The equations most likely to hit it are the ones the writer understands least, so "best-effort space conversion" is the worst possible response.
Component(s)
src/simlin-engine/src/mdl/writer.rs(equation_to_mdl)src/simlin-engine/src/mdl/xmile_compat.rs(importer-side--formatting; the trigger)src/simlin-engine/src/ast/mod.rs+ the equation parser (the engine: print_eqn emits stacked unary negatives (--x) that the equation parser rejects #863 asymmetry)
Possible approaches
- Do not leak XMILE into MDL. On parse failure, either return
Err(an unparseable equation is a structural impossibility for a valid datamodel) or emit the raw text plus anExportWarningon the engine: MDL export has no lossiness contract -- callers can't learn an export was degraded #856 channel. Silent pass-through should not remain an option. - Fix the trigger (engine: print_eqn emits stacked unary negatives (--x) that the equation parser rejects #863): make
print_eqnandxmile_compat's Op1 formatting parenthesize stacked unary negatives (-(-x)), or make the equation parser accept--x. This also fixes the import-side "legal Vensim model imports as uncompilable" bug above, which is the more user-visible half. - Tighten the proptest so the property covers each write, not just the first (
equations_section(mdl1)vsmdl2after normalization), and drop theprop_filteronce (2) lands.
(1) and (2) are complementary: (2) removes today's only known trigger, (1) makes the next one loud instead of silent.
Activity
engine: pin the MDL exponent grouping rules; fix inherited CI failures