1#[cfg(creusot)]
4use crate::resolve::structural_resolve;
5use crate::{
6 logic::{FSet, Mapping, ops::IndexLogic},
7 prelude::*,
8};
9use core::marker::PhantomData;
10
11#[opaque]
32pub struct FMap<K, V>(PhantomData<K>, PhantomData<V>);
33
34impl<K, V> FMap<K, V> {
36 #[logic(opaque)]
38 pub fn to_mapping(self) -> Mapping<K, Option<V>> {
39 dead
40 }
41
42 #[trusted]
44 #[logic(opaque)]
45 #[ensures(result.len() == 0)]
46 #[ensures(result.to_mapping() == Mapping::cst(None))]
47 pub fn empty() -> Self {
48 dead
49 }
50
51 #[trusted]
53 #[logic(opaque)]
54 #[ensures(result >= 0)]
55 pub fn len(self) -> Int {
56 dead
57 }
58
59 #[trusted]
61 #[logic(opaque)]
62 #[ensures(result.to_mapping() == self.to_mapping().set(k, Some(v)))]
63 #[ensures(result.len() == if self.contains(k) { self.len() } else { self.len() + 1 })]
64 pub fn insert(self, k: K, v: V) -> Self {
65 dead
66 }
67
68 #[logic(open)]
70 pub fn singleton(k: K, v: V) -> Self {
71 Self::empty().insert(k, v)
72 }
73
74 #[trusted]
76 #[logic(opaque)]
77 #[ensures(result.to_mapping() == self.to_mapping().set(k, None))]
78 #[ensures(result.len() == if self.contains(k) {self.len() - 1} else {self.len()})]
79 pub fn remove(self, k: K) -> Self {
80 dead
81 }
82
83 #[logic(open, inline)]
87 pub fn get(self, k: K) -> Option<V> {
88 self.to_mapping().get(k)
89 }
90
91 #[logic(open, inline)]
95 pub fn lookup(self, k: K) -> V {
96 self.get(k).unwrap_logic()
97 }
98
99 #[logic(open, inline)]
101 pub fn contains(self, k: K) -> bool {
102 self.get(k) != None
103 }
104
105 #[logic(open)]
107 pub fn is_empty(self) -> bool {
108 self.ext_eq(FMap::empty())
109 }
110
111 #[logic(open)]
113 pub fn disjoint(self, other: Self) -> bool {
114 pearlite! {forall<k: K> !self.contains(k) || !other.contains(k)}
115 }
116
117 #[logic(open)]
119 pub fn subset(self, other: Self) -> bool {
120 pearlite! {
121 forall<k: K> self.contains(k) ==> other.get(k) == self.get(k)
122 }
123 }
124
125 #[trusted]
129 #[logic(opaque)]
130 #[requires(self.disjoint(other))]
131 #[ensures(forall<k: K> #[trigger(result.get(k))] !self.contains(k) ==> result.get(k) == other.get(k))]
132 #[ensures(forall<k: K> #[trigger(result.get(k))] !other.contains(k) ==> result.get(k) == self.get(k))]
133 #[ensures(result.len() == self.len() + other.len())]
134 pub fn union(self, other: Self) -> Self {
135 dead
136 }
137
138 #[trusted]
141 #[logic(opaque)]
142 #[ensures(result.disjoint(other))]
143 #[ensures(other.subset(self) ==> other.union(result) == self)]
144 #[ensures(forall<k: K> #[trigger(result.get(k))] result.get(k) ==
145 if other.contains(k) {
146 None
147 } else {
148 self.get(k)
149 }
150 )]
151 pub fn subtract(self, other: Self) -> Self {
152 dead
153 }
154
155 #[logic]
157 #[trusted]
158 #[requires(self.to_mapping() == other.to_mapping())]
159 #[ensures(self == other)]
160 fn to_mapping_inj(self, other: Self) {}
161
162 #[logic(open)]
168 #[ensures(result == (self == other))]
169 pub fn ext_eq(self, other: Self) -> bool {
170 pearlite! {
171 let _ = Self::to_mapping_inj;
172 forall<k: K> self.get(k) == other.get(k)
173 }
174 }
175
176 #[trusted]
180 #[logic(opaque)]
181 #[ensures(
182 forall<k: K> #[trigger(result.get(k))]
183 match (self.get(k), m.get(k)) {
184 (None, y) => result.get(k) == y,
185 (x, None) => result.get(k) == x,
186 (Some(x), Some(y)) => result.get(k) == Some(f[(x, y)]),
187 }
188 )]
189 pub fn merge(self, m: FMap<K, V>, f: Mapping<(V, V), V>) -> FMap<K, V> {
190 dead
191 }
192
193 #[logic]
195 #[trusted] #[ensures(forall<k: K> #[trigger(result.get(k))] result.get(k) == match self.get(k) {
197 None => None,
198 Some(v) => Some(f[(k, v)]),
199 })]
200 #[ensures(result.len() == self.len())]
201 pub fn map<V2>(self, f: Mapping<(K, V), V2>) -> FMap<K, V2> {
202 self.filter_map(|(k, v)| Some(f[(k, v)]))
203 }
204
205 #[logic]
210 #[ensures(forall<k: K> #[trigger(result.get(k))] result.get(k) == match self.get(k) {
211 None => None,
212 Some(v) => if p[(k, v)] { Some(v) } else { None },
213 })]
214 pub fn filter(self, p: Mapping<(K, V), bool>) -> Self {
215 self.filter_map(|(k, v)| if p[(k, v)] { Some(v) } else { None })
216 }
217
218 #[trusted]
221 #[logic(opaque)]
222 #[ensures(forall<k: K> #[trigger(result.get(k))] result.get(k) == match self.get(k) {
223 None => None,
224 Some(v) => f[(k, v)],
225 })]
226 pub fn filter_map<V2>(self, f: Mapping<(K, V), Option<V2>>) -> FMap<K, V2> {
227 dead
228 }
229
230 #[trusted]
232 #[logic(opaque)]
233 #[ensures(forall<k: K> result.contains(k) == self.contains(k))]
234 #[ensures(result.len() == self.len())]
235 pub fn keys(self) -> FSet<K> {
236 dead
237 }
238}
239
240impl<K, V> IndexLogic<K> for FMap<K, V> {
241 type Item = V;
242
243 #[logic(open, inline)]
244 fn index_logic(self, key: K) -> Self::Item {
245 self.lookup(key)
246 }
247}
248
249impl<K, V> FMap<K, V> {
251 #[trusted]
253 #[check(ghost)]
254 #[ensures(result.is_empty())]
255 #[allow(unreachable_code)]
256 pub fn new() -> Ghost<Self> {
257 Ghost::conjure()
258 }
259
260 #[trusted]
280 #[check(ghost)]
281 #[ensures(result == self.len())]
282 pub fn len_ghost(&self) -> Int {
283 panic!()
284 }
285
286 #[trusted]
287 #[check(ghost)]
288 #[ensures(result == (self.len() == 0 && forall<k> !self.contains(k)))]
289 pub fn is_empty_ghost(&self) -> bool {
290 panic!()
291 }
292
293 #[check(ghost)]
308 #[ensures(result == self.contains(*key))]
309 pub fn contains_ghost(&self, key: &K) -> bool {
310 self.get_ghost(key).is_some()
311 }
312
313 #[trusted]
329 #[check(ghost)]
330 #[ensures(result == self.get(*key).map_logic(|v|&v))]
331 pub fn get_ghost(&self, key: &K) -> Option<&V> {
332 let _ = key;
333 panic!()
334 }
335
336 #[trusted]
352 #[check(ghost)]
353 #[ensures(if self.contains(*key) {
354 match result {
355 None => false,
356 Some(r) =>
357 (^self).contains(*key) && self[*key] == *r && (^self)[*key] == ^r,
358 }
359 } else {
360 result == None && *self == ^self
361 })]
362 #[ensures(forall<k: K> k != *key ==> (*self).get(k) == (^self).get(k))]
363 #[ensures((*self).len() == (^self).len())]
364 pub fn get_mut_ghost(&mut self, key: &K) -> Option<&mut V> {
365 let _ = key;
366 panic!()
367 }
368
369 #[trusted]
390 #[check(ghost)]
391 #[requires(self.contains(*key))]
392 #[ensures(*result.1 == (*self).remove(*key))]
393 #[ensures(self[*key] == *result.0 && ^self == (^result.1).insert(*key, ^result.0))]
394 pub fn split_mut_ghost(&mut self, key: &K) -> (&mut V, &mut Self) {
395 let _ = key;
396 panic!()
397 }
398
399 #[trusted]
419 #[check(ghost)]
420 #[ensures(^self == (*self).insert(key, value))]
421 #[ensures(result == (*self).get(key))]
422 pub fn insert_ghost(&mut self, key: K, value: V) -> Option<V> {
423 let _ = key;
424 let _ = value;
425 panic!()
426 }
427
428 #[trusted]
444 #[check(ghost)]
445 #[ensures(^self == (*self).remove(*key))]
446 #[ensures(result == (*self).get(*key))]
447 pub fn remove_ghost(&mut self, key: &K) -> Option<V> {
448 let _ = key;
449 panic!()
450 }
451
452 #[trusted]
468 #[check(ghost)]
469 #[ensures(^self == Self::empty())]
470 pub fn clear_ghost(&mut self) {}
471
472 #[trusted]
473 #[check(ghost)]
474 #[ensures(match result {
475 None => *self == ^self && self.is_empty(),
476 Some((k, v)) => *self == (^self).insert(k, v) && !(^self).contains(k),
477 })]
478 pub fn remove_one_ghost(&mut self) -> Option<(K, V)> {
479 panic!()
480 }
481
482 #[trusted]
483 #[check(ghost)]
484 #[ensures(result.len() == self.len())]
485 #[ensures(forall<k, v> (self.get(k) == Some(v)) == (exists<i> result.get(i) == Some((k, v))))]
486 pub fn to_seq(self) -> Seq<(K, V)> {
487 panic!()
488 }
489
490 #[trusted]
491 #[check(ghost)]
492 #[ensures(result.len() == self.len())]
493 #[ensures(forall<k, v> (self.get(k) == Some(v)) == (result.get(&k) == Some(&v)))]
494 pub fn as_ref_ghost(&self) -> FMap<&K, &V> {
495 panic!()
496 }
497
498 #[trusted]
499 #[check(ghost)]
500 #[ensures(result.len() == self.len())]
501 #[ensures(forall<k> match result.get(&k) {
502 None => !(*self).contains(k) && !(^self).contains(k),
503 Some(v) => (*self).get(k) == Some(*v) && (^self).get(k) == Some(^v),
504 })]
505 pub fn as_mut_ghost(&mut self) -> FMap<&K, &mut V> {
506 panic!()
507 }
508}
509
510impl<'a, K, V> core::ops::Index<&'a K> for FMap<K, V> {
511 type Output = V;
512
513 #[check(ghost)]
514 #[requires(self.contains(*key))]
515 #[ensures(Some(*result) == self.get(*key))]
516 fn index(&self, key: &'a K) -> &Self::Output {
517 self.get_ghost(key).unwrap()
518 }
519}
520
521impl<K: Clone + Copy, V: Clone + Copy> Clone for FMap<K, V> {
522 #[trusted]
523 #[check(ghost)]
524 #[ensures(result == *self)]
525 fn clone(&self) -> Self {
526 *self
527 }
528}
529
530impl<K: Clone + Copy, V: Clone + Copy> Copy for FMap<K, V> {}
532
533impl<K, V> Invariant for FMap<K, V> {
534 #[logic(open, prophetic, inline)]
535 #[creusot::trusted_trivial_if_param_trivial]
536 fn invariant(self) -> bool {
537 pearlite! { forall<k: K> self.contains(k) ==> inv(k) && inv(self[k]) }
538 }
539}
540
541impl<K, V> Iterator for FMap<K, V> {
542 type Item = (K, V);
543
544 #[check(ghost)]
545 #[ensures(match result {
546 None => self.completed(),
547 Some((k, v)) => (*self).produces(Seq::singleton((k, v)), ^self) && (*self) == (^self).insert(k, v),
548 })]
549 fn next(&mut self) -> Option<(K, V)> {
550 self.remove_one_ghost()
551 }
552}
553
554impl<K, V> IteratorSpec for FMap<K, V> {
555 #[logic(prophetic, open)]
556 fn produces(self, visited: Seq<(K, V)>, o: Self) -> bool {
557 pearlite! {
558 (forall<i, j> 0 <= i && i < j && j < visited.len() ==> visited[i].0 != visited[j].0) &&
560 (forall<k, v, i> visited.get(i) == Some((k, v)) ==> !o.contains(k) && self.get(k) == Some(v)) &&
562 self.len() == visited.len() + o.len() &&
564 (forall<k> (forall<i> 0 <= i && i < visited.len() ==> visited[i].0 != k) ==> o.get(k) == self.get(k))
566 }
567 }
568
569 #[logic(prophetic, open)]
570 fn completed(&mut self) -> bool {
571 pearlite! { self.is_empty() }
572 }
573
574 #[logic(law)]
575 #[ensures(self.produces(Seq::empty(), self))]
576 fn produces_refl(self) {}
577
578 #[logic(law)]
579 #[requires(a.produces(ab, b))]
580 #[requires(b.produces(bc, c))]
581 #[ensures(a.produces(ab.concat(bc), c))]
582 fn produces_trans(a: Self, ab: Seq<Self::Item>, b: Self, bc: Seq<Self::Item>, c: Self) {
583 let ac = ab.concat(bc);
584 proof_assert!(forall<x> ab.contains(x) ==> ac.contains(x));
585 proof_assert!(forall<i> 0 <= i && i < bc.len() ==> ac[i + ab.len()] == bc[i]);
586 proof_assert!(forall<k> (forall<i> 0 <= i && i < ac.len() ==> ac[i].0 != k) ==> {
587 (forall<i> 0 <= i && i < ab.len() ==> ab[i].0 != k) &&
588 (forall<i> 0 <= i && i < bc.len() ==> bc[i].0 != k) &&
589 a.get(k) == b.get(k) && b.get(k) == c.get(k)
590 });
591 }
592}
593
594impl<'a, K, V> IntoIterator for &'a FMap<K, V> {
595 type Item = (&'a K, &'a V);
596 type IntoIter = FMap<&'a K, &'a V>;
597
598 #[check(ghost)]
599 #[ensures(result.len() == self.len())]
600 #[ensures(forall<k, v> (self.get(k) == Some(v)) == (result.get(&k) == Some(&v)))]
601 fn into_iter(self) -> FMap<&'a K, &'a V> {
602 self.as_ref_ghost()
603 }
604}
605
606impl<K, V> Resolve for FMap<K, V> {
607 #[logic(open, prophetic)]
608 #[creusot::trusted_trivial_if_param_trivial]
609 fn resolve(self) -> bool {
610 pearlite! { forall<k: K, v: V> self.get(k) == Some(v) ==> resolve(k) && resolve(v) }
611 }
612
613 #[trusted]
614 #[logic(open(self), prophetic)]
615 #[requires(structural_resolve(self))]
616 #[ensures(self.resolve())]
617 fn resolve_coherence(self) {}
618}