@@ -99,6 +99,7 @@ private static Expression simplifyUnary(UnaryExpression unary, List<Expression>
9999 if (!operand .equals (simplifiedOperand ))
100100 return new UnaryExpression (unary .getOp (), simplifiedOperand );
101101
102+ // -(-x) -> x
102103 if ("-" .equals (unary .getOp ()) && isNegation (operand ))
103104 return negatedExpression (operand ).clone ();
104105
@@ -157,14 +158,19 @@ private static Expression simplifyLocalBinary(Expression left, Expression right,
157158 * Applies addition identities involving zero and unary negation.
158159 */
159160 private static Expression simplifyAddition (Expression left , Expression right ) {
161+ // x + 0 -> x
160162 if (isZero (right ))
161163 return left .clone ();
164+ // 0 + x -> x
162165 if (isZero (left ))
163166 return right .clone ();
167+ // x + (-x) -> 0
164168 if (isNegation (right ) && left .equals (negatedExpression (right )))
165169 return new LiteralInt (0 );
170+ // (-x) + x -> 0
166171 if (isNegation (left ) && negatedExpression (left ).equals (right ))
167172 return new LiteralInt (0 );
173+ // x + (-y) -> x - y
168174 if (isNegation (right ))
169175 return new BinaryExpression (left .clone (), "-" , negatedExpression (right ).clone ());
170176 return null ;
@@ -174,12 +180,16 @@ private static Expression simplifyAddition(Expression left, Expression right) {
174180 * Applies subtraction identities involving zero, same operands, and unary negation.
175181 */
176182 private static Expression simplifySubtraction (Expression left , Expression right ) {
183+ // x - 0 -> x
177184 if (isZero (right ))
178185 return left .clone ();
186+ // 0 - x -> -x
179187 if (isZero (left ))
180188 return new UnaryExpression ("-" , right .clone ());
189+ // x - x -> 0
181190 if (left .equals (right ))
182191 return new LiteralInt (0 );
192+ // x - (-y) -> x + y
183193 if (isNegation (right ))
184194 return new BinaryExpression (left .clone (), "+" , negatedExpression (right ).clone ());
185195 return null ;
@@ -189,12 +199,16 @@ private static Expression simplifySubtraction(Expression left, Expression right)
189199 * Applies multiplication identities involving one and zero.
190200 */
191201 private static Expression simplifyMultiplication (Expression left , Expression right ) {
202+ // x * 1 -> x
192203 if (isOne (right ))
193204 return left .clone ();
205+ // 1 * x -> x
194206 if (isOne (left ))
195207 return right .clone ();
208+ // x * 0 -> 0
196209 if (isZero (right ))
197210 return right .clone ();
211+ // 0 * x -> 0
198212 if (isZero (left ))
199213 return left .clone ();
200214 return null ;
@@ -204,10 +218,13 @@ private static Expression simplifyMultiplication(Expression left, Expression rig
204218 * Applies division identities, using prior non-zero premises when needed.
205219 */
206220 private static Expression simplifyDivision (Expression left , Expression right , List <Expression > nonZeroTerms ) {
221+ // x / 1 -> x
207222 if (isOne (right ))
208223 return left .clone ();
224+ // 0 / x -> 0 (x != 0)
209225 if (isZero (left ) && isKnownNonZero (right , nonZeroTerms ))
210226 return left .clone ();
227+ // x / x -> 1 (x != 0)
211228 if (left .equals (right ) && isKnownNonZero (right , nonZeroTerms ))
212229 return new LiteralInt (1 );
213230 return null ;
@@ -217,8 +234,10 @@ private static Expression simplifyDivision(Expression left, Expression right, Li
217234 * Applies modulo identities, using prior non-zero premises when needed.
218235 */
219236 private static Expression simplifyModulo (Expression left , Expression right , List <Expression > nonZeroTerms ) {
237+ // x % 1 -> 0
220238 if (isOne (right ))
221239 return new LiteralInt (0 );
240+ // x % x -> 0 (x != 0)
222241 if (left .equals (right ) && isKnownNonZero (right , nonZeroTerms ))
223242 return new LiteralInt (0 );
224243 return null ;
0 commit comments