Skip to content

Commit 3aa3189

Browse files
dlucanudlucanu
dlucanu
authored and
dlucanu
committed
examples/added used-in-papers
1 parent 01e35b6 commit 3aa3189

20 files changed

+490
-0
lines changed
6 KB
Binary file not shown.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
COMPACT_LIST_SEARCH(L, n, k)
2+
{
3+
i = 0;
4+
while (i < L.size() && L.at(i).key < k)
5+
{
6+
uniform j from [1..n];
7+
if (L.at(i).key < L.at(j).key && L.at(j).key < k) {
8+
i = j;
9+
if (L.at(i).key == k) return i;
10+
}
11+
i = i+1;
12+
}
13+
if (i == L.size() || L.at(i).key > k)
14+
return -1;
15+
else return i;
16+
}
17+
18+
x = COMPACT_LIST_SEARCH(L, n, k);
19+
20+
//L = < {key -> 5}, {key -> 8}, {key -> 12}, {key -> 15}, {key -> 24} >;
21+
//x = COMPACT_LIST_SEARCH(L, 3, 12);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
uniform j from [1..n];
2+
if (L.at(i).key < L.at(j).key && L.at(j).key < k) {
3+
i = j;
4+
if (L.at(i).key == k) return i;
5+
}
6+
i = i+1;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
/*base(out n)
2+
@requires n: int
3+
@requires n == 3
4+
@ensures n == 0
5+
*/
6+
{
7+
// the first turn
8+
// choose take from {1,2};
9+
// ... symbolically
10+
n=3;
11+
@havoc take: int;
12+
@assume 1 <= take && take <= 2;
13+
n = n - take;
14+
// the next turn
15+
take = n % 3;
16+
n = n - take;
17+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
// the first turn
2+
choose take from {1,2};
3+
n = n - take;
4+
// the next turn
5+
take = n;
6+
n = n - take;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
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+
*/
12+
winingStrategy(n)
13+
@requires n: int
14+
@requires n > 0 && n % 3 == 0;
15+
@ensures \result == 2;
16+
{
17+
turn = 1;
18+
while (n > 0)
19+
@modifies n, turn;
20+
@invariant n % 3 == 0 && n >= 0
21+
@invariant n > 0 ==> turn == 1;
22+
@invariant n == 0 ==> turn == 2;
23+
{
24+
// the current turn
25+
choose take from {1,2};
26+
n = n - take;
27+
// the next turn
28+
turn = 3 - turn;
29+
take = n % 3;
30+
n = n - take;
31+
// prepare the next current turn (if any)
32+
if (n > 0) turn = 3 - turn;
33+
}
34+
return turn;
35+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
winingStrategy(n)
2+
{
3+
turn = 1;
4+
while (n > 0)
5+
{
6+
// the current turn
7+
choose take from {1,2};
8+
n = n - take;
9+
// the next turn
10+
turn = 3 - turn;
11+
take = n % 3;
12+
n = n - take;
13+
// prepare the next current turn (if any)
14+
if (n > 0) turn = 3 - turn;
15+
}
16+
return turn;
17+
}
18+
19+
for (i = 1; i < 6; ++i)
20+
w[i-1] = winingStrategy(3*i);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
@havoc n: int;
2+
@assume n % 3 == 0;
3+
// the first turn
4+
// choose take from {1,2};
5+
// ... symbolically
6+
@havoc take: int;
7+
@assume 1 <= take && take <= 2;
8+
n = n - take;
9+
// the next turn
10+
take = n % 3;
11+
n = n - take;
12+
@assert n % 3 == 0;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
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+
winingStrategy(n)
7+
@requires n: int;
8+
@requires n > 0;
9+
@ensures \result: int;
10+
@ensures (\old(n) % 3 == 0 ==> \result == 2) && (\old(n) % 3 != 0 ==> \result == 1);
11+
{
12+
turn = 1;
13+
if (n % 3 != 0) {
14+
take = n % 3;
15+
n = n - take;
16+
if (n > 0) turn = 2;
17+
}
18+
while (n > 0)
19+
@modifies n, turn
20+
@invariant n % 3 == 0 && n >= 0
21+
@invariant n > 0 ==> (\old(n) % 3 == 0 ==> turn == 1) && (\old(n) % 3 != 0 ==> turn == 2);
22+
@invariant n == 0 ==> ((\old(n) % 3 == 0 ==> turn == 2) && (\old(n) % 3 != 0 ==> turn == 1));
23+
{
24+
// the current turn
25+
choose take from {1,2};
26+
n = n - take;
27+
// the next turn
28+
turn = 3 - turn;
29+
take = n % 3;
30+
n = n - take;
31+
// prepare the next current turn (if any)
32+
if (n > 0) turn = 3 - turn;
33+
}
34+
return turn;
35+
}
36+
37+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
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+
*/
12+
winingStrategy(n)
13+
{
14+
turn = 1;
15+
if (n % 3 != 0) {
16+
take = n % 3;
17+
n = n - take;
18+
if (n > 0) turn = 2;
19+
}
20+
while (n > 0)
21+
{
22+
// the current turn
23+
choose take from {1,2};
24+
n = n - take;
25+
// the next turn
26+
turn = 3 - turn;
27+
take = n % 3;
28+
n = n - take;
29+
// prepare the next current turn (if any)
30+
if (n > 0) turn = 3 - turn;
31+
}
32+
return turn;
33+
}
34+
35+
for (i = 1; i < 10; ++i) {
36+
print([i, winingStrategy(i)]);
37+
}
38+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/*
2+
alki -a askith_assert.alk -s -smt="Z3"
3+
4+
a |-> $a_0
5+
res |-> 0
6+
i |-> $i_0
7+
Path condition:
8+
((($a_0.size()>0)&&(0<=$i_0))&&($i_0<($a_0.size()-1))) &&
9+
!($a_0[$i_0]>$a_0[($i_0+1)])
10+
11+
a |-> (store (store $a_0 $i_0 $a_0[($i_0+1)]) ($i_0+1) $a_0[$i_0])
12+
res |-> 0
13+
tmp |-> $a_0[$i_0]
14+
i |-> $i_0
15+
Path condition:
16+
((($a_0.size()>0)&&(0<=$i_0))&&($i_0<($a_0.size()-1))) &&
17+
($a_0[$i_0]>$a_0[($i_0+1)]) &&
18+
validStore($a_0, $i_0, $a_0[($i_0+1)]) &&
19+
validStore((store $a_0 $i_0 $a_0[($i_0+1)]), ($i_0+1), $a_0[$i_0])
20+
*/
21+
22+
@havoc a : array<int>;
23+
@havoc i : int;
24+
@assume a.size() > 0 && 0 <= i && i < a.size() - 1;
25+
if (a[i] > a[i + 1])
26+
{
27+
tmp = a[i];
28+
a[i] = a[i + 1];
29+
a[i + 1] = tmp;
30+
res = i;
31+
}
32+
res = 0;
33+
@assert a[i] <= a[i + 1];
34+
@assert res > 0 ==> res == i;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/*
2+
alki -a askith_call.alk -m -s -smt="Z3"
3+
4+
Successfully verified: askIth
5+
6+
a |-> $a_1
7+
last |-> $\result_0
8+
Path condition:
9+
($a.size()>3) &&
10+
($a_1[2]<=$a_1[3]) &&
11+
($\result_0>0) ==> ($\result_0==2)
12+
13+
*/
14+
15+
askIth(out a, i)
16+
@requires a : array<int>
17+
@requires i : int
18+
@requires a.size() > 0 && 0 <= i && i < a.size() - 1
19+
@ensures a[i] <= a[i + 1]
20+
@ensures \result > 0 ==> \result == i
21+
@ensures \result : int
22+
{
23+
if (a[i] > a[i + 1])
24+
{
25+
tmp = a[i];
26+
a[i] = a[i + 1];
27+
a[i + 1] = tmp;
28+
return i;
29+
}
30+
return 0;
31+
}
32+
33+
@symbolic $a : array<int>;
34+
a = $a;
35+
@assume a.size() > 3;
36+
last = askIth(a, 2);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/*
2+
alki -a askith_verify.alk -s -smt="Z3"
3+
4+
Successfully verified: askIth
5+
*/
6+
7+
askIth(out a, i)
8+
@requires a : array<int>
9+
@requires i : int
10+
@requires a.size() > 0 && 0 <= i && i < a.size() - 1
11+
@ensures a[i] <= a[i + 1]
12+
@ensures \result > 0 ==> \result == i
13+
@ensures \result : int
14+
{
15+
if (a[i] > a[i + 1])
16+
{
17+
tmp = a[i];
18+
a[i] = a[i + 1];
19+
a[i + 1] = tmp;
20+
return i;
21+
}
22+
return 0;
23+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/*
2+
alki -a bubblesort.alk -m -i "b |-> [2, 3, 1]"
3+
b |-> [1, 2, 3]
4+
alki -a bubblesort.alk -m -i "b |-> [1.2, 1.5, 1.4]"
5+
b |-> [1.2, 1.4, 1.5]
6+
alki -a bubblesort.alk -m -i "b |-> [\"ab\", \"ac\", \"aa\"]"
7+
b |-> ["aa", "ab", "ac"]
8+
*/
9+
10+
swap(out a, i, j)
11+
@requires 0 <= i && i < a.size()
12+
@requires 0 <= j && j < a.size()
13+
{
14+
temp = a[i];
15+
a[i] = a[j];
16+
a[j] = temp;
17+
}
18+
19+
askIth(out a, i)
20+
@requires a.size() > 1
21+
@requires 0 <= i && i < a.size() - 1
22+
{
23+
if (a[i] > a[i + 1]) {
24+
swap(a, i, i + 1);
25+
return i;
26+
}
27+
return 0;
28+
}
29+
30+
bubbleSort(out a)
31+
@requires a.size() > 0
32+
{
33+
last = a.size() - 1;
34+
while (last > 0)
35+
{
36+
n1 = last;
37+
for (i = 0; i < n1; ++i)
38+
{
39+
last = askIth(a, i);
40+
}
41+
}
42+
}
43+
44+
bubbleSort(b);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
/*
2+
alki -a fstalg.alk -m -i "a |-> [1, 2, 2, 3, 3, 3]"
3+
4+
a |-> [1, 2, 2, 3, 3, 3]
5+
slp |-> 3
6+
i |-> 6
7+
*/
8+
9+
slp = 1;
10+
i = 1;
11+
while (i < a.size())
12+
{
13+
if (a[i] == a[i - slp])
14+
slp = slp + 1;
15+
i = i + 1;
16+
}

0 commit comments

Comments
 (0)