Skip to content

Commit 53f8b1d

Browse files
Expand aliases before translating states (#507)
Fixes #503 **Cause.** Before Z3, every refinement went through `changeStatesToRefinements` and then `changeAliasToRefinement`. A state that only appears once an alias is expanded, such as `closed(x)` in the body of `Closed(x)`, was never translated to its state set. It stayed an uninterpreted Boolean unrelated to `state1(x)`, so `Closed(_)` rejected a closed object. **Fix.** One method, `Predicate.toSolverPredicate`, now does both steps in the right order: expand aliases, then translate states, then validate ghost invocations. Validation moves to the end so it sees the translated states. It replaces the six copies of the old pair in `VCChecker` and `TypeChecker`. **Before / after** (`ErrorStateInAlias`): ``` Refinement Error: closed(door) is not a subtype of Closed(door) where Closed(x) = closed(x) ``` was reported at the first `needsClosed(door)`, right after the constructor. Now that call verifies, and only the call after `door.open()` is reported. This is also the step #502 relies on to translate `phase(x) == closed` in alias bodies. **Tests.** `ErrorStateInAlias`: one call that must verify and one that must fail. On main the first call fails. Full suite passes locally. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 2c4f484 commit 53f8b1d

4 files changed

Lines changed: 45 additions & 21 deletions

File tree

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package testSuite;
2+
3+
import liquidjava.specification.Refinement;
4+
import liquidjava.specification.RefinementAlias;
5+
import liquidjava.specification.StateRefinement;
6+
import liquidjava.specification.StateSet;
7+
8+
@StateSet({"closed", "opened"})
9+
@RefinementAlias("Closed(ErrorStateInAlias x) { closed(x) }")
10+
public class ErrorStateInAlias {
11+
12+
@StateRefinement(to = "closed(this)")
13+
public ErrorStateInAlias() {}
14+
15+
@StateRefinement(from = "closed(this)", to = "opened(this)")
16+
public void open() {}
17+
18+
static void needsClosed(@Refinement("Closed(_)") ErrorStateInAlias door) {}
19+
20+
public static void main(String[] args) {
21+
ErrorStateInAlias door = new ErrorStateInAlias();
22+
needsClosed(door);
23+
door.open();
24+
needsClosed(door); // Expect: Refinement Error
25+
}
26+
}

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

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -167,9 +167,8 @@ private void checkRefinementSatisfiability(String refinement, Predicate predicat
167167
if (qualifiedClassName != null && !context.hasVariable(Keys.THIS))
168168
context.addVarToContext(Keys.THIS, factory.Type().createReference(qualifiedClassName), p, element);
169169

170-
Predicate refinementPredicate = predicate.changeStatesToRefinements(context.getGhostStates(),
171-
new String[] { Keys.WILDCARD, Keys.THIS });
172-
refinementPredicate = refinementPredicate.changeAliasToRefinement(context, factory);
170+
Predicate refinementPredicate = predicate.toSolverPredicate(context.getGhostStates(),
171+
new String[] { Keys.WILDCARD, Keys.THIS }, context, factory);
173172

174173
if (new SMTEvaluator().isUnsatisfiable(refinementPredicate, context)) {
175174
SourcePosition position = Utils.getLJAnnotationPosition(element, refinement);

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

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,8 @@ public void processSubtyping(Predicate expectedType, List<GhostState> list, CtEl
5959
Predicate expected;
6060
try {
6161
List<GhostState> filtered = filterGhostStatesForVariables(list, mainVars, lrv);
62-
premises = premisesBeforeChange.changeStatesToRefinements(filtered, s).changeAliasToRefinement(context, f);
63-
expected = expectedType.changeStatesToRefinements(filtered, s).changeAliasToRefinement(context, f);
62+
premises = premisesBeforeChange.toSolverPredicate(filtered, s, context, f);
63+
expected = expectedType.toSolverPredicate(filtered, s, context, f);
6464
transformChainForDebug(impl, filtered, s, f);
6565
} catch (LJError e) {
6666
// add location info to error
@@ -186,14 +186,12 @@ public SMTResult verifySMTSubtypeStates(Predicate type, Predicate expectedType,
186186
VCImplication impl = joinPredicates(expectedType, mainVars, lrv, map);
187187
Predicate premisesJoined = impl.toConjunctions();
188188
List<GhostState> filtered = filterGhostStatesForVariables(states, mainVars, lrv);
189-
Predicate premises = Predicate.createConjunction(premisesJoined, type).changeStatesToRefinements(filtered, s)
190-
.changeAliasToRefinement(context, factory);
191-
Predicate expected = expectedType.changeStatesToRefinements(filtered, s).changeAliasToRefinement(context,
189+
Predicate premises = Predicate.createConjunction(premisesJoined, type).toSolverPredicate(filtered, s, context,
192190
factory);
191+
Predicate expected = expectedType.toSolverPredicate(filtered, s, context, factory);
193192
if (!silent) {
194193
transformChainForDebug(impl, filtered, s, factory);
195-
Predicate foundExtra = type.changeStatesToRefinements(filtered, s).changeAliasToRefinement(context,
196-
factory);
194+
Predicate foundExtra = type.toSolverPredicate(filtered, s, context, factory);
197195
DebugLog.smtStart(impl, foundExtra, expected);
198196
}
199197

@@ -250,9 +248,9 @@ private List<GhostState> filterGhostStatesForVariables(List<GhostState> list, Li
250248
}
251249

252250
/**
253-
* Apply the same {@code changeStatesToRefinements} / {@code changeAliasToRefinement} pipeline to each node of a
254-
* {@link VCImplication} chain so the structured debug print mirrors the predicates Z3 actually sees. Mutates the
255-
* chain in place; safe because {@code joinPredicates} returns a clone.
251+
* Apply the same {@code toSolverPredicate} pipeline to each node of a {@link VCImplication} chain so the structured
252+
* debug print mirrors the predicates Z3 actually sees. Mutates the chain in place; safe because
253+
* {@code joinPredicates} returns a clone.
256254
*/
257255
private void transformChainForDebug(VCImplication chain, List<GhostState> filtered, String[] s, Factory f) {
258256
if (!DebugLog.enabled())
@@ -261,8 +259,7 @@ private void transformChainForDebug(VCImplication chain, List<GhostState> filter
261259
if (n.getRefinement() == null)
262260
continue;
263261
try {
264-
n.setRefinement(
265-
n.getRefinement().changeStatesToRefinements(filtered, s).changeAliasToRefinement(context, f));
262+
n.setRefinement(n.getRefinement().toSolverPredicate(filtered, s, context, f));
266263
} catch (LJError ignored) {
267264
// best-effort transformation for debug; leave node as-is on failure
268265
}

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

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -154,17 +154,19 @@ protected Expression innerParse(String ref, String prefix) throws LJError {
154154
return RefinementsParser.createAST(ref, prefix);
155155
}
156156

157-
public Predicate changeAliasToRefinement(Context context, Factory f) throws LJError {
158-
Expression ref = getExpression();
159-
157+
/**
158+
* The predicate the solver checks: aliases expanded first, so that the states their bodies use are replaced too,
159+
* then the states replaced by the refinements of their state sets
160+
*/
161+
public Predicate toSolverPredicate(List<GhostState> ghostStates, String[] toChange, Context context, Factory f)
162+
throws LJError {
160163
Map<String, AliasDTO> alias = new HashMap<>();
161164
for (AliasWrapper aw : context.getAliases()) {
162165
alias.put(aw.getName(), aw.createAliasDTO());
163166
}
164-
165-
ref = ref.changeAlias(alias, context, f);
167+
Predicate expanded = new Predicate(getExpression().changeAlias(alias, context, f));
168+
Expression ref = expanded.changeStatesToRefinements(ghostStates, toChange).getExpression();
166169
ref.validateGhostInvocations(context, f);
167-
168170
return new Predicate(ref);
169171
}
170172

0 commit comments

Comments
 (0)