use crate::proof::Certificate;
use crate::smt::{self, Pred, Var, Verdict};
#[derive(Debug, Clone, Copy)]
pub struct Precondition(pub(crate) Pred);
#[derive(Debug, Clone, Copy)]
pub struct Contract {
pub(crate) pre: Pred,
pub(crate) post: Pred,
}
pub fn requires(pre: Pred) -> Precondition {
Precondition(pre)
}
pub fn ensures(pre: Precondition, post: Pred) -> Contract {
Contract { pre: pre.0, post }
}
impl Contract {
pub fn discharge(&self) -> Verdict {
smt::prove_implies(self.pre, self.post)
}
pub fn precondition_is_satisfiable(&self) -> bool {
matches!(
self.precondition_satisfiability(),
Verdict::Valid | Verdict::Counterexample(_)
)
}
pub fn precondition_satisfiability(&self) -> Verdict {
smt::find_model(self.pre)
}
pub fn precondition(&self) -> Pred {
self.pre
}
pub fn postcondition(&self) -> Pred {
self.post
}
pub fn certify(&self) -> Option<Certificate> {
Certificate::discharge(smt::implies(self.pre, self.post))
}
}
pub fn verify_contract(pre: Pred, post: Pred) -> Verdict {
smt::prove_implies(pre, post)
}
pub fn invariant(init: Pred, keep: Pred) -> Verdict {
smt::prove_implies(init, keep)
}
pub fn verify_body<B, P>(pre: impl Fn(Var) -> Pred, post: P, body: B) -> Verdict
where
B: Fn(Var) -> Var,
P: Fn(Var, Var) -> Pred,
{
smt::with_fresh(|| {
let input = smt::var(32);
let pre_p = pre(input);
let output = body(input);
let post_p = post(input, output);
smt::prove_implies(pre_p, post_p)
})
}
#[derive(Debug, Clone, Copy)]
pub struct Verified<T> {
value: T,
statement: Pred,
_cert: Certificate,
}
impl<T> Verified<T> {
pub fn new(value: T, statement: Pred) -> Option<Self> {
Certificate::discharge(statement)
.map(|cert| Verified { value, statement, _cert: cert })
}
pub fn from_certificate(value: T, cert: Certificate) -> Option<Self> {
cert.statement()
.map(|statement| Verified { value, statement, _cert: cert })
}
pub fn statement(&self) -> Pred {
self.statement
}
pub fn get(&self) -> &T {
&self.value
}
pub fn into_inner(self) -> T {
self.value
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn contract_is_discharged() {
let v = smt::with_fresh(|| {
let x = smt::var(8);
let c = ensures(requires(smt::ult_const(x, 16)), smt::ult_const(x, 32));
c.discharge()
});
assert!(v.is_proved());
}
#[test]
fn an_undecided_precondition_is_not_reported_satisfiable() {
let v = smt::with_fresh(|| {
let x = smt::var(8);
let y = smt::var(16);
let pre = smt::eq(x, y);
let c = ensures(requires(pre), smt::truth(true));
(c.precondition_satisfiability(), c.precondition_is_satisfiable())
});
assert!(
!v.1,
"an undecided precondition must not be reported satisfiable (verdict: {})",
v.0
);
}
#[test]
fn satisfiability_distinguishes_a_model_from_an_undecided_query() {
let (sat, unsat) = smt::with_fresh(|| {
let x = smt::var(8);
let good = ensures(requires(smt::ult_const(x, 16)), smt::truth(true));
let bad = ensures(requires(smt::and(smt::ult_const(x, 3), smt::ugt_const(x, 10))), smt::truth(true));
(good.precondition_satisfiability(), bad.precondition_satisfiability())
});
assert!(
matches!(sat, Verdict::Counterexample(_)) || sat == Verdict::Valid,
"a satisfiable precondition must yield a model or `Valid`, got {sat}"
);
assert_eq!(unsat, Verdict::Unsat, "an unsatisfiable precondition must be `Unsat`");
}
#[test]
fn a_vacuous_contract_is_visible_through_the_satisfiability_verdict() {
let (discharged, satisfiability) = smt::with_fresh(|| {
let x = smt::var(8);
let c = ensures(
requires(smt::and(smt::ult_const(x, 3), smt::ugt_const(x, 10))),
smt::ult_const(x, 100),
);
(c.discharge(), c.precondition_satisfiability())
});
assert!(discharged.is_proved(), "the implication is vacuously valid");
assert_eq!(
satisfiability,
Verdict::Unsat,
"and the precondition is the reason: no input satisfies it"
);
}
#[test]
fn weak_contract_is_refuted() {
let v = smt::with_fresh(|| {
let x = smt::var(8);
let c = ensures(requires(smt::ult_const(x, 32)), smt::ult_const(x, 16));
c.discharge()
});
assert!(matches!(v, Verdict::Counterexample(_)));
}
#[test]
fn body_verification_works() {
let v = verify_body(
|x| smt::ult_const(x, 100),
|_x, y| smt::ule_const(y, 100),
|x| smt::add(x, smt::word(1, 32)),
);
assert!(v.is_proved(), "got {}", v);
}
#[test]
fn loop_invariant_holds() {
let v = smt::with_fresh(|| {
let i = smt::var(8);
let init = smt::ule_const(i, 10);
let ip = smt::add(i, smt::word(1, 8));
let guard = smt::ult_const(i, 10);
let keep = smt::implies(smt::and(init, guard), smt::ule_const(ip, 10));
invariant(init, keep)
});
assert!(v.is_proved(), "got {}", v);
}
}