1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454
use crate::{
logic::{ops::IndexLogic, Mapping},
util::*,
*,
};
/// A mapping, where keys may not have an associated value.
#[cfg_attr(not(creusot), allow(dead_code))]
type PMap<K, V> = Mapping<K, Option<SizedW<V>>>;
/// A finite map type usable in pearlite and `ghost!` blocks.
///
/// If you need an infinite map, see [`Mapping`].
///
/// # Ghost
///
/// Since [`std::collections::HashMap`] and [`std::collections::BTreeMap`] have finite
/// capacity, this could cause some issues in ghost code:
/// ```rust,creusot,compile_fail
/// ghost! {
/// let mut map = HashMap::new();
/// for _ in 0..=usize::MAX as u128 + 1 {
/// map.insert(0, 0); // cannot fail, since we are in a ghost block
/// }
/// proof_assert!(map.len() <= usize::MAX@); // by definition
/// proof_assert!(map.len() > usize::MAX@); // uh-oh
/// }
/// ```
///
/// This type is designed for this use-case, with no restriction on the capacity.
#[trusted] //opaque
pub struct FMap<K, V: ?Sized>(std::marker::PhantomData<K>, std::marker::PhantomData<V>);
/// Logical definitions
impl<K, V: ?Sized> FMap<K, V> {
/// Returns the empty map.
#[trusted]
#[logic]
#[ensures(result.len() == 0)]
#[ensures(result.view() == Mapping::cst(None))]
pub fn empty() -> Self {
dead
}
/// The number of elements in the map, also called its length.
#[trusted]
#[logic]
#[ensures(result >= 0)]
pub fn len(self) -> Int {
dead
}
/// View of the map
///
/// This represents the actual content of the map: other methods are specified relative to this.
#[trusted]
#[logic]
#[ensures(forall<m1: Self, m2: Self> m1 != m2 ==> Self::view(m1) != Self::view(m2))]
pub fn view(self) -> PMap<K, V> {
dead
}
/// Returns a new map, where the key-value pair `(k, v)` have been inserted.
#[trusted]
#[logic]
#[ensures(result.view() == self.view().set(k, Some(v.make_sized())))]
#[ensures(self.contains(k) ==> result.len() == self.len())]
#[ensures(!self.contains(k) ==> result.len() == self.len() + 1)]
pub fn insert(self, k: K, v: V) -> Self {
dead
}
/// Returns a new map, where the key `k` is no longer present.
#[trusted]
#[logic]
#[ensures(result.view() == self.view().set(k, None))]
#[ensures(result.len() == if self.contains(k) {self.len() - 1} else {self.len()})]
pub fn remove(self, k: K) -> Self {
dead
}
/// Get the value associated with key `k` in the map.
///
/// If no value is present, returns [`None`].
#[logic]
#[open]
#[why3::attr = "inline:trivial"]
pub fn get(self, k: K) -> Option<V>
where
V: Sized,
{
match self.get_unsized(k) {
None => None,
Some(x) => Some(*x),
}
}
/// Same as [`Self::get`], but can handle unsized values.
#[logic]
#[open]
#[why3::attr = "inline:trivial"]
pub fn get_unsized(self, k: K) -> Option<SizedW<V>> {
self.view().get(k)
}
/// Get the value associated with key `k` in the map.
///
/// If no value is present, the returned value is meaningless.
#[logic]
#[open]
#[why3::attr = "inline:trivial"]
pub fn lookup(self, k: K) -> V
where
V: Sized,
{
*self.lookup_unsized(k)
}
/// Same as [`Self::lookup`], but can handle unsized values.
#[logic]
#[open]
#[why3::attr = "inline:trivial"]
pub fn lookup_unsized(self, k: K) -> SizedW<V> {
unwrap(self.get_unsized(k))
}
/// Returns `true` if the map contains a value for the specified key.
#[logic]
#[open]
#[why3::attr = "inline:trivial"]
pub fn contains(self, k: K) -> bool {
self.get_unsized(k) != None
}
/// Returns `true` if the map contains no elements.
#[logic]
#[open]
pub fn is_empty(self) -> bool {
self.ext_eq(FMap::empty())
}
/// Returns `true` if the two maps have no key in common.
#[logic]
#[open]
pub fn disjoint(self, other: Self) -> bool {
pearlite! {forall<k: K> !self.contains(k) || !other.contains(k)}
}
/// Returns `true` if all key-value pairs in `self` are also in `other`.
#[logic]
#[open]
pub fn subset(self, other: Self) -> bool {
pearlite! {
forall<k: K> self.contains(k) ==> other.get_unsized(k) == self.get_unsized(k)
}
}
/// Returns a new map, which is the union of `self` and `other`.
///
/// If `self` and `other` are not [`disjoint`](Self::disjoint), the result is unspecified.
#[trusted]
#[logic]
#[requires(self.disjoint(other))]
#[ensures(forall<k: K> result.get_unsized(k) == if self.contains(k) {
self.get_unsized(k)
} else if other.contains(k) {
other.get_unsized(k)
} else {
None
})]
#[ensures(result.len() == self.len() + other.len())]
pub fn union(self, other: Self) -> Self {
dead
}
/// Returns a new map, that contains all the key-value pairs of `self` such that the
/// key is not in `other`.
#[trusted]
#[logic]
#[ensures(forall<k: K> result.get_unsized(k) == if other.contains(k) {
None
} else {
self.get_unsized(k)
})]
pub fn subtract_keys(self, other: Self) -> Self {
dead
}
/// Same as [`Self::subtract_keys`], but the result is meaningless if `other` is not
/// a [`subset`](Self::subset) of `self`.
///
/// In return, this gives more guarantees than `Self::subtract_keys`.
#[logic]
#[open]
#[requires(other.subset(self))]
#[ensures(result.disjoint(other))]
#[ensures(other.union(result).ext_eq(self))]
#[ensures(forall<k: K> result.get_unsized(k) == if other.contains(k) {
None
} else {
self.get_unsized(k)
})]
pub fn subtract(self, other: Self) -> Self {
self.subtract_keys(other)
}
/// Extensional equality.
///
/// Returns `true` if `self` and `other` contain exactly the same key-value pairs.
///
/// This is in fact equivalent with normal equality.
#[logic]
#[open]
#[ensures(result ==> self == other)]
#[ensures((forall<k: K> self.get_unsized(k) == other.get_unsized(k)) ==> result)]
pub fn ext_eq(self, other: Self) -> bool {
self.view() == other.view()
}
}
impl<K, V> IndexLogic<K> for FMap<K, V> {
type Item = V;
#[logic]
#[open]
#[why3::attr = "inline:trivial"]
fn index_logic(self, key: K) -> Self::Item {
self.lookup(key)
}
}
/// Ghost definitions
impl<K, V: ?Sized> FMap<K, V> {
/// Create a new, empty map on the ghost heap.
#[trusted]
#[pure]
#[ensures(result.is_empty())]
#[allow(unreachable_code)]
pub fn new() -> GhostBox<Self> {
ghost!(panic!())
}
/// Returns the number of elements in the map.
///
/// If you need to get the length in pearlite, consider using [`len`](Self::len).
///
/// # Example
/// ```rust,creusot
/// use creusot_contracts::{logic::FMap, *};
///
/// let mut map = FMap::new();
/// ghost! {
/// let len1 = map.len_ghost();
/// map.insert_ghost(1, 21);
/// map.insert_ghost(1, 42);
/// map.insert_ghost(2, 50);
/// let len2 = map.len_ghost();
/// proof_assert!(len1 == 0);
/// proof_assert!(len2 == 2);
/// };
/// ```
#[trusted]
#[pure]
#[ensures(result == self.len())]
pub fn len_ghost(&self) -> Int {
panic!()
}
/// Returns true if the map contains a value for the specified key.
///
/// # Example
/// ```rust,creusot
/// use creusot_contracts::{logic::FMap, *};
///
/// let mut map = FMap::new();
/// ghost! {
/// map.insert_ghost(1, 42);
/// let (b1, b2) = (map.contains_ghost(&1), map.contains_ghost(&2));
/// proof_assert!(b1);
/// proof_assert!(!b2);
/// };
/// ```
#[pure]
#[ensures(result == self.contains(*key))]
pub fn contains_ghost(&self, key: &K) -> bool {
self.get_ghost(key).is_some()
}
/// Returns a reference to the value corresponding to the key.
///
/// # Example
/// ```rust,creusot
/// use creusot_contracts::{logic::FMap, *};
///
/// let mut map = FMap::new();
/// ghost! {
/// map.insert_ghost(1, 2);
/// let x1 = map.get_ghost(&1);
/// let x2 = map.get_ghost(&2);
/// proof_assert!(x1 == Some(&2));
/// proof_assert!(x2 == None);
/// };
/// ```
#[trusted]
#[pure]
#[ensures(if self.contains(*key) {
match result {
None => false,
Some(r) => *self.lookup_unsized(*key) == *r,
}
} else {
result == None
})]
pub fn get_ghost(&self, key: &K) -> Option<&V> {
let _ = key;
panic!()
}
/// Returns a mutable reference to the value corresponding to the key.
///
/// # Example
/// ```rust,creusot
/// use creusot_contracts::{logic::FMap, *};
///
/// let mut map = FMap::new();
/// ghost! {
/// map.insert_ghost(1, 21);
/// if let Some(x) = map.get_mut_ghost(&1) {
/// *x = 42;
/// }
/// proof_assert!(map.lookup(1i32) == 42i32);
/// };
/// ```
#[trusted]
#[pure]
#[ensures(if self.contains(*key) {
match result {
None => false,
Some(r) => (^self).contains(*key) &&
*(*self).lookup_unsized(*key) == *r &&
*(^self).lookup_unsized(*key) == ^r,
}
} else {
result == None && *self == ^self
})]
#[ensures(forall<k: K> k != *key ==> (*self).get_unsized(k) == (^self).get_unsized(k))]
#[ensures((*self).len() == (^self).len())]
pub fn get_mut_ghost(&mut self, key: &K) -> Option<&mut V> {
let _ = key;
panic!()
}
/// Inserts a key-value pair into the map.
///
/// If the map did not have this key present, `None` is returned.
///
/// # Example
/// ```rust,creusot
/// use creusot_contracts::{logic::FMap, *};
///
/// let mut map = FMap::new();
/// ghost! {
/// let res1 = map.insert_ghost(37, 41);
/// proof_assert!(res1 == None);
/// proof_assert!(map.is_empty() == false);
///
/// let res2 = map.insert_ghost(37, 42);
/// proof_assert!(res2 == Some(41));
/// proof_assert!(map.lookup(37i32) == 42i32);
/// };
/// ```
#[trusted]
#[pure]
#[ensures(^self == (*self).insert(key, value))]
#[ensures(result == (*self).get(key))]
pub fn insert_ghost(&mut self, key: K, value: V) -> Option<V>
where
V: Sized,
{
let _ = key;
let _ = value;
panic!()
}
/// Same as [`Self::insert_ghost`], but for unsized values.
#[trusted]
#[pure]
#[ensures(^self == (*self).insert(key, *value))]
#[ensures(result == (*self).get_unsized(key))]
pub fn insert_ghost_unsized(&mut self, key: K, value: Box<V>) -> Option<Box<V>> {
let _ = key;
let _ = value;
panic!()
}
/// Removes a key from the map, returning the value at the key if the key was previously in the map.
///
/// # Example
/// ```rust,creusot
/// use creusot_contracts::{logic::FMap, *};
///
/// let mut map = FMap::new();
/// let res = ghost! {
/// map.insert_ghost(1, 42);
/// let res1 = map.remove_ghost(&1);
/// let res2 = map.remove_ghost(&1);
/// proof_assert!(res1 == Some(42i32));
/// proof_assert!(res2 == None);
/// };
/// ```
#[trusted]
#[pure]
#[ensures(^self == (*self).remove(*key))]
#[ensures(result == (*self).get(*key))]
pub fn remove_ghost(&mut self, key: &K) -> Option<V>
where
V: Sized,
{
let _ = key;
panic!()
}
/// Same as [`Self::remove_ghost`], but for unsized values.
#[trusted]
#[pure]
#[ensures(^self == (*self).remove(*key))]
#[ensures(result == (*self).get_unsized(*key))]
pub fn remove_ghost_unsized(&mut self, key: &K) -> Option<Box<V>> {
let _ = key;
panic!()
}
}
impl<K: Clone + Copy, V: Clone + Copy> Clone for FMap<K, V> {
#[pure]
#[ensures(result == *self)]
#[trusted]
fn clone(&self) -> Self {
*self
}
}
// Having `Copy` guarantees that the operation is pure, even if we decide to change the definition of `Clone`.
impl<K: Clone + Copy, V: Clone + Copy> Copy for FMap<K, V> {}
impl<K, V: ?Sized> Invariant for FMap<K, V> {
#[predicate(prophetic)]
#[open]
#[creusot::trusted_ignore_structural_inv]
#[creusot::trusted_is_tyinv_trivial_if_param_trivial]
fn invariant(self) -> bool {
pearlite! { forall<k: K> self.contains(k) ==> inv(k) && inv(self.lookup_unsized(k)) }
}
}