Skip to content

Add built-in unit aliases for currency and generic units#396

Merged
bpowers merged 1 commit intomainfrom
claude/expand-unit-equivalences-UZWVW
Mar 16, 2026
Merged

Add built-in unit aliases for currency and generic units#396
bpowers merged 1 commit intomainfrom
claude/expand-unit-equivalences-UZWVW

Conversation

@bpowers
Copy link
Owner

@bpowers bpowers commented Mar 16, 2026

Summary

This change adds two new built-in unit definitions to the Simlin engine to improve unit handling for common use cases: currency (dollar) and generic units.

Key Changes

  • Added "$" as a built-in unit with aliases "dollar", "dollars", and "$s" to standardize currency unit handling
  • Added "unit" as a built-in unit with alias "units" for generic dimensionless unit references
  • Added comprehensive test coverage for both new built-in units:
    • test_builtin_dollar_equivalences(): Verifies that $, Dollar, Dollars, and $s all resolve to the same unit and that they properly cancel out in ratios
    • test_builtin_unit_equivalences(): Verifies that Unit and Units both resolve to the unit unit

Implementation Details

The built-in units follow the same pattern as existing aliases like "person"/"people". They are registered in the builtin_units array in the Context::new_with_builtins() method, ensuring they are only added if the model doesn't already define units with those names. The tests confirm that case-insensitive matching works correctly and that unit cancellation operates as expected.

https://claude.ai/code/session_01SFHWDB5sKsuobFpi8wQhLP

Add dollar ($, Dollar, Dollars, $s) and generic unit (Unit, Units)
equivalence classes to the built-in unit aliases. These match the
default unit equivalences that Vensim ships with, improving
compatibility when importing Vensim models that use these units
without explicit equivalence declarations.

https://claude.ai/code/session_01SFHWDB5sKsuobFpi8wQhLP
@bpowers bpowers merged commit fa2b8c6 into main Mar 16, 2026
10 checks passed
@bpowers bpowers deleted the claude/expand-unit-equivalences-UZWVW branch March 16, 2026 15:10
@claude
Copy link

claude bot commented Mar 16, 2026

Code Review

Reviewed the changes in src/simlin-engine/src/units.rs.

No bugs found. The implementation correctly follows the established pattern for builtin unit aliases (matching person/people, year/years, etc.). Specific observations:

  • $ as a unit name: The lexer already supports $ as a valid identifier character in LexerType::Units mode (via is_identifier_start / is_identifier_continue), so bare $ lexes and parses correctly.
  • $s alias: Lexes as a single identifier $s in units mode — no ambiguity.
  • "unit" builtin: Does not conflict with "dmnl" (the engine's dimensionless marker) or any other internal concept. The existing conflict-detection logic correctly skips builtins when the model already defines a unit with that name.
  • Canonicalization: $ passes through canonicalize() unchanged (not affected by lowercasing, whitespace replacement, or period-to-middle-dot conversion).
  • Test coverage: Both new tests verify alias resolution and unit cancellation, consistent with the existing test_year_years_builtin_alias pattern.

Verdict: Correct. The patch introduces no bugs and existing code/tests will not break.

@codecov
Copy link

codecov bot commented Mar 16, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 76.60%. Comparing base (3c07a42) to head (9e7cd6f).
⚠️ Report is 1 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main     #396      +/-   ##
==========================================
+ Coverage   76.57%   76.60%   +0.02%     
==========================================
  Files         143      143              
  Lines       37026    37026              
==========================================
+ Hits        28353    28363      +10     
+ Misses       8673     8663      -10     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants