#![allow(clippy::question_mark)]
use crate::bounds::{Bound, Clock, Constraint};
#[cfg(creusot)]
use crate::bounds::{order_refines, sat};
#[cfg(creusot)]
use crate::dbm::Matrix;
use crate::dbm::{Dbm, Workspace, tighter};
use crate::error::Error;
use crate::ops::constraints::below_le_zero;
#[cfg(creusot)]
use crate::ops::constraints::clock_error;
#[cfg(creusot)]
use crate::ops::constraints::proof::{csat, valid};
use creusot_std::prelude::*;
impl Dbm {
#[check(terminates)]
#[ensures(match result {
Ok(r) => other.variables_logic() == self.variables_logic()
&& r.variables_logic() == self.variables_logic()
&& forall<u: crate::dbm::proof::Val> r.zone(u) == (self.zone(u) && other.zone(u)),
Err(e) => if other.variables_logic() != self.variables_logic() {
match e {
Error::DimensionMismatch { expected, found } =>
expected@ == self.variables_logic() && found@ == other.variables_logic(),
_ => false,
}
} else {
e == Error::Range && !self.is_empty_logic() && !other.is_empty_logic()
&& crate::dbm::proof::mrange_failure(
crate::dbm::proof::bmin_mat(self.mmat(), other.mmat()),
self.variables_logic() + 1)
},
})]
#[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())
==> match result { Ok(_) => true, Err(_) => false })]
pub fn try_intersect(self, other: &Dbm, ws: &mut Workspace) -> Result<Dbm, Error> {
match self.check_variables(other.variables()) {
Ok(()) => {}
Err(e) => return Err(e),
}
snapshot!(self.model());
snapshot!(other.model());
let min = snapshot!(crate::dbm::proof::bmin_mat(self.mmat(), other.mmat()));
let empty = self.empty_like();
if other.is_empty() {
return Ok(empty);
}
let mut m = match self.into_matrix() {
Some(m) => m,
None => return Ok(empty),
};
let w0 = snapshot!(m);
snapshot!(w0.model());
ws.reset_touched(m.dimension());
if !m.meet(other, &mut ws.touched) {
return Ok(empty);
}
snapshot!(Matrix::meet_ready(*w0, *other, m, ws.touched@));
let w1 = snapshot!(m);
let r = m.close_pivots(&ws.touched);
proof_assert!(match r {
Err(_) => {
w1.range_failure_model();
crate::dbm::proof::mrange_failure_meq(w1.mmat(), *min, w1.dim_logic());
true
}
Ok(_) => true,
});
r
}
}
impl Dbm {
#[check(terminates)]
#[ensures(match result {
Ok(b) => other.variables_logic() == self.variables_logic()
&& b == forall<u: crate::dbm::proof::Val> self.zone(u) ==> other.zone(u),
Err(e) => other.variables_logic() != self.variables_logic() && match e {
Error::DimensionMismatch { expected, found } =>
expected@ == self.variables_logic() && found@ == other.variables_logic(),
_ => false,
},
})]
pub fn is_subset_of(&self, other: &Dbm) -> Result<bool, Error> {
match self.check_variables(other.variables()) {
Ok(()) => {}
Err(e) => return Err(e),
}
snapshot!(self.model());
snapshot!(other.model());
if self.is_empty() {
Ok(true)
} else if other.is_empty() {
Ok(false)
} else {
let d = snapshot!(self.variables_logic() + 1);
proof_assert!(forall<i: Int, j: Int> 0 <= i && i < *d && 0 <= j && j < *d ==> {
order_refines(self.at(i, j), other.at(i, j));
(self.at(i, j).deep_model() <= other.at(i, j).deep_model())
== self.mmat()[i][j].le(other.mmat()[i][j])
});
snapshot!(proof::subset_pointwise(self.mmat(), other.mmat(), *d));
Ok(self.entries_le(other))
}
}
#[check(terminates)]
#[ensures(match result {
Ok(b) => valid(*c, self.variables_logic())
&& b == forall<u: crate::dbm::proof::Val> self.zone(u) ==> csat(*c, u),
Err(e) => !valid(*c, self.variables_logic()) && clock_error(*c, self.variables_logic(), e),
})]
pub fn entails(&self, c: &Constraint) -> Result<bool, Error> {
let (i, j) = match self.check_constraint(*c) {
Ok(ij) => ij,
Err(e) => return Err(e),
};
snapshot!(self.model());
if self.is_empty() {
return Ok(true);
}
let e = self.entry(i, j);
let b = c.bound();
snapshot!(order_refines(e, b));
snapshot!(proof::entry_tight(self.mmat(), self.variables_logic() + 1, i@, j@, b@));
Ok(!tighter(b, e))
}
#[check(terminates)]
#[ensures(match result {
Ok(b) => valid(*c, self.variables_logic())
&& b == exists<u: crate::dbm::proof::Val> self.zone(u) && csat(*c, u),
Err(e) => !valid(*c, self.variables_logic()) && clock_error(*c, self.variables_logic(), e),
})]
pub fn intersects_constraint(&self, c: &Constraint) -> Result<bool, Error> {
let (i, j) = match self.check_constraint(*c) {
Ok(ij) => ij,
Err(e) => return Err(e),
};
snapshot!(self.model());
if self.is_empty() {
return Ok(false);
}
snapshot!(proof::meets(self.mmat(), self.variables_logic() + 1, i@, j@, c@.2));
Ok(!below_le_zero(c.bound(), self.entry(j, i)))
}
#[check(terminates)]
#[ensures(match result {
Ok(None) => i@ <= self.variables_logic() && j@ <= self.variables_logic()
&& self.is_empty_logic(),
Ok(Some(b)) => i@ <= self.variables_logic() && j@ <= self.variables_logic()
&& !self.is_empty_logic() && b == self.at(i@, j@)
&& forall<c: crate::bounds::BoundModel> b@.le(c) ==
forall<u: crate::dbm::proof::Val> self.zone(u) ==> sat(c, u[i@] - u[j@]),
Err(e) => match e {
Error::ClockOutOfRange { clock, variables } =>
variables@ == self.variables_logic()
&& if i@ > self.variables_logic() { clock@ == i@ }
else { j@ > self.variables_logic() && clock@ == j@ },
_ => false,
},
})]
pub fn difference_bound(&self, i: Clock, j: Clock) -> Result<Option<Bound>, Error> {
let i = match self.check_clock(i) {
Ok(i) => i,
Err(e) => return Err(e),
};
let j = match self.check_clock(j) {
Ok(j) => j,
Err(e) => return Err(e),
};
snapshot!(self.model());
if self.is_empty() {
return Ok(None);
}
let b = self.entry(i, j);
proof_assert!(forall<c: crate::bounds::BoundModel> {
proof::entry_tight(self.mmat(), self.variables_logic() + 1, i@, j@, c);
true
});
Ok(Some(b))
}
}
#[cfg(creusot)]
pub(crate) mod proof;
#[cfg(test)]
mod tests;