1#[cfg(creusot)]
2use crate::logic::such_that;
3use crate::{
4 logic::{
5 FMap,
6 ra::{RA, UnitRA, update::LocalUpdate},
7 },
8 prelude::*,
9};
10
11impl<K, V: RA> RA for FMap<K, V> {
12 #[logic(open)]
13 fn op(self, other: Self) -> Option<Self> {
14 pearlite! {
15 if (forall<k: K> self.get(k).op(other.get(k)) != None) {
16 Some(self.total_op(other))
17 } else {
18 None
19 }
20 }
21 }
22
23 #[logic(open)]
24 #[ensures(match result {
25 Some(c) => factor.op(c) == Some(self),
26 None => forall<c: Self> factor.op(c) != Some(self),
27 })]
28 fn factor(self, factor: Self) -> Option<Self> {
29 pearlite! {
30 if (forall<k: K> factor.get(k).incl(self.get(k))) {
31 let res = self.filter_map(|(k, vo): (K, V)|
32 match Some(vo).factor(factor.get(k)) {
33 Some(r) => r,
34 None => None,
35 });
36 proof_assert!(
37 match factor.op(res) {
38 None => false,
39 Some(o) => o.ext_eq(self)
40 }
41 );
42 Some(res)
43 } else {
44 None
45 }
46 }
47 }
48
49 #[logic(open, inline)]
50 #[ensures(result == (self == other))]
51 fn eq(self, other: Self) -> bool {
52 pearlite! {
53 let _ = Self::ext_eq;
54 forall<k: K> self.get(k).eq(other.get(k))
55 }
56 }
57
58 #[logic(law)]
59 #[ensures(a.op(b) == b.op(a))]
60 fn commutative(a: Self, b: Self) {
61 proof_assert!(match (a.op(b), b.op(a)) {
62 (Some(ab), Some(ba)) => ab.ext_eq(ba),
63 (None, None) => true,
64 _ => false,
65 })
66 }
67
68 #[logic]
69 #[ensures(a.op(b).and_then_logic(|ab: Self| ab.op(c)) == b.op(c).and_then_logic(|bc| a.op(bc)))]
70 fn associative(a: Self, b: Self, c: Self) {
71 match (a.op(b), b.op(c)) {
72 (Some(ab), Some(bc)) => match (ab.op(c), a.op(bc)) {
73 (Some(x), Some(y)) => proof_assert!(x.ext_eq(y)),
74 _ => (),
75 },
76 _ => (),
77 }
78 }
79
80 #[logic(open)]
81 fn core(self) -> Option<Self> {
82 Some(self.filter_map(|(_, v): (K, V)| v.core()))
83 }
84
85 #[logic]
86 #[requires(self.core() != None)]
87 #[ensures({
88 let c = self.core().unwrap_logic();
89 c.op(c) == Some(c)
90 })]
91 #[ensures(self.core().unwrap_logic().op(self) == Some(self))]
92 fn core_idemp(self) {
93 self.core_total_idemp()
94 }
95
96 #[logic]
97 #[requires(i.op(i) == Some(i))]
98 #[requires(i.op(self) == Some(self))]
99 #[ensures(match self.core() {
100 Some(c) => i.incl(c),
101 None => false,
102 })]
103 fn core_is_maximal_idemp(self, i: Self) {
104 let _ = V::core_is_maximal_idemp;
105 }
106}
107
108impl<K, V: RA> UnitRA for FMap<K, V> {
109 #[logic(open)]
110 #[ensures(forall<x: Self> #[trigger(x.op(result))] x.op(result) == Some(x))]
111 fn unit() -> Self {
112 proof_assert!(forall<x: Self> x.op(Self::empty()).unwrap_logic().ext_eq(x));
113 Self::empty()
114 }
115
116 #[logic(open)]
117 #[ensures(self.core() == Some(result))]
118 fn core_total(self) -> Self {
119 self.filter_map(|(_, v): (K, V)| v.core())
120 }
121
122 #[logic]
123 #[ensures(self.core_total().op(self.core_total()) == Some(self.core_total()))]
124 #[ensures(self.core_total().op(self) == Some(self))]
125 fn core_total_idemp(self) {
126 let _ = V::core_idemp;
127 let c = self.core_total();
128 proof_assert!(c.op(c).unwrap_logic().ext_eq(c));
129 proof_assert!(c.op(self).unwrap_logic().ext_eq(self));
130 }
131}
132
133impl<K, V: RA> FMap<K, V> {
134 #[logic]
135 #[requires(forall<k: K> self.get(k).op(other.get(k)) != None)]
136 #[ensures(forall<k: K> Some(result.get(k)) == self.get(k).op(other.get(k)))]
137 pub fn total_op(self, other: Self) -> Self {
138 self.merge(other, |(x, y): (V, V)| match x.op(y) {
139 Some(r) => r,
140 _ => such_that(|_| true),
141 })
142 }
143}
144
145pub struct FMapInsertLocalUpdate<K, V>(pub Snapshot<K>, pub Snapshot<V>);
146
147impl<K, V: RA> LocalUpdate<FMap<K, V>> for FMapInsertLocalUpdate<K, V> {
148 #[logic(open, inline)]
149 fn premise(self, from_auth: FMap<K, V>, _: FMap<K, V>) -> bool {
150 from_auth.get(*self.0) == None
151 }
152
153 #[logic(open, inline)]
154 fn update(self, from_auth: FMap<K, V>, from_frag: FMap<K, V>) -> (FMap<K, V>, FMap<K, V>) {
155 (from_auth.insert(*self.0, *self.1), from_frag.insert(*self.0, *self.1))
156 }
157
158 #[logic]
159 #[allow(unused)]
160 #[requires(self.premise(from_auth, from_frag))]
161 #[requires(Some(from_frag).op(frame) == Some(Some(from_auth)))]
162 #[ensures({
163 let (to_auth, to_frag) = self.update(from_auth, from_frag);
164 Some(to_frag).op(frame) == Some(Some(to_auth))
165 })]
166 fn frame_preserving(
167 self,
168 from_auth: FMap<K, V>,
169 from_frag: FMap<K, V>,
170 frame: Option<FMap<K, V>>,
171 ) {
172 let (to_auth, to_frag) = self.update(from_auth, from_frag);
173 proof_assert!(match Some(to_frag).op(frame) {
174 Some(Some(x)) => to_auth.ext_eq(x),
175 _ => false,
176 });
177 }
178}