Skip to content

Commit 6571723

Browse files
authored
resource: update examples to make use of new Resource Algebra interface (#2396)
1 parent 51b9864 commit 6571723

7 files changed

Lines changed: 308 additions & 196 deletions

File tree

examples/pcm/agreement.rs

Lines changed: 0 additions & 175 deletions
This file was deleted.

examples/resource/agreement.rs

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
//! This file implements agreement on a constant value using a custom
2+
//! resource algebra.
3+
//!
4+
//! An agreement resource constitutes knowledge of a constant value.
5+
//! To create an instance of a constant value of type `T`, use
6+
//! `AgreementResource::<T>::alloc()` as in the following example:
7+
//!
8+
//! ```
9+
//! let tracked r1 = AgreementResource::<int>::alloc(72);
10+
//! assert(r1@ == 72);
11+
//! ```
12+
//!
13+
//! Knowledge of a constant value can be duplicated with `duplicate`,
14+
//! which creates another agreement resource with the same constant
15+
//! value and the same ID. Here's an example:
16+
//!
17+
//! ```
18+
//! let tracked r2 = r1.duplicate();
19+
//! assert(r2.loc() == r1.loc());
20+
//! assert(r2@ == r1@);
21+
//! ```
22+
//!
23+
//! Any two agreement resources with the same `loc()` are guaranteed to
24+
//! have equal values. You can establish this by calling
25+
//! `lemma_agreement`, as in the following example:
26+
//!
27+
//! ```
28+
//! assert(r2.loc() == r1.loc());
29+
//! proof { r1.lemma_agreement(&mut r2); }
30+
//! assert(r2@ == r1@);
31+
//! ```
32+
#![allow(unused_imports)]
33+
use std::result::*;
34+
use verus_builtin::*;
35+
use verus_builtin_macros::*;
36+
use vstd::prelude::*;
37+
use vstd::resource;
38+
use vstd::resource::agree::lemma_agree;
39+
use vstd::resource::agree::AgreementRA;
40+
use vstd::resource::algebra::Resource;
41+
use vstd::resource::algebra::ResourceAlgebra;
42+
use vstd::resource::Loc;
43+
44+
verus! {
45+
46+
pub struct AgreementResource<T> {
47+
r: Resource<AgreementRA<T>>,
48+
}
49+
50+
impl<T> AgreementResource<T> {
51+
pub closed spec fn loc(self) -> Loc {
52+
self.r.loc()
53+
}
54+
55+
pub closed spec fn view(self) -> T
56+
{
57+
self.r.value()@
58+
}
59+
60+
pub proof fn alloc(c: T) -> (tracked result: AgreementResource<T>)
61+
ensures
62+
result@ == c,
63+
{
64+
let carrier = AgreementRA::Agree(c);
65+
let tracked r = Resource::alloc(carrier);
66+
AgreementResource::<T> { r }
67+
}
68+
69+
pub proof fn duplicate(tracked self: &AgreementResource<T>) -> (tracked result: AgreementResource<T>)
70+
ensures
71+
result.loc() == self.loc(),
72+
self@ == result@,
73+
{
74+
let tracked r = self.r.duplicate_previous(self.r.value());
75+
AgreementResource::<T> { r }
76+
}
77+
78+
pub proof fn lemma_agreement(
79+
tracked self: &AgreementResource<T>,
80+
tracked other: &AgreementResource<T>,
81+
)
82+
requires
83+
self.loc() == other.loc(),
84+
ensures
85+
self@ == other@,
86+
{
87+
lemma_agree(&self.r, &other.r);
88+
}
89+
}
90+
91+
pub fn main() {
92+
let tracked r1 = AgreementResource::<int>::alloc(72);
93+
assert(r1@ == 72);
94+
let tracked r2 = r1.duplicate();
95+
assert(r2@ == r1@);
96+
proof {
97+
r1.lemma_agreement(&mut r2);
98+
}
99+
}
100+
101+
} // verus!
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -238,7 +238,7 @@ impl<T> LogResource<T> {
238238
ensures
239239
final(self)@ is HalfAuthority,
240240
final(self).id() == old(self).id(),
241-
final(other).id() == old(self).id(),
241+
final(other).id() == old(other).id(),
242242
final(self)@.log() == old(self)@.log() + seq![v],
243243
final(other)@ == final(self)@,
244244
{
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -285,8 +285,8 @@ impl MonotonicCounterResource {
285285
ensures
286286
old(self)@ == old(other)@,
287287
final(self).id() == old(self).id(),
288-
final(other).id() == old(self).id(),
289-
final(other)@ == final(self)@,
288+
final(other).id() == old(other).id(),
289+
final(self)@ == final(other)@,
290290
final(self)@ == (MonotonicCounterResourceValue::HalfRightToAdvance {
291291
value: old(self)@->HalfRightToAdvance_value + 1,
292292
}),

0 commit comments

Comments
 (0)