-
Notifications
You must be signed in to change notification settings - Fork 109
/
bisect.rs
54 lines (49 loc) · 1.47 KB
/
bisect.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
// compile-flags: -Puse_more_complete_exhale=false
use prusti_contracts::*;
/// A monotonically strictly increasing discrete function, with domain [0, domain_size)
pub trait Function {
#[pure]
fn domain_size(&self) -> usize;
#[pure]
#[requires(x < self.domain_size())]
fn eval(&self, x: usize) -> i32;
predicate!{
fn invariant(&self) -> bool {
forall(|x1: usize, x2: usize|
x1 < x2 && x2 < self.domain_size() ==> self.eval(x1) < self.eval(x2)
)
}
}
}
/// Find the unique `x` s.t. `f(x) == target`
#[requires(f.invariant())]
#[ensures(match result {
Some(found_x) => {
f.eval(found_x) == target &&
forall(|x: usize| x < f.domain_size() && f.eval(x) == target ==> x == found_x)
}
None => {
forall(|x: usize| x < f.domain_size() ==> f.eval(x) != target)
}
})]
pub fn bisect<T: Function>(f: &T, target: i32) -> Option<usize> {
let mut low = 0;
let mut high = f.domain_size();
while low < high {
body_invariant!(high <= f.domain_size());
body_invariant!(forall(|x: usize|
(x < low || high <= x) && x < f.domain_size() ==> f.eval(x) != target
));
let mid = low + ((high - low) / 2);
let mid_val = f.eval(mid);
if mid_val < target {
low = mid + 1;
} else if mid_val > target {
high = mid;
} else {
return Some(mid)
}
}
None
}
fn main() {}