Skip to content

Commit e8d770e

Browse files
committed
resource: rework agreement example to use combinators
1 parent e04e90c commit e8d770e

1 file changed

Lines changed: 17 additions & 91 deletions

File tree

examples/resource/agreement.rs

Lines changed: 17 additions & 91 deletions
Original file line numberDiff line numberDiff line change
@@ -16,16 +16,16 @@
1616
//!
1717
//! ```
1818
//! let tracked r2 = r1.duplicate();
19-
//! assert(r2.id() == r1.id());
19+
//! assert(r2.loc() == r1.loc());
2020
//! assert(r2@ == r1@);
2121
//! ```
2222
//!
23-
//! Any two agreement resources with the same `id()` are guaranteed to
23+
//! Any two agreement resources with the same `loc()` are guaranteed to
2424
//! have equal values. You can establish this by calling
2525
//! `lemma_agreement`, as in the following example:
2626
//!
2727
//! ```
28-
//! assert(r2.id() == r1.id());
28+
//! assert(r2.loc() == r1.loc());
2929
//! proof { r1.lemma_agreement(&mut r2); }
3030
//! assert(r2@ == r1@);
3131
//! ```
@@ -35,130 +35,56 @@ use verus_builtin::*;
3535
use verus_builtin_macros::*;
3636
use vstd::prelude::*;
3737
use vstd::resource;
38+
use vstd::resource::agree::lemma_agree;
39+
use vstd::resource::agree::AgreementRA;
40+
use vstd::resource::algebra::Resource;
3841
use vstd::resource::algebra::ResourceAlgebra;
39-
use vstd::resource::pcm::Resource;
40-
use vstd::resource::pcm::PCM;
4142
use vstd::resource::Loc;
4243

4344
verus! {
4445

45-
pub enum AgreementResourceValue<T> {
46-
Empty,
47-
Chosen { c: T },
48-
Invalid,
49-
}
50-
51-
impl<T> AgreementResourceValue<T> {
52-
pub open spec fn new(c: T) -> Self {
53-
AgreementResourceValue::<T>::Chosen { c }
54-
}
55-
}
56-
57-
impl<T> ResourceAlgebra for AgreementResourceValue<T> {
58-
open spec fn valid(self) -> bool {
59-
!(self is Invalid)
60-
}
61-
62-
open spec fn op(a: Self, b: Self) -> Self {
63-
match (a, b) {
64-
(AgreementResourceValue::<T>::Empty, _) => b,
65-
(_, AgreementResourceValue::<T>::Empty) => a,
66-
(AgreementResourceValue::<T>::Invalid, _) => AgreementResourceValue::<T>::Invalid { },
67-
(_, AgreementResourceValue::<T>::Invalid) => AgreementResourceValue::<T>::Invalid { },
68-
(
69-
AgreementResourceValue::<T>::Chosen { c: c1 },
70-
AgreementResourceValue::<T>::Chosen { c: c2 },
71-
) => if c1 == c2 {
72-
a
73-
} else {
74-
AgreementResourceValue::<T>::Invalid { }
75-
},
76-
}
77-
}
78-
79-
proof fn valid_op(a: Self, b: Self) {
80-
}
81-
82-
proof fn commutative(a: Self, b: Self) {
83-
}
84-
85-
proof fn associative(a: Self, b: Self, c: Self) {
86-
}
87-
}
88-
89-
90-
impl<T> PCM for AgreementResourceValue<T> {
91-
open spec fn unit() -> Self {
92-
AgreementResourceValue::<T>::Empty { }
93-
}
94-
95-
proof fn op_unit(self) {
96-
}
97-
98-
proof fn unit_valid() {
99-
}
100-
}
101-
10246
pub struct AgreementResource<T> {
103-
r: Resource<AgreementResourceValue<T>>,
47+
r: Resource<AgreementRA<T>>,
10448
}
10549

10650
impl<T> AgreementResource<T> {
107-
pub closed spec fn inv(self) -> bool {
108-
self.r.value() is Chosen
109-
}
110-
111-
pub closed spec fn id(self) -> Loc {
51+
pub closed spec fn loc(self) -> Loc {
11252
self.r.loc()
11353
}
11454

11555
pub closed spec fn view(self) -> T
116-
recommends
117-
self.inv(),
11856
{
119-
self.r.value()->c
57+
self.r.value()@
12058
}
12159

12260
pub proof fn alloc(c: T) -> (tracked result: AgreementResource<T>)
12361
ensures
124-
result.inv(),
12562
result@ == c,
12663
{
127-
let r_value = AgreementResourceValue::<T>::new(c);
128-
let tracked r = Resource::<AgreementResourceValue::<T>>::alloc(r_value);
64+
let carrier = AgreementRA::Agree(c);
65+
let tracked r = Resource::alloc(carrier);
12966
AgreementResource::<T> { r }
13067
}
13168

132-
pub proof fn duplicate(tracked self: &mut AgreementResource<T>) -> (tracked result:
133-
AgreementResource<T>)
134-
requires
135-
old(self).inv(),
69+
pub proof fn duplicate(tracked self: &AgreementResource<T>) -> (tracked result: AgreementResource<T>)
13670
ensures
137-
self.inv(),
138-
result.inv(),
139-
self.id() == old(self).id(),
140-
result.id() == old(self).id(),
71+
result.loc() == self.loc(),
14172
self@ == result@,
142-
self@ == old(self)@,
14373
{
144-
let tracked r = resource::duplicate(&self.r);
74+
let tracked r = self.r.duplicate_previous(self.r.value());
14575
AgreementResource::<T> { r }
14676
}
14777

14878
pub proof fn lemma_agreement(
149-
tracked self: &mut AgreementResource<T>,
79+
tracked self: &AgreementResource<T>,
15080
tracked other: &AgreementResource<T>,
15181
)
15282
requires
153-
old(self).inv(),
154-
other.inv(),
155-
old(self).id() == other.id(),
83+
self.loc() == other.loc(),
15684
ensures
157-
self.id() == old(self).id(),
158-
self@ == old(self)@,
15985
self@ == other@,
16086
{
161-
self.r.validate_2(&other.r);
87+
lemma_agree(&self.r, &other.r);
16288
}
16389
}
16490

0 commit comments

Comments
 (0)