Skip to content

Commit 8db7a47

Browse files
committed
Simplify Symmetric Equalities
1 parent 79ca87d commit 8db7a47

File tree

2 files changed

+23
-12
lines changed

2 files changed

+23
-12
lines changed

liquidjava-verifier/src/main/java/liquidjava/rj_language/opt/ExpressionSimplifier.java

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,12 @@ private static ValDerivationNode simplifyValDerivationNode(ValDerivationNode nod
6565
return leftSimplified;
6666

6767
// collapse identical sides (x && x => x)
68-
if (leftSimplified.getValue().toString().equals(rightSimplified.getValue().toString())) {
68+
if (leftSimplified.getValue().equals(rightSimplified.getValue())) {
69+
return leftSimplified;
70+
}
71+
72+
// collapse symmetric equalities (e.g. x == y && y == x => x == y)
73+
if (isSymmetricEquality(leftSimplified.getValue(), rightSimplified.getValue())) {
6974
return leftSimplified;
7075
}
7176

@@ -78,6 +83,19 @@ private static ValDerivationNode simplifyValDerivationNode(ValDerivationNode nod
7883
return node;
7984
}
8085

86+
private static boolean isSymmetricEquality(Expression left, Expression right) {
87+
if (left instanceof BinaryExpression b1 && "==".equals(b1.getOperator()) && right instanceof BinaryExpression b2
88+
&& "==".equals(b2.getOperator())) {
89+
90+
Expression l1 = b1.getFirstOperand();
91+
Expression r1 = b1.getSecondOperand();
92+
Expression l2 = b2.getFirstOperand();
93+
Expression r2 = b2.getSecondOperand();
94+
return l1.equals(r2) && r1.equals(l2);
95+
}
96+
return false;
97+
}
98+
8199
/**
82100
* Checks if an expression is redundant (e.g. true or x == x)
83101
*/

liquidjava-verifier/src/test/java/liquidjava/rj_language/opt/ExpressionSimplifierTest.java

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -487,9 +487,9 @@ void testSameExpressionTwiceShouldSimplifyToSingle() {
487487
}
488488

489489
@Test
490-
void testCircularDependencyShouldNotSimplify() {
490+
void testSymmetricEqualityShouldSimplify() {
491491
// Given: x == y && y == x
492-
// Expected: x == y && y == x (should not be simplified to "true")
492+
// Expected: x == y
493493

494494
Expression varX = new Var("x");
495495
Expression varY = new Var("y");
@@ -502,15 +502,8 @@ void testCircularDependencyShouldNotSimplify() {
502502

503503
// Then
504504
assertNotNull(result, "Result should not be null");
505-
assertEquals("x == y && y == x", result.getValue().toString(),
506-
"Circular dependency should not be simplified to a boolean literal");
507-
508-
// The result should be the original expression unchanged
509-
assertInstanceOf(BinaryExpression.class, result.getValue(), "Result should still be a binary expression");
510-
BinaryExpression resultExpr = (BinaryExpression) result.getValue();
511-
assertEquals("&&", resultExpr.getOperator(), "Operator should still be &&");
512-
assertEquals("x == y", resultExpr.getFirstOperand().toString(), "Left operand should be x == y");
513-
assertEquals("y == x", resultExpr.getSecondOperand().toString(), "Right operand should be y == x");
505+
assertEquals("x == y", result.getValue().toString(),
506+
"Symmetric equality should be simplified to a single equality");
514507
}
515508

516509
@Test

0 commit comments

Comments
 (0)