-
Notifications
You must be signed in to change notification settings - Fork 274
Allow quantifiers within loop invariants #6012
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
SaswatPadhi
merged 2 commits into
diffblue:develop
from
ArenBabikian:loop-quantifiers-temp
May 20, 2021
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
#include <assert.h> | ||
|
||
#define N 16 | ||
|
||
void main() | ||
{ | ||
int a[N]; | ||
a[10] = 0; | ||
|
||
for(int i = 0; i < N; ++i) | ||
// clang-format off | ||
__CPROVER_loop_invariant( | ||
(0 <= i) && (i <= N) && | ||
__CPROVER_forall { | ||
int k; | ||
// constant bounds for explicit unrolling with SAT backend | ||
(0 <= k && k <= N) ==> ( | ||
// the actual symbolic bound for `k` | ||
k < i ==> a[k] == 1 | ||
) | ||
} | ||
) | ||
// clang-format on | ||
{ | ||
a[i] = 1; | ||
} | ||
|
||
assert(a[10] == 1); | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
CORE | ||
main.c | ||
--enforce-all-contracts | ||
^EXIT=0$ | ||
^SIGNAL=0$ | ||
^\[main.1\] line .* Check loop invariant before entry: SUCCESS | ||
^\[main.2\] line .* Check that loop invariant is preserved: SUCCESS | ||
^\[main.assertion.1\] line .* assertion a\[10\] == 1: SUCCESS | ||
^VERIFICATION SUCCESSFUL$ | ||
-- | ||
-- | ||
This test case checks the handling of a `forall` quantifier within a loop invariant. | ||
|
||
This test case uses explicit constant bounds on the quantified variable, | ||
so that it can be unrolled (to conjunctions) with the SAT backend. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
#include <assert.h> | ||
|
||
void main() | ||
{ | ||
int N, a[64]; | ||
__CPROVER_assume(0 <= N && N < 64); | ||
|
||
for(int i = 0; i < N; ++i) | ||
// clang-format off | ||
__CPROVER_loop_invariant( | ||
(0 <= i) && (i <= N) && | ||
__CPROVER_forall { | ||
int k; | ||
(0 <= k && k < i) ==> a[k] == 1 | ||
} | ||
) | ||
// clang-format on | ||
{ | ||
a[i] = 1; | ||
} | ||
|
||
// clang-format off | ||
assert(__CPROVER_forall { | ||
int k; | ||
(0 <= k && k < N) ==> a[k] == 1 | ||
}); | ||
// clang-format on | ||
|
||
int k; | ||
__CPROVER_assume(0 <= k && k < N); | ||
assert(a[k] == 1); | ||
SaswatPadhi marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
KNOWNBUG smt-backend broken-cprover-smt-backend | ||
main.c | ||
--enforce-all-contracts | ||
^EXIT=0$ | ||
^SIGNAL=0$ | ||
^\[main.1\] line .* Check loop invariant before entry: SUCCESS | ||
^\[main.2\] line .* Check that loop invariant is preserved: SUCCESS | ||
^\[main.assertion.1\] line .* assertion .*: SUCCESS | ||
^VERIFICATION SUCCESSFUL$ | ||
-- | ||
-- | ||
This test case checks the handling of a universal quantifier, with a symbolic | ||
upper bound, within a loop invariant. | ||
|
||
The test is tagged: | ||
- `smt-backend`: | ||
because the SAT backend does not support (simply ignores) `forall` in negative (e.g. assume) contexts. | ||
- `broken-cprover-smt-backend`: | ||
because the CPROVER SMT2 solver cannot handle (errors out on) `forall` in negative (e.g. assume) contexts. | ||
|
||
It has been tagged `KNOWNBUG` for now since `contracts` regression tests are not run with SMT backend yet. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
#include <assert.h> | ||
#include <stdlib.h> | ||
|
||
#define MAX_SIZE 64 | ||
|
||
void main() | ||
{ | ||
unsigned N; | ||
__CPROVER_assume(N <= MAX_SIZE); | ||
|
||
int *a = malloc(N * sizeof(int)); | ||
|
||
for(int i = 0; i < N; ++i) | ||
// clang-format off | ||
__CPROVER_loop_invariant( | ||
(0 <= i) && (i <= N) && | ||
(i != 0 ==> __CPROVER_exists { | ||
int k; | ||
// constant bounds for explicit unrolling with SAT backend | ||
(0 <= k && k <= MAX_SIZE) && ( | ||
// the actual symbolic bound for `k` | ||
k < i && a[k] == 1 | ||
) | ||
}) | ||
) | ||
// clang-format on | ||
{ | ||
a[i] = 1; | ||
} | ||
|
||
// clang-format off | ||
assert( | ||
N != 0 ==> __CPROVER_exists { | ||
int k; | ||
// constant bounds for explicit unrolling with SAT backend | ||
(0 <= k && k <= MAX_SIZE) && ( | ||
// the actual symbolic bound for `k` | ||
k < N && a[k] == 1 | ||
) | ||
}); | ||
// clang-format on | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
CORE | ||
main.c | ||
--enforce-all-contracts | ||
^EXIT=0$ | ||
^SIGNAL=0$ | ||
^\[main.1\] line .* Check loop invariant before entry: SUCCESS | ||
^\[main.2\] line .* Check that loop invariant is preserved: SUCCESS | ||
^\[main.assertion.1\] line .* assertion .*: SUCCESS | ||
^VERIFICATION SUCCESSFUL$ | ||
-- | ||
-- | ||
This test case checks the handling of an existential quantifier, with a symbolic | ||
upper bound, within a loop invariant. | ||
|
||
This test case uses explicit constant bounds on the quantified variable, | ||
so that it can be unrolled (to conjunctions) with the SAT backend. |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.