use std::convert::Infallible;
use std::rc::Rc;
use crate::bounds::{Bound, Clock, Constraint, Variable};
#[cfg(creusot)]
use crate::bounds::{le_total, order_refines};
#[cfg(creusot)]
use crate::dbm::proof::{Val, edge_ok};
use crate::dbm::{Dbm, Workspace, tighter};
use crate::error::Error;
#[cfg(creusot)]
use crate::ops::constraints::proof::{all_valid, conj, contradiction, csat, implied, valid};
use crate::ops::constraints::{Strategy, below_le_zero};
#[cfg(creusot)]
use crate::ops::constraints::{all_implied, some_contradiction};
use crate::ops::extra_m::MaxBounds;
#[cfg(creusot)]
use crate::ops::relations::proof::subset_pointwise;
use creusot_std::prelude::*;
#[cfg(creusot)]
mod proof;
#[cfg(creusot)]
mod rc_spec;
#[derive(Debug)]
#[cfg_attr(not(creusot), derive(Hash))]
#[cfg_attr(not(creusot), allow(clippy::derived_hash_with_manual_eq))]
pub struct SharedDbm(Rc<Dbm>);
impl View for SharedDbm {
type ViewTy = Dbm;
#[logic(open(self))]
fn view(self) -> Dbm {
pearlite! { *self.0@ }
}
}
impl Invariant for SharedDbm {
#[logic(open(self))]
fn invariant(self) -> bool {
pearlite! { self@.invariant() }
}
}
impl DeepModel for SharedDbm {
type DeepModelTy = (Int, Seq<Int>);
#[logic(open(self))]
fn deep_model(self) -> (Int, Seq<Int>) {
pearlite! { self@.deep_model() }
}
}
#[logic(open(self))]
fn lift<E>(r: Result<SharedDbm, E>) -> Result<Dbm, E> {
pearlite! {
match r {
Ok(s) => Ok(s@),
Err(e) => Err(e),
}
}
}
#[logic(open(self))]
fn same_outcome(r: Result<Dbm, Error>, s: Result<Dbm, Error>) -> bool {
pearlite! {
match (r, s) {
(Ok(a), Ok(b)) => a.deep_model() == b.deep_model(),
(Err(e), Err(f)) => e == f,
_ => false,
}
}
}
#[logic(prophetic)]
#[ensures(forall<a: Dbm, b: Dbm> inv(a) && inv(b) && a.variables_logic() == b.variables_logic()
&& (forall<u: Val> a.zone(u) == b.zone(u)) ==> a.deep_model() == b.deep_model())]
fn unique_zones() {
pearlite! {
proof_assert!(forall<a: Dbm, b: Dbm> inv(a) && inv(b)
&& a.variables_logic() == b.variables_logic() ==> {
a.eq_semantic(b);
true
});
}
}
impl Clone for SharedDbm {
#[check(terminates)]
#[ensures(result@ == self@)]
fn clone(&self) -> SharedDbm {
SharedDbm(Rc::clone(&self.0))
}
}
impl PartialEq for SharedDbm {
#[ensures(result == (self.deep_model() == other.deep_model()))]
fn eq(&self, other: &SharedDbm) -> bool {
Rc::ptr_eq(&self.0, &other.0) || *self.0 == *other.0
}
}
impl Eq for SharedDbm {}
impl From<Dbm> for SharedDbm {
#[check(terminates)]
#[ensures(result@ == z)]
fn from(z: Dbm) -> SharedDbm {
SharedDbm(Rc::new(z))
}
}
impl AsRef<Dbm> for SharedDbm {
#[check(terminates)]
#[ensures(*result == self@)]
fn as_ref(&self) -> &Dbm {
&self.0
}
}
impl SharedDbm {
#[check(terminates)]
#[ensures(Dbm::zero.postcondition((variables,), lift(result)))]
pub fn zero(variables: usize) -> Result<SharedDbm, Error> {
match Dbm::zero(variables) {
Ok(z) => Ok(SharedDbm::from(z)),
Err(e) => Err(e),
}
}
#[check(terminates)]
#[ensures(Dbm::universe.postcondition((variables,), lift(result)))]
pub fn universe(variables: usize) -> Result<SharedDbm, Error> {
match Dbm::universe(variables) {
Ok(z) => Ok(SharedDbm::from(z)),
Err(e) => Err(e),
}
}
#[check(terminates)]
#[ensures(Dbm::empty.postcondition((variables,), lift(result)))]
pub fn empty(variables: usize) -> Result<SharedDbm, Error> {
match Dbm::empty(variables) {
Ok(z) => Ok(SharedDbm::from(z)),
Err(e) => Err(e),
}
}
#[check(terminates)]
#[ensures(Dbm::try_from_constraints.postcondition((variables, cs, ws), lift(result)))]
pub fn try_from_constraints(
variables: usize,
cs: &[Constraint],
ws: &mut Workspace,
) -> Result<SharedDbm, Error> {
match Dbm::try_from_constraints(variables, cs, ws) {
Ok(z) => Ok(SharedDbm::from(z)),
Err(e) => Err(e),
}
}
#[ensures(result == self@)]
pub fn into_dbm(self) -> Dbm {
Rc::unwrap_or_clone(self.0)
}
#[check(terminates)]
#[ensures(*result == self@)]
pub fn as_dbm(&self) -> &Dbm {
&self.0
}
#[check(terminates)]
#[ensures(Dbm::variables.postcondition((&self@,), result))]
pub fn variables(&self) -> usize {
self.0.variables()
}
#[check(terminates)]
#[ensures(Dbm::dimension.postcondition((&self@,), result))]
pub fn dimension(&self) -> usize {
self.0.dimension()
}
#[check(terminates)]
#[ensures(Dbm::is_empty.postcondition((&self@,), result))]
pub fn is_empty(&self) -> bool {
self.0.is_empty()
}
#[check(terminates)]
#[ensures(Dbm::is_subset_of.postcondition((&self@, &other@), result))]
pub fn is_subset_of(&self, other: &SharedDbm) -> Result<bool, Error> {
self.0.is_subset_of(&other.0)
}
#[check(terminates)]
#[ensures(Dbm::entails.postcondition((&self@, c), result))]
pub fn entails(&self, c: &Constraint) -> Result<bool, Error> {
self.0.entails(c)
}
#[check(terminates)]
#[ensures(Dbm::intersects_constraint.postcondition((&self@, c), result))]
pub fn intersects_constraint(&self, c: &Constraint) -> Result<bool, Error> {
self.0.intersects_constraint(c)
}
#[check(terminates)]
#[ensures(Dbm::difference_bound.postcondition((&self@, i, j), result))]
pub fn difference_bound(&self, i: Clock, j: Clock) -> Result<Option<Bound>, Error> {
self.0.difference_bound(i, j)
}
#[ensures(Dbm::try_constrain.postcondition((self@, c), lift(result)))]
#[ensures(valid(c, self@.variables_logic())
&& (self@.is_empty_logic() || self@.mmat()[c@.0][c@.1].le(c@.2)
|| !edge_ok(self@.mmat(), c@.0, c@.1, c@.2))
==> forall<r: Result<Dbm, Error>> Dbm::try_constrain.postcondition((self@, c), r)
==> same_outcome(r, lift(result)))]
pub fn try_constrain(self, c: Constraint) -> Result<SharedDbm, Error> {
let z = &*self.0;
proof_assert!({
unique_zones();
true
});
let (i, j) = z.check_constraint(c)?;
let b = c.bound();
let f = snapshot!(z.mmat());
let dd = snapshot!(z.variables_logic() + 1);
snapshot!(z.model());
if z.is_empty() || !tighter(b, z.entry(i, j)) {
proof_assert!(z.is_empty_logic() || {
order_refines(b, z.at(i@, j@));
le_total(b@, f[i@][j@]);
implied(*f, *dd, i@, j@, b@);
true
});
return Ok(self);
}
snapshot!(order_refines(b, z.at(i@, j@)));
if below_le_zero(b, z.entry(j, i)) {
snapshot!(contradiction(*f, *dd, i@, j@, b@));
let e = z.empty_like();
snapshot!(e.model());
return Ok(SharedDbm::from(e));
}
self.update(|z| z.try_constrain(c))
}
#[ensures(Dbm::try_constrain_all.postcondition((self@, cs, ws), lift(result)))]
#[ensures(all_valid(cs@, self@.variables_logic())
&& (self@.is_empty_logic() || some_contradiction(self@, cs@)
|| (cs@.len() <= 4 * (self@.variables_logic() + 1) && all_implied(self@, cs@)))
==> forall<r: Result<Dbm, Error>> Dbm::try_constrain_all.postcondition((self@, cs, ws), r)
==> same_outcome(r, lift(result)))]
pub fn try_constrain_all(
self,
cs: &[Constraint],
ws: &mut Workspace,
) -> Result<SharedDbm, Error> {
let z = &*self.0;
proof_assert!({
unique_zones();
true
});
let n = snapshot!(z.variables_logic());
let f = snapshot!(z.mmat());
snapshot!(z.model());
let mut t: usize = 0;
#[invariant(t@ <= cs@.len())]
#[invariant(forall<s: Int> 0 <= s && s < t@ ==> valid(cs@[s], *n))]
#[variant(cs@.len() - t@)]
while t < cs.len() {
z.check_constraint(cs[t])?;
t += 1;
}
if z.is_empty() {
return Ok(self);
}
let mut t: usize = 0;
#[invariant(t@ <= cs@.len())]
#[invariant(forall<s: Int> 0 <= s && s < t@ ==>
edge_ok(*f, cs@[s]@.0, cs@[s]@.1, cs@[s]@.2))]
#[variant(cs@.len() - t@)]
while t < cs.len() {
let c = cs[t];
if below_le_zero(c.bound(), z.entry(c.j().index(), c.i().index())) {
snapshot!(contradiction(*f, *n + 1, c@.0, c@.1, c@.2));
proof_assert!(forall<u: Val> !(z.zone(u) && csat(c, u)));
let e = z.empty_like();
snapshot!(e.model());
return Ok(SharedDbm::from(e));
}
t += 1;
}
if matches!(
Strategy::choose(z.dimension(), cs.len()),
Strategy::Incremental
) {
let mut t: usize = 0;
#[invariant(t@ <= cs@.len())]
#[invariant(forall<u: Val> z.zone(u) ==> conj(cs@, t@, u))]
#[variant(cs@.len() - t@)]
while t < cs.len() {
let c = cs[t];
let b = c.bound();
if tighter(b, z.entry(c.i().index(), c.j().index())) {
break;
}
snapshot!(order_refines(b, z.at(c@.0, c@.1)));
snapshot!(le_total(c@.2, f[c@.0][c@.1]));
snapshot!(implied(*f, *n + 1, c@.0, c@.1, c@.2));
t += 1;
}
if t == cs.len() {
return Ok(self);
}
}
self.update(|z| z.try_constrain_all(cs, ws))
}
#[ensures(Dbm::try_intersect.postcondition((self@, &other@, ws), lift(result)))]
#[ensures(other@.variables_logic() == self@.variables_logic()
&& (self@.is_empty_logic() || other@.is_empty_logic()
|| forall<i: Int, j: Int>
0 <= i && i <= self@.variables_logic() && 0 <= j && j <= self@.variables_logic()
==> self@.at(i, j).deep_model() <= other@.at(i, j).deep_model())
==> forall<r: Result<Dbm, Error>> Dbm::try_intersect.postcondition((self@, &other@, ws), r)
==> same_outcome(r, lift(result)))]
pub fn try_intersect(self, other: &SharedDbm, ws: &mut Workspace) -> Result<SharedDbm, Error> {
let z = &*self.0;
let o = &*other.0;
proof_assert!({
unique_zones();
true
});
z.check_variables(o.variables())?;
snapshot!(z.model());
snapshot!(o.model());
if z.is_empty() {
return Ok(self);
}
if o.is_empty() {
return Ok(SharedDbm::from(z.empty_like()));
}
if Rc::strong_count(&self.0) > 1 && (Rc::ptr_eq(&self.0, &other.0) || z.entries_le(o)) {
let d = snapshot!(z.variables_logic() + 1);
proof_assert!(forall<i: Int, j: Int> 0 <= i && i < *d && 0 <= j && j < *d ==> {
order_refines(z.at(i, j), o.at(i, j));
(z.at(i, j).deep_model() <= o.at(i, j).deep_model())
== z.mmat()[i][j].le(o.mmat()[i][j])
});
snapshot!(subset_pointwise(z.mmat(), o.mmat(), *d));
return Ok(self);
}
self.update(|z| z.try_intersect(o, ws))
}
#[ensures(Dbm::future.postcondition((self@,), result@))]
pub fn future(self) -> SharedDbm {
let Ok(z) = self.update(|z| Ok::<_, Infallible>(z.future()));
z
}
#[ensures(Dbm::past.postcondition((self@,), result@))]
pub fn past(self) -> SharedDbm {
let Ok(z) = self.update(|z| Ok::<_, Infallible>(z.past()));
z
}
#[ensures(Dbm::try_reset_zero.postcondition((self@, x), lift(result)))]
pub fn try_reset_zero(self, x: Variable) -> Result<SharedDbm, Error> {
self.update(|z| z.try_reset_zero(x))
}
#[ensures(Dbm::try_reset.postcondition((self@, x, value), lift(result)))]
pub fn try_reset(self, x: Variable, value: i32) -> Result<SharedDbm, Error> {
self.update(|z| z.try_reset(x, value))
}
#[ensures(Dbm::try_free.postcondition((self@, x), lift(result)))]
pub fn try_free(self, x: Variable) -> Result<SharedDbm, Error> {
self.update(|z| z.try_free(x))
}
#[ensures(Dbm::try_extrapolate_max.postcondition((self@, bounds, ws), lift(result)))]
pub fn try_extrapolate_max(
self,
bounds: &MaxBounds,
ws: &mut Workspace,
) -> Result<SharedDbm, Error> {
self.update(|z| z.try_extrapolate_max(bounds, ws))
}
#[requires(forall<z: Dbm> z == self@ && inv(z) ==> f.precondition((z,)))]
#[ensures(f.postcondition_once((self@,), lift(result)))]
fn update<E, F: FnOnce(Dbm) -> Result<Dbm, E>>(mut self, f: F) -> Result<SharedDbm, E> {
match Rc::get_mut(&mut self.0) {
Some(slot) => {
let placeholder = slot.empty_like();
match f(std::mem::replace(slot, placeholder)) {
Ok(z) => {
*slot = z;
Ok(self)
}
Err(e) => Err(e),
}
}
None => match f(Dbm::clone(&self.0)) {
Ok(z) => Ok(SharedDbm::from(z)),
Err(e) => Err(e),
},
}
}
#[cfg(test)]
pub(crate) fn ptr_eq(&self, other: &SharedDbm) -> bool {
Rc::ptr_eq(&self.0, &other.0)
}
}
#[cfg(test)]
mod tests;