use crate::{error::QuantorKind, QuantorError};
#[inline]
#[must_use = "Quantifier results must be checked. Use `.is_ok()` or `?` to handle them."]
pub fn forallexists<'a, A: 'a, B: 'a>(
a: impl IntoIterator<Item = &'a A>,
b: impl IntoIterator<Item = &'a B>,
pred: impl Fn(&A, &B) -> bool,
) -> Result<(), QuantorError> {
let b_vec: Vec<&'a B> = b.into_iter().collect();
let mut matched;
for (outer_index, item_a) in a.into_iter().enumerate() {
matched = false;
for item_b in &b_vec {
if pred(item_a, item_b) {
matched = true;
break;
}
}
if !matched {
return Err(QuantorError::ForAllExistsFailed { kind: QuantorKind::ForAllExists, outer_index });
}
}
Ok(())
}
#[inline]
#[must_use = "Quantifier results must be checked. Use `.is_ok()` or `?` to handle them."]
pub fn existsforall<'a, A: 'a, B: 'a>(
a: impl IntoIterator<Item = &'a A>,
b: impl IntoIterator<Item = &'a B>,
pred: impl Fn(&A, &B) -> bool,
) -> Result<(), QuantorError> {
let b_vec: Vec<&'a B> = b.into_iter().collect();
let mut first_index = None;
for (index, item_a) in a.into_iter().enumerate() {
let mut all_match = true;
for item_b in &b_vec {
if !pred(item_a, item_b) {
all_match = false;
break;
}
}
if all_match {
return Ok(());
}
if first_index.is_none() {
first_index = Some(index)
}
}
Err(QuantorError::ExistsForAllFailed { kind: QuantorKind::ExistsForAll, outer_index: first_index.unwrap_or(0) })
}