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