|
| 1 | +package liquidjava.rj_language.opt; |
| 2 | + |
| 3 | +import static liquidjava.rj_language.opt.VCSimplificationUtils.copyWithRefinement; |
| 4 | +import static liquidjava.rj_language.opt.VCSimplificationUtils.factsOf; |
| 5 | +import static liquidjava.rj_language.opt.VCSimplificationUtils.variablesOf; |
| 6 | + |
| 7 | +import java.util.ArrayList; |
| 8 | +import java.util.HashMap; |
| 9 | +import java.util.HashSet; |
| 10 | +import java.util.Iterator; |
| 11 | +import java.util.List; |
| 12 | +import java.util.Map; |
| 13 | +import java.util.Set; |
| 14 | +import java.util.function.Function; |
| 15 | + |
| 16 | +import liquidjava.processor.VCImplication; |
| 17 | +import liquidjava.rj_language.Predicate; |
| 18 | +import liquidjava.rj_language.ast.BinaryExpression; |
| 19 | +import liquidjava.rj_language.ast.Expression; |
| 20 | +import liquidjava.rj_language.ast.FunctionInvocation; |
| 21 | +import liquidjava.rj_language.ast.Var; |
| 22 | + |
| 23 | +/** |
| 24 | + * Keeps in a state error's Found only the facts the expected state depends on, for display: the solver's VC is not |
| 25 | + * changed. Each fact is split into the symbols it constrains: a ghost function applied to an object, such as the state |
| 26 | + * set {@code state1(x)} that both {@code open(x)} and {@code state1(x) == state1(y)} constrain, or a variable whose own |
| 27 | + * value it constrains. Different ghost functions, or the same one over different objects, are different solver terms, |
| 28 | + * so facts that share no symbol with expected, directly or through other facts, are independent of it and are left out. |
| 29 | + * An object whose value a fact constrains, such as {@code x == y}, or that an alias or unknown function takes, is one |
| 30 | + * symbol with all its ghost functions, so its facts are never split. Nothing changes when every fact or none is |
| 31 | + * relevant |
| 32 | + */ |
| 33 | +public class VCRelevantFacts implements VCSimplificationPass { |
| 34 | + |
| 35 | + private final Expression expected; |
| 36 | + private final Function<String, Set<String>> ghostFunctionsOf; |
| 37 | + |
| 38 | + /** |
| 39 | + * @param ghostFunctionsOf |
| 40 | + * the ghost functions an invocation name stands for: a state's state set function, or the ghost itself; |
| 41 | + * none for any other name |
| 42 | + */ |
| 43 | + public VCRelevantFacts(Expression expected, Function<String, Set<String>> ghostFunctionsOf) { |
| 44 | + this.expected = expected; |
| 45 | + this.ghostFunctionsOf = ghostFunctionsOf; |
| 46 | + } |
| 47 | + |
| 48 | + @Override |
| 49 | + public String getName() { |
| 50 | + return "Relevant to Expected"; |
| 51 | + } |
| 52 | + |
| 53 | + @Override |
| 54 | + public VCImplication apply(VCImplication implication) { |
| 55 | + List<List<Expression>> rowFacts = new ArrayList<>(); |
| 56 | + for (VCImplication row = implication; row != null; row = row.getNext()) |
| 57 | + rowFacts.add(factsOf(row.getRefinement().getExpression())); |
| 58 | + List<Expression> allFacts = rowFacts.stream().flatMap(List::stream).toList(); |
| 59 | + Set<String> values = new HashSet<>(); |
| 60 | + addSymbols(expected, new HashMap<>(), values); |
| 61 | + allFacts.forEach(fact -> addSymbols(fact, new HashMap<>(), values)); |
| 62 | + |
| 63 | + Set<String> linked = symbolsOf(expected, values); |
| 64 | + List<Set<String>> unlinked = new ArrayList<>(allFacts.stream().map(fact -> symbolsOf(fact, values)).toList()); |
| 65 | + boolean changed = true; |
| 66 | + while (changed) { |
| 67 | + changed = false; |
| 68 | + for (Iterator<Set<String>> it = unlinked.iterator(); it.hasNext();) { |
| 69 | + Set<String> symbols = it.next(); |
| 70 | + if (symbols.stream().anyMatch(linked::contains)) { |
| 71 | + linked.addAll(symbols); |
| 72 | + it.remove(); |
| 73 | + changed = true; |
| 74 | + } |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + List<List<Expression>> kept = rowFacts.stream().map(facts -> facts.stream() |
| 79 | + .filter(fact -> symbolsOf(fact, values).stream().anyMatch(linked::contains)).toList()).toList(); |
| 80 | + int relevant = kept.stream().mapToInt(List::size).sum(); |
| 81 | + if (relevant == 0 || relevant == allFacts.size()) |
| 82 | + return implication; |
| 83 | + return rebuild(implication, rowFacts.iterator(), kept.iterator()); |
| 84 | + } |
| 85 | + |
| 86 | + // The rows with their relevant facts, leaving out those that keep none |
| 87 | + private VCImplication rebuild(VCImplication row, Iterator<List<Expression>> all, Iterator<List<Expression>> kept) { |
| 88 | + if (row == null) |
| 89 | + return null; |
| 90 | + List<Expression> rowAll = all.next(), rowKept = kept.next(); |
| 91 | + VCImplication next = rebuild(row.getNext(), all, kept); |
| 92 | + if (rowKept.isEmpty()) |
| 93 | + return next; |
| 94 | + Expression refinement = rowKept.size() == rowAll.size() ? row.getRefinement().getExpression().clone() |
| 95 | + : rowKept.stream().map(Expression::clone) |
| 96 | + .reduce((first, second) -> new BinaryExpression(first, "&&", second)).get(); |
| 97 | + VCImplication result = copyWithRefinement(row, new Predicate(refinement)); |
| 98 | + result.setNext(next); |
| 99 | + return result; |
| 100 | + } |
| 101 | + |
| 102 | + // The symbols an expression constrains: f(x) for a ghost function over an object, or the object itself when values |
| 103 | + // has it, and the variables whose value it constrains |
| 104 | + private Set<String> symbolsOf(Expression expression, Set<String> values) { |
| 105 | + Map<String, String> applications = new HashMap<>(); |
| 106 | + Set<String> symbols = new HashSet<>(); |
| 107 | + addSymbols(expression, applications, symbols); |
| 108 | + applications.forEach((application, object) -> symbols.add(values.contains(object) ? object : application)); |
| 109 | + return symbols; |
| 110 | + } |
| 111 | + |
| 112 | + // Adds each ghost function applied to a variable, as f(x) mapped to x, to applications, and the variables whose |
| 113 | + // value the expression constrains, or that an unknown function takes, to values |
| 114 | + private void addSymbols(Expression expression, Map<String, String> applications, Set<String> values) { |
| 115 | + if (expression instanceof FunctionInvocation invocation) { |
| 116 | + Set<String> functions = ghostFunctionsOf.apply(invocation.getName()); |
| 117 | + if (!functions.isEmpty() && invocation.getArgs().size() == 1 |
| 118 | + && invocation.getArgs().get(0)instanceof Var object) |
| 119 | + functions.forEach( |
| 120 | + function -> applications.put(function + "(" + object.getName() + ")", object.getName())); |
| 121 | + else |
| 122 | + values.addAll(variablesOf(invocation)); |
| 123 | + return; |
| 124 | + } |
| 125 | + if (expression instanceof Var var) |
| 126 | + values.add(var.getName()); |
| 127 | + for (Expression child : expression.getChildren()) |
| 128 | + addSymbols(child, applications, values); |
| 129 | + } |
| 130 | +} |
0 commit comments