creusot_std/ghost/
resource.rs1mod auth;
6
7pub use auth::{Authority, Fragment};
8
9mod m {
12 #[cfg(creusot)]
13 use crate::{
14 ghost::Objective,
15 logic::{any, such_that},
16 };
17 use crate::{
18 logic::{
19 Id, Set,
20 ra::{RA, UnitRA, update::Update},
21 },
22 prelude::*,
23 };
24 use core::marker::PhantomData;
25
26 #[opaque]
52 pub struct Resource<R>(PhantomData<R>);
53
54 #[trusted]
55 unsafe impl<R> Send for Resource<R> {}
56 #[trusted]
57 unsafe impl<R> Sync for Resource<R> {}
58
59 #[cfg(creusot)]
60 #[trusted]
61 impl<R> Objective for Resource<R> {}
62
63 impl<R: RA> View for Resource<R> {
64 type ViewTy = R;
65 #[logic(open, inline)]
66 fn view(self) -> R {
67 self.val()
68 }
69 }
70
71 #[allow(unused_variables)]
72 impl<R: RA> Resource<R> {
73 #[logic(opaque)]
77 pub fn id_logic(self) -> Id {
78 dead
79 }
80
81 #[trusted]
85 #[check(ghost)]
86 #[logic_alias(self.id_logic())]
87 pub fn id(&self) -> Id {
88 panic!("ghost code only")
89 }
90
91 #[logic(opaque)]
93 pub fn val(self) -> R {
94 dead
95 }
96
97 #[trusted]
103 #[check(ghost)]
104 #[ensures(result@ == *r)]
105 pub fn alloc(r: Snapshot<R>) -> Ghost<Self> {
106 Ghost::conjure()
107 }
108
109 #[trusted]
111 #[check(ghost)]
112 #[ensures(result@ == R::unit() && result.id() == id)]
113 pub fn new_unit(id: Id) -> Self
114 where
115 R: UnitRA,
116 {
117 panic!("ghost code only")
118 }
119
120 #[trusted]
128 #[check(ghost)]
129 fn dummy() -> Self {
130 panic!("ghost code only")
131 }
132
133 #[trusted]
135 #[requires(self@.core() != None)]
136 #[ensures(result.id() == self.id())]
137 #[ensures(Some(result@) == self@.core())]
138 #[check(ghost)]
139 pub fn core(&self) -> Self {
140 panic!("ghost code only")
141 }
142
143 #[trusted]
151 #[check(ghost)]
152 #[requires(R::incl_eq_op(*a, *b, self@))]
153 #[ensures(result.0.id() == self.id() && result.1.id() == self.id())]
154 #[ensures(result.0@ == *a)]
155 #[ensures(result.1@ == *b)]
156 pub fn split(self, a: Snapshot<R>, b: Snapshot<R>) -> (Self, Self) {
157 panic!("ghost code only")
158 }
159
160 #[trusted]
162 #[check(ghost)]
163 #[requires(R::incl_eq_op(*a, *b, self@))]
164 #[ensures(result.0.id() == self.id() && result.1.id() == self.id())]
165 #[ensures(result.0@ == *a && result.1@ == *b)]
166 #[ensures((^result.0).id() == self.id() && (^result.1).id() == self.id() ==>
167 (^self).id() == self.id() &&
168 Some((^self)@) == (^result.0)@.op((^result.1)@))]
169 pub fn split_mut(&mut self, a: Snapshot<R>, b: Snapshot<R>) -> (&mut Self, &mut Self) {
170 panic!("ghost code only")
171 }
172
173 #[check(ghost)]
175 #[requires(R::incl_eq_op(*r, *s, self@))]
176 #[ensures((^self).id() == self.id() && result.id() == self.id())]
177 #[ensures((^self)@ == *s)]
178 #[ensures(result@ == *r)]
179 pub fn split_off(&mut self, r: Snapshot<R>, s: Snapshot<R>) -> Self {
180 let this = core::mem::replace(self, Self::dummy());
181 let (r, this) = this.split(r, s);
182 let _ = core::mem::replace(self, this);
183 r
184 }
185
186 #[trusted]
194 #[check(ghost)]
195 #[requires(self.id() == other.id())]
196 #[ensures(result.id() == self.id())]
197 #[ensures(Some(result@) == self@.op(other@))]
198 pub fn join(self, other: Self) -> Self {
199 panic!("ghost code only")
200 }
201
202 #[check(ghost)]
204 #[requires(self.id() == other.id())]
205 #[ensures((^self).id() == self.id())]
206 #[ensures(Some((^self)@) == self@.op(other@))]
207 pub fn join_in(&mut self, other: Self) {
208 let this = core::mem::replace(self, Self::dummy());
209 let this = this.join(other);
210 let _ = core::mem::replace(self, this);
211 }
212
213 #[trusted]
219 #[check(ghost)]
220 #[requires(self.id() == other.id())]
221 #[ensures(result.id() == self.id())]
222 #[ensures(self@.incl_eq(result@) && other@.incl_eq(result@))]
223 pub fn join_shared<'a>(&'a self, other: &'a Self) -> &'a Self {
224 panic!("ghost code only")
225 }
226
227 #[check(ghost)]
229 #[requires(target.incl(self@))]
230 #[ensures((^self).id() == self.id())]
231 #[ensures((^self)@ == *target)]
232 pub fn weaken(&mut self, target: Snapshot<R>) {
233 let f = snapshot! {self@.factor(*target)};
234 self.split_off(f, target);
235 }
236
237 #[trusted]
239 #[check(ghost)]
240 #[requires(self.id() == other.id())]
241 #[ensures(^self == *self)]
242 #[ensures(self@.op(other@) != None)]
243 pub fn valid_op_lemma(&mut self, other: &Self) {}
244
245 #[trusted]
247 #[check(ghost)]
248 #[requires(forall<f: Option<R>> Some(self@).op(f) != None ==>
249 exists<x: R> target_s.contains(x) && Some(x).op(f) != None)]
250 #[ensures((^self).id() == self.id())]
251 #[ensures(target_s.contains(*result))]
252 #[ensures((^self)@ == *result)]
253 fn update_raw(&mut self, target_s: Snapshot<Set<R>>) -> Snapshot<R> {
254 panic!("ghost code only")
255 }
256
257 #[check(ghost)]
269 #[requires(upd.premise(self@))]
270 #[ensures((^self).id() == self.id())]
271 #[ensures((^self)@ == upd.update(self@, *result))]
272 pub fn update<U: Update<R>>(&mut self, upd: U) -> Snapshot<U::Choice> {
273 let v = snapshot!(self@);
274 let target_s = snapshot!(Set::from_predicate(|r| exists<ch> upd.update(*v, ch) == r));
275 proof_assert!(target_s.contains(upd.update(*v, any())));
276 proof_assert!(
277 forall<f: R> v.op(f) != None ==>
278 upd.update(*v, upd.frame_preserving(*v, f)).op(f) != None
279 );
280 let _ = snapshot!(U::frame_preserving);
281 let r = self.update_raw(target_s);
282 snapshot!(such_that(|ch| upd.update(*v, ch) == *r))
283 }
284 }
285
286 impl<R: UnitRA> Resource<R> {
287 #[check(ghost)]
288 #[ensures((^self).id() == self.id() && result.id() == self.id())]
289 #[ensures((^self)@ == UnitRA::unit())]
290 #[ensures(result@ == self@)]
291 pub fn take(&mut self) -> Self {
292 let r = snapshot!(self@);
293 self.split_off(r, snapshot!(UnitRA::unit()))
294 }
295 }
296}
297
298pub use m::*;