Skip to content

Commit 997c95c

Browse files
authored
RFC: Quantifiers (rust-lang#3242)
RFC describing the support for existential and universal quantifiers in Kani. By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 and MIT licenses. --------- Signed-off-by: Felipe R. Monteiro <felisous@amazon.com>
1 parent 1491dd6 commit 997c95c

File tree

2 files changed

+204
-0
lines changed

2 files changed

+204
-0
lines changed

rfc/src/SUMMARY.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,4 @@
1515
- [0007-global-conditions](rfcs/0007-global-conditions.md)
1616
- [0008-line-coverage](rfcs/0008-line-coverage.md)
1717
- [0009-function-contracts](rfcs/0009-function-contracts.md)
18+
- [0010-quantifiers](rfcs/0010-quantifiers.md)

rfc/src/rfcs/0010-quantifiers.md

Lines changed: 203 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,203 @@
1+
- **Feature Name:** Quantifiers
2+
- **Feature Request Issue:** [#2546](https://github.com/model-checking/kani/issues/2546) and [#836](https://github.com/model-checking/kani/issues/836)
3+
- **RFC PR:** [#](https://github.com/model-checking/kani/pull/)
4+
- **Status:** Unstable
5+
- **Version:** 1.0
6+
7+
-------------------
8+
9+
## Summary
10+
11+
Quantifiers are logical operators that allow users to express that a property or condition applies to some or all objects within a given domain.
12+
13+
## User Impact
14+
15+
There are two primary quantifiers: the existential quantifier (∃) and the universal quantifier (∀).
16+
17+
1. The existential quantifier (∃): represents the statement "there exists." We use to express that there is at least one object in the domain that satisfies a given condition. For example, "∃x P(x)" means "there exists a value x such that P(x) is true."
18+
19+
2. The universal quantifier (∀): represents the statement "for all" or "for every." We use it to express that a given condition is true for every object in the domain. For example, "∀x P(x)" means "for every value x, P(x) is true."
20+
21+
Rather than exhaustively listing all elements in a domain, quantifiers enable users to make statements about the entire domain at once. This compact representation is crucial when dealing with large or unbounded inputs. Quantifiers also facilitate abstraction and generalization of properties. Instead of specifying properties for specific instances, quantified properties can capture general patterns and behaviors that hold across different objects in a domain. Additionally, by replacing loops in the specification with quantifiers, Kani can encode the properties more efficiently within the specified bounds, making the verification process more manageable and computationally feasible.
22+
23+
This new feature doesn't introduce any breaking changes to users. It will only allow them to write properites using the existential (∃) and universal (∀) quantifiers.
24+
25+
## User Experience
26+
27+
We propose a syntax inspired by ["Pattern Types"](https://github.com/rust-lang/rust/pull/120131). The syntax of existential (i.e., `kani::exists`) and universal (i.e., `kani::forall`) quantifiers are:
28+
29+
```rust
30+
kani::exists(|<var>: <type> [is <range-expr>] | <boolean-expression>)
31+
kani::forall(|<var>: <type> [is <range-expr>] | <boolean-expression>)
32+
```
33+
34+
If `<range-expr>` is not provided, we assume `<var>` can range over all possible values of the given `<type>` (i.e., syntactic sugar for full range `|<var>: <type> as .. |`). CBMC's SAT backend only supports bounded quantification under **constant** lower and upper bounds (for more details, see the [documentation for quantifiers in CBMC](https://diffblue.github.io/cbmc/contracts-quantifiers.html)). The SMT backend, on the other hand, supports arbitrary Boolean expressions. In any case, `<boolean-expression>` should not have side effects, as the purpose of quantifiers is to assert a condition over a domain of objects without altering the state.
35+
36+
Consider the following example adapted from the documentation for the [from_raw_parts](https://doc.rust-lang.org/std/vec/struct.Vec.html#method.from_raw_parts) function:
37+
38+
```rust
39+
use std::ptr;
40+
use std::mem;
41+
42+
#[kani::proof]
43+
fn main() {
44+
let v = vec![kani::any::<usize>(); 100];
45+
46+
// Prevent running `v`'s destructor so we are in complete control
47+
// of the allocation.
48+
let mut v = mem::ManuallyDrop::new(v);
49+
50+
// Pull out the various important pieces of information about `v`
51+
let p = v.as_mut_ptr();
52+
let len = v.len();
53+
let cap = v.capacity();
54+
55+
unsafe {
56+
// Overwrite memory
57+
for i in 0..len {
58+
*p.add(i) += 1;
59+
}
60+
61+
// Put everything back together into a Vec
62+
let rebuilt = Vec::from_raw_parts(p, len, cap);
63+
}
64+
}
65+
```
66+
67+
Given the `v` vector has non-deterministic values, there are potential arithmetic overflows that might happen in the for loop. So we need to constrain all values of the array. We may also want to check all values of `rebuilt` after the operation. Without quantifiers, we might be tempted to use loops as follows:
68+
69+
```rust
70+
use std::ptr;
71+
use std::mem;
72+
73+
#[kani::proof]
74+
fn main() {
75+
let original_v = vec![kani::any::<usize>(); 100];
76+
let v = original_v.clone();
77+
for i in 0..v.len() {
78+
kani::assume(v[i] < 5);
79+
}
80+
81+
// Prevent running `v`'s destructor so we are in complete control
82+
// of the allocation.
83+
let mut v = mem::ManuallyDrop::new(v);
84+
85+
// Pull out the various important pieces of information about `v`
86+
let p = v.as_mut_ptr();
87+
let len = v.len();
88+
let cap = v.capacity();
89+
90+
unsafe {
91+
// Overwrite memory
92+
for i in 0..len {
93+
*p.add(i) += 1;
94+
}
95+
96+
// Put everything back together into a Vec
97+
let rebuilt = Vec::from_raw_parts(p, len, cap);
98+
for i in 0..len {
99+
assert_eq!(rebuilt[i], original_v[i]+1);
100+
}
101+
}
102+
}
103+
```
104+
105+
This, however, might unnecessary increase the complexity of the verication process. We can achieve the same effect using quantifiers as shown below.
106+
107+
```rust
108+
use std::ptr;
109+
use std::mem;
110+
111+
#[kani::proof]
112+
fn main() {
113+
let original_v = vec![kani::any::<usize>(); 3];
114+
let v = original_v.clone();
115+
kani::assume(kani::forall(|i: usize is ..v.len() | v[i] < 5));
116+
117+
// Prevent running `v`'s destructor so we are in complete control
118+
// of the allocation.
119+
let mut v = mem::ManuallyDrop::new(v);
120+
121+
// Pull out the various important pieces of information about `v`
122+
let p = v.as_mut_ptr();
123+
let len = v.len();
124+
let cap = v.capacity();
125+
126+
unsafe {
127+
// Overwrite memory
128+
for i in 0..len {
129+
*p.add(i) += 1;
130+
}
131+
132+
// Put everything back together into a Vec
133+
let rebuilt = Vec::from_raw_parts(p, len, cap);
134+
assert!(kani::forall(|i: usize is ..len | rebuilt[i] == original_v[i]+1));
135+
}
136+
}
137+
```
138+
139+
The same principle applies if we want to use the existential quantifier.
140+
141+
```rust
142+
use std::ptr;
143+
use std::mem;
144+
145+
#[kani::proof]
146+
fn main() {
147+
let original_v = vec![kani::any::<usize>(); 3];
148+
let v = original_v.clone();
149+
kani::assume(kani::forall(|i: usize is ..v.len() | v[i] < 5));
150+
151+
// Prevent running `v`'s destructor so we are in complete control
152+
// of the allocation.
153+
let mut v = mem::ManuallyDrop::new(v);
154+
155+
// Pull out the various important pieces of information about `v`
156+
let p = v.as_mut_ptr();
157+
let len = v.len();
158+
let cap = v.capacity();
159+
160+
unsafe {
161+
// Overwrite memory
162+
for i in 0..len {
163+
*p.add(i) += 1;
164+
if i == 1 {
165+
*p.add(i) = 0;
166+
}
167+
}
168+
169+
// Put everything back together into a Vec
170+
let rebuilt = Vec::from_raw_parts(p, len, cap);
171+
assert!(kani::exists(|i: usize is ..len | rebuilt[i] == 0));
172+
}
173+
}
174+
```
175+
176+
The usage of quantifiers should be valid in any part of the Rust code analysed by Kani.
177+
178+
## Detailed Design
179+
180+
<!-- For the implementors or the hackers -->
181+
182+
Kani should have the same support that CBMC has for quantifiers. For more details, see [Quantifiers](https://github.com/diffblue/cbmc/blob/0a69a64e4481473d62496f9975730d24f194884a/doc/cprover-manual/contracts-quantifiers.md).
183+
184+
185+
## Open questions
186+
187+
<!-- For Developers -->
188+
- **Function Contracts RFC** - CBMC has support for both `exists` and `forall`, but the
189+
code generation is difficult. The most ergonomic and easy way to implement
190+
quantifiers on the Rust side is as higher-order functions taking `Fn(T) ->
191+
bool`, where `T` is some arbitrary type that can be quantified over. This
192+
interface is familiar to developers, but the code generation is tricky, as
193+
CBMC level quantifiers only allow certain kinds of expressions. This
194+
necessitates a rewrite of the `Fn` closure to a compliant expression.
195+
- Which kind of expressions should be accepted as a "compliant expression"?
196+
197+
198+
## Future possibilities
199+
200+
<!-- For Developers -->
201+
- CBMC has an SMT backend which allows the use of quantifiers with arbitrary Boolean expressions. Kani must include an option for users to experiment with this backend.
202+
203+
---

0 commit comments

Comments
 (0)