Macro quantify

Source
macro_rules! quantify {
    (forall $x:ident in $xs:expr => $cond:expr) => { ... };
    (exists $x:ident in $xs:expr => $cond:expr) => { ... };
    (none $x:ident in $xs:expr => $cond:expr) => { ... };
    (exactly_one $x:ident in $xs:expr => $cond:expr) => { ... };
    (all_equal $x:ident in $xs:expr) => { ... };
    (pairwise $x:ident in $xs:expr => $cond:expr) => { ... };
    (existsforall $a:ident in $as:expr, $b:ident in $bs:expr => $cond:expr) => { ... };
    (forallexists $a:ident in $as:expr, $b:ident in $bs:expr => $cond:expr) => { ... };
    ($($t:tt)*) => { ... };
}
Expand description

Evaluates logical quantifiers using mathematical-style syntax over iterables.

This macro acts as a declarative frontend to the basic quantifier functions in quantor, allowing expressive, readable logic similar to mathematics.

§Supported Quantifiers

  • forall x in &a => predicate
  • exists x in &a => predicate
  • none x in &a => predicate
  • exactly_one x in &a => predicate
  • all_equal x in &a => predicate
  • pairwise x in &a => predicate
  • forallexists x in &a, y in &b => predicate
  • existsforall x in &a, y in &b => predicate

See the quantifier functions (e.g. forall) for behavior.

§Examples

use quantor::quantify;

let xs = vec!(2, 4, 6);
assert!(quantify!(forall x in &xs => x % 2 == 0));

let ys = vec!(1, 3, 5);
assert!(quantify!(exists x in &ys => *x == 3));
 
let numbers = vec!(1, 1, 1);
assert!(quantify!(all_equal x in &numbers));

let a = vec!(1, 2);
let b = vec!(3, 4);
assert!(quantify!(forallexists x in &a, y in &b => x < y));