Skip to content

Commit 330264d

Browse files
Drop a ghost definition only when nothing else constrains that ghost
The conjunct _ == g(...) is left out only when g is a declared ghost (a built-in such as length may be interpreted), g(...) is in no other conjunct and not in Expected, and every fact of Found that mentions it is the argument's own equality with it. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent f23b4de commit 330264d

1 file changed

Lines changed: 33 additions & 12 deletions

File tree

  • liquidjava-verifier/src/main/java/liquidjava/processor/refinement_checker

‎liquidjava-verifier/src/main/java/liquidjava/processor/refinement_checker/OriginNotes.java‎

Lines changed: 33 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,9 @@ static List<String> unrefinedParameters(RefinementError error, CtElement checked
6969

7070
/**
7171
* For each argument of expected that is a call to a refined method, what the method's return refinement guarantees
72-
* about it. An equality defining a ghost, {@code _ == size(this)}, is left out when the ghost is nowhere else in
73-
* the refinement: it then constrains no value, so what is left is all it says about the value
72+
* about it. An equality defining a ghost, {@code _ == size(this)}, is left out when nothing else the error knows or
73+
* expects mentions that ghost application: it then constrains no value, so what is left is all the refinement says
74+
* about the value
7475
*/
7576
static List<String> returnedValues(RefinementError error, Context context) {
7677
List<String> notes = new ArrayList<>();
@@ -84,7 +85,7 @@ static List<String> returnedValues(RefinementError error, Context context) {
8485
: call.function().getPlacementInCode().getAnnotationPosition();
8586
if (declared == null || !declared.isValidPosition() || declared.getFile() == null)
8687
continue;
87-
Expression guaranteed = withoutGhostDefinitions(call.returned().getExpression());
88+
Expression guaranteed = withoutGhostDefinitions(call.returned().getExpression(), variable, error, context);
8889
String value = ExpressionFormatter.format(new Var(variable), error.getDisplayNames());
8990
String says = isTrue(guaranteed) ? "guarantees nothing about " + value
9091
: "guarantees only " + ExpressionFormatter.format(
@@ -95,29 +96,49 @@ static List<String> returnedValues(RefinementError error, Context context) {
9596
return notes;
9697
}
9798

98-
private static Expression withoutGhostDefinitions(Expression refinement) {
99+
private static Expression withoutGhostDefinitions(Expression refinement, String variable, RefinementError error,
100+
Context context) {
99101
List<Expression> facts = factsOf(refinement);
102+
List<Expression> known = factsOf(error.getFound().getOriginal().getImplication().toPredicate().getExpression());
100103
Expression kept = new LiteralBoolean(true);
101104
for (Expression fact : facts) {
102-
boolean definesGhost = ghostDefined(fact).filter(
103-
ghost -> facts.stream().noneMatch(other -> other != fact && containsExpression(other, ghost)))
105+
boolean definesGhost = ghostDefined(fact).filter(ghost -> isGhost(ghost.getName(), context))
106+
.filter(ghost -> facts.stream()
107+
.noneMatch(other -> other != fact && containsExpression(other, ghost)))
108+
.filter(ghost -> !containsExpression(error.getExpected().getExpression(), ghost))
109+
.filter(ghost -> known.stream().noneMatch(
110+
other -> containsExpression(other, ghost) && !definesGhostAs(other, ghost, variable)))
104111
.isPresent();
105112
if (!definesGhost)
106113
kept = isTrue(kept) ? fact : new BinaryExpression(kept, "&&", fact);
107114
}
108115
return kept;
109116
}
110117

111-
/** The ghost application {@code g(...)} of a fact {@code _ == g(...)} */
112-
private static Optional<Expression> ghostDefined(Expression fact) {
118+
/** Whether {@code name} is a declared ghost, unlike a built-in such as {@code length}, which may be interpreted */
119+
private static boolean isGhost(String name, Context context) {
120+
return context.getGhosts().stream().anyMatch(g -> g.matches(name))
121+
|| context.getGhostStates().stream().anyMatch(g -> g.getParent() == null && g.matches(name));
122+
}
123+
124+
/** Whether {@code fact} is {@code variable == ghost}, the argument's own definition */
125+
private static boolean definesGhostAs(Expression fact, Expression ghost, String variable) {
126+
return fact instanceof BinaryExpression equality && "==".equals(equality.getOperator())
127+
&& (equality.getFirstOperand().equals(new Var(variable)) && equality.getSecondOperand().equals(ghost)
128+
|| equality.getSecondOperand().equals(new Var(variable))
129+
&& equality.getFirstOperand().equals(ghost));
130+
}
131+
132+
/** The function application {@code g(...)} of a fact {@code _ == g(...)} */
133+
private static Optional<FunctionInvocation> ghostDefined(Expression fact) {
113134
if (!(fact instanceof BinaryExpression equality) || !"==".equals(equality.getOperator()))
114135
return Optional.empty();
115136
Expression left = equality.getFirstOperand();
116137
Expression right = equality.getSecondOperand();
117-
if (isWildcard(left) && right instanceof FunctionInvocation)
118-
return Optional.of(right);
119-
if (isWildcard(right) && left instanceof FunctionInvocation)
120-
return Optional.of(left);
138+
if (isWildcard(left) && right instanceof FunctionInvocation ghost)
139+
return Optional.of(ghost);
140+
if (isWildcard(right) && left instanceof FunctionInvocation ghost)
141+
return Optional.of(ghost);
121142
return Optional.empty();
122143
}
123144

0 commit comments

Comments
 (0)