Skip to content

Commit 712759f

Browse files
Show no counterexample when found fixes every value expected depends on
The fallback to the other terms found shows could point at an unrelated path condition (port == 1 for take(value) with value == 0 already in found). Only terms linked to expected are now kept, so when found states all of them the counterexample is empty, as for a literal argument. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 9e037b6 commit 712759f

7 files changed

Lines changed: 99 additions & 15 deletions

File tree

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package testSuite;
2+
3+
import liquidjava.specification.Refinement;
4+
5+
public class ErrorCounterexampleKnownAliasedArgument {
6+
7+
static void take(@Refinement("_ > 1") int value) {}
8+
9+
static void probe(int port) {
10+
int base = 0;
11+
int value = base;
12+
if (port > 0)
13+
take(value); // Expect: Refinement Error
14+
}
15+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package testSuite;
2+
3+
import liquidjava.specification.Refinement;
4+
5+
public class ErrorCounterexampleKnownGuardedArgument {
6+
7+
static void take(@Refinement("_ > 1") int value) {}
8+
9+
static void probe(int port) {
10+
int value = 0;
11+
if (port > 0)
12+
take(value); // Expect: Refinement Error
13+
}
14+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package testSuite;
2+
3+
import liquidjava.specification.Refinement;
4+
5+
public class ErrorCounterexampleKnownTwiceGuardedArgument {
6+
7+
static void take(@Refinement("_ > 1") int value) {}
8+
9+
static void probe(int port, int retries) {
10+
int value = 0;
11+
if (port > 0)
12+
if (retries < 3)
13+
take(value); // Expect: Refinement Error
14+
}
15+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package testSuite;
2+
3+
import liquidjava.specification.Refinement;
4+
5+
public class ErrorCounterexampleReturnFromPath {
6+
7+
@Refinement("_ > 10")
8+
static int ten(int port) {
9+
if (port < 0)
10+
return port + 1; // Expect: Refinement Error
11+
return 11;
12+
}
13+
}

‎liquidjava-verifier/src/main/java/liquidjava/diagnostics/errors/RefinementError.java‎

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -89,9 +89,9 @@ public VCSimplificationResult getFound() {
8989
}
9090

9191
// Keeps the counterexample assignments to the terms of expected and of the found facts linked to them, except
92-
// values found already states, or to every term found shows when that leaves nothing. The terms of expected come
93-
// first, those found says nothing about before the rest, then the linked terms in the order of the VC binders, with
94-
// generated names last
92+
// values found already states, so there is none when found fixes every value expected depends on. The terms of
93+
// expected come first, those found says nothing about before the rest, then the linked terms in the order of the
94+
// VC binders, with generated names last
9595
private Counterexample filterCounterexample(Counterexample counterexample) {
9696
if (counterexample == null)
9797
return new Counterexample(List.of());
@@ -102,12 +102,8 @@ private Counterexample filterCounterexample(Counterexample counterexample) {
102102
List<String> expectedTerms = termsOf(getExpected().getExpression());
103103
Set<String> linkedTerms = expectedTerms.isEmpty() ? new HashSet<>(foundTerms)
104104
: linkedTerms(facts, expectedTerms);
105-
List<Pair<String, String>> unknown = counterexample.assignments().stream().filter(a -> !isKnown(a, facts))
106-
.toList();
107-
List<Pair<String, String>> assignments = unknown.stream().filter(a -> linkedTerms.contains(a.first())).toList();
108-
// e.g. a return value found states, where the path condition that led to it is what is left to show
109-
if (assignments.isEmpty())
110-
assignments = unknown.stream().filter(a -> foundTerms.contains(a.first())).toList();
105+
List<Pair<String, String>> assignments = counterexample.assignments().stream()
106+
.filter(a -> linkedTerms.contains(a.first())).filter(a -> !isKnown(a, facts)).toList();
111107

112108
List<String> order = new ArrayList<>(originalBinders());
113109
order.addAll(foundTerms);

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

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -163,9 +163,9 @@ void ghostOfArgumentFoundKnowsNothingAboutIsKept() {
163163
}
164164

165165
@Test
166-
void ghostValueFoundAlreadyStatesIsOmittedSoThePathConditionIsShown() {
166+
void ghostValueFoundAlreadyStatesIsOmittedWithTheUnrelatedPathCondition() {
167167
RefinementError error = verify("ErrorCounterexampleKnownGhost.java");
168-
assertAssignments(error, assignment("port", "0"));
168+
assertTrue(error.getCounterexample().isEmpty());
169169
}
170170

171171
@Test
@@ -187,9 +187,33 @@ void guardRelatingTheArgumentToAnotherVariableKeepsItAfterTheArgument() {
187187
}
188188

189189
@Test
190-
void returnedConstantShowsThePathConditionThatLedToIt() {
190+
void returnedConstantFoundAlreadyStatesHasNoCounterexample() {
191191
RefinementError error = verify("ErrorCounterexampleReturnConstant.java");
192-
assertAssignments(error, assignment("port", "-1"));
192+
assertTrue(error.getCounterexample().isEmpty());
193+
}
194+
195+
@Test
196+
void returnComputedFromThePathConditionShowsBoth() {
197+
RefinementError error = verify("ErrorCounterexampleReturnFromPath.java");
198+
assertAssignments(error, assignment("#ret", "0"), assignment("port", "-1"));
199+
}
200+
201+
@Test
202+
void argumentFoundAlreadyStatesHasNoCounterexampleUnderAGuard() {
203+
RefinementError error = verify("ErrorCounterexampleKnownGuardedArgument.java");
204+
assertTrue(error.getCounterexample().isEmpty());
205+
}
206+
207+
@Test
208+
void argumentFoundAlreadyStatesHasNoCounterexampleUnderTwoGuards() {
209+
RefinementError error = verify("ErrorCounterexampleKnownTwiceGuardedArgument.java");
210+
assertTrue(error.getCounterexample().isEmpty());
211+
}
212+
213+
@Test
214+
void argumentKnownThroughAnAliasHasNoCounterexampleUnderAGuard() {
215+
RefinementError error = verify("ErrorCounterexampleKnownAliasedArgument.java");
216+
assertTrue(error.getCounterexample().isEmpty());
193217
}
194218

195219
@Test

‎liquidjava-verifier/src/test/java/liquidjava/diagnostics/errors/RefinementErrorCounterexampleTest.java‎

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,9 +67,16 @@ void keepsGhostApplicationsOfExpectedAndFormatsTheirArguments() {
6767
}
6868

6969
@Test
70-
void fallsBackToThePathConditionWhenFoundStatesEveryLinkedValue() {
70+
void omitsThePathConditionWhenFoundStatesEveryLinkedValue() {
7171
RefinementError error = error("x > 1", vc("∀x:int. x == 1", "∀port:int. port > 0", "true"), "x=1", "port=1");
72-
assertEquals(List.of(pair("port", "1")), error.getCounterexample().assignments());
72+
assertTrue(error.getCounterexample().isEmpty());
73+
}
74+
75+
@Test
76+
void keepsAPathConditionTheExpectedValueIsComputedFrom() {
77+
RefinementError error = error("#ret_1 > 10",
78+
vc("∀port:int. port < 0", "∀#ret_1:int. #ret_1 == port + 1", "true"), "#ret_1=0", "port=-1");
79+
assertEquals(List.of(pair("#ret_1", "0"), pair("port", "-1")), error.getCounterexample().assignments());
7380
}
7481

7582
@Test

0 commit comments

Comments
 (0)