Skip to content

Commit 30f3be2

Browse files
author
Long Pham
committed
Multidimensional decreases clauses
1 parent 06c563a commit 30f3be2

File tree

14 files changed

+326
-66
lines changed

14 files changed

+326
-66
lines changed
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
#include <stdio.h>
2+
3+
int ackermann(int m, int n);
4+
5+
int main()
6+
{
7+
int m = 3;
8+
int n = 5;
9+
int result = ackermann(m, n);
10+
11+
printf("Result of the Ackermann function: %d\n", result);
12+
return 0;
13+
}
14+
15+
int ackermann(int m, int n)
16+
// clang-format off
17+
__CPROVER_requires(0 <= m && 0 <= n)
18+
__CPROVER_ensures(__CPROVER_return_value >= 0)
19+
// clang-format on
20+
{
21+
while(m > 0)
22+
// clang-format off
23+
__CPROVER_loop_invariant(0 <= m && 0 <= n)
24+
__CPROVER_decreases(m, n)
25+
// clang-format on
26+
{
27+
if(n == 0)
28+
{
29+
m--;
30+
n = 1;
31+
}
32+
else
33+
{
34+
n = ackermann(m, n - 1);
35+
m--;
36+
}
37+
}
38+
39+
return n + 1;
40+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
CORE
2+
main.c
3+
--apply-loop-contracts --replace-all-calls-with-contracts
4+
activate-multi-line-match
5+
^\[precondition.1\] file main.c .* Check requires clause: SUCCESS$
6+
^\[precondition.2\] file main.c .* Check requires clause: SUCCESS$
7+
^\[ackermann.1\] .* Check loop invariant before entry: SUCCESS$
8+
^\[ackermann.2\] .* Check that loop invariant is preserved: SUCCESS$
9+
^\[ackermann.3\] .* Check decreases clause on loop iteration: SUCCESS$
10+
^.*0 of 5 failed \(1 iterations\)$
11+
^VERIFICATION SUCCESSFUL$
12+
^EXIT=0$
13+
^SIGNAL=0$
14+
--
15+
--
16+
This test succeeds. It tests whether we can prove (only partially) the
17+
termination of the Ackermann function using a multidimensional decreases clause.
18+
19+
Note that this particular implementation of the Ackermann function contains
20+
both a while-loop and recursion. Therefore, to fully prove the termination of
21+
the Ackermann function, we must prove both
22+
(i) the termination of the while-loop and
23+
(ii) the termination of the recursion.
24+
Because CBMC does not support termination proofs of recursions (yet), we cannot
25+
prove the latter, but the former. Hence, the termination proof in the code is
26+
only "partial."
27+
28+
Furthermore, the Ackermann function has a function contract that the result
29+
is always non-negative. This post-condition is necessary for establishing
30+
the loop invariant. However, in this test, we do not enforce the function
31+
contract. Instead, we assume that the function contract is correct and use it
32+
(i.e. replace a recursive call of the Ackermann function with its contract).
33+
34+
We cannot verify/enforce the function contract of the Ackermann function, since
35+
CBMC does not support function contracts for recursively defined functions.
36+
As of now, CBMC only supports function contracts for non-recursive functions.
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
2+
int main()
3+
{
4+
int i = 0;
5+
int j = 0;
6+
int N = 10;
7+
while(i < N)
8+
// clang-format off
9+
__CPROVER_loop_invariant(0 <= i && i <= N && 0 <= j && j <= N)
10+
__CPROVER_decreases(N - i, N - j)
11+
// clang-format on
12+
{
13+
if(j < N)
14+
{
15+
j++;
16+
}
17+
else
18+
{
19+
i++;
20+
j = 0;
21+
}
22+
}
23+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
CORE
2+
main.c
3+
--apply-loop-contracts
4+
activate-multi-line-match
5+
^\[main.1\].*Check loop invariant before entry: SUCCESS$
6+
^\[main.2\].*Check that loop invariant is preserved: SUCCESS$
7+
^\[main.3\].*Check decreases clause on loop iteration: SUCCESS$
8+
^EXIT=0$
9+
^SIGNAL=0$
10+
--
11+
--
12+
This test succeeds. It tests whether a multidimensional decreases clause works
13+
properly.
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
2+
int main()
3+
{
4+
int i = 0;
5+
int j = 0;
6+
int N = 10;
7+
while(i < N)
8+
// clang-format off
9+
__CPROVER_loop_invariant(0 <= i && i <= N && 0 <= j && j <= N)
10+
__CPROVER_decreases(N - i, j)
11+
// clang-format on
12+
{
13+
if(j < N)
14+
{
15+
j++;
16+
}
17+
else
18+
{
19+
i++;
20+
j = 0;
21+
}
22+
}
23+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
CORE
2+
main.c
3+
--apply-loop-contracts
4+
activate-multi-line-match
5+
^\[main.1\] .* Check loop invariant before entry: SUCCESS$
6+
^\[main.2\] .* Check that loop invariant is preserved: SUCCESS$
7+
^\[main.3\] .* Check decreases clause on loop iteration: FAILURE$
8+
^.*1 of 3 failed \(2 iterations\)$
9+
^VERIFICATION FAILED$
10+
^EXIT=10$
11+
^SIGNAL=0$
12+
--
13+
--
14+
This test fails because the given multidimensional decreases clause does not
15+
monotonically decrease. A mistake is in the second component of the
16+
decreases clause: j. It should instead be N - j.

regression/contracts/variant_parsing_tuple_fail/main.c renamed to regression/contracts/variant_parsing_multiple_clauses_fail/main.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ int main()
55
while(i != N)
66
// clang-format off
77
__CPROVER_loop_invariant(0 <= i && i <= N)
8-
__CPROVER_decreases(N - i, 42)
8+
__CPROVER_decreases(N - i)
9+
__CPROVER_decreases(42)
910
// clang-format on
1011
{
1112
i++;
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
CORE
2+
main.c
3+
--apply-loop-contracts
4+
activate-multi-line-match
5+
^main.c.*error: syntax error before '__CPROVER_decreases'\n.*__CPROVER_decreases\(42\)$
6+
^PARSING ERROR$
7+
^EXIT=(1|64)$
8+
^SIGNAL=0$
9+
--
10+
--
11+
This test fails because we have multiple decreases clauses. CBMC only admits
12+
one decreases clause for each loop. If one wants to write a multidimensional
13+
decreases clause, it should be placed inside a single __CPROVER_decreases(...),
14+
where each component of the multidimensional decreases clause is separated by a
15+
comma. Hence, in this test, the correct syntax is
16+
__CPROVER_decreases(N - i, 42).

regression/contracts/variant_parsing_tuple_fail/test.desc

Lines changed: 0 additions & 13 deletions
This file was deleted.

src/ansi-c/c_typecheck_code.cpp

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -828,9 +828,11 @@ void c_typecheck_baset::typecheck_spec_decreases(codet &code)
828828
{
829829
if(code.find(ID_C_spec_decreases).is_not_nil())
830830
{
831-
exprt &variant = static_cast<exprt &>(code.add(ID_C_spec_decreases));
832-
833-
typecheck_expr(variant);
834-
implicit_typecast_arithmetic(variant);
831+
for(auto &decreases_clause :
832+
(static_cast<exprt &>(code.add(ID_C_spec_decreases)).operands()))
833+
{
834+
typecheck_expr(decreases_clause);
835+
implicit_typecast_arithmetic(decreases_clause);
836+
}
835837
}
836838
}

0 commit comments

Comments
 (0)