#![allow(clippy::question_mark)]
use crate::bounds::{Bound, MAX_CONSTANT, Variable};
#[cfg(creusot)]
use crate::bounds::{BoundModel, order_refines, view_injective};
#[cfg(creusot)]
use crate::dbm::proof::{meq, meq_transfer, mrange_failure, mrange_failure_meq};
use crate::dbm::{Dbm, MAX_VARIABLES, Matrix, Workspace, same, tighter};
use crate::error::Error;
#[cfg(creusot)]
use creusot_std::logic::Mapping;
use creusot_std::{
invariant::Invariant,
prelude::{Clone, PartialEq, *},
};
#[derive(PartialEq, Eq, Debug)]
#[cfg_attr(not(creusot), derive(Hash))]
pub struct MaxBounds {
ceilings: Box<[i32]>,
}
#[logic(open)]
pub(crate) fn valid_ceiling(c: i32) -> bool {
pearlite! { 0 <= c@ && c@ <= MAX_CONSTANT@ }
}
impl Invariant for MaxBounds {
#[logic(open(self))]
fn invariant(self) -> bool {
pearlite! {
self.ceilings@.len() <= MAX_VARIABLES@
&& forall<p: Int> 0 <= p && p < self.ceilings@.len() ==>
valid_ceiling(self.ceilings@[p])
}
}
}
impl Clone for MaxBounds {
#[ensures(result.ceilings_logic() == self.ceilings_logic())]
fn clone(&self) -> MaxBounds {
let ceilings = self.ceilings.to_vec().into_boxed_slice();
proof_assert!(ceilings@.ext_eq(self.ceilings@));
MaxBounds { ceilings }
}
}
impl DeepModel for MaxBounds {
type DeepModelTy = Seq<Int>;
#[logic(open(self))]
fn deep_model(self) -> Seq<Int> {
pearlite! { *self.ceilings.deep_model() }
}
}
impl MaxBounds {
#[logic(open(self))]
pub(crate) fn ceilings_logic(self) -> Seq<i32> {
pearlite! { self.ceilings@ }
}
#[logic(open(self))]
pub(crate) fn variables_logic(self) -> Int {
pearlite! { self.ceilings@.len() }
}
#[logic(open(self))]
pub(crate) fn kmap(self) -> Mapping<Int, Int> {
pearlite! {
|i: Int| if 1 <= i && i <= self.ceilings@.len() { self.ceilings@[i - 1]@ } else { 0 }
}
}
#[logic]
#[requires(inv(self))]
#[ensures(self.variables_logic() <= MAX_VARIABLES@)]
#[ensures(forall<i: Int> 0 <= self.kmap()[i] && self.kmap()[i] <= MAX_CONSTANT@)]
#[ensures(self.kmap()[0] == 0)]
pub(crate) fn model(self) {}
#[check(terminates)]
#[ensures(match result {
Ok(b) => ceilings@.len() <= MAX_VARIABLES@ && b.ceilings_logic() == ceilings@,
Err(e) => if ceilings@.len() > MAX_VARIABLES@ {
match e {
Error::TooManyVariables { requested } => requested@ == ceilings@.len(),
_ => false,
}
} else {
exists<p: Int> 0 <= p && p < ceilings@.len() && !valid_ceiling(ceilings@[p])
&& (forall<q: Int> 0 <= q && q < p ==> valid_ceiling(ceilings@[q]))
&& match e {
Error::InvalidMaxBounds { variable, value } =>
variable@ == p + 1 && value == ceilings@[p],
_ => false,
}
},
})]
#[allow(
clippy::manual_range_contains,
reason = "`RangeInclusive::contains` is not a terminating call for Creusot"
)]
pub fn new(ceilings: Vec<i32>) -> Result<MaxBounds, Error> {
if ceilings.len() > MAX_VARIABLES {
return Err(Error::TooManyVariables {
requested: ceilings.len(),
});
}
let mut p: usize = 0;
#[invariant(p@ <= ceilings@.len())]
#[invariant(forall<q: Int> 0 <= q && q < p@ ==> valid_ceiling(ceilings@[q]))]
#[variant(ceilings@.len() - p@)]
while p < ceilings.len() {
let value = ceilings[p];
if value < 0 || value > MAX_CONSTANT {
return Err(Error::InvalidMaxBounds {
variable: p + 1,
value,
});
}
p += 1;
}
Ok(MaxBounds {
ceilings: ceilings.into_boxed_slice(),
})
}
#[check(terminates)]
#[ensures(result@ == self.variables_logic())]
pub fn variables(&self) -> usize {
self.ceilings.len()
}
#[check(terminates)]
#[ensures(match result {
Some(c) => x@ <= self.variables_logic() && c == self.ceilings_logic()[x@ - 1],
None => x@ > self.variables_logic(),
})]
pub fn get(&self, x: Variable) -> Option<i32> {
let p = x.index() - 1;
if p < self.ceilings.len() {
Some(self.ceilings[p])
} else {
None
}
}
#[check(terminates)]
#[requires(i@ <= self.variables_logic())]
#[ensures(result@ == self.kmap()[i@])]
#[ensures(valid_ceiling(result))]
fn k(&self, i: usize) -> i32 {
if i == 0 { 0 } else { self.ceilings[i - 1] }
}
}
#[check(terminates)]
#[ensures(forall<t: Int> up@ == BoundModel::Le(t) ==> result@ == proof::norm_upper(e@, t))]
fn norm_upper(e: Bound, up: Bound) -> Bound {
snapshot!(order_refines(up, e));
if tighter(up, e) { Bound::INFINITY } else { e }
}
#[check(terminates)]
#[ensures(forall<t: Int> low@ == BoundModel::Lt(t) ==> result@ == proof::norm_lower(e@, t))]
fn norm_lower(e: Bound, low: Bound) -> Bound {
snapshot!(order_refines(e, low));
if tighter(e, low) { low } else { e }
}
#[check(terminates)]
#[ensures(result@ == proof::norm_diag(e@))]
fn norm_diag(e: Bound) -> Bound {
snapshot!(order_refines(e, Bound::LE_ZERO));
snapshot!(view_injective(e, Bound::LE_ZERO));
if tighter(e, Bound::LE_ZERO) {
Bound::LT_ZERO
} else if same(e, Bound::LE_ZERO) {
e
} else {
Bound::INFINITY
}
}
#[check(terminates)]
#[requires(valid_ceiling(k))]
#[ensures(result.0@ == BoundModel::Le(k@) && result.1@ == BoundModel::Lt(-k@))]
fn thresholds(k: i32) -> (Bound, Bound) {
match (Bound::le(k), Bound::lt(-k)) {
(Ok(up), Ok(low)) => (up, low),
_ => unreachable!(),
}
}
impl Dbm {
#[check(terminates)]
#[ensures(match result {
Ok(r) => bounds.variables_logic() == self.variables_logic()
&& r.variables_logic() == self.variables_logic()
&& (self.is_empty_logic() ==> r.is_empty_logic())
&& (!self.is_empty_logic() ==> forall<u: crate::dbm::proof::Val> r.zone(u) ==
crate::dbm::proof::mzone(proof::norm(self.mmat(), bounds.kmap()),
self.variables_logic() + 1, u))
&& (forall<u: crate::dbm::proof::Val> self.zone(u) ==> r.zone(u))
&& forall<z: Dbm> inv(z) && z.variables_logic() == r.variables_logic()
&& (forall<u: crate::dbm::proof::Val> z.zone(u) == r.zone(u))
==> z.deep_model() == r.deep_model(),
Err(e) => if bounds.variables_logic() != self.variables_logic() {
match e {
Error::DimensionMismatch { expected, found } =>
expected@ == self.variables_logic() && found@ == bounds.variables_logic(),
_ => false,
}
} else {
e == Error::Range && !self.is_empty_logic()
&& mrange_failure(proof::norm(self.mmat(), bounds.kmap()),
self.variables_logic() + 1)
},
})]
pub fn try_extrapolate_max(self, bounds: &MaxBounds, ws: &mut Workspace) -> Result<Dbm, Error> {
if let Err(e) = self.check_variables(bounds.variables()) {
return Err(e);
}
snapshot!(self.model());
snapshot!(bounds.model());
let z0 = snapshot!(self);
let nf = snapshot!(proof::norm(self.mmat(), bounds.kmap()));
snapshot!(proof::norm_extensive(
self.mmat(),
bounds.kmap(),
self.variables_logic() + 1
));
let empty = self.empty_like();
let mut m = match self.into_matrix() {
Some(m) => m,
None => {
proof_assert!(forall<z: Dbm> inv(z) && z.variables_logic() == empty.variables_logic()
&& (forall<u: crate::dbm::proof::Val> z.zone(u) == empty.zone(u)) ==> {
Dbm::eq_semantic(z, empty);
z.deep_model() == empty.deep_model()
});
return Ok(empty);
}
};
let w0 = snapshot!(m);
let d = snapshot!(m.dim_logic());
snapshot!(w0.model());
let changed = normalize(&mut m, bounds);
snapshot!(m.model());
proof_assert!(meq(m.mmat(), *nf, *d));
snapshot!(meq_transfer(m.mmat(), *nf, *d));
let r = if changed {
snapshot!(proof::norm_ready(w0.mmat(), bounds.kmap(), *d));
ws.select_all(m.dimension());
snapshot!(Matrix::full_pivot_ready(m, ws.touched@));
let w1 = snapshot!(m);
let r = m.close_pivots(&ws.touched);
proof_assert!(match r {
Err(_) => {
w1.range_failure_model();
mrange_failure_meq(w1.mmat(), *nf, *d);
true
}
Ok(_) => true,
});
r
} else {
proof_assert!(forall<a: Int, b: Int> 0 <= a && a < *d && 0 <= b && b < *d ==> {
view_injective(m.at(a, b), w0.at(a, b));
m.at(a, b)@ == w0.at(a, b)@
});
proof_assert!(meq(m.mmat(), w0.mmat(), *d));
snapshot!(meq_transfer(m.mmat(), w0.mmat(), *d));
Ok(m.publish_canonical())
};
proof_assert!(match r {
Ok(y) => forall<z: Dbm> inv(z) && z.variables_logic() == y.variables_logic()
&& (forall<u: crate::dbm::proof::Val> z.zone(u) == y.zone(u)) ==> {
Dbm::eq_semantic(z, y);
z.deep_model() == y.deep_model()
},
Err(_) => true,
});
proof_assert!(forall<u: crate::dbm::proof::Val> z0.zone(u) ==>
crate::dbm::proof::mzone(*nf, *d, u));
r
}
}
#[check(terminates)]
#[requires(bounds.variables_logic() + 1 == m.dim_logic())]
#[ensures((^m).dim_logic() == m.dim_logic())]
#[ensures(forall<a: Int, b: Int>
0 <= a && a < m.dim_logic() && 0 <= b && b < m.dim_logic() ==>
(^m).at(a, b)@ == proof::norm_entry(m.at(a, b)@, bounds.kmap(), a, b))]
#[ensures(result == exists<a: Int, b: Int>
0 <= a && a < m.dim_logic() && 0 <= b && b < m.dim_logic() && (^m).at(a, b).deep_model() != m.at(a, b).deep_model())]
fn normalize(m: &mut Matrix, bounds: &MaxBounds) -> bool {
let m0 = snapshot!(*m);
let k = snapshot!(bounds.kmap());
snapshot!(bounds.model());
let mut changed = false;
let d = m.dimension();
let mut i: usize = 0;
#[invariant(i@ <= d@ && m.dim_logic() == d@)]
#[invariant(forall<a: Int, b: Int> 0 <= a && a < d@ && 0 <= b && b < d@ ==>
if a < i@ {
m.at(a, b)@ == proof::norm_entry(m0.at(a, b)@, *k, a, b)
} else {
m.at(a, b) == m0.at(a, b)
})]
#[invariant(changed == exists<a: Int, b: Int>
0 <= a && a < i@ && 0 <= b && b < d@ && m.at(a, b).deep_model() != m0.at(a, b).deep_model())]
#[variant(d@ - i@)]
while i < d {
let (up, _) = thresholds(bounds.k(i));
let mut j: usize = 0;
#[invariant(j@ <= d@ && m.dim_logic() == d@)]
#[invariant(forall<a: Int, b: Int> 0 <= a && a < d@ && 0 <= b && b < d@ ==>
if a < i@ || (a == i@ && b < j@) {
m.at(a, b)@ == proof::norm_entry(m0.at(a, b)@, *k, a, b)
} else {
m.at(a, b) == m0.at(a, b)
})]
#[invariant(changed == exists<a: Int, b: Int>
0 <= a && a < d@ && 0 <= b && b < d@ && (a < i@ || (a == i@ && b < j@))
&& m.at(a, b).deep_model() != m0.at(a, b).deep_model())]
#[variant(d@ - j@)]
while j < d {
let e = m.get(i, j);
let normed = if i == j {
norm_diag(e)
} else {
let (_, low) = thresholds(bounds.k(j));
norm_lower(norm_upper(e, up), low)
};
proof_assert!(normed@ == proof::norm_entry(e@, *k, i@, j@));
snapshot!(view_injective(normed, e));
if !same(normed, e) {
m.set(i, j, normed);
changed = true;
}
j += 1;
}
i += 1;
}
changed
}
#[cfg(creusot)]
mod proof;
#[cfg(test)]
mod tests;