#![allow(clippy::question_mark)]
use crate::bounds::{Bound, Clock, Variable};
#[cfg(creusot)]
use crate::bounds::{MAX_CONSTANT, neg_in_range, variable_not_zero, view_representable};
use crate::dbm::Dbm;
#[cfg(creusot)]
use crate::dbm::proof::{meq, meq_transfer};
use crate::error::Error;
#[cfg(creusot)]
use creusot_std::logic::real::Real;
use creusot_std::prelude::*;
impl Dbm {
#[check(terminates)]
#[ensures(result.variables_logic() == self.variables_logic())]
#[ensures(result.is_empty_logic() == self.is_empty_logic())]
#[ensures(forall<v: crate::dbm::proof::Val> result.zone(v) ==
exists<u: crate::dbm::proof::Val, t: Real> Real::from_int(0) <= t && self.zone(u)
&& proof::shifted(u, t, v, self.variables_logic() + 1))]
pub fn future(self) -> Dbm {
let z = snapshot!(self);
snapshot!(self.model());
let empty = self.empty_like();
let mut m = match self.into_matrix() {
Some(m) => m,
None => return empty,
};
snapshot!(m.dim_pos());
let d = snapshot!(m.dim_logic());
let f = snapshot!(m.mmat());
let m0 = snapshot!(m);
snapshot!(m0.model());
snapshot!(proof::up_canonical(*f, *d));
snapshot!(proof::up_zone(*f, *d));
m.fill_column(0, Bound::INFINITY);
m.set(0, 0, Bound::LE_ZERO);
snapshot!(m.model());
proof_assert!(meq(m.mmat(), proof::up(*f), *d));
snapshot!(meq_transfer(m.mmat(), proof::up(*f), *d));
proof_assert!(forall<v: crate::dbm::proof::Val> m.zone(v) ==
exists<u: crate::dbm::proof::Val, t: Real> Real::from_int(0) <= t && z.zone(u)
&& proof::shifted(u, t, v, *d));
m.publish_canonical()
}
#[check(terminates)]
#[ensures(result.variables_logic() == self.variables_logic())]
#[ensures(result.is_empty_logic() == self.is_empty_logic())]
#[ensures(forall<v: crate::dbm::proof::Val> result.zone(v) ==
(crate::dbm::proof::valuation(self.variables_logic() + 1, v)
&& exists<u: crate::dbm::proof::Val, t: Real> Real::from_int(0) <= t && self.zone(u)
&& proof::shifted(v, t, u, self.variables_logic() + 1)))]
pub fn past(self) -> Dbm {
let z = snapshot!(self);
snapshot!(self.model());
let empty = self.empty_like();
let mut m = match self.into_matrix() {
Some(m) => m,
None => return empty,
};
snapshot!(m.dim_pos());
let d = snapshot!(m.dim_logic());
let f = snapshot!(m.mmat());
let m0 = snapshot!(m);
snapshot!(m0.model());
proof_assert!(forall<i: Int, j: Int> 0 <= i && i < *d && 0 <= j && j < *d ==> {
view_representable(m0.at(i, j)); f[i][j].representable()
});
snapshot!(proof::down_canonical(*f, *d));
snapshot!(proof::down_zone(*f, *d));
m.fill_row(0, Bound::LE_ZERO);
let dim = m.dimension();
let mut i: usize = 1;
#[invariant(1 <= i@ && i@ <= *d && m.dim_logic() == *d)]
#[invariant(forall<k: Int, l: Int> 1 <= k && k < *d && 0 <= l && l < *d ==>
m.at(k, l) == m0.at(k, l))]
#[invariant(forall<l: Int> 0 <= l && l < *d ==> m.at(0, l)@ == proof::pmin(*f, i@, l))]
#[variant(*d - i@)]
while i < dim {
let before = snapshot!(m);
proof_assert!(forall<l: Int> 0 <= l && l < *d ==>
before.at(i@, l)@ == f[i@][l] && f[i@][l].representable());
let overflow = m.relax_row(0, i, Bound::LE_ZERO);
proof_assert!(forall<l: Int> 0 <= l && l < *d ==> {
proof::relax_le_zero(before.at(0, l)@, f[i@][l], m.at(0, l)@);
m.at(0, l)@ == proof::pmin(*f, i@ + 1, l)
});
proof_assert!(!overflow);
debug_assert!(!overflow);
i += 1;
}
snapshot!(m.model());
proof_assert!(meq(m.mmat(), proof::down(*f, *d), *d));
snapshot!(meq_transfer(m.mmat(), proof::down(*f, *d), *d));
proof_assert!(forall<v: crate::dbm::proof::Val> m.zone(v) ==
(crate::dbm::proof::valuation(*d, v)
&& exists<u: crate::dbm::proof::Val, t: Real> Real::from_int(0) <= t && z.zone(u)
&& proof::shifted(v, t, u, *d)));
m.publish_canonical()
}
#[check(terminates)]
#[ensures(match result {
Ok(r) => x@ <= self.variables_logic()
&& r.variables_logic() == self.variables_logic()
&& r.is_empty_logic() == self.is_empty_logic()
&& forall<v: crate::dbm::proof::Val> r.zone(v) ==
exists<u: crate::dbm::proof::Val> self.zone(u) && v[x@] == Real::from_int(0)
&& proof::agree_except(u, v, self.variables_logic() + 1, x@),
Err(e) => x@ > self.variables_logic() && match e {
Error::ClockOutOfRange { clock, variables } =>
clock@ == x@ && variables@ == self.variables_logic(),
_ => false,
},
})]
pub fn try_reset_zero(self, x: Variable) -> Result<Dbm, Error> {
self.try_reset(x, 0)
}
#[check(terminates)]
#[ensures(match result {
Ok(r) => x@ <= self.variables_logic() && 0 <= value@
&& (self.is_empty_logic() || value@ <= MAX_CONSTANT@)
&& r.variables_logic() == self.variables_logic()
&& r.is_empty_logic() == self.is_empty_logic()
&& forall<v: crate::dbm::proof::Val> r.zone(v) ==
exists<u: crate::dbm::proof::Val> self.zone(u) && v[x@] == Real::from_int(value@)
&& proof::agree_except(u, v, self.variables_logic() + 1, x@),
Err(e) => match e {
Error::ClockOutOfRange { clock, variables } => x@ > self.variables_logic()
&& clock@ == x@ && variables@ == self.variables_logic(),
Error::NegativeConstant { value: v } =>
x@ <= self.variables_logic() && value@ < 0 && v == value,
Error::Range => x@ <= self.variables_logic() && 0 <= value@
&& !self.is_empty_logic() && MAX_CONSTANT@ < value@,
_ => false,
},
})]
pub fn try_reset(self, x: Variable, value: i32) -> Result<Dbm, Error> {
let z = snapshot!(self);
snapshot!(self.model());
snapshot!(variable_not_zero(x));
let k = match self.check_clock(Clock::from(x)) {
Ok(k) => k,
Err(e) => return Err(e),
};
if value < 0 {
return Err(Error::NegativeConstant { value });
}
let empty = self.empty_like();
let mut m = match self.into_matrix() {
Some(m) => m,
None => return Ok(empty),
};
snapshot!(if crate::bounds::BoundModel::in_range(value@) { neg_in_range(value@) });
let (up, down) = match (Bound::le(value), Bound::le(-value)) {
(Ok(up), Ok(down)) => (up, down),
_ => return Err(Error::Range),
};
proof_assert!(value@ <= MAX_CONSTANT@);
snapshot!(m.dim_pos());
let d = snapshot!(m.dim_logic());
let f = snapshot!(m.mmat());
let m0 = snapshot!(m);
snapshot!(m0.model());
proof_assert!(forall<i: Int, j: Int> 0 <= i && i < *d && 0 <= j && j < *d ==> {
view_representable(m0.at(i, j)); f[i][j].representable()
});
snapshot!(proof::reset_representable(*f, *d, value@));
snapshot!(proof::cancel(value@));
snapshot!(proof::reset_canonical(*f, *d, k@, value@));
snapshot!(proof::reset_zone(*f, *d, k@, value@));
m.add_weak_to_row(k, 0, up);
let m1 = snapshot!(m);
proof_assert!(forall<i: Int, j: Int> 0 <= i && i < *d && 0 <= j && j < *d ==>
m1.at(i, j)@ == if i == k@ {
crate::bounds::BoundModel::Le(value@).add(f[0][j])
} else {
f[i][j]
});
proof_assert!(forall<i: Int> 0 <= i && i < *d ==>
down@.add_in_range(m1.at(i, 0)@));
m.add_weak_to_column(k, 0, down);
let m2 = snapshot!(m);
proof_assert!(forall<i: Int, j: Int> 0 <= i && i < *d && 0 <= j && j < *d ==>
m2.at(i, j)@ == if j == k@ {
crate::bounds::BoundModel::Le(-value@).add(m1.at(i, 0)@)
} else {
m1.at(i, j)@
});
snapshot!(m.model());
proof_assert!(forall<i: Int, j: Int> 0 <= i && i < *d && 0 <= j && j < *d ==>
m.mmat()[i][j] == m2.at(i, j)@);
proof_assert!(meq(m.mmat(), proof::reset(*f, k@, value@), *d));
snapshot!(meq_transfer(m.mmat(), proof::reset(*f, k@, value@), *d));
proof_assert!(forall<v: crate::dbm::proof::Val> m.zone(v) ==
exists<u: crate::dbm::proof::Val> z.zone(u) && v[k@] == Real::from_int(value@)
&& proof::agree_except(u, v, *d, k@));
Ok(m.publish_canonical())
}
#[check(terminates)]
#[ensures(match result {
Ok(r) => x@ <= self.variables_logic()
&& r.variables_logic() == self.variables_logic()
&& r.is_empty_logic() == self.is_empty_logic()
&& forall<v: crate::dbm::proof::Val> r.zone(v) ==
exists<u: crate::dbm::proof::Val> self.zone(u) && Real::from_int(0) <= v[x@]
&& proof::agree_except(u, v, self.variables_logic() + 1, x@),
Err(e) => x@ > self.variables_logic() && match e {
Error::ClockOutOfRange { clock, variables } =>
clock@ == x@ && variables@ == self.variables_logic(),
_ => false,
},
})]
pub fn try_free(self, x: Variable) -> Result<Dbm, Error> {
snapshot!(self.model());
snapshot!(variable_not_zero(x));
let k = match self.check_clock(Clock::from(x)) {
Ok(k) => k,
Err(e) => return Err(e),
};
let empty = self.empty_like();
let mut m = match self.into_matrix() {
Some(m) => m,
None => return Ok(empty),
};
snapshot!(m.dim_pos());
let d = snapshot!(m.dim_logic());
let f = snapshot!(m.mmat());
let m0 = snapshot!(m);
snapshot!(m0.model());
snapshot!(proof::free_canonical(*f, *d, k@));
snapshot!(proof::free_zone(*f, *d, k@));
m.copy_column(k, 0);
m.fill_row(k, Bound::INFINITY);
m.set(k, k, Bound::LE_ZERO);
snapshot!(m.model());
proof_assert!(meq(m.mmat(), proof::free(*f, k@), *d));
snapshot!(meq_transfer(m.mmat(), proof::free(*f, k@), *d));
Ok(m.publish_canonical())
}
}
#[cfg(creusot)]
mod proof;
#[cfg(test)]
mod tests;