#[macro_export]
macro_rules! verify {
($p:expr $(,)?) => {{
let verdict = $crate::smt::prove($p);
assert!(
verdict.is_proved(),
"verif: verification obligation failed: {}",
verdict
);
verdict
}};
}
#[macro_export]
macro_rules! verify_implies {
($pre:expr, $post:expr $(,)?) => {{
let verdict = $crate::smt::prove_implies($pre, $post);
assert!(
verdict.is_proved(),
"verif: implication failed: {}",
verdict
);
verdict
}};
}
#[macro_export]
macro_rules! contract {
(requires $pre:expr, ensures $post:expr $(,)?) => {{
$crate::vc::ensures($crate::vc::requires($pre), $post).discharge()
}};
}
#[macro_export]
macro_rules! proof {
($p:expr $(,)?) => {{
match $crate::proof::Certificate::discharge($p) {
::core::option::Option::Some(c) => c,
::core::option::Option::None => {
panic!("verif: could not prove the stated proposition")
}
}
}};
}
#[macro_export]
macro_rules! assert_proof {
($p:ty) => {{
fn _verif_assert_proof<T: $crate::proof::Theorem>() {}
_verif_assert_proof::<$p>();
}};
}
#[macro_export]
macro_rules! check {
($p:expr, $value:expr $(,)?) => {{
$crate::contract::assert_that($p, $value)
}};
}
#[macro_export]
macro_rules! verify_exhaustive {
($a:ident : u8, $b:ident : u8 => $prop:expr $(,)?) => {{
match $crate::exhaustive::find_violation_u8_2(|$a, $b| $prop) {
::core::option::Option::Some((a, b)) => panic!(
"verif: exhaustive verification failed: counterexample {} = {}, {} = {}",
::core::stringify!($a),
a,
::core::stringify!($b),
b
),
::core::option::Option::None => {}
}
}};
($a:ident : u8 => $prop:expr $(,)?) => {{
match $crate::exhaustive::find_violation_u8(|$a| $prop) {
::core::option::Option::Some(a) => panic!(
"verif: exhaustive verification failed: counterexample {} = {}",
::core::stringify!($a),
a
),
::core::option::Option::None => {}
}
}};
($a:ident : u16 => $prop:expr $(,)?) => {{
match $crate::exhaustive::find_violation_u16(|$a| $prop) {
::core::option::Option::Some(a) => panic!(
"verif: exhaustive verification failed: counterexample {} = {}",
::core::stringify!($a),
a
),
::core::option::Option::None => {}
}
}};
}
#[macro_export]
macro_rules! spec {
($($clause:ident : $body:expr,)*) => {};
}
#[cfg(test)]
mod tests {
use crate::smt;
use crate::smt::var;
#[test]
fn verify_macro_accepts_truths() {
smt::with_fresh(|| {
let x = var(8);
verify!(x.lt_val(16).iff(x.lt_val(16)));
});
}
#[test]
#[should_panic(expected = "verification obligation failed")]
fn verify_macro_rejects_falsehoods() {
smt::with_fresh(|| {
let x = var(8);
verify!(x.lt_val(4));
});
}
#[test]
fn contract_macro_works() {
smt::with_fresh(|| {
let x = var(8);
let v = contract! { requires x.lt_val(16), ensures x.lt_val(32) };
assert!(v.is_proved());
});
}
#[test]
fn proof_macro_yields_a_certificate() {
smt::with_fresh(|| {
let x = var(8);
let _c = proof!(x.lt_val(16).iff(x.lt_val(16)));
});
}
#[test]
fn check_macro_checks_concrete_values() {
use crate::contract::{Obligation, Pred};
assert!(check!(Pred::Lt(10), 9).is_ok());
let err = check!(Pred::Lt(10), 10).unwrap_err();
assert_eq!(err.kind, Obligation::Assertion);
assert_eq!(err.value, 10);
}
#[test]
fn exhaustive_macro_works() {
verify_exhaustive!(a: u8, b: u8 =>
(a as u16 + b as u16 > 255) == (a.wrapping_add(b) < a));
verify_exhaustive!(a: u8 => a.wrapping_add(0) == a);
}
#[test]
#[should_panic(expected = "exhaustive verification failed: counterexample a = 128")]
fn exhaustive_macro_rejects_falsehoods() {
verify_exhaustive!(a: u8 => a < 128);
}
}