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<i, j> 0 <= i && i < self.len() && 0 <= j && j < self.len() && result[i].0 == result[j].0 ==> i == j)]
486 #[ensures(forall<k, v> (self.get(k) == Some(v)) == result.contains((k, v)))]
487 pub fn to_seq(self) -> Seq<(K, V)> {
488 panic!()
489 }
490
491 #[trusted]
492 #[check(ghost)]
493 #[ensures(result.len() == self.len())]
494 #[ensures(forall<k, v> (self.get(k) == Some(v)) == (result.get(&k) == Some(&v)))]
495 pub fn as_ref_ghost(&self) -> FMap<&K, &V> {
496 panic!()
497 }
498
499 #[trusted]
500 #[check(ghost)]
501 #[ensures(result.len() == self.len())]
502 #[ensures((^self).len() == self.len())]
503 #[ensures(forall<k> match result.get(&k) {
504 None => !(*self).contains(k) && !(^self).contains(k),
505 Some(v) => (*self).get(k) == Some(*v) && (^self).get(k) == Some(^v),
506 })]
507 pub fn as_mut_ghost(&mut self) -> FMap<&K, &mut V> {
508 panic!()
509 }
510}
511
512impl<'a, K, V> core::ops::Index<&'a K> for FMap<K, V> {
513 type Output = V;
514
515 #[check(ghost)]
516 #[requires(self.contains(*key))]
517 #[ensures(Some(*result) == self.get(*key))]
518 fn index(&self, key: &'a K) -> &Self::Output {
519 self.get_ghost(key).unwrap()
520 }
521}
522
523impl<K: Clone + Copy, V: Clone + Copy> Clone for FMap<K, V> {
524 #[trusted]
525 #[check(ghost)]
526 #[ensures(result == *self)]
527 fn clone(&self) -> Self {
528 *self
529 }
530}
531
532impl<K: Clone + Copy, V: Clone + Copy> Copy for FMap<K, V> {}
534
535impl<K, V> Invariant for FMap<K, V> {
536 #[logic(open, prophetic, inline)]
537 #[creusot::trusted_trivial_if_param_trivial]
538 fn invariant(self) -> bool {
539 pearlite! { forall<k: K> self.contains(k) ==> inv(k) && inv(self[k]) }
540 }
541}
542
543pub struct Iter<K, V>(FMap<K, V>);
545
546impl<K, V> View for Iter<K, V> {
547 type ViewTy = FMap<K, V>;
548
549 #[logic]
550 fn view(self) -> Self::ViewTy {
551 self.0
552 }
553}
554
555impl<K, V> Iterator for Iter<K, V> {
556 type Item = (K, V);
557
558 #[check(ghost)]
559 #[ensures(match result {
560 None => self.completed(),
561 Some((k, v)) => (*self).produces(Seq::singleton((k, v)), ^self) && (*self)@ == (^self)@.insert(k, v),
562 })]
563 fn next(&mut self) -> Option<(K, V)> {
564 self.0.remove_one_ghost()
565 }
566}
567
568impl<K, V> IteratorSpec for Iter<K, V> {
569 #[logic(prophetic, open)]
570 fn produces(self, visited: Seq<(K, V)>, o: Self) -> bool {
571 pearlite! {
572 (forall<i, j> 0 <= i && i < j && j < visited.len() ==> visited[i].0 != visited[j].0) &&
574 (forall<k, v, i> visited.get(i) == Some((k, v)) ==> !o@.contains(k) && self@.get(k) == Some(v)) &&
576 self@.len() == visited.len() + o@.len() &&
578 (forall<k> (forall<i> 0 <= i && i < visited.len() ==> visited[i].0 != k) ==> o@.get(k) == self@.get(k))
580 }
581 }
582
583 #[logic(prophetic, open)]
584 fn completed(&mut self) -> bool {
585 pearlite! { self@.is_empty() }
586 }
587
588 #[logic(law)]
589 #[ensures(self.produces(Seq::empty(), self))]
590 fn produces_refl(self) {}
591
592 #[logic(law)]
593 #[requires(a.produces(ab, b))]
594 #[requires(b.produces(bc, c))]
595 #[ensures(a.produces(ab.concat(bc), c))]
596 fn produces_trans(a: Self, ab: Seq<Self::Item>, b: Self, bc: Seq<Self::Item>, c: Self) {
597 let ac = ab.concat(bc);
598 proof_assert!(forall<x> ab.contains(x) ==> ac.contains(x));
599 proof_assert!(forall<i> 0 <= i && i < bc.len() ==> ac[i + ab.len()] == bc[i]);
600 proof_assert!(forall<k> (forall<i> 0 <= i && i < ac.len() ==> ac[i].0 != k) ==> {
601 (forall<i> 0 <= i && i < ab.len() ==> ab[i].0 != k) &&
602 (forall<i> 0 <= i && i < bc.len() ==> bc[i].0 != k) &&
603 a@.get(k) == b@.get(k) && b@.get(k) == c@.get(k)
604 });
605 }
606}
607
608impl<K, V> IntoIterator for FMap<K, V> {
609 type Item = (K, V);
610 type IntoIter = Iter<K, V>;
611
612 #[check(ghost)]
613 #[ensures(result@.len() == self.len())]
614 #[ensures(forall<k, v> (self.get(k) == Some(v)) == (result@.get(k) == Some(v)))]
615 fn into_iter(self) -> Self::IntoIter {
616 Iter(self)
617 }
618}
619
620impl<'a, K, V> IntoIterator for &'a FMap<K, V> {
621 type Item = (&'a K, &'a V);
622 type IntoIter = Iter<&'a K, &'a V>;
623
624 #[check(ghost)]
625 #[ensures(result@.len() == self.len())]
626 #[ensures(forall<k, v> (self.get(k) == Some(v)) == (result@.get(&k) == Some(&v)))]
627 fn into_iter(self) -> Self::IntoIter {
628 Iter(self.as_ref_ghost())
629 }
630}
631
632impl<'a, K, V> IntoIterator for &'a mut FMap<K, V> {
633 type Item = (&'a K, &'a mut V);
634 type IntoIter = Iter<&'a K, &'a mut V>;
635
636 #[check(ghost)]
637 #[ensures(result@.len() == (*self).len())]
638 #[ensures((^self).len() == (*self).len())]
639 #[ensures(forall<k, v> (*self).get(k) == Some(v) ==> exists<w> result@.get(&k) == Some(w) && v == *w && (^self).get(k) == Some(^w))]
640 #[ensures(forall<k, w> result@.get(&k) == Some(w) ==> (*self).get(k) == Some(*w) && (^self).get(k) == Some(^w))]
641 fn into_iter(self) -> Self::IntoIter {
642 Iter(self.as_mut_ghost())
643 }
644}
645
646impl<K, V> Resolve for FMap<K, V> {
647 #[logic(open, prophetic)]
648 #[creusot::trusted_trivial_if_param_trivial]
649 fn resolve(self) -> bool {
650 pearlite! { forall<k: K, v: V> self.get(k) == Some(v) ==> resolve(k) && resolve(v) }
651 }
652
653 #[trusted]
654 #[logic(prophetic)]
655 #[requires(structural_resolve(self))]
656 #[ensures(self.resolve())]
657 fn resolve_coherence(self) {}
658}
659
660impl<K, V> Resolve for Iter<K, V> {
661 #[logic(open, prophetic, inline)]
662 #[creusot::trusted_trivial_if_param_trivial]
663 fn resolve(self) -> bool {
664 pearlite! { resolve(self@) }
665 }
666
667 #[logic(prophetic)]
668 #[requires(structural_resolve(self))]
669 #[ensures(self.resolve())]
670 fn resolve_coherence(self) {}
671}