Skip to content

Commit 9ce8f93

Browse files
dlucanudlucanu
dlucanu
authored and
dlucanu
committed
added examples/used-in-papers/fmtea2023/winstrat-with-bug-with-spec.alk
1 parent f3bebf7 commit 9ce8f93

File tree

3 files changed

+39
-12
lines changed

3 files changed

+39
-12
lines changed

.ignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
.DS_Store
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
winingStrategy(n)
2+
@requires n: int;
3+
@requires n > 0;
4+
@ensures \result: int;
5+
@ensures (\old(n) % 3 == 0 ==> \result == 2) && (\old(n) % 3 != 0 ==> \result == 1);
6+
{
7+
turn = 1;
8+
if (n % 3 != 0) {
9+
take = n % 3;
10+
n = n - take;
11+
turn = 2;
12+
}
13+
while (n > 0)
14+
@modifies n, turn
15+
@invariant n % 3 == 0 && n >= 0
16+
@invariant n > 0 ==> (\old(n) % 3 == 0 ==> turn == 1) &&
17+
(\old(n) % 3 != 0 ==> turn == 2);
18+
@invariant n == 0 ==> ((\old(n) % 3 == 0 ==> turn == 2) &&
19+
(\old(n) % 3 != 0 ==> turn == 1));
20+
{
21+
// the current turn
22+
choose take from {1,2};
23+
n = n - take;
24+
// the next turn
25+
turn = 3 - turn;
26+
take = n % 3;
27+
n = n - take;
28+
// prepare the next current turn (if any)
29+
if (n > 0) turn = 3 - turn;
30+
}
31+
return turn;
32+
}
33+
34+

examples/used-in-papers/fmtea2023/winstrat-with-bug.alk

+4-12
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,3 @@
1-
/*
2-
A box contains N matches. Two players take turns taking one or two matches from the box.
3-
The player, whose turn it is and has no legal move to make, loses.
4-
Who has the winning strategy?
5-
*/
6-
7-
/*
8-
We show first that
9-
- if N is a multiple of 3 then the second player has a strategy to win;
10-
- otherwise, the first player has one.
11-
*/
121
winingStrategy(n)
132
{
143
turn = 1;
@@ -32,7 +21,10 @@ winingStrategy(n)
3221
return turn;
3322
}
3423

24+
/*
3525
for (i = 1; i < 10; ++i) {
36-
print([i, winingStrategy(i)]);
26+
w[i-1] = winingStrategy(i);
3727
}
28+
*/
29+
w = winingStrategy(2);
3830

0 commit comments

Comments
 (0)