creusot_std/logic/ra/
update.rs1#[cfg(creusot)]
2use crate::logic::such_that;
3use crate::{
4 logic::{Mapping, ra::RA},
5 prelude::*,
6};
7
8pub trait Update<R: RA> {
13 type Choice;
16
17 #[logic]
21 fn premise(self, from: R) -> bool;
22
23 #[logic]
29 #[requires(self.premise(from))]
30 fn update(self, from: R, ch: Self::Choice) -> R;
31
32 #[logic]
41 #[requires(self.premise(from))]
42 #[requires(from.op(frame) != None)]
43 #[ensures(self.update(from, result).op(frame) != None)]
44 fn frame_preserving(self, from: R, frame: R) -> Self::Choice;
45}
46
47impl<R: RA> Update<R> for Snapshot<R> {
53 type Choice = ();
54
55 #[logic(open, inline)]
56 fn premise(self, from: R) -> bool {
57 pearlite! {
58 forall<y: R> from.op(y) != None ==> self.op(y) != None
59 }
60 }
61
62 #[logic(open, inline)]
63 #[requires(self.premise(from))]
64 fn update(self, from: R, _: ()) -> R {
65 *self
66 }
67
68 #[logic]
69 #[requires(self.premise(from))]
70 #[requires(from.op(frame) != None)]
71 #[ensures(self.update(from, result).op(frame) != None)]
72 fn frame_preserving(self, from: R, frame: R) {}
73}
74
75impl<R: RA, Choice> Update<R> for Snapshot<Mapping<Choice, R>> {
80 type Choice = Choice;
81
82 #[logic(open, inline)]
83 fn premise(self, from: R) -> bool {
84 pearlite! {
85 forall<y: R> from.op(y) != None ==>
86 exists<ch: Choice> self[ch].op(y) != None
87 }
88 }
89
90 #[logic(open, inline)]
91 #[requires(self.premise(from))]
92 fn update(self, from: R, ch: Choice) -> R {
93 self[ch]
94 }
95
96 #[logic]
97 #[requires(self.premise(from))]
98 #[requires(from.op(frame) != None)]
99 #[ensures(self.update(from, result).op(frame) != None)]
100 fn frame_preserving(self, from: R, frame: R) -> Choice {
101 such_that(|ch| self.update(from, ch).op(frame) != None)
102 }
103}
104
105impl<R: RA> Update<R> for () {
107 type Choice = ();
108
109 #[logic(open, inline)]
110 fn premise(self, _: R) -> bool {
111 true
112 }
113
114 #[logic(open, inline)]
115 fn update(self, from: R, _: ()) -> R {
116 from
117 }
118
119 #[logic]
120 #[requires(from.op(frame) != None)]
121 #[ensures(from.op(frame) != None)]
122 fn frame_preserving(self, from: R, frame: R) {}
123}
124
125pub trait LocalUpdate<R: RA>: Sized {
133 #[logic]
138 fn premise(self, from_auth: R, from_frag: R) -> bool;
139
140 #[logic]
144 fn update(self, from_auth: R, from_frag: R) -> (R, R);
145
146 #[logic]
154 #[requires(self.premise(from_auth, from_frag))]
155 #[requires(Some(from_frag).op(frame) == Some(Some(from_auth)))]
156 #[ensures({
157 let (to_auth, to_frag) = self.update(from_auth, from_frag);
158 Some(to_frag).op(frame) == Some(Some(to_auth))
159 })]
160 fn frame_preserving(self, from_auth: R, from_frag: R, frame: Option<R>);
161}
162
163impl<R: RA> LocalUpdate<R> for Snapshot<(R, R)> {
167 #[logic(open, inline)]
168 fn premise(self, from_auth: R, from_frag: R) -> bool {
169 pearlite! {
170 forall<f: Option<R>>
171 Some(from_frag).op(f) == Some(Some(from_auth)) ==>
172 Some(self.1).op(f) == Some(Some(self.0))
173 }
174 }
175
176 #[logic(open, inline)]
177 fn update(self, _: R, _: R) -> (R, R) {
178 *self
179 }
180
181 #[logic]
182 #[allow(unused)]
183 #[requires(LocalUpdate::premise(self, from_auth, from_frag))]
184 #[requires(Some(from_frag).op(frame) == Some(Some(from_auth)))]
185 #[ensures({
186 let (to_auth, to_frag) = LocalUpdate::update(self, from_auth, from_frag);
187 Some(to_frag).op(frame) == Some(Some(to_auth))
188 })]
189 fn frame_preserving(self, from_auth: R, from_frag: R, frame: Option<R>) {}
190}
191
192impl<R: RA> LocalUpdate<R> for () {
194 #[logic(open, inline)]
195 fn premise(self, _: R, _: R) -> bool {
196 true
197 }
198
199 #[logic(open, inline)]
200 fn update(self, from_auth: R, from_frag: R) -> (R, R) {
201 (from_auth, from_frag)
202 }
203
204 #[logic]
205 #[requires(Some(frag).op(frame) == Some(Some(auth)))]
206 #[ensures(Some(frag).op(frame) == Some(Some(auth)))]
207 fn frame_preserving(self, auth: R, frag: R, frame: Option<R>) {}
208}