return new Subtraction(l, r);
}
private Expression simplify() {
Expression l = lhs.evaluate();
Expression r = rhs.evaluate();
if (l.equals(r)) { // a - a = 0
return Naturals.zero();
} else if (l instanceof Addition && ((Addition) l).rhs().equals(r)) { // (a + b) - b = a
return ((Addition) l).lhs();
// contradicts to
// } else if (l instanceof Negation) { // -a - b = -(a + b)
// return new Negation(new Addition(((Negation) l).getChild(), r)).evaluate();
} else if (r instanceof ANumber && ((ANumber) r).isNegative()) { // a - (-b) = a + b
return new Addition(l, ((ANumber) r).negate()).evaluate();
} else if (r instanceof Negation) { // a - (-b) = a + b
return new Addition(l, ((Negation) r).getChild()).evaluate();
} else if (l instanceof Operation && l.getPrecedence() == Precedence.Addition) {
return new Sum(l, new Negation(r)).evaluate();
} else if (r instanceof Operation && r.getPrecedence() == Precedence.Addition) {
return new Sum(l, new Negation(r)).evaluate();
}
return new Subtraction(l, r);
}