forked from viperproject/prusti-dev
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Fibonacci_sequence.rs
184 lines (166 loc) · 4.45 KB
/
Fibonacci_sequence.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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
//! An adaptation of the example from
//! https://rosettacode.org/wiki/Fibonacci_sequence#Rust
//!
//!
//! Omitted:
//!
//! + Analytical version:
//!
//! + Uses closures.
//! + Uses floating point numbers.
//!
//! Changes:
//!
//! + Replaced ``println!`` with calling trusted functions.
//! + Unified function types.
//! + Renamed functions.
//! + Added ghost counters to prove that all versions generate the same sequence.
//! + Rewrote loops into supported shape (while bool with no break, continue, or return).
//! + Rewrote closure into a match statement.
//! + Replaced Iterator::next function with a function next.
//! + Wrapped built-in types and functions.
//!
//! Verified properties:
//!
//! + Absence of panics.
//! + The verified three implementations print only Fibonacci numbers.
extern crate prusti_contracts;
use std::mem;
#[pure]
fn fib(i: usize) -> usize {
match i {
0 => 0,
1 => 1,
n => fib(n-1) + fib(n-2),
}
}
#[trusted]
#[requires="fib(_i) == n"]
fn print_fib(_i: usize, n: usize) {
println!("{}", n);
}
#[trusted]
#[ensures="*a == old(*b)"]
#[ensures="*b == old(*a)"]
fn swap(a: &mut usize, b: &mut usize) {
mem::swap(a, b);
}
#[trusted]
#[ensures="result.is_some() ==> a + b == result.peek()"]
fn checked_add(a: usize, b: usize) -> UsizeOption {
match a.checked_add(b) {
Some(n) => UsizeOption::Some(n),
None => UsizeOption::None,
}
}
// Iterative
fn iterative_fibonacci() {
let mut prev = 0;
// Rust needs this type hint for the checked_add method
let mut curr = 1usize;
let mut _ghost_counter = 1;
let mut add_succeeded = true;
#[invariant="_ghost_counter >= 1"]
#[invariant="add_succeeded ==> fib(_ghost_counter) == curr"]
#[invariant="add_succeeded ==> fib(_ghost_counter-1) == prev"]
while add_succeeded {
if let UsizeOption::Some(n) = checked_add(curr, prev) {
prev = curr;
curr = n;
_ghost_counter += 1;
print_fib(_ghost_counter, curr);
} else {
add_succeeded = false;
}
}
}
// Recursive
#[requires="_ghost_counter >= 1"]
#[requires="fib(_ghost_counter-1) == prev"]
#[requires="fib(_ghost_counter) == curr"]
fn recursive_fibonacci(_ghost_counter: usize, prev: usize, curr: usize) {
let mut prev = prev;
let mut curr = curr;
swap(&mut prev, &mut curr);
if let UsizeOption::Some(n) = checked_add(curr, prev) {
print_fib(_ghost_counter+1, n);
recursive_fibonacci(_ghost_counter+1, prev, n);
}
}
// Using an Iterator
enum UsizeOption {
Some(usize),
None,
}
impl UsizeOption {
#[pure]
fn is_some(&self) -> bool {
match self {
UsizeOption::Some(_) => true,
UsizeOption::None => false,
}
}
#[pure]
#[requires="self.is_some()"]
fn peek(&self) -> usize {
match self {
UsizeOption::Some(n) => *n,
UsizeOption::None => unreachable!(),
}
}
}
struct Fib {
prev: usize,
curr: usize,
_ghost_counter: usize,
}
impl Fib {
#[ensures="result.valid()"]
#[ensures="result.counter() == 1"]
fn new() -> Self {
Fib {prev: 0, curr: 1, _ghost_counter: 1}
}
#[pure]
fn counter(&self) -> usize {
self._ghost_counter
}
#[pure]
fn valid(&self) -> bool {
self._ghost_counter >= 1 &&
self.prev == fib(self._ghost_counter-1) &&
self.curr == fib(self._ghost_counter)
}
#[requires="self.valid()"]
#[ensures="result.is_some() ==> self.valid()"]
#[ensures="result.is_some() ==> fib(self.counter()) == result.peek()"]
fn next(&mut self) -> UsizeOption {
swap(&mut self.curr, &mut self.prev);
if let UsizeOption::Some(n) = checked_add(self.curr, self.prev) {
self.curr = n;
self._ghost_counter += 1;
UsizeOption::Some(n)
}
else {
UsizeOption::None
}
}
}
fn main() {
let mut iter = Fib::new();
let mut continue_iteration = true;
#[invariant="continue_iteration ==> iter.valid()"]
while continue_iteration {
let item = iter.next();
match item {
UsizeOption::Some(n) => {
let i = iter.counter();
print_fib(i, n);
}
UsizeOption::None => {
continue_iteration = false;
}
}
}
iterative_fibonacci();
recursive_fibonacci(1, 0, 1);
}