Skip to content

Commit 72808ca

Browse files
committed
resource: add ResourceAlgebra trait
This is done by partitioning the previous PCM trait (while keeping that axiomatization of the pcm::Resource API intact and unchanged). Then the algebra::Resouce API is verified agains the pcm::Resource API, using an Option combinator (which lifts any RA into a PCM).
1 parent 20f7960 commit 72808ca

File tree

12 files changed

+607
-74
lines changed

12 files changed

+607
-74
lines changed

examples/pcm/agreement.rs

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ use verus_builtin::*;
3535
use verus_builtin_macros::*;
3636
use vstd::prelude::*;
3737
use vstd::resource;
38+
use vstd::resource::algebra::ResourceAlgebra;
3839
use vstd::resource::pcm::Resource;
3940
use vstd::resource::pcm::PCM;
4041
use vstd::resource::Loc;
@@ -53,7 +54,7 @@ impl<T> AgreementResourceValue<T> {
5354
}
5455
}
5556

56-
impl<T> PCM for AgreementResourceValue<T> {
57+
impl<T> ResourceAlgebra for AgreementResourceValue<T> {
5758
open spec fn valid(self) -> bool {
5859
!(self is Invalid)
5960
}
@@ -75,10 +76,6 @@ impl<T> PCM for AgreementResourceValue<T> {
7576
}
7677
}
7778

78-
open spec fn unit() -> Self {
79-
AgreementResourceValue::<T>::Empty { }
80-
}
81-
8279
proof fn valid_op(a: Self, b: Self) {
8380
}
8481

@@ -87,6 +84,13 @@ impl<T> PCM for AgreementResourceValue<T> {
8784

8885
proof fn associative(a: Self, b: Self, c: Self) {
8986
}
87+
}
88+
89+
90+
impl<T> PCM for AgreementResourceValue<T> {
91+
open spec fn unit() -> Self {
92+
AgreementResourceValue::<T>::Empty { }
93+
}
9094

9195
proof fn op_unit(self) {
9296
}

examples/pcm/log.rs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ use std::result::*;
4848
use verus_builtin::*;
4949
use verus_builtin_macros::*;
5050
use vstd::prelude::*;
51+
use vstd::resource::algebra::ResourceAlgebra;
5152
use vstd::resource::copy_duplicable_part;
5253
use vstd::resource::pcm::Resource;
5354
use vstd::resource::pcm::PCM;
@@ -69,7 +70,7 @@ pub open spec fn is_prefix<T>(s1: Seq<T>, s2: Seq<T>) -> bool {
6970
&&& forall|i| 0 <= i < s1.len() ==> s1[i] == s2[i]
7071
}
7172

72-
impl<T> PCM for LogResourceValue<T> {
73+
impl<T> ResourceAlgebra for LogResourceValue<T> {
7374
open spec fn valid(self) -> bool {
7475
&&& !(self is Invalid)
7576
}
@@ -130,10 +131,6 @@ impl<T> PCM for LogResourceValue<T> {
130131
}
131132
}
132133

133-
open spec fn unit() -> Self {
134-
Self::PrefixKnowledge { prefix: Seq::<T>::empty() }
135-
}
136-
137134
proof fn valid_op(a: Self, b: Self) {
138135
}
139136

@@ -147,6 +144,11 @@ impl<T> PCM for LogResourceValue<T> {
147144
is_prefix(log1, log2) && is_prefix(log2, log1) <==> log1 =~= log2);
148145
assert(forall|log| is_prefix(log, Seq::<T>::empty()) ==> log =~= Seq::<T>::empty());
149146
}
147+
}
148+
impl<T> PCM for LogResourceValue<T> {
149+
open spec fn unit() -> Self {
150+
Self::PrefixKnowledge { prefix: Seq::<T>::empty() }
151+
}
150152

151153
proof fn op_unit(self) {
152154
assert(forall|log| is_prefix(log, Seq::<T>::empty()) ==> log =~= Seq::<T>::empty());

examples/pcm/monotonic_counter.rs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ use std::result::*;
5555
use verus_builtin::*;
5656
use verus_builtin_macros::*;
5757
use vstd::prelude::*;
58+
use vstd::resource::algebra::ResourceAlgebra;
5859
use vstd::resource::copy_duplicable_part;
5960
use vstd::resource::pcm::Resource;
6061
use vstd::resource::pcm::PCM;
@@ -86,7 +87,7 @@ pub enum MonotonicCounterResourceValue {
8687

8788
// To use `MonotonicCounterResourceValue` as a resource, we have to implement
8889
// `PCM`, showing how to use it in a resource algebra.
89-
impl PCM for MonotonicCounterResourceValue {
90+
impl ResourceAlgebra for MonotonicCounterResourceValue {
9091
open spec fn valid(self) -> bool {
9192
!(self is Invalid)
9293
}
@@ -160,10 +161,6 @@ impl PCM for MonotonicCounterResourceValue {
160161
}
161162
}
162163

163-
open spec fn unit() -> Self {
164-
MonotonicCounterResourceValue::LowerBound { lower_bound: 0 }
165-
}
166-
167164
proof fn valid_op(a: Self, b: Self) {
168165
}
169166

@@ -172,6 +169,11 @@ impl PCM for MonotonicCounterResourceValue {
172169

173170
proof fn associative(a: Self, b: Self, c: Self) {
174171
}
172+
}
173+
impl PCM for MonotonicCounterResourceValue {
174+
open spec fn unit() -> Self {
175+
MonotonicCounterResourceValue::LowerBound { lower_bound: 0 }
176+
}
175177

176178
proof fn op_unit(self) {
177179
}

examples/pcm/oneshot.rs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ use verus_builtin::*;
6969
use verus_builtin_macros::*;
7070
use vstd::prelude::*;
7171
use vstd::resource;
72+
use vstd::resource::algebra::ResourceAlgebra;
7273
use vstd::resource::pcm::Resource;
7374
use vstd::resource::pcm::PCM;
7475
use vstd::resource::update_and_redistribute;
@@ -98,7 +99,7 @@ pub enum OneShotResourceValue {
9899

99100
// To use `OneShotResourceValue` as a resource, we have to implement
100101
// `PCM`, showing how to use it in a resource algebra.
101-
impl PCM for OneShotResourceValue {
102+
impl ResourceAlgebra for OneShotResourceValue {
102103
open spec fn valid(self) -> bool {
103104
!(self is Invalid)
104105
}
@@ -119,10 +120,6 @@ impl PCM for OneShotResourceValue {
119120
}
120121
}
121122

122-
open spec fn unit() -> Self {
123-
OneShotResourceValue::Empty { }
124-
}
125-
126123
proof fn valid_op(a: Self, b: Self) {
127124
}
128125

@@ -131,6 +128,11 @@ impl PCM for OneShotResourceValue {
131128

132129
proof fn associative(a: Self, b: Self, c: Self) {
133130
}
131+
}
132+
impl PCM for OneShotResourceValue {
133+
open spec fn unit() -> Self {
134+
OneShotResourceValue::Empty { }
135+
}
134136

135137
proof fn op_unit(self) {
136138
}

0 commit comments

Comments
 (0)