creusot_std/logic/ra/
fmap.rs1#[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(law)]
50 #[ensures(a.op(b) == b.op(a))]
51 fn commutative(a: Self, b: Self) {
52 proof_assert!(match (a.op(b), b.op(a)) {
53 (Some(ab), Some(ba)) => ab.ext_eq(ba),
54 (None, None) => true,
55 _ => false,
56 })
57 }
58
59 #[logic(law)]
60 #[ensures(a.op(b).and_then_logic(|ab: Self| ab.op(c)) == b.op(c).and_then_logic(|bc| a.op(bc)))]
61 fn associative(a: Self, b: Self, c: Self) {
62 match (a.op(b), b.op(c)) {
63 (Some(ab), Some(bc)) => match (ab.op(c), a.op(bc)) {
64 (Some(x), Some(y)) => proof_assert!(x.ext_eq(y)),
65 _ => (),
66 },
67 _ => (),
68 }
69 }
70
71 #[logic(open)]
72 fn core(self) -> Option<Self> {
73 Some(self.filter_map(|(_, v): (K, V)| v.core()))
74 }
75
76 #[logic]
77 #[requires(self.core() != None)]
78 #[ensures({
79 let c = self.core().unwrap_logic();
80 c.op(c) == Some(c)
81 })]
82 #[ensures(self.core().unwrap_logic().op(self) == Some(self))]
83 fn core_idemp(self) {
84 self.core_total_idemp()
85 }
86
87 #[logic]
88 #[requires(i.op(i) == Some(i))]
89 #[requires(i.op(self) == Some(self))]
90 #[ensures(match self.core() {
91 Some(c) => i.incl(c),
92 None => false,
93 })]
94 fn core_is_maximal_idemp(self, i: Self) {
95 let _ = V::core_is_maximal_idemp;
96 }
97}
98
99impl<K, V: RA> UnitRA for FMap<K, V> {
100 #[logic(open)]
101 #[ensures(forall<x: Self> #[trigger(x.op(result))] x.op(result) == Some(x))]
102 fn unit() -> Self {
103 proof_assert!(forall<x: Self> x.op(Self::empty()).unwrap_logic().ext_eq(x));
104 Self::empty()
105 }
106
107 #[logic(open)]
108 #[ensures(self.core() == Some(result))]
109 fn core_total(self) -> Self {
110 self.filter_map(|(_, v): (K, V)| v.core())
111 }
112
113 #[logic]
114 #[ensures(self.core_total().op(self.core_total()) == Some(self.core_total()))]
115 #[ensures(self.core_total().op(self) == Some(self))]
116 fn core_total_idemp(self) {
117 let _ = V::core_idemp;
118 let c = self.core_total();
119 proof_assert!(c.op(c).unwrap_logic().ext_eq(c));
120 proof_assert!(c.op(self).unwrap_logic().ext_eq(self));
121 }
122}
123
124impl<K, V: RA> FMap<K, V> {
125 #[logic]
126 #[requires(forall<k: K> self.get(k).op(other.get(k)) != None)]
127 #[ensures(forall<k: K> Some(result.get(k)) == self.get(k).op(other.get(k)))]
128 pub fn total_op(self, other: Self) -> Self {
129 self.merge(other, |(x, y): (V, V)| match x.op(y) {
130 Some(r) => r,
131 _ => such_that(|_| true),
132 })
133 }
134}
135
136pub struct FMapInsertLocalUpdate<K, V>(pub Snapshot<K>, pub Snapshot<V>);
137
138impl<K, V: RA> LocalUpdate<FMap<K, V>> for FMapInsertLocalUpdate<K, V> {
139 #[logic(open)]
140 fn premise(self, from_auth: FMap<K, V>, _: FMap<K, V>) -> bool {
141 from_auth.get(*self.0) == None
142 }
143
144 #[logic(open)]
145 fn update(self, from_auth: FMap<K, V>, from_frag: FMap<K, V>) -> (FMap<K, V>, FMap<K, V>) {
146 (from_auth.insert(*self.0, *self.1), from_frag.insert(*self.0, *self.1))
147 }
148
149 #[logic]
150 #[allow(unused)]
151 #[requires(self.premise(from_auth, from_frag))]
152 #[requires(Some(from_frag).op(frame) == Some(Some(from_auth)))]
153 #[ensures({
154 let (to_auth, to_frag) = self.update(from_auth, from_frag);
155 Some(to_frag).op(frame) == Some(Some(to_auth))
156 })]
157 fn frame_preserving(
158 self,
159 from_auth: FMap<K, V>,
160 from_frag: FMap<K, V>,
161 frame: Option<FMap<K, V>>,
162 ) {
163 let (to_auth, to_frag) = self.update(from_auth, from_frag);
164 proof_assert!(match Some(to_frag).op(frame) {
165 Some(Some(x)) => to_auth.ext_eq(x),
166 _ => false,
167 });
168 }
169}