Skip to main content

creusot_std/ghost/
resource.rs

1//! Resource algebras
2//!
3//! See [`Resource`].
4
5mod auth;
6pub use auth::{Authority, Fragment};
7
8// We use a nested module that we re-export, to make sure that the definitions
9// are opaque to the fmap_view module
10mod 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    /// A ghost wrapper around a [resource algebra](RA).
23    ///
24    /// This structure is meant to be manipulated in [`ghost`] code.
25    ///
26    /// The usual usage is this:
27    /// - [Create](Self::alloc) some ghost resource
28    /// - [Split](Self::split) it into multiple parts.
29    ///   This may be used to duplicate the resource if we have `self.op(self) == self`.
30    /// - [Join](Self::join) these parts later. By exploiting validity of the combination, this allows
31    ///   one to learn information about one part from the other.
32    ///
33    /// # Example
34    ///
35    /// ```rust
36    /// use creusot_std::{ghost::resource::Resource, logic::ra::agree::Ag, prelude::*};
37    /// let mut res: Ghost<Resource<Ag<Int>>> = Resource::alloc(snapshot!(Ag(1)));
38    ///
39    /// ghost! {
40    ///     let part = res.split_off(snapshot!(Ag(1)), snapshot!(Ag(1)));
41    ///     // Pass `part` around, forget what it contained...
42    ///     let _ = res.join_shared(&part);
43    ///     // And now we remember: the only way the above worked is if `part` contained `1`!
44    ///     proof_assert!(part@ == Ag(1));
45    /// };
46    /// ```
47    #[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        /// Get the id for this resource.
63        ///
64        /// This prevents mixing resources of different origins.
65        #[logic(opaque)]
66        pub fn id(self) -> Id {
67            dead
68        }
69
70        /// Get the id for this resource.
71        ///
72        /// This is the same as [`Self::id`], but for ghost code.
73        #[trusted]
74        #[check(ghost)]
75        #[ensures(result == self.id())]
76        pub fn id_ghost(&self) -> Id {
77            panic!("ghost code only")
78        }
79
80        /// Get the RA element contained in this resource.
81        #[logic(opaque)]
82        pub fn val(self) -> R {
83            dead
84        }
85
86        /// Create a new resource
87        ///
88        /// # Corresponding reasoning
89        ///
90        /// `⊢ |=> ∃γ, Own(value, γ)`
91        #[trusted]
92        #[check(ghost)]
93        #[ensures(result@ == *r)]
94        pub fn alloc(r: Snapshot<R>) -> Ghost<Self> {
95            Ghost::conjure()
96        }
97
98        /// Create a unit resource for a given identifier
99        #[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        /// Dummy resource.
110        /// This funciton is unsound, because there does not necessarilly exist
111        /// a value of the RA that does not carry any ownership.
112        ///
113        /// However, thanks to this, we can prove some of the functions bellow, that would be
114        /// otherwise axiomatized. These proofs are morally trusted, but being to prove them is
115        /// a good measure against stupid mistakes in their specifications.
116        #[trusted]
117        #[check(ghost)]
118        fn dummy() -> Self {
119            panic!("ghost code only")
120        }
121
122        /// Duplicate the duplicable core of a resource
123        #[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        /// Split a resource into two parts, described by `a` and `b`.
133        ///
134        /// See also [`Self::split_mut`] and [`Self::split_off`].
135        ///
136        /// # Corresponding reasoning
137        ///
138        /// `⌜a = b ⋅ c⌝ ∧ Own(a, γ) ⊢ Own(b, γ) ∗ Own(c, γ)`
139        #[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        /// Split a resource into two, and join it again once the mutable borrows are dropped.
150        #[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        /// Remove `r` from `self` and return it, leaving `s` in `self`.
163        #[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        /// Join two owned resources together.
176        ///
177        /// See also [`Self::join_in`] and [`Self::join_shared`].
178        ///
179        /// # Corresponding reasoning
180        ///
181        /// `⌜c = a ⋅ b⌝ ∗ Own(a, γ) ∗ Own(b, γ) ⊢ Own(c, γ)`
182        #[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        /// Same as [`Self::join`], but put the result into `self`.
192        #[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        /// Join two shared resources together.
203        ///
204        /// # Corresponding reasoning
205        ///
206        /// `⌜a ≼ c⌝ ∧ ⌜b ≼ c⌝ ∧ Own(a, γ) ∧ Own(b, γ) ⊢ Own(c, γ)`
207        #[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        /// Transforms `self` into `target`, given that `target` is included in `self`.
217        #[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        /// Validate the composition of `self` and `other`.
227        #[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        /// This private function axiomatizes updates as they are formalized in Iris.
235        #[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        /// Perform an update.
247        ///
248        /// # Corresponding reasoning
249        ///
250        /// `⌜a ⇝ B⌝ ∧ Own(a, γ) ⊢ ∃b∈B, Own(b, γ)`
251        #[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::*;