#[cfg(creusot)]
use crate::resolve::structural_resolve;
use crate::{
logic::{FSet, Mapping, ops::IndexLogic},
prelude::*,
};
use core::marker::PhantomData;
#[opaque]
pub struct FMap<K, V>(PhantomData<K>, PhantomData<V>);
impl<K, V> FMap<K, V> {
#[logic(opaque)]
pub fn to_mapping(self) -> Mapping<K, Option<V>> {
dead
}
#[trusted]
#[logic(opaque)]
#[ensures(result.len() == 0)]
#[ensures(result.to_mapping() == Mapping::cst(None))]
pub fn empty() -> Self {
dead
}
#[trusted]
#[logic(opaque)]
#[ensures(result >= 0)]
pub fn len(self) -> Int {
dead
}
#[trusted]
#[logic(opaque)]
#[ensures(result.to_mapping() == self.to_mapping().set(k, Some(v)))]
#[ensures(result.len() == if self.contains(k) { self.len() } else { self.len() + 1 })]
pub fn insert(self, k: K, v: V) -> Self {
dead
}
#[logic(open)]
pub fn singleton(k: K, v: V) -> Self {
Self::empty().insert(k, v)
}
#[trusted]
#[logic(opaque)]
#[ensures(result.to_mapping() == self.to_mapping().set(k, None))]
#[ensures(result.len() == if self.contains(k) {self.len() - 1} else {self.len()})]
pub fn remove(self, k: K) -> Self {
dead
}
#[logic(open, inline)]
pub fn get(self, k: K) -> Option<V> {
self.to_mapping().get(k)
}
#[logic(open, inline)]
pub fn lookup(self, k: K) -> V {
self.get(k).unwrap_logic()
}
#[logic(open, inline)]
pub fn contains(self, k: K) -> bool {
self.get(k) != None
}
#[logic(open)]
#[ensures(result == (self.len() == 0))]
pub fn is_empty(self) -> bool {
proof_assert!(forall<k> self.contains(k) ==> self.len() == self.remove(k).len() + 1);
self.ext_eq(FMap::empty())
}
#[logic(open)]
pub fn disjoint(self, other: Self) -> bool {
pearlite! {forall<k: K> !self.contains(k) || !other.contains(k)}
}
#[logic(open)]
pub fn subset(self, other: Self) -> bool {
pearlite! {
forall<k: K> self.contains(k) ==> other.get(k) == self.get(k)
}
}
#[trusted]
#[logic(opaque)]
#[requires(self.disjoint(other))]
#[ensures(forall<k: K> #[trigger(result.get(k))] !self.contains(k) ==> result.get(k) == other.get(k))]
#[ensures(forall<k: K> #[trigger(result.get(k))] !other.contains(k) ==> result.get(k) == self.get(k))]
#[ensures(result.len() == self.len() + other.len())]
pub fn union(self, other: Self) -> Self {
dead
}
#[trusted]
#[logic(opaque)]
#[ensures(result.disjoint(other))]
#[ensures(other.subset(self) ==> other.union(result) == self)]
#[ensures(forall<k: K> #[trigger(result.get(k))] result.get(k) ==
if other.contains(k) {
None
} else {
self.get(k)
}
)]
pub fn subtract(self, other: Self) -> Self {
dead
}
#[logic]
#[trusted]
#[requires(self.to_mapping() == other.to_mapping())]
#[ensures(self == other)]
fn to_mapping_inj(self, other: Self) {}
#[logic(open)]
#[ensures(#[trigger(self == other)] result == (self == other))]
pub fn ext_eq(self, other: Self) -> bool {
pearlite! {
let _ = Self::to_mapping_inj;
forall<k: K> self.get(k) == other.get(k)
}
}
#[trusted]
#[logic(opaque)]
#[ensures(
forall<k: K> #[trigger(result.get(k))]
match (self.get(k), m.get(k)) {
(None, y) => result.get(k) == y,
(x, None) => result.get(k) == x,
(Some(x), Some(y)) => result.get(k) == Some(f[(x, y)]),
}
)]
pub fn merge(self, m: FMap<K, V>, f: Mapping<(V, V), V>) -> FMap<K, V> {
dead
}
#[logic]
#[trusted] #[ensures(forall<k: K> #[trigger(result.get(k))] result.get(k) == match self.get(k) {
None => None,
Some(v) => Some(f[(k, v)]),
})]
#[ensures(result.len() == self.len())]
pub fn map<V2>(self, f: Mapping<(K, V), V2>) -> FMap<K, V2> {
self.filter_map(|(k, v)| Some(f[(k, v)]))
}
#[logic]
#[ensures(forall<k: K> #[trigger(result.get(k))] result.get(k) == match self.get(k) {
None => None,
Some(v) => if p[(k, v)] { Some(v) } else { None },
})]
pub fn filter(self, p: Mapping<(K, V), bool>) -> Self {
self.filter_map(|(k, v)| if p[(k, v)] { Some(v) } else { None })
}
#[trusted]
#[logic(opaque)]
#[ensures(forall<k: K> #[trigger(result.get(k))] result.get(k) == match self.get(k) {
None => None,
Some(v) => f[(k, v)],
})]
pub fn filter_map<V2>(self, f: Mapping<(K, V), Option<V2>>) -> FMap<K, V2> {
dead
}
#[trusted]
#[logic(opaque)]
#[ensures(forall<k: K> result.contains(k) == self.contains(k))]
#[ensures(result.len() == self.len())]
pub fn keys(self) -> FSet<K> {
dead
}
}
impl<K, V> IndexLogic<K> for FMap<K, V> {
type Item = V;
#[logic(open, inline)]
fn index_logic(self, key: K) -> Self::Item {
self.lookup(key)
}
}
impl<K, V> FMap<K, V> {
#[trusted]
#[check(ghost)]
#[ensures(result.is_empty())]
#[allow(unreachable_code)]
pub fn new() -> Ghost<Self> {
Ghost::conjure()
}
#[trusted]
#[check(ghost)]
#[ensures(result == self.len())]
pub fn len_ghost(&self) -> Int {
panic!()
}
#[trusted]
#[check(ghost)]
#[ensures(result == self.is_empty())]
pub fn is_empty_ghost(&self) -> bool {
panic!()
}
#[check(ghost)]
#[ensures(result == self.contains(*key))]
pub fn contains_ghost(&self, key: &K) -> bool {
self.get_ghost(key).is_some()
}
#[trusted]
#[check(ghost)]
#[ensures(result == self.get(*key).map_logic(|v|&v))]
pub fn get_ghost(&self, key: &K) -> Option<&V> {
let _ = key;
panic!()
}
#[trusted]
#[check(ghost)]
#[ensures(if self.contains(*key) {
match result {
None => false,
Some(r) =>
(^self).contains(*key) && self[*key] == *r && (^self)[*key] == ^r,
}
} else {
result == None && *self == ^self
})]
#[ensures(forall<k: K> k != *key ==> (*self).get(k) == (^self).get(k))]
#[ensures((*self).len() == (^self).len())]
pub fn get_mut_ghost(&mut self, key: &K) -> Option<&mut V> {
let _ = key;
panic!()
}
#[trusted]
#[check(ghost)]
#[requires(self.contains(*key))]
#[ensures(*result.1 == (*self).remove(*key))]
#[ensures(self[*key] == *result.0 && ^self == (^result.1).insert(*key, ^result.0))]
pub fn split_mut_ghost(&mut self, key: &K) -> (&mut V, &mut Self) {
let _ = key;
panic!()
}
#[trusted]
#[check(ghost)]
#[ensures(^self == (*self).insert(key, value))]
#[ensures(result == (*self).get(key))]
pub fn insert_ghost(&mut self, key: K, value: V) -> Option<V> {
let _ = key;
let _ = value;
panic!()
}
#[trusted]
#[check(ghost)]
#[ensures(^self == (*self).remove(*key))]
#[ensures(result == (*self).get(*key))]
pub fn remove_ghost(&mut self, key: &K) -> Option<V> {
let _ = key;
panic!()
}
#[trusted]
#[check(ghost)]
#[ensures(^self == Self::empty())]
pub fn clear_ghost(&mut self) {}
#[trusted]
#[check(ghost)]
#[ensures(match result {
None => *self == ^self && self.is_empty(),
Some((k, v)) => *self == (^self).insert(k, v) && !(^self).contains(k),
})]
pub fn remove_one_ghost(&mut self) -> Option<(K, V)> {
panic!()
}
#[trusted]
#[check(ghost)]
#[ensures(result.len() == self.len())]
#[ensures(forall<i, j> 0 <= i && i < self.len() && 0 <= j && j < self.len() && result[i].0 == result[j].0 ==> i == j)]
#[ensures(forall<k, v> (self.get(k) == Some(v)) == result.contains((k, v)))]
pub fn to_seq(self) -> Seq<(K, V)> {
panic!()
}
#[trusted]
#[check(ghost)]
#[ensures(result.len() == self.len())]
#[ensures(forall<k, v> (self.get(k) == Some(v)) == (result.get(&k) == Some(&v)))]
pub fn as_ref_ghost(&self) -> FMap<&K, &V> {
panic!()
}
#[trusted]
#[check(ghost)]
#[ensures(result.len() == self.len())]
#[ensures((^self).len() == self.len())]
#[ensures(forall<k> match result.get(&k) {
None => !(*self).contains(k) && !(^self).contains(k),
Some(v) => (*self).get(k) == Some(*v) && (^self).get(k) == Some(^v),
})]
pub fn as_mut_ghost(&mut self) -> FMap<&K, &mut V> {
panic!()
}
}
impl<'a, K, V> core::ops::Index<&'a K> for FMap<K, V> {
type Output = V;
#[check(ghost)]
#[requires(self.contains(*key))]
#[ensures(Some(*result) == self.get(*key))]
fn index(&self, key: &'a K) -> &Self::Output {
self.get_ghost(key).unwrap()
}
}
impl<K: Clone + Copy, V: Clone + Copy> Clone for FMap<K, V> {
#[trusted]
#[check(ghost)]
#[ensures(result == *self)]
fn clone(&self) -> Self {
*self
}
}
impl<K: Clone + Copy, V: Clone + Copy> Copy for FMap<K, V> {}
impl<K, V> Invariant for FMap<K, V> {
#[logic(open, prophetic, inline)]
#[creusot::trusted_trivial_if_param_trivial]
fn invariant(self) -> bool {
pearlite! { forall<k: K> self.contains(k) ==> inv(k) && inv(self[k]) }
}
}
pub struct Iter<K, V>(FMap<K, V>);
impl<K, V> View for Iter<K, V> {
type ViewTy = FMap<K, V>;
#[logic]
fn view(self) -> Self::ViewTy {
self.0
}
}
impl<K, V> Iterator for Iter<K, V> {
type Item = (K, V);
#[check(ghost)]
#[ensures(match result {
None => self.completed(),
Some((k, v)) => (*self).produces(Seq::singleton((k, v)), ^self) && (*self)@ == (^self)@.insert(k, v),
})]
fn next(&mut self) -> Option<(K, V)> {
self.0.remove_one_ghost()
}
}
impl<K, V> IteratorSpec for Iter<K, V> {
#[logic(prophetic, open)]
fn produces(self, visited: Seq<(K, V)>, o: Self) -> bool {
pearlite! {
(forall<i, j> 0 <= i && i < j && j < visited.len() ==> visited[i].0 != visited[j].0) &&
(forall<k, v, i> visited.get(i) == Some((k, v)) ==> !o@.contains(k) && self@.get(k) == Some(v)) &&
self@.len() == visited.len() + o@.len() &&
(forall<k> (forall<i> 0 <= i && i < visited.len() ==> visited[i].0 != k) ==> o@.get(k) == self@.get(k))
}
}
#[logic(prophetic, open)]
fn completed(&mut self) -> bool {
pearlite! { self@.is_empty() }
}
#[logic(law)]
#[ensures(self.produces(Seq::empty(), self))]
fn produces_refl(self) {}
#[logic(law)]
#[requires(a.produces(ab, b))]
#[requires(b.produces(bc, c))]
#[ensures(a.produces(ab.concat(bc), c))]
fn produces_trans(a: Self, ab: Seq<Self::Item>, b: Self, bc: Seq<Self::Item>, c: Self) {
let ac = ab.concat(bc);
proof_assert!(forall<x> ab.contains(x) ==> ac.contains(x));
proof_assert!(forall<i> 0 <= i && i < bc.len() ==> ac[i + ab.len()] == bc[i]);
proof_assert!(forall<k> (forall<i> 0 <= i && i < ac.len() ==> ac[i].0 != k) ==> {
(forall<i> 0 <= i && i < ab.len() ==> ab[i].0 != k) &&
(forall<i> 0 <= i && i < bc.len() ==> bc[i].0 != k) &&
a@.get(k) == b@.get(k) && b@.get(k) == c@.get(k)
});
}
}
impl<K, V> IntoIterator for FMap<K, V> {
type Item = (K, V);
type IntoIter = Iter<K, V>;
#[check(ghost)]
#[ensures(result@.len() == self.len())]
#[ensures(forall<k, v> (self.get(k) == Some(v)) == (result@.get(k) == Some(v)))]
fn into_iter(self) -> Self::IntoIter {
Iter(self)
}
}
impl<'a, K, V> IntoIterator for &'a FMap<K, V> {
type Item = (&'a K, &'a V);
type IntoIter = Iter<&'a K, &'a V>;
#[check(ghost)]
#[ensures(result@.len() == self.len())]
#[ensures(forall<k, v> (self.get(k) == Some(v)) == (result@.get(&k) == Some(&v)))]
fn into_iter(self) -> Self::IntoIter {
Iter(self.as_ref_ghost())
}
}
impl<'a, K, V> IntoIterator for &'a mut FMap<K, V> {
type Item = (&'a K, &'a mut V);
type IntoIter = Iter<&'a K, &'a mut V>;
#[check(ghost)]
#[ensures(result@.len() == (*self).len())]
#[ensures((^self).len() == (*self).len())]
#[ensures(forall<k, v> (*self).get(k) == Some(v) ==> exists<w> result@.get(&k) == Some(w) && v == *w && (^self).get(k) == Some(^w))]
#[ensures(forall<k, w> result@.get(&k) == Some(w) ==> (*self).get(k) == Some(*w) && (^self).get(k) == Some(^w))]
fn into_iter(self) -> Self::IntoIter {
Iter(self.as_mut_ghost())
}
}
impl<K, V> Resolve for FMap<K, V> {
#[logic(open, prophetic)]
#[creusot::trusted_trivial_if_param_trivial]
fn resolve(self) -> bool {
pearlite! { forall<k: K, v: V> self.get(k) == Some(v) ==> resolve(k) && resolve(v) }
}
#[trusted]
#[logic(prophetic)]
#[requires(structural_resolve(self))]
#[ensures(self.resolve())]
fn resolve_coherence(self) {}
}
impl<K, V> Resolve for Iter<K, V> {
#[logic(open, prophetic, inline)]
#[creusot::trusted_trivial_if_param_trivial]
fn resolve(self) -> bool {
pearlite! { resolve(self@) }
}
#[logic(prophetic)]
#[requires(structural_resolve(self))]
#[ensures(self.resolve())]
fn resolve_coherence(self) {}
}