Skip to content

XMILE equation printer drops parens for right children of non-commutative operators #327

Closed

Description

@bpowers

Description

The XMILE equation printer in src/simlin-engine/src/ast/mod.rs has a right-child parenthesization bug that causes incorrect output for non-commutative operators (subtraction, division, modulo). Both paren_if_necessary (line 247, using macro child_needs_parens at line 239) and paren_if_necessary1 (line 282, using macro child_needs_parens2 at line 274) use a strictly-greater-than precedence comparison for both left and right children. This means an expression like a - (b - c) is emitted as a - b - c, which changes the semantics due to left-associativity.

Concrete example

  • Input AST: Sub(a, Sub(b, c)) representing a - (b - c)
  • Expected output: a - (b - c)
  • Actual output: a - b - c (which parses as (a - b) - c)

The same problem affects division (a / (b / c) -> a / b / c) and modulo.

Why it matters

This is a correctness bug. Any model that round-trips through the XMILE printer and parser will silently change semantics for expressions involving right-nested non-commutative operators. This affects model serialization (saving XMILE files) and any code path that prints equations back to XMILE format.

Components affected

  • src/simlin-engine/src/ast/mod.rs -- child_needs_parens macro (line 239), child_needs_parens2 macro (line 274), PrintVisitor::walk (lines 365-366), and the Expr2 variant handling (lines 577-578)

Possible approach

This is the same bug that was fixed in the MDL writer (commit ff04a5a). The fix should be ported:

  1. Update both child_needs_parens macros so that right children of non-commutative same-precedence operators get parenthesized when parent_prec == child_prec
  2. Add an is_right_child flag to the call sites in PrintVisitor::walk and the Expr2 variant
  3. Add tests for a - (b - c), a / (b / c), and a mod (b mod c) round-tripping correctly

Context

Identified during review of the MDL writer parenthesization fix (PR #324, commit ff04a5a). The MDL writer had the identical bug and was fixed; the XMILE printer was not updated at the same time.

Activity

  1. bpowers commented on Feb 25, 2026

    @bpowers
    OwnerAuthor

    @codex please fix

  2. chatgpt-codex-connector commented on Feb 25, 2026

    @chatgpt-codex-connector

    Summary

    • Added regression assertions to test_print_eqn for right-nested non-commutative operators so XMILE printing preserves grouping for:
    • Committed the change as engine: add xmile right-child paren tests and created a follow-up PR message via the make_pr tool summarizing this delta on top of the original PR.

    Testing

    • ✅ cargo test -p simlin-engine test_print_eqn
    • ✅ PATH=/tmp/bin:$PATH git commit -m "engine: add xmile right-child paren tests" -m "Add regression coverage for right-nested subtraction, division, and modulo in the XMILE equation printer. These cases are sensitive to associativity and must retain parentheses on the right child to preserve AST semantics." -m "The parenthesization logic is already implemented in this branch, so this change focuses on preventing future regressions and documenting the expected string output for non-commutative operators at equal precedence." (pre-commit hook ran and passed all Rust/TypeScript/Python pipelines)

    View task →

  3. bpowers commented on Feb 25, 2026

    @bpowers
    OwnerAuthor

    Fixed in #333

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