#[cfg(creusot)]
use crate::resolve::structural_resolve;
use crate::{
ghost::Plain,
logic::{Mapping, ops::IndexLogic},
prelude::*,
std::ops::RangeInclusiveExt as _,
};
use core::{
marker::PhantomData,
ops::{Range, RangeFrom, RangeFull, RangeInclusive, RangeTo, RangeToInclusive},
};
#[builtin("seq.Seq.seq")]
pub struct Seq<T>(PhantomData<T>);
impl<T> Seq<T> {
#[logic]
#[builtin("seq.Seq.empty", ascription)]
pub fn empty() -> Self {
dead
}
#[logic]
#[builtin("seq.Seq.create")]
pub fn create(n: Int, mapping: Mapping<Int, T>) -> Self {
let _ = n;
let _ = mapping;
dead
}
#[logic(open)]
pub fn get(self, ix: Int) -> Option<T> {
if 0 <= ix && ix < self.len() { Some(self.index_logic(ix)) } else { None }
}
#[logic]
#[builtin("seq.Seq.get")]
pub fn index_logic_unsized<'a>(self, ix: Int) -> &'a T {
let _ = ix;
dead
}
#[logic]
#[builtin("seq.Seq.([..])")]
pub fn subsequence(self, start: Int, end: Int) -> Self {
let _ = start;
let _ = end;
dead
}
#[logic]
#[builtin("seq.Seq.singleton")]
pub fn singleton(value: T) -> Self {
let _ = value;
dead
}
#[logic(open)]
pub fn tail(self) -> Self {
self.subsequence(1, self.len())
}
#[logic(open)]
pub fn pop_front(self) -> Self {
self.tail()
}
#[logic(open)]
pub fn pop_back(self) -> Self {
self.subsequence(0, self.len() - 1)
}
#[logic]
#[builtin("seq.Seq.length")]
pub fn len(self) -> Int {
dead
}
#[logic]
#[builtin("seq.Seq.set")]
pub fn set(self, ix: Int, x: T) -> Self {
let _ = ix;
let _ = x;
dead
}
#[logic]
#[builtin("seq.Seq.(==)")]
pub fn ext_eq(self, other: Self) -> bool {
let _ = other;
dead
}
#[doc(hidden)]
#[logic]
#[builtin("seq.Seq.cons")]
pub fn cons(_: T, _: Self) -> Self {
dead
}
#[logic(open, inline)]
pub fn push_front(self, x: T) -> Self {
Self::cons(x, self)
}
#[logic]
#[builtin("seq.Seq.snoc")]
pub fn push_back(self, x: T) -> Self {
let _ = x;
dead
}
#[logic]
#[builtin("seq.Seq.(++)")]
pub fn concat(self, other: Self) -> Self {
let _ = other;
dead
}
#[logic]
#[ensures(result.len() == self.len())]
#[ensures(forall<i> 0 <= i && i < self.len() ==> result[i] == m[self[i]])]
#[variant(self.len())]
pub fn map<U>(self, m: Mapping<T, U>) -> Seq<U> {
if self.len() == 0 {
Seq::empty()
} else {
self.tail().map(m).push_front(m.get(*self.index_logic_unsized(0)))
}
}
#[logic(open)]
#[variant(self.len())]
pub fn flat_map<U>(self, other: Mapping<T, Seq<U>>) -> Seq<U> {
if self.len() == 0 {
Seq::empty()
} else {
other.get(*self.index_logic_unsized(0)).concat(self.tail().flat_map(other))
}
}
#[logic]
#[builtin("seq.Reverse.reverse")]
pub fn reverse(self) -> Self {
dead
}
#[logic]
#[ensures(Self::empty().reverse() == Self::empty())]
pub fn reverse_empty() {}
#[logic]
#[requires(0 <= position && position <= self.len())]
#[ensures(result.len() == self.len() + 1)]
#[ensures(forall<i> 0 <= i && i <= self.len() ==>
if i < position {
result[i] == self[i]
} else if i == position {
result[i] == value
} else {
result[i] == self[i - 1]
}
)]
#[variant(position)]
pub fn insert(self, position: Int, value: T) -> Self {
if position == 0 {
self.push_front(value)
} else {
self.pop_front().insert(position - 1, value).push_front(self[0])
}
}
#[logic(open)]
pub fn removed(self, index: Int) -> Self {
pearlite! { self[..index].concat(self[index+1..]) }
}
#[logic(open)]
pub fn permutation_of(self, other: Self) -> bool {
self.permut(other, 0, self.len())
}
#[logic]
#[builtin("seq.Permut.permut")]
pub fn permut(self, other: Self, start: Int, end: Int) -> bool {
let _ = other;
let _ = start;
let _ = end;
dead
}
#[logic]
#[builtin("seq.Permut.exchange")]
pub fn exchange(self, other: Self, i: Int, j: Int) -> bool {
let _ = other;
let _ = i;
let _ = j;
dead
}
#[logic(open)]
pub fn contains(self, x: T) -> bool {
pearlite! { exists<i> 0 <= i && i < self.len() && self[i] == x }
}
#[logic(open)]
pub fn sorted_range(self, start: Int, end: Int) -> bool
where
T: OrdLogic,
{
pearlite! {
forall<i, j> start <= i && i <= j && j < end ==> self[i] <= self[j]
}
}
#[logic(open)]
pub fn sorted(self) -> bool
where
T: OrdLogic,
{
self.sorted_range(0, self.len())
}
#[logic(open)]
#[ensures(forall<a: Seq<T>, b: Seq<T>, x>
a.concat(b).contains(x) == a.contains(x) || b.contains(x))]
pub fn concat_contains() {}
#[logic]
#[ensures(self.concat(other1).concat(other2) == self.concat(other1.concat(other2)))]
pub fn concat_assoc(self, other1: Self, other2: Self) {}
#[logic]
#[ensures(self.concat(Seq::empty()) == self)]
#[ensures(Seq::empty().concat(self) == self)]
pub fn concat_empty(self) {}
#[logic]
#[ensures(self.concat(other).reverse() == other.reverse().concat(self.reverse()))]
pub fn reverse_concat(self, other: Self) {}
}
impl<T> Seq<Seq<T>> {
#[logic(open)]
#[variant(self.len())]
pub fn flatten(self) -> Seq<T> {
if self.len() == 0 {
Seq::empty()
} else {
self.index_logic_unsized(0).concat(self.tail().flatten())
}
}
}
impl<T> Seq<&T> {
#[logic]
#[builtin("identity")]
pub fn to_owned_seq(self) -> Seq<T> {
dead
}
}
impl<T> IndexLogic<Int> for Seq<T> {
type Item = T;
#[logic]
#[builtin("seq.Seq.get")]
fn index_logic(self, _: Int) -> Self::Item {
dead
}
}
impl<T> IndexLogic<Range<Int>> for Seq<T> {
type Item = Seq<T>;
#[logic(open, inline)]
fn index_logic(self, range: Range<Int>) -> Self::Item {
self.subsequence(range.start, range.end)
}
}
impl<T> IndexLogic<RangeInclusive<Int>> for Seq<T> {
type Item = Seq<T>;
#[logic(open, inline)]
fn index_logic(self, range: RangeInclusive<Int>) -> Self::Item {
self.subsequence(range.start_log(), range.end_log() + 1)
}
}
impl<T> IndexLogic<RangeFull> for Seq<T> {
type Item = Seq<T>;
#[logic(open, inline)]
fn index_logic(self, _: RangeFull) -> Self::Item {
self
}
}
impl<T> IndexLogic<RangeFrom<Int>> for Seq<T> {
type Item = Seq<T>;
#[logic(open, inline)]
fn index_logic(self, range: RangeFrom<Int>) -> Self::Item {
self.subsequence(range.start, self.len())
}
}
impl<T> IndexLogic<RangeTo<Int>> for Seq<T> {
type Item = Seq<T>;
#[logic(open, inline)]
fn index_logic(self, range: RangeTo<Int>) -> Self::Item {
self.subsequence(0, range.end)
}
}
impl<T> IndexLogic<RangeToInclusive<Int>> for Seq<T> {
type Item = Seq<T>;
#[logic(open, inline)]
fn index_logic(self, range: RangeToInclusive<Int>) -> Self::Item {
self.subsequence(0, range.end + 1)
}
}
impl<T> Seq<T> {
#[trusted]
#[check(ghost)]
#[ensures(*result == Self::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.len() == 0))]
pub fn is_empty_ghost(&self) -> bool {
panic!()
}
#[trusted]
#[check(ghost)]
#[ensures(^self == self.push_front(x))]
pub fn push_front_ghost(&mut self, x: T) {
let _ = x;
panic!()
}
#[trusted]
#[check(ghost)]
#[ensures(^self == self.push_back(x))]
pub fn push_back_ghost(&mut self, x: T) {
let _ = x;
panic!()
}
#[check(ghost)]
#[ensures(match self.get(index) {
None => result == None,
Some(v) => result == Some(&v),
})]
pub fn get_ghost(&self, index: Int) -> Option<&T> {
if index - index <= index && index < self.len_ghost() {
Some(self.as_refs().extract(index))
} else {
None
}
}
#[check(ghost)]
#[ensures(match result {
None => self.get(index) == None && *self == ^self,
Some(r) => self.get(index) == Some(*r) && ^r == (^self)[index],
})]
#[ensures(forall<i> i != index ==> (*self).get(i) == (^self).get(i))]
#[ensures((*self).len() == (^self).len())]
pub fn get_mut_ghost(&mut self, index: Int) -> Option<&mut T> {
if index - index <= index && index < self.len_ghost() {
Some(self.as_muts().extract(index))
} else {
None
}
}
#[check(ghost)]
#[requires(0 <= position && position <= self.len())]
#[ensures((^self) == self.insert(position, x))]
#[variant(position)]
pub fn insert_ghost(&mut self, position: Int, x: T) {
let after = self.split_off_ghost(position);
self.push_back_ghost(x);
self.extend(after);
}
#[check(ghost)]
#[requires(0 <= index && index < self.len())]
#[ensures(result == self[index])]
#[ensures(forall<i> 0 <= i && i < self.len() && i != index ==> resolve(self[i]))]
pub fn extract(mut self, index: Int) -> T {
proof_assert! { forall<i> index < i && i < self.len() ==> self[i] == self[index + 1..][i - index - 1] }
self.split_off_ghost(index).pop_front_ghost().unwrap()
}
#[check(ghost)]
#[requires(0 <= index && index < self.len())]
#[ensures(result == self[index])]
#[ensures(^self == (*self).removed(index))]
pub fn remove(&mut self, index: Int) -> T {
let mut right = self.split_off_ghost(index);
let result = right.pop_front_ghost().unwrap();
self.extend(right);
result
}
#[check(ghost)]
#[ensures(^self == (*self).concat(rhs))]
pub fn extend(&mut self, mut rhs: Self) {
let _final = snapshot! { self.concat(rhs) };
#[variant(rhs.len())]
#[invariant(self.concat(rhs) == *_final)]
while let Some(x) = rhs.pop_front_ghost() {
self.push_back_ghost(x)
}
}
#[trusted]
#[check(ghost)]
#[ensures(match result {
None => *self == Seq::empty() && *self == ^self,
Some(r) => *self == (^self).push_back(r)
})]
pub fn pop_back_ghost(&mut self) -> Option<T> {
panic!()
}
#[trusted]
#[check(ghost)]
#[ensures(match result {
None => *self == Seq::empty() && *self == ^self,
Some(r) => (*self).len() > 0 && r == (*self)[0] && ^self == (*self).tail()
})]
pub fn pop_front_ghost(&mut self) -> Option<T> {
panic!()
}
#[trusted]
#[check(ghost)]
#[ensures(^self == Self::empty())]
pub fn clear_ghost(&mut self) {}
#[trusted]
#[check(ghost)]
#[requires(0 <= mid && mid <= self.len())]
#[ensures(^self == self[..mid])]
#[ensures(result == self[mid..])]
pub fn split_off_ghost(&mut self, mid: Int) -> Self {
let _ = mid;
panic!("ghost code")
}
#[trusted]
#[check(ghost)]
#[ensures(*self == result.to_owned_seq())]
pub fn as_refs(&self) -> Seq<&T> {
panic!("ghost code")
}
#[trusted]
#[check(ghost)]
#[ensures(result.len() == self.len())]
#[ensures((^self).len() == self.len())]
#[ensures(forall<i> 0 <= i && i < self.len() ==> *result[i] == (*self)[i])]
#[ensures(forall<i> 0 <= i && i < self.len() ==> ^result[i] == (^self)[i])]
pub fn as_muts(&mut self) -> Seq<&mut T> {
panic!("ghost code")
}
}
impl<T> core::ops::Index<Int> for Seq<T> {
type Output = T;
#[check(ghost)]
#[requires(0 <= index && index < self.len())]
#[ensures(*result == self[index])]
fn index(&self, index: Int) -> &Self::Output {
self.get_ghost(index).unwrap()
}
}
impl<T> core::ops::IndexMut<Int> for Seq<T> {
#[check(ghost)]
#[requires(0 <= index && index < self.len())]
#[ensures((*self).len() == (^self).len())]
#[ensures(*result == (*self)[index] && ^result == (^self)[index])]
#[ensures(forall<i> i != index ==> (*self).get(i) == (^self).get(i))]
fn index_mut(&mut self, index: Int) -> &mut Self::Output {
self.get_mut_ghost(index).unwrap()
}
}
impl<T> core::ops::Index<(Int, Int)> for Seq<T> {
type Output = (T, T);
#[trusted]
#[check(ghost)]
#[requires(0 <= index.0 && index.0 < self.len() && 0 <= index.1 && index.1 < self.len())]
#[ensures(result.0 == self[index.0] && result.1 == self[index.1])]
#[allow(unused_variables)]
fn index(&self, index: (Int, Int)) -> &Self::Output {
panic!()
}
}
impl<T> core::ops::IndexMut<(Int, Int)> for Seq<T> {
#[trusted]
#[check(ghost)]
#[requires(0 <= index.0 && index.0 < self.len() && 0 <= index.1 && index.1 < self.len())]
#[requires(index.0 != index.1)]
#[ensures((*result).0 == (*self)[index.0] && (*result).1 == (*self)[index.1]
&& (^result).0 == (^self)[index.0] && (^result).1 == (^self)[index.1])]
#[ensures(forall<i> i != index.0 && i != index.1 ==> (*self).get(i) == (^self).get(i))]
#[ensures((*self).len() == (^self).len())]
#[allow(unused_variables)]
fn index_mut(&mut self, index: (Int, Int)) -> &mut Self::Output {
panic!()
}
}
impl<T: Clone + Copy> Clone for Seq<T> {
#[trusted]
#[check(ghost)]
#[ensures(result == *self)]
fn clone(&self) -> Self {
*self
}
}
impl<T: Copy> Copy for Seq<T> {}
impl<T: Plain> Plain for Seq<T> {
#[ensures(*result == *snap)]
#[check(ghost)]
#[allow(unused_variables)]
fn into_ghost(snap: Snapshot<Self>) -> Ghost<Self> {
ghost! {
let mut res = Seq::new().into_inner();
let len: Snapshot<Int> = snapshot!(snap.len());
let len = len.into_ghost().into_inner();
let mut i = 0int;
#[variant(len - i)]
#[invariant(i <= len)]
#[invariant(res.len() == i)]
#[invariant(forall<j> 0 <= j && j < i ==> res[j] == snap[j])]
while i < len {
let elem: Snapshot<T> = snapshot!(snap[i]);
res.push_back_ghost(elem.into_ghost().into_inner());
i = i + 1int;
}
res
}
}
}
impl<T> Invariant for Seq<T> {
#[logic(open, prophetic, inline)]
#[creusot::trusted_trivial_if_param_trivial]
fn invariant(self) -> bool {
pearlite! { forall<i> 0 <= i && i < self.len() ==> inv(self.index_logic_unsized(i)) }
}
}
impl<T: PartialOrdLogic> PartialOrdLogic for Seq<T> {
#[logic(open)]
fn lt_log(self, other: Self) -> bool {
pearlite! {
(exists<i: Int> 0 <= i && i < self.len() && i < other.len() &&
(forall<j: Int> 0 <= j && j < i ==> self[j] == other[j]) &&
self[i] < other[i])
||
self.len() < other.len() &&
(forall<i: Int> 0 <= i && i < self.len() ==> self[i] == other[i])
}
}
#[logic(law)]
#[ensures(!(self < self))]
fn irreflexive(self) {}
#[logic(law)]
#[requires(x < y)]
#[requires(y < z)]
#[ensures(x < z)]
fn transitive(x: Self, y: Self, z: Self) {}
#[logic(law)]
#[ensures((self <= other) == (self < other || self == other))]
fn le_lt_log(self, other: Self) {}
}
impl<T: OrdLogic> Seq<T> {
#[logic]
#[requires(self.len() > 0 && other.len() > 0)]
#[requires(self[0] == other[0])]
#[ensures((self < other) == (self[1..] < other[1..]))]
fn lt_log_tail(self, other: Self) {}
}
impl<T: OrdLogic> OrdLogic for Seq<T> {
#[logic(law)]
#[ensures(self < other || self == other || other < self)]
#[variant(self.len())]
fn lt_log_total(self, other: Self) {
if self.len() > 0 && other.len() > 0 {
if self[0] == other[0] {
self[1..].lt_log_total(other[1..]);
self.lt_log_tail(other);
other.lt_log_tail(self);
proof_assert!(forall<i> 0 < i && i < self.len() ==> self[i] == self[1..][i-1]);
proof_assert!(forall<i> 0 < i && i < other.len() ==> other[i] == other[1..][i-1]);
} else {
self[0].lt_log_total(other[0]);
}
}
}
}
pub struct Iter<T>(Seq<T>);
impl<T> View for Iter<T> {
type ViewTy = Seq<T>;
#[logic]
fn view(self) -> Self::ViewTy {
self.0
}
}
impl<T> Iterator for Iter<T> {
type Item = T;
#[check(ghost)]
#[ensures(match result {
None => self.completed(),
Some(v) => (*self).produces(Seq::singleton(v), ^self)
})]
fn next(&mut self) -> Option<T> {
self.0.pop_front_ghost()
}
}
impl<T> IteratorSpec for Iter<T> {
#[logic(prophetic, open)]
fn produces(self, visited: Seq<T>, o: Self) -> bool {
pearlite! { self@ == visited.concat(o@) }
}
#[logic(prophetic, open)]
fn completed(&mut self) -> bool {
pearlite! { self@ == Seq::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) {}
}
impl<T> IntoIterator for Seq<T> {
type Item = T;
type IntoIter = Iter<T>;
#[check(ghost)]
#[ensures(self == result@)]
fn into_iter(self) -> Self::IntoIter {
Iter(self)
}
}
impl<'a, T> IntoIterator for &'a Seq<T> {
type Item = &'a T;
type IntoIter = Iter<&'a T>;
#[check(ghost)]
#[ensures(*self == result@.to_owned_seq())]
fn into_iter(self) -> Self::IntoIter {
Iter(self.as_refs())
}
}
impl<'a, T> IntoIterator for &'a mut Seq<T> {
type Item = &'a mut T;
type IntoIter = Iter<&'a mut T>;
#[check(ghost)]
#[ensures(result@.len() == self.len())]
#[ensures((^self).len() == self.len())]
#[ensures(forall<i> 0 <= i && i < self.len() ==> *result@[i] == (*self)[i])]
#[ensures(forall<i> 0 <= i && i < self.len() ==> ^result@[i] == (^self)[i])]
fn into_iter(self) -> Self::IntoIter {
Iter(self.as_muts())
}
}
impl<T> Resolve for Seq<T> {
#[logic(open, prophetic)]
#[creusot::trusted_trivial_if_param_trivial]
fn resolve(self) -> bool {
pearlite! { forall<i : Int> 0 <= i && i < self.len() ==> resolve(self[i]) }
}
#[trusted]
#[logic(prophetic)]
#[requires(structural_resolve(self))]
#[ensures(self.resolve())]
fn resolve_coherence(self) {}
}
impl<T> Resolve for Iter<T> {
#[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) {}
}
impl<T> Seq<T> {
#[logic(open)]
#[ensures(Seq::singleton(x).flat_map(f) == f.get(x))]
pub fn flat_map_singleton<U>(x: T, f: Mapping<T, Seq<U>>) {}
#[logic(open)]
#[ensures(self.push_back(x).flat_map(f) == self.flat_map(f).concat(f.get(x)))]
#[variant(self.len())]
pub fn flat_map_push_back<U>(self, x: T, f: Mapping<T, Seq<U>>) {
if self.len() > 0 {
Self::flat_map_push_back::<U>(self.tail(), x, f);
proof_assert! { self.tail().push_back(x) == self.push_back(x).tail() }
}
}
}