Skip to content

Fix Usage Detection for Repeated Equalities#226

Open
rcosta358 wants to merge 6 commits into
mainfrom
fix-has-usage
Open

Fix Usage Detection for Repeated Equalities#226
rcosta358 wants to merge 6 commits into
mainfrom
fix-has-usage

Conversation

@rcosta358
Copy link
Copy Markdown
Collaborator

Description

This PR fixes the VariableResolver.hasUsage so a matching equality is only excluded as a definition when it appears in a definition under a conjunction. This means that repeated equalities inside nested expressions now count as usages, allowing substitutions to propagate and simplify correctly.

Example

Before

Both x == 1 count has definitions.

x == 1 && (x == 1 ? a(y) : b(y)) // most simplified

After

The first one counts has a definition and the second one counts as an usage.

a(y) // most simplified

Related Issue

None.

Type of change

  • Bug fix
  • New feature
  • Documentation update
  • Code refactoring

Checklist

  • Added/updated tests in ExpressionSimplifierTests and VariableResolverTests
  • mvn test passes locally
  • Updated docs/README if behavior or API changed

@rcosta358 rcosta358 requested a review from CatarinaGamboa May 14, 2026 14:58
@rcosta358 rcosta358 self-assigned this May 14, 2026
@rcosta358 rcosta358 added bug Something isn't working simplification Related to the simplification of expressions labels May 14, 2026
@CatarinaGamboa
Copy link
Copy Markdown
Collaborator

what do you mean by "a matching equality is only excluded as a definition when it appears in a definition under a conjunction"?
It could be the opposite right (x == 1 ? a(y) : b(y)) && x == 1 and it should still simplify
I'm not sure i get the idea

@rcosta358
Copy link
Copy Markdown
Collaborator Author

Those two expressions are equivalent. It's not about the order of the conjunctions, it's about whether the equality appears as a top level conjunct or nested inside another expression.

In both examples, the outer x == 1 treated as the definition, while the x == 1 within the ite is treated as a usage.
In the previous logic both equalities would count as definitions, so x had no usages and the substitution mapping was removed.

Copy link
Copy Markdown
Collaborator

@CatarinaGamboa CatarinaGamboa left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we add a couple more tests to ensure similar but more complex expressions are working?
Maybe like:
mode == 2 && other == 5 && ((mode == 2 && other == 5) ? explicit(param) : start(param)) -> explicit(param)
or
"mode == 2 && other == 3 && (mode == 2 ? (other == 3 ? a(p) : b(p)) : c(p))" -> a(p)

@rcosta358 rcosta358 requested a review from CatarinaGamboa May 15, 2026 15:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working simplification Related to the simplification of expressions

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants