pub enum CsumP<A, B, const TOTAL: u64> {
Unit,
Cinl(Option<A>, int, bool),
Cinr(Option<B>, int, bool),
CsumInvalid,
}Expand description
A sum-type protocol monoid that stores a tracked object of either type A or type B.
The knowledge of the existence of a specific type of resource can be shared up to TOTAL pieces, but only one piece has the exclusive ownership of the resource, allowing arbitrary withdrawing, depositing, or updating.
Variants§
Unit
The unit element, only for technical reasons, not intended to be used directly.
Cinl(Option<A>, int, bool)
The left side of the sum, with an optional resource, a fraction, and a boolean indicating whether it is the exclusive owner of the resource.
Cinr(Option<B>, int, bool)
The right side of the sum, with an optional resource, a fraction, and a boolean indicating whether it is the exclusive owner of the resource.
CsumInvalid
An invalid state, used to represent an invalid combination of resources.
Implementations§
Source§impl<A, B, const TOTAL: u64> CsumP<A, B, TOTAL>
impl<A, B, const TOTAL: u64> CsumP<A, B, TOTAL>
pub fn arrow_2(self) -> bool
pub fn arrow_1(self) -> int
pub fn arrow_Cinl_0(self) -> Option<A>
pub fn arrow_Cinl_1(self) -> int
pub fn arrow_Cinl_2(self) -> bool
pub fn arrow_Cinr_0(self) -> Option<B>
pub fn arrow_Cinr_1(self) -> int
pub fn arrow_Cinr_2(self) -> bool
Source§impl<A, B, const TOTAL: u64> CsumP<A, B, TOTAL>
impl<A, B, const TOTAL: u64> CsumP<A, B, TOTAL>
Sourcepub open spec fn is_left(self) -> bool
pub open spec fn is_left(self) -> bool
{ self is Cinl }Whether the protocol monoid is currently in the left state.
Sourcepub open spec fn is_right(self) -> bool
pub open spec fn is_right(self) -> bool
{ self is Cinr }Whether the protocol monoid is currently in the right state.
Sourcepub open spec fn is_resource_owner(self) -> bool
pub open spec fn is_resource_owner(self) -> bool
{
match self {
CsumP::Cinl(_, _, b) | CsumP::Cinr(_, _, b) => b,
_ => false,
}
}Whether the protocol monoid is an exclusive owner of a resource.
Sourcepub open spec fn has_resource(self) -> bool
pub open spec fn has_resource(self) -> bool
{
match self {
CsumP::Cinl(ov, n, true) => ov is Some,
CsumP::Cinr(ov, n, true) => ov is Some,
_ => false,
}
}Whether the protocol monoid currently owns a resource, only meaningful when it is the exclusive owner.
Sourcepub open spec fn has_no_resource(self) -> bool
pub open spec fn has_no_resource(self) -> bool
{
match self {
CsumP::Cinl(ov, n, true) => ov is None,
CsumP::Cinr(ov, n, true) => ov is None,
_ => false,
}
}Whether the protocol monoid has had its resource taken, only meaningful when it is the exclusive owner.
Sourcepub open spec fn resource(self) -> Sum<A, B>
pub open spec fn resource(self) -> Sum<A, B>
{
match self {
CsumP::Cinl(Some(a), n, true) => Sum::Left(a),
CsumP::Cinr(Some(b), n, true) => Sum::Right(b),
_ => arbitrary(),
}
}The resource stored in the protocol monoid.
Sourcepub open spec fn frac(self) -> int
pub open spec fn frac(self) -> int
{
match self {
CsumP::Cinl(_, n, _) | CsumP::Cinr(_, n, _) => n,
_ => 1,
}
}The fraction of the resource knowledge.
Sourcepub open spec fn is_valid(self) -> bool
pub open spec fn is_valid(self) -> bool
{
match self {
CsumP::Unit => true,
CsumP::Cinl(ov, n, b) => 0 < n <= TOTAL && (ov is Some ==> b),
CsumP::Cinr(ov, n, b) => 0 < n <= TOTAL && (ov is Some ==> b),
_ => false,
}
}The invariant of the protocol monoid.
Sourcepub proof fn lemma_withdraws_left(self)
pub proof fn lemma_withdraws_left(self)
self.is_left(),self.is_resource_owner(),self.has_resource(),self.is_valid(),ensureswithdraws(self, CsumP::Cinl(None, self.frac(), true), map![() => self.resource()]),Sourcepub proof fn lemma_withdraws_right(self)
pub proof fn lemma_withdraws_right(self)
self.is_resource_owner(),self.is_right(),self.has_resource(),self.is_valid(),ensureswithdraws(self, CsumP::Cinr(None, self.frac(), true), map![() => self.resource()]),Sourcepub proof fn lemma_deposit_left(self, a: A)
pub proof fn lemma_deposit_left(self, a: A)
self.is_resource_owner(),self.is_left(),self.has_no_resource(),self.is_valid(),ensuresdeposits(self, map![() => Sum::Left(a)], CsumP::Cinl(Some(a), self.frac(), true)),Sourcepub proof fn lemma_deposit_right(self, b: B)
pub proof fn lemma_deposit_right(self, b: B)
self.is_resource_owner(),self.is_right(),self.has_no_resource(),self.is_valid(),ensuresdeposits(self, map![() => Sum::Right(b)], CsumP::Cinr(Some(b), self.frac(), true)),Sourcepub proof fn lemma_updates_none(self)
pub proof fn lemma_updates_none(self)
self.is_left() || self.is_right(),self.frac() == TOTAL,self.is_resource_owner(),self.has_no_resource(),ensuresupdates(self, CsumP::Cinl(None, self.frac(), true)),updates(self, CsumP::Cinr(None, self.frac(), true)),Trait Implementations§
Source§impl<A, B, const TOTAL: u64> Protocol<(), Sum<A, B>> for CsumP<A, B, TOTAL>
impl<A, B, const TOTAL: u64> Protocol<(), Sum<A, B>> for CsumP<A, B, TOTAL>
Source§open spec fn op(self, other: Self) -> Self
open spec fn op(self, other: Self) -> Self
{
match (self, other) {
(CsumP::Unit, x) => x,
(x, CsumP::Unit) => x,
(CsumP::Cinl(ov1, n1, b1), CsumP::Cinl(ov2, n2, b2)) => {
if !self.is_valid() || !other.is_valid() || n1 + n2 > TOTAL || b1 && b2
|| ov1 is Some && ov2 is Some
{
CsumP::CsumInvalid
} else {
CsumP::Cinl(if ov1 is Some { ov1 } else { ov2 }, n1 + n2, b1 || b2)
}
}
(CsumP::Cinr(ov1, n1, b1), CsumP::Cinr(ov2, n2, b2)) => {
if !self.is_valid() || !other.is_valid() || n1 + n2 > TOTAL || b1 && b2
|| ov1 is Some && ov2 is Some
{
CsumP::CsumInvalid
} else {
CsumP::Cinr(if ov1 is Some { ov1 } else { ov2 }, n1 + n2, b1 || b2)
}
}
_ => CsumP::CsumInvalid,
}
}Source§open spec fn rel(self, s: Map<(), Sum<A, B>>) -> bool
open spec fn rel(self, s: Map<(), Sum<A, B>>) -> bool
{
match self {
CsumP::Unit => s.is_empty(),
CsumP::Cinl(None, n, true) => 0 <= n <= TOTAL && s.is_empty(),
CsumP::Cinl(Some(a), n, true) => {
0 <= n <= TOTAL && s.contains_key(()) && s[()] == Sum::<A, B>::Left(a)
}
CsumP::Cinr(None, n, true) => 0 <= n <= TOTAL && s.is_empty(),
CsumP::Cinr(Some(b), n, true) => {
0 <= n <= TOTAL && s.contains_key(()) && s[()] == Sum::<A, B>::Right(b)
}
_ => false,
}
}