Skip to content

Commit f240944

Browse files
Show the states of named sets as values of their sets in state errors
Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 6572019 commit f240944

10 files changed

Lines changed: 84 additions & 14 deletions

File tree

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

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import java.util.ArrayList;
66
import java.util.List;
77
import java.util.function.Predicate;
8+
import java.util.function.UnaryOperator;
89

910
import liquidjava.diagnostics.TranslationTable;
1011
import liquidjava.processor.context.PlacementInCode;
@@ -18,10 +19,11 @@ record ErrorMessage(String text, DisplayNames names) {
1819

1920
/**
2021
* Writes the facts of found about the values expected constrains and expected in frame, or only expected when
21-
* showsFound rejects those facts; path conditions about other values stay in the derivation
22+
* showsFound rejects those facts; path conditions about other values stay in the derivation. The facts are chosen
23+
* on expected and found as given, and written as display rewrites them
2224
*/
2325
static ErrorMessage of(Expression expected, VCSimplificationResult found, TranslationTable translationTable,
24-
Predicate<Expression> showsFound, String frame) {
26+
Predicate<Expression> showsFound, String frame, UnaryOperator<Expression> display) {
2527
Expression shown = linkedToExpected(found.getImplication().toPredicate().getExpression(), expected);
2628
// the names of the message first, then those only the original found has, each with the line of its value
2729
List<String> message = new ArrayList<>();
@@ -34,9 +36,9 @@ static ErrorMessage of(Expression expected, VCSimplificationResult found, Transl
3436
PlacementInCode placement = translationTable == null ? null : translationTable.get(name);
3537
return placement == null ? null : placement.getLine();
3638
});
37-
String expectedText = ExpressionFormatter.format(expected, names);
39+
String expectedText = ExpressionFormatter.format(display.apply(expected), names);
3840
String text = showsFound.test(shown)
39-
? String.format(frame, ExpressionFormatter.format(shown, names), expectedText)
41+
? String.format(frame, ExpressionFormatter.format(display.apply(shown), names), expectedText)
4042
: String.format("expected %s but could not prove it", expectedText);
4143
return new ErrorMessage(text, names);
4244
}

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import java.util.List;
88
import java.util.Map;
99
import java.util.Set;
10+
import java.util.function.UnaryOperator;
1011
import java.util.stream.Collectors;
1112

