creusot_std/invariant.rs
1//! Type invariants
2
3use crate::prelude::*;
4#[cfg(creusot)]
5use crate::resolve::structural_resolve;
6use core::ops::{Deref, DerefMut};
7
8/// A user-defined _type invariant_.
9///
10/// Type invariants are additional pre- and postconditions added to each program functions.
11///
12/// # Example
13///
14/// ```rust
15/// # use creusot_std::prelude::*;
16/// struct SumTo10 {
17/// a: i32,
18/// b: i32,
19/// }
20/// // The type invariant constrains the set of valid `SumTo10`s to
21/// // only allow values where the sum of both fields is equal to 10.
22/// impl Invariant for SumTo10 {
23/// #[logic(open)]
24/// fn invariant(self) -> bool {
25/// pearlite! {
26/// self.a@ + self.b@ == 10
27/// }
28/// }
29/// }
30///
31/// // #[requires(inv(x))] // generated by Creusot
32/// // #[ensures(inv(result))] // generated by Creusot
33/// fn invariant_holds(mut x: SumTo10) -> SumTo10 {
34/// assert!(x.a + x.b == 10); // We are given the invariant when entering the function
35/// x.a = 5; // we can break it locally!
36/// x.b = 5; // but it must be restored eventually
37/// x
38/// }
39/// ```
40///
41/// # Structural invariants
42///
43/// A type automatically inherits the invariants of its fields.
44///
45/// Examples:
46/// - `x: (T, U)` -> `inv(x.0) && inv(x.1)`
47/// - `x: &T` -> `inv(*x)`
48/// - `x: Vec<T>` -> `forall<i> 0 <= i && i < x@.len() ==> inv(x@[i])`
49///
50/// This does not prevent the type to additionnaly implement the `Invariant` trait.
51///
52/// ## Mutable borrows
53///
54/// For mutable borrows, the invariant is the conjunction of the invariants of the current
55/// and final values: `x: &mut T` -> `inv(*x) && inv(^x)`.
56///
57/// # Logical functions
58///
59/// Invariant pre- and postconditions are not added to logical functions:
60/// ```
61/// # use creusot_std::prelude::*;
62/// # struct SumTo10 { a: i32, b: i32 }
63/// # impl Invariant for SumTo10 {
64/// # #[logic(open)] fn invariant(self) -> bool { pearlite!{self.a@ + self.b@ == 10} }
65/// # }
66/// #[logic]
67/// #[ensures(x.a@ + x.b@ == 10)]
68/// fn not_provable(x: SumTo10) {}
69/// ```
70pub trait Invariant {
71 #[logic(prophetic)]
72 #[intrinsic("invariant")]
73 fn invariant(self) -> bool;
74}
75
76impl<T: ?Sized> Invariant for &T {
77 #[logic(open, prophetic, inline)]
78 #[creusot::trusted_trivial_if_param_trivial]
79 fn invariant(self) -> bool {
80 inv(*self)
81 }
82}
83
84impl<T: ?Sized> Invariant for &mut T {
85 #[logic(open, prophetic, inline)]
86 #[creusot::trusted_trivial_if_param_trivial]
87 fn invariant(self) -> bool {
88 pearlite! { inv(*self) && inv(^self) }
89 }
90}
91
92/// Whether the invariant of a value holds
93///
94/// This function is functionnaly equivalent to [`Invariant::invariant`], except that it
95/// can be called on any type (even if it does not implement [`Invariant`]).
96#[logic(prophetic, inline, open)]
97#[intrinsic("inv")]
98pub fn inv<T: ?Sized>(_: T) -> bool {
99 dead
100}
101
102#[cfg(not(creusot))]
103pub fn inv<T: ?Sized>(_: &T) -> bool {
104 panic!()
105}
106
107/// A type implements `InhabitedInvariants` when its type invariant is inhabited.
108/// This is needed to define subset types.
109pub trait InhabitedInvariant: Invariant {
110 #[logic]
111 #[ensures(result.invariant())]
112 fn inhabits() -> Self;
113}
114
115/// A _subset_ type.
116///
117/// This the same as `T`, with one exception: the invariant for `T` will also
118/// be verified in logic.
119///
120/// # Example
121///
122/// ```
123/// # use creusot_std::{invariant::{InhabitedInvariant, Subset}, prelude::*};
124/// struct Pair(i32);
125/// impl Invariant for Pair {
126/// #[logic] fn invariant(self) -> bool { self.0 % 2 == 0 }
127/// }
128/// impl InhabitedInvariant for Pair {
129/// #[logic]
130/// #[ensures(result.invariant())]
131/// fn inhabits() -> Self { Self(0i32) }
132/// }
133///
134/// #[logic]
135/// fn pair_in_logic(x: Subset<Pair>) {
136/// proof_assert!(x.0 % 2 == 0);
137/// }
138/// ```
139#[repr(transparent)]
140#[opaque]
141pub struct Subset<T: InhabitedInvariant>(T);
142
143impl<T: InhabitedInvariant + DeepModel> DeepModel for Subset<T> {
144 type DeepModelTy = T::DeepModelTy;
145
146 #[logic(inline)]
147 fn deep_model(self) -> T::DeepModelTy {
148 pearlite! { self.inner().deep_model() }
149 }
150}
151
152impl<T: InhabitedInvariant> Subset<T> {
153 #[trusted]
154 #[logic(opaque)]
155 #[ensures(result.invariant())]
156 pub fn inner(self) -> T {
157 dead
158 }
159
160 /// Create a new element of `Subset<T>` in logic.
161 ///
162 /// As per the [documentation of Subset](Subset), the returned value will
163 /// satisfy `T`'s type invariant.
164 #[trusted]
165 #[logic(opaque)]
166 #[requires(x.invariant())]
167 #[ensures(result.inner() == x)]
168 pub fn new_logic(x: T) -> Self {
169 let _ = x;
170 dead
171 }
172
173 /// Characterize that `Subset<T>` indeed contains a `T` (and only a `T`).
174 ///
175 /// # Example
176 ///
177 /// ```
178 /// # use creusot_std::{invariant::Subset, prelude::*};
179 /// #[requires(x == y.inner())]
180 /// fn foo<T: InhabitedInvariant>(x: T, y: Subset<T>) {
181 /// let x = Subset::new(x);
182 /// let _ = snapshot!(Subset::<T>::inner_inj);
183 /// proof_assert!(x == y);
184 /// }
185 /// ```
186 #[trusted]
187 #[logic(opaque)]
188 #[requires(self.inner() == other.inner())]
189 #[ensures(self == other)]
190 pub fn inner_inj(self, other: Self) {}
191
192 /// Create a new element of `Subset<T>`.
193 ///
194 /// # Example
195 ///
196 /// ```
197 /// # use creusot_std::{invariant::{InhabitedInvariant, Subset}, prelude::*};
198 /// // Use the `Pair` type defined in `Subset`'s documentation
199 /// # struct Pair(i32);
200 /// # impl Invariant for Pair {
201 /// # #[logic] fn invariant(self) -> bool { self.0 % 2 == 0 } }
202 /// # impl InhabitedInvariant for Pair {
203 /// # #[logic] #[ensures(result.invariant())]
204 /// # fn inhabits() -> Self { Self(0i32) } }
205 ///
206 /// let p = Subset::new(Pair(0));
207 /// proof_assert!(p.inner().0 == 0i32);
208 /// ```
209 #[check(ghost)]
210 #[trusted]
211 #[ensures(result == Self::new_logic(x))]
212 pub fn new(x: T) -> Self {
213 Subset(x)
214 }
215
216 /// Unwrap the `Subset` to get the inner value.
217 ///
218 /// # Example
219 ///
220 /// ```
221 /// # use creusot_std::{invariant::{InhabitedInvariant, Subset}, prelude::*};
222 /// // Use the `Pair` type defined in `Subset`'s documentation
223 /// # struct Pair(i32);
224 /// # impl Invariant for Pair {
225 /// # #[logic] fn invariant(self) -> bool { self.0 % 2 == 0 } }
226 /// # impl InhabitedInvariant for Pair {
227 /// # #[logic] #[ensures(result.invariant())]
228 /// # fn inhabits() -> Self { Self(0i32) } }
229 ///
230 /// fn changes_pair(p: &mut Subset<Pair>) { /* ... */ }
231 ///
232 /// let mut p = Subset::new(Pair(0));
233 /// changes_pair(&mut p);
234 /// let inner = p.into_inner();
235 /// proof_assert!(inner.0 % 2 == 0);
236 /// ```
237 #[check(ghost)]
238 #[trusted]
239 #[ensures(result == self.inner())]
240 pub fn into_inner(self) -> T {
241 self.0
242 }
243}
244
245impl<T: InhabitedInvariant> Deref for Subset<T> {
246 type Target = T;
247
248 #[check(ghost)]
249 #[trusted]
250 #[ensures(*result == self.inner())]
251 fn deref(&self) -> &Self::Target {
252 &self.0
253 }
254}
255
256impl<T: InhabitedInvariant> DerefMut for Subset<T> {
257 #[check(ghost)]
258 #[trusted]
259 #[ensures(*result == self.inner())]
260 #[ensures(^result == (^self).inner())]
261 fn deref_mut(&mut self) -> &mut Self::Target {
262 &mut self.0
263 }
264}
265
266impl<T: InhabitedInvariant + Clone> Clone for Subset<T> {
267 #[ensures(T::clone.postcondition((&(self.inner()),), result.inner()))]
268 fn clone(&self) -> Self {
269 snapshot! { Self::inner_inj };
270 Self::new(self.deref().clone())
271 }
272}
273
274impl<T: InhabitedInvariant + Copy> Copy for Subset<T> {}
275
276impl<T: InhabitedInvariant> Resolve for Subset<T> {
277 #[logic(open, prophetic, inline)]
278 fn resolve(self) -> bool {
279 pearlite! { resolve(self.inner()) }
280 }
281
282 #[trusted]
283 #[logic(prophetic)]
284 #[requires(structural_resolve(self))]
285 #[ensures(self.resolve())]
286 fn resolve_coherence(self) {}
287}
288
289impl<T: InhabitedInvariant + DeepModel + PartialEq> PartialEq for Subset<T> {
290 #[trusted]
291 #[ensures(result == (self.deep_model() == rhs.deep_model()))]
292 fn eq(&self, rhs: &Self) -> bool {
293 self.0 == rhs.0
294 }
295}
296
297impl<T: InhabitedInvariant + DeepModel + Eq> Eq for Subset<T> {}