#[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_logic(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 add(self, k: K, v: V) -> Self {
dead
}
#[logic(open)]
pub fn singleton(k: K, v: V) -> Self {
Self::empty().add(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 lookup(self, k: K) -> Option<V> {
self.to_mapping().get(k)
}
#[logic(open, inline)]
pub fn contains_logic(self, k: K) -> bool {
self.lookup(k) != None
}
#[logic(open)]
#[ensures(result == (self.len() == 0))]
pub fn is_empty_logic(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.lookup(k) == self.lookup(k)
}
}
#[trusted]
#[logic(opaque)]
#[requires(self.disjoint(other))]
#[ensures(forall<k: K> #[trigger(result.lookup(k))] !self.contains(&k) ==> result.lookup(k) == other.lookup(k))]
#[ensures(forall<k: K> #[trigger(result.lookup(k))] !other.contains(&k) ==> result.lookup(k) == self.lookup(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.lookup(k))] result.lookup(k) ==
if other.contains(&k) {
None
} else {
self.lookup(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.lookup(k) == other.lookup(k)
}
}
#[trusted]
#[logic(opaque)]
#[ensures(
forall<k: K> #[trigger(result.lookup(k))]
match (self.lookup(k), m.lookup(k)) {
(None, y) => result.lookup(k) == y,
(x, None) => result.lookup(k) == x,
(Some(x), Some(y)) => result.lookup(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.lookup(k))] result.lookup(k) == match self.lookup(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.lookup(k))] result.lookup(k) == match self.lookup(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.lookup(k))] result.lookup(k) == match self.lookup(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).unwrap_logic()
}
}
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)]
#[logic_alias(self.len_logic())]
pub fn len(&self) -> Int {
panic!()
}
#[trusted]
#[check(ghost)]
#[logic_alias(self.is_empty_logic())]
pub fn is_empty(&self) -> bool {
panic!()
}
#[check(ghost)]
#[logic_alias(self.contains_logic(*key))]
pub fn contains(&self, key: &K) -> bool {
self.get(key).is_some()
}
#[trusted]
#[check(ghost)]
#[ensures(result == self.lookup(*key).map_logic(|v| &v))]
pub fn get(&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).lookup(k) == (^self).lookup(k))]
#[ensures((*self).len() == (^self).len())]
pub fn get_mut(&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).add(*key, ^result.0))]
pub fn split_mut(&mut self, key: &K) -> (&mut V, &mut Self) {
let _ = key;
panic!()
}
#[trusted]
#[check(ghost)]
#[ensures(^self == (*self).add(key, value))]
#[ensures(result == (*self).lookup(key))]
pub fn insert(&mut self, key: K, value: V) -> Option<V> {
let _ = key;
let _ = value;
panic!()
}
#[trusted]
#[check(ghost)]
#[ensures(^self == (*self).remove(*key))]
#[ensures(result == (*self).lookup(*key))]
pub fn delete(&mut self, key: &K) -> Option<V> {
let _ = key;
panic!()
}
#[trusted]
#[check(ghost)]
#[ensures(^self == Self::empty())]
pub fn clear(&mut self) {}
#[trusted]
#[check(ghost)]
#[ensures(match result {
None => *self == ^self && self.is_empty(),
Some((k, v)) => *self == (^self).add(k, v) && !(^self).contains(&k),
})]
pub fn remove_one(&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.lookup(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.lookup(k) == Some(v)) == (result.lookup(&k) == Some(&v)))]
pub fn as_ref(&self) -> FMap<&K, &V> {
panic!()
}
#[trusted]
#[check(ghost)]
#[ensures(result.len() == self.len())]
#[ensures((^self).len() == self.len())]
#[ensures(forall<k> match result.lookup(&k) {
None => !(*self).contains(&k) && !(^self).contains(&k),
Some(v) => (*self).lookup(k) == Some(*v) && (^self).lookup(k) == Some(^v),
})]
pub fn as_mut(&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.lookup(*key))]
fn index(&self, key: &'a K) -> &Self::Output {
self.get(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)@.add(k, v),
})]
fn next(&mut self) -> Option<(K, V)> {
self.0.remove_one()
}
}
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@.lookup(k) == Some(v)) &&
self@.len() == visited.len() + o@.len() &&
(forall<k> (forall<i> 0 <= i && i < visited.len() ==> visited[i].0 != k) ==> o@.lookup(k) == self@.lookup(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@.lookup(k) == b@.lookup(k) && b@.lookup(k) == c@.lookup(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.lookup(k) == Some(v)) == (result@.lookup(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.lookup(k) == Some(v)) == (result@.lookup(&k) == Some(&v)))]
fn into_iter(self) -> Self::IntoIter {
Iter(self.as_ref())
}
}
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).lookup(k) == Some(v) ==> exists<w> result@.lookup(&k) == Some(w) && v == *w && (^self).lookup(k) == Some(^w))]
#[ensures(forall<k, w> result@.lookup(&k) == Some(w) ==> (*self).lookup(k) == Some(*w) && (^self).lookup(k) == Some(^w))]
fn into_iter(self) -> Self::IntoIter {
Iter(self.as_mut())
}
}
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.lookup(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) {}
}