1213
import liquidjava.diagnostics.AliasExpansion;
@@ -51,8 +52,10 @@ public class RefinementError extends LJError {
5152
public RefinementError(SourcePosition position, SourcePosition declarationPosition, Predicate expected,
5253
VCSimplificationResult found, TranslationTable translationTable, Counterexample counterexample,
5354
String customMessage, List<AliasWrapper> aliases) {
54-
super("Refinement Error", ErrorMessage.of(expected.getExpression(), found, translationTable,
55-
shown -> !isTrue(shown), "%s is not a subtype of %s"), position, translationTable, customMessage);
55+
super("Refinement Error",
56+
ErrorMessage.of(expected.getExpression(), found, translationTable, shown -> !isTrue(shown),
57+
"%s is not a subtype of %s", UnaryOperator.identity()),
58+
position, translationTable, customMessage);
5659
this.expected = expected;
5760
this.found = found;
5861
Expression foundExpression = found.getImplication().toPredicate().getExpression();

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

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import static liquidjava.rj_language.opt.VCSimplificationUtils.isTrue;
44

55
import java.util.List;
6+
import java.util.function.UnaryOperator;
67

78
import liquidjava.diagnostics.TranslationTable;
89
import liquidjava.diagnostics.categories.ErrorCategory;
@@ -26,15 +27,20 @@ public class StateRefinementError extends LJError {
2627
private final SourcePosition declarationPosition;
2728
private final StateErrorContext context;
2829

30+
/**
31+
* @param display
32+
* rewrites expected and found as the error shows them, here and to the IDE, after the facts to show are
33+
* chosen on them as given
34+
*/
2935
public StateRefinementError(SourcePosition position, SourcePosition declarationPosition, Predicate expected,
3036
VCSimplificationResult found, TranslationTable translationTable, String customMessage, List<Hint> hints,
31-
ErrorCategory category, StateErrorContext context) {
37+
ErrorCategory category, StateErrorContext context, UnaryOperator<Expression> display) {
3238
super("State Refinement Error", ErrorMessage.of(expected.getExpression(), found, translationTable,
33-
StateRefinementError::appliesGhost, "found %s but expected %s"), position, translationTable,
39+
StateRefinementError::appliesGhost, "found %s but expected %s", display), position, translationTable,
3440
customMessage);
3541
this.declarationPosition = declarationPosition;
36-
this.expected = expected;
37-
this.found = found;
42+
this.expected = new Predicate(display.apply(expected.getExpression()));
43+
this.found = found.map(display);
3844
this.context = context;
3945
hints.forEach(this::addHint);
4046
setCategory(category);

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,8 @@ public void registerGhostsAndStateSets(CtElement element) throws LJError {
213213
SourcePosition namePosition = nameValue == null ? null : Utils.getAnnotationValuePosition(nameValue);
214214
String name = stateSetName(nameValue, namePosition);
215215
CtNewArray<String> states = (CtNewArray<String>) ann.getAllValues().get("value");
216-
GhostFunction g = createStateSet(states, name != null ? name : String.format("state%d", set), element);
216+
GhostFunction g = createStateSet(states, name != null ? name : String.format(Formats.STATE_SET, set),
217+
element);
217218
stateSets.put(g, name != null ? namePosition : Utils.getAnnotationValuePosition(states));
218219
checkStatesAreNotInAnotherSet(g, states, setOfState);
219220
}

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import liquidjava.processor.VCImplication;
1818
import liquidjava.processor.context.*;
1919
import liquidjava.rj_language.Predicate;
20+
import liquidjava.rj_language.StateValues;
2021
import liquidjava.rj_language.ast.Expression;
2122
import liquidjava.rj_language.ast.Ite;
2223
import liquidjava.rj_language.ast.Var;
@@ -463,7 +464,7 @@ protected void throwStateRefinementError(SourcePosition position, SourcePosition
463464
VCSimplificationResult shown = VCSimplification.simplifyOnce(simplified,
464465
new VCRelevantFacts(expected.getExpression(), this::ghostFunctionsOf));
465466
throw new StateRefinementError(position, declarationPosition, expected, shown, map, customMessage, hints,
466-
category, context);
467+
category, context, e -> StateValues.toSetForm(e, this.context.getGhostStates()));
467468
}
468469

469470
/** The ghost functions an invocation name stands for: a state's state set function, or the ghost itself */

‎liquidjava-verifier/src/main/java/liquidjava/rj_language/StateValues.java‎

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,18 @@
1616
import liquidjava.rj_language.ast.FunctionInvocation;
1717
import liquidjava.rj_language.ast.UnaryExpression;
1818
import liquidjava.rj_language.ast.Var;
19+
import liquidjava.utils.Utils;
20+
import liquidjava.utils.constants.Formats;
1921

2022
/**
2123
* Reads a state written as a value of its state set, {@code phase(e) == open}, as the state shorthand {@code open(e)},
2224
* so that everything that reads states sees one form
2325
*/
2426
public final class StateValues {
2527

28+
/** A name {@link Formats#STATE_SET} generates */
29+
private static final String GENERATED_SET_NAME = "state\\d+";
30+
2631
private final List<GhostState> states;
2732
private final Predicate<String> isVariable;
2833
private final BiPredicate<Expression, GhostFunction> appliesTo;
@@ -90,6 +95,43 @@ private Optional<FunctionInvocation> stateOf(Expression set, Expression name) th
9095
return Optional.empty();
9196
}
9297

98+
/**
99+
* {@code expression} with each state of a named set, {@code open(e)}, written as a value of its set,
100+
* {@code phase(e) == open}, and {@code !open(e)} as {@code phase(e) != open}, for display. A state keeps the
101+
* shorthand when its set has a generated name, such as {@code state1}, or when sets with different names have a
102+
* state of that name
103+
*/
104+
public static Expression toSetForm(Expression expression, List<GhostState> states) {
105+
return rewriteToSetForm(expression.clone(), states);
106+
}
107+
108+
private static Expression rewriteToSetForm(Expression e, List<GhostState> states) {
109+
if (e instanceof UnaryExpression u && u.getOp().equals("!")) {
110+
Optional<BinaryExpression> value = setValue(u.getExpression(), "!=", states);
111+
if (value.isPresent())
112+
return value.get();
113+
}
114+
Optional<BinaryExpression> value = setValue(e, "==", states);
115+
if (value.isPresent())
116+
return value.get();
117+
for (int i = 0; i < e.getChildren().size(); i++)
118+
e.setChild(i, rewriteToSetForm(e.getChildren().get(i), states));
119+
return e;
120+
}
121+
122+
/** {@code set(e) op state} when {@code e} is the state {@code state(e)} of a set with one, given name */
123+
private static Optional<BinaryExpression> setValue(Expression e, String op, List<GhostState> states) {
124+
if (!(e instanceof FunctionInvocation invocation && invocation.getArgs().size() == 1))
125+
return Optional.empty();
126+
List<String> setNames = states.stream().filter(s -> s.getParent() != null && s.matches(invocation.getName()))
127+
.map(s -> s.getParent().getName()).distinct().toList();
128+
if (setNames.size() != 1 || setNames.get(0).matches(GENERATED_SET_NAME))
129+
return Optional.empty();
130+
FunctionInvocation set = new FunctionInvocation(qualifiedLike(invocation.getName(), setNames.get(0)),
131+
List.of(invocation.getArgs().get(0).clone()));
132+
return Optional.of(new BinaryExpression(set, op, new Var(Utils.getSimpleName(invocation.getName()))));
133+
}
134+
93135
/** The states of {@code set}, or only those named {@code name} when it is not null */
94136
private List<GhostState> statesOf(GhostFunction set, String name) {
95137
return states.stream().filter(s -> s.getParent() == set && (name == null || s.getName().equals(name))).toList();

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,11 @@
22

33
import java.util.ArrayList;
44
import java.util.List;
5+
import java.util.function.UnaryOperator;
56

67
import liquidjava.processor.VCImplication;
8+
import liquidjava.rj_language.Predicate;
9+
import liquidjava.rj_language.ast.Expression;
710

811
/**
912
* Result of simplifying VC implication chain
@@ -65,6 +68,15 @@ public List<String> getBinders() {
6568
return binderNames;
6669
}
6770

71+
/** This result with each refinement of its chain, and of the chains it was simplified from, rewritten by f */
72+
public VCSimplificationResult map(UnaryOperator<Expression> f) {
73+
VCImplication chain = implication.clone();
74+
for (VCImplication row = chain; row != null; row = row.getNext())
75+
if (row.getRefinement() != null)
76+
row.setRefinement(new Predicate(f.apply(row.getRefinement().getExpression())));
77+
return new VCSimplificationResult(chain, origin == null ? null : origin.map(f), simplification);
78+
}
79+
6880
@Override
6981
public String toString() {
7082
if (origin == null)

‎liquidjava-verifier/src/main/java/liquidjava/utils/constants/Formats.java‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,6 @@ public final class Formats {
55
public static final String THIS = "this#%s";
66
public static final String RET = "#ret_%d";
77
public static final String ENUM = "%s.%s";
8+
/** The name of a state set declared without one, numbered from 1 in the order of the declarations */
9+
public static final String STATE_SET = "state%d";
810
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -343,7 +343,7 @@ void foundOfTheStudysUndoManagerOmitsTheResultsOfOtherCalls() {
343343
void foundKeepsANamedStateSetByItsName() {
344344
StateRefinementError error = errors("ErrorStateHintMultipleSets.java", 1).get(0);
345345
assertEquals(
346-
"found closed(d@26) && locked(d) && doorState(d) == doorState(d@26) but expected closed(d) && unlocked(d)",
346+
"found doorState(d@26) == closed && lockState(d) == locked && doorState(d) == doorState(d@26) but expected doorState(d) == closed && lockState(d) == unlocked",
347347
error.getMessage().replaceAll("[⁰¹²³⁴⁵⁶⁷⁸⁹]", ""));
348348
}
349349

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
import java.util.Arrays;
99
import java.util.List;
10+
import java.util.function.UnaryOperator;
1011

1112
import org.junit.jupiter.api.Test;
1213

@@ -143,7 +144,7 @@ void stateMessageKeepsAStateReachingExpectedThroughAnAlias() {
143144
new Predicate(RefinementsParser.createAST("full(box)", "")),
144145
new VCSimplificationResult(
145146
vc("∀b1:Object. empty(b1)", "∀x:int. x > 0", "∀box:Object. box == b1", "true")),
146-
new TranslationTable(), null, List.of(), null, null);
147+
new TranslationTable(), null, List.of(), null, null, UnaryOperator.identity());
147148
assertEquals("found empty(b1) && box == b1 but expected full(box)", error.getMessage());
148149
}
149150

0 commit comments

Comments
 (0)