-
Notifications
You must be signed in to change notification settings - Fork 121
Add support for quantifiers #3993
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
Merged
Changes from all commits
Commits
Show all changes
28 commits
Select commit
Hold shift + click to select a range
a92688f
Add support for quantifiers
feliperodri e8c5e43
Format code and avoid code duplication
feliperodri 0be406a
Inlining function calls in quantifier expressions
qinheping f838e91
Fix ranged existential quantification
qinheping 6d0017b
Add support of switch in quantifier expression
qinheping df78d91
Remove debug leftover
qinheping d463882
Make tests depending on new CBMC fixme
qinheping bf9eb97
Fix clippy
qinheping f21041d
Fix clippy
qinheping 49c22c9
Fix clippy
qinheping 8210209
Make universal quantification complete
qinheping e276642
Make universal quantification complete
qinheping 5a60da9
Add tests for quantifier expression with array indexing
qinheping 8c242b8
Add fixme info
qinheping 40195bc
Update RFC with function inlining
qinheping fac313f
Make even test non fixme
qinheping 131d503
Update array tests
qinheping 3bf250b
Add tests with quantifiers in contracts
qinheping acee4ef
Add document for quantifiers
qinheping e336cad
Fix format
qinheping a3549ec
Fix clippy
qinheping 39832fa
Merge branch 'main' into quantifiers
tautschnig 41b4755
Update tests
tautschnig 6d795e9
Add expected tests
qinheping 71d1a92
Fix format
qinheping abb3e8e
Address Carolyn's comments
qinheping c8ea72f
Remove the need of import quantifiers
qinheping 445b809
Make quantifier functions hidden
qinheping 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
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
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
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
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
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,56 @@ | ||
# Quantifiers in Kani | ||
|
||
Quantifiers are a powerful feature in formal verification that allow you to express properties over a range of values. Kani provides experimental support for quantifiers, enabling users to write concise and expressive specifications for their programs. | ||
|
||
## Supported Quantifiers | ||
|
||
Kani currently supports the following quantifiers: | ||
|
||
1. **Universal Quantifier**: | ||
- Ensures that a property holds for all values in a given range. | ||
- Syntax: `kani::forall!(|variable in range| condition)` | ||
- Example: | ||
|
||
```rust | ||
#[kani::proof] | ||
fn test_forall() { | ||
let v = vec![10; 10]; | ||
kani::assert(kani::forall!(|i in 0..10| v[i] == 10)); | ||
} | ||
``` | ||
|
||
2. **Existential Quantifier**: | ||
- Ensures that there exists at least one value in a given range for which a property holds. | ||
- Syntax: `kani::exists!(|variable in range| condition)` | ||
- Example: | ||
|
||
```rust | ||
#[kani::proof] | ||
fn test_exists() { | ||
let v = vec![1, 2, 3, 4, 5]; | ||
kani::assert(kani::exists!(|i in 0..v.len()| v[i] == 3)); | ||
} | ||
``` | ||
|
||
### Limitations | ||
|
||
#### Array Indexing | ||
|
||
The performance of quantifiers can be affected by the depth of call stacks in the quantified expressions. If the call stack is too deep, Kani may not be able to evaluate the quantifier effectively, leading to potential timeouts or running out of memory. Actually, array indexing in Rust leads to a deep call stack, which can cause issues with quantifiers. To mitigate this, consider using *unsafe* pointer dereferencing instead of array indexing when working with quantifiers. For example: | ||
|
||
```rust | ||
|
||
#[kani::proof] | ||
fn vec_assert_forall_harness() { | ||
let v = vec![10 as u8; 128]; | ||
let ptr = v.as_ptr(); | ||
unsafe { | ||
kani::assert(kani::forall!(|i in (0,128)| *ptr.wrapping_byte_offset(i as isize) == 10), ""); | ||
} | ||
} | ||
``` | ||
|
||
#### Types of Quantified Variables | ||
|
||
We now assume that all quantified variables are of type `usize`. This means that the range specified in the quantifier must be compatible with `usize`. | ||
We plan to support other types in the future, but for now, ensure that your quantifiers use `usize` ranges. |
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
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
Oops, something went wrong.
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.