-
Notifications
You must be signed in to change notification settings - Fork 82
/
Copy pathnevd_script.rs
214 lines (197 loc) · 4.51 KB
/
nevd_script.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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
fn main() {}
// ## 11 -- 10-program.rs
#[allow(unused_imports)]
use {builtin::*, builtin_macros::*, prelude::*, seq::*, vstd::*};
verus! {
// ## A -- A-program.rs
fn max(a: u64, b: u64) -> (ret: u64)
ensures
ret == a || ret == b,
ret >= a && ret >= b,
{
//- if a >= b { b } else { a }
/*+*/
if a >= b {
a
} else {
b
}
}
// ;; Function-Def crate::max
// (push)
// (declare-const ret~10@ Int)
// (declare-const a~2@ Int)
// (declare-const b~4@ Int)
// (assert fuel_defaults)
// (assert (uInv 64 a~2@))
// (assert (uInv 64 b~4@))
// ;; postcondition not satisfied
// (declare-const %%location_label%%0 Bool)
// ;; postcondition not satisfied
// (declare-const %%location_label%%1 Bool)
// (declare-const %%query%% Bool)
// (assert
// (=>
// %%query%%
// (not (=>
// (= ret~10@ (ite
// (>= a~2@ b~4@)
// a~2@
// b~4@
// ))
// (and
// (=>
// %%location_label%%0
// (or
// (= ret~10@ a~2@)
// (= ret~10@ b~4@)
// ))
// (=>
// %%location_label%%1
// (and
// (>= ret~10@ a~2@)
// (>= ret~10@ b~4@)
// )))))))
// (assert %%query%%)
// (set-option :rlimit 30000000)
// (check-sat)
// (set-option :rlimit 0)
// (pop)
// ## B -- B-fibo.rs
spec fn fibo(n: nat) -> nat
decreases n,
{
if n == 0 {
0
} else if n == 1 {
1
} else {
fibo((n - 2) as nat) + fibo((n - 1) as nat)
}
}
proof fn lemma_fibo_is_monotonic(i: nat, j: nat)
requires
i <= j,
ensures
fibo(i) <= fibo(j),
decreases j - i,
{
if i < 2 && j < 2 {
} else if i == j {
} else if i == j - 1 {
reveal_with_fuel(fibo, 2);
lemma_fibo_is_monotonic(i, (j - 1) as nat);
} else {
lemma_fibo_is_monotonic(i, (j - 1) as nat);
lemma_fibo_is_monotonic(i, (j - 2) as nat);
}
}
spec fn fibo_fits_u64(n: nat) -> bool {
fibo(n) <= 0xffff_ffff_ffff_ffff
}
exec fn fibo_impl(n: u64) -> (result: u64)
requires
fibo_fits_u64(n as nat),
ensures
result == fibo(n as nat),
{
if n == 0 {
return 0;
}
let mut prev: u64 = 0;
let mut cur: u64 = 1;
let mut i: u64 = 1;
while i < n
invariant
0 < i <= n,
fibo_fits_u64(n as nat),
fibo_fits_u64(i as nat),
cur == fibo(i as nat),
prev == fibo((i - 1) as nat),
{
i = i + 1;
proof {
lemma_fibo_is_monotonic(i as nat, n as nat);
}
let new_cur = cur + prev;
prev = cur;
cur = new_cur;
}
cur
}
// ## C -- C-linearity.rs
//- exec fn f(v: Vec<u64>) -> (Vec<u64>, Vec<u64>) {
//- let v1 = v;
//- let v2 = v;
//- (v1, v2)
//- }
/*+*/
exec fn f(v: Vec<u64>) {
/*+*/
let v1: Ghost<Vec<u64>> = Ghost(v);
/*+*/
let v2: Ghost<Vec<u64>> = Ghost(v);
/*+*/
assert([email protected]() == [email protected]());
/*+*/
}
exec fn g(v1: &mut Vec<u64>, v2: &mut Vec<u64>)
requires
old(v1)@.len() == 2,
old(v2)@.len() == 3,
ensures
[email protected]() == [email protected](),
{
v1.push(42);
v1.push(43);
v2.push(52);
}
// exec fn g_(v1: Vec<u64>, v2: Vec<u64>) -> (out: (Vec<u64>, Vec<u64>))
// requires
// [email protected]() == 2,
// [email protected]() == 3,
// ensures
// [email protected]() == [email protected]()
// {
// let v1a = v1.push(42);
// let v1b = v1.push(43);
// let v2a.push(52);
// (v1b, v2a)
// }
// ## E -- E-reverse.rs -- spec variables
/* See vectors.rs
fn reverse(v: &mut Vec<u64>) {
ensures([
v.len() == old(v).len(),
forall(|i: int| 0 <= i && i < old(v).len()
>>= v.index(i) == old(v).index(old(v).len() - i - 1)),
]);
let length = v.len();
#[spec] let v1 = *v;
let mut n: usize = 0;
while n < length / 2 {
invariant([
length == v.len(),
forall(|i: int| n <= i && i + n < length >>= v.index(i) == v1.index(i)),
forall(|i: int| 0 <= i && i < n >>= v.index(i) == v1.index(length - i - 1)),
forall(|i: int| 0 <= i && i < n >>= v1.index(i) == v.index(length - i - 1)),
]);
let x = *v.index(n);
let y = *v.index(length - 1 - n);
v.set(n, y);
v.set(length - 1 - n, x);
n = n + 1;
}
}
*/
// F -- F-linear-proof
// cell::RefCell::Cell<X>
// G -- G-bitvector.rs
fn mod8_bw(x: u32) -> (ret: u32)
ensures
ret == x % 8,
{
assert(x & 7 == x % 8) by (bit_vector);
x & 7
}
} // verus!