vstd/resource/pcm.rs
1use super::super::prelude::*;
2use super::Loc;
3use super::algebra::ResourceAlgebra;
4use super::relations::*;
5
6#[cfg(verus_keep_ghost)]
7use super::super::modes::tracked_swap;
8
9verus! {
10
11broadcast use super::super::set::group_set_axioms;
12
13/// Interface for PCM / Resource Algebra ghost state.
14///
15/// RA-based ghost state is a well-established theory that is especially
16/// useful for verifying concurrent code. An introduction to the concept
17/// can be found in
18/// [Iris: Monoids and Invariants as an Orthogonal Basis for Concurrent Reasoning](https://iris-project.org/pdfs/2015-popl-iris1-final.pdf)
19/// or
20/// [Iris from the ground up](https://people.mpi-sws.org/~dreyer/papers/iris-ground-up/paper.pdf).
21///
22/// To embed the concept into Verus, we:
23/// * Use a trait, [`PCM`], to embed the well-formedness laws of a resource algebra
24/// * use a "tracked ghost" object, `Resource<P>` (this page) to represent ownership of a resource.
25///
26/// Most operations are fairly standard, just "translated" from the usual CSL presentation into Verus.
27///
28/// * [`alloc`](Self::alloc) to allocate a resource.
29/// * [`join`](Self::join) to combine two resources via `P::op`, and [`split`](Self::split), its inverse.
30/// * [`validate`](Self::validate) to assert the validity of any held resource.
31/// * [`update`](Self::update) or [`update_nondeterministic`](Self::update_nondeterministic) to perform a frame-preserving update.
32///
33/// The interface also includes a nontrivial extension for working with _shared references_ to resources.
34/// Shared resources do not compose in a "separating" way via `P::op`, but rather, in a "potentially overlapping" way ([`join_shared`](Self::join_shared)). Shared resources can also be used to "help" perform frame-preserving updates, as long as they themselves do not change ([`update_with_shared`](Self::update_with_shared)).
35///
36/// ### Examples
37///
38/// See:
39/// * Any of the examples in [this directory](https://github.com/verus-lang/verus/tree/main/examples/pcm)
40/// * The source code for the [fractional resource library](super::frac::FracGhost)
41///
42/// ### See also
43///
44/// The ["storage protocol"](super::storage_protocol::StorageResource) formalism
45/// is an even more significant
46/// extension with additional capabilities for interacting with shared resources.
47///
48/// [VerusSync](https://verus-lang.github.io/verus/state_machines/intro.html) provides a higher-level
49/// "swiss army knife" for building useful ghost resources.
50// Resource must be external_body, otherwise it could be constructed directly
51#[verifier::external_body]
52#[verifier::accept_recursive_types(P)]
53pub tracked struct Resource<P: PCM> {
54 p: core::marker::PhantomData<P>,
55}
56
57/// A Partial Commutative Monoid is a special type of [`ResourceAlgebra`], where all elements have
58/// the same core (which belongs in the carrier), the unit. For this reason, they are also called
59/// unitary resource algebras[^note].
60///
61/// [^note]: This is slightly misleading. PCMs are partial in the sense that the operation is not
62/// defined for certain inputs. Because Verus does not support partial functions, we model that
63/// partiality with the validity predicate, which does make it a unitary resource algebra.
64pub trait PCM: ResourceAlgebra {
65 /// The unit of the monoid, i.e., the carrier value that composed with any other carrier value
66 /// yields the identity function
67 spec fn unit() -> Self;
68
69 /// The core of an element `a` is, by definition, some other element `x`
70 /// such that `a · x = a`
71 proof fn op_unit(self)
72 ensures
73 Self::op(self, Self::unit()) == self,
74 ;
75
76 /// The unit is always a valid element
77 proof fn unit_valid()
78 ensures
79 Self::unit().valid(),
80 ;
81}
82
83impl<P: PCM> Resource<P> {
84 /// The underlying value of the resource
85 pub uninterp spec fn value(self) -> P;
86
87 /// The location of the resource
88 pub uninterp spec fn loc(self) -> Loc;
89
90 // AXIOMS
91 /// Allocate a new resource at a fresh location
92 // GHOST-ALLOC rule
93 pub axiom fn alloc(value: P) -> (tracked out: Self)
94 requires
95 value.valid(),
96 ensures
97 out.value() == value,
98 ;
99
100 /// We can join together two resources at the same location, where we obtain the combination of
101 /// the two
102 // reverse implication in the GHOST-OP rule
103 pub axiom fn join(tracked self, tracked other: Self) -> (tracked out: Self)
104 requires
105 self.loc() == other.loc(),
106 ensures
107 out.loc() == self.loc(),
108 out.value() == P::op(self.value(), other.value()),
109 ;
110
111 /// If a resource holds the result of a composition, we can split it into the components
112 // implication in the GHOST-OP rule
113 pub axiom fn split(tracked self, left: P, right: P) -> (tracked out: (Self, Self))
114 requires
115 self.value() == P::op(left, right),
116 ensures
117 out.0.loc() == self.loc(),
118 out.1.loc() == self.loc(),
119 out.0.value() == left,
120 out.1.value() == right,
121 ;
122
123 /// Create a unit at a particular location
124 pub axiom fn create_unit(loc: Loc) -> (tracked out: Self)
125 ensures
126 out.value() == P::unit(),
127 out.loc() == loc,
128 ;
129
130 /// Whenever we have a resource, that resource is valid. This intuitively (and inductively)
131 /// holds because:
132 /// 1. We only create valid resources (either via [`alloc`] or
133 /// [`create_unit`](Self::create_unit)).
134 /// 2. We can only update the value of a resource via a [`frame_preserving_update`] (see
135 /// [`update`](Self::update) for more details. Because of the requirement of that
136 /// updates are frame preserving this means that `join`s remain valid.
137 // GHOST-VALID rule
138 pub axiom fn validate(tracked &self)
139 ensures
140 self.value().valid(),
141 ;
142
143 /// This is a more general version of [`update`](Self::update).
144 // GHOST-UPDATE rule
145 pub axiom fn update_nondeterministic(tracked self, new_values: Set<P>) -> (tracked out: Self)
146 requires
147 frame_preserving_update_nondeterministic(self.value(), new_values),
148 ensures
149 out.loc() == self.loc(),
150 new_values.contains(out.value()),
151 ;
152
153 // VERIFIED
154 /// Update a resource to a new value. This can only be done if the update is frame preserving
155 /// (i.e., any value that could be composed with the original value can still be composed with
156 /// the new value).
157 ///
158 /// See [`frame_preserving_update`] for more information.
159 // variant of the GHOST-UPDATE rule
160 pub proof fn update(tracked self, new_value: P) -> (tracked out: Self)
161 requires
162 frame_preserving_update(self.value(), new_value),
163 ensures
164 out.loc() == self.loc(),
165 out.value() == new_value,
166 {
167 let new_values = set![new_value];
168 assert(new_values.contains(new_value));
169 self.update_nondeterministic(new_values)
170 }
171
172 /// Extracts the resource from `r`, leaving `r` unspecified and returning
173 /// a new resource holding the previous value of `r`.
174 pub proof fn extract(tracked &mut self) -> (tracked other: Self)
175 ensures
176 other.loc() == old(self).loc(),
177 other.value() == old(self).value(),
178 {
179 self.validate();
180 let tracked mut other = Self::alloc(self.value());
181 tracked_swap(self, &mut other);
182 other
183 }
184
185 // Operations with shared references
186 // AXIOMS on shared references
187 /// This is useful when you have two (or more) shared resources and want to learn
188 /// that they agree, as you can combine this validate, e.g., `x.join_shared(y).validate()`.
189 pub axiom fn join_shared<'a>(tracked &'a self, tracked other: &'a Self) -> (tracked out:
190 &'a Self)
191 requires
192 self.loc() == other.loc(),
193 ensures
194 out.loc() == self.loc(),
195 incl(self.value(), out.value()),
196 incl(other.value(), out.value()),
197 ;
198
199 /// Extract a shared reference to a preceding element in the extension order from a shared reference.
200 pub axiom fn weaken<'a>(tracked &'a self, target: P) -> (tracked out: &'a Self)
201 requires
202 incl(target, self.value()),
203 ensures
204 out.loc() == self.loc(),
205 out.value() == target,
206 ;
207
208 /// Validate two items at once. If we have two resources at the same location then its
209 /// composition is valid.
210 pub axiom fn validate_2(tracked &mut self, tracked other: &Self)
211 requires
212 old(self).loc() == other.loc(),
213 ensures
214 *self == *old(self),
215 P::op(self.value(), other.value()).valid(),
216 ;
217
218 /// We can do a similar update to [`update_with_shared`](Self::update_with_shared) for non-deterministic updates
219 pub axiom fn update_nondeterministic_with_shared(
220 tracked self,
221 tracked other: &Self,
222 new_values: Set<P>,
223 ) -> (tracked out: Self)
224 requires
225 self.loc() == other.loc(),
226 frame_preserving_update_nondeterministic(
227 P::op(self.value(), other.value()),
228 set_op(new_values, other.value()),
229 ),
230 ensures
231 out.loc() == self.loc(),
232 new_values.contains(out.value()),
233 ;
234
235 // VERIFIED facts about shared references
236 /// We can do a similar update to [`update_with_shared`](Self::update_with_shared) for non-deterministic updates
237 pub proof fn join_shared_to_target<'a>(
238 tracked &'a self,
239 tracked other: &'a Self,
240 target: P,
241 ) -> (tracked out: &'a Self)
242 requires
243 self.loc() == other.loc(),
244 conjunct_shared(self.value(), other.value(), target),
245 ensures
246 out.loc() == self.loc(),
247 out.value() == target,
248 {
249 let tracked j = self.join_shared(other);
250 j.validate();
251 j.weaken(target)
252 }
253
254 /// If `x · y --> x · z` is a frame-perserving update, and we have a shared reference to `x`,
255 /// we can update the `y` resource to `z`.
256 pub proof fn update_with_shared(
257 tracked self,
258 tracked other: &Self,
259 new_value: P,
260 ) -> (tracked out: Self)
261 requires
262 self.loc() == other.loc(),
263 frame_preserving_update(
264 P::op(self.value(), other.value()),
265 P::op(new_value, other.value()),
266 ),
267 ensures
268 out.loc() == self.loc(),
269 out.value() == new_value,
270 {
271 let new_values = set![new_value];
272 let so = set_op(new_values, other.value());
273 assert(so.contains(P::op(new_value, other.value())));
274 self.update_nondeterministic_with_shared(other, new_values)
275 }
276
277 /// If `x --> x · y` is a frame-perserving update, and we have a shared reference to `x`,
278 /// we can create a resource to y
279 pub proof fn duplicate_previous(tracked &self, value: P) -> (tracked out: Self)
280 requires
281 frame_preserving_update(self.value(), P::op(value, self.value())),
282 ensures
283 out.loc() == self.loc(),
284 out.value() == value,
285 {
286 let tracked mut unit = Self::create_unit(self.loc());
287 self.value().op_unit();
288 P::commutative(self.value(), unit.value());
289 unit.update_with_shared(self, value)
290 }
291}
292
293} // verus!