|
1 | 1 | //! Sum types for ghost resources. |
2 | 2 | use vstd::prelude::*; |
| 3 | +use vstd::pcm::Loc; |
| 4 | +use vstd::storage_protocol::*; |
| 5 | + |
| 6 | + |
| 7 | +use crate::resource::storage_protocol::csum::*; |
| 8 | +use crate::sum::*; |
| 9 | + |
3 | 10 |
|
4 | 11 | verus! { |
5 | 12 |
|
6 | | -pub ghost enum CsumP<A, B> { |
7 | | - Unit, |
8 | | - Cinl(A), |
9 | | - Cinr(B), |
10 | | - CsumInvalid, |
| 13 | +/// `SumResourceStorage` is a storage resource that stores either an A or a B, but not both. |
| 14 | +pub tracked struct SumResourceStorage<A, B> { |
| 15 | + tracked r: StorageResource<(), Sum<A, B>, CsumP<A, B>>, |
| 16 | +} |
| 17 | + |
| 18 | +impl<A,B> SumResourceStorage<A, B> |
| 19 | +{ |
| 20 | + pub closed spec fn id(self) -> Loc { |
| 21 | + self.r.loc() |
| 22 | + } |
| 23 | + |
| 24 | + pub closed spec fn protocol_monoid(self) -> CsumP<A, B> { |
| 25 | + self.r.value() |
| 26 | + } |
| 27 | + |
| 28 | + pub open spec fn is_empty(self) -> bool { |
| 29 | + self.protocol_monoid() is Unit |
| 30 | + } |
| 31 | + |
| 32 | + pub open spec fn is_left(self) -> bool { |
| 33 | + self.protocol_monoid() is Cinl |
| 34 | + } |
| 35 | + |
| 36 | + pub open spec fn is_right(self) -> bool { |
| 37 | + self.protocol_monoid() is Cinr |
| 38 | + } |
| 39 | + |
| 40 | + pub open spec fn resource(self) -> Sum<A, B> { |
| 41 | + self.protocol_monoid().to_sum() |
| 42 | + } |
| 43 | + |
| 44 | + pub proof fn alloc_empty() -> (tracked res:Self) |
| 45 | + ensures |
| 46 | + res.is_empty(), |
| 47 | + { |
| 48 | + let tracked r = StorageResource::alloc(CsumP::Unit, Map::tracked_empty()); |
| 49 | + SumResourceStorage { r } |
| 50 | + } |
| 51 | + |
| 52 | + pub proof fn alloc_left(tracked a: A) -> (tracked res: Self) |
| 53 | + ensures |
| 54 | + res.is_left(), |
| 55 | + res.resource() is Left, |
| 56 | + res.resource()->Left_0 == a, |
| 57 | + { |
| 58 | + let tracked mut m = Map::tracked_empty(); |
| 59 | + m.tracked_insert((), Sum::Left(a)); |
| 60 | + let tracked r = StorageResource::alloc(CsumP::Cinl(a), m); |
| 61 | + SumResourceStorage { r } |
| 62 | + } |
| 63 | + |
| 64 | + pub proof fn alloc_right(tracked b: B) -> (tracked res: Self) |
| 65 | + ensures |
| 66 | + res.is_right(), |
| 67 | + res.resource() is Right, |
| 68 | + res.resource()->Right_0 == b, |
| 69 | + { |
| 70 | + let tracked mut m = Map::tracked_empty(); |
| 71 | + m.tracked_insert((), Sum::Right(b)); |
| 72 | + let tracked r = StorageResource::alloc(CsumP::Cinr(b), m); |
| 73 | + SumResourceStorage { r } |
| 74 | + } |
| 75 | + |
| 76 | + pub proof fn tracked_take(tracked self) -> (tracked res: Sum<A, B>) |
| 77 | + requires |
| 78 | + self.is_left() || self.is_right(), |
| 79 | + ensures |
| 80 | + res == self.resource(), |
| 81 | + { |
| 82 | + self.protocol_monoid().lemma_csum_withdraws(); |
| 83 | + let tracked r = self.r; |
| 84 | + let tracked (_,mut m) = r.withdraw(CsumP::Unit, map![() => self.protocol_monoid().to_sum()]); |
| 85 | + m.tracked_remove(()) |
| 86 | + } |
| 87 | + |
| 88 | + pub proof fn tracked_take_left(tracked self) -> (tracked res: A) |
| 89 | + requires |
| 90 | + self.is_left(), |
| 91 | + ensures |
| 92 | + res == self.resource()->Left_0, |
| 93 | + { |
| 94 | + let tracked sum = self.tracked_take(); |
| 95 | + sum.tracked_take_left() |
| 96 | + } |
| 97 | + |
| 98 | + pub proof fn tracked_take_right(tracked self) -> (tracked res: B) |
| 99 | + requires |
| 100 | + self.is_right(), |
| 101 | + ensures |
| 102 | + res == self.resource()->Right_0, |
| 103 | + { |
| 104 | + let tracked sum = self.tracked_take(); |
| 105 | + sum.tracked_take_right() |
| 106 | + } |
| 107 | + |
| 108 | + pub proof fn tracked_borrow(tracked &self) -> (tracked res: &Sum<A, B>) |
| 109 | + requires |
| 110 | + self.is_left() || self.is_right(), |
| 111 | + ensures |
| 112 | + *res == self.resource(), |
| 113 | + { |
| 114 | + StorageResource::guard(&self.r, map![() => self.resource()]).tracked_borrow(()) |
| 115 | + } |
| 116 | + |
| 117 | + pub proof fn tracked_borrow_left(tracked &self) -> (tracked res: &A) |
| 118 | + requires |
| 119 | + self.is_left(), |
| 120 | + ensures |
| 121 | + *res == self.resource()->Left_0, |
| 122 | + { |
| 123 | + self.tracked_borrow().tracked_borrow_left() |
| 124 | + } |
| 125 | + |
| 126 | + pub proof fn tracked_borrow_right(tracked &self) -> (tracked res: &B) |
| 127 | + requires |
| 128 | + self.is_right(), |
| 129 | + ensures |
| 130 | + *res == self.resource()->Right_0, |
| 131 | + { |
| 132 | + self.tracked_borrow().tracked_borrow_right() |
| 133 | + } |
| 134 | + |
11 | 135 | } |
12 | 136 |
|
13 | 137 | } // verus! |
0 commit comments