metal icon indicating copy to clipboard operation
metal copied to clipboard

Enhance equals() with semantic equivalence

Open jvdb opened this issue 9 years ago • 2 comments

As an example, for commutative operators such as Add and Mul, but also for the logical And, we could change the equals() implementations to also state that x + y equals y + x.

jvdb avatar Feb 10 '17 11:02 jvdb

A problem here is with the contract for Object.hashCode() in Java, which has as second clause:

If two objects are equal according to the equals(Object) method, then calling the hashCode method on each of the two objects must produce the same integer result.

Which will not be satisfied since two semantically equal objects will typically have a different hashcode if they were determined equal based on reordering operands. E.g., x+y and y+x would then be determined equal, but have a different hashcode.

A solution for this would be to keep the current implementations of equals()/hashCode(), but add another method (e.g., semanticEquals()) to determine this kind of equality. This implementation could then be used selectively for some specific operations such as cycle detection in Sub.

jvdb avatar Feb 23 '17 12:02 jvdb

Another solution is to use rewriting to normalize expressions. This could then lead to full equality of originally different expressions. Only thing to consider then is origin tracking so that the rewritten expressions can be traced back to their original form.

jvdb avatar Oct 10 '17 20:10 jvdb