Skip to content

Commit 926f027

Browse files
Drop true rows for variables that another row of Found already shows (#472)
## Problem After #459, Found still shows `true` rows for variables that another row already shows (#375): - **A1_56** (JabRef `EditAction`, state error): `∀getUndoManager true`. - **A1_09** (`PlayfieldWindow`, state error): `∀previousMode true`, `∀DisplayMode.BORDERLESS true` and `∀newMode true`, above the ternary that uses them. #459 kept a `true` binder when a fact linked to Expected constrains its value. That fact is always a row the user already sees, and it already names the variable. So the extra `true` row adds no fact. It also reads as if the variable's value were `true`, which is what #375 is about. The A1_09 rows are not needed: the ternary row shows `previousMode`, `newMode` and `BORDERLESS`, and that row explains why `host` may still be displayable. `DisplayMode.BORDERLESS` is an enum constant, so a binder row for it says nothing. ## Rule A binder refined only by `true` is shown only when **all** of these hold: 1. the error is a refinement error, so it has a counterexample; 2. Expected constrains the variable's value; 3. **no other row of Found mentions the variable.** When all three hold, the row is the only place the variable appears in Found. It tells the user that nothing is known about a value the error depends on (`∀t true`, counterexample `t == 0`). In every other case the row goes, because the variable is either shown in the rows that use it or not needed. No variable disappears from what the user sees: - A dropped binder's variable still appears in the rows that mention it. - The counterexample is unchanged. `RefinementError.filterCounterexample` takes its terms from Expected and from Found's facts, not from the binders, so for example `Basket.served == -1` still shows after its row goes. `NeededVariables` no longer links facts to Expected. Two pieces of code had no other use, so this PR removes them: - `splitConjuncts` and `linkedConjuncts` in `VCSimplificationUtils`; - the `VCBinderOrdering` pass and its test. That pass only moved kept `true` binders before their first use. A kept binder now has no other use, so the pass could never fire. ## Before / after **A1_56** (`undoManager.redo()` on a manager from a getter) ``` before: ∀getUndoManager¹²⁹ true ∀#fresh¹¹² !#fresh¹¹¹ ∀#fresh¹²⁶ !(#fresh¹²⁴ || #fresh¹²⁵) ∀undoManager¹³¹ undoManager¹³¹ == getUndoManager¹²⁹ after: ∀#fresh¹¹² !#fresh¹¹¹ ∀#fresh¹²⁶ !(#fresh¹²⁴ || #fresh¹²⁵) ∀undoManager¹³¹ undoManager¹³¹ == getUndoManager¹²⁹ ``` **A1_09** (`host.setUndecorated(true)` after a conditional `dispose()`) ``` before: ∀previousMode true ∀DisplayMode.BORDERLESS true ∀newMode true ∀host¹²¹ previousMode == BORDERLESS && newMode != BORDERLESS ? notDisplayable(host¹²¹) : (previousMode != BORDERLESS && newMode == BORDERLESS ? notDisplayable(host¹²¹) : displayable(host¹²¹)) after: ∀host¹²¹ previousMode == BORDERLESS && newMode != BORDERLESS ? notDisplayable(host¹²¹) : (previousMode != BORDERLESS && newMode == BORDERLESS ? notDisplayable(host¹²¹) : displayable(host¹²¹)) ``` **Test suite fixture** (`classes/found_binders_error`) | Case | before | after | |---|---|---| | `int sum = first + second` refined `_ > 0` (new) | `∀first true`, `∀second true`, `∀sum sum == first + second` | `∀sum sum == first + second`, counterexample `sum == 0 && first == 0 && second == 0` (unchanged) | | `int count = served + 1` (static field) | `∀Basket.served true`, `∀count count == Basket.served + 1` | `∀count count == Basket.served + 1`, counterexample `count == 0 && Basket.served == -1` (unchanged) | | `positive(t)` with `t` unconstrained (new, kept) | `∀t true`, counterexample `t == 0` | same | In the same way, `ErrorStateHintPathDependent` now shows only `ready ? open(f) : created(f)`, without `∀ready true`. What the fixed `#fresh` path rows still show (`∀#fresh¹¹² !#fresh¹¹¹`) is the separate part of #375 that ties in with #373. They are not `true` rows, and this PR leaves them alone. ## Tests - `TestFoundBinders`: - `variablesAnotherRowShowsGetNoTrueRow` (positive): new `addUp` fixture, the A1_09 pattern as a refinement error. - `variableOnlyExpectedNamesKeepsItsTrueRow` (negative): new `forward` fixture. The informative `∀t true` row and its counterexample stay. - `staticFieldIsWrittenWithItsClass` updated: the `Basket.served` row goes, and its counterexample value stays. - `VCBinderSimplificationTest`: - Tests that encoded the old linking rule now expect the row to go: `removesTrueBinderThatALaterRowShows`, `removesTrueBinderThatIsTheArgumentOfAGhost`, and `removesTrueBindersThatAConditionalStateShows` (the A1_09 shape). - New `removesTrueBinderOfExpectedVariableThatAnotherRowShows`. - `keepsTrueBinderOfExpectedVariableNoOtherRowMentions` (renamed) still keeps the row. - `skipsInapplicableTrueBinderAndSimplifiesLaterBinder` now uses a row that does not mention `x`, so `x` is still the inapplicable binder. - Without the fix (`main` sources, new tests kept), 6 of the 22 tests in these two classes fail, including both positive tests. The negative test passes on both, as it should. - `mvn test`: **667 tests, 0 failures.** Part of #375. The `#fresh` path rows are left for #373. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
1 parent dc1a63f commit 926f027

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)