|
| 1 | +package liquidjava.rj_language.opt; |
| 2 | + |
| 3 | +import liquidjava.processor.SimplifiedVCImplication; |
| 4 | +import liquidjava.processor.VCImplication; |
| 5 | +import liquidjava.rj_language.Predicate; |
| 6 | +import liquidjava.rj_language.ast.BinaryExpression; |
| 7 | +import liquidjava.rj_language.ast.Expression; |
| 8 | +import liquidjava.rj_language.ast.GroupExpression; |
| 9 | +import liquidjava.rj_language.ast.Ite; |
| 10 | +import liquidjava.rj_language.ast.LiteralBoolean; |
| 11 | +import liquidjava.rj_language.ast.UnaryExpression; |
| 12 | + |
| 13 | +/** |
| 14 | + * Simplifies VCImplication chains by applying logical identities inside refinements |
| 15 | + */ |
| 16 | +public class VCLogicalSimplification { |
| 17 | + |
| 18 | + /** |
| 19 | + * Applies the first logical simplification available in a VC chain |
| 20 | + */ |
| 21 | + public static VCImplication apply(VCImplication implication) { |
| 22 | + if (implication == null) |
| 23 | + return null; |
| 24 | + |
| 25 | + Expression expression = implication.getRefinement().getExpression(); |
| 26 | + Expression simplified = simplify(expression); |
| 27 | + if (!expression.equals(simplified)) { |
| 28 | + VCImplication result = new SimplifiedVCImplication(implication, new Predicate(simplified), implication); |
| 29 | + result.setNext(implication.getNext() == null ? null : implication.getNext().clone()); |
| 30 | + return result; |
| 31 | + } |
| 32 | + |
| 33 | + VCImplication next = apply(implication.getNext()); |
| 34 | + if (implication.getNext() == null || implication.getNext().equals(next)) |
| 35 | + return implication; |
| 36 | + |
| 37 | + VCImplication result = implication.copyWithRefinement(implication.getRefinement().clone()); |
| 38 | + result.setNext(next); |
| 39 | + return result; |
| 40 | + } |
| 41 | + |
| 42 | + /** |
| 43 | + * Simplifies the first logical identity found inside an expression |
| 44 | + */ |
| 45 | + private static Expression simplify(Expression expression) { |
| 46 | + if (expression instanceof BinaryExpression binary) |
| 47 | + return simplifyBinary(binary); |
| 48 | + if (expression instanceof UnaryExpression unary) |
| 49 | + return simplifyUnary(unary); |
| 50 | + if (expression instanceof Ite ite) |
| 51 | + return simplifyIte(ite); |
| 52 | + if (expression instanceof GroupExpression group) |
| 53 | + return simplifyGroup(group); |
| 54 | + return expression.clone(); |
| 55 | + } |
| 56 | + |
| 57 | + /** |
| 58 | + * Simplifies a binary expression by visiting operands before the current node |
| 59 | + */ |
| 60 | + private static Expression simplifyBinary(BinaryExpression binary) { |
| 61 | + Expression left = binary.getFirstOperand(); |
| 62 | + Expression simplifiedLeft = simplify(left); |
| 63 | + if (!left.equals(simplifiedLeft)) |
| 64 | + return new BinaryExpression(simplifiedLeft, binary.getOperator(), binary.getSecondOperand().clone()); |
| 65 | + |
| 66 | + Expression right = binary.getSecondOperand(); |
| 67 | + Expression simplifiedRight = simplify(right); |
| 68 | + if (!right.equals(simplifiedRight)) |
| 69 | + return new BinaryExpression(left.clone(), binary.getOperator(), simplifiedRight); |
| 70 | + |
| 71 | + Expression simplifiedBinary = simplifyLocalBinary(left, right, binary.getOperator()); |
| 72 | + if (simplifiedBinary != null) |
| 73 | + return simplifiedBinary; |
| 74 | + |
| 75 | + return new BinaryExpression(left.clone(), binary.getOperator(), right.clone()); |
| 76 | + } |
| 77 | + |
| 78 | + /** |
| 79 | + * Simplifies a unary expression by visiting its operand before the current node |
| 80 | + */ |
| 81 | + private static Expression simplifyUnary(UnaryExpression unary) { |
| 82 | + Expression operand = unary.getExpression(); |
| 83 | + Expression simplifiedOperand = simplify(operand); |
| 84 | + if (!operand.equals(simplifiedOperand)) |
| 85 | + return new UnaryExpression(unary.getOp(), simplifiedOperand); |
| 86 | + |
| 87 | + // !!x -> x |
| 88 | + if ("!".equals(unary.getOp()) && isNot(operand)) |
| 89 | + return negatedExpression(operand).clone(); |
| 90 | + |
| 91 | + return new UnaryExpression(unary.getOp(), operand.clone()); |
| 92 | + } |
| 93 | + |
| 94 | + /** |
| 95 | + * Simplifies a ternary expression by visiting condition, then branch, and else branch |
| 96 | + */ |
| 97 | + private static Expression simplifyIte(Ite ite) { |
| 98 | + Expression condition = ite.getCondition(); |
| 99 | + Expression simplifiedCondition = simplify(condition); |
| 100 | + if (!condition.equals(simplifiedCondition)) |
| 101 | + return new Ite(simplifiedCondition, ite.getThen().clone(), ite.getElse().clone()); |
| 102 | + |
| 103 | + Expression thenExpression = ite.getThen(); |
| 104 | + Expression simplifiedThen = simplify(thenExpression); |
| 105 | + if (!thenExpression.equals(simplifiedThen)) |
| 106 | + return new Ite(condition.clone(), simplifiedThen, ite.getElse().clone()); |
| 107 | + |
| 108 | + Expression elseExpression = ite.getElse(); |
| 109 | + Expression simplifiedElse = simplify(elseExpression); |
| 110 | + if (!elseExpression.equals(simplifiedElse)) |
| 111 | + return new Ite(condition.clone(), thenExpression.clone(), simplifiedElse); |
| 112 | + |
| 113 | + return new Ite(condition.clone(), thenExpression.clone(), elseExpression.clone()); |
| 114 | + } |
| 115 | + |
| 116 | + /** |
| 117 | + * Simplifies an expression wrapped in parentheses while preserving the group node |
| 118 | + */ |
| 119 | + private static Expression simplifyGroup(GroupExpression group) { |
| 120 | + Expression expression = group.getExpression(); |
| 121 | + Expression simplified = simplify(expression); |
| 122 | + if (!expression.equals(simplified)) |
| 123 | + return new GroupExpression(simplified); |
| 124 | + return group.clone(); |
| 125 | + } |
| 126 | + |
| 127 | + /** |
| 128 | + * Dispatches a local binary logical identity by operator |
| 129 | + */ |
| 130 | + private static Expression simplifyLocalBinary(Expression left, Expression right, String op) { |
| 131 | + return switch (op) { |
| 132 | + case "&&" -> simplifyConjunction(left, right); |
| 133 | + case "||" -> simplifyDisjunction(left, right); |
| 134 | + case "==" -> simplifyEquality(left, right); |
| 135 | + case "!=" -> simplifyInequality(left, right); |
| 136 | + case "-->" -> simplifyImplication(left, right); |
| 137 | + default -> null; |
| 138 | + }; |
| 139 | + } |
| 140 | + |
| 141 | + /** |
| 142 | + * Applies conjunction identities involving boolean literals and same operands |
| 143 | + */ |
| 144 | + private static Expression simplifyConjunction(Expression left, Expression right) { |
| 145 | + // x && true -> x |
| 146 | + if (isTrue(right)) |
| 147 | + return left.clone(); |
| 148 | + // true && x -> x |
| 149 | + if (isTrue(left)) |
| 150 | + return right.clone(); |
| 151 | + // x && false -> false |
| 152 | + if (isFalse(right)) |
| 153 | + return right.clone(); |
| 154 | + // false && x -> false |
| 155 | + if (isFalse(left)) |
| 156 | + return left.clone(); |
| 157 | + // p && p -> p |
| 158 | + if (left.equals(right)) |
| 159 | + return left.clone(); |
| 160 | + return null; |
| 161 | + } |
| 162 | + |
| 163 | + /** |
| 164 | + * Applies disjunction identities involving boolean literals and same operands |
| 165 | + */ |
| 166 | + private static Expression simplifyDisjunction(Expression left, Expression right) { |
| 167 | + // x || true -> true |
| 168 | + if (isTrue(right)) |
| 169 | + return right.clone(); |
| 170 | + // true || x -> true |
| 171 | + if (isTrue(left)) |
| 172 | + return left.clone(); |
| 173 | + // x || false -> x |
| 174 | + if (isFalse(right)) |
| 175 | + return left.clone(); |
| 176 | + // false || x -> x |
| 177 | + if (isFalse(left)) |
| 178 | + return right.clone(); |
| 179 | + // p || p -> p |
| 180 | + if (left.equals(right)) |
| 181 | + return left.clone(); |
| 182 | + return null; |
| 183 | + } |
| 184 | + |
| 185 | + /** |
| 186 | + * Applies equality identity for same operands |
| 187 | + */ |
| 188 | + private static Expression simplifyEquality(Expression left, Expression right) { |
| 189 | + // x == x -> true |
| 190 | + if (left.equals(right)) |
| 191 | + return new LiteralBoolean(true); |
| 192 | + return null; |
| 193 | + } |
| 194 | + |
| 195 | + /** |
| 196 | + * Applies inequality identity for same operands |
| 197 | + */ |
| 198 | + private static Expression simplifyInequality(Expression left, Expression right) { |
| 199 | + // x != x -> false |
| 200 | + if (left.equals(right)) |
| 201 | + return new LiteralBoolean(false); |
| 202 | + return null; |
| 203 | + } |
| 204 | + |
| 205 | + /** |
| 206 | + * Applies implication identities involving boolean literals and same operands |
| 207 | + */ |
| 208 | + private static Expression simplifyImplication(Expression left, Expression right) { |
| 209 | + // x --> true -> true |
| 210 | + if (isTrue(right)) |
| 211 | + return right.clone(); |
| 212 | + // false --> x -> true |
| 213 | + if (isFalse(left)) |
| 214 | + return new LiteralBoolean(true); |
| 215 | + // true --> x -> x |
| 216 | + if (isTrue(left)) |
| 217 | + return right.clone(); |
| 218 | + // x --> x -> true |
| 219 | + if (left.equals(right)) |
| 220 | + return new LiteralBoolean(true); |
| 221 | + return null; |
| 222 | + } |
| 223 | + |
| 224 | + /** |
| 225 | + * Checks whether an expression is true |
| 226 | + */ |
| 227 | + private static boolean isTrue(Expression expression) { |
| 228 | + return expression instanceof LiteralBoolean literal && literal.isBooleanTrue(); |
| 229 | + } |
| 230 | + |
| 231 | + /** |
| 232 | + * Checks whether an expression is false |
| 233 | + */ |
| 234 | + private static boolean isFalse(Expression expression) { |
| 235 | + return expression instanceof LiteralBoolean literal && !literal.isBooleanTrue(); |
| 236 | + } |
| 237 | + |
| 238 | + /** |
| 239 | + * Checks whether an expression is unary logical negation |
| 240 | + */ |
| 241 | + private static boolean isNot(Expression expression) { |
| 242 | + return expression instanceof UnaryExpression unary && "!".equals(unary.getOp()); |
| 243 | + } |
| 244 | + |
| 245 | + /** |
| 246 | + * Returns the operand of a unary logical negation expression |
| 247 | + */ |
| 248 | + private static Expression negatedExpression(Expression expression) { |
| 249 | + return ((UnaryExpression) expression).getExpression(); |
| 250 | + } |
| 251 | +} |
0 commit comments