Skip to content

Commit c99ef24

Browse files
Drop true binders that another row already shows
A binder refined only by true states no fact. When another row of Found mentions its variable, the variable is already shown there and the true row only reads as if its value were true. Keep such a row only in a refinement error, for a variable whose value expected constrains and that no other row mentions: it then shows that nothing is known about a value the error depends on. This removes the linking to expected that kept getUndoManager, the parameters of a conditional state and enum constants, and the binder ordering pass that only moved those kept rows. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent dc1a63f commit c99ef24

8 files changed

Lines changed: 55 additions & 195 deletions

File tree

‎liquidjava-example/src/main/java/testSuite/classes/found_binders_error/Basket.java‎

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
import liquidjava.specification.Refinement;
77

8-
// Found drops the true rows the error does not need and writes fields as in code (#457)
8+
// Found drops the true rows the error does not need and writes fields as in code (#457, #375)
99
public class Basket {
1010
static int served;
1111

@@ -55,4 +55,17 @@ void writeThroughAlias() {
5555
@Refinement("_ == 7")
5656
int k = level; // Expect: Refinement Error
5757
}
58+
59+
static void positive(@Refinement("_ > 0") int t) {}
60+
61+
// parameters that another row already shows get no true row
62+
void addUp(int first, int second) {
63+
@Refinement("_ > 0")
64+
int sum = first + second; // Expect: Refinement Error
65+
}
66+
67+
// a variable only expected names keeps its true row: nothing is known about it
68+
void forward(int t) {
69+
positive(t); // Expect: Refinement Error
70+
}
5871
}

‎liquidjava-verifier/src/main/java/liquidjava/rj_language/opt/NeededVariables.java‎

Lines changed: 14 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,10 @@
11
package liquidjava.rj_language.opt;
22

3-
import static liquidjava.rj_language.opt.VCSimplificationUtils.isTrue;
4-
import static liquidjava.rj_language.opt.VCSimplificationUtils.linkedConjuncts;
5-
import static liquidjava.rj_language.opt.VCSimplificationUtils.splitConjuncts;
3+
import static liquidjava.rj_language.opt.VCSimplificationUtils.containsVar;
64
import static liquidjava.rj_language.opt.VCSimplificationUtils.termsOf;
75
import static liquidjava.rj_language.opt.VCSimplificationUtils.valueVariablesOf;
8-
import static liquidjava.rj_language.opt.VCSimplificationUtils.variablesOf;
96

10-
import java.util.ArrayList;
117
import java.util.HashSet;
12-
import java.util.List;
138
import java.util.Set;
149

1510
import liquidjava.processor.VCImplication;
@@ -18,16 +13,16 @@
1813

1914
/**
2015
* Decides which binders refined only by {@code true} a simplified VC chain keeps for an error. Such a binder states no
21-
* fact, so it is kept only when a fact linked to the expected refinement (sharing a term with it, directly or through
22-
* other facts) constrains its variable's value, or, when the error shows a counterexample, when expected itself
23-
* constrains its value, so that the counterexample can show it
16+
* fact, and a variable that another row mentions is already shown there, so its row would only read as if its value
17+
* were true. It is kept only when the error shows a counterexample, expected constrains the variable's value and no
18+
* other row mentions it: the row then shows that nothing is known about a value the error depends on
2419
*
2520
* @param expectedTerms
2621
* variables and ghost applications of the expected refinement
2722
* @param expectedValues
2823
* variables whose value the expected refinement constrains
2924
* @param keepExpected
30-
* whether the binders of expectedValues are kept for that alone
25+
* whether the binders of expectedValues that no other row mentions are kept
3126
*/
3227
public record NeededVariables(Set<String> expectedTerms, Set<String> expectedValues, boolean keepExpected) {
3328

@@ -39,7 +34,7 @@ public record NeededVariables(Set<String> expectedTerms, Set<String> expectedVal
3934
}
4035

4136
/**
42-
* For a refinement error, whose counterexample lists the values of the binders
37+
* For a refinement error, which shows a counterexample
4338
*/
4439
public static NeededVariables forRefinementError(Predicate expected) {
4540
return of(expected, true);
@@ -59,40 +54,20 @@ private static NeededVariables of(Predicate expected, boolean keepExpected) {
5954
}
6055

6156
/**
62-
* Variables whose binder refined only by true the chain keeps: those whose own value a fact linked to expected
63-
* constrains, and, when {@code keepExpected}, those whose value expected constrains
57+
* Variables whose binder refined only by true the chain keeps: when {@code keepExpected}, those whose value
58+
* expected constrains and that no other row of the chain mentions
6459
*/
6560
public Set<String> trueBindersToKeep(VCImplication chain) {
66-
Set<String> primitives = primitiveBinders(chain);
6761
Set<String> keep = new HashSet<>();
68-
for (Expression fact : linkedConjuncts(factsOf(chain), VCSimplificationUtils::termsOf, expectedTerms))
69-
for (String name : variablesOf(fact))
70-
if (!expectedTerms.contains(name) && hasOwnValue(name, fact, primitives))
71-
keep.add(name);
72-
if (keepExpected)
73-
for (String name : expectedTerms)
74-
if (expectedValues.contains(name) || primitives.contains(name))
75-
keep.add(name);
62+
if (!keepExpected)
63+
return keep;
64+
Set<String> primitives = primitiveBinders(chain);
65+
for (String name : expectedTerms)
66+
if ((expectedValues.contains(name) || primitives.contains(name)) && !containsVar(chain, name))
67+
keep.add(name);
7668
return keep;
7769
}
7870

79-
/**
80-
* Whether a fact constrains the variable's own value. A variable of an object type that the fact names only as the
81-
* argument of a ghost or state application ({@code x} in {@code b == size(x)}) has no value of its own there, so
82-
* its row would say nothing; a primitive argument ({@code k} in {@code y == f(k)}) has one
83-
*/
84-
private static boolean hasOwnValue(String name, Expression fact, Set<String> primitives) {
85-
return primitives.contains(name) || valueVariablesOf(fact).contains(name);
86-
}
87-
88-
private static List<Expression> factsOf(VCImplication chain) {
89-
List<Expression> facts = new ArrayList<>();
90-
for (VCImplication current = chain; current != null; current = current.getNext())
91-
if (!isTrue(current.getRefinement().getExpression()))
92-
facts.addAll(splitConjuncts(current.getRefinement().getExpression()));
93-
return facts;
94-
}
95-
9671
private static Set<String> primitiveBinders(VCImplication chain) {
9772
Set<String> primitives = new HashSet<>();
9873
for (VCImplication current = chain; current != null; current = current.getNext())

‎liquidjava-verifier/src/main/java/liquidjava/rj_language/opt/VCBinderOrdering.java‎

Lines changed: 0 additions & 54 deletions
This file was deleted.

‎liquidjava-verifier/src/main/java/liquidjava/rj_language/opt/VCSimplification.java‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ private static VCSimplificationResult simplifyOnce(VCSimplificationResult implic
5858
private static List<VCSimplificationPass> passes(NeededVariables needed) {
5959
return List.of(new VCSubstitution(), new VCFunctionSubstitution(), new VCBinderSimplification(needed),
6060
new VCFolding(), new VCArithmeticSimplification(), new VCLogicalSimplification(),
61-
new VCConstraintSimplification(), new VCBinderOrdering());
61+
new VCConstraintSimplification());
6262
}
6363

6464
/**

‎liquidjava-verifier/src/main/java/liquidjava/rj_language/opt/VCSimplificationUtils.java‎

Lines changed: 0 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,13 @@
11
package liquidjava.rj_language.opt;
22

33
import java.util.ArrayList;
4-
import java.util.Collection;
5-
import java.util.HashSet;
64
import java.util.List;
7-
import java.util.Set;
8-
import java.util.function.Function;
95

106
import liquidjava.processor.VCImplication;
117
import liquidjava.rj_language.Predicate;
12-
import liquidjava.rj_language.ast.BinaryExpression;
138
import liquidjava.rj_language.ast.Expression;
149
import liquidjava.rj_language.ast.FunctionInvocation;
1510
import liquidjava.rj_language.ast.LiteralBoolean;
16-
import liquidjava.rj_language.ast.UnaryExpression;
1711
import liquidjava.rj_language.ast.Var;
1812

1913
public final class VCSimplificationUtils {
@@ -61,61 +55,6 @@ public static boolean isFalse(Expression expression) {
6155
return expression instanceof LiteralBoolean literal && !literal.isBooleanTrue();
6256
}
6357

64-
/**
65-
* Splits an expression into the facts it states together: the operands of {@code &&}, and of a negated disjunction
66-
* {@code !(a || b)}, which states {@code !a} and {@code !b}
67-
*/
68-
public static List<Expression> splitConjuncts(Expression expression) {
69-
List<Expression> conjuncts = new ArrayList<>();
70-
addConjuncts(expression, conjuncts);
71-
return conjuncts;
72-
}
73-
74-
private static void addConjuncts(Expression expression, List<Expression> conjuncts) {
75-
Expression negated = negatedOperand(expression);
76-
if (expression instanceof BinaryExpression binary && "&&".equals(binary.getOperator())) {
77-
addConjuncts(binary.getFirstOperand(), conjuncts);
78-
addConjuncts(binary.getSecondOperand(), conjuncts);
79-
} else if (negated instanceof BinaryExpression binary && "||".equals(binary.getOperator())) {
80-
addConjuncts(new UnaryExpression("!", binary.getFirstOperand()), conjuncts);
81-
addConjuncts(new UnaryExpression("!", binary.getSecondOperand()), conjuncts);
82-
} else if (negatedOperand(negated) != null) {
83-
addConjuncts(negatedOperand(negated), conjuncts);
84-
} else {
85-
conjuncts.add(expression);
86-
}
87-
}
88-
89-
// the operand of !e, or null for any other expression
90-
private static Expression negatedOperand(Expression expression) {
91-
return expression instanceof UnaryExpression not && "!".equals(not.getOp()) ? not.getExpression() : null;
92-
}
93-
94-
/**
95-
* Returns the conjuncts linked to the seed terms: those sharing a term with the seed, directly or through other
96-
* linked conjuncts. The remaining conjuncts only constrain values that the seed does not depend on
97-
*/
98-
public static List<Expression> linkedConjuncts(List<Expression> conjuncts,
99-
Function<Expression, ? extends Collection<String>> termsOf, Collection<String> seed) {
100-
Set<String> linkedTerms = new HashSet<>(seed);
101-
List<Expression> linked = new ArrayList<>();
102-
List<Expression> unlinked = new ArrayList<>(conjuncts);
103-
boolean changed = true;
104-
while (changed) {
105-
changed = false;
106-
for (Expression conjunct : List.copyOf(unlinked)) {
107-
Collection<String> terms = termsOf.apply(conjunct);
108-
if (terms.stream().anyMatch(linkedTerms::contains)) {
109-
linkedTerms.addAll(terms);
110-
linked.add(conjunct);
111-
unlinked.remove(conjunct);
112-
changed = true;
113-
}
114-
}
115-
}
116-
return linked;
117-
}
118-
11958
/**
12059
* Variable names of an expression
12160
*/

‎liquidjava-verifier/src/test/java/liquidjava/api/tests/TestFoundBinders.java‎

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,10 +75,24 @@ void steppingBackReachesTheOriginalWithTheDroppedRow() {
7575
@Test
7676
void staticFieldIsWrittenWithItsClass() {
7777
Shown shown = ERRORS.get(31);
78-
assertEquals(List.of("∀Basket.served true", "∀count count == Basket.served + 1"), shown.rows());
78+
assertEquals(List.of("∀count count == Basket.served + 1"), shown.rows());
7979
assertEquals("Counterexample: count == 0 && Basket.served == -1", shown.counterexample());
8080
}
8181

82+
@Test
83+
void variablesAnotherRowShowsGetNoTrueRow() {
84+
Shown shown = ERRORS.get(64);
85+
assertEquals(List.of("∀sum sum == first + second"), shown.rows());
86+
assertEquals("Counterexample: sum == 0 && first == 0 && second == 0", shown.counterexample());
87+
}
88+
89+
@Test
90+
void variableOnlyExpectedNamesKeepsItsTrueRow() {
91+
Shown shown = ERRORS.get(69);
92+
assertEquals(List.of("∀t true"), shown.rows());
93+
assertEquals("Counterexample: t == 0", shown.counterexample());
94+
}
95+
8296
@Test
8397
void writeToAnotherObjectsFieldIsNamedAfterThatObject() {
8498
assertEquals("other.level == -1 is not a subtype of other.level >= 0", ERRORS.get(36).message());

‎liquidjava-verifier/src/test/java/liquidjava/rj_language/opt/VCBinderOrderingTest.java‎

Lines changed: 0 additions & 18 deletions
This file was deleted.

‎liquidjava-verifier/src/test/java/liquidjava/rj_language/opt/VCBinderSimplificationTest.java‎

Lines changed: 11 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -49,27 +49,18 @@ void removesTrueBinderUsedDownstreamWhenNothingNeedsIt() {
4949
}
5050

5151
@Test
52-
void keepsTrueBinderLinkedToExpectedThroughALaterRow() {
52+
void removesTrueBinderThatALaterRowShows() {
5353
assertSimplificationSteps(forRefinementError("y > 0"), vc("∀x:int. true", "∀y:int. y == x + 1"),
54-
step("true", "y == x + 1"));
54+
step("y == x + 1"));
5555
}
5656

5757
@Test
58-
void keepsTrueBinderLinkedToExpectedThroughSeveralRows() {
59-
assertSimplificationSteps(forRefinementError("z > 0"),
60-
vc("∀x:int. true", "∀y:int. y == x + 1", "∀z:int. z == y"), step("true", "y == x + 1", "z == y"));
58+
void removesTrueBinderOfExpectedVariableThatAnotherRowShows() {
59+
assertSimplificationSteps(forRefinementError("x > 0"), vc("∀x:int. true", "x != 0"), step("x != 0"));
6160
}
6261

6362
@Test
64-
void removesTrueBindersOnlyAnUnlinkedGuardUses() {
65-
// one guard !(p || t < 0) states !p and !(t < 0) apart, so only the second is linked to t
66-
assertSimplificationSteps(forRefinementError("t > 0"),
67-
vc("∀port:int. true", "∀p:boolean. true", "!(port < 0)", "!(p || t < 0)", "∀t:int. true"),
68-
step("true", "!(port < 0)", "!(p || t < 0)", "true"), step("!(port < 0)", "!(p || t < 0)", "true"));
69-
}
70-
71-
@Test
72-
void keepsTrueBinderOfExpectedVariableInRefinementError() {
63+
void keepsTrueBinderOfExpectedVariableNoOtherRowMentions() {
7364
assertSimplificationSteps(forRefinementError("x > 0"), vc("∀x:int. true", "y > 0"), step("true", "y > 0"));
7465
}
7566

@@ -79,16 +70,16 @@ void removesTrueBinderOfExpectedVariableInStateError() {
7970
}
8071

8172
@Test
82-
void keepsPrimitiveTrueBinderThatIsTheArgumentOfAGhost() {
73+
void removesTrueBinderThatIsTheArgumentOfAGhost() {
8374
assertSimplificationSteps(forRefinementError("y > 0"), vc("∀k:int. true", "∀y:int. y == f(k)"),
84-
step("true", "y == f(k)"));
75+
step("y == f(k)"));
8576
}
8677

8778
@Test
88-
void linksThroughGhostApplicationsButKeepsOnlyValueVariables() {
79+
void removesTrueBindersThatAConditionalStateShows() {
8980
assertSimplificationSteps(forStateError("open(f)"),
9081
vc("∀ready:boolean. true", "∀f:Object. true", "ready ? open(f) : created(f)"),
91-
step("true", "ready ? open(f) : created(f)"));
82+
step("true", "ready ? open(f) : created(f)"), step("ready ? open(f) : created(f)"));
9283
}
9384

9485
@Test
@@ -104,8 +95,8 @@ void simplifiesOnlyFirstApplicableBinder() {
10495

10596
@Test
10697
void skipsInapplicableTrueBinderAndSimplifiesLaterBinder() {
107-
assertSimplificationSteps(forRefinementError("x > 0"), vc("∀x:int. true", "x > 0", "∀y:int. true", "z > 0"),
108-
step("true", "x > 0", "z > 0"));
98+
assertSimplificationSteps(forRefinementError("x > 0"), vc("∀x:int. true", "w > 0", "∀y:int. true", "z > 0"),
99+
step("true", "w > 0", "z > 0"));
109100
}
110101

111102
@Test

0 commit comments

Comments
 (0)