creusot_std/ghost/
resource.rs1mod auth;
6pub use auth::{Authority, Fragment};
7
8mod m {
11 #[cfg(creusot)]
12 use crate::logic::such_that;
13 use crate::{
14 logic::{
15 Id, Set,
16 ra::{RA, UnitRA, update::Update},
17 },
18 prelude::*,
19 };
20 use core::marker::PhantomData;
21
22 #[opaque]
48 pub struct Resource<R>(PhantomData<R>);
49
50 unsafe impl<R> Send for Resource<R> {}
51
52 impl<R: RA> View for Resource<R> {
53 type ViewTy = R;
54 #[logic(open, inline)]
55 fn view(self) -> R {
56 self.val()
57 }
58 }
59
60 #[allow(unused_variables)]
61 impl<R: RA> Resource<R> {
62 #[logic(opaque)]
66 pub fn id(self) -> Id {
67 dead
68 }
69
70 #[trusted]
74 #[check(ghost)]
75 #[ensures(result == self.id())]
76 pub fn id_ghost(&self) -> Id {
77 panic!("ghost code only")
78 }
79
80 #[logic(opaque)]
82 pub fn val(self) -> R {
83 dead
84 }
85
86 #[trusted]
92 #[check(ghost)]
93 #[ensures(result@ == *r)]
94 pub fn alloc(r: Snapshot<R>) -> Ghost<Self> {
95 Ghost::conjure()
96 }
97
98 #[trusted]
100 #[check(ghost)]
101 #[ensures(result@ == R::unit() && result.id() == id)]
102 pub fn new_unit(id: Id) -> Self
103 where
104 R: UnitRA,
105 {
106 panic!("ghost code only")
107 }
108
109 #[trusted]
117 #[check(ghost)]
118 fn dummy() -> Self {
119 panic!("ghost code only")
120 }
121
122 #[trusted]
124 #[requires(self@.core() != None)]
125 #[ensures(result.id() == self.id())]
126 #[ensures(Some(result@) == self@.core())]
127 #[check(ghost)]
128 pub fn core(&self) -> Self {
129 panic!("ghost code only")
130 }
131
132 #[trusted]
140 #[check(ghost)]
141 #[requires(R::incl_eq_op(*a, *b, self@))]
142 #[ensures(result.0.id() == self.id() && result.1.id() == self.id())]
143 #[ensures(result.0@ == *a)]
144 #[ensures(result.1@ == *b)]
145 pub fn split(self, a: Snapshot<R>, b: Snapshot<R>) -> (Self, Self) {
146 panic!("ghost code only")
147 }
148
149 #[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 && result.1@ == *b)]
155 #[ensures((^result.0).id() == self.id() && (^result.1).id() == self.id() ==>
156 (^self).id() == self.id() &&
157 Some((^self)@) == (^result.0)@.op((^result.1)@))]
158 pub fn split_mut(&mut self, a: Snapshot<R>, b: Snapshot<R>) -> (&mut Self, &mut Self) {
159 panic!("ghost code only")
160 }
161
162 #[check(ghost)]
164 #[requires(R::incl_eq_op(*r, *s, self@))]
165 #[ensures((^self).id() == self.id() && result.id() == self.id())]
166 #[ensures((^self)@ == *s)]
167 #[ensures(result@ == *r)]
168 pub fn split_off(&mut self, r: Snapshot<R>, s: Snapshot<R>) -> Self {
169 let this = core::mem::replace(self, Self::dummy());
170 let (r, this) = this.split(r, s);
171 let _ = core::mem::replace(self, this);
172 r
173 }
174
175 #[trusted]
183 #[check(ghost)]
184 #[requires(self.id() == other.id())]
185 #[ensures(result.id() == self.id())]
186 #[ensures(Some(result@) == self@.op(other@))]
187 pub fn join(self, other: Self) -> Self {
188 panic!("ghost code only")
189 }
190
191 #[check(ghost)]
193 #[requires(self.id() == other.id())]
194 #[ensures((^self).id() == self.id())]
195 #[ensures(Some((^self)@) == self@.op(other@))]
196 pub fn join_in(&mut self, other: Self) {
197 let this = core::mem::replace(self, Self::dummy());
198 let this = this.join(other);
199 let _ = core::mem::replace(self, this);
200 }
201
202 #[trusted]
208 #[check(ghost)]
209 #[requires(self.id() == other.id())]
210 #[ensures(result.id() == self.id())]
211 #[ensures(self@.incl_eq(result@) && other@.incl_eq(result@))]
212 pub fn join_shared<'a>(&'a self, other: &'a Self) -> &'a Self {
213 panic!("ghost code only")
214 }
215
216 #[check(ghost)]
218 #[requires(target.incl(self@))]
219 #[ensures((^self).id() == self.id())]
220 #[ensures((^self)@ == *target)]
221 pub fn weaken(&mut self, target: Snapshot<R>) {
222 let f = snapshot! {self@.factor(*target).unwrap_logic()};
223 self.split_off(f, target);
224 }
225
226 #[trusted]
228 #[check(ghost)]
229 #[requires(self.id() == other.id())]
230 #[ensures(^self == *self)]
231 #[ensures(self@.op(other@) != None)]
232 pub fn valid_op_lemma(&mut self, other: &Self) {}
233
234 #[trusted]
236 #[check(ghost)]
237 #[requires(forall<f: Option<R>> Some(self@).op(f) != None ==>
238 exists<x: R> target_s.contains(x) && Some(x).op(f) != None)]
239 #[ensures((^self).id() == self.id())]
240 #[ensures(target_s.contains(*result))]
241 #[ensures((^self)@ == *result)]
242 fn update_raw(&mut self, target_s: Snapshot<Set<R>>) -> Snapshot<R> {
243 panic!("ghost code only")
244 }
245
246 #[check(ghost)]
252 #[requires(upd.premise(self@))]
253 #[ensures((^self).id() == self.id())]
254 #[ensures((^self)@ == upd.update(self@, *result))]
255 pub fn update<U: Update<R>>(&mut self, upd: U) -> Snapshot<U::Choice> {
256 let v = snapshot!(self@);
257 let target_s = snapshot!(Set::from_predicate(|r| exists<ch> upd.update(*v, ch) == r));
258 proof_assert!(target_s.contains(upd.update(*v, such_that(|_| true))));
259 proof_assert!(
260 forall<f: R> v.op(f) != None ==>
261 exists<ch: U::Choice> upd.update(*v, ch).op(f) != None
262 );
263 let _ = snapshot!(U::frame_preserving);
264 let r = self.update_raw(target_s);
265 snapshot!(such_that(|ch| upd.update(*v, ch) == *r))
266 }
267 }
268
269 impl<R: UnitRA> Resource<R> {
270 #[check(ghost)]
271 #[ensures((^self).id() == self.id() && result.id() == self.id())]
272 #[ensures((^self)@ == UnitRA::unit())]
273 #[ensures(result@ == self@)]
274 pub fn take(&mut self) -> Self {
275 let r = snapshot!(self@);
276 self.split_off(r, snapshot!(UnitRA::unit()))
277 }
278 }
279}
280
281pub use m::*;