Skip to content

Commit be9b5ba

Browse files
Suggest result checks and argument conditions in state hints
Methods whose state refinements mention their parameters or result were left out of the state machine, so the hints could not suggest checking canRedo() before redo(), or calling setCompressionMode(int) with mode == 2. They are now kept as call variants: with any arguments, with arguments meeting each condition the refinement tests, and, for a boolean result, with the result checked. Boolean methods whose return refinement relates the result to the state are kept as checks too. The solver works out what a variant does only when a hint needs it. A check is suggested only if the verifier allows it in every state the receiver can be in, its result can have the value checked, and the failing call is then allowed; if several checks would do, none is suggested. An argument condition is suggested only if every argument meeting it leads to a state that allows the call, and the value shown for an earlier call's argument is now checked the same way. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent dc1a63f commit be9b5ba

20 files changed

Lines changed: 1658 additions & 57 deletions

File tree

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
package testSuite;
2+
3+
import liquidjava.specification.StateRefinement;
4+
import liquidjava.specification.StateSet;
5+
6+
// The programs of ErrorStateHintGuards and ErrorStateHintArgumentConditions with their hints followed
7+
public class CorrectStateHintGuards {
8+
9+
static void negated() {
10+
FixedChannel c = FixedChannel.make();
11+
if (!c.isClosed())
12+
c.send();
13+
}
14+
15+
static void withParameter() {
16+
FixedLock l = new FixedLock();
17+
if (l.tryLock(10))
18+
l.unlock();
19+
}
20+
21+
static void checkAllowed() {
22+
FixedWriter w = new FixedWriter();
23+
if (w.canWrite())
24+
w.write();
25+
}
26+
27+
static void twoParameters() {
28+
FixedRange r = new FixedRange();
29+
r.set(1, 2);
30+
r.scan();
31+
}
32+
33+
static void thenAnotherCall() {
34+
FixedChain chain = new FixedChain();
35+
chain.pick(1);
36+
chain.next();
37+
chain.done();
38+
}
39+
}
40+
41+
@StateSet({"open", "closed"})
42+
class FixedChannel {
43+
@StateRefinement(to = "open(this)")
44+
public FixedChannel() {}
45+
46+
@StateRefinement(to = "return ? closed(this) : open(this)")
47+
public boolean isClosed() { return false; }
48+
49+
@StateRefinement(from = "open(this)", to = "closed(this)")
50+
public void close() {}
51+
52+
@StateRefinement(from = "open(this)")
53+
public void send() {}
54+
55+
static FixedChannel make() { return new FixedChannel(); }
56+
}
57+
58+
@StateSet({"unlocked", "locked"})
59+
class FixedLock {
60+
@StateRefinement(to = "unlocked(this)")
61+
public FixedLock() {}
62+
63+
@StateRefinement(from = "unlocked(this)", to = "return ? locked(this) : unlocked(this)")
64+
public boolean tryLock(long timeout) { return true; }
65+
66+
@StateRefinement(from = "locked(this)", to = "unlocked(this)")
67+
public void unlock() {}
68+
}
69+
70+
@StateSet({"start", "accept"})
71+
class FixedWriter {
72+
@StateRefinement(to = "start(this)")
73+
public FixedWriter() {}
74+
75+
@StateRefinement(from = "start(this)", to = "return ? accept(this) : start(this)")
76+
public boolean canWrite() { return true; }
77+
78+
@StateRefinement(from = "accept(this)")
79+
public void write() {}
80+
81+
static FixedWriter make() { return new FixedWriter(); }
82+
}
83+
84+
@StateSet({"low", "high"})
85+
class FixedRange {
86+
@StateRefinement(to = "low(this)")
87+
public FixedRange() {}
88+
89+
@StateRefinement(to = "lo < hi ? high(this) : low(this)")
90+
public void set(int lo, int hi) {}
91+
92+
@StateRefinement(from = "high(this)")
93+
public void scan() {}
94+
}
95+
96+
@StateSet({"a", "b", "c"})
97+
class FixedChain {
98+
@StateRefinement(to = "a(this)")
99+
public FixedChain() {}
100+
101+
@StateRefinement(from = "a(this)", to = "k == 1 ? b(this) : a(this)")
102+
public void pick(int k) {}
103+
104+
@StateRefinement(from = "b(this)", to = "c(this)")
105+
public void next() {}
106+
107+
@StateRefinement(from = "c(this)")
108+
public void done() {}
109+
}
Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
package testSuite;
2+
3+
import liquidjava.specification.Ghost;
4+
import liquidjava.specification.StateRefinement;
5+
import liquidjava.specification.StateSet;
6+
7+
// A call whose state after depends on its arguments is suggested with the condition its refinement tests, only when
8+
// every argument that meets the condition leads to a state that allows the failing call
9+
public class ErrorStateHintArgumentConditions {
10+
11+
// a condition on two parameters
12+
static void twoParameters() {
13+
ConditionRange r = new ConditionRange();
14+
r.scan(); // Expect: State Refinement Error
15+
}
16+
17+
// a condition, then a call that needs no condition
18+
static void thenAnotherCall() {
19+
ConditionChain chain = new ConditionChain();
20+
chain.done(); // Expect: State Refinement Error
21+
}
22+
23+
// the negation of the condition the refinement tests
24+
static void negatedCondition() {
25+
ConditionLimit l = new ConditionLimit();
26+
l.stream(); // Expect: State Refinement Error
27+
}
28+
29+
// the condition does not decide the state
30+
static void undecided() {
31+
ConditionLoose l = new ConditionLoose();
32+
l.go(); // Expect: State Refinement Error
33+
}
34+
35+
// nor does an argument value that meets it: no value is suggested for the earlier call
36+
static void undecidedValue() {
37+
ConditionLoose l = new ConditionLoose();
38+
l.set(1);
39+
l.go(); // Expect: State Refinement Error
40+
}
41+
42+
// the condition mentions a ghost of this, and then of another parameter: not a condition on the arguments alone
43+
static void ghostOfThis() {
44+
ConditionSized s = new ConditionSized();
45+
s.shrink(); // Expect: State Refinement Error
46+
}
47+
48+
static void ghostOfParameter() {
49+
ConditionBin b = new ConditionBin();
50+
b.dump(); // Expect: State Refinement Error
51+
}
52+
}
53+
54+
@StateSet({"low", "high"})
55+
class ConditionRange {
56+
@StateRefinement(to = "low(this)")
57+
public ConditionRange() {}
58+
59+
@StateRefinement(to = "lo < hi ? high(this) : low(this)")
60+
public void set(int lo, int hi) {}
61+
62+
@StateRefinement(from = "high(this)")
63+
public void scan() {}
64+
}
65+
66+
@StateSet({"a", "b", "c"})
67+
class ConditionChain {
68+
@StateRefinement(to = "a(this)")
69+
public ConditionChain() {}
70+
71+
@StateRefinement(from = "a(this)", to = "k == 1 ? b(this) : a(this)")
72+
public void pick(int k) {}
73+
74+
@StateRefinement(from = "b(this)", to = "c(this)")
75+
public void next() {}
76+
77+
@StateRefinement(from = "c(this)")
78+
public void done() {}
79+
}
80+
81+
@StateSet({"bounded", "unbounded"})
82+
class ConditionLimit {
83+
@StateRefinement(to = "bounded(this)")
84+
public ConditionLimit() {}
85+
86+
@StateRefinement(to = "limit > 0 ? bounded(this) : unbounded(this)")
87+
public void setLimit(int limit) {}
88+
89+
@StateRefinement(from = "unbounded(this)")
90+
public void stream() {}
91+
}
92+
93+
@StateSet({"a", "b"})
94+
class ConditionLoose {
95+
@StateRefinement(to = "b(this)")
96+
public ConditionLoose() {}
97+
98+
@StateRefinement(to = "mode == 2 ? (a(this) || b(this)) : b(this)")
99+
public void set(int mode) {}
100+
101+
@StateRefinement(from = "a(this)")
102+
public void go() {}
103+
}
104+
105+
@Ghost("int size")
106+
@StateSet({"small", "big"})
107+
class ConditionSized {
108+
@StateRefinement(to = "small(this) && size(this) == 0")
109+
public ConditionSized() {}
110+
111+
@StateRefinement(to = "n > size(old(this)) ? big(this) : small(this)")
112+
public void grow(int n) {}
113+
114+
@StateRefinement(from = "big(this)")
115+
public void shrink() {}
116+
}
117+
118+
@Ghost("int capacity")
119+
@StateSet({"empty", "full"})
120+
class ConditionBin {
121+
@StateRefinement(to = "empty(this)")
122+
public ConditionBin() {}
123+
124+
@StateRefinement(from = "empty(this)", to = "capacity(other) > 3 ? full(this) : empty(this)")
125+
public void fill(ConditionBin other) {}
126+
127+
@StateRefinement(from = "full(this)")
128+
public void dump() {}
129+
}

0 commit comments

Comments
 (0)