|
| 1 | +package liquidjava.processor.refinement_checker; |
| 2 | + |
| 3 | +import java.util.ArrayList; |
| 4 | +import java.util.IdentityHashMap; |
| 5 | +import java.util.LinkedHashMap; |
| 6 | +import java.util.List; |
| 7 | +import java.util.Map; |
| 8 | +import java.util.Optional; |
| 9 | + |
| 10 | +import liquidjava.diagnostics.errors.LJError; |
| 11 | +import liquidjava.processor.context.RefinedVariable; |
| 12 | +import liquidjava.processor.context.VariableInstance; |
| 13 | +import liquidjava.rj_language.Predicate; |
| 14 | +import liquidjava.utils.constants.Ops; |
| 15 | +import spoon.reflect.code.CtDo; |
| 16 | +import spoon.reflect.code.CtExpression; |
| 17 | +import spoon.reflect.code.CtFor; |
| 18 | +import spoon.reflect.code.CtLoop; |
| 19 | +import spoon.reflect.code.CtWhile; |
| 20 | + |
| 21 | +/** |
| 22 | + * The invariants inferred for the {@code int} local variables a loop changes, with the Houdini algorithm (Flanagan and |
| 23 | + * Leino): of their candidates (see {@link InvariantCandidates}), those that the facts before the loop prove are assumed |
| 24 | + * at the loop head; each check of the body drops those not proved where the next iteration starts, until a check drops |
| 25 | + * none. What remains holds before the loop and after every iteration that goes on to the next one, so it holds at the |
| 26 | + * loop head on every iteration. The proofs are silent: a candidate that is not proved is only dropped. |
| 27 | + * |
| 28 | + * <p> |
| 29 | + * Where the continues of the body join, at its end, the values of what the loop changes are forgotten. The facts proved |
| 30 | + * at every continue and at the end of the body are kept there: the candidates, and whether each variable grew or shrank |
| 31 | + * since the loop head. |
| 32 | + */ |
| 33 | +class LoopInvariants { |
| 34 | + |
| 35 | + /** The most checks of the body that drop candidates, after which all are dropped */ |
| 36 | + static final int MAX_ROUNDS = 8; |
| 37 | + |
| 38 | + private final RefinementTypeChecker checker; |
| 39 | + private final CtLoop loop; |
| 40 | + private final List<String> variables; |
| 41 | + private final List<Predicate> candidates = new ArrayList<>(); |
| 42 | + // the instance of each variable at the loop head |
| 43 | + private final Map<String, VariableInstance> head = new LinkedHashMap<>(); |
| 44 | + // the refinement of each instance before facts were added to it |
| 45 | + private final Map<VariableInstance, Predicate> declared = new IdentityHashMap<>(); |
| 46 | + private int rounds; |
| 47 | + private boolean dropped; |
| 48 | + // whether the point of the iteration being checked can be reached |
| 49 | + private boolean reachable; |
| 50 | + // the facts proved at every continue reached so far, null if none was reached |
| 51 | + private List<Predicate> atContinues; |
| 52 | + |
| 53 | + /** |
| 54 | + * Keeps the candidates that the facts before the loop prove; to be created before what the loop changes is |
| 55 | + * forgotten |
| 56 | + * |
| 57 | + * @param variables |
| 58 | + * the {@code int} local variables the loop changes |
| 59 | + */ |
| 60 | + LoopInvariants(RefinementTypeChecker checker, CtLoop loop, List<String> variables) { |
| 61 | + this.checker = checker; |
| 62 | + this.loop = loop; |
| 63 | + this.variables = variables; |
| 64 | + for (String variable : variables) { |
| 65 | + Optional<String> before = checker.getContext().getLastVariableInstance(variable) |
| 66 | + .map(RefinedVariable::getName); |
| 67 | + for (Predicate candidate : InvariantCandidates.of(variable, before, condition(loop))) |
| 68 | + if (!candidates.contains(candidate) && proved(candidate)) |
| 69 | + candidates.add(candidate); |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + /** Starts checking an iteration: the variables at the loop head satisfy the candidates */ |
| 74 | + void startIteration() { |
| 75 | + for (String variable : variables) |
| 76 | + checker.getContext().getLastVariableInstance(variable).ifPresent(instance -> head.put(variable, instance)); |
| 77 | + addToLastInstances(candidates); |
| 78 | + dropped = false; |
| 79 | + reachable = true; |
| 80 | + atContinues = null; |
| 81 | + } |
| 82 | + |
| 83 | + void recordContinue() { |
| 84 | + List<Predicate> proved = provedForNextIteration(); |
| 85 | + if (atContinues != null) |
| 86 | + proved.retainAll(atContinues); |
| 87 | + atContinues = proved; |
| 88 | + } |
| 89 | + |
| 90 | + /** |
| 91 | + * At the end of the body, where its continues join, before the values there are forgotten |
| 92 | + * |
| 93 | + * @param completes |
| 94 | + * whether the end of the body can be reached without a continue |
| 95 | + * |
| 96 | + * @return the facts that hold on every path that reaches the join, for {@link #joined} |
| 97 | + */ |
| 98 | + List<Predicate> atJoin(boolean completes) { |
| 99 | + if (completes) |
| 100 | + recordContinue(); |
| 101 | + return atContinues == null ? List.of() : atContinues; |
| 102 | + } |
| 103 | + |
| 104 | + /** After the continues joined: the new instances of the variables satisfy the facts {@link #atJoin} returned */ |
| 105 | + void joined(List<Predicate> facts) { |
| 106 | + addToLastInstances(facts); |
| 107 | + } |
| 108 | + |
| 109 | + /** |
| 110 | + * Marks the end of the body |
| 111 | + * |
| 112 | + * @param completes |
| 113 | + * whether the end of the body can be reached without a continue |
| 114 | + */ |
| 115 | + void endBody(boolean completes) { |
| 116 | + reachable = completes || atContinues != null; |
| 117 | + } |
| 118 | + |
| 119 | + /** The next iteration starts here, if this point is reachable: drops the candidates not proved here */ |
| 120 | + void recordNextIteration() { |
| 121 | + if (reachable) |
| 122 | + dropped |= candidates.removeIf(candidate -> !proved(candidate)); |
| 123 | + } |
| 124 | + |
| 125 | + /** |
| 126 | + * Whether the iteration just checked dropped no candidate, so it is the last one to check. Once {@link #MAX_ROUNDS} |
| 127 | + * checks dropped some, all are dropped. |
| 128 | + */ |
| 129 | + boolean reachedFixpoint() { |
| 130 | + if (dropped && ++rounds >= MAX_ROUNDS) |
| 131 | + candidates.clear(); |
| 132 | + return !dropped; |
| 133 | + } |
| 134 | + |
| 135 | + /** The candidates, and that each variable grew or shrank since the loop head, that are proved here */ |
| 136 | + private List<Predicate> provedForNextIteration() { |
| 137 | + List<Predicate> facts = new ArrayList<>(candidates); |
| 138 | + head.forEach((variable, instance) -> { |
| 139 | + Predicate name = Predicate.createVar(variable), atHead = Predicate.createVar(instance.getName()); |
| 140 | + facts.add(Predicate.createOperation(name, Ops.GE, atHead)); |
| 141 | + facts.add(Predicate.createOperation(name, Ops.LE, atHead)); |
| 142 | + }); |
| 143 | + facts.removeIf(fact -> !proved(fact)); |
| 144 | + return facts; |
| 145 | + } |
| 146 | + |
| 147 | + /** |
| 148 | + * Adds to the last instance of each variable the facts that mention it, on the last instances of the variables. |
| 149 | + * Unlike a path condition, the refinement of an instance is not dropped when the variable is assigned. |
| 150 | + */ |
| 151 | + private void addToLastInstances(List<Predicate> facts) { |
| 152 | + for (String variable : variables) { |
| 153 | + Optional<VariableInstance> last = checker.getContext().getLastVariableInstance(variable); |
| 154 | + if (last.isEmpty()) |
| 155 | + continue; |
| 156 | + Predicate refinement = declared.computeIfAbsent(last.get(), RefinedVariable::getRefinement); |
| 157 | + for (Predicate fact : facts) |
| 158 | + if (fact.getVariableNames().contains(variable)) |
| 159 | + refinement = Predicate.createConjunction(refinement, onLastInstances(fact)); |
| 160 | + last.get().setRefinement(refinement); |
| 161 | + } |
| 162 | + } |
| 163 | + |
| 164 | + /** Whether a fact on variable names holds for their last instances, from what is known here */ |
| 165 | + private boolean proved(Predicate fact) { |
| 166 | + try { |
| 167 | + return checker.checkStateSMT(new Predicate(), onLastInstances(fact), loop.getPosition(), true); |
| 168 | + } catch (LJError e) { |
| 169 | + return false; |
| 170 | + } |
| 171 | + } |
| 172 | + |
| 173 | + private Predicate onLastInstances(Predicate fact) { |
| 174 | + Predicate result = fact; |
| 175 | + for (String name : fact.getVariableNames()) { |
| 176 | + Optional<VariableInstance> last = checker.getContext().getLastVariableInstance(name); |
| 177 | + if (last.isPresent()) |
| 178 | + result = result.substituteVariable(name, last.get().getName()); |
| 179 | + } |
| 180 | + return result; |
| 181 | + } |
| 182 | + |
| 183 | + private static CtExpression<Boolean> condition(CtLoop loop) { |
| 184 | + if (loop instanceof CtWhile whileLoop) |
| 185 | + return whileLoop.getLoopingExpression(); |
| 186 | + if (loop instanceof CtDo doLoop) |
| 187 | + return doLoop.getLoopingExpression(); |
| 188 | + if (loop instanceof CtFor forLoop) |
| 189 | + return forLoop.getExpression(); |
| 190 | + return null; |
| 191 | + } |
| 192 | +} |
0 commit comments