-
Notifications
You must be signed in to change notification settings - Fork 79
/
Copy pathprophecy.rs
200 lines (164 loc) · 5.62 KB
/
prophecy.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
#![feature(rustc_private)]
#[macro_use]
mod common;
use common::*;
const PROPH: &str = verus_code_str! {
#[verifier::external_body]
#[verifier::reject_recursive_types_in_ground_variants(T)]
pub tracked struct Prophecy<T> { _t: core::marker::PhantomData<T> }
impl<T> Prophecy<T> {
#[verifier::prophetic]
pub open spec fn value(&self) -> T;
pub open spec fn may_resolve(&self) -> bool;
#[verifier::external_body]
pub proof fn new() -> (tracked s: Self)
ensures s.may_resolve()
{ unimplemented!() }
#[verifier::external_body]
pub proof fn resolve(tracked &mut self, value: T)
requires old(self).may_resolve(),
ensures !self.may_resolve(),
self.value() == old(self).value(),
self.value() == value,
{ unimplemented!() }
}
};
test_verify_one_file! {
#[test] once_flip PROPH.to_string() + verus_code_str! {
use vstd::*;
fn rand() -> bool { true }
struct OnceFlip {
value: Option<bool>,
proph: Tracked<Prophecy<bool>>,
}
impl OnceFlip {
#[verifier::prophetic]
pub closed spec fn result(&self) -> bool {
if [email protected]_resolve() {
} else {
self.value.unwrap()
}
}
pub closed spec fn wf(&self) -> bool {
self.value.is_some() <==> [email protected]_resolve()
}
pub fn new() -> (s: Self)
ensures s.wf(),
{
OnceFlip {
value: None,
proph: Tracked(Prophecy::new()),
}
}
pub fn get_result(&mut self) -> (b: bool)
requires
old(self).wf(),
ensures
self.wf(),
self.result() == old(self).result(),
b == self.result(),
{
if self.value.is_none() {
let flip = rand();
self.value = Some(flip);
proof {
self.proph.borrow_mut().resolve(flip);
}
}
self.value.unwrap()
}
}
} => Ok(())
}
test_verify_one_file! {
#[test] grandfather_paradox PROPH.to_string() + verus_code_str! {
proof fn grandfather_paradox() {
let tracked proph = Prophecy::<bool>::new();
let luigi = proph.value();
let waluigi = !luigi;
proph.resolve(waluigi);
assert(luigi == proph.value());
assert(waluigi == proph.value());
assert(false);
}
} => Err(err) => assert_vir_error_msg(err, "cannot call prophecy-dependent function in prophecy-independent context")
}
test_verify_one_file! {
#[test] proph_dep1 PROPH.to_string() + verus_code_str! {
spec fn stuff(p: Prophecy<bool>) -> bool {
p.value()
}
} => Err(err) => assert_vir_error_msg(err, "cannot call prophecy-dependent function in prophecy-independent context")
}
test_verify_one_file! {
#[test] proph_dep2 PROPH.to_string() + verus_code_str! {
proof fn stuff(p: Prophecy<bool>) {
// This would be okay as long as we track how x is used
let x = p.value();
}
} => Err(err) => assert_vir_error_msg(err, "cannot call prophecy-dependent function in prophecy-independent context")
}
test_verify_one_file! {
#[test] proph_dep_trait_impl PROPH.to_string() + verus_code_str! {
trait Tr {
spec fn f(&self) -> bool;
}
impl Tr for Prophecy<bool> {
#[verifier::prophetic]
spec fn f(&self) -> bool { self.value() }
}
} => Err(err) => assert_vir_error_msg(err, "prophetic attribute not supported on trait functions")
}
test_verify_one_file! {
#[test] calls_requires_ensures PROPH.to_string() + verus_code_str! {
fn freq(tracked t: Prophecy<bool>)
requires t.value() == false,
{ }
proof fn test_req(tracked t: Prophecy<bool>)
requires t.may_resolve(),
{
let tracked mut t = t;
let b = call_requires(freq, (t,));
t.resolve(b);
assert(false); // FAILS
}
fn fens(tracked t: Prophecy<bool>)
ensures t.value() == false,
{ assume(false); }
proof fn test_ens(tracked t: Prophecy<bool>)
requires t.may_resolve(),
{
let tracked mut t = t;
let b = call_ensures(freq, (t,), ());
t.resolve(b);
assert(false); // FAILS
}
fn test_req_closure(Tracked(t): Tracked<Prophecy<bool>>)
requires t.may_resolve(),
{
let tracked mut t = t;
let clos = |t: Prophecy<bool>|
requires t.value() == false,
{ };
proof {
let b = call_requires(clos, (t,));
t.resolve(b);
assert(false); // FAILS
}
}
fn test_ens_closure(tracked t: Prophecy<bool>)
requires t.may_resolve(),
{
let tracked mut t = t;
let clos = |t: Prophecy<bool>|
ensures t.value() == false,
{ assume(false); };
proof {
let b = call_ensures(clos, (t,), ());
t.resolve(b);
assert(false); // FAILS
}
}
} => Err(err) => assert_fails(err, 4)
